From 6cc020b2fd950264cba0decd7d5d986c8a3fe768 Mon Sep 17 00:00:00 2001 From: Zeex Date: Thu, 6 Sep 2018 23:52:15 +0600 Subject: Rename "options" to "flags" --- tests/CMakeLists.txt | 64 ++++++++++++++++++++++++-------------------- tests/test.c | 4 +-- tests/test.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 30 deletions(-) create mode 100644 tests/test.cpp (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1b2514e..92c8461 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -40,39 +40,46 @@ add_custom_command( MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/${asm_file}" ) -add_executable(subhook_test +add_executable(subhook_test_exe test.c "${CMAKE_CURRENT_BINARY_DIR}/${asm_file}.obj" ) +set_target_properties(subhook_test_exe PROPERTIES OUTPUT_NAME test) -set_target_properties(subhook_test PROPERTIES OUTPUT_NAME test) +enable_language(CXX) +add_executable(subhook_cxx_test_exe + test.cpp + "${CMAKE_CURRENT_BINARY_DIR}/${asm_file}.obj" +) +set_target_properties(subhook_cxx_test_exe PROPERTIES OUTPUT_NAME test++) -if(SUBHOOK_FORCE_32BIT) - if(APPLE) - set_target_properties(subhook_test PROPERTIES OSX_ARCHITECTURES i386) - endif() - if(UNIX) - set_property(TARGET subhook_test APPEND_STRING PROPERTY - COMPILE_FLAGS " -m32") - set_property(TARGET subhook_test APPEND_STRING PROPERTY LINK_FLAGS " -m32") +foreach(target subhook_test_exe subhook_cxx_test_exe) + if(SUBHOOK_FORCE_32BIT) + if(APPLE) + set_target_properties(${target} PROPERTIES OSX_ARCHITECTURES i386) + endif() + if(UNIX) + set_property(TARGET ${target} APPEND_STRING PROPERTY + COMPILE_FLAGS " -m32") + set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS " -m32") + endif() endif() -endif() -target_link_libraries(subhook_test subhook) + target_link_libraries(${target} subhook) -if(MSVC) - set_property(TARGET subhook_test - APPEND_STRING PROPERTY LINK_FLAGS " /INCREMENTAL:NO") -endif() + if(MSVC) + set_property(TARGET ${target} + APPEND_STRING PROPERTY LINK_FLAGS " /INCREMENTAL:NO") + endif() -if(APPLE AND CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT SUBHOOK_FORCE_32BIT) - set_property(TARGET subhook_test APPEND_STRING PROPERTY - LINK_FLAGS " -Wl,-no_pie") -endif() + if(APPLE AND CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT SUBHOOK_FORCE_32BIT) + set_property(TARGET ${target} APPEND_STRING PROPERTY + LINK_FLAGS " -Wl,-no_pie") + endif() -add_test(NAME test COMMAND $) + add_test(NAME ${target}_test COMMAND $) -set(expected_output "\ + set(expected_output "\ Testing initial install[\r\n]+\ foo_hooked\\(\\) called[\r\n]+\ foo\\(\\) called[\r\n]+\ @@ -83,10 +90,11 @@ Testing trampoline[\r\n]+\ foo_hooked_tr\\(\\) called[\r\n]+\ foo\\(\\) called[\r\n]+\ ") -set_tests_properties(test PROPERTIES - PASS_REGULAR_EXPRESSION "${expected_output}") + set_tests_properties(${target}_test PROPERTIES + PASS_REGULAR_EXPRESSION "${expected_output}") -if(WIN32 AND NOT SUBHOOK_STATIC) - set_tests_properties(test PROPERTIES - ENVIRONMENT PATH=$) -endif() + if(WIN32 AND NOT SUBHOOK_STATIC) + set_tests_properties(${target}_test PROPERTIES + ENVIRONMENT PATH=$) + endif() +endforeach() diff --git a/tests/test.c b/tests/test.c index 278a4fd..513c060 100644 --- a/tests/test.c +++ b/tests/test.c @@ -32,7 +32,7 @@ int main() { subhook_t foo_hook = subhook_new((void *)foo, (void *)foo_hooked, - SUBHOOK_OPTION_64BIT_OFFSET); + SUBHOOK_64BIT_OFFSET); if (foo_hook == NULL || subhook_install(foo_hook) < 0) { puts("Install failed"); return EXIT_FAILURE; @@ -61,7 +61,7 @@ int main() { subhook_t foo_hook_tr = subhook_new((void *)foo, (void *)foo_hooked_tr, - SUBHOOK_OPTION_64BIT_OFFSET); + SUBHOOK_64BIT_OFFSET); if (subhook_install(foo_hook_tr) < 0) { puts("Install failed"); return EXIT_FAILURE; diff --git a/tests/test.cpp b/tests/test.cpp new file mode 100644 index 0000000..0eee1fa --- /dev/null +++ b/tests/test.cpp @@ -0,0 +1,75 @@ +#include +#include + +typedef void (*foo_func_t)(); + +#ifdef SUBHOOK_X86 + #if defined SUBHOOK_WINDOWS + #define FOO_CALL __cdecl + #elif defined SUBHOOK_UNIX + #define FOO_CALL __attribute__((cdecl)) + #endif +#else + #define FOO_CALL +#endif + +extern "C" void FOO_CALL foo(); +foo_func_t foo_tr = nullptr; + +void foo_hooked() { + std::cout << "foo_hooked() called" << std::endl;; +} + +void foo_hooked_tr() { + std::cout << "foo_hooked_tr() called" << std::endl; + foo_tr(); +} + +int main() { + std::cout << "Testing initial install" << std::endl; + + subhook::Hook foo_hook((void *)foo, + (void *)foo_hooked, + subhook::HookFlag64BitOffset); + if (!foo_hook.Install()) { + std::cout << "Install failed" << std::endl; + return EXIT_FAILURE; + } + foo(); + if (!foo_hook.Remove()) { + std::cout << "Remove failed" << std::endl; + return EXIT_FAILURE; + } + foo(); + + std::cout << "Testing re-install" << std::endl; + + if (!foo_hook.Install()) { + std::cout << "Install failed" << std::endl; + return EXIT_FAILURE; + } + foo(); + if (!foo_hook.Remove()) { + std::cout << "Remove failed" << std::endl; + return EXIT_FAILURE; + } + foo(); + + std::cout << "Testing trampoline" << std::endl; + + subhook::Hook foo_hook_tr((void *)foo, + (void *)foo_hooked_tr, + subhook::HookFlag64BitOffset); + if (!foo_hook_tr.Install()) { + std::cout << "Install failed" << std::endl; + return EXIT_FAILURE; + } + foo_tr = (foo_func_t)foo_hook_tr.GetTrampoline(); + if (foo_tr == nullptr) { + std::cout << "Failed to build trampoline" << std::endl; + return EXIT_FAILURE; + } + foo(); + + return EXIT_SUCCESS; +} -- cgit v1.1