| OLD | NEW |
| 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/cert_issuer_source_static.h" | 5 #include "net/cert/internal/cert_issuer_source_static.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "net/cert/internal/cert_issuer_source_sync_unittest.h" |
| 8 #include "net/cert/internal/cert_errors.h" | |
| 9 #include "net/cert/internal/parsed_certificate.h" | 8 #include "net/cert/internal/parsed_certificate.h" |
| 10 #include "net/cert/internal/test_helpers.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 10 |
| 13 namespace net { | 11 namespace net { |
| 14 | 12 |
| 15 namespace { | 13 namespace { |
| 16 | 14 |
| 17 ::testing::AssertionResult ReadTestPem(const std::string& file_name, | 15 class CertIssuerSourceStaticTestDelegate { |
| 18 const std::string& block_name, | |
| 19 std::string* result) { | |
| 20 const PemBlockMapping mappings[] = { | |
| 21 {block_name.c_str(), result}, | |
| 22 }; | |
| 23 | |
| 24 return ReadTestDataFromPemFile(file_name, mappings); | |
| 25 } | |
| 26 | |
| 27 ::testing::AssertionResult ReadTestCert( | |
| 28 const std::string& file_name, | |
| 29 scoped_refptr<ParsedCertificate>* result) { | |
| 30 std::string der; | |
| 31 ::testing::AssertionResult r = | |
| 32 ReadTestPem("net/data/cert_issuer_source_static_unittest/" + file_name, | |
| 33 "CERTIFICATE", &der); | |
| 34 if (!r) | |
| 35 return r; | |
| 36 CertErrors errors; | |
| 37 *result = ParsedCertificate::Create(der, {}, &errors); | |
| 38 if (!*result) { | |
| 39 return ::testing::AssertionFailure() | |
| 40 << "ParsedCertificate::Create() failed:\n" | |
| 41 << errors.ToDebugString(); | |
| 42 } | |
| 43 return ::testing::AssertionSuccess(); | |
| 44 } | |
| 45 | |
| 46 class CertIssuerSourceStaticTest : public ::testing::Test { | |
| 47 public: | 16 public: |
| 48 void SetUp() override { | 17 void AddCert(scoped_refptr<ParsedCertificate> cert) { |
| 49 ASSERT_TRUE(ReadTestCert("root.pem", &root_)); | 18 source_.AddCert(std::move(cert)); |
| 50 ASSERT_TRUE(ReadTestCert("i1_1.pem", &i1_1_)); | |
| 51 ASSERT_TRUE(ReadTestCert("i1_2.pem", &i1_2_)); | |
| 52 ASSERT_TRUE(ReadTestCert("i2.pem", &i2_)); | |
| 53 ASSERT_TRUE(ReadTestCert("c1.pem", &c1_)); | |
| 54 ASSERT_TRUE(ReadTestCert("c2.pem", &c2_)); | |
| 55 ASSERT_TRUE(ReadTestCert("d.pem", &d_)); | |
| 56 } | 19 } |
| 57 | 20 |
| 58 void AddAllCerts(CertIssuerSourceStatic* source) { | 21 CertIssuerSource& source() { return source_; } |
| 59 source->AddCert(root_); | |
| 60 source->AddCert(i1_1_); | |
| 61 source->AddCert(i1_2_); | |
| 62 source->AddCert(i2_); | |
| 63 source->AddCert(c1_); | |
| 64 source->AddCert(c2_); | |
| 65 source->AddCert(d_); | |
| 66 } | |
| 67 | 22 |
| 68 protected: | 23 protected: |
| 69 scoped_refptr<ParsedCertificate> root_; | 24 CertIssuerSourceStatic source_; |
| 70 scoped_refptr<ParsedCertificate> i1_1_; | |
| 71 scoped_refptr<ParsedCertificate> i1_2_; | |
| 72 scoped_refptr<ParsedCertificate> i2_; | |
| 73 scoped_refptr<ParsedCertificate> c1_; | |
| 74 scoped_refptr<ParsedCertificate> c2_; | |
| 75 scoped_refptr<ParsedCertificate> d_; | |
| 76 }; | 25 }; |
| 77 | 26 |
| 78 TEST_F(CertIssuerSourceStaticTest, NoMatch) { | 27 INSTANTIATE_TYPED_TEST_CASE_P(CertIssuerSourceStaticTest, |
| 79 CertIssuerSourceStatic source; | 28 CertIssuerSourceSyncTest, |
| 80 source.AddCert(root_); | 29 CertIssuerSourceStaticTestDelegate); |
| 81 | 30 |
| 82 ParsedCertificateList issuers; | 31 INSTANTIATE_TYPED_TEST_CASE_P(CertIssuerSourceStaticNormalizationTest, |
| 83 source.SyncGetIssuersOf(c1_.get(), &issuers); | 32 CertIssuerSourceSyncNormalizationTest, |
| 84 ASSERT_EQ(0U, issuers.size()); | 33 CertIssuerSourceStaticTestDelegate); |
| 85 } | |
| 86 | |
| 87 TEST_F(CertIssuerSourceStaticTest, OneMatch) { | |
| 88 CertIssuerSourceStatic source; | |
| 89 AddAllCerts(&source); | |
| 90 | |
| 91 ParsedCertificateList issuers; | |
| 92 source.SyncGetIssuersOf(i1_1_.get(), &issuers); | |
| 93 ASSERT_EQ(1U, issuers.size()); | |
| 94 EXPECT_TRUE(issuers[0] == root_); | |
| 95 | |
| 96 issuers.clear(); | |
| 97 source.SyncGetIssuersOf(d_.get(), &issuers); | |
| 98 ASSERT_EQ(1U, issuers.size()); | |
| 99 EXPECT_TRUE(issuers[0] == i2_); | |
| 100 } | |
| 101 | |
| 102 TEST_F(CertIssuerSourceStaticTest, MultipleMatches) { | |
| 103 CertIssuerSourceStatic source; | |
| 104 AddAllCerts(&source); | |
| 105 | |
| 106 ParsedCertificateList issuers; | |
| 107 source.SyncGetIssuersOf(c1_.get(), &issuers); | |
| 108 | |
| 109 ASSERT_EQ(2U, issuers.size()); | |
| 110 EXPECT_TRUE(std::find(issuers.begin(), issuers.end(), i1_1_) != | |
| 111 issuers.end()); | |
| 112 EXPECT_TRUE(std::find(issuers.begin(), issuers.end(), i1_2_) != | |
| 113 issuers.end()); | |
| 114 } | |
| 115 | |
| 116 // Searching for the issuer of a self-issued cert returns the same cert if it | |
| 117 // happens to be in the CertIssuerSourceStatic. | |
| 118 // Conceptually this makes sense, though probably not very useful in practice. | |
| 119 // Doesn't hurt anything though. | |
| 120 TEST_F(CertIssuerSourceStaticTest, SelfIssued) { | |
| 121 CertIssuerSourceStatic source; | |
| 122 AddAllCerts(&source); | |
| 123 | |
| 124 ParsedCertificateList issuers; | |
| 125 source.SyncGetIssuersOf(root_.get(), &issuers); | |
| 126 | |
| 127 ASSERT_EQ(1U, issuers.size()); | |
| 128 EXPECT_TRUE(issuers[0] == root_); | |
| 129 } | |
| 130 | |
| 131 // CertIssuerSourceStatic never returns results asynchronously. | |
| 132 TEST_F(CertIssuerSourceStaticTest, IsNotAsync) { | |
| 133 CertIssuerSourceStatic source; | |
| 134 source.AddCert(i1_1_); | |
| 135 std::unique_ptr<CertIssuerSource::Request> request; | |
| 136 source.AsyncGetIssuersOf(c1_.get(), &request); | |
| 137 EXPECT_EQ(nullptr, request); | |
| 138 } | |
| 139 | 34 |
| 140 } // namespace | 35 } // namespace |
| 141 | 36 |
| 142 } // namespace net | 37 } // namespace net |
| OLD | NEW |