diff -r 264cecd7a0a8 -r a95db73792e5 sourcehook/test/testbail.cpp --- a/sourcehook/test/testbail.cpp Thu Dec 22 21:00:54 2005 +0000 +++ b/sourcehook/test/testbail.cpp Fri Dec 23 11:58:11 2005 +0000 @@ -1,74 +1,103 @@ // TESTBAIL -// Test for a bug Bail has found +// This test used to be a test for a bug BAIL found. +// That bug is now fixed so I've granted BAIL the pleasure of being a test for +// the correct priority ordering of hook managers based on their version. + +/* + THE PROBLEM: + Old hook funcs don't work right when you combine override returns and recalls. + THE SOLUTION: + Always use a new hook func when possible. For this, hook funcs have to be able to say + " HELLO I'M NEW! " + This file tests that functionality. + + How it works: + testbail.cpp compiles with old version of sourcehook.h. + It sets everything up, adds a hook on a function + Then testbail2.cpp which has the new version adds a hook on the same function and + does a recall and overrides the value in it. +*/ + +#include "sourcehook_test.h" #include "testbail.h" void *___testbail_gabgab; +SourceHook::ISourceHook *___testbail_shptr; namespace { - class zomg_lolz + int EatYams_Handler1(int a) { - public: - virtual void zomg() - { - } - }; - SH_DECL_HOOK0_void(zomg_lolz, zomg, SH_NOATTRIB, 0); - void Handler() - { - SH_REMOVE_HOOK_STATICFUNC(zomg_lolz, zomg, META_IFACEPTR(zomg_lolz), - Handler, false); + ADD_STATE(State_EatYams_Handler1_Called(a)); + RETURN_META_VALUE(MRES_IGNORED, 0); } - void Handler2() - { - } +} + +// These are here so they can access this CU's g_States +int ___testbail_EatYams_Handler2(int a) +{ + ADD_STATE(State_EatYams_Handler2_Called(a)); + RETURN_META_VALUE_NEWPARAMS(MRES_OVERRIDE, 6, IGaben, EatYams, (0xBEEF)); +} + +int ___testbail_EatYams_Handler3(int a) +{ + ADD_STATE(State_EatYams_Handler3_Called(a)); + RETURN_META_VALUE(MRES_IGNORED, 0); } bool TestBail(std::string &error) { - SourceHook::CSourceHookImpl g_SHImpl; - g_SHPtr = &g_SHImpl; + GET_SHPTR(g_SHPtr); g_PLID = 1; g_Gabgab = new IGaben; ___testbail_gabgab = (void*)g_Gabgab; + ___testbail_shptr = g_SHPtr; - g_Gabgab->EatYams(); + SH_ADD_HOOK_STATICFUNC(IGaben, EatYams, g_Gabgab, EatYams_Handler1, false); + + ADD_STATE(State_EatYams_Return(g_Gabgab->EatYams(0xDEAD))); - SH_ADD_HOOK_STATICFUNC(IGaben, EatYams, g_Gabgab, EatYams0_Handler, false); + CHECK_STATES((&g_States, + new State_EatYams_Handler1_Called(0xDEAD), + new State_EatYams_Called(0xDEAD), + new State_EatYams_Return(5), + NULL), "Part 1"); - g_Gabgab->EatYams(); + if (!___TestBail2(error)) + return false; - ___TestBail2(); - - g_Gabgab->EatYams(); + CHECK_STATES((&g_States, + new State_EatYams_Handler1_Called(0xDEAD), + new State_EatYams_Handler2_Called(0xDEAD), + new State_EatYams_Handler3_Called(0xBEEF), + new State_EatYams_Called(0xBEEF), + NULL), "Part 2.1"); + + // WHAT IF NOW SOMEONE UNLOADS PLUGIN 2 !?!?!?!? + Test_UnloadPlugin(g_SHPtr, 2); + + ADD_STATE(State_EatYams_Return(g_Gabgab->EatYams(0xDEAD))); - SH_REMOVE_HOOK_STATICFUNC(IGaben, EatYams, g_Gabgab, EatYams0_Handler, false); + CHECK_STATES((&g_States, + new State_EatYams_Handler1_Called(0xDEAD), + new State_EatYams_Called(0xDEAD), + new State_EatYams_Return(5), + NULL), "Part 3"); - g_Gabgab->EatYams(); + SH_REMOVE_HOOK_STATICFUNC(IGaben, EatYams, g_Gabgab, EatYams_Handler1, false); + + ADD_STATE(State_EatYams_Return(g_Gabgab->EatYams(0xDEAD))); + + CHECK_STATES((&g_States, + new State_EatYams_Called(0xDEAD), + new State_EatYams_Return(5), + NULL), "Part 4"); delete g_Gabgab; - // If it didn't crash, it's ok - - // NEW TEST: Remove hook from handler - zomg_lolz inst; - SH_ADD_HOOK_STATICFUNC(zomg_lolz, zomg, &inst, Handler, false); - SH_ADD_HOOK_STATICFUNC(zomg_lolz, zomg, &inst, Handler2, false); - - zomg_lolz *mwah = &inst; - mwah->zomg(); - mwah->zomg(); - - SH_ADD_HOOK_STATICFUNC(zomg_lolz, zomg, &inst, Handler, false); - SH_REMOVE_HOOK_STATICFUNC(zomg_lolz, zomg, &inst, Handler2, false); - - mwah->zomg(); - mwah->zomg(); - - // Shouldn't crash again... - return true; }