/*
 * Unit test for tree.h
 */

#include "libisofs.h"
#include "tree.h"
#include "test.h"

#include <assert.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

 
static void test_new_root() {
	struct iso_tree_node_dir *root;
	root = iso_tree_new_root();
	CU_ASSERT_PTR_NOT_NULL(root);
	CU_ASSERT_EQUAL(root->nchildren, 0);
	CU_ASSERT_PTR_NULL(root->children);
	CU_ASSERT_PTR_NULL(root->node.parent);
	CU_ASSERT_PTR_NULL(root->node.name);
	CU_ASSERT(S_ISDIR(root->node.attrib.st_mode) );
}

static void test_add_dir() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node_dir *dir;
	
	root = iso_tree_new_root();
	CU_ASSERT_PTR_NOT_NULL(root);
	dir = iso_tree_add_dir(root, "New dir name");
	CU_ASSERT_PTR_NOT_NULL(root);
	CU_ASSERT_PTR_NOT_NULL(dir);
	CU_ASSERT_PTR_EQUAL(dir->node.parent, root);
	CU_ASSERT_EQUAL(root->nchildren, 1);
	CU_ASSERT_PTR_EQUAL(root->children[0], (struct iso_tree_node *)dir);
	CU_ASSERT_STRING_EQUAL( dir->node.name, "New dir name");
	CU_ASSERT( S_ISDIR(dir->node.attrib.st_mode) );
	iso_tree_free((struct iso_tree_node *)root);
}

static void test_add_file() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node_file *file;
	
	root = iso_tree_new_root();
	file = (struct iso_tree_node_file *) 
		iso_tree_add_file(root, "/tmp/libisofs_test/README");
	CU_ASSERT_PTR_NOT_NULL(root);
	CU_ASSERT_PTR_NOT_NULL(file);
	CU_ASSERT_PTR_EQUAL(file->node.parent, root);
	CU_ASSERT_EQUAL(root->nchildren, 1);
	CU_ASSERT_PTR_EQUAL(root->children[0], (struct iso_tree_node *)file);
	CU_ASSERT_STRING_EQUAL( file->node.name, "README" );
	CU_ASSERT_STRING_EQUAL( file->loc.path, "/tmp/libisofs_test/README" );
	CU_ASSERT( S_ISREG(file->node.attrib.st_mode) );
	iso_tree_free((struct iso_tree_node *)root);
}

static void test_add_symlink() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node *lnk;
	
	root = iso_tree_new_root();
	lnk = iso_tree_add_symlink(root, "read", "/tmp/libisofs_test/README");
	CU_ASSERT_PTR_NOT_NULL(root);
	CU_ASSERT_PTR_NOT_NULL(lnk);
	CU_ASSERT_PTR_EQUAL(lnk->parent, root);
	CU_ASSERT_EQUAL(root->nchildren, 1);
	CU_ASSERT_PTR_EQUAL(root->children[0], (struct iso_tree_node *)lnk);
	CU_ASSERT_STRING_EQUAL( lnk->name, "read");
	CU_ASSERT_STRING_EQUAL( ((struct iso_tree_node_symlink*)lnk)->dest, 
			        "/tmp/libisofs_test/README" );
	CU_ASSERT( S_ISLNK(lnk->attrib.st_mode) );
	iso_tree_free((struct iso_tree_node *)root);
}

static void test_add_node() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node *node;
	
	root = iso_tree_new_root();
	
	/* test addition of a dir */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_PTR_EQUAL(node->parent, root);
	CU_ASSERT_EQUAL(root->nchildren, 1);
	CU_ASSERT_PTR_EQUAL(root->children[0], node);
	CU_ASSERT_STRING_EQUAL( node->name, "dir1");
	CU_ASSERT( ISO_ISDIR(node) );
	CU_ASSERT( S_ISDIR(node->attrib.st_mode) );
	
	/* test addition of a link */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_PTR_EQUAL(node->parent, root);
	CU_ASSERT_EQUAL(root->nchildren, 2);
	CU_ASSERT_PTR_EQUAL(root->children[1], node);
	CU_ASSERT( ISO_ISLNK(node) );
	CU_ASSERT( S_ISLNK(node->attrib.st_mode) );
	CU_ASSERT_STRING_EQUAL( node->name, "link to readme");
	CU_ASSERT_STRING_EQUAL( ((struct iso_tree_node_symlink*)node)->dest, 
			        "/tmp/libisofs_test/README" );
			        
	/* test addition of a file */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_PTR_EQUAL(node->parent, root);
	CU_ASSERT_EQUAL(root->nchildren, 3);
	CU_ASSERT_PTR_EQUAL(root->children[2], node);
	CU_ASSERT( S_ISREG(node->attrib.st_mode) );
	CU_ASSERT( ISO_ISREG(node) );
	CU_ASSERT_STRING_EQUAL( node->name, "README" );
	CU_ASSERT_STRING_EQUAL( ((struct iso_tree_node_file *) node)->loc.path, 
	                        "/tmp/libisofs_test/README" );
	                        
	/* test no exiting file */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/THISNOTEXIST");
	CU_ASSERT_PTR_NULL(node);
	CU_ASSERT_EQUAL(libisofs_errno, NO_FILE);
	CU_ASSERT_EQUAL(root->nchildren, 3);
	                        
	/* test no valid file */
	node = iso_tree_add_node(root, "/dev/zero");
	CU_ASSERT_PTR_NULL(node);
	CU_ASSERT_EQUAL(libisofs_errno, UNEXPECTED_FILE_TYPE);
	CU_ASSERT_EQUAL(root->nchildren, 3);
	                        
	/* test no read perm file */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/no_read");
	CU_ASSERT_PTR_NULL(node);
	CU_ASSERT_EQUAL(libisofs_errno, NO_READ_ACCESS);
	CU_ASSERT_EQUAL(root->nchildren, 3);
	
	iso_tree_free((struct iso_tree_node *)root);
}

