1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/sourcehook/test/testmanual.cpp Fri Nov 14 04:04:02 2008 -0600
1.3 @@ -0,0 +1,382 @@
1.4 +#include <string>
1.5 +#include "sourcehook.h"
1.6 +#include "sourcehook_test.h"
1.7 +#include "testevents.h"
1.8 +
1.9 +// TESTMANUAL
1.10 +// Test manual hooks
1.11 +// :TODO: test more extensively
1.12 +
1.13 +namespace
1.14 +{
1.15 + StateList g_States;
1.16 + SourceHook::ISourceHook *g_SHPtr;
1.17 + SourceHook::Plugin g_PLID;
1.18 +
1.19 + MAKE_STATE_1(State_Func1_Called, void*); // param1: This pointer
1.20 + MAKE_STATE_2(State_Func2_Called, void*, int); // param1: This pointer; param2: parameter
1.21 + MAKE_STATE_1(State_Func3_Called, void*); // param1: This pointer
1.22 + MAKE_STATE_2(State_Func4_Called, void*, int); // param1: This pointer; param2: parameter
1.23 + MAKE_STATE_1(State_Func5_Called, void*); // param1: This pointer
1.24 +
1.25 + MAKE_STATE_1(State_Func1H_Called, void*);
1.26 + MAKE_STATE_2(State_Func2H_Called, void*, int);
1.27 + MAKE_STATE_1(State_Func3H_Called, void*);
1.28 + MAKE_STATE_2(State_Func4H_Called, void*, int);
1.29 +
1.30 + MAKE_STATE_1(State_Return, short);
1.31 +
1.32 + class TheWall
1.33 + {
1.34 + public:
1.35 + virtual void Func1()
1.36 + {
1.37 + ADD_STATE(State_Func1_Called(reinterpret_cast<void*>(this)));
1.38 + }
1.39 + virtual void Func2(int x)
1.40 + {
1.41 + ADD_STATE(State_Func2_Called(reinterpret_cast<void*>(this), x));
1.42 + }
1.43 +
1.44 + virtual short Func3()
1.45 + {
1.46 + ADD_STATE(State_Func3_Called(reinterpret_cast<void*>(this)));
1.47 + return 3;
1.48 + }
1.49 + virtual short Func4(int x)
1.50 + {
1.51 + ADD_STATE(State_Func4_Called(reinterpret_cast<void*>(this), x));
1.52 + return 4;
1.53 + }
1.54 + virtual void Func5()
1.55 + {
1.56 + ADD_STATE(State_Func5_Called(reinterpret_cast<void*>(this)));
1.57 + }
1.58 +
1.59 + virtual const char *Func6()
1.60 + {
1.61 + return 0;
1.62 + }
1.63 + virtual char *Func7()
1.64 + {
1.65 + return 0;
1.66 + }
1.67 + };
1.68 +
1.69 + SH_DECL_HOOK0_void(TheWall, Func1, SH_NOATTRIB, 0);
1.70 + SH_DECL_HOOK1_void(TheWall, Func2, SH_NOATTRIB, 0, int);
1.71 + SH_DECL_HOOK0(TheWall, Func3, SH_NOATTRIB, 0, short);
1.72 + SH_DECL_HOOK1(TheWall, Func4, SH_NOATTRIB, 0, short, int);
1.73 +
1.74 + SH_DECL_MANUALHOOK0_void(TheWall_Func1, 0, 0, 0);
1.75 + SH_DECL_MANUALHOOK1_void(TheWall_Func2, 1, 0, 0, int);
1.76 + SH_DECL_MANUALHOOK0(TheWall_Func3, 2, 0, 0, short);
1.77 + SH_DECL_MANUALHOOK1(TheWall_Func4, 3, 0, 0, short, int);
1.78 +
1.79 + SH_DECL_MANUALHOOK0(TheWall_Func6, 5, 0, 0, const char *);
1.80 + SH_DECL_MANUALEXTERN0(TheWall_Func6, const char *);
1.81 +
1.82 + SH_DECL_MANUALHOOK0(TheWall_Func7, 6, 0, 0, char *);
1.83 + SH_DECL_MANUALEXTERN0(TheWall_Func7, char *);
1.84 +
1.85 + SH_DECL_MANUALEXTERN0_void(TheWall_Func1);
1.86 + SH_DECL_MANUALEXTERN1(TheWall_Func4, short, int);
1.87 +
1.88 + typedef void (SourceHook::EmptyClass::*MFP_Func1)();
1.89 + typedef void (SourceHook::EmptyClass::*MFP_Func2)(int);
1.90 + typedef short (SourceHook::EmptyClass::*MFP_Func3)();
1.91 + typedef short (SourceHook::EmptyClass::*MFP_Func4)(int);
1.92 +
1.93 + void Handler_Func1()
1.94 + {
1.95 + ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
1.96 + }
1.97 + void Handler_Func2(int x)
1.98 + {
1.99 + ADD_STATE(State_Func2H_Called(META_IFACEPTR(void), x));
1.100 + RETURN_META_MNEWPARAMS(MRES_IGNORED, TheWall_Func2, (0x87654321));
1.101 + }
1.102 + short Handler_Func3()
1.103 + {
1.104 + ADD_STATE(State_Func3H_Called(META_IFACEPTR(void)));
1.105 + return 0;
1.106 + }
1.107 + short Handler_Func4(int x)
1.108 + {
1.109 + ADD_STATE(State_Func4H_Called(META_IFACEPTR(void), x));
1.110 +
1.111 + // newparams (recall) using the manual hook declaration!
1.112 + RETURN_META_VALUE_MNEWPARAMS(MRES_IGNORED, 0, TheWall_Func4, (0x12345678));
1.113 + }
1.114 +
1.115 + struct AnotherBrick
1.116 + {
1.117 + void Handler_Func1()
1.118 + {
1.119 + ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
1.120 + RETURN_META(MRES_SUPERCEDE);
1.121 + }
1.122 + };
1.123 +
1.124 + TheWall *MyInstanceFactory()
1.125 + {
1.126 + return new TheWall;
1.127 + }
1.128 +}
1.129 +
1.130 +bool TestManual(std::string &error)
1.131 +{
1.132 + GET_SHPTR(g_SHPtr);
1.133 + g_PLID = 1337;
1.134 +
1.135 + TheWall *p = MyInstanceFactory();
1.136 + CAutoPtrDestruction<TheWall> apd(p);
1.137 +
1.138 + SourceHook::ManualCallClass *cc = SH_GET_MCALLCLASS(p, sizeof(void*));
1.139 +
1.140 + // 1)
1.141 + // Call each function
1.142 + p->Func1();
1.143 + p->Func2(200);
1.144 + ADD_STATE(State_Return(p->Func3()));
1.145 + ADD_STATE(State_Return(p->Func4(400)));
1.146 +
1.147 + CHECK_STATES((&g_States,
1.148 + new State_Func1_Called(p),
1.149 + new State_Func2_Called(p, 200),
1.150 + new State_Func3_Called(p),
1.151 + new State_Return(3),
1.152 + new State_Func4_Called(p, 400),
1.153 + new State_Return(4),
1.154 + NULL), "Part 1");
1.155 +
1.156 + // 1.1)
1.157 + // Now call each function through the manual call class, using the hook decl and manually
1.158 + SH_MCALL(cc, TheWall_Func1)();
1.159 + SH_MCALL2(cc, MFP_Func1(), 0, 0, 0)();
1.160 + SH_MCALL(cc, TheWall_Func2)(200);
1.161 + SH_MCALL2(cc, MFP_Func2(), 1, 0, 0)(200);
1.162 + ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func3)()));
1.163 + ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func3(), 2, 0, 0)()));
1.164 + ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func4)(400)));
1.165 + ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func4(), 3, 0, 0)(400)));
1.166 +
1.167 + CHECK_STATES((&g_States,
1.168 + new State_Func1_Called(p),
1.169 + new State_Func1_Called(p),
1.170 + new State_Func2_Called(p, 200),
1.171 + new State_Func2_Called(p, 200),
1.172 + new State_Func3_Called(p),
1.173 + new State_Return(3),
1.174 + new State_Func3_Called(p),
1.175 + new State_Return(3),
1.176 + new State_Func4_Called(p, 400),
1.177 + new State_Return(4),
1.178 + new State_Func4_Called(p, 400),
1.179 + new State_Return(4),
1.180 + NULL), "Part 1.1");
1.181 +
1.182 + // 2)
1.183 + // Hook each function normally, call them
1.184 + SH_ADD_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
1.185 + SH_ADD_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
1.186 + SH_ADD_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
1.187 + SH_ADD_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
1.188 +
1.189 + p->Func1();
1.190 + p->Func2(200);
1.191 + ADD_STATE(State_Return(p->Func3()));
1.192 + ADD_STATE(State_Return(p->Func4(400)));
1.193 +
1.194 + CHECK_STATES((&g_States,
1.195 + new State_Func1H_Called(p),
1.196 + new State_Func1_Called(p),
1.197 + new State_Func2H_Called(p, 200),
1.198 + new State_Func2_Called(p, 0x87654321),
1.199 + new State_Func3H_Called(p),
1.200 + new State_Func3_Called(p),
1.201 + new State_Return(3),
1.202 + new State_Func4H_Called(p, 400),
1.203 + new State_Func4_Called(p, 0x12345678),
1.204 + new State_Return(4),
1.205 + NULL), "Part 2");
1.206 +
1.207 + // Call them through the mcallclass
1.208 + // 2.1)
1.209 + // Now call each function through the manual call class, using the hook decl and manually
1.210 + SH_MCALL(cc, TheWall_Func1)();
1.211 + SH_MCALL2(cc, MFP_Func1(), 0, 0, 0)();
1.212 + SH_MCALL(cc, TheWall_Func2)(200);
1.213 + SH_MCALL2(cc, MFP_Func2(), 1, 0, 0)(200);
1.214 + ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func3)()));
1.215 + ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func3(), 2, 0, 0)()));
1.216 + ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func4)(400)));
1.217 + ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func4(), 3, 0, 0)(400)));
1.218 +
1.219 + CHECK_STATES((&g_States,
1.220 + new State_Func1_Called(p),
1.221 + new State_Func1_Called(p),
1.222 + new State_Func2_Called(p, 200),
1.223 + new State_Func2_Called(p, 200),
1.224 + new State_Func3_Called(p),
1.225 + new State_Return(3),
1.226 + new State_Func3_Called(p),
1.227 + new State_Return(3),
1.228 + new State_Func4_Called(p, 400),
1.229 + new State_Return(4),
1.230 + new State_Func4_Called(p, 400),
1.231 + new State_Return(4),
1.232 + NULL), "Part 2.1");
1.233 +
1.234 + // Unhook them
1.235 + SH_REMOVE_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
1.236 + SH_REMOVE_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
1.237 + SH_REMOVE_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
1.238 + SH_REMOVE_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
1.239 +
1.240 + // 3)
1.241 + // Hook each function manually, call them
1.242 +
1.243 + SH_ADD_MANUALHOOK(TheWall_Func1, p, SH_STATIC(Handler_Func1), false);
1.244 + SH_ADD_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
1.245 + SH_ADD_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
1.246 + SH_ADD_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
1.247 +
1.248 + p->Func1();
1.249 + p->Func2(200);
1.250 + ADD_STATE(State_Return(p->Func3()));
1.251 + ADD_STATE(State_Return(p->Func4(400)));
1.252 +
1.253 + CHECK_STATES((&g_States,
1.254 + new State_Func1H_Called(p),
1.255 + new State_Func1_Called(p),
1.256 + new State_Func2H_Called(p, 200),
1.257 + new State_Func2_Called(p, 0x87654321),
1.258 + new State_Func3H_Called(p),
1.259 + new State_Func3_Called(p),
1.260 + new State_Return(3),
1.261 + new State_Func4H_Called(p, 400),
1.262 + new State_Func4_Called(p, 0x12345678),
1.263 + new State_Return(4),
1.264 + NULL), "Part 3");
1.265 +
1.266 + // Now through the mcallclass
1.267 +
1.268 + // Call them through the mcallclass
1.269 + // 3.1)
1.270 + // Now call each function through the manual call class, using the hook decl and manually
1.271 + SH_MCALL(cc, TheWall_Func1)();
1.272 + SH_MCALL2(cc, MFP_Func1(), 0, 0, 0)();
1.273 + SH_MCALL(cc, TheWall_Func2)(200);
1.274 + SH_MCALL2(cc, MFP_Func2(), 1, 0, 0)(200);
1.275 + ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func3)()));
1.276 + ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func3(), 2, 0, 0)()));
1.277 + ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func4)(400)));
1.278 + ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func4(), 3, 0, 0)(400)));
1.279 +
1.280 + CHECK_STATES((&g_States,
1.281 + new State_Func1_Called(p),
1.282 + new State_Func1_Called(p),
1.283 + new State_Func2_Called(p, 200),
1.284 + new State_Func2_Called(p, 200),
1.285 + new State_Func3_Called(p),
1.286 + new State_Return(3),
1.287 + new State_Func3_Called(p),
1.288 + new State_Return(3),
1.289 + new State_Func4_Called(p, 400),
1.290 + new State_Return(4),
1.291 + new State_Func4_Called(p, 400),
1.292 + new State_Return(4),
1.293 + NULL), "Part 3.1");
1.294 +
1.295 + // Unhook them
1.296 + SH_REMOVE_MANUALHOOK(TheWall_Func1, p, SH_STATIC(Handler_Func1), false);
1.297 + SH_REMOVE_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
1.298 + SH_REMOVE_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
1.299 + SH_REMOVE_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
1.300 +
1.301 + // 4)
1.302 + // Hook each function manually, then normally, call, unhook
1.303 +
1.304 + AnotherBrick handler_inst;
1.305 +
1.306 + // Why this?
1.307 + // 1) tests sh_add_manualhook
1.308 + // 2) in my tests, the proto of the manual hook was not equal to the proto of the auto hook
1.309 + // (because there are no attribs for manual hooks).
1.310 + // sourcehook.cpp did a !strcmp(..), so it assigned a new hook manager even though there
1.311 + // already was one for this vfnptr. This hook manager stored the pointer of the original
1.312 + // hook manager's hookfunc as the orig pointer - everything seemingly worked.
1.313 + // The problem with this is that returning MRES_SUPERCEDE (as AnotherBrick::Handler_Func1
1.314 + // does) will supercede the second hook func from being called - thus bypassing the call
1.315 + // of the auto hook here.
1.316 + SH_ADD_MANUALHOOK(TheWall_Func1, p, SH_MEMBER(&handler_inst, &AnotherBrick::Handler_Func1), false);
1.317 + SH_ADD_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
1.318 + SH_ADD_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
1.319 + SH_ADD_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
1.320 +
1.321 + SH_ADD_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
1.322 + SH_ADD_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
1.323 + SH_ADD_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
1.324 + SH_ADD_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
1.325 +
1.326 + p->Func1();
1.327 + p->Func2(200);
1.328 + ADD_STATE(State_Return(p->Func3()));
1.329 + ADD_STATE(State_Return(p->Func4(400)));
1.330 +
1.331 + CHECK_STATES((&g_States,
1.332 + new State_Func1H_Called(p),
1.333 + new State_Func1H_Called(p),
1.334 + //new State_Func1_Called(p),
1.335 + new State_Func2H_Called(p, 200),
1.336 + new State_Func2H_Called(p, 0x87654321),
1.337 + new State_Func2_Called(p, 0x87654321),
1.338 + new State_Func3H_Called(p),
1.339 + new State_Func3H_Called(p),
1.340 + new State_Func3_Called(p),
1.341 + new State_Return(3),
1.342 + new State_Func4H_Called(p, 400),
1.343 + new State_Func4H_Called(p, 0x12345678),
1.344 + new State_Func4_Called(p, 0x12345678),
1.345 + new State_Return(4),
1.346 + NULL), "Part 4");
1.347 +
1.348 + SH_REMOVE_MANUALHOOK(TheWall_Func1, p, SH_MEMBER(&handler_inst, &AnotherBrick::Handler_Func1), false);
1.349 + SH_REMOVE_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
1.350 + SH_REMOVE_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
1.351 + SH_REMOVE_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
1.352 +
1.353 + SH_REMOVE_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
1.354 + SH_REMOVE_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
1.355 + SH_REMOVE_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
1.356 + SH_REMOVE_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
1.357 +
1.358 + // 5) Reconfigure TheWall_Func1 to actually hook Func5:
1.359 + SH_MANUALHOOK_RECONFIGURE(TheWall_Func1, 4, 0, 0);
1.360 + SH_MANUALHOOK_RECONFIGURE(TheWall_Func1, 4, 0, 0);
1.361 + SH_ADD_MANUALHOOK(TheWall_Func1, p, SH_STATIC(Handler_Func1), false);
1.362 +
1.363 + p->Func5();
1.364 +
1.365 + SH_MCALL(p, TheWall_Func1)();
1.366 +
1.367 + CHECK_STATES((&g_States,
1.368 + new State_Func1H_Called(p),
1.369 + new State_Func5_Called(p),
1.370 + new State_Func5_Called(p),
1.371 + NULL), "Part 5");
1.372 +
1.373 + // 6) Test auto-remove on reconfig
1.374 + SH_MANUALHOOK_RECONFIGURE(TheWall_Func1, 0, 0, 0);
1.375 +
1.376 + p->Func1();
1.377 + p->Func5();
1.378 +
1.379 + CHECK_STATES((&g_States,
1.380 + new State_Func1_Called(p),
1.381 + new State_Func5_Called(p),
1.382 + NULL), "Part 6");
1.383 +
1.384 + return true;
1.385 +}