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