Skip to content
Permalink
Browse files

Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuit…

…e; add Hints testsuite
  • Loading branch information
ferzkopp committed Mar 20, 2014
1 parent 6ca1127 commit f018ca469439b7d73fee0103127bbed52fb4f6c2
@@ -271,6 +271,10 @@
RelativePath="..\..\..\test\testautomation_video.c"
>
</File>
<File
RelativePath="..\..\..\test\testautomation_hints.c"
>
</File>
</Files>
<Globals>
</Globals>
@@ -194,6 +194,7 @@
<ClCompile Include="..\..\..\test\testautomation_syswm.c" />
<ClCompile Include="..\..\..\test\testautomation_timer.c" />
<ClCompile Include="..\..\..\test\testautomation_video.c" />
<ClCompile Include="..\..\..\test\testautomation_hints.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\test\testautomation_suites.h" />
@@ -198,6 +198,7 @@
<ClCompile Include="..\..\..\test\testautomation_syswm.c" />
<ClCompile Include="..\..\..\test\testautomation_timer.c" />
<ClCompile Include="..\..\..\test\testautomation_video.c" />
<ClCompile Include="..\..\..\test\testautomation_hints.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\test\testautomation_suites.h" />
@@ -198,6 +198,7 @@
<ClCompile Include="..\..\..\test\testautomation_syswm.c" />
<ClCompile Include="..\..\..\test\testautomation_timer.c" />
<ClCompile Include="..\..\..\test\testautomation_video.c" />
<ClCompile Include="..\..\..\test\testautomation_hints.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\test\testautomation_suites.h" />
@@ -33,16 +33,27 @@ static size_t SDL_envmemlen = 0;
#endif

/* Put a variable into the environment */
/* Note: Name may not contain a '=' character. (Reference: http://www.unix.com/man-page/Linux/3/setenv/) */
#if defined(HAVE_SETENV)
int
SDL_setenv(const char *name, const char *value, int overwrite)
{
/* Input validation */
if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
return (-1);
}

return setenv(name, value, overwrite);
}
#elif defined(__WIN32__)
int
SDL_setenv(const char *name, const char *value, int overwrite)
{
/* Input validation */
if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
return (-1);
}

if (!overwrite) {
char ch = 0;
const size_t len = GetEnvironmentVariableA(name, &ch, sizeof (ch));
@@ -63,6 +74,11 @@ SDL_setenv(const char *name, const char *value, int overwrite)
size_t len;
char *new_variable;

/* Input validation */
if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
return (-1);
}

