Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixing the previous commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkauppila committed Jul 24, 2011
1 parent 3153d49 commit c163a88
Show file tree
Hide file tree
Showing 9 changed files with 1,056 additions and 1 deletion.
119 changes: 119 additions & 0 deletions test/test-automation/fuzzer/fuzzer.c
@@ -0,0 +1,119 @@
#include <stdio.h>
#include <stdlib.h>

#include <string.h>


#include "../SDL_test.h"

#include "fuzzer.h"


//! context for test-specific random number generator
RND_CTX rndContext3;

int
GenerateExecKey(CRC32_CTX crcContext, char *runSeed, char *suiteName,
char *testName, int iterationNumber)
{
if(runSeed == NULL || suiteName == NULL ||
testName == NULL || iterationNumber < 0) {
fprintf(stderr, "Incorrect parameter given to GenerateExecKey function\n");
return -1;
}

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 + iterationString + 3 + 1;

int result = 0;

/* Let's take a hash from the strings separately because
* it's really slow to calculate md5 or crc32 for a really long string
* like 'runSeed/testSuiteName/testName/iteration'
*/
MD5_CTX md5Context;
utl_md5Init(&md5Context);

utl_md5Update(&md5Context, runSeed, runSeedLength);
utl_md5Update(&md5Context, suiteName, suiteNameLength);
utl_md5Update(&md5Context, testName, testNameLength);
utl_md5Update(&md5Context, iterationString, iterationStringLength);

utl_md5Final(&md5Context);

utl_crc32Calc(&crcContext, md5Context.digest, sizeof(md5Context.digest), &result);

return result;
}

void
InitFuzzer(const int execKey)
{
utl_randomInit(&rndContext3, globalExecKey, globalExecKey / 0xfafafafa);
}

void
DeinitFuzzer()
{

}

int
RandomInteger()
{
return utl_randomInt(&rndContext3);
}

int
RandomPositiveIntegerInRange(int min, int max)
{
int number = utl_randomInt(&rndContext3);
number = abs(number);

return (number % (max - min)) + min;
}

int
RandomBoundaryValue(const 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;

do {
int index = RandomPositiveIntegerInRange(0, 10);
retValue = boundaryValues[index];

} while( !(retValue <= max) );

return retValue;
}


char *
RandomAsciiString()
{
const int size = abs(RandomInteger);
char *string = SDL_malloc(size * sizeof(size));

int counter = 0;
for( ; counter < size; ++counter) {
char character = (char) RandomPositiveIntegerInRange(0, 127);
string[counter] = character;
}

return string;
}
80 changes: 80 additions & 0 deletions test/test-automation/fuzzer/fuzzer.h
@@ -0,0 +1,80 @@
/*
Copyright (C) 2011 Markus Kauppila <markus.kauppila@gmail.com>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

#ifndef _FUZZER_H
#define _FUZZER_H

#include "utl_crc32.h"
#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)
*/
void DeinitFuzzer();


/*!
* Returns random integer
*
* \returns Generated integer
*/
int RandomInteger();

/*!
* Returns positive integer in range [min, max]
*
* \returns Generated integer
*/
int RandomPositiveIntegerInRange(int min, int max);

/*!
* Generates random ASCII string
*
* \returns newly allocated random string
*/
char *RandomAsciiString();

/*!
* Generates a random boundary value. Max is the biggest
* value the function can return.
*
* \returns a boundary value
*/
int RandomBoundaryValue(const int max);

/*!
* Generates execution key (used for random seed) for a test
*
* \param runSeed Seed of the harness
* \param suiteName Test suite name
* \param testName Test name
* \param iteration Number of test iteration
*
* \return Generated execution key
*/
int GenerateExecKey(CRC32_CTX crcContext, char *runSeed, char *suiteName, char *testName, int interationNumber);

#endif
125 changes: 125 additions & 0 deletions test/test-automation/fuzzer/utl_crc32.c
@@ -0,0 +1,125 @@

#include "utl_crc32.h"

int utl_crc32Init(CRC32_CTX *crcContext)
{
int i,j;
CrcUint32 c;

/* Sanity check context pointer */
if (crcContext==NULL) {
return(-1);
}

/*
* Build auxiliary table for parallel byte-at-a-time CRC-32
*/
#ifdef ORIGINAL_METHOD
for (i = 0; i < 256; ++i) {
for (c = i << 24, j = 8; j > 0; --j) {
c = c & 0x80000000 ? (c << 1) ^ CRC32_POLY : (c << 1);
}
crcContext->crc32_table[i] = c;
}
#else
for (i=0; i<256; i++) {
c = i;
for (j=8; j>0; j--) {
if (c & 1) {
c = (c >> 1) ^ CRC32_POLY;
} else {
c >>= 1;
}
}
crcContext->crc32_table[i] = c;
}
#endif

return(0);
}

/* Complete CRC32 calculation on a memory block */

int utl_crc32Calc(CRC32_CTX * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32)
{
if (utl_crc32CalcStart(crcContext,crc32)) {
return(-1);
}
if (utl_crc32CalcBuffer(crcContext, inBuf, inLen, crc32)) {
return(-1);
}
if (utl_crc32CalcEnd(crcContext, crc32)) {
return(-1);
}
return(0);
}

/* Start crc calculation */

int utl_crc32CalcStart(CRC32_CTX * crcContext, CrcUint32 *crc32)
{
/* Sanity check pointers */
if (crcContext==NULL) {
*crc32=0;
return(-1);
}

/*
* Preload shift register, per CRC-32 spec
*/
*crc32 = 0xffffffff;

return(0);
}

/* Finish crc calculation */

int utl_crc32CalcEnd(CRC32_CTX * crcContext, CrcUint32 *crc32)
{
/*
* Return complement, per CRC-32 spec
*/
*crc32 = (~(*crc32));

return(0);
}

/* Include memory block in crc */

int utl_crc32CalcBuffer(CRC32_CTX * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32)
{
CrcUint8 *p;
register CrcUint32 crc;

/* Sanity check pointers */
if (crcContext==NULL) {
*crc32=0;
return(-1);
}


/*
* Calculate CRC from data
*/
crc = *crc32;
for (p = inBuf; inLen > 0; ++p, --inLen) {
#ifdef ORIGINAL_METHOD
crc = (crc << 8) ^ crcContext->crc32_table[(crc >> 24) ^ *p];
#else
crc = ((crc >> 8) & 0x00FFFFFF) ^ crcContext->crc32_table[ (crc ^ *p) & 0xFF ];
#endif
}
*crc32 = crc;

return(0);
}

int utl_crc32Done(CRC32_CTX * crcContext)
{
/* Sanity check context pointer */
if (crcContext==NULL) {
return(-1);
}

return(0);
}

0 comments on commit c163a88

Please sign in to comment.