hook.test.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* This file is dedicated to the public domain. */
  2. {.desc = "inline function hooking"};
  3. #ifdef _WIN32
  4. #include "../src/x86.c"
  5. #include "../src/hook.c"
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. // stubs
  10. void con_warn(const char *msg, ...) {
  11. va_list l;
  12. va_start(l, msg);
  13. vfprintf(stderr, msg, l);
  14. va_end(l);
  15. }
  16. __attribute__((noinline))
  17. static int some_function(int a, int b) { return a + b; }
  18. static int (*orig_some_function)(int, int);
  19. static int some_hook(int a, int b) {
  20. return orig_some_function(a, b) + 5;
  21. }
  22. __attribute__((noinline))
  23. static int other_function(int a, int b) { return a - b; }
  24. static int (*orig_other_function)(int, int);
  25. static int other_hook(int a, int b) {
  26. return orig_other_function(a, b) + 5;
  27. }
  28. TEST("Inline hooks should be able to wrap the original function") {
  29. if (!hook_init()) return false;
  30. orig_some_function = hook_inline(&some_function, &some_hook);
  31. if (!orig_some_function) return false;
  32. return some_function(5, 5) == 15;
  33. }
  34. TEST("Inline hooks should be removable again") {
  35. if (!hook_init()) return false;
  36. orig_some_function = hook_inline(&some_function, &some_hook);
  37. if (!orig_some_function) return false;
  38. unhook_inline(orig_some_function);
  39. return some_function(5, 5) == 10;
  40. }
  41. TEST("Multiple functions should be able to be inline hooked at once") {
  42. if (!hook_init()) return false;
  43. orig_some_function = hook_inline(&some_function, &some_hook);
  44. if (!orig_some_function) return false;
  45. orig_other_function = hook_inline(&other_function, &other_hook);
  46. if (!orig_other_function) return false;
  47. return other_function(5, 5) == 5;
  48. }
  49. #endif
  50. // vi: sw=4 ts=4 noet tw=80 cc=80