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, CookieSameSite::DEFAULT_MODE, 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, CookieSameSite::DEFAULT_MODE, 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, |
448 COOKIE_PRIORITY_DEFAULT)); | 448 CookieSameSite::DEFAULT_MODE, 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, |
475 COOKIE_PRIORITY_DEFAULT)); | 475 CookieSameSite::DEFAULT_MODE, 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, |
505 false, false, COOKIE_PRIORITY_DEFAULT)); | 505 CookieSameSite::DEFAULT_MODE, 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, CookieSameSite::DEFAULT_MODE, 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, CookieSameSite::DEFAULT_MODE, 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, CookieSameSite::DEFAULT_MODE, 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, CookieSameSite::DEFAULT_MODE, 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 const char kNoneName[] = "none"; |
| 606 const char kLaxName[] = "lax"; |
| 607 const char kStrictName[] = "strict"; |
| 608 const char kCookieDomain[] = "sessioncookie.com"; |
| 609 const char kCookieValue[] = "value"; |
| 610 const char kCookiePath[] = "/"; |
| 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, CookieSameSite::NO_RESTRICTION, 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, CookieSameSite::LAX_MODE, 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, CookieSameSite::STRICT_MODE, 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, for comparison below. |
| 645 std::map<std::string, CanonicalCookie*> cookie_map; |
| 646 for (const auto& cookie : cookies) |
| 647 cookie_map[cookie->Name()] = cookie; |
| 648 |
| 649 // Validate that each cookie has the correct SameSite. |
| 650 ASSERT_EQ(1u, cookie_map.count(kNoneName)); |
| 651 EXPECT_EQ(CookieSameSite::NO_RESTRICTION, cookie_map[kNoneName]->SameSite()); |
| 652 |
| 653 ASSERT_EQ(1u, cookie_map.count(kLaxName)); |
| 654 EXPECT_EQ(CookieSameSite::LAX_MODE, cookie_map[kLaxName]->SameSite()); |
| 655 |
| 656 ASSERT_EQ(1u, cookie_map.count(kStrictName)); |
| 657 EXPECT_EQ(CookieSameSite::STRICT_MODE, cookie_map[kStrictName]->SameSite()); |
| 658 |
| 659 STLDeleteElements(&cookies); |
| 660 } |
| 661 |
604 TEST_F(SQLitePersistentCookieStoreTest, UpdateToEncryption) { | 662 TEST_F(SQLitePersistentCookieStoreTest, UpdateToEncryption) { |
605 CanonicalCookieVector cookies; | 663 CanonicalCookieVector cookies; |
606 | 664 |
607 // Create unencrypted cookie store and write something to it. | 665 // Create unencrypted cookie store and write something to it. |
608 InitializeStore(false, false); | 666 InitializeStore(false, false); |
609 AddCookie("name", "value123XYZ", "foo.bar", "/", base::Time::Now()); | 667 AddCookie("name", "value123XYZ", "foo.bar", "/", base::Time::Now()); |
610 DestroyStore(); | 668 DestroyStore(); |
611 | 669 |
612 // Verify that "value" is visible in the file. This is necessary in order to | 670 // 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. | 671 // 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); | 804 EXPECT_TRUE(was_called_with_no_cookies); |
747 | 805 |
748 // Same with trying to load a specific cookie. | 806 // Same with trying to load a specific cookie. |
749 was_called_with_no_cookies = false; | 807 was_called_with_no_cookies = false; |
750 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, | 808 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, |
751 &was_called_with_no_cookies)); | 809 &was_called_with_no_cookies)); |
752 EXPECT_TRUE(was_called_with_no_cookies); | 810 EXPECT_TRUE(was_called_with_no_cookies); |
753 } | 811 } |
754 | 812 |
755 } // namespace net | 813 } // namespace net |
OLD | NEW |