Browse Source

engine: add cmd and cvar tests for privileged mode

pull/2/head
Alibek Omarov 3 years ago committed by a1batross
parent
commit
992bcd89ef
  1. 63
      engine/common/cmd.c
  2. 52
      engine/common/cvar.c
  3. 4
      engine/common/host.c
  4. 2
      engine/common/tests.h

63
engine/common/cmd.c

@ -957,7 +957,7 @@ static qboolean Cmd_ShouldAllowCommand( cmd_t *cmd, qboolean isPrivileged ) @@ -957,7 +957,7 @@ static qboolean Cmd_ShouldAllowCommand( cmd_t *cmd, qboolean isPrivileged )
for( i = 0; i < ARRAYSIZE( prefixes ); i++ )
{
if( !Q_stricmp( cmd->name, prefixes[i] ))
if( !Q_strnicmp( cmd->name, prefixes[i], Q_strlen( prefixes[i] )))
return false;
}
@ -1398,3 +1398,64 @@ void Cmd_Init( void ) @@ -1398,3 +1398,64 @@ void Cmd_Init( void )
Cmd_AddCommand( "basecmd_test", BaseCmd_Test_f, "test basecmd" );
#endif
}
#if XASH_ENGINE_TESTS
#include "tests.h"
enum
{
NO_CALL = 0,
PRIV = 1,
UNPRIV = 2
};
static int test_flags[3] = { NO_CALL, NO_CALL, NO_CALL };
static void Test_PrivilegedCommand_f( void )
{
test_flags[0] = Cmd_CurrentCommandIsPrivileged() ? PRIV : UNPRIV;
}
static void Test_UnprivilegedCommand_f( void )
{
test_flags[1] = Cmd_CurrentCommandIsPrivileged() ? PRIV : UNPRIV;
}
static void Test_FilteredCommand_f( void )
{
test_flags[2] = Cmd_CurrentCommandIsPrivileged() ? PRIV : UNPRIV;
}
void Test_RunCmd( void )
{
Cmd_AddCommand( "test_privileged", Test_PrivilegedCommand_f, "bark bark" );
Cmd_AddRestrictedCommand( "test_unprivileged", Test_UnprivilegedCommand_f, "meow meow" );
Cmd_AddCommand( "hud_filtered", Test_FilteredCommand_f, "dummy description" );
Cbuf_AddText( "test_privileged; test_unprivileged; hud_filtered\n" );
Cbuf_Execute();
TASSERT( test_flags[0] == PRIV );
TASSERT( test_flags[1] == PRIV );
TASSERT( test_flags[2] == PRIV );
VectorSet( test_flags, NO_CALL, NO_CALL, NO_CALL );
Cvar_DirectSet( &cl_filterstuffcmd, "0" );
Cbuf_AddFilteredText( "test_privileged; test_unprivileged; hud_filtered\n" );
Cbuf_Execute();
TASSERT( test_flags[0] == UNPRIV );
TASSERT( test_flags[1] == NO_CALL );
TASSERT( test_flags[2] == UNPRIV );
VectorSet( test_flags, NO_CALL, NO_CALL, NO_CALL );
Cvar_DirectSet( &cl_filterstuffcmd, "1" );
Cbuf_AddFilteredText( "test_privileged; test_unprivileged; hud_filtered\n" );
Cbuf_Execute();
TASSERT( test_flags[0] == UNPRIV );
TASSERT( test_flags[1] == NO_CALL );
TASSERT( test_flags[2] == NO_CALL );
Cmd_RemoveCommand( "hud_filtered" );
Cmd_RemoveCommand( "test_unprivileged" );
Cmd_RemoveCommand( "test_privileged" );
}
#endif

52
engine/common/cvar.c

