nextcloud-desktop/tests/std_tests/check_std_c_file.c

153 lines
3.7 KiB
C
Raw Normal View History

2008-02-27 20:56:47 +03:00
#include <errno.h>
#include <stdlib.h>
2008-02-27 20:56:47 +03:00
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "torture.h"
2008-02-27 20:56:47 +03:00
#include "std/c_file.h"
const char *check_dir = "/tmp/check";
const char *check_src_file = "/tmp/check/foo.txt";
const char *check_dst_file = "/tmp/check/bar.txt";
static int test_file(const char *path, mode_t mode) {
struct stat sb;
if (lstat(path, &sb) < 0) {
return -1;
}
if (! S_ISREG(sb.st_mode)) {
return -1;
}
if ((sb.st_mode & mode) == mode) {
return 0;
}
return -1;
}
static void setup(void **state) {
int rc;
2008-02-27 20:56:47 +03:00
(void) state; /* unused */
2008-02-27 20:56:47 +03:00
rc = system("mkdir -p /tmp/check");
assert_int_equal(rc, 0);
rc = system("echo 42 > /tmp/check/foo.txt");
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03:00
}
static void teardown(void **state) {
int rc;
(void) state; /* unused */
rc = system("rm -rf /tmp/check");
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03:00
}
static void check_c_copy(void **state)
2008-02-27 20:56:47 +03:00
{
int rc;
(void) state; /* unused */
rc = c_copy(check_src_file, check_dst_file, 0644);
assert_int_equal(rc, 0);
rc = test_file(check_dst_file, 0644);
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03:00
}
static void check_c_copy_same_file(void **state)
{
int rc;
2008-02-27 20:56:47 +03:00
(void) state; /* unused */
2008-02-27 20:56:47 +03:00
rc = c_copy(check_src_file, check_src_file, 0644);
assert_int_equal(rc, -1);
2008-02-27 20:56:47 +03:00
}
static void check_c_copy_isdir(void **state)
{
int rc;
2008-05-07 13:09:56 +04:00
(void) state; /* unused */
2008-05-07 13:09:56 +04:00
rc = c_copy(check_src_file, check_dir, 0644);
assert_int_equal(rc, -1);
assert_int_equal(errno, EISDIR);
rc = c_copy(check_dir, check_dst_file, 0644);
assert_int_equal(rc, -1);
assert_int_equal(errno, EISDIR);
}
2008-02-27 20:56:47 +03:00
static void check_c_compare_file(void **state)
{
int rc;
(void) state;
rc = c_copy(check_src_file, check_dst_file, 0644);
assert_int_equal(rc, 0);
rc = c_compare_file( check_src_file, check_dst_file );
assert_int_equal(rc, 1);
2013-03-22 13:23:17 +04:00
/* Check error conditions */
rc = c_compare_file( NULL, check_dst_file );
assert_int_equal(rc, -1);
rc = c_compare_file( check_dst_file, NULL );
assert_int_equal(rc, -1);
rc = c_compare_file( NULL, NULL );
assert_int_equal(rc, -1);
rc = c_compare_file( check_src_file, "/I_do_not_exist_in_the_filesystem.dummy");
assert_int_equal(rc, -1);
rc = c_compare_file( "/I_do_not_exist_in_the_filesystem.dummy", check_dst_file);
assert_int_equal(rc, -1);
rc = system("echo \"hallo42\" > /tmp/check/foo.txt");
assert_int_equal(rc, 0);
rc = system("echo \"hallo52\" > /tmp/check/bar.txt");
assert_int_equal(rc, 0);
2013-03-22 16:54:46 +04:00
rc = c_compare_file( check_src_file, check_dst_file );
assert_int_equal(rc, 0);
/* Create two 1MB random files */
rc = system("dd if=/dev/urandom of=/tmp/check/foo.txt bs=1024 count=1024");
assert_int_equal(rc, 0);
rc = system("dd if=/dev/urandom of=/tmp/check/bar.txt bs=1024 count=1024");
assert_int_equal(rc, 0);
rc = c_compare_file( check_src_file, check_dst_file );
assert_int_equal(rc, 0);
2013-03-22 16:54:46 +04:00
/* Create two 1MB random files with different size */
rc = system("dd if=/dev/urandom of=/tmp/check/foo.txt bs=1024 count=1024");
assert_int_equal(rc, 0);
rc = system("dd if=/dev/urandom of=/tmp/check/bar.txt bs=1024 count=1020");
assert_int_equal(rc, 0);
rc = c_compare_file( check_src_file, check_dst_file );
assert_int_equal(rc, 0);
2013-03-22 16:54:46 +04:00
/* compare two big files which are equal */
rc = c_copy(check_src_file, check_dst_file, 0644);
assert_int_equal(rc, 0);
}
int torture_run_tests(void)
{
const UnitTest tests[] = {
unit_test_setup_teardown(check_c_copy, setup, teardown),
unit_test(check_c_copy_same_file),
unit_test_setup_teardown(check_c_copy_isdir, setup, teardown),
unit_test_setup_teardown(check_c_compare_file, setup, teardown),
};
2008-02-27 20:56:47 +03:00
return run_tests(tests);
2008-02-27 20:56:47 +03:00
}