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