2 * BIRD -- Unit Test Framework (BIRD Test)
4 * Can be freely distributed and used under the terms of the GNU GPL.
15 #include <sys/ioctl.h>
16 #include <sys/resource.h>
19 #include "test/birdtest.h"
20 #include "lib/string.h"
22 #ifdef HAVE_EXECINFO_H
26 #define BACKTRACE_MAX_LINES 100
28 #define sprintf_concat(s, format, ...) \
29 snprintf(s + strlen(s), sizeof(s) - strlen(s), format, ##__VA_ARGS__)
31 static const char *request
;
32 static int list_tests
;
36 static int no_timeout
;
37 static int is_terminal
; /* Whether stdout is a live terminal or pipe redirect */
40 const char *bt_filename
;
41 const char *bt_test_id
;
43 int bt_result
; /* Overall program run result */
44 int bt_suite_result
; /* One suit result */
45 char bt_out_fmt_buf
[1024]; /* Temporary memory buffer for output of testing function */
50 /* Seeded in bt_init() */
51 long int rand_low
, rand_high
;
55 return (rand_low
& 0xffff) | ((rand_high
& 0xffff) << 16);
59 bt_init(int argc
, char *argv
[])
63 srandom(BT_RANDOM_SEED
);
66 bt_filename
= argv
[0];
69 is_terminal
= isatty(fileno(stdout
));
71 while ((c
= getopt(argc
, argv
, "lcdftv")) >= 0)
102 /* Optional requested test_id */
103 if ((optind
+ 1) == argc
)
104 request
= argv
[optind
++];
111 struct rlimit rl
= {RLIM_INFINITY
, RLIM_INFINITY
};
112 int rv
= setrlimit(RLIMIT_CORE
, &rl
);
113 bt_syscall(rv
< 0, "setrlimit RLIMIT_CORE");
119 printf("Usage: %s [-l] [-c] [-d] [-f] [-t] [-vvv] [<test_suit_name>]\n", argv
[0]);
120 printf("Options: \n");
121 printf(" -l List all test suite names and descriptions \n");
122 printf(" -c Force unlimit core dumps (needs root privileges) \n");
123 printf(" -d Die on first failed test case \n");
124 printf(" -f No forking \n");
125 printf(" -t No timeout limit \n");
126 printf(" -v More verbosity, maximum is 3 -vvv \n");
131 bt_dump_backtrace(void)
133 #ifdef HAVE_EXECINFO_H
134 void *buf
[BACKTRACE_MAX_LINES
];
141 lines
= backtrace(buf
, BACKTRACE_MAX_LINES
);
142 bt_log("backtrace() returned %d addresses", lines
);
144 pp_backtrace
= backtrace_symbols(buf
, lines
);
145 if (pp_backtrace
== NULL
)
147 perror("backtrace_symbols");
151 for (j
= 0; j
< lines
; j
++)
152 bt_log("%s", pp_backtrace
[j
]);
155 #endif /* HAVE_EXECINFO_H */
159 int bt_run_test_fn(int (*fn
)(const void *), const void *fn_arg
, int timeout
)
166 if (!bt_suite_result
)
173 get_num_terminal_cols(void)
175 struct winsize w
= {};
176 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
177 uint cols
= w
.ws_col
;
178 return (cols
> 0 ? cols
: 80);
182 * bt_log_result - pretty print of test result
184 * @fmt: a description message (could be long, over more lines)
185 * @argptr: variable argument list
187 * This function is used for pretty printing of test results on all verbose
191 bt_log_result(int result
, const char *fmt
, va_list argptr
)
193 static char msg_buf
[BT_BUFFER_SIZE
];
196 snprintf(msg_buf
, sizeof(msg_buf
), "%s%s%s%s",
198 bt_test_id
? ": " : "",
199 bt_test_id
? bt_test_id
: "",
200 (fmt
&& strlen(fmt
) > 0) ? ": " : "");
201 pos
= msg_buf
+ strlen(msg_buf
);
203 vsnprintf(pos
, sizeof(msg_buf
) - (pos
- msg_buf
), fmt
, argptr
);
206 for (uint i
= 0; i
< strlen(msg_buf
); i
+= get_num_terminal_cols())
210 char *stop
= msg_buf
+ i
+ get_num_terminal_cols();
213 chrs
= printf("%s", msg_buf
+ i
);
217 int offset
= get_num_terminal_cols() - chrs
- BT_PROMPT_OK_FAIL_STRLEN
;
221 offset
= get_num_terminal_cols() - BT_PROMPT_OK_FAIL_STRLEN
;
224 for (int i
= 0; i
< offset
; i
++)
227 const char *result_str
= is_terminal
? BT_PROMPT_OK
: BT_PROMPT_OK_NO_COLOR
;
229 result_str
= is_terminal
? BT_PROMPT_FAIL
: BT_PROMPT_FAIL_NO_COLOR
;
231 printf("%s\n", result_str
);
233 if (do_die
&& !result
)
238 * bt_log_overall_result - pretty print of suite case result
240 * @fmt: a description message (could be long, over more lines)
241 * ...: variable argument list
243 * This function is used for pretty printing of test suite case result.
246 bt_log_overall_result(int result
, const char *fmt
, ...)
249 va_start(argptr
, fmt
);
250 bt_log_result(result
, fmt
, argptr
);
255 * bt_log_suite_result - pretty print of suite case result
257 * @fmt: a description message (could be long, over more lines)
258 * ...: variable argument list
260 * This function is used for pretty printing of test suite case result.
263 bt_log_suite_result(int result
, const char *fmt
, ...)
265 if(bt_verbose
>= BT_VERBOSE_SUITE
|| !result
)
268 va_start(argptr
, fmt
);
269 bt_log_result(result
, fmt
, argptr
);
275 * bt_log_suite_case_result - pretty print of suite result
277 * @fmt: a description message (could be long, over more lines)
278 * ...: variable argument list
280 * This function is used for pretty printing of test suite result.
283 bt_log_suite_case_result(int result
, const char *fmt
, ...)
285 if(bt_verbose
>= BT_VERBOSE_SUITE_CASE
)
288 va_start(argptr
, fmt
);
289 bt_log_result(result
, fmt
, argptr
);
295 bt_test_suite_base(int (*fn
)(const void *), const char *id
, const void *fn_arg
, int forked
, int timeout
, const char *dsc
, ...)
299 printf("%28s - ", id
);
314 if (request
&& strcmp(id
, request
))
320 if (bt_verbose
>= BT_VERBOSE_ABSOLUTELY_ALL
)
325 bt_suite_result
= bt_run_test_fn(fn
, fn_arg
, timeout
);
330 bt_syscall(pid
< 0, "fork");
335 _exit(bt_run_test_fn(fn
, fn_arg
, timeout
));
339 int rv
= waitpid(pid
, &s
, 0);
340 bt_syscall(rv
< 0, "waitpid");
345 bt_suite_result
= WEXITSTATUS(s
);
347 else if (WIFSIGNALED(s
))
349 /* Stopped by signal */
352 int sn
= WTERMSIG(s
);
355 bt_log("Timeout expired");
357 else if (sn
== SIGSEGV
)
359 bt_log("Segmentation fault");
362 else if (sn
!= SIGABRT
)
363 bt_log("Signal %d received", sn
);
366 if (WCOREDUMP(s
) && bt_verbose
)
367 bt_log("Core dumped");
370 if (!bt_suite_result
)
373 bt_log_suite_result(bt_suite_result
, NULL
);
376 return bt_suite_result
;
382 if (!list_tests
|| (list_tests
&& !bt_result
))
383 bt_log_overall_result(bt_result
, "");
384 return bt_result
? EXIT_SUCCESS
: EXIT_FAILURE
;
388 * bt_assert_batch__ - test a batch of inputs/outputs tests
389 * @opts: includes all necessary data
391 * Should be called using macro bt_assert_batch().
395 bt_assert_batch__(struct bt_batch
*opts
)
398 for (i
= 0; i
< opts
->ndata
; i
++)
400 int bt_suit_case_result
= opts
->test_fn(opts
->out_buf
, opts
->data
[i
].in
, opts
->data
[i
].out
);
402 if (bt_suit_case_result
== 0)
405 char b
[BT_BUFFER_SIZE
];
406 snprintf(b
, sizeof(b
), "%s(", opts
->test_fn_name
);
408 opts
->in_fmt(b
+strlen(b
), sizeof(b
)-strlen(b
), opts
->data
[i
].in
);
409 sprintf_concat(b
, ") gives ");
410 opts
->out_fmt(b
+strlen(b
), sizeof(b
)-strlen(b
), opts
->out_buf
);
412 if (bt_suit_case_result
== 0)
414 sprintf_concat(b
, ", but expecting is ");
415 opts
->out_fmt(b
+strlen(b
), sizeof(b
)-strlen(b
), opts
->data
[i
].out
);
418 bt_log_suite_case_result(bt_suit_case_result
, "%s", b
);
421 return bt_suite_result
;
425 * bt_fmt_str - formating string into output buffer
426 * @buf: buffer for write
427 * @size: empty size in @buf
428 * @data: null-byte terminated string
430 * This function can be used with bt_assert_batch() function.
431 * Input @data should be const char * string.
434 bt_fmt_str(char *buf
, size_t size
, const void *data
)
436 const byte
*s
= data
;
438 snprintf(buf
, size
, "\"");
441 snprintf(buf
+strlen(buf
), size
-strlen(buf
), bt_is_char(*s
) ? "%c" : "\\%03u", *s
);
444 snprintf(buf
+strlen(buf
), size
-strlen(buf
), "\"");
448 * bt_fmt_unsigned - formating unsigned int into output buffer
449 * @buf: buffer for write
450 * @size: empty size in @buf
451 * @data: unsigned number
453 * This function can be used with bt_assert_batch() function.
456 bt_fmt_unsigned(char *buf
, size_t size
, const void *data
)
458 const uint
*n
= data
;
459 snprintf(buf
, size
, "0x%x (%u)", *n
, *n
);
463 * bt_fmt_ipa - formating ip_addr into output buffer
464 * @buf: buffer for write
465 * @size: empty size in @buf
466 * @data: should be struct ip_addr *
468 * This function can be used with bt_assert_batch() function.
471 bt_fmt_ipa(char *buf
, size_t size
, const void *data
)
473 const ip_addr
*ip
= data
;
474 bsnprintf(buf
, size
, "%I", *ip
);
480 return (c
>= (byte
) 32 && c
<= (byte
) 126);
484 * Mock-ups of all necessary public functions in main.c
488 void async_config(void) {}
489 void async_dump(void) {}
490 void async_shutdown(void) {}
491 void cmd_check_config(char *name UNUSED
) {}
492 void cmd_reconfig(char *name UNUSED
, int type UNUSED
, int timeout UNUSED
) {}
493 void cmd_reconfig_confirm(void) {}
494 void cmd_reconfig_undo(void) {}
495 void cmd_reconfig_status(void) {}
496 void cmd_graceful_restart(void) {}
497 void cmd_shutdown(void) {}
498 void cmd_reconfig_undo_notify(void) {}
500 #include "nest/bird.h"
502 #include "conf/conf.h"
503 void sysdep_preconfig(struct config
*c UNUSED
) {}
504 int sysdep_commit(struct config
*new UNUSED
, struct config
*old UNUSED
) { return 0; }
505 void sysdep_shutdown_done(void) {}
507 #include "nest/cli.h"
508 int cli_get_command(cli
*c UNUSED
) { return 0; }
509 void cli_write_trigger(cli
*c UNUSED
) {}
510 cli
*cmd_reconfig_stored_cli
;