sourcehook/test/testvphooks.cpp
branchsourcemm-1.6.0
changeset 362 bf93a1c52b0f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sourcehook/test/testvphooks.cpp	Fri Sep 28 21:41:41 2007 +0000
     1.3 @@ -0,0 +1,347 @@
     1.4 +#include <string>
     1.5 +#include "sourcehook.h"
     1.6 +#include "sourcehook_test.h"
     1.7 +#include "testevents.h"
     1.8 +
     1.9 +// TEST VP HOOKS
    1.10 +// Test vfnptr-wide hooks
    1.11 +// Also contains a test for removing hooks on deleted instances (by id)
    1.12 +
    1.13 +namespace
    1.14 +{
    1.15 +	StateList g_States;
    1.16 +	SourceHook::ISourceHook *g_SHPtr;
    1.17 +	SourceHook::Plugin g_PLID;
    1.18 +
    1.19 +	class IBase;
    1.20 +
    1.21 +	MAKE_STATE_1(State_D1_Func1, IBase *);
    1.22 +	MAKE_STATE_1(State_D2_Func1, IBase *);
    1.23 +	MAKE_STATE_1(State_Func1_Pre, IBase *);
    1.24 +	MAKE_STATE_1(State_Func1_Post, IBase *);
    1.25 +
    1.26 +	MAKE_STATE_1(State_D1_Func2, IBase *);
    1.27 +	MAKE_STATE_1(State_D2_Func2, IBase *);
    1.28 +	MAKE_STATE_1(State_Func2_Pre, IBase *);
    1.29 +	MAKE_STATE_1(State_Func2_Post, IBase *);
    1.30 +
    1.31 +	MAKE_STATE_2(State_D1_Func3, IBase *, int);
    1.32 +	MAKE_STATE_2(State_D2_Func3, IBase *, int);
    1.33 +	MAKE_STATE_2(State_Func3_Pre, IBase *, int);
    1.34 +	MAKE_STATE_2(State_Func3_Post, IBase *, int);
    1.35 +
    1.36 +	class IBase
    1.37 +	{
    1.38 +	public:
    1.39 +		virtual void Func1() = 0;
    1.40 +		virtual void Func2() = 0;
    1.41 +		virtual void Func3(int x) = 0;
    1.42 +	};
    1.43 +
    1.44 +	class CDerived1 : public IBase
    1.45 +	{
    1.46 +	public:
    1.47 +		virtual void Func1()
    1.48 +		{
    1.49 +			ADD_STATE(State_D1_Func1(this));
    1.50 +		}
    1.51 +		virtual void Func2()
    1.52 +		{
    1.53 +			ADD_STATE(State_D1_Func2(this));
    1.54 +		}
    1.55 +		virtual void Func3(int x)
    1.56 +		{
    1.57 +			ADD_STATE(State_D1_Func3(this, x));
    1.58 +		}
    1.59 +	};
    1.60 +
    1.61 +	class CDerived2 : public IBase
    1.62 +	{
    1.63 +	public:
    1.64 +		virtual void Func1()
    1.65 +		{
    1.66 +			ADD_STATE(State_D2_Func1(this));
    1.67 +		}
    1.68 +		virtual void Func2()
    1.69 +		{
    1.70 +			ADD_STATE(State_D2_Func2(this));
    1.71 +		}
    1.72 +		virtual void Func3(int x)
    1.73 +		{
    1.74 +			ADD_STATE(State_D2_Func3(this, x));
    1.75 +		}
    1.76 +	};
    1.77 +
    1.78 +	void Handler_Func1_Pre()
    1.79 +	{
    1.80 +		ADD_STATE(State_Func1_Pre(META_IFACEPTR(IBase)));
    1.81 +	}
    1.82 +	void Handler_Func1_Post()
    1.83 +	{
    1.84 +		ADD_STATE(State_Func1_Post(META_IFACEPTR(IBase)));
    1.85 +	}
    1.86 +	int g_F2_Pre_HookToRemove = 0;
    1.87 +	void Handler_Func2_Pre()
    1.88 +	{
    1.89 +		ADD_STATE(State_Func2_Pre(META_IFACEPTR(IBase)));
    1.90 +		SH_REMOVE_HOOK_ID(g_F2_Pre_HookToRemove);
    1.91 +	}
    1.92 +	void Handler_Func2_Post()
    1.93 +	{
    1.94 +		ADD_STATE(State_Func2_Post(META_IFACEPTR(IBase)));
    1.95 +	}
    1.96 +
    1.97 +
    1.98 +	void Handler_Func3_Pre(int x)
    1.99 +	{
   1.100 +		ADD_STATE(State_Func3_Pre(META_IFACEPTR(IBase), x));
   1.101 +
   1.102 +		RETURN_META_NEWPARAMS(MRES_IGNORED, &IBase::Func3, (x+1));
   1.103 +	}
   1.104 +	void Handler_Func3_Post(int x)
   1.105 +	{
   1.106 +		ADD_STATE(State_Func3_Post(META_IFACEPTR(IBase), x));
   1.107 +	}
   1.108 +
   1.109 +	SH_DECL_HOOK0_void(IBase, Func1, SH_NOATTRIB, 0);
   1.110 +	SH_DECL_HOOK0_void(IBase, Func2, SH_NOATTRIB, 0);
   1.111 +	SH_DECL_HOOK1_void(IBase, Func3, SH_NOATTRIB, 0, int);
   1.112 +
   1.113 +	SH_DECL_MANUALHOOK1_void(IBase_Func3_Manual, 2, 0, 0, int);
   1.114 +}
   1.115 +
   1.116 +bool TestVPHooks(std::string &error)
   1.117 +{
   1.118 +	GET_SHPTR(g_SHPtr);
   1.119 +	g_PLID = 1337;
   1.120 +
   1.121 +	CDerived1 d1i1;
   1.122 +	CDerived1 d1i2;
   1.123 +	CDerived2 d2i1;
   1.124 +
   1.125 +	IBase *p_d1i1 = &d1i1;
   1.126 +	IBase *p_d1i2 = &d1i2;
   1.127 +	IBase *p_d2i1 = &d2i1;
   1.128 +
   1.129 +	int hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
   1.130 +
   1.131 +	p_d1i1->Func1();
   1.132 +	p_d1i2->Func1();
   1.133 +	p_d2i1->Func1();
   1.134 +
   1.135 +	CHECK_STATES((&g_States,
   1.136 +		new State_Func1_Pre(p_d1i1),
   1.137 +		new State_D1_Func1(p_d1i1),
   1.138 +		
   1.139 +		new State_Func1_Pre(p_d1i2),
   1.140 +		new State_D1_Func1(p_d1i2),
   1.141 +
   1.142 +		new State_D2_Func1(p_d2i1),
   1.143 +		NULL), "Part 1");
   1.144 +
   1.145 +	SH_CALL(p_d1i1, &IBase::Func1)();
   1.146 +	SH_CALL(p_d1i2, &IBase::Func1)();
   1.147 +	SH_CALL(p_d2i1, &IBase::Func1)();
   1.148 +
   1.149 +	CHECK_STATES((&g_States,
   1.150 +		new State_D1_Func1(p_d1i1),
   1.151 +
   1.152 +		new State_D1_Func1(p_d1i2),
   1.153 +
   1.154 +		new State_D2_Func1(p_d2i1),
   1.155 +		NULL), "Part 1.1");
   1.156 +
   1.157 +	SH_REMOVE_HOOK_ID(hook1);
   1.158 +
   1.159 +	p_d1i1->Func1();
   1.160 +	p_d1i2->Func1();
   1.161 +	p_d2i1->Func1();
   1.162 +
   1.163 +	CHECK_STATES((&g_States,
   1.164 +		new State_D1_Func1(p_d1i1),
   1.165 +
   1.166 +		new State_D1_Func1(p_d1i2),
   1.167 +
   1.168 +		new State_D2_Func1(p_d2i1),
   1.169 +		NULL), "Part 2");
   1.170 +
   1.171 +
   1.172 +
   1.173 +	// Normal hook, then vp hook
   1.174 +
   1.175 +	int hook2 = SH_ADD_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
   1.176 +	hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
   1.177 +
   1.178 +	p_d1i1->Func1();
   1.179 +	p_d1i2->Func1();
   1.180 +	p_d2i1->Func1();
   1.181 +
   1.182 +	CHECK_STATES((&g_States,
   1.183 +		new State_Func1_Pre(p_d1i1),
   1.184 +		new State_Func1_Pre(p_d1i1),
   1.185 +		new State_D1_Func1(p_d1i1),
   1.186 +
   1.187 +		new State_Func1_Pre(p_d1i2),
   1.188 +		new State_D1_Func1(p_d1i2),
   1.189 +
   1.190 +		new State_D2_Func1(p_d2i1),
   1.191 +		NULL), "Part 3");
   1.192 +
   1.193 +	SH_REMOVE_HOOK_ID(hook1);
   1.194 +
   1.195 +	p_d1i1->Func1();
   1.196 +	p_d1i2->Func1();
   1.197 +	p_d2i1->Func1();
   1.198 +
   1.199 +	CHECK_STATES((&g_States,
   1.200 +		new State_Func1_Pre(p_d1i1),
   1.201 +		new State_D1_Func1(p_d1i1),
   1.202 +
   1.203 +		new State_D1_Func1(p_d1i2),
   1.204 +
   1.205 +		new State_D2_Func1(p_d2i1),
   1.206 +		NULL), "Part 4");
   1.207 +
   1.208 +	SH_REMOVE_HOOK_ID(hook2);
   1.209 +
   1.210 +	p_d1i1->Func1();
   1.211 +	p_d1i2->Func1();
   1.212 +	p_d2i1->Func1();
   1.213 +
   1.214 +	CHECK_STATES((&g_States,
   1.215 +		new State_D1_Func1(p_d1i1),
   1.216 +
   1.217 +		new State_D1_Func1(p_d1i2),
   1.218 +
   1.219 +		new State_D2_Func1(p_d2i1),
   1.220 +		NULL), "Part 5");
   1.221 +
   1.222 +	// Test this:
   1.223 +	// Normal hook AND vp hook on Func2
   1.224 +	// Func2's pre handler removes the VP hook. It has to work anyway :)
   1.225 +
   1.226 +	hook1 = SH_ADD_VPHOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
   1.227 +	hook2 = SH_ADD_HOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
   1.228 +
   1.229 +	g_F2_Pre_HookToRemove = hook1;
   1.230 +	p_d1i1->Func2();
   1.231 +	p_d1i1->Func2();
   1.232 +
   1.233 +	CHECK_STATES((&g_States,
   1.234 +		new State_Func2_Pre(p_d1i1),
   1.235 +		new State_Func2_Pre(p_d1i1),
   1.236 +		new State_D1_Func2(p_d1i1),
   1.237 +
   1.238 +		new State_Func2_Pre(p_d1i1),
   1.239 +		new State_D1_Func2(p_d1i1),
   1.240 +
   1.241 +		NULL), "Part 6");
   1.242 +
   1.243 +	SH_REMOVE_HOOK_ID(hook1);
   1.244 +
   1.245 +	// Hook function 3:
   1.246 +	//  Using manualhook, VP
   1.247 +	hook1 = SH_ADD_MANUALVPHOOK(IBase_Func3_Manual, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
   1.248 +
   1.249 +	//  Normally, VP
   1.250 +	hook2 = SH_ADD_VPHOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
   1.251 +
   1.252 +	// Normally, no VP
   1.253 +	int hook3 = SH_ADD_HOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
   1.254 +
   1.255 +	p_d1i1->Func3(1);
   1.256 +
   1.257 +	CHECK_STATES((&g_States,
   1.258 +		new State_Func3_Pre(p_d1i1, 1),		// manual vp hook
   1.259 +		new State_Func3_Pre(p_d1i1, 2),		// normal vp hook
   1.260 +		new State_Func3_Pre(p_d1i1, 3),		// normal non-vp hook
   1.261 +
   1.262 +		new State_D1_Func3(p_d1i1, 4),		// function
   1.263 +
   1.264 +		NULL), "Part 7.1");
   1.265 +
   1.266 +	p_d1i2->Func3(1);
   1.267 +
   1.268 +	CHECK_STATES((&g_States,
   1.269 +		new State_Func3_Pre(p_d1i2, 1),		// manual vp hook
   1.270 +		new State_Func3_Pre(p_d1i2, 2),		// normal vp hook
   1.271 +
   1.272 +		new State_D1_Func3(p_d1i2, 3),		// function
   1.273 +
   1.274 +		NULL), "Part 7.2");
   1.275 +
   1.276 +	SH_REMOVE_HOOK_ID(hook1);
   1.277 +	SH_REMOVE_HOOK_ID(hook2);
   1.278 +	SH_REMOVE_HOOK_ID(hook3);
   1.279 +
   1.280 +	// Test direct VP hook
   1.281 +
   1.282 +	p_d1i1->Func1();
   1.283 +	p_d1i2->Func1();
   1.284 +
   1.285 +	// get vtable manually
   1.286 +	void *pVtable = *reinterpret_cast<void**>(p_d1i1);
   1.287 +
   1.288 +	hook1 = SH_ADD_DVPHOOK(IBase, Func1, pVtable, SH_STATIC(Handler_Func1_Pre), false);
   1.289 +
   1.290 +	p_d1i1->Func1();
   1.291 +	p_d1i2->Func1();
   1.292 +
   1.293 +	CHECK_STATES((&g_States,
   1.294 +		new State_D1_Func1(p_d1i1),
   1.295 +		new State_D1_Func1(p_d1i2),
   1.296 +
   1.297 +		new State_Func1_Pre(p_d1i1),
   1.298 +		new State_D1_Func1(p_d1i1),
   1.299 +		new State_Func1_Pre(p_d1i2),
   1.300 +		new State_D1_Func1(p_d1i2),
   1.301 +		NULL), "Part 8.1");
   1.302 +
   1.303 +	SH_REMOVE_HOOK_ID(hook1);
   1.304 +
   1.305 +	// Now try manual dvp hooks
   1.306 +	p_d1i1->Func3(1);
   1.307 +	p_d1i2->Func3(1);
   1.308 +	
   1.309 +	hook1 = SH_ADD_MANUALDVPHOOK(IBase_Func3_Manual, pVtable, SH_STATIC(Handler_Func3_Pre), false);
   1.310 +
   1.311 +	p_d1i1->Func3(1);
   1.312 +	p_d1i2->Func3(1);
   1.313 +
   1.314 +	CHECK_STATES((&g_States,
   1.315 +		new State_D1_Func3(p_d1i1, 1),
   1.316 +		new State_D1_Func3(p_d1i2, 1),
   1.317 +
   1.318 +		new State_Func3_Pre(p_d1i1, 1),		// manual vp hook
   1.319 +		new State_D1_Func3(p_d1i1, 2),		// function
   1.320 +
   1.321 +		new State_Func3_Pre(p_d1i2, 1),		// normal vp hook
   1.322 +		new State_D1_Func3(p_d1i2, 2),		// function
   1.323 +
   1.324 +		NULL), "Part 8.2");
   1.325 +
   1.326 +
   1.327 +	// Test removing normal hooks even though the instance is deleted
   1.328 +	IBase *pOther = new CDerived1;
   1.329 +	SH_ADD_HOOK(IBase, Func1, pOther, SH_STATIC(Handler_Func1_Pre), false);
   1.330 +	delete pOther;
   1.331 +	// The following line may not crash!
   1.332 +	SH_REMOVE_HOOK(IBase, Func1, pOther, SH_STATIC(Handler_Func1_Pre), false);
   1.333 +
   1.334 +
   1.335 +	// Now test GetOrigVfnPtrEntry
   1.336 +	void *origfuncptr = (*reinterpret_cast<void***>(p_d1i1))[0];
   1.337 +
   1.338 +	CHECK_COND(SH_GET_ORIG_VFNPTR_ENTRY(p_d1i1, &IBase::Func1) == origfuncptr, "Part 9.1");
   1.339 +		
   1.340 +	SH_ADD_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
   1.341 +	
   1.342 +	CHECK_COND(SH_GET_ORIG_VFNPTR_ENTRY(p_d1i1, &IBase::Func1) == origfuncptr, "Part 9.2");
   1.343 +
   1.344 +	SH_REMOVE_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
   1.345 +
   1.346 +	CHECK_COND(SH_GET_ORIG_VFNPTR_ENTRY(p_d1i1, &IBase::Func1) == origfuncptr, "Part 9.3");
   1.347 +
   1.348 +	return true;
   1.349 +}
   1.350 +