-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestrunner.c
276 lines (223 loc) · 6.76 KB
/
testrunner.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*************** YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE ***************/
/*
A simple testrunner framework
Original Author: L. Angrave
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include "testrunner.h"
/* Constants */
#define false (0)
#define true (1)
#define test_killed (2)
/* defaults */
static int default_timeout_seconds = 15;
static int timeout_seconds;
void set_testrunner_default_timeout(int s)
{
assert(s > 0);
default_timeout_seconds = s;
}
void set_testrunner_timeout(int s)
{
assert(s > 0);
timeout_seconds = s;
}
/* --- Helper macros and functions --- */
#define DIE(mesg) {fprintf(stderr,"\n%s(%d):%s\n",__fname__,__LINE__,mesg); exit(1);}
static int eql(const char *s1, const char *s2)
{
return s1 && s2 && !strcmp(s1, s2);
}
/* Callback function for qsort on strings */
static int mystrcmp(const void *p1, const void *p2)
{
return eql((const char *) p1, (const char *) p2);
}
/* Stats of all tests run so far */
typedef struct {
int ran, passed, failed;
} stats_t;
/* -- Signal handlers -- */
static pid_t child_pid;
static int sent_child_timeout_kill_signal;
static void kill_child_signal_handler(intsigno)
{
if (!child_pid)
return;
char m[] = "-Timeout(Killing test process)-";
write(0, m, sizeof(m) - 1);
kill(child_pid, SIGKILL);
sent_child_timeout_kill_signal = 1;
}
/* Internal function to run a test as a forked child. The child process is terminated if it runs for more than a few seconds */
static int invoke_test_with_timelimit(testentry_t * test,
int redirect_stdouterr, int argc,
const char **argv)
{
char fname[255];
int wait_status;
pid_t wait_val;
struct sigaction action;
assert(!child_pid);
assert(test && test->test_function && test->name);
set_testrunner_timeout(default_timeout_seconds);
errno = 0;
child_pid = fork();
if (child_pid == -1) {
fprintf(stderr, "-fork failed so running test inline-");
return test->test_function(argc, argv);
}
if (child_pid == 0) {
if (redirect_stdouterr) {
snprintf(fname, (int) sizeof(fname), "stdout-%s.txt",
test->name);
fname[sizeof(fname) - 1] = 0;
freopen(fname, "w", stdout);
memcpy(fname + 3, "err", 3);
freopen(fname, "w", stderr);
}
exit(test->test_function(argc, argv));
} else {
wait_status = -1;
sigemptyset(&action.sa_mask);
action.sa_handler = kill_child_signal_handler;
sigaction(SIGALRM, &action, NULL);
sent_child_timeout_kill_signal = 0;
alarm(timeout_seconds);
wait_val = waitpid(child_pid, &wait_status, 0);
int child_exited_normally = WIFEXITED(wait_status);
int child_exit_value = WEXITSTATUS(wait_status);
int child_term_by_signal = WIFSIGNALED(wait_status);
int child_term_signal = WTERMSIG(wait_status);
if (child_term_by_signal) {
fprintf(stderr, "testrunner:Test terminated by signal %d\n",
child_term_signal);
fprintf(stderr,
"testrunner:waitpid returned %d (child_pid=%d,wait_status=%d)",
wait_val, child_pid, wait_status);
}
if (child_pid != wait_val)
fprintf(stderr,
"testrunner: strange... wait_val != child_pid\n");
int passed = (child_pid == wait_val) && (child_exit_value == 0)
&& (child_exited_normally != 0);
alarm(0);
kill(child_pid, SIGKILL);
child_pid = 0;
return sent_child_timeout_kill_signal ? test_killed : passed ? 0 :
1;
}
}
/*
* run a test and update the stats. The main guts of this functionality is provided by invoke_test_with_timelimit
* This outer wrapper updates thes output and statistics before and after running the test.
*/
static int
run_one_test(stats_t * stats, testentry_t * test, int redirect_stdouterr,
int argc, const char **argv)
{
int test_result;
assert(stats && test->name && argc > 0 && argv && *argv);
stats->ran++;
stats->failed++;
printf("%2d.%-20s:", stats->ran, test->name);
fflush(stdout);
test_result =
invoke_test_with_timelimit(test, redirect_stdouterr, argc, argv);
if (test_result == 0) {
stats->failed--;
stats->passed++;
}
printf(":%s\n", (test_result == 0 ? "pass" : test_result ==
2 ? "TIMEOUT * " : "FAIL *"));
return test_result != 0;
}
/* Help functionality to print out sorted list of test names and suite names */
static void print_targets(testentry_t tests[], int count)
{
const char **array;
const char *previous;
int i;
array = (const char **) calloc(sizeof(const char *), count);
/* Sort the test names and print unique entries */
for (i = 0; i < count; i++)
array[i] = tests[i].name;
qsort(array, count, sizeof(array[0]), mystrcmp);
printf("\nValid tests : all");
for (i = 0, previous = ""; i < count; i++)
if (!eql(previous, array[i]))
printf(" %s", (previous = array[i]));
/* Sort the suite names and print unique entries */
for (i = 0; i < count; i++)
array[i] = tests[i].suite;
qsort(array, count, sizeof(array[0]), mystrcmp);
printf("\nValid suites:");
for (i = 0, previous = ""; i < count; i++)
if (!eql(previous, array[i]))
printf(" %s", (previous = array[i]));
printf("\n");
}
/*
* Main entry point for test harness
*/
int
run_testrunner(int argc, const char **argv, testentry_t tests[], int test_count)
{
const char *test_name, *target;
int i;
stats_t stats;
int target_matched, max_errors_before_quit, redirect_stdouterr;
memset(&stats, 0, sizeof(stats));
max_errors_before_quit = 1;
redirect_stdouterr = 0;
assert(tests != NULL);
assert(test_count > 0);
assert(argc > 0 && argv && *argv);
while (true) {
target = argc > 1 ? argv[1] : "";
assert(target);
if (*target != '-')
break;
argc--;
argv++;
if (target[1] == 'f' && target[2])
max_errors_before_quit = atoi(target + 1);
else if (target[1] == 'r')
redirect_stdouterr = 1;
}
target_matched = false;
for (i = 0;
i < test_count && (max_errors_before_quit < 1
|| stats.failed != max_errors_before_quit);
i++) {
test_name = tests[i].name;
assert(test_name);
assert(tests[i].suite);
assert(tests[i].test_function);
if (eql(target, test_name) || eql(target, "all")
|| eql(target, tests[i].suite)) {
if (!target_matched)
printf("Running tests...\n");
target_matched = true;
run_one_test(&stats, &tests[i], redirect_stdouterr, argc - 1,
argv + 1);
}
}
if (!target_matched) {
fprintf(stderr, "Test '%s' not found",
(strlen(target) > 0 ? target : "(empty)"));
print_targets(tests, test_count);
} else {
printf("\nTest Results:%d tests,%d passed,%d failed.\n", stats.ran,
stats.passed, stats.failed);
}
return stats.passed == stats.ran && target_matched ? 0 : 1;
}