nextcloud-desktop/csync/tests/csync_tests/check_csync_statedb_load.c

154 lines
3.8 KiB
C
Raw Normal View History

/*
* 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-05-09 12:00:10 +04:00
#include <string.h>
#include <unistd.h>
2008-05-09 12:00:10 +04:00
#include "torture.h"
2008-05-09 12:00:10 +04:00
#define CSYNC_TEST 1
#include "csync_statedb.c"
2008-05-09 12:00:10 +04:00
#define TESTDB "/tmp/check_csync1/test.db"
2008-05-09 12:00:10 +04:00
static void setup(void **state) {
CSYNC *csync;
int rc;
2008-05-09 12:00:10 +04:00
rc = system("rm -rf /tmp/check_csync1");
assert_int_equal(rc, 0);
2008-05-09 12:00:10 +04:00
rc = system("mkdir -p /tmp/check_csync1");
assert_int_equal(rc, 0);
2008-05-09 12:00:10 +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
2014-03-20 15:34:54 +04:00
csync->statedb.file = c_strdup( TESTDB );
*state = csync;
}
2008-05-09 12:00:10 +04:00
static void teardown(void **state) {
CSYNC *csync = *state;
int rc;
2008-05-09 12:00:10 +04:00
rc = csync_destroy(csync);
assert_int_equal(rc, 0);
2008-05-09 12:00:10 +04:00
rc = system("rm -rf /tmp/check_csync1");
assert_int_equal(rc, 0);
*state = NULL;
2008-05-09 12:00:10 +04:00
}
static void check_csync_statedb_check(void **state)
2008-05-09 12:00:10 +04:00
{
int rc;
2008-05-09 12:00:10 +04:00
(void) state; /* unused */
2008-05-09 12:00:10 +04:00
rc = system("mkdir -p /tmp/check_csync1");
2008-05-09 12:00:10 +04:00
/* old db */
rc = system("echo \"SQLite format 2\" > /tmp/check_csync1/test.db");
assert_int_equal(rc, 0);
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
/* db already exists */
rc = _csync_statedb_check(TESTDB);
2013-05-22 18:51:09 +04:00
assert_int_equal(rc, 1);
/* no db exists */
rc = system("rm -f /tmp/check_csync1/test.db");
assert_int_equal(rc, 0);
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
rc = _csync_statedb_check("/tmp/check_csync1/");
assert_int_equal(rc, -1);
2008-05-09 12:00:10 +04:00
rc = system("rm -rf /tmp/check_csync1");
assert_int_equal(rc, 0);
}
2008-05-09 12:00:10 +04:00
static void check_csync_statedb_load(void **state)
{
CSYNC *csync = *state;
int rc;
rc = csync_statedb_load(csync, TESTDB, &csync->statedb.db);
assert_int_equal(rc, 0);
sqlite3_close(csync->statedb.db);
2008-05-09 12:00:10 +04:00
}
static void check_csync_statedb_close(void **state)
{
CSYNC *csync = *state;
csync_stat_t sb;
time_t modtime;
mbchar_t *testdb = c_utf8_to_locale(TESTDB);
int rc;
/* statedb not written */
csync_statedb_load(csync, TESTDB, &csync->statedb.db);
2008-05-09 12:00:10 +04:00
rc = _tstat(testdb, &sb);
assert_int_equal(rc, 0);
modtime = sb.st_mtime;
2008-05-09 12:00:10 +04:00
rc = csync_statedb_close(csync);
assert_int_equal(rc, 0);
2008-05-09 12:00:10 +04:00
rc = _tstat(testdb, &sb);
assert_int_equal(rc, 0);
assert_int_equal(modtime, sb.st_mtime);
2008-05-09 12:00:10 +04:00
csync_statedb_load(csync, TESTDB, &csync->statedb.db);
2008-05-09 12:00:10 +04:00
rc = _tstat(testdb, &sb);
assert_int_equal(rc, 0);
modtime = sb.st_mtime;
2008-05-09 12:00:10 +04:00
/* wait a sec or the modtime will be the same */
sleep(1);
2008-05-09 12:00:10 +04:00
/* statedb written */
rc = csync_statedb_close(csync);
assert_int_equal(rc, 0);
rc = _tstat(testdb, &sb);
assert_int_equal(rc, 0);
c_free_locale_string(testdb);
}
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),
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
return run_tests(tests);
2008-05-09 12:00:10 +04:00
}