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

View File

@ -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)

View File

@ -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)

View File

@ -135,7 +135,7 @@ while test -n "$1"; do
shift
;;
*-unittesting)
OPTIONS="${OPTIONS} -DUNIT_TESTING=ON"
OPTIONS="${OPTIONS} -DWITH_TESTING=ON"
shift
;;
*-withssh1)

View File

@ -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)

View File

@ -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)

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;
}