From 4947fb555305230a09b204143f8e4b295f5ee00b Mon Sep 17 00:00:00 2001 From: Lysann Tranvouez Date: Wed, 1 Oct 2025 23:46:55 +0200 Subject: [PATCH] add test case --- tests/CMakeLists.txt | 10 +++++++++- tests/test.cpp | 36 ++++++++++++++++++++++++++++++++++++ tests/test_func.c | 9 +++++++++ tests/test_func.h | 13 +++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/test_func.c create mode 100644 tests/test_func.h diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d216ff3..56fc78c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,14 @@ add_executable(mach_detours_tests test.cpp) +add_library(mach_detours_test_func SHARED + test_func.c + test_func.h +) + target_link_libraries(mach_detours_tests - PRIVATE Catch2::Catch2WithMain mach_detours + PRIVATE + mach_detours + Catch2::Catch2WithMain + mach_detours_test_func ) \ No newline at end of file diff --git a/tests/test.cpp b/tests/test.cpp index b8e5087..de4941f 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1 +1,37 @@ // Copyright (c) Lysann Tranvouez. All rights reserved. + +#include + +#include + +#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(&realTestFunction), reinterpret_cast(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 ); +} diff --git a/tests/test_func.c b/tests/test_func.c new file mode 100644 index 0000000..2d688c3 --- /dev/null +++ b/tests/test_func.c @@ -0,0 +1,9 @@ +#include "test_func.h" + +int testFunctionCounter = 0; + +int testFunction() +{ + testFunctionCounter++; + return 42; +} diff --git a/tests/test_func.h b/tests/test_func.h new file mode 100644 index 0000000..0dbf07f --- /dev/null +++ b/tests/test_func.h @@ -0,0 +1,13 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +extern int testFunctionCounter; + +int testFunction(); + +#ifdef __cplusplus +} +#endif