Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Update SDL_InvalidParamError to take param name; add additional fuzze…
Browse files Browse the repository at this point in the history
…r function; add new tests to keyboard test suite; improve surface test suite
  • Loading branch information
ferzkopp committed Jan 13, 2013
1 parent 0cd120a commit e5dbcee
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 51 deletions.
3 changes: 1 addition & 2 deletions include/SDL_error.h
Expand Up @@ -52,15 +52,14 @@ extern DECLSPEC void SDLCALL SDL_ClearError(void);
/*@{*/
#define SDL_OutOfMemory() SDL_Error(SDL_ENOMEM)
#define SDL_Unsupported() SDL_Error(SDL_UNSUPPORTED)
#define SDL_InvalidParamError() SDL_Error(SDL_INVALIDPARAM)
#define SDL_InvalidParamError(param) SDL_SetError("Parameter '%s' is invalid", (param))
typedef enum
{
SDL_ENOMEM,
SDL_EFREAD,
SDL_EFWRITE,
SDL_EFSEEK,
SDL_UNSUPPORTED,
SDL_INVALIDPARAM,
SDL_LASTERROR
} SDL_errorcode;
extern DECLSPEC void SDLCALL SDL_Error(SDL_errorcode code);
Expand Down
30 changes: 22 additions & 8 deletions include/SDL_test_fuzzer.h
Expand Up @@ -329,30 +329,44 @@ Sint32 SDLTest_RandomIntegerInRange(Sint32 min, Sint32 max);


/**
* Generates random null-terminated string. The maximum length for
* the string is 255 characters and it can contain ASCII characters
* from 1 to 127.
* Generates random null-terminated string. The minimum length for
* the string is 1 character, maximum length for the string is 255
* characters and it can contain ASCII characters from 32 to 126.
*
* Note: Returned string needs to be deallocated.
*
* \returns newly allocated random string
* \returns Newly allocated random string; or NULL if length was invalid or string could not be allocated.
*/
char * SDLTest_RandomAsciiString();


/**
* Generates random null-terminated string. The maximum length for
* the string is defined by maxLenght parameter.
* String can contain ASCII characters from 1 to 127.
* the string is defined by the maxLength parameter.
* String can contain ASCII characters from 32 to 126.
*
* Note: Returned string needs to be deallocated.
*
* \param maxLength Maximum length of the generated string
* \param maxLength The maximum length of the generated string.
*
* \returns newly allocated random string
* \returns Newly allocated random string; or NULL if maxLength was invalid or string could not be allocated.
*/
char * SDLTest_RandomAsciiStringWithMaximumLength(int maxLength);


/**
* Generates random null-terminated string. The length for
* the string is defined by the size parameter.
* String can contain ASCII characters from 32 to 126.
*
* Note: Returned string needs to be deallocated.
*
* \param size The length of the generated string
*
* \returns Newly allocated random string; or NULL if size was invalid or string could not be allocated.
*/
char * SDLTest_RandomAsciiStringOfSize(int size);

/**
* Returns the invocation count for the fuzzer since last ...FuzzerInit.
*/
Expand Down
3 changes: 0 additions & 3 deletions src/SDL_error.c
Expand Up @@ -235,9 +235,6 @@ SDL_Error(SDL_errorcode code)
case SDL_UNSUPPORTED:
SDL_SetError("That operation is not supported");
break;
case SDL_INVALIDPARAM:
SDL_SetError("Parameter is invalid");
break;
default:
SDL_SetError("Unknown SDL error");
break;
Expand Down
5 changes: 4 additions & 1 deletion src/events/SDL_keyboard.c
Expand Up @@ -895,17 +895,20 @@ SDL_Scancode SDL_GetScancodeFromName(const char *name)
int i;

if (!name || !*name) {
SDL_InvalidParamError("name");
return SDL_SCANCODE_UNKNOWN;
}

for (i = 0; i < SDL_arraysize(SDL_scancode_names); ++i) {
if (!SDL_scancode_names[i]) {
continue;
}
}
if (SDL_strcasecmp(name, SDL_scancode_names[i]) == 0) {
return (SDL_Scancode)i;
}
}

SDL_InvalidParamError("name");
return SDL_SCANCODE_UNKNOWN;
}

Expand Down
26 changes: 20 additions & 6 deletions src/test/SDL_test_fuzzer.c
Expand Up @@ -614,25 +614,37 @@ SDLTest_RandomDouble()
char *
SDLTest_RandomAsciiString()
{
// note: fuzzerInvocationCounter is increment in the RandomAsciiStringWithMaximumLenght
return SDLTest_RandomAsciiStringWithMaximumLength(255);
}

char *
SDLTest_RandomAsciiStringWithMaximumLength(int maxSize)
SDLTest_RandomAsciiStringWithMaximumLength(int maxLength)
{
int size;

if(maxLength < 1) {
SDL_InvalidParamError("maxLength");
return NULL;
}

size = (SDLTest_RandomUint32() % (maxLength + 1));

return SDLTest_RandomAsciiStringOfSize(size);
}

