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
)
154 if (!bt_suite_result
)
161 get_num_terminal_cols(void)
163 struct winsize w
= {};
164 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
165 uint cols
= w
.ws_col
;
166 return (cols
> 0 ? cols
: 80);
170 * bt_log_result - pretty print of test result
172 * @fmt: a description message (could be long, over more lines)
173 * @argptr: variable argument list
175 * This function is used for pretty printing of test results on all verbose
179 bt_log_result(int result
, const char *fmt
, va_list argptr
)
181 static char msg_buf
[BT_BUFFER_SIZE
];
184 snprintf(msg_buf
, sizeof(msg_buf
), "%s%s%s%s",
186 bt_test_id
? ": " : "",
187 bt_test_id
? bt_test_id
: "",
188 (fmt
&& strlen(fmt
) > 0) ? ": " : "");
189 pos
= msg_buf
+ strlen(msg_buf
);
191 vsnprintf(pos
, sizeof(msg_buf
) - (pos
- msg_buf
), fmt
, argptr
);
194 for (uint i
= 0; i
< strlen(msg_buf
); i
+= get_num_terminal_cols())
198 char *stop
= msg_buf
+ i
+ get_num_terminal_cols();
201 chrs
= printf("%s", msg_buf
+ i
);
205 int offset
= get_num_terminal_cols() - chrs
- BT_PROMPT_OK_FAIL_STRLEN
;
209 offset
= get_num_terminal_cols() - BT_PROMPT_OK_FAIL_STRLEN
;
212 for (int i
= 0; i
< offset
; i
++)
215 const char *result_str
= is_terminal
? BT_PROMPT_OK
: BT_PROMPT_OK_NO_COLOR
;
217 result_str
= is_terminal
? BT_PROMPT_FAIL
: BT_PROMPT_FAIL_NO_COLOR
;
219 printf("%s\n", result_str
);
223 * bt_log_overall_result - pretty print of suite case result
225 * @fmt: a description message (could be long, over more lines)
226 * ...: variable argument list
228 * This function is used for pretty printing of test suite case result.
231 bt_log_overall_result(int result
, const char *fmt
, ...)
234 va_start(argptr
, fmt
);
235 bt_log_result(result
, fmt
, argptr
);
240 * bt_log_suite_result - pretty print of suite case result
242 * @fmt: a description message (could be long, over more lines)
243 * ...: variable argument list
245 * This function is used for pretty printing of test suite case result.
248 bt_log_suite_result(int result
, const char *fmt
, ...)
250 if(bt_verbose
>= BT_VERBOSE_SUITE
|| !result
)
253 va_start(argptr
, fmt
);
254 bt_log_result(result
, fmt
, argptr
);
260 * bt_log_suite_case_result - pretty print of suite result
262 * @fmt: a description message (could be long, over more lines)
263 * ...: variable argument list
265 * This function is used for pretty printing of test suite result.
268 bt_log_suite_case_result(int result
, const char *fmt
, ...)
270 if(bt_verbose
>= BT_VERBOSE_SUITE_CASE
)
273 va_start(argptr
, fmt
);
274 bt_log_result(result
, fmt
, argptr
);
280 bt_test_suite_base(int (*fn
)(const void *), const char *id
, const void *fn_arg
, int forked
, int timeout
, const char *dsc
, ...)
284 printf("%28s - ", id
);
299 if (request
&& strcmp(id
, request
))
305 if (bt_verbose
>= BT_VERBOSE_ABSOLUTELY_ALL
)
310 bt_suite_result
= bt_run_test_fn(fn
, fn_arg
, timeout
);
315 bt_syscall(pid
< 0, "fork");
320 _exit(bt_run_test_fn(fn
, fn_arg
, timeout
));
324 int rv
= waitpid(pid
, &s
, 0);
325 bt_syscall(rv
< 0, "waitpid");
330 bt_suite_result
= WEXITSTATUS(s
);
332 else if (WIFSIGNALED(s
))
334 /* Stopped by signal */
337 int sn
= WTERMSIG(s
);
340 bt_log("Timeout expired");
342 else if (sn
== SIGSEGV
)
344 bt_log("Segmentation fault");
347 else if (sn
!= SIGABRT
)
348 bt_log("Signal %d received", sn
);
351 if (WCOREDUMP(s
) && bt_verbose
)
352 bt_log("Core dumped");
355 if (!bt_suite_result
)
358 bt_log_suite_result(bt_suite_result
, NULL
);
361 return bt_suite_result
;
367 if (!list_tests
|| (list_tests
&& !bt_result
))
368 bt_log_overall_result(bt_result
, "");
369 return bt_result
? EXIT_SUCCESS
: EXIT_FAILURE
;
373 * bt_assert_batch__ - test a batch of inputs/outputs tests
374 * @opts: includes all necessary data
376 * Should be called using macro bt_assert_batch().
380 bt_assert_batch__(struct bt_batch
*opts
)
383 for (i
= 0; i
< opts
->ndata
; i
++)
385 int bt_suit_case_result
= opts
->test_fn(opts
->out_buf
, opts
->data
[i
].in
, opts
->data
[i
].out
);
387 if (bt_suit_case_result
== 0)
390 char b
[BT_BUFFER_SIZE
];
391 snprintf(b
, sizeof(b
), "%s(", opts
->test_fn_name
);
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
);
397 if (bt_suit_case_result
== 0)
399 sprintf_concat(b
, ", but expecting is ");
400 opts
->out_fmt(b
+strlen(b
), sizeof(b
)-strlen(b
), opts
->data
[i
].out
);
403 bt_log_suite_case_result(bt_suit_case_result
, "%s", b
);
406 return bt_suite_result
;
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
415 * This function can be used with bt_assert_batch() function.
416 * Input @data should be const char * string.
419 bt_fmt_str(char *buf
, size_t size
, const void *data
)
421 const byte
*s
= data
;
423 snprintf(buf
, size
, "\"");
426 snprintf(buf
+strlen(buf
), size
-strlen(buf
), bt_is_char(*s
) ? "%c" : "\\%03u", *s
);
429 snprintf(buf
+strlen(buf
), size
-strlen(buf
), "\"");
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
438 * This function can be used with bt_assert_batch() function.
441 bt_fmt_unsigned(char *buf
, size_t size
, const void *data
)
443 const uint
*n
= data
;
444 snprintf(buf
, size
, "0x%x (%u)", *n
, *n
);
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 *
453 * This function can be used with bt_assert_batch() function.
456 bt_fmt_ipa(char *buf
, size_t size
, const void *data
)
458 const ip_addr
*ip
= data
;
459 bsnprintf(buf
, size
, "%I", *ip
);
465 return (c
>= (byte
) 32 && c
<= (byte
) 126);
469 * Mock-ups of all necessary public functions in main.c
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) {}
485 #include "nest/bird.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) {}
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
;