SDL-mirror/test/test-automation/fuzzer/fuzzer.c

151 lines
2.8 KiB
C
Raw Normal View History

2011-07-24 18:58:36 +03:00
#include "../SDL_test.h"
#include "fuzzer.h"
//! context for test-specific random number generator
static RND_CTX rndContext;
2011-07-24 18:58:36 +03:00
2011-07-28 22:11:30 +03:00
int
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) {
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 +
testNameLength + iterationStringLength + 3 + 1;
2011-07-24 18:58:36 +03:00
char *buffer = SDL_malloc(entireString);
if(!buffer) {
2011-07-28 22:11:30 +03:00
return -1;
}
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);
utl_md5Update(&md5Context, buffer, entireString);
2011-07-24 18:58:36 +03:00
utl_md5Final(&md5Context);
SDL_free(buffer);
2011-07-24 18:58:36 +03:00
2011-07-28 22:11:30 +03:00
char *execKey = md5Context.digest;
//! \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()
{
return utl_randomInt(&rndContext);
2011-07-24 18:58:36 +03:00
}
int
RandomPositiveInteger()
2011-07-24 18:58:36 +03:00
{
return abs(utl_randomInt(&rndContext));
}
int
RandomIntegerInRange(int min, int max)
{
if(min > max || (min - max) == 0) {
return -1; // Doesn't really make sense to return -1 on error?
}
int number = utl_randomInt(&rndContext);
2011-07-24 18:58:36 +03:00
number = abs(number);
return (number % ((max + 1) - min)) + min;
2011-07-24 18:58:36 +03:00
}
int
GenerateBoundaryValueForSize(const int size)
2011-07-24 18:58:36 +03:00
{
if(size < 0) {
return -1;
}
2011-07-24 18:58:36 +03:00
const int adjustment = RandomIntegerInRange(-1, 1);
int retValue = (1 << (RandomPositiveInteger() % size)) + adjustment;
2011-07-24 18:58:36 +03:00
return retValue;
}
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()
{
return RandomAsciiStringWithMaximumLength(255);
}
char *
RandomAsciiStringWithMaximumLength(int maxSize)
{
if(maxSize < 0) {
return NULL;
}
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) {
string[counter] = (char) RandomIntegerInRange(1, 127);
2011-07-24 18:58:36 +03:00
}
string[counter] = '\0';
2011-07-24 18:58:36 +03:00
return string;
}