linux_system_roots_test.cc 3.3 KB

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