sourcehook/test/testmanual.cpp
author Pavol Marko <pavolmarko@gmail.com>
Sun Oct 14 11:59:13 2007 +0000 (2007-10-14)
changeset 418 109309ff6350
parent 391 32e7aee084a6
child 421 573b9a092a5f
permissions -rw-r--r--
Removed "trust the prototype" model which could lead to problems. Instead, every CVfnPtr now has a list of hook managers.
-> Removed CHookManagerContainer
Also removed listcat (unnecessary since "V2" interface)
     1 #include <string>
     2 #include "sourcehook.h"
     3 #include "sourcehook_test.h"
     4 #include "testevents.h"
     5 
     6 // TESTMANUAL
     7 // Test manual hooks
     8 // :TODO: test more extensively
     9 
    10 namespace
    11 {
    12 	StateList g_States;
    13 	SourceHook::ISourceHook *g_SHPtr;
    14 	SourceHook::Plugin g_PLID;
    15 
    16 	MAKE_STATE_1(State_Func1_Called, void*);				// param1: This pointer
    17 	MAKE_STATE_2(State_Func2_Called, void*, int);			// param1: This pointer; param2: parameter
    18 	MAKE_STATE_1(State_Func3_Called, void*);				// param1: This pointer
    19 	MAKE_STATE_2(State_Func4_Called, void*, int);			// param1: This pointer; param2: parameter
    20 	MAKE_STATE_1(State_Func5_Called, void*);				// param1: This pointer
    21 
    22 	MAKE_STATE_1(State_Func1H_Called, void*);
    23 	MAKE_STATE_2(State_Func2H_Called, void*, int);
    24 	MAKE_STATE_1(State_Func3H_Called, void*);
    25 	MAKE_STATE_2(State_Func4H_Called, void*, int);
    26 
    27 	MAKE_STATE_1(State_Return, short);
    28 
    29 	class TheWall
    30 	{
    31 	public:
    32 		virtual void Func1()
    33 		{
    34 			ADD_STATE(State_Func1_Called(reinterpret_cast<void*>(this)));
    35 		}
    36 		virtual void Func2(int x)
    37 		{
    38 			ADD_STATE(State_Func2_Called(reinterpret_cast<void*>(this), x));
    39 		}
    40 
    41 		virtual short Func3()
    42 		{
    43 			ADD_STATE(State_Func3_Called(reinterpret_cast<void*>(this)));
    44 			return 3;
    45 		}
    46 		virtual short Func4(int x)
    47 		{
    48 			ADD_STATE(State_Func4_Called(reinterpret_cast<void*>(this), x));
    49 			return 4;
    50 		}
    51 		virtual void Func5()
    52 		{
    53 			ADD_STATE(State_Func5_Called(reinterpret_cast<void*>(this)));
    54 		}
    55 	};
    56 
    57 	SH_DECL_HOOK0_void(TheWall, Func1, SH_NOATTRIB, 0);
    58 	SH_DECL_HOOK1_void(TheWall, Func2, SH_NOATTRIB, 0, int);
    59 	SH_DECL_HOOK0(TheWall, Func3, SH_NOATTRIB, 0, short);
    60 	SH_DECL_HOOK1(TheWall, Func4, SH_NOATTRIB, 0, short, int);
    61 
    62 	SH_DECL_MANUALHOOK0_void(TheWall_Func1, 0, 0, 0);
    63 	SH_DECL_MANUALHOOK1_void(TheWall_Func2, 1, 0, 0, int);
    64 	SH_DECL_MANUALHOOK0(TheWall_Func3, 2, 0, 0, short);
    65 	SH_DECL_MANUALHOOK1(TheWall_Func4, 3, 0, 0, short, int);
    66 
    67 	typedef void (SourceHook::EmptyClass::*MFP_Func1)();
    68 	typedef void (SourceHook::EmptyClass::*MFP_Func2)(int);
    69 	typedef short (SourceHook::EmptyClass::*MFP_Func3)();
    70 	typedef short (SourceHook::EmptyClass::*MFP_Func4)(int);
    71 
    72 	void Handler_Func1()
    73 	{
    74 		ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
    75 	}
    76 	void Handler_Func2(int x)
    77 	{
    78 		ADD_STATE(State_Func2H_Called(META_IFACEPTR(void), x));
    79 		RETURN_META_MNEWPARAMS(MRES_IGNORED, TheWall_Func2, (0x87654321));
    80 	}
    81 	short Handler_Func3()
    82 	{
    83 		ADD_STATE(State_Func3H_Called(META_IFACEPTR(void)));
    84 		return 0;
    85 	}
    86 	short Handler_Func4(int x)
    87 	{
    88 		ADD_STATE(State_Func4H_Called(META_IFACEPTR(void), x));
    89 
    90 		// newparams (recall) using the manual hook declaration!
    91 		RETURN_META_VALUE_MNEWPARAMS(MRES_IGNORED, 0, TheWall_Func4, (0x12345678));
    92 	}
    93 
    94 	struct AnotherBrick
    95 	{
    96 		void Handler_Func1()
    97 		{
    98 			ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
    99 			RETURN_META(MRES_SUPERCEDE);
   100 		}
   101 	};
   102 
   103 	TheWall *MyInstanceFactory()
   104 	{
   105 		return new TheWall;
   106 	}
   107 }
   108 
   109 bool TestManual(std::string &error)
   110 {
   111 	GET_SHPTR(g_SHPtr);
   112 	g_PLID = 1337;
   113 
   114 	TheWall *p = MyInstanceFactory();
   115 	CAutoPtrDestruction<TheWall> apd(p);
   116 
   117 	SourceHook::ManualCallClass *cc = SH_GET_MCALLCLASS(p, sizeof(void*));
   118 
   119 	// 1)
   120 	// Call each function
   121 	p->Func1();
   122 	p->Func2(200);
   123 	ADD_STATE(State_Return(p->Func3()));
   124 	ADD_STATE(State_Return(p->Func4(400)));
   125 
   126 	CHECK_STATES((&g_States,
   127 		new State_Func1_Called(p),
   128 		new State_Func2_Called(p, 200),
   129 		new State_Func3_Called(p),
   130 		new State_Return(3),
   131 		new State_Func4_Called(p, 400),
   132 		new State_Return(4),
   133 		NULL), "Part 1");
   134 
   135 	// 1.1)
   136 	// Now call each function through the manual call class, using the hook decl and manually
   137 	SH_MCALL(cc, TheWall_Func1)();
   138 	SH_MCALL2(cc, MFP_Func1(), 0, 0, 0)();
   139 	SH_MCALL(cc, TheWall_Func2)(200);
   140 	SH_MCALL2(cc, MFP_Func2(), 1, 0, 0)(200);
   141 	ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func3)()));
   142 	ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func3(), 2, 0, 0)()));
   143 	ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func4)(400)));
   144 	ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func4(), 3, 0, 0)(400)));
   145 
   146 	CHECK_STATES((&g_States,
   147 		new State_Func1_Called(p),
   148 		new State_Func1_Called(p),
   149 		new State_Func2_Called(p, 200),
   150 		new State_Func2_Called(p, 200),
   151 		new State_Func3_Called(p),
   152 		new State_Return(3),
   153 		new State_Func3_Called(p),
   154 		new State_Return(3),
   155 		new State_Func4_Called(p, 400),
   156 		new State_Return(4),
   157 		new State_Func4_Called(p, 400),
   158 		new State_Return(4),
   159 		NULL), "Part 1.1");
   160 
   161 	// 2)
   162 	// Hook each function normally, call them
   163 	SH_ADD_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
   164 	SH_ADD_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
   165 	SH_ADD_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
   166 	SH_ADD_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
   167 
   168 	p->Func1();
   169 	p->Func2(200);
   170 	ADD_STATE(State_Return(p->Func3()));
   171 	ADD_STATE(State_Return(p->Func4(400)));
   172 
   173 	CHECK_STATES((&g_States,
   174 		new State_Func1H_Called(p),
   175 		new State_Func1_Called(p),
   176 		new State_Func2H_Called(p, 200),
   177 		new State_Func2_Called(p, 0x87654321),
   178 		new State_Func3H_Called(p),
   179 		new State_Func3_Called(p),
   180 		new State_Return(3),
   181 		new State_Func4H_Called(p, 400),
   182 		new State_Func4_Called(p, 0x12345678),
   183 		new State_Return(4),
   184 		NULL), "Part 2");
   185 
   186 	// Call them through the mcallclass
   187 	// 2.1)
   188 	// Now call each function through the manual call class, using the hook decl and manually
   189 	SH_MCALL(cc, TheWall_Func1)();
   190 	SH_MCALL2(cc, MFP_Func1(), 0, 0, 0)();
   191 	SH_MCALL(cc, TheWall_Func2)(200);
   192 	SH_MCALL2(cc, MFP_Func2(), 1, 0, 0)(200);
   193 	ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func3)()));
   194 	ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func3(), 2, 0, 0)()));
   195 	ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func4)(400)));
   196 	ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func4(), 3, 0, 0)(400)));
   197 
   198 	CHECK_STATES((&g_States,
   199 		new State_Func1_Called(p),
   200 		new State_Func1_Called(p),
   201 		new State_Func2_Called(p, 200),
   202 		new State_Func2_Called(p, 200),
   203 		new State_Func3_Called(p),
   204 		new State_Return(3),
   205 		new State_Func3_Called(p),
   206 		new State_Return(3),
   207 		new State_Func4_Called(p, 400),
   208 		new State_Return(4),
   209 		new State_Func4_Called(p, 400),
   210 		new State_Return(4),
   211 		NULL), "Part 2.1");
   212 
   213 	// Unhook them
   214 	SH_REMOVE_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
   215 	SH_REMOVE_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
   216 	SH_REMOVE_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
   217 	SH_REMOVE_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
   218 
   219 	// 3)
   220 	// Hook each function manually, call them
   221 
   222 	SH_ADD_MANUALHOOK(TheWall_Func1, p, SH_STATIC(Handler_Func1), false);
   223 	SH_ADD_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
   224 	SH_ADD_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
   225 	SH_ADD_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
   226 
   227 	p->Func1();
   228 	p->Func2(200);
   229 	ADD_STATE(State_Return(p->Func3()));
   230 	ADD_STATE(State_Return(p->Func4(400)));
   231 
   232 	CHECK_STATES((&g_States,
   233 		new State_Func1H_Called(p),
   234 		new State_Func1_Called(p),
   235 		new State_Func2H_Called(p, 200),
   236 		new State_Func2_Called(p, 0x87654321),
   237 		new State_Func3H_Called(p),
   238 		new State_Func3_Called(p),
   239 		new State_Return(3),
   240 		new State_Func4H_Called(p, 400),
   241 		new State_Func4_Called(p, 0x12345678),
   242 		new State_Return(4),
   243 		NULL), "Part 3");
   244 
   245 	// Now through the mcallclass
   246 
   247 	// Call them through the mcallclass
   248 	// 3.1)
   249 	// Now call each function through the manual call class, using the hook decl and manually
   250 	SH_MCALL(cc, TheWall_Func1)();
   251 	SH_MCALL2(cc, MFP_Func1(), 0, 0, 0)();
   252 	SH_MCALL(cc, TheWall_Func2)(200);
   253 	SH_MCALL2(cc, MFP_Func2(), 1, 0, 0)(200);
   254 	ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func3)()));
   255 	ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func3(), 2, 0, 0)()));
   256 	ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func4)(400)));
   257 	ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func4(), 3, 0, 0)(400)));
   258 
   259 	CHECK_STATES((&g_States,
   260 		new State_Func1_Called(p),
   261 		new State_Func1_Called(p),
   262 		new State_Func2_Called(p, 200),
   263 		new State_Func2_Called(p, 200),
   264 		new State_Func3_Called(p),
   265 		new State_Return(3),
   266 		new State_Func3_Called(p),
   267 		new State_Return(3),
   268 		new State_Func4_Called(p, 400),
   269 		new State_Return(4),
   270 		new State_Func4_Called(p, 400),
   271 		new State_Return(4),
   272 		NULL), "Part 3.1");
   273 
   274 	// Unhook them
   275 	SH_REMOVE_MANUALHOOK(TheWall_Func1, p, SH_STATIC(Handler_Func1), false);
   276 	SH_REMOVE_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
   277 	SH_REMOVE_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
   278 	SH_REMOVE_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
   279 
   280 	// 4)
   281 	// Hook each function manually, then normally, call, unhook
   282 
   283 	AnotherBrick handler_inst;
   284 
   285 	// Why this?
   286 	// 1) tests sh_add_manualhook
   287 	// 2) in my tests, the proto of the manual hook was not equal to the proto of the auto hook
   288 	//    (because there are no attribs for manual hooks).
   289 	//    sourcehook.cpp did a !strcmp(..), so it assigned a new hook manager even though there
   290 	//    already was one for this vfnptr. This hook manager stored the pointer of the original
   291 	//    hook manager's hookfunc as the orig pointer - everything seemingly worked.
   292 	//    The problem with this is that returning MRES_SUPERCEDE (as AnotherBrick::Handler_Func1
   293 	//    does) will supercede the second hook func from being called - thus bypassing the call
   294 	//    of the auto hook here.
   295 	SH_ADD_MANUALHOOK(TheWall_Func1, p, SH_MEMBER(&handler_inst, &AnotherBrick::Handler_Func1), false);
   296 	SH_ADD_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
   297 	SH_ADD_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
   298 	SH_ADD_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
   299 
   300 	SH_ADD_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
   301 	SH_ADD_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
   302 	SH_ADD_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
   303 	SH_ADD_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
   304 
   305 	p->Func1();
   306 	p->Func2(200);
   307 	ADD_STATE(State_Return(p->Func3()));
   308 	ADD_STATE(State_Return(p->Func4(400)));
   309 
   310 	CHECK_STATES((&g_States,
   311 		new State_Func1H_Called(p),
   312 		new State_Func1H_Called(p),
   313 		//new State_Func1_Called(p),
   314 		new State_Func2H_Called(p, 200),
   315 		new State_Func2H_Called(p, 0x87654321),
   316 		new State_Func2_Called(p, 0x87654321),
   317 		new State_Func3H_Called(p),
   318 		new State_Func3H_Called(p),
   319 		new State_Func3_Called(p),
   320 		new State_Return(3),
   321 		new State_Func4H_Called(p, 400),
   322 		new State_Func4H_Called(p, 0x12345678),
   323 		new State_Func4_Called(p, 0x12345678),
   324 		new State_Return(4),
   325 		NULL), "Part 4");
   326 
   327 	SH_REMOVE_MANUALHOOK(TheWall_Func1, p, SH_MEMBER(&handler_inst, &AnotherBrick::Handler_Func1), false);
   328 	SH_REMOVE_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
   329 	SH_REMOVE_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
   330 	SH_REMOVE_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
   331 
   332 	SH_REMOVE_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
   333 	SH_REMOVE_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
   334 	SH_REMOVE_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
   335 	SH_REMOVE_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
   336 
   337 	// 5) Reconfigure TheWall_Func1 to actually hook Func5:
   338 	SH_MANUALHOOK_RECONFIGURE(TheWall_Func1, 4, 0, 0);
   339 	SH_MANUALHOOK_RECONFIGURE(TheWall_Func1, 4, 0, 0);
   340 	SH_ADD_MANUALHOOK(TheWall_Func1, p, SH_STATIC(Handler_Func1), false);
   341 
   342 	p->Func5();
   343 
   344 	CHECK_STATES((&g_States,
   345 		new State_Func1H_Called(p),
   346 		new State_Func5_Called(p),
   347 		NULL), "Part 5");
   348 
   349 	// 6) Test auto-remove on reconfig
   350 	SH_MANUALHOOK_RECONFIGURE(TheWall_Func1, 0, 0, 0);
   351 
   352 	p->Func1();
   353 	p->Func5();
   354 	
   355 	CHECK_STATES((&g_States,
   356 		new State_Func1_Called(p),
   357 		new State_Func5_Called(p),
   358 		NULL), "Part 6");
   359 
   360 	return true;
   361 }