From 0a4a263ad8abd34649be24dbb5b6e2b7fddfc0e4 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Fri, 1 Jun 2007 12:57:09 +0000 Subject: [PATCH] Implemented basic unit tests --- libisofs/trunk/Makefile.am | 16 +- libisofs/trunk/libisofs/file.c | 376 ++++++++++++++++++++++ libisofs/trunk/libisofs/file.h | 110 +++++++ libisofs/trunk/test.sh | 60 ++++ libisofs/trunk/test/test.c | 18 ++ libisofs/trunk/test/test.h | 26 ++ libisofs/trunk/test/test_exclude.c | 58 ++++ libisofs/trunk/test/test_file_hashtable.c | 104 ++++++ libisofs/trunk/test/test_tree.c | 258 +++++++++++++++ 9 files changed, 1011 insertions(+), 15 deletions(-) create mode 100644 libisofs/trunk/test.sh create mode 100644 libisofs/trunk/test/test.c create mode 100644 libisofs/trunk/test/test.h create mode 100644 libisofs/trunk/test/test_exclude.c create mode 100644 libisofs/trunk/test/test_file_hashtable.c create mode 100644 libisofs/trunk/test/test_tree.c diff --git a/libisofs/trunk/Makefile.am b/libisofs/trunk/Makefile.am index 368040f6..37d2e723 100644 --- a/libisofs/trunk/Makefile.am +++ b/libisofs/trunk/Makefile.am @@ -40,26 +40,12 @@ libinclude_HEADERS = \ ## Build test applications noinst_PROGRAMS = \ - test/iso + test/iso test_iso_CPPFLAGS = -Ilibisofs test_iso_LDADD = $(libisofs_libisofs_la_OBJECTS) $(THREAD_LIBS) test_iso_SOURCES = test/iso.c -## Build unit test - -check_PROGRAMS = \ - test/test - -test_test_CPPFLAGS = -Ilibisofs -test_test_LDADD = $(libisofs_libisofs_la_OBJECTS) $(THREAD_LIBS) - -test_test_SOURCES = \ - test/test_exclude.c \ - test/test_tree.c \ - test/test_file_hashtable.c \ - test/test.c - ## ========================================================================= ## diff --git a/libisofs/trunk/libisofs/file.c b/libisofs/trunk/libisofs/file.c index 53035163..4074f733 100644 --- a/libisofs/trunk/libisofs/file.c +++ b/libisofs/trunk/libisofs/file.c @@ -186,3 +186,379 @@ iso_file_table_lookup(struct iso_file_table *ft, struct iso_tree_node_file *f) } +#include +#include +#include +#include +#include +#include + +#include "file.h" +#include "tree.h" + +//TODO: refactor both hash and this hash table into a single one!! + +struct iso_file * +iso_file_new(struct iso_tree_node_file *f) +{ + struct iso_file *file = calloc(1, sizeof(struct iso_file)); + file->path = f->path; /*TODO strdup? it needs to be free on clear then */ + file->size = f->node.attrib.st_size; + file->nlink = 1; + file->real_dev = f->node.attrib.st_dev; + file->real_ino = f->node.attrib.st_ino; + file->sort_weight = f->sort_weight; + return file; +} + +static unsigned int +iso_file_table_hash(const char *path) +{ + unsigned int hash_num=0; + const char *c; + + c=path; + while(*c) + hash_num = (hash_num << 15) + (hash_num << 3) + (hash_num >> 3) + *c++; + + return hash_num % FILE_HASH_NODES; +} + +static inline unsigned int +iso_file_table_hash_inode(dev_t dev, ino_t ino) +{ + return (dev ^ ino) % FILE_HASH_NODES; +} + +struct iso_file_table* +iso_file_table_new(int cache_inodes) +{ + struct iso_file_table *table = calloc(1, sizeof(struct iso_file_table)); + table->cache_inodes = cache_inodes; + return table; +} + +static struct iso_file_hash_node * +iso_file_table_node_new(struct iso_file *file) +{ + struct iso_file_hash_node *node; + node = calloc(1, sizeof(struct iso_file_hash_node) ); + node->file = file; + return node; +} + +static void +iso_file_table_node_free(struct iso_file_hash_node *node) +{ + free(node->file); + free(node); +} + +void +iso_file_table_clear(struct iso_file_table *ft) +{ + int i; + + for (i=0; i < FILE_HASH_NODES; i++) { + struct iso_file_hash_node *node; + + node=ft->table[i]; + if (!node) + continue; + + ft->table[i] = NULL; + + do { + struct iso_file_hash_node *next; + + next = node->next; + iso_file_table_node_free(node); + node = next; + } while (node); + } +} + +/** + * return 0 if equal, != 0 if not + */ +static int +iso_table_compare_files(struct iso_file_table *ft, + struct iso_file *f1, struct iso_file *f2) +{ + if (ft->cache_inodes) { + return (f1->real_dev != f2->real_dev) || (f1->real_ino != f2->real_ino); + } else { + return strcmp(f1->path, f2->path); + } +} + +int +iso_file_table_add_file(struct iso_file_table *ft, struct iso_file *f) +{ + struct iso_file_hash_node *node; + unsigned int hash_num; + + /* find the hash number */ + if (ft->cache_inodes) + hash_num = iso_file_table_hash_inode(f->real_dev, f->real_ino); + else + hash_num = iso_file_table_hash(f->path); + + /* insert it */ + node = ft->table[hash_num]; + + /* unfortunately, we can't safely consider that a file + * won't be twice in the hash table so make sure it + * doesn't already exists */ + if (!node) { + ft->table[hash_num]=iso_file_table_node_new(f); + ft->count++; + return 1; + } + + /* if it's already in, we don't do anything */ + if (!iso_table_compare_files(ft, f, node->file)) + return 0; + + while (node->next) { + node = node->next; + + /* if it's already in, we don't do anything */ + if (!iso_table_compare_files(ft, f, node->file)) + return 0; + } + + node->next = iso_file_table_node_new(f); + ft->count++; + return 1; +} + +struct iso_file * +iso_file_table_lookup(struct iso_file_table *ft, struct iso_tree_node_file *f) +{ + struct iso_file_hash_node *node; + unsigned int hash_num; + int equal; + + /* find the hash number */ + if ( ft->cache_inodes ) + hash_num = iso_file_table_hash_inode(f->node.attrib.st_dev, + f->node.attrib.st_ino); + else + hash_num = iso_file_table_hash(f->path); + + node = ft->table[hash_num]; + + if (!node) + return NULL; + + equal = ft->cache_inodes ? + ((f->node.attrib.st_dev == node->file->real_dev) + && (f->node.attrib.st_ino == node->file->real_ino)) + : !strcmp(f->path, node->file->path); + if (equal) + return node->file; + + while (node->next) { + node = node->next; + + equal = ft->cache_inodes ? + ((f->node.attrib.st_dev == node->file->real_dev) + && (f->node.attrib.st_ino == node->file->real_ino)) + : !strcmp(f->path, node->file->path); + if (equal) + return node->file; + } + + return NULL; +} + + +#include +#include +#include +#include +#include +#include + +#include "file.h" +#include "tree.h" + +//TODO: refactor both hash and this hash table into a single one!! + +struct iso_file * +iso_file_new(struct iso_tree_node_file *f) +{ + struct iso_file *file = calloc(1, sizeof(struct iso_file)); + file->path = f->path; /*TODO strdup? it needs to be free on clear then */ + file->size = f->node.attrib.st_size; + file->nlink = 1; + file->real_dev = f->node.attrib.st_dev; + file->real_ino = f->node.attrib.st_ino; + file->sort_weight = f->sort_weight; + return file; +} + +static unsigned int +iso_file_table_hash(const char *path) +{ + unsigned int hash_num=0; + const char *c; + + c=path; + while(*c) + hash_num = (hash_num << 15) + (hash_num << 3) + (hash_num >> 3) + *c++; + + return hash_num % FILE_HASH_NODES; +} + +static inline unsigned int +iso_file_table_hash_inode(dev_t dev, ino_t ino) +{ + return (dev ^ ino) % FILE_HASH_NODES; +} + +struct iso_file_table* +iso_file_table_new(int cache_inodes) +{ + struct iso_file_table *table = calloc(1, sizeof(struct iso_file_table)); + table->cache_inodes = cache_inodes; + return table; +} + +static struct iso_file_hash_node * +iso_file_table_node_new(struct iso_file *file) +{ + struct iso_file_hash_node *node; + node = calloc(1, sizeof(struct iso_file_hash_node) ); + node->file = file; + return node; +} + +static void +iso_file_table_node_free(struct iso_file_hash_node *node) +{ + free(node->file); + free(node); +} + +void +iso_file_table_clear(struct iso_file_table *ft) +{ + int i; + + for (i=0; i < FILE_HASH_NODES; i++) { + struct iso_file_hash_node *node; + + node=ft->table[i]; + if (!node) + continue; + + ft->table[i] = NULL; + + do { + struct iso_file_hash_node *next; + + next = node->next; + iso_file_table_node_free(node); + node = next; + } while (node); + } +} + +/** + * return 0 if equal, != 0 if not + */ +static int +iso_table_compare_files(struct iso_file_table *ft, + struct iso_file *f1, struct iso_file *f2) +{ + if (ft->cache_inodes) { + return (f1->real_dev != f2->real_dev) || (f1->real_ino != f2->real_ino); + } else { + return strcmp(f1->path, f2->path); + } +} + +int +iso_file_table_add_file(struct iso_file_table *ft, struct iso_file *f) +{ + struct iso_file_hash_node *node; + unsigned int hash_num; + + /* find the hash number */ + if (ft->cache_inodes) + hash_num = iso_file_table_hash_inode(f->real_dev, f->real_ino); + else + hash_num = iso_file_table_hash(f->path); + + /* insert it */ + node = ft->table[hash_num]; + + /* unfortunately, we can't safely consider that a file + * won't be twice in the hash table so make sure it + * doesn't already exists */ + if (!node) { + ft->table[hash_num]=iso_file_table_node_new(f); + ft->count++; + return 1; + } + + /* if it's already in, we don't do anything */ + if (!iso_table_compare_files(ft, f, node->file)) + return 0; + + while (node->next) { + node = node->next; + + /* if it's already in, we don't do anything */ + if (!iso_table_compare_files(ft, f, node->file)) + return 0; + } + + node->next = iso_file_table_node_new(f); + ft->count++; + return 1; +} + +struct iso_file * +iso_file_table_lookup(struct iso_file_table *ft, struct iso_tree_node_file *f) +{ + struct iso_file_hash_node *node; + unsigned int hash_num; + int equal; + + /* find the hash number */ + if ( ft->cache_inodes ) + hash_num = iso_file_table_hash_inode(f->node.attrib.st_dev, + f->node.attrib.st_ino); + else + hash_num = iso_file_table_hash(f->path); + + node = ft->table[hash_num]; + + if (!node) + return NULL; + + equal = ft->cache_inodes ? + ((f->node.attrib.st_dev == node->file->real_dev) + && (f->node.attrib.st_ino == node->file->real_ino)) + : !strcmp(f->path, node->file->path); + if (equal) + return node->file; + + while (node->next) { + node = node->next; + + equal = ft->cache_inodes ? + ((f->node.attrib.st_dev == node->file->real_dev) + && (f->node.attrib.st_ino == node->file->real_ino)) + : !strcmp(f->path, node->file->path); + if (equal) + return node->file; + } + + return NULL; +} + + diff --git a/libisofs/trunk/libisofs/file.h b/libisofs/trunk/libisofs/file.h index 9c2745be..1d84f9ad 100644 --- a/libisofs/trunk/libisofs/file.h +++ b/libisofs/trunk/libisofs/file.h @@ -53,3 +53,113 @@ struct iso_file *iso_file_table_lookup(struct iso_file_table *ft, struct iso_tree_node_file *f); #endif /*FILE_H_*/ +/* -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 8; -*- */ +/* vim: set noet ts=8 sts=8 sw=8 : */ + +/** + * \file file.h + * + * Declare the structs to keep track of the files to be written into image. + * + */ + +#ifndef FILE_H_ +#define FILE_H_ + +#define FILE_HASH_NODES 2048 + +struct iso_file { + char *path; + off_t size; /**< size of this file */ + ino_t ino; /**< This will be the inode number on CD of the file (RR) */ + nlink_t nlink; /**< Number of hard links of the file on CD (RR) */ + size_t block; /**< Block where this file is to be written on image */ + dev_t real_dev; + ino_t real_ino; /**< for lookup by inode caching */ + int sort_weight; +}; + +struct iso_file_hash_node { + struct iso_file_hash_node *next; + struct iso_file *file; +}; + +struct iso_file_table { + struct iso_file_hash_node *table[FILE_HASH_NODES]; + size_t count; + int cache_inodes; /**< 1 to index by inode number */ +}; + +struct iso_tree_node_file; + +struct iso_file *iso_file_new(struct iso_tree_node_file*); + +struct iso_file_table *iso_file_table_new(int cache_inodes); + +/** + * Clear a hash table. All iso_file structs stored will also be freed, + * but not the path of each iso_file + */ +void iso_file_table_clear(struct iso_file_table *ft); + +int iso_file_table_add_file(struct iso_file_table *ft, struct iso_file *f); + +struct iso_file *iso_file_table_lookup(struct iso_file_table *ft, + struct iso_tree_node_file *f); + +#endif /*FILE_H_*/ +/* -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 8; -*- */ +/* vim: set noet ts=8 sts=8 sw=8 : */ + +/** + * \file file.h + * + * Declare the structs to keep track of the files to be written into image. + * + */ + +#ifndef FILE_H_ +#define FILE_H_ + +#define FILE_HASH_NODES 2048 + +struct iso_file { + char *path; + off_t size; /**< size of this file */ + ino_t ino; /**< This will be the inode number on CD of the file (RR) */ + nlink_t nlink; /**< Number of hard links of the file on CD (RR) */ + size_t block; /**< Block where this file is to be written on image */ + dev_t real_dev; + ino_t real_ino; /**< for lookup by inode caching */ + int sort_weight; +}; + +struct iso_file_hash_node { + struct iso_file_hash_node *next; + struct iso_file *file; +}; + +struct iso_file_table { + struct iso_file_hash_node *table[FILE_HASH_NODES]; + size_t count; + int cache_inodes; /**< 1 to index by inode number */ +}; + +struct iso_tree_node_file; + +struct iso_file *iso_file_new(struct iso_tree_node_file*); + +struct iso_file_table *iso_file_table_new(int cache_inodes); + +/** + * Clear a hash table. All iso_file structs stored will also be freed, + * but not the path of each iso_file + */ +void iso_file_table_clear(struct iso_file_table *ft); + +int iso_file_table_add_file(struct iso_file_table *ft, struct iso_file *f); + +struct iso_file *iso_file_table_lookup(struct iso_file_table *ft, + struct iso_tree_node_file *f); + +#endif /*FILE_H_*/ diff --git a/libisofs/trunk/test.sh b/libisofs/trunk/test.sh new file mode 100644 index 00000000..8b6ca308 --- /dev/null +++ b/libisofs/trunk/test.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +TEST_ROOT=/tmp/libisofs_test + +rm -rf $TEST_ROOT + +#create test folders +mkdir -p $TEST_ROOT +mkdir -p $TEST_ROOT/dir1/dir11 + +touch $TEST_ROOT/dir1/dir11/a +echo "This file is to check correct file permissions. set them to 754" > $TEST_ROOT/dir1/permtest +chmod 754 $TEST_ROOT/dir1/permtest + +mkdir -p $TEST_ROOT/dir2 +ln -s $TEST_ROOT/dir1 "$TEST_ROOT/link to dir1" + +echo "README file" > $TEST_ROOT/README +ln -s $TEST_ROOT/README "$TEST_ROOT/link to readme" + +echo "No read file" > $TEST_ROOT/no_read +chmod 000 $TEST_ROOT/no_read + +if ! make check +then + exit 1 +fi + +test/test + +#!/bin/bash + +TEST_ROOT=/tmp/libisofs_test + +rm -rf $TEST_ROOT + +#create test folders +mkdir -p $TEST_ROOT +mkdir -p $TEST_ROOT/dir1/dir11 + +touch $TEST_ROOT/dir1/dir11/a +echo "This file is to check correct file permissions. set them to 754" > $TEST_ROOT/dir1/permtest +chmod 754 $TEST_ROOT/dir1/permtest + +mkdir -p $TEST_ROOT/dir2 +ln -s $TEST_ROOT/dir1 "$TEST_ROOT/link to dir1" + +echo "README file" > $TEST_ROOT/README +ln -s $TEST_ROOT/README "$TEST_ROOT/link to readme" + +echo "No read file" > $TEST_ROOT/no_read +chmod 000 $TEST_ROOT/no_read + +if ! make check +then + exit 1 +fi + +test/test + diff --git a/libisofs/trunk/test/test.c b/libisofs/trunk/test/test.c new file mode 100644 index 00000000..c7c3a915 --- /dev/null +++ b/libisofs/trunk/test/test.c @@ -0,0 +1,18 @@ +#include "test.h" + +int main(int argc, char **argv) +{ + test_tree(); + test_exclude(); + test_file_hashtable(); + return 0; +} +#include "test.h" + +int main(int argc, char **argv) +{ + test_tree(); + test_exclude(); + test_file_hashtable(); + return 0; +} diff --git a/libisofs/trunk/test/test.h b/libisofs/trunk/test/test.h new file mode 100644 index 00000000..be911bb7 --- /dev/null +++ b/libisofs/trunk/test/test.h @@ -0,0 +1,26 @@ +#ifndef TEST_H_ +#define TEST_H_ + +#include +#include + +void test_tree(); + +void test_exclude(); + +void test_file_hashtable(); + +#endif /*TEST_H_*/ +#ifndef TEST_H_ +#define TEST_H_ + +#include +#include + +void test_tree(); + +void test_exclude(); + +void test_file_hashtable(); + +#endif /*TEST_H_*/ diff --git a/libisofs/trunk/test/test_exclude.c b/libisofs/trunk/test/test_exclude.c new file mode 100644 index 00000000..0ff7aadf --- /dev/null +++ b/libisofs/trunk/test/test_exclude.c @@ -0,0 +1,58 @@ +/* + * Unit test for exclude.h + */ + + +#include "exclude.h" +#include "test.h" + +#include +#include + +void test_exclude() +{ + struct iso_hash_table table = { {0,}, 0}; + + printf("Testing exclude hashtable..."); + assert( !iso_exclude_lookup(&table, "/dir") ); + assert( !iso_exclude_lookup(&table, "/otherdir") ); + iso_exclude_add_path(&table, "/otherdir"); + assert( iso_exclude_lookup(&table, "/otherdir") ); + assert( !iso_exclude_lookup(&table, "/dir") ); + iso_exclude_add_path(&table, "/dir"); + assert( iso_exclude_lookup(&table, "/otherdir") ); + assert( iso_exclude_lookup(&table, "/dir") ); + iso_exclude_empty(&table); + assert( !iso_exclude_lookup(&table, "/dir") ); + assert( !iso_exclude_lookup(&table, "/otherdir") ); + printf("\tOK\n"); +} +/* + * Unit test for exclude.h + */ + + +#include "exclude.h" +#include "test.h" + +#include +#include + +void test_exclude() +{ + struct iso_hash_table table = { {0,}, 0}; + + printf("Testing exclude hashtable..."); + assert( !iso_exclude_lookup(&table, "/dir") ); + assert( !iso_exclude_lookup(&table, "/otherdir") ); + iso_exclude_add_path(&table, "/otherdir"); + assert( iso_exclude_lookup(&table, "/otherdir") ); + assert( !iso_exclude_lookup(&table, "/dir") ); + iso_exclude_add_path(&table, "/dir"); + assert( iso_exclude_lookup(&table, "/otherdir") ); + assert( iso_exclude_lookup(&table, "/dir") ); + iso_exclude_empty(&table); + assert( !iso_exclude_lookup(&table, "/dir") ); + assert( !iso_exclude_lookup(&table, "/otherdir") ); + printf("\tOK\n"); +} diff --git a/libisofs/trunk/test/test_file_hashtable.c b/libisofs/trunk/test/test_file_hashtable.c new file mode 100644 index 00000000..76e79ddf --- /dev/null +++ b/libisofs/trunk/test/test_file_hashtable.c @@ -0,0 +1,104 @@ +/* + * Unit test for exclude.h + */ + + +#include "test.h" +#include "file.h" +#include "tree.h" + +#include +#include +#include +#include + +static void test_cache_inodes() +{ + struct iso_file_table *table; + struct iso_tree_node_file *file1; + struct iso_tree_node_file *file2; + struct iso_tree_node_file *file3; + struct iso_file *iso1; + + printf("Testing file hashtable with cache inodes..."); + + table = iso_file_table_new(1); + + assert( table ); + assert( table->cache_inodes ); + + file1 = calloc(1, sizeof(struct iso_tree_node_file) ); + file1->node.name = "fileName"; + file1->node.attrib.st_size = 12; + file1->node.attrib.st_dev = 15; + file1->node.attrib.st_ino = 204; + file1->path = "/tmp/filename"; + file1->sort_weight = 1; + + iso1 = iso_file_new(file1); + + assert( !strcmp(iso1->path, "/tmp/filename") ); + + //TODO + + free( file1 ); + printf("\tOK\n"); +} + + +void test_file_hashtable() +{ + test_cache_inodes(); +} +/* + * Unit test for exclude.h + */ + + +#include "test.h" +#include "file.h" +#include "tree.h" + +#include +#include +#include +#include + +static void test_cache_inodes() +{ + struct iso_file_table *table; + struct iso_tree_node_file *file1; + struct iso_tree_node_file *file2; + struct iso_tree_node_file *file3; + struct iso_file *iso1; + + printf("Testing file hashtable with cache inodes..."); + + table = iso_file_table_new(1); + + assert( table ); + assert( table->cache_inodes ); + + file1 = calloc(1, sizeof(struct iso_tree_node_file) ); + file1->node.name = "fileName"; + file1->node.attrib.st_size = 12; + file1->node.attrib.st_dev = 15; + file1->node.attrib.st_ino = 204; + file1->path = "/tmp/filename"; + file1->sort_weight = 1; + + iso1 = iso_file_new(file1); + + assert( !strcmp(iso1->path, "/tmp/filename") ); + + //TODO + + free( file1 ); + printf("\tOK\n"); +} + + +void test_file_hashtable() +{ + test_cache_inodes(); +} diff --git a/libisofs/trunk/test/test_tree.c b/libisofs/trunk/test/test_tree.c new file mode 100644 index 00000000..f89a75cb --- /dev/null +++ b/libisofs/trunk/test/test_tree.c @@ -0,0 +1,258 @@ +/* + * Unit test for tree.h + */ + + +#include "libisofs.h" +#include "tree.h" +#include "test.h" + +#include +#include +#include +#include +#include +#include + + +static void test_new_root() { + struct iso_tree_node_dir *root; + printf("Testing iso_tree_new_root()..."); + root = iso_tree_new_root(); + assert(root); + assert(root->nchildren == 0); + assert(root->children == NULL); + assert(root->node.parent == NULL); + assert(root->node.name == NULL); + assert( S_ISDIR(root->node.attrib.st_mode) ); + printf("\tOK\n"); +} + +static void test_add_dir() { + struct iso_tree_node_dir *root; + struct iso_tree_node_dir *dir; + + printf("Testing iso_tree_add_dir()..."); + root = iso_tree_new_root(); + dir = iso_tree_add_dir(root, "New dir name"); + assert(root); + assert(dir); + assert(dir->node.parent == root); + assert(root->nchildren == 1); + assert(root->children[0] == (struct iso_tree_node *)dir); + assert( strcmp(dir->node.name, "New dir name") == 0 ); + assert( S_ISDIR(dir->node.attrib.st_mode) ); + printf("\tOK\n"); +} + +static void test_add_file() { + struct iso_tree_node_dir *root; + struct iso_tree_node_file *file; + + printf("Testing iso_tree_add_file()..."); + root = iso_tree_new_root(); + file = (struct iso_tree_node_file *) + iso_tree_add_file(root, "/tmp/libisofs_test/README"); + assert(root); + assert(file); + assert(file->node.parent == root); + assert(root->nchildren == 1); + assert(root->children[0] == (struct iso_tree_node *)file); + assert( strcmp(file->node.name, "README") == 0 ); + assert( strcmp(file->path, "/tmp/libisofs_test/README") == 0 ); + assert( S_ISREG(file->node.attrib.st_mode) ); + printf("\tOK\n"); +} + +static void test_add_symlink() { + struct iso_tree_node_dir *root; + struct iso_tree_node *lnk; + + printf("Testing iso_tree_add_symlink()..."); + root = iso_tree_new_root(); + lnk = iso_tree_add_symlink(root, "read", "/tmp/libisofs_test/README"); + assert(root); + assert(lnk); + assert(lnk->parent == root); + assert(root->nchildren == 1); + assert(root->children[0] == (struct iso_tree_node *)lnk); + assert( strcmp(lnk->name, "read") == 0 ); + assert( strcmp( ((struct iso_tree_node_symlink*)lnk)->dest, + "/tmp/libisofs_test/README") == 0 ); + assert( S_ISLNK(lnk->attrib.st_mode) ); + printf("\tOK\n"); +} + +static void test_radd_dir() { + struct iso_tree_node_dir *root; + struct iso_tree_node_dir *dir; + struct iso_tree_node_dir *child; + struct iso_tree_radd_dir_behavior behavior = {0,0,0}; + + printf("Testing iso_tree_radd_dir()..."); + root = iso_tree_new_root(); + assert(root); + + dir = iso_tree_radd_dir(root, "/tmp/libisofs_test", &behavior); + assert(dir); + assert(dir->node.parent == root); + assert(root->nchildren == 1); + assert(root->children[0] == (struct iso_tree_node *)dir); + assert( S_ISDIR(dir->node.attrib.st_mode) ); + assert(dir->nchildren == 5); + assert( strcmp(dir->node.name, "libisofs_test") == 0 ); + + /* test _root_ children */ + child = (struct iso_tree_node_dir *)dir->children[0]; + assert( S_ISDIR(child->node.attrib.st_mode) ); + assert( child->nchildren == 2); + assert( strcmp(child->node.name, "dir1") == 0 ); + + child = (struct iso_tree_node_dir *)dir->children[1]; + assert( S_ISDIR(child->node.attrib.st_mode) ); + assert( child->nchildren == 0); + assert( strcmp(child->node.name, "dir2") == 0 ); + + //TODO write really full test + + printf("\tOK\n"); + //iso_tree_print( (struct iso_tree_node *)root, 4 ); +} + +void test_tree() +{ + test_new_root(); + test_add_dir(); + test_add_file(); + test_add_symlink(); + test_radd_dir(); +} +/* + * Unit test for tree.h + */ + + +#include "libisofs.h" +#include "tree.h" +#include "test.h" + +#include +#include +#include +#include +#include +#include + + +static void test_new_root() { + struct iso_tree_node_dir *root; + printf("Testing iso_tree_new_root()..."); + root = iso_tree_new_root(); + assert(root); + assert(root->nchildren == 0); + assert(root->children == NULL); + assert(root->node.parent == NULL); + assert(root->node.name == NULL); + assert( S_ISDIR(root->node.attrib.st_mode) ); + printf("\tOK\n"); +} + +static void test_add_dir() { + struct iso_tree_node_dir *root; + struct iso_tree_node_dir *dir; + + printf("Testing iso_tree_add_dir()..."); + root = iso_tree_new_root(); + dir = iso_tree_add_dir(root, "New dir name"); + assert(root); + assert(dir); + assert(dir->node.parent == root); + assert(root->nchildren == 1); + assert(root->children[0] == (struct iso_tree_node *)dir); + assert( strcmp(dir->node.name, "New dir name") == 0 ); + assert( S_ISDIR(dir->node.attrib.st_mode) ); + printf("\tOK\n"); +} + +static void test_add_file() { + struct iso_tree_node_dir *root; + struct iso_tree_node_file *file; + + printf("Testing iso_tree_add_file()..."); + root = iso_tree_new_root(); + file = (struct iso_tree_node_file *) + iso_tree_add_file(root, "/tmp/libisofs_test/README"); + assert(root); + assert(file); + assert(file->node.parent == root); + assert(root->nchildren == 1); + assert(root->children[0] == (struct iso_tree_node *)file); + assert( strcmp(file->node.name, "README") == 0 ); + assert( strcmp(file->path, "/tmp/libisofs_test/README") == 0 ); + assert( S_ISREG(file->node.attrib.st_mode) ); + printf("\tOK\n"); +} + +static void test_add_symlink() { + struct iso_tree_node_dir *root; + struct iso_tree_node *lnk; + + printf("Testing iso_tree_add_symlink()..."); + root = iso_tree_new_root(); + lnk = iso_tree_add_symlink(root, "read", "/tmp/libisofs_test/README"); + assert(root); + assert(lnk); + assert(lnk->parent == root); + assert(root->nchildren == 1); + assert(root->children[0] == (struct iso_tree_node *)lnk); + assert( strcmp(lnk->name, "read") == 0 ); + assert( strcmp( ((struct iso_tree_node_symlink*)lnk)->dest, + "/tmp/libisofs_test/README") == 0 ); + assert( S_ISLNK(lnk->attrib.st_mode) ); + printf("\tOK\n"); +} + +static void test_radd_dir() { + struct iso_tree_node_dir *root; + struct iso_tree_node_dir *dir; + struct iso_tree_node_dir *child; + struct iso_tree_radd_dir_behavior behavior = {0,0,0}; + + printf("Testing iso_tree_radd_dir()..."); + root = iso_tree_new_root(); + assert(root); + + dir = iso_tree_radd_dir(root, "/tmp/libisofs_test", &behavior); + assert(dir); + assert(dir->node.parent == root); + assert(root->nchildren == 1); + assert(root->children[0] == (struct iso_tree_node *)dir); + assert( S_ISDIR(dir->node.attrib.st_mode) ); + assert(dir->nchildren == 5); + assert( strcmp(dir->node.name, "libisofs_test") == 0 ); + + /* test _root_ children */ + child = (struct iso_tree_node_dir *)dir->children[0]; + assert( S_ISDIR(child->node.attrib.st_mode) ); + assert( child->nchildren == 2); + assert( strcmp(child->node.name, "dir1") == 0 ); + + child = (struct iso_tree_node_dir *)dir->children[1]; + assert( S_ISDIR(child->node.attrib.st_mode) ); + assert( child->nchildren == 0); + assert( strcmp(child->node.name, "dir2") == 0 ); + + //TODO write really full test + + printf("\tOK\n"); + //iso_tree_print( (struct iso_tree_node *)root, 4 ); +} + +void test_tree() +{ + test_new_root(); + test_add_dir(); + test_add_file(); + test_add_symlink(); + test_radd_dir(); +}