summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMaximilian Brune <maximilian.brune@9elements.com>2024-05-09 02:53:51 +0200
committerLean Sheng Tan <sheng.tan@9elements.com>2024-05-21 13:44:39 +0000
commit25c737d403818a92dfeb06e29ffaf04102d0f260 (patch)
treed27d4e1a366a8f8aab0e338d0f3ee577b640d339 /tests
parent62a6188da5080d17afe33a2de9218c7313c2e64b (diff)
tests/lib: Factor out file related functions
Signed-off-by: Maximilian Brune <maximilian.brune@9elements.com> Change-Id: I5c22913b35848c5ea32d6805ea081abefd3380bf Reviewed-on: https://review.coreboot.org/c/coreboot/+/82237 Reviewed-by: Julius Werner <jwerner@chromium.org> Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Jakub Czapiga <czapiga@google.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.common2
-rw-r--r--tests/helpers/file.c51
-rw-r--r--tests/include/helpers/file.h12
-rw-r--r--tests/lib/Makefile.mk1
-rw-r--r--tests/lib/lzma-test.c35
5 files changed, 72 insertions, 29 deletions
diff --git a/tests/Makefile.common b/tests/Makefile.common
index d6c9bd60fc..e856fe07c1 100644
--- a/tests/Makefile.common
+++ b/tests/Makefile.common
@@ -60,6 +60,7 @@ TEST_CFLAGS += -Os
endif
TEST_CFLAGS += -D__TEST__ -D__COREBOOT__
+TEST_CFLAGS += -D__TEST_DATA_DIR__=\"$(testsrc)/data\"
ifneq ($(filter-out 0,$(TEST_PRINT)),)
TEST_CFLAGS += -DTEST_PRINT=1
@@ -121,7 +122,6 @@ $$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER)
$($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
-D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ \
-D__TEST_NAME__=\"$(subst /,_,$(1))\" \
- -D__TEST_DATA_DIR__=\"$(testsrc)/data\"
# Give us a way to distinguish between coreboot source files and test files in code.
$($(1)-srcobjs): TEST_CFLAGS += -D__TEST_SRCOBJ__
diff --git a/tests/helpers/file.c b/tests/helpers/file.c
new file mode 100644
index 0000000000..53f4f12dc6
--- /dev/null
+++ b/tests/helpers/file.c
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+/*
+ * Get the file size of a given file
+ *
+ * @params fname name of the file relative to the __TEST_DATA_DIR__ directory
+ *
+ * @return On success file size in bytes is returned. On failure -1 is returned.
+ */
+int test_get_file_size(const char *fname)
+{
+ char path[strlen(__TEST_DATA_DIR__) + strlen(fname) + 2];
+ sprintf(path, "%s/%s", __TEST_DATA_DIR__, fname);
+
+ struct stat st;
+ if (stat(path, &st) == -1)
+ return -1;
+ return st.st_size;
+}
+
+/*
+ * Read a file and write its contents into a buffer
+ *
+ * @params fname name of the file relative to the __TEST_DATA_DIR__ directory
+ * @params buf buffer to write file contents into
+ * @params size size of buf
+ *
+ * @return On success number of bytes read is returned. On failure -1 is returned.
+ */
+int test_read_file(const char *fname, uint8_t *buf, size_t size)
+{
+ char path[strlen(__TEST_DATA_DIR__) + strlen(fname) + 2];
+ sprintf(path, "%s/%s", __TEST_DATA_DIR__, fname);
+
+ int f = open(path, O_RDONLY);
+ if (f == -1)
+ return -1;
+
+ int read_size = read(f, buf, size);
+
+ close(f);
+ return read_size;
+}
diff --git a/tests/include/helpers/file.h b/tests/include/helpers/file.h
new file mode 100644
index 0000000000..a583b000a2
--- /dev/null
+++ b/tests/include/helpers/file.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef _TESTS_HELPERS_FILE_H
+#define _TESTS_HELPERS_FILE_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+int test_get_file_size(const char *fname);
+int test_read_file(const char *fname, uint8_t *buf, size_t size);
+
+#endif
diff --git a/tests/lib/Makefile.mk b/tests/lib/Makefile.mk
index 01777171c9..7fc5471529 100644
--- a/tests/lib/Makefile.mk
+++ b/tests/lib/Makefile.mk
@@ -234,6 +234,7 @@ lzma-test-srcs += tests/lib/lzma-test.c
lzma-test-srcs += tests/stubs/console.c
lzma-test-srcs += src/lib/lzma.c
lzma-test-srcs += src/lib/lzmadecode.c
+lzma-test-syssrcs += tests/helpers/file.c
ux_locales-test-srcs += tests/lib/ux_locales-test.c
ux_locales-test-srcs += tests/stubs/console.c
diff --git a/tests/lib/lzma-test.c b/tests/lib/lzma-test.c
index b68d38e122..48d629a179 100644
--- a/tests/lib/lzma-test.c
+++ b/tests/lib/lzma-test.c
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <fcntl.h>
+#include <helpers/file.h>
#include <lib.h>
#include <lib/lzmadecode.h>
#include <stdlib.h>
@@ -10,7 +11,6 @@
#include <tests/test.h>
#include <unistd.h>
-
struct lzma_test_state {
char *raw_filename;
size_t raw_file_sz;
@@ -18,14 +18,6 @@ struct lzma_test_state {
size_t comp_file_sz;
};
-static int get_file_size(const char *fname)
-{
- struct stat st;
- if (stat(fname, &st) == -1)
- return -1;
- return st.st_size;
-}
-
static int teardown_ulzman_file(void **state)
{
struct lzma_test_state *s = *state;
@@ -42,7 +34,7 @@ static int setup_ulzman_file(void **state)
{
int ret = 0;
const char *fname_base = *state;
- const char path_prefix[] = __TEST_DATA_DIR__ "/lib/lzma-test/%s%s";
+ const char path_prefix[] = "lib/lzma-test/%s%s";
const char raw_file_suffix[] = ".bin";
const char comp_file_suffix[] = ".lzma.bin";
struct lzma_test_state *s = test_malloc(sizeof(*s));
@@ -69,8 +61,8 @@ static int setup_ulzman_file(void **state)
snprintf(s->comp_filename, comp_filename_size, path_prefix, fname_base,
comp_file_suffix);
- s->raw_file_sz = get_file_size(s->raw_filename);
- s->comp_file_sz = get_file_size(s->comp_filename);
+ s->raw_file_sz = test_get_file_size(s->raw_filename);
+ s->comp_file_sz = test_get_file_size(s->comp_filename);
if (s->raw_file_sz == -1) {
print_error("Unable to open file: %s\n", s->raw_filename);
@@ -91,20 +83,6 @@ error:
return ret;
}
-static int read_file(const char *fname, uint8_t *buf, size_t sz)
-{
- int f = open(fname, O_RDONLY);
- int read_sz = 0;
-
- if (f == -1)
- return -1;
-
- read_sz = read(f, buf, sz);
-
- close(f);
- return read_sz;
-}
-
static void test_ulzman_correct_file(void **state)
{
struct lzma_test_state *s = *state;
@@ -115,9 +93,10 @@ static void test_ulzman_correct_file(void **state)
assert_non_null(raw_buf);
assert_non_null(decomp_buf);
assert_non_null(comp_buf);
- assert_int_equal(s->raw_file_sz, read_file(s->raw_filename, raw_buf, s->raw_file_sz));
+ assert_int_equal(s->raw_file_sz,
+ test_read_file(s->raw_filename, raw_buf, s->raw_file_sz));
assert_int_equal(s->comp_file_sz,
- read_file(s->comp_filename, comp_buf, s->comp_file_sz));
+ test_read_file(s->comp_filename, comp_buf, s->comp_file_sz));
assert_int_equal(s->raw_file_sz,
ulzman(comp_buf, s->comp_file_sz, decomp_buf, s->raw_file_sz));