mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 21:35:11 +01:00
wallet: Add wallet_tests and simplify db_tests
I'll eventually move the utils into a different location (maybe CCAN?) but for now I'm keeping it close to where it is used.
This commit is contained in:
parent
e91aff210e
commit
5396335363
@ -1,5 +1,7 @@
|
||||
#include "db.c"
|
||||
|
||||
#include "wallet/test_utils.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@ -20,24 +22,13 @@ static struct db *create_test_db(const char *testname)
|
||||
static bool test_empty_db_migrate(void)
|
||||
{
|
||||
struct db *db = create_test_db(__func__);
|
||||
if (!db)
|
||||
goto fail;
|
||||
|
||||
if (db_get_version(db) != -1)
|
||||
goto fail;
|
||||
|
||||
if (!db_migrate(db))
|
||||
goto fail;
|
||||
|
||||
if (db_get_version(db) != db_migration_count())
|
||||
goto fail;
|
||||
CHECK(db);
|
||||
CHECK(db_get_version(db) == -1);
|
||||
CHECK(db_migrate(db));
|
||||
CHECK(db_get_version(db) == db_migration_count());
|
||||
|
||||
tal_free(db);
|
||||
return true;
|
||||
fail:
|
||||
printf("Migration failed with error: %s\n", db->err);
|
||||
tal_free(db);
|
||||
return false;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
|
24
wallet/test_utils.h
Normal file
24
wallet/test_utils.h
Normal file
@ -0,0 +1,24 @@
|
||||
#ifndef LIGHTNING_WALLET_TEST_UTILS_H
|
||||
#define LIGHTNING_WALLET_TEST_UTILS_H
|
||||
|
||||
/* Definitions "inspired" by libsecp256k1 */
|
||||
#define TEST_FAILURE(msg) do { \
|
||||
fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, msg); \
|
||||
return false; \
|
||||
} while(0)
|
||||
|
||||
#ifdef HAVE_BUILTIN_EXPECT
|
||||
#define EXPECT(x,c) __builtin_expect((x),(c))
|
||||
#else
|
||||
#define EXPECT(x,c) (x)
|
||||
#endif
|
||||
|
||||
#define CHECK_MSG(cond,msg) do { \
|
||||
if (EXPECT(!(cond), 0)) { \
|
||||
TEST_FAILURE(msg); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#define CHECK(cond) CHECK_MSG(cond,"test condition failed");
|
||||
|
||||
#endif /* LIGHTNING_WALLET_TEST_UTILS_H */
|
41
wallet/wallet_tests.c
Normal file
41
wallet/wallet_tests.c
Normal file
@ -0,0 +1,41 @@
|
||||
#include "wallet.c"
|
||||
|
||||
#include "db.c"
|
||||
#include "wallet/test_utils.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static bool test_wallet_add_utxo(void)
|
||||
{
|
||||
char filename[] = "/tmp/ldb-XXXXXX";
|
||||
struct utxo u;
|
||||
int fd = mkstemp(filename);
|
||||
struct wallet *w = tal(NULL, struct wallet);
|
||||
CHECK_MSG(fd != -1, "Unable to generate temp filename");
|
||||
close(fd);
|
||||
|
||||
w->db = db_open(w, filename);
|
||||
CHECK_MSG(w->db,"Failed opening the db");
|
||||
CHECK_MSG(db_migrate(w->db), "DB migration failed");
|
||||
|
||||
memset(&u, 0, sizeof(u));
|
||||
|
||||
/* Should work, it's the first time we add it */
|
||||
CHECK_MSG(wallet_add_utxo(w, &u, p2sh_wpkh), "wallet_add_utxo failed on first add");
|
||||
|
||||
/* Should fail, we already have that UTXO */
|
||||
CHECK_MSG(!wallet_add_utxo(w, &u, p2sh_wpkh), "wallet_add_utxo succeeded on second add");
|
||||
tal_free(w);
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
bool ok = true;
|
||||
|
||||
ok &= test_wallet_add_utxo();
|
||||
|
||||
return !ok;
|
||||
}
|
Loading…
Reference in New Issue
Block a user