From 62ffb42908df4785b3274c7a838c61cdf00d20b6 Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Mon, 21 Aug 2023 15:50:28 -0500 Subject: [PATCH] fuzz: target for open_channel Fuzz the decoding and encoding of open_channel. --- tests/fuzz/fuzz-wire-open_channel.c | 84 +++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 tests/fuzz/fuzz-wire-open_channel.c diff --git a/tests/fuzz/fuzz-wire-open_channel.c b/tests/fuzz/fuzz-wire-open_channel.c new file mode 100644 index 000000000..91deed56b --- /dev/null +++ b/tests/fuzz/fuzz-wire-open_channel.c @@ -0,0 +1,84 @@ +#include "config.h" +#include +#include +#include +#include +#include +#include + +struct open_channel { + struct bitcoin_blkid chain_hash; + struct channel_id temporary_channel_id; + struct amount_sat funding_satoshis; + struct amount_msat push_msat; + struct amount_sat dust_limit_satoshis; + struct amount_msat max_htlc_value_in_flight_msat; + struct amount_sat channel_reserve_satoshis; + struct amount_msat htlc_minimum_msat; + u32 feerate_per_kw; + u16 to_self_delay; + u16 max_accepted_htlcs; + struct pubkey funding_pubkey; + struct pubkey revocation_basepoint; + struct pubkey payment_basepoint; + struct pubkey delayed_payment_basepoint; + struct pubkey htlc_basepoint; + struct pubkey first_per_commitment_point; + u8 channel_flags; + struct tlv_open_channel_tlvs *tlvs; +}; + +static void *encode(const tal_t *ctx, const struct open_channel *s) +{ + return towire_open_channel( + ctx, &s->chain_hash, &s->temporary_channel_id, s->funding_satoshis, + s->push_msat, s->dust_limit_satoshis, + s->max_htlc_value_in_flight_msat, s->channel_reserve_satoshis, + s->htlc_minimum_msat, s->feerate_per_kw, s->to_self_delay, + s->max_accepted_htlcs, &s->funding_pubkey, &s->revocation_basepoint, + &s->payment_basepoint, &s->delayed_payment_basepoint, + &s->htlc_basepoint, &s->first_per_commitment_point, + s->channel_flags, s->tlvs); +} + +static struct open_channel *decode(const tal_t *ctx, const void *p) +{ + struct open_channel *s = tal(ctx, struct open_channel); + + if (fromwire_open_channel( + s, p, &s->chain_hash, &s->temporary_channel_id, + &s->funding_satoshis, &s->push_msat, &s->dust_limit_satoshis, + &s->max_htlc_value_in_flight_msat, &s->channel_reserve_satoshis, + &s->htlc_minimum_msat, &s->feerate_per_kw, &s->to_self_delay, + &s->max_accepted_htlcs, &s->funding_pubkey, + &s->revocation_basepoint, &s->payment_basepoint, + &s->delayed_payment_basepoint, &s->htlc_basepoint, + &s->first_per_commitment_point, &s->channel_flags, &s->tlvs)) + return s; + return tal_free(s); +} + +static bool equal(const struct open_channel *x, const struct open_channel *y) +{ + size_t upto_channel_flags = (uintptr_t)&x->channel_flags - (uintptr_t)x; + if (memcmp(x, y, upto_channel_flags) != 0) + return false; + if (x->channel_flags != y->channel_flags) + return false; + + assert(x->tlvs && y->tlvs); + + if (!memeq(x->tlvs->upfront_shutdown_script, + tal_bytelen(x->tlvs->upfront_shutdown_script), + y->tlvs->upfront_shutdown_script, + tal_bytelen(y->tlvs->upfront_shutdown_script))) + return false; + + return memeq(x->tlvs->channel_type, tal_bytelen(x->tlvs->channel_type), + y->tlvs->channel_type, tal_bytelen(y->tlvs->channel_type)); +} + +void run(const u8 *data, size_t size) +{ + test_decode_encode(data, size, WIRE_OPEN_CHANNEL, struct open_channel); +}