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

Side by Side 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, 8 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 unified diff | Download patch
OLDNEW
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_store_unittest.h" 5 #include "net/cookies/cookie_store_unittest.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 virtual void Flush(const base::Closure& callback) { 57 virtual void Flush(const base::Closure& callback) {
58 if (!callback.is_null()) 58 if (!callback.is_null())
59 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); 59 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
60 } 60 }
61 MOCK_METHOD0(SetForceKeepSessionState, void()); 61 MOCK_METHOD0(SetForceKeepSessionState, void());
62 62
63 private: 63 private:
64 virtual ~NewMockPersistentCookieStore() {} 64 virtual ~NewMockPersistentCookieStore() {}
65 }; 65 };
66 66
67 // False means 'less than or equal', so we test both ways for full equal.
68 MATCHER_P(CookieEquals, expected, "") {
69 return !(arg.FullCompare(expected) || expected.FullCompare(arg));
70 }
71
67 const char kTopLevelDomainPlus1[] = "http://www.harvard.edu"; 72 const char kTopLevelDomainPlus1[] = "http://www.harvard.edu";
68 const char kTopLevelDomainPlus2[] = "http://www.math.harvard.edu"; 73 const char kTopLevelDomainPlus2[] = "http://www.math.harvard.edu";
69 const char kTopLevelDomainPlus2Secure[] = "https://www.math.harvard.edu"; 74 const char kTopLevelDomainPlus2Secure[] = "https://www.math.harvard.edu";
70 const char kTopLevelDomainPlus3[] = "http://www.bourbaki.math.harvard.edu"; 75 const char kTopLevelDomainPlus3[] = "http://www.bourbaki.math.harvard.edu";
71 const char kOtherDomain[] = "http://www.mit.edu"; 76 const char kOtherDomain[] = "http://www.mit.edu";
72 77
78 bool AlwaysTrueCookiePredicate(CanonicalCookie* to_save,
79 const CanonicalCookie& cookie) {
80 if (to_save)
81 *to_save = cookie;
82 return true;
83 }
84
85 bool AlwaysFalseCookiePredicate(CanonicalCookie* to_save,
86 const CanonicalCookie& cookie) {
87 if (to_save)
88 *to_save = cookie;
89 return false;
90 }
91
73 struct CookieMonsterTestTraits { 92 struct CookieMonsterTestTraits {
74 static scoped_ptr<CookieStore> Create() { 93 static scoped_ptr<CookieStore> Create() {
75 return make_scoped_ptr(new CookieMonster(nullptr, nullptr)); 94 return make_scoped_ptr(new CookieMonster(nullptr, nullptr));
76 } 95 }
77 96
78 static const bool supports_http_only = true; 97 static const bool supports_http_only = true;
79 static const bool supports_non_dotted_domains = true; 98 static const bool supports_non_dotted_domains = true;
80 static const bool preserves_trailing_dots = true; 99 static const bool preserves_trailing_dots = true;
81 static const bool filters_schemes = true; 100 static const bool filters_schemes = true;
82 static const bool has_path_prefix_bug = false; 101 static const bool has_path_prefix_bug = false;
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 callback.WaitUntilDone(); 168 callback.WaitUntilDone();
150 return callback.result(); 169 return callback.result();
151 } 170 }
152 171
153 int DeleteAllCreatedBetweenForHost(CookieMonster* cm, 172 int DeleteAllCreatedBetweenForHost(CookieMonster* cm,
154 const base::Time delete_begin, 173 const base::Time delete_begin,
155 const base::Time delete_end, 174 const base::Time delete_end,
156 const GURL& url) { 175 const GURL& url) {
157 DCHECK(cm); 176 DCHECK(cm);
158 ResultSavingCookieCallback<int> callback; 177 ResultSavingCookieCallback<int> callback;
159 cm->DeleteAllCreatedBetweenForHostAsync( 178 cm->DeleteAllCreatedBetweenWithPredicateAsync(
160 delete_begin, delete_end, url, 179 delete_begin, delete_end,
180 CookieStore::CreatePredicateForHostCookies(url),
161 base::Bind(&ResultSavingCookieCallback<int>::Run, 181 base::Bind(&ResultSavingCookieCallback<int>::Run,
162 base::Unretained(&callback))); 182 base::Unretained(&callback)));
163 callback.WaitUntilDone(); 183 callback.WaitUntilDone();
184 return callback.result();
185 }
186
187 int DeleteAllCreatedBetweenWithPredicate(
188 CookieMonster* cm,
189 const base::Time delete_begin,
190 const base::Time delete_end,
191 const base::Callback<bool(const CanonicalCookie&)>& predicate) {
192 DCHECK(cm);
193 ResultSavingCookieCallback<int> callback;
194 cm->DeleteAllCreatedBetweenWithPredicateAsync(
195 delete_begin, delete_end, predicate,
196 base::Bind(&ResultSavingCookieCallback<int>::Run,
197 base::Unretained(&callback)));
198 callback.WaitUntilDone();
164 return callback.result(); 199 return callback.result();
165 } 200 }
166 201
167 // Helper for DeleteAllForHost test; repopulates CM with same layout 202 // Helper for DeleteAllForHost test; repopulates CM with same layout
168 // each time. 203 // each time.
169 void PopulateCmForDeleteAllForHost(CookieMonster* cm) { 204 void PopulateCmForDeleteAllForHost(CookieMonster* cm) {
170 GURL url_top_level_domain_plus_1(kTopLevelDomainPlus1); 205 GURL url_top_level_domain_plus_1(kTopLevelDomainPlus1);
171 GURL url_top_level_domain_plus_2(kTopLevelDomainPlus2); 206 GURL url_top_level_domain_plus_2(kTopLevelDomainPlus2);
172 GURL url_top_level_domain_plus_2_secure(kTopLevelDomainPlus2Secure); 207 GURL url_top_level_domain_plus_2_secure(kTopLevelDomainPlus2Secure);
173 GURL url_top_level_domain_plus_3(kTopLevelDomainPlus3); 208 GURL url_top_level_domain_plus_3(kTopLevelDomainPlus3);
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after
660 cc.url, cc.name, cc.value, cc.domain, cc.path, base::Time(), 695 cc.url, cc.name, cc.value, cc.domain, cc.path, base::Time(),
661 cc.expiration_time, base::Time(), cc.secure, cc.http_only, cc.same_site, 696 cc.expiration_time, base::Time(), cc.secure, cc.http_only, cc.same_site,
662 false /* enforces strict secure cookies */, cc.priority, 697 false /* enforces strict secure cookies */, cc.priority,
663 callback->AsCallback()); 698 callback->AsCallback());
664 } 699 }
665 700
666 ACTION_P2(GetAllCookiesAction, cookie_monster, callback) { 701 ACTION_P2(GetAllCookiesAction, cookie_monster, callback) {
667 cookie_monster->GetAllCookiesAsync(callback->AsCallback()); 702 cookie_monster->GetAllCookiesAsync(callback->AsCallback());
668 } 703 }
669 704
670 ACTION_P5(DeleteAllCreatedBetweenForHostAction, 705 ACTION_P5(DeleteAllCreatedBetweenWithPredicateAction,
671 cookie_monster, 706 cookie_monster,
672 delete_begin, 707 delete_begin,
673 delete_end, 708 delete_end,
674 url, 709 predicate,
675 callback) { 710 callback) {
676 cookie_monster->DeleteAllCreatedBetweenForHostAsync( 711 cookie_monster->DeleteAllCreatedBetweenWithPredicateAsync(
677 delete_begin, delete_end, url, callback->AsCallback()); 712 delete_begin, delete_end, predicate, callback->AsCallback());
678 } 713 }
679 714
680 ACTION_P3(DeleteCanonicalCookieAction, cookie_monster, cookie, callback) { 715 ACTION_P3(DeleteCanonicalCookieAction, cookie_monster, cookie, callback) {
681 cookie_monster->DeleteCanonicalCookieAsync(cookie, callback->AsCallback()); 716 cookie_monster->DeleteCanonicalCookieAsync(cookie, callback->AsCallback());
682 } 717 }
683 718
684 ACTION_P2(DeleteAllAction, cookie_monster, callback) { 719 ACTION_P2(DeleteAllAction, cookie_monster, callback) {
685 cookie_monster->DeleteAllAsync(callback->AsCallback()); 720 cookie_monster->DeleteAllAsync(callback->AsCallback());
686 } 721 }
687 722
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
1043 .WillOnce(DeleteAllCreatedBetweenAction(&cookie_monster(), base::Time(), 1078 .WillOnce(DeleteAllCreatedBetweenAction(&cookie_monster(), base::Time(),
1044 base::Time::Now(), 1079 base::Time::Now(),
1045 &delete_callback)); 1080 &delete_callback));
1046 base::RunLoop loop; 1081 base::RunLoop loop;
1047 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(QuitRunLoop(&loop)); 1082 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(QuitRunLoop(&loop));
1048 1083
1049 CompleteLoading(); 1084 CompleteLoading();
1050 loop.Run(); 1085 loop.Run();
1051 } 1086 }
1052 1087
1053 TEST_F(DeferredCookieTaskTest, DeferredDeleteAllForHostCreatedBetweenCookies) { 1088 TEST_F(DeferredCookieTaskTest,
1089 DeferredDeleteAllWithPredicateCreatedBetweenCookies) {
1054 MockDeleteCallback delete_callback; 1090 MockDeleteCallback delete_callback;
1055 1091
1056 BeginWithForDomainKey(http_www_google_.domain(), 1092 base::Callback<bool(const CanonicalCookie&)> predicate =
1057 DeleteAllCreatedBetweenForHostAction( 1093 base::Bind(&AlwaysTrueCookiePredicate, nullptr);
1058 &cookie_monster(), base::Time(), base::Time::Now(), 1094
1059 http_www_google_.url(), &delete_callback)); 1095 BeginWith(DeleteAllCreatedBetweenWithPredicateAction(
1096 &cookie_monster(), base::Time(), base::Time::Now(), predicate,
1097 &delete_callback));
1060 1098
1061 WaitForLoadCall(); 1099 WaitForLoadCall();
1062 1100
1063 EXPECT_CALL(delete_callback, Invoke(false)) 1101 EXPECT_CALL(delete_callback, Invoke(false))
1064 .WillOnce(DeleteAllCreatedBetweenForHostAction( 1102 .WillOnce(DeleteAllCreatedBetweenWithPredicateAction(
1065 &cookie_monster(), base::Time(), base::Time::Now(), 1103 &cookie_monster(), base::Time(), base::Time::Now(), predicate,
1066 http_www_google_.url(), &delete_callback)); 1104 &delete_callback));
1067 base::RunLoop loop; 1105 base::RunLoop loop;
1068 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(QuitRunLoop(&loop)); 1106 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(QuitRunLoop(&loop));
1069 1107
1070 CompleteLoading(); 1108 CompleteLoading();
1071 loop.Run(); 1109 loop.Run();
1072 } 1110 }
1073 1111
1074 TEST_F(DeferredCookieTaskTest, DeferredDeleteCanonicalCookie) { 1112 TEST_F(DeferredCookieTaskTest, DeferredDeleteCanonicalCookie) {
1075 std::vector<CanonicalCookie*> cookies; 1113 std::vector<CanonicalCookie*> cookies;
1076 CanonicalCookie cookie = BuildCanonicalCookie( 1114 CanonicalCookie cookie = BuildCanonicalCookie(
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
1180 } 1218 }
1181 1219
1182 TEST_F(CookieMonsterTest, TestCookieDeleteAllCreatedBetweenTimestamps) { 1220 TEST_F(CookieMonsterTest, TestCookieDeleteAllCreatedBetweenTimestamps) {
1183 scoped_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); 1221 scoped_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr));
1184 Time now = Time::Now(); 1222 Time now = Time::Now();
1185 1223
1186 // Nothing has been added so nothing should be deleted. 1224 // Nothing has been added so nothing should be deleted.
1187 EXPECT_EQ(0, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(99), 1225 EXPECT_EQ(0, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(99),
1188 Time())); 1226 Time()));
1189 1227
1190 // Create 3 cookies with creation date of today, yesterday and the day before. 1228 // Create 5 cookies with different creation dates.
1191 EXPECT_TRUE( 1229 EXPECT_TRUE(
1192 cm->SetCookieWithCreationTime(http_www_google_.url(), "T-0=Now", now)); 1230 cm->SetCookieWithCreationTime(http_www_google_.url(), "T-0=Now", now));
1193 EXPECT_TRUE(cm->SetCookieWithCreationTime( 1231 EXPECT_TRUE(cm->SetCookieWithCreationTime(
1194 http_www_google_.url(), "T-1=Yesterday", now - TimeDelta::FromDays(1))); 1232 http_www_google_.url(), "T-1=Yesterday", now - TimeDelta::FromDays(1)));
1195 EXPECT_TRUE(cm->SetCookieWithCreationTime( 1233 EXPECT_TRUE(cm->SetCookieWithCreationTime(
1196 http_www_google_.url(), "T-2=DayBefore", now - TimeDelta::FromDays(2))); 1234 http_www_google_.url(), "T-2=DayBefore", now - TimeDelta::FromDays(2)));
1197 EXPECT_TRUE(cm->SetCookieWithCreationTime( 1235 EXPECT_TRUE(cm->SetCookieWithCreationTime(
1198 http_www_google_.url(), "T-3=ThreeDays", now - TimeDelta::FromDays(3))); 1236 http_www_google_.url(), "T-3=ThreeDays", now - TimeDelta::FromDays(3)));
1199 EXPECT_TRUE(cm->SetCookieWithCreationTime( 1237 EXPECT_TRUE(cm->SetCookieWithCreationTime(
1200 http_www_google_.url(), "T-7=LastWeek", now - TimeDelta::FromDays(7))); 1238 http_www_google_.url(), "T-7=LastWeek", now - TimeDelta::FromDays(7)));
(...skipping 11 matching lines...) Expand all
1212 EXPECT_EQ( 1250 EXPECT_EQ(
1213 1, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(7), now)); 1251 1, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(7), now));
1214 1252
1215 // Delete the last (now) item. 1253 // Delete the last (now) item.
1216 EXPECT_EQ(1, DeleteAllCreatedBetween(cm.get(), Time(), Time())); 1254 EXPECT_EQ(1, DeleteAllCreatedBetween(cm.get(), Time(), Time()));
1217 1255
1218 // Really make sure everything is gone. 1256 // Really make sure everything is gone.
1219 EXPECT_EQ(0, DeleteAll(cm.get())); 1257 EXPECT_EQ(0, DeleteAll(cm.get()));
1220 } 1258 }
1221 1259
1260 TEST_F(CookieMonsterTest,
1261 TestCookieDeleteAllCreatedBetweenTimestampsWithPredicate) {
1262 scoped_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr));
1263 Time now = Time::Now();
1264
1265 CanonicalCookie test_cookie;
1266 base::Callback<bool(const CanonicalCookie&)> true_predicate =
1267 base::Bind(&AlwaysTrueCookiePredicate, &test_cookie);
1268
1269 base::Callback<bool(const CanonicalCookie&)> false_predicate =
1270 base::Bind(&AlwaysFalseCookiePredicate, &test_cookie);
1271
1272 // Nothing has been added so nothing should be deleted.
1273 EXPECT_EQ(
1274 0, DeleteAllCreatedBetweenWithPredicate(
1275 cm.get(), now - TimeDelta::FromDays(99), Time(), true_predicate));
1276
1277 // Create 5 cookies with different creation dates.
1278 EXPECT_TRUE(
1279 cm->SetCookieWithCreationTime(http_www_google_.url(), "T-0=Now", now));
1280 EXPECT_TRUE(cm->SetCookieWithCreationTime(
1281 http_www_google_.url(), "T-1=Yesterday", now - TimeDelta::FromDays(1)));
1282 EXPECT_TRUE(cm->SetCookieWithCreationTime(
1283 http_www_google_.url(), "T-2=DayBefore", now - TimeDelta::FromDays(2)));
1284 EXPECT_TRUE(cm->SetCookieWithCreationTime(
1285 http_www_google_.url(), "T-3=ThreeDays", now - TimeDelta::FromDays(3)));
1286 EXPECT_TRUE(cm->SetCookieWithCreationTime(
1287 http_www_google_.url(), "T-7=LastWeek", now - TimeDelta::FromDays(7)));
1288
1289 // Try to delete threedays and the daybefore, but we should do nothing due
1290 // to the predicate.
1291 EXPECT_EQ(0, DeleteAllCreatedBetweenWithPredicate(
1292 cm.get(), now - TimeDelta::FromDays(3),
1293 now - TimeDelta::FromDays(1), false_predicate));
1294 // Same as above, but we use the true_predicate, so it works.
1295 EXPECT_EQ(2, DeleteAllCreatedBetweenWithPredicate(
1296 cm.get(), now - TimeDelta::FromDays(3),
1297 now - TimeDelta::FromDays(1), true_predicate));
1298
1299 // Try to delete yesterday, also make sure that delete_end is not
1300 // inclusive.
1301 EXPECT_EQ(0,
1302 DeleteAllCreatedBetweenWithPredicate(
1303 cm.get(), now - TimeDelta::FromDays(2), now, false_predicate));
1304 EXPECT_EQ(1,
1305 DeleteAllCreatedBetweenWithPredicate(
1306 cm.get(), now - TimeDelta::FromDays(2), now, true_predicate));
1307 // Check our cookie values.
1308 scoped_ptr<CanonicalCookie> expected_cookie =
1309 CanonicalCookie::Create(http_www_google_.url(), "T-1=Yesterday",
1310 now - TimeDelta::FromDays(1), CookieOptions());
1311 EXPECT_THAT(test_cookie, CookieEquals(*expected_cookie))
1312 << "Actual:\n"
1313 << test_cookie.DebugString() << "\nExpected:\n"
1314 << expected_cookie->DebugString();
1315
1316 // Make sure the delete_begin is inclusive.
1317 EXPECT_EQ(0,
1318 DeleteAllCreatedBetweenWithPredicate(
1319 cm.get(), now - TimeDelta::FromDays(7), now, false_predicate));
1320 EXPECT_EQ(1,
1321 DeleteAllCreatedBetweenWithPredicate(
1322 cm.get(), now - TimeDelta::FromDays(7), now, true_predicate));
1323
1324 // Delete the last (now) item.
1325 EXPECT_EQ(0, DeleteAllCreatedBetweenWithPredicate(cm.get(), Time(), Time(),
1326 false_predicate));
1327 EXPECT_EQ(1, DeleteAllCreatedBetweenWithPredicate(cm.get(), Time(), Time(),
1328 true_predicate));
1329 expected_cookie = CanonicalCookie::Create(http_www_google_.url(), "T-0=Now",
1330 now, CookieOptions());
1331 EXPECT_THAT(test_cookie, CookieEquals(*expected_cookie))
1332 << "Actual:\n"
1333 << test_cookie.DebugString() << "\nExpected:\n"
1334 << expected_cookie->DebugString();
1335
1336 // Really make sure everything is gone.
1337 EXPECT_EQ(0, DeleteAll(cm.get()));
1338 }
1339
1222 static const int kAccessDelayMs = kLastAccessThresholdMilliseconds + 20; 1340 static const int kAccessDelayMs = kLastAccessThresholdMilliseconds + 20;
1223 1341
1224 TEST_F(CookieMonsterTest, TestLastAccess) { 1342 TEST_F(CookieMonsterTest, TestLastAccess) {
1225 scoped_ptr<CookieMonster> cm( 1343 scoped_ptr<CookieMonster> cm(
1226 new CookieMonster(nullptr, nullptr, kLastAccessThresholdMilliseconds)); 1344 new CookieMonster(nullptr, nullptr, kLastAccessThresholdMilliseconds));
1227 1345
1228 EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "A=B")); 1346 EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "A=B"));
1229 const Time last_access_date(GetFirstCookieAccessDate(cm.get())); 1347 const Time last_access_date(GetFirstCookieAccessDate(cm.get()));
1230 1348
1231 // Reading the cookie again immediately shouldn't update the access date, 1349 // Reading the cookie again immediately shouldn't update the access date,
(...skipping 1955 matching lines...) Expand 10 before | Expand all | Expand 10 after
3187 monster()->AddCallbackForCookie( 3305 monster()->AddCallbackForCookie(
3188 test_url_, "abc", 3306 test_url_, "abc",
3189 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); 3307 base::Bind(&RecordCookieChanges, &cookies1, nullptr)));
3190 SetCookie(monster(), test_url_, "abc=def"); 3308 SetCookie(monster(), test_url_, "abc=def");
3191 base::MessageLoop::current()->RunUntilIdle(); 3309 base::MessageLoop::current()->RunUntilIdle();
3192 EXPECT_EQ(1U, cookies0.size()); 3310 EXPECT_EQ(1U, cookies0.size());
3193 EXPECT_EQ(1U, cookies0.size()); 3311 EXPECT_EQ(1U, cookies0.size());
3194 } 3312 }
3195 3313
3196 } // namespace net 3314 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698