aboutsummaryrefslogtreecommitdiff
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
parente8a1d135e21721426851312dc2a9180e5c38273f (diff)
downloadlibssh-464176d5111676161d809ac3f5aeaa0f9fb2ff5b.tar.gz
libssh-464176d5111676161d809ac3f5aeaa0f9fb2ff5b.tar.xz
libssh-464176d5111676161d809ac3f5aeaa0f9fb2ff5b.zip
Added unit testing support using check.
-rw-r--r--CMakeLists.txt8
-rw-r--r--DefineOptions.cmake1
-rwxr-xr-xbuild/build_make.sh2
-rw-r--r--cmake/Modules/AddCheckTest.cmake22
-rw-r--r--cmake/Modules/FindCheck.cmake90
-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
11 files changed, 317 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d087366d..ac6a3598 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -37,6 +37,7 @@ include(MacroCopyFile)
# search for libraries
find_package(ZLIB REQUIRED)
+find_package(Check)
if (WITH_GCRYPT)
find_package(GCrypt REQUIRED)
@@ -75,8 +76,13 @@ install(
if (UNIX AND NOT WIN32)
add_subdirectory(examples)
-
endif (UNIX AND NOT WIN32)
+
+if (CHECK_FOUND AND WITH_TESTING)
+ include(AddCheckTest)
+ add_subdirectory(tests)
+endif (CHECK_FOUND AND WITH_TESTING)
+
MESSAGE(STATUS "********************************************")
MESSAGE(STATUS "********** ${PROJECT_NAME} build options : **********")
if (WITH_LIBZ)
diff --git a/DefineOptions.cmake b/DefineOptions.cmake
index 21e562ed..ce251e12 100644
--- a/DefineOptions.cmake
+++ b/DefineOptions.cmake
@@ -8,3 +8,4 @@ option(WITH_DEBUG_CALLTRACE "Build with calltrace debug output" ON)
option(WITH_GCRYPT "Compile against libgcrypt" OFF)
option(WITH_PCAP "Compile with Pcap generation support" ON)
option(WITH_INTERNAL_DOC "Compile doxygen internal documentation" OFF)
+option(WITH_TESTING "Build with unit tests" OFF)
diff --git a/build/build_make.sh b/build/build_make.sh
index a51f5e82..1e93c2fc 100755
--- a/build/build_make.sh
+++ b/build/build_make.sh
@@ -135,7 +135,7 @@ while test -n "$1"; do
shift
;;
*-unittesting)
- OPTIONS="${OPTIONS} -DUNIT_TESTING=ON"
+ OPTIONS="${OPTIONS} -DWITH_TESTING=ON"
shift
;;
*-withssh1)
diff --git a/cmake/Modules/AddCheckTest.cmake b/cmake/Modules/AddCheckTest.cmake
new file mode 100644
index 00000000..089a09c2
--- /dev/null
+++ b/cmake/Modules/AddCheckTest.cmake
@@ -0,0 +1,22 @@
+# - ADD_CHECK_TEST(test_name test_source linklib1 ... linklibN)
+
+# Copyright (c) 2007, Daniel Gollub, <dgollub@suse.de>
+# Copyright (c) 2007, Andreas Schneider, <mail@cynapses.org>
+#
+# Redistribution and use is allowed according to the terms of the BSD license.
+# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
+
+enable_testing()
+include(CTest)
+
+set(CMAKE_C_FLAGS_PROFILING "-g -O0 -Wall -W -Wshadow -Wunused-variable -Wunused-parameter -Wunused-function -Wunused -Wno-system-headers -Wwrite-strings -fprofile-arcs -ftest-coverage" CACHE STRING "Profiling Compiler Flags")
+set(CMAKE_SHARED_LINKER_FLAGS_PROFILING " -fprofile-arcs -ftest-coverage" CACHE STRING "Profiling Linker Flags")
+set(CMAKE_MODULE_LINKER_FLAGS_PROFILING " -fprofile-arcs -ftest-coverage" CACHE STRING "Profiling Linker Flags")
+set(CMAKE_EXEC_LINKER_FLAGS_PROFILING " -fprofile-arcs -ftest-coverage" CACHE STRING "Profiling Linker Flags")
+
+function (ADD_CHECK_TEST _testName _testSource)
+ add_executable(${_testName} ${_testSource})
+ target_link_libraries(${_testName} ${ARGN})
+ add_test(${_testName} ${CMAKE_CURRENT_BINARY_DIR}/${_testName})
+endfunction (ADD_CHECK_TEST)
+
diff --git a/cmake/Modules/FindCheck.cmake b/cmake/Modules/FindCheck.cmake
new file mode 100644
index 00000000..35d4431e
--- /dev/null
+++ b/cmake/Modules/FindCheck.cmake
@@ -0,0 +1,90 @@
+# - Try to find Check
+# Once done this will define
+#
+# CHECK_FOUND - system has Check
+# CHECK_INCLUDE_DIRS - the Check include directory
+# CHECK_LIBRARIES - Link these to use Check
+# CHECK_DEFINITIONS - Compiler switches required for using Check
+#
+# Copyright (c) 2010 Andreas Schneider <mail@cynapses.org>
+#
+# Redistribution and use is allowed according to the terms of the New
+# BSD license.
+# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
+#
+
+
+if (CHECK_LIBRARIES AND CHECK_INCLUDE_DIRS)
+ # in cache already
+ set(CHECK_FOUND TRUE)
+else (CHECK_LIBRARIES AND CHECK_INCLUDE_DIRS)
+ # use pkg-config to get the directories and then use these values
+ # in the FIND_PATH() and FIND_LIBRARY() calls
+ if (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
+ include(UsePkgConfig)
+ pkgconfig(check _CHECK_INCLUDEDIR _CHECK_LIBDIR _CHECK_LDFLAGS _CHECK_CFLAGS)
+ else (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
+ find_package(PkgConfig)
+ if (PKG_CONFIG_FOUND)
+ pkg_check_modules(_CHECK check)
+ endif (PKG_CONFIG_FOUND)
+ endif (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
+
+ find_path(CHECK_INCLUDE_DIR
+ NAMES
+ check.h
+ PATHS
+ ${_CHECK_INCLUDEDIR}
+ /usr/include
+ /usr/local/include
+ /opt/local/include
+ /sw/include
+ )
+ mark_as_advanced(CHECK_INCLUDE_DIR)
+
+ find_library(CHECK_LIBRARY
+ NAMES
+ check
+ PATHS
+ ${_CHECK_LIBDIR}
+ /usr/lib
+ /usr/local/lib
+ /opt/local/lib
+ /sw/lib
+ )
+ mark_as_advanced(CHECK_LIBRARY)
+
+ if (CHECK_LIBRARY)
+ set(CHECK_FOUND TRUE CACHE INTERNAL "Wether the check library has been found" FORCE)
+ endif (CHECK_LIBRARY)
+
+ set(CHECK_INCLUDE_DIRS
+ ${CHECK_INCLUDE_DIR}
+ )
+
+ if (CHECK_FOUND)
+ set(CHECK_LIBRARIES
+ ${CHECK_LIBRARIES}
+ ${CHECK_LIBRARY}
+ )
+ endif (CHECK_FOUND)
+
+ if (CHECK_INCLUDE_DIRS AND CHECK_LIBRARIES)
+ set(CHECK_FOUND TRUE)
+ endif (CHECK_INCLUDE_DIRS AND CHECK_LIBRARIES)
+
+ if (CHECK_FOUND)
+ if (NOT Check_FIND_QUIETLY)
+ message(STATUS "Found Check: ${CHECK_LIBRARIES}")
+ endif (NOT Check_FIND_QUIETLY)
+ else (CHECK_FOUND)
+ if (Check_FIND_REQUIRED)
+ message(FATAL_ERROR "Could not find Check")
+ endif (Check_FIND_REQUIRED)
+ endif (CHECK_FOUND)
+
+ # show the CHECK_INCLUDE_DIRS and CHECK_LIBRARIES variables only in the advanced view
+ mark_as_advanced(CHECK_INCLUDE_DIRS CHECK_LIBRARIES)
+
+endif (CHECK_LIBRARIES AND CHECK_INCLUDE_DIRS)
+
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;
+}
+