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