diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/util/Makemodule.am | 8 | ||||
-rw-r--r-- | lib/util/src/unix_dir_iterator.c | 1 | ||||
-rw-r--r-- | lib/util/test/dir_iterator.c | 214 | ||||
-rw-r--r-- | lib/util/test/testdir/dira/file_a0 | 0 | ||||
-rw-r--r-- | lib/util/test/testdir/dira/file_a1 | 0 | ||||
-rw-r--r-- | lib/util/test/testdir/dira/file_a2 | 0 | ||||
-rw-r--r-- | lib/util/test/testdir/dirb/file_b0 | 0 | ||||
-rw-r--r-- | lib/util/test/testdir/dirb/file_b1 | 0 | ||||
-rw-r--r-- | lib/util/test/testdir/dirb/file_b2 | 0 | ||||
-rw-r--r-- | lib/util/test/testdir/dirc/file_c0 | 0 | ||||
-rw-r--r-- | lib/util/test/testdir/dirc/file_c1 | 0 | ||||
-rw-r--r-- | lib/util/test/testdir/dirc/file_c2 | 0 |
12 files changed, 222 insertions, 1 deletions
diff --git a/lib/util/Makemodule.am b/lib/util/Makemodule.am index 2c50594..04c5989 100644 --- a/lib/util/Makemodule.am +++ b/lib/util/Makemodule.am @@ -80,11 +80,17 @@ test_hex_decode_LDADD = libutil.a libcompat.a test_base64_decode_SOURCES = lib/util/test/base64_decode.c test_base64_decode_LDADD = libutil.a libcompat.a +test_dir_iterator_SOURCES = lib/util/test/dir_iterator.c +test_dir_iterator_LDADD = libutil.a libcompat.a +test_dir_iterator_CPPFLAGS = $(AM_CPPFLAGS) +test_dir_iterator_CPPFLAGS += -DTESTPATH=$(top_srcdir)/lib/util/test/testdir + LIBUTIL_TESTS = \ test_str_table test_rbtree test_xxhash test_threadpool test_ismemzero \ test_canonicalize_name test_filename_sane test_filename_sane_w32 \ - test_sdate_epoch test_hex_decode test_base64_decode + test_sdate_epoch test_hex_decode test_base64_decode test_dir_iterator check_PROGRAMS += $(LIBUTIL_TESTS) TESTS += $(LIBUTIL_TESTS) EXTRA_DIST += $(top_srcdir)/lib/util/test/words.txt +EXTRA_DIST += $(top_srcdir)/lib/util/test/testdir diff --git a/lib/util/src/unix_dir_iterator.c b/lib/util/src/unix_dir_iterator.c index e712a45..69a4251 100644 --- a/lib/util/src/unix_dir_iterator.c +++ b/lib/util/src/unix_dir_iterator.c @@ -91,6 +91,7 @@ static int dir_next(dir_iterator_t *base, dir_entry_t **out) dir_entry_t *decoded; size_t len; + *out = NULL; if (it->state != 0) return it->state; diff --git a/lib/util/test/dir_iterator.c b/lib/util/test/dir_iterator.c new file mode 100644 index 0000000..22b05dc --- /dev/null +++ b/lib/util/test/dir_iterator.c @@ -0,0 +1,214 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * dir_iterator.c + * + * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> + */ +#include "config.h" + +#include "util/dir_iterator.h" +#include "util/test.h" +#include "compat.h" + +static int compare_entries(const void *a, const void *b) +{ + const dir_entry_t *const *lhs = a; + const dir_entry_t *const *rhs = b; + + return strcmp((*lhs)->name, (*rhs)->name); +} + +int main(int argc, char **argv) +{ + dir_iterator_t *dir; + dir_entry_t *ent[6]; + size_t i; + int ret; + (void)argc; (void)argv; + + /* scan the top level hierarchy */ + dir = dir_iterator_create(TEST_PATH); + 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 */ + dir = dir_iterator_create(TEST_PATH "/dira"); + 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 */ + dir = dir_iterator_create(TEST_PATH "/dirb"); + 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_b0"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "file_b1"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "file_b2"); + TEST_ASSERT(S_ISREG(ent[4]->mode)); + + for (i = 0; i < 5; ++i) + free(ent[i]); + + /* scan first sub hierarchy */ + dir = dir_iterator_create(TEST_PATH "/dirc"); + 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]); + + return EXIT_SUCCESS; +} diff --git a/lib/util/test/testdir/dira/file_a0 b/lib/util/test/testdir/dira/file_a0 new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/util/test/testdir/dira/file_a0 diff --git a/lib/util/test/testdir/dira/file_a1 b/lib/util/test/testdir/dira/file_a1 new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/util/test/testdir/dira/file_a1 diff --git a/lib/util/test/testdir/dira/file_a2 b/lib/util/test/testdir/dira/file_a2 new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/util/test/testdir/dira/file_a2 diff --git a/lib/util/test/testdir/dirb/file_b0 b/lib/util/test/testdir/dirb/file_b0 new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/util/test/testdir/dirb/file_b0 diff --git a/lib/util/test/testdir/dirb/file_b1 b/lib/util/test/testdir/dirb/file_b1 new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/util/test/testdir/dirb/file_b1 diff --git a/lib/util/test/testdir/dirb/file_b2 b/lib/util/test/testdir/dirb/file_b2 new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/util/test/testdir/dirb/file_b2 diff --git a/lib/util/test/testdir/dirc/file_c0 b/lib/util/test/testdir/dirc/file_c0 new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/util/test/testdir/dirc/file_c0 diff --git a/lib/util/test/testdir/dirc/file_c1 b/lib/util/test/testdir/dirc/file_c1 new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/util/test/testdir/dirc/file_c1 diff --git a/lib/util/test/testdir/dirc/file_c2 b/lib/util/test/testdir/dirc/file_c2 new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/util/test/testdir/dirc/file_c2 |