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
|
|
|
|
2018-02-21 16:06:07 +01:00
|
|
|
static void db_log_(struct log *log UNUSED, enum log_level level 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
|
|
|
|
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 */
|
2018-03-26 02:08:16 +02:00
|
|
|
/* Generated stub for json_escaped_string_ */
|
|
|
|
struct json_escaped *json_escaped_string_(const tal_t *ctx UNNEEDED,
|
|
|
|
const void *bytes UNNEEDED, size_t len UNNEEDED)
|
|
|
|
{ fprintf(stderr, "json_escaped_string_ 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);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool test_empty_db_migrate(void)
|
|
|
|
{
|
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);
|
2018-01-02 05:03:42 +01:00
|
|
|
db_migrate(db, NULL);
|
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) == db_migration_count());
|
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)
|
|
|
|
{
|
2018-02-21 16:36:21 +01:00
|
|
|
struct db *db = create_test_db();
|
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);
|
|
|
|
db_exec(__func__, db, "SELECT name FROM sqlite_master WHERE type='table';");
|
|
|
|
CHECK_MSG(!db_err, "Simple correct SQL command");
|
2017-06-13 15:43:38 +02:00
|
|
|
|
2017-11-01 02:21:56 +01:00
|
|
|
db_exec(__func__, db, "not a valid SQL statement");
|
|
|
|
CHECK_MSG(db_err, "Failing SQL command");
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-06-01 16:10:40 +02:00
|
|
|
static bool test_vars(void)
|
|
|
|
{
|
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);
|
2018-01-02 05:03:42 +01:00
|
|
|
db_migrate(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;
|
|
|
|
|
|
|
|
ok &= test_empty_db_migrate();
|
2017-06-01 16:10:40 +02:00
|
|
|
ok &= test_vars();
|
2017-06-13 15:43:38 +02:00
|
|
|
ok &= test_primitives();
|
2017-05-12 20:24:18 +02:00
|
|
|
|
|
|
|
return !ok;
|
|
|
|
}
|