char *
SDLTest_RandomAsciiStringOfSize(int size)
{
char *string;
int counter;

fuzzerInvocationCounter++;

if(maxSize < 1) {
if(size < 1) {
SDL_InvalidParamError("size");
return NULL;
}

size = (SDLTest_RandomUint32() % (maxSize + 1)) + 1;
string = (char *)SDL_malloc(size * sizeof(char));
string = (char *)SDL_malloc((size + 1) * sizeof(char));
if (string==NULL) {
return NULL;
}
Expand All @@ -643,5 +655,7 @@ SDLTest_RandomAsciiStringWithMaximumLength(int maxSize)

string[counter] = '\0';

fuzzerInvocationCounter++;

return string;
}
2 changes: 1 addition & 1 deletion src/video/android/SDL_androidkeyboard.c
Expand Up @@ -318,7 +318,7 @@ Android_SetTextInputRect(_THIS, SDL_Rect *rect)
SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;

if (!rect) {
SDL_InvalidParamError();
SDL_InvalidParamError("rect");
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/video/cocoa/SDL_cocoakeyboard.m
Expand Up @@ -669,7 +669,7 @@ - (NSArray *) validAttributesForMarkedText
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;

if (!rect) {
SDL_InvalidParamError();
SDL_InvalidParamError("rect");
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/video/windows/SDL_windowskeyboard.c
Expand Up @@ -214,7 +214,7 @@ WIN_SetTextInputRect(_THIS, SDL_Rect *rect)
SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;

if (!rect) {
SDL_InvalidParamError();
SDL_InvalidParamError("rect");
return;
}

Expand Down
164 changes: 162 additions & 2 deletions test/testautomation_keyboard.c
Expand Up @@ -382,7 +382,7 @@ keyboard_setTextInputRect(void *arg)
int
keyboard_setTextInputRectNegative(void *arg)
{
const char *expectedError = "Parameter is invalid";
const char *expectedError = "Parameter 'rect' is invalid";
const char *error;

SDL_ClearError();
Expand All @@ -405,6 +405,157 @@ keyboard_setTextInputRectNegative(void *arg)
return TEST_COMPLETED;
}

/**
* @brief Check call to SDL_GetScancodeFromKey
*
* @sa http://wiki.libsdl.org/moin.cgi/SDL_GetScancodeFromKey
* @sa http://wiki.libsdl.org/moin.cgi/SDL_Keycode
*/
int
keyboard_getScancodeFromKey(void *arg)
{
SDL_Scancode scancode;

/* Regular key */
scancode = SDL_GetScancodeFromKey(SDLK_4);
SDLTest_AssertPass("Call to SDL_GetScancodeFromKey(SDLK_4)");
SDLTest_AssertCheck(scancode == SDL_SCANCODE_4, "Validate return value from SDL_GetScancodeFromKey, expected: %i, got: %i", SDL_SCANCODE_4, scancode);

/* Virtual key */
scancode = SDL_GetScancodeFromKey(SDLK_PLUS);
SDLTest_AssertPass("Call to SDL_GetScancodeFromKey(SDLK_PLUS)");
SDLTest_AssertCheck(scancode == 0, "Validate return value from SDL_GetScancodeFromKey, expected: 0, got: %i", scancode);

return TEST_COMPLETED;
}

/**
* @brief Check call to SDL_GetScancodeFromName
*
* @sa http://wiki.libsdl.org/moin.cgi/SDL_GetScancodeFromName
* @sa http://wiki.libsdl.org/moin.cgi/SDL_Keycode
*/
int
keyboard_getScancodeFromName(void *arg)
{
SDL_Scancode scancode;

/* Regular key, 1 character, first name in list */
scancode = SDL_GetScancodeFromName("A");
SDLTest_AssertPass("Call to SDL_GetScancodeFromName('A')");
SDLTest_AssertCheck(scancode == SDL_SCANCODE_A, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_A, scancode);

/* Regular key, 1 character */
scancode = SDL_GetScancodeFromName("4");
SDLTest_AssertPass("Call to SDL_GetScancodeFromName('4')");
SDLTest_AssertCheck(scancode == SDL_SCANCODE_4, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_4, scancode);

/* Regular key, 2 characters */
scancode = SDL_GetScancodeFromName("F1");
SDLTest_AssertPass("Call to SDL_GetScancodeFromName('F1')");
SDLTest_AssertCheck(scancode == SDL_SCANCODE_F1, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_F1, scancode);

/* Regular key, 3 characters */
scancode = SDL_GetScancodeFromName("End");
SDLTest_AssertPass("Call to SDL_GetScancodeFromName('End')");
SDLTest_AssertCheck(scancode == SDL_SCANCODE_END, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_END, scancode);

/* Regular key, 4 characters */
scancode = SDL_GetScancodeFromName("Find");
SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Find')");
SDLTest_AssertCheck(scancode == SDL_SCANCODE_FIND, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_FIND, scancode);

/* Regular key, several characters */
scancode = SDL_GetScancodeFromName("Backspace");
SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Backspace')");
SDLTest_AssertCheck(scancode == SDL_SCANCODE_BACKSPACE, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_BACKSPACE, scancode);

/* Regular key, several characters with space */
scancode = SDL_GetScancodeFromName("Keypad Enter");
SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Keypad Enter')");
SDLTest_AssertCheck(scancode == SDL_SCANCODE_KP_ENTER, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_KP_ENTER, scancode);

/* Regular key, last name in list */
scancode = SDL_GetScancodeFromName("Sleep");
SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Sleep')");
SDLTest_AssertCheck(scancode == SDL_SCANCODE_SLEEP, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_SLEEP, scancode);

return TEST_COMPLETED;
}

/**
* @brief Check call to SDL_GetScancodeFromName with invalid data
*
* @sa http://wiki.libsdl.org/moin.cgi/SDL_GetScancodeFromName
* @sa http://wiki.libsdl.org/moin.cgi/SDL_Keycode
*/
int
keyboard_getScancodeFromNameNegative(void *arg)
{
char *name;
SDL_Scancode scancode;
const char *expectedError = "Parameter 'name' is invalid";
const char *error;

SDL_ClearError();
SDLTest_AssertPass("Call to SDL_ClearError()");

/* Random string input */
name = SDLTest_RandomAsciiStringOfSize(32);
SDLTest_Assert(name != NULL, "Check that random name is not NULL");
if (name == NULL) {
return TEST_ABORTED;
}
scancode = SDL_GetScancodeFromName((const char *)name);
SDLTest_AssertPass("Call to SDL_GetScancodeFromName('%s')", name);
SDL_free(name);
SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_UNKNOWN, scancode);
error = SDL_GetError();
SDLTest_AssertPass("Call to SDL_GetError()");
SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
if (error != NULL) {
SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
"Validate error message, expected: '%s', got: '%s'", expectedError, error);
}

SDL_ClearError();
SDLTest_AssertPass("Call to SDL_ClearError()");

/* Zero length string input */
name = "";
scancode = SDL_GetScancodeFromName((const char *)name);
SDLTest_AssertPass("Call to SDL_GetScancodeFromName(NULL)");
SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_UNKNOWN, scancode);
error = SDL_GetError();
SDLTest_AssertPass("Call to SDL_GetError()");
SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
if (error != NULL) {
SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
"Validate error message, expected: '%s', got: '%s'", expectedError, error);
}

