xref: /aosp_15_r20/external/grpc-grpc/test/core/security/ssl_credentials_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2017 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/ssl/ssl_credentials.h"
20 
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include <gtest/gtest.h>
25 
26 #include <grpc/grpc_security.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29 
30 #include "src/core/lib/gprpp/crash.h"
31 #include "src/core/lib/security/security_connector/ssl_utils.h"
32 #include "src/core/tsi/ssl_transport_security.h"
33 #include "test/core/util/test_config.h"
34 
TEST(SslCredentialsTest,ConvertGrpcToTsiCertPairs)35 TEST(SslCredentialsTest, ConvertGrpcToTsiCertPairs) {
36   grpc_ssl_pem_key_cert_pair grpc_pairs[] = {{"private_key1", "cert_chain1"},
37                                              {"private_key2", "cert_chain2"},
38                                              {"private_key3", "cert_chain3"}};
39   const size_t num_pairs = 3;
40 
41   {
42     tsi_ssl_pem_key_cert_pair* tsi_pairs =
43         grpc_convert_grpc_to_tsi_cert_pairs(grpc_pairs, 0);
44     ASSERT_EQ(tsi_pairs, nullptr);
45   }
46 
47   {
48     tsi_ssl_pem_key_cert_pair* tsi_pairs =
49         grpc_convert_grpc_to_tsi_cert_pairs(grpc_pairs, num_pairs);
50 
51     ASSERT_NE(tsi_pairs, nullptr);
52     for (size_t i = 0; i < num_pairs; i++) {
53       ASSERT_EQ(strncmp(grpc_pairs[i].private_key, tsi_pairs[i].private_key,
54                         strlen(grpc_pairs[i].private_key)),
55                 0);
56       ASSERT_EQ(strncmp(grpc_pairs[i].cert_chain, tsi_pairs[i].cert_chain,
57                         strlen(grpc_pairs[i].cert_chain)),
58                 0);
59     }
60 
61     grpc_tsi_ssl_pem_key_cert_pairs_destroy(tsi_pairs, num_pairs);
62   }
63 }
64 
main(int argc,char ** argv)65 int main(int argc, char** argv) {
66   grpc::testing::TestEnvironment env(&argc, argv);
67   ::testing::InitGoogleTest(&argc, argv);
68   grpc::testing::TestGrpcScope grpc_scope;
69   return RUN_ALL_TESTS();
70 }
71