diff --git a/test/test-automation/fuzzer/fuzzer.c b/test/test-automation/fuzzer/fuzzer.c index baa3396a4..bd2e53e07 100644 --- a/test/test-automation/fuzzer/fuzzer.c +++ b/test/test-automation/fuzzer/fuzzer.c @@ -35,6 +35,7 @@ GenerateExecKey(char *runSeed, char *suiteName, } + // Change to itoa char iterationString[16]; memset(iterationString, 0, sizeof(iterationString)); SDL_snprintf(iterationString, sizeof(iterationString) - 1, "%d", iterationNumber); @@ -69,14 +70,15 @@ GenerateExecKey(char *runSeed, char *suiteName, //printf("Debug: digest = %s\n", execKey); - Uint64 key = execKey[8] << 56 | - execKey[9] << 48 | - execKey[10] << 40 | - execKey[11] << 32 | - execKey[12] << 24 | - execKey[13] << 16 | - execKey[14] << 8 | - execKey[15] << 0; + // Casting fixes compiler warnings + Uint64 key = ((Uint64) execKey[8]) << 56 | + ((Uint64) execKey[9]) << 48 | + ((Uint64) execKey[10]) << 40 | + ((Uint64) execKey[11]) << 32 | + ((Uint64) execKey[12]) << 24 | + ((Uint64) execKey[13]) << 16 | + ((Uint64) execKey[14]) << 8 | + ((Uint64) execKey[15]) << 0; return key; } @@ -99,54 +101,150 @@ DeinitFuzzer() } -int +Sint32 RandomInteger() { return utl_randomInt(&rndContext); } -int +Uint32 RandomPositiveInteger() { - return abs(utl_randomInt(&rndContext)); + return utl_randomInt(&rndContext); } -int -RandomIntegerInRange(int min, int max) +Sint32 +RandomIntegerInRange(Sint32 pMin, Sint32 pMax) { - if(min > max || (min - max) == 0) { - return -1; // Doesn't really make sense to return -1 on error? + Sint64 min = (Sint64) pMin, max = (Sint64) pMax; + + if(min > max) { + Sint64 temp = min; + min = max; + max = temp; + } else if(min == max) { + return min; } - int number = utl_randomInt(&rndContext); - number = abs(number); + Sint32 number = abs(utl_randomInt(&rndContext)); return (number % ((max + 1) - min)) + min; } -int -GenerateBoundaryValueForSize(const int size) +/*! + * Generates boundary values between the given boundaries. + * Boundary values are inclusive. See the examples below. + * If boundary2 < boundary1, the values are swapped. + * If boundary1 == boundary2, value of boundary1 will be returned + * + * Generating boundary values for Uint8 + * BoundaryValues(sizeof(Uint8), 10, 20, True) -> [10,11,19,20] + * BoundaryValues(sizeof(Uint8), 10, 20, False) -> [9,21] + * BoundaryValues(sizeof(Uint8), 0, 15, True) -> [0, 1, 14, 15] + * BoundaryValues(sizeof(Uint8), 0, 15, False) -> [16] + * BoundaryValues(sizeof(Uint8), 0, 255, False) -> [] + * + * \param maxValue The biggest value that is acceptable for this data type. + * For instance, for Uint8 -> 255, Uint16 -> 65536 etc. + * \param pBoundary1 defines lower boundary + * \param pBoundary2 defines upper boundary + * \param validDomain Generate only for valid domain (for the data type) + * + * \param outBuffer The generated boundary values are put here + * \param outBufferSize Size of outBuffer + * + * \returns NULL on error, outBuffer on success + * + */ +Uint64 * +GenerateUnsignedBoundaryValues(const Uint64 maxValue, + Uint64 pBoundary1, Uint64 pBoundary2, SDL_bool validDomain, + Uint64 *outBuffer, Uint32 *outBufferSize) { - if(size < 0) { - return -1; + Uint64 boundary1 = pBoundary1, boundary2 = pBoundary2; + + if(outBuffer != NULL) { + SDL_free(outBuffer); } - const int adjustment = RandomIntegerInRange(-1, 1); - int retValue = (1 << (RandomPositiveInteger() % size)) + adjustment; + if(boundary1 > boundary2) { + Uint64 temp = boundary1; + boundary1 = boundary2; + boundary2 = temp; + } - return retValue; + Uint64 tempBuf[8]; + memset(tempBuf, 0, 8 * sizeof(Uint64)); + + Uint64 index = 0; + + if(boundary1 == boundary2) { + tempBuf[index++] = boundary1; + } + else if(validDomain) { + tempBuf[index++] = boundary1; + tempBuf[index++] = boundary1 + 1; + + tempBuf[index++] = boundary2 - 1; + tempBuf[index++] = boundary2; + } + else { + if(boundary1 != 0) { + tempBuf[index++] = boundary1 - 1; + } + + if(boundary2 != maxValue) { + tempBuf[index++] = boundary2 + 1; + } + } + + if(index == 0) { + // There are no valid boundaries + return NULL; + } + + // Create the return buffer + outBuffer = SDL_malloc(index * sizeof(Uint64)); + if(outBuffer == NULL) { + return NULL; + } + + SDL_memcpy(outBuffer, tempBuf, index * sizeof(Uint64)); + + *outBufferSize = index; + + return outBuffer; } -int -RandomUint8BoundaryValue() +Uint8 +RandomUint8BoundaryValue(Uint8 boundary1, Uint8 boundary2, SDL_bool validDomain) { - return GenerateBoundaryValueForSize(8); + Uint64 *buffer = NULL; + Uint32 size; + + // max value for Uint8 + const Uint64 maxValue = 255; + + buffer = GenerateUnsignedBoundaryValues(maxValue, + (Uint64) boundary1, (Uint64) boundary2, + validDomain, buffer, &size); + if(buffer == NULL) { + return 0; // Change to some better error value? What would be better? + } + + Uint32 index = RandomInteger() % size; + Uint8 retVal = (Uint8) buffer[index]; + + SDL_free(buffer); + + return retVal; } -int -RandomInt8BoundaryValue() + +Sint8 +RandomSint8BoundaryValue() { - int value = GenerateBoundaryValueForSize(8); + int value = 0; //GenerateBoundaryValueForSize(8); return (RandomPositiveInteger() % 2 == 0 ? value : -value); } @@ -164,7 +262,7 @@ RandomAsciiStringWithMaximumLength(int maxSize) return NULL; } - int size = abs(RandomInteger) % maxSize; + int size = abs(RandomInteger()) % maxSize; char *string = SDL_malloc(size * sizeof(size)); int counter = 0; diff --git a/test/test-automation/fuzzer/fuzzer.h b/test/test-automation/fuzzer/fuzzer.h index 32b82c05b..f5023ca92 100644 --- a/test/test-automation/fuzzer/fuzzer.h +++ b/test/test-automation/fuzzer/fuzzer.h @@ -45,7 +45,7 @@ void DeinitFuzzer(); * * \returns Generated integer */ -int RandomInteger(); +Sint32 RandomInteger(); /*! @@ -53,29 +53,30 @@ int RandomInteger(); * * \returns Generated integer */ -int RandomPositiveInteger(); +Uint32 RandomPositiveInteger(); /*! * todo add markup */ -int RandomUint8BoundaryValue(); +Uint8 RandomUint8BoundaryValue(Uint8 boundary1, Uint8 boundary2, SDL_bool validDomain); /*! * todo add markup */ -int RandomInt8BoundaryValue(); +Sint8 RandomSint8BoundaryValue(); /*! - * 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 integer in range [min, max] (inclusive). + * Min and max values can be negative values. + * If Max in smaller tham min, then the values are swapped. + * Min and max are the same value, that value will be returned. * - * \returns Generated integer or ? in error + * \returns Generated integer */ -int RandomIntegerInRange(int min, int max); +Sint32 RandomIntegerInRange(Sint32 min, Sint32 max); /*! @@ -117,4 +118,5 @@ char *RandomAsciiStringWithMaximumLength(int maxLength); */ Uint64 GenerateExecKey(char *runSeed, char *suiteName, char *testName, int interationNumber); + #endif diff --git a/test/test-automation/testdummy/testdummy.c b/test/test-automation/testdummy/testdummy.c index c1a8c1b12..d32f7ce60 100644 --- a/test/test-automation/testdummy/testdummy.c +++ b/test/test-automation/testdummy/testdummy.c @@ -91,10 +91,18 @@ dummycase1(void *arg) { AssertEquals(5, 5, "Assert message"); - for(; 0 ;) { - Log(0, "uint8: %d", RandomUint8BoundaryValue()); - Log(0, "int8: %d", RandomInt8BoundaryValue()); + /* + for( ; 1 ; ) + Log(0, "uint8 (same value): %u", RandomPositiveInteger()); + // */ + + //Log(0, "uint8 (same value): %d", RandomUint8BoundaryValue(200, 200, SDL_TRUE)); + + + for(; 1 ;) { + Log(0, "uint8: %u", RandomUint8BoundaryValue(10, 20, SDL_FALSE)); + //Log(0, "int8: %d", RandomInt8BoundaryValue()); } for(; 0 ;) {