Both the fwknop client and server have their own test suites tied to fwknop_utests

and fwknopd_utests binaries.

When profil coverage is enbaled, lcov filee are parsed by test-fwknop.pl and added
to the main profil coverage report in the output directory.

Running make from the main directory build the c-unit test suites if enabled.
This commit is contained in:
Franck Joncourt
2014-09-07 15:15:22 +02:00
parent 934d764159
commit 688f08c2a0
20 changed files with 250 additions and 137 deletions
+5
View File
@@ -8,11 +8,16 @@ if WANT_SERVER
SERVER_DIR = server
endif
if WANT_C_UNIT_TESTS
C_UNIT_TESTS_DIR = test/c-unit-tests
endif
SUBDIRS = \
lib \
common \
$(CLIENT_DIR) \
$(SERVER_DIR) \
$(C_UNIT_TESTS_DIR) \
doc
EXTRA_DIST = \
+33 -30
View File
@@ -53,10 +53,9 @@
#define LF_CHAR 0x0A /*!< Hexadecimal value associated to the LF char */
#ifdef HAVE_C_UNIT_TESTS
#define TEST_SUITE_DESCRIPTION "Config init module"
DECLARE_TEST_SUITE(config_init, "Config init test suite");
#endif
/**
* Structure to handle long bitmask.
*
@@ -2556,42 +2555,46 @@ static int ut_clean_test_suite(void)
return 0;
}
DECLARE_UTEST(test1, "Description de ut1 dans config_init.c")
DECLARE_UTEST(critical_var, "Check critcial vars")
{
CU_ASSERT(var_is_critical(0) == 0);
CU_ASSERT(var_is_critical(FWKNOP_CLI_ARG_KEY_RIJNDAEL) == 1);
CU_ASSERT(var_is_critical(FWKNOP_CLI_ARG_WGET_CMD) == 0);
}
DECLARE_UTEST(test2, "Description de ut2 dans config_init.c")
DECLARE_UTEST(check_var_bitmask, "Check var_bitmask functions")
{
CU_ASSERT(0);
fko_var_bitmask_t var_bitmask;
memset(&var_bitmask, 0x00, sizeof(fko_var_bitmask_t));
add_var_to_bitmask(FWKNOP_CLI_FIRST_ARG, &var_bitmask);
CU_ASSERT(bitmask_has_var(FWKNOP_CLI_FIRST_ARG, &var_bitmask) == 1);
CU_ASSERT(var_bitmask.dw[0] == 1);
remove_var_from_bitmask(FWKNOP_CLI_FIRST_ARG, &var_bitmask);
CU_ASSERT(bitmask_has_var(FWKNOP_CLI_FIRST_ARG, &var_bitmask) == 0);
CU_ASSERT(var_bitmask.dw[0] == 0);
add_var_to_bitmask(FWKNOP_CLI_ARG_KEY_RIJNDAEL, &var_bitmask);
CU_ASSERT(bitmask_has_var(FWKNOP_CLI_ARG_KEY_RIJNDAEL, &var_bitmask) == 1);
remove_var_from_bitmask(FWKNOP_CLI_ARG_KEY_RIJNDAEL, &var_bitmask);
CU_ASSERT(bitmask_has_var(FWKNOP_CLI_ARG_KEY_RIJNDAEL, &var_bitmask) == 0);
add_var_to_bitmask(FWKNOP_CLI_LAST_ARG, &var_bitmask);
CU_ASSERT(bitmask_has_var(FWKNOP_CLI_LAST_ARG, &var_bitmask) == 1);
remove_var_from_bitmask(FWKNOP_CLI_LAST_ARG, &var_bitmask);
CU_ASSERT(bitmask_has_var(FWKNOP_CLI_LAST_ARG, &var_bitmask) == 0);
add_var_to_bitmask(FWKNOP_CLI_LAST_ARG+32, &var_bitmask);
CU_ASSERT(bitmask_has_var(FWKNOP_CLI_LAST_ARG+32, &var_bitmask) == 0);
}
DECLARE_UTEST(test3, "Description de ut3 dans config_init.c")
{ CU_ASSERT(1);
}
/* TODO add a generic function to go through a tab which contains all utests */
int register_ts_config_init(void)
{
CU_pSuite pSuite = NULL;
/* add a suite to the registry */
pSuite = CU_add_suite(TEST_SUITE_DESCRIPTION, ut_init_test_suite, ut_clean_test_suite);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
/* add the tests to the suite */
if ((NULL == CU_add_test(pSuite, UTEST_DESCR(test1), UTEST_FCT(test1))) ||
(NULL == CU_add_test(pSuite, UTEST_DESCR(test2), UTEST_FCT(test2))) ||
(NULL == CU_add_test(pSuite, UTEST_DESCR(test3), UTEST_FCT(test3))))
{
CU_cleanup_registry();
return CU_get_error();
}
return 0;
ts_init(&TEST_SUITE(config_init), TEST_SUITE_DESCR(config_init));
ts_add_utest(&TEST_SUITE(config_init), UTEST_FCT(critical_var), UTEST_DESCR(critical_var));
ts_add_utest(&TEST_SUITE(config_init), UTEST_FCT(check_var_bitmask), UTEST_DESCR(check_var_bitmask));
return register_ts(&TEST_SUITE(config_init));
}
#endif /* HAVE_C_UNIT_TESTS */
Regular → Executable
-52
View File
@@ -52,10 +52,6 @@
#define ARRAY_FIRST_ELT_ADR(t) &((t)[0]) /*!< Macro to get the first element of an array */
#define ARRAY_LAST_ELT_ADR(t) &((t)[sizeof(t)-1]) /*!< Macro to get the last element of an array */
#ifdef HAVE_C_UNIT_TESTS
#define TEST_SUITE_DESCRIPTION "Get password module"
#endif
/**
* @brief Read a password from a stream object
*
@@ -297,52 +293,4 @@ get_key_file(char *key, int *key_len, const char *key_file,
return 1;
}
#ifdef HAVE_C_UNIT_TESTS
static int ut_init_test_suite(void)
{
return 0;
}
static int ut_clean_test_suite(void)
{
return 0;
}
DECLARE_UTEST(test1, "Description de ut1 dans getpassword.c")
{
CU_ASSERT(1 == 0);
}
DECLARE_UTEST(test2, "Description de ut2 dans getpassword.c")
{
CU_ASSERT(0);
}
/* TODO add a generic function to go through a tab which contains all utests */
int register_ts_getpassword(void)
{
CU_pSuite pSuite = NULL;
/* add a suite to the registry */
pSuite = CU_add_suite(TEST_SUITE_DESCRIPTION, ut_init_test_suite, ut_clean_test_suite);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
/* add the tests to the suite */
if ((NULL == CU_add_test(pSuite, UTEST_DESCR(test1), UTEST_FCT(test1))) ||
(NULL == CU_add_test(pSuite, UTEST_DESCR(test2), UTEST_FCT(test2))))
{
CU_cleanup_registry();
return CU_get_error();
}
return 0;
}
#endif /* HAVE_C_UNIT_TESTS */
/***EOF***/
Regular → Executable
-4
View File
@@ -40,8 +40,4 @@ char* getpasswd(const char *prompt, int fd);
int get_key_file(char *key, int *key_len, const char *key_file,
fko_ctx_t ctx, const fko_cli_options_t *options);
#ifdef HAVE_C_UNIT_TESTS
int register_ts_getpassword(void);
#endif
#endif /* GETPASSWD_H */
Regular → Executable
+1 -1
View File
@@ -2,7 +2,7 @@ AUTOMAKE_OPTIONS = subdir-objects
noinst_LIBRARIES = libfko_util.a
libfko_util_source_files = ../lib/strlcpy.c ../lib/strlcat.c ../lib/fko_util.c ../lib/fko_util.h
libfko_util_source_files = ../lib/cunit_common.c cunit_common.h ../lib/strlcpy.c ../lib/strlcat.c ../lib/fko_util.c ../lib/fko_util.h
libfko_util_a_SOURCES = $(libfko_util_source_files)
Regular → Executable
+1 -10
View File
@@ -45,6 +45,7 @@
/* Include cunit header if c unit testing support is enabled. */
#ifdef HAVE_C_UNIT_TESTS
#include "CUnit/Basic.h"
#include "cunit_common.h"
#endif
#include <stdio.h>
@@ -184,16 +185,6 @@ enum {
#define strnlen(s, l) (strlen(s) < l ? strlen(s) : l)
#endif
/* Add some constant helpers for c unit support */
#ifdef HAVE_C_UNIT_TESTS
#define UTEST_DESCR(name) descr_##name
#define UTEST_FCT(name) ut_##name
#define DECLARE_UTEST(name, description) static const char descr_##name[] = description; \
static void ut_##name(void)
#endif
#endif /* _COMMON_H */
/***EOF***/
+31
View File
@@ -0,0 +1,31 @@
#ifndef CUNIT_COMMON_H
#define CUNIT_COMMON_H
typedef struct c_unit_test
{
char description[128];
void (*func)(void);
} c_unit_test_t;
typedef struct c_unit_test_suite
{
char description[128];
c_unit_test_t test_array[12];
int nb_c_unit_test;
} c_unit_test_suite_t;
#define UTEST_DESCR(name) ut_descr_##name
#define UTEST_FCT(name) ut_##name
#define TEST_SUITE(name) ts_##name
#define TEST_SUITE_DESCR(name) ts_descr_##name
#define DECLARE_TEST_SUITE(name, description) static const char ts_descr_##name[] = description; \
static c_unit_test_suite_t ts_##name;
#define DECLARE_UTEST(name, description) static const char ut_descr_##name[] = description; \
static void ut_##name(void)
void ts_init(c_unit_test_suite_t* ts, const char* description);
void ts_add_utest(c_unit_test_suite_t* ts, void (*utest_func)(void), const char* utest_description);
int register_ts(c_unit_test_suite_t *ts);
#endif // CUNIT_COMMON_H
+3 -1
View File
@@ -163,10 +163,12 @@ AC_ARG_ENABLE([c-unit-tests],
if test "x$want_c_unit_tests" = "xyes"; then
AC_DEFINE([HAVE_C_UNIT_TESTS], [1], [Define for C unit testing support])
FKO_CHECK_COMPILER_ARG([-DHAVE_C_UNIT_TESTS])
FKO_CHECK_COMPILER_ARG([-DHAVE_C_UNIT_TESTS])
FKO_CHECK_COMPILER_ARG_LDFLAGS_ONLY([-lcunit])
fi
AM_CONDITIONAL([WANT_C_UNIT_TESTS], [test "$want_c_unit_tests" = yes])
dnl Decide whether or not to compile in certain features that enable fuzzing
dnl of fwknop code - this is for testing purposes only.
dnl
Regular → Executable
+2 -1
View File
@@ -8,7 +8,8 @@ libfko_source_files = \
fko.h fko_limits.h fko_timestamp.c fko_hmac.c hmac.c hmac.h \
fko_user.c fko_user.h md5.c md5.h rijndael.c rijndael.h sha1.c \
sha1.h sha2.c sha2.h fko_context.h fko_state.h fko_context.h \
gpgme_funcs.c gpgme_funcs.h strlcpy.c strlcat.c fko_util.c fko_util.h
gpgme_funcs.c gpgme_funcs.h strlcpy.c strlcat.c fko_util.c fko_util.h \
cunit_common.c cunit_common.h
libfko_la_SOURCES = $(libfko_source_files)
libfko_la_LDFLAGS = -version-info 2:3:0 $(GPGME_LIBS) -export-symbols-regex '^fko_'
+45
View File
@@ -0,0 +1,45 @@
#include "cunit_common.h"
#include "stdlib.h"
#include "stdio.h"
#include "CUnit/Basic.h"
void ts_init(c_unit_test_suite_t* ts, const char* description)
{
memset(ts, 0x00, sizeof(c_unit_test_suite_t));
strcpy(ts->description, description);
}
void ts_add_utest(c_unit_test_suite_t* ts, void (*utest_func)(void), const char* utest_description)
{
c_unit_test_t* utest = &(ts->test_array[ts->nb_c_unit_test]);
utest->func = utest_func;
strcpy(utest->description, utest_description);
(ts->nb_c_unit_test)++;
}
int register_ts(c_unit_test_suite_t *ts)
{
CU_pSuite pSuite = NULL;
int ix_utest;
pSuite = CU_add_suite(ts->description, NULL, NULL);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
/* add the tests to the suite */
for (ix_utest=0 ; ix_utest<ts->nb_c_unit_test ; ix_utest++)
{
c_unit_test_t* utest = &(ts->test_array[ix_utest]);
if (NULL == CU_add_test(pSuite, utest->description, utest->func))
{
CU_cleanup_registry();
return CU_get_error();
}
}
return 0;
}
Regular → Executable
+2 -1
View File
@@ -10,7 +10,8 @@ fwknopd_SOURCES = fwknopd.c fwknopd.h config_init.c config_init.h \
fw_util.c fw_util.h fw_util_ipf.c fw_util_ipf.h \
fw_util_iptables.c fw_util_iptables.h \
fw_util_ipfw.c fw_util_ipfw.h \
fw_util_pf.c fw_util_pf.h cmd_opts.h
fw_util_pf.c fw_util_pf.h cmd_opts.h \
common.h
fwknopd_LDADD = $(top_builddir)/lib/libfko.la $(top_builddir)/common/libfko_util.a -lpcap
Regular → Executable
+35
View File
@@ -40,6 +40,7 @@
#include "access.h"
#include "utils.h"
#include "log_msg.h"
#include "common.h"
#define FATAL_ERR -1
@@ -47,6 +48,10 @@
#define SUCCESS 1
#endif
#ifdef HAVE_C_UNIT_TESTS
DECLARE_TEST_SUITE(access, "Access test suite");
#endif
/* Add an access string entry
*/
static int
@@ -1914,5 +1919,35 @@ dump_access_list(const fko_srv_options_t *opts)
fprintf(stdout, "\n");
fflush(stdout);
}
#ifdef HAVE_C_UNIT_TESTS
DECLARE_UTEST(compare_port_list, "check compare_port_list function")
{
acc_port_list_t *in1_pl = NULL;
acc_port_list_t *in2_pl = NULL;
acc_port_list_t *acc_pl = NULL;
/* Match any test */
free_acc_port_list(in1_pl);
free_acc_port_list(acc_pl);
add_port_list_ent(&in1_pl, "udp/6002");
add_port_list_ent(&in2_pl, "udp/6002, udp/6003");
add_port_list_ent(&acc_pl, "udp/6002, udp/6003");
CU_ASSERT(compare_port_list(in1_pl, acc_pl, 1) == 1); /* Only one match is needed from access port list - 1 */
CU_ASSERT(compare_port_list(in2_pl, acc_pl, 1) == 1); /* Only match is needed from access port list - 2 */
CU_ASSERT(compare_port_list(in1_pl, acc_pl, 0) == 1); /* All ports must match access port list - 1 */
CU_ASSERT(compare_port_list(in2_pl, acc_pl, 0) == 1); /* All ports must match access port list - 2 */
CU_ASSERT(compare_port_list(acc_pl, in1_pl, 0) == 0); /* All ports must match in1 port list - 1 */
CU_ASSERT(compare_port_list(acc_pl, in2_pl, 0) == 1); /* All ports must match in2 port list - 2 */
}
int register_ts_access(void)
{
ts_init(&TEST_SUITE(access), TEST_SUITE_DESCR(access));
ts_add_utest(&TEST_SUITE(access), UTEST_FCT(compare_port_list), UTEST_DESCR(compare_port_list));
return register_ts(&TEST_SUITE(access));
}
#endif /* HAVE_C_UNIT_TESTS */
/***EOF***/
Regular → Executable
+4
View File
@@ -48,6 +48,10 @@ int expand_acc_port_list(acc_port_list_t **plist, char *plist_str);
void free_acc_stanzas(fko_srv_options_t *opts);
void free_acc_port_list(acc_port_list_t *plist);
#ifdef HAVE_C_UNIT_TESTS
int register_ts_access(void);
#endif
#endif /* ACCESS_H */
/***EOF***/
Regular → Executable
-22
View File
@@ -838,26 +838,4 @@ enable_fault_injections(fko_srv_options_t * const opts)
}
#endif
void
clean_exit(fko_srv_options_t *opts, unsigned int fw_cleanup_flag, unsigned int exit_status)
{
#if HAVE_LIBFIU
if(opts->config[CONF_FAULT_INJECTION_TAG] != NULL)
{
fiu_disable(opts->config[CONF_FAULT_INJECTION_TAG]);
}
#endif
if(!opts->test && (fw_cleanup_flag == FW_CLEANUP))
fw_cleanup(opts);
#if USE_FILE_CACHE
free_replay_list(opts);
#endif
free_logging();
free_configs(opts);
exit(exit_status);
}
/***EOF***/
Regular → Executable
+26
View File
@@ -28,6 +28,10 @@
#include "fwknopd_common.h"
#include "utils.h"
#include "log_msg.h"
#include "replay_cache.h"
#include "config_init.h"
#include "fw_util.h"
#include <stdarg.h>
/* Generic hex dump function.
@@ -161,4 +165,26 @@ verify_file_perms_ownership(const char *file)
return res;
}
void
clean_exit(fko_srv_options_t *opts, unsigned int fw_cleanup_flag, unsigned int exit_status)
{
#if HAVE_LIBFIU
if(opts->config[CONF_FAULT_INJECTION_TAG] != NULL)
{
fiu_disable(opts->config[CONF_FAULT_INJECTION_TAG]);
}
#endif
if(!opts->test && (fw_cleanup_flag == FW_CLEANUP))
fw_cleanup(opts);
#if USE_FILE_CACHE
free_replay_list(opts);
#endif
free_logging();
free_configs(opts);
exit(exit_status);
}
/***EOF***/
+24 -6
View File
@@ -1,16 +1,34 @@
bin_PROGRAMS = fko_utests
bin_PROGRAMS = fwknop_utests fwknopd_utests
fko_utests_SOURCES = fko_utests.c \
fwknop_utests_SOURCES = fwknop_utests.c fwknop_common.h \
$(top_srcdir)/client/config_init.c config_init.h \
fwknop_common.h \
$(top_srcdir)/client/spa_comm.c spa_comm.h \
$(top_srcdir)/client/utils.c utils.h \
$(top_srcdir)/client/http_resolve_host.c \
$(top_srcdir)/client/getpasswd.c getpasswd.h cmd_opts.h \
$(top_srcdir)/client/log_msg.c log_msg.h
fko_utests_CPPFLAGS = -I $(top_srcdir)/lib -I $(top_srcdir)/common -I $(top_srcdir)/client
fwknop_utests_CPPFLAGS = -I $(top_srcdir)/lib -I $(top_srcdir)/common -I $(top_srcdir)/client
fko_utests_LDADD = $(top_builddir)/lib/libfko.la $(top_builddir)/common/libfko_util.a
fwknop_utests_LDADD = $(top_builddir)/lib/libfko.la $(top_builddir)/common/libfko_util.a
fko_utests_LDFLAGS = -lcunit
fwknop_utests_LDFLAGS = -lcunit
fwknopd_utests_SOURCES = fwknopd_utests.c fwknopd.h $(top_srcdir)/server/config_init.c config_init.h \
fwknopd_common.h $(top_srcdir)/server/incoming_spa.c incoming_spa.h \
$(top_srcdir)/server/pcap_capture.c pcap_capture.h $(top_srcdir)/server/process_packet.c \
process_packet.h $(top_srcdir)/server/log_msg.c log_msg.h $(top_srcdir)/server/utils.c utils.h \
$(top_srcdir)/server/sig_handler.c sig_handler.h $(top_srcdir)/server/replay_cache.c replay_cache.h \
$(top_srcdir)/server/access.c access.h $(top_srcdir)/server/fwknopd_errors.c fwknopd_errors.h \
$(top_srcdir)/server/tcp_server.c tcp_server.h $(top_srcdir)/server/extcmd.c extcmd.h \
$(top_srcdir)/server/fw_util.c fw_util.h $(top_srcdir)/server/fw_util_ipf.c fw_util_ipf.h \
$(top_srcdir)/server/fw_util_iptables.c fw_util_iptables.h \
$(top_srcdir)/server/fw_util_ipfw.c fw_util_ipfw.h \
$(top_srcdir)/server/fw_util_pf.c fw_util_pf.h cmd_opts.h \
common.h
fwknopd_utests_CPPFLAGS = -I $(top_srcdir)/lib -I $(top_srcdir)/common -I $(top_srcdir)/server -DSYSCONFDIR=\"$(sysconfdir)\" -DSYSRUNDIR=\"$(localstatedir)\"
fwknopd_utests_LDADD = $(top_builddir)/lib/libfko.la $(top_builddir)/common/libfko_util.a
fwknopd_utests_LDFLAGS = -lcunit -lpcap
-4
View File
@@ -1,4 +0,0 @@
#ifndef FKO_UTESTS_H
#define FKO_UTESTS_H
#endif /* FKO_UTESTS_H */
@@ -1,19 +1,17 @@
#include "CUnit/Basic.h"
#include "fwknop.h"
#include "fwknop_common.h"
#include "config_init.h"
#include "getpasswd.h"
/**
* Register test suites from FKO files.
*
* The module should fetch functions according to used modules. All of them follow the same
* naming convetion.
* naming convention.
*/
static void register_test_suites(void)
{
register_ts_config_init();
register_ts_getpassword();
}
/* The main() function for setting up and running the tests.
+35
View File
@@ -0,0 +1,35 @@
#include "CUnit/Basic.h"
#include "fwknopd_common.h"
#include "access.h"
/**
* Register test suites from FKO files.
*
* The module should fetch functions according to used modules. All of them follow the same
* naming convention.
*/
static void register_test_suites(void)
{
register_ts_access();
}
/* The main() function for setting up and running the tests.
* Returns a CUE_SUCCESS on successful running, another
* CUnit error code on failure.
*/
int main()
{
/* initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
/* Register test suites from fko files */
register_test_suites();
/* Run all tests using the CUnit Basic interface */
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}
+1 -1
View File
@@ -1286,7 +1286,7 @@ sub profile_coverage() {
my $curr_dir = getcwd() or die $!;
### gcov -b ../client/*.gcno
for my $dir ('../client', '../server', '../lib/.libs') {
for my $dir ('../client', '../server', '../lib/.libs', '../test/c-unit-tests') {
next unless -d $dir;
chdir $dir or die $!;
system "$gcov_path -b -u *.gcno > /dev/null 2>&1";