diff --git a/BUILD b/BUILD index 145f9803428..89b3ecd2aac 100644 --- a/BUILD +++ b/BUILD @@ -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", diff --git a/CMakeLists.txt b/CMakeLists.txt index 797ee03bb3a..1c9e7f7b2af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/Makefile b/Makefile index 5ad65ba66e1..4dc9e0ab66d 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/Package.swift b/Package.swift index 6cf43da1fb0..e485d6afd5d 100644 --- a/Package.swift +++ b/Package.swift @@ -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", diff --git a/build_autogenerated.yaml b/build_autogenerated.yaml index 04c2cd81a07..6bad73a8219 100644 --- a/build_autogenerated.yaml +++ b/build_autogenerated.yaml @@ -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 diff --git a/config.m4 b/config.m4 index 7d05f7b8f43..38e010c507a 100644 --- a/config.m4 +++ b/config.m4 @@ -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 \ diff --git a/config.w32 b/config.w32 index 4a6bd77a184..d9fbfda7e5d 100644 --- a/config.w32 +++ b/config.w32 @@ -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 " + diff --git a/gRPC-C++.podspec b/gRPC-C++.podspec index fd75e613cd3..5500b91b4ca 100644 --- a/gRPC-C++.podspec +++ b/gRPC-C++.podspec @@ -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', diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec index 6c9edbce1d7..532c5c99723 100644 --- a/gRPC-Core.podspec +++ b/gRPC-Core.podspec @@ -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', diff --git a/grpc.gemspec b/grpc.gemspec index 234cd253852..c506aa01110 100644 --- a/grpc.gemspec +++ b/grpc.gemspec @@ -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 ) diff --git a/grpc.gyp b/grpc.gyp index e7245887711..c0feaf43829 100644 --- a/grpc.gyp +++ b/grpc.gyp @@ -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', diff --git a/package.xml b/package.xml index 4b276061aac..317b648e59a 100644 --- a/package.xml +++ b/package.xml @@ -1468,8 +1468,6 @@ - - diff --git a/src/core/BUILD b/src/core/BUILD index b4779505fcb..ddf373fc979 100644 --- a/src/core/BUILD +++ b/src/core/BUILD @@ -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", diff --git a/src/core/ext/xds/xds_client_grpc.cc b/src/core/ext/xds/xds_client_grpc.cc index 5aa9b06d4c3..720ea4208fb 100644 --- a/src/core/ext/xds/xds_client_grpc.cc +++ b/src/core/ext/xds/xds_client_grpc.cc @@ -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 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"); diff --git a/src/core/lib/iomgr/load_file.cc b/src/core/lib/iomgr/load_file.cc deleted file mode 100644 index 8a6ac72a818..00000000000 --- a/src/core/lib/iomgr/load_file.cc +++ /dev/null @@ -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 - -#include "src/core/lib/iomgr/load_file.h" - -#include -#include - -#include -#include -#include - -#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(ftell(file)); - fseek(file, 0, SEEK_SET); - contents = static_cast( - 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; -} diff --git a/src/core/lib/iomgr/load_file.h b/src/core/lib/iomgr/load_file.h deleted file mode 100644 index b50724751f6..00000000000 --- a/src/core/lib/iomgr/load_file.h +++ /dev/null @@ -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 - -#include - -#include - -#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 diff --git a/src/core/lib/security/authorization/grpc_authorization_policy_provider.cc b/src/core/lib/security/authorization/grpc_authorization_policy_provider.cc index 4133f436a13..712adeee2d0 100644 --- a/src/core/lib/security/authorization/grpc_authorization_policy_provider.cc +++ b/src/core/lib/security/authorization/grpc_authorization_policy_provider.cc @@ -30,9 +30,9 @@ #include #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 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) { diff --git a/src/core/lib/security/credentials/external/file_external_account_credentials.cc b/src/core/lib/security/credentials/external/file_external_account_credentials.cc index 0fb3ce5091d..eb0110387ba 100644 --- a/src/core/lib/security/credentials/external/file_external_account_credentials.cc +++ b/src/core/lib/security/credentials/external/file_external_account_credentials.cc @@ -27,7 +27,7 @@ #include #include -#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 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) { diff --git a/src/core/lib/security/credentials/google_default/google_default_credentials.cc b/src/core/lib/security/credentials/google_default/google_default_credentials.cc index 517e8a85f64..361af9a41f9 100644 --- a/src/core/lib/security/credentials/google_default/google_default_credentials.cc +++ b/src/core/lib/security/credentials/google_default/google_default_credentials.cc @@ -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 result; - grpc_slice creds_data = grpc_empty_slice(); + absl::StatusOr 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; } diff --git a/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc b/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc index e0533faaf34..9f2ed31aeb6 100644 --- a/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +++ b/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc @@ -45,12 +45,12 @@ #include #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 diff --git a/src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.cc b/src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.cc index 9e0d797c3fc..bbd0fb20f9c 100644 --- a/src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.cc +++ b/src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.cc @@ -32,11 +32,11 @@ #include #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 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 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. diff --git a/src/core/lib/security/security_connector/load_system_roots_supported.cc b/src/core/lib/security/security_connector/load_system_roots_supported.cc index 49e7b2495a4..a0a64392812 100644 --- a/src/core/lib/security/security_connector/load_system_roots_supported.cc +++ b/src/core/lib/security/security_connector/load_system_roots_supported.cc @@ -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(); } diff --git a/src/core/lib/security/security_connector/ssl_utils.cc b/src/core/lib/security/security_connector/ssl_utils.cc index b0b907ac503..24c57bc1df7 100644 --- a/src/core/lib/security/security_connector/ssl_utils.cc +++ b/src/core/lib/security/security_connector/ssl_utils.cc @@ -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() { diff --git a/src/cpp/client/secure_credentials.cc b/src/cpp/client/secure_credentials.cc index dadebcbfd67..00b3ee266ab 100644 --- a/src/cpp/client/secure_credentials.cc +++ b/src/cpp/client/secure_credentials.cc @@ -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(GRPC_SLICE_START_PTR(json_string)), - options); - return cleanup(); + return StsCredentialsOptionsFromJson(json_slice->as_string_view().data(), + options); } // C++ to Core STS Credentials options. diff --git a/src/cpp/ext/csm/BUILD b/src/cpp/ext/csm/BUILD index 25aa1bf21dc..06e1c266168 100644 --- a/src/cpp/ext/csm/BUILD +++ b/src/cpp/ext/csm/BUILD @@ -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", diff --git a/src/cpp/ext/csm/metadata_exchange.cc b/src/cpp/ext/csm/metadata_exchange.cc index b5dd64a17f8..662fd419a4f 100644 --- a/src/cpp/ext/csm/metadata_exchange.cc +++ b/src/cpp/ext/csm/metadata_exchange.cc @@ -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"); diff --git a/src/cpp/ext/gcp/BUILD b/src/cpp/ext/gcp/BUILD index 5e21f1eb2d6..b7e8c08295b 100644 --- a/src/cpp/ext/gcp/BUILD +++ b/src/cpp/ext/gcp/BUILD @@ -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", diff --git a/src/cpp/ext/gcp/observability_config.cc b/src/cpp/ext/gcp/observability_config.cc index bf65f04246c..ccbeb9657e2 100644 --- a/src/cpp/ext/gcp/observability_config.cc +++ b/src/cpp/ext/gcp/observability_config.cc @@ -34,10 +34,10 @@ #include #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 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"); diff --git a/src/python/grpcio/grpc_core_dependencies.py b/src/python/grpcio/grpc_core_dependencies.py index 2551487924b..a99f08f7b57 100644 --- a/src/python/grpcio/grpc_core_dependencies.py +++ b/src/python/grpcio/grpc_core_dependencies.py @@ -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', diff --git a/test/core/bad_ssl/servers/alpn.cc b/test/core/bad_ssl/servers/alpn.cc index 527bf4a9004..14693494cee 100644 --- a/test/core/bad_ssl/servers/alpn.cc +++ b/test/core/bad_ssl/servers/alpn.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 GRPC_SLICE_START_PTR(cert_slice); - const char* server_key = - reinterpret_cast 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; diff --git a/test/core/bad_ssl/servers/cert.cc b/test/core/bad_ssl/servers/cert.cc index 01e6f693fc1..954c28fca7f 100644 --- a/test/core/bad_ssl/servers/cert.cc +++ b/test/core/bad_ssl/servers/cert.cc @@ -22,8 +22,8 @@ #include #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 GRPC_SLICE_START_PTR(key_slice); - pem_key_cert_pair.cert_chain = - reinterpret_cast 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(); diff --git a/test/core/end2end/end2end_test_suites.cc b/test/core/end2end/end2end_test_suites.cc index a4ddc9eb286..246e60f26e6 100644 --- a/test/core/end2end/end2end_test_suites.cc +++ b/test/core/end2end/end2end_test_suites.cc @@ -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 @@ -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 GRPC_SLICE_START_PTR(cert_slice); - const char* server_key = - reinterpret_cast 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& 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 GRPC_SLICE_START_PTR(cert_slice); - const char* server_key = - reinterpret_cast 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}; diff --git a/test/core/end2end/fixtures/h2_oauth2_common.h b/test/core/end2end/fixtures/h2_oauth2_common.h index 76e246b0821..57ae5a7d60d 100644 --- a/test/core/end2end/fixtures/h2_oauth2_common.h +++ b/test/core/end2end/fixtures/h2_oauth2_common.h @@ -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 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 GRPC_SLICE_START_PTR(cert_slice); - const char* server_key = - reinterpret_cast 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; } diff --git a/test/core/end2end/fixtures/h2_ssl_cred_reload_fixture.h b/test/core/end2end/fixtures/h2_ssl_cred_reload_fixture.h index 1e52f14b02f..fe6bc5d43dd 100644 --- a/test/core/end2end/fixtures/h2_ssl_cred_reload_fixture.h +++ b/test/core/end2end/fixtures/h2_ssl_cred_reload_fixture.h @@ -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 GRPC_SLICE_START_PTR(ca_slice); - const char* server_cert = - reinterpret_cast GRPC_SLICE_START_PTR(cert_slice); - const char* server_key = - reinterpret_cast 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 { diff --git a/test/core/end2end/fixtures/h2_ssl_tls_common.h b/test/core/end2end/fixtures/h2_ssl_tls_common.h index 2337dd89ac6..1a2d8da331d 100644 --- a/test/core/end2end/fixtures/h2_ssl_tls_common.h +++ b/test/core/end2end/fixtures/h2_ssl_tls_common.h @@ -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 GRPC_SLICE_START_PTR(cert_slice); - const char* server_key = - reinterpret_cast 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}; diff --git a/test/core/end2end/fixtures/h2_tls_common.h b/test/core/end2end/fixtures/h2_tls_common.h index c9fe5b4925f..5fd2972fd0f 100644 --- a/test/core/end2end/fixtures/h2_tls_common.h +++ b/test/core/end2end/fixtures/h2_tls_common.h @@ -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: { diff --git a/test/core/end2end/h2_ssl_session_reuse_test.cc b/test/core/end2end/h2_ssl_session_reuse_test.cc index 30c1640c08b..5ff49b9b5eb 100644 --- a/test/core/end2end/h2_ssl_session_reuse_test.cc +++ b/test/core/end2end/h2_ssl_session_reuse_test.cc @@ -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 GRPC_SLICE_START_PTR(ca_slice); - const char* server_cert = - reinterpret_cast GRPC_SLICE_START_PTR(cert_slice); - const char* server_key = - reinterpret_cast 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 GRPC_SLICE_START_PTR(ca_slice); - const char* client_cert = - reinterpret_cast GRPC_SLICE_START_PTR(cert_slice); - const char* client_key = - reinterpret_cast 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; } diff --git a/test/core/end2end/h2_tls_peer_property_external_verifier_test.cc b/test/core/end2end/h2_tls_peer_property_external_verifier_test.cc index 9093b8d979c..52ea6adfd25 100644 --- a/test/core/end2end/h2_tls_peer_property_external_verifier_test.cc +++ b/test/core/end2end/h2_tls_peer_property_external_verifier_test.cc @@ -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 GRPC_SLICE_START_PTR(ca_slice); - const char* server_cert = - reinterpret_cast GRPC_SLICE_START_PTR(cert_slice); - const char* server_key = - reinterpret_cast 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 GRPC_SLICE_START_PTR(ca_slice); - const char* client_cert = - reinterpret_cast GRPC_SLICE_START_PTR(cert_slice); - const char* client_key = - reinterpret_cast 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; } diff --git a/test/core/handshake/client_ssl.cc b/test/core/handshake/client_ssl.cc index d26b6a5fca9..5540d3084e1 100644 --- a/test/core/handshake/client_ssl.cc +++ b/test/core/handshake/client_ssl.cc @@ -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 GRPC_SLICE_START_PTR(ca_slice); - pem_key_cert_pair.private_key = - reinterpret_cast GRPC_SLICE_START_PTR(key_slice); - pem_key_cert_pair.cert_chain = - reinterpret_cast 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(); diff --git a/test/core/handshake/server_ssl_common.cc b/test/core/handshake/server_ssl_common.cc index e29bf9f8289..0f0c4b195b1 100644 --- a/test/core/handshake/server_ssl_common.cc +++ b/test/core/handshake/server_ssl_common.cc @@ -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 @@ -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 GRPC_SLICE_START_PTR(ca_slice); - pem_key_cert_pair.private_key = - reinterpret_cast GRPC_SLICE_START_PTR(key_slice); - pem_key_cert_pair.cert_chain = - reinterpret_cast 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 diff --git a/test/core/handshake/verify_peer_options.cc b/test/core/handshake/verify_peer_options.cc index aa9c0791b35..c9f47d39de6 100644 --- a/test/core/handshake/verify_peer_options.cc +++ b/test/core/handshake/verify_peer_options.cc @@ -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(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 GRPC_SLICE_START_PTR(ca_slice); - pem_key_cert_pair.private_key = - reinterpret_cast GRPC_SLICE_START_PTR(key_slice); - pem_key_cert_pair.cert_chain = - reinterpret_cast 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 GRPC_SLICE_START_PTR(ca_slice); - pem_key_cert_pair.private_key = - reinterpret_cast GRPC_SLICE_START_PTR(key_slice); - pem_key_cert_pair.cert_chain = - reinterpret_cast 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 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(&userdata)); GPR_ASSERT(destruct_userdata == static_cast(&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; } diff --git a/test/core/iomgr/BUILD b/test/core/iomgr/BUILD index eaeb880d472..44758b1a540 100644 --- a/test/core/iomgr/BUILD +++ b/test/core/iomgr/BUILD @@ -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"], diff --git a/test/core/iomgr/load_file_test.cc b/test/core/iomgr/load_file_test.cc deleted file mode 100644 index c72ac3862ba..00000000000 --- a/test/core/iomgr/load_file_test.cc +++ /dev/null @@ -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 -#include - -#include - -#include -#include -#include -#include - -#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(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(); -} diff --git a/test/core/security/create_jwt.cc b/test/core/security/create_jwt.cc index 4fd4c915672..c645a30c792 100644 --- a/test/core/security/create_jwt.cc +++ b/test/core/security/create_jwt.cc @@ -24,20 +24,17 @@ #include #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 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); diff --git a/test/core/security/fetch_oauth2.cc b/test/core/security/fetch_oauth2.cc index 29400d3cf9d..49cee158a09 100644 --- a/test/core/security/fetch_oauth2.cc +++ b/test/core/security/fetch_oauth2.cc @@ -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(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 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) { diff --git a/test/core/security/ssl_server_fuzzer.cc b/test/core/security/ssl_server_fuzzer.cc index e39fbb53f4f..e5bee8d81a1 100644 --- a/test/core/security/ssl_server_fuzzer.cc +++ b/test/core/security/ssl_server_fuzzer.cc @@ -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 GRPC_SLICE_START_PTR(ca_slice); - const char* server_cert = - reinterpret_cast GRPC_SLICE_START_PTR(cert_slice); - const char* server_key = - reinterpret_cast 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 sc = diff --git a/test/core/security/system_roots_test.cc b/test/core/security/system_roots_test.cc index d807333600d..2b6ec0f5ab2 100644 --- a/test/core/security/system_roots_test.cc +++ b/test/core/security/system_roots_test.cc @@ -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 diff --git a/test/core/security/tls_security_connector_test.cc b/test/core/security/tls_security_connector_test.cc index 837b7077409..ea58663f8e1 100644 --- a/test/core/security/tls_security_connector_test.cc +++ b/test/core/security/tls_security_connector_test.cc @@ -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) { diff --git a/test/core/surface/num_external_connectivity_watchers_test.cc b/test/core/surface/num_external_connectivity_watchers_test.cc index 5917330aaa0..bf0178b6ffc 100644 --- a/test/core/surface/num_external_connectivity_watchers_test.cc +++ b/test/core/surface/num_external_connectivity_watchers_test.cc @@ -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 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(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG), diff --git a/test/core/surface/sequential_connectivity_test.cc b/test/core/surface/sequential_connectivity_test.cc index d9623f22cee..6efaa787e8d 100644 --- a/test/core/surface/sequential_connectivity_test.cc +++ b/test/core/surface/sequential_connectivity_test.cc @@ -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 GRPC_SLICE_START_PTR(cert_slice); - const char* server_key = - reinterpret_cast 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 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, diff --git a/test/core/tsi/ssl_transport_security_test.cc b/test/core/tsi/ssl_transport_security_test.cc index 98b1d739d64..fe35649fee9 100644 --- a/test/core/tsi/ssl_transport_security_test.cc +++ b/test/core/tsi/ssl_transport_security_test.cc @@ -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( - 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( 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( gpr_zalloc(sizeof(char) * (strlen(cert) + strlen(ca) + 1))); memcpy(chain, cert, strlen(cert)); diff --git a/test/core/util/BUILD b/test/core/util/BUILD index b519c306ca0..e902bd2def0 100644 --- a/test/core/util/BUILD +++ b/test/core/util/BUILD @@ -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", diff --git a/test/core/util/fuzzer_corpus_test.cc b/test/core/util/fuzzer_corpus_test.cc index b79b33e362e..9d4bb8aa3db 100644 --- a/test/core/util/fuzzer_corpus_test.cc +++ b/test/core/util/fuzzer_corpus_test.cc @@ -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(data), length); gpr_free(data); } diff --git a/test/core/util/one_corpus_entry_fuzzer.cc b/test/core/util/one_corpus_entry_fuzzer.cc index 6f98bc72971..82452a648d1 100644 --- a/test/core/util/one_corpus_entry_fuzzer.cc +++ b/test/core/util/one_corpus_entry_fuzzer.cc @@ -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; } diff --git a/test/core/util/tls_utils.cc b/test/core/util/tls_utils.cc index 5ab70d64d5d..93e6b3635ff 100644 --- a/test/core/util/tls_utils.cc +++ b/test/core/util/tls_utils.cc @@ -17,6 +17,8 @@ #include +#include "absl/strings/str_cat.h" + #include #include #include @@ -24,8 +26,8 @@ #include #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, diff --git a/test/cpp/end2end/channelz_service_test.cc b/test/cpp/end2end/channelz_service_test.cc index 333d2804d9b..b5c29d34743 100644 --- a/test/cpp/end2end/channelz_service_test.cc +++ b/test/cpp/end2end/channelz_service_test.cc @@ -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 GetChannelCredentials( CredentialsType type, ChannelArguments* args) { if (type == CredentialsType::kInsecure) { @@ -150,11 +141,11 @@ std::shared_ptr GetChannelCredentials( } args->SetSslTargetNameOverride("foo.test.google.fr"); std::vector identity_key_cert_pairs = { - {ReadFile(kClientKeyPath), ReadFile(kClientCertPath)}}; + GetFileContents(kClientKeyPath), GetFileContents(kClientCertPath)}; grpc::experimental::TlsChannelCredentialsOptions options; options.set_certificate_provider( std::make_shared( - 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 GetServerCredentials( return InsecureServerCredentials(); } std::vector identity_key_cert_pairs = { - {ReadFile(kServerKeyPath), ReadFile(kServerCertPath)}}; + {GetFileContents(kServerKeyPath), GetFileContents(kServerCertPath)}}; auto certificate_provider = std::make_shared( - 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() diff --git a/test/cpp/end2end/grpc_authz_end2end_test.cc b/test/cpp/end2end/grpc_authz_end2end_test.cc index 8a4d90f0728..0d7dd359423 100644 --- a/test/cpp/end2end/grpc_authz_end2end_test.cc +++ b/test/cpp/end2end/grpc_authz_end2end_test.cc @@ -25,7 +25,6 @@ #include #include -#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 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 - 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( - 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); diff --git a/test/cpp/end2end/ssl_credentials_test.cc b/test/cpp/end2end/ssl_credentials_test.cc index b1a51dafccd..44ccdd4d329 100644 --- a/test/cpp/end2end/ssl_credentials_test.cc +++ b/test/cpp/end2end/ssl_credentials_test.cc @@ -29,9 +29,9 @@ #include #include -#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(¬ification); }); 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(¬ification); }); 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(¬ification); }); 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; diff --git a/test/cpp/end2end/tls_credentials_test.cc b/test/cpp/end2end/tls_credentials_test.cc index 1c3fd837082..2949aa0794b 100644 --- a/test/cpp/end2end/tls_credentials_test.cc +++ b/test/cpp/end2end/tls_credentials_test.cc @@ -31,9 +31,9 @@ #include #include -#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; diff --git a/test/cpp/end2end/xds/xds_end2end_test.cc b/test/cpp/end2end/xds/xds_end2end_test.cc index 78907cbb1c9..f48de70c7d1 100644 --- a/test/cpp/end2end/xds/xds_end2end_test.cc +++ b/test/cpp/end2end/xds/xds_end2end_test.cc @@ -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 identity_key_cert_pairs; identity_key_cert_pairs.emplace_back(key_cert_pair); auto certificate_provider = std::make_shared( - 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(ReadFile(kCaCertPath)); + auto certificate_provider = std::make_shared( + grpc_core::testing::GetFileContents(kCaCertPath)); grpc::experimental::TlsChannelCredentialsOptions options; options.set_certificate_provider(std::move(certificate_provider)); options.watch_root_certs(); diff --git a/test/cpp/end2end/xds/xds_end2end_test_lib.cc b/test/cpp/end2end/xds/xds_end2end_test_lib.cc index ca04697130d..e91e4af4511 100644 --- a/test/cpp/end2end/xds/xds_end2end_test_lib.cc +++ b/test/cpp/end2end/xds/xds_end2end_test_lib.cc @@ -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 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 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 identity_key_cert_pairs; identity_key_cert_pairs.emplace_back(key_cert_pair); auto certificate_provider = std::make_shared( - 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(); diff --git a/test/cpp/end2end/xds/xds_end2end_test_lib.h b/test/cpp/end2end/xds/xds_end2end_test_lib.h index fb83b901803..cdb98d90591 100644 --- a/test/cpp/end2end/xds/xds_end2end_test_lib.h +++ b/test/cpp/end2end/xds/xds_end2end_test_lib.h @@ -933,9 +933,6 @@ class XdsEnd2endTest : public ::testing::TestWithParam, // 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); diff --git a/test/cpp/end2end/xds/xds_utils.cc b/test/cpp/end2end/xds/xds_utils.cc index 07f37ee770e..28f58988169 100644 --- a/test/cpp/end2end/xds/xds_utils.cc +++ b/test/cpp/end2end/xds/xds_utils.cc @@ -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" diff --git a/test/cpp/qps/BUILD b/test/cpp/qps/BUILD index 85ae36dcf97..3d0dbe6824a 100644 --- a/test/cpp/qps/BUILD +++ b/test/cpp/qps/BUILD @@ -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", ], diff --git a/test/cpp/qps/scenario_runner.cc b/test/cpp/qps/scenario_runner.cc index b3716f201e5..1c1988642af 100644 --- a/test/cpp/qps/scenario_runner.cc +++ b/test/cpp/qps/scenario_runner.cc @@ -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()); diff --git a/test/cpp/util/BUILD b/test/cpp/util/BUILD index e4b98dc1ec2..da0a2117153 100644 --- a/test/cpp/util/BUILD +++ b/test/cpp/util/BUILD @@ -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", ], ) diff --git a/test/cpp/util/cli_credentials.cc b/test/cpp/util/cli_credentials.cc index 4e6427a3154..1a2d355438f 100644 --- a/test/cpp/util/cli_credentials.cc +++ b/test/cpp/util/cli_credentials.cc @@ -25,7 +25,7 @@ #include #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") { diff --git a/test/cpp/util/grpc_tool_test.cc b/test/cpp/util/grpc_tool_test.cc index 890c5546658..1148c182666 100644 --- a/test/cpp/util/grpc_tool_test.cc +++ b/test/cpp/util/grpc_tool_test.cc @@ -39,11 +39,11 @@ #include #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 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 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 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 GRPC_SLICE_START_PTR(cert_slice); - const char* server_key = - reinterpret_cast 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(); } diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index 83b2932d558..02ea5b7d7b1 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -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 \ diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal index a0e3208e899..874ccec7d34 100644 --- a/tools/doxygen/Doxyfile.core.internal +++ b/tools/doxygen/Doxyfile.core.internal @@ -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 \ diff --git a/tools/run_tests/generated/tests.json b/tools/run_tests/generated/tests.json index 6d3fc272028..2d6b64409a1 100644 --- a/tools/run_tests/generated/tests.json +++ b/tools/run_tests/generated/tests.json @@ -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,