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 */
49 /* Seeded in bt_init() */
50 long int rand_low
, rand_high
;
54 return (rand_low
& 0xffff) | ((rand_high
& 0xffff) << 16);
58 bt_init(int argc
, char *argv
[])
62 srandom(BT_RANDOM_SEED
);
65 bt_filename
= argv
[0];
68 is_terminal
= isatty(fileno(stdout
));
70 while ((c
= getopt(argc
, argv
, "lcftv")) >= 0)
97 /* Optional requested test_id */
98 if ((optind
+ 1) == argc
)
99 request
= argv
[optind
++];
106 struct rlimit rl
= {RLIM_INFINITY
, RLIM_INFINITY
};
107 int rv
= setrlimit(RLIMIT_CORE
, &rl
);
108 bt_syscall(rv
< 0, "setrlimit RLIMIT_CORE");
114 printf("Usage: %s [-l] [-c] [-f] [-t] [-vvv] [<test_suit_name>]\n", argv
[0]);
115 printf("Options: \n");
116 printf(" -l List all test suite names and descriptions \n");
117 printf(" -c Force unlimit core dumps (needs root privileges) \n");
118 printf(" -f No forking \n");
119 printf(" -t No timeout limit \n");
120 printf(" -v More verbosity, maximum is 3 -vvv \n");
125 bt_dump_backtrace(void)
127 #ifdef HAVE_EXECINFO_H
128 void *buf
[BACKTRACE_MAX_LINES
];
135 lines
= backtrace(buf
, BACKTRACE_MAX_LINES
);
136 bt_log("backtrace() returned %d addresses", lines
);
138 pp_backtrace
= backtrace_symbols(buf
, lines
);
139 if (pp_backtrace
== NULL
)
141 perror("backtrace_symbols");
145 for (j
= 0; j
< lines
; j
++)
146 bt_log("%s", pp_backtrace
[j
]);
149 #endif /* HAVE_EXECINFO_H */
153 int bt_run_test_fn(int (*fn
)(const void *), const void *fn_arg
, int timeout
)
161 result
= ((int (*)(void))fn
)();
163 if (!bt_suite_result
)
170 get_num_terminal_cols(void)
172 struct winsize w
= {};
173 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
174 uint cols
= w
.ws_col
;
175 return (cols
> 0 ? cols
: 80);
179 * bt_log_result - pretty print of test result
181 * @fmt: a description message (could be long, over more lines)
182 * @argptr: variable argument list
184 * This function is used for pretty printing of test results on all verbose
188 bt_log_result(int result
, const char *fmt
, va_list argptr
)
190 static char msg_buf
[BT_BUFFER_SIZE
];
193 snprintf(msg_buf
, sizeof(msg_buf
), "%s%s%s%s",
195 bt_test_id
? ": " : "",
196 bt_test_id
? bt_test_id
: "",
197 (fmt
&& strlen(fmt
) > 0) ? ": " : "");
198 pos
= msg_buf
+ strlen(msg_buf
);
200 vsnprintf(pos
, sizeof(msg_buf
) - (pos
- msg_buf
), fmt
, argptr
);
203 for (uint i
= 0; i
< strlen(msg_buf
); i
+= get_num_terminal_cols())
207 char *stop
= msg_buf
+ i
+ get_num_terminal_cols();
210 chrs
= printf("%s", msg_buf
+ i
);
214 int offset
= get_num_terminal_cols() - chrs
- BT_PROMPT_OK_FAIL_STRLEN
;
218 offset
= get_num_terminal_cols() - BT_PROMPT_OK_FAIL_STRLEN
;
221 for (int i
= 0; i
< offset
; i
++)
224 const char *result_str
= is_terminal
? BT_PROMPT_OK
: BT_PROMPT_OK_NO_COLOR
;
226 result_str
= is_terminal
? BT_PROMPT_FAIL
: BT_PROMPT_FAIL_NO_COLOR
;
228 printf("%s\n", result_str
);
232 * bt_log_overall_result - pretty print of suite case result
234 * @fmt: a description message (could be long, over more lines)
235 * ...: variable argument list
237 * This function is used for pretty printing of test suite case result.
240 bt_log_overall_result(int result
, const char *fmt
, ...)
243 va_start(argptr
, fmt
);
244 bt_log_result(result
, fmt
, argptr
);
249 * bt_log_suite_result - pretty print of suite case result
251 * @fmt: a description message (could be long, over more lines)
252 * ...: variable argument list
254 * This function is used for pretty printing of test suite case result.
257 bt_log_suite_result(int result
, const char *fmt
, ...)
259 if(bt_verbose
>= BT_VERBOSE_SUITE
|| !result
)
262 va_start(argptr
, fmt
);
263 bt_log_result(result
, fmt
, argptr
);
269 * bt_log_suite_case_result - pretty print of suite result
271 * @fmt: a description message (could be long, over more lines)
272 * ...: variable argument list
274 * This function is used for pretty printing of test suite result.
277 bt_log_suite_case_result(int result
, const char *fmt
, ...)
279 if(bt_verbose
>= BT_VERBOSE_SUITE_CASE
)
282 va_start(argptr
, fmt
);
283 bt_log_result(result
, fmt
, argptr
);
289 bt_test_suite_base(int (*fn
)(const void *), const char *id
, const void *fn_arg
, int forked
, int timeout
, const char *dsc
, ...)
293 printf("%28s - ", id
);
308 if (request
&& strcmp(id
, request
))
314 if (bt_verbose
>= BT_VERBOSE_ABSOLUTELY_ALL
)
319 bt_suite_result
= bt_run_test_fn(fn
, fn_arg
, timeout
);
324 bt_syscall(pid
< 0, "fork");
329 _exit(bt_run_test_fn(fn
, fn_arg
, timeout
));
333 int rv
= waitpid(pid
, &s
, 0);
334 bt_syscall(rv
< 0, "waitpid");
339 bt_suite_result
= WEXITSTATUS(s
);
341 else if (WIFSIGNALED(s
))
343 /* Stopped by signal */
346 int sn
= WTERMSIG(s
);
349 bt_log("Timeout expired");
351 else if (sn
== SIGSEGV
)
353 bt_log("Segmentation fault");
356 else if (sn
!= SIGABRT
)
357 bt_log("Signal %d received", sn
);
360 if (WCOREDUMP(s
) && bt_verbose
)
361 bt_log("Core dumped");
364 if (!bt_suite_result
)
367 bt_log_suite_result(bt_suite_result
, NULL
);
370 return bt_suite_result
;
376 if (!list_tests
|| (list_tests
&& !bt_result
))
377 bt_log_overall_result(bt_result
, "");
378 return bt_result
? EXIT_SUCCESS
: EXIT_FAILURE
;
382 * bt_assert_batch__ - test a batch of inputs/outputs tests
383 * @opts: includes all necessary data
385 * Should be called using macro bt_assert_batch().
389 bt_assert_batch__(struct bt_batch
*opts
)
392 for (i
= 0; i
< opts
->ndata
; i
++)
394 int bt_suit_case_result
= opts
->test_fn(opts
->out_buf
, opts
->data
[i
].in
, opts
->data
[i
].out
);
396 if (bt_suit_case_result
== 0)
399 char b
[BT_BUFFER_SIZE
];
400 snprintf(b
, sizeof(b
), "%s(", opts
->test_fn_name
);
402 opts
->in_fmt(b
+strlen(b
), sizeof(b
)-strlen(b
), opts
->data
[i
].in
);
403 sprintf_concat(b
, ") gives ");
404 opts
->out_fmt(b
+strlen(b
), sizeof(b
)-strlen(b
), opts
->out_buf
);
406 if (bt_suit_case_result
== 0)
408 sprintf_concat(b
, ", but expecting is ");
409 opts
->out_fmt(b
+strlen(b
), sizeof(b
)-strlen(b
), opts
->data
[i
].out
);
412 bt_log_suite_case_result(bt_suit_case_result
, "%s", b
);
415 return bt_suite_result
;
419 * bt_fmt_str - formating string into output buffer
420 * @buf: buffer for write
421 * @size: empty size in @buf
422 * @data: null-byte terminated string
424 * This function can be used with bt_assert_batch() function.
425 * Input @data should be const char * string.
428 bt_fmt_str(char *buf
, size_t size
, const void *data
)
430 const byte
*s
= data
;
432 snprintf(buf
, size
, "\"");
435 snprintf(buf
+strlen(buf
), size
-strlen(buf
), bt_is_char(*s
) ? "%c" : "\\%03u", *s
);
438 snprintf(buf
+strlen(buf
), size
-strlen(buf
), "\"");
442 * bt_fmt_unsigned - formating unsigned int into output buffer
443 * @buf: buffer for write
444 * @size: empty size in @buf
445 * @data: unsigned number
447 * This function can be used with bt_assert_batch() function.
450 bt_fmt_unsigned(char *buf
, size_t size
, const void *data
)
452 const uint
*n
= data
;
453 snprintf(buf
, size
, "0x%x (%u)", *n
, *n
);
457 * bt_fmt_ipa - formating ip_addr into output buffer
458 * @buf: buffer for write
459 * @size: empty size in @buf
460 * @data: should be struct ip_addr *
462 * This function can be used with bt_assert_batch() function.
465 bt_fmt_ipa(char *buf
, size_t size
, const void *data
)
467 const ip_addr
*ip
= data
;
468 bsnprintf(buf
, size
, "%I", *ip
);
474 return (c
>= (byte
) 32 && c
<= (byte
) 126);
478 * Mock-ups of all necessary public functions in main.c
482 void async_config(void) {}
483 void async_dump(void) {}
484 void async_shutdown(void) {}
485 void cmd_check_config(char *name UNUSED
) {}
486 void cmd_reconfig(char *name UNUSED
, int type UNUSED
, int timeout UNUSED
) {}
487 void cmd_reconfig_confirm(void) {}
488 void cmd_reconfig_undo(void) {}
489 void cmd_reconfig_status(void) {}
490 void cmd_shutdown(void) {}
491 void cmd_reconfig_undo_notify(void) {}
493 #include "nest/bird.h"
495 #include "conf/conf.h"
496 void sysdep_preconfig(struct config
*c UNUSED
) {}
497 int sysdep_commit(struct config
*new UNUSED
, struct config
*old UNUSED
) { return 0; }
498 void sysdep_shutdown_done(void) {}
500 #include "nest/cli.h"
501 int cli_get_command(cli
*c UNUSED
) { return 0; }
502 void cli_write_trigger(cli
*c UNUSED
) {}
503 cli
*cmd_reconfig_stored_cli
;