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
;
35 static int no_timeout
;
36 static int is_terminal
; /* Whether stdout is a live terminal or pipe redirect */
39 const char *bt_filename
;
40 const char *bt_test_id
;
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 */
46 u64 bt_random_state
[] = {
47 0x80241f302bd4d95d, 0xd10ba2e910f772b, 0xea188c9046f507c5, 0x4c4c581f04e6da05,
48 0x53d9772877c1b647, 0xab8ce3eb466de6c5, 0xad02844c8a8e865f, 0xe8cc78080295065d
52 bt_init(int argc
, char *argv
[])
56 initstate(BT_RANDOM_SEED
, (char *) bt_random_state
, sizeof(bt_random_state
));
59 bt_filename
= argv
[0];
62 is_terminal
= isatty(fileno(stdout
));
64 while ((c
= getopt(argc
, argv
, "lcftv")) >= 0)
91 /* Optional requested test_id */
92 if ((optind
+ 1) == argc
)
93 request
= argv
[optind
++];
100 struct rlimit rl
= {RLIM_INFINITY
, RLIM_INFINITY
};
101 int rv
= setrlimit(RLIMIT_CORE
, &rl
);
102 bt_syscall(rv
< 0, "setrlimit RLIMIT_CORE");
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");
119 bt_dump_backtrace(void)
121 #ifdef HAVE_EXECINFO_H
122 void *buf
[BACKTRACE_MAX_LINES
];
129 lines
= backtrace(buf
, BACKTRACE_MAX_LINES
);
130 bt_log("backtrace() returned %d addresses", lines
);
132 pp_backtrace
= backtrace_symbols(buf
, lines
);
133 if (pp_backtrace
== NULL
)
135 perror("backtrace_symbols");
139 for (j
= 0; j
< lines
; j
++)
140 bt_log("%s", pp_backtrace
[j
]);
143 #endif /* HAVE_EXECINFO_H */
147 int bt_run_test_fn(int (*fn
)(const void *), const void *fn_arg
, int timeout
)
155 result
= ((int (*)(void))fn
)();
157 if (!bt_suite_result
)
164 get_num_terminal_cols(void)
166 struct winsize w
= {};
167 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
168 uint cols
= w
.ws_col
;
169 return (cols
> 0 ? cols
: 80);
173 * bt_log_result - pretty print of test result
175 * @fmt: a description message (could be long, over more lines)
176 * @argptr: variable argument list
178 * This function is used for pretty printing of test results on all verbose
182 bt_log_result(int result
, const char *fmt
, va_list argptr
)
184 static char msg_buf
[BT_BUFFER_SIZE
];
187 snprintf(msg_buf
, sizeof(msg_buf
), "%s%s%s%s",
189 bt_test_id
? ": " : "",
190 bt_test_id
? bt_test_id
: "",
191 (fmt
&& strlen(fmt
) > 0) ? ": " : "");
192 pos
= msg_buf
+ strlen(msg_buf
);
194 vsnprintf(pos
, sizeof(msg_buf
) - (pos
- msg_buf
), fmt
, argptr
);
197 for (uint i
= 0; i
< strlen(msg_buf
); i
+= get_num_terminal_cols())
201 char *stop
= msg_buf
+ i
+ get_num_terminal_cols();
204 chrs
= printf("%s", msg_buf
+ i
);
208 int offset
= get_num_terminal_cols() - chrs
- BT_PROMPT_OK_FAIL_STRLEN
;
212 offset
= get_num_terminal_cols() - BT_PROMPT_OK_FAIL_STRLEN
;
215 for (int i
= 0; i
< offset
; i
++)
218 const char *result_str
= is_terminal
? BT_PROMPT_OK
: BT_PROMPT_OK_NO_COLOR
;
220 result_str
= is_terminal
? BT_PROMPT_FAIL
: BT_PROMPT_FAIL_NO_COLOR
;
222 printf("%s\n", result_str
);
226 * bt_log_overall_result - pretty print of suite case result
228 * @fmt: a description message (could be long, over more lines)
229 * ...: variable argument list
231 * This function is used for pretty printing of test suite case result.
234 bt_log_overall_result(int result
, const char *fmt
, ...)
237 va_start(argptr
, fmt
);
238 bt_log_result(result
, fmt
, argptr
);
243 * bt_log_suite_result - pretty print of suite case result
245 * @fmt: a description message (could be long, over more lines)
246 * ...: variable argument list
248 * This function is used for pretty printing of test suite case result.
251 bt_log_suite_result(int result
, const char *fmt
, ...)
253 if(bt_verbose
>= BT_VERBOSE_SUITE
|| !result
)
256 va_start(argptr
, fmt
);
257 bt_log_result(result
, fmt
, argptr
);
263 * bt_log_suite_case_result - pretty print of suite result
265 * @fmt: a description message (could be long, over more lines)
266 * ...: variable argument list
268 * This function is used for pretty printing of test suite result.
271 bt_log_suite_case_result(int result
, const char *fmt
, ...)
273 if(bt_verbose
>= BT_VERBOSE_SUITE_CASE
)
276 va_start(argptr
, fmt
);
277 bt_log_result(result
, fmt
, argptr
);
283 bt_test_suite_base(int (*fn
)(const void *), const char *id
, const void *fn_arg
, int forked
, int timeout
, const char *dsc
, ...)
287 printf("%28s - ", id
);
302 if (request
&& strcmp(id
, request
))
308 if (bt_verbose
>= BT_VERBOSE_ABSOLUTELY_ALL
)
313 bt_suite_result
= bt_run_test_fn(fn
, fn_arg
, timeout
);
318 bt_syscall(pid
< 0, "fork");
323 _exit(bt_run_test_fn(fn
, fn_arg
, timeout
));
327 int rv
= waitpid(pid
, &s
, 0);
328 bt_syscall(rv
< 0, "waitpid");
333 bt_suite_result
= WEXITSTATUS(s
);
335 else if (WIFSIGNALED(s
))
337 /* Stopped by signal */
340 int sn
= WTERMSIG(s
);
343 bt_log("Timeout expired");
345 else if (sn
== SIGSEGV
)
347 bt_log("Segmentation fault");
350 else if (sn
!= SIGABRT
)
351 bt_log("Signal %d received", sn
);
354 if (WCOREDUMP(s
) && bt_verbose
)
355 bt_log("Core dumped");
358 if (!bt_suite_result
)
361 bt_log_suite_result(bt_suite_result
, NULL
);
364 return bt_suite_result
;
370 if (!list_tests
|| (list_tests
&& !bt_result
))
371 bt_log_overall_result(bt_result
, "");
372 return bt_result
? EXIT_SUCCESS
: EXIT_FAILURE
;
376 * bt_assert_batch__ - test a batch of inputs/outputs tests
377 * @opts: includes all necessary data
379 * Should be called using macro bt_assert_batch().
383 bt_assert_batch__(struct bt_batch
*opts
)
386 for (i
= 0; i
< opts
->ndata
; i
++)
388 int bt_suit_case_result
= opts
->test_fn(opts
->out_buf
, opts
->data
[i
].in
, opts
->data
[i
].out
);
390 if (bt_suit_case_result
== 0)
393 char b
[BT_BUFFER_SIZE
];
394 snprintf(b
, sizeof(b
), "%s(", opts
->test_fn_name
);
396 opts
->in_fmt(b
+strlen(b
), sizeof(b
)-strlen(b
), opts
->data
[i
].in
);
397 sprintf_concat(b
, ") gives ");
398 opts
->out_fmt(b
+strlen(b
), sizeof(b
)-strlen(b
), opts
->out_buf
);
400 if (bt_suit_case_result
== 0)
402 sprintf_concat(b
, ", but expecting is ");
403 opts
->out_fmt(b
+strlen(b
), sizeof(b
)-strlen(b
), opts
->data
[i
].out
);
406 bt_log_suite_case_result(bt_suit_case_result
, "%s", b
);
409 return bt_suite_result
;
413 * bt_fmt_str - formating string into output buffer
414 * @buf: buffer for write
415 * @size: empty size in @buf
416 * @data: null-byte terminated string
418 * This function can be used with bt_assert_batch() function.
419 * Input @data should be const char * string.
422 bt_fmt_str(char *buf
, size_t size
, const void *data
)
424 const byte
*s
= data
;
426 snprintf(buf
, size
, "\"");
429 snprintf(buf
+strlen(buf
), size
-strlen(buf
), bt_is_char(*s
) ? "%c" : "\\%03u", *s
);
432 snprintf(buf
+strlen(buf
), size
-strlen(buf
), "\"");
436 * bt_fmt_unsigned - formating unsigned int into output buffer
437 * @buf: buffer for write
438 * @size: empty size in @buf
439 * @data: unsigned number
441 * This function can be used with bt_assert_batch() function.
444 bt_fmt_unsigned(char *buf
, size_t size
, const void *data
)
446 const uint
*n
= data
;
447 snprintf(buf
, size
, "0x%x (%u)", *n
, *n
);
451 * bt_fmt_ipa - formating ip_addr into output buffer
452 * @buf: buffer for write
453 * @size: empty size in @buf
454 * @data: should be struct ip_addr *
456 * This function can be used with bt_assert_batch() function.
459 bt_fmt_ipa(char *buf
, size_t size
, const void *data
)
461 const ip_addr
*ip
= data
;
462 bsnprintf(buf
, size
, "%I", *ip
);
468 return (c
>= (byte
) 32 && c
<= (byte
) 126);
472 * Mock-ups of all necessary public functions in main.c
476 void async_config(void) {}
477 void async_dump(void) {}
478 void async_shutdown(void) {}
479 void cmd_check_config(char *name UNUSED
) {}
480 void cmd_reconfig(char *name UNUSED
, int type UNUSED
, int timeout UNUSED
) {}
481 void cmd_reconfig_confirm(void) {}
482 void cmd_reconfig_undo(void) {}
483 void cmd_reconfig_status(void) {}
484 void cmd_graceful_restart(void) {}
485 void cmd_shutdown(void) {}
486 void cmd_reconfig_undo_notify(void) {}
488 #include "nest/bird.h"
490 #include "conf/conf.h"
491 void sysdep_preconfig(struct config
*c UNUSED
) {}
492 int sysdep_commit(struct config
*new UNUSED
, struct config
*old UNUSED
) { return 0; }
493 void sysdep_shutdown_done(void) {}
495 #include "nest/cli.h"
496 int cli_get_command(cli
*c UNUSED
) { return 0; }
497 void cli_write_trigger(cli
*c UNUSED
) {}
498 cli
*cmd_reconfig_stored_cli
;