github.com/decred/dcrlnd@v0.7.6/fuzz/lnwire/open_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_open_channel is used by go-fuzz.
    13  func Fuzz_open_channel(data []byte) int {
    14  	// Prefix with MsgOpenChannel.
    15  	data = prefixWithMsgType(data, lnwire.MsgOpenChannel)
    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  		return 1
    35  	}
    36  
    37  	// We will serialize the message into a new bytes buffer.
    38  	var b bytes.Buffer
    39  	if _, err := lnwire.WriteMessage(&b, msg, 0); err != nil {
    40  		// Could not serialize message into bytes buffer, panic
    41  		panic(err)
    42  	}
    43  
    44  	// Deserialize the message from the serialized bytes buffer, and then
    45  	// assert that the original message is equal to the newly deserialized
    46  	// message.
    47  	newMsg, err := lnwire.ReadMessage(&b, 0)
    48  	if err != nil {
    49  		// Could not deserialize message from bytes buffer, panic
    50  		panic(err)
    51  	}
    52  
    53  	// Now compare every field instead of using reflect.DeepEqual.
    54  	// For UpfrontShutdownScript, we only compare bytes. This probably takes
    55  	// up more branches than necessary, but that's fine for now.
    56  	var shouldPanic bool
    57  	first := msg.(*lnwire.OpenChannel)
    58  	second := newMsg.(*lnwire.OpenChannel)
    59  
    60  	if !first.ChainHash.IsEqual(&second.ChainHash) {
    61  		shouldPanic = true
    62  	}
    63  
    64  	if !bytes.Equal(first.PendingChannelID[:], second.PendingChannelID[:]) {
    65  		shouldPanic = true
    66  	}
    67  
    68  	if first.FundingAmount != second.FundingAmount {
    69  		shouldPanic = true
    70  	}
    71  
    72  	if first.PushAmount != second.PushAmount {
    73  		shouldPanic = true
    74  	}
    75  
    76  	if first.DustLimit != second.DustLimit {
    77  		shouldPanic = true
    78  	}
    79  
    80  	if first.MaxValueInFlight != second.MaxValueInFlight {
    81  		shouldPanic = true
    82  	}
    83  
    84  	if first.ChannelReserve != second.ChannelReserve {
    85  		shouldPanic = true
    86  	}
    87  
    88  	if first.HtlcMinimum != second.HtlcMinimum {
    89  		shouldPanic = true
    90  	}
    91  
    92  	if first.FeePerKiloByte != second.FeePerKiloByte {
    93  		shouldPanic = true
    94  	}
    95  
    96  	if first.CsvDelay != second.CsvDelay {
    97  		shouldPanic = true
    98  	}
    99  
   100  	if first.MaxAcceptedHTLCs != second.MaxAcceptedHTLCs {
   101  		shouldPanic = true
   102  	}
   103  
   104  	if !first.FundingKey.IsEqual(second.FundingKey) {
   105  		shouldPanic = true
   106  	}
   107  
   108  	if !first.RevocationPoint.IsEqual(second.RevocationPoint) {
   109  		shouldPanic = true
   110  	}
   111  
   112  	if !first.PaymentPoint.IsEqual(second.PaymentPoint) {
   113  		shouldPanic = true
   114  	}
   115  
   116  	if !first.DelayedPaymentPoint.IsEqual(second.DelayedPaymentPoint) {
   117  		shouldPanic = true
   118  	}
   119  
   120  	if !first.HtlcPoint.IsEqual(second.HtlcPoint) {
   121  		shouldPanic = true
   122  	}
   123  
   124  	if !first.FirstCommitmentPoint.IsEqual(second.FirstCommitmentPoint) {
   125  		shouldPanic = true
   126  	}
   127  
   128  	if first.ChannelFlags != second.ChannelFlags {
   129  		shouldPanic = true
   130  	}
   131  
   132  	if !bytes.Equal(first.UpfrontShutdownScript, second.UpfrontShutdownScript) {
   133  		shouldPanic = true
   134  	}
   135  
   136  	if shouldPanic {
   137  		panic("original message and deserialized message are not equal")
   138  	}
   139  
   140  	// Add this input to the corpus.
   141  	return 1
   142  }