sourcehook/test/testmanual.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Nov 14 03:26:48 2008 -0600 (2008-11-14)
changeset 538 6ec9d491ec1e
parent 443 8889b02fd07e
permissions -rw-r--r--
Update makefile to allow building against Orange Box and Left 4 Dead
     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 		virtual const char *Func6()
    57 		{
    58 			return 0;
    59 		}
    60 		virtual char *Func7()
    61 		{
    62 			return 0;
    63 		}
    64 	};
    65 
    66 	SH_DECL_HOOK0_void(TheWall, Func1, SH_NOATTRIB, 0);
    67 	SH_DECL_HOOK1_void(TheWall, Func2, SH_NOATTRIB, 0, int);
    68 	SH_DECL_HOOK0(TheWall, Func3, SH_NOATTRIB, 0, short);
    69 	SH_DECL_HOOK1(TheWall, Func4, SH_NOATTRIB, 0, short, int);
    70 
    71 	SH_DECL_MANUALHOOK0_void(TheWall_Func1, 0, 0, 0);
    72 	SH_DECL_MANUALHOOK1_void(TheWall_Func2, 1, 0, 0, int);
    73 	SH_DECL_MANUALHOOK0(TheWall_Func3, 2, 0, 0, short);
    74 	SH_DECL_MANUALHOOK1(TheWall_Func4, 3, 0, 0, short, int);
    75 
    76 	SH_DECL_MANUALHOOK0(TheWall_Func6, 5, 0, 0, const char *);
    77 	SH_DECL_MANUALEXTERN0(TheWall_Func6, const char *);
    78 
    79 	SH_DECL_MANUALHOOK0(TheWall_Func7, 6, 0, 0, char *);
    80 	SH_DECL_MANUALEXTERN0(TheWall_Func7, char *);
    81 
    82 	SH_DECL_MANUALEXTERN0_void(TheWall_Func1);
    83 	SH_DECL_MANUALEXTERN1(TheWall_Func4, short, int);
    84 
    85 	typedef void (SourceHook::EmptyClass::*MFP_Func1)();
    86 	typedef void (SourceHook::EmptyClass::*MFP_Func2)(int);
    87 	typedef short (SourceHook::EmptyClass::*MFP_Func3)();
    88 	typedef short (SourceHook::EmptyClass::*MFP_Func4)(int);
    89 
    90 	void Handler_Func1()
    91 	{
    92 		ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
    93 	}
    94 	void Handler_Func2(int x)
    95 	{
    96 		ADD_STATE(State_Func2H_Called(META_IFACEPTR(void), x));
    97 		RETURN_META_MNEWPARAMS(MRES_IGNORED, TheWall_Func2, (0x87654321));
    98 	}
    99 	short Handler_Func3()
   100 	{
   101 		ADD_STATE(State_Func3H_Called(META_IFACEPTR(void)));
   102 		return 0;
   103 	}
   104 	short Handler_Func4(int x)
   105 	{
   106 		ADD_STATE(State_Func4H_Called(META_IFACEPTR(void), x));
   107 
   108 		// newparams (recall) using the manual hook declaration!
   109 		RETURN_META_VALUE_MNEWPARAMS(MRES_IGNORED, 0, TheWall_Func4, (0x12345678));
   110 	}
   111 
   112 	struct AnotherBrick
   113 	{
   114 		void Handler_Func1()
   115 		{
   116 			ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
   117 			RETURN_META(MRES_SUPERCEDE);
   118 		}
   119 	};
   120 
   121 	TheWall *MyInstanceFactory()
   122 	{
   123 		return new TheWall;
   124 	}
   125 }
   126 
   127 bool TestManual(std::string &error)
   128 {
   129 	GET_SHPTR(g_SHPtr);
   130 	g_PLID = 1337;
   131 
   132 	TheWall *p = MyInstanceFactory();
   133 	CAutoPtrDestruction<TheWall> apd(p);
   134 
   135 	SourceHook::ManualCallClass *cc = SH_GET_MCALLCLASS(p, sizeof(void*));
   136 
   137 	// 1)
   138 	// Call each function
   139 	p->Func1();
   140 	p->Func2(200);
   141 	ADD_STATE(State_Return(p->Func3()));
   142 	ADD_STATE(State_Return(p->Func4(400)));
   143 
   144 	CHECK_STATES((&g_States,
   145 		new State_Func1_Called(p),
   146 		new State_Func2_Called(p, 200),
   147 		new State_Func3_Called(p),
   148 		new State_Return(3),
   149 		new State_Func4_Called(p, 400),
   150 		new State_Return(4),
   151 		NULL), "Part 1");
   152 
   153 	// 1.1)
   154 	// Now call each function through the manual call class, using the hook decl and manually
   155 	SH_MCALL(cc, TheWall_Func1)();
   156 	SH_MCALL2(cc, MFP_Func1(), 0, 0, 0)();
   157 	SH_MCALL(cc, TheWall_Func2)(200);
   158 	SH_MCALL2(cc, MFP_Func2(), 1, 0, 0)(200);
   159 	ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func3)()));
   160 	ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func3(), 2, 0, 0)()));
   161 	ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func4)(400)));
   162 	ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func4(), 3, 0, 0)(400)));
   163 
   164 	CHECK_STATES((&g_States,
   165 		new State_Func1_Called(p),
   166 		new State_Func1_Called(p),
   167 		new State_Func2_Called(p, 200),
   168 		new State_Func2_Called(p, 200),
   169 		new State_Func3_Called(p),
   170 		new State_Return(3),
   171 		new State_Func3_Called(p),
   172 		new State_Return(3),
   173 		new State_Func4_Called(p, 400),
   174 		new State_Return(4),
   175 		new State_Func4_Called(p, 400),
   176 		new State_Return(4),
   177 		NULL), "Part 1.1");
   178 
   179 	// 2)
   180 	// Hook each function normally, call them
   181 	SH_ADD_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
   182 	SH_ADD_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
   183 	SH_ADD_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
   184 	SH_ADD_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
   185 
   186 	p->Func1();
   187 	p->Func2(200);
   188 	ADD_STATE(State_Return(p->Func3()));
   189 	ADD_STATE(State_Return(p->Func4(400)));
   190 
   191 	CHECK_STATES((&g_States,
   192 		new State_Func1H_Called(p),
   193 		new State_Func1_Called(p),
   194 		new State_Func2H_Called(p, 200),
   195 		new State_Func2_Called(p, 0x87654321),
   196 		new State_Func3H_Called(p),
   197 		new State_Func3_Called(p),
   198 		new State_Return(3),
   199 		new State_Func4H_Called(p, 400),
   200 		new State_Func4_Called(p, 0x12345678),
   201 		new State_Return(4),
   202 		NULL), "Part 2");
   203 
   204 	// Call them through the mcallclass
   205 	// 2.1)
   206 	// Now call each function through the manual call class, using the hook decl and manually
   207 	SH_MCALL(cc, TheWall_Func1)();
   208 	SH_MCALL2(cc, MFP_Func1(), 0, 0, 0)();
   209 	SH_MCALL(cc, TheWall_Func2)(200);
   210 	SH_MCALL2(cc, MFP_Func2(), 1, 0, 0)(200);
   211 	ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func3)()));
   212 	ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func3(), 2, 0, 0)()));
   213 	ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func4)(400)));
   214 	ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func4(), 3, 0, 0)(400)));
   215 
   216 	CHECK_STATES((&g_States,
   217 		new State_Func1_Called(p),
   218 		new State_Func1_Called(p),
   219 		new State_Func2_Called(p, 200),
   220 		new State_Func2_Called(p, 200),
   221 		new State_Func3_Called(p),
   222 		new State_Return(3),
   223 		new State_Func3_Called(p),
   224 		new State_Return(3),
   225 		new State_Func4_Called(p, 400),
   226 		new State_Return(4),
   227 		new State_Func4_Called(p, 400),
   228 		new State_Return(4),
   229 		NULL), "Part 2.1");
   230 
   231 	// Unhook them
   232 	SH_REMOVE_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
   233 	SH_REMOVE_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
   234 	SH_REMOVE_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
   235 	SH_REMOVE_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
   236 
   237 	// 3)
   238 	// Hook each function manually, call them
   239 
   240 	SH_ADD_MANUALHOOK(TheWall_Func1, p, SH_STATIC(Handler_Func1), false);
   241 	SH_ADD_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
   242 	SH_ADD_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
   243 	SH_ADD_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
   244 
   245 	p->Func1();
   246 	p->Func2(200);
   247 	ADD_STATE(State_Return(p->Func3()));
   248 	ADD_STATE(State_Return(p->Func4(400)));
   249 
   250 	CHECK_STATES((&g_States,
   251 		new State_Func1H_Called(p),
   252 		new State_Func1_Called(p),
   253 		new State_Func2H_Called(p, 200),
   254 		new State_Func2_Called(p, 0x87654321),
   255 		new State_Func3H_Called(p),
   256 		new State_Func3_Called(p),
   257 		new State_Return(3),
   258 		new State_Func4H_Called(p, 400),
   259 		new State_Func4_Called(p, 0x12345678),
   260 		new State_Return(4),
   261 		NULL), "Part 3");
   262 
   263 	// Now through the mcallclass
   264 
   265 	// Call them through the mcallclass
   266 	// 3.1)
   267 	// Now call each function through the manual call class, using the hook decl and manually
   268 	SH_MCALL(cc, TheWall_Func1)();
   269 	SH_MCALL2(cc, MFP_Func1(), 0, 0, 0)();
   270 	SH_MCALL(cc, TheWall_Func2)(200);
   271 	SH_MCALL2(cc, MFP_Func2(), 1, 0, 0)(200);
   272 	ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func3)()));
   273 	ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func3(), 2, 0, 0)()));
   274 	ADD_STATE(State_Return(SH_MCALL(cc, TheWall_Func4)(400)));
   275 	ADD_STATE(State_Return(SH_MCALL2(cc, MFP_Func4(), 3, 0, 0)(400)));
   276 
   277 	CHECK_STATES((&g_States,
   278 		new State_Func1_Called(p),
   279 		new State_Func1_Called(p),
   280 		new State_Func2_Called(p, 200),
   281 		new State_Func2_Called(p, 200),
   282 		new State_Func3_Called(p),
   283 		new State_Return(3),
   284 		new State_Func3_Called(p),
   285 		new State_Return(3),
   286 		new State_Func4_Called(p, 400),
   287 		new State_Return(4),
   288 		new State_Func4_Called(p, 400),
   289 		new State_Return(4),
   290 		NULL), "Part 3.1");
   291 
   292 	// Unhook them
   293 	SH_REMOVE_MANUALHOOK(TheWall_Func1, p, SH_STATIC(Handler_Func1), false);
   294 	SH_REMOVE_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
   295 	SH_REMOVE_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
   296 	SH_REMOVE_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
   297 
   298 	// 4)
   299 	// Hook each function manually, then normally, call, unhook
   300 
   301 	AnotherBrick handler_inst;
   302 
   303 	// Why this?
   304 	// 1) tests sh_add_manualhook
   305 	// 2) in my tests, the proto of the manual hook was not equal to the proto of the auto hook
   306 	//    (because there are no attribs for manual hooks).
   307 	//    sourcehook.cpp did a !strcmp(..), so it assigned a new hook manager even though there
   308 	//    already was one for this vfnptr. This hook manager stored the pointer of the original
   309 	//    hook manager's hookfunc as the orig pointer - everything seemingly worked.
   310 	//    The problem with this is that returning MRES_SUPERCEDE (as AnotherBrick::Handler_Func1
   311 	//    does) will supercede the second hook func from being called - thus bypassing the call
   312 	//    of the auto hook here.
   313 	SH_ADD_MANUALHOOK(TheWall_Func1, p, SH_MEMBER(&handler_inst, &AnotherBrick::Handler_Func1), false);
   314 	SH_ADD_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
   315 	SH_ADD_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
   316 	SH_ADD_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
   317 
   318 	SH_ADD_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
   319 	SH_ADD_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
   320 	SH_ADD_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
   321 	SH_ADD_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
   322 
   323 	p->Func1();
   324 	p->Func2(200);
   325 	ADD_STATE(State_Return(p->Func3()));
   326 	ADD_STATE(State_Return(p->Func4(400)));
   327 
   328 	CHECK_STATES((&g_States,
   329 		new State_Func1H_Called(p),
   330 		new State_Func1H_Called(p),
   331 		//new State_Func1_Called(p),
   332 		new State_Func2H_Called(p, 200),
   333 		new State_Func2H_Called(p, 0x87654321),
   334 		new State_Func2_Called(p, 0x87654321),
   335 		new State_Func3H_Called(p),
   336 		new State_Func3H_Called(p),
   337 		new State_Func3_Called(p),
   338 		new State_Return(3),
   339 		new State_Func4H_Called(p, 400),
   340 		new State_Func4H_Called(p, 0x12345678),
   341 		new State_Func4_Called(p, 0x12345678),
   342 		new State_Return(4),
   343 		NULL), "Part 4");
   344 
   345 	SH_REMOVE_MANUALHOOK(TheWall_Func1, p, SH_MEMBER(&handler_inst, &AnotherBrick::Handler_Func1), false);
   346 	SH_REMOVE_MANUALHOOK(TheWall_Func2, p, SH_STATIC(Handler_Func2), false);
   347 	SH_REMOVE_MANUALHOOK(TheWall_Func3, p, SH_STATIC(Handler_Func3), false);
   348 	SH_REMOVE_MANUALHOOK(TheWall_Func4, p, SH_STATIC(Handler_Func4), false);
   349 
   350 	SH_REMOVE_HOOK(TheWall, Func1, p, SH_STATIC(Handler_Func1), false);
   351 	SH_REMOVE_HOOK(TheWall, Func2, p, SH_STATIC(Handler_Func2), false);
   352 	SH_REMOVE_HOOK(TheWall, Func3, p, SH_STATIC(Handler_Func3), false);
   353 	SH_REMOVE_HOOK(TheWall, Func4, p, SH_STATIC(Handler_Func4), false);
   354 
   355 	// 5) Reconfigure TheWall_Func1 to actually hook Func5:
   356 	SH_MANUALHOOK_RECONFIGURE(TheWall_Func1, 4, 0, 0);
   357 	SH_MANUALHOOK_RECONFIGURE(TheWall_Func1, 4, 0, 0);
   358 	SH_ADD_MANUALHOOK(TheWall_Func1, p, SH_STATIC(Handler_Func1), false);
   359 
   360 	p->Func5();
   361 
   362 	SH_MCALL(p, TheWall_Func1)();
   363 
   364 	CHECK_STATES((&g_States,
   365 		new State_Func1H_Called(p),
   366 		new State_Func5_Called(p),
   367 		new State_Func5_Called(p),
   368 		NULL), "Part 5");
   369 
   370 	// 6) Test auto-remove on reconfig
   371 	SH_MANUALHOOK_RECONFIGURE(TheWall_Func1, 0, 0, 0);
   372 
   373 	p->Func1();
   374 	p->Func5();
   375 	
   376 	CHECK_STATES((&g_States,
   377 		new State_Func1_Called(p),
   378 		new State_Func5_Called(p),
   379 		NULL), "Part 6");
   380 
   381 	return true;
   382 }