Repository navigation
Expand file tree
/
Copy pathfuzz_test.go
More file actions
361 lines (349 loc) · 8.37 KB
/
Copy pathfuzz_test.go
File metadata and controls
361 lines (349 loc) · 8.37 KB
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
package glob
import (
"errors"
"fmt"
"regexp"
"slices"
"strings"
"testing"
"unicode/utf8"
)
// FuzzMatchRegexp verifies Pattern.Match() against the regexp package.
//
// The fuzzer generates arbitrary (pattern, string, separators, globstar)
// inputs; each compilable pattern is translated into an equivalent anchored
// regular expression by translateGlob() -- a naive, independent
// re-implementation of the glob syntax -- and the match results are
// compared. Since the translator shares no code with Compile(), this
// validates the lexer, the parser, the compile-time rewrites and the
// matching engine all at once.
//
// Run with:
//
// go test -fuzz=FuzzMatchRegexp
//
// Without -fuzz only the seed corpus is run, as a regression test.
func FuzzMatchRegexp(f *testing.F) {
for _, seed := range []struct {
pattern string
fixture string
seps string // The separators, one rune each.
globstar bool
}{
{
pattern: "",
fixture: "",
},
{
pattern: "abc",
fixture: "abc",
},
{
pattern: "a*c",
fixture: "abbbc",
},
{
pattern: "a*a*a*a*b",
fixture: "aaaaaaab",
},
{
pattern: "**a*b",
fixture: "axc.ab",
seps: ".",
},
{
pattern: "*a**b",
fixture: "xa.b",
seps: ".",
},
{
pattern: "{*,**,?}",
fixture: "a.b",
seps: ".",
},
{
pattern: "{a,ab}c",
fixture: "abc",
},
{
pattern: "{*.google.*,yandex.*}",
fixture: "www.google.com",
seps: ".",
},
{
pattern: "{https://*gobwas.com,http://exclude.gobwas.com}",
fixture: "https://safe.gobwas.com",
},
{
pattern: "[!a]*",
fixture: "this is a test3",
},
{
pattern: "[a-z][!a-x]*cat*[h][!b]*eyes*",
fixture: "my cat has very bright eyes",
},
{
pattern: `\*`,
fixture: "*",
},
{
pattern: "*ä",
fixture: "åä",
},
{
pattern: "{,a}{a,}a",
fixture: "a",
},
{
pattern: "{{a,b},c}d",
fixture: "bd",
},
{
// A star inside an alternative must not discard the restart
// point of a star outside of it; see the starsFloor handling
// in matchContext.storeStar().
pattern: "*{*0,}",
fixture: "1",
},
{
// U+FFFD is a valid character, not an invalid byte: the class
// must match it, and the pattern must compile.
pattern: "[!a]",
fixture: "�",
},
{
pattern: "[�]*",
fixture: "�x",
},
{
// Globstar: a leading `**.` may match zero segments.
pattern: "**.x",
fixture: "x",
seps: ".",
globstar: true,
},
{
pattern: "a.**.b",
fixture: "a.b",
seps: ".",
globstar: true,
},
{
// Adjacent `**/` coalesce only with the same separator:
// `{**.,}{**/,}x` is not `{**.,}x`.
pattern: "**.**/x",
fixture: "a.b/x",
seps: "./",
globstar: true,
},
{
pattern: "**.**/x",
fixture: "x",
seps: "./",
globstar: true,
},
{
// An escaped separator is a separator too.
pattern: `**\.**.x`,
fixture: "a.x",
seps: ".",
globstar: true,
},
} {
f.Add(seed.pattern, seed.fixture, seed.seps, seed.globstar)
}
f.Fuzz(func(t *testing.T, pattern, s, seps string, globstar bool) {
if len(pattern) > 32 || len(s) > 64 {
t.Skip("too large")
}
if !utf8.ValidString(pattern) || !utf8.ValidString(s) || !utf8.ValidString(seps) {
// The regexp package normalizes invalid UTF-8; the comparison
// makes no sense for it.
t.Skip("invalid utf8")
}
if len(seps) > 3 || strings.ContainsAny(seps, `*?[]{}\\,!-`) {
// A meta character as a separator is a degenerate case the
// naive oracle does not define; the rest is enough.
t.Skip("odd separators")
}
sep := []rune(seps)
if len(sep) > 0 {
if strings.Count(pattern, "*") > 4 {
// Backtracking over many separator-limited stars is
// combinatorial in the worst case; keep the fuzzing fast.
t.Skip("too many stars")
}
}
if globstar && strings.ContainsAny(pattern, "{}") {
// The oracle below does not track the segment starts across
// the alternatives; TestGlobstar covers them.
t.Skip("globstar with braces")
}
p, err := Compiler{Separators: sep, Globstar: globstar}.Compile(pattern)
if err != nil {
t.Skip("does not compile")
}
reStr, err := translateGlob(pattern, sep, globstar)
if err != nil {
t.Fatalf("Compile() accepted %#q but the translator did not: %v", pattern, err)
}
re, err := regexp.Compile(reStr)
if err != nil {
t.Fatalf("bad regexp translation of %#q: %v", pattern, err)
}
var (
got = p.Match(s)
want = re.MatchString(s)
)
if got != want {
t.Errorf(
"sep=%q globstar=%t pattern=%#q (regexp %#q) string=%#q: Match()=%t, regexp=%t",
string(sep), globstar, pattern, re, s, got, want,
)
}
})
}
// translateGlob translates the glob pattern into an equivalent anchored
// regular expression. It is intentionally independent from Compile(): a naive
// rune-by-rune re-implementation of the documented syntax, sharing no code
// with the lexer or the parser, to serve as the differential oracle.
//
// With globstar set, a run of two or more stars that begins the pattern or
// follows a separator, and is followed by a (possibly escaped) separator,
// is an optional run of segments. Braces are not supported in this mode.
func translateGlob(pattern string, sep []rune, globstar bool) (string, error) {
var sb strings.Builder
sb.WriteString("^(?:")
rs := []rune(pattern)
depth := 0
for i := 0; i < len(rs); i++ {
switch r := rs[i]; r {
case '\\':
// A trailing backslash does not compile, so it never gets here.
if i+1 < len(rs) {
i++
sb.WriteString(regexp.QuoteMeta(string(rs[i])))
}
case '*':
// Adjacent stars are one star: the most general of them.
at := i
for i+1 < len(rs) && rs[i+1] == '*' {
i++
}
if at == i {
sb.WriteString(sepClassRegexp(sep) + `*`)
break
}
if globstar && (at == 0 || slices.Contains(sep, rs[at-1])) {
j := i + 1
if j < len(rs) && rs[j] == '\\' {
j++
}
if j < len(rs) && slices.Contains(sep, rs[j]) {
sb.WriteString(`(?:(?s:.*)` + regexp.QuoteMeta(string(rs[j])) + `)?`)
i = j
break
}
}
sb.WriteString(`(?s:.*)`)
case '?':
sb.WriteString(sepClassRegexp(sep))
case '{':
depth++
sb.WriteString(`(?:`)
case '}':
if depth == 0 {
// A literal outside of braces.
sb.WriteString(regexp.QuoteMeta(`}`))
break
}
depth--
sb.WriteString(`)`)
case ',':
if depth == 0 {
// A literal outside of braces.
sb.WriteString(`,`)
break
}
sb.WriteString(`|`)
case '[':
n, err := translateClass(&sb, rs[i:])
if err != nil {
return "", err
}
i += n - 1
default:
sb.WriteString(regexp.QuoteMeta(string(r)))
}
}
if depth != 0 {
return "", errors.New("unclosed `{`")
}
sb.WriteString(`)$`)
return sb.String(), nil
}
// translateClass translates the `[...]` character class rs begins with and
// returns the number of runes consumed. Mirroring the syntax, a class is
// either a range `[a-z]` or a set `[abc]`, both optionally negated with the
// leading `!`; the range boundary characters are taken verbatim, the set
// characters may be escaped.
func translateClass(sb *strings.Builder, rs []rune) (int, error) {
i := 1 // Skip the `[`.
not := ""
if i < len(rs) && rs[i] == '!' {
not = "^"
i++
}
if i+1 < len(rs) && rs[i+1] == '-' {
// A range.
if i+3 >= len(rs) || rs[i+3] != ']' {
return 0, errors.New("unclosed range")
}
lo, hi := rs[i], rs[i+2]
if hi < lo {
return 0, errors.New("range hi character is less than lo")
}
fmt.Fprintf(sb, `[%s\x{%x}-\x{%x}]`, not, lo, hi)
return i + 4, nil
}
// A set.
var chars []rune
escaped := false
for ; i < len(rs); i++ {
r := rs[i]
switch {
case escaped:
chars = append(chars, r)
escaped = false
case r == '\\':
escaped = true
case r == ']':
if len(chars) == 0 {
return 0, errors.New("empty class")
}
sb.WriteString("[" + not)
for _, c := range chars {
fmt.Fprintf(sb, `\x{%x}`, c)
}
sb.WriteString("]")
return i + 1, nil
default:
chars = append(chars, r)
}
}
return 0, errors.New("unclosed `[`")
}
// sepClassRegexp returns a class matching any single rune except the
// separators, e.g. `[^\x{2e}]` -- or the match-all when there are none.
func sepClassRegexp(sep []rune) string {
if len(sep) == 0 {
return `(?s:.)`
}
var sb strings.Builder
sb.WriteString("[^")
for _, r := range sep {
fmt.Fprintf(&sb, `\x{%x}`, r)
}
sb.WriteString("]")
return sb.String()
}