From: Andreas Reichel <[email protected]>

Emulate config files to test calls to probe_config_file.

Signed-off-by: Andreas Reichel <[email protected]>
---
 .travis-build.sh                     |   2 +
 tools/tests/Makefile.am              |   9 +-
 tools/tests/test_probe_config_file.c | 191 +++++++++++++++++++++++++++++++++++
 3 files changed, 201 insertions(+), 1 deletion(-)
 create mode 100644 tools/tests/test_probe_config_file.c

diff --git a/.travis-build.sh b/.travis-build.sh
index 6c8d79c..4e58407 100755
--- a/.travis-build.sh
+++ b/.travis-build.sh
@@ -114,6 +114,8 @@ case "$TARGET_EFFECTIVE" in
         suppress+=" --suppress=unusedFunction:env/fatvars.c"
         suppress+=" --suppress=unusedFunction:tools/tests/test_environment.c"
         suppress+=" --suppress=unusedFunction:env/env_api_fat.c"
+        # Some functions are used by linker wrapping
+        suppress+=" 
--suppress=unusedFunction:tools/tests/test_probe_config_file.c"
         # EFI uses void* as ImageBase needed for further calculations
         suppress+=" --suppress=arithOperationsOnVoidPointer:main.c"
 
diff --git a/tools/tests/Makefile.am b/tools/tests/Makefile.am
index 72e9a49..957a044 100644
--- a/tools/tests/Makefile.am
+++ b/tools/tests/Makefile.am
@@ -45,7 +45,8 @@ libenvapi_testlib_fat.a: libtest_env_api_fat.a
        $(OBJCOPY) --weaken $^ $@
 
 check_PROGRAMS = test_bgenv_init_retval \
-                test_probe_config_partitions
+                test_probe_config_partitions \
+                test_probe_config_file
 
 FAT_TESTLIB=libenvapi_testlib_fat.a
 
@@ -61,4 +62,10 @@ test_probe_config_partitions_SOURCES = 
test_probe_config_partitions.c \
                                       $(SRC_TEST_COMMON)
 test_probe_config_partitions_LDADD = $(FAT_TESTLIB) $(LIBCHECK_LIBS)
 
+test_probe_config_file_CFLAGS = $(AM_CFLAGS) -Wl,--wrap=probe_config_file
+test_probe_config_file_SOURCES = test_probe_config_file.c fake_devices.c \
+                                $(SRC_TEST_COMMON)
+test_probe_config_file_LDADD = $(FAT_TESTLIB) $(LIBCHECK_LIBS)
+
+
 TESTS = $(check_PROGRAMS)
