1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/test/SDL_test_harness.c Fri Nov 30 23:25:34 2012 -0800
1.3 @@ -0,0 +1,142 @@
1.4 +/*
1.5 + Simple DirectMedia Layer
1.6 + Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
1.7 +
1.8 + This software is provided 'as-is', without any express or implied
1.9 + warranty. In no event will the authors be held liable for any damages
1.10 + arising from the use of this software.
1.11 +
1.12 + Permission is granted to anyone to use this software for any purpose,
1.13 + including commercial applications, and to alter it and redistribute it
1.14 + freely, subject to the following restrictions:
1.15 +
1.16 + 1. The origin of this software must not be misrepresented; you must not
1.17 + claim that you wrote the original software. If you use this software
1.18 + in a product, an acknowledgment in the product documentation would be
1.19 + appreciated but is not required.
1.20 + 2. Altered source versions must be plainly marked as such, and must not be
1.21 + misrepresented as being the original software.
1.22 + 3. This notice may not be removed or altered from any source distribution.
1.23 +*/
1.24 +
1.25 +#include "SDL_config.h"
1.26 +
1.27 +#include "SDL_test.h"
1.28 +
1.29 +// TODO: port over harness
1.30 +
1.31 +/**
1.32 + * Generates a random run seed string for the harness. The generated seed
1.33 + * will contain alphanumeric characters (0-9A-Z).
1.34 + *
1.35 + * Note: The returned string needs to be deallocated by the caller.
1.36 + *
1.37 + * \param length The length of the seed string to generate
1.38 + *
1.39 + * \returns The generated seed string
1.40 + */
1.41 +char *
1.42 +SDLTest_GenerateRunSeed(const int length)
1.43 +{
1.44 + char *seed = NULL;
1.45 + SDLTest_RandomContext randomContext;
1.46 + int counter;
1.47 +
1.48 + // Sanity check input
1.49 + if (length <= 0) {
1.50 + SDLTest_LogError("The length of the harness seed must be >0.");
1.51 + return NULL;
1.52 + }
1.53 +
1.54 + // Allocate output buffer
1.55 + seed = (char *)SDL_malloc((length + 1) * sizeof(char));
1.56 + if (seed == NULL) {
1.57 + SDLTest_LogError("SDL_malloc for run seed output buffer failed.");
1.58 + return NULL;
1.59 + }
1.60 +
1.61 + // Generate a random string of alphanumeric characters
1.62 + SDLTest_RandomInitTime(&randomContext);
1.63 + for (counter = 0; counter < length - 1; ++counter) {
1.64 + unsigned int number = SDLTest_Random(&randomContext);
1.65 + char ch = (char) (number % (91 - 48)) + 48;
1.66 + if (ch >= 58 && ch <= 64) {
1.67 + ch = 65;
1.68 + }
1.69 + seed[counter] = ch;
1.70 + }
1.71 + seed[counter] = '\0';
1.72 +
1.73 + return seed;
1.74 +}
1.75 +
1.76 +/**
1.77 + * Generates an execution key for the fuzzer.
1.78 + *
1.79 + * \param runSeed The run seed to use
1.80 + * \param suiteName The name of the test suite
1.81 + * \param testName The name of the test
1.82 + * \param iteration The iteration count
1.83 + *
1.84 + * \returns The generated execution key to initialize the fuzzer with.
1.85 + *
1.86 + */
1.87 +Uint64
1.88 +SDLTest_GenerateExecKey(char *runSeed, char *suiteName, char *testName, int iteration)
1.89 +{
1.90 + SDLTest_Md5Context md5Context;
1.91 + Uint64 *keys;
1.92 + char iterationString[16];
1.93 + Uint32 runSeedLength;
1.94 + Uint32 suiteNameLength;
1.95 + Uint32 testNameLength;
1.96 + Uint32 iterationStringLength;
1.97 + Uint32 entireStringLength;
1.98 + char *buffer;
1.99 +
1.100 + if (runSeed == NULL || strlen(runSeed)==0) {
1.101 + SDLTest_LogError("Invalid runSeed string.");
1.102 + return -1;
1.103 + }
1.104 +
1.105 + if (suiteName == NULL || strlen(suiteName)==0) {
1.106 + SDLTest_LogError("Invalid suiteName string.");
1.107 + return -1;
1.108 + }
1.109 +
1.110 + if (testName == NULL || strlen(testName)==0) {
1.111 + SDLTest_LogError("Invalid testName string.");
1.112 + return -1;
1.113 + }
1.114 +
1.115 + if (iteration <= 0) {
1.116 + SDLTest_LogError("Invalid iteration count.");
1.117 + return -1;
1.118 + }
1.119 +
1.120 + // Convert iteration number into a string
1.121 + memset(iterationString, 0, sizeof(iterationString));
1.122 + SDL_snprintf(iterationString, sizeof(iterationString) - 1, "%d", iteration);
1.123 +
1.124 + // Combine the parameters into single string
1.125 + runSeedLength = strlen(runSeed);
1.126 + suiteNameLength = strlen(suiteName);
1.127 + testNameLength = strlen(testName);
1.128 + iterationStringLength = strlen(iterationString);
1.129 + entireStringLength = runSeedLength + suiteNameLength + testNameLength + iterationStringLength + 1;
1.130 + buffer = (char *)SDL_malloc(entireStringLength);
1.131 + if (buffer == NULL) {
1.132 + SDLTest_LogError("SDL_malloc failed to allocate buffer for execKey generation.");
1.133 + return 0;
1.134 + }
1.135 + SDL_snprintf(buffer, entireStringLength, "%s%s%s%d", runSeed, suiteName, testName, iteration);
1.136 +
1.137 + // Hash string and use half of the digest as 64bit exec key
1.138 + SDLTest_Md5Init(&md5Context);
1.139 + SDLTest_Md5Update(&md5Context, (unsigned char *)buffer, entireStringLength);
1.140 + SDLTest_Md5Final(&md5Context);
1.141 + SDL_free(buffer);
1.142 + keys = (Uint64 *)md5Context.digest;
1.143 +
1.144 + return keys[0];
1.145 +}