Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(532)

Side by Side Diff: net/cookies/cookie_monster_unittest.cc

Issue 1698693002: Make CookieStore no longer threadsafe. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@getcookiemonster
Patch Set: merge Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 static const bool filters_schemes = true; 95 static const bool filters_schemes = true;
96 static const bool has_path_prefix_bug = false; 96 static const bool has_path_prefix_bug = false;
97 static const int creation_time_granularity_in_ms = 0; 97 static const int creation_time_granularity_in_ms = 0;
98 static const bool enforce_strict_secure = true; 98 static const bool enforce_strict_secure = true;
99 }; 99 };
100 100
101 INSTANTIATE_TYPED_TEST_CASE_P(CookieMonster, 101 INSTANTIATE_TYPED_TEST_CASE_P(CookieMonster,
102 CookieStoreTest, 102 CookieStoreTest,
103 CookieMonsterTestTraits); 103 CookieMonsterTestTraits);
104 104
105 INSTANTIATE_TYPED_TEST_CASE_P(CookieMonster,
106 MultiThreadedCookieStoreTest,
107 CookieMonsterTestTraits);
108
109 INSTANTIATE_TYPED_TEST_CASE_P(CookieMonsterStrictSecure, 105 INSTANTIATE_TYPED_TEST_CASE_P(CookieMonsterStrictSecure,
110 CookieStoreTest, 106 CookieStoreTest,
111 CookieMonsterEnforcingStrictSecure); 107 CookieMonsterEnforcingStrictSecure);
112 108
113 template <typename T> 109 template <typename T>
114 class CookieMonsterTestBase : public CookieStoreTest<T> { 110 class CookieMonsterTestBase : public CookieStoreTest<T> {
115 public: 111 public:
116 using CookieStoreTest<T>::SetCookie; 112 using CookieStoreTest<T>::SetCookie;
117 113
118 protected: 114 protected:
(...skipping 2339 matching lines...) Expand 10 before | Expand all | Expand 10 after
2458 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount()); 2454 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount());
2459 2455
2460 // kValidCookieLine creates a session cookie. 2456 // kValidCookieLine creates a session cookie.
2461 ASSERT_TRUE(SetCookie(cm.get(), http_www_google_.url(), kValidCookieLine)); 2457 ASSERT_TRUE(SetCookie(cm.get(), http_www_google_.url(), kValidCookieLine));
2462 2458
2463 scoped_ptr<base::HistogramSamples> samples3( 2459 scoped_ptr<base::HistogramSamples> samples3(
2464 expired_histogram->SnapshotSamples()); 2460 expired_histogram->SnapshotSamples());
2465 EXPECT_EQ(samples2->TotalCount(), samples3->TotalCount()); 2461 EXPECT_EQ(samples2->TotalCount(), samples3->TotalCount());
2466 } 2462 }
2467 2463
2468 namespace {
2469
2470 class MultiThreadedCookieMonsterTest : public CookieMonsterTest {
2471 public:
2472 MultiThreadedCookieMonsterTest() : other_thread_("CMTthread") {}
2473
2474 // Helper methods for calling the asynchronous CookieMonster methods
2475 // from a different thread.
2476
2477 void GetAllCookiesTask(CookieMonster* cm, GetCookieListCallback* callback) {
2478 cm->GetAllCookiesAsync(
2479 base::Bind(&GetCookieListCallback::Run, base::Unretained(callback)));
2480 }
2481
2482 void GetAllCookiesForURLTask(CookieMonster* cm,
2483 const GURL& url,
2484 GetCookieListCallback* callback) {
2485 cm->GetAllCookiesForURLAsync(url, base::Bind(&GetCookieListCallback::Run,
2486 base::Unretained(callback)));
2487 }
2488
2489 void GetAllCookiesForURLWithOptionsTask(CookieMonster* cm,
2490 const GURL& url,
2491 const CookieOptions& options,
2492 GetCookieListCallback* callback) {
2493 cm->GetCookieListWithOptionsAsync(
2494 url, options,
2495 base::Bind(&GetCookieListCallback::Run, base::Unretained(callback)));
2496 }
2497
2498 void SetCookieWithDetailsTask(CookieMonster* cm,
2499 const GURL& url,
2500 ResultSavingCookieCallback<bool>* callback) {
2501 // Define the parameters here instead of in the calling fucntion.
2502 // The maximum number of parameters for Bind function is 6.
2503 std::string name = "A";
2504 std::string value = "B";
2505 std::string domain = std::string();
2506 std::string path = "/foo";
2507 base::Time expiration_time = base::Time();
2508 bool secure = false;
2509 bool http_only = false;
2510 bool same_site = false;
2511 CookiePriority priority = COOKIE_PRIORITY_DEFAULT;
2512 cm->SetCookieWithDetailsAsync(
2513 url, name, value, domain, path, base::Time(), expiration_time,
2514 base::Time(), secure, http_only, same_site,
2515 false /* enforces strict secure cookies */, priority,
2516 base::Bind(&ResultSavingCookieCallback<bool>::Run,
2517 base::Unretained(callback)));
2518 }
2519
2520 void DeleteAllCreatedBetweenTask(CookieMonster* cm,
2521 const base::Time& delete_begin,
2522 const base::Time& delete_end,
2523 ResultSavingCookieCallback<int>* callback) {
2524 cm->DeleteAllCreatedBetweenAsync(
2525 delete_begin, delete_end,
2526 base::Bind(&ResultSavingCookieCallback<int>::Run,
2527 base::Unretained(callback)));
2528 }
2529
2530 void DeleteAllCreatedBetweenForHostTask(
2531 CookieMonster* cm,
2532 const base::Time delete_begin,
2533 const base::Time delete_end,
2534 const GURL& url,
2535 ResultSavingCookieCallback<int>* callback) {
2536 cm->DeleteAllCreatedBetweenForHostAsync(
2537 delete_begin, delete_end, url,
2538 base::Bind(&ResultSavingCookieCallback<int>::Run,
2539 base::Unretained(callback)));
2540 }
2541
2542 void DeleteCanonicalCookieTask(CookieMonster* cm,
2543 const CanonicalCookie& cookie,
2544 ResultSavingCookieCallback<int>* callback) {
2545 cm->DeleteCanonicalCookieAsync(
2546 cookie, base::Bind(&ResultSavingCookieCallback<int>::Run,
2547 base::Unretained(callback)));
2548 }
2549
2550 protected:
2551 void RunOnOtherThread(const base::Closure& task) {
2552 other_thread_.Start();
2553 other_thread_.task_runner()->PostTask(FROM_HERE, task);
2554 other_thread_.Stop();
2555 }
2556
2557 Thread other_thread_;
2558 };
2559
2560 } // namespace
2561
2562 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookies) {
2563 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2564 EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "A=B"));
2565 CookieList cookies = GetAllCookies(cm.get());
2566 CookieList::const_iterator it = cookies.begin();
2567 ASSERT_TRUE(it != cookies.end());
2568 EXPECT_EQ(http_www_google_.host(), it->Domain());
2569 EXPECT_EQ("A", it->Name());
2570 ASSERT_TRUE(++it == cookies.end());
2571 GetCookieListCallback callback(&other_thread_);
2572 base::Closure task =
2573 base::Bind(&MultiThreadedCookieMonsterTest::GetAllCookiesTask,
2574 base::Unretained(this), cm, &callback);
2575 RunOnOtherThread(task);
2576 callback.WaitUntilDone();
2577 it = callback.cookies().begin();
2578 ASSERT_TRUE(it != callback.cookies().end());
2579 EXPECT_EQ(http_www_google_.host(), it->Domain());
2580 EXPECT_EQ("A", it->Name());
2581 ASSERT_TRUE(++it == callback.cookies().end());
2582 }
2583
2584 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookiesForURL) {
2585 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2586 EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "A=B"));
2587 CookieList cookies = GetAllCookiesForURL(cm.get(), http_www_google_.url());
2588 CookieList::const_iterator it = cookies.begin();
2589 ASSERT_TRUE(it != cookies.end());
2590 EXPECT_EQ(http_www_google_.host(), it->Domain());
2591 EXPECT_EQ("A", it->Name());
2592 ASSERT_TRUE(++it == cookies.end());
2593 GetCookieListCallback callback(&other_thread_);
2594 base::Closure task =
2595 base::Bind(&MultiThreadedCookieMonsterTest::GetAllCookiesForURLTask,
2596 base::Unretained(this), cm, http_www_google_.url(), &callback);
2597 RunOnOtherThread(task);
2598 callback.WaitUntilDone();
2599 it = callback.cookies().begin();
2600 ASSERT_TRUE(it != callback.cookies().end());
2601 EXPECT_EQ(http_www_google_.host(), it->Domain());
2602 EXPECT_EQ("A", it->Name());
2603 ASSERT_TRUE(++it == callback.cookies().end());
2604 }
2605
2606 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookiesForURLWithOpt) {
2607 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2608 EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "A=B"));
2609 CookieOptions options;
2610 CookieList cookies =
2611 GetAllCookiesForURLWithOptions(cm.get(), http_www_google_.url(), options);
2612 CookieList::const_iterator it = cookies.begin();
2613 ASSERT_TRUE(it != cookies.end());
2614 EXPECT_EQ(http_www_google_.host(), it->Domain());
2615 EXPECT_EQ("A", it->Name());
2616 ASSERT_TRUE(++it == cookies.end());
2617 GetCookieListCallback callback(&other_thread_);
2618 base::Closure task = base::Bind(
2619 &MultiThreadedCookieMonsterTest::GetAllCookiesForURLWithOptionsTask,
2620 base::Unretained(this), cm, http_www_google_.url(), options, &callback);
2621 RunOnOtherThread(task);
2622 callback.WaitUntilDone();
2623 it = callback.cookies().begin();
2624 ASSERT_TRUE(it != callback.cookies().end());
2625 EXPECT_EQ(http_www_google_.host(), it->Domain());
2626 EXPECT_EQ("A", it->Name());
2627 ASSERT_TRUE(++it == callback.cookies().end());
2628 }
2629
2630 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckSetCookieWithDetails) {
2631 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2632 EXPECT_TRUE(SetCookieWithDetails(cm.get(), www_google_foo_.url(), "A", "B",
2633 std::string(), "/foo", base::Time(),
2634 base::Time(), base::Time(), false, false,
2635 false, COOKIE_PRIORITY_DEFAULT));
2636 ResultSavingCookieCallback<bool> callback(&other_thread_);
2637 base::Closure task =
2638 base::Bind(&MultiThreadedCookieMonsterTest::SetCookieWithDetailsTask,
2639 base::Unretained(this), cm, www_google_foo_.url(), &callback);
2640 RunOnOtherThread(task);
2641 callback.WaitUntilDone();
2642 EXPECT_TRUE(callback.result());
2643 }
2644
2645 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteAllCreatedBetween) {
2646 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2647 CookieOptions options;
2648 Time now = Time::Now();
2649 EXPECT_TRUE(
2650 SetCookieWithOptions(cm.get(), http_www_google_.url(), "A=B", options));
2651 EXPECT_EQ(1, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(99),
2652 Time()));
2653 EXPECT_TRUE(
2654 SetCookieWithOptions(cm.get(), http_www_google_.url(), "A=B", options));
2655 ResultSavingCookieCallback<int> callback(&other_thread_);
2656 base::Closure task =
2657 base::Bind(&MultiThreadedCookieMonsterTest::DeleteAllCreatedBetweenTask,
2658 base::Unretained(this), cm, now - TimeDelta::FromDays(99),
2659 Time(), &callback);
2660 RunOnOtherThread(task);
2661 callback.WaitUntilDone();
2662 EXPECT_EQ(1, callback.result());
2663 }
2664
2665 TEST_F(MultiThreadedCookieMonsterTest,
2666 ThreadCheckDeleteAllCreatedBetweenForHost) {
2667 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2668 GURL url_not_google("http://www.notgoogle.com");
2669
2670 CookieOptions options;
2671 Time now = Time::Now();
2672 // ago1 < ago2 < ago3 < now.
2673 Time ago1 = now - TimeDelta::FromDays(101);
2674 Time ago2 = now - TimeDelta::FromDays(100);
2675 Time ago3 = now - TimeDelta::FromDays(99);
2676
2677 // These 3 cookies match the first deletion.
2678 EXPECT_TRUE(
2679 SetCookieWithOptions(cm.get(), http_www_google_.url(), "A=B", options));
2680 EXPECT_TRUE(
2681 SetCookieWithOptions(cm.get(), http_www_google_.url(), "C=D", options));
2682 EXPECT_TRUE(
2683 SetCookieWithOptions(cm.get(), http_www_google_.url(), "Y=Z", options));
2684
2685 // This cookie does not match host.
2686 EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_not_google, "E=F", options));
2687
2688 // This cookie does not match time range: [ago3, inf], for first deletion, but
2689 // matches for the second deletion.
2690 EXPECT_TRUE(
2691 cm->SetCookieWithCreationTime(http_www_google_.url(), "G=H", ago2));
2692
2693 // 1. First set of deletions.
2694 EXPECT_EQ(3, // Deletes A=B, C=D, Y=Z
2695 DeleteAllCreatedBetweenForHost(cm.get(), ago3, Time::Max(),
2696 http_www_google_.url()));
2697
2698 EXPECT_TRUE(
2699 SetCookieWithOptions(cm.get(), http_www_google_.url(), "A=B", options));
2700 ResultSavingCookieCallback<int> callback(&other_thread_);
2701
2702 // 2. Second set of deletions.
2703 base::Closure task = base::Bind(
2704 &MultiThreadedCookieMonsterTest::DeleteAllCreatedBetweenForHostTask,
2705 base::Unretained(this), cm, ago1, Time(), http_www_google_.url(),
2706 &callback);
2707 RunOnOtherThread(task);
2708 callback.WaitUntilDone();
2709 EXPECT_EQ(2, callback.result()); // Deletes A=B, G=H.
2710 }
2711
2712 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteCanonicalCookie) {
2713 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2714 CookieOptions options;
2715 EXPECT_TRUE(
2716 SetCookieWithOptions(cm.get(), http_www_google_.url(), "A=B", options));
2717 CookieList cookies = GetAllCookies(cm.get());
2718 CookieList::iterator it = cookies.begin();
2719 EXPECT_TRUE(DeleteCanonicalCookie(cm.get(), *it));
2720
2721 EXPECT_TRUE(
2722 SetCookieWithOptions(cm.get(), http_www_google_.url(), "A=B", options));
2723 ResultSavingCookieCallback<int> callback(&other_thread_);
2724 cookies = GetAllCookies(cm.get());
2725 it = cookies.begin();
2726 base::Closure task =
2727 base::Bind(&MultiThreadedCookieMonsterTest::DeleteCanonicalCookieTask,
2728 base::Unretained(this), cm, *it, &callback);
2729 RunOnOtherThread(task);
2730 callback.WaitUntilDone();
2731 EXPECT_EQ(1, callback.result());
2732 }
2733
2734 // Ensure that cookies for http, https, ws, and wss all share the same storage
2735 // and policies when GetAllCookiesForURLAsync is used. This test is part of
2736 // MultiThreadedCookieMonsterTest in order to test and use
2737 // GetAllCookiesForURLAsync, but it does not use any additional threads.
2738 TEST_F(MultiThreadedCookieMonsterTest, GetAllCookiesForURLEffectiveDomain) {
2739 scoped_ptr<CanonicalCookie> cookie(CanonicalCookie::Create(
2740 http_www_google_.url(), kValidCookieLine, Time::Now(), CookieOptions()));
2741
2742 // This cookie will be freed by the CookieMonster.
2743 std::vector<CanonicalCookie*> cookies = {new CanonicalCookie(*cookie)};
2744 scoped_refptr<NewMockPersistentCookieStore> store(
2745 new NewMockPersistentCookieStore);
2746 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL));
2747
2748 CookieMonster::PersistentCookieStore::LoadedCallback loaded_callback;
2749 ::testing::StrictMock<::testing::MockFunction<void(int)>> checkpoint;
2750 const std::string key = cookie_util::GetEffectiveDomain(
2751 http_www_google_.url().scheme(), http_www_google_.url().host());
2752
2753 ::testing::InSequence s;
2754 EXPECT_CALL(checkpoint, Call(0));
2755 EXPECT_CALL(*store, Load(::testing::_));
2756 EXPECT_CALL(*store, LoadCookiesForKey(key, ::testing::_))
2757 .WillOnce(::testing::SaveArg<1>(&loaded_callback));
2758 EXPECT_CALL(checkpoint, Call(1));
2759 // LoadCookiesForKey will never be called after checkpoint.Call(1) although
2760 // we will call GetAllCookiesForURLAsync again, because all URLs below share
2761 // the same key.
2762 EXPECT_CALL(*store, LoadCookiesForKey(::testing::_, ::testing::_)).Times(0);
2763
2764 GetCookieListCallback callback;
2765 checkpoint.Call(0);
2766 GetAllCookiesForURLTask(cm.get(), http_www_google_.url(), &callback);
2767 checkpoint.Call(1);
2768 // Pass the cookies to the CookieMonster.
2769 loaded_callback.Run(cookies);
2770 // Now GetAllCookiesForURLTask is done.
2771 callback.WaitUntilDone();
2772 // See that the callback was called with the cookies.
2773 ASSERT_EQ(1u, callback.cookies().size());
2774 EXPECT_TRUE(cookie->IsEquivalent(callback.cookies()[0]));
2775
2776 // All urls in |urls| should share the same cookie domain.
2777 const GURL kUrls[] = {
2778 http_www_google_.url(), https_www_google_.url(), ws_www_google_.url(),
2779 wss_www_google_.url(),
2780 };
2781 for (const GURL& url : kUrls) {
2782 // Call the function with |url| and verify it is done synchronously without
2783 // calling LoadCookiesForKey.
2784 GetCookieListCallback callback;
2785 GetAllCookiesForURLTask(cm.get(), url, &callback);
2786 callback.WaitUntilDone();
2787 ASSERT_EQ(1u, callback.cookies().size());
2788 EXPECT_TRUE(cookie->IsEquivalent(callback.cookies()[0]));
2789 }
2790 }
2791
2792 TEST_F(CookieMonsterTest, InvalidExpiryTime) { 2464 TEST_F(CookieMonsterTest, InvalidExpiryTime) {
2793 std::string cookie_line = 2465 std::string cookie_line =
2794 std::string(kValidCookieLine) + "; expires=Blarg arg arg"; 2466 std::string(kValidCookieLine) + "; expires=Blarg arg arg";
2795 scoped_ptr<CanonicalCookie> cookie(CanonicalCookie::Create( 2467 scoped_ptr<CanonicalCookie> cookie(CanonicalCookie::Create(
2796 http_www_google_.url(), cookie_line, Time::Now(), CookieOptions())); 2468 http_www_google_.url(), cookie_line, Time::Now(), CookieOptions()));
2797 ASSERT_FALSE(cookie->IsPersistent()); 2469 ASSERT_FALSE(cookie->IsPersistent());
2798 } 2470 }
2799 2471
2800 // Test that CookieMonster writes session cookies into the underlying 2472 // Test that CookieMonster writes session cookies into the underlying
2801 // CookieStore if the "persist session cookies" option is on. 2473 // CookieStore if the "persist session cookies" option is on.
(...skipping 687 matching lines...) Expand 10 before | Expand all | Expand 10 after
3489 monster()->AddCallbackForCookie( 3161 monster()->AddCallbackForCookie(
3490 test_url_, "abc", 3162 test_url_, "abc",
3491 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); 3163 base::Bind(&RecordCookieChanges, &cookies1, nullptr)));
3492 SetCookie(monster(), test_url_, "abc=def"); 3164 SetCookie(monster(), test_url_, "abc=def");
3493 base::MessageLoop::current()->RunUntilIdle(); 3165 base::MessageLoop::current()->RunUntilIdle();
3494 EXPECT_EQ(1U, cookies0.size()); 3166 EXPECT_EQ(1U, cookies0.size());
3495 EXPECT_EQ(1U, cookies0.size()); 3167 EXPECT_EQ(1U, cookies0.size());
3496 } 3168 }
3497 3169
3498 } // namespace net 3170 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698