mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-05-03 16:30:18 +02:00
Merge pull request #9271 from morehouse/simplify_fuzz_wtwire
wtwire: simply fuzz targets
This commit is contained in:
commit
411ad1d9cd
@ -18,25 +18,28 @@ func prefixWithMsgType(data []byte, prefix MessageType) []byte {
|
|||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
// harness performs the actual fuzz testing of the appropriate wire message.
|
// wireMsgHarness performs the actual fuzz testing of the appropriate wire
|
||||||
// This function will check that the passed-in message passes wire length
|
// message. This function will check that the passed-in message passes wire
|
||||||
// checks, is a valid message once deserialized, and passes a sequence of
|
// length checks, is a valid message once deserialized, and passes a sequence of
|
||||||
// serialization and deserialization checks. Returns an int that determines
|
// serialization and deserialization checks. emptyMsg must be an empty Message
|
||||||
// whether the input is unique or not.
|
// of the type to be fuzzed, as it is used to determine the appropriate prefix
|
||||||
func harness(t *testing.T, data []byte, emptyMsg Message) {
|
// bytes and max payload length for decoding.
|
||||||
|
func wireMsgHarness(t *testing.T, data []byte, emptyMsg Message) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
// Create a reader with the byte array.
|
// Make sure byte array length is less than max payload size for the
|
||||||
r := bytes.NewReader(data)
|
// wire message.
|
||||||
|
payloadLen := uint32(len(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) {
|
if payloadLen > emptyMsg.MaxPayloadLength(0) {
|
||||||
// Ignore this input - max payload constraint violated.
|
// Ignore this input - max payload constraint violated.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data = prefixWithMsgType(data, emptyMsg.MsgType())
|
||||||
|
|
||||||
|
// Create a reader with the byte array.
|
||||||
|
r := bytes.NewReader(data)
|
||||||
|
|
||||||
msg, err := ReadMessage(r, 0)
|
msg, err := ReadMessage(r, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -57,120 +60,48 @@ func harness(t *testing.T, data []byte, emptyMsg Message) {
|
|||||||
|
|
||||||
func FuzzCreateSessionReply(f *testing.F) {
|
func FuzzCreateSessionReply(f *testing.F) {
|
||||||
f.Fuzz(func(t *testing.T, data []byte) {
|
f.Fuzz(func(t *testing.T, data []byte) {
|
||||||
// Prefix with MsgCreateSessionReply.
|
wireMsgHarness(t, data, &CreateSessionReply{})
|
||||||
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 FuzzCreateSession(f *testing.F) {
|
func FuzzCreateSession(f *testing.F) {
|
||||||
f.Fuzz(func(t *testing.T, data []byte) {
|
f.Fuzz(func(t *testing.T, data []byte) {
|
||||||
// Prefix with MsgCreateSession.
|
wireMsgHarness(t, data, &CreateSession{})
|
||||||
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 FuzzDeleteSessionReply(f *testing.F) {
|
func FuzzDeleteSessionReply(f *testing.F) {
|
||||||
f.Fuzz(func(t *testing.T, data []byte) {
|
f.Fuzz(func(t *testing.T, data []byte) {
|
||||||
// Prefix with MsgDeleteSessionReply.
|
wireMsgHarness(t, data, &DeleteSessionReply{})
|
||||||
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 FuzzDeleteSession(f *testing.F) {
|
func FuzzDeleteSession(f *testing.F) {
|
||||||
f.Fuzz(func(t *testing.T, data []byte) {
|
f.Fuzz(func(t *testing.T, data []byte) {
|
||||||
// Prefix with MsgDeleteSession.
|
wireMsgHarness(t, data, &DeleteSession{})
|
||||||
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 FuzzError(f *testing.F) {
|
func FuzzError(f *testing.F) {
|
||||||
f.Fuzz(func(t *testing.T, data []byte) {
|
f.Fuzz(func(t *testing.T, data []byte) {
|
||||||
// Prefix with MsgError.
|
wireMsgHarness(t, data, &Error{})
|
||||||
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 FuzzInit(f *testing.F) {
|
func FuzzInit(f *testing.F) {
|
||||||
f.Fuzz(func(t *testing.T, data []byte) {
|
f.Fuzz(func(t *testing.T, data []byte) {
|
||||||
// Prefix with MsgInit.
|
wireMsgHarness(t, data, &Init{})
|
||||||
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 FuzzStateUpdateReply(f *testing.F) {
|
func FuzzStateUpdateReply(f *testing.F) {
|
||||||
f.Fuzz(func(t *testing.T, data []byte) {
|
f.Fuzz(func(t *testing.T, data []byte) {
|
||||||
// Prefix with MsgStateUpdateReply.
|
wireMsgHarness(t, data, &StateUpdateReply{})
|
||||||
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 FuzzStateUpdate(f *testing.F) {
|
func FuzzStateUpdate(f *testing.F) {
|
||||||
f.Fuzz(func(t *testing.T, data []byte) {
|
f.Fuzz(func(t *testing.T, data []byte) {
|
||||||
// Prefix with MsgStateUpdate.
|
wireMsgHarness(t, data, &StateUpdate{})
|
||||||
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)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user