Refining the fuzzer. Adding new functions and fixing the old ones.
authorMarkus Kauppila
Mon, 25 Jul 2011 18:51:57 +0300
changeset 57823e6c6c39f908
parent 5781 7ff8df8e36f2
child 5783 236ed35e4b34
Refining the fuzzer. Adding new functions and fixing the old ones.
test/test-automation/fuzzer/fuzzer.c
test/test-automation/fuzzer/fuzzer.h
test/test-automation/testdummy/testdummy.c
     1.1 --- a/test/test-automation/fuzzer/fuzzer.c	Sun Jul 24 18:58:36 2011 +0300
     1.2 +++ b/test/test-automation/fuzzer/fuzzer.c	Mon Jul 25 18:51:57 2011 +0300
     1.3 @@ -1,8 +1,3 @@
     1.4 -#include <stdio.h>
     1.5 -#include <stdlib.h>
     1.6 -
     1.7 -#include <string.h>
     1.8 -
     1.9  
    1.10  #include "../SDL_test.h"
    1.11  
    1.12 @@ -77,43 +72,74 @@
    1.13  }
    1.14  
    1.15  int
    1.16 -RandomPositiveIntegerInRange(int min, int max)
    1.17 +RandomPositiveInteger()
    1.18  {
    1.19 -	int number = utl_randomInt(&rndContext3);
    1.20 -	number = abs(number);
    1.21 -
    1.22 -	return (number % (max - min)) + min;
    1.23 +	return abs(utl_randomInt(&rndContext3));
    1.24  }
    1.25  
    1.26  int
    1.27 -RandomBoundaryValue(const int max)
    1.28 +RandomIntegerInRange(int min, int max)
    1.29  {
    1.30 -	// Note: somehow integrate with RandomInteger?
    1.31 -	// try to make more sensible & add new values
    1.32 -	int boundaryValues[] = {0, 1, 15, 16, 17, 31, 32, 33, 63, 64, 65};
    1.33 -	int retValue = -1;
    1.34 +	if(min > max || (min - max) == 0) {
    1.35 +		return -1; // Doesn't really make sense to return -1 on error?
    1.36 +	}
    1.37 +
    1.38 +	int number = utl_randomInt(&rndContext3);
    1.39 +	number = abs(number);
    1.40 +
    1.41 +	return (number % ((max + 1) - min)) + min;
    1.42 +}
    1.43  
    1.44 -	do {
    1.45 -		int index = RandomPositiveIntegerInRange(0, 10);
    1.46 -		retValue = boundaryValues[index];
    1.47 +int
    1.48 +GenerateBoundaryValueForSize(const int size)
    1.49 +{
    1.50 +	if(size < 0) {
    1.51 +		return -1;
    1.52 +	}
    1.53  
    1.54 -	} while( !(retValue <= max) );
    1.55 +	const int adjustment = RandomIntegerInRange(-1, 1);
    1.56 +	int retValue = (1 << (RandomPositiveInteger() % size)) + adjustment;
    1.57  
    1.58  	return retValue;
    1.59  }
    1.60  
    1.61 +int
    1.62 +RandomUint8BoundaryValue()
    1.63 +{
    1.64 +	return GenerateBoundaryValueForSize(8);
    1.65 +}
    1.66 +
    1.67 +int
    1.68 +RandomInt8BoundaryValue()
    1.69 +{
    1.70 +	int value = GenerateBoundaryValueForSize(8);
    1.71 +
    1.72 +	return (RandomPositiveInteger() % 2 == 0 ? value : -value);
    1.73 +}
    1.74  
    1.75  char *
    1.76  RandomAsciiString()
    1.77  {
    1.78 -	const int size = abs(RandomInteger);
    1.79 +	return RandomAsciiStringWithMaximumLength(255);
    1.80 +}
    1.81 +
    1.82 +char *
    1.83 +RandomAsciiStringWithMaximumLength(int maxSize)
    1.84 +{
    1.85 +	if(maxSize < 0) {
    1.86 +		return NULL;
    1.87 +	}
    1.88 +
    1.89 +	const int size = abs(RandomInteger) % maxSize;
    1.90  	char *string = SDL_malloc(size * sizeof(size));
    1.91  
    1.92  	int counter = 0;
    1.93  	for( ; counter < size; ++counter) {
    1.94 -		char character = (char) RandomPositiveIntegerInRange(0, 127);
    1.95 +		char character = (char) RandomPositiveIntegerInRange(1, 127);
    1.96  		string[counter] = character;
    1.97  	}
    1.98  
    1.99 +	string[counter] = '\0';
   1.100 +
   1.101  	return string;
   1.102  }
     2.1 --- a/test/test-automation/fuzzer/fuzzer.h	Sun Jul 24 18:58:36 2011 +0300
     2.2 +++ b/test/test-automation/fuzzer/fuzzer.h	Mon Jul 25 18:51:57 2011 +0300
     2.3 @@ -25,11 +25,13 @@
     2.4  #include "utl_md5.h"
     2.5  #include "utl_random.h"
     2.6  
     2.7 +
     2.8  /*!
     2.9   * Inits the fuzzer for a test
    2.10   */
    2.11  void InitFuzzer(const int execKey);
    2.12  
    2.13 +
    2.14  /*!
    2.15   * Deinits the fuzzer (for a test)
    2.16   */
    2.17 @@ -37,33 +39,68 @@
    2.18  
    2.19  
    2.20  /*!
    2.21 - * Returns random integer
    2.22 + * Returns a random integer
    2.23   *
    2.24   * \returns Generated integer
    2.25   */
    2.26  int RandomInteger();
    2.27  
    2.28 +
    2.29  /*!
    2.30 - * Returns positive integer in range [min, max]
    2.31 + * Returns a random positive integer
    2.32   *
    2.33   * \returns Generated integer
    2.34   */
    2.35 -int RandomPositiveIntegerInRange(int min, int max);
    2.36 +int RandomPositiveInteger();
    2.37 +
    2.38  
    2.39  /*!
    2.40 - * Generates random ASCII string
    2.41 + * Returns integer in range [min, max]. Min and max
    2.42 + * value can be negative values as long as min is smaller than max.
    2.43 + * Min and max also can't be the same value.
    2.44 + *
    2.45 + * \returns Generated integer or ? in error
    2.46 + */
    2.47 +int RandomIntegerInRange(int min, int max);
    2.48 +
    2.49 +
    2.50 +/*!
    2.51 + * Generates random null-terminated string. The maximum length for
    2.52 + * the string is 255 characters and it can contain ASCII characters
    2.53 + * from 1 to 127.
    2.54 + *
    2.55 + * Note: Returned string needs to be deallocated.
    2.56   *
    2.57   * \returns newly allocated random string
    2.58   */
    2.59  char *RandomAsciiString();
    2.60  
    2.61 +
    2.62  /*!
    2.63 - * Generates a random boundary value. Max is the biggest
    2.64 - * value the function can return.
    2.65 + * Generates random null-terminated string. The maximum length for
    2.66 + * the string is defined by maxLenght parameter.
    2.67 + * String can contain ASCII characters from 1 to 127.
    2.68 + *
    2.69 + * Note: Returned string needs to be deallocated.
    2.70 + *
    2.71 + * \param maxLength Maximum length of the generated string
    2.72   *
    2.73 - * \returns a boundary value
    2.74 + * \returns newly allocated random string
    2.75 + */
    2.76 +char *RandomAsciiStringWithMaximumLength(int maxLength);
    2.77 +
    2.78 +
    2.79 +/*!
    2.80 + * todo add markup
    2.81   */
    2.82 -int RandomBoundaryValue(const int max);
    2.83 +int RandomUint8BoundaryValue();
    2.84 +
    2.85 +
    2.86 +/*!
    2.87 + * todo add markup
    2.88 + */
    2.89 +int RandomInt8BoundaryValue();
    2.90 +
    2.91  
    2.92  /*!
    2.93   * Generates execution key (used for random seed) for a test
     3.1 --- a/test/test-automation/testdummy/testdummy.c	Sun Jul 24 18:58:36 2011 +0300
     3.2 +++ b/test/test-automation/testdummy/testdummy.c	Mon Jul 25 18:51:57 2011 +0300
     3.3 @@ -91,9 +91,15 @@
     3.4  {
     3.5  	AssertEquals(5, 5, "Assert message");
     3.6  
     3.7 +	for(; 1 ;) {
     3.8 +		Log(0, "uint8: %d", RandomUint8BoundaryValue());
     3.9 +		Log(0, "int8: %d", RandomInt8BoundaryValue());
    3.10 +
    3.11 +	}
    3.12 +
    3.13  	for(; 0 ;) {
    3.14 -		int min = 50;
    3.15 -		int max = 69;
    3.16 +		int min = -5;
    3.17 +		int max = 5;
    3.18  		int random =  RandomPositiveIntegerInRange(min, max);
    3.19  		if(random < min || random > max ) {
    3.20  			AssertFail("Generated incorrect integer");
    3.21 @@ -101,7 +107,7 @@
    3.22  		Log(0, "%d", random);
    3.23  	}
    3.24  
    3.25 -	//Log(0, "Random: %s", RandomAsciiString());
    3.26 +	//Log(0, "Random: %s", RandomAsciiStringWithMaximumLength(2));
    3.27  }
    3.28  
    3.29  void