Chromium Code Reviews| Index: net/cookies/cookie_monster_unittest.cc |
| diff --git a/net/cookies/cookie_monster_unittest.cc b/net/cookies/cookie_monster_unittest.cc |
| index 08a57ff8d57190fdd39ba9c7a96e7a1131f9fd70..05b42751b4c0428c2adf1af9f8b164f6e1c0805b 100644 |
| --- a/net/cookies/cookie_monster_unittest.cc |
| +++ b/net/cookies/cookie_monster_unittest.cc |
| @@ -64,12 +64,30 @@ class NewMockPersistentCookieStore |
| virtual ~NewMockPersistentCookieStore() {} |
| }; |
| +MATCHER_P(CookieEquals, expected, "") { |
| + return !(arg.FullCompare(expected) || expected.FullCompare(arg)); |
| +} |
| + |
| const char kTopLevelDomainPlus1[] = "http://www.harvard.edu"; |
| const char kTopLevelDomainPlus2[] = "http://www.math.harvard.edu"; |
| const char kTopLevelDomainPlus2Secure[] = "https://www.math.harvard.edu"; |
| const char kTopLevelDomainPlus3[] = "http://www.bourbaki.math.harvard.edu"; |
| const char kOtherDomain[] = "http://www.mit.edu"; |
| +bool AlwaysTrueCookiePredicate(CanonicalCookie* to_save, |
| + const CanonicalCookie& cookie) { |
| + if (to_save) |
| + *to_save = cookie; |
| + return true; |
| +} |
| + |
| +bool AlwaysFalseCookiePredicate(CanonicalCookie* to_save, |
| + const CanonicalCookie& cookie) { |
| + if (to_save) |
| + *to_save = cookie; |
| + return false; |
| +} |
| + |
| struct CookieMonsterTestTraits { |
| static scoped_ptr<CookieStore> Create() { |
| return make_scoped_ptr(new CookieMonster(nullptr, nullptr)); |
| @@ -164,6 +182,21 @@ class CookieMonsterTestBase : public CookieStoreTest<T> { |
| return callback.result(); |
| } |
| + int DeleteAllCreatedBetweenWithPredicate( |
| + CookieMonster* cm, |
| + const base::Time delete_begin, |
| + const base::Time delete_end, |
| + const base::Callback<bool(const CanonicalCookie&)>& predicate) { |
| + DCHECK(cm); |
| + ResultSavingCookieCallback<int> callback; |
| + cm->DeleteAllCreatedBetweenWithPredicateAsync( |
| + delete_begin, delete_end, predicate, |
| + base::Bind(&ResultSavingCookieCallback<int>::Run, |
| + base::Unretained(&callback))); |
| + callback.WaitUntilDone(); |
| + return callback.result(); |
| + } |
| + |
| // Helper for DeleteAllForHost test; repopulates CM with same layout |
| // each time. |
| void PopulateCmForDeleteAllForHost(CookieMonster* cm) { |
| @@ -675,6 +708,16 @@ ACTION_P5(DeleteAllCreatedBetweenForHostAction, |
| delete_begin, delete_end, url, callback->AsCallback()); |
| } |
| +ACTION_P5(DeleteAllCreatedBetweenWithPredicateAction, |
| + cookie_monster, |
| + delete_begin, |
| + delete_end, |
| + predicate, |
| + callback) { |
| + cookie_monster->DeleteAllCreatedBetweenWithPredicateAsync( |
| + delete_begin, delete_end, predicate, callback->AsCallback()); |
| +} |
| + |
| ACTION_P3(DeleteCanonicalCookieAction, cookie_monster, cookie, callback) { |
| cookie_monster->DeleteCanonicalCookieAsync(cookie, callback->AsCallback()); |
| } |
| @@ -1069,6 +1112,30 @@ TEST_F(DeferredCookieTaskTest, DeferredDeleteAllForHostCreatedBetweenCookies) { |
| loop.Run(); |
| } |
| +TEST_F(DeferredCookieTaskTest, |
| + DeferredDeleteAllWithPredicateCreatedBetweenCookies) { |
| + MockDeleteCallback delete_callback; |
| + |
| + base::Callback<bool(const CanonicalCookie&)> predicate = |
| + base::Bind(&AlwaysTrueCookiePredicate, nullptr); |
| + |
| + BeginWith(DeleteAllCreatedBetweenWithPredicateAction( |
| + &cookie_monster(), base::Time(), base::Time::Now(), predicate, |
| + &delete_callback)); |
| + |
| + WaitForLoadCall(); |
| + |
| + EXPECT_CALL(delete_callback, Invoke(false)) |
| + .WillOnce(DeleteAllCreatedBetweenWithPredicateAction( |
| + &cookie_monster(), base::Time(), base::Time::Now(), predicate, |
| + &delete_callback)); |
| + base::RunLoop loop; |
| + EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(QuitRunLoop(&loop)); |
| + |
| + CompleteLoading(); |
| + loop.Run(); |
| +} |
| + |
| TEST_F(DeferredCookieTaskTest, DeferredDeleteCanonicalCookie) { |
| std::vector<CanonicalCookie*> cookies; |
| CanonicalCookie cookie = BuildCanonicalCookie( |
| @@ -1217,6 +1284,89 @@ TEST_F(CookieMonsterTest, TestCookieDeleteAllCreatedBetweenTimestamps) { |
| EXPECT_EQ(0, DeleteAll(cm.get())); |
| } |
| +TEST_F(CookieMonsterTest, |
| + TestCookieDeleteAllCreatedBetweenTimestampsWithPredicate) { |
| + scoped_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); |
| + Time now = Time::Now(); |
| + |
| + CanonicalCookie test_cookie; |
| + base::Callback<bool(const CanonicalCookie&)> true_predicate = |
| + base::Bind(&AlwaysTrueCookiePredicate, &test_cookie); |
| + |
| + base::Callback<bool(const CanonicalCookie&)> false_predicate = |
| + base::Bind(&AlwaysFalseCookiePredicate, &test_cookie); |
| + |
| + // Nothing has been added so nothing should be deleted. |
| + EXPECT_EQ( |
| + 0, DeleteAllCreatedBetweenWithPredicate( |
| + cm.get(), now - TimeDelta::FromDays(99), Time(), true_predicate)); |
| + |
| + // Create 3 cookies with creation date of today, yesterday and the day before. |
|
Mike West
2016/03/11 08:36:11
You're creating 5 cookies.
dmurph
2016/03/30 22:21:27
Done.
|
| + EXPECT_TRUE( |
| + cm->SetCookieWithCreationTime(http_www_google_.url(), "T-0=Now", now)); |
| + EXPECT_TRUE(cm->SetCookieWithCreationTime( |
| + http_www_google_.url(), "T-1=Yesterday", now - TimeDelta::FromDays(1))); |
| + EXPECT_TRUE(cm->SetCookieWithCreationTime( |
| + http_www_google_.url(), "T-2=DayBefore", now - TimeDelta::FromDays(2))); |
| + EXPECT_TRUE(cm->SetCookieWithCreationTime( |
| + http_www_google_.url(), "T-3=ThreeDays", now - TimeDelta::FromDays(3))); |
| + EXPECT_TRUE(cm->SetCookieWithCreationTime( |
| + http_www_google_.url(), "T-7=LastWeek", now - TimeDelta::FromDays(7))); |
| + |
| + // Try to delete threedays and the daybefore, but we should do nothing due |
| + // to the predicate. |
| + EXPECT_EQ(0, DeleteAllCreatedBetweenWithPredicate( |
| + cm.get(), now - TimeDelta::FromDays(3), |
| + now - TimeDelta::FromDays(1), false_predicate)); |
| + // Same as above, but we use the true_predicate, so it works. |
| + EXPECT_EQ(2, DeleteAllCreatedBetweenWithPredicate( |
| + cm.get(), now - TimeDelta::FromDays(3), |
| + now - TimeDelta::FromDays(1), true_predicate)); |
| + |
| + // Try to delete yesterday, also make sure that delete_end is not |
| + // inclusive. |
| + EXPECT_EQ(0, |
| + DeleteAllCreatedBetweenWithPredicate( |
| + cm.get(), now - TimeDelta::FromDays(2), now, false_predicate)); |
| + EXPECT_EQ(1, |
| + DeleteAllCreatedBetweenWithPredicate( |
| + cm.get(), now - TimeDelta::FromDays(2), now, true_predicate)); |
| + // Check our cookie values. |
| + scoped_ptr<CanonicalCookie> expected_cookie = |
| + CanonicalCookie::Create(http_www_google_.url(), "T-1=Yesterday", |
| + now - TimeDelta::FromDays(1), CookieOptions()); |
| + // False means 'less than or equal', so we test both ways for full equal. |
|
Mike West
2016/03/11 08:36:11
Nit: Please move this comment up with the matcher.
dmurph
2016/03/30 22:21:27
Done.
|
| + EXPECT_THAT(test_cookie, CookieEquals(*expected_cookie)) |
| + << "Actual:\n" |
| + << test_cookie.DebugString() << "\nExpected:\n" |
| + << expected_cookie->DebugString(); |
| + |
| + // Make sure the delete_begin is inclusive. |
| + EXPECT_EQ(0, |
| + DeleteAllCreatedBetweenWithPredicate( |
| + cm.get(), now - TimeDelta::FromDays(7), now, false_predicate)); |
| + EXPECT_EQ(1, |
| + DeleteAllCreatedBetweenWithPredicate( |
| + cm.get(), now - TimeDelta::FromDays(7), now, true_predicate)); |
| + |
| + // Delete the last (now) item. |
| + EXPECT_EQ(0, DeleteAllCreatedBetweenWithPredicate(cm.get(), Time(), Time(), |
| + false_predicate)); |
| + EXPECT_EQ(1, DeleteAllCreatedBetweenWithPredicate(cm.get(), Time(), Time(), |
| + true_predicate)); |
| + expected_cookie = CanonicalCookie::Create(http_www_google_.url(), "T-0=Now", |
| + now, CookieOptions()); |
| + // False means 'less than or equal', so we test both ways for full equal. |
| + EXPECT_FALSE(expected_cookie->FullCompare(test_cookie) || |
| + test_cookie.FullCompare(*expected_cookie)) |
| + << "Actual:\n" |
| + << test_cookie.DebugString() << "\nExpected:\n" |
| + << expected_cookie->DebugString(); |
| + |
| + // Really make sure everything is gone. |
| + EXPECT_EQ(0, DeleteAll(cm.get())); |
| +} |
| + |
| static const int kAccessDelayMs = kLastAccessThresholdMilliseconds + 20; |
| TEST_F(CookieMonsterTest, TestLastAccess) { |
| @@ -2210,7 +2360,6 @@ class FlushablePersistentStore : public CookieMonster::PersistentCookieStore { |
| base::Bind(&LoadedCallbackTask::Run, |
| new LoadedCallbackTask(loaded_callback, out_cookies))); |
| } |
| - |
|
Mike West
2016/03/11 08:36:11
Nit: Why remove this newline?
dmurph
2016/03/30 22:21:27
Fixed.
|
| void LoadCookiesForKey(const std::string& key, |
| const LoadedCallback& loaded_callback) override { |
| Load(loaded_callback); |