core/sourcehook/test/testbail.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Dec 10 00:59:50 2009 -0600 (2009-12-10)
changeset 689 4065ba5e9155
parent 539 db914129ae01
permissions -rw-r--r--
Rewrote user message caching code to always use IServerGameDLL::GetUserMessageInfo (bug 4117, r=dvander).

Games where the above function can crash are handled by detouring tier0's Error() function and jumping back to our own code. So in theory this should work on all games now.
     1 // TESTBAIL
     2 // This test used to be a test for a bug BAIL found.
     3 // That bug is now fixed so I've granted BAIL the pleasure of being a test for
     4 // the correct priority ordering of hook managers based on their version.
     5 
     6 /*
     7 	THE PROBLEM:
     8 		Old hook funcs don't work right when you combine override returns and recalls.
     9 	THE SOLUTION:
    10 		Always use a new hook func when possible. For this, hook funcs have to be able to say
    11 			" HELLO I'M NEW! "
    12 
    13 	This file tests that functionality.
    14 
    15 	How it works:
    16 		testbail.cpp compiles with old version of sourcehook.h.
    17 		It sets everything up, adds a hook on a function
    18 		Then testbail2.cpp which has the new version adds a hook on the same function and 
    19 		does a recall and overrides the value in it.
    20 */
    21 #include "sourcehook_test.h"
    22 #include "testbail.h"
    23 
    24 namespace N_TestBail
    25 {
    26 	StateList g_States;
    27 	SourceHook::ISourceHook *g_SHPtr;
    28 	IGaben *g_Gabgab;
    29 }
    30 
    31 namespace
    32 {
    33 	int EatYams_Handler1(int a)
    34 	{
    35 		ADD_STATE(State_EatYams_Handler1_Called(a));
    36 		RETURN_META_VALUE(MRES_IGNORED, 0);
    37 	}
    38 }
    39 
    40 bool TestBail(std::string &error)
    41 {
    42 	GET_SHPTR(g_SHPtr);
    43 	g_PLID = 1;
    44 
    45 	g_Gabgab = new IGaben;
    46 
    47 	SH_ADD_HOOK(IGaben, EatYams, g_Gabgab, SH_STATIC(EatYams_Handler1), false);
    48 
    49 	ADD_STATE(State_EatYams_Return(g_Gabgab->EatYams(0xDEAD)));
    50 
    51 	CHECK_STATES((&g_States,
    52 		new State_EatYams_Handler1_Called(0xDEAD),
    53 		new State_EatYams_Called(0xDEAD),
    54 		new State_EatYams_Return(5),
    55 		NULL), "Part 1");
    56 
    57 	if (!TestBail2(error))
    58 		return false;
    59 
    60 	CHECK_STATES((&g_States,
    61 		new State_EatYams_Handler1_Called(0xDEAD),
    62 		new State_EatYams_Handler2_Called(0xDEAD),
    63 		new State_EatYams_Handler3_Called(0xBEEF),
    64 		new State_EatYams_Called(0xBEEF),
    65 		NULL), "Part 2.1");
    66 
    67 	// WHAT IF NOW SOMEONE UNLOADS PLUGIN 2 !?!?!?!?
    68 	Test_UnloadPlugin(g_SHPtr, 2);
    69 
    70 	ADD_STATE(State_EatYams_Return(g_Gabgab->EatYams(0xDEAD)));
    71 
    72 	CHECK_STATES((&g_States,
    73 		new State_EatYams_Handler1_Called(0xDEAD),
    74 		new State_EatYams_Called(0xDEAD),
    75 		new State_EatYams_Return(5),
    76 		NULL), "Part 3");
    77 
    78 	SH_REMOVE_HOOK(IGaben, EatYams, g_Gabgab, SH_STATIC(EatYams_Handler1), false);
    79 
    80 	ADD_STATE(State_EatYams_Return(g_Gabgab->EatYams(0xDEAD)));
    81 
    82 	CHECK_STATES((&g_States,
    83 		new State_EatYams_Called(0xDEAD),
    84 		new State_EatYams_Return(5),
    85 		NULL), "Part 4");
    86 
    87 	// Now, heh, try it the other way round.
    88 
    89 	Test_CompleteShutdown(g_SHPtr);
    90 
    91 	if (!TestBail2(error))
    92 		return false;
    93 
    94 	CHECK_STATES((&g_States,
    95 		new State_EatYams_Handler2_Called(0xDEAD),
    96 		new State_EatYams_Handler3_Called(0xBEEF),
    97 		new State_EatYams_Called(0xBEEF),
    98 		NULL), "Part 5");
    99 
   100 	SH_ADD_HOOK(IGaben, EatYams, g_Gabgab, SH_STATIC(EatYams_Handler1), false);
   101 
   102 	ADD_STATE(State_EatYams_Return(g_Gabgab->EatYams(0xDEAD)));
   103 
   104 	CHECK_STATES((&g_States,
   105 		new State_EatYams_Handler2_Called(0xDEAD),
   106 		new State_EatYams_Handler3_Called(0xBEEF),
   107 		new State_EatYams_Handler1_Called(0xBEEF),
   108 		new State_EatYams_Called(0xBEEF),
   109 		new State_EatYams_Return(6),
   110 		NULL), "Part 6");
   111 
   112 	delete g_Gabgab;
   113 
   114 	return true;
   115 }