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 <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
161 // We have to create this method to wrap WaitableEvent::Wait, since we cannot | 161 // We have to create this method to wrap WaitableEvent::Wait, since we cannot |
162 // bind a non-void returning method as a Closure. | 162 // bind a non-void returning method as a Closure. |
163 void WaitOnDBEvent() { db_thread_event_.Wait(); } | 163 void WaitOnDBEvent() { db_thread_event_.Wait(); } |
164 | 164 |
165 // Adds a persistent cookie to store_. | 165 // Adds a persistent cookie to store_. |
166 void AddCookie(const std::string& name, | 166 void AddCookie(const std::string& name, |
167 const std::string& value, | 167 const std::string& value, |
168 const std::string& domain, | 168 const std::string& domain, |
169 const std::string& path, | 169 const std::string& path, |
170 const base::Time& creation) { | 170 const base::Time& creation) { |
171 store_->AddCookie(CanonicalCookie(GURL(), name, value, domain, path, | 171 store_->AddCookie(CanonicalCookie( |
172 creation, creation, creation, false, | 172 GURL(), name, value, domain, path, creation, creation, creation, false, |
173 false, false, COOKIE_PRIORITY_DEFAULT)); | 173 false, COOKIE_SAME_SITE_DEFAULT, COOKIE_PRIORITY_DEFAULT)); |
174 } | 174 } |
175 | 175 |
176 void AddCookieWithExpiration(const std::string& name, | 176 void AddCookieWithExpiration(const std::string& name, |
177 const std::string& value, | 177 const std::string& value, |
178 const std::string& domain, | 178 const std::string& domain, |
179 const std::string& path, | 179 const std::string& path, |
180 const base::Time& creation, | 180 const base::Time& creation, |
181 const base::Time& expiration) { | 181 const base::Time& expiration) { |
182 store_->AddCookie(CanonicalCookie(GURL(), name, value, domain, path, | 182 store_->AddCookie(CanonicalCookie( |
183 creation, expiration, creation, false, | 183 GURL(), name, value, domain, path, creation, expiration, creation, |
184 false, false, COOKIE_PRIORITY_DEFAULT)); | 184 false, false, COOKIE_SAME_SITE_DEFAULT, COOKIE_PRIORITY_DEFAULT)); |
185 } | 185 } |
186 | 186 |
187 std::string ReadRawDBContents() { | 187 std::string ReadRawDBContents() { |
188 std::string contents; | 188 std::string contents; |
189 if (!base::ReadFileToString(temp_dir_.path().Append(kCookieFilename), | 189 if (!base::ReadFileToString(temp_dir_.path().Append(kCookieFilename), |
190 &contents)) | 190 &contents)) |
191 return std::string(); | 191 return std::string(); |
192 return contents; | 192 return contents; |
193 } | 193 } |
194 | 194 |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
435 // We forced a write, so now the file will be bigger. | 435 // We forced a write, so now the file will be bigger. |
436 ASSERT_TRUE(base::GetFileInfo(path, &info)); | 436 ASSERT_TRUE(base::GetFileInfo(path, &info)); |
437 ASSERT_GT(info.size, base_size); | 437 ASSERT_GT(info.size, base_size); |
438 } | 438 } |
439 | 439 |
440 // Test loading old session cookies from the disk. | 440 // Test loading old session cookies from the disk. |
441 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { | 441 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { |
442 InitializeStore(false, true); | 442 InitializeStore(false, true); |
443 | 443 |
444 // Add a session cookie. | 444 // Add a session cookie. |
445 store_->AddCookie(CanonicalCookie(GURL(), "C", "D", "sessioncookie.com", "/", | 445 store_->AddCookie(CanonicalCookie( |
446 base::Time::Now(), base::Time(), | 446 GURL(), "C", "D", "sessioncookie.com", "/", base::Time::Now(), |
447 base::Time::Now(), false, false, false, | 447 base::Time(), base::Time::Now(), false, false, COOKIE_SAME_SITE_DEFAULT, |
448 COOKIE_PRIORITY_DEFAULT)); | 448 COOKIE_PRIORITY_DEFAULT)); |
449 | 449 |
450 // Force the store to write its data to the disk. | 450 // Force the store to write its data to the disk. |
451 DestroyStore(); | 451 DestroyStore(); |
452 | 452 |
453 // Create a store that loads session cookies and test that the session cookie | 453 // Create a store that loads session cookies and test that the session cookie |
454 // was loaded. | 454 // was loaded. |
455 CanonicalCookieVector cookies; | 455 CanonicalCookieVector cookies; |
456 CreateAndLoad(false, true, &cookies); | 456 CreateAndLoad(false, true, &cookies); |
457 | 457 |
458 ASSERT_EQ(1U, cookies.size()); | 458 ASSERT_EQ(1U, cookies.size()); |
459 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); | 459 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); |
460 ASSERT_STREQ("C", cookies[0]->Name().c_str()); | 460 ASSERT_STREQ("C", cookies[0]->Name().c_str()); |
461 ASSERT_STREQ("D", cookies[0]->Value().c_str()); | 461 ASSERT_STREQ("D", cookies[0]->Value().c_str()); |
462 ASSERT_EQ(COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); | 462 ASSERT_EQ(COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); |
463 | 463 |
464 STLDeleteElements(&cookies); | 464 STLDeleteElements(&cookies); |
465 } | 465 } |
466 | 466 |
467 // Test loading old session cookies from the disk. | 467 // Test loading old session cookies from the disk. |
468 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { | 468 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { |
469 InitializeStore(false, true); | 469 InitializeStore(false, true); |
470 | 470 |
471 // Add a session cookie. | 471 // Add a session cookie. |
472 store_->AddCookie(CanonicalCookie(GURL(), "C", "D", "sessioncookie.com", "/", | 472 store_->AddCookie(CanonicalCookie( |
473 base::Time::Now(), base::Time(), | 473 GURL(), "C", "D", "sessioncookie.com", "/", base::Time::Now(), |
474 base::Time::Now(), false, false, false, | 474 base::Time(), base::Time::Now(), false, false, COOKIE_SAME_SITE_DEFAULT, |
475 COOKIE_PRIORITY_DEFAULT)); | 475 COOKIE_PRIORITY_DEFAULT)); |
476 | 476 |
477 // Force the store to write its data to the disk. | 477 // Force the store to write its data to the disk. |
478 DestroyStore(); | 478 DestroyStore(); |
479 | 479 |
480 // Create a store that doesn't load old session cookies and test that the | 480 // Create a store that doesn't load old session cookies and test that the |
481 // session cookie was not loaded. | 481 // session cookie was not loaded. |
482 CanonicalCookieVector cookies; | 482 CanonicalCookieVector cookies; |
483 CreateAndLoad(false, false, &cookies); | 483 CreateAndLoad(false, false, &cookies); |
484 ASSERT_EQ(0U, cookies.size()); | 484 ASSERT_EQ(0U, cookies.size()); |
485 | 485 |
486 // The store should also delete the session cookie. Wait until that has been | 486 // The store should also delete the session cookie. Wait until that has been |
487 // done. | 487 // done. |
488 DestroyStore(); | 488 DestroyStore(); |
489 | 489 |
490 // Create a store that loads old session cookies and test that the session | 490 // Create a store that loads old session cookies and test that the session |
491 // cookie is gone. | 491 // cookie is gone. |
492 CreateAndLoad(false, true, &cookies); | 492 CreateAndLoad(false, true, &cookies); |
493 ASSERT_EQ(0U, cookies.size()); | 493 ASSERT_EQ(0U, cookies.size()); |
494 } | 494 } |
495 | 495 |
496 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { | 496 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { |
497 InitializeStore(false, true); | 497 InitializeStore(false, true); |
498 static const char kSessionName[] = "session"; | 498 static const char kSessionName[] = "session"; |
499 static const char kPersistentName[] = "persistent"; | 499 static const char kPersistentName[] = "persistent"; |
500 | 500 |
501 // Add a session cookie. | 501 // Add a session cookie. |
502 store_->AddCookie(CanonicalCookie(GURL(), kSessionName, "val", | 502 store_->AddCookie(CanonicalCookie( |
503 "sessioncookie.com", "/", base::Time::Now(), | 503 GURL(), kSessionName, "val", "sessioncookie.com", "/", base::Time::Now(), |
504 base::Time(), base::Time::Now(), false, | 504 base::Time(), base::Time::Now(), false, false, COOKIE_SAME_SITE_DEFAULT, |
505 false, false, COOKIE_PRIORITY_DEFAULT)); | 505 COOKIE_PRIORITY_DEFAULT)); |
506 // Add a persistent cookie. | 506 // Add a persistent cookie. |
507 store_->AddCookie(CanonicalCookie( | 507 store_->AddCookie(CanonicalCookie( |
508 GURL(), kPersistentName, "val", "sessioncookie.com", "/", | 508 GURL(), kPersistentName, "val", "sessioncookie.com", "/", |
509 base::Time::Now() - base::TimeDelta::FromDays(1), | 509 base::Time::Now() - base::TimeDelta::FromDays(1), |
510 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), | 510 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), |
511 false, false, false, COOKIE_PRIORITY_DEFAULT)); | 511 false, false, COOKIE_SAME_SITE_DEFAULT, COOKIE_PRIORITY_DEFAULT)); |
512 | 512 |
513 // Force the store to write its data to the disk. | 513 // Force the store to write its data to the disk. |
514 DestroyStore(); | 514 DestroyStore(); |
515 | 515 |
516 // Create a store that loads session cookie and test that the IsPersistent | 516 // Create a store that loads session cookie and test that the IsPersistent |
517 // attribute is restored. | 517 // attribute is restored. |
518 CanonicalCookieVector cookies; | 518 CanonicalCookieVector cookies; |
519 CreateAndLoad(false, true, &cookies); | 519 CreateAndLoad(false, true, &cookies); |
520 ASSERT_EQ(2U, cookies.size()); | 520 ASSERT_EQ(2U, cookies.size()); |
521 | 521 |
(...skipping 23 matching lines...) Expand all Loading... | |
545 static const char kCookieValue[] = "value"; | 545 static const char kCookieValue[] = "value"; |
546 static const char kCookiePath[] = "/"; | 546 static const char kCookiePath[] = "/"; |
547 | 547 |
548 InitializeStore(false, true); | 548 InitializeStore(false, true); |
549 | 549 |
550 // Add a low-priority persistent cookie. | 550 // Add a low-priority persistent cookie. |
551 store_->AddCookie(CanonicalCookie( | 551 store_->AddCookie(CanonicalCookie( |
552 GURL(), kLowName, kCookieValue, kCookieDomain, kCookiePath, | 552 GURL(), kLowName, kCookieValue, kCookieDomain, kCookiePath, |
553 base::Time::Now() - base::TimeDelta::FromMinutes(1), | 553 base::Time::Now() - base::TimeDelta::FromMinutes(1), |
554 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), | 554 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), |
555 false, false, false, COOKIE_PRIORITY_LOW)); | 555 false, false, COOKIE_SAME_SITE_DEFAULT, COOKIE_PRIORITY_LOW)); |
556 | 556 |
557 // Add a medium-priority persistent cookie. | 557 // Add a medium-priority persistent cookie. |
558 store_->AddCookie(CanonicalCookie( | 558 store_->AddCookie(CanonicalCookie( |
559 GURL(), kMediumName, kCookieValue, kCookieDomain, kCookiePath, | 559 GURL(), kMediumName, kCookieValue, kCookieDomain, kCookiePath, |
560 base::Time::Now() - base::TimeDelta::FromMinutes(2), | 560 base::Time::Now() - base::TimeDelta::FromMinutes(2), |
561 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), | 561 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), |
562 false, false, false, COOKIE_PRIORITY_MEDIUM)); | 562 false, false, COOKIE_SAME_SITE_DEFAULT, COOKIE_PRIORITY_MEDIUM)); |
563 | 563 |
564 // Add a high-priority peristent cookie. | 564 // Add a high-priority peristent cookie. |
565 store_->AddCookie(CanonicalCookie( | 565 store_->AddCookie(CanonicalCookie( |
566 GURL(), kHighName, kCookieValue, kCookieDomain, kCookiePath, | 566 GURL(), kHighName, kCookieValue, kCookieDomain, kCookiePath, |
567 base::Time::Now() - base::TimeDelta::FromMinutes(3), | 567 base::Time::Now() - base::TimeDelta::FromMinutes(3), |
568 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), | 568 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), |
569 false, false, false, COOKIE_PRIORITY_HIGH)); | 569 false, false, COOKIE_SAME_SITE_DEFAULT, COOKIE_PRIORITY_HIGH)); |
570 | 570 |
571 // Force the store to write its data to the disk. | 571 // Force the store to write its data to the disk. |
572 DestroyStore(); | 572 DestroyStore(); |
573 | 573 |
574 // Create a store that loads session cookie and test that the priority | 574 // Create a store that loads session cookie and test that the priority |
575 // attribute values are restored. | 575 // attribute values are restored. |
576 CanonicalCookieVector cookies; | 576 CanonicalCookieVector cookies; |
577 CreateAndLoad(false, true, &cookies); | 577 CreateAndLoad(false, true, &cookies); |
578 ASSERT_EQ(3U, cookies.size()); | 578 ASSERT_EQ(3U, cookies.size()); |
579 | 579 |
(...skipping 14 matching lines...) Expand all Loading... | |
594 ASSERT_TRUE(it != cookie_map.end()); | 594 ASSERT_TRUE(it != cookie_map.end()); |
595 EXPECT_EQ(COOKIE_PRIORITY_MEDIUM, cookie_map[kMediumName]->Priority()); | 595 EXPECT_EQ(COOKIE_PRIORITY_MEDIUM, cookie_map[kMediumName]->Priority()); |
596 | 596 |
597 it = cookie_map.find(kHighName); | 597 it = cookie_map.find(kHighName); |
598 ASSERT_TRUE(it != cookie_map.end()); | 598 ASSERT_TRUE(it != cookie_map.end()); |
599 EXPECT_EQ(COOKIE_PRIORITY_HIGH, cookie_map[kHighName]->Priority()); | 599 EXPECT_EQ(COOKIE_PRIORITY_HIGH, cookie_map[kHighName]->Priority()); |
600 | 600 |
601 STLDeleteElements(&cookies); | 601 STLDeleteElements(&cookies); |
602 } | 602 } |
603 | 603 |
604 TEST_F(SQLitePersistentCookieStoreTest, SameSiteIsPersistent) { | |
605 static const char kNoneName[] = "none"; | |
606 static const char kLaxName[] = "lax"; | |
607 static const char kStrictName[] = "strict"; | |
608 static const char kCookieDomain[] = "sessioncookie.com"; | |
609 static const char kCookieValue[] = "value"; | |
610 static const char kCookiePath[] = "/"; | |
mmenke
2016/03/11 18:08:31
I think the preference is not to use "static" in t
| |
611 | |
612 InitializeStore(false, true); | |
613 | |
614 // Add a non-samesite cookie. | |
615 store_->AddCookie(CanonicalCookie( | |
616 GURL(), kNoneName, kCookieValue, kCookieDomain, kCookiePath, | |
617 base::Time::Now() - base::TimeDelta::FromMinutes(1), | |
618 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), | |
619 false, false, COOKIE_SAME_SITE_NONE, COOKIE_PRIORITY_DEFAULT)); | |
620 | |
621 // Add a lax-samesite persistent cookie. | |
622 store_->AddCookie(CanonicalCookie( | |
623 GURL(), kLaxName, kCookieValue, kCookieDomain, kCookiePath, | |
624 base::Time::Now() - base::TimeDelta::FromMinutes(2), | |
625 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), | |
626 false, false, COOKIE_SAME_SITE_LAX, COOKIE_PRIORITY_DEFAULT)); | |
627 | |
628 // Add a strict-samesite peristent cookie. | |
629 store_->AddCookie(CanonicalCookie( | |
630 GURL(), kStrictName, kCookieValue, kCookieDomain, kCookiePath, | |
631 base::Time::Now() - base::TimeDelta::FromMinutes(3), | |
632 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), | |
633 false, false, COOKIE_SAME_SITE_STRICT, COOKIE_PRIORITY_DEFAULT)); | |
634 | |
635 // Force the store to write its data to the disk. | |
636 DestroyStore(); | |
637 | |
638 // Create a store that loads session cookie and test that the priority | |
639 // attribute values are restored. | |
640 CanonicalCookieVector cookies; | |
641 CreateAndLoad(false, true, &cookies); | |
642 ASSERT_EQ(3U, cookies.size()); | |
643 | |
644 // Put the cookies into a map, by name, so we can easily find them. | |
mmenke
2016/03/11 18:08:31
nit: --"we"
Mike West
2016/03/14 10:18:54
1. Ugh. I continue to not like this rule. :/
2. Th
mmenke
2016/03/14 20:14:50
Up to you.
| |
645 std::map<std::string, CanonicalCookie*> cookie_map; | |
646 for (CanonicalCookieVector::const_iterator it = cookies.begin(); | |
647 it != cookies.end(); ++it) { | |
648 cookie_map[(*it)->Name()] = *it; | |
649 } | |
mmenke
2016/03/11 18:08:31
optional: Suggest just using a range loop, and ma
Mike West
2016/03/14 10:18:54
Done.
| |
650 | |
651 // Validate that each cookie has the correct samesite. | |
mmenke
2016/03/11 18:08:31
SameSite?
Mike West
2016/03/14 10:18:54
Indeed.
| |
652 std::map<std::string, CanonicalCookie*>::const_iterator it = | |
653 cookie_map.find(kNoneName); | |
654 ASSERT_TRUE(it != cookie_map.end()); | |
mmenke
2016/03/11 18:08:31
Combining these two lines seems a little simpler,
Mike West
2016/03/14 10:18:54
Yup. Makes sense.
| |
655 EXPECT_EQ(COOKIE_SAME_SITE_NONE, cookie_map[kNoneName]->SameSite()); | |
656 | |
657 it = cookie_map.find(kLaxName); | |
658 ASSERT_TRUE(it != cookie_map.end()); | |
659 EXPECT_EQ(COOKIE_SAME_SITE_LAX, cookie_map[kLaxName]->SameSite()); | |
660 | |
661 it = cookie_map.find(kStrictName); | |
662 ASSERT_TRUE(it != cookie_map.end()); | |
663 EXPECT_EQ(COOKIE_SAME_SITE_STRICT, cookie_map[kStrictName]->SameSite()); | |
664 | |
665 STLDeleteElements(&cookies); | |
666 } | |
667 | |
604 TEST_F(SQLitePersistentCookieStoreTest, UpdateToEncryption) { | 668 TEST_F(SQLitePersistentCookieStoreTest, UpdateToEncryption) { |
605 CanonicalCookieVector cookies; | 669 CanonicalCookieVector cookies; |
606 | 670 |
607 // Create unencrypted cookie store and write something to it. | 671 // Create unencrypted cookie store and write something to it. |
608 InitializeStore(false, false); | 672 InitializeStore(false, false); |
609 AddCookie("name", "value123XYZ", "foo.bar", "/", base::Time::Now()); | 673 AddCookie("name", "value123XYZ", "foo.bar", "/", base::Time::Now()); |
610 DestroyStore(); | 674 DestroyStore(); |
611 | 675 |
612 // Verify that "value" is visible in the file. This is necessary in order to | 676 // Verify that "value" is visible in the file. This is necessary in order to |
613 // have confidence in a later test that "encrypted_value" is not visible. | 677 // have confidence in a later test that "encrypted_value" is not visible. |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
746 EXPECT_TRUE(was_called_with_no_cookies); | 810 EXPECT_TRUE(was_called_with_no_cookies); |
747 | 811 |
748 // Same with trying to load a specific cookie. | 812 // Same with trying to load a specific cookie. |
749 was_called_with_no_cookies = false; | 813 was_called_with_no_cookies = false; |
750 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, | 814 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, |
751 &was_called_with_no_cookies)); | 815 &was_called_with_no_cookies)); |
752 EXPECT_TRUE(was_called_with_no_cookies); | 816 EXPECT_TRUE(was_called_with_no_cookies); |
753 } | 817 } |
754 | 818 |
755 } // namespace net | 819 } // namespace net |
OLD | NEW |