cpp/server/credentials_test.cc
Go to the documentation of this file.
1 //
2 // Copyright 2020 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include <memory>
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <grpc/grpc.h>
23 #include <grpc/grpc_security.h>
26 
28 #include "test/core/util/port.h"
31 
32 #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
33 #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
34 #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
35 #define CRL_DIR_PATH "test/core/tsi/test_creds/crl_data"
36 
37 namespace {
38 
39 constexpr const char* kRootCertName = "root_cert_name";
40 constexpr const char* kRootCertContents = "root_cert_contents";
41 constexpr const char* kIdentityCertName = "identity_cert_name";
42 constexpr const char* kIdentityCertPrivateKey = "identity_private_key";
43 constexpr const char* kIdentityCertContents = "identity_cert_contents";
44 
45 using ::grpc::experimental::ExternalCertificateVerifier;
46 using ::grpc::experimental::FileWatcherCertificateProvider;
47 using ::grpc::experimental::StaticDataCertificateProvider;
48 
49 } // namespace
50 
51 namespace grpc {
52 namespace testing {
53 namespace {
54 
55 TEST(
56  CredentialsTest,
57  TlsServerCredentialsWithStaticDataCertificateProviderLoadingRootAndIdentity) {
58  experimental::IdentityKeyCertPair key_cert_pair;
59  key_cert_pair.private_key = kIdentityCertPrivateKey;
60  key_cert_pair.certificate_chain = kIdentityCertContents;
61  std::vector<experimental::IdentityKeyCertPair> identity_key_cert_pairs;
62  identity_key_cert_pairs.emplace_back(key_cert_pair);
63  auto certificate_provider = std::make_shared<StaticDataCertificateProvider>(
64  kRootCertContents, identity_key_cert_pairs);
66  options.watch_root_certs();
67  options.set_root_cert_name(kRootCertName);
68  options.watch_identity_key_cert_pairs();
69  options.set_identity_cert_name(kIdentityCertName);
70  options.set_cert_request_type(
72  auto server_credentials = grpc::experimental::TlsServerCredentials(options);
73  GPR_ASSERT(server_credentials.get() != nullptr);
74 }
75 
76 // ServerCredentials should always have identity credential presented.
77 // Otherwise gRPC stack will fail.
78 TEST(CredentialsTest,
79  TlsServerCredentialsWithStaticDataCertificateProviderLoadingIdentityOnly) {
80  experimental::IdentityKeyCertPair key_cert_pair;
81  key_cert_pair.private_key = kIdentityCertPrivateKey;
82  key_cert_pair.certificate_chain = kIdentityCertContents;
83  std::vector<experimental::IdentityKeyCertPair> identity_key_cert_pairs;
84  // Adding two key_cert_pair(s) should still work.
85  identity_key_cert_pairs.emplace_back(key_cert_pair);
86  identity_key_cert_pairs.emplace_back(key_cert_pair);
87  auto certificate_provider =
88  std::make_shared<StaticDataCertificateProvider>(identity_key_cert_pairs);
90  options.watch_identity_key_cert_pairs();
91  options.set_identity_cert_name(kIdentityCertName);
92  options.set_cert_request_type(
94  auto server_credentials = grpc::experimental::TlsServerCredentials(options);
95  GPR_ASSERT(server_credentials.get() != nullptr);
96 }
97 
98 TEST(
99  CredentialsTest,
100  TlsServerCredentialsWithFileWatcherCertificateProviderLoadingRootAndIdentity) {
101  auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>(
104  options.watch_root_certs();
105  options.set_root_cert_name(kRootCertName);
106  options.watch_identity_key_cert_pairs();
107  options.set_identity_cert_name(kIdentityCertName);
108  options.set_cert_request_type(
110  auto server_credentials = grpc::experimental::TlsServerCredentials(options);
111  GPR_ASSERT(server_credentials.get() != nullptr);
112 }
113 
114 TEST(CredentialsTest, TlsServerCredentialsWithCrlChecking) {
115  auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>(
118  options.watch_root_certs();
119  options.set_root_cert_name(kRootCertName);
120  options.watch_identity_key_cert_pairs();
121  options.set_identity_cert_name(kIdentityCertName);
122  options.set_cert_request_type(
124  options.set_crl_directory(CRL_DIR_PATH);
125  auto server_credentials = grpc::experimental::TlsServerCredentials(options);
126  GPR_ASSERT(server_credentials.get() != nullptr);
127 }
128 
129 // ServerCredentials should always have identity credential presented.
130 // Otherwise gRPC stack will fail.
131 TEST(
132  CredentialsTest,
133  TlsServerCredentialsWithFileWatcherCertificateProviderLoadingIdentityOnly) {
134  auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>(
137  options.watch_identity_key_cert_pairs();
138  options.set_identity_cert_name(kIdentityCertName);
139  options.set_cert_request_type(
141  auto server_credentials = grpc::experimental::TlsServerCredentials(options);
142  GPR_ASSERT(server_credentials.get() != nullptr);
143 }
144 
145 TEST(CredentialsTest, TlsServerCredentialsWithSyncExternalVerifier) {
146  auto verifier =
147  ExternalCertificateVerifier::Create<SyncCertificateVerifier>(true);
148  auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>(
151  options.watch_root_certs();
152  options.set_root_cert_name(kRootCertName);
153  options.watch_identity_key_cert_pairs();
154  options.set_identity_cert_name(kIdentityCertName);
155  options.set_cert_request_type(
157  options.set_certificate_verifier(verifier);
158  auto server_credentials = grpc::experimental::TlsServerCredentials(options);
159  GPR_ASSERT(server_credentials.get() != nullptr);
160 }
161 
162 TEST(CredentialsTest, TlsServerCredentialsWithAsyncExternalVerifier) {
163  auto verifier =
164  ExternalCertificateVerifier::Create<AsyncCertificateVerifier>(true);
165  auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>(
168  options.watch_root_certs();
169  options.set_root_cert_name(kRootCertName);
170  options.watch_identity_key_cert_pairs();
171  options.set_identity_cert_name(kIdentityCertName);
172  options.set_cert_request_type(
174  options.set_certificate_verifier(verifier);
175  auto server_credentials = grpc::experimental::TlsServerCredentials(options);
176  GPR_ASSERT(server_credentials.get() != nullptr);
177 }
178 
179 } // namespace
180 } // namespace testing
181 } // namespace grpc
182 
183 int main(int argc, char** argv) {
184  ::testing::InitGoogleTest(&argc, argv);
185  grpc::testing::TestEnvironment env(&argc, argv);
186  int ret = RUN_ALL_TESTS();
187  return ret;
188 }
testing
Definition: aws_request_signer_test.cc:25
tls_credentials_options.h
port.h
generate.env
env
Definition: generate.py:37
grpc
Definition: grpcpp/alarm.h:33
options
double_dict options[]
Definition: capstone_test.c:55
secure_credentials.h
grpc_core::testing::kRootCertName
constexpr const char * kRootCertName
Definition: tls_security_connector_test.cc:52
grpc_core::testing::kIdentityCertName
constexpr const char * kIdentityCertName
Definition: tls_security_connector_test.cc:53
grpc::experimental::TlsServerCredentialsOptions
Definition: tls_credentials_options.h:136
grpc_security.h
tls_test_utils.h
CRL_DIR_PATH
#define CRL_DIR_PATH
Definition: cpp/server/credentials_test.cc:35
verifier
static void verifier(grpc_server *server, grpc_completion_queue *cq, void *)
Definition: badreq.cc:31
CA_CERT_PATH
#define CA_CERT_PATH
Definition: cpp/server/credentials_test.cc:32
SERVER_CERT_PATH
#define SERVER_CERT_PATH
Definition: cpp/server/credentials_test.cc:33
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc::experimental::TlsServerCredentials
std::shared_ptr< ServerCredentials > TlsServerCredentials(const experimental::TlsServerCredentialsOptions &options)
Builds TLS ServerCredentials given TLS options.
Definition: secure_server_credentials.cc:153
grpc.h
main
int main(int argc, char **argv)
Definition: cpp/server/credentials_test.cc:183
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
server_credentials.h
test_config.h
identity_key_cert_pairs
grpc_core::PemKeyCertPairList identity_key_cert_pairs
Definition: xds_end2end_test.cc:143
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::testing::TEST
TEST(StatsTest, IncCounters)
Definition: stats_test.cc:51
GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY
@ GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY
Definition: grpc_security_constants.h:125
SERVER_KEY_PATH
#define SERVER_KEY_PATH
Definition: cpp/server/credentials_test.cc:34


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:00