Added test

parent 95cbd788
......@@ -276,6 +276,7 @@ int print_dir(const char *name, int type) {
int tree(const char *name) {
foreach_file(name, print_tree);
return 0;
}
/* --- REMOVE FILE/DIR --- */
......@@ -286,13 +287,18 @@ int rm_file(const char *name) {
return 0;
}
int rm_link(const char *name) {
/* TODO */
if (unlink(name) == -1) {
perror("unlink");
return -1;
}
return 0;
}
int rm_dir(const char *name, int type) {
if (type == 1) { /* directory should be empty now, so we can delete it */
if (rmdir(name) == -1)
if (rmdir(name) == -1) {
perror("rmdir");
return -1;
}
}
return 0;
}
......@@ -301,6 +307,7 @@ int rm(const char *name) {
struct stat info;
stat(name, &info);
/* TODO: Use rm_* funcs directly, so I don't have to implement error handling twice? */
int ret;
if (!file_exists(name)) {
printf("rm: Cannot remove '%s': No such file or directory\n", name);
return -1;
......@@ -308,12 +315,12 @@ int rm(const char *name) {
if (S_ISDIR(info.st_mode)) {
/* directory - remove files recursively and then remove dir */
foreach_file(name, rm_tree);
rmdir(name); /* TODO: error handling */
ret = rmdir(name); /* TODO: error handling */
} else {
/* file - remove file directly */
unlink(name); /* TODO: error handlink */
ret = unlink(name); /* TODO: error handlink */
}
return 0;
return ret;
}
/* --- COPY/MOVE FILE/DIR --- */
......@@ -442,21 +449,19 @@ printf("\n@@@CPMV@@@\n");
char dst_top[strlen(src) + 1];
memset(dst_top, '\0', sizeof(dst_top));
strncpy(dst_top, dst, strlen(src));
/* FIXME: This needs to check not if 'src' and 'dst' are same, but if 'src' is same as start of 'dst'
* for eaxmple 'src' -> 'src/subdir' is problem also
*/
/* printf("===compare\n>'%s'\n<'%s'\n", src, dst_top);*/
if (!strcmp(src, dst_top)) {
if (is_dir(src)) {
/* FIXME: This error message can sometime say 'dir//dir', but that's not such big problem */
printf("%s: cannot %s a directory '%s' into itself, '%s/%s'\n",
fn_name, act_name, src, src, dst);
} else {
printf("%s: '%s' and '%s' are the same file\n",
fn_name, src, dst);
}
if (is_dir(src) && !strcmp(src, dst_top)) {
/* FIXME: This error message can sometime say 'dir//dir', but that's not such big problem */
printf("%s: cannot %s a directory '%s' into itself, '%s/%s'\n",
fn_name, act_name, src, src, dst);
return -1;
}
if (!strcmp(src, dst)) {
printf("%s: '%s' and '%s' are the same file\n",
fn_name, src, dst);
return -1;
}
strcpy(real_src, src);
printf("source exists?\n<%s>=<%d>\n---\n", real_src, file_exists(real_src));
if (!file_exists(real_src)) {
......@@ -554,6 +559,7 @@ int find_file(const char *name) {
}
return 0;
}
int find_dir(const char *name, int type) {
return 0;
}
......
......@@ -9,6 +9,7 @@ C_TESTS := \
arguments \
events \
util \
files \
subprocess \
interpreter
......
/*
* Copyright 2019, CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This file is part of the turris updater.
*
* Updater is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Updater is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Updater. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ctest.h"
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "../src/lib/interpreter.h"
#include "../src/lib/file-funcs.h"
const char *test_file = "../testdir/test";
const char *test_file2 = "../testdir/test2";
const char *dst_file = "../testdir/test-copy";
const char *dst_dir = "../testdir/dst-dir";
const char *dst_dir2 = "../testdir/dst-dir2";
int generate_file(const char *path, int length) {
int f_path = open(path, O_WRONLY | O_CREAT | O_EXCL, 0777);
write(f_path, "abcdefgh", length);
return 0;
}
/* TODO: this is not test, make a function */
START_TEST(init) {
/* cleanup */
system("rm -rf ../testdir/*");
/* make files and dirs */
generate_file(test_file, 5);
generate_file(test_file2, 7);
mkdir(dst_dir, 0777);
mkdir(dst_dir2, 0777);
}
END_TEST
START_TEST(file_exist) {
printf ("file_exists returned %d\n", file_exists(test_file));
}
END_TEST
START_TEST(remove_file) {
int ret = rm_file(test_file);
ck_assert_int_eq(ret, 0);
}
END_TEST
START_TEST(copy_file) {
int ret;
/* copy file to new file */
ret = cp(test_file, dst_file);
ck_assert_int_eq(ret, 0);
/* copy file over existing file */
ret = cp(test_file2, dst_file);
ck_assert_int_eq(ret, 0);
/* copy file over itself (should fail) */
ret = cp(test_file, test_file);
ck_assert_int_eq(ret, -1);
/* copy file to directory */
ret = cp(test_file, dst_dir);
ck_assert_int_eq(ret, 0);
/* copy directory to directory */
}
END_TEST
/*
ck_assert();
ck_assert_int_eq();
*/
Suite *gen_test_suite(void) {
Suite *result = suite_create("File");
TCase *file = tcase_create("file");
tcase_set_timeout(file, 30);
tcase_add_test(file, init);
tcase_add_test(file, file_exist);
tcase_add_test(file, copy_file);
tcase_add_test(file, remove_file);
suite_add_tcase(result, file);
return result;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment