Added unit testing support using check.

This commit is contained in:
Andreas Schneider
2010-03-02 13:47:14 +01:00
parent e8a1d135e2
commit 464176d511
11 changed files with 317 additions and 2 deletions

19
tests/CMakeLists.txt Normal file
View File

@@ -0,0 +1,19 @@
project(tests C)
set(TORTURE_LIBRARY torture)
include_directories(
${LIBSSH_PUBLIC_INCLUDE_DIRS}
${CHECK_INCLUDE_DIRS}
${CMAKE_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
)
# create test library
add_library(${TORTURE_LIBRARY} SHARED torture.c cmdline.c)
target_link_libraries(${TORTURE_LIBRARY} ${CHECK_LIBRARIES} ${LIBSSH_LIBRARY})
set(TEST_TARGET_LIBRARIES ${SUPPORT_LIBRARY})
add_subdirectory(unittests)

63
tests/cmdline.c Normal file
View File

@@ -0,0 +1,63 @@
#include <argp.h>
#include "torture.h"
const char *argp_program_version = "check test 0.1";
const char *argp_program_bug_address = "<csync-devel@csync.org>";
static char **cmdline;
/* Program documentation. */
static char doc[] = "check test";
/* The options we understand. */
static struct argp_option options[] = {
{
.name = "no-fork",
.key = 'n',
.arg = NULL,
.flags = 0,
.doc = "Don't fork the testcases",
.group = 0
},
{NULL, 0, NULL, 0, NULL, 0}
};
/* Parse a single option. */
static error_t parse_opt (int key, char *arg, struct argp_state *state) {
/* Get the input argument from argp_parse, which we
* know is a pointer to our arguments structure.
*/
struct argument_s *arguments = state->input;
/* arg is currently not used */
(void) arg;
switch (key) {
case 'n':
arguments->nofork = 1;
break;
case ARGP_KEY_ARG:
/* End processing here. */
cmdline = &state->argv [state->next - 1];
state->next = state->argc;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/* Our argp parser. */
/* static struct argp argp = {options, parse_opt, args_doc, doc, NULL, NULL, NULL}; */
static struct argp argp = {options, parse_opt, NULL, doc, NULL, NULL, NULL};
void torture_cmdline_parse(int argc, char **argv, struct argument_s *arguments) {
/*
* Parse our arguments; every option seen by parse_opt will
* be reflected in arguments.
*/
argp_parse(&argp, argc, argv, 0, 0, arguments);
}

26
tests/torture.c Normal file
View File

@@ -0,0 +1,26 @@
#include "torture.h"
#include <stdio.h>
void torture_create_case(Suite *s, const char *name, TFun function) {
TCase *tc_new = tcase_create(name);
tcase_set_timeout(tc_new, 30);
suite_add_tcase (s, tc_new);
tcase_add_test(tc_new, function);
}
void torture_create_case_fixture(Suite *s, const char *name, TFun function, void (*setup)(void), void (*teardown)(void)) {
TCase *tc_new = tcase_create(name);
tcase_add_checked_fixture(tc_new, setup, teardown);
tcase_set_timeout(tc_new, 30);
suite_add_tcase (s, tc_new);
tcase_add_test(tc_new, function);
}
void torture_create_case_timeout(Suite *s, const char *name, TFun function, int timeout) {
TCase *tc_new = tcase_create(name);
tcase_set_timeout(tc_new, timeout);
suite_add_tcase (s, tc_new);
tcase_add_test(tc_new, function);
}

34
tests/torture.h Normal file
View File

@@ -0,0 +1,34 @@
#ifndef _TORTURE_H
#define _TORTURE_H
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <check.h>
/* Used by main to communicate with parse_opt. */
struct argument_s {
char *args[2];
int nofork;
};
void torture_cmdline_parse(int argc, char **argv, struct argument_s *arguments);
/* create_case() with timeout of 30seconds (default) */
void torture_create_case(Suite *s, const char *name, TFun function);
/* create_case() with timeout of 30seconds (default) and fixture */
void torture_create_case_fixture(Suite *s, const char *name, TFun function,
void (*setup)(void), void (*teardown)(void));
/*
* create_case_timeout() allow to specific a specific timeout - intended for
* breaking testcases which needs longer then 30seconds (default)
*/
void torture_create_case_timeout(Suite *s, const char *name, TFun function,
int timeout);
#endif /* _TORTURE_H */

View File

@@ -0,0 +1,3 @@
project(unittests C)
add_check_test(torture_misc torture_misc.c ${TORTURE_LIBRARY})

View File

@@ -0,0 +1,51 @@
#include <sys/types.h>
#include <pwd.h>
#include <libssh/priv.h>
#include "torture.h"
#include "misc.c"
START_TEST (torture_get_user_home_dir)
{
struct passwd *pwd;
char *user;
pwd = getpwuid(getuid());
user = ssh_get_user_home_dir();
ck_assert_str_eq(user, pwd->pw_dir);
}
END_TEST
static Suite *torture_make_suite(void) {
Suite *s = suite_create("libssh_misc");
torture_create_case(s, "torture_get_user_home_dir", torture_get_user_home_dir);
return s;
}
int main(int argc, char **argv) {
Suite *s = NULL;
SRunner *sr = NULL;
struct argument_s arguments;
int nf;
ZERO_STRUCT(arguments);
torture_cmdline_parse(argc, argv, &arguments);
s = torture_make_suite();
sr = srunner_create(s);
if (arguments.nofork) {
srunner_set_fork_status(sr, CK_NOFORK);
}
srunner_run_all(sr, CK_VERBOSE);
nf = srunner_ntests_failed(sr);
srunner_free(sr);
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}