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 #ifndef NET_COOKIES_COOKIE_STORE_UNITTEST_H_ | 5 #ifndef NET_COOKIES_COOKIE_STORE_UNITTEST_H_ |
6 #define NET_COOKIES_COOKIE_STORE_UNITTEST_H_ | 6 #define NET_COOKIES_COOKIE_STORE_UNITTEST_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 ResultSavingCookieCallback<bool> callback; | 174 ResultSavingCookieCallback<bool> callback; |
175 cs->SetCookieWithDetailsAsync( | 175 cs->SetCookieWithDetailsAsync( |
176 url, name, value, domain, path, creation_time, expiration_time, | 176 url, name, value, domain, path, creation_time, expiration_time, |
177 last_access_time, secure, http_only, same_site, priority, | 177 last_access_time, secure, http_only, same_site, priority, |
178 base::Bind(&ResultSavingCookieCallback<bool>::Run, | 178 base::Bind(&ResultSavingCookieCallback<bool>::Run, |
179 base::Unretained(&callback))); | 179 base::Unretained(&callback))); |
180 callback.WaitUntilDone(); | 180 callback.WaitUntilDone(); |
181 return callback.result(); | 181 return callback.result(); |
182 } | 182 } |
183 | 183 |
184 bool SetCanonicalCookie(CookieStore* cs, | |
185 const CanonicalCookie& cookie, | |
186 bool secure_source, | |
187 bool can_modify_httponly) { | |
188 DCHECK(cs); | |
189 ResultSavingCookieCallback<bool> callback; | |
190 cs->SetCanonicalCookieAsync( | |
191 cookie, secure_source, can_modify_httponly, | |
192 base::Bind(&ResultSavingCookieCallback<bool>::Run, | |
193 base::Unretained(&callback))); | |
194 callback.WaitUntilDone(); | |
195 return callback.result(); | |
196 } | |
197 | |
184 bool SetCookieWithServerTime(CookieStore* cs, | 198 bool SetCookieWithServerTime(CookieStore* cs, |
185 const GURL& url, | 199 const GURL& url, |
186 const std::string& cookie_line, | 200 const std::string& cookie_line, |
187 const base::Time& server_time) { | 201 const base::Time& server_time) { |
188 CookieOptions options; | 202 CookieOptions options; |
189 if (!CookieStoreTestTraits::supports_http_only) | 203 if (!CookieStoreTestTraits::supports_http_only) |
190 options.set_include_httponly(); | 204 options.set_include_httponly(); |
191 options.set_server_time(server_time); | 205 options.set_server_time(server_time); |
192 return SetCookieWithOptions(cs, url, cookie_line, options); | 206 return SetCookieWithOptions(cs, url, cookie_line, options); |
193 } | 207 } |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
458 EXPECT_FALSE(it->IsPersistent()); | 472 EXPECT_FALSE(it->IsPersistent()); |
459 // Some CookieStores don't store last access date. | 473 // Some CookieStores don't store last access date. |
460 if (!it->LastAccessDate().is_null()) | 474 if (!it->LastAccessDate().is_null()) |
461 EXPECT_EQ(it->CreationDate(), it->LastAccessDate()); | 475 EXPECT_EQ(it->CreationDate(), it->LastAccessDate()); |
462 EXPECT_TRUE(it->IsSecure()); | 476 EXPECT_TRUE(it->IsSecure()); |
463 EXPECT_FALSE(it->IsHttpOnly()); | 477 EXPECT_FALSE(it->IsHttpOnly()); |
464 | 478 |
465 EXPECT_TRUE(++it == cookies.end()); | 479 EXPECT_TRUE(++it == cookies.end()); |
466 } | 480 } |
467 | 481 |
482 TYPED_TEST_P(CookieStoreTest, SetCanonicalCookieTest) { | |
483 CookieStore* cs = this->GetCookieStore(); | |
484 | |
485 base::Time two_hours_ago = base::Time::Now() - base::TimeDelta::FromHours(2); | |
486 base::Time one_hour_ago = base::Time::Now() - base::TimeDelta::FromHours(1); | |
487 base::Time one_hour_from_now = | |
488 base::Time::Now() + base::TimeDelta::FromHours(1); | |
489 | |
490 std::string foo_foo_host(this->www_foo_foo_.url().host()); | |
491 std::string foo_bar_domain(this->www_foo_bar_.domain()); | |
492 std::string http_foo_host(this->http_www_foo_.url().host()); | |
493 std::string https_foo_host(this->https_www_foo_.url().host()); | |
494 | |
495 EXPECT_TRUE(this->SetCanonicalCookie( | |
496 cs, | |
497 CanonicalCookie("A", "B", foo_foo_host, "/foo", one_hour_ago, | |
498 one_hour_from_now, base::Time(), false, false, | |
499 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT), | |
500 false, true)); | |
501 // Note that for the creation time to be set exactly, without modification, | |
502 // it must be different from the one set by the line above. | |
503 EXPECT_TRUE(this->SetCanonicalCookie( | |
504 cs, | |
505 CanonicalCookie("C", "D", "." + foo_bar_domain, "/bar", two_hours_ago, | |
506 base::Time(), one_hour_ago, false, true, | |
507 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT), | |
508 false, true)); | |
509 | |
510 // A secure source is required for creating secure cookies. | |
511 EXPECT_FALSE(this->SetCanonicalCookie( | |
512 cs, | |
513 CanonicalCookie("E", "F", http_foo_host, "/", base::Time(), base::Time(), | |
514 base::Time(), true, false, CookieSameSite::DEFAULT_MODE, | |
515 COOKIE_PRIORITY_DEFAULT), | |
516 false, true)); | |
517 | |
518 // A secure source is also required for overwriting secure cookies. Writing | |
519 // a secure cookie then overwriting it from a non-secure source should fail. | |
520 EXPECT_TRUE(this->SetCanonicalCookie( | |
521 cs, | |
522 CanonicalCookie("E", "F", http_foo_host, "/", base::Time(), base::Time(), | |
523 base::Time(), true, false, CookieSameSite::DEFAULT_MODE, | |
524 COOKIE_PRIORITY_DEFAULT), | |
525 true, true)); | |
526 | |
527 EXPECT_FALSE(this->SetCanonicalCookie( | |
528 cs, | |
529 CanonicalCookie("E", "F", http_foo_host, "/", base::Time(), base::Time(), | |
530 base::Time(), true, false, CookieSameSite::DEFAULT_MODE, | |
531 COOKIE_PRIORITY_DEFAULT), | |
532 false, true)); | |
533 | |
534 // Get all the cookies for a given URL, regardless of properties. This 'get()' | |
535 // operation shouldn't update the access time, as the test checks that the | |
536 // access time is set properly upon creation. Updating the access time would | |
537 // make that difficult. | |
538 CookieOptions options; | |
539 options.set_include_httponly(); | |
540 options.set_same_site_cookie_mode( | |
541 CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); | |
542 options.set_do_not_update_access_time(); | |
543 | |
544 CookieList cookies = | |
545 this->GetCookieListWithOptions(cs, this->www_foo_foo_.url(), options); | |
546 CookieList::iterator it = cookies.begin(); | |
547 | |
548 ASSERT_TRUE(it != cookies.end()); | |
549 EXPECT_EQ("A", it->Name()); | |
550 EXPECT_EQ("B", it->Value()); | |
551 EXPECT_EQ(this->www_foo_foo_.host(), it->Domain()); | |
552 EXPECT_EQ("/foo", it->Path()); | |
553 EXPECT_EQ(one_hour_ago, it->CreationDate()); | |
554 EXPECT_TRUE(it->IsPersistent()); | |
555 // Expect expiration date is in the right range. Some cookie implementations | |
556 // may not record it with millisecond accuracy. | |
557 EXPECT_LE((one_hour_from_now - it->ExpiryDate()).magnitude().InSeconds(), 5); | |
558 // Some CookieStores don't store last access date. | |
559 if (!it->LastAccessDate().is_null()) | |
560 EXPECT_EQ(one_hour_ago, it->LastAccessDate()); | |
561 EXPECT_FALSE(it->IsSecure()); | |
562 EXPECT_FALSE(it->IsHttpOnly()); | |
563 | |
564 ASSERT_TRUE(++it == cookies.end()); | |
mmenke
2017/06/16 15:56:28
Maybe instead ASSERT on the size (x3)?
"ASSERT_EQ
Randy Smith (Not in Mondays)
2017/06/16 21:38:00
Done.
(For context, this test was copied&modified
| |
565 | |
566 // Verify that the cookie was set as 'httponly' by passing in a CookieOptions | |
567 // that excludes them and getting an empty result. | |
mmenke
2017/06/16 15:56:28
Does this get us anything? We check it->IsHttpOnl
Randy Smith (Not in Mondays)
2017/06/16 21:38:00
Done.
| |
568 if (TypeParam::supports_http_only) { | |
569 cookies = this->GetCookieListWithOptions(cs, this->www_foo_bar_.url(), | |
570 CookieOptions()); | |
571 it = cookies.begin(); | |
572 ASSERT_TRUE(it == cookies.end()); | |
573 } | |
574 | |
575 // Get the cookie using the wide open |options|: | |
576 cookies = | |
577 this->GetCookieListWithOptions(cs, this->www_foo_bar_.url(), options); | |
578 it = cookies.begin(); | |
579 | |
580 ASSERT_TRUE(it != cookies.end()); | |
581 EXPECT_EQ("C", it->Name()); | |
582 EXPECT_EQ("D", it->Value()); | |
583 EXPECT_EQ(this->www_foo_bar_.Format(".%D"), it->Domain()); | |
584 EXPECT_EQ("/bar", it->Path()); | |
585 EXPECT_EQ(two_hours_ago, it->CreationDate()); | |
586 EXPECT_FALSE(it->IsPersistent()); | |
587 // Some CookieStores don't store last access date. | |
588 if (!it->LastAccessDate().is_null()) | |
589 EXPECT_EQ(one_hour_ago, it->LastAccessDate()); | |
590 EXPECT_FALSE(it->IsSecure()); | |
591 EXPECT_TRUE(it->IsHttpOnly()); | |
592 | |
593 EXPECT_TRUE(++it == cookies.end()); | |
594 | |
595 cookies = | |
596 this->GetCookieListWithOptions(cs, this->https_www_foo_.url(), options); | |
597 it = cookies.begin(); | |
598 | |
599 ASSERT_TRUE(it != cookies.end()); | |
600 EXPECT_EQ("E", it->Name()); | |
601 EXPECT_EQ("F", it->Value()); | |
602 EXPECT_EQ("/", it->Path()); | |
603 EXPECT_EQ(this->https_www_foo_.host(), it->Domain()); | |
604 // Cookie should have its creation time set, and be in a reasonable range. | |
605 EXPECT_LE((base::Time::Now() - it->CreationDate()).magnitude().InMinutes(), | |
606 2); | |
607 EXPECT_FALSE(it->IsPersistent()); | |
608 // Some CookieStores don't store last access date. | |
609 if (!it->LastAccessDate().is_null()) | |
610 EXPECT_EQ(it->CreationDate(), it->LastAccessDate()); | |
611 EXPECT_TRUE(it->IsSecure()); | |
612 EXPECT_FALSE(it->IsHttpOnly()); | |
613 | |
614 EXPECT_TRUE(++it == cookies.end()); | |
615 } | |
616 | |
468 // Test enforcement around setting secure cookies. | 617 // Test enforcement around setting secure cookies. |
469 TYPED_TEST_P(CookieStoreTest, SetCookieWithDetailsSecureEnforcement) { | 618 TYPED_TEST_P(CookieStoreTest, SetCookieWithDetailsSecureEnforcement) { |
470 CookieStore* cs = this->GetCookieStore(); | 619 CookieStore* cs = this->GetCookieStore(); |
471 GURL http_url(this->http_www_foo_.url()); | 620 GURL http_url(this->http_www_foo_.url()); |
472 std::string http_domain(http_url.host()); | 621 std::string http_domain(http_url.host()); |
473 GURL https_url(this->https_www_foo_.url()); | 622 GURL https_url(this->https_www_foo_.url()); |
474 std::string https_domain(https_url.host()); | 623 std::string https_domain(https_url.host()); |
475 | 624 |
476 // Confirm that setting the secure attribute on an HTTP URL fails, but | 625 // Confirm that setting the secure attribute on an HTTP URL fails, but |
477 // the other combinations work. | 626 // the other combinations work. |
(...skipping 991 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1469 this->MatchCookieLines("A=B; C=D", | 1618 this->MatchCookieLines("A=B; C=D", |
1470 this->GetCookies(cs, this->http_www_foo_.url())); | 1619 this->GetCookies(cs, this->http_www_foo_.url())); |
1471 // Delete the session cookie. | 1620 // Delete the session cookie. |
1472 this->DeleteSessionCookies(cs); | 1621 this->DeleteSessionCookies(cs); |
1473 // Check that the session cookie has been deleted but not the persistent one. | 1622 // Check that the session cookie has been deleted but not the persistent one. |
1474 EXPECT_EQ("C=D", this->GetCookies(cs, this->http_www_foo_.url())); | 1623 EXPECT_EQ("C=D", this->GetCookies(cs, this->http_www_foo_.url())); |
1475 } | 1624 } |
1476 | 1625 |
1477 REGISTER_TYPED_TEST_CASE_P(CookieStoreTest, | 1626 REGISTER_TYPED_TEST_CASE_P(CookieStoreTest, |
1478 SetCookieWithDetailsAsync, | 1627 SetCookieWithDetailsAsync, |
1628 SetCanonicalCookieTest, | |
1479 SetCookieWithDetailsSecureEnforcement, | 1629 SetCookieWithDetailsSecureEnforcement, |
1480 EmptyKeyTest, | 1630 EmptyKeyTest, |
1481 DomainTest, | 1631 DomainTest, |
1482 DomainWithTrailingDotTest, | 1632 DomainWithTrailingDotTest, |
1483 ValidSubdomainTest, | 1633 ValidSubdomainTest, |
1484 InvalidDomainTest, | 1634 InvalidDomainTest, |
1485 InvalidDomainSameDomainAndRegistry, | 1635 InvalidDomainSameDomainAndRegistry, |
1486 DomainWithoutLeadingDotParentDomain, | 1636 DomainWithoutLeadingDotParentDomain, |
1487 DomainWithoutLeadingDotSameDomain, | 1637 DomainWithoutLeadingDotSameDomain, |
1488 CaseInsensitiveDomainTest, | 1638 CaseInsensitiveDomainTest, |
(...skipping 22 matching lines...) Expand all Loading... | |
1511 EmptyName, | 1661 EmptyName, |
1512 CookieOrdering, | 1662 CookieOrdering, |
1513 GetAllCookiesAsync, | 1663 GetAllCookiesAsync, |
1514 DeleteCookieAsync, | 1664 DeleteCookieAsync, |
1515 DeleteCanonicalCookieAsync, | 1665 DeleteCanonicalCookieAsync, |
1516 DeleteSessionCookie); | 1666 DeleteSessionCookie); |
1517 | 1667 |
1518 } // namespace net | 1668 } // namespace net |
1519 | 1669 |
1520 #endif // NET_COOKIES_COOKIE_STORE_UNITTEST_H_ | 1670 #endif // NET_COOKIES_COOKIE_STORE_UNITTEST_H_ |
OLD | NEW |