| 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 "base/strings/stringprintf.h" |
| 8 #include "net/cert/internal/cert_errors.h" |
| 8 #include "net/cert/internal/test_helpers.h" | 9 #include "net/cert/internal/test_helpers.h" |
| 9 #include "net/der/input.h" | 10 #include "net/der/input.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 12 |
| 12 namespace net { | 13 namespace net { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // Pretty-prints a GeneralizedTime as a human-readable string for use in test | 17 // Pretty-prints a GeneralizedTime as a human-readable string for use in test |
| 17 // expectations (it is more readable to specify the expected results as a | 18 // expectations (it is more readable to specify the expected results as a |
| (...skipping 24 matching lines...) Expand all Loading... |
| 42 {"SIGNATURE ALGORITHM", &expected_signature_algorithm}, | 43 {"SIGNATURE ALGORITHM", &expected_signature_algorithm}, |
| 43 {"TBS CERTIFICATE", &expected_tbs_certificate}, | 44 {"TBS CERTIFICATE", &expected_tbs_certificate}, |
| 44 }; | 45 }; |
| 45 ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings)); | 46 ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings)); |
| 46 | 47 |
| 47 // Parsing the certificate should succeed. | 48 // Parsing the certificate should succeed. |
| 48 der::Input tbs_certificate_tlv; | 49 der::Input tbs_certificate_tlv; |
| 49 der::Input signature_algorithm_tlv; | 50 der::Input signature_algorithm_tlv; |
| 50 der::BitString signature_value; | 51 der::BitString signature_value; |
| 51 ASSERT_TRUE(ParseCertificate(der::Input(&data), &tbs_certificate_tlv, | 52 ASSERT_TRUE(ParseCertificate(der::Input(&data), &tbs_certificate_tlv, |
| 52 &signature_algorithm_tlv, &signature_value)); | 53 &signature_algorithm_tlv, &signature_value, |
| 54 nullptr)); |
| 53 | 55 |
| 54 // Ensure that the parsed certificate matches expectations. | 56 // Ensure that the parsed certificate matches expectations. |
| 55 EXPECT_EQ(0, signature_value.unused_bits()); | 57 EXPECT_EQ(0, signature_value.unused_bits()); |
| 56 EXPECT_EQ(der::Input(&expected_signature), signature_value.bytes()); | 58 EXPECT_EQ(der::Input(&expected_signature), signature_value.bytes()); |
| 57 EXPECT_EQ(der::Input(&expected_signature_algorithm), signature_algorithm_tlv); | 59 EXPECT_EQ(der::Input(&expected_signature_algorithm), signature_algorithm_tlv); |
| 58 EXPECT_EQ(der::Input(&expected_tbs_certificate), tbs_certificate_tlv); | 60 EXPECT_EQ(der::Input(&expected_tbs_certificate), tbs_certificate_tlv); |
| 59 } | 61 } |
| 60 | 62 |
| 61 // Loads certificate data from the PEM file |file_name| and verifies that the | 63 // Loads certificate data from the PEM file |file_name| and verifies that the |
| 62 // Certificate parsing fails. | 64 // Certificate parsing fails. |
| 63 void EnsureParsingCertificateFails(const std::string& file_name) { | 65 void EnsureParsingCertificateFails(const std::string& file_name) { |
| 64 std::string data; | 66 std::string data; |
| 65 | 67 |
| 66 const PemBlockMapping mappings[] = { | 68 const PemBlockMapping mappings[] = { |
| 67 {"CERTIFICATE", &data}, | 69 {"CERTIFICATE", &data}, |
| 68 }; | 70 }; |
| 69 | 71 |
| 70 ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings)); | 72 ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings)); |
| 71 | 73 |
| 72 // Parsing the Certificate should fail. | 74 // Parsing the Certificate should fail. |
| 73 der::Input tbs_certificate_tlv; | 75 der::Input tbs_certificate_tlv; |
| 74 der::Input signature_algorithm_tlv; | 76 der::Input signature_algorithm_tlv; |
| 75 der::BitString signature_value; | 77 der::BitString signature_value; |
| 78 CertErrors errors; |
| 76 ASSERT_FALSE(ParseCertificate(der::Input(&data), &tbs_certificate_tlv, | 79 ASSERT_FALSE(ParseCertificate(der::Input(&data), &tbs_certificate_tlv, |
| 77 &signature_algorithm_tlv, &signature_value)); | 80 &signature_algorithm_tlv, &signature_value, |
| 81 &errors)); |
| 82 // TODO(crbug.com/634443): Verify |errors| to make sure it failed for the |
| 83 // expected reason. |
| 78 } | 84 } |
| 79 | 85 |
| 80 // Tests parsing a Certificate. | 86 // Tests parsing a Certificate. |
| 81 TEST(ParseCertificateTest, Version3) { | 87 TEST(ParseCertificateTest, Version3) { |
| 82 EnsureParsingCertificateSucceeds("cert_version3.pem"); | 88 EnsureParsingCertificateSucceeds("cert_version3.pem"); |
| 83 } | 89 } |
| 84 | 90 |
| 85 // Tests parsing a simplified Certificate-like structure (the sub-fields for | 91 // Tests parsing a simplified Certificate-like structure (the sub-fields for |
| 86 // algorithm and tbsCertificate are not actually valid, but ParseCertificate() | 92 // algorithm and tbsCertificate are not actually valid, but ParseCertificate() |
| 87 // doesn't check them) | 93 // doesn't check them) |
| (...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 786 0x00, // Number of unused bits | 792 0x00, // Number of unused bits |
| 787 }; | 793 }; |
| 788 | 794 |
| 789 der::BitString key_usage; | 795 der::BitString key_usage; |
| 790 ASSERT_FALSE(ParseKeyUsage(der::Input(der), &key_usage)); | 796 ASSERT_FALSE(ParseKeyUsage(der::Input(der), &key_usage)); |
| 791 } | 797 } |
| 792 | 798 |
| 793 } // namespace | 799 } // namespace |
| 794 | 800 |
| 795 } // namespace net | 801 } // namespace net |
| OLD | NEW |