Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: net/cookies/cookie_util_unittest.cc

Issue 2358343004: When parsing cookie expiration times, clip out of range dates rather
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/cookies/cookie_util.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 },
130 { "Thu, 12-Aug-9999999999 20:49:07 GMT", false, 0 }, 129 { "Thu, 12-Aug-9999999999 20:49:07 GMT", false, 0 },
131 { "Thu, 999999999999-Aug-2007 20:49:07 GMT", false, 0 }, 130 { "Thu, 999999999999-Aug-2007 20:49:07 GMT", false, 0 },
132 { "Thu, 12-Aug-2007 20:61:99999999999 GMT", false, 0 }, 131 { "Thu, 12-Aug-2007 20:61:99999999999 GMT", false, 0 },
133 { "IAintNoDateFool", false, 0 }, 132 { "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 POSIX systems are incapable
151 // of doing this, however the expectation is for cookie parsing to succeed
152 // anyway (and return base::Time::Max()).
153 TEST(CookieUtilTest, ParseCookieExpirationTimeBeyond2038) {
154 const char* kTests[] = {
155 "Thu, 12-Aug-31841 20:49:07 GMT",
156 "2039 April 15 21:01:22",
157 "2039 April 15 21:01:22",
158 "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 should be base::Time::Max().
166 // For simplicity just check that it is greater than an arbitray date.
167 base::Time jan_2038 =
168 base::Time::UnixEpoch() + base::TimeDelta::FromDays(365 * 68);
169 EXPECT_GT(parsed_time, jan_2038);
170 }
171 }
172
173 // Tests parsing dates that are prior to (or around) 1970. POSIX systems are
174 // incapable of doing this, however the expectation is for cookie parsing to
175 // succeed
176 // anyway (and return a minimal base::Time).
177 TEST(CookieUtilTest, ParseCookieExpirationTimeBefore1970) {
178 const char* kTests[] = {
179 // The unix epoch.
180 "1970 Jan 1 00:00:00",
181 // The windows epoch.
182 "1601 Jan 1 00:00:00",
183 // Other dates.
184 "1969 March 3 21:01:22", "1600 April 15 21:01:22",
185 };
186
187 for (const auto& test : kTests) {
188 base::Time parsed_time = cookie_util::ParseCookieExpirationTime(test);
189 EXPECT_FALSE(parsed_time.is_null());
190
191 // It should either have an exact value, or should be base::Time(1)
192 // For simplicity just check that it is less than the unix epoch.
193 EXPECT_LT(parsed_time, base::Time::UnixEpoch());
194 }
195 }
196
148 TEST(CookieUtilTest, TestRequestCookieParsing) { 197 TEST(CookieUtilTest, TestRequestCookieParsing) {
149 std::vector<RequestCookieParsingTest> tests; 198 std::vector<RequestCookieParsingTest> tests;
150 199
151 // Simple case. 200 // Simple case.
152 tests.push_back(RequestCookieParsingTest()); 201 tests.push_back(RequestCookieParsingTest());
153 tests.back().str = "key=value"; 202 tests.back().str = "key=value";
154 tests.back().parsed.push_back(std::make_pair(std::string("key"), 203 tests.back().parsed.push_back(std::make_pair(std::string("key"),
155 std::string("value"))); 204 std::string("value")));
156 // Multiple key/value pairs. 205 // Multiple key/value pairs.
157 tests.push_back(RequestCookieParsingTest()); 206 tests.push_back(RequestCookieParsingTest());
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 cookie_util::GetEffectiveDomain("ws", "www.example.com")); 251 cookie_util::GetEffectiveDomain("ws", "www.example.com"));
203 EXPECT_EQ("example.com", 252 EXPECT_EQ("example.com",
204 cookie_util::GetEffectiveDomain("wss", "www.example.com")); 253 cookie_util::GetEffectiveDomain("wss", "www.example.com"));
205 EXPECT_EQ("www.example.com", 254 EXPECT_EQ("www.example.com",
206 cookie_util::GetEffectiveDomain("ftp", "www.example.com")); 255 cookie_util::GetEffectiveDomain("ftp", "www.example.com"));
207 } 256 }
208 257
209 } // namespace 258 } // namespace
210 259
211 } // namespace net 260 } // namespace net
OLDNEW
« no previous file with comments | « net/cookies/cookie_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698