OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/cookies/cookie_store_unittest.h" | 5 #include "net/cookies/cookie_store_unittest.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
15 #include "base/metrics/histogram_samples.h" | 15 #include "base/metrics/histogram_samples.h" |
16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
17 #include "base/strings/string_tokenizer.h" | 17 #include "base/strings/string_tokenizer.h" |
18 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
19 #include "base/time.h" | 19 #include "base/time.h" |
20 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
21 #include "net/cookies/canonical_cookie.h" | 21 #include "net/cookies/canonical_cookie.h" |
| 22 #include "net/cookies/cookie_constants.h" |
22 #include "net/cookies/cookie_monster.h" | 23 #include "net/cookies/cookie_monster.h" |
23 #include "net/cookies/cookie_monster_store_test.h" // For CookieStore mock | 24 #include "net/cookies/cookie_monster_store_test.h" // For CookieStore mock |
24 #include "net/cookies/cookie_util.h" | 25 #include "net/cookies/cookie_util.h" |
25 #include "net/cookies/parsed_cookie.h" | 26 #include "net/cookies/parsed_cookie.h" |
26 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
28 | 29 |
29 namespace net { | 30 namespace net { |
30 | 31 |
31 using base::Time; | 32 using base::Time; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 return callback.cookies(); | 139 return callback.cookies(); |
139 } | 140 } |
140 | 141 |
141 bool SetCookieWithDetails(CookieMonster* cm, | 142 bool SetCookieWithDetails(CookieMonster* cm, |
142 const GURL& url, | 143 const GURL& url, |
143 const std::string& name, | 144 const std::string& name, |
144 const std::string& value, | 145 const std::string& value, |
145 const std::string& domain, | 146 const std::string& domain, |
146 const std::string& path, | 147 const std::string& path, |
147 const base::Time& expiration_time, | 148 const base::Time& expiration_time, |
148 bool secure, bool http_only) { | 149 bool secure, |
| 150 bool http_only, |
| 151 CookiePriority priority) { |
149 DCHECK(cm); | 152 DCHECK(cm); |
150 SetCookieCallback callback; | 153 SetCookieCallback callback; |
151 cm->SetCookieWithDetailsAsync( | 154 cm->SetCookieWithDetailsAsync( |
152 url, name, value, domain, path, expiration_time, secure, http_only, | 155 url, name, value, domain, path, expiration_time, secure, http_only, |
| 156 priority, |
153 base::Bind(&SetCookieCallback::Run, base::Unretained(&callback))); | 157 base::Bind(&SetCookieCallback::Run, base::Unretained(&callback))); |
154 RunFor(kTimeout); | 158 RunFor(kTimeout); |
155 EXPECT_TRUE(callback.did_run()); | 159 EXPECT_TRUE(callback.did_run()); |
156 return callback.result(); | 160 return callback.result(); |
157 } | 161 } |
158 | 162 |
159 int DeleteAll(CookieMonster*cm) { | 163 int DeleteAll(CookieMonster*cm) { |
160 DCHECK(cm); | 164 DCHECK(cm); |
161 DeleteCallback callback; | 165 DeleteCallback callback; |
162 cm->DeleteAllAsync( | 166 cm->DeleteAllAsync( |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 // Static population for probe: | 219 // Static population for probe: |
216 // * Three levels of domain cookie (.b.a, .c.b.a, .d.c.b.a) | 220 // * Three levels of domain cookie (.b.a, .c.b.a, .d.c.b.a) |
217 // * Three levels of host cookie (w.b.a, w.c.b.a, w.d.c.b.a) | 221 // * Three levels of host cookie (w.b.a, w.c.b.a, w.d.c.b.a) |
218 // * http_only cookie (w.c.b.a) | 222 // * http_only cookie (w.c.b.a) |
219 // * Two secure cookies (.c.b.a, w.c.b.a) | 223 // * Two secure cookies (.c.b.a, w.c.b.a) |
220 // * Two domain path cookies (.c.b.a/dir1, .c.b.a/dir1/dir2) | 224 // * Two domain path cookies (.c.b.a/dir1, .c.b.a/dir1/dir2) |
221 // * Two host path cookies (w.c.b.a/dir1, w.c.b.a/dir1/dir2) | 225 // * Two host path cookies (w.c.b.a/dir1, w.c.b.a/dir1/dir2) |
222 | 226 |
223 // Domain cookies | 227 // Domain cookies |
224 EXPECT_TRUE(this->SetCookieWithDetails(cm, url_top_level_domain_plus_1, | 228 EXPECT_TRUE(this->SetCookieWithDetails(cm, url_top_level_domain_plus_1, |
225 "dom_1", "X", ".harvard.edu", "/", | 229 "dom_1", "X", ".harvard.edu", "/", |
226 base::Time(), false, false)); | 230 base::Time(), false, false, |
| 231 PRIORITY_DEFAULT)); |
227 EXPECT_TRUE(this->SetCookieWithDetails(cm, url_top_level_domain_plus_2, | 232 EXPECT_TRUE(this->SetCookieWithDetails(cm, url_top_level_domain_plus_2, |
228 "dom_2", "X", ".math.harvard.edu", | 233 "dom_2", "X", ".math.harvard.edu", |
229 "/", base::Time(), false, false)); | 234 "/", base::Time(), false, false, |
| 235 PRIORITY_DEFAULT)); |
230 EXPECT_TRUE(this->SetCookieWithDetails(cm, url_top_level_domain_plus_3, | 236 EXPECT_TRUE(this->SetCookieWithDetails(cm, url_top_level_domain_plus_3, |
231 "dom_3", "X", | 237 "dom_3", "X", |
232 ".bourbaki.math.harvard.edu", "/", | 238 ".bourbaki.math.harvard.edu", "/", |
233 base::Time(), false, false)); | 239 base::Time(), false, false, |
| 240 PRIORITY_DEFAULT)); |
234 | 241 |
235 // Host cookies | 242 // Host cookies |
236 EXPECT_TRUE(this->SetCookieWithDetails(cm, | 243 EXPECT_TRUE(this->SetCookieWithDetails(cm, |
237 url_top_level_domain_plus_1, | 244 url_top_level_domain_plus_1, |
238 "host_1", | 245 "host_1", |
239 "X", | 246 "X", |
240 std::string(), | 247 std::string(), |
241 "/", | 248 "/", |
242 base::Time(), | 249 base::Time(), |
243 false, | 250 false, |
244 false)); | 251 false, |
| 252 PRIORITY_DEFAULT)); |
245 EXPECT_TRUE(this->SetCookieWithDetails(cm, | 253 EXPECT_TRUE(this->SetCookieWithDetails(cm, |
246 url_top_level_domain_plus_2, | 254 url_top_level_domain_plus_2, |
247 "host_2", | 255 "host_2", |
248 "X", | 256 "X", |
249 std::string(), | 257 std::string(), |
250 "/", | 258 "/", |
251 base::Time(), | 259 base::Time(), |
252 false, | 260 false, |
253 false)); | 261 false, |
| 262 PRIORITY_DEFAULT)); |
254 EXPECT_TRUE(this->SetCookieWithDetails(cm, | 263 EXPECT_TRUE(this->SetCookieWithDetails(cm, |
255 url_top_level_domain_plus_3, | 264 url_top_level_domain_plus_3, |
256 "host_3", | 265 "host_3", |
257 "X", | 266 "X", |
258 std::string(), | 267 std::string(), |
259 "/", | 268 "/", |
260 base::Time(), | 269 base::Time(), |
261 false, | 270 false, |
262 false)); | 271 false, |
| 272 PRIORITY_DEFAULT)); |
263 | 273 |
264 // Http_only cookie | 274 // Http_only cookie |
265 EXPECT_TRUE(this->SetCookieWithDetails(cm, | 275 EXPECT_TRUE(this->SetCookieWithDetails(cm, |
266 url_top_level_domain_plus_2, | 276 url_top_level_domain_plus_2, |
267 "httpo_check", | 277 "httpo_check", |
268 "X", | 278 "X", |
269 std::string(), | 279 std::string(), |
270 "/", | 280 "/", |
271 base::Time(), | 281 base::Time(), |
272 false, | 282 false, |
273 true)); | 283 true, |
| 284 PRIORITY_DEFAULT)); |
274 | 285 |
275 // Secure cookies | 286 // Secure cookies |
276 EXPECT_TRUE(this->SetCookieWithDetails(cm, | 287 EXPECT_TRUE(this->SetCookieWithDetails(cm, |
277 url_top_level_domain_plus_2_secure, | 288 url_top_level_domain_plus_2_secure, |
278 "sec_dom", "X", ".math.harvard.edu", | 289 "sec_dom", "X", ".math.harvard.edu", |
279 "/", base::Time(), true, false)); | 290 "/", base::Time(), true, false, |
| 291 PRIORITY_DEFAULT)); |
280 EXPECT_TRUE(this->SetCookieWithDetails(cm, | 292 EXPECT_TRUE(this->SetCookieWithDetails(cm, |
281 url_top_level_domain_plus_2_secure, | 293 url_top_level_domain_plus_2_secure, |
282 "sec_host", | 294 "sec_host", |
283 "X", | 295 "X", |
284 std::string(), | 296 std::string(), |
285 "/", | 297 "/", |
286 base::Time(), | 298 base::Time(), |
287 true, | 299 true, |
288 false)); | 300 false, |
| 301 PRIORITY_DEFAULT)); |
289 | 302 |
290 // Domain path cookies | 303 // Domain path cookies |
291 EXPECT_TRUE(this->SetCookieWithDetails(cm, url_top_level_domain_plus_2, | 304 EXPECT_TRUE(this->SetCookieWithDetails(cm, url_top_level_domain_plus_2, |
292 "dom_path_1", "X", | 305 "dom_path_1", "X", |
293 ".math.harvard.edu", "/dir1", | 306 ".math.harvard.edu", "/dir1", |
294 base::Time(), false, false)); | 307 base::Time(), false, false, |
| 308 PRIORITY_DEFAULT)); |
295 EXPECT_TRUE(this->SetCookieWithDetails(cm, url_top_level_domain_plus_2, | 309 EXPECT_TRUE(this->SetCookieWithDetails(cm, url_top_level_domain_plus_2, |
296 "dom_path_2", "X", | 310 "dom_path_2", "X", |
297 ".math.harvard.edu", "/dir1/dir2", | 311 ".math.harvard.edu", "/dir1/dir2", |
298 base::Time(), false, false)); | 312 base::Time(), false, false, |
| 313 PRIORITY_DEFAULT)); |
299 | 314 |
300 // Host path cookies | 315 // Host path cookies |
301 EXPECT_TRUE(this->SetCookieWithDetails(cm, | 316 EXPECT_TRUE(this->SetCookieWithDetails(cm, |
302 url_top_level_domain_plus_2, | 317 url_top_level_domain_plus_2, |
303 "host_path_1", | 318 "host_path_1", |
304 "X", | 319 "X", |
305 std::string(), | 320 std::string(), |
306 "/dir1", | 321 "/dir1", |
307 base::Time(), | 322 base::Time(), |
308 false, | 323 false, |
309 false)); | 324 false, |
| 325 PRIORITY_DEFAULT)); |
310 EXPECT_TRUE(this->SetCookieWithDetails(cm, | 326 EXPECT_TRUE(this->SetCookieWithDetails(cm, |
311 url_top_level_domain_plus_2, | 327 url_top_level_domain_plus_2, |
312 "host_path_2", | 328 "host_path_2", |
313 "X", | 329 "X", |
314 std::string(), | 330 std::string(), |
315 "/dir1/dir2", | 331 "/dir1/dir2", |
316 base::Time(), | 332 base::Time(), |
317 false, | 333 false, |
318 false)); | 334 false, |
| 335 PRIORITY_DEFAULT)); |
319 | 336 |
320 EXPECT_EQ(13U, this->GetAllCookies(cm).size()); | 337 EXPECT_EQ(13U, this->GetAllCookies(cm).size()); |
321 } | 338 } |
322 | 339 |
323 Time GetFirstCookieAccessDate(CookieMonster* cm) { | 340 Time GetFirstCookieAccessDate(CookieMonster* cm) { |
324 const CookieList all_cookies(this->GetAllCookies(cm)); | 341 const CookieList all_cookies(this->GetAllCookies(cm)); |
325 return all_cookies.front().LastAccessDate(); | 342 return all_cookies.front().LastAccessDate(); |
326 } | 343 } |
327 | 344 |
328 bool FindAndDeleteCookie(CookieMonster* cm, | 345 bool FindAndDeleteCookie(CookieMonster* cm, |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 MOCK_METHOD1(Invoke, void(int num_deleted)); | 462 MOCK_METHOD1(Invoke, void(int num_deleted)); |
446 }; | 463 }; |
447 | 464 |
448 class MockDeleteCookieCallback | 465 class MockDeleteCookieCallback |
449 : public MockCookieCallback<MockDeleteCookieCallback, | 466 : public MockCookieCallback<MockDeleteCookieCallback, |
450 CookieMonster::DeleteCookieCallback> { | 467 CookieMonster::DeleteCookieCallback> { |
451 public: | 468 public: |
452 MOCK_METHOD1(Invoke, void(bool success)); | 469 MOCK_METHOD1(Invoke, void(bool success)); |
453 }; | 470 }; |
454 | 471 |
| 472 struct CookiesInputInfo { |
| 473 const GURL url; |
| 474 const std::string name; |
| 475 const std::string value; |
| 476 const std::string domain; |
| 477 const std::string path; |
| 478 const base::Time expiration_time; |
| 479 bool secure; |
| 480 bool http_only; |
| 481 CookiePriority priority; |
| 482 }; |
| 483 |
455 ACTION(QuitCurrentMessageLoop) { | 484 ACTION(QuitCurrentMessageLoop) { |
456 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 485 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
457 } | 486 } |
458 | 487 |
459 // TODO(erikwright): When the synchronous helpers 'GetCookies' etc. are removed, | 488 // TODO(erikwright): When the synchronous helpers 'GetCookies' etc. are removed, |
460 // rename these, removing the 'Action' suffix. | 489 // rename these, removing the 'Action' suffix. |
461 ACTION_P4(DeleteCookieAction, cookie_monster, url, name, callback) { | 490 ACTION_P4(DeleteCookieAction, cookie_monster, url, name, callback) { |
462 cookie_monster->DeleteCookieAsync(url, name, callback->AsCallback()); | 491 cookie_monster->DeleteCookieAsync(url, name, callback->AsCallback()); |
463 } | 492 } |
464 ACTION_P3(GetCookiesAction, cookie_monster, url, callback) { | 493 ACTION_P3(GetCookiesAction, cookie_monster, url, callback) { |
465 cookie_monster->GetCookiesWithOptionsAsync( | 494 cookie_monster->GetCookiesWithOptionsAsync( |
466 url, CookieOptions(), callback->AsCallback()); | 495 url, CookieOptions(), callback->AsCallback()); |
467 } | 496 } |
468 ACTION_P4(SetCookieAction, cookie_monster, url, cookie_line, callback) { | 497 ACTION_P4(SetCookieAction, cookie_monster, url, cookie_line, callback) { |
469 cookie_monster->SetCookieWithOptionsAsync( | 498 cookie_monster->SetCookieWithOptionsAsync( |
470 url, cookie_line, CookieOptions(), callback->AsCallback()); | 499 url, cookie_line, CookieOptions(), callback->AsCallback()); |
471 } | 500 } |
472 ACTION_P4(DeleteAllCreatedBetweenAction, | 501 ACTION_P4(DeleteAllCreatedBetweenAction, |
473 cookie_monster, delete_begin, delete_end, callback) { | 502 cookie_monster, delete_begin, delete_end, callback) { |
474 cookie_monster->DeleteAllCreatedBetweenAsync( | 503 cookie_monster->DeleteAllCreatedBetweenAsync( |
475 delete_begin, delete_end, callback->AsCallback()); | 504 delete_begin, delete_end, callback->AsCallback()); |
476 } | 505 } |
477 ACTION_P10(SetCookieWithDetailsAction, | 506 ACTION_P3(SetCookieWithDetailsAction, cookie_monster, cc, callback) { |
478 cookie_monster, url, name, value, domain, path, expiration_time, | |
479 secure, http_only, callback) { | |
480 cookie_monster->SetCookieWithDetailsAsync( | 507 cookie_monster->SetCookieWithDetailsAsync( |
481 url, name, value, domain, path, expiration_time, secure, http_only, | 508 cc.url, cc.name, cc.value, cc.domain, cc.path, cc.expiration_time, |
| 509 cc.secure, cc.http_only, cc.priority, |
482 callback->AsCallback()); | 510 callback->AsCallback()); |
483 } | 511 } |
484 | 512 |
485 ACTION_P2(GetAllCookiesAction, cookie_monster, callback) { | 513 ACTION_P2(GetAllCookiesAction, cookie_monster, callback) { |
486 cookie_monster->GetAllCookiesAsync(callback->AsCallback()); | 514 cookie_monster->GetAllCookiesAsync(callback->AsCallback()); |
487 } | 515 } |
488 | 516 |
489 ACTION_P3(DeleteAllForHostAction, cookie_monster, url, callback) { | 517 ACTION_P3(DeleteAllForHostAction, cookie_monster, url, callback) { |
490 cookie_monster->DeleteAllForHostAsync(url, callback->AsCallback()); | 518 cookie_monster->DeleteAllForHostAsync(url, callback->AsCallback()); |
491 } | 519 } |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 &cookie_monster(), url_google_, "X", &delete_cookie_callback)); | 704 &cookie_monster(), url_google_, "X", &delete_cookie_callback)); |
677 EXPECT_CALL(delete_cookie_callback, Invoke()).WillOnce( | 705 EXPECT_CALL(delete_cookie_callback, Invoke()).WillOnce( |
678 QuitCurrentMessageLoop()); | 706 QuitCurrentMessageLoop()); |
679 | 707 |
680 CompleteLoadingAndWait(); | 708 CompleteLoadingAndWait(); |
681 } | 709 } |
682 | 710 |
683 TEST_F(DeferredCookieTaskTest, DeferredSetCookieWithDetails) { | 711 TEST_F(DeferredCookieTaskTest, DeferredSetCookieWithDetails) { |
684 MockSetCookiesCallback set_cookies_callback; | 712 MockSetCookiesCallback set_cookies_callback; |
685 | 713 |
| 714 CookiesInputInfo cookie_info = { |
| 715 url_google_foo_, "A", "B", std::string(), "/foo", |
| 716 base::Time(), false, false, PRIORITY_DEFAULT |
| 717 }; |
686 BeginWithForDomainKey("google.izzle", SetCookieWithDetailsAction( | 718 BeginWithForDomainKey("google.izzle", SetCookieWithDetailsAction( |
687 &cookie_monster(), url_google_foo_, "A", "B", std::string(), "/foo", | 719 &cookie_monster(), cookie_info, &set_cookies_callback)); |
688 base::Time(), false, false, &set_cookies_callback)); | |
689 | 720 |
690 WaitForLoadCall(); | 721 WaitForLoadCall(); |
691 | 722 |
| 723 CookiesInputInfo cookie_info_exp = { |
| 724 url_google_foo_, "A", "B", std::string(), "/foo", |
| 725 base::Time(), false, false, PRIORITY_DEFAULT |
| 726 }; |
692 EXPECT_CALL(set_cookies_callback, Invoke(true)).WillOnce( | 727 EXPECT_CALL(set_cookies_callback, Invoke(true)).WillOnce( |
693 SetCookieWithDetailsAction( | 728 SetCookieWithDetailsAction( |
694 &cookie_monster(), url_google_foo_, "A", "B", std::string(), "/foo", | 729 &cookie_monster(), cookie_info_exp, &set_cookies_callback)); |
695 base::Time(), false, false, &set_cookies_callback)); | |
696 EXPECT_CALL(set_cookies_callback, Invoke(true)).WillOnce( | 730 EXPECT_CALL(set_cookies_callback, Invoke(true)).WillOnce( |
697 QuitCurrentMessageLoop()); | 731 QuitCurrentMessageLoop()); |
698 | 732 |
699 CompleteLoadingAndWait(); | 733 CompleteLoadingAndWait(); |
700 } | 734 } |
701 | 735 |
702 TEST_F(DeferredCookieTaskTest, DeferredGetAllCookies) { | 736 TEST_F(DeferredCookieTaskTest, DeferredGetAllCookies) { |
703 DeclareLoadedCookie("www.google.izzle", | 737 DeclareLoadedCookie("www.google.izzle", |
704 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT", | 738 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT", |
705 Time::Now() + TimeDelta::FromDays(3)); | 739 Time::Now() + TimeDelta::FromDays(3)); |
(...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1377 EXPECT_EQ("a", delegate->changes()[1].first.Name()); | 1411 EXPECT_EQ("a", delegate->changes()[1].first.Name()); |
1378 EXPECT_EQ("val2", delegate->changes()[1].first.Value()); | 1412 EXPECT_EQ("val2", delegate->changes()[1].first.Value()); |
1379 delegate->reset(); | 1413 delegate->reset(); |
1380 } | 1414 } |
1381 | 1415 |
1382 TEST_F(CookieMonsterTest, SetCookieWithDetails) { | 1416 TEST_F(CookieMonsterTest, SetCookieWithDetails) { |
1383 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 1417 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
1384 | 1418 |
1385 EXPECT_TRUE(SetCookieWithDetails( | 1419 EXPECT_TRUE(SetCookieWithDetails( |
1386 cm, url_google_foo_, "A", "B", std::string(), "/foo", base::Time(), | 1420 cm, url_google_foo_, "A", "B", std::string(), "/foo", base::Time(), |
1387 false, false)); | 1421 false, false, PRIORITY_DEFAULT)); |
1388 EXPECT_TRUE(SetCookieWithDetails( | 1422 EXPECT_TRUE(SetCookieWithDetails( |
1389 cm, url_google_bar_, "C", "D", "google.izzle", "/bar", base::Time(), | 1423 cm, url_google_bar_, "C", "D", "google.izzle", "/bar", base::Time(), |
1390 false, true)); | 1424 false, true, PRIORITY_DEFAULT)); |
1391 EXPECT_TRUE(SetCookieWithDetails( | 1425 EXPECT_TRUE(SetCookieWithDetails( |
1392 cm, url_google_, "E", "F", std::string(), std::string(), base::Time(), | 1426 cm, url_google_, "E", "F", std::string(), std::string(), base::Time(), |
1393 true, false)); | 1427 true, false, PRIORITY_DEFAULT)); |
1394 | 1428 |
1395 // Test that malformed attributes fail to set the cookie. | 1429 // Test that malformed attributes fail to set the cookie. |
1396 EXPECT_FALSE(SetCookieWithDetails( | 1430 EXPECT_FALSE(SetCookieWithDetails( |
1397 cm, url_google_foo_, " A", "B", std::string(), "/foo", base::Time(), | 1431 cm, url_google_foo_, " A", "B", std::string(), "/foo", base::Time(), |
1398 false, false)); | 1432 false, false, PRIORITY_DEFAULT)); |
1399 EXPECT_FALSE(SetCookieWithDetails( | 1433 EXPECT_FALSE(SetCookieWithDetails( |
1400 cm, url_google_foo_, "A;", "B", std::string(), "/foo", base::Time(), | 1434 cm, url_google_foo_, "A;", "B", std::string(), "/foo", base::Time(), |
1401 false, false)); | 1435 false, false, PRIORITY_DEFAULT)); |
1402 EXPECT_FALSE(SetCookieWithDetails( | 1436 EXPECT_FALSE(SetCookieWithDetails( |
1403 cm, url_google_foo_, "A=", "B", std::string(), "/foo", base::Time(), | 1437 cm, url_google_foo_, "A=", "B", std::string(), "/foo", base::Time(), |
1404 false, false)); | 1438 false, false, PRIORITY_DEFAULT)); |
1405 EXPECT_FALSE(SetCookieWithDetails( | 1439 EXPECT_FALSE(SetCookieWithDetails( |
1406 cm, url_google_foo_, "A", "B", "google.ozzzzzzle", "foo", base::Time(), | 1440 cm, url_google_foo_, "A", "B", "google.ozzzzzzle", "foo", base::Time(), |
1407 false, false)); | 1441 false, false, PRIORITY_DEFAULT)); |
1408 EXPECT_FALSE(SetCookieWithDetails( | 1442 EXPECT_FALSE(SetCookieWithDetails( |
1409 cm, url_google_foo_, "A=", "B", std::string(), "foo", base::Time(), | 1443 cm, url_google_foo_, "A=", "B", std::string(), "foo", base::Time(), |
1410 false, false)); | 1444 false, false, PRIORITY_DEFAULT)); |
1411 | 1445 |
1412 CookieList cookies = GetAllCookiesForURL(cm, url_google_foo_); | 1446 CookieList cookies = GetAllCookiesForURL(cm, url_google_foo_); |
1413 CookieList::iterator it = cookies.begin(); | 1447 CookieList::iterator it = cookies.begin(); |
1414 | 1448 |
1415 ASSERT_TRUE(it != cookies.end()); | 1449 ASSERT_TRUE(it != cookies.end()); |
1416 EXPECT_EQ("A", it->Name()); | 1450 EXPECT_EQ("A", it->Name()); |
1417 EXPECT_EQ("B", it->Value()); | 1451 EXPECT_EQ("B", it->Value()); |
1418 EXPECT_EQ("www.google.izzle", it->Domain()); | 1452 EXPECT_EQ("www.google.izzle", it->Domain()); |
1419 EXPECT_EQ("/foo", it->Path()); | 1453 EXPECT_EQ("/foo", it->Path()); |
1420 EXPECT_FALSE(it->IsPersistent()); | 1454 EXPECT_FALSE(it->IsPersistent()); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1528 | 1562 |
1529 SetCookie(cm, url_google_, "SetCookie1=A"); | 1563 SetCookie(cm, url_google_, "SetCookie1=A"); |
1530 SetCookie(cm, url_google_, "SetCookie2=A"); | 1564 SetCookie(cm, url_google_, "SetCookie2=A"); |
1531 SetCookie(cm, url_google_, "SetCookie3=A"); | 1565 SetCookie(cm, url_google_, "SetCookie3=A"); |
1532 | 1566 |
1533 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions1=A", options); | 1567 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions1=A", options); |
1534 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions2=A", options); | 1568 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions2=A", options); |
1535 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions3=A", options); | 1569 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions3=A", options); |
1536 | 1570 |
1537 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails1", "A", | 1571 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails1", "A", |
1538 ".google.com", "/", Time(), false, false); | 1572 ".google.com", "/", Time(), false, false, |
| 1573 PRIORITY_DEFAULT); |
1539 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails2", "A", | 1574 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails2", "A", |
1540 ".google.com", "/", Time(), false, false); | 1575 ".google.com", "/", Time(), false, false, |
| 1576 PRIORITY_DEFAULT); |
1541 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails3", "A", | 1577 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails3", "A", |
1542 ".google.com", "/", Time(), false, false); | 1578 ".google.com", "/", Time(), false, false, |
| 1579 PRIORITY_DEFAULT); |
1543 | 1580 |
1544 // Now we check | 1581 // Now we check |
1545 CookieList cookie_list(GetAllCookies(cm)); | 1582 CookieList cookie_list(GetAllCookies(cm)); |
1546 typedef std::map<int64, CanonicalCookie> TimeCookieMap; | 1583 typedef std::map<int64, CanonicalCookie> TimeCookieMap; |
1547 TimeCookieMap check_map; | 1584 TimeCookieMap check_map; |
1548 for (CookieList::const_iterator it = cookie_list.begin(); | 1585 for (CookieList::const_iterator it = cookie_list.begin(); |
1549 it != cookie_list.end(); it++) { | 1586 it != cookie_list.end(); it++) { |
1550 const int64 creation_date = it->CreationDate().ToInternalValue(); | 1587 const int64 creation_date = it->CreationDate().ToInternalValue(); |
1551 TimeCookieMap::const_iterator | 1588 TimeCookieMap::const_iterator |
1552 existing_cookie_it(check_map.find(creation_date)); | 1589 existing_cookie_it(check_map.find(creation_date)); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1590 // Test that cookies transfer from/to the backing store correctly. | 1627 // Test that cookies transfer from/to the backing store correctly. |
1591 TEST_F(CookieMonsterTest, BackingStoreCommunication) { | 1628 TEST_F(CookieMonsterTest, BackingStoreCommunication) { |
1592 // Store details for cookies transforming through the backing store interface. | 1629 // Store details for cookies transforming through the backing store interface. |
1593 | 1630 |
1594 base::Time current(base::Time::Now()); | 1631 base::Time current(base::Time::Now()); |
1595 scoped_refptr<MockSimplePersistentCookieStore> store( | 1632 scoped_refptr<MockSimplePersistentCookieStore> store( |
1596 new MockSimplePersistentCookieStore); | 1633 new MockSimplePersistentCookieStore); |
1597 base::Time new_access_time; | 1634 base::Time new_access_time; |
1598 base::Time expires(base::Time::Now() + base::TimeDelta::FromSeconds(100)); | 1635 base::Time expires(base::Time::Now() + base::TimeDelta::FromSeconds(100)); |
1599 | 1636 |
1600 struct CookiesInputInfo { | |
1601 std::string gurl; | |
1602 std::string name; | |
1603 std::string value; | |
1604 std::string domain; | |
1605 std::string path; | |
1606 base::Time expires; | |
1607 bool secure; | |
1608 bool http_only; | |
1609 }; | |
1610 const CookiesInputInfo input_info[] = { | 1637 const CookiesInputInfo input_info[] = { |
1611 {"http://a.b.google.com", "a", "1", "", "/path/to/cookie", expires, | 1638 {GURL("http://a.b.google.com"), "a", "1", "", "/path/to/cookie", expires, |
1612 false, false}, | 1639 false, false, PRIORITY_DEFAULT}, |
1613 {"https://www.google.com", "b", "2", ".google.com", "/path/from/cookie", | 1640 {GURL("https://www.google.com"), "b", "2", ".google.com", |
1614 expires + TimeDelta::FromSeconds(10), true, true}, | 1641 "/path/from/cookie", expires + TimeDelta::FromSeconds(10), true, true, |
1615 {"https://google.com", "c", "3", "", "/another/path/to/cookie", | 1642 PRIORITY_DEFAULT}, |
| 1643 {GURL("https://google.com"), "c", "3", "", "/another/path/to/cookie", |
1616 base::Time::Now() + base::TimeDelta::FromSeconds(100), | 1644 base::Time::Now() + base::TimeDelta::FromSeconds(100), |
1617 true, false} | 1645 true, false, PRIORITY_DEFAULT} |
1618 }; | 1646 }; |
1619 const int INPUT_DELETE = 1; | 1647 const int INPUT_DELETE = 1; |
1620 | 1648 |
1621 // Create new cookies and flush them to the store. | 1649 // Create new cookies and flush them to the store. |
1622 { | 1650 { |
1623 scoped_refptr<CookieMonster> cmout(new CookieMonster(store, NULL)); | 1651 scoped_refptr<CookieMonster> cmout(new CookieMonster(store, NULL)); |
1624 for (const CookiesInputInfo* p = input_info; | 1652 for (const CookiesInputInfo* p = input_info; |
1625 p < &input_info[ARRAYSIZE_UNSAFE(input_info)]; p++) { | 1653 p < &input_info[ARRAYSIZE_UNSAFE(input_info)]; p++) { |
1626 EXPECT_TRUE(SetCookieWithDetails(cmout, GURL(p->gurl), p->name, p->value, | 1654 EXPECT_TRUE(SetCookieWithDetails(cmout, p->url, p->name, p->value, |
1627 p->domain, p->path, p->expires, | 1655 p->domain, p->path, p->expiration_time, |
1628 p->secure, p->http_only)); | 1656 p->secure, p->http_only, p->priority)); |
1629 } | 1657 } |
1630 DeleteCookie(cmout, GURL(std::string(input_info[INPUT_DELETE].gurl) + | 1658 GURL del_url(input_info[INPUT_DELETE].url.Resolve( |
1631 input_info[INPUT_DELETE].path), | 1659 input_info[INPUT_DELETE].path).spec()); |
1632 input_info[INPUT_DELETE].name); | 1660 DeleteCookie(cmout, del_url, input_info[INPUT_DELETE].name); |
1633 } | 1661 } |
1634 | 1662 |
1635 // Create a new cookie monster and make sure that everything is correct | 1663 // Create a new cookie monster and make sure that everything is correct |
1636 { | 1664 { |
1637 scoped_refptr<CookieMonster> cmin(new CookieMonster(store, NULL)); | 1665 scoped_refptr<CookieMonster> cmin(new CookieMonster(store, NULL)); |
1638 CookieList cookies(GetAllCookies(cmin)); | 1666 CookieList cookies(GetAllCookies(cmin)); |
1639 ASSERT_EQ(2u, cookies.size()); | 1667 ASSERT_EQ(2u, cookies.size()); |
1640 // Ordering is path length, then creation time. So second cookie | 1668 // Ordering is path length, then creation time. So second cookie |
1641 // will come first, and we need to swap them. | 1669 // will come first, and we need to swap them. |
1642 std::swap(cookies[0], cookies[1]); | 1670 std::swap(cookies[0], cookies[1]); |
1643 for (int output_index = 0; output_index < 2; output_index++) { | 1671 for (int output_index = 0; output_index < 2; output_index++) { |
1644 int input_index = output_index * 2; | 1672 int input_index = output_index * 2; |
1645 const CookiesInputInfo* input = &input_info[input_index]; | 1673 const CookiesInputInfo* input = &input_info[input_index]; |
1646 const CanonicalCookie* output = &cookies[output_index]; | 1674 const CanonicalCookie* output = &cookies[output_index]; |
1647 | 1675 |
1648 EXPECT_EQ(input->name, output->Name()); | 1676 EXPECT_EQ(input->name, output->Name()); |
1649 EXPECT_EQ(input->value, output->Value()); | 1677 EXPECT_EQ(input->value, output->Value()); |
1650 EXPECT_EQ(GURL(input->gurl).host(), output->Domain()); | 1678 EXPECT_EQ(input->url.host(), output->Domain()); |
1651 EXPECT_EQ(input->path, output->Path()); | 1679 EXPECT_EQ(input->path, output->Path()); |
1652 EXPECT_LE(current.ToInternalValue(), | 1680 EXPECT_LE(current.ToInternalValue(), |
1653 output->CreationDate().ToInternalValue()); | 1681 output->CreationDate().ToInternalValue()); |
1654 EXPECT_EQ(input->secure, output->IsSecure()); | 1682 EXPECT_EQ(input->secure, output->IsSecure()); |
1655 EXPECT_EQ(input->http_only, output->IsHttpOnly()); | 1683 EXPECT_EQ(input->http_only, output->IsHttpOnly()); |
1656 EXPECT_TRUE(output->IsPersistent()); | 1684 EXPECT_TRUE(output->IsPersistent()); |
1657 EXPECT_EQ(input->expires.ToInternalValue(), | 1685 EXPECT_EQ(input->expiration_time.ToInternalValue(), |
1658 output->ExpiryDate().ToInternalValue()); | 1686 output->ExpiryDate().ToInternalValue()); |
1659 } | 1687 } |
1660 } | 1688 } |
1661 } | 1689 } |
1662 | 1690 |
1663 TEST_F(CookieMonsterTest, CookieListOrdering) { | 1691 TEST_F(CookieMonsterTest, CookieListOrdering) { |
1664 // Put a random set of cookies into a monster and make sure | 1692 // Put a random set of cookies into a monster and make sure |
1665 // they're returned in the right order. | 1693 // they're returned in the right order. |
1666 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 1694 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
1667 EXPECT_TRUE(SetCookie(cm, GURL("http://d.c.b.a.google.com/aa/x.html"), | 1695 EXPECT_TRUE(SetCookie(cm, GURL("http://d.c.b.a.google.com/aa/x.html"), |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1931 base::HistogramBase* expired_histogram = | 1959 base::HistogramBase* expired_histogram = |
1932 base::Histogram::FactoryGet( | 1960 base::Histogram::FactoryGet( |
1933 "Cookie.ExpirationDurationMinutes", 1, 10 * 365 * 24 * 60, 50, | 1961 "Cookie.ExpirationDurationMinutes", 1, 10 * 365 * 24 * 60, 50, |
1934 base::Histogram::kUmaTargetedHistogramFlag); | 1962 base::Histogram::kUmaTargetedHistogramFlag); |
1935 | 1963 |
1936 scoped_ptr<base::HistogramSamples> samples1( | 1964 scoped_ptr<base::HistogramSamples> samples1( |
1937 expired_histogram->SnapshotSamples()); | 1965 expired_histogram->SnapshotSamples()); |
1938 ASSERT_TRUE(SetCookieWithDetails( | 1966 ASSERT_TRUE(SetCookieWithDetails( |
1939 cm, GURL("http://fake.a.url"), "a", "b", "a.url", "/", | 1967 cm, GURL("http://fake.a.url"), "a", "b", "a.url", "/", |
1940 base::Time::Now() + base::TimeDelta::FromMinutes(59), | 1968 base::Time::Now() + base::TimeDelta::FromMinutes(59), |
1941 false, false)); | 1969 false, false, PRIORITY_DEFAULT)); |
1942 | 1970 |
1943 scoped_ptr<base::HistogramSamples> samples2( | 1971 scoped_ptr<base::HistogramSamples> samples2( |
1944 expired_histogram->SnapshotSamples()); | 1972 expired_histogram->SnapshotSamples()); |
1945 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount()); | 1973 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount()); |
1946 | 1974 |
1947 // kValidCookieLine creates a session cookie. | 1975 // kValidCookieLine creates a session cookie. |
1948 ASSERT_TRUE(SetCookie(cm, url_google_, kValidCookieLine)); | 1976 ASSERT_TRUE(SetCookie(cm, url_google_, kValidCookieLine)); |
1949 | 1977 |
1950 scoped_ptr<base::HistogramSamples> samples3( | 1978 scoped_ptr<base::HistogramSamples> samples3( |
1951 expired_histogram->SnapshotSamples()); | 1979 expired_histogram->SnapshotSamples()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1988 SetCookieCallback* callback) { | 2016 SetCookieCallback* callback) { |
1989 // Define the parameters here instead of in the calling fucntion. | 2017 // Define the parameters here instead of in the calling fucntion. |
1990 // The maximum number of parameters for Bind function is 6. | 2018 // The maximum number of parameters for Bind function is 6. |
1991 std::string name = "A"; | 2019 std::string name = "A"; |
1992 std::string value = "B"; | 2020 std::string value = "B"; |
1993 std::string domain = std::string(); | 2021 std::string domain = std::string(); |
1994 std::string path = "/foo"; | 2022 std::string path = "/foo"; |
1995 base::Time expiration_time = base::Time(); | 2023 base::Time expiration_time = base::Time(); |
1996 bool secure = false; | 2024 bool secure = false; |
1997 bool http_only = false; | 2025 bool http_only = false; |
| 2026 CookiePriority priority = PRIORITY_DEFAULT; |
1998 cm->SetCookieWithDetailsAsync( | 2027 cm->SetCookieWithDetailsAsync( |
1999 url, name, value, domain, path, expiration_time, secure, http_only, | 2028 url, name, value, domain, path, expiration_time, secure, http_only, |
| 2029 priority, |
2000 base::Bind(&SetCookieCallback::Run, base::Unretained(callback))); | 2030 base::Bind(&SetCookieCallback::Run, base::Unretained(callback))); |
2001 } | 2031 } |
2002 | 2032 |
2003 void DeleteAllCreatedBetweenTask(CookieMonster* cm, | 2033 void DeleteAllCreatedBetweenTask(CookieMonster* cm, |
2004 const base::Time& delete_begin, | 2034 const base::Time& delete_begin, |
2005 const base::Time& delete_end, | 2035 const base::Time& delete_end, |
2006 DeleteCallback* callback) { | 2036 DeleteCallback* callback) { |
2007 cm->DeleteAllCreatedBetweenAsync( | 2037 cm->DeleteAllCreatedBetweenAsync( |
2008 delete_begin, delete_end, | 2038 delete_begin, delete_end, |
2009 base::Bind(&DeleteCallback::Run, | 2039 base::Bind(&DeleteCallback::Run, |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2108 EXPECT_EQ("www.google.izzle", it->Domain()); | 2138 EXPECT_EQ("www.google.izzle", it->Domain()); |
2109 EXPECT_EQ("A", it->Name()); | 2139 EXPECT_EQ("A", it->Name()); |
2110 ASSERT_TRUE(++it == callback.cookies().end()); | 2140 ASSERT_TRUE(++it == callback.cookies().end()); |
2111 } | 2141 } |
2112 | 2142 |
2113 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckSetCookieWithDetails) { | 2143 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckSetCookieWithDetails) { |
2114 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 2144 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
2115 EXPECT_TRUE(SetCookieWithDetails( | 2145 EXPECT_TRUE(SetCookieWithDetails( |
2116 cm, url_google_foo_, | 2146 cm, url_google_foo_, |
2117 "A", "B", std::string(), "/foo", base::Time(), | 2147 "A", "B", std::string(), "/foo", base::Time(), |
2118 false, false)); | 2148 false, false, PRIORITY_DEFAULT)); |
2119 SetCookieCallback callback(&other_thread_); | 2149 SetCookieCallback callback(&other_thread_); |
2120 base::Closure task = base::Bind( | 2150 base::Closure task = base::Bind( |
2121 &net::MultiThreadedCookieMonsterTest::SetCookieWithDetailsTask, | 2151 &net::MultiThreadedCookieMonsterTest::SetCookieWithDetailsTask, |
2122 base::Unretained(this), | 2152 base::Unretained(this), |
2123 cm, url_google_foo_, &callback); | 2153 cm, url_google_foo_, &callback); |
2124 RunOnOtherThread(task); | 2154 RunOnOtherThread(task); |
2125 EXPECT_TRUE(callback.did_run()); | 2155 EXPECT_TRUE(callback.did_run()); |
2126 EXPECT_TRUE(callback.result()); | 2156 EXPECT_TRUE(callback.result()); |
2127 } | 2157 } |
2128 | 2158 |
2129 | |
2130 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteAllCreatedBetween) { | 2159 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteAllCreatedBetween) { |
2131 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 2160 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
2132 CookieOptions options; | 2161 CookieOptions options; |
2133 Time now = Time::Now(); | 2162 Time now = Time::Now(); |
2134 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options)); | 2163 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options)); |
2135 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(99), | 2164 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(99), |
2136 Time())); | 2165 Time())); |
2137 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options)); | 2166 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options)); |
2138 DeleteCallback callback(&other_thread_); | 2167 DeleteCallback callback(&other_thread_); |
2139 base::Closure task = base::Bind( | 2168 base::Closure task = base::Bind( |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2263 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); | 2292 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); |
2264 | 2293 |
2265 // Create some non-persistent cookies and check that they don't go to the | 2294 // Create some non-persistent cookies and check that they don't go to the |
2266 // persistent storage. | 2295 // persistent storage. |
2267 EXPECT_TRUE(SetCookie(cm, url_google_, "B=Bar")); | 2296 EXPECT_TRUE(SetCookie(cm, url_google_, "B=Bar")); |
2268 this->MatchCookieLines("A=Foo; B=Bar", GetCookies(cm, url_google_)); | 2297 this->MatchCookieLines("A=Foo; B=Bar", GetCookies(cm, url_google_)); |
2269 EXPECT_EQ(5u, store->commands().size()); | 2298 EXPECT_EQ(5u, store->commands().size()); |
2270 } | 2299 } |
2271 | 2300 |
2272 } // namespace net | 2301 } // namespace net |
OLD | NEW |