• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/lib/security/credentials/tls/grpc_tls_credentials_options.h"
20 
21 #include <gmock/gmock.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/log.h>
24 #include <grpc/support/string_util.h>
25 #include <gtest/gtest.h>
26 
27 #include "src/core/lib/iomgr/load_file.h"
28 #include "test/core/util/test_config.h"
29 
30 #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
31 #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
32 #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
33 
34 namespace testing {
35 
SetKeyMaterials(grpc_tls_key_materials_config * config)36 static void SetKeyMaterials(grpc_tls_key_materials_config* config) {
37   grpc_slice ca_slice, cert_slice, key_slice;
38   GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
39                                grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
40   GPR_ASSERT(GRPC_LOG_IF_ERROR(
41       "load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
42   GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
43                                grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
44   const char* ca_cert =
45       reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
46   const char* server_cert =
47       reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
48   const char* server_key =
49       reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
50   grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
51   const auto* pem_key_cert_pair_ptr = &pem_key_cert_pair;
52   grpc_tls_key_materials_config_set_key_materials(config, ca_cert,
53                                                   &pem_key_cert_pair_ptr, 1);
54   grpc_slice_unref(cert_slice);
55   grpc_slice_unref(key_slice);
56   grpc_slice_unref(ca_slice);
57 }
58 
TEST(GrpcTlsCredentialsOptionsTest,SetKeyMaterials)59 TEST(GrpcTlsCredentialsOptionsTest, SetKeyMaterials) {
60   grpc_tls_key_materials_config* config =
61       grpc_tls_key_materials_config_create();
62   SetKeyMaterials(config);
63   grpc_slice ca_slice, cert_slice, key_slice;
64   GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
65                                grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
66   GPR_ASSERT(GRPC_LOG_IF_ERROR(
67       "load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
68   GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
69                                grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
70   const char* ca_cert =
71       reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
72   const char* server_cert =
73       reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
74   const char* server_key =
75       reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
76   EXPECT_STREQ(config->pem_root_certs(), ca_cert);
77   EXPECT_EQ(config->pem_key_cert_pair_list().size(), 1);
78   EXPECT_STREQ(config->pem_key_cert_pair_list()[0].private_key(), server_key);
79   EXPECT_STREQ(config->pem_key_cert_pair_list()[0].cert_chain(), server_cert);
80   grpc_slice_unref(cert_slice);
81   grpc_slice_unref(key_slice);
82   grpc_slice_unref(ca_slice);
83   delete config;
84 }
85 
TEST(GrpcTlsCredentialsOptionsTest,ErrorDetails)86 TEST(GrpcTlsCredentialsOptionsTest, ErrorDetails) {
87   grpc_tls_error_details error_details;
88   EXPECT_STREQ(error_details.error_details().c_str(), "");
89   error_details.set_error_details("test error details");
90   EXPECT_STREQ(error_details.error_details().c_str(), "test error details");
91 }
92 
93 }  // namespace testing
94 
main(int argc,char ** argv)95 int main(int argc, char** argv) {
96   grpc::testing::TestEnvironment env(argc, argv);
97   ::testing::InitGoogleTest(&argc, argv);
98   grpc_init();
99   int ret = RUN_ALL_TESTS();
100   grpc_shutdown();
101   return ret;
102 }
103