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 <string> | 5 #include <string> |
| 6 #include <time.h> |
6 | 7 |
7 #include "net/cookies/parsed_cookie.h" | 8 #include "net/cookies/parsed_cookie.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 namespace net { | 11 namespace net { |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 class ParsedCookieTest : public testing::Test { }; | 15 class ParsedCookieTest : public testing::Test { }; |
15 | 16 |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 EXPECT_EQ("hello", | 269 EXPECT_EQ("hello", |
269 ParsedCookie::ParseValueString("hello\nworld")); | 270 ParsedCookie::ParseValueString("hello\nworld")); |
270 EXPECT_EQ("fs!!@", | 271 EXPECT_EQ("fs!!@", |
271 ParsedCookie::ParseValueString("fs!!@;helloworld")); | 272 ParsedCookie::ParseValueString("fs!!@;helloworld")); |
272 EXPECT_EQ("hello world\tgood", | 273 EXPECT_EQ("hello world\tgood", |
273 ParsedCookie::ParseValueString("hello world\tgood\rbye")); | 274 ParsedCookie::ParseValueString("hello world\tgood\rbye")); |
274 EXPECT_EQ("A=B=C", | 275 EXPECT_EQ("A=B=C", |
275 ParsedCookie::ParseValueString("A=B=C;D=E")); | 276 ParsedCookie::ParseValueString("A=B=C;D=E")); |
276 } | 277 } |
277 | 278 |
| 279 TEST_F(ParsedCookieTest, TestCookieDateParsing) { |
| 280 const struct { |
| 281 const char* str; |
| 282 const bool valid; |
| 283 const time_t epoch; |
| 284 } tests[] = { |
| 285 { "Sat, 15-Apr-17 21:01:22 GMT", true, 1492290082 }, |
| 286 { "Thu, 19-Apr-2007 16:00:00 GMT", true, 1176998400 }, |
| 287 { "Wed, 25 Apr 2007 21:02:13 GMT", true, 1177534933 }, |
| 288 { "Thu, 19/Apr\\2007 16:00:00 GMT", true, 1176998400 }, |
| 289 { "Fri, 1 Jan 2010 01:01:50 GMT", true, 1262307710 }, |
| 290 { "Wednesday, 1-Jan-2003 00:00:00 GMT", true, 1041379200 }, |
| 291 { ", 1-Jan-2003 00:00:00 GMT", true, 1041379200 }, |
| 292 { " 1-Jan-2003 00:00:00 GMT", true, 1041379200 }, |
| 293 { "1-Jan-2003 00:00:00 GMT", true, 1041379200 }, |
| 294 { "Wed,18-Apr-07 22:50:12 GMT", true, 1176936612 }, |
| 295 { "WillyWonka , 18-Apr-07 22:50:12 GMT", true, 1176936612 }, |
| 296 { "WillyWonka , 18-Apr-07 22:50:12", true, 1176936612 }, |
| 297 { "WillyWonka , 18-apr-07 22:50:12", true, 1176936612 }, |
| 298 { "Mon, 18-Apr-1977 22:50:13 GMT", true, 230251813 }, |
| 299 { "Mon, 18-Apr-77 22:50:13 GMT", true, 230251813 }, |
| 300 // If the cookie came in with the expiration quoted (which in terms of |
| 301 // the RFC you shouldn't do), we will get string quoted. Bug 1261605. |
| 302 { "\"Sat, 15-Apr-17\\\"21:01:22\\\"GMT\"", true, 1492290082 }, |
| 303 // Test with full month names and partial names. |
| 304 { "Partyday, 18- April-07 22:50:12", true, 1176936612 }, |
| 305 { "Partyday, 18 - Apri-07 22:50:12", true, 1176936612 }, |
| 306 { "Wednes, 1-Januar-2003 00:00:00 GMT", true, 1041379200 }, |
| 307 // Test that we always take GMT even with other time zones or bogus |
| 308 // values. The RFC says everything should be GMT, and in the worst case |
| 309 // we are 24 hours off because of zone issues. |
| 310 { "Sat, 15-Apr-17 21:01:22", true, 1492290082 }, |
| 311 { "Sat, 15-Apr-17 21:01:22 GMT-2", true, 1492290082 }, |
| 312 { "Sat, 15-Apr-17 21:01:22 GMT BLAH", true, 1492290082 }, |
| 313 { "Sat, 15-Apr-17 21:01:22 GMT-0400", true, 1492290082 }, |
| 314 { "Sat, 15-Apr-17 21:01:22 GMT-0400 (EDT)",true, 1492290082 }, |
| 315 { "Sat, 15-Apr-17 21:01:22 DST", true, 1492290082 }, |
| 316 { "Sat, 15-Apr-17 21:01:22 -0400", true, 1492290082 }, |
| 317 { "Sat, 15-Apr-17 21:01:22 (hello there)", true, 1492290082 }, |
| 318 // Test that if we encounter multiple : fields, that we take the first |
| 319 // that correctly parses. |
| 320 { "Sat, 15-Apr-17 21:01:22 11:22:33", true, 1492290082 }, |
| 321 { "Sat, 15-Apr-17 ::00 21:01:22", true, 1492290082 }, |
| 322 { "Sat, 15-Apr-17 boink:z 21:01:22", true, 1492290082 }, |
| 323 // We take the first, which in this case is invalid. |
| 324 { "Sat, 15-Apr-17 91:22:33 21:01:22", false, 0 }, |
| 325 // amazon.com formats their cookie expiration like this. |
| 326 { "Thu Apr 18 22:50:12 2007 GMT", true, 1176936612 }, |
| 327 // Test that hh:mm:ss can occur anywhere. |
| 328 { "22:50:12 Thu Apr 18 2007 GMT", true, 1176936612 }, |
| 329 { "Thu 22:50:12 Apr 18 2007 GMT", true, 1176936612 }, |
| 330 { "Thu Apr 22:50:12 18 2007 GMT", true, 1176936612 }, |
| 331 { "Thu Apr 18 22:50:12 2007 GMT", true, 1176936612 }, |
| 332 { "Thu Apr 18 2007 22:50:12 GMT", true, 1176936612 }, |
| 333 { "Thu Apr 18 2007 GMT 22:50:12", true, 1176936612 }, |
| 334 // Test that the day and year can be anywhere if they are unambigious. |
| 335 { "Sat, 15-Apr-17 21:01:22 GMT", true, 1492290082 }, |
| 336 { "15-Sat, Apr-17 21:01:22 GMT", true, 1492290082 }, |
| 337 { "15-Sat, Apr 21:01:22 GMT 17", true, 1492290082 }, |
| 338 { "15-Sat, Apr 21:01:22 GMT 2017", true, 1492290082 }, |
| 339 { "15 Apr 21:01:22 2017", true, 1492290082 }, |
| 340 { "15 17 Apr 21:01:22", true, 1492290082 }, |
| 341 { "Apr 15 17 21:01:22", true, 1492290082 }, |
| 342 { "Apr 15 21:01:22 17", true, 1492290082 }, |
| 343 { "2017 April 15 21:01:22", true, 1492290082 }, |
| 344 { "15 April 2017 21:01:22", true, 1492290082 }, |
| 345 // Some invalid dates |
| 346 { "98 April 17 21:01:22", false, 0 }, |
| 347 { "Thu, 012-Aug-2008 20:49:07 GMT", false, 0 }, |
| 348 { "Thu, 12-Aug-31841 20:49:07 GMT", false, 0 }, |
| 349 { "Thu, 12-Aug-9999999999 20:49:07 GMT", false, 0 }, |
| 350 { "Thu, 999999999999-Aug-2007 20:49:07 GMT", false, 0 }, |
| 351 { "Thu, 12-Aug-2007 20:61:99999999999 GMT", false, 0 }, |
| 352 { "IAintNoDateFool", false, 0 }, |
| 353 }; |
| 354 |
| 355 base::Time parsed_time; |
| 356 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 357 parsed_time = ParsedCookie::ParseCookieTime(tests[i].str); |
| 358 if (!tests[i].valid) { |
| 359 EXPECT_FALSE(!parsed_time.is_null()) << tests[i].str; |
| 360 continue; |
| 361 } |
| 362 EXPECT_TRUE(!parsed_time.is_null()) << tests[i].str; |
| 363 EXPECT_EQ(tests[i].epoch, parsed_time.ToTimeT()) << tests[i].str; |
| 364 } |
| 365 } |
| 366 |
278 } // namespace net | 367 } // namespace net |
OLD | NEW |