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