From 475cd6e344d916f5eb60f094949192533a7bfef8 Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Tue, 10 Sep 2024 11:41:31 -0500 Subject: [PATCH] lnwire: manually compare Timestamps in fuzz test We can't use require.Equal because it considers nil slices and empty slices to be not equal. --- lnwire/fuzz_test.go | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/lnwire/fuzz_test.go b/lnwire/fuzz_test.go index 9a759604a..d6ec3b61b 100644 --- a/lnwire/fuzz_test.go +++ b/lnwire/fuzz_test.go @@ -494,9 +494,47 @@ func FuzzReplyChannelRange(f *testing.F) { // Prefix with MsgReplyChannelRange. data = prefixWithMsgType(data, MsgReplyChannelRange) - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + // Because require.Equal considers nil slices and empty slices + // to be non-equal, we must manually compare the Timestamps + // field rather than using the harness. + + if len(data) > MaxSliceLength { + return + } + + r := bytes.NewReader(data) + msg, err := ReadMessage(r, 0) + if err != nil { + return + } + + // We will serialize the message into a new bytes buffer. + var b bytes.Buffer + _, err = WriteMessage(&b, msg, 0) + require.NoError(t, err) + + // Deserialize the message from the serialized bytes buffer, and + // then assert that the original message is equal to the newly + // deserialized message. + newMsg, err := ReadMessage(&b, 0) + require.NoError(t, err) + + require.IsType(t, &ReplyChannelRange{}, msg) + first, _ := msg.(*ReplyChannelRange) + require.IsType(t, &ReplyChannelRange{}, newMsg) + second, _ := newMsg.(*ReplyChannelRange) + + // We can't use require.Equal for Timestamps, since we consider + // the empty slice and nil to be equivalent. + require.Equal(t, len(first.Timestamps), len(second.Timestamps)) + for i, ts1 := range first.Timestamps { + ts2 := second.Timestamps[i] + require.Equal(t, ts1, ts2) + } + first.Timestamps = nil + second.Timestamps = nil + + require.Equal(t, first, second) }) }