Debian Codegraph @

test/core/security/system_roots_test.cc

raw
1 /*
2 *
3 * Copyright 2018 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 <grpc/support/port_platform.h>
20
21 #include <stdio.h>
22
23 #if defined(GPR_LINUX) || defined(GPR_FREEBSD) || defined(GPR_APPLE)
24 #include <string.h>
25 #include <sys/param.h>
26
27 #include "gtest/gtest.h"
28
29 #include <grpc/grpc_security.h>
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
32 #include <grpc/support/string_util.h>
33
34 #include "src/core/lib/gpr/tmpfile.h"
35 #include "src/core/lib/gprpp/env.h"
36 #include "src/core/lib/iomgr/load_file.h"
37 #include "src/core/lib/security/context/security_context.h"
38 #include "src/core/lib/security/security_connector/load_system_roots.h"
39 #include "src/core/lib/security/security_connector/load_system_roots_supported.h"
40 #include "src/core/lib/security/security_connector/security_connector.h"
41 #include "src/core/lib/slice/slice_string_helpers.h"
42 #include "src/core/tsi/ssl_transport_security.h"
43 #include "src/core/tsi/transport_security.h"
44 #include "test/core/util/test_config.h"
45
46 namespace grpc {
47 namespace {
48
49 TEST(AbsoluteFilePathTest, ConcatenatesCorrectly) {
50 const char* directory = "nonexistent/test/directory";
51 const char* filename = "doesnotexist.txt";
52 char result_path[MAXPATHLEN];
53 grpc_core::GetAbsoluteFilePath(directory, filename, result_path);
54 EXPECT_STREQ(result_path, "nonexistent/test/directory/doesnotexist.txt");
55 }
56
57 TEST(CreateRootCertsBundleTest, ReturnsEmpty) {
58 // Test that CreateRootCertsBundle returns an empty slice for null or
59 // nonexistent cert directories.
60 grpc_slice result_slice = grpc_core::CreateRootCertsBundle(nullptr);
61 EXPECT_TRUE(GRPC_SLICE_IS_EMPTY(result_slice));
62 grpc_slice_unref(result_slice);
63 result_slice = grpc_core::CreateRootCertsBundle("does/not/exist");
64 EXPECT_TRUE(GRPC_SLICE_IS_EMPTY(result_slice));
65 grpc_slice_unref(result_slice);
66 }
67
68 TEST(CreateRootCertsBundleTest, BundlesCorrectly) {
69 // Test that CreateRootCertsBundle returns a correct slice.
70 grpc_slice roots_bundle = grpc_empty_slice();
71 GRPC_LOG_IF_ERROR(
72 "load_file",
73 grpc_load_file("test/core/security/etc/bundle.pem", 1, &roots_bundle));
74 // result_slice should have the same content as roots_bundle.
75 grpc_slice result_slice =
76 grpc_core::CreateRootCertsBundle("test/core/security/etc/test_roots");
77 char* result_str = grpc_slice_to_c_string(result_slice);
78 char* bundle_str = grpc_slice_to_c_string(roots_bundle);
79 EXPECT_STREQ(result_str, bundle_str);
80 // Clean up.
81 gpr_free(result_str);
82 gpr_free(bundle_str);
83 grpc_slice_unref(roots_bundle);
84 grpc_slice_unref(result_slice);
85 }
86
87 } // namespace
88 } // namespace grpc
89
90 int main(int argc, char** argv) {
91 grpc::testing::TestEnvironment env(&argc, argv);
92 ::testing::InitGoogleTest(&argc, argv);
93 return RUN_ALL_TESTS();
94 }
95 #else
96 int main() {
97 printf(
98 "*** WARNING: this test is only supported on Linux, FreeBSD, and MacOS"
99 "systems ***\n");
100 return 0;
101 }
102 #endif // GPR_LINUX || GPR_FREEBSD || GPR_APPLE