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