github.com/decred/dcrlnd@v0.7.6/fuzz/lnwire/node_announcement.go (about)

     1  //go:build gofuzz
     2  // +build gofuzz
     3  
     4  package lnwirefuzz
     5  
     6  import (
     7  	"bytes"
     8  	"reflect"
     9  
    10  	"github.com/decred/dcrlnd/lnwire"
    11  )
    12  
    13  // Fuzz_node_announcement is used by go-fuzz.
    14  func Fuzz_node_announcement(data []byte) int {
    15  	// Prefix with MsgNodeAnnouncement.
    16  	data = prefixWithMsgType(data, lnwire.MsgNodeAnnouncement)
    17  
    18  	// We have to do this here instead of in fuzz.Harness so that
    19  	// reflect.DeepEqual isn't called. Address (de)serialization messes up
    20  	// the fuzzing assertions.
    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 for the
    54  	// Addresses field.
    55  	var shouldPanic bool
    56  	first := msg.(*lnwire.NodeAnnouncement)
    57  	second := newMsg.(*lnwire.NodeAnnouncement)
    58  	if !bytes.Equal(first.Signature[:], second.Signature[:]) {
    59  		shouldPanic = true
    60  	}
    61  
    62  	if !reflect.DeepEqual(first.Features, second.Features) {
    63  		shouldPanic = true
    64  	}
    65  
    66  	if first.Timestamp != second.Timestamp {
    67  		shouldPanic = true
    68  	}
    69  
    70  	if !bytes.Equal(first.NodeID[:], second.NodeID[:]) {
    71  		shouldPanic = true
    72  	}
    73  
    74  	if !reflect.DeepEqual(first.RGBColor, second.RGBColor) {
    75  		shouldPanic = true
    76  	}
    77  
    78  	if !bytes.Equal(first.Alias[:], second.Alias[:]) {
    79  		shouldPanic = true
    80  	}
    81  
    82  	if len(first.Addresses) != len(second.Addresses) {
    83  		shouldPanic = true
    84  	}
    85  
    86  	for i := range first.Addresses {
    87  		if first.Addresses[i].String() != second.Addresses[i].String() {
    88  			shouldPanic = true
    89  			break
    90  		}
    91  	}
    92  
    93  	if !reflect.DeepEqual(first.ExtraOpaqueData, second.ExtraOpaqueData) {
    94  		shouldPanic = true
    95  	}
    96  
    97  	if shouldPanic {
    98  		panic("original message and deserialized message are not equal")
    99  	}
   100  
   101  	// Add this input to the corpus.
   102  	return 1
   103  }