diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index be2c7b8..4063cfc 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,14 +6,14 @@ add_executable(mach_detours_tests # The target function must be in a shared library because otherwise it might be in the same code page as the test.cpp functions. # Between attach and commit the target function's code page is not executable, which can mean our test driver code would not # be executable. -add_library(mach_detours_test_func SHARED - test_func.c - test_func.h +add_library(mach_detours_tests_lib SHARED + lib_function.c + lib_function.h ) target_link_libraries(mach_detours_tests PRIVATE mach_detours Catch2::Catch2WithMain - mach_detours_test_func + mach_detours_tests_lib ) \ No newline at end of file diff --git a/tests/lib_function.c b/tests/lib_function.c new file mode 100644 index 0000000..c9dad8c --- /dev/null +++ b/tests/lib_function.c @@ -0,0 +1,11 @@ +// Copyright (c) Lysann Tranvouez. All rights reserved. + +#include "lib_function.h" + +int libFunctionCounter = 0; + +int libFunction() +{ + libFunctionCounter++; + return 42; +} diff --git a/tests/test_func.h b/tests/lib_function.h similarity index 73% rename from tests/test_func.h rename to tests/lib_function.h index 0b4cbfd..4d7b839 100644 --- a/tests/test_func.h +++ b/tests/lib_function.h @@ -6,9 +6,9 @@ extern "C" { #endif -extern int testFunctionCounter; +extern int libFunctionCounter; -int testFunction(); +int libFunction(); #ifdef __cplusplus } diff --git a/tests/test.cpp b/tests/test.cpp index 7bc0b28..cf8ec4a 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -6,36 +6,36 @@ //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -#include "test_func.h" +#include "lib_function.h" +int (*realLibFunction)() = libFunction; -static int testFunctionDetourCounter = 0; -int testFunctionDetour() +static int libFunctionDetourCounter = 0; +int libFunctionDetour() { - testFunctionDetourCounter++; + libFunctionDetourCounter++; return 94; } TEST_CASE( "Overriding custom function in dylib" ) { - int (*realTestFunction)() = testFunction; - testFunctionCounter = 0; - testFunctionDetourCounter = 0; + libFunctionCounter = 0; + libFunctionDetourCounter = 0; - REQUIRE( testFunction() == 42 ); - REQUIRE( testFunctionCounter == 1 ); - REQUIRE( testFunctionDetourCounter == 0 ); + REQUIRE( libFunction() == 42 ); + REQUIRE( libFunctionCounter == 1 ); + REQUIRE( libFunctionDetourCounter == 0 ); CHECK( detour_transaction_begin() == err_none ); - CHECK( detour_attach(reinterpret_cast(&realTestFunction), reinterpret_cast(testFunctionDetour)) == err_none ); + CHECK( detour_attach(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetour)) == err_none ); CHECK( detour_transaction_commit() == err_none ); - REQUIRE( realTestFunction != testFunction ); + REQUIRE( realLibFunction != libFunction ); - REQUIRE( testFunctionCounter == 1 ); - REQUIRE( testFunctionDetourCounter == 0 ); - REQUIRE( testFunction() == 94 ); - REQUIRE( testFunctionCounter == 1 ); - REQUIRE( testFunctionDetourCounter == 1 ); + REQUIRE( libFunctionCounter == 1 ); + REQUIRE( libFunctionDetourCounter == 0 ); + REQUIRE( libFunction() == 94 ); + REQUIRE( libFunctionCounter == 1 ); + REQUIRE( libFunctionDetourCounter == 1 ); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/tests/test_func.c b/tests/test_func.c deleted file mode 100644 index 59bc081..0000000 --- a/tests/test_func.c +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (c) Lysann Tranvouez. All rights reserved. - -#include "test_func.h" - -int testFunctionCounter = 0; - -int testFunction() -{ - testFunctionCounter++; - return 42; -}