From 7f9c5c9065ba273bfea17aa8c7be082f1769ce18 Mon Sep 17 00:00:00 2001 From: Vreixo Formoso Date: Thu, 6 Dec 2007 22:45:16 +0100 Subject: [PATCH] Unit test for tree functions. Little fixes. --- Makefile.am | 3 +- src/tree.c | 8 +- test/test.c | 3 +- test/test.h | 1 + test/test_tree.c | 256 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 267 insertions(+), 4 deletions(-) create mode 100644 test/test_tree.c diff --git a/Makefile.am b/Makefile.am index 2e65e4d..1837e74 100644 --- a/Makefile.am +++ b/Makefile.am @@ -59,7 +59,8 @@ test_test_SOURCES = \ test/test.h \ test/test.c \ test/test_node.c \ - test/test_image.c + test/test_image.c \ + test/test_tree.c ## ========================================================================= ## diff --git a/src/tree.c b/src/tree.c index da54b35..9a0679d 100644 --- a/src/tree.c +++ b/src/tree.c @@ -130,6 +130,9 @@ int iso_tree_add_new_symlink(IsoDir *parent, const char *name, if (parent == NULL || name == NULL || dest == NULL) { return ISO_NULL_POINTER; } + if (link) { + *link = NULL; + } /* find place where to insert */ pos = &(parent->children); @@ -232,9 +235,12 @@ int iso_tree_add_new_special(IsoDir *parent, const char *name, mode_t mode, if (parent == NULL || name == NULL) { return ISO_NULL_POINTER; } - if (mode & (S_IFLNK | S_IFREG | S_IFDIR)) { + if (S_ISLNK(mode) || S_ISREG(mode) || S_ISDIR(mode)) { return ISO_WRONG_ARG_VALUE; } + if (special) { + *special = NULL; + } /* find place where to insert */ pos = &(parent->children); diff --git a/test/test.c b/test/test.c index 1f1675f..9e1e04a 100644 --- a/test/test.c +++ b/test/test.c @@ -4,12 +4,11 @@ static void create_test_suite() { add_node_suite(); add_image_suite(); + add_tree_suite(); } int main(int argc, char **argv) { - CU_pSuite pSuite = NULL; - /* initialize the CUnit test registry */ if (CUE_SUCCESS != CU_initialize_registry()) return CU_get_error(); diff --git a/test/test.h b/test/test.h index 3e74586..54ae9af 100644 --- a/test/test.h +++ b/test/test.h @@ -6,5 +6,6 @@ void add_node_suite(); void add_image_suite(); +void add_tree_suite(); #endif /*TEST_H_*/ diff --git a/test/test_tree.c b/test/test_tree.c new file mode 100644 index 0000000..4084383 --- /dev/null +++ b/test/test_tree.c @@ -0,0 +1,256 @@ +/* + * Unit test for node.h + */ + +#include "libisofs.h" +#include "node.h" +#include "test.h" +#include "error.h" + +#include + +static +void test_iso_tree_add_new_dir() +{ + int result; + IsoDir *root; + IsoDir *node1, *node2, *node3, *node4; + IsoImage *image; + + result = iso_image_new("volume_id", &image); + CU_ASSERT_EQUAL(result, 1); + root = iso_image_get_root(image); + CU_ASSERT_PTR_NOT_NULL(root); + + result = iso_tree_add_new_dir(root, "Dir1", &node1); + CU_ASSERT_EQUAL(result, 1); + CU_ASSERT_EQUAL(root->nchildren, 1); + CU_ASSERT_PTR_EQUAL(root->children, node1); + CU_ASSERT_PTR_NULL(node1->node.next); + CU_ASSERT_PTR_EQUAL(node1->node.parent, root); + CU_ASSERT_EQUAL(node1->node.type, LIBISO_DIR); + CU_ASSERT_STRING_EQUAL(node1->node.name, "Dir1"); + + /* creation of a second dir, to be inserted before */ + result = iso_tree_add_new_dir(root, "A node to be added first", &node2); + CU_ASSERT_EQUAL(result, 2); + CU_ASSERT_EQUAL(root->nchildren, 2); + CU_ASSERT_PTR_EQUAL(root->children, node2); + CU_ASSERT_PTR_EQUAL(node2->node.next, node1); + CU_ASSERT_PTR_NULL(node1->node.next); + CU_ASSERT_PTR_EQUAL(node2->node.parent, root); + CU_ASSERT_EQUAL(node2->node.type, LIBISO_DIR); + CU_ASSERT_STRING_EQUAL(node2->node.name, "A node to be added first"); + + /* creation of a 3rd node, to be inserted last */ + result = iso_tree_add_new_dir(root, "This node will be inserted last", &node3); + CU_ASSERT_EQUAL(result, 3); + CU_ASSERT_EQUAL(root->nchildren, 3); + CU_ASSERT_PTR_EQUAL(root->children, node2); + CU_ASSERT_PTR_EQUAL(node2->node.next, node1); + CU_ASSERT_PTR_EQUAL(node1->node.next, node3); + CU_ASSERT_PTR_NULL(node3->node.next); + CU_ASSERT_PTR_EQUAL(node3->node.parent, root); + CU_ASSERT_EQUAL(node3->node.type, LIBISO_DIR); + CU_ASSERT_STRING_EQUAL(node3->node.name, "This node will be inserted last"); + + /* force some failures */ + result = iso_tree_add_new_dir(NULL, "dsadas", &node4); + CU_ASSERT_EQUAL(result, ISO_NULL_POINTER); + result = iso_tree_add_new_dir(root, NULL, &node4); + CU_ASSERT_EQUAL(result, ISO_NULL_POINTER); + + /* try to insert a new dir with same name */ + result = iso_tree_add_new_dir(root, "This node will be inserted last", &node4); + CU_ASSERT_EQUAL(result, ISO_NODE_NAME_NOT_UNIQUE); + CU_ASSERT_EQUAL(root->nchildren, 3); + CU_ASSERT_PTR_EQUAL(root->children, node2); + CU_ASSERT_PTR_EQUAL(node2->node.next, node1); + CU_ASSERT_PTR_EQUAL(node1->node.next, node3); + CU_ASSERT_PTR_NULL(node3->node.next); + CU_ASSERT_PTR_NULL(node4); + + /* but pointer to new dir can be null */ + result = iso_tree_add_new_dir(root, "Another node", NULL); + CU_ASSERT_EQUAL(result, 4); + CU_ASSERT_EQUAL(root->nchildren, 4); + CU_ASSERT_PTR_EQUAL(node2->node.next->next, node1); + CU_ASSERT_STRING_EQUAL(node2->node.next->name, "Another node"); + + iso_image_unref(image); +} + +static +void test_iso_tree_add_new_symlink() +{ + int result; + IsoDir *root; + IsoSymlink *node1, *node2, *node3, *node4; + IsoImage *image; + + result = iso_image_new("volume_id", &image); + CU_ASSERT_EQUAL(result, 1); + root = iso_image_get_root(image); + CU_ASSERT_PTR_NOT_NULL(root); + + result = iso_tree_add_new_symlink(root, "Link1", "/path/to/dest", &node1); + CU_ASSERT_EQUAL(result, 1); + CU_ASSERT_EQUAL(root->nchildren, 1); + CU_ASSERT_PTR_EQUAL(root->children, node1); + CU_ASSERT_PTR_NULL(node1->node.next); + CU_ASSERT_PTR_EQUAL(node1->node.parent, root); + CU_ASSERT_EQUAL(node1->node.type, LIBISO_SYMLINK); + CU_ASSERT_STRING_EQUAL(node1->node.name, "Link1"); + CU_ASSERT_STRING_EQUAL(node1->dest, "/path/to/dest"); + + /* creation of a second link, to be inserted before */ + result = iso_tree_add_new_symlink(root, "A node to be added first", "/home/me", &node2); + CU_ASSERT_EQUAL(result, 2); + CU_ASSERT_EQUAL(root->nchildren, 2); + CU_ASSERT_PTR_EQUAL(root->children, node2); + CU_ASSERT_PTR_EQUAL(node2->node.next, node1); + CU_ASSERT_PTR_NULL(node1->node.next); + CU_ASSERT_PTR_EQUAL(node2->node.parent, root); + CU_ASSERT_EQUAL(node2->node.type, LIBISO_SYMLINK); + CU_ASSERT_STRING_EQUAL(node2->node.name, "A node to be added first"); + CU_ASSERT_STRING_EQUAL(node2->dest, "/home/me"); + + /* creation of a 3rd node, to be inserted last */ + result = iso_tree_add_new_symlink(root, "This node will be inserted last", + "/path/to/dest", &node3); + CU_ASSERT_EQUAL(result, 3); + CU_ASSERT_EQUAL(root->nchildren, 3); + CU_ASSERT_PTR_EQUAL(root->children, node2); + CU_ASSERT_PTR_EQUAL(node2->node.next, node1); + CU_ASSERT_PTR_EQUAL(node1->node.next, node3); + CU_ASSERT_PTR_NULL(node3->node.next); + CU_ASSERT_PTR_EQUAL(node3->node.parent, root); + CU_ASSERT_EQUAL(node3->node.type, LIBISO_SYMLINK); + CU_ASSERT_STRING_EQUAL(node3->node.name, "This node will be inserted last"); + CU_ASSERT_STRING_EQUAL(node3->dest, "/path/to/dest"); + + /* force some failures */ + result = iso_tree_add_new_symlink(NULL, "dsadas", "/path/to/dest", &node4); + CU_ASSERT_EQUAL(result, ISO_NULL_POINTER); + result = iso_tree_add_new_symlink(root, NULL, "/path/to/dest", &node4); + CU_ASSERT_EQUAL(result, ISO_NULL_POINTER); + result = iso_tree_add_new_symlink(root, "dsadas", NULL, &node4); + CU_ASSERT_EQUAL(result, ISO_NULL_POINTER); + + /* try to insert a new link with same name */ + result = iso_tree_add_new_symlink(root, "This node will be inserted last", "/", &node4); + CU_ASSERT_EQUAL(result, ISO_NODE_NAME_NOT_UNIQUE); + CU_ASSERT_EQUAL(root->nchildren, 3); + CU_ASSERT_PTR_EQUAL(root->children, node2); + CU_ASSERT_PTR_EQUAL(node2->node.next, node1); + CU_ASSERT_PTR_EQUAL(node1->node.next, node3); + CU_ASSERT_PTR_NULL(node3->node.next); + CU_ASSERT_PTR_NULL(node4); + + /* but pointer to new link can be null */ + result = iso_tree_add_new_symlink(root, "Another node", ".", NULL); + CU_ASSERT_EQUAL(result, 4); + CU_ASSERT_EQUAL(root->nchildren, 4); + CU_ASSERT_PTR_EQUAL(node2->node.next->next, node1); + CU_ASSERT_EQUAL(node2->node.next->type, LIBISO_SYMLINK); + CU_ASSERT_STRING_EQUAL(((IsoSymlink*)(node2->node.next))->dest, "."); + CU_ASSERT_STRING_EQUAL(node2->node.next->name, "Another node"); + + iso_image_unref(image); +} + +static +void test_iso_tree_add_new_special() +{ + int result; + IsoDir *root; + IsoSpecial *node1, *node2, *node3, *node4; + IsoImage *image; + + result = iso_image_new("volume_id", &image); + CU_ASSERT_EQUAL(result, 1); + root = iso_image_get_root(image); + CU_ASSERT_PTR_NOT_NULL(root); + + result = iso_tree_add_new_special(root, "Special1", S_IFSOCK | 0644, 0, &node1); + CU_ASSERT_EQUAL(result, 1); + CU_ASSERT_EQUAL(root->nchildren, 1); + CU_ASSERT_PTR_EQUAL(root->children, node1); + CU_ASSERT_PTR_NULL(node1->node.next); + CU_ASSERT_PTR_EQUAL(node1->node.parent, root); + CU_ASSERT_EQUAL(node1->node.type, LIBISO_SPECIAL); + CU_ASSERT_STRING_EQUAL(node1->node.name, "Special1"); + CU_ASSERT_EQUAL(node1->dev, 0); + CU_ASSERT_EQUAL(node1->node.mode, S_IFSOCK | 0644); + + /* creation of a block dev, to be inserted before */ + result = iso_tree_add_new_special(root, "A node to be added first", S_IFBLK | 0640, 34, &node2); + CU_ASSERT_EQUAL(result, 2); + CU_ASSERT_EQUAL(root->nchildren, 2); + CU_ASSERT_PTR_EQUAL(root->children, node2); + CU_ASSERT_PTR_EQUAL(node2->node.next, node1); + CU_ASSERT_PTR_NULL(node1->node.next); + CU_ASSERT_PTR_EQUAL(node2->node.parent, root); + CU_ASSERT_EQUAL(node2->node.type, LIBISO_SPECIAL); + CU_ASSERT_STRING_EQUAL(node2->node.name, "A node to be added first"); + CU_ASSERT_EQUAL(node2->dev, 34); + CU_ASSERT_EQUAL(node2->node.mode, S_IFBLK | 0640); + + /* creation of a 3rd node, to be inserted last */ + result = iso_tree_add_new_special(root, "This node will be inserted last", + S_IFCHR | 0440, 345, &node3); + CU_ASSERT_EQUAL(result, 3); + CU_ASSERT_EQUAL(root->nchildren, 3); + CU_ASSERT_PTR_EQUAL(root->children, node2); + CU_ASSERT_PTR_EQUAL(node2->node.next, node1); + CU_ASSERT_PTR_EQUAL(node1->node.next, node3); + CU_ASSERT_PTR_NULL(node3->node.next); + CU_ASSERT_PTR_EQUAL(node3->node.parent, root); + CU_ASSERT_EQUAL(node3->node.type, LIBISO_SPECIAL); + CU_ASSERT_STRING_EQUAL(node3->node.name, "This node will be inserted last"); + CU_ASSERT_EQUAL(node3->dev, 345); + CU_ASSERT_EQUAL(node3->node.mode, S_IFCHR | 0440); + + /* force some failures */ + result = iso_tree_add_new_special(NULL, "dsadas", S_IFBLK | 0440, 345, &node4); + CU_ASSERT_EQUAL(result, ISO_NULL_POINTER); + result = iso_tree_add_new_special(root, NULL, S_IFBLK | 0440, 345, &node4); + CU_ASSERT_EQUAL(result, ISO_NULL_POINTER); + result = iso_tree_add_new_special(root, "dsadas", S_IFDIR | 0666, 0, &node4); + CU_ASSERT_EQUAL(result, ISO_WRONG_ARG_VALUE); + result = iso_tree_add_new_special(root, "dsadas", S_IFREG | 0666, 0, &node4); + CU_ASSERT_EQUAL(result, ISO_WRONG_ARG_VALUE); + result = iso_tree_add_new_special(root, "dsadas", S_IFLNK | 0666, 0, &node4); + CU_ASSERT_EQUAL(result, ISO_WRONG_ARG_VALUE); + + /* try to insert a new special file with same name */ + result = iso_tree_add_new_special(root, "This node will be inserted last", S_IFIFO | 0666, 0, &node4); + CU_ASSERT_EQUAL(result, ISO_NODE_NAME_NOT_UNIQUE); + CU_ASSERT_EQUAL(root->nchildren, 3); + CU_ASSERT_PTR_EQUAL(root->children, node2); + CU_ASSERT_PTR_EQUAL(node2->node.next, node1); + CU_ASSERT_PTR_EQUAL(node1->node.next, node3); + CU_ASSERT_PTR_NULL(node3->node.next); + CU_ASSERT_PTR_NULL(node4); + + /* but pointer to new special can be null */ + result = iso_tree_add_new_special(root, "Another node", S_IFIFO | 0666, 0, NULL); + CU_ASSERT_EQUAL(result, 4); + CU_ASSERT_EQUAL(root->nchildren, 4); + CU_ASSERT_PTR_EQUAL(node2->node.next->next, node1); + CU_ASSERT_EQUAL(node2->node.next->type, LIBISO_SPECIAL); + CU_ASSERT_EQUAL(((IsoSpecial*)(node2->node.next))->dev, 0); + CU_ASSERT_EQUAL(node2->node.next->mode, S_IFIFO | 0666); + CU_ASSERT_STRING_EQUAL(node2->node.next->name, "Another node"); + + iso_image_unref(image); +} + +void add_tree_suite() +{ + CU_pSuite pSuite = CU_add_suite("Iso Tree Suite", NULL, NULL); + + CU_add_test(pSuite, "iso_tree_add_new_dir()", test_iso_tree_add_new_dir); + CU_add_test(pSuite, "iso_tree_add_new_symlink()", test_iso_tree_add_new_symlink); + CU_add_test(pSuite, "iso_tree_add_new_special()", test_iso_tree_add_new_special); +}