-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzz_test.go
116 lines (109 loc) · 3.98 KB
/
fuzz_test.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
//go:build go1.18
// +build go1.18
package sfv
import (
"reflect"
"testing"
)
func FuzzDecodeItem(f *testing.F) {
addFuzzingData(f)
f.Fuzz(func(t *testing.T, field string) {
item, err := DecodeItem([]string{field})
if err != nil {
t.Skip(field)
}
field2, err := EncodeItem(item)
if err != nil {
t.Fatal(err)
}
item2, err := DecodeItem([]string{field2})
if err != nil {
t.Fatalf("DecodeItem failed to decode %q: %v", field2, err)
}
if !reflect.DeepEqual(item, item2) {
t.Errorf("DecodeItem different query after being encoded\nbefore: %v\nafter: %v", item, item2)
}
})
}
func FuzzDecodeList(f *testing.F) {
addFuzzingData(f)
f.Fuzz(func(t *testing.T, field string) {
list, err := DecodeList([]string{field})
if err != nil {
t.Skip(field)
}
field2, err := EncodeList(list)
if err != nil {
t.Fatal(err)
}
list2, err := DecodeList([]string{field2})
if err != nil {
t.Fatalf("DecodeList failed to decode %q: %v", field2, err)
}
if !reflect.DeepEqual(list, list2) {
t.Errorf("DecodeList different query after being encoded\nbefore: %v\nafter: %v", list, list2)
}
})
}
func FuzzDecodeDictionary(f *testing.F) {
addFuzzingData(f)
f.Fuzz(func(t *testing.T, field string) {
dict, err := DecodeDictionary([]string{field})
if err != nil {
t.Skip(field)
}
field2, err := EncodeDictionary(dict)
if err != nil {
t.Fatal(err)
}
dict2, err := DecodeDictionary([]string{field2})
if err != nil {
t.Fatalf("DecodeDictionary failed to decode %q: %v", field2, err)
}
if !reflect.DeepEqual(dict, dict2) {
t.Errorf("DecodeDictionary different query after being encoded\nbefore: %v\nafter: %v", dict, dict2)
}
})
}
func addFuzzingData(f *testing.F) {
addFuzzingDataFile(f, "./testdata/extra.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/binary.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/binary.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/binary.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/boolean.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/date.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/dictionary.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/display-string.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/examples.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/item.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/key-generated.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/large-generated.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/list.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/number-generated.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/number.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/param-dict.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/param-list.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/param-listlist.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/serialisation-tests/key-generated.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/serialisation-tests/number.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/serialisation-tests/string-generated.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/serialisation-tests/token-generated.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/string-generated.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/string.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/token-generated.json")
addFuzzingDataFile(f, "./testdata/structured-field-tests/token.json")
}
func addFuzzingDataFile(f *testing.F, filename string) {
cases, err := readTestCases(filename)
if err != nil {
f.Fatalf("failed to read %q: %v", filename, err)
}
for _, tt := range cases {
if len(tt.Raw) == 1 {
f.Add(tt.Raw[0])
}
if len(tt.Canonical) == 1 {
f.Add(tt.Canonical[0])
}
}
}