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