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 |
| 91 bool TrueForHostCookiePredicate(const std::string& host, |
| 92 std::vector<CanonicalCookie>* out, |
| 93 const CanonicalCookie& cookie) { |
| 94 bool deleting = cookie.IsDomainMatch(host); |
| 95 if (deleting) { |
| 96 out->push_back(cookie); |
| 97 } |
| 98 return deleting; |
| 99 } |
| 100 |
73 struct CookieMonsterTestTraits { | 101 struct CookieMonsterTestTraits { |
74 static scoped_ptr<CookieStore> Create() { | 102 static scoped_ptr<CookieStore> Create() { |
75 return make_scoped_ptr(new CookieMonster(nullptr, nullptr)); | 103 return make_scoped_ptr(new CookieMonster(nullptr, nullptr)); |
76 } | 104 } |
77 | 105 |
78 static const bool supports_http_only = true; | 106 static const bool supports_http_only = true; |
79 static const bool supports_non_dotted_domains = true; | 107 static const bool supports_non_dotted_domains = true; |
80 static const bool preserves_trailing_dots = true; | 108 static const bool preserves_trailing_dots = true; |
81 static const bool filters_schemes = true; | 109 static const bool filters_schemes = true; |
82 static const bool has_path_prefix_bug = false; | 110 static const bool has_path_prefix_bug = false; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 DCHECK(cm); | 185 DCHECK(cm); |
158 ResultSavingCookieCallback<int> callback; | 186 ResultSavingCookieCallback<int> callback; |
159 cm->DeleteAllCreatedBetweenForHostAsync( | 187 cm->DeleteAllCreatedBetweenForHostAsync( |
160 delete_begin, delete_end, url, | 188 delete_begin, delete_end, url, |
161 base::Bind(&ResultSavingCookieCallback<int>::Run, | 189 base::Bind(&ResultSavingCookieCallback<int>::Run, |
162 base::Unretained(&callback))); | 190 base::Unretained(&callback))); |
163 callback.WaitUntilDone(); | 191 callback.WaitUntilDone(); |
164 return callback.result(); | 192 return callback.result(); |
165 } | 193 } |
166 | 194 |
| 195 int DeleteAllCreatedBetweenWithPredicate( |
| 196 CookieMonster* cm, |
| 197 const base::Time delete_begin, |
| 198 const base::Time delete_end, |
| 199 const base::Callback<bool(const CanonicalCookie&)>& predicate) { |
| 200 DCHECK(cm); |
| 201 ResultSavingCookieCallback<int> callback; |
| 202 cm->DeleteAllCreatedBetweenWithPredicateAsync( |
| 203 delete_begin, delete_end, predicate, |
| 204 base::Bind(&ResultSavingCookieCallback<int>::Run, |
| 205 base::Unretained(&callback))); |
| 206 callback.WaitUntilDone(); |
| 207 return callback.result(); |
| 208 } |
| 209 |
167 // Helper for DeleteAllForHost test; repopulates CM with same layout | 210 // Helper for DeleteAllForHost test; repopulates CM with same layout |
168 // each time. | 211 // each time. |
169 void PopulateCmForDeleteAllForHost(CookieMonster* cm) { | 212 void PopulateCmForDeleteAllForHost(CookieMonster* cm) { |
170 GURL url_top_level_domain_plus_1(kTopLevelDomainPlus1); | 213 GURL url_top_level_domain_plus_1(kTopLevelDomainPlus1); |
171 GURL url_top_level_domain_plus_2(kTopLevelDomainPlus2); | 214 GURL url_top_level_domain_plus_2(kTopLevelDomainPlus2); |
172 GURL url_top_level_domain_plus_2_secure(kTopLevelDomainPlus2Secure); | 215 GURL url_top_level_domain_plus_2_secure(kTopLevelDomainPlus2Secure); |
173 GURL url_top_level_domain_plus_3(kTopLevelDomainPlus3); | 216 GURL url_top_level_domain_plus_3(kTopLevelDomainPlus3); |
174 GURL url_other(kOtherDomain); | 217 GURL url_other(kOtherDomain); |
175 | 218 |
176 this->DeleteAll(cm); | 219 this->DeleteAll(cm); |
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
668 ACTION_P5(DeleteAllCreatedBetweenForHostAction, | 711 ACTION_P5(DeleteAllCreatedBetweenForHostAction, |
669 cookie_monster, | 712 cookie_monster, |
670 delete_begin, | 713 delete_begin, |
671 delete_end, | 714 delete_end, |
672 url, | 715 url, |
673 callback) { | 716 callback) { |
674 cookie_monster->DeleteAllCreatedBetweenForHostAsync( | 717 cookie_monster->DeleteAllCreatedBetweenForHostAsync( |
675 delete_begin, delete_end, url, callback->AsCallback()); | 718 delete_begin, delete_end, url, callback->AsCallback()); |
676 } | 719 } |
677 | 720 |
| 721 ACTION_P5(DeleteAllCreatedBetweenWithPredicateAction, |
| 722 cookie_monster, |
| 723 delete_begin, |
| 724 delete_end, |
| 725 predicate, |
| 726 callback) { |
| 727 cookie_monster->DeleteAllCreatedBetweenWithPredicateAsync( |
| 728 delete_begin, delete_end, predicate, callback->AsCallback()); |
| 729 } |
| 730 |
678 ACTION_P3(DeleteCanonicalCookieAction, cookie_monster, cookie, callback) { | 731 ACTION_P3(DeleteCanonicalCookieAction, cookie_monster, cookie, callback) { |
679 cookie_monster->DeleteCanonicalCookieAsync(cookie, callback->AsCallback()); | 732 cookie_monster->DeleteCanonicalCookieAsync(cookie, callback->AsCallback()); |
680 } | 733 } |
681 | 734 |
682 ACTION_P2(DeleteAllAction, cookie_monster, callback) { | 735 ACTION_P2(DeleteAllAction, cookie_monster, callback) { |
683 cookie_monster->DeleteAllAsync(callback->AsCallback()); | 736 cookie_monster->DeleteAllAsync(callback->AsCallback()); |
684 } | 737 } |
685 | 738 |
686 ACTION_P3(GetCookieListForUrlWithOptionsAction, cookie_monster, url, callback) { | 739 ACTION_P3(GetCookieListForUrlWithOptionsAction, cookie_monster, url, callback) { |
687 cookie_monster->GetCookieListWithOptionsAsync(url, CookieOptions(), | 740 cookie_monster->GetCookieListWithOptionsAsync(url, CookieOptions(), |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1062 .WillOnce(DeleteAllCreatedBetweenForHostAction( | 1115 .WillOnce(DeleteAllCreatedBetweenForHostAction( |
1063 &cookie_monster(), base::Time(), base::Time::Now(), | 1116 &cookie_monster(), base::Time(), base::Time::Now(), |
1064 http_www_google_.url(), &delete_callback)); | 1117 http_www_google_.url(), &delete_callback)); |
1065 base::RunLoop loop; | 1118 base::RunLoop loop; |
1066 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(QuitRunLoop(&loop)); | 1119 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(QuitRunLoop(&loop)); |
1067 | 1120 |
1068 CompleteLoading(); | 1121 CompleteLoading(); |
1069 loop.Run(); | 1122 loop.Run(); |
1070 } | 1123 } |
1071 | 1124 |
| 1125 TEST_F(DeferredCookieTaskTest, |
| 1126 DeferredDeleteAllWithPredicateCreatedBetweenCookies) { |
| 1127 MockDeleteCallback delete_callback; |
| 1128 |
| 1129 base::Callback<bool(const CanonicalCookie&)> predicate = |
| 1130 base::Bind(&AlwaysTrueCookiePredicate, nullptr); |
| 1131 |
| 1132 BeginWith(DeleteAllCreatedBetweenWithPredicateAction( |
| 1133 &cookie_monster(), base::Time(), base::Time::Now(), predicate, |
| 1134 &delete_callback)); |
| 1135 |
| 1136 WaitForLoadCall(); |
| 1137 |
| 1138 EXPECT_CALL(delete_callback, Invoke(false)) |
| 1139 .WillOnce(DeleteAllCreatedBetweenWithPredicateAction( |
| 1140 &cookie_monster(), base::Time(), base::Time::Now(), predicate, |
| 1141 &delete_callback)); |
| 1142 base::RunLoop loop; |
| 1143 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(QuitRunLoop(&loop)); |
| 1144 |
| 1145 CompleteLoading(); |
| 1146 loop.Run(); |
| 1147 } |
| 1148 |
1072 TEST_F(DeferredCookieTaskTest, DeferredDeleteCanonicalCookie) { | 1149 TEST_F(DeferredCookieTaskTest, DeferredDeleteCanonicalCookie) { |
1073 std::vector<CanonicalCookie*> cookies; | 1150 std::vector<CanonicalCookie*> cookies; |
1074 CanonicalCookie cookie = BuildCanonicalCookie( | 1151 CanonicalCookie cookie = BuildCanonicalCookie( |
1075 http_www_google_.host(), "X=1; path=/", base::Time::Now()); | 1152 http_www_google_.host(), "X=1; path=/", base::Time::Now()); |
1076 | 1153 |
1077 MockDeleteCallback delete_cookie_callback; | 1154 MockDeleteCallback delete_cookie_callback; |
1078 | 1155 |
1079 BeginWith(DeleteCanonicalCookieAction(&cookie_monster(), cookie, | 1156 BeginWith(DeleteCanonicalCookieAction(&cookie_monster(), cookie, |
1080 &delete_cookie_callback)); | 1157 &delete_cookie_callback)); |
1081 | 1158 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1210 EXPECT_EQ( | 1287 EXPECT_EQ( |
1211 1, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(7), now)); | 1288 1, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(7), now)); |
1212 | 1289 |
1213 // Delete the last (now) item. | 1290 // Delete the last (now) item. |
1214 EXPECT_EQ(1, DeleteAllCreatedBetween(cm.get(), Time(), Time())); | 1291 EXPECT_EQ(1, DeleteAllCreatedBetween(cm.get(), Time(), Time())); |
1215 | 1292 |
1216 // Really make sure everything is gone. | 1293 // Really make sure everything is gone. |
1217 EXPECT_EQ(0, DeleteAll(cm.get())); | 1294 EXPECT_EQ(0, DeleteAll(cm.get())); |
1218 } | 1295 } |
1219 | 1296 |
| 1297 TEST_F(CookieMonsterTest, |
| 1298 TestCookieDeleteAllCreatedBetweenTimestampsWithPredicate) { |
| 1299 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
| 1300 Time now = Time::Now(); |
| 1301 |
| 1302 CanonicalCookie test_cookie; |
| 1303 base::Callback<bool(const CanonicalCookie&)> true_predicate = |
| 1304 base::Bind(&AlwaysTrueCookiePredicate, &test_cookie); |
| 1305 |
| 1306 base::Callback<bool(const CanonicalCookie&)> false_predicate = |
| 1307 base::Bind(&AlwaysFalseCookiePredicate, &test_cookie); |
| 1308 |
| 1309 // Nothing has been added so nothing should be deleted. |
| 1310 EXPECT_EQ( |
| 1311 0, DeleteAllCreatedBetweenWithPredicate( |
| 1312 cm.get(), now - TimeDelta::FromDays(99), Time(), true_predicate)); |
| 1313 |
| 1314 // Create 3 cookies with creation date of today, yesterday and the day before. |
| 1315 EXPECT_TRUE( |
| 1316 cm->SetCookieWithCreationTime(http_www_google_.url(), "T-0=Now", now)); |
| 1317 EXPECT_TRUE(cm->SetCookieWithCreationTime( |
| 1318 http_www_google_.url(), "T-1=Yesterday", now - TimeDelta::FromDays(1))); |
| 1319 EXPECT_TRUE(cm->SetCookieWithCreationTime( |
| 1320 http_www_google_.url(), "T-2=DayBefore", now - TimeDelta::FromDays(2))); |
| 1321 EXPECT_TRUE(cm->SetCookieWithCreationTime( |
| 1322 http_www_google_.url(), "T-3=ThreeDays", now - TimeDelta::FromDays(3))); |
| 1323 EXPECT_TRUE(cm->SetCookieWithCreationTime( |
| 1324 http_www_google_.url(), "T-7=LastWeek", now - TimeDelta::FromDays(7))); |
| 1325 |
| 1326 // Try to delete threedays and the daybefore, but we should do nothing due |
| 1327 // to the predicate. |
| 1328 EXPECT_EQ(0, DeleteAllCreatedBetweenWithPredicate( |
| 1329 cm.get(), now - TimeDelta::FromDays(3), |
| 1330 now - TimeDelta::FromDays(1), false_predicate)); |
| 1331 // Same as above, but we use the true_predicate, so it works. |
| 1332 EXPECT_EQ(2, DeleteAllCreatedBetweenWithPredicate( |
| 1333 cm.get(), now - TimeDelta::FromDays(3), |
| 1334 now - TimeDelta::FromDays(1), true_predicate)); |
| 1335 |
| 1336 // Try to delete yesterday, also make sure that delete_end is not |
| 1337 // inclusive. |
| 1338 EXPECT_EQ(0, |
| 1339 DeleteAllCreatedBetweenWithPredicate( |
| 1340 cm.get(), now - TimeDelta::FromDays(2), now, false_predicate)); |
| 1341 EXPECT_EQ(1, |
| 1342 DeleteAllCreatedBetweenWithPredicate( |
| 1343 cm.get(), now - TimeDelta::FromDays(2), now, true_predicate)); |
| 1344 // Check our cookie values. |
| 1345 scoped_ptr<CanonicalCookie> expected_cookie = |
| 1346 CanonicalCookie::Create(http_www_google_.url(), "T-1=Yesterday", |
| 1347 now - TimeDelta::FromDays(1), CookieOptions()); |
| 1348 // False means 'less than or equal', so we test both ways for full equal. |
| 1349 EXPECT_THAT(test_cookie, CookieEquals(*expected_cookie)) |
| 1350 << "Actual:\n" |
| 1351 << test_cookie.DebugString() << "\nExpected:\n" |
| 1352 << expected_cookie->DebugString(); |
| 1353 |
| 1354 // Make sure the delete_begin is inclusive. |
| 1355 EXPECT_EQ(0, |
| 1356 DeleteAllCreatedBetweenWithPredicate( |
| 1357 cm.get(), now - TimeDelta::FromDays(7), now, false_predicate)); |
| 1358 EXPECT_EQ(1, |
| 1359 DeleteAllCreatedBetweenWithPredicate( |
| 1360 cm.get(), now - TimeDelta::FromDays(7), now, true_predicate)); |
| 1361 |
| 1362 // Delete the last (now) item. |
| 1363 EXPECT_EQ(0, DeleteAllCreatedBetweenWithPredicate(cm.get(), Time(), Time(), |
| 1364 false_predicate)); |
| 1365 EXPECT_EQ(1, DeleteAllCreatedBetweenWithPredicate(cm.get(), Time(), Time(), |
| 1366 true_predicate)); |
| 1367 expected_cookie = CanonicalCookie::Create(http_www_google_.url(), "T-0=Now", |
| 1368 now, CookieOptions()); |
| 1369 // False means 'less than or equal', so we test both ways for full equal. |
| 1370 EXPECT_FALSE(expected_cookie->FullCompare(test_cookie) || |
| 1371 test_cookie.FullCompare(*expected_cookie)) |
| 1372 << "Actual:\n" |
| 1373 << test_cookie.DebugString() << "\nExpected:\n" |
| 1374 << expected_cookie->DebugString(); |
| 1375 |
| 1376 // Really make sure everything is gone. |
| 1377 EXPECT_EQ(0, DeleteAll(cm.get())); |
| 1378 } |
| 1379 |
1220 static const int kAccessDelayMs = kLastAccessThresholdMilliseconds + 20; | 1380 static const int kAccessDelayMs = kLastAccessThresholdMilliseconds + 20; |
1221 | 1381 |
1222 TEST_F(CookieMonsterTest, TestLastAccess) { | 1382 TEST_F(CookieMonsterTest, TestLastAccess) { |
1223 scoped_ptr<CookieMonster> cm( | 1383 scoped_ptr<CookieMonster> cm( |
1224 new CookieMonster(nullptr, nullptr, kLastAccessThresholdMilliseconds)); | 1384 new CookieMonster(nullptr, nullptr, kLastAccessThresholdMilliseconds)); |
1225 | 1385 |
1226 EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "A=B")); | 1386 EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "A=B")); |
1227 const Time last_access_date(GetFirstCookieAccessDate(cm.get())); | 1387 const Time last_access_date(GetFirstCookieAccessDate(cm.get())); |
1228 | 1388 |
1229 // Reading the cookie again immediately shouldn't update the access date, | 1389 // 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: | 2363 public: |
2204 FlushablePersistentStore() : flush_count_(0) {} | 2364 FlushablePersistentStore() : flush_count_(0) {} |
2205 | 2365 |
2206 void Load(const LoadedCallback& loaded_callback) override { | 2366 void Load(const LoadedCallback& loaded_callback) override { |
2207 std::vector<CanonicalCookie*> out_cookies; | 2367 std::vector<CanonicalCookie*> out_cookies; |
2208 base::ThreadTaskRunnerHandle::Get()->PostTask( | 2368 base::ThreadTaskRunnerHandle::Get()->PostTask( |
2209 FROM_HERE, | 2369 FROM_HERE, |
2210 base::Bind(&LoadedCallbackTask::Run, | 2370 base::Bind(&LoadedCallbackTask::Run, |
2211 new LoadedCallbackTask(loaded_callback, out_cookies))); | 2371 new LoadedCallbackTask(loaded_callback, out_cookies))); |
2212 } | 2372 } |
2213 | 2373 delete_begin void LoadCookiesForKey( |
2214 void LoadCookiesForKey(const std::string& key, | 2374 const std::string& key, |
2215 const LoadedCallback& loaded_callback) override { | 2375 const LoadedCallback& loaded_callback) override { |
2216 Load(loaded_callback); | 2376 Load(loaded_callback); |
2217 } | 2377 } |
2218 | 2378 |
2219 void AddCookie(const CanonicalCookie&) override {} | 2379 void AddCookie(const CanonicalCookie&) override {} |
2220 void UpdateCookieAccessTime(const CanonicalCookie&) override {} | 2380 void UpdateCookieAccessTime(const CanonicalCookie&) override {} |
2221 void DeleteCookie(const CanonicalCookie&) override {} | 2381 void DeleteCookie(const CanonicalCookie&) override {} |
2222 void SetForceKeepSessionState() override {} | 2382 void SetForceKeepSessionState() override {} |
2223 | 2383 |
2224 void Flush(const base::Closure& callback) override { | 2384 void Flush(const base::Closure& callback) override { |
2225 ++flush_count_; | 2385 ++flush_count_; |
(...skipping 980 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3206 monster()->AddCallbackForCookie( | 3366 monster()->AddCallbackForCookie( |
3207 test_url_, "abc", | 3367 test_url_, "abc", |
3208 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); | 3368 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); |
3209 SetCookie(monster(), test_url_, "abc=def"); | 3369 SetCookie(monster(), test_url_, "abc=def"); |
3210 base::MessageLoop::current()->RunUntilIdle(); | 3370 base::MessageLoop::current()->RunUntilIdle(); |
3211 EXPECT_EQ(1U, cookies0.size()); | 3371 EXPECT_EQ(1U, cookies0.size()); |
3212 EXPECT_EQ(1U, cookies0.size()); | 3372 EXPECT_EQ(1U, cookies0.size()); |
3213 } | 3373 } |
3214 | 3374 |
3215 } // namespace net | 3375 } // namespace net |
OLD | NEW |