diff --git a/tests/test_dylib_function.cpp b/tests/test_dylib_function.cpp index 996ad9a..a044f57 100644 --- a/tests/test_dylib_function.cpp +++ b/tests/test_dylib_function.cpp @@ -13,6 +13,11 @@ static int libFunctionDetour() libFunctionDetourCounter++; return 94; } +static int libFunctionDetourCallingReal() +{ + libFunctionDetourCounter++; + return realLibFunction() + 94; +} TEST_CASE( "Attaching custom function in dylib installs detour", "[attach][dylib]" ) { @@ -48,6 +53,54 @@ TEST_CASE( "Attaching custom function in dylib installs detour", "[attach][dylib CHECK( detour_detach_and_commit(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetour)) == err_none ); } +TEST_CASE( "We can call real function using the reassigned trampoline pointer", "[attach][dylib]" ) +{ + libFunctionCounter = 0; + libFunctionDetourCounter = 0; + + REQUIRE( realLibFunction == libFunction ); + REQUIRE( libFunction() == 42 ); + REQUIRE( libFunctionCounter == 1 ); + REQUIRE( libFunctionDetourCounter == 0 ); + + REQUIRE( detour_transaction_begin() == err_none ); + CHECK( detour_attach_and_commit(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetour)) == err_none ); + + CHECK( realLibFunction != libFunction ); + CHECK( realLibFunction() == 42 ); + CHECK( libFunctionCounter == 2 ); + CHECK( libFunctionDetourCounter == 0 ); + + // clean up + REQUIRE( detour_transaction_begin() == err_none ); + CHECK( detour_detach_and_commit(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetour)) == err_none ); +} + +TEST_CASE( "Detour function can call real function", "[attach][dylib]" ) +{ + libFunctionCounter = 0; + libFunctionDetourCounter = 0; + + REQUIRE( realLibFunction == libFunction ); + REQUIRE( libFunction() == 42 ); + REQUIRE( libFunctionCounter == 1 ); + REQUIRE( libFunctionDetourCounter == 0 ); + + REQUIRE( detour_transaction_begin() == err_none ); + CHECK( detour_attach_and_commit(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetourCallingReal)) == err_none ); + + CHECK( realLibFunction != libFunction ); + CHECK( libFunctionCounter == 1 ); + CHECK( libFunctionDetourCounter == 0 ); + CHECK( libFunction() == 94 + 42 ); + CHECK( libFunctionCounter == 2 ); + CHECK( libFunctionDetourCounter == 1 ); + + // clean up + REQUIRE( detour_transaction_begin() == err_none ); + CHECK( detour_detach_and_commit(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetourCallingReal)) == err_none ); +} + TEST_CASE( "Detaching custom function in dylib removes detour", "[detach][dylib]" ) { libFunctionCounter = 0;