From b7b046ce83c695275a71d90fab530d724e77a7ff Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Mon, 21 Aug 2023 15:35:13 -0500 Subject: [PATCH] fuzz: target for channel_reestablish Fuzz the decoding and encoding of channel_reestablish. --- tests/fuzz/fuzz-wire-channel_reestablish.c | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 tests/fuzz/fuzz-wire-channel_reestablish.c diff --git a/tests/fuzz/fuzz-wire-channel_reestablish.c b/tests/fuzz/fuzz-wire-channel_reestablish.c new file mode 100644 index 000000000..771ea9876 --- /dev/null +++ b/tests/fuzz/fuzz-wire-channel_reestablish.c @@ -0,0 +1,73 @@ +#include "config.h" +#include +#include +#include +#include +#include +#include + +struct channel_reestablish { + struct channel_id channel_id; + u64 next_commitment_number; + u64 next_revocation_number; + struct secret your_last_per_commitment_secret; + struct pubkey my_current_per_commitment_point; + struct tlv_channel_reestablish_tlvs *tlvs; +}; + +static void *encode(const tal_t *ctx, const struct channel_reestablish *s) +{ + return towire_channel_reestablish( + ctx, &s->channel_id, s->next_commitment_number, + s->next_revocation_number, &s->your_last_per_commitment_secret, + &s->my_current_per_commitment_point, s->tlvs); +} + +static struct channel_reestablish *decode(const tal_t *ctx, const void *p) +{ + struct channel_reestablish *s = tal(ctx, struct channel_reestablish); + + if (fromwire_channel_reestablish( + s, p, &s->channel_id, &s->next_commitment_number, + &s->next_revocation_number, &s->your_last_per_commitment_secret, + &s->my_current_per_commitment_point, &s->tlvs)) + return s; + return tal_free(s); +} + +static bool equal(const struct channel_reestablish *x, + const struct channel_reestablish *y) +{ + size_t upto_tlvs = (uintptr_t)&x->tlvs - (uintptr_t)x; + if (memcmp(x, y, upto_tlvs) != 0) + return false; + + assert(x->tlvs && y->tlvs); + + if (!memeq(x->tlvs->next_funding, tal_bytelen(x->tlvs->next_funding), + y->tlvs->next_funding, tal_bytelen(y->tlvs->next_funding))) + return false; + if (!memeq(x->tlvs->next_to_send, tal_bytelen(x->tlvs->next_to_send), + y->tlvs->next_to_send, tal_bytelen(y->tlvs->next_to_send))) + return false; + if (!memeq(x->tlvs->desired_channel_type, + tal_bytelen(x->tlvs->desired_channel_type), + y->tlvs->desired_channel_type, + tal_bytelen(y->tlvs->desired_channel_type))) + return false; + if (!memeq(x->tlvs->current_channel_type, + tal_bytelen(x->tlvs->current_channel_type), + y->tlvs->current_channel_type, + tal_bytelen(y->tlvs->current_channel_type))) + return false; + return memeq(x->tlvs->upgradable_channel_type, + tal_bytelen(x->tlvs->upgradable_channel_type), + y->tlvs->upgradable_channel_type, + tal_bytelen(y->tlvs->upgradable_channel_type)); +} + +void run(const u8 *data, size_t size) +{ + test_decode_encode(data, size, WIRE_CHANNEL_REESTABLISH, + struct channel_reestablish); +}