mirror of
https://github.com/lightningnetwork/lnd.git
synced 2024-11-19 09:53:54 +01:00
0b4e03f5fc
With go 1.17 a change to the build flags was implemented: https://go.googlesource.com/proposal/+/master/design/draft-gobuild.md The formatter now automatically adds the forward-compatible build tag format and the linter checks for them, so we need to include them in our code.
66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
//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
|
|
}
|