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.
This commit is contained in:
Matt Morehouse 2024-11-08 15:03:30 -06:00
parent f1b7d52308
commit 4f7267ecea
No known key found for this signature in database
GPG Key ID: CC8ECA224831C982

View File

@ -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(),
)
})
}