diff --git a/test/test-automation/fuzzer/fuzzer.c b/test/test-automation/fuzzer/fuzzer.c index b2e72d80e..72aa272d4 100644 --- a/test/test-automation/fuzzer/fuzzer.c +++ b/test/test-automation/fuzzer/fuzzer.c @@ -1,8 +1,3 @@ -#include -#include - -#include - #include "../SDL_test.h" @@ -77,43 +72,74 @@ RandomInteger() } int -RandomPositiveIntegerInRange(int min, int max) +RandomPositiveInteger() { - int number = utl_randomInt(&rndContext3); - number = abs(number); - - return (number % (max - min)) + min; + return abs(utl_randomInt(&rndContext3)); } int -RandomBoundaryValue(const int max) +RandomIntegerInRange(int min, int max) { - // Note: somehow integrate with RandomInteger? - // try to make more sensible & add new values - int boundaryValues[] = {0, 1, 15, 16, 17, 31, 32, 33, 63, 64, 65}; - int retValue = -1; + if(min > max || (min - max) == 0) { + return -1; // Doesn't really make sense to return -1 on error? + } - do { - int index = RandomPositiveIntegerInRange(0, 10); - retValue = boundaryValues[index]; + int number = utl_randomInt(&rndContext3); + number = abs(number); - } while( !(retValue <= max) ); + return (number % ((max + 1) - min)) + min; +} + +int +GenerateBoundaryValueForSize(const int size) +{ + if(size < 0) { + return -1; + } + + const int adjustment = RandomIntegerInRange(-1, 1); + int retValue = (1 << (RandomPositiveInteger() % size)) + adjustment; return retValue; } +int +RandomUint8BoundaryValue() +{ + return GenerateBoundaryValueForSize(8); +} + +int +RandomInt8BoundaryValue() +{ + int value = GenerateBoundaryValueForSize(8); + + return (RandomPositiveInteger() % 2 == 0 ? value : -value); +} char * RandomAsciiString() { - const int size = abs(RandomInteger); + return RandomAsciiStringWithMaximumLength(255); +} + +char * +RandomAsciiStringWithMaximumLength(int maxSize) +{ + if(maxSize < 0) { + return NULL; + } + + const int size = abs(RandomInteger) % maxSize; char *string = SDL_malloc(size * sizeof(size)); int counter = 0; for( ; counter < size; ++counter) { - char character = (char) RandomPositiveIntegerInRange(0, 127); + char character = (char) RandomPositiveIntegerInRange(1, 127); string[counter] = character; } + string[counter] = '\0'; + return string; } diff --git a/test/test-automation/fuzzer/fuzzer.h b/test/test-automation/fuzzer/fuzzer.h index 8403af0dd..892165200 100644 --- a/test/test-automation/fuzzer/fuzzer.h +++ b/test/test-automation/fuzzer/fuzzer.h @@ -25,11 +25,13 @@ #include "utl_md5.h" #include "utl_random.h" + /*! * Inits the fuzzer for a test */ void InitFuzzer(const int execKey); + /*! * Deinits the fuzzer (for a test) */ @@ -37,33 +39,68 @@ void DeinitFuzzer(); /*! - * Returns random integer + * Returns a random integer * * \returns Generated integer */ int RandomInteger(); + /*! - * Returns positive integer in range [min, max] + * Returns a random positive integer * * \returns Generated integer */ -int RandomPositiveIntegerInRange(int min, int max); +int RandomPositiveInteger(); + /*! - * Generates random ASCII string + * Returns integer in range [min, max]. Min and max + * value can be negative values as long as min is smaller than max. + * Min and max also can't be the same value. + * + * \returns Generated integer or ? in error + */ +int RandomIntegerInRange(int min, int max); + + +/*! + * Generates random null-terminated string. The maximum length for + * the string is 255 characters and it can contain ASCII characters + * from 1 to 127. + * + * Note: Returned string needs to be deallocated. * * \returns newly allocated random string */ char *RandomAsciiString(); + /*! - * Generates a random boundary value. Max is the biggest - * value the function can return. + * Generates random null-terminated string. The maximum length for + * the string is defined by maxLenght parameter. + * String can contain ASCII characters from 1 to 127. * - * \returns a boundary value + * Note: Returned string needs to be deallocated. + * + * \param maxLength Maximum length of the generated string + * + * \returns newly allocated random string */ -int RandomBoundaryValue(const int max); +char *RandomAsciiStringWithMaximumLength(int maxLength); + + +/*! + * todo add markup + */ +int RandomUint8BoundaryValue(); + + +/*! + * todo add markup + */ +int RandomInt8BoundaryValue(); + /*! * Generates execution key (used for random seed) for a test diff --git a/test/test-automation/testdummy/testdummy.c b/test/test-automation/testdummy/testdummy.c index 2bc63b2f3..108e791a0 100644 --- a/test/test-automation/testdummy/testdummy.c +++ b/test/test-automation/testdummy/testdummy.c @@ -91,9 +91,15 @@ dummycase1(void *arg) { AssertEquals(5, 5, "Assert message"); + for(; 1 ;) { + Log(0, "uint8: %d", RandomUint8BoundaryValue()); + Log(0, "int8: %d", RandomInt8BoundaryValue()); + + } + for(; 0 ;) { - int min = 50; - int max = 69; + int min = -5; + int max = 5; int random = RandomPositiveIntegerInRange(min, max); if(random < min || random > max ) { AssertFail("Generated incorrect integer"); @@ -101,7 +107,7 @@ dummycase1(void *arg) Log(0, "%d", random); } - //Log(0, "Random: %s", RandomAsciiString()); + //Log(0, "Random: %s", RandomAsciiStringWithMaximumLength(2)); } void