Add default skel for unit tests. Move test programs to demo.

This commit is contained in:
Vreixo Formoso
2007-11-29 22:06:56 +01:00
parent efff783602
commit 3c7f1285d6
8 changed files with 68 additions and 17 deletions

View File

@ -1,74 +0,0 @@
/*
* Copyright (c) 2007 Vreixo Formoso
*
* This file is part of the libisofs project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. See COPYING file for details.
*/
#include "libisofs.h"
#include "fsource.h"
#include <stdio.h>
#include <stdlib.h>
/*
* Little test program to test filesystem implementations.
* Outputs file contents to stdout!
*/
int main(int argc, char **argv)
{
int res;
IsoFilesystem *fs;
IsoFileSource *file;
struct stat info;
if (argc != 2) {
fprintf(stderr, "Usage: cat /path/to/file\n");
return 1;
}
/* create filesystem object */
res = iso_local_filesystem_new(&fs);
if (res < 0) {
fprintf(stderr, "Can't get local fs object, err = %d\n", res);
return 1;
}
res = fs->get_by_path(fs, argv[1], &file);
if (res < 0) {
fprintf(stderr, "Can't get file, err = %d\n", res);
return 1;
}
res = file->lstat(file, &info);
if (res < 0) {
fprintf(stderr, "Can't stat file, err = %d\n", res);
return 1;
}
if (S_ISDIR(info.st_mode)) {
fprintf(stderr, "Path refers to a directory!!\n");
return 1;
} else {
char buf[1024];
res = file->open(file);
if (res < 0) {
fprintf(stderr, "Can't open file, err = %d\n", res);
return 1;
}
while ((res = file->read(file, buf, 1024)) > 0) {
fwrite(buf, 1, res, stdout);
}
if (res < 0) {
fprintf(stderr, "Error reading, err = %d\n", res);
return 1;
}
file->close(file);
}
iso_file_source_unref(file);
iso_filesystem_unref(fs);
return 0;
}

View File

@ -1,129 +0,0 @@
/*
* Copyright (c) 2007 Vreixo Formoso
*
* This file is part of the libisofs project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. See COPYING file for details.
*/
#include "libisofs.h"
#include "fsource.h"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
/*
* Little test program to test filesystem implementations.
*
*/
static void
print_permissions(mode_t mode)
{
char perm[10];
//TODO suid, sticky...
perm[9] = '\0';
perm[8] = mode & S_IXOTH ? 'x' : '-';
perm[7] = mode & S_IWOTH ? 'w' : '-';
perm[6] = mode & S_IROTH ? 'r' : '-';
perm[5] = mode & S_IXGRP ? 'x' : '-';
perm[4] = mode & S_IWGRP ? 'w' : '-';
perm[3] = mode & S_IRGRP ? 'r' : '-';
perm[2] = mode & S_IXUSR ? 'x' : '-';
perm[1] = mode & S_IWUSR ? 'w' : '-';
perm[0] = mode & S_IRUSR ? 'r' : '-';
printf(" %s ",perm);
}
static void
print_type(mode_t mode)
{
switch(mode & S_IFMT) {
case S_IFSOCK: printf("[S] "); break;
case S_IFLNK: printf("[L] "); break;
case S_IFREG: printf("[R] "); break;
case S_IFBLK: printf("[B] "); break;
case S_IFDIR: printf("[D] "); break;
case S_IFIFO: printf("[F] "); break;
}
}
static void
print_file_src(IsoFileSource *file)
{
struct stat info;
char *name;
file->lstat(file, &info);
print_type(info.st_mode);
print_permissions(info.st_mode);
name = file->get_name(file);
printf(" %s", name);
free(name);
if (S_ISLNK(info.st_mode)) {
char buf[PATH_MAX];
file->readlink(file, buf, PATH_MAX);
printf(" -> %s\n", buf);
}
printf("\n");
}
int main(int argc, char **argv)
{
int res;
IsoFilesystem *fs;
IsoFileSource *dir;
IsoFileSource *file;
struct stat info;
if (argc != 2) {
fprintf(stderr, "Usage: lsl /path/to/file\n");
return 1;
}
/* create filesystem object */
res = iso_local_filesystem_new(&fs);
if (res < 0) {
fprintf(stderr, "Can't get local fs object, err = %d\n", res);
return 1;
}
res = fs->get_by_path(fs, argv[1], &dir);
if (res < 0) {
fprintf(stderr, "Can't get file, err = %d\n", res);
return 1;
}
res = dir->lstat(dir, &info);
if (res < 0) {
fprintf(stderr, "Can't stat file, err = %d\n", res);
return 1;
}
if (S_ISDIR(info.st_mode)) {
res = dir->open(dir);
if (res < 0) {
fprintf(stderr, "Can't open file, err = %d\n", res);
return 1;
}
while (dir->readdir(dir, &file) == 1) {
print_file_src(file);
iso_file_source_unref(file);
}
res = dir->close(dir);
if (res < 0) {
fprintf(stderr, "Can't close file, err = %d\n", res);
return 1;
}
} else {
print_file_src(dir);
}
iso_file_source_unref(dir);
iso_filesystem_unref(fs);
return 0;
}

23
test/test.c Normal file
View File

@ -0,0 +1,23 @@
#include "test.h"
static void create_test_suite()
{
add_node_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();
create_test_suite();
/* Run all tests using the console interface */
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}

9
test/test.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef TEST_H_
#define TEST_H_
#include <CUnit/Basic.h>
#include "libisofs.h"
void add_node_suite();
#endif /*TEST_H_*/

13
test/test_node.c Normal file
View File

@ -0,0 +1,13 @@
/*
* Unit test for node.h
*/
#include "libisofs.h"
#include "node.h"
#include "test.h"
void add_node_suite()
{
CU_pSuite pSuite = CU_add_suite("Node Test Suite", NULL, NULL);
}