SDL_ClearError();
SDLTest_AssertPass("Call to SDL_ClearError()");

/* NULL input */
name = NULL;
scancode = SDL_GetScancodeFromName((const char *)name);
SDLTest_AssertPass("Call to SDL_GetScancodeFromName(NULL)");
SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_UNKNOWN, scancode);
error = SDL_GetError();
SDLTest_AssertPass("Call to SDL_GetError()");
SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
if (error != NULL) {
SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
"Validate error message, expected: '%s', got: '%s'", expectedError, error);
}

SDL_ClearError();
SDLTest_AssertPass("Call to SDL_ClearError()");

return TEST_COMPLETED;
}



/* ================= Test References ================== */
Expand Down Expand Up @@ -437,10 +588,19 @@ static const SDLTest_TestCaseReference keyboardTest8 =
static const SDLTest_TestCaseReference keyboardTest9 =
{ (SDLTest_TestCaseFp)keyboard_setTextInputRectNegative, "keyboard_setTextInputRectNegative", "Check call to SDL_SetTextInputRect with invalid data", TEST_ENABLED };

static const SDLTest_TestCaseReference keyboardTest10 =
{ (SDLTest_TestCaseFp)keyboard_getScancodeFromKey, "keyboard_getScancodeFromKey", "Check call to SDL_GetScancodeFromKey", TEST_ENABLED };

static const SDLTest_TestCaseReference keyboardTest11 =
{ (SDLTest_TestCaseFp)keyboard_getScancodeFromName, "keyboard_getScancodeFromName", "Check call to SDL_GetScancodeFromName", TEST_ENABLED };

static const SDLTest_TestCaseReference keyboardTest12 =
{ (SDLTest_TestCaseFp)keyboard_getScancodeFromNameNegative, "keyboard_getScancodeFromNameNegative", "Check call to SDL_GetScancodeFromName with invalid data", TEST_ENABLED };

/* Sequence of Keyboard test cases */
static const SDLTest_TestCaseReference *keyboardTests[] = {
&keyboardTest1, &keyboardTest2, &keyboardTest3, &keyboardTest4, &keyboardTest5, &keyboardTest6,
&keyboardTest7, &keyboardTest8, &keyboardTest9, NULL
&keyboardTest7, &keyboardTest8, &keyboardTest9, &keyboardTest10, &keyboardTest11, &keyboardTest12, NULL
};

/* Keyboard test suite (global) */
Expand Down

0 comments on commit e5dbcee

Please sign in to comment.