From 306dd0f6852a383d0c4ba2ef17f73c411b75c6b9 Mon Sep 17 00:00:00 2001 From: Franck Joncourt Date: Sun, 24 Aug 2014 21:05:44 +0200 Subject: [PATCH 01/12] First layout to add c unit testing support to fwknop. --- client/config_init.c | 57 ++++++++++++++++++++++++++++++++++ client/config_init.h | 4 +++ client/getpasswd.c | 52 +++++++++++++++++++++++++++++++ client/getpasswd.h | 4 +++ common/common.h | 15 +++++++++ configure.ac | 16 ++++++++++ test/c-unit-tests/Makefile.am | 16 ++++++++++ test/c-unit-tests/fko_utests.c | 34 ++++++++++++++++++++ test/c-unit-tests/fko_utests.h | 6 ++++ 9 files changed, 204 insertions(+) mode change 100644 => 100755 client/config_init.c mode change 100644 => 100755 client/config_init.h mode change 100644 => 100755 configure.ac create mode 100755 test/c-unit-tests/Makefile.am create mode 100755 test/c-unit-tests/fko_utests.c create mode 100755 test/c-unit-tests/fko_utests.h diff --git a/client/config_init.c b/client/config_init.c old mode 100644 new mode 100755 index fb01d965..71fdcf56 --- a/client/config_init.c +++ b/client/config_init.c @@ -52,6 +52,11 @@ #define BITMASK_ARRAY_SIZE 2 /*!< Number of 32bits integer used to handle bitmask in the fko_var_bitmask_t structure */ #define LF_CHAR 0x0A /*!< Hexadecimal value associated to the LF char */ +#ifdef HAVE_C_UNIT_TESTS + #define TEST_SUITE_DESCRIPTION "Config init module" +#endif + + /** * Structure to handle long bitmask. * @@ -2539,3 +2544,55 @@ usage(void) return; } +#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 config_init.c") +{ + CU_ASSERT(var_is_critical(0) == 0); +} + +DECLARE_UTEST(test2, "Description de ut2 dans config_init.c") +{ + CU_ASSERT(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; +} + +#endif /* HAVE_C_UNIT_TESTS */ + diff --git a/client/config_init.h b/client/config_init.h old mode 100644 new mode 100755 index f9b6fe34..81a00fe1 --- a/client/config_init.h +++ b/client/config_init.h @@ -43,6 +43,10 @@ void config_init(fko_cli_options_t *options, int argc, char **argv); void usage(void); +#ifdef HAVE_C_UNIT_TESTS +int register_ts_config_init(void); +#endif + #endif /* CONFIG_INIT_H */ /***EOF***/ diff --git a/client/getpasswd.c b/client/getpasswd.c index d62cd47e..0da8e7ed 100644 --- a/client/getpasswd.c +++ b/client/getpasswd.c @@ -52,6 +52,10 @@ #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 * @@ -293,4 +297,52 @@ 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***/ diff --git a/client/getpasswd.h b/client/getpasswd.h index b0548c2b..39c526a2 100644 --- a/client/getpasswd.h +++ b/client/getpasswd.h @@ -40,4 +40,8 @@ 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 */ diff --git a/common/common.h b/common/common.h index 5f3007a8..b8416950 100644 --- a/common/common.h +++ b/common/common.h @@ -42,6 +42,11 @@ #include #endif +/* Include cunit header if c unit testing support is enabled. */ +#ifdef HAVE_C_UNIT_TESTS + #include "CUnit/Basic.h" +#endif + #include #if HAVE_SYS_TYPES_H @@ -179,6 +184,16 @@ 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***/ diff --git a/configure.ac b/configure.ac old mode 100644 new mode 100755 index e87eeab2..63d325e6 --- a/configure.ac +++ b/configure.ac @@ -152,6 +152,21 @@ if test "x$want_libfiu_support" = "xyes"; then FKO_CHECK_COMPILER_ARG_LDFLAGS_ONLY([-lfiu]) fi +dnl Decide whether or not to enable C unit testing +dnl +want_c_unit_tests=no +AC_ARG_ENABLE([c-unit-tests], + [AS_HELP_STRING([--enable-c-unit-tests], + [Enable C unit testing with libcunit support @<:@default is to disable@:>@])], + [want_c_unit_tests=$enableval], + []) + +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_LDFLAGS_ONLY([-lcunit]) +fi + dnl Decide whether or not to compile in certain features that enable fuzzing dnl of fwknop code - this is for testing purposes only. dnl @@ -622,6 +637,7 @@ AC_CONFIG_FILES([Makefile client/Makefile server/Makefile common/Makefile + test/c-unit-tests/Makefile doc/Makefile]) AC_OUTPUT diff --git a/test/c-unit-tests/Makefile.am b/test/c-unit-tests/Makefile.am new file mode 100755 index 00000000..f5ba2f19 --- /dev/null +++ b/test/c-unit-tests/Makefile.am @@ -0,0 +1,16 @@ +bin_PROGRAMS = fko_utests + +fko_utests_SOURCES = fko_utests.c \ + $(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 + +fko_utests_LDADD = $(top_builddir)/lib/libfko.la $(top_builddir)/common/libfko_util.a + +fko_utests_LDFLAGS = -lcunit \ No newline at end of file diff --git a/test/c-unit-tests/fko_utests.c b/test/c-unit-tests/fko_utests.c new file mode 100755 index 00000000..007de74c --- /dev/null +++ b/test/c-unit-tests/fko_utests.c @@ -0,0 +1,34 @@ +#include "CUnit/Basic.h" + +#include "fwknop.h" +#include "config_init.h" +#include "getpasswd.h" + +/** + * Register test suites from FKO files + */ +static void register_test_suites(void) +{ + register_ts_config_init(); + register_ts_getpassword(); +} + +/* 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(); +} diff --git a/test/c-unit-tests/fko_utests.h b/test/c-unit-tests/fko_utests.h new file mode 100755 index 00000000..80db4b2c --- /dev/null +++ b/test/c-unit-tests/fko_utests.h @@ -0,0 +1,6 @@ +#ifndef FKO_UTESTS_H +#define FKO_UTESTS_H + + + +#endif /* FKO_UTESTS_H */ \ No newline at end of file From 934d764159293ef93a3a5c382e94655922a0c658 Mon Sep 17 00:00:00 2001 From: Franck Joncourt Date: Sun, 24 Aug 2014 21:14:45 +0200 Subject: [PATCH 02/12] Minor update. --- test/c-unit-tests/Makefile.am | 16 ++++++++-------- test/c-unit-tests/fko_utests.c | 9 ++++++--- test/c-unit-tests/fko_utests.h | 4 +--- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/test/c-unit-tests/Makefile.am b/test/c-unit-tests/Makefile.am index f5ba2f19..2d009f5f 100755 --- a/test/c-unit-tests/Makefile.am +++ b/test/c-unit-tests/Makefile.am @@ -1,16 +1,16 @@ bin_PROGRAMS = fko_utests fko_utests_SOURCES = fko_utests.c \ - $(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 + $(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 fko_utests_LDADD = $(top_builddir)/lib/libfko.la $(top_builddir)/common/libfko_util.a -fko_utests_LDFLAGS = -lcunit \ No newline at end of file +fko_utests_LDFLAGS = -lcunit diff --git a/test/c-unit-tests/fko_utests.c b/test/c-unit-tests/fko_utests.c index 007de74c..ef716212 100755 --- a/test/c-unit-tests/fko_utests.c +++ b/test/c-unit-tests/fko_utests.c @@ -5,11 +5,14 @@ #include "getpasswd.h" /** - * Register test suites from FKO files + * Register test suites from FKO files. + * + * The module should fetch functions according to used modules. All of them follow the same + * naming convetion. */ static void register_test_suites(void) { - register_ts_config_init(); + register_ts_config_init(); register_ts_getpassword(); } @@ -23,7 +26,7 @@ int main() if (CUE_SUCCESS != CU_initialize_registry()) return CU_get_error(); - /* Register test suites from fko files */ + /* Register test suites from fko files */ register_test_suites(); /* Run all tests using the CUnit Basic interface */ diff --git a/test/c-unit-tests/fko_utests.h b/test/c-unit-tests/fko_utests.h index 80db4b2c..0cca6e93 100755 --- a/test/c-unit-tests/fko_utests.h +++ b/test/c-unit-tests/fko_utests.h @@ -1,6 +1,4 @@ #ifndef FKO_UTESTS_H #define FKO_UTESTS_H - - -#endif /* FKO_UTESTS_H */ \ No newline at end of file +#endif /* FKO_UTESTS_H */ From 688f08c2a0378fb9afc9b9015a454ffdde6e969e Mon Sep 17 00:00:00 2001 From: Franck Joncourt Date: Sun, 31 Aug 2014 20:48:44 +0200 Subject: [PATCH 03/12] 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. --- Makefile.am | 5 ++ client/config_init.c | 63 ++++++++++--------- client/getpasswd.c | 52 --------------- client/getpasswd.h | 4 -- common/Makefile.am | 2 +- common/common.h | 11 +--- common/cunit_common.h | 31 +++++++++ configure.ac | 4 +- lib/Makefile.am | 3 +- lib/cunit_common.c | 45 +++++++++++++ server/Makefile.am | 3 +- server/access.c | 35 +++++++++++ server/access.h | 4 ++ server/fwknopd.c | 22 ------- server/utils.c | 26 ++++++++ test/c-unit-tests/Makefile.am | 30 +++++++-- test/c-unit-tests/fko_utests.h | 4 -- .../{fko_utests.c => fwknop_utests.c} | 6 +- test/c-unit-tests/fwknopd_utests.c | 35 +++++++++++ test/test-fwknop.pl | 2 +- 20 files changed, 250 insertions(+), 137 deletions(-) mode change 100644 => 100755 client/getpasswd.c mode change 100644 => 100755 client/getpasswd.h mode change 100644 => 100755 common/Makefile.am mode change 100644 => 100755 common/common.h create mode 100755 common/cunit_common.h mode change 100644 => 100755 lib/Makefile.am create mode 100755 lib/cunit_common.c mode change 100644 => 100755 server/Makefile.am mode change 100644 => 100755 server/access.c mode change 100644 => 100755 server/access.h mode change 100644 => 100755 server/fwknopd.c mode change 100644 => 100755 server/utils.c delete mode 100755 test/c-unit-tests/fko_utests.h rename test/c-unit-tests/{fko_utests.c => fwknop_utests.c} (89%) create mode 100755 test/c-unit-tests/fwknopd_utests.c diff --git a/Makefile.am b/Makefile.am index 314ae954..a9d80ecc 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 = \ diff --git a/client/config_init.c b/client/config_init.c index 71fdcf56..d59184fb 100755 --- a/client/config_init.c +++ b/client/config_init.c @@ -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 */ diff --git a/client/getpasswd.c b/client/getpasswd.c old mode 100644 new mode 100755 index 0da8e7ed..d62cd47e --- a/client/getpasswd.c +++ b/client/getpasswd.c @@ -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***/ diff --git a/client/getpasswd.h b/client/getpasswd.h old mode 100644 new mode 100755 index 39c526a2..b0548c2b --- a/client/getpasswd.h +++ b/client/getpasswd.h @@ -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 */ diff --git a/common/Makefile.am b/common/Makefile.am old mode 100644 new mode 100755 index 95d4c185..53e8794d --- a/common/Makefile.am +++ b/common/Makefile.am @@ -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) diff --git a/common/common.h b/common/common.h old mode 100644 new mode 100755 index b8416950..b73f7f18 --- a/common/common.h +++ b/common/common.h @@ -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 @@ -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***/ diff --git a/common/cunit_common.h b/common/cunit_common.h new file mode 100755 index 00000000..63057605 --- /dev/null +++ b/common/cunit_common.h @@ -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 \ No newline at end of file diff --git a/configure.ac b/configure.ac index 63d325e6..32875e2e 100755 --- a/configure.ac +++ b/configure.ac @@ -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 diff --git a/lib/Makefile.am b/lib/Makefile.am old mode 100644 new mode 100755 index 3f0ef7b2..106244e4 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -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_' diff --git a/lib/cunit_common.c b/lib/cunit_common.c new file mode 100755 index 00000000..cd118b04 --- /dev/null +++ b/lib/cunit_common.c @@ -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_utestnb_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; +} \ No newline at end of file diff --git a/server/Makefile.am b/server/Makefile.am old mode 100644 new mode 100755 index fc321ba4..c27e3387 --- a/server/Makefile.am +++ b/server/Makefile.am @@ -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 diff --git a/server/access.c b/server/access.c old mode 100644 new mode 100755 index 28acd0b9..ae3826a5 --- a/server/access.c +++ b/server/access.c @@ -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***/ diff --git a/server/access.h b/server/access.h old mode 100644 new mode 100755 index 62289deb..d4788b9c --- a/server/access.h +++ b/server/access.h @@ -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***/ diff --git a/server/fwknopd.c b/server/fwknopd.c old mode 100644 new mode 100755 index 357a0bbd..89aa00fb --- a/server/fwknopd.c +++ b/server/fwknopd.c @@ -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***/ diff --git a/server/utils.c b/server/utils.c old mode 100644 new mode 100755 index 8daa3aac..f285f90c --- a/server/utils.c +++ b/server/utils.c @@ -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 /* 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***/ diff --git a/test/c-unit-tests/Makefile.am b/test/c-unit-tests/Makefile.am index 2d009f5f..f2e96a84 100755 --- a/test/c-unit-tests/Makefile.am +++ b/test/c-unit-tests/Makefile.am @@ -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 \ No newline at end of file diff --git a/test/c-unit-tests/fko_utests.h b/test/c-unit-tests/fko_utests.h deleted file mode 100755 index 0cca6e93..00000000 --- a/test/c-unit-tests/fko_utests.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef FKO_UTESTS_H -#define FKO_UTESTS_H - -#endif /* FKO_UTESTS_H */ diff --git a/test/c-unit-tests/fko_utests.c b/test/c-unit-tests/fwknop_utests.c similarity index 89% rename from test/c-unit-tests/fko_utests.c rename to test/c-unit-tests/fwknop_utests.c index ef716212..0ccebd25 100755 --- a/test/c-unit-tests/fko_utests.c +++ b/test/c-unit-tests/fwknop_utests.c @@ -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. diff --git a/test/c-unit-tests/fwknopd_utests.c b/test/c-unit-tests/fwknopd_utests.c new file mode 100755 index 00000000..b3bc6675 --- /dev/null +++ b/test/c-unit-tests/fwknopd_utests.c @@ -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(); +} diff --git a/test/test-fwknop.pl b/test/test-fwknop.pl index a532839a..14941e83 100755 --- a/test/test-fwknop.pl +++ b/test/test-fwknop.pl @@ -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"; From bd0035af1e1efa2d6b82f9be27bac075c69d9f52 Mon Sep 17 00:00:00 2001 From: Franck Joncourt Date: Sun, 7 Sep 2014 15:40:03 +0200 Subject: [PATCH 04/12] Fixed tab vs spaces --- client/config_init.c | 56 +++++++++++++++--------------- common/common.h | 2 +- common/cunit_common.h | 44 +++++++++++------------ lib/cunit_common.c | 54 ++++++++++++++-------------- server/access.c | 36 +++++++++---------- test/c-unit-tests/fwknop_utests.c | 16 ++++----- test/c-unit-tests/fwknopd_utests.c | 18 +++++----- 7 files changed, 113 insertions(+), 113 deletions(-) diff --git a/client/config_init.c b/client/config_init.c index a6f6a804..cead2af1 100755 --- a/client/config_init.c +++ b/client/config_init.c @@ -2559,43 +2559,43 @@ static int ut_clean_test_suite(void) DECLARE_UTEST(critical_var, "Check critcial vars") { CU_ASSERT(var_is_critical(FWKNOP_CLI_ARG_KEY_RIJNDAEL) == 1); - CU_ASSERT(var_is_critical(FWKNOP_CLI_ARG_WGET_CMD) == 0); + CU_ASSERT(var_is_critical(FWKNOP_CLI_ARG_WGET_CMD) == 0); } DECLARE_UTEST(check_var_bitmask, "Check var_bitmask functions") { - fko_var_bitmask_t var_bitmask; + 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); + 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); } int register_ts_config_init(void) { - 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)); + 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 */ diff --git a/common/common.h b/common/common.h index 5759be21..4e3dd4a2 100755 --- a/common/common.h +++ b/common/common.h @@ -45,7 +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" + #include "cunit_common.h" #endif #include diff --git a/common/cunit_common.h b/common/cunit_common.h index 63057605..a118195d 100755 --- a/common/cunit_common.h +++ b/common/cunit_common.h @@ -1,31 +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; + 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) - + #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); - +int register_ts(c_unit_test_suite_t *ts); + #endif // CUNIT_COMMON_H \ No newline at end of file diff --git a/lib/cunit_common.c b/lib/cunit_common.c index cd118b04..00ed5b46 100755 --- a/lib/cunit_common.c +++ b/lib/cunit_common.c @@ -5,41 +5,41 @@ 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); + 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)++; + 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; + 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(); - } + 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_utestnb_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(); - } - } + /* add the tests to the suite */ + for (ix_utest=0 ; ix_utestnb_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; -} \ No newline at end of file + return 0; +} diff --git a/server/access.c b/server/access.c index 318e8eb4..ab623fd6 100755 --- a/server/access.c +++ b/server/access.c @@ -1950,29 +1950,29 @@ dump_access_list(const fko_srv_options_t *opts) 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 *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 */ + + /* 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)); + 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 */ diff --git a/test/c-unit-tests/fwknop_utests.c b/test/c-unit-tests/fwknop_utests.c index 0ccebd25..f0948168 100755 --- a/test/c-unit-tests/fwknop_utests.c +++ b/test/c-unit-tests/fwknop_utests.c @@ -20,16 +20,16 @@ static void register_test_suites(void) */ int main() { - /* initialize the CUnit test registry */ - if (CUE_SUCCESS != CU_initialize_registry()) - return CU_get_error(); + /* 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(); + /* 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(); } diff --git a/test/c-unit-tests/fwknopd_utests.c b/test/c-unit-tests/fwknopd_utests.c index b3bc6675..49072947 100755 --- a/test/c-unit-tests/fwknopd_utests.c +++ b/test/c-unit-tests/fwknopd_utests.c @@ -11,7 +11,7 @@ */ static void register_test_suites(void) { - register_ts_access(); + register_ts_access(); } /* The main() function for setting up and running the tests. @@ -20,16 +20,16 @@ static void register_test_suites(void) */ int main() { - /* initialize the CUnit test registry */ - if (CUE_SUCCESS != CU_initialize_registry()) - return CU_get_error(); + /* 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(); + /* 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(); } From 260cd5481b41384f802eaa4bd1a33824d751cf8f Mon Sep 17 00:00:00 2001 From: Franck Joncourt Date: Sun, 7 Sep 2014 20:33:39 +0200 Subject: [PATCH 05/12] Use of init and cleanup functions for the test suite. --- client/config_init.c | 64 +++++++++++++++++++++++-------------------- common/cunit_common.h | 54 ++++++++++++++++++++---------------- lib/cunit_common.c | 22 ++++++++------- server/access.c | 36 ++++++++++++------------ 4 files changed, 94 insertions(+), 82 deletions(-) diff --git a/client/config_init.c b/client/config_init.c index a6f6a804..41c036eb 100755 --- a/client/config_init.c +++ b/client/config_init.c @@ -2546,12 +2546,13 @@ usage(void) #ifdef HAVE_C_UNIT_TESTS -static int ut_init_test_suite(void) +DECLARE_TEST_SUITE_INIT(config_init) { + log_set_verbosity(LOG_VERBOSITY_ERROR); return 0; } -static int ut_clean_test_suite(void) +DECLARE_TEST_SUITE_CLEANUP(config_init) { return 0; } @@ -2559,43 +2560,46 @@ static int ut_clean_test_suite(void) DECLARE_UTEST(critical_var, "Check critcial vars") { CU_ASSERT(var_is_critical(FWKNOP_CLI_ARG_KEY_RIJNDAEL) == 1); - CU_ASSERT(var_is_critical(FWKNOP_CLI_ARG_WGET_CMD) == 0); + CU_ASSERT(var_is_critical(FWKNOP_CLI_ARG_WGET_CMD) == 0); } DECLARE_UTEST(check_var_bitmask, "Check var_bitmask functions") { - fko_var_bitmask_t var_bitmask; + 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); + 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); + + add_var_to_bitmask(FWKNOP_CLI_LAST_ARG+34, &var_bitmask); + CU_ASSERT(bitmask_has_var(FWKNOP_CLI_LAST_ARG+34, &var_bitmask) == 0); } int register_ts_config_init(void) { - 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)); + ts_init(&TEST_SUITE(config_init), TEST_SUITE_DESCR(config_init), TEST_SUITE_INIT(config_init), TEST_SUITE_CLEANUP(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 */ diff --git a/common/cunit_common.h b/common/cunit_common.h index 63057605..d3cb9460 100755 --- a/common/cunit_common.h +++ b/common/cunit_common.h @@ -1,31 +1,37 @@ #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; + typedef struct c_unit_test + { + char description[128]; + void (*func)(void); + } c_unit_test_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); + typedef struct c_unit_test_suite + { + char description[128]; + int (*init_func)(void); + int (*cleanup_func)(void); + 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 TEST_SUITE_INIT(name) _ts_init_##name + #define TEST_SUITE_CLEANUP(name) _ts_cleanup_##name + + #define DECLARE_TEST_SUITE(name, description) static const char ts_descr_##name[] = description; \ + static c_unit_test_suite_t ts_##name; + #define DECLARE_TEST_SUITE_INIT(name) int _ts_init_##name(void) + #define DECLARE_TEST_SUITE_CLEANUP(name) int _ts_cleanup_##name(void) + #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, int (*init)(void), int (*cleanup)(void)); 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 \ No newline at end of file diff --git a/lib/cunit_common.c b/lib/cunit_common.c index cd118b04..5aeb355c 100755 --- a/lib/cunit_common.c +++ b/lib/cunit_common.c @@ -3,20 +3,22 @@ #include "stdio.h" #include "CUnit/Basic.h" -void ts_init(c_unit_test_suite_t* ts, const char* description) +void ts_init(c_unit_test_suite_t* ts, const char* description, int (*init)(void), int (*cleanup)(void)) { - memset(ts, 0x00, sizeof(c_unit_test_suite_t)); - strcpy(ts->description, description); + memset(ts, 0x00, sizeof(c_unit_test_suite_t)); + strcpy(ts->description, description); + ts->init_func = init; + ts->cleanup_func = cleanup; } 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)++; + 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) @@ -24,7 +26,7 @@ int register_ts(c_unit_test_suite_t *ts) CU_pSuite pSuite = NULL; int ix_utest; - pSuite = CU_add_suite(ts->description, NULL, NULL); + pSuite = CU_add_suite(ts->description, ts->init_func, ts->cleanup_func); if (NULL == pSuite) { CU_cleanup_registry(); return CU_get_error(); diff --git a/server/access.c b/server/access.c index 318e8eb4..5e7b492f 100755 --- a/server/access.c +++ b/server/access.c @@ -1950,29 +1950,29 @@ dump_access_list(const fko_srv_options_t *opts) 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 *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 */ + + /* 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)); + ts_init(&TEST_SUITE(access), TEST_SUITE_DESCR(access), NULL, NULL); + 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 */ From 526cae6464633696bc9241412b0bdb975bcabbd2 Mon Sep 17 00:00:00 2001 From: Franck Joncourt Date: Sun, 28 Dec 2014 23:55:52 +0100 Subject: [PATCH 06/12] Fixed upper case --- test/c-unit-tests/fwknop_utests.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/c-unit-tests/fwknop_utests.c b/test/c-unit-tests/fwknop_utests.c index f0948168..0f9afa91 100755 --- a/test/c-unit-tests/fwknop_utests.c +++ b/test/c-unit-tests/fwknop_utests.c @@ -28,8 +28,8 @@ int main() 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(); + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + CU_cleanup_registry(); + return CU_get_error(); } From fdb3da0769be166ff73b4b7c8c4db710aec7356e Mon Sep 17 00:00:00 2001 From: Franck Joncourt Date: Mon, 29 Dec 2014 11:45:23 +0100 Subject: [PATCH 07/12] Added README for c unit test --- test/c-unit-tests/README.md | 150 ++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100755 test/c-unit-tests/README.md diff --git a/test/c-unit-tests/README.md b/test/c-unit-tests/README.md new file mode 100755 index 00000000..5a009078 --- /dev/null +++ b/test/c-unit-tests/README.md @@ -0,0 +1,150 @@ +##Build +~~~ +$ ./autogen.sh +$ ./configure --enable-c-unit-tests --prefix=/usr --sysconfdir=/etc --enable-profile-coverage +$ make +~~~ + +~~~ +$ ./test-fwknop.pl --enable-profile-coverage-check --loopback lo +~~~ + +The HAVE_C_UNIT_TESTS constant is used in source files to define c-unit test code. +Source code is built against libcunit. + +##Run test suites +Once the build is complete, two test programs allow the user to run the tests suites: + * fwknopd_utests: program to run fwknopd c unit test suites + * fwknop_utests: program to run fwknop c unit test suites + +~~~ +$ test/c-unit-tests/fwknopd_utests + + CUnit - A unit testing framework for C - Version 2.1-2 + http://cunit.sourceforge.net/ + +Suite: Access test suite + Test: check compare_port_list function ...FAILED + 1. ../../server/access.c:2017 - compare_port_list(acc_pl, in1_pl, 0) == 0 + +Run Summary: Type Total Ran Passed Failed Inactive + suites 1 1 n/a 0 0 + tests 1 1 0 1 0 + asserts 6 6 5 1 n/a + +Elapsed time = 0.000 seconds +~~~ + +~~~ +$ test/c-unit-tests/fwknop_utests + + CUnit - A unit testing framework for C - Version 2.1-2 + http://cunit.sourceforge.net/ + +Suite: Config init test suite + Test: Check critcial vars ...passed + Test: Check var_bitmask functions ...passed + +Run Summary: Type Total Ran Passed Failed Inactive + suites 1 1 n/a 0 0 + tests 2 2 2 0 0 + asserts 12 12 12 0 n/a + +Elapsed time = 0.000 seconds +~~~ + +##Manage C unit tests +C unit tests are implemented in source files directly and registered in a test suite. +All test suites are then added to fwknopd or fwknop test programs + +In order to add new tests, the user must follow the below steps: + * Declare the tests suite + * Create one or more unit tests + * Create a function to register new tests + +###Declare the tests suite + +In access.c file: + +~~~ + #ifdef HAVE_C_UNIT_TESTS + DECLARE_TEST_SUITE(access, "Access test suite"); + #endif +~~~ + +In the above example, we create a test suite using the DECLARE_TEST_SUITE macro: + * the test suite is named "access". + * the test suite description is "Access test suite" and is displayed on the console + when the test program is executed + +###Create unit tests + +In access.c file: + +#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 */ +} + +#endif /* HAVE_C_UNIT_TESTS */ +~~~ + +In the above example, we create a c-unit test using the DECLARE_UTEST macro: + * the unit test is named "compare_port_list". This id must be unique + * the unit test description is "check compare_port_list function" and is displayed on the console + when the test program is executed + +###Create a function to register new tests + +In access.c file: + +~~~ +#ifdef HAVE_C_UNIT_TESTS + +int register_ts_access(void) +{ + ts_init(&TEST_SUITE(access), TEST_SUITE_DESCR(access), NULL, NULL); + 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 */ +~~~ + +In access.h file: + +~~~ +#ifdef HAVE_C_UNIT_TESTS +int register_ts_access(void); +#endif +~~~ +In fwknopd_utests.c file: + +~~~ +static void register_test_suites(void) +{ + register_ts_access(); +} +~~~ + +The register_ts_access function create the new test suite and add unit test to it. + +###Check gcov coverage \ No newline at end of file From 750fd97bda3a5f482ff2a41f6a534d40eed47b24 Mon Sep 17 00:00:00 2001 From: Franck Joncourt Date: Mon, 29 Dec 2014 11:45:30 +0100 Subject: [PATCH 08/12] Allow to build without c unit test --- lib/cunit_common.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/cunit_common.c b/lib/cunit_common.c index afc71824..59f6375b 100755 --- a/lib/cunit_common.c +++ b/lib/cunit_common.c @@ -1,3 +1,5 @@ +#ifdef HAVE_C_UNIT_TESTS + #include "cunit_common.h" #include "stdlib.h" #include "stdio.h" @@ -45,3 +47,5 @@ int register_ts(c_unit_test_suite_t *ts) return 0; } + +#endif /* HAVE_C_UNIT_TESTS */ From cb13d84d489b2f9a43ec47e7ba2b72bd03789bca Mon Sep 17 00:00:00 2001 From: Franck Joncourt Date: Mon, 29 Dec 2014 13:25:01 +0100 Subject: [PATCH 09/12] Use of subdir-objects libtool option. --- configure.ac | 2 +- test/c-unit-tests/Makefile.am | 52 +++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/configure.ac b/configure.ac index 6dd458db..dfa46fbb 100755 --- a/configure.ac +++ b/configure.ac @@ -21,7 +21,7 @@ AC_CONFIG_AUX_DIR(config) AC_CANONICAL_TARGET -AM_INIT_AUTOMAKE +AM_INIT_AUTOMAKE([subdir-objects]) dnl AM_MAINTAINER_MODE diff --git a/test/c-unit-tests/Makefile.am b/test/c-unit-tests/Makefile.am index f2e96a84..bfd572c7 100755 --- a/test/c-unit-tests/Makefile.am +++ b/test/c-unit-tests/Makefile.am @@ -1,34 +1,40 @@ bin_PROGRAMS = fwknop_utests fwknopd_utests -fwknop_utests_SOURCES = fwknop_utests.c fwknop_common.h \ - $(top_srcdir)/client/config_init.c config_init.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 +fwknop_utests_SOURCES = fwknop_utests.c \ + ../../client/config_init.c \ + ../../client/spa_comm.c \ + ../../client/utils.c \ + ../../client/http_resolve_host.c \ + ../../client/getpasswd.c \ + ../../client/log_msg.c -fwknop_utests_CPPFLAGS = -I $(top_srcdir)/lib -I $(top_srcdir)/common -I $(top_srcdir)/client +fwknop_utests_CPPFLAGS = -I ../../lib -I ../../common -I ../../client -fwknop_utests_LDADD = $(top_builddir)/lib/libfko.la $(top_builddir)/common/libfko_util.a +fwknop_utests_LDADD = ../../lib/libfko.la ../../common/libfko_util.a 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_SOURCES = fwknopd_utests.c \ + ../../server/config_init.c \ + ../../server/incoming_spa.c \ + ../../server/pcap_capture.c \ + ../../server/process_packet.c \ + ../../server/log_msg.c \ + ../../server/utils.c \ + ../../server/sig_handler.c \ + ../../server/replay_cache.c \ + ../../server/access.c\ + ../../server/fwknopd_errors.c \ + ../../server/tcp_server.c \ + ../../server/extcmd.c \ + ../../server/fw_util.c \ + ../../server/fw_util_ipf.c \ + ../../server/fw_util_iptables.c \ + ../../server/fw_util_ipfw.c \ + ../../server/fw_util_pf.c -fwknopd_utests_CPPFLAGS = -I $(top_srcdir)/lib -I $(top_srcdir)/common -I $(top_srcdir)/server -DSYSCONFDIR=\"$(sysconfdir)\" -DSYSRUNDIR=\"$(localstatedir)\" +fwknopd_utests_CPPFLAGS = -I ../../lib -I ../../common -I ../../server -DSYSCONFDIR=\"$(sysconfdir)\" -DSYSRUNDIR=\"$(localstatedir)\" fwknopd_utests_LDADD = $(top_builddir)/lib/libfko.la $(top_builddir)/common/libfko_util.a -fwknopd_utests_LDFLAGS = -lcunit -lpcap \ No newline at end of file +fwknopd_utests_LDFLAGS = -lcunit -lpcap From 620d31364e6984eb25ad2279fb4fd71fc33870ef Mon Sep 17 00:00:00 2001 From: Franck Joncourt Date: Mon, 29 Dec 2014 18:47:17 +0100 Subject: [PATCH 10/12] * Removed duplicated entry in Makefile.am for libfko * Added c unit tests for fko (draft) * Updated c unit test README file. --- lib/Makefile.am | 2 +- lib/fko.h | 4 + lib/fko_decode.c | 23 +++ test/c-unit-tests/Makefile.am | 39 +++- test/c-unit-tests/README.md | 341 ++++++++++++++++++--------------- test/c-unit-tests/fko_utests.c | 34 ++++ 6 files changed, 291 insertions(+), 152 deletions(-) create mode 100755 test/c-unit-tests/fko_utests.c diff --git a/lib/Makefile.am b/lib/Makefile.am index 106244e4..702c1525 100755 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -7,7 +7,7 @@ libfko_source_files = \ fko_message.h fko_nat_access.c fko_rand_value.c fko_server_auth.c \ 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 \ + sha1.h sha2.c sha2.h fko_context.h fko_state.h \ gpgme_funcs.c gpgme_funcs.h strlcpy.c strlcat.c fko_util.c fko_util.h \ cunit_common.c cunit_common.h diff --git a/lib/fko.h b/lib/fko.h index 2d9b216b..0da1294b 100644 --- a/lib/fko.h +++ b/lib/fko.h @@ -448,6 +448,10 @@ DLL_API int fko_gpg_signature_fpr_match(fko_ctx_t ctx, const char * const fpr, } #endif +#ifdef HAVE_C_UNIT_TESTS +int register_ts_fko_decode(void); +#endif + #endif /* FKO_H */ /***EOF***/ diff --git a/lib/fko_decode.c b/lib/fko_decode.c index bd88d505..573bf452 100644 --- a/lib/fko_decode.c +++ b/lib/fko_decode.c @@ -36,6 +36,10 @@ #define FIELD_PARSERS 9 +#ifdef HAVE_C_UNIT_TESTS +DECLARE_TEST_SUITE(fko_decode, "FKO decode test suite"); +#endif + static int num_fields(char *str) { @@ -590,4 +594,23 @@ fko_decode_spa_data(fko_ctx_t ctx) return(FKO_SUCCESS); } +#ifdef HAVE_C_UNIT_TESTS + +DECLARE_UTEST(num_fields, "Count the number of SPA fields in a SPA packet") +{ + CU_ASSERT(num_fields("abcdef") == 0); + CU_ASSERT(num_fields("abcdef:gh") == 1); + +} + +int register_ts_fko_decode(void) +{ + ts_init(&TEST_SUITE(fko_decode), TEST_SUITE_DESCR(fko_decode), NULL, NULL); + ts_add_utest(&TEST_SUITE(fko_decode), UTEST_FCT(num_fields), UTEST_DESCR(num_fields)); + + return register_ts(&TEST_SUITE(fko_decode)); +} + +#endif /* HAVE_C_UNIT_TESTS */ + /***EOF***/ diff --git a/test/c-unit-tests/Makefile.am b/test/c-unit-tests/Makefile.am index bfd572c7..f82679b0 100755 --- a/test/c-unit-tests/Makefile.am +++ b/test/c-unit-tests/Makefile.am @@ -1,4 +1,4 @@ -bin_PROGRAMS = fwknop_utests fwknopd_utests +bin_PROGRAMS = fwknop_utests fwknopd_utests fko_utests fwknop_utests_SOURCES = fwknop_utests.c \ ../../client/config_init.c \ @@ -14,6 +14,9 @@ fwknop_utests_LDADD = ../../lib/libfko.la ../../common/libfko_util.a fwknop_utests_LDFLAGS = -lcunit + + + fwknopd_utests_SOURCES = fwknopd_utests.c \ ../../server/config_init.c \ ../../server/incoming_spa.c \ @@ -38,3 +41,37 @@ fwknopd_utests_CPPFLAGS = -I ../../lib -I ../../common -I ../../server -DSYSCONF fwknopd_utests_LDADD = $(top_builddir)/lib/libfko.la $(top_builddir)/common/libfko_util.a fwknopd_utests_LDFLAGS = -lcunit -lpcap + + + +fko_utests_SOURCES = fko_utests.c \ + ../../lib/base64.c \ + ../../lib/cipher_funcs.c \ + ../../lib/digest.c \ + ../../lib/fko_client_timeout.c \ + ../../lib/fko_digest.c \ + ../../lib/fko_encode.c \ + ../../lib/fko_decode.c \ + ../../lib/fko_encryption.c \ + ../../lib/fko_error.c \ + ../../lib/fko_funcs.c \ + ../../lib/fko_message.c \ + ../../lib/fko_nat_access.c \ + ../../lib/fko_rand_value.c \ + ../../lib/fko_server_auth.c \ + ../../lib/fko_timestamp.c \ + ../../lib/fko_hmac.c \ + ../../lib/hmac.c \ + ../../lib/fko_user.c \ + ../../lib/md5.c \ + ../../lib/rijndael.c \ + ../../lib/sha1.c \ + ../../lib/sha2.c \ + ../../lib/strlcpy.c \ + ../../lib/strlcat.c \ + ../../lib/fko_util.c \ + ../../lib/cunit_common.c + +fko_utests_CPPFLAGS = -I ../../lib -I ../../common + +fko_utests_LDFLAGS = -lcunit \ No newline at end of file diff --git a/test/c-unit-tests/README.md b/test/c-unit-tests/README.md index 5a009078..3633de62 100755 --- a/test/c-unit-tests/README.md +++ b/test/c-unit-tests/README.md @@ -1,150 +1,191 @@ -##Build -~~~ -$ ./autogen.sh -$ ./configure --enable-c-unit-tests --prefix=/usr --sysconfdir=/etc --enable-profile-coverage -$ make -~~~ - -~~~ -$ ./test-fwknop.pl --enable-profile-coverage-check --loopback lo -~~~ - -The HAVE_C_UNIT_TESTS constant is used in source files to define c-unit test code. -Source code is built against libcunit. - -##Run test suites -Once the build is complete, two test programs allow the user to run the tests suites: - * fwknopd_utests: program to run fwknopd c unit test suites - * fwknop_utests: program to run fwknop c unit test suites - -~~~ -$ test/c-unit-tests/fwknopd_utests - - CUnit - A unit testing framework for C - Version 2.1-2 - http://cunit.sourceforge.net/ - -Suite: Access test suite - Test: check compare_port_list function ...FAILED - 1. ../../server/access.c:2017 - compare_port_list(acc_pl, in1_pl, 0) == 0 - -Run Summary: Type Total Ran Passed Failed Inactive - suites 1 1 n/a 0 0 - tests 1 1 0 1 0 - asserts 6 6 5 1 n/a - -Elapsed time = 0.000 seconds -~~~ - -~~~ -$ test/c-unit-tests/fwknop_utests - - CUnit - A unit testing framework for C - Version 2.1-2 - http://cunit.sourceforge.net/ - -Suite: Config init test suite - Test: Check critcial vars ...passed - Test: Check var_bitmask functions ...passed - -Run Summary: Type Total Ran Passed Failed Inactive - suites 1 1 n/a 0 0 - tests 2 2 2 0 0 - asserts 12 12 12 0 n/a - -Elapsed time = 0.000 seconds -~~~ - -##Manage C unit tests -C unit tests are implemented in source files directly and registered in a test suite. -All test suites are then added to fwknopd or fwknop test programs - -In order to add new tests, the user must follow the below steps: - * Declare the tests suite - * Create one or more unit tests - * Create a function to register new tests - -###Declare the tests suite - -In access.c file: - -~~~ - #ifdef HAVE_C_UNIT_TESTS - DECLARE_TEST_SUITE(access, "Access test suite"); - #endif -~~~ - -In the above example, we create a test suite using the DECLARE_TEST_SUITE macro: - * the test suite is named "access". - * the test suite description is "Access test suite" and is displayed on the console - when the test program is executed - -###Create unit tests - -In access.c file: - -#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 */ -} - -#endif /* HAVE_C_UNIT_TESTS */ -~~~ - -In the above example, we create a c-unit test using the DECLARE_UTEST macro: - * the unit test is named "compare_port_list". This id must be unique - * the unit test description is "check compare_port_list function" and is displayed on the console - when the test program is executed - -###Create a function to register new tests - -In access.c file: - -~~~ -#ifdef HAVE_C_UNIT_TESTS - -int register_ts_access(void) -{ - ts_init(&TEST_SUITE(access), TEST_SUITE_DESCR(access), NULL, NULL); - 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 */ -~~~ - -In access.h file: - -~~~ -#ifdef HAVE_C_UNIT_TESTS -int register_ts_access(void); -#endif -~~~ -In fwknopd_utests.c file: - -~~~ -static void register_test_suites(void) -{ - register_ts_access(); -} -~~~ - -The register_ts_access function create the new test suite and add unit test to it. - -###Check gcov coverage \ No newline at end of file +# Build +~~~ +$ ./autogen.sh +$ ./configure --enable-c-unit-tests --prefix=/usr --sysconfdir=/etc --enable-profile-coverage +$ make +~~~ + +~~~ +$ ./test-fwknop.pl --enable-profile-coverage-check --loopback lo +~~~ + +The HAVE_C_UNIT_TESTS constant is used in source files to define c-unit test code. +Source code is built against libcunit. + +# Run test suites +Once the build is complete, two test programs allow the user to run the tests suites: + + * fwknopd_utests: program to run fwknopd c unit test suites + * fwknop_utests: program to run fwknop c unit test suites + +~~~ +$ test/c-unit-tests/fwknopd_utests + + CUnit - A unit testing framework for C - Version 2.1-2 + http://cunit.sourceforge.net/ + +Suite: Access test suite + Test: check compare_port_list function ...FAILED + 1. ../../server/access.c:2017 - compare_port_list(acc_pl, in1_pl, 0) == 0 + +Run Summary: Type Total Ran Passed Failed Inactive + suites 1 1 n/a 0 0 + tests 1 1 0 1 0 + asserts 6 6 5 1 n/a + +Elapsed time = 0.000 seconds +~~~ + +~~~ +$ test/c-unit-tests/fwknop_utests + + CUnit - A unit testing framework for C - Version 2.1-2 + http://cunit.sourceforge.net/ + +Suite: Config init test suite + Test: Check critcial vars ...passed + Test: Check var_bitmask functions ...passed + +Run Summary: Type Total Ran Passed Failed Inactive + suites 1 1 n/a 0 0 + tests 2 2 2 0 0 + asserts 12 12 12 0 n/a + +Elapsed time = 0.000 seconds +~~~ + +# Manage C unit tests +C unit tests are implemented in source files directly and registered in a test suite. +All test suites are then added to fwknopd or fwknop test programs + +In order to add new tests, the user must follow the below steps: + + * Declare the tests suite + * Declare an initialization function + * Declare a cleanup function + * Create one or more unit tests + * Create a function to register new tests + +## Declare the tests suite + +In access.c file: + +~~~ + #ifdef HAVE_C_UNIT_TESTS + DECLARE_TEST_SUITE(access, "Access test suite"); + #endif +~~~ + +In the above example, we create a test suite using the DECLARE_TEST_SUITE macro: + + * the test suite is named "access". + * the test suite description is "Access test suite" and is displayed on the console + when the test program is executed + +## Declare an initialization function + +To declare an init function to execute before runnning the test suite to initiize the context use the DECLARE_TEST_SUITE_INIT macro as follow: + + DECLARE_TEST_SUITE_INIT(filename) + +~~~ +DECLARE_TEST_SUITE_INIT(access) +{ + log_set_verbosity(LOG_VERBOSITY_ERROR); + return 0; +} +~~~ + +In the above example, the log message verbosity is decreeased to error to only display error messages since debug messages are useless. + +## Declare a cleanup function + +To declare a cleanup function to execute at the end of the test suite to cleanup the context use the DECLARE_TEST_SUITE_CLEANUP macro as follow: + + DECLARE_TEST_SUITE_CLEANUP(filename) + +~~~ +DECLARE_TEST_SUITE_CLEANUP(access) +{ + return 0; +} +~~~ + +In the above example, the cleanup function returns 0 and does strictly nothing. There is no need to declare such function and +thus could be replaced by a NULL pointer at test suite initialization + +## Create unit tests + +In access.c file: + +~~~ +#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 */ +} + +#endif /* HAVE_C_UNIT_TESTS */ +~~~ + +In the above example, we create a c-unit test using the DECLARE_UTEST macro: + + * the unit test is named "compare_port_list". This id must be unique + * the unit test description is "check compare_port_list function" and is displayed on the console + when the test program is executed + +## Create a function to register new tests + +In access.c file: + +~~~ +#ifdef HAVE_C_UNIT_TESTS + +int register_ts_access(void) +{ + ts_init(&TEST_SUITE(access), TEST_SUITE_DESCR(access), TEST_SUITE_INIT(access), TEST_SUITE_CLEANUP(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 */ +~~~ + +If no init or cleanup function is defined, they have to be replaced by a NULL pointer at test suite initialization. + +In access.h file: + +~~~ +#ifdef HAVE_C_UNIT_TESTS +int register_ts_access(void); +#endif +~~~ +In fwknopd_utests.c file: + +~~~ +static void register_test_suites(void) +{ + register_ts_access(); +} +~~~ + +The register_ts_access function create the new test suite and add unit test to it. + +## Check gcov coverage \ No newline at end of file diff --git a/test/c-unit-tests/fko_utests.c b/test/c-unit-tests/fko_utests.c new file mode 100755 index 00000000..6312d2db --- /dev/null +++ b/test/c-unit-tests/fko_utests.c @@ -0,0 +1,34 @@ +#include "CUnit/Basic.h" + +#include "fko.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_fko_decode(); +} + +/* 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(); +} From 29a5a9804d68395c10a176bd9baec1ecbe1a701a Mon Sep 17 00:00:00 2001 From: Franck Joncourt Date: Tue, 30 Dec 2014 20:49:14 +0100 Subject: [PATCH 11/12] * Added libfko unit tests --- lib/fko_decode.c | 49 +++++++++++++- test/c-unit-tests/Makefile.am | 7 +- test/c-unit-tests/README.md | 123 ++++++++++++++++++++++------------ 3 files changed, 130 insertions(+), 49 deletions(-) mode change 100755 => 100644 test/c-unit-tests/README.md diff --git a/lib/fko_decode.c b/lib/fko_decode.c index 573bf452..c680f9c5 100644 --- a/lib/fko_decode.c +++ b/lib/fko_decode.c @@ -36,6 +36,9 @@ #define FIELD_PARSERS 9 +/* Char used to separate SPA fields in an SPA packet */ +#define SPA_FIELD_SEPARATOR ":" + #ifdef HAVE_C_UNIT_TESTS DECLARE_TEST_SUITE(fko_decode, "FKO decode test suite"); #endif @@ -598,15 +601,57 @@ fko_decode_spa_data(fko_ctx_t ctx) DECLARE_UTEST(num_fields, "Count the number of SPA fields in a SPA packet") { - CU_ASSERT(num_fields("abcdef") == 0); - CU_ASSERT(num_fields("abcdef:gh") == 1); + int ix_field=0; + char spa_packet[(MAX_SPA_FIELDS+1)*3]; + /* Zeroing the spa packet */ + memset(spa_packet, 0, sizeof(spa_packet)); + + /* Check we are able to count the number of SPA fields */ + for(ix_field=0 ; ix_field<=MAX_SPA_FIELDS+2 ; ix_field++) + { + strcat(spa_packet, "x"); + CU_ASSERT(num_fields(spa_packet) == ix_field); + strcat(spa_packet, SPA_FIELD_SEPARATOR); + } + + /* Check for possible overflow */ + strcat(spa_packet, "x"); + CU_ASSERT(num_fields(spa_packet) == MAX_SPA_FIELDS + 2); + strcat(spa_packet, "x"); + strcat(spa_packet, SPA_FIELD_SEPARATOR); + CU_ASSERT(num_fields(spa_packet) == MAX_SPA_FIELDS + 2); +} + +DECLARE_UTEST(last_field, "Count the number of bytes to the last :") +{ + int ix_field; + char spa_packet[(MAX_SPA_FIELDS+1)*3]; + + /* Zeroing the spa packet */ + memset(spa_packet, 0, sizeof(spa_packet)); + + /* Check for a valid count when the number of field is less than MAX_SPA_FIELDS */ + CU_ASSERT(last_field("a:") == 2); + CU_ASSERT(last_field("ab:abc:") == 7); + CU_ASSERT(last_field("abc:abcd:") == 9); + CU_ASSERT(last_field("abc:abcd:abc") == 9); + + + /* */ + for(ix_field=0 ; ix_field<=MAX_SPA_FIELDS+2 ; ix_field++) + { + strcat(spa_packet, "x"); + strcat(spa_packet, SPA_FIELD_SEPARATOR); + } + CU_ASSERT(last_field(spa_packet) == ((MAX_SPA_FIELDS+2)*2)); } int register_ts_fko_decode(void) { ts_init(&TEST_SUITE(fko_decode), TEST_SUITE_DESCR(fko_decode), NULL, NULL); ts_add_utest(&TEST_SUITE(fko_decode), UTEST_FCT(num_fields), UTEST_DESCR(num_fields)); + ts_add_utest(&TEST_SUITE(fko_decode), UTEST_FCT(last_field), UTEST_DESCR(last_field)); return register_ts(&TEST_SUITE(fko_decode)); } diff --git a/test/c-unit-tests/Makefile.am b/test/c-unit-tests/Makefile.am index f82679b0..7ad3be15 100755 --- a/test/c-unit-tests/Makefile.am +++ b/test/c-unit-tests/Makefile.am @@ -70,8 +70,9 @@ fko_utests_SOURCES = fko_utests.c \ ../../lib/strlcpy.c \ ../../lib/strlcat.c \ ../../lib/fko_util.c \ - ../../lib/cunit_common.c + ../../lib/cunit_common.c \ + ../../lib/gpgme_funcs.c -fko_utests_CPPFLAGS = -I ../../lib -I ../../common +fko_utests_CPPFLAGS = -I ../../lib -I ../../common $(GPGME_CFLAGS) -fko_utests_LDFLAGS = -lcunit \ No newline at end of file +fko_utests_LDFLAGS = -lcunit $(GPGME_LIBS) diff --git a/test/c-unit-tests/README.md b/test/c-unit-tests/README.md old mode 100755 new mode 100644 index 3633de62..9066aff9 --- a/test/c-unit-tests/README.md +++ b/test/c-unit-tests/README.md @@ -1,4 +1,20 @@ # Build + +C unit library is used to perform c unit testing. Source code associated to those tests +must be started with + +~~~ +#ifdef HAVE_C_UNIT_TESTS +~~~ + +and closed with: + +~~~ +#endif /* HAVE_C_UNIT_TESTS */ +~~~ +In order to build the test suite use the following commands with the **--enable-c-unit-tests** +switch + ~~~ $ ./autogen.sh $ ./configure --enable-c-unit-tests --prefix=/usr --sysconfdir=/etc --enable-profile-coverage @@ -6,24 +22,19 @@ $ make ~~~ ~~~ -$ ./test-fwknop.pl --enable-profile-coverage-check --loopback lo +$ ./test-fwknop.pl --enable-profile-coverage-check --loopback lo --client-only-mode ~~~ -The HAVE_C_UNIT_TESTS constant is used in source files to define c-unit test code. -Source code is built against libcunit. - # Run test suites -Once the build is complete, two test programs allow the user to run the tests suites: +Once the build is complete, three test programs allow the user to run the tests suites: * fwknopd_utests: program to run fwknopd c unit test suites * fwknop_utests: program to run fwknop c unit test suites + * fko_utests: program to run fko c unit test suites ~~~ $ test/c-unit-tests/fwknopd_utests - CUnit - A unit testing framework for C - Version 2.1-2 - http://cunit.sourceforge.net/ - Suite: Access test suite Test: check compare_port_list function ...FAILED 1. ../../server/access.c:2017 - compare_port_list(acc_pl, in1_pl, 0) == 0 @@ -39,9 +50,6 @@ Elapsed time = 0.000 seconds ~~~ $ test/c-unit-tests/fwknop_utests - CUnit - A unit testing framework for C - Version 2.1-2 - http://cunit.sourceforge.net/ - Suite: Config init test suite Test: Check critcial vars ...passed Test: Check var_bitmask functions ...passed @@ -54,21 +62,36 @@ Run Summary: Type Total Ran Passed Failed Inactive Elapsed time = 0.000 seconds ~~~ +~~~ +$ test/c-unit-tests/fko_utests + +Suite: FKO decode test suite + Test: Count the number of SPA fields in a SPA packet ...passed + Test: Count the number of bytes to the last : ...passed + +Run Summary: Type Total Ran Passed Failed Inactive + suites 1 1 n/a 0 0 + tests 2 2 2 0 0 + asserts 19 19 19 0 n/a + +Elapsed time = 0.000 seconds +~~~ + # Manage C unit tests -C unit tests are implemented in source files directly and registered in a test suite. -All test suites are then added to fwknopd or fwknop test programs +C unit tests are implemented in source files and registered in a test suite. +All test suites are then added to fwknopd, fwknop or fko test programs In order to add new tests, the user must follow the below steps: - * Declare the tests suite + * Declare a test suite * Declare an initialization function - * Declare a cleanup function + * Declare a clean up function * Create one or more unit tests * Create a function to register new tests + +## Declare a test suite -## Declare the tests suite - -In access.c file: +In *source* file: ~~~ #ifdef HAVE_C_UNIT_TESTS @@ -76,7 +99,7 @@ In access.c file: #endif ~~~ -In the above example, we create a test suite using the DECLARE_TEST_SUITE macro: +In the above example, we create a test suite using the **DECLARE_TEST_SUITE** macro: * the test suite is named "access". * the test suite description is "Access test suite" and is displayed on the console @@ -84,9 +107,10 @@ In the above example, we create a test suite using the DECLARE_TEST_SUITE macro: ## Declare an initialization function -To declare an init function to execute before runnning the test suite to initiize the context use the DECLARE_TEST_SUITE_INIT macro as follow: +Before running the test suite, an init function can be used to initialize the test suite context. +To declare such a function use the **DECLARE_TEST_SUITE_INIT** macro. - DECLARE_TEST_SUITE_INIT(filename) +In *source* file: ~~~ DECLARE_TEST_SUITE_INIT(access) @@ -96,13 +120,17 @@ DECLARE_TEST_SUITE_INIT(access) } ~~~ -In the above example, the log message verbosity is decreeased to error to only display error messages since debug messages are useless. +In the above example, the log message verbosity is decreased to error level to only display error +messages since debug messages are too verbose. -## Declare a cleanup function +In some cases, there is no need for such a function and thus this declaration is not mandatory. -To declare a cleanup function to execute at the end of the test suite to cleanup the context use the DECLARE_TEST_SUITE_CLEANUP macro as follow: +## Declare a clean-up function - DECLARE_TEST_SUITE_CLEANUP(filename) +In order to clean up the context at the end of the test suite, it is possible to declare a clean up +function with the **DECLARE_TEST_SUITE_CLEANUP** macro + +In *source* file: ~~~ DECLARE_TEST_SUITE_CLEANUP(access) @@ -111,17 +139,17 @@ DECLARE_TEST_SUITE_CLEANUP(access) } ~~~ -In the above example, the cleanup function returns 0 and does strictly nothing. There is no need to declare such function and -thus could be replaced by a NULL pointer at test suite initialization +In the above example, the clean up function returns 0 and does strictly nothing. + +In some cases, there is no need for such function and thus this declaration is not mandatory. ## Create unit tests -In access.c file: +In *source* file: ~~~ #ifdef HAVE_C_UNIT_TESTS - DECLARE_UTEST(compare_port_list, "check compare_port_list function") { acc_port_list_t *in1_pl = NULL; @@ -134,26 +162,29 @@ DECLARE_UTEST(compare_port_list, "check compare_port_list function") 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 */ + CU_ASSERT(compare_port_list(in1_pl, acc_pl, 1) == 1); + CU_ASSERT(compare_port_list(in2_pl, acc_pl, 1) == 1); + CU_ASSERT(compare_port_list(in1_pl, acc_pl, 0) == 1); + CU_ASSERT(compare_port_list(in2_pl, acc_pl, 0) == 1); + CU_ASSERT(compare_port_list(acc_pl, in1_pl, 0) == 0); + CU_ASSERT(compare_port_list(acc_pl, in2_pl, 0) == 1); } #endif /* HAVE_C_UNIT_TESTS */ ~~~ -In the above example, we create a c-unit test using the DECLARE_UTEST macro: +In the above example, we create a c-unit test using the **DECLARE_UTEST** macro: - * the unit test is named "compare_port_list". This id must be unique - * the unit test description is "check compare_port_list function" and is displayed on the console + * The unit test is named "compare_port_list" ; This id must be unique + * The unit test description is "check compare_port_list function" and is displayed on the console when the test program is executed ## Create a function to register new tests -In access.c file: +We have previously declared unit tests, but they have to be registered to a test suite +to be executed. + +In *source* file: ~~~ #ifdef HAVE_C_UNIT_TESTS @@ -165,19 +196,25 @@ int register_ts_access(void) return register_ts(&TEST_SUITE(access)); } + #endif /* HAVE_C_UNIT_TESTS */ ~~~ -If no init or cleanup function is defined, they have to be replaced by a NULL pointer at test suite initialization. +If no init or cleanup function is defined, they have to be replaced by a NULL pointer +at test suite initialization : **ts_init** -In access.h file: +Each unit test must be added using **ts_add_utest** function. + +In *header* file, add the register function prototype as follows: ~~~ #ifdef HAVE_C_UNIT_TESTS int register_ts_access(void); #endif ~~~ -In fwknopd_utests.c file: + +In the unit test program, add the test suite to the current list of existing +test suite. ~~~ static void register_test_suites(void) @@ -186,6 +223,4 @@ static void register_test_suites(void) } ~~~ -The register_ts_access function create the new test suite and add unit test to it. - ## Check gcov coverage \ No newline at end of file From 1c81aef39d426d7426ef07a692fb243e61e254b1 Mon Sep 17 00:00:00 2001 From: Franck Joncourt Date: Wed, 31 Dec 2014 09:51:08 +0100 Subject: [PATCH 12/12] Fixed file permissions --- client/config_init.c | 0 client/config_init.h | 0 client/getpasswd.c | 0 client/getpasswd.h | 0 common/Makefile.am | 0 common/common.h | 0 configure.ac | 0 lib/Makefile.am | 0 lib/cunit_common.c | 0 server/Makefile.am | 0 server/access.c | 0 server/access.h | 0 server/fwknopd.c | 0 server/utils.c | 0 14 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 client/config_init.c mode change 100755 => 100644 client/config_init.h mode change 100755 => 100644 client/getpasswd.c mode change 100755 => 100644 client/getpasswd.h mode change 100755 => 100644 common/Makefile.am mode change 100755 => 100644 common/common.h mode change 100755 => 100644 configure.ac mode change 100755 => 100644 lib/Makefile.am mode change 100755 => 100644 lib/cunit_common.c mode change 100755 => 100644 server/Makefile.am mode change 100755 => 100644 server/access.c mode change 100755 => 100644 server/access.h mode change 100755 => 100644 server/fwknopd.c mode change 100755 => 100644 server/utils.c diff --git a/client/config_init.c b/client/config_init.c old mode 100755 new mode 100644 diff --git a/client/config_init.h b/client/config_init.h old mode 100755 new mode 100644 diff --git a/client/getpasswd.c b/client/getpasswd.c old mode 100755 new mode 100644 diff --git a/client/getpasswd.h b/client/getpasswd.h old mode 100755 new mode 100644 diff --git a/common/Makefile.am b/common/Makefile.am old mode 100755 new mode 100644 diff --git a/common/common.h b/common/common.h old mode 100755 new mode 100644 diff --git a/configure.ac b/configure.ac old mode 100755 new mode 100644 diff --git a/lib/Makefile.am b/lib/Makefile.am old mode 100755 new mode 100644 diff --git a/lib/cunit_common.c b/lib/cunit_common.c old mode 100755 new mode 100644 diff --git a/server/Makefile.am b/server/Makefile.am old mode 100755 new mode 100644 diff --git a/server/access.c b/server/access.c old mode 100755 new mode 100644 diff --git a/server/access.h b/server/access.h old mode 100755 new mode 100644 diff --git a/server/fwknopd.c b/server/fwknopd.c old mode 100755 new mode 100644 diff --git a/server/utils.c b/server/utils.c old mode 100755 new mode 100644