1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/sourcehook/test/test3.cpp Fri Nov 14 04:04:02 2008 -0600
1.3 @@ -0,0 +1,245 @@
1.4 +#include <string>
1.5 +#include "sourcehook_test.h"
1.6 +#include "testevents.h"
1.7 +
1.8 +// TEST3
1.9 +// Tests with inheritance / thisptroffsets
1.10 +
1.11 +namespace
1.12 +{
1.13 + StateList g_States;
1.14 + SourceHook::ISourceHook *g_SHPtr;
1.15 + SourceHook::Plugin g_PLID;
1.16 +
1.17 + MAKE_STATE_1(State_Func1_Called, void*); // param1: This pointer
1.18 + MAKE_STATE_1(State_Func2_Called, void*); // param1: This pointer
1.19 + MAKE_STATE_1(State_Func3_Called, void*); // param1: This pointer
1.20 +
1.21 + MAKE_STATE_1(State_Func1H_Called, void*);
1.22 + MAKE_STATE_1(State_Func2H_Called, void*);
1.23 + MAKE_STATE_1(State_Func3H_Called, void*);
1.24 +
1.25 + class Base1
1.26 + {
1.27 + int a;
1.28 + public:
1.29 + virtual void Func1()
1.30 + {
1.31 + ADD_STATE(State_Func1_Called(reinterpret_cast<void*>(this)));
1.32 + }
1.33 + };
1.34 + class Base2
1.35 + {
1.36 + int b;
1.37 + public:
1.38 + virtual void Func2()
1.39 + {
1.40 + ADD_STATE(State_Func2_Called(reinterpret_cast<void*>(this)));
1.41 + }
1.42 + };
1.43 + class Derived : public Base1, public Base2
1.44 + {
1.45 + int c;
1.46 + public:
1.47 + virtual void Func3()
1.48 + {
1.49 + ADD_STATE(State_Func3_Called(reinterpret_cast<void*>(this)));
1.50 + }
1.51 + };
1.52 +
1.53 + SH_DECL_HOOK0_void(Derived, Func1, SH_NOATTRIB, 0);
1.54 + SH_DECL_HOOK0_void(Derived, Func2, SH_NOATTRIB, 0);
1.55 + SH_DECL_HOOK0_void(Derived, Func3, SH_NOATTRIB, 0);
1.56 + SH_DECL_HOOK0_void(Base1, Func1, SH_NOATTRIB, 0);
1.57 + SH_DECL_HOOK0_void(Base2, Func2, SH_NOATTRIB, 0);
1.58 +
1.59 + void Handler_Func1()
1.60 + {
1.61 + ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
1.62 + }
1.63 + void Handler_Func2()
1.64 + {
1.65 + ADD_STATE(State_Func2H_Called(META_IFACEPTR(void)));
1.66 + }
1.67 + void Handler_Func3()
1.68 + {
1.69 + ADD_STATE(State_Func3H_Called(META_IFACEPTR(void)));
1.70 + }
1.71 +
1.72 + Derived *MyInstanceFactory()
1.73 + {
1.74 + return new Derived;
1.75 + }
1.76 +}
1.77 +
1.78 +bool TestThisPtrOffs(std::string &error)
1.79 +{
1.80 + GET_SHPTR(g_SHPtr);
1.81 + g_PLID = 1337;
1.82 +
1.83 + Derived *pD = MyInstanceFactory();
1.84 + Base1 *pB1 = pD;
1.85 + Base2 *pB2 = pD;
1.86 +
1.87 + CAutoPtrDestruction<Derived> apd(pD);
1.88 +
1.89 +
1.90 + // It should be:
1.91 + // pB1 = pD
1.92 + // pB2 > pB1
1.93 +
1.94 + // 1)
1.95 + // Get a callclass for pD
1.96 + // Verify whether the this pointers are correct
1.97 + // Also call them normally to make sure that we aren't messing it up ;)
1.98 + SourceHook::CallClass<Derived> *pD_CC = SH_GET_CALLCLASS(pD);
1.99 +
1.100 + SH_CALL(pD_CC, &Derived::Func1)();
1.101 + SH_CALL(pD_CC, &Derived::Func2)();
1.102 + SH_CALL(pD_CC, &Derived::Func3)();
1.103 + pD->Func1();
1.104 + pD->Func2();
1.105 + pD->Func3();
1.106 +
1.107 + CHECK_STATES((&g_States,
1.108 + new State_Func1_Called(pB1),
1.109 + new State_Func2_Called(pB2),
1.110 + new State_Func3_Called(pD),
1.111 + new State_Func1_Called(pB1),
1.112 + new State_Func2_Called(pB2),
1.113 + new State_Func3_Called(pD),
1.114 + NULL), "Part 1");
1.115 +
1.116 + SH_CALL(pD_CC, &Base1::Func1)();
1.117 + SH_CALL(pD_CC, &Base2::Func2)();
1.118 +
1.119 + CHECK_STATES((&g_States,
1.120 + new State_Func1_Called(pB1),
1.121 + new State_Func2_Called(pB2),
1.122 + NULL), "Part 1.1");
1.123 +
1.124 + // 2)
1.125 + // Get callclasses for the other ones and verify it as well
1.126 +
1.127 + SourceHook::CallClass<Base1> *pB1_CC = SH_GET_CALLCLASS(pB1);
1.128 + SourceHook::CallClass<Base2> *pB2_CC = SH_GET_CALLCLASS(pB2);
1.129 +
1.130 + SH_CALL(pB1_CC, &Base1::Func1)();
1.131 + SH_CALL(pB2_CC, &Base2::Func2)();
1.132 +
1.133 + CHECK_STATES((&g_States,
1.134 + new State_Func1_Called(pB1),
1.135 + new State_Func2_Called(pB2),
1.136 + NULL), "Part 2");
1.137 +
1.138 +
1.139 + // 3) Add hooks on them (referring to them through pD1 / Derived)
1.140 + // Check whether the hooks are called with the correct this pointer
1.141 +
1.142 + SH_ADD_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
1.143 + SH_ADD_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
1.144 + SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
1.145 +
1.146 + pD->Func1();
1.147 + pD->Func2();
1.148 + pD->Func3();
1.149 + pB1->Func1();
1.150 + pB2->Func2();
1.151 +
1.152 + // The handlers should always be called with the pointer to Derived
1.153 + CHECK_STATES((&g_States,
1.154 + new State_Func1H_Called(pD),
1.155 + new State_Func1_Called(pB1),
1.156 + new State_Func2H_Called(pD),
1.157 + new State_Func2_Called(pB2),
1.158 + new State_Func3H_Called(pD),
1.159 + new State_Func3_Called(pD),
1.160 + new State_Func1H_Called(pD),
1.161 + new State_Func1_Called(pB1),
1.162 + new State_Func2H_Called(pD),
1.163 + new State_Func2_Called(pB2),
1.164 + NULL), "Part 3");
1.165 +
1.166 + SH_REMOVE_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
1.167 + SH_REMOVE_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
1.168 + SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
1.169 +
1.170 + // 4)
1.171 + // Now add the hooks on Base1 and Base2 and check again
1.172 +
1.173 + // Note that the new implicit_cast should convert the pD to Base1*/Base2* :)
1.174 + SH_ADD_HOOK(Base1, Func1, pD, SH_STATIC(Handler_Func1), false);
1.175 + SH_ADD_HOOK(Base2, Func2, pD, SH_STATIC(Handler_Func2), false);
1.176 + SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
1.177 +
1.178 + pD->Func1();
1.179 + pD->Func2();
1.180 + pD->Func3();
1.181 + pB1->Func1();
1.182 + pB2->Func2();
1.183 +
1.184 + // This time, the handlers for Func1 should be called with pB1 and the handlers
1.185 + // for Func2 should be called with pB2
1.186 + CHECK_STATES((&g_States,
1.187 + new State_Func1H_Called(pB1),
1.188 + new State_Func1_Called(pB1),
1.189 + new State_Func2H_Called(pB2),
1.190 + new State_Func2_Called(pB2),
1.191 + new State_Func3H_Called(pD),
1.192 + new State_Func3_Called(pD),
1.193 + new State_Func1H_Called(pB1),
1.194 + new State_Func1_Called(pB1),
1.195 + new State_Func2H_Called(pB2),
1.196 + new State_Func2_Called(pB2),
1.197 + NULL), "Part 4");
1.198 +
1.199 + SH_REMOVE_HOOK(Base1, Func1, pD, SH_STATIC(Handler_Func1), false);
1.200 + SH_REMOVE_HOOK(Base2, Func2, pD, SH_STATIC(Handler_Func2), false);
1.201 + SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
1.202 +
1.203 +
1.204 + // 5)
1.205 + // Add some hooks, and use callclasses
1.206 +
1.207 + // 5.1) First off, add all of them on pD
1.208 + SH_ADD_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
1.209 + SH_ADD_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
1.210 + SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
1.211 +
1.212 + pD->Func1();
1.213 + pD->Func2();
1.214 + pD->Func3();
1.215 +
1.216 + CHECK_STATES((&g_States,
1.217 + new State_Func1H_Called(pD),
1.218 + new State_Func1_Called(pB1),
1.219 + new State_Func2H_Called(pD),
1.220 + new State_Func2_Called(pB2),
1.221 + new State_Func3H_Called(pD),
1.222 + new State_Func3_Called(pD),
1.223 + NULL), "Part 5.1");
1.224 +
1.225 + SH_CALL(pD_CC, &Derived::Func1)();
1.226 + SH_CALL(pD_CC, &Derived::Func2)();
1.227 + SH_CALL(pD_CC, &Derived::Func3)();
1.228 + SH_CALL(pB1_CC, &Base1::Func1)();
1.229 + SH_CALL(pB2_CC, &Base2::Func2)();
1.230 +
1.231 + CHECK_STATES((&g_States,
1.232 + new State_Func1_Called(pB1),
1.233 + new State_Func2_Called(pB2),
1.234 + new State_Func3_Called(pD),
1.235 + new State_Func1_Called(pB1),
1.236 + new State_Func2_Called(pB2),
1.237 + NULL), "Part 5.2");
1.238 +
1.239 + SH_REMOVE_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
1.240 + SH_REMOVE_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
1.241 + SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
1.242 +
1.243 + SH_RELEASE_CALLCLASS(pB1_CC);
1.244 + SH_RELEASE_CALLCLASS(pB2_CC);
1.245 + SH_RELEASE_CALLCLASS(pD_CC);
1.246 +
1.247 + return true;
1.248 +}