static void test_set_name() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node *node;
	
	root = iso_tree_new_root();
	
	/* test on a dir */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_STRING_EQUAL( node->name, "dir1");
	iso_tree_node_set_name(node, "newname");
	CU_ASSERT_STRING_EQUAL( node->name, "newname");
	
	/* test on a link */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_STRING_EQUAL( node->name, "link to readme");
	iso_tree_node_set_name(node, "new link name");
	CU_ASSERT_STRING_EQUAL( node->name, "new link name");
	
	/* test on a file */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_STRING_EQUAL( node->name, "README" );
	iso_tree_node_set_name(node, "new file name");
	CU_ASSERT_STRING_EQUAL( node->name, "new file name");

	iso_tree_free((struct iso_tree_node *)root);
}

static void test_get_name() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node *node;
	
	root = iso_tree_new_root();
	
	/* test on a dir */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	CU_ASSERT_STRING_EQUAL( iso_tree_node_get_name(node), "dir1");
	iso_tree_node_set_name(node, "newname");
	CU_ASSERT_STRING_EQUAL( iso_tree_node_get_name(node), "newname");
	
	/* test on a link */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	CU_ASSERT_STRING_EQUAL( iso_tree_node_get_name(node), "link to readme");
	iso_tree_node_set_name(node, "new link name");
	CU_ASSERT_STRING_EQUAL( iso_tree_node_get_name(node), "new link name");
	
	/* test on a file */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	CU_ASSERT_STRING_EQUAL( iso_tree_node_get_name(node), "README" );
	iso_tree_node_set_name(node, "new file name");
	CU_ASSERT_STRING_EQUAL( iso_tree_node_get_name(node), "new file name");

	iso_tree_free((struct iso_tree_node *)root);
}

static void test_set_hidden() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node *node;
	
	root = iso_tree_new_root();
	
	/* test on a dir */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_EQUAL(node->hide_flags, 0);
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_RR);
	CU_ASSERT_EQUAL(node->hide_flags, LIBISO_HIDE_ON_RR);
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_JOLIET);
	CU_ASSERT_EQUAL(node->hide_flags, LIBISO_HIDE_ON_JOLIET);
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_RR|LIBISO_HIDE_ON_JOLIET);
	CU_ASSERT_EQUAL(node->hide_flags, LIBISO_HIDE_ON_RR|LIBISO_HIDE_ON_JOLIET);
	
	/* test on a link */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_EQUAL(node->hide_flags, 0);
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_RR);
	CU_ASSERT_EQUAL(node->hide_flags, LIBISO_HIDE_ON_RR);
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_JOLIET);
	CU_ASSERT_EQUAL(node->hide_flags, LIBISO_HIDE_ON_JOLIET);
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_RR|LIBISO_HIDE_ON_JOLIET);
	CU_ASSERT_EQUAL(node->hide_flags, LIBISO_HIDE_ON_RR|LIBISO_HIDE_ON_JOLIET);
	
	/* test on a file */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_EQUAL(node->hide_flags, 0);
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_RR);
	CU_ASSERT_EQUAL(node->hide_flags, LIBISO_HIDE_ON_RR);
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_JOLIET);
	CU_ASSERT_EQUAL(node->hide_flags, LIBISO_HIDE_ON_JOLIET);
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_RR|LIBISO_HIDE_ON_JOLIET);
	CU_ASSERT_EQUAL(node->hide_flags, LIBISO_HIDE_ON_RR|LIBISO_HIDE_ON_JOLIET);

	iso_tree_free((struct iso_tree_node *)root);
}

