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 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 168 |
169 // Adds a persistent cookie to store_. | 169 // Adds a persistent cookie to store_. |
170 void AddCookie(const GURL& url, | 170 void AddCookie(const GURL& url, |
171 const std::string& name, | 171 const std::string& name, |
172 const std::string& value, | 172 const std::string& value, |
173 const std::string& domain, | 173 const std::string& domain, |
174 const std::string& path, | 174 const std::string& path, |
175 const base::Time& creation) { | 175 const base::Time& creation) { |
176 store_->AddCookie(*CanonicalCookie::Create( | 176 store_->AddCookie(*CanonicalCookie::Create( |
177 url, name, value, domain, path, creation, creation, false, false, | 177 url, name, value, domain, path, creation, creation, false, false, |
178 CookieSameSite::DEFAULT_MODE, false, COOKIE_PRIORITY_DEFAULT)); | 178 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); |
179 } | 179 } |
180 | 180 |
181 void AddCookieWithExpiration(const GURL& url, | 181 void AddCookieWithExpiration(const GURL& url, |
182 const std::string& name, | 182 const std::string& name, |
183 const std::string& value, | 183 const std::string& value, |
184 const std::string& domain, | 184 const std::string& domain, |
185 const std::string& path, | 185 const std::string& path, |
186 const base::Time& creation, | 186 const base::Time& creation, |
187 const base::Time& expiration) { | 187 const base::Time& expiration) { |
188 store_->AddCookie(*CanonicalCookie::Create( | 188 store_->AddCookie(*CanonicalCookie::Create( |
189 url, name, value, domain, path, creation, expiration, false, false, | 189 url, name, value, domain, path, creation, expiration, false, false, |
190 CookieSameSite::DEFAULT_MODE, false, COOKIE_PRIORITY_DEFAULT)); | 190 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); |
191 } | 191 } |
192 | 192 |
193 std::string ReadRawDBContents() { | 193 std::string ReadRawDBContents() { |
194 std::string contents; | 194 std::string contents; |
195 if (!base::ReadFileToString(temp_dir_.GetPath().Append(kCookieFilename), | 195 if (!base::ReadFileToString(temp_dir_.GetPath().Append(kCookieFilename), |
196 &contents)) | 196 &contents)) |
197 return std::string(); | 197 return std::string(); |
198 return contents; | 198 return contents; |
199 } | 199 } |
200 | 200 |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 } | 454 } |
455 | 455 |
456 // Test loading old session cookies from the disk. | 456 // Test loading old session cookies from the disk. |
457 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { | 457 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { |
458 InitializeStore(false, true); | 458 InitializeStore(false, true); |
459 | 459 |
460 // Add a session cookie. | 460 // Add a session cookie. |
461 store_->AddCookie(*CanonicalCookie::Create( | 461 store_->AddCookie(*CanonicalCookie::Create( |
462 GURL("http://sessioncookie.com"), "C", "D", std::string(), "/", | 462 GURL("http://sessioncookie.com"), "C", "D", std::string(), "/", |
463 base::Time::Now(), base::Time(), false, false, | 463 base::Time::Now(), base::Time(), false, false, |
464 CookieSameSite::DEFAULT_MODE, false, COOKIE_PRIORITY_DEFAULT)); | 464 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); |
465 | 465 |
466 // Force the store to write its data to the disk. | 466 // Force the store to write its data to the disk. |
467 DestroyStore(); | 467 DestroyStore(); |
468 | 468 |
469 // Create a store that loads session cookies and test that the session cookie | 469 // Create a store that loads session cookies and test that the session cookie |
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 cookies.clear(); | 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, |
491 CookieSameSite::DEFAULT_MODE, false, COOKIE_PRIORITY_DEFAULT)); | 491 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); |
492 | 492 |
493 // Force the store to write its data to the disk. | 493 // Force the store to write its data to the disk. |
494 DestroyStore(); | 494 DestroyStore(); |
495 | 495 |
496 // Create a store that doesn't load old session cookies and test that the | 496 // Create a store that doesn't load old session cookies and test that the |
497 // session cookie was not loaded. | 497 // session cookie was not loaded. |
498 CanonicalCookieVector cookies; | 498 CanonicalCookieVector cookies; |
499 CreateAndLoad(false, false, &cookies); | 499 CreateAndLoad(false, false, &cookies); |
500 ASSERT_EQ(0U, cookies.size()); | 500 ASSERT_EQ(0U, cookies.size()); |
501 | 501 |
502 // The store should also delete the session cookie. Wait until that has been | 502 // The store should also delete the session cookie. Wait until that has been |
503 // done. | 503 // done. |
504 DestroyStore(); | 504 DestroyStore(); |
505 | 505 |
506 // Create a store that loads old session cookies and test that the session | 506 // Create a store that loads old session cookies and test that the session |
507 // cookie is gone. | 507 // cookie is gone. |
508 CreateAndLoad(false, true, &cookies); | 508 CreateAndLoad(false, true, &cookies); |
509 ASSERT_EQ(0U, cookies.size()); | 509 ASSERT_EQ(0U, cookies.size()); |
510 } | 510 } |
511 | 511 |
512 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { | 512 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { |
513 InitializeStore(false, true); | 513 InitializeStore(false, true); |
514 static const char kSessionName[] = "session"; | 514 static const char kSessionName[] = "session"; |
515 static const char kPersistentName[] = "persistent"; | 515 static const char kPersistentName[] = "persistent"; |
516 | 516 |
517 // Add a session cookie. | 517 // Add a session cookie. |
518 store_->AddCookie(*CanonicalCookie::Create( | 518 store_->AddCookie(*CanonicalCookie::Create( |
519 GURL("http://sessioncookie.com"), kSessionName, "val", std::string(), "/", | 519 GURL("http://sessioncookie.com"), kSessionName, "val", std::string(), "/", |
520 base::Time::Now(), base::Time(), false, false, | 520 base::Time::Now(), base::Time(), false, false, |
521 CookieSameSite::DEFAULT_MODE, false, COOKIE_PRIORITY_DEFAULT)); | 521 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); |
522 // Add a persistent cookie. | 522 // Add a persistent cookie. |
523 store_->AddCookie(*CanonicalCookie::Create( | 523 store_->AddCookie(*CanonicalCookie::Create( |
524 GURL("http://sessioncookie.com"), kPersistentName, "val", std::string(), | 524 GURL("http://sessioncookie.com"), kPersistentName, "val", std::string(), |
525 "/", base::Time::Now() - base::TimeDelta::FromDays(1), | 525 "/", base::Time::Now() - base::TimeDelta::FromDays(1), |
526 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, | 526 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, |
527 CookieSameSite::DEFAULT_MODE, false, COOKIE_PRIORITY_DEFAULT)); | 527 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); |
528 | 528 |
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 |
(...skipping 20 matching lines...) Expand all Loading... |
558 static const char kCookieValue[] = "value"; | 558 static const char kCookieValue[] = "value"; |
559 static const char kCookiePath[] = "/"; | 559 static const char kCookiePath[] = "/"; |
560 | 560 |
561 InitializeStore(false, true); | 561 InitializeStore(false, true); |
562 | 562 |
563 // Add a low-priority persistent cookie. | 563 // Add a low-priority persistent cookie. |
564 store_->AddCookie(*CanonicalCookie::Create( | 564 store_->AddCookie(*CanonicalCookie::Create( |
565 GURL(kURL), kLowName, kCookieValue, std::string(), kCookiePath, | 565 GURL(kURL), kLowName, kCookieValue, std::string(), kCookiePath, |
566 base::Time::Now() - base::TimeDelta::FromMinutes(1), | 566 base::Time::Now() - base::TimeDelta::FromMinutes(1), |
567 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, | 567 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, |
568 CookieSameSite::DEFAULT_MODE, false, COOKIE_PRIORITY_LOW)); | 568 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_LOW)); |
569 | 569 |
570 // Add a medium-priority persistent cookie. | 570 // Add a medium-priority persistent cookie. |
571 store_->AddCookie(*CanonicalCookie::Create( | 571 store_->AddCookie(*CanonicalCookie::Create( |
572 GURL(kURL), kMediumName, kCookieValue, std::string(), kCookiePath, | 572 GURL(kURL), kMediumName, kCookieValue, std::string(), kCookiePath, |
573 base::Time::Now() - base::TimeDelta::FromMinutes(2), | 573 base::Time::Now() - base::TimeDelta::FromMinutes(2), |
574 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, | 574 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, |
575 CookieSameSite::DEFAULT_MODE, false, COOKIE_PRIORITY_MEDIUM)); | 575 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_MEDIUM)); |
576 | 576 |
577 // Add a high-priority peristent cookie. | 577 // Add a high-priority peristent cookie. |
578 store_->AddCookie(*CanonicalCookie::Create( | 578 store_->AddCookie(*CanonicalCookie::Create( |
579 GURL(kURL), kHighName, kCookieValue, std::string(), kCookiePath, | 579 GURL(kURL), kHighName, kCookieValue, std::string(), kCookiePath, |
580 base::Time::Now() - base::TimeDelta::FromMinutes(3), | 580 base::Time::Now() - base::TimeDelta::FromMinutes(3), |
581 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, | 581 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, |
582 CookieSameSite::DEFAULT_MODE, false, COOKIE_PRIORITY_HIGH)); | 582 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_HIGH)); |
583 | 583 |
584 // Force the store to write its data to the disk. | 584 // Force the store to write its data to the disk. |
585 DestroyStore(); | 585 DestroyStore(); |
586 | 586 |
587 // 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 |
588 // attribute values are restored. | 588 // attribute values are restored. |
589 CanonicalCookieVector cookies; | 589 CanonicalCookieVector cookies; |
590 CreateAndLoad(false, true, &cookies); | 590 CreateAndLoad(false, true, &cookies); |
591 ASSERT_EQ(3U, cookies.size()); | 591 ASSERT_EQ(3U, cookies.size()); |
592 | 592 |
(...skipping 26 matching lines...) Expand all Loading... |
619 const char kCookieValue[] = "value"; | 619 const char kCookieValue[] = "value"; |
620 const char kCookiePath[] = "/"; | 620 const char kCookiePath[] = "/"; |
621 | 621 |
622 InitializeStore(false, true); | 622 InitializeStore(false, true); |
623 | 623 |
624 // Add a non-samesite cookie. | 624 // Add a non-samesite cookie. |
625 store_->AddCookie(*CanonicalCookie::Create( | 625 store_->AddCookie(*CanonicalCookie::Create( |
626 GURL(kURL), kNoneName, kCookieValue, std::string(), kCookiePath, | 626 GURL(kURL), kNoneName, kCookieValue, std::string(), kCookiePath, |
627 base::Time::Now() - base::TimeDelta::FromMinutes(1), | 627 base::Time::Now() - base::TimeDelta::FromMinutes(1), |
628 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, | 628 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, |
629 CookieSameSite::NO_RESTRICTION, false, COOKIE_PRIORITY_DEFAULT)); | 629 CookieSameSite::NO_RESTRICTION, COOKIE_PRIORITY_DEFAULT)); |
630 | 630 |
631 // Add a lax-samesite persistent cookie. | 631 // Add a lax-samesite persistent cookie. |
632 store_->AddCookie(*CanonicalCookie::Create( | 632 store_->AddCookie(*CanonicalCookie::Create( |
633 GURL(kURL), kLaxName, kCookieValue, std::string(), kCookiePath, | 633 GURL(kURL), kLaxName, kCookieValue, std::string(), kCookiePath, |
634 base::Time::Now() - base::TimeDelta::FromMinutes(2), | 634 base::Time::Now() - base::TimeDelta::FromMinutes(2), |
635 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, | 635 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, |
636 CookieSameSite::LAX_MODE, false, COOKIE_PRIORITY_DEFAULT)); | 636 CookieSameSite::LAX_MODE, COOKIE_PRIORITY_DEFAULT)); |
637 | 637 |
638 // Add a strict-samesite persistent cookie. | 638 // Add a strict-samesite persistent cookie. |
639 store_->AddCookie(*CanonicalCookie::Create( | 639 store_->AddCookie(*CanonicalCookie::Create( |
640 GURL(kURL), kStrictName, kCookieValue, std::string(), kCookiePath, | 640 GURL(kURL), kStrictName, kCookieValue, std::string(), kCookiePath, |
641 base::Time::Now() - base::TimeDelta::FromMinutes(3), | 641 base::Time::Now() - base::TimeDelta::FromMinutes(3), |
642 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, | 642 base::Time::Now() + base::TimeDelta::FromDays(1), false, false, |
643 CookieSameSite::STRICT_MODE, false, COOKIE_PRIORITY_DEFAULT)); | 643 CookieSameSite::STRICT_MODE, COOKIE_PRIORITY_DEFAULT)); |
644 | 644 |
645 // Force the store to write its data to the disk. | 645 // Force the store to write its data to the disk. |
646 DestroyStore(); | 646 DestroyStore(); |
647 | 647 |
648 // 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 |
649 // attribute values are restored. | 649 // attribute values are restored. |
650 CanonicalCookieVector cookies; | 650 CanonicalCookieVector cookies; |
651 CreateAndLoad(false, true, &cookies); | 651 CreateAndLoad(false, true, &cookies); |
652 ASSERT_EQ(3U, cookies.size()); | 652 ASSERT_EQ(3U, cookies.size()); |
653 | 653 |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
820 EXPECT_TRUE(was_called_with_no_cookies); | 820 EXPECT_TRUE(was_called_with_no_cookies); |
821 | 821 |
822 // Same with trying to load a specific cookie. | 822 // Same with trying to load a specific cookie. |
823 was_called_with_no_cookies = false; | 823 was_called_with_no_cookies = false; |
824 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, | 824 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, |
825 &was_called_with_no_cookies)); | 825 &was_called_with_no_cookies)); |
826 EXPECT_TRUE(was_called_with_no_cookies); | 826 EXPECT_TRUE(was_called_with_no_cookies); |
827 } | 827 } |
828 | 828 |
829 } // namespace net | 829 } // namespace net |
OLD | NEW |