certificate_provider_store_test.cc
Go to the documentation of this file.
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 
20 
21 #include <thread>
22 
23 #include <gmock/gmock.h>
24 
28 
29 namespace grpc_core {
30 namespace testing {
31 namespace {
32 
33 class CertificateProviderStoreTest : public ::testing::Test {
34  public:
35  CertificateProviderStoreTest() { grpc_init(); }
36 
37  ~CertificateProviderStoreTest() override { grpc_shutdown_blocking(); }
38 };
39 
40 class FakeCertificateProvider : public grpc_tls_certificate_provider {
41  public:
42  RefCountedPtr<grpc_tls_certificate_distributor> distributor() const override {
43  // never called
44  GPR_ASSERT(0);
45  return nullptr;
46  }
47 
48  UniqueTypeName type() const override {
49  static UniqueTypeName::Factory kFactory("fake");
50  return kFactory.Create();
51  }
52 
53  private:
54  int CompareImpl(const grpc_tls_certificate_provider* other) const override {
55  // TODO(yashykt): Maybe do something better here.
56  return QsortCompare(static_cast<const grpc_tls_certificate_provider*>(this),
57  other);
58  }
59 };
60 
61 class FakeCertificateProviderFactory1 : public CertificateProviderFactory {
62  public:
64  public:
65  const char* name() const override { return "fake1"; }
66 
67  std::string ToString() const override { return "{}"; }
68  };
69 
70  const char* name() const override { return "fake1"; }
71 
72  RefCountedPtr<CertificateProviderFactory::Config>
73  CreateCertificateProviderConfig(const Json& /*config_json*/,
74  grpc_error_handle* /*error*/) override {
75  return MakeRefCounted<Config>();
76  }
77 
78  RefCountedPtr<grpc_tls_certificate_provider> CreateCertificateProvider(
79  RefCountedPtr<CertificateProviderFactory::Config> /*config*/) override {
80  return MakeRefCounted<FakeCertificateProvider>();
81  }
82 };
83 
84 class FakeCertificateProviderFactory2 : public CertificateProviderFactory {
85  public:
87  public:
88  const char* name() const override { return "fake2"; }
89 
90  std::string ToString() const override { return "{}"; }
91  };
92 
93  const char* name() const override { return "fake2"; }
94 
95  RefCountedPtr<CertificateProviderFactory::Config>
96  CreateCertificateProviderConfig(const Json& /*config_json*/,
97  grpc_error_handle* /*error*/) override {
98  return MakeRefCounted<Config>();
99  }
100 
101  RefCountedPtr<grpc_tls_certificate_provider> CreateCertificateProvider(
102  RefCountedPtr<CertificateProviderFactory::Config> /*config*/) override {
103  return MakeRefCounted<FakeCertificateProvider>();
104  }
105 };
106 
107 TEST_F(CertificateProviderStoreTest, Basic) {
108  // Set up factories. (Register only one of the factories.)
109  auto* fake_factory_1 = new FakeCertificateProviderFactory1;
111  std::unique_ptr<CertificateProviderFactory>(fake_factory_1));
112  auto fake_factory_2 = absl::make_unique<FakeCertificateProviderFactory2>();
113  // Set up store
115  {"fake_plugin_1",
116  {"fake1", fake_factory_1->CreateCertificateProviderConfig(Json::Object(),
117  nullptr)}},
118  {"fake_plugin_2",
119  {"fake2", fake_factory_2->CreateCertificateProviderConfig(Json::Object(),
120  nullptr)}},
121  {"fake_plugin_3",
122  {"fake1", fake_factory_1->CreateCertificateProviderConfig(Json::Object(),
123  nullptr)}},
124  };
125  auto store = MakeOrphanable<CertificateProviderStore>(std::move(map));
126  // Test for creating certificate providers with known plugin configuration.
127  auto cert_provider_1 = store->CreateOrGetCertificateProvider("fake_plugin_1");
128  ASSERT_NE(cert_provider_1, nullptr);
129  auto cert_provider_3 = store->CreateOrGetCertificateProvider("fake_plugin_3");
130  ASSERT_NE(cert_provider_3, nullptr);
131  // Test for creating certificate provider with known plugin configuration but
132  // unregistered factory.
133  ASSERT_EQ(store->CreateOrGetCertificateProvider("fake_plugin_2"), nullptr);
134  // Test for creating certificate provider with unknown plugin configuration.
135  ASSERT_EQ(store->CreateOrGetCertificateProvider("unknown"), nullptr);
136  // Test for getting previously created certificate providers.
137  ASSERT_EQ(store->CreateOrGetCertificateProvider("fake_plugin_1"),
138  cert_provider_1);
139  ASSERT_EQ(store->CreateOrGetCertificateProvider("fake_plugin_3"),
140  cert_provider_3);
141  // Release previously created certificate providers so that the store outlasts
142  // the certificate providers.
143  cert_provider_1.reset();
144  cert_provider_3.reset();
145 }
146 
147 TEST_F(CertificateProviderStoreTest, Multithreaded) {
148  auto* fake_factory_1 = new FakeCertificateProviderFactory1;
150  std::unique_ptr<CertificateProviderFactory>(fake_factory_1));
152  {"fake_plugin_1",
153  {"fake1", fake_factory_1->CreateCertificateProviderConfig(Json::Object(),
154  nullptr)}}};
155  auto store = MakeOrphanable<CertificateProviderStore>(std::move(map));
156  // Test concurrent `CreateOrGetCertificateProvider()` with the same key.
157  std::vector<std::thread> threads;
158  threads.reserve(1000);
159  for (auto i = 0; i < 1000; i++) {
160  threads.emplace_back([&store]() {
161  for (auto i = 0; i < 10; ++i) {
162  ASSERT_NE(store->CreateOrGetCertificateProvider("fake_plugin_1"),
163  nullptr);
164  }
165  });
166  }
167  for (auto& thread : threads) {
168  thread.join();
169  }
170 }
171 
172 } // namespace
173 } // namespace testing
174 } // namespace grpc_core
175 
176 int main(int argc, char** argv) {
177  ::testing::InitGoogleTest(&argc, argv);
178  grpc::testing::TestEnvironment env(&argc, argv);
179  auto result = RUN_ALL_TESTS();
180  return result;
181 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
testing
Definition: aws_request_signer_test.cc:25
ASSERT_NE
#define ASSERT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2060
grpc_core::CertificateProviderRegistry::RegisterCertificateProviderFactory
static void RegisterCertificateProviderFactory(std::unique_ptr< CertificateProviderFactory > factory)
Definition: certificate_provider_registry.cc:85
generate.env
env
Definition: generate.py:37
certificate_provider_store.h
grpc_shutdown_blocking
GRPCAPI void grpc_shutdown_blocking(void)
Definition: init.cc:238
grpc_core
Definition: call_metric_recorder.h:31
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
setup.name
name
Definition: setup.py:542
grpc_core::testing::TEST_F
TEST_F(ServiceConfigTest, ErrorCheck1)
Definition: service_config_test.cc:192
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
threads
static uv_thread_t * threads
Definition: threadpool.c:38
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
grpc_core::CertificateProviderStore::PluginDefinitionMap
std::map< std::string, PluginDefinition > PluginDefinitionMap
Definition: certificate_provider_store.h:55
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
ToString
std::string ToString(const grpc::string_ref &r)
Definition: string_ref_helper.cc:24
Json
JSON (JavaScript Object Notation).
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:227
main
int main(int argc, char **argv)
Definition: certificate_provider_store_test.cc:176
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
test_config.h
grpc_core::Json::Object
std::map< std::string, Json > Object
Definition: src/core/lib/json/json.h:54
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_core::QsortCompare
int QsortCompare(const T &a, const T &b)
Definition: useful.h:95
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
certificate_provider_registry.h
unique_type_name.h
check_redundant_namespace_qualifiers.Config
Config
Definition: check_redundant_namespace_qualifiers.py:142
grpc_tls_certificate_provider
Definition: grpc_tls_certificate_provider.h:53
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_error
Definition: error_internal.h:42
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:52