static void test_is_hidden() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node *node;
	
	root = iso_tree_new_root();
	
	/* test on a dir */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	CU_ASSERT_FALSE(iso_tree_node_is_hidden(node));
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_RR);
	CU_ASSERT_EQUAL(iso_tree_node_is_hidden(node), LIBISO_HIDE_ON_RR);
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_JOLIET);
	CU_ASSERT_EQUAL(iso_tree_node_is_hidden(node), LIBISO_HIDE_ON_JOLIET);
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_RR|LIBISO_HIDE_ON_JOLIET);
	CU_ASSERT_EQUAL(iso_tree_node_is_hidden(node), LIBISO_HIDE_ON_RR|LIBISO_HIDE_ON_JOLIET);
	
	/* test on a link */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	CU_ASSERT_FALSE(iso_tree_node_is_hidden(node));
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_RR);
	CU_ASSERT_EQUAL(iso_tree_node_is_hidden(node), LIBISO_HIDE_ON_RR);
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_JOLIET);
	CU_ASSERT_EQUAL(iso_tree_node_is_hidden(node), LIBISO_HIDE_ON_JOLIET);
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_RR|LIBISO_HIDE_ON_JOLIET);
	CU_ASSERT_EQUAL(iso_tree_node_is_hidden(node), LIBISO_HIDE_ON_RR|LIBISO_HIDE_ON_JOLIET);
	
	/* test on a file */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	CU_ASSERT_FALSE(iso_tree_node_is_hidden(node));
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_RR);
	CU_ASSERT_EQUAL(iso_tree_node_is_hidden(node), LIBISO_HIDE_ON_RR);
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_JOLIET);
	CU_ASSERT_EQUAL(iso_tree_node_is_hidden(node), LIBISO_HIDE_ON_JOLIET);
	iso_tree_node_set_hidden(node, LIBISO_HIDE_ON_RR|LIBISO_HIDE_ON_JOLIET);
	CU_ASSERT_EQUAL(iso_tree_node_is_hidden(node), LIBISO_HIDE_ON_RR|LIBISO_HIDE_ON_JOLIET);
	
	iso_tree_free((struct iso_tree_node *)root);
}

static void test_set_gid() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node *node;
	gid_t mygid = getgid();
	
	root = iso_tree_new_root();
	
	/* test on a dir */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_EQUAL(node->attrib.st_gid, mygid);
	iso_tree_node_set_gid(node, 1234);
	CU_ASSERT_EQUAL(node->attrib.st_gid, 1234);
	
	/* test on a link */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_EQUAL(node->attrib.st_gid, mygid);
	iso_tree_node_set_gid(node, 1234);
	CU_ASSERT_EQUAL(node->attrib.st_gid, 1234);
	
	/* test on a file */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_EQUAL(node->attrib.st_gid, mygid);
	iso_tree_node_set_gid(node, 1234);
	CU_ASSERT_EQUAL(node->attrib.st_gid, 1234);

	iso_tree_free((struct iso_tree_node *)root);
}

static void test_get_gid() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node *node;
	gid_t mygid = getgid();
	
	root = iso_tree_new_root();
	
	/* test on a dir */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	CU_ASSERT_EQUAL(iso_tree_node_get_gid(node), mygid);
	iso_tree_node_set_gid(node, 1234);
	CU_ASSERT_EQUAL(iso_tree_node_get_gid(node), 1234);
	
	/* test on a link */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	CU_ASSERT_EQUAL(iso_tree_node_get_gid(node), mygid);
	iso_tree_node_set_gid(node, 1234);
	CU_ASSERT_EQUAL(iso_tree_node_get_gid(node), 1234);
	
	/* test on a file */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	CU_ASSERT_EQUAL(iso_tree_node_get_gid(node), mygid);
	iso_tree_node_set_gid(node, 1234);
	CU_ASSERT_EQUAL(iso_tree_node_get_gid(node), 1234);

	iso_tree_free((struct iso_tree_node *)root);
}

static void test_set_uid() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node *node;
	uid_t myuid = getuid();
	
	root = iso_tree_new_root();
	
	/* test on a dir */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_EQUAL(node->attrib.st_uid, myuid);
	iso_tree_node_set_uid(node, 1234);
	CU_ASSERT_EQUAL(node->attrib.st_uid, 1234);
	
	/* test on a link */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_EQUAL(node->attrib.st_uid, myuid);
	iso_tree_node_set_uid(node, 1234);
	CU_ASSERT_EQUAL(node->attrib.st_uid, 1234);
	
	/* test on a file */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_EQUAL(node->attrib.st_uid, myuid);
	iso_tree_node_set_uid(node, 1234);
	CU_ASSERT_EQUAL(node->attrib.st_uid, 1234);

	//TODO

	iso_tree_free((struct iso_tree_node *)root);
}

