1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcehook/test/test3.cpp Thu May 05 10:40:47 2005 +0000
1.3 @@ -0,0 +1,239 @@
1.4 +#include <string>
1.5 +#include "sourcehook_impl.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(reinterpret_cast<void*>(META_IFACEPTR)));
1.62 + }
1.63 + void Handler_Func2()
1.64 + {
1.65 + ADD_STATE(State_Func2H_Called(reinterpret_cast<void*>(META_IFACEPTR)));
1.66 + }
1.67 + void Handler_Func3()
1.68 + {
1.69 + ADD_STATE(State_Func3H_Called(reinterpret_cast<void*>(META_IFACEPTR)));
1.70 + }
1.71 +}
1.72 +
1.73 +bool TestThisPtrOffs(std::string &error)
1.74 +{
1.75 + SourceHook::CSourceHookImpl g_SHImpl;
1.76 + g_SHPtr = &g_SHImpl;
1.77 + g_PLID = 1337;
1.78 +
1.79 + Derived inst;
1.80 + Derived *pD = &inst;
1.81 + Base1 *pB1 = pD;
1.82 + Base2 *pB2 = pD;
1.83 +
1.84 + // It should be:
1.85 + // pB1 = pD
1.86 + // pB2 > pB1
1.87 +
1.88 + // 1)
1.89 + // Get a callclass for pD
1.90 + // Verify whether the this pointers are correct
1.91 + // Also call them normally to make sure that we aren't messing it up ;)
1.92 + SourceHook::CallClass<Derived> *pD_CC = SH_GET_CALLCLASS(pD);
1.93 +
1.94 + SH_CALL(pD_CC, &Derived::Func1)();
1.95 + SH_CALL(pD_CC, &Derived::Func2)();
1.96 + SH_CALL(pD_CC, &Derived::Func3)();
1.97 + pD->Func1();
1.98 + pD->Func2();
1.99 + pD->Func3();
1.100 +
1.101 + CHECK_STATES((&g_States,
1.102 + new State_Func1_Called(pB1),
1.103 + new State_Func2_Called(pB2),
1.104 + new State_Func3_Called(pD),
1.105 + new State_Func1_Called(pB1),
1.106 + new State_Func2_Called(pB2),
1.107 + new State_Func3_Called(pD),
1.108 + NULL), "Part 1");
1.109 +
1.110 + SH_CALL(pD_CC, &Base1::Func1)();
1.111 + SH_CALL(pD_CC, &Base2::Func2)();
1.112 +
1.113 + CHECK_STATES((&g_States,
1.114 + new State_Func1_Called(pB1),
1.115 + new State_Func2_Called(pB2),
1.116 + NULL), "Part 1.1");
1.117 +
1.118 + // 2)
1.119 + // Get callclasses for the other ones and verify it as well
1.120 +
1.121 + SourceHook::CallClass<Base1> *pB1_CC = SH_GET_CALLCLASS(pB1);
1.122 + SourceHook::CallClass<Base2> *pB2_CC = SH_GET_CALLCLASS(pB2);
1.123 +
1.124 + SH_CALL(pB1_CC, &Base1::Func1)();
1.125 + SH_CALL(pB2_CC, &Base2::Func2)();
1.126 +
1.127 + CHECK_STATES((&g_States,
1.128 + new State_Func1_Called(pB1),
1.129 + new State_Func2_Called(pB2),
1.130 + NULL), "Part 2");
1.131 +
1.132 +
1.133 + // 3) Add hooks on them (referring to them through pD1 / Derived)
1.134 + // Check whether the hooks are called with the correct this pointer
1.135 +
1.136 + SH_ADD_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
1.137 + SH_ADD_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
1.138 + SH_ADD_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
1.139 +
1.140 + pD->Func1();
1.141 + pD->Func2();
1.142 + pD->Func3();
1.143 + pB1->Func1();
1.144 + pB2->Func2();
1.145 +
1.146 + // The handlers should always be called with the pointer to Derived
1.147 + CHECK_STATES((&g_States,
1.148 + new State_Func1H_Called(pD),
1.149 + new State_Func1_Called(pB1),
1.150 + new State_Func2H_Called(pD),
1.151 + new State_Func2_Called(pB2),
1.152 + new State_Func3H_Called(pD),
1.153 + new State_Func3_Called(pD),
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 + NULL), "Part 3");
1.159 +
1.160 + SH_REMOVE_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
1.161 + SH_REMOVE_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
1.162 + SH_REMOVE_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
1.163 +
1.164 + // 4)
1.165 + // Now add the hooks on Base1 and Base2 and check again
1.166 +
1.167 + // Note that the new implicit_cast should convert the pD to Base1*/Base2* :)
1.168 + SH_ADD_HOOK_STATICFUNC(Base1, Func1, pD, Handler_Func1, false);
1.169 + SH_ADD_HOOK_STATICFUNC(Base2, Func2, pD, Handler_Func2, false);
1.170 + SH_ADD_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
1.171 +
1.172 + pD->Func1();
1.173 + pD->Func2();
1.174 + pD->Func3();
1.175 + pB1->Func1();
1.176 + pB2->Func2();
1.177 +
1.178 + // This time, the handlers for Func1 should be called with pB1 and the handlers
1.179 + // for Func2 should be called with pB2
1.180 + CHECK_STATES((&g_States,
1.181 + new State_Func1H_Called(pB1),
1.182 + new State_Func1_Called(pB1),
1.183 + new State_Func2H_Called(pB2),
1.184 + new State_Func2_Called(pB2),
1.185 + new State_Func3H_Called(pD),
1.186 + new State_Func3_Called(pD),
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 + NULL), "Part 4");
1.192 +
1.193 + SH_REMOVE_HOOK_STATICFUNC(Base1, Func1, pD, Handler_Func1, false);
1.194 + SH_REMOVE_HOOK_STATICFUNC(Base2, Func2, pD, Handler_Func2, false);
1.195 + SH_REMOVE_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
1.196 +
1.197 +
1.198 + // 5)
1.199 + // Add some hooks, and use callclasses
1.200 +
1.201 + // 5.1) First off, add all of them on pD
1.202 + SH_ADD_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
1.203 + SH_ADD_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
1.204 + SH_ADD_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
1.205 +
1.206 + pD->Func1();
1.207 + pD->Func2();
1.208 + pD->Func3();
1.209 +
1.210 + CHECK_STATES((&g_States,
1.211 + new State_Func1H_Called(pD),
1.212 + new State_Func1_Called(pB1),
1.213 + new State_Func2H_Called(pD),
1.214 + new State_Func2_Called(pB2),
1.215 + new State_Func3H_Called(pD),
1.216 + new State_Func3_Called(pD),
1.217 + NULL), "Part 5.1");
1.218 +
1.219 + SH_CALL(pD_CC, &Derived::Func1)();
1.220 + SH_CALL(pD_CC, &Derived::Func2)();
1.221 + SH_CALL(pD_CC, &Derived::Func3)();
1.222 + SH_CALL(pB1_CC, &Base1::Func1)();
1.223 + SH_CALL(pB2_CC, &Base2::Func2)();
1.224 +
1.225 + CHECK_STATES((&g_States,
1.226 + new State_Func1_Called(pB1),
1.227 + new State_Func2_Called(pB2),
1.228 + new State_Func3_Called(pD),
1.229 + new State_Func1_Called(pB1),
1.230 + new State_Func2_Called(pB2),
1.231 + NULL), "Part 5.2");
1.232 +
1.233 + SH_REMOVE_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
1.234 + SH_REMOVE_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
1.235 + SH_REMOVE_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
1.236 +
1.237 + SH_RELEASE_CALLCLASS(pB1_CC);
1.238 + SH_RELEASE_CALLCLASS(pB2_CC);
1.239 + SH_RELEASE_CALLCLASS(pD_CC);
1.240 +
1.241 + return true;
1.242 +}
1.243 \ No newline at end of file