diff options
Diffstat (limited to 'test/hook.test.c')
-rw-r--r-- | test/hook.test.c | 51 |
1 files changed, 23 insertions, 28 deletions
diff --git a/test/hook.test.c b/test/hook.test.c index a2447cc..9e7cfa9 100644 --- a/test/hook.test.c +++ b/test/hook.test.c @@ -12,7 +12,7 @@ #include <stdio.h> #include <string.h> -// stubs +// stub void con_warn(const char *msg, ...) { va_list l; va_start(l, msg); @@ -20,43 +20,38 @@ void con_warn(const char *msg, ...) { va_end(l); } -__attribute__((noinline)) -static int some_function(int a, int b) { return a + b; } -static int (*orig_some_function)(int, int); -static int some_hook(int a, int b) { - return orig_some_function(a, b) + 5; -} -__attribute__((noinline)) -static int other_function(int a, int b) { return a - b; } -static int (*orig_other_function)(int, int); -static int other_hook(int a, int b) { - return orig_other_function(a, b) + 5; -} +typedef int (*testfunc)(int, int); + +__attribute__((noinline)) static int func1(int a, int b) { return a + b; } +static int (*orig_func1)(int, int); +static int hook1(int a, int b) { return orig_func1(a, b) + 5; } + +__attribute__((noinline)) static int func2(int a, int b) { return a - b; } +static int (*orig_func2)(int, int); +static int hook2(int a, int b) { return orig_func2(a, b) + 5; } TEST("Inline hooks should be able to wrap the original function") { if (!hook_init()) return false; - orig_some_function = hook_inline(&some_function, &some_hook); - if (!orig_some_function) return false; - return some_function(5, 5) == 15; + orig_func1 = (testfunc)hook_inline((void *)&func1, (void *)&hook1); + if (!orig_func1) return false; + return func1(5, 5) == 15; } TEST("Inline hooks should be removable again") { if (!hook_init()) return false; - orig_some_function = hook_inline(&some_function, &some_hook); - if (!orig_some_function) return false; - unhook_inline(orig_some_function); - return some_function(5, 5) == 10; + orig_func1 = (testfunc)hook_inline((void *)&func1, (void *)&hook1); + if (!orig_func1) return false; + unhook_inline((void *)orig_func1); + return func1(5, 5) == 10; } -TEST("Multiple functions should be able to be inline hooked at once") { +TEST("Multiple functions should be able to be inline-hooked at once") { if (!hook_init()) return false; - orig_some_function = hook_inline(&some_function, &some_hook); - if (!orig_some_function) return false; - - orig_other_function = hook_inline(&other_function, &other_hook); - if (!orig_other_function) return false; - - return other_function(5, 5) == 5; + orig_func1 = (testfunc)hook_inline((void *)&func1, (void *)&hook1); + if (!orig_func1) return false; + orig_func2 = (testfunc)hook_inline((void *)&func2, (void *)&hook2); + if (!orig_func2) return false; + return func2(5, 5) == 5; } #endif |