From: Andreas Reichel <[email protected]> Add test for bgenv_init faking probe_config_partition.
Signed-off-by: Andreas Reichel <[email protected]> --- env/env_api_fat.c | 6 +-- include/test-interface.h | 2 + tools/tests/Makefile.am | 29 +++++++++-- tools/tests/test_bgenv_init_retval.c | 96 ++++++++++++++++++++++++++++++++++++ tools/tests/test_main.c | 34 +++++++++++++ 5 files changed, 161 insertions(+), 6 deletions(-) create mode 100644 tools/tests/test_bgenv_init_retval.c create mode 100644 tools/tests/test_main.c diff --git a/env/env_api_fat.c b/env/env_api_fat.c index a344fcb..8eb1454 100644 --- a/env/env_api_fat.c +++ b/env/env_api_fat.c @@ -20,7 +20,7 @@ bool bgenv_verbosity = false; -static EBGENVKEY bgenv_str2enum(char *key) +EBGENVKEY bgenv_str2enum(char *key) { if (strncmp(key, "kernelfile", strlen("kernelfile") + 1) == 0) { return EBGENV_KERNELFILE; @@ -120,8 +120,8 @@ bool write_env(CONFIG_PART *part, BG_ENVDATA *env) return result; } -static CONFIG_PART config_parts[ENV_NUM_CONFIG_PARTS]; -static BG_ENVDATA envdata[ENV_NUM_CONFIG_PARTS]; +CONFIG_PART config_parts[ENV_NUM_CONFIG_PARTS]; +BG_ENVDATA envdata[ENV_NUM_CONFIG_PARTS]; bool bgenv_init() { diff --git a/include/test-interface.h b/include/test-interface.h index 36b0a62..bd2572d 100644 --- a/include/test-interface.h +++ b/include/test-interface.h @@ -22,4 +22,6 @@ bool probe_config_file(CONFIG_PART *cfgpart); bool probe_config_partitions(CONFIG_PART *cfgparts); bool mount_partition(CONFIG_PART *cfgpart); +EBGENVKEY bgenv_str2enum(char *key); + #endif // __TEST_INTERFACE_H__ diff --git a/tools/tests/Makefile.am b/tools/tests/Makefile.am index 9b2344f..f5bc4a4 100644 --- a/tools/tests/Makefile.am +++ b/tools/tests/Makefile.am @@ -16,7 +16,10 @@ AM_CFLAGS = \ -I$(top_srcdir)/include \ -I$(top_srcdir)/swupdate-adapter \ -I$(top_srcdir)/tools \ + -I$(top_srcdir)/tools/tests \ -I$(top_srcdir)/testing/fff \ + -I$(top_srcdir) \ + -include config.h \ -Wno-unused-parameter \ -Wmissing-prototypes \ -fshort-wchar \ @@ -24,11 +27,31 @@ AM_CFLAGS = \ -D_GNU_SOURCE \ -g +libtest_env_api_fat_a_SRC = \ + ../../env/env_api.c \ + ../../env/env_api_fat.c \ + ../../tools/ebgpart.c \ + ../../env/env_config_file.c \ + ../../env/env_config_partitions.c \ + ../../env/env_disk_utils.c \ + ../../env/uservars.c + CLEANFILES = -test_api: - ln -sf /bin/true test_api +noinst_LIBRARIES = libtest_env_api_fat.a +libtest_env_api_fat_a_SOURCES = $(libtest_env_api_fat_a_SRC) + +libenvapi_testlib_fat.a: libtest_env_api_fat.a + $(OBJCOPY) --weaken $^ $@ + +check_PROGRAMS = test_bgenv_init_retval + +FAT_TESTLIB=libenvapi_testlib_fat.a + +SRC_TEST_COMMON=test_main.c -check_PROGRAMS = test_api +test_bgenv_init_retval_CFLAGS = $(AM_CFLAGS) +test_bgenv_init_retval_SOURCES = test_bgenv_init_retval.c $(SRC_TEST_COMMON) +test_bgenv_init_retval_LDADD = $(FAT_TESTLIB) $(LIBCHECK_LIBS) TESTS = $(check_PROGRAMS) diff --git a/tools/tests/test_bgenv_init_retval.c b/tools/tests/test_bgenv_init_retval.c new file mode 100644 index 0000000..27324c9 --- /dev/null +++ b/tools/tests/test_bgenv_init_retval.c @@ -0,0 +1,96 @@ +/* + * EFI Boot Guard + * + * Copyright (c) Siemens AG, 2017 + * + * Authors: + * Andreas Reichel <[email protected]> + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + */ + +#include <stdlib.h> +#include <check.h> +#include <fff.h> +#include <env_api.h> +#include <env_config_file.h> +#include <env_config_partitions.h> + +DEFINE_FFF_GLOBALS; + +static char *devpath = "/dev/nobrain"; + +bool read_env(CONFIG_PART *part, BG_ENVDATA *env); + +Suite *env_api_fat_suite(void); +bool probe_config_partitions_custom_fake(CONFIG_PART *cfgpart); +bool read_env_custom_fake(CONFIG_PART *cp, BG_ENVDATA *env); + +Suite *ebg_test_suite(void); + +bool probe_config_partitions_custom_fake(CONFIG_PART *cfgpart) +{ + for (int i = 0; i < ENV_NUM_CONFIG_PARTS; i++) { + cfgpart[i].devpath = devpath; + } + return true; +} + +bool read_env_custom_fake(CONFIG_PART *cp, BG_ENVDATA *env) +{ + if (!env) { + return false; + } + memset(env, 0, sizeof(BG_ENVDATA)); + return true; +} + +FAKE_VALUE_FUNC(bool, probe_config_partitions, CONFIG_PART *); +FAKE_VALUE_FUNC(bool, read_env, CONFIG_PART *, BG_ENVDATA *); + +START_TEST(env_api_fat_test_bgenv_init_retval) +{ + bool result; + /* In this unit test, contents of environment data are + * faked to be all zero + */ + + /* Test if bgenv_init fails if no config partitions are found + */ + RESET_FAKE(probe_config_partitions); + + probe_config_partitions_fake.return_val = false; + + result = bgenv_init(); + + ck_assert(probe_config_partitions_fake.call_count == 1); + ck_assert(result == false); + + /* Test if bgenv_init succeeds if config partitions are found + */ + RESET_FAKE(probe_config_partitions); + + probe_config_partitions_fake.custom_fake = probe_config_partitions_custom_fake; + read_env_fake.custom_fake = read_env_custom_fake; + result = bgenv_init(); + + ck_assert(probe_config_partitions_fake.call_count == 1); + ck_assert(read_env_fake.call_count == ENV_NUM_CONFIG_PARTS); + ck_assert(result == true); +} +END_TEST + +Suite *ebg_test_suite(void) +{ + Suite *s; + TCase *tc_core; + + s = suite_create("env_api_fat"); + + tc_core = tcase_create("Core"); + tcase_add_test(tc_core, env_api_fat_test_bgenv_init_retval); + suite_add_tcase(s, tc_core); + + return s; +} diff --git a/tools/tests/test_main.c b/tools/tests/test_main.c new file mode 100644 index 0000000..a25e092 --- /dev/null +++ b/tools/tests/test_main.c @@ -0,0 +1,34 @@ +/* + * EFI Boot Guard + * + * Copyright (c) Siemens AG, 2017 + * + * Authors: + * Andreas Reichel <[email protected]> + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + */ + +#include <stdlib.h> +#include <check.h> +#include <fff.h> + +extern Suite *ebg_test_suite(void); + +int main(void) +{ + int number_failed; + + Suite *s; + SRunner *sr; + + s = ebg_test_suite(); + sr = srunner_create(s); + + srunner_run_all(sr, CK_NORMAL); + number_failed = srunner_ntests_failed(sr); + srunner_free(sr); + + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} -- 2.14.2 -- You received this message because you are subscribed to the Google Groups "EFI Boot Guard" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/efibootguard-dev/20171102155648.16140-6-andreas.reichel.ext%40siemens.com. For more options, visit https://groups.google.com/d/optout.
