2017-11-01 02:21:56 +01:00
|
|
|
#include <lightningd/log.h>
|
|
|
|
|
2018-08-09 04:27:20 +02:00
|
|
|
static void db_test_fatal(const char *fmt, ...);
|
|
|
|
#define db_fatal db_test_fatal
|
2017-11-01 02:21:56 +01:00
|
|
|
|
2019-06-06 10:26:42 +02:00
|
|
|
static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, bool call_notifier UNUSED, const char *fmt UNUSED, ...)
|
2018-01-02 05:03:42 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
#define log_ db_log_
|
|
|
|
|
2017-12-12 04:42:37 +01:00
|
|
|
#include "wallet/db.c"
|
2017-05-12 20:24:18 +02:00
|
|
|
|
2017-12-12 04:42:37 +01:00
|
|
|
#include "test_utils.h"
|
2017-05-31 15:26:30 +02:00
|
|
|
|
2019-02-20 12:31:48 +01:00
|
|
|
#include <common/amount.h>
|
2017-12-15 11:17:54 +01:00
|
|
|
#include <common/memleak.h>
|
2017-05-12 20:24:18 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-12-12 04:42:38 +01:00
|
|
|
/* AUTOGENERATED MOCKS START */
|
2019-07-30 07:25:12 +02:00
|
|
|
/* Generated stub for bigsize_get */
|
|
|
|
size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED)
|
|
|
|
{ fprintf(stderr, "bigsize_get called!\n"); abort(); }
|
|
|
|
/* Generated stub for bigsize_put */
|
|
|
|
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
|
|
|
|
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
|
2019-07-25 22:45:02 +02:00
|
|
|
/* Generated stub for fatal */
|
|
|
|
void fatal(const char *fmt UNNEEDED, ...)
|
|
|
|
{ fprintf(stderr, "fatal called!\n"); abort(); }
|
2017-12-12 04:42:38 +01:00
|
|
|
/* AUTOGENERATED MOCKS END */
|
|
|
|
|
2017-11-01 02:21:56 +01:00
|
|
|
static char *db_err;
|
2018-08-09 04:27:20 +02:00
|
|
|
static void db_test_fatal(const char *fmt, ...)
|
2017-11-01 02:21:56 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
/* Fail hard if we're complaining about not being in transaction */
|
|
|
|
assert(!strstarts(fmt, "No longer in transaction"));
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
db_err = tal_vfmt(NULL, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2019-03-18 03:40:32 +01:00
|
|
|
void plugin_hook_db_sync(struct db *db UNNEEDED, const char **changes UNNEEDED, const char *final UNNEEDED)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-21 16:36:21 +01:00
|
|
|
static struct db *create_test_db(void)
|
2017-05-12 20:24:18 +02:00
|
|
|
{
|
|
|
|
struct db *db;
|
|
|
|
char filename[] = "/tmp/ldb-XXXXXX";
|
|
|
|
|
|
|
|
int fd = mkstemp(filename);
|
|
|
|
if (fd == -1)
|
|
|
|
return NULL;
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
db = db_open(NULL, filename);
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
2019-03-05 00:39:21 +01:00
|
|
|
static bool test_empty_db_migrate(struct lightningd *ld)
|
2017-05-12 20:24:18 +02:00
|
|
|
{
|
2018-02-21 16:36:21 +01:00
|
|
|
struct db *db = create_test_db();
|
2017-05-31 15:26:30 +02:00
|
|
|
CHECK(db);
|
2017-11-01 02:21:56 +01:00
|
|
|
db_begin_transaction(db);
|
2017-05-31 15:26:30 +02:00
|
|
|
CHECK(db_get_version(db) == -1);
|
2017-11-01 02:21:56 +01:00
|
|
|
db_commit_transaction(db);
|
2019-03-05 00:39:21 +01:00
|
|
|
db_migrate(ld, db, NULL);
|
2017-11-01 02:21:56 +01:00
|
|
|
db_begin_transaction(db);
|
2019-02-24 06:12:09 +01:00
|
|
|
CHECK(db_get_version(db) == ARRAY_SIZE(dbmigrations) - 1);
|
2017-11-01 02:21:56 +01:00
|
|
|
db_commit_transaction(db);
|
2017-05-12 20:24:18 +02:00
|
|
|
|
|
|
|
tal_free(db);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-13 15:43:38 +02:00
|
|
|
static bool test_primitives(void)
|
|
|
|
{
|
2019-08-13 16:00:46 +02:00
|
|
|
struct db_stmt *stmt;
|
2018-02-21 16:36:21 +01:00
|
|
|
struct db *db = create_test_db();
|
2019-08-13 16:00:46 +02:00
|
|
|
db_err = NULL;
|
2017-11-01 02:21:56 +01:00
|
|
|
db_begin_transaction(db);
|
2017-06-13 15:43:38 +02:00
|
|
|
CHECK(db->in_transaction);
|
2017-11-01 02:21:56 +01:00
|
|
|
db_commit_transaction(db);
|
2017-06-13 15:43:38 +02:00
|
|
|
CHECK(!db->in_transaction);
|
2017-11-01 02:21:56 +01:00
|
|
|
db_begin_transaction(db);
|
|
|
|
db_commit_transaction(db);
|
|
|
|
|
|
|
|
db_begin_transaction(db);
|
2019-08-13 16:00:46 +02:00
|
|
|
stmt = db_prepare_v2(db, SQL("SELECT name FROM sqlite_master WHERE type='table';"));
|
|
|
|
CHECK_MSG(db_exec_prepared_v2(stmt), "db_exec_prepared must succeed");
|
2017-11-01 02:21:56 +01:00
|
|
|
CHECK_MSG(!db_err, "Simple correct SQL command");
|
2019-08-13 16:00:46 +02:00
|
|
|
tal_free(stmt);
|
2017-06-13 15:43:38 +02:00
|
|
|
|
2019-08-13 16:00:46 +02:00
|
|
|
stmt = db_prepare_v2(db, SQL("not a valid SQL statement"));
|
|
|
|
CHECK_MSG(!db_exec_prepared_v2(stmt), "db_exec_prepared must fail");
|
2017-11-01 02:21:56 +01:00
|
|
|
CHECK_MSG(db_err, "Failing SQL command");
|
2019-08-13 16:00:46 +02:00
|
|
|
tal_free(stmt);
|
2017-11-01 02:21:56 +01:00
|
|
|
db_err = tal_free(db_err);
|
|
|
|
db_commit_transaction(db);
|
2017-06-13 15:43:38 +02:00
|
|
|
CHECK(!db->in_transaction);
|
|
|
|
tal_free(db);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-05 00:39:21 +01:00
|
|
|
static bool test_vars(struct lightningd *ld)
|
2017-06-01 16:10:40 +02:00
|
|
|
{
|
2018-02-21 16:36:21 +01:00
|
|
|
struct db *db = create_test_db();
|
2017-06-01 16:10:40 +02:00
|
|
|
char *varname = "testvar";
|
|
|
|
CHECK(db);
|
2019-03-05 00:39:21 +01:00
|
|
|
db_migrate(ld, db, NULL);
|
2017-06-01 16:10:40 +02:00
|
|
|
|
2017-11-01 02:21:56 +01:00
|
|
|
db_begin_transaction(db);
|
2017-06-01 16:10:40 +02:00
|
|
|
/* Check default behavior */
|
|
|
|
CHECK(db_get_intvar(db, varname, 42) == 42);
|
|
|
|
|
|
|
|
/* Check setting and getting */
|
2017-11-01 02:21:56 +01:00
|
|
|
db_set_intvar(db, varname, 1);
|
2017-06-01 16:10:40 +02:00
|
|
|
CHECK(db_get_intvar(db, varname, 42) == 1);
|
|
|
|
|
|
|
|
/* Check updating */
|
2017-11-01 02:21:56 +01:00
|
|
|
db_set_intvar(db, varname, 2);
|
2017-06-01 16:10:40 +02:00
|
|
|
CHECK(db_get_intvar(db, varname, 42) == 2);
|
2017-11-01 02:21:56 +01:00
|
|
|
db_commit_transaction(db);
|
2017-06-01 16:10:40 +02:00
|
|
|
|
|
|
|
tal_free(db);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-12 20:24:18 +02:00
|
|
|
int main(void)
|
|
|
|
{
|
2018-04-25 12:55:34 +02:00
|
|
|
setup_locale();
|
|
|
|
|
2017-05-12 20:24:18 +02:00
|
|
|
bool ok = true;
|
2019-03-05 00:39:21 +01:00
|
|
|
/* Dummy for migration hooks */
|
|
|
|
struct lightningd *ld = tal(NULL, struct lightningd);
|
2019-03-13 17:20:54 +01:00
|
|
|
ld->config = test_config;
|
2017-05-12 20:24:18 +02:00
|
|
|
|
2019-03-05 00:39:21 +01:00
|
|
|
ok &= test_empty_db_migrate(ld);
|
|
|
|
ok &= test_vars(ld);
|
2017-06-13 15:43:38 +02:00
|
|
|
ok &= test_primitives();
|
2017-05-12 20:24:18 +02:00
|
|
|
|
2019-03-05 00:39:21 +01:00
|
|
|
tal_free(ld);
|
2017-05-12 20:24:18 +02:00
|
|
|
return !ok;
|
|
|
|
}
|