bitcoin: add first unit test.

Should grow from here.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-04-11 16:42:53 +09:30
parent 66084271c3
commit ed70b13041
3 changed files with 81 additions and 1 deletions

View File

@ -190,7 +190,9 @@ test-onion3: test/test_onion test/onion_key
test-onion4: test/test_onion test/onion_key
set -e; TMPF=/tmp/onion.$$$$; python test/test_onion.py generate $$(test/onion_key --pub `seq 20`) > $$TMPF; for k in `seq 20`; do python test/test_onion.py decode $$(test/onion_key --priv $$k) $$(test/onion_key --pub $$k) < $$TMPF > $$TMPF.unwrap; mv $$TMPF.unwrap $$TMPF; done; rm -f $$TMPF
check: daemon-tests test-onion
check: daemon-tests test-onion bitcoin-tests
include bitcoin/Makefile
# Keep includes in alpha order.
check-src-include-order/%: %

18
bitcoin/Makefile Normal file
View File

@ -0,0 +1,18 @@
# Included for one dir up.
# Note that these actually #include everything they need, except ccan/.
# That allows for unit testing of statics, and special effects.
BITCOIN_TEST_SRC := $(wildcard bitcoin/test/run-*.c)
BITCOIN_TEST_OBJS := $(BITCOIN_TEST_SRC:.c=.o)
BITCOIN_TEST_PROGRAMS := $(BITCOIN_TEST_OBJS:.o=)
$(BITCOIN_TEST_PROGRAMS): $(CCAN_OBJS)
$(BITCOIN_TEST_OBJS): $(CCAN_HEADERS) $(BITCOIN_HEADERS) $(BITCOIN_SRC)
VALGRIND=valgrind -q --error-exitcode=99
VALGRIND_TEST_ARGS = --track-origins=yes --leak-check=full --show-reachable=yes
bitcoin-tests: $(BITCOIN_TEST_PROGRAMS)
set -e; for f in $(BITCOIN_TEST_PROGRAMS); do $(VALGRIND) $(VALGRIND_TEST_ARGS) $$f; done

View File

@ -0,0 +1,60 @@
#include "bitcoin/tx.c"
#include "bitcoin/shadouble.c"
#include <assert.h>
#include <ccan/str/hex/hex.h>
const char extended_tx[] = "02000000000101b5bef485c41d0d1f58d1e8a561924ece5c476d86cff063ea10c8df06136eb31d00000000171600144aa38e396e1394fb45cbf83f48d1464fbc9f498fffffffff0140330f000000000017a9140580ba016669d3efaf09a0b2ec3954469ea2bf038702483045022100f2abf9e9cf238c66533af93f23937eae8ac01fb6f105a00ab71dbefb9637dc9502205c1ac745829b3f6889607961f5d817dfa0c8f52bdda12e837c4f7b162f6db8a701210204096eb817f7efb414ef4d3d8be39dd04374256d3b054a322d4a6ee22736d03b00000000";
static void hexeq(const void *p, size_t len, const char *hex)
{
char *tmphex = tal_arr(NULL, char, hex_str_size(len));
hex_encode(p, len, tmphex, tal_count(tmphex));
if (strcmp(hex, tmphex)) {
fprintf(stderr, "Expected '%s' got '%s'", hex, tmphex);
abort();
}
tal_free(tmphex);
}
static void tal_hexeq(const u8 *p, const char *hex)
{
hexeq(p, tal_count(p),hex);
}
int main(void)
{
struct bitcoin_tx *tx;
tx = bitcoin_tx_from_hex(NULL, extended_tx, strlen(extended_tx));
assert(tx);
/* Canonical results from Nichola Dorier's
* http://n.bitcoin.ninja/checktx
* With much thanks!
*/
assert(tx->input_count == 1);
assert(tx->output_count == 1);
reverse_bytes(tx->input[0].txid.sha.u.u8, sizeof(tx->input[0].txid));
hexeq(&tx->input[0].txid, sizeof(tx->input[0].txid),
"1db36e1306dfc810ea63f0cf866d475cce4e9261a5e8d1581f0d1dc485f4beb5");
assert(tx->input[0].index == 0);
/* This is a p2sh-p2wpkh: */
/* ScriptSig is push of "version 0 + hash of pubkey" */
hexeq(tx->input[0].script, tx->input[0].script_length,
"16" "00" "144aa38e396e1394fb45cbf83f48d1464fbc9f498f");
/* Witness with 2 items */
assert(tx->input[0].witness);
assert(tal_count(tx->input[0].witness) == 2);
tal_hexeq(tx->input[0].witness[0],
"3045022100f2abf9e9cf238c66533af93f23937eae8ac01fb6f105a00ab71dbefb9637dc9502205c1ac745829b3f6889607961f5d817dfa0c8f52bdda12e837c4f7b162f6db8a701");
tal_hexeq(tx->input[0].witness[1],
"0204096eb817f7efb414ef4d3d8be39dd04374256d3b054a322d4a6ee22736d03b");
tal_free(tx);
return 0;
}