diff options
Diffstat (limited to 'lib/util/test')
-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 |
10 files changed, 214 insertions, 0 deletions
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 |