add test case

This commit is contained in:
Lysann Tranvouez 2025-10-01 23:46:55 +02:00
parent 7c9b945386
commit 4947fb5553
4 changed files with 67 additions and 1 deletions

View file

@ -1,6 +1,14 @@
add_executable(mach_detours_tests add_executable(mach_detours_tests
test.cpp) test.cpp)
target_link_libraries(mach_detours_tests add_library(mach_detours_test_func SHARED
PRIVATE Catch2::Catch2WithMain mach_detours test_func.c
test_func.h
)
target_link_libraries(mach_detours_tests
PRIVATE
mach_detours
Catch2::Catch2WithMain
mach_detours_test_func
) )

View file

@ -1 +1,37 @@
// Copyright (c) Lysann Tranvouez. All rights reserved. // Copyright (c) Lysann Tranvouez. All rights reserved.
#include <catch2/catch_test_macros.hpp>
#include <mach_detours.h>
#include "test_func.h"
static int testFunctionDetourCounter = 0;
int testFunctionDetour()
{
testFunctionDetourCounter++;
return 94;
}
TEST_CASE( "Overriding custom function" )
{
int (*realTestFunction)() = testFunction;
testFunctionCounter = 0;
testFunctionDetourCounter = 0;
REQUIRE( testFunction() == 42 );
REQUIRE( testFunctionCounter == 1 );
REQUIRE( testFunctionDetourCounter == 0 );
CHECK( detour_transaction_begin() == err_none );
CHECK( detour_attach(reinterpret_cast<detour_func_t*>(&realTestFunction), reinterpret_cast<detour_func_t>(testFunctionDetour)) == err_none );
CHECK( detour_transaction_commit() == err_none );
REQUIRE( realTestFunction != testFunction );
REQUIRE( testFunctionCounter == 1 );
REQUIRE( testFunctionDetourCounter == 0 );
REQUIRE( testFunction() == 94 );
REQUIRE( testFunctionCounter == 1 );
REQUIRE( testFunctionDetourCounter == 1 );
}

9
tests/test_func.c Normal file
View file

@ -0,0 +1,9 @@
#include "test_func.h"
int testFunctionCounter = 0;
int testFunction()
{
testFunctionCounter++;
return 42;
}

13
tests/test_func.h Normal file
View file

@ -0,0 +1,13 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
extern int testFunctionCounter;
int testFunction();
#ifdef __cplusplus
}
#endif