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