system_roots_test.cc
Go to the documentation of this file.
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 
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>
33 
34 #include "src/core/lib/gpr/env.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();
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) {
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
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
load_system_roots.h
log.h
grpc_load_file
grpc_error_handle grpc_load_file(const char *filename, int add_null_terminator, grpc_slice *output)
Definition: load_file.cc:33
generate.env
env
Definition: generate.py:37
grpc::gpr_free
gpr_free(creds_file_name)
load_file.h
grpc
Definition: grpcpp/alarm.h:33
string.h
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
env.h
GRPC_LOG_IF_ERROR
#define GRPC_LOG_IF_ERROR(what, error)
Definition: error.h:398
grpc_security.h
ssl_transport_security.h
grpc::EXPECT_TRUE
EXPECT_TRUE(status.ok())
string_util.h
GRPC_SLICE_IS_EMPTY
#define GRPC_SLICE_IS_EMPTY(slice)
Definition: include/grpc/impl/codegen/slice.h:112
main
int main()
Definition: system_roots_test.cc:96
security_connector.h
tmpfile.h
grpc_empty_slice
GPRAPI grpc_slice grpc_empty_slice(void)
Definition: slice/slice.cc:42
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
load_system_roots_supported.h
test_config.h
EXPECT_STREQ
#define EXPECT_STREQ(s1, s2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2095
grpc_slice_to_c_string
GPRAPI char * grpc_slice_to_c_string(grpc_slice s)
Definition: slice/slice.cc:35
security_context.h
transport_security.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
regress.directory
directory
Definition: regress/regress.py:17
grpc::TEST
TEST(CredentialsTest, StsCredentialsOptionsFromEnv)
Definition: cpp/client/credentials_test.cc:229
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
slice_string_helpers.h
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:28