84 lines
3.1 KiB
C++
84 lines
3.1 KiB
C++
// Copyright (c) Lysann Tranvouez. All rights reserved.
|
|
|
|
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include <mach_detours.h>
|
|
|
|
#include "lib_function.h"
|
|
static int (*realLibFunction)() = libFunction;
|
|
|
|
static int libFunctionDetourCounter = 0;
|
|
static int libFunctionDetour()
|
|
{
|
|
libFunctionDetourCounter++;
|
|
return 94;
|
|
}
|
|
|
|
TEST_CASE( "Must start a transaction to attach", "[transaction]" )
|
|
{
|
|
libFunctionCounter = 0;
|
|
libFunctionDetourCounter = 0;
|
|
|
|
CHECK( detour_attach(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == detour_err_wrong_thread );
|
|
CHECK( libFunction() == 42 );
|
|
CHECK( libFunctionCounter == 1 );
|
|
CHECK( libFunctionDetourCounter == 0 );
|
|
}
|
|
|
|
TEST_CASE( "Must start a transaction to detach", "[transaction]" )
|
|
{
|
|
libFunctionCounter = 0;
|
|
libFunctionDetourCounter = 0;
|
|
|
|
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 );
|
|
|
|
CHECK( detour_detach_and_commit(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == detour_err_wrong_thread );
|
|
|
|
CHECK( libFunction() == 94 );
|
|
CHECK( libFunctionCounter == 0 );
|
|
CHECK( libFunctionDetourCounter == 1 );
|
|
|
|
CHECK( detour_transaction_begin() == err_none );
|
|
CHECK( detour_detach_and_commit(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
|
|
|
|
CHECK( libFunction() == 42 );
|
|
CHECK( libFunctionCounter == 1 );
|
|
CHECK( libFunctionDetourCounter == 1 );
|
|
}
|
|
|
|
TEST_CASE( "aborting transaction means no detour", "[transaction]" )
|
|
{
|
|
libFunctionCounter = 0;
|
|
libFunctionDetourCounter = 0;
|
|
|
|
REQUIRE( detour_transaction_begin() == err_none );
|
|
REQUIRE( detour_attach(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
|
|
CHECK( detour_transaction_abort() == err_none );
|
|
|
|
CHECK( realLibFunction == libFunction );
|
|
CHECK( libFunctionCounter == 0 );
|
|
CHECK( libFunctionDetourCounter == 0 );
|
|
CHECK( libFunction() == 42 );
|
|
CHECK( libFunctionCounter == 1 );
|
|
CHECK( libFunctionDetourCounter == 0 );
|
|
}
|
|
|
|
TEST_CASE( "an error in a transaction means no detour", "[transaction]" )
|
|
{
|
|
libFunctionCounter = 0;
|
|
libFunctionDetourCounter = 0;
|
|
|
|
REQUIRE( detour_transaction_begin() == err_none );
|
|
REQUIRE( detour_attach(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == err_none );
|
|
// cannot detach because trampoline is not yet in place
|
|
CHECK( detour_detach(reinterpret_cast<detour_func_t*>(&realLibFunction), reinterpret_cast<detour_func_t>(libFunctionDetour)) == KERN_FAILURE );
|
|
CHECK( detour_transaction_commit() == KERN_FAILURE );
|
|
|
|
CHECK( realLibFunction == libFunction );
|
|
CHECK( libFunctionCounter == 0 );
|
|
CHECK( libFunctionDetourCounter == 0 );
|
|
CHECK( libFunction() == 42 );
|
|
CHECK( libFunctionCounter == 1 );
|
|
CHECK( libFunctionDetourCounter == 0 );
|
|
}
|