OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/cookies/cookie_store_unittest.h" | 5 #include "net/cookies/cookie_store_unittest.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 case 'H': | 346 case 'H': |
347 return COOKIE_PRIORITY_HIGH; | 347 return COOKIE_PRIORITY_HIGH; |
348 } | 348 } |
349 NOTREACHED(); | 349 NOTREACHED(); |
350 return COOKIE_PRIORITY_DEFAULT; | 350 return COOKIE_PRIORITY_DEFAULT; |
351 } | 351 } |
352 | 352 |
353 // Instantiates a CookieMonster, adds multiple cookies (to http_www_google_) | 353 // Instantiates a CookieMonster, adds multiple cookies (to http_www_google_) |
354 // with priorities specified by |coded_priority_str|, and tests priority-aware | 354 // with priorities specified by |coded_priority_str|, and tests priority-aware |
355 // domain cookie eviction. | 355 // domain cookie eviction. |
356 // | 356 // |coded_priority_str| specifies a run-length-encoded string of priorities. |
357 // Example: |coded_priority_string| of "2MN 3LS MN 4HN" specifies sequential | 357 // Example: "2M 3L M 4H" means "MMLLLMHHHH", and speicifies sequential (i.e., |
358 // (i.e., from least- to most-recently accessed) insertion of 2 | 358 // from least- to most-recently accessed) insertion of 2 medium-priority |
359 // medium-priority non-secure cookies, 3 low-priority secure cookies, 1 | 359 // cookies, 3 low-priority cookies, 1 medium-priority cookie, and 4 |
360 // medium-priority non-secure cookie, and 4 high-priority non-secure cookies. | 360 // high-priority cookies. |
361 // | |
362 // Within each priority, only the least-accessed cookies should be evicted. | 361 // Within each priority, only the least-accessed cookies should be evicted. |
363 // Thus, to describe expected suriving cookies, it suffices to specify the | 362 // Thus, to describe expected suriving cookies, it suffices to specify the |
364 // expected population of surviving cookies per priority, i.e., | 363 // expected population of surviving cookies per priority, i.e., |
365 // |expected_low_count|, |expected_medium_count|, and |expected_high_count|. | 364 // |expected_low_count|, |expected_medium_count|, and |expected_high_count|. |
366 void TestPriorityCookieCase(CookieMonster* cm, | 365 void TestPriorityCookieCase(CookieMonster* cm, |
367 const std::string& coded_priority_str, | 366 const std::string& coded_priority_str, |
368 size_t expected_low_count, | 367 size_t expected_low_count, |
369 size_t expected_medium_count, | 368 size_t expected_medium_count, |
370 size_t expected_high_count, | 369 size_t expected_high_count) { |
371 size_t expected_nonsecure, | |
372 size_t expected_secure) { | |
373 SCOPED_TRACE(coded_priority_str.c_str()); | |
374 this->DeleteAll(cm); | 370 this->DeleteAll(cm); |
375 int next_cookie_id = 0; | 371 int next_cookie_id = 0; |
376 // A list of cookie IDs, indexed by secure status, then by priority. | 372 std::vector<CookiePriority> priority_list; |
377 std::vector<int> id_list[2][3]; | 373 std::vector<int> id_list[3]; // Indexed by CookiePriority. |
378 // A list of all the cookies stored, along with their properties. | |
379 std::vector<std::pair<bool, CookiePriority>> cookie_data; | |
380 | 374 |
381 // Parse |coded_priority_str| and add cookies. | 375 // Parse |coded_priority_str| and add cookies. |
382 for (const std::string& token : | 376 for (const std::string& token : |
383 base::SplitString(coded_priority_str, " ", base::TRIM_WHITESPACE, | 377 base::SplitString(coded_priority_str, " ", base::TRIM_WHITESPACE, |
384 base::SPLIT_WANT_ALL)) { | 378 base::SPLIT_WANT_ALL)) { |
385 DCHECK(!token.empty()); | 379 DCHECK(!token.empty()); |
386 | 380 // Take last character as priority. |
387 // Take last character as security status, then discard it. | 381 CookiePriority priority = CharToPriority(token.back()); |
388 bool is_secure = token[token.size() - 1] == 'S'; | 382 std::string priority_str = CookiePriorityToString(priority); |
389 | |
390 // The second-to-last character is the priority. Grab and discard it. | |
391 CookiePriority priority = CharToPriority(token[token.size() - 2]); | |
392 | |
393 // The rest of the string (possibly empty) specifies repetition. | 383 // The rest of the string (possibly empty) specifies repetition. |
394 int rep = 1; | 384 int rep = 1; |
395 if (!token.empty()) { | 385 if (!token.empty()) { |
396 bool result = base::StringToInt( | 386 bool result = base::StringToInt( |
397 base::StringPiece(token.begin(), token.end() - 2), &rep); | 387 base::StringPiece(token.begin(), token.end() - 1), &rep); |
398 DCHECK(result); | 388 DCHECK(result); |
399 } | 389 } |
400 for (; rep > 0; --rep, ++next_cookie_id) { | 390 for (; rep > 0; --rep, ++next_cookie_id) { |
401 std::string cookie = | 391 std::string cookie = base::StringPrintf( |
402 base::StringPrintf("a%d=b;priority=%s;%s", next_cookie_id, | 392 "a%d=b;priority=%s", next_cookie_id, priority_str.c_str()); |
403 CookiePriorityToString(priority).c_str(), | 393 EXPECT_TRUE(SetCookie(cm, http_www_google_.url(), cookie)); |
404 is_secure ? "secure" : ""); | 394 priority_list.push_back(priority); |
405 EXPECT_TRUE(SetCookie(cm, https_www_google_.url(), cookie)); | 395 id_list[priority].push_back(next_cookie_id); |
406 cookie_data.push_back(std::make_pair(is_secure, priority)); | |
407 id_list[is_secure][priority].push_back(next_cookie_id); | |
408 } | 396 } |
409 } | 397 } |
410 | 398 |
411 int num_cookies = static_cast<int>(cookie_data.size()); | 399 int num_cookies = static_cast<int>(priority_list.size()); |
412 // A list of cookie IDs, indexed by secure status, then by priority. | 400 std::vector<int> surviving_id_list[3]; // Indexed by CookiePriority. |
413 std::vector<int> surviving_id_list[2][3]; | |
414 | 401 |
415 // Parse the list of cookies | 402 // Parse the list of cookies |
416 std::string cookie_str = this->GetCookies(cm, https_www_google_.url()); | 403 std::string cookie_str = this->GetCookies(cm, http_www_google_.url()); |
417 size_t num_nonsecure = 0; | |
418 size_t num_secure = 0; | |
419 for (const std::string& token : base::SplitString( | 404 for (const std::string& token : base::SplitString( |
420 cookie_str, ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { | 405 cookie_str, ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { |
421 // Assuming *it is "a#=b", so extract and parse "#" portion. | 406 // Assuming *it is "a#=b", so extract and parse "#" portion. |
422 int id = -1; | 407 int id = -1; |
423 bool result = base::StringToInt( | 408 bool result = base::StringToInt( |
424 base::StringPiece(token.begin() + 1, token.end() - 2), &id); | 409 base::StringPiece(token.begin() + 1, token.end() - 2), &id); |
425 DCHECK(result); | 410 DCHECK(result); |
426 DCHECK_GE(id, 0); | 411 DCHECK_GE(id, 0); |
427 DCHECK_LT(id, num_cookies); | 412 DCHECK_LT(id, num_cookies); |
428 surviving_id_list[cookie_data[id].first][cookie_data[id].second] | 413 surviving_id_list[priority_list[id]].push_back(id); |
429 .push_back(id); | |
430 if (cookie_data[id].first) | |
431 num_secure += 1; | |
432 else | |
433 num_nonsecure += 1; | |
434 } | 414 } |
435 | 415 |
436 EXPECT_EQ(expected_nonsecure, num_nonsecure); | |
437 EXPECT_EQ(expected_secure, num_secure); | |
438 | |
439 // Validate each priority. | 416 // Validate each priority. |
440 size_t expected_count[3] = { | 417 size_t expected_count[3] = { |
441 expected_low_count, expected_medium_count, expected_high_count}; | 418 expected_low_count, expected_medium_count, expected_high_count}; |
442 for (int i = 0; i < 3; ++i) { | 419 for (int i = 0; i < 3; ++i) { |
443 size_t num_for_priority = | 420 DCHECK_LE(surviving_id_list[i].size(), id_list[i].size()); |
444 surviving_id_list[0][i].size() + surviving_id_list[1][i].size(); | 421 EXPECT_EQ(expected_count[i], surviving_id_list[i].size()); |
445 EXPECT_EQ(expected_count[i], num_for_priority); | |
446 // Verify that the remaining cookies are the most recent among those | 422 // Verify that the remaining cookies are the most recent among those |
447 // with the same priorities. | 423 // with the same priorities. |
448 if (expected_count[i] == num_for_priority) { | 424 if (expected_count[i] == surviving_id_list[i].size()) { |
449 // Non-secure: | 425 std::sort(surviving_id_list[i].begin(), surviving_id_list[i].end()); |
450 std::sort(surviving_id_list[0][i].begin(), | 426 EXPECT_TRUE(std::equal(surviving_id_list[i].begin(), |
451 surviving_id_list[0][i].end()); | 427 surviving_id_list[i].end(), |
452 EXPECT_TRUE(std::equal( | 428 id_list[i].end() - expected_count[i])); |
453 surviving_id_list[0][i].begin(), surviving_id_list[0][i].end(), | |
454 id_list[0][i].end() - surviving_id_list[0][i].size())); | |
455 | |
456 // Secure: | |
457 std::sort(surviving_id_list[1][i].begin(), | |
458 surviving_id_list[1][i].end()); | |
459 EXPECT_TRUE(std::equal( | |
460 surviving_id_list[1][i].begin(), surviving_id_list[1][i].end(), | |
461 id_list[1][i].end() - surviving_id_list[1][i].size())); | |
462 } | 429 } |
463 } | 430 } |
464 } | 431 } |
465 | 432 |
466 // Represents a number of cookies to create, if they are Secure cookies, and | 433 // Represents a number of cookies to create, if they are Secure cookies, and |
467 // a url to add them to. | 434 // a url to add them to. |
468 struct CookiesEntry { | 435 struct CookiesEntry { |
469 size_t num_cookies; | 436 size_t num_cookies; |
470 bool is_secure; | 437 bool is_secure; |
471 }; | 438 }; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 if (cookie.IsSecure()) | 485 if (cookie.IsSecure()) |
519 ++total_secure_cookies; | 486 ++total_secure_cookies; |
520 else | 487 else |
521 ++total_non_secure_cookies; | 488 ++total_non_secure_cookies; |
522 } | 489 } |
523 | 490 |
524 EXPECT_EQ(expected_secure_cookies, total_secure_cookies); | 491 EXPECT_EQ(expected_secure_cookies, total_secure_cookies); |
525 EXPECT_EQ(expected_non_secure_cookies, total_non_secure_cookies); | 492 EXPECT_EQ(expected_non_secure_cookies, total_non_secure_cookies); |
526 } | 493 } |
527 | 494 |
528 void TestPriorityAwareGarbageCollectHelperNonSecure() { | 495 void TestPriorityAwareGarbageCollectHelper() { |
529 // Hard-coding limits in the test, but use DCHECK_EQ to enforce constraint. | 496 // Hard-coding limits in the test, but use DCHECK_EQ to enforce constraint. |
530 DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies); | 497 DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies); |
531 DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies - | 498 DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies - |
532 CookieMonster::kDomainPurgeCookies); | 499 CookieMonster::kDomainPurgeCookies); |
| 500 DCHECK_EQ(30U, CookieMonster::kDomainCookiesQuotaLow); |
| 501 DCHECK_EQ(50U, CookieMonster::kDomainCookiesQuotaMedium); |
| 502 DCHECK_EQ(70U, CookieMonster::kDomainCookiesQuotaHigh); |
533 | 503 |
534 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 504 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
535 | 505 |
536 // Each test case adds 181 cookies, so 31 cookies are evicted. | 506 // Each test case adds 181 cookies, so 31 cookies are evicted. |
537 // Cookie same priority, repeated for each priority. | 507 // Cookie same priority, repeated for each priority. |
538 TestPriorityCookieCase(cm.get(), "181LN", 150U, 0U, 0U, 150U, 0U); | 508 TestPriorityCookieCase(cm.get(), "181L", 150U, 0U, 0U); |
539 TestPriorityCookieCase(cm.get(), "181MN", 0U, 150U, 0U, 150U, 0U); | 509 TestPriorityCookieCase(cm.get(), "181M", 0U, 150U, 0U); |
540 TestPriorityCookieCase(cm.get(), "181HN", 0U, 0U, 150U, 150U, 0U); | 510 TestPriorityCookieCase(cm.get(), "181H", 0U, 0U, 150U); |
541 | 511 |
542 // Pairwise scenarios. | 512 // Pairwise scenarios. |
543 // Round 1 => none; round2 => 31M; round 3 => none. | 513 // Round 1 => none; round2 => 31M; round 3 => none. |
544 TestPriorityCookieCase(cm.get(), "10HN 171MN", 0U, 140U, 10U, 150U, 0U); | 514 TestPriorityCookieCase(cm.get(), "10H 171M", 0U, 140U, 10U); |
545 // Round 1 => 10L; round2 => 21M; round 3 => none. | 515 // Round 1 => 10L; round2 => 21M; round 3 => none. |
546 TestPriorityCookieCase(cm.get(), "141MN 40LN", 30U, 120U, 0U, 150U, 0U); | 516 TestPriorityCookieCase(cm.get(), "141M 40L", 30U, 120U, 0U); |
547 // Round 1 => none; round2 => none; round 3 => 31H. | 517 // Round 1 => none; round2 => none; round 3 => 31H. |
548 TestPriorityCookieCase(cm.get(), "101HN 80MN", 0U, 80U, 70U, 150U, 0U); | 518 TestPriorityCookieCase(cm.get(), "101H 80M", 0U, 80U, 70U); |
549 | 519 |
550 // For {low, medium} priorities right on quota, different orders. | 520 // For {low, medium} priorities right on quota, different orders. |
551 // Round 1 => 1L; round 2 => none, round3 => 30L. | 521 // Round 1 => 1L; round 2 => none, round3 => 30L. |
552 TestPriorityCookieCase(cm.get(), "31LN 50MN 100HN", 0U, 50U, 100U, 150U, | 522 TestPriorityCookieCase(cm.get(), "31L 50M 100H", 0U, 50U, 100U); |
553 0U); | |
554 // Round 1 => none; round 2 => 1M, round3 => 30M. | 523 // Round 1 => none; round 2 => 1M, round3 => 30M. |
555 TestPriorityCookieCase(cm.get(), "51MN 100HN 30LN", 30U, 20U, 100U, 150U, | 524 TestPriorityCookieCase(cm.get(), "51M 100H 30L", 30U, 20U, 100U); |
556 0U); | |
557 // Round 1 => none; round 2 => none; round3 => 31H. | 525 // Round 1 => none; round 2 => none; round3 => 31H. |
558 TestPriorityCookieCase(cm.get(), "101HN 50MN 30LN", 30U, 50U, 70U, 150U, | 526 TestPriorityCookieCase(cm.get(), "101H 50M 30L", 30U, 50U, 70U); |
559 0U); | |
560 | 527 |
561 // Round 1 => 10L; round 2 => 10M; round3 => 11H. | 528 // Round 1 => 10L; round 2 => 10M; round3 => 11H. |
562 TestPriorityCookieCase(cm.get(), "81HN 60MN 40LN", 30U, 50U, 70U, 150U, 0U); | 529 TestPriorityCookieCase(cm.get(), "81H 60M 40L", 30U, 50U, 70U); |
563 | 530 |
564 // More complex scenarios. | 531 // More complex scenarios. |
565 // Round 1 => 10L; round 2 => 10M; round 3 => 11H. | 532 // Round 1 => 10L; round 2 => 10M; round 3 => 11H. |
566 TestPriorityCookieCase(cm.get(), "21HN 60MN 40LN 60HN", 30U, 50U, 70U, 150U, | 533 TestPriorityCookieCase(cm.get(), "21H 60M 40L 60H", 30U, 50U, 70U); |
567 0U); | |
568 // Round 1 => 10L; round 2 => 11M, 10L; round 3 => none. | 534 // Round 1 => 10L; round 2 => 11M, 10L; round 3 => none. |
569 TestPriorityCookieCase(cm.get(), "11HN 10MN 20LN 110MN 20LN 10HN", 20U, | 535 TestPriorityCookieCase(cm.get(), "11H 10M 20L 110M 20L 10H", 20U, 109U, |
570 109U, 21U, 150U, 0U); | 536 21U); |
571 // Round 1 => none; round 2 => none; round 3 => 11L, 10M, 10H. | 537 // Round 1 => none; round 2 => none; round 3 => 11L, 10M, 10H. |
572 TestPriorityCookieCase(cm.get(), "11LN 10MN 140HN 10MN 10LN", 10U, 10U, | 538 TestPriorityCookieCase(cm.get(), "11L 10M 140H 10M 10L", 10U, 10U, 130U); |
573 130U, 150U, 0U); | |
574 // Round 1 => none; round 2 => 1M; round 3 => 10L, 10M, 10H. | 539 // Round 1 => none; round 2 => 1M; round 3 => 10L, 10M, 10H. |
575 TestPriorityCookieCase(cm.get(), "11MN 10HN 10LN 60MN 90HN", 0U, 60U, 90U, | 540 TestPriorityCookieCase(cm.get(), "11M 10H 10L 60M 90H", 0U, 60U, 90U); |
576 150U, 0U); | |
577 // Round 1 => none; round 2 => 10L, 21M; round 3 => none. | 541 // Round 1 => none; round 2 => 10L, 21M; round 3 => none. |
578 TestPriorityCookieCase(cm.get(), "11MN 10HN 10LN 90MN 60HN", 0U, 80U, 70U, | 542 TestPriorityCookieCase(cm.get(), "11M 10H 10L 90M 60H", 0U, 80U, 70U); |
579 150U, 0U); | |
580 } | |
581 | |
582 void TestPriorityAwareGarbageCollectHelperSecure() { | |
583 // Hard-coding limits in the test, but use DCHECK_EQ to enforce constraint. | |
584 DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies); | |
585 DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies - | |
586 CookieMonster::kDomainPurgeCookies); | |
587 | |
588 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | |
589 | |
590 // Each test case adds 181 cookies, so 31 cookies are evicted. | |
591 // Cookie same priority, repeated for each priority. | |
592 TestPriorityCookieCase(cm.get(), "181LS", 150U, 0U, 0U, 0U, 150U); | |
593 TestPriorityCookieCase(cm.get(), "181MS", 0U, 150U, 0U, 0U, 150U); | |
594 TestPriorityCookieCase(cm.get(), "181HS", 0U, 0U, 150U, 0U, 150U); | |
595 | |
596 // Pairwise scenarios. | |
597 // Round 1 => none; round2 => 31M; round 3 => none. | |
598 TestPriorityCookieCase(cm.get(), "10HS 171MS", 0U, 140U, 10U, 0U, 150U); | |
599 // Round 1 => 10L; round2 => 21M; round 3 => none. | |
600 TestPriorityCookieCase(cm.get(), "141MS 40LS", 30U, 120U, 0U, 0U, 150U); | |
601 // Round 1 => none; round2 => none; round 3 => 31H. | |
602 TestPriorityCookieCase(cm.get(), "101HS 80MS", 0U, 80U, 70U, 0U, 150U); | |
603 | |
604 // For {low, medium} priorities right on quota, different orders. | |
605 // Round 1 => 1L; round 2 => none, round3 => 30L. | |
606 TestPriorityCookieCase(cm.get(), "31LS 50MS 100HS", 0U, 50U, 100U, 0U, | |
607 150U); | |
608 // Round 1 => none; round 2 => 1M, round3 => 30M. | |
609 TestPriorityCookieCase(cm.get(), "51MS 100HS 30LS", 30U, 20U, 100U, 0U, | |
610 150U); | |
611 // Round 1 => none; round 2 => none; round3 => 31H. | |
612 TestPriorityCookieCase(cm.get(), "101HS 50MS 30LS", 30U, 50U, 70U, 0U, | |
613 150U); | |
614 | |
615 // Round 1 => 10L; round 2 => 10M; round3 => 11H. | |
616 TestPriorityCookieCase(cm.get(), "81HS 60MS 40LS", 30U, 50U, 70U, 0U, 150U); | |
617 | |
618 // More complex scenarios. | |
619 // Round 1 => 10L; round 2 => 10M; round 3 => 11H. | |
620 TestPriorityCookieCase(cm.get(), "21HS 60MS 40LS 60HS", 30U, 50U, 70U, 0U, | |
621 150U); | |
622 // Round 1 => 10L; round 2 => 11M, 10L; round 3 => none. | |
623 TestPriorityCookieCase(cm.get(), "11HS 10MS 20LS 110MS 20LS 10HS", 20U, | |
624 109U, 21U, 0U, 150U); | |
625 // Round 1 => none; round 2 => none; round 3 => 11L, 10M, 10H. | |
626 TestPriorityCookieCase(cm.get(), "11LS 10MS 140HS 10MS 10LS", 10U, 10U, | |
627 130U, 0U, 150U); | |
628 // Round 1 => none; round 2 => 1M; round 3 => 10L, 10M, 10H. | |
629 TestPriorityCookieCase(cm.get(), "11MS 10HS 10LS 60MS 90HS", 0U, 60U, 90U, | |
630 0U, 150U); | |
631 // Round 1 => none; round 2 => 10L, 21M; round 3 => none. | |
632 TestPriorityCookieCase(cm.get(), "11MS 10HS 10LS 90MS 60HS", 0U, 80U, 70U, | |
633 0U, 150U); | |
634 } | |
635 | |
636 void TestPriorityAwareGarbageCollectHelperMixed() { | |
637 // Hard-coding limits in the test, but use DCHECK_EQ to enforce constraint. | |
638 DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies); | |
639 DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies - | |
640 CookieMonster::kDomainPurgeCookies); | |
641 | |
642 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | |
643 | |
644 // Each test case adds 180 secure cookies, and some non-secure cookie. The | |
645 // secure cookies take priority, so the non-secure cookie is removed, along | |
646 // with 30 secure cookies. Repeated for each priority, and with the | |
647 // non-secure cookie as older and newer. | |
648 TestPriorityCookieCase(cm.get(), "1LN 180LS", 150U, 0U, 0U, 0U, 150U); | |
649 TestPriorityCookieCase(cm.get(), "1MN 180MS", 0U, 150U, 0U, 0U, 150U); | |
650 TestPriorityCookieCase(cm.get(), "1HN 180HS", 0U, 0U, 150U, 0U, 150U); | |
651 TestPriorityCookieCase(cm.get(), "180LS 1LN", 150U, 0U, 0U, 0U, 150U); | |
652 TestPriorityCookieCase(cm.get(), "180MS 1MN", 0U, 150U, 0U, 0U, 150U); | |
653 TestPriorityCookieCase(cm.get(), "180HS 1HN", 0U, 0U, 150U, 0U, 150U); | |
654 | |
655 // Higher-priority non-secure cookies are removed before any secure cookie. | |
656 TestPriorityCookieCase(cm.get(), "180LS 1MN", 150U, 0U, 0U, 0U, 150U); | |
657 TestPriorityCookieCase(cm.get(), "180LS 1HN", 150U, 0U, 0U, 0U, 150U); | |
658 TestPriorityCookieCase(cm.get(), "180MS 1HN", 0U, 150U, 0U, 0U, 150U); | |
659 TestPriorityCookieCase(cm.get(), "1MN 180LS", 150U, 0U, 0U, 0U, 150U); | |
660 TestPriorityCookieCase(cm.get(), "1HN 180LS", 150U, 0U, 0U, 0U, 150U); | |
661 TestPriorityCookieCase(cm.get(), "1HN 180MS", 0U, 150U, 0U, 0U, 150U); | |
662 | |
663 // Pairwise: | |
664 TestPriorityCookieCase(cm.get(), "1LS 180LN", 150U, 0U, 0U, 149U, 1U); | |
665 TestPriorityCookieCase(cm.get(), "100LS 81LN", 150U, 0U, 0U, 50U, 100U); | |
666 TestPriorityCookieCase(cm.get(), "150LS 31LN", 150U, 0U, 0U, 0U, 150U); | |
667 TestPriorityCookieCase(cm.get(), "1LS 180HN", 1U, 0U, 149U, 149U, 1U); | |
668 TestPriorityCookieCase(cm.get(), "100LS 81HN", 100U, 0U, 50U, 50U, 100U); | |
669 TestPriorityCookieCase(cm.get(), "150LS 31HN", 150U, 0U, 0U, 0U, 150U); | |
670 | |
671 // Quota calculations inside non-secure/secure blocks remain in place: | |
672 TestPriorityCookieCase(cm.get(), "50HN 50LS 81HS", 50U, 0U, 100U, 19U, | |
673 131U); | |
674 TestPriorityCookieCase(cm.get(), "11MS 10HN 10LS 90MN 60HN", 10U, 79U, 61U, | |
675 129U, 21U); | |
676 | |
677 // Multiple GC rounds end up with consistent behavior: | |
678 TestPriorityCookieCase(cm.get(), "100HS 100LN 100MN", 0, 76U, 100U, 76U, | |
679 100U); | |
680 } | 543 } |
681 | 544 |
682 // Function for creating a CM with a number of cookies in it, | 545 // Function for creating a CM with a number of cookies in it, |
683 // no store (and hence no ability to affect access time). | 546 // no store (and hence no ability to affect access time). |
684 CookieMonster* CreateMonsterForGC(int num_cookies) { | 547 CookieMonster* CreateMonsterForGC(int num_cookies) { |
685 CookieMonster* cm(new CookieMonster(NULL, NULL)); | 548 CookieMonster* cm(new CookieMonster(NULL, NULL)); |
686 for (int i = 0; i < num_cookies; i++) { | 549 for (int i = 0; i < num_cookies; i++) { |
687 SetCookie(cm, GURL(base::StringPrintf("http://h%05d.izzle", i)), "a=1"); | 550 SetCookie(cm, GURL(base::StringPrintf("http://h%05d.izzle", i)), "a=1"); |
688 } | 551 } |
689 return cm; | 552 return cm; |
(...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1378 base::PlatformThread::Sleep( | 1241 base::PlatformThread::Sleep( |
1379 base::TimeDelta::FromMilliseconds(kAccessDelayMs)); | 1242 base::TimeDelta::FromMilliseconds(kAccessDelayMs)); |
1380 EXPECT_EQ("A=B", GetCookies(cm.get(), http_www_google_.url())); | 1243 EXPECT_EQ("A=B", GetCookies(cm.get(), http_www_google_.url())); |
1381 EXPECT_FALSE(last_access_date == GetFirstCookieAccessDate(cm.get())); | 1244 EXPECT_FALSE(last_access_date == GetFirstCookieAccessDate(cm.get())); |
1382 } | 1245 } |
1383 | 1246 |
1384 TEST_F(CookieMonsterTest, TestHostGarbageCollection) { | 1247 TEST_F(CookieMonsterTest, TestHostGarbageCollection) { |
1385 TestHostGarbageCollectHelper(); | 1248 TestHostGarbageCollectHelper(); |
1386 } | 1249 } |
1387 | 1250 |
1388 TEST_F(CookieMonsterTest, TestPriorityAwareGarbageCollectionNonSecure) { | 1251 TEST_F(CookieMonsterTest, TestPriorityAwareGarbageCollection) { |
1389 TestPriorityAwareGarbageCollectHelperNonSecure(); | 1252 TestPriorityAwareGarbageCollectHelper(); |
1390 } | |
1391 | |
1392 TEST_F(CookieMonsterTest, TestPriorityAwareGarbageCollectionSecure) { | |
1393 TestPriorityAwareGarbageCollectHelperSecure(); | |
1394 } | |
1395 | |
1396 TEST_F(CookieMonsterTest, TestPriorityAwareGarbageCollectionMixed) { | |
1397 TestPriorityAwareGarbageCollectHelperMixed(); | |
1398 } | 1253 } |
1399 | 1254 |
1400 TEST_F(CookieMonsterTest, SetCookieableSchemes) { | 1255 TEST_F(CookieMonsterTest, SetCookieableSchemes) { |
1401 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 1256 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
1402 scoped_refptr<CookieMonster> cm_foo(new CookieMonster(NULL, NULL)); | 1257 scoped_refptr<CookieMonster> cm_foo(new CookieMonster(NULL, NULL)); |
1403 | 1258 |
1404 // Only cm_foo should allow foo:// cookies. | 1259 // Only cm_foo should allow foo:// cookies. |
1405 std::vector<std::string> schemes; | 1260 std::vector<std::string> schemes; |
1406 schemes.push_back("foo"); | 1261 schemes.push_back("foo"); |
1407 cm_foo->SetCookieableSchemes(schemes); | 1262 cm_foo->SetCookieableSchemes(schemes); |
(...skipping 1692 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3100 DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies); | 2955 DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies); |
3101 DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies - | 2956 DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies - |
3102 CookieMonster::kDomainPurgeCookies); | 2957 CookieMonster::kDomainPurgeCookies); |
3103 DCHECK_EQ(3300U, CookieMonster::kMaxCookies); | 2958 DCHECK_EQ(3300U, CookieMonster::kMaxCookies); |
3104 DCHECK_EQ(30, CookieMonster::kSafeFromGlobalPurgeDays); | 2959 DCHECK_EQ(30, CookieMonster::kSafeFromGlobalPurgeDays); |
3105 | 2960 |
3106 // If secure cookies for one domain hit the per domain limit (180), a | 2961 // If secure cookies for one domain hit the per domain limit (180), a |
3107 // non-secure cookie will not evict them (and, in fact, the non-secure cookie | 2962 // non-secure cookie will not evict them (and, in fact, the non-secure cookie |
3108 // will be removed right after creation). | 2963 // will be removed right after creation). |
3109 const CookiesEntry test1[] = {{180U, true}, {1U, false}}; | 2964 const CookiesEntry test1[] = {{180U, true}, {1U, false}}; |
3110 TestSecureCookieEviction(test1, arraysize(test1), 150U, 0U, nullptr); | 2965 TestSecureCookieEviction(test1, arraysize(test1), 180U, 0U, nullptr); |
3111 | 2966 |
3112 // If non-secure cookies for one domain hit the per domain limit (180), the | 2967 // If non-secure cookies for one domain hit the per domain limit (180), the |
3113 // creation of secure cookies will evict the non-secure cookies first, making | 2968 // creation of secure cookies will evict all of the non-secure cookies, and |
3114 // room for the secure cookies. | 2969 // the secure cookies will still be created. |
3115 const CookiesEntry test2[] = {{180U, false}, {20U, true}}; | 2970 const CookiesEntry test2[] = {{180U, false}, {20U, true}}; |
3116 TestSecureCookieEviction(test2, arraysize(test2), 20U, 149U, nullptr); | 2971 TestSecureCookieEviction(test2, arraysize(test2), 20U, 0U, nullptr); |
3117 | 2972 |
3118 // If secure cookies for one domain go past the per domain limit (180), they | 2973 // If secure cookies for one domain go past the per domain limit (180), they |
3119 // will be evicted as normal by the per domain purge amount (30) down to a | 2974 // will be evicted as normal by the per domain purge amount (30) down to a |
3120 // lower amount (150), and then will continue to create the remaining cookies | 2975 // lower amount (150), and then will continue to create the remaining cookies |
3121 // (19 more to 169). | 2976 // (19 more to 169). |
3122 const CookiesEntry test3[] = {{200U, true}}; | 2977 const CookiesEntry test3[] = {{200U, true}}; |
3123 TestSecureCookieEviction(test3, arraysize(test3), 169U, 0U, nullptr); | 2978 TestSecureCookieEviction(test3, arraysize(test3), 169U, 0U, nullptr); |
3124 | 2979 |
3125 // If a non-secure cookie is created, and a number of secure cookies exceeds | 2980 // If a non-secure cookie is created, and a number of secure cookies exceeds |
3126 // the per domain limit (18), the total cookies will be evicted down to a | 2981 // the per domain limit (18), the total cookies will be evicted down to a |
3127 // lower amount (150), enforcing the eviction of the non-secure cookie, and | 2982 // lower amount (150), enforcing the eviction of the non-secure cookie, and |
3128 // the remaining secure cookies will be created (another 19 to 169). | 2983 // the remaining secure cookies will be created (another 18 to 168). |
3129 const CookiesEntry test4[] = {{1U, false}, {199U, true}}; | 2984 const CookiesEntry test4[] = {{1U, false}, {199U, true}}; |
3130 TestSecureCookieEviction(test4, arraysize(test4), 169U, 0U, nullptr); | 2985 TestSecureCookieEviction(test4, arraysize(test4), 168U, 0U, nullptr); |
3131 | 2986 |
3132 // If an even number of non-secure and secure cookies are created below the | 2987 // If an even number of non-secure and secure cookies are created below the |
3133 // per-domain limit (180), all will be created and none evicted. | 2988 // per-domain limit (180), all will be created and none evicted. |
3134 const CookiesEntry test5[] = {{75U, false}, {75U, true}}; | 2989 const CookiesEntry test5[] = {{75U, false}, {75U, true}}; |
3135 TestSecureCookieEviction(test5, arraysize(test5), 75U, 75U, nullptr); | 2990 TestSecureCookieEviction(test5, arraysize(test5), 75U, 75U, nullptr); |
3136 | 2991 |
3137 // If the same number of secure and non-secure cookies are created (50 each) | 2992 // If the same number of secure and non-secure cookies are created (50 each) |
3138 // below the per domain limit (180), and then another set of secure cookies | 2993 // below the per domain limit (180), and then another set of secure cookies |
3139 // are created to bring the total above the per-domain limit, all secure | 2994 // are created to bring the total above the per-domain limit, all of the |
3140 // cookies will be retained, and the non-secure cookies will be culled down | 2995 // non-secure cookies will be evicted but none of the secure ones will be |
3141 // to the limit. | 2996 // evicted. |
3142 const CookiesEntry test6[] = {{50U, true}, {50U, false}, {81U, true}}; | 2997 const CookiesEntry test6[] = {{50U, true}, {50U, false}, {81U, true}}; |
3143 TestSecureCookieEviction(test6, arraysize(test6), 131U, 19U, nullptr); | 2998 TestSecureCookieEviction(test6, arraysize(test6), 131U, 0U, nullptr); |
3144 | 2999 |
3145 // If the same number of non-secure and secure cookies are created (50 each) | 3000 // If the same number of non-secure and secure cookies are created (50 each) |
3146 // below the per domain limit (180), and then another set of non-secure | 3001 // below the per domain limit (180), and then another set of non-secure |
3147 // cookies are created to bring the total above the per-domain limit, all | 3002 // cookies are created to bring the total above the per-domain limit, all of |
3148 // secure cookies will be retained, and the non-secure cookies will be culled | 3003 // the non-secure cookies will be evicted but none of the secure ones will be |
3149 // down to the limit. | 3004 // evicted. |
3150 const CookiesEntry test7[] = {{50U, false}, {50U, true}, {81U, false}}; | 3005 const CookiesEntry test7[] = {{50U, false}, {50U, true}, {81U, false}}; |
3151 TestSecureCookieEviction(test7, arraysize(test7), 50U, 100U, nullptr); | 3006 TestSecureCookieEviction(test7, arraysize(test7), 50U, 0U, nullptr); |
3152 | 3007 |
3153 // If the same number of non-secure and secure cookies are created (50 each) | 3008 // If the same number of non-secure and secure cookies are created (50 each) |
3154 // below the per domain limit (180), and then another set of non-secure | 3009 // below the per domain limit (180), and then another set of non-secure |
3155 // cookies are created to bring the total above the per-domain limit, all | 3010 // cookies are created to bring the total above the per-domain limit, all of |
3156 // secure cookies will be retained, and the non-secure cookies will be culled | 3011 // the non-secure cookies will be evicted but none of the secure ones will be |
3157 // down to the limit, then the remaining non-secure cookies will be created | 3012 // evicted, and then the remaining non-secure cookies will be created (9). |
3158 // (9). | |
3159 const CookiesEntry test8[] = {{50U, false}, {50U, true}, {90U, false}}; | 3013 const CookiesEntry test8[] = {{50U, false}, {50U, true}, {90U, false}}; |
3160 TestSecureCookieEviction(test8, arraysize(test8), 50U, 109U, nullptr); | 3014 TestSecureCookieEviction(test8, arraysize(test8), 50U, 9U, nullptr); |
3161 | 3015 |
3162 // If a number of non-secure cookies are created on other hosts (20) and are | 3016 // If a number of non-secure cookies are created on other hosts (20) and are |
3163 // past the global 'safe' date, and then the number of non-secure cookies for | 3017 // past the global 'safe' date, and then the number of non-secure cookies for |
3164 // a single domain are brought to the per-domain limit (180), followed by | 3018 // a single domain are brought to the per-domain limit (180), followed by |
3165 // another set of secure cookies on that same domain (20), all the secure | 3019 // another set of secure cookies on that same domain (20), all of the |
3166 // cookies for that domain should be retained, while the non-secure should be | 3020 // non-secure cookies for that domain should be evicted, but the non-secure |
3167 // culled down to the per-domain limit. The non-secure cookies for other | 3021 // cookies for other domains should remain, as should the secure cookies for |
3168 // domains should remain untouched. | 3022 // that domain. |
3169 const CookiesEntry test9[] = {{180U, false}, {20U, true}}; | 3023 const CookiesEntry test9[] = {{180U, false}, {20U, true}}; |
3170 const AltHosts test9_alt_hosts(0, 20); | 3024 const AltHosts test9_alt_hosts(0, 20); |
3171 TestSecureCookieEviction(test9, arraysize(test9), 20U, 169U, | 3025 TestSecureCookieEviction(test9, arraysize(test9), 20U, 20U, &test9_alt_hosts); |
3172 &test9_alt_hosts); | |
3173 | 3026 |
3174 // If a number of secure cookies are created on other hosts and hit the global | 3027 // If a number of secure cookies are created on other hosts and hit the global |
3175 // cookie limit (3300) and are past the global 'safe' date, and then a single | 3028 // cookie limit (3300) and are past the global 'safe' date, and then a single |
3176 // non-secure cookie is created now, the secure cookies are removed so that | 3029 // non-secure cookie is created now, the secure cookies are removed so that |
3177 // the global total number of cookies is at the global purge goal (3000), but | 3030 // the global total number of cookies is at the global purge goal (3000), but |
3178 // the non-secure cookie is not evicted since it is too young. | 3031 // the non-secure cookie is not evicted since it is too young. |
3179 const CookiesEntry test10[] = {{1U, false}}; | 3032 const CookiesEntry test10[] = {{1U, false}}; |
3180 const AltHosts test10_alt_hosts(3300, 0); | 3033 const AltHosts test10_alt_hosts(3300, 0); |
3181 TestSecureCookieEviction(test10, arraysize(test10), 2999U, 1U, | 3034 TestSecureCookieEviction(test10, arraysize(test10), 2999U, 1U, |
3182 &test10_alt_hosts); | 3035 &test10_alt_hosts); |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3470 monster()->AddCallbackForCookie( | 3323 monster()->AddCallbackForCookie( |
3471 test_url_, "abc", | 3324 test_url_, "abc", |
3472 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); | 3325 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); |
3473 SetCookie(monster(), test_url_, "abc=def"); | 3326 SetCookie(monster(), test_url_, "abc=def"); |
3474 base::MessageLoop::current()->RunUntilIdle(); | 3327 base::MessageLoop::current()->RunUntilIdle(); |
3475 EXPECT_EQ(1U, cookies0.size()); | 3328 EXPECT_EQ(1U, cookies0.size()); |
3476 EXPECT_EQ(1U, cookies0.size()); | 3329 EXPECT_EQ(1U, cookies0.size()); |
3477 } | 3330 } |
3478 | 3331 |
3479 } // namespace net | 3332 } // namespace net |
OLD | NEW |