Add input validation to SDL_GetKeyFromScancode; fix compiler warning in testautomation driver on Linux; update keyboard test suite
authorAndreas Schiffler <aschiffler@ferzkopp.net>
Sun, 13 Jan 2013 22:55:26 -0800
changeset 6813b21879e67db2
parent 6812 1f92a4ad24e7
child 6814 fccdef204fcf
Add input validation to SDL_GetKeyFromScancode; fix compiler warning in testautomation driver on Linux; update keyboard test suite
src/events/SDL_keyboard.c
test/testautomation.c
test/testautomation_keyboard.c
     1.1 --- a/src/events/SDL_keyboard.c	Sun Jan 13 14:31:53 2013 -0800
     1.2 +++ b/src/events/SDL_keyboard.c	Sun Jan 13 22:55:26 2013 -0800
     1.3 @@ -860,6 +860,11 @@
     1.4  SDL_GetKeyFromScancode(SDL_Scancode scancode)
     1.5  {
     1.6      SDL_Keyboard *keyboard = &SDL_keyboard;
     1.7 +    
     1.8 +    if (scancode<SDL_SCANCODE_UNKNOWN || scancode >= SDL_NUM_SCANCODES) {
     1.9 +          SDL_InvalidParamError("scancode");
    1.10 +          return 0;
    1.11 +    }
    1.12  
    1.13      return keyboard->keymap[scancode];
    1.14  }
     2.1 --- a/test/testautomation.c	Sun Jan 13 14:31:53 2013 -0800
     2.2 +++ b/test/testautomation.c	Sun Jan 13 22:55:26 2013 -0800
     2.3 @@ -61,7 +61,7 @@
     2.4              } 
     2.5              else if (SDL_strcasecmp(argv[i], "--execKey") == 0) {
     2.6                  if (argv[i + 1]) {
     2.7 -                    SDL_sscanf(argv[i + 1], "%llu", &userExecKey);
     2.8 +                    SDL_sscanf(argv[i + 1], "%llu", (long long unsigned int *)&userExecKey);
     2.9                      consumed = 2;                    
    2.10                  }
    2.11              } 
     3.1 --- a/test/testautomation_keyboard.c	Sun Jan 13 14:31:53 2013 -0800
     3.2 +++ b/test/testautomation_keyboard.c	Sun Jan 13 22:55:26 2013 -0800
     3.3 @@ -4,6 +4,7 @@
     3.4  
     3.5  #include <stdio.h>
     3.6  
     3.7 +#include "SDL_config.h"
     3.8  #include "SDL.h"
     3.9  #include "SDL_test.h"
    3.10  
    3.11 @@ -110,6 +111,8 @@
    3.12  int
    3.13  keyboard_getKeyFromScancode(void *arg)
    3.14  {
    3.15 +   const char *expectedError = "Parameter 'scancode' is invalid";
    3.16 +   const char *error;   
    3.17     SDL_Keycode result;
    3.18  
    3.19     /* Case where input is valid */
    3.20 @@ -119,13 +122,41 @@
    3.21  
    3.22     /* Case where input is zero */
    3.23     result = SDL_GetKeyFromScancode(0);
    3.24 -   SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(zero)");
    3.25 +   SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(0)");
    3.26     SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
    3.27  
    3.28 -   /* Case where input is invalid */
    3.29 +   SDL_ClearError();
    3.30 +   SDLTest_AssertPass("Call to SDL_ClearError()");
    3.31 +
    3.32 +   /* Case where input is invalid (too small) */
    3.33     result = SDL_GetKeyFromScancode(-999);
    3.34 -   SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(invalid)");
    3.35 +   SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(-999)");
    3.36     SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
    3.37 +   error = SDL_GetError();
    3.38 +   SDLTest_AssertPass("Call to SDL_GetError()");
    3.39 +   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
    3.40 +   if (error != NULL) {
    3.41 +      SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
    3.42 +          "Validate error message, expected: '%s', got: '%s'", expectedError, error);
    3.43 +   }
    3.44 +
    3.45 +   SDL_ClearError();
    3.46 +   SDLTest_AssertPass("Call to SDL_ClearError()");
    3.47 +
    3.48 +   /* Case where input is invalid (too big) */
    3.49 +   result = SDL_GetKeyFromScancode(999);
    3.50 +   SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(999)");
    3.51 +   SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
    3.52 +   error = SDL_GetError();
    3.53 +   SDLTest_AssertPass("Call to SDL_GetError()");
    3.54 +   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
    3.55 +   if (error != NULL) {
    3.56 +      SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
    3.57 +          "Validate error message, expected: '%s', got: '%s'", expectedError, error);
    3.58 +   }
    3.59 +
    3.60 +   SDL_ClearError();
    3.61 +   SDLTest_AssertPass("Call to SDL_ClearError()");
    3.62  
    3.63     return TEST_COMPLETED;
    3.64  }
    3.65 @@ -382,15 +413,21 @@
    3.66  int
    3.67  keyboard_setTextInputRectNegative(void *arg)
    3.68  {      
    3.69 +   /* Some platforms set also an error message; prepare for checking it */
    3.70 +#if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_COCOA  
    3.71     const char *expectedError = "Parameter 'rect' is invalid";
    3.72     const char *error;
    3.73     
    3.74     SDL_ClearError();
    3.75     SDLTest_AssertPass("Call to SDL_ClearError()");
    3.76 +#endif
    3.77     
    3.78     /* NULL refRect */
    3.79     SDL_SetTextInputRect(NULL);
    3.80     SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)");
    3.81 +
    3.82 +   /* Some platforms set also an error message; so check it */
    3.83 +#if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_COCOA  
    3.84     error = SDL_GetError();
    3.85     SDLTest_AssertPass("Call to SDL_GetError()");
    3.86     SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
    3.87 @@ -401,6 +438,7 @@
    3.88  
    3.89     SDL_ClearError();
    3.90     SDLTest_AssertPass("Call to SDL_ClearError()");
    3.91 +#endif
    3.92        
    3.93     return TEST_COMPLETED;
    3.94  }