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 "net/cookies/cookie_constants.h" | 8 #include "net/cookies/cookie_constants.h" |
9 #include "net/cookies/cookie_options.h" | 9 #include "net/cookies/cookie_options.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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.reset(CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, | 81 cookie.reset(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 first_party_options; |
87 first_party_options.set_first_party(url::Origin(url)); | 87 first_party_options.set_include_first_party_only(); |
88 cookie.reset(CanonicalCookie::Create(url, "A=2; First-Party-Only", | 88 cookie.reset(CanonicalCookie::Create(url, "A=2; First-Party-Only", |
89 creation_time, httponly_options)); | 89 creation_time, httponly_options)); |
90 EXPECT_TRUE(cookie.get()); | 90 EXPECT_TRUE(cookie.get()); |
91 EXPECT_TRUE(cookie->IsFirstPartyOnly()); | 91 EXPECT_TRUE(cookie->IsFirstPartyOnly()); |
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.reset(CanonicalCookie::Create( | 95 cookie.reset(CanonicalCookie::Create( |
96 url, "A", "2", "www.example.com", "/test", creation_time, base::Time(), | 96 url, "A", "2", "www.example.com", "/test", creation_time, base::Time(), |
97 false, false, false, COOKIE_PRIORITY_DEFAULT)); | 97 false, false, false, COOKIE_PRIORITY_DEFAULT)); |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 | 340 |
341 TEST(CanonicalCookieTest, IncludeFirstPartyForFirstPartyURL) { | 341 TEST(CanonicalCookieTest, IncludeFirstPartyForFirstPartyURL) { |
342 GURL insecure_url("http://example.test"); | 342 GURL insecure_url("http://example.test"); |
343 GURL secure_url("https://example.test"); | 343 GURL secure_url("https://example.test"); |
344 GURL secure_url_with_path("https://example.test/foo/bar/index.html"); | 344 GURL secure_url_with_path("https://example.test/foo/bar/index.html"); |
345 GURL third_party_url("https://not-example.test"); | 345 GURL third_party_url("https://not-example.test"); |
346 base::Time creation_time = base::Time::Now(); | 346 base::Time creation_time = base::Time::Now(); |
347 CookieOptions options; | 347 CookieOptions options; |
348 scoped_ptr<CanonicalCookie> cookie; | 348 scoped_ptr<CanonicalCookie> cookie; |
349 | 349 |
350 // First-party-only cookies are not inlcuded if a top-level URL is unset. | 350 // First-party-only cookies are not included for non-first-party requests, |
| 351 // even if other properties match: |
351 cookie.reset(CanonicalCookie::Create(secure_url, "A=2; First-Party-Only", | 352 cookie.reset(CanonicalCookie::Create(secure_url, "A=2; First-Party-Only", |
352 creation_time, options)); | 353 creation_time, options)); |
353 EXPECT_TRUE(cookie->IsFirstPartyOnly()); | 354 EXPECT_TRUE(cookie->IsFirstPartyOnly()); |
354 options.set_first_party(url::Origin()); | |
355 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); | 355 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); |
356 | |
357 // First-party-only cookies are included only if the cookie's origin matches | |
358 // the | |
359 // first-party origin. | |
360 options.set_first_party(url::Origin(secure_url)); | |
361 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options)); | |
362 options.set_first_party(url::Origin(insecure_url)); | |
363 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); | |
364 options.set_first_party(url::Origin(third_party_url)); | |
365 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); | |
366 | |
367 // "First-Party-Only" doesn't override the 'secure' flag. | |
368 cookie.reset(CanonicalCookie::Create( | 356 cookie.reset(CanonicalCookie::Create( |
369 secure_url, "A=2; Secure; First-Party-Only", creation_time, options)); | 357 secure_url, "A=2; Secure; First-Party-Only", creation_time, options)); |
370 options.set_first_party(url::Origin(secure_url)); | 358 EXPECT_TRUE(cookie->IsFirstPartyOnly()); |
371 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options)); | |
372 EXPECT_FALSE(cookie->IncludeForRequestURL(insecure_url, options)); | |
373 options.set_first_party(url::Origin(insecure_url)); | |
374 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); | 359 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); |
375 EXPECT_FALSE(cookie->IncludeForRequestURL(insecure_url, options)); | |
376 | |
377 // "First-Party-Only" doesn't override the 'path' flag. | |
378 cookie.reset(CanonicalCookie::Create(secure_url_with_path, | 360 cookie.reset(CanonicalCookie::Create(secure_url_with_path, |
379 "A=2; First-Party-Only; path=/foo/bar", | 361 "A=2; First-Party-Only; path=/foo/bar", |
380 creation_time, options)); | 362 creation_time, options)); |
381 options.set_first_party(url::Origin(secure_url_with_path)); | 363 EXPECT_TRUE(cookie->IsFirstPartyOnly()); |
| 364 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); |
| 365 |
| 366 // First-party-only cookies are included for first-party requests: |
| 367 options.set_include_first_party_only(); |
| 368 cookie.reset(CanonicalCookie::Create(secure_url, "A=2; First-Party-Only", |
| 369 creation_time, options)); |
| 370 EXPECT_TRUE(cookie->IsFirstPartyOnly()); |
| 371 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options)); |
| 372 cookie.reset(CanonicalCookie::Create( |
| 373 secure_url, "A=2; Secure; First-Party-Only", creation_time, options)); |
| 374 EXPECT_TRUE(cookie->IsFirstPartyOnly()); |
| 375 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options)); |
| 376 cookie.reset(CanonicalCookie::Create(secure_url_with_path, |
| 377 "A=2; First-Party-Only; path=/foo/bar", |
| 378 creation_time, options)); |
| 379 EXPECT_TRUE(cookie->IsFirstPartyOnly()); |
382 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url_with_path, options)); | 380 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url_with_path, options)); |
383 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); | |
384 options.set_first_party(url::Origin(secure_url)); | |
385 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url_with_path, options)); | |
386 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); | |
387 } | 381 } |
388 | 382 |
389 TEST(CanonicalCookieTest, PartialCompare) { | 383 TEST(CanonicalCookieTest, PartialCompare) { |
390 GURL url("http://www.example.com"); | 384 GURL url("http://www.example.com"); |
391 base::Time creation_time = base::Time::Now(); | 385 base::Time creation_time = base::Time::Now(); |
392 CookieOptions options; | 386 CookieOptions options; |
393 scoped_ptr<CanonicalCookie> cookie( | 387 scoped_ptr<CanonicalCookie> cookie( |
394 CanonicalCookie::Create(url, "a=b", creation_time, options)); | 388 CanonicalCookie::Create(url, "a=b", creation_time, options)); |
395 scoped_ptr<CanonicalCookie> cookie_different_path( | 389 scoped_ptr<CanonicalCookie> cookie_different_path( |
396 CanonicalCookie::Create(url, "a=b; path=/foo", creation_time, options)); | 390 CanonicalCookie::Create(url, "a=b; path=/foo", creation_time, options)); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 else if (b.FullCompare(a)) | 437 else if (b.FullCompare(a)) |
444 EXPECT_FALSE(a.PartialCompare(b)); | 438 EXPECT_FALSE(a.PartialCompare(b)); |
445 }; | 439 }; |
446 | 440 |
447 check_consistency(*cookie, *cookie_different_path); | 441 check_consistency(*cookie, *cookie_different_path); |
448 check_consistency(*cookie, *cookie_different_value); | 442 check_consistency(*cookie, *cookie_different_value); |
449 check_consistency(*cookie_different_path, *cookie_different_value); | 443 check_consistency(*cookie_different_path, *cookie_different_value); |
450 } | 444 } |
451 | 445 |
452 } // namespace net | 446 } // namespace net |
OLD | NEW |