if (getenv(name) != NULL) {
if (overwrite) {
unsetenv(name);
@@ -91,8 +107,8 @@ SDL_setenv(const char *name, const char *value, int overwrite)
char **new_env;
char *new_variable;

/* A little error checking */
if (!name || !value) {
/* Input validation */
if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
return (-1);
}

@@ -152,6 +168,11 @@ SDL_setenv(const char *name, const char *value, int overwrite)
char *
SDL_getenv(const char *name)
{
/* Input validation */
if (!name || SDL_strlen(name)==0) {
return NULL;
}

return getenv(name);
}
#elif defined(__WIN32__)
@@ -160,6 +181,11 @@ SDL_getenv(const char *name)
{
size_t bufferlen;

/* Input validation */
if (!name || SDL_strlen(name)==0) {
return NULL;
}

bufferlen =
GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
if (bufferlen == 0) {
@@ -183,6 +209,11 @@ SDL_getenv(const char *name)
int len, i;
char *value;

/* Input validation */
if (!name || SDL_strlen(name)==0) {
return NULL;
}

value = (char *) 0;
if (SDL_env) {
len = SDL_strlen(name);
@@ -92,7 +92,8 @@ testautomation$(EXE): $(srcdir)/testautomation.c \
$(srcdir)/testautomation_surface.c \
$(srcdir)/testautomation_syswm.c \
$(srcdir)/testautomation_timer.c \
$(srcdir)/testautomation_video.c
$(srcdir)/testautomation_video.c \
$(srcdir)/testautomation_hints.c
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)

testmultiaudio$(EXE): $(srcdir)/testmultiaudio.c
@@ -0,0 +1,168 @@
/**
* Hints test suite
*/

#include <stdio.h>

#include "SDL.h"
#include "SDL_test.h"


const int _numHintsEnum = 25;
char* _HintsEnum[] =
{
SDL_HINT_ACCELEROMETER_AS_JOYSTICK,
SDL_HINT_FRAMEBUFFER_ACCELERATION,
SDL_HINT_GAMECONTROLLERCONFIG,
SDL_HINT_GRAB_KEYBOARD,
SDL_HINT_IDLE_TIMER_DISABLED,
SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK,
SDL_HINT_MOUSE_RELATIVE_MODE_WARP,
SDL_HINT_ORIENTATIONS,
SDL_HINT_RENDER_DIRECT3D_THREADSAFE,
SDL_HINT_RENDER_DRIVER,
SDL_HINT_RENDER_OPENGL_SHADERS,
SDL_HINT_RENDER_SCALE_QUALITY,
SDL_HINT_RENDER_VSYNC,
SDL_HINT_TIMER_RESOLUTION,
SDL_HINT_VIDEO_ALLOW_SCREENSAVER,
SDL_HINT_VIDEO_HIGHDPI_DISABLED,
SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES,
SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS,
SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT,
SDL_HINT_VIDEO_WIN_D3DCOMPILER,
SDL_HINT_VIDEO_X11_XINERAMA,
SDL_HINT_VIDEO_X11_XRANDR,
SDL_HINT_VIDEO_X11_XVIDMODE,
SDL_HINT_XINPUT_ENABLED,
};
char* _HintsVerbose[] =
{
"SDL_HINT_ACCELEROMETER_AS_JOYSTICK",
"SDL_HINT_FRAMEBUFFER_ACCELERATION",
"SDL_HINT_GAMECONTROLLERCONFIG",
"SDL_HINT_GRAB_KEYBOARD",
"SDL_HINT_IDLE_TIMER_DISABLED",
"SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS",
"SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK",
"SDL_HINT_MOUSE_RELATIVE_MODE_WARP",
"SDL_HINT_ORIENTATIONS",
"SDL_HINT_RENDER_DIRECT3D_THREADSAFE",
"SDL_HINT_RENDER_DRIVER",
"SDL_HINT_RENDER_OPENGL_SHADERS",
"SDL_HINT_RENDER_SCALE_QUALITY",
"SDL_HINT_RENDER_VSYNC",
"SDL_HINT_TIMER_RESOLUTION",
"SDL_HINT_VIDEO_ALLOW_SCREENSAVER",
"SDL_HINT_VIDEO_HIGHDPI_DISABLED",
"SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES",
"SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS",
"SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT",
"SDL_HINT_VIDEO_WIN_D3DCOMPILER",
"SDL_HINT_VIDEO_X11_XINERAMA",
"SDL_HINT_VIDEO_X11_XRANDR",
"SDL_HINT_VIDEO_X11_XVIDMODE",
"SDL_HINT_XINPUT_ENABLED"
};


/* Test case functions */

/**
* @brief Call to SDL_GetHint
*/
int
hints_getHint(void *arg)
{
char *result1;
char *result2;
int i;

for (i=0; i<_numHintsEnum; i++) {
result1 = (char *)SDL_GetHint((char*)_HintsEnum[i]);
SDLTest_AssertPass("Call to SDL_GetHint(%s) - using define definition", (char*)_HintsEnum[i]);
result2 = (char *)SDL_GetHint((char *)_HintsVerbose[i]);
SDLTest_AssertPass("Call to SDL_GetHint(%s) - using string definition", (char*)_HintsVerbose[i]);
SDLTest_AssertCheck(
(result1 == NULL && result2 == NULL) || (SDL_strcmp(result1, result2) == 0),
"Verify returned values are equal; got: result1='%s' result2='%s",
(result1 == NULL) ? "null" : result1,
(result2 == NULL) ? "null" : result2);
}

return TEST_COMPLETED;
}

/**
* @brief Call to SDL_SetHint
*/
int
hints_setHint(void *arg)
{
char *originalValue;
char *value;
char *testValue;
SDL_bool result;
int i, j;

/* Create random values to set */
value = SDLTest_RandomAsciiStringOfSize(10);

for (i=0; i<_numHintsEnum; i++) {
/* Capture current value */
originalValue = (char *)SDL_GetHint((char*)_HintsEnum[i]);
SDLTest_AssertPass("Call to SDL_GetHint(%s)", (char*)_HintsEnum[i]);

/* Set value (twice) */
for (j=1; j<=2; j++) {
result = SDL_SetHint((char*)_HintsEnum[i], value);
SDLTest_AssertPass("Call to SDL_SetHint(%s, %s) (iteration %i)", (char*)_HintsEnum[i], value, j);
SDLTest_AssertCheck(
result == SDL_TRUE || result == SDL_FALSE,
"Verify valid result was returned, got: %i",
(int)result);
testValue = (char *)SDL_GetHint((char*)_HintsEnum[i]);
SDLTest_AssertPass("Call to SDL_GetHint(%s) - using string definition", (char*)_HintsVerbose[i]);
SDLTest_AssertCheck(
(SDL_strcmp(value, testValue) == 0),
"Verify returned value equals set value; got: testValue='%s' value='%s",
(testValue == NULL) ? "null" : testValue,
value);
}

/* Reset original value */
result = SDL_SetHint((char*)_HintsEnum[i], originalValue);
SDLTest_AssertPass("Call to SDL_SetHint(%s, originalValue)", (char*)_HintsEnum[i]);
SDLTest_AssertCheck(
result == SDL_TRUE || result == SDL_FALSE,
"Verify valid result was returned, got: %i",
(int)result);
}

SDL_free(value);

return TEST_COMPLETED;
}

/* ================= Test References ================== */

/* Hints test cases */
static const SDLTest_TestCaseReference hintsTest1 =
{ (SDLTest_TestCaseFp)hints_getHint, "hints_getHint", "Call to SDL_GetHint", TEST_ENABLED };

static const SDLTest_TestCaseReference hintsTest2 =
{ (SDLTest_TestCaseFp)hints_setHint, "hints_setHint", "Call to SDL_SetHint", TEST_ENABLED };

/* Sequence of Hints test cases */
static const SDLTest_TestCaseReference *hintsTests[] = {
&hintsTest1, &hintsTest2, NULL
};

/* Hints test suite (global) */
SDLTest_TestSuiteReference hintsTestSuite = {
"Hints",
NULL,
hintsTests,
NULL
};

0 comments on commit f018ca4

Please sign in to comment.