Debian Codegraph @

test/core/security/certificate_provider_registry_test.cc

raw
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 "src/core/lib/security/certificate_provider/certificate_provider_registry.h"
22
23 #include <gmock/gmock.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:
34 const char* name() const override { return "fake1"; }
35
36 RefCountedPtr<Config> CreateCertificateProviderConfig(
37 const Json& /*config_json*/, grpc_error_handle* /*error*/) override {
38 return nullptr;
39 }
40
41 RefCountedPtr<grpc_tls_certificate_provider> CreateCertificateProvider(
42 RefCountedPtr<Config> /*config*/) override {
43 return nullptr;
44 }
45 };
46
47 class FakeCertificateProviderFactory2 : public CertificateProviderFactory {
48 public:
49 const char* name() const override { return "fake2"; }
50
51 RefCountedPtr<Config> CreateCertificateProviderConfig(
52 const Json& /*config_json*/, grpc_error_handle* /*error*/) override {
53 return nullptr;
54 }
55
56 RefCountedPtr<grpc_tls_certificate_provider> CreateCertificateProvider(
57 RefCountedPtr<Config> /*config*/) override {
58 return nullptr;
59 }
60 };
61
62 TEST(CertificateProviderRegistryTest, Basic) {
63 CertificateProviderRegistry::Builder b;
64 auto* fake_factory_1 = new FakeCertificateProviderFactory1;
65 auto* fake_factory_2 = new FakeCertificateProviderFactory2;
66 b.RegisterCertificateProviderFactory(
67 std::unique_ptr<CertificateProviderFactory>(fake_factory_1));
68 b.RegisterCertificateProviderFactory(
69 std::unique_ptr<CertificateProviderFactory>(fake_factory_2));
70 auto r = b.Build();
71 EXPECT_EQ(r.LookupCertificateProviderFactory("fake1"), fake_factory_1);
72 EXPECT_EQ(r.LookupCertificateProviderFactory("fake2"), fake_factory_2);
73 EXPECT_EQ(r.LookupCertificateProviderFactory("fake3"), nullptr);
74 }
75
76 } // namespace
77 } // namespace testing
78 } // namespace grpc_core
79
80 int main(int argc, char** argv) {
81 ::testing::InitGoogleTest(&argc, argv);
82 grpc::testing::TestEnvironment env(&argc, argv);
83 auto result = RUN_ALL_TESTS();
84 return result;
85 }