2014-02-24 14:08:58 +04:00
|
|
|
/*
|
|
|
|
* libcsync -- a library to sync a directory with another
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2008-04-25 19:13:27 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2008-04-21 11:45:55 +04:00
|
|
|
#include <string.h>
|
2008-04-25 19:13:27 +04:00
|
|
|
#include <errno.h>
|
2008-04-21 11:45:55 +04:00
|
|
|
|
|
|
|
#include "csync_private.h"
|
2017-09-05 17:12:32 +03:00
|
|
|
#include "std/c_utf8.h"
|
2008-04-21 11:45:55 +04:00
|
|
|
#include "vio/csync_vio.h"
|
|
|
|
|
2017-09-07 12:02:05 +03:00
|
|
|
#include "torture.h"
|
|
|
|
|
2013-10-02 10:51:11 +04:00
|
|
|
#define CSYNC_TEST_DIR "/tmp/csync_test/"
|
|
|
|
#define CSYNC_TEST_DIRS "/tmp/csync_test/this/is/a/mkdirs/test"
|
|
|
|
#define CSYNC_TEST_FILE "/tmp/csync_test/file.txt"
|
|
|
|
|
|
|
|
#define MKDIR_MASK (S_IRWXU |S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
|
2008-04-25 19:13:27 +04:00
|
|
|
|
2013-03-14 17:29:31 +04:00
|
|
|
#define WD_BUFFER_SIZE 255
|
|
|
|
|
|
|
|
static char wd_buffer[WD_BUFFER_SIZE];
|
|
|
|
|
2017-08-21 21:05:00 +03:00
|
|
|
static int setup(void **state)
|
2012-10-19 20:14:28 +04:00
|
|
|
{
|
|
|
|
CSYNC *csync;
|
|
|
|
int rc;
|
2008-04-21 11:45:55 +04:00
|
|
|
|
2013-03-14 17:29:31 +04:00
|
|
|
assert_non_null(getcwd(wd_buffer, WD_BUFFER_SIZE));
|
|
|
|
|
2013-10-02 10:51:11 +04:00
|
|
|
rc = system("rm -rf /tmp/csync_test");
|
2012-10-19 20:14:28 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2008-04-25 19:13:27 +04:00
|
|
|
|
2017-09-04 20:06:13 +03:00
|
|
|
csync = new CSYNC("/tmp/check_csync1", "");
|
2008-04-25 19:13:27 +04:00
|
|
|
|
2017-09-04 16:09:09 +03:00
|
|
|
csync->current = LOCAL_REPLICA;
|
2012-10-19 20:14:28 +04:00
|
|
|
|
|
|
|
*state = csync;
|
2017-08-21 21:05:00 +03:00
|
|
|
return 0;
|
2008-04-21 11:45:55 +04:00
|
|
|
}
|
|
|
|
|
2017-08-21 21:05:00 +03:00
|
|
|
static int setup_dir(void **state) {
|
2012-10-19 20:14:28 +04:00
|
|
|
int rc;
|
2015-07-03 12:45:58 +03:00
|
|
|
mbchar_t *dir = c_utf8_path_to_locale(CSYNC_TEST_DIR);
|
2012-10-19 20:14:28 +04:00
|
|
|
|
|
|
|
setup(state);
|
|
|
|
|
2013-10-02 10:51:11 +04:00
|
|
|
rc = _tmkdir(dir, MKDIR_MASK);
|
2013-02-07 14:41:46 +04:00
|
|
|
c_free_locale_string(dir);
|
2012-10-19 20:14:28 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2013-03-14 17:29:31 +04:00
|
|
|
|
|
|
|
assert_non_null(getcwd(wd_buffer, WD_BUFFER_SIZE));
|
|
|
|
|
|
|
|
rc = chdir(CSYNC_TEST_DIR);
|
|
|
|
assert_int_equal(rc, 0);
|
2017-08-21 21:05:00 +03:00
|
|
|
return 0;
|
2008-04-25 19:13:27 +04:00
|
|
|
}
|
|
|
|
|
2017-08-21 21:05:00 +03:00
|
|
|
static int teardown(void **state) {
|
2017-08-14 17:19:52 +03:00
|
|
|
CSYNC *csync = (CSYNC*)*state;
|
2012-10-19 20:14:28 +04:00
|
|
|
int rc;
|
|
|
|
|
2017-09-04 20:06:13 +03:00
|
|
|
delete csync;
|
2012-10-19 20:14:28 +04:00
|
|
|
|
2013-03-14 17:29:31 +04:00
|
|
|
rc = chdir(wd_buffer);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
2013-10-02 10:51:11 +04:00
|
|
|
rc = system("rm -rf /tmp/csync_test/");
|
2012-10-19 20:14:28 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2008-04-25 19:13:27 +04:00
|
|
|
|
2012-10-19 20:14:28 +04:00
|
|
|
*state = NULL;
|
2017-08-21 21:05:00 +03:00
|
|
|
return 0;
|
2012-10-19 20:14:28 +04:00
|
|
|
}
|
|
|
|
|
2008-08-06 20:14:34 +04:00
|
|
|
|
2008-04-25 19:13:27 +04:00
|
|
|
/*
|
|
|
|
* Test directory function
|
|
|
|
*/
|
|
|
|
|
2012-10-19 20:14:28 +04:00
|
|
|
static void check_csync_vio_opendir(void **state)
|
2008-04-25 19:13:27 +04:00
|
|
|
{
|
2017-08-14 17:19:52 +03:00
|
|
|
CSYNC *csync = (CSYNC*)*state;
|
2014-02-28 19:43:15 +04:00
|
|
|
csync_vio_handle_t *dh;
|
2012-10-19 20:14:28 +04:00
|
|
|
int rc;
|
2008-04-25 19:13:27 +04:00
|
|
|
|
2012-10-19 20:14:28 +04:00
|
|
|
dh = csync_vio_opendir(csync, CSYNC_TEST_DIR);
|
|
|
|
assert_non_null(dh);
|
2008-04-25 19:13:27 +04:00
|
|
|
|
2012-10-19 20:14:28 +04:00
|
|
|
rc = csync_vio_closedir(csync, dh);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-04-25 19:13:27 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 20:14:28 +04:00
|
|
|
static void check_csync_vio_opendir_perm(void **state)
|
2008-04-25 19:13:27 +04:00
|
|
|
{
|
2017-08-14 17:19:52 +03:00
|
|
|
CSYNC *csync = (CSYNC*)*state;
|
2014-02-28 19:43:15 +04:00
|
|
|
csync_vio_handle_t *dh;
|
2012-10-19 20:14:28 +04:00
|
|
|
int rc;
|
2015-07-03 12:45:58 +03:00
|
|
|
mbchar_t *dir = c_utf8_path_to_locale(CSYNC_TEST_DIR);
|
2013-01-28 23:35:35 +04:00
|
|
|
|
|
|
|
assert_non_null(dir);
|
2008-04-25 19:13:27 +04:00
|
|
|
|
2013-10-02 14:33:10 +04:00
|
|
|
rc = _tmkdir(dir, (S_IWUSR|S_IXUSR));
|
2012-10-19 20:14:28 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2008-04-25 19:13:27 +04:00
|
|
|
|
2012-10-19 20:14:28 +04:00
|
|
|
dh = csync_vio_opendir(csync, CSYNC_TEST_DIR);
|
|
|
|
assert_null(dh);
|
|
|
|
assert_int_equal(errno, EACCES);
|
2013-10-02 10:51:11 +04:00
|
|
|
|
2013-10-02 14:33:10 +04:00
|
|
|
_tchmod(dir, MKDIR_MASK);
|
|
|
|
c_free_locale_string(dir);
|
2008-04-25 19:13:27 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 20:14:28 +04:00
|
|
|
static void check_csync_vio_closedir_null(void **state)
|
2008-04-25 19:13:27 +04:00
|
|
|
{
|
2017-08-14 17:19:52 +03:00
|
|
|
CSYNC *csync = (CSYNC*)*state;
|
2012-10-19 20:14:28 +04:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = csync_vio_closedir(csync, NULL);
|
|
|
|
assert_int_equal(rc, -1);
|
2008-04-25 19:13:27 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 20:14:28 +04:00
|
|
|
static void check_csync_vio_readdir(void **state)
|
2008-04-25 19:13:27 +04:00
|
|
|
{
|
2017-08-14 17:19:52 +03:00
|
|
|
CSYNC *csync = (CSYNC*)*state;
|
2014-02-28 19:43:15 +04:00
|
|
|
csync_vio_handle_t *dh;
|
2017-09-05 17:12:32 +03:00
|
|
|
std::unique_ptr<csync_file_stat_t> dirent;
|
2012-10-19 20:14:28 +04:00
|
|
|
int rc;
|
2008-04-25 19:13:27 +04:00
|
|
|
|
2012-10-19 20:14:28 +04:00
|
|
|
dh = csync_vio_opendir(csync, CSYNC_TEST_DIR);
|
|
|
|
assert_non_null(dh);
|
2008-04-25 19:13:27 +04:00
|
|
|
|
2012-10-19 20:14:28 +04:00
|
|
|
dirent = csync_vio_readdir(csync, dh);
|
2017-09-05 17:12:32 +03:00
|
|
|
assert_non_null(dirent.get());
|
2008-04-25 19:13:27 +04:00
|
|
|
|
2012-10-19 20:14:28 +04:00
|
|
|
rc = csync_vio_closedir(csync, dh);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-04-25 19:13:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-19 20:14:28 +04:00
|
|
|
int torture_run_tests(void)
|
|
|
|
{
|
2017-08-21 21:05:00 +03:00
|
|
|
const struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test_setup_teardown(check_csync_vio_opendir, setup_dir, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(check_csync_vio_opendir_perm, setup, teardown),
|
|
|
|
cmocka_unit_test(check_csync_vio_closedir_null),
|
|
|
|
cmocka_unit_test_setup_teardown(check_csync_vio_readdir, setup_dir, teardown),
|
2012-10-19 20:14:28 +04:00
|
|
|
};
|
|
|
|
|
2017-08-21 21:05:00 +03:00
|
|
|
return cmocka_run_group_tests(tests, NULL, NULL);
|
2008-04-21 11:45:55 +04:00
|
|
|
}
|