]> git.ipfire.org Git - thirdparty/bird.git/blob - test/birdtest.c
Test: Fixed annoying warnings (and possible obscure bugs).
[thirdparty/bird.git] / test / birdtest.c
1 /*
2 * BIRD -- Unit Test Framework (BIRD Test)
3 *
4 * Can be freely distributed and used under the terms of the GNU GPL.
5 */
6
7 #include <stdarg.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <signal.h>
13 #include <unistd.h>
14
15 #include <sys/ioctl.h>
16 #include <sys/resource.h>
17 #include <sys/wait.h>
18
19 #include "test/birdtest.h"
20 #include "lib/string.h"
21
22 #ifdef HAVE_EXECINFO_H
23 #include <execinfo.h>
24 #endif
25
26 #define BACKTRACE_MAX_LINES 100
27
28 #define sprintf_concat(s, format, ...) \
29 snprintf(s + strlen(s), sizeof(s) - strlen(s), format, ##__VA_ARGS__)
30
31 static const char *request;
32 static int list_tests;
33 static int do_core;
34 static int no_fork;
35 static int no_timeout;
36 static int is_terminal; /* Whether stdout is a live terminal or pipe redirect */
37
38 uint bt_verbose;
39 const char *bt_filename;
40 const char *bt_test_id;
41
42 int bt_result; /* Overall program run result */
43 int bt_suite_result; /* One suit result */
44 char bt_out_fmt_buf[1024]; /* Temporary memory buffer for output of testing function */
45
46 u64 bt_random_state[] = {
47 0x80241f302bd4d95d, 0xd10ba2e910f772b, 0xea188c9046f507c5, 0x4c4c581f04e6da05,
48 0x53d9772877c1b647, 0xab8ce3eb466de6c5, 0xad02844c8a8e865f, 0xe8cc78080295065d
49 };
50
51 void
52 bt_init(int argc, char *argv[])
53 {
54 int c;
55
56 initstate(BT_RANDOM_SEED, (char *) bt_random_state, sizeof(bt_random_state));
57
58 bt_verbose = 0;
59 bt_filename = argv[0];
60 bt_result = 1;
61 bt_test_id = NULL;
62 is_terminal = isatty(fileno(stdout));
63
64 while ((c = getopt(argc, argv, "lcftv")) >= 0)
65 switch (c)
66 {
67 case 'l':
68 list_tests = 1;
69 break;
70
71 case 'c':
72 do_core = 1;
73 break;
74
75 case 'f':
76 no_fork = 1;
77 break;
78
79 case 't':
80 no_timeout = 1;
81 break;
82
83 case 'v':
84 bt_verbose++;
85 break;
86
87 default:
88 goto usage;
89 }
90
91 /* Optional requested test_id */
92 if ((optind + 1) == argc)
93 request = argv[optind++];
94
95 if (optind != argc)
96 goto usage;
97
98 if (do_core)
99 {
100 struct rlimit rl = {RLIM_INFINITY, RLIM_INFINITY};
101 int rv = setrlimit(RLIMIT_CORE, &rl);
102 bt_syscall(rv < 0, "setrlimit RLIMIT_CORE");
103 }
104
105 return;
106
107 usage:
108 printf("Usage: %s [-l] [-c] [-f] [-t] [-vvv] [<test_suit_name>]\n", argv[0]);
109 printf("Options: \n");
110 printf(" -l List all test suite names and descriptions \n");
111 printf(" -c Force unlimit core dumps (needs root privileges) \n");
112 printf(" -f No forking \n");
113 printf(" -t No timeout limit \n");
114 printf(" -v More verbosity, maximum is 3 -vvv \n");
115 exit(3);
116 }
117
118 static void
119 bt_dump_backtrace(void)
120 {
121 #ifdef HAVE_EXECINFO_H
122 void *buf[BACKTRACE_MAX_LINES];
123 char **pp_backtrace;
124 int lines, j;
125
126 if (!bt_verbose)
127 return;
128
129 lines = backtrace(buf, BACKTRACE_MAX_LINES);
130 bt_log("backtrace() returned %d addresses", lines);
131
132 pp_backtrace = backtrace_symbols(buf, lines);
133 if (pp_backtrace == NULL)
134 {
135 perror("backtrace_symbols");
136 exit(EXIT_FAILURE);
137 }
138
139 for (j = 0; j < lines; j++)
140 bt_log("%s", pp_backtrace[j]);
141
142 free(pp_backtrace);
143 #endif /* HAVE_EXECINFO_H */
144 }
145
146 static
147 int bt_run_test_fn(int (*fn)(const void *), const void *fn_arg, int timeout)
148 {
149 int result;
150 alarm(timeout);
151
152 result = fn(fn_arg);
153
154 if (!bt_suite_result)
155 result = 0;
156
157 return result;
158 }
159
160 static uint
161 get_num_terminal_cols(void)
162 {
163 struct winsize w = {};
164 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
165 uint cols = w.ws_col;
166 return (cols > 0 ? cols : 80);
167 }
168
169 /**
170 * bt_log_result - pretty print of test result
171 * @result: 1 or 0
172 * @fmt: a description message (could be long, over more lines)
173 * @argptr: variable argument list
174 *
175 * This function is used for pretty printing of test results on all verbose
176 * levels.
177 */
178 static void
179 bt_log_result(int result, const char *fmt, va_list argptr)
180 {
181 static char msg_buf[BT_BUFFER_SIZE];
182 char *pos;
183
184 snprintf(msg_buf, sizeof(msg_buf), "%s%s%s%s",
185 bt_filename,
186 bt_test_id ? ": " : "",
187 bt_test_id ? bt_test_id : "",
188 (fmt && strlen(fmt) > 0) ? ": " : "");
189 pos = msg_buf + strlen(msg_buf);
190
191 vsnprintf(pos, sizeof(msg_buf) - (pos - msg_buf), fmt, argptr);
192
193 int chrs = 0;
194 for (uint i = 0; i < strlen(msg_buf); i += get_num_terminal_cols())
195 {
196 if (i)
197 printf("\n");
198 char *stop = msg_buf + i + get_num_terminal_cols();
199 char backup = *stop;
200 *stop = 0;
201 chrs = printf("%s", msg_buf + i);
202 *stop = backup;
203 }
204
205 int offset = get_num_terminal_cols() - chrs - BT_PROMPT_OK_FAIL_STRLEN;
206 if (offset < 0)
207 {
208 printf("\n");
209 offset = get_num_terminal_cols() - BT_PROMPT_OK_FAIL_STRLEN;
210 }
211
212 for (int i = 0; i < offset; i++)
213 putchar(' ');
214
215 const char *result_str = is_terminal ? BT_PROMPT_OK : BT_PROMPT_OK_NO_COLOR;
216 if (!result)
217 result_str = is_terminal ? BT_PROMPT_FAIL : BT_PROMPT_FAIL_NO_COLOR;
218
219 printf("%s\n", result_str);
220 }
221
222 /**
223 * bt_log_overall_result - pretty print of suite case result
224 * @result: 1 or 0
225 * @fmt: a description message (could be long, over more lines)
226 * ...: variable argument list
227 *
228 * This function is used for pretty printing of test suite case result.
229 */
230 static void
231 bt_log_overall_result(int result, const char *fmt, ...)
232 {
233 va_list argptr;
234 va_start(argptr, fmt);
235 bt_log_result(result, fmt, argptr);
236 va_end(argptr);
237 }
238
239 /**
240 * bt_log_suite_result - pretty print of suite case result
241 * @result: 1 or 0
242 * @fmt: a description message (could be long, over more lines)
243 * ...: variable argument list
244 *
245 * This function is used for pretty printing of test suite case result.
246 */
247 void
248 bt_log_suite_result(int result, const char *fmt, ...)
249 {
250 if(bt_verbose >= BT_VERBOSE_SUITE || !result)
251 {
252 va_list argptr;
253 va_start(argptr, fmt);
254 bt_log_result(result, fmt, argptr);
255 va_end(argptr);
256 }
257 }
258
259 /**
260 * bt_log_suite_case_result - pretty print of suite result
261 * @result: 1 or 0
262 * @fmt: a description message (could be long, over more lines)
263 * ...: variable argument list
264 *
265 * This function is used for pretty printing of test suite result.
266 */
267 void
268 bt_log_suite_case_result(int result, const char *fmt, ...)
269 {
270 if(bt_verbose >= BT_VERBOSE_SUITE_CASE)
271 {
272 va_list argptr;
273 va_start(argptr, fmt);
274 bt_log_result(result, fmt, argptr);
275 va_end(argptr);
276 }
277 }
278
279 int
280 bt_test_suite_base(int (*fn)(const void *), const char *id, const void *fn_arg, int forked, int timeout, const char *dsc, ...)
281 {
282 if (list_tests)
283 {
284 printf("%28s - ", id);
285 va_list args;
286 va_start(args, dsc);
287 vprintf(dsc, args);
288 va_end(args);
289 printf("\n");
290 return 1;
291 }
292
293 if (no_fork)
294 forked = 0;
295
296 if (no_timeout)
297 timeout = 0;
298
299 if (request && strcmp(id, request))
300 return 1;
301
302 bt_suite_result = 1;
303 bt_test_id = id;
304
305 if (bt_verbose >= BT_VERBOSE_ABSOLUTELY_ALL)
306 bt_log("Starting");
307
308 if (!forked)
309 {
310 bt_suite_result = bt_run_test_fn(fn, fn_arg, timeout);
311 }
312 else
313 {
314 pid_t pid = fork();
315 bt_syscall(pid < 0, "fork");
316
317 if (pid == 0)
318 {
319 /* child of fork */
320 _exit(bt_run_test_fn(fn, fn_arg, timeout));
321 }
322
323 int s;
324 int rv = waitpid(pid, &s, 0);
325 bt_syscall(rv < 0, "waitpid");
326
327 if (WIFEXITED(s))
328 {
329 /* Normal exit */
330 bt_suite_result = WEXITSTATUS(s);
331 }
332 else if (WIFSIGNALED(s))
333 {
334 /* Stopped by signal */
335 bt_suite_result = 0;
336
337 int sn = WTERMSIG(s);
338 if (sn == SIGALRM)
339 {
340 bt_log("Timeout expired");
341 }
342 else if (sn == SIGSEGV)
343 {
344 bt_log("Segmentation fault");
345 bt_dump_backtrace();
346 }
347 else if (sn != SIGABRT)
348 bt_log("Signal %d received", sn);
349 }
350
351 if (WCOREDUMP(s) && bt_verbose)
352 bt_log("Core dumped");
353 }
354
355 if (!bt_suite_result)
356 bt_result = 0;
357
358 bt_log_suite_result(bt_suite_result, NULL);
359 bt_test_id = NULL;
360
361 return bt_suite_result;
362 }
363
364 int
365 bt_exit_value(void)
366 {
367 if (!list_tests || (list_tests && !bt_result))
368 bt_log_overall_result(bt_result, "");
369 return bt_result ? EXIT_SUCCESS : EXIT_FAILURE;
370 }
371
372 /**
373 * bt_assert_batch__ - test a batch of inputs/outputs tests
374 * @opts: includes all necessary data
375 *
376 * Should be called using macro bt_assert_batch().
377 * Returns 1 or 0.
378 */
379 int
380 bt_assert_batch__(struct bt_batch *opts)
381 {
382 int i;
383 for (i = 0; i < opts->ndata; i++)
384 {
385 int bt_suit_case_result = opts->test_fn(opts->out_buf, opts->data[i].in, opts->data[i].out);
386
387 if (bt_suit_case_result == 0)
388 bt_suite_result = 0;
389
390 char b[BT_BUFFER_SIZE];
391 snprintf(b, sizeof(b), "%s(", opts->test_fn_name);
392
393 opts->in_fmt(b+strlen(b), sizeof(b)-strlen(b), opts->data[i].in);
394 sprintf_concat(b, ") gives ");
395 opts->out_fmt(b+strlen(b), sizeof(b)-strlen(b), opts->out_buf);
396
397 if (bt_suit_case_result == 0)
398 {
399 sprintf_concat(b, ", but expecting is ");
400 opts->out_fmt(b+strlen(b), sizeof(b)-strlen(b), opts->data[i].out);
401 }
402
403 bt_log_suite_case_result(bt_suit_case_result, "%s", b);
404 }
405
406 return bt_suite_result;
407 }
408
409 /**
410 * bt_fmt_str - formating string into output buffer
411 * @buf: buffer for write
412 * @size: empty size in @buf
413 * @data: null-byte terminated string
414 *
415 * This function can be used with bt_assert_batch() function.
416 * Input @data should be const char * string.
417 */
418 void
419 bt_fmt_str(char *buf, size_t size, const void *data)
420 {
421 const byte *s = data;
422
423 snprintf(buf, size, "\"");
424 while (*s)
425 {
426 snprintf(buf+strlen(buf), size-strlen(buf), bt_is_char(*s) ? "%c" : "\\%03u", *s);
427 s++;
428 }
429 snprintf(buf+strlen(buf), size-strlen(buf), "\"");
430 }
431
432 /**
433 * bt_fmt_unsigned - formating unsigned int into output buffer
434 * @buf: buffer for write
435 * @size: empty size in @buf
436 * @data: unsigned number
437 *
438 * This function can be used with bt_assert_batch() function.
439 */
440 void
441 bt_fmt_unsigned(char *buf, size_t size, const void *data)
442 {
443 const uint *n = data;
444 snprintf(buf, size, "0x%x (%u)", *n, *n);
445 }
446
447 /**
448 * bt_fmt_ipa - formating ip_addr into output buffer
449 * @buf: buffer for write
450 * @size: empty size in @buf
451 * @data: should be struct ip_addr *
452 *
453 * This function can be used with bt_assert_batch() function.
454 */
455 void
456 bt_fmt_ipa(char *buf, size_t size, const void *data)
457 {
458 const ip_addr *ip = data;
459 bsnprintf(buf, size, "%I", *ip);
460 }
461
462 int
463 bt_is_char(byte c)
464 {
465 return (c >= (byte) 32 && c <= (byte) 126);
466 }
467
468 /*
469 * Mock-ups of all necessary public functions in main.c
470 */
471
472 char *bird_name;
473 void async_config(void) {}
474 void async_dump(void) {}
475 void async_shutdown(void) {}
476 void cmd_check_config(char *name UNUSED) {}
477 void cmd_reconfig(char *name UNUSED, int type UNUSED, int timeout UNUSED) {}
478 void cmd_reconfig_confirm(void) {}
479 void cmd_reconfig_undo(void) {}
480 void cmd_reconfig_status(void) {}
481 void cmd_graceful_restart(void) {}
482 void cmd_shutdown(void) {}
483 void cmd_reconfig_undo_notify(void) {}
484
485 #include "nest/bird.h"
486 #include "lib/net.h"
487 #include "conf/conf.h"
488 void sysdep_preconfig(struct config *c UNUSED) {}
489 int sysdep_commit(struct config *new UNUSED, struct config *old UNUSED) { return 0; }
490 void sysdep_shutdown_done(void) {}
491
492 #include "nest/cli.h"
493 int cli_get_command(cli *c UNUSED) { return 0; }
494 void cli_write_trigger(cli *c UNUSED) {}
495 cli *cmd_reconfig_stored_cli;