sourcehook/test/testhookmangen.cpp
author Pavol Marko <pavolmarko@gmail.com>
Sun Oct 21 21:55:17 2007 +0000 (2007-10-21)
branchhookman_autogen
changeset 433 a2d986ddd522
parent 432 f26dd3e3030b
child 435 35296642e147
permissions -rw-r--r--
Changed license in sourcehook_hookmangen_x86.h (agreed to by BAILOPAN and faluco)
Updated MSVC7 proj file
     1 #include <string>
     2 #include "sourcehook.h"
     3 #include "sourcehook_test.h"
     4 #include "testevents.h"
     5 #include "sourcehook_hookmangen.h"
     6 
     7 // TESTHOOKMANGEN
     8 // Test automatic hookman generation
     9 //  Tests void and non-void functions
    10 //  0 to 6 params:
    11 //   integer-type, float-type, plain-old-data struct and objects with ctors/dtors
    12 //   both byval and byref
    13 //  also tests ignore/supercede
    14 //  also tests recalls
    15 
    16 // :TODO: test override as well
    17 
    18 namespace
    19 {
    20 	#include "testhookmangen.h"
    21 
    22 	StateList g_States;
    23 	SourceHook::ISourceHook *g_SHPtr;
    24 	SourceHook::Plugin g_PLID;
    25 
    26 	// PtrBuf(ptr) gives ptrs unique numbers
    27 	// in the order they appear
    28 	SourceHook::List<const void*> g_PtrHash;
    29 
    30 	bool g_Inside_LeafFunc = false;			// inside a hook or a func
    31 
    32 	template <class T>
    33 	int PtrBuf(T ptr)
    34 	{
    35 		int a = 0;
    36 
    37 		const void *vptr = reinterpret_cast<const void*>(ptr);
    38 		for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end(); ++iter)
    39 		{
    40 			if (*iter == vptr)
    41 				return a;
    42 			else
    43 				++a;
    44 		}
    45 		g_PtrHash.push_back(vptr);
    46 		return static_cast<int>(g_PtrHash.size()) - 1;
    47 	}
    48 
    49 	template <class T>
    50 	T PtrBufPtr(T ptr)
    51 	{
    52 		PtrBuf(ptr);
    53 		return ptr;
    54 	}
    55 
    56 	void PtrBuf_Clear(int leave_in = 0)
    57 	{
    58 		for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end();)
    59 		{
    60 			if (--leave_in < 0)
    61 				iter = g_PtrHash.erase(iter);
    62 			else
    63 				++iter;
    64 		}
    65 	}
    66 
    67 	// POD / Object types
    68 	template <int MYSIZE>
    69 	struct POD
    70 	{
    71 		char x[MYSIZE];
    72 
    73 		bool operator==(const POD<MYSIZE> &other)
    74 		{
    75 			return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
    76 		}
    77 	};
    78 
    79 	template <int MYSIZE>
    80 	std::ostream& operator <<(std::ostream &os, const POD<MYSIZE> &obj)
    81 	{
    82 		os << "Some POD!";
    83 		return os;
    84 	}
    85 
    86 	MAKE_STATE_1(State_ObjOCtor_Called, int /*MYSIZE*/);
    87 	MAKE_STATE_1(State_ObjCCtor_Called, int /*MYSIZE*/);
    88 	MAKE_STATE_1(State_ObjODtor_Called, int /*MYSIZE*/);
    89 
    90 	template <int MYSIZE>
    91 	struct Object
    92 	{
    93 		char x[MYSIZE];
    94 
    95 		Object(char initch)
    96 		{
    97 			memset(reinterpret_cast<void*>(x), initch, MYSIZE);
    98 			if (!g_Inside_LeafFunc)
    99 				ADD_STATE(State_ObjOCtor_Called(MYSIZE));
   100 		}
   101 
   102 		Object()
   103 		{
   104 			if (!g_Inside_LeafFunc)
   105 				ADD_STATE(State_ObjOCtor_Called(MYSIZE));
   106 		}
   107 
   108 		Object(const Object<MYSIZE> & other)
   109 		{
   110 			memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
   111 			if (!g_Inside_LeafFunc)
   112 				ADD_STATE(State_ObjCCtor_Called(MYSIZE));
   113 		}
   114 
   115 		~Object()
   116 		{
   117 			if (!g_Inside_LeafFunc)
   118 				ADD_STATE(State_ObjODtor_Called(MYSIZE));
   119 		}
   120 
   121 		bool operator==(const Object<MYSIZE> &other)
   122 		{
   123 			return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
   124 		}
   125 	};
   126 
   127 	template <int MYSIZE>
   128 	std::ostream& operator <<(std::ostream &os, const Object<MYSIZE> &obj)
   129 	{
   130 		os << "Some Obj!";
   131 		return os;
   132 	}
   133 
   134 
   135 	// "Increment" something:  (reproducible change for recall tests)
   136 	//  integer: ++
   137 	//  float: += 1.3
   138 	//  pod/object: x[i]++, 0 <= i < MYSIZE
   139 	template <class T>
   140 	struct Increment
   141 	{
   142 		static void Incr(T &what)	
   143 		{
   144 			++what;
   145 		}
   146 	};
   147 
   148 	template<>
   149 	struct Increment<float>
   150 	{
   151 		static void Incr(float &what)	
   152 		{
   153 			what += 1.3f;
   154 		}
   155 	};
   156 
   157 	template<>
   158 	struct Increment<double>
   159 	{
   160 		static void Incr(double &what)	
   161 		{
   162 			what += 1.3;
   163 		}
   164 	};
   165 
   166 	template<int MYSIZE>
   167 	struct Increment< POD<MYSIZE> >
   168 	{
   169 		static void Incr(POD<MYSIZE> &what)	
   170 		{
   171 			for (int i = 0; i < MYSIZE; ++i)
   172 				++ what.x[i];
   173 		}
   174 	};
   175 
   176 	template<int MYSIZE>
   177 	struct Increment< Object<MYSIZE> >
   178 	{
   179 		static void Incr(Object<MYSIZE> &what)	
   180 		{
   181 			for (int i = 0; i < MYSIZE; ++i)
   182 				++ what.x[i];
   183 		}
   184 	};
   185 
   186 	// MyDelegate base class for other delegates
   187 	class MyDelegate : public SourceHook::ISHDelegate
   188 	{
   189 		// Unneeded
   190 		//  because we don't use old SH_REMOVE_HOOK syntax
   191 		virtual bool IsEqual(SourceHook::ISHDelegate *pOtherDeleg)
   192 		{
   193 			return false;
   194 		}
   195 
   196 		virtual void DeleteThis()
   197 		{
   198 			delete this;
   199 		}
   200 	};
   201 
   202 	THGM_MAKE_TEST0_void(0);
   203 	THGM_SETUP_PI0(0);
   204 
   205 	THGM_MAKE_TEST1_void(1, char);
   206 	THGM_SETUP_PI1(1, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   207 
   208 	THGM_MAKE_TEST1_void(2, short);
   209 	THGM_SETUP_PI1(2, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   210 
   211 	THGM_MAKE_TEST1_void(3, int);
   212 	THGM_SETUP_PI1(3, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   213 
   214 	THGM_MAKE_TEST1_void(4, float);
   215 	THGM_SETUP_PI1(4, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   216 
   217 	THGM_MAKE_TEST1_void(5, double);
   218 	THGM_SETUP_PI1(5, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   219 	
   220 	THGM_MAKE_TEST5_void(6, char, short, int, float, double);
   221 	THGM_SETUP_PI5(6,
   222 		char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   223 		short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   224 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   225 		float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal,
   226 		double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal
   227 		);
   228 
   229 	THGM_MAKE_TEST2_void(7, char&, double&);
   230 	THGM_SETUP_PI2(7,
   231 		char&, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByRef,
   232 		double&, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByRef
   233 		);
   234 
   235 	THGM_MAKE_TEST1_void(8, POD<7>);
   236 	THGM_SETUP_PI1(8, POD<7>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   237 
   238 	THGM_MAKE_TEST1_void(9, POD<600>);
   239 	THGM_SETUP_PI1(9, POD<600>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   240 
   241 	THGM_MAKE_TEST1_void(10, POD<600> &);
   242 	THGM_SETUP_PI1(10, POD<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef);
   243 
   244 	THGM_MAKE_TEST2_void(11, Object<3>, Object<600>&);
   245 	THGM_SETUP_PI2(11,
   246 		Object<3>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor,
   247 		Object<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor
   248 		);
   249 
   250 	THGM_MAKE_TEST0(101, char);
   251 	THGM_SETUP_PI0(101);
   252 	THGM_SETUP_RI(101, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   253 
   254 	THGM_MAKE_TEST0(102, short);
   255 	THGM_SETUP_PI0(102);
   256 	THGM_SETUP_RI(102, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   257 
   258 	THGM_MAKE_TEST0(103, int);
   259 	THGM_SETUP_PI0(103);
   260 	THGM_SETUP_RI(103, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   261 
   262 	THGM_MAKE_TEST0(104, float);
   263 	THGM_SETUP_PI0(104);
   264 	THGM_SETUP_RI(104, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   265 
   266 	THGM_MAKE_TEST0(105, double);
   267 	THGM_SETUP_PI0(105);
   268 	THGM_SETUP_RI(105, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   269 }
   270 
   271 
   272 bool TestHookManGen(std::string &error)
   273 {
   274 	GET_SHPTR(g_SHPtr);
   275 	g_PLID = 1337;
   276 
   277 	THGM_DO_TEST_void(0, ());
   278 
   279 	THGM_DO_TEST_void(1, (100));
   280 	
   281 	THGM_DO_TEST_void(2, (0x1F00));
   282 
   283 	THGM_DO_TEST_void(3, (0x1F000000));
   284 
   285 	THGM_DO_TEST_void(4, (0.5f));
   286 
   287 	THGM_DO_TEST_void(5, (5.5));
   288 
   289 	THGM_DO_TEST_void(6, (100, 0x1f00, 0x1f000000, 0.5f, 5.5));
   290 
   291 	char a = 5;
   292 	double b = 233.33;
   293 	THGM_DO_TEST_void(7, (a, b));
   294 
   295 	POD<7> pod7 = {78};
   296 	THGM_DO_TEST_void(8, (pod7));
   297 
   298 	POD<600> pod600 = {34};
   299 	THGM_DO_TEST_void(9, (pod600));
   300 	
   301 	THGM_DO_TEST_void(10, (pod600));
   302 
   303 	// Test11: Special: constructors/destructors
   304 	PtrBuf_Clear();
   305 	Object<3> obj3(33);
   306 	Object<600> obj600(21);
   307 
   308 	CHECK_STATES((&g_States,
   309 		new State_ObjOCtor_Called(3),
   310 		new State_ObjOCtor_Called(600),
   311 		NULL), "Test11 Part0");
   312 
   313 	setuppi_11();
   314 	g_Genc11 = new SourceHook::Impl::GenContext(&protoinfo_11, 0, 0, g_SHPtr);
   315 	g_Genc_ad11.set(g_Genc11);
   316 	SourceHook::HookManagerPubFunc myhookman11 = g_Genc11->Generate();
   317 	int hook1_11, hook2_11, hook3_11, hook4_11;
   318 	
   319 	TestClass11 *pTest11 = new TestClass11;
   320 	CAutoPtrDestruction<TestClass11> apd11(pTest11);
   321 	
   322 	/* no hooks - no hooks */
   323 	PtrBuf_Clear();
   324 	pTest11->Func(obj3, obj600);
   325 
   326 	g_Inside_LeafFunc = true;
   327 	CHECK_STATES((&g_States,
   328 		new State_ObjCCtor_Called(3),
   329 		new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
   330 		new State_ObjODtor_Called(3),
   331 		NULL), "Test" "11" " Part1");
   332 	g_Inside_LeafFunc = false;
   333 
   334 	/* hook1 - no hooks */
   335 	THGM_ADD_HOOK(11, 1);
   336 	
   337 	PtrBuf_Clear();
   338 	pTest11->Func(obj3, obj600);
   339 	g_Inside_LeafFunc = true;
   340 	CHECK_STATES((&g_States,
   341 		new State_ObjCCtor_Called(3),
   342 
   343 		new State_ObjCCtor_Called(3),
   344 		new State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
   345 		new State_ObjODtor_Called(3),
   346 
   347 		new State_ObjCCtor_Called(3),
   348 		new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
   349 		new State_ObjODtor_Called(3),
   350 
   351 		new State_ObjODtor_Called(3),
   352 		NULL), "Test" "11" " Part2");
   353 	g_Inside_LeafFunc = false;
   354 
   355 	THGM_REMOVE_HOOK(11, 1);
   356 
   357 	/* hook1, hook2 - hook3, hook4 */
   358 	THGM_ADD_HOOK(11, 1);
   359 	THGM_ADD_HOOK(11, 2);
   360 	THGM_ADD_HOOK(11, 3);
   361 	THGM_ADD_HOOK(11, 4);
   362 	PtrBuf_Clear();
   363 	pTest11->Func(obj3, obj600);
   364 	g_Inside_LeafFunc = true;
   365 	CHECK_STATES((&g_States,
   366 		new State_ObjCCtor_Called(3),
   367 
   368 		new State_ObjCCtor_Called(3),
   369 		new	State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
   370 		new State_ObjODtor_Called(3),
   371 
   372 		new State_ObjCCtor_Called(3),
   373 		new State_Deleg2_11(pTest11, 1, ParamState_m11 (obj3, obj600)),
   374 		new State_ObjODtor_Called(3),
   375 
   376 		new State_ObjCCtor_Called(3),
   377 		new State_Deleg3_11(pTest11, 2, ParamState_m11 (obj3, obj600)),
   378 		new State_ObjODtor_Called(3),
   379 
   380 		new State_ObjCCtor_Called(3),
   381 		new State_Deleg4_11(pTest11, 3, ParamState_m11 (obj3, obj600)),
   382 		new State_ObjODtor_Called(3),
   383 
   384 		new State_ObjODtor_Called(3),
   385 	NULL), "Test" "11" " Part3");
   386 	g_Inside_LeafFunc = false;
   387 
   388 	/* hook1 - hook3, hook4 WITH RECALLS */
   389 	THGM_REMOVE_HOOK(11, 2);
   390 	PtrBuf_Clear();
   391 	TestClass11::ms_DoRecall = true;
   392 	pTest11->Func(obj3, obj600);
   393 	g_Inside_LeafFunc = true;
   394 	CHECK_STATES((&g_States,
   395 		new State_ObjCCtor_Called(3),
   396 
   397 		new State_ObjCCtor_Called(3),
   398 		new	State_Deleg1_11(pTest11, 0 /* first deleg ptr */, ParamState_m11 (obj3, obj600)),
   399 
   400 		// recall !
   401 			// second hookfunc -> new copy
   402 			new State_ObjCCtor_Called(3),
   403 
   404 			// in second hookfunc now
   405 			//   -> calls orig func
   406 
   407 			new State_ObjCCtor_Called(3),
   408 			new State_Func11(pTest11, ParamState_m11 (obj3, obj600)(1)),
   409 			new State_ObjODtor_Called(3),
   410 
   411 			// calls first posthook
   412 			new State_ObjCCtor_Called(3),
   413 			new State_Deleg3_11(pTest11, 1 /* second deleg ptr */, ParamState_m11 (obj3, obj600)(1)),
   414 
   415 			// recall!
   416 				// third hookfunc -> new copy
   417 				new State_ObjCCtor_Called(3),
   418 
   419 				// calls second posthook
   420 
   421 				new State_ObjCCtor_Called(3),
   422 				new State_Deleg4_11(pTest11, 2 /* third deleg ptr */, ParamState_m11 (obj3, obj600)(2)),
   423 
   424 				// recall!
   425 					// fourth hookfunc -> new copy
   426 					new State_ObjCCtor_Called(3),
   427 
   428 					// has nothing to do though!
   429 
   430 					// fourth hookfunc done -> ret
   431 					new State_ObjODtor_Called(3),
   432 
   433 				// third hookfunc done -> ret
   434 				new State_ObjODtor_Called(3),
   435 				// ret from hookhandler which did the recall
   436 				new State_ObjODtor_Called(3),
   437 
   438 			// second hookfunc done -> ret
   439 			new State_ObjODtor_Called(3),
   440 			// ret from hookhandler which did the recall
   441 			new State_ObjODtor_Called(3),
   442 
   443 		// deleg1's instance
   444 		new State_ObjODtor_Called(3),
   445 		// first hookfunc done -> ret
   446 		new State_ObjODtor_Called(3),
   447 		NULL), "Test" "11" " Part4");
   448 	g_Inside_LeafFunc = false;
   449 
   450 	THGM_REMOVE_HOOK(11, 1);
   451 	THGM_REMOVE_HOOK(11, 3);
   452 	THGM_REMOVE_HOOK(11, 4);
   453 
   454 	THGM_DO_TEST(101, ());
   455 
   456 	THGM_DO_TEST(102, ());
   457 
   458 	THGM_DO_TEST(103, ());
   459 
   460 	THGM_DO_TEST(104, ());
   461 
   462 	THGM_DO_TEST(105, ());
   463 
   464 	// Shutdown now!
   465 	// If we don't SH will auto-shutdown _after_ genc's destructor is called
   466 	//  -> crash
   467 
   468 	Test_CompleteShutdown(g_SHPtr);
   469 	return true;
   470 }