-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzz_test.go
202 lines (166 loc) · 4.77 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
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
package parser_test
// The fuzz tests in here are designed to fully exercise all our error handling, identify any
// cases we haven't handled, and to try and ensure that no parser ever panics.
import (
"math/rand"
"reflect"
"testing"
"unicode"
"github.com/FollowTheProcess/parser"
)
var chars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@£$%^&*()_+][';/.,]語ç日ð本Ê語")
var corpus = [...]string{
"",
"a normal sentence",
"日a本b語ç日ð本Ê語þ日¥本¼語i日©",
"\xf8\xa1\xa1\xa1\xa1",
"£$%^&*(((())))",
"91836347287",
"日ð本Ê語þ日¥本¼語i",
"✅🛠️🧠⚡️⚠️😎🪜",
"\n\n\r\n\t ",
}
func FuzzTake(f *testing.F) {
for _, item := range corpus {
f.Add(item, rand.Int())
}
f.Fuzz(func(t *testing.T, input string, n int) {
value, remainder, err := parser.Take(n)(input)
fuzzParser(t, value, remainder, err)
})
}
func FuzzExact(f *testing.F) {
for _, item := range corpus {
f.Add(item, randomString(5))
}
f.Fuzz(func(t *testing.T, input string, match string) {
value, remainder, err := parser.Exact(match)(input)
fuzzParser(t, value, remainder, err)
})
}
func FuzzExactCaseInsensitive(f *testing.F) {
for _, item := range corpus {
f.Add(item, randomString(5))
}
f.Fuzz(func(t *testing.T, input string, match string) {
value, remainder, err := parser.ExactCaseInsensitive(match)(input)
fuzzParser(t, value, remainder, err)
})
}
func FuzzChar(f *testing.F) {
for _, item := range corpus {
f.Add(item, randomRune())
}
f.Fuzz(func(t *testing.T, input string, char rune) {
value, remainder, err := parser.Char(char)(input)
fuzzParser(t, value, remainder, err)
})
}
func FuzzTakeWhile(f *testing.F) {
for _, item := range corpus {
f.Add(item)
}
f.Fuzz(func(t *testing.T, input string) {
value, remainder, err := parser.TakeWhile(unicode.IsLetter)(input)
fuzzParser(t, value, remainder, err)
})
}
func FuzzTakeUntil(f *testing.F) {
for _, item := range corpus {
f.Add(item)
}
f.Fuzz(func(t *testing.T, input string) {
value, remainder, err := parser.TakeUntil(unicode.IsSpace)(input)
fuzzParser(t, value, remainder, err)
})
}
func FuzzTakeWhileBetween(f *testing.F) {
for _, item := range corpus {
f.Add(item, rand.Intn(10), rand.Intn(10))
}
f.Fuzz(func(t *testing.T, input string, lower, upper int) {
value, remainder, err := parser.TakeWhileBetween(lower, upper, unicode.IsGraphic)(input)
fuzzParser(t, value, remainder, err)
})
}
func FuzzTakeTo(f *testing.F) {
for _, item := range corpus {
f.Add(item, randomString(5))
}
f.Fuzz(func(t *testing.T, input string, match string) {
value, remainder, err := parser.TakeTo(match)(input)
fuzzParser(t, value, remainder, err)
})
}
func FuzzOneOf(f *testing.F) {
for _, item := range corpus {
f.Add(item, randomString(rand.Intn(10)))
}
f.Fuzz(func(t *testing.T, input string, chars string) {
value, remainder, err := parser.OneOf(chars)(input)
fuzzParser(t, value, remainder, err)
})
}
func FuzzNoneOf(f *testing.F) {
for _, item := range corpus {
f.Add(item, randomString(rand.Intn(10)))
}
f.Fuzz(func(t *testing.T, input string, chars string) {
value, remainder, err := parser.NoneOf(chars)(input)
fuzzParser(t, value, remainder, err)
})
}
func FuzzAnyOf(f *testing.F) {
for _, item := range corpus {
f.Add(item, randomString(rand.Intn(10)))
}
f.Fuzz(func(t *testing.T, input string, chars string) {
value, remainder, err := parser.AnyOf(chars)(input)
fuzzParser(t, value, remainder, err)
})
}
func FuzzNotAnyOf(f *testing.F) {
for _, item := range corpus {
f.Add(item, randomString(rand.Intn(10)))
}
f.Fuzz(func(t *testing.T, input string, chars string) {
value, remainder, err := parser.NotAnyOf(chars)(input)
fuzzParser(t, value, remainder, err)
})
}
func FuzzOptional(f *testing.F) {
for _, item := range corpus {
f.Add(item, randomString(5))
}
f.Fuzz(func(t *testing.T, input string, match string) {
value, remainder, err := parser.Optional(match)(input)
fuzzParser(t, value, remainder, err)
})
}
// fuzzParser is a helper that asserts empty value and remainders were returned if the
// err was not nil.
func fuzzParser[T any](t *testing.T, value T, remainder string, err error) {
t.Helper()
var zero T // The zero value of type T
// If err is not nil, value and remainder must be empty
if err != nil {
if !reflect.DeepEqual(value, zero) {
t.Errorf("Value: %#v, Wanted: %#v", value, zero)
}
if !reflect.DeepEqual(remainder, zero) {
t.Errorf("Remainder: %#v, Wanted: %#v", remainder, zero)
}
}
}
// generate a random utf-8 string of length n.
func randomString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = chars[rand.Intn(len(chars))]
}
return string(b)
}
// generate a random utf-8 rune.
func randomRune() rune {
return chars[rand.Intn(len(chars))]
}