| 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/cookies/cookie_monster.h" | 5 #include "net/cookies/cookie_monster.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 1488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1499 now, CookieOptions()); | 1499 now, CookieOptions()); |
| 1500 EXPECT_THAT(test_cookie, CookieEquals(*expected_cookie)) | 1500 EXPECT_THAT(test_cookie, CookieEquals(*expected_cookie)) |
| 1501 << "Actual:\n" | 1501 << "Actual:\n" |
| 1502 << test_cookie.DebugString() << "\nExpected:\n" | 1502 << test_cookie.DebugString() << "\nExpected:\n" |
| 1503 << expected_cookie->DebugString(); | 1503 << expected_cookie->DebugString(); |
| 1504 | 1504 |
| 1505 // Really make sure everything is gone. | 1505 // Really make sure everything is gone. |
| 1506 EXPECT_EQ(0, DeleteAll(cm.get())); | 1506 EXPECT_EQ(0, DeleteAll(cm.get())); |
| 1507 } | 1507 } |
| 1508 | 1508 |
| 1509 static const int kAccessDelayMs = kLastAccessThresholdMilliseconds + 20; | 1509 static const base::TimeDelta kAccessDelay = |
| 1510 kLastAccessThreshold + base::TimeDelta::FromMilliseconds(20); |
| 1510 | 1511 |
| 1511 TEST_F(CookieMonsterTest, TestLastAccess) { | 1512 TEST_F(CookieMonsterTest, TestLastAccess) { |
| 1512 std::unique_ptr<CookieMonster> cm( | 1513 std::unique_ptr<CookieMonster> cm( |
| 1513 new CookieMonster(nullptr, nullptr, kLastAccessThresholdMilliseconds)); | 1514 new CookieMonster(nullptr, nullptr, kLastAccessThreshold)); |
| 1514 | 1515 |
| 1515 EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "A=B")); | 1516 EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "A=B")); |
| 1516 const Time last_access_date(GetFirstCookieAccessDate(cm.get())); | 1517 const Time last_access_date(GetFirstCookieAccessDate(cm.get())); |
| 1517 | 1518 |
| 1518 // Reading the cookie again immediately shouldn't update the access date, | 1519 // Reading the cookie again immediately shouldn't update the access date, |
| 1519 // since we're inside the threshold. | 1520 // since we're inside the threshold. |
| 1520 EXPECT_EQ("A=B", GetCookies(cm.get(), http_www_google_.url())); | 1521 EXPECT_EQ("A=B", GetCookies(cm.get(), http_www_google_.url())); |
| 1521 EXPECT_EQ(last_access_date, GetFirstCookieAccessDate(cm.get())); | 1522 EXPECT_EQ(last_access_date, GetFirstCookieAccessDate(cm.get())); |
| 1522 | 1523 |
| 1523 // Reading after a short wait will update the access date, if the cookie | 1524 // Reading after a short wait will update the access date, if the cookie |
| 1524 // is requested with options that would update the access date. First, test | 1525 // is requested with options that would update the access date. First, test |
| 1525 // that the flag's behavior is respected. | 1526 // that the flag's behavior is respected. |
| 1526 base::PlatformThread::Sleep( | 1527 base::PlatformThread::Sleep(kAccessDelay); |
| 1527 base::TimeDelta::FromMilliseconds(kAccessDelayMs)); | |
| 1528 CookieOptions options; | 1528 CookieOptions options; |
| 1529 options.set_do_not_update_access_time(); | 1529 options.set_do_not_update_access_time(); |
| 1530 EXPECT_EQ("A=B", | 1530 EXPECT_EQ("A=B", |
| 1531 GetCookiesWithOptions(cm.get(), http_www_google_.url(), options)); | 1531 GetCookiesWithOptions(cm.get(), http_www_google_.url(), options)); |
| 1532 EXPECT_EQ(last_access_date, GetFirstCookieAccessDate(cm.get())); | 1532 EXPECT_EQ(last_access_date, GetFirstCookieAccessDate(cm.get())); |
| 1533 | 1533 |
| 1534 // Getting all cookies for a URL doesn't update the accessed time either. | 1534 // Getting all cookies for a URL doesn't update the accessed time either. |
| 1535 CookieList cookies = GetAllCookiesForURL(cm.get(), http_www_google_.url()); | 1535 CookieList cookies = GetAllCookiesForURL(cm.get(), http_www_google_.url()); |
| 1536 CookieList::iterator it = cookies.begin(); | 1536 CookieList::iterator it = cookies.begin(); |
| 1537 ASSERT_TRUE(it != cookies.end()); | 1537 ASSERT_TRUE(it != cookies.end()); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1577 GURL http_url("http://host/path"); | 1577 GURL http_url("http://host/path"); |
| 1578 | 1578 |
| 1579 EXPECT_TRUE(SetCookie(cm.get(), http_url, "x=1")); | 1579 EXPECT_TRUE(SetCookie(cm.get(), http_url, "x=1")); |
| 1580 EXPECT_FALSE(SetCookie(cm.get(), foo_url, "x=1")); | 1580 EXPECT_FALSE(SetCookie(cm.get(), foo_url, "x=1")); |
| 1581 EXPECT_TRUE(SetCookie(cm_foo.get(), foo_url, "x=1")); | 1581 EXPECT_TRUE(SetCookie(cm_foo.get(), foo_url, "x=1")); |
| 1582 EXPECT_FALSE(SetCookie(cm_foo.get(), http_url, "x=1")); | 1582 EXPECT_FALSE(SetCookie(cm_foo.get(), http_url, "x=1")); |
| 1583 } | 1583 } |
| 1584 | 1584 |
| 1585 TEST_F(CookieMonsterTest, GetAllCookiesForURL) { | 1585 TEST_F(CookieMonsterTest, GetAllCookiesForURL) { |
| 1586 std::unique_ptr<CookieMonster> cm( | 1586 std::unique_ptr<CookieMonster> cm( |
| 1587 new CookieMonster(nullptr, nullptr, kLastAccessThresholdMilliseconds)); | 1587 new CookieMonster(nullptr, nullptr, kLastAccessThreshold)); |
| 1588 | 1588 |
| 1589 // Create an httponly cookie. | 1589 // Create an httponly cookie. |
| 1590 CookieOptions options; | 1590 CookieOptions options; |
| 1591 options.set_include_httponly(); | 1591 options.set_include_httponly(); |
| 1592 | 1592 |
| 1593 EXPECT_TRUE(SetCookieWithOptions(cm.get(), http_www_google_.url(), | 1593 EXPECT_TRUE(SetCookieWithOptions(cm.get(), http_www_google_.url(), |
| 1594 "A=B; httponly", options)); | 1594 "A=B; httponly", options)); |
| 1595 EXPECT_TRUE(SetCookieWithOptions(cm.get(), http_www_google_.url(), | 1595 EXPECT_TRUE(SetCookieWithOptions(cm.get(), http_www_google_.url(), |
| 1596 http_www_google_.Format("C=D; domain=.%D"), | 1596 http_www_google_.Format("C=D; domain=.%D"), |
| 1597 options)); | 1597 options)); |
| 1598 EXPECT_TRUE(SetCookieWithOptions( | 1598 EXPECT_TRUE(SetCookieWithOptions( |
| 1599 cm.get(), https_www_google_.url(), | 1599 cm.get(), https_www_google_.url(), |
| 1600 http_www_google_.Format("E=F; domain=.%D; secure"), options)); | 1600 http_www_google_.Format("E=F; domain=.%D; secure"), options)); |
| 1601 | 1601 |
| 1602 const Time last_access_date(GetFirstCookieAccessDate(cm.get())); | 1602 const Time last_access_date(GetFirstCookieAccessDate(cm.get())); |
| 1603 | 1603 |
| 1604 base::PlatformThread::Sleep( | 1604 base::PlatformThread::Sleep(kAccessDelay); |
| 1605 base::TimeDelta::FromMilliseconds(kAccessDelayMs)); | |
| 1606 | 1605 |
| 1607 // Check cookies for url. | 1606 // Check cookies for url. |
| 1608 CookieList cookies = GetAllCookiesForURL(cm.get(), http_www_google_.url()); | 1607 CookieList cookies = GetAllCookiesForURL(cm.get(), http_www_google_.url()); |
| 1609 CookieList::iterator it = cookies.begin(); | 1608 CookieList::iterator it = cookies.begin(); |
| 1610 | 1609 |
| 1611 ASSERT_TRUE(it != cookies.end()); | 1610 ASSERT_TRUE(it != cookies.end()); |
| 1612 EXPECT_EQ(http_www_google_.host(), it->Domain()); | 1611 EXPECT_EQ(http_www_google_.host(), it->Domain()); |
| 1613 EXPECT_EQ("A", it->Name()); | 1612 EXPECT_EQ("A", it->Name()); |
| 1614 | 1613 |
| 1615 ASSERT_TRUE(++it != cookies.end()); | 1614 ASSERT_TRUE(++it != cookies.end()); |
| (...skipping 1831 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3447 monster()->AddCallbackForCookie( | 3446 monster()->AddCallbackForCookie( |
| 3448 test_url_, "abc", | 3447 test_url_, "abc", |
| 3449 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); | 3448 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); |
| 3450 SetCookie(monster(), test_url_, "abc=def"); | 3449 SetCookie(monster(), test_url_, "abc=def"); |
| 3451 base::MessageLoop::current()->RunUntilIdle(); | 3450 base::MessageLoop::current()->RunUntilIdle(); |
| 3452 EXPECT_EQ(1U, cookies0.size()); | 3451 EXPECT_EQ(1U, cookies0.size()); |
| 3453 EXPECT_EQ(1U, cookies0.size()); | 3452 EXPECT_EQ(1U, cookies0.size()); |
| 3454 } | 3453 } |
| 3455 | 3454 |
| 3456 } // namespace net | 3455 } // namespace net |
| OLD | NEW |