src/test/SDL_test_harness.c
changeset 6717 2acd95060548
child 6718 918ba414168b
equal deleted inserted replaced
6716:1616f6b3738c 6717:2acd95060548
       
     1 /*
       
     2   Simple DirectMedia Layer
       
     3   Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
       
     4 
       
     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.
       
     8 
       
     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:
       
    12 
       
    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.
       
    20 */
       
    21 
       
    22 #include "SDL_config.h"
       
    23 
       
    24 #include "SDL_test.h"
       
    25 
       
    26 // TODO: port over harness
       
    27 
       
    28 /**
       
    29  * Generates a random run seed string for the harness. The generated seed
       
    30  * will contain alphanumeric characters (0-9A-Z).
       
    31  *
       
    32  * Note: The returned string needs to be deallocated by the caller.
       
    33  *
       
    34  * \param length The length of the seed string to generate
       
    35  *
       
    36  * \returns The generated seed string
       
    37  */
       
    38 char *
       
    39 SDLTest_GenerateRunSeed(const int length)
       
    40 {
       
    41 	char *seed = NULL;
       
    42 	SDLTest_RandomContext randomContext;
       
    43 	int counter;
       
    44 
       
    45 	// Sanity check input
       
    46 	if (length <= 0) {
       
    47 		SDLTest_LogError("The length of the harness seed must be >0.");
       
    48 		return NULL;
       
    49 	}
       
    50 
       
    51 	// Allocate output buffer
       
    52 	seed = (char *)SDL_malloc((length + 1) * sizeof(char));
       
    53 	if (seed == NULL) {
       
    54 		SDLTest_LogError("SDL_malloc for run seed output buffer failed.");
       
    55 		return NULL;
       
    56 	}
       
    57 
       
    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) {
       
    64 			ch = 65;
       
    65 		}
       
    66 		seed[counter] = ch;
       
    67 	}
       
    68 	seed[counter] = '\0';
       
    69 
       
    70 	return seed;
       
    71 }
       
    72 
       
    73 /**
       
    74  * Generates an execution key for the fuzzer.
       
    75  *
       
    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
       
    80  *
       
    81  * \returns The generated execution key to initialize the fuzzer with.
       
    82  *
       
    83  */
       
    84 Uint64
       
    85 SDLTest_GenerateExecKey(char *runSeed, char *suiteName, char *testName, int iteration)
       
    86 {
       
    87 	SDLTest_Md5Context md5Context;
       
    88 	Uint64 *keys;
       
    89 	char iterationString[16];
       
    90 	Uint32 runSeedLength;
       
    91 	Uint32 suiteNameLength;
       
    92 	Uint32 testNameLength;
       
    93 	Uint32 iterationStringLength;
       
    94 	Uint32 entireStringLength;
       
    95 	char *buffer;
       
    96 
       
    97 	if (runSeed == NULL || strlen(runSeed)==0) {
       
    98 		SDLTest_LogError("Invalid runSeed string.");
       
    99 		return -1;
       
   100 	}
       
   101 
       
   102 	if (suiteName == NULL || strlen(suiteName)==0) {
       
   103 		SDLTest_LogError("Invalid suiteName string.");
       
   104 		return -1;
       
   105 	}
       
   106 
       
   107 	if (testName == NULL || strlen(testName)==0) {
       
   108 		SDLTest_LogError("Invalid testName string.");
       
   109 		return -1;
       
   110 	}
       
   111 
       
   112 	if (iteration <= 0) {
       
   113 		SDLTest_LogError("Invalid iteration count.");
       
   114 		return -1;
       
   115 	}
       
   116 
       
   117 	// Convert iteration number into a string
       
   118 	memset(iterationString, 0, sizeof(iterationString));
       
   119 	SDL_snprintf(iterationString, sizeof(iterationString) - 1, "%d", iteration);
       
   120 
       
   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.");
       
   130 		return 0;
       
   131 	}
       
   132 	SDL_snprintf(buffer, entireStringLength, "%s%s%s%d", runSeed, suiteName, testName, iteration);
       
   133 
       
   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);
       
   138 	SDL_free(buffer);
       
   139 	keys = (Uint64 *)md5Context.digest;
       
   140 
       
   141 	return keys[0];
       
   142 }