2022-07-29 18:19:01 -04:00
|
|
|
package wtwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"testing"
|
2023-05-19 12:07:49 -05:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-07-29 18:19:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 MessageType) []byte {
|
|
|
|
var prefixBytes [2]byte
|
|
|
|
binary.BigEndian.PutUint16(prefixBytes[:], uint16(prefix))
|
|
|
|
data = append(prefixBytes[:], data...)
|
2022-11-07 16:38:16 -06:00
|
|
|
|
2022-07-29 18:19:01 -04:00
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2024-11-13 15:31:05 -06:00
|
|
|
// wireMsgHarness 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
|
2024-11-13 15:50:11 -06:00
|
|
|
// serialization and deserialization checks. emptyMsg must be an empty Message
|
|
|
|
// of the type to be fuzzed, as it is used to determine the appropriate prefix
|
|
|
|
// bytes and max payload length for decoding.
|
2024-11-13 15:31:05 -06:00
|
|
|
func wireMsgHarness(t *testing.T, data []byte, emptyMsg Message) {
|
2022-11-07 16:38:16 -06:00
|
|
|
t.Helper()
|
|
|
|
|
2024-11-13 15:28:06 -06:00
|
|
|
// Make sure byte array length is less than max payload size for the
|
|
|
|
// wire message.
|
|
|
|
payloadLen := uint32(len(data))
|
2022-07-29 18:19:01 -04:00
|
|
|
if payloadLen > emptyMsg.MaxPayloadLength(0) {
|
|
|
|
// Ignore this input - max payload constraint violated.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-13 15:28:06 -06:00
|
|
|
data = prefixWithMsgType(data, emptyMsg.MsgType())
|
|
|
|
|
|
|
|
// Create a reader with the byte array.
|
|
|
|
r := bytes.NewReader(data)
|
|
|
|
|
2022-07-29 18:19:01 -04:00
|
|
|
msg, err := ReadMessage(r, 0)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// We will serialize the message into a new bytes buffer.
|
|
|
|
var b bytes.Buffer
|
2023-05-19 12:07:49 -05:00
|
|
|
_, err = WriteMessage(&b, msg, 0)
|
|
|
|
require.NoError(t, err)
|
2022-07-29 18:19:01 -04:00
|
|
|
|
|
|
|
// Deserialize the message from the serialized bytes buffer, and then
|
|
|
|
// assert that the original message is equal to the newly deserialized
|
|
|
|
// message.
|
|
|
|
newMsg, err := ReadMessage(&b, 0)
|
2023-05-19 12:07:49 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, msg, newMsg)
|
2022-07-29 18:19:01 -04:00
|
|
|
}
|
|
|
|
|
2022-11-07 14:58:46 -06:00
|
|
|
func FuzzCreateSessionReply(f *testing.F) {
|
2022-07-29 18:19:01 -04:00
|
|
|
f.Fuzz(func(t *testing.T, data []byte) {
|
2024-11-13 15:50:11 -06:00
|
|
|
wireMsgHarness(t, data, &CreateSessionReply{})
|
2022-07-29 18:19:01 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-07 14:58:46 -06:00
|
|
|
func FuzzCreateSession(f *testing.F) {
|
2022-07-29 18:19:01 -04:00
|
|
|
f.Fuzz(func(t *testing.T, data []byte) {
|
2024-11-13 15:50:11 -06:00
|
|
|
wireMsgHarness(t, data, &CreateSession{})
|
2022-07-29 18:19:01 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-07 14:58:46 -06:00
|
|
|
func FuzzDeleteSessionReply(f *testing.F) {
|
2022-07-29 18:19:01 -04:00
|
|
|
f.Fuzz(func(t *testing.T, data []byte) {
|
2024-11-13 15:50:11 -06:00
|
|
|
wireMsgHarness(t, data, &DeleteSessionReply{})
|
2022-07-29 18:19:01 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-07 14:58:46 -06:00
|
|
|
func FuzzDeleteSession(f *testing.F) {
|
2022-07-29 18:19:01 -04:00
|
|
|
f.Fuzz(func(t *testing.T, data []byte) {
|
2024-11-13 15:50:11 -06:00
|
|
|
wireMsgHarness(t, data, &DeleteSession{})
|
2022-07-29 18:19:01 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-07 14:58:46 -06:00
|
|
|
func FuzzError(f *testing.F) {
|
2022-07-29 18:19:01 -04:00
|
|
|
f.Fuzz(func(t *testing.T, data []byte) {
|
2024-11-13 15:50:11 -06:00
|
|
|
wireMsgHarness(t, data, &Error{})
|
2022-07-29 18:19:01 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-07 14:58:46 -06:00
|
|
|
func FuzzInit(f *testing.F) {
|
2022-07-29 18:19:01 -04:00
|
|
|
f.Fuzz(func(t *testing.T, data []byte) {
|
2024-11-13 15:50:11 -06:00
|
|
|
wireMsgHarness(t, data, &Init{})
|
2022-07-29 18:19:01 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-07 14:58:46 -06:00
|
|
|
func FuzzStateUpdateReply(f *testing.F) {
|
2022-07-29 18:19:01 -04:00
|
|
|
f.Fuzz(func(t *testing.T, data []byte) {
|
2024-11-13 15:50:11 -06:00
|
|
|
wireMsgHarness(t, data, &StateUpdateReply{})
|
2022-07-29 18:19:01 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-07 14:58:46 -06:00
|
|
|
func FuzzStateUpdate(f *testing.F) {
|
2022-07-29 18:19:01 -04:00
|
|
|
f.Fuzz(func(t *testing.T, data []byte) {
|
2024-11-13 15:50:11 -06:00
|
|
|
wireMsgHarness(t, data, &StateUpdate{})
|
2022-07-29 18:19:01 -04:00
|
|
|
})
|
|
|
|
}
|