From 9f2d63162e82d423faeda9c7449d6edd819e4591 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 23 Sep 2023 11:16:46 +0200 Subject: Cleanup: remove rest of libio Move the directory iterator test to libsquashfs, move the tree scanning code to libcommon. Signed-off-by: David Oberhollenzer --- lib/sqfs/test/dir_iterator.c | 417 ++++++++++++++++++++++++++++++++ lib/sqfs/test/testdir/dira/file_a0 | 0 lib/sqfs/test/testdir/dira/file_a1 | 0 lib/sqfs/test/testdir/dira/file_a2 | 0 lib/sqfs/test/testdir/dirb/dirx/file_x0 | 0 lib/sqfs/test/testdir/dirb/dirx/file_x1 | 0 lib/sqfs/test/testdir/dirb/dirx/file_x2 | 0 lib/sqfs/test/testdir/dirb/file_b0 | 0 lib/sqfs/test/testdir/dirb/file_b1 | 0 lib/sqfs/test/testdir/dirb/file_b2 | 0 lib/sqfs/test/testdir/dirc/file_c0 | 1 + lib/sqfs/test/testdir/dirc/file_c1 | 0 lib/sqfs/test/testdir/dirc/file_c2 | 1 + 13 files changed, 419 insertions(+) create mode 100644 lib/sqfs/test/dir_iterator.c create mode 100644 lib/sqfs/test/testdir/dira/file_a0 create mode 100644 lib/sqfs/test/testdir/dira/file_a1 create mode 100644 lib/sqfs/test/testdir/dira/file_a2 create mode 100644 lib/sqfs/test/testdir/dirb/dirx/file_x0 create mode 100644 lib/sqfs/test/testdir/dirb/dirx/file_x1 create mode 100644 lib/sqfs/test/testdir/dirb/dirx/file_x2 create mode 100644 lib/sqfs/test/testdir/dirb/file_b0 create mode 100644 lib/sqfs/test/testdir/dirb/file_b1 create mode 100644 lib/sqfs/test/testdir/dirb/file_b2 create mode 100644 lib/sqfs/test/testdir/dirc/file_c0 create mode 100644 lib/sqfs/test/testdir/dirc/file_c1 create mode 100644 lib/sqfs/test/testdir/dirc/file_c2 (limited to 'lib/sqfs/test') diff --git a/lib/sqfs/test/dir_iterator.c b/lib/sqfs/test/dir_iterator.c new file mode 100644 index 0000000..701b8f0 --- /dev/null +++ b/lib/sqfs/test/dir_iterator.c @@ -0,0 +1,417 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * dir_iterator.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" + +#include "sqfs/dir_entry.h" +#include "sqfs/error.h" +#include "util/test.h" +#include "sqfs/io.h" +#include "compat.h" + +static int compare_entries(const void *a, const void *b) +{ + const sqfs_dir_entry_t *const *lhs = a; + const sqfs_dir_entry_t *const *rhs = b; + + return strcmp((*lhs)->name, (*rhs)->name); +} + +static int compare_files(const void *a, const void *b) +{ + const sqfs_istream_t *const *lhs = a; + const sqfs_istream_t *const *rhs = b; + + return strcmp((*lhs)->get_filename(*lhs), + (*rhs)->get_filename(*rhs)); +} + +int main(int argc, char **argv) +{ + sqfs_dir_iterator_t *dir, *suba, *subb, *subc, *sub; + sqfs_dir_entry_t *dent, *ent[6]; + sqfs_istream_t *files[3]; + char buffer[128]; + size_t i, j; + int ret; + (void)argc; (void)argv; + + /* scan the top level hierarchy */ + ret = sqfs_dir_iterator_create_native(&dir, TEST_PATH, 0); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(dir); + + ret = dir->next(dir, &ent[0]); + TEST_NOT_NULL(ent[0]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[1]); + TEST_NOT_NULL(ent[1]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[2]); + TEST_NOT_NULL(ent[2]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[3]); + TEST_NOT_NULL(ent[3]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[4]); + TEST_NOT_NULL(ent[4]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[5]); + TEST_NULL(ent[5]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 5, sizeof(ent[0]), compare_entries); + + TEST_STR_EQUAL(ent[0]->name, "."); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, ".."); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dira"); + TEST_ASSERT(S_ISDIR(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "dirb"); + TEST_ASSERT(S_ISDIR(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "dirc"); + TEST_ASSERT(S_ISDIR(ent[4]->mode)); + + for (i = 0; i < 5; ++i) + free(ent[i]); + + /* scan first sub hierarchy */ + ret = sqfs_dir_iterator_create_native(&dir, TEST_PATH "/dira", 0); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(dir); + + ret = dir->next(dir, &ent[0]); + TEST_NOT_NULL(ent[0]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[1]); + TEST_NOT_NULL(ent[1]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[2]); + TEST_NOT_NULL(ent[2]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[3]); + TEST_NOT_NULL(ent[3]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[4]); + TEST_NOT_NULL(ent[4]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[5]); + TEST_NULL(ent[5]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 5, sizeof(ent[0]), compare_entries); + + TEST_STR_EQUAL(ent[0]->name, "."); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, ".."); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "file_a0"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "file_a1"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "file_a2"); + TEST_ASSERT(S_ISREG(ent[4]->mode)); + + for (i = 0; i < 5; ++i) + free(ent[i]); + + /* scan second sub hierarchy */ + ret = sqfs_dir_iterator_create_native(&dir, TEST_PATH "/dirb", 0); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(dir); + + ret = dir->next(dir, &ent[0]); + TEST_NOT_NULL(ent[0]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[1]); + TEST_NOT_NULL(ent[1]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[2]); + TEST_NOT_NULL(ent[2]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[3]); + TEST_NOT_NULL(ent[3]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[4]); + TEST_NOT_NULL(ent[4]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[5]); + TEST_NOT_NULL(ent[5]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &dent); + TEST_NULL(dent); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 6, sizeof(ent[0]), compare_entries); + + TEST_STR_EQUAL(ent[0]->name, "."); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, ".."); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dirx"); + TEST_ASSERT(S_ISDIR(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "file_b0"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "file_b1"); + TEST_ASSERT(S_ISREG(ent[4]->mode)); + TEST_STR_EQUAL(ent[5]->name, "file_b2"); + TEST_ASSERT(S_ISREG(ent[5]->mode)); + + for (i = 0; i < 6; ++i) + free(ent[i]); + + /* scan first sub hierarchy */ + ret = sqfs_dir_iterator_create_native(&dir, TEST_PATH "/dirc", 0); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(dir); + + ret = dir->next(dir, &ent[0]); + TEST_NOT_NULL(ent[0]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[1]); + TEST_NOT_NULL(ent[1]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[2]); + TEST_NOT_NULL(ent[2]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[3]); + TEST_NOT_NULL(ent[3]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[4]); + TEST_NOT_NULL(ent[4]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[5]); + TEST_NULL(ent[5]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 5, sizeof(ent[0]), compare_entries); + + TEST_STR_EQUAL(ent[0]->name, "."); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, ".."); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "file_c0"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "file_c1"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "file_c2"); + TEST_ASSERT(S_ISREG(ent[4]->mode)); + + for (i = 0; i < 5; ++i) + free(ent[i]); + + /* test sub directory iterators */ + suba = NULL; + subb = NULL; + subc = NULL; + + ret = sqfs_dir_iterator_create_native(&dir, TEST_PATH, 0); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(dir); + + for (i = 0; i < 5; ++i) { + ret = dir->next(dir, &dent); + TEST_NOT_NULL(dent); + TEST_EQUAL_I(ret, 0); + + if (!strcmp(dent->name, "dira")) { + TEST_NULL(suba); + ret = dir->open_subdir(dir, &suba); + TEST_NOT_NULL(suba); + TEST_EQUAL_I(ret, 0); + } else if (!strcmp(dent->name, "dirb")) { + TEST_NULL(subb); + ret = dir->open_subdir(dir, &subb); + TEST_NOT_NULL(subb); + TEST_EQUAL_I(ret, 0); + } else if (!strcmp(dent->name, "dirc")) { + TEST_NULL(subc); + ret = dir->open_subdir(dir, &subc); + TEST_NOT_NULL(subc); + TEST_EQUAL_I(ret, 0); + } + + free(dent); + } + + ret = dir->next(dir, &dent); + TEST_NULL(dent); + TEST_ASSERT(ret > 0); + dir = sqfs_drop(dir); + + TEST_NOT_NULL(suba); + TEST_NOT_NULL(subb); + TEST_NOT_NULL(subc); + + /* sub iterator a */ + for (i = 0; i < 5; ++i) { + ret = suba->next(suba, &ent[i]); + TEST_NOT_NULL(ent[0]); + TEST_EQUAL_I(ret, 0); + + if (S_ISREG(ent[i]->mode)) { + ret = suba->open_subdir(suba, &sub); + TEST_NULL(sub); + TEST_EQUAL_I(ret, SQFS_ERROR_NOT_DIR); + } + } + + ret = suba->next(suba, &dent); + TEST_NULL(dent); + TEST_ASSERT(ret > 0); + suba = sqfs_drop(suba); + + qsort(ent, 5, sizeof(ent[0]), compare_entries); + + TEST_STR_EQUAL(ent[0]->name, "."); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, ".."); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "file_a0"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "file_a1"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "file_a2"); + TEST_ASSERT(S_ISREG(ent[4]->mode)); + + for (i = 0; i < 5; ++i) + free(ent[i]); + + /* sub iterator b */ + for (i = 0; i < 6; ++i) { + ret = subb->next(subb, &ent[i]); + TEST_NOT_NULL(ent[0]); + TEST_EQUAL_I(ret, 0); + + if (S_ISREG(ent[i]->mode)) { + ret = subb->open_subdir(subb, &sub); + TEST_NULL(sub); + TEST_EQUAL_I(ret, SQFS_ERROR_NOT_DIR); + } + } + + ret = subb->next(subb, &dent); + TEST_NULL(dent); + TEST_ASSERT(ret > 0); + subb = sqfs_drop(subb); + + qsort(ent, 6, sizeof(ent[0]), compare_entries); + + TEST_STR_EQUAL(ent[0]->name, "."); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, ".."); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dirx"); + TEST_ASSERT(S_ISDIR(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "file_b0"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "file_b1"); + TEST_ASSERT(S_ISREG(ent[4]->mode)); + TEST_STR_EQUAL(ent[5]->name, "file_b2"); + TEST_ASSERT(S_ISREG(ent[5]->mode)); + + for (i = 0; i < 6; ++i) + free(ent[i]); + + /* sub iterator c */ + j = 0; + + for (i = 0; i < 5; ++i) { + ret = subc->next(subc, &ent[i]); + TEST_NOT_NULL(ent[0]); + TEST_EQUAL_I(ret, 0); + + if (S_ISREG(ent[i]->mode)) { + ret = subc->open_subdir(subc, &sub); + TEST_NULL(sub); + TEST_EQUAL_I(ret, SQFS_ERROR_NOT_DIR); + + TEST_ASSERT(j < 3); + ret = subc->open_file_ro(subc, &(files[j++])); + TEST_EQUAL_I(ret, 0); + } + } + + TEST_EQUAL_UI(j, 3); + + ret = subc->next(subc, &dent); + TEST_NULL(dent); + TEST_ASSERT(ret > 0); + subc = sqfs_drop(subc); + + qsort(ent, 5, sizeof(ent[0]), compare_entries); + qsort(files, 3, sizeof(files[0]), compare_files); + + TEST_STR_EQUAL(ent[0]->name, "."); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, ".."); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "file_c0"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "file_c1"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "file_c2"); + TEST_ASSERT(S_ISREG(ent[4]->mode)); + + for (i = 0; i < 5; ++i) + free(ent[i]); + + TEST_STR_EQUAL(files[0]->get_filename(files[0]), "file_c0"); + TEST_STR_EQUAL(files[1]->get_filename(files[1]), "file_c1"); + TEST_STR_EQUAL(files[2]->get_filename(files[2]), "file_c2"); + + ret = sqfs_istream_read(files[0], buffer, sizeof(buffer)); + TEST_EQUAL_I(ret, 12); + buffer[ret] = '\0'; + TEST_STR_EQUAL(buffer, "test string\n"); + ret = sqfs_istream_read(files[0], buffer, sizeof(buffer)); + TEST_EQUAL_I(ret, 0); + + ret = sqfs_istream_read(files[1], buffer, sizeof(buffer)); + TEST_EQUAL_I(ret, 0); + + ret = sqfs_istream_read(files[2], buffer, sizeof(buffer)); + TEST_EQUAL_I(ret, 10); + buffer[ret] = '\0'; + TEST_STR_EQUAL(buffer, "你好!\n"); + ret = sqfs_istream_read(files[2], buffer, sizeof(buffer)); + TEST_EQUAL_I(ret, 0); + + for (i = 0; i < j; ++i) + sqfs_drop(files[i]); + + return EXIT_SUCCESS; +} diff --git a/lib/sqfs/test/testdir/dira/file_a0 b/lib/sqfs/test/testdir/dira/file_a0 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dira/file_a1 b/lib/sqfs/test/testdir/dira/file_a1 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dira/file_a2 b/lib/sqfs/test/testdir/dira/file_a2 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirb/dirx/file_x0 b/lib/sqfs/test/testdir/dirb/dirx/file_x0 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirb/dirx/file_x1 b/lib/sqfs/test/testdir/dirb/dirx/file_x1 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirb/dirx/file_x2 b/lib/sqfs/test/testdir/dirb/dirx/file_x2 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirb/file_b0 b/lib/sqfs/test/testdir/dirb/file_b0 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirb/file_b1 b/lib/sqfs/test/testdir/dirb/file_b1 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirb/file_b2 b/lib/sqfs/test/testdir/dirb/file_b2 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirc/file_c0 b/lib/sqfs/test/testdir/dirc/file_c0 new file mode 100644 index 0000000..71c1922 --- /dev/null +++ b/lib/sqfs/test/testdir/dirc/file_c0 @@ -0,0 +1 @@ +test string diff --git a/lib/sqfs/test/testdir/dirc/file_c1 b/lib/sqfs/test/testdir/dirc/file_c1 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirc/file_c2 b/lib/sqfs/test/testdir/dirc/file_c2 new file mode 100644 index 0000000..efde6ef --- /dev/null +++ b/lib/sqfs/test/testdir/dirc/file_c2 @@ -0,0 +1 @@ +你好! -- cgit v1.2.3