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-9999999999 20:49:07 GMT", false, 0}, | 129 { "Thu, 12-Aug-31841 20:49:07 GMT", false, 0 }, |
130 {"Thu, 999999999999-Aug-2007 20:49:07 GMT", false, 0}, | 130 { "Thu, 12-Aug-9999999999 20:49:07 GMT", false, 0 }, |
131 {"Thu, 12-Aug-2007 20:61:99999999999 GMT", false, 0}, | 131 { "Thu, 999999999999-Aug-2007 20:49:07 GMT", false, 0 }, |
132 {"IAintNoDateFool", false, 0}, | 132 { "Thu, 12-Aug-2007 20:61:99999999999 GMT", false, 0 }, |
133 {"1600 April 33 21:01:22", false, 0}, | 133 { "IAintNoDateFool", false, 0 }, |
134 {"1970 April 33 21:01:22", false, 0}, | |
135 {"Thu, 33-Aug-31841 20:49:07 GMT", false, 0}, | |
136 }; | 134 }; |
137 | 135 |
138 base::Time parsed_time; | 136 base::Time parsed_time; |
139 for (size_t i = 0; i < arraysize(tests); ++i) { | 137 for (size_t i = 0; i < arraysize(tests); ++i) { |
140 parsed_time = cookie_util::ParseCookieExpirationTime(tests[i].str); | 138 parsed_time = cookie_util::ParseCookieTime(tests[i].str); |
141 if (!tests[i].valid) { | 139 if (!tests[i].valid) { |
142 EXPECT_TRUE(parsed_time.is_null()) << tests[i].str; | 140 EXPECT_TRUE(parsed_time.is_null()) << tests[i].str; |
143 continue; | 141 continue; |
144 } | 142 } |
145 EXPECT_TRUE(!parsed_time.is_null()) << tests[i].str; | 143 EXPECT_TRUE(!parsed_time.is_null()) << tests[i].str; |
146 EXPECT_EQ(tests[i].epoch, parsed_time.ToTimeT()) << tests[i].str; | 144 EXPECT_EQ(tests[i].epoch, parsed_time.ToTimeT()) << tests[i].str; |
147 } | 145 } |
148 } | 146 } |
149 | 147 |
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). | |
154 TEST(CookieUtilTest, ParseCookieExpirationTimeBeyond2038) { | |
155 const char* kTests[] = { | |
156 "Thu, 12-Aug-31841 20:49:07 GMT", "2039 April 15 21:01:22", | |
157 "2039 April 15 21:01:22", "2038 April 15 21:01:22", | |
158 }; | |
159 | |
160 for (const auto& test : kTests) { | |
161 base::Time parsed_time = cookie_util::ParseCookieExpirationTime(test); | |
162 EXPECT_FALSE(parsed_time.is_null()); | |
163 | |
164 // It should either have an exact value, or the maximum value that | |
165 // Time::FromeUTCExploded() can return on the current platform. 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 base::Time almost_jan_32000 = | |
172 base::Time::UnixEpoch() + base::TimeDelta::FromDays(365 * 32030); | |
173 EXPECT_LT(parsed_time, almost_jan_32000); | |
174 } | |
175 } | |
176 | |
177 // Tests parsing dates that are prior to (or around) 1970. POSIX systems are | |
178 // incapable of doing this, however the expectation is for cookie parsing to | |
179 // succeed anyway (and return a minimal base::Time). | |
180 TEST(CookieUtilTest, ParseCookieExpirationTimeBefore1970) { | |
181 const char* kTests[] = { | |
182 // The unix epoch. | |
183 "1970 Jan 1 00:00:00", | |
184 // The windows epoch. | |
185 "1601 Jan 1 00:00:00", | |
186 // Other dates. | |
187 "1969 March 3 21:01:22", "1600 April 15 21:01:22", | |
188 }; | |
189 | |
190 for (const auto& test : kTests) { | |
191 base::Time parsed_time = cookie_util::ParseCookieExpirationTime(test); | |
192 EXPECT_FALSE(parsed_time.is_null()); | |
193 | |
194 // It should either have an exact value, or should be base::Time(1) | |
195 // For simplicity just check that it is less than the unix epoch. | |
196 EXPECT_LE(parsed_time, base::Time::UnixEpoch()); | |
197 } | |
198 } | |
199 | |
200 TEST(CookieUtilTest, TestRequestCookieParsing) { | 148 TEST(CookieUtilTest, TestRequestCookieParsing) { |
201 std::vector<RequestCookieParsingTest> tests; | 149 std::vector<RequestCookieParsingTest> tests; |
202 | 150 |
203 // Simple case. | 151 // Simple case. |
204 tests.push_back(RequestCookieParsingTest()); | 152 tests.push_back(RequestCookieParsingTest()); |
205 tests.back().str = "key=value"; | 153 tests.back().str = "key=value"; |
206 tests.back().parsed.push_back(std::make_pair(std::string("key"), | 154 tests.back().parsed.push_back(std::make_pair(std::string("key"), |
207 std::string("value"))); | 155 std::string("value"))); |
208 // Multiple key/value pairs. | 156 // Multiple key/value pairs. |
209 tests.push_back(RequestCookieParsingTest()); | 157 tests.push_back(RequestCookieParsingTest()); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 cookie_util::GetEffectiveDomain("ws", "www.example.com")); | 202 cookie_util::GetEffectiveDomain("ws", "www.example.com")); |
255 EXPECT_EQ("example.com", | 203 EXPECT_EQ("example.com", |
256 cookie_util::GetEffectiveDomain("wss", "www.example.com")); | 204 cookie_util::GetEffectiveDomain("wss", "www.example.com")); |
257 EXPECT_EQ("www.example.com", | 205 EXPECT_EQ("www.example.com", |
258 cookie_util::GetEffectiveDomain("ftp", "www.example.com")); | 206 cookie_util::GetEffectiveDomain("ftp", "www.example.com")); |
259 } | 207 } |
260 | 208 |
261 } // namespace | 209 } // namespace |
262 | 210 |
263 } // namespace net | 211 } // namespace net |
OLD | NEW |