static void test_get_uid() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node *node;
	uid_t myuid = getuid();
	
	root = iso_tree_new_root();
	
	/* test on a dir */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	CU_ASSERT_EQUAL(iso_tree_node_get_uid(node), myuid);
	iso_tree_node_set_uid(node, 1234);
	CU_ASSERT_EQUAL(iso_tree_node_get_uid(node), 1234);
	
	/* test on a link */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	CU_ASSERT_EQUAL(iso_tree_node_get_uid(node), myuid);
	iso_tree_node_set_uid(node, 1234);
	CU_ASSERT_EQUAL(iso_tree_node_get_uid(node), 1234);
	
	/* test on a file */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	CU_ASSERT_EQUAL(iso_tree_node_get_uid(node), myuid);
	iso_tree_node_set_uid(node, 1234);
	CU_ASSERT_EQUAL(iso_tree_node_get_uid(node), 1234);

	iso_tree_free((struct iso_tree_node *)root);
}

static void test_set_permissions() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node *node;
	
	root = iso_tree_new_root();
	
	/* test on a dir */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_EQUAL(node->attrib.st_mode, S_IFDIR | 0755);
	iso_tree_node_set_permissions(node, 0777);
	CU_ASSERT_EQUAL(node->attrib.st_mode, S_IFDIR | 0777);
	iso_tree_node_set_permissions(node, 0744);
	CU_ASSERT_EQUAL(node->attrib.st_mode, S_IFDIR | 0744);
	iso_tree_node_set_permissions(node, 0411);
	CU_ASSERT_EQUAL(node->attrib.st_mode, S_IFDIR | 0411);
	
	/* test on a link */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_EQUAL(node->attrib.st_mode, S_IFLNK | 0777);
	iso_tree_node_set_permissions(node, 0555);
	CU_ASSERT_EQUAL(node->attrib.st_mode, S_IFLNK | 0555);
	iso_tree_node_set_permissions(node, 0744);
	CU_ASSERT_EQUAL(node->attrib.st_mode, S_IFLNK | 0744);
	iso_tree_node_set_permissions(node, 0411);
	CU_ASSERT_EQUAL(node->attrib.st_mode, S_IFLNK | 0411);
	
	/* test on a file */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	CU_ASSERT_PTR_NOT_NULL(node);
	CU_ASSERT_EQUAL(node->attrib.st_mode, S_IFREG | 0555);
	iso_tree_node_set_permissions(node, 0777);
	CU_ASSERT_EQUAL(node->attrib.st_mode, S_IFREG | 0777);
	iso_tree_node_set_permissions(node, 0744);
	CU_ASSERT_EQUAL(node->attrib.st_mode, S_IFREG | 0744);
	iso_tree_node_set_permissions(node, 0411);
	CU_ASSERT_EQUAL(node->attrib.st_mode, S_IFREG | 0411);

	iso_tree_free((struct iso_tree_node *)root);
}

static void test_get_permissions() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node *node;
	
	root = iso_tree_new_root();
	
	/* test on a dir */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	CU_ASSERT_EQUAL(iso_tree_node_get_permissions(node), 0755);
	iso_tree_node_set_permissions(node, 0777);
	CU_ASSERT_EQUAL(iso_tree_node_get_permissions(node), 0777);
	iso_tree_node_set_permissions(node, 0744);
	CU_ASSERT_EQUAL(iso_tree_node_get_permissions(node), 0744);
	iso_tree_node_set_permissions(node, 0411);
	CU_ASSERT_EQUAL(iso_tree_node_get_permissions(node), 0411);
	
	/* test on a link */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	CU_ASSERT_EQUAL(iso_tree_node_get_permissions(node), 0777);
	iso_tree_node_set_permissions(node, 0555);
	CU_ASSERT_EQUAL(iso_tree_node_get_permissions(node), 0555);
	iso_tree_node_set_permissions(node, 0744);
	CU_ASSERT_EQUAL(iso_tree_node_get_permissions(node), 0744);
	iso_tree_node_set_permissions(node, 0411);
	CU_ASSERT_EQUAL(iso_tree_node_get_permissions(node), 0411);
	
	/* test on a file */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	CU_ASSERT_EQUAL(iso_tree_node_get_permissions(node), 0555);
	iso_tree_node_set_permissions(node, 0777);
	CU_ASSERT_EQUAL(iso_tree_node_get_permissions(node), 0777);
	iso_tree_node_set_permissions(node, 0744);
	CU_ASSERT_EQUAL(iso_tree_node_get_permissions(node), 0744);
	iso_tree_node_set_permissions(node, 0411);
	CU_ASSERT_EQUAL(iso_tree_node_get_permissions(node), 0411);

	iso_tree_free((struct iso_tree_node *)root);
}

