more tests and tweaks/fixes as a result
This commit is contained in:
parent
327dfb0cb7
commit
058069d1f3
6 changed files with 194 additions and 101 deletions
108
tests/test_dylib_function.cpp
Normal file
108
tests/test_dylib_function.cpp
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
// Copyright (c) Lysann Tranvouez. All rights reserved.
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include <mach_detours.h>
|
||||
|
||||
#include "lib_function.h"
|
||||
int (*realLibFunction)() = libFunction;
|
||||
|
||||
static int libFunctionDetourCounter = 0;
|
||||
int libFunctionDetour()
|
||||
{
|
||||
libFunctionDetourCounter++;
|
||||
return 94;
|
||||
}
|
||||
|
||||
TEST_CASE( "Overriding custom function in dylib", "[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( 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 );
|
||||
}
|
||||
|
||||
CHECK( realLibFunction != libFunction );
|
||||
CHECK( libFunctionCounter == 1 );
|
||||
CHECK( libFunctionDetourCounter == 0 );
|
||||
CHECK( libFunction() == 94 );
|
||||
CHECK( libFunctionCounter == 1 );
|
||||
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>(libFunctionDetour)) == err_none );
|
||||
}
|
||||
|
||||
SECTION( "detaching in separate transaction removes detour" )
|
||||
{
|
||||
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 );
|
||||
|
||||
REQUIRE( detour_transaction_begin() == err_none );
|
||||
|
||||
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 );
|
||||
}
|
||||
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 );
|
||||
}
|
||||
|
||||
SECTION( "aborting transaction means no detour" )
|
||||
{
|
||||
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 );
|
||||
}
|
||||
|
||||
SECTION( "an error in a transaction means no detour" )
|
||||
{
|
||||
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 );
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue