From 4097527efce9e6a2951983ebc1fe6cac2bfb7992 Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Tue, 12 Nov 2024 14:42:24 -0600 Subject: [PATCH 1/5] lnwire: remove superfluous "Prefix with..." comments These comments add nothing of value since the following line is always self-documenting: data = prefixWithMsgType(data, MsgTypeToBePrefixed) --- lnwire/fuzz_test.go | 41 ----------------------------------------- 1 file changed, 41 deletions(-) diff --git a/lnwire/fuzz_test.go b/lnwire/fuzz_test.go index 58f790aaf..57a20b3d6 100644 --- a/lnwire/fuzz_test.go +++ b/lnwire/fuzz_test.go @@ -105,7 +105,6 @@ func FuzzAcceptChannel(f *testing.F) { func FuzzAnnounceSignatures(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgAnnounceSignatures. data = prefixWithMsgType(data, MsgAnnounceSignatures) // Pass the message into our general fuzz harness for wire @@ -116,7 +115,6 @@ func FuzzAnnounceSignatures(f *testing.F) { func FuzzAnnounceSignatures2(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgAnnounceSignatures2. data = prefixWithMsgType(data, MsgAnnounceSignatures2) // Pass the message into our general fuzz harness for wire @@ -127,7 +125,6 @@ func FuzzAnnounceSignatures2(f *testing.F) { func FuzzChannelAnnouncement(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgChannelAnnouncement. data = prefixWithMsgType(data, MsgChannelAnnouncement) // Pass the message into our general fuzz harness for wire @@ -138,7 +135,6 @@ func FuzzChannelAnnouncement(f *testing.F) { func FuzzChannelAnnouncement2(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgChannelAnnouncement2. data = prefixWithMsgType(data, MsgChannelAnnouncement2) // Because require.Equal considers nil maps and empty maps @@ -183,7 +179,6 @@ func FuzzChannelAnnouncement2(f *testing.F) { func FuzzChannelReestablish(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgChannelReestablish. data = prefixWithMsgType(data, MsgChannelReestablish) // Pass the message into our general fuzz harness for wire @@ -194,7 +189,6 @@ func FuzzChannelReestablish(f *testing.F) { func FuzzChannelUpdate(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgChannelUpdate. data = prefixWithMsgType(data, MsgChannelUpdate) // Pass the message into our general fuzz harness for wire @@ -205,7 +199,6 @@ func FuzzChannelUpdate(f *testing.F) { func FuzzChannelUpdate2(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgChannelUpdate2. data = prefixWithMsgType(data, MsgChannelUpdate2) // Pass the message into our general fuzz harness for wire @@ -216,7 +209,6 @@ func FuzzChannelUpdate2(f *testing.F) { func FuzzClosingSigned(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgClosingSigned. data = prefixWithMsgType(data, MsgClosingSigned) // Pass the message into our general fuzz harness for wire @@ -227,7 +219,6 @@ func FuzzClosingSigned(f *testing.F) { func FuzzCommitSig(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgCommitSig. data = prefixWithMsgType(data, MsgCommitSig) // Pass the message into our general fuzz harness for wire @@ -238,7 +229,6 @@ func FuzzCommitSig(f *testing.F) { func FuzzError(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgError. data = prefixWithMsgType(data, MsgError) // Pass the message into our general fuzz harness for wire @@ -249,7 +239,6 @@ func FuzzError(f *testing.F) { func FuzzWarning(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgWarning. data = prefixWithMsgType(data, MsgWarning) // Pass the message into our general fuzz harness for wire @@ -260,7 +249,6 @@ func FuzzWarning(f *testing.F) { func FuzzStfu(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgStfu. data = prefixWithMsgType(data, MsgStfu) // Pass the message into our general fuzz harness for wire @@ -271,7 +259,6 @@ func FuzzStfu(f *testing.F) { func FuzzFundingCreated(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgFundingCreated. data = prefixWithMsgType(data, MsgFundingCreated) // Pass the message into our general fuzz harness for wire @@ -282,7 +269,6 @@ func FuzzFundingCreated(f *testing.F) { func FuzzChannelReady(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgChannelReady. data = prefixWithMsgType(data, MsgChannelReady) // Pass the message into our general fuzz harness for wire @@ -293,7 +279,6 @@ func FuzzChannelReady(f *testing.F) { func FuzzFundingSigned(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgFundingSigned. data = prefixWithMsgType(data, MsgFundingSigned) // Pass the message into our general fuzz harness for wire @@ -304,7 +289,6 @@ func FuzzFundingSigned(f *testing.F) { func FuzzGossipTimestampRange(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgGossipTimestampRange. data = prefixWithMsgType(data, MsgGossipTimestampRange) // Pass the message into our general fuzz harness for wire @@ -315,7 +299,6 @@ func FuzzGossipTimestampRange(f *testing.F) { func FuzzInit(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgInit. data = prefixWithMsgType(data, MsgInit) // Pass the message into our general fuzz harness for wire @@ -326,7 +309,6 @@ func FuzzInit(f *testing.F) { func FuzzNodeAnnouncement(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgNodeAnnouncement. data = prefixWithMsgType(data, MsgNodeAnnouncement) // We have to do this here instead of in harness so that @@ -383,7 +365,6 @@ func FuzzNodeAnnouncement(f *testing.F) { func FuzzOpenChannel(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgOpenChannel. data = prefixWithMsgType(data, MsgOpenChannel) // We have to do this here instead of in harness so that @@ -440,7 +421,6 @@ func FuzzOpenChannel(f *testing.F) { func FuzzPing(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgPing. data = prefixWithMsgType(data, MsgPing) // Pass the message into our general fuzz harness for wire @@ -451,7 +431,6 @@ func FuzzPing(f *testing.F) { func FuzzPong(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgPong. data = prefixWithMsgType(data, MsgPong) // Pass the message into our general fuzz harness for wire @@ -462,7 +441,6 @@ func FuzzPong(f *testing.F) { func FuzzQueryChannelRange(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgQueryChannelRange. data = prefixWithMsgType(data, MsgQueryChannelRange) // Pass the message into our general fuzz harness for wire @@ -495,7 +473,6 @@ func FuzzZlibQueryShortChanIDs(f *testing.F) { payload = append(payload, zlibByte...) payload = append(payload, compressedPayload...) - // Prefix with MsgQueryShortChanIDs. payload = prefixWithMsgType(payload, MsgQueryShortChanIDs) // Pass the message into our general fuzz harness for wire @@ -506,7 +483,6 @@ func FuzzZlibQueryShortChanIDs(f *testing.F) { func FuzzQueryShortChanIDs(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgQueryShortChanIDs. data = prefixWithMsgType(data, MsgQueryShortChanIDs) // Pass the message into our general fuzz harness for wire @@ -547,7 +523,6 @@ func FuzzZlibReplyChannelRange(f *testing.F) { payload = append(payload, zlibByte...) payload = append(payload, compressedPayload...) - // Prefix with MsgReplyChannelRange. payload = prefixWithMsgType(payload, MsgReplyChannelRange) // Pass the message into our general fuzz harness for wire @@ -558,7 +533,6 @@ func FuzzZlibReplyChannelRange(f *testing.F) { func FuzzReplyChannelRange(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgReplyChannelRange. data = prefixWithMsgType(data, MsgReplyChannelRange) // Because require.Equal considers nil slices and empty slices @@ -607,7 +581,6 @@ func FuzzReplyChannelRange(f *testing.F) { func FuzzReplyShortChanIDsEnd(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgReplyShortChanIDsEnd. data = prefixWithMsgType(data, MsgReplyShortChanIDsEnd) // Pass the message into our general fuzz harness for wire @@ -618,7 +591,6 @@ func FuzzReplyShortChanIDsEnd(f *testing.F) { func FuzzRevokeAndAck(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgRevokeAndAck. data = prefixWithMsgType(data, MsgRevokeAndAck) // Pass the message into our general fuzz harness for wire @@ -629,7 +601,6 @@ func FuzzRevokeAndAck(f *testing.F) { func FuzzShutdown(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgShutdown. data = prefixWithMsgType(data, MsgShutdown) // Pass the message into our general fuzz harness for wire @@ -640,7 +611,6 @@ func FuzzShutdown(f *testing.F) { func FuzzUpdateAddHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgUpdateAddHTLC. data = prefixWithMsgType(data, MsgUpdateAddHTLC) // Pass the message into our general fuzz harness for wire @@ -651,7 +621,6 @@ func FuzzUpdateAddHTLC(f *testing.F) { func FuzzUpdateFailHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgUpdateFailHTLC. data = prefixWithMsgType(data, MsgUpdateFailHTLC) // Pass the message into our general fuzz harness for wire @@ -662,7 +631,6 @@ func FuzzUpdateFailHTLC(f *testing.F) { func FuzzUpdateFailMalformedHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgUpdateFailMalformedHTLC. data = prefixWithMsgType(data, MsgUpdateFailMalformedHTLC) // Pass the message into our general fuzz harness for wire @@ -673,7 +641,6 @@ func FuzzUpdateFailMalformedHTLC(f *testing.F) { func FuzzUpdateFee(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgUpdateFee. data = prefixWithMsgType(data, MsgUpdateFee) // Pass the message into our general fuzz harness for wire @@ -684,7 +651,6 @@ func FuzzUpdateFee(f *testing.F) { func FuzzUpdateFulfillHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with MsgUpdateFulFillHTLC. data = prefixWithMsgType(data, MsgUpdateFulfillHTLC) // Pass the message into our general fuzz harness for wire @@ -695,7 +661,6 @@ func FuzzUpdateFulfillHTLC(f *testing.F) { func FuzzDynPropose(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with DynPropose. data = prefixWithMsgType(data, MsgDynPropose) // Pass the message into our general fuzz harness for wire @@ -706,7 +671,6 @@ func FuzzDynPropose(f *testing.F) { func FuzzDynReject(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with DynReject. data = prefixWithMsgType(data, MsgDynReject) // Pass the message into our general fuzz harness for wire @@ -717,7 +681,6 @@ func FuzzDynReject(f *testing.F) { func FuzzDynAck(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with DynReject. data = prefixWithMsgType(data, MsgDynAck) // Pass the message into our general fuzz harness for wire @@ -728,7 +691,6 @@ func FuzzDynAck(f *testing.F) { func FuzzKickoffSig(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with KickoffSig data = prefixWithMsgType(data, MsgKickoffSig) // Pass the message into our general fuzz harness for wire @@ -743,7 +705,6 @@ func FuzzCustomMessage(f *testing.F) { customMessageType += uint16(CustomTypeStart) } - // Prefix with CustomMessage. data = prefixWithMsgType(data, MessageType(customMessageType)) // Pass the message into our general fuzz harness for wire @@ -1034,7 +995,6 @@ func FuzzFailInvalidBlinding(f *testing.F) { func FuzzClosingSig(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with ClosingSig. data = prefixWithMsgType(data, MsgClosingSig) // Pass the message into our general fuzz harness for wire @@ -1045,7 +1005,6 @@ func FuzzClosingSig(f *testing.F) { func FuzzClosingComplete(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - // Prefix with ClosingComplete. data = prefixWithMsgType(data, MsgClosingComplete) // Pass the message into our general fuzz harness for wire From b7a1a1e9b8b26e9499c8620a485d0d74bfee9a28 Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Tue, 12 Nov 2024 14:55:43 -0600 Subject: [PATCH 2/5] lnwire: s/harness/wireMsgHarness This slightly more descriptive name distinguishes the wire message harness from the onion failure harness while also obviating the repetitive comments at every call site. --- lnwire/fuzz_test.go | 193 ++++++++++---------------------------------- 1 file changed, 41 insertions(+), 152 deletions(-) diff --git a/lnwire/fuzz_test.go b/lnwire/fuzz_test.go index 57a20b3d6..4499aa8a5 100644 --- a/lnwire/fuzz_test.go +++ b/lnwire/fuzz_test.go @@ -20,11 +20,11 @@ func prefixWithMsgType(data []byte, prefix MessageType) []byte { return data } -// harness performs the actual fuzz testing of the appropriate wire message. -// This function will check that the passed-in message passes wire length -// checks, is a valid message once deserialized, and passes a sequence of +// wireMsgHarness performs the actual fuzz testing of the appropriate wire +// message. This function will check that the passed-in message passes wire +// length checks, is a valid message once deserialized, and passes a sequence of // serialization and deserialization checks. -func harness(t *testing.T, data []byte) { +func wireMsgHarness(t *testing.T, data []byte) { t.Helper() // Create a reader with the byte array. @@ -106,30 +106,21 @@ func FuzzAcceptChannel(f *testing.F) { func FuzzAnnounceSignatures(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgAnnounceSignatures) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzAnnounceSignatures2(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgAnnounceSignatures2) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzChannelAnnouncement(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgChannelAnnouncement) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } @@ -180,130 +171,91 @@ func FuzzChannelAnnouncement2(f *testing.F) { func FuzzChannelReestablish(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgChannelReestablish) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzChannelUpdate(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgChannelUpdate) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzChannelUpdate2(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgChannelUpdate2) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzClosingSigned(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgClosingSigned) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzCommitSig(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgCommitSig) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzError(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgError) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzWarning(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgWarning) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzStfu(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgStfu) - - // Pass the message into our general fuzz harness for wire - // messages. - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzFundingCreated(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgFundingCreated) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzChannelReady(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgChannelReady) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzFundingSigned(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgFundingSigned) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzGossipTimestampRange(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgGossipTimestampRange) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzInit(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgInit) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } @@ -422,30 +374,21 @@ func FuzzOpenChannel(f *testing.F) { func FuzzPing(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgPing) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzPong(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgPong) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzQueryChannelRange(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgQueryChannelRange) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } @@ -474,20 +417,14 @@ func FuzzZlibQueryShortChanIDs(f *testing.F) { payload = append(payload, compressedPayload...) payload = prefixWithMsgType(payload, MsgQueryShortChanIDs) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, payload) + wireMsgHarness(t, payload) }) } func FuzzQueryShortChanIDs(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgQueryShortChanIDs) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } @@ -524,10 +461,7 @@ func FuzzZlibReplyChannelRange(f *testing.F) { payload = append(payload, compressedPayload...) payload = prefixWithMsgType(payload, MsgReplyChannelRange) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, payload) + wireMsgHarness(t, payload) }) } @@ -582,120 +516,84 @@ func FuzzReplyChannelRange(f *testing.F) { func FuzzReplyShortChanIDsEnd(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgReplyShortChanIDsEnd) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzRevokeAndAck(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgRevokeAndAck) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzShutdown(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgShutdown) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzUpdateAddHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgUpdateAddHTLC) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzUpdateFailHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgUpdateFailHTLC) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzUpdateFailMalformedHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgUpdateFailMalformedHTLC) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzUpdateFee(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgUpdateFee) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzUpdateFulfillHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgUpdateFulfillHTLC) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzDynPropose(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgDynPropose) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzDynReject(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgDynReject) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzDynAck(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgDynAck) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzKickoffSig(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgKickoffSig) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } @@ -706,10 +604,7 @@ func FuzzCustomMessage(f *testing.F) { } data = prefixWithMsgType(data, MessageType(customMessageType)) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } @@ -996,20 +891,14 @@ func FuzzFailInvalidBlinding(f *testing.F) { func FuzzClosingSig(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgClosingSig) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } func FuzzClosingComplete(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgClosingComplete) - - // Pass the message into our general fuzz harness for wire - // messages! - harness(t, data) + wireMsgHarness(t, data) }) } From b9381acb2d36431a6f0305461542da3c30137499 Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Tue, 12 Nov 2024 15:14:51 -0600 Subject: [PATCH 3/5] lnwire: move message prefixing to the harness The prefixing is done every time the harness is used, so it may as well reside in the harness itself. --- lnwire/fuzz_test.go | 115 +++++++++++++++----------------------------- 1 file changed, 40 insertions(+), 75 deletions(-) diff --git a/lnwire/fuzz_test.go b/lnwire/fuzz_test.go index 4499aa8a5..f82961139 100644 --- a/lnwire/fuzz_test.go +++ b/lnwire/fuzz_test.go @@ -24,9 +24,11 @@ func prefixWithMsgType(data []byte, prefix MessageType) []byte { // message. This function will check that the passed-in message passes wire // length checks, is a valid message once deserialized, and passes a sequence of // serialization and deserialization checks. -func wireMsgHarness(t *testing.T, data []byte) { +func wireMsgHarness(t *testing.T, data []byte, msgType MessageType) { t.Helper() + data = prefixWithMsgType(data, msgType) + // Create a reader with the byte array. r := bytes.NewReader(data) @@ -105,22 +107,19 @@ func FuzzAcceptChannel(f *testing.F) { func FuzzAnnounceSignatures(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgAnnounceSignatures) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgAnnounceSignatures) }) } func FuzzAnnounceSignatures2(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgAnnounceSignatures2) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgAnnounceSignatures2) }) } func FuzzChannelAnnouncement(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgChannelAnnouncement) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgChannelAnnouncement) }) } @@ -170,92 +169,79 @@ func FuzzChannelAnnouncement2(f *testing.F) { func FuzzChannelReestablish(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgChannelReestablish) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgChannelReestablish) }) } func FuzzChannelUpdate(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgChannelUpdate) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgChannelUpdate) }) } func FuzzChannelUpdate2(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgChannelUpdate2) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgChannelUpdate2) }) } func FuzzClosingSigned(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgClosingSigned) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgClosingSigned) }) } func FuzzCommitSig(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgCommitSig) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgCommitSig) }) } func FuzzError(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgError) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgError) }) } func FuzzWarning(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgWarning) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgWarning) }) } func FuzzStfu(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgStfu) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgStfu) }) } func FuzzFundingCreated(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgFundingCreated) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgFundingCreated) }) } func FuzzChannelReady(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgChannelReady) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgChannelReady) }) } func FuzzFundingSigned(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgFundingSigned) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgFundingSigned) }) } func FuzzGossipTimestampRange(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgGossipTimestampRange) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgGossipTimestampRange) }) } func FuzzInit(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgInit) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgInit) }) } @@ -373,22 +359,19 @@ func FuzzOpenChannel(f *testing.F) { func FuzzPing(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgPing) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgPing) }) } func FuzzPong(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgPong) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgPong) }) } func FuzzQueryChannelRange(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgQueryChannelRange) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgQueryChannelRange) }) } @@ -416,15 +399,13 @@ func FuzzZlibQueryShortChanIDs(f *testing.F) { payload = append(payload, zlibByte...) payload = append(payload, compressedPayload...) - payload = prefixWithMsgType(payload, MsgQueryShortChanIDs) - wireMsgHarness(t, payload) + wireMsgHarness(t, payload, MsgQueryShortChanIDs) }) } func FuzzQueryShortChanIDs(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgQueryShortChanIDs) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgQueryShortChanIDs) }) } @@ -460,8 +441,7 @@ func FuzzZlibReplyChannelRange(f *testing.F) { payload = append(payload, zlibByte...) payload = append(payload, compressedPayload...) - payload = prefixWithMsgType(payload, MsgReplyChannelRange) - wireMsgHarness(t, payload) + wireMsgHarness(t, payload, MsgReplyChannelRange) }) } @@ -515,85 +495,73 @@ func FuzzReplyChannelRange(f *testing.F) { func FuzzReplyShortChanIDsEnd(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgReplyShortChanIDsEnd) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgReplyShortChanIDsEnd) }) } func FuzzRevokeAndAck(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgRevokeAndAck) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgRevokeAndAck) }) } func FuzzShutdown(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgShutdown) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgShutdown) }) } func FuzzUpdateAddHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgUpdateAddHTLC) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgUpdateAddHTLC) }) } func FuzzUpdateFailHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgUpdateFailHTLC) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgUpdateFailHTLC) }) } func FuzzUpdateFailMalformedHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgUpdateFailMalformedHTLC) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgUpdateFailMalformedHTLC) }) } func FuzzUpdateFee(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgUpdateFee) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgUpdateFee) }) } func FuzzUpdateFulfillHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgUpdateFulfillHTLC) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgUpdateFulfillHTLC) }) } func FuzzDynPropose(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgDynPropose) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgDynPropose) }) } func FuzzDynReject(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgDynReject) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgDynReject) }) } func FuzzDynAck(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgDynAck) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgDynAck) }) } func FuzzKickoffSig(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgKickoffSig) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgKickoffSig) }) } @@ -603,8 +571,7 @@ func FuzzCustomMessage(f *testing.F) { customMessageType += uint16(CustomTypeStart) } - data = prefixWithMsgType(data, MessageType(customMessageType)) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MessageType(customMessageType)) }) } @@ -890,15 +857,13 @@ func FuzzFailInvalidBlinding(f *testing.F) { func FuzzClosingSig(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgClosingSig) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgClosingSig) }) } func FuzzClosingComplete(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgClosingComplete) - wireMsgHarness(t, data) + wireMsgHarness(t, data, MsgClosingComplete) }) } From d0e6a7a37bcf4fd5fc5724e125571810269c285b Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Tue, 12 Nov 2024 16:03:25 -0600 Subject: [PATCH 4/5] lnwire: adapt harness for custom equality funcs There are several fuzz targets that can't use the standard require.Equal check for various reasons. By adapting the harness to accept a custom equality function, we can reduce code duplication in these targets. --- lnwire/fuzz_test.go | 326 +++++++++++++++----------------------------- 1 file changed, 110 insertions(+), 216 deletions(-) diff --git a/lnwire/fuzz_test.go b/lnwire/fuzz_test.go index f82961139..66f545217 100644 --- a/lnwire/fuzz_test.go +++ b/lnwire/fuzz_test.go @@ -20,12 +20,16 @@ func prefixWithMsgType(data []byte, prefix MessageType) []byte { return data } -// wireMsgHarness performs the actual fuzz testing of the appropriate wire +// assertEqualFunc is a function used to assert that two deserialized messages +// are equivalent. +type assertEqualFunc func(t *testing.T, x, y any) + +// wireMsgHarnessCustom performs the actual fuzz testing of the appropriate wire // message. This function will check that the passed-in message passes wire // length checks, is a valid message once deserialized, and passes a sequence of // serialization and deserialization checks. -func wireMsgHarness(t *testing.T, data []byte, msgType MessageType) { - t.Helper() +func wireMsgHarnessCustom(t *testing.T, data []byte, msgType MessageType, + assertEqual assertEqualFunc) { data = prefixWithMsgType(data, msgType) @@ -53,55 +57,41 @@ func wireMsgHarness(t *testing.T, data []byte, msgType MessageType) { // message. newMsg, err := ReadMessage(&b, 0) require.NoError(t, err) - require.Equal(t, msg, newMsg) + + assertEqual(t, msg, newMsg) +} + +func wireMsgHarness(t *testing.T, data []byte, msgType MessageType) { + t.Helper() + assertEq := func(t *testing.T, x, y any) { + require.Equal(t, x, y) + } + wireMsgHarnessCustom(t, data, msgType, assertEq) } func FuzzAcceptChannel(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgAcceptChannel) - // Create a reader with the byte array. - r := bytes.NewReader(data) - - // Make sure byte array length (excluding 2 bytes for message - // type) is less than max payload size for the wire message. - payloadLen := uint32(len(data)) - 2 - if payloadLen > MaxMsgBody { - return - } - - 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, &AcceptChannel{}, msg) - first, _ := msg.(*AcceptChannel) - require.IsType(t, &AcceptChannel{}, newMsg) - second, _ := newMsg.(*AcceptChannel) - // We can't use require.Equal for UpfrontShutdownScript, since // we consider the empty slice and nil to be equivalent. - require.True( - t, bytes.Equal( - first.UpfrontShutdownScript, - second.UpfrontShutdownScript, - ), - ) - first.UpfrontShutdownScript = nil - second.UpfrontShutdownScript = nil + assertEq := func(t *testing.T, x, y any) { + require.IsType(t, &AcceptChannel{}, x) + first, _ := x.(*AcceptChannel) + require.IsType(t, &AcceptChannel{}, y) + second, _ := y.(*AcceptChannel) - require.Equal(t, first, second) + require.True( + t, bytes.Equal( + first.UpfrontShutdownScript, + second.UpfrontShutdownScript, + ), + ) + first.UpfrontShutdownScript = nil + second.UpfrontShutdownScript = nil + + require.Equal(t, first, second) + } + + wireMsgHarnessCustom(t, data, MsgAcceptChannel, assertEq) }) } @@ -125,45 +115,25 @@ func FuzzChannelAnnouncement(f *testing.F) { func FuzzChannelAnnouncement2(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgChannelAnnouncement2) - - // Because require.Equal considers nil maps and empty maps - // to be non-equal, we must manually compare Features 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, &ChannelAnnouncement2{}, msg) - first, _ := msg.(*ChannelAnnouncement2) - require.IsType(t, &ChannelAnnouncement2{}, newMsg) - second, _ := newMsg.(*ChannelAnnouncement2) - // We can't use require.Equal for Features, since we consider // the empty map and nil to be equivalent. - require.True(t, first.Features.Val.Equals(&second.Features.Val)) - first.Features.Val = *NewRawFeatureVector() - second.Features.Val = *NewRawFeatureVector() + assertEq := func(t *testing.T, x, y any) { + require.IsType(t, &ChannelAnnouncement2{}, x) + first, _ := x.(*ChannelAnnouncement2) + require.IsType(t, &ChannelAnnouncement2{}, y) + second, _ := y.(*ChannelAnnouncement2) - require.Equal(t, first, second) + require.True( + t, + first.Features.Val.Equals(&second.Features.Val), + ) + first.Features.Val = *NewRawFeatureVector() + second.Features.Val = *NewRawFeatureVector() + + require.Equal(t, first, second) + } + + wireMsgHarnessCustom(t, data, MsgChannelAnnouncement2, assertEq) }) } @@ -247,113 +217,57 @@ func FuzzInit(f *testing.F) { func FuzzNodeAnnouncement(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgNodeAnnouncement) - - // We have to do this here instead of in harness so that - // reflect.DeepEqual isn't called. Address (de)serialization - // messes up the fuzzing assertions. - - // Create a reader with the byte array. - r := bytes.NewReader(data) - - // Make sure byte array length (excluding 2 bytes for message - // type) is less than max payload size for the wire message. - payloadLen := uint32(len(data)) - 2 - if payloadLen > MaxMsgBody { - return - } - - 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, &NodeAnnouncement{}, msg) - first, _ := msg.(*NodeAnnouncement) - require.IsType(t, &NodeAnnouncement{}, newMsg) - second, _ := newMsg.(*NodeAnnouncement) - // We can't use require.Equal for Addresses, since the same IP // can be represented by different underlying bytes. Instead, we // compare the normalized string representation of each address. - require.Equal(t, len(first.Addresses), len(second.Addresses)) - for i := range first.Addresses { - require.Equal( - t, first.Addresses[i].String(), - second.Addresses[i].String(), - ) - } - first.Addresses = nil - second.Addresses = nil + assertEq := func(t *testing.T, x, y any) { + require.IsType(t, &NodeAnnouncement{}, x) + first, _ := x.(*NodeAnnouncement) + require.IsType(t, &NodeAnnouncement{}, y) + second, _ := y.(*NodeAnnouncement) - require.Equal(t, first, second) + require.Equal( + t, len(first.Addresses), len(second.Addresses), + ) + for i := range first.Addresses { + require.Equal( + t, first.Addresses[i].String(), + second.Addresses[i].String(), + ) + } + first.Addresses = nil + second.Addresses = nil + + require.Equal(t, first, second) + } + + wireMsgHarnessCustom(t, data, MsgNodeAnnouncement, assertEq) }) } func FuzzOpenChannel(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgOpenChannel) - - // We have to do this here instead of in harness so that - // reflect.DeepEqual isn't called. Because of the - // UpfrontShutdownScript encoding, the first message and second - // message aren't deeply equal since the first has a nil slice - // and the other has an empty slice. - - // Create a reader with the byte array. - r := bytes.NewReader(data) - - // Make sure byte array length (excluding 2 bytes for message - // type) is less than max payload size for the wire message. - payloadLen := uint32(len(data)) - 2 - if payloadLen > MaxMsgBody { - return - } - - 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, &OpenChannel{}, msg) - first, _ := msg.(*OpenChannel) - require.IsType(t, &OpenChannel{}, newMsg) - second, _ := newMsg.(*OpenChannel) - // We can't use require.Equal for UpfrontShutdownScript, since // we consider the empty slice and nil to be equivalent. - require.True( - t, bytes.Equal( - first.UpfrontShutdownScript, - second.UpfrontShutdownScript, - ), - ) - first.UpfrontShutdownScript = nil - second.UpfrontShutdownScript = nil + assertEq := func(t *testing.T, x, y any) { + require.IsType(t, &OpenChannel{}, x) + first, _ := x.(*OpenChannel) + require.IsType(t, &OpenChannel{}, y) + second, _ := y.(*OpenChannel) - require.Equal(t, first, second) + require.True( + t, bytes.Equal( + first.UpfrontShutdownScript, + second.UpfrontShutdownScript, + ), + ) + first.UpfrontShutdownScript = nil + second.UpfrontShutdownScript = nil + + require.Equal(t, first, second) + } + + wireMsgHarnessCustom(t, data, MsgOpenChannel, assertEq) }) } @@ -447,49 +361,29 @@ func FuzzZlibReplyChannelRange(f *testing.F) { func FuzzReplyChannelRange(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - data = prefixWithMsgType(data, MsgReplyChannelRange) - - // 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 + assertEq := func(t *testing.T, x, y any) { + require.IsType(t, &ReplyChannelRange{}, x) + first, _ := x.(*ReplyChannelRange) + require.IsType(t, &ReplyChannelRange{}, y) + second, _ := y.(*ReplyChannelRange) - require.Equal(t, first, second) + 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) + } + + wireMsgHarnessCustom(t, data, MsgReplyChannelRange, assertEq) }) } From 75bdf2d252011528d682f4d3a76b941036aa9bf5 Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Tue, 12 Nov 2024 16:11:17 -0600 Subject: [PATCH 5/5] lnwire: use assertEqualFunc in onion failure harness Simplifies the code slightly and improves the error message printed if the original and deserialized messages do not match. --- lnwire/fuzz_test.go | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/lnwire/fuzz_test.go b/lnwire/fuzz_test.go index 66f545217..e608eb9b0 100644 --- a/lnwire/fuzz_test.go +++ b/lnwire/fuzz_test.go @@ -4,7 +4,6 @@ import ( "bytes" "compress/zlib" "encoding/binary" - "reflect" "testing" "github.com/stretchr/testify/require" @@ -554,16 +553,12 @@ func prefixWithFailCode(data []byte, code FailCode) []byte { return data } -// equalFunc is a function used to determine whether two deserialized messages -// are equivalent. -type equalFunc func(x, y any) bool - // onionFailureHarnessCustom performs the actual fuzz testing of the appropriate // onion failure message. This function will check that the passed-in message // passes wire length checks, is a valid message once deserialized, and passes a // sequence of serialization and deserialization checks. func onionFailureHarnessCustom(t *testing.T, data []byte, code FailCode, - eq equalFunc) { + assertEqual assertEqualFunc) { data = prefixWithFailCode(data, code) @@ -589,12 +584,7 @@ func onionFailureHarnessCustom(t *testing.T, data []byte, code FailCode, newMsg, err := DecodeFailureMessage(&b, 0) require.NoError(t, err, "failed to decode serialized failure message") - require.True( - t, eq(msg, newMsg), - "original message and deserialized message are not equal: "+ - "%v != %v", - msg, newMsg, - ) + assertEqual(t, msg, newMsg) // Now verify that encoding/decoding full packets works as expected. @@ -628,17 +618,15 @@ func onionFailureHarnessCustom(t *testing.T, data []byte, code FailCode, pktMsg, err := DecodeFailure(&pktBuf, 0) require.NoError(t, err, "failed to decode failure packet") - require.True( - t, eq(msg, pktMsg), - "original message and decoded packet message are not equal: "+ - "%v != %v", - msg, pktMsg, - ) + assertEqual(t, msg, pktMsg) } func onionFailureHarness(t *testing.T, data []byte, code FailCode) { t.Helper() - onionFailureHarnessCustom(t, data, code, reflect.DeepEqual) + assertEq := func(t *testing.T, x, y any) { + require.Equal(t, x, y) + } + onionFailureHarnessCustom(t, data, code, assertEq) } func FuzzFailIncorrectDetails(f *testing.F) { @@ -646,7 +634,7 @@ func FuzzFailIncorrectDetails(f *testing.F) { // Since FailIncorrectDetails.Decode can leave extraOpaqueData // as nil while FailIncorrectDetails.Encode writes an empty // slice, we need to use a custom equality function. - eq := func(x, y any) bool { + assertEq := func(t *testing.T, x, y any) { msg1, ok := x.(*FailIncorrectDetails) require.True( t, ok, "msg1 was not FailIncorrectDetails", @@ -657,16 +645,18 @@ func FuzzFailIncorrectDetails(f *testing.F) { t, ok, "msg2 was not FailIncorrectDetails", ) - return msg1.amount == msg2.amount && - msg1.height == msg2.height && - bytes.Equal( + require.Equal(t, msg1.amount, msg2.amount) + require.Equal(t, msg1.height, msg2.height) + require.True( + t, bytes.Equal( msg1.extraOpaqueData, msg2.extraOpaqueData, - ) + ), + ) } onionFailureHarnessCustom( - t, data, CodeIncorrectOrUnknownPaymentDetails, eq, + t, data, CodeIncorrectOrUnknownPaymentDetails, assertEq, ) }) }