summaryrefslogtreecommitdiffhomepage
path: root/test/hook.test.c
diff options
context:
space:
mode:
authorMichael Smith <mikesmiffy128@gmail.com>2021-11-20 03:10:50 +0000
committerMichael Smith <mikesmiffy128@gmail.com>2021-11-20 03:18:08 +0000
commitda6f343032cb01597dc7866e66f091adf3243a62 (patch)
tree870f8cb8e82bb42202ab92bea03fc6ab35ada7ca /test/hook.test.c
Initial public snapshot
With code from Bill. Thanks Bill!
Diffstat (limited to 'test/hook.test.c')
-rw-r--r--test/hook.test.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/test/hook.test.c b/test/hook.test.c
new file mode 100644
index 0000000..a918e22
--- /dev/null
+++ b/test/hook.test.c
@@ -0,0 +1,45 @@
+/* This file is dedicated to the public domain. */
+
+{.desc = "inline function hooking"};
+
+#ifdef _WIN32
+
+#include "../src/udis86.c"
+#include "../src/os.c"
+#include "../src/hook.c"
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+// stubs
+void con_warn(const char *msg, ...) {
+ va_list l;
+ va_start(l, msg);
+ vfprintf(stderr, msg, l);
+ 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;
+}
+
+TEST("Inline hooks should be able to wrap the original function", 0) {
+ orig_some_function = hook_inline(&some_function, &some_hook);
+ if (!orig_some_function) return false;
+ return some_function(5, 5) == 15;
+}
+
+TEST("Inline hooks should be removable again", 0) {
+ 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;
+}
+
+#endif
+
+// vi: sw=4 ts=4 noet tw=80 cc=80