| 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/ext/xds/certificate_provider_store.h" |
| 20 |
|
| 21 |
#include <algorithm> |
| 22 |
#include <memory> |
| 23 |
#include <thread> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
#include "gtest/gtest.h" |
| 27 |
|
| 28 |
#include <grpc/grpc.h> |
| 29 |
#include <grpc/support/log.h> |
| 30 |
|
| 31 |
#include "src/core/lib/config/core_configuration.h" |
| 32 |
#include "src/core/lib/gprpp/unique_type_name.h" |
| 33 |
#include "src/core/lib/iomgr/error.h" |
| 34 |
#include "test/core/util/test_config.h" |
| 35 |
|
| 36 |
namespace grpc_core { |
| 37 |
namespace testing { |
| 38 |
namespace { |
| 39 |
|
| 40 |
class CertificateProviderStoreTest : public ::testing::Test { |
| 41 |
public: |
| 42 |
CertificateProviderStoreTest() { grpc_init(); } |
| 43 |
|
| 44 |
~CertificateProviderStoreTest() override { grpc_shutdown_blocking(); } |
| 45 |
}; |
| 46 |
|
| 47 |
class FakeCertificateProvider : public grpc_tls_certificate_provider { |
| 48 |
public: |
| 49 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor() const override { |
| 50 |
// never called |
| 51 |
GPR_ASSERT(0); |
| 52 |
return nullptr; |
| 53 |
} |
| 54 |
|
| 55 |
UniqueTypeName type() const override { |
| 56 |
static UniqueTypeName::Factory kFactory("fake"); |
| 57 |
return kFactory.Create(); |
| 58 |
} |
| 59 |
|
| 60 |
private: |
| 61 |
int CompareImpl(const grpc_tls_certificate_provider* other) const override { |
| 62 |
// TODO(yashykt): Maybe do something better here. |
| 63 |
return QsortCompare(static_cast<const grpc_tls_certificate_provider*>(this), |
| 64 |
other); |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
class FakeCertificateProviderFactory1 : public CertificateProviderFactory { |
| 69 |
public: |
| 70 |
class Config : public CertificateProviderFactory::Config { |
| 71 |
public: |
| 72 |
const char* name() const override { return "fake1"; } |
| 73 |
|
| 74 |
std::string ToString() const override { return "{}"; } |
| 75 |
}; |
| 76 |
|
| 77 |
const char* name() const override { return "fake1"; } |
| 78 |
|
| 79 |
RefCountedPtr<CertificateProviderFactory::Config> |
| 80 |
CreateCertificateProviderConfig(const Json& /*config_json*/, |
| 81 |
grpc_error_handle* /*error*/) override { |
| 82 |
return MakeRefCounted<Config>(); |
| 83 |
} |
| 84 |
|
| 85 |
RefCountedPtr<grpc_tls_certificate_provider> CreateCertificateProvider( |
| 86 |
RefCountedPtr<CertificateProviderFactory::Config> /*config*/) override { |
| 87 |
return MakeRefCounted<FakeCertificateProvider>(); |
| 88 |
} |
| 89 |
}; |
| 90 |
|
| 91 |
class FakeCertificateProviderFactory2 : public CertificateProviderFactory { |
| 92 |
public: |
| 93 |
class Config : public CertificateProviderFactory::Config { |
| 94 |
public: |
| 95 |
const char* name() const override { return "fake2"; } |
| 96 |
|
| 97 |
std::string ToString() const override { return "{}"; } |
| 98 |
}; |
| 99 |
|
| 100 |
const char* name() const override { return "fake2"; } |
| 101 |
|
| 102 |
RefCountedPtr<CertificateProviderFactory::Config> |
| 103 |
CreateCertificateProviderConfig(const Json& /*config_json*/, |
| 104 |
grpc_error_handle* /*error*/) override { |
| 105 |
return MakeRefCounted<Config>(); |
| 106 |
} |
| 107 |
|
| 108 |
RefCountedPtr<grpc_tls_certificate_provider> CreateCertificateProvider( |
| 109 |
RefCountedPtr<CertificateProviderFactory::Config> /*config*/) override { |
| 110 |
return MakeRefCounted<FakeCertificateProvider>(); |
| 111 |
} |
| 112 |
}; |
| 113 |
|
| 114 |
TEST_F(CertificateProviderStoreTest, Basic) { |
| 115 |
// Set up factories. (Register only one of the factories.) |
| 116 |
auto* fake_factory_1 = new FakeCertificateProviderFactory1; |
| 117 |
CoreConfiguration::RunWithSpecialConfiguration( |
| 118 |
[=](CoreConfiguration::Builder* builder) { |
| 119 |
builder->certificate_provider_registry() |
| 120 |
->RegisterCertificateProviderFactory( |
| 121 |
std::unique_ptr<CertificateProviderFactory>(fake_factory_1)); |
| 122 |
}, |
| 123 |
[=] { |
| 124 |
auto fake_factory_2 = |
| 125 |
std::make_unique<FakeCertificateProviderFactory2>(); |
| 126 |
// Set up store |
| 127 |
CertificateProviderStore::PluginDefinitionMap map = { |
| 128 |
{"fake_plugin_1", |
| 129 |
{"fake1", fake_factory_1->CreateCertificateProviderConfig( |
| 130 |
Json::Object(), nullptr)}}, |
| 131 |
{"fake_plugin_2", |
| 132 |
{"fake2", fake_factory_2->CreateCertificateProviderConfig( |
| 133 |
Json::Object(), nullptr)}}, |
| 134 |
{"fake_plugin_3", |
| 135 |
{"fake1", fake_factory_1->CreateCertificateProviderConfig( |
| 136 |
Json::Object(), nullptr)}}, |
| 137 |
}; |
| 138 |
auto store = MakeOrphanable<CertificateProviderStore>(std::move(map)); |
| 139 |
// Test for creating certificate providers with known plugin |
| 140 |
// configuration. |
| 141 |
auto cert_provider_1 = |
| 142 |
store->CreateOrGetCertificateProvider("fake_plugin_1"); |
| 143 |
ASSERT_NE(cert_provider_1, nullptr); |
| 144 |
auto cert_provider_3 = |
| 145 |
store->CreateOrGetCertificateProvider("fake_plugin_3"); |
| 146 |
ASSERT_NE(cert_provider_3, nullptr); |
| 147 |
// Test for creating certificate provider with known plugin |
| 148 |
// configuration but unregistered factory. |
| 149 |
ASSERT_EQ(store->CreateOrGetCertificateProvider("fake_plugin_2"), |
| 150 |
nullptr); |
| 151 |
// Test for creating certificate provider with unknown plugin |
| 152 |
// configuration. |
| 153 |
ASSERT_EQ(store->CreateOrGetCertificateProvider("unknown"), nullptr); |
| 154 |
// Test for getting previously created certificate providers. |
| 155 |
ASSERT_EQ(store->CreateOrGetCertificateProvider("fake_plugin_1"), |
| 156 |
cert_provider_1); |
| 157 |
ASSERT_EQ(store->CreateOrGetCertificateProvider("fake_plugin_3"), |
| 158 |
cert_provider_3); |
| 159 |
// Release previously created certificate providers so that the store |
| 160 |
// outlasts the certificate providers. |
| 161 |
cert_provider_1.reset(); |
| 162 |
cert_provider_3.reset(); |
| 163 |
}); |
| 164 |
} |
| 165 |
|
| 166 |
TEST_F(CertificateProviderStoreTest, Multithreaded) { |
| 167 |
auto* fake_factory_1 = new FakeCertificateProviderFactory1; |
| 168 |
CoreConfiguration::RunWithSpecialConfiguration( |
| 169 |
[=](CoreConfiguration::Builder* builder) { |
| 170 |
builder->certificate_provider_registry() |
| 171 |
->RegisterCertificateProviderFactory( |
| 172 |
std::unique_ptr<CertificateProviderFactory>(fake_factory_1)); |
| 173 |
}, |
| 174 |
[=] { |
| 175 |
CertificateProviderStore::PluginDefinitionMap map = { |
| 176 |
{"fake_plugin_1", |
| 177 |
{"fake1", fake_factory_1->CreateCertificateProviderConfig( |
| 178 |
Json::Object(), nullptr)}}}; |
| 179 |
auto store = MakeOrphanable<CertificateProviderStore>(std::move(map)); |
| 180 |
// Test concurrent `CreateOrGetCertificateProvider()` with the same key. |
| 181 |
std::vector<std::thread> threads; |
| 182 |
threads.reserve(1000); |
| 183 |
for (auto i = 0; i < 1000; i++) { |
| 184 |
threads.emplace_back([&store]() { |
| 185 |
for (auto i = 0; i < 10; ++i) { |
| 186 |
ASSERT_NE(store->CreateOrGetCertificateProvider("fake_plugin_1"), |
| 187 |
nullptr); |
| 188 |
} |
| 189 |
}); |
| 190 |
} |
| 191 |
for (auto& thread : threads) { |
| 192 |
thread.join(); |
| 193 |
} |
| 194 |
}); |
| 195 |
} |
| 196 |
|
| 197 |
} // namespace |
| 198 |
} // namespace testing |
| 199 |
} // namespace grpc_core |
| 200 |
|
| 201 |
int main(int argc, char** argv) { |
| 202 |
::testing::InitGoogleTest(&argc, argv); |
| 203 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 204 |
auto result = RUN_ALL_TESTS(); |
| 205 |
return result; |
| 206 |
} |