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
277
|
/*
* Copyright (C) 2013 Martin Willi
* Copyright (C) 2013 revosec AG
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "test_suite.h"
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
/**
* Failure message buf
*/
static char failure_buf[512];
/**
* Source file failure occurred
*/
static const char *failure_file;
/**
* Line of source file failure occurred
*/
static int failure_line;
/**
* Backtrace of failure, if any
*/
static backtrace_t *failure_backtrace;
/**
* Longjump restore point when failing
*/
sigjmp_buf test_restore_point_env;
/**
* See header.
*/
test_suite_t* test_suite_create(const char *name)
{
test_suite_t *suite;
INIT(suite,
.name = name,
.tcases = array_create(0, 0),
);
return suite;
}
/**
* See header.
*/
test_case_t* test_case_create(const char *name)
{
test_case_t *tcase;
INIT(tcase,
.name = name,
.functions = array_create(sizeof(test_function_t), 0),
.fixtures = array_create(sizeof(test_fixture_t), 0),
.timeout = TEST_FUNCTION_DEFAULT_TIMEOUT,
);
return tcase;
}
/**
* See header.
*/
void test_case_add_checked_fixture(test_case_t *tcase, test_fixture_cb_t setup,
test_fixture_cb_t teardown)
{
test_fixture_t fixture = {
.setup = setup,
.teardown = teardown,
};
array_insert(tcase->fixtures, -1, &fixture);
}
/**
* See header.
*/
void test_case_add_test_name(test_case_t *tcase, char *name,
test_function_cb_t cb, int start, int end)
{
test_function_t fun = {
.name = name,
.cb = cb,
.start = start,
.end = end,
};
array_insert(tcase->functions, -1, &fun);
}
/**
* See header.
*/
void test_case_set_timeout(test_case_t *tcase, int s)
{
tcase->timeout = s;
}
/**
* See header.
*/
void test_suite_add_case(test_suite_t *suite, test_case_t *tcase)
{
array_insert(suite->tcases, -1, tcase);
}
/**
* Main thread performing tests
*/
static pthread_t main_thread;
/**
* Let test case fail
*/
static inline void test_failure()
{
if (pthread_self() == main_thread)
{
siglongjmp(test_restore_point_env, 1);
}
else
{
pthread_kill(main_thread, SIGUSR1);
/* how can we stop just the thread? longjmp to a restore point? */
}
}
/**
* See header.
*/
void test_fail_vmsg(const char *file, int line, char *fmt, va_list args)
{
vsnprintf(failure_buf, sizeof(failure_buf), fmt, args);
failure_line = line;
failure_file = file;
test_failure();
}
/**
* See header.
*/
void test_fail_msg(const char *file, int line, char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vsnprintf(failure_buf, sizeof(failure_buf), fmt, args);
failure_line = line;
failure_file = file;
va_end(args);
test_failure();
}
/**
* Signal handler catching critical and alarm signals
*/
static void test_sighandler(int signal)
{
char *signame;
bool old = FALSE;
switch (signal)
{
case SIGUSR1:
/* a different thread failed, abort test */
return test_failure();
case SIGSEGV:
signame = "SIGSEGV";
break;
case SIGILL:
signame = "SIGILL";
break;
case SIGBUS:
signame = "SIGBUS";
break;
case SIGALRM:
signame = "timeout";
break;
default:
signame = "SIG";
break;
}
if (lib->leak_detective)
{
old = lib->leak_detective->set_state(lib->leak_detective, FALSE);
}
failure_backtrace = backtrace_create(3);
if (lib->leak_detective)
{
lib->leak_detective->set_state(lib->leak_detective, old);
}
test_fail_msg(NULL, 0, "%s(%d)", signame, signal);
/* unable to restore a valid context for that thread, terminate */
fprintf(stderr, "\n%s(%d) outside of main thread:\n", signame, signal);
failure_backtrace->log(failure_backtrace, stderr, TRUE);
fprintf(stderr, "terminating...\n");
abort();
}
/**
* See header.
*/
void test_setup_handler()
{
struct sigaction action = {
.sa_handler = test_sighandler,
};
main_thread = pthread_self();
/* signal handler inherited by all threads */
sigaction(SIGSEGV, &action, NULL);
sigaction(SIGILL, &action, NULL);
sigaction(SIGBUS, &action, NULL);
/* ignore ALRM/USR1, these are catched by main thread only */
action.sa_handler = SIG_IGN;
sigaction(SIGALRM, &action, NULL);
sigaction(SIGUSR1, &action, NULL);
}
/**
* See header.
*/
void test_setup_timeout(int s)
{
struct sigaction action = {
.sa_handler = test_sighandler,
};
/* This called by main thread only. Setup handler for timeout and
* failure cross-thread signaling. */
sigaction(SIGALRM, &action, NULL);
sigaction(SIGUSR1, &action, NULL);
alarm(s);
}
/**
* See header.
*/
int test_failure_get(char *msg, int len, const char **file)
{
strncpy(msg, failure_buf, len - 1);
msg[len - 1] = 0;
*file = failure_file;
return failure_line;
}
/**
* See header.
*/
backtrace_t *test_failure_backtrace()
{
backtrace_t *bt;
bt = failure_backtrace;
failure_backtrace = NULL;
return bt;
}
|