From 5d833e2928dce9694a846d6e787c7721a6c9e170 Mon Sep 17 00:00:00 2001 From: Lysann Tranvouez Date: Thu, 2 Oct 2025 22:41:33 +0200 Subject: [PATCH] testing system function override --- tests/CMakeLists.txt | 1 + tests/test_dylib_function.cpp | 2 +- tests/test_local_function.cpp | 2 +- tests/test_system_function.cpp | 25 +++++++++++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 tests/test_system_function.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f9cc485..17c8a60 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -3,6 +3,7 @@ add_executable(mach_detours_tests test_dylib_function.cpp test_local_function.cpp + test_system_function.cpp test_transaction.cpp ) diff --git a/tests/test_dylib_function.cpp b/tests/test_dylib_function.cpp index c32679a..4ad66cb 100644 --- a/tests/test_dylib_function.cpp +++ b/tests/test_dylib_function.cpp @@ -14,7 +14,7 @@ static int libFunctionDetour() return 94; } -TEST_CASE( "Overriding custom function in dylib", "[dylib]" ) +TEST_CASE( "Overriding custom function in dylib", "[attach][detach][dylib]" ) { libFunctionCounter = 0; libFunctionDetourCounter = 0; diff --git a/tests/test_local_function.cpp b/tests/test_local_function.cpp index 97af062..49b2d60 100644 --- a/tests/test_local_function.cpp +++ b/tests/test_local_function.cpp @@ -18,7 +18,7 @@ int localFunctionDetour() return 12; } -TEST_CASE( "Overriding local function", "[local][attach]" ) +TEST_CASE( "Overriding local function", "[attach][local]" ) { localFunctionCounter = 0; localFunctionDetourCounter = 0; diff --git a/tests/test_system_function.cpp b/tests/test_system_function.cpp new file mode 100644 index 0000000..31b285f --- /dev/null +++ b/tests/test_system_function.cpp @@ -0,0 +1,25 @@ +// Copyright (c) Lysann Tranvouez. All rights reserved. + +#include + +#include + +char* (*realStrerror)(int) = strerror; +char* strerrorDetour(int errno) +{ + return (char *)"strerrorOverride"; +} + +TEST_CASE( "Overriding system function", "[attach][system]" ) +{ + REQUIRE( std::string_view(strerror( 0 )) == "Undefined error: 0" ); + + REQUIRE( detour_transaction_begin() == err_none ); + REQUIRE( detour_attach_and_commit(reinterpret_cast(&realStrerror), reinterpret_cast(strerrorDetour)) == err_none ); + + CHECK( std::string_view(strerror( 0 )) == "strerrorOverride" ); + + // clean up + CHECK( detour_transaction_begin() == err_none ); + CHECK( detour_detach_and_commit(reinterpret_cast(&realStrerror), reinterpret_cast(strerrorDetour)) == err_none ); +}