First layout to add c unit testing support to fwknop.

This commit is contained in:
Franck Joncourt 2014-08-24 21:05:44 +02:00
parent 00a057a09d
commit 306dd0f685
9 changed files with 204 additions and 0 deletions

57
client/config_init.c Normal file → Executable file
View File

@ -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 */

4
client/config_init.h Normal file → Executable file
View File

@ -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***/

View File

@ -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***/

View File

@ -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 */

View File

@ -42,6 +42,11 @@
#include <fiu-control.h>
#endif
/* Include cunit header if c unit testing support is enabled. */
#ifdef HAVE_C_UNIT_TESTS
#include "CUnit/Basic.h"
#endif
#include <stdio.h>
#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***/

16
configure.ac Normal file → Executable file
View File

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

16
test/c-unit-tests/Makefile.am Executable file
View File

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

34
test/c-unit-tests/fko_utests.c Executable file
View File

@ -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();
}

6
test/c-unit-tests/fko_utests.h Executable file
View File

@ -0,0 +1,6 @@
#ifndef FKO_UTESTS_H
#define FKO_UTESTS_H
#endif /* FKO_UTESTS_H */