| 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/test_helpers.h" | 8 #include "net/cert/internal/test_helpers.h" |
| 9 #include "net/der/input.h" | 9 #include "net/der/input.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 // Read the certificate data and test expectations from a single PEM file. | 38 // Read the certificate data and test expectations from a single PEM file. |
| 39 const PemBlockMapping mappings[] = { | 39 const PemBlockMapping mappings[] = { |
| 40 {"CERTIFICATE", &data}, | 40 {"CERTIFICATE", &data}, |
| 41 {"SIGNATURE", &expected_signature}, | 41 {"SIGNATURE", &expected_signature}, |
| 42 {"SIGNATURE ALGORITHM", &expected_signature_algorithm}, | 42 {"SIGNATURE ALGORITHM", &expected_signature_algorithm}, |
| 43 {"TBS CERTIFICATE", &expected_tbs_certificate}, | 43 {"TBS CERTIFICATE", &expected_tbs_certificate}, |
| 44 }; | 44 }; |
| 45 ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings)); | 45 ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings)); |
| 46 | 46 |
| 47 // Parsing the certificate should succeed. | 47 // Parsing the certificate should succeed. |
| 48 ParsedCertificate parsed; | 48 der::Input tbs_certificate_tlv; |
| 49 ASSERT_TRUE(ParseCertificate(der::Input(&data), &parsed)); | 49 der::Input signature_algorithm_tlv; |
| 50 der::BitString signature_value; |
| 51 ASSERT_TRUE(ParseCertificate(der::Input(&data), &tbs_certificate_tlv, |
| 52 &signature_algorithm_tlv, &signature_value)); |
| 50 | 53 |
| 51 // Ensure that the ParsedCertificate matches expectations. | 54 // Ensure that the parsed certificate matches expectations. |
| 52 EXPECT_EQ(0, parsed.signature_value.unused_bits()); | 55 EXPECT_EQ(0, signature_value.unused_bits()); |
| 53 EXPECT_EQ(der::Input(&expected_signature), parsed.signature_value.bytes()); | 56 EXPECT_EQ(der::Input(&expected_signature), signature_value.bytes()); |
| 54 EXPECT_EQ(der::Input(&expected_signature_algorithm), | 57 EXPECT_EQ(der::Input(&expected_signature_algorithm), signature_algorithm_tlv); |
| 55 parsed.signature_algorithm_tlv); | 58 EXPECT_EQ(der::Input(&expected_tbs_certificate), tbs_certificate_tlv); |
| 56 EXPECT_EQ(der::Input(&expected_tbs_certificate), parsed.tbs_certificate_tlv); | |
| 57 } | 59 } |
| 58 | 60 |
| 59 // Loads certificate data from the PEM file |file_name| and verifies that the | 61 // Loads certificate data from the PEM file |file_name| and verifies that the |
| 60 // Certificate parsing fails. | 62 // Certificate parsing fails. |
| 61 void EnsureParsingCertificateFails(const std::string& file_name) { | 63 void EnsureParsingCertificateFails(const std::string& file_name) { |
| 62 std::string data; | 64 std::string data; |
| 63 | 65 |
| 64 const PemBlockMapping mappings[] = { | 66 const PemBlockMapping mappings[] = { |
| 65 {"CERTIFICATE", &data}, | 67 {"CERTIFICATE", &data}, |
| 66 }; | 68 }; |
| 67 | 69 |
| 68 ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings)); | 70 ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings)); |
| 69 | 71 |
| 70 // Parsing the Certificate should fail. | 72 // Parsing the Certificate should fail. |
| 71 ParsedCertificate parsed; | 73 der::Input tbs_certificate_tlv; |
| 72 ASSERT_FALSE(ParseCertificate(der::Input(&data), &parsed)); | 74 der::Input signature_algorithm_tlv; |
| 75 der::BitString signature_value; |
| 76 ASSERT_FALSE(ParseCertificate(der::Input(&data), &tbs_certificate_tlv, |
| 77 &signature_algorithm_tlv, &signature_value)); |
| 73 } | 78 } |
| 74 | 79 |
| 75 // Tests parsing a Certificate. | 80 // Tests parsing a Certificate. |
| 76 TEST(ParseCertificateTest, Version3) { | 81 TEST(ParseCertificateTest, Version3) { |
| 77 EnsureParsingCertificateSucceeds("cert_version3.pem"); | 82 EnsureParsingCertificateSucceeds("cert_version3.pem"); |
| 78 } | 83 } |
| 79 | 84 |
| 80 // Tests parsing a simplified Certificate-like structure (the sub-fields for | 85 // Tests parsing a simplified Certificate-like structure (the sub-fields for |
| 81 // algorithm and tbsCertificate are not actually valid, but ParseCertificate() | 86 // algorithm and tbsCertificate are not actually valid, but ParseCertificate() |
| 82 // doesn't check them) | 87 // doesn't check them) |
| (...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 781 0x00, // Number of unused bits | 786 0x00, // Number of unused bits |
| 782 }; | 787 }; |
| 783 | 788 |
| 784 der::BitString key_usage; | 789 der::BitString key_usage; |
| 785 ASSERT_FALSE(ParseKeyUsage(der::Input(der), &key_usage)); | 790 ASSERT_FALSE(ParseKeyUsage(der::Input(der), &key_usage)); |
| 786 } | 791 } |
| 787 | 792 |
| 788 } // namespace | 793 } // namespace |
| 789 | 794 |
| 790 } // namespace net | 795 } // namespace net |
| OLD | NEW |