From 2d92372ea6a64ce898a872f496e62524c19d4b21 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Tue, 15 Dec 2020 11:59:39 +0100 Subject: Add a simple test case for the directory scanning code Signed-off-by: David Oberhollenzer --- tests/Makemodule.am | 7 + tests/fstree_from_dir.c | 379 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 386 insertions(+) create mode 100644 tests/fstree_from_dir.c (limited to 'tests') diff --git a/tests/Makemodule.am b/tests/Makemodule.am index 7b86af9..cf09f2a 100644 --- a/tests/Makemodule.am +++ b/tests/Makemodule.am @@ -51,6 +51,11 @@ test_fstree_from_file_SOURCES = tests/fstree_from_file.c tests/test.h test_fstree_from_file_CPPFLAGS = $(AM_CPPFLAGS) -DTESTPATH=$(top_srcdir)/tests/fstree1.txt test_fstree_from_file_LDADD = libfstree.a libfstream.a libcompat.a +test_fstree_from_dir_SOURCES = tests/fstree_from_dir.c tests/test.h +test_fstree_from_dir_CPPFLAGS = $(AM_CPPFLAGS) +test_fstree_from_dir_CPPFLAGS += -DTESTPATH=$(top_srcdir)/tests/tar +test_fstree_from_dir_LDADD = libfstree.a libcompat.a + test_fstree_init_SOURCES = tests/fstree_init.c tests/test.h test_fstree_init_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/lib/fstree test_fstree_init_LDADD = libfstree.a libfstream.a libcompat.a @@ -236,6 +241,7 @@ check_PROGRAMS += test_mknode_simple test_mknode_slink test_mknode_reg check_PROGRAMS += test_mknode_dir test_gen_inode_numbers test_add_by_path check_PROGRAMS += test_get_path test_fstree_sort test_fstree_from_file check_PROGRAMS += test_fstree_init test_filename_sane test_filename_sane_w32 +check_PROGRAMS += test_fstree_from_dir check_PROGRAMS += test_tar_ustar0 test_tar_ustar1 test_tar_ustar2 check_PROGRAMS += test_tar_ustar3 test_tar_ustar4 test_tar_ustar5 check_PROGRAMS += test_tar_ustar6 @@ -255,6 +261,7 @@ TESTS += test_mknode_simple test_mknode_slink TESTS += test_mknode_reg test_mknode_dir test_gen_inode_numbers TESTS += test_add_by_path test_get_path test_fstree_sort test_fstree_from_file TESTS += test_fstree_init test_filename_sane test_filename_sane_w32 +TESTS += test_fstree_from_dir TESTS += test_tar_ustar0 test_tar_ustar1 test_tar_ustar2 test_tar_ustar3 TESTS += test_tar_ustar4 test_tar_ustar5 test_tar_ustar6 TESTS += test_tar_gnu0 test_tar_gnu1 test_tar_gnu2 test_tar_gnu3 test_tar_gnu4 diff --git a/tests/fstree_from_dir.c b/tests/fstree_from_dir.c new file mode 100644 index 0000000..8e08aeb --- /dev/null +++ b/tests/fstree_from_dir.c @@ -0,0 +1,379 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * fstree_from_dir.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" + +#include "fstree.h" +#include "test.h" + +static void check_hierarchy(tree_node_t *root, bool recursive) +{ + tree_node_t *n, *m; + + n = root->data.dir.children; + TEST_NOT_NULL(n); + TEST_STR_EQUAL(n->name, "CREDITS"); + TEST_ASSERT(S_ISREG(n->mode)); + TEST_ASSERT(n->parent == root); + + n = n->next; + TEST_NOT_NULL(n); + TEST_STR_EQUAL(n->name, "file-size"); + TEST_ASSERT(S_ISDIR(n->mode)); + TEST_ASSERT(n->parent == root); + + if (recursive) { + m = n->data.dir.children; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "12-digit.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "gnu.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "pax.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NULL(m); + } else { + TEST_NULL(n->data.dir.children); + } + + n = n->next; + TEST_NOT_NULL(n); + TEST_STR_EQUAL(n->name, "format-acceptance"); + TEST_ASSERT(S_ISDIR(n->mode)); + TEST_ASSERT(n->parent == root); + + if (recursive) { + m = n->data.dir.children; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "gnu-g.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "gnu.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "link_filled.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "pax.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "ustar-pre-posix.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "ustar.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "v7.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NULL(m); + } else { + TEST_NULL(n->data.dir.children); + } + + n = n->next; + TEST_NOT_NULL(n); + TEST_STR_EQUAL(n->name, "large-mtime"); + TEST_ASSERT(S_ISDIR(n->mode)); + TEST_ASSERT(n->parent == root); + + if (recursive) { + m = n->data.dir.children; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "12-digit.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "gnu.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "pax.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NULL(m); + } else { + TEST_NULL(n->data.dir.children); + } + + n = n->next; + TEST_NOT_NULL(n); + TEST_STR_EQUAL(n->name, "long-paths"); + TEST_ASSERT(S_ISDIR(n->mode)); + TEST_ASSERT(n->parent == root); + + if (recursive) { + m = n->data.dir.children; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "gnu.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "pax.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "ustar.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NULL(m); + } else { + TEST_NULL(n->data.dir.children); + } + + n = n->next; + TEST_NOT_NULL(n); + TEST_STR_EQUAL(n->name, "negative-mtime"); + TEST_ASSERT(S_ISDIR(n->mode)); + TEST_ASSERT(n->parent == root); + + if (recursive) { + m = n->data.dir.children; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "gnu.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "pax.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NULL(m); + } else { + TEST_NULL(n->data.dir.children); + } + + n = n->next; + TEST_NOT_NULL(n); + TEST_STR_EQUAL(n->name, "sparse-files"); + TEST_ASSERT(S_ISDIR(n->mode)); + TEST_ASSERT(n->parent == root); + + if (recursive) { + m = n->data.dir.children; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "gnu-small.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "gnu.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "pax-gnu0-0.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "pax-gnu0-1.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "pax-gnu1-0.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NULL(m); + } else { + TEST_NULL(n->data.dir.children); + } + + n = n->next; + TEST_NOT_NULL(n); + TEST_STR_EQUAL(n->name, "sqfs.sha512"); + TEST_ASSERT(S_ISREG(n->mode)); + TEST_ASSERT(n->parent == root); + + n = n->next; + TEST_NOT_NULL(n); + TEST_STR_EQUAL(n->name, "user-group-largenum"); + TEST_ASSERT(S_ISDIR(n->mode)); + TEST_ASSERT(n->parent == root); + + if (recursive) { + m = n->data.dir.children; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "8-digit.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "gnu.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "pax.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NULL(m); + } else { + TEST_NULL(n->data.dir.children); + } + + n = n->next; + TEST_NOT_NULL(n); + TEST_STR_EQUAL(n->name, "xattr"); + TEST_ASSERT(S_ISDIR(n->mode)); + TEST_ASSERT(n->parent == root); + + if (recursive) { + m = n->data.dir.children; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "acl.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "xattr-libarchive.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "xattr-schily-binary.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NOT_NULL(m); + TEST_STR_EQUAL(m->name, "xattr-schily.tar"); + TEST_ASSERT(S_ISREG(m->mode)); + TEST_ASSERT(m->parent == n); + + m = m->next; + TEST_NULL(m); + } else { + TEST_NULL(n->data.dir.children); + } + + n = n->next; + TEST_NULL(n); +} + +int main(void) +{ + struct stat sb; + tree_node_t *n; + fstree_t fs; + + /* recursively scan into root */ + TEST_ASSERT(fstree_init(&fs, NULL) == 0); + TEST_ASSERT(fstree_from_dir(&fs, fs.root, TEST_PATH, 0) == 0); + + fstree_post_process(&fs); + check_hierarchy(fs.root, true); + fstree_cleanup(&fs); + + /* non-recursively scan into root */ + TEST_ASSERT(fstree_init(&fs, NULL) == 0); + TEST_ASSERT(fstree_from_dir(&fs, fs.root, + TEST_PATH, DIR_SCAN_NO_RECURSION) == 0); + + fstree_post_process(&fs); + check_hierarchy(fs.root, false); + fstree_cleanup(&fs); + + /* recursively scan into a sub-directory of root */ + memset(&sb, 0, sizeof(sb)); + sb.st_mode = S_IFDIR | 0755; + + TEST_ASSERT(fstree_init(&fs, NULL) == 0); + + n = fstree_mknode(fs.root, "foodir", 6, NULL, &sb); + TEST_NOT_NULL(n); + fs.root->data.dir.children = n; + + TEST_ASSERT(fstree_from_dir(&fs, n, TEST_PATH, 0) == 0); + + TEST_ASSERT(fs.root->data.dir.children == n); + TEST_NULL(n->next); + + fstree_post_process(&fs); + check_hierarchy(n, true); + fstree_cleanup(&fs); + + /* non-recursively scan into a sub-directory of root */ + memset(&sb, 0, sizeof(sb)); + sb.st_mode = S_IFDIR | 0755; + + TEST_ASSERT(fstree_init(&fs, NULL) == 0); + + n = fstree_mknode(fs.root, "foodir", 6, NULL, &sb); + TEST_NOT_NULL(n); + fs.root->data.dir.children = n; + + TEST_ASSERT(fstree_from_dir(&fs, n, + TEST_PATH, DIR_SCAN_NO_RECURSION) == 0); + + TEST_ASSERT(fs.root->data.dir.children == n); + TEST_NULL(n->next); + + fstree_post_process(&fs); + check_hierarchy(n, false); + fstree_cleanup(&fs); + + return EXIT_SUCCESS; +} -- cgit v1.2.3