summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-06-22 00:06:16 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-06-22 00:07:41 +0200
commit79e67f767bb34448c75312a538dc9f06a365448f (patch)
tree344b9fc2e31535a1e8e206e9c86077cfcc057676 /tests
parent5e18393c2b2964c69054509747f09435006ae825 (diff)
Add test cases for adding tree node by absolute path
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'tests')
-rw-r--r--tests/Makemodule.am5
-rw-r--r--tests/add_by_path.c108
2 files changed, 113 insertions, 0 deletions
diff --git a/tests/Makemodule.am b/tests/Makemodule.am
index 8ec9592..8706bca 100644
--- a/tests/Makemodule.am
+++ b/tests/Makemodule.am
@@ -16,8 +16,13 @@ test_mknode_dir_LDADD = libfstree.a
test_gen_inode_table_SOURCES = tests/gen_inode_table.c
test_gen_inode_table_LDADD = libfstree.a libutil.a
+test_add_by_path_SOURCES = tests/add_by_path.c
+test_add_by_path_LDADD = libfstree.a libutil.a
+
check_PROGRAMS += test_canonicalize_name test_mknode_simple test_mknode_slink
check_PROGRAMS += test_mknode_reg test_mknode_dir test_gen_inode_table
+check_PROGRAMS += test_add_by_path
TESTS += test_canonicalize_name test_mknode_simple test_mknode_slink
TESTS += test_mknode_reg test_mknode_dir test_gen_inode_table
+TESTS += test_add_by_path
diff --git a/tests/add_by_path.c b/tests/add_by_path.c
new file mode 100644
index 0000000..0023e0a
--- /dev/null
+++ b/tests/add_by_path.c
@@ -0,0 +1,108 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "fstree.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+
+int main(void)
+{
+ tree_node_t *a, *b;
+ struct stat sb;
+ fstree_t fs;
+
+ assert(fstree_init(&fs, 512, 1337, 0755, 21, 42) == 0);
+
+ memset(&sb, 0, sizeof(sb));
+ sb.st_mode = S_IFDIR | 0750;
+ sb.st_uid = 1000;
+ sb.st_gid = 100;
+
+ a = fstree_add_generic(&fs, "dir", &sb, NULL);
+ assert(a != NULL);
+ assert(strcmp(a->name, "dir") == 0);
+ assert(a->mode == sb.st_mode);
+ assert(a->uid == sb.st_uid);
+ assert(a->gid == sb.st_gid);
+ assert(a->parent == fs.root);
+ assert(a->next == NULL);
+ assert(fs.root->data.dir->children == a);
+ assert(!a->data.dir->created_implicitly);
+
+ memset(&sb, 0, sizeof(sb));
+ sb.st_mode = S_IFBLK | 0640;
+ sb.st_rdev = 1234;
+
+ b = fstree_add_generic(&fs, "blkdev", &sb, NULL);
+ assert(b != NULL);
+ assert(b != a);
+ assert(strcmp(b->name, "blkdev") == 0);
+ assert(b->mode == sb.st_mode);
+ assert(b->uid == sb.st_uid);
+ assert(b->gid == sb.st_gid);
+ assert(b->parent == fs.root);
+ assert(b->data.devno == sb.st_rdev);
+ assert(b->next == a);
+ assert(fs.root->data.dir->children == b);
+
+ assert(fstree_add_generic(&fs, "blkdev/foo", &sb, NULL) == NULL);
+ assert(errno == ENOTDIR);
+
+ assert(fstree_add_generic(&fs, "dir", &sb, NULL) == NULL);
+ assert(errno == EEXIST);
+
+ memset(&sb, 0, sizeof(sb));
+ sb.st_mode = S_IFDIR | 0755;
+ assert(fstree_add_generic(&fs, "dir", &sb, NULL) == NULL);
+ assert(errno == EEXIST);
+
+ memset(&sb, 0, sizeof(sb));
+ sb.st_mode = S_IFCHR | 0444;
+ b = fstree_add_generic(&fs, "dir/chrdev", &sb, NULL);
+ assert(b != NULL);
+ assert(b->mode == sb.st_mode);
+ assert(b->uid == sb.st_uid);
+ assert(b->gid == sb.st_gid);
+ assert(b->parent == a);
+ assert(b->data.devno == sb.st_rdev);
+ assert(b->next == NULL);
+ assert(a->data.dir->children == b);
+
+ b = fstree_add_generic(&fs, "dir/foo/chrdev", &sb, NULL);
+ assert(b != NULL);
+ assert(b->next == NULL);
+ assert(b->mode == sb.st_mode);
+ assert(b->uid == sb.st_uid);
+ assert(b->gid == sb.st_gid);
+ assert(b->parent != a);
+ assert(b->parent->parent == a);
+ assert(b->data.devno == sb.st_rdev);
+ assert(b->next == NULL);
+ assert(a->data.dir->children != b);
+
+ b = b->parent;
+ assert(b->data.dir->created_implicitly);
+ assert(b->mode == (S_IFDIR | 0755));
+ assert(b->uid == 21);
+ assert(b->gid == 42);
+
+ memset(&sb, 0, sizeof(sb));
+ sb.st_mode = S_IFDIR | 0750;
+ sb.st_uid = 1000;
+ sb.st_gid = 100;
+
+ a = fstree_add_generic(&fs, "dir/foo", &sb, NULL);
+ assert(a != NULL);
+ assert(a == b);
+ assert(!a->data.dir->created_implicitly);
+ assert(a->mode == sb.st_mode);
+ assert(a->uid == sb.st_uid);
+ assert(a->gid == sb.st_gid);
+
+ assert(fstree_add_generic(&fs, "dir/foo", &sb, NULL) == NULL);
+ assert(errno == EEXIST);
+
+ fstree_cleanup(&fs);
+ return EXIT_SUCCESS;
+}