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