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_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 Loading... |
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 MATCHER_P(CookieEquals, expected, "") { |
| 68 return !(arg.FullCompare(expected) || expected.FullCompare(arg)); |
| 69 } |
| 70 |
67 const char kTopLevelDomainPlus1[] = "http://www.harvard.edu"; | 71 const char kTopLevelDomainPlus1[] = "http://www.harvard.edu"; |
68 const char kTopLevelDomainPlus2[] = "http://www.math.harvard.edu"; | 72 const char kTopLevelDomainPlus2[] = "http://www.math.harvard.edu"; |
69 const char kTopLevelDomainPlus2Secure[] = "https://www.math.harvard.edu"; | 73 const char kTopLevelDomainPlus2Secure[] = "https://www.math.harvard.edu"; |
70 const char kTopLevelDomainPlus3[] = "http://www.bourbaki.math.harvard.edu"; | 74 const char kTopLevelDomainPlus3[] = "http://www.bourbaki.math.harvard.edu"; |
71 const char kOtherDomain[] = "http://www.mit.edu"; | 75 const char kOtherDomain[] = "http://www.mit.edu"; |
72 | 76 |
| 77 bool AlwaysTrueCookiePredicate(CanonicalCookie* to_save, |
| 78 const CanonicalCookie& cookie) { |
| 79 if (to_save) |
| 80 *to_save = cookie; |
| 81 return true; |
| 82 } |
| 83 |
| 84 bool AlwaysFalseCookiePredicate(CanonicalCookie* to_save, |
| 85 const CanonicalCookie& cookie) { |
| 86 if (to_save) |
| 87 *to_save = cookie; |
| 88 return false; |
| 89 } |
| 90 |
73 struct CookieMonsterTestTraits { | 91 struct CookieMonsterTestTraits { |
74 static scoped_ptr<CookieStore> Create() { | 92 static scoped_ptr<CookieStore> Create() { |
75 return make_scoped_ptr(new CookieMonster(nullptr, nullptr)); | 93 return make_scoped_ptr(new CookieMonster(nullptr, nullptr)); |
76 } | 94 } |
77 | 95 |
78 static const bool supports_http_only = true; | 96 static const bool supports_http_only = true; |
79 static const bool supports_non_dotted_domains = true; | 97 static const bool supports_non_dotted_domains = true; |
80 static const bool preserves_trailing_dots = true; | 98 static const bool preserves_trailing_dots = true; |
81 static const bool filters_schemes = true; | 99 static const bool filters_schemes = true; |
82 static const bool has_path_prefix_bug = false; | 100 static const bool has_path_prefix_bug = false; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 DCHECK(cm); | 175 DCHECK(cm); |
158 ResultSavingCookieCallback<int> callback; | 176 ResultSavingCookieCallback<int> callback; |
159 cm->DeleteAllCreatedBetweenForHostAsync( | 177 cm->DeleteAllCreatedBetweenForHostAsync( |
160 delete_begin, delete_end, url, | 178 delete_begin, delete_end, url, |
161 base::Bind(&ResultSavingCookieCallback<int>::Run, | 179 base::Bind(&ResultSavingCookieCallback<int>::Run, |
162 base::Unretained(&callback))); | 180 base::Unretained(&callback))); |
163 callback.WaitUntilDone(); | 181 callback.WaitUntilDone(); |
164 return callback.result(); | 182 return callback.result(); |
165 } | 183 } |
166 | 184 |
| 185 int DeleteAllCreatedBetweenWithPredicate( |
| 186 CookieMonster* cm, |
| 187 const base::Time delete_begin, |
| 188 const base::Time delete_end, |
| 189 const base::Callback<bool(const CanonicalCookie&)>& predicate) { |
| 190 DCHECK(cm); |
| 191 ResultSavingCookieCallback<int> callback; |
| 192 cm->DeleteAllCreatedBetweenWithPredicateAsync( |
| 193 delete_begin, delete_end, predicate, |
| 194 base::Bind(&ResultSavingCookieCallback<int>::Run, |
| 195 base::Unretained(&callback))); |
| 196 callback.WaitUntilDone(); |
| 197 return callback.result(); |
| 198 } |
| 199 |
167 // Helper for DeleteAllForHost test; repopulates CM with same layout | 200 // Helper for DeleteAllForHost test; repopulates CM with same layout |
168 // each time. | 201 // each time. |
169 void PopulateCmForDeleteAllForHost(CookieMonster* cm) { | 202 void PopulateCmForDeleteAllForHost(CookieMonster* cm) { |
170 GURL url_top_level_domain_plus_1(kTopLevelDomainPlus1); | 203 GURL url_top_level_domain_plus_1(kTopLevelDomainPlus1); |
171 GURL url_top_level_domain_plus_2(kTopLevelDomainPlus2); | 204 GURL url_top_level_domain_plus_2(kTopLevelDomainPlus2); |
172 GURL url_top_level_domain_plus_2_secure(kTopLevelDomainPlus2Secure); | 205 GURL url_top_level_domain_plus_2_secure(kTopLevelDomainPlus2Secure); |
173 GURL url_top_level_domain_plus_3(kTopLevelDomainPlus3); | 206 GURL url_top_level_domain_plus_3(kTopLevelDomainPlus3); |
174 GURL url_other(kOtherDomain); | 207 GURL url_other(kOtherDomain); |
175 | 208 |
176 this->DeleteAll(cm); | 209 this->DeleteAll(cm); |
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
668 ACTION_P5(DeleteAllCreatedBetweenForHostAction, | 701 ACTION_P5(DeleteAllCreatedBetweenForHostAction, |
669 cookie_monster, | 702 cookie_monster, |
670 delete_begin, | 703 delete_begin, |
671 delete_end, | 704 delete_end, |
672 url, | 705 url, |
673 callback) { | 706 callback) { |
674 cookie_monster->DeleteAllCreatedBetweenForHostAsync( | 707 cookie_monster->DeleteAllCreatedBetweenForHostAsync( |
675 delete_begin, delete_end, url, callback->AsCallback()); | 708 delete_begin, delete_end, url, callback->AsCallback()); |
676 } | 709 } |
677 | 710 |
| 711 ACTION_P5(DeleteAllCreatedBetweenWithPredicateAction, |
| 712 cookie_monster, |
| 713 delete_begin, |
| 714 delete_end, |
| 715 predicate, |
| 716 callback) { |
| 717 cookie_monster->DeleteAllCreatedBetweenWithPredicateAsync( |
| 718 delete_begin, delete_end, predicate, callback->AsCallback()); |
| 719 } |
| 720 |
678 ACTION_P3(DeleteCanonicalCookieAction, cookie_monster, cookie, callback) { | 721 ACTION_P3(DeleteCanonicalCookieAction, cookie_monster, cookie, callback) { |
679 cookie_monster->DeleteCanonicalCookieAsync(cookie, callback->AsCallback()); | 722 cookie_monster->DeleteCanonicalCookieAsync(cookie, callback->AsCallback()); |
680 } | 723 } |
681 | 724 |
682 ACTION_P2(DeleteAllAction, cookie_monster, callback) { | 725 ACTION_P2(DeleteAllAction, cookie_monster, callback) { |
683 cookie_monster->DeleteAllAsync(callback->AsCallback()); | 726 cookie_monster->DeleteAllAsync(callback->AsCallback()); |
684 } | 727 } |
685 | 728 |
686 ACTION_P3(GetCookieListForUrlWithOptionsAction, cookie_monster, url, callback) { | 729 ACTION_P3(GetCookieListForUrlWithOptionsAction, cookie_monster, url, callback) { |
687 cookie_monster->GetCookieListWithOptionsAsync(url, CookieOptions(), | 730 cookie_monster->GetCookieListWithOptionsAsync(url, CookieOptions(), |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1062 .WillOnce(DeleteAllCreatedBetweenForHostAction( | 1105 .WillOnce(DeleteAllCreatedBetweenForHostAction( |
1063 &cookie_monster(), base::Time(), base::Time::Now(), | 1106 &cookie_monster(), base::Time(), base::Time::Now(), |
1064 http_www_google_.url(), &delete_callback)); | 1107 http_www_google_.url(), &delete_callback)); |
1065 base::RunLoop loop; | 1108 base::RunLoop loop; |
1066 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(QuitRunLoop(&loop)); | 1109 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(QuitRunLoop(&loop)); |
1067 | 1110 |
1068 CompleteLoading(); | 1111 CompleteLoading(); |
1069 loop.Run(); | 1112 loop.Run(); |
1070 } | 1113 } |
1071 | 1114 |
| 1115 TEST_F(DeferredCookieTaskTest, |
| 1116 DeferredDeleteAllWithPredicateCreatedBetweenCookies) { |
| 1117 MockDeleteCallback delete_callback; |
| 1118 |
| 1119 base::Callback<bool(const CanonicalCookie&)> predicate = |
| 1120 base::Bind(&AlwaysTrueCookiePredicate, nullptr); |
| 1121 |
| 1122 BeginWith(DeleteAllCreatedBetweenWithPredicateAction( |
| 1123 &cookie_monster(), base::Time(), base::Time::Now(), predicate, |
| 1124 &delete_callback)); |
| 1125 |
| 1126 WaitForLoadCall(); |
| 1127 |
| 1128 EXPECT_CALL(delete_callback, Invoke(false)) |
| 1129 .WillOnce(DeleteAllCreatedBetweenWithPredicateAction( |
| 1130 &cookie_monster(), base::Time(), base::Time::Now(), predicate, |
| 1131 &delete_callback)); |
| 1132 base::RunLoop loop; |
| 1133 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(QuitRunLoop(&loop)); |
| 1134 |
| 1135 CompleteLoading(); |
| 1136 loop.Run(); |
| 1137 } |
| 1138 |
1072 TEST_F(DeferredCookieTaskTest, DeferredDeleteCanonicalCookie) { | 1139 TEST_F(DeferredCookieTaskTest, DeferredDeleteCanonicalCookie) { |
1073 std::vector<CanonicalCookie*> cookies; | 1140 std::vector<CanonicalCookie*> cookies; |
1074 CanonicalCookie cookie = BuildCanonicalCookie( | 1141 CanonicalCookie cookie = BuildCanonicalCookie( |
1075 http_www_google_.host(), "X=1; path=/", base::Time::Now()); | 1142 http_www_google_.host(), "X=1; path=/", base::Time::Now()); |
1076 | 1143 |
1077 MockDeleteCallback delete_cookie_callback; | 1144 MockDeleteCallback delete_cookie_callback; |
1078 | 1145 |
1079 BeginWith(DeleteCanonicalCookieAction(&cookie_monster(), cookie, | 1146 BeginWith(DeleteCanonicalCookieAction(&cookie_monster(), cookie, |
1080 &delete_cookie_callback)); | 1147 &delete_cookie_callback)); |
1081 | 1148 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1210 EXPECT_EQ( | 1277 EXPECT_EQ( |
1211 1, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(7), now)); | 1278 1, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(7), now)); |
1212 | 1279 |
1213 // Delete the last (now) item. | 1280 // Delete the last (now) item. |
1214 EXPECT_EQ(1, DeleteAllCreatedBetween(cm.get(), Time(), Time())); | 1281 EXPECT_EQ(1, DeleteAllCreatedBetween(cm.get(), Time(), Time())); |
1215 | 1282 |
1216 // Really make sure everything is gone. | 1283 // Really make sure everything is gone. |
1217 EXPECT_EQ(0, DeleteAll(cm.get())); | 1284 EXPECT_EQ(0, DeleteAll(cm.get())); |
1218 } | 1285 } |
1219 | 1286 |
| 1287 TEST_F(CookieMonsterTest, |
| 1288 TestCookieDeleteAllCreatedBetweenTimestampsWithPredicate) { |
| 1289 scoped_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); |
| 1290 Time now = Time::Now(); |
| 1291 |
| 1292 CanonicalCookie test_cookie; |
| 1293 base::Callback<bool(const CanonicalCookie&)> true_predicate = |
| 1294 base::Bind(&AlwaysTrueCookiePredicate, &test_cookie); |
| 1295 |
| 1296 base::Callback<bool(const CanonicalCookie&)> false_predicate = |
| 1297 base::Bind(&AlwaysFalseCookiePredicate, &test_cookie); |
| 1298 |
| 1299 // Nothing has been added so nothing should be deleted. |
| 1300 EXPECT_EQ( |
| 1301 0, DeleteAllCreatedBetweenWithPredicate( |
| 1302 cm.get(), now - TimeDelta::FromDays(99), Time(), true_predicate)); |
| 1303 |
| 1304 // Create 3 cookies with creation date of today, yesterday and the day before. |
| 1305 EXPECT_TRUE( |
| 1306 cm->SetCookieWithCreationTime(http_www_google_.url(), "T-0=Now", now)); |
| 1307 EXPECT_TRUE(cm->SetCookieWithCreationTime( |
| 1308 http_www_google_.url(), "T-1=Yesterday", now - TimeDelta::FromDays(1))); |
| 1309 EXPECT_TRUE(cm->SetCookieWithCreationTime( |
| 1310 http_www_google_.url(), "T-2=DayBefore", now - TimeDelta::FromDays(2))); |
| 1311 EXPECT_TRUE(cm->SetCookieWithCreationTime( |
| 1312 http_www_google_.url(), "T-3=ThreeDays", now - TimeDelta::FromDays(3))); |
| 1313 EXPECT_TRUE(cm->SetCookieWithCreationTime( |
| 1314 http_www_google_.url(), "T-7=LastWeek", now - TimeDelta::FromDays(7))); |
| 1315 |
| 1316 // Try to delete threedays and the daybefore, but we should do nothing due |
| 1317 // to the predicate. |
| 1318 EXPECT_EQ(0, DeleteAllCreatedBetweenWithPredicate( |
| 1319 cm.get(), now - TimeDelta::FromDays(3), |
| 1320 now - TimeDelta::FromDays(1), false_predicate)); |
| 1321 // Same as above, but we use the true_predicate, so it works. |
| 1322 EXPECT_EQ(2, DeleteAllCreatedBetweenWithPredicate( |
| 1323 cm.get(), now - TimeDelta::FromDays(3), |
| 1324 now - TimeDelta::FromDays(1), true_predicate)); |
| 1325 |
| 1326 // Try to delete yesterday, also make sure that delete_end is not |
| 1327 // inclusive. |
| 1328 EXPECT_EQ(0, |
| 1329 DeleteAllCreatedBetweenWithPredicate( |
| 1330 cm.get(), now - TimeDelta::FromDays(2), now, false_predicate)); |
| 1331 EXPECT_EQ(1, |
| 1332 DeleteAllCreatedBetweenWithPredicate( |
| 1333 cm.get(), now - TimeDelta::FromDays(2), now, true_predicate)); |
| 1334 // Check our cookie values. |
| 1335 scoped_ptr<CanonicalCookie> expected_cookie = |
| 1336 CanonicalCookie::Create(http_www_google_.url(), "T-1=Yesterday", |
| 1337 now - TimeDelta::FromDays(1), CookieOptions()); |
| 1338 // False means 'less than or equal', so we test both ways for full equal. |
| 1339 EXPECT_THAT(test_cookie, CookieEquals(*expected_cookie)) |
| 1340 << "Actual:\n" |
| 1341 << test_cookie.DebugString() << "\nExpected:\n" |
| 1342 << expected_cookie->DebugString(); |
| 1343 |
| 1344 // Make sure the delete_begin is inclusive. |
| 1345 EXPECT_EQ(0, |
| 1346 DeleteAllCreatedBetweenWithPredicate( |
| 1347 cm.get(), now - TimeDelta::FromDays(7), now, false_predicate)); |
| 1348 EXPECT_EQ(1, |
| 1349 DeleteAllCreatedBetweenWithPredicate( |
| 1350 cm.get(), now - TimeDelta::FromDays(7), now, true_predicate)); |
| 1351 |
| 1352 // Delete the last (now) item. |
| 1353 EXPECT_EQ(0, DeleteAllCreatedBetweenWithPredicate(cm.get(), Time(), Time(), |
| 1354 false_predicate)); |
| 1355 EXPECT_EQ(1, DeleteAllCreatedBetweenWithPredicate(cm.get(), Time(), Time(), |
| 1356 true_predicate)); |
| 1357 expected_cookie = CanonicalCookie::Create(http_www_google_.url(), "T-0=Now", |
| 1358 now, CookieOptions()); |
| 1359 // False means 'less than or equal', so we test both ways for full equal. |
| 1360 EXPECT_FALSE(expected_cookie->FullCompare(test_cookie) || |
| 1361 test_cookie.FullCompare(*expected_cookie)) |
| 1362 << "Actual:\n" |
| 1363 << test_cookie.DebugString() << "\nExpected:\n" |
| 1364 << expected_cookie->DebugString(); |
| 1365 |
| 1366 // Really make sure everything is gone. |
| 1367 EXPECT_EQ(0, DeleteAll(cm.get())); |
| 1368 } |
| 1369 |
1220 static const int kAccessDelayMs = kLastAccessThresholdMilliseconds + 20; | 1370 static const int kAccessDelayMs = kLastAccessThresholdMilliseconds + 20; |
1221 | 1371 |
1222 TEST_F(CookieMonsterTest, TestLastAccess) { | 1372 TEST_F(CookieMonsterTest, TestLastAccess) { |
1223 scoped_ptr<CookieMonster> cm( | 1373 scoped_ptr<CookieMonster> cm( |
1224 new CookieMonster(nullptr, nullptr, kLastAccessThresholdMilliseconds)); | 1374 new CookieMonster(nullptr, nullptr, kLastAccessThresholdMilliseconds)); |
1225 | 1375 |
1226 EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "A=B")); | 1376 EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "A=B")); |
1227 const Time last_access_date(GetFirstCookieAccessDate(cm.get())); | 1377 const Time last_access_date(GetFirstCookieAccessDate(cm.get())); |
1228 | 1378 |
1229 // Reading the cookie again immediately shouldn't update the access date, | 1379 // Reading the cookie again immediately shouldn't update the access date, |
(...skipping 973 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2203 public: | 2353 public: |
2204 FlushablePersistentStore() : flush_count_(0) {} | 2354 FlushablePersistentStore() : flush_count_(0) {} |
2205 | 2355 |
2206 void Load(const LoadedCallback& loaded_callback) override { | 2356 void Load(const LoadedCallback& loaded_callback) override { |
2207 std::vector<CanonicalCookie*> out_cookies; | 2357 std::vector<CanonicalCookie*> out_cookies; |
2208 base::ThreadTaskRunnerHandle::Get()->PostTask( | 2358 base::ThreadTaskRunnerHandle::Get()->PostTask( |
2209 FROM_HERE, | 2359 FROM_HERE, |
2210 base::Bind(&LoadedCallbackTask::Run, | 2360 base::Bind(&LoadedCallbackTask::Run, |
2211 new LoadedCallbackTask(loaded_callback, out_cookies))); | 2361 new LoadedCallbackTask(loaded_callback, out_cookies))); |
2212 } | 2362 } |
2213 | |
2214 void LoadCookiesForKey(const std::string& key, | 2363 void LoadCookiesForKey(const std::string& key, |
2215 const LoadedCallback& loaded_callback) override { | 2364 const LoadedCallback& loaded_callback) override { |
2216 Load(loaded_callback); | 2365 Load(loaded_callback); |
2217 } | 2366 } |
2218 | 2367 |
2219 void AddCookie(const CanonicalCookie&) override {} | 2368 void AddCookie(const CanonicalCookie&) override {} |
2220 void UpdateCookieAccessTime(const CanonicalCookie&) override {} | 2369 void UpdateCookieAccessTime(const CanonicalCookie&) override {} |
2221 void DeleteCookie(const CanonicalCookie&) override {} | 2370 void DeleteCookie(const CanonicalCookie&) override {} |
2222 void SetForceKeepSessionState() override {} | 2371 void SetForceKeepSessionState() override {} |
2223 | 2372 |
(...skipping 982 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3206 monster()->AddCallbackForCookie( | 3355 monster()->AddCallbackForCookie( |
3207 test_url_, "abc", | 3356 test_url_, "abc", |
3208 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); | 3357 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); |
3209 SetCookie(monster(), test_url_, "abc=def"); | 3358 SetCookie(monster(), test_url_, "abc=def"); |
3210 base::MessageLoop::current()->RunUntilIdle(); | 3359 base::MessageLoop::current()->RunUntilIdle(); |
3211 EXPECT_EQ(1U, cookies0.size()); | 3360 EXPECT_EQ(1U, cookies0.size()); |
3212 EXPECT_EQ(1U, cookies0.size()); | 3361 EXPECT_EQ(1U, cookies0.size()); |
3213 } | 3362 } |
3214 | 3363 |
3215 } // namespace net | 3364 } // namespace net |
OLD | NEW |