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