Add additional input validation to SDL_GetWindowDisplayMode; add tests to video suite
authorAndreas Schiffler <aschiffler@ferzkopp.net>
Thu, 28 Feb 2013 09:11:04 -0800
changeset 693676d9c31e823d
parent 6934 3ce238619067
child 6937 6a0e4dc3a489
child 6946 d25107785792
Add additional input validation to SDL_GetWindowDisplayMode; add tests to video suite
src/video/SDL_video.c
test/testautomation_video.c
     1.1 --- a/src/video/SDL_video.c	Tue Feb 26 23:26:00 2013 -0800
     1.2 +++ b/src/video/SDL_video.c	Thu Feb 28 09:11:04 2013 -0800
     1.3 @@ -1020,6 +1020,11 @@
     1.4      SDL_DisplayMode fullscreen_mode;
     1.5  	SDL_VideoDisplay *display;
     1.6  
     1.7 +    if (!mode) {
     1.8 +      SDL_InvalidParamError("mode");
     1.9 +      return -1;
    1.10 +    }
    1.11 +    
    1.12      CHECK_WINDOW_MAGIC(window, -1);
    1.13  
    1.14      fullscreen_mode = window->fullscreen_mode;
     2.1 --- a/test/testautomation_video.c	Tue Feb 26 23:26:00 2013 -0800
     2.2 +++ b/test/testautomation_video.c	Thu Feb 28 09:11:04 2013 -0800
     2.3 @@ -497,6 +497,8 @@
     2.4  
     2.5  /**
     2.6   * @brief Tests call to SDL_GetWindowBrightness
     2.7 + *
     2.8 +* @sa http://wiki.libsdl.org/moin.fcg/SDL_GetWindowBrightness
     2.9   */
    2.10  int
    2.11  video_getWindowBrightness(void *arg)
    2.12 @@ -519,6 +521,218 @@
    2.13    return TEST_COMPLETED;
    2.14  }
    2.15  
    2.16 +/**
    2.17 + * @brief Tests call to SDL_GetWindowBrightness with invalid input
    2.18 + *
    2.19 +* @sa http://wiki.libsdl.org/moin.fcg/SDL_GetWindowBrightness
    2.20 + */
    2.21 +int
    2.22 +video_getWindowBrightnessNegative(void *arg)
    2.23 +{
    2.24 +  const char *invalidWindowError = "Invalid window";
    2.25 +  char *lastError;
    2.26 +  const char* title = "video_getWindowBrightnessNegative Test Window";
    2.27 +  float result;
    2.28 +
    2.29 +  /* Call against invalid window */ 
    2.30 +  result = SDL_GetWindowBrightness(NULL);
    2.31 +  SDLTest_AssertPass("Call to SDL_GetWindowBrightness(window=NULL)");
    2.32 +  SDLTest_AssertCheck(result == 1.0, "Validate result value; expected: 1.0, got: %f", result);
    2.33 +  lastError = (char *)SDL_GetError();
    2.34 +  SDLTest_AssertPass("SDL_GetError()");
    2.35 +  SDLTest_AssertCheck(lastError != NULL, "Verify error message is not NULL");
    2.36 +  if (lastError != NULL) {
    2.37 +      SDLTest_AssertCheck(SDL_strcmp(lastError, invalidWindowError) == 0,
    2.38 +         "SDL_GetError(): expected message '%s', was message: '%s'",
    2.39 +         invalidWindowError,
    2.40 +         lastError);
    2.41 +  }
    2.42 +
    2.43 +  return TEST_COMPLETED;
    2.44 +}
    2.45 +
    2.46 +/**
    2.47 + * @brief Tests call to SDL_GetWindowDisplayMode
    2.48 + *
    2.49 + * @sa http://wiki.libsdl.org/moin.fcg/SDL_GetWindowDisplayMode
    2.50 + */
    2.51 +int
    2.52 +video_getWindowDisplayMode(void *arg)
    2.53 +{
    2.54 +  SDL_Window* window;
    2.55 +  const char* title = "video_getWindowDisplayMode Test Window";
    2.56 +  SDL_DisplayMode mode;
    2.57 +  int result;
    2.58 +
    2.59 +  /* Invalidate part of the mode content so we can check values later */
    2.60 +  mode.w = -1;
    2.61 +  mode.h = -1;
    2.62 +  mode.refresh_rate = -1;
    2.63 +
    2.64 +  /* Call against new test window */ 
    2.65 +  window = _createVideoSuiteTestWindow(title);
    2.66 +  if (window != NULL) {
    2.67 +      result = SDL_GetWindowDisplayMode(window, &mode);
    2.68 +      SDLTest_AssertPass("Call to SDL_GetWindowDisplayMode");
    2.69 +      SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0, got: %d", result);
    2.70 +      SDLTest_AssertCheck(mode.w > 0, "Validate mode.w content; expected: >0, got: %d", mode.w);
    2.71 +      SDLTest_AssertCheck(mode.h > 0, "Validate mode.h content; expected: >0, got: %d", mode.h);
    2.72 +      SDLTest_AssertCheck(mode.refresh_rate > 0, "Validate mode.refresh_rate content; expected: >0, got: %d", mode.refresh_rate);
    2.73 +  }
    2.74 +
    2.75 +  /* Clean up */    
    2.76 +  _destroyVideoSuiteTestWindow(window);
    2.77 +  
    2.78 +  return TEST_COMPLETED;
    2.79 +}
    2.80 +
    2.81 +/**
    2.82 + * @brief Tests call to SDL_GetWindowDisplayMode with invalid input
    2.83 + *
    2.84 + * @sa http://wiki.libsdl.org/moin.fcg/SDL_GetWindowDisplayMode
    2.85 + */
    2.86 +int
    2.87 +video_getWindowDisplayModeNegative(void *arg)
    2.88 +{
    2.89 +  const char *expectedError = "Parameter 'mode' is invalid";
    2.90 +  const char *invalidWindowError = "Invalid window";
    2.91 +  char *lastError;
    2.92 +  SDL_Window* window;
    2.93 +  const char* title = "video_getWindowDisplayModeNegative Test Window";
    2.94 +  SDL_DisplayMode mode;
    2.95 +  int result;
    2.96 +
    2.97 +  /* Call against new test window */ 
    2.98 +  window = _createVideoSuiteTestWindow(title);
    2.99 +  if (window != NULL) {
   2.100 +      result = SDL_GetWindowDisplayMode(window, NULL);
   2.101 +      SDLTest_AssertPass("Call to SDL_GetWindowDisplayMode(...,mode=NULL)");
   2.102 +      SDLTest_AssertCheck(result == -1, "Validate result value; expected: -1, got: %d", result);
   2.103 +      lastError = (char *)SDL_GetError();
   2.104 +      SDLTest_AssertPass("SDL_GetError()");
   2.105 +      SDLTest_AssertCheck(lastError != NULL, "Verify error message is not NULL");
   2.106 +      if (lastError != NULL) {
   2.107 +      	SDLTest_AssertCheck(SDL_strcmp(lastError, expectedError) == 0,
   2.108 +             "SDL_GetError(): expected message '%s', was message: '%s'",
   2.109 +             expectedError,
   2.110 +             lastError);
   2.111 +      }
   2.112 +  }
   2.113 +
   2.114 +  /* Clean up */    
   2.115 +  _destroyVideoSuiteTestWindow(window);
   2.116 +  
   2.117 +  /* Call against invalid window */
   2.118 +  result = SDL_GetWindowDisplayMode(NULL, &mode);
   2.119 +  SDLTest_AssertPass("Call to SDL_GetWindowDisplayMode(window=NULL,...)");
   2.120 +  SDLTest_AssertCheck(result == -1, "Validate result value; expected: -1, got: %d", result);
   2.121 +  lastError = (char *)SDL_GetError();
   2.122 +  SDLTest_AssertPass("SDL_GetError()");
   2.123 +  SDLTest_AssertCheck(lastError != NULL, "Verify error message is not NULL");
   2.124 +  if (lastError != NULL) {
   2.125 +      SDLTest_AssertCheck(SDL_strcmp(lastError, invalidWindowError) == 0,
   2.126 +         "SDL_GetError(): expected message '%s', was message: '%s'",
   2.127 +         invalidWindowError,
   2.128 +         lastError);
   2.129 +  }
   2.130 +  
   2.131 +  return TEST_COMPLETED;
   2.132 +}
   2.133 +
   2.134 +/**
   2.135 + * @brief Tests call to SDL_GetWindowGammaRamp
   2.136 + *
   2.137 + * @sa http://wiki.libsdl.org/moin.fcg/SDL_GetWindowGammaRamp
   2.138 + */
   2.139 +int
   2.140 +video_getWindowGammaRamp(void *arg)
   2.141 +{
   2.142 +  SDL_Window* window;
   2.143 +  const char* title = "video_getWindowGammaRamp Test Window";
   2.144 +  Uint16 red[256];
   2.145 +  Uint16 green[256];
   2.146 +  Uint16 blue[256];
   2.147 +  int result;
   2.148 +
   2.149 +  /* Call against new test window */ 
   2.150 +  window = _createVideoSuiteTestWindow(title);
   2.151 +  if (window != NULL) {
   2.152 +      /* Retrieve no channel */
   2.153 +      result = SDL_GetWindowGammaRamp(window, NULL, NULL, NULL);
   2.154 +      SDLTest_AssertPass("Call to SDL_GetWindowGammaRamp(all NULL)");
   2.155 +      SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0, got: %d", result);
   2.156 +
   2.157 +      /* Retrieve single channel */
   2.158 +      result = SDL_GetWindowGammaRamp(window, red, NULL, NULL);
   2.159 +      SDLTest_AssertPass("Call to SDL_GetWindowGammaRamp(r)");
   2.160 +      SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0, got: %d", result);
   2.161 +
   2.162 +      result = SDL_GetWindowGammaRamp(window, NULL, green, NULL);
   2.163 +      SDLTest_AssertPass("Call to SDL_GetWindowGammaRamp(g)");
   2.164 +      SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0, got: %d", result);
   2.165 +
   2.166 +      result = SDL_GetWindowGammaRamp(window, NULL, NULL, blue);
   2.167 +      SDLTest_AssertPass("Call to SDL_GetWindowGammaRamp(b)");
   2.168 +      SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0, got: %d", result);
   2.169 +
   2.170 +      /* Retrieve two channels */
   2.171 +      result = SDL_GetWindowGammaRamp(window, red, green, NULL);
   2.172 +      SDLTest_AssertPass("Call to SDL_GetWindowGammaRamp(r, g)");
   2.173 +      SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0, got: %d", result);
   2.174 +
   2.175 +      result = SDL_GetWindowGammaRamp(window, NULL, green, blue);
   2.176 +      SDLTest_AssertPass("Call to SDL_GetWindowGammaRamp(g,b)");
   2.177 +      SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0, got: %d", result);
   2.178 +
   2.179 +      result = SDL_GetWindowGammaRamp(window, red, NULL, blue);
   2.180 +      SDLTest_AssertPass("Call to SDL_GetWindowGammaRamp(r,b)");
   2.181 +      SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0, got: %d", result);
   2.182 +
   2.183 +      /* Retrieve all channels */
   2.184 +      result = SDL_GetWindowGammaRamp(window, red, green, blue);
   2.185 +      SDLTest_AssertPass("Call to SDL_GetWindowGammaRamp(r,g,b)");
   2.186 +      SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0, got: %d", result);
   2.187 +  }
   2.188 +
   2.189 +  /* Clean up */    
   2.190 +  _destroyVideoSuiteTestWindow(window);
   2.191 +  
   2.192 +  return TEST_COMPLETED;
   2.193 +}
   2.194 +
   2.195 +/**
   2.196 + * @brief Tests call to SDL_GetWindowGammaRamp with invalid input
   2.197 + *
   2.198 +* @sa http://wiki.libsdl.org/moin.fcg/SDL_GetWindowGammaRamp
   2.199 + */
   2.200 +int
   2.201 +video_getWindowGammaRampNegative(void *arg)
   2.202 +{
   2.203 +  const char *invalidWindowError = "Invalid window";
   2.204 +  char *lastError;
   2.205 +  const char* title = "video_getWindowGammaRampNegative Test Window";
   2.206 +  Uint16 red[256];
   2.207 +  Uint16 green[256];
   2.208 +  Uint16 blue[256];
   2.209 +  int result;
   2.210 +
   2.211 +  /* Call against invalid window */ 
   2.212 +  result = SDL_GetWindowGammaRamp(NULL, red, green, blue);
   2.213 +  SDLTest_AssertPass("Call to SDL_GetWindowGammaRamp(window=NULL,r,g,b)");
   2.214 +  SDLTest_AssertCheck(result == -1, "Validate result value; expected: -1, got: %f", result);
   2.215 +  lastError = (char *)SDL_GetError();
   2.216 +  SDLTest_AssertPass("SDL_GetError()");
   2.217 +  SDLTest_AssertCheck(lastError != NULL, "Verify error message is not NULL");
   2.218 +  if (lastError != NULL) {
   2.219 +      SDLTest_AssertCheck(SDL_strcmp(lastError, invalidWindowError) == 0,
   2.220 +         "SDL_GetError(): expected message '%s', was message: '%s'",
   2.221 +         invalidWindowError,
   2.222 +         lastError);
   2.223 +  }
   2.224 +
   2.225 +  return TEST_COMPLETED;
   2.226 +}
   2.227 +
   2.228  /* ================= Test References ================== */
   2.229  
   2.230  /* Video test cases */
   2.231 @@ -552,10 +766,26 @@
   2.232  static const SDLTest_TestCaseReference videoTest10 =
   2.233  		{ (SDLTest_TestCaseFp)video_getWindowBrightness, "video_getWindowBrightness",  "Get window brightness", TEST_ENABLED };
   2.234  
   2.235 +static const SDLTest_TestCaseReference videoTest11 =
   2.236 +		{ (SDLTest_TestCaseFp)video_getWindowBrightnessNegative, "video_getWindowBrightnessNegative",  "Get window brightness with invalid input", TEST_ENABLED };
   2.237 +
   2.238 +static const SDLTest_TestCaseReference videoTest12 =
   2.239 +		{ (SDLTest_TestCaseFp)video_getWindowDisplayMode, "video_getWindowDisplayMode",  "Get window display mode", TEST_ENABLED };
   2.240 +
   2.241 +static const SDLTest_TestCaseReference videoTest13 =
   2.242 +		{ (SDLTest_TestCaseFp)video_getWindowDisplayModeNegative, "video_getWindowDisplayModeNegative",  "Get window display mode with invalid input", TEST_ENABLED };
   2.243 +
   2.244 +static const SDLTest_TestCaseReference videoTest14 =
   2.245 +		{ (SDLTest_TestCaseFp)video_getWindowGammaRamp, "video_getWindowGammaRamp",  "Get window gamma ramp", TEST_ENABLED };
   2.246 +
   2.247 +static const SDLTest_TestCaseReference videoTest15 =
   2.248 +		{ (SDLTest_TestCaseFp)video_getWindowGammaRampNegative, "video_getWindowGammaRampNegative",  "Get window gamma ramp against invalid input", TEST_ENABLED };
   2.249 +
   2.250  /* Sequence of Video test cases */
   2.251  static const SDLTest_TestCaseReference *videoTests[] =  {
   2.252  	&videoTest1, &videoTest2, &videoTest3, &videoTest4, &videoTest5, &videoTest6, 
   2.253 -	&videoTest7, &videoTest8, &videoTest9, &videoTest10, NULL
   2.254 +	&videoTest7, &videoTest8, &videoTest9, &videoTest10, &videoTest11, &videoTest12, 
   2.255 +	&videoTest13, &videoTest14, &videoTest15, NULL
   2.256  };
   2.257  
   2.258  /* Video test suite (global) */