1 //
2 //
3 // Copyright 2020 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include "src/core/lib/security/certificate_provider/certificate_provider_registry.h"
20
21 #include <gmock/gmock.h>
22
23 #include <grpc/support/port_platform.h>
24
25 #include "src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.h"
26 #include "test/core/util/test_config.h"
27
28 namespace grpc_core {
29 namespace testing {
30 namespace {
31
32 class FakeCertificateProviderFactory1 : public CertificateProviderFactory {
33 public:
name() const34 absl::string_view name() const override { return "fake1"; }
35
CreateCertificateProviderConfig(const Json &,const JsonArgs &,ValidationErrors *)36 RefCountedPtr<Config> CreateCertificateProviderConfig(
37 const Json& /*config_json*/, const JsonArgs& /*args*/,
38 ValidationErrors* /*errors*/) override {
39 return nullptr;
40 }
41
CreateCertificateProvider(RefCountedPtr<Config>)42 RefCountedPtr<grpc_tls_certificate_provider> CreateCertificateProvider(
43 RefCountedPtr<Config> /*config*/) override {
44 return nullptr;
45 }
46 };
47
48 class FakeCertificateProviderFactory2 : public CertificateProviderFactory {
49 public:
name() const50 absl::string_view name() const override { return "fake2"; }
51
CreateCertificateProviderConfig(const Json &,const JsonArgs &,ValidationErrors *)52 RefCountedPtr<Config> CreateCertificateProviderConfig(
53 const Json& /*config_json*/, const JsonArgs& /*args*/,
54 ValidationErrors* /*errors*/) override {
55 return nullptr;
56 }
57
CreateCertificateProvider(RefCountedPtr<Config>)58 RefCountedPtr<grpc_tls_certificate_provider> CreateCertificateProvider(
59 RefCountedPtr<Config> /*config*/) override {
60 return nullptr;
61 }
62 };
63
TEST(CertificateProviderRegistryTest,Basic)64 TEST(CertificateProviderRegistryTest, Basic) {
65 CertificateProviderRegistry::Builder b;
66 auto* fake_factory_1 = new FakeCertificateProviderFactory1;
67 auto* fake_factory_2 = new FakeCertificateProviderFactory2;
68 b.RegisterCertificateProviderFactory(
69 std::unique_ptr<CertificateProviderFactory>(fake_factory_1));
70 b.RegisterCertificateProviderFactory(
71 std::unique_ptr<CertificateProviderFactory>(fake_factory_2));
72 auto r = b.Build();
73 EXPECT_EQ(r.LookupCertificateProviderFactory("fake1"), fake_factory_1);
74 EXPECT_EQ(r.LookupCertificateProviderFactory("fake2"), fake_factory_2);
75 EXPECT_EQ(r.LookupCertificateProviderFactory("fake3"), nullptr);
76 }
77
78 } // namespace
79 } // namespace testing
80 } // namespace grpc_core
81
main(int argc,char ** argv)82 int main(int argc, char** argv) {
83 ::testing::InitGoogleTest(&argc, argv);
84 grpc::testing::TestEnvironment env(&argc, argv);
85 auto result = RUN_ALL_TESTS();
86 return result;
87 }
88