summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-06-19 16:07:55 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-06-20 20:39:30 +0200
commitd766a9b089aefff63bca3e9914f5332af65efd12 (patch)
tree507ed59787e1a61770831034ebf7c3ff20558193
parentf2981c2ff581e4e425c7b23da43aee29f25232c5 (diff)
Add unit tests
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r--.gitignore2
-rw-r--r--Makefile.am3
-rw-r--r--tests/Makemodule.am20
-rw-r--r--tests/canonicalize_name.c74
-rw-r--r--tests/mknode_dir.c55
-rw-r--r--tests/mknode_reg.c39
-rw-r--r--tests/mknode_simple.c93
-rw-r--r--tests/mknode_slink.c47
8 files changed, 333 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index a0fc326..06eb71e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -22,3 +22,5 @@ gensquashfs
rdsquashfs
sqfs2tar
tar2sqfs
+test_*
+test-*
diff --git a/Makefile.am b/Makefile.am
index 2af68c1..27bb063 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -6,11 +6,14 @@ AM_CFLAGS = $(WARN_CFLAGS)
noinst_LIBRARIES =
bin_PROGRAMS =
dist_man1_MANS =
+check_PROGRAMS =
EXTRA_DIST = autogen.sh LICENSE README
+TESTS =
include doc/Makemodule.am
include lib/Makemodule.am
include tar/Makemodule.am
include mkfs/Makemodule.am
include unpack/Makemodule.am
+include tests/Makemodule.am
diff --git a/tests/Makemodule.am b/tests/Makemodule.am
new file mode 100644
index 0000000..d7d0b70
--- /dev/null
+++ b/tests/Makemodule.am
@@ -0,0 +1,20 @@
+test_canonicalize_name_SOURCES = tests/canonicalize_name.c
+test_canonicalize_name_LDADD = libutil.a
+
+test_mknode_simple_SOURCES = tests/mknode_simple.c
+test_mknode_simple_LDADD = libfstree.a
+
+test_mknode_slink_SOURCES = tests/mknode_slink.c
+test_mknode_slink_LDADD = libfstree.a
+
+test_mknode_reg_SOURCES = tests/mknode_reg.c
+test_mknode_reg_LDADD = libfstree.a
+
+test_mknode_dir_SOURCES = tests/mknode_dir.c
+test_mknode_dir_LDADD = libfstree.a
+
+check_PROGRAMS += test_canonicalize_name test_mknode_simple test_mknode_slink
+check_PROGRAMS += test_mknode_reg test_mknode_dir
+
+TESTS += test_canonicalize_name test_mknode_simple test_mknode_slink
+TESTS += test_mknode_reg test_mknode_dir
diff --git a/tests/canonicalize_name.c b/tests/canonicalize_name.c
new file mode 100644
index 0000000..ad12752
--- /dev/null
+++ b/tests/canonicalize_name.c
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "util.h"
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+static const struct {
+ const char *in;
+ const char *out;
+} must_work[] = {
+ { "", "" },
+ { "/", "" },
+ { "\\", "" },
+ { "///", "" },
+ { "\\\\\\", "" },
+ { "/\\//\\\\/", "" },
+ { "foo/bar/test", "foo/bar/test" },
+ { "foo\\bar\\test", "foo/bar/test" },
+ { "/foo/bar/test/", "foo/bar/test" },
+ { "\\foo\\bar\\test\\", "foo/bar/test" },
+ { "///foo//bar//test///", "foo/bar/test" },
+ { "./foo/././bar/test/./.", "foo/bar/test" },
+ { "./foo/././", "foo" },
+ { ".", "" },
+ { "./", "" },
+ { "./.", "" },
+ { "foo/.../bar", "foo/.../bar" },
+ { "foo/.test/bar", "foo/.test/bar" },
+};
+
+static const char *must_not_work[] = {
+ "..",
+ "foo/../bar",
+ "../foo/bar",
+ "foo/bar/..",
+ "foo/bar/../",
+};
+
+int main(void)
+{
+ char buffer[512];
+ size_t i;
+
+ for (i = 0; i < sizeof(must_work) / sizeof(must_work[0]); ++i) {
+ strcpy(buffer, must_work[i].in);
+
+ if (canonicalize_name(buffer)) {
+ fprintf(stderr, "Test case rejected: '%s'\n",
+ must_work[i].in);
+ return EXIT_FAILURE;
+ }
+
+ if (strcmp(buffer, must_work[i].out) != 0) {
+ fprintf(stderr, "Expected result: %s\n",
+ must_work[i].out);
+ fprintf(stderr, "Actual result: %s\n", buffer);
+ return EXIT_FAILURE;
+ }
+ }
+
+ for (i = 0; i < sizeof(must_not_work) / sizeof(must_not_work[0]); ++i) {
+ strcpy(buffer, must_not_work[i]);
+
+ if (canonicalize_name(buffer) == 0) {
+ fprintf(stderr, "Test case accepted: '%s'\n",
+ must_not_work[i]);
+ fprintf(stderr, "Transformed into: '%s'\n", buffer);
+ return EXIT_FAILURE;
+ }
+ }
+
+ return EXIT_SUCCESS;
+}
diff --git a/tests/mknode_dir.c b/tests/mknode_dir.c
new file mode 100644
index 0000000..5efe7a8
--- /dev/null
+++ b/tests/mknode_dir.c
@@ -0,0 +1,55 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "fstree.h"
+
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+
+int main(void)
+{
+ tree_node_t *root, *a, *b;
+ struct stat sb;
+ fstree_t fs;
+
+ memset(&fs, 0, sizeof(fs));
+ memset(&sb, 0, sizeof(sb));
+ sb.st_mode = S_IFDIR | 0654;
+ sb.st_uid = 123;
+ sb.st_gid = 456;
+ sb.st_rdev = 789;
+ sb.st_size = 4096;
+
+ root = fstree_mknode(&fs, NULL, "rootdir", 7, (void *)0x100, &sb);
+ assert(root->uid == sb.st_uid);
+ assert(root->gid == sb.st_gid);
+ assert(root->mode == sb.st_mode);
+ assert((char *)root->name >= (char *)root->payload);
+ assert((char *)root->data.dir >= (char *)root->payload);
+ assert(root->name >= (char *)(root->data.dir + 1));
+ assert(strcmp(root->name, "rootdir") == 0);
+ assert(root->data.dir->children == NULL);
+ assert(root->parent == NULL);
+ assert(root->next == NULL);
+
+ a = fstree_mknode(&fs, root, "adir", 4, (void *)0x100, &sb);
+ assert(a->parent == root);
+ assert(a->next == NULL);
+ assert(root->data.dir->children == a);
+ assert(root->parent == NULL);
+ assert(root->next == NULL);
+
+ b = fstree_mknode(&fs, root, "bdir", 4, (void *)0x100, &sb);
+ assert(a->parent == root);
+ assert(b->parent == root);
+ assert(root->data.dir->children == b);
+ assert(b->next == a);
+ assert(a->next == NULL);
+ assert(root->parent == NULL);
+ assert(root->next == NULL);
+
+ free(root);
+ free(a);
+ free(b);
+
+ return EXIT_SUCCESS;
+}
diff --git a/tests/mknode_reg.c b/tests/mknode_reg.c
new file mode 100644
index 0000000..f614b2e
--- /dev/null
+++ b/tests/mknode_reg.c
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "fstree.h"
+
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+
+int main(void)
+{
+ tree_node_t *node;
+ struct stat sb;
+ fstree_t fs;
+
+ memset(&fs, 0, sizeof(fs));
+ fs.block_size = 512;
+
+ memset(&sb, 0, sizeof(sb));
+ sb.st_mode = S_IFREG | 0654;
+ sb.st_uid = 123;
+ sb.st_gid = 456;
+ sb.st_rdev = 789;
+ sb.st_size = 4096;
+
+ node = fstree_mknode(&fs, NULL, "filename", 8, "input", &sb);
+ assert(node->uid == sb.st_uid);
+ assert(node->gid == sb.st_gid);
+ assert(node->mode == sb.st_mode);
+ assert(node->parent == NULL);
+ assert((char *)node->name >= (char *)node->payload);
+ assert((char *)node->data.file >= (char *)node->payload);
+ assert(node->data.file->input_file > (char *)(node->data.file + 1) +
+ sizeof(uint32_t) * 4);
+ assert(node->name >= node->data.file->input_file + 6);
+ assert(strcmp(node->name, "filename") == 0);
+ assert(strcmp(node->data.file->input_file, "input") == 0);
+ free(node);
+
+ return EXIT_SUCCESS;
+}
diff --git a/tests/mknode_simple.c b/tests/mknode_simple.c
new file mode 100644
index 0000000..24b0c5e
--- /dev/null
+++ b/tests/mknode_simple.c
@@ -0,0 +1,93 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "fstree.h"
+
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+
+int main(void)
+{
+ tree_node_t *node;
+ struct stat sb;
+ fstree_t fs;
+
+ memset(&fs, 0, sizeof(fs));
+ memset(&sb, 0, sizeof(sb));
+ sb.st_mode = S_IFSOCK | 0654;
+ sb.st_uid = 123;
+ sb.st_gid = 456;
+ sb.st_rdev = 789;
+ sb.st_size = 1337;
+
+ node = fstree_mknode(&fs, NULL, "sockfile", 8, (void *)0x1000, &sb);
+ assert((char *)node->name >= (char *)node->payload);
+ assert(strcmp(node->name, "sockfile") == 0);
+ assert(node->uid == sb.st_uid);
+ assert(node->gid == sb.st_gid);
+ assert(node->mode == sb.st_mode);
+ assert(node->parent == NULL);
+ assert(node->data.dir == NULL);
+ assert(node->data.file == NULL);
+ assert(node->data.slink_target == NULL);
+ assert(node->data.devno == 0);
+ free(node);
+
+ memset(&fs, 0, sizeof(fs));
+ memset(&sb, 0, sizeof(sb));
+ sb.st_mode = S_IFIFO | 0654;
+ sb.st_uid = 123;
+ sb.st_gid = 456;
+ sb.st_rdev = 789;
+ sb.st_size = 1337;
+
+ node = fstree_mknode(&fs, NULL, "fifo", 4, (void *)0x1000, &sb);
+ assert((char *)node->name >= (char *)node->payload);
+ assert(strcmp(node->name, "fifo") == 0);
+ assert(node->uid == sb.st_uid);
+ assert(node->gid == sb.st_gid);
+ assert(node->mode == sb.st_mode);
+ assert(node->parent == NULL);
+ assert(node->data.dir == NULL);
+ assert(node->data.file == NULL);
+ assert(node->data.slink_target == NULL);
+ assert(node->data.devno == 0);
+ free(node);
+
+ memset(&fs, 0, sizeof(fs));
+ memset(&sb, 0, sizeof(sb));
+ sb.st_mode = S_IFBLK | 0654;
+ sb.st_uid = 123;
+ sb.st_gid = 456;
+ sb.st_rdev = 789;
+ sb.st_size = 1337;
+
+ node = fstree_mknode(&fs, NULL, "blkdev", 6, (void *)0x1000, &sb);
+ assert((char *)node->name >= (char *)node->payload);
+ assert(strcmp(node->name, "blkdev") == 0);
+ assert(node->uid == sb.st_uid);
+ assert(node->gid == sb.st_gid);
+ assert(node->mode == sb.st_mode);
+ assert(node->data.devno == sb.st_rdev);
+ assert(node->parent == NULL);
+ free(node);
+
+ memset(&fs, 0, sizeof(fs));
+ memset(&sb, 0, sizeof(sb));
+ sb.st_mode = S_IFCHR | 0654;
+ sb.st_uid = 123;
+ sb.st_gid = 456;
+ sb.st_rdev = 789;
+ sb.st_size = 1337;
+
+ node = fstree_mknode(&fs, NULL, "chardev", 7, (void *)0x1000, &sb);
+ assert((char *)node->name >= (char *)node->payload);
+ assert(strcmp(node->name, "chardev") == 0);
+ assert(node->uid == sb.st_uid);
+ assert(node->gid == sb.st_gid);
+ assert(node->mode == sb.st_mode);
+ assert(node->data.devno == sb.st_rdev);
+ assert(node->parent == NULL);
+ free(node);
+
+ return EXIT_SUCCESS;
+}
diff --git a/tests/mknode_slink.c b/tests/mknode_slink.c
new file mode 100644
index 0000000..1cea92d
--- /dev/null
+++ b/tests/mknode_slink.c
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "fstree.h"
+
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+
+int main(void)
+{
+ tree_node_t *node;
+ struct stat sb;
+ fstree_t fs;
+
+ memset(&fs, 0, sizeof(fs));
+ memset(&sb, 0, sizeof(sb));
+ sb.st_mode = S_IFLNK | 0654;
+ sb.st_uid = 123;
+ sb.st_gid = 456;
+ sb.st_rdev = 789;
+ sb.st_size = 1337;
+
+ node = fstree_mknode(&fs, NULL, "symlink", 7, "target", &sb);
+ assert(node->uid == sb.st_uid);
+ assert(node->gid == sb.st_gid);
+ assert(node->mode == sb.st_mode);
+ assert(node->parent == NULL);
+ assert((char *)node->name >= (char *)node->payload);
+ assert(node->data.slink_target >= (char *)node->payload);
+ assert(node->name >= node->data.slink_target + 7);
+ assert(strcmp(node->name, "symlink") == 0);
+ assert(strcmp(node->data.slink_target, "target") == 0);
+ free(node);
+
+ node = fstree_mknode(&fs, NULL, "symlink", 7, "", &sb);
+ assert(node->uid == sb.st_uid);
+ assert(node->gid == sb.st_gid);
+ assert(node->mode == sb.st_mode);
+ assert(node->parent == NULL);
+ assert((char *)node->name >= (char *)node->payload);
+ assert(node->data.slink_target >= (char *)node->payload);
+ assert(node->name >= node->data.slink_target + 1);
+ assert(strcmp(node->name, "symlink") == 0);
+ assert(node->data.slink_target[0] == '\0');
+ free(node);
+
+ return EXIT_SUCCESS;
+}