1 // Copyright 2016 The Chromium Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "cert_issuer_source_static.h"
16 
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include "cert_issuer_source_sync_unittest.h"
20 #include "parsed_certificate.h"
21 
22 BSSL_NAMESPACE_BEGIN
23 
24 namespace {
25 
26 class CertIssuerSourceStaticTestDelegate {
27  public:
AddCert(std::shared_ptr<const ParsedCertificate> cert)28   void AddCert(std::shared_ptr<const ParsedCertificate> cert) {
29     source_.AddCert(std::move(cert));
30   }
31 
source()32   CertIssuerSource &source() { return source_; }
33 
34  protected:
35   CertIssuerSourceStatic source_;
36 };
37 
38 INSTANTIATE_TYPED_TEST_SUITE_P(CertIssuerSourceStaticSyncTest,
39                                CertIssuerSourceSyncTest,
40                                CertIssuerSourceStaticTestDelegate);
41 
42 INSTANTIATE_TYPED_TEST_SUITE_P(CertIssuerSourceStaticNormalizationTest,
43                                CertIssuerSourceSyncNormalizationTest,
44                                CertIssuerSourceStaticTestDelegate);
45 
46 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(
47     CertIssuerSourceSyncNotNormalizedTest);
48 
TEST(CertIssuerSourceStaticTest,AddCertsGetCertsAndClear)49 TEST(CertIssuerSourceStaticTest, AddCertsGetCertsAndClear) {
50   std::string test_dir = "testdata/cert_issuer_source_static_unittest/";
51   std::shared_ptr<const ParsedCertificate> cert1 =
52       ReadCertFromFile(test_dir + "root.pem");
53   ASSERT_TRUE(cert1);
54   std::shared_ptr<const ParsedCertificate> cert2 =
55       ReadCertFromFile(test_dir + "i1_1.pem");
56   ASSERT_TRUE(cert2);
57   std::shared_ptr<const ParsedCertificate> cert3 =
58       ReadCertFromFile(test_dir + "i1_2.pem");
59   ASSERT_TRUE(cert3);
60 
61   CertIssuerSourceStatic source;
62   EXPECT_TRUE(source.Certs().empty());
63   EXPECT_EQ(source.size(), 0u);
64 
65   source.AddCert(cert1);
66   EXPECT_THAT(source.Certs(), testing::UnorderedElementsAre(cert1));
67   EXPECT_EQ(source.size(), 1u);
68 
69   source.AddCert(cert2);
70   EXPECT_THAT(source.Certs(), testing::UnorderedElementsAre(cert1, cert2));
71   EXPECT_EQ(source.size(), 2u);
72 
73   source.AddCert(cert3);
74   EXPECT_THAT(source.Certs(),
75               testing::UnorderedElementsAre(cert1, cert2, cert3));
76   EXPECT_EQ(source.size(), 3u);
77 
78   source.Clear();
79   EXPECT_TRUE(source.Certs().empty());
80   EXPECT_EQ(source.size(), 0u);
81 }
82 
83 }  // namespace
84 
85 BSSL_NAMESPACE_END
86