Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: chrome/browser/net/sqlite_persistent_cookie_store_unittest.cc

Issue 8533013: SessionRestore: Store session cookies and restore them if chrome crashes or auto-restarts. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Code review. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/net/sqlite_persistent_cookie_store.cc ('k') | chrome/browser/profiles/profile.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/memory/ref_counted.h" 7 #include "base/memory/ref_counted.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/scoped_temp_dir.h" 9 #include "base/scoped_temp_dir.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 void WaitOnDBEvent() { 54 void WaitOnDBEvent() {
55 db_thread_event_.Wait(); 55 db_thread_event_.Wait();
56 } 56 }
57 57
58 virtual void SetUp() { 58 virtual void SetUp() {
59 ui_thread_.Start(); 59 ui_thread_.Start();
60 db_thread_.Start(); 60 db_thread_.Start();
61 io_thread_.Start(); 61 io_thread_.Start();
62 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 62 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
63 store_ = new SQLitePersistentCookieStore( 63 store_ = new SQLitePersistentCookieStore(
64 temp_dir_.path().Append(chrome::kCookieFilename)); 64 temp_dir_.path().Append(chrome::kCookieFilename), false);
65 std::vector<net::CookieMonster::CanonicalCookie*> cookies; 65 std::vector<net::CookieMonster::CanonicalCookie*> cookies;
66 Load(&cookies); 66 Load(&cookies);
67 ASSERT_EQ(0u, cookies.size()); 67 ASSERT_EQ(0u, cookies.size());
68 // Make sure the store gets written at least once. 68 // Make sure the store gets written at least once.
69 store_->AddCookie( 69 store_->AddCookie(
70 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", "http://foo.bar", 70 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", "http://foo.bar",
71 "/", std::string(), std::string(), 71 "/", std::string(), std::string(),
72 base::Time::Now(), 72 base::Time::Now(),
73 base::Time::Now(), 73 base::Time::Now(),
74 base::Time::Now(), 74 base::Time::Now(),
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 // Replace the store effectively destroying the current one and forcing it 124 // Replace the store effectively destroying the current one and forcing it
125 // to write it's data to disk. Then we can see if after loading it again it 125 // to write it's data to disk. Then we can see if after loading it again it
126 // is still there. 126 // is still there.
127 store_ = NULL; 127 store_ = NULL;
128 scoped_refptr<base::ThreadTestHelper> helper( 128 scoped_refptr<base::ThreadTestHelper> helper(
129 new base::ThreadTestHelper( 129 new base::ThreadTestHelper(
130 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); 130 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)));
131 // Make sure we wait until the destructor has run. 131 // Make sure we wait until the destructor has run.
132 ASSERT_TRUE(helper->Run()); 132 ASSERT_TRUE(helper->Run());
133 store_ = new SQLitePersistentCookieStore( 133 store_ = new SQLitePersistentCookieStore(
134 temp_dir_.path().Append(chrome::kCookieFilename)); 134 temp_dir_.path().Append(chrome::kCookieFilename), false);
135 135
136 // Reload and test for persistence 136 // Reload and test for persistence
137 Load(&cookies); 137 Load(&cookies);
138 ASSERT_EQ(1U, cookies.size()); 138 ASSERT_EQ(1U, cookies.size());
139 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); 139 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str());
140 ASSERT_STREQ("A", cookies[0]->Name().c_str()); 140 ASSERT_STREQ("A", cookies[0]->Name().c_str());
141 ASSERT_STREQ("B", cookies[0]->Value().c_str()); 141 ASSERT_STREQ("B", cookies[0]->Value().c_str());
142 142
143 // Now delete the cookie and check persistence again. 143 // Now delete the cookie and check persistence again.
144 store_->DeleteCookie(*cookies[0]); 144 store_->DeleteCookie(*cookies[0]);
145 store_ = NULL; 145 store_ = NULL;
146 // Make sure we wait until the destructor has run. 146 // Make sure we wait until the destructor has run.
147 ASSERT_TRUE(helper->Run()); 147 ASSERT_TRUE(helper->Run());
148 STLDeleteContainerPointers(cookies.begin(), cookies.end()); 148 STLDeleteContainerPointers(cookies.begin(), cookies.end());
149 cookies.clear(); 149 cookies.clear();
150 store_ = new SQLitePersistentCookieStore( 150 store_ = new SQLitePersistentCookieStore(
151 temp_dir_.path().Append(chrome::kCookieFilename)); 151 temp_dir_.path().Append(chrome::kCookieFilename), false);
152 152
153 // Reload and check if the cookie has been removed. 153 // Reload and check if the cookie has been removed.
154 Load(&cookies); 154 Load(&cookies);
155 ASSERT_EQ(0U, cookies.size()); 155 ASSERT_EQ(0U, cookies.size());
156 } 156 }
157 157
158 // Test that priority load of cookies for a specfic domain key could be 158 // Test that priority load of cookies for a specfic domain key could be
159 // completed before the entire store is loaded 159 // completed before the entire store is loaded
160 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { 160 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) {
161 base::Time t = base::Time::Now() + base::TimeDelta::FromInternalValue(10); 161 base::Time t = base::Time::Now() + base::TimeDelta::FromInternalValue(10);
(...skipping 17 matching lines...) Expand all
179 t, t, t, false, false, true, true)); 179 t, t, t, false, false, true, true));
180 store_ = NULL; 180 store_ = NULL;
181 181
182 scoped_refptr<base::ThreadTestHelper> helper( 182 scoped_refptr<base::ThreadTestHelper> helper(
183 new base::ThreadTestHelper( 183 new base::ThreadTestHelper(
184 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); 184 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)));
185 // Make sure we wait until the destructor has run. 185 // Make sure we wait until the destructor has run.
186 ASSERT_TRUE(helper->Run()); 186 ASSERT_TRUE(helper->Run());
187 187
188 store_ = new SQLitePersistentCookieStore( 188 store_ = new SQLitePersistentCookieStore(
189 temp_dir_.path().Append(chrome::kCookieFilename)); 189 temp_dir_.path().Append(chrome::kCookieFilename), false);
190 // Posting a blocking task to db_thread_ makes sure that the DB thread waits 190 // Posting a blocking task to db_thread_ makes sure that the DB thread waits
191 // until both Load and LoadCookiesForKey have been posted to its task queue. 191 // until both Load and LoadCookiesForKey have been posted to its task queue.
192 BrowserThread::PostTask( 192 BrowserThread::PostTask(
193 BrowserThread::DB, FROM_HERE, 193 BrowserThread::DB, FROM_HERE,
194 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, 194 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent,
195 base::Unretained(this))); 195 base::Unretained(this)));
196 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, 196 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded,
197 base::Unretained(this))); 197 base::Unretained(this)));
198 store_->LoadCookiesForKey("aaa.com", 198 store_->LoadCookiesForKey("aaa.com",
199 base::Bind(&SQLitePersistentCookieStoreTest::OnKeyLoaded, 199 base::Bind(&SQLitePersistentCookieStoreTest::OnKeyLoaded,
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 295
296 store_->Flush(NewRunnableMethod(counter.get(), &CallbackCounter::Callback)); 296 store_->Flush(NewRunnableMethod(counter.get(), &CallbackCounter::Callback));
297 297
298 scoped_refptr<base::ThreadTestHelper> helper( 298 scoped_refptr<base::ThreadTestHelper> helper(
299 new base::ThreadTestHelper( 299 new base::ThreadTestHelper(
300 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); 300 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)));
301 ASSERT_TRUE(helper->Run()); 301 ASSERT_TRUE(helper->Run());
302 302
303 ASSERT_EQ(1, counter->callback_count()); 303 ASSERT_EQ(1, counter->callback_count());
304 } 304 }
305
306 // Test loading old session cookies from the disk.
307 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) {
308 std::vector<net::CookieMonster::CanonicalCookie*> cookies;
309
310 // Use a separate cookie store for this test.
311 ScopedTempDir temp_dir;
312 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
313 store_ = new SQLitePersistentCookieStore(
314 temp_dir.path().Append(chrome::kCookieFilename), true);
315 // Make sure the database is initialized.
316 Load(&cookies);
317 ASSERT_EQ(0u, cookies.size());
318
319 // Add a session cookie.
320 store_->AddCookie(
321 net::CookieMonster::CanonicalCookie(
322 GURL(), "C", "D", "http://sessioncookie.com", "/", std::string(),
323 std::string(), base::Time::Now(), base::Time::Now(),
324 base::Time::Now(), false, false, true, false /*is_persistent*/));
325
326 // Force the store to write its data to the disk.
327 store_ = NULL;
328 scoped_refptr<base::ThreadTestHelper> helper(
329 new base::ThreadTestHelper(
330 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)));
331 // Make sure we wait until the destructor has run.
332 ASSERT_TRUE(helper->Run());
333
334 // Create a store that loads session cookies.
335 store_ = new SQLitePersistentCookieStore(
336 temp_dir.path().Append(chrome::kCookieFilename), true);
337
338 // Reload and test that the session cookie was loaded.
339 Load(&cookies);
340 ASSERT_EQ(1U, cookies.size());
341 ASSERT_STREQ("http://sessioncookie.com", cookies[0]->Domain().c_str());
342 ASSERT_STREQ("C", cookies[0]->Name().c_str());
343 ASSERT_STREQ("D", cookies[0]->Value().c_str());
344 }
345
346 // Test loading old session cookies from the disk.
347 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) {
348 std::vector<net::CookieMonster::CanonicalCookie*> cookies;
349 // Use a separate cookie store for this test.
350 ScopedTempDir temp_dir;
351 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
352 store_ = new SQLitePersistentCookieStore(
353 temp_dir.path().Append(chrome::kCookieFilename), true);
354 // Make sure the database is initialized.
355 Load(&cookies);
356 ASSERT_EQ(0u, cookies.size());
357
358 // Add a session cookie.
359 store_->AddCookie(
360 net::CookieMonster::CanonicalCookie(
361 GURL(), "C", "D", "http://sessioncookie.com", "/", std::string(),
362 std::string(), base::Time::Now(), base::Time::Now(),
363 base::Time::Now(), false, false, true, false /*is_persistent*/));
364
365 // Force the store to write its data to the disk.
366 store_ = NULL;
367 scoped_refptr<base::ThreadTestHelper> helper(
368 new base::ThreadTestHelper(
369 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)));
370 // Make sure we wait until the destructor has run.
371 ASSERT_TRUE(helper->Run());
372
373 // Create a store that doesn't load old session cookies.
374 store_ = new SQLitePersistentCookieStore(
375 temp_dir.path().Append(chrome::kCookieFilename), false);
376
377 // Reload and test that the session cookie was not loaded.
378 Load(&cookies);
379 ASSERT_EQ(0U, cookies.size());
380
381 // The store should also delete the session cookie. Wait until that has been
382 // done.
383 store_ = NULL;
384 helper = new base::ThreadTestHelper(
385 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB));
386 ASSERT_TRUE(helper->Run());
387
388 // Create a store that loads old session cookies.
389 store_ = new SQLitePersistentCookieStore(
390 temp_dir.path().Append(chrome::kCookieFilename), true);
391
392 // Reload and test that the session cookie is gone.
393 Load(&cookies);
394 ASSERT_EQ(0U, cookies.size());
395 }
396
397 TEST_F(SQLitePersistentCookieStoreTest, PersistHasExpiresAndIsPersistent) {
398 std::vector<net::CookieMonster::CanonicalCookie*> cookies;
399
400 // Use a separate cookie store for this test.
401 ScopedTempDir temp_dir;
402 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
403 store_ = new SQLitePersistentCookieStore(
404 temp_dir.path().Append(chrome::kCookieFilename), true);
405 // Make sure the database is initialized.
406 Load(&cookies);
407 ASSERT_EQ(0u, cookies.size());
408
409 // Add a session cookie with has_expires = false, and another session cookie
410 // with has_expires = true.
411 store_->AddCookie(
412 net::CookieMonster::CanonicalCookie(
413 GURL(), "session-hasexpires", "val", "http://sessioncookie.com", "/",
414 std::string(), std::string(),
415 base::Time::Now() - base::TimeDelta::FromDays(3), base::Time::Now(),
416 base::Time::Now(), false, false, true /* has_expires */,
417 false /* is_persistent */));
418 store_->AddCookie(
419 net::CookieMonster::CanonicalCookie(
420 GURL(), "session-noexpires", "val", "http://sessioncookie.com", "/",
421 std::string(), std::string(),
422 base::Time::Now() - base::TimeDelta::FromDays(2), base::Time::Now(),
423 base::Time::Now(), false, false, false /* has_expires */,
424 false /* is_persistent */));
425 // Add a persistent cookie.
426 store_->AddCookie(
427 net::CookieMonster::CanonicalCookie(
428 GURL(), "persistent", "val", "http://sessioncookie.com", "/",
429 std::string(), std::string(),
430 base::Time::Now() - base::TimeDelta::FromDays(1), base::Time::Now(),
431 base::Time::Now(), false, false, true /* has_expires */,
432 true /* is_persistent */));
433
434 // Force the store to write its data to the disk.
435 store_ = NULL;
436 scoped_refptr<base::ThreadTestHelper> helper(
437 new base::ThreadTestHelper(
438 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)));
439 // Make sure we wait until the destructor has run.
440 ASSERT_TRUE(helper->Run());
441
442 // Create a store that loads session cookies.
443 store_ = new SQLitePersistentCookieStore(
444 temp_dir.path().Append(chrome::kCookieFilename), true);
445
446 // Reload and test that the the DoesExpire and IsPersistent attributes are
447 // restored.
448 Load(&cookies);
449 ASSERT_EQ(3U, cookies.size());
450
451 std::map<std::string, net::CookieMonster::CanonicalCookie*> cookie_map;
452 std::vector<net::CookieMonster::CanonicalCookie*>::const_iterator it;
453 for (it = cookies.begin(); it != cookies.end(); ++it)
454 cookie_map[(*it)->Name()] = *it;
455
456 EXPECT_TRUE(cookie_map["session-hasexpires"]->DoesExpire());
457 EXPECT_FALSE(cookie_map["session-hasexpires"]->IsPersistent());
458
459 EXPECT_FALSE(cookie_map["session-noexpires"]->DoesExpire());
460 EXPECT_FALSE(cookie_map["session-noexpires"]->IsPersistent());
461
462 EXPECT_TRUE(cookie_map["persistent"]->DoesExpire());
463 EXPECT_TRUE(cookie_map["persistent"]->IsPersistent());
464 }
OLDNEW
« no previous file with comments | « chrome/browser/net/sqlite_persistent_cookie_store.cc ('k') | chrome/browser/profiles/profile.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698