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

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: Updated ParsedCookie unittests 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
« net/cookies/parsed_cookie.cc ('K') | « 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 ParsedCookie pc1("=; path=/; secure;");
23 EXPECT_FALSE(pc1.IsValid()); 24 EXPECT_TRUE(pc1.IsValid());
24 ParsedCookie pc2("= ; path=/; secure;"); 25 ParsedCookie pc2("= ; path=/; secure;");
25 EXPECT_FALSE(pc2.IsValid()); 26 EXPECT_TRUE(pc2.IsValid());
26 ParsedCookie pc3(" =; path=/; secure;"); 27 ParsedCookie pc3(" =; path=/; secure;");
27 EXPECT_FALSE(pc3.IsValid()); 28 EXPECT_TRUE(pc3.IsValid());
28 ParsedCookie pc4(" = ; path=/; secure;"); 29 ParsedCookie pc4(" = ; path=/; secure;");
29 EXPECT_FALSE(pc4.IsValid()); 30 EXPECT_TRUE(pc4.IsValid());
30 ParsedCookie pc5(" ; path=/; secure;"); 31 ParsedCookie pc5(" ; path=/; secure;");
31 EXPECT_FALSE(pc5.IsValid()); 32 EXPECT_TRUE(pc5.IsValid());
32 ParsedCookie pc6("; path=/; secure;"); 33 ParsedCookie pc6("; path=/; secure;");
33 EXPECT_FALSE(pc6.IsValid()); 34 EXPECT_TRUE(pc6.IsValid());
35 ParsedCookie pc7("\t; path=/; secure;");
36 EXPECT_TRUE(pc7.IsValid());
mmenke 2016/08/15 21:11:03 We should check the actual value, too, right? Als
mmenke 2016/08/15 21:11:03 We should also check ";" and "=" (With no extra st
mmenke 2016/08/15 21:11:03 optional: Suggest merging ValidOnlyWhitespace and
jww 2016/08/16 02:24:23 Good calls all around. I've done all of your sugge
34 } 37 }
35 38
36 TEST(ParsedCookieTest, TestQuoted) { 39 TEST(ParsedCookieTest, TestQuoted) {
37 // These are some quoting cases which the major browsers all 40 // These are some quoting cases which the major browsers all
38 // handle differently. I've tested Internet Explorer 6, Opera 9.6, 41 // 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 42 // Firefox 3, and Safari Windows 3.2.1. We originally tried to match
40 // Firefox closely, however we now match Internet Explorer and Safari. 43 // Firefox closely, however we now match Internet Explorer and Safari.
41 const char* const values[] = { 44 const char* const values[] = {
42 // Trailing whitespace after a quoted value. The whitespace after 45 // Trailing whitespace after a quoted value. The whitespace after
43 // the quote is stripped in all browsers. 46 // 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"); 217 ParsedCookie pc1("a=b;" + blankpairs + "secure");
215 EXPECT_TRUE(pc1.IsValid()); 218 EXPECT_TRUE(pc1.IsValid());
216 EXPECT_TRUE(pc1.IsSecure()); 219 EXPECT_TRUE(pc1.IsSecure());
217 220
218 ParsedCookie pc2("a=b;" + blankpairs + ";secure"); 221 ParsedCookie pc2("a=b;" + blankpairs + ";secure");
219 EXPECT_TRUE(pc2.IsValid()); 222 EXPECT_TRUE(pc2.IsValid());
220 EXPECT_FALSE(pc2.IsSecure()); 223 EXPECT_FALSE(pc2.IsSecure());
221 } 224 }
222 225
223 // TODO(erikwright): some better test cases for invalid cookies. 226 // 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) { 227 TEST(ParsedCookieTest, InvalidTooLong) {
230 std::string maxstr; 228 std::string maxstr;
231 maxstr.resize(ParsedCookie::kMaxCookieSize, 'a'); 229 maxstr.resize(ParsedCookie::kMaxCookieSize, 'a');
232 230
233 ParsedCookie pc1(maxstr); 231 ParsedCookie pc1(maxstr);
234 EXPECT_TRUE(pc1.IsValid()); 232 EXPECT_TRUE(pc1.IsValid());
235 233
236 ParsedCookie pc2(maxstr + "A"); 234 ParsedCookie pc2(maxstr + "A");
237 EXPECT_FALSE(pc2.IsValid()); 235 EXPECT_FALSE(pc2.IsValid());
238 } 236 }
239 237
240 TEST(ParsedCookieTest, InvalidEmpty) { 238 // De facto standard behavior, per https://crbug.com/601786.
239 TEST(ParsedCookieTest, ValidOnlyWhitespace) {
240 ParsedCookie pc(" ");
241 EXPECT_TRUE(pc.IsValid());
242 }
243
244 // De facto standard behavior, per https://crbug.com/601786.
245 TEST(ParsedCookieTest, ValidEmpty) {
241 ParsedCookie pc((std::string())); 246 ParsedCookie pc((std::string()));
242 EXPECT_FALSE(pc.IsValid()); 247 EXPECT_TRUE(pc.IsValid());
243 } 248 }
244 249
245 TEST(ParsedCookieTest, EmbeddedTerminator) { 250 TEST(ParsedCookieTest, EmbeddedTerminator) {
246 ParsedCookie pc1("AAA=BB\0ZYX"); 251 ParsedCookie pc1("AAA=BB\0ZYX");
247 ParsedCookie pc2("AAA=BB\rZYX"); 252 ParsedCookie pc2("AAA=BB\rZYX");
248 ParsedCookie pc3("AAA=BB\nZYX"); 253 ParsedCookie pc3("AAA=BB\nZYX");
249 EXPECT_TRUE(pc1.IsValid()); 254 EXPECT_TRUE(pc1.IsValid());
250 EXPECT_EQ("AAA", pc1.Name()); 255 EXPECT_EQ("AAA", pc1.Name());
251 EXPECT_EQ("BB", pc1.Value()); 256 EXPECT_EQ("BB", pc1.Value());
252 EXPECT_TRUE(pc2.IsValid()); 257 EXPECT_TRUE(pc2.IsValid());
(...skipping 25 matching lines...) Expand all
278 const char output[] = 283 const char output[] =
279 "ANCUUID=zohNumRKgI0oxyhSsV3Z7D; " 284 "ANCUUID=zohNumRKgI0oxyhSsV3Z7D; "
280 "expires=Sun, 18-Apr-2027 21:06:29 GMT; " 285 "expires=Sun, 18-Apr-2027 21:06:29 GMT; "
281 "path=/; priority=low"; 286 "path=/; priority=low";
282 ParsedCookie pc(input); 287 ParsedCookie pc(input);
283 EXPECT_EQ(output, pc.ToCookieLine()); 288 EXPECT_EQ(output, pc.ToCookieLine());
284 } 289 }
285 290
286 TEST(ParsedCookieTest, SetNameAndValue) { 291 TEST(ParsedCookieTest, SetNameAndValue) {
287 ParsedCookie empty((std::string())); 292 ParsedCookie empty((std::string()));
288 EXPECT_FALSE(empty.IsValid()); 293 EXPECT_TRUE(empty.IsValid());
289 EXPECT_FALSE(empty.SetDomain("foobar.com")); 294 EXPECT_TRUE(empty.SetDomain("foobar.com"));
290 EXPECT_TRUE(empty.SetName("name")); 295 EXPECT_TRUE(empty.SetName("name"));
291 EXPECT_TRUE(empty.SetValue("value")); 296 EXPECT_TRUE(empty.SetValue("value"));
292 EXPECT_EQ("name=value", empty.ToCookieLine()); 297 EXPECT_EQ("name=value; domain=foobar.com", empty.ToCookieLine());
293 EXPECT_TRUE(empty.IsValid()); 298 EXPECT_TRUE(empty.IsValid());
294 299
295 // We don't test 300 // We don't test
296 // ParsedCookie invalid("@foo=bar"); 301 // ParsedCookie invalid("@foo=bar");
297 // EXPECT_FALSE(invalid.IsValid()); 302 // EXPECT_FALSE(invalid.IsValid());
298 // here because we are slightly more tolerant to invalid cookie names and 303 // 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 304 // values that are set by webservers. We only enforce a correct name and
300 // value if set via SetName() and SetValue(). 305 // value if set via SetName() and SetValue().
301 306
302 ParsedCookie pc("name=value"); 307 ParsedCookie pc("name=value");
303 EXPECT_TRUE(pc.IsValid()); 308 EXPECT_TRUE(pc.IsValid());
304 309
305 // Set invalid name / value. 310 // Set invalid name / value.
306 EXPECT_FALSE(pc.SetName("@foobar")); 311 EXPECT_FALSE(pc.SetName("@foobar"));
307 EXPECT_EQ("name=value", pc.ToCookieLine()); 312 EXPECT_EQ("name=value", pc.ToCookieLine());
308 EXPECT_TRUE(pc.IsValid()); 313 EXPECT_TRUE(pc.IsValid());
309 314
310 EXPECT_FALSE(pc.SetName(std::string())); 315 EXPECT_FALSE(pc.SetName(std::string()));
mmenke 2016/08/15 21:11:03 Hrm...we allow unnamed cookies, but SetName doesn'
jww 2016/08/16 02:24:23 Good catch! Indeed, that doesn't make sense, so I'
311 EXPECT_EQ("name=value", pc.ToCookieLine()); 316 EXPECT_EQ("name=value", pc.ToCookieLine());
312 EXPECT_TRUE(pc.IsValid()); 317 EXPECT_TRUE(pc.IsValid());
313 318
314 EXPECT_FALSE(pc.SetValue("foo bar")); 319 EXPECT_FALSE(pc.SetValue("foo bar"));
315 EXPECT_EQ("name=value", pc.ToCookieLine()); 320 EXPECT_EQ("name=value", pc.ToCookieLine());
316 EXPECT_TRUE(pc.IsValid()); 321 EXPECT_TRUE(pc.IsValid());
317 322
318 EXPECT_FALSE(pc.SetValue("\"foobar")); 323 EXPECT_FALSE(pc.SetValue("\"foobar"));
319 EXPECT_EQ("name=value", pc.ToCookieLine()); 324 EXPECT_EQ("name=value", pc.ToCookieLine());
320 EXPECT_TRUE(pc.IsValid()); 325 EXPECT_TRUE(pc.IsValid());
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 EXPECT_FALSE(pc.HasDomain()); 413 EXPECT_FALSE(pc.HasDomain());
409 EXPECT_FALSE(pc.HasPath()); 414 EXPECT_FALSE(pc.HasPath());
410 EXPECT_FALSE(pc.HasExpires()); 415 EXPECT_FALSE(pc.HasExpires());
411 EXPECT_FALSE(pc.HasMaxAge()); 416 EXPECT_FALSE(pc.HasMaxAge());
412 EXPECT_FALSE(pc.IsSecure()); 417 EXPECT_FALSE(pc.IsSecure());
413 EXPECT_FALSE(pc.IsHttpOnly()); 418 EXPECT_FALSE(pc.IsHttpOnly());
414 EXPECT_EQ(CookieSameSite::NO_RESTRICTION, pc.SameSite()); 419 EXPECT_EQ(CookieSameSite::NO_RESTRICTION, pc.SameSite());
415 EXPECT_EQ("name2=value2", pc.ToCookieLine()); 420 EXPECT_EQ("name2=value2", pc.ToCookieLine());
416 } 421 }
417 422
423 // Set the domain attribute twice in a cookie line. If the second attribute's
424 // value is empty, it shoud be ignored.
425 //
426 // This is de facto standard behavior, per https://crbug.com/601786.
427 TEST(ParsedCookieTest, MultipleDomainAttributes) {
428 ParsedCookie pc1("name=value; domain=foo.com; domain=bar.com");
429 EXPECT_EQ("bar.com", pc1.Domain());
430 ParsedCookie pc2("name=value; domain=foo.com; domain=");
431 EXPECT_EQ("foo.com", pc2.Domain());
432 }
433
418 TEST(ParsedCookieTest, SetPriority) { 434 TEST(ParsedCookieTest, SetPriority) {
419 ParsedCookie pc("name=value"); 435 ParsedCookie pc("name=value");
420 EXPECT_TRUE(pc.IsValid()); 436 EXPECT_TRUE(pc.IsValid());
421 437
422 EXPECT_EQ("name=value", pc.ToCookieLine()); 438 EXPECT_EQ("name=value", pc.ToCookieLine());
423 EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority()); 439 EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority());
424 440
425 // Test each priority, expect case-insensitive compare. 441 // Test each priority, expect case-insensitive compare.
426 EXPECT_TRUE(pc.SetPriority("high")); 442 EXPECT_TRUE(pc.SetPriority("high"));
427 EXPECT_EQ("name=value; priority=high", pc.ToCookieLine()); 443 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()); 610 EXPECT_TRUE(pc5.IsValid());
595 EXPECT_EQ(pc5_literal, pc5.ToCookieLine()); 611 EXPECT_EQ(pc5_literal, pc5.ToCookieLine());
596 EXPECT_TRUE(pc6.IsValid()); 612 EXPECT_TRUE(pc6.IsValid());
597 EXPECT_EQ(pc6_literal, pc6.ToCookieLine()); 613 EXPECT_EQ(pc6_literal, pc6.ToCookieLine());
598 EXPECT_TRUE(pc7.IsValid()); 614 EXPECT_TRUE(pc7.IsValid());
599 EXPECT_EQ(pc7_literal, pc7.ToCookieLine()); 615 EXPECT_EQ(pc7_literal, pc7.ToCookieLine());
600 EXPECT_TRUE(pc8.IsValid()); 616 EXPECT_TRUE(pc8.IsValid());
601 EXPECT_EQ(pc8_literal, pc8.ToCookieLine()); 617 EXPECT_EQ(pc8_literal, pc8.ToCookieLine());
602 } 618 }
603 } 619 }
OLDNEW
« net/cookies/parsed_cookie.cc ('K') | « net/cookies/parsed_cookie.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698