OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/extras/sqlite/sqlite_persistent_cookie_store.h" | 5 #include "net/extras/sqlite/sqlite_persistent_cookie_store.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
| 8 #include <memory> |
8 #include <set> | 9 #include <set> |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/callback.h" | 12 #include "base/callback.h" |
12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
13 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
14 #include "base/location.h" | 15 #include "base/location.h" |
15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
16 #include "base/sequenced_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
17 #include "base/stl_util.h" | |
18 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
19 #include "base/test/sequenced_worker_pool_owner.h" | 19 #include "base/test/sequenced_worker_pool_owner.h" |
20 #include "base/threading/sequenced_worker_pool.h" | 20 #include "base/threading/sequenced_worker_pool.h" |
21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
22 #include "crypto/encryptor.h" | 22 #include "crypto/encryptor.h" |
23 #include "crypto/symmetric_key.h" | 23 #include "crypto/symmetric_key.h" |
24 #include "net/cookies/canonical_cookie.h" | 24 #include "net/cookies/canonical_cookie.h" |
25 #include "net/cookies/cookie_constants.h" | 25 #include "net/cookies/cookie_constants.h" |
26 #include "net/extras/sqlite/cookie_crypto_delegate.h" | 26 #include "net/extras/sqlite/cookie_crypto_delegate.h" |
27 #include "sql/connection.h" | 27 #include "sql/connection.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 return encryptor_.Encrypt(plaintext, ciphertext); | 73 return encryptor_.Encrypt(plaintext, ciphertext); |
74 } | 74 } |
75 | 75 |
76 bool CookieCryptor::DecryptString(const std::string& ciphertext, | 76 bool CookieCryptor::DecryptString(const std::string& ciphertext, |
77 std::string* plaintext) { | 77 std::string* plaintext) { |
78 return encryptor_.Decrypt(ciphertext, plaintext); | 78 return encryptor_.Decrypt(ciphertext, plaintext); |
79 } | 79 } |
80 | 80 |
81 } // namespace | 81 } // namespace |
82 | 82 |
83 typedef std::vector<CanonicalCookie*> CanonicalCookieVector; | 83 typedef std::vector<std::unique_ptr<CanonicalCookie>> CanonicalCookieVector; |
84 | 84 |
85 class SQLitePersistentCookieStoreTest : public testing::Test { | 85 class SQLitePersistentCookieStoreTest : public testing::Test { |
86 public: | 86 public: |
87 SQLitePersistentCookieStoreTest() | 87 SQLitePersistentCookieStoreTest() |
88 : pool_owner_(new base::SequencedWorkerPoolOwner(3, "Background Pool")), | 88 : pool_owner_(new base::SequencedWorkerPoolOwner(3, "Background Pool")), |
89 loaded_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 89 loaded_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
90 base::WaitableEvent::InitialState::NOT_SIGNALED), | 90 base::WaitableEvent::InitialState::NOT_SIGNALED), |
91 key_loaded_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 91 key_loaded_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
92 base::WaitableEvent::InitialState::NOT_SIGNALED), | 92 base::WaitableEvent::InitialState::NOT_SIGNALED), |
93 db_thread_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 93 db_thread_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
94 base::WaitableEvent::InitialState::NOT_SIGNALED) {} | 94 base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
95 | 95 |
96 void OnLoaded(const CanonicalCookieVector& cookies) { | 96 void OnLoaded(CanonicalCookieVector cookies) { |
97 cookies_ = cookies; | 97 cookies_.swap(cookies); |
98 loaded_event_.Signal(); | 98 loaded_event_.Signal(); |
99 } | 99 } |
100 | 100 |
101 void OnKeyLoaded(const CanonicalCookieVector& cookies) { | 101 void OnKeyLoaded(CanonicalCookieVector cookies) { |
102 cookies_ = cookies; | 102 cookies_.swap(cookies); |
103 key_loaded_event_.Signal(); | 103 key_loaded_event_.Signal(); |
104 } | 104 } |
105 | 105 |
106 void Load(CanonicalCookieVector* cookies) { | 106 void Load(CanonicalCookieVector* cookies) { |
107 EXPECT_FALSE(loaded_event_.IsSignaled()); | 107 EXPECT_FALSE(loaded_event_.IsSignaled()); |
108 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 108 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
109 base::Unretained(this))); | 109 base::Unretained(this))); |
110 loaded_event_.Wait(); | 110 loaded_event_.Wait(); |
111 *cookies = cookies_; | 111 cookies->swap(cookies_); |
112 } | 112 } |
113 | 113 |
114 void Flush() { | 114 void Flush() { |
115 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 115 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
116 base::WaitableEvent::InitialState::NOT_SIGNALED); | 116 base::WaitableEvent::InitialState::NOT_SIGNALED); |
117 store_->Flush( | 117 store_->Flush( |
118 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event))); | 118 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event))); |
119 event.Wait(); | 119 event.Wait(); |
120 } | 120 } |
121 | 121 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 DestroyStore(); | 222 DestroyStore(); |
223 | 223 |
224 // Load up the store and verify that it has good data in it. | 224 // Load up the store and verify that it has good data in it. |
225 CanonicalCookieVector cookies; | 225 CanonicalCookieVector cookies; |
226 CreateAndLoad(false, false, &cookies); | 226 CreateAndLoad(false, false, &cookies); |
227 ASSERT_EQ(1U, cookies.size()); | 227 ASSERT_EQ(1U, cookies.size()); |
228 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); | 228 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); |
229 ASSERT_STREQ("A", cookies[0]->Name().c_str()); | 229 ASSERT_STREQ("A", cookies[0]->Name().c_str()); |
230 ASSERT_STREQ("B", cookies[0]->Value().c_str()); | 230 ASSERT_STREQ("B", cookies[0]->Value().c_str()); |
231 DestroyStore(); | 231 DestroyStore(); |
232 base::STLDeleteElements(&cookies); | 232 cookies.clear(); |
233 | 233 |
234 // Now corrupt the meta table. | 234 // Now corrupt the meta table. |
235 { | 235 { |
236 sql::Connection db; | 236 sql::Connection db; |
237 ASSERT_TRUE(db.Open(temp_dir_.GetPath().Append(kCookieFilename))); | 237 ASSERT_TRUE(db.Open(temp_dir_.GetPath().Append(kCookieFilename))); |
238 sql::MetaTable meta_table_; | 238 sql::MetaTable meta_table_; |
239 meta_table_.Init(&db, 1, 1); | 239 meta_table_.Init(&db, 1, 1); |
240 ASSERT_TRUE(db.Execute("DELETE FROM meta")); | 240 ASSERT_TRUE(db.Execute("DELETE FROM meta")); |
241 db.Close(); | 241 db.Close(); |
242 } | 242 } |
243 | 243 |
244 // Upon loading, the database should be reset to a good, blank state. | 244 // Upon loading, the database should be reset to a good, blank state. |
245 CreateAndLoad(false, false, &cookies); | 245 CreateAndLoad(false, false, &cookies); |
246 ASSERT_EQ(0U, cookies.size()); | 246 ASSERT_EQ(0U, cookies.size()); |
247 | 247 |
248 // Verify that, after, recovery, the database persists properly. | 248 // Verify that, after, recovery, the database persists properly. |
249 AddCookie(GURL("http://foo.bar"), "X", "Y", std::string(), "/", | 249 AddCookie(GURL("http://foo.bar"), "X", "Y", std::string(), "/", |
250 base::Time::Now()); | 250 base::Time::Now()); |
251 DestroyStore(); | 251 DestroyStore(); |
252 CreateAndLoad(false, false, &cookies); | 252 CreateAndLoad(false, false, &cookies); |
253 ASSERT_EQ(1U, cookies.size()); | 253 ASSERT_EQ(1U, cookies.size()); |
254 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); | 254 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); |
255 ASSERT_STREQ("X", cookies[0]->Name().c_str()); | 255 ASSERT_STREQ("X", cookies[0]->Name().c_str()); |
256 ASSERT_STREQ("Y", cookies[0]->Value().c_str()); | 256 ASSERT_STREQ("Y", cookies[0]->Value().c_str()); |
257 base::STLDeleteElements(&cookies); | 257 cookies.clear(); |
258 } | 258 } |
259 | 259 |
260 // Test if data is stored as expected in the SQLite database. | 260 // Test if data is stored as expected in the SQLite database. |
261 TEST_F(SQLitePersistentCookieStoreTest, TestPersistance) { | 261 TEST_F(SQLitePersistentCookieStoreTest, TestPersistance) { |
262 InitializeStore(false, false); | 262 InitializeStore(false, false); |
263 AddCookie(GURL("http://foo.bar"), "A", "B", std::string(), "/", | 263 AddCookie(GURL("http://foo.bar"), "A", "B", std::string(), "/", |
264 base::Time::Now()); | 264 base::Time::Now()); |
265 // Replace the store effectively destroying the current one and forcing it | 265 // Replace the store effectively destroying the current one and forcing it |
266 // to write its data to disk. Then we can see if after loading it again it | 266 // to write its data to disk. Then we can see if after loading it again it |
267 // is still there. | 267 // is still there. |
268 DestroyStore(); | 268 DestroyStore(); |
269 // Reload and test for persistence | 269 // Reload and test for persistence |
270 CanonicalCookieVector cookies; | 270 CanonicalCookieVector cookies; |
271 CreateAndLoad(false, false, &cookies); | 271 CreateAndLoad(false, false, &cookies); |
272 ASSERT_EQ(1U, cookies.size()); | 272 ASSERT_EQ(1U, cookies.size()); |
273 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); | 273 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); |
274 ASSERT_STREQ("A", cookies[0]->Name().c_str()); | 274 ASSERT_STREQ("A", cookies[0]->Name().c_str()); |
275 ASSERT_STREQ("B", cookies[0]->Value().c_str()); | 275 ASSERT_STREQ("B", cookies[0]->Value().c_str()); |
276 | 276 |
277 // Now delete the cookie and check persistence again. | 277 // Now delete the cookie and check persistence again. |
278 store_->DeleteCookie(*cookies[0]); | 278 store_->DeleteCookie(*cookies[0]); |
279 DestroyStore(); | 279 DestroyStore(); |
280 base::STLDeleteElements(&cookies); | 280 cookies.clear(); |
281 | 281 |
282 // Reload and check if the cookie has been removed. | 282 // Reload and check if the cookie has been removed. |
283 CreateAndLoad(false, false, &cookies); | 283 CreateAndLoad(false, false, &cookies); |
284 ASSERT_EQ(0U, cookies.size()); | 284 ASSERT_EQ(0U, cookies.size()); |
285 } | 285 } |
286 | 286 |
287 TEST_F(SQLitePersistentCookieStoreTest, TestSessionCookiesDeletedOnStartup) { | 287 TEST_F(SQLitePersistentCookieStoreTest, TestSessionCookiesDeletedOnStartup) { |
288 // Initialize the cookie store with 3 persistent cookies, 5 transient | 288 // Initialize the cookie store with 3 persistent cookies, 5 transient |
289 // cookies. | 289 // cookies. |
290 InitializeStore(false, false); | 290 InitializeStore(false, false); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 // Now the DB-thread queue contains: | 339 // Now the DB-thread queue contains: |
340 // (active:) | 340 // (active:) |
341 // 1. Wait (on db_event) | 341 // 1. Wait (on db_event) |
342 // (pending:) | 342 // (pending:) |
343 // 2. "Init And Chain-Load First Domain" | 343 // 2. "Init And Chain-Load First Domain" |
344 // 3. Add Cookie (c.com) | 344 // 3. Add Cookie (c.com) |
345 // 4. Flush Cookie (c.com) | 345 // 4. Flush Cookie (c.com) |
346 db_thread_event_.Signal(); | 346 db_thread_event_.Signal(); |
347 event.Wait(); | 347 event.Wait(); |
348 loaded_event_.Wait(); | 348 loaded_event_.Wait(); |
349 base::STLDeleteElements(&cookies_); | 349 cookies_.clear(); |
350 DestroyStore(); | 350 DestroyStore(); |
351 | 351 |
352 // Load the store a third time, this time restoring session cookies. The | 352 // Load the store a third time, this time restoring session cookies. The |
353 // store should contain exactly 4 cookies: the 3 persistent, and "c.com", | 353 // store should contain exactly 4 cookies: the 3 persistent, and "c.com", |
354 // which was added during the second cookie store load. | 354 // which was added during the second cookie store load. |
355 store_ = new SQLitePersistentCookieStore( | 355 store_ = new SQLitePersistentCookieStore( |
356 temp_dir_.GetPath().Append(kCookieFilename), client_task_runner(), | 356 temp_dir_.GetPath().Append(kCookieFilename), client_task_runner(), |
357 background_task_runner(), true, nullptr); | 357 background_task_runner(), true, nullptr); |
358 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 358 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
359 base::Unretained(this))); | 359 base::Unretained(this))); |
360 loaded_event_.Wait(); | 360 loaded_event_.Wait(); |
361 ASSERT_EQ(4u, cookies_.size()); | 361 ASSERT_EQ(4u, cookies_.size()); |
362 base::STLDeleteElements(&cookies_); | 362 cookies_.clear(); |
363 } | 363 } |
364 | 364 |
365 // Test that priority load of cookies for a specfic domain key could be | 365 // Test that priority load of cookies for a specfic domain key could be |
366 // completed before the entire store is loaded | 366 // completed before the entire store is loaded |
367 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { | 367 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { |
368 InitializeStore(false, false); | 368 InitializeStore(false, false); |
369 base::Time t = base::Time::Now(); | 369 base::Time t = base::Time::Now(); |
370 AddCookie(GURL("http://foo.bar"), "A", "B", std::string(), "/", t); | 370 AddCookie(GURL("http://foo.bar"), "A", "B", std::string(), "/", t); |
371 t += base::TimeDelta::FromInternalValue(10); | 371 t += base::TimeDelta::FromInternalValue(10); |
372 AddCookie(GURL("http://www.aaa.com"), "A", "B", std::string(), "/", t); | 372 AddCookie(GURL("http://www.aaa.com"), "A", "B", std::string(), "/", t); |
(...skipping 29 matching lines...) Expand all Loading... |
402 // 3. Priority Load (aaa.com) | 402 // 3. Priority Load (aaa.com) |
403 // 4. Wait (on db_event) | 403 // 4. Wait (on db_event) |
404 db_thread_event_.Signal(); | 404 db_thread_event_.Signal(); |
405 key_loaded_event_.Wait(); | 405 key_loaded_event_.Wait(); |
406 ASSERT_EQ(loaded_event_.IsSignaled(), false); | 406 ASSERT_EQ(loaded_event_.IsSignaled(), false); |
407 std::set<std::string> cookies_loaded; | 407 std::set<std::string> cookies_loaded; |
408 for (CanonicalCookieVector::const_iterator it = cookies_.begin(); | 408 for (CanonicalCookieVector::const_iterator it = cookies_.begin(); |
409 it != cookies_.end(); ++it) { | 409 it != cookies_.end(); ++it) { |
410 cookies_loaded.insert((*it)->Domain().c_str()); | 410 cookies_loaded.insert((*it)->Domain().c_str()); |
411 } | 411 } |
412 base::STLDeleteElements(&cookies_); | 412 cookies_.clear(); |
413 ASSERT_GT(4U, cookies_loaded.size()); | 413 ASSERT_GT(4U, cookies_loaded.size()); |
414 ASSERT_EQ(true, cookies_loaded.find("www.aaa.com") != cookies_loaded.end()); | 414 ASSERT_EQ(true, cookies_loaded.find("www.aaa.com") != cookies_loaded.end()); |
415 ASSERT_EQ(true, | 415 ASSERT_EQ(true, |
416 cookies_loaded.find("travel.aaa.com") != cookies_loaded.end()); | 416 cookies_loaded.find("travel.aaa.com") != cookies_loaded.end()); |
417 | 417 |
418 db_thread_event_.Signal(); | 418 db_thread_event_.Signal(); |
419 loaded_event_.Wait(); | 419 loaded_event_.Wait(); |
420 for (CanonicalCookieVector::const_iterator it = cookies_.begin(); | 420 for (CanonicalCookieVector::const_iterator it = cookies_.begin(); |
421 it != cookies_.end(); ++it) { | 421 it != cookies_.end(); ++it) { |
422 cookies_loaded.insert((*it)->Domain().c_str()); | 422 cookies_loaded.insert((*it)->Domain().c_str()); |
423 } | 423 } |
424 ASSERT_EQ(4U, cookies_loaded.size()); | 424 ASSERT_EQ(4U, cookies_loaded.size()); |
425 ASSERT_EQ(cookies_loaded.find("foo.bar") != cookies_loaded.end(), true); | 425 ASSERT_EQ(cookies_loaded.find("foo.bar") != cookies_loaded.end(), true); |
426 ASSERT_EQ(cookies_loaded.find("www.bbb.com") != cookies_loaded.end(), true); | 426 ASSERT_EQ(cookies_loaded.find("www.bbb.com") != cookies_loaded.end(), true); |
427 base::STLDeleteElements(&cookies_); | 427 cookies_.clear(); |
428 } | 428 } |
429 | 429 |
430 // Test that we can force the database to be written by calling Flush(). | 430 // Test that we can force the database to be written by calling Flush(). |
431 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { | 431 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { |
432 InitializeStore(false, false); | 432 InitializeStore(false, false); |
433 // File timestamps don't work well on all platforms, so we'll determine | 433 // File timestamps don't work well on all platforms, so we'll determine |
434 // whether the DB file has been modified by checking its size. | 434 // whether the DB file has been modified by checking its size. |
435 base::FilePath path = temp_dir_.GetPath().Append(kCookieFilename); | 435 base::FilePath path = temp_dir_.GetPath().Append(kCookieFilename); |
436 base::File::Info info; | 436 base::File::Info info; |
437 ASSERT_TRUE(base::GetFileInfo(path, &info)); | 437 ASSERT_TRUE(base::GetFileInfo(path, &info)); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 // was loaded. | 470 // was loaded. |
471 CanonicalCookieVector cookies; | 471 CanonicalCookieVector cookies; |
472 CreateAndLoad(false, true, &cookies); | 472 CreateAndLoad(false, true, &cookies); |
473 | 473 |
474 ASSERT_EQ(1U, cookies.size()); | 474 ASSERT_EQ(1U, cookies.size()); |
475 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); | 475 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); |
476 ASSERT_STREQ("C", cookies[0]->Name().c_str()); | 476 ASSERT_STREQ("C", cookies[0]->Name().c_str()); |
477 ASSERT_STREQ("D", cookies[0]->Value().c_str()); | 477 ASSERT_STREQ("D", cookies[0]->Value().c_str()); |
478 ASSERT_EQ(COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); | 478 ASSERT_EQ(COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); |
479 | 479 |
480 base::STLDeleteElements(&cookies); | 480 cookies.clear(); |
481 } | 481 } |
482 | 482 |
483 // Test loading old session cookies from the disk. | 483 // Test loading old session cookies from the disk. |
484 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { | 484 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { |
485 InitializeStore(false, true); | 485 InitializeStore(false, true); |
486 | 486 |
487 // Add a session cookie. | 487 // Add a session cookie. |
488 store_->AddCookie(*CanonicalCookie::Create( | 488 store_->AddCookie(*CanonicalCookie::Create( |
489 GURL("http://sessioncookie.com"), "C", "D", std::string(), "/", | 489 GURL("http://sessioncookie.com"), "C", "D", std::string(), "/", |
490 base::Time::Now(), base::Time(), false, false, | 490 base::Time::Now(), base::Time(), false, false, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 // Force the store to write its data to the disk. | 529 // Force the store to write its data to the disk. |
530 DestroyStore(); | 530 DestroyStore(); |
531 | 531 |
532 // Create a store that loads session cookie and test that the IsPersistent | 532 // Create a store that loads session cookie and test that the IsPersistent |
533 // attribute is restored. | 533 // attribute is restored. |
534 CanonicalCookieVector cookies; | 534 CanonicalCookieVector cookies; |
535 CreateAndLoad(false, true, &cookies); | 535 CreateAndLoad(false, true, &cookies); |
536 ASSERT_EQ(2U, cookies.size()); | 536 ASSERT_EQ(2U, cookies.size()); |
537 | 537 |
538 std::map<std::string, CanonicalCookie*> cookie_map; | 538 std::map<std::string, CanonicalCookie*> cookie_map; |
539 for (CanonicalCookieVector::const_iterator it = cookies.begin(); | 539 for (const auto& cookie : cookies) |
540 it != cookies.end(); ++it) { | 540 cookie_map[cookie->Name()] = cookie.get(); |
541 cookie_map[(*it)->Name()] = *it; | |
542 } | |
543 | 541 |
544 std::map<std::string, CanonicalCookie*>::const_iterator it = | 542 auto it = cookie_map.find(kSessionName); |
545 cookie_map.find(kSessionName); | |
546 ASSERT_TRUE(it != cookie_map.end()); | 543 ASSERT_TRUE(it != cookie_map.end()); |
547 EXPECT_FALSE(cookie_map[kSessionName]->IsPersistent()); | 544 EXPECT_FALSE(cookie_map[kSessionName]->IsPersistent()); |
548 | 545 |
549 it = cookie_map.find(kPersistentName); | 546 it = cookie_map.find(kPersistentName); |
550 ASSERT_TRUE(it != cookie_map.end()); | 547 ASSERT_TRUE(it != cookie_map.end()); |
551 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); | 548 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); |
552 | 549 |
553 base::STLDeleteElements(&cookies); | 550 cookies.clear(); |
554 } | 551 } |
555 | 552 |
556 TEST_F(SQLitePersistentCookieStoreTest, PriorityIsPersistent) { | 553 TEST_F(SQLitePersistentCookieStoreTest, PriorityIsPersistent) { |
557 static const char kURL[] = "http://sessioncookie.com"; | 554 static const char kURL[] = "http://sessioncookie.com"; |
558 static const char kLowName[] = "low"; | 555 static const char kLowName[] = "low"; |
559 static const char kMediumName[] = "medium"; | 556 static const char kMediumName[] = "medium"; |
560 static const char kHighName[] = "high"; | 557 static const char kHighName[] = "high"; |
561 static const char kCookieValue[] = "value"; | 558 static const char kCookieValue[] = "value"; |
562 static const char kCookiePath[] = "/"; | 559 static const char kCookiePath[] = "/"; |
563 | 560 |
(...skipping 24 matching lines...) Expand all Loading... |
588 DestroyStore(); | 585 DestroyStore(); |
589 | 586 |
590 // Create a store that loads session cookie and test that the priority | 587 // Create a store that loads session cookie and test that the priority |
591 // attribute values are restored. | 588 // attribute values are restored. |
592 CanonicalCookieVector cookies; | 589 CanonicalCookieVector cookies; |
593 CreateAndLoad(false, true, &cookies); | 590 CreateAndLoad(false, true, &cookies); |
594 ASSERT_EQ(3U, cookies.size()); | 591 ASSERT_EQ(3U, cookies.size()); |
595 | 592 |
596 // Put the cookies into a map, by name, so we can easily find them. | 593 // Put the cookies into a map, by name, so we can easily find them. |
597 std::map<std::string, CanonicalCookie*> cookie_map; | 594 std::map<std::string, CanonicalCookie*> cookie_map; |
598 for (CanonicalCookieVector::const_iterator it = cookies.begin(); | 595 for (const auto& cookie : cookies) |
599 it != cookies.end(); ++it) { | 596 cookie_map[cookie->Name()] = cookie.get(); |
600 cookie_map[(*it)->Name()] = *it; | |
601 } | |
602 | 597 |
603 // Validate that each cookie has the correct priority. | 598 // Validate that each cookie has the correct priority. |
604 std::map<std::string, CanonicalCookie*>::const_iterator it = | 599 auto it = cookie_map.find(kLowName); |
605 cookie_map.find(kLowName); | |
606 ASSERT_TRUE(it != cookie_map.end()); | 600 ASSERT_TRUE(it != cookie_map.end()); |
607 EXPECT_EQ(COOKIE_PRIORITY_LOW, cookie_map[kLowName]->Priority()); | 601 EXPECT_EQ(COOKIE_PRIORITY_LOW, cookie_map[kLowName]->Priority()); |
608 | 602 |
609 it = cookie_map.find(kMediumName); | 603 it = cookie_map.find(kMediumName); |
610 ASSERT_TRUE(it != cookie_map.end()); | 604 ASSERT_TRUE(it != cookie_map.end()); |
611 EXPECT_EQ(COOKIE_PRIORITY_MEDIUM, cookie_map[kMediumName]->Priority()); | 605 EXPECT_EQ(COOKIE_PRIORITY_MEDIUM, cookie_map[kMediumName]->Priority()); |
612 | 606 |
613 it = cookie_map.find(kHighName); | 607 it = cookie_map.find(kHighName); |
614 ASSERT_TRUE(it != cookie_map.end()); | 608 ASSERT_TRUE(it != cookie_map.end()); |
615 EXPECT_EQ(COOKIE_PRIORITY_HIGH, cookie_map[kHighName]->Priority()); | 609 EXPECT_EQ(COOKIE_PRIORITY_HIGH, cookie_map[kHighName]->Priority()); |
616 | 610 |
617 base::STLDeleteElements(&cookies); | 611 cookies.clear(); |
618 } | 612 } |
619 | 613 |
620 TEST_F(SQLitePersistentCookieStoreTest, SameSiteIsPersistent) { | 614 TEST_F(SQLitePersistentCookieStoreTest, SameSiteIsPersistent) { |
621 const char kURL[] = "http://sessioncookie.com"; | 615 const char kURL[] = "http://sessioncookie.com"; |
622 const char kNoneName[] = "none"; | 616 const char kNoneName[] = "none"; |
623 const char kLaxName[] = "lax"; | 617 const char kLaxName[] = "lax"; |
624 const char kStrictName[] = "strict"; | 618 const char kStrictName[] = "strict"; |
625 const char kCookieValue[] = "value"; | 619 const char kCookieValue[] = "value"; |
626 const char kCookiePath[] = "/"; | 620 const char kCookiePath[] = "/"; |
627 | 621 |
628 InitializeStore(false, true); | 622 InitializeStore(false, true); |
629 | 623 |
630 // Add a non-samesite cookie. | 624 // Add a non-samesite cookie. |
631 store_->AddCookie(*CanonicalCookie::Create( | 625 store_->AddCookie(*CanonicalCookie::Create( |
632 GURL(kURL), kNoneName, kCookieValue, std::string(), kCookiePath, | 626 GURL(kURL), kNoneName, kCookieValue, std::string(), kCookiePath, |
633 base::Time::Now() - base::TimeDelta::FromMinutes(1), | 627 base::Time::Now() - base::TimeDelta::FromMinutes(1), |
634 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, | 628 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, |
635 CookieSameSite::NO_RESTRICTION, false, COOKIE_PRIORITY_DEFAULT)); | 629 CookieSameSite::NO_RESTRICTION, false, COOKIE_PRIORITY_DEFAULT)); |
636 | 630 |
637 // Add a lax-samesite persistent cookie. | 631 // Add a lax-samesite persistent cookie. |
638 store_->AddCookie(*CanonicalCookie::Create( | 632 store_->AddCookie(*CanonicalCookie::Create( |
639 GURL(kURL), kLaxName, kCookieValue, std::string(), kCookiePath, | 633 GURL(kURL), kLaxName, kCookieValue, std::string(), kCookiePath, |
640 base::Time::Now() - base::TimeDelta::FromMinutes(2), | 634 base::Time::Now() - base::TimeDelta::FromMinutes(2), |
641 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, | 635 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, |
642 CookieSameSite::LAX_MODE, false, COOKIE_PRIORITY_DEFAULT)); | 636 CookieSameSite::LAX_MODE, false, COOKIE_PRIORITY_DEFAULT)); |
643 | 637 |
644 // Add a strict-samesite peristent cookie. | 638 // Add a strict-samesite persistent cookie. |
645 store_->AddCookie(*CanonicalCookie::Create( | 639 store_->AddCookie(*CanonicalCookie::Create( |
646 GURL(kURL), kStrictName, kCookieValue, std::string(), kCookiePath, | 640 GURL(kURL), kStrictName, kCookieValue, std::string(), kCookiePath, |
647 base::Time::Now() - base::TimeDelta::FromMinutes(3), | 641 base::Time::Now() - base::TimeDelta::FromMinutes(3), |
648 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, | 642 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, |
649 CookieSameSite::STRICT_MODE, false, COOKIE_PRIORITY_DEFAULT)); | 643 CookieSameSite::STRICT_MODE, false, COOKIE_PRIORITY_DEFAULT)); |
650 | 644 |
651 // Force the store to write its data to the disk. | 645 // Force the store to write its data to the disk. |
652 DestroyStore(); | 646 DestroyStore(); |
653 | 647 |
654 // Create a store that loads session cookie and test that the priority | 648 // Create a store that loads session cookie and test that the priority |
655 // attribute values are restored. | 649 // attribute values are restored. |
656 CanonicalCookieVector cookies; | 650 CanonicalCookieVector cookies; |
657 CreateAndLoad(false, true, &cookies); | 651 CreateAndLoad(false, true, &cookies); |
658 ASSERT_EQ(3U, cookies.size()); | 652 ASSERT_EQ(3U, cookies.size()); |
659 | 653 |
660 // Put the cookies into a map, by name, for comparison below. | 654 // Put the cookies into a map, by name, for comparison below. |
661 std::map<std::string, CanonicalCookie*> cookie_map; | 655 std::map<std::string, CanonicalCookie*> cookie_map; |
662 for (auto* cookie : cookies) | 656 for (const auto& cookie : cookies) |
663 cookie_map[cookie->Name()] = cookie; | 657 cookie_map[cookie->Name()] = cookie.get(); |
664 | 658 |
665 // Validate that each cookie has the correct SameSite. | 659 // Validate that each cookie has the correct SameSite. |
666 ASSERT_EQ(1u, cookie_map.count(kNoneName)); | 660 ASSERT_EQ(1u, cookie_map.count(kNoneName)); |
667 EXPECT_EQ(CookieSameSite::NO_RESTRICTION, cookie_map[kNoneName]->SameSite()); | 661 EXPECT_EQ(CookieSameSite::NO_RESTRICTION, cookie_map[kNoneName]->SameSite()); |
668 | 662 |
669 ASSERT_EQ(1u, cookie_map.count(kLaxName)); | 663 ASSERT_EQ(1u, cookie_map.count(kLaxName)); |
670 EXPECT_EQ(CookieSameSite::LAX_MODE, cookie_map[kLaxName]->SameSite()); | 664 EXPECT_EQ(CookieSameSite::LAX_MODE, cookie_map[kLaxName]->SameSite()); |
671 | 665 |
672 ASSERT_EQ(1u, cookie_map.count(kStrictName)); | 666 ASSERT_EQ(1u, cookie_map.count(kStrictName)); |
673 EXPECT_EQ(CookieSameSite::STRICT_MODE, cookie_map[kStrictName]->SameSite()); | 667 EXPECT_EQ(CookieSameSite::STRICT_MODE, cookie_map[kStrictName]->SameSite()); |
674 | 668 |
675 base::STLDeleteElements(&cookies); | 669 cookies.clear(); |
676 } | 670 } |
677 | 671 |
678 TEST_F(SQLitePersistentCookieStoreTest, UpdateToEncryption) { | 672 TEST_F(SQLitePersistentCookieStoreTest, UpdateToEncryption) { |
679 CanonicalCookieVector cookies; | 673 CanonicalCookieVector cookies; |
680 | 674 |
681 // Create unencrypted cookie store and write something to it. | 675 // Create unencrypted cookie store and write something to it. |
682 InitializeStore(false, false); | 676 InitializeStore(false, false); |
683 AddCookie(GURL("http://foo.bar"), "name", "value123XYZ", std::string(), "/", | 677 AddCookie(GURL("http://foo.bar"), "name", "value123XYZ", std::string(), "/", |
684 base::Time::Now()); | 678 base::Time::Now()); |
685 DestroyStore(); | 679 DestroyStore(); |
686 | 680 |
687 // Verify that "value" is visible in the file. This is necessary in order to | 681 // Verify that "value" is visible in the file. This is necessary in order to |
688 // have confidence in a later test that "encrypted_value" is not visible. | 682 // have confidence in a later test that "encrypted_value" is not visible. |
689 std::string contents = ReadRawDBContents(); | 683 std::string contents = ReadRawDBContents(); |
690 EXPECT_NE(0U, contents.length()); | 684 EXPECT_NE(0U, contents.length()); |
691 EXPECT_NE(contents.find("value123XYZ"), std::string::npos); | 685 EXPECT_NE(contents.find("value123XYZ"), std::string::npos); |
692 | 686 |
693 // Create encrypted cookie store and ensure old cookie still reads. | 687 // Create encrypted cookie store and ensure old cookie still reads. |
694 base::STLDeleteElements(&cookies_); | 688 cookies.clear(); |
695 EXPECT_EQ(0U, cookies_.size()); | 689 EXPECT_EQ(0U, cookies.size()); |
696 CreateAndLoad(true, false, &cookies); | 690 CreateAndLoad(true, false, &cookies); |
697 EXPECT_EQ(1U, cookies_.size()); | 691 EXPECT_EQ(1U, cookies.size()); |
698 EXPECT_EQ("name", cookies_[0]->Name()); | 692 EXPECT_EQ("name", cookies[0]->Name()); |
699 EXPECT_EQ("value123XYZ", cookies_[0]->Value()); | 693 EXPECT_EQ("value123XYZ", cookies[0]->Value()); |
700 | 694 |
701 // Make sure we can update existing cookie and add new cookie as encrypted. | 695 // Make sure we can update existing cookie and add new cookie as encrypted. |
702 store_->DeleteCookie(*(cookies_[0])); | 696 store_->DeleteCookie(*(cookies[0])); |
703 AddCookie(GURL("http://foo.bar"), "name", "encrypted_value123XYZ", | 697 AddCookie(GURL("http://foo.bar"), "name", "encrypted_value123XYZ", |
704 std::string(), "/", base::Time::Now()); | 698 std::string(), "/", base::Time::Now()); |
705 AddCookie(GURL("http://foo.bar"), "other", "something456ABC", std::string(), | 699 AddCookie(GURL("http://foo.bar"), "other", "something456ABC", std::string(), |
706 "/", base::Time::Now() + base::TimeDelta::FromInternalValue(10)); | 700 "/", base::Time::Now() + base::TimeDelta::FromInternalValue(10)); |
707 DestroyStore(); | 701 DestroyStore(); |
708 base::STLDeleteElements(&cookies_); | 702 cookies.clear(); |
709 CreateAndLoad(true, false, &cookies); | 703 CreateAndLoad(true, false, &cookies); |
710 EXPECT_EQ(2U, cookies_.size()); | 704 EXPECT_EQ(2U, cookies.size()); |
711 CanonicalCookie* cookie_name = nullptr; | 705 CanonicalCookie* cookie_name = nullptr; |
712 CanonicalCookie* cookie_other = nullptr; | 706 CanonicalCookie* cookie_other = nullptr; |
713 if (cookies_[0]->Name() == "name") { | 707 if (cookies[0]->Name() == "name") { |
714 cookie_name = cookies_[0]; | 708 cookie_name = cookies[0].get(); |
715 cookie_other = cookies_[1]; | 709 cookie_other = cookies[1].get(); |
716 } else { | 710 } else { |
717 cookie_name = cookies_[1]; | 711 cookie_name = cookies[1].get(); |
718 cookie_other = cookies_[0]; | 712 cookie_other = cookies[0].get(); |
719 } | 713 } |
720 EXPECT_EQ("encrypted_value123XYZ", cookie_name->Value()); | 714 EXPECT_EQ("encrypted_value123XYZ", cookie_name->Value()); |
721 EXPECT_EQ("something456ABC", cookie_other->Value()); | 715 EXPECT_EQ("something456ABC", cookie_other->Value()); |
722 DestroyStore(); | 716 DestroyStore(); |
723 base::STLDeleteElements(&cookies_); | 717 cookies.clear(); |
724 | 718 |
725 // Examine the real record to make sure plaintext version doesn't exist. | 719 // Examine the real record to make sure plaintext version doesn't exist. |
726 sql::Connection db; | 720 sql::Connection db; |
727 sql::Statement smt; | 721 sql::Statement smt; |
728 int resultcount = 0; | 722 int resultcount = 0; |
729 ASSERT_TRUE(db.Open(temp_dir_.GetPath().Append(kCookieFilename))); | 723 ASSERT_TRUE(db.Open(temp_dir_.GetPath().Append(kCookieFilename))); |
730 smt.Assign(db.GetCachedStatement(SQL_FROM_HERE, | 724 smt.Assign(db.GetCachedStatement(SQL_FROM_HERE, |
731 "SELECT * " | 725 "SELECT * " |
732 "FROM cookies " | 726 "FROM cookies " |
733 "WHERE host_key = 'foo.bar'")); | 727 "WHERE host_key = 'foo.bar'")); |
(...skipping 21 matching lines...) Expand all Loading... |
755 AddCookie(GURL("http://foo.bar"), "name", "value123XYZ", std::string(), "/", | 749 AddCookie(GURL("http://foo.bar"), "name", "value123XYZ", std::string(), "/", |
756 base::Time::Now()); | 750 base::Time::Now()); |
757 DestroyStore(); | 751 DestroyStore(); |
758 | 752 |
759 // Verify that "value" is not visible in the file. | 753 // Verify that "value" is not visible in the file. |
760 std::string contents = ReadRawDBContents(); | 754 std::string contents = ReadRawDBContents(); |
761 EXPECT_NE(0U, contents.length()); | 755 EXPECT_NE(0U, contents.length()); |
762 EXPECT_EQ(contents.find("value123XYZ"), std::string::npos); | 756 EXPECT_EQ(contents.find("value123XYZ"), std::string::npos); |
763 | 757 |
764 // Create encrypted cookie store and ensure old cookie still reads. | 758 // Create encrypted cookie store and ensure old cookie still reads. |
765 base::STLDeleteElements(&cookies_); | 759 cookies.clear(); |
766 EXPECT_EQ(0U, cookies_.size()); | 760 EXPECT_EQ(0U, cookies.size()); |
767 CreateAndLoad(true, false, &cookies); | 761 CreateAndLoad(true, false, &cookies); |
768 EXPECT_EQ(1U, cookies_.size()); | 762 EXPECT_EQ(1U, cookies.size()); |
769 EXPECT_EQ("name", cookies_[0]->Name()); | 763 EXPECT_EQ("name", cookies[0]->Name()); |
770 EXPECT_EQ("value123XYZ", cookies_[0]->Value()); | 764 EXPECT_EQ("value123XYZ", cookies[0]->Value()); |
771 | 765 |
772 // Make sure we can update existing cookie and it writes unencrypted. | 766 // Make sure we can update existing cookie and it writes unencrypted. |
773 cookie_crypto_delegate_->should_encrypt_ = false; | 767 cookie_crypto_delegate_->should_encrypt_ = false; |
774 store_->DeleteCookie(*(cookies_[0])); | 768 store_->DeleteCookie(*(cookies[0])); |
775 AddCookie(GURL("http://foo.bar"), "name", "plaintext_value123XYZ", | 769 AddCookie(GURL("http://foo.bar"), "name", "plaintext_value123XYZ", |
776 std::string(), "/", base::Time::Now()); | 770 std::string(), "/", base::Time::Now()); |
777 AddCookie(GURL("http://foo.bar"), "other", "something456ABC", std::string(), | 771 AddCookie(GURL("http://foo.bar"), "other", "something456ABC", std::string(), |
778 "/", base::Time::Now() + base::TimeDelta::FromInternalValue(10)); | 772 "/", base::Time::Now() + base::TimeDelta::FromInternalValue(10)); |
779 DestroyStore(); | 773 DestroyStore(); |
780 base::STLDeleteElements(&cookies_); | 774 cookies.clear(); |
781 CreateAndLoad(true, false, &cookies); | 775 CreateAndLoad(true, false, &cookies); |
782 EXPECT_EQ(2U, cookies_.size()); | 776 EXPECT_EQ(2U, cookies.size()); |
783 CanonicalCookie* cookie_name = nullptr; | 777 CanonicalCookie* cookie_name = nullptr; |
784 CanonicalCookie* cookie_other = nullptr; | 778 CanonicalCookie* cookie_other = nullptr; |
785 if (cookies_[0]->Name() == "name") { | 779 if (cookies[0]->Name() == "name") { |
786 cookie_name = cookies_[0]; | 780 cookie_name = cookies[0].get(); |
787 cookie_other = cookies_[1]; | 781 cookie_other = cookies[1].get(); |
788 } else { | 782 } else { |
789 cookie_name = cookies_[1]; | 783 cookie_name = cookies[1].get(); |
790 cookie_other = cookies_[0]; | 784 cookie_other = cookies[0].get(); |
791 } | 785 } |
792 EXPECT_EQ("plaintext_value123XYZ", cookie_name->Value()); | 786 EXPECT_EQ("plaintext_value123XYZ", cookie_name->Value()); |
793 EXPECT_EQ("something456ABC", cookie_other->Value()); | 787 EXPECT_EQ("something456ABC", cookie_other->Value()); |
794 DestroyStore(); | 788 DestroyStore(); |
795 base::STLDeleteElements(&cookies_); | 789 cookies.clear(); |
796 | 790 |
797 // Verify that "value" is now visible in the file. | 791 // Verify that "value" is now visible in the file. |
798 contents = ReadRawDBContents(); | 792 contents = ReadRawDBContents(); |
799 EXPECT_NE(0U, contents.length()); | 793 EXPECT_NE(0U, contents.length()); |
800 EXPECT_NE(contents.find("value123XYZ"), std::string::npos); | 794 EXPECT_NE(contents.find("value123XYZ"), std::string::npos); |
801 } | 795 } |
802 | 796 |
803 namespace { | 797 namespace { |
804 void WasCalledWithNoCookies(bool* was_called_with_no_cookies, | 798 void WasCalledWithNoCookies( |
805 const std::vector<CanonicalCookie*>& cookies) { | 799 bool* was_called_with_no_cookies, |
| 800 std::vector<std::unique_ptr<CanonicalCookie>> cookies) { |
806 *was_called_with_no_cookies = cookies.empty(); | 801 *was_called_with_no_cookies = cookies.empty(); |
807 } | 802 } |
808 } | 803 } |
809 | 804 |
810 TEST_F(SQLitePersistentCookieStoreTest, EmptyLoadAfterClose) { | 805 TEST_F(SQLitePersistentCookieStoreTest, EmptyLoadAfterClose) { |
811 // Create unencrypted cookie store and write something to it. | 806 // Create unencrypted cookie store and write something to it. |
812 InitializeStore(false, false); | 807 InitializeStore(false, false); |
813 AddCookie(GURL("http://foo.bar"), "name", "value123XYZ", std::string(), "/", | 808 AddCookie(GURL("http://foo.bar"), "name", "value123XYZ", std::string(), "/", |
814 base::Time::Now()); | 809 base::Time::Now()); |
815 DestroyStore(); | 810 DestroyStore(); |
816 | 811 |
817 // Create the cookie store, but immediately close it. | 812 // Create the cookie store, but immediately close it. |
818 Create(false, false); | 813 Create(false, false); |
819 store_->Close(base::Closure()); | 814 store_->Close(base::Closure()); |
820 | 815 |
821 // Expect any attempt to call Load() to synchronously respond with an empty | 816 // Expect any attempt to call Load() to synchronously respond with an empty |
822 // vector of cookies after we've Close()d the database. | 817 // vector of cookies after we've Close()d the database. |
823 bool was_called_with_no_cookies = false; | 818 bool was_called_with_no_cookies = false; |
824 store_->Load(base::Bind(WasCalledWithNoCookies, &was_called_with_no_cookies)); | 819 store_->Load(base::Bind(WasCalledWithNoCookies, &was_called_with_no_cookies)); |
825 EXPECT_TRUE(was_called_with_no_cookies); | 820 EXPECT_TRUE(was_called_with_no_cookies); |
826 | 821 |
827 // Same with trying to load a specific cookie. | 822 // Same with trying to load a specific cookie. |
828 was_called_with_no_cookies = false; | 823 was_called_with_no_cookies = false; |
829 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, | 824 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, |
830 &was_called_with_no_cookies)); | 825 &was_called_with_no_cookies)); |
831 EXPECT_TRUE(was_called_with_no_cookies); | 826 EXPECT_TRUE(was_called_with_no_cookies); |
832 } | 827 } |
833 | 828 |
834 } // namespace net | 829 } // namespace net |
OLD | NEW |