2008-05-09 12:00:10 +04:00
|
|
|
#include <string.h>
|
2008-05-20 18:29:45 +04:00
|
|
|
#include <unistd.h>
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
#include "torture.h"
|
2008-05-09 12:00:10 +04:00
|
|
|
|
|
|
|
#define CSYNC_TEST 1
|
2008-07-09 11:57:19 +04:00
|
|
|
#include "csync_statedb.c"
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
#define TESTDB "/tmp/check_csync1/test.db"
|
|
|
|
#define TESTDBTMP "/tmp/check_csync1/test.db.ctmp"
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
static void setup(void **state) {
|
|
|
|
CSYNC *csync;
|
|
|
|
int rc;
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
rc = system("rm -rf /tmp/check_csync1");
|
|
|
|
assert_int_equal(rc, 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
rc = system("mkdir -p /tmp/check_csync1");
|
|
|
|
assert_int_equal(rc, 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
rc = csync_create(&csync, "/tmp/check_csync1", "/tmp/check_csync2");
|
|
|
|
assert_int_equal(rc, 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-27 21:27:14 +04:00
|
|
|
csync_set_config_dir(csync, "/tmp/check_csync1/");
|
2012-10-19 16:43:25 +04:00
|
|
|
|
|
|
|
*state = csync;
|
|
|
|
}
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
static void teardown(void **state) {
|
|
|
|
CSYNC *csync = *state;
|
|
|
|
int rc;
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
rc = csync_destroy(csync);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
rc = system("rm -rf /tmp/check_csync1");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
*state = NULL;
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
static void check_csync_statedb_check(void **state)
|
2008-05-09 12:00:10 +04:00
|
|
|
{
|
2012-10-19 16:43:25 +04:00
|
|
|
int rc;
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
(void) state; /* unused */
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
rc = system("mkdir -p /tmp/check_csync1");
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
/* old db */
|
|
|
|
rc = system("echo \"SQLite format 2\" > /tmp/check_csync1/test.db");
|
|
|
|
assert_int_equal(rc, 0);
|
2013-07-04 12:28:19 +04:00
|
|
|
rc = _csync_statedb_check(TESTDB);
|
2013-05-22 18:51:09 +04:00
|
|
|
assert_int_equal(rc, 1);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
/* db already exists */
|
2013-07-04 12:28:19 +04:00
|
|
|
rc = _csync_statedb_check(TESTDB);
|
2013-05-22 18:51:09 +04:00
|
|
|
assert_int_equal(rc, 1);
|
2008-05-20 18:29:45 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
/* no db exists */
|
|
|
|
rc = system("rm -f /tmp/check_csync1/test.db");
|
|
|
|
assert_int_equal(rc, 0);
|
2013-07-04 12:28:19 +04:00
|
|
|
rc = _csync_statedb_check(TESTDB);
|
2013-05-22 18:51:09 +04:00
|
|
|
assert_int_equal(rc, 1);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2013-07-04 12:28:19 +04:00
|
|
|
rc = _csync_statedb_check("/tmp/check_csync1/");
|
2012-10-19 16:43:25 +04:00
|
|
|
assert_int_equal(rc, -1);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
rc = system("rm -rf /tmp/check_csync1");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
}
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
static void check_csync_statedb_load(void **state)
|
|
|
|
{
|
|
|
|
CSYNC *csync = *state;
|
2013-02-07 14:41:46 +04:00
|
|
|
csync_stat_t sb;
|
2012-10-19 16:43:25 +04:00
|
|
|
int rc;
|
2013-03-12 20:20:21 +04:00
|
|
|
mbchar_t *testdbtmp = c_utf8_to_locale(TESTDBTMP);
|
|
|
|
assert_non_null( testdbtmp );
|
2008-05-20 18:29:45 +04:00
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
rc = csync_statedb_load(csync, TESTDB, &csync->statedb.db);
|
2012-10-19 16:43:25 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2008-05-20 18:29:45 +04:00
|
|
|
|
2013-03-12 20:20:21 +04:00
|
|
|
rc = _tstat(testdbtmp, &sb);
|
2012-10-19 16:43:25 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
sqlite3_close(csync->statedb.db);
|
2013-03-12 20:20:21 +04:00
|
|
|
c_free_locale_string(testdbtmp);
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
static void check_csync_statedb_close(void **state)
|
|
|
|
{
|
|
|
|
CSYNC *csync = *state;
|
2013-02-07 14:41:46 +04:00
|
|
|
csync_stat_t sb;
|
2012-10-19 16:43:25 +04:00
|
|
|
time_t modtime;
|
2013-03-12 20:20:21 +04:00
|
|
|
mbchar_t *testdb = c_utf8_to_locale(TESTDB);
|
2012-10-19 16:43:25 +04:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* statedb not written */
|
2013-06-26 01:14:08 +04:00
|
|
|
csync_statedb_load(csync, TESTDB, &csync->statedb.db);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2013-03-12 20:20:21 +04:00
|
|
|
rc = _tstat(testdb, &sb);
|
2012-10-19 16:43:25 +04:00
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
modtime = sb.st_mtime;
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2013-07-04 12:28:19 +04:00
|
|
|
rc = csync_statedb_close(TESTDB, csync->statedb.db, 0);
|
2012-10-19 16:43:25 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2013-03-12 20:20:21 +04:00
|
|
|
rc = _tstat(testdb, &sb);
|
2012-10-19 16:43:25 +04:00
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_int_equal(modtime, sb.st_mtime);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
csync_statedb_load(csync, TESTDB, &csync->statedb.db);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2013-03-12 20:20:21 +04:00
|
|
|
rc = _tstat(testdb, &sb);
|
2012-10-19 16:43:25 +04:00
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
modtime = sb.st_mtime;
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
/* wait a sec or the modtime will be the same */
|
|
|
|
sleep(1);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
/* statedb written */
|
2013-07-04 12:28:19 +04:00
|
|
|
rc = csync_statedb_close(TESTDB, csync->statedb.db, 1);
|
2012-10-19 16:43:25 +04:00
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
2013-03-12 20:20:21 +04:00
|
|
|
rc = _tstat(testdb, &sb);
|
2012-10-19 16:43:25 +04:00
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_true(modtime < sb.st_mtime);
|
2013-03-12 20:20:21 +04:00
|
|
|
|
|
|
|
c_free_locale_string(testdb);
|
2012-10-19 16:43:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int torture_run_tests(void)
|
|
|
|
{
|
|
|
|
const UnitTest tests[] = {
|
2013-05-22 18:51:09 +04:00
|
|
|
unit_test_setup_teardown(check_csync_statedb_check, setup, teardown),
|
2012-10-19 16:43:25 +04:00
|
|
|
unit_test_setup_teardown(check_csync_statedb_load, setup, teardown),
|
|
|
|
unit_test_setup_teardown(check_csync_statedb_close, setup, teardown),
|
|
|
|
};
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 16:43:25 +04:00
|
|
|
return run_tests(tests);
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|