2 #include "sourcehook.h"
3 #include "sourcehook_test.h"
4 #include "testevents.h"
8 // :TODO: test more extensively
13 SourceHook::ISourceHook *g_SHPtr;
14 SourceHook::Plugin g_PLID;
16 MAKE_STATE_1(State_Func1_Called, void*); // param1: This pointer
17 MAKE_STATE_2(State_Func2_Called, void*, int); // param1: This pointer; param2: parameter
18 MAKE_STATE_1(State_Func3_Called, void*); // param1: This pointer
19 MAKE_STATE_2(State_Func4_Called, void*, int); // param1: This pointer; param2: parameter
20 MAKE_STATE_1(State_Func5_Called, void*); // param1: This pointer
22 MAKE_STATE_1(State_Func1H_Called, void*);
23 MAKE_STATE_2(State_Func2H_Called, void*, int);
24 MAKE_STATE_1(State_Func3H_Called, void*);
25 MAKE_STATE_2(State_Func4H_Called, void*, int);
27 MAKE_STATE_1(State_Return, short);
34 ADD_STATE(State_Func1_Called(reinterpret_cast<void*>(this)));
36 virtual void Func2(int x)
38 ADD_STATE(State_Func2_Called(reinterpret_cast<void*>(this), x));
43 ADD_STATE(State_Func3_Called(reinterpret_cast<void*>(this)));
46 virtual short Func4(int x)
48 ADD_STATE(State_Func4_Called(reinterpret_cast<void*>(this), x));
53 ADD_STATE(State_Func5_Called(reinterpret_cast<void*>(this)));
57 SH_DECL_HOOK0_void(TheWall, Func1, SH_NOATTRIB, 0);
58 SH_DECL_HOOK1_void(TheWall, Func2, SH_NOATTRIB, 0, int);
59 SH_DECL_HOOK0(TheWall, Func3, SH_NOATTRIB, 0, short);
60 SH_DECL_HOOK1(TheWall, Func4, SH_NOATTRIB, 0, short, int);
62 SH_DECL_MANUALHOOK0_void(TheWall_Func1, 0, 0, 0);
63 SH_DECL_MANUALHOOK1_void(TheWall_Func2, 1, 0, 0, int);
64 SH_DECL_MANUALHOOK0(TheWall_Func3, 2, 0, 0, short);
65 SH_DECL_MANUALHOOK1(TheWall_Func4, 3, 0, 0, short, int);
67 SH_DECL_MANUALEXTERN0_void(TheWall_Func1);
68 SH_DECL_MANUALEXTERN1(TheWall_Func1, short, int);
70 typedef void (SourceHook::EmptyClass::*MFP_Func1)();
71 typedef void (SourceHook::EmptyClass::*MFP_Func2)(int);
72 typedef short (SourceHook::EmptyClass::*MFP_Func3)();
73 typedef short (SourceHook::EmptyClass::*MFP_Func4)(int);
77 ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
79 void Handler_Func2(int x)
81 ADD_STATE(State_Func2H_Called(META_IFACEPTR(void), x));
82 RETURN_META_MNEWPARAMS(MRES_IGNORED, TheWall_Func2, (0x87654321));
86 ADD_STATE(State_Func3H_Called(META_IFACEPTR(void)));
89 short Handler_Func4(int x)
91 ADD_STATE(State_Func4H_Called(META_IFACEPTR(void), x));
93 // newparams (recall) using the manual hook declaration!
94 RETURN_META_VALUE_MNEWPARAMS(MRES_IGNORED, 0, TheWall_Func4, (0x12345678));
101 ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
102 RETURN_META(MRES_SUPERCEDE);
106 TheWall *MyInstanceFactory()
112 bool TestManual(std::string &error)
117 TheWall *p = MyInstanceFactory();
118 CAutoPtrDestruction<TheWall> apd(p);
120 SourceHook::ManualCallClass *cc = SH_GET_MCALLCLASS(p, sizeof(void*));
123 // Call each function
126 ADD_STATE(State_Return(p->Func3()));
127 ADD_STATE(State_Return(p->Func4(400)));
129 CHECK_STATES((&g_States,
130 new State_Func1_Called(p),
131 new State_Func2_Called(p, 200),
132 new State_Func3_Called(p),
134 new State_Func4_Called(p, 400),
139 // Now call each function through the manual call class, using the hook decl and manually
140 SH_MCALL(cc, TheWall_Func1)();
141 SH_MCALL2(cc, MFP_Func1(), 0, 0, 0)();
142 SH_MCALL(cc, TheWall_Func2)(200);
143 SH_MCALL2(cc, MFP_Func2(), 1, 0, 0)(200);
144 ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func3)()));
145 ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func3(), 2, 0, 0)()));
146 ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func4)(400)));
147 ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func4(), 3, 0, 0)(400)));
149 CHECK_STATES((&g_States,
150 new State_Func1_Called(p),
151 new State_Func1_Called(p),
152 new State_Func2_Called(p, 200),
153 new State_Func2_Called(p, 200),
154 new State_Func3_Called(p),
156 new State_Func3_Called(p),
158 new State_Func4_Called(p, 400),
160 new State_Func4_Called(p, 400),
165 // Hook each function normally, call them
166 SH_ADD_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
167 SH_ADD_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
168 SH_ADD_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
169 SH_ADD_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
173 ADD_STATE(State_Return(p->Func3()));
174 ADD_STATE(State_Return(p->Func4(400)));
176 CHECK_STATES((&g_States,
177 new State_Func1H_Called(p),
178 new State_Func1_Called(p),
179 new State_Func2H_Called(p, 200),
180 new State_Func2_Called(p, 0x87654321),
181 new State_Func3H_Called(p),
182 new State_Func3_Called(p),
184 new State_Func4H_Called(p, 400),
185 new State_Func4_Called(p, 0x12345678),
189 // Call them through the mcallclass
191 // Now call each function through the manual call class, using the hook decl and manually
192 SH_MCALL(cc, TheWall_Func1)();
193 SH_MCALL2(cc, MFP_Func1(), 0, 0, 0)();
194 SH_MCALL(cc, TheWall_Func2)(200);
195 SH_MCALL2(cc, MFP_Func2(), 1, 0, 0)(200);
196 ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func3)()));
197 ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func3(), 2, 0, 0)()));
198 ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func4)(400)));
199 ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func4(), 3, 0, 0)(400)));
201 CHECK_STATES((&g_States,
202 new State_Func1_Called(p),
203 new State_Func1_Called(p),
204 new State_Func2_Called(p, 200),
205 new State_Func2_Called(p, 200),
206 new State_Func3_Called(p),
208 new State_Func3_Called(p),
210 new State_Func4_Called(p, 400),
212 new State_Func4_Called(p, 400),
217 SH_REMOVE_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
218 SH_REMOVE_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
219 SH_REMOVE_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
220 SH_REMOVE_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
223 // Hook each function manually, call them
225 SH_ADD_MANUALHOOK(TheWall_Func1, p, SH_STATIC(Handler_Func1), false);
226 SH_ADD_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
227 SH_ADD_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
228 SH_ADD_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
232 ADD_STATE(State_Return(p->Func3()));
233 ADD_STATE(State_Return(p->Func4(400)));
235 CHECK_STATES((&g_States,
236 new State_Func1H_Called(p),
237 new State_Func1_Called(p),
238 new State_Func2H_Called(p, 200),
239 new State_Func2_Called(p, 0x87654321),
240 new State_Func3H_Called(p),
241 new State_Func3_Called(p),
243 new State_Func4H_Called(p, 400),
244 new State_Func4_Called(p, 0x12345678),
248 // Now through the mcallclass
250 // Call them through the mcallclass
252 // Now call each function through the manual call class, using the hook decl and manually
253 SH_MCALL(cc, TheWall_Func1)();
254 SH_MCALL2(cc, MFP_Func1(), 0, 0, 0)();
255 SH_MCALL(cc, TheWall_Func2)(200);
256 SH_MCALL2(cc, MFP_Func2(), 1, 0, 0)(200);
257 ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func3)()));
258 ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func3(), 2, 0, 0)()));
259 ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func4)(400)));
260 ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func4(), 3, 0, 0)(400)));
262 CHECK_STATES((&g_States,
263 new State_Func1_Called(p),
264 new State_Func1_Called(p),
265 new State_Func2_Called(p, 200),
266 new State_Func2_Called(p, 200),
267 new State_Func3_Called(p),
269 new State_Func3_Called(p),
271 new State_Func4_Called(p, 400),
273 new State_Func4_Called(p, 400),
278 SH_REMOVE_MANUALHOOK(TheWall_Func1, p, SH_STATIC(Handler_Func1), false);
279 SH_REMOVE_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
280 SH_REMOVE_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
281 SH_REMOVE_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
284 // Hook each function manually, then normally, call, unhook
286 AnotherBrick handler_inst;
289 // 1) tests sh_add_manualhook
290 // 2) in my tests, the proto of the manual hook was not equal to the proto of the auto hook
291 // (because there are no attribs for manual hooks).
292 // sourcehook.cpp did a !strcmp(..), so it assigned a new hook manager even though there
293 // already was one for this vfnptr. This hook manager stored the pointer of the original
294 // hook manager's hookfunc as the orig pointer - everything seemingly worked.
295 // The problem with this is that returning MRES_SUPERCEDE (as AnotherBrick::Handler_Func1
296 // does) will supercede the second hook func from being called - thus bypassing the call
297 // of the auto hook here.
298 SH_ADD_MANUALHOOK(TheWall_Func1, p, SH_MEMBER(&handler_inst, &AnotherBrick::Handler_Func1), false);
299 SH_ADD_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
300 SH_ADD_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
301 SH_ADD_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
303 SH_ADD_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
304 SH_ADD_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
305 SH_ADD_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
306 SH_ADD_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
310 ADD_STATE(State_Return(p->Func3()));
311 ADD_STATE(State_Return(p->Func4(400)));
313 CHECK_STATES((&g_States,
314 new State_Func1H_Called(p),
315 new State_Func1H_Called(p),
316 //new State_Func1_Called(p),
317 new State_Func2H_Called(p, 200),
318 new State_Func2H_Called(p, 0x87654321),
319 new State_Func2_Called(p, 0x87654321),
320 new State_Func3H_Called(p),
321 new State_Func3H_Called(p),
322 new State_Func3_Called(p),
324 new State_Func4H_Called(p, 400),
325 new State_Func4H_Called(p, 0x12345678),
326 new State_Func4_Called(p, 0x12345678),
330 SH_REMOVE_MANUALHOOK(TheWall_Func1, p, SH_MEMBER(&handler_inst, &AnotherBrick::Handler_Func1), false);
331 SH_REMOVE_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
332 SH_REMOVE_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
333 SH_REMOVE_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
335 SH_REMOVE_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
336 SH_REMOVE_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
337 SH_REMOVE_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
338 SH_REMOVE_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
340 // 5) Reconfigure TheWall_Func1 to actually hook Func5:
341 SH_MANUALHOOK_RECONFIGURE(TheWall_Func1, 4, 0, 0);
342 SH_MANUALHOOK_RECONFIGURE(TheWall_Func1, 4, 0, 0);
343 SH_ADD_MANUALHOOK(TheWall_Func1, p, SH_STATIC(Handler_Func1), false);
347 CHECK_STATES((&g_States,
348 new State_Func1H_Called(p),
349 new State_Func5_Called(p),
352 // 6) Test auto-remove on reconfig
353 SH_MANUALHOOK_RECONFIGURE(TheWall_Func1, 0, 0, 0);
358 CHECK_STATES((&g_States,
359 new State_Func1_Called(p),
360 new State_Func5_Called(p),