From 3d4d8cd6cc61badff9ce62d6a45e0700af6b2a64 Mon Sep 17 00:00:00 2001 From: Conner Date: Fri, 29 Jul 2022 17:42:42 -0400 Subject: [PATCH 1/7] lnwire: migrate fuzz tests --- fuzz/lnwire/accept_channel.go | 129 ---- fuzz/lnwire/announce_signatures.go | 17 - fuzz/lnwire/channel_announcement.go | 17 - fuzz/lnwire/channel_reestablish.go | 17 - fuzz/lnwire/channel_update.go | 17 - fuzz/lnwire/closing_signed.go | 17 - fuzz/lnwire/commit_sig.go | 17 - fuzz/lnwire/error.go | 17 - fuzz/lnwire/funding_created.go | 17 - fuzz/lnwire/funding_locked.go | 17 - fuzz/lnwire/funding_signed.go | 17 - fuzz/lnwire/fuzz_utils.go | 65 -- fuzz/lnwire/gossip_timestamp_range.go | 17 - fuzz/lnwire/init.go | 17 - fuzz/lnwire/node_announcement.go | 103 --- fuzz/lnwire/open_channel.go | 142 ---- fuzz/lnwire/ping.go | 17 - fuzz/lnwire/pong.go | 17 - fuzz/lnwire/query_channel_range.go | 17 - fuzz/lnwire/query_short_chan_ids.go | 17 - fuzz/lnwire/query_short_chan_ids_zlib.go | 48 -- fuzz/lnwire/reply_channel_range.go | 17 - fuzz/lnwire/reply_channel_range_zlib.go | 56 -- fuzz/lnwire/reply_short_chan_ids_end.go | 17 - fuzz/lnwire/revoke_and_ack.go | 17 - fuzz/lnwire/shutdown.go | 17 - fuzz/lnwire/update_add_htlc.go | 17 - fuzz/lnwire/update_fail_htlc.go | 17 - fuzz/lnwire/update_fail_malformed_htlc.go | 17 - fuzz/lnwire/update_fee.go | 17 - fuzz/lnwire/update_fulfill_htlc.go | 17 - lnwire/fuzz_test.go | 752 ++++++++++++++++++++++ 32 files changed, 752 insertions(+), 968 deletions(-) delete mode 100644 fuzz/lnwire/accept_channel.go delete mode 100644 fuzz/lnwire/announce_signatures.go delete mode 100644 fuzz/lnwire/channel_announcement.go delete mode 100644 fuzz/lnwire/channel_reestablish.go delete mode 100644 fuzz/lnwire/channel_update.go delete mode 100644 fuzz/lnwire/closing_signed.go delete mode 100644 fuzz/lnwire/commit_sig.go delete mode 100644 fuzz/lnwire/error.go delete mode 100644 fuzz/lnwire/funding_created.go delete mode 100644 fuzz/lnwire/funding_locked.go delete mode 100644 fuzz/lnwire/funding_signed.go delete mode 100644 fuzz/lnwire/fuzz_utils.go delete mode 100644 fuzz/lnwire/gossip_timestamp_range.go delete mode 100644 fuzz/lnwire/init.go delete mode 100644 fuzz/lnwire/node_announcement.go delete mode 100644 fuzz/lnwire/open_channel.go delete mode 100644 fuzz/lnwire/ping.go delete mode 100644 fuzz/lnwire/pong.go delete mode 100644 fuzz/lnwire/query_channel_range.go delete mode 100644 fuzz/lnwire/query_short_chan_ids.go delete mode 100644 fuzz/lnwire/query_short_chan_ids_zlib.go delete mode 100644 fuzz/lnwire/reply_channel_range.go delete mode 100644 fuzz/lnwire/reply_channel_range_zlib.go delete mode 100644 fuzz/lnwire/reply_short_chan_ids_end.go delete mode 100644 fuzz/lnwire/revoke_and_ack.go delete mode 100644 fuzz/lnwire/shutdown.go delete mode 100644 fuzz/lnwire/update_add_htlc.go delete mode 100644 fuzz/lnwire/update_fail_htlc.go delete mode 100644 fuzz/lnwire/update_fail_malformed_htlc.go delete mode 100644 fuzz/lnwire/update_fee.go delete mode 100644 fuzz/lnwire/update_fulfill_htlc.go create mode 100644 lnwire/fuzz_test.go diff --git a/fuzz/lnwire/accept_channel.go b/fuzz/lnwire/accept_channel.go deleted file mode 100644 index f57c2af42..000000000 --- a/fuzz/lnwire/accept_channel.go +++ /dev/null @@ -1,129 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "bytes" - - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_accept_channel is used by go-fuzz. -func Fuzz_accept_channel(data []byte) int { - // Prefix with MsgAcceptChannel. - data = prefixWithMsgType(data, lnwire.MsgAcceptChannel) - - // We have to do this here instead of in fuzz.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 > lnwire.MaxMsgBody { - return 1 - } - - msg, err := lnwire.ReadMessage(r, 0) - if err != nil { - // go-fuzz generated []byte that cannot be represented as a - // wire message but we will return 0 so go-fuzz can modify the - // input. - return 1 - } - - // We will serialize the message into a new bytes buffer. - var b bytes.Buffer - if _, err := lnwire.WriteMessage(&b, msg, 0); err != nil { - // Could not serialize message into bytes buffer, panic - panic(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 := lnwire.ReadMessage(&b, 0) - if err != nil { - // Could not deserialize message from bytes buffer, panic - panic(err) - } - - // Now compare every field instead of using reflect.DeepEqual. - // For UpfrontShutdownScript, we only compare bytes. This probably takes - // up more branches than necessary, but that's fine for now. - var shouldPanic bool - first := msg.(*lnwire.AcceptChannel) - second := newMsg.(*lnwire.AcceptChannel) - - if !bytes.Equal(first.PendingChannelID[:], second.PendingChannelID[:]) { - shouldPanic = true - } - - if first.DustLimit != second.DustLimit { - shouldPanic = true - } - - if first.MaxValueInFlight != second.MaxValueInFlight { - shouldPanic = true - } - - if first.ChannelReserve != second.ChannelReserve { - shouldPanic = true - } - - if first.HtlcMinimum != second.HtlcMinimum { - shouldPanic = true - } - - if first.MinAcceptDepth != second.MinAcceptDepth { - shouldPanic = true - } - - if first.CsvDelay != second.CsvDelay { - shouldPanic = true - } - - if first.MaxAcceptedHTLCs != second.MaxAcceptedHTLCs { - shouldPanic = true - } - - if !first.FundingKey.IsEqual(second.FundingKey) { - shouldPanic = true - } - - if !first.RevocationPoint.IsEqual(second.RevocationPoint) { - shouldPanic = true - } - - if !first.PaymentPoint.IsEqual(second.PaymentPoint) { - shouldPanic = true - } - - if !first.DelayedPaymentPoint.IsEqual(second.DelayedPaymentPoint) { - shouldPanic = true - } - - if !first.HtlcPoint.IsEqual(second.HtlcPoint) { - shouldPanic = true - } - - if !first.FirstCommitmentPoint.IsEqual(second.FirstCommitmentPoint) { - shouldPanic = true - } - - if !bytes.Equal(first.UpfrontShutdownScript, second.UpfrontShutdownScript) { - shouldPanic = true - } - - if shouldPanic { - panic("original message and deserialized message are not equal") - } - - // Add this input to the corpus. - return 1 -} diff --git a/fuzz/lnwire/announce_signatures.go b/fuzz/lnwire/announce_signatures.go deleted file mode 100644 index c35d5a248..000000000 --- a/fuzz/lnwire/announce_signatures.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_announce_signatures is used by go-fuzz. -func Fuzz_announce_signatures(data []byte) int { - // Prefix with MsgAnnounceSignatures. - data = prefixWithMsgType(data, lnwire.MsgAnnounceSignatures) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/channel_announcement.go b/fuzz/lnwire/channel_announcement.go deleted file mode 100644 index 92800185b..000000000 --- a/fuzz/lnwire/channel_announcement.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_channel_announcement is used by go-fuzz. -func Fuzz_channel_announcement(data []byte) int { - // Prefix with MsgChannelAnnouncement. - data = prefixWithMsgType(data, lnwire.MsgChannelAnnouncement) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/channel_reestablish.go b/fuzz/lnwire/channel_reestablish.go deleted file mode 100644 index 841142c1c..000000000 --- a/fuzz/lnwire/channel_reestablish.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_channel_reestablish is used by go-fuzz. -func Fuzz_channel_reestablish(data []byte) int { - // Prefix with MsgChannelReestablish. - data = prefixWithMsgType(data, lnwire.MsgChannelReestablish) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/channel_update.go b/fuzz/lnwire/channel_update.go deleted file mode 100644 index cce82442c..000000000 --- a/fuzz/lnwire/channel_update.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_channel_update is used by go-fuzz. -func Fuzz_channel_update(data []byte) int { - // Prefix with MsgChannelUpdate. - data = prefixWithMsgType(data, lnwire.MsgChannelUpdate) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/closing_signed.go b/fuzz/lnwire/closing_signed.go deleted file mode 100644 index 4d3dd59f1..000000000 --- a/fuzz/lnwire/closing_signed.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_closing_signed is used by go-fuzz. -func Fuzz_closing_signed(data []byte) int { - // Prefix with MsgClosingSigned. - data = prefixWithMsgType(data, lnwire.MsgClosingSigned) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/commit_sig.go b/fuzz/lnwire/commit_sig.go deleted file mode 100644 index 7dae96518..000000000 --- a/fuzz/lnwire/commit_sig.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_commit_sig is used by go-fuzz. -func Fuzz_commit_sig(data []byte) int { - // Prefix with MsgCommitSig. - data = prefixWithMsgType(data, lnwire.MsgCommitSig) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/error.go b/fuzz/lnwire/error.go deleted file mode 100644 index f613606e4..000000000 --- a/fuzz/lnwire/error.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_error is used by go-fuzz. -func Fuzz_error(data []byte) int { - // Prefix with MsgError. - data = prefixWithMsgType(data, lnwire.MsgError) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/funding_created.go b/fuzz/lnwire/funding_created.go deleted file mode 100644 index a14b1083f..000000000 --- a/fuzz/lnwire/funding_created.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_funding_created is used by go-fuzz. -func Fuzz_funding_created(data []byte) int { - // Prefix with MsgFundingCreated. - data = prefixWithMsgType(data, lnwire.MsgFundingCreated) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/funding_locked.go b/fuzz/lnwire/funding_locked.go deleted file mode 100644 index c5512f97e..000000000 --- a/fuzz/lnwire/funding_locked.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_funding_locked is used by go-fuzz. -func Fuzz_funding_locked(data []byte) int { - // Prefix with MsgFundingLocked. - data = prefixWithMsgType(data, lnwire.MsgFundingLocked) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/funding_signed.go b/fuzz/lnwire/funding_signed.go deleted file mode 100644 index 1af68add6..000000000 --- a/fuzz/lnwire/funding_signed.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_funding_signed is used by go-fuzz. -func Fuzz_funding_signed(data []byte) int { - // Prefix with MsgFundingSigned. - prefixWithMsgType(data, lnwire.MsgFundingSigned) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/fuzz_utils.go b/fuzz/lnwire/fuzz_utils.go deleted file mode 100644 index 98ab50bce..000000000 --- a/fuzz/lnwire/fuzz_utils.go +++ /dev/null @@ -1,65 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "bytes" - "encoding/binary" - "reflect" - - "github.com/lightningnetwork/lnd/lnwire" -) - -// prefixWithMsgType takes []byte and adds a wire protocol prefix -// to make the []byte into an actual message to be used in fuzzing. -func prefixWithMsgType(data []byte, prefix lnwire.MessageType) []byte { - var prefixBytes [2]byte - binary.BigEndian.PutUint16(prefixBytes[:], uint16(prefix)) - data = append(prefixBytes[:], data...) - 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 serialization -// and deserialization checks. Returns an int that determines whether the input -// is unique or not. -func harness(data []byte) int { - // Create a reader with the byte array. - r := bytes.NewReader(data) - - // Check that the created message is not greater than the maximum - // message size. - if len(data) > lnwire.MaxSliceLength { - return 1 - } - - msg, err := lnwire.ReadMessage(r, 0) - if err != nil { - return 1 - } - - // We will serialize the message into a new bytes buffer. - var b bytes.Buffer - if _, err := lnwire.WriteMessage(&b, msg, 0); err != nil { - // Could not serialize message into bytes buffer, panic - panic(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 := lnwire.ReadMessage(&b, 0) - if err != nil { - // Could not deserialize message from bytes buffer, panic - panic(err) - } - - if !reflect.DeepEqual(msg, newMsg) { - // Deserialized message and original message are not deeply equal. - panic("original message and deserialized message are not deeply equal") - } - - return 1 -} diff --git a/fuzz/lnwire/gossip_timestamp_range.go b/fuzz/lnwire/gossip_timestamp_range.go deleted file mode 100644 index 5cfe89170..000000000 --- a/fuzz/lnwire/gossip_timestamp_range.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_gossip_timestamp_range is used by go-fuzz. -func Fuzz_gossip_timestamp_range(data []byte) int { - // Prefix with MsgGossipTimestampRange. - data = prefixWithMsgType(data, lnwire.MsgGossipTimestampRange) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/init.go b/fuzz/lnwire/init.go deleted file mode 100644 index 3a10fb675..000000000 --- a/fuzz/lnwire/init.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_init is used by go-fuzz. -func Fuzz_init(data []byte) int { - // Prefix with MsgInit. - data = prefixWithMsgType(data, lnwire.MsgInit) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/node_announcement.go b/fuzz/lnwire/node_announcement.go deleted file mode 100644 index 7f73c8371..000000000 --- a/fuzz/lnwire/node_announcement.go +++ /dev/null @@ -1,103 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "bytes" - "reflect" - - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_node_announcement is used by go-fuzz. -func Fuzz_node_announcement(data []byte) int { - // Prefix with MsgNodeAnnouncement. - data = prefixWithMsgType(data, lnwire.MsgNodeAnnouncement) - - // We have to do this here instead of in fuzz.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 > lnwire.MaxMsgBody { - return 1 - } - - msg, err := lnwire.ReadMessage(r, 0) - if err != nil { - return 1 - } - - // We will serialize the message into a new bytes buffer. - var b bytes.Buffer - if _, err := lnwire.WriteMessage(&b, msg, 0); err != nil { - // Could not serialize message into bytes buffer, panic - panic(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 := lnwire.ReadMessage(&b, 0) - if err != nil { - // Could not deserialize message from bytes buffer, panic - panic(err) - } - - // Now compare every field instead of using reflect.DeepEqual for the - // Addresses field. - var shouldPanic bool - first := msg.(*lnwire.NodeAnnouncement) - second := newMsg.(*lnwire.NodeAnnouncement) - if !bytes.Equal(first.Signature[:], second.Signature[:]) { - shouldPanic = true - } - - if !reflect.DeepEqual(first.Features, second.Features) { - shouldPanic = true - } - - if first.Timestamp != second.Timestamp { - shouldPanic = true - } - - if !bytes.Equal(first.NodeID[:], second.NodeID[:]) { - shouldPanic = true - } - - if !reflect.DeepEqual(first.RGBColor, second.RGBColor) { - shouldPanic = true - } - - if !bytes.Equal(first.Alias[:], second.Alias[:]) { - shouldPanic = true - } - - if len(first.Addresses) != len(second.Addresses) { - shouldPanic = true - } - - for i := range first.Addresses { - if first.Addresses[i].String() != second.Addresses[i].String() { - shouldPanic = true - break - } - } - - if !reflect.DeepEqual(first.ExtraOpaqueData, second.ExtraOpaqueData) { - shouldPanic = true - } - - if shouldPanic { - panic("original message and deserialized message are not equal") - } - - // Add this input to the corpus. - return 1 -} diff --git a/fuzz/lnwire/open_channel.go b/fuzz/lnwire/open_channel.go deleted file mode 100644 index c7ab024a9..000000000 --- a/fuzz/lnwire/open_channel.go +++ /dev/null @@ -1,142 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "bytes" - - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_open_channel is used by go-fuzz. -func Fuzz_open_channel(data []byte) int { - // Prefix with MsgOpenChannel. - data = prefixWithMsgType(data, lnwire.MsgOpenChannel) - - // We have to do this here instead of in fuzz.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 > lnwire.MaxMsgBody { - return 1 - } - - msg, err := lnwire.ReadMessage(r, 0) - if err != nil { - return 1 - } - - // We will serialize the message into a new bytes buffer. - var b bytes.Buffer - if _, err := lnwire.WriteMessage(&b, msg, 0); err != nil { - // Could not serialize message into bytes buffer, panic - panic(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 := lnwire.ReadMessage(&b, 0) - if err != nil { - // Could not deserialize message from bytes buffer, panic - panic(err) - } - - // Now compare every field instead of using reflect.DeepEqual. - // For UpfrontShutdownScript, we only compare bytes. This probably takes - // up more branches than necessary, but that's fine for now. - var shouldPanic bool - first := msg.(*lnwire.OpenChannel) - second := newMsg.(*lnwire.OpenChannel) - - if !first.ChainHash.IsEqual(&second.ChainHash) { - shouldPanic = true - } - - if !bytes.Equal(first.PendingChannelID[:], second.PendingChannelID[:]) { - shouldPanic = true - } - - if first.FundingAmount != second.FundingAmount { - shouldPanic = true - } - - if first.PushAmount != second.PushAmount { - shouldPanic = true - } - - if first.DustLimit != second.DustLimit { - shouldPanic = true - } - - if first.MaxValueInFlight != second.MaxValueInFlight { - shouldPanic = true - } - - if first.ChannelReserve != second.ChannelReserve { - shouldPanic = true - } - - if first.HtlcMinimum != second.HtlcMinimum { - shouldPanic = true - } - - if first.FeePerKiloWeight != second.FeePerKiloWeight { - shouldPanic = true - } - - if first.CsvDelay != second.CsvDelay { - shouldPanic = true - } - - if first.MaxAcceptedHTLCs != second.MaxAcceptedHTLCs { - shouldPanic = true - } - - if !first.FundingKey.IsEqual(second.FundingKey) { - shouldPanic = true - } - - if !first.RevocationPoint.IsEqual(second.RevocationPoint) { - shouldPanic = true - } - - if !first.PaymentPoint.IsEqual(second.PaymentPoint) { - shouldPanic = true - } - - if !first.DelayedPaymentPoint.IsEqual(second.DelayedPaymentPoint) { - shouldPanic = true - } - - if !first.HtlcPoint.IsEqual(second.HtlcPoint) { - shouldPanic = true - } - - if !first.FirstCommitmentPoint.IsEqual(second.FirstCommitmentPoint) { - shouldPanic = true - } - - if first.ChannelFlags != second.ChannelFlags { - shouldPanic = true - } - - if !bytes.Equal(first.UpfrontShutdownScript, second.UpfrontShutdownScript) { - shouldPanic = true - } - - if shouldPanic { - panic("original message and deserialized message are not equal") - } - - // Add this input to the corpus. - return 1 -} diff --git a/fuzz/lnwire/ping.go b/fuzz/lnwire/ping.go deleted file mode 100644 index 2147dc8a6..000000000 --- a/fuzz/lnwire/ping.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_ping is used by go-fuzz. -func Fuzz_ping(data []byte) int { - // Prefix with MsgPing. - data = prefixWithMsgType(data, lnwire.MsgPing) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/pong.go b/fuzz/lnwire/pong.go deleted file mode 100644 index bc8b1a174..000000000 --- a/fuzz/lnwire/pong.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_pong is used by go-fuzz. -func Fuzz_pong(data []byte) int { - // Prefix with MsgPong. - data = prefixWithMsgType(data, lnwire.MsgPong) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/query_channel_range.go b/fuzz/lnwire/query_channel_range.go deleted file mode 100644 index c8bdbc2db..000000000 --- a/fuzz/lnwire/query_channel_range.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_query_channel_range is used by go-fuzz. -func Fuzz_query_channel_range(data []byte) int { - // Prefix with MsgQueryChannelRange. - data = prefixWithMsgType(data, lnwire.MsgQueryChannelRange) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/query_short_chan_ids.go b/fuzz/lnwire/query_short_chan_ids.go deleted file mode 100644 index 8d4e99368..000000000 --- a/fuzz/lnwire/query_short_chan_ids.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_query_short_chan_ids is used by go-fuzz. -func Fuzz_query_short_chan_ids(data []byte) int { - // Prefix with MsgQueryShortChanIDs. - data = prefixWithMsgType(data, lnwire.MsgQueryShortChanIDs) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/query_short_chan_ids_zlib.go b/fuzz/lnwire/query_short_chan_ids_zlib.go deleted file mode 100644 index eaf1e1f77..000000000 --- a/fuzz/lnwire/query_short_chan_ids_zlib.go +++ /dev/null @@ -1,48 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "bytes" - "compress/zlib" - "encoding/binary" - - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_query_short_chan_ids_zlib is used by go-fuzz. -func Fuzz_query_short_chan_ids_zlib(data []byte) int { - - var buf bytes.Buffer - zlibWriter := zlib.NewWriter(&buf) - _, err := zlibWriter.Write(data) - if err != nil { - // Zlib bug? - panic(err) - } - - if err := zlibWriter.Close(); err != nil { - // Zlib bug? - panic(err) - } - - compressedPayload := buf.Bytes() - - chainhash := []byte("00000000000000000000000000000000") - numBytesInBody := len(compressedPayload) + 1 - zlibByte := []byte("\x01") - - bodyBytes := make([]byte, 2) - binary.BigEndian.PutUint16(bodyBytes, uint16(numBytesInBody)) - - payload := append(chainhash, bodyBytes...) - payload = append(payload, zlibByte...) - payload = append(payload, compressedPayload...) - - // Prefix with MsgQueryShortChanIDs. - payload = prefixWithMsgType(payload, lnwire.MsgQueryShortChanIDs) - - // Pass the message into our general fuzz harness for wire messages! - return harness(payload) -} diff --git a/fuzz/lnwire/reply_channel_range.go b/fuzz/lnwire/reply_channel_range.go deleted file mode 100644 index e1611aa77..000000000 --- a/fuzz/lnwire/reply_channel_range.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_reply_channel_range is used by go-fuzz. -func Fuzz_reply_channel_range(data []byte) int { - // Prefix with MsgReplyChannelRange. - data = prefixWithMsgType(data, lnwire.MsgReplyChannelRange) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/reply_channel_range_zlib.go b/fuzz/lnwire/reply_channel_range_zlib.go deleted file mode 100644 index fe2517179..000000000 --- a/fuzz/lnwire/reply_channel_range_zlib.go +++ /dev/null @@ -1,56 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "bytes" - "compress/zlib" - "encoding/binary" - - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_reply_channel_range_zlib is used by go-fuzz. -func Fuzz_reply_channel_range_zlib(data []byte) int { - - var buf bytes.Buffer - zlibWriter := zlib.NewWriter(&buf) - _, err := zlibWriter.Write(data) - if err != nil { - // Zlib bug? - panic(err) - } - - if err := zlibWriter.Close(); err != nil { - // Zlib bug? - panic(err) - } - - compressedPayload := buf.Bytes() - - // Initialize some []byte vars which will prefix our payload - chainhash := []byte("00000000000000000000000000000000") - firstBlockHeight := []byte("\x00\x00\x00\x00") - numBlocks := []byte("\x00\x00\x00\x00") - completeByte := []byte("\x00") - - numBytesInBody := len(compressedPayload) + 1 - zlibByte := []byte("\x01") - - bodyBytes := make([]byte, 2) - binary.BigEndian.PutUint16(bodyBytes, uint16(numBytesInBody)) - - payload := append(chainhash, firstBlockHeight...) - payload = append(payload, numBlocks...) - payload = append(payload, completeByte...) - payload = append(payload, bodyBytes...) - payload = append(payload, zlibByte...) - payload = append(payload, compressedPayload...) - - // Prefix with MsgReplyChannelRange. - payload = prefixWithMsgType(payload, lnwire.MsgReplyChannelRange) - - // Pass the message into our general fuzz harness for wire messages! - return harness(payload) -} diff --git a/fuzz/lnwire/reply_short_chan_ids_end.go b/fuzz/lnwire/reply_short_chan_ids_end.go deleted file mode 100644 index b91f3b548..000000000 --- a/fuzz/lnwire/reply_short_chan_ids_end.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_reply_short_chan_ids_end is used by go-fuzz. -func Fuzz_reply_short_chan_ids_end(data []byte) int { - // Prefix with MsgReplyShortChanIDsEnd. - data = prefixWithMsgType(data, lnwire.MsgReplyShortChanIDsEnd) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/revoke_and_ack.go b/fuzz/lnwire/revoke_and_ack.go deleted file mode 100644 index 276ba7c21..000000000 --- a/fuzz/lnwire/revoke_and_ack.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_revoke_and_ack is used by go-fuzz. -func Fuzz_revoke_and_ack(data []byte) int { - // Prefix with MsgRevokeAndAck. - data = prefixWithMsgType(data, lnwire.MsgRevokeAndAck) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/shutdown.go b/fuzz/lnwire/shutdown.go deleted file mode 100644 index 84e40cc7c..000000000 --- a/fuzz/lnwire/shutdown.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_shutdown is used by go-fuzz. -func Fuzz_shutdown(data []byte) int { - // Prefix with MsgShutdown. - data = prefixWithMsgType(data, lnwire.MsgShutdown) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/update_add_htlc.go b/fuzz/lnwire/update_add_htlc.go deleted file mode 100644 index 3ec84ff9b..000000000 --- a/fuzz/lnwire/update_add_htlc.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_update_add_htlc is used by go-fuzz. -func Fuzz_update_add_htlc(data []byte) int { - // Prefix with MsgUpdateAddHTLC. - data = prefixWithMsgType(data, lnwire.MsgUpdateAddHTLC) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/update_fail_htlc.go b/fuzz/lnwire/update_fail_htlc.go deleted file mode 100644 index 77b4bac8e..000000000 --- a/fuzz/lnwire/update_fail_htlc.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_update_fail_htlc is used by go-fuzz. -func Fuzz_update_fail_htlc(data []byte) int { - // Prefix with MsgUpdateFailHTLC. - data = prefixWithMsgType(data, lnwire.MsgUpdateFailHTLC) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/update_fail_malformed_htlc.go b/fuzz/lnwire/update_fail_malformed_htlc.go deleted file mode 100644 index 38ddfdf18..000000000 --- a/fuzz/lnwire/update_fail_malformed_htlc.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_update_fail_malformed_htlc is used by go-fuzz. -func Fuzz_update_fail_malformed_htlc(data []byte) int { - // Prefix with MsgUpdateFailMalformedHTLC. - data = prefixWithMsgType(data, lnwire.MsgUpdateFailMalformedHTLC) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/update_fee.go b/fuzz/lnwire/update_fee.go deleted file mode 100644 index 228d12fdb..000000000 --- a/fuzz/lnwire/update_fee.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_update_fee is used by go-fuzz. -func Fuzz_update_fee(data []byte) int { - // Prefix with MsgUpdateFee. - data = prefixWithMsgType(data, lnwire.MsgUpdateFee) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/fuzz/lnwire/update_fulfill_htlc.go b/fuzz/lnwire/update_fulfill_htlc.go deleted file mode 100644 index 241f37c74..000000000 --- a/fuzz/lnwire/update_fulfill_htlc.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package lnwirefuzz - -import ( - "github.com/lightningnetwork/lnd/lnwire" -) - -// Fuzz_update_fulfill_htlc is used by go-fuzz. -func Fuzz_update_fulfill_htlc(data []byte) int { - // Prefix with MsgUpdateFulfillHTLC. - data = prefixWithMsgType(data, lnwire.MsgUpdateFulfillHTLC) - - // Pass the message into our general fuzz harness for wire messages! - return harness(data) -} diff --git a/lnwire/fuzz_test.go b/lnwire/fuzz_test.go new file mode 100644 index 000000000..bbb48056a --- /dev/null +++ b/lnwire/fuzz_test.go @@ -0,0 +1,752 @@ +package lnwire + +import ( + "bytes" + "compress/zlib" + "encoding/binary" + "reflect" + "testing" +) + +// prefixWithMsgType takes []byte and adds a wire protocol prefix +// to make the []byte into an actual message to be used in fuzzing. +func prefixWithMsgType(data []byte, prefix MessageType) []byte { + var prefixBytes [2]byte + binary.BigEndian.PutUint16(prefixBytes[:], uint16(prefix)) + data = append(prefixBytes[:], data...) + 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 +// serialization and deserialization checks. +func harness(t *testing.T, data []byte) { + // Create a reader with the byte array. + r := bytes.NewReader(data) + + // Check that the created message is not greater than the maximum + // message size. + if len(data) > MaxSliceLength { + return + } + + msg, err := ReadMessage(r, 0) + if err != nil { + return + } + + // We will serialize the message into a new bytes buffer. + var b bytes.Buffer + if _, err := WriteMessage(&b, msg, 0); err != nil { + // Could not serialize message into bytes buffer, panic + t.Fatal(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) + if err != nil { + // Could not deserialize message from bytes buffer, panic + t.Fatal(err) + } + + if !reflect.DeepEqual(msg, newMsg) { + // Deserialized message and original message are not deeply + // equal. + t.Fatal("original message and deserialized message are not deeply equal") + } +} + +func Fuzz_accept_channel(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 + if _, err := WriteMessage(&b, msg, 0); err != nil { + // Could not serialize message into bytes buffer, panic + t.Fatal(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) + if err != nil { + // Could not deserialize message from bytes buffer, + // panic + t.Fatal(err) + } + + // Now compare every field instead of using reflect.DeepEqual. + // For UpfrontShutdownScript, we only compare bytes. This + // probably takes up more branches than necessary, but that's + // fine for now. + var shouldPanic bool + first := msg.(*AcceptChannel) + second := newMsg.(*AcceptChannel) + + if !bytes.Equal(first.PendingChannelID[:], second.PendingChannelID[:]) { + shouldPanic = true + } + + if first.DustLimit != second.DustLimit { + shouldPanic = true + } + + if first.MaxValueInFlight != second.MaxValueInFlight { + shouldPanic = true + } + + if first.ChannelReserve != second.ChannelReserve { + shouldPanic = true + } + + if first.HtlcMinimum != second.HtlcMinimum { + shouldPanic = true + } + + if first.MinAcceptDepth != second.MinAcceptDepth { + shouldPanic = true + } + + if first.CsvDelay != second.CsvDelay { + shouldPanic = true + } + + if first.MaxAcceptedHTLCs != second.MaxAcceptedHTLCs { + shouldPanic = true + } + + if !first.FundingKey.IsEqual(second.FundingKey) { + shouldPanic = true + } + + if !first.RevocationPoint.IsEqual(second.RevocationPoint) { + shouldPanic = true + } + + if !first.PaymentPoint.IsEqual(second.PaymentPoint) { + shouldPanic = true + } + + if !first.DelayedPaymentPoint.IsEqual(second.DelayedPaymentPoint) { + shouldPanic = true + } + + if !first.HtlcPoint.IsEqual(second.HtlcPoint) { + shouldPanic = true + } + + if !first.FirstCommitmentPoint.IsEqual(second.FirstCommitmentPoint) { + shouldPanic = true + } + + if !bytes.Equal(first.UpfrontShutdownScript, second.UpfrontShutdownScript) { + shouldPanic = true + } + + if shouldPanic { + t.Fatalf("original message and deseralized message are not equal") + } + }) +} + +func Fuzz_announce_signatures(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_channel_announcement(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_channel_reestablish(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_channel_update(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_closing_signed(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_commit_sig(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_error(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_funding_created(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_funding_locked(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Prefix with MsgFundingLocked. + data = prefixWithMsgType(data, MsgFundingLocked) + + // Pass the message into our general fuzz harness for wire + // messages! + harness(t, data) + }) +} + +func Fuzz_funding_signed(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_gossip_timestamp_range(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_init(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_node_announcement(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 + // 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 + if _, err := WriteMessage(&b, msg, 0); err != nil { + // Could not serialize message into bytes buffer, panic + t.Fatal(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) + if err != nil { + // Could not deserialize message from bytes buffer, + // panic + t.Fatal(err) + } + + // Now compare every field instead of using reflect.DeepEqual + // for the Addresses field. + var shouldPanic bool + first := msg.(*NodeAnnouncement) + second := newMsg.(*NodeAnnouncement) + if !bytes.Equal(first.Signature[:], second.Signature[:]) { + shouldPanic = true + } + + if !reflect.DeepEqual(first.Features, second.Features) { + shouldPanic = true + } + + if first.Timestamp != second.Timestamp { + shouldPanic = true + } + + if !bytes.Equal(first.NodeID[:], second.NodeID[:]) { + shouldPanic = true + } + + if !reflect.DeepEqual(first.RGBColor, second.RGBColor) { + shouldPanic = true + } + + if !bytes.Equal(first.Alias[:], second.Alias[:]) { + shouldPanic = true + } + + if len(first.Addresses) != len(second.Addresses) { + shouldPanic = true + } + + for i := range first.Addresses { + if first.Addresses[i].String() != second.Addresses[i].String() { + shouldPanic = true + break + } + } + + if !reflect.DeepEqual(first.ExtraOpaqueData, second.ExtraOpaqueData) { + shouldPanic = true + } + + if shouldPanic { + t.Fatal("original message and deserialized message are not equal") + } + }) +} + +func Fuzz_open_channel(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 + // 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 + if _, err := WriteMessage(&b, msg, 0); err != nil { + // Could not serialize message into bytes buffer, panic + t.Fatal(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) + if err != nil { + // Could not deserialize message from bytes buffer, + // panic + t.Fatal(err) + } + + // Now compare every field instead of using reflect.DeepEqual. + // For UpfrontShutdownScript, we only compare bytes. This + // probably takes up more branches than necessary, but that's + // fine for now. + var shouldPanic bool + first := msg.(*OpenChannel) + second := newMsg.(*OpenChannel) + + if !first.ChainHash.IsEqual(&second.ChainHash) { + shouldPanic = true + } + + if !bytes.Equal(first.PendingChannelID[:], second.PendingChannelID[:]) { + shouldPanic = true + } + + if first.FundingAmount != second.FundingAmount { + shouldPanic = true + } + + if first.PushAmount != second.PushAmount { + shouldPanic = true + } + + if first.DustLimit != second.DustLimit { + shouldPanic = true + } + + if first.MaxValueInFlight != second.MaxValueInFlight { + shouldPanic = true + } + + if first.ChannelReserve != second.ChannelReserve { + shouldPanic = true + } + + if first.HtlcMinimum != second.HtlcMinimum { + shouldPanic = true + } + + if first.FeePerKiloWeight != second.FeePerKiloWeight { + shouldPanic = true + } + + if first.CsvDelay != second.CsvDelay { + shouldPanic = true + } + + if first.MaxAcceptedHTLCs != second.MaxAcceptedHTLCs { + shouldPanic = true + } + + if !first.FundingKey.IsEqual(second.FundingKey) { + shouldPanic = true + } + + if !first.RevocationPoint.IsEqual(second.RevocationPoint) { + shouldPanic = true + } + + if !first.PaymentPoint.IsEqual(second.PaymentPoint) { + shouldPanic = true + } + + if !first.DelayedPaymentPoint.IsEqual(second.DelayedPaymentPoint) { + shouldPanic = true + } + + if !first.HtlcPoint.IsEqual(second.HtlcPoint) { + shouldPanic = true + } + + if !first.FirstCommitmentPoint.IsEqual(second.FirstCommitmentPoint) { + shouldPanic = true + } + + if first.ChannelFlags != second.ChannelFlags { + shouldPanic = true + } + + if !bytes.Equal(first.UpfrontShutdownScript, second.UpfrontShutdownScript) { + shouldPanic = true + } + + if shouldPanic { + t.Fatal("original message and deserialized message are not equal") + } + }) +} + +func Fuzz_ping(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_pong(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_query_channel_range(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_zlib_query_short_chan_ids(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + var buf bytes.Buffer + zlibWriter := zlib.NewWriter(&buf) + _, err := zlibWriter.Write(data) + if err != nil { + // Zlib bug? + t.Fatal(err) + } + + if err := zlibWriter.Close(); err != nil { + // Zlib bug? + t.Fatal(err) + } + + compressedPayload := buf.Bytes() + + chainhash := []byte("00000000000000000000000000000000") + numBytesInBody := len(compressedPayload) + 1 + zlibByte := []byte("\x01") + + bodyBytes := make([]byte, 2) + binary.BigEndian.PutUint16(bodyBytes, uint16(numBytesInBody)) + + payload := chainhash + payload = append(payload, bodyBytes...) + 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 + // messages! + harness(t, payload) + }) +} + +func Fuzz_query_short_chan_ids(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_zlib_reply_channel_range(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + var buf bytes.Buffer + zlibWriter := zlib.NewWriter(&buf) + _, err := zlibWriter.Write(data) + if err != nil { + // Zlib bug? + t.Fatal(err) + } + + if err := zlibWriter.Close(); err != nil { + // Zlib bug? + t.Fatal(err) + } + + compressedPayload := buf.Bytes() + + // Initialize some []byte vars which will prefix our payload + chainhash := []byte("00000000000000000000000000000000") + firstBlockHeight := []byte("\x00\x00\x00\x00") + numBlocks := []byte("\x00\x00\x00\x00") + completeByte := []byte("\x00") + + numBytesInBody := len(compressedPayload) + 1 + zlibByte := []byte("\x01") + + bodyBytes := make([]byte, 2) + binary.BigEndian.PutUint16(bodyBytes, uint16(numBytesInBody)) + + payload := chainhash + payload = append(payload, firstBlockHeight...) + payload = append(payload, numBlocks...) + payload = append(payload, completeByte...) + payload = append(payload, bodyBytes...) + 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 + // messages! + harness(t, payload) + }) +} + +func Fuzz_reply_channel_range(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Prefix with MsgReplyChannelRange. + data = prefixWithMsgType(data, MsgReplyChannelRange) + + // Pass the message into our general fuzz harness for wire + // messages! + harness(t, data) + }) +} + +func Fuzz_reply_short_chan_ids_end(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_revoke_and_ack(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_shutdown(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_update_add_htlc(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_update_fail_htlc(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_update_fail_malformed_htlc(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_update_fee(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 + // messages! + harness(t, data) + }) +} + +func Fuzz_update_fulfill_htlc(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 + // messages! + harness(t, data) + }) +} From 1ed2a1bc283a746e9da40fe76d1760ef2c407efa Mon Sep 17 00:00:00 2001 From: Conner Date: Fri, 29 Jul 2022 18:19:01 -0400 Subject: [PATCH 2/7] wtwire: migrate fuzz tests --- fuzz/wtwire/create_session.go | 21 ---- fuzz/wtwire/create_session_reply.go | 21 ---- fuzz/wtwire/delete_session.go | 21 ---- fuzz/wtwire/delete_session_reply.go | 21 ---- fuzz/wtwire/error.go | 21 ---- fuzz/wtwire/fuzz_utils.go | 76 ------------ fuzz/wtwire/init.go | 21 ---- fuzz/wtwire/state_update.go | 21 ---- fuzz/wtwire/state_update_reply.go | 21 ---- watchtower/wtwire/fuzz_test.go | 183 ++++++++++++++++++++++++++++ 10 files changed, 183 insertions(+), 244 deletions(-) delete mode 100644 fuzz/wtwire/create_session.go delete mode 100644 fuzz/wtwire/create_session_reply.go delete mode 100644 fuzz/wtwire/delete_session.go delete mode 100644 fuzz/wtwire/delete_session_reply.go delete mode 100644 fuzz/wtwire/error.go delete mode 100644 fuzz/wtwire/fuzz_utils.go delete mode 100644 fuzz/wtwire/init.go delete mode 100644 fuzz/wtwire/state_update.go delete mode 100644 fuzz/wtwire/state_update_reply.go create mode 100644 watchtower/wtwire/fuzz_test.go diff --git a/fuzz/wtwire/create_session.go b/fuzz/wtwire/create_session.go deleted file mode 100644 index b2bd3e705..000000000 --- a/fuzz/wtwire/create_session.go +++ /dev/null @@ -1,21 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package wtwirefuzz - -import ( - "github.com/lightningnetwork/lnd/watchtower/wtwire" -) - -// Fuzz_create_session is used by go-fuzz. -func Fuzz_create_session(data []byte) int { - // Prefix with MsgCreateSession. - data = prefixWithMsgType(data, wtwire.MsgCreateSession) - - // Create an empty message so that the FuzzHarness func can check if the - // max payload constraint is violated. - emptyMsg := wtwire.CreateSession{} - - // Pass the message into our general fuzz harness for wire messages! - return harness(data, &emptyMsg) -} diff --git a/fuzz/wtwire/create_session_reply.go b/fuzz/wtwire/create_session_reply.go deleted file mode 100644 index e862e3e36..000000000 --- a/fuzz/wtwire/create_session_reply.go +++ /dev/null @@ -1,21 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package wtwirefuzz - -import ( - "github.com/lightningnetwork/lnd/watchtower/wtwire" -) - -// Fuzz_create_session_reply is used by go-fuzz. -func Fuzz_create_session_reply(data []byte) int { - // Prefix with MsgCreateSessionReply. - data = prefixWithMsgType(data, wtwire.MsgCreateSessionReply) - - // Create an empty message so that the FuzzHarness func can check if the - // max payload constraint is violated. - emptyMsg := wtwire.CreateSessionReply{} - - // Pass the message into our general fuzz harness for wire messages! - return harness(data, &emptyMsg) -} diff --git a/fuzz/wtwire/delete_session.go b/fuzz/wtwire/delete_session.go deleted file mode 100644 index cca278093..000000000 --- a/fuzz/wtwire/delete_session.go +++ /dev/null @@ -1,21 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package wtwirefuzz - -import ( - "github.com/lightningnetwork/lnd/watchtower/wtwire" -) - -// Fuzz_delete_session is used by go-fuzz. -func Fuzz_delete_session(data []byte) int { - // Prefix with MsgDeleteSession. - data = prefixWithMsgType(data, wtwire.MsgDeleteSession) - - // Create an empty message so that the FuzzHarness func can check if the - // max payload constraint is violated. - emptyMsg := wtwire.DeleteSession{} - - // Pass the message into our general fuzz harness for wire messages! - return harness(data, &emptyMsg) -} diff --git a/fuzz/wtwire/delete_session_reply.go b/fuzz/wtwire/delete_session_reply.go deleted file mode 100644 index ec890aa11..000000000 --- a/fuzz/wtwire/delete_session_reply.go +++ /dev/null @@ -1,21 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package wtwirefuzz - -import ( - "github.com/lightningnetwork/lnd/watchtower/wtwire" -) - -// Fuzz_delete_session_reply is used by go-fuzz. -func Fuzz_delete_session_reply(data []byte) int { - // Prefix with MsgDeleteSessionReply. - data = prefixWithMsgType(data, wtwire.MsgDeleteSessionReply) - - // Create an empty message so that the FuzzHarness func can check if the - // max payload constraint is violated. - emptyMsg := wtwire.DeleteSessionReply{} - - // Pass the message into our general fuzz harness for wire messages! - return harness(data, &emptyMsg) -} diff --git a/fuzz/wtwire/error.go b/fuzz/wtwire/error.go deleted file mode 100644 index 7d73e1f30..000000000 --- a/fuzz/wtwire/error.go +++ /dev/null @@ -1,21 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package wtwirefuzz - -import ( - "github.com/lightningnetwork/lnd/watchtower/wtwire" -) - -// Fuzz_error is used by go-fuzz. -func Fuzz_error(data []byte) int { - // Prefix with MsgError. - data = prefixWithMsgType(data, wtwire.MsgError) - - // Create an empty message so that the FuzzHarness func can check if the - // max payload constraint is violated. - emptyMsg := wtwire.Error{} - - // Pass the message into our general fuzz harness for wire messages! - return harness(data, &emptyMsg) -} diff --git a/fuzz/wtwire/fuzz_utils.go b/fuzz/wtwire/fuzz_utils.go deleted file mode 100644 index 06a00a2de..000000000 --- a/fuzz/wtwire/fuzz_utils.go +++ /dev/null @@ -1,76 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package wtwirefuzz - -import ( - "bytes" - "encoding/binary" - "fmt" - "reflect" - - "github.com/lightningnetwork/lnd/watchtower/wtwire" -) - -// prefixWithMsgType takes []byte and adds a wire protocol prefix -// to make the []byte into an actual message to be used in fuzzing. -func prefixWithMsgType(data []byte, prefix wtwire.MessageType) []byte { - var prefixBytes [2]byte - binary.BigEndian.PutUint16(prefixBytes[:], uint16(prefix)) - data = append(prefixBytes[:], data...) - 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 serialization -// and deserialization checks. Returns an int that determines whether the input -// is unique or not. -func harness(data []byte, emptyMsg wtwire.Message) int { - // 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. We check this because - // otherwise `go-fuzz` will keep creating inputs that crash on ReadMessage - // due to a large message size. - payloadLen := uint32(len(data)) - 2 - if payloadLen > emptyMsg.MaxPayloadLength(0) { - // Ignore this input - max payload constraint violated. - return 1 - } - - msg, err := wtwire.ReadMessage(r, 0) - if err != nil { - // go-fuzz generated []byte that cannot be represented as a - // wire message but we will return 0 so go-fuzz can modify the - // input. - return 1 - } - - // We will serialize the message into a new bytes buffer. - var b bytes.Buffer - if _, err := wtwire.WriteMessage(&b, msg, 0); err != nil { - // Could not serialize message into bytes buffer, panic. - panic(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 := wtwire.ReadMessage(&b, 0) - if err != nil { - // Could not deserialize message from bytes buffer, panic. - panic(err) - } - - if !reflect.DeepEqual(msg, newMsg) { - // Deserialized message and original message are not - // deeply equal. - panic(fmt.Errorf("deserialized message and original message " + - "are not deeply equal.")) - } - - // Add this input to the corpus. - return 1 -} diff --git a/fuzz/wtwire/init.go b/fuzz/wtwire/init.go deleted file mode 100644 index ea157a646..000000000 --- a/fuzz/wtwire/init.go +++ /dev/null @@ -1,21 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package wtwirefuzz - -import ( - "github.com/lightningnetwork/lnd/watchtower/wtwire" -) - -// Fuzz_init is used by go-fuzz. -func Fuzz_init(data []byte) int { - // Prefix with MsgInit. - data = prefixWithMsgType(data, wtwire.MsgInit) - - // Create an empty message so that the FuzzHarness func can check if the - // max payload constraint is violated. - emptyMsg := wtwire.Init{} - - // Pass the message into our general fuzz harness for wire messages! - return harness(data, &emptyMsg) -} diff --git a/fuzz/wtwire/state_update.go b/fuzz/wtwire/state_update.go deleted file mode 100644 index 82323e146..000000000 --- a/fuzz/wtwire/state_update.go +++ /dev/null @@ -1,21 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package wtwirefuzz - -import ( - "github.com/lightningnetwork/lnd/watchtower/wtwire" -) - -// Fuzz_state_update is used by go-fuzz. -func Fuzz_state_update(data []byte) int { - // Prefix with MsgStateUpdate. - data = prefixWithMsgType(data, wtwire.MsgStateUpdate) - - // Create an empty message so that the FuzzHarness func can check if the - // max payload constraint is violated. - emptyMsg := wtwire.StateUpdate{} - - // Pass the message into our general fuzz harness for wire messages! - return harness(data, &emptyMsg) -} diff --git a/fuzz/wtwire/state_update_reply.go b/fuzz/wtwire/state_update_reply.go deleted file mode 100644 index c645031d4..000000000 --- a/fuzz/wtwire/state_update_reply.go +++ /dev/null @@ -1,21 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package wtwirefuzz - -import ( - "github.com/lightningnetwork/lnd/watchtower/wtwire" -) - -// Fuzz_state_update_reply is used by go-fuzz. -func Fuzz_state_update_reply(data []byte) int { - // Prefix with MsgStateUpdateReply. - data = prefixWithMsgType(data, wtwire.MsgStateUpdateReply) - - // Create an empty message so that the FuzzHarness func can check if the - // max payload constraint is violated. - emptyMsg := wtwire.StateUpdateReply{} - - // Pass the message into our general fuzz harness for wire messages! - return harness(data, &emptyMsg) -} diff --git a/watchtower/wtwire/fuzz_test.go b/watchtower/wtwire/fuzz_test.go new file mode 100644 index 000000000..7a6004013 --- /dev/null +++ b/watchtower/wtwire/fuzz_test.go @@ -0,0 +1,183 @@ +package wtwire + +import ( + "bytes" + "encoding/binary" + "reflect" + "testing" +) + +// prefixWithMsgType takes []byte and adds a wire protocol prefix +// to make the []byte into an actual message to be used in fuzzing. +func prefixWithMsgType(data []byte, prefix MessageType) []byte { + var prefixBytes [2]byte + binary.BigEndian.PutUint16(prefixBytes[:], uint16(prefix)) + data = append(prefixBytes[:], data...) + 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 +// serialization and deserialization checks. Returns an int that determines +// whether the input is unique or not. +func harness(t *testing.T, data []byte, emptyMsg Message) { + // 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 > emptyMsg.MaxPayloadLength(0) { + // Ignore this input - max payload constraint violated. + return + } + + msg, err := ReadMessage(r, 0) + if err != nil { + return + } + + // We will serialize the message into a new bytes buffer. + var b bytes.Buffer + if _, err := WriteMessage(&b, msg, 0); err != nil { + // Could not serialize message into bytes buffer, panic. + t.Fatal(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) + if err != nil { + // Could not deserialize message from bytes buffer, panic. + t.Fatal(err) + } + + if !reflect.DeepEqual(msg, newMsg) { + // Deserialized message and original message are not + // deeply equal. + t.Fatal("deserialized message and original message " + + "are not deeply equal.") + } +} + +func Fuzz_create_session_reply(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Prefix with MsgCreateSessionReply. + data = prefixWithMsgType(data, MsgCreateSessionReply) + + // Create an empty message so that the FuzzHarness func can + // check if the max payload constraint is violated. + emptyMsg := CreateSessionReply{} + + // Pass the message into our general fuzz harness for wire + // messages! + harness(t, data, &emptyMsg) + }) +} + +func Fuzz_create_session(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Prefix with MsgCreateSession. + data = prefixWithMsgType(data, MsgCreateSession) + + // Create an empty message so that the FuzzHarness func can + // check if the max payload constraint is violated. + emptyMsg := CreateSession{} + + // Pass the message into our general fuzz harness for wire + // messages! + harness(t, data, &emptyMsg) + }) +} + +func Fuzz_delete_session_reply(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Prefix with MsgDeleteSessionReply. + data = prefixWithMsgType(data, MsgDeleteSessionReply) + + // Create an empty message so that the FuzzHarness func can + // check if the max payload constraint is violated. + emptyMsg := DeleteSessionReply{} + + // Pass the message into our general fuzz harness for wire + // messages! + harness(t, data, &emptyMsg) + }) +} + +func Fuzz_delete_session(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Prefix with MsgDeleteSession. + data = prefixWithMsgType(data, MsgDeleteSession) + + // Create an empty message so that the FuzzHarness func can + // check if the max payload constraint is violated. + emptyMsg := DeleteSession{} + + // Pass the message into our general fuzz harness for wire + // messages! + harness(t, data, &emptyMsg) + }) +} + +func Fuzz_error(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Prefix with MsgError. + data = prefixWithMsgType(data, MsgError) + + // Create an empty message so that the FuzzHarness func can + // check if the max payload constraint is violated. + emptyMsg := Error{} + + // Pass the message into our general fuzz harness for wire + // messages! + harness(t, data, &emptyMsg) + }) +} + +func Fuzz_init(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Prefix with MsgInit. + data = prefixWithMsgType(data, MsgInit) + + // Create an empty message so that the FuzzHarness func can + // check if the max payload constraint is violated. + emptyMsg := Init{} + + // Pass the message into our general fuzz harness for wire + // messages! + harness(t, data, &emptyMsg) + }) +} + +func Fuzz_state_update_reply(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Prefix with MsgStateUpdateReply. + data = prefixWithMsgType(data, MsgStateUpdateReply) + + // Create an empty message so that the FuzzHarness func can + // check if the max payload constraint is violated. + emptyMsg := StateUpdateReply{} + + // Pass the message into our general fuzz harness for wire + // messages! + harness(t, data, &emptyMsg) + }) +} + +func Fuzz_state_update(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Prefix with MsgStateUpdate. + data = prefixWithMsgType(data, MsgStateUpdate) + + // Create an empty message so that the FuzzHarness func can + // check if the max payload constraint is violated. + emptyMsg := StateUpdate{} + + // Pass the message into our general fuzz harness for wire + // messages! + harness(t, data, &emptyMsg) + }) +} From 782f9732986e20a490f79da86ef4bc884c97c4fc Mon Sep 17 00:00:00 2001 From: Conner Date: Fri, 29 Jul 2022 18:32:59 -0400 Subject: [PATCH 3/7] zpay32: migrate fuzz tests --- fuzz/zpay32/decode.go | 23 ---------------- fuzz/zpay32/encode.go | 52 ----------------------------------- zpay32/fuzz_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 75 deletions(-) delete mode 100644 fuzz/zpay32/decode.go delete mode 100644 fuzz/zpay32/encode.go create mode 100644 zpay32/fuzz_test.go diff --git a/fuzz/zpay32/decode.go b/fuzz/zpay32/decode.go deleted file mode 100644 index a5573e208..000000000 --- a/fuzz/zpay32/decode.go +++ /dev/null @@ -1,23 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package zpay32fuzz - -import ( - "github.com/btcsuite/btcd/chaincfg" - "github.com/lightningnetwork/lnd/zpay32" -) - -// Fuzz_decode is used by go-fuzz. -func Fuzz_decode(data []byte) int { - inv, err := zpay32.Decode(string(data), &chaincfg.TestNet3Params) - if err != nil { - return 1 - } - - // Call these functions as a sanity check to make sure the invoice - // is well-formed. - _ = inv.MinFinalCLTVExpiry() - _ = inv.Expiry() - return 1 -} diff --git a/fuzz/zpay32/encode.go b/fuzz/zpay32/encode.go deleted file mode 100644 index 9c3a08dbc..000000000 --- a/fuzz/zpay32/encode.go +++ /dev/null @@ -1,52 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package zpay32fuzz - -import ( - "encoding/hex" - "fmt" - - "github.com/btcsuite/btcd/btcec/v2" - "github.com/btcsuite/btcd/btcec/v2/ecdsa" - "github.com/btcsuite/btcd/chaincfg" - "github.com/btcsuite/btcd/chaincfg/chainhash" - "github.com/lightningnetwork/lnd/zpay32" -) - -// Fuzz_encode is used by go-fuzz. -func Fuzz_encode(data []byte) int { - inv, err := zpay32.Decode(string(data), &chaincfg.TestNet3Params) - if err != nil { - return 1 - } - - // Call these functions as a sanity check to make sure the invoice - // is well-formed. - _ = inv.MinFinalCLTVExpiry() - _ = inv.Expiry() - - // Initialize the static key we will be using for this fuzz test. - testPrivKeyBytes, _ := hex.DecodeString("e126f68f7eafcc8b74f54d269fe206be715000f94dac067d1c04a8ca3b2db734") - testPrivKey, _ := btcec.PrivKeyFromBytes(testPrivKeyBytes) - - // Then, initialize the testMessageSigner so we can encode out - // invoices with this private key. - testMessageSigner := zpay32.MessageSigner{ - SignCompact: func(msg []byte) ([]byte, error) { - hash := chainhash.HashB(msg) - sig, err := ecdsa.SignCompact(testPrivKey, hash, true) - if err != nil { - return nil, fmt.Errorf("can't sign the "+ - "message: %v", err) - } - return sig, nil - }, - } - _, err = inv.Encode(testMessageSigner) - if err != nil { - return 1 - } - - return 1 -} diff --git a/zpay32/fuzz_test.go b/zpay32/fuzz_test.go new file mode 100644 index 000000000..f777bcce0 --- /dev/null +++ b/zpay32/fuzz_test.go @@ -0,0 +1,64 @@ +package zpay32 + +import ( + "encoding/hex" + "fmt" + "testing" + + "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec/v2/ecdsa" + "github.com/btcsuite/btcd/chaincfg" + "github.com/btcsuite/btcd/chaincfg/chainhash" +) + +func Fuzz_decode(f *testing.F) { + f.Fuzz(func(t *testing.T, data string) { + inv, err := Decode(data, &chaincfg.TestNet3Params) + if err != nil { + return + } + + // Call these functions as a sanity check to make sure the + // invoice is well-formed. + _ = inv.MinFinalCLTVExpiry() + _ = inv.Expiry() + }) +} + +func Fuzz_encode(f *testing.F) { + f.Fuzz(func(t *testing.T, data string) { + inv, err := Decode(data, &chaincfg.TestNet3Params) + if err != nil { + return + } + + // Call these functions as a sanity check to make sure the + // invoice is well-formed. + _ = inv.MinFinalCLTVExpiry() + _ = inv.Expiry() + + // Initialize the static key we will be using for this fuzz + // test. + testPrivKeyBytes, _ := hex.DecodeString("e126f68f7eafcc8b74f54d269fe206be715000f94dac067d1c04a8ca3b2db734") + testPrivKey, _ := btcec.PrivKeyFromBytes(testPrivKeyBytes) + + // Then, initialize the testMessageSigner so we can encode out + // invoices with this private key. + testMessageSigner := MessageSigner{ + SignCompact: func(msg []byte) ([]byte, error) { + hash := chainhash.HashB(msg) + sig, err := ecdsa.SignCompact(testPrivKey, hash, true) + if err != nil { + return nil, + fmt.Errorf("can't sign the "+ + "message: %v", err) + } + return sig, nil + }, + } + _, err = inv.Encode(testMessageSigner) + if err != nil { + return + } + }) +} From c77852a68c5e6df8b607dcc51fe93c47b1c94dc9 Mon Sep 17 00:00:00 2001 From: Conner Date: Fri, 29 Jul 2022 19:17:06 -0400 Subject: [PATCH 4/7] brontide: migrate fuzz tests --- brontide/fuzz_test.go | 701 +++++++++++++++++++++ docs/release-notes/release-notes-0.16.0.md | 4 + fuzz/brontide/fuzz_utils.go | 139 ---- fuzz/brontide/random_actone.go | 31 - fuzz/brontide/random_actthree.go | 51 -- fuzz/brontide/random_acttwo.go | 40 -- fuzz/brontide/random_init_decrypt.go | 28 - fuzz/brontide/random_init_enc_dec.go | 49 -- fuzz/brontide/random_init_encrypt.go | 38 -- fuzz/brontide/random_resp_decrypt.go | 28 - fuzz/brontide/random_resp_enc_dec.go | 49 -- fuzz/brontide/random_resp_encrypt.go | 38 -- fuzz/brontide/static_actone.go | 31 - fuzz/brontide/static_actthree.go | 51 -- fuzz/brontide/static_acttwo.go | 40 -- fuzz/brontide/static_init_decrypt.go | 28 - fuzz/brontide/static_init_enc_dec.go | 50 -- fuzz/brontide/static_init_encrypt.go | 38 -- fuzz/brontide/static_resp_decrypt.go | 28 - fuzz/brontide/static_resp_enc_dec.go | 49 -- fuzz/brontide/static_resp_encrypt.go | 38 -- 21 files changed, 705 insertions(+), 844 deletions(-) create mode 100644 brontide/fuzz_test.go delete mode 100644 fuzz/brontide/fuzz_utils.go delete mode 100644 fuzz/brontide/random_actone.go delete mode 100644 fuzz/brontide/random_actthree.go delete mode 100644 fuzz/brontide/random_acttwo.go delete mode 100644 fuzz/brontide/random_init_decrypt.go delete mode 100644 fuzz/brontide/random_init_enc_dec.go delete mode 100644 fuzz/brontide/random_init_encrypt.go delete mode 100644 fuzz/brontide/random_resp_decrypt.go delete mode 100644 fuzz/brontide/random_resp_enc_dec.go delete mode 100644 fuzz/brontide/random_resp_encrypt.go delete mode 100644 fuzz/brontide/static_actone.go delete mode 100644 fuzz/brontide/static_actthree.go delete mode 100644 fuzz/brontide/static_acttwo.go delete mode 100644 fuzz/brontide/static_init_decrypt.go delete mode 100644 fuzz/brontide/static_init_enc_dec.go delete mode 100644 fuzz/brontide/static_init_encrypt.go delete mode 100644 fuzz/brontide/static_resp_decrypt.go delete mode 100644 fuzz/brontide/static_resp_enc_dec.go delete mode 100644 fuzz/brontide/static_resp_encrypt.go diff --git a/brontide/fuzz_test.go b/brontide/fuzz_test.go new file mode 100644 index 000000000..5cc7278a7 --- /dev/null +++ b/brontide/fuzz_test.go @@ -0,0 +1,701 @@ +package brontide + +import ( + "bytes" + "encoding/hex" + "math" + "testing" + + "github.com/btcsuite/btcd/btcec/v2" + "github.com/davecgh/go-spew/spew" + "github.com/lightningnetwork/lnd/keychain" +) + +var ( + initBytes = []byte{ + 0x81, 0xb6, 0x37, 0xd8, 0xfc, 0xd2, 0xc6, 0xda, + 0x63, 0x59, 0xe6, 0x96, 0x31, 0x13, 0xa1, 0x17, + 0xd, 0xe7, 0x95, 0xe4, 0xb7, 0x25, 0xb8, 0x4d, + 0x1e, 0xb, 0x4c, 0xfd, 0x9e, 0xc5, 0x8c, 0xe9, + } + + respBytes = []byte{ + 0xaa, 0xb6, 0x37, 0xd9, 0xfc, 0xd2, 0xc6, 0xda, + 0x63, 0x59, 0xe6, 0x99, 0x31, 0x13, 0xa1, 0x17, + 0xd, 0xe7, 0x95, 0xe9, 0xb7, 0x25, 0xb8, 0x4d, + 0x1e, 0xb, 0x4c, 0xf9, 0x9e, 0xc5, 0x8c, 0xe9, + } + + // Returns the initiator's ephemeral private key. + initEphemeral = EphemeralGenerator(func() (*btcec.PrivateKey, error) { + e := "121212121212121212121212121212121212121212121212121212" + + "1212121212" + eBytes, err := hex.DecodeString(e) + if err != nil { + return nil, err + } + + priv, _ := btcec.PrivKeyFromBytes(eBytes) + return priv, nil + }) + + // Returns the responder's ephemeral private key. + respEphemeral = EphemeralGenerator(func() (*btcec.PrivateKey, error) { + e := "222222222222222222222222222222222222222222222222222" + + "2222222222222" + eBytes, err := hex.DecodeString(e) + if err != nil { + return nil, err + } + + priv, _ := btcec.PrivKeyFromBytes(eBytes) + return priv, nil + }) +) + +// completeHandshake takes two brontide machines (initiator, responder) +// and completes the brontide handshake between them. If any part of the +// handshake fails, this function will panic. +func completeHandshake(initiator, responder *Machine, t *testing.T) { + if err := handshake(initiator, responder); err != nil { + nilAndPanic(initiator, responder, err, t) + } +} + +// handshake actually completes the brontide handshake and bubbles up +// an error to the calling function. +func handshake(initiator, responder *Machine) error { + // Generate ActOne and send to the responder. + actOne, err := initiator.GenActOne() + if err != nil { + return err + } + + if err := responder.RecvActOne(actOne); err != nil { + return err + } + + // Generate ActTwo and send to initiator. + actTwo, err := responder.GenActTwo() + if err != nil { + return err + } + + if err := initiator.RecvActTwo(actTwo); err != nil { + return err + } + + // Generate ActThree and send to responder. + actThree, err := initiator.GenActThree() + if err != nil { + return err + } + + return responder.RecvActThree(actThree) +} + +// nilAndPanic first nils the initiator and responder's Curve fields and then +// panics. +func nilAndPanic(initiator, responder *Machine, err error, t *testing.T) { + t.Fatalf("error: %v, initiator: %v, responder: %v", err, + spew.Sdump(initiator), spew.Sdump(responder)) +} + +// getBrontideMachines returns two brontide machines that use random keys +// everywhere. +func getBrontideMachines() (*Machine, *Machine) { + initPriv, _ := btcec.NewPrivateKey() + respPriv, _ := btcec.NewPrivateKey() + respPub := (respPriv.PubKey()) + + initPrivECDH := &keychain.PrivKeyECDH{PrivKey: initPriv} + respPrivECDH := &keychain.PrivKeyECDH{PrivKey: respPriv} + + initiator := NewBrontideMachine(true, initPrivECDH, respPub) + responder := NewBrontideMachine(false, respPrivECDH, nil) + + return initiator, responder +} + +// getStaticBrontideMachines returns two brontide machines that use static keys +// everywhere. +func getStaticBrontideMachines() (*Machine, *Machine) { + initPriv, _ := btcec.PrivKeyFromBytes(initBytes) + respPriv, respPub := btcec.PrivKeyFromBytes(respBytes) + + initPrivECDH := &keychain.PrivKeyECDH{PrivKey: initPriv} + respPrivECDH := &keychain.PrivKeyECDH{PrivKey: respPriv} + + initiator := NewBrontideMachine( + true, initPrivECDH, respPub, initEphemeral, + ) + responder := NewBrontideMachine( + false, respPrivECDH, nil, respEphemeral, + ) + + return initiator, responder +} + +// Fuzz_random_actone fuzz tests ActOne in the brontide handshake. +func Fuzz_random_actone(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Check if data is large enough. + if len(data) < ActOneSize { + return + } + + // This will return brontide machines with random keys. + _, responder := getBrontideMachines() + + // Copy data into [ActOneSize]byte. + var actOne [ActOneSize]byte + copy(actOne[:], data) + + // Responder receives ActOne, should fail on the MAC check. + if err := responder.RecvActOne(actOne); err == nil { + nilAndPanic(nil, responder, nil, t) + } + }) +} + +// Fuzz_random_actthree fuzz tests ActThree in the brontide handshake. +func Fuzz_random_actthree(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Check if data is large enough. + if len(data) < ActThreeSize { + return + } + + // This will return brontide machines with random keys. + initiator, responder := getBrontideMachines() + + // Generate ActOne and send to the responder. + actOne, err := initiator.GenActOne() + if err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Receiving ActOne should succeed, so we panic on error. + if err := responder.RecvActOne(actOne); err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Generate ActTwo - this is not sent to the initiator because + // nothing is done with the initiator after this point and it + // would slow down fuzzing. GenActTwo needs to be called to set + // the appropriate state in the responder machine. + _, err = responder.GenActTwo() + if err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Copy data into [ActThreeSize]byte. + var actThree [ActThreeSize]byte + copy(actThree[:], data) + + // Responder receives ActThree, should fail on the MAC check. + if err := responder.RecvActThree(actThree); err == nil { + nilAndPanic(initiator, responder, nil, t) + } + }) +} + +// Fuzz_random_acttwo fuzz tests ActTwo in the brontide handshake. +func Fuzz_random_acttwo(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Check if data is large enough. + if len(data) < ActTwoSize { + return + } + + // This will return brontide machines with random keys. + initiator, _ := getBrontideMachines() + + // Generate ActOne - this isn't sent to the responder because + // nothing is done with the responder machine and this would + // slow down fuzzing. GenActOne needs to be called to set the + // appropriate state in the initiator machine. + _, err := initiator.GenActOne() + if err != nil { + nilAndPanic(initiator, nil, err, t) + } + + // Copy data into [ActTwoSize]byte. + var actTwo [ActTwoSize]byte + copy(actTwo[:], data) + + // Initiator receives ActTwo, should fail. + if err := initiator.RecvActTwo(actTwo); err == nil { + nilAndPanic(initiator, nil, nil, t) + } + }) +} + +// Fuzz_random_init_decrypt fuzz tests decrypting arbitrary data with the +// initiator. +func Fuzz_random_init_decrypt(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // This will return brontide machines with random keys. + initiator, responder := getBrontideMachines() + + // Complete the brontide handshake. + completeHandshake(initiator, responder, t) + + // Create a reader with the byte array. + r := bytes.NewReader(data) + + // Decrypt the encrypted message using ReadMessage w/ initiator + // machine. + if _, err := initiator.ReadMessage(r); err == nil { + nilAndPanic(initiator, responder, nil, t) + } + }) +} + +// Fuzz_random_init_enc_dec fuzz tests round-trip encryption and decryption +// between the initiator and the responder. +func Fuzz_random_init_enc_dec(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Ensure that length of message is not greater than max allowed + // size. + if len(data) > math.MaxUint16 { + return + } + + // This will return brontide machines with random keys. + initiator, responder := getBrontideMachines() + + // Complete the brontide handshake. + completeHandshake(initiator, responder, t) + + var b bytes.Buffer + + // Encrypt the message using WriteMessage w/ initiator machine. + if err := initiator.WriteMessage(data); err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Flush the encrypted message w/ initiator machine. + if _, err := initiator.Flush(&b); err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Decrypt the ciphertext using ReadMessage w/ responder machine. + plaintext, err := responder.ReadMessage(&b) + if err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Check that the decrypted message and the original message are + // equal. + if !bytes.Equal(data, plaintext) { + nilAndPanic(initiator, responder, nil, t) + } + }) +} + +// Fuzz_random_init_encrypt fuzz tests the encryption of arbitrary data with +// the initiator. +func Fuzz_random_init_encrypt(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Ensure that length of message is not greater than max allowed + // size. + if len(data) > math.MaxUint16 { + return + } + + // This will return brontide machines with random keys. + initiator, responder := getBrontideMachines() + + // Complete the brontide handshake. + completeHandshake(initiator, responder, t) + + var b bytes.Buffer + + // Encrypt the message using WriteMessage w/ initiator machine. + if err := initiator.WriteMessage(data); err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Flush the encrypted message w/ initiator machine. + if _, err := initiator.Flush(&b); err != nil { + nilAndPanic(initiator, responder, err, t) + } + }) +} + +// Fuzz_random_resp_decrypt fuzz tests the decryption of arbitrary data with +// the responder. +func Fuzz_random_resp_decrypt(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // This will return brontide machines with random keys. + initiator, responder := getBrontideMachines() + + // Complete the brontide handshake. + completeHandshake(initiator, responder, t) + + // Create a reader with the byte array. + r := bytes.NewReader(data) + + // Decrypt the encrypted message using ReadMessage w/ responder + // machine. + if _, err := responder.ReadMessage(r); err == nil { + nilAndPanic(initiator, responder, nil, t) + } + }) +} + +// Fuzz_random_resp_enc_dec fuzz tests round-trip encryption and decryption +// between the responder and the initiator. +func Fuzz_random_resp_enc_dec(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Ensure that length of message is not greater than max allowed + // size. + if len(data) > math.MaxUint16 { + return + } + + // This will return brontide machines with random keys. + initiator, responder := getBrontideMachines() + + // Complete the brontide handshake. + completeHandshake(initiator, responder, t) + + var b bytes.Buffer + + // Encrypt the message using WriteMessage w/ responder machine. + if err := responder.WriteMessage(data); err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Flush the encrypted message w/ responder machine. + if _, err := responder.Flush(&b); err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Decrypt the ciphertext using ReadMessage w/ initiator machine. + plaintext, err := initiator.ReadMessage(&b) + if err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Check that the decrypted message and the original message are + // equal. + if !bytes.Equal(data, plaintext) { + nilAndPanic(initiator, responder, nil, t) + } + }) +} + +// Fuzz_random_resp_encrypt fuzz tests encryption of arbitrary data with the +// responder. +func Fuzz_random_resp_encrypt(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Ensure that length of message is not greater than max allowed + // size. + if len(data) > math.MaxUint16 { + return + } + + // This will return brontide machines with random keys. + initiator, responder := getBrontideMachines() + + // Complete the brontide handshake. + completeHandshake(initiator, responder, t) + + var b bytes.Buffer + + // Encrypt the message using WriteMessage w/ responder machine. + if err := responder.WriteMessage(data); err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Flush the encrypted message w/ responder machine. + if _, err := responder.Flush(&b); err != nil { + nilAndPanic(initiator, responder, err, t) + } + }) +} + +// Fuzz_static_actone fuzz tests ActOne in the brontide handshake. +func Fuzz_static_actone(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Check if data is large enough. + if len(data) < ActOneSize { + return + } + + // This will return brontide machines with static keys. + _, responder := getStaticBrontideMachines() + + // Copy data into [ActOneSize]byte. + var actOne [ActOneSize]byte + copy(actOne[:], data) + + // Responder receives ActOne, should fail. + if err := responder.RecvActOne(actOne); err == nil { + nilAndPanic(nil, responder, nil, t) + } + }) +} + +// Fuzz_static_actthree fuzz tests ActThree in the brontide handshake. +func Fuzz_static_actthree(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Check if data is large enough. + if len(data) < ActThreeSize { + return + } + + // This will return brontide machines with static keys. + initiator, responder := getStaticBrontideMachines() + + // Generate ActOne and send to the responder. + actOne, err := initiator.GenActOne() + if err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Receiving ActOne should succeed, so we panic on error. + if err := responder.RecvActOne(actOne); err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Generate ActTwo - this is not sent to the initiator because + // nothing is done with the initiator after this point and it + // would slow down fuzzing. GenActTwo needs to be called to set + // the appropriate state in the responder machine. + _, err = responder.GenActTwo() + if err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Copy data into [ActThreeSize]byte. + var actThree [ActThreeSize]byte + copy(actThree[:], data) + + // Responder receives ActThree, should fail. + if err := responder.RecvActThree(actThree); err == nil { + nilAndPanic(initiator, responder, nil, t) + } + }) +} + +// Fuzz_static_acttwo fuzz tests ActTwo in the brontide handshake. +func Fuzz_static_acttwo(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Check if data is large enough. + if len(data) < ActTwoSize { + return + } + + // This will return brontide machines with static keys. + initiator, _ := getStaticBrontideMachines() + + // Generate ActOne - this isn't sent to the responder because + // nothing is done with the responder machine and this would + // slow down fuzzing. GenActOne needs to be called to set the + // appropriate state in the initiator machine. + _, err := initiator.GenActOne() + if err != nil { + nilAndPanic(initiator, nil, err, t) + } + + // Copy data into [ActTwoSize]byte. + var actTwo [ActTwoSize]byte + copy(actTwo[:], data) + + // Initiator receives ActTwo, should fail. + if err := initiator.RecvActTwo(actTwo); err == nil { + nilAndPanic(initiator, nil, nil, t) + } + }) +} + +// Fuzz_static_init_decrypt fuzz tests the decryption of arbitrary data with +// the initiator. +func Fuzz_static_init_decrypt(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // This will return brontide machines with static keys. + initiator, responder := getStaticBrontideMachines() + + // Complete the brontide handshake. + completeHandshake(initiator, responder, t) + + // Create a reader with the byte array. + r := bytes.NewReader(data) + + // Decrypt the encrypted message using ReadMessage w/ initiator + // machine. + if _, err := initiator.ReadMessage(r); err == nil { + nilAndPanic(initiator, responder, nil, t) + } + }) +} + +// Fuzz_static_init_enc_dec fuzz tests round-trip encryption and decryption +// between the initiator and the responder. +func Fuzz_static_init_enc_dec(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Ensure that length of message is not greater than max allowed + // size. + if len(data) > math.MaxUint16 { + return + } + + // This will return brontide machines with static keys. + initiator, responder := getStaticBrontideMachines() + + // Complete the brontide handshake. + completeHandshake(initiator, responder, t) + + var b bytes.Buffer + + // Encrypt the message using WriteMessage w/ initiator machine. + if err := initiator.WriteMessage(data); err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Flush the encrypted message w/ initiator machine. + if _, err := initiator.Flush(&b); err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Decrypt the ciphertext using ReadMessage w/ responder + // machine. + plaintext, err := responder.ReadMessage(&b) + if err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Check that the decrypted message and the original message are + // equal. + if !bytes.Equal(data, plaintext) { + nilAndPanic(initiator, responder, nil, t) + } + }) +} + +// Fuzz_static_init_encrypt fuzz tests the encryption of arbitrary data with +// the initiator. +func Fuzz_static_init_encrypt(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Ensure that length of message is not greater than max allowed + // size. + if len(data) > math.MaxUint16 { + return + } + + // This will return brontide machines with static keys. + initiator, responder := getStaticBrontideMachines() + + // Complete the brontide handshake. + completeHandshake(initiator, responder, t) + + var b bytes.Buffer + + // Encrypt the message using WriteMessage w/ initiator machine. + if err := initiator.WriteMessage(data); err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Flush the encrypted message w/ initiator machine. + if _, err := initiator.Flush(&b); err != nil { + nilAndPanic(initiator, responder, err, t) + } + }) +} + +// Fuzz_static_resp_decrypt fuzz tests the decryption of arbitrary data with +// the responder. +func Fuzz_static_resp_decrypt(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // This will return brontide machines with static keys. + initiator, responder := getStaticBrontideMachines() + + // Complete the brontide handshake. + completeHandshake(initiator, responder, t) + + // Create a reader with the byte array. + r := bytes.NewReader(data) + + // Decrypt the encrypted message using ReadMessage w/ responder + // machine. + if _, err := responder.ReadMessage(r); err == nil { + nilAndPanic(initiator, responder, nil, t) + } + }) +} + +// Fuzz_static_resp_enc_dec fuzz tests the round-trip encryption and decryption +// between the responder and the initiator. +func Fuzz_static_resp_enc_dec(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Ensure that length of message is not greater than max allowed + // size. + if len(data) > math.MaxUint16 { + return + } + + // This will return brontide machines with static keys. + initiator, responder := getStaticBrontideMachines() + + // Complete the brontide handshake. + completeHandshake(initiator, responder, t) + + var b bytes.Buffer + + // Encrypt the message using WriteMessage w/ responder machine. + if err := responder.WriteMessage(data); err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Flush the encrypted message w/ responder machine. + if _, err := responder.Flush(&b); err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Decrypt the ciphertext using ReadMessage w/ initiator + // machine. + plaintext, err := initiator.ReadMessage(&b) + if err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Check that the decrypted message and the original message are + // equal. + if !bytes.Equal(data, plaintext) { + nilAndPanic(initiator, responder, nil, t) + } + }) +} + +// Fuzz_static_resp_encrypt fuzz tests the encryption of arbitrary data with +// the responder. +func Fuzz_static_resp_encrypt(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + // Ensure that length of message is not greater than max allowed + // size. + if len(data) > math.MaxUint16 { + return + } + + // This will return brontide machines with static keys. + initiator, responder := getStaticBrontideMachines() + + // Complete the brontide handshake. + completeHandshake(initiator, responder, t) + + var b bytes.Buffer + + // Encrypt the message using WriteMessage w/ responder machine. + if err := responder.WriteMessage(data); err != nil { + nilAndPanic(initiator, responder, err, t) + } + + // Flush the encrypted message w/ responder machine. + if _, err := responder.Flush(&b); err != nil { + nilAndPanic(initiator, responder, err, t) + } + }) +} diff --git a/docs/release-notes/release-notes-0.16.0.md b/docs/release-notes/release-notes-0.16.0.md index d6df7387b..c130b73c3 100644 --- a/docs/release-notes/release-notes-0.16.0.md +++ b/docs/release-notes/release-notes-0.16.0.md @@ -131,6 +131,9 @@ certain large transactions](https://github.com/lightningnetwork/lnd/pull/7100). * [Fixed a bug where paying an invoice with a malformed route hint triggers a never-ending retry loop](https://github.com/lightningnetwork/lnd/pull/6766) +* [Migrated from go-fuzz to Go 1.18's new standard fuzz testing + library](https://github.com/lightningnetwork/lnd/pull/6784). + ## `lncli` * [Add an `insecure` flag to skip tls auth as well as a `metadata` string slice flag](https://github.com/lightningnetwork/lnd/pull/6818) that allows the @@ -226,6 +229,7 @@ to refactor the itest for code health and maintenance. * andreihod * Carla Kirk-Cohen +* Conner Babinchak * cutiful * Daniel McNally * Elle Mouton diff --git a/fuzz/brontide/fuzz_utils.go b/fuzz/brontide/fuzz_utils.go deleted file mode 100644 index e7a1e86b2..000000000 --- a/fuzz/brontide/fuzz_utils.go +++ /dev/null @@ -1,139 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "encoding/hex" - "fmt" - - "github.com/btcsuite/btcd/btcec/v2" - "github.com/davecgh/go-spew/spew" - "github.com/lightningnetwork/lnd/brontide" - "github.com/lightningnetwork/lnd/keychain" -) - -var ( - initBytes = []byte{ - 0x81, 0xb6, 0x37, 0xd8, 0xfc, 0xd2, 0xc6, 0xda, - 0x63, 0x59, 0xe6, 0x96, 0x31, 0x13, 0xa1, 0x17, - 0xd, 0xe7, 0x95, 0xe4, 0xb7, 0x25, 0xb8, 0x4d, - 0x1e, 0xb, 0x4c, 0xfd, 0x9e, 0xc5, 0x8c, 0xe9, - } - - respBytes = []byte{ - 0xaa, 0xb6, 0x37, 0xd9, 0xfc, 0xd2, 0xc6, 0xda, - 0x63, 0x59, 0xe6, 0x99, 0x31, 0x13, 0xa1, 0x17, - 0xd, 0xe7, 0x95, 0xe9, 0xb7, 0x25, 0xb8, 0x4d, - 0x1e, 0xb, 0x4c, 0xf9, 0x9e, 0xc5, 0x8c, 0xe9, - } - - // Returns the initiator's ephemeral private key. - initEphemeral = brontide.EphemeralGenerator(func() (*btcec.PrivateKey, error) { - e := "121212121212121212121212121212121212121212121212121212" + - "1212121212" - eBytes, err := hex.DecodeString(e) - if err != nil { - return nil, err - } - - priv, _ := btcec.PrivKeyFromBytes(eBytes) - return priv, nil - }) - - // Returns the responder's ephemeral private key. - respEphemeral = brontide.EphemeralGenerator(func() (*btcec.PrivateKey, error) { - e := "222222222222222222222222222222222222222222222222222" + - "2222222222222" - eBytes, err := hex.DecodeString(e) - if err != nil { - return nil, err - } - - priv, _ := btcec.PrivKeyFromBytes(eBytes) - return priv, nil - }) -) - -// completeHandshake takes two brontide machines (initiator, responder) -// and completes the brontide handshake between them. If any part of the -// handshake fails, this function will panic. -func completeHandshake(initiator, responder *brontide.Machine) { - if err := handshake(initiator, responder); err != nil { - nilAndPanic(initiator, responder, err) - } -} - -// handshake actually completes the brontide handshake and bubbles up -// an error to the calling function. -func handshake(initiator, responder *brontide.Machine) error { - // Generate ActOne and send to the responder. - actOne, err := initiator.GenActOne() - if err != nil { - return err - } - - if err := responder.RecvActOne(actOne); err != nil { - return err - } - - // Generate ActTwo and send to initiator. - actTwo, err := responder.GenActTwo() - if err != nil { - return err - } - - if err := initiator.RecvActTwo(actTwo); err != nil { - return err - } - - // Generate ActThree and send to responder. - actThree, err := initiator.GenActThree() - if err != nil { - return err - } - - return responder.RecvActThree(actThree) -} - -// nilAndPanic first nils the initiator and responder's Curve fields and then -// panics. -func nilAndPanic(initiator, responder *brontide.Machine, err error) { - panic(fmt.Errorf("error: %v, initiator: %v, responder: %v", err, - spew.Sdump(initiator), spew.Sdump(responder))) -} - -// getBrontideMachines returns two brontide machines that use random keys -// everywhere. -func getBrontideMachines() (*brontide.Machine, *brontide.Machine) { - initPriv, _ := btcec.NewPrivateKey() - respPriv, _ := btcec.NewPrivateKey() - respPub := (*btcec.PublicKey)(&respPriv.PublicKey) - - initPrivECDH := &keychain.PrivKeyECDH{PrivKey: initPriv} - respPrivECDH := &keychain.PrivKeyECDH{PrivKey: respPriv} - - initiator := brontide.NewBrontideMachine(true, initPrivECDH, respPub) - responder := brontide.NewBrontideMachine(false, respPrivECDH, nil) - - return initiator, responder -} - -// getStaticBrontideMachines returns two brontide machines that use static keys -// everywhere. -func getStaticBrontideMachines() (*brontide.Machine, *brontide.Machine) { - initPriv, _ := btcec.PrivKeyFromBytes(initBytes) - respPriv, respPub := btcec.PrivKeyFromBytes(respBytes) - - initPrivECDH := &keychain.PrivKeyECDH{PrivKey: initPriv} - respPrivECDH := &keychain.PrivKeyECDH{PrivKey: respPriv} - - initiator := brontide.NewBrontideMachine( - true, initPrivECDH, respPub, initEphemeral, - ) - responder := brontide.NewBrontideMachine( - false, respPrivECDH, nil, respEphemeral, - ) - - return initiator, responder -} diff --git a/fuzz/brontide/random_actone.go b/fuzz/brontide/random_actone.go deleted file mode 100644 index 7d2e0c078..000000000 --- a/fuzz/brontide/random_actone.go +++ /dev/null @@ -1,31 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "github.com/lightningnetwork/lnd/brontide" -) - -// Fuzz_random_actone is a go-fuzz harness for ActOne in the brontide -// handshake. -func Fuzz_random_actone(data []byte) int { - // Check if data is large enough. - if len(data) < brontide.ActOneSize { - return 1 - } - - // This will return brontide machines with random keys. - _, responder := getBrontideMachines() - - // Copy data into [ActOneSize]byte. - var actOne [brontide.ActOneSize]byte - copy(actOne[:], data) - - // Responder receives ActOne, should fail on the MAC check. - if err := responder.RecvActOne(actOne); err == nil { - nilAndPanic(nil, responder, nil) - } - - return 1 -} diff --git a/fuzz/brontide/random_actthree.go b/fuzz/brontide/random_actthree.go deleted file mode 100644 index fe0c70765..000000000 --- a/fuzz/brontide/random_actthree.go +++ /dev/null @@ -1,51 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "github.com/lightningnetwork/lnd/brontide" -) - -// Fuzz_random_actthree is a go-fuzz harness for ActThree in the brontide -// handshake. -func Fuzz_random_actthree(data []byte) int { - // Check if data is large enough. - if len(data) < brontide.ActThreeSize { - return 1 - } - - // This will return brontide machines with random keys. - initiator, responder := getBrontideMachines() - - // Generate ActOne and send to the responder. - actOne, err := initiator.GenActOne() - if err != nil { - nilAndPanic(initiator, responder, err) - } - - // Receiving ActOne should succeed, so we panic on error. - if err := responder.RecvActOne(actOne); err != nil { - nilAndPanic(initiator, responder, err) - } - - // Generate ActTwo - this is not sent to the initiator because nothing is - // done with the initiator after this point and it would slow down fuzzing. - // GenActTwo needs to be called to set the appropriate state in the - // responder machine. - _, err = responder.GenActTwo() - if err != nil { - nilAndPanic(initiator, responder, err) - } - - // Copy data into [ActThreeSize]byte. - var actThree [brontide.ActThreeSize]byte - copy(actThree[:], data) - - // Responder receives ActThree, should fail on the MAC check. - if err := responder.RecvActThree(actThree); err == nil { - nilAndPanic(initiator, responder, nil) - } - - return 1 -} diff --git a/fuzz/brontide/random_acttwo.go b/fuzz/brontide/random_acttwo.go deleted file mode 100644 index 8cef21b7b..000000000 --- a/fuzz/brontide/random_acttwo.go +++ /dev/null @@ -1,40 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "github.com/lightningnetwork/lnd/brontide" -) - -// Fuzz_random_acttwo is a go-fuzz harness for ActTwo in the brontide -// handshake. -func Fuzz_random_acttwo(data []byte) int { - // Check if data is large enough. - if len(data) < brontide.ActTwoSize { - return 1 - } - - // This will return brontide machines with random keys. - initiator, _ := getBrontideMachines() - - // Generate ActOne - this isn't sent to the responder because nothing is - // done with the responder machine and this would slow down fuzzing. - // GenActOne needs to be called to set the appropriate state in the - // initiator machine. - _, err := initiator.GenActOne() - if err != nil { - nilAndPanic(initiator, nil, err) - } - - // Copy data into [ActTwoSize]byte. - var actTwo [brontide.ActTwoSize]byte - copy(actTwo[:], data) - - // Initiator receives ActTwo, should fail. - if err := initiator.RecvActTwo(actTwo); err == nil { - nilAndPanic(initiator, nil, nil) - } - - return 1 -} diff --git a/fuzz/brontide/random_init_decrypt.go b/fuzz/brontide/random_init_decrypt.go deleted file mode 100644 index b9a4aac52..000000000 --- a/fuzz/brontide/random_init_decrypt.go +++ /dev/null @@ -1,28 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "bytes" -) - -// Fuzz_random_init_decrypt is a go-fuzz harness that decrypts arbitrary data -// with the initiator. -func Fuzz_random_init_decrypt(data []byte) int { - // This will return brontide machines with random keys. - initiator, responder := getBrontideMachines() - - // Complete the brontide handshake. - completeHandshake(initiator, responder) - - // Create a reader with the byte array. - r := bytes.NewReader(data) - - // Decrypt the encrypted message using ReadMessage w/ initiator machine. - if _, err := initiator.ReadMessage(r); err == nil { - nilAndPanic(initiator, responder, nil) - } - - return 1 -} diff --git a/fuzz/brontide/random_init_enc_dec.go b/fuzz/brontide/random_init_enc_dec.go deleted file mode 100644 index 7d79cfc45..000000000 --- a/fuzz/brontide/random_init_enc_dec.go +++ /dev/null @@ -1,49 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "bytes" - "math" -) - -// Fuzz_random_init_enc_dec is a go-fuzz harness that tests round-trip -// encryption and decryption between the initiator and the responder. -func Fuzz_random_init_enc_dec(data []byte) int { - // Ensure that length of message is not greater than max allowed size. - if len(data) > math.MaxUint16 { - return 1 - } - - // This will return brontide machines with random keys. - initiator, responder := getBrontideMachines() - - // Complete the brontide handshake. - completeHandshake(initiator, responder) - - var b bytes.Buffer - - // Encrypt the message using WriteMessage w/ initiator machine. - if err := initiator.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err) - } - - // Flush the encrypted message w/ initiator machine. - if _, err := initiator.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err) - } - - // Decrypt the ciphertext using ReadMessage w/ responder machine. - plaintext, err := responder.ReadMessage(&b) - if err != nil { - nilAndPanic(initiator, responder, err) - } - - // Check that the decrypted message and the original message are equal. - if !bytes.Equal(data, plaintext) { - nilAndPanic(initiator, responder, nil) - } - - return 1 -} diff --git a/fuzz/brontide/random_init_encrypt.go b/fuzz/brontide/random_init_encrypt.go deleted file mode 100644 index 50f784c45..000000000 --- a/fuzz/brontide/random_init_encrypt.go +++ /dev/null @@ -1,38 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "bytes" - "math" -) - -// Fuzz_random_init_encrypt is a go-fuzz harness that encrypts arbitrary data -// with the initiator. -func Fuzz_random_init_encrypt(data []byte) int { - // Ensure that length of message is not greater than max allowed size. - if len(data) > math.MaxUint16 { - return 1 - } - - // This will return brontide machines with random keys. - initiator, responder := getBrontideMachines() - - // Complete the brontide handshake. - completeHandshake(initiator, responder) - - var b bytes.Buffer - - // Encrypt the message using WriteMessage w/ initiator machine. - if err := initiator.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err) - } - - // Flush the encrypted message w/ initiator machine. - if _, err := initiator.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err) - } - - return 1 -} diff --git a/fuzz/brontide/random_resp_decrypt.go b/fuzz/brontide/random_resp_decrypt.go deleted file mode 100644 index 9bb44faa1..000000000 --- a/fuzz/brontide/random_resp_decrypt.go +++ /dev/null @@ -1,28 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "bytes" -) - -// Fuzz_random_resp_decrypt is a go-fuzz harness that decrypts arbitrary data -// with the responder. -func Fuzz_random_resp_decrypt(data []byte) int { - // This will return brontide machines with random keys. - initiator, responder := getBrontideMachines() - - // Complete the brontide handshake. - completeHandshake(initiator, responder) - - // Create a reader with the byte array. - r := bytes.NewReader(data) - - // Decrypt the encrypted message using ReadMessage w/ responder machine. - if _, err := responder.ReadMessage(r); err == nil { - nilAndPanic(initiator, responder, nil) - } - - return 1 -} diff --git a/fuzz/brontide/random_resp_enc_dec.go b/fuzz/brontide/random_resp_enc_dec.go deleted file mode 100644 index 9d50816a2..000000000 --- a/fuzz/brontide/random_resp_enc_dec.go +++ /dev/null @@ -1,49 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "bytes" - "math" -) - -// Fuzz_random_resp_enc_dec is a go-fuzz harness that tests round-trip -// encryption and decryption between the responder and the initiator. -func Fuzz_random_resp_enc_dec(data []byte) int { - // Ensure that length of message is not greater than max allowed size. - if len(data) > math.MaxUint16 { - return 1 - } - - // This will return brontide machines with random keys. - initiator, responder := getBrontideMachines() - - // Complete the brontide handshake. - completeHandshake(initiator, responder) - - var b bytes.Buffer - - // Encrypt the message using WriteMessage w/ responder machine. - if err := responder.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err) - } - - // Flush the encrypted message w/ responder machine. - if _, err := responder.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err) - } - - // Decrypt the ciphertext using ReadMessage w/ initiator machine. - plaintext, err := initiator.ReadMessage(&b) - if err != nil { - nilAndPanic(initiator, responder, err) - } - - // Check that the decrypted message and the original message are equal. - if !bytes.Equal(data, plaintext) { - nilAndPanic(initiator, responder, nil) - } - - return 1 -} diff --git a/fuzz/brontide/random_resp_encrypt.go b/fuzz/brontide/random_resp_encrypt.go deleted file mode 100644 index c72f64df5..000000000 --- a/fuzz/brontide/random_resp_encrypt.go +++ /dev/null @@ -1,38 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "bytes" - "math" -) - -// Fuzz_random_resp_encrypt is a go-fuzz harness that encrypts arbitrary data -// with the responder. -func Fuzz_random_resp_encrypt(data []byte) int { - // Ensure that length of message is not greater than max allowed size. - if len(data) > math.MaxUint16 { - return 1 - } - - // This will return brontide machines with random keys. - initiator, responder := getBrontideMachines() - - // Complete the brontide handshake. - completeHandshake(initiator, responder) - - var b bytes.Buffer - - // Encrypt the message using WriteMessage w/ responder machine. - if err := responder.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err) - } - - // Flush the encrypted message w/ responder machine. - if _, err := responder.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err) - } - - return 1 -} diff --git a/fuzz/brontide/static_actone.go b/fuzz/brontide/static_actone.go deleted file mode 100644 index ee9700aff..000000000 --- a/fuzz/brontide/static_actone.go +++ /dev/null @@ -1,31 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "github.com/lightningnetwork/lnd/brontide" -) - -// Fuzz_static_actone is a go-fuzz harness for ActOne in the brontide -// handshake. -func Fuzz_static_actone(data []byte) int { - // Check if data is large enough. - if len(data) < brontide.ActOneSize { - return 1 - } - - // This will return brontide machines with static keys. - _, responder := getStaticBrontideMachines() - - // Copy data into [ActOneSize]byte. - var actOne [brontide.ActOneSize]byte - copy(actOne[:], data) - - // Responder receives ActOne, should fail. - if err := responder.RecvActOne(actOne); err == nil { - nilAndPanic(nil, responder, nil) - } - - return 1 -} diff --git a/fuzz/brontide/static_actthree.go b/fuzz/brontide/static_actthree.go deleted file mode 100644 index 61fa00b23..000000000 --- a/fuzz/brontide/static_actthree.go +++ /dev/null @@ -1,51 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "github.com/lightningnetwork/lnd/brontide" -) - -// Fuzz_static_actthree is a go-fuzz harness for ActThree in the brontide -// handshake. -func Fuzz_static_actthree(data []byte) int { - // Check if data is large enough. - if len(data) < brontide.ActThreeSize { - return 1 - } - - // This will return brontide machines with static keys. - initiator, responder := getStaticBrontideMachines() - - // Generate ActOne and send to the responder. - actOne, err := initiator.GenActOne() - if err != nil { - nilAndPanic(initiator, responder, err) - } - - // Receiving ActOne should succeed, so we panic on error. - if err := responder.RecvActOne(actOne); err != nil { - nilAndPanic(initiator, responder, err) - } - - // Generate ActTwo - this is not sent to the initiator because nothing is - // done with the initiator after this point and it would slow down fuzzing. - // GenActTwo needs to be called to set the appropriate state in the responder - // machine. - _, err = responder.GenActTwo() - if err != nil { - nilAndPanic(initiator, responder, err) - } - - // Copy data into [ActThreeSize]byte. - var actThree [brontide.ActThreeSize]byte - copy(actThree[:], data) - - // Responder receives ActThree, should fail. - if err := responder.RecvActThree(actThree); err == nil { - nilAndPanic(initiator, responder, nil) - } - - return 1 -} diff --git a/fuzz/brontide/static_acttwo.go b/fuzz/brontide/static_acttwo.go deleted file mode 100644 index e7e12326f..000000000 --- a/fuzz/brontide/static_acttwo.go +++ /dev/null @@ -1,40 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "github.com/lightningnetwork/lnd/brontide" -) - -// Fuzz_static_acttwo is a go-fuzz harness for ActTwo in the brontide -// handshake. -func Fuzz_static_acttwo(data []byte) int { - // Check if data is large enough. - if len(data) < brontide.ActTwoSize { - return 1 - } - - // This will return brontide machines with static keys. - initiator, _ := getStaticBrontideMachines() - - // Generate ActOne - this isn't sent to the responder because nothing is - // done with the responder machine and this would slow down fuzzing. - // GenActOne needs to be called to set the appropriate state in the initiator - // machine. - _, err := initiator.GenActOne() - if err != nil { - nilAndPanic(initiator, nil, err) - } - - // Copy data into [ActTwoSize]byte. - var actTwo [brontide.ActTwoSize]byte - copy(actTwo[:], data) - - // Initiator receives ActTwo, should fail. - if err := initiator.RecvActTwo(actTwo); err == nil { - nilAndPanic(initiator, nil, nil) - } - - return 1 -} diff --git a/fuzz/brontide/static_init_decrypt.go b/fuzz/brontide/static_init_decrypt.go deleted file mode 100644 index 9a39c8031..000000000 --- a/fuzz/brontide/static_init_decrypt.go +++ /dev/null @@ -1,28 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "bytes" -) - -// Fuzz_static_init_decrypt is a go-fuzz harness that decrypts arbitrary data -// with the initiator. -func Fuzz_static_init_decrypt(data []byte) int { - // This will return brontide machines with static keys. - initiator, responder := getStaticBrontideMachines() - - // Complete the brontide handshake. - completeHandshake(initiator, responder) - - // Create a reader with the byte array. - r := bytes.NewReader(data) - - // Decrypt the encrypted message using ReadMessage w/ initiator machine. - if _, err := initiator.ReadMessage(r); err == nil { - nilAndPanic(initiator, responder, nil) - } - - return 1 -} diff --git a/fuzz/brontide/static_init_enc_dec.go b/fuzz/brontide/static_init_enc_dec.go deleted file mode 100644 index 5a33aabf9..000000000 --- a/fuzz/brontide/static_init_enc_dec.go +++ /dev/null @@ -1,50 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "bytes" - "math" -) - -// Fuzz_static_init_enc_dec is a go-fuzz harness that tests round-trip -// encryption and decryption -// between the initiator and the responder. -func Fuzz_static_init_enc_dec(data []byte) int { - // Ensure that length of message is not greater than max allowed size. - if len(data) > math.MaxUint16 { - return 1 - } - - // This will return brontide machines with static keys. - initiator, responder := getStaticBrontideMachines() - - // Complete the brontide handshake. - completeHandshake(initiator, responder) - - var b bytes.Buffer - - // Encrypt the message using WriteMessage w/ initiator machine. - if err := initiator.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err) - } - - // Flush the encrypted message w/ initiator machine. - if _, err := initiator.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err) - } - - // Decrypt the ciphertext using ReadMessage w/ responder machine. - plaintext, err := responder.ReadMessage(&b) - if err != nil { - nilAndPanic(initiator, responder, err) - } - - // Check that the decrypted message and the original message are equal. - if !bytes.Equal(data, plaintext) { - nilAndPanic(initiator, responder, nil) - } - - return 1 -} diff --git a/fuzz/brontide/static_init_encrypt.go b/fuzz/brontide/static_init_encrypt.go deleted file mode 100644 index 6375dd4d5..000000000 --- a/fuzz/brontide/static_init_encrypt.go +++ /dev/null @@ -1,38 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "bytes" - "math" -) - -// Fuzz_static_init_encrypt is a go-fuzz harness that encrypts arbitrary data -// with the initiator. -func Fuzz_static_init_encrypt(data []byte) int { - // Ensure that length of message is not greater than max allowed size. - if len(data) > math.MaxUint16 { - return 1 - } - - // This will return brontide machines with static keys. - initiator, responder := getStaticBrontideMachines() - - // Complete the brontide handshake. - completeHandshake(initiator, responder) - - var b bytes.Buffer - - // Encrypt the message using WriteMessage w/ initiator machine. - if err := initiator.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err) - } - - // Flush the encrypted message w/ initiator machine. - if _, err := initiator.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err) - } - - return 1 -} diff --git a/fuzz/brontide/static_resp_decrypt.go b/fuzz/brontide/static_resp_decrypt.go deleted file mode 100644 index 140a52bbc..000000000 --- a/fuzz/brontide/static_resp_decrypt.go +++ /dev/null @@ -1,28 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "bytes" -) - -// Fuzz_static_resp_decrypt is a go-fuzz harness that decrypts arbitrary data -// with the responder. -func Fuzz_static_resp_decrypt(data []byte) int { - // This will return brontide machines with static keys. - initiator, responder := getStaticBrontideMachines() - - // Complete the brontide handshake. - completeHandshake(initiator, responder) - - // Create a reader with the byte array. - r := bytes.NewReader(data) - - // Decrypt the encrypted message using ReadMessage w/ responder machine. - if _, err := responder.ReadMessage(r); err == nil { - nilAndPanic(initiator, responder, nil) - } - - return 1 -} diff --git a/fuzz/brontide/static_resp_enc_dec.go b/fuzz/brontide/static_resp_enc_dec.go deleted file mode 100644 index 1f0f31b9a..000000000 --- a/fuzz/brontide/static_resp_enc_dec.go +++ /dev/null @@ -1,49 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "bytes" - "math" -) - -// Fuzz_static_resp_enc_dec is a go-fuzz harness that tests round-trip -// encryption and decryption between the responder and the initiator. -func Fuzz_static_resp_enc_dec(data []byte) int { - // Ensure that length of message is not greater than max allowed size. - if len(data) > math.MaxUint16 { - return 1 - } - - // This will return brontide machines with static keys. - initiator, responder := getStaticBrontideMachines() - - // Complete the brontide handshake. - completeHandshake(initiator, responder) - - var b bytes.Buffer - - // Encrypt the message using WriteMessage w/ responder machine. - if err := responder.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err) - } - - // Flush the encrypted message w/ responder machine. - if _, err := responder.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err) - } - - // Decrypt the ciphertext using ReadMessage w/ initiator machine. - plaintext, err := initiator.ReadMessage(&b) - if err != nil { - nilAndPanic(initiator, responder, err) - } - - // Check that the decrypted message and the original message are equal. - if !bytes.Equal(data, plaintext) { - nilAndPanic(initiator, responder, nil) - } - - return 1 -} diff --git a/fuzz/brontide/static_resp_encrypt.go b/fuzz/brontide/static_resp_encrypt.go deleted file mode 100644 index 9c957fb15..000000000 --- a/fuzz/brontide/static_resp_encrypt.go +++ /dev/null @@ -1,38 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package brontidefuzz - -import ( - "bytes" - "math" -) - -// Fuzz_static_resp_encrypt is a go-fuzz harness that encrypts arbitrary data -// with the responder. -func Fuzz_static_resp_encrypt(data []byte) int { - // Ensure that length of message is not greater than max allowed size. - if len(data) > math.MaxUint16 { - return 1 - } - - // This will return brontide machines with static keys. - initiator, responder := getStaticBrontideMachines() - - // Complete the brontide handshake. - completeHandshake(initiator, responder) - - var b bytes.Buffer - - // Encrypt the message using WriteMessage w/ responder machine. - if err := responder.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err) - } - - // Flush the encrypted message w/ responder machine. - if _, err := responder.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err) - } - - return 1 -} From 5a4856880669ff5afb18b9762d5de23d20a26195 Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Mon, 7 Nov 2022 14:58:46 -0600 Subject: [PATCH 5/7] brontide,lnwire,wtwire,zpay32: use CamelCase Rename fuzz test functions to use CamelCase. --- brontide/fuzz_test.go | 90 +++++++++++++++++----------------- lnwire/fuzz_test.go | 60 +++++++++++------------ watchtower/wtwire/fuzz_test.go | 16 +++--- zpay32/fuzz_test.go | 4 +- 4 files changed, 85 insertions(+), 85 deletions(-) diff --git a/brontide/fuzz_test.go b/brontide/fuzz_test.go index 5cc7278a7..2cea13c10 100644 --- a/brontide/fuzz_test.go +++ b/brontide/fuzz_test.go @@ -136,8 +136,8 @@ func getStaticBrontideMachines() (*Machine, *Machine) { return initiator, responder } -// Fuzz_random_actone fuzz tests ActOne in the brontide handshake. -func Fuzz_random_actone(f *testing.F) { +// FuzzRandomActOne fuzz tests ActOne in the brontide handshake. +func FuzzRandomActOne(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Check if data is large enough. if len(data) < ActOneSize { @@ -158,8 +158,8 @@ func Fuzz_random_actone(f *testing.F) { }) } -// Fuzz_random_actthree fuzz tests ActThree in the brontide handshake. -func Fuzz_random_actthree(f *testing.F) { +// FuzzRandomActThree fuzz tests ActThree in the brontide handshake. +func FuzzRandomActThree(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Check if data is large enough. if len(data) < ActThreeSize { @@ -200,8 +200,8 @@ func Fuzz_random_actthree(f *testing.F) { }) } -// Fuzz_random_acttwo fuzz tests ActTwo in the brontide handshake. -func Fuzz_random_acttwo(f *testing.F) { +// FuzzRandomActTwo fuzz tests ActTwo in the brontide handshake. +func FuzzRandomActTwo(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Check if data is large enough. if len(data) < ActTwoSize { @@ -231,9 +231,9 @@ func Fuzz_random_acttwo(f *testing.F) { }) } -// Fuzz_random_init_decrypt fuzz tests decrypting arbitrary data with the +// FuzzRandomInitDecrypt fuzz tests decrypting arbitrary data with the // initiator. -func Fuzz_random_init_decrypt(f *testing.F) { +func FuzzRandomInitDecrypt(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // This will return brontide machines with random keys. initiator, responder := getBrontideMachines() @@ -252,9 +252,9 @@ func Fuzz_random_init_decrypt(f *testing.F) { }) } -// Fuzz_random_init_enc_dec fuzz tests round-trip encryption and decryption -// between the initiator and the responder. -func Fuzz_random_init_enc_dec(f *testing.F) { +// FuzzRandomInitEncDec fuzz tests round-trip encryption and decryption between +// the initiator and the responder. +func FuzzRandomInitEncDec(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Ensure that length of message is not greater than max allowed // size. @@ -294,9 +294,9 @@ func Fuzz_random_init_enc_dec(f *testing.F) { }) } -// Fuzz_random_init_encrypt fuzz tests the encryption of arbitrary data with -// the initiator. -func Fuzz_random_init_encrypt(f *testing.F) { +// FuzzRandomInitEncrypt fuzz tests the encryption of arbitrary data with the +// initiator. +func FuzzRandomInitEncrypt(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Ensure that length of message is not greater than max allowed // size. @@ -324,9 +324,9 @@ func Fuzz_random_init_encrypt(f *testing.F) { }) } -// Fuzz_random_resp_decrypt fuzz tests the decryption of arbitrary data with -// the responder. -func Fuzz_random_resp_decrypt(f *testing.F) { +// FuzzRandomRespDecrypt fuzz tests the decryption of arbitrary data with the +// responder. +func FuzzRandomRespDecrypt(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // This will return brontide machines with random keys. initiator, responder := getBrontideMachines() @@ -345,9 +345,9 @@ func Fuzz_random_resp_decrypt(f *testing.F) { }) } -// Fuzz_random_resp_enc_dec fuzz tests round-trip encryption and decryption -// between the responder and the initiator. -func Fuzz_random_resp_enc_dec(f *testing.F) { +// FuzzRandomRespEncDec fuzz tests round-trip encryption and decryption between +// the responder and the initiator. +func FuzzRandomRespEncDec(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Ensure that length of message is not greater than max allowed // size. @@ -387,9 +387,9 @@ func Fuzz_random_resp_enc_dec(f *testing.F) { }) } -// Fuzz_random_resp_encrypt fuzz tests encryption of arbitrary data with the +// FuzzRandomRespEncrypt fuzz tests encryption of arbitrary data with the // responder. -func Fuzz_random_resp_encrypt(f *testing.F) { +func FuzzRandomRespEncrypt(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Ensure that length of message is not greater than max allowed // size. @@ -417,8 +417,8 @@ func Fuzz_random_resp_encrypt(f *testing.F) { }) } -// Fuzz_static_actone fuzz tests ActOne in the brontide handshake. -func Fuzz_static_actone(f *testing.F) { +// FuzzStaticActOne fuzz tests ActOne in the brontide handshake. +func FuzzStaticActOne(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Check if data is large enough. if len(data) < ActOneSize { @@ -439,8 +439,8 @@ func Fuzz_static_actone(f *testing.F) { }) } -// Fuzz_static_actthree fuzz tests ActThree in the brontide handshake. -func Fuzz_static_actthree(f *testing.F) { +// FuzzStaticActThree fuzz tests ActThree in the brontide handshake. +func FuzzStaticActThree(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Check if data is large enough. if len(data) < ActThreeSize { @@ -481,8 +481,8 @@ func Fuzz_static_actthree(f *testing.F) { }) } -// Fuzz_static_acttwo fuzz tests ActTwo in the brontide handshake. -func Fuzz_static_acttwo(f *testing.F) { +// FuzzStaticActTwo fuzz tests ActTwo in the brontide handshake. +func FuzzStaticActTwo(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Check if data is large enough. if len(data) < ActTwoSize { @@ -512,9 +512,9 @@ func Fuzz_static_acttwo(f *testing.F) { }) } -// Fuzz_static_init_decrypt fuzz tests the decryption of arbitrary data with -// the initiator. -func Fuzz_static_init_decrypt(f *testing.F) { +// FuzzStaticInitDecrypt fuzz tests the decryption of arbitrary data with the +// initiator. +func FuzzStaticInitDecrypt(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // This will return brontide machines with static keys. initiator, responder := getStaticBrontideMachines() @@ -533,9 +533,9 @@ func Fuzz_static_init_decrypt(f *testing.F) { }) } -// Fuzz_static_init_enc_dec fuzz tests round-trip encryption and decryption -// between the initiator and the responder. -func Fuzz_static_init_enc_dec(f *testing.F) { +// FuzzStaticInitEncDec fuzz tests round-trip encryption and decryption between +// the initiator and the responder. +func FuzzStaticInitEncDec(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Ensure that length of message is not greater than max allowed // size. @@ -576,9 +576,9 @@ func Fuzz_static_init_enc_dec(f *testing.F) { }) } -// Fuzz_static_init_encrypt fuzz tests the encryption of arbitrary data with -// the initiator. -func Fuzz_static_init_encrypt(f *testing.F) { +// FuzzStaticInitEncrypt fuzz tests the encryption of arbitrary data with the +// initiator. +func FuzzStaticInitEncrypt(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Ensure that length of message is not greater than max allowed // size. @@ -606,9 +606,9 @@ func Fuzz_static_init_encrypt(f *testing.F) { }) } -// Fuzz_static_resp_decrypt fuzz tests the decryption of arbitrary data with -// the responder. -func Fuzz_static_resp_decrypt(f *testing.F) { +// FuzzStaticRespDecrypt fuzz tests the decryption of arbitrary data with the +// responder. +func FuzzStaticRespDecrypt(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // This will return brontide machines with static keys. initiator, responder := getStaticBrontideMachines() @@ -627,9 +627,9 @@ func Fuzz_static_resp_decrypt(f *testing.F) { }) } -// Fuzz_static_resp_enc_dec fuzz tests the round-trip encryption and decryption +// FuzzStaticRespEncDec fuzz tests the round-trip encryption and decryption // between the responder and the initiator. -func Fuzz_static_resp_enc_dec(f *testing.F) { +func FuzzStaticRespEncDec(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Ensure that length of message is not greater than max allowed // size. @@ -670,9 +670,9 @@ func Fuzz_static_resp_enc_dec(f *testing.F) { }) } -// Fuzz_static_resp_encrypt fuzz tests the encryption of arbitrary data with -// the responder. -func Fuzz_static_resp_encrypt(f *testing.F) { +// FuzzStaticRespEncrypt fuzz tests the encryption of arbitrary data with the +// responder. +func FuzzStaticRespEncrypt(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Ensure that length of message is not greater than max allowed // size. diff --git a/lnwire/fuzz_test.go b/lnwire/fuzz_test.go index bbb48056a..95f6b5314 100644 --- a/lnwire/fuzz_test.go +++ b/lnwire/fuzz_test.go @@ -59,7 +59,7 @@ func harness(t *testing.T, data []byte) { } } -func Fuzz_accept_channel(f *testing.F) { +func FuzzAcceptChannel(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { data = prefixWithMsgType(data, MsgAcceptChannel) // Create a reader with the byte array. @@ -168,7 +168,7 @@ func Fuzz_accept_channel(f *testing.F) { }) } -func Fuzz_announce_signatures(f *testing.F) { +func FuzzAnnounceSignatures(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgAnnounceSignatures. data = prefixWithMsgType(data, MsgAnnounceSignatures) @@ -179,7 +179,7 @@ func Fuzz_announce_signatures(f *testing.F) { }) } -func Fuzz_channel_announcement(f *testing.F) { +func FuzzChannelAnnouncement(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgChannelAnnouncement. data = prefixWithMsgType(data, MsgChannelAnnouncement) @@ -190,7 +190,7 @@ func Fuzz_channel_announcement(f *testing.F) { }) } -func Fuzz_channel_reestablish(f *testing.F) { +func FuzzChannelReestablish(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgChannelReestablish. data = prefixWithMsgType(data, MsgChannelReestablish) @@ -201,7 +201,7 @@ func Fuzz_channel_reestablish(f *testing.F) { }) } -func Fuzz_channel_update(f *testing.F) { +func FuzzChannelUpdate(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgChannelUpdate. data = prefixWithMsgType(data, MsgChannelUpdate) @@ -212,7 +212,7 @@ func Fuzz_channel_update(f *testing.F) { }) } -func Fuzz_closing_signed(f *testing.F) { +func FuzzClosingSigned(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgClosingSigned. data = prefixWithMsgType(data, MsgClosingSigned) @@ -223,7 +223,7 @@ func Fuzz_closing_signed(f *testing.F) { }) } -func Fuzz_commit_sig(f *testing.F) { +func FuzzCommitSig(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgCommitSig. data = prefixWithMsgType(data, MsgCommitSig) @@ -234,7 +234,7 @@ func Fuzz_commit_sig(f *testing.F) { }) } -func Fuzz_error(f *testing.F) { +func FuzzError(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgError. data = prefixWithMsgType(data, MsgError) @@ -245,7 +245,7 @@ func Fuzz_error(f *testing.F) { }) } -func Fuzz_funding_created(f *testing.F) { +func FuzzFundingCreated(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgFundingCreated. data = prefixWithMsgType(data, MsgFundingCreated) @@ -256,7 +256,7 @@ func Fuzz_funding_created(f *testing.F) { }) } -func Fuzz_funding_locked(f *testing.F) { +func FuzzFundingLocked(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgFundingLocked. data = prefixWithMsgType(data, MsgFundingLocked) @@ -267,7 +267,7 @@ func Fuzz_funding_locked(f *testing.F) { }) } -func Fuzz_funding_signed(f *testing.F) { +func FuzzFundingSigned(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgFundingSigned. data = prefixWithMsgType(data, MsgFundingSigned) @@ -278,7 +278,7 @@ func Fuzz_funding_signed(f *testing.F) { }) } -func Fuzz_gossip_timestamp_range(f *testing.F) { +func FuzzGossipTimestampRange(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgGossipTimestampRange. data = prefixWithMsgType(data, MsgGossipTimestampRange) @@ -289,7 +289,7 @@ func Fuzz_gossip_timestamp_range(f *testing.F) { }) } -func Fuzz_init(f *testing.F) { +func FuzzInit(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgInit. data = prefixWithMsgType(data, MsgInit) @@ -300,7 +300,7 @@ func Fuzz_init(f *testing.F) { }) } -func Fuzz_node_announcement(f *testing.F) { +func FuzzNodeAnnouncement(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgNodeAnnouncement. data = prefixWithMsgType(data, MsgNodeAnnouncement) @@ -391,7 +391,7 @@ func Fuzz_node_announcement(f *testing.F) { }) } -func Fuzz_open_channel(f *testing.F) { +func FuzzOpenChannel(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgOpenChannel. data = prefixWithMsgType(data, MsgOpenChannel) @@ -524,7 +524,7 @@ func Fuzz_open_channel(f *testing.F) { }) } -func Fuzz_ping(f *testing.F) { +func FuzzPing(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgPing. data = prefixWithMsgType(data, MsgPing) @@ -535,7 +535,7 @@ func Fuzz_ping(f *testing.F) { }) } -func Fuzz_pong(f *testing.F) { +func FuzzPong(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgPong. data = prefixWithMsgType(data, MsgPong) @@ -546,7 +546,7 @@ func Fuzz_pong(f *testing.F) { }) } -func Fuzz_query_channel_range(f *testing.F) { +func FuzzQueryChannelRange(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgQueryChannelRange. data = prefixWithMsgType(data, MsgQueryChannelRange) @@ -557,7 +557,7 @@ func Fuzz_query_channel_range(f *testing.F) { }) } -func Fuzz_zlib_query_short_chan_ids(f *testing.F) { +func FuzzZlibQueryShortChanIDs(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { var buf bytes.Buffer zlibWriter := zlib.NewWriter(&buf) @@ -595,7 +595,7 @@ func Fuzz_zlib_query_short_chan_ids(f *testing.F) { }) } -func Fuzz_query_short_chan_ids(f *testing.F) { +func FuzzQueryShortChanIDs(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgQueryShortChanIDs. data = prefixWithMsgType(data, MsgQueryShortChanIDs) @@ -606,7 +606,7 @@ func Fuzz_query_short_chan_ids(f *testing.F) { }) } -func Fuzz_zlib_reply_channel_range(f *testing.F) { +func FuzzZlibReplyChannelRange(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { var buf bytes.Buffer zlibWriter := zlib.NewWriter(&buf) @@ -652,7 +652,7 @@ func Fuzz_zlib_reply_channel_range(f *testing.F) { }) } -func Fuzz_reply_channel_range(f *testing.F) { +func FuzzReplyChannelRange(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgReplyChannelRange. data = prefixWithMsgType(data, MsgReplyChannelRange) @@ -663,7 +663,7 @@ func Fuzz_reply_channel_range(f *testing.F) { }) } -func Fuzz_reply_short_chan_ids_end(f *testing.F) { +func FuzzReplyShortChanIDsEnd(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgReplyShortChanIDsEnd. data = prefixWithMsgType(data, MsgReplyShortChanIDsEnd) @@ -674,7 +674,7 @@ func Fuzz_reply_short_chan_ids_end(f *testing.F) { }) } -func Fuzz_revoke_and_ack(f *testing.F) { +func FuzzRevokeAndAck(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgRevokeAndAck. data = prefixWithMsgType(data, MsgRevokeAndAck) @@ -685,7 +685,7 @@ func Fuzz_revoke_and_ack(f *testing.F) { }) } -func Fuzz_shutdown(f *testing.F) { +func FuzzShutdown(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgShutdown. data = prefixWithMsgType(data, MsgShutdown) @@ -696,7 +696,7 @@ func Fuzz_shutdown(f *testing.F) { }) } -func Fuzz_update_add_htlc(f *testing.F) { +func FuzzUpdateAddHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgUpdateAddHTLC. data = prefixWithMsgType(data, MsgUpdateAddHTLC) @@ -707,7 +707,7 @@ func Fuzz_update_add_htlc(f *testing.F) { }) } -func Fuzz_update_fail_htlc(f *testing.F) { +func FuzzUpdateFailHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgUpdateFailHTLC. data = prefixWithMsgType(data, MsgUpdateFailHTLC) @@ -718,7 +718,7 @@ func Fuzz_update_fail_htlc(f *testing.F) { }) } -func Fuzz_update_fail_malformed_htlc(f *testing.F) { +func FuzzUpdateFailMalformedHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgUpdateFailMalformedHTLC. data = prefixWithMsgType(data, MsgUpdateFailMalformedHTLC) @@ -729,7 +729,7 @@ func Fuzz_update_fail_malformed_htlc(f *testing.F) { }) } -func Fuzz_update_fee(f *testing.F) { +func FuzzUpdateFee(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgUpdateFee. data = prefixWithMsgType(data, MsgUpdateFee) @@ -740,7 +740,7 @@ func Fuzz_update_fee(f *testing.F) { }) } -func Fuzz_update_fulfill_htlc(f *testing.F) { +func FuzzUpdateFulfillHTLC(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgUpdateFulFillHTLC. data = prefixWithMsgType(data, MsgUpdateFulfillHTLC) diff --git a/watchtower/wtwire/fuzz_test.go b/watchtower/wtwire/fuzz_test.go index 7a6004013..b3ca44b45 100644 --- a/watchtower/wtwire/fuzz_test.go +++ b/watchtower/wtwire/fuzz_test.go @@ -62,7 +62,7 @@ func harness(t *testing.T, data []byte, emptyMsg Message) { } } -func Fuzz_create_session_reply(f *testing.F) { +func FuzzCreateSessionReply(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgCreateSessionReply. data = prefixWithMsgType(data, MsgCreateSessionReply) @@ -77,7 +77,7 @@ func Fuzz_create_session_reply(f *testing.F) { }) } -func Fuzz_create_session(f *testing.F) { +func FuzzCreateSession(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgCreateSession. data = prefixWithMsgType(data, MsgCreateSession) @@ -92,7 +92,7 @@ func Fuzz_create_session(f *testing.F) { }) } -func Fuzz_delete_session_reply(f *testing.F) { +func FuzzDeleteSessionReply(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgDeleteSessionReply. data = prefixWithMsgType(data, MsgDeleteSessionReply) @@ -107,7 +107,7 @@ func Fuzz_delete_session_reply(f *testing.F) { }) } -func Fuzz_delete_session(f *testing.F) { +func FuzzDeleteSession(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgDeleteSession. data = prefixWithMsgType(data, MsgDeleteSession) @@ -122,7 +122,7 @@ func Fuzz_delete_session(f *testing.F) { }) } -func Fuzz_error(f *testing.F) { +func FuzzError(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgError. data = prefixWithMsgType(data, MsgError) @@ -137,7 +137,7 @@ func Fuzz_error(f *testing.F) { }) } -func Fuzz_init(f *testing.F) { +func FuzzInit(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgInit. data = prefixWithMsgType(data, MsgInit) @@ -152,7 +152,7 @@ func Fuzz_init(f *testing.F) { }) } -func Fuzz_state_update_reply(f *testing.F) { +func FuzzStateUpdateReply(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgStateUpdateReply. data = prefixWithMsgType(data, MsgStateUpdateReply) @@ -167,7 +167,7 @@ func Fuzz_state_update_reply(f *testing.F) { }) } -func Fuzz_state_update(f *testing.F) { +func FuzzStateUpdate(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { // Prefix with MsgStateUpdate. data = prefixWithMsgType(data, MsgStateUpdate) diff --git a/zpay32/fuzz_test.go b/zpay32/fuzz_test.go index f777bcce0..b008a2c3c 100644 --- a/zpay32/fuzz_test.go +++ b/zpay32/fuzz_test.go @@ -11,7 +11,7 @@ import ( "github.com/btcsuite/btcd/chaincfg/chainhash" ) -func Fuzz_decode(f *testing.F) { +func FuzzDecode(f *testing.F) { f.Fuzz(func(t *testing.T, data string) { inv, err := Decode(data, &chaincfg.TestNet3Params) if err != nil { @@ -25,7 +25,7 @@ func Fuzz_decode(f *testing.F) { }) } -func Fuzz_encode(f *testing.F) { +func FuzzEncode(f *testing.F) { f.Fuzz(func(t *testing.T, data string) { inv, err := Decode(data, &chaincfg.TestNet3Params) if err != nil { From e15d3e898ed5f4226ff46a41ef839a5f08cc70ad Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Mon, 7 Nov 2022 16:38:16 -0600 Subject: [PATCH 6/7] brontide,lnwire,wtwire,zpay32: appease linter Address all linter complaints for the newly migrated fuzz tests. --- brontide/fuzz_test.go | 126 ++++++++++++++++++--------------- lnwire/fuzz_test.go | 86 ++++++++++++++++------ watchtower/wtwire/fuzz_test.go | 3 + zpay32/fuzz_test.go | 6 +- 4 files changed, 140 insertions(+), 81 deletions(-) diff --git a/brontide/fuzz_test.go b/brontide/fuzz_test.go index 2cea13c10..91b5cd8dc 100644 --- a/brontide/fuzz_test.go +++ b/brontide/fuzz_test.go @@ -36,6 +36,7 @@ var ( } priv, _ := btcec.PrivKeyFromBytes(eBytes) + return priv, nil }) @@ -49,6 +50,7 @@ var ( } priv, _ := btcec.PrivKeyFromBytes(eBytes) + return priv, nil }) ) @@ -56,9 +58,11 @@ var ( // completeHandshake takes two brontide machines (initiator, responder) // and completes the brontide handshake between them. If any part of the // handshake fails, this function will panic. -func completeHandshake(initiator, responder *Machine, t *testing.T) { +func completeHandshake(t *testing.T, initiator, responder *Machine) { + t.Helper() + if err := handshake(initiator, responder); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } } @@ -96,7 +100,9 @@ func handshake(initiator, responder *Machine) error { // nilAndPanic first nils the initiator and responder's Curve fields and then // panics. -func nilAndPanic(initiator, responder *Machine, err error, t *testing.T) { +func nilAndPanic(t *testing.T, initiator, responder *Machine, err error) { + t.Helper() + t.Fatalf("error: %v, initiator: %v, responder: %v", err, spew.Sdump(initiator), spew.Sdump(responder)) } @@ -153,7 +159,7 @@ func FuzzRandomActOne(f *testing.F) { // Responder receives ActOne, should fail on the MAC check. if err := responder.RecvActOne(actOne); err == nil { - nilAndPanic(nil, responder, nil, t) + nilAndPanic(t, nil, responder, nil) } }) } @@ -172,12 +178,12 @@ func FuzzRandomActThree(f *testing.F) { // Generate ActOne and send to the responder. actOne, err := initiator.GenActOne() if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Receiving ActOne should succeed, so we panic on error. if err := responder.RecvActOne(actOne); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Generate ActTwo - this is not sent to the initiator because @@ -186,7 +192,7 @@ func FuzzRandomActThree(f *testing.F) { // the appropriate state in the responder machine. _, err = responder.GenActTwo() if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Copy data into [ActThreeSize]byte. @@ -195,7 +201,7 @@ func FuzzRandomActThree(f *testing.F) { // Responder receives ActThree, should fail on the MAC check. if err := responder.RecvActThree(actThree); err == nil { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -217,7 +223,7 @@ func FuzzRandomActTwo(f *testing.F) { // appropriate state in the initiator machine. _, err := initiator.GenActOne() if err != nil { - nilAndPanic(initiator, nil, err, t) + nilAndPanic(t, initiator, nil, err) } // Copy data into [ActTwoSize]byte. @@ -226,7 +232,7 @@ func FuzzRandomActTwo(f *testing.F) { // Initiator receives ActTwo, should fail. if err := initiator.RecvActTwo(actTwo); err == nil { - nilAndPanic(initiator, nil, nil, t) + nilAndPanic(t, initiator, nil, nil) } }) } @@ -239,7 +245,7 @@ func FuzzRandomInitDecrypt(f *testing.F) { initiator, responder := getBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) // Create a reader with the byte array. r := bytes.NewReader(data) @@ -247,7 +253,7 @@ func FuzzRandomInitDecrypt(f *testing.F) { // Decrypt the encrypted message using ReadMessage w/ initiator // machine. if _, err := initiator.ReadMessage(r); err == nil { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -266,30 +272,31 @@ func FuzzRandomInitEncDec(f *testing.F) { initiator, responder := getBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ initiator machine. if err := initiator.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ initiator machine. if _, err := initiator.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } - // Decrypt the ciphertext using ReadMessage w/ responder machine. + // Decrypt the ciphertext using ReadMessage w/ responder + // machine. plaintext, err := responder.ReadMessage(&b) if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Check that the decrypted message and the original message are // equal. if !bytes.Equal(data, plaintext) { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -308,18 +315,18 @@ func FuzzRandomInitEncrypt(f *testing.F) { initiator, responder := getBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ initiator machine. if err := initiator.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ initiator machine. if _, err := initiator.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } }) } @@ -332,7 +339,7 @@ func FuzzRandomRespDecrypt(f *testing.F) { initiator, responder := getBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) // Create a reader with the byte array. r := bytes.NewReader(data) @@ -340,7 +347,7 @@ func FuzzRandomRespDecrypt(f *testing.F) { // Decrypt the encrypted message using ReadMessage w/ responder // machine. if _, err := responder.ReadMessage(r); err == nil { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -359,30 +366,31 @@ func FuzzRandomRespEncDec(f *testing.F) { initiator, responder := getBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ responder machine. if err := responder.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ responder machine. if _, err := responder.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } - // Decrypt the ciphertext using ReadMessage w/ initiator machine. + // Decrypt the ciphertext using ReadMessage w/ initiator + // machine. plaintext, err := initiator.ReadMessage(&b) if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Check that the decrypted message and the original message are // equal. if !bytes.Equal(data, plaintext) { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -401,18 +409,18 @@ func FuzzRandomRespEncrypt(f *testing.F) { initiator, responder := getBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ responder machine. if err := responder.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ responder machine. if _, err := responder.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } }) } @@ -434,7 +442,7 @@ func FuzzStaticActOne(f *testing.F) { // Responder receives ActOne, should fail. if err := responder.RecvActOne(actOne); err == nil { - nilAndPanic(nil, responder, nil, t) + nilAndPanic(t, nil, responder, nil) } }) } @@ -453,12 +461,12 @@ func FuzzStaticActThree(f *testing.F) { // Generate ActOne and send to the responder. actOne, err := initiator.GenActOne() if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Receiving ActOne should succeed, so we panic on error. if err := responder.RecvActOne(actOne); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Generate ActTwo - this is not sent to the initiator because @@ -467,7 +475,7 @@ func FuzzStaticActThree(f *testing.F) { // the appropriate state in the responder machine. _, err = responder.GenActTwo() if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Copy data into [ActThreeSize]byte. @@ -476,7 +484,7 @@ func FuzzStaticActThree(f *testing.F) { // Responder receives ActThree, should fail. if err := responder.RecvActThree(actThree); err == nil { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -498,7 +506,7 @@ func FuzzStaticActTwo(f *testing.F) { // appropriate state in the initiator machine. _, err := initiator.GenActOne() if err != nil { - nilAndPanic(initiator, nil, err, t) + nilAndPanic(t, initiator, nil, err) } // Copy data into [ActTwoSize]byte. @@ -507,7 +515,7 @@ func FuzzStaticActTwo(f *testing.F) { // Initiator receives ActTwo, should fail. if err := initiator.RecvActTwo(actTwo); err == nil { - nilAndPanic(initiator, nil, nil, t) + nilAndPanic(t, initiator, nil, nil) } }) } @@ -520,7 +528,7 @@ func FuzzStaticInitDecrypt(f *testing.F) { initiator, responder := getStaticBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) // Create a reader with the byte array. r := bytes.NewReader(data) @@ -528,7 +536,7 @@ func FuzzStaticInitDecrypt(f *testing.F) { // Decrypt the encrypted message using ReadMessage w/ initiator // machine. if _, err := initiator.ReadMessage(r); err == nil { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -547,31 +555,31 @@ func FuzzStaticInitEncDec(f *testing.F) { initiator, responder := getStaticBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ initiator machine. if err := initiator.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ initiator machine. if _, err := initiator.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Decrypt the ciphertext using ReadMessage w/ responder // machine. plaintext, err := responder.ReadMessage(&b) if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Check that the decrypted message and the original message are // equal. if !bytes.Equal(data, plaintext) { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -590,18 +598,18 @@ func FuzzStaticInitEncrypt(f *testing.F) { initiator, responder := getStaticBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ initiator machine. if err := initiator.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ initiator machine. if _, err := initiator.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } }) } @@ -614,7 +622,7 @@ func FuzzStaticRespDecrypt(f *testing.F) { initiator, responder := getStaticBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) // Create a reader with the byte array. r := bytes.NewReader(data) @@ -622,7 +630,7 @@ func FuzzStaticRespDecrypt(f *testing.F) { // Decrypt the encrypted message using ReadMessage w/ responder // machine. if _, err := responder.ReadMessage(r); err == nil { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -641,31 +649,31 @@ func FuzzStaticRespEncDec(f *testing.F) { initiator, responder := getStaticBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ responder machine. if err := responder.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ responder machine. if _, err := responder.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Decrypt the ciphertext using ReadMessage w/ initiator // machine. plaintext, err := initiator.ReadMessage(&b) if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Check that the decrypted message and the original message are // equal. if !bytes.Equal(data, plaintext) { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -684,18 +692,18 @@ func FuzzStaticRespEncrypt(f *testing.F) { initiator, responder := getStaticBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ responder machine. if err := responder.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ responder machine. if _, err := responder.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } }) } diff --git a/lnwire/fuzz_test.go b/lnwire/fuzz_test.go index 95f6b5314..ed7088280 100644 --- a/lnwire/fuzz_test.go +++ b/lnwire/fuzz_test.go @@ -14,6 +14,7 @@ func prefixWithMsgType(data []byte, prefix MessageType) []byte { var prefixBytes [2]byte binary.BigEndian.PutUint16(prefixBytes[:], uint16(prefix)) data = append(prefixBytes[:], data...) + return data } @@ -22,6 +23,8 @@ func prefixWithMsgType(data []byte, prefix MessageType) []byte { // checks, is a valid message once deserialized, and passes a sequence of // serialization and deserialization checks. func harness(t *testing.T, data []byte) { + t.Helper() + // Create a reader with the byte array. r := bytes.NewReader(data) @@ -55,7 +58,8 @@ func harness(t *testing.T, data []byte) { if !reflect.DeepEqual(msg, newMsg) { // Deserialized message and original message are not deeply // equal. - t.Fatal("original message and deserialized message are not deeply equal") + t.Fatal("original message and deserialized message are not " + + "deeply equal") } } @@ -99,10 +103,18 @@ func FuzzAcceptChannel(f *testing.F) { // probably takes up more branches than necessary, but that's // fine for now. var shouldPanic bool - first := msg.(*AcceptChannel) - second := newMsg.(*AcceptChannel) + first, ok := msg.(*AcceptChannel) + if !ok { + t.Fatal("message was not AcceptChannel") + } + second, ok := newMsg.(*AcceptChannel) + if !ok { + t.Fatal("new message was not AcceptChannel") + } + + if !bytes.Equal(first.PendingChannelID[:], + second.PendingChannelID[:]) { - if !bytes.Equal(first.PendingChannelID[:], second.PendingChannelID[:]) { shouldPanic = true } @@ -146,7 +158,9 @@ func FuzzAcceptChannel(f *testing.F) { shouldPanic = true } - if !first.DelayedPaymentPoint.IsEqual(second.DelayedPaymentPoint) { + if !first.DelayedPaymentPoint.IsEqual( + second.DelayedPaymentPoint) { + shouldPanic = true } @@ -154,16 +168,21 @@ func FuzzAcceptChannel(f *testing.F) { shouldPanic = true } - if !first.FirstCommitmentPoint.IsEqual(second.FirstCommitmentPoint) { + if !first.FirstCommitmentPoint.IsEqual( + second.FirstCommitmentPoint) { + shouldPanic = true } - if !bytes.Equal(first.UpfrontShutdownScript, second.UpfrontShutdownScript) { + if !bytes.Equal(first.UpfrontShutdownScript, + second.UpfrontShutdownScript) { + shouldPanic = true } if shouldPanic { - t.Fatalf("original message and deseralized message are not equal") + t.Fatal("original message and deseralized message " + + "are not equal") } }) } @@ -344,8 +363,15 @@ func FuzzNodeAnnouncement(f *testing.F) { // Now compare every field instead of using reflect.DeepEqual // for the Addresses field. var shouldPanic bool - first := msg.(*NodeAnnouncement) - second := newMsg.(*NodeAnnouncement) + first, ok := msg.(*NodeAnnouncement) + if !ok { + t.Fatal("message was not NodeAnnouncement") + } + second, ok := newMsg.(*NodeAnnouncement) + if !ok { + t.Fatal("new message was not NodeAnnouncement") + } + if !bytes.Equal(first.Signature[:], second.Signature[:]) { shouldPanic = true } @@ -375,18 +401,23 @@ func FuzzNodeAnnouncement(f *testing.F) { } for i := range first.Addresses { - if first.Addresses[i].String() != second.Addresses[i].String() { + if first.Addresses[i].String() != + second.Addresses[i].String() { + shouldPanic = true break } } - if !reflect.DeepEqual(first.ExtraOpaqueData, second.ExtraOpaqueData) { + if !reflect.DeepEqual(first.ExtraOpaqueData, + second.ExtraOpaqueData) { + shouldPanic = true } if shouldPanic { - t.Fatal("original message and deserialized message are not equal") + t.Fatal("original message and deserialized message " + + "are not equal") } }) } @@ -439,14 +470,22 @@ func FuzzOpenChannel(f *testing.F) { // probably takes up more branches than necessary, but that's // fine for now. var shouldPanic bool - first := msg.(*OpenChannel) - second := newMsg.(*OpenChannel) + first, ok := msg.(*OpenChannel) + if !ok { + t.Fatal("message was not OpenChannel") + } + second, ok := newMsg.(*OpenChannel) + if !ok { + t.Fatal("new message was not OpenChannel") + } if !first.ChainHash.IsEqual(&second.ChainHash) { shouldPanic = true } - if !bytes.Equal(first.PendingChannelID[:], second.PendingChannelID[:]) { + if !bytes.Equal(first.PendingChannelID[:], + second.PendingChannelID[:]) { + shouldPanic = true } @@ -498,7 +537,9 @@ func FuzzOpenChannel(f *testing.F) { shouldPanic = true } - if !first.DelayedPaymentPoint.IsEqual(second.DelayedPaymentPoint) { + if !first.DelayedPaymentPoint.IsEqual( + second.DelayedPaymentPoint) { + shouldPanic = true } @@ -506,7 +547,9 @@ func FuzzOpenChannel(f *testing.F) { shouldPanic = true } - if !first.FirstCommitmentPoint.IsEqual(second.FirstCommitmentPoint) { + if !first.FirstCommitmentPoint.IsEqual( + second.FirstCommitmentPoint) { + shouldPanic = true } @@ -514,12 +557,15 @@ func FuzzOpenChannel(f *testing.F) { shouldPanic = true } - if !bytes.Equal(first.UpfrontShutdownScript, second.UpfrontShutdownScript) { + if !bytes.Equal(first.UpfrontShutdownScript, + second.UpfrontShutdownScript) { + shouldPanic = true } if shouldPanic { - t.Fatal("original message and deserialized message are not equal") + t.Fatal("original message and deserialized message " + + "are not equal") } }) } diff --git a/watchtower/wtwire/fuzz_test.go b/watchtower/wtwire/fuzz_test.go index b3ca44b45..06b0b3ffd 100644 --- a/watchtower/wtwire/fuzz_test.go +++ b/watchtower/wtwire/fuzz_test.go @@ -13,6 +13,7 @@ func prefixWithMsgType(data []byte, prefix MessageType) []byte { var prefixBytes [2]byte binary.BigEndian.PutUint16(prefixBytes[:], uint16(prefix)) data = append(prefixBytes[:], data...) + return data } @@ -22,6 +23,8 @@ func prefixWithMsgType(data []byte, prefix MessageType) []byte { // serialization and deserialization checks. Returns an int that determines // whether the input is unique or not. func harness(t *testing.T, data []byte, emptyMsg Message) { + t.Helper() + // Create a reader with the byte array. r := bytes.NewReader(data) diff --git a/zpay32/fuzz_test.go b/zpay32/fuzz_test.go index b008a2c3c..4d3a19a9c 100644 --- a/zpay32/fuzz_test.go +++ b/zpay32/fuzz_test.go @@ -39,7 +39,7 @@ func FuzzEncode(f *testing.F) { // Initialize the static key we will be using for this fuzz // test. - testPrivKeyBytes, _ := hex.DecodeString("e126f68f7eafcc8b74f54d269fe206be715000f94dac067d1c04a8ca3b2db734") + testPrivKeyBytes, _ := hex.DecodeString("e126f68f7eafcc8b74f54d269fe206be715000f94dac067d1c04a8ca3b2db734") // nolint:lll testPrivKey, _ := btcec.PrivKeyFromBytes(testPrivKeyBytes) // Then, initialize the testMessageSigner so we can encode out @@ -47,12 +47,14 @@ func FuzzEncode(f *testing.F) { testMessageSigner := MessageSigner{ SignCompact: func(msg []byte) ([]byte, error) { hash := chainhash.HashB(msg) - sig, err := ecdsa.SignCompact(testPrivKey, hash, true) + sig, err := ecdsa.SignCompact(testPrivKey, hash, + true) if err != nil { return nil, fmt.Errorf("can't sign the "+ "message: %v", err) } + return sig, nil }, } From 48c71d7f2b09e505dc6d94458253c119e0a08c5e Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Mon, 7 Nov 2022 16:48:05 -0600 Subject: [PATCH 7/7] docs: update release notes --- docs/release-notes/release-notes-0.16.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/release-notes/release-notes-0.16.0.md b/docs/release-notes/release-notes-0.16.0.md index c130b73c3..07056586a 100644 --- a/docs/release-notes/release-notes-0.16.0.md +++ b/docs/release-notes/release-notes-0.16.0.md @@ -132,7 +132,7 @@ certain large transactions](https://github.com/lightningnetwork/lnd/pull/7100). never-ending retry loop](https://github.com/lightningnetwork/lnd/pull/6766) * [Migrated from go-fuzz to Go 1.18's new standard fuzz testing - library](https://github.com/lightningnetwork/lnd/pull/6784). + library](https://github.com/lightningnetwork/lnd/pull/7127). ## `lncli` * [Add an `insecure` flag to skip tls auth as well as a `metadata` string slice