Index: net/cookies/cookie_monster_unittest.cc |
diff --git a/net/cookies/cookie_monster_unittest.cc b/net/cookies/cookie_monster_unittest.cc |
index 04d69fdcf7d77710abcd851f117f07154e534954..716290038a78f64e59a64e7bae40b9dac5613e85 100644 |
--- a/net/cookies/cookie_monster_unittest.cc |
+++ b/net/cookies/cookie_monster_unittest.cc |
@@ -145,6 +145,20 @@ class CookieMonsterTestBase : public CookieStoreTest<T> { |
return callback.result(); |
} |
+ bool SetCanonicalCookie(CookieMonster* cm, |
+ CanonicalCookie cookie, |
+ bool secure_source, |
+ bool modify_http_only) { |
+ DCHECK(cm); |
+ ResultSavingCookieCallback<bool> callback; |
+ cm->SetCanonicalCookieAsync( |
+ cookie, secure_source, modify_http_only, |
+ base::Bind(&ResultSavingCookieCallback<bool>::Run, |
+ base::Unretained(&callback))); |
+ callback.WaitUntilDone(); |
+ return callback.result(); |
+ } |
+ |
int DeleteAllCreatedBetween(CookieMonster* cm, |
const base::Time& delete_begin, |
const base::Time& delete_end) { |
@@ -2301,7 +2315,7 @@ TEST_F(CookieMonsterTest, WhileLoadingDeleteAllGetForURL) { |
// When passed to the CookieMonster, it takes ownership of the pointed to |
// cookies. |
cookies.push_back( |
- CanonicalCookie::Create(kUrl, "a=b", base::Time(), CookieOptions())); |
+ CanonicalCookie::Create(kUrl, "a=b", base::Time::Now(), CookieOptions())); |
ASSERT_TRUE(cookies[0]); |
store->commands()[0].loaded_callback.Run(std::move(cookies)); |
@@ -2856,7 +2870,7 @@ TEST_F(CookieMonsterTest, ControlCharacterPurge) { |
// Test that cookie source schemes are histogrammed correctly. |
TEST_F(CookieMonsterTest, CookieSourceHistogram) { |
base::HistogramTester histograms; |
- const std::string cookie_source_histogram = "Cookie.CookieSourceScheme"; |
+ const std::string cookie_source_histogram = "Cookie.CookieSourceScheme2"; |
scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore); |
std::unique_ptr<CookieMonster> cm(new CookieMonster(store.get(), nullptr)); |
@@ -2999,6 +3013,127 @@ TEST_F(CookieMonsterTest, CookieDeleteEquivalentHistogramTest) { |
7); |
} |
+TEST_F(CookieMonsterTest, SetCanonicalCookie) { |
+ std::unique_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); |
+ base::Time two_hours_ago = base::Time::Now() - base::TimeDelta::FromHours(2); |
+ base::Time one_hour_ago = base::Time::Now() - base::TimeDelta::FromHours(1); |
+ base::Time one_hour_from_now = |
+ base::Time::Now() + base::TimeDelta::FromHours(1); |
+ std::string google_foo_host(www_google_foo_.url().host()); |
+ std::string google_bar_domain(www_google_bar_.domain()); |
+ std::string http_google_host(http_www_google_.url().host()); |
+ std::string https_google_host(https_www_google_.url().host()); |
+ |
+ EXPECT_TRUE(SetCanonicalCookie( |
+ cm.get(), |
+ CanonicalCookie("A", "B", google_foo_host, "/foo", one_hour_ago, |
+ one_hour_from_now, base::Time(), false, false, |
+ CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT), |
+ false, true)); |
+ // Note that for the creation time to be set exactly, without modification, |
+ // it must be different from the one set by the line above. |
+ EXPECT_TRUE(SetCanonicalCookie( |
+ cm.get(), |
+ CanonicalCookie("C", "D", "." + google_bar_domain, "/bar", two_hours_ago, |
+ base::Time(), one_hour_ago, false, true, |
+ CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT), |
+ false, true)); |
+ // Because of strict secure cookies, a cookie made by an HTTP URL should fail |
+ // to create a cookie with a the secure attribute. |
+ EXPECT_FALSE(SetCanonicalCookie( |
+ cm.get(), |
+ CanonicalCookie("E", "F", http_google_host, "/", base::Time(), |
+ base::Time(), base::Time(), true, false, |
+ CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT), |
+ false, true)); |
+ EXPECT_TRUE(SetCanonicalCookie( |
+ cm.get(), |
+ CanonicalCookie("E", "F", https_google_host, "/", base::Time(), |
+ base::Time(), base::Time(), true, false, |
+ CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT), |
+ true, true)); |
+ |
+ // Get all the cookies for a given URL, regardless of properties. This 'get()' |
+ // operation shouldn't update the access time, as the test checks that the |
+ // access time is set properly upon creation. Updating the access time would |
+ // make that difficult. |
+ CookieOptions options; |
+ options.set_include_httponly(); |
+ options.set_same_site_cookie_mode( |
+ CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); |
+ options.set_do_not_update_access_time(); |
+ |
+ CookieList cookies = |
+ GetCookieListWithOptions(cm.get(), this->www_google_foo_.url(), options); |
+ CookieList::iterator it = cookies.begin(); |
+ |
+ ASSERT_TRUE(it != cookies.end()); |
+ EXPECT_EQ("A", it->Name()); |
+ EXPECT_EQ("B", it->Value()); |
+ EXPECT_EQ(this->www_google_foo_.host(), it->Domain()); |
+ EXPECT_EQ("/foo", it->Path()); |
+ EXPECT_EQ(one_hour_ago, it->CreationDate()); |
+ EXPECT_TRUE(it->IsPersistent()); |
+ // Expect expiration date is in the right range. Some cookie implementations |
+ // may not record it with millisecond accuracy. |
+ EXPECT_LE((one_hour_from_now - it->ExpiryDate()).magnitude().InSeconds(), 5); |
+ // Some CookieStores don't store last access date. |
+ if (!it->LastAccessDate().is_null()) |
+ EXPECT_EQ(one_hour_ago, it->LastAccessDate()); |
+ EXPECT_FALSE(it->IsSecure()); |
+ EXPECT_FALSE(it->IsHttpOnly()); |
+ |
+ ASSERT_TRUE(++it == cookies.end()); |
+ |
+ // Verify that the cookie was set as 'httponly' by passing in a CookieOptions |
+ // that excludes them and getting an empty result. |
+ cookies = GetCookieListWithOptions(cm.get(), this->www_google_bar_.url(), |
+ CookieOptions()); |
+ it = cookies.begin(); |
+ ASSERT_TRUE(it == cookies.end()); |
+ |
+ // Get the cookie using the wide open |options|: |
+ cookies = |
+ GetCookieListWithOptions(cm.get(), this->www_google_bar_.url(), options); |
+ it = cookies.begin(); |
+ |
+ ASSERT_TRUE(it != cookies.end()); |
+ EXPECT_EQ("C", it->Name()); |
+ EXPECT_EQ("D", it->Value()); |
+ EXPECT_EQ(this->www_google_bar_.Format(".%D"), it->Domain()); |
+ EXPECT_EQ("/bar", it->Path()); |
+ EXPECT_EQ(two_hours_ago, it->CreationDate()); |
+ EXPECT_FALSE(it->IsPersistent()); |
+ // Some CookieStores don't store last access date. |
+ if (!it->LastAccessDate().is_null()) |
+ EXPECT_EQ(one_hour_ago, it->LastAccessDate()); |
+ EXPECT_FALSE(it->IsSecure()); |
+ EXPECT_TRUE(it->IsHttpOnly()); |
+ |
+ EXPECT_TRUE(++it == cookies.end()); |
+ |
+ cookies = GetCookieListWithOptions(cm.get(), this->https_www_google_.url(), |
+ options); |
+ it = cookies.begin(); |
+ |
+ ASSERT_TRUE(it != cookies.end()); |
+ EXPECT_EQ("E", it->Name()); |
+ EXPECT_EQ("F", it->Value()); |
+ EXPECT_EQ("/", it->Path()); |
+ EXPECT_EQ(this->https_www_google_.host(), it->Domain()); |
+ // Cookie should have its creation time set, and be in a reasonable range. |
+ EXPECT_LE((base::Time::Now() - it->CreationDate()).magnitude().InMinutes(), |
+ 2); |
+ EXPECT_FALSE(it->IsPersistent()); |
+ // Some CookieStores don't store last access date. |
+ if (!it->LastAccessDate().is_null()) |
+ EXPECT_EQ(it->CreationDate(), it->LastAccessDate()); |
+ EXPECT_TRUE(it->IsSecure()); |
+ EXPECT_FALSE(it->IsHttpOnly()); |
+ |
+ EXPECT_TRUE(++it == cookies.end()); |
+} |
+ |
TEST_F(CookieMonsterTest, SetSecureCookies) { |
std::unique_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); |
GURL http_url("http://www.google.com"); |