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>
This commit is contained in:
parent
62a6188da5
commit
25c737d403
@ -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__
|
||||
|
51
tests/helpers/file.c
Normal file
51
tests/helpers/file.c
Normal file
@ -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;
|
||||
}
|
12
tests/include/helpers/file.h
Normal file
12
tests/include/helpers/file.h
Normal file
@ -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
|
@ -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
|
||||
|
@ -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));
|
||||
|
Loading…
x
Reference in New Issue
Block a user