2008-05-09 12:00:10 +04:00
|
|
|
#define _GNU_SOURCE /* asprintf */
|
|
|
|
#include <string.h>
|
2008-05-20 18:29:45 +04:00
|
|
|
#include <unistd.h>
|
2008-05-09 12:00:10 +04:00
|
|
|
|
|
|
|
#include "support.h"
|
|
|
|
|
|
|
|
#define CSYNC_TEST 1
|
2008-07-09 11:57:19 +04:00
|
|
|
#include "csync_statedb.c"
|
2008-05-09 12:00:10 +04:00
|
|
|
|
|
|
|
CSYNC *csync;
|
|
|
|
const char *testdb = (char *) "/tmp/check_csync1/test.db";
|
|
|
|
const char *testtmpdb = (char *) "/tmp/check_csync1/test.db.ctmp";
|
|
|
|
|
|
|
|
static void setup(void) {
|
|
|
|
fail_if(system("rm -rf /tmp/check_csync1") < 0, "Setup failed");
|
|
|
|
fail_if(system("mkdir -p /tmp/check_csync1") < 0, "Setup failed");
|
|
|
|
fail_if(csync_create(&csync, "/tmp/check_csync1", "/tmp/check_csync2") < 0, "Setup failed");
|
|
|
|
SAFE_FREE(csync->options.config_dir);
|
|
|
|
csync->options.config_dir = c_strdup("/tmp/check_csync1/");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void teardown(void) {
|
|
|
|
fail_if(csync_destroy(csync) < 0, "Teardown failed");
|
|
|
|
fail_if(system("rm -rf /tmp/check_csync1") < 0, "Teardown failed");
|
|
|
|
}
|
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
START_TEST (check_csync_statedb_check)
|
2008-05-09 12:00:10 +04:00
|
|
|
{
|
|
|
|
fail_if(system("mkdir -p /tmp/check_csync1") < 0, NULL);
|
|
|
|
|
|
|
|
/* old db */
|
|
|
|
fail_if(system("echo \"SQLite format 2\" > /tmp/check_csync1/test.db") < 0, NULL);
|
2008-07-09 12:10:00 +04:00
|
|
|
fail_unless(_csync_statedb_check(testdb) == 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
|
|
|
/* db already exists */
|
2008-07-09 12:10:00 +04:00
|
|
|
fail_unless(_csync_statedb_check(testdb) == 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
|
|
|
/* no db exists */
|
|
|
|
fail_if(system("rm -f /tmp/check_csync1/test.db") < 0, NULL);
|
2008-07-09 12:10:00 +04:00
|
|
|
fail_unless(_csync_statedb_check(testdb) == 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
fail_unless(_csync_statedb_check((char *) "/tmp/check_csync1/") < 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
|
|
|
fail_if(system("rm -rf /tmp/check_csync1") < 0, NULL);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
START_TEST (check_csync_statedb_load)
|
2008-05-09 12:00:10 +04:00
|
|
|
{
|
|
|
|
struct stat sb;
|
2008-07-09 12:10:00 +04:00
|
|
|
fail_unless(csync_statedb_load(csync, testdb) == 0, NULL);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
|
|
|
fail_unless(lstat(testtmpdb, &sb) == 0, NULL);
|
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
sqlite3_close(csync->statedb.db);
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
START_TEST (check_csync_statedb_close)
|
2008-05-09 12:00:10 +04:00
|
|
|
{
|
|
|
|
struct stat sb;
|
2008-05-20 18:29:45 +04:00
|
|
|
time_t modtime;
|
2008-07-09 12:10:00 +04:00
|
|
|
/* statedb not written */
|
|
|
|
csync_statedb_load(csync, testdb);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2008-05-20 18:29:45 +04:00
|
|
|
fail_unless(lstat(testdb, &sb) == 0, NULL);
|
|
|
|
modtime = sb.st_mtime;
|
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
fail_unless(csync_statedb_close(csync, testdb, 0) == 0, NULL);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2008-05-20 18:29:45 +04:00
|
|
|
fail_unless(lstat(testdb, &sb) == 0, NULL);
|
|
|
|
fail_unless(modtime == sb.st_mtime, NULL);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
csync_statedb_load(csync, testdb);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2008-05-20 18:29:45 +04:00
|
|
|
fail_unless(lstat(testdb, &sb) == 0, NULL);
|
|
|
|
modtime = sb.st_mtime;
|
|
|
|
|
|
|
|
/* wait a sec or the modtime will be the same */
|
|
|
|
sleep(1);
|
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
/* statedb written */
|
|
|
|
fail_unless(csync_statedb_close(csync, testdb, 1) == 0, NULL);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2008-05-20 18:29:45 +04:00
|
|
|
fail_unless(lstat(testdb, &sb) == 0, NULL);
|
|
|
|
fail_unless(modtime < sb.st_mtime, NULL);
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
static Suite *make_csync_suite(void) {
|
2008-07-09 12:10:00 +04:00
|
|
|
Suite *s = suite_create("csync_statedb");
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
create_case(s, "check_csync_statedb_check", check_csync_statedb_check);
|
|
|
|
create_case_fixture(s, "check_csync_statedb_load", check_csync_statedb_load, setup, teardown);
|
|
|
|
create_case_fixture(s, "check_csync_statedb_close", check_csync_statedb_close, setup, teardown);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
Suite *s = NULL;
|
|
|
|
SRunner *sr = NULL;
|
|
|
|
struct argument_s arguments;
|
|
|
|
int nf;
|
|
|
|
|
|
|
|
ZERO_STRUCT(arguments);
|
|
|
|
|
|
|
|
cmdline_parse(argc, argv, &arguments);
|
|
|
|
|
|
|
|
s = make_csync_suite();
|
|
|
|
|
|
|
|
sr = srunner_create(s);
|
|
|
|
if (arguments.nofork) {
|
|
|
|
srunner_set_fork_status(sr, CK_NOFORK);
|
|
|
|
}
|
|
|
|
srunner_run_all(sr, CK_VERBOSE);
|
|
|
|
nf = srunner_ntests_failed(sr);
|
|
|
|
srunner_free(sr);
|
|
|
|
|
|
|
|
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|