21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
31 constexpr
const char* kRootCert1 =
"root_cert_1_contents";
32 constexpr
const char* kRootCert2 =
"root_cert_2_contents";
34 constexpr
const char* kIdentityCert1 =
"identity_cert_1_contents";
36 constexpr
const char* kIdentityCert2 =
"identity_cert_2_contents";
48 class TestCertificatesWatcher
51 ~TestCertificatesWatcher()
override {
56 void OnCertificatesChanged(
103 XdsCertificateProviderTest,
104 RootCertDistributorDifferentFromIdentityCertDistributorDifferentCertNames) {
105 auto root_cert_distributor =
106 MakeRefCounted<grpc_tls_certificate_distributor>();
107 auto identity_cert_distributor =
108 MakeRefCounted<grpc_tls_certificate_distributor>();
109 XdsCertificateProvider provider;
110 provider.UpdateRootCertNameAndDistributor(
"",
"root", root_cert_distributor);
111 provider.UpdateIdentityCertNameAndDistributor(
"",
"identity",
112 identity_cert_distributor);
113 auto*
watcher =
new TestCertificatesWatcher;
114 provider.distributor()->WatchTlsCertificates(
115 std::unique_ptr<TestCertificatesWatcher>(
watcher),
"",
"");
121 root_cert_distributor->SetKeyMaterials(
"root", kRootCert1, absl::nullopt);
122 identity_cert_distributor->SetKeyMaterials(
"identity", absl::nullopt,
123 MakeKeyCertPairsType1());
129 root_cert_distributor->SetKeyMaterials(
131 MakeKeyCertPairsType2() );
137 identity_cert_distributor->SetKeyMaterials(
138 "identity", kRootCert1 ,
139 MakeKeyCertPairsType2());
145 root_cert_distributor->SetErrorForCert(
148 identity_cert_distributor->SetErrorForCert(
149 "identity", absl::nullopt,
158 root_cert_distributor->SetKeyMaterials(
"root", kRootCert1, absl::nullopt);
166 identity_cert_distributor->SetKeyMaterials(
"identity", absl::nullopt,
167 MakeKeyCertPairsType1());
174 TEST(XdsCertificateProviderTest,
175 RootCertDistributorDifferentFromIdentityCertDistributorSameCertNames) {
176 auto root_cert_distributor =
177 MakeRefCounted<grpc_tls_certificate_distributor>();
178 auto identity_cert_distributor =
179 MakeRefCounted<grpc_tls_certificate_distributor>();
180 XdsCertificateProvider provider;
181 provider.UpdateRootCertNameAndDistributor(
"",
"test", root_cert_distributor);
182 provider.UpdateIdentityCertNameAndDistributor(
"",
"test",
183 identity_cert_distributor);
184 auto*
watcher =
new TestCertificatesWatcher;
185 provider.distributor()->WatchTlsCertificates(
186 std::unique_ptr<TestCertificatesWatcher>(
watcher),
"",
"");
192 root_cert_distributor->SetKeyMaterials(
"test", kRootCert1, absl::nullopt);
193 identity_cert_distributor->SetKeyMaterials(
"test", absl::nullopt,
194 MakeKeyCertPairsType1());
200 root_cert_distributor->SetKeyMaterials(
"test", kRootCert2, absl::nullopt);
206 identity_cert_distributor->SetKeyMaterials(
"test", absl::nullopt,
207 MakeKeyCertPairsType2());
213 root_cert_distributor->SetErrorForCert(
216 identity_cert_distributor->SetErrorForCert(
217 "test", absl::nullopt,
226 root_cert_distributor->SetKeyMaterials(
"test", kRootCert1, absl::nullopt);
234 identity_cert_distributor->SetKeyMaterials(
"test", absl::nullopt,
235 MakeKeyCertPairsType1());
241 identity_cert_distributor->SetKeyMaterials(
"identity", kRootCert2,
242 MakeKeyCertPairsType2());
243 root_cert_distributor->SetKeyMaterials(
"root", kRootCert1,
244 MakeKeyCertPairsType1());
247 TEST(XdsCertificateProviderTest,
248 RootCertDistributorSameAsIdentityCertDistributorDifferentCertNames) {
249 auto distributor = MakeRefCounted<grpc_tls_certificate_distributor>();
250 XdsCertificateProvider provider;
251 provider.UpdateRootCertNameAndDistributor(
"",
"root", distributor);
252 provider.UpdateIdentityCertNameAndDistributor(
"",
"identity", distributor);
253 auto*
watcher =
new TestCertificatesWatcher;
254 provider.distributor()->WatchTlsCertificates(
255 std::unique_ptr<TestCertificatesWatcher>(
watcher),
"",
"");
261 distributor->SetKeyMaterials(
"root", kRootCert1, MakeKeyCertPairsType2());
262 distributor->SetKeyMaterials(
"identity", kRootCert2, MakeKeyCertPairsType1());
268 distributor->SetKeyMaterials(
"root", kRootCert2, MakeKeyCertPairsType2());
274 distributor->SetKeyMaterials(
"identity", kRootCert1, MakeKeyCertPairsType2());
280 distributor->SetErrorForCert(
288 distributor->SetErrorForCert(
298 distributor->SetKeyMaterials(
"root", kRootCert1, MakeKeyCertPairsType1());
305 distributor->SetKeyMaterials(
"identity", kRootCert2, MakeKeyCertPairsType1());
312 TEST(XdsCertificateProviderTest,
313 RootCertDistributorSameAsIdentityCertDistributorSameCertNames) {
314 auto distributor = MakeRefCounted<grpc_tls_certificate_distributor>();
315 XdsCertificateProvider provider;
316 provider.UpdateRootCertNameAndDistributor(
"",
"", distributor);
317 provider.UpdateIdentityCertNameAndDistributor(
"",
"", distributor);
318 auto*
watcher =
new TestCertificatesWatcher;
319 provider.distributor()->WatchTlsCertificates(
320 std::unique_ptr<TestCertificatesWatcher>(
watcher),
"",
"");
326 distributor->SetKeyMaterials(
"", kRootCert1, MakeKeyCertPairsType1());
332 distributor->SetKeyMaterials(
"", kRootCert2, absl::nullopt);
338 distributor->SetKeyMaterials(
"", absl::nullopt, MakeKeyCertPairsType2());
344 distributor->SetErrorForCert(
353 distributor->SetErrorForCert(
363 distributor->SetKeyMaterials(
"", kRootCert1, absl::nullopt);
370 distributor->SetKeyMaterials(
"", absl::nullopt, MakeKeyCertPairsType1());
377 TEST(XdsCertificateProviderTest, SwapOutDistributorsMultipleTimes) {
378 auto distributor = MakeRefCounted<grpc_tls_certificate_distributor>();
379 distributor->SetKeyMaterials(
"", kRootCert1, MakeKeyCertPairsType1());
380 XdsCertificateProvider provider;
381 auto*
watcher =
new TestCertificatesWatcher;
382 provider.distributor()->WatchTlsCertificates(
383 std::unique_ptr<TestCertificatesWatcher>(
watcher),
"",
"");
389 "No certificate provider available for root certificates"));
393 "No certificate provider available for identity certificates"));
395 provider.UpdateRootCertNameAndDistributor(
"",
"", distributor);
402 "No certificate provider available for identity certificates"));
404 provider.UpdateIdentityCertNameAndDistributor(
"",
"", distributor);
410 distributor->SetKeyMaterials(
"", kRootCert2, MakeKeyCertPairsType2());
416 distributor->SetErrorForCert(
426 distributor->SetKeyMaterials(
"", kRootCert1, MakeKeyCertPairsType1());
432 provider.UpdateRootCertNameAndDistributor(
"",
"",
nullptr);
433 distributor->SetKeyMaterials(
"", kRootCert2, MakeKeyCertPairsType2());
438 "No certificate provider available for root certificates"));
441 provider.UpdateIdentityCertNameAndDistributor(
"",
"",
nullptr);
442 distributor->SetKeyMaterials(
"", kRootCert1, MakeKeyCertPairsType1());
447 "No certificate provider available for root certificates"));
451 "No certificate provider available for identity certificates"));
453 provider.UpdateRootCertNameAndDistributor(
"",
"root", distributor);
454 provider.UpdateIdentityCertNameAndDistributor(
"",
"identity", distributor);
459 "No certificate provider available for root certificates"));
463 "No certificate provider available for identity certificates"));
465 distributor->SetKeyMaterials(
"root", kRootCert2, absl::nullopt);
466 distributor->SetKeyMaterials(
"identity", absl::nullopt,
467 MakeKeyCertPairsType1());
474 auto root_cert_distributor =
475 MakeRefCounted<grpc_tls_certificate_distributor>();
476 auto identity_cert_distributor =
477 MakeRefCounted<grpc_tls_certificate_distributor>();
478 provider.UpdateRootCertNameAndDistributor(
"",
"root", root_cert_distributor);
479 provider.UpdateIdentityCertNameAndDistributor(
"",
"identity",
480 identity_cert_distributor);
486 provider.UpdateRootCertNameAndDistributor(
"",
"test", root_cert_distributor);
487 provider.UpdateIdentityCertNameAndDistributor(
"",
"test",
488 identity_cert_distributor);
494 root_cert_distributor->SetKeyMaterials(
"test", kRootCert1,
495 MakeKeyCertPairsType1());
496 identity_cert_distributor->SetKeyMaterials(
"test", kRootCert2,
497 MakeKeyCertPairsType2());
504 TEST(XdsCertificateProviderTest, MultipleCertNames) {
505 XdsCertificateProvider provider;
508 auto* watcher1 =
new TestCertificatesWatcher;
509 provider.distributor()->WatchTlsCertificates(
510 std::unique_ptr<TestCertificatesWatcher>(watcher1),
"test1",
"test1");
511 EXPECT_EQ(watcher1->root_certs(), absl::nullopt);
512 EXPECT_EQ(watcher1->key_cert_pairs(), absl::nullopt);
515 "No certificate provider available for root certificates"));
519 "No certificate provider available for identity certificates"));
521 auto cert_distributor1 = MakeRefCounted<grpc_tls_certificate_distributor>();
522 cert_distributor1->SetKeyMaterials(
"root", kRootCert1, absl::nullopt);
523 cert_distributor1->SetKeyMaterials(
"identity", absl::nullopt,
524 MakeKeyCertPairsType1());
525 provider.UpdateRootCertNameAndDistributor(
"test1",
"root", cert_distributor1);
526 provider.UpdateIdentityCertNameAndDistributor(
"test1",
"identity",
528 EXPECT_EQ(watcher1->root_certs(), kRootCert1);
529 EXPECT_EQ(watcher1->key_cert_pairs(), MakeKeyCertPairsType1());
533 auto cert_distributor2 = MakeRefCounted<grpc_tls_certificate_distributor>();
534 cert_distributor2->SetKeyMaterials(
"root2", kRootCert2, absl::nullopt);
535 cert_distributor2->SetKeyMaterials(
"identity2", absl::nullopt,
536 MakeKeyCertPairsType2());
537 provider.UpdateRootCertNameAndDistributor(
"test2",
"root2",
539 provider.UpdateIdentityCertNameAndDistributor(
"test2",
"identity2",
542 auto* watcher2 =
new TestCertificatesWatcher;
543 provider.distributor()->WatchTlsCertificates(
544 std::unique_ptr<TestCertificatesWatcher>(watcher2),
"test2",
"test2");
545 EXPECT_EQ(watcher2->root_certs(), kRootCert2);
546 EXPECT_EQ(watcher2->key_cert_pairs(), MakeKeyCertPairsType2());
550 EXPECT_EQ(watcher1->root_certs(), kRootCert1);
551 EXPECT_EQ(watcher1->key_cert_pairs(), MakeKeyCertPairsType1());
556 TEST(XdsCertificateProviderTest, UnknownCertName) {
557 XdsCertificateProvider provider;
558 auto*
watcher =
new TestCertificatesWatcher;
559 provider.distributor()->WatchTlsCertificates(
560 std::unique_ptr<TestCertificatesWatcher>(
watcher),
"test",
"test");
563 "No certificate provider available for root certificates"));
567 "No certificate provider available for identity certificates"));
574 int main(
int argc,
char** argv) {