Add input validation to SDL_CalculateGammaRamp; add test coverage to Pixels suite; update test cases in Pixels suite
authorAndreas Schiffler <aschiffler@ferzkopp.net>
Sun, 05 May 2013 11:17:40 -0700
changeset 715199a4964db4c3
parent 7150 151aa63ab281
child 7152 e9ed46195235
Add input validation to SDL_CalculateGammaRamp; add test coverage to Pixels suite; update test cases in Pixels suite
src/video/SDL_pixels.c
test/testautomation_pixels.c
     1.1 --- a/src/video/SDL_pixels.c	Sun May 05 16:01:19 2013 +0200
     1.2 +++ b/src/video/SDL_pixels.c	Sun May 05 11:17:40 2013 -0700
     1.3 @@ -1090,9 +1090,19 @@
     1.4  SDL_CalculateGammaRamp(float gamma, Uint16 * ramp)
     1.5  {
     1.6      int i;
     1.7 +    
     1.8 +    /* Input validation */
     1.9 +    if (gamma < 0.0f ) {
    1.10 +      SDL_InvalidParamError("gamma");
    1.11 +      return;
    1.12 +    }
    1.13 +    if (ramp == NULL) {
    1.14 +      SDL_InvalidParamError("ramp");
    1.15 +      return;
    1.16 +    }
    1.17  
    1.18      /* 0.0 gamma is all black */
    1.19 -    if (gamma <= 0.0f) {
    1.20 +    if (gamma == 0.0f) {
    1.21          for (i = 0; i < 256; ++i) {
    1.22              ramp[i] = 0;
    1.23          }
     2.1 --- a/test/testautomation_pixels.c	Sun May 05 16:01:19 2013 +0200
     2.2 +++ b/test/testautomation_pixels.c	Sun May 05 11:17:40 2013 -0700
     2.3 @@ -108,6 +108,9 @@
     2.4  int
     2.5  pixels_allocFreeFormat(void *arg)
     2.6  {
     2.7 +  const char *expectedError = "Parameter 'format' is invalid";
     2.8 +  const char *error;   
     2.9 +  char message[256];
    2.10    int i;
    2.11    Uint32 format;
    2.12    Uint32 masks;
    2.13 @@ -149,15 +152,34 @@
    2.14    }
    2.15    
    2.16    /* Negative cases */
    2.17 +  
    2.18 +  /* Invalid Format */  
    2.19 +  SDL_ClearError();
    2.20 +  SDLTest_AssertPass("Call to SDL_ClearError()");
    2.21    format = 0xffffffff;
    2.22    result = SDL_AllocFormat(format);
    2.23    SDLTest_AssertPass("Call to SDL_AllocFormat(0xffffffff)");
    2.24    SDLTest_AssertCheck(result == NULL, "Verify result is NULL");  
    2.25 -  /* TODO: check error code */
    2.26 +  error = SDL_GetError();
    2.27 +  SDLTest_AssertPass("Call to SDL_GetError()");
    2.28 +  SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
    2.29 +  if (error != NULL) {
    2.30 +      SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
    2.31 +          "Validate error message, expected: '%s', got: '%s'", expectedError, error);
    2.32 +  }
    2.33  
    2.34 +  /* Invalid free pointer */
    2.35 +  SDL_ClearError();
    2.36 +  SDLTest_AssertPass("Call to SDL_ClearError()");
    2.37    SDL_FreeFormat(NULL);
    2.38    SDLTest_AssertPass("Call to SDL_FreeFormat(NULL)");
    2.39 -  /* TODO: check error code */
    2.40 +  error = SDL_GetError();
    2.41 +  SDLTest_AssertPass("Call to SDL_GetError()");
    2.42 +  SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
    2.43 +  if (error != NULL) {
    2.44 +      SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
    2.45 +          "Validate error message, expected: '%s', got: '%s'", expectedError, error);
    2.46 +  }
    2.47    
    2.48    return TEST_COMPLETED;
    2.49  }
    2.50 @@ -171,6 +193,10 @@
    2.51  int
    2.52  pixels_allocFreePalette(void *arg)
    2.53  {
    2.54 +  const char *expectedError1 = "Parameter 'ncolors' is invalid";
    2.55 +  const char *expectedError2 = "Parameter 'palette' is invalid";
    2.56 +  const char *error;   
    2.57 +  char message[256];
    2.58    int variation;
    2.59    int i;
    2.60    int ncolors;
    2.61 @@ -216,16 +242,140 @@
    2.62    }
    2.63  
    2.64    /* Negative cases */
    2.65 +    
    2.66 +  /* Invalid number of colors */
    2.67    for (ncolors = 0; ncolors > -3; ncolors--) {
    2.68 +    SDL_ClearError();
    2.69 +    SDLTest_AssertPass("Call to SDL_ClearError()");
    2.70      result = SDL_AllocPalette(ncolors);
    2.71      SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors);
    2.72      SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
    2.73 -    /* TODO: check error code */
    2.74 +    error = SDL_GetError();
    2.75 +    SDLTest_AssertPass("Call to SDL_GetError()");
    2.76 +    SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
    2.77 +    if (error != NULL) {
    2.78 +      SDLTest_AssertCheck(SDL_strcmp(error, expectedError1) == 0, 
    2.79 +          "Validate error message, expected: '%s', got: '%s'", expectedError1, error);
    2.80 +    }
    2.81    }
    2.82  
    2.83 +  /* Invalid free pointer */
    2.84 +  SDL_ClearError();
    2.85 +  SDLTest_AssertPass("Call to SDL_ClearError()");
    2.86    SDL_FreePalette(NULL);
    2.87    SDLTest_AssertPass("Call to SDL_FreePalette(NULL)");
    2.88 -  /* TODO: check error code */
    2.89 +  error = SDL_GetError();
    2.90 +  SDLTest_AssertPass("Call to SDL_GetError()");
    2.91 +  SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
    2.92 +  if (error != NULL) {
    2.93 +      SDLTest_AssertCheck(SDL_strcmp(error, expectedError2) == 0, 
    2.94 +          "Validate error message, expected: '%s', got: '%s'", expectedError2, error);
    2.95 +  }
    2.96 +    
    2.97 +  return TEST_COMPLETED;
    2.98 +}
    2.99 +
   2.100 +/**
   2.101 + * @brief Call to SDL_CalculateGammaRamp
   2.102 + *
   2.103 + * @sa http://wiki.libsdl.org/moin.fcg/SDL_CalculateGammaRamp
   2.104 + */
   2.105 +int
   2.106 +pixels_calcGammaRamp(void *arg)
   2.107 +{
   2.108 +  const char *expectedError1 = "Parameter 'gamma' is invalid";
   2.109 +  const char *expectedError2 = "Parameter 'ramp' is invalid";
   2.110 +  const char *error;   
   2.111 +  char message[256];
   2.112 +  float gamma;
   2.113 +  Uint16 *ramp;
   2.114 +  int variation;
   2.115 +  int i;
   2.116 +  int changed;
   2.117 +  Uint16 magic = 0xbeef;
   2.118 +
   2.119 +  /* Allocate temp ramp array and fill with some value*/
   2.120 +  ramp = (Uint16 *)SDL_malloc(256 * sizeof(Uint16));
   2.121 +  SDLTest_AssertCheck(ramp != NULL, "Validate temp ramp array could be allocated");
   2.122 +  if (ramp == NULL) return TEST_ABORTED;
   2.123 +  
   2.124 +  /* Make call with different gamma values */
   2.125 +  for (variation = 0; variation < 4; variation++) {
   2.126 +    switch (variation) {
   2.127 +      /* gamma = 0 all black */
   2.128 +      case 0:      
   2.129 +        gamma = 0.0f;
   2.130 +        break;
   2.131 +      /* gamma = 1 identity */
   2.132 +      case 1:
   2.133 +        gamma = 1.0f;
   2.134 +        break;
   2.135 +      /* gamma = ]0,1[ normal range */  
   2.136 +      case 2:
   2.137 +        gamma = 0.01f + 0.98f * SDLTest_RandomUnitFloat();
   2.138 +        break;
   2.139 +      /* gamma = >1.0 non-standard range */  
   2.140 +      case 3:
   2.141 +        gamma = 0.01f + 0.98f * SDLTest_RandomUnitFloat();
   2.142 +        break;
   2.143 +    }
   2.144 +
   2.145 +    /* Make call and check that values were updated */  
   2.146 +    for (i = 0; i < 256; i++) ramp[i] = magic;
   2.147 +    SDL_CalculateGammaRamp(gamma, ramp);
   2.148 +    SDLTest_AssertPass("Call to SDL_CalculateGammaRamp(%f)", gamma);
   2.149 +    changed = 0;
   2.150 +    for (i = 0; i < 256; i++) if (ramp[i] != magic) changed++;
   2.151 +    SDLTest_AssertCheck(changed > 250, "Validate that ramp was calculated; expected: >250 values changed, got: %d values changed", changed);
   2.152 +
   2.153 +    /* Additional value checks for some cases */
   2.154 +    i = SDLTest_RandomIntegerInRange(64,192);
   2.155 +    switch (variation) {
   2.156 +      case 0:
   2.157 +        SDLTest_AssertCheck(ramp[i] == 0, "Validate value at position %d; expected: 0, got: %d", i, ramp[i]);
   2.158 +        break;
   2.159 +      case 1:
   2.160 +        SDLTest_AssertCheck(ramp[i] == (i << 8) | i, "Validate value at position %d; expected: %d, got: %d", i, (i << 8) | i, ramp[i]);
   2.161 +        break;
   2.162 +      case 2:
   2.163 +      case 3:
   2.164 +        SDLTest_AssertCheck(ramp[i] > 0, "Validate value at position %d; expected: >0, got: %d", i, ramp[i]);
   2.165 +        break;
   2.166 +    }
   2.167 +  }
   2.168 +
   2.169 +  
   2.170 +  /* Negative cases */
   2.171 +  SDL_ClearError();
   2.172 +  SDLTest_AssertPass("Call to SDL_ClearError()");
   2.173 +  gamma = -1;
   2.174 +  for (i=0; i<256; i++) ramp[i] = magic;
   2.175 +  SDL_CalculateGammaRamp(gamma, ramp);
   2.176 +  SDLTest_AssertPass("Call to SDL_CalculateGammaRamp(%f)", gamma);
   2.177 +  error = SDL_GetError();
   2.178 +  SDLTest_AssertPass("Call to SDL_GetError()");
   2.179 +  SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   2.180 +  if (error != NULL) {
   2.181 +      SDLTest_AssertCheck(SDL_strcmp(error, expectedError1) == 0, 
   2.182 +          "Validate error message, expected: '%s', got: '%s'", expectedError1, error);
   2.183 +  }
   2.184 +  changed = 0;
   2.185 +  for (i = 0; i < 256; i++) if (ramp[i] != magic) changed++;
   2.186 +  SDLTest_AssertCheck(changed ==0, "Validate that ramp unchanged; expected: 0 values changed got: %d values changed", changed);
   2.187 +
   2.188 +  SDL_CalculateGammaRamp(0.5f, NULL);
   2.189 +  SDLTest_AssertPass("Call to SDL_CalculateGammaRamp(0.5,NULL)");
   2.190 +  error = SDL_GetError();
   2.191 +  SDLTest_AssertPass("Call to SDL_GetError()");
   2.192 +  SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   2.193 +  if (error != NULL) {
   2.194 +      SDLTest_AssertCheck(SDL_strcmp(error, expectedError2) == 0, 
   2.195 +          "Validate error message, expected: '%s', got: '%s'", expectedError2, error);
   2.196 +  }
   2.197 +  
   2.198 +  /* Cleanup */
   2.199 +  SDL_free(ramp);
   2.200 +    
   2.201      
   2.202    return TEST_COMPLETED;
   2.203  }
   2.204 @@ -239,9 +389,12 @@
   2.205  static const SDLTest_TestCaseReference pixelsTest2 =
   2.206  		{ (SDLTest_TestCaseFp)pixels_allocFreePalette, "pixels_allocFreePalette", "Call to SDL_AllocPalette and SDL_FreePalette", TEST_ENABLED };
   2.207  
   2.208 +static const SDLTest_TestCaseReference pixelsTest3 =
   2.209 +		{ (SDLTest_TestCaseFp)pixels_calcGammaRamp, "pixels_calcGammaRamp", "Call to SDL_CalculateGammaRamp", TEST_ENABLED };
   2.210 +
   2.211  /* Sequence of Pixels test cases */
   2.212  static const SDLTest_TestCaseReference *pixelsTests[] =  {
   2.213 -	&pixelsTest1, &pixelsTest2, NULL
   2.214 +	&pixelsTest1, &pixelsTest2, &pixelsTest3, NULL
   2.215  };
   2.216  
   2.217  /* Pixels test suite (global) */