From 061fbc12fe49ff49088a644def3227d3800cd8a7 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 2 Jun 2023 17:42:30 +0200 Subject: libio: consolidate add-hoc memory istream_t implementations In several places, there are ad-hoc istream_t implementations that read from a memory buffer to test something else stacked on top. This commit consolidates those ad-hoc implmentations into a proper one in libio, and uses the chance to remove external files for some older tests that rely on file I/O instead. Signed-off-by: David Oberhollenzer --- bin/gensquashfs/test/fstree1.txt | 10 ------ bin/gensquashfs/test/fstree_from_file.c | 20 ++++++++++- bin/gensquashfs/test/sort_file.c | 63 +++++++-------------------------- 3 files changed, 31 insertions(+), 62 deletions(-) delete mode 100644 bin/gensquashfs/test/fstree1.txt (limited to 'bin/gensquashfs/test') diff --git a/bin/gensquashfs/test/fstree1.txt b/bin/gensquashfs/test/fstree1.txt deleted file mode 100644 index 95ee469..0000000 --- a/bin/gensquashfs/test/fstree1.txt +++ /dev/null @@ -1,10 +0,0 @@ -# comment line -slink /slink 0644 2 3 slinktarget -dir /dir 0755 4 5 -nod /chardev 0600 6 7 c 13 37 -nod /blkdev 0600 8 9 b 42 21 -pipe /pipe 0644 10 11 -dir / 0755 1000 100 -dir "/foo bar" 0755 0 0 -dir "/foo bar/ test \"/" 0755 0 0 - sock /sock 0555 12 13 \ No newline at end of file diff --git a/bin/gensquashfs/test/fstree_from_file.c b/bin/gensquashfs/test/fstree_from_file.c index 03591d5..e54111c 100644 --- a/bin/gensquashfs/test/fstree_from_file.c +++ b/bin/gensquashfs/test/fstree_from_file.c @@ -7,18 +7,36 @@ #include "config.h" #include "util/test.h" +#include "io/mem.h" #include "mkfs.h" +const char *listing = +"# comment line\n" +"slink /slink 0644 2 3 slinktarget\n" +"dir /dir 0755 4 5\n" +"nod /chardev 0600 6 7 c 13 37\n" +"nod /blkdev 0600 8 9 b 42 21\n" +"pipe /pipe 0644 10 11\n" +"dir / 0755 1000 100\n" +"dir \"/foo bar\" 0755 0 0\n" +"dir \"/foo bar/ test \\\"/\" 0755 0 0\n" +" sock /sock 0555 12 13 "; + int main(int argc, char **argv) { fstree_defaults_t fsd; + istream_t *file; tree_node_t *n; fstree_t fs; (void)argc; (void)argv; + file = istream_memory_create("memfile", 7, listing, strlen(listing)); + TEST_NOT_NULL(file); + TEST_ASSERT(parse_fstree_defaults(&fsd, NULL) == 0); TEST_ASSERT(fstree_init(&fs, &fsd) == 0); - TEST_ASSERT(fstree_from_file(&fs, TEST_PATH, NULL) == 0); + TEST_ASSERT(fstree_from_file_stream(&fs, file, NULL) == 0); + sqfs_drop(file); fstree_post_process(&fs); n = fs.root->data.children; diff --git a/bin/gensquashfs/test/sort_file.c b/bin/gensquashfs/test/sort_file.c index 8022cff..af50260 100644 --- a/bin/gensquashfs/test/sort_file.c +++ b/bin/gensquashfs/test/sort_file.c @@ -9,6 +9,7 @@ #include "sqfs/block.h" #include "util/test.h" #include "util/util.h" +#include "io/mem.h" #include "mkfs.h" static const char *listing = @@ -108,62 +109,23 @@ static int flags[] = { 0, }; -/*****************************************************************************/ - -static sqfs_u8 temp_buffer[2048]; -static const char *input_file = NULL; - -static void destroy_noop(sqfs_object_t *obj) -{ - (void)obj; -} - -static int memfile_load(istream_t *strm) -{ - strcpy((char *)temp_buffer, input_file); - strm->eof = true; - strm->buffer_used = strlen(input_file); - return 0; -} - -static const char *get_filename(istream_t *strm) -{ - (void)strm; - return "memstream"; -} - -static istream_t memstream = { - .base = { - .destroy = destroy_noop, - }, - - .buffer_used = 0, - .buffer_offset = 0, - .eof = false, - .buffer = temp_buffer, - - .precache = memfile_load, - .get_filename = get_filename, -}; - -/*****************************************************************************/ - int main(int argc, char **argv) { fstree_defaults_t fsd; + istream_t *memstream; tree_node_t *n; fstree_t fs; size_t i; (void)argc; (void)argv; - input_file = listing; - memstream.buffer_used = 0; - memstream.buffer_offset = 0; - memstream.eof = false; + memstream = istream_memory_create("listing.txt", 1024, + listing, strlen(listing)); + TEST_NOT_NULL(memstream); TEST_ASSERT(parse_fstree_defaults(&fsd, NULL) == 0); TEST_ASSERT(fstree_init(&fs, &fsd) == 0); - TEST_ASSERT(fstree_from_file_stream(&fs, &memstream, NULL) == 0); + TEST_ASSERT(fstree_from_file_stream(&fs, memstream, NULL) == 0); + sqfs_drop(memstream); fstree_post_process(&fs); @@ -186,12 +148,11 @@ int main(int argc, char **argv) TEST_EQUAL_UI(i, sizeof(initial_order) / sizeof(initial_order[0])); - input_file = sort_file; - memstream.buffer_used = 0; - memstream.buffer_offset = 0; - memstream.eof = false; - - TEST_ASSERT(fstree_sort_files(&fs, &memstream) == 0); + memstream = istream_memory_create("sortfile.txt", 1024, + sort_file, strlen(sort_file)); + TEST_NOT_NULL(memstream); + TEST_ASSERT(fstree_sort_files(&fs, memstream) == 0); + sqfs_drop(memstream); for (i = 0, n = fs.files; n != NULL; n = n->next_by_type, ++i) { char *path = fstree_get_path(n); -- cgit v1.2.3