xref: /netbsd-src/external/bsd/jemalloc/dist/test/unit/test_hooks.c (revision 7bdf38e5b7a28439665f2fdeff81e36913eef7dd)
1*7bdf38e5Schristos #include "test/jemalloc_test.h"
2*7bdf38e5Schristos 
3*7bdf38e5Schristos static bool hook_called = false;
4*7bdf38e5Schristos 
5*7bdf38e5Schristos static void
6*7bdf38e5Schristos hook() {
7*7bdf38e5Schristos 	hook_called = true;
8*7bdf38e5Schristos }
9*7bdf38e5Schristos 
10*7bdf38e5Schristos static int
11*7bdf38e5Schristos func_to_hook(int arg1, int arg2) {
12*7bdf38e5Schristos 	return arg1 + arg2;
13*7bdf38e5Schristos }
14*7bdf38e5Schristos 
15*7bdf38e5Schristos #define func_to_hook JEMALLOC_TEST_HOOK(func_to_hook, test_hooks_libc_hook)
16*7bdf38e5Schristos 
17*7bdf38e5Schristos TEST_BEGIN(unhooked_call) {
18*7bdf38e5Schristos 	test_hooks_libc_hook = NULL;
19*7bdf38e5Schristos 	hook_called = false;
20*7bdf38e5Schristos 	expect_d_eq(3, func_to_hook(1, 2), "Hooking changed return value.");
21*7bdf38e5Schristos 	expect_false(hook_called, "Nulling out hook didn't take.");
22*7bdf38e5Schristos }
23*7bdf38e5Schristos TEST_END
24*7bdf38e5Schristos 
25*7bdf38e5Schristos TEST_BEGIN(hooked_call) {
26*7bdf38e5Schristos 	test_hooks_libc_hook = &hook;
27*7bdf38e5Schristos 	hook_called = false;
28*7bdf38e5Schristos 	expect_d_eq(3, func_to_hook(1, 2), "Hooking changed return value.");
29*7bdf38e5Schristos 	expect_true(hook_called, "Hook should have executed.");
30*7bdf38e5Schristos }
31*7bdf38e5Schristos TEST_END
32*7bdf38e5Schristos 
33*7bdf38e5Schristos int
34*7bdf38e5Schristos main(void) {
35*7bdf38e5Schristos 	return test(
36*7bdf38e5Schristos 	    unhooked_call,
37*7bdf38e5Schristos 	    hooked_call);
38*7bdf38e5Schristos }
39