-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhook.test.cpp
87 lines (68 loc) · 2.64 KB
/
hook.test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <boost/ut.hpp>
#include <lime/hooks/hook.hpp>
#include <iostream>
using namespace boost::ut;
using namespace boost::ut::literals;
int test_fn(int param)
{
std::cout << std::format("test_fn({}): called", param) << std::endl;
if (param == 1337)
{
return 0;
}
return param;
}
std::unique_ptr<lime::hook<int(int)>> original_test;
int test_hook(int param)
{
return original_test->original()(param + 5);
}
suite<"Hooks"> hook_suite = []
{
expect(eq(test_fn(10), 10));
expect(eq(test_fn(1337), 0));
original_test = std::move(lime::hook<int(int)>::create(test_fn, test_hook).value());
expect(eq(test_fn(10), 15));
expect(eq(test_fn(1337), 1342));
original_test.reset();
expect(eq(test_fn(10), 10));
original_test = std::move(lime::make_hook(test_fn, test_hook).value());
expect(eq(test_fn(10), 15));
original_test.reset();
expect(eq(test_fn(10), 10));
lime::hook<int(int)>::create(test_fn,
[](auto *hook, int param) -> int
{
auto rtn = hook->original()(param + 10);
delete hook;
return rtn;
});
#if INTPTR_MAX == INT32_MAX
using hook_t = lime::hook<int(void *, int), lime::convention::c_fastcall>;
hook_t::create(0xDEADBEEF,
[&](auto *hook, void *thiz, int param) -> int
{
auto ret = hook->original()(thiz, param);
delete hook;
return ret;
});
lime::make_hook<int(void *, int), lime::convention::automatic>(0xDEADBEEF,
[&](auto *hook, void *thiz, int param) -> int
{
auto ret = hook->original()(thiz, param);
delete hook;
return ret;
});
#endif
expect(eq(test_fn(10), 20));
expect(eq(test_fn(10), 10));
lime::make_hook(test_fn,
[](auto *hook, int param) -> int
{
auto rtn = hook->original()(param + 10);
delete hook;
return rtn;
});
expect(eq(test_fn(10), 20));
expect(eq(test_fn(10), 10));
};