From 8b7efd01d77e3890f33d31144c37b580b53ef231 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 18 Jul 2017 19:03:05 +0200 Subject: [PATCH] wallet: Created a simple persisted shachain unit-test This exercises the create, read and update functionality of the persisted shachain. --- wallet/Makefile | 2 +- wallet/wallet_tests.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/wallet/Makefile b/wallet/Makefile index b0b01e7e0..857bf540f 100644 --- a/wallet/Makefile +++ b/wallet/Makefile @@ -22,7 +22,7 @@ WALLET_TEST_PROGRAMS := $(WALLET_TEST_OBJS:.o=) $(WALLET_TEST_OBJS): $(WALLET_LIB_OBJS) -$(WALLET_TEST_PROGRAMS): $(BITCOIN_OBJS) $(CCAN_OBJS) $(LIBBASE58_OBJS) daemon/log.o type_to_string.o daemon/pseudorand.o utils.o libwallycore.a libsecp256k1.a libsodium.a +$(WALLET_TEST_PROGRAMS): $(BITCOIN_OBJS) $(CCAN_OBJS) $(LIBBASE58_OBJS) daemon/log.o type_to_string.o daemon/pseudorand.o ccan-crypto-shachain-48.o utils.o libwallycore.a libsecp256k1.a libsodium.a $(WALLET_TEST_OBJS): $(CCAN_HEADERS) wallet/tests: $(WALLET_TEST_PROGRAMS:%=unittest/%) diff --git a/wallet/wallet_tests.c b/wallet/wallet_tests.c index dccb160f8..a8a104c5e 100644 --- a/wallet/wallet_tests.c +++ b/wallet/wallet_tests.c @@ -57,11 +57,51 @@ static bool test_wallet_outputs(void) return true; } +static bool test_shachain_crud(void) +{ + struct wallet_shachain a, b; + char filename[] = "/tmp/ldb-XXXXXX"; + int fd = mkstemp(filename); + struct wallet *w = tal(NULL, struct wallet); + struct sha256 seed, hash; + shachain_index_t index = UINT64_MAX >> (64 - SHACHAIN_BITS); + + w->db = db_open(w, filename); + CHECK_MSG(w->db, "Failed opening the db"); + CHECK_MSG(db_migrate(w->db), "DB migration failed"); + + CHECK_MSG(fd != -1, "Unable to generate temp filename"); + close(fd); + memset(&seed, 'A', sizeof(seed)); + + memset(&a, 0, sizeof(a)); + memset(&b, 0, sizeof(b)); + + w->db = db_open(w, filename); + CHECK(wallet_shachain_init(w, &a)); + + CHECK(a.id == 1); + + CHECK(a.chain.num_valid == 0 && a.chain.min_index == 0); + + for (int i=0; i<100; i++) { + shachain_from_seed(&seed, index, &hash); + CHECK(wallet_shachain_add_hash(w, &a, index, &hash)); + index--; + } + + CHECK(wallet_shachain_load(w, a.id, &b)); + CHECK_MSG(memcmp(&a, &b, sizeof(a)) == 0, "Loading from database doesn't match"); + tal_free(w); + return true; +} + int main(void) { bool ok = true; ok &= test_wallet_outputs(); + ok &= test_shachain_crud(); return !ok; }