static void test_set_sort_weight() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node_dir *dir;
	struct iso_tree_node_file *file;
	
	root = iso_tree_new_root();
	
	dir = iso_tree_add_dir(root, "New dir name");
	CU_ASSERT_PTR_NOT_NULL(dir);
	
	file = (struct iso_tree_node_file *) 
		iso_tree_add_file(dir, "/tmp/libisofs_test/README");
	CU_ASSERT_EQUAL(file->sort_weight, 0);
	iso_tree_node_set_sort_weight((struct iso_tree_node *) file, 15);
	CU_ASSERT_EQUAL(file->sort_weight, 15);
	iso_tree_node_set_sort_weight((struct iso_tree_node *) file, -15);
	CU_ASSERT_EQUAL(file->sort_weight, -15);
	
	/* changes to dir involve update files inside it */
	iso_tree_node_set_sort_weight((struct iso_tree_node *) dir, 28);
	CU_ASSERT_EQUAL(file->sort_weight, 28);

	iso_tree_free((struct iso_tree_node *)root);
}

static void test_set_dest() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node *node;
	struct iso_tree_node_symlink *link;
	
	root = iso_tree_new_root();
	
	node = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	link = (struct iso_tree_node_symlink *) node;
	CU_ASSERT_STRING_EQUAL( link->dest, "/tmp/libisofs_test/README");
	iso_tree_node_symlink_set_dest(link, "/tmp/inexistent");
	CU_ASSERT_STRING_EQUAL( link->dest, "/tmp/inexistent");
	
	iso_tree_free((struct iso_tree_node *)root);
}

static void test_get_dest() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node *node;
	struct iso_tree_node_symlink *link;
	
	root = iso_tree_new_root();
	
	node = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	link = (struct iso_tree_node_symlink *) node;
	CU_ASSERT_STRING_EQUAL( iso_tree_node_symlink_get_dest(link), "/tmp/libisofs_test/README");
	iso_tree_node_symlink_set_dest(link, "/tmp/inexistent");
	CU_ASSERT_STRING_EQUAL( iso_tree_node_symlink_get_dest(link), "/tmp/inexistent");
	
	iso_tree_free((struct iso_tree_node *)root);
}

static void test_get_node_type() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node *node;
	
	root = iso_tree_new_root();
	
	/* test on a dir */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	CU_ASSERT_EQUAL(iso_tree_node_get_type(node), LIBISO_NODE_DIR);
	
	/* test on a link */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	CU_ASSERT_EQUAL(iso_tree_node_get_type(node), LIBISO_NODE_SYMLINK);
	
	/* test on a file */
	node = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	CU_ASSERT_EQUAL(iso_tree_node_get_type(node), LIBISO_NODE_FILE);

	iso_tree_free((struct iso_tree_node *)root);
}

static void test_children_iter() {
	struct iso_tree_node_dir *root;
	struct iso_tree_iter *iter;
	struct iso_tree_node *child;
	struct iso_tree_node *node1, *node2, *node3;
	
	root = iso_tree_new_root();
	
	node1 = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	node2 = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	node3 = iso_tree_add_node(root, "/tmp/libisofs_test/README");

	/* get the iterator */
	iter = iso_tree_node_children(root);
	CU_ASSERT_PTR_NOT_NULL(iter);
	
	/* test correct iteration */
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node1);
	
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node2);
	
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node3);
	
	/* and NULL when no more children */
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NULL(child);
	
	iso_tree_iter_free(iter);

	/* now an iter on a empty dir */
	iter = iso_tree_node_children((struct iso_tree_node_dir *)node1);
	CU_ASSERT_PTR_NOT_NULL(iter);
	
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NULL(child);

	iso_tree_free((struct iso_tree_node *)root);
}

