[load_file] remove grpc_load_file() in favor of grpc_core::LoadFile()

pull/35857/head
Mark D. Roth 10 months ago
parent f22c954ef5
commit a5133740f0
  1. 5
      BUILD
  2. 143
      CMakeLists.txt
  3. 2
      Makefile
  4. 2
      Package.swift
  5. 62
      build_autogenerated.yaml
  6. 1
      config.m4
  7. 1
      config.w32
  8. 2
      gRPC-C++.podspec
  9. 3
      gRPC-Core.podspec
  10. 2
      grpc.gemspec
  11. 3
      grpc.gyp
  12. 2
      package.xml
  13. 6
      src/core/BUILD
  14. 12
      src/core/ext/xds/xds_client_grpc.cc
  15. 78
      src/core/lib/iomgr/load_file.cc
  16. 35
      src/core/lib/iomgr/load_file.h
  17. 16
      src/core/lib/security/authorization/grpc_authorization_policy_provider.cc
  18. 16
      src/core/lib/security/credentials/external/file_external_account_credentials.cc
  19. 17
      src/core/lib/security/credentials/google_default/google_default_credentials.cc
  20. 13
      src/core/lib/security/credentials/oauth2/oauth2_credentials.cc
  21. 40
      src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.cc
  22. 10
      src/core/lib/security/security_connector/load_system_roots_supported.cc
  23. 43
      src/core/lib/security/security_connector/ssl_utils.cc
  24. 31
      src/cpp/client/secure_credentials.cc
  25. 1
      src/cpp/ext/csm/BUILD
  26. 12
      src/cpp/ext/csm/metadata_exchange.cc
  27. 1
      src/cpp/ext/gcp/BUILD
  28. 17
      src/cpp/ext/gcp/observability_config.cc
  29. 1
      src/python/grpcio/grpc_core_dependencies.py
  30. 19
      test/core/bad_ssl/servers/alpn.cc
  31. 22
      test/core/bad_ssl/servers/cert.cc
  32. 34
      test/core/end2end/end2end_test_suites.cc
  33. 32
      test/core/end2end/fixtures/h2_oauth2_common.h
  34. 26
      test/core/end2end/fixtures/h2_ssl_cred_reload_fixture.h
  35. 20
      test/core/end2end/fixtures/h2_ssl_tls_common.h
  36. 17
      test/core/end2end/fixtures/h2_tls_common.h
  37. 52
      test/core/end2end/h2_ssl_session_reuse_test.cc
  38. 53
      test/core/end2end/h2_tls_peer_property_external_verifier_test.cc
  39. 26
      test/core/handshake/client_ssl.cc
  40. 26
      test/core/handshake/server_ssl_common.cc
  41. 62
      test/core/handshake/verify_peer_options.cc
  42. 15
      test/core/iomgr/BUILD
  43. 161
      test/core/iomgr/load_file_test.cc
  44. 11
      test/core/security/create_jwt.cc
  45. 26
      test/core/security/fetch_oauth2.cc
  46. 27
      test/core/security/ssl_server_fuzzer.cc
  47. 22
      test/core/security/system_roots_test.cc
  48. 39
      test/core/security/tls_security_connector_test.cc
  49. 14
      test/core/surface/num_external_connectivity_watchers_test.cc
  50. 31
      test/core/surface/sequential_connectivity_test.cc
  51. 59
      test/core/tsi/ssl_transport_security_test.cc
  52. 1
      test/core/util/BUILD
  53. 11
      test/core/util/fuzzer_corpus_test.cc
  54. 15
      test/core/util/one_corpus_entry_fuzzer.cc
  55. 15
      test/core/util/tls_utils.cc
  56. 25
      test/cpp/end2end/channelz_service_test.cc
  57. 22
      test/cpp/end2end/grpc_authz_end2end_test.cc
  58. 33
      test/cpp/end2end/ssl_credentials_test.cc
  59. 15
      test/cpp/end2end/tls_credentials_test.cc
  60. 23
      test/cpp/end2end/xds/xds_end2end_test.cc
  61. 33
      test/cpp/end2end/xds/xds_end2end_test_lib.cc
  62. 3
      test/cpp/end2end/xds/xds_end2end_test_lib.h
  63. 1
      test/cpp/end2end/xds/xds_utils.cc
  64. 1
      test/cpp/qps/BUILD
  65. 9
      test/cpp/qps/scenario_runner.cc
  66. 1
      test/cpp/util/BUILD
  67. 22
      test/cpp/util/cli_credentials.cc
  68. 29
      test/cpp/util/grpc_tool_test.cc
  69. 2
      tools/doxygen/Doxyfile.c++.internal
  70. 2
      tools/doxygen/Doxyfile.core.internal
  71. 72
      tools/run_tests/generated/tests.json

