Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite

This commit is contained in:
Andreas Schiffler 2014-03-19 21:39:55 -07:00
parent 6ca112739e
commit f018ca4694
9 changed files with 351 additions and 5 deletions

View File

@ -271,6 +271,10 @@
RelativePath="..\..\..\test\testautomation_video.c" RelativePath="..\..\..\test\testautomation_video.c"
> >
</File> </File>
<File
RelativePath="..\..\..\test\testautomation_hints.c"
>
</File>
</Files> </Files>
<Globals> <Globals>
</Globals> </Globals>

View File

@ -194,6 +194,7 @@
<ClCompile Include="..\..\..\test\testautomation_syswm.c" /> <ClCompile Include="..\..\..\test\testautomation_syswm.c" />
<ClCompile Include="..\..\..\test\testautomation_timer.c" /> <ClCompile Include="..\..\..\test\testautomation_timer.c" />
<ClCompile Include="..\..\..\test\testautomation_video.c" /> <ClCompile Include="..\..\..\test\testautomation_video.c" />
<ClCompile Include="..\..\..\test\testautomation_hints.c" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\test\testautomation_suites.h" /> <ClInclude Include="..\..\..\test\testautomation_suites.h" />

View File

@ -198,6 +198,7 @@
<ClCompile Include="..\..\..\test\testautomation_syswm.c" /> <ClCompile Include="..\..\..\test\testautomation_syswm.c" />
<ClCompile Include="..\..\..\test\testautomation_timer.c" /> <ClCompile Include="..\..\..\test\testautomation_timer.c" />
<ClCompile Include="..\..\..\test\testautomation_video.c" /> <ClCompile Include="..\..\..\test\testautomation_video.c" />
<ClCompile Include="..\..\..\test\testautomation_hints.c" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\test\testautomation_suites.h" /> <ClInclude Include="..\..\..\test\testautomation_suites.h" />

View File

@ -198,6 +198,7 @@
<ClCompile Include="..\..\..\test\testautomation_syswm.c" /> <ClCompile Include="..\..\..\test\testautomation_syswm.c" />
<ClCompile Include="..\..\..\test\testautomation_timer.c" /> <ClCompile Include="..\..\..\test\testautomation_timer.c" />
<ClCompile Include="..\..\..\test\testautomation_video.c" /> <ClCompile Include="..\..\..\test\testautomation_video.c" />
<ClCompile Include="..\..\..\test\testautomation_hints.c" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\test\testautomation_suites.h" /> <ClInclude Include="..\..\..\test\testautomation_suites.h" />

View File