static void test_tree_node_take() {
	int res;
	struct iso_tree_node_dir *root;
	struct iso_tree_iter *iter;
	struct iso_tree_node *child;
	struct iso_tree_node *node1, *node2, *node3;
	
	root = iso_tree_new_root();
	
	node1 = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	node2 = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	node3 = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	
	/* we take a ref to each node to test current reference behavior */ 
	iso_tree_node_ref(node1);
	iso_tree_node_ref(node2);
	iso_tree_node_ref(node3);
	
	/* remove node2 */
	res = iso_tree_node_take(root, node2);
	CU_ASSERT_EQUAL(res, 0);
	CU_ASSERT_EQUAL(root->nchildren, 2);
	CU_ASSERT_PTR_NULL(node2->parent);
	
	/* node2 should keep both refs */
	CU_ASSERT_EQUAL(node2->refcount, 2);
	
	/* test iteration */
	iter = iso_tree_node_children(root);
	CU_ASSERT_PTR_NOT_NULL(iter);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node1);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node3);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NULL(child);
	iso_tree_iter_free(iter);
	
	/* try to remove again node2 */
	res = iso_tree_node_take(root, node2);
	CU_ASSERT_EQUAL(res, -1);
	CU_ASSERT_EQUAL(root->nchildren, 2);
	iso_tree_free(node2);

	/* ok, now remove node1, and then node3 */
	res = iso_tree_node_take(root, node1);
	CU_ASSERT_EQUAL(res, 0);
	CU_ASSERT_EQUAL(root->nchildren, 1);
	CU_ASSERT_EQUAL(node1->refcount, 2);
	CU_ASSERT_PTR_NULL(node1->parent);
	
	iter = iso_tree_node_children(root);
	CU_ASSERT_PTR_NOT_NULL(iter);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node3);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NULL(child);
	iso_tree_iter_free(iter);
	
	res = iso_tree_node_take(root, node3);
	CU_ASSERT_EQUAL(res, 0);
	CU_ASSERT_EQUAL(root->nchildren, 0);
	CU_ASSERT_EQUAL(node3->refcount, 2);
	CU_ASSERT_PTR_NULL(node3->parent);
	
	iter = iso_tree_node_children(root);
	CU_ASSERT_PTR_NOT_NULL(iter);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NULL(child);
	iso_tree_iter_free(iter);
	
	/* and try to remove on an empty dir */
	res = iso_tree_node_take(root, node3);
	CU_ASSERT_EQUAL(res, -1);
	
	iso_tree_free(node1);
	iso_tree_free(node3);
	
	iso_tree_free((struct iso_tree_node *)root);
	iso_tree_free(node1);
	iso_tree_free(node2);
	iso_tree_free(node3);
}

static void test_tree_node_remove() {
	int res;
	struct iso_tree_node_dir *root;
	struct iso_tree_iter *iter;
	struct iso_tree_node *child;
	struct iso_tree_node *node1, *node2, *node3;
	
	root = iso_tree_new_root();
	
	node1 = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	node2 = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	node3 = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	
	/* we take a ref to each node to test current reference behavior */ 
	iso_tree_node_ref(node1);
	iso_tree_node_ref(node2);
	iso_tree_node_ref(node3);
	
	/* remove node2 */
	res = iso_tree_node_remove(root, node2);
	CU_ASSERT_EQUAL(res, 0);
	CU_ASSERT_EQUAL(root->nchildren, 2);
	CU_ASSERT_PTR_NULL(node2->parent);
	
	/* node2 should be unref */
	CU_ASSERT_EQUAL(node2->refcount, 1);
	
	/* test iteration */
	iter = iso_tree_node_children(root);
	CU_ASSERT_PTR_NOT_NULL(iter);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node1);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node3);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NULL(child);
	iso_tree_iter_free(iter);
	
	/* try to remove again node2 */
	res = iso_tree_node_remove(root, node2);
	CU_ASSERT_EQUAL(res, -1);
	CU_ASSERT_EQUAL(root->nchildren, 2);
	CU_ASSERT_EQUAL(node2->refcount, 1);

	/* ok, now remove node1, and then node3 */
	res = iso_tree_node_remove(root, node1);
	CU_ASSERT_EQUAL(res, 0);
	CU_ASSERT_EQUAL(root->nchildren, 1);
	CU_ASSERT_EQUAL(node1->refcount, 1);
	CU_ASSERT_PTR_NULL(node1->parent);
	
	iter = iso_tree_node_children(root);
	CU_ASSERT_PTR_NOT_NULL(iter);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node3);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NULL(child);
	iso_tree_iter_free(iter);
	
	res = iso_tree_node_remove(root, node3);
	CU_ASSERT_EQUAL(res, 0);
	CU_ASSERT_EQUAL(root->nchildren, 0);
	CU_ASSERT_EQUAL(node3->refcount, 1);
	CU_ASSERT_PTR_NULL(node3->parent);
	
	iter = iso_tree_node_children(root);
	CU_ASSERT_PTR_NOT_NULL(iter);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NULL(child);
	iso_tree_iter_free(iter);
	
	/* and try to remove on an empty dir */
	res = iso_tree_node_remove(root, node3);
	CU_ASSERT_EQUAL(res, -1);
	CU_ASSERT_EQUAL(node3->refcount, 1);
	
	iso_tree_free((struct iso_tree_node *)root);
	iso_tree_free(node1);
	iso_tree_free(node2);
	iso_tree_free(node3);
}

