From 1fff136d66041500e3dbde44a61e5b4209ea1812 Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Mon, 21 Aug 2023 15:59:46 -0500 Subject: [PATCH] fuzz: target for shutdown Fuzz the decoding and encoding of shutdown. --- tests/fuzz/fuzz-wire-shutdown.c | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tests/fuzz/fuzz-wire-shutdown.c diff --git a/tests/fuzz/fuzz-wire-shutdown.c b/tests/fuzz/fuzz-wire-shutdown.c new file mode 100644 index 000000000..115d4ea17 --- /dev/null +++ b/tests/fuzz/fuzz-wire-shutdown.c @@ -0,0 +1,56 @@ +#include "config.h" +#include +#include +#include +#include +#include +#include + +struct shutdown { + struct channel_id channel_id; + u8 *scriptpubkey; + struct tlv_shutdown_tlvs *tlvs; +}; + +static void *encode(const tal_t *ctx, const struct shutdown *s) +{ + return towire_shutdown(ctx, &s->channel_id, s->scriptpubkey, s->tlvs); +} + +static struct shutdown *decode(const tal_t *ctx, const void *p) +{ + struct shutdown *s = tal(ctx, struct shutdown); + + if (fromwire_shutdown(s, p, &s->channel_id, &s->scriptpubkey, &s->tlvs)) + return s; + return tal_free(s); +} + +static bool wrong_funding_equal(const struct tlv_shutdown_tlvs_wrong_funding *x, + const struct tlv_shutdown_tlvs_wrong_funding *y) +{ + if (!x && !y) + return true; + if (!x || !y) + return false; + + return bitcoin_txid_eq(&x->txid, &y->txid) && x->outnum == y->outnum; +} + +static bool equal(const struct shutdown *x, const struct shutdown *y) +{ + if (!channel_id_eq(&x->channel_id, &y->channel_id)) + return false; + if (!memeq(x->scriptpubkey, tal_bytelen(x->scriptpubkey), + y->scriptpubkey, tal_bytelen(y->scriptpubkey))) + return false; + + assert(x->tlvs && y->tlvs); + return wrong_funding_equal(x->tlvs->wrong_funding, + y->tlvs->wrong_funding); +} + +void run(const u8 *data, size_t size) +{ + test_decode_encode(data, size, WIRE_SHUTDOWN, struct shutdown); +}