xref: /aosp_15_r20/external/grpc-grpc/test/cpp/client/credentials_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2015 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 <memory>
20 
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 
24 #include <grpc/grpc.h>
25 #include <grpc/grpc_crl_provider.h>
26 #include <grpc/grpc_security.h>
27 #include <grpcpp/security/credentials.h>
28 #include <grpcpp/security/server_credentials.h>
29 #include <grpcpp/security/tls_credentials_options.h>
30 #include <grpcpp/server_builder.h>
31 
32 #include "src/core/lib/gpr/tmpfile.h"
33 #include "src/core/lib/gprpp/env.h"
34 #include "src/cpp/client/secure_credentials.h"
35 #include "test/cpp/util/tls_test_utils.h"
36 
37 #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
38 #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
39 #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
40 #define CRL_DIR_PATH "test/core/tsi/test_creds/crl_data/crls"
41 
42 namespace {
43 
44 constexpr const char* kRootCertName = "root_cert_name";
45 constexpr const char* kRootCertContents = "root_cert_contents";
46 constexpr const char* kIdentityCertName = "identity_cert_name";
47 constexpr const char* kIdentityCertPrivateKey = "identity_private_key";
48 constexpr const char* kIdentityCertContents = "identity_cert_contents";
49 
50 using ::grpc::experimental::CreateStaticCrlProvider;
51 using ::grpc::experimental::ExternalCertificateVerifier;
52 using ::grpc::experimental::FileWatcherCertificateProvider;
53 using ::grpc::experimental::HostNameCertificateVerifier;
54 using ::grpc::experimental::NoOpCertificateVerifier;
55 using ::grpc::experimental::StaticDataCertificateProvider;
56 using ::grpc::experimental::TlsChannelCredentialsOptions;
57 using ::grpc::experimental::TlsCredentialsOptions;
58 
59 }  // namespace
60 
61 namespace grpc {
62 namespace testing {
63 namespace {
64 
TEST(CredentialsTest,InvalidGoogleRefreshToken)65 TEST(CredentialsTest, InvalidGoogleRefreshToken) {
66   std::shared_ptr<CallCredentials> bad1 = GoogleRefreshTokenCredentials("");
67   EXPECT_EQ(static_cast<CallCredentials*>(nullptr), bad1.get());
68 }
69 
TEST(CredentialsTest,DefaultCredentials)70 TEST(CredentialsTest, DefaultCredentials) {
71   auto creds = GoogleDefaultCredentials();
72 }
73 
TEST(CredentialsTest,ExternalAccountCredentials)74 TEST(CredentialsTest, ExternalAccountCredentials) {
75   // url credentials
76   std::string url_options_string(
77       "{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_"
78       "token_type\":\"subject_token_type\",\"service_account_impersonation_"
79       "url\":\"service_account_impersonation_url\",\"token_url\":\"https://"
80       "foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/"
81       "token_info\",\"credential_source\":{\"url\":\"https://foo.com:5555/"
82       "generate_subject_token_format_json\",\"headers\":{\"Metadata-Flavor\":"
83       "\"Google\"},\"format\":{\"type\":\"json\",\"subject_token_field_name\":"
84       "\"access_token\"}},\"quota_project_id\":\"quota_"
85       "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
86       "secret\"}");
87   auto url_creds = grpc::ExternalAccountCredentials(url_options_string,
88                                                     {"scope1", "scope2"});
89   EXPECT_TRUE(url_creds != nullptr);
90   // file credentials
91   std::string file_options_string(
92       "{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_"
93       "token_type\":\"subject_token_type\",\"service_account_impersonation_"
94       "url\":\"service_account_impersonation_url\",\"token_url\":\"https://"
95       "foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/"
96       "token_info\",\"credential_source\":{\"file\":\"credentials_file_path\"},"
97       "\"quota_project_id\":\"quota_"
98       "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
99       "secret\"}");
100   auto file_creds = grpc::ExternalAccountCredentials(file_options_string,
101                                                      {"scope1", "scope2"});
102   EXPECT_TRUE(file_creds != nullptr);
103   // aws credentials
104   std::string aws_options_string(
105       "{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_"
106       "token_type\":\"subject_token_type\",\"service_account_impersonation_"
107       "url\":\"service_account_impersonation_url\",\"token_url\":\"https://"
108       "foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/"
109       "token_info\",\"credential_source\":{\"environment_id\":\"aws1\","
110       "\"region_url\":\"https://169.254.169.254:5555/"
111       "region_url\",\"url\":\"https://"
112       "169.254.169.254:5555/url\",\"regional_cred_verification_url\":\"https://"
113       "foo.com:5555/regional_cred_verification_url_{region}\"},"
114       "\"quota_project_id\":\"quota_"
115       "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
116       "secret\"}");
117   auto aws_creds = grpc::ExternalAccountCredentials(aws_options_string,
118                                                     {"scope1", "scope2"});
119   EXPECT_TRUE(aws_creds != nullptr);
120 }
121 
TEST(CredentialsTest,StsCredentialsOptionsCppToCore)122 TEST(CredentialsTest, StsCredentialsOptionsCppToCore) {
123   grpc::experimental::StsCredentialsOptions options;
124   options.token_exchange_service_uri = "https://foo.com/exchange";
125   options.resource = "resource";
126   options.audience = "audience";
127   options.scope = "scope";
128   // options.requested_token_type explicitly not set.
129   options.subject_token_path = "/foo/bar";
130   options.subject_token_type = "nice_token_type";
131   options.actor_token_path = "/foo/baz";
132   options.actor_token_type = "even_nicer_token_type";
133   grpc_sts_credentials_options core_opts =
134       grpc::experimental::StsCredentialsCppToCoreOptions(options);
135   EXPECT_EQ(options.token_exchange_service_uri,
136             core_opts.token_exchange_service_uri);
137   EXPECT_EQ(options.resource, core_opts.resource);
138   EXPECT_EQ(options.audience, core_opts.audience);
139   EXPECT_EQ(options.scope, core_opts.scope);
140   EXPECT_EQ(options.requested_token_type, core_opts.requested_token_type);
141   EXPECT_EQ(options.subject_token_path, core_opts.subject_token_path);
142   EXPECT_EQ(options.subject_token_type, core_opts.subject_token_type);
143   EXPECT_EQ(options.actor_token_path, core_opts.actor_token_path);
144   EXPECT_EQ(options.actor_token_type, core_opts.actor_token_type);
145 }
146 
TEST(CredentialsTest,StsCredentialsOptionsJson)147 TEST(CredentialsTest, StsCredentialsOptionsJson) {
148   const char valid_json[] = R"(
149   {
150     "token_exchange_service_uri": "https://foo/exchange",
151     "resource": "resource",
152     "audience": "audience",
153     "scope": "scope",
154     "requested_token_type": "requested_token_type",
155     "subject_token_path": "subject_token_path",
156     "subject_token_type": "subject_token_type",
157     "actor_token_path": "actor_token_path",
158     "actor_token_type": "actor_token_type"
159   })";
160   grpc::experimental::StsCredentialsOptions options;
161   EXPECT_TRUE(
162       grpc::experimental::StsCredentialsOptionsFromJson(valid_json, &options)
163           .ok());
164   EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange");
165   EXPECT_EQ(options.resource, "resource");
166   EXPECT_EQ(options.audience, "audience");
167   EXPECT_EQ(options.scope, "scope");
168   EXPECT_EQ(options.requested_token_type, "requested_token_type");
169   EXPECT_EQ(options.subject_token_path, "subject_token_path");
170   EXPECT_EQ(options.subject_token_type, "subject_token_type");
171   EXPECT_EQ(options.actor_token_path, "actor_token_path");
172   EXPECT_EQ(options.actor_token_type, "actor_token_type");
173 
174   const char minimum_valid_json[] = R"(
175   {
176     "token_exchange_service_uri": "https://foo/exchange",
177     "subject_token_path": "subject_token_path",
178     "subject_token_type": "subject_token_type"
179   })";
180   EXPECT_TRUE(grpc::experimental::StsCredentialsOptionsFromJson(
181                   minimum_valid_json, &options)
182                   .ok());
183   EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange");
184   EXPECT_EQ(options.resource, "");
185   EXPECT_EQ(options.audience, "");
186   EXPECT_EQ(options.scope, "");
187   EXPECT_EQ(options.requested_token_type, "");
188   EXPECT_EQ(options.subject_token_path, "subject_token_path");
189   EXPECT_EQ(options.subject_token_type, "subject_token_type");
190   EXPECT_EQ(options.actor_token_path, "");
191   EXPECT_EQ(options.actor_token_type, "");
192 
193   const char invalid_json[] = R"(
194   I'm not a valid JSON.
195   )";
196   EXPECT_EQ(
197       grpc::StatusCode::INVALID_ARGUMENT,
198       grpc::experimental::StsCredentialsOptionsFromJson(invalid_json, &options)
199           .error_code());
200 
201   const char invalid_json_missing_subject_token_type[] = R"(
202   {
203     "token_exchange_service_uri": "https://foo/exchange",
204     "subject_token_path": "subject_token_path"
205   })";
206   auto status = grpc::experimental::StsCredentialsOptionsFromJson(
207       invalid_json_missing_subject_token_type, &options);
208   EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code());
209   EXPECT_THAT(status.error_message(),
210               ::testing::HasSubstr("subject_token_type"));
211 
212   const char invalid_json_missing_subject_token_path[] = R"(
213   {
214     "token_exchange_service_uri": "https://foo/exchange",
215     "subject_token_type": "subject_token_type"
216   })";
217   status = grpc::experimental::StsCredentialsOptionsFromJson(
218       invalid_json_missing_subject_token_path, &options);
219   EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code());
220   EXPECT_THAT(status.error_message(),
221               ::testing::HasSubstr("subject_token_path"));
222 
223   const char invalid_json_missing_token_exchange_uri[] = R"(
224   {
225     "subject_token_path": "subject_token_path",
226     "subject_token_type": "subject_token_type"
227   })";
228   status = grpc::experimental::StsCredentialsOptionsFromJson(
229       invalid_json_missing_token_exchange_uri, &options);
230   EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code());
231   EXPECT_THAT(status.error_message(),
232               ::testing::HasSubstr("token_exchange_service_uri"));
233 }
234 
TEST(CredentialsTest,StsCredentialsOptionsFromEnv)235 TEST(CredentialsTest, StsCredentialsOptionsFromEnv) {
236   // Unset env and check expected failure.
237   grpc_core::UnsetEnv("STS_CREDENTIALS");
238   grpc::experimental::StsCredentialsOptions options;
239   auto status = grpc::experimental::StsCredentialsOptionsFromEnv(&options);
240   EXPECT_EQ(grpc::StatusCode::NOT_FOUND, status.error_code());
241 
242   // Set env and check for success.
243   const char valid_json[] = R"(
244   {
245     "token_exchange_service_uri": "https://foo/exchange",
246     "subject_token_path": "subject_token_path",
247     "subject_token_type": "subject_token_type"
248   })";
249   char* creds_file_name;
250   FILE* creds_file = gpr_tmpfile("sts_creds_options", &creds_file_name);
251   ASSERT_NE(creds_file_name, nullptr);
252   ASSERT_NE(creds_file, nullptr);
253   ASSERT_EQ(sizeof(valid_json),
254             fwrite(valid_json, 1, sizeof(valid_json), creds_file));
255   fclose(creds_file);
256   grpc_core::SetEnv("STS_CREDENTIALS", creds_file_name);
257   gpr_free(creds_file_name);
258   status = grpc::experimental::StsCredentialsOptionsFromEnv(&options);
259   EXPECT_TRUE(status.ok());
260   EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange");
261   EXPECT_EQ(options.resource, "");
262   EXPECT_EQ(options.audience, "");
263   EXPECT_EQ(options.scope, "");
264   EXPECT_EQ(options.requested_token_type, "");
265   EXPECT_EQ(options.subject_token_path, "subject_token_path");
266   EXPECT_EQ(options.subject_token_type, "subject_token_type");
267   EXPECT_EQ(options.actor_token_path, "");
268   EXPECT_EQ(options.actor_token_type, "");
269 
270   // Cleanup.
271   grpc_core::UnsetEnv("STS_CREDENTIALS");
272 }
273 
TEST(CredentialsTest,TlsChannelCredentialsWithDefaultRootsAndDefaultVerifier)274 TEST(CredentialsTest, TlsChannelCredentialsWithDefaultRootsAndDefaultVerifier) {
275   grpc::experimental::TlsChannelCredentialsOptions options;
276   options.set_verify_server_certs(true);
277   auto channel_credentials = grpc::experimental::TlsCredentials(options);
278   GPR_ASSERT(channel_credentials.get() != nullptr);
279 }
280 
TEST(CredentialsTest,TlsChannelCredentialsWithStaticDataCertificateProviderLoadingRootAndIdentity)281 TEST(
282     CredentialsTest,
283     TlsChannelCredentialsWithStaticDataCertificateProviderLoadingRootAndIdentity) {
284   experimental::IdentityKeyCertPair key_cert_pair;
285   key_cert_pair.private_key = kIdentityCertPrivateKey;
286   key_cert_pair.certificate_chain = kIdentityCertContents;
287   std::vector<experimental::IdentityKeyCertPair> identity_key_cert_pairs;
288   identity_key_cert_pairs.emplace_back(key_cert_pair);
289   auto certificate_provider = std::make_shared<StaticDataCertificateProvider>(
290       kRootCertContents, identity_key_cert_pairs);
291   grpc::experimental::TlsChannelCredentialsOptions options;
292   options.set_certificate_provider(certificate_provider);
293   options.watch_root_certs();
294   options.set_root_cert_name(kRootCertName);
295   options.watch_identity_key_cert_pairs();
296   options.set_identity_cert_name(kIdentityCertName);
297   auto channel_credentials = grpc::experimental::TlsCredentials(options);
298   GPR_ASSERT(channel_credentials.get() != nullptr);
299 }
300 
TEST(CredentialsTest,TlsChannelCredentialsWithStaticDataCertificateProviderLoadingRootOnly)301 TEST(CredentialsTest,
302      TlsChannelCredentialsWithStaticDataCertificateProviderLoadingRootOnly) {
303   auto certificate_provider =
304       std::make_shared<StaticDataCertificateProvider>(kRootCertContents);
305   GPR_ASSERT(certificate_provider != nullptr);
306   GPR_ASSERT(certificate_provider->c_provider() != nullptr);
307   grpc::experimental::TlsChannelCredentialsOptions options;
308   options.set_certificate_provider(certificate_provider);
309   options.watch_root_certs();
310   options.set_root_cert_name(kRootCertName);
311   auto channel_credentials = grpc::experimental::TlsCredentials(options);
312   GPR_ASSERT(channel_credentials.get() != nullptr);
313 }
314 
TEST(CredentialsTest,TlsChannelCredentialsWithDefaultRootsAndStaticDataCertificateProviderLoadingIdentityOnly)315 TEST(
316     CredentialsTest,
317     TlsChannelCredentialsWithDefaultRootsAndStaticDataCertificateProviderLoadingIdentityOnly) {
318   experimental::IdentityKeyCertPair key_cert_pair;
319   key_cert_pair.private_key = kIdentityCertPrivateKey;
320   key_cert_pair.certificate_chain = kIdentityCertContents;
321   std::vector<experimental::IdentityKeyCertPair> identity_key_cert_pairs;
322   identity_key_cert_pairs.emplace_back(key_cert_pair);
323   auto certificate_provider =
324       std::make_shared<StaticDataCertificateProvider>(identity_key_cert_pairs);
325   grpc::experimental::TlsChannelCredentialsOptions options;
326   options.set_certificate_provider(certificate_provider);
327   options.watch_identity_key_cert_pairs();
328   options.set_identity_cert_name(kIdentityCertName);
329   auto channel_credentials = grpc::experimental::TlsCredentials(options);
330   GPR_ASSERT(channel_credentials.get() != nullptr);
331 }
332 
TEST(CredentialsTest,TlsChannelCredentialsWithFileWatcherCertificateProviderLoadingRootAndIdentity)333 TEST(
334     CredentialsTest,
335     TlsChannelCredentialsWithFileWatcherCertificateProviderLoadingRootAndIdentity) {
336   auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>(
337       SERVER_KEY_PATH, SERVER_CERT_PATH, CA_CERT_PATH, 1);
338   grpc::experimental::TlsChannelCredentialsOptions options;
339   options.set_certificate_provider(certificate_provider);
340   options.watch_root_certs();
341   options.set_root_cert_name(kRootCertName);
342   options.watch_identity_key_cert_pairs();
343   options.set_identity_cert_name(kIdentityCertName);
344   auto channel_credentials = grpc::experimental::TlsCredentials(options);
345   GPR_ASSERT(channel_credentials.get() != nullptr);
346 }
347 
TEST(CredentialsTest,TlsChannelCredentialsWithFileWatcherCertificateProviderLoadingRootOnly)348 TEST(CredentialsTest,
349      TlsChannelCredentialsWithFileWatcherCertificateProviderLoadingRootOnly) {
350   auto certificate_provider =
351       std::make_shared<FileWatcherCertificateProvider>(CA_CERT_PATH, 1);
352   grpc::experimental::TlsChannelCredentialsOptions options;
353   options.set_certificate_provider(certificate_provider);
354   options.watch_root_certs();
355   options.set_root_cert_name(kRootCertName);
356   auto channel_credentials = grpc::experimental::TlsCredentials(options);
357   GPR_ASSERT(channel_credentials.get() != nullptr);
358 }
359 
TEST(CredentialsTest,TlsChannelCredentialsWithHostNameVerifier)360 TEST(CredentialsTest, TlsChannelCredentialsWithHostNameVerifier) {
361   auto verifier = std::make_shared<HostNameCertificateVerifier>();
362   grpc::experimental::TlsChannelCredentialsOptions options;
363   options.set_verify_server_certs(true);
364   options.set_certificate_verifier(verifier);
365   auto channel_credentials = grpc::experimental::TlsCredentials(options);
366   GPR_ASSERT(channel_credentials.get() != nullptr);
367 }
368 
TEST(CredentialsTest,TlsChannelCredentialsWithSyncExternalVerifier)369 TEST(CredentialsTest, TlsChannelCredentialsWithSyncExternalVerifier) {
370   auto verifier =
371       ExternalCertificateVerifier::Create<SyncCertificateVerifier>(true);
372   grpc::experimental::TlsChannelCredentialsOptions options;
373   options.set_verify_server_certs(true);
374   options.set_certificate_verifier(verifier);
375   options.set_check_call_host(false);
376   auto channel_credentials = grpc::experimental::TlsCredentials(options);
377   GPR_ASSERT(channel_credentials.get() != nullptr);
378 }
379 
TEST(CredentialsTest,TlsChannelCredentialsWithAsyncExternalVerifier)380 TEST(CredentialsTest, TlsChannelCredentialsWithAsyncExternalVerifier) {
381   auto verifier =
382       ExternalCertificateVerifier::Create<AsyncCertificateVerifier>(true);
383   grpc::experimental::TlsChannelCredentialsOptions options;
384   options.set_verify_server_certs(true);
385   options.set_certificate_verifier(verifier);
386   options.set_check_call_host(false);
387   auto channel_credentials = grpc::experimental::TlsCredentials(options);
388   GPR_ASSERT(channel_credentials.get() != nullptr);
389 }
390 
TEST(CredentialsTest,TlsChannelCredentialsWithCrlDirectory)391 TEST(CredentialsTest, TlsChannelCredentialsWithCrlDirectory) {
392   auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>(
393       SERVER_KEY_PATH, SERVER_CERT_PATH, CA_CERT_PATH, 1);
394   grpc::experimental::TlsChannelCredentialsOptions options;
395   options.set_certificate_provider(certificate_provider);
396   options.watch_root_certs();
397   options.set_root_cert_name(kRootCertName);
398   options.watch_identity_key_cert_pairs();
399   options.set_identity_cert_name(kIdentityCertName);
400   options.set_crl_directory(CRL_DIR_PATH);
401   auto channel_credentials = grpc::experimental::TlsCredentials(options);
402   GPR_ASSERT(channel_credentials.get() != nullptr);
403 }
404 
TEST(CredentialsTest,TlsChannelCredentialsWithCrlProvider)405 TEST(CredentialsTest, TlsChannelCredentialsWithCrlProvider) {
406   auto provider = experimental::CreateStaticCrlProvider({});
407   ASSERT_TRUE(provider.ok());
408   grpc::experimental::TlsChannelCredentialsOptions options;
409   options.set_crl_provider(*provider);
410   auto channel_credentials = grpc::experimental::TlsCredentials(options);
411   GPR_ASSERT(channel_credentials.get() != nullptr);
412 }
413 
TEST(CredentialsTest,TlsChannelCredentialsWithCrlProviderAndDirectory)414 TEST(CredentialsTest, TlsChannelCredentialsWithCrlProviderAndDirectory) {
415   auto provider = experimental::CreateStaticCrlProvider({});
416   ASSERT_TRUE(provider.ok());
417   grpc::experimental::TlsChannelCredentialsOptions options;
418   options.set_crl_directory(CRL_DIR_PATH);
419   options.set_crl_provider(*provider);
420   auto channel_credentials = grpc::experimental::TlsCredentials(options);
421   // TODO(gtcooke94) - behavior might change to make this return nullptr in the
422   // future
423   GPR_ASSERT(channel_credentials.get() != nullptr);
424 }
425 
TEST(CredentialsTest,TlsCredentialsOptionsCopyConstructor)426 TEST(CredentialsTest, TlsCredentialsOptionsCopyConstructor) {
427   // define a class that grants access to the internal
428   // grpc_tls_credentials_options pointer
429   class TlsTestCredentialsOptions
430       : public grpc::experimental::TlsCredentialsOptions {
431    public:
432     grpc_tls_credentials_options* internal_cred_opts() {
433       return mutable_c_credentials_options();
434     }
435   };
436   TlsTestCredentialsOptions options;
437   TlsTestCredentialsOptions copied_options = options;
438 
439   // Make sure the copy constructor cloned the internal pointer
440   GPR_ASSERT(options.internal_cred_opts() !=
441              copied_options.internal_cred_opts());
442 }
443 
TEST(CredentialsTest,TlsCredentialsOptionsDoesNotLeak)444 TEST(CredentialsTest, TlsCredentialsOptionsDoesNotLeak) {
445   TlsCredentialsOptions options;
446   (void)options;
447 }
448 
TEST(CredentialsTest,MultipleOptionsOneCertificateProviderDoesNotLeak)449 TEST(CredentialsTest, MultipleOptionsOneCertificateProviderDoesNotLeak) {
450   auto provider = std::make_shared<StaticDataCertificateProvider>("root-pem");
451   TlsCredentialsOptions options_1;
452   options_1.set_certificate_provider(provider);
453   TlsCredentialsOptions options_2;
454   options_2.set_certificate_provider(provider);
455 }
456 
TEST(CredentialsTest,MultipleOptionsOneCertificateVerifierDoesNotLeak)457 TEST(CredentialsTest, MultipleOptionsOneCertificateVerifierDoesNotLeak) {
458   auto verifier = std::make_shared<NoOpCertificateVerifier>();
459   TlsCredentialsOptions options_1;
460   options_1.set_certificate_verifier(verifier);
461   TlsCredentialsOptions options_2;
462   options_2.set_certificate_verifier(verifier);
463 }
464 
TEST(CredentialsTest,MultipleOptionsOneCrlProviderDoesNotLeak)465 TEST(CredentialsTest, MultipleOptionsOneCrlProviderDoesNotLeak) {
466   auto crl_provider = CreateStaticCrlProvider(/*crls=*/{});
467   EXPECT_TRUE(crl_provider.ok());
468   TlsCredentialsOptions options_1;
469   options_1.set_crl_provider(*crl_provider);
470   TlsCredentialsOptions options_2;
471   options_2.set_crl_provider(*crl_provider);
472 }
473 
TEST(CredentialsTest,TlsChannelCredentialsDoesNotLeak)474 TEST(CredentialsTest, TlsChannelCredentialsDoesNotLeak) {
475   TlsChannelCredentialsOptions options;
476   auto channel_creds = TlsCredentials(options);
477   EXPECT_NE(channel_creds, nullptr);
478 }
479 
TEST(CredentialsTest,MultipleChannelCredentialsSameOptionsDoesNotLeak)480 TEST(CredentialsTest, MultipleChannelCredentialsSameOptionsDoesNotLeak) {
481   TlsChannelCredentialsOptions options;
482   auto channel_creds_1 = TlsCredentials(options);
483   EXPECT_NE(channel_creds_1, nullptr);
484   auto channel_creds_2 = TlsCredentials(options);
485   EXPECT_NE(channel_creds_2, nullptr);
486 }
487 
TEST(CredentialsTest,MultipleChannelCredentialsOneCertificateProviderDoesNotLeak)488 TEST(CredentialsTest,
489      MultipleChannelCredentialsOneCertificateProviderDoesNotLeak) {
490   TlsChannelCredentialsOptions options;
491   auto provider = std::make_shared<StaticDataCertificateProvider>("root-pem");
492   options.set_certificate_provider(provider);
493   auto channel_creds_1 = TlsCredentials(options);
494   EXPECT_NE(channel_creds_1, nullptr);
495   auto channel_creds_2 = TlsCredentials(options);
496   EXPECT_NE(channel_creds_2, nullptr);
497 }
498 
TEST(CredentialsTest,MultipleChannelCredentialsOneCertificateVerifierDoesNotLeak)499 TEST(CredentialsTest,
500      MultipleChannelCredentialsOneCertificateVerifierDoesNotLeak) {
501   TlsChannelCredentialsOptions options;
502   auto verifier = std::make_shared<NoOpCertificateVerifier>();
503   options.set_certificate_verifier(verifier);
504   auto channel_creds_1 = TlsCredentials(options);
505   EXPECT_NE(channel_creds_1, nullptr);
506   auto channel_creds_2 = TlsCredentials(options);
507   EXPECT_NE(channel_creds_2, nullptr);
508 }
509 
TEST(CredentialsTest,MultipleChannelCredentialsOneCrlProviderDoesNotLeak)510 TEST(CredentialsTest, MultipleChannelCredentialsOneCrlProviderDoesNotLeak) {
511   TlsChannelCredentialsOptions options;
512   auto provider = CreateStaticCrlProvider(/*crls=*/{});
513   EXPECT_TRUE(provider.ok());
514   options.set_crl_provider(*provider);
515   auto channel_creds_1 = TlsCredentials(options);
516   EXPECT_NE(channel_creds_1, nullptr);
517   auto channel_creds_2 = TlsCredentials(options);
518   EXPECT_NE(channel_creds_2, nullptr);
519 }
520 
TEST(CredentialsTest,TlsChannelCredentialsWithGoodMinAndMaxTlsVersions)521 TEST(CredentialsTest, TlsChannelCredentialsWithGoodMinAndMaxTlsVersions) {
522   grpc::experimental::TlsChannelCredentialsOptions options;
523   options.set_min_tls_version(grpc_tls_version::TLS1_2);
524   options.set_max_tls_version(grpc_tls_version::TLS1_3);
525   auto channel_credentials = grpc::experimental::TlsCredentials(options);
526   EXPECT_NE(channel_credentials, nullptr);
527 }
528 
TEST(CredentialsTest,TlsChannelCredentialsWithBadMinAndMaxTlsVersions)529 TEST(CredentialsTest, TlsChannelCredentialsWithBadMinAndMaxTlsVersions) {
530   grpc::experimental::TlsChannelCredentialsOptions options;
531   options.set_min_tls_version(grpc_tls_version::TLS1_3);
532   options.set_max_tls_version(grpc_tls_version::TLS1_2);
533   auto channel_credentials = grpc::experimental::TlsCredentials(options);
534   EXPECT_EQ(channel_credentials, nullptr);
535 }
536 
537 }  // namespace
538 }  // namespace testing
539 }  // namespace grpc
540 
main(int argc,char ** argv)541 int main(int argc, char** argv) {
542   ::testing::InitGoogleTest(&argc, argv);
543   int ret = RUN_ALL_TESTS();
544   return ret;
545 }
546