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
|
|
|
|
*/
|
2012-10-19 17:53:22 +04:00
|
|
|
#include "torture.h"
|
2008-05-09 12:00:10 +04:00
|
|
|
|
|
|
|
#define CSYNC_TEST 1
|
2017-08-14 17:19:52 +03:00
|
|
|
#include "csync_statedb.cpp"
|
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
|
|
|
|
|
|
|
|
2016-09-02 16:49:54 +03:00
|
|
|
static int setup(void **state)
|
2012-10-19 17:53:22 +04:00
|
|
|
{
|
|
|
|
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("mkdir -p /tmp/check_csync1");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = system("mkdir -p /tmp/check_csync");
|
|
|
|
assert_int_equal(rc, 0);
|
2017-09-04 20:06:13 +03:00
|
|
|
csync = new CSYNC("/tmp/check_csync1", TESTDB);
|
2012-10-19 17:53:22 +04:00
|
|
|
|
2014-10-22 13:50:11 +04:00
|
|
|
sqlite3 *db = NULL;
|
|
|
|
rc = sqlite3_open_v2(TESTDB, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
|
|
|
|
assert_int_equal(rc, SQLITE_OK);
|
2014-10-22 14:44:03 +04:00
|
|
|
rc = sqlite3_close(db);
|
2014-10-22 13:50:11 +04:00
|
|
|
assert_int_equal(rc, SQLITE_OK);
|
|
|
|
|
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;
|
2016-09-02 16:49:54 +03:00
|
|
|
|
|
|
|
return 0;
|
2008-05-09 13:25:21 +04:00
|
|
|
}
|
|
|
|
|
2016-09-02 16:49:54 +03:00
|
|
|
static int setup_db(void **state)
|
2012-10-19 17:53:22 +04:00
|
|
|
{
|
2014-10-09 19:45:10 +04:00
|
|
|
char *errmsg;
|
2013-10-23 19:51:56 +04:00
|
|
|
int rc = 0;
|
2014-10-09 19:45:10 +04:00
|
|
|
sqlite3 *db = NULL;
|
2012-10-19 17:53:22 +04:00
|
|
|
|
2014-10-09 19:45:10 +04:00
|
|
|
const char *sql = "CREATE TABLE IF NOT EXISTS metadata ("
|
2013-05-22 18:43:16 +04:00
|
|
|
"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)"
|
2014-10-09 19:45:10 +04:00
|
|
|
");";
|
2013-05-22 18:43:16 +04:00
|
|
|
|
2014-10-09 19:45:10 +04:00
|
|
|
const char *sql2 = "INSERT INTO metadata"
|
|
|
|
"(phash, pathlen, path, inode, uid, gid, mode, modtime, type, md5) VALUES"
|
|
|
|
"(42, 42, 'Its funny stuff', 23, 42, 43, 55, 66, 2, 54);";
|
2013-05-22 18:43:16 +04:00
|
|
|
|
|
|
|
|
2014-10-09 19:45:10 +04:00
|
|
|
setup(state);
|
|
|
|
rc = sqlite3_open( TESTDB, &db);
|
|
|
|
assert_int_equal(rc, SQLITE_OK);
|
|
|
|
|
|
|
|
rc = sqlite3_exec( db, sql, NULL, NULL, &errmsg );
|
|
|
|
assert_int_equal(rc, SQLITE_OK);
|
|
|
|
|
|
|
|
rc = sqlite3_exec( db, sql2, NULL, NULL, &errmsg );
|
|
|
|
assert_int_equal(rc, SQLITE_OK);
|
|
|
|
|
|
|
|
sqlite3_close(db);
|
2016-09-02 16:49:54 +03:00
|
|
|
|
|
|
|
return 0;
|
2014-10-09 19:45:10 +04:00
|
|
|
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|
2016-09-02 16:49:54 +03:00
|
|
|
static int teardown(void **state) {
|
2017-08-14 17:19:52 +03:00
|
|
|
CSYNC *csync = (CSYNC*)*state;
|
2013-10-23 19:51:56 +04:00
|
|
|
int rc = 0;
|
2012-10-19 17:53:22 +04:00
|
|
|
|
2017-09-04 20:06:13 +03:00
|
|
|
delete csync;
|
2012-10-19 17:53:22 +04:00
|
|
|
rc = system("rm -rf /tmp/check_csync");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
rc = system("rm -rf /tmp/check_csync1");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
*state = NULL;
|
2016-09-02 16:49:54 +03:00
|
|
|
|
|
|
|
return 0;
|
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
|
|
|
{
|
2017-08-14 17:19:52 +03:00
|
|
|
CSYNC *csync = (CSYNC*)*state;
|
2012-10-19 17:53:22 +04:00
|
|
|
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_drop_tables(void **state)
|
2008-05-09 12:00:10 +04:00
|
|
|
{
|
2017-08-14 17:19:52 +03:00
|
|
|
// CSYNC *csync = (CSYNC*)*state;
|
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_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
|
|
|
{
|
2017-08-14 17:19:52 +03:00
|
|
|
CSYNC *csync = (CSYNC*)*state;
|
2012-10-19 17:53:22 +04:00
|
|
|
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++) {
|
2017-08-17 11:06:14 +03:00
|
|
|
st = new csync_file_stat_t;
|
|
|
|
st->path = QString("file_%1").arg(i).toUtf8();
|
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
|
|
|
{
|
2017-08-14 17:19:52 +03:00
|
|
|
CSYNC *csync = (CSYNC*)*state;
|
2012-10-19 17:53:22 +04:00
|
|
|
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++) {
|
2017-08-17 11:06:14 +03:00
|
|
|
st = new csync_file_stat_t;
|
|
|
|
st->path = QString("file_%1").arg(i).toUtf8();
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-08-14 17:19:52 +03:00
|
|
|
CSYNC *csync = (CSYNC*)*state;
|
2017-08-17 11:06:14 +03:00
|
|
|
std::unique_ptr<csync_file_stat_t> tmp;
|
2008-05-09 13:25:21 +04:00
|
|
|
|
2014-03-20 15:34:54 +04:00
|
|
|
tmp = csync_statedb_get_stat_by_hash(csync, (uint64_t) 666);
|
2017-08-17 11:06:14 +03:00
|
|
|
assert_null(tmp.get());
|
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
|
|
|
{
|
2017-08-14 17:19:52 +03:00
|
|
|
CSYNC *csync = (CSYNC*)*state;
|
2017-08-17 11:06:14 +03:00
|
|
|
std::unique_ptr<csync_file_stat_t> tmp;
|
2008-05-09 13:25:21 +04:00
|
|
|
|
2014-03-20 15:34:54 +04:00
|
|
|
tmp = csync_statedb_get_stat_by_inode(csync, (ino_t) 666);
|
2017-08-17 11:06:14 +03:00
|
|
|
assert_null(tmp.get());
|
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)
|
|
|
|
{
|
2016-09-02 16:49:54 +03:00
|
|
|
const struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test_setup_teardown(check_csync_statedb_query_statement, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(check_csync_statedb_drop_tables, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(check_csync_statedb_insert_metadata, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(check_csync_statedb_write, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(check_csync_statedb_get_stat_by_hash_not_found, setup_db, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(check_csync_statedb_get_stat_by_inode_not_found, setup_db, teardown),
|
2012-10-19 17:53:22 +04:00
|
|
|
};
|
|
|
|
|
2016-09-02 16:49:54 +03:00
|
|
|
return cmocka_run_group_tests(tests, NULL, NULL);
|
2008-05-09 12:00:10 +04:00
|
|
|
}
|
|
|
|
|