This commit is contained in:
Lysann Tranvouez 2025-10-02 20:51:25 +02:00
parent 5e76138b53
commit 327dfb0cb7
5 changed files with 34 additions and 34 deletions

View file

@ -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. # 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 # Between attach and commit the target function's code page is not executable, which can mean our test driver code would not
# be executable. # be executable.
add_library(mach_detours_test_func SHARED add_library(mach_detours_tests_lib SHARED
test_func.c lib_function.c
test_func.h lib_function.h
) )
target_link_libraries(mach_detours_tests target_link_libraries(mach_detours_tests
PRIVATE PRIVATE
mach_detours mach_detours
Catch2::Catch2WithMain Catch2::Catch2WithMain
mach_detours_test_func mach_detours_tests_lib
) )

11
tests/lib_function.c Normal file
View file

@ -0,0 +1,11 @@
// Copyright (c) Lysann Tranvouez. All rights reserved.
#include "lib_function.h"
int libFunctionCounter = 0;
int libFunction()
{
libFunctionCounter++;
return 42;
}

View file

@ -6,9 +6,9 @@
extern "C" { extern "C" {
#endif #endif
extern int testFunctionCounter; extern int libFunctionCounter;
int testFunction(); int libFunction();
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -6,36 +6,36 @@
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "test_func.h" #include "lib_function.h"
int (*realLibFunction)() = libFunction;
static int testFunctionDetourCounter = 0; static int libFunctionDetourCounter = 0;
int testFunctionDetour() int libFunctionDetour()
{ {
testFunctionDetourCounter++; libFunctionDetourCounter++;
return 94; return 94;
} }
TEST_CASE( "Overriding custom function in dylib" ) TEST_CASE( "Overriding custom function in dylib" )
{ {
int (*realTestFunction)() = testFunction; libFunctionCounter = 0;
testFunctionCounter = 0; libFunctionDetourCounter = 0;
testFunctionDetourCounter = 0;
REQUIRE( testFunction() == 42 ); REQUIRE( libFunction() == 42 );
REQUIRE( testFunctionCounter == 1 ); REQUIRE( libFunctionCounter == 1 );
REQUIRE( testFunctionDetourCounter == 0 ); REQUIRE( libFunctionDetourCounter == 0 );
CHECK( detour_transaction_begin() == err_none ); CHECK( detour_transaction_begin() == err_none );
CHECK( detour_attach(reinterpret_cast<detour_func_t*>(&realTestFunction), reinterpret_cast<detour_func_t>(testFunctionDetour)) == err_none ); CHECK( detour_attach(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
CHECK( detour_transaction_commit() == err_none ); CHECK( detour_transaction_commit() == err_none );
REQUIRE( realTestFunction != testFunction ); REQUIRE( realLibFunction != libFunction );
REQUIRE( testFunctionCounter == 1 ); REQUIRE( libFunctionCounter == 1 );
REQUIRE( testFunctionDetourCounter == 0 ); REQUIRE( libFunctionDetourCounter == 0 );
REQUIRE( testFunction() == 94 ); REQUIRE( libFunction() == 94 );
REQUIRE( testFunctionCounter == 1 ); REQUIRE( libFunctionCounter == 1 );
REQUIRE( testFunctionDetourCounter == 1 ); REQUIRE( libFunctionDetourCounter == 1 );
} }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View file

@ -1,11 +0,0 @@
// Copyright (c) Lysann Tranvouez. All rights reserved.
#include "test_func.h"
int testFunctionCounter = 0;
int testFunction()
{
testFunctionCounter++;
return 42;
}