From: Andreas Reichel <[email protected]> Generate fake devices and fake device enumeration to test config partition probing.
Signed-off-by: Andreas Reichel <[email protected]> --- tools/tests/Makefile.am | 9 ++- tools/tests/fake_devices.c | 94 +++++++++++++++++++++++++++++ tools/tests/fake_devices.h | 28 +++++++++ tools/tests/test_probe_config_partitions.c | 95 ++++++++++++++++++++++++++++++ 4 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 tools/tests/fake_devices.c create mode 100644 tools/tests/fake_devices.h create mode 100644 tools/tests/test_probe_config_partitions.c diff --git a/tools/tests/Makefile.am b/tools/tests/Makefile.am index f5bc4a4..72e9a49 100644 --- a/tools/tests/Makefile.am +++ b/tools/tests/Makefile.am @@ -44,7 +44,8 @@ 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 +check_PROGRAMS = test_bgenv_init_retval \ + test_probe_config_partitions FAT_TESTLIB=libenvapi_testlib_fat.a @@ -54,4 +55,10 @@ 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) +test_probe_config_partitions_CFLAGS = $(AM_CFLAGS) +test_probe_config_partitions_SOURCES = test_probe_config_partitions.c \ + fake_devices.c \ + $(SRC_TEST_COMMON) +test_probe_config_partitions_LDADD = $(FAT_TESTLIB) $(LIBCHECK_LIBS) + TESTS = $(check_PROGRAMS) diff --git a/tools/tests/fake_devices.c b/tools/tests/fake_devices.c new file mode 100644 index 0000000..58a47bd --- /dev/null +++ b/tools/tests/fake_devices.c @@ -0,0 +1,94 @@ +/* + * 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 <env_api.h> +#include <env_config_file.h> +#include <env_config_partitions.h> +#include <fake_devices.h> + +PedDevice *fake_devices; +int num_fake_devices; + +void allocate_fake_devices(int n) +{ + fake_devices = (PedDevice *)malloc(n * sizeof(PedDevice)); + for (char i = 0; i < n; i++) { + asprintf(&fake_devices[i].model, "%s", "Fake Device"); + asprintf(&fake_devices[i].path, "/dev/nobrain_%c", 'a' + i); + fake_devices[i].part_list = NULL; + fake_devices[i].next = NULL; + } + num_fake_devices = n; + for (char i = n - 1; i > 0; i--) { + fake_devices[i-1].next = &fake_devices[i]; + } +} + +void add_fake_partition(int devnum) +{ + PedPartition **pp = &fake_devices[devnum].part_list; + + int16_t num = 0; + while (*pp) { + pp = &(*pp)->next; + num++; + } + *pp = (PedPartition *)malloc(sizeof(PedPartition)); + (*pp)->num = num; + (*pp)->fs_type = (PedFileSystemType *)malloc(sizeof(PedFileSystemType)); + asprintf(&(*pp)->fs_type->name, "%s", "fat16"); + (*pp)->next = NULL; +} + +void remove_fake_partitions(int n) +{ + PedPartition *pp = fake_devices[n].part_list; + PedPartition *next; + while(pp) { + next = pp->next; + if (!pp->fs_type) + goto skip_fstype; + if (pp->fs_type->name) + free(pp->fs_type->name); + free(pp->fs_type); +skip_fstype: + free(pp); + pp = next; + } +} + +void free_fake_devices() +{ + if (!fake_devices) { + return; + } + + for (int i = 0; i < num_fake_devices; i++) { + if (fake_devices[i].model) + free(fake_devices[i].model); + if (fake_devices[i].path) + free(fake_devices[i].path); + if (fake_devices[i].part_list) + remove_fake_partitions(i); + } + + free(fake_devices); +} + +PedDevice *ped_device_get_next_custom_fake(const PedDevice *dev) +{ + if (!dev) + return fake_devices; + + return dev->next; +} diff --git a/tools/tests/fake_devices.h b/tools/tests/fake_devices.h new file mode 100644 index 0000000..aa5f423 --- /dev/null +++ b/tools/tests/fake_devices.h @@ -0,0 +1,28 @@ +/* + * 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. + */ + +#ifndef __FAKE_DEVICES_H__ +#define __FAKE_DEVICES_H__ + +#include <ebgpart.h> + +extern PedDevice *fake_devices; +extern int num_fake_devices; + +void allocate_fake_devices(int n); +void add_fake_partition(int devnum); +void remove_fake_partitions(int n); +void free_fake_devices(void); + +PedDevice *ped_device_get_next_custom_fake(const PedDevice *dev); + +#endif // __FAKE_DEVICES_H__ diff --git a/tools/tests/test_probe_config_partitions.c b/tools/tests/test_probe_config_partitions.c new file mode 100644 index 0000000..4536aea --- /dev/null +++ b/tools/tests/test_probe_config_partitions.c @@ -0,0 +1,95 @@ +/* + * 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> +#include <fake_devices.h> + +DEFINE_FFF_GLOBALS; + +Suite *ebg_test_suite(void); + +bool read_env_custom_fake(CONFIG_PART *cp, BG_ENVDATA *env); +bool read_env(CONFIG_PART *part, BG_ENVDATA *env); + +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, read_env, CONFIG_PART *, BG_ENVDATA *); +FAKE_VOID_FUNC(ped_device_probe_all); +FAKE_VALUE_FUNC(PedDevice *, ped_device_get_next, const PedDevice *); + +START_TEST(env_api_fat_test_probe_config_partitions) +{ + bool result; + /* In this unit test, contents of environment data are + * faked to be all zero + */ + + /* Test if bgenv_init fails if no block devices are found + */ + RESET_FAKE(ped_device_probe_all); + RESET_FAKE(ped_device_get_next); + + ped_device_get_next_fake.return_val = NULL; + + result = bgenv_init(); + + ck_assert(ped_device_probe_all_fake.call_count == 1); + ck_assert(result == false); + + /* Test if bgenv_init fails if a device with two partitions is found + * but now config file is there + */ + RESET_FAKE(ped_device_probe_all); + RESET_FAKE(ped_device_get_next); + + allocate_fake_devices(1); + + for (int i = 0; i < ENV_NUM_CONFIG_PARTS; i++) { + add_fake_partition(0); + } + + ped_device_get_next_fake.custom_fake = ped_device_get_next_custom_fake; + result = bgenv_init(); + + free_fake_devices(); + + ck_assert(ped_device_probe_all_fake.call_count == 1); + ck_assert(ped_device_get_next_fake.call_count == 2); + ck_assert(result == false); +} +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_probe_config_partitions); + suite_add_tcase(s, tc_core); + + return s; +} -- 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-7-andreas.reichel.ext%40siemens.com. For more options, visit https://groups.google.com/d/optout.