@ -33,16 +33,27 @@ static size_t SDL_envmemlen = 0;
#endif #endif
/* Put a variable into the environment */ /* 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) #if defined(HAVE_SETENV)
int int
SDL_setenv(const char *name, const char *value, int overwrite) 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); return setenv(name, value, overwrite);
} }
#elif defined(__WIN32__) #elif defined(__WIN32__)
int int
SDL_setenv(const char *name, const char *value, int overwrite) 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) { if (!overwrite) {
char ch = 0; char ch = 0;
const size_t len = GetEnvironmentVariableA(name, &ch, sizeof (ch)); 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; size_t len;
char *new_variable; char *new_variable;
/* Input validation */
if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
return (-1);
}
if (getenv(name) != NULL) { if (getenv(name) != NULL) {
if (overwrite) { if (overwrite) {
unsetenv(name); unsetenv(name);
@ -91,8 +107,8 @@ SDL_setenv(const char *name, const char *value, int overwrite)
char **new_env; char **new_env;
char *new_variable; char *new_variable;
/* A little error checking */ /* Input validation */
if (!name || !value) { if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
return (-1); return (-1);
} }
@ -152,6 +168,11 @@ SDL_setenv(const char *name, const char *value, int overwrite)
char * char *
SDL_getenv(const char *name) SDL_getenv(const char *name)
{ {
/* Input validation */
if (!name || SDL_strlen(name)==0) {
return NULL;
}
return getenv(name); return getenv(name);
} }
#elif defined(__WIN32__) #elif defined(__WIN32__)
@ -160,6 +181,11 @@ SDL_getenv(const char *name)
{ {
size_t bufferlen; size_t bufferlen;
/* Input validation */
if (!name || SDL_strlen(name)==0) {
return NULL;
}
bufferlen = bufferlen =
GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen); GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
if (bufferlen == 0) { if (bufferlen == 0) {
@ -183,6 +209,11 @@ SDL_getenv(const char *name)
int len, i; int len, i;
char *value; char *value;
/* Input validation */
if (!name || SDL_strlen(name)==0) {
return NULL;
}
value = (char *) 0; value = (char *) 0;
if (SDL_env) { if (SDL_env) {
len = SDL_strlen(name); len = SDL_strlen(name);

View File

@ -92,7 +92,8 @@ testautomation$(EXE): $(srcdir)/testautomation.c \
$(srcdir)/testautomation_surface.c \ $(srcdir)/testautomation_surface.c \
$(srcdir)/testautomation_syswm.c \ $(srcdir)/testautomation_syswm.c \
$(srcdir)/testautomation_timer.c \ $(srcdir)/testautomation_timer.c \
$(srcdir)/testautomation_video.c $(srcdir)/testautomation_video.c \
$(srcdir)/testautomation_hints.c
$(CC) -o $@ $^ $(CFLAGS) $(LIBS) $(CC) -o $@ $^ $(CFLAGS) $(LIBS)
testmultiaudio$(EXE): $(srcdir)/testmultiaudio.c testmultiaudio$(EXE): $(srcdir)/testmultiaudio.c

168
test/testautomation_hints.c Normal file
View File

@ -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
};

View File

@ -119,6 +119,140 @@ stdlib_snprintf(void *arg)
return TEST_COMPLETED; return TEST_COMPLETED;
} }
/**
* @brief Call to SDL_getenv and SDL_setenv
*/
int
stdlib_getsetenv(void *arg)
{
const int nameLen = 16;
int counter;
int result;
char name[nameLen + 1];
char * value1;
char * value2;
char * expected;
int overwrite;
char * text;
/* Create a random name. This tests SDL_getenv, since we need to */
/* make sure the variable is not set yet (it shouldn't). */
do {
for(counter = 0; counter < nameLen; counter++) {
name[counter] = (char)SDLTest_RandomIntegerInRange(65, 90);
}
name[nameLen] = '\0';
text = SDL_getenv(name);
SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
if (text != NULL) {
SDLTest_Log("Expected: NULL, Got: '%s' (%i)", text, SDL_strlen(text));
}
} while (text != NULL);
/* Create random values to set */
value1 = SDLTest_RandomAsciiStringOfSize(10);
value2 = SDLTest_RandomAsciiStringOfSize(10);
/* Set value 1 without overwrite */
overwrite = 0;
expected = value1;
result = SDL_setenv(name, value1, overwrite);
SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
/* Check value */
text = SDL_getenv(name);
SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
if (text != NULL) {
SDLTest_AssertCheck(
SDL_strcmp(text, expected) == 0,
"Verify returned text, expected: %s, got: %s",
expected,
text);
}
/* Set value 2 with overwrite */
overwrite = 1;
expected = value2;
result = SDL_setenv(name, value2, overwrite);
SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value2, overwrite);
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
/* Check value */
text = SDL_getenv(name);
SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
if (text != NULL) {
SDLTest_AssertCheck(
SDL_strcmp(text, expected) == 0,
"Verify returned text, expected: %s, got: %s",
expected,
text);
}
/* Set value 1 without overwrite */
overwrite = 0;
expected = value2;
result = SDL_setenv(name, value1, overwrite);
SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
/* Check value */
text = SDL_getenv(name);
SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
if (text != NULL) {
SDLTest_AssertCheck(
SDL_strcmp(text, expected) == 0,
"Verify returned text, expected: %s, got: %s",
expected,
text);
}
/* Set value 1 without overwrite */
overwrite = 1;
expected = value1;
result = SDL_setenv(name, value1, overwrite);
SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
/* Check value */
text = SDL_getenv(name);
SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
if (text != NULL) {
SDLTest_AssertCheck(
SDL_strcmp(text, expected) == 0,
"Verify returned text, expected: %s, got: %s",
expected,
text);
}
/* Negative cases */
for (overwrite=0; overwrite <= 1; overwrite++) {
result = SDL_setenv(NULL, value1, overwrite);
SDLTest_AssertPass("Call to SDL_setenv(NULL,'%s', %i)", value1, overwrite);
SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
result = SDL_setenv("", value1, overwrite);
SDLTest_AssertPass("Call to SDL_setenv('','%s', %i)", value1, overwrite);
SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
result = SDL_setenv("=", value1, overwrite);
SDLTest_AssertPass("Call to SDL_setenv('=','%s', %i)", value1, overwrite);
SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
result = SDL_setenv(name, NULL, overwrite);
SDLTest_AssertPass("Call to SDL_setenv('%s', NULL, %i)", name, overwrite);
SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
}
/* Clean up */
SDL_free(value1);
SDL_free(value2);
return TEST_COMPLETED;
}
/* ================= Test References ================== */ /* ================= Test References ================== */
/* Standard C routine test cases */ /* Standard C routine test cases */
@ -128,14 +262,17 @@ static const SDLTest_TestCaseReference stdlibTest1 =
static const SDLTest_TestCaseReference stdlibTest2 = static const SDLTest_TestCaseReference stdlibTest2 =
{ (SDLTest_TestCaseFp)stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED }; { (SDLTest_TestCaseFp)stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED };
static const SDLTest_TestCaseReference stdlibTest3 =
{ (SDLTest_TestCaseFp)stdlib_getsetenv, "stdlib_getsetenv", "Call to SDL_getenv and SDL_setenv", TEST_ENABLED };
/* Sequence of Standard C routine test cases */ /* Sequence of Standard C routine test cases */
static const SDLTest_TestCaseReference *stdlibTests[] = { static const SDLTest_TestCaseReference *stdlibTests[] = {
&stdlibTest1, &stdlibTest2, NULL &stdlibTest1, &stdlibTest2, &stdlibTest3, NULL
}; };
/* Timer test suite (global) */ /* Timer test suite (global) */
SDLTest_TestSuiteReference stdlibTestSuite = { SDLTest_TestSuiteReference stdlibTestSuite = {
"Standard C routines", "Stdlib",
NULL, NULL,
stdlibTests, stdlibTests,
NULL NULL

View File

@ -26,6 +26,7 @@ extern SDLTest_TestSuiteReference surfaceTestSuite;
extern SDLTest_TestSuiteReference syswmTestSuite; extern SDLTest_TestSuiteReference syswmTestSuite;
extern SDLTest_TestSuiteReference timerTestSuite; extern SDLTest_TestSuiteReference timerTestSuite;
extern SDLTest_TestSuiteReference videoTestSuite; extern SDLTest_TestSuiteReference videoTestSuite;
extern SDLTest_TestSuiteReference hintsTestSuite;
/* All test suites */ /* All test suites */
SDLTest_TestSuiteReference *testSuites[] = { SDLTest_TestSuiteReference *testSuites[] = {
@ -46,6 +47,7 @@ SDLTest_TestSuiteReference *testSuites[] = {
&syswmTestSuite, &syswmTestSuite,
&timerTestSuite, &timerTestSuite,
&videoTestSuite, &videoTestSuite,
&hintsTestSuite,
NULL NULL
}; };