static void test_tree_node_take_iter() {
	int res;
	struct iso_tree_node_dir *root;
	struct iso_tree_iter *iter;
	struct iso_tree_node *child;
	struct iso_tree_node *node1, *node2, *node3;
	
	root = iso_tree_new_root();
	
	node1 = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	node2 = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	node3 = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	
	/* we take a ref to each node to test current reference behavior */ 
	iso_tree_node_ref(node1);
	iso_tree_node_ref(node2);
	iso_tree_node_ref(node3);
	
	/* begin iteration */
	iter = iso_tree_node_children(root);
	CU_ASSERT_PTR_NOT_NULL(iter);
	
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node1);
	
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node2);
	
	/* ok, take node 2 */
	res = iso_tree_node_take_iter(iter);
	CU_ASSERT_EQUAL(res, 0);
	CU_ASSERT_EQUAL(root->nchildren, 2);
	CU_ASSERT_EQUAL(node2->refcount, 2);
	CU_ASSERT_PTR_NULL(node2->parent);
	
	/* the iter have to work after remove */
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node3);
	
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NULL(child);
	iso_tree_iter_free(iter);
	
	/* ok, now remove before the begining */
	iter = iso_tree_node_children(root);
	CU_ASSERT_PTR_NOT_NULL(iter);
	
	res = iso_tree_node_take_iter(iter);
	CU_ASSERT_TRUE(res < 0);
	
	/* and the iter still works */
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node1);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node3);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NULL(child);
	
	/* and now try to remove at the end */
	res = iso_tree_node_take_iter(iter);
	CU_ASSERT_TRUE(res < 0);
	iso_tree_iter_free(iter);
	
	/* ok, now remove all during iteration */
	iter = iso_tree_node_children(root);
	CU_ASSERT_PTR_NOT_NULL(iter);
	
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node1);
	
	res = iso_tree_node_take_iter(iter);
	CU_ASSERT_EQUAL(res, 0);
	CU_ASSERT_EQUAL(root->nchildren, 1);
	CU_ASSERT_EQUAL(node1->refcount, 2);
	CU_ASSERT_PTR_NULL(node1->parent);
	
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node3);
	
	res = iso_tree_node_take_iter(iter);
	CU_ASSERT_EQUAL(res, 0);
	CU_ASSERT_EQUAL(root->nchildren, 0);
	CU_ASSERT_EQUAL(node3->refcount, 2);
	CU_ASSERT_PTR_NULL(node3->parent);
	
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NULL(child);
	iso_tree_iter_free(iter);
	
	iso_tree_free(node1);
	iso_tree_free(node2);
	iso_tree_free(node3);
	
	iso_tree_free((struct iso_tree_node *)root);
	iso_tree_free(node1);
	iso_tree_free(node2);
	iso_tree_free(node3);
}

static void test_tree_node_remove_iter() {
	int res;
	struct iso_tree_node_dir *root;
	struct iso_tree_iter *iter;
	struct iso_tree_node *child;
	struct iso_tree_node *node1, *node2, *node3;
	
	root = iso_tree_new_root();
	
	node1 = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	node2 = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	node3 = iso_tree_add_node(root, "/tmp/libisofs_test/README");
	
	/* we take a ref to each node to test current reference behavior */ 
	iso_tree_node_ref(node1);
	iso_tree_node_ref(node2);
	iso_tree_node_ref(node3);
	
	/* begin iteration */
	iter = iso_tree_node_children(root);
	CU_ASSERT_PTR_NOT_NULL(iter);
	
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node1);
	
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node2);
	
	/* ok, remove node 2 */
	res = iso_tree_node_remove_iter(iter);
	CU_ASSERT_EQUAL(res, 0);
	CU_ASSERT_EQUAL(root->nchildren, 2);
	CU_ASSERT_EQUAL(node2->refcount, 1);
	CU_ASSERT_PTR_NULL(node2->parent);
	
	/* the iter have to work after remove */
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node3);
	
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NULL(child);
	iso_tree_iter_free(iter);
	
	/* ok, now remove before the begining */
	iter = iso_tree_node_children(root);
	CU_ASSERT_PTR_NOT_NULL(iter);
	
	res = iso_tree_node_remove_iter(iter);
	CU_ASSERT_TRUE(res < 0);
	
	/* and the iter still works */
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node1);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node3);
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NULL(child);
	
	/* and now try to remove at the end */
	res = iso_tree_node_remove_iter(iter);
	CU_ASSERT_TRUE(res < 0);
	iso_tree_iter_free(iter);
	
	/* ok, now remove all during iteration */
	iter = iso_tree_node_children(root);
	CU_ASSERT_PTR_NOT_NULL(iter);
	
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node1);
	
	res = iso_tree_node_remove_iter(iter);
	CU_ASSERT_EQUAL(res, 0);
	CU_ASSERT_EQUAL(root->nchildren, 1);
	CU_ASSERT_EQUAL(node1->refcount, 1);
	CU_ASSERT_PTR_NULL(node1->parent);
	
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NOT_NULL(child);
	CU_ASSERT_PTR_EQUAL(child, node3);
	
	res = iso_tree_node_remove_iter(iter);
	CU_ASSERT_EQUAL(res, 0);
	CU_ASSERT_EQUAL(root->nchildren, 0);
	CU_ASSERT_EQUAL(node3->refcount, 1);
	CU_ASSERT_PTR_NULL(node3->parent);
	
	child = iso_tree_iter_next(iter);
	CU_ASSERT_PTR_NULL(child);
	iso_tree_iter_free(iter);
	
	iso_tree_free((struct iso_tree_node *)root);
	iso_tree_free(node1);
	iso_tree_free(node2);
	iso_tree_free(node3);
}

