aboutsummaryrefslogtreecommitdiff
path: root/lib/util/test/dir_iterator.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-04-17 20:27:16 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-04-17 20:27:16 +0200
commit483bf42fe4507fa709d9892c581c98f5a4b54849 (patch)
treeb17f2cdb558c140040c38ec47efa58e7a8444101 /lib/util/test/dir_iterator.c
parent4ba31cfa1521c94f4be73ea4c4c97d9ff475e89f (diff)
Add unit test for directory iterator
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/util/test/dir_iterator.c')
-rw-r--r--lib/util/test/dir_iterator.c214
1 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;
+}