| 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/canonical_cookie.h" | 5 #include "net/cookies/canonical_cookie.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/test/histogram_tester.h" | 8 #include "base/test/histogram_tester.h" |
| 9 #include "net/cookies/cookie_constants.h" | 9 #include "net/cookies/cookie_constants.h" |
| 10 #include "net/cookies/cookie_options.h" | 10 #include "net/cookies/cookie_options.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "url/gurl.h" | 12 #include "url/gurl.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 TEST(CanonicalCookieTest, Constructor) { | 16 TEST(CanonicalCookieTest, Constructor) { |
| 17 GURL url("http://www.example.com/test"); | 17 GURL url("http://www.example.com/test"); |
| 18 base::Time current_time = base::Time::Now(); | 18 base::Time current_time = base::Time::Now(); |
| 19 | 19 |
| 20 CanonicalCookie cookie(url, "A", "2", "www.example.com", "/test", | 20 CanonicalCookie cookie(url, "A", "2", "www.example.com", "/test", |
| 21 current_time, base::Time(), current_time, false, false, | 21 current_time, base::Time(), current_time, false, false, |
| 22 false, COOKIE_PRIORITY_DEFAULT); | 22 false, COOKIE_PRIORITY_DEFAULT); |
| 23 EXPECT_EQ(url.GetOrigin(), cookie.Source()); | 23 EXPECT_EQ(url.GetOrigin(), cookie.Source()); |
| 24 EXPECT_EQ("A", cookie.Name()); | 24 EXPECT_EQ("A", cookie.Name()); |
| 25 EXPECT_EQ("2", cookie.Value()); | 25 EXPECT_EQ("2", cookie.Value()); |
| 26 EXPECT_EQ("www.example.com", cookie.Domain()); | 26 EXPECT_EQ("www.example.com", cookie.Domain()); |
| 27 EXPECT_EQ("/test", cookie.Path()); | 27 EXPECT_EQ("/test", cookie.Path()); |
| 28 EXPECT_FALSE(cookie.IsSecure()); | 28 EXPECT_FALSE(cookie.IsSecure()); |
| 29 EXPECT_FALSE(cookie.IsHttpOnly()); | 29 EXPECT_FALSE(cookie.IsHttpOnly()); |
| 30 EXPECT_FALSE(cookie.IsFirstPartyOnly()); | 30 EXPECT_FALSE(cookie.IsSameSite()); |
| 31 | 31 |
| 32 CanonicalCookie cookie2(url, "A", "2", std::string(), std::string(), | 32 CanonicalCookie cookie2(url, "A", "2", std::string(), std::string(), |
| 33 current_time, base::Time(), current_time, false, | 33 current_time, base::Time(), current_time, false, |
| 34 false, false, COOKIE_PRIORITY_DEFAULT); | 34 false, false, COOKIE_PRIORITY_DEFAULT); |
| 35 EXPECT_EQ(url.GetOrigin(), cookie.Source()); | 35 EXPECT_EQ(url.GetOrigin(), cookie.Source()); |
| 36 EXPECT_EQ("A", cookie2.Name()); | 36 EXPECT_EQ("A", cookie2.Name()); |
| 37 EXPECT_EQ("2", cookie2.Value()); | 37 EXPECT_EQ("2", cookie2.Value()); |
| 38 EXPECT_EQ("", cookie2.Domain()); | 38 EXPECT_EQ("", cookie2.Domain()); |
| 39 EXPECT_EQ("", cookie2.Path()); | 39 EXPECT_EQ("", cookie2.Path()); |
| 40 EXPECT_FALSE(cookie2.IsSecure()); | 40 EXPECT_FALSE(cookie2.IsSecure()); |
| 41 EXPECT_FALSE(cookie2.IsHttpOnly()); | 41 EXPECT_FALSE(cookie2.IsHttpOnly()); |
| 42 EXPECT_FALSE(cookie2.IsFirstPartyOnly()); | 42 EXPECT_FALSE(cookie2.IsSameSite()); |
| 43 } | 43 } |
| 44 | 44 |
| 45 TEST(CanonicalCookieTest, Create) { | 45 TEST(CanonicalCookieTest, Create) { |
| 46 // Test creating cookies from a cookie string. | 46 // Test creating cookies from a cookie string. |
| 47 GURL url("http://www.example.com/test/foo.html"); | 47 GURL url("http://www.example.com/test/foo.html"); |
| 48 base::Time creation_time = base::Time::Now(); | 48 base::Time creation_time = base::Time::Now(); |
| 49 CookieOptions options; | 49 CookieOptions options; |
| 50 | 50 |
| 51 scoped_ptr<CanonicalCookie> cookie( | 51 scoped_ptr<CanonicalCookie> cookie( |
| 52 CanonicalCookie::Create(url, "A=2", creation_time, options)); | 52 CanonicalCookie::Create(url, "A=2", creation_time, options)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 76 cookie = | 76 cookie = |
| 77 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, options); | 77 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, options); |
| 78 EXPECT_FALSE(cookie.get()); | 78 EXPECT_FALSE(cookie.get()); |
| 79 CookieOptions httponly_options; | 79 CookieOptions httponly_options; |
| 80 httponly_options.set_include_httponly(); | 80 httponly_options.set_include_httponly(); |
| 81 cookie = CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, | 81 cookie = CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, |
| 82 httponly_options); | 82 httponly_options); |
| 83 EXPECT_TRUE(cookie->IsHttpOnly()); | 83 EXPECT_TRUE(cookie->IsHttpOnly()); |
| 84 | 84 |
| 85 // Test creating http only cookies. | 85 // Test creating http only cookies. |
| 86 CookieOptions first_party_options; | 86 CookieOptions same_site_options; |
| 87 first_party_options.set_include_first_party_only_cookies(); | 87 same_site_options.set_include_same_site(); |
| 88 cookie = CanonicalCookie::Create(url, "A=2; First-Party-Only", creation_time, | 88 cookie = CanonicalCookie::Create(url, "A=2; SameSite", creation_time, |
| 89 httponly_options); | 89 same_site_options); |
| 90 EXPECT_TRUE(cookie.get()); | 90 EXPECT_TRUE(cookie.get()); |
| 91 EXPECT_TRUE(cookie->IsFirstPartyOnly()); | 91 EXPECT_TRUE(cookie->IsSameSite()); |
| 92 | 92 |
| 93 // Test the creating cookies using specific parameter instead of a cookie | 93 // Test the creating cookies using specific parameter instead of a cookie |
| 94 // string. | 94 // string. |
| 95 cookie = CanonicalCookie::Create(url, "A", "2", "www.example.com", "/test", | 95 cookie = CanonicalCookie::Create(url, "A", "2", "www.example.com", "/test", |
| 96 creation_time, base::Time(), false, false, | 96 creation_time, base::Time(), false, false, |
| 97 false, false, COOKIE_PRIORITY_DEFAULT); | 97 false, false, COOKIE_PRIORITY_DEFAULT); |
| 98 EXPECT_EQ(url.GetOrigin(), cookie->Source()); | 98 EXPECT_EQ(url.GetOrigin(), cookie->Source()); |
| 99 EXPECT_EQ("A", cookie->Name()); | 99 EXPECT_EQ("A", cookie->Name()); |
| 100 EXPECT_EQ("2", cookie->Value()); | 100 EXPECT_EQ("2", cookie->Value()); |
| 101 EXPECT_EQ(".www.example.com", cookie->Domain()); | 101 EXPECT_EQ(".www.example.com", cookie->Domain()); |
| 102 EXPECT_EQ("/test", cookie->Path()); | 102 EXPECT_EQ("/test", cookie->Path()); |
| 103 EXPECT_FALSE(cookie->IsSecure()); | 103 EXPECT_FALSE(cookie->IsSecure()); |
| 104 EXPECT_FALSE(cookie->IsHttpOnly()); | 104 EXPECT_FALSE(cookie->IsHttpOnly()); |
| 105 EXPECT_FALSE(cookie->IsFirstPartyOnly()); | 105 EXPECT_FALSE(cookie->IsSameSite()); |
| 106 | 106 |
| 107 cookie = CanonicalCookie::Create(url, "A", "2", ".www.example.com", "/test", | 107 cookie = CanonicalCookie::Create(url, "A", "2", ".www.example.com", "/test", |
| 108 creation_time, base::Time(), false, false, | 108 creation_time, base::Time(), false, false, |
| 109 false, false, COOKIE_PRIORITY_DEFAULT); | 109 false, false, COOKIE_PRIORITY_DEFAULT); |
| 110 EXPECT_EQ(url.GetOrigin(), cookie->Source()); | 110 EXPECT_EQ(url.GetOrigin(), cookie->Source()); |
| 111 EXPECT_EQ("A", cookie->Name()); | 111 EXPECT_EQ("A", cookie->Name()); |
| 112 EXPECT_EQ("2", cookie->Value()); | 112 EXPECT_EQ("2", cookie->Value()); |
| 113 EXPECT_EQ(".www.example.com", cookie->Domain()); | 113 EXPECT_EQ(".www.example.com", cookie->Domain()); |
| 114 EXPECT_EQ("/test", cookie->Path()); | 114 EXPECT_EQ("/test", cookie->Path()); |
| 115 EXPECT_FALSE(cookie->IsSecure()); | 115 EXPECT_FALSE(cookie->IsSecure()); |
| 116 EXPECT_FALSE(cookie->IsHttpOnly()); | 116 EXPECT_FALSE(cookie->IsHttpOnly()); |
| 117 EXPECT_FALSE(cookie->IsFirstPartyOnly()); | 117 EXPECT_FALSE(cookie->IsSameSite()); |
| 118 } | 118 } |
| 119 | 119 |
| 120 TEST(CanonicalCookieTest, EmptyExpiry) { | 120 TEST(CanonicalCookieTest, EmptyExpiry) { |
| 121 GURL url("http://www7.ipdl.inpit.go.jp/Tokujitu/tjkta.ipdl?N0000=108"); | 121 GURL url("http://www7.ipdl.inpit.go.jp/Tokujitu/tjkta.ipdl?N0000=108"); |
| 122 base::Time creation_time = base::Time::Now(); | 122 base::Time creation_time = base::Time::Now(); |
| 123 CookieOptions options; | 123 CookieOptions options; |
| 124 | 124 |
| 125 std::string cookie_line = | 125 std::string cookie_line = |
| 126 "ACSTM=20130308043820420042; path=/; domain=ipdl.inpit.go.jp; Expires="; | 126 "ACSTM=20130308043820420042; path=/; domain=ipdl.inpit.go.jp; Expires="; |
| 127 scoped_ptr<CanonicalCookie> cookie( | 127 scoped_ptr<CanonicalCookie> cookie( |
| (...skipping 24 matching lines...) Expand all Loading... |
| 152 GURL url("http://www.example.com/"); | 152 GURL url("http://www.example.com/"); |
| 153 std::string cookie_name = "A"; | 153 std::string cookie_name = "A"; |
| 154 std::string cookie_value = "2EDA-EF"; | 154 std::string cookie_value = "2EDA-EF"; |
| 155 std::string cookie_domain = ".www.example.com"; | 155 std::string cookie_domain = ".www.example.com"; |
| 156 std::string cookie_path = "/"; | 156 std::string cookie_path = "/"; |
| 157 base::Time creation_time = base::Time::Now(); | 157 base::Time creation_time = base::Time::Now(); |
| 158 base::Time last_access_time = creation_time; | 158 base::Time last_access_time = creation_time; |
| 159 base::Time expiration_time = creation_time + base::TimeDelta::FromDays(2); | 159 base::Time expiration_time = creation_time + base::TimeDelta::FromDays(2); |
| 160 bool secure(false); | 160 bool secure(false); |
| 161 bool httponly(false); | 161 bool httponly(false); |
| 162 bool firstparty(false); | 162 bool same_site(false); |
| 163 | 163 |
| 164 // Test that a cookie is equivalent to itself. | 164 // Test that a cookie is equivalent to itself. |
| 165 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie( | 165 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie( |
| 166 url, cookie_name, cookie_value, cookie_domain, cookie_path, creation_time, | 166 url, cookie_name, cookie_value, cookie_domain, cookie_path, creation_time, |
| 167 expiration_time, last_access_time, secure, httponly, firstparty, | 167 expiration_time, last_access_time, secure, httponly, same_site, |
| 168 COOKIE_PRIORITY_MEDIUM)); | 168 COOKIE_PRIORITY_MEDIUM)); |
| 169 EXPECT_TRUE(cookie->IsEquivalent(*cookie)); | 169 EXPECT_TRUE(cookie->IsEquivalent(*cookie)); |
| 170 | 170 |
| 171 // Test that two identical cookies are equivalent. | 171 // Test that two identical cookies are equivalent. |
| 172 scoped_ptr<CanonicalCookie> other_cookie(new CanonicalCookie( | 172 scoped_ptr<CanonicalCookie> other_cookie(new CanonicalCookie( |
| 173 url, cookie_name, cookie_value, cookie_domain, cookie_path, creation_time, | 173 url, cookie_name, cookie_value, cookie_domain, cookie_path, creation_time, |
| 174 expiration_time, last_access_time, secure, httponly, firstparty, | 174 expiration_time, last_access_time, secure, httponly, same_site, |
| 175 COOKIE_PRIORITY_MEDIUM)); | 175 COOKIE_PRIORITY_MEDIUM)); |
| 176 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 176 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
| 177 | 177 |
| 178 // Tests that use different variations of attribute values that | 178 // Tests that use different variations of attribute values that |
| 179 // DON'T affect cookie equivalence. | 179 // DON'T affect cookie equivalence. |
| 180 other_cookie.reset( | 180 other_cookie.reset( |
| 181 new CanonicalCookie(url, cookie_name, "2", cookie_domain, cookie_path, | 181 new CanonicalCookie(url, cookie_name, "2", cookie_domain, cookie_path, |
| 182 creation_time, expiration_time, last_access_time, | 182 creation_time, expiration_time, last_access_time, |
| 183 secure, httponly, firstparty, COOKIE_PRIORITY_HIGH)); | 183 secure, httponly, same_site, COOKIE_PRIORITY_HIGH)); |
| 184 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 184 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
| 185 | 185 |
| 186 base::Time other_creation_time = | 186 base::Time other_creation_time = |
| 187 creation_time + base::TimeDelta::FromMinutes(2); | 187 creation_time + base::TimeDelta::FromMinutes(2); |
| 188 other_cookie.reset(new CanonicalCookie( | 188 other_cookie.reset(new CanonicalCookie( |
| 189 url, cookie_name, "2", cookie_domain, cookie_path, other_creation_time, | 189 url, cookie_name, "2", cookie_domain, cookie_path, other_creation_time, |
| 190 expiration_time, last_access_time, secure, httponly, firstparty, | 190 expiration_time, last_access_time, secure, httponly, same_site, |
| 191 COOKIE_PRIORITY_MEDIUM)); | 191 COOKIE_PRIORITY_MEDIUM)); |
| 192 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 192 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
| 193 | 193 |
| 194 other_cookie.reset(new CanonicalCookie( | 194 other_cookie.reset(new CanonicalCookie( |
| 195 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, | 195 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, |
| 196 expiration_time, last_access_time, true, httponly, firstparty, | 196 expiration_time, last_access_time, true, httponly, same_site, |
| 197 COOKIE_PRIORITY_LOW)); | 197 COOKIE_PRIORITY_LOW)); |
| 198 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 198 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
| 199 | 199 |
| 200 other_cookie.reset(new CanonicalCookie( | 200 other_cookie.reset(new CanonicalCookie( |
| 201 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, | 201 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, |
| 202 expiration_time, last_access_time, secure, true, firstparty, | 202 expiration_time, last_access_time, secure, true, same_site, |
| 203 COOKIE_PRIORITY_LOW)); | 203 COOKIE_PRIORITY_LOW)); |
| 204 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 204 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
| 205 | 205 |
| 206 other_cookie.reset(new CanonicalCookie( | 206 other_cookie.reset(new CanonicalCookie( |
| 207 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, | 207 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, |
| 208 expiration_time, last_access_time, secure, httponly, true, | 208 expiration_time, last_access_time, secure, httponly, true, |
| 209 COOKIE_PRIORITY_LOW)); | 209 COOKIE_PRIORITY_LOW)); |
| 210 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 210 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
| 211 | 211 |
| 212 // Tests that use different variations of attribute values that | 212 // Tests that use different variations of attribute values that |
| 213 // DO affect cookie equivalence. | 213 // DO affect cookie equivalence. |
| 214 other_cookie.reset(new CanonicalCookie( | 214 other_cookie.reset( |
| 215 url, "B", cookie_value, cookie_domain, cookie_path, creation_time, | 215 new CanonicalCookie(url, "B", cookie_value, cookie_domain, cookie_path, |
| 216 expiration_time, last_access_time, secure, httponly, firstparty, | 216 creation_time, expiration_time, last_access_time, |
| 217 COOKIE_PRIORITY_MEDIUM)); | 217 secure, httponly, same_site, COOKIE_PRIORITY_MEDIUM)); |
| 218 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 218 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
| 219 | 219 |
| 220 other_cookie.reset(new CanonicalCookie( | 220 other_cookie.reset(new CanonicalCookie( |
| 221 url, cookie_name, cookie_value, "www.example.com", cookie_path, | 221 url, cookie_name, cookie_value, "www.example.com", cookie_path, |
| 222 creation_time, expiration_time, last_access_time, secure, httponly, | 222 creation_time, expiration_time, last_access_time, secure, httponly, |
| 223 firstparty, COOKIE_PRIORITY_MEDIUM)); | 223 same_site, COOKIE_PRIORITY_MEDIUM)); |
| 224 EXPECT_TRUE(cookie->IsDomainCookie()); | 224 EXPECT_TRUE(cookie->IsDomainCookie()); |
| 225 EXPECT_FALSE(other_cookie->IsDomainCookie()); | 225 EXPECT_FALSE(other_cookie->IsDomainCookie()); |
| 226 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 226 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
| 227 | 227 |
| 228 other_cookie.reset(new CanonicalCookie( | 228 other_cookie.reset(new CanonicalCookie( |
| 229 url, cookie_name, cookie_value, ".example.com", cookie_path, | 229 url, cookie_name, cookie_value, ".example.com", cookie_path, |
| 230 creation_time, expiration_time, last_access_time, secure, httponly, | 230 creation_time, expiration_time, last_access_time, secure, httponly, |
| 231 firstparty, COOKIE_PRIORITY_MEDIUM)); | 231 same_site, COOKIE_PRIORITY_MEDIUM)); |
| 232 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 232 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
| 233 | 233 |
| 234 other_cookie.reset(new CanonicalCookie( | 234 other_cookie.reset(new CanonicalCookie( |
| 235 url, cookie_name, cookie_value, cookie_domain, "/test/0", creation_time, | 235 url, cookie_name, cookie_value, cookie_domain, "/test/0", creation_time, |
| 236 expiration_time, last_access_time, secure, httponly, firstparty, | 236 expiration_time, last_access_time, secure, httponly, same_site, |
| 237 COOKIE_PRIORITY_MEDIUM)); | 237 COOKIE_PRIORITY_MEDIUM)); |
| 238 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 238 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
| 239 } | 239 } |
| 240 | 240 |
| 241 TEST(CanonicalCookieTest, IsEquivalentForSecureCookieMatching) { | 241 TEST(CanonicalCookieTest, IsEquivalentForSecureCookieMatching) { |
| 242 GURL url("http://www.example.com/"); | 242 GURL url("http://www.example.com/"); |
| 243 std::string cookie_name = "A"; | 243 std::string cookie_name = "A"; |
| 244 std::string cookie_value = "2EDA-EF"; | 244 std::string cookie_value = "2EDA-EF"; |
| 245 std::string cookie_domain = ".www.example.com"; | 245 std::string cookie_domain = ".www.example.com"; |
| 246 std::string cookie_path = "/"; | 246 std::string cookie_path = "/"; |
| 247 base::Time creation_time = base::Time::Now(); | 247 base::Time creation_time = base::Time::Now(); |
| 248 base::Time last_access_time = creation_time; | 248 base::Time last_access_time = creation_time; |
| 249 base::Time expiration_time = creation_time + base::TimeDelta::FromDays(2); | 249 base::Time expiration_time = creation_time + base::TimeDelta::FromDays(2); |
| 250 bool secure(false); | 250 bool secure(false); |
| 251 bool httponly(false); | 251 bool httponly(false); |
| 252 bool firstparty(false); | 252 bool same_site(false); |
| 253 | 253 |
| 254 // Test that a cookie is equivalent to itself. | 254 // Test that a cookie is equivalent to itself. |
| 255 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie( | 255 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie( |
| 256 url, cookie_name, cookie_value, cookie_domain, cookie_path, creation_time, | 256 url, cookie_name, cookie_value, cookie_domain, cookie_path, creation_time, |
| 257 expiration_time, last_access_time, secure, httponly, firstparty, | 257 expiration_time, last_access_time, secure, httponly, same_site, |
| 258 COOKIE_PRIORITY_MEDIUM)); | 258 COOKIE_PRIORITY_MEDIUM)); |
| 259 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*cookie)); | 259 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*cookie)); |
| 260 | 260 |
| 261 // Test that two identical cookies are equivalent. | 261 // Test that two identical cookies are equivalent. |
| 262 scoped_ptr<CanonicalCookie> other_cookie(new CanonicalCookie( | 262 scoped_ptr<CanonicalCookie> other_cookie(new CanonicalCookie( |
| 263 url, cookie_name, cookie_value, cookie_domain, cookie_path, creation_time, | 263 url, cookie_name, cookie_value, cookie_domain, cookie_path, creation_time, |
| 264 expiration_time, last_access_time, secure, httponly, firstparty, | 264 expiration_time, last_access_time, secure, httponly, same_site, |
| 265 COOKIE_PRIORITY_MEDIUM)); | 265 COOKIE_PRIORITY_MEDIUM)); |
| 266 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | 266 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); |
| 267 | 267 |
| 268 // Tests that use different variations of attribute values that | 268 // Tests that use different variations of attribute values that |
| 269 // DON'T affect cookie equivalence. Differs from the IsEquivalent tests above | 269 // DON'T affect cookie equivalence. Differs from the IsEquivalent tests above |
| 270 // as follows: | 270 // as follows: |
| 271 // * Should return true even if paths differ. | 271 // * Should return true even if paths differ. |
| 272 // * Should return true if the domains "domain-match" (but are not | 272 // * Should return true if the domains "domain-match" (but are not |
| 273 // identical). | 273 // identical). |
| 274 other_cookie.reset( | 274 other_cookie.reset( |
| 275 new CanonicalCookie(url, cookie_name, "2", cookie_domain, cookie_path, | 275 new CanonicalCookie(url, cookie_name, "2", cookie_domain, cookie_path, |
| 276 creation_time, expiration_time, last_access_time, | 276 creation_time, expiration_time, last_access_time, |
| 277 secure, httponly, firstparty, COOKIE_PRIORITY_HIGH)); | 277 secure, httponly, same_site, COOKIE_PRIORITY_HIGH)); |
| 278 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | 278 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); |
| 279 | 279 |
| 280 base::Time other_creation_time = | 280 base::Time other_creation_time = |
| 281 creation_time + base::TimeDelta::FromMinutes(2); | 281 creation_time + base::TimeDelta::FromMinutes(2); |
| 282 other_cookie.reset(new CanonicalCookie( | 282 other_cookie.reset(new CanonicalCookie( |
| 283 url, cookie_name, "2", cookie_domain, cookie_path, other_creation_time, | 283 url, cookie_name, "2", cookie_domain, cookie_path, other_creation_time, |
| 284 expiration_time, last_access_time, secure, httponly, firstparty, | 284 expiration_time, last_access_time, secure, httponly, same_site, |
| 285 COOKIE_PRIORITY_MEDIUM)); | 285 COOKIE_PRIORITY_MEDIUM)); |
| 286 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | 286 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); |
| 287 | 287 |
| 288 other_cookie.reset(new CanonicalCookie( | 288 other_cookie.reset(new CanonicalCookie( |
| 289 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, | 289 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, |
| 290 expiration_time, last_access_time, true, httponly, firstparty, | 290 expiration_time, last_access_time, true, httponly, same_site, |
| 291 COOKIE_PRIORITY_LOW)); | 291 COOKIE_PRIORITY_LOW)); |
| 292 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | 292 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); |
| 293 | 293 |
| 294 other_cookie.reset(new CanonicalCookie( | 294 other_cookie.reset(new CanonicalCookie( |
| 295 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, | 295 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, |
| 296 expiration_time, last_access_time, secure, true, firstparty, | 296 expiration_time, last_access_time, secure, true, same_site, |
| 297 COOKIE_PRIORITY_LOW)); | 297 COOKIE_PRIORITY_LOW)); |
| 298 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | 298 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); |
| 299 | 299 |
| 300 other_cookie.reset(new CanonicalCookie( | 300 other_cookie.reset(new CanonicalCookie( |
| 301 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, | 301 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, |
| 302 expiration_time, last_access_time, secure, httponly, true, | 302 expiration_time, last_access_time, secure, httponly, true, |
| 303 COOKIE_PRIORITY_LOW)); | 303 COOKIE_PRIORITY_LOW)); |
| 304 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | 304 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); |
| 305 | 305 |
| 306 // The following 3 tests' expected results differ from their IsEquivalent | 306 // The following 3 tests' expected results differ from their IsEquivalent |
| 307 // counterparts above. | 307 // counterparts above. |
| 308 other_cookie.reset(new CanonicalCookie( | 308 other_cookie.reset(new CanonicalCookie( |
| 309 url, cookie_name, cookie_value, cookie_domain, "/test/0", creation_time, | 309 url, cookie_name, cookie_value, cookie_domain, "/test/0", creation_time, |
| 310 expiration_time, last_access_time, secure, httponly, firstparty, | 310 expiration_time, last_access_time, secure, httponly, same_site, |
| 311 COOKIE_PRIORITY_MEDIUM)); | 311 COOKIE_PRIORITY_MEDIUM)); |
| 312 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | 312 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); |
| 313 | 313 |
| 314 other_cookie.reset(new CanonicalCookie( | 314 other_cookie.reset(new CanonicalCookie( |
| 315 url, cookie_name, cookie_value, "www.example.com", cookie_path, | 315 url, cookie_name, cookie_value, "www.example.com", cookie_path, |
| 316 creation_time, expiration_time, last_access_time, secure, httponly, | 316 creation_time, expiration_time, last_access_time, secure, httponly, |
| 317 firstparty, COOKIE_PRIORITY_MEDIUM)); | 317 same_site, COOKIE_PRIORITY_MEDIUM)); |
| 318 EXPECT_TRUE(cookie->IsDomainCookie()); | 318 EXPECT_TRUE(cookie->IsDomainCookie()); |
| 319 EXPECT_FALSE(other_cookie->IsDomainCookie()); | 319 EXPECT_FALSE(other_cookie->IsDomainCookie()); |
| 320 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | 320 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); |
| 321 | 321 |
| 322 other_cookie.reset(new CanonicalCookie( | 322 other_cookie.reset(new CanonicalCookie( |
| 323 url, cookie_name, cookie_value, ".example.com", cookie_path, | 323 url, cookie_name, cookie_value, ".example.com", cookie_path, |
| 324 creation_time, expiration_time, last_access_time, secure, httponly, | 324 creation_time, expiration_time, last_access_time, secure, httponly, |
| 325 firstparty, COOKIE_PRIORITY_MEDIUM)); | 325 same_site, COOKIE_PRIORITY_MEDIUM)); |
| 326 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | 326 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); |
| 327 | 327 |
| 328 // Tests that use different variations of attribute values that | 328 // Tests that use different variations of attribute values that |
| 329 // DO affect cookie equivalence. Note that unlike the IsEquivalent tests | 329 // DO affect cookie equivalence. Note that unlike the IsEquivalent tests |
| 330 // above, this does *not* include tests for differing paths or domains that | 330 // above, this does *not* include tests for differing paths or domains that |
| 331 // "domain-match". | 331 // "domain-match". |
| 332 other_cookie.reset(new CanonicalCookie( | 332 other_cookie.reset( |
| 333 url, "B", cookie_value, cookie_domain, cookie_path, creation_time, | 333 new CanonicalCookie(url, "B", cookie_value, cookie_domain, cookie_path, |
| 334 expiration_time, last_access_time, secure, httponly, firstparty, | 334 creation_time, expiration_time, last_access_time, |
| 335 COOKIE_PRIORITY_MEDIUM)); | 335 secure, httponly, same_site, COOKIE_PRIORITY_MEDIUM)); |
| 336 EXPECT_FALSE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | 336 EXPECT_FALSE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); |
| 337 } | 337 } |
| 338 | 338 |
| 339 TEST(CanonicalCookieTest, IsDomainMatch) { | 339 TEST(CanonicalCookieTest, IsDomainMatch) { |
| 340 GURL url("http://www.example.com/test/foo.html"); | 340 GURL url("http://www.example.com/test/foo.html"); |
| 341 base::Time creation_time = base::Time::Now(); | 341 base::Time creation_time = base::Time::Now(); |
| 342 CookieOptions options; | 342 CookieOptions options; |
| 343 | 343 |
| 344 scoped_ptr<CanonicalCookie> cookie( | 344 scoped_ptr<CanonicalCookie> cookie( |
| 345 CanonicalCookie::Create(url, "A=2", creation_time, options)); | 345 CanonicalCookie::Create(url, "A=2", creation_time, options)); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 // is set on the cookie options. | 426 // is set on the cookie options. |
| 427 options.set_include_httponly(); | 427 options.set_include_httponly(); |
| 428 cookie = | 428 cookie = |
| 429 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, options); | 429 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, options); |
| 430 EXPECT_TRUE(cookie->IsHttpOnly()); | 430 EXPECT_TRUE(cookie->IsHttpOnly()); |
| 431 EXPECT_TRUE(cookie->IncludeForRequestURL(url, options)); | 431 EXPECT_TRUE(cookie->IncludeForRequestURL(url, options)); |
| 432 options.set_exclude_httponly(); | 432 options.set_exclude_httponly(); |
| 433 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options)); | 433 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options)); |
| 434 } | 434 } |
| 435 | 435 |
| 436 TEST(CanonicalCookieTest, IncludeFirstPartyForFirstPartyURL) { | 436 TEST(CanonicalCookieTest, IncludeSameSiteForSameSiteURL) { |
| 437 GURL insecure_url("http://example.test"); | 437 GURL insecure_url("http://example.test"); |
| 438 GURL secure_url("https://example.test"); | 438 GURL secure_url("https://example.test"); |
| 439 GURL secure_url_with_path("https://example.test/foo/bar/index.html"); | 439 GURL secure_url_with_path("https://example.test/foo/bar/index.html"); |
| 440 GURL third_party_url("https://not-example.test"); | 440 GURL third_party_url("https://not-example.test"); |
| 441 base::Time creation_time = base::Time::Now(); | 441 base::Time creation_time = base::Time::Now(); |
| 442 CookieOptions options; | 442 CookieOptions options; |
| 443 scoped_ptr<CanonicalCookie> cookie; | 443 scoped_ptr<CanonicalCookie> cookie; |
| 444 | 444 |
| 445 // First-party-only cookies are not included for non-first-party requests, | 445 // Same-site cookies are not included for cross-site requests, |
| 446 // even if other properties match: | 446 // even if other properties match: |
| 447 cookie = CanonicalCookie::Create(secure_url, "A=2; First-Party-Only", | 447 cookie = CanonicalCookie::Create(secure_url, "A=2; SameSite", creation_time, |
| 448 options); |
| 449 EXPECT_TRUE(cookie->IsSameSite()); |
| 450 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); |
| 451 cookie = CanonicalCookie::Create(secure_url, "A=2; Secure; SameSite", |
| 448 creation_time, options); | 452 creation_time, options); |
| 449 EXPECT_TRUE(cookie->IsFirstPartyOnly()); | 453 EXPECT_TRUE(cookie->IsSameSite()); |
| 450 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); | |
| 451 cookie = CanonicalCookie::Create(secure_url, "A=2; Secure; First-Party-Only", | |
| 452 creation_time, options); | |
| 453 EXPECT_TRUE(cookie->IsFirstPartyOnly()); | |
| 454 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); | 454 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); |
| 455 cookie = CanonicalCookie::Create(secure_url_with_path, | 455 cookie = CanonicalCookie::Create(secure_url_with_path, |
| 456 "A=2; First-Party-Only; path=/foo/bar", | 456 "A=2; SameSite; path=/foo/bar", |
| 457 creation_time, options); | 457 creation_time, options); |
| 458 EXPECT_TRUE(cookie->IsFirstPartyOnly()); | 458 EXPECT_TRUE(cookie->IsSameSite()); |
| 459 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); | 459 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); |
| 460 | 460 |
| 461 // First-party-only cookies are included for first-party requests: | 461 // Same-site cookies are included for same-site requests: |
| 462 options.set_include_first_party_only_cookies(); | 462 options.set_include_same_site(); |
| 463 cookie = CanonicalCookie::Create(secure_url, "A=2; First-Party-Only", | 463 cookie = CanonicalCookie::Create(secure_url, "A=2; SameSite", creation_time, |
| 464 options); |
| 465 EXPECT_TRUE(cookie->IsSameSite()); |
| 466 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options)); |
| 467 cookie = CanonicalCookie::Create(secure_url, "A=2; Secure; SameSite", |
| 464 creation_time, options); | 468 creation_time, options); |
| 465 EXPECT_TRUE(cookie->IsFirstPartyOnly()); | 469 EXPECT_TRUE(cookie->IsSameSite()); |
| 466 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options)); | |
| 467 cookie = CanonicalCookie::Create(secure_url, "A=2; Secure; First-Party-Only", | |
| 468 creation_time, options); | |
| 469 EXPECT_TRUE(cookie->IsFirstPartyOnly()); | |
| 470 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options)); | 470 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options)); |
| 471 cookie = CanonicalCookie::Create(secure_url_with_path, | 471 cookie = CanonicalCookie::Create(secure_url_with_path, |
| 472 "A=2; First-Party-Only; path=/foo/bar", | 472 "A=2; SameSite; path=/foo/bar", |
| 473 creation_time, options); | 473 creation_time, options); |
| 474 EXPECT_TRUE(cookie->IsFirstPartyOnly()); | 474 EXPECT_TRUE(cookie->IsSameSite()); |
| 475 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url_with_path, options)); | 475 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url_with_path, options)); |
| 476 } | 476 } |
| 477 | 477 |
| 478 TEST(CanonicalCookieTest, PartialCompare) { | 478 TEST(CanonicalCookieTest, PartialCompare) { |
| 479 GURL url("http://www.example.com"); | 479 GURL url("http://www.example.com"); |
| 480 base::Time creation_time = base::Time::Now(); | 480 base::Time creation_time = base::Time::Now(); |
| 481 CookieOptions options; | 481 CookieOptions options; |
| 482 scoped_ptr<CanonicalCookie> cookie( | 482 scoped_ptr<CanonicalCookie> cookie( |
| 483 CanonicalCookie::Create(url, "a=b", creation_time, options)); | 483 CanonicalCookie::Create(url, "a=b", creation_time, options)); |
| 484 scoped_ptr<CanonicalCookie> cookie_different_path( | 484 scoped_ptr<CanonicalCookie> cookie_different_path( |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 701 CanonicalCookie::COOKIE_PREFIX_SECURE, 1); | 701 CanonicalCookie::COOKIE_PREFIX_SECURE, 1); |
| 702 EXPECT_TRUE(CanonicalCookie::Create(https_url, "__SecureA=B; Path=/; Secure", | 702 EXPECT_TRUE(CanonicalCookie::Create(https_url, "__SecureA=B; Path=/; Secure", |
| 703 creation_time, options)); | 703 creation_time, options)); |
| 704 histograms.ExpectBucketCount(kCookiePrefixHistogram, | 704 histograms.ExpectBucketCount(kCookiePrefixHistogram, |
| 705 CanonicalCookie::COOKIE_PREFIX_SECURE, 2); | 705 CanonicalCookie::COOKIE_PREFIX_SECURE, 2); |
| 706 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram, | 706 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram, |
| 707 CanonicalCookie::COOKIE_PREFIX_SECURE, 1); | 707 CanonicalCookie::COOKIE_PREFIX_SECURE, 1); |
| 708 } | 708 } |
| 709 | 709 |
| 710 } // namespace net | 710 } // namespace net |
| OLD | NEW |