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 <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/test/histogram_tester.h" | 9 #include "base/test/histogram_tester.h" |
10 #include "net/cookies/cookie_constants.h" | 10 #include "net/cookies/cookie_constants.h" |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
164 EXPECT_FALSE(cookie->IsPersistent()); | 164 EXPECT_FALSE(cookie->IsPersistent()); |
165 EXPECT_FALSE(cookie->IsExpired(creation_time)); | 165 EXPECT_FALSE(cookie->IsExpired(creation_time)); |
166 EXPECT_EQ(base::Time(), cookie->ExpiryDate()); | 166 EXPECT_EQ(base::Time(), cookie->ExpiryDate()); |
167 } | 167 } |
168 | 168 |
169 TEST(CanonicalCookieTest, IsEquivalent) { | 169 TEST(CanonicalCookieTest, IsEquivalent) { |
170 GURL url("http://www.example.com/"); | 170 GURL url("http://www.example.com/"); |
171 std::string cookie_name = "A"; | 171 std::string cookie_name = "A"; |
172 std::string cookie_value = "2EDA-EF"; | 172 std::string cookie_value = "2EDA-EF"; |
173 std::string cookie_domain = ".www.example.com"; | 173 std::string cookie_domain = ".www.example.com"; |
174 std::string cookie_path = "/"; | 174 std::string cookie_path = "/path"; |
175 base::Time creation_time = base::Time::Now(); | 175 base::Time creation_time = base::Time::Now(); |
176 base::Time expiration_time = creation_time + base::TimeDelta::FromDays(2); | 176 base::Time expiration_time = creation_time + base::TimeDelta::FromDays(2); |
177 bool secure(false); | 177 bool secure(false); |
178 bool httponly(false); | 178 bool httponly(false); |
179 CookieSameSite same_site(CookieSameSite::NO_RESTRICTION); | 179 CookieSameSite same_site(CookieSameSite::NO_RESTRICTION); |
180 | 180 |
181 // Test that a cookie is equivalent to itself. | 181 // Test that a cookie is equivalent to itself. |
182 std::unique_ptr<CanonicalCookie> cookie(CanonicalCookie::Create( | 182 std::unique_ptr<CanonicalCookie> cookie(CanonicalCookie::Create( |
183 url, cookie_name, cookie_value, cookie_domain, cookie_path, creation_time, | 183 url, cookie_name, cookie_value, cookie_domain, cookie_path, creation_time, |
184 expiration_time, secure, httponly, same_site, false, | 184 expiration_time, secure, httponly, same_site, false, |
185 COOKIE_PRIORITY_MEDIUM)); | 185 COOKIE_PRIORITY_MEDIUM)); |
186 EXPECT_TRUE(cookie->IsEquivalent(*cookie)); | 186 EXPECT_TRUE(cookie->IsEquivalent(*cookie)); |
187 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*cookie)); | |
187 | 188 |
188 // Test that two identical cookies are equivalent. | 189 // Test that two identical cookies are equivalent. |
189 std::unique_ptr<CanonicalCookie> other_cookie(CanonicalCookie::Create( | 190 std::unique_ptr<CanonicalCookie> other_cookie(CanonicalCookie::Create( |
190 url, cookie_name, cookie_value, cookie_domain, cookie_path, creation_time, | 191 url, cookie_name, cookie_value, cookie_domain, cookie_path, creation_time, |
191 expiration_time, secure, httponly, same_site, false, | 192 expiration_time, secure, httponly, same_site, false, |
192 COOKIE_PRIORITY_MEDIUM)); | 193 COOKIE_PRIORITY_MEDIUM)); |
193 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 194 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
195 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
194 | 196 |
195 // Tests that use different variations of attribute values that | 197 // Tests that use different variations of attribute values that |
196 // DON'T affect cookie equivalence. | 198 // DON'T affect cookie equivalence. |
197 other_cookie = | 199 other_cookie = |
198 CanonicalCookie::Create(url, cookie_name, "2", cookie_domain, cookie_path, | 200 CanonicalCookie::Create(url, cookie_name, "2", cookie_domain, cookie_path, |
199 creation_time, expiration_time, secure, httponly, | 201 creation_time, expiration_time, secure, httponly, |
200 same_site, false, COOKIE_PRIORITY_HIGH); | 202 same_site, false, COOKIE_PRIORITY_HIGH); |
201 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 203 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
204 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
202 | 205 |
203 base::Time other_creation_time = | 206 base::Time other_creation_time = |
204 creation_time + base::TimeDelta::FromMinutes(2); | 207 creation_time + base::TimeDelta::FromMinutes(2); |
205 other_cookie = CanonicalCookie::Create( | 208 other_cookie = CanonicalCookie::Create( |
206 url, cookie_name, "2", cookie_domain, cookie_path, other_creation_time, | 209 url, cookie_name, "2", cookie_domain, cookie_path, other_creation_time, |
207 expiration_time, secure, httponly, same_site, false, | 210 expiration_time, secure, httponly, same_site, false, |
208 COOKIE_PRIORITY_MEDIUM); | 211 COOKIE_PRIORITY_MEDIUM); |
209 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 212 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
213 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
210 | 214 |
211 other_cookie = CanonicalCookie::Create( | 215 other_cookie = CanonicalCookie::Create( |
212 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, | 216 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, |
213 expiration_time, true, httponly, same_site, false, COOKIE_PRIORITY_LOW); | 217 expiration_time, true, httponly, same_site, false, COOKIE_PRIORITY_LOW); |
214 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 218 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
219 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
215 | 220 |
216 other_cookie = CanonicalCookie::Create( | 221 other_cookie = CanonicalCookie::Create( |
217 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, | 222 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, |
218 expiration_time, secure, true, same_site, false, COOKIE_PRIORITY_LOW); | 223 expiration_time, secure, true, same_site, false, COOKIE_PRIORITY_LOW); |
219 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 224 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
225 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
220 | 226 |
221 other_cookie = CanonicalCookie::Create( | 227 other_cookie = CanonicalCookie::Create( |
222 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, | 228 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, |
223 expiration_time, secure, httponly, CookieSameSite::STRICT_MODE, false, | 229 expiration_time, secure, httponly, CookieSameSite::STRICT_MODE, false, |
224 COOKIE_PRIORITY_LOW); | 230 COOKIE_PRIORITY_LOW); |
225 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 231 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
232 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
226 | 233 |
227 // Tests that use different variations of attribute values that | 234 // Cookies whose names mismatch are not equivalent. |
228 // DO affect cookie equivalence. | |
229 other_cookie = CanonicalCookie::Create( | 235 other_cookie = CanonicalCookie::Create( |
230 url, "B", cookie_value, cookie_domain, cookie_path, creation_time, | 236 url, "B", cookie_value, cookie_domain, cookie_path, creation_time, |
231 expiration_time, secure, httponly, same_site, false, | 237 expiration_time, secure, httponly, same_site, false, |
232 COOKIE_PRIORITY_MEDIUM); | 238 COOKIE_PRIORITY_MEDIUM); |
233 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 239 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
240 EXPECT_FALSE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
234 | 241 |
242 // A domain cookie at 'www.example.com' is not equivalent to a host cookie | |
243 // at the same domain. These are, however, equivalent according to the laxer | |
244 // rules of 'IsEquivalentForSecureCookieMatching'. | |
235 other_cookie = CanonicalCookie::Create( | 245 other_cookie = CanonicalCookie::Create( |
236 url, cookie_name, cookie_value, std::string(), cookie_path, creation_time, | 246 url, cookie_name, cookie_value, std::string(), cookie_path, creation_time, |
237 expiration_time, secure, httponly, same_site, false, | 247 expiration_time, secure, httponly, same_site, false, |
238 COOKIE_PRIORITY_MEDIUM); | 248 COOKIE_PRIORITY_MEDIUM); |
239 EXPECT_TRUE(cookie->IsDomainCookie()); | 249 EXPECT_TRUE(cookie->IsDomainCookie()); |
240 EXPECT_FALSE(other_cookie->IsDomainCookie()); | 250 EXPECT_FALSE(other_cookie->IsDomainCookie()); |
241 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 251 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
252 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
jww
2016/09/06 22:45:10
nit: In a few of these cases, it's probably worth
| |
242 | 253 |
254 // Likewise, a cookie on 'example.com' is not equivalent to a cookie on | |
255 // 'www.example.com', but they are equivalent for secure cookie matching. | |
243 other_cookie = CanonicalCookie::Create( | 256 other_cookie = CanonicalCookie::Create( |
244 url, cookie_name, cookie_value, ".example.com", cookie_path, | 257 url, cookie_name, cookie_value, ".example.com", cookie_path, |
245 creation_time, expiration_time, secure, httponly, same_site, false, | 258 creation_time, expiration_time, secure, httponly, same_site, false, |
246 COOKIE_PRIORITY_MEDIUM); | 259 COOKIE_PRIORITY_MEDIUM); |
247 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 260 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
261 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
248 | 262 |
263 // Paths are a bit more complicated. 'IsEquivalent' requires an exact path | |
264 // match, while secure cookie matching uses a more relaxed 'IsOnPath' check. | |
265 // That is, |cookie| set on '/path' is not equivalent in either way to | |
266 // |other_cookie| set on '/test' or '/path/subpath'. It is, however, | |
267 // equivalent for secure cookie matching to |other_cookie| set on '/'. | |
249 other_cookie = CanonicalCookie::Create( | 268 other_cookie = CanonicalCookie::Create( |
250 url, cookie_name, cookie_value, cookie_domain, "/test/0", creation_time, | 269 url, cookie_name, cookie_value, cookie_domain, "/test", creation_time, |
251 expiration_time, secure, httponly, same_site, false, | 270 expiration_time, secure, httponly, same_site, false, |
252 COOKIE_PRIORITY_MEDIUM); | 271 COOKIE_PRIORITY_MEDIUM); |
253 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 272 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
254 } | 273 EXPECT_FALSE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); |
255 | 274 |
256 TEST(CanonicalCookieTest, IsEquivalentForSecureCookieMatching) { | 275 other_cookie = CanonicalCookie::Create( |
257 GURL url("http://www.example.com/"); | 276 url, cookie_name, cookie_value, cookie_domain, cookie_path + "/subpath", |
258 std::string cookie_name = "A"; | 277 creation_time, expiration_time, secure, httponly, same_site, false, |
259 std::string cookie_value = "2EDA-EF"; | 278 COOKIE_PRIORITY_MEDIUM); |
260 std::string cookie_domain = ".www.example.com"; | 279 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
261 std::string cookie_path = "/"; | 280 EXPECT_FALSE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); |
262 base::Time creation_time = base::Time::Now(); | |
263 base::Time expiration_time = creation_time + base::TimeDelta::FromDays(2); | |
264 bool secure(false); | |
265 bool httponly(false); | |
266 CookieSameSite same_site(CookieSameSite::NO_RESTRICTION); | |
267 | 281 |
268 // Test that a cookie is equivalent to itself. | |
269 std::unique_ptr<CanonicalCookie> cookie(CanonicalCookie::Create( | |
270 url, cookie_name, cookie_value, cookie_domain, cookie_path, creation_time, | |
271 expiration_time, secure, httponly, same_site, false, | |
272 COOKIE_PRIORITY_MEDIUM)); | |
273 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*cookie)); | |
274 | |
275 // Test that two identical cookies are equivalent. | |
276 std::unique_ptr<CanonicalCookie> other_cookie(CanonicalCookie::Create( | |
277 url, cookie_name, cookie_value, cookie_domain, cookie_path, creation_time, | |
278 expiration_time, secure, httponly, same_site, false, | |
279 COOKIE_PRIORITY_MEDIUM)); | |
280 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
281 | |
282 // Tests that use different variations of attribute values that | |
283 // DON'T affect cookie equivalence. Differs from the IsEquivalent tests above | |
284 // as follows: | |
285 // * Should return true even if paths differ. | |
286 // * Should return true if the domains "domain-match" (but are not | |
287 // identical). | |
288 other_cookie = | |
289 CanonicalCookie::Create(url, cookie_name, "2", cookie_domain, cookie_path, | |
290 creation_time, expiration_time, secure, httponly, | |
291 same_site, false, COOKIE_PRIORITY_HIGH); | |
292 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
293 | |
294 base::Time other_creation_time = | |
295 creation_time + base::TimeDelta::FromMinutes(2); | |
296 other_cookie = CanonicalCookie::Create( | 282 other_cookie = CanonicalCookie::Create( |
297 url, cookie_name, "2", cookie_domain, cookie_path, other_creation_time, | 283 url, cookie_name, cookie_value, cookie_domain, "/", creation_time, |
298 expiration_time, secure, httponly, same_site, false, | 284 expiration_time, secure, httponly, same_site, false, |
299 COOKIE_PRIORITY_MEDIUM); | 285 COOKIE_PRIORITY_MEDIUM); |
286 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | |
300 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | 287 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); |
301 | |
302 other_cookie = CanonicalCookie::Create( | |
303 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, | |
304 expiration_time, true, httponly, same_site, false, COOKIE_PRIORITY_LOW); | |
305 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
306 | |
307 other_cookie = CanonicalCookie::Create( | |
308 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, | |
309 expiration_time, secure, true, same_site, false, COOKIE_PRIORITY_LOW); | |
310 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
311 | |
312 other_cookie = CanonicalCookie::Create( | |
313 url, cookie_name, cookie_name, cookie_domain, cookie_path, creation_time, | |
314 expiration_time, secure, httponly, CookieSameSite::STRICT_MODE, false, | |
315 COOKIE_PRIORITY_LOW); | |
316 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
317 | |
318 // The following 3 tests' expected results differ from their IsEquivalent | |
319 // counterparts above. | |
320 other_cookie = CanonicalCookie::Create( | |
321 url, cookie_name, cookie_value, cookie_domain, "/test/0", creation_time, | |
322 expiration_time, secure, httponly, same_site, false, | |
323 COOKIE_PRIORITY_MEDIUM); | |
324 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
325 | |
326 other_cookie = CanonicalCookie::Create( | |
327 url, cookie_name, cookie_value, std::string(), cookie_path, creation_time, | |
328 expiration_time, secure, httponly, same_site, false, | |
329 COOKIE_PRIORITY_MEDIUM); | |
330 EXPECT_TRUE(cookie->IsDomainCookie()); | |
331 EXPECT_FALSE(other_cookie->IsDomainCookie()); | |
332 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
333 | |
334 other_cookie = CanonicalCookie::Create( | |
335 url, cookie_name, cookie_value, ".example.com", cookie_path, | |
336 creation_time, expiration_time, secure, httponly, same_site, false, | |
337 COOKIE_PRIORITY_MEDIUM); | |
338 EXPECT_TRUE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
339 | |
340 // Tests that use different variations of attribute values that | |
341 // DO affect cookie equivalence. Note that unlike the IsEquivalent tests | |
342 // above, this does *not* include tests for differing paths or domains that | |
343 // "domain-match". | |
344 other_cookie = CanonicalCookie::Create( | |
345 url, "B", cookie_value, cookie_domain, cookie_path, creation_time, | |
346 expiration_time, secure, httponly, same_site, false, | |
347 COOKIE_PRIORITY_MEDIUM); | |
348 EXPECT_FALSE(cookie->IsEquivalentForSecureCookieMatching(*other_cookie)); | |
349 } | 288 } |
350 | 289 |
351 TEST(CanonicalCookieTest, IsDomainMatch) { | 290 TEST(CanonicalCookieTest, IsDomainMatch) { |
352 GURL url("http://www.example.com/test/foo.html"); | 291 GURL url("http://www.example.com/test/foo.html"); |
353 base::Time creation_time = base::Time::Now(); | 292 base::Time creation_time = base::Time::Now(); |
354 CookieOptions options; | 293 CookieOptions options; |
355 | 294 |
356 std::unique_ptr<CanonicalCookie> cookie( | 295 std::unique_ptr<CanonicalCookie> cookie( |
357 CanonicalCookie::Create(url, "A=2", creation_time, options)); | 296 CanonicalCookie::Create(url, "A=2", creation_time, options)); |
358 EXPECT_TRUE(cookie->IsHostCookie()); | 297 EXPECT_TRUE(cookie->IsHostCookie()); |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
708 CanonicalCookie::COOKIE_PREFIX_SECURE, 1); | 647 CanonicalCookie::COOKIE_PREFIX_SECURE, 1); |
709 EXPECT_TRUE(CanonicalCookie::Create(https_url, "__SecureA=B; Path=/; Secure", | 648 EXPECT_TRUE(CanonicalCookie::Create(https_url, "__SecureA=B; Path=/; Secure", |
710 creation_time, options)); | 649 creation_time, options)); |
711 histograms.ExpectBucketCount(kCookiePrefixHistogram, | 650 histograms.ExpectBucketCount(kCookiePrefixHistogram, |
712 CanonicalCookie::COOKIE_PREFIX_SECURE, 2); | 651 CanonicalCookie::COOKIE_PREFIX_SECURE, 2); |
713 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram, | 652 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram, |
714 CanonicalCookie::COOKIE_PREFIX_SECURE, 1); | 653 CanonicalCookie::COOKIE_PREFIX_SECURE, 1); |
715 } | 654 } |
716 | 655 |
717 } // namespace net | 656 } // namespace net |
OLD | NEW |