@ -21,6 +21,8 @@ GNU General Public License for more details. @@ -21,6 +21,8 @@ GNU General Public License for more details.
convar_t *cvar_vars = NULL; // head of list
convar_t *cmd_scripting;
CVAR_DEFINE_AUTO( cl_filterstuffcmd, "1", FCVAR_ARCHIVE | FCVAR_PRIVILEGED, "filter commands coming from server" );
/*
============
Cvar_GetList
@ -780,7 +782,7 @@ static qboolean Cvar_ShouldSetCvar( convar_t *v, qboolean isPrivileged ) @@ -780,7 +782,7 @@ static qboolean Cvar_ShouldSetCvar( convar_t *v, qboolean isPrivileged )
for( i = 0; i < ARRAYSIZE( prefixes ); i++ )
{
if( !Q_stricmp( v->name, prefixes[i] ))
if( !Q_strnicmp( v->name, prefixes[i], Q_strlen( prefixes[i] )))
return false;
}
@ -994,9 +996,57 @@ void Cvar_Init( void ) @@ -994,9 +996,57 @@ void Cvar_Init( void )
cvar_vars = NULL;
cmd_scripting = Cvar_Get( "cmd_scripting", "0", FCVAR_ARCHIVE|FCVAR_PRIVILEGED, "enable simple condition checking and variable operations" );
Cvar_RegisterVariable (&host_developer); // early registering for dev
Cvar_RegisterVariable( &cl_filterstuffcmd );
Cmd_AddRestrictedCommand( "setgl", Cvar_SetGL_f, "change the value of a opengl variable" ); // OBSOLETE
Cmd_AddRestrictedCommand( "toggle", Cvar_Toggle_f, "toggles a console variable's values (use for more info)" );
Cmd_AddRestrictedCommand( "reset", Cvar_Reset_f, "reset any type variable to initial value" );
Cmd_AddCommand( "cvarlist", Cvar_List_f, "display all console variables beginning with the specified prefix" );
}
#if XASH_ENGINE_TESTS
#include "tests.h"
void Test_RunCvar( void )
{
convar_t *test_privileged = Cvar_Get( "test_privileged", "0", FCVAR_PRIVILEGED, "bark bark" );
convar_t *test_unprivileged = Cvar_Get( "test_unprivileged", "0", 0, "meow meow" );
convar_t *hud_filtered = Cvar_Get( "hud_filtered", "0", 0, "dummy description" );
convar_t *filtered2 = Cvar_Get( "filtered2", "0", FCVAR_FILTERABLE, "filtered2" );
Cbuf_AddText( "test_privileged 1; test_unprivileged 1; hud_filtered 1; filtered2 1\n" );
Cbuf_Execute();
TASSERT( test_privileged->value != 0.0f );
TASSERT( test_unprivileged->value != 0.0f );
TASSERT( hud_filtered->value != 0.0f );
TASSERT( filtered2->value != 0.0f );
Cvar_DirectSet( test_privileged, "0" );
Cvar_DirectSet( test_unprivileged, "0" );
Cvar_DirectSet( hud_filtered, "0" );
Cvar_DirectSet( filtered2, "0" );
Cvar_DirectSet( &cl_filterstuffcmd, "0" );
Cbuf_AddFilteredText( "test_privileged 1; test_unprivileged 1; hud_filtered 1; filtered2 1\n" );
Cbuf_Execute();
Cbuf_Execute();
Cbuf_Execute();
TASSERT( test_privileged->value == 0.0f );
TASSERT( test_unprivileged->value != 0.0f );
TASSERT( hud_filtered->value != 0.0f );
TASSERT( filtered2->value != 0.0f );
Cvar_DirectSet( test_privileged, "0" );
Cvar_DirectSet( test_unprivileged, "0" );
Cvar_DirectSet( hud_filtered, "0" );
Cvar_DirectSet( filtered2, "0" );
Cvar_DirectSet( &cl_filterstuffcmd, "1" );
Cbuf_AddFilteredText( "test_privileged 1; test_unprivileged 1; hud_filtered 1; filtered2 1\n" );
Cbuf_Execute();
Cbuf_Execute();
Cbuf_Execute();
TASSERT( test_privileged->value == 0.0f );
TASSERT( test_unprivileged->value != 0.0f );
TASSERT( hud_filtered->value == 0.0f );
TASSERT( filtered2->value == 0.0f );
}
#endif

4
engine/common/host.c

@ -52,7 +52,6 @@ struct tests_stats_s tests_stats; @@ -52,7 +52,6 @@ struct tests_stats_s tests_stats;
CVAR_DEFINE( host_developer, "developer", "0", FCVAR_FILTERABLE, "engine is in development-mode" );
CVAR_DEFINE_AUTO( sys_ticrate, "100", 0, "framerate in dedicated mode" );
CVAR_DEFINE_AUTO( cl_filterstuffcmd, "1", FCVAR_ARCHIVE | FCVAR_PRIVILEGED, "filter commands coming from server" );
convar_t *host_serverstate;
convar_t *host_gameloaded;
@ -819,6 +818,8 @@ static void Host_RunTests( int stage ) @@ -819,6 +818,8 @@ static void Host_RunTests( int stage )
memset( &tests_stats, 0, sizeof( tests_stats ));
Test_RunLibCommon();
Test_RunCommon();
Test_RunCmd();
Test_RunCvar();
break;
case 1: // after FS load
Test_RunImagelib();
@ -1086,7 +1087,6 @@ int EXPORT Host_Main( int argc, char **argv, const char *progname, int bChangeGa @@ -1086,7 +1087,6 @@ int EXPORT Host_Main( int argc, char **argv, const char *progname, int bChangeGa
Cmd_AddRestrictedCommand ( "crash", Host_Crash_f, "a way to force a bus error for development reasons");
}
Cvar_RegisterVariable( &cl_filterstuffcmd );
host_serverstate = Cvar_Get( "host_serverstate", "0", FCVAR_READ_ONLY, "displays current server state" );
host_maxfps = Cvar_Get( "fps_max", "72", FCVAR_ARCHIVE|FCVAR_FILTERABLE, "host fps upper limit" );
host_framerate = Cvar_Get( "host_framerate", "0", FCVAR_FILTERABLE, "locks frame timing to this value in seconds" );

2
engine/common/tests.h

@ -24,6 +24,8 @@ extern struct tests_stats_s tests_stats; @@ -24,6 +24,8 @@ extern struct tests_stats_s tests_stats;
void Test_RunImagelib( void );
void Test_RunLibCommon( void );
void Test_RunCommon( void );
void Test_RunCmd( void );
void Test_RunCvar( void );
#endif

Loading…
Cancel
Save