2010-01-31 22:05:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Mark Heily <mark@heily.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2012-11-26 02:56:10 +00:00
|
|
|
#if defined(__linux__) && !defined(__ANDROID__)
|
2010-02-06 00:18:15 +00:00
|
|
|
#include <execinfo.h>
|
2010-07-27 01:26:56 +00:00
|
|
|
#endif
|
2010-01-31 22:05:44 +00:00
|
|
|
#include <sys/types.h>
|
2010-02-03 02:52:44 +00:00
|
|
|
#include <limits.h>
|
2010-01-31 22:05:44 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2011-01-17 02:23:12 +00:00
|
|
|
static int testnum = 1;
|
|
|
|
static int error_flag = 1;
|
2010-01-31 22:05:44 +00:00
|
|
|
|
2010-02-06 00:18:15 +00:00
|
|
|
/* FIXME: not portable beyond linux */
|
2011-04-23 18:27:10 +00:00
|
|
|
#ifndef _WIN32
|
2010-02-06 00:18:15 +00:00
|
|
|
static void
|
|
|
|
error_handler(int signum)
|
|
|
|
{
|
2012-11-26 02:56:10 +00:00
|
|
|
#if defined(__linux__) && !defined(__ANDROID__)
|
2010-02-06 00:18:15 +00:00
|
|
|
void *buf[32];
|
|
|
|
|
2010-02-06 01:43:34 +00:00
|
|
|
/* FIXME: the symbols aren't printing */
|
2010-02-06 00:18:15 +00:00
|
|
|
printf("***** ERROR: Program received signal %d *****\n", signum);
|
|
|
|
backtrace_symbols_fd(buf, sizeof(buf) / sizeof(void *), 2);
|
2010-07-27 01:26:56 +00:00
|
|
|
#else
|
|
|
|
printf("***** ERROR: Program received signal %d *****\n", signum);
|
|
|
|
#endif
|
2010-02-06 00:18:15 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2011-04-23 18:27:10 +00:00
|
|
|
#endif /* ! _WIN32 */
|
2010-02-06 00:18:15 +00:00
|
|
|
|
2010-01-31 22:05:44 +00:00
|
|
|
static void
|
|
|
|
testing_atexit(void)
|
|
|
|
{
|
|
|
|
if (error_flag) {
|
2011-05-30 14:59:08 +00:00
|
|
|
printf(" *** TEST FAILED ***\n");
|
2010-01-31 22:05:44 +00:00
|
|
|
//TODO: print detailed log
|
|
|
|
} else {
|
|
|
|
printf("\n---\n"
|
|
|
|
"+OK All %d tests completed.\n", testnum - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-30 14:59:08 +00:00
|
|
|
test_begin(struct test_context *ctx, const char *func)
|
2010-01-31 22:05:44 +00:00
|
|
|
{
|
2011-05-30 14:59:08 +00:00
|
|
|
if (ctx->cur_test_id)
|
|
|
|
free(ctx->cur_test_id);
|
|
|
|
ctx->cur_test_id = strdup(func);
|
2010-01-31 22:05:44 +00:00
|
|
|
|
2011-05-30 14:59:08 +00:00
|
|
|
printf("%d: %s\n", testnum++, ctx->cur_test_id);
|
2010-01-31 22:05:44 +00:00
|
|
|
//TODO: redirect stdout/err to logfile
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-30 14:59:08 +00:00
|
|
|
test_end(struct test_context *ctx)
|
2010-01-31 22:05:44 +00:00
|
|
|
{
|
2011-05-30 14:59:08 +00:00
|
|
|
free(ctx->cur_test_id);
|
|
|
|
ctx->cur_test_id = NULL;
|
2010-01-31 22:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
testing_begin(void)
|
|
|
|
{
|
2011-01-17 02:23:12 +00:00
|
|
|
#ifndef _WIN32
|
2010-02-06 00:18:15 +00:00
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
/* Install a signal handler for crashes and hangs */
|
2010-02-06 01:43:34 +00:00
|
|
|
memset(&sa, 0, sizeof(sa));
|
2010-02-06 00:18:15 +00:00
|
|
|
sa.sa_handler = error_handler;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaction(SIGSEGV, &sa, NULL);
|
|
|
|
sigaction(SIGABRT, &sa, NULL);
|
|
|
|
sigaction(SIGINT, &sa, NULL);
|
2011-01-17 02:23:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
atexit(testing_atexit);
|
|
|
|
|
2010-01-31 22:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
testing_end(void)
|
|
|
|
{
|
|
|
|
error_flag = 0;
|
|
|
|
}
|
2010-02-03 02:52:44 +00:00
|
|
|
|
|
|
|
/* Generate a unique ID */
|
|
|
|
int
|
|
|
|
testing_make_uid(void)
|
|
|
|
{
|
|
|
|
static int id = 0;
|
|
|
|
|
|
|
|
if (id == INT_MAX)
|
|
|
|
abort();
|
|
|
|
id++;
|
2011-01-17 02:23:12 +00:00
|
|
|
|
2010-02-03 02:52:44 +00:00
|
|
|
return (id);
|
|
|
|
}
|