2 Simple DirectMedia Layer
3 Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
22 #include "SDL_config.h"
26 // TODO: port over harness
29 * Generates a random run seed string for the harness. The generated seed
30 * will contain alphanumeric characters (0-9A-Z).
32 * Note: The returned string needs to be deallocated by the caller.
34 * \param length The length of the seed string to generate
36 * \returns The generated seed string
39 SDLTest_GenerateRunSeed(const int length)
42 SDLTest_RandomContext randomContext;
47 SDLTest_LogError("The length of the harness seed must be >0.");
51 // Allocate output buffer
52 seed = (char *)SDL_malloc((length + 1) * sizeof(char));
54 SDLTest_LogError("SDL_malloc for run seed output buffer failed.");
58 // Generate a random string of alphanumeric characters
59 SDLTest_RandomInitTime(&randomContext);
60 for (counter = 0; counter < length - 1; ++counter) {
61 unsigned int number = SDLTest_Random(&randomContext);
62 char ch = (char) (number % (91 - 48)) + 48;
63 if (ch >= 58 && ch <= 64) {
74 * Generates an execution key for the fuzzer.
76 * \param runSeed The run seed to use
77 * \param suiteName The name of the test suite
78 * \param testName The name of the test
79 * \param iteration The iteration count
81 * \returns The generated execution key to initialize the fuzzer with.
85 SDLTest_GenerateExecKey(char *runSeed, char *suiteName, char *testName, int iteration)
87 SDLTest_Md5Context md5Context;
89 char iterationString[16];
91 Uint32 suiteNameLength;
92 Uint32 testNameLength;
93 Uint32 iterationStringLength;
94 Uint32 entireStringLength;
97 if (runSeed == NULL || strlen(runSeed)==0) {
98 SDLTest_LogError("Invalid runSeed string.");
102 if (suiteName == NULL || strlen(suiteName)==0) {
103 SDLTest_LogError("Invalid suiteName string.");
107 if (testName == NULL || strlen(testName)==0) {
108 SDLTest_LogError("Invalid testName string.");
112 if (iteration <= 0) {
113 SDLTest_LogError("Invalid iteration count.");
117 // Convert iteration number into a string
118 memset(iterationString, 0, sizeof(iterationString));
119 SDL_snprintf(iterationString, sizeof(iterationString) - 1, "%d", iteration);
121 // Combine the parameters into single string
122 runSeedLength = strlen(runSeed);
123 suiteNameLength = strlen(suiteName);
124 testNameLength = strlen(testName);
125 iterationStringLength = strlen(iterationString);
126 entireStringLength = runSeedLength + suiteNameLength + testNameLength + iterationStringLength + 1;
127 buffer = (char *)SDL_malloc(entireStringLength);
128 if (buffer == NULL) {
129 SDLTest_LogError("SDL_malloc failed to allocate buffer for execKey generation.");
132 SDL_snprintf(buffer, entireStringLength, "%s%s%s%d", runSeed, suiteName, testName, iteration);
134 // Hash string and use half of the digest as 64bit exec key
135 SDLTest_Md5Init(&md5Context);
136 SDLTest_Md5Update(&md5Context, (unsigned char *)buffer, entireStringLength);
137 SDLTest_Md5Final(&md5Context);
139 keys = (Uint64 *)md5Context.digest;