| 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 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 case 'H': | 344 case 'H': |
| 345 return COOKIE_PRIORITY_HIGH; | 345 return COOKIE_PRIORITY_HIGH; |
| 346 } | 346 } |
| 347 NOTREACHED(); | 347 NOTREACHED(); |
| 348 return COOKIE_PRIORITY_DEFAULT; | 348 return COOKIE_PRIORITY_DEFAULT; |
| 349 } | 349 } |
| 350 | 350 |
| 351 // Instantiates a CookieMonster, adds multiple cookies (to http_www_google_) | 351 // Instantiates a CookieMonster, adds multiple cookies (to http_www_google_) |
| 352 // with priorities specified by |coded_priority_str|, and tests priority-aware | 352 // with priorities specified by |coded_priority_str|, and tests priority-aware |
| 353 // domain cookie eviction. | 353 // domain cookie eviction. |
| 354 // | 354 // |coded_priority_str| specifies a run-length-encoded string of priorities. |
| 355 // Example: |coded_priority_string| of "2MN 3LS MN 4HN" specifies sequential | 355 // Example: "2M 3L M 4H" means "MMLLLMHHHH", and speicifies sequential (i.e., |
| 356 // (i.e., from least- to most-recently accessed) insertion of 2 | 356 // from least- to most-recently accessed) insertion of 2 medium-priority |
| 357 // medium-priority non-secure cookies, 3 low-priority secure cookies, 1 | 357 // cookies, 3 low-priority cookies, 1 medium-priority cookie, and 4 |
| 358 // medium-priority non-secure cookie, and 4 high-priority non-secure cookies. | 358 // high-priority cookies. |
| 359 // | |
| 360 // Within each priority, only the least-accessed cookies should be evicted. | 359 // Within each priority, only the least-accessed cookies should be evicted. |
| 361 // Thus, to describe expected suriving cookies, it suffices to specify the | 360 // Thus, to describe expected suriving cookies, it suffices to specify the |
| 362 // expected population of surviving cookies per priority, i.e., | 361 // expected population of surviving cookies per priority, i.e., |
| 363 // |expected_low_count|, |expected_medium_count|, and |expected_high_count|. | 362 // |expected_low_count|, |expected_medium_count|, and |expected_high_count|. |
| 364 void TestPriorityCookieCase(CookieMonster* cm, | 363 void TestPriorityCookieCase(CookieMonster* cm, |
| 365 const std::string& coded_priority_str, | 364 const std::string& coded_priority_str, |
| 366 size_t expected_low_count, | 365 size_t expected_low_count, |
| 367 size_t expected_medium_count, | 366 size_t expected_medium_count, |
| 368 size_t expected_high_count, | 367 size_t expected_high_count) { |
| 369 size_t expected_nonsecure, | |
| 370 size_t expected_secure) { | |
| 371 SCOPED_TRACE(coded_priority_str.c_str()); | |
| 372 this->DeleteAll(cm); | 368 this->DeleteAll(cm); |
| 373 int next_cookie_id = 0; | 369 int next_cookie_id = 0; |
| 374 // A list of cookie IDs, indexed by secure status, then by priority. | 370 std::vector<CookiePriority> priority_list; |
| 375 std::vector<int> id_list[2][3]; | 371 std::vector<int> id_list[3]; // Indexed by CookiePriority. |
| 376 // A list of all the cookies stored, along with their properties. | |
| 377 std::vector<std::pair<bool, CookiePriority>> cookie_data; | |
| 378 | 372 |
| 379 // Parse |coded_priority_str| and add cookies. | 373 // Parse |coded_priority_str| and add cookies. |
| 380 for (const std::string& token : | 374 for (const std::string& token : |
| 381 base::SplitString(coded_priority_str, " ", base::TRIM_WHITESPACE, | 375 base::SplitString(coded_priority_str, " ", base::TRIM_WHITESPACE, |
| 382 base::SPLIT_WANT_ALL)) { | 376 base::SPLIT_WANT_ALL)) { |
| 383 DCHECK(!token.empty()); | 377 DCHECK(!token.empty()); |
| 384 | 378 // Take last character as priority. |
| 385 // Take last character as security status, then discard it. | 379 CookiePriority priority = CharToPriority(token.back()); |
| 386 bool is_secure = token[token.size() - 1] == 'S'; | 380 std::string priority_str = CookiePriorityToString(priority); |
| 387 | |
| 388 // The second-to-last character is the priority. Grab and discard it. | |
| 389 CookiePriority priority = CharToPriority(token[token.size() - 2]); | |
| 390 | |
| 391 // The rest of the string (possibly empty) specifies repetition. | 381 // The rest of the string (possibly empty) specifies repetition. |
| 392 int rep = 1; | 382 int rep = 1; |
| 393 if (!token.empty()) { | 383 if (!token.empty()) { |
| 394 bool result = base::StringToInt( | 384 bool result = base::StringToInt( |
| 395 base::StringPiece(token.begin(), token.end() - 2), &rep); | 385 base::StringPiece(token.begin(), token.end() - 1), &rep); |
| 396 DCHECK(result); | 386 DCHECK(result); |
| 397 } | 387 } |
| 398 for (; rep > 0; --rep, ++next_cookie_id) { | 388 for (; rep > 0; --rep, ++next_cookie_id) { |
| 399 std::string cookie = | 389 std::string cookie = base::StringPrintf( |
| 400 base::StringPrintf("a%d=b;priority=%s;%s", next_cookie_id, | 390 "a%d=b;priority=%s", next_cookie_id, priority_str.c_str()); |
| 401 CookiePriorityToString(priority).c_str(), | 391 EXPECT_TRUE(SetCookie(cm, http_www_google_.url(), cookie)); |
| 402 is_secure ? "secure" : ""); | 392 priority_list.push_back(priority); |
| 403 EXPECT_TRUE(SetCookie(cm, https_www_google_.url(), cookie)); | 393 id_list[priority].push_back(next_cookie_id); |
| 404 cookie_data.push_back(std::make_pair(is_secure, priority)); | |
| 405 id_list[is_secure][priority].push_back(next_cookie_id); | |
| 406 } | 394 } |
| 407 } | 395 } |
| 408 | 396 |
| 409 int num_cookies = static_cast<int>(cookie_data.size()); | 397 int num_cookies = static_cast<int>(priority_list.size()); |
| 410 // A list of cookie IDs, indexed by secure status, then by priority. | 398 std::vector<int> surviving_id_list[3]; // Indexed by CookiePriority. |
| 411 std::vector<int> surviving_id_list[2][3]; | |
| 412 | 399 |
| 413 // Parse the list of cookies | 400 // Parse the list of cookies |
| 414 std::string cookie_str = this->GetCookies(cm, https_www_google_.url()); | 401 std::string cookie_str = this->GetCookies(cm, http_www_google_.url()); |
| 415 size_t num_nonsecure = 0; | |
| 416 size_t num_secure = 0; | |
| 417 for (const std::string& token : base::SplitString( | 402 for (const std::string& token : base::SplitString( |
| 418 cookie_str, ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { | 403 cookie_str, ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { |
| 419 // Assuming *it is "a#=b", so extract and parse "#" portion. | 404 // Assuming *it is "a#=b", so extract and parse "#" portion. |
| 420 int id = -1; | 405 int id = -1; |
| 421 bool result = base::StringToInt( | 406 bool result = base::StringToInt( |
| 422 base::StringPiece(token.begin() + 1, token.end() - 2), &id); | 407 base::StringPiece(token.begin() + 1, token.end() - 2), &id); |
| 423 DCHECK(result); | 408 DCHECK(result); |
| 424 DCHECK_GE(id, 0); | 409 DCHECK_GE(id, 0); |
| 425 DCHECK_LT(id, num_cookies); | 410 DCHECK_LT(id, num_cookies); |
| 426 surviving_id_list[cookie_data[id].first][cookie_data[id].second] | 411 surviving_id_list[priority_list[id]].push_back(id); |
| 427 .push_back(id); | |
| 428 if (cookie_data[id].first) | |
| 429 num_secure += 1; | |
| 430 else | |
| 431 num_nonsecure += 1; | |
| 432 } | 412 } |
| 433 | 413 |
| 434 EXPECT_EQ(expected_nonsecure, num_nonsecure); | |
| 435 EXPECT_EQ(expected_secure, num_secure); | |
| 436 | |
| 437 // Validate each priority. | 414 // Validate each priority. |
| 438 size_t expected_count[3] = { | 415 size_t expected_count[3] = { |
| 439 expected_low_count, expected_medium_count, expected_high_count}; | 416 expected_low_count, expected_medium_count, expected_high_count}; |
| 440 for (int i = 0; i < 3; ++i) { | 417 for (int i = 0; i < 3; ++i) { |
| 441 size_t num_for_priority = | 418 DCHECK_LE(surviving_id_list[i].size(), id_list[i].size()); |
| 442 surviving_id_list[0][i].size() + surviving_id_list[1][i].size(); | 419 EXPECT_EQ(expected_count[i], surviving_id_list[i].size()); |
| 443 EXPECT_EQ(expected_count[i], num_for_priority); | |
| 444 // Verify that the remaining cookies are the most recent among those | 420 // Verify that the remaining cookies are the most recent among those |
| 445 // with the same priorities. | 421 // with the same priorities. |
| 446 if (expected_count[i] == num_for_priority) { | 422 if (expected_count[i] == surviving_id_list[i].size()) { |
| 447 // Non-secure: | 423 std::sort(surviving_id_list[i].begin(), surviving_id_list[i].end()); |
| 448 std::sort(surviving_id_list[0][i].begin(), | 424 EXPECT_TRUE(std::equal(surviving_id_list[i].begin(), |
| 449 surviving_id_list[0][i].end()); | 425 surviving_id_list[i].end(), |
| 450 EXPECT_TRUE(std::equal( | 426 id_list[i].end() - expected_count[i])); |
| 451 surviving_id_list[0][i].begin(), surviving_id_list[0][i].end(), | |
| 452 id_list[0][i].end() - surviving_id_list[0][i].size())); | |
| 453 | |
| 454 // Secure: | |
| 455 std::sort(surviving_id_list[1][i].begin(), | |
| 456 surviving_id_list[1][i].end()); | |
| 457 EXPECT_TRUE(std::equal( | |
| 458 surviving_id_list[1][i].begin(), surviving_id_list[1][i].end(), | |
| 459 id_list[1][i].end() - surviving_id_list[1][i].size())); | |
| 460 } | 427 } |
| 461 } | 428 } |
| 462 } | 429 } |
| 463 | 430 |
| 464 // Represents a number of cookies to create, if they are Secure cookies, and | 431 // Represents a number of cookies to create, if they are Secure cookies, and |
| 465 // a url to add them to. | 432 // a url to add them to. |
| 466 struct CookiesEntry { | 433 struct CookiesEntry { |
| 467 size_t num_cookies; | 434 size_t num_cookies; |
| 468 bool is_secure; | 435 bool is_secure; |
| 469 }; | 436 }; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 if (cookie.IsSecure()) | 483 if (cookie.IsSecure()) |
| 517 ++total_secure_cookies; | 484 ++total_secure_cookies; |
| 518 else | 485 else |
| 519 ++total_non_secure_cookies; | 486 ++total_non_secure_cookies; |
| 520 } | 487 } |
| 521 | 488 |
| 522 EXPECT_EQ(expected_secure_cookies, total_secure_cookies); | 489 EXPECT_EQ(expected_secure_cookies, total_secure_cookies); |
| 523 EXPECT_EQ(expected_non_secure_cookies, total_non_secure_cookies); | 490 EXPECT_EQ(expected_non_secure_cookies, total_non_secure_cookies); |
| 524 } | 491 } |
| 525 | 492 |
| 526 void TestPriorityAwareGarbageCollectHelperNonSecure() { | 493 void TestPriorityAwareGarbageCollectHelper() { |
| 527 // Hard-coding limits in the test, but use DCHECK_EQ to enforce constraint. | 494 // Hard-coding limits in the test, but use DCHECK_EQ to enforce constraint. |
| 528 DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies); | 495 DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies); |
| 529 DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies - | 496 DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies - |
| 530 CookieMonster::kDomainPurgeCookies); | 497 CookieMonster::kDomainPurgeCookies); |
| 498 DCHECK_EQ(30U, CookieMonster::kDomainCookiesQuotaLow); |
| 499 DCHECK_EQ(50U, CookieMonster::kDomainCookiesQuotaMedium); |
| 500 DCHECK_EQ(70U, CookieMonster::kDomainCookiesQuotaHigh); |
| 531 | 501 |
| 532 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 502 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
| 533 | 503 |
| 534 // Each test case adds 181 cookies, so 31 cookies are evicted. | 504 // Each test case adds 181 cookies, so 31 cookies are evicted. |
| 535 // Cookie same priority, repeated for each priority. | 505 // Cookie same priority, repeated for each priority. |
| 536 TestPriorityCookieCase(cm.get(), "181LN", 150U, 0U, 0U, 150U, 0U); | 506 TestPriorityCookieCase(cm.get(), "181L", 150U, 0U, 0U); |
| 537 TestPriorityCookieCase(cm.get(), "181MN", 0U, 150U, 0U, 150U, 0U); | 507 TestPriorityCookieCase(cm.get(), "181M", 0U, 150U, 0U); |
| 538 TestPriorityCookieCase(cm.get(), "181HN", 0U, 0U, 150U, 150U, 0U); | 508 TestPriorityCookieCase(cm.get(), "181H", 0U, 0U, 150U); |
| 539 | 509 |
| 540 // Pairwise scenarios. | 510 // Pairwise scenarios. |
| 541 // Round 1 => none; round2 => 31M; round 3 => none. | 511 // Round 1 => none; round2 => 31M; round 3 => none. |
| 542 TestPriorityCookieCase(cm.get(), "10HN 171MN", 0U, 140U, 10U, 150U, 0U); | 512 TestPriorityCookieCase(cm.get(), "10H 171M", 0U, 140U, 10U); |
| 543 // Round 1 => 10L; round2 => 21M; round 3 => none. | 513 // Round 1 => 10L; round2 => 21M; round 3 => none. |
| 544 TestPriorityCookieCase(cm.get(), "141MN 40LN", 30U, 120U, 0U, 150U, 0U); | 514 TestPriorityCookieCase(cm.get(), "141M 40L", 30U, 120U, 0U); |
| 545 // Round 1 => none; round2 => none; round 3 => 31H. | 515 // Round 1 => none; round2 => none; round 3 => 31H. |
| 546 TestPriorityCookieCase(cm.get(), "101HN 80MN", 0U, 80U, 70U, 150U, 0U); | 516 TestPriorityCookieCase(cm.get(), "101H 80M", 0U, 80U, 70U); |
| 547 | 517 |
| 548 // For {low, medium} priorities right on quota, different orders. | 518 // For {low, medium} priorities right on quota, different orders. |
| 549 // Round 1 => 1L; round 2 => none, round3 => 30L. | 519 // Round 1 => 1L; round 2 => none, round3 => 30L. |
| 550 TestPriorityCookieCase(cm.get(), "31LN 50MN 100HN", 0U, 50U, 100U, 150U, | 520 TestPriorityCookieCase(cm.get(), "31L 50M 100H", 0U, 50U, 100U); |
| 551 0U); | |
| 552 // Round 1 => none; round 2 => 1M, round3 => 30M. | 521 // Round 1 => none; round 2 => 1M, round3 => 30M. |
| 553 TestPriorityCookieCase(cm.get(), "51MN 100HN 30LN", 30U, 20U, 100U, 150U, | 522 TestPriorityCookieCase(cm.get(), "51M 100H 30L", 30U, 20U, 100U); |
| 554 0U); | |
| 555 // Round 1 => none; round 2 => none; round3 => 31H. | 523 // Round 1 => none; round 2 => none; round3 => 31H. |
| 556 TestPriorityCookieCase(cm.get(), "101HN 50MN 30LN", 30U, 50U, 70U, 150U, | 524 TestPriorityCookieCase(cm.get(), "101H 50M 30L", 30U, 50U, 70U); |
| 557 0U); | |
| 558 | 525 |
| 559 // Round 1 => 10L; round 2 => 10M; round3 => 11H. | 526 // Round 1 => 10L; round 2 => 10M; round3 => 11H. |
| 560 TestPriorityCookieCase(cm.get(), "81HN 60MN 40LN", 30U, 50U, 70U, 150U, 0U); | 527 TestPriorityCookieCase(cm.get(), "81H 60M 40L", 30U, 50U, 70U); |
| 561 | 528 |
| 562 // More complex scenarios. | 529 // More complex scenarios. |
| 563 // Round 1 => 10L; round 2 => 10M; round 3 => 11H. | 530 // Round 1 => 10L; round 2 => 10M; round 3 => 11H. |
| 564 TestPriorityCookieCase(cm.get(), "21HN 60MN 40LN 60HN", 30U, 50U, 70U, 150U, | 531 TestPriorityCookieCase(cm.get(), "21H 60M 40L 60H", 30U, 50U, 70U); |
| 565 0U); | |
| 566 // Round 1 => 10L; round 2 => 11M, 10L; round 3 => none. | 532 // Round 1 => 10L; round 2 => 11M, 10L; round 3 => none. |
| 567 TestPriorityCookieCase(cm.get(), "11HN 10MN 20LN 110MN 20LN 10HN", 20U, | 533 TestPriorityCookieCase(cm.get(), "11H 10M 20L 110M 20L 10H", 20U, 109U, |
| 568 109U, 21U, 150U, 0U); | 534 21U); |
| 569 // Round 1 => none; round 2 => none; round 3 => 11L, 10M, 10H. | 535 // Round 1 => none; round 2 => none; round 3 => 11L, 10M, 10H. |
| 570 TestPriorityCookieCase(cm.get(), "11LN 10MN 140HN 10MN 10LN", 10U, 10U, | 536 TestPriorityCookieCase(cm.get(), "11L 10M 140H 10M 10L", 10U, 10U, 130U); |
| 571 130U, 150U, 0U); | |
| 572 // Round 1 => none; round 2 => 1M; round 3 => 10L, 10M, 10H. | 537 // Round 1 => none; round 2 => 1M; round 3 => 10L, 10M, 10H. |
| 573 TestPriorityCookieCase(cm.get(), "11MN 10HN 10LN 60MN 90HN", 0U, 60U, 90U, | 538 TestPriorityCookieCase(cm.get(), "11M 10H 10L 60M 90H", 0U, 60U, 90U); |
| 574 150U, 0U); | |
| 575 // Round 1 => none; round 2 => 10L, 21M; round 3 => none. | 539 // Round 1 => none; round 2 => 10L, 21M; round 3 => none. |
| 576 TestPriorityCookieCase(cm.get(), "11MN 10HN 10LN 90MN 60HN", 0U, 80U, 70U, | 540 TestPriorityCookieCase(cm.get(), "11M 10H 10L 90M 60H", 0U, 80U, 70U); |
| 577 150U, 0U); | |
| 578 } | |
| 579 | |
| 580 void TestPriorityAwareGarbageCollectHelperSecure() { | |
| 581 // Hard-coding limits in the test, but use DCHECK_EQ to enforce constraint. | |
| 582 DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies); | |
| 583 DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies - | |
| 584 CookieMonster::kDomainPurgeCookies); | |
| 585 | |
| 586 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | |
| 587 | |
| 588 // Each test case adds 181 cookies, so 31 cookies are evicted. | |
| 589 // Cookie same priority, repeated for each priority. | |
| 590 TestPriorityCookieCase(cm.get(), "181LS", 150U, 0U, 0U, 0U, 150U); | |
| 591 TestPriorityCookieCase(cm.get(), "181MS", 0U, 150U, 0U, 0U, 150U); | |
| 592 TestPriorityCookieCase(cm.get(), "181HS", 0U, 0U, 150U, 0U, 150U); | |
| 593 | |
| 594 // Pairwise scenarios. | |
| 595 // Round 1 => none; round2 => 31M; round 3 => none. | |
| 596 TestPriorityCookieCase(cm.get(), "10HS 171MS", 0U, 140U, 10U, 0U, 150U); | |
| 597 // Round 1 => 10L; round2 => 21M; round 3 => none. | |
| 598 TestPriorityCookieCase(cm.get(), "141MS 40LS", 30U, 120U, 0U, 0U, 150U); | |
| 599 // Round 1 => none; round2 => none; round 3 => 31H. | |
| 600 TestPriorityCookieCase(cm.get(), "101HS 80MS", 0U, 80U, 70U, 0U, 150U); | |
| 601 | |
| 602 // For {low, medium} priorities right on quota, different orders. | |
| 603 // Round 1 => 1L; round 2 => none, round3 => 30L. | |
| 604 TestPriorityCookieCase(cm.get(), "31LS 50MS 100HS", 0U, 50U, 100U, 0U, | |
| 605 150U); | |
| 606 // Round 1 => none; round 2 => 1M, round3 => 30M. | |
| 607 TestPriorityCookieCase(cm.get(), "51MS 100HS 30LS", 30U, 20U, 100U, 0U, | |
| 608 150U); | |
| 609 // Round 1 => none; round 2 => none; round3 => 31H. | |
| 610 TestPriorityCookieCase(cm.get(), "101HS 50MS 30LS", 30U, 50U, 70U, 0U, | |
| 611 150U); | |
| 612 | |
| 613 // Round 1 => 10L; round 2 => 10M; round3 => 11H. | |
| 614 TestPriorityCookieCase(cm.get(), "81HS 60MS 40LS", 30U, 50U, 70U, 0U, 150U); | |
| 615 | |
| 616 // More complex scenarios. | |
| 617 // Round 1 => 10L; round 2 => 10M; round 3 => 11H. | |
| 618 TestPriorityCookieCase(cm.get(), "21HS 60MS 40LS 60HS", 30U, 50U, 70U, 0U, | |
| 619 150U); | |
| 620 // Round 1 => 10L; round 2 => 11M, 10L; round 3 => none. | |
| 621 TestPriorityCookieCase(cm.get(), "11HS 10MS 20LS 110MS 20LS 10HS", 20U, | |
| 622 109U, 21U, 0U, 150U); | |
| 623 // Round 1 => none; round 2 => none; round 3 => 11L, 10M, 10H. | |
| 624 TestPriorityCookieCase(cm.get(), "11LS 10MS 140HS 10MS 10LS", 10U, 10U, | |
| 625 130U, 0U, 150U); | |
| 626 // Round 1 => none; round 2 => 1M; round 3 => 10L, 10M, 10H. | |
| 627 TestPriorityCookieCase(cm.get(), "11MS 10HS 10LS 60MS 90HS", 0U, 60U, 90U, | |
| 628 0U, 150U); | |
| 629 // Round 1 => none; round 2 => 10L, 21M; round 3 => none. | |
| 630 TestPriorityCookieCase(cm.get(), "11MS 10HS 10LS 90MS 60HS", 0U, 80U, 70U, | |
| 631 0U, 150U); | |
| 632 } | |
| 633 | |
| 634 void TestPriorityAwareGarbageCollectHelperMixed() { | |
| 635 // Hard-coding limits in the test, but use DCHECK_EQ to enforce constraint. | |
| 636 DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies); | |
| 637 DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies - | |
| 638 CookieMonster::kDomainPurgeCookies); | |
| 639 | |
| 640 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | |
| 641 | |
| 642 // Each test case adds 180 secure cookies, and some non-secure cookie. The | |
| 643 // secure cookies take priority, so the non-secure cookie is removed, along | |
| 644 // with 30 secure cookies. Repeated for each priority, and with the | |
| 645 // non-secure cookie as older and newer. | |
| 646 TestPriorityCookieCase(cm.get(), "1LN 180LS", 150U, 0U, 0U, 0U, 150U); | |
| 647 TestPriorityCookieCase(cm.get(), "1MN 180MS", 0U, 150U, 0U, 0U, 150U); | |
| 648 TestPriorityCookieCase(cm.get(), "1HN 180HS", 0U, 0U, 150U, 0U, 150U); | |
| 649 TestPriorityCookieCase(cm.get(), "180LS 1LN", 150U, 0U, 0U, 0U, 150U); | |
| 650 TestPriorityCookieCase(cm.get(), "180MS 1MN", 0U, 150U, 0U, 0U, 150U); | |
| 651 TestPriorityCookieCase(cm.get(), "180HS 1HN", 0U, 0U, 150U, 0U, 150U); | |
| 652 | |
| 653 // Higher-priority non-secure cookies are removed before any secure cookie. | |
| 654 TestPriorityCookieCase(cm.get(), "180LS 1MN", 150U, 0U, 0U, 0U, 150U); | |
| 655 TestPriorityCookieCase(cm.get(), "180LS 1HN", 150U, 0U, 0U, 0U, 150U); | |
| 656 TestPriorityCookieCase(cm.get(), "180MS 1HN", 0U, 150U, 0U, 0U, 150U); | |
| 657 TestPriorityCookieCase(cm.get(), "1MN 180LS", 150U, 0U, 0U, 0U, 150U); | |
| 658 TestPriorityCookieCase(cm.get(), "1HN 180LS", 150U, 0U, 0U, 0U, 150U); | |
| 659 TestPriorityCookieCase(cm.get(), "1HN 180MS", 0U, 150U, 0U, 0U, 150U); | |
| 660 | |
| 661 // Pairwise: | |
| 662 TestPriorityCookieCase(cm.get(), "1LS 180LN", 150U, 0U, 0U, 149U, 1U); | |
| 663 TestPriorityCookieCase(cm.get(), "100LS 81LN", 150U, 0U, 0U, 50U, 100U); | |
| 664 TestPriorityCookieCase(cm.get(), "150LS 31LN", 150U, 0U, 0U, 0U, 150U); | |
| 665 TestPriorityCookieCase(cm.get(), "1LS 180HN", 1U, 0U, 149U, 149U, 1U); | |
| 666 TestPriorityCookieCase(cm.get(), "100LS 81HN", 100U, 0U, 50U, 50U, 100U); | |
| 667 TestPriorityCookieCase(cm.get(), "150LS 31HN", 150U, 0U, 0U, 0U, 150U); | |
| 668 | |
| 669 // Quota calculations inside non-secure/secure blocks remain in place: | |
| 670 TestPriorityCookieCase(cm.get(), "50HN 50LS 81HS", 50U, 0U, 100U, 19U, | |
| 671 131U); | |
| 672 TestPriorityCookieCase(cm.get(), "11MS 10HN 10LS 90MN 60HN", 10U, 79U, 61U, | |
| 673 129U, 21U); | |
| 674 | |
| 675 // Multiple GC rounds end up with consistent behavior: | |
| 676 TestPriorityCookieCase(cm.get(), "100HS 100LN 100MN", 0, 76U, 100U, 76U, | |
| 677 100U); | |
| 678 } | 541 } |
| 679 | 542 |
| 680 // Function for creating a CM with a number of cookies in it, | 543 // Function for creating a CM with a number of cookies in it, |
| 681 // no store (and hence no ability to affect access time). | 544 // no store (and hence no ability to affect access time). |
| 682 CookieMonster* CreateMonsterForGC(int num_cookies) { | 545 CookieMonster* CreateMonsterForGC(int num_cookies) { |
| 683 CookieMonster* cm(new CookieMonster(NULL, NULL)); | 546 CookieMonster* cm(new CookieMonster(NULL, NULL)); |
| 684 for (int i = 0; i < num_cookies; i++) { | 547 for (int i = 0; i < num_cookies; i++) { |
| 685 SetCookie(cm, GURL(base::StringPrintf("http://h%05d.izzle", i)), "a=1"); | 548 SetCookie(cm, GURL(base::StringPrintf("http://h%05d.izzle", i)), "a=1"); |
| 686 } | 549 } |
| 687 return cm; | 550 return cm; |
| (...skipping 709 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1397 options = CookieOptions(); | 1260 options = CookieOptions(); |
| 1398 EXPECT_EQ("A=B", | 1261 EXPECT_EQ("A=B", |
| 1399 GetCookiesWithOptions(cm.get(), http_www_google_.url(), options)); | 1262 GetCookiesWithOptions(cm.get(), http_www_google_.url(), options)); |
| 1400 EXPECT_FALSE(last_access_date == GetFirstCookieAccessDate(cm.get())); | 1263 EXPECT_FALSE(last_access_date == GetFirstCookieAccessDate(cm.get())); |
| 1401 } | 1264 } |
| 1402 | 1265 |
| 1403 TEST_F(CookieMonsterTest, TestHostGarbageCollection) { | 1266 TEST_F(CookieMonsterTest, TestHostGarbageCollection) { |
| 1404 TestHostGarbageCollectHelper(); | 1267 TestHostGarbageCollectHelper(); |
| 1405 } | 1268 } |
| 1406 | 1269 |
| 1407 TEST_F(CookieMonsterTest, TestPriorityAwareGarbageCollectionNonSecure) { | 1270 TEST_F(CookieMonsterTest, TestPriorityAwareGarbageCollection) { |
| 1408 TestPriorityAwareGarbageCollectHelperNonSecure(); | 1271 TestPriorityAwareGarbageCollectHelper(); |
| 1409 } | |
| 1410 | |
| 1411 TEST_F(CookieMonsterTest, TestPriorityAwareGarbageCollectionSecure) { | |
| 1412 TestPriorityAwareGarbageCollectHelperSecure(); | |
| 1413 } | |
| 1414 | |
| 1415 TEST_F(CookieMonsterTest, TestPriorityAwareGarbageCollectionMixed) { | |
| 1416 TestPriorityAwareGarbageCollectHelperMixed(); | |
| 1417 } | 1272 } |
| 1418 | 1273 |
| 1419 TEST_F(CookieMonsterTest, SetCookieableSchemes) { | 1274 TEST_F(CookieMonsterTest, SetCookieableSchemes) { |
| 1420 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 1275 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
| 1421 scoped_refptr<CookieMonster> cm_foo(new CookieMonster(NULL, NULL)); | 1276 scoped_refptr<CookieMonster> cm_foo(new CookieMonster(NULL, NULL)); |
| 1422 | 1277 |
| 1423 // Only cm_foo should allow foo:// cookies. | 1278 // Only cm_foo should allow foo:// cookies. |
| 1424 std::vector<std::string> schemes; | 1279 std::vector<std::string> schemes; |
| 1425 schemes.push_back("foo"); | 1280 schemes.push_back("foo"); |
| 1426 cm_foo->SetCookieableSchemes(schemes); | 1281 cm_foo->SetCookieableSchemes(schemes); |
| (...skipping 1692 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3119 DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies); | 2974 DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies); |
| 3120 DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies - | 2975 DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies - |
| 3121 CookieMonster::kDomainPurgeCookies); | 2976 CookieMonster::kDomainPurgeCookies); |
| 3122 DCHECK_EQ(3300U, CookieMonster::kMaxCookies); | 2977 DCHECK_EQ(3300U, CookieMonster::kMaxCookies); |
| 3123 DCHECK_EQ(30, CookieMonster::kSafeFromGlobalPurgeDays); | 2978 DCHECK_EQ(30, CookieMonster::kSafeFromGlobalPurgeDays); |
| 3124 | 2979 |
| 3125 // If secure cookies for one domain hit the per domain limit (180), a | 2980 // If secure cookies for one domain hit the per domain limit (180), a |
| 3126 // non-secure cookie will not evict them (and, in fact, the non-secure cookie | 2981 // non-secure cookie will not evict them (and, in fact, the non-secure cookie |
| 3127 // will be removed right after creation). | 2982 // will be removed right after creation). |
| 3128 const CookiesEntry test1[] = {{180U, true}, {1U, false}}; | 2983 const CookiesEntry test1[] = {{180U, true}, {1U, false}}; |
| 3129 TestSecureCookieEviction(test1, arraysize(test1), 150U, 0U, nullptr); | 2984 TestSecureCookieEviction(test1, arraysize(test1), 180U, 0U, nullptr); |
| 3130 | 2985 |
| 3131 // If non-secure cookies for one domain hit the per domain limit (180), the | 2986 // If non-secure cookies for one domain hit the per domain limit (180), the |
| 3132 // creation of secure cookies will evict the non-secure cookies first, making | 2987 // creation of secure cookies will evict all of the non-secure cookies, and |
| 3133 // room for the secure cookies. | 2988 // the secure cookies will still be created. |
| 3134 const CookiesEntry test2[] = {{180U, false}, {20U, true}}; | 2989 const CookiesEntry test2[] = {{180U, false}, {20U, true}}; |
| 3135 TestSecureCookieEviction(test2, arraysize(test2), 20U, 149U, nullptr); | 2990 TestSecureCookieEviction(test2, arraysize(test2), 20U, 0U, nullptr); |
| 3136 | 2991 |
| 3137 // If secure cookies for one domain go past the per domain limit (180), they | 2992 // If secure cookies for one domain go past the per domain limit (180), they |
| 3138 // will be evicted as normal by the per domain purge amount (30) down to a | 2993 // will be evicted as normal by the per domain purge amount (30) down to a |
| 3139 // lower amount (150), and then will continue to create the remaining cookies | 2994 // lower amount (150), and then will continue to create the remaining cookies |
| 3140 // (19 more to 169). | 2995 // (19 more to 169). |
| 3141 const CookiesEntry test3[] = {{200U, true}}; | 2996 const CookiesEntry test3[] = {{200U, true}}; |
| 3142 TestSecureCookieEviction(test3, arraysize(test3), 169U, 0U, nullptr); | 2997 TestSecureCookieEviction(test3, arraysize(test3), 169U, 0U, nullptr); |
| 3143 | 2998 |
| 3144 // If a non-secure cookie is created, and a number of secure cookies exceeds | 2999 // If a non-secure cookie is created, and a number of secure cookies exceeds |
| 3145 // the per domain limit (18), the total cookies will be evicted down to a | 3000 // the per domain limit (18), the total cookies will be evicted down to a |
| 3146 // lower amount (150), enforcing the eviction of the non-secure cookie, and | 3001 // lower amount (150), enforcing the eviction of the non-secure cookie, and |
| 3147 // the remaining secure cookies will be created (another 19 to 169). | 3002 // the remaining secure cookies will be created (another 18 to 168). |
| 3148 const CookiesEntry test4[] = {{1U, false}, {199U, true}}; | 3003 const CookiesEntry test4[] = {{1U, false}, {199U, true}}; |
| 3149 TestSecureCookieEviction(test4, arraysize(test4), 169U, 0U, nullptr); | 3004 TestSecureCookieEviction(test4, arraysize(test4), 168U, 0U, nullptr); |
| 3150 | 3005 |
| 3151 // If an even number of non-secure and secure cookies are created below the | 3006 // If an even number of non-secure and secure cookies are created below the |
| 3152 // per-domain limit (180), all will be created and none evicted. | 3007 // per-domain limit (180), all will be created and none evicted. |
| 3153 const CookiesEntry test5[] = {{75U, false}, {75U, true}}; | 3008 const CookiesEntry test5[] = {{75U, false}, {75U, true}}; |
| 3154 TestSecureCookieEviction(test5, arraysize(test5), 75U, 75U, nullptr); | 3009 TestSecureCookieEviction(test5, arraysize(test5), 75U, 75U, nullptr); |
| 3155 | 3010 |
| 3156 // If the same number of secure and non-secure cookies are created (50 each) | 3011 // If the same number of secure and non-secure cookies are created (50 each) |
| 3157 // below the per domain limit (180), and then another set of secure cookies | 3012 // below the per domain limit (180), and then another set of secure cookies |
| 3158 // are created to bring the total above the per-domain limit, all secure | 3013 // are created to bring the total above the per-domain limit, all of the |
| 3159 // cookies will be retained, and the non-secure cookies will be culled down | 3014 // non-secure cookies will be evicted but none of the secure ones will be |
| 3160 // to the limit. | 3015 // evicted. |
| 3161 const CookiesEntry test6[] = {{50U, true}, {50U, false}, {81U, true}}; | 3016 const CookiesEntry test6[] = {{50U, true}, {50U, false}, {81U, true}}; |
| 3162 TestSecureCookieEviction(test6, arraysize(test6), 131U, 19U, nullptr); | 3017 TestSecureCookieEviction(test6, arraysize(test6), 131U, 0U, nullptr); |
| 3163 | 3018 |
| 3164 // If the same number of non-secure and secure cookies are created (50 each) | 3019 // If the same number of non-secure and secure cookies are created (50 each) |
| 3165 // below the per domain limit (180), and then another set of non-secure | 3020 // below the per domain limit (180), and then another set of non-secure |
| 3166 // cookies are created to bring the total above the per-domain limit, all | 3021 // cookies are created to bring the total above the per-domain limit, all of |
| 3167 // secure cookies will be retained, and the non-secure cookies will be culled | 3022 // the non-secure cookies will be evicted but none of the secure ones will be |
| 3168 // down to the limit. | 3023 // evicted. |
| 3169 const CookiesEntry test7[] = {{50U, false}, {50U, true}, {81U, false}}; | 3024 const CookiesEntry test7[] = {{50U, false}, {50U, true}, {81U, false}}; |
| 3170 TestSecureCookieEviction(test7, arraysize(test7), 50U, 100U, nullptr); | 3025 TestSecureCookieEviction(test7, arraysize(test7), 50U, 0U, nullptr); |
| 3171 | 3026 |
| 3172 // If the same number of non-secure and secure cookies are created (50 each) | 3027 // If the same number of non-secure and secure cookies are created (50 each) |
| 3173 // below the per domain limit (180), and then another set of non-secure | 3028 // below the per domain limit (180), and then another set of non-secure |
| 3174 // cookies are created to bring the total above the per-domain limit, all | 3029 // cookies are created to bring the total above the per-domain limit, all of |
| 3175 // secure cookies will be retained, and the non-secure cookies will be culled | 3030 // the non-secure cookies will be evicted but none of the secure ones will be |
| 3176 // down to the limit, then the remaining non-secure cookies will be created | 3031 // evicted, and then the remaining non-secure cookies will be created (9). |
| 3177 // (9). | |
| 3178 const CookiesEntry test8[] = {{50U, false}, {50U, true}, {90U, false}}; | 3032 const CookiesEntry test8[] = {{50U, false}, {50U, true}, {90U, false}}; |
| 3179 TestSecureCookieEviction(test8, arraysize(test8), 50U, 109U, nullptr); | 3033 TestSecureCookieEviction(test8, arraysize(test8), 50U, 9U, nullptr); |
| 3180 | 3034 |
| 3181 // If a number of non-secure cookies are created on other hosts (20) and are | 3035 // If a number of non-secure cookies are created on other hosts (20) and are |
| 3182 // past the global 'safe' date, and then the number of non-secure cookies for | 3036 // past the global 'safe' date, and then the number of non-secure cookies for |
| 3183 // a single domain are brought to the per-domain limit (180), followed by | 3037 // a single domain are brought to the per-domain limit (180), followed by |
| 3184 // another set of secure cookies on that same domain (20), all the secure | 3038 // another set of secure cookies on that same domain (20), all of the |
| 3185 // cookies for that domain should be retained, while the non-secure should be | 3039 // non-secure cookies for that domain should be evicted, but the non-secure |
| 3186 // culled down to the per-domain limit. The non-secure cookies for other | 3040 // cookies for other domains should remain, as should the secure cookies for |
| 3187 // domains should remain untouched. | 3041 // that domain. |
| 3188 const CookiesEntry test9[] = {{180U, false}, {20U, true}}; | 3042 const CookiesEntry test9[] = {{180U, false}, {20U, true}}; |
| 3189 const AltHosts test9_alt_hosts(0, 20); | 3043 const AltHosts test9_alt_hosts(0, 20); |
| 3190 TestSecureCookieEviction(test9, arraysize(test9), 20U, 169U, | 3044 TestSecureCookieEviction(test9, arraysize(test9), 20U, 20U, &test9_alt_hosts); |
| 3191 &test9_alt_hosts); | |
| 3192 | 3045 |
| 3193 // If a number of secure cookies are created on other hosts and hit the global | 3046 // If a number of secure cookies are created on other hosts and hit the global |
| 3194 // cookie limit (3300) and are past the global 'safe' date, and then a single | 3047 // cookie limit (3300) and are past the global 'safe' date, and then a single |
| 3195 // non-secure cookie is created now, the secure cookies are removed so that | 3048 // non-secure cookie is created now, the secure cookies are removed so that |
| 3196 // the global total number of cookies is at the global purge goal (3000), but | 3049 // the global total number of cookies is at the global purge goal (3000), but |
| 3197 // the non-secure cookie is not evicted since it is too young. | 3050 // the non-secure cookie is not evicted since it is too young. |
| 3198 const CookiesEntry test10[] = {{1U, false}}; | 3051 const CookiesEntry test10[] = {{1U, false}}; |
| 3199 const AltHosts test10_alt_hosts(3300, 0); | 3052 const AltHosts test10_alt_hosts(3300, 0); |
| 3200 TestSecureCookieEviction(test10, arraysize(test10), 2999U, 1U, | 3053 TestSecureCookieEviction(test10, arraysize(test10), 2999U, 1U, |
| 3201 &test10_alt_hosts); | 3054 &test10_alt_hosts); |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3489 monster()->AddCallbackForCookie( | 3342 monster()->AddCallbackForCookie( |
| 3490 test_url_, "abc", | 3343 test_url_, "abc", |
| 3491 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); | 3344 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); |
| 3492 SetCookie(monster(), test_url_, "abc=def"); | 3345 SetCookie(monster(), test_url_, "abc=def"); |
| 3493 base::MessageLoop::current()->RunUntilIdle(); | 3346 base::MessageLoop::current()->RunUntilIdle(); |
| 3494 EXPECT_EQ(1U, cookies0.size()); | 3347 EXPECT_EQ(1U, cookies0.size()); |
| 3495 EXPECT_EQ(1U, cookies0.size()); | 3348 EXPECT_EQ(1U, cookies0.size()); |
| 3496 } | 3349 } |
| 3497 | 3350 |
| 3498 } // namespace net | 3351 } // namespace net |
| OLD | NEW |