Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / cookies / cookie_monster_unittest.cc
blob6f3c0b616fb5703cd2e937dde5a3ce0cbd1382fe
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
3 // found in the LICENSE file.
5 #include "net/cookies/cookie_store_unittest.h"
7 #include <algorithm>
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/bind.h"
13 #include "base/location.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/metrics/histogram.h"
19 #include "base/metrics/histogram_samples.h"
20 #include "base/single_thread_task_runner.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_piece.h"
23 #include "base/strings/string_split.h"
24 #include "base/strings/string_tokenizer.h"
25 #include "base/strings/stringprintf.h"
26 #include "base/thread_task_runner_handle.h"
27 #include "base/threading/thread.h"
28 #include "base/time/time.h"
29 #include "net/cookies/canonical_cookie.h"
30 #include "net/cookies/cookie_constants.h"
31 #include "net/cookies/cookie_monster.h"
32 #include "net/cookies/cookie_monster_store_test.h" // For CookieStore mock
33 #include "net/cookies/cookie_util.h"
34 #include "net/cookies/parsed_cookie.h"
35 #include "testing/gmock/include/gmock/gmock.h"
36 #include "testing/gtest/include/gtest/gtest.h"
37 #include "url/gurl.h"
39 namespace net {
41 using base::Time;
42 using base::TimeDelta;
44 namespace {
46 // TODO(erikwright): Replace the pre-existing MockPersistentCookieStore (and
47 // brethren) with this one, and remove the 'New' prefix.
48 class NewMockPersistentCookieStore
49 : public CookieMonster::PersistentCookieStore {
50 public:
51 MOCK_METHOD1(Load, void(const LoadedCallback& loaded_callback));
52 MOCK_METHOD2(LoadCookiesForKey,
53 void(const std::string& key,
54 const LoadedCallback& loaded_callback));
55 MOCK_METHOD1(AddCookie, void(const CanonicalCookie& cc));
56 MOCK_METHOD1(UpdateCookieAccessTime, void(const CanonicalCookie& cc));
57 MOCK_METHOD1(DeleteCookie, void(const CanonicalCookie& cc));
58 virtual void Flush(const base::Closure& callback) {
59 if (!callback.is_null())
60 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
62 MOCK_METHOD0(SetForceKeepSessionState, void());
64 private:
65 virtual ~NewMockPersistentCookieStore() {}
68 const char kTopLevelDomainPlus1[] = "http://www.harvard.edu";
69 const char kTopLevelDomainPlus2[] = "http://www.math.harvard.edu";
70 const char kTopLevelDomainPlus2Secure[] = "https://www.math.harvard.edu";
71 const char kTopLevelDomainPlus3[] = "http://www.bourbaki.math.harvard.edu";
72 const char kOtherDomain[] = "http://www.mit.edu";
73 const char kUrlGoogleSpecific[] = "http://www.gmail.google.izzle";
75 class GetCookieListCallback : public CookieCallback {
76 public:
77 GetCookieListCallback() {}
78 explicit GetCookieListCallback(Thread* run_in_thread)
79 : CookieCallback(run_in_thread) {}
81 void Run(const CookieList& cookies) {
82 cookies_ = cookies;
83 CallbackEpilogue();
86 const CookieList& cookies() { return cookies_; }
88 private:
89 CookieList cookies_;
92 struct CookieMonsterTestTraits {
93 static scoped_refptr<CookieStore> Create() {
94 return new CookieMonster(NULL, NULL);
97 static const bool is_cookie_monster = true;
98 static const bool supports_http_only = true;
99 static const bool supports_non_dotted_domains = true;
100 static const bool supports_trailing_dots = true;
101 static const bool filters_schemes = true;
102 static const bool has_path_prefix_bug = false;
103 static const int creation_time_granularity_in_ms = 0;
106 INSTANTIATE_TYPED_TEST_CASE_P(CookieMonster,
107 CookieStoreTest,
108 CookieMonsterTestTraits);
110 INSTANTIATE_TYPED_TEST_CASE_P(CookieMonster,
111 MultiThreadedCookieStoreTest,
112 CookieMonsterTestTraits);
114 class CookieMonsterTest : public CookieStoreTest<CookieMonsterTestTraits> {
115 protected:
116 CookieList GetAllCookies(CookieMonster* cm) {
117 DCHECK(cm);
118 GetCookieListCallback callback;
119 cm->GetAllCookiesAsync(
120 base::Bind(&GetCookieListCallback::Run, base::Unretained(&callback)));
121 RunFor(kTimeout);
122 EXPECT_TRUE(callback.did_run());
123 return callback.cookies();
126 CookieList GetAllCookiesForURL(CookieMonster* cm, const GURL& url) {
127 DCHECK(cm);
128 GetCookieListCallback callback;
129 cm->GetAllCookiesForURLAsync(url, base::Bind(&GetCookieListCallback::Run,
130 base::Unretained(&callback)));
131 RunFor(kTimeout);
132 EXPECT_TRUE(callback.did_run());
133 return callback.cookies();
136 CookieList GetAllCookiesForURLWithOptions(CookieMonster* cm,
137 const GURL& url,
138 const CookieOptions& options) {
139 DCHECK(cm);
140 GetCookieListCallback callback;
141 cm->GetAllCookiesForURLWithOptionsAsync(
142 url, options,
143 base::Bind(&GetCookieListCallback::Run, base::Unretained(&callback)));
144 RunFor(kTimeout);
145 EXPECT_TRUE(callback.did_run());
146 return callback.cookies();
149 bool SetCookieWithDetails(CookieMonster* cm,
150 const GURL& url,
151 const std::string& name,
152 const std::string& value,
153 const std::string& domain,
154 const std::string& path,
155 const base::Time& expiration_time,
156 bool secure,
157 bool http_only,
158 bool first_party_only,
159 CookiePriority priority) {
160 DCHECK(cm);
161 ResultSavingCookieCallback<bool> callback;
162 cm->SetCookieWithDetailsAsync(
163 url, name, value, domain, path, expiration_time, secure, http_only,
164 first_party_only, priority,
165 base::Bind(&ResultSavingCookieCallback<bool>::Run,
166 base::Unretained(&callback)));
167 RunFor(kTimeout);
168 EXPECT_TRUE(callback.did_run());
169 return callback.result();
172 bool SetAllCookies(CookieMonster* cm, const CookieList& list) {
173 DCHECK(cm);
174 ResultSavingCookieCallback<bool> callback;
175 cm->SetAllCookiesAsync(list,
176 base::Bind(&ResultSavingCookieCallback<bool>::Run,
177 base::Unretained(&callback)));
178 RunFor(kTimeout);
179 EXPECT_TRUE(callback.did_run());
180 return callback.result();
183 int DeleteAll(CookieMonster* cm) {
184 DCHECK(cm);
185 ResultSavingCookieCallback<int> callback;
186 cm->DeleteAllAsync(base::Bind(&ResultSavingCookieCallback<int>::Run,
187 base::Unretained(&callback)));
188 RunFor(kTimeout);
189 EXPECT_TRUE(callback.did_run());
190 return callback.result();
193 int DeleteAllCreatedBetween(CookieMonster* cm,
194 const base::Time& delete_begin,
195 const base::Time& delete_end) {
196 DCHECK(cm);
197 ResultSavingCookieCallback<int> callback;
198 cm->DeleteAllCreatedBetweenAsync(
199 delete_begin, delete_end,
200 base::Bind(&ResultSavingCookieCallback<int>::Run,
201 base::Unretained(&callback)));
202 RunFor(kTimeout);
203 EXPECT_TRUE(callback.did_run());
204 return callback.result();
207 int DeleteAllCreatedBetweenForHost(CookieMonster* cm,
208 const base::Time delete_begin,
209 const base::Time delete_end,
210 const GURL& url) {
211 DCHECK(cm);
212 ResultSavingCookieCallback<int> callback;
213 cm->DeleteAllCreatedBetweenForHostAsync(
214 delete_begin, delete_end, url,
215 base::Bind(&ResultSavingCookieCallback<int>::Run,
216 base::Unretained(&callback)));
217 RunFor(kTimeout);
218 EXPECT_TRUE(callback.did_run());
219 return callback.result();
222 int DeleteAllForHost(CookieMonster* cm, const GURL& url) {
223 DCHECK(cm);
224 ResultSavingCookieCallback<int> callback;
225 cm->DeleteAllForHostAsync(url,
226 base::Bind(&ResultSavingCookieCallback<int>::Run,
227 base::Unretained(&callback)));
228 RunFor(kTimeout);
229 EXPECT_TRUE(callback.did_run());
230 return callback.result();
233 bool DeleteCanonicalCookie(CookieMonster* cm, const CanonicalCookie& cookie) {
234 DCHECK(cm);
235 ResultSavingCookieCallback<bool> callback;
236 cm->DeleteCanonicalCookieAsync(
237 cookie, base::Bind(&ResultSavingCookieCallback<bool>::Run,
238 base::Unretained(&callback)));
239 RunFor(kTimeout);
240 EXPECT_TRUE(callback.did_run());
241 return callback.result();
244 // Helper for DeleteAllForHost test; repopulates CM with same layout
245 // each time.
246 void PopulateCmForDeleteAllForHost(scoped_refptr<CookieMonster> cm) {
247 GURL url_top_level_domain_plus_1(kTopLevelDomainPlus1);
248 GURL url_top_level_domain_plus_2(kTopLevelDomainPlus2);
249 GURL url_top_level_domain_plus_2_secure(kTopLevelDomainPlus2Secure);
250 GURL url_top_level_domain_plus_3(kTopLevelDomainPlus3);
251 GURL url_other(kOtherDomain);
253 DeleteAll(cm.get());
255 // Static population for probe:
256 // * Three levels of domain cookie (.b.a, .c.b.a, .d.c.b.a)
257 // * Three levels of host cookie (w.b.a, w.c.b.a, w.d.c.b.a)
258 // * http_only cookie (w.c.b.a)
259 // * first-party cookie (w.c.b.a)
260 // * Two secure cookies (.c.b.a, w.c.b.a)
261 // * Two domain path cookies (.c.b.a/dir1, .c.b.a/dir1/dir2)
262 // * Two host path cookies (w.c.b.a/dir1, w.c.b.a/dir1/dir2)
264 // Domain cookies
265 EXPECT_TRUE(this->SetCookieWithDetails(
266 cm.get(), url_top_level_domain_plus_1, "dom_1", "X", ".harvard.edu",
267 "/", base::Time(), false, false, false, COOKIE_PRIORITY_DEFAULT));
268 EXPECT_TRUE(this->SetCookieWithDetails(
269 cm.get(), url_top_level_domain_plus_2, "dom_2", "X",
270 ".math.harvard.edu", "/", base::Time(), false, false, false,
271 COOKIE_PRIORITY_DEFAULT));
272 EXPECT_TRUE(this->SetCookieWithDetails(
273 cm.get(), url_top_level_domain_plus_3, "dom_3", "X",
274 ".bourbaki.math.harvard.edu", "/", base::Time(), false, false, false,
275 COOKIE_PRIORITY_DEFAULT));
277 // Host cookies
278 EXPECT_TRUE(this->SetCookieWithDetails(
279 cm.get(), url_top_level_domain_plus_1, "host_1", "X", std::string(),
280 "/", base::Time(), false, false, false, COOKIE_PRIORITY_DEFAULT));
281 EXPECT_TRUE(this->SetCookieWithDetails(
282 cm.get(), url_top_level_domain_plus_2, "host_2", "X", std::string(),
283 "/", base::Time(), false, false, false, COOKIE_PRIORITY_DEFAULT));
284 EXPECT_TRUE(this->SetCookieWithDetails(
285 cm.get(), url_top_level_domain_plus_3, "host_3", "X", std::string(),
286 "/", base::Time(), false, false, false, COOKIE_PRIORITY_DEFAULT));
288 // http_only cookie
289 EXPECT_TRUE(this->SetCookieWithDetails(
290 cm.get(), url_top_level_domain_plus_2, "httpo_check", "x",
291 std::string(), "/", base::Time(), false, true, false,
292 COOKIE_PRIORITY_DEFAULT));
294 // first-party cookie
295 EXPECT_TRUE(this->SetCookieWithDetails(
296 cm.get(), url_top_level_domain_plus_2, "firstp_check", "x",
297 std::string(), "/", base::Time(), false, false, true,
298 COOKIE_PRIORITY_DEFAULT));
300 // Secure cookies
301 EXPECT_TRUE(this->SetCookieWithDetails(
302 cm.get(), url_top_level_domain_plus_2_secure, "sec_dom", "X",
303 ".math.harvard.edu", "/", base::Time(), true, false, false,
304 COOKIE_PRIORITY_DEFAULT));
305 EXPECT_TRUE(this->SetCookieWithDetails(
306 cm.get(), url_top_level_domain_plus_2_secure, "sec_host", "X",
307 std::string(), "/", base::Time(), true, false, false,
308 COOKIE_PRIORITY_DEFAULT));
310 // Domain path cookies
311 EXPECT_TRUE(this->SetCookieWithDetails(
312 cm.get(), url_top_level_domain_plus_2, "dom_path_1", "X",
313 ".math.harvard.edu", "/dir1", base::Time(), false, false, false,
314 COOKIE_PRIORITY_DEFAULT));
315 EXPECT_TRUE(this->SetCookieWithDetails(
316 cm.get(), url_top_level_domain_plus_2, "dom_path_2", "X",
317 ".math.harvard.edu", "/dir1/dir2", base::Time(), false, false, false,
318 COOKIE_PRIORITY_DEFAULT));
320 // Host path cookies
321 EXPECT_TRUE(this->SetCookieWithDetails(
322 cm.get(), url_top_level_domain_plus_2, "host_path_1", "X",
323 std::string(), "/dir1", base::Time(), false, false, false,
324 COOKIE_PRIORITY_DEFAULT));
325 EXPECT_TRUE(this->SetCookieWithDetails(
326 cm.get(), url_top_level_domain_plus_2, "host_path_2", "X",
327 std::string(), "/dir1/dir2", base::Time(), false, false, false,
328 COOKIE_PRIORITY_DEFAULT));
330 EXPECT_EQ(14U, this->GetAllCookies(cm.get()).size());
333 Time GetFirstCookieAccessDate(CookieMonster* cm) {
334 const CookieList all_cookies(this->GetAllCookies(cm));
335 return all_cookies.front().LastAccessDate();
338 bool FindAndDeleteCookie(CookieMonster* cm,
339 const std::string& domain,
340 const std::string& name) {
341 CookieList cookies = this->GetAllCookies(cm);
342 for (CookieList::iterator it = cookies.begin(); it != cookies.end(); ++it)
343 if (it->Domain() == domain && it->Name() == name)
344 return this->DeleteCanonicalCookie(cm, *it);
345 return false;
348 int CountInString(const std::string& str, char c) {
349 return std::count(str.begin(), str.end(), c);
352 void TestHostGarbageCollectHelper() {
353 int domain_max_cookies = CookieMonster::kDomainMaxCookies;
354 int domain_purge_cookies = CookieMonster::kDomainPurgeCookies;
355 const int more_than_enough_cookies =
356 (domain_max_cookies + domain_purge_cookies) * 2;
357 // Add a bunch of cookies on a single host, should purge them.
359 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
360 for (int i = 0; i < more_than_enough_cookies; ++i) {
361 std::string cookie = base::StringPrintf("a%03d=b", i);
362 EXPECT_TRUE(SetCookie(cm.get(), url_google_, cookie));
363 std::string cookies = this->GetCookies(cm.get(), url_google_);
364 // Make sure we find it in the cookies.
365 EXPECT_NE(cookies.find(cookie), std::string::npos);
366 // Count the number of cookies.
367 EXPECT_LE(CountInString(cookies, '='), domain_max_cookies);
371 // Add a bunch of cookies on multiple hosts within a single eTLD.
372 // Should keep at least kDomainMaxCookies - kDomainPurgeCookies
373 // between them. We shouldn't go above kDomainMaxCookies for both together.
374 GURL url_google_specific(kUrlGoogleSpecific);
376 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
377 for (int i = 0; i < more_than_enough_cookies; ++i) {
378 std::string cookie_general = base::StringPrintf("a%03d=b", i);
379 EXPECT_TRUE(SetCookie(cm.get(), url_google_, cookie_general));
380 std::string cookie_specific = base::StringPrintf("c%03d=b", i);
381 EXPECT_TRUE(SetCookie(cm.get(), url_google_specific, cookie_specific));
382 std::string cookies_general = this->GetCookies(cm.get(), url_google_);
383 EXPECT_NE(cookies_general.find(cookie_general), std::string::npos);
384 std::string cookies_specific =
385 this->GetCookies(cm.get(), url_google_specific);
386 EXPECT_NE(cookies_specific.find(cookie_specific), std::string::npos);
387 EXPECT_LE((CountInString(cookies_general, '=') +
388 CountInString(cookies_specific, '=')),
389 domain_max_cookies);
391 // After all this, there should be at least
392 // kDomainMaxCookies - kDomainPurgeCookies for both URLs.
393 std::string cookies_general = this->GetCookies(cm.get(), url_google_);
394 std::string cookies_specific =
395 this->GetCookies(cm.get(), url_google_specific);
396 int total_cookies = (CountInString(cookies_general, '=') +
397 CountInString(cookies_specific, '='));
398 EXPECT_GE(total_cookies, domain_max_cookies - domain_purge_cookies);
399 EXPECT_LE(total_cookies, domain_max_cookies);
403 CookiePriority CharToPriority(char ch) {
404 switch (ch) {
405 case 'L':
406 return COOKIE_PRIORITY_LOW;
407 case 'M':
408 return COOKIE_PRIORITY_MEDIUM;
409 case 'H':
410 return COOKIE_PRIORITY_HIGH;
412 NOTREACHED();
413 return COOKIE_PRIORITY_DEFAULT;
416 // Instantiates a CookieMonster, adds multiple cookies (to url_google_) with
417 // priorities specified by |coded_priority_str|, and tests priority-aware
418 // domain cookie eviction.
419 // |coded_priority_str| specifies a run-length-encoded string of priorities.
420 // Example: "2M 3L M 4H" means "MMLLLMHHHH", and speicifies sequential (i.e.,
421 // from least- to most-recently accessed) insertion of 2 medium-priority
422 // cookies, 3 low-priority cookies, 1 medium-priority cookie, and 4
423 // high-priority cookies.
424 // Within each priority, only the least-accessed cookies should be evicted.
425 // Thus, to describe expected suriving cookies, it suffices to specify the
426 // expected population of surviving cookies per priority, i.e.,
427 // |expected_low_count|, |expected_medium_count|, and |expected_high_count|.
428 void TestPriorityCookieCase(CookieMonster* cm,
429 const std::string& coded_priority_str,
430 size_t expected_low_count,
431 size_t expected_medium_count,
432 size_t expected_high_count) {
433 DeleteAll(cm);
434 int next_cookie_id = 0;
435 std::vector<CookiePriority> priority_list;
436 std::vector<int> id_list[3]; // Indexed by CookiePriority.
438 // Parse |coded_priority_str| and add cookies.
439 for (const std::string& token :
440 base::SplitString(coded_priority_str, " ", base::TRIM_WHITESPACE,
441 base::SPLIT_WANT_ALL)) {
442 DCHECK(!token.empty());
443 // Take last character as priority.
444 CookiePriority priority = CharToPriority(token[token.length() - 1]);
445 std::string priority_str = CookiePriorityToString(priority);
446 // The rest of the string (possibly empty) specifies repetition.
447 int rep = 1;
448 if (!token.empty()) {
449 bool result = base::StringToInt(
450 base::StringPiece(token.begin(), token.end() - 1), &rep);
451 DCHECK(result);
453 for (; rep > 0; --rep, ++next_cookie_id) {
454 std::string cookie = base::StringPrintf(
455 "a%d=b;priority=%s", next_cookie_id, priority_str.c_str());
456 EXPECT_TRUE(SetCookie(cm, url_google_, cookie));
457 priority_list.push_back(priority);
458 id_list[priority].push_back(next_cookie_id);
462 int num_cookies = static_cast<int>(priority_list.size());
463 std::vector<int> surviving_id_list[3]; // Indexed by CookiePriority.
465 // Parse the list of cookies
466 std::string cookie_str = this->GetCookies(cm, url_google_);
467 for (const std::string& token : base::SplitString(
468 cookie_str, ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
469 // Assuming *it is "a#=b", so extract and parse "#" portion.
470 int id = -1;
471 bool result = base::StringToInt(
472 base::StringPiece(token.begin() + 1, token.end() - 2), &id);
473 DCHECK(result);
474 DCHECK_GE(id, 0);
475 DCHECK_LT(id, num_cookies);
476 surviving_id_list[priority_list[id]].push_back(id);
479 // Validate each priority.
480 size_t expected_count[3] = {
481 expected_low_count, expected_medium_count, expected_high_count};
482 for (int i = 0; i < 3; ++i) {
483 DCHECK_LE(surviving_id_list[i].size(), id_list[i].size());
484 EXPECT_EQ(expected_count[i], surviving_id_list[i].size());
485 // Verify that the remaining cookies are the most recent among those
486 // with the same priorities.
487 if (expected_count[i] == surviving_id_list[i].size()) {
488 std::sort(surviving_id_list[i].begin(), surviving_id_list[i].end());
489 EXPECT_TRUE(std::equal(surviving_id_list[i].begin(),
490 surviving_id_list[i].end(),
491 id_list[i].end() - expected_count[i]));
496 void TestPriorityAwareGarbageCollectHelper() {
497 // Hard-coding limits in the test, but use DCHECK_EQ to enforce constraint.
498 DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies);
499 DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies -
500 CookieMonster::kDomainPurgeCookies);
501 DCHECK_EQ(30U, CookieMonster::kDomainCookiesQuotaLow);
502 DCHECK_EQ(50U, CookieMonster::kDomainCookiesQuotaMedium);
503 DCHECK_EQ(70U, CookieMonster::kDomainCookiesQuotaHigh);
505 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
507 // Each test case adds 181 cookies, so 31 cookies are evicted.
508 // Cookie same priority, repeated for each priority.
509 TestPriorityCookieCase(cm.get(), "181L", 150U, 0U, 0U);
510 TestPriorityCookieCase(cm.get(), "181M", 0U, 150U, 0U);
511 TestPriorityCookieCase(cm.get(), "181H", 0U, 0U, 150U);
513 // Pairwise scenarios.
514 // Round 1 => none; round2 => 31M; round 3 => none.
515 TestPriorityCookieCase(cm.get(), "10H 171M", 0U, 140U, 10U);
516 // Round 1 => 10L; round2 => 21M; round 3 => none.
517 TestPriorityCookieCase(cm.get(), "141M 40L", 30U, 120U, 0U);
518 // Round 1 => none; round2 => none; round 3 => 31H.
519 TestPriorityCookieCase(cm.get(), "101H 80M", 0U, 80U, 70U);
521 // For {low, medium} priorities right on quota, different orders.
522 // Round 1 => 1L; round 2 => none, round3 => 30L.
523 TestPriorityCookieCase(cm.get(), "31L 50M 100H", 0U, 50U, 100U);
524 // Round 1 => none; round 2 => 1M, round3 => 30M.
525 TestPriorityCookieCase(cm.get(), "51M 100H 30L", 30U, 20U, 100U);
526 // Round 1 => none; round 2 => none; round3 => 31H.
527 TestPriorityCookieCase(cm.get(), "101H 50M 30L", 30U, 50U, 70U);
529 // Round 1 => 10L; round 2 => 10M; round3 => 11H.
530 TestPriorityCookieCase(cm.get(), "81H 60M 40L", 30U, 50U, 70U);
532 // More complex scenarios.
533 // Round 1 => 10L; round 2 => 10M; round 3 => 11H.
534 TestPriorityCookieCase(cm.get(), "21H 60M 40L 60H", 30U, 50U, 70U);
535 // Round 1 => 10L; round 2 => 11M, 10L; round 3 => none.
536 TestPriorityCookieCase(cm.get(), "11H 10M 20L 110M 20L 10H", 20U, 109U,
537 21U);
538 // Round 1 => none; round 2 => none; round 3 => 11L, 10M, 10H.
539 TestPriorityCookieCase(cm.get(), "11L 10M 140H 10M 10L", 10U, 10U, 130U);
540 // Round 1 => none; round 2 => 1M; round 3 => 10L, 10M, 10H.
541 TestPriorityCookieCase(cm.get(), "11M 10H 10L 60M 90H", 0U, 60U, 90U);
542 // Round 1 => none; round 2 => 10L, 21M; round 3 => none.
543 TestPriorityCookieCase(cm.get(), "11M 10H 10L 90M 60H", 0U, 80U, 70U);
546 // Function for creating a CM with a number of cookies in it,
547 // no store (and hence no ability to affect access time).
548 CookieMonster* CreateMonsterForGC(int num_cookies) {
549 CookieMonster* cm(new CookieMonster(NULL, NULL));
550 for (int i = 0; i < num_cookies; i++) {
551 SetCookie(cm, GURL(base::StringPrintf("http://h%05d.izzle", i)), "a=1");
553 return cm;
556 bool IsCookieInList(const CanonicalCookie& cookie, const CookieList& list) {
557 for (CookieList::const_iterator it = list.begin(); it != list.end(); ++it) {
558 if (it->Source() == cookie.Source() && it->Name() == cookie.Name() &&
559 it->Value() == cookie.Value() && it->Domain() == cookie.Domain() &&
560 it->Path() == cookie.Path() &&
561 it->CreationDate() == cookie.CreationDate() &&
562 it->ExpiryDate() == cookie.ExpiryDate() &&
563 it->LastAccessDate() == cookie.LastAccessDate() &&
564 it->IsSecure() == cookie.IsSecure() &&
565 it->IsHttpOnly() == cookie.IsHttpOnly() &&
566 it->Priority() == cookie.Priority()) {
567 return true;
571 return false;
575 // TODO(erikwright): Replace the other callbacks and synchronous helper methods
576 // in this test suite with these Mocks.
577 template <typename T, typename C>
578 class MockCookieCallback {
579 public:
580 C AsCallback() {
581 return base::Bind(&T::Invoke, base::Unretained(static_cast<T*>(this)));
585 class MockGetCookiesCallback
586 : public MockCookieCallback<MockGetCookiesCallback,
587 CookieStore::GetCookiesCallback> {
588 public:
589 MOCK_METHOD1(Invoke, void(const std::string& cookies));
592 class MockSetCookiesCallback
593 : public MockCookieCallback<MockSetCookiesCallback,
594 CookieStore::SetCookiesCallback> {
595 public:
596 MOCK_METHOD1(Invoke, void(bool success));
599 class MockClosure : public MockCookieCallback<MockClosure, base::Closure> {
600 public:
601 MOCK_METHOD0(Invoke, void(void));
604 class MockGetCookieListCallback
605 : public MockCookieCallback<MockGetCookieListCallback,
606 CookieMonster::GetCookieListCallback> {
607 public:
608 MOCK_METHOD1(Invoke, void(const CookieList& cookies));
611 class MockDeleteCallback
612 : public MockCookieCallback<MockDeleteCallback,
613 CookieMonster::DeleteCallback> {
614 public:
615 MOCK_METHOD1(Invoke, void(int num_deleted));
618 class MockDeleteCookieCallback
619 : public MockCookieCallback<MockDeleteCookieCallback,
620 CookieMonster::DeleteCookieCallback> {
621 public:
622 MOCK_METHOD1(Invoke, void(bool success));
625 struct CookiesInputInfo {
626 const GURL url;
627 const std::string name;
628 const std::string value;
629 const std::string domain;
630 const std::string path;
631 const base::Time expiration_time;
632 bool secure;
633 bool http_only;
634 bool first_party_only;
635 CookiePriority priority;
638 ACTION(QuitCurrentMessageLoop) {
639 base::ThreadTaskRunnerHandle::Get()->PostTask(
640 FROM_HERE, base::MessageLoop::QuitClosure());
643 // TODO(erikwright): When the synchronous helpers 'GetCookies' etc. are removed,
644 // rename these, removing the 'Action' suffix.
645 ACTION_P4(DeleteCookieAction, cookie_monster, url, name, callback) {
646 cookie_monster->DeleteCookieAsync(url, name, callback->AsCallback());
648 ACTION_P3(GetCookiesAction, cookie_monster, url, callback) {
649 cookie_monster->GetCookiesWithOptionsAsync(url, CookieOptions(),
650 callback->AsCallback());
652 ACTION_P4(SetCookieAction, cookie_monster, url, cookie_line, callback) {
653 cookie_monster->SetCookieWithOptionsAsync(url, cookie_line, CookieOptions(),
654 callback->AsCallback());
656 ACTION_P3(SetAllCookiesAction, cookie_monster, list, callback) {
657 cookie_monster->SetAllCookiesAsync(list, callback->AsCallback());
659 ACTION_P4(DeleteAllCreatedBetweenAction,
660 cookie_monster,
661 delete_begin,
662 delete_end,
663 callback) {
664 cookie_monster->DeleteAllCreatedBetweenAsync(delete_begin, delete_end,
665 callback->AsCallback());
667 ACTION_P3(SetCookieWithDetailsAction, cookie_monster, cc, callback) {
668 cookie_monster->SetCookieWithDetailsAsync(
669 cc.url, cc.name, cc.value, cc.domain, cc.path, cc.expiration_time,
670 cc.secure, cc.http_only, cc.first_party_only, cc.priority,
671 callback->AsCallback());
674 ACTION_P2(GetAllCookiesAction, cookie_monster, callback) {
675 cookie_monster->GetAllCookiesAsync(callback->AsCallback());
678 ACTION_P3(DeleteAllForHostAction, cookie_monster, url, callback) {
679 cookie_monster->DeleteAllForHostAsync(url, callback->AsCallback());
682 ACTION_P3(DeleteCanonicalCookieAction, cookie_monster, cookie, callback) {
683 cookie_monster->DeleteCanonicalCookieAsync(cookie, callback->AsCallback());
686 ACTION_P2(DeleteAllAction, cookie_monster, callback) {
687 cookie_monster->DeleteAllAsync(callback->AsCallback());
690 ACTION_P3(GetAllCookiesForUrlWithOptionsAction, cookie_monster, url, callback) {
691 cookie_monster->GetAllCookiesForURLWithOptionsAsync(url, CookieOptions(),
692 callback->AsCallback());
695 ACTION_P3(GetAllCookiesForUrlAction, cookie_monster, url, callback) {
696 cookie_monster->GetAllCookiesForURLAsync(url, callback->AsCallback());
699 ACTION_P(PushCallbackAction, callback_vector) {
700 callback_vector->push(arg1);
703 ACTION_P2(DeleteSessionCookiesAction, cookie_monster, callback) {
704 cookie_monster->DeleteSessionCookiesAsync(callback->AsCallback());
707 } // namespace
709 // This test suite verifies the task deferral behaviour of the CookieMonster.
710 // Specifically, for each asynchronous method, verify that:
711 // 1. invoking it on an uninitialized cookie store causes the store to begin
712 // chain-loading its backing data or loading data for a specific domain key
713 // (eTLD+1).
714 // 2. The initial invocation does not complete until the loading completes.
715 // 3. Invocations after the loading has completed complete immediately.
716 class DeferredCookieTaskTest : public CookieMonsterTest {
717 protected:
718 DeferredCookieTaskTest() {
719 persistent_store_ = new NewMockPersistentCookieStore();
720 cookie_monster_ = new CookieMonster(persistent_store_.get(), NULL);
723 // Defines a cookie to be returned from PersistentCookieStore::Load
724 void DeclareLoadedCookie(const std::string& key,
725 const std::string& cookie_line,
726 const base::Time& creation_time) {
727 AddCookieToList(key, cookie_line, creation_time, &loaded_cookies_);
730 // Runs the message loop, waiting until PersistentCookieStore::Load is called.
731 // Call CompleteLoadingAndWait to cause the load to complete.
732 void WaitForLoadCall() {
733 RunFor(kTimeout);
735 // Verify that PeristentStore::Load was called.
736 testing::Mock::VerifyAndClear(persistent_store_.get());
739 // Invokes the PersistentCookieStore::LoadCookiesForKey completion callbacks
740 // and PersistentCookieStore::Load completion callback and waits
741 // until the message loop is quit.
742 void CompleteLoadingAndWait() {
743 while (!loaded_for_key_callbacks_.empty()) {
744 loaded_for_key_callbacks_.front().Run(loaded_cookies_);
745 loaded_cookies_.clear();
746 loaded_for_key_callbacks_.pop();
749 loaded_callback_.Run(loaded_cookies_);
750 RunFor(kTimeout);
753 // Performs the provided action, expecting it to cause a call to
754 // PersistentCookieStore::Load. Call WaitForLoadCall to verify the load call
755 // is received.
756 void BeginWith(testing::Action<void(void)> action) {
757 EXPECT_CALL(*this, Begin()).WillOnce(action);
758 ExpectLoadCall();
759 Begin();
762 void BeginWithForDomainKey(std::string key,
763 testing::Action<void(void)> action) {
764 EXPECT_CALL(*this, Begin()).WillOnce(action);
765 ExpectLoadCall();
766 ExpectLoadForKeyCall(key, false);
767 Begin();
770 // Declares an expectation that PersistentCookieStore::Load will be called,
771 // saving the provided callback and sending a quit to the message loop.
772 void ExpectLoadCall() {
773 EXPECT_CALL(*persistent_store_.get(), Load(testing::_))
774 .WillOnce(testing::DoAll(testing::SaveArg<0>(&loaded_callback_),
775 QuitCurrentMessageLoop()));
778 // Declares an expectation that PersistentCookieStore::LoadCookiesForKey
779 // will be called, saving the provided callback and sending a quit to the
780 // message loop.
781 void ExpectLoadForKeyCall(std::string key, bool quit_queue) {
782 if (quit_queue)
783 EXPECT_CALL(*persistent_store_.get(), LoadCookiesForKey(key, testing::_))
784 .WillOnce(
785 testing::DoAll(PushCallbackAction(&loaded_for_key_callbacks_),
786 QuitCurrentMessageLoop()));
787 else
788 EXPECT_CALL(*persistent_store_.get(), LoadCookiesForKey(key, testing::_))
789 .WillOnce(PushCallbackAction(&loaded_for_key_callbacks_));
792 // Invokes the initial action.
793 MOCK_METHOD0(Begin, void(void));
795 // Returns the CookieMonster instance under test.
796 CookieMonster& cookie_monster() { return *cookie_monster_.get(); }
798 private:
799 // Declares that mock expectations in this test suite are strictly ordered.
800 testing::InSequence in_sequence_;
801 // Holds cookies to be returned from PersistentCookieStore::Load or
802 // PersistentCookieStore::LoadCookiesForKey.
803 std::vector<CanonicalCookie*> loaded_cookies_;
804 // Stores the callback passed from the CookieMonster to the
805 // PersistentCookieStore::Load
806 CookieMonster::PersistentCookieStore::LoadedCallback loaded_callback_;
807 // Stores the callback passed from the CookieMonster to the
808 // PersistentCookieStore::LoadCookiesForKey
809 std::queue<CookieMonster::PersistentCookieStore::LoadedCallback>
810 loaded_for_key_callbacks_;
812 // Stores the CookieMonster under test.
813 scoped_refptr<CookieMonster> cookie_monster_;
814 // Stores the mock PersistentCookieStore.
815 scoped_refptr<NewMockPersistentCookieStore> persistent_store_;
818 TEST_F(DeferredCookieTaskTest, DeferredGetCookies) {
819 DeclareLoadedCookie("www.google.izzle",
820 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
821 Time::Now() + TimeDelta::FromDays(3));
823 MockGetCookiesCallback get_cookies_callback;
825 BeginWithForDomainKey(
826 "google.izzle",
827 GetCookiesAction(&cookie_monster(), url_google_, &get_cookies_callback));
829 WaitForLoadCall();
831 EXPECT_CALL(get_cookies_callback, Invoke("X=1"))
832 .WillOnce(GetCookiesAction(&cookie_monster(), url_google_,
833 &get_cookies_callback));
834 EXPECT_CALL(get_cookies_callback, Invoke("X=1"))
835 .WillOnce(QuitCurrentMessageLoop());
837 CompleteLoadingAndWait();
840 TEST_F(DeferredCookieTaskTest, DeferredSetCookie) {
841 MockSetCookiesCallback set_cookies_callback;
843 BeginWithForDomainKey("google.izzle",
844 SetCookieAction(&cookie_monster(), url_google_, "A=B",
845 &set_cookies_callback));
847 WaitForLoadCall();
849 EXPECT_CALL(set_cookies_callback, Invoke(true))
850 .WillOnce(SetCookieAction(&cookie_monster(), url_google_, "X=Y",
851 &set_cookies_callback));
852 EXPECT_CALL(set_cookies_callback, Invoke(true))
853 .WillOnce(QuitCurrentMessageLoop());
855 CompleteLoadingAndWait();
858 TEST_F(DeferredCookieTaskTest, DeferredSetAllCookies) {
859 MockSetCookiesCallback set_cookies_callback;
860 CookieList list;
861 list.push_back(CanonicalCookie(url_google_, "A", "B", "google.izzle", "/",
862 base::Time::Now(), base::Time(), base::Time(),
863 false, true, false, COOKIE_PRIORITY_DEFAULT));
864 list.push_back(CanonicalCookie(url_google_, "C", "D", "google.izzle", "/",
865 base::Time::Now(), base::Time(), base::Time(),
866 false, true, false, COOKIE_PRIORITY_DEFAULT));
868 BeginWith(
869 SetAllCookiesAction(&cookie_monster(), list, &set_cookies_callback));
871 WaitForLoadCall();
873 EXPECT_CALL(set_cookies_callback, Invoke(true))
874 .WillOnce(
875 SetAllCookiesAction(&cookie_monster(), list, &set_cookies_callback));
876 EXPECT_CALL(set_cookies_callback, Invoke(true))
877 .WillOnce(QuitCurrentMessageLoop());
879 CompleteLoadingAndWait();
882 TEST_F(DeferredCookieTaskTest, DeferredDeleteCookie) {
883 MockClosure delete_cookie_callback;
885 BeginWithForDomainKey("google.izzle",
886 DeleteCookieAction(&cookie_monster(), url_google_, "A",
887 &delete_cookie_callback));
889 WaitForLoadCall();
891 EXPECT_CALL(delete_cookie_callback, Invoke())
892 .WillOnce(DeleteCookieAction(&cookie_monster(), url_google_, "X",
893 &delete_cookie_callback));
894 EXPECT_CALL(delete_cookie_callback, Invoke())
895 .WillOnce(QuitCurrentMessageLoop());
897 CompleteLoadingAndWait();
900 TEST_F(DeferredCookieTaskTest, DeferredSetCookieWithDetails) {
901 MockSetCookiesCallback set_cookies_callback;
903 CookiesInputInfo cookie_info = {url_google_foo_,
904 "A",
905 "B",
906 std::string(),
907 "/foo",
908 base::Time(),
909 false,
910 false,
911 false,
912 COOKIE_PRIORITY_DEFAULT};
913 BeginWithForDomainKey(
914 "google.izzle", SetCookieWithDetailsAction(&cookie_monster(), cookie_info,
915 &set_cookies_callback));
917 WaitForLoadCall();
919 CookiesInputInfo cookie_info_exp = {url_google_foo_,
920 "A",
921 "B",
922 std::string(),
923 "/foo",
924 base::Time(),
925 false,
926 false,
927 false,
928 COOKIE_PRIORITY_DEFAULT};
929 EXPECT_CALL(set_cookies_callback, Invoke(true))
930 .WillOnce(SetCookieWithDetailsAction(&cookie_monster(), cookie_info_exp,
931 &set_cookies_callback));
932 EXPECT_CALL(set_cookies_callback, Invoke(true))
933 .WillOnce(QuitCurrentMessageLoop());
935 CompleteLoadingAndWait();
938 TEST_F(DeferredCookieTaskTest, DeferredGetAllCookies) {
939 DeclareLoadedCookie("www.google.izzle",
940 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
941 Time::Now() + TimeDelta::FromDays(3));
943 MockGetCookieListCallback get_cookie_list_callback;
945 BeginWith(GetAllCookiesAction(&cookie_monster(), &get_cookie_list_callback));
947 WaitForLoadCall();
949 EXPECT_CALL(get_cookie_list_callback, Invoke(testing::_))
950 .WillOnce(
951 GetAllCookiesAction(&cookie_monster(), &get_cookie_list_callback));
952 EXPECT_CALL(get_cookie_list_callback, Invoke(testing::_))
953 .WillOnce(QuitCurrentMessageLoop());
955 CompleteLoadingAndWait();
958 TEST_F(DeferredCookieTaskTest, DeferredGetAllForUrlCookies) {
959 DeclareLoadedCookie("www.google.izzle",
960 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
961 Time::Now() + TimeDelta::FromDays(3));
963 MockGetCookieListCallback get_cookie_list_callback;
965 BeginWithForDomainKey(
966 "google.izzle", GetAllCookiesForUrlAction(&cookie_monster(), url_google_,
967 &get_cookie_list_callback));
969 WaitForLoadCall();
971 EXPECT_CALL(get_cookie_list_callback, Invoke(testing::_))
972 .WillOnce(GetAllCookiesForUrlAction(&cookie_monster(), url_google_,
973 &get_cookie_list_callback));
974 EXPECT_CALL(get_cookie_list_callback, Invoke(testing::_))
975 .WillOnce(QuitCurrentMessageLoop());
977 CompleteLoadingAndWait();
980 TEST_F(DeferredCookieTaskTest, DeferredGetAllForUrlWithOptionsCookies) {
981 DeclareLoadedCookie("www.google.izzle",
982 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
983 Time::Now() + TimeDelta::FromDays(3));
985 MockGetCookieListCallback get_cookie_list_callback;
987 BeginWithForDomainKey("google.izzle", GetAllCookiesForUrlWithOptionsAction(
988 &cookie_monster(), url_google_,
989 &get_cookie_list_callback));
991 WaitForLoadCall();
993 EXPECT_CALL(get_cookie_list_callback, Invoke(testing::_))
994 .WillOnce(GetAllCookiesForUrlWithOptionsAction(
995 &cookie_monster(), url_google_, &get_cookie_list_callback));
996 EXPECT_CALL(get_cookie_list_callback, Invoke(testing::_))
997 .WillOnce(QuitCurrentMessageLoop());
999 CompleteLoadingAndWait();
1002 TEST_F(DeferredCookieTaskTest, DeferredDeleteAllCookies) {
1003 MockDeleteCallback delete_callback;
1005 BeginWith(DeleteAllAction(&cookie_monster(), &delete_callback));
1007 WaitForLoadCall();
1009 EXPECT_CALL(delete_callback, Invoke(false))
1010 .WillOnce(DeleteAllAction(&cookie_monster(), &delete_callback));
1011 EXPECT_CALL(delete_callback, Invoke(false))
1012 .WillOnce(QuitCurrentMessageLoop());
1014 CompleteLoadingAndWait();
1017 TEST_F(DeferredCookieTaskTest, DeferredDeleteAllCreatedBetweenCookies) {
1018 MockDeleteCallback delete_callback;
1020 BeginWith(DeleteAllCreatedBetweenAction(&cookie_monster(), base::Time(),
1021 base::Time::Now(), &delete_callback));
1023 WaitForLoadCall();
1025 EXPECT_CALL(delete_callback, Invoke(false))
1026 .WillOnce(DeleteAllCreatedBetweenAction(&cookie_monster(), base::Time(),
1027 base::Time::Now(),
1028 &delete_callback));
1029 EXPECT_CALL(delete_callback, Invoke(false))
1030 .WillOnce(QuitCurrentMessageLoop());
1032 CompleteLoadingAndWait();
1035 TEST_F(DeferredCookieTaskTest, DeferredDeleteAllForHostCookies) {
1036 MockDeleteCallback delete_callback;
1038 BeginWithForDomainKey(
1039 "google.izzle",
1040 DeleteAllForHostAction(&cookie_monster(), url_google_, &delete_callback));
1042 WaitForLoadCall();
1044 EXPECT_CALL(delete_callback, Invoke(false))
1045 .WillOnce(DeleteAllForHostAction(&cookie_monster(), url_google_,
1046 &delete_callback));
1047 EXPECT_CALL(delete_callback, Invoke(false))
1048 .WillOnce(QuitCurrentMessageLoop());
1050 CompleteLoadingAndWait();
1053 TEST_F(DeferredCookieTaskTest, DeferredDeleteCanonicalCookie) {
1054 std::vector<CanonicalCookie*> cookies;
1055 CanonicalCookie cookie =
1056 BuildCanonicalCookie("www.google.com", "X=1; path=/", base::Time::Now());
1058 MockDeleteCookieCallback delete_cookie_callback;
1060 BeginWith(DeleteCanonicalCookieAction(&cookie_monster(), cookie,
1061 &delete_cookie_callback));
1063 WaitForLoadCall();
1065 EXPECT_CALL(delete_cookie_callback, Invoke(false))
1066 .WillOnce(DeleteCanonicalCookieAction(&cookie_monster(), cookie,
1067 &delete_cookie_callback));
1068 EXPECT_CALL(delete_cookie_callback, Invoke(false))
1069 .WillOnce(QuitCurrentMessageLoop());
1071 CompleteLoadingAndWait();
1074 TEST_F(DeferredCookieTaskTest, DeferredDeleteSessionCookies) {
1075 MockDeleteCallback delete_callback;
1077 BeginWith(DeleteSessionCookiesAction(&cookie_monster(), &delete_callback));
1079 WaitForLoadCall();
1081 EXPECT_CALL(delete_callback, Invoke(false))
1082 .WillOnce(
1083 DeleteSessionCookiesAction(&cookie_monster(), &delete_callback));
1084 EXPECT_CALL(delete_callback, Invoke(false))
1085 .WillOnce(QuitCurrentMessageLoop());
1087 CompleteLoadingAndWait();
1090 // Verify that a series of queued tasks are executed in order upon loading of
1091 // the backing store and that new tasks received while the queued tasks are
1092 // being dispatched go to the end of the queue.
1093 TEST_F(DeferredCookieTaskTest, DeferredTaskOrder) {
1094 DeclareLoadedCookie("www.google.izzle",
1095 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
1096 Time::Now() + TimeDelta::FromDays(3));
1098 MockGetCookiesCallback get_cookies_callback;
1099 MockSetCookiesCallback set_cookies_callback;
1100 MockGetCookiesCallback get_cookies_callback_deferred;
1102 EXPECT_CALL(*this, Begin())
1103 .WillOnce(testing::DoAll(GetCookiesAction(&cookie_monster(), url_google_,
1104 &get_cookies_callback),
1105 SetCookieAction(&cookie_monster(), url_google_,
1106 "A=B", &set_cookies_callback)));
1107 ExpectLoadCall();
1108 ExpectLoadForKeyCall("google.izzle", false);
1109 Begin();
1111 WaitForLoadCall();
1112 EXPECT_CALL(get_cookies_callback, Invoke("X=1"))
1113 .WillOnce(GetCookiesAction(&cookie_monster(), url_google_,
1114 &get_cookies_callback_deferred));
1115 EXPECT_CALL(set_cookies_callback, Invoke(true));
1116 EXPECT_CALL(get_cookies_callback_deferred, Invoke("A=B; X=1"))
1117 .WillOnce(QuitCurrentMessageLoop());
1119 CompleteLoadingAndWait();
1122 TEST_F(CookieMonsterTest, TestCookieDeleteAll) {
1123 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore);
1124 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL));
1125 CookieOptions options;
1126 options.set_include_httponly();
1128 EXPECT_TRUE(SetCookie(cm.get(), url_google_, kValidCookieLine));
1129 EXPECT_EQ("A=B", GetCookies(cm.get(), url_google_));
1131 EXPECT_TRUE(
1132 SetCookieWithOptions(cm.get(), url_google_, "C=D; httponly", options));
1133 EXPECT_EQ("A=B; C=D", GetCookiesWithOptions(cm.get(), url_google_, options));
1135 EXPECT_EQ(2, DeleteAll(cm.get()));
1136 EXPECT_EQ("", GetCookiesWithOptions(cm.get(), url_google_, options));
1137 EXPECT_EQ(0u, store->commands().size());
1139 // Create a persistent cookie.
1140 EXPECT_TRUE(SetCookie(
1141 cm.get(), url_google_,
1142 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-22 22:50:13 GMT"));
1143 ASSERT_EQ(1u, store->commands().size());
1144 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type);
1146 EXPECT_EQ(1, DeleteAll(cm.get())); // sync_to_store = true.
1147 ASSERT_EQ(2u, store->commands().size());
1148 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type);
1150 EXPECT_EQ("", GetCookiesWithOptions(cm.get(), url_google_, options));
1153 TEST_F(CookieMonsterTest, TestCookieDeleteAllCreatedBetweenTimestamps) {
1154 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1155 Time now = Time::Now();
1157 // Nothing has been added so nothing should be deleted.
1158 EXPECT_EQ(0, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(99),
1159 Time()));
1161 // Create 3 cookies with creation date of today, yesterday and the day before.
1162 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-0=Now", now));
1163 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-1=Yesterday",
1164 now - TimeDelta::FromDays(1)));
1165 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-2=DayBefore",
1166 now - TimeDelta::FromDays(2)));
1167 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-3=ThreeDays",
1168 now - TimeDelta::FromDays(3)));
1169 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-7=LastWeek",
1170 now - TimeDelta::FromDays(7)));
1172 // Try to delete threedays and the daybefore.
1173 EXPECT_EQ(2, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(3),
1174 now - TimeDelta::FromDays(1)));
1176 // Try to delete yesterday, also make sure that delete_end is not
1177 // inclusive.
1178 EXPECT_EQ(
1179 1, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(2), now));
1181 // Make sure the delete_begin is inclusive.
1182 EXPECT_EQ(
1183 1, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(7), now));
1185 // Delete the last (now) item.
1186 EXPECT_EQ(1, DeleteAllCreatedBetween(cm.get(), Time(), Time()));
1188 // Really make sure everything is gone.
1189 EXPECT_EQ(0, DeleteAll(cm.get()));
1192 static const int kAccessDelayMs = kLastAccessThresholdMilliseconds + 20;
1194 TEST_F(CookieMonsterTest, TestLastAccess) {
1195 scoped_refptr<CookieMonster> cm(
1196 new CookieMonster(NULL, NULL, kLastAccessThresholdMilliseconds));
1198 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "A=B"));
1199 const Time last_access_date(GetFirstCookieAccessDate(cm.get()));
1201 // Reading the cookie again immediately shouldn't update the access date,
1202 // since we're inside the threshold.
1203 EXPECT_EQ("A=B", GetCookies(cm.get(), url_google_));
1204 EXPECT_TRUE(last_access_date == GetFirstCookieAccessDate(cm.get()));
1206 // Reading after a short wait should update the access date.
1207 base::PlatformThread::Sleep(
1208 base::TimeDelta::FromMilliseconds(kAccessDelayMs));
1209 EXPECT_EQ("A=B", GetCookies(cm.get(), url_google_));
1210 EXPECT_FALSE(last_access_date == GetFirstCookieAccessDate(cm.get()));
1213 TEST_F(CookieMonsterTest, TestHostGarbageCollection) {
1214 TestHostGarbageCollectHelper();
1217 TEST_F(CookieMonsterTest, TestPriorityAwareGarbageCollection) {
1218 TestPriorityAwareGarbageCollectHelper();
1221 TEST_F(CookieMonsterTest, TestDeleteSingleCookie) {
1222 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1224 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "A=B"));
1225 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "C=D"));
1226 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "E=F"));
1227 EXPECT_EQ("A=B; C=D; E=F", GetCookies(cm.get(), url_google_));
1229 EXPECT_TRUE(FindAndDeleteCookie(cm.get(), url_google_.host(), "C"));
1230 EXPECT_EQ("A=B; E=F", GetCookies(cm.get(), url_google_));
1232 EXPECT_FALSE(FindAndDeleteCookie(cm.get(), "random.host", "E"));
1233 EXPECT_EQ("A=B; E=F", GetCookies(cm.get(), url_google_));
1236 TEST_F(CookieMonsterTest, SetCookieableSchemes) {
1237 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1238 scoped_refptr<CookieMonster> cm_foo(new CookieMonster(NULL, NULL));
1240 // Only cm_foo should allow foo:// cookies.
1241 const char* const kSchemes[] = {"foo"};
1242 cm_foo->SetCookieableSchemes(kSchemes, 1);
1244 GURL foo_url("foo://host/path");
1245 GURL http_url("http://host/path");
1247 EXPECT_TRUE(SetCookie(cm.get(), http_url, "x=1"));
1248 EXPECT_FALSE(SetCookie(cm.get(), foo_url, "x=1"));
1249 EXPECT_TRUE(SetCookie(cm_foo.get(), foo_url, "x=1"));
1250 EXPECT_FALSE(SetCookie(cm_foo.get(), http_url, "x=1"));
1253 TEST_F(CookieMonsterTest, GetAllCookiesForURL) {
1254 scoped_refptr<CookieMonster> cm(
1255 new CookieMonster(NULL, NULL, kLastAccessThresholdMilliseconds));
1257 // Create an httponly cookie.
1258 CookieOptions options;
1259 options.set_include_httponly();
1261 EXPECT_TRUE(
1262 SetCookieWithOptions(cm.get(), url_google_, "A=B; httponly", options));
1263 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_,
1264 "C=D; domain=.google.izzle", options));
1265 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_secure_,
1266 "E=F; domain=.google.izzle; secure",
1267 options));
1269 const Time last_access_date(GetFirstCookieAccessDate(cm.get()));
1271 base::PlatformThread::Sleep(
1272 base::TimeDelta::FromMilliseconds(kAccessDelayMs));
1274 // Check cookies for url.
1275 CookieList cookies = GetAllCookiesForURL(cm.get(), url_google_);
1276 CookieList::iterator it = cookies.begin();
1278 ASSERT_TRUE(it != cookies.end());
1279 EXPECT_EQ("www.google.izzle", it->Domain());
1280 EXPECT_EQ("A", it->Name());
1282 ASSERT_TRUE(++it != cookies.end());
1283 EXPECT_EQ(".google.izzle", it->Domain());
1284 EXPECT_EQ("C", it->Name());
1286 ASSERT_TRUE(++it == cookies.end());
1288 // Check cookies for url excluding http-only cookies.
1289 cookies =
1290 GetAllCookiesForURLWithOptions(cm.get(), url_google_, CookieOptions());
1291 it = cookies.begin();
1293 ASSERT_TRUE(it != cookies.end());
1294 EXPECT_EQ(".google.izzle", it->Domain());
1295 EXPECT_EQ("C", it->Name());
1297 ASSERT_TRUE(++it == cookies.end());
1299 // Test secure cookies.
1300 cookies = GetAllCookiesForURL(cm.get(), url_google_secure_);
1301 it = cookies.begin();
1303 ASSERT_TRUE(it != cookies.end());
1304 EXPECT_EQ("www.google.izzle", it->Domain());
1305 EXPECT_EQ("A", it->Name());
1307 ASSERT_TRUE(++it != cookies.end());
1308 EXPECT_EQ(".google.izzle", it->Domain());
1309 EXPECT_EQ("C", it->Name());
1311 ASSERT_TRUE(++it != cookies.end());
1312 EXPECT_EQ(".google.izzle", it->Domain());
1313 EXPECT_EQ("E", it->Name());
1315 ASSERT_TRUE(++it == cookies.end());
1317 // Reading after a short wait should not update the access date.
1318 EXPECT_TRUE(last_access_date == GetFirstCookieAccessDate(cm.get()));
1321 TEST_F(CookieMonsterTest, GetAllCookiesForURLPathMatching) {
1322 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1323 CookieOptions options;
1325 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_foo_, "A=B; path=/foo;",
1326 options));
1327 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_bar_, "C=D; path=/bar;",
1328 options));
1329 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_, "E=F;", options));
1331 CookieList cookies = GetAllCookiesForURL(cm.get(), url_google_foo_);
1332 CookieList::iterator it = cookies.begin();
1334 ASSERT_TRUE(it != cookies.end());
1335 EXPECT_EQ("A", it->Name());
1336 EXPECT_EQ("/foo", it->Path());
1338 ASSERT_TRUE(++it != cookies.end());
1339 EXPECT_EQ("E", it->Name());
1340 EXPECT_EQ("/", it->Path());
1342 ASSERT_TRUE(++it == cookies.end());
1344 cookies = GetAllCookiesForURL(cm.get(), url_google_bar_);
1345 it = cookies.begin();
1347 ASSERT_TRUE(it != cookies.end());
1348 EXPECT_EQ("C", it->Name());
1349 EXPECT_EQ("/bar", it->Path());
1351 ASSERT_TRUE(++it != cookies.end());
1352 EXPECT_EQ("E", it->Name());
1353 EXPECT_EQ("/", it->Path());
1355 ASSERT_TRUE(++it == cookies.end());
1358 TEST_F(CookieMonsterTest, DeleteCookieByName) {
1359 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1361 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "A=A1; path=/"));
1362 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "A=A2; path=/foo"));
1363 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "A=A3; path=/bar"));
1364 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "B=B1; path=/"));
1365 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "B=B2; path=/foo"));
1366 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "B=B3; path=/bar"));
1368 DeleteCookie(cm.get(), GURL(std::string(kUrlGoogle) + "/foo/bar"), "A");
1370 CookieList cookies = GetAllCookies(cm.get());
1371 size_t expected_size = 4;
1372 EXPECT_EQ(expected_size, cookies.size());
1373 for (CookieList::iterator it = cookies.begin(); it != cookies.end(); ++it) {
1374 EXPECT_NE("A1", it->Value());
1375 EXPECT_NE("A2", it->Value());
1379 TEST_F(CookieMonsterTest, ImportCookiesFromCookieMonster) {
1380 scoped_refptr<CookieMonster> cm_1(new CookieMonster(NULL, NULL));
1381 CookieOptions options;
1383 EXPECT_TRUE(SetCookieWithOptions(cm_1.get(), url_google_foo_,
1384 "A1=B; path=/foo;", options));
1385 EXPECT_TRUE(SetCookieWithOptions(cm_1.get(), url_google_bar_,
1386 "A2=D; path=/bar;", options));
1387 EXPECT_TRUE(SetCookieWithOptions(cm_1.get(), url_google_, "A3=F;", options));
1389 CookieList cookies_1 = GetAllCookies(cm_1.get());
1390 scoped_refptr<CookieMonster> cm_2(new CookieMonster(NULL, NULL));
1391 ASSERT_TRUE(cm_2->ImportCookies(cookies_1));
1392 CookieList cookies_2 = GetAllCookies(cm_2.get());
1394 size_t expected_size = 3;
1395 EXPECT_EQ(expected_size, cookies_2.size());
1397 CookieList::iterator it = cookies_2.begin();
1399 ASSERT_TRUE(it != cookies_2.end());
1400 EXPECT_EQ("A1", it->Name());
1401 EXPECT_EQ("/foo", it->Path());
1403 ASSERT_TRUE(++it != cookies_2.end());
1404 EXPECT_EQ("A2", it->Name());
1405 EXPECT_EQ("/bar", it->Path());
1407 ASSERT_TRUE(++it != cookies_2.end());
1408 EXPECT_EQ("A3", it->Name());
1409 EXPECT_EQ("/", it->Path());
1412 // Tests importing from a persistent cookie store that contains duplicate
1413 // equivalent cookies. This situation should be handled by removing the
1414 // duplicate cookie (both from the in-memory cache, and from the backing store).
1416 // This is a regression test for: http://crbug.com/17855.
1417 TEST_F(CookieMonsterTest, DontImportDuplicateCookies) {
1418 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore);
1420 // We will fill some initial cookies into the PersistentCookieStore,
1421 // to simulate a database with 4 duplicates. Note that we need to
1422 // be careful not to have any duplicate creation times at all (as it's a
1423 // violation of a CookieMonster invariant) even if Time::Now() doesn't
1424 // move between calls.
1425 std::vector<CanonicalCookie*> initial_cookies;
1427 // Insert 4 cookies with name "X" on path "/", with varying creation
1428 // dates. We expect only the most recent one to be preserved following
1429 // the import.
1431 AddCookieToList("www.google.com",
1432 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
1433 Time::Now() + TimeDelta::FromDays(3), &initial_cookies);
1435 AddCookieToList("www.google.com",
1436 "X=2; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
1437 Time::Now() + TimeDelta::FromDays(1), &initial_cookies);
1439 // ===> This one is the WINNER (biggest creation time). <====
1440 AddCookieToList("www.google.com",
1441 "X=3; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
1442 Time::Now() + TimeDelta::FromDays(4), &initial_cookies);
1444 AddCookieToList("www.google.com",
1445 "X=4; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
1446 Time::Now(), &initial_cookies);
1448 // Insert 2 cookies with name "X" on path "/2", with varying creation
1449 // dates. We expect only the most recent one to be preserved the import.
1451 // ===> This one is the WINNER (biggest creation time). <====
1452 AddCookieToList("www.google.com",
1453 "X=a1; path=/2; expires=Mon, 18-Apr-22 22:50:14 GMT",
1454 Time::Now() + TimeDelta::FromDays(9), &initial_cookies);
1456 AddCookieToList("www.google.com",
1457 "X=a2; path=/2; expires=Mon, 18-Apr-22 22:50:14 GMT",
1458 Time::Now() + TimeDelta::FromDays(2), &initial_cookies);
1460 // Insert 1 cookie with name "Y" on path "/".
1461 AddCookieToList("www.google.com",
1462 "Y=a; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
1463 Time::Now() + TimeDelta::FromDays(10), &initial_cookies);
1465 // Inject our initial cookies into the mock PersistentCookieStore.
1466 store->SetLoadExpectation(true, initial_cookies);
1468 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL));
1470 // Verify that duplicates were not imported for path "/".
1471 // (If this had failed, GetCookies() would have also returned X=1, X=2, X=4).
1472 EXPECT_EQ("X=3; Y=a", GetCookies(cm.get(), GURL("http://www.google.com/")));
1474 // Verify that same-named cookie on a different path ("/x2") didn't get
1475 // messed up.
1476 EXPECT_EQ("X=a1; X=3; Y=a",
1477 GetCookies(cm.get(), GURL("http://www.google.com/2/x")));
1479 // Verify that the PersistentCookieStore was told to kill its 4 duplicates.
1480 ASSERT_EQ(4u, store->commands().size());
1481 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[0].type);
1482 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type);
1483 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[2].type);
1484 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[3].type);
1487 // Tests importing from a persistent cookie store that contains cookies
1488 // with duplicate creation times. This situation should be handled by
1489 // dropping the cookies before insertion/visibility to user.
1491 // This is a regression test for: http://crbug.com/43188.
1492 TEST_F(CookieMonsterTest, DontImportDuplicateCreationTimes) {
1493 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore);
1495 Time now(Time::Now());
1496 Time earlier(now - TimeDelta::FromDays(1));
1498 // Insert 8 cookies, four with the current time as creation times, and
1499 // four with the earlier time as creation times. We should only get
1500 // two cookies remaining, but which two (other than that there should
1501 // be one from each set) will be random.
1502 std::vector<CanonicalCookie*> initial_cookies;
1503 AddCookieToList("www.google.com", "X=1; path=/", now, &initial_cookies);
1504 AddCookieToList("www.google.com", "X=2; path=/", now, &initial_cookies);
1505 AddCookieToList("www.google.com", "X=3; path=/", now, &initial_cookies);
1506 AddCookieToList("www.google.com", "X=4; path=/", now, &initial_cookies);
1508 AddCookieToList("www.google.com", "Y=1; path=/", earlier, &initial_cookies);
1509 AddCookieToList("www.google.com", "Y=2; path=/", earlier, &initial_cookies);
1510 AddCookieToList("www.google.com", "Y=3; path=/", earlier, &initial_cookies);
1511 AddCookieToList("www.google.com", "Y=4; path=/", earlier, &initial_cookies);
1513 // Inject our initial cookies into the mock PersistentCookieStore.
1514 store->SetLoadExpectation(true, initial_cookies);
1516 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL));
1518 CookieList list(GetAllCookies(cm.get()));
1519 EXPECT_EQ(2U, list.size());
1520 // Confirm that we have one of each.
1521 std::string name1(list[0].Name());
1522 std::string name2(list[1].Name());
1523 EXPECT_TRUE(name1 == "X" || name2 == "X");
1524 EXPECT_TRUE(name1 == "Y" || name2 == "Y");
1525 EXPECT_NE(name1, name2);
1528 TEST_F(CookieMonsterTest, CookieMonsterDelegate) {
1529 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore);
1530 scoped_refptr<MockCookieMonsterDelegate> delegate(
1531 new MockCookieMonsterDelegate);
1532 scoped_refptr<CookieMonster> cm(
1533 new CookieMonster(store.get(), delegate.get()));
1535 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "A=B"));
1536 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "C=D"));
1537 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "E=F"));
1538 EXPECT_EQ("A=B; C=D; E=F", GetCookies(cm.get(), url_google_));
1539 ASSERT_EQ(3u, delegate->changes().size());
1540 EXPECT_FALSE(delegate->changes()[0].second);
1541 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain());
1542 EXPECT_EQ("A", delegate->changes()[0].first.Name());
1543 EXPECT_EQ("B", delegate->changes()[0].first.Value());
1544 EXPECT_EQ(url_google_.host(), delegate->changes()[1].first.Domain());
1545 EXPECT_FALSE(delegate->changes()[1].second);
1546 EXPECT_EQ("C", delegate->changes()[1].first.Name());
1547 EXPECT_EQ("D", delegate->changes()[1].first.Value());
1548 EXPECT_EQ(url_google_.host(), delegate->changes()[2].first.Domain());
1549 EXPECT_FALSE(delegate->changes()[2].second);
1550 EXPECT_EQ("E", delegate->changes()[2].first.Name());
1551 EXPECT_EQ("F", delegate->changes()[2].first.Value());
1552 delegate->reset();
1554 EXPECT_TRUE(FindAndDeleteCookie(cm.get(), url_google_.host(), "C"));
1555 EXPECT_EQ("A=B; E=F", GetCookies(cm.get(), url_google_));
1556 ASSERT_EQ(1u, delegate->changes().size());
1557 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain());
1558 EXPECT_TRUE(delegate->changes()[0].second);
1559 EXPECT_EQ("C", delegate->changes()[0].first.Name());
1560 EXPECT_EQ("D", delegate->changes()[0].first.Value());
1561 delegate->reset();
1563 EXPECT_FALSE(FindAndDeleteCookie(cm.get(), "random.host", "E"));
1564 EXPECT_EQ("A=B; E=F", GetCookies(cm.get(), url_google_));
1565 EXPECT_EQ(0u, delegate->changes().size());
1567 // Insert a cookie "a" for path "/path1"
1568 EXPECT_TRUE(SetCookie(cm.get(), url_google_,
1569 "a=val1; path=/path1; "
1570 "expires=Mon, 18-Apr-22 22:50:13 GMT"));
1571 ASSERT_EQ(1u, store->commands().size());
1572 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type);
1573 ASSERT_EQ(1u, delegate->changes().size());
1574 EXPECT_FALSE(delegate->changes()[0].second);
1575 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain());
1576 EXPECT_EQ("a", delegate->changes()[0].first.Name());
1577 EXPECT_EQ("val1", delegate->changes()[0].first.Value());
1578 delegate->reset();
1580 // Insert a cookie "a" for path "/path1", that is httponly. This should
1581 // overwrite the non-http-only version.
1582 CookieOptions allow_httponly;
1583 allow_httponly.set_include_httponly();
1584 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_,
1585 "a=val2; path=/path1; httponly; "
1586 "expires=Mon, 18-Apr-22 22:50:14 GMT",
1587 allow_httponly));
1588 ASSERT_EQ(3u, store->commands().size());
1589 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type);
1590 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[2].type);
1591 ASSERT_EQ(2u, delegate->changes().size());
1592 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain());
1593 EXPECT_TRUE(delegate->changes()[0].second);
1594 EXPECT_EQ("a", delegate->changes()[0].first.Name());
1595 EXPECT_EQ("val1", delegate->changes()[0].first.Value());
1596 EXPECT_EQ(url_google_.host(), delegate->changes()[1].first.Domain());
1597 EXPECT_FALSE(delegate->changes()[1].second);
1598 EXPECT_EQ("a", delegate->changes()[1].first.Name());
1599 EXPECT_EQ("val2", delegate->changes()[1].first.Value());
1600 delegate->reset();
1603 TEST_F(CookieMonsterTest, SetCookieWithDetails) {
1604 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1606 EXPECT_TRUE(SetCookieWithDetails(cm.get(), url_google_foo_, "A", "B",
1607 std::string(), "/foo", base::Time(), false,
1608 false, false, COOKIE_PRIORITY_DEFAULT));
1609 EXPECT_TRUE(SetCookieWithDetails(cm.get(), url_google_bar_, "C", "D",
1610 "google.izzle", "/bar", base::Time(), false,
1611 true, false, COOKIE_PRIORITY_DEFAULT));
1612 EXPECT_TRUE(SetCookieWithDetails(
1613 cm.get(), url_google_, "E", "F", std::string(), std::string(),
1614 base::Time(), true, false, false, COOKIE_PRIORITY_DEFAULT));
1616 // Test that malformed attributes fail to set the cookie.
1617 EXPECT_FALSE(SetCookieWithDetails(cm.get(), url_google_foo_, " A", "B",
1618 std::string(), "/foo", base::Time(), false,
1619 false, false, COOKIE_PRIORITY_DEFAULT));
1620 EXPECT_FALSE(SetCookieWithDetails(cm.get(), url_google_foo_, "A;", "B",
1621 std::string(), "/foo", base::Time(), false,
1622 false, false, COOKIE_PRIORITY_DEFAULT));
1623 EXPECT_FALSE(SetCookieWithDetails(cm.get(), url_google_foo_, "A=", "B",
1624 std::string(), "/foo", base::Time(), false,
1625 false, false, COOKIE_PRIORITY_DEFAULT));
1626 EXPECT_FALSE(SetCookieWithDetails(
1627 cm.get(), url_google_foo_, "A", "B", "google.ozzzzzzle", "foo",
1628 base::Time(), false, false, false, COOKIE_PRIORITY_DEFAULT));
1629 EXPECT_FALSE(SetCookieWithDetails(cm.get(), url_google_foo_, "A=", "B",
1630 std::string(), "foo", base::Time(), false,
1631 false, false, COOKIE_PRIORITY_DEFAULT));
1633 CookieList cookies = GetAllCookiesForURL(cm.get(), url_google_foo_);
1634 CookieList::iterator it = cookies.begin();
1636 ASSERT_TRUE(it != cookies.end());
1637 EXPECT_EQ("A", it->Name());
1638 EXPECT_EQ("B", it->Value());
1639 EXPECT_EQ("www.google.izzle", it->Domain());
1640 EXPECT_EQ("/foo", it->Path());
1641 EXPECT_FALSE(it->IsPersistent());
1642 EXPECT_FALSE(it->IsSecure());
1643 EXPECT_FALSE(it->IsHttpOnly());
1645 ASSERT_TRUE(++it == cookies.end());
1647 cookies = GetAllCookiesForURL(cm.get(), url_google_bar_);
1648 it = cookies.begin();
1650 ASSERT_TRUE(it != cookies.end());
1651 EXPECT_EQ("C", it->Name());
1652 EXPECT_EQ("D", it->Value());
1653 EXPECT_EQ(".google.izzle", it->Domain());
1654 EXPECT_EQ("/bar", it->Path());
1655 EXPECT_FALSE(it->IsSecure());
1656 EXPECT_TRUE(it->IsHttpOnly());
1658 ASSERT_TRUE(++it == cookies.end());
1660 cookies = GetAllCookiesForURL(cm.get(), url_google_secure_);
1661 it = cookies.begin();
1663 ASSERT_TRUE(it != cookies.end());
1664 EXPECT_EQ("E", it->Name());
1665 EXPECT_EQ("F", it->Value());
1666 EXPECT_EQ("/", it->Path());
1667 EXPECT_EQ("www.google.izzle", it->Domain());
1668 EXPECT_TRUE(it->IsSecure());
1669 EXPECT_FALSE(it->IsHttpOnly());
1671 ASSERT_TRUE(++it == cookies.end());
1674 TEST_F(CookieMonsterTest, DeleteAllForHost) {
1675 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1677 // Test probes:
1678 // * Non-secure URL, mid-level (http://w.c.b.a)
1679 // * Secure URL, mid-level (https://w.c.b.a)
1680 // * URL with path, mid-level (https:/w.c.b.a/dir1/xx)
1681 // All three tests should nuke only the midlevel host cookie,
1682 // the http_only cookie, the host secure cookie, and the two host
1683 // path cookies. http_only, secure, and paths are ignored by
1684 // this call, and domain cookies arent touched.
1685 PopulateCmForDeleteAllForHost(cm);
1686 EXPECT_EQ("dom_1=X; dom_2=X; dom_3=X; host_3=X",
1687 GetCookies(cm.get(), GURL(kTopLevelDomainPlus3)));
1688 EXPECT_EQ("dom_1=X; dom_2=X; host_2=X; sec_dom=X; sec_host=X",
1689 GetCookies(cm.get(), GURL(kTopLevelDomainPlus2Secure)));
1690 EXPECT_EQ("dom_1=X; host_1=X",
1691 GetCookies(cm.get(), GURL(kTopLevelDomainPlus1)));
1692 EXPECT_EQ(
1693 "dom_path_2=X; host_path_2=X; dom_path_1=X; host_path_1=X; "
1694 "dom_1=X; dom_2=X; host_2=X; sec_dom=X; sec_host=X",
1695 GetCookies(cm.get(), GURL(kTopLevelDomainPlus2Secure +
1696 std::string("/dir1/dir2/xxx"))));
1698 EXPECT_EQ(6, DeleteAllForHost(cm.get(), GURL(kTopLevelDomainPlus2)));
1699 EXPECT_EQ(8U, GetAllCookies(cm.get()).size());
1701 EXPECT_EQ("dom_1=X; dom_2=X; dom_3=X; host_3=X",
1702 GetCookies(cm.get(), GURL(kTopLevelDomainPlus3)));
1703 EXPECT_EQ("dom_1=X; dom_2=X; sec_dom=X",
1704 GetCookies(cm.get(), GURL(kTopLevelDomainPlus2Secure)));
1705 EXPECT_EQ("dom_1=X; host_1=X",
1706 GetCookies(cm.get(), GURL(kTopLevelDomainPlus1)));
1707 EXPECT_EQ("dom_path_2=X; dom_path_1=X; dom_1=X; dom_2=X; sec_dom=X",
1708 GetCookies(cm.get(), GURL(kTopLevelDomainPlus2Secure +
1709 std::string("/dir1/dir2/xxx"))));
1711 PopulateCmForDeleteAllForHost(cm);
1712 EXPECT_EQ(6, DeleteAllForHost(cm.get(), GURL(kTopLevelDomainPlus2Secure)));
1713 EXPECT_EQ(8U, GetAllCookies(cm.get()).size());
1715 EXPECT_EQ("dom_1=X; dom_2=X; dom_3=X; host_3=X",
1716 GetCookies(cm.get(), GURL(kTopLevelDomainPlus3)));
1717 EXPECT_EQ("dom_1=X; dom_2=X; sec_dom=X",
1718 GetCookies(cm.get(), GURL(kTopLevelDomainPlus2Secure)));
1719 EXPECT_EQ("dom_1=X; host_1=X",
1720 GetCookies(cm.get(), GURL(kTopLevelDomainPlus1)));
1721 EXPECT_EQ("dom_path_2=X; dom_path_1=X; dom_1=X; dom_2=X; sec_dom=X",
1722 GetCookies(cm.get(), GURL(kTopLevelDomainPlus2Secure +
1723 std::string("/dir1/dir2/xxx"))));
1725 PopulateCmForDeleteAllForHost(cm);
1726 EXPECT_EQ(6, DeleteAllForHost(cm.get(), GURL(kTopLevelDomainPlus2Secure +
1727 std::string("/dir1/xxx"))));
1728 EXPECT_EQ(8U, GetAllCookies(cm.get()).size());
1730 EXPECT_EQ("dom_1=X; dom_2=X; dom_3=X; host_3=X",
1731 GetCookies(cm.get(), GURL(kTopLevelDomainPlus3)));
1732 EXPECT_EQ("dom_1=X; dom_2=X; sec_dom=X",
1733 GetCookies(cm.get(), GURL(kTopLevelDomainPlus2Secure)));
1734 EXPECT_EQ("dom_1=X; host_1=X",
1735 GetCookies(cm.get(), GURL(kTopLevelDomainPlus1)));
1736 EXPECT_EQ("dom_path_2=X; dom_path_1=X; dom_1=X; dom_2=X; sec_dom=X",
1737 GetCookies(cm.get(), GURL(kTopLevelDomainPlus2Secure +
1738 std::string("/dir1/dir2/xxx"))));
1741 TEST_F(CookieMonsterTest, UniqueCreationTime) {
1742 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1743 CookieOptions options;
1745 // Add in three cookies through every public interface to the
1746 // CookieMonster and confirm that none of them have duplicate
1747 // creation times.
1749 // SetCookieWithCreationTime and SetCookieWithCreationTimeAndOptions
1750 // are not included as they aren't going to be public for very much
1751 // longer.
1753 // SetCookie, SetCookieWithOptions, SetCookieWithDetails
1755 SetCookie(cm.get(), url_google_, "SetCookie1=A");
1756 SetCookie(cm.get(), url_google_, "SetCookie2=A");
1757 SetCookie(cm.get(), url_google_, "SetCookie3=A");
1759 SetCookieWithOptions(cm.get(), url_google_, "setCookieWithOptions1=A",
1760 options);
1761 SetCookieWithOptions(cm.get(), url_google_, "setCookieWithOptions2=A",
1762 options);
1763 SetCookieWithOptions(cm.get(), url_google_, "setCookieWithOptions3=A",
1764 options);
1766 SetCookieWithDetails(cm.get(), url_google_, "setCookieWithDetails1", "A",
1767 ".google.com", "/", Time(), false, false, false,
1768 COOKIE_PRIORITY_DEFAULT);
1769 SetCookieWithDetails(cm.get(), url_google_, "setCookieWithDetails2", "A",
1770 ".google.com", "/", Time(), false, false, false,
1771 COOKIE_PRIORITY_DEFAULT);
1772 SetCookieWithDetails(cm.get(), url_google_, "setCookieWithDetails3", "A",
1773 ".google.com", "/", Time(), false, false, false,
1774 COOKIE_PRIORITY_DEFAULT);
1776 // Now we check
1777 CookieList cookie_list(GetAllCookies(cm.get()));
1778 typedef std::map<int64, CanonicalCookie> TimeCookieMap;
1779 TimeCookieMap check_map;
1780 for (CookieList::const_iterator it = cookie_list.begin();
1781 it != cookie_list.end(); it++) {
1782 const int64 creation_date = it->CreationDate().ToInternalValue();
1783 TimeCookieMap::const_iterator existing_cookie_it(
1784 check_map.find(creation_date));
1785 EXPECT_TRUE(existing_cookie_it == check_map.end())
1786 << "Cookie " << it->Name() << " has same creation date ("
1787 << it->CreationDate().ToInternalValue()
1788 << ") as previously entered cookie "
1789 << existing_cookie_it->second.Name();
1791 if (existing_cookie_it == check_map.end()) {
1792 check_map.insert(
1793 TimeCookieMap::value_type(it->CreationDate().ToInternalValue(), *it));
1798 // Mainly a test of GetEffectiveDomain, or more specifically, of the
1799 // expected behavior of GetEffectiveDomain within the CookieMonster.
1800 TEST_F(CookieMonsterTest, GetKey) {
1801 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1803 // This test is really only interesting if GetKey() actually does something.
1804 EXPECT_EQ("google.com", cm->GetKey("www.google.com"));
1805 EXPECT_EQ("google.izzie", cm->GetKey("www.google.izzie"));
1806 EXPECT_EQ("google.izzie", cm->GetKey(".google.izzie"));
1807 EXPECT_EQ("bbc.co.uk", cm->GetKey("bbc.co.uk"));
1808 EXPECT_EQ("bbc.co.uk", cm->GetKey("a.b.c.d.bbc.co.uk"));
1809 EXPECT_EQ("apple.com", cm->GetKey("a.b.c.d.apple.com"));
1810 EXPECT_EQ("apple.izzie", cm->GetKey("a.b.c.d.apple.izzie"));
1812 // Cases where the effective domain is null, so we use the host
1813 // as the key.
1814 EXPECT_EQ("co.uk", cm->GetKey("co.uk"));
1815 const std::string extension_name("iehocdgbbocmkdidlbnnfbmbinnahbae");
1816 EXPECT_EQ(extension_name, cm->GetKey(extension_name));
1817 EXPECT_EQ("com", cm->GetKey("com"));
1818 EXPECT_EQ("hostalias", cm->GetKey("hostalias"));
1819 EXPECT_EQ("localhost", cm->GetKey("localhost"));
1822 // Test that cookies transfer from/to the backing store correctly.
1823 TEST_F(CookieMonsterTest, BackingStoreCommunication) {
1824 // Store details for cookies transforming through the backing store interface.
1826 base::Time current(base::Time::Now());
1827 scoped_refptr<MockSimplePersistentCookieStore> store(
1828 new MockSimplePersistentCookieStore);
1829 base::Time new_access_time;
1830 base::Time expires(base::Time::Now() + base::TimeDelta::FromSeconds(100));
1832 const CookiesInputInfo input_info[] = {
1833 {GURL("http://a.b.google.com"),
1834 "a",
1835 "1",
1837 "/path/to/cookie",
1838 expires,
1839 false,
1840 false,
1841 false,
1842 COOKIE_PRIORITY_DEFAULT},
1843 {GURL("https://www.google.com"),
1844 "b",
1845 "2",
1846 ".google.com",
1847 "/path/from/cookie",
1848 expires + TimeDelta::FromSeconds(10),
1849 true,
1850 true,
1851 false,
1852 COOKIE_PRIORITY_DEFAULT},
1853 {GURL("https://google.com"),
1854 "c",
1855 "3",
1857 "/another/path/to/cookie",
1858 base::Time::Now() + base::TimeDelta::FromSeconds(100),
1859 true,
1860 false,
1861 true,
1862 COOKIE_PRIORITY_DEFAULT}};
1863 const int INPUT_DELETE = 1;
1865 // Create new cookies and flush them to the store.
1867 scoped_refptr<CookieMonster> cmout(new CookieMonster(store.get(), NULL));
1868 for (const CookiesInputInfo* p = input_info;
1869 p < &input_info[arraysize(input_info)]; p++) {
1870 EXPECT_TRUE(SetCookieWithDetails(cmout.get(), p->url, p->name, p->value,
1871 p->domain, p->path, p->expiration_time,
1872 p->secure, p->http_only,
1873 p->first_party_only, p->priority));
1875 GURL del_url(input_info[INPUT_DELETE]
1876 .url.Resolve(input_info[INPUT_DELETE].path)
1877 .spec());
1878 DeleteCookie(cmout.get(), del_url, input_info[INPUT_DELETE].name);
1881 // Create a new cookie monster and make sure that everything is correct
1883 scoped_refptr<CookieMonster> cmin(new CookieMonster(store.get(), NULL));
1884 CookieList cookies(GetAllCookies(cmin.get()));
1885 ASSERT_EQ(2u, cookies.size());
1886 // Ordering is path length, then creation time. So second cookie
1887 // will come first, and we need to swap them.
1888 std::swap(cookies[0], cookies[1]);
1889 for (int output_index = 0; output_index < 2; output_index++) {
1890 int input_index = output_index * 2;
1891 const CookiesInputInfo* input = &input_info[input_index];
1892 const CanonicalCookie* output = &cookies[output_index];
1894 EXPECT_EQ(input->name, output->Name());
1895 EXPECT_EQ(input->value, output->Value());
1896 EXPECT_EQ(input->url.host(), output->Domain());
1897 EXPECT_EQ(input->path, output->Path());
1898 EXPECT_LE(current.ToInternalValue(),
1899 output->CreationDate().ToInternalValue());
1900 EXPECT_EQ(input->secure, output->IsSecure());
1901 EXPECT_EQ(input->http_only, output->IsHttpOnly());
1902 EXPECT_EQ(input->first_party_only, output->IsFirstPartyOnly());
1903 EXPECT_TRUE(output->IsPersistent());
1904 EXPECT_EQ(input->expiration_time.ToInternalValue(),
1905 output->ExpiryDate().ToInternalValue());
1910 TEST_F(CookieMonsterTest, CookieListOrdering) {
1911 // Put a random set of cookies into a monster and make sure
1912 // they're returned in the right order.
1913 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1914 EXPECT_TRUE(
1915 SetCookie(cm.get(), GURL("http://d.c.b.a.google.com/aa/x.html"), "c=1"));
1916 EXPECT_TRUE(SetCookie(cm.get(), GURL("http://b.a.google.com/aa/bb/cc/x.html"),
1917 "d=1; domain=b.a.google.com"));
1918 EXPECT_TRUE(SetCookie(cm.get(), GURL("http://b.a.google.com/aa/bb/cc/x.html"),
1919 "a=4; domain=b.a.google.com"));
1920 EXPECT_TRUE(SetCookie(cm.get(),
1921 GURL("http://c.b.a.google.com/aa/bb/cc/x.html"),
1922 "e=1; domain=c.b.a.google.com"));
1923 EXPECT_TRUE(SetCookie(cm.get(),
1924 GURL("http://d.c.b.a.google.com/aa/bb/x.html"), "b=1"));
1925 EXPECT_TRUE(SetCookie(cm.get(), GURL("http://news.bbc.co.uk/midpath/x.html"),
1926 "g=10"));
1928 unsigned int i = 0;
1929 CookieList cookies(GetAllCookiesForURL(
1930 cm.get(), GURL("http://d.c.b.a.google.com/aa/bb/cc/dd")));
1931 ASSERT_EQ(5u, cookies.size());
1932 EXPECT_EQ("d", cookies[i++].Name());
1933 EXPECT_EQ("a", cookies[i++].Name());
1934 EXPECT_EQ("e", cookies[i++].Name());
1935 EXPECT_EQ("b", cookies[i++].Name());
1936 EXPECT_EQ("c", cookies[i++].Name());
1940 unsigned int i = 0;
1941 CookieList cookies(GetAllCookies(cm.get()));
1942 ASSERT_EQ(6u, cookies.size());
1943 EXPECT_EQ("d", cookies[i++].Name());
1944 EXPECT_EQ("a", cookies[i++].Name());
1945 EXPECT_EQ("e", cookies[i++].Name());
1946 EXPECT_EQ("g", cookies[i++].Name());
1947 EXPECT_EQ("b", cookies[i++].Name());
1948 EXPECT_EQ("c", cookies[i++].Name());
1952 // This test and CookieMonstertest.TestGCTimes (in cookie_monster_perftest.cc)
1953 // are somewhat complementary twins. This test is probing for whether
1954 // garbage collection always happens when it should (i.e. that we actually
1955 // get rid of cookies when we should). The perftest is probing for
1956 // whether garbage collection happens when it shouldn't. See comments
1957 // before that test for more details.
1959 // Disabled on Windows, see crbug.com/126095
1960 #if defined(OS_WIN)
1961 #define MAYBE_GarbageCollectionTriggers DISABLED_GarbageCollectionTriggers
1962 #else
1963 #define MAYBE_GarbageCollectionTriggers GarbageCollectionTriggers
1964 #endif
1966 TEST_F(CookieMonsterTest, MAYBE_GarbageCollectionTriggers) {
1967 // First we check to make sure that a whole lot of recent cookies
1968 // doesn't get rid of anything after garbage collection is checked for.
1970 scoped_refptr<CookieMonster> cm(
1971 CreateMonsterForGC(CookieMonster::kMaxCookies * 2));
1972 EXPECT_EQ(CookieMonster::kMaxCookies * 2, GetAllCookies(cm.get()).size());
1973 SetCookie(cm.get(), GURL("http://newdomain.com"), "b=2");
1974 EXPECT_EQ(CookieMonster::kMaxCookies * 2 + 1,
1975 GetAllCookies(cm.get()).size());
1978 // Now we explore a series of relationships between cookie last access
1979 // time and size of store to make sure we only get rid of cookies when
1980 // we really should.
1981 const struct TestCase {
1982 size_t num_cookies;
1983 size_t num_old_cookies;
1984 size_t expected_initial_cookies;
1985 // Indexed by ExpiryAndKeyScheme
1986 size_t expected_cookies_after_set;
1987 } test_cases[] = {
1988 {// A whole lot of recent cookies; gc shouldn't happen.
1989 CookieMonster::kMaxCookies * 2,
1991 CookieMonster::kMaxCookies * 2,
1992 CookieMonster::kMaxCookies * 2 + 1},
1993 {// Some old cookies, but still overflowing max.
1994 CookieMonster::kMaxCookies * 2,
1995 CookieMonster::kMaxCookies / 2,
1996 CookieMonster::kMaxCookies * 2,
1997 CookieMonster::kMaxCookies * 2 - CookieMonster::kMaxCookies / 2 + 1},
1998 {// Old cookies enough to bring us right down to our purge line.
1999 CookieMonster::kMaxCookies * 2,
2000 CookieMonster::kMaxCookies + CookieMonster::kPurgeCookies + 1,
2001 CookieMonster::kMaxCookies * 2,
2002 CookieMonster::kMaxCookies - CookieMonster::kPurgeCookies},
2003 {// Old cookies enough to bring below our purge line (which we
2004 // shouldn't do).
2005 CookieMonster::kMaxCookies * 2,
2006 CookieMonster::kMaxCookies * 3 / 2,
2007 CookieMonster::kMaxCookies * 2,
2008 CookieMonster::kMaxCookies - CookieMonster::kPurgeCookies}};
2010 for (int ci = 0; ci < static_cast<int>(arraysize(test_cases)); ++ci) {
2011 const TestCase* test_case = &test_cases[ci];
2012 scoped_refptr<CookieMonster> cm(CreateMonsterFromStoreForGC(
2013 test_case->num_cookies, test_case->num_old_cookies,
2014 CookieMonster::kSafeFromGlobalPurgeDays * 2));
2015 EXPECT_EQ(test_case->expected_initial_cookies,
2016 GetAllCookies(cm.get()).size())
2017 << "For test case " << ci;
2018 // Will trigger GC
2019 SetCookie(cm.get(), GURL("http://newdomain.com"), "b=2");
2020 EXPECT_EQ(test_case->expected_cookies_after_set,
2021 GetAllCookies(cm.get()).size())
2022 << "For test case " << ci;
2026 // This test checks that keep expired cookies flag is working.
2027 TEST_F(CookieMonsterTest, KeepExpiredCookies) {
2028 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2029 cm->SetKeepExpiredCookies();
2030 CookieOptions options;
2032 // Set a persistent cookie.
2033 ASSERT_TRUE(SetCookieWithOptions(
2034 cm.get(), url_google_,
2035 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-22 22:50:13 GMT",
2036 options));
2038 // Get the canonical cookie.
2039 CookieList cookie_list = GetAllCookies(cm.get());
2040 ASSERT_EQ(1U, cookie_list.size());
2042 // Use a past expiry date to delete the cookie.
2043 ASSERT_TRUE(SetCookieWithOptions(
2044 cm.get(), url_google_,
2045 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-1977 22:50:13 GMT",
2046 options));
2048 // Check that the cookie with the past expiry date is still there.
2049 // GetAllCookies() also triggers garbage collection.
2050 cookie_list = GetAllCookies(cm.get());
2051 ASSERT_EQ(1U, cookie_list.size());
2052 ASSERT_TRUE(cookie_list[0].IsExpired(Time::Now()));
2055 namespace {
2057 // Mock PersistentCookieStore that keeps track of the number of Flush() calls.
2058 class FlushablePersistentStore : public CookieMonster::PersistentCookieStore {
2059 public:
2060 FlushablePersistentStore() : flush_count_(0) {}
2062 void Load(const LoadedCallback& loaded_callback) override {
2063 std::vector<CanonicalCookie*> out_cookies;
2064 base::ThreadTaskRunnerHandle::Get()->PostTask(
2065 FROM_HERE,
2066 base::Bind(&LoadedCallbackTask::Run,
2067 new LoadedCallbackTask(loaded_callback, out_cookies)));
2070 void LoadCookiesForKey(const std::string& key,
2071 const LoadedCallback& loaded_callback) override {
2072 Load(loaded_callback);
2075 void AddCookie(const CanonicalCookie&) override {}
2076 void UpdateCookieAccessTime(const CanonicalCookie&) override {}
2077 void DeleteCookie(const CanonicalCookie&) override {}
2078 void SetForceKeepSessionState() override {}
2080 void Flush(const base::Closure& callback) override {
2081 ++flush_count_;
2082 if (!callback.is_null())
2083 callback.Run();
2086 int flush_count() { return flush_count_; }
2088 private:
2089 ~FlushablePersistentStore() override {}
2091 volatile int flush_count_;
2094 // Counts the number of times Callback() has been run.
2095 class CallbackCounter : public base::RefCountedThreadSafe<CallbackCounter> {
2096 public:
2097 CallbackCounter() : callback_count_(0) {}
2099 void Callback() { ++callback_count_; }
2101 int callback_count() { return callback_count_; }
2103 private:
2104 friend class base::RefCountedThreadSafe<CallbackCounter>;
2105 ~CallbackCounter() {}
2107 volatile int callback_count_;
2110 } // namespace
2112 // Test that FlushStore() is forwarded to the store and callbacks are posted.
2113 TEST_F(CookieMonsterTest, FlushStore) {
2114 scoped_refptr<CallbackCounter> counter(new CallbackCounter());
2115 scoped_refptr<FlushablePersistentStore> store(new FlushablePersistentStore());
2116 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL));
2118 ASSERT_EQ(0, store->flush_count());
2119 ASSERT_EQ(0, counter->callback_count());
2121 // Before initialization, FlushStore() should just run the callback.
2122 cm->FlushStore(base::Bind(&CallbackCounter::Callback, counter.get()));
2123 base::MessageLoop::current()->RunUntilIdle();
2125 ASSERT_EQ(0, store->flush_count());
2126 ASSERT_EQ(1, counter->callback_count());
2128 // NULL callback is safe.
2129 cm->FlushStore(base::Closure());
2130 base::MessageLoop::current()->RunUntilIdle();
2132 ASSERT_EQ(0, store->flush_count());
2133 ASSERT_EQ(1, counter->callback_count());
2135 // After initialization, FlushStore() should delegate to the store.
2136 GetAllCookies(cm.get()); // Force init.
2137 cm->FlushStore(base::Bind(&CallbackCounter::Callback, counter.get()));
2138 base::MessageLoop::current()->RunUntilIdle();
2140 ASSERT_EQ(1, store->flush_count());
2141 ASSERT_EQ(2, counter->callback_count());
2143 // NULL callback is still safe.
2144 cm->FlushStore(base::Closure());
2145 base::MessageLoop::current()->RunUntilIdle();
2147 ASSERT_EQ(2, store->flush_count());
2148 ASSERT_EQ(2, counter->callback_count());
2150 // If there's no backing store, FlushStore() is always a safe no-op.
2151 cm = new CookieMonster(NULL, NULL);
2152 GetAllCookies(cm.get()); // Force init.
2153 cm->FlushStore(base::Closure());
2154 base::MessageLoop::current()->RunUntilIdle();
2156 ASSERT_EQ(2, counter->callback_count());
2158 cm->FlushStore(base::Bind(&CallbackCounter::Callback, counter.get()));
2159 base::MessageLoop::current()->RunUntilIdle();
2161 ASSERT_EQ(3, counter->callback_count());
2164 TEST_F(CookieMonsterTest, SetAllCookies) {
2165 scoped_refptr<FlushablePersistentStore> store(new FlushablePersistentStore());
2166 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL));
2167 cm->SetPersistSessionCookies(true);
2169 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "U=V; path=/"));
2170 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "W=X; path=/foo"));
2171 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "Y=Z; path=/"));
2173 CookieList list;
2174 list.push_back(CanonicalCookie(url_google_, "A", "B", url_google_.host(), "/",
2175 base::Time::Now(), base::Time(), base::Time(),
2176 false, false, false, COOKIE_PRIORITY_DEFAULT));
2177 list.push_back(CanonicalCookie(url_google_, "W", "X", url_google_.host(),
2178 "/bar", base::Time::Now(), base::Time(),
2179 base::Time(), false, false, false,
2180 COOKIE_PRIORITY_DEFAULT));
2181 list.push_back(CanonicalCookie(url_google_, "Y", "Z", url_google_.host(), "/",
2182 base::Time::Now(), base::Time(), base::Time(),
2183 false, false, false, COOKIE_PRIORITY_DEFAULT));
2185 // SetAllCookies must not flush.
2186 ASSERT_EQ(0, store->flush_count());
2187 EXPECT_TRUE(SetAllCookies(cm.get(), list));
2188 EXPECT_EQ(0, store->flush_count());
2190 CookieList cookies = GetAllCookies(cm.get());
2191 size_t expected_size = 3; // "A", "W" and "Y". "U" is gone.
2192 EXPECT_EQ(expected_size, cookies.size());
2193 CookieList::iterator it = cookies.begin();
2195 ASSERT_TRUE(it != cookies.end());
2196 EXPECT_EQ("W", it->Name());
2197 EXPECT_EQ("X", it->Value());
2198 EXPECT_EQ("/bar", it->Path()); // The path has been updated.
2200 ASSERT_TRUE(++it != cookies.end());
2201 EXPECT_EQ("A", it->Name());
2202 EXPECT_EQ("B", it->Value());
2204 ASSERT_TRUE(++it != cookies.end());
2205 EXPECT_EQ("Y", it->Name());
2206 EXPECT_EQ("Z", it->Value());
2209 TEST_F(CookieMonsterTest, ComputeCookieDiff) {
2210 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2212 base::Time now = base::Time::Now();
2213 base::Time creation_time = now - base::TimeDelta::FromSeconds(1);
2215 CanonicalCookie cookie1(url_google_, "A", "B", url_google_.host(), "/",
2216 creation_time, base::Time(), base::Time(), false,
2217 false, false, COOKIE_PRIORITY_DEFAULT);
2218 CanonicalCookie cookie2(url_google_, "C", "D", url_google_.host(), "/",
2219 creation_time, base::Time(), base::Time(), false,
2220 false, false, COOKIE_PRIORITY_DEFAULT);
2221 CanonicalCookie cookie3(url_google_, "E", "F", url_google_.host(), "/",
2222 creation_time, base::Time(), base::Time(), false,
2223 false, false, COOKIE_PRIORITY_DEFAULT);
2224 CanonicalCookie cookie4(url_google_, "G", "H", url_google_.host(), "/",
2225 creation_time, base::Time(), base::Time(), false,
2226 false, false, COOKIE_PRIORITY_DEFAULT);
2227 CanonicalCookie cookie4_with_new_value(
2228 url_google_, "G", "iamnew", url_google_.host(), "/", creation_time,
2229 base::Time(), base::Time(), false, false, false, COOKIE_PRIORITY_DEFAULT);
2230 CanonicalCookie cookie5(url_google_, "I", "J", url_google_.host(), "/",
2231 creation_time, base::Time(), base::Time(), false,
2232 false, false, COOKIE_PRIORITY_DEFAULT);
2233 CanonicalCookie cookie5_with_new_creation_time(
2234 url_google_, "I", "J", url_google_.host(), "/", now, base::Time(),
2235 base::Time(), false, false, false, COOKIE_PRIORITY_DEFAULT);
2236 CanonicalCookie cookie6(url_google_, "K", "L", url_google_.host(), "/foo",
2237 creation_time, base::Time(), base::Time(), false,
2238 false, false, COOKIE_PRIORITY_DEFAULT);
2239 CanonicalCookie cookie6_with_new_path(
2240 url_google_, "K", "L", url_google_.host(), "/bar", creation_time,
2241 base::Time(), base::Time(), false, false, false, COOKIE_PRIORITY_DEFAULT);
2242 CanonicalCookie cookie7(url_google_, "M", "N", url_google_.host(), "/foo",
2243 creation_time, base::Time(), base::Time(), false,
2244 false, false, COOKIE_PRIORITY_DEFAULT);
2245 CanonicalCookie cookie7_with_new_path(
2246 url_google_, "M", "N", url_google_.host(), "/bar", creation_time,
2247 base::Time(), base::Time(), false, false, false, COOKIE_PRIORITY_DEFAULT);
2249 CookieList old_cookies;
2250 old_cookies.push_back(cookie1);
2251 old_cookies.push_back(cookie2);
2252 old_cookies.push_back(cookie4);
2253 old_cookies.push_back(cookie5);
2254 old_cookies.push_back(cookie6);
2255 old_cookies.push_back(cookie7);
2257 CookieList new_cookies;
2258 new_cookies.push_back(cookie1);
2259 new_cookies.push_back(cookie3);
2260 new_cookies.push_back(cookie4_with_new_value);
2261 new_cookies.push_back(cookie5_with_new_creation_time);
2262 new_cookies.push_back(cookie6_with_new_path);
2263 new_cookies.push_back(cookie7);
2264 new_cookies.push_back(cookie7_with_new_path);
2266 CookieList cookies_to_add;
2267 CookieList cookies_to_delete;
2269 cm->ComputeCookieDiff(&old_cookies, &new_cookies, &cookies_to_add,
2270 &cookies_to_delete);
2272 // |cookie1| has not changed.
2273 EXPECT_FALSE(IsCookieInList(cookie1, cookies_to_add));
2274 EXPECT_FALSE(IsCookieInList(cookie1, cookies_to_delete));
2276 // |cookie2| has been deleted.
2277 EXPECT_FALSE(IsCookieInList(cookie2, cookies_to_add));
2278 EXPECT_TRUE(IsCookieInList(cookie2, cookies_to_delete));
2280 // |cookie3| has been added.
2281 EXPECT_TRUE(IsCookieInList(cookie3, cookies_to_add));
2282 EXPECT_FALSE(IsCookieInList(cookie3, cookies_to_delete));
2284 // |cookie4| has a new value: new cookie overrides the old one (which does not
2285 // need to be explicitly removed).
2286 EXPECT_FALSE(IsCookieInList(cookie4, cookies_to_add));
2287 EXPECT_FALSE(IsCookieInList(cookie4, cookies_to_delete));
2288 EXPECT_TRUE(IsCookieInList(cookie4_with_new_value, cookies_to_add));
2289 EXPECT_FALSE(IsCookieInList(cookie4_with_new_value, cookies_to_delete));
2291 // |cookie5| has a new creation time: new cookie overrides the old one (which
2292 // does not need to be explicitly removed).
2293 EXPECT_FALSE(IsCookieInList(cookie5, cookies_to_add));
2294 EXPECT_FALSE(IsCookieInList(cookie5, cookies_to_delete));
2295 EXPECT_TRUE(IsCookieInList(cookie5_with_new_creation_time, cookies_to_add));
2296 EXPECT_FALSE(
2297 IsCookieInList(cookie5_with_new_creation_time, cookies_to_delete));
2299 // |cookie6| has a new path: the new cookie does not overrides the old one,
2300 // which needs to be explicitly removed.
2301 EXPECT_FALSE(IsCookieInList(cookie6, cookies_to_add));
2302 EXPECT_TRUE(IsCookieInList(cookie6, cookies_to_delete));
2303 EXPECT_TRUE(IsCookieInList(cookie6_with_new_path, cookies_to_add));
2304 EXPECT_FALSE(IsCookieInList(cookie6_with_new_path, cookies_to_delete));
2306 // |cookie7| is kept and |cookie7_with_new_path| is added as a new cookie.
2307 EXPECT_FALSE(IsCookieInList(cookie7, cookies_to_add));
2308 EXPECT_FALSE(IsCookieInList(cookie7, cookies_to_delete));
2309 EXPECT_TRUE(IsCookieInList(cookie7_with_new_path, cookies_to_add));
2310 EXPECT_FALSE(IsCookieInList(cookie7_with_new_path, cookies_to_delete));
2313 // Check that DeleteAll does flush (as a sanity check that flush_count()
2314 // works).
2315 TEST_F(CookieMonsterTest, DeleteAll) {
2316 scoped_refptr<FlushablePersistentStore> store(new FlushablePersistentStore());
2317 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL));
2318 cm->SetPersistSessionCookies(true);
2320 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "X=Y; path=/"));
2322 ASSERT_EQ(0, store->flush_count());
2323 EXPECT_EQ(1, DeleteAll(cm.get()));
2324 EXPECT_EQ(1, store->flush_count());
2327 TEST_F(CookieMonsterTest, HistogramCheck) {
2328 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2329 // Should match call in InitializeHistograms, but doesn't really matter
2330 // since the histogram should have been initialized by the CM construction
2331 // above.
2332 base::HistogramBase* expired_histogram = base::Histogram::FactoryGet(
2333 "Cookie.ExpirationDurationMinutes", 1, 10 * 365 * 24 * 60, 50,
2334 base::Histogram::kUmaTargetedHistogramFlag);
2336 scoped_ptr<base::HistogramSamples> samples1(
2337 expired_histogram->SnapshotSamples());
2338 ASSERT_TRUE(SetCookieWithDetails(
2339 cm.get(), GURL("http://fake.a.url"), "a", "b", "a.url", "/",
2340 base::Time::Now() + base::TimeDelta::FromMinutes(59), false, false, false,
2341 COOKIE_PRIORITY_DEFAULT));
2343 scoped_ptr<base::HistogramSamples> samples2(
2344 expired_histogram->SnapshotSamples());
2345 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount());
2347 // kValidCookieLine creates a session cookie.
2348 ASSERT_TRUE(SetCookie(cm.get(), url_google_, kValidCookieLine));
2350 scoped_ptr<base::HistogramSamples> samples3(
2351 expired_histogram->SnapshotSamples());
2352 EXPECT_EQ(samples2->TotalCount(), samples3->TotalCount());
2355 namespace {
2357 class MultiThreadedCookieMonsterTest : public CookieMonsterTest {
2358 public:
2359 MultiThreadedCookieMonsterTest() : other_thread_("CMTthread") {}
2361 // Helper methods for calling the asynchronous CookieMonster methods
2362 // from a different thread.
2364 void GetAllCookiesTask(CookieMonster* cm, GetCookieListCallback* callback) {
2365 cm->GetAllCookiesAsync(
2366 base::Bind(&GetCookieListCallback::Run, base::Unretained(callback)));
2369 void GetAllCookiesForURLTask(CookieMonster* cm,
2370 const GURL& url,
2371 GetCookieListCallback* callback) {
2372 cm->GetAllCookiesForURLAsync(url, base::Bind(&GetCookieListCallback::Run,
2373 base::Unretained(callback)));
2376 void GetAllCookiesForURLWithOptionsTask(CookieMonster* cm,
2377 const GURL& url,
2378 const CookieOptions& options,
2379 GetCookieListCallback* callback) {
2380 cm->GetAllCookiesForURLWithOptionsAsync(
2381 url, options,
2382 base::Bind(&GetCookieListCallback::Run, base::Unretained(callback)));
2385 void SetCookieWithDetailsTask(CookieMonster* cm,
2386 const GURL& url,
2387 ResultSavingCookieCallback<bool>* callback) {
2388 // Define the parameters here instead of in the calling fucntion.
2389 // The maximum number of parameters for Bind function is 6.
2390 std::string name = "A";
2391 std::string value = "B";
2392 std::string domain = std::string();
2393 std::string path = "/foo";
2394 base::Time expiration_time = base::Time();
2395 bool secure = false;
2396 bool http_only = false;
2397 bool first_party_only = false;
2398 CookiePriority priority = COOKIE_PRIORITY_DEFAULT;
2399 cm->SetCookieWithDetailsAsync(
2400 url, name, value, domain, path, expiration_time, secure, http_only,
2401 first_party_only, priority,
2402 base::Bind(&ResultSavingCookieCallback<bool>::Run,
2403 base::Unretained(callback)));
2406 void DeleteAllCreatedBetweenTask(CookieMonster* cm,
2407 const base::Time& delete_begin,
2408 const base::Time& delete_end,
2409 ResultSavingCookieCallback<int>* callback) {
2410 cm->DeleteAllCreatedBetweenAsync(
2411 delete_begin, delete_end,
2412 base::Bind(&ResultSavingCookieCallback<int>::Run,
2413 base::Unretained(callback)));
2416 void DeleteAllForHostTask(CookieMonster* cm,
2417 const GURL& url,
2418 ResultSavingCookieCallback<int>* callback) {
2419 cm->DeleteAllForHostAsync(url,
2420 base::Bind(&ResultSavingCookieCallback<int>::Run,
2421 base::Unretained(callback)));
2424 void DeleteAllCreatedBetweenForHostTask(
2425 CookieMonster* cm,
2426 const base::Time delete_begin,
2427 const base::Time delete_end,
2428 const GURL& url,
2429 ResultSavingCookieCallback<int>* callback) {
2430 cm->DeleteAllCreatedBetweenForHostAsync(
2431 delete_begin, delete_end, url,
2432 base::Bind(&ResultSavingCookieCallback<int>::Run,
2433 base::Unretained(callback)));
2436 void DeleteCanonicalCookieTask(CookieMonster* cm,
2437 const CanonicalCookie& cookie,
2438 ResultSavingCookieCallback<bool>* callback) {
2439 cm->DeleteCanonicalCookieAsync(
2440 cookie, base::Bind(&ResultSavingCookieCallback<bool>::Run,
2441 base::Unretained(callback)));
2444 protected:
2445 void RunOnOtherThread(const base::Closure& task) {
2446 other_thread_.Start();
2447 other_thread_.task_runner()->PostTask(FROM_HERE, task);
2448 RunFor(kTimeout);
2449 other_thread_.Stop();
2452 Thread other_thread_;
2455 } // namespace
2457 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookies) {
2458 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2459 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "A=B"));
2460 CookieList cookies = GetAllCookies(cm.get());
2461 CookieList::const_iterator it = cookies.begin();
2462 ASSERT_TRUE(it != cookies.end());
2463 EXPECT_EQ("www.google.izzle", it->Domain());
2464 EXPECT_EQ("A", it->Name());
2465 ASSERT_TRUE(++it == cookies.end());
2466 GetCookieListCallback callback(&other_thread_);
2467 base::Closure task =
2468 base::Bind(&MultiThreadedCookieMonsterTest::GetAllCookiesTask,
2469 base::Unretained(this), cm, &callback);
2470 RunOnOtherThread(task);
2471 EXPECT_TRUE(callback.did_run());
2472 it = callback.cookies().begin();
2473 ASSERT_TRUE(it != callback.cookies().end());
2474 EXPECT_EQ("www.google.izzle", it->Domain());
2475 EXPECT_EQ("A", it->Name());
2476 ASSERT_TRUE(++it == callback.cookies().end());
2479 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookiesForURL) {
2480 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2481 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "A=B"));
2482 CookieList cookies = GetAllCookiesForURL(cm.get(), url_google_);
2483 CookieList::const_iterator it = cookies.begin();
2484 ASSERT_TRUE(it != cookies.end());
2485 EXPECT_EQ("www.google.izzle", it->Domain());
2486 EXPECT_EQ("A", it->Name());
2487 ASSERT_TRUE(++it == cookies.end());
2488 GetCookieListCallback callback(&other_thread_);
2489 base::Closure task =
2490 base::Bind(&MultiThreadedCookieMonsterTest::GetAllCookiesForURLTask,
2491 base::Unretained(this), cm, url_google_, &callback);
2492 RunOnOtherThread(task);
2493 EXPECT_TRUE(callback.did_run());
2494 it = callback.cookies().begin();
2495 ASSERT_TRUE(it != callback.cookies().end());
2496 EXPECT_EQ("www.google.izzle", it->Domain());
2497 EXPECT_EQ("A", it->Name());
2498 ASSERT_TRUE(++it == callback.cookies().end());
2501 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookiesForURLWithOpt) {
2502 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2503 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "A=B"));
2504 CookieOptions options;
2505 CookieList cookies =
2506 GetAllCookiesForURLWithOptions(cm.get(), url_google_, options);
2507 CookieList::const_iterator it = cookies.begin();
2508 ASSERT_TRUE(it != cookies.end());
2509 EXPECT_EQ("www.google.izzle", it->Domain());
2510 EXPECT_EQ("A", it->Name());
2511 ASSERT_TRUE(++it == cookies.end());
2512 GetCookieListCallback callback(&other_thread_);
2513 base::Closure task = base::Bind(
2514 &MultiThreadedCookieMonsterTest::GetAllCookiesForURLWithOptionsTask,
2515 base::Unretained(this), cm, url_google_, options, &callback);
2516 RunOnOtherThread(task);
2517 EXPECT_TRUE(callback.did_run());
2518 it = callback.cookies().begin();
2519 ASSERT_TRUE(it != callback.cookies().end());
2520 EXPECT_EQ("www.google.izzle", it->Domain());
2521 EXPECT_EQ("A", it->Name());
2522 ASSERT_TRUE(++it == callback.cookies().end());
2525 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckSetCookieWithDetails) {
2526 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2527 EXPECT_TRUE(SetCookieWithDetails(cm.get(), url_google_foo_, "A", "B",
2528 std::string(), "/foo", base::Time(), false,
2529 false, false, COOKIE_PRIORITY_DEFAULT));
2530 ResultSavingCookieCallback<bool> callback(&other_thread_);
2531 base::Closure task =
2532 base::Bind(&MultiThreadedCookieMonsterTest::SetCookieWithDetailsTask,
2533 base::Unretained(this), cm, url_google_foo_, &callback);
2534 RunOnOtherThread(task);
2535 EXPECT_TRUE(callback.did_run());
2536 EXPECT_TRUE(callback.result());
2539 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteAllCreatedBetween) {
2540 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2541 CookieOptions options;
2542 Time now = Time::Now();
2543 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_, "A=B", options));
2544 EXPECT_EQ(1, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(99),
2545 Time()));
2546 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_, "A=B", options));
2547 ResultSavingCookieCallback<int> callback(&other_thread_);
2548 base::Closure task =
2549 base::Bind(&MultiThreadedCookieMonsterTest::DeleteAllCreatedBetweenTask,
2550 base::Unretained(this), cm, now - TimeDelta::FromDays(99),
2551 Time(), &callback);
2552 RunOnOtherThread(task);
2553 EXPECT_TRUE(callback.did_run());
2554 EXPECT_EQ(1, callback.result());
2557 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteAllForHost) {
2558 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2559 CookieOptions options;
2560 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_, "A=B", options));
2561 EXPECT_EQ(1, DeleteAllForHost(cm.get(), url_google_));
2562 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_, "A=B", options));
2563 ResultSavingCookieCallback<int> callback(&other_thread_);
2564 base::Closure task =
2565 base::Bind(&MultiThreadedCookieMonsterTest::DeleteAllForHostTask,
2566 base::Unretained(this), cm, url_google_, &callback);
2567 RunOnOtherThread(task);
2568 EXPECT_TRUE(callback.did_run());
2569 EXPECT_EQ(1, callback.result());
2572 TEST_F(MultiThreadedCookieMonsterTest,
2573 ThreadCheckDeleteAllCreatedBetweenForHost) {
2574 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2575 GURL url_not_google("http://www.notgoogle.com");
2577 CookieOptions options;
2578 Time now = Time::Now();
2579 // ago1 < ago2 < ago3 < now.
2580 Time ago1 = now - TimeDelta::FromDays(101);
2581 Time ago2 = now - TimeDelta::FromDays(100);
2582 Time ago3 = now - TimeDelta::FromDays(99);
2584 // These 3 cookies match the first deletion.
2585 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_, "A=B", options));
2586 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_, "C=D", options));
2587 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_, "Y=Z", options));
2589 // This cookie does not match host.
2590 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_not_google, "E=F", options));
2592 // This cookie does not match time range: [ago3, inf], for first deletion, but
2593 // matches for the second deletion.
2594 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "G=H", ago2));
2596 // 1. First set of deletions.
2597 EXPECT_EQ(
2598 3, // Deletes A=B, C=D, Y=Z
2599 DeleteAllCreatedBetweenForHost(cm.get(), ago3, Time::Max(), url_google_));
2601 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_, "A=B", options));
2602 ResultSavingCookieCallback<int> callback(&other_thread_);
2604 // 2. Second set of deletions.
2605 base::Closure task = base::Bind(
2606 &MultiThreadedCookieMonsterTest::DeleteAllCreatedBetweenForHostTask,
2607 base::Unretained(this), cm, ago1, Time(), url_google_, &callback);
2608 RunOnOtherThread(task);
2609 EXPECT_TRUE(callback.did_run());
2610 EXPECT_EQ(2, callback.result()); // Deletes A=B, G=H.
2613 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteCanonicalCookie) {
2614 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2615 CookieOptions options;
2616 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_, "A=B", options));
2617 CookieList cookies = GetAllCookies(cm.get());
2618 CookieList::iterator it = cookies.begin();
2619 EXPECT_TRUE(DeleteCanonicalCookie(cm.get(), *it));
2621 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_google_, "A=B", options));
2622 ResultSavingCookieCallback<bool> callback(&other_thread_);
2623 cookies = GetAllCookies(cm.get());
2624 it = cookies.begin();
2625 base::Closure task =
2626 base::Bind(&MultiThreadedCookieMonsterTest::DeleteCanonicalCookieTask,
2627 base::Unretained(this), cm, *it, &callback);
2628 RunOnOtherThread(task);
2629 EXPECT_TRUE(callback.did_run());
2630 EXPECT_TRUE(callback.result());
2633 // Ensure that cookies for http, https, ws, and wss all share the same storage
2634 // and policies when GetAllCookiesForURLAsync is used. This test is part of
2635 // MultiThreadedCookieMonsterTest in order to test and use
2636 // GetAllCookiesForURLAsync, but it does not use any additional threads.
2637 TEST_F(MultiThreadedCookieMonsterTest, GetAllCookiesForURLEffectiveDomain) {
2638 std::vector<CanonicalCookie*> cookies;
2639 // This cookie will be freed by the CookieMonster.
2640 cookies.push_back(CanonicalCookie::Create(url_google_, kValidCookieLine,
2641 Time::Now(), CookieOptions()));
2642 CanonicalCookie cookie = *cookies[0];
2643 scoped_refptr<NewMockPersistentCookieStore> store(
2644 new NewMockPersistentCookieStore);
2645 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL));
2647 CookieMonster::PersistentCookieStore::LoadedCallback loaded_callback;
2648 ::testing::StrictMock<::testing::MockFunction<void(int)>> checkpoint;
2649 const std::string key =
2650 cookie_util::GetEffectiveDomain(url_google_.scheme(), url_google_.host());
2652 ::testing::InSequence s;
2653 EXPECT_CALL(checkpoint, Call(0));
2654 EXPECT_CALL(*store, Load(::testing::_));
2655 EXPECT_CALL(*store, LoadCookiesForKey(key, ::testing::_))
2656 .WillOnce(::testing::SaveArg<1>(&loaded_callback));
2657 EXPECT_CALL(checkpoint, Call(1));
2658 // LoadCookiesForKey will never be called after checkpoint.Call(1) although
2659 // we will call GetAllCookiesForURLAsync again, because all URLs below share
2660 // the same key.
2661 EXPECT_CALL(*store, LoadCookiesForKey(::testing::_, ::testing::_)).Times(0);
2663 GetCookieListCallback callback;
2664 checkpoint.Call(0);
2665 GetAllCookiesForURLTask(cm.get(), url_google_, &callback);
2666 checkpoint.Call(1);
2667 ASSERT_FALSE(callback.did_run());
2668 // Pass the cookies to the CookieMonster.
2669 loaded_callback.Run(cookies);
2670 // Now GetAllCookiesForURLTask is done.
2671 ASSERT_TRUE(callback.did_run());
2672 // See that the callback was called with the cookies.
2673 ASSERT_EQ(1u, callback.cookies().size());
2674 EXPECT_TRUE(cookie.IsEquivalent(callback.cookies()[0]));
2676 // All urls in |urls| should share the same cookie domain.
2677 const GURL kUrls[] = {
2678 url_google_,
2679 url_google_secure_,
2680 GURL(kUrlGoogleWebSocket),
2681 GURL(kUrlGoogleWebSocketSecure),
2683 for (const GURL& url : kUrls) {
2684 // Call the function with |url| and verify it is done synchronously without
2685 // calling LoadCookiesForKey.
2686 GetCookieListCallback callback;
2687 GetAllCookiesForURLTask(cm.get(), url, &callback);
2688 ASSERT_TRUE(callback.did_run());
2689 ASSERT_EQ(1u, callback.cookies().size());
2690 EXPECT_TRUE(cookie.IsEquivalent(callback.cookies()[0]));
2694 TEST_F(CookieMonsterTest, InvalidExpiryTime) {
2695 std::string cookie_line =
2696 std::string(kValidCookieLine) + "; expires=Blarg arg arg";
2697 scoped_ptr<CanonicalCookie> cookie(CanonicalCookie::Create(
2698 url_google_, cookie_line, Time::Now(), CookieOptions()));
2699 ASSERT_FALSE(cookie->IsPersistent());
2702 // Test that CookieMonster writes session cookies into the underlying
2703 // CookieStore if the "persist session cookies" option is on.
2704 TEST_F(CookieMonsterTest, PersistSessionCookies) {
2705 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore);
2706 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL));
2707 cm->SetPersistSessionCookies(true);
2709 // All cookies set with SetCookie are session cookies.
2710 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "A=B"));
2711 EXPECT_EQ("A=B", GetCookies(cm.get(), url_google_));
2713 // The cookie was written to the backing store.
2714 EXPECT_EQ(1u, store->commands().size());
2715 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type);
2716 EXPECT_EQ("A", store->commands()[0].cookie.Name());
2717 EXPECT_EQ("B", store->commands()[0].cookie.Value());
2719 // Modify the cookie.
2720 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "A=C"));
2721 EXPECT_EQ("A=C", GetCookies(cm.get(), url_google_));
2722 EXPECT_EQ(3u, store->commands().size());
2723 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type);
2724 EXPECT_EQ("A", store->commands()[1].cookie.Name());
2725 EXPECT_EQ("B", store->commands()[1].cookie.Value());
2726 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[2].type);
2727 EXPECT_EQ("A", store->commands()[2].cookie.Name());
2728 EXPECT_EQ("C", store->commands()[2].cookie.Value());
2730 // Delete the cookie.
2731 DeleteCookie(cm.get(), url_google_, "A");
2732 EXPECT_EQ("", GetCookies(cm.get(), url_google_));
2733 EXPECT_EQ(4u, store->commands().size());
2734 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[3].type);
2735 EXPECT_EQ("A", store->commands()[3].cookie.Name());
2736 EXPECT_EQ("C", store->commands()[3].cookie.Value());
2739 // Test the commands sent to the persistent cookie store.
2740 TEST_F(CookieMonsterTest, PersisentCookieStorageTest) {
2741 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore);
2742 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL));
2744 // Add a cookie.
2745 EXPECT_TRUE(SetCookie(cm.get(), url_google_,
2746 "A=B; expires=Mon, 18-Apr-22 22:50:13 GMT"));
2747 this->MatchCookieLines("A=B", GetCookies(cm.get(), url_google_));
2748 ASSERT_EQ(1u, store->commands().size());
2749 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type);
2750 // Remove it.
2751 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "A=B; max-age=0"));
2752 this->MatchCookieLines(std::string(), GetCookies(cm.get(), url_google_));
2753 ASSERT_EQ(2u, store->commands().size());
2754 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type);
2756 // Add a cookie.
2757 EXPECT_TRUE(SetCookie(cm.get(), url_google_,
2758 "A=B; expires=Mon, 18-Apr-22 22:50:13 GMT"));
2759 this->MatchCookieLines("A=B", GetCookies(cm.get(), url_google_));
2760 ASSERT_EQ(3u, store->commands().size());
2761 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[2].type);
2762 // Overwrite it.
2763 EXPECT_TRUE(SetCookie(cm.get(), url_google_,
2764 "A=Foo; expires=Mon, 18-Apr-22 22:50:14 GMT"));
2765 this->MatchCookieLines("A=Foo", GetCookies(cm.get(), url_google_));
2766 ASSERT_EQ(5u, store->commands().size());
2767 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[3].type);
2768 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type);
2770 // Create some non-persistent cookies and check that they don't go to the
2771 // persistent storage.
2772 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "B=Bar"));
2773 this->MatchCookieLines("A=Foo; B=Bar", GetCookies(cm.get(), url_google_));
2774 EXPECT_EQ(5u, store->commands().size());
2777 // Test to assure that cookies with control characters are purged appropriately.
2778 // See http://crbug.com/238041 for background.
2779 TEST_F(CookieMonsterTest, ControlCharacterPurge) {
2780 const Time now1(Time::Now());
2781 const Time now2(Time::Now() + TimeDelta::FromSeconds(1));
2782 const Time now3(Time::Now() + TimeDelta::FromSeconds(2));
2783 const Time later(now1 + TimeDelta::FromDays(1));
2784 const GURL url("http://host/path");
2785 const std::string domain("host");
2786 const std::string path("/path");
2788 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore);
2790 std::vector<CanonicalCookie*> initial_cookies;
2792 AddCookieToList(domain, "foo=bar; path=" + path, now1, &initial_cookies);
2794 // We have to manually build this cookie because it contains a control
2795 // character, and our cookie line parser rejects control characters.
2796 CanonicalCookie* cc =
2797 new CanonicalCookie(url, "baz",
2798 "\x05"
2799 "boo",
2800 domain, path, now2, later, now2, false, false, false,
2801 COOKIE_PRIORITY_DEFAULT);
2802 initial_cookies.push_back(cc);
2804 AddCookieToList(domain, "hello=world; path=" + path, now3, &initial_cookies);
2806 // Inject our initial cookies into the mock PersistentCookieStore.
2807 store->SetLoadExpectation(true, initial_cookies);
2809 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL));
2811 EXPECT_EQ("foo=bar; hello=world", GetCookies(cm.get(), url));
2814 class CookieMonsterNotificationTest : public CookieMonsterTest {
2815 public:
2816 CookieMonsterNotificationTest()
2817 : test_url_("http://www.google.com/foo"),
2818 store_(new MockPersistentCookieStore),
2819 monster_(new CookieMonster(store_.get(), NULL)) {}
2821 ~CookieMonsterNotificationTest() override {}
2823 CookieMonster* monster() { return monster_.get(); }
2825 protected:
2826 const GURL test_url_;
2828 private:
2829 scoped_refptr<MockPersistentCookieStore> store_;
2830 scoped_refptr<CookieMonster> monster_;
2833 void RecordCookieChanges(std::vector<CanonicalCookie>* out_cookies,
2834 std::vector<bool>* out_removes,
2835 const CanonicalCookie& cookie,
2836 bool removed) {
2837 DCHECK(out_cookies);
2838 out_cookies->push_back(cookie);
2839 if (out_removes)
2840 out_removes->push_back(removed);
2843 TEST_F(CookieMonsterNotificationTest, NoNotifyWithNoCookie) {
2844 std::vector<CanonicalCookie> cookies;
2845 scoped_ptr<CookieStore::CookieChangedSubscription> sub(
2846 monster()->AddCallbackForCookie(
2847 test_url_, "abc",
2848 base::Bind(&RecordCookieChanges, &cookies, nullptr)));
2849 base::MessageLoop::current()->RunUntilIdle();
2850 EXPECT_EQ(0U, cookies.size());
2853 TEST_F(CookieMonsterNotificationTest, NoNotifyWithInitialCookie) {
2854 std::vector<CanonicalCookie> cookies;
2855 SetCookie(monster(), test_url_, "abc=def");
2856 base::MessageLoop::current()->RunUntilIdle();
2857 scoped_ptr<CookieStore::CookieChangedSubscription> sub(
2858 monster()->AddCallbackForCookie(
2859 test_url_, "abc",
2860 base::Bind(&RecordCookieChanges, &cookies, nullptr)));
2861 base::MessageLoop::current()->RunUntilIdle();
2862 EXPECT_EQ(0U, cookies.size());
2865 TEST_F(CookieMonsterNotificationTest, NotifyOnSet) {
2866 std::vector<CanonicalCookie> cookies;
2867 std::vector<bool> removes;
2868 scoped_ptr<CookieStore::CookieChangedSubscription> sub(
2869 monster()->AddCallbackForCookie(
2870 test_url_, "abc",
2871 base::Bind(&RecordCookieChanges, &cookies, &removes)));
2872 SetCookie(monster(), test_url_, "abc=def");
2873 base::MessageLoop::current()->RunUntilIdle();
2874 EXPECT_EQ(1U, cookies.size());
2875 EXPECT_EQ(1U, removes.size());
2877 EXPECT_EQ("abc", cookies[0].Name());
2878 EXPECT_EQ("def", cookies[0].Value());
2879 EXPECT_FALSE(removes[0]);
2882 TEST_F(CookieMonsterNotificationTest, NotifyOnDelete) {
2883 std::vector<CanonicalCookie> cookies;
2884 std::vector<bool> removes;
2885 scoped_ptr<CookieStore::CookieChangedSubscription> sub(
2886 monster()->AddCallbackForCookie(
2887 test_url_, "abc",
2888 base::Bind(&RecordCookieChanges, &cookies, &removes)));
2889 SetCookie(monster(), test_url_, "abc=def");
2890 base::MessageLoop::current()->RunUntilIdle();
2891 EXPECT_EQ(1U, cookies.size());
2892 EXPECT_EQ(1U, removes.size());
2894 DeleteCookie(monster(), test_url_, "abc");
2895 base::MessageLoop::current()->RunUntilIdle();
2896 EXPECT_EQ(2U, cookies.size());
2897 EXPECT_EQ(2U, removes.size());
2899 EXPECT_EQ("abc", cookies[1].Name());
2900 EXPECT_EQ("def", cookies[1].Value());
2901 EXPECT_TRUE(removes[1]);
2904 TEST_F(CookieMonsterNotificationTest, NotifyOnUpdate) {
2905 std::vector<CanonicalCookie> cookies;
2906 std::vector<bool> removes;
2907 scoped_ptr<CookieStore::CookieChangedSubscription> sub(
2908 monster()->AddCallbackForCookie(
2909 test_url_, "abc",
2910 base::Bind(&RecordCookieChanges, &cookies, &removes)));
2911 SetCookie(monster(), test_url_, "abc=def");
2912 base::MessageLoop::current()->RunUntilIdle();
2913 EXPECT_EQ(1U, cookies.size());
2915 // Replacing an existing cookie is actually a two-phase delete + set
2916 // operation, so we get an extra notification.
2917 SetCookie(monster(), test_url_, "abc=ghi");
2918 base::MessageLoop::current()->RunUntilIdle();
2920 EXPECT_EQ(3U, cookies.size());
2921 EXPECT_EQ(3U, removes.size());
2923 EXPECT_EQ("abc", cookies[1].Name());
2924 EXPECT_EQ("def", cookies[1].Value());
2925 EXPECT_TRUE(removes[1]);
2927 EXPECT_EQ("abc", cookies[2].Name());
2928 EXPECT_EQ("ghi", cookies[2].Value());
2929 EXPECT_FALSE(removes[2]);
2932 TEST_F(CookieMonsterNotificationTest, MultipleNotifies) {
2933 std::vector<CanonicalCookie> cookies0;
2934 std::vector<CanonicalCookie> cookies1;
2935 scoped_ptr<CookieStore::CookieChangedSubscription> sub0(
2936 monster()->AddCallbackForCookie(
2937 test_url_, "abc",
2938 base::Bind(&RecordCookieChanges, &cookies0, nullptr)));
2939 scoped_ptr<CookieStore::CookieChangedSubscription> sub1(
2940 monster()->AddCallbackForCookie(
2941 test_url_, "def",
2942 base::Bind(&RecordCookieChanges, &cookies1, nullptr)));
2943 SetCookie(monster(), test_url_, "abc=def");
2944 base::MessageLoop::current()->RunUntilIdle();
2945 EXPECT_EQ(1U, cookies0.size());
2946 EXPECT_EQ(0U, cookies1.size());
2947 SetCookie(monster(), test_url_, "def=abc");
2948 base::MessageLoop::current()->RunUntilIdle();
2949 EXPECT_EQ(1U, cookies0.size());
2950 EXPECT_EQ(1U, cookies1.size());
2953 TEST_F(CookieMonsterNotificationTest, MultipleSameNotifies) {
2954 std::vector<CanonicalCookie> cookies0;
2955 std::vector<CanonicalCookie> cookies1;
2956 scoped_ptr<CookieStore::CookieChangedSubscription> sub0(
2957 monster()->AddCallbackForCookie(
2958 test_url_, "abc",
2959 base::Bind(&RecordCookieChanges, &cookies0, nullptr)));
2960 scoped_ptr<CookieStore::CookieChangedSubscription> sub1(
2961 monster()->AddCallbackForCookie(
2962 test_url_, "abc",
2963 base::Bind(&RecordCookieChanges, &cookies1, nullptr)));
2964 SetCookie(monster(), test_url_, "abc=def");
2965 base::MessageLoop::current()->RunUntilIdle();
2966 EXPECT_EQ(1U, cookies0.size());
2967 EXPECT_EQ(1U, cookies0.size());
2970 } // namespace net