mach-detours/tests/test_dylib_function.cpp

133 lines
4.9 KiB
C++
Raw Normal View History

// Copyright (c) Lysann Tranvouez. All rights reserved.
#include <catch2/catch_test_macros.hpp>
#include <mach_detours.h>
#include "lib_function.h"
2025-10-02 22:31:45 +02:00
static int (*realLibFunction)() = libFunction;
static int libFunctionDetourCounter = 0;
2025-10-02 22:31:45 +02:00
static int libFunctionDetour()
{
libFunctionDetourCounter++;
return 94;
}
2025-10-02 22:51:15 +02:00
static int libFunctionDetourCallingReal()
{
libFunctionDetourCounter++;
return realLibFunction() + 94;
}
2025-10-02 22:44:29 +02:00
TEST_CASE( "Attaching custom function in dylib installs detour", "[attach][dylib]" )
{
libFunctionCounter = 0;
libFunctionDetourCounter = 0;
2025-10-02 22:44:29 +02:00
REQUIRE( realLibFunction == libFunction );
REQUIRE( libFunction() == 42 );
REQUIRE( libFunctionCounter == 1 );
REQUIRE( libFunctionDetourCounter == 0 );
2025-10-02 22:44:29 +02:00
REQUIRE( detour_transaction_begin() == err_none );
2025-10-02 22:44:29 +02:00
SECTION( "attach + transaction_commit" )
{
REQUIRE( detour_attach(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
CHECK( detour_transaction_commit() == err_none );
}
SECTION( "attach_and_commit" )
{
CHECK( detour_attach_and_commit(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
}
2025-10-02 22:44:29 +02:00
CHECK( realLibFunction != libFunction );
CHECK( libFunctionCounter == 1 );
CHECK( libFunctionDetourCounter == 0 );
CHECK( libFunction() == 94 );
CHECK( libFunctionCounter == 1 );
CHECK( libFunctionDetourCounter == 1 );
2025-10-02 22:44:29 +02:00
// clean up
REQUIRE( detour_transaction_begin() == err_none );
CHECK( detour_detach_and_commit(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
}
2025-10-02 22:51:15 +02:00
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<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(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<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(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<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(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<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetourCallingReal)) == err_none );
}
2025-10-02 22:44:29 +02:00
TEST_CASE( "Detaching custom function in dylib removes detour", "[detach][dylib]" )
{
libFunctionCounter = 0;
libFunctionDetourCounter = 0;
2025-10-02 22:44:29 +02:00
REQUIRE( detour_transaction_begin() == err_none );
REQUIRE( detour_attach_and_commit(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
REQUIRE( realLibFunction != libFunction );
2025-10-02 22:44:29 +02:00
// 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 );
2025-10-02 22:44:29 +02:00
SECTION( "detach + transaction_commit" )
{
REQUIRE( detour_detach(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
CHECK( detour_transaction_commit() == err_none );
}
2025-10-02 22:44:29 +02:00
SECTION( "detach_and_commit" )
{
CHECK( detour_detach_and_commit(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
}
CHECK( realLibFunction == libFunction );
CHECK( libFunctionCounter == 0 );
CHECK( libFunctionDetourCounter == 0 );
CHECK( libFunction() == 42 );
CHECK( libFunctionCounter == 1 );
CHECK( libFunctionDetourCounter == 0 );
}