From 7257abce7132a3483351c318aa24ccfc20724dc7 Mon Sep 17 00:00:00 2001 From: Lysann Tranvouez Date: Thu, 2 Oct 2025 22:44:29 +0200 Subject: [PATCH] small test restructure again --- tests/test_dylib_function.cpp | 99 ++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/tests/test_dylib_function.cpp b/tests/test_dylib_function.cpp index 4ad66cb..996ad9a 100644 --- a/tests/test_dylib_function.cpp +++ b/tests/test_dylib_function.cpp @@ -14,65 +14,66 @@ static int libFunctionDetour() return 94; } -TEST_CASE( "Overriding custom function in dylib", "[attach][detach][dylib]" ) +TEST_CASE( "Attaching custom function in dylib installs detour", "[attach][dylib]" ) { libFunctionCounter = 0; libFunctionDetourCounter = 0; - SECTION( "attaching installs a detour" ) + REQUIRE( realLibFunction == libFunction ); + REQUIRE( libFunction() == 42 ); + REQUIRE( libFunctionCounter == 1 ); + REQUIRE( libFunctionDetourCounter == 0 ); + + REQUIRE( detour_transaction_begin() == err_none ); + + SECTION( "attach + transaction_commit" ) { - REQUIRE( realLibFunction == libFunction ); - REQUIRE( libFunction() == 42 ); - REQUIRE( libFunctionCounter == 1 ); - REQUIRE( libFunctionDetourCounter == 0 ); + REQUIRE( detour_attach(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetour)) == err_none ); + CHECK( detour_transaction_commit() == err_none ); + } + SECTION( "attach_and_commit" ) + { + CHECK( detour_attach_and_commit(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetour)) == err_none ); + } - REQUIRE( detour_transaction_begin() == err_none ); + CHECK( realLibFunction != libFunction ); + CHECK( libFunctionCounter == 1 ); + CHECK( libFunctionDetourCounter == 0 ); + CHECK( libFunction() == 94 ); + CHECK( libFunctionCounter == 1 ); + CHECK( libFunctionDetourCounter == 1 ); - SECTION( "attach + transaction_commit" ) - { - REQUIRE( detour_attach(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetour)) == err_none ); - CHECK( detour_transaction_commit() == err_none ); - } - SECTION( "attach_and_commit" ) - { - CHECK( detour_attach_and_commit(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetour)) == err_none ); - } + // clean up + REQUIRE( detour_transaction_begin() == err_none ); + CHECK( detour_detach_and_commit(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetour)) == err_none ); +} - CHECK( realLibFunction != libFunction ); - CHECK( libFunctionCounter == 1 ); - CHECK( libFunctionDetourCounter == 0 ); - CHECK( libFunction() == 94 ); - CHECK( libFunctionCounter == 1 ); - CHECK( libFunctionDetourCounter == 1 ); +TEST_CASE( "Detaching custom function in dylib removes detour", "[detach][dylib]" ) +{ + libFunctionCounter = 0; + libFunctionDetourCounter = 0; - // clean up - REQUIRE( detour_transaction_begin() == err_none ); + REQUIRE( detour_transaction_begin() == err_none ); + REQUIRE( detour_attach_and_commit(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetour)) == err_none ); + REQUIRE( realLibFunction != libFunction ); + + // this must be a separate transaction because detach works only if the trampoline is actually in place already (committed) + REQUIRE( detour_transaction_begin() == err_none ); + + SECTION( "detach + transaction_commit" ) + { + REQUIRE( detour_detach(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetour)) == err_none ); + CHECK( detour_transaction_commit() == err_none ); + } + SECTION( "detach_and_commit" ) + { CHECK( detour_detach_and_commit(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetour)) == err_none ); } - SECTION( "detaching in separate transaction removes detour" ) - { - REQUIRE( detour_transaction_begin() == err_none ); - REQUIRE( detour_attach_and_commit(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetour)) == err_none ); - REQUIRE( realLibFunction != libFunction ); - - REQUIRE( detour_transaction_begin() == err_none ); - - SECTION( "detach + transaction_commit" ) - { - REQUIRE( detour_detach(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetour)) == err_none ); - CHECK( detour_transaction_commit() == err_none ); - } - SECTION( "detach_and_commit" ) - { - CHECK( detour_detach_and_commit(reinterpret_cast(&realLibFunction), reinterpret_cast(libFunctionDetour)) == err_none ); - } - - CHECK( realLibFunction == libFunction ); - CHECK( libFunctionCounter == 0 ); - CHECK( libFunctionDetourCounter == 0 ); - CHECK( libFunction() == 42 ); - CHECK( libFunctionCounter == 1 ); - CHECK( libFunctionDetourCounter == 0 ); - } + CHECK( realLibFunction == libFunction ); + CHECK( libFunctionCounter == 0 ); + CHECK( libFunctionDetourCounter == 0 ); + CHECK( libFunction() == 42 ); + CHECK( libFunctionCounter == 1 ); + CHECK( libFunctionDetourCounter == 0 ); }