#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <apr_pools.h>
#include <apr_general.h>
#include <apr_signal.h>
#include <apr_env.h>
#include "svn_cmdline.h"
#include "svn_opt.h"
#include "svn_pools.h"
#include "svn_error.h"
#include "svn_test.h"
#include "svn_io.h"
#include "svn_path.h"
#include "svn_ctype.h"
#include "svn_private_config.h"
int test_argc;
const char **test_argv;
static svn_boolean_t verbose_mode = FALSE;
static svn_boolean_t trap_assertion_failures = FALSE;
static svn_boolean_t quiet_mode = FALSE;
static svn_boolean_t cleanup_mode = FALSE;
static svn_boolean_t allow_segfaults = FALSE;
enum svn_test_mode_t mode_filter = svn_test_all;
enum {
cleanup_opt = SVN_OPT_FIRST_LONGOPT_ID,
fstype_opt,
list_opt,
verbose_opt,
trap_assert_opt,
quiet_opt,
config_opt,
server_minor_version_opt,
allow_segfault_opt,
srcdir_opt,
mode_filter_opt
};
static const apr_getopt_option_t cl_options[] =
{
{"cleanup", cleanup_opt, 0,
N_("remove test directories after success")},
{"config-file", config_opt, 1,
N_("specify test config file ARG")},
{"fs-type", fstype_opt, 1,
N_("specify a filesystem backend type ARG")},
{"list", list_opt, 0,
N_("lists all the tests with their short description")},
{"mode-filter", mode_filter_opt, 1,
N_("only run/list tests with expected mode ARG = PASS, "
"XFAIL, SKIP, or ALL (default)\n")},
{"verbose", verbose_opt, 0,
N_("print extra information")},
{"server-minor-version", server_minor_version_opt, 1,
N_("set the minor version for the server ('3', '4',\n"
"'5', or '6')")},
{"trap-assertion-failures", trap_assert_opt, 0,
N_("catch and report SVN_ERR_ASSERT failures")},
{"quiet", quiet_opt, 0,
N_("print only unexpected results")},
{"allow-segfaults", allow_segfault_opt, 0,
N_("don't trap seg faults (useful for debugging)")},
{"srcdir", srcdir_opt, 1,
N_("source directory")},
{0, 0, 0, 0}
};
static svn_boolean_t skip_cleanup = FALSE;
static apr_pool_t *cleanup_pool = NULL;
static apr_status_t
cleanup_rmtree(void *data)
{
if (!skip_cleanup)
{
apr_pool_t *pool = svn_pool_create(NULL);
const char *path = data;
svn_error_t *err = svn_io_remove_dir2(path, FALSE, NULL, NULL, pool);
svn_error_clear(err);
if (verbose_mode)
{
if (err)
printf("FAILED CLEANUP: %s\n", path);
else
printf("CLEANUP: %s\n", path);
}
svn_pool_destroy(pool);
}
return APR_SUCCESS;
}
void
svn_test_add_dir_cleanup(const char *path)
{
if (cleanup_mode)
{
const char *abspath;
svn_error_t *err = svn_path_get_absolute(&abspath, path, cleanup_pool);
svn_error_clear(err);
if (!err)
apr_pool_cleanup_register(cleanup_pool, abspath, cleanup_rmtree,
apr_pool_cleanup_null);
else if (verbose_mode)
printf("FAILED ABSPATH: %s\n", path);
}
}
apr_uint32_t
svn_test_rand(apr_uint32_t *seed)
{
*seed = (*seed * 1103515245UL + 12345UL) & 0xffffffffUL;
return *seed;
}
static int
get_array_size(void)
{
int i;
for (i = 1; test_funcs[i].func2 || test_funcs[i].func_opts; i++)
{
}
return (i - 1);
}
static jmp_buf jump_buffer;
static void
crash_handler(int signum)
{
longjmp(jump_buffer, 1);
}
static svn_boolean_t
do_test_num(const char *progname,
int test_num,
svn_boolean_t msg_only,
svn_test_opts_t *opts,
const char **header_msg,
apr_pool_t *pool)
{
svn_boolean_t skip, xfail, wimp;
svn_error_t *err = NULL;
svn_boolean_t test_failed;
const char *msg = NULL;
const struct svn_test_descriptor_t *desc;
const int array_size = get_array_size();
svn_boolean_t run_this_test;
if (test_num < 0)
test_num += array_size + 1;
if ((test_num > array_size) || (test_num <= 0))
{
if (header_msg && *header_msg)
printf("%s", *header_msg);
printf("FAIL: %s: THERE IS NO TEST NUMBER %2d\n", progname, test_num);
skip_cleanup = TRUE;
return TRUE;
}
desc = &test_funcs[test_num];
skip = desc->mode == svn_test_skip;
xfail = desc->mode == svn_test_xfail;
wimp = xfail && desc->wip;
msg = desc->msg;
run_this_test = mode_filter == svn_test_all || mode_filter == desc->mode;
if (run_this_test && header_msg && *header_msg)
{
printf("%s", *header_msg);
*header_msg = NULL;
}
if (!allow_segfaults)
{
apr_signal(SIGSEGV, crash_handler);
}
if (setjmp(jump_buffer) == 0)
{
if (msg_only || skip || !run_this_test)
;
else if (desc->func2)
err = (*desc->func2)(pool);
else
err = (*desc->func_opts)(opts, pool);
if (err && err->apr_err == SVN_ERR_TEST_SKIPPED)
{
svn_error_clear(err);
err = SVN_NO_ERROR;
skip = TRUE;
}
}
else
err = svn_error_create(SVN_ERR_TEST_FAILED, NULL,
"Test crashed "
"(run in debugger with '--allow-segfaults')");
if (!allow_segfaults)
{
apr_signal(SIGSEGV, SIG_DFL);
}
test_failed = (!wimp && ((err != SVN_NO_ERROR) != (xfail != 0)));
if (err)
{
svn_handle_error2(err, stdout, FALSE, "svn_tests: ");
svn_error_clear(err);
}
if (msg_only)
{
if (run_this_test)
printf(" %3d %-5s %s%s%s%s\n",
test_num,
(xfail ? "XFAIL" : (skip ? "SKIP" : "")),
msg ? msg : "(test did not provide name)",
(wimp && verbose_mode) ? " [[" : "",
(wimp && verbose_mode) ? desc->wip : "",
(wimp && verbose_mode) ? "]]" : "");
}
else if (run_this_test && ((! quiet_mode) || test_failed))
{
printf("%s %s %d: %s%s%s%s\n",
(err
? (xfail ? "XFAIL:" : "FAIL: ")
: (xfail ? "XPASS:" : (skip ? "SKIP: " : "PASS: "))),
progname,
test_num,
msg ? msg : "(test did not provide name)",
wimp ? " [[WIMP: " : "",
wimp ? desc->wip : "",
wimp ? "]]" : "");
}
if (msg)
{
size_t len = strlen(msg);
if (len > 50)
printf("WARNING: Test docstring exceeds 50 characters\n");
if (msg[len - 1] == '.')
printf("WARNING: Test docstring ends in a period (.)\n");
if (svn_ctype_isupper(msg[0]))
printf("WARNING: Test docstring is capitalized\n");
}
if (desc->msg == NULL)
printf("WARNING: New-style test descriptor is missing a docstring.\n");
fflush(stdout);
skip_cleanup = test_failed;
return test_failed;
}
int
main(int argc, const char *argv[])
{
const char *prog_name;
int i;
svn_boolean_t got_error = FALSE;
apr_allocator_t *allocator;
apr_pool_t *pool, *test_pool;
svn_boolean_t ran_a_test = FALSE;
svn_boolean_t list_mode = FALSE;
int opt_id;
apr_status_t apr_err;
apr_getopt_t *os;
svn_error_t *err;
char errmsg[200];
int array_size = get_array_size();
svn_test_opts_t opts = { NULL };
opts.fs_type = DEFAULT_FS_TYPE;
if (apr_initialize() != APR_SUCCESS)
{
printf("apr_initialize() failed.\n");
exit(1);
}
if (apr_allocator_create(&allocator))
return EXIT_FAILURE;
apr_allocator_max_free_set(allocator, SVN_ALLOCATOR_RECOMMENDED_MAX_FREE);
pool = svn_pool_create_ex(NULL, allocator);
apr_allocator_owner_set(allocator, pool);
test_argc = argc;
test_argv = argv;
err = svn_cmdline__getopt_init(&os, argc, argv, pool);
os->interleave = TRUE;
prog_name = strrchr(argv[0], '/');
if (prog_name)
prog_name++;
else
{
prog_name = strrchr(argv[0], '\\');
if (prog_name)
prog_name++;
else
prog_name = argv[0];
}
if (err)
return svn_cmdline_handle_exit_error(err, pool, prog_name);
while (1)
{
const char *opt_arg;
apr_err = apr_getopt_long(os, cl_options, &opt_id, &opt_arg);
if (APR_STATUS_IS_EOF(apr_err))
break;
else if (apr_err && (apr_err != APR_BADCH))
{
fprintf(stderr, "apr_getopt_long failed : [%d] %s\n",
apr_err, apr_strerror(apr_err, errmsg, sizeof(errmsg)));
exit(1);
}
switch (opt_id) {
case cleanup_opt:
cleanup_mode = TRUE;
break;
case config_opt:
opts.config_file = apr_pstrdup(pool, opt_arg);
break;
case fstype_opt:
opts.fs_type = apr_pstrdup(pool, opt_arg);
break;
case list_opt:
list_mode = TRUE;
break;
case mode_filter_opt:
if (svn_cstring_casecmp(opt_arg, "PASS") == 0)
mode_filter = svn_test_pass;
else if (svn_cstring_casecmp(opt_arg, "XFAIL") == 0)
mode_filter = svn_test_xfail;
else if (svn_cstring_casecmp(opt_arg, "SKIP") == 0)
mode_filter = svn_test_skip;
else if (svn_cstring_casecmp(opt_arg, "ALL") == 0)
mode_filter = svn_test_all;
else
{
fprintf(stderr, "FAIL: Invalid --mode-filter option. Try ");
fprintf(stderr, " PASS, XFAIL, SKIP or ALL.\n");
exit(1);
}
break;
case verbose_opt:
verbose_mode = TRUE;
break;
case trap_assert_opt:
trap_assertion_failures = TRUE;
break;
case quiet_opt:
quiet_mode = TRUE;
break;
case allow_segfault_opt:
allow_segfaults = TRUE;
break;
case server_minor_version_opt:
{
char *end;
opts.server_minor_version = strtol(opt_arg, &end, 10);
if (end == opt_arg || *end != '\0')
{
fprintf(stderr, "FAIL: Non-numeric minor version given\n");
exit(1);
}
if ((opts.server_minor_version < 3)
|| (opts.server_minor_version > 6))
{
fprintf(stderr, "FAIL: Invalid minor version given\n");
exit(1);
}
}
}
}
apr_env_set(
"SVN_I_LOVE_CORRUPTED_WORKING_COPIES_SO_DISABLE_SLEEP_FOR_TIMESTAMPS",
"yes", pool);
if (quiet_mode && verbose_mode)
{
fprintf(stderr, "FAIL: --verbose and --quiet are mutually exclusive\n");
exit(1);
}
cleanup_pool = svn_pool_create(pool);
test_pool = svn_pool_create(pool);
if (trap_assertion_failures)
svn_error_set_malfunction_handler(svn_error_raise_on_malfunction);
if (argc >= 2)
{
if (! strcmp(argv[1], "list") || list_mode)
{
const char *header_msg;
ran_a_test = TRUE;
header_msg = "Test # Mode Test Description\n"
"------ ----- ----------------\n";
for (i = 1; i <= array_size; i++)
{
if (do_test_num(prog_name, i, TRUE, &opts, &header_msg,
test_pool))
got_error = TRUE;
svn_pool_clear(test_pool);
svn_pool_clear(cleanup_pool);
}
}
else
{
for (i = 1; i < argc; i++)
{
if (svn_ctype_isdigit(argv[i][0]) || argv[i][0] == '-')
{
int test_num = atoi(argv[i]);
if (test_num == 0)
continue;
ran_a_test = TRUE;
if (do_test_num(prog_name, test_num, FALSE, &opts, NULL,
test_pool))
got_error = TRUE;
svn_pool_clear(test_pool);
svn_pool_clear(cleanup_pool);
}
}
}
}
if (! ran_a_test)
{
for (i = 1; i <= array_size; i++)
{
if (do_test_num(prog_name, i, FALSE, &opts, NULL, test_pool))
got_error = TRUE;
svn_pool_clear(test_pool);
svn_pool_clear(cleanup_pool);
}
}
svn_pool_destroy(pool);
apr_terminate();
return got_error;
}