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 <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
9 #include "net/cookies/cookie_util.h" | 9 #include "net/cookies/cookie_util.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 cookie_util::DomainIsHostOnly(tests[i].str)); | 56 cookie_util::DomainIsHostOnly(tests[i].str)); |
57 } | 57 } |
58 } | 58 } |
59 | 59 |
60 TEST(CookieUtilTest, TestCookieDateParsing) { | 60 TEST(CookieUtilTest, TestCookieDateParsing) { |
61 const struct { | 61 const struct { |
62 const char* str; | 62 const char* str; |
63 const bool valid; | 63 const bool valid; |
64 const time_t epoch; | 64 const time_t epoch; |
65 } tests[] = { | 65 } tests[] = { |
66 { "Sat, 15-Apr-17 21:01:22 GMT", true, 1492290082 }, | 66 {"Sat, 15-Apr-17 21:01:22 GMT", true, 1492290082}, |
67 { "Thu, 19-Apr-2007 16:00:00 GMT", true, 1176998400 }, | 67 {"Thu, 19-Apr-2007 16:00:00 GMT", true, 1176998400}, |
68 { "Wed, 25 Apr 2007 21:02:13 GMT", true, 1177534933 }, | 68 {"Wed, 25 Apr 2007 21:02:13 GMT", true, 1177534933}, |
69 { "Thu, 19/Apr\\2007 16:00:00 GMT", true, 1176998400 }, | 69 {"Thu, 19/Apr\\2007 16:00:00 GMT", true, 1176998400}, |
70 { "Fri, 1 Jan 2010 01:01:50 GMT", true, 1262307710 }, | 70 {"Fri, 1 Jan 2010 01:01:50 GMT", true, 1262307710}, |
71 { "Wednesday, 1-Jan-2003 00:00:00 GMT", true, 1041379200 }, | 71 {"Wednesday, 1-Jan-2003 00:00:00 GMT", true, 1041379200}, |
72 { ", 1-Jan-2003 00:00:00 GMT", true, 1041379200 }, | 72 {", 1-Jan-2003 00:00:00 GMT", true, 1041379200}, |
73 { " 1-Jan-2003 00:00:00 GMT", true, 1041379200 }, | 73 {" 1-Jan-2003 00:00:00 GMT", true, 1041379200}, |
74 { "1-Jan-2003 00:00:00 GMT", true, 1041379200 }, | 74 {"1-Jan-2003 00:00:00 GMT", true, 1041379200}, |
75 { "Wed,18-Apr-07 22:50:12 GMT", true, 1176936612 }, | 75 {"Wed,18-Apr-07 22:50:12 GMT", true, 1176936612}, |
76 { "WillyWonka , 18-Apr-07 22:50:12 GMT", true, 1176936612 }, | 76 {"WillyWonka , 18-Apr-07 22:50:12 GMT", true, 1176936612}, |
77 { "WillyWonka , 18-Apr-07 22:50:12", true, 1176936612 }, | 77 {"WillyWonka , 18-Apr-07 22:50:12", true, 1176936612}, |
78 { "WillyWonka , 18-apr-07 22:50:12", true, 1176936612 }, | 78 {"WillyWonka , 18-apr-07 22:50:12", true, 1176936612}, |
79 { "Mon, 18-Apr-1977 22:50:13 GMT", true, 230251813 }, | 79 {"Mon, 18-Apr-1977 22:50:13 GMT", true, 230251813}, |
80 { "Mon, 18-Apr-77 22:50:13 GMT", true, 230251813 }, | 80 {"Mon, 18-Apr-77 22:50:13 GMT", true, 230251813}, |
81 // If the cookie came in with the expiration quoted (which in terms of | 81 // If the cookie came in with the expiration quoted (which in terms of |
82 // the RFC you shouldn't do), we will get string quoted. Bug 1261605. | 82 // the RFC you shouldn't do), we will get string quoted. Bug 1261605. |
83 { "\"Sat, 15-Apr-17\\\"21:01:22\\\"GMT\"", true, 1492290082 }, | 83 {"\"Sat, 15-Apr-17\\\"21:01:22\\\"GMT\"", true, 1492290082}, |
84 // Test with full month names and partial names. | 84 // Test with full month names and partial names. |
85 { "Partyday, 18- April-07 22:50:12", true, 1176936612 }, | 85 {"Partyday, 18- April-07 22:50:12", true, 1176936612}, |
86 { "Partyday, 18 - Apri-07 22:50:12", true, 1176936612 }, | 86 {"Partyday, 18 - Apri-07 22:50:12", true, 1176936612}, |
87 { "Wednes, 1-Januar-2003 00:00:00 GMT", true, 1041379200 }, | 87 {"Wednes, 1-Januar-2003 00:00:00 GMT", true, 1041379200}, |
88 // Test that we always take GMT even with other time zones or bogus | 88 // Test that we always take GMT even with other time zones or bogus |
89 // values. The RFC says everything should be GMT, and in the worst case | 89 // values. The RFC says everything should be GMT, and in the worst case |
90 // we are 24 hours off because of zone issues. | 90 // we are 24 hours off because of zone issues. |
91 { "Sat, 15-Apr-17 21:01:22", true, 1492290082 }, | 91 {"Sat, 15-Apr-17 21:01:22", true, 1492290082}, |
92 { "Sat, 15-Apr-17 21:01:22 GMT-2", true, 1492290082 }, | 92 {"Sat, 15-Apr-17 21:01:22 GMT-2", true, 1492290082}, |
93 { "Sat, 15-Apr-17 21:01:22 GMT BLAH", true, 1492290082 }, | 93 {"Sat, 15-Apr-17 21:01:22 GMT BLAH", true, 1492290082}, |
94 { "Sat, 15-Apr-17 21:01:22 GMT-0400", true, 1492290082 }, | 94 {"Sat, 15-Apr-17 21:01:22 GMT-0400", true, 1492290082}, |
95 { "Sat, 15-Apr-17 21:01:22 GMT-0400 (EDT)",true, 1492290082 }, | 95 {"Sat, 15-Apr-17 21:01:22 GMT-0400 (EDT)", true, 1492290082}, |
96 { "Sat, 15-Apr-17 21:01:22 DST", true, 1492290082 }, | 96 {"Sat, 15-Apr-17 21:01:22 DST", true, 1492290082}, |
97 { "Sat, 15-Apr-17 21:01:22 -0400", true, 1492290082 }, | 97 {"Sat, 15-Apr-17 21:01:22 -0400", true, 1492290082}, |
98 { "Sat, 15-Apr-17 21:01:22 (hello there)", true, 1492290082 }, | 98 {"Sat, 15-Apr-17 21:01:22 (hello there)", true, 1492290082}, |
99 // Test that if we encounter multiple : fields, that we take the first | 99 // Test that if we encounter multiple : fields, that we take the first |
100 // that correctly parses. | 100 // that correctly parses. |
101 { "Sat, 15-Apr-17 21:01:22 11:22:33", true, 1492290082 }, | 101 {"Sat, 15-Apr-17 21:01:22 11:22:33", true, 1492290082}, |
102 { "Sat, 15-Apr-17 ::00 21:01:22", true, 1492290082 }, | 102 {"Sat, 15-Apr-17 ::00 21:01:22", true, 1492290082}, |
103 { "Sat, 15-Apr-17 boink:z 21:01:22", true, 1492290082 }, | 103 {"Sat, 15-Apr-17 boink:z 21:01:22", true, 1492290082}, |
104 // We take the first, which in this case is invalid. | 104 // We take the first, which in this case is invalid. |
105 { "Sat, 15-Apr-17 91:22:33 21:01:22", false, 0 }, | 105 {"Sat, 15-Apr-17 91:22:33 21:01:22", false, 0}, |
106 // amazon.com formats their cookie expiration like this. | 106 // amazon.com formats their cookie expiration like this. |
107 { "Thu Apr 18 22:50:12 2007 GMT", true, 1176936612 }, | 107 {"Thu Apr 18 22:50:12 2007 GMT", true, 1176936612}, |
108 // Test that hh:mm:ss can occur anywhere. | 108 // Test that hh:mm:ss can occur anywhere. |
109 { "22:50:12 Thu Apr 18 2007 GMT", true, 1176936612 }, | 109 {"22:50:12 Thu Apr 18 2007 GMT", true, 1176936612}, |
110 { "Thu 22:50:12 Apr 18 2007 GMT", true, 1176936612 }, | 110 {"Thu 22:50:12 Apr 18 2007 GMT", true, 1176936612}, |
111 { "Thu Apr 22:50:12 18 2007 GMT", true, 1176936612 }, | 111 {"Thu Apr 22:50:12 18 2007 GMT", true, 1176936612}, |
112 { "Thu Apr 18 22:50:12 2007 GMT", true, 1176936612 }, | 112 {"Thu Apr 18 22:50:12 2007 GMT", true, 1176936612}, |
113 { "Thu Apr 18 2007 22:50:12 GMT", true, 1176936612 }, | 113 {"Thu Apr 18 2007 22:50:12 GMT", true, 1176936612}, |
114 { "Thu Apr 18 2007 GMT 22:50:12", true, 1176936612 }, | 114 {"Thu Apr 18 2007 GMT 22:50:12", true, 1176936612}, |
115 // Test that the day and year can be anywhere if they are unambigious. | 115 // Test that the day and year can be anywhere if they are unambigious. |
116 { "Sat, 15-Apr-17 21:01:22 GMT", true, 1492290082 }, | 116 {"Sat, 15-Apr-17 21:01:22 GMT", true, 1492290082}, |
117 { "15-Sat, Apr-17 21:01:22 GMT", true, 1492290082 }, | 117 {"15-Sat, Apr-17 21:01:22 GMT", true, 1492290082}, |
118 { "15-Sat, Apr 21:01:22 GMT 17", true, 1492290082 }, | 118 {"15-Sat, Apr 21:01:22 GMT 17", true, 1492290082}, |
119 { "15-Sat, Apr 21:01:22 GMT 2017", true, 1492290082 }, | 119 {"15-Sat, Apr 21:01:22 GMT 2017", true, 1492290082}, |
120 { "15 Apr 21:01:22 2017", true, 1492290082 }, | 120 {"15 Apr 21:01:22 2017", true, 1492290082}, |
121 { "15 17 Apr 21:01:22", true, 1492290082 }, | 121 {"15 17 Apr 21:01:22", true, 1492290082}, |
122 { "Apr 15 17 21:01:22", true, 1492290082 }, | 122 {"Apr 15 17 21:01:22", true, 1492290082}, |
123 { "Apr 15 21:01:22 17", true, 1492290082 }, | 123 {"Apr 15 21:01:22 17", true, 1492290082}, |
124 { "2017 April 15 21:01:22", true, 1492290082 }, | 124 {"2017 April 15 21:01:22", true, 1492290082}, |
125 { "15 April 2017 21:01:22", true, 1492290082 }, | 125 {"15 April 2017 21:01:22", true, 1492290082}, |
126 // Some invalid dates | 126 // Some invalid dates |
127 { "98 April 17 21:01:22", false, 0 }, | 127 {"98 April 17 21:01:22", false, 0}, |
128 { "Thu, 012-Aug-2008 20:49:07 GMT", false, 0 }, | 128 {"Thu, 012-Aug-2008 20:49:07 GMT", false, 0}, |
129 { "Thu, 12-Aug-31841 20:49:07 GMT", false, 0 }, | 129 {"Thu, 12-Aug-9999999999 20:49:07 GMT", false, 0}, |
130 { "Thu, 12-Aug-9999999999 20:49:07 GMT", false, 0 }, | 130 {"Thu, 999999999999-Aug-2007 20:49:07 GMT", false, 0}, |
131 { "Thu, 999999999999-Aug-2007 20:49:07 GMT", false, 0 }, | 131 {"Thu, 12-Aug-2007 20:61:99999999999 GMT", false, 0}, |
132 { "Thu, 12-Aug-2007 20:61:99999999999 GMT", false, 0 }, | 132 {"IAintNoDateFool", false, 0}, |
133 { "IAintNoDateFool", false, 0 }, | 133 {"1600 April 33 21:01:22", false, 0}, |
| 134 {"1970 April 33 21:01:22", false, 0}, |
| 135 {"Thu, 33-Aug-31841 20:49:07 GMT", false, 0}, |
134 }; | 136 }; |
135 | 137 |
136 base::Time parsed_time; | 138 base::Time parsed_time; |
137 for (size_t i = 0; i < arraysize(tests); ++i) { | 139 for (size_t i = 0; i < arraysize(tests); ++i) { |
138 parsed_time = cookie_util::ParseCookieTime(tests[i].str); | 140 parsed_time = cookie_util::ParseCookieExpirationTime(tests[i].str); |
139 if (!tests[i].valid) { | 141 if (!tests[i].valid) { |
140 EXPECT_TRUE(parsed_time.is_null()) << tests[i].str; | 142 EXPECT_TRUE(parsed_time.is_null()) << tests[i].str; |
141 continue; | 143 continue; |
142 } | 144 } |
143 EXPECT_TRUE(!parsed_time.is_null()) << tests[i].str; | 145 EXPECT_TRUE(!parsed_time.is_null()) << tests[i].str; |
144 EXPECT_EQ(tests[i].epoch, parsed_time.ToTimeT()) << tests[i].str; | 146 EXPECT_EQ(tests[i].epoch, parsed_time.ToTimeT()) << tests[i].str; |
145 } | 147 } |
146 } | 148 } |
147 | 149 |
| 150 // Tests parsing dates that are beyond 2038. 32-bit (non-Mac) POSIX systems are |
| 151 // incapable of doing this, however the expectation is for cookie parsing to |
| 152 // succeed anyway (and return the minimum value Time::FromUTCExploded() can |
| 153 // parse on the current platform). Also checks a date outside the limit on |
| 154 // Windows, which is year 30827. |
| 155 TEST(CookieUtilTest, ParseCookieExpirationTimeBeyond2038) { |
| 156 const char* kTests[] = { |
| 157 "Thu, 12-Aug-31841 20:49:07 GMT", "2039 April 15 21:01:22", |
| 158 "2039 April 15 21:01:22", "2038 April 15 21:01:22", |
| 159 }; |
| 160 |
| 161 for (const auto& test : kTests) { |
| 162 base::Time parsed_time = cookie_util::ParseCookieExpirationTime(test); |
| 163 EXPECT_FALSE(parsed_time.is_null()); |
| 164 |
| 165 // It should either have an exact value, or be base::Time::Max(). For |
| 166 // simplicity just check that it is greater than an arbitray date. |
| 167 base::Time almost_jan_2038 = |
| 168 base::Time::UnixEpoch() + base::TimeDelta::FromDays(365 * 68); |
| 169 EXPECT_LT(almost_jan_2038, parsed_time); |
| 170 } |
| 171 } |
| 172 |
| 173 // Tests parsing dates that are prior to (or around) 1970. Non-Mac POSIX systems |
| 174 // are incapable of doing this, however the expectation is for cookie parsing to |
| 175 // succeed anyway (and return a minimal base::Time). |
| 176 TEST(CookieUtilTest, ParseCookieExpirationTimeBefore1970) { |
| 177 const char* kTests[] = { |
| 178 // The unix epoch. |
| 179 "1970 Jan 1 00:00:00", |
| 180 // The windows epoch. |
| 181 "1601 Jan 1 00:00:00", |
| 182 // Other dates. |
| 183 "1969 March 3 21:01:22", "1600 April 15 21:01:22", |
| 184 }; |
| 185 |
| 186 for (const auto& test : kTests) { |
| 187 base::Time parsed_time = cookie_util::ParseCookieExpirationTime(test); |
| 188 EXPECT_FALSE(parsed_time.is_null()); |
| 189 |
| 190 // It should either have an exact value, or should be base::Time(1) |
| 191 // For simplicity just check that it is less than the unix epoch. |
| 192 EXPECT_LE(parsed_time, base::Time::UnixEpoch()); |
| 193 } |
| 194 } |
| 195 |
148 TEST(CookieUtilTest, TestRequestCookieParsing) { | 196 TEST(CookieUtilTest, TestRequestCookieParsing) { |
149 std::vector<RequestCookieParsingTest> tests; | 197 std::vector<RequestCookieParsingTest> tests; |
150 | 198 |
151 // Simple case. | 199 // Simple case. |
152 tests.push_back(RequestCookieParsingTest()); | 200 tests.push_back(RequestCookieParsingTest()); |
153 tests.back().str = "key=value"; | 201 tests.back().str = "key=value"; |
154 tests.back().parsed.push_back(std::make_pair(std::string("key"), | 202 tests.back().parsed.push_back(std::make_pair(std::string("key"), |
155 std::string("value"))); | 203 std::string("value"))); |
156 // Multiple key/value pairs. | 204 // Multiple key/value pairs. |
157 tests.push_back(RequestCookieParsingTest()); | 205 tests.push_back(RequestCookieParsingTest()); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 cookie_util::GetEffectiveDomain("ws", "www.example.com")); | 250 cookie_util::GetEffectiveDomain("ws", "www.example.com")); |
203 EXPECT_EQ("example.com", | 251 EXPECT_EQ("example.com", |
204 cookie_util::GetEffectiveDomain("wss", "www.example.com")); | 252 cookie_util::GetEffectiveDomain("wss", "www.example.com")); |
205 EXPECT_EQ("www.example.com", | 253 EXPECT_EQ("www.example.com", |
206 cookie_util::GetEffectiveDomain("ftp", "www.example.com")); | 254 cookie_util::GetEffectiveDomain("ftp", "www.example.com")); |
207 } | 255 } |
208 | 256 |
209 } // namespace | 257 } // namespace |
210 | 258 |
211 } // namespace net | 259 } // namespace net |
OLD | NEW |