diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2022-07-05 12:31:48 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2022-07-08 19:17:35 +0200 |
commit | 4a607edbdfc12f97da0810563fd2e699dcecaa71 (patch) | |
tree | 7e504c39477ba15ad9260c3ceae312a7e15f234d /tests/libsqfs | |
parent | 75063b2e14dacc13fcbeeba24e580198a7c1c638 (diff) |
Add test case for sqfs_tree_node_get_path
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'tests/libsqfs')
-rw-r--r-- | tests/libsqfs/Makemodule.am | 5 | ||||
-rw-r--r-- | tests/libsqfs/get_node_path.c | 130 |
2 files changed, 134 insertions, 1 deletions
diff --git a/tests/libsqfs/Makemodule.am b/tests/libsqfs/Makemodule.am index ca24caf..38f5006 100644 --- a/tests/libsqfs/Makemodule.am +++ b/tests/libsqfs/Makemodule.am @@ -10,8 +10,11 @@ test_xattr_writer_LDADD = libsquashfs.la libcompat.a xattr_benchmark_SOURCES = tests/libsqfs/xattr_benchmark.c xattr_benchmark_LDADD = libcommon.a libsquashfs.la libcompat.a +test_get_node_path_SOURCES = tests/libsqfs/get_node_path.c +test_get_node_path_LDADD = libcommon.a libsquashfs.la libcompat.a + LIBSQFS_TESTS = \ - test_abi test_table test_xattr_writer + test_abi test_table test_xattr_writer test_get_node_path if BUILD_TOOLS noinst_PROGRAMS += xattr_benchmark diff --git a/tests/libsqfs/get_node_path.c b/tests/libsqfs/get_node_path.c new file mode 100644 index 0000000..d2ca9f8 --- /dev/null +++ b/tests/libsqfs/get_node_path.c @@ -0,0 +1,130 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * get_node_path.c + * + * Copyright (C) 2022 David Oberhollenzer <goliath@infraroot.at> + */ +#include "config.h" +#include "compat.h" +#include "util/test.h" + +#include "sqfs/dir_reader.h" +#include "sqfs/error.h" + +int main(void) +{ + sqfs_tree_node_t *n0, *n1, *n2; + char *str; + int ret; + + n0 = calloc(1, sizeof(*n0) + 16); + TEST_NOT_NULL(n0); + + n1 = calloc(1, sizeof(*n1) + 16); + TEST_NOT_NULL(n1); + + n2 = calloc(1, sizeof(*n2) + 16); + TEST_NOT_NULL(n2); + + /* no parent -> must return "/" */ + ret = sqfs_tree_node_get_path(n0, &str); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(str); + TEST_STR_EQUAL(str, "/"); + sqfs_free(str); + + /* hiearchy levels */ + n1->parent = n0; + n0->children = n1; + strcpy((char *)n1->name, "bar"); + + n2->parent = n1; + n1->children = n2; + strcpy((char *)n2->name, "baz"); + + ret = sqfs_tree_node_get_path(n1, &str); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(str); + TEST_STR_EQUAL(str, "/bar"); + sqfs_free(str); + + ret = sqfs_tree_node_get_path(n2, &str); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(str); + TEST_STR_EQUAL(str, "/bar/baz"); + sqfs_free(str); + + /* root node must not have a name */ + strcpy((char *)n0->name, "foo"); + + ret = sqfs_tree_node_get_path(n2, &str); + TEST_EQUAL_I(ret, SQFS_ERROR_ARG_INVALID); + TEST_NULL(str); + n0->name[0] = '\0'; + + ret = sqfs_tree_node_get_path(n2, &str); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(str); + TEST_STR_EQUAL(str, "/bar/baz"); + sqfs_free(str); + + /* non-root nodes must have names */ + n1->name[0] = '\0'; + + ret = sqfs_tree_node_get_path(n2, &str); + TEST_EQUAL_I(ret, SQFS_ERROR_CORRUPTED); + TEST_NULL(str); + n1->name[0] = 'b'; + + ret = sqfs_tree_node_get_path(n2, &str); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(str); + TEST_STR_EQUAL(str, "/bar/baz"); + sqfs_free(str); + + /* some names are illegal */ + strcpy((char *)n1->name, ".."); + ret = sqfs_tree_node_get_path(n2, &str); + TEST_EQUAL_I(ret, SQFS_ERROR_CORRUPTED); + TEST_NULL(str); + + strcpy((char *)n1->name, "."); + ret = sqfs_tree_node_get_path(n2, &str); + TEST_EQUAL_I(ret, SQFS_ERROR_CORRUPTED); + TEST_NULL(str); + + strcpy((char *)n1->name, "a/b"); + ret = sqfs_tree_node_get_path(n2, &str); + TEST_EQUAL_I(ret, SQFS_ERROR_CORRUPTED); + TEST_NULL(str); + + strcpy((char *)n1->name, "bar"); + ret = sqfs_tree_node_get_path(n2, &str); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(str); + TEST_STR_EQUAL(str, "/bar/baz"); + sqfs_free(str); + + /* link loops must be detected */ + n0->parent = n2; + strcpy((char *)n0->name, "foo"); + + ret = sqfs_tree_node_get_path(n2, &str); + TEST_EQUAL_I(ret, SQFS_ERROR_LINK_LOOP); + TEST_NULL(str); + + n0->parent = NULL; + n0->name[0] = '\0'; + + ret = sqfs_tree_node_get_path(n2, &str); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(str); + TEST_STR_EQUAL(str, "/bar/baz"); + sqfs_free(str); + + /* cleanup */ + free(n0); + free(n1); + free(n2); + return EXIT_SUCCESS; +} |