| 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_monster.h" | 5 #include "net/cookies/cookie_monster.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 2642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2653 | 2653 |
| 2654 ASSERT_TRUE(++it != cookies.end()); | 2654 ASSERT_TRUE(++it != cookies.end()); |
| 2655 EXPECT_EQ("A", it->Name()); | 2655 EXPECT_EQ("A", it->Name()); |
| 2656 EXPECT_EQ("B", it->Value()); | 2656 EXPECT_EQ("B", it->Value()); |
| 2657 | 2657 |
| 2658 ASSERT_TRUE(++it != cookies.end()); | 2658 ASSERT_TRUE(++it != cookies.end()); |
| 2659 EXPECT_EQ("Y", it->Name()); | 2659 EXPECT_EQ("Y", it->Name()); |
| 2660 EXPECT_EQ("Z", it->Value()); | 2660 EXPECT_EQ("Z", it->Value()); |
| 2661 } | 2661 } |
| 2662 | 2662 |
| 2663 TEST_F(CookieMonsterTest, ComputeCookieDiff) { | |
| 2664 std::unique_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); | |
| 2665 | |
| 2666 base::Time now = base::Time::Now(); | |
| 2667 base::Time creation_time = now - base::TimeDelta::FromSeconds(1); | |
| 2668 | |
| 2669 std::unique_ptr<CanonicalCookie> cookie1(base::MakeUnique<CanonicalCookie>( | |
| 2670 "A", "B", "." + http_www_foo_.url().host(), "/", creation_time, | |
| 2671 base::Time(), base::Time(), false, false, CookieSameSite::DEFAULT_MODE, | |
| 2672 COOKIE_PRIORITY_DEFAULT)); | |
| 2673 std::unique_ptr<CanonicalCookie> cookie2(base::MakeUnique<CanonicalCookie>( | |
| 2674 "C", "D", "." + http_www_foo_.url().host(), "/", creation_time, | |
| 2675 base::Time(), base::Time(), false, false, CookieSameSite::DEFAULT_MODE, | |
| 2676 COOKIE_PRIORITY_DEFAULT)); | |
| 2677 std::unique_ptr<CanonicalCookie> cookie3(base::MakeUnique<CanonicalCookie>( | |
| 2678 "E", "F", "." + http_www_foo_.url().host(), "/", creation_time, | |
| 2679 base::Time(), base::Time(), false, false, CookieSameSite::DEFAULT_MODE, | |
| 2680 COOKIE_PRIORITY_DEFAULT)); | |
| 2681 std::unique_ptr<CanonicalCookie> cookie4(base::MakeUnique<CanonicalCookie>( | |
| 2682 "G", "H", "." + http_www_foo_.url().host(), "/", creation_time, | |
| 2683 base::Time(), base::Time(), false, false, CookieSameSite::DEFAULT_MODE, | |
| 2684 COOKIE_PRIORITY_DEFAULT)); | |
| 2685 std::unique_ptr<CanonicalCookie> cookie4_with_new_value( | |
| 2686 base::MakeUnique<CanonicalCookie>( | |
| 2687 "G", "iamnew", "." + http_www_foo_.url().host(), "/", creation_time, | |
| 2688 base::Time(), base::Time(), false, false, | |
| 2689 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); | |
| 2690 std::unique_ptr<CanonicalCookie> cookie5(base::MakeUnique<CanonicalCookie>( | |
| 2691 "I", "J", "." + http_www_foo_.url().host(), "/", creation_time, | |
| 2692 base::Time(), base::Time(), false, false, CookieSameSite::DEFAULT_MODE, | |
| 2693 COOKIE_PRIORITY_DEFAULT)); | |
| 2694 std::unique_ptr<CanonicalCookie> cookie5_with_new_creation_time( | |
| 2695 base::MakeUnique<CanonicalCookie>( | |
| 2696 "I", "J", "." + http_www_foo_.url().host(), "/", now, base::Time(), | |
| 2697 base::Time(), false, false, CookieSameSite::DEFAULT_MODE, | |
| 2698 COOKIE_PRIORITY_DEFAULT)); | |
| 2699 std::unique_ptr<CanonicalCookie> cookie6(base::MakeUnique<CanonicalCookie>( | |
| 2700 "K", "L", "." + http_www_foo_.url().host(), "/foo", creation_time, | |
| 2701 base::Time(), base::Time(), false, false, CookieSameSite::DEFAULT_MODE, | |
| 2702 COOKIE_PRIORITY_DEFAULT)); | |
| 2703 std::unique_ptr<CanonicalCookie> cookie6_with_new_path( | |
| 2704 base::MakeUnique<CanonicalCookie>( | |
| 2705 "K", "L", "." + http_www_foo_.url().host(), "/bar", creation_time, | |
| 2706 base::Time(), base::Time(), false, false, | |
| 2707 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); | |
| 2708 std::unique_ptr<CanonicalCookie> cookie7(base::MakeUnique<CanonicalCookie>( | |
| 2709 "M", "N", "." + http_www_foo_.url().host(), "/foo", creation_time, | |
| 2710 base::Time(), base::Time(), false, false, CookieSameSite::DEFAULT_MODE, | |
| 2711 COOKIE_PRIORITY_DEFAULT)); | |
| 2712 std::unique_ptr<CanonicalCookie> cookie7_with_new_path( | |
| 2713 base::MakeUnique<CanonicalCookie>( | |
| 2714 "M", "N", "." + http_www_foo_.url().host(), "/bar", creation_time, | |
| 2715 base::Time(), base::Time(), false, false, | |
| 2716 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT)); | |
| 2717 | |
| 2718 CookieList old_cookies; | |
| 2719 old_cookies.push_back(*cookie1); | |
| 2720 old_cookies.push_back(*cookie2); | |
| 2721 old_cookies.push_back(*cookie4); | |
| 2722 old_cookies.push_back(*cookie5); | |
| 2723 old_cookies.push_back(*cookie6); | |
| 2724 old_cookies.push_back(*cookie7); | |
| 2725 | |
| 2726 CookieList new_cookies; | |
| 2727 new_cookies.push_back(*cookie1); | |
| 2728 new_cookies.push_back(*cookie3); | |
| 2729 new_cookies.push_back(*cookie4_with_new_value); | |
| 2730 new_cookies.push_back(*cookie5_with_new_creation_time); | |
| 2731 new_cookies.push_back(*cookie6_with_new_path); | |
| 2732 new_cookies.push_back(*cookie7); | |
| 2733 new_cookies.push_back(*cookie7_with_new_path); | |
| 2734 | |
| 2735 CookieList cookies_to_add; | |
| 2736 CookieList cookies_to_delete; | |
| 2737 | |
| 2738 cm->ComputeCookieDiff(&old_cookies, &new_cookies, &cookies_to_add, | |
| 2739 &cookies_to_delete); | |
| 2740 | |
| 2741 // |cookie1| has not changed. | |
| 2742 EXPECT_FALSE(IsCookieInList(*cookie1, cookies_to_add)); | |
| 2743 EXPECT_FALSE(IsCookieInList(*cookie1, cookies_to_delete)); | |
| 2744 | |
| 2745 // |cookie2| has been deleted. | |
| 2746 EXPECT_FALSE(IsCookieInList(*cookie2, cookies_to_add)); | |
| 2747 EXPECT_TRUE(IsCookieInList(*cookie2, cookies_to_delete)); | |
| 2748 | |
| 2749 // |cookie3| has been added. | |
| 2750 EXPECT_TRUE(IsCookieInList(*cookie3, cookies_to_add)); | |
| 2751 EXPECT_FALSE(IsCookieInList(*cookie3, cookies_to_delete)); | |
| 2752 | |
| 2753 // |cookie4| has a new value: new cookie overrides the old one (which does not | |
| 2754 // need to be explicitly removed). | |
| 2755 EXPECT_FALSE(IsCookieInList(*cookie4, cookies_to_add)); | |
| 2756 EXPECT_FALSE(IsCookieInList(*cookie4, cookies_to_delete)); | |
| 2757 EXPECT_TRUE(IsCookieInList(*cookie4_with_new_value, cookies_to_add)); | |
| 2758 EXPECT_FALSE(IsCookieInList(*cookie4_with_new_value, cookies_to_delete)); | |
| 2759 | |
| 2760 // |cookie5| has a new creation time: new cookie overrides the old one (which | |
| 2761 // does not need to be explicitly removed). | |
| 2762 EXPECT_FALSE(IsCookieInList(*cookie5, cookies_to_add)); | |
| 2763 EXPECT_FALSE(IsCookieInList(*cookie5, cookies_to_delete)); | |
| 2764 EXPECT_TRUE(IsCookieInList(*cookie5_with_new_creation_time, cookies_to_add)); | |
| 2765 EXPECT_FALSE( | |
| 2766 IsCookieInList(*cookie5_with_new_creation_time, cookies_to_delete)); | |
| 2767 | |
| 2768 // |cookie6| has a new path: the new cookie does not overrides the old one, | |
| 2769 // which needs to be explicitly removed. | |
| 2770 EXPECT_FALSE(IsCookieInList(*cookie6, cookies_to_add)); | |
| 2771 EXPECT_TRUE(IsCookieInList(*cookie6, cookies_to_delete)); | |
| 2772 EXPECT_TRUE(IsCookieInList(*cookie6_with_new_path, cookies_to_add)); | |
| 2773 EXPECT_FALSE(IsCookieInList(*cookie6_with_new_path, cookies_to_delete)); | |
| 2774 | |
| 2775 // |cookie7| is kept and |cookie7_with_new_path| is added as a new cookie. | |
| 2776 EXPECT_FALSE(IsCookieInList(*cookie7, cookies_to_add)); | |
| 2777 EXPECT_FALSE(IsCookieInList(*cookie7, cookies_to_delete)); | |
| 2778 EXPECT_TRUE(IsCookieInList(*cookie7_with_new_path, cookies_to_add)); | |
| 2779 EXPECT_FALSE(IsCookieInList(*cookie7_with_new_path, cookies_to_delete)); | |
| 2780 } | |
| 2781 | |
| 2782 // Check that DeleteAll does flush (as a sanity check that flush_count() | 2663 // Check that DeleteAll does flush (as a sanity check that flush_count() |
| 2783 // works). | 2664 // works). |
| 2784 TEST_F(CookieMonsterTest, DeleteAll) { | 2665 TEST_F(CookieMonsterTest, DeleteAll) { |
| 2785 scoped_refptr<FlushablePersistentStore> store(new FlushablePersistentStore()); | 2666 scoped_refptr<FlushablePersistentStore> store(new FlushablePersistentStore()); |
| 2786 std::unique_ptr<CookieMonster> cm(new CookieMonster(store.get(), nullptr)); | 2667 std::unique_ptr<CookieMonster> cm(new CookieMonster(store.get(), nullptr)); |
| 2787 cm->SetPersistSessionCookies(true); | 2668 cm->SetPersistSessionCookies(true); |
| 2788 | 2669 |
| 2789 EXPECT_TRUE(SetCookie(cm.get(), http_www_foo_.url(), "X=Y; path=/")); | 2670 EXPECT_TRUE(SetCookie(cm.get(), http_www_foo_.url(), "X=Y; path=/")); |
| 2790 | 2671 |
| 2791 ASSERT_EQ(0, store->flush_count()); | 2672 ASSERT_EQ(0, store->flush_count()); |
| (...skipping 665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3457 monster()->AddCallbackForCookie( | 3338 monster()->AddCallbackForCookie( |
| 3458 test_url_, "abc", | 3339 test_url_, "abc", |
| 3459 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); | 3340 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); |
| 3460 SetCookie(monster(), test_url_, "abc=def"); | 3341 SetCookie(monster(), test_url_, "abc=def"); |
| 3461 base::RunLoop().RunUntilIdle(); | 3342 base::RunLoop().RunUntilIdle(); |
| 3462 EXPECT_EQ(1U, cookies0.size()); | 3343 EXPECT_EQ(1U, cookies0.size()); |
| 3463 EXPECT_EQ(1U, cookies0.size()); | 3344 EXPECT_EQ(1U, cookies0.size()); |
| 3464 } | 3345 } |
| 3465 | 3346 |
| 3466 } // namespace net | 3347 } // namespace net |
| OLD | NEW |