sourcehook/test/testbail.cpp
changeset 162 a95db73792e5
parent 133 ffe46578a1ae
child 163 377bdd31c28d
     1.1 --- a/sourcehook/test/testbail.cpp	Thu Dec 22 21:00:54 2005 +0000
     1.2 +++ b/sourcehook/test/testbail.cpp	Fri Dec 23 11:58:11 2005 +0000
     1.3 @@ -1,74 +1,103 @@
     1.4  // TESTBAIL
     1.5 -// Test for a bug Bail has found
     1.6 +// This test used to be a test for a bug BAIL found.
     1.7 +// That bug is now fixed so I've granted BAIL the pleasure of being a test for
     1.8 +// the correct priority ordering of hook managers based on their version.
     1.9 +
    1.10 +/*
    1.11 +	THE PROBLEM:
    1.12 +		Old hook funcs don't work right when you combine override returns and recalls.
    1.13 +	THE SOLUTION:
    1.14 +		Always use a new hook func when possible. For this, hook funcs have to be able to say
    1.15 +			" HELLO I'M NEW! "
    1.16  
    1.17 +	This file tests that functionality.
    1.18 +
    1.19 +	How it works:
    1.20 +		testbail.cpp compiles with old version of sourcehook.h.
    1.21 +		It sets everything up, adds a hook on a function
    1.22 +		Then testbail2.cpp which has the new version adds a hook on the same function and 
    1.23 +		does a recall and overrides the value in it.
    1.24 +*/
    1.25 +
    1.26 +#include "sourcehook_test.h"
    1.27  #include "testbail.h"
    1.28  
    1.29  void *___testbail_gabgab;
    1.30 +SourceHook::ISourceHook *___testbail_shptr;
    1.31  
    1.32  namespace
    1.33  {
    1.34 -	class zomg_lolz
    1.35 +	int EatYams_Handler1(int a)
    1.36  	{
    1.37 -	public:
    1.38 -		virtual void zomg()
    1.39 -		{
    1.40 -		}
    1.41 -	};
    1.42 -	SH_DECL_HOOK0_void(zomg_lolz, zomg, SH_NOATTRIB, 0);
    1.43 -	void Handler()
    1.44 -	{
    1.45 -		SH_REMOVE_HOOK_STATICFUNC(zomg_lolz, zomg, META_IFACEPTR(zomg_lolz),
    1.46 -			Handler, false);
    1.47 +		ADD_STATE(State_EatYams_Handler1_Called(a));
    1.48 +		RETURN_META_VALUE(MRES_IGNORED, 0);
    1.49  	}
    1.50 -	void Handler2()
    1.51 -	{
    1.52 -	}
    1.53 +}
    1.54 +
    1.55 +// These are here so they can access this CU's g_States
    1.56 +int ___testbail_EatYams_Handler2(int a)
    1.57 +{
    1.58 +	ADD_STATE(State_EatYams_Handler2_Called(a));
    1.59 +	RETURN_META_VALUE_NEWPARAMS(MRES_OVERRIDE, 6, IGaben, EatYams, (0xBEEF));
    1.60 +}
    1.61 +
    1.62 +int ___testbail_EatYams_Handler3(int a)
    1.63 +{
    1.64 +	ADD_STATE(State_EatYams_Handler3_Called(a));
    1.65 +	RETURN_META_VALUE(MRES_IGNORED, 0);
    1.66  }
    1.67  
    1.68  bool TestBail(std::string &error)
    1.69  {
    1.70 -	SourceHook::CSourceHookImpl g_SHImpl;
    1.71 -	g_SHPtr = &g_SHImpl;
    1.72 +	GET_SHPTR(g_SHPtr);
    1.73  	g_PLID = 1;
    1.74  
    1.75  	g_Gabgab = new IGaben;
    1.76  	___testbail_gabgab = (void*)g_Gabgab;
    1.77 +	___testbail_shptr = g_SHPtr;
    1.78  
    1.79 -	g_Gabgab->EatYams();
    1.80 +	SH_ADD_HOOK_STATICFUNC(IGaben, EatYams, g_Gabgab, EatYams_Handler1, false);
    1.81 +
    1.82 +	ADD_STATE(State_EatYams_Return(g_Gabgab->EatYams(0xDEAD)));
    1.83  
    1.84 -	SH_ADD_HOOK_STATICFUNC(IGaben, EatYams, g_Gabgab, EatYams0_Handler, false);
    1.85 +	CHECK_STATES((&g_States,
    1.86 +		new State_EatYams_Handler1_Called(0xDEAD),
    1.87 +		new State_EatYams_Called(0xDEAD),
    1.88 +		new State_EatYams_Return(5),
    1.89 +		NULL), "Part 1");
    1.90  
    1.91 -	g_Gabgab->EatYams();
    1.92 +	if (!___TestBail2(error))
    1.93 +		return false;
    1.94  
    1.95 -	___TestBail2();
    1.96 -	
    1.97 -	g_Gabgab->EatYams();
    1.98 +	CHECK_STATES((&g_States,
    1.99 +		new State_EatYams_Handler1_Called(0xDEAD),
   1.100 +		new State_EatYams_Handler2_Called(0xDEAD),
   1.101 +		new State_EatYams_Handler3_Called(0xBEEF),
   1.102 +		new State_EatYams_Called(0xBEEF),
   1.103 +		NULL), "Part 2.1");
   1.104 +
   1.105 +	// WHAT IF NOW SOMEONE UNLOADS PLUGIN 2 !?!?!?!?
   1.106 +	Test_UnloadPlugin(g_SHPtr, 2);
   1.107 +
   1.108 +	ADD_STATE(State_EatYams_Return(g_Gabgab->EatYams(0xDEAD)));
   1.109  
   1.110 -	SH_REMOVE_HOOK_STATICFUNC(IGaben, EatYams, g_Gabgab, EatYams0_Handler, false);
   1.111 +	CHECK_STATES((&g_States,
   1.112 +		new State_EatYams_Handler1_Called(0xDEAD),
   1.113 +		new State_EatYams_Called(0xDEAD),
   1.114 +		new State_EatYams_Return(5),
   1.115 +		NULL), "Part 3");
   1.116  
   1.117 -	g_Gabgab->EatYams();
   1.118 +	SH_REMOVE_HOOK_STATICFUNC(IGaben, EatYams, g_Gabgab, EatYams_Handler1, false);
   1.119 +
   1.120 +	ADD_STATE(State_EatYams_Return(g_Gabgab->EatYams(0xDEAD)));
   1.121 +
   1.122 +	CHECK_STATES((&g_States,
   1.123 +		new State_EatYams_Called(0xDEAD),
   1.124 +		new State_EatYams_Return(5),
   1.125 +		NULL), "Part 4");
   1.126  
   1.127  	delete g_Gabgab;
   1.128  
   1.129 -	// If it didn't crash, it's ok
   1.130 -
   1.131 -	// NEW TEST: Remove hook from handler
   1.132 -	zomg_lolz inst;
   1.133 -	SH_ADD_HOOK_STATICFUNC(zomg_lolz, zomg, &inst, Handler, false);
   1.134 -	SH_ADD_HOOK_STATICFUNC(zomg_lolz, zomg, &inst, Handler2, false);
   1.135 -
   1.136 -	zomg_lolz *mwah = &inst;
   1.137 -	mwah->zomg();
   1.138 -	mwah->zomg();
   1.139 -
   1.140 -	SH_ADD_HOOK_STATICFUNC(zomg_lolz, zomg, &inst, Handler, false);
   1.141 -	SH_REMOVE_HOOK_STATICFUNC(zomg_lolz, zomg, &inst, Handler2, false);
   1.142 -
   1.143 -	mwah->zomg();
   1.144 -	mwah->zomg();
   1.145 -
   1.146 -	// Shouldn't crash again...
   1.147 -
   1.148  	return true;
   1.149  }
   1.150