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_issuer_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(CertIssuerSource::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 = | |
35 ReadTestPem("net/data/cert_issuer_source_static_unittest/" + file_name, | |
36 "CERTIFICATE", &der); | |
37 if (!r) | |
38 return r; | |
39 *result = ParsedCertificate::CreateFromCertificateCopy(der); | |
40 if (!*result) | |
41 return ::testing::AssertionFailure() << "CreateFromCertificateCopy failed"; | |
42 return ::testing::AssertionSuccess(); | |
43 } | |
44 | |
45 class CertIssuerSourceStaticTest : public ::testing::Test { | |
46 public: | |
47 void SetUp() override { | |
48 ASSERT_TRUE(ReadTestCert("root.pem", &root_)); | |
49 ASSERT_TRUE(ReadTestCert("i1_1.pem", &i1_1_)); | |
50 ASSERT_TRUE(ReadTestCert("i1_2.pem", &i1_2_)); | |
51 ASSERT_TRUE(ReadTestCert("i2.pem", &i2_)); | |
52 ASSERT_TRUE(ReadTestCert("c1.pem", &c1_)); | |
53 ASSERT_TRUE(ReadTestCert("c2.pem", &c2_)); | |
54 ASSERT_TRUE(ReadTestCert("d.pem", &d_)); | |
55 } | |
56 | |
57 void AddAllCerts(CertIssuerSourceStatic* source) { | |
eroman
2016/06/01 23:30:23
optional: might read more easily as StaticCertIssu
mattm
2016/06/01 23:48:33
Reads easier, but I like the suffix since it sorts
| |
58 source->AddCert(root_); | |
59 source->AddCert(i1_1_); | |
60 source->AddCert(i1_2_); | |
61 source->AddCert(i2_); | |
62 source->AddCert(c1_); | |
63 source->AddCert(c2_); | |
64 source->AddCert(d_); | |
65 } | |
66 | |
67 protected: | |
68 scoped_refptr<ParsedCertificate> root_; | |
69 scoped_refptr<ParsedCertificate> i1_1_; | |
70 scoped_refptr<ParsedCertificate> i1_2_; | |
71 scoped_refptr<ParsedCertificate> i2_; | |
72 scoped_refptr<ParsedCertificate> c1_; | |
73 scoped_refptr<ParsedCertificate> c2_; | |
74 scoped_refptr<ParsedCertificate> d_; | |
75 }; | |
76 | |
77 TEST_F(CertIssuerSourceStaticTest, NoMatch) { | |
78 CertIssuerSourceStatic source; | |
79 source.AddCert(root_); | |
80 | |
81 std::vector<scoped_refptr<ParsedCertificate>> issuers; | |
82 source.SyncGetIssuersOf(c1_.get(), &issuers); | |
83 ASSERT_EQ(0U, issuers.size()); | |
84 } | |
85 | |
86 TEST_F(CertIssuerSourceStaticTest, OneMatch) { | |
87 CertIssuerSourceStatic source; | |
88 AddAllCerts(&source); | |
89 | |
90 std::vector<scoped_refptr<ParsedCertificate>> issuers; | |
91 source.SyncGetIssuersOf(i1_1_.get(), &issuers); | |
92 ASSERT_EQ(1U, issuers.size()); | |
93 EXPECT_TRUE(issuers[0] == root_); | |
94 | |
95 issuers.clear(); | |
96 source.SyncGetIssuersOf(d_.get(), &issuers); | |
97 ASSERT_EQ(1U, issuers.size()); | |
98 EXPECT_TRUE(issuers[0] == i2_); | |
99 } | |
100 | |
101 TEST_F(CertIssuerSourceStaticTest, MultipleMatches) { | |
102 CertIssuerSourceStatic source; | |
103 AddAllCerts(&source); | |
104 | |
105 std::vector<scoped_refptr<ParsedCertificate>> issuers; | |
106 source.SyncGetIssuersOf(c1_.get(), &issuers); | |
107 | |
108 ASSERT_EQ(2U, issuers.size()); | |
109 EXPECT_TRUE(std::find(issuers.begin(), issuers.end(), i1_1_) != | |
110 issuers.end()); | |
111 EXPECT_TRUE(std::find(issuers.begin(), issuers.end(), i1_2_) != | |
112 issuers.end()); | |
113 } | |
114 | |
115 // Searching for the issuer of a self-issued cert returns the same cert if it | |
116 // happens to be in the CertIssuerSourceStatic. | |
117 // Conceptually this makes sense, though probably not very useful in practice. | |
118 // Doesn't hurt anything though. | |
119 TEST_F(CertIssuerSourceStaticTest, SelfIssued) { | |
120 CertIssuerSourceStatic source; | |
121 AddAllCerts(&source); | |
122 | |
123 std::vector<scoped_refptr<ParsedCertificate>> issuers; | |
124 source.SyncGetIssuersOf(root_.get(), &issuers); | |
125 | |
126 ASSERT_EQ(1U, issuers.size()); | |
127 EXPECT_TRUE(issuers[0] == root_); | |
128 } | |
129 | |
130 // CertIssuerSourceStatic never returns results asynchronously. | |
131 TEST_F(CertIssuerSourceStaticTest, IsNotAsync) { | |
132 CertIssuerSourceStatic source; | |
133 source.AddCert(i1_1_); | |
134 std::unique_ptr<CertIssuerSource::Request> request; | |
135 source.AsyncGetIssuersOf(c1_.get(), base::Bind(&NotCalled), &request); | |
eroman
2016/06/01 23:30:23
Note because there is no indication of whether an
mattm
2016/06/01 23:48:33
request being null is the indication that no async
eroman
2016/06/01 23:53:06
I missed the |request| test, per my earlier stupid
| |
136 EXPECT_EQ(nullptr, request); | |
137 } | |
138 | |
139 } // namespace | |
140 | |
141 } // namespace net | |
OLD | NEW |