Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Unified Diff: net/cookies/cookie_monster_unittest.cc

Issue 1844243002: [CookieStore] Upgrading host-based deleting to predicate-based deleting. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added android changes Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/cookies/cookie_monster_unittest.cc
diff --git a/net/cookies/cookie_monster_unittest.cc b/net/cookies/cookie_monster_unittest.cc
index 741807c1d6f2a47c36a4fcd53701fa99d4c3f1f8..45a9f70982d32b7631ae533d0c97f7e1ab895ec1 100644
--- a/net/cookies/cookie_monster_unittest.cc
+++ b/net/cookies/cookie_monster_unittest.cc
@@ -64,12 +64,31 @@ class NewMockPersistentCookieStore
virtual ~NewMockPersistentCookieStore() {}
};
+// False means 'less than or equal', so we test both ways for full equal.
+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));
@@ -156,8 +175,24 @@ class CookieMonsterTestBase : public CookieStoreTest<T> {
const GURL& url) {
DCHECK(cm);
ResultSavingCookieCallback<int> callback;
- cm->DeleteAllCreatedBetweenForHostAsync(
- delete_begin, delete_end, url,
+ cm->DeleteAllCreatedBetweenWithPredicateAsync(
+ delete_begin, delete_end,
+ CookieStore::CreatePredicateForHostCookies(url),
+ base::Bind(&ResultSavingCookieCallback<int>::Run,
+ base::Unretained(&callback)));
+ callback.WaitUntilDone();
+ 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();
@@ -667,14 +702,14 @@ ACTION_P2(GetAllCookiesAction, cookie_monster, callback) {
cookie_monster->GetAllCookiesAsync(callback->AsCallback());
}
-ACTION_P5(DeleteAllCreatedBetweenForHostAction,
+ACTION_P5(DeleteAllCreatedBetweenWithPredicateAction,
cookie_monster,
delete_begin,
delete_end,
- url,
+ predicate,
callback) {
- cookie_monster->DeleteAllCreatedBetweenForHostAsync(
- delete_begin, delete_end, url, callback->AsCallback());
+ cookie_monster->DeleteAllCreatedBetweenWithPredicateAsync(
+ delete_begin, delete_end, predicate, callback->AsCallback());
}
ACTION_P3(DeleteCanonicalCookieAction, cookie_monster, cookie, callback) {
@@ -1050,20 +1085,23 @@ TEST_F(DeferredCookieTaskTest, DeferredDeleteAllCreatedBetweenCookies) {
loop.Run();
}
-TEST_F(DeferredCookieTaskTest, DeferredDeleteAllForHostCreatedBetweenCookies) {
+TEST_F(DeferredCookieTaskTest,
+ DeferredDeleteAllWithPredicateCreatedBetweenCookies) {
MockDeleteCallback delete_callback;
- BeginWithForDomainKey(http_www_google_.domain(),
- DeleteAllCreatedBetweenForHostAction(
- &cookie_monster(), base::Time(), base::Time::Now(),
- http_www_google_.url(), &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(DeleteAllCreatedBetweenForHostAction(
- &cookie_monster(), base::Time(), base::Time::Now(),
- http_www_google_.url(), &delete_callback));
+ .WillOnce(DeleteAllCreatedBetweenWithPredicateAction(
+ &cookie_monster(), base::Time(), base::Time::Now(), predicate,
+ &delete_callback));
base::RunLoop loop;
EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(QuitRunLoop(&loop));
@@ -1187,7 +1225,7 @@ TEST_F(CookieMonsterTest, TestCookieDeleteAllCreatedBetweenTimestamps) {
EXPECT_EQ(0, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(99),
Time()));
- // Create 3 cookies with creation date of today, yesterday and the day before.
+ // Create 5 cookies with different creation dates.
EXPECT_TRUE(
cm->SetCookieWithCreationTime(http_www_google_.url(), "T-0=Now", now));
EXPECT_TRUE(cm->SetCookieWithCreationTime(
@@ -1219,6 +1257,86 @@ 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 5 cookies with different creation dates.
+ 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());
+ 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());
+ EXPECT_THAT(test_cookie, CookieEquals(*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) {

Powered by Google App Engine
This is Rietveld 408576698