Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_certificate.h" | 5 #include "net/cert/internal/parse_certificate.h" |
| 6 | 6 |
| 7 #include "base/strings/stringprintf.h" | |
| 7 #include "net/cert/internal/test_helpers.h" | 8 #include "net/cert/internal/test_helpers.h" |
| 8 #include "net/der/input.h" | 9 #include "net/der/input.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 11 |
| 11 namespace net { | 12 namespace net { |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 std::string GetFilePath(const std::string file_name) { | 16 std::string GetFilePath(const std::string file_name) { |
| 16 return std::string("net/data/parse_certificate_unittest/") + file_name; | 17 return std::string("net/data/parse_certificate_unittest/") + file_name; |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 // The version was set to v4, which is unrecognized. | 226 // The version was set to v4, which is unrecognized. |
| 226 TEST(ParseTbsCertificateTest, Version4) { | 227 TEST(ParseTbsCertificateTest, Version4) { |
| 227 EnsureParsingTbsFails("tbs_v4.pem"); | 228 EnsureParsingTbsFails("tbs_v4.pem"); |
| 228 } | 229 } |
| 229 | 230 |
| 230 // Tests that extraneous data after extensions in a v3 is rejected. | 231 // Tests that extraneous data after extensions in a v3 is rejected. |
| 231 TEST(ParseTbsCertificateTest, Version3DataAfterExtensions) { | 232 TEST(ParseTbsCertificateTest, Version3DataAfterExtensions) { |
| 232 EnsureParsingTbsFails("tbs_v3_data_after_extensions.pem"); | 233 EnsureParsingTbsFails("tbs_v3_data_after_extensions.pem"); |
| 233 } | 234 } |
| 234 | 235 |
| 236 // Pretty-prints a GeneralizedTime as a human-readable string for use in test | |
| 237 // expectations (it is more readable to specify the expected results as a | |
| 238 // string). | |
| 239 std::string ToString(const der::GeneralizedTime& time) { | |
| 240 return base::StringPrintf( | |
| 241 "year=%d, month=%d, day=%d, hours=%d, minutes=%d, seconds=%d", time.year, | |
| 242 time.month, time.day, time.hours, time.minutes, time.seconds); | |
| 243 } | |
| 244 | |
| 245 // Reads the "VALIDITY" block from a PEM file. | |
| 246 ::testing::AssertionResult LoadTestValidity(const std::string& file_name, | |
| 247 std::string* validity) { | |
| 248 std::string path = GetFilePath(file_name); | |
| 249 const PemBlockMapping mappings[] = {{"VALIDITY", validity}}; | |
| 250 return ReadTestDataFromPemFile(path, mappings); | |
| 251 } | |
| 252 | |
| 253 // Parses a valid "validity" field, where both notBefore and notAfter are | |
| 254 // expressed using UTCTime. | |
| 255 TEST(ParseCertificateValidityTest, BothUtcTime) { | |
| 256 std::string validity; | |
| 257 | |
| 258 ASSERT_TRUE(LoadTestValidity("validity_both_utc_time.pem", &validity)); | |
| 259 | |
| 260 der::GeneralizedTime validity_not_before; | |
| 261 der::GeneralizedTime validity_not_after; | |
| 262 ASSERT_TRUE(ParseValidity(InputFromString(&validity), &validity_not_before, | |
| 263 &validity_not_after)); | |
| 264 | |
| 265 EXPECT_EQ("year=2012, month=10, day=18, hours=3, minutes=12, seconds=0", | |
| 266 ToString(validity_not_before)); | |
| 267 | |
| 268 EXPECT_EQ("year=2013, month=10, day=18, hours=14, minutes=59, seconds=59", | |
| 269 ToString(validity_not_after)); | |
| 270 } | |
| 271 | |
| 272 // Parses a valid "validity" field, where both notBefore and notAfter are | |
| 273 // expressed using GeneralizedTime. | |
| 274 TEST(ParseCertificateValidityTest, BothGeneralizedTime) { | |
| 275 std::string validity; | |
| 276 | |
| 277 ASSERT_TRUE( | |
| 278 LoadTestValidity("validity_both_generalized_time.pem", &validity)); | |
| 279 | |
| 280 der::GeneralizedTime validity_not_before; | |
| 281 der::GeneralizedTime validity_not_after; | |
| 282 ASSERT_TRUE(ParseValidity(InputFromString(&validity), &validity_not_before, | |
| 283 &validity_not_after)); | |
| 284 | |
| 285 EXPECT_EQ("year=2014, month=1, day=31, hours=0, minutes=0, seconds=0", | |
| 286 ToString(validity_not_before)); | |
| 287 | |
| 288 EXPECT_EQ("year=2016, month=2, day=29, hours=0, minutes=0, seconds=0", | |
| 289 ToString(validity_not_after)); | |
| 290 } | |
| 291 | |
| 292 // Parses a valid "validity" field, where notBefore is a UTCTime and notAfter is | |
| 293 // a GeneralizedTime | |
| 294 TEST(ParseCertificateValidityTest, UTCTimeAndGeneralizedTime) { | |
| 295 std::string validity; | |
| 296 | |
| 297 ASSERT_TRUE(LoadTestValidity("validity_utc_time_and_generalized_time.pem", | |
| 298 &validity)); | |
| 299 | |
| 300 der::GeneralizedTime validity_not_before; | |
| 301 der::GeneralizedTime validity_not_after; | |
| 302 ASSERT_TRUE(ParseValidity(InputFromString(&validity), &validity_not_before, | |
| 303 &validity_not_after)); | |
| 304 | |
| 305 EXPECT_EQ("year=2012, month=10, day=18, hours=3, minutes=12, seconds=0", | |
| 306 ToString(validity_not_before)); | |
| 307 | |
| 308 EXPECT_EQ("year=2016, month=2, day=29, hours=0, minutes=0, seconds=0", | |
| 309 ToString(validity_not_after)); | |
| 310 } | |
| 311 | |
| 312 // Parses a valid "validity" field, where notBefore is a GeneralizedTime and | |
| 313 // notAfter is | |
| 314 // a UTCTime. Also of interest, notBefore > notAfter. Parsing will succeed, | |
| 315 // however no time will fall in this range. | |
|
davidben
2015/08/18 17:24:27
Nit: This looks like a wrapped funny.
| |
| 316 TEST(ParseCertificateValidityTest, GeneralizedTimeAndUTCTime) { | |
| 317 std::string validity; | |
| 318 | |
| 319 ASSERT_TRUE(LoadTestValidity("validity_generalized_time_and_utc_time.pem", | |
| 320 &validity)); | |
| 321 | |
| 322 der::GeneralizedTime validity_not_before; | |
| 323 der::GeneralizedTime validity_not_after; | |
| 324 ASSERT_TRUE(ParseValidity(InputFromString(&validity), &validity_not_before, | |
| 325 &validity_not_after)); | |
| 326 | |
| 327 EXPECT_EQ("year=2014, month=1, day=31, hours=0, minutes=0, seconds=0", | |
| 328 ToString(validity_not_before)); | |
| 329 | |
| 330 EXPECT_EQ("year=2013, month=10, day=18, hours=14, minutes=59, seconds=59", | |
| 331 ToString(validity_not_after)); | |
| 332 } | |
| 333 | |
| 235 } // namespace | 334 } // namespace |
| 236 | 335 |
| 237 } // namespace net | 336 } // namespace net |
| OLD | NEW |