2008-02-27 20:56:47 +03:00
|
|
|
#include <errno.h>
|
2012-10-16 13:04:13 +04:00
|
|
|
#include <stdlib.h>
|
2008-02-27 20:56:47 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
#include "torture.h"
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2013-01-28 23:35:35 +04:00
|
|
|
#include "std/c_private.h"
|
2008-02-27 20:56:47 +03:00
|
|
|
#include "std/c_dir.h"
|
2013-07-17 15:22:57 +04:00
|
|
|
#include "std/c_string.h"
|
2008-02-27 20:56:47 +03:00
|
|
|
|
|
|
|
const char *check_dir = "/tmp/check/c_mkdirs//with/check//";
|
|
|
|
const char *check_file = "/tmp/check/c_mkdirs/with/check/foobar.txt";
|
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
static void setup(void **state) {
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
|
|
|
|
rc = c_mkdirs(check_dir, 0755);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = system("touch /tmp/check/c_mkdirs/with/check/foobar.txt");
|
|
|
|
assert_int_equal(rc, 0);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
static void teardown(void **state) {
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
|
|
|
|
rc = c_rmdirs(check_dir);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int test_dir(const char *path, mode_t mode) {
|
2013-02-07 14:41:46 +04:00
|
|
|
csync_stat_t sb;
|
2008-02-27 20:56:47 +03:00
|
|
|
if (lstat(path, &sb) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! S_ISDIR(sb.st_mode)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME */
|
|
|
|
if ((sb.st_mode & mode) == mode) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
static void check_c_mkdirs_rmdirs(void **state)
|
2008-02-27 20:56:47 +03:00
|
|
|
{
|
2013-02-07 14:41:46 +04:00
|
|
|
csync_stat_t sb;
|
2012-10-16 13:04:13 +04:00
|
|
|
int rc;
|
2013-07-17 15:22:57 +04:00
|
|
|
mbchar_t *wcheck_dir;
|
2012-10-16 13:04:13 +04:00
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
|
|
|
|
rc = c_mkdirs(check_dir, 0755);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = test_dir(check_dir, 0755);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = c_rmdirs(check_dir);
|
|
|
|
assert_int_equal(rc, 0);
|
2013-07-17 15:22:57 +04:00
|
|
|
wcheck_dir = c_utf8_to_locale(check_dir);
|
|
|
|
rc = _tstat(wcheck_dir, &sb);
|
|
|
|
c_free_locale_string(wcheck_dir);
|
2012-10-16 13:04:13 +04:00
|
|
|
assert_int_equal(rc, -1);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
static void check_c_mkdirs_mode(void **state)
|
2008-02-27 20:56:47 +03:00
|
|
|
{
|
2013-02-07 14:41:46 +04:00
|
|
|
csync_stat_t sb;
|
2012-10-16 13:04:13 +04:00
|
|
|
int rc;
|
2013-07-17 15:22:57 +04:00
|
|
|
mbchar_t *wcheck_dir;
|
2012-10-16 13:04:13 +04:00
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
rc = c_mkdirs(check_dir, 0700);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = test_dir(check_dir, 0700);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = c_rmdirs(check_dir);
|
|
|
|
assert_int_equal(rc, 0);
|
2013-07-17 15:22:57 +04:00
|
|
|
wcheck_dir = c_utf8_to_locale(check_dir);
|
|
|
|
rc = _tstat(wcheck_dir, &sb);
|
2012-10-16 13:04:13 +04:00
|
|
|
assert_int_equal(rc, -1);
|
2013-07-17 15:22:57 +04:00
|
|
|
c_free_locale_string(wcheck_dir);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
static void check_c_mkdirs_existing_path(void **state)
|
2008-02-27 20:56:47 +03:00
|
|
|
{
|
2012-10-16 13:04:13 +04:00
|
|
|
int rc;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
(void) state; /* unused */
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
rc = c_mkdirs(check_dir, 0755);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
static void check_c_mkdirs_file(void **state)
|
2008-02-27 20:56:47 +03:00
|
|
|
{
|
2012-10-16 13:04:13 +04:00
|
|
|
int rc;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
(void) state; /* unused */
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
rc = c_mkdirs(check_file, 0755);
|
|
|
|
assert_int_equal(rc, -1);
|
|
|
|
assert_int_equal(errno, ENOTDIR);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
static void check_c_mkdirs_null(void **state)
|
|
|
|
{
|
|
|
|
(void) state; /* unused */
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
assert_int_equal(c_mkdirs(NULL, 0755), -1);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
static void check_c_isdir(void **state)
|
|
|
|
{
|
|
|
|
(void) state; /* unused */
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2013-07-04 16:09:55 +04:00
|
|
|
assert_int_equal(c_isdir(check_dir), 1);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
static void check_c_isdir_on_file(void **state)
|
|
|
|
{
|
|
|
|
(void) state; /* unused */
|
2008-05-07 13:09:56 +04:00
|
|
|
|
2013-07-04 16:09:55 +04:00
|
|
|
assert_int_equal(c_isdir(check_file), 0);
|
2012-10-16 13:04:13 +04:00
|
|
|
}
|
2008-05-07 13:09:56 +04:00
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
static void check_c_isdir_null(void **state)
|
|
|
|
{
|
|
|
|
(void) state; /* unused */
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
assert_int_equal(c_isdir(NULL), 0);
|
|
|
|
}
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 13:04:13 +04:00
|
|
|
int torture_run_tests(void)
|
|
|
|
{
|
|
|
|
const UnitTest tests[] = {
|
|
|
|
unit_test(check_c_mkdirs_rmdirs),
|
|
|
|
unit_test(check_c_mkdirs_mode),
|
|
|
|
unit_test_setup_teardown(check_c_mkdirs_existing_path, setup, teardown),
|
|
|
|
unit_test_setup_teardown(check_c_mkdirs_file, setup, teardown),
|
|
|
|
unit_test(check_c_mkdirs_null),
|
|
|
|
unit_test_setup_teardown(check_c_isdir, setup, teardown),
|
|
|
|
unit_test_setup_teardown(check_c_isdir_on_file, setup, teardown),
|
|
|
|
unit_test(check_c_isdir_null),
|
|
|
|
};
|
|
|
|
|
|
|
|
return run_tests(tests);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|