2015-03-03 12:51:35 +00:00
|
|
|
/*
|
2015-03-19 17:38:38 +00:00
|
|
|
* BIRD -- Unit Test Framework (BIRD Test)
|
2015-03-03 12:51:35 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2015-03-23 16:40:13 +00:00
|
|
|
#include <signal.h>
|
2015-03-03 12:51:35 +00:00
|
|
|
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2015-03-23 16:40:13 +00:00
|
|
|
#include "test/birdtest.h"
|
2015-03-03 12:51:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
static const char *request;
|
|
|
|
static int list_tests;
|
|
|
|
static int do_core;
|
|
|
|
static int no_fork;
|
|
|
|
static int no_timeout;
|
|
|
|
|
|
|
|
int bt_verbose;
|
|
|
|
const char *bt_filename;
|
|
|
|
const char *bt_test_id;
|
|
|
|
|
|
|
|
void
|
|
|
|
bt_init(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
2015-03-27 13:03:47 +00:00
|
|
|
srandom(BT_RANDOM_SEED);
|
|
|
|
|
2015-03-13 17:27:33 +00:00
|
|
|
bt_verbose = 0;
|
2015-03-03 12:51:35 +00:00
|
|
|
bt_filename = argv[0];
|
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "lcftv")) >= 0)
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'l':
|
2015-03-13 17:29:21 +00:00
|
|
|
printf("\n"
|
|
|
|
" List of test cases \n"
|
|
|
|
"------------------------------\n");
|
2015-03-03 12:51:35 +00:00
|
|
|
list_tests = 1;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
do_core = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
no_fork = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
no_timeout = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
2015-03-13 17:27:33 +00:00
|
|
|
bt_verbose++;
|
2015-03-03 12:51:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Optional requested test_id */
|
|
|
|
if ((optind + 1) == argc)
|
|
|
|
request = argv[optind++];
|
|
|
|
|
|
|
|
if (optind != argc)
|
|
|
|
goto usage;
|
|
|
|
|
|
|
|
|
|
|
|
if (do_core)
|
|
|
|
{
|
|
|
|
struct rlimit rl = {RLIM_INFINITY, RLIM_INFINITY};
|
|
|
|
int rv = setrlimit(RLIMIT_CORE, &rl);
|
|
|
|
bt_syscall(rv < 0, "setrlimit RLIMIT_CORE");
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
usage:
|
2015-03-13 17:27:33 +00:00
|
|
|
printf("Usage: %s [-l] [-c] [-f] [-t] [-vv] [<test_id>]\n", argv[0]);
|
2015-03-03 12:51:35 +00:00
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-17 10:27:40 +00:00
|
|
|
bt_test_case5(int (*test_fn)(void), const char *test_id, const char *dsc, int forked, int timeout)
|
2015-03-03 12:51:35 +00:00
|
|
|
{
|
|
|
|
if (list_tests)
|
|
|
|
{
|
2015-03-13 17:29:21 +00:00
|
|
|
printf("%28s : %s\n", test_id, dsc);
|
2015-03-03 12:51:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (no_fork)
|
|
|
|
forked = 0;
|
|
|
|
|
|
|
|
if (no_timeout)
|
|
|
|
timeout = 0;
|
|
|
|
|
|
|
|
if (request && strcmp(test_id, request))
|
|
|
|
return;
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
bt_test_id = test_id;
|
|
|
|
|
2015-03-13 17:29:21 +00:00
|
|
|
bt_note("Starting %s: %s", test_id, dsc);
|
2015-03-03 12:51:35 +00:00
|
|
|
|
|
|
|
if (!forked)
|
|
|
|
{
|
|
|
|
alarm(timeout);
|
|
|
|
result = test_fn();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pid_t pid = fork();
|
|
|
|
bt_syscall(pid < 0, "fork");
|
|
|
|
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
alarm(timeout);
|
|
|
|
result = test_fn();
|
|
|
|
_exit(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
int s;
|
|
|
|
int rv = waitpid(pid, &s, 0);
|
|
|
|
bt_syscall(rv < 0, "waitpid");
|
|
|
|
|
|
|
|
result = 2;
|
|
|
|
if (WIFEXITED(s))
|
|
|
|
result = WEXITSTATUS(s);
|
|
|
|
else if (WIFSIGNALED(s))
|
|
|
|
{
|
|
|
|
int sn = WTERMSIG(s);
|
|
|
|
if (sn == SIGALRM)
|
|
|
|
bt_log("Timeout expired");
|
|
|
|
else if (sn == SIGSEGV)
|
|
|
|
bt_log("Segmentation fault");
|
|
|
|
else if (sn != SIGABRT)
|
|
|
|
bt_log("Signal %d received", sn);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WCOREDUMP(s))
|
|
|
|
bt_log("Core dumped");
|
|
|
|
}
|
|
|
|
|
2015-03-13 17:29:21 +00:00
|
|
|
if (result != BT_SUCCESS)
|
2015-03-03 12:51:35 +00:00
|
|
|
{
|
|
|
|
bt_log("Test case failed");
|
|
|
|
exit(result);
|
|
|
|
}
|
|
|
|
|
2015-03-13 17:29:21 +00:00
|
|
|
bt_note("OK");
|
2015-03-03 12:51:35 +00:00
|
|
|
}
|
2015-03-27 13:03:47 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
bt_rand_num(void)
|
|
|
|
{
|
|
|
|
return random();
|
|
|
|
}
|