static void test_tree_node_get_parent() {
	struct iso_tree_node_dir *root;
	struct iso_tree_node_dir *parent;
	struct iso_tree_node *node1, *node2, *node3;
	
	root = iso_tree_new_root();
	
	node1 = iso_tree_add_node(root, "/tmp/libisofs_test/dir1");
	node2 = iso_tree_add_node(root, "/tmp/libisofs_test/link to readme");
	node3 = iso_tree_add_node( (struct iso_tree_node_dir*)node1, 
	                           "/tmp/libisofs_test/README");
	
	parent = iso_tree_node_get_parent((struct iso_tree_node *)root);
	CU_ASSERT_PTR_NULL(parent);
	
	parent = iso_tree_node_get_parent(node1);
	CU_ASSERT_PTR_NOT_NULL(parent);
	CU_ASSERT_PTR_EQUAL(parent, root);
	
	parent = iso_tree_node_get_parent(node2);
	CU_ASSERT_PTR_NOT_NULL(parent);
	CU_ASSERT_PTR_EQUAL(parent, root);
	
	parent = iso_tree_node_get_parent(node3);
	CU_ASSERT_PTR_NOT_NULL(parent);
	CU_ASSERT_PTR_EQUAL(parent, node1);

	iso_tree_node_take(root, node1);
	parent = iso_tree_node_get_parent(node1);
	CU_ASSERT_PTR_NULL(parent);
	
	iso_tree_free((struct iso_tree_node *)root);
	iso_tree_free(node1);
}

void add_tree_suite()
{
	CU_pSuite pSuite = CU_add_suite("TreeSuite", NULL, NULL);
	
	CU_add_test(pSuite, "test of iso_tree_new_root()", test_new_root);
	CU_add_test(pSuite, "test of iso_tree_add_dir()", test_add_dir);
	CU_add_test(pSuite, "test of iso_tree_add_file()", test_add_file);
	CU_add_test(pSuite, "test of iso_tree_add_symlink()", test_add_symlink);
	CU_add_test(pSuite, "test of iso_tree_add_node()", test_add_node);
	
	CU_add_test(pSuite, "test of iso_tree_node_set_name()", test_set_name);
	CU_add_test(pSuite, "test of iso_tree_node_get_name()", test_get_name);
	CU_add_test(pSuite, "test of iso_tree_node_set_hidden()", test_set_hidden);
	CU_add_test(pSuite, "test of iso_tree_node_is_hidden()", test_is_hidden);
	CU_add_test(pSuite, "test of iso_tree_node_set_gid()", test_set_gid);
	CU_add_test(pSuite, "test of iso_tree_node_get_gid()", test_get_gid);
	CU_add_test(pSuite, "test of iso_tree_node_set_uid()", test_set_uid);
	CU_add_test(pSuite, "test of iso_tree_node_get_uid()", test_get_uid);
	CU_add_test(pSuite, "test of iso_tree_node_set_permissions()", test_set_permissions);
	CU_add_test(pSuite, "test of iso_tree_node_get_permissions()", test_get_permissions);
	CU_add_test(pSuite, "test of iso_tree_node_set_sort_weight()", test_set_sort_weight);
	CU_add_test(pSuite, "test of iso_tree_node_symlink_set_dest()", test_set_dest);
	CU_add_test(pSuite, "test of iso_tree_node_symlink_get_dest()", test_get_dest);
	CU_add_test(pSuite, "test of iso_tree_node_get_type()", test_get_node_type);

	CU_add_test(pSuite, "test of children iteration", test_children_iter);
	CU_add_test(pSuite, "test of iso_tree_node_take()", test_tree_node_take);
	CU_add_test(pSuite, "test of iso_tree_node_remove()", test_tree_node_remove);
	CU_add_test(pSuite, "test of iso_tree_node_take_iter()", test_tree_node_take_iter);
	CU_add_test(pSuite, "test of iso_tree_node_remove_iter()", test_tree_node_remove_iter);
	
	CU_add_test(pSuite, "test of iso_tree_node_get_parent()", test_tree_node_get_parent);
}
