Imported Upstream version 1.34.0
[platform/upstream/grpc.git] / test / core / client_channel / certificate_provider_registry_test.cc
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 <grpc/support/port_platform.h>
20
21 #include <gmock/gmock.h>
22
23 #include "src/core/ext/xds/certificate_provider_registry.h"
24
25 #include "test/core/util/test_config.h"
26
27 namespace grpc_core {
28 namespace testing {
29 namespace {
30
31 class FakeCertificateProviderFactory1 : public CertificateProviderFactory {
32  public:
33   const char* name() const override { return "fake1"; }
34
35   RefCountedPtr<Config> CreateCertificateProviderConfig(
36       const Json& config_json, grpc_error** error) override {
37     return nullptr;
38   }
39
40   RefCountedPtr<grpc_tls_certificate_provider> CreateCertificateProvider(
41       RefCountedPtr<Config> config) override {
42     return nullptr;
43   }
44 };
45
46 class FakeCertificateProviderFactory2 : public CertificateProviderFactory {
47  public:
48   const char* name() const override { return "fake2"; }
49
50   RefCountedPtr<Config> CreateCertificateProviderConfig(
51       const Json& config_json, grpc_error** error) override {
52     return nullptr;
53   }
54
55   RefCountedPtr<grpc_tls_certificate_provider> CreateCertificateProvider(
56       RefCountedPtr<Config> config) override {
57     return nullptr;
58   }
59 };
60
61 TEST(CertificateProviderRegistryTest, Basic) {
62   CertificateProviderRegistry::InitRegistry();
63   auto* fake_factory_1 = new FakeCertificateProviderFactory1;
64   auto* fake_factory_2 = new FakeCertificateProviderFactory2;
65   CertificateProviderRegistry::RegisterCertificateProviderFactory(
66       std::unique_ptr<CertificateProviderFactory>(fake_factory_1));
67   CertificateProviderRegistry::RegisterCertificateProviderFactory(
68       std::unique_ptr<CertificateProviderFactory>(fake_factory_2));
69   EXPECT_EQ(
70       CertificateProviderRegistry::LookupCertificateProviderFactory("fake1"),
71       fake_factory_1);
72   EXPECT_EQ(
73       CertificateProviderRegistry::LookupCertificateProviderFactory("fake2"),
74       fake_factory_2);
75   EXPECT_EQ(
76       CertificateProviderRegistry::LookupCertificateProviderFactory("fake3"),
77       nullptr);
78   CertificateProviderRegistry::ShutdownRegistry();
79 }
80
81 }  // namespace
82 }  // namespace testing
83 }  // namespace grpc_core
84
85 int main(int argc, char** argv) {
86   ::testing::InitGoogleTest(&argc, argv);
87   grpc::testing::TestEnvironment env(argc, argv);
88   auto result = RUN_ALL_TESTS();
89   return result;
90 }