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 3// found in the LICENSE file. 4 5#include "base/basictypes.h" 6#include "net/cookies/cookie_util.h" 7#include "testing/gtest/include/gtest/gtest.h" 8 9TEST(CookieUtilTest, TestDomainIsHostOnly) { 10 const struct { 11 const char* str; 12 const bool is_host_only; 13 } tests[] = { 14 { "", true }, 15 { "www.google.com", true }, 16 { ".google.com", false } 17 }; 18 19 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 20 EXPECT_EQ(tests[i].is_host_only, 21 net::cookie_util::DomainIsHostOnly(tests[i].str)); 22 } 23} 24 25TEST(CookieUtilTest, TestCookieDateParsing) { 26 const struct { 27 const char* str; 28 const bool valid; 29 const time_t epoch; 30 } tests[] = { 31 { "Sat, 15-Apr-17 21:01:22 GMT", true, 1492290082 }, 32 { "Thu, 19-Apr-2007 16:00:00 GMT", true, 1176998400 }, 33 { "Wed, 25 Apr 2007 21:02:13 GMT", true, 1177534933 }, 34 { "Thu, 19/Apr\\2007 16:00:00 GMT", true, 1176998400 }, 35 { "Fri, 1 Jan 2010 01:01:50 GMT", true, 1262307710 }, 36 { "Wednesday, 1-Jan-2003 00:00:00 GMT", true, 1041379200 }, 37 { ", 1-Jan-2003 00:00:00 GMT", true, 1041379200 }, 38 { " 1-Jan-2003 00:00:00 GMT", true, 1041379200 }, 39 { "1-Jan-2003 00:00:00 GMT", true, 1041379200 }, 40 { "Wed,18-Apr-07 22:50:12 GMT", true, 1176936612 }, 41 { "WillyWonka , 18-Apr-07 22:50:12 GMT", true, 1176936612 }, 42 { "WillyWonka , 18-Apr-07 22:50:12", true, 1176936612 }, 43 { "WillyWonka , 18-apr-07 22:50:12", true, 1176936612 }, 44 { "Mon, 18-Apr-1977 22:50:13 GMT", true, 230251813 }, 45 { "Mon, 18-Apr-77 22:50:13 GMT", true, 230251813 }, 46 // If the cookie came in with the expiration quoted (which in terms of 47 // the RFC you shouldn't do), we will get string quoted. Bug 1261605. 48 { "\"Sat, 15-Apr-17\\\"21:01:22\\\"GMT\"", true, 1492290082 }, 49 // Test with full month names and partial names. 50 { "Partyday, 18- April-07 22:50:12", true, 1176936612 }, 51 { "Partyday, 18 - Apri-07 22:50:12", true, 1176936612 }, 52 { "Wednes, 1-Januar-2003 00:00:00 GMT", true, 1041379200 }, 53 // Test that we always take GMT even with other time zones or bogus 54 // values. The RFC says everything should be GMT, and in the worst case 55 // we are 24 hours off because of zone issues. 56 { "Sat, 15-Apr-17 21:01:22", true, 1492290082 }, 57 { "Sat, 15-Apr-17 21:01:22 GMT-2", true, 1492290082 }, 58 { "Sat, 15-Apr-17 21:01:22 GMT BLAH", true, 1492290082 }, 59 { "Sat, 15-Apr-17 21:01:22 GMT-0400", true, 1492290082 }, 60 { "Sat, 15-Apr-17 21:01:22 GMT-0400 (EDT)",true, 1492290082 }, 61 { "Sat, 15-Apr-17 21:01:22 DST", true, 1492290082 }, 62 { "Sat, 15-Apr-17 21:01:22 -0400", true, 1492290082 }, 63 { "Sat, 15-Apr-17 21:01:22 (hello there)", true, 1492290082 }, 64 // Test that if we encounter multiple : fields, that we take the first 65 // that correctly parses. 66 { "Sat, 15-Apr-17 21:01:22 11:22:33", true, 1492290082 }, 67 { "Sat, 15-Apr-17 ::00 21:01:22", true, 1492290082 }, 68 { "Sat, 15-Apr-17 boink:z 21:01:22", true, 1492290082 }, 69 // We take the first, which in this case is invalid. 70 { "Sat, 15-Apr-17 91:22:33 21:01:22", false, 0 }, 71 // amazon.com formats their cookie expiration like this. 72 { "Thu Apr 18 22:50:12 2007 GMT", true, 1176936612 }, 73 // Test that hh:mm:ss can occur anywhere. 74 { "22:50:12 Thu Apr 18 2007 GMT", true, 1176936612 }, 75 { "Thu 22:50:12 Apr 18 2007 GMT", true, 1176936612 }, 76 { "Thu Apr 22:50:12 18 2007 GMT", true, 1176936612 }, 77 { "Thu Apr 18 22:50:12 2007 GMT", true, 1176936612 }, 78 { "Thu Apr 18 2007 22:50:12 GMT", true, 1176936612 }, 79 { "Thu Apr 18 2007 GMT 22:50:12", true, 1176936612 }, 80 // Test that the day and year can be anywhere if they are unambigious. 81 { "Sat, 15-Apr-17 21:01:22 GMT", true, 1492290082 }, 82 { "15-Sat, Apr-17 21:01:22 GMT", true, 1492290082 }, 83 { "15-Sat, Apr 21:01:22 GMT 17", true, 1492290082 }, 84 { "15-Sat, Apr 21:01:22 GMT 2017", true, 1492290082 }, 85 { "15 Apr 21:01:22 2017", true, 1492290082 }, 86 { "15 17 Apr 21:01:22", true, 1492290082 }, 87 { "Apr 15 17 21:01:22", true, 1492290082 }, 88 { "Apr 15 21:01:22 17", true, 1492290082 }, 89 { "2017 April 15 21:01:22", true, 1492290082 }, 90 { "15 April 2017 21:01:22", true, 1492290082 }, 91 // Some invalid dates 92 { "98 April 17 21:01:22", false, 0 }, 93 { "Thu, 012-Aug-2008 20:49:07 GMT", false, 0 }, 94 { "Thu, 12-Aug-31841 20:49:07 GMT", false, 0 }, 95 { "Thu, 12-Aug-9999999999 20:49:07 GMT", false, 0 }, 96 { "Thu, 999999999999-Aug-2007 20:49:07 GMT", false, 0 }, 97 { "Thu, 12-Aug-2007 20:61:99999999999 GMT", false, 0 }, 98 { "IAintNoDateFool", false, 0 }, 99 }; 100 101 base::Time parsed_time; 102 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 103 parsed_time = net::cookie_util::ParseCookieTime(tests[i].str); 104 if (!tests[i].valid) { 105 EXPECT_FALSE(!parsed_time.is_null()) << tests[i].str; 106 continue; 107 } 108 EXPECT_TRUE(!parsed_time.is_null()) << tests[i].str; 109 EXPECT_EQ(tests[i].epoch, parsed_time.ToTimeT()) << tests[i].str; 110 } 111} 112