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 <memory> |
9 #include <set> | 9 #include <set> |
10 | 10 |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 // We have to create this method to wrap WaitableEvent::Wait, since we cannot | 165 // We have to create this method to wrap WaitableEvent::Wait, since we cannot |
166 // bind a non-void returning method as a Closure. | 166 // bind a non-void returning method as a Closure. |
167 void WaitOnDBEvent() { db_thread_event_.Wait(); } | 167 void WaitOnDBEvent() { db_thread_event_.Wait(); } |
168 | 168 |
169 // Adds a persistent cookie to store_. | 169 // Adds a persistent cookie to store_. |
170 void AddCookie(const std::string& name, | 170 void AddCookie(const std::string& name, |
171 const std::string& value, | 171 const std::string& value, |
172 const std::string& domain, | 172 const std::string& domain, |
173 const std::string& path, | 173 const std::string& path, |
174 const base::Time& creation) { | 174 const base::Time& creation) { |
175 store_->AddCookie(*CanonicalCookie::Create( | 175 store_->AddCookie(CanonicalCookie( |
176 name, value, domain, path, creation, creation, base::Time(), false, | 176 name, value, domain, path, creation, creation, base::Time(), false, |
177 false, CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); | 177 false, CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); |
178 } | 178 } |
179 | 179 |
180 void AddCookieWithExpiration(const std::string& name, | 180 void AddCookieWithExpiration(const std::string& name, |
181 const std::string& value, | 181 const std::string& value, |
182 const std::string& domain, | 182 const std::string& domain, |
183 const std::string& path, | 183 const std::string& path, |
184 const base::Time& creation, | 184 const base::Time& creation, |
185 const base::Time& expiration) { | 185 const base::Time& expiration) { |
186 store_->AddCookie(*CanonicalCookie::Create( | 186 store_->AddCookie(CanonicalCookie( |
187 name, value, domain, path, creation, expiration, base::Time(), false, | 187 name, value, domain, path, creation, expiration, base::Time(), false, |
188 false, CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); | 188 false, CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); |
189 } | 189 } |
190 | 190 |
191 std::string ReadRawDBContents() { | 191 std::string ReadRawDBContents() { |
192 std::string contents; | 192 std::string contents; |
193 if (!base::ReadFileToString(temp_dir_.GetPath().Append(kCookieFilename), | 193 if (!base::ReadFileToString(temp_dir_.GetPath().Append(kCookieFilename), |
194 &contents)) | 194 &contents)) |
195 return std::string(); | 195 return std::string(); |
196 return contents; | 196 return contents; |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 // We forced a write, so now the file will be bigger. | 440 // We forced a write, so now the file will be bigger. |
441 ASSERT_TRUE(base::GetFileInfo(path, &info)); | 441 ASSERT_TRUE(base::GetFileInfo(path, &info)); |
442 ASSERT_GT(info.size, base_size); | 442 ASSERT_GT(info.size, base_size); |
443 } | 443 } |
444 | 444 |
445 // Test loading old session cookies from the disk. | 445 // Test loading old session cookies from the disk. |
446 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { | 446 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { |
447 InitializeStore(false, true); | 447 InitializeStore(false, true); |
448 | 448 |
449 // Add a session cookie. | 449 // Add a session cookie. |
450 store_->AddCookie(*CanonicalCookie::Create( | 450 store_->AddCookie( |
451 "C", "D", "sessioncookie.com", "/", base::Time::Now(), base::Time(), | 451 CanonicalCookie("C", "D", "sessioncookie.com", "/", base::Time::Now(), |
452 base::Time(), false, false, CookieSameSite::DEFAULT_MODE, | 452 base::Time(), base::Time(), false, false, |
453 COOKIE_PRIORITY_DEFAULT)); | 453 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); |
454 | 454 |
455 // Force the store to write its data to the disk. | 455 // Force the store to write its data to the disk. |
456 DestroyStore(); | 456 DestroyStore(); |
457 | 457 |
458 // Create a store that loads session cookies and test that the session cookie | 458 // Create a store that loads session cookies and test that the session cookie |
459 // was loaded. | 459 // was loaded. |
460 CanonicalCookieVector cookies; | 460 CanonicalCookieVector cookies; |
461 CreateAndLoad(false, true, &cookies); | 461 CreateAndLoad(false, true, &cookies); |
462 | 462 |
463 ASSERT_EQ(1U, cookies.size()); | 463 ASSERT_EQ(1U, cookies.size()); |
464 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); | 464 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); |
465 ASSERT_STREQ("C", cookies[0]->Name().c_str()); | 465 ASSERT_STREQ("C", cookies[0]->Name().c_str()); |
466 ASSERT_STREQ("D", cookies[0]->Value().c_str()); | 466 ASSERT_STREQ("D", cookies[0]->Value().c_str()); |
467 ASSERT_EQ(COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); | 467 ASSERT_EQ(COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); |
468 | 468 |
469 cookies.clear(); | 469 cookies.clear(); |
470 } | 470 } |
471 | 471 |
472 // Test loading old session cookies from the disk. | 472 // Test loading old session cookies from the disk. |
473 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { | 473 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { |
474 InitializeStore(false, true); | 474 InitializeStore(false, true); |
475 | 475 |
476 // Add a session cookie. | 476 // Add a session cookie. |
477 store_->AddCookie(*CanonicalCookie::Create( | 477 store_->AddCookie( |
478 "C", "D", "sessioncookie.com", "/", base::Time::Now(), base::Time(), | 478 CanonicalCookie("C", "D", "sessioncookie.com", "/", base::Time::Now(), |
479 base::Time(), false, false, CookieSameSite::DEFAULT_MODE, | 479 base::Time(), base::Time(), false, false, |
480 COOKIE_PRIORITY_DEFAULT)); | 480 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); |
481 | 481 |
482 // Force the store to write its data to the disk. | 482 // Force the store to write its data to the disk. |
483 DestroyStore(); | 483 DestroyStore(); |
484 | 484 |
485 // Create a store that doesn't load old session cookies and test that the | 485 // Create a store that doesn't load old session cookies and test that the |
486 // session cookie was not loaded. | 486 // session cookie was not loaded. |
487 CanonicalCookieVector cookies; | 487 CanonicalCookieVector cookies; |
488 CreateAndLoad(false, false, &cookies); | 488 CreateAndLoad(false, false, &cookies); |
489 ASSERT_EQ(0U, cookies.size()); | 489 ASSERT_EQ(0U, cookies.size()); |
490 | 490 |
491 // The store should also delete the session cookie. Wait until that has been | 491 // The store should also delete the session cookie. Wait until that has been |
492 // done. | 492 // done. |
493 DestroyStore(); | 493 DestroyStore(); |
494 | 494 |
495 // Create a store that loads old session cookies and test that the session | 495 // Create a store that loads old session cookies and test that the session |
496 // cookie is gone. | 496 // cookie is gone. |
497 CreateAndLoad(false, true, &cookies); | 497 CreateAndLoad(false, true, &cookies); |
498 ASSERT_EQ(0U, cookies.size()); | 498 ASSERT_EQ(0U, cookies.size()); |
499 } | 499 } |
500 | 500 |
501 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { | 501 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { |
502 InitializeStore(false, true); | 502 InitializeStore(false, true); |
503 static const char kSessionName[] = "session"; | 503 static const char kSessionName[] = "session"; |
504 static const char kPersistentName[] = "persistent"; | 504 static const char kPersistentName[] = "persistent"; |
505 | 505 |
506 // Add a session cookie. | 506 // Add a session cookie. |
507 store_->AddCookie(*CanonicalCookie::Create( | 507 store_->AddCookie(CanonicalCookie( |
508 kSessionName, "val", "sessioncookie.com", "/", base::Time::Now(), | 508 kSessionName, "val", "sessioncookie.com", "/", base::Time::Now(), |
509 base::Time(), base::Time(), false, false, CookieSameSite::DEFAULT_MODE, | 509 base::Time(), base::Time(), false, false, CookieSameSite::DEFAULT_MODE, |
510 COOKIE_PRIORITY_DEFAULT)); | 510 COOKIE_PRIORITY_DEFAULT)); |
511 // Add a persistent cookie. | 511 // Add a persistent cookie. |
512 store_->AddCookie(*CanonicalCookie::Create( | 512 store_->AddCookie(CanonicalCookie( |
513 kPersistentName, "val", "sessioncookie.com", "/", | 513 kPersistentName, "val", "sessioncookie.com", "/", |
514 base::Time::Now() - base::TimeDelta::FromDays(1), | 514 base::Time::Now() - base::TimeDelta::FromDays(1), |
515 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time(), false, | 515 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time(), false, |
516 false, CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); | 516 false, CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); |
517 | 517 |
518 // Force the store to write its data to the disk. | 518 // Force the store to write its data to the disk. |
519 DestroyStore(); | 519 DestroyStore(); |
520 | 520 |
521 // Create a store that loads session cookie and test that the IsPersistent | 521 // Create a store that loads session cookie and test that the IsPersistent |
522 // attribute is restored. | 522 // attribute is restored. |
(...skipping 20 matching lines...) Expand all Loading... |
543 static const char kDomain[] = "sessioncookie.com"; | 543 static const char kDomain[] = "sessioncookie.com"; |
544 static const char kLowName[] = "low"; | 544 static const char kLowName[] = "low"; |
545 static const char kMediumName[] = "medium"; | 545 static const char kMediumName[] = "medium"; |
546 static const char kHighName[] = "high"; | 546 static const char kHighName[] = "high"; |
547 static const char kCookieValue[] = "value"; | 547 static const char kCookieValue[] = "value"; |
548 static const char kCookiePath[] = "/"; | 548 static const char kCookiePath[] = "/"; |
549 | 549 |
550 InitializeStore(false, true); | 550 InitializeStore(false, true); |
551 | 551 |
552 // Add a low-priority persistent cookie. | 552 // Add a low-priority persistent cookie. |
553 store_->AddCookie(*CanonicalCookie::Create( | 553 store_->AddCookie(CanonicalCookie( |
554 kLowName, kCookieValue, kDomain, kCookiePath, | 554 kLowName, kCookieValue, kDomain, kCookiePath, |
555 base::Time::Now() - base::TimeDelta::FromMinutes(1), | 555 base::Time::Now() - base::TimeDelta::FromMinutes(1), |
556 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time(), false, | 556 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time(), false, |
557 false, CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_LOW)); | 557 false, CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_LOW)); |
558 | 558 |
559 // Add a medium-priority persistent cookie. | 559 // Add a medium-priority persistent cookie. |
560 store_->AddCookie(*CanonicalCookie::Create( | 560 store_->AddCookie(CanonicalCookie( |
561 kMediumName, kCookieValue, kDomain, kCookiePath, | 561 kMediumName, kCookieValue, kDomain, kCookiePath, |
562 base::Time::Now() - base::TimeDelta::FromMinutes(2), | 562 base::Time::Now() - base::TimeDelta::FromMinutes(2), |
563 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time(), false, | 563 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time(), false, |
564 false, CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_MEDIUM)); | 564 false, CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_MEDIUM)); |
565 | 565 |
566 // Add a high-priority peristent cookie. | 566 // Add a high-priority peristent cookie. |
567 store_->AddCookie(*CanonicalCookie::Create( | 567 store_->AddCookie(CanonicalCookie( |
568 kHighName, kCookieValue, kDomain, kCookiePath, | 568 kHighName, kCookieValue, kDomain, kCookiePath, |
569 base::Time::Now() - base::TimeDelta::FromMinutes(3), | 569 base::Time::Now() - base::TimeDelta::FromMinutes(3), |
570 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time(), false, | 570 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time(), false, |
571 false, CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_HIGH)); | 571 false, CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_HIGH)); |
572 | 572 |
573 // Force the store to write its data to the disk. | 573 // Force the store to write its data to the disk. |
574 DestroyStore(); | 574 DestroyStore(); |
575 | 575 |
576 // Create a store that loads session cookie and test that the priority | 576 // Create a store that loads session cookie and test that the priority |
577 // attribute values are restored. | 577 // attribute values are restored. |
(...skipping 26 matching lines...) Expand all Loading... |
604 const char kDomain[] = "sessioncookie.com"; | 604 const char kDomain[] = "sessioncookie.com"; |
605 const char kNoneName[] = "none"; | 605 const char kNoneName[] = "none"; |
606 const char kLaxName[] = "lax"; | 606 const char kLaxName[] = "lax"; |
607 const char kStrictName[] = "strict"; | 607 const char kStrictName[] = "strict"; |
608 const char kCookieValue[] = "value"; | 608 const char kCookieValue[] = "value"; |
609 const char kCookiePath[] = "/"; | 609 const char kCookiePath[] = "/"; |
610 | 610 |
611 InitializeStore(false, true); | 611 InitializeStore(false, true); |
612 | 612 |
613 // Add a non-samesite cookie. | 613 // Add a non-samesite cookie. |
614 store_->AddCookie(*CanonicalCookie::Create( | 614 store_->AddCookie(CanonicalCookie( |
615 kNoneName, kCookieValue, kDomain, kCookiePath, | 615 kNoneName, kCookieValue, kDomain, kCookiePath, |
616 base::Time::Now() - base::TimeDelta::FromMinutes(1), | 616 base::Time::Now() - base::TimeDelta::FromMinutes(1), |
617 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time(), false, | 617 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time(), false, |
618 false, CookieSameSite::NO_RESTRICTION, COOKIE_PRIORITY_DEFAULT)); | 618 false, CookieSameSite::NO_RESTRICTION, COOKIE_PRIORITY_DEFAULT)); |
619 | 619 |
620 // Add a lax-samesite persistent cookie. | 620 // Add a lax-samesite persistent cookie. |
621 store_->AddCookie(*CanonicalCookie::Create( | 621 store_->AddCookie(CanonicalCookie( |
622 kLaxName, kCookieValue, kDomain, kCookiePath, | 622 kLaxName, kCookieValue, kDomain, kCookiePath, |
623 base::Time::Now() - base::TimeDelta::FromMinutes(2), | 623 base::Time::Now() - base::TimeDelta::FromMinutes(2), |
624 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time(), false, | 624 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time(), false, |
625 false, CookieSameSite::LAX_MODE, COOKIE_PRIORITY_DEFAULT)); | 625 false, CookieSameSite::LAX_MODE, COOKIE_PRIORITY_DEFAULT)); |
626 | 626 |
627 // Add a strict-samesite persistent cookie. | 627 // Add a strict-samesite persistent cookie. |
628 store_->AddCookie(*CanonicalCookie::Create( | 628 store_->AddCookie(CanonicalCookie( |
629 kStrictName, kCookieValue, kDomain, kCookiePath, | 629 kStrictName, kCookieValue, kDomain, kCookiePath, |
630 base::Time::Now() - base::TimeDelta::FromMinutes(3), | 630 base::Time::Now() - base::TimeDelta::FromMinutes(3), |
631 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time(), false, | 631 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time(), false, |
632 false, CookieSameSite::STRICT_MODE, COOKIE_PRIORITY_DEFAULT)); | 632 false, CookieSameSite::STRICT_MODE, COOKIE_PRIORITY_DEFAULT)); |
633 | 633 |
634 // Force the store to write its data to the disk. | 634 // Force the store to write its data to the disk. |
635 DestroyStore(); | 635 DestroyStore(); |
636 | 636 |
637 // Create a store that loads session cookie and test that the priority | 637 // Create a store that loads session cookie and test that the priority |
638 // attribute values are restored. | 638 // attribute values are restored. |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
804 EXPECT_TRUE(was_called_with_no_cookies); | 804 EXPECT_TRUE(was_called_with_no_cookies); |
805 | 805 |
806 // Same with trying to load a specific cookie. | 806 // Same with trying to load a specific cookie. |
807 was_called_with_no_cookies = false; | 807 was_called_with_no_cookies = false; |
808 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, | 808 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, |
809 &was_called_with_no_cookies)); | 809 &was_called_with_no_cookies)); |
810 EXPECT_TRUE(was_called_with_no_cookies); | 810 EXPECT_TRUE(was_called_with_no_cookies); |
811 } | 811 } |
812 | 812 |
813 } // namespace net | 813 } // namespace net |
OLD | NEW |