Skip to content

Commit 411ad1d

Browse files
authored
Merge pull request #9271 from morehouse/simplify_fuzz_wtwire
wtwire: simply fuzz targets
2 parents c136901 + 9dd9212 commit 411ad1d

File tree

1 file changed

+23
-92
lines changed

1 file changed

+23
-92
lines changed

watchtower/wtwire/fuzz_test.go

Lines changed: 23 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,28 @@ func prefixWithMsgType(data []byte, prefix MessageType) []byte {
1818
return data
1919
}
2020

21-
// harness performs the actual fuzz testing of the appropriate wire message.
22-
// This function will check that the passed-in message passes wire length
23-
// checks, is a valid message once deserialized, and passes a sequence of
24-
// serialization and deserialization checks. Returns an int that determines
25-
// whether the input is unique or not.
26-
func harness(t *testing.T, data []byte, emptyMsg Message) {
21+
// wireMsgHarness performs the actual fuzz testing of the appropriate wire
22+
// message. This function will check that the passed-in message passes wire
23+
// length checks, is a valid message once deserialized, and passes a sequence of
24+
// serialization and deserialization checks. emptyMsg must be an empty Message
25+
// of the type to be fuzzed, as it is used to determine the appropriate prefix
26+
// bytes and max payload length for decoding.
27+
func wireMsgHarness(t *testing.T, data []byte, emptyMsg Message) {
2728
t.Helper()
2829

29-
// Create a reader with the byte array.
30-
r := bytes.NewReader(data)
31-
32-
// Make sure byte array length (excluding 2 bytes for message type) is
33-
// less than max payload size for the wire message.
34-
payloadLen := uint32(len(data)) - 2
30+
// Make sure byte array length is less than max payload size for the
31+
// wire message.
32+
payloadLen := uint32(len(data))
3533
if payloadLen > emptyMsg.MaxPayloadLength(0) {
3634
// Ignore this input - max payload constraint violated.
3735
return
3836
}
3937

38+
data = prefixWithMsgType(data, emptyMsg.MsgType())
39+
40+
// Create a reader with the byte array.
41+
r := bytes.NewReader(data)
42+
4043
msg, err := ReadMessage(r, 0)
4144
if err != nil {
4245
return
@@ -57,120 +60,48 @@ func harness(t *testing.T, data []byte, emptyMsg Message) {
5760

5861
func FuzzCreateSessionReply(f *testing.F) {
5962
f.Fuzz(func(t *testing.T, data []byte) {
60-
// Prefix with MsgCreateSessionReply.
61-
data = prefixWithMsgType(data, MsgCreateSessionReply)
62-
63-
// Create an empty message so that the FuzzHarness func can
64-
// check if the max payload constraint is violated.
65-
emptyMsg := CreateSessionReply{}
66-
67-
// Pass the message into our general fuzz harness for wire
68-
// messages!
69-
harness(t, data, &emptyMsg)
63+
wireMsgHarness(t, data, &CreateSessionReply{})
7064
})
7165
}
7266

7367
func FuzzCreateSession(f *testing.F) {
7468
f.Fuzz(func(t *testing.T, data []byte) {
75-
// Prefix with MsgCreateSession.
76-
data = prefixWithMsgType(data, MsgCreateSession)
77-
78-
// Create an empty message so that the FuzzHarness func can
79-
// check if the max payload constraint is violated.
80-
emptyMsg := CreateSession{}
81-
82-
// Pass the message into our general fuzz harness for wire
83-
// messages!
84-
harness(t, data, &emptyMsg)
69+
wireMsgHarness(t, data, &CreateSession{})
8570
})
8671
}
8772

8873
func FuzzDeleteSessionReply(f *testing.F) {
8974
f.Fuzz(func(t *testing.T, data []byte) {
90-
// Prefix with MsgDeleteSessionReply.
91-
data = prefixWithMsgType(data, MsgDeleteSessionReply)
92-
93-
// Create an empty message so that the FuzzHarness func can
94-
// check if the max payload constraint is violated.
95-
emptyMsg := DeleteSessionReply{}
96-
97-
// Pass the message into our general fuzz harness for wire
98-
// messages!
99-
harness(t, data, &emptyMsg)
75+
wireMsgHarness(t, data, &DeleteSessionReply{})
10076
})
10177
}
10278

10379
func FuzzDeleteSession(f *testing.F) {
10480
f.Fuzz(func(t *testing.T, data []byte) {
105-
// Prefix with MsgDeleteSession.
106-
data = prefixWithMsgType(data, MsgDeleteSession)
107-
108-
// Create an empty message so that the FuzzHarness func can
109-
// check if the max payload constraint is violated.
110-
emptyMsg := DeleteSession{}
111-
112-
// Pass the message into our general fuzz harness for wire
113-
// messages!
114-
harness(t, data, &emptyMsg)
81+
wireMsgHarness(t, data, &DeleteSession{})
11582
})
11683
}
11784

11885
func FuzzError(f *testing.F) {
11986
f.Fuzz(func(t *testing.T, data []byte) {
120-
// Prefix with MsgError.
121-
data = prefixWithMsgType(data, MsgError)
122-
123-
// Create an empty message so that the FuzzHarness func can
124-
// check if the max payload constraint is violated.
125-
emptyMsg := Error{}
126-
127-
// Pass the message into our general fuzz harness for wire
128-
// messages!
129-
harness(t, data, &emptyMsg)
87+
wireMsgHarness(t, data, &Error{})
13088
})
13189
}
13290

13391
func FuzzInit(f *testing.F) {
13492
f.Fuzz(func(t *testing.T, data []byte) {
135-
// Prefix with MsgInit.
136-
data = prefixWithMsgType(data, MsgInit)
137-
138-
// Create an empty message so that the FuzzHarness func can
139-
// check if the max payload constraint is violated.
140-
emptyMsg := Init{}
141-
142-
// Pass the message into our general fuzz harness for wire
143-
// messages!
144-
harness(t, data, &emptyMsg)
93+
wireMsgHarness(t, data, &Init{})
14594
})
14695
}
14796

14897
func FuzzStateUpdateReply(f *testing.F) {
14998
f.Fuzz(func(t *testing.T, data []byte) {
150-
// Prefix with MsgStateUpdateReply.
151-
data = prefixWithMsgType(data, MsgStateUpdateReply)
152-
153-
// Create an empty message so that the FuzzHarness func can
154-
// check if the max payload constraint is violated.
155-
emptyMsg := StateUpdateReply{}
156-
157-
// Pass the message into our general fuzz harness for wire
158-
// messages!
159-
harness(t, data, &emptyMsg)
99+
wireMsgHarness(t, data, &StateUpdateReply{})
160100
})
161101
}
162102

163103
func FuzzStateUpdate(f *testing.F) {
164104
f.Fuzz(func(t *testing.T, data []byte) {
165-
// Prefix with MsgStateUpdate.
166-
data = prefixWithMsgType(data, MsgStateUpdate)
167-
168-
// Create an empty message so that the FuzzHarness func can
169-
// check if the max payload constraint is violated.
170-
emptyMsg := StateUpdate{}
171-
172-
// Pass the message into our general fuzz harness for wire
173-
// messages!
174-
harness(t, data, &emptyMsg)
105+
wireMsgHarness(t, data, &StateUpdate{})
175106
})
176107
}

0 commit comments

Comments
 (0)