sourcehook/test/test3.cpp
author Pavol Marko <pavolmarko@gmail.com>
Fri Dec 23 11:58:11 2005 +0000 (2005-12-23)
changeset 162 a95db73792e5
parent 133 ffe46578a1ae
child 294 0d87bc8606f8
permissions -rw-r--r--
One bugfix, addition of META_RETURN_(VALUE_)NEWPARAMS (implemented using recalls), tests now support compiling against an older version of sourcehook.h (through the sourcehook_test.h file) while main.cpp which now also acts as a CSourceHookImpl factory compiles against the new version.
     1 #include <string>
     2 #include "sourcehook_test.h"
     3 #include "testevents.h"
     4 
     5 // TEST3
     6 // Tests with inheritance / thisptroffsets
     7 
     8 namespace
     9 {
    10 	StateList g_States;
    11 	SourceHook::ISourceHook *g_SHPtr;
    12 	SourceHook::Plugin g_PLID;
    13 
    14 	MAKE_STATE_1(State_Func1_Called, void*);			// param1: This pointer
    15 	MAKE_STATE_1(State_Func2_Called, void*);			// param1: This pointer
    16 	MAKE_STATE_1(State_Func3_Called, void*);			// param1: This pointer
    17 
    18 	MAKE_STATE_1(State_Func1H_Called, void*);
    19 	MAKE_STATE_1(State_Func2H_Called, void*);
    20 	MAKE_STATE_1(State_Func3H_Called, void*);
    21 
    22 	class Base1
    23 	{
    24 		int a;
    25 	public:
    26 		virtual void Func1()
    27 		{
    28 			ADD_STATE(State_Func1_Called(reinterpret_cast<void*>(this)));
    29 		}
    30 	};
    31 	class Base2
    32 	{
    33 		int b;
    34 	public:
    35 		virtual void Func2()
    36 		{
    37 			ADD_STATE(State_Func2_Called(reinterpret_cast<void*>(this)));
    38 		}
    39 	};
    40 	class Derived : public Base1, public Base2
    41 	{
    42 		int c;
    43 	public:
    44 		virtual void Func3()
    45 		{
    46 			ADD_STATE(State_Func3_Called(reinterpret_cast<void*>(this)));
    47 		}
    48 	};
    49 
    50 	SH_DECL_HOOK0_void(Derived, Func1, SH_NOATTRIB, 0);
    51 	SH_DECL_HOOK0_void(Derived, Func2, SH_NOATTRIB, 0);
    52 	SH_DECL_HOOK0_void(Derived, Func3, SH_NOATTRIB, 0);
    53 	SH_DECL_HOOK0_void(Base1, Func1, SH_NOATTRIB, 0);
    54 	SH_DECL_HOOK0_void(Base2, Func2, SH_NOATTRIB, 0);
    55 
    56 	void Handler_Func1()
    57 	{
    58 		ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
    59 	}
    60 	void Handler_Func2()
    61 	{
    62 		ADD_STATE(State_Func2H_Called(META_IFACEPTR(void)));
    63 	}
    64 	void Handler_Func3()
    65 	{
    66 		ADD_STATE(State_Func3H_Called(META_IFACEPTR(void)));
    67 	}
    68 }
    69 
    70 bool TestThisPtrOffs(std::string &error)
    71 {
    72 	GET_SHPTR(g_SHPtr);
    73 	g_PLID = 1337;
    74 
    75 	Derived inst;
    76 	Derived *pD = &inst;
    77 	Base1 *pB1 = pD;
    78 	Base2 *pB2 = pD;
    79 
    80 	// It should be:
    81 	// pB1 = pD
    82 	// pB2 > pB1
    83 
    84 	// 1)
    85 	//  Get a callclass for pD
    86 	//  Verify whether the this pointers are correct
    87 	//  Also call them normally to make sure that we aren't messing it up ;)
    88 	SourceHook::CallClass<Derived> *pD_CC = SH_GET_CALLCLASS(pD);
    89 
    90 	SH_CALL(pD_CC, &Derived::Func1)();
    91 	SH_CALL(pD_CC, &Derived::Func2)();
    92 	SH_CALL(pD_CC, &Derived::Func3)();
    93 	pD->Func1();
    94 	pD->Func2();
    95 	pD->Func3();
    96 
    97 	CHECK_STATES((&g_States,
    98 		new State_Func1_Called(pB1),
    99 		new State_Func2_Called(pB2),
   100 		new State_Func3_Called(pD),
   101 		new State_Func1_Called(pB1),
   102 		new State_Func2_Called(pB2),
   103 		new State_Func3_Called(pD),
   104 		NULL), "Part 1");
   105 
   106 	SH_CALL(pD_CC, &Base1::Func1)();
   107 	SH_CALL(pD_CC, &Base2::Func2)();
   108 
   109 	CHECK_STATES((&g_States,
   110 		new State_Func1_Called(pB1),
   111 		new State_Func2_Called(pB2),
   112 		NULL), "Part 1.1");
   113 
   114 	// 2)
   115 	//   Get callclasses for the other ones and verify it as well
   116 
   117 	SourceHook::CallClass<Base1> *pB1_CC = SH_GET_CALLCLASS(pB1);
   118 	SourceHook::CallClass<Base2> *pB2_CC = SH_GET_CALLCLASS(pB2);
   119 
   120 	SH_CALL(pB1_CC, &Base1::Func1)();
   121 	SH_CALL(pB2_CC, &Base2::Func2)();
   122 
   123 	CHECK_STATES((&g_States,
   124 		new State_Func1_Called(pB1),
   125 		new State_Func2_Called(pB2),
   126 		NULL), "Part 2");
   127 
   128 
   129 	// 3) Add hooks on them (referring to them through pD1 / Derived)
   130 	//   Check whether the hooks are called with the correct this pointer
   131 
   132 	SH_ADD_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
   133 	SH_ADD_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
   134 	SH_ADD_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
   135 
   136 	pD->Func1();
   137 	pD->Func2();
   138 	pD->Func3();
   139 	pB1->Func1();
   140 	pB2->Func2();
   141 
   142 	// The handlers should always be called with the pointer to Derived
   143 	CHECK_STATES((&g_States,
   144 		new State_Func1H_Called(pD),
   145 		new State_Func1_Called(pB1),
   146 		new State_Func2H_Called(pD),
   147 		new State_Func2_Called(pB2),
   148 		new State_Func3H_Called(pD),
   149 		new State_Func3_Called(pD),
   150 		new State_Func1H_Called(pD),
   151 		new State_Func1_Called(pB1),
   152 		new State_Func2H_Called(pD),
   153 		new State_Func2_Called(pB2),
   154 		NULL), "Part 3");
   155 
   156 	SH_REMOVE_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
   157 	SH_REMOVE_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
   158 	SH_REMOVE_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
   159 
   160 	// 4)
   161 	//   Now add the hooks on Base1 and Base2 and check again
   162 
   163 	// Note that the new implicit_cast should convert the pD to Base1*/Base2* :)
   164 	SH_ADD_HOOK_STATICFUNC(Base1, Func1, pD, Handler_Func1, false);
   165 	SH_ADD_HOOK_STATICFUNC(Base2, Func2, pD, Handler_Func2, false);
   166 	SH_ADD_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
   167 
   168 	pD->Func1();
   169 	pD->Func2();
   170 	pD->Func3();
   171 	pB1->Func1();
   172 	pB2->Func2();
   173 
   174 	// This time, the handlers for Func1 should be called with pB1 and the handlers
   175 	// for Func2 should be called with pB2
   176 	CHECK_STATES((&g_States,
   177 		new State_Func1H_Called(pB1),
   178 		new State_Func1_Called(pB1),
   179 		new State_Func2H_Called(pB2),
   180 		new State_Func2_Called(pB2),
   181 		new State_Func3H_Called(pD),
   182 		new State_Func3_Called(pD),
   183 		new State_Func1H_Called(pB1),
   184 		new State_Func1_Called(pB1),
   185 		new State_Func2H_Called(pB2),
   186 		new State_Func2_Called(pB2),
   187 		NULL), "Part 4");
   188 
   189 	SH_REMOVE_HOOK_STATICFUNC(Base1, Func1, pD, Handler_Func1, false);
   190 	SH_REMOVE_HOOK_STATICFUNC(Base2, Func2, pD, Handler_Func2, false);
   191 	SH_REMOVE_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
   192 
   193 
   194 	// 5)
   195 	//   Add some hooks, and use callclasses
   196 
   197 	// 5.1) First off, add all of them on pD
   198 	SH_ADD_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
   199 	SH_ADD_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
   200 	SH_ADD_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
   201 
   202 	pD->Func1();
   203 	pD->Func2();
   204 	pD->Func3();
   205 
   206 	CHECK_STATES((&g_States,
   207 		new State_Func1H_Called(pD),
   208 		new State_Func1_Called(pB1),
   209 		new State_Func2H_Called(pD),
   210 		new State_Func2_Called(pB2),
   211 		new State_Func3H_Called(pD),
   212 		new State_Func3_Called(pD),
   213 		NULL), "Part 5.1");
   214 
   215 	SH_CALL(pD_CC, &Derived::Func1)();
   216 	SH_CALL(pD_CC, &Derived::Func2)();
   217 	SH_CALL(pD_CC, &Derived::Func3)();
   218 	SH_CALL(pB1_CC, &Base1::Func1)();
   219 	SH_CALL(pB2_CC, &Base2::Func2)();
   220 
   221 	CHECK_STATES((&g_States,
   222 		new State_Func1_Called(pB1),
   223 		new State_Func2_Called(pB2),
   224 		new State_Func3_Called(pD),
   225 		new State_Func1_Called(pB1),
   226 		new State_Func2_Called(pB2),
   227 		NULL), "Part 5.2");
   228 
   229 	SH_REMOVE_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
   230 	SH_REMOVE_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
   231 	SH_REMOVE_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
   232 
   233 	SH_RELEASE_CALLCLASS(pB1_CC);
   234 	SH_RELEASE_CALLCLASS(pB2_CC);
   235 	SH_RELEASE_CALLCLASS(pD_CC);
   236 
   237 	return true;
   238 }
   239