This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autotest.c
249 lines (209 loc) · 5.34 KB
/
autotest.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
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#ifndef _POSIX_C_SOURCE
# define _POSIX_C_SOURCE 200809L
#endif
#include <errno.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define AUTOTEST_BUILDING
#include "./autotest.h"
static jmp_buf *jmpbuf_harness;
static sigjmp_buf *jmpbuf_test;
static FILE *real_stdout;
static int real_stdout_fd;
static struct sigaction sa;
__attribute__((noreturn)) void bailout(const char *fmt, ...) {
va_list ap;
fputs("Bail out! ", real_stdout);
va_start(ap, fmt);
vfprintf(real_stdout, fmt, ap);
va_end(ap);
fputs("\n", real_stdout);
longjmp(*jmpbuf_harness, 1);
__builtin_unreachable();
}
int populate_test_case(test_case *tcase, const char *name, test_case_fn fn) {
if (*name == '\0') return 1;
tcase->skip = 0;
tcase->allow_segv = 0;
tcase->allow_abrt = 0;
tcase->must_fail = 0;
tcase->name = NULL;
tcase->next = NULL;
if (*name == '_') {
++name;
tcase->skip = 1;
}
if (strncmp(name, "TEST_", 5) == 0) {
name += 5;
} else {
return 1;
}
while (1) {
if (strncmp(name, "SEGV_", 5) == 0) {
tcase->allow_segv = 1;
name += 5;
continue;
}
if (strncmp(name, "ABRT_", 5) == 0) {
tcase->allow_abrt = 1;
name += 5;
continue;
}
if (strncmp(name, "FAIL_", 5) == 0) {
tcase->must_fail = 1;
name += 5;
continue;
}
break;
}
if (*name == '\0') {
/* don't allow zero-length names */
return 1;
}
tcase->name = name;
tcase->fn = fn;
return 0;
}
__attribute__((noreturn)) static void handle_sig(int sig) {
if (sig == SIGSEGV) {
siglongjmp(*jmpbuf_test, 2);
} else if (sig == SIGABRT) {
siglongjmp(*jmpbuf_test, 1);
} else {
bailout("Autotest signal handler encountered unknown signal: %s (%d)", strsignal(sig), sig);
}
__builtin_unreachable();
}
static void install_sighandlers(void) {
sa.sa_handler = handle_sig;
sigemptyset(&(sa.sa_mask));
if (sigaction(SIGABRT, &sa, NULL) == -1) bailout("Could not install SIGABRT action: %s", strerror(errno));
if (sigaction(SIGSEGV, &sa, NULL) == -1) bailout("Could not install SIGSEGV action: %s", strerror(errno));
}
int main(int argc, const char **argv) {
test_case *test_cases;
volatile int had_failure = 0;
jmp_buf _jmpbuf_harness;
(void) argc;
real_stdout_fd = dup(1);
if (real_stdout_fd == -1) {
printf("Bail out! Could not duplicate standard output file descriptor: %s\n", strerror(errno));
return 1;
}
if (dup2(2, 1) == -1) {
printf("Bail out! Could not redirect standard output to standard error: %s\n", strerror(errno));
return 1;
}
real_stdout = fdopen(real_stdout_fd, "wb");
if (real_stdout == 0) {
const char *msg = "Bail out! Could not open new standard out file descriptor as a file: ";
write(real_stdout_fd, "Bail out! Could not open new standard out file descriptor as a file: ", strlen(msg));
write(real_stdout_fd, strerror(errno), strlen(strerror(errno)));
write(real_stdout_fd, "\n", 1);
return 1;
}
jmpbuf_harness = &_jmpbuf_harness;
if (setjmp(_jmpbuf_harness) == 1) {
/* bailout() was called */
return 1;
}
discover_tests(argv[0], &test_cases);
fputs("TAP version 13\n", real_stdout);
if (test_cases) {
volatile size_t num_cases = 0;
const test_case *tcase;
const test_case *tcase_tofree;
for (tcase = test_cases; tcase;) {
volatile int failed = 0;
const char * volatile message = NULL;
++num_cases;
tcase_tofree = NULL;
install_sighandlers();
if (tcase->skip) {
fprintf(
real_stdout,
"ok %lu %s # SKIP Symbol prefixed with underscore (_TEST_%s)\n",
num_cases, tcase->name, tcase->name);
} else {
sigjmp_buf _jmpbuf_test;
jmpbuf_test = &_jmpbuf_test;
switch (sigsetjmp(_jmpbuf_test, 1)) {
case 0:
tcase->fn();
fflush(stderr);
fflush(stdout);
failed = 0;
message = NULL;
break;
case 1: /* SIGABRT */
fflush(stderr);
fflush(stdout);
failed = !tcase->allow_abrt;
message = "Test case aborted";
break;
case 2: /* SIGSEGV */
fflush(stderr);
fflush(stdout);
failed = !tcase->allow_segv;
message = "Test case encountered a segfault";
break;
}
fflush(stderr);
if (tcase->must_fail == failed) {
failed = 0;
fprintf(
real_stdout,
"ok %lu %s\n",
num_cases, tcase->name);
if (message != NULL) {
fprintf(
real_stdout,
" ---\n message: %s\n severity: comment\n ...\n",
message);
}
} else if (tcase->must_fail && !failed) {
failed = 1;
fprintf(
real_stdout,
"not ok %lu %s\n ---\n message: Test case was expected to fail\n severity: fail\n ...\n",
num_cases, tcase->name);
if (message != NULL) {
fprintf(
real_stdout,
" ---\n message: %s\n severity: comment\n ...\n",
message);
}
} else {
failed = 1;
fprintf(
real_stdout,
"not ok %lu %s\n",
num_cases, tcase->name);
if (message != NULL) {
fprintf(
real_stdout,
" ---\n message: %s\n severity: fail\n ...\n",
message);
}
}
had_failure = had_failure || failed;
}
tcase_tofree = tcase;
tcase = tcase->next;
free((void *) tcase_tofree);
fflush(real_stdout);
}
fprintf(real_stdout, "1..%lu\n", num_cases);
} else {
fputs("0..0\n", real_stdout);
}
return had_failure;
}