Add additional input validation to SDL_BuildAudioCVT; add additional tests to automation (audio, rwops)
authorAndreas Schiffler <aschiffler@ferzkopp.net>
Mon, 21 Jan 2013 09:16:27 -0800
changeset 6819afef68703f54
parent 6818 6f17d1353ffa
child 6820 0ce561f6c805
Add additional input validation to SDL_BuildAudioCVT; add additional tests to automation (audio, rwops)
src/audio/SDL_audiocvt.c
test/testautomation_audio.c
test/testautomation_rwops.c
     1.1 --- a/src/audio/SDL_audiocvt.c	Mon Jan 21 03:33:42 2013 -0500
     1.2 +++ b/src/audio/SDL_audiocvt.c	Mon Jan 21 09:16:27 2013 -0800
     1.3 @@ -968,6 +968,12 @@
     1.4       * !!! FIXME: good in practice as it sounds in theory, though.
     1.5       */
     1.6  
     1.7 +    /* Sanity check target pointer */
     1.8 +    if (cvt == NULL) {
     1.9 +        SDL_InvalidParamError("cvt");
    1.10 +        return -1;
    1.11 +    }
    1.12 +    
    1.13      /* there are no unsigned types over 16 bits, so catch this up front. */
    1.14      if ((SDL_AUDIO_BITSIZE(src_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(src_fmt))) {
    1.15          SDL_SetError("Invalid source format");
    1.16 @@ -979,6 +985,10 @@
    1.17      }
    1.18  
    1.19      /* prevent possible divisions by zero, etc. */
    1.20 +    if ((src_channels == 0) || (dst_channels == 0)) {
    1.21 +        SDL_SetError("Source or destination channels is zero");
    1.22 +        return -1;
    1.23 +    }
    1.24      if ((src_rate == 0) || (dst_rate == 0)) {
    1.25          SDL_SetError("Source or destination rate is zero");
    1.26          return -1;
     2.1 --- a/test/testautomation_audio.c	Mon Jan 21 03:33:42 2013 -0500
     2.2 +++ b/test/testautomation_audio.c	Mon Jan 21 09:16:27 2013 -0800
     2.3 @@ -4,6 +4,7 @@
     2.4   */
     2.5  
     2.6  #include <stdio.h>
     2.7 +#include <string.h>
     2.8  
     2.9  #include "SDL.h"
    2.10  #include "SDL_test.h"
    2.11 @@ -133,7 +134,10 @@
    2.12  
    2.13  
    2.14  /**
    2.15 - * @brief Checks available audio driver names.
    2.16 + * \brief Checks available audio driver names.
    2.17 + *
    2.18 + * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetNumAudioDrivers
    2.19 + * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetAudioDriver
    2.20   */
    2.21  int audio_printAudioDrivers()
    2.22  {
    2.23 @@ -163,7 +167,9 @@
    2.24  
    2.25  
    2.26  /**
    2.27 - * @brief Checks current audio driver name with initialized audio.
    2.28 + * \brief Checks current audio driver name with initialized audio.
    2.29 + *
    2.30 + * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetCurrentAudioDriver
    2.31   */
    2.32  int audio_printCurrentAudioDriver()
    2.33  {
    2.34 @@ -180,6 +186,316 @@
    2.35     return TEST_COMPLETED;
    2.36  }
    2.37  
    2.38 +
    2.39 +/**
    2.40 + * \brief Builds various audio conversion structures
    2.41 + *
    2.42 + * \sa http://wiki.libsdl.org/moin.cgi/SDL_BuildAudioCVT
    2.43 + */
    2.44 +int audio_buildAudioCVT()
    2.45 +{
    2.46 +  int result;
    2.47 +  SDL_AudioCVT  cvt;
    2.48 +  SDL_AudioSpec spec1;
    2.49 +  SDL_AudioSpec spec2;
    2.50 +  int i, j, k;
    2.51 +  const int numFormats = 18;
    2.52 +  SDL_AudioFormat formats[] = { AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S16SYS, AUDIO_S16, AUDIO_U16LSB, 
    2.53 +                                AUDIO_U16MSB, AUDIO_U16SYS, AUDIO_U16, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S32SYS, AUDIO_S32, 
    2.54 +                                AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_F32SYS, AUDIO_F32 };
    2.55 +  const int numChannels = 4;
    2.56 +  Uint8 channels[] = { 1, 2, 4, 6 };
    2.57 +  const int numFrequencies = 4;
    2.58 +  int frequencies[] = { 11025, 22050, 44100, 48000 };
    2.59 +  
    2.60 +  /* No conversion needed */
    2.61 +  spec1.format = AUDIO_S16LSB;
    2.62 +  spec1.channels = 2;
    2.63 +  spec1.freq = 22050;
    2.64 +  result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
    2.65 +                                   spec1.format, spec1.channels, spec1.freq);
    2.66 +  SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec1)");
    2.67 +  SDLTest_AssertCheck(result == 0, "Verify result value; expected: 0, got: %i", result);
    2.68 +
    2.69 +  /* Typical conversion */
    2.70 +  spec1.format = AUDIO_S8;
    2.71 +  spec1.channels = 1;
    2.72 +  spec1.freq = 22050;
    2.73 +  spec2.format = AUDIO_S16LSB;
    2.74 +  spec2.channels = 2;
    2.75 +  spec2.freq = 44100;  
    2.76 +  result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
    2.77 +                                   spec2.format, spec2.channels, spec2.freq);
    2.78 +  SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec2)");
    2.79 +  SDLTest_AssertCheck(result == 1, "Verify result value; expected: 1, got: %i", result);
    2.80 +
    2.81 +  /* All source conversions with random conversion targets */
    2.82 +  for (i=0; i<numFormats; i++) {
    2.83 +    for (j=0; j<numChannels; j++) {
    2.84 +      for (k=0; k<numFrequencies; k++) {
    2.85 +        spec1.format = formats[i];
    2.86 +        spec1.channels = channels[j];
    2.87 +        spec1.freq = frequencies[k];
    2.88 +        spec2.format = formats[SDLTest_RandomIntegerInRange(0, numFormats - 1)];
    2.89 +        spec2.channels = channels[SDLTest_RandomIntegerInRange(0, numChannels - 1)];
    2.90 +        spec2.freq = frequencies[SDLTest_RandomIntegerInRange(0, numFrequencies - 1)];
    2.91 +        result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
    2.92 +                                        spec2.format, spec2.channels, spec2.freq);
    2.93 +        SDLTest_AssertPass("Call to SDL_BuildAudioCVT(format=%i,channels=%i,freq=%i ==> format=%i,channels=%i,freq=%i)", 
    2.94 +            spec1.format, spec1.channels, spec1.freq, spec2.format, spec2.channels, spec2.freq);
    2.95 +        SDLTest_AssertCheck(result == 0 || result == 1, "Verify result value; expected: 0 or 1, got: %i", result);
    2.96 +        if (result<0) {
    2.97 +          SDLTest_LogError(SDL_GetError());
    2.98 +        }
    2.99 +      }
   2.100 +    }
   2.101 +  }
   2.102 +
   2.103 +   return TEST_COMPLETED;
   2.104 +}
   2.105 +
   2.106 +/**
   2.107 + * \brief Checkes calls with invalid input to SDL_BuildAudioCVT
   2.108 + *
   2.109 + * \sa http://wiki.libsdl.org/moin.cgi/SDL_BuildAudioCVT
   2.110 + */
   2.111 +int audio_buildAudioCVTNegative()
   2.112 +{
   2.113 +  const char *expectedError = "Parameter 'cvt' is invalid";
   2.114 +  const char *error;   
   2.115 +  int result;
   2.116 +  SDL_AudioCVT  cvt;
   2.117 +  SDL_AudioSpec spec1;
   2.118 +  SDL_AudioSpec spec2;
   2.119 +  int i;
   2.120 +  char message[256];
   2.121 +
   2.122 +  /* Valid format */  
   2.123 +  spec1.format = AUDIO_S8;
   2.124 +  spec1.channels = 1;
   2.125 +  spec1.freq = 22050;
   2.126 +  spec2.format = AUDIO_S16LSB;
   2.127 +  spec2.channels = 2;
   2.128 +  spec2.freq = 44100;  
   2.129 +
   2.130 +  SDL_ClearError();
   2.131 +  SDLTest_AssertPass("Call to SDL_ClearError()");
   2.132 +
   2.133 +  /* NULL input for CVT buffer */
   2.134 +  result = SDL_BuildAudioCVT((SDL_AudioCVT *)NULL, spec1.format, spec1.channels, spec1.freq,
   2.135 +                                                   spec2.format, spec2.channels, spec2.freq);
   2.136 +  SDLTest_AssertPass("Call to SDL_BuildAudioCVT(NULL,...)");
   2.137 +  SDLTest_AssertCheck(result == -1, "Verify result value; expected: -1, got: %i", result); 
   2.138 +  error = SDL_GetError();
   2.139 +  SDLTest_AssertPass("Call to SDL_GetError()");
   2.140 +  SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   2.141 +  if (error != NULL) {
   2.142 +      SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
   2.143 +          "Validate error message, expected: '%s', got: '%s'", expectedError, error);
   2.144 +  }
   2.145 +
   2.146 +  /* Invalid conversions */
   2.147 +  for (i = 1; i < 64; i++) {
   2.148 +    /* Valid format to start with */  
   2.149 +    spec1.format = AUDIO_S8;
   2.150 +    spec1.channels = 1;
   2.151 +    spec1.freq = 22050;
   2.152 +    spec2.format = AUDIO_S16LSB;
   2.153 +    spec2.channels = 2;
   2.154 +    spec2.freq = 44100;  
   2.155 +
   2.156 +    SDL_ClearError();
   2.157 +    SDLTest_AssertPass("Call to SDL_ClearError()");
   2.158 +   
   2.159 +    /* Set various invalid format inputs */
   2.160 +    strcpy(message, "Invalid: ");
   2.161 +    if (i & 1) {
   2.162 +        strcat(message, " spec1.format");
   2.163 +        spec1.format = 0;
   2.164 +    }
   2.165 +    if (i & 2) { 
   2.166 +        strcat(message, " spec1.channels");
   2.167 +        spec1.channels = 0;
   2.168 +    }
   2.169 +    if (i & 4) { 
   2.170 +        strcat(message, " spec1.freq");
   2.171 +        spec1.freq = 0;
   2.172 +    }
   2.173 +    if (i & 8) { 
   2.174 +        strcat(message, " spec2.format");
   2.175 +        spec2.format = 0;
   2.176 +    }
   2.177 +    if (i & 16) { 
   2.178 +        strcat(message, " spec2.channels");
   2.179 +        spec2.channels = 0;
   2.180 +    }
   2.181 +    if (i & 32) { 
   2.182 +        strcat(message, " spec2.freq");
   2.183 +        spec2.freq = 0;
   2.184 +    }
   2.185 +    SDLTest_Log(message);
   2.186 +    result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
   2.187 +                                   spec2.format, spec2.channels, spec2.freq);
   2.188 +    SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec2)");
   2.189 +    SDLTest_AssertCheck(result == -1, "Verify result value; expected: -1, got: %i", result);
   2.190 +    error = SDL_GetError();
   2.191 +    SDLTest_AssertPass("Call to SDL_GetError()");
   2.192 +    SDLTest_AssertCheck(error != NULL && SDL_strlen(error)>0, "Validate that error message was not NULL or empty");
   2.193 +  }
   2.194 +
   2.195 +  SDL_ClearError();
   2.196 +  SDLTest_AssertPass("Call to SDL_ClearError()");
   2.197 +                                                
   2.198 +  return TEST_COMPLETED;
   2.199 +}
   2.200 +
   2.201 +/**
   2.202 + * \brief Checks current audio status.
   2.203 + *
   2.204 + * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetAudioStatus
   2.205 + */
   2.206 +int audio_getAudioStatus()
   2.207 +{
   2.208 +   SDL_AudioStatus result;
   2.209 +
   2.210 +   /* Check current audio status */
   2.211 +   result = SDL_GetAudioStatus();
   2.212 +   SDLTest_AssertPass("Call to SDL_GetAudioStatus()");
   2.213 +   SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED,
   2.214 +        "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i", 
   2.215 +        SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result);
   2.216 +
   2.217 +   return TEST_COMPLETED;
   2.218 +}
   2.219 +
   2.220 +
   2.221 +/* Test callback function */
   2.222 +void _audio_testCallback(void *userdata, Uint8 *stream, int len)
   2.223 +{
   2.224 +}
   2.225 +
   2.226 +/**
   2.227 + * \brief Opens, checks current audio status, and closes a device.
   2.228 + *
   2.229 + * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetAudioStatus
   2.230 + */
   2.231 +int audio_openCloseAndGetAudioStatus()
   2.232 +{
   2.233 +   SDL_AudioStatus result;
   2.234 +   int i;
   2.235 +   int count;
   2.236 +   char *device;   
   2.237 +   SDL_AudioDeviceID id;
   2.238 +   SDL_AudioSpec desired, obtained;
   2.239 +   
   2.240 +   /* Get number of devices. */
   2.241 +   count = SDL_GetNumAudioDevices(0);
   2.242 +   SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
   2.243 +   if (count>0) {
   2.244 +     for (i=0; i< count; i++) {
   2.245 +       /* Get device name */
   2.246 +       device = (char *)SDL_GetAudioDeviceName(i, 0);
   2.247 +       SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
   2.248 +       SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
   2.249 +       if (device == NULL) return TEST_ABORTED;
   2.250 +
   2.251 +       /* Set standard desired spec */
   2.252 +       desired.freq=22050;
   2.253 +       desired.format=AUDIO_S16SYS;
   2.254 +       desired.channels=2;
   2.255 +       desired.samples=4096;
   2.256 +       desired.callback=_audio_testCallback;
   2.257 +       desired.userdata=NULL;
   2.258 +       
   2.259 +       /* Open device */
   2.260 +       id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
   2.261 +       SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
   2.262 +       SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %i", id);
   2.263 +       if (id > 1) {
   2.264 +     
   2.265 +         /* Check device audio status */
   2.266 +         result = SDL_GetAudioDeviceStatus(id);
   2.267 +         SDLTest_AssertPass("Call to SDL_GetAudioDeviceStatus()");
   2.268 +         SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED,
   2.269 +            "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i", 
   2.270 +            SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result);         
   2.271 +         
   2.272 +         /* Close device again */
   2.273 +         SDL_CloseAudioDevice(id);
   2.274 +         SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
   2.275 +       }
   2.276 +     }
   2.277 +   } else {
   2.278 +     SDLTest_Log("No devices to test with");
   2.279 +   }
   2.280 +   
   2.281 +   return TEST_COMPLETED;
   2.282 +}
   2.283 +
   2.284 +/**
   2.285 + * \brief Locks and unlocks open audio device.
   2.286 + *
   2.287 + * \sa http://wiki.libsdl.org/moin.cgi/SDL_LockAudioDevice
   2.288 + * \sa http://wiki.libsdl.org/moin.cgi/SDL_UnlockAudioDevice
   2.289 + */
   2.290 +int audio_lockUnlockOpenAudioDevice()
   2.291 +{
   2.292 +   int i;
   2.293 +   int count;
   2.294 +   char *device;   
   2.295 +   SDL_AudioDeviceID id;
   2.296 +   SDL_AudioSpec desired, obtained;
   2.297 +   
   2.298 +   /* Get number of devices. */
   2.299 +   count = SDL_GetNumAudioDevices(0);
   2.300 +   SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
   2.301 +   if (count>0) {
   2.302 +     for (i=0; i< count; i++) {
   2.303 +       /* Get device name */
   2.304 +       device = (char *)SDL_GetAudioDeviceName(i, 0);
   2.305 +       SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
   2.306 +       SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
   2.307 +       if (device == NULL) return TEST_ABORTED;
   2.308 +
   2.309 +       /* Set standard desired spec */
   2.310 +       desired.freq=22050;
   2.311 +       desired.format=AUDIO_S16SYS;
   2.312 +       desired.channels=2;
   2.313 +       desired.samples=4096;
   2.314 +       desired.callback=_audio_testCallback;
   2.315 +       desired.userdata=NULL;
   2.316 +       
   2.317 +       /* Open device */
   2.318 +       id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
   2.319 +       SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
   2.320 +       SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %i", id);
   2.321 +       if (id > 1) {     
   2.322 +         /* Lock to protect callback */
   2.323 +         SDL_LockAudioDevice(id);
   2.324 +         SDLTest_AssertPass("SDL_LockAudioDevice(%i)", id);
   2.325 +         
   2.326 +         /* Simulate callback processing */
   2.327 +         SDL_Delay(10);
   2.328 +         SDLTest_Log("Simulate callback processing - delay");
   2.329 +         
   2.330 +         /* Unlock again*/         
   2.331 +         SDL_UnlockAudioDevice(id);
   2.332 +         SDLTest_AssertPass("SDL_UnlockAudioDevice(%i)", id);         
   2.333 +         
   2.334 +         /* Close device again */
   2.335 +         SDL_CloseAudioDevice(id);
   2.336 +         SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
   2.337 +       }
   2.338 +     }
   2.339 +   } else {
   2.340 +     SDLTest_Log("No devices to test with");
   2.341 +   }
   2.342 +   
   2.343 +   return TEST_COMPLETED;
   2.344 +}
   2.345 +
   2.346 +
   2.347 +
   2.348  /* ================= Test Case References ================== */
   2.349  
   2.350  /* Audio test cases */
   2.351 @@ -195,9 +511,24 @@
   2.352  static const SDLTest_TestCaseReference audioTest4 =
   2.353  		{ (SDLTest_TestCaseFp)audio_printCurrentAudioDriver, "audio_printCurrentAudioDriver", "Checks current audio driver name with initialized audio.", TEST_ENABLED };
   2.354  
   2.355 +static const SDLTest_TestCaseReference audioTest5 =
   2.356 +		{ (SDLTest_TestCaseFp)audio_buildAudioCVT, "audio_buildAudioCVT", "Builds various audio conversion structures.", TEST_ENABLED };
   2.357 +
   2.358 +static const SDLTest_TestCaseReference audioTest6 =
   2.359 +		{ (SDLTest_TestCaseFp)audio_buildAudioCVTNegative, "audio_buildAudioCVTNegative", "Checks calls with invalid input to SDL_BuildAudioCVT", TEST_ENABLED };
   2.360 +
   2.361 +static const SDLTest_TestCaseReference audioTest7 =
   2.362 +		{ (SDLTest_TestCaseFp)audio_getAudioStatus, "audio_getAudioStatus", "Checks current audio status.", TEST_ENABLED };
   2.363 +
   2.364 +static const SDLTest_TestCaseReference audioTest8 =
   2.365 +		{ (SDLTest_TestCaseFp)audio_openCloseAndGetAudioStatus, "audio_openCloseAndGetAudioStatus", "Opens and closes audio device and get audio status.", TEST_ENABLED };
   2.366 +
   2.367 +static const SDLTest_TestCaseReference audioTest9 =
   2.368 +		{ (SDLTest_TestCaseFp)audio_lockUnlockOpenAudioDevice, "audio_lockUnlockOpenAudioDevice", "Locks and unlocks an open audio device.", TEST_ENABLED };
   2.369 +
   2.370  /* Sequence of Audio test cases */
   2.371  static const SDLTest_TestCaseReference *audioTests[] =  {
   2.372 -	&audioTest1, &audioTest2, &audioTest3, &audioTest4, NULL
   2.373 +	&audioTest1, &audioTest2, &audioTest3, &audioTest4, &audioTest5, &audioTest6, &audioTest7, &audioTest8, &audioTest9, NULL
   2.374  };
   2.375  
   2.376  /* Audio test suite (global) */
     3.1 --- a/test/testautomation_rwops.c	Mon Jan 21 03:33:42 2013 -0500
     3.2 +++ b/test/testautomation_rwops.c	Mon Jan 21 09:16:27 2013 -0800
     3.3 @@ -4,7 +4,7 @@
     3.4   *
     3.5   * Original code written by Edgar Simo "bobbens"
     3.6   * Ported by Markus Kauppila (markus.kauppila@gmail.com)
     3.7 - * Updated for SDL_test by aschiffler at ferzkopp dot net
     3.8 + * Updated and extended for SDL_test by aschiffler at ferzkopp dot net
     3.9   *
    3.10   * Released under Public Domain.
    3.11   */
    3.12 @@ -42,7 +42,7 @@
    3.13  	/* Create a test file */
    3.14  	handle = fopen(RWopsReadTestFilename, "w");
    3.15  	SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", RWopsReadTestFilename);
    3.16 -    if (handle == NULL) return;
    3.17 +        if (handle == NULL) return;
    3.18  
    3.19  	/* Write some known test into it */
    3.20  	writtenLen = (int)fwrite(RWopsHelloWorldTestString, 1, fileLen, handle);
    3.21 @@ -177,6 +177,7 @@
    3.22   * @brief Tests opening from memory.
    3.23   *
    3.24   * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromMem
    3.25 + * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
    3.26   */
    3.27  int
    3.28  rwops_testMem (void)
    3.29 @@ -211,6 +212,7 @@
    3.30   *
    3.31   * \sa
    3.32   * http://wiki.libsdl.org/moin.cgi/SDL_RWFromConstMem
    3.33 + * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
    3.34   */
    3.35  int 
    3.36  rwops_testConstMem (void)
    3.37 @@ -241,7 +243,7 @@
    3.38   *
    3.39   * \sa
    3.40   * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
    3.41 - * http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
    3.42 + * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
    3.43   */
    3.44  int
    3.45  rwops_testFileRead(void)
    3.46 @@ -267,11 +269,11 @@
    3.47  }
    3.48  
    3.49  /**
    3.50 - * @brief Tests writing from memory.
    3.51 + * @brief Tests writing from file.
    3.52   *
    3.53   * \sa
    3.54   * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
    3.55 - * http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
    3.56 + * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
    3.57   */
    3.58  int
    3.59  rwops_testFileWrite(void)
    3.60 @@ -302,7 +304,7 @@
    3.61   *
    3.62   * \sa
    3.63   * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFP
    3.64 - * http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
    3.65 + * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
    3.66   *
    3.67   */
    3.68  int
    3.69 @@ -345,7 +347,7 @@
    3.70   *
    3.71   * \sa
    3.72   * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFP
    3.73 - * http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
    3.74 + * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
    3.75   *
    3.76   */
    3.77  int
    3.78 @@ -385,8 +387,8 @@
    3.79  /**
    3.80   * @brief Tests alloc and free RW context.
    3.81   *
    3.82 + * \sa http://wiki.libsdl.org/moin.cgi/SDL_AllocRW
    3.83   * \sa http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
    3.84 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_AllocRW
    3.85   */
    3.86  int
    3.87  rwops_testAllocFree (void)
    3.88 @@ -404,6 +406,130 @@
    3.89     return TEST_COMPLETED;
    3.90  }
    3.91  
    3.92 +/**
    3.93 + * @brief Tests writing and reading from file using endian aware functions.
    3.94 + *
    3.95 + * \sa
    3.96 + * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
    3.97 + * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
    3.98 + * http://wiki.libsdl.org/moin.cgi/SDL_ReadBE16
    3.99 + * http://wiki.libsdl.org/moin.cgi/SDL_WriteBE16
   3.100 + */
   3.101 +int
   3.102 +rwops_testFileWriteReadEndian(void)
   3.103 +{
   3.104 +   SDL_RWops *rw;
   3.105 +   int result;
   3.106 +   int mode;
   3.107 +   size_t objectsWritten;
   3.108 +   Uint16 BE16value;
   3.109 +   Uint32 BE32value;
   3.110 +   Uint64 BE64value;
   3.111 +   Uint16 LE16value;
   3.112 +   Uint32 LE32value;
   3.113 +   Uint64 LE64value;
   3.114 +   Uint16 BE16test;
   3.115 +   Uint32 BE32test;
   3.116 +   Uint64 BE64test;
   3.117 +   Uint16 LE16test;
   3.118 +   Uint32 LE32test;
   3.119 +   Uint64 LE64test;
   3.120 +
   3.121 +   for (mode = 0; mode < 3; mode++) {
   3.122 +   
   3.123 +     /* Create test data */
   3.124 +     switch (mode) {
   3.125 +       case 0:
   3.126 +        SDLTest_Log("All 0 values");
   3.127 +        BE16value = 0;
   3.128 +        BE32value = 0;
   3.129 +        BE64value = 0;
   3.130 +        LE16value = 0;
   3.131 +        LE32value = 0;
   3.132 +        LE64value = 0;
   3.133 +        break;
   3.134 +       case 1:
   3.135 +        SDLTest_Log("All 1 values");
   3.136 +        BE16value = 1;
   3.137 +        BE32value = 1;
   3.138 +        BE64value = 1;
   3.139 +        LE16value = 1;
   3.140 +        LE32value = 1;
   3.141 +        LE64value = 1;
   3.142 +        break;
   3.143 +       case 2:
   3.144 +        SDLTest_Log("Random values");
   3.145 +        BE16value = SDLTest_RandomUint16();
   3.146 +        BE32value = SDLTest_RandomUint32();
   3.147 +        BE64value = SDLTest_RandomUint64();
   3.148 +        LE16value = SDLTest_RandomUint16();
   3.149 +        LE32value = SDLTest_RandomUint32();
   3.150 +        LE64value = SDLTest_RandomUint64();
   3.151 +        break;
   3.152 +     }
   3.153 +   
   3.154 +     /* Write test. */
   3.155 +     rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
   3.156 +     SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\")");
   3.157 +     SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
   3.158 +
   3.159 +     // Bail out if NULL
   3.160 +     if (rw == NULL) return TEST_ABORTED;
   3.161 +
   3.162 +     /* Write test data */
   3.163 +     objectsWritten = SDL_WriteBE16(rw, BE16value);
   3.164 +     SDLTest_AssertPass("Call to SDL_WriteBE16");
   3.165 +     SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);   
   3.166 +     objectsWritten = SDL_WriteBE32(rw, BE32value);
   3.167 +     SDLTest_AssertPass("Call to SDL_WriteBE32");
   3.168 +     SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);   
   3.169 +     objectsWritten = SDL_WriteBE64(rw, BE64value);
   3.170 +     SDLTest_AssertPass("Call to SDL_WriteBE64");
   3.171 +     SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);   
   3.172 +     objectsWritten = SDL_WriteLE16(rw, LE16value);
   3.173 +     SDLTest_AssertPass("Call to SDL_WriteLE16");
   3.174 +     SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);   
   3.175 +     objectsWritten = SDL_WriteLE32(rw, LE32value);
   3.176 +     SDLTest_AssertPass("Call to SDL_WriteLE32");
   3.177 +     SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);   
   3.178 +     objectsWritten = SDL_WriteLE64(rw, LE64value);
   3.179 +     SDLTest_AssertPass("Call to SDL_WriteLE64");
   3.180 +     SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);   
   3.181 +
   3.182 +     /* Test seek to start */
   3.183 +     result = SDL_RWseek( rw, 0, RW_SEEK_SET );
   3.184 +     SDLTest_AssertPass("Call to SDL_RWseek succeeded");
   3.185 +     SDLTest_AssertCheck(result == 0, "Verify result from position 0 with SDL_RWseek, expected 0, got %i", result);
   3.186 +
   3.187 +     /* Read test data */
   3.188 +     BE16test = SDL_ReadBE16(rw);
   3.189 +     SDLTest_AssertPass("Call to SDL_ReadBE16");
   3.190 +     SDLTest_AssertCheck(BE16test == BE16value, "Validate return value from SDL_ReadBE16, expected: %hu, got: %hu", BE16value, BE16test);
   3.191 +     BE32test = SDL_ReadBE32(rw);
   3.192 +     SDLTest_AssertPass("Call to SDL_ReadBE32");
   3.193 +     SDLTest_AssertCheck(BE32test == BE32value, "Validate return value from SDL_ReadBE32, expected: %u, got: %u", BE32value, BE32test);   
   3.194 +     BE64test = SDL_ReadBE64(rw);
   3.195 +     SDLTest_AssertPass("Call to SDL_ReadBE64");
   3.196 +     SDLTest_AssertCheck(BE64test == BE64value, "Validate return value from SDL_ReadBE64, expected: %llu, got: %llu", BE64value, BE64test);   
   3.197 +     LE16test = SDL_ReadLE16(rw);
   3.198 +     SDLTest_AssertPass("Call to SDL_ReadLE16");
   3.199 +     SDLTest_AssertCheck(LE16test == LE16value, "Validate return value from SDL_ReadLE16, expected: %hu, got: %hu", LE16value, LE16test);
   3.200 +     LE32test = SDL_ReadLE32(rw);
   3.201 +     SDLTest_AssertPass("Call to SDL_ReadLE32");
   3.202 +     SDLTest_AssertCheck(LE32test == LE32value, "Validate return value from SDL_ReadLE32, expected: %u, got: %u", LE32value, LE32test);   
   3.203 +     LE64test = SDL_ReadLE64(rw);
   3.204 +     SDLTest_AssertPass("Call to SDL_ReadLE64");
   3.205 +     SDLTest_AssertCheck(LE64test == LE64value, "Validate return value from SDL_ReadLE64, expected: %llu, got: %llu", LE64value, LE64test);
   3.206 +
   3.207 +     /* Close handle */
   3.208 +     SDL_RWclose(rw);
   3.209 +     SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   3.210 +   
   3.211 +   }
   3.212 +
   3.213 +   return TEST_COMPLETED;
   3.214 +}
   3.215 +
   3.216  
   3.217  /* ================= Test References ================== */
   3.218  
   3.219 @@ -432,9 +558,13 @@
   3.220  static const SDLTest_TestCaseReference rwopsTest8 =
   3.221  		{ (SDLTest_TestCaseFp)rwops_testAllocFree, "rwops_testAllocFree", "Test alloc and free of RW context", TEST_ENABLED };
   3.222  
   3.223 +static const SDLTest_TestCaseReference rwopsTest9 =
   3.224 +		{ (SDLTest_TestCaseFp)rwops_testFileWriteReadEndian, "rwops_testFileWriteReadEndian", "Test writing and reading via the Endian aware functions", TEST_ENABLED };
   3.225 +
   3.226  /* Sequence of RWops test cases */
   3.227  static const SDLTest_TestCaseReference *rwopsTests[] =  {
   3.228 -	&rwopsTest1, &rwopsTest2, &rwopsTest3, &rwopsTest4, &rwopsTest5, &rwopsTest6, &rwopsTest7, &rwopsTest8, NULL
   3.229 +	&rwopsTest1, &rwopsTest2, &rwopsTest3, &rwopsTest4, &rwopsTest5, &rwopsTest6, 
   3.230 +	&rwopsTest7, &rwopsTest8, &rwopsTest9, NULL
   3.231  };
   3.232  
   3.233  /* RWops test suite (global) */