2011-07-24 18:58:36 +03:00
|
|
|
|
|
|
|
#include "../SDL_test.h"
|
|
|
|
|
|
|
|
#include "fuzzer.h"
|
|
|
|
|
|
|
|
|
|
|
|
//! context for test-specific random number generator
|
2011-07-25 20:32:31 +03:00
|
|
|
static RND_CTX rndContext;
|
2011-07-24 18:58:36 +03:00
|
|
|
|
2011-07-28 22:11:30 +03:00
|
|
|
int
|
2011-07-25 20:32:31 +03:00
|
|
|
GenerateExecKey(char *runSeed, char *suiteName,
|
2011-07-24 18:58:36 +03:00
|
|
|
char *testName, int iterationNumber)
|
|
|
|
{
|
|
|
|
if(runSeed == NULL || suiteName == NULL ||
|
|
|
|
testName == NULL || iterationNumber < 0) {
|
2011-07-25 20:32:31 +03:00
|
|
|
fprintf(stderr, "Error: Incorrect parameter given to GenerateExecKey function\n");
|
2011-07-28 22:11:30 +03:00
|
|
|
return -1;
|
2011-07-24 18:58:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char iterationString[256];
|
|
|
|
memset(iterationString, 0, sizeof(iterationString));
|
|
|
|
|
|
|
|
snprintf(iterationString, sizeof(iterationString), "%d", iterationNumber);
|
|
|
|
|
|
|
|
// combine the parameters
|
|
|
|
const int runSeedLength = strlen(runSeed);
|
|
|
|
const int suiteNameLength = strlen(suiteName);
|
|
|
|
const int testNameLength = strlen(testName);
|
|
|
|
const int iterationStringLength = strlen(iterationString);
|
|
|
|
|
|
|
|
// size of the entire + 3 for slashes and + 1 for '\0'
|
|
|
|
const int entireString = runSeedLength + suiteNameLength +
|
2011-07-25 20:32:31 +03:00
|
|
|
testNameLength + iterationStringLength + 3 + 1;
|
2011-07-24 18:58:36 +03:00
|
|
|
|
2011-07-25 20:32:31 +03:00
|
|
|
char *buffer = SDL_malloc(entireString);
|
|
|
|
if(!buffer) {
|
2011-07-28 22:11:30 +03:00
|
|
|
return -1;
|
2011-07-25 20:32:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_snprintf(buffer, entireString, "%s/%s/%s/%d", runSeed, suiteName,
|
|
|
|
testName, iterationNumber);
|
|
|
|
|
2011-07-24 18:58:36 +03:00
|
|
|
MD5_CTX md5Context;
|
|
|
|
utl_md5Init(&md5Context);
|
|
|
|
|
2011-07-25 20:32:31 +03:00
|
|
|
utl_md5Update(&md5Context, buffer, entireString);
|
2011-07-24 18:58:36 +03:00
|
|
|
utl_md5Final(&md5Context);
|
|
|
|
|
2011-07-25 20:32:31 +03:00
|
|
|
SDL_free(buffer);
|
2011-07-24 18:58:36 +03:00
|
|
|
|
2011-07-28 22:11:30 +03:00
|
|
|
char *execKey = md5Context.digest;
|
2011-07-25 20:32:31 +03:00
|
|
|
|
2011-07-29 16:22:03 +03:00
|
|
|
//! \todo could this be enhanced?
|
2011-07-28 22:11:30 +03:00
|
|
|
int key = execKey[4] << 24 |
|
|
|
|
execKey[9] << 16 |
|
|
|
|
execKey[13] << 8 |
|
|
|
|
execKey[3] << 0;
|
|
|
|
|
|
|
|
return abs(key);
|
2011-07-24 18:58:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-07-28 22:11:30 +03:00
|
|
|
InitFuzzer(int execKey)
|
2011-07-24 18:58:36 +03:00
|
|
|
{
|
2011-07-28 22:11:30 +03:00
|
|
|
utl_randomInit(&rndContext, execKey, execKey / 0xfafafafa);
|
2011-07-24 18:58:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DeinitFuzzer()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
RandomInteger()
|
|
|
|
{
|
2011-07-25 20:32:31 +03:00
|
|
|
return utl_randomInt(&rndContext);
|
2011-07-24 18:58:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-07-25 18:51:57 +03:00
|
|
|
RandomPositiveInteger()
|
2011-07-24 18:58:36 +03:00
|
|
|
{
|
2011-07-25 20:32:31 +03:00
|
|
|
return abs(utl_randomInt(&rndContext));
|
2011-07-25 18:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
RandomIntegerInRange(int min, int max)
|
|
|
|
{
|
|
|
|
if(min > max || (min - max) == 0) {
|
|
|
|
return -1; // Doesn't really make sense to return -1 on error?
|
|
|
|
}
|
|
|
|
|
2011-07-25 20:32:31 +03:00
|
|
|
int number = utl_randomInt(&rndContext);
|
2011-07-24 18:58:36 +03:00
|
|
|
number = abs(number);
|
|
|
|
|
2011-07-25 18:51:57 +03:00
|
|
|
return (number % ((max + 1) - min)) + min;
|
2011-07-24 18:58:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-07-25 18:51:57 +03:00
|
|
|
GenerateBoundaryValueForSize(const int size)
|
2011-07-24 18:58:36 +03:00
|
|
|
{
|
2011-07-25 18:51:57 +03:00
|
|
|
if(size < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-07-24 18:58:36 +03:00
|
|
|
|
2011-07-25 18:51:57 +03:00
|
|
|
const int adjustment = RandomIntegerInRange(-1, 1);
|
|
|
|
int retValue = (1 << (RandomPositiveInteger() % size)) + adjustment;
|
2011-07-24 18:58:36 +03:00
|
|
|
|
|
|
|
return retValue;
|
|
|
|
}
|
|
|
|
|
2011-07-25 18:51:57 +03:00
|
|
|
int
|
|
|
|
RandomUint8BoundaryValue()
|
|
|
|
{
|
|
|
|
return GenerateBoundaryValueForSize(8);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
RandomInt8BoundaryValue()
|
|
|
|
{
|
|
|
|
int value = GenerateBoundaryValueForSize(8);
|
|
|
|
|
|
|
|
return (RandomPositiveInteger() % 2 == 0 ? value : -value);
|
|
|
|
}
|
2011-07-24 18:58:36 +03:00
|
|
|
|
|
|
|
char *
|
|
|
|
RandomAsciiString()
|
|
|
|
{
|
2011-07-25 18:51:57 +03:00
|
|
|
return RandomAsciiStringWithMaximumLength(255);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
RandomAsciiStringWithMaximumLength(int maxSize)
|
|
|
|
{
|
|
|
|
if(maxSize < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-25 20:32:31 +03:00
|
|
|
int size = abs(RandomInteger) % maxSize;
|
2011-07-24 18:58:36 +03:00
|
|
|
char *string = SDL_malloc(size * sizeof(size));
|
|
|
|
|
|
|
|
int counter = 0;
|
|
|
|
for( ; counter < size; ++counter) {
|
2011-07-25 20:32:31 +03:00
|
|
|
string[counter] = (char) RandomIntegerInRange(1, 127);
|
2011-07-24 18:58:36 +03:00
|
|
|
}
|
|
|
|
|
2011-07-25 18:51:57 +03:00
|
|
|
string[counter] = '\0';
|
|
|
|
|
2011-07-24 18:58:36 +03:00
|
|
|
return string;
|
|
|
|
}
|