aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAndreas Schneider <mail@cynapses.org>2010-03-02 13:47:14 +0100
committerAndreas Schneider <mail@cynapses.org>2010-03-02 13:54:14 +0100
commit464176d5111676161d809ac3f5aeaa0f9fb2ff5b (patch)
treef9ca2912faa6b68e23603438fc5f13ad19702789 /tests
parente8a1d135e21721426851312dc2a9180e5c38273f (diff)
downloadlibssh-464176d5111676161d809ac3f5aeaa0f9fb2ff5b.tar.gz
libssh-464176d5111676161d809ac3f5aeaa0f9fb2ff5b.tar.xz
libssh-464176d5111676161d809ac3f5aeaa0f9fb2ff5b.zip
Added unit testing support using check.
Diffstat (limited to 'tests')
-rw-r--r--tests/CMakeLists.txt19
-rw-r--r--tests/cmdline.c63
-rw-r--r--tests/torture.c26
-rw-r--r--tests/torture.h34
-rw-r--r--tests/unittests/CMakeLists.txt3
-rw-r--r--tests/unittests/torture_misc.c51
6 files changed, 196 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644
index 00000000..3acee2ea
--- /dev/null
+++ b/tests/CMakeLists.txt
@@ -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)
diff --git a/tests/cmdline.c b/tests/cmdline.c
new file mode 100644
index 00000000..0f3b7fc3
--- /dev/null
+++ b/tests/cmdline.c
@@ -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);
+}
+
diff --git a/tests/torture.c b/tests/torture.c
new file mode 100644
index 00000000..6fa4c459
--- /dev/null
+++ b/tests/torture.c
@@ -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);
+}
+
diff --git a/tests/torture.h b/tests/torture.h
new file mode 100644
index 00000000..542b2757
--- /dev/null
+++ b/tests/torture.h
@@ -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 */
diff --git a/tests/unittests/CMakeLists.txt b/tests/unittests/CMakeLists.txt
new file mode 100644
index 00000000..066e8c55
--- /dev/null
+++ b/tests/unittests/CMakeLists.txt
@@ -0,0 +1,3 @@
+project(unittests C)
+
+add_check_test(torture_misc torture_misc.c ${TORTURE_LIBRARY})
diff --git a/tests/unittests/torture_misc.c b/tests/unittests/torture_misc.c
new file mode 100644
index 00000000..54fc7c30
--- /dev/null
+++ b/tests/unittests/torture_misc.c
@@ -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;
+}
+