xref: /aosp_15_r20/external/grpc-grpc/test/core/security/system_roots_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <stdio.h>
20 
21 #include <grpc/support/port_platform.h>
22 
23 #if defined(GPR_LINUX) || defined(GPR_FREEBSD) || defined(GPR_APPLE) || \
24     defined(GPR_WINDOWS)
25 #include <string.h>
26 #if defined(GPR_LINUX) || defined(GPR_FREEBSD) || defined(GPR_APPLE)
27 #include <sys/param.h>
28 #endif  // GPR_LINUX || GPR_FREEBSD || GPR_APPLE
29 
30 #include "gtest/gtest.h"
31 
32 #include <grpc/grpc_security.h>
33 #include <grpc/support/alloc.h>
34 #include <grpc/support/log.h>
35 #include <grpc/support/string_util.h>
36 
37 #include "src/core/lib/gpr/tmpfile.h"
38 #include "src/core/lib/gprpp/crash.h"
39 #include "src/core/lib/gprpp/env.h"
40 #include "src/core/lib/gprpp/load_file.h"
41 #include "src/core/lib/security/context/security_context.h"
42 #include "src/core/lib/security/security_connector/load_system_roots.h"
43 #include "src/core/lib/security/security_connector/load_system_roots_supported.h"
44 #include "src/core/lib/security/security_connector/security_connector.h"
45 #include "src/core/lib/slice/slice.h"
46 #include "src/core/lib/slice/slice_string_helpers.h"
47 #include "src/core/tsi/ssl_transport_security.h"
48 #include "src/core/tsi/transport_security.h"
49 #include "test/core/util/test_config.h"
50 
51 namespace grpc {
52 namespace {
53 
54 // The GetAbsoluteFilePath and CreateRootCertsBundle helper functions are only
55 // defined on some platforms. On other platforms (e.g. Windows), we rely on
56 // built-in helper functions to play similar (but not exactly the same) roles.
57 #if defined(GPR_LINUX) || defined(GPR_FREEBSD) || defined(GPR_APPLE)
TEST(AbsoluteFilePathTest,ConcatenatesCorrectly)58 TEST(AbsoluteFilePathTest, ConcatenatesCorrectly) {
59   const char* directory = "nonexistent/test/directory";
60   const char* filename = "doesnotexist.txt";
61   char result_path[MAXPATHLEN];
62   grpc_core::GetAbsoluteFilePath(directory, filename, result_path);
63   EXPECT_STREQ(result_path, "nonexistent/test/directory/doesnotexist.txt");
64 }
65 
TEST(CreateRootCertsBundleTest,ReturnsEmpty)66 TEST(CreateRootCertsBundleTest, ReturnsEmpty) {
67   // Test that CreateRootCertsBundle returns an empty slice for null or
68   // nonexistent cert directories.
69   grpc_slice result_slice = grpc_core::CreateRootCertsBundle(nullptr);
70   EXPECT_TRUE(GRPC_SLICE_IS_EMPTY(result_slice));
71   grpc_slice_unref(result_slice);
72   result_slice = grpc_core::CreateRootCertsBundle("does/not/exist");
73   EXPECT_TRUE(GRPC_SLICE_IS_EMPTY(result_slice));
74   grpc_slice_unref(result_slice);
75 }
76 
TEST(CreateRootCertsBundleTest,BundlesCorrectly)77 TEST(CreateRootCertsBundleTest, BundlesCorrectly) {
78   // Test that CreateRootCertsBundle returns a correct slice.
79   absl::string_view roots_bundle_str;
80   auto roots_bundle = grpc_core::LoadFile("test/core/security/etc/bundle.pem",
81                                           /*add_null_terminator=*/false);
82   if (roots_bundle.ok()) roots_bundle_str = roots_bundle->as_string_view();
83   // result_slice should have the same content as roots_bundle.
84   grpc_core::Slice result_slice(
85       grpc_core::CreateRootCertsBundle("test/core/security/etc/test_roots"));
86   EXPECT_EQ(result_slice.as_string_view(), roots_bundle_str)
87       << "Expected: \"" << result_slice.as_string_view() << "\"\n"
88       << "Actual:   \"" << roots_bundle_str << "\"";
89 }
90 #endif  // GPR_LINUX || GPR_FREEBSD || GPR_APPLE
91 
92 #if defined(GPR_WINDOWS)
TEST(LoadSystemRootCertsTest,Success)93 TEST(LoadSystemRootCertsTest, Success) {
94   grpc_slice roots_slice = grpc_core::LoadSystemRootCerts();
95   EXPECT_FALSE(GRPC_SLICE_IS_EMPTY(roots_slice));
96   grpc_slice_unref(roots_slice);
97 }
98 #endif  // GPR_WINDOWS
99 
100 }  // namespace
101 }  // namespace grpc
102 
main(int argc,char ** argv)103 int main(int argc, char** argv) {
104   grpc::testing::TestEnvironment env(&argc, argv);
105   ::testing::InitGoogleTest(&argc, argv);
106   return RUN_ALL_TESTS();
107 }
108 #else
main()109 int main() {
110   printf(
111       "*** WARNING: this test is only supported on Linux, FreeBSD, and MacOS"
112       "systems ***\n");
113   return 0;
114 }
115 #endif  // GPR_LINUX || GPR_FREEBSD || GPR_APPLE || GPR_WINDOWS
116