generated from WillAbides/goproject-tmpl
-
Notifications
You must be signed in to change notification settings - Fork 6
/
fuzzers.go
381 lines (340 loc) · 11 KB
/
fuzzers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package rjson
import (
"encoding/json"
"fmt"
)
type fuzzer struct {
name string
fn func([]byte) (int, error)
}
var fuzzers = []fuzzer{
{name: "fuzzReadFloat64", fn: fuzzReadFloat64},
{name: "fuzzReadUint64", fn: fuzzReadUint64},
{name: "fuzzReadUint32", fn: fuzzReadUint32},
{name: "fuzzReadUint", fn: fuzzReadUint},
{name: "fuzzReadInt64", fn: fuzzReadInt64},
{name: "fuzzReadInt32", fn: fuzzReadInt32},
{name: "fuzzReadInt", fn: fuzzReadInt},
{name: "fuzzReadString", fn: fuzzReadString},
{name: "fuzzReadStringBytes", fn: fuzzReadStringBytes},
{name: "fuzzReadBool", fn: fuzzReadBool},
{name: "fuzzReadNull", fn: fuzzReadNull},
{name: "fuzzSkipValue", fn: fuzzSkipValue},
{name: "fuzzValid", fn: fuzzValid},
{name: "fuzzNextToken", fn: fuzzNextToken},
{name: "fuzzReadArray", fn: fuzzReadArray},
{name: "fuzzReadObject", fn: fuzzReadObject},
{name: "fuzzReadValue", fn: fuzzReadValue},
{name: "fuzzDecodeFloat64", fn: fuzzDecodeFloat64},
{name: "fuzzDecodeUint64", fn: fuzzDecodeUint64},
{name: "fuzzDecodeUint32", fn: fuzzDecodeUint32},
{name: "fuzzDecodeUint", fn: fuzzDecodeUint},
{name: "fuzzDecodeInt64", fn: fuzzDecodeInt64},
{name: "fuzzDecodeInt32", fn: fuzzDecodeInt32},
{name: "fuzzDecodeInt", fn: fuzzDecodeInt},
{name: "fuzzDecodeString", fn: fuzzDecodeString},
{name: "fuzzDecodeBool", fn: fuzzDecodeBool},
{name: "fuzzHandleArrayValues", fn: fuzzHandleArrayValues},
{name: "fuzzHandleObjectValues", fn: fuzzHandleObjectValues},
}
func fuzzHandleArrayValues(data []byte) (int, error) {
var buf Buffer
validArray := false
valid := Valid(data, &buf)
if valid {
tkn, _, err := NextTokenType(data)
validArray = err == nil && tkn == ArrayStartType
}
for name, handlerFunc := range arrayHandlers {
wantNoErr := validArray && (name == "alwaysZero" || name == "skipValue")
_, err := HandleArrayValues(data, handlerFunc, &buf)
if wantNoErr && err != nil {
return 0, err
}
for i := 2; i <= 4; i++ {
h := nCallArrayValueHandler{
handler: handlerFunc,
n: i,
}
_, err = HandleArrayValues(data, &h, nil)
if wantNoErr && err != nil {
return 0, err
}
}
}
return 0, nil
}
func fuzzHandleObjectValues(data []byte) (int, error) {
var buf Buffer
for _, handlerFunc := range arrayHandlers {
hh := ObjectValueHandlerFunc(func(_, data []byte) (p int, err error) {
return handlerFunc(data)
})
_, err := HandleObjectValues(data, hh, &buf)
_ = err //nolint:errcheck // just checking for panic
for i := 2; i <= 4; i++ {
h := nCallArrayValueHandler{
handler: handlerFunc,
n: i,
}
hh = func(_, data []byte) (p int, err error) {
return h.HandleArrayValue(data)
}
_, err := HandleObjectValues(data, hh, nil)
_ = err //nolint:errcheck // just checking for panic
}
}
return 0, nil
}
func fuzzReadUint64(data []byte) (int, error) {
want, wantP, wantErr := readUint64Compat(data)
got, gotP, gotErr := ReadUint64(data)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzDecodeUint64(data []byte) (int, error) {
var want, got uint64
wantP, wantErr := decodeUint64Compat(data, &want)
gotP, gotErr := DecodeUint64(data, &got)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzReadUint32(data []byte) (int, error) {
want, wantP, wantErr := readUint32Compat(data)
got, gotP, gotErr := ReadUint32(data)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzDecodeUint32(data []byte) (int, error) {
var want, got uint32
wantP, wantErr := decodeUint32Compat(data, &want)
gotP, gotErr := DecodeUint32(data, &got)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzReadUint(data []byte) (int, error) {
want, wantP, wantErr := readUintCompat(data)
got, gotP, gotErr := ReadUint(data)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzDecodeUint(data []byte) (int, error) {
var want, got uint
wantP, wantErr := decodeUintCompat(data, &want)
gotP, gotErr := DecodeUint(data, &got)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzReadInt64(data []byte) (int, error) {
want, wantP, wantErr := readInt64Compat(data)
got, gotP, gotErr := ReadInt64(data)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzDecodeInt64(data []byte) (int, error) {
var want, got int64
wantP, wantErr := decodeInt64Compat(data, &want)
gotP, gotErr := DecodeInt64(data, &got)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzReadInt32(data []byte) (int, error) {
want, wantP, wantErr := readInt32Compat(data)
got, gotP, gotErr := ReadInt32(data)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzDecodeInt32(data []byte) (int, error) {
var want, got int32
wantP, wantErr := decodeInt32Compat(data, &want)
gotP, gotErr := DecodeInt32(data, &got)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzReadInt(data []byte) (int, error) {
want, wantP, wantErr := readIntCompat(data)
got, gotP, gotErr := ReadInt(data)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzDecodeInt(data []byte) (int, error) {
var want, got int
wantP, wantErr := decodeIntCompat(data, &want)
gotP, gotErr := DecodeInt(data, &got)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzReadFloat64(data []byte) (int, error) {
want, wantP, wantErr := readFloat64Compat(data)
got, gotP, gotErr := ReadFloat64(data)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzDecodeFloat64(data []byte) (int, error) {
var want, got float64
wantP, wantErr := decodeFloat64Compat(data, &want)
gotP, gotErr := DecodeFloat64(data, &got)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzReadString(data []byte) (int, error) {
want, wantP, wantErr := readStringCompat(data)
got, gotP, gotErr := ReadString(data, nil)
got = StdLibCompatibleString(got)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
if err != nil {
return 0, err
}
// try again with a dirty buffer
got, gotP, gotErr = ReadString(data, dirtyStringBuffer())
got = StdLibCompatibleString(got)
err = checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzDecodeString(data []byte) (int, error) {
var want, got string
wantP, wantErr := decodeStringCompat(data, &want)
gotP, gotErr := DecodeString(data, &got, nil)
got = StdLibCompatibleString(got)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
if err != nil {
return 0, err
}
// try again with a dirty buffer
gotP, gotErr = DecodeString(data, &got, dirtyStringBuffer())
got = StdLibCompatibleString(got)
err = checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzReadStringBytes(data []byte) (int, error) {
want, wantP, wantErr := readStringBytesCompat(data)
gotBytes, gotP, gotErr := ReadStringBytes(data, nil)
got := StdLibCompatibleString(string(gotBytes))
err := checkFuzzResults(string(want), got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzReadBool(data []byte) (int, error) {
want, wantP, wantErr := readBoolCompat(data)
got, gotP, gotErr := ReadBool(data)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzDecodeBool(data []byte) (int, error) {
var want, got bool
wantP, wantErr := decodeBoolCompat(data, &want)
gotP, gotErr := DecodeBool(data, &got)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzReadNull(data []byte) (int, error) {
wantP, wantErr := readNullCompat(data)
gotP, gotErr := ReadNull(data)
err := checkFuzzResults(nil, nil, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzSkipValue(data []byte) (int, error) {
wantP, wantErr := skipValueCompat(data)
var buf Buffer
gotP, gotErr := SkipValue(data, &buf)
err := checkFuzzResults(nil, nil, wantP, gotP, wantErr, gotErr)
if err != nil {
return 0, err
}
// try again with nil buffer
gotP, gotErr = SkipValue(data, &buf)
err = checkFuzzResults(nil, nil, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzValid(data []byte) (int, error) {
want := json.Valid(data)
var buf Buffer
got := Valid(data, &buf)
err := checkFuzzResults(want, got, 0, 0, nil, nil)
if err != nil {
return 0, err
}
// try again with nil buffer
got = Valid(data, nil)
err = checkFuzzResults(want, got, 0, 0, nil, nil)
return 0, err
}
func fuzzNextToken(data []byte) (int, error) {
want, wantP, wantErr := nextTokenCompat(data)
got, gotP, gotErr := NextToken(data)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzNextTokenType(data []byte) (int, error) {
want, wantP, wantErr := nextTokenTypeCompat(data)
got, gotP, gotErr := NextTokenType(data)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzReadArray(data []byte) (int, error) {
want, wantP, wantErr := readArrayCompat(data)
got, gotP, gotErr := ReadArray(data)
got = StdLibCompatibleSlice(got)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
if err != nil {
return 0, err
}
// try again with a ValueReader
got, gotP, gotErr = (&ValueReader{}).ReadArray(data)
got = StdLibCompatibleSlice(got)
err = checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzReadObject(data []byte) (int, error) {
want, wantP, wantErr := readObjectCompat(data)
got, gotP, gotErr := ReadObject(data)
got = StdLibCompatibleMap(got)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
if err != nil {
return 0, err
}
// try again with a ValueReader
got, gotP, gotErr = (&ValueReader{}).ReadObject(data)
got = StdLibCompatibleMap(got)
err = checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func fuzzReadValue(data []byte) (int, error) {
want, wantP, wantErr := readValueCompat(data)
got, gotP, gotErr := ReadValue(data)
got = stdLibCompatibleValue(got)
err := checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
if err != nil {
return 0, err
}
// try again with a ValueReader
got, gotP, gotErr = (&ValueReader{}).ReadValue(data)
got = stdLibCompatibleValue(got)
err = checkFuzzResults(want, got, wantP, gotP, wantErr, gotErr)
return 0, err
}
func checkFuzzResults(want, got interface{}, wantP, gotP int, wantErr, gotErr error) error {
err := checkFuzzErrors(wantErr, gotErr)
if err != nil {
return err
}
err = fuzzCompare(want, got)
if err != nil {
return err
}
if wantErr == nil && gotP != wantP {
return fmt.Errorf("expected p=%d, but got p=%d", wantP, gotP)
}
return nil
}
func checkFuzzErrors(wantErr, gotErr error) error {
if wantErr != nil {
if gotErr == nil {
return fmt.Errorf("we got no error but json got: %v", wantErr)
}
return nil
}
if gotErr != nil {
return fmt.Errorf("json got no error but we did: %v", gotErr)
}
return nil
}