From 4f7267ecea27f3a94c3445b9c96de9bcbf80ce33 Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Fri, 8 Nov 2024 15:03:30 -0600 Subject: [PATCH] lnwire: add fuzz target for Fee TLV The new Fee TLV is not included in any other messages within the lnwire package, so it currently has no fuzzing coverage. This fuzz target directly tests the encoding/decoding of the TLV to get some coverage. --- lnwire/fuzz_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lnwire/fuzz_test.go b/lnwire/fuzz_test.go index 5809a7e16..58f790aaf 100644 --- a/lnwire/fuzz_test.go +++ b/lnwire/fuzz_test.go @@ -1053,3 +1053,30 @@ func FuzzClosingComplete(f *testing.F) { harness(t, data) }) } + +// FuzzFee tests that decoding and re-encoding a Fee TLV does not mutate it. +func FuzzFee(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + if len(data) > 8 { + return + } + + var fee Fee + var buf [8]byte + r := bytes.NewReader(data) + + if err := feeDecoder(r, &fee, &buf, 8); err != nil { + return + } + + var b bytes.Buffer + require.NoError(t, feeEncoder(&b, &fee, &buf)) + + // Use bytes.Equal instead of require.Equal so that nil and + // empty slices are considered equal. + require.True( + t, bytes.Equal(data, b.Bytes()), "%v != %v", data, + b.Bytes(), + ) + }) +}