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 google_foo_host(this->www_google_foo_.url().host()); |
| 491 std::string google_bar_domain(this->www_google_bar_.domain()); |
| 492 std::string http_google_host(this->http_www_google_.url().host()); |
| 493 std::string https_google_host(this->https_www_google_.url().host()); |
| 494 |
| 495 EXPECT_TRUE(this->SetCanonicalCookie( |
| 496 cs, |
| 497 CanonicalCookie("A", "B", google_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", "." + google_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 // Because of strict secure cookies, a cookie made by an HTTP URL should fail |
| 510 // to create a cookie with a the secure attribute. |
| 511 EXPECT_FALSE(this->SetCanonicalCookie( |
| 512 cs, |
| 513 CanonicalCookie("E", "F", http_google_host, "/", base::Time(), |
| 514 base::Time(), base::Time(), true, false, |
| 515 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT), |
| 516 false, true)); |
| 517 EXPECT_TRUE(this->SetCanonicalCookie( |
| 518 cs, |
| 519 CanonicalCookie("E", "F", https_google_host, "/", base::Time(), |
| 520 base::Time(), base::Time(), true, false, |
| 521 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT), |
| 522 true, true)); |
| 523 |
| 524 // Get all the cookies for a given URL, regardless of properties. This 'get()' |
| 525 // operation shouldn't update the access time, as the test checks that the |
| 526 // access time is set properly upon creation. Updating the access time would |
| 527 // make that difficult. |
| 528 CookieOptions options; |
| 529 options.set_include_httponly(); |
| 530 options.set_same_site_cookie_mode( |
| 531 CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); |
| 532 options.set_do_not_update_access_time(); |
| 533 |
| 534 CookieList cookies = |
| 535 this->GetCookieListWithOptions(cs, this->www_google_foo_.url(), options); |
| 536 CookieList::iterator it = cookies.begin(); |
| 537 |
| 538 ASSERT_TRUE(it != cookies.end()); |
| 539 EXPECT_EQ("A", it->Name()); |
| 540 EXPECT_EQ("B", it->Value()); |
| 541 EXPECT_EQ(this->www_google_foo_.host(), it->Domain()); |
| 542 EXPECT_EQ("/foo", it->Path()); |
| 543 EXPECT_EQ(one_hour_ago, it->CreationDate()); |
| 544 EXPECT_TRUE(it->IsPersistent()); |
| 545 // Expect expiration date is in the right range. Some cookie implementations |
| 546 // may not record it with millisecond accuracy. |
| 547 EXPECT_LE((one_hour_from_now - it->ExpiryDate()).magnitude().InSeconds(), 5); |
| 548 // Some CookieStores don't store last access date. |
| 549 if (!it->LastAccessDate().is_null()) |
| 550 EXPECT_EQ(one_hour_ago, it->LastAccessDate()); |
| 551 EXPECT_FALSE(it->IsSecure()); |
| 552 EXPECT_FALSE(it->IsHttpOnly()); |
| 553 |
| 554 ASSERT_TRUE(++it == cookies.end()); |
| 555 |
| 556 // Verify that the cookie was set as 'httponly' by passing in a CookieOptions |
| 557 // that excludes them and getting an empty result. |
| 558 if (TypeParam::supports_http_only) { |
| 559 cookies = this->GetCookieListWithOptions(cs, this->www_google_bar_.url(), |
| 560 CookieOptions()); |
| 561 it = cookies.begin(); |
| 562 ASSERT_TRUE(it == cookies.end()); |
| 563 } |
| 564 |
| 565 // Get the cookie using the wide open |options|: |
| 566 cookies = |
| 567 this->GetCookieListWithOptions(cs, this->www_google_bar_.url(), options); |
| 568 it = cookies.begin(); |
| 569 |
| 570 ASSERT_TRUE(it != cookies.end()); |
| 571 EXPECT_EQ("C", it->Name()); |
| 572 EXPECT_EQ("D", it->Value()); |
| 573 EXPECT_EQ(this->www_google_bar_.Format(".%D"), it->Domain()); |
| 574 EXPECT_EQ("/bar", it->Path()); |
| 575 EXPECT_EQ(two_hours_ago, it->CreationDate()); |
| 576 EXPECT_FALSE(it->IsPersistent()); |
| 577 // Some CookieStores don't store last access date. |
| 578 if (!it->LastAccessDate().is_null()) |
| 579 EXPECT_EQ(one_hour_ago, it->LastAccessDate()); |
| 580 EXPECT_FALSE(it->IsSecure()); |
| 581 EXPECT_TRUE(it->IsHttpOnly()); |
| 582 |
| 583 EXPECT_TRUE(++it == cookies.end()); |
| 584 |
| 585 cookies = this->GetCookieListWithOptions(cs, this->https_www_google_.url(), |
| 586 options); |
| 587 it = cookies.begin(); |
| 588 |
| 589 ASSERT_TRUE(it != cookies.end()); |
| 590 EXPECT_EQ("E", it->Name()); |
| 591 EXPECT_EQ("F", it->Value()); |
| 592 EXPECT_EQ("/", it->Path()); |
| 593 EXPECT_EQ(this->https_www_google_.host(), it->Domain()); |
| 594 // Cookie should have its creation time set, and be in a reasonable range. |
| 595 EXPECT_LE((base::Time::Now() - it->CreationDate()).magnitude().InMinutes(), |
| 596 2); |
| 597 EXPECT_FALSE(it->IsPersistent()); |
| 598 // Some CookieStores don't store last access date. |
| 599 if (!it->LastAccessDate().is_null()) |
| 600 EXPECT_EQ(it->CreationDate(), it->LastAccessDate()); |
| 601 EXPECT_TRUE(it->IsSecure()); |
| 602 EXPECT_FALSE(it->IsHttpOnly()); |
| 603 |
| 604 EXPECT_TRUE(++it == cookies.end()); |
| 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_google_.url()); | 610 GURL http_url(this->http_www_google_.url()); |
472 std::string http_domain(http_url.host()); | 611 std::string http_domain(http_url.host()); |
473 GURL https_url(this->https_www_google_.url()); | 612 GURL https_url(this->https_www_google_.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 966 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1444 this->MatchCookieLines("A=B; C=D", | 1583 this->MatchCookieLines("A=B; C=D", |
1445 this->GetCookies(cs, this->http_www_google_.url())); | 1584 this->GetCookies(cs, this->http_www_google_.url())); |
1446 // Delete the session cookie. | 1585 // Delete the session cookie. |
1447 this->DeleteSessionCookies(cs); | 1586 this->DeleteSessionCookies(cs); |
1448 // Check that the session cookie has been deleted but not the persistent one. | 1587 // Check that the session cookie has been deleted but not the persistent one. |
1449 EXPECT_EQ("C=D", this->GetCookies(cs, this->http_www_google_.url())); | 1588 EXPECT_EQ("C=D", this->GetCookies(cs, this->http_www_google_.url())); |
1450 } | 1589 } |
1451 | 1590 |
1452 REGISTER_TYPED_TEST_CASE_P(CookieStoreTest, | 1591 REGISTER_TYPED_TEST_CASE_P(CookieStoreTest, |
1453 SetCookieWithDetailsAsync, | 1592 SetCookieWithDetailsAsync, |
| 1593 SetCanonicalCookieTest, |
1454 SetCookieWithDetailsSecureEnforcement, | 1594 SetCookieWithDetailsSecureEnforcement, |
1455 EmptyKeyTest, | 1595 EmptyKeyTest, |
1456 DomainTest, | 1596 DomainTest, |
1457 DomainWithTrailingDotTest, | 1597 DomainWithTrailingDotTest, |
1458 ValidSubdomainTest, | 1598 ValidSubdomainTest, |
1459 InvalidDomainTest, | 1599 InvalidDomainTest, |
1460 InvalidDomainSameDomainAndRegistry, | 1600 InvalidDomainSameDomainAndRegistry, |
1461 DomainWithoutLeadingDotParentDomain, | 1601 DomainWithoutLeadingDotParentDomain, |
1462 DomainWithoutLeadingDotSameDomain, | 1602 DomainWithoutLeadingDotSameDomain, |
1463 CaseInsensitiveDomainTest, | 1603 CaseInsensitiveDomainTest, |
(...skipping 21 matching lines...) Expand all Loading... |
1485 OverwritePersistentCookie, | 1625 OverwritePersistentCookie, |
1486 CookieOrdering, | 1626 CookieOrdering, |
1487 GetAllCookiesAsync, | 1627 GetAllCookiesAsync, |
1488 DeleteCookieAsync, | 1628 DeleteCookieAsync, |
1489 DeleteCanonicalCookieAsync, | 1629 DeleteCanonicalCookieAsync, |
1490 DeleteSessionCookie); | 1630 DeleteSessionCookie); |
1491 | 1631 |
1492 } // namespace net | 1632 } // namespace net |
1493 | 1633 |
1494 #endif // NET_COOKIES_COOKIE_STORE_UNITTEST_H_ | 1634 #endif // NET_COOKIES_COOKIE_STORE_UNITTEST_H_ |
OLD | NEW |