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

Side by Side Diff: net/cert/internal/parse_ocsp_unittest.cc

Issue 2091103002: Add CheckOCSPDateValid() to net/cert/internal (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix compilation Created 4 years, 5 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "net/cert/internal/parse_ocsp.h" 5 #include "net/cert/internal/parse_ocsp.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "net/cert/internal/test_helpers.h" 9 #include "net/cert/internal/test_helpers.h"
10 #include "net/cert/x509_certificate.h" 10 #include "net/cert/x509_certificate.h"
11 #include "net/der/encode_values.h"
11 #include "net/test/test_data_directory.h" 12 #include "net/test/test_data_directory.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace net { 15 namespace net {
15 16
16 namespace { 17 namespace {
17 18
19 const base::TimeDelta kOCSPAgeOneWeek = base::TimeDelta::FromDays(7);
20
21 const base::Time kWindowsEpoch =
22 base::Time::UnixEpoch() -
23 base::TimeDelta::FromSeconds(INT64_C(11644473600));
Ryan Sleevi 2016/07/08 02:21:02 No need for the INT64_C now that we're C++11 (whic
dadrian 2016/07/08 22:25:42 Done.
24
18 std::string GetFilePath(const std::string& file_name) { 25 std::string GetFilePath(const std::string& file_name) {
19 return std::string("net/data/parse_ocsp_unittest/") + file_name; 26 return std::string("net/data/parse_ocsp_unittest/") + file_name;
20 } 27 }
21 28
22 enum OCSPFailure { 29 enum OCSPFailure {
23 OCSP_SUCCESS, 30 OCSP_SUCCESS,
24 PARSE_CERT, 31 PARSE_CERT,
25 PARSE_OCSP, 32 PARSE_OCSP,
26 OCSP_NOT_SUCCESSFUL, 33 OCSP_NOT_SUCCESSFUL,
27 PARSE_OCSP_DATA, 34 PARSE_OCSP_DATA,
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 } 182 }
176 183
177 TEST(ParseOCSPTest, OCSPOCSPSingleExtension) { 184 TEST(ParseOCSPTest, OCSPOCSPSingleExtension) {
178 ASSERT_EQ(OCSP_SUCCESS, ParseOCSP("has_single_extension.pem")); 185 ASSERT_EQ(OCSP_SUCCESS, ParseOCSP("has_single_extension.pem"));
179 } 186 }
180 187
181 TEST(ParseOCSPTest, OCSPMissingResponse) { 188 TEST(ParseOCSPTest, OCSPMissingResponse) {
182 ASSERT_EQ(PARSE_OCSP_SINGLE_RESPONSE, ParseOCSP("missing_response.pem")); 189 ASSERT_EQ(PARSE_OCSP_SINGLE_RESPONSE, ParseOCSP("missing_response.pem"));
183 } 190 }
184 191
192 TEST(OCSPDateTest, Valid) {
193 OCSPSingleResponse response;
194
195 base::Time now = base::Time::Now();
196 base::Time this_update = now - base::TimeDelta::FromHours(1);
197 ASSERT_TRUE(
198 der::EncodeTimeAsGeneralizedTime(this_update, &response.this_update));
199 response.has_next_update = false;
200 EXPECT_TRUE(CheckOCSPDateValid(response, now, kOCSPAgeOneWeek));
201
202 base::Time next_update = this_update + base::TimeDelta::FromDays(7);
203 ASSERT_TRUE(
204 der::EncodeTimeAsGeneralizedTime(next_update, &response.next_update));
205 response.has_next_update = true;
206 EXPECT_TRUE(CheckOCSPDateValid(response, now, kOCSPAgeOneWeek));
207 }
208
209 TEST(OCSPDateTest, ThisUpdateInTheFuture) {
210 OCSPSingleResponse response;
211
212 base::Time now = base::Time::Now();
213 base::Time this_update = now + base::TimeDelta::FromHours(1);
214 ASSERT_TRUE(
215 der::EncodeTimeAsGeneralizedTime(this_update, &response.this_update));
216 response.has_next_update = false;
217 EXPECT_FALSE(CheckOCSPDateValid(response, now, kOCSPAgeOneWeek));
218
219 base::Time next_update = this_update + base::TimeDelta::FromDays(7);
220 ASSERT_TRUE(
221 der::EncodeTimeAsGeneralizedTime(next_update, &response.next_update));
222 response.has_next_update = true;
223 EXPECT_FALSE(CheckOCSPDateValid(response, now, kOCSPAgeOneWeek));
224 }
225
226 TEST(OCSPDateTest, NextUpdatePassed) {
227 OCSPSingleResponse response;
228
229 base::Time now = base::Time::Now();
230 base::Time this_update = now - base::TimeDelta::FromDays(6);
231 ASSERT_TRUE(
232 der::EncodeTimeAsGeneralizedTime(this_update, &response.this_update));
233 response.has_next_update = false;
234 EXPECT_TRUE(CheckOCSPDateValid(response, now, kOCSPAgeOneWeek));
235
236 base::Time next_update = now - base::TimeDelta::FromHours(1);
237 ASSERT_TRUE(
238 der::EncodeTimeAsGeneralizedTime(next_update, &response.next_update));
239 response.has_next_update = true;
240 EXPECT_FALSE(CheckOCSPDateValid(response, now, kOCSPAgeOneWeek));
241 }
242
243 TEST(OCSPDateTest, NextUpdateBeforeThisUpdate) {
244 OCSPSingleResponse response;
245
246 base::Time now = base::Time::Now();
247 base::Time this_update = now - base::TimeDelta::FromDays(1);
248 ASSERT_TRUE(
249 der::EncodeTimeAsGeneralizedTime(this_update, &response.this_update));
250 response.has_next_update = false;
251 EXPECT_TRUE(CheckOCSPDateValid(response, now, kOCSPAgeOneWeek));
252
253 base::Time next_update = this_update - base::TimeDelta::FromDays(1);
254 ASSERT_TRUE(
255 der::EncodeTimeAsGeneralizedTime(next_update, &response.next_update));
256 response.has_next_update = true;
257 EXPECT_FALSE(CheckOCSPDateValid(response, now, kOCSPAgeOneWeek));
258 }
259
260 TEST(OCSPDateTest, ThisUpdateOlderThanMaxAge) {
261 OCSPSingleResponse response;
262
263 base::Time now = base::Time::Now();
264 base::Time this_update = now - kOCSPAgeOneWeek;
265 ASSERT_TRUE(
266 der::EncodeTimeAsGeneralizedTime(this_update, &response.this_update));
267 response.has_next_update = false;
268 EXPECT_TRUE(CheckOCSPDateValid(response, now, kOCSPAgeOneWeek));
269
270 base::Time next_update = now + base::TimeDelta::FromHours(1);
271 ASSERT_TRUE(
272 der::EncodeTimeAsGeneralizedTime(next_update, &response.next_update));
273 response.has_next_update = true;
274 EXPECT_TRUE(CheckOCSPDateValid(response, now, kOCSPAgeOneWeek));
275
276 ASSERT_TRUE(der::EncodeTimeAsGeneralizedTime(
277 this_update - base::TimeDelta::FromSeconds(1), &response.this_update));
278 response.has_next_update = false;
279 EXPECT_FALSE(CheckOCSPDateValid(response, now, kOCSPAgeOneWeek));
280 response.has_next_update = true;
281 EXPECT_FALSE(CheckOCSPDateValid(response, now, kOCSPAgeOneWeek));
282 }
283
284 TEST(OCSPDateTest, VerifyTimeFromBeforeWindowsEpoch) {
285 OCSPSingleResponse response;
286 base::Time verify_time = kWindowsEpoch - base::TimeDelta::FromDays(1);
Ryan Sleevi 2016/07/08 02:21:02 I think my concerns about Windows Epoch may have b
dadrian 2016/07/08 18:04:46 Since GeneralizedTime handles times from before th
287
288 base::Time now = base::Time::Now();
289 base::Time this_update = now - base::TimeDelta::FromHours(1);
290 ASSERT_TRUE(
291 der::EncodeTimeAsGeneralizedTime(this_update, &response.this_update));
292 response.has_next_update = false;
293 EXPECT_FALSE(CheckOCSPDateValid(response, verify_time, kOCSPAgeOneWeek));
294
295 base::Time next_update = this_update + kOCSPAgeOneWeek;
296 ASSERT_TRUE(
297 der::EncodeTimeAsGeneralizedTime(next_update, &response.next_update));
298 response.has_next_update = true;
299 EXPECT_FALSE(CheckOCSPDateValid(response, verify_time, kOCSPAgeOneWeek));
300 }
301
302 TEST(OCSPDateTest, VerifyTimeMinusAgeFromBeforeWindowsEpoch) {
303 OCSPSingleResponse response;
304 base::Time verify_time = kWindowsEpoch + base::TimeDelta::FromDays(1);
305
306 base::Time this_update = kWindowsEpoch;
307 ASSERT_TRUE(
308 der::EncodeTimeAsGeneralizedTime(this_update, &response.this_update));
309 response.has_next_update = false;
310 #ifdef OS_WIN
311 EXPECT_FALSE(CheckOCSPDateValid(response, verify_time, kOCSPAgeOneWeek));
312 #else
313 EXPECT_TRUE(CheckOCSPDateValid(response, verify_time, kOCSPAgeOneWeek));
314 #endif
315 }
316
185 } // namespace net 317 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698