Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: net/cookies/canonical_cookie_unittest.cc

Issue 1602283002: Convert some raw CanonicalCookie ptrs to scoped_ptr<CanonicalCookie>. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix missed CanonicalCookie::Create() callsite in ios/net/cookies/cookie_store_ios_unittest.mm Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/cookies/canonical_cookie.cc ('k') | net/cookies/cookie_monster.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
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));
53 EXPECT_EQ(url.GetOrigin(), cookie->Source()); 53 EXPECT_EQ(url.GetOrigin(), cookie->Source());
54 EXPECT_EQ("A", cookie->Name()); 54 EXPECT_EQ("A", cookie->Name());
55 EXPECT_EQ("2", cookie->Value()); 55 EXPECT_EQ("2", cookie->Value());
56 EXPECT_EQ("www.example.com", cookie->Domain()); 56 EXPECT_EQ("www.example.com", cookie->Domain());
57 EXPECT_EQ("/test", cookie->Path()); 57 EXPECT_EQ("/test", cookie->Path());
58 EXPECT_FALSE(cookie->IsSecure()); 58 EXPECT_FALSE(cookie->IsSecure());
59 59
60 GURL url2("http://www.foo.com"); 60 GURL url2("http://www.foo.com");
61 cookie.reset(CanonicalCookie::Create(url2, "B=1", creation_time, options)); 61 cookie = CanonicalCookie::Create(url2, "B=1", creation_time, options);
62 EXPECT_EQ(url2.GetOrigin(), cookie->Source()); 62 EXPECT_EQ(url2.GetOrigin(), cookie->Source());
63 EXPECT_EQ("B", cookie->Name()); 63 EXPECT_EQ("B", cookie->Name());
64 EXPECT_EQ("1", cookie->Value()); 64 EXPECT_EQ("1", cookie->Value());
65 EXPECT_EQ("www.foo.com", cookie->Domain()); 65 EXPECT_EQ("www.foo.com", cookie->Domain());
66 EXPECT_EQ("/", cookie->Path()); 66 EXPECT_EQ("/", cookie->Path());
67 EXPECT_FALSE(cookie->IsSecure()); 67 EXPECT_FALSE(cookie->IsSecure());
68 68
69 // Test creating secure cookies. RFC 6265 allows insecure urls to set secure 69 // Test creating secure cookies. RFC 6265 allows insecure urls to set secure
70 // cookies. 70 // cookies.
71 cookie.reset( 71 cookie = CanonicalCookie::Create(url, "A=2; Secure", creation_time, options);
72 CanonicalCookie::Create(url, "A=2; Secure", creation_time, options));
73 EXPECT_TRUE(cookie.get()); 72 EXPECT_TRUE(cookie.get());
74 EXPECT_TRUE(cookie->IsSecure()); 73 EXPECT_TRUE(cookie->IsSecure());
75 74
76 // Test creating http only cookies. 75 // Test creating http only cookies.
77 cookie.reset( 76 cookie =
78 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, options)); 77 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, options);
79 EXPECT_FALSE(cookie.get()); 78 EXPECT_FALSE(cookie.get());
80 CookieOptions httponly_options; 79 CookieOptions httponly_options;
81 httponly_options.set_include_httponly(); 80 httponly_options.set_include_httponly();
82 cookie.reset(CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, 81 cookie = CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time,
83 httponly_options)); 82 httponly_options);
84 EXPECT_TRUE(cookie->IsHttpOnly()); 83 EXPECT_TRUE(cookie->IsHttpOnly());
85 84
86 // Test creating http only cookies. 85 // Test creating http only cookies.
87 CookieOptions first_party_options; 86 CookieOptions first_party_options;
88 first_party_options.set_include_first_party_only_cookies(); 87 first_party_options.set_include_first_party_only_cookies();
89 cookie.reset(CanonicalCookie::Create(url, "A=2; First-Party-Only", 88 cookie = CanonicalCookie::Create(url, "A=2; First-Party-Only", creation_time,
90 creation_time, httponly_options)); 89 httponly_options);
91 EXPECT_TRUE(cookie.get()); 90 EXPECT_TRUE(cookie.get());
92 EXPECT_TRUE(cookie->IsFirstPartyOnly()); 91 EXPECT_TRUE(cookie->IsFirstPartyOnly());
93 92
94 // Test the creating cookies using specific parameter instead of a cookie 93 // Test the creating cookies using specific parameter instead of a cookie
95 // string. 94 // string.
96 cookie.reset(CanonicalCookie::Create( 95 cookie = CanonicalCookie::Create(url, "A", "2", "www.example.com", "/test",
97 url, "A", "2", "www.example.com", "/test", creation_time, base::Time(), 96 creation_time, base::Time(), false, false,
98 false, false, false, false, COOKIE_PRIORITY_DEFAULT)); 97 false, false, COOKIE_PRIORITY_DEFAULT);
99 EXPECT_EQ(url.GetOrigin(), cookie->Source()); 98 EXPECT_EQ(url.GetOrigin(), cookie->Source());
100 EXPECT_EQ("A", cookie->Name()); 99 EXPECT_EQ("A", cookie->Name());
101 EXPECT_EQ("2", cookie->Value()); 100 EXPECT_EQ("2", cookie->Value());
102 EXPECT_EQ(".www.example.com", cookie->Domain()); 101 EXPECT_EQ(".www.example.com", cookie->Domain());
103 EXPECT_EQ("/test", cookie->Path()); 102 EXPECT_EQ("/test", cookie->Path());
104 EXPECT_FALSE(cookie->IsSecure()); 103 EXPECT_FALSE(cookie->IsSecure());
105 EXPECT_FALSE(cookie->IsHttpOnly()); 104 EXPECT_FALSE(cookie->IsHttpOnly());
106 EXPECT_FALSE(cookie->IsFirstPartyOnly()); 105 EXPECT_FALSE(cookie->IsFirstPartyOnly());
107 106
108 cookie.reset(CanonicalCookie::Create( 107 cookie = CanonicalCookie::Create(url, "A", "2", ".www.example.com", "/test",
109 url, "A", "2", ".www.example.com", "/test", creation_time, base::Time(), 108 creation_time, base::Time(), false, false,
110 false, false, false, false, COOKIE_PRIORITY_DEFAULT)); 109 false, false, COOKIE_PRIORITY_DEFAULT);
111 EXPECT_EQ(url.GetOrigin(), cookie->Source()); 110 EXPECT_EQ(url.GetOrigin(), cookie->Source());
112 EXPECT_EQ("A", cookie->Name()); 111 EXPECT_EQ("A", cookie->Name());
113 EXPECT_EQ("2", cookie->Value()); 112 EXPECT_EQ("2", cookie->Value());
114 EXPECT_EQ(".www.example.com", cookie->Domain()); 113 EXPECT_EQ(".www.example.com", cookie->Domain());
115 EXPECT_EQ("/test", cookie->Path()); 114 EXPECT_EQ("/test", cookie->Path());
116 EXPECT_FALSE(cookie->IsSecure()); 115 EXPECT_FALSE(cookie->IsSecure());
117 EXPECT_FALSE(cookie->IsHttpOnly()); 116 EXPECT_FALSE(cookie->IsHttpOnly());
118 EXPECT_FALSE(cookie->IsFirstPartyOnly()); 117 EXPECT_FALSE(cookie->IsFirstPartyOnly());
119 } 118 }
120 119
121 TEST(CanonicalCookieTest, EmptyExpiry) { 120 TEST(CanonicalCookieTest, EmptyExpiry) {
122 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");
123 base::Time creation_time = base::Time::Now(); 122 base::Time creation_time = base::Time::Now();
124 CookieOptions options; 123 CookieOptions options;
125 124
126 std::string cookie_line = 125 std::string cookie_line =
127 "ACSTM=20130308043820420042; path=/; domain=ipdl.inpit.go.jp; Expires="; 126 "ACSTM=20130308043820420042; path=/; domain=ipdl.inpit.go.jp; Expires=";
128 scoped_ptr<CanonicalCookie> cookie( 127 scoped_ptr<CanonicalCookie> cookie(
129 CanonicalCookie::Create(url, cookie_line, creation_time, options)); 128 CanonicalCookie::Create(url, cookie_line, creation_time, options));
130 EXPECT_TRUE(cookie.get()); 129 EXPECT_TRUE(cookie.get());
131 EXPECT_FALSE(cookie->IsPersistent()); 130 EXPECT_FALSE(cookie->IsPersistent());
132 EXPECT_FALSE(cookie->IsExpired(creation_time)); 131 EXPECT_FALSE(cookie->IsExpired(creation_time));
133 EXPECT_EQ(base::Time(), cookie->ExpiryDate()); 132 EXPECT_EQ(base::Time(), cookie->ExpiryDate());
134 133
135 // With a stale server time 134 // With a stale server time
136 options.set_server_time(creation_time - base::TimeDelta::FromHours(1)); 135 options.set_server_time(creation_time - base::TimeDelta::FromHours(1));
137 cookie.reset( 136 cookie = CanonicalCookie::Create(url, cookie_line, creation_time, options);
138 CanonicalCookie::Create(url, cookie_line, creation_time, options));
139 EXPECT_TRUE(cookie.get()); 137 EXPECT_TRUE(cookie.get());
140 EXPECT_FALSE(cookie->IsPersistent()); 138 EXPECT_FALSE(cookie->IsPersistent());
141 EXPECT_FALSE(cookie->IsExpired(creation_time)); 139 EXPECT_FALSE(cookie->IsExpired(creation_time));
142 EXPECT_EQ(base::Time(), cookie->ExpiryDate()); 140 EXPECT_EQ(base::Time(), cookie->ExpiryDate());
143 141
144 // With a future server time 142 // With a future server time
145 options.set_server_time(creation_time + base::TimeDelta::FromHours(1)); 143 options.set_server_time(creation_time + base::TimeDelta::FromHours(1));
146 cookie.reset( 144 cookie = CanonicalCookie::Create(url, cookie_line, creation_time, options);
147 CanonicalCookie::Create(url, cookie_line, creation_time, options));
148 EXPECT_TRUE(cookie.get()); 145 EXPECT_TRUE(cookie.get());
149 EXPECT_FALSE(cookie->IsPersistent()); 146 EXPECT_FALSE(cookie->IsPersistent());
150 EXPECT_FALSE(cookie->IsExpired(creation_time)); 147 EXPECT_FALSE(cookie->IsExpired(creation_time));
151 EXPECT_EQ(base::Time(), cookie->ExpiryDate()); 148 EXPECT_EQ(base::Time(), cookie->ExpiryDate());
152 } 149 }
153 150
154 TEST(CanonicalCookieTest, IsEquivalent) { 151 TEST(CanonicalCookieTest, IsEquivalent) {
155 GURL url("http://www.example.com/"); 152 GURL url("http://www.example.com/");
156 std::string cookie_name = "A"; 153 std::string cookie_name = "A";
157 std::string cookie_value = "2EDA-EF"; 154 std::string cookie_value = "2EDA-EF";
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 343
347 scoped_ptr<CanonicalCookie> cookie( 344 scoped_ptr<CanonicalCookie> cookie(
348 CanonicalCookie::Create(url, "A=2", creation_time, options)); 345 CanonicalCookie::Create(url, "A=2", creation_time, options));
349 EXPECT_TRUE(cookie->IsHostCookie()); 346 EXPECT_TRUE(cookie->IsHostCookie());
350 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); 347 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com"));
351 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); 348 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com"));
352 EXPECT_FALSE(cookie->IsDomainMatch("foo.www.example.com")); 349 EXPECT_FALSE(cookie->IsDomainMatch("foo.www.example.com"));
353 EXPECT_FALSE(cookie->IsDomainMatch("www0.example.com")); 350 EXPECT_FALSE(cookie->IsDomainMatch("www0.example.com"));
354 EXPECT_FALSE(cookie->IsDomainMatch("example.com")); 351 EXPECT_FALSE(cookie->IsDomainMatch("example.com"));
355 352
356 cookie.reset(CanonicalCookie::Create(url, "A=2; Domain=www.example.com", 353 cookie = CanonicalCookie::Create(url, "A=2; Domain=www.example.com",
357 creation_time, options)); 354 creation_time, options);
358 EXPECT_TRUE(cookie->IsDomainCookie()); 355 EXPECT_TRUE(cookie->IsDomainCookie());
359 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); 356 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com"));
360 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); 357 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com"));
361 EXPECT_TRUE(cookie->IsDomainMatch("foo.www.example.com")); 358 EXPECT_TRUE(cookie->IsDomainMatch("foo.www.example.com"));
362 EXPECT_FALSE(cookie->IsDomainMatch("www0.example.com")); 359 EXPECT_FALSE(cookie->IsDomainMatch("www0.example.com"));
363 EXPECT_FALSE(cookie->IsDomainMatch("example.com")); 360 EXPECT_FALSE(cookie->IsDomainMatch("example.com"));
364 361
365 cookie.reset(CanonicalCookie::Create(url, "A=2; Domain=.www.example.com", 362 cookie = CanonicalCookie::Create(url, "A=2; Domain=.www.example.com",
366 creation_time, options)); 363 creation_time, options);
367 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); 364 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com"));
368 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); 365 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com"));
369 EXPECT_TRUE(cookie->IsDomainMatch("foo.www.example.com")); 366 EXPECT_TRUE(cookie->IsDomainMatch("foo.www.example.com"));
370 EXPECT_FALSE(cookie->IsDomainMatch("www0.example.com")); 367 EXPECT_FALSE(cookie->IsDomainMatch("www0.example.com"));
371 EXPECT_FALSE(cookie->IsDomainMatch("example.com")); 368 EXPECT_FALSE(cookie->IsDomainMatch("example.com"));
372 } 369 }
373 370
374 TEST(CanonicalCookieTest, IsOnPath) { 371 TEST(CanonicalCookieTest, IsOnPath) {
375 base::Time creation_time = base::Time::Now(); 372 base::Time creation_time = base::Time::Now();
376 CookieOptions options; 373 CookieOptions options;
377 374
378 scoped_ptr<CanonicalCookie> cookie(CanonicalCookie::Create( 375 scoped_ptr<CanonicalCookie> cookie(CanonicalCookie::Create(
379 GURL("http://www.example.com"), "A=2", creation_time, options)); 376 GURL("http://www.example.com"), "A=2", creation_time, options));
380 EXPECT_TRUE(cookie->IsOnPath("/")); 377 EXPECT_TRUE(cookie->IsOnPath("/"));
381 EXPECT_TRUE(cookie->IsOnPath("/test")); 378 EXPECT_TRUE(cookie->IsOnPath("/test"));
382 EXPECT_TRUE(cookie->IsOnPath("/test/bar.html")); 379 EXPECT_TRUE(cookie->IsOnPath("/test/bar.html"));
383 380
384 // Test the empty string edge case. 381 // Test the empty string edge case.
385 EXPECT_FALSE(cookie->IsOnPath(std::string())); 382 EXPECT_FALSE(cookie->IsOnPath(std::string()));
386 383
387 cookie.reset( 384 cookie = CanonicalCookie::Create(GURL("http://www.example.com/test/foo.html"),
388 CanonicalCookie::Create(GURL("http://www.example.com/test/foo.html"), 385 "A=2", creation_time, options);
389 "A=2", creation_time, options));
390 EXPECT_FALSE(cookie->IsOnPath("/")); 386 EXPECT_FALSE(cookie->IsOnPath("/"));
391 EXPECT_TRUE(cookie->IsOnPath("/test")); 387 EXPECT_TRUE(cookie->IsOnPath("/test"));
392 EXPECT_TRUE(cookie->IsOnPath("/test/bar.html")); 388 EXPECT_TRUE(cookie->IsOnPath("/test/bar.html"));
393 EXPECT_TRUE(cookie->IsOnPath("/test/sample/bar.html")); 389 EXPECT_TRUE(cookie->IsOnPath("/test/sample/bar.html"));
394 } 390 }
395 391
396 TEST(CanonicalCookieTest, IncludeForRequestURL) { 392 TEST(CanonicalCookieTest, IncludeForRequestURL) {
397 GURL url("http://www.example.com"); 393 GURL url("http://www.example.com");
398 base::Time creation_time = base::Time::Now(); 394 base::Time creation_time = base::Time::Now();
399 CookieOptions options; 395 CookieOptions options;
400 396
401 scoped_ptr<CanonicalCookie> cookie( 397 scoped_ptr<CanonicalCookie> cookie(
402 CanonicalCookie::Create(url, "A=2", creation_time, options)); 398 CanonicalCookie::Create(url, "A=2", creation_time, options));
403 EXPECT_TRUE(cookie->IncludeForRequestURL(url, options)); 399 EXPECT_TRUE(cookie->IncludeForRequestURL(url, options));
404 EXPECT_TRUE(cookie->IncludeForRequestURL( 400 EXPECT_TRUE(cookie->IncludeForRequestURL(
405 GURL("http://www.example.com/foo/bar"), options)); 401 GURL("http://www.example.com/foo/bar"), options));
406 EXPECT_TRUE(cookie->IncludeForRequestURL( 402 EXPECT_TRUE(cookie->IncludeForRequestURL(
407 GURL("https://www.example.com/foo/bar"), options)); 403 GURL("https://www.example.com/foo/bar"), options));
408 EXPECT_FALSE( 404 EXPECT_FALSE(
409 cookie->IncludeForRequestURL(GURL("https://sub.example.com"), options)); 405 cookie->IncludeForRequestURL(GURL("https://sub.example.com"), options));
410 EXPECT_FALSE(cookie->IncludeForRequestURL(GURL("https://sub.www.example.com"), 406 EXPECT_FALSE(cookie->IncludeForRequestURL(GURL("https://sub.www.example.com"),
411 options)); 407 options));
412 408
413 // Test that cookie with a cookie path that does not match the url path are 409 // Test that cookie with a cookie path that does not match the url path are
414 // not included. 410 // not included.
415 cookie.reset(CanonicalCookie::Create(url, "A=2; Path=/foo/bar", creation_time, 411 cookie = CanonicalCookie::Create(url, "A=2; Path=/foo/bar", creation_time,
416 options)); 412 options);
417 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options)); 413 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options));
418 EXPECT_TRUE(cookie->IncludeForRequestURL( 414 EXPECT_TRUE(cookie->IncludeForRequestURL(
419 GURL("http://www.example.com/foo/bar/index.html"), options)); 415 GURL("http://www.example.com/foo/bar/index.html"), options));
420 416
421 // Test that a secure cookie is not included for a non secure URL. 417 // Test that a secure cookie is not included for a non secure URL.
422 GURL secure_url("https://www.example.com"); 418 GURL secure_url("https://www.example.com");
423 cookie.reset(CanonicalCookie::Create(secure_url, "A=2; Secure", creation_time, 419 cookie = CanonicalCookie::Create(secure_url, "A=2; Secure", creation_time,
424 options)); 420 options);
425 EXPECT_TRUE(cookie->IsSecure()); 421 EXPECT_TRUE(cookie->IsSecure());
426 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options)); 422 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options));
427 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options)); 423 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options));
428 424
429 // Test that http only cookies are only included if the include httponly flag 425 // Test that http only cookies are only included if the include httponly flag
430 // is set on the cookie options. 426 // is set on the cookie options.
431 options.set_include_httponly(); 427 options.set_include_httponly();
432 cookie.reset( 428 cookie =
433 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, options)); 429 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, options);
434 EXPECT_TRUE(cookie->IsHttpOnly()); 430 EXPECT_TRUE(cookie->IsHttpOnly());
435 EXPECT_TRUE(cookie->IncludeForRequestURL(url, options)); 431 EXPECT_TRUE(cookie->IncludeForRequestURL(url, options));
436 options.set_exclude_httponly(); 432 options.set_exclude_httponly();
437 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options)); 433 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options));
438 } 434 }
439 435
440 TEST(CanonicalCookieTest, IncludeFirstPartyForFirstPartyURL) { 436 TEST(CanonicalCookieTest, IncludeFirstPartyForFirstPartyURL) {
441 GURL insecure_url("http://example.test"); 437 GURL insecure_url("http://example.test");
442 GURL secure_url("https://example.test"); 438 GURL secure_url("https://example.test");
443 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");
444 GURL third_party_url("https://not-example.test"); 440 GURL third_party_url("https://not-example.test");
445 base::Time creation_time = base::Time::Now(); 441 base::Time creation_time = base::Time::Now();
446 CookieOptions options; 442 CookieOptions options;
447 scoped_ptr<CanonicalCookie> cookie; 443 scoped_ptr<CanonicalCookie> cookie;
448 444
449 // First-party-only cookies are not included for non-first-party requests, 445 // First-party-only cookies are not included for non-first-party requests,
450 // even if other properties match: 446 // even if other properties match:
451 cookie.reset(CanonicalCookie::Create(secure_url, "A=2; First-Party-Only", 447 cookie = CanonicalCookie::Create(secure_url, "A=2; First-Party-Only",
452 creation_time, options)); 448 creation_time, options);
453 EXPECT_TRUE(cookie->IsFirstPartyOnly()); 449 EXPECT_TRUE(cookie->IsFirstPartyOnly());
454 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); 450 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options));
455 cookie.reset(CanonicalCookie::Create( 451 cookie = CanonicalCookie::Create(secure_url, "A=2; Secure; First-Party-Only",
456 secure_url, "A=2; Secure; First-Party-Only", creation_time, options)); 452 creation_time, options);
457 EXPECT_TRUE(cookie->IsFirstPartyOnly()); 453 EXPECT_TRUE(cookie->IsFirstPartyOnly());
458 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); 454 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options));
459 cookie.reset(CanonicalCookie::Create(secure_url_with_path, 455 cookie = CanonicalCookie::Create(secure_url_with_path,
460 "A=2; First-Party-Only; path=/foo/bar", 456 "A=2; First-Party-Only; path=/foo/bar",
461 creation_time, options)); 457 creation_time, options);
462 EXPECT_TRUE(cookie->IsFirstPartyOnly()); 458 EXPECT_TRUE(cookie->IsFirstPartyOnly());
463 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options)); 459 EXPECT_FALSE(cookie->IncludeForRequestURL(secure_url, options));
464 460
465 // First-party-only cookies are included for first-party requests: 461 // First-party-only cookies are included for first-party requests:
466 options.set_include_first_party_only_cookies(); 462 options.set_include_first_party_only_cookies();
467 cookie.reset(CanonicalCookie::Create(secure_url, "A=2; First-Party-Only", 463 cookie = CanonicalCookie::Create(secure_url, "A=2; First-Party-Only",
468 creation_time, options)); 464 creation_time, options);
469 EXPECT_TRUE(cookie->IsFirstPartyOnly()); 465 EXPECT_TRUE(cookie->IsFirstPartyOnly());
470 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options)); 466 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options));
471 cookie.reset(CanonicalCookie::Create( 467 cookie = CanonicalCookie::Create(secure_url, "A=2; Secure; First-Party-Only",
472 secure_url, "A=2; Secure; First-Party-Only", creation_time, options)); 468 creation_time, options);
473 EXPECT_TRUE(cookie->IsFirstPartyOnly()); 469 EXPECT_TRUE(cookie->IsFirstPartyOnly());
474 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options)); 470 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options));
475 cookie.reset(CanonicalCookie::Create(secure_url_with_path, 471 cookie = CanonicalCookie::Create(secure_url_with_path,
476 "A=2; First-Party-Only; path=/foo/bar", 472 "A=2; First-Party-Only; path=/foo/bar",
477 creation_time, options)); 473 creation_time, options);
478 EXPECT_TRUE(cookie->IsFirstPartyOnly()); 474 EXPECT_TRUE(cookie->IsFirstPartyOnly());
479 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url_with_path, options)); 475 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url_with_path, options));
480 } 476 }
481 477
482 TEST(CanonicalCookieTest, PartialCompare) { 478 TEST(CanonicalCookieTest, PartialCompare) {
483 GURL url("http://www.example.com"); 479 GURL url("http://www.example.com");
484 base::Time creation_time = base::Time::Now(); 480 base::Time creation_time = base::Time::Now();
485 CookieOptions options; 481 CookieOptions options;
486 scoped_ptr<CanonicalCookie> cookie( 482 scoped_ptr<CanonicalCookie> cookie(
487 CanonicalCookie::Create(url, "a=b", creation_time, options)); 483 CanonicalCookie::Create(url, "a=b", creation_time, options));
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 check_consistency(*cookie_different_path, *cookie_different_value); 538 check_consistency(*cookie_different_path, *cookie_different_value);
543 } 539 }
544 540
545 TEST(CanonicalCookieTest, SecureCookiePrefix) { 541 TEST(CanonicalCookieTest, SecureCookiePrefix) {
546 GURL https_url("https://www.example.test"); 542 GURL https_url("https://www.example.test");
547 GURL http_url("http://www.example.test"); 543 GURL http_url("http://www.example.test");
548 base::Time creation_time = base::Time::Now(); 544 base::Time creation_time = base::Time::Now();
549 CookieOptions options; 545 CookieOptions options;
550 546
551 // A __Secure- cookie must be Secure. 547 // A __Secure- cookie must be Secure.
552 EXPECT_EQ(nullptr, make_scoped_ptr(CanonicalCookie::Create( 548 EXPECT_FALSE(CanonicalCookie::Create(https_url, "__Secure-A=B", creation_time,
553 https_url, "__Secure-A=B", creation_time, options))); 549 options));
554 EXPECT_EQ(nullptr, 550 EXPECT_FALSE(CanonicalCookie::Create(https_url, "__Secure-A=B; httponly",
555 make_scoped_ptr(CanonicalCookie::Create( 551 creation_time, options));
556 https_url, "__Secure-A=B; httponly", creation_time, options)));
557 552
558 // A typoed prefix does not have to be Secure. 553 // A typoed prefix does not have to be Secure.
559 EXPECT_NE(nullptr, 554 EXPECT_TRUE(CanonicalCookie::Create(https_url, "__secure-A=B; Secure",
560 make_scoped_ptr(CanonicalCookie::Create( 555 creation_time, options));
561 https_url, "__secure-A=B; Secure", creation_time, options))); 556 EXPECT_TRUE(CanonicalCookie::Create(https_url, "__secure-A=C;", creation_time,
562 EXPECT_NE(nullptr, make_scoped_ptr(CanonicalCookie::Create( 557 options));
563 https_url, "__secure-A=C;", creation_time, options))); 558 EXPECT_TRUE(CanonicalCookie::Create(https_url, "__SecureA=B; Secure",
564 EXPECT_NE(nullptr, 559 creation_time, options));
565 make_scoped_ptr(CanonicalCookie::Create( 560 EXPECT_TRUE(CanonicalCookie::Create(https_url, "__SecureA=C;", creation_time,
566 https_url, "__SecureA=B; Secure", creation_time, options))); 561 options));
567 EXPECT_NE(nullptr, make_scoped_ptr(CanonicalCookie::Create(
568 https_url, "__SecureA=C;", creation_time, options)));
569 562
570 // A __Secure- cookie can't be set on a non-secure origin. 563 // A __Secure- cookie can't be set on a non-secure origin.
571 EXPECT_EQ(nullptr, 564 EXPECT_FALSE(CanonicalCookie::Create(http_url, "__Secure-A=B; Secure",
572 make_scoped_ptr(CanonicalCookie::Create( 565 creation_time, options));
573 http_url, "__Secure-A=B; Secure", creation_time, options)));
574 } 566 }
575 567
576 TEST(CanonicalCookieTest, HostCookiePrefix) { 568 TEST(CanonicalCookieTest, HostCookiePrefix) {
577 GURL https_url("https://www.example.test"); 569 GURL https_url("https://www.example.test");
578 GURL http_url("http://www.example.test"); 570 GURL http_url("http://www.example.test");
579 base::Time creation_time = base::Time::Now(); 571 base::Time creation_time = base::Time::Now();
580 CookieOptions options; 572 CookieOptions options;
581 std::string domain = https_url.host(); 573 std::string domain = https_url.host();
582 574
583 // A __Host- cookie must be Secure. 575 // A __Host- cookie must be Secure.
584 EXPECT_EQ(nullptr, make_scoped_ptr(CanonicalCookie::Create( 576 EXPECT_FALSE(CanonicalCookie::Create(https_url, "__Host-A=B;", creation_time,
585 https_url, "__Host-A=B;", creation_time, options))); 577 options));
586 EXPECT_EQ(nullptr, 578 EXPECT_FALSE(CanonicalCookie::Create(
587 make_scoped_ptr(CanonicalCookie::Create( 579 https_url, "__Host-A=B; Domain=" + domain + "; Path=/;", creation_time,
588 https_url, "__Host-A=B; Domain=" + domain + "; Path=/;", 580 options));
589 creation_time, options))); 581 EXPECT_TRUE(CanonicalCookie::Create(https_url, "__Host-A=B; Path=/; Secure;",
590 EXPECT_NE(nullptr, make_scoped_ptr(CanonicalCookie::Create( 582 creation_time, options));
591 https_url, "__Host-A=B; Path=/; Secure;",
592 creation_time, options)));
593 583
594 // A __Host- cookie must be set from a secure scheme. 584 // A __Host- cookie must be set from a secure scheme.
595 EXPECT_EQ(nullptr, 585 EXPECT_FALSE(CanonicalCookie::Create(
596 make_scoped_ptr(CanonicalCookie::Create( 586 http_url, "__Host-A=B; Domain=" + domain + "; Path=/; Secure;",
597 http_url, "__Host-A=B; Domain=" + domain + "; Path=/; Secure;", 587 creation_time, options));
598 creation_time, options))); 588 EXPECT_TRUE(CanonicalCookie::Create(https_url, "__Host-A=B; Path=/; Secure;",
599 EXPECT_NE(nullptr, make_scoped_ptr(CanonicalCookie::Create( 589 creation_time, options));
600 https_url, "__Host-A=B; Path=/; Secure;",
601 creation_time, options)));
602 590
603 // A __Host- cookie can't have a Domain. 591 // A __Host- cookie can't have a Domain.
604 EXPECT_EQ(nullptr, 592 EXPECT_FALSE(CanonicalCookie::Create(
605 make_scoped_ptr(CanonicalCookie::Create( 593 https_url, "__Host-A=B; Domain=" + domain + "; Path=/; Secure;",
606 https_url, "__Host-A=B; Domain=" + domain + "; Path=/; Secure;", 594 creation_time, options));
607 creation_time, options))); 595 EXPECT_FALSE(CanonicalCookie::Create(
608 EXPECT_EQ(nullptr, 596 https_url, "__Host-A=B; Domain=" + domain + "; Secure;", creation_time,
609 make_scoped_ptr(CanonicalCookie::Create( 597 options));
610 https_url, "__Host-A=B; Domain=" + domain + "; Secure;",
611 creation_time, options)));
612 598
613 // A __Host- cookie must have a Path of "/". 599 // A __Host- cookie must have a Path of "/".
614 EXPECT_EQ(nullptr, make_scoped_ptr(CanonicalCookie::Create( 600 EXPECT_FALSE(CanonicalCookie::Create(
615 https_url, "__Host-A=B; Path=/foo; Secure;", 601 https_url, "__Host-A=B; Path=/foo; Secure;", creation_time, options));
616 creation_time, options))); 602 EXPECT_FALSE(CanonicalCookie::Create(https_url, "__Host-A=B; Secure;",
617 EXPECT_EQ(nullptr, 603 creation_time, options));
618 make_scoped_ptr(CanonicalCookie::Create( 604 EXPECT_TRUE(CanonicalCookie::Create(https_url, "__Host-A=B; Secure; Path=/;",
619 https_url, "__Host-A=B; Secure;", creation_time, options))); 605 creation_time, options));
620 EXPECT_NE(nullptr, make_scoped_ptr(CanonicalCookie::Create(
621 https_url, "__Host-A=B; Secure; Path=/;",
622 creation_time, options)));
623 606
624 // Rules don't apply for a typoed prefix. 607 // Rules don't apply for a typoed prefix.
625 EXPECT_NE(nullptr, 608 EXPECT_TRUE(CanonicalCookie::Create(
626 make_scoped_ptr(CanonicalCookie::Create( 609 http_url, "__host-A=B; Domain=" + domain + "; Path=/; Secure;",
627 http_url, "__host-A=B; Domain=" + domain + "; Path=/; Secure;", 610 creation_time, options));
628 creation_time, options))); 611 EXPECT_TRUE(CanonicalCookie::Create(
629 EXPECT_NE(nullptr, make_scoped_ptr(CanonicalCookie::Create( 612 https_url, "__HostA=B; Domain=" + domain + "; Secure;", creation_time,
630 https_url, "__HostA=B; Domain=" + domain + "; Secure;", 613 options));
631 creation_time, options)));
632 } 614 }
633 615
634 TEST(CanonicalCookieTest, EnforceSecureCookiesRequireSecureScheme) { 616 TEST(CanonicalCookieTest, EnforceSecureCookiesRequireSecureScheme) {
635 GURL http_url("http://www.example.com"); 617 GURL http_url("http://www.example.com");
636 GURL https_url("https://www.example.com"); 618 GURL https_url("https://www.example.com");
637 base::Time creation_time = base::Time::Now(); 619 base::Time creation_time = base::Time::Now();
638 CookieOptions options; 620 CookieOptions options;
639 options.set_enforce_strict_secure(); 621 options.set_enforce_strict_secure();
640 622
641 scoped_ptr<CanonicalCookie> http_cookie_no_secure( 623 scoped_ptr<CanonicalCookie> http_cookie_no_secure(
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
676 } 658 }
677 659
678 TEST(CanonicalCookieTest, TestPrefixHistograms) { 660 TEST(CanonicalCookieTest, TestPrefixHistograms) {
679 base::HistogramTester histograms; 661 base::HistogramTester histograms;
680 const char kCookiePrefixHistogram[] = "Cookie.CookiePrefix"; 662 const char kCookiePrefixHistogram[] = "Cookie.CookiePrefix";
681 const char kCookiePrefixBlockedHistogram[] = "Cookie.CookiePrefixBlocked"; 663 const char kCookiePrefixBlockedHistogram[] = "Cookie.CookiePrefixBlocked";
682 GURL https_url("https://www.example.test"); 664 GURL https_url("https://www.example.test");
683 base::Time creation_time = base::Time::Now(); 665 base::Time creation_time = base::Time::Now();
684 CookieOptions options; 666 CookieOptions options;
685 667
686 scoped_ptr<CanonicalCookie> cookie1 = make_scoped_ptr(CanonicalCookie::Create( 668 EXPECT_FALSE(CanonicalCookie::Create(https_url, "__Host-A=B;", creation_time,
687 https_url, "__Host-A=B;", creation_time, options)); 669 options));
688 EXPECT_EQ(nullptr, cookie1);
689 670
690 histograms.ExpectBucketCount(kCookiePrefixHistogram, 671 histograms.ExpectBucketCount(kCookiePrefixHistogram,
691 CanonicalCookie::COOKIE_PREFIX_HOST, 1); 672 CanonicalCookie::COOKIE_PREFIX_HOST, 1);
692 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram, 673 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram,
693 CanonicalCookie::COOKIE_PREFIX_HOST, 1); 674 CanonicalCookie::COOKIE_PREFIX_HOST, 1);
694 675
695 EXPECT_NE(nullptr, make_scoped_ptr(CanonicalCookie::Create( 676 EXPECT_TRUE(CanonicalCookie::Create(https_url, "__Host-A=B; Path=/; Secure",
696 https_url, "__Host-A=B; Path=/; Secure", creation_time, 677 creation_time, options));
697 options)));
698 histograms.ExpectBucketCount(kCookiePrefixHistogram, 678 histograms.ExpectBucketCount(kCookiePrefixHistogram,
699 CanonicalCookie::COOKIE_PREFIX_HOST, 2); 679 CanonicalCookie::COOKIE_PREFIX_HOST, 2);
700 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram, 680 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram,
701 CanonicalCookie::COOKIE_PREFIX_HOST, 1); 681 CanonicalCookie::COOKIE_PREFIX_HOST, 1);
702 EXPECT_NE(nullptr, make_scoped_ptr(CanonicalCookie::Create( 682 EXPECT_TRUE(CanonicalCookie::Create(https_url, "__HostA=B; Path=/; Secure",
703 https_url, "__HostA=B; Path=/; Secure", creation_time, 683 creation_time, options));
704 options)));
705 histograms.ExpectBucketCount(kCookiePrefixHistogram, 684 histograms.ExpectBucketCount(kCookiePrefixHistogram,
706 CanonicalCookie::COOKIE_PREFIX_HOST, 2); 685 CanonicalCookie::COOKIE_PREFIX_HOST, 2);
707 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram, 686 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram,
708 CanonicalCookie::COOKIE_PREFIX_HOST, 1); 687 CanonicalCookie::COOKIE_PREFIX_HOST, 1);
709 688
710 scoped_ptr<CanonicalCookie> cookie2 = make_scoped_ptr(CanonicalCookie::Create( 689 EXPECT_FALSE(CanonicalCookie::Create(https_url, "__Secure-A=B;",
711 https_url, "__Secure-A=B;", creation_time, options)); 690 creation_time, options));
712 EXPECT_EQ(nullptr, cookie2);
713 691
714 histograms.ExpectBucketCount(kCookiePrefixHistogram, 692 histograms.ExpectBucketCount(kCookiePrefixHistogram,
715 CanonicalCookie::COOKIE_PREFIX_SECURE, 1); 693 CanonicalCookie::COOKIE_PREFIX_SECURE, 1);
716 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram, 694 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram,
717 CanonicalCookie::COOKIE_PREFIX_SECURE, 1); 695 CanonicalCookie::COOKIE_PREFIX_SECURE, 1);
718 EXPECT_NE(nullptr, make_scoped_ptr(CanonicalCookie::Create( 696 EXPECT_TRUE(CanonicalCookie::Create(https_url, "__Secure-A=B; Path=/; Secure",
719 https_url, "__Secure-A=B; Path=/; Secure", 697 creation_time, options));
720 creation_time, options)));
721 histograms.ExpectBucketCount(kCookiePrefixHistogram, 698 histograms.ExpectBucketCount(kCookiePrefixHistogram,
722 CanonicalCookie::COOKIE_PREFIX_SECURE, 2); 699 CanonicalCookie::COOKIE_PREFIX_SECURE, 2);
723 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram, 700 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram,
724 CanonicalCookie::COOKIE_PREFIX_SECURE, 1); 701 CanonicalCookie::COOKIE_PREFIX_SECURE, 1);
725 EXPECT_NE(nullptr, make_scoped_ptr(CanonicalCookie::Create( 702 EXPECT_TRUE(CanonicalCookie::Create(https_url, "__SecureA=B; Path=/; Secure",
726 https_url, "__SecureA=B; Path=/; Secure", 703 creation_time, options));
727 creation_time, options)));
728 histograms.ExpectBucketCount(kCookiePrefixHistogram, 704 histograms.ExpectBucketCount(kCookiePrefixHistogram,
729 CanonicalCookie::COOKIE_PREFIX_SECURE, 2); 705 CanonicalCookie::COOKIE_PREFIX_SECURE, 2);
730 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram, 706 histograms.ExpectBucketCount(kCookiePrefixBlockedHistogram,
731 CanonicalCookie::COOKIE_PREFIX_SECURE, 1); 707 CanonicalCookie::COOKIE_PREFIX_SECURE, 1);
732 } 708 }
733 709
734 } // namespace net 710 } // namespace net
OLDNEW
« no previous file with comments | « net/cookies/canonical_cookie.cc ('k') | net/cookies/cookie_monster.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698