From e5dbceef22077a6ec91d13753ff6cddba3fe09ee Mon Sep 17 00:00:00 2001 From: Andreas Schiffler Date: Sat, 12 Jan 2013 22:58:12 -0800 Subject: [PATCH] Update SDL_InvalidParamError to take param name; add additional fuzzer function; add new tests to keyboard test suite; improve surface test suite --- include/SDL_error.h | 3 +- include/SDL_test_fuzzer.h | 30 +++-- src/SDL_error.c | 3 - src/events/SDL_keyboard.c | 5 +- src/test/SDL_test_fuzzer.c | 26 +++- src/video/android/SDL_androidkeyboard.c | 2 +- src/video/cocoa/SDL_cocoakeyboard.m | 2 +- src/video/windows/SDL_windowskeyboard.c | 2 +- test/testautomation_keyboard.c | 164 +++++++++++++++++++++++- test/testautomation_surface.c | 83 ++++++++---- 10 files changed, 269 insertions(+), 51 deletions(-) diff --git a/include/SDL_error.h b/include/SDL_error.h index 5bec2afa8..47cfe91dd 100644 --- a/include/SDL_error.h +++ b/include/SDL_error.h @@ -52,7 +52,7 @@ 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, @@ -60,7 +60,6 @@ typedef enum SDL_EFWRITE, SDL_EFSEEK, SDL_UNSUPPORTED, - SDL_INVALIDPARAM, SDL_LASTERROR } SDL_errorcode; extern DECLSPEC void SDLCALL SDL_Error(SDL_errorcode code); diff --git a/include/SDL_test_fuzzer.h b/include/SDL_test_fuzzer.h index 8cd05f098..c56bdb410 100644 --- a/include/SDL_test_fuzzer.h +++ b/include/SDL_test_fuzzer.h @@ -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. */ diff --git a/src/SDL_error.c b/src/SDL_error.c index 30d3da67d..306dfe2ba 100644 --- a/src/SDL_error.c +++ b/src/SDL_error.c @@ -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; diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c index 0e96ad822..2a71a8050 100644 --- a/src/events/SDL_keyboard.c +++ b/src/events/SDL_keyboard.c @@ -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; } diff --git a/src/test/SDL_test_fuzzer.c b/src/test/SDL_test_fuzzer.c index 2e0a3de0d..92c1ae942 100644 --- a/src/test/SDL_test_fuzzer.c +++ b/src/test/SDL_test_fuzzer.c @@ -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; } @@ -643,5 +655,7 @@ SDLTest_RandomAsciiStringWithMaximumLength(int maxSize) string[counter] = '\0'; + fuzzerInvocationCounter++; + return string; } diff --git a/src/video/android/SDL_androidkeyboard.c b/src/video/android/SDL_androidkeyboard.c index be5a1d483..eb54f5cb1 100644 --- a/src/video/android/SDL_androidkeyboard.c +++ b/src/video/android/SDL_androidkeyboard.c @@ -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; } diff --git a/src/video/cocoa/SDL_cocoakeyboard.m b/src/video/cocoa/SDL_cocoakeyboard.m index 9c23ebae7..522224d44 100644 --- a/src/video/cocoa/SDL_cocoakeyboard.m +++ b/src/video/cocoa/SDL_cocoakeyboard.m @@ -669,7 +669,7 @@ - (NSArray *) validAttributesForMarkedText SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; if (!rect) { - SDL_InvalidParamError(); + SDL_InvalidParamError("rect"); return; } diff --git a/src/video/windows/SDL_windowskeyboard.c b/src/video/windows/SDL_windowskeyboard.c index fbe1a8349..80793b2e6 100644 --- a/src/video/windows/SDL_windowskeyboard.c +++ b/src/video/windows/SDL_windowskeyboard.c @@ -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; } diff --git a/test/testautomation_keyboard.c b/test/testautomation_keyboard.c index 02da89a76..61f412648 100644 --- a/test/testautomation_keyboard.c +++ b/test/testautomation_keyboard.c @@ -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(); @@ -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 ================== */ @@ -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) */ diff --git a/test/testautomation_surface.c b/test/testautomation_surface.c index a3c79d998..6979d9288 100644 --- a/test/testautomation_surface.c +++ b/test/testautomation_surface.c @@ -4,7 +4,7 @@ */ /* Supress C4996 VS compiler warnings for unlink() */ -#define _CRT_SECURE_NO_DEPRECATE +#define _CRT_SECURE_NO_DEPRECATE #define _CRT_NONSTDC_NO_DEPRECATE #include @@ -27,26 +27,37 @@ static SDL_Surface *testSurface = NULL; /* Fixture */ -/* Create a 32-bit writable surface for screen tests */ +/* Create a 32-bit writable surface for blitting tests */ void _surfaceSetUp(void *arg) { - Uint32 rmask, gmask, bmask, amask; -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - rmask = 0xff000000; - gmask = 0x00ff0000; - bmask = 0x0000ff00; - amask = 0x000000ff; -#else - rmask = 0x000000ff; - gmask = 0x0000ff00; - bmask = 0x00ff0000; - amask = 0xff000000; -#endif - - referenceSurface = SDLTest_ImageBlit(); /* For size info */ - testSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, referenceSurface->w, referenceSurface->h, 32, rmask, gmask, bmask, amask); + int result; + SDL_BlendMode blendMode = SDL_BLENDMODE_NONE; + SDL_BlendMode currentBlendMode; + Uint32 rmask, gmask, bmask, amask; +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + rmask = 0xff000000; + gmask = 0x00ff0000; + bmask = 0x0000ff00; + amask = 0x000000ff; +#else + rmask = 0x000000ff; + gmask = 0x0000ff00; + bmask = 0x00ff0000; + amask = 0xff000000; +#endif + + referenceSurface = SDLTest_ImageBlit(); /* For size info */ + testSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, referenceSurface->w, referenceSurface->h, 32, rmask, gmask, bmask, amask); SDLTest_AssertCheck(testSurface != NULL, "Check that testSurface is not NULL"); + if (testSurface != NULL) { + /* Disable blend mode for target surface */ + result = SDL_SetSurfaceBlendMode(testSurface, blendMode); + SDLTest_AssertCheck(result == 0, "Validate result from SDL_SetSurfaceBlendMode, expected: 0, got: %i", result); + result = SDL_GetSurfaceBlendMode(testSurface, ¤tBlendMode); + SDLTest_AssertCheck(result == 0, "Validate result from SDL_GetSurfaceBlendMode, expected: 0, got: %i", result); + SDLTest_AssertCheck(currentBlendMode == blendMode, "Validate blendMode, expected: %i, got: %i", blendMode, currentBlendMode); + } } void @@ -69,10 +80,10 @@ void _clearTestSurface() { int ret; Uint32 color; - + /* Clear surface. */ - color = SDL_MapRGB( testSurface->format, 0, 0, 0); - SDLTest_AssertPass("Call to SDL_MapRGB()"); + color = SDL_MapRGBA( testSurface->format, 0, 0, 0, 0); + SDLTest_AssertPass("Call to SDL_MapRGBA()"); ret = SDL_FillRect( testSurface, NULL, color); SDLTest_AssertPass("Call to SDL_FillRect()"); SDLTest_AssertCheck(ret == 0, "Verify result from SDL_FillRect, expected: 0, got: %i", ret); @@ -103,6 +114,21 @@ void _testBlitBlendMode(int mode) SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL"); if (face == NULL) return; + /* Reset alpha modulation */ + ret = SDL_SetSurfaceAlphaMod(face, 255); + SDLTest_AssertPass("Call to SDL_SetSurfaceAlphaMod()"); + SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceAlphaMod(), expected: 0, got: %i", ret); + + /* Reset color modulation */ + ret = SDL_SetSurfaceColorMod(face, 255, 255, 255); + SDLTest_AssertPass("Call to SDL_SetSurfaceColorMod()"); + SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceColorMod(), expected: 0, got: %i", ret); + + /* Reset color key */ + ret = SDL_SetColorKey(face, SDL_FALSE, 0); + SDLTest_AssertPass("Call to SDL_SetColorKey()"); + SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetColorKey(), expected: 0, got: %i", ret); + /* Clear the test surface */ _clearTestSurface(); @@ -141,11 +167,16 @@ void _testBlitBlendMode(int mode) else if (mode == -4) { /* Crazy blending mode magic. */ nmode = (i/4*j/4) % 4; - if (nmode==0) bmode = SDL_BLENDMODE_NONE; - else if (nmode==1) bmode = SDL_BLENDMODE_BLEND; - else if (nmode==2) bmode = SDL_BLENDMODE_ADD; - else if (nmode==3) bmode = SDL_BLENDMODE_MOD; - ret = SDL_SetSurfaceBlendMode( face, bmode ); + if (nmode==0) { + bmode = SDL_BLENDMODE_NONE; + } else if (nmode==1) { + bmode = SDL_BLENDMODE_BLEND; + } else if (nmode==2) { + bmode = SDL_BLENDMODE_ADD; + } else if (nmode==3) { + bmode = SDL_BLENDMODE_MOD; + } + ret = SDL_SetSurfaceBlendMode( face, bmode ); if (ret != 0) checkFailCount4++; } @@ -473,7 +504,7 @@ surface_testBlitBlendLoop(void *arg) { int ret; SDL_Surface *referenceSurface; - /* All blitting */ + /* All blitting modes */ _testBlitBlendMode(-4); /* Verify result by comparing surfaces */