2025-10-02 22:18:10 +02:00
|
|
|
// 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;
|
2025-10-02 22:18:10 +02:00
|
|
|
|
|
|
|
|
static int libFunctionDetourCounter = 0;
|
2025-10-02 22:31:45 +02:00
|
|
|
static int libFunctionDetour()
|
2025-10-02 22:18:10 +02:00
|
|
|
{
|
|
|
|
|
libFunctionDetourCounter++;
|
|
|
|
|
return 94;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-02 22:44:29 +02:00
|
|
|
TEST_CASE( "Attaching custom function in dylib installs detour", "[attach][dylib]" )
|
2025-10-02 22:18:10 +02:00
|
|
|
{
|
|
|
|
|
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:18:10 +02:00
|
|
|
|
2025-10-02 22:44:29 +02:00
|
|
|
REQUIRE( detour_transaction_begin() == err_none );
|
2025-10-02 22:18:10 +02:00
|
|
|
|
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:18:10 +02:00
|
|
|
|
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:18:10 +02:00
|
|
|
|
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:18:10 +02:00
|
|
|
|
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:18:10 +02:00
|
|
|
|
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:18:10 +02:00
|
|
|
|
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:18:10 +02:00
|
|
|
|
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:18:10 +02:00
|
|
|
}
|
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 );
|
2025-10-02 22:18:10 +02:00
|
|
|
}
|