legacy/libisofs/branches/ZeroTwoEight/test/test_tree.c
2007-07-31 07:32:26 +00:00

396 lines
13 KiB
C

/*
* 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->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)->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_radd_dir() {
struct iso_tree_node_dir *root;
struct iso_tree_node_dir *child;
struct iso_tree_node_file *file;
struct iso_tree_radd_dir_behavior behavior = {0,0,0};
//TODO write really full test
root = iso_tree_new_root();
CU_ASSERT_PTR_NOT_NULL(root);
iso_tree_radd_dir(root, "/tmp/libisofs_test", &behavior);
/* test _root_ children */
/*
child = (struct iso_tree_node_dir *)root->children[0];
CU_ASSERT( S_ISDIR(child->node.attrib.st_mode) );
CU_ASSERT_EQUAL( child->nchildren, 2);
CU_ASSERT_STRING_EQUAL( child->node.name, "dir1" );
child = (struct iso_tree_node_dir *)root->children[1];
CU_ASSERT( S_ISDIR(child->node.attrib.st_mode) );
CU_ASSERT_EQUAL( child->nchildren, 0);
CU_ASSERT_STRING_EQUAL( child->node.name, "dir2" );
file = (struct iso_tree_node_file *)root->children[2];
CU_ASSERT( S_ISREG(file->node.attrib.st_mode) );
CU_ASSERT_STRING_EQUAL( file->node.name, "README" );
*/
//iso_tree_print( (struct iso_tree_node *)root, 4 );
}
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_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_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_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_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_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);
}
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_radd_dir()", test_radd_dir);
CU_add_test(pSuite, "test of iso_tree_node_set_name()", test_set_name);
CU_add_test(pSuite, "test of iso_tree_node_set_hidden()", test_set_hidden);
CU_add_test(pSuite, "test of iso_tree_node_set_gid()", test_set_gid);
CU_add_test(pSuite, "test of iso_tree_node_set_uid()", test_set_uid);
CU_add_test(pSuite, "test of iso_tree_node_set_permissions()", test_set_permissions);
CU_add_test(pSuite, "test of iso_tree_node_set_sort_weight()", test_set_sort_weight);
}