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 <algorithm> | 7 #include <algorithm> |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 bool SetAllCookies(CookieMonster* cm, const CookieList& list) { | 138 bool SetAllCookies(CookieMonster* cm, const CookieList& list) { |
139 DCHECK(cm); | 139 DCHECK(cm); |
140 ResultSavingCookieCallback<bool> callback; | 140 ResultSavingCookieCallback<bool> callback; |
141 cm->SetAllCookiesAsync(list, | 141 cm->SetAllCookiesAsync(list, |
142 base::Bind(&ResultSavingCookieCallback<bool>::Run, | 142 base::Bind(&ResultSavingCookieCallback<bool>::Run, |
143 base::Unretained(&callback))); | 143 base::Unretained(&callback))); |
144 callback.WaitUntilDone(); | 144 callback.WaitUntilDone(); |
145 return callback.result(); | 145 return callback.result(); |
146 } | 146 } |
147 | 147 |
| 148 bool SetCanonicalCookie(CookieMonster* cm, |
| 149 CanonicalCookie cookie, |
| 150 bool secure_source, |
| 151 bool modify_http_only) { |
| 152 DCHECK(cm); |
| 153 ResultSavingCookieCallback<bool> callback; |
| 154 cm->SetCanonicalCookieAsync( |
| 155 cookie, secure_source, modify_http_only, |
| 156 base::Bind(&ResultSavingCookieCallback<bool>::Run, |
| 157 base::Unretained(&callback))); |
| 158 callback.WaitUntilDone(); |
| 159 return callback.result(); |
| 160 } |
| 161 |
148 int DeleteAllCreatedBetween(CookieMonster* cm, | 162 int DeleteAllCreatedBetween(CookieMonster* cm, |
149 const base::Time& delete_begin, | 163 const base::Time& delete_begin, |
150 const base::Time& delete_end) { | 164 const base::Time& delete_end) { |
151 DCHECK(cm); | 165 DCHECK(cm); |
152 ResultSavingCookieCallback<int> callback; | 166 ResultSavingCookieCallback<int> callback; |
153 cm->DeleteAllCreatedBetweenAsync( | 167 cm->DeleteAllCreatedBetweenAsync( |
154 delete_begin, delete_end, | 168 delete_begin, delete_end, |
155 base::Bind(&ResultSavingCookieCallback<int>::Run, | 169 base::Bind(&ResultSavingCookieCallback<int>::Run, |
156 base::Unretained(&callback))); | 170 base::Unretained(&callback))); |
157 callback.WaitUntilDone(); | 171 callback.WaitUntilDone(); |
(...skipping 2136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2294 base::Unretained(&get_cookie_list_callback))); | 2308 base::Unretained(&get_cookie_list_callback))); |
2295 | 2309 |
2296 // Only the main load should have been queued. | 2310 // Only the main load should have been queued. |
2297 ASSERT_EQ(1u, store->commands().size()); | 2311 ASSERT_EQ(1u, store->commands().size()); |
2298 ASSERT_EQ(CookieStoreCommand::LOAD, store->commands()[0].type); | 2312 ASSERT_EQ(CookieStoreCommand::LOAD, store->commands()[0].type); |
2299 | 2313 |
2300 std::vector<std::unique_ptr<CanonicalCookie>> cookies; | 2314 std::vector<std::unique_ptr<CanonicalCookie>> cookies; |
2301 // When passed to the CookieMonster, it takes ownership of the pointed to | 2315 // When passed to the CookieMonster, it takes ownership of the pointed to |
2302 // cookies. | 2316 // cookies. |
2303 cookies.push_back( | 2317 cookies.push_back( |
2304 CanonicalCookie::Create(kUrl, "a=b", base::Time(), CookieOptions())); | 2318 CanonicalCookie::Create(kUrl, "a=b", base::Time::Now(), CookieOptions())); |
2305 ASSERT_TRUE(cookies[0]); | 2319 ASSERT_TRUE(cookies[0]); |
2306 store->commands()[0].loaded_callback.Run(std::move(cookies)); | 2320 store->commands()[0].loaded_callback.Run(std::move(cookies)); |
2307 | 2321 |
2308 delete_callback.WaitUntilDone(); | 2322 delete_callback.WaitUntilDone(); |
2309 EXPECT_EQ(1, delete_callback.result()); | 2323 EXPECT_EQ(1, delete_callback.result()); |
2310 | 2324 |
2311 get_cookie_list_callback.WaitUntilDone(); | 2325 get_cookie_list_callback.WaitUntilDone(); |
2312 EXPECT_EQ(0u, get_cookie_list_callback.cookies().size()); | 2326 EXPECT_EQ(0u, get_cookie_list_callback.cookies().size()); |
2313 } | 2327 } |
2314 | 2328 |
(...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2849 store->SetLoadExpectation(true, std::move(initial_cookies)); | 2863 store->SetLoadExpectation(true, std::move(initial_cookies)); |
2850 | 2864 |
2851 std::unique_ptr<CookieMonster> cm(new CookieMonster(store.get(), nullptr)); | 2865 std::unique_ptr<CookieMonster> cm(new CookieMonster(store.get(), nullptr)); |
2852 | 2866 |
2853 EXPECT_EQ("foo=bar; hello=world", GetCookies(cm.get(), url)); | 2867 EXPECT_EQ("foo=bar; hello=world", GetCookies(cm.get(), url)); |
2854 } | 2868 } |
2855 | 2869 |
2856 // Test that cookie source schemes are histogrammed correctly. | 2870 // Test that cookie source schemes are histogrammed correctly. |
2857 TEST_F(CookieMonsterTest, CookieSourceHistogram) { | 2871 TEST_F(CookieMonsterTest, CookieSourceHistogram) { |
2858 base::HistogramTester histograms; | 2872 base::HistogramTester histograms; |
2859 const std::string cookie_source_histogram = "Cookie.CookieSourceScheme"; | 2873 const std::string cookie_source_histogram = "Cookie.CookieSourceScheme2"; |
2860 | 2874 |
2861 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore); | 2875 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore); |
2862 std::unique_ptr<CookieMonster> cm(new CookieMonster(store.get(), nullptr)); | 2876 std::unique_ptr<CookieMonster> cm(new CookieMonster(store.get(), nullptr)); |
2863 | 2877 |
2864 histograms.ExpectTotalCount(cookie_source_histogram, 0); | 2878 histograms.ExpectTotalCount(cookie_source_histogram, 0); |
2865 | 2879 |
2866 // Set a secure cookie on a cryptographic scheme. | 2880 // Set a secure cookie on a cryptographic scheme. |
2867 EXPECT_TRUE( | 2881 EXPECT_TRUE( |
2868 SetCookie(cm.get(), https_www_google_.url(), "A=B; path=/; Secure")); | 2882 SetCookie(cm.get(), https_www_google_.url(), "A=B; path=/; Secure")); |
2869 histograms.ExpectTotalCount(cookie_source_histogram, 1); | 2883 histograms.ExpectTotalCount(cookie_source_histogram, 1); |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2992 // Set a secure cookie from a secure origin that matches the name of an | 3006 // Set a secure cookie from a secure origin that matches the name of an |
2993 // already existing cookie and is not equivalent. | 3007 // already existing cookie and is not equivalent. |
2994 EXPECT_TRUE(SetCookie(cm.get(), https_www_google_.url(), | 3008 EXPECT_TRUE(SetCookie(cm.get(), https_www_google_.url(), |
2995 "A=E; secure; path=/some/other/path")); | 3009 "A=E; secure; path=/some/other/path")); |
2996 histograms.ExpectTotalCount(cookie_source_histogram, 11); | 3010 histograms.ExpectTotalCount(cookie_source_histogram, 11); |
2997 histograms.ExpectBucketCount(cookie_source_histogram, | 3011 histograms.ExpectBucketCount(cookie_source_histogram, |
2998 CookieMonster::COOKIE_DELETE_EQUIVALENT_ATTEMPT, | 3012 CookieMonster::COOKIE_DELETE_EQUIVALENT_ATTEMPT, |
2999 7); | 3013 7); |
3000 } | 3014 } |
3001 | 3015 |
| 3016 TEST_F(CookieMonsterTest, SetCanonicalCookie) { |
| 3017 std::unique_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); |
| 3018 base::Time two_hours_ago = base::Time::Now() - base::TimeDelta::FromHours(2); |
| 3019 base::Time one_hour_ago = base::Time::Now() - base::TimeDelta::FromHours(1); |
| 3020 base::Time one_hour_from_now = |
| 3021 base::Time::Now() + base::TimeDelta::FromHours(1); |
| 3022 std::string google_foo_host(www_google_foo_.url().host()); |
| 3023 std::string google_bar_domain(www_google_bar_.domain()); |
| 3024 std::string http_google_host(http_www_google_.url().host()); |
| 3025 std::string https_google_host(https_www_google_.url().host()); |
| 3026 |
| 3027 EXPECT_TRUE(SetCanonicalCookie( |
| 3028 cm.get(), |
| 3029 CanonicalCookie("A", "B", google_foo_host, "/foo", one_hour_ago, |
| 3030 one_hour_from_now, base::Time(), false, false, |
| 3031 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT), |
| 3032 false, true)); |
| 3033 // Note that for the creation time to be set exactly, without modification, |
| 3034 // it must be different from the one set by the line above. |
| 3035 EXPECT_TRUE(SetCanonicalCookie( |
| 3036 cm.get(), |
| 3037 CanonicalCookie("C", "D", "." + google_bar_domain, "/bar", two_hours_ago, |
| 3038 base::Time(), one_hour_ago, false, true, |
| 3039 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT), |
| 3040 false, true)); |
| 3041 // Because of strict secure cookies, a cookie made by an HTTP URL should fail |
| 3042 // to create a cookie with a the secure attribute. |
| 3043 EXPECT_FALSE(SetCanonicalCookie( |
| 3044 cm.get(), |
| 3045 CanonicalCookie("E", "F", http_google_host, "/", base::Time(), |
| 3046 base::Time(), base::Time(), true, false, |
| 3047 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT), |
| 3048 false, true)); |
| 3049 EXPECT_TRUE(SetCanonicalCookie( |
| 3050 cm.get(), |
| 3051 CanonicalCookie("E", "F", https_google_host, "/", base::Time(), |
| 3052 base::Time(), base::Time(), true, false, |
| 3053 CookieSameSite::DEFAULT_MODE, COOKIE_PRIORITY_DEFAULT), |
| 3054 true, true)); |
| 3055 |
| 3056 // Get all the cookies for a given URL, regardless of properties. This 'get()' |
| 3057 // operation shouldn't update the access time, as the test checks that the |
| 3058 // access time is set properly upon creation. Updating the access time would |
| 3059 // make that difficult. |
| 3060 CookieOptions options; |
| 3061 options.set_include_httponly(); |
| 3062 options.set_same_site_cookie_mode( |
| 3063 CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); |
| 3064 options.set_do_not_update_access_time(); |
| 3065 |
| 3066 CookieList cookies = |
| 3067 GetCookieListWithOptions(cm.get(), this->www_google_foo_.url(), options); |
| 3068 CookieList::iterator it = cookies.begin(); |
| 3069 |
| 3070 ASSERT_TRUE(it != cookies.end()); |
| 3071 EXPECT_EQ("A", it->Name()); |
| 3072 EXPECT_EQ("B", it->Value()); |
| 3073 EXPECT_EQ(this->www_google_foo_.host(), it->Domain()); |
| 3074 EXPECT_EQ("/foo", it->Path()); |
| 3075 EXPECT_EQ(one_hour_ago, it->CreationDate()); |
| 3076 EXPECT_TRUE(it->IsPersistent()); |
| 3077 // Expect expiration date is in the right range. Some cookie implementations |
| 3078 // may not record it with millisecond accuracy. |
| 3079 EXPECT_LE((one_hour_from_now - it->ExpiryDate()).magnitude().InSeconds(), 5); |
| 3080 // Some CookieStores don't store last access date. |
| 3081 if (!it->LastAccessDate().is_null()) |
| 3082 EXPECT_EQ(one_hour_ago, it->LastAccessDate()); |
| 3083 EXPECT_FALSE(it->IsSecure()); |
| 3084 EXPECT_FALSE(it->IsHttpOnly()); |
| 3085 |
| 3086 ASSERT_TRUE(++it == cookies.end()); |
| 3087 |
| 3088 // Verify that the cookie was set as 'httponly' by passing in a CookieOptions |
| 3089 // that excludes them and getting an empty result. |
| 3090 cookies = GetCookieListWithOptions(cm.get(), this->www_google_bar_.url(), |
| 3091 CookieOptions()); |
| 3092 it = cookies.begin(); |
| 3093 ASSERT_TRUE(it == cookies.end()); |
| 3094 |
| 3095 // Get the cookie using the wide open |options|: |
| 3096 cookies = |
| 3097 GetCookieListWithOptions(cm.get(), this->www_google_bar_.url(), options); |
| 3098 it = cookies.begin(); |
| 3099 |
| 3100 ASSERT_TRUE(it != cookies.end()); |
| 3101 EXPECT_EQ("C", it->Name()); |
| 3102 EXPECT_EQ("D", it->Value()); |
| 3103 EXPECT_EQ(this->www_google_bar_.Format(".%D"), it->Domain()); |
| 3104 EXPECT_EQ("/bar", it->Path()); |
| 3105 EXPECT_EQ(two_hours_ago, it->CreationDate()); |
| 3106 EXPECT_FALSE(it->IsPersistent()); |
| 3107 // Some CookieStores don't store last access date. |
| 3108 if (!it->LastAccessDate().is_null()) |
| 3109 EXPECT_EQ(one_hour_ago, it->LastAccessDate()); |
| 3110 EXPECT_FALSE(it->IsSecure()); |
| 3111 EXPECT_TRUE(it->IsHttpOnly()); |
| 3112 |
| 3113 EXPECT_TRUE(++it == cookies.end()); |
| 3114 |
| 3115 cookies = GetCookieListWithOptions(cm.get(), this->https_www_google_.url(), |
| 3116 options); |
| 3117 it = cookies.begin(); |
| 3118 |
| 3119 ASSERT_TRUE(it != cookies.end()); |
| 3120 EXPECT_EQ("E", it->Name()); |
| 3121 EXPECT_EQ("F", it->Value()); |
| 3122 EXPECT_EQ("/", it->Path()); |
| 3123 EXPECT_EQ(this->https_www_google_.host(), it->Domain()); |
| 3124 // Cookie should have its creation time set, and be in a reasonable range. |
| 3125 EXPECT_LE((base::Time::Now() - it->CreationDate()).magnitude().InMinutes(), |
| 3126 2); |
| 3127 EXPECT_FALSE(it->IsPersistent()); |
| 3128 // Some CookieStores don't store last access date. |
| 3129 if (!it->LastAccessDate().is_null()) |
| 3130 EXPECT_EQ(it->CreationDate(), it->LastAccessDate()); |
| 3131 EXPECT_TRUE(it->IsSecure()); |
| 3132 EXPECT_FALSE(it->IsHttpOnly()); |
| 3133 |
| 3134 EXPECT_TRUE(++it == cookies.end()); |
| 3135 } |
| 3136 |
3002 TEST_F(CookieMonsterTest, SetSecureCookies) { | 3137 TEST_F(CookieMonsterTest, SetSecureCookies) { |
3003 std::unique_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); | 3138 std::unique_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); |
3004 GURL http_url("http://www.google.com"); | 3139 GURL http_url("http://www.google.com"); |
3005 GURL http_superdomain_url("http://google.com"); | 3140 GURL http_superdomain_url("http://google.com"); |
3006 GURL https_url("https://www.google.com"); | 3141 GURL https_url("https://www.google.com"); |
3007 | 3142 |
3008 // A non-secure cookie can be created from either a URL with a secure or | 3143 // A non-secure cookie can be created from either a URL with a secure or |
3009 // insecure scheme. | 3144 // insecure scheme. |
3010 EXPECT_TRUE(SetCookie(cm.get(), http_url, "A=C;")); | 3145 EXPECT_TRUE(SetCookie(cm.get(), http_url, "A=C;")); |
3011 EXPECT_TRUE(SetCookie(cm.get(), https_url, "A=B;")); | 3146 EXPECT_TRUE(SetCookie(cm.get(), https_url, "A=B;")); |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3371 monster()->AddCallbackForCookie( | 3506 monster()->AddCallbackForCookie( |
3372 test_url_, "abc", | 3507 test_url_, "abc", |
3373 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); | 3508 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); |
3374 SetCookie(monster(), test_url_, "abc=def"); | 3509 SetCookie(monster(), test_url_, "abc=def"); |
3375 base::RunLoop().RunUntilIdle(); | 3510 base::RunLoop().RunUntilIdle(); |
3376 EXPECT_EQ(1U, cookies0.size()); | 3511 EXPECT_EQ(1U, cookies0.size()); |
3377 EXPECT_EQ(1U, cookies0.size()); | 3512 EXPECT_EQ(1U, cookies0.size()); |
3378 } | 3513 } |
3379 | 3514 |
3380 } // namespace net | 3515 } // namespace net |
OLD | NEW |