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

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

Issue 2246613003: Update cookie value and attribute setting behavior to match other UAs (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Simplification Created 4 years, 4 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/parsed_cookie.cc ('k') | no next file » | 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 <string> 5 #include <string>
6 6
7 #include "net/cookies/cookie_constants.h" 7 #include "net/cookies/cookie_constants.h"
8 #include "net/cookies/parsed_cookie.h" 8 #include "net/cookies/parsed_cookie.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 namespace net { 11 namespace net {
12 12
13 TEST(ParsedCookieTest, TestBasic) { 13 TEST(ParsedCookieTest, TestBasic) {
14 ParsedCookie pc("a=b"); 14 ParsedCookie pc("a=b");
15 EXPECT_TRUE(pc.IsValid()); 15 EXPECT_TRUE(pc.IsValid());
16 EXPECT_FALSE(pc.IsSecure()); 16 EXPECT_FALSE(pc.IsSecure());
17 EXPECT_EQ("a", pc.Name()); 17 EXPECT_EQ("a", pc.Name());
18 EXPECT_EQ("b", pc.Value()); 18 EXPECT_EQ("b", pc.Value());
19 } 19 }
20 20
21 // De facto standard behavior, per https://crbug.com/601786.
21 TEST(ParsedCookieTest, TestEmpty) { 22 TEST(ParsedCookieTest, TestEmpty) {
22 ParsedCookie pc1("=; path=/; secure;"); 23 const struct {
23 EXPECT_FALSE(pc1.IsValid()); 24 const char* cookie;
24 ParsedCookie pc2("= ; path=/; secure;"); 25 const char* expected_path;
25 EXPECT_FALSE(pc2.IsValid()); 26 bool expect_secure;
26 ParsedCookie pc3(" =; path=/; secure;"); 27 } kTestCookieLines[]{{"", "", false}, {" ", "", false},
27 EXPECT_FALSE(pc3.IsValid()); 28 {"=;", "", false}, {"=; path=/; secure;", "/", true},
28 ParsedCookie pc4(" = ; path=/; secure;"); 29 {"= ;", "", false}, {"= ; path=/; secure;", "/", true},
29 EXPECT_FALSE(pc4.IsValid()); 30 {" =;", "", false}, {" =; path=/; secure;", "/", true},
30 ParsedCookie pc5(" ; path=/; secure;"); 31 {" = ;", "", false}, {" = ; path=/; secure;", "/", true},
31 EXPECT_FALSE(pc5.IsValid()); 32 {" ;", "", false}, {" ; path=/; secure;", "/", true},
32 ParsedCookie pc6("; path=/; secure;"); 33 {";", "", false}, {"; path=/; secure;", "/", true},
33 EXPECT_FALSE(pc6.IsValid()); 34 {"\t;", "", false}, {"\t; path=/; secure;", "/", true}};
35
36 for (const auto& test : kTestCookieLines) {
37 ParsedCookie pc(test.cookie);
38 EXPECT_TRUE(pc.IsValid());
39 EXPECT_EQ("", pc.Name());
40 EXPECT_EQ("", pc.Value());
41 EXPECT_EQ(test.expected_path, pc.Path());
42 EXPECT_EQ(test.expect_secure, pc.IsSecure());
43 }
34 } 44 }
35 45
36 TEST(ParsedCookieTest, TestQuoted) { 46 TEST(ParsedCookieTest, TestQuoted) {
37 // These are some quoting cases which the major browsers all 47 // These are some quoting cases which the major browsers all
38 // handle differently. I've tested Internet Explorer 6, Opera 9.6, 48 // handle differently. I've tested Internet Explorer 6, Opera 9.6,
39 // Firefox 3, and Safari Windows 3.2.1. We originally tried to match 49 // Firefox 3, and Safari Windows 3.2.1. We originally tried to match
40 // Firefox closely, however we now match Internet Explorer and Safari. 50 // Firefox closely, however we now match Internet Explorer and Safari.
41 const char* const values[] = { 51 const char* const values[] = {
42 // Trailing whitespace after a quoted value. The whitespace after 52 // Trailing whitespace after a quoted value. The whitespace after
43 // the quote is stripped in all browsers. 53 // the quote is stripped in all browsers.
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 ParsedCookie pc1("a=b;" + blankpairs + "secure"); 224 ParsedCookie pc1("a=b;" + blankpairs + "secure");
215 EXPECT_TRUE(pc1.IsValid()); 225 EXPECT_TRUE(pc1.IsValid());
216 EXPECT_TRUE(pc1.IsSecure()); 226 EXPECT_TRUE(pc1.IsSecure());
217 227
218 ParsedCookie pc2("a=b;" + blankpairs + ";secure"); 228 ParsedCookie pc2("a=b;" + blankpairs + ";secure");
219 EXPECT_TRUE(pc2.IsValid()); 229 EXPECT_TRUE(pc2.IsValid());
220 EXPECT_FALSE(pc2.IsSecure()); 230 EXPECT_FALSE(pc2.IsSecure());
221 } 231 }
222 232
223 // TODO(erikwright): some better test cases for invalid cookies. 233 // TODO(erikwright): some better test cases for invalid cookies.
224 TEST(ParsedCookieTest, InvalidWhitespace) {
225 ParsedCookie pc(" ");
226 EXPECT_FALSE(pc.IsValid());
227 }
228
229 TEST(ParsedCookieTest, InvalidTooLong) { 234 TEST(ParsedCookieTest, InvalidTooLong) {
230 std::string maxstr; 235 std::string maxstr;
231 maxstr.resize(ParsedCookie::kMaxCookieSize, 'a'); 236 maxstr.resize(ParsedCookie::kMaxCookieSize, 'a');
232 237
233 ParsedCookie pc1(maxstr); 238 ParsedCookie pc1(maxstr);
234 EXPECT_TRUE(pc1.IsValid()); 239 EXPECT_TRUE(pc1.IsValid());
235 240
236 ParsedCookie pc2(maxstr + "A"); 241 ParsedCookie pc2(maxstr + "A");
237 EXPECT_FALSE(pc2.IsValid()); 242 EXPECT_FALSE(pc2.IsValid());
238 } 243 }
239 244
240 TEST(ParsedCookieTest, InvalidEmpty) {
241 ParsedCookie pc((std::string()));
242 EXPECT_FALSE(pc.IsValid());
243 }
244
245 TEST(ParsedCookieTest, EmbeddedTerminator) { 245 TEST(ParsedCookieTest, EmbeddedTerminator) {
246 ParsedCookie pc1("AAA=BB\0ZYX"); 246 ParsedCookie pc1("AAA=BB\0ZYX");
247 ParsedCookie pc2("AAA=BB\rZYX"); 247 ParsedCookie pc2("AAA=BB\rZYX");
248 ParsedCookie pc3("AAA=BB\nZYX"); 248 ParsedCookie pc3("AAA=BB\nZYX");
249 EXPECT_TRUE(pc1.IsValid()); 249 EXPECT_TRUE(pc1.IsValid());
250 EXPECT_EQ("AAA", pc1.Name()); 250 EXPECT_EQ("AAA", pc1.Name());
251 EXPECT_EQ("BB", pc1.Value()); 251 EXPECT_EQ("BB", pc1.Value());
252 EXPECT_TRUE(pc2.IsValid()); 252 EXPECT_TRUE(pc2.IsValid());
253 EXPECT_EQ("AAA", pc2.Name()); 253 EXPECT_EQ("AAA", pc2.Name());
254 EXPECT_EQ("BB", pc2.Value()); 254 EXPECT_EQ("BB", pc2.Value());
(...skipping 23 matching lines...) Expand all
278 const char output[] = 278 const char output[] =
279 "ANCUUID=zohNumRKgI0oxyhSsV3Z7D; " 279 "ANCUUID=zohNumRKgI0oxyhSsV3Z7D; "
280 "expires=Sun, 18-Apr-2027 21:06:29 GMT; " 280 "expires=Sun, 18-Apr-2027 21:06:29 GMT; "
281 "path=/; priority=low"; 281 "path=/; priority=low";
282 ParsedCookie pc(input); 282 ParsedCookie pc(input);
283 EXPECT_EQ(output, pc.ToCookieLine()); 283 EXPECT_EQ(output, pc.ToCookieLine());
284 } 284 }
285 285
286 TEST(ParsedCookieTest, SetNameAndValue) { 286 TEST(ParsedCookieTest, SetNameAndValue) {
287 ParsedCookie empty((std::string())); 287 ParsedCookie empty((std::string()));
288 EXPECT_FALSE(empty.IsValid()); 288 EXPECT_TRUE(empty.IsValid());
289 EXPECT_FALSE(empty.SetDomain("foobar.com")); 289 EXPECT_TRUE(empty.SetDomain("foobar.com"));
290 EXPECT_TRUE(empty.SetName("name")); 290 EXPECT_TRUE(empty.SetName("name"));
291 EXPECT_TRUE(empty.SetValue("value")); 291 EXPECT_TRUE(empty.SetValue("value"));
292 EXPECT_EQ("name=value", empty.ToCookieLine()); 292 EXPECT_EQ("name=value; domain=foobar.com", empty.ToCookieLine());
293 EXPECT_TRUE(empty.IsValid()); 293 EXPECT_TRUE(empty.IsValid());
294 294
295 // We don't test 295 // We don't test
296 // ParsedCookie invalid("@foo=bar"); 296 // ParsedCookie invalid("@foo=bar");
297 // EXPECT_FALSE(invalid.IsValid()); 297 // EXPECT_FALSE(invalid.IsValid());
298 // here because we are slightly more tolerant to invalid cookie names and 298 // here because we are slightly more tolerant to invalid cookie names and
299 // values that are set by webservers. We only enforce a correct name and 299 // values that are set by webservers. We only enforce a correct name and
300 // value if set via SetName() and SetValue(). 300 // value if set via SetName() and SetValue().
301 301
302 ParsedCookie pc("name=value"); 302 ParsedCookie pc("name=value");
303 EXPECT_TRUE(pc.IsValid()); 303 EXPECT_TRUE(pc.IsValid());
304 304
305 // Set invalid name / value. 305 // Set invalid name / value.
306 EXPECT_FALSE(pc.SetName("@foobar")); 306 EXPECT_FALSE(pc.SetName("@foobar"));
307 EXPECT_EQ("name=value", pc.ToCookieLine()); 307 EXPECT_EQ("name=value", pc.ToCookieLine());
308 EXPECT_TRUE(pc.IsValid()); 308 EXPECT_TRUE(pc.IsValid());
309 309
310 EXPECT_FALSE(pc.SetName(std::string()));
311 EXPECT_EQ("name=value", pc.ToCookieLine());
312 EXPECT_TRUE(pc.IsValid());
313
314 EXPECT_FALSE(pc.SetValue("foo bar")); 310 EXPECT_FALSE(pc.SetValue("foo bar"));
315 EXPECT_EQ("name=value", pc.ToCookieLine()); 311 EXPECT_EQ("name=value", pc.ToCookieLine());
316 EXPECT_TRUE(pc.IsValid()); 312 EXPECT_TRUE(pc.IsValid());
317 313
318 EXPECT_FALSE(pc.SetValue("\"foobar")); 314 EXPECT_FALSE(pc.SetValue("\"foobar"));
319 EXPECT_EQ("name=value", pc.ToCookieLine()); 315 EXPECT_EQ("name=value", pc.ToCookieLine());
320 EXPECT_TRUE(pc.IsValid()); 316 EXPECT_TRUE(pc.IsValid());
321 317
322 // Set valid name / value 318 // Set valid name / value
319 EXPECT_TRUE(pc.SetName(std::string()));
320 EXPECT_EQ("=value", pc.ToCookieLine());
321 EXPECT_TRUE(pc.IsValid());
322
323 EXPECT_TRUE(pc.SetName("test")); 323 EXPECT_TRUE(pc.SetName("test"));
324 EXPECT_EQ("test=value", pc.ToCookieLine()); 324 EXPECT_EQ("test=value", pc.ToCookieLine());
325 EXPECT_TRUE(pc.IsValid()); 325 EXPECT_TRUE(pc.IsValid());
326 326
327 EXPECT_TRUE(pc.SetValue("\"foobar\"")); 327 EXPECT_TRUE(pc.SetValue("\"foobar\""));
328 EXPECT_EQ("test=\"foobar\"", pc.ToCookieLine()); 328 EXPECT_EQ("test=\"foobar\"", pc.ToCookieLine());
329 EXPECT_TRUE(pc.IsValid()); 329 EXPECT_TRUE(pc.IsValid());
330 330
331 EXPECT_TRUE(pc.SetValue(std::string())); 331 EXPECT_TRUE(pc.SetValue(std::string()));
332 EXPECT_EQ("test=", pc.ToCookieLine()); 332 EXPECT_EQ("test=", pc.ToCookieLine());
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 EXPECT_FALSE(pc.HasDomain()); 408 EXPECT_FALSE(pc.HasDomain());
409 EXPECT_FALSE(pc.HasPath()); 409 EXPECT_FALSE(pc.HasPath());
410 EXPECT_FALSE(pc.HasExpires()); 410 EXPECT_FALSE(pc.HasExpires());
411 EXPECT_FALSE(pc.HasMaxAge()); 411 EXPECT_FALSE(pc.HasMaxAge());
412 EXPECT_FALSE(pc.IsSecure()); 412 EXPECT_FALSE(pc.IsSecure());
413 EXPECT_FALSE(pc.IsHttpOnly()); 413 EXPECT_FALSE(pc.IsHttpOnly());
414 EXPECT_EQ(CookieSameSite::NO_RESTRICTION, pc.SameSite()); 414 EXPECT_EQ(CookieSameSite::NO_RESTRICTION, pc.SameSite());
415 EXPECT_EQ("name2=value2", pc.ToCookieLine()); 415 EXPECT_EQ("name2=value2", pc.ToCookieLine());
416 } 416 }
417 417
418 // Set the domain attribute twice in a cookie line. If the second attribute's
419 // value is empty, it shoud be ignored.
420 //
421 // This is de facto standard behavior, per https://crbug.com/601786.
422 TEST(ParsedCookieTest, MultipleDomainAttributes) {
423 ParsedCookie pc1("name=value; domain=foo.com; domain=bar.com");
424 EXPECT_EQ("bar.com", pc1.Domain());
425 ParsedCookie pc2("name=value; domain=foo.com; domain=");
426 EXPECT_EQ("foo.com", pc2.Domain());
427 }
428
418 TEST(ParsedCookieTest, SetPriority) { 429 TEST(ParsedCookieTest, SetPriority) {
419 ParsedCookie pc("name=value"); 430 ParsedCookie pc("name=value");
420 EXPECT_TRUE(pc.IsValid()); 431 EXPECT_TRUE(pc.IsValid());
421 432
422 EXPECT_EQ("name=value", pc.ToCookieLine()); 433 EXPECT_EQ("name=value", pc.ToCookieLine());
423 EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority()); 434 EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority());
424 435
425 // Test each priority, expect case-insensitive compare. 436 // Test each priority, expect case-insensitive compare.
426 EXPECT_TRUE(pc.SetPriority("high")); 437 EXPECT_TRUE(pc.SetPriority("high"));
427 EXPECT_EQ("name=value; priority=high", pc.ToCookieLine()); 438 EXPECT_EQ("name=value; priority=high", pc.ToCookieLine());
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 EXPECT_TRUE(pc5.IsValid()); 605 EXPECT_TRUE(pc5.IsValid());
595 EXPECT_EQ(pc5_literal, pc5.ToCookieLine()); 606 EXPECT_EQ(pc5_literal, pc5.ToCookieLine());
596 EXPECT_TRUE(pc6.IsValid()); 607 EXPECT_TRUE(pc6.IsValid());
597 EXPECT_EQ(pc6_literal, pc6.ToCookieLine()); 608 EXPECT_EQ(pc6_literal, pc6.ToCookieLine());
598 EXPECT_TRUE(pc7.IsValid()); 609 EXPECT_TRUE(pc7.IsValid());
599 EXPECT_EQ(pc7_literal, pc7.ToCookieLine()); 610 EXPECT_EQ(pc7_literal, pc7.ToCookieLine());
600 EXPECT_TRUE(pc8.IsValid()); 611 EXPECT_TRUE(pc8.IsValid());
601 EXPECT_EQ(pc8_literal, pc8.ToCookieLine()); 612 EXPECT_EQ(pc8_literal, pc8.ToCookieLine());
602 } 613 }
603 } 614 }
OLDNEW
« no previous file with comments | « net/cookies/parsed_cookie.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698