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 std::unique_ptr<CanonicalCookie> cookie, |
| 186 bool secure_source, |
| 187 bool can_modify_httponly) { |
| 188 DCHECK(cs); |
| 189 ResultSavingCookieCallback<bool> callback; |
| 190 cs->SetCanonicalCookieAsync( |
| 191 std::move(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 base::MakeUnique<CanonicalCookie>( |
| 498 "A", "B", foo_foo_host, "/foo", one_hour_ago, one_hour_from_now, |
| 499 base::Time(), false, false, CookieSameSite::DEFAULT_MODE, |
| 500 COOKIE_PRIORITY_DEFAULT), |
| 501 false, true)); |
| 502 // Note that for the creation time to be set exactly, without modification, |
| 503 // it must be different from the one set by the line above. |
| 504 EXPECT_TRUE(this->SetCanonicalCookie( |
| 505 cs, |
| 506 base::MakeUnique<CanonicalCookie>( |
| 507 "C", "D", "." + foo_bar_domain, "/bar", two_hours_ago, base::Time(), |
| 508 one_hour_ago, false, true, CookieSameSite::DEFAULT_MODE, |
| 509 COOKIE_PRIORITY_DEFAULT), |
| 510 false, true)); |
| 511 |
| 512 // A secure source is required for creating secure cookies. |
| 513 EXPECT_FALSE(this->SetCanonicalCookie( |
| 514 cs, |
| 515 base::MakeUnique<CanonicalCookie>( |
| 516 "E", "F", http_foo_host, "/", base::Time(), base::Time(), |
| 517 base::Time(), true, false, CookieSameSite::DEFAULT_MODE, |
| 518 COOKIE_PRIORITY_DEFAULT), |
| 519 false, true)); |
| 520 |
| 521 // A secure source is also required for overwriting secure cookies. Writing |
| 522 // a secure cookie then overwriting it from a non-secure source should fail. |
| 523 EXPECT_TRUE(this->SetCanonicalCookie( |
| 524 cs, |
| 525 base::MakeUnique<CanonicalCookie>( |
| 526 "E", "F", http_foo_host, "/", base::Time(), base::Time(), |
| 527 base::Time(), true, false, CookieSameSite::DEFAULT_MODE, |
| 528 COOKIE_PRIORITY_DEFAULT), |
| 529 true, true)); |
| 530 |
| 531 EXPECT_FALSE(this->SetCanonicalCookie( |
| 532 cs, |
| 533 base::MakeUnique<CanonicalCookie>( |
| 534 "E", "F", http_foo_host, "/", base::Time(), base::Time(), |
| 535 base::Time(), true, false, CookieSameSite::DEFAULT_MODE, |
| 536 COOKIE_PRIORITY_DEFAULT), |
| 537 false, true)); |
| 538 |
| 539 // Get all the cookies for a given URL, regardless of properties. This 'get()' |
| 540 // operation shouldn't update the access time, as the test checks that the |
| 541 // access time is set properly upon creation. Updating the access time would |
| 542 // make that difficult. |
| 543 CookieOptions options; |
| 544 options.set_include_httponly(); |
| 545 options.set_same_site_cookie_mode( |
| 546 CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); |
| 547 options.set_do_not_update_access_time(); |
| 548 |
| 549 CookieList cookies = |
| 550 this->GetCookieListWithOptions(cs, this->www_foo_foo_.url(), options); |
| 551 CookieList::iterator it = cookies.begin(); |
| 552 |
| 553 ASSERT_EQ(1u, cookies.size()); |
| 554 EXPECT_EQ("A", it->Name()); |
| 555 EXPECT_EQ("B", it->Value()); |
| 556 EXPECT_EQ(this->www_foo_foo_.host(), it->Domain()); |
| 557 EXPECT_EQ("/foo", it->Path()); |
| 558 EXPECT_EQ(one_hour_ago, it->CreationDate()); |
| 559 EXPECT_TRUE(it->IsPersistent()); |
| 560 // Expect expiration date is in the right range. Some cookie implementations |
| 561 // may not record it with millisecond accuracy. |
| 562 EXPECT_LE((one_hour_from_now - it->ExpiryDate()).magnitude().InSeconds(), 5); |
| 563 // Some CookieStores don't store last access date. |
| 564 if (!it->LastAccessDate().is_null()) |
| 565 EXPECT_EQ(one_hour_ago, it->LastAccessDate()); |
| 566 EXPECT_FALSE(it->IsSecure()); |
| 567 EXPECT_FALSE(it->IsHttpOnly()); |
| 568 |
| 569 // Get the cookie using the wide open |options|: |
| 570 cookies = |
| 571 this->GetCookieListWithOptions(cs, this->www_foo_bar_.url(), options); |
| 572 ASSERT_EQ(1u, cookies.size()); |
| 573 it = cookies.begin(); |
| 574 |
| 575 EXPECT_EQ("C", it->Name()); |
| 576 EXPECT_EQ("D", it->Value()); |
| 577 EXPECT_EQ(this->www_foo_bar_.Format(".%D"), it->Domain()); |
| 578 EXPECT_EQ("/bar", it->Path()); |
| 579 EXPECT_EQ(two_hours_ago, it->CreationDate()); |
| 580 EXPECT_FALSE(it->IsPersistent()); |
| 581 // Some CookieStores don't store last access date. |
| 582 if (!it->LastAccessDate().is_null()) |
| 583 EXPECT_EQ(one_hour_ago, it->LastAccessDate()); |
| 584 EXPECT_FALSE(it->IsSecure()); |
| 585 EXPECT_TRUE(it->IsHttpOnly()); |
| 586 |
| 587 cookies = |
| 588 this->GetCookieListWithOptions(cs, this->https_www_foo_.url(), options); |
| 589 ASSERT_EQ(1u, cookies.size()); |
| 590 it = cookies.begin(); |
| 591 |
| 592 EXPECT_EQ("E", it->Name()); |
| 593 EXPECT_EQ("F", it->Value()); |
| 594 EXPECT_EQ("/", it->Path()); |
| 595 EXPECT_EQ(this->https_www_foo_.host(), it->Domain()); |
| 596 // Cookie should have its creation time set, and be in a reasonable range. |
| 597 EXPECT_LE((base::Time::Now() - it->CreationDate()).magnitude().InMinutes(), |
| 598 2); |
| 599 EXPECT_FALSE(it->IsPersistent()); |
| 600 // Some CookieStores don't store last access date. |
| 601 if (!it->LastAccessDate().is_null()) |
| 602 EXPECT_EQ(it->CreationDate(), it->LastAccessDate()); |
| 603 EXPECT_TRUE(it->IsSecure()); |
| 604 EXPECT_FALSE(it->IsHttpOnly()); |
| 605 } |
| 606 |
468 // Test enforcement around setting secure cookies. | 607 // Test enforcement around setting secure cookies. |
469 TYPED_TEST_P(CookieStoreTest, SetCookieWithDetailsSecureEnforcement) { | 608 TYPED_TEST_P(CookieStoreTest, SetCookieWithDetailsSecureEnforcement) { |
470 CookieStore* cs = this->GetCookieStore(); | 609 CookieStore* cs = this->GetCookieStore(); |
471 GURL http_url(this->http_www_foo_.url()); | 610 GURL http_url(this->http_www_foo_.url()); |
472 std::string http_domain(http_url.host()); | 611 std::string http_domain(http_url.host()); |
473 GURL https_url(this->https_www_foo_.url()); | 612 GURL https_url(this->https_www_foo_.url()); |
474 std::string https_domain(https_url.host()); | 613 std::string https_domain(https_url.host()); |
475 | 614 |
476 // Confirm that setting the secure attribute on an HTTP URL fails, but | 615 // Confirm that setting the secure attribute on an HTTP URL fails, but |
477 // the other combinations work. | 616 // the other combinations work. |
(...skipping 991 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1469 this->MatchCookieLines("A=B; C=D", | 1608 this->MatchCookieLines("A=B; C=D", |
1470 this->GetCookies(cs, this->http_www_foo_.url())); | 1609 this->GetCookies(cs, this->http_www_foo_.url())); |
1471 // Delete the session cookie. | 1610 // Delete the session cookie. |
1472 this->DeleteSessionCookies(cs); | 1611 this->DeleteSessionCookies(cs); |
1473 // Check that the session cookie has been deleted but not the persistent one. | 1612 // 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())); | 1613 EXPECT_EQ("C=D", this->GetCookies(cs, this->http_www_foo_.url())); |
1475 } | 1614 } |
1476 | 1615 |
1477 REGISTER_TYPED_TEST_CASE_P(CookieStoreTest, | 1616 REGISTER_TYPED_TEST_CASE_P(CookieStoreTest, |
1478 SetCookieWithDetailsAsync, | 1617 SetCookieWithDetailsAsync, |
| 1618 SetCanonicalCookieTest, |
1479 SetCookieWithDetailsSecureEnforcement, | 1619 SetCookieWithDetailsSecureEnforcement, |
1480 EmptyKeyTest, | 1620 EmptyKeyTest, |
1481 DomainTest, | 1621 DomainTest, |
1482 DomainWithTrailingDotTest, | 1622 DomainWithTrailingDotTest, |
1483 ValidSubdomainTest, | 1623 ValidSubdomainTest, |
1484 InvalidDomainTest, | 1624 InvalidDomainTest, |
1485 InvalidDomainSameDomainAndRegistry, | 1625 InvalidDomainSameDomainAndRegistry, |
1486 DomainWithoutLeadingDotParentDomain, | 1626 DomainWithoutLeadingDotParentDomain, |
1487 DomainWithoutLeadingDotSameDomain, | 1627 DomainWithoutLeadingDotSameDomain, |
1488 CaseInsensitiveDomainTest, | 1628 CaseInsensitiveDomainTest, |
(...skipping 22 matching lines...) Expand all Loading... |
1511 EmptyName, | 1651 EmptyName, |
1512 CookieOrdering, | 1652 CookieOrdering, |
1513 GetAllCookiesAsync, | 1653 GetAllCookiesAsync, |
1514 DeleteCookieAsync, | 1654 DeleteCookieAsync, |
1515 DeleteCanonicalCookieAsync, | 1655 DeleteCanonicalCookieAsync, |
1516 DeleteSessionCookie); | 1656 DeleteSessionCookie); |
1517 | 1657 |
1518 } // namespace net | 1658 } // namespace net |
1519 | 1659 |
1520 #endif // NET_COOKIES_COOKIE_STORE_UNITTEST_H_ | 1660 #endif // NET_COOKIES_COOKIE_STORE_UNITTEST_H_ |
OLD | NEW |