diff --git a/tools/tests/test_probe_config_file.c 
b/tools/tests/test_probe_config_file.c
new file mode 100644
index 0000000..c32fb61
--- /dev/null
+++ b/tools/tests/test_probe_config_file.c
@@ -0,0 +1,191 @@
+/*
+ * 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 <errno.h>
+#include <sys/queue.h>
+#include <check.h>
+#include <fff.h>
+#include <env_api.h>
+#include <env_config_file.h>
+#include <env_config_partitions.h>
+#include <ebgpart.h>
+#include <stdio.h>
+#include <env_disk_utils.h>
+#include <fake_devices.h>
+
+DEFINE_FFF_GLOBALS;
+
+Suite *ebg_test_suite(void);
+
+char *get_mountpoint_custom_fake(char *devpath);
+void delete_temp_files(void);
+
+bool __wrap_probe_config_file(CONFIG_PART *);
+bool __real_probe_config_file(CONFIG_PART *);
+int probe_config_file_call_count;
+
+char *fake_mountpoint = "/tmp/tmp.XXXXXX";
+
+struct stailhead *headp;
+struct fake_env_file_path {
+       char *path;
+       STAILQ_ENTRY(fake_env_file_path) fake_env_file_paths;
+};
+STAILQ_HEAD(stailhead, fake_env_file_path) head = 
STAILQ_HEAD_INITIALIZER(head);
+
+char *get_mountpoint_custom_fake(char *devpath)
+{
+       char *buff = NULL;
+       char *tmpdir = NULL;
+       char *tmpfile = NULL;
+
+       if (asprintf(&tmpdir, "%s", fake_mountpoint) == -1)
+       {
+               tmpdir = NULL;
+               goto fake_mountpoint_error;
+       }
+
+       tmpdir = mkdtemp(tmpdir);
+       if (!tmpdir)
+               goto fake_mountpoint_error;
+
+       if (asprintf(&buff, "%s", tmpdir) == -1) {
+               buff = NULL;
+               goto fake_mountpoint_error;
+       }
+
+       if (asprintf(&tmpfile, "%s/%s", tmpdir, FAT_ENV_FILENAME) == -1) {
+               tmpfile = NULL;
+               goto fake_mountpoint_error;
+       }
+
+       /* create a fake environment file
+        */
+       FILE *temp_env_file = fopen(tmpfile, "w");
+
+       BG_ENVDATA env_data;
+       memset(&env_data, 0, sizeof(BG_ENVDATA));
+       fwrite(&env_data, sizeof(BG_ENVDATA), 1, temp_env_file);
+       fclose(temp_env_file);
+
+       free(tmpfile);
+       free(tmpdir);
+
+       struct fake_env_file_path *fefp;
+       fefp = malloc(sizeof(struct fake_env_file_path));
+
+       /* If possibly store created temporary files and paths in a list to
+        * tidy up later. If not, the test should not fail because of this.
+        */
+       char *buffer_copy;
+       asprintf(&buffer_copy, "%s", buff);
+
+       if (fefp && buffer_copy) {
+               fefp->path = buffer_copy;
+               STAILQ_INSERT_TAIL(&head, fefp, fake_env_file_paths);
+       }
+       return buff;
+
+fake_mountpoint_error:
+       free(buff);
+       free(tmpdir);
+       return NULL;
+}
+
+bool __wrap_probe_config_file(CONFIG_PART *cp)
+{
+       bool ret;
+       probe_config_file_call_count++;
+
+       if (asprintf(&cp->mountpoint, "tmpdir") == -1)
+       {
+               cp->not_mounted = true;
+               cp->mountpoint = NULL;
+               return false;
+       }
+       cp->not_mounted = false;
+       ret =  __real_probe_config_file(cp);
+
+       free(cp->mountpoint);
+       return ret;
+}
+
+void delete_temp_files(void)
+{
+       char *buffer;
+       while (!STAILQ_EMPTY(&head)) {
+               struct  fake_env_file_path *fefp = STAILQ_FIRST(&head);
+
+               if (asprintf(&buffer, "%s/BGENV.DAT", fefp->path) != -1) {
+                       remove(buffer);
+                       free(buffer);
+               }
+               rmdir(fefp->path);
+               free(fefp->path);
+
+               STAILQ_REMOVE_HEAD(&head, fake_env_file_paths);
+               free(fefp);
+       }
+}
+
+FAKE_VOID_FUNC(ped_device_probe_all);
+FAKE_VALUE_FUNC(PedDevice *, ped_device_get_next, const PedDevice *);
+FAKE_VALUE_FUNC(char *, get_mountpoint, char *);
+
+START_TEST(env_api_fat_test_probe_config_file)
+{
+       bool result;
+
+       RESET_FAKE(ped_device_probe_all);
+       RESET_FAKE(ped_device_get_next);
+       RESET_FAKE(get_mountpoint);
+
+       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;
+       get_mountpoint_fake.custom_fake = get_mountpoint_custom_fake;
+       probe_config_file_call_count = 0;
+
+       STAILQ_INIT(&head);
+
+       result = bgenv_init();
+
+       delete_temp_files();
+
+       free_fake_devices();
+
+       ck_assert_int_eq(ped_device_probe_all_fake.call_count, 1);
+       ck_assert_int_eq(ped_device_get_next_fake.call_count, 2);
+       ck_assert_int_eq(probe_config_file_call_count, ENV_NUM_CONFIG_PARTS);
+       ck_assert_int_eq(get_mountpoint_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_probe_config_file);
+       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-8-andreas.reichel.ext%40siemens.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to