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