@ -1321,7 +1321,6 @@ grpc_cc_library(
"//src/core:lib/iomgr/iomgr_posix.cc",
"//src/core:lib/iomgr/iomgr_posix_cfstream.cc",
"//src/core:lib/iomgr/iomgr_windows.cc",
"//src/core:lib/iomgr/load_file.cc",
"//src/core:lib/iomgr/lockfree_event.cc",
"//src/core:lib/iomgr/polling_entity.cc",
"//src/core:lib/iomgr/pollset.cc",
@ -1414,7 +1413,6 @@ grpc_cc_library(
"//src/core:lib/iomgr/gethostname.h",
"//src/core:lib/iomgr/iocp_windows.h",
"//src/core:lib/iomgr/iomgr.h",
"//src/core:lib/iomgr/load_file.h",
"//src/core:lib/iomgr/lockfree_event.h",
"//src/core:lib/iomgr/nameser.h",
"//src/core:lib/iomgr/polling_entity.h",
@ -1984,6 +1982,7 @@ grpc_cc_library(
"//src/core:grpc_transport_inproc",
"//src/core:json",
"//src/core:json_reader",
"//src/core:load_file",
"//src/core:ref_counted",
"//src/core:resource_quota",
"//src/core:slice",
@ -3421,6 +3420,7 @@ grpc_cc_library(
"grpc_security_base",
"//src/core:error",
"//src/core:json",
"//src/core:load_file",
"//src/core:useful",
],
)
@ -3578,6 +3578,7 @@ grpc_cc_library(
"//src/core:error",
"//src/core:grpc_crl_provider",
"//src/core:grpc_transport_chttp2_alpn",
"//src/core:load_file",
"//src/core:ref_counted",
"//src/core:slice",
"//src/core:tsi_ssl_types",

143
CMakeLists.txt generated

@ -1155,6 +1155,7 @@ if(gRPC_BUILD_TESTS)
add_dependencies(buildtests_cxx lb_get_cpu_stats_test)
add_dependencies(buildtests_cxx lb_load_data_store_test)
add_dependencies(buildtests_cxx load_config_test)
add_dependencies(buildtests_cxx load_file_test)
if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_POSIX)
add_dependencies(buildtests_cxx lock_free_event_test)
endif()
@ -1417,9 +1418,7 @@ if(gRPC_BUILD_TESTS)
add_dependencies(buildtests_cxx test_core_event_engine_posix_timer_list_test)
add_dependencies(buildtests_cxx test_core_event_engine_slice_buffer_test)
add_dependencies(buildtests_cxx test_core_gpr_time_test)
add_dependencies(buildtests_cxx test_core_gprpp_load_file_test)
add_dependencies(buildtests_cxx test_core_gprpp_time_test)
add_dependencies(buildtests_cxx test_core_iomgr_load_file_test)
add_dependencies(buildtests_cxx test_core_iomgr_timer_heap_test)
add_dependencies(buildtests_cxx test_core_security_credentials_test)
add_dependencies(buildtests_cxx test_core_security_ssl_credentials_test)
@ -2324,7 +2323,6 @@ add_library(grpc
src/core/lib/iomgr/iomgr_posix.cc
src/core/lib/iomgr/iomgr_posix_cfstream.cc
src/core/lib/iomgr/iomgr_windows.cc
src/core/lib/iomgr/load_file.cc
src/core/lib/iomgr/lockfree_event.cc
src/core/lib/iomgr/polling_entity.cc
src/core/lib/iomgr/pollset.cc
@ -3088,7 +3086,6 @@ add_library(grpc_unsecure
src/core/lib/iomgr/iomgr_posix.cc
src/core/lib/iomgr/iomgr_posix_cfstream.cc
src/core/lib/iomgr/iomgr_windows.cc
src/core/lib/iomgr/load_file.cc
src/core/lib/iomgr/lockfree_event.cc
src/core/lib/iomgr/polling_entity.cc
src/core/lib/iomgr/pollset.cc
@ -5211,7 +5208,6 @@ add_library(grpc_authorization_provider
src/core/lib/iomgr/iomgr_posix.cc
src/core/lib/iomgr/iomgr_posix_cfstream.cc
src/core/lib/iomgr/iomgr_windows.cc
src/core/lib/iomgr/load_file.cc
src/core/lib/iomgr/lockfree_event.cc
src/core/lib/iomgr/polling_entity.cc
src/core/lib/iomgr/pollset.cc
@ -15148,6 +15144,7 @@ target_link_libraries(grpc_cli
${_gRPC_ALLTARGETS_LIBRARIES}
grpc++
${_gRPC_PROTOBUF_PROTOC_LIBRARIES}
grpc_test_util
grpc++_test_config
)
@ -18436,6 +18433,48 @@ target_link_libraries(load_config_test
)
endif()
if(gRPC_BUILD_TESTS)
add_executable(load_file_test
test/core/gprpp/load_file_test.cc
)
if(WIN32 AND MSVC)
if(BUILD_SHARED_LIBS)
target_compile_definitions(load_file_test
PRIVATE
"GPR_DLL_IMPORTS"
"GRPC_DLL_IMPORTS"
)
endif()
endif()
target_compile_features(load_file_test PUBLIC cxx_std_14)
target_include_directories(load_file_test
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
${_gRPC_ADDRESS_SORTING_INCLUDE_DIR}
${_gRPC_RE2_INCLUDE_DIR}
${_gRPC_SSL_INCLUDE_DIR}
${_gRPC_UPB_GENERATED_DIR}
${_gRPC_UPB_GRPC_GENERATED_DIR}
${_gRPC_UPB_INCLUDE_DIR}
${_gRPC_XXHASH_INCLUDE_DIR}
${_gRPC_ZLIB_INCLUDE_DIR}
third_party/googletest/googletest/include
third_party/googletest/googletest
third_party/googletest/googlemock/include
third_party/googletest/googlemock
${_gRPC_PROTO_GENS_DIR}
)
target_link_libraries(load_file_test
${_gRPC_ALLTARGETS_LIBRARIES}
gtest
grpc_test_util
)
endif()
if(gRPC_BUILD_TESTS)
if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_POSIX)
@ -28740,48 +28779,6 @@ target_link_libraries(test_core_gpr_time_test
)
endif()
if(gRPC_BUILD_TESTS)
add_executable(test_core_gprpp_load_file_test
test/core/gprpp/load_file_test.cc
)
if(WIN32 AND MSVC)
if(BUILD_SHARED_LIBS)
target_compile_definitions(test_core_gprpp_load_file_test
PRIVATE
"GPR_DLL_IMPORTS"
"GRPC_DLL_IMPORTS"
)
endif()
endif()
target_compile_features(test_core_gprpp_load_file_test PUBLIC cxx_std_14)
target_include_directories(test_core_gprpp_load_file_test
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
${_gRPC_ADDRESS_SORTING_INCLUDE_DIR}
${_gRPC_RE2_INCLUDE_DIR}
${_gRPC_SSL_INCLUDE_DIR}
${_gRPC_UPB_GENERATED_DIR}
${_gRPC_UPB_GRPC_GENERATED_DIR}
${_gRPC_UPB_INCLUDE_DIR}
${_gRPC_XXHASH_INCLUDE_DIR}
${_gRPC_ZLIB_INCLUDE_DIR}
third_party/googletest/googletest/include
third_party/googletest/googletest
third_party/googletest/googlemock/include
third_party/googletest/googlemock
${_gRPC_PROTO_GENS_DIR}
)
target_link_libraries(test_core_gprpp_load_file_test
${_gRPC_ALLTARGETS_LIBRARIES}
gtest
grpc_test_util
)
endif()
if(gRPC_BUILD_TESTS)
@ -28825,58 +28822,6 @@ target_link_libraries(test_core_gprpp_time_test
)
endif()
if(gRPC_BUILD_TESTS)
add_executable(test_core_iomgr_load_file_test
test/core/iomgr/load_file_test.cc
test/core/util/cmdline.cc
test/core/util/fuzzer_util.cc
test/core/util/grpc_profiler.cc
test/core/util/histogram.cc
test/core/util/mock_endpoint.cc
test/core/util/parse_hexstring.cc
test/core/util/passthru_endpoint.cc
test/core/util/resolve_localhost_ip46.cc
test/core/util/slice_splitter.cc
test/core/util/tracer_util.cc
)
if(WIN32 AND MSVC)
if(BUILD_SHARED_LIBS)
target_compile_definitions(test_core_iomgr_load_file_test
PRIVATE
"GPR_DLL_IMPORTS"
"GRPC_DLL_IMPORTS"
)
endif()
endif()
target_compile_features(test_core_iomgr_load_file_test PUBLIC cxx_std_14)
target_include_directories(test_core_iomgr_load_file_test
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
${_gRPC_ADDRESS_SORTING_INCLUDE_DIR}
${_gRPC_RE2_INCLUDE_DIR}
${_gRPC_SSL_INCLUDE_DIR}
${_gRPC_UPB_GENERATED_DIR}
${_gRPC_UPB_GRPC_GENERATED_DIR}
${_gRPC_UPB_INCLUDE_DIR}
${_gRPC_XXHASH_INCLUDE_DIR}
${_gRPC_ZLIB_INCLUDE_DIR}
third_party/googletest/googletest/include
third_party/googletest/googletest
third_party/googletest/googlemock/include
third_party/googletest/googlemock
${_gRPC_PROTO_GENS_DIR}
)
target_link_libraries(test_core_iomgr_load_file_test
${_gRPC_ALLTARGETS_LIBRARIES}
gtest
grpc_test_util
)
endif()
if(gRPC_BUILD_TESTS)

2
Makefile generated

@ -1506,7 +1506,6 @@ LIBGRPC_SRC = \
src/core/lib/iomgr/iomgr_posix.cc \
src/core/lib/iomgr/iomgr_posix_cfstream.cc \
src/core/lib/iomgr/iomgr_windows.cc \
src/core/lib/iomgr/load_file.cc \
src/core/lib/iomgr/lockfree_event.cc \
src/core/lib/iomgr/polling_entity.cc \
src/core/lib/iomgr/pollset.cc \
@ -2104,7 +2103,6 @@ LIBGRPC_UNSECURE_SRC = \
src/core/lib/iomgr/iomgr_posix.cc \
src/core/lib/iomgr/iomgr_posix_cfstream.cc \
src/core/lib/iomgr/iomgr_windows.cc \
src/core/lib/iomgr/load_file.cc \
src/core/lib/iomgr/lockfree_event.cc \
src/core/lib/iomgr/polling_entity.cc \
src/core/lib/iomgr/pollset.cc \

2
Package.swift generated

@ -1480,8 +1480,6 @@ let package = Package(
"src/core/lib/iomgr/iomgr_posix.cc",
"src/core/lib/iomgr/iomgr_posix_cfstream.cc",
"src/core/lib/iomgr/iomgr_windows.cc",
"src/core/lib/iomgr/load_file.cc",
"src/core/lib/iomgr/load_file.h",
"src/core/lib/iomgr/lockfree_event.cc",
"src/core/lib/iomgr/lockfree_event.h",
"src/core/lib/iomgr/nameser.h",

@ -964,7 +964,6 @@ libs:
- src/core/lib/iomgr/iomgr.h
- src/core/lib/iomgr/iomgr_fwd.h
- src/core/lib/iomgr/iomgr_internal.h
- src/core/lib/iomgr/load_file.h
- src/core/lib/iomgr/lockfree_event.h
- src/core/lib/iomgr/nameser.h
- src/core/lib/iomgr/polling_entity.h
@ -1781,7 +1780,6 @@ libs:
- src/core/lib/iomgr/iomgr_posix.cc
- src/core/lib/iomgr/iomgr_posix_cfstream.cc
- src/core/lib/iomgr/iomgr_windows.cc
- src/core/lib/iomgr/load_file.cc
- src/core/lib/iomgr/lockfree_event.cc
- src/core/lib/iomgr/polling_entity.cc
- src/core/lib/iomgr/pollset.cc
@ -2458,7 +2456,6 @@ libs:
- src/core/lib/iomgr/iomgr.h
- src/core/lib/iomgr/iomgr_fwd.h
- src/core/lib/iomgr/iomgr_internal.h
- src/core/lib/iomgr/load_file.h
- src/core/lib/iomgr/lockfree_event.h
- src/core/lib/iomgr/nameser.h
- src/core/lib/iomgr/polling_entity.h
@ -2898,7 +2895,6 @@ libs:
- src/core/lib/iomgr/iomgr_posix.cc
- src/core/lib/iomgr/iomgr_posix_cfstream.cc
- src/core/lib/iomgr/iomgr_windows.cc
- src/core/lib/iomgr/load_file.cc
- src/core/lib/iomgr/lockfree_event.cc
- src/core/lib/iomgr/polling_entity.cc
- src/core/lib/iomgr/pollset.cc
@ -4532,7 +4528,6 @@ libs:
- src/core/lib/iomgr/iomgr.h
- src/core/lib/iomgr/iomgr_fwd.h
- src/core/lib/iomgr/iomgr_internal.h
- src/core/lib/iomgr/load_file.h
- src/core/lib/iomgr/lockfree_event.h
- src/core/lib/iomgr/nameser.h
- src/core/lib/iomgr/polling_entity.h
@ -4855,7 +4850,6 @@ libs:
- src/core/lib/iomgr/iomgr_posix.cc
- src/core/lib/iomgr/iomgr_posix_cfstream.cc
- src/core/lib/iomgr/iomgr_windows.cc
- src/core/lib/iomgr/load_file.cc
- src/core/lib/iomgr/lockfree_event.cc
- src/core/lib/iomgr/polling_entity.cc
- src/core/lib/iomgr/pollset.cc
@ -9978,6 +9972,7 @@ targets:
deps:
- grpc++
- protoc
- grpc_test_util
- grpc++_test_config
- name: grpc_completion_queue_test
gtest: true
@ -11487,6 +11482,17 @@ targets:
- gtest
- grpc
uses_polling: false
- name: load_file_test
gtest: true
build: test
language: c++
headers: []
src:
- test/core/gprpp/load_file_test.cc
deps:
- gtest
- grpc_test_util
uses_polling: false
- name: lock_free_event_test
gtest: true
build: test
@ -16547,17 +16553,6 @@ targets:
- gtest
- grpc_test_util
uses_polling: false
- name: test_core_gprpp_load_file_test
gtest: true
build: test
language: c++
headers: []
src:
- test/core/gprpp/load_file_test.cc
deps:
- gtest
- grpc_test_util
uses_polling: false
- name: test_core_gprpp_time_test
gtest: true
build: test
@ -16572,39 +16567,6 @@ targets:
- absl/status:statusor
- gpr
uses_polling: false
- name: test_core_iomgr_load_file_test
gtest: true
build: test
language: c++
headers:
- test/core/util/cmdline.h
- test/core/util/evaluate_args_test_util.h
- test/core/util/fuzzer_util.h
- test/core/util/grpc_profiler.h
- test/core/util/histogram.h
- test/core/util/mock_authorization_endpoint.h
- test/core/util/mock_endpoint.h
- test/core/util/parse_hexstring.h
- test/core/util/passthru_endpoint.h
- test/core/util/resolve_localhost_ip46.h
- test/core/util/slice_splitter.h
- test/core/util/tracer_util.h
src:
- test/core/iomgr/load_file_test.cc
- test/core/util/cmdline.cc
- test/core/util/fuzzer_util.cc
- test/core/util/grpc_profiler.cc
- test/core/util/histogram.cc
- test/core/util/mock_endpoint.cc
- test/core/util/parse_hexstring.cc
- test/core/util/passthru_endpoint.cc
- test/core/util/resolve_localhost_ip46.cc
- test/core/util/slice_splitter.cc
- test/core/util/tracer_util.cc
deps:
- gtest
- grpc_test_util
uses_polling: false
- name: test_core_iomgr_timer_heap_test
gtest: true
build: test

1
config.m4 generated

@ -634,7 +634,6 @@ if test "$PHP_GRPC" != "no"; then
src/core/lib/iomgr/iomgr_posix.cc \
src/core/lib/iomgr/iomgr_posix_cfstream.cc \
src/core/lib/iomgr/iomgr_windows.cc \
src/core/lib/iomgr/load_file.cc \
src/core/lib/iomgr/lockfree_event.cc \
src/core/lib/iomgr/polling_entity.cc \
src/core/lib/iomgr/pollset.cc \

1
config.w32 generated

@ -599,7 +599,6 @@ if (PHP_GRPC != "no") {
"src\\core\\lib\\iomgr\\iomgr_posix.cc " +
"src\\core\\lib\\iomgr\\iomgr_posix_cfstream.cc " +
"src\\core\\lib\\iomgr\\iomgr_windows.cc " +
"src\\core\\lib\\iomgr\\load_file.cc " +
"src\\core\\lib\\iomgr\\lockfree_event.cc " +
"src\\core\\lib\\iomgr\\polling_entity.cc " +
"src\\core\\lib\\iomgr\\pollset.cc " +

2
gRPC-C++.podspec generated

@ -1068,7 +1068,6 @@ Pod::Spec.new do |s|
'src/core/lib/iomgr/iomgr.h',
'src/core/lib/iomgr/iomgr_fwd.h',
'src/core/lib/iomgr/iomgr_internal.h',
'src/core/lib/iomgr/load_file.h',
'src/core/lib/iomgr/lockfree_event.h',
'src/core/lib/iomgr/nameser.h',
'src/core/lib/iomgr/polling_entity.h',
@ -2325,7 +2324,6 @@ Pod::Spec.new do |s|
'src/core/lib/iomgr/iomgr.h',
'src/core/lib/iomgr/iomgr_fwd.h',
'src/core/lib/iomgr/iomgr_internal.h',
'src/core/lib/iomgr/load_file.h',
'src/core/lib/iomgr/lockfree_event.h',
'src/core/lib/iomgr/nameser.h',
'src/core/lib/iomgr/polling_entity.h',

3
gRPC-Core.podspec generated

@ -1593,8 +1593,6 @@ Pod::Spec.new do |s|
'src/core/lib/iomgr/iomgr_posix.cc',
'src/core/lib/iomgr/iomgr_posix_cfstream.cc',
'src/core/lib/iomgr/iomgr_windows.cc',
'src/core/lib/iomgr/load_file.cc',
'src/core/lib/iomgr/load_file.h',
'src/core/lib/iomgr/lockfree_event.cc',
'src/core/lib/iomgr/lockfree_event.h',
'src/core/lib/iomgr/nameser.h',
@ -3105,7 +3103,6 @@ Pod::Spec.new do |s|
'src/core/lib/iomgr/iomgr.h',
'src/core/lib/iomgr/iomgr_fwd.h',
'src/core/lib/iomgr/iomgr_internal.h',
'src/core/lib/iomgr/load_file.h',
'src/core/lib/iomgr/lockfree_event.h',
'src/core/lib/iomgr/nameser.h',
'src/core/lib/iomgr/polling_entity.h',

2
grpc.gemspec generated

@ -1486,8 +1486,6 @@ Gem::Specification.new do |s|
s.files += %w( src/core/lib/iomgr/iomgr_posix.cc )
s.files += %w( src/core/lib/iomgr/iomgr_posix_cfstream.cc )
s.files += %w( src/core/lib/iomgr/iomgr_windows.cc )
s.files += %w( src/core/lib/iomgr/load_file.cc )
s.files += %w( src/core/lib/iomgr/load_file.h )
s.files += %w( src/core/lib/iomgr/lockfree_event.cc )
s.files += %w( src/core/lib/iomgr/lockfree_event.h )
s.files += %w( src/core/lib/iomgr/nameser.h )

3
grpc.gyp generated

@ -820,7 +820,6 @@
'src/core/lib/iomgr/iomgr_posix.cc',
'src/core/lib/iomgr/iomgr_posix_cfstream.cc',
'src/core/lib/iomgr/iomgr_windows.cc',
'src/core/lib/iomgr/load_file.cc',
'src/core/lib/iomgr/lockfree_event.cc',
'src/core/lib/iomgr/polling_entity.cc',
'src/core/lib/iomgr/pollset.cc',
@ -1358,7 +1357,6 @@
'src/core/lib/iomgr/iomgr_posix.cc',
'src/core/lib/iomgr/iomgr_posix_cfstream.cc',
'src/core/lib/iomgr/iomgr_windows.cc',
'src/core/lib/iomgr/load_file.cc',
'src/core/lib/iomgr/lockfree_event.cc',
'src/core/lib/iomgr/polling_entity.cc',
'src/core/lib/iomgr/pollset.cc',
@ -2163,7 +2161,6 @@
'src/core/lib/iomgr/iomgr_posix.cc',
'src/core/lib/iomgr/iomgr_posix_cfstream.cc',
'src/core/lib/iomgr/iomgr_windows.cc',
'src/core/lib/iomgr/load_file.cc',
'src/core/lib/iomgr/lockfree_event.cc',
'src/core/lib/iomgr/polling_entity.cc',
'src/core/lib/iomgr/pollset.cc',

2
package.xml generated

@ -1468,8 +1468,6 @@
<file baseinstalldir="/" name="src/core/lib/iomgr/iomgr_posix.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/iomgr_posix_cfstream.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/iomgr_windows.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/load_file.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/load_file.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/lockfree_event.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/lockfree_event.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/nameser.h" role="src" />

@ -3275,6 +3275,7 @@ grpc_cc_library(
"channel_args",
"channel_fwd",
"dual_ref_counted",
"load_file",
"metadata_batch",
"ref_counted",
"resolved_address",
@ -3534,6 +3535,7 @@ grpc_cc_library(
"iomgr_fwd",
"json",
"json_reader",
"load_file",
"slice",
"slice_refcount",
"status_helper",
@ -3606,6 +3608,7 @@ grpc_cc_library(
"closure",
"error",
"iomgr_fwd",
"load_file",
"ref_counted",
"slice",
"slice_refcount",
@ -3687,6 +3690,7 @@ grpc_cc_library(
"httpcli_ssl_credentials",
"json",
"json_reader",
"load_file",
"metadata_batch",
"poll",
"pollset_set",
@ -3746,6 +3750,7 @@ grpc_cc_library(
"json",
"json_reader",
"json_writer",
"load_file",
"slice",
"slice_refcount",
"status_helper",
@ -4623,6 +4628,7 @@ grpc_cc_library(
"json_util",
"json_writer",
"lb_policy_registry",
"load_file",
"match",
"metadata_batch",
"pollset_set",

@ -53,13 +53,13 @@
#include "src/core/lib/event_engine/default_event_engine.h"
#include "src/core/lib/gprpp/debug_location.h"
#include "src/core/lib/gprpp/env.h"
#include "src/core/lib/gprpp/load_file.h"
#include "src/core/lib/gprpp/orphanable.h"
#include "src/core/lib/gprpp/ref_counted_ptr.h"
#include "src/core/lib/gprpp/sync.h"
#include "src/core/lib/gprpp/time.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/exec_ctx.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/slice/slice.h"
#include "src/core/lib/slice/slice_internal.h"
#include "src/core/lib/transport/error_utils.h"
@ -109,13 +109,9 @@ absl::StatusOr<std::string> GetBootstrapContents(const char* fallback_config) {
"environment variable: %s",
path->c_str());
}
grpc_slice contents;
grpc_error_handle error =
grpc_load_file(path->c_str(), /*add_null_terminator=*/true, &contents);
if (!error.ok()) return grpc_error_to_absl_status(error);
std::string contents_str(StringViewFromSlice(contents));
CSliceUnref(contents);
return contents_str;
auto contents = LoadFile(*path, /*add_null_terminator=*/true);
if (!contents.ok()) return contents.status();
return std::string(contents->as_string_view());
}
// Next, try GRPC_XDS_BOOTSTRAP_CONFIG env var.
auto env_config = GetEnv("GRPC_XDS_BOOTSTRAP_CONFIG");

@ -1,78 +0,0 @@
//
//
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
#include <grpc/support/port_platform.h>
#include "src/core/lib/iomgr/load_file.h"
#include <errno.h>
#include <string.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
#include "src/core/lib/gpr/string.h"
#include "src/core/lib/gprpp/crash.h"
#include "src/core/lib/iomgr/block_annotate.h"
grpc_error_handle grpc_load_file(const char* filename, int add_null_terminator,
grpc_slice* output) {
unsigned char* contents = nullptr;
size_t contents_size = 0;
grpc_slice result = grpc_empty_slice();
FILE* file;
size_t bytes_read = 0;
grpc_error_handle error;
GRPC_SCHEDULING_START_BLOCKING_REGION;
file = fopen(filename, "rb");
if (file == nullptr) {
error = GRPC_OS_ERROR(errno, "fopen");
goto end;
}
fseek(file, 0, SEEK_END);
// Converting to size_t on the assumption that it will not fail
contents_size = static_cast<size_t>(ftell(file));
fseek(file, 0, SEEK_SET);
contents = static_cast<unsigned char*>(
gpr_malloc(contents_size + (add_null_terminator ? 1 : 0)));
bytes_read = fread(contents, 1, contents_size, file);
if (bytes_read < contents_size) {
gpr_free(contents);
error = GRPC_OS_ERROR(errno, "fread");
GPR_ASSERT(ferror(file));
goto end;
}
if (add_null_terminator) {
contents[contents_size++] = 0;
}
result = grpc_slice_new(contents, contents_size, gpr_free);
end:
*output = result;
if (file != nullptr) fclose(file);
if (!error.ok()) {
grpc_error_handle error_out = grpc_error_set_str(
GRPC_ERROR_CREATE_REFERENCING("Failed to load file", &error, 1),
grpc_core::StatusStrProperty::kFilename, filename);
error = error_out;
}
GRPC_SCHEDULING_END_BLOCKING_REGION_NO_EXEC_CTX;
return error;
}

@ -1,35 +0,0 @@
//
//
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
#ifndef GRPC_SRC_CORE_LIB_IOMGR_LOAD_FILE_H
#define GRPC_SRC_CORE_LIB_IOMGR_LOAD_FILE_H
#include <grpc/support/port_platform.h>
#include <stdio.h>
#include <grpc/slice.h>
#include "src/core/lib/iomgr/error.h"
// Loads the content of a file into a slice. add_null_terminator will add
// a NULL terminator if non-zero.
grpc_error_handle grpc_load_file(const char* filename, int add_null_terminator,
grpc_slice* output);
#endif // GRPC_SRC_CORE_LIB_IOMGR_LOAD_FILE_H

@ -30,9 +30,9 @@
#include <grpc/support/time.h>
#include "src/core/lib/debug/trace.h"
#include "src/core/lib/gprpp/load_file.h"
#include "src/core/lib/gprpp/status_helper.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/security/authorization/grpc_authorization_engine.h"
#include "src/core/lib/security/authorization/rbac_policy.h"
#include "src/core/lib/security/authorization/rbac_translator.h"
@ -65,16 +65,12 @@ StaticDataAuthorizationPolicyProvider::StaticDataAuthorizationPolicyProvider(
namespace {
absl::StatusOr<std::string> ReadPolicyFromFile(absl::string_view policy_path) {
grpc_slice policy_slice = grpc_empty_slice();
grpc_error_handle error =
grpc_load_file(std::string(policy_path).c_str(), 0, &policy_slice);
if (!error.ok()) {
absl::Status status = absl::InvalidArgumentError(StatusToString(error));
return status;
auto policy_slice =
LoadFile(std::string(policy_path), /*add_null_terminator=*/false);
if (!policy_slice.ok()) {
return absl::InvalidArgumentError(policy_slice.status().ToString());
}
std::string policy_contents(StringViewFromSlice(policy_slice));
CSliceUnref(policy_slice);
return policy_contents;
return std::string(policy_contents->as_string_view());
}
gpr_timespec TimeoutSecondsToDeadline(int64_t seconds) {

@ -27,7 +27,7 @@
#include <grpc/slice.h>
#include <grpc/support/json.h>
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/gprpp/load_file.h"
#include "src/core/lib/json/json.h"
#include "src/core/lib/json/json_reader.h"
#include "src/core/lib/slice/slice.h"
@ -100,20 +100,14 @@ FileExternalAccountCredentials::FileExternalAccountCredentials(
void FileExternalAccountCredentials::RetrieveSubjectToken(
HTTPRequestContext* /*ctx*/, const Options& /*options*/,
std::function<void(std::string, grpc_error_handle)> cb) {
struct SliceWrapper {
~SliceWrapper() { CSliceUnref(slice); }
grpc_slice slice = grpc_empty_slice();
};
SliceWrapper content_slice;
// To retrieve the subject token, we read the file every time we make a
// request because it may have changed since the last request.
grpc_error_handle error =
grpc_load_file(file_.c_str(), 0, &content_slice.slice);
if (!error.ok()) {
cb("", error);
auto content_slice = LoadFile(file_.c_str(), /*add_null_terminator=*/false);
if (!content_slice.ok()) {
cb("", content_slice.status());
return;
}
absl::string_view content = StringViewFromSlice(content_slice.slice);
absl::string_view content = content_slice->as_string_view();
if (format_type_ == "json") {
auto content_json = JsonParse(content);
if (!content_json.ok() || content_json->type() != Json::Type::kObject) {

@ -41,6 +41,7 @@
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/debug/trace.h"
#include "src/core/lib/gprpp/env.h"
#include "src/core/lib/gprpp/load_file.h"
#include "src/core/lib/gprpp/orphanable.h"
#include "src/core/lib/gprpp/ref_counted_ptr.h"
#include "src/core/lib/gprpp/status_helper.h"
@ -52,7 +53,6 @@
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/exec_ctx.h"
#include "src/core/lib/iomgr/iomgr_fwd.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/iomgr/polling_entity.h"
#include "src/core/lib/iomgr/pollset.h"
#include "src/core/lib/json/json.h"
@ -260,18 +260,20 @@ static grpc_error_handle create_default_creds_from_path(
grpc_auth_json_key key;
grpc_auth_refresh_token token;
grpc_core::RefCountedPtr<grpc_call_credentials> result;
grpc_slice creds_data = grpc_empty_slice();
absl::StatusOr<grpc_core::Slice> creds_data;
grpc_error_handle error;
Json json;
if (creds_path.empty()) {
error = GRPC_ERROR_CREATE("creds_path unset");
goto end;
}
error = grpc_load_file(creds_path.c_str(), 0, &creds_data);
if (!error.ok()) goto end;
creds_data = grpc_core::LoadFile(creds_path, /*add_null_terminator=*/false);
if (!creds_data.ok()) {
error = absl_status_to_grpc_error(creds_data.status());
goto end;
}
{
auto json_or =
grpc_core::JsonParse(grpc_core::StringViewFromSlice(creds_data));
auto json_or = grpc_core::JsonParse(creds_data->as_string_view());
if (!json_or.ok()) {
error = absl_status_to_grpc_error(json_or.status());
goto end;
@ -281,7 +283,7 @@ static grpc_error_handle create_default_creds_from_path(
if (json.type() != Json::Type::kObject) {
error = grpc_error_set_str(GRPC_ERROR_CREATE("Failed to parse JSON"),
grpc_core::StatusStrProperty::kRawBytes,
grpc_core::StringViewFromSlice(creds_data));
creds_data->as_string_view());
goto end;
}
@ -316,7 +318,6 @@ static grpc_error_handle create_default_creds_from_path(
end:
GPR_ASSERT((result == nullptr) + (error.ok()) == 1);
grpc_core::CSliceUnref(creds_data);
*creds = result;
return error;
}

@ -45,12 +45,12 @@
#include <grpc/support/time.h>
#include "src/core/lib/debug/trace.h"
#include "src/core/lib/gprpp/load_file.h"
#include "src/core/lib/gprpp/memory.h"
#include "src/core/lib/gprpp/ref_counted_ptr.h"
#include "src/core/lib/gprpp/status_helper.h"
#include "src/core/lib/http/httpcli_ssl_credentials.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/iomgr/pollset_set.h"
#include "src/core/lib/json/json.h"
#include "src/core/lib/json/json_reader.h"
@ -537,13 +537,14 @@ void MaybeAddToBody(const char* field_name, const char* field,
}
grpc_error_handle LoadTokenFile(const char* path, grpc_slice* token) {
grpc_error_handle err = grpc_load_file(path, 1, token);
if (!err.ok()) return err;
if (GRPC_SLICE_LENGTH(*token) == 0) {
auto slice = LoadFile(path, /*add_null_terminator=*/true);
if (!slice.ok()) return slice.status();
if (slice->length() == 0) {
gpr_log(GPR_ERROR, "Token file %s is empty", path);
err = GRPC_ERROR_CREATE("Token file is empty.");
return GRPC_ERROR_CREATE("Token file is empty.");
}
return err;
*token = slice->TakeCSlice();
return absl::OkStatus();
}
class StsTokenFetcherCredentials

@ -32,11 +32,11 @@
#include <grpc/support/time.h>
#include "src/core/lib/debug/trace.h"
#include "src/core/lib/gprpp/load_file.h"
#include "src/core/lib/gprpp/stat.h"
#include "src/core/lib/gprpp/status_helper.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/exec_ctx.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/slice/slice.h"
#include "src/core/lib/slice/slice_internal.h"
#include "src/core/lib/surface/api_trace.h"
@ -280,17 +280,15 @@ absl::optional<std::string>
FileWatcherCertificateProvider::ReadRootCertificatesFromFile(
const std::string& root_cert_full_path) {
// Read the root file.
grpc_slice root_slice = grpc_empty_slice();
grpc_error_handle root_error =
grpc_load_file(root_cert_full_path.c_str(), 0, &root_slice);
if (!root_error.ok()) {
auto root_slice =
LoadFile(root_cert_full_path, /*add_null_terminator=*/false);
if (!root_slice.ok()) {
gpr_log(GPR_ERROR, "Reading file %s failed: %s",
root_cert_full_path.c_str(), StatusToString(root_error).c_str());
root_cert_full_path.c_str(),
root_slice.status().ToString().c_str());
return absl::nullopt;
}
std::string root_cert(StringViewFromSlice(root_slice));
CSliceUnref(root_slice);
return root_cert;
return std::string(root_slice->as_string_view());
}
namespace {
@ -309,10 +307,6 @@ absl::optional<PemKeyCertPairList>
FileWatcherCertificateProvider::ReadIdentityKeyCertPairFromFiles(
const std::string& private_key_path,
const std::string& identity_certificate_path) {
struct SliceWrapper {
grpc_slice slice = grpc_empty_slice();
~SliceWrapper() { CSliceUnref(slice); }
};
const int kNumRetryAttempts = 3;
for (int i = 0; i < kNumRetryAttempts; ++i) {
// TODO(ZhenLian): replace the timestamp approach with key-match approach
@ -337,24 +331,22 @@ FileWatcherCertificateProvider::ReadIdentityKeyCertPairFromFiles(
continue;
}
// Read the identity files.
SliceWrapper key_slice, cert_slice;
grpc_error_handle key_error =
grpc_load_file(private_key_path.c_str(), 0, &key_slice.slice);
if (!key_error.ok()) {
auto key_slice = LoadFile(private_key_path, /*add_null_terminator=*/false);
if (!key_slice.ok()) {
gpr_log(GPR_ERROR, "Reading file %s failed: %s. Start retrying...",
private_key_path.c_str(), StatusToString(key_error).c_str());
private_key_path.c_str(), key_slice.status().ToString().c_str());
continue;
}
grpc_error_handle cert_error =
grpc_load_file(identity_certificate_path.c_str(), 0, &cert_slice.slice);
if (!cert_error.ok()) {
auto cert_slice =
LoadFile(identity_certificate_path, /*add_null_terminator=*/false);
if (!cert_slice.ok()) {
gpr_log(GPR_ERROR, "Reading file %s failed: %s. Start retrying...",
identity_certificate_path.c_str(),
StatusToString(cert_error).c_str());
cert_slice.status().ToString().c_str());
continue;
}
std::string private_key(StringViewFromSlice(key_slice.slice));
std::string cert_chain(StringViewFromSlice(cert_slice.slice));
std::string private_key(key_slice->as_string_view());
std::string cert_chain(cert_slice->as_string_view());
PemKeyCertPairList identity_pairs;
identity_pairs.emplace_back(private_key, cert_chain);
// Checking the last modification of identity files before reading.

@ -37,8 +37,8 @@
#include "src/core/lib/config/config_vars.h"
#include "src/core/lib/gpr/useful.h"
#include "src/core/lib/gprpp/load_file.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/security/security_connector/load_system_roots.h"
#include "src/core/lib/security/security_connector/load_system_roots_supported.h"
@ -63,14 +63,10 @@ const char* kCertDirectories[] = {""};
#endif // GPR_APPLE
grpc_slice GetSystemRootCerts() {
grpc_slice valid_bundle_slice = grpc_empty_slice();
size_t num_cert_files_ = GPR_ARRAY_SIZE(kCertFiles);
for (size_t i = 0; i < num_cert_files_; i++) {
grpc_error_handle error =
grpc_load_file(kCertFiles[i], 1, &valid_bundle_slice);
if (error.ok()) {
return valid_bundle_slice;
}
auto slice = LoadFile(kCertFiles[i], /*add_null_terminator=*/true);
if (slice.ok()) return slice->TakeCSlice();
}
return grpc_empty_slice();
}

@ -44,8 +44,8 @@
#include "src/core/lib/config/config_vars.h"
#include "src/core/lib/gpr/useful.h"
#include "src/core/lib/gprpp/host_port.h"
#include "src/core/lib/gprpp/load_file.h"
#include "src/core/lib/gprpp/ref_counted_ptr.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/security/context/security_context.h"
#include "src/core/lib/security/security_connector/load_system_roots.h"
#include "src/core/tsi/ssl_transport_security.h"
@ -566,40 +566,49 @@ const char* DefaultSslRootStore::GetPemRootCerts() {
}
grpc_slice DefaultSslRootStore::ComputePemRootCerts() {
grpc_slice result = grpc_empty_slice();
Slice result;
// First try to load the roots from the configuration.
auto default_root_certs_path = ConfigVars::Get().DefaultSslRootsFilePath();
std::string default_root_certs_path =
ConfigVars::Get().DefaultSslRootsFilePath();
if (!default_root_certs_path.empty()) {
GRPC_LOG_IF_ERROR(
"load_file",
grpc_load_file(std::string(default_root_certs_path).c_str(), 1,
&result));
auto slice =
LoadFile(default_root_certs_path, /*add_null_terminator=*/true);
if (!slice.ok()) {
gpr_log(GPR_ERROR, "error loading file %s: %s",
default_root_certs_path.c_str(),
slice.status().ToString().c_str());
} else {
result = std::move(*slice);
}
}
// Try overridden roots if needed.
grpc_ssl_roots_override_result ovrd_res = GRPC_SSL_ROOTS_OVERRIDE_FAIL;
if (GRPC_SLICE_IS_EMPTY(result) && ssl_roots_override_cb != nullptr) {
if (result.empty() && ssl_roots_override_cb != nullptr) {
char* pem_root_certs = nullptr;
ovrd_res = ssl_roots_override_cb(&pem_root_certs);
if (ovrd_res == GRPC_SSL_ROOTS_OVERRIDE_OK) {
GPR_ASSERT(pem_root_certs != nullptr);
result = grpc_slice_from_copied_buffer(
result = Slice::FromCopiedBuffer(
pem_root_certs,
strlen(pem_root_certs) + 1); // nullptr terminator.
}
gpr_free(pem_root_certs);
}
// Try loading roots from OS trust store if flag is enabled.
if (GRPC_SLICE_IS_EMPTY(result) &&
!ConfigVars::Get().NotUseSystemSslRoots()) {
result = LoadSystemRootCerts();
if (result.empty() && !ConfigVars::Get().NotUseSystemSslRoots()) {
result = Slice(LoadSystemRootCerts());
}
// Fallback to roots manually shipped with gRPC.
if (GRPC_SLICE_IS_EMPTY(result) &&
ovrd_res != GRPC_SSL_ROOTS_OVERRIDE_FAIL_PERMANENTLY) {
GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(installed_roots_path, 1, &result));
if (result.empty() && ovrd_res != GRPC_SSL_ROOTS_OVERRIDE_FAIL_PERMANENTLY) {
auto slice = LoadFile(installed_roots_path, /*add_null_terminator=*/true);
if (!slice.ok()) {
gpr_log(GPR_ERROR, "error loading file %s: %s", installed_roots_path,
slice.status().ToString().c_str());
} else {
result = std::move(*slice);
}
}
return result;
return result.TakeCSlice();
}
void DefaultSslRootStore::InitRootStore() {

@ -46,9 +46,9 @@
#include "src/core/lib/event_engine/default_event_engine.h"
#include "src/core/lib/gprpp/env.h"
#include "src/core/lib/gprpp/load_file.h"
#include "src/core/lib/gprpp/status_helper.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/json/json.h"
#include "src/core/lib/json/json_reader.h"
#include "src/core/lib/security/util/json_util.h"
@ -220,30 +220,19 @@ grpc::Status StsCredentialsOptionsFromEnv(StsCredentialsOptions* options) {
"options cannot be nullptr.");
}
ClearStsCredentialsOptions(options);
grpc_slice json_string = grpc_empty_slice();
auto sts_creds_path = grpc_core::GetEnv("STS_CREDENTIALS");
grpc_error_handle error;
grpc::Status status;
// NOLINTNEXTLINE(clang-diagnostic-unused-lambda-capture)
auto cleanup = [&json_string, &status]() {
grpc_slice_unref(json_string);
return status;
};
if (!sts_creds_path.has_value()) {
status = grpc::Status(grpc::StatusCode::NOT_FOUND,
"STS_CREDENTIALS environment variable not set.");
return cleanup();
return grpc::Status(grpc::StatusCode::NOT_FOUND,
"STS_CREDENTIALS environment variable not set.");
}
error = grpc_load_file(sts_creds_path->c_str(), 1, &json_string);
if (!error.ok()) {
status = grpc::Status(grpc::StatusCode::NOT_FOUND,
grpc_core::StatusToString(error));
return cleanup();
auto json_slice =
grpc_core::LoadFile(*sts_creds_path, /*add_null_terminator=*/true);
if (!json_slice.ok()) {
return grpc::Status(grpc::StatusCode::NOT_FOUND,
json_slice.status().ToString());
}
status = StsCredentialsOptionsFromJson(
reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(json_string)),
options);
return cleanup();
return StsCredentialsOptionsFromJson(json_slice->as_string_view().data(),
options);
}
// C++ to Core STS Credentials options.

@ -66,6 +66,7 @@ grpc_cc_library(
"//src/core:json_args",
"//src/core:json_object_loader",
"//src/core:json_reader",
"//src/core:load_file",
"//src/core:metadata_batch",
"//src/core:slice",
"//src/core:xds_enabled_server",

@ -43,8 +43,8 @@
#include "src/core/lib/channel/call_tracer.h"
#include "src/core/lib/gprpp/env.h"
#include "src/core/lib/gprpp/load_file.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/json/json_args.h"
#include "src/core/lib/json/json_object_loader.h"
#include "src/core/lib/json/json_reader.h"
@ -132,13 +132,9 @@ std::string GetXdsBootstrapContents() {
// First, try GRPC_XDS_BOOTSTRAP env var.
auto path = grpc_core::GetEnv("GRPC_XDS_BOOTSTRAP");
if (path.has_value()) {
grpc_slice contents;
grpc_error_handle error =
grpc_load_file(path->c_str(), /*add_null_terminator=*/true, &contents);
if (!error.ok()) return "";
std::string contents_str(grpc_core::StringViewFromSlice(contents));
grpc_core::CSliceUnref(contents);
return contents_str;
auto contents = LoadFile(*path, /*add_null_terminator=*/true);
if (!contents.ok()) return "";
return std::string(contents->as_string_view());
}
// Next, try GRPC_XDS_BOOTSTRAP_CONFIG env var.
auto env_config = grpc_core::GetEnv("GRPC_XDS_BOOTSTRAP_CONFIG");

@ -96,6 +96,7 @@ grpc_cc_library(
"//src/core:json_args",
"//src/core:json_object_loader",
"//src/core:json_reader",
"//src/core:load_file",
"//src/core:slice",
"//src/core:slice_refcount",
"//src/core:status_helper",

@ -34,10 +34,10 @@
#include <grpc/status.h>
#include "src/core/lib/gprpp/env.h"
#include "src/core/lib/gprpp/load_file.h"
#include "src/core/lib/gprpp/status_helper.h"
#include "src/core/lib/gprpp/validation_errors.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/json/json.h"
#include "src/core/lib/json/json_reader.h"
#include "src/core/lib/slice/slice_internal.h"
@ -56,17 +56,12 @@ absl::StatusOr<std::string> GetGcpObservabilityConfigContents() {
std::string contents_str;
auto path = grpc_core::GetEnv("GRPC_GCP_OBSERVABILITY_CONFIG_FILE");
if (path.has_value() && !path.value().empty()) {
grpc_slice contents;
grpc_error_handle error =
grpc_load_file(path->c_str(), /*add_null_terminator=*/true, &contents);
if (!error.ok()) {
return grpc_error_to_absl_status(
grpc_error_set_int(error, grpc_core::StatusIntProperty::kRpcStatus,
GRPC_STATUS_FAILED_PRECONDITION));
auto contents = LoadFile(*path, /*add_null_terminator=*/true);
if (!contents.ok()) {
return absl::FailedPreconditionError(absl::StrCat(
"error loading file ", *path, ": ", contents.status().ToString()));
}
std::string contents_str(grpc_core::StringViewFromSlice(contents));
grpc_slice_unref(contents);
return std::move(contents_str);
return std::string(contents->as_string_view());
}
// Next, try GRPC_GCP_OBSERVABILITY_CONFIG env var.
auto env_config = grpc_core::GetEnv("GRPC_GCP_OBSERVABILITY_CONFIG");

@ -608,7 +608,6 @@ CORE_SOURCE_FILES = [
'src/core/lib/iomgr/iomgr_posix.cc',
'src/core/lib/iomgr/iomgr_posix_cfstream.cc',
'src/core/lib/iomgr/iomgr_windows.cc',
'src/core/lib/iomgr/load_file.cc',
'src/core/lib/iomgr/lockfree_event.cc',
'src/core/lib/iomgr/polling_entity.cc',
'src/core/lib/iomgr/pollset.cc',

@ -27,8 +27,8 @@
#include "src/core/lib/gpr/useful.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "test/core/bad_ssl/server_common.h"
#include "test/core/util/tls_utils.h"
#define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
#define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
@ -59,16 +59,11 @@ const char* grpc_chttp2_get_alpn_version_index(size_t i) {
int main(int argc, char** argv) {
const char* addr = bad_ssl_addr(argc, argv);
grpc_slice cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
const char* server_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
const char* server_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
std::string server_cert =
grpc_core::testing::GetFileContents(SERVER_CERT_PATH);
std::string server_key = grpc_core::testing::GetFileContents(SERVER_KEY_PATH);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key.c_str(),
server_cert.c_str()};
grpc_server_credentials* ssl_creds;
grpc_server* server;
@ -80,8 +75,6 @@ int main(int argc, char** argv) {
grpc_server_credentials_release(ssl_creds);
bad_ssl_run(server);
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
grpc_shutdown();
return 0;

@ -22,8 +22,8 @@
#include <grpc/support/log.h>
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "test/core/bad_ssl/server_common.h"
#include "test/core/util/tls_utils.h"
// This server will present an untrusted cert to the connecting client,
// causing the SSL handshake to fail
@ -33,20 +33,15 @@ int main(int argc, char** argv) {
grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
grpc_server_credentials* ssl_creds;
grpc_server* server;
grpc_slice cert_slice, key_slice;
grpc_init();
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file",
grpc_load_file("src/core/tsi/test_creds/badserver.pem", 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file",
grpc_load_file("src/core/tsi/test_creds/badserver.key", 1, &key_slice)));
pem_key_cert_pair.private_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
pem_key_cert_pair.cert_chain =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
std::string cert = grpc_core::testing::GetFileContents(
"src/core/tsi/test_creds/badserver.pem");
std::string key = grpc_core::testing::GetFileContents(
"src/core/tsi/test_creds/badserver.key");
pem_key_cert_pair.private_key = key.c_str();
pem_key_cert_pair.cert_chain = cert.c_str();
ssl_creds = grpc_ssl_server_credentials_create(nullptr, &pem_key_cert_pair, 1,
0, nullptr);
@ -54,9 +49,6 @@ int main(int argc, char** argv) {
GPR_ASSERT(grpc_server_add_http2_port(server, addr, ssl_creds));
grpc_server_credentials_release(ssl_creds);
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
bad_ssl_run(server);
grpc_shutdown();

@ -52,7 +52,6 @@
#include "src/core/lib/gprpp/sync.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/exec_ctx.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/iomgr/port.h"
#include "src/core/lib/security/credentials/fake/fake_credentials.h"
#include "test/core/end2end/end2end_tests.h"
@ -68,6 +67,7 @@
#include "test/core/end2end/fixtures/sockpair_fixture.h"
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
#include "test/core/util/tls_utils.h"
// IWYU pragma: no_include <unistd.h>
@ -415,20 +415,12 @@ class SslProxyFixture : public CoreTestFixture {
static grpc_server* CreateProxyServer(const char* port,
const grpc_channel_args* server_args) {
grpc_server* s = grpc_server_create(server_args, nullptr);
grpc_slice cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
const char* server_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
const char* server_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
std::string server_cert = testing::GetFileContents(SERVER_CERT_PATH);
std::string server_key = testing::GetFileContents(SERVER_KEY_PATH);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key.c_str(),
server_cert.c_str()};
grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
nullptr, &pem_key_cert_pair, 1, 0, nullptr);
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
GPR_ASSERT(grpc_server_add_http2_port(s, port, ssl_creds));
grpc_server_credentials_release(ssl_creds);
return s;
@ -457,20 +449,12 @@ class SslProxyFixture : public CoreTestFixture {
grpc_server* MakeServer(
const ChannelArgs& args, grpc_completion_queue* cq,
absl::AnyInvocable<void(grpc_server*)>& pre_server_start) override {
grpc_slice cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
const char* server_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
const char* server_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
std::string server_cert = testing::GetFileContents(SERVER_CERT_PATH);
std::string server_key = testing::GetFileContents(SERVER_KEY_PATH);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key.c_str(),
server_cert.c_str()};
grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
nullptr, &pem_key_cert_pair, 1, 0, nullptr);
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
if (args.Contains(FAIL_AUTH_CHECK_SERVER_ARG_NAME)) {
grpc_auth_metadata_processor processor = {ProcessAuthFailure, nullptr,
nullptr};

@ -27,11 +27,11 @@
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/security/credentials/credentials.h"
#include "src/core/lib/security/credentials/ssl/ssl_credentials.h"
#include "test/core/end2end/end2end_tests.h"
#include "test/core/end2end/fixtures/secure_fixture.h"
#include "test/core/util/tls_utils.h"
class Oauth2Fixture : public SecureFixture {
public:
@ -109,13 +109,10 @@ class Oauth2Fixture : public SecureFixture {
grpc_channel_credentials* MakeClientCreds(
const grpc_core::ChannelArgs&) override {
grpc_slice ca_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(CaCertPath(), 1, &ca_slice)));
const char* test_root_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
grpc_channel_credentials* ssl_creds =
grpc_ssl_credentials_create(test_root_cert, nullptr, nullptr, nullptr);
std::string test_root_cert =
grpc_core::testing::GetFileContents(CaCertPath());
grpc_channel_credentials* ssl_creds = grpc_ssl_credentials_create(
test_root_cert.c_str(), nullptr, nullptr, nullptr);
if (ssl_creds != nullptr) {
// Set the min and max TLS version.
grpc_ssl_credentials* creds =
@ -130,22 +127,17 @@ class Oauth2Fixture : public SecureFixture {
nullptr);
grpc_channel_credentials_release(ssl_creds);
grpc_call_credentials_release(oauth2_creds);
grpc_slice_unref(ca_slice);
return ssl_oauth2_creds;
}
grpc_server_credentials* MakeServerCreds(
const grpc_core::ChannelArgs& args) override {
grpc_slice cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(ServerCertPath(), 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(ServerKeyPath(), 1, &key_slice)));
const char* server_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
const char* server_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
std::string server_cert =
grpc_core::testing::GetFileContents(ServerCertPath());
std::string server_key =
grpc_core::testing::GetFileContents(ServerKeyPath());
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key.c_str(),
server_cert.c_str()};
grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
nullptr, &pem_key_cert_pair, 1, 0, nullptr);
if (ssl_creds != nullptr) {
@ -158,8 +150,6 @@ class Oauth2Fixture : public SecureFixture {
grpc_server_credentials_set_auth_metadata_processor(
ssl_creds,
test_processor_create(args.Contains(FAIL_AUTH_CHECK_SERVER_ARG_NAME)));
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
return ssl_creds;
}

@ -27,10 +27,10 @@
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/security/credentials/ssl/ssl_credentials.h"
#include "test/core/end2end/end2end_tests.h"
#include "test/core/end2end/fixtures/secure_fixture.h"
#include "test/core/util/tls_utils.h"
class SslCredReloadFixture : public SecureFixture {
public:
@ -100,25 +100,13 @@ class SslCredReloadFixture : public SecureFixture {
return GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_FAIL;
}
if (!server_credential_reloaded_) {
grpc_slice ca_slice, cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(CaCertPath(), 1, &ca_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(CertPath(), 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(KeyPath(), 1, &key_slice)));
const char* ca_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
const char* server_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
const char* server_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
std::string ca_cert = grpc_core::testing::GetFileContents(CaCertPath());
std::string server_cert = grpc_core::testing::GetFileContents(CertPath());
std::string server_key = grpc_core::testing::GetFileContents(KeyPath());
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key.c_str(),
server_cert.c_str()};
*config = grpc_ssl_server_certificate_config_create(
ca_cert, &pem_key_cert_pair, 1);
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
grpc_slice_unref(ca_slice);
ca_cert.c_str(), &pem_key_cert_pair, 1);
server_credential_reloaded_ = true;
return GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_NEW;
} else {

@ -27,10 +27,10 @@
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/security/credentials/ssl/ssl_credentials.h"
#include "test/core/end2end/end2end_tests.h"
#include "test/core/end2end/fixtures/secure_fixture.h"
#include "test/core/util/tls_utils.h"
class SslTlsFixture : public SecureFixture {
public:
@ -67,16 +67,12 @@ class SslTlsFixture : public SecureFixture {
grpc_server_credentials* MakeServerCreds(
const grpc_core::ChannelArgs& args) override {
grpc_slice cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(ServerCertPath(), 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(ServerKeyPath(), 1, &key_slice)));
const char* server_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
const char* server_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
std::string server_cert =
grpc_core::testing::GetFileContents(ServerCertPath());
std::string server_key =
grpc_core::testing::GetFileContents(ServerKeyPath());
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key.c_str(),
server_cert.c_str()};
grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
nullptr, &pem_key_cert_pair, 1, 0, nullptr);
if (ssl_creds != nullptr) {
@ -86,8 +82,6 @@ class SslTlsFixture : public SecureFixture {
creds->set_min_tls_version(tls_version_);
creds->set_max_tls_version(tls_version_);
}
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
if (args.Contains(FAIL_AUTH_CHECK_SERVER_ARG_NAME)) {
grpc_auth_metadata_processor processor = {process_auth_failure, nullptr,
nullptr};

@ -36,7 +36,6 @@
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h"
#include "src/core/lib/slice/slice_internal.h"
#include "test/core/end2end/end2end_tests.h"
@ -84,19 +83,12 @@ class TlsFixture : public SecureFixture {
}
switch (provider_type) {
case SecurityPrimitives::ProviderType::STATIC_PROVIDER: {
grpc_slice root_slice, cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(CA_CERT_PATH, 1, &root_slice)));
std::string root_cert =
std::string(grpc_core::StringViewFromSlice(root_slice));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
grpc_core::testing::GetFileContents(CA_CERT_PATH);
std::string identity_cert =
std::string(grpc_core::StringViewFromSlice(cert_slice));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
grpc_core::testing::GetFileContents(SERVER_CERT_PATH);
std::string private_key =
std::string(grpc_core::StringViewFromSlice(key_slice));
grpc_core::testing::GetFileContents(SERVER_KEY_PATH);
grpc_tls_identity_pairs* client_pairs =
grpc_tls_identity_pairs_create();
grpc_tls_identity_pairs_add_pair(client_pairs, private_key.c_str(),
@ -109,9 +101,6 @@ class TlsFixture : public SecureFixture {
identity_cert.c_str());
server_provider_ = grpc_tls_certificate_provider_static_data_create(
root_cert.c_str(), server_pairs);
grpc_slice_unref(root_slice);
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
break;
}
case SecurityPrimitives::ProviderType::FILE_PROVIDER: {

@ -40,10 +40,10 @@
#include "src/core/lib/gprpp/host_port.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/exec_ctx.h"
#include "src/core/lib/iomgr/load_file.h"
#include "test/core/end2end/cq_verifier.h"
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
#include "test/core/util/tls_utils.h"
#define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
#define CLIENT_CERT_PATH "src/core/tsi/test_creds/client.pem"
@ -58,22 +58,13 @@ namespace {
gpr_timespec five_seconds_time() { return grpc_timeout_seconds_to_deadline(5); }
grpc_server* server_create(grpc_completion_queue* cq, const char* server_addr) {
grpc_slice ca_slice, cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
const char* ca_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
const char* server_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
const char* server_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
grpc_ssl_pem_key_cert_pair pem_cert_key_pair = {server_key, server_cert};
std::string ca_cert = GetFileContents(CA_CERT_PATH);
std::string server_cert = GetFileContents(SERVER_CERT_PATH);
std::string server_key = GetFileContents(SERVER_KEY_PATH);
grpc_ssl_pem_key_cert_pair pem_cert_key_pair = {server_key.c_str(),
server_cert.c_str()};
grpc_server_credentials* server_creds = grpc_ssl_server_credentials_create_ex(
ca_cert, &pem_cert_key_pair, 1,
ca_cert.c_str(), &pem_cert_key_pair, 1,
GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY, nullptr);
grpc_server* server = grpc_server_create(nullptr, nullptr);
@ -81,32 +72,18 @@ grpc_server* server_create(grpc_completion_queue* cq, const char* server_addr) {
GPR_ASSERT(grpc_server_add_http2_port(server, server_addr, server_creds));
grpc_server_credentials_release(server_creds);
grpc_server_start(server);
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
grpc_slice_unref(ca_slice);
return server;
}
grpc_channel* client_create(const char* server_addr,
grpc_ssl_session_cache* cache) {
grpc_slice ca_slice, cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(CLIENT_CERT_PATH, 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(CLIENT_KEY_PATH, 1, &key_slice)));
const char* ca_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
const char* client_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
const char* client_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
grpc_ssl_pem_key_cert_pair signed_client_key_cert_pair = {client_key,
client_cert};
std::string ca_cert = GetFileContents(CA_CERT_PATH);
std::string client_cert = GetFileContents(CLIENT_CERT_PATH);
std::string client_key = GetFileContents(CLIENT_KEY_PATH);
grpc_ssl_pem_key_cert_pair signed_client_key_cert_pair = {
client_key.c_str(), client_cert.c_str()};
grpc_channel_credentials* client_creds = grpc_ssl_credentials_create(
ca_cert, &signed_client_key_cert_pair, nullptr, nullptr);
ca_cert.c_str(), &signed_client_key_cert_pair, nullptr, nullptr);
grpc_arg args[] = {
grpc_channel_arg_string_create(
@ -128,9 +105,6 @@ grpc_channel* client_create(const char* server_addr,
grpc_channel_args_destroy(client_args);
}
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
grpc_slice_unref(ca_slice);
return client;
}

@ -40,7 +40,6 @@
#include "src/core/lib/gprpp/host_port.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/exec_ctx.h"
#include "src/core/lib/iomgr/load_file.h"
#include "test/core/end2end/cq_verifier.h"
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
@ -66,26 +65,17 @@ gpr_timespec five_seconds_time() { return grpc_timeout_seconds_to_deadline(5); }
grpc_server* server_create(grpc_completion_queue* cq, const char* server_addr,
grpc_tls_certificate_provider** server_provider,
grpc_tls_certificate_verifier** verifier) {
grpc_slice ca_slice, cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
const char* ca_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
const char* server_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
const char* server_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
std::string ca_cert = GetFileContents(CA_CERT_PATH);
std::string server_cert = GetFileContents(SERVER_CERT_PATH);
std::string server_key = GetFileContents(SERVER_KEY_PATH);
grpc_tls_credentials_options* options = grpc_tls_credentials_options_create();
// Set credential provider.
grpc_tls_identity_pairs* server_pairs = grpc_tls_identity_pairs_create();
grpc_tls_identity_pairs_add_pair(server_pairs, server_key, server_cert);
*server_provider =
grpc_tls_certificate_provider_static_data_create(ca_cert, server_pairs);
grpc_tls_identity_pairs_add_pair(server_pairs, server_key.c_str(),
server_cert.c_str());
*server_provider = grpc_tls_certificate_provider_static_data_create(
ca_cert.c_str(), server_pairs);
grpc_tls_credentials_options_set_certificate_provider(options,
*server_provider);
grpc_tls_credentials_options_watch_root_certs(options);
@ -108,35 +98,23 @@ grpc_server* server_create(grpc_completion_queue* cq, const char* server_addr,
grpc_server_start(server);
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
grpc_slice_unref(ca_slice);
return server;
}
grpc_channel* client_create(const char* server_addr,
grpc_tls_certificate_provider** client_provider,
grpc_tls_certificate_verifier** verifier) {
grpc_slice ca_slice, cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(CLIENT_CERT_PATH, 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(CLIENT_KEY_PATH, 1, &key_slice)));
const char* ca_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
const char* client_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
const char* client_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
std::string ca_cert = GetFileContents(CA_CERT_PATH);
std::string client_cert = GetFileContents(CLIENT_CERT_PATH);
std::string client_key = GetFileContents(CLIENT_KEY_PATH);
grpc_tls_credentials_options* options = grpc_tls_credentials_options_create();
// Set credential provider.
grpc_tls_identity_pairs* client_pairs = grpc_tls_identity_pairs_create();
grpc_tls_identity_pairs_add_pair(client_pairs, client_key, client_cert);
*client_provider =
grpc_tls_certificate_provider_static_data_create(ca_cert, client_pairs);
grpc_tls_identity_pairs_add_pair(client_pairs, client_key.c_str(),
client_cert.c_str());
*client_provider = grpc_tls_certificate_provider_static_data_create(
ca_cert.c_str(), client_pairs);
grpc_tls_credentials_options_set_certificate_provider(options,
*client_provider);
@ -171,9 +149,6 @@ grpc_channel* client_create(const char* server_addr,
grpc_channel_args_destroy(client_args);
}
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
grpc_slice_unref(ca_slice);
return client;
}

@ -59,7 +59,7 @@
#include "src/core/lib/gprpp/crash.h"
#include "src/core/lib/gprpp/sync.h"
#include "src/core/lib/gprpp/thd.h"
#include "src/core/lib/iomgr/load_file.h"
#include "test/core/util/tls_utils.h"
#define SSL_CERT_PATH "src/core/tsi/test_creds/server1.pem"
#define SSL_KEY_PATH "src/core/tsi/test_creds/server1.key"
@ -320,22 +320,15 @@ static bool client_ssl_test(char* server_alpn_preferred) {
ssl_library_info.Await();
// Load key pair and establish client SSL credentials.
std::string ca_cert = grpc_core::testing::GetFileContents(SSL_CA_PATH);
std::string cert = grpc_core::testing::GetFileContents(SSL_CERT_PATH);
std::string key = grpc_core::testing::GetFileContents(SSL_KEY_PATH);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
grpc_slice ca_slice, cert_slice, key_slice;
EXPECT_TRUE(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SSL_CA_PATH, 1, &ca_slice)));
EXPECT_TRUE(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
EXPECT_TRUE(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SSL_KEY_PATH, 1, &key_slice)));
const char* ca_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
pem_key_cert_pair.private_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
pem_key_cert_pair.cert_chain =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
pem_key_cert_pair.private_key = key.c_str();
pem_key_cert_pair.cert_chain = cert.c_str();
grpc_channel_credentials* ssl_creds = grpc_ssl_credentials_create(
ca_cert, &pem_key_cert_pair, nullptr, nullptr);
ca_cert.c_str(), &pem_key_cert_pair, nullptr, nullptr);
// Establish a channel pointing at the TLS server. Since the gRPC runtime is
// lazy, this won't necessarily establish a connection yet.
@ -380,9 +373,6 @@ static bool client_ssl_test(char* server_alpn_preferred) {
grpc_channel_destroy(channel);
grpc_channel_credentials_release(ssl_creds);
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
grpc_slice_unref(ca_slice);
thd.Join();

@ -48,9 +48,9 @@
#include "src/core/lib/gprpp/sync.h"
#include "src/core/lib/gprpp/thd.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
#include "test/core/util/tls_utils.h"
// IWYU pragma: no_include <arpa/inet.h>
@ -117,22 +117,15 @@ void server_thread(void* arg) {
const int port = s->port();
// Load key pair and establish server SSL credentials.
std::string ca_cert = grpc_core::testing::GetFileContents(SSL_CA_PATH);
std::string cert = grpc_core::testing::GetFileContents(SSL_CERT_PATH);
std::string key = grpc_core::testing::GetFileContents(SSL_KEY_PATH);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
grpc_slice ca_slice, cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SSL_CA_PATH, 1, &ca_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SSL_KEY_PATH, 1, &key_slice)));
const char* ca_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
pem_key_cert_pair.private_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
pem_key_cert_pair.cert_chain =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
pem_key_cert_pair.private_key = key.c_str();
pem_key_cert_pair.cert_chain = cert.c_str();
grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
ca_cert, &pem_key_cert_pair, 1, 0, nullptr);
ca_cert.c_str(), &pem_key_cert_pair, 1, 0, nullptr);
// Start server listening on local port.
std::string addr = absl::StrCat("127.0.0.1:", port);
@ -168,9 +161,6 @@ void server_thread(void* arg) {
grpc_server_destroy(server);
grpc_completion_queue_destroy(cq);
grpc_server_credentials_release(ssl_creds);
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
grpc_slice_unref(ca_slice);
}
} // namespace

@ -41,9 +41,9 @@
#include "src/core/lib/gprpp/crash.h"
#include "src/core/lib/gprpp/thd.h"
#include "src/core/lib/iomgr/load_file.h"
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
#include "test/core/util/tls_utils.h"
#define SSL_CERT_PATH "src/core/tsi/test_creds/server1.pem"
#define SSL_KEY_PATH "src/core/tsi/test_creds/server1.key"
@ -56,22 +56,15 @@ static void server_thread(void* arg) {
const int port = *static_cast<int*>(arg);
// Load key pair and establish server SSL credentials.
std::string ca_cert = grpc_core::testing::GetFileContents(SSL_CA_PATH);
std::string cert = grpc_core::testing::GetFileContents(SSL_CERT_PATH);
std::string key = grpc_core::testing::GetFileContents(SSL_KEY_PATH);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
grpc_slice ca_slice, cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SSL_CA_PATH, 1, &ca_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SSL_KEY_PATH, 1, &key_slice)));
const char* ca_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
pem_key_cert_pair.private_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
pem_key_cert_pair.cert_chain =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
pem_key_cert_pair.private_key = key.c_str();
pem_key_cert_pair.cert_chain = cert.c_str();
grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
ca_cert, &pem_key_cert_pair, 1, 0, nullptr);
ca_cert.c_str(), &pem_key_cert_pair, 1, 0, nullptr);
// Start server listening on local port.
std::string addr = absl::StrCat("127.0.0.1:", port);
@ -106,9 +99,6 @@ static void server_thread(void* arg) {
grpc_server_destroy(server);
grpc_completion_queue_destroy(cq);
grpc_server_credentials_release(ssl_creds);
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
grpc_slice_unref(ca_slice);
}
// This test launches a minimal TLS grpc server on a separate thread and then
@ -123,26 +113,19 @@ static bool verify_peer_options_test(verify_peer_options* verify_options) {
// Load key pair and establish client SSL credentials.
// NOTE: we intentionally load the credential files before starting
// the server thread because grpc_load_file can experience trouble
// the server thread because loading the file can experience trouble
// when two threads attempt to load the same file concurrently
// and server thread also reads the same files as soon as it starts.
// See https://github.com/grpc/grpc/issues/23503 for details.
std::string ca_cert = grpc_core::testing::GetFileContents(SSL_CA_PATH);
std::string cert = grpc_core::testing::GetFileContents(SSL_CERT_PATH);
std::string key = grpc_core::testing::GetFileContents(SSL_KEY_PATH);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
grpc_slice ca_slice, cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SSL_CA_PATH, 1, &ca_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SSL_KEY_PATH, 1, &key_slice)));
const char* ca_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
pem_key_cert_pair.private_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
pem_key_cert_pair.cert_chain =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
pem_key_cert_pair.private_key = key.c_str();
pem_key_cert_pair.cert_chain = cert.c_str();
grpc_channel_credentials* ssl_creds = grpc_ssl_credentials_create(
ca_cert, &pem_key_cert_pair, verify_options, nullptr);
ca_cert.c_str(), &pem_key_cert_pair, verify_options, nullptr);
// Launch the gRPC server thread.
bool ok;
@ -193,9 +176,6 @@ static bool verify_peer_options_test(verify_peer_options* verify_options) {
grpc_channel_destroy(channel);
grpc_channel_credentials_release(ssl_creds);
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
grpc_slice_unref(ca_slice);
// Now that the client is completely cleaned up, trigger the server to
// shutdown
@ -242,11 +222,7 @@ int main(int argc, char* argv[]) {
verify_peer_options verify_options;
// Load the server's cert so that we can assert it gets passed to the callback
grpc_slice cert_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
const char* server_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
std::string server_cert = grpc_core::testing::GetFileContents(SSL_CERT_PATH);
// Running with all-null values should have no effect
verify_options.verify_peer_callback = nullptr;
@ -264,7 +240,7 @@ int main(int argc, char* argv[]) {
verify_options.verify_peer_destruct = verify_destruct;
GPR_ASSERT(verify_peer_options_test(&verify_options));
GPR_ASSERT(strcmp(callback_target_host, "foo.test.google.fr") == 0);
GPR_ASSERT(strcmp(callback_target_pem, server_cert) == 0);
GPR_ASSERT(strcmp(callback_target_pem, server_cert.c_str()) == 0);
GPR_ASSERT(callback_userdata == static_cast<void*>(&userdata));
GPR_ASSERT(destruct_userdata == static_cast<void*>(&userdata));
@ -272,8 +248,6 @@ int main(int argc, char* argv[]) {
callback_return_value = 1;
GPR_ASSERT(!verify_peer_options_test(&verify_options));
grpc_slice_unref(cert_slice);
grpc_shutdown();
return 0;
}

@ -132,21 +132,6 @@ grpc_cc_test(
],
)
grpc_cc_test(
name = "load_file_test",
srcs = ["load_file_test.cc"],
external_deps = ["gtest"],
language = "C++",
uses_event_engine = False,
uses_polling = False,
deps = [
"//:gpr",
"//:grpc",
"//test/core/util:grpc_test_util",
"//test/core/util:grpc_test_util_base",
],
)
grpc_cc_test(
name = "resolve_address_using_ares_resolver_posix_test",
srcs = ["resolve_address_posix_test.cc"],

@ -1,161 +0,0 @@
//
//
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
#include "src/core/lib/iomgr/load_file.h"
#include <stdio.h>
#include <string.h>
#include <gtest/gtest.h>
#include <grpc/grpc.h>
#include <grpc/slice.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include "src/core/lib/gpr/string.h"
#include "src/core/lib/gpr/tmpfile.h"
#include "src/core/lib/gprpp/crash.h"
#include "test/core/util/test_config.h"
#define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x)
static const char prefix[] = "file_test";
TEST(LoadFileTest, TestLoadEmptyFile) {
FILE* tmp = nullptr;
grpc_slice slice;
grpc_slice slice_with_null_term;
grpc_error_handle error;
char* tmp_name;
LOG_TEST_NAME("test_load_empty_file");
tmp = gpr_tmpfile(prefix, &tmp_name);
ASSERT_NE(tmp_name, nullptr);
ASSERT_NE(tmp, nullptr);
fclose(tmp);
error = grpc_load_file(tmp_name, 0, &slice);
ASSERT_TRUE(error.ok());
ASSERT_EQ(GRPC_SLICE_LENGTH(slice), 0);
error = grpc_load_file(tmp_name, 1, &slice_with_null_term);
ASSERT_TRUE(error.ok());
ASSERT_EQ(GRPC_SLICE_LENGTH(slice_with_null_term), 1);
ASSERT_EQ(GRPC_SLICE_START_PTR(slice_with_null_term)[0], 0);
remove(tmp_name);
gpr_free(tmp_name);
grpc_slice_unref(slice);
grpc_slice_unref(slice_with_null_term);
}
TEST(LoadFileTest, TestLoadFailure) {
FILE* tmp = nullptr;
grpc_slice slice;
grpc_error_handle error;
char* tmp_name;
LOG_TEST_NAME("test_load_failure");
tmp = gpr_tmpfile(prefix, &tmp_name);
ASSERT_NE(tmp_name, nullptr);
ASSERT_NE(tmp, nullptr);
fclose(tmp);
remove(tmp_name);
error = grpc_load_file(tmp_name, 0, &slice);
ASSERT_FALSE(error.ok());
ASSERT_EQ(GRPC_SLICE_LENGTH(slice), 0);
gpr_free(tmp_name);
grpc_slice_unref(slice);
}
TEST(LoadFileTest, TestLoadSmallFile) {
FILE* tmp = nullptr;
grpc_slice slice;
grpc_slice slice_with_null_term;
grpc_error_handle error;
char* tmp_name;
const char* blah = "blah";
LOG_TEST_NAME("test_load_small_file");
tmp = gpr_tmpfile(prefix, &tmp_name);
ASSERT_NE(tmp_name, nullptr);
ASSERT_NE(tmp, nullptr);
ASSERT_EQ(fwrite(blah, 1, strlen(blah), tmp), strlen(blah));
fclose(tmp);
error = grpc_load_file(tmp_name, 0, &slice);
ASSERT_TRUE(error.ok());
ASSERT_EQ(GRPC_SLICE_LENGTH(slice), strlen(blah));
ASSERT_FALSE(memcmp(GRPC_SLICE_START_PTR(slice), blah, strlen(blah)));
error = grpc_load_file(tmp_name, 1, &slice_with_null_term);
ASSERT_TRUE(error.ok());
ASSERT_EQ(GRPC_SLICE_LENGTH(slice_with_null_term), (strlen(blah) + 1));
ASSERT_STREQ((const char*)GRPC_SLICE_START_PTR(slice_with_null_term), blah);
remove(tmp_name);
gpr_free(tmp_name);
grpc_slice_unref(slice);
grpc_slice_unref(slice_with_null_term);
}
TEST(LoadFileTest, TestLoadBigFile) {
FILE* tmp = nullptr;
grpc_slice slice;
grpc_error_handle error;
char* tmp_name;
static const size_t buffer_size = 124631;
unsigned char* buffer = static_cast<unsigned char*>(gpr_malloc(buffer_size));
unsigned char* current;
size_t i;
LOG_TEST_NAME("test_load_big_file");
memset(buffer, 42, buffer_size);
tmp = gpr_tmpfile(prefix, &tmp_name);
ASSERT_NE(tmp, nullptr);
ASSERT_NE(tmp_name, nullptr);
ASSERT_EQ(fwrite(buffer, 1, buffer_size, tmp), buffer_size);
fclose(tmp);
error = grpc_load_file(tmp_name, 0, &slice);
ASSERT_TRUE(error.ok());
ASSERT_EQ(GRPC_SLICE_LENGTH(slice), buffer_size);
current = GRPC_SLICE_START_PTR(slice);
for (i = 0; i < buffer_size; i++) {
ASSERT_EQ(current[i], 42);
}
remove(tmp_name);
gpr_free(tmp_name);
grpc_slice_unref(slice);
gpr_free(buffer);
}
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
::testing::InitGoogleTest(&argc, argv);
grpc::testing::TestGrpcScope grpc_scope;
return RUN_ALL_TESTS();
}

@ -24,20 +24,17 @@
#include <grpc/support/log.h>
#include "src/core/lib/gprpp/crash.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/security/credentials/jwt/jwt_credentials.h"
#include "test/core/util/cmdline.h"
#include "test/core/util/tls_utils.h"
void create_jwt(const char* json_key_file_path, const char* service_url,
const char* scope) {
grpc_auth_json_key key;
char* jwt;
grpc_slice json_key_data;
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(json_key_file_path, 1, &json_key_data)));
key = grpc_auth_json_key_create_from_string(
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(json_key_data));
grpc_slice_unref(json_key_data);
std::string json_key_data =
grpc_core::testing::GetFileContents(json_key_file_path);
key = grpc_auth_json_key_create_from_string(json_key_data.c_str());
if (!grpc_auth_json_key_is_valid(&key)) {
fprintf(stderr, "Could not parse json key.\n");
fflush(stderr);

@ -29,12 +29,12 @@
#include "src/core/lib/gprpp/crash.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/security/credentials/credentials.h"
#include "src/core/lib/security/util/json_util.h"
#include "src/cpp/client/secure_credentials.h"
#include "test/core/security/oauth2_utils.h"
#include "test/core/util/cmdline.h"
#include "test/core/util/tls_utils.h"
static grpc_call_credentials* create_sts_creds(const char* json_file_path) {
grpc::experimental::StsCredentialsOptions options;
@ -45,13 +45,10 @@ static grpc_call_credentials* create_sts_creds(const char* json_file_path) {
return nullptr;
}
} else {
grpc_slice sts_options_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(json_file_path, 1, &sts_options_slice)));
auto status = grpc::experimental::StsCredentialsOptionsFromJson(
reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(sts_options_slice)),
&options);
grpc_slice_unref(sts_options_slice);
std::string sts_options =
grpc_core::testing::GetFileContents(json_file_path);
auto status = grpc::experimental::StsCredentialsOptionsFromJson(sts_options,
&options);
if (!status.ok()) {
gpr_log(GPR_ERROR, "%s", status.error_message().c_str());
return nullptr;
@ -65,15 +62,10 @@ static grpc_call_credentials* create_sts_creds(const char* json_file_path) {
static grpc_call_credentials* create_refresh_token_creds(
const char* json_refresh_token_file_path) {
grpc_slice refresh_token;
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file",
grpc_load_file(json_refresh_token_file_path, 1, &refresh_token)));
grpc_call_credentials* result = grpc_google_refresh_token_credentials_create(
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(refresh_token),
nullptr);
grpc_slice_unref(refresh_token);
return result;
std::string refresh_token =
grpc_core::testing::GetFileContents(json_refresh_token_file_path);
return grpc_google_refresh_token_credentials_create(refresh_token.c_str(),
nullptr);
}
int main(int argc, char** argv) {

@ -23,7 +23,6 @@
#include "src/core/lib/event_engine/default_event_engine.h"
#include "src/core/lib/gprpp/crash.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/security/credentials/credentials.h"
#include "src/core/lib/security/security_connector/security_connector.h"
#include "test/core/util/mock_endpoint.h"
@ -72,25 +71,15 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
grpc_mock_endpoint_finish_put_reads(mock_endpoint);
// Load key pair and establish server SSL credentials.
grpc_slice ca_slice, cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
const char* ca_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
const char* server_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
const char* server_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
std::string ca_cert = grpc_core::testing::GetFileContents(CA_CERT_PATH);
std::string server_cert =
grpc_core::testing::GetFileContents(SERVER_CERT_PATH);
std::string server_key =
grpc_core::testing::GetFileContents(SERVER_KEY_PATH);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key.c_str(),
server_cert.c_str()};
grpc_server_credentials* creds = grpc_ssl_server_credentials_create(
ca_cert, &pem_key_cert_pair, 1, 0, nullptr);
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
grpc_slice_unref(ca_slice);
ca_cert.c_str(), &pem_key_cert_pair, 1, 0, nullptr);
// Create security connector
grpc_core::RefCountedPtr<grpc_server_security_connector> sc =

@ -34,15 +34,16 @@
#include "src/core/lib/gpr/tmpfile.h"
#include "src/core/lib/gprpp/crash.h"
#include "src/core/lib/gprpp/env.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/security/context/security_context.h"
#include "src/core/lib/security/security_connector/load_system_roots.h"
#include "src/core/lib/security/security_connector/load_system_roots_supported.h"
#include "src/core/lib/security/security_connector/security_connector.h"
#include "src/core/lib/slice/slice.h"
#include "src/core/lib/slice/slice_string_helpers.h"
#include "src/core/tsi/ssl_transport_security.h"
#include "src/core/tsi/transport_security.h"
#include "test/core/util/test_config.h"
#include "test/core/util/tls_utils.h"
namespace grpc {
namespace {
@ -68,21 +69,12 @@ TEST(CreateRootCertsBundleTest, ReturnsEmpty) {
TEST(CreateRootCertsBundleTest, BundlesCorrectly) {
// Test that CreateRootCertsBundle returns a correct slice.
grpc_slice roots_bundle = grpc_empty_slice();
GRPC_LOG_IF_ERROR(
"load_file",
grpc_load_file("test/core/security/etc/bundle.pem", 1, &roots_bundle));
std::string roots_bundle =
grpc_core::testing::GetFileContents("test/core/security/etc/bundle.pem");
// result_slice should have the same content as roots_bundle.
grpc_slice result_slice =
grpc_core::CreateRootCertsBundle("test/core/security/etc/test_roots");
char* result_str = grpc_slice_to_c_string(result_slice);
char* bundle_str = grpc_slice_to_c_string(roots_bundle);
EXPECT_STREQ(result_str, bundle_str);
// Clean up.
gpr_free(result_str);
gpr_free(bundle_str);
grpc_slice_unref(roots_bundle);
grpc_slice_unref(result_slice);
grpc_core::Slice result_slice(
grpc_core::CreateRootCertsBundle("test/core/security/etc/test_roots"));
EXPECT_EQ(result_slice.as_string_view(), roots_bundle);
}
} // namespace

@ -32,7 +32,6 @@
#include "src/core/lib/config/config_vars.h"
#include "src/core/lib/gprpp/crash.h"
#include "src/core/lib/gprpp/unique_type_name.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/security/context/security_context.h"
#include "src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.h"
#include "src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h"
@ -61,36 +60,14 @@ class TlsSecurityConnectorTest : public ::testing::Test {
TlsSecurityConnectorTest() {}
void SetUp() override {
grpc_slice ca_slice_1, ca_slice_0, cert_slice_1, key_slice_1, cert_slice_0,
key_slice_0;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(CA_CERT_PATH, 1, &ca_slice_1)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(CLIENT_CERT_PATH, 1, &ca_slice_0)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_CERT_PATH_1, 1, &cert_slice_1)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_KEY_PATH_1, 1, &key_slice_1)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_CERT_PATH_0, 1, &cert_slice_0)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_KEY_PATH_0, 1, &key_slice_0)));
root_cert_1_ = std::string(StringViewFromSlice(ca_slice_1));
root_cert_0_ = std::string(StringViewFromSlice(ca_slice_0));
std::string identity_key_1 = std::string(StringViewFromSlice(key_slice_1));
std::string identity_key_0 = std::string(StringViewFromSlice(key_slice_0));
std::string identity_cert_1 =
std::string(StringViewFromSlice(cert_slice_1));
std::string identity_cert_0 =
std::string(StringViewFromSlice(cert_slice_0));
identity_pairs_1_.emplace_back(identity_key_1, identity_cert_1);
identity_pairs_0_.emplace_back(identity_key_0, identity_cert_0);
grpc_slice_unref(ca_slice_1);
grpc_slice_unref(ca_slice_0);
grpc_slice_unref(cert_slice_1);
grpc_slice_unref(key_slice_1);
grpc_slice_unref(cert_slice_0);
grpc_slice_unref(key_slice_0);
root_cert_1_ = grpc_core::testing::GetFileContents(CA_CERT_PATH);
root_cert_0_ = grpc_core::testing::GetFileContents(CLIENT_CERT_PATH);
identity_pairs_1_.emplace_back(
grpc_core::testing::GetFileContents(SERVER_KEY_PATH_1),
grpc_core::testing::GetFileContents(SERVER_CERT_PATH_1));
identity_pairs_0_.emplace_back(
grpc_core::testing::GetFileContents(SERVER_KEY_PATH_0),
grpc_core::testing::GetFileContents(SERVER_CERT_PATH_0));
}
static void VerifyExpectedErrorCallback(void* arg, grpc_error_handle error) {

@ -33,9 +33,9 @@
#include "src/core/lib/gprpp/host_port.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/exec_ctx.h"
#include "src/core/lib/iomgr/load_file.h"
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
#include "test/core/util/tls_utils.h"
#define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
@ -178,14 +178,10 @@ static const test_fixture insecure_test = {
};
static grpc_channel* secure_test_create_channel(const char* addr) {
grpc_slice ca_slice;
EXPECT_TRUE(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
const char* test_root_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
grpc_channel_credentials* ssl_creds =
grpc_ssl_credentials_create(test_root_cert, nullptr, nullptr, nullptr);
grpc_slice_unref(ca_slice);
std::string test_root_cert =
grpc_core::testing::GetFileContents(CA_CERT_PATH);
grpc_channel_credentials* ssl_creds = grpc_ssl_credentials_create(
test_root_cert.c_str(), nullptr, nullptr, nullptr);
grpc_arg ssl_name_override = {
GRPC_ARG_STRING,
const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),

@ -35,9 +35,9 @@
#include "src/core/lib/gprpp/host_port.h"
#include "src/core/lib/gprpp/thd.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
#include "test/core/util/tls_utils.h"
#define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
#define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
@ -165,20 +165,13 @@ static void insecure_test_add_port(grpc_server* server, const char* addr) {
}
static void secure_test_add_port(grpc_server* server, const char* addr) {
grpc_slice cert_slice, key_slice;
ASSERT_TRUE(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
ASSERT_TRUE(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
const char* server_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
const char* server_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
std::string server_cert =
grpc_core::testing::GetFileContents(SERVER_CERT_PATH);
std::string server_key = grpc_core::testing::GetFileContents(SERVER_KEY_PATH);
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key.c_str(),
server_cert.c_str()};
grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
nullptr, &pem_key_cert_pair, 1, 0, nullptr);
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
grpc_server_add_http2_port(server, addr, ssl_creds);
grpc_server_credentials_release(ssl_creds);
}
@ -194,14 +187,10 @@ TEST(SequentialConnectivityTest, MainTest) {
run_test(&insecure_test, /*share_subchannel=*/true);
run_test(&insecure_test, /*share_subchannel=*/false);
grpc_slice ca_slice;
ASSERT_TRUE(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
const char* test_root_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
grpc_channel_credentials* ssl_creds =
grpc_ssl_credentials_create(test_root_cert, nullptr, nullptr, nullptr);
grpc_slice_unref(ca_slice);
std::string test_root_cert =
grpc_core::testing::GetFileContents(CA_CERT_PATH);
grpc_channel_credentials* ssl_creds = grpc_ssl_credentials_create(
test_root_cert.c_str(), nullptr, nullptr, nullptr);
const test_fixture secure_test = {
"secure",
secure_test_add_port,

@ -36,13 +36,13 @@
#include "src/core/lib/gprpp/crash.h"
#include "src/core/lib/gprpp/memory.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/security/security_connector/security_connector.h"
#include "src/core/tsi/transport_security.h"
#include "src/core/tsi/transport_security_interface.h"
#include "test/core/tsi/transport_security_test_lib.h"
#include "test/core/util/build.h"
#include "test/core/util/test_config.h"
#include "test/core/util/tls_utils.h"
#define SSL_TSI_TEST_ALPN1 "foo"
#define SSL_TSI_TEST_ALPN2 "toto"
@ -504,17 +504,9 @@ static const struct tsi_test_fixture_vtable vtable = {
ssl_test_setup_handshakers, ssl_test_check_handshaker_peers,
ssl_test_destruct};
static char* load_file(const char* dir_path, const char* file_name) {
char* file_path = static_cast<char*>(
gpr_zalloc(sizeof(char) * (strlen(dir_path) + strlen(file_name) + 1)));
memcpy(file_path, dir_path, strlen(dir_path));
memcpy(file_path + strlen(dir_path), file_name, strlen(file_name));
grpc_slice slice;
EXPECT_EQ(grpc_load_file(file_path, 1, &slice), absl::OkStatus());
char* data = grpc_slice_to_c_string(slice);
grpc_slice_unref(slice);
gpr_free(file_path);
return data;
static char* load_file(std::string path) {
std::string data = grpc_core::testing::GetFileContents(path);
return gpr_strdup(data.c_str());
}
static bool is_slow_build() {
@ -575,32 +567,30 @@ static tsi_test_fixture* ssl_tsi_test_fixture_create() {
sizeof(tsi_ssl_pem_key_cert_pair) *
key_cert_lib->leaf_signed_by_intermediate_num_key_cert_pairs));
key_cert_lib->server_pem_key_cert_pairs[0].private_key =
load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "server0.key");
load_file(SSL_TSI_TEST_CREDENTIALS_DIR "server0.key");
key_cert_lib->server_pem_key_cert_pairs[0].cert_chain =
load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "server0.pem");
load_file(SSL_TSI_TEST_CREDENTIALS_DIR "server0.pem");
key_cert_lib->server_pem_key_cert_pairs[1].private_key =
load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "server1.key");
load_file(SSL_TSI_TEST_CREDENTIALS_DIR "server1.key");
key_cert_lib->server_pem_key_cert_pairs[1].cert_chain =
load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "server1.pem");
load_file(SSL_TSI_TEST_CREDENTIALS_DIR "server1.pem");
key_cert_lib->bad_server_pem_key_cert_pairs[0].private_key =
load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "badserver.key");
load_file(SSL_TSI_TEST_CREDENTIALS_DIR "badserver.key");
key_cert_lib->bad_server_pem_key_cert_pairs[0].cert_chain =
load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "badserver.pem");
load_file(SSL_TSI_TEST_CREDENTIALS_DIR "badserver.pem");
key_cert_lib->client_pem_key_cert_pair.private_key =
load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "client.key");
load_file(SSL_TSI_TEST_CREDENTIALS_DIR "client.key");
key_cert_lib->client_pem_key_cert_pair.cert_chain =
load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "client.pem");
load_file(SSL_TSI_TEST_CREDENTIALS_DIR "client.pem");
key_cert_lib->bad_client_pem_key_cert_pair.private_key =
load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "badclient.key");
load_file(SSL_TSI_TEST_CREDENTIALS_DIR "badclient.key");
key_cert_lib->bad_client_pem_key_cert_pair.cert_chain =
load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "badclient.pem");
load_file(SSL_TSI_TEST_CREDENTIALS_DIR "badclient.pem");
key_cert_lib->leaf_signed_by_intermediate_key_cert_pairs[0].private_key =
load_file(SSL_TSI_TEST_CREDENTIALS_DIR,
"leaf_signed_by_intermediate.key");
load_file(SSL_TSI_TEST_CREDENTIALS_DIR "leaf_signed_by_intermediate.key");
key_cert_lib->leaf_signed_by_intermediate_key_cert_pairs[0].cert_chain =
load_file(SSL_TSI_TEST_CREDENTIALS_DIR,
"leaf_and_intermediate_chain.pem");
key_cert_lib->root_cert = load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "ca.pem");
load_file(SSL_TSI_TEST_CREDENTIALS_DIR "leaf_and_intermediate_chain.pem");
key_cert_lib->root_cert = load_file(SSL_TSI_TEST_CREDENTIALS_DIR "ca.pem");
key_cert_lib->root_store =
tsi_ssl_root_certs_store_create(key_cert_lib->root_cert);
EXPECT_NE(key_cert_lib->root_store, nullptr);
@ -979,7 +969,7 @@ static tsi_ssl_handshaker_factory_vtable test_handshaker_factory_vtable = {
void test_tsi_ssl_client_handshaker_factory_refcounting() {
int i;
char* cert_chain = load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "client.pem");
char* cert_chain = load_file(SSL_TSI_TEST_CREDENTIALS_DIR "client.pem");
tsi_ssl_client_handshaker_options options;
options.pem_root_certs = cert_chain;
@ -1027,12 +1017,11 @@ void test_tsi_ssl_server_handshaker_factory_refcounting() {
tsi_ssl_server_handshaker_factory* server_handshaker_factory;
tsi_handshaker* handshaker[3];
const char* cert_chain =
load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "server0.pem");
load_file(SSL_TSI_TEST_CREDENTIALS_DIR "server0.pem");
tsi_ssl_pem_key_cert_pair cert_pair;
cert_pair.cert_chain = cert_chain;
cert_pair.private_key =
load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "server0.key");
cert_pair.private_key = load_file(SSL_TSI_TEST_CREDENTIALS_DIR "server0.key");
tsi_ssl_server_handshaker_options options;
options.pem_key_cert_pairs = &cert_pair;
options.num_key_cert_pairs = 1;
@ -1091,7 +1080,7 @@ void ssl_tsi_test_handshaker_factory_internals() {
void ssl_tsi_test_duplicate_root_certificates() {
gpr_log(GPR_INFO, "ssl_tsi_test_duplicate_root_certificates");
char* root_cert = load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "ca.pem");
char* root_cert = load_file(SSL_TSI_TEST_CREDENTIALS_DIR "ca.pem");
char* dup_root_cert = static_cast<char*>(
gpr_zalloc(sizeof(char) * (strlen(root_cert) * 2 + 1)));
memcpy(dup_root_cert, root_cert, strlen(root_cert));
@ -1107,7 +1096,7 @@ void ssl_tsi_test_duplicate_root_certificates() {
void ssl_tsi_test_extract_x509_subject_names() {
gpr_log(GPR_INFO, "ssl_tsi_test_extract_x509_subject_names");
char* cert = load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "multi-domain.pem");
char* cert = load_file(SSL_TSI_TEST_CREDENTIALS_DIR "multi-domain.pem");
tsi_peer peer;
ASSERT_EQ(tsi_ssl_extract_x509_subject_names_from_pem_cert(cert, &peer),
TSI_OK);
@ -1211,8 +1200,8 @@ void ssl_tsi_test_extract_x509_subject_names() {
void ssl_tsi_test_extract_cert_chain() {
gpr_log(GPR_INFO, "ssl_tsi_test_extract_cert_chain");
char* cert = load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "server1.pem");
char* ca = load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "ca.pem");
char* cert = load_file(SSL_TSI_TEST_CREDENTIALS_DIR "server1.pem");
char* ca = load_file(SSL_TSI_TEST_CREDENTIALS_DIR "ca.pem");
char* chain = static_cast<char*>(
gpr_zalloc(sizeof(char) * (strlen(cert) + strlen(ca) + 1)));
memcpy(chain, cert, strlen(cert));

@ -148,6 +148,7 @@ grpc_cc_library(
"//src/core:grpc_sockaddr",
"//src/core:iomgr_fwd",
"//src/core:iomgr_port",
"//src/core:load_file",
"//src/core:resolved_address",
"//src/core:slice",
"//src/core:slice_refcount",

@ -36,9 +36,9 @@
#include "src/core/lib/gprpp/env.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "test/core/util/test_config.h"
#include "test/cpp/util/test_config.h"
#include "test/cpp/util/tls_utils.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
extern bool squelch;
@ -54,16 +54,13 @@ TEST_P(FuzzerCorpusTest, RunOneExample) {
// implementations of that function will initialize and shutdown gRPC
// internally.
fprintf(stderr, "Example file: %s\n", GetParam().c_str());
grpc_slice buffer;
squelch = false;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(GetParam().c_str(), 0, &buffer)));
size_t length = GRPC_SLICE_LENGTH(buffer);
std::string buffer = grpc_core::testing::GetFileContents(GetParam());
size_t length = buffer.size();
void* data = gpr_malloc(length);
if (length > 0) {
memcpy(data, GRPC_SLICE_START_PTR(buffer), length);
memcpy(data, buffer.data(), length);
}
grpc_slice_unref(buffer);
LLVMFuzzerTestOneInput(static_cast<uint8_t*>(data), length);
gpr_free(data);
}

@ -23,7 +23,7 @@
#include "src/core/lib/gprpp/crash.h"
#include "src/core/lib/iomgr/exec_ctx.h"
#include "src/core/lib/iomgr/load_file.h"
#include "test/cpp/util/tls_utils.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
@ -31,19 +31,10 @@ extern bool squelch;
extern bool leak_check;
int main(int argc, char** argv) {
grpc_slice buffer;
squelch = false;
leak_check = false;
// TODO(yashkt) Calling grpc_init breaks tests. Fix the tests and replace
// grpc_core::ExecCtx::GlobalInit with grpc_init and GlobalShutdown with
// grpc_shutdown
GPR_ASSERT(argc > 1); // Make sure that we have a filename argument
GPR_ASSERT(
GRPC_LOG_IF_ERROR("load_file", grpc_load_file(argv[1], 0, &buffer)));
LLVMFuzzerTestOneInput(GRPC_SLICE_START_PTR(buffer),
GRPC_SLICE_LENGTH(buffer));
grpc_core::ExecCtx::GlobalInit();
grpc_slice_unref(buffer);
grpc_core::ExecCtx::GlobalShutdown();
std::string buffer = grpc_core::testing::GetFileContents(argv[1]);
LLVMFuzzerTestOneInput(buffer.data(), buffer.size());
return 0;
}

@ -17,6 +17,8 @@
#include <stdio.h>
#include "absl/strings/str_cat.h"
#include <grpc/slice.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
@ -24,8 +26,8 @@
#include <grpc/support/time.h>
#include "src/core/lib/gpr/tmpfile.h"
#include "src/core/lib/gprpp/load_file.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/slice/slice_internal.h"
#include "test/core/util/test_config.h"
@ -77,11 +79,12 @@ PemKeyCertPairList MakeCertKeyPairs(absl::string_view private_key,
}
std::string GetFileContents(const char* path) {
grpc_slice slice = grpc_empty_slice();
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file", grpc_load_file(path, 0, &slice)));
std::string data = std::string(StringViewFromSlice(slice));
grpc_slice_unref(slice);
return data;
auto slice = LoadFile(path, /*add_null_terminator=*/false);
if (!slice.ok()) {
Crash(absl::StrCat("error loading file ", path, ": ",
slice.status().ToString()));
}
return std::string(slice->as_string_view());
}
int SyncExternalVerifier::Verify(void* user_data,

@ -37,7 +37,6 @@
#include "src/core/lib/event_engine/default_event_engine.h"
#include "src/core/lib/gprpp/env.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.h"
#include "src/core/lib/security/security_connector/ssl_utils.h"
#include "src/core/lib/slice/slice_internal.h"
@ -50,6 +49,7 @@
#include "test/core/util/test_config.h"
#include "test/cpp/end2end/test_service_impl.h"
#include "test/cpp/util/test_credentials_provider.h"
#include "test/cpp/util/tls_utils.h"
using grpc::channelz::v1::Address;
using grpc::channelz::v1::GetChannelRequest;
@ -134,15 +134,6 @@ constexpr char kServerKeyPath[] = "src/core/tsi/test_creds/server1.key";
constexpr char kClientCertPath[] = "src/core/tsi/test_creds/client.pem";
constexpr char kClientKeyPath[] = "src/core/tsi/test_creds/client.key";
std::string ReadFile(const char* file_path) {
grpc_slice slice;
GPR_ASSERT(
GRPC_LOG_IF_ERROR("load_file", grpc_load_file(file_path, 0, &slice)));
std::string file_contents(grpc_core::StringViewFromSlice(slice));
grpc_slice_unref(slice);
return file_contents;
}
std::shared_ptr<grpc::ChannelCredentials> GetChannelCredentials(
CredentialsType type, ChannelArguments* args) {
if (type == CredentialsType::kInsecure) {
@ -150,11 +141,11 @@ std::shared_ptr<grpc::ChannelCredentials> GetChannelCredentials(
}
args->SetSslTargetNameOverride("foo.test.google.fr");
std::vector<experimental::IdentityKeyCertPair> identity_key_cert_pairs = {
{ReadFile(kClientKeyPath), ReadFile(kClientCertPath)}};
GetFileContents(kClientKeyPath), GetFileContents(kClientCertPath)};
grpc::experimental::TlsChannelCredentialsOptions options;
options.set_certificate_provider(
std::make_shared<grpc::experimental::StaticDataCertificateProvider>(
ReadFile(kCaCertPath), identity_key_cert_pairs));
GetFileContents(kCaCertPath), identity_key_cert_pairs));
if (type == CredentialsType::kMtls) {
options.watch_identity_key_cert_pairs();
}
@ -168,10 +159,10 @@ std::shared_ptr<grpc::ServerCredentials> GetServerCredentials(
return InsecureServerCredentials();
}
std::vector<experimental::IdentityKeyCertPair> identity_key_cert_pairs = {
{ReadFile(kServerKeyPath), ReadFile(kServerCertPath)}};
{GetFileContents(kServerKeyPath), GetFileContents(kServerCertPath)}};
auto certificate_provider =
std::make_shared<grpc::experimental::StaticDataCertificateProvider>(
ReadFile(kCaCertPath), identity_key_cert_pairs);
GetFileContents(kCaCertPath), identity_key_cert_pairs);
grpc::experimental::TlsServerCredentialsOptions options(certificate_provider);
options.watch_root_certs();
options.watch_identity_key_cert_pairs();
@ -711,7 +702,7 @@ TEST_P(ChannelzServerTest, ManySubchannelsAndSockets) {
EXPECT_EQ(
RemoveWhitespaces(
get_socket_resp.socket().security().tls().remote_certificate()),
RemoveWhitespaces(ReadFile(kServerCertPath)));
RemoveWhitespaces(GetFileContents(kServerCertPath)));
break;
}
}
@ -777,7 +768,7 @@ TEST_P(ChannelzServerTest, StreamingRPC) {
.security()
.tls()
.remote_certificate()),
RemoveWhitespaces(ReadFile(kServerCertPath)));
RemoveWhitespaces(GetFileContents(kServerCertPath)));
break;
}
}
@ -829,7 +820,7 @@ TEST_P(ChannelzServerTest, GetServerSocketsTest) {
.security()
.tls()
.remote_certificate()),
RemoveWhitespaces(ReadFile(kClientCertPath)));
RemoveWhitespaces(GetFileContents(kClientCertPath)));
} else {
EXPECT_TRUE(get_socket_response.socket()
.security()

@ -25,7 +25,6 @@
#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/security/authorization/audit_logging.h"
#include "src/core/lib/security/authorization/grpc_authorization_policy_provider.h"
#include "src/core/lib/security/credentials/fake/fake_credentials.h"
@ -56,23 +55,14 @@ using experimental::RegisterAuditLoggerFactory;
using grpc_core::experimental::AuditLoggerRegistry;
using grpc_core::testing::TestAuditLoggerFactory;
std::string ReadFile(const char* file_path) {
grpc_slice slice;
GPR_ASSERT(
GRPC_LOG_IF_ERROR("load_file", grpc_load_file(file_path, 0, &slice)));
std::string file_contents(grpc_core::StringViewFromSlice(slice));
grpc_slice_unref(slice);
return file_contents;
}
class GrpcAuthzEnd2EndTest : public ::testing::Test {
protected:
GrpcAuthzEnd2EndTest()
: server_address_(
absl::StrCat("localhost:", grpc_pick_unused_port_or_die())) {
std::string root_cert = ReadFile(kCaCertPath);
std::string identity_cert = ReadFile(kServerCertPath);
std::string private_key = ReadFile(kServerKeyPath);
std::string root_cert = GetFileContents(kCaCertPath);
std::string identity_cert = GetFileContents(kServerCertPath);
std::string private_key = GetFileContents(kServerKeyPath);
std::vector<experimental::IdentityKeyCertPair>
server_identity_key_cert_pairs = {{private_key, identity_cert}};
grpc::experimental::TlsServerCredentialsOptions server_options(
@ -84,12 +74,12 @@ class GrpcAuthzEnd2EndTest : public ::testing::Test {
GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY);
server_creds_ = grpc::experimental::TlsServerCredentials(server_options);
std::vector<experimental::IdentityKeyCertPair>
channel_identity_key_cert_pairs = {
{ReadFile(kClientKeyPath), ReadFile(kClientCertPath)}};
channel_identity_key_cert_pairs = {{GetFileContents(kClientKeyPath),
GetFileContents(kClientCertPath)}};
grpc::experimental::TlsChannelCredentialsOptions channel_options;
channel_options.set_certificate_provider(
std::make_shared<grpc::experimental::StaticDataCertificateProvider>(
ReadFile(kCaCertPath), channel_identity_key_cert_pairs));
GetFileContents(kCaCertPath), channel_identity_key_cert_pairs));
channel_options.watch_identity_key_cert_pairs();
channel_options.watch_root_certs();
channel_creds_ = grpc::experimental::TlsCredentials(channel_options);

@ -29,9 +29,9 @@
#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>
#include "src/core/lib/iomgr/load_file.h"
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
#include "test/core/util/tls_utils.h"
#include "test/cpp/end2end/test_service_impl.h"
namespace grpc {
@ -45,21 +45,12 @@ constexpr char kClientCertPath[] = "src/core/tsi/test_creds/client.pem";
constexpr char kClientKeyPath[] = "src/core/tsi/test_creds/client.key";
constexpr char kMessage[] = "Hello";
std::string ReadFile(const std::string& file_path) {
grpc_slice slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(file_path.c_str(), 0, &slice)));
std::string file_contents(grpc_core::StringViewFromSlice(slice));
grpc_slice_unref(slice);
return file_contents;
}
class SslCredentialsTest : public ::testing::Test {
protected:
void RunServer(absl::Notification* notification) {
std::string root_cert = ReadFile(kCaCertPath);
std::string root_cert = GetFileContents(kCaCertPath);
grpc::SslServerCredentialsOptions::PemKeyCertPair key_cert_pair = {
ReadFile(kServerKeyPath), ReadFile(kServerCertPath)};
GetFileContents(kServerKeyPath), GetFileContents(kServerCertPath)};
// TODO(gtcooke94) Parametrize this test for TLS and mTLS as well
grpc::SslServerCredentialsOptions ssl_options;
ssl_options.pem_key_cert_pairs.push_back(key_cert_pair);
@ -132,9 +123,9 @@ TEST_F(SslCredentialsTest, SequentialResumption) {
server_thread_ = new std::thread([&]() { RunServer(&notification); });
notification.WaitForNotification();
std::string root_cert = ReadFile(kCaCertPath);
std::string client_key = ReadFile(kClientKeyPath);
std::string client_cert = ReadFile(kClientCertPath);
std::string root_cert = GetFileContents(kCaCertPath);
std::string client_key = GetFileContents(kClientKeyPath);
std::string client_cert = GetFileContents(kClientCertPath);
grpc::SslCredentialsOptions ssl_options;
ssl_options.pem_root_certs = root_cert;
ssl_options.pem_private_key = client_key;
@ -157,9 +148,9 @@ TEST_F(SslCredentialsTest, ConcurrentResumption) {
server_thread_ = new std::thread([&]() { RunServer(&notification); });
notification.WaitForNotification();
std::string root_cert = ReadFile(kCaCertPath);
std::string client_key = ReadFile(kClientKeyPath);
std::string client_cert = ReadFile(kClientCertPath);
std::string root_cert = GetFileContents(kCaCertPath);
std::string client_key = GetFileContents(kClientKeyPath);
std::string client_cert = GetFileContents(kClientCertPath);
grpc::SslCredentialsOptions ssl_options;
ssl_options.pem_root_certs = root_cert;
ssl_options.pem_private_key = client_key;
@ -189,9 +180,9 @@ TEST_F(SslCredentialsTest, ResumptionFailsDueToNoCapacityInCache) {
server_thread_ = new std::thread([&]() { RunServer(&notification); });
notification.WaitForNotification();
std::string root_cert = ReadFile(kCaCertPath);
std::string client_key = ReadFile(kClientKeyPath);
std::string client_cert = ReadFile(kClientCertPath);
std::string root_cert = GetFileContents(kCaCertPath);
std::string client_key = GetFileContents(kClientKeyPath);
std::string client_cert = GetFileContents(kClientCertPath);
grpc::SslCredentialsOptions ssl_options;
ssl_options.pem_root_certs = root_cert;
ssl_options.pem_private_key = client_key;

@ -31,9 +31,9 @@
#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>
#include "src/core/lib/iomgr/load_file.h"
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
#include "test/core/util/tls_utils.h"
#include "test/cpp/end2end/test_service_impl.h"
namespace grpc {
@ -48,15 +48,6 @@ constexpr char kServerCertPath[] = "src/core/tsi/test_creds/server1.pem";
constexpr char kServerKeyPath[] = "src/core/tsi/test_creds/server1.key";
constexpr char kMessage[] = "Hello";
std::string ReadFile(const std::string& file_path) {
grpc_slice slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(file_path.c_str(), 0, &slice)));
std::string file_contents(grpc_core::StringViewFromSlice(slice));
grpc_slice_unref(slice);
return file_contents;
}
class NoOpCertificateVerifier : public ExternalCertificateVerifier {
public:
~NoOpCertificateVerifier() override = default;
@ -75,9 +66,9 @@ class NoOpCertificateVerifier : public ExternalCertificateVerifier {
class TlsCredentialsTest : public ::testing::Test {
protected:
void RunServer(absl::Notification* notification) {
std::string root_cert = ReadFile(kCaCertPath);
std::string root_cert = GetFileContents(kCaCertPath);
grpc::SslServerCredentialsOptions::PemKeyCertPair key_cert_pair = {
ReadFile(kServerKeyPath), ReadFile(kServerCertPath)};
GetFileContents(kServerKeyPath), GetFileContents(kServerCertPath)};
grpc::SslServerCredentialsOptions ssl_options;
ssl_options.pem_key_cert_pairs.push_back(key_cert_pair);
ssl_options.pem_root_certs = root_cert;

@ -75,7 +75,6 @@
#include "src/core/lib/gprpp/sync.h"
#include "src/core/lib/gprpp/time.h"
#include "src/core/lib/gprpp/time_util.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/iomgr/sockaddr.h"
#include "src/core/lib/security/authorization/audit_logging.h"
#include "src/core/lib/security/certificate_provider/certificate_provider_registry.h"
@ -107,6 +106,7 @@
#include "test/core/util/resolve_localhost_ip46.h"
#include "test/core/util/scoped_env_var.h"
#include "test/core/util/test_config.h"
#include "test/core/util/tls_utils.h"
#include "test/cpp/end2end/xds/xds_end2end_test_lib.h"
#include "test/cpp/util/test_config.h"
#include "test/cpp/util/tls_test_utils.h"
@ -293,8 +293,8 @@ class XdsSecurityTest : public XdsEnd2endTest {
absl::StrJoin(fields, ",\n"));
InitClient(builder);
CreateAndStartBackends(2);
root_cert_ = ReadFile(kCaCertPath);
bad_root_cert_ = ReadFile(kBadClientCertPath);
root_cert_ = grpc_core::testing::GetFileContents(kCaCertPath);
bad_root_cert_ = grpc_core::testing::GetFileContents(kBadClientCertPath);
identity_pair_ = ReadTlsIdentityPair(kClientKeyPath, kClientCertPath);
// TODO(yashykt): Use different client certs here instead of reusing
// server certs after https://github.com/grpc/grpc/pull/24876 is merged
@ -922,8 +922,8 @@ class XdsServerSecurityTest : public XdsEnd2endTest {
absl::StrJoin(fields, ",\n"));
InitClient(builder);
CreateBackends(1, /*xds_enabled=*/true);
root_cert_ = ReadFile(kCaCertPath);
bad_root_cert_ = ReadFile(kBadClientCertPath);
root_cert_ = grpc_core::testing::GetFileContents(kCaCertPath);
bad_root_cert_ = grpc_core::testing::GetFileContents(kBadClientCertPath);
identity_pair_ = ReadTlsIdentityPair(kServerKeyPath, kServerCertPath);
bad_identity_pair_ =
ReadTlsIdentityPair(kBadClientKeyPath, kBadClientCertPath);
@ -986,12 +986,15 @@ class XdsServerSecurityTest : public XdsEnd2endTest {
args.SetInt(GRPC_ARG_USE_LOCAL_SUBCHANNEL_POOL, 1);
std::string uri = grpc_core::LocalIpUri(backends_[0]->port());
IdentityKeyCertPair key_cert_pair;
key_cert_pair.private_key = ReadFile(kServerKeyPath);
key_cert_pair.certificate_chain = ReadFile(kServerCertPath);
key_cert_pair.private_key =
grpc_core::testing::GetFileContents(kServerKeyPath);
key_cert_pair.certificate_chain =
grpc_core::testing::GetFileContents(kServerCertPath);
std::vector<IdentityKeyCertPair> identity_key_cert_pairs;
identity_key_cert_pairs.emplace_back(key_cert_pair);
auto certificate_provider = std::make_shared<StaticDataCertificateProvider>(
ReadFile(kCaCertPath), identity_key_cert_pairs);
grpc_core::testing::GetFileContents(kCaCertPath),
identity_key_cert_pairs);
grpc::experimental::TlsChannelCredentialsOptions options;
options.set_certificate_provider(std::move(certificate_provider));
options.watch_root_certs();
@ -1012,8 +1015,8 @@ class XdsServerSecurityTest : public XdsEnd2endTest {
std::string(grpc_core::LocalIp()));
args.SetInt(GRPC_ARG_USE_LOCAL_SUBCHANNEL_POOL, 1);
std::string uri = grpc_core::LocalIpUri(backends_[0]->port());
auto certificate_provider =
std::make_shared<StaticDataCertificateProvider>(ReadFile(kCaCertPath));
auto certificate_provider = std::make_shared<StaticDataCertificateProvider>(
grpc_core::testing::GetFileContents(kCaCertPath));
grpc::experimental::TlsChannelCredentialsOptions options;
options.set_certificate_provider(std::move(certificate_provider));
options.watch_root_certs();

@ -41,11 +41,11 @@
#include "src/core/ext/xds/xds_client_grpc.h"
#include "src/core/lib/gpr/tmpfile.h"
#include "src/core/lib/gprpp/env.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/surface/server.h"
#include "src/cpp/client/secure_credentials.h"
#include "src/proto/grpc/testing/xds/v3/router.grpc.pb.h"
#include "test/core/util/resolve_localhost_ip46.h"
#include "test/core/util/tls_utils.h"
#include "test/cpp/util/tls_test_utils.h"
namespace grpc {
@ -228,9 +228,11 @@ XdsEnd2endTest::BackendServerThread::Credentials() {
return XdsServerCredentials(InsecureServerCredentials());
} else {
// We are testing client's use of XdsCredentials
std::string root_cert = ReadFile(kCaCertPath);
std::string identity_cert = ReadFile(kServerCertPath);
std::string private_key = ReadFile(kServerKeyPath);
std::string root_cert = grpc_core::testing::GetFileContents(kCaCertPath);
std::string identity_cert =
grpc_core::testing::GetFileContents(kServerCertPath);
std::string private_key =
grpc_core::testing::GetFileContents(kServerKeyPath);
std::vector<experimental::IdentityKeyCertPair> identity_key_cert_pairs = {
{private_key, identity_cert}};
auto certificate_provider =
@ -797,30 +799,25 @@ std::string XdsEnd2endTest::MakeConnectionFailureRegex(
"Socket closed|FD shutdown)");
}
std::string XdsEnd2endTest::ReadFile(const char* file_path) {
grpc_slice slice;
GPR_ASSERT(
GRPC_LOG_IF_ERROR("load_file", grpc_load_file(file_path, 0, &slice)));
std::string file_contents(grpc_core::StringViewFromSlice(slice));
grpc_slice_unref(slice);
return file_contents;
}
grpc_core::PemKeyCertPairList XdsEnd2endTest::ReadTlsIdentityPair(
const char* key_path, const char* cert_path) {
return grpc_core::PemKeyCertPairList{
grpc_core::PemKeyCertPair(ReadFile(key_path), ReadFile(cert_path))};
return grpc_core::PemKeyCertPairList{grpc_core::PemKeyCertPair(
grpc_core::testing::GetFileContents(key_path),
grpc_core::testing::GetFileContents(cert_path))};
}
std::shared_ptr<ChannelCredentials>
XdsEnd2endTest::CreateTlsFallbackCredentials() {
IdentityKeyCertPair key_cert_pair;
key_cert_pair.private_key = ReadFile(kServerKeyPath);
key_cert_pair.certificate_chain = ReadFile(kServerCertPath);
key_cert_pair.private_key =
grpc_core::testing::GetFileContents(kServerKeyPath);
key_cert_pair.certificate_chain =
grpc_core::testing::GetFileContents(kServerCertPath);
std::vector<IdentityKeyCertPair> identity_key_cert_pairs;
identity_key_cert_pairs.emplace_back(key_cert_pair);
auto certificate_provider = std::make_shared<StaticDataCertificateProvider>(
ReadFile(kCaCertPath), identity_key_cert_pairs);
grpc_core::testing::GetFileContents(kCaCertPath),
identity_key_cert_pairs);
grpc::experimental::TlsChannelCredentialsOptions options;
options.set_certificate_provider(std::move(certificate_provider));
options.watch_root_certs();

@ -933,9 +933,6 @@ class XdsEnd2endTest : public ::testing::TestWithParam<XdsTestType>,
// message for a connection failure.
static std::string MakeConnectionFailureRegex(absl::string_view prefix);
// Returns the contents of the specified file.
static std::string ReadFile(const char* file_path);
// Returns a private key pair, read from local files.
static grpc_core::PemKeyCertPairList ReadTlsIdentityPair(
const char* key_path, const char* cert_path);

@ -38,7 +38,6 @@
#include "src/core/ext/xds/xds_client_grpc.h"
#include "src/core/lib/gpr/tmpfile.h"
#include "src/core/lib/gprpp/env.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/surface/server.h"
#include "src/cpp/client/secure_credentials.h"
#include "src/proto/grpc/testing/xds/v3/router.grpc.pb.h"

@ -234,6 +234,7 @@ grpc_cc_binary(
":benchmark_config",
":driver_impl",
"//:grpc++",
"//test/core/util:grpc_test_util",
"//test/cpp/util:test_config",
"//test/cpp/util:test_util",
],

@ -18,10 +18,10 @@
#include "src/core/lib/debug/stats.h"
#include "src/core/lib/debug/stats_data.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/slice/slice_internal.h"
#include "src/proto/grpc/testing/control.pb.h"
#include "test/core/util/test_config.h"
#include "test/core/util/tls_utils.h"
#include "test/cpp/qps/benchmark_config.h"
#include "test/cpp/qps/driver.h"
#include "test/cpp/qps/parse_json.h"
@ -36,11 +36,8 @@ namespace grpc {
namespace testing {
static void RunScenario() {
grpc_slice buffer;
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(absl::GetFlag(FLAGS_loadtest_config).c_str(),
0, &buffer)));
std::string json_str(grpc_core::StringViewFromSlice(buffer));
std::string json_str =
grpc_core::testing::GetFileContents(absl::GetFlag(FLAGS_loadtest_config));
Scenarios scenarios;
ParseJson(json_str, "grpc.testing.Scenarios", &scenarios);
gpr_log(GPR_INFO, "Running %s", scenarios.scenarios(0).name().c_str());

@ -145,6 +145,7 @@ grpc_cc_library(
":grpc++_proto_reflection_desc_db",
"//:grpc++",
"//src/proto/grpc/reflection/v1alpha:reflection_proto",
"//test/core/util:grpc_test_util",
],
)

@ -25,7 +25,7 @@
#include <grpcpp/support/slice.h>
#include "src/core/lib/gprpp/crash.h"
#include "src/core/lib/iomgr/load_file.h"
#include "test/core/util/tls_utils.h"
ABSL_RETIRED_FLAG(bool, enable_ssl, false,
"Replaced by --channel_creds_type=ssl.");
@ -94,24 +94,12 @@ CliCredentials::GetChannelCredentials() const {
grpc::SslCredentialsOptions ssl_creds_options;
// TODO(@Capstan): This won't affect Google Default Credentials using SSL.
if (!absl::GetFlag(FLAGS_ssl_client_cert).empty()) {
grpc_slice cert_slice = grpc_empty_slice();
GRPC_LOG_IF_ERROR(
"load_file",
grpc_load_file(absl::GetFlag(FLAGS_ssl_client_cert).c_str(), 1,
&cert_slice));
ssl_creds_options.pem_cert_chain =
grpc::StringFromCopiedSlice(cert_slice);
grpc_slice_unref(cert_slice);
ssl_creds_options.pem_cert_chain = grpc_core::testing::GetFileContents(
absl::GetFlag(FLAGS_ssl_client_cert));
}
if (!absl::GetFlag(FLAGS_ssl_client_key).empty()) {
grpc_slice key_slice = grpc_empty_slice();
GRPC_LOG_IF_ERROR(
"load_file",
grpc_load_file(absl::GetFlag(FLAGS_ssl_client_key).c_str(), 1,
&key_slice));
ssl_creds_options.pem_private_key =
grpc::StringFromCopiedSlice(key_slice);
grpc_slice_unref(key_slice);
ssl_creds_options.pem_private_key = grpc_core::testing::GetFileContents(
absl::GetFlag(FLAGS_ssl_client_key));
}
return grpc::SslCredentials(ssl_creds_options);
} else if (absl::GetFlag(FLAGS_channel_creds_type) == "gdc") {

@ -39,11 +39,11 @@
#include <grpcpp/server_context.h>
#include "src/core/lib/gprpp/env.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/proto/grpc/testing/echo.grpc.pb.h"
#include "src/proto/grpc/testing/echo.pb.h"
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
#include "test/core/util/tls_utils.h"
#include "test/cpp/util/cli_credentials.h"
#include "test/cpp/util/string_ref_helper.h"
#include "test/cpp/util/test_config.h"
@ -148,16 +148,10 @@ class TestCliCredentials final : public grpc::testing::CliCredentials {
if (!secure_) {
return InsecureChannelCredentials();
}
grpc_slice ca_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
const char* test_root_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
std::string test_root_cert =
grpc_core::testing::GetFileContents(CA_CERT_PATH);
SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
std::shared_ptr<grpc::ChannelCredentials> credential_ptr =
grpc::SslCredentials(grpc::SslCredentialsOptions(ssl_opts));
grpc_slice_unref(ca_slice);
return credential_ptr;
return grpc::SslCredentials(grpc::SslCredentialsOptions(ssl_opts));
}
std::string GetCredentialUsage() const override { return ""; }
@ -300,15 +294,10 @@ class GrpcToolTest : public ::testing::Test {
// Setup server
ServerBuilder builder;
std::shared_ptr<grpc::ServerCredentials> creds;
grpc_slice cert_slice, key_slice;
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR(
"load_file", grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
const char* server_cert =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
const char* server_key =
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
std::string server_cert =
grpc_core::testing::GetFileContents(SERVER_CERT_PATH);
std::string server_key =
grpc_core::testing::GetFileContents(SERVER_KEY_PATH);
SslServerCredentialsOptions::PemKeyCertPair pkcp = {server_key,
server_cert};
if (secure) {
@ -322,8 +311,6 @@ class GrpcToolTest : public ::testing::Test {
builder.AddListeningPort(server_address.str(), creds);
builder.RegisterService(&service_);
server_ = builder.BuildAndStart();
grpc_slice_unref(cert_slice);
grpc_slice_unref(key_slice);
return server_address.str();
}

@ -2485,8 +2485,6 @@ src/core/lib/iomgr/iomgr_internal.h \
src/core/lib/iomgr/iomgr_posix.cc \
src/core/lib/iomgr/iomgr_posix_cfstream.cc \
src/core/lib/iomgr/iomgr_windows.cc \
src/core/lib/iomgr/load_file.cc \
src/core/lib/iomgr/load_file.h \
src/core/lib/iomgr/lockfree_event.cc \
src/core/lib/iomgr/lockfree_event.h \
src/core/lib/iomgr/nameser.h \

@ -2260,8 +2260,6 @@ src/core/lib/iomgr/iomgr_internal.h \
src/core/lib/iomgr/iomgr_posix.cc \
src/core/lib/iomgr/iomgr_posix_cfstream.cc \
src/core/lib/iomgr/iomgr_windows.cc \
src/core/lib/iomgr/load_file.cc \
src/core/lib/iomgr/load_file.h \
src/core/lib/iomgr/lockfree_event.cc \
src/core/lib/iomgr/lockfree_event.h \
src/core/lib/iomgr/nameser.h \

@ -5553,6 +5553,30 @@
],
"uses_polling": false
},
{
"args": [],
"benchmark": false,
"ci_platforms": [
"linux",
"mac",
"posix",
"windows"
],
"cpu_cost": 1.0,
"exclude_configs": [],
"exclude_iomgrs": [],
"flaky": false,
"gtest": true,
"language": "c++",
"name": "load_file_test",
"platforms": [
"linux",
"mac",
"posix",
"windows"
],
"uses_polling": false
},
{
"args": [],
"benchmark": true,
@ -10363,30 +10387,6 @@
],
"uses_polling": false
},
{
"args": [],
"benchmark": false,
"ci_platforms": [
"linux",
"mac",
"posix",
"windows"
],
"cpu_cost": 1.0,
"exclude_configs": [],
"exclude_iomgrs": [],
"flaky": false,
"gtest": true,
"language": "c++",
"name": "test_core_gprpp_load_file_test",
"platforms": [
"linux",
"mac",
"posix",
"windows"
],
"uses_polling": false
},
{
"args": [],
"benchmark": false,
@ -10411,30 +10411,6 @@
],
"uses_polling": false
},
{
"args": [],
"benchmark": false,
"ci_platforms": [
"linux",
"mac",
"posix",
"windows"
],
"cpu_cost": 1.0,
"exclude_configs": [],
"exclude_iomgrs": [],
"flaky": false,
"gtest": true,
"language": "c++",
"name": "test_core_iomgr_load_file_test",
"platforms": [
"linux",
"mac",
"posix",
"windows"
],
"uses_polling": false
},
{
"args": [],
"benchmark": false,

Loading…
Cancel
Save