Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/cert/internal/cert_source_static.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "net/cert/internal/parsed_certificate.h" | |
| 9 #include "net/cert/internal/test_helpers.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace net { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 void NotCalled(CertSource::Request* req) { | |
| 17 ADD_FAILURE() << "NotCalled was called"; | |
| 18 } | |
| 19 | |
| 20 ::testing::AssertionResult ReadTestPem(const std::string& file_name, | |
| 21 const std::string& block_name, | |
| 22 std::string* result) { | |
| 23 const PemBlockMapping mappings[] = { | |
| 24 {block_name.c_str(), result}, | |
| 25 }; | |
| 26 | |
| 27 return ReadTestDataFromPemFile(file_name, mappings); | |
| 28 } | |
| 29 | |
| 30 ::testing::AssertionResult ReadTestCert( | |
| 31 const std::string& file_name, | |
| 32 scoped_refptr<ParsedCertificate>* result) { | |
| 33 std::string der; | |
| 34 ::testing::AssertionResult r = ReadTestPem( | |
| 35 "net/data/cert_source_static_unittest/" + file_name, "CERTIFICATE", &der); | |
| 36 if (!r) | |
| 37 return r; | |
| 38 *result = ParsedCertificate::CreateFromCertificateCopy(der); | |
| 39 if (!*result) | |
| 40 return ::testing::AssertionFailure() << "CreateFromCertificateCopy failed"; | |
| 41 return ::testing::AssertionSuccess(); | |
| 42 } | |
| 43 | |
| 44 class CertSourceStaticTest : public ::testing::Test { | |
| 45 public: | |
| 46 void SetUp() override { | |
| 47 ASSERT_TRUE(ReadTestCert("root.pem", &root_)); | |
| 48 ASSERT_TRUE(ReadTestCert("b1_1.pem", &b1_1_)); | |
| 49 ASSERT_TRUE(ReadTestCert("b1_2.pem", &b1_2_)); | |
| 50 ASSERT_TRUE(ReadTestCert("b2_1.pem", &b2_1_)); | |
| 51 ASSERT_TRUE(ReadTestCert("c1_1.pem", &c1_1_)); | |
| 52 ASSERT_TRUE(ReadTestCert("c1_2.pem", &c1_2_)); | |
| 53 ASSERT_TRUE(ReadTestCert("c2_1.pem", &c2_1_)); | |
| 54 } | |
| 55 | |
| 56 void AddAllCerts(CertSourceStatic* source) { | |
| 57 source->AddCert(root_); | |
| 58 source->AddCert(b1_1_); | |
| 59 source->AddCert(b1_2_); | |
| 60 source->AddCert(b2_1_); | |
| 61 source->AddCert(c1_1_); | |
| 62 source->AddCert(c1_2_); | |
| 63 source->AddCert(c2_1_); | |
| 64 } | |
| 65 | |
| 66 protected: | |
| 67 scoped_refptr<ParsedCertificate> root_, b1_1_, b1_2_, b2_1_, c1_1_, c1_2_, | |
|
eroman
2016/06/01 20:52:10
one per line please -- i believe that is the usual
mattm
2016/06/01 22:20:41
Done.
| |
| 68 c2_1_; | |
| 69 }; | |
| 70 | |
| 71 TEST_F(CertSourceStaticTest, NoMatch) { | |
| 72 CertSourceStatic source; | |
| 73 source.AddCert(root_); | |
| 74 | |
| 75 std::vector<scoped_refptr<ParsedCertificate>> issuers; | |
| 76 source.SyncGetIssuersOf(c1_1_.get(), &issuers); | |
| 77 ASSERT_EQ(0U, issuers.size()); | |
| 78 } | |
| 79 | |
| 80 TEST_F(CertSourceStaticTest, OneMatch) { | |
| 81 CertSourceStatic source; | |
| 82 AddAllCerts(&source); | |
| 83 | |
| 84 std::vector<scoped_refptr<ParsedCertificate>> issuers; | |
| 85 source.SyncGetIssuersOf(b1_1_.get(), &issuers); | |
| 86 ASSERT_EQ(1U, issuers.size()); | |
| 87 EXPECT_TRUE(issuers[0] == root_); | |
| 88 | |
| 89 issuers.clear(); | |
| 90 source.SyncGetIssuersOf(c2_1_.get(), &issuers); | |
| 91 ASSERT_EQ(1U, issuers.size()); | |
| 92 EXPECT_TRUE(issuers[0] == b2_1_); | |
| 93 } | |
| 94 | |
| 95 TEST_F(CertSourceStaticTest, MultipleMatches) { | |
| 96 CertSourceStatic source; | |
| 97 AddAllCerts(&source); | |
| 98 | |
| 99 std::vector<scoped_refptr<ParsedCertificate>> issuers; | |
| 100 source.SyncGetIssuersOf(c1_1_.get(), &issuers); | |
| 101 | |
| 102 ASSERT_EQ(2U, issuers.size()); | |
| 103 EXPECT_TRUE(std::find(issuers.begin(), issuers.end(), b1_1_) != | |
| 104 issuers.end()); | |
| 105 EXPECT_TRUE(std::find(issuers.begin(), issuers.end(), b1_2_) != | |
| 106 issuers.end()); | |
| 107 } | |
| 108 | |
| 109 // Searching for the issuer of a self-issued cert returns the same cert if it | |
| 110 // happens to be in the CertSourceStatic. | |
| 111 // Conceptually this makes sense, though probably not very useful in practice. | |
| 112 // Doesn't hurt anything though. | |
| 113 TEST_F(CertSourceStaticTest, SelfIssued) { | |
| 114 CertSourceStatic source; | |
| 115 AddAllCerts(&source); | |
| 116 | |
| 117 std::vector<scoped_refptr<ParsedCertificate>> issuers; | |
| 118 source.SyncGetIssuersOf(root_.get(), &issuers); | |
| 119 | |
| 120 ASSERT_EQ(1U, issuers.size()); | |
| 121 EXPECT_TRUE(issuers[0] == root_); | |
| 122 } | |
| 123 | |
| 124 // CertSourceStatic never returns results asynchronously. | |
| 125 TEST_F(CertSourceStaticTest, IsNotAsync) { | |
| 126 CertSourceStatic source; | |
| 127 source.AddCert(b1_1_); | |
| 128 std::unique_ptr<CertSource::Request> request; | |
| 129 source.AsyncGetIssuersOf(c1_1_.get(), base::Bind(&NotCalled), &request); | |
| 130 EXPECT_EQ(nullptr, request); | |
| 131 } | |
| 132 | |
| 133 } // namespace | |
| 134 | |
| 135 } // namespace net | |
| OLD | NEW |