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

Latest commit

 

History

History
1188 lines (972 loc) · 31.8 KB

File metadata and controls

1188 lines (972 loc) · 31.8 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
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.
*/
May 23, 2011
May 23, 2011
21
#include "SDL/SDL.h"
22
23
24
25
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Jun 5, 2011
Jun 5, 2011
26
#include <string.h>
Jun 7, 2011
Jun 7, 2011
27
#include <dirent.h>
Jun 5, 2011
Jun 5, 2011
28
29
30
#include <sys/types.h>
Jul 24, 2011
Jul 24, 2011
31
32
33
#include "fuzzer/fuzzer.h"
Jul 4, 2011
Jul 4, 2011
34
35
#include "config.h"
May 30, 2011
May 30, 2011
36
#include "SDL_test.h"
Jul 6, 2011
Jul 6, 2011
37
38
39
#include "plain_logger.h"
#include "xml_logger.h"
Jun 26, 2011
Jun 26, 2011
40
#include "logger.h"
Jul 18, 2011
Jul 18, 2011
41
#include "support.h"
Jun 9, 2011
Jun 9, 2011
42
May 30, 2011
May 30, 2011
43
//!< Function pointer to a test case function
Jun 9, 2011
Jun 9, 2011
44
typedef void (*TestCaseFp)(void *arg);
Jun 4, 2011
Jun 4, 2011
45
//!< Function pointer to a test case init function
Jul 11, 2011
Jul 11, 2011
46
typedef void (*InitTestInvironmentFp)(void);
Jun 4, 2011
Jun 4, 2011
47
//!< Function pointer to a test case quit function
Jul 11, 2011
Jul 11, 2011
48
49
50
51
52
typedef int (*QuitTestInvironmentFp)(void);
//!< Function pointer to a test case set up function
typedef void (*TestCaseSetUpFp)(void *arg);
//!< Function pointer to a test case tear down function
typedef void (*TestCaseTearDownFp)(void *arg);
Jul 11, 2011
Jul 11, 2011
53
54
//!< Function pointer to a function which returns the failed assert count
typedef int (*CountFailedAssertsFp)(void);
Jun 9, 2011
Jun 9, 2011
55
May 30, 2011
May 30, 2011
56
Jun 1, 2011
Jun 1, 2011
57
58
//!< Flag for executing tests in-process
static int execute_inproc = 0;
Jun 13, 2011
Jun 13, 2011
59
60
//!< Flag for only printing out the test names
static int only_print_tests = 0;
Jun 5, 2011
Jun 5, 2011
61
62
63
64
//!< Flag for executing only test with selected name
static int only_selected_test = 0;
//!< Flag for executing only the selected test suite
static int only_selected_suite = 0;
Jun 9, 2011
Jun 9, 2011
65
66
//!< Flag for executing only tests that contain certain string in their name
static int only_tests_with_string = 0;
Jun 26, 2011
Jun 26, 2011
67
68
//!< Flag for enabling XML logging
static int xml_enabled = 0;
Jun 30, 2011
Jun 30, 2011
69
70
//! Flag for enabling user-supplied style sheet for XML test report
static int custom_xsl_enabled = 0;
Jul 1, 2011
Jul 1, 2011
71
72
//! Flag for disabling xsl-style from xml report
static int xsl_enabled = 0;
Jul 18, 2011
Jul 18, 2011
73
74
//! Flag for enabling universal timeout for tests
static int universal_timeout_enabled = 0;
Jul 27, 2011
Jul 27, 2011
75
76
77
//! Flag for enabling verbose logging
static int enable_verbose_logger = 0;
Jun 5, 2011
Jun 5, 2011
78
Jun 8, 2011
Jun 8, 2011
79
//!< Size of the test and suite name buffers
Jun 7, 2011
Jun 7, 2011
80
#define NAME_BUFFER_SIZE 1024
Jun 5, 2011
Jun 5, 2011
81
82
83
84
//!< Name of the selected test
char selected_test_name[NAME_BUFFER_SIZE];
//!< Name of the selected suite
char selected_suite_name[NAME_BUFFER_SIZE];
Jun 4, 2011
Jun 4, 2011
85
Jun 9, 2011
Jun 9, 2011
86
87
88
//!< substring of test case name
char testcase_name_substring[NAME_BUFFER_SIZE];
Jun 30, 2011
Jun 30, 2011
89
90
//! Name for user-supplied XSL style sheet name
char xsl_stylesheet_name[NAME_BUFFER_SIZE];
Jul 24, 2011
Jul 24, 2011
91
//! User-suppled timeout value for tests
Jul 18, 2011
Jul 18, 2011
92
93
int universal_timeout = -1;
Jun 8, 2011
Jun 8, 2011
94
95
96
//! Default directory of the test suites
#define DEFAULT_TEST_DIRECTORY "tests/"
Jul 25, 2011
Jul 25, 2011
97
98
char *globalExecKey = NULL;
char *runSeed = "seed";
Jul 24, 2011
Jul 24, 2011
99
Jul 25, 2011
Jul 25, 2011
100
char *userExecKey = NULL;
Jul 24, 2011
Jul 24, 2011
101
102
103
104
105
106
107
108
//! How man time a test will be invocated
int testInvocationCount = 1;
// \todo move this upper!! (and add comments)
int totalTestFailureCount = 0, totalTestPassCount = 0, totalTestSkipCount = 0;
int testFailureCount = 0, testPassCount = 0, testSkipCount = 0;
Jun 9, 2011
Jun 9, 2011
109
Jun 7, 2011
Jun 7, 2011
110
/*!
Jun 9, 2011
Jun 9, 2011
111
112
* Holds information about test suite such as it's name
* and pointer to dynamic library. Implemented as linked list.
Jun 7, 2011
Jun 7, 2011
113
114
*/
typedef struct TestSuiteReference {
Jun 9, 2011
Jun 9, 2011
115
char *name; //!< test suite name
Jul 1, 2011
Jul 1, 2011
116
char *directoryPath; //!< test suites path (eg. tests/libtestsuite)
Jun 9, 2011
Jun 9, 2011
117
void *library; //!< pointer to shared/dynamic library implementing the suite
Jun 9, 2011
Jun 9, 2011
118
Jun 7, 2011
Jun 7, 2011
119
120
121
struct TestSuiteReference *next; //!< Pointer to next item in the list
} TestSuiteReference;
Jun 9, 2011
Jun 9, 2011
122
123
124
125
126
127
/*!
* Holds information about the tests that will be executed.
*
* Implemented as linked list.
*/
Jun 9, 2011
Jun 9, 2011
128
129
130
131
typedef struct TestCaseItem {
char *testName;
char *suiteName;
Jun 26, 2011
Jun 26, 2011
132
133
134
135
char *description;
long requirements;
long timeout;
Jul 11, 2011
Jul 11, 2011
136
137
InitTestInvironmentFp initTestEnvironment;
TestCaseSetUpFp testSetUp;
Jun 9, 2011
Jun 9, 2011
138
TestCaseFp testCase;
Jul 11, 2011
Jul 11, 2011
139
140
TestCaseTearDownFp testTearDown;
QuitTestInvironmentFp quitTestEnvironment;
Jun 9, 2011
Jun 9, 2011
141
Jul 11, 2011
Jul 11, 2011
142
143
CountFailedAssertsFp countFailedAsserts;
Jun 9, 2011
Jun 9, 2011
144
struct TestCaseItem *next;
Jun 9, 2011
Jun 9, 2011
145
146
147
148
149
} TestCase;
/*! Some function prototypes. Add the rest of functions and move to runner.h */
TestCaseFp LoadTestCaseFunction(void *suite, char *testName);
Jul 11, 2011
Jul 11, 2011
150
151
InitTestInvironmentFp LoadInitTestInvironmentFunction(void *suite);
QuitTestInvironmentFp LoadQuitTestInvironmentFunction(void *suite);
Jun 9, 2011
Jun 9, 2011
152
TestCaseReference **QueryTestCaseReferences(void *library);
Jul 11, 2011
Jul 11, 2011
153
154
TestCaseSetUpFp LoadTestSetUpFunction(void *suite);
TestCaseTearDownFp LoadTestTearDownFunction(void *suite);
Jul 11, 2011
Jul 11, 2011
155
CountFailedAssertsFp LoadCountFailedAssertsFunction(void *suite);
Jul 18, 2011
Jul 18, 2011
156
void KillHungTestInChildProcess(int signum);
Jul 11, 2011
Jul 11, 2011
157
Jun 9, 2011
Jun 9, 2011
158
Jul 6, 2011
Jul 6, 2011
159
160
161
162
163
164
165
166
167
168
169
170
/*! Pointers to selected logger implementation */
RunStartedFp RunStarted = NULL;
RunEndedFp RunEnded = NULL;
SuiteStartedFp SuiteStarted = NULL;
SuiteEndedFp SuiteEnded = NULL;
TestStartedFp TestStarted = NULL;
TestEndedFp TestEnded = NULL;
AssertFp Assert = NULL;
AssertWithValuesFp AssertWithValues = NULL;
AssertSummaryFp AssertSummary = NULL;
LogFp Log = NULL;
Jun 4, 2011
Jun 4, 2011
171
Jun 30, 2011
Jun 30, 2011
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*!
* Scans the tests/ directory and returns the names
* of the dynamic libraries implementing the test suites.
*
* Note: currently function assumes that test suites names
* are in following format: libtestsuite.dylib or libtestsuite.so.
*
* Note: if only_selected_suite flags is non-zero, only the selected
* test will be loaded.
*
* \param directoryName Name of the directory which will be scanned
* \param extension What file extension is used with dynamic objects
*
* \return Pointer to TestSuiteReference which holds all the info about suites
*/
TestSuiteReference *
ScanForTestSuites(char *directoryName, char *extension)
{
typedef struct dirent Entry;
DIR *directory = opendir(directoryName);
TestSuiteReference *suites = NULL;
Entry *entry = NULL;
Jul 11, 2011
Jul 11, 2011
194
Jun 30, 2011
Jun 30, 2011
195
if(!directory) {
Jul 11, 2011
Jul 11, 2011
196
197
198
fprintf(stderr, "Failed to open test suite directory: %s\n", directoryName);
perror("Error message");
exit(1);
Jun 30, 2011
Jun 30, 2011
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
}
while(entry = readdir(directory)) {
if(strlen(entry->d_name) > 2) { // discards . and ..
const char *delimiters = ".";
char *name = strtok(entry->d_name, delimiters);
char *ext = strtok(NULL, delimiters);
// filter out all other suites but the selected test suite
int ok = 1;
if(only_selected_suite) {
ok = SDL_strncmp(selected_suite_name, name, NAME_BUFFER_SIZE) == 0;
}
if(ok && SDL_strcmp(ext, extension) == 0) {
// create test suite reference
TestSuiteReference *reference = (TestSuiteReference *) SDL_malloc(sizeof(TestSuiteReference));
Jul 11, 2011
Jul 11, 2011
216
217
218
if(reference == NULL) {
fprintf(stderr, "Allocating TestSuiteReference failed\n");
}
Jun 30, 2011
Jun 30, 2011
219
Jul 11, 2011
Jul 11, 2011
220
memset(reference, 0, sizeof(TestSuiteReference));
Jun 30, 2011
Jun 30, 2011
221
Jul 1, 2011
Jul 1, 2011
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const int dirSize = SDL_strlen(directoryName);
const int extSize = SDL_strlen(ext);
const int nameSize = SDL_strlen(name) + 1;
// copy the name
reference->name = SDL_malloc(nameSize * sizeof(char));
if(reference->name == NULL) {
SDL_free(reference);
return NULL;
}
SDL_snprintf(reference->name, nameSize, "%s", name);
// copy the directory path
const int dpSize = dirSize + nameSize + 1 + extSize + 1;
reference->directoryPath = SDL_malloc(dpSize * sizeof(char));
if(reference->directoryPath == NULL) {
SDL_free(reference->name);
SDL_free(reference);
return NULL;
}
SDL_snprintf(reference->directoryPath, dpSize, "%s%s.%s",
directoryName, name, ext);
Jun 30, 2011
Jun 30, 2011
245
246
247
248
249
250
251
252
253
254
255
256
257
reference->next = suites;
suites = reference;
}
}
}
closedir(directory);
return suites;
}
Jun 1, 2011
Jun 1, 2011
258
259
260
/*!
* Loads test suite which is implemented as dynamic library.
*
Jul 1, 2011
Jul 1, 2011
261
* \param suite Reference to test suite that'll be loaded
Jun 1, 2011
Jun 1, 2011
262
263
264
265
*
* \return Pointer to loaded test suite, or NULL if library could not be loaded
*/
void *
Jul 1, 2011
Jul 1, 2011
266
LoadTestSuite(const TestSuiteReference *suite)
Jun 1, 2011
Jun 1, 2011
267
{
Jul 1, 2011
Jul 1, 2011
268
void *library = SDL_LoadObject(suite->directoryPath);
269
if(library == NULL) {
Jul 1, 2011
Jul 1, 2011
270
fprintf(stderr, "Loading %s failed\n", suite->name);
May 31, 2011
May 31, 2011
271
fprintf(stderr, "%s\n", SDL_GetError());
May 26, 2011
May 26, 2011
274
275
276
return library;
}
Jun 5, 2011
Jun 5, 2011
277
Jun 9, 2011
Jun 9, 2011
278
279
280
281
282
283
284
285
286
287
/*!
* Goes through all the given TestSuiteReferences
* and loads the dynamic libraries. Updates the suites
* parameter on-the-fly and returns it.
*
* \param suites Suites that will be loaded
*
* \return Updated TestSuiteReferences with pointer to loaded libraries
*/
TestSuiteReference *
Jul 1, 2011
Jul 1, 2011
288
LoadTestSuites(TestSuiteReference *suites)
Jun 9, 2011
Jun 9, 2011
289
290
291
{
TestSuiteReference *reference = NULL;
for(reference = suites; reference; reference = reference->next) {
Jul 1, 2011
Jul 1, 2011
292
reference->library = LoadTestSuite(reference);
Jun 9, 2011
Jun 9, 2011
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
}
return suites;
}
/*!
* Unloads the given TestSuiteReferences. Frees all
* the allocated resources including the dynamic libraries.
*
* \param suites TestSuiteReferences for deallocation process
*/
void
UnloadTestSuites(TestSuiteReference *suites)
{
TestSuiteReference *ref = suites;
while(ref) {
SDL_free(ref->name);
Jul 1, 2011
Jul 1, 2011
311
SDL_free(ref->directoryPath);
Jun 9, 2011
Jun 9, 2011
312
313
314
315
316
317
318
319
320
321
322
SDL_UnloadObject(ref->library);
TestSuiteReference *temp = ref->next;
SDL_free(ref);
ref = temp;
}
suites = NULL;
}
Jul 11, 2011
Jul 11, 2011
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*!
* Goes through the previously loaded test suites and
* loads test cases from them. Test cases are filtered
* during the process. Function will only return the
* test cases which aren't filtered out.
*
* \param suites previously loaded test suites
*
* \return Test cases that survived filtering process.
*/
TestCase *
LoadTestCases(TestSuiteReference *suites)
{
TestCase *testCases = NULL;
TestSuiteReference *suiteReference = NULL;
for(suiteReference = suites; suiteReference; suiteReference = suiteReference->next) {
TestCaseReference **tests = QueryTestCaseReferences(suiteReference->library);
TestCaseReference *testReference = NULL;
int counter = 0;
for(testReference = tests[counter]; testReference; testReference = tests[++counter]) {
void *suite = suiteReference->library;
// Load test case functions
InitTestInvironmentFp initTestEnvironment = LoadInitTestInvironmentFunction(suiteReference->library);
QuitTestInvironmentFp quitTestEnvironment = LoadQuitTestInvironmentFunction(suiteReference->library);
TestCaseSetUpFp testSetUp = LoadTestSetUpFunction(suiteReference->library);
TestCaseTearDownFp testTearDown = LoadTestTearDownFunction(suiteReference->library);
TestCaseFp testCase = LoadTestCaseFunction(suiteReference->library, testReference->name);
CountFailedAssertsFp countFailedAsserts = LoadCountFailedAssertsFunction(suiteReference->library);
// Do the filtering
if(FilterTestCase(testReference)) {
TestCase *item = SDL_malloc(sizeof(TestCase));
memset(item, 0, sizeof(TestCase));
item->initTestEnvironment = initTestEnvironment;
item->quitTestEnvironment = quitTestEnvironment;
item->testSetUp = testSetUp;
item->testTearDown = testTearDown;
item->testCase = testCase;
item->countFailedAsserts = countFailedAsserts;
// copy suite name
int length = SDL_strlen(suiteReference->name) + 1;
item->suiteName = SDL_malloc(length);
strncpy(item->suiteName, suiteReference->name, length);
// copy test name
length = SDL_strlen(testReference->name) + 1;
item->testName = SDL_malloc(length);
strncpy(item->testName, testReference->name, length);
// copy test description
length = SDL_strlen(testReference->description) + 1;
item->description = SDL_malloc(length);
strncpy(item->description, testReference->description, length);
item->requirements = testReference->requirements;
item->timeout = testReference->timeout;
// prepend the list
item->next = testCases;
testCases = item;
//printf("Added test: %s\n", testReference->name);
}
}
}
return testCases;
}
/*!
* Unloads the given TestCases. Frees all the resources
* allocated for test cases.
*
* \param testCases Test cases to be deallocated
*/
void
UnloadTestCases(TestCase *testCases)
{
TestCase *ref = testCases;
while(ref) {
SDL_free(ref->testName);
SDL_free(ref->suiteName);
SDL_free(ref->description);
TestCase *temp = ref->next;
SDL_free(ref);
ref = temp;
}
testCases = NULL;
}
/*!
* Filters a test case based on its properties in TestCaseReference and user
* preference.
*
* \return Non-zero means test will be added to execution list, zero means opposite
*/
int
FilterTestCase(TestCaseReference *testReference)
{
int retVal = 1;
if(testReference->enabled == TEST_DISABLED) {
retVal = 0;
}
if(only_selected_test) {
if(SDL_strncmp(testReference->name, selected_test_name, NAME_BUFFER_SIZE) == 0) {
retVal = 1;
} else {
retVal = 0;
}
}
if(only_tests_with_string) {
if(strstr(testReference->name, testcase_name_substring) != NULL) {
retVal = 1;
} else {
retVal = 0;
}
}
return retVal;
}
May 30, 2011
May 30, 2011
464
465
466
467
/*!
* Loads the test case references from the given test suite.
* \param library Previously loaded dynamic library AKA test suite
May 31, 2011
May 31, 2011
468
* \return Pointer to array of TestCaseReferences or NULL if function failed
May 30, 2011
May 30, 2011
469
*/
May 30, 2011
May 30, 2011
470
TestCaseReference **
Jun 9, 2011
Jun 9, 2011
471
QueryTestCaseReferences(void *library)
May 30, 2011
May 30, 2011
472
{
Jul 11, 2011
Jul 11, 2011
473
TestCaseReference **(*suite)(void);
May 26, 2011
May 26, 2011
474
Jul 11, 2011
Jul 11, 2011
475
476
477
478
479
suite = (TestCaseReference **(*)(void)) SDL_LoadFunction(library, "QueryTestSuite");
if(suite == NULL) {
fprintf(stderr, "Loading QueryTestCaseReferences() failed.\n");
fprintf(stderr, "%s\n", SDL_GetError());
}
May 26, 2011
May 26, 2011
480
Jul 11, 2011
Jul 11, 2011
481
482
483
484
485
TestCaseReference **tests = suite();
if(tests == NULL) {
fprintf(stderr, "Failed to load test references.\n");
fprintf(stderr, "%s\n", SDL_GetError());
}
May 26, 2011
May 26, 2011
486
Jul 11, 2011
Jul 11, 2011
487
return tests;
May 26, 2011
May 26, 2011
488
489
}
Jun 4, 2011
Jun 4, 2011
490
May 30, 2011
May 30, 2011
491
492
493
/*!
* Loads test case from a test suite
*
May 30, 2011
May 30, 2011
494
495
* \param suite a test suite
* \param testName Name of the test that is going to be loaded
May 30, 2011
May 30, 2011
496
*
May 31, 2011
May 31, 2011
497
* \return Function Pointer (TestCase) to loaded test case, NULL if function failed
May 30, 2011
May 30, 2011
498
*/
Jun 9, 2011
Jun 9, 2011
499
500
TestCaseFp
LoadTestCaseFunction(void *suite, char *testName)
May 30, 2011
May 30, 2011
501
{
Jun 9, 2011
Jun 9, 2011
502
TestCaseFp test = (TestCaseFp) SDL_LoadFunction(suite, testName);
May 30, 2011
May 30, 2011
503
if(test == NULL) {
May 31, 2011
May 31, 2011
504
505
fprintf(stderr, "Loading test failed, tests == NULL\n");
fprintf(stderr, "%s\n", SDL_GetError());
May 30, 2011
May 30, 2011
506
507
508
509
510
}
return test;
}
Jun 5, 2011
Jun 5, 2011
511
Jun 4, 2011
Jun 4, 2011
512
/*!
Jul 11, 2011
Jul 11, 2011
513
514
515
516
517
518
519
520
521
* Loads function that sets up a fixture for a test case. Note: if there's
* no SetUp function present in the suite the function will return NULL.
*
* \param suite Used test suite
*
* \return Function pointer to test case's set up function
*/
TestCaseSetUpFp
LoadTestSetUpFunction(void *suite) {
Jul 17, 2011
Jul 17, 2011
522
return (TestCaseSetUpFp) SDL_LoadFunction(suite, "SetUp");
Jul 11, 2011
Jul 11, 2011
523
524
525
526
527
528
529
530
531
532
533
534
535
}
/*!
* Loads function that tears down a fixture for a test case. Note: if there's
* no TearDown function present in the suite the function will return NULL.
*
* \param suite Used test suite
*
* \return Function pointer to test case's tear down function
*/
TestCaseTearDownFp
LoadTestTearDownFunction(void *suite) {
Jul 17, 2011
Jul 17, 2011
536
return (TestCaseTearDownFp) SDL_LoadFunction(suite, "TearDown");
Jul 11, 2011
Jul 11, 2011
537
538
539
540
541
542
}
/*!
* Loads function that initialises the test environment for
* a test case in the given suite.
Jun 4, 2011
Jun 4, 2011
543
544
545
*
* \param suite Used test suite
*
Jul 11, 2011
Jul 11, 2011
546
* \return Function pointer (InitTestInvironmentFp) which points to loaded init function. NULL if function fails.
Jun 4, 2011
Jun 4, 2011
547
*/
Jul 11, 2011
Jul 11, 2011
548
549
550
551
552
InitTestInvironmentFp
LoadInitTestInvironmentFunction(void *suite) {
InitTestInvironmentFp testEnvInit = (InitTestInvironmentFp) SDL_LoadFunction(suite, "_InitTestEnvironment");
if(testEnvInit == NULL) {
fprintf(stderr, "Loading _InitTestInvironment function failed, testEnvInit == NULL\n");
Jun 4, 2011
Jun 4, 2011
553
554
555
fprintf(stderr, "%s\n", SDL_GetError());
}
Jul 11, 2011
Jul 11, 2011
556
return testEnvInit;
Jun 4, 2011
Jun 4, 2011
557
558
}
Jun 5, 2011
Jun 5, 2011
559
Jun 4, 2011
Jun 4, 2011
560
/*!
Jul 11, 2011
Jul 11, 2011
561
562
* Loads function that deinitialises the test environment (and returns
* the test case's result) created for the test case in the given suite.
Jun 4, 2011
Jun 4, 2011
563
564
565
*
* \param suite Used test suite
*
Jul 11, 2011
Jul 11, 2011
566
* \return Function pointer (QuitTestInvironmentFp) which points to loaded init function. NULL if function fails.
Jun 4, 2011
Jun 4, 2011
567
*/
Jul 11, 2011
Jul 11, 2011
568
569
570
571
572
QuitTestInvironmentFp
LoadQuitTestInvironmentFunction(void *suite) {
QuitTestInvironmentFp testEnvQuit = (QuitTestInvironmentFp) SDL_LoadFunction(suite, "_QuitTestEnvironment");
if(testEnvQuit == NULL) {
fprintf(stderr, "Loading _QuitTestEnvironment function failed, testEnvQuit == NULL\n");
Jun 4, 2011
Jun 4, 2011
573
574
575
fprintf(stderr, "%s\n", SDL_GetError());
}
Jul 11, 2011
Jul 11, 2011
576
return testEnvQuit;
Jun 4, 2011
Jun 4, 2011
577
}
May 30, 2011
May 30, 2011
578
Jul 18, 2011
Jul 18, 2011
579
Jul 11, 2011
Jul 11, 2011
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*!
* Loads function that returns failed assert count in the current
* test environment
*
* \param suite Used test suite
*
* \return Function pointer to _CountFailedAsserts function
*/
CountFailedAssertsFp
LoadCountFailedAssertsFunction(void *suite) {
CountFailedAssertsFp countFailedAssert = (CountFailedAssertsFp) SDL_LoadFunction(suite, "_CountFailedAsserts");
if(countFailedAssert == NULL) {
fprintf(stderr, "Loading _CountFailedAsserts function failed, countFailedAssert == NULL\n");
fprintf(stderr, "%s\n", SDL_GetError());
}
return countFailedAssert;
}
Jul 18, 2011
Jul 18, 2011
600
601
602
603
604
605
/*!
* Set timeout for test.
*
* \param timeout Timeout interval in seconds!
* \param callback Function that will be called after timeout has elapsed
*/
Jul 20, 2011
Jul 20, 2011
606
607
void
SetTestTimeout(int timeout, void (*callback)(int))
Jul 18, 2011
Jul 18, 2011
608
{
Jul 18, 2011
Jul 18, 2011
609
610
611
if(callback == NULL) {
fprintf(stderr, "Error: timeout callback can't be NULL");
}
Jul 20, 2011
Jul 20, 2011
612
Jul 18, 2011
Jul 18, 2011
613
614
615
616
if(timeout < 0) {
fprintf(stderr, "Error: timeout value must be bigger than zero.");
}
Jul 20, 2011
Jul 20, 2011
617
618
619
620
621
622
623
624
625
626
627
int tm = (timeout > universal_timeout ? timeout : universal_timeout);
#if 1
/* Init SDL timer if not initialized before */
if(SDL_WasInit(SDL_INIT_TIMER) == 0) {
if(SDL_InitSubSystem(SDL_INIT_TIMER)) {
fprintf(stderr, "Error: Failed to init timer subsystem");
fprintf(stderr, "%s\n", SDL_GetError());
}
}
Jul 18, 2011
Jul 18, 2011
628
629
630
/* Note:
* SDL_Init(SDL_INIT_TIMER) should be successfully called before using this
*/
Jul 20, 2011
Jul 20, 2011
631
632
int timeoutInMilliseconds = tm * 1000;
Jul 18, 2011
Jul 18, 2011
633
634
635
SDL_TimerID timerID = SDL_AddTimer(timeoutInMilliseconds, callback, 0x0);
if(timerID == NULL) {
fprintf(stderr, "Error: Creation of SDL timer failed.\n");
Jul 20, 2011
Jul 20, 2011
636
fprintf(stderr, "Error: %s\n", SDL_GetError());
Jul 18, 2011
Jul 18, 2011
637
638
639
}
#else
signal(SIGALRM, callback);
Jul 18, 2011
Jul 18, 2011
640
alarm((unsigned int) tm);
Jul 18, 2011
Jul 18, 2011
641
642
643
644
#endif
}
Jul 17, 2011
Jul 17, 2011
645
646
647
648
649
650
651
652
653
654
655
656
/*!
* Kills test that hungs. Test hungs when its execution
* takes longer than timeout specified for it.
*
* When test will be killed SIG_ALRM will be triggered and
* it'll call this function which kills the test process.
*
* Note: if runner is executed with --in-proc then hung tests
* can't be killed
*
* \param signum
*/
Jul 18, 2011
Jul 18, 2011
657
658
void
KillHungTestInChildProcess(int signum)
Jul 18, 2011
Jul 18, 2011
659
{
Jul 17, 2011
Jul 17, 2011
660
661
662
exit(TEST_RESULT_KILLED);
}
Jul 19, 2011
Jul 19, 2011
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*!
* Checks if given test case can be executed on the current platform.
*
* \param testCase Test to be checked
* \returns 1 if test is runnable, otherwise 0. On error returns -1
*/
int
CheckTestRequirements(TestCase *testCase)
{
int retVal = 1;
if(testCase == NULL) {
fprintf(stderr, "TestCase parameter can't be NULL");
return -1;
}
if(testCase->requirements & TEST_REQUIRES_AUDIO) {
retVal = PlatformSupportsAudio();
}
return retVal;
}
Jul 17, 2011
Jul 17, 2011
686
Jul 11, 2011
Jul 11, 2011
687
/*
Jul 14, 2011
Jul 14, 2011
688
689
* Execute a test. Loads the test, executes it and
* returns the tests return value to the caller.
Jul 11, 2011
Jul 11, 2011
690
691
*
* \param testItem Test to be executed
Jul 14, 2011
Jul 14, 2011
692
* \param test result
Jul 11, 2011
Jul 11, 2011
693
694
*/
int
Jul 25, 2011
Jul 25, 2011
695
RunTest(TestCase *testCase, char *execKey)
Jul 18, 2011
Jul 18, 2011
696
{
Jul 24, 2011
Jul 24, 2011
697
698
699
700
if(!testCase) {
return -1;
}
Jul 19, 2011
Jul 19, 2011
701
702
703
704
705
706
int runnable = CheckTestRequirements(testCase);
if(runnable != 1) {
return TEST_RESULT_SKIPPED;
}
if(testCase->timeout > 0 || universal_timeout > 0) {
Jul 18, 2011
Jul 18, 2011
707
if(execute_inproc) {
Jul 18, 2011
Jul 18, 2011
708
Log(time(0), "Test asked for timeout which is not supported.");
Jul 18, 2011
Jul 18, 2011
709
710
}
else {
Jul 19, 2011
Jul 19, 2011
711
SetTestTimeout(testCase->timeout, KillHungTestInChildProcess);
Jul 18, 2011
Jul 18, 2011
712
713
714
}
}
Jul 19, 2011
Jul 19, 2011
715
testCase->initTestEnvironment();
Jul 11, 2011
Jul 11, 2011
716
Jul 19, 2011
Jul 19, 2011
717
718
if(testCase->testSetUp) {
testCase->testSetUp(0x0);
Jul 11, 2011
Jul 11, 2011
719
720
}
Jul 19, 2011
Jul 19, 2011
721
int cntFailedAsserts = testCase->countFailedAsserts();
Jul 11, 2011
Jul 11, 2011
722
if(cntFailedAsserts != 0) {
Jul 13, 2011
Jul 13, 2011
723
return TEST_RESULT_SETUP_FAILURE;
Jul 11, 2011
Jul 11, 2011
724
725
}
Jul 19, 2011
Jul 19, 2011
726
testCase->testCase(0x0);
Jul 11, 2011
Jul 11, 2011
727
Jul 19, 2011
Jul 19, 2011
728
729
if(testCase->testTearDown) {
testCase->testTearDown(0x0);
Jul 11, 2011
Jul 11, 2011
730
731
}
Jul 19, 2011
Jul 19, 2011
732
return testCase->quitTestEnvironment();
Jul 11, 2011
Jul 11, 2011
733
734
}
Jul 13, 2011
Jul 13, 2011
735
Jul 11, 2011
Jul 11, 2011
736
/*!
Jul 14, 2011
Jul 14, 2011
737
738
* Sets up a test case. Decideds wheter the test will
* be executed in-proc or out-of-proc.
Jul 11, 2011
Jul 11, 2011
739
740
741
742
743
*
* \param testItem The test case that will be executed
* \return The return value of the test. Zero means success, non-zero failure.
*/
int
Jul 25, 2011
Jul 25, 2011
744
ExecuteTest(TestCase *testItem, char *execKey) {
Jul 13, 2011
Jul 13, 2011
745
int retVal = -1;
Jul 11, 2011
Jul 11, 2011
746
747
if(execute_inproc) {
Jul 24, 2011
Jul 24, 2011
748
retVal = RunTest(testItem, execKey);
Jul 11, 2011
Jul 11, 2011
749
750
751
} else {
int childpid = fork();
if(childpid == 0) {
Jul 24, 2011
Jul 24, 2011
752
exit(RunTest(testItem, execKey));
Jul 11, 2011
Jul 11, 2011
753
754
755
756
757
758
759
760
} else {
int stat_lock = -1;
int child = wait(&stat_lock);
retVal = HandleChildProcessReturnValue(stat_lock);
}
}
Jul 24, 2011
Jul 24, 2011
761
762
763
764
765
766
767
768
769
770
771
772
773
774
if(retVal == TEST_RESULT_SKIPPED) {
testSkipCount++;
totalTestSkipCount++;
}
else if(retVal) {
totalTestFailureCount++;
testFailureCount++;
}
else {
totalTestPassCount++;
testPassCount++;
}
// return the value for logger
Jul 11, 2011
Jul 11, 2011
775
776
777
return retVal;
}
Jun 5, 2011
Jun 5, 2011
778
May 30, 2011
May 30, 2011
779
/*!
May 30, 2011
May 30, 2011
780
781
782
783
* If using out-of-proc execution of tests. This function
* will handle the return value of the child process
* and interprets it to the runner. Also prints warnings
* if child was aborted by a signela.
May 30, 2011
May 30, 2011
784
*
May 30, 2011
May 30, 2011
785
* \param stat_lock information about the exited child process
May 30, 2011
May 30, 2011
786
*
May 30, 2011
May 30, 2011
787
* \return 0 if test case succeeded, 1 otherwise
May 30, 2011
May 30, 2011
788
*/
May 30, 2011
May 30, 2011
789
int
Jun 9, 2011
Jun 9, 2011
790
HandleChildProcessReturnValue(int stat_lock)
May 30, 2011
May 30, 2011
791
{
May 30, 2011
May 30, 2011
792
int returnValue = -1;
May 26, 2011
May 26, 2011
793
May 30, 2011
May 30, 2011
794
795
if(WIFEXITED(stat_lock)) {
returnValue = WEXITSTATUS(stat_lock);
May 26, 2011
May 26, 2011
796
797
} else if(WIFSIGNALED(stat_lock)) {
int signal = WTERMSIG(stat_lock);
Jul 10, 2011
Jul 10, 2011
798
// \todo add this to logger (add signal number)
Jul 18, 2011
Jul 18, 2011
799
Log(time(0), "FAILURE: test was aborted due to %d\n", signal);
May 30, 2011
May 30, 2011
800
returnValue = 1;
May 26, 2011
May 26, 2011
801
802
}
May 30, 2011
May 30, 2011
803
return returnValue;
May 26, 2011
May 26, 2011
804
805
}
Jun 5, 2011
Jun 5, 2011
806
Jun 4, 2011
Jun 4, 2011
807
/*!
Jul 11, 2011
Jul 11, 2011
808
* Sets up the logger.
Jun 4, 2011
Jun 4, 2011
809
*
Jul 27, 2011
Jul 27, 2011
810
* \return Logger data structure (that needs be deallocated)
Jun 4, 2011
Jun 4, 2011
811
*/
Jul 11, 2011
Jul 11, 2011
812
813
814
void *
SetUpLogger()
{
Jul 27, 2011
Jul 27, 2011
815
816
817
818
819
820
821
822
LoggerData *loggerData = SDL_malloc(sizeof(loggerData));
if(loggerData == NULL) {
fprintf(stderr, "Error: Logger data structure not allocated.");
return NULL;
}
loggerData->level = (enable_verbose_logger ? VERBOSE : STANDARD);
Jul 11, 2011
Jul 11, 2011
823
824
825
if(xml_enabled) {
RunStarted = XMLRunStarted;
RunEnded = XMLRunEnded;
Jul 11, 2011
Jul 11, 2011
826
Jul 11, 2011
Jul 11, 2011
827
828
SuiteStarted = XMLSuiteStarted;
SuiteEnded = XMLSuiteEnded;
Jun 4, 2011
Jun 4, 2011
829
Jul 11, 2011
Jul 11, 2011
830
831
TestStarted = XMLTestStarted;
TestEnded = XMLTestEnded;
Jun 4, 2011
Jun 4, 2011
832
Jul 11, 2011
Jul 11, 2011
833
834
835
836
837
838
839
840
841
Assert = XMLAssert;
AssertWithValues = XMLAssertWithValues;
AssertSummary = XMLAssertSummary;
Log = XMLLog;
char *sheet = NULL;
if(xsl_enabled) {
sheet = "style.xsl"; // default style sheet;
Jul 11, 2011
Jul 11, 2011
842
843
}
Jul 11, 2011
Jul 11, 2011
844
845
846
if(custom_xsl_enabled) {
sheet = xsl_stylesheet_name;
}
Jul 11, 2011
Jul 11, 2011
847
Jul 27, 2011
Jul 27, 2011
848
loggerData->custom = sheet;
Jul 11, 2011
Jul 11, 2011
849
850
851
} else {
RunStarted = PlainRunStarted;
RunEnded = PlainRunEnded;
Jun 4, 2011
Jun 4, 2011
852
Jul 11, 2011
Jul 11, 2011
853
854
SuiteStarted = PlainSuiteStarted;
SuiteEnded = PlainSuiteEnded;
Jun 4, 2011
Jun 4, 2011
855
Jul 11, 2011
Jul 11, 2011
856
857
TestStarted = PlainTestStarted;
TestEnded = PlainTestEnded;
Jul 11, 2011
Jul 11, 2011
858
Jul 11, 2011
Jul 11, 2011
859
860
861
Assert = PlainAssert;
AssertWithValues = PlainAssertWithValues;
AssertSummary = PlainAssertSummary;
Jun 4, 2011
Jun 4, 2011
862
Jul 11, 2011
Jul 11, 2011
863
Log = PlainLog;
Jun 4, 2011
Jun 4, 2011
864
865
}
Jul 11, 2011
Jul 11, 2011
866
return loggerData;
Jun 4, 2011
Jun 4, 2011
867
868
869
}
Jun 4, 2011
Jun 4, 2011
870
871
872
/*!
* Prints usage information
*/
Jun 6, 2011
Jun 6, 2011
873
void
Jul 4, 2011
Jul 4, 2011
874
PrintUsage() {
Jul 27, 2011
Jul 27, 2011
875
876
877
878
879
printf("Usage: ./runner [--in-proc] [--show-tests] [--verbose] [--xml]\n");
printf(" [--xsl [STYLESHEET]] [--seed VALUE] [--iterations VALUE]\n");
printf(" [--exec-key KEY] [--timeout VALUE] [--test TEST]\n");
printf(" [--name-contains SUBSTR] [--suite SUITE]\n");
printf(" [--version] [--help]\n");
Jun 4, 2011
Jun 4, 2011
880
printf("Options:\n");
Jun 9, 2011
Jun 9, 2011
881
printf(" --in-proc Executes tests in-process\n");
Jun 13, 2011
Jun 13, 2011
882
printf(" --show-tests Prints out all the executable tests\n");
Jul 27, 2011
Jul 27, 2011
883
printf(" -v --verbose Enables verbose logging\n");
Jul 1, 2011
Jul 1, 2011
884
printf(" --xml Enables XML logger\n");
Jul 3, 2011
Jul 3, 2011
885
886
887
printf(" --xsl [STYLESHEET] Adds XSL stylesheet to the XML test reports for\n");
printf(" browser viewing. Optionally uses the specified XSL\n");
printf(" file or URL instead of the default one\n");
Jul 24, 2011
Jul 24, 2011
888
889
890
printf(" --seed VALUE Specify fuzzing seed for the harness\n");
printf(" --iterations VALUE Specify how many times a test will be executed\n");
printf(" --exec-key KEY Run test(s) with specific execution key\n");
Jul 18, 2011
Jul 18, 2011
891
892
893
894
895
printf(" -tm --timeout VALUE Specify common timeout value for all tests\n");
printf(" Timeout is given in seconds and it'll override\n");
printf(" test specific timeout value only if the given\n");
printf(" value is greater than the test specific value\n");
printf(" note: doesn't work with --in-proc option.\n");
Jun 9, 2011
Jun 9, 2011
896
897
898
899
printf(" -t --test TEST Executes only tests with given name\n");
printf(" -ts --name-contains SUBSTR Executes only tests that have given\n");
printf(" substring in test name\n");
printf(" -s --suite SUITE Executes only the given test suite\n");
Jun 5, 2011
Jun 5, 2011
900
Jul 27, 2011
Jul 27, 2011
901
printf(" --version Print version information\n");
Jun 9, 2011
Jun 9, 2011
902
printf(" -h --help Print this help\n");
Jun 4, 2011
Jun 4, 2011
903
904
}
Jun 5, 2011
Jun 5, 2011
905
May 30, 2011
May 30, 2011
906
907
/*!
* Parse command line arguments
Jun 1, 2011
Jun 1, 2011
908
909
910
*
* \param argc Count of command line arguments
* \param argv Array of commond lines arguments
May 30, 2011
May 30, 2011
911
912
913
914
915
916
917
918
*/
void
ParseOptions(int argc, char *argv[])
{
int i;
for (i = 1; i < argc; ++i) {
const char *arg = argv[i];
Jun 1, 2011
Jun 1, 2011
919
if(SDL_strcmp(arg, "--in-proc") == 0) {
May 30, 2011
May 30, 2011
920
921
execute_inproc = 1;
}
Jun 13, 2011
Jun 13, 2011
922
923
else if(SDL_strcmp(arg, "--show-tests") == 0) {
only_print_tests = 1;
Jun 5, 2011
Jun 5, 2011
924
}
Jun 26, 2011
Jun 26, 2011
925
926
927
else if(SDL_strcmp(arg, "--xml") == 0) {
xml_enabled = 1;
}
Jul 27, 2011
Jul 27, 2011
928
929
930
else if(SDL_strcmp(arg, "--verbose") == 0 || SDL_strcmp(arg, "-v") == 0) {
enable_verbose_logger = 1;
}
Jul 18, 2011
Jul 18, 2011
931
932
933
934
935
936
937
938
939
940
941
942
943
944
else if(SDL_strcmp(arg, "--timeout") == 0 || SDL_strcmp(arg, "-tm") == 0) {
universal_timeout_enabled = 1;
char *timeoutString = NULL;
if( (i + 1) < argc) {
timeoutString = argv[++i];
} else {
printf("runner: timeout is missing\n");
PrintUsage();
exit(1);
}
universal_timeout = atoi(timeoutString);
}
Jul 24, 2011
Jul 24, 2011
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
else if(SDL_strcmp(arg, "--seed") == 0) {
if( (i + 1) < argc) {
runSeed = argv[++i];
} else {
printf("runner: seed value is missing\n");
PrintUsage();
exit(1);
}
//!Ê\todo should the seed be copied to a buffer?
}
else if(SDL_strcmp(arg, "--iterations") == 0) {
char *iterationsString = NULL;
if( (i + 1) < argc) {
iterationsString = argv[++i];
} else {
printf("runner: iterations value is missing\n");
PrintUsage();
exit(1);
}
testInvocationCount = atoi(iterationsString);
Jul 25, 2011
Jul 25, 2011
966
967
968
969
if(testInvocationCount < 1) {
printf("Iteration value has to bigger than 0.\n");
exit(1);
}
Jul 24, 2011
Jul 24, 2011
970
971
972
973
974
975
976
977
978
979
980
}
else if(SDL_strcmp(arg, "--exec-key") == 0) {
char *execKeyString = NULL;
if( (i + 1) < argc) {
execKeyString = argv[++i];
} else {
printf("runner: execkey value is missing\n");
PrintUsage();
exit(1);
}
Jul 25, 2011
Jul 25, 2011
981
userExecKey = execKeyString;
Jul 24, 2011
Jul 24, 2011
982
}
Jun 5, 2011
Jun 5, 2011
983
984
else if(SDL_strcmp(arg, "--test") == 0 || SDL_strcmp(arg, "-t") == 0) {
only_selected_test = 1;
Jun 5, 2011
Jun 5, 2011
985
986
987
988
989
990
char *testName = NULL;
if( (i + 1) < argc) {
testName = argv[++i];
} else {
printf("runner: test name is missing\n");
Jul 4, 2011
Jul 4, 2011
991
PrintUsage();
Jun 5, 2011
Jun 5, 2011
992
993
exit(1);
}
Jun 5, 2011
Jun 5, 2011
994
Jun 5, 2011
Jun 5, 2011
995
memset(selected_test_name, 0, NAME_BUFFER_SIZE);
Jun 5, 2011
Jun 5, 2011
996
997
strcpy(selected_test_name, testName);
}
Jun 30, 2011
Jun 30, 2011
998
else if(SDL_strcmp(arg, "--xsl") == 0) {
Jul 1, 2011
Jul 1, 2011
999
xsl_enabled = 1;
Jun 30, 2011
Jun 30, 2011
1000