github.com/decred/dcrlnd@v0.7.6/fuzz/lnwire/accept_channel.go (about) 1 //go:build gofuzz 2 // +build gofuzz 3 4 package lnwirefuzz 5 6 import ( 7 "bytes" 8 9 "github.com/decred/dcrlnd/lnwire" 10 ) 11 12 // Fuzz_accept_channel is used by go-fuzz. 13 func Fuzz_accept_channel(data []byte) int { 14 // Prefix with MsgAcceptChannel. 15 data = prefixWithMsgType(data, lnwire.MsgAcceptChannel) 16 17 // We have to do this here instead of in fuzz.Harness so that 18 // reflect.DeepEqual isn't called. Because of the UpfrontShutdownScript 19 // encoding, the first message and second message aren't deeply equal since 20 // the first has a nil slice and the other has an empty slice. 21 22 // Create a reader with the byte array. 23 r := bytes.NewReader(data) 24 25 // Make sure byte array length (excluding 2 bytes for message type) is 26 // less than max payload size for the wire message. 27 payloadLen := uint32(len(data)) - 2 28 if payloadLen > lnwire.MaxMsgBody { 29 return 1 30 } 31 32 msg, err := lnwire.ReadMessage(r, 0) 33 if err != nil { 34 // go-fuzz generated []byte that cannot be represented as a 35 // wire message but we will return 0 so go-fuzz can modify the 36 // input. 37 return 1 38 } 39 40 // We will serialize the message into a new bytes buffer. 41 var b bytes.Buffer 42 if _, err := lnwire.WriteMessage(&b, msg, 0); err != nil { 43 // Could not serialize message into bytes buffer, panic 44 panic(err) 45 } 46 47 // Deserialize the message from the serialized bytes buffer, and then 48 // assert that the original message is equal to the newly deserialized 49 // message. 50 newMsg, err := lnwire.ReadMessage(&b, 0) 51 if err != nil { 52 // Could not deserialize message from bytes buffer, panic 53 panic(err) 54 } 55 56 // Now compare every field instead of using reflect.DeepEqual. 57 // For UpfrontShutdownScript, we only compare bytes. This probably takes 58 // up more branches than necessary, but that's fine for now. 59 var shouldPanic bool 60 first := msg.(*lnwire.AcceptChannel) 61 second := newMsg.(*lnwire.AcceptChannel) 62 63 if !bytes.Equal(first.PendingChannelID[:], second.PendingChannelID[:]) { 64 shouldPanic = true 65 } 66 67 if first.DustLimit != second.DustLimit { 68 shouldPanic = true 69 } 70 71 if first.MaxValueInFlight != second.MaxValueInFlight { 72 shouldPanic = true 73 } 74 75 if first.ChannelReserve != second.ChannelReserve { 76 shouldPanic = true 77 } 78 79 if first.HtlcMinimum != second.HtlcMinimum { 80 shouldPanic = true 81 } 82 83 if first.MinAcceptDepth != second.MinAcceptDepth { 84 shouldPanic = true 85 } 86 87 if first.CsvDelay != second.CsvDelay { 88 shouldPanic = true 89 } 90 91 if first.MaxAcceptedHTLCs != second.MaxAcceptedHTLCs { 92 shouldPanic = true 93 } 94 95 if !first.FundingKey.IsEqual(second.FundingKey) { 96 shouldPanic = true 97 } 98 99 if !first.RevocationPoint.IsEqual(second.RevocationPoint) { 100 shouldPanic = true 101 } 102 103 if !first.PaymentPoint.IsEqual(second.PaymentPoint) { 104 shouldPanic = true 105 } 106 107 if !first.DelayedPaymentPoint.IsEqual(second.DelayedPaymentPoint) { 108 shouldPanic = true 109 } 110 111 if !first.HtlcPoint.IsEqual(second.HtlcPoint) { 112 shouldPanic = true 113 } 114 115 if !first.FirstCommitmentPoint.IsEqual(second.FirstCommitmentPoint) { 116 shouldPanic = true 117 } 118 119 if !bytes.Equal(first.UpfrontShutdownScript, second.UpfrontShutdownScript) { 120 shouldPanic = true 121 } 122 123 if shouldPanic { 124 panic("original message and deserialized message are not equal") 125 } 126 127 // Add this input to the corpus. 128 return 1 129 }