diff --git a/tests/end_to_end/end_to_end.cpp b/tests/end_to_end/end_to_end.cpp index 442744717..fb141dc0e 100644 --- a/tests/end_to_end/end_to_end.cpp +++ b/tests/end_to_end/end_to_end.cpp @@ -2398,7 +2398,7 @@ TEST_CASE("load_native_program_negative4", "[end-to-end]") size_t count_of_maps = 0; size_t count_of_programs = 0; std::wstring file_path(L"droppacket_um.dll"); - ebpf_handle_t module_handle = ebpf_handle_invalid; + _test_handle_helper module_handle; ebpf_handle_t map_handles[INCORRECT_MAP_COUNT]; ebpf_handle_t program_handles[PROGRAM_COUNT]; @@ -2417,7 +2417,11 @@ TEST_CASE("load_native_program_negative4", "[end-to-end]") service_path = service_path + NATIVE_DRIVER_SERVICE_NAME; REQUIRE( test_ioctl_load_native_module( - service_path, &provider_module_id, &module_handle, &count_of_maps, &count_of_programs) == ERROR_SUCCESS); + service_path, + &provider_module_id, + module_handle.get_handle_pointer(), + &count_of_maps, + &count_of_programs) == ERROR_SUCCESS); // Try to load the programs by passing wrong map and program handles size. This should fail. REQUIRE( @@ -2427,9 +2431,6 @@ TEST_CASE("load_native_program_negative4", "[end-to-end]") // Delete the created service. Platform::_delete_service(service_handle); - - // Close the module handle. - Platform::CloseHandle(module_handle); } // Try to load a .sys in user mode. @@ -2461,8 +2462,8 @@ TEST_CASE("load_native_program_negative6", "[end-to-end]") SC_HANDLE service_handle2 = nullptr; std::wstring service_path(SERVICE_PATH_PREFIX); std::wstring service_path2(SERVICE_PATH_PREFIX); - ebpf_handle_t module_handle = ebpf_handle_invalid; - ebpf_handle_t module_handle2 = ebpf_handle_invalid; + _test_handle_helper module_handle; + _test_handle_helper module_handle2; size_t count_of_maps = 0; size_t count_of_programs = 0; set_native_module_failures(true); @@ -2476,7 +2477,11 @@ TEST_CASE("load_native_program_negative6", "[end-to-end]") service_path = service_path + NATIVE_DRIVER_SERVICE_NAME; REQUIRE( test_ioctl_load_native_module( - service_path, &provider_module_id, &module_handle, &count_of_maps, &count_of_programs) == ERROR_SUCCESS); + service_path, + &provider_module_id, + module_handle.get_handle_pointer(), + &count_of_maps, + &count_of_programs) == ERROR_SUCCESS); // Create a new service with same driver and same module id. _create_service_helper(L"droppacket_um.dll", NATIVE_DRIVER_SERVICE_NAME_2, &provider_module_id, &service_handle2); @@ -2487,10 +2492,11 @@ TEST_CASE("load_native_program_negative6", "[end-to-end]") service_path2 = service_path2 + NATIVE_DRIVER_SERVICE_NAME_2; REQUIRE( test_ioctl_load_native_module( - service_path2, &provider_module_id, &module_handle2, &count_of_maps, &count_of_programs) == - ERROR_OBJECT_ALREADY_EXISTS); - - Platform::CloseHandle(module_handle); + service_path2, + &provider_module_id, + module_handle2.get_handle_pointer(), + &count_of_maps, + &count_of_programs) == ERROR_OBJECT_ALREADY_EXISTS); } // The below tests try to load native drivers for invalid programs (that will fail verification). @@ -2509,9 +2515,9 @@ TEST_CASE("load_native_program_negative8", "[end-to-end]") size_t count_of_maps = 0; size_t count_of_programs = 0; std::wstring file_path(L"droppacket_um.dll"); - ebpf_handle_t module_handle = ebpf_handle_invalid; ebpf_handle_t map_handles; ebpf_handle_t program_handles; + _test_handle_helper module_handle; REQUIRE(UuidCreate(&provider_module_id) == RPC_S_OK); @@ -2522,15 +2528,17 @@ TEST_CASE("load_native_program_negative8", "[end-to-end]") service_path = service_path + NATIVE_DRIVER_SERVICE_NAME; REQUIRE( test_ioctl_load_native_module( - service_path, &provider_module_id, &module_handle, &count_of_maps, &count_of_programs) == ERROR_SUCCESS); + service_path, + &provider_module_id, + module_handle.get_handle_pointer(), + &count_of_maps, + &count_of_programs) == ERROR_SUCCESS); // Try to load the programs from the module with 0 programs. REQUIRE( test_ioctl_load_native_programs(&provider_module_id, nullptr, 1, &map_handles, 1, &program_handles) == ERROR_INVALID_PARAMETER); - Platform::CloseHandle(module_handle); - // Delete the created service. Platform::_delete_service(service_handle); } diff --git a/tests/end_to_end/test_helper.cpp b/tests/end_to_end/test_helper.cpp index d8b1b62f0..765339602 100644 --- a/tests/end_to_end/test_helper.cpp +++ b/tests/end_to_end/test_helper.cpp @@ -633,6 +633,13 @@ _test_helper_end_to_end::_test_helper_end_to_end() api_initialized = true; } +_test_handle_helper::~_test_handle_helper() +{ + if (handle != ebpf_handle_invalid) { + GlueCloseHandle((HANDLE)handle); + } +} + static void _rundown_osfhandles() { diff --git a/tests/end_to_end/test_helper.hpp b/tests/end_to_end/test_helper.hpp index 31c401fcb..4e833869d 100644 --- a/tests/end_to_end/test_helper.hpp +++ b/tests/end_to_end/test_helper.hpp @@ -32,6 +32,25 @@ class _test_helper_libbpf _single_instance_hook* cgroup_inet4_connect_hook; }; +class _test_handle_helper +{ + public: + _test_handle_helper() : handle(ebpf_handle_invalid){}; + _test_handle_helper(ebpf_handle_t handle) : handle(handle){}; + _test_handle_helper(const _test_handle_helper& object) = delete; + void + operator=(const _test_handle_helper& object) = delete; + ~_test_handle_helper(); + ebpf_handle_t* + get_handle_pointer() + { + return &handle; + }; + + private: + ebpf_handle_t handle = ebpf_handle_invalid; +}; + void set_native_module_failures(bool expected);