2012-10-19 17:53:22 +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 17:53:22 +04:00
|
|
|
#define TESTDB "/tmp/check_csync1/test.db"
|
|
|
|
#define TESTDBTMP "/tmp/check_csync1/test.db.ctmp"
|
|
|
|
|
2013-10-23 19:51:56 +04:00
|
|
|
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
static void setup(void **state)
|
|
|
|
{
|
|
|
|
CSYNC *csync;
|
2013-10-23 19:51:56 +04:00
|
|
|
int rc = 0;
|
2012-10-19 17:53:22 +04:00
|
|
|
|
|
|
|
rc = system("rm -rf /tmp/check_csync1");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = system("rm -rf /tmp/check_csync2");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = system("mkdir -p /tmp/check_csync1");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = system("mkdir -p /tmp/check_csync2");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = system("mkdir -p /tmp/check_csync");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = csync_create(&csync, "/tmp/check_csync1", "/tmp/check_csync2");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = csync_set_config_dir(csync, "/tmp/check_csync/");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = csync_init(csync);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
2013-08-18 21:11:36 +04:00
|
|
|
rc = csync_statedb_load(csync, TESTDB, &csync->statedb.db);
|
2013-08-07 14:43:32 +04:00
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
*state = csync;
|
2008-05-09 13:25:21 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
static void setup_db(void **state)
|
|
|
|
{
|
|
|
|
CSYNC *csync;
|
|
|
|
char *stmt = NULL;
|
2013-10-23 19:51:56 +04:00
|
|
|
int rc = 0;
|
2013-05-22 18:43:16 +04:00
|
|
|
c_strlist_t *result = NULL;
|
2008-05-09 13:25:21 +04:00
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
setup(state);
|
|
|
|
csync = *state;
|
|
|
|
|
2013-10-23 19:51:56 +04:00
|
|
|
// rc = csync_statedb_create_tables(csync->statedb.db);
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2008-05-09 13:25:21 +04:00
|
|
|
|
2013-08-18 21:11:36 +04:00
|
|
|
result = csync_statedb_query(csync->statedb.db,
|
2013-05-22 18:43:16 +04:00
|
|
|
"CREATE TABLE IF NOT EXISTS metadata ("
|
|
|
|
"phash INTEGER(8),"
|
|
|
|
"pathlen INTEGER,"
|
|
|
|
"path VARCHAR(4096),"
|
|
|
|
"inode INTEGER,"
|
|
|
|
"uid INTEGER,"
|
|
|
|
"gid INTEGER,"
|
|
|
|
"mode INTEGER,"
|
|
|
|
"modtime INTEGER(8),"
|
|
|
|
"type INTEGER,"
|
|
|
|
"md5 VARCHAR(32),"
|
|
|
|
"PRIMARY KEY(phash)"
|
|
|
|
");"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_non_null(result);
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
|
|
|
|
|
|
|
stmt = sqlite3_mprintf("INSERT INTO metadata"
|
|
|
|
"(phash, pathlen, path, inode, uid, gid, mode, modtime, type, md5) VALUES"
|
|
|
|
"(%lu, %d, '%q', %d, %d, %d, %d, %lu, %d, %lu);",
|
|
|
|
42,
|
|
|
|
42,
|
|
|
|
"It's a rainy day",
|
|
|
|
23,
|
|
|
|
42,
|
|
|
|
42,
|
|
|
|
42,
|
|
|
|
42,
|
|
|
|
2,
|
|
|
|
43);
|
2008-05-09 13:25:21 +04:00
|
|
|
|
2013-10-23 19:51:56 +04:00
|
|
|
// rc = csync_statedb_insert(csync->statedb.db, stmt);
|
2012-10-19 17:53:22 +04:00
|
|
|
sqlite3_free(stmt);
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
static void teardown(void **state) {
|
|
|
|
CSYNC *csync = *state;
|
2013-10-23 19:51:56 +04:00
|
|
|
int rc = 0;
|
2012-10-19 17:53:22 +04:00
|
|
|
|
|
|
|
rc = csync_destroy(csync);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = system("rm -rf /tmp/check_csync");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = system("rm -rf /tmp/check_csync1");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = system("rm -rf /tmp/check_csync2");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
*state = NULL;
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
static void check_csync_statedb_query_statement(void **state)
|
2008-05-09 12:00:10 +04:00
|
|
|
{
|
2012-10-19 17:53:22 +04:00
|
|
|
CSYNC *csync = *state;
|
|
|
|
c_strlist_t *result;
|
|
|
|
|
2013-07-04 12:28:19 +04:00
|
|
|
result = csync_statedb_query(csync->statedb.db, "");
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_null(result);
|
2013-07-10 16:05:58 +04:00
|
|
|
if (result != NULL) {
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
}
|
2012-10-19 17:53:22 +04:00
|
|
|
|
2013-07-04 12:28:19 +04:00
|
|
|
result = csync_statedb_query(csync->statedb.db, "SELECT;");
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_null(result);
|
2013-07-10 16:05:58 +04:00
|
|
|
if (result != NULL) {
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
}
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
static void check_csync_statedb_create_error(void **state)
|
2008-05-09 12:00:10 +04:00
|
|
|
{
|
2012-10-19 17:53:22 +04:00
|
|
|
CSYNC *csync = *state;
|
|
|
|
c_strlist_t *result;
|
|
|
|
|
2013-07-04 12:28:19 +04:00
|
|
|
result = csync_statedb_query(csync->statedb.db, "CREATE TABLE test(phash INTEGER, text VARCHAR(10));");
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_non_null(result);
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
2013-07-04 12:28:19 +04:00
|
|
|
result = csync_statedb_query(csync->statedb.db, "CREATE TABLE test(phash INTEGER, text VARCHAR(10));");
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_null(result);
|
2013-07-04 13:52:08 +04:00
|
|
|
|
|
|
|
c_strlist_destroy(result);
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
static void check_csync_statedb_insert_statement(void **state)
|
2008-05-09 12:00:10 +04:00
|
|
|
{
|
2012-10-19 17:53:22 +04:00
|
|
|
CSYNC *csync = *state;
|
|
|
|
c_strlist_t *result;
|
2013-10-23 19:51:56 +04:00
|
|
|
int rc = 0;
|
2012-10-19 17:53:22 +04:00
|
|
|
|
2013-07-04 12:28:19 +04:00
|
|
|
result = csync_statedb_query(csync->statedb.db, "CREATE TABLE test(phash INTEGER, text VARCHAR(10));");
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_non_null(result);
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
2013-10-23 19:51:56 +04:00
|
|
|
// rc = csync_statedb_insert(csync->statedb.db, "INSERT;");
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2013-10-23 19:51:56 +04:00
|
|
|
// rc = csync_statedb_insert(csync->statedb.db, "INSERT");
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2013-10-23 19:51:56 +04:00
|
|
|
// rc = csync_statedb_insert(csync->statedb.db, "");
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
static void check_csync_statedb_is_empty(void **state)
|
2008-05-09 12:00:10 +04:00
|
|
|
{
|
2012-10-19 17:53:22 +04:00
|
|
|
CSYNC *csync = *state;
|
|
|
|
c_strlist_t *result;
|
2013-10-23 19:51:56 +04:00
|
|
|
int rc = 0;
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
/* we have an empty db */
|
2013-07-04 12:28:19 +04:00
|
|
|
assert_true(_csync_statedb_is_empty(csync->statedb.db));
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
/* add a table and an entry */
|
2013-07-04 12:28:19 +04:00
|
|
|
result = csync_statedb_query(csync->statedb.db, "CREATE TABLE metadata(phash INTEGER, text VARCHAR(10));");
|
2012-10-19 17:53:22 +04:00
|
|
|
c_strlist_destroy(result);
|
2013-10-23 19:51:56 +04:00
|
|
|
// rc = csync_statedb_insert(csync->statedb.db, "INSERT INTO metadata (phash, text) VALUES (42, 'hello');");
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_true(rc > 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2013-07-04 12:28:19 +04:00
|
|
|
assert_false(_csync_statedb_is_empty(csync->statedb.db));
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
static void check_csync_statedb_create_tables(void **state)
|
2008-05-09 12:00:10 +04:00
|
|
|
{
|
2013-10-23 19:51:56 +04:00
|
|
|
// CSYNC *csync = *state;
|
2012-10-19 17:53:22 +04:00
|
|
|
char *stmt = NULL;
|
2013-10-23 19:51:56 +04:00
|
|
|
int rc = 0;
|
|
|
|
(void) state;
|
2012-10-19 17:53:22 +04:00
|
|
|
|
2013-10-23 19:51:56 +04:00
|
|
|
// rc = csync_statedb_create_tables(csync->statedb.db);
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
stmt = sqlite3_mprintf("INSERT INTO metadata_temp"
|
2013-05-22 18:43:16 +04:00
|
|
|
"(phash, pathlen, path, inode, uid, gid, mode, modtime, type, md5) VALUES"
|
|
|
|
"(%lu, %d, '%q', %d, %d, %d, %d, %ld, %d, '%q');",
|
2013-07-31 16:50:44 +04:00
|
|
|
(unsigned long)42,
|
2012-10-19 17:53:22 +04:00
|
|
|
42,
|
|
|
|
"It's a rainy day",
|
|
|
|
42,
|
|
|
|
42,
|
|
|
|
42,
|
|
|
|
42,
|
2013-05-22 18:43:16 +04:00
|
|
|
(long)42,
|
|
|
|
2, "xsyxcmfkdsjaf");
|
2012-10-19 17:53:22 +04:00
|
|
|
|
2013-10-23 19:51:56 +04:00
|
|
|
// rc = csync_statedb_insert(csync->statedb.db, stmt);
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_true(rc > 0);
|
|
|
|
|
|
|
|
sqlite3_free(stmt);
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
static void check_csync_statedb_drop_tables(void **state)
|
2008-05-09 12:00:10 +04:00
|
|
|
{
|
2013-10-23 19:51:56 +04:00
|
|
|
// CSYNC *csync = *state;
|
|
|
|
int rc = 0;
|
|
|
|
(void) state;
|
2012-10-19 17:53:22 +04:00
|
|
|
|
2013-10-23 19:51:56 +04:00
|
|
|
// rc = csync_statedb_drop_tables(csync->statedb.db);
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2013-10-23 19:51:56 +04:00
|
|
|
// rc = csync_statedb_create_tables(csync->statedb.db);
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2013-10-23 19:51:56 +04:00
|
|
|
// rc = csync_statedb_drop_tables(csync->statedb.db);
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
static void check_csync_statedb_insert_metadata(void **state)
|
2008-05-09 12:00:10 +04:00
|
|
|
{
|
2012-10-19 17:53:22 +04:00
|
|
|
CSYNC *csync = *state;
|
|
|
|
csync_file_stat_t *st;
|
2013-10-23 19:51:56 +04:00
|
|
|
int i, rc = 0;
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2013-10-23 19:51:56 +04:00
|
|
|
// rc = csync_statedb_create_tables(csync->statedb.db);
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
for (i = 0; i < 100; i++) {
|
2013-07-10 14:06:00 +04:00
|
|
|
st = c_malloc(sizeof(csync_file_stat_t) + 30 );
|
|
|
|
snprintf(st->path, 29, "file_%d" , i );
|
2012-10-19 17:53:22 +04:00
|
|
|
st->phash = i;
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
rc = c_rbtree_insert(csync->local.tree, (void *) st);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
}
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2013-10-23 19:51:56 +04:00
|
|
|
// rc = csync_statedb_insert_metadata(csync, csync->statedb.db);
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
static void check_csync_statedb_write(void **state)
|
2008-05-09 12:00:10 +04:00
|
|
|
{
|
2012-10-19 17:53:22 +04:00
|
|
|
CSYNC *csync = *state;
|
|
|
|
csync_file_stat_t *st;
|
|
|
|
int i, rc;
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
for (i = 0; i < 100; i++) {
|
2013-07-10 14:06:00 +04:00
|
|
|
st = c_malloc(sizeof(csync_file_stat_t) + 30);
|
|
|
|
snprintf(st->path, 29, "file_%d" , i );
|
2012-10-19 17:53:22 +04:00
|
|
|
st->phash = i;
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
rc = c_rbtree_insert(csync->local.tree, (void *) st);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
}
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2013-10-23 19:51:56 +04:00
|
|
|
// rc = csync_statedb_write(csync, csync->statedb.db);
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_int_equal(rc, 0);
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
static void check_csync_statedb_get_stat_by_hash(void **state)
|
2008-05-09 13:25:21 +04:00
|
|
|
{
|
2012-10-19 17:53:22 +04:00
|
|
|
CSYNC *csync = *state;
|
|
|
|
csync_file_stat_t *tmp;
|
2008-05-09 13:25:21 +04:00
|
|
|
|
2013-07-04 12:28:19 +04:00
|
|
|
tmp = csync_statedb_get_stat_by_hash(csync->statedb.db, (uint64_t) 42);
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_non_null(tmp);
|
2008-05-09 13:25:21 +04:00
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_int_equal(tmp->phash, 42);
|
|
|
|
assert_int_equal(tmp->inode, 23);
|
2008-05-09 13:25:21 +04:00
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
free(tmp);
|
2008-05-09 13:25:21 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
static void check_csync_statedb_get_stat_by_hash_not_found(void **state)
|
2008-05-09 13:25:21 +04:00
|
|
|
{
|
2012-10-19 17:53:22 +04:00
|
|
|
CSYNC *csync = *state;
|
|
|
|
csync_file_stat_t *tmp;
|
2008-05-09 13:25:21 +04:00
|
|
|
|
2013-07-04 12:28:19 +04:00
|
|
|
tmp = csync_statedb_get_stat_by_hash(csync->statedb.db, (uint64_t) 666);
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_null(tmp);
|
2013-07-04 13:57:31 +04:00
|
|
|
|
|
|
|
free(tmp);
|
2008-05-09 13:25:21 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
static void check_csync_statedb_get_stat_by_inode(void **state)
|
2008-05-09 13:25:21 +04:00
|
|
|
{
|
2012-10-19 17:53:22 +04:00
|
|
|
CSYNC *csync = *state;
|
|
|
|
csync_file_stat_t *tmp;
|
2008-05-09 13:25:21 +04:00
|
|
|
|
2013-07-04 12:28:19 +04:00
|
|
|
tmp = csync_statedb_get_stat_by_inode(csync->statedb.db, (ino_t) 23);
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_non_null(tmp);
|
2008-05-09 13:25:21 +04:00
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_int_equal(tmp->phash, 42);
|
|
|
|
assert_int_equal(tmp->inode, 23);
|
2008-05-09 13:25:21 +04:00
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
free(tmp);
|
2008-05-09 13:25:21 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
static void check_csync_statedb_get_stat_by_inode_not_found(void **state)
|
2008-05-09 13:25:21 +04:00
|
|
|
{
|
2012-10-19 17:53:22 +04:00
|
|
|
CSYNC *csync = *state;
|
|
|
|
csync_file_stat_t *tmp;
|
2008-05-09 13:25:21 +04:00
|
|
|
|
2013-07-04 12:28:19 +04:00
|
|
|
tmp = csync_statedb_get_stat_by_inode(csync->statedb.db, (ino_t) 666);
|
2012-10-19 17:53:22 +04:00
|
|
|
assert_null(tmp);
|
2008-05-09 13:25:21 +04:00
|
|
|
}
|
2008-05-09 12:00:10 +04:00
|
|
|
|
2012-10-19 17:53:22 +04:00
|
|
|
int torture_run_tests(void)
|
|
|
|
{
|
|
|
|
const UnitTest tests[] = {
|
|
|
|
unit_test_setup_teardown(check_csync_statedb_query_statement, setup, teardown),
|
|
|
|
unit_test_setup_teardown(check_csync_statedb_create_error, setup, teardown),
|
|
|
|
unit_test_setup_teardown(check_csync_statedb_insert_statement, setup, teardown),
|
2013-10-25 16:43:04 +04:00
|
|
|
/* unit_test_setup_teardown(check_csync_statedb_is_empty, setup, teardown), */
|
|
|
|
/* unit_test_setup_teardown(check_csync_statedb_create_tables, setup, teardown), */
|
2012-10-19 17:53:22 +04:00
|
|
|
unit_test_setup_teardown(check_csync_statedb_drop_tables, setup, teardown),
|
|
|
|
unit_test_setup_teardown(check_csync_statedb_insert_metadata, setup, teardown),
|
|
|
|
unit_test_setup_teardown(check_csync_statedb_write, setup, teardown),
|
2013-10-25 16:43:04 +04:00
|
|
|
/* unit_test_setup_teardown(check_csync_statedb_get_stat_by_hash, setup_db, teardown), */
|
2012-10-19 17:53:22 +04:00
|
|
|
unit_test_setup_teardown(check_csync_statedb_get_stat_by_hash_not_found, setup_db, teardown),
|
2013-10-25 16:43:04 +04:00
|
|
|
/* unit_test_setup_teardown(check_csync_statedb_get_stat_by_inode, setup_db, teardown), */
|
2012-10-19 17:53:22 +04:00
|
|
|
unit_test_setup_teardown(check_csync_statedb_get_stat_by_inode_not_found, setup_db, teardown),
|
|
|
|
};
|
|
|
|
|
|
|
|
return run_tests(tests);
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|