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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package plan
import (
"testing"
"time"
)
// FuzzExpand: expanding any template against any file name never panics,
// gives the same answer twice, and never succeeds with a {N} beyond the
// capture groups there are - MaxIndex and Expand read placeholders alike.
func FuzzExpand(f *testing.F) {
for _, s := range []string{"{name}", "{stem}{ext}", "{mtime:%Y/%m}", "{1}_{2}", "{{literal}}", "{", "}", "{now:%", "{0}", "{9}", "{99999999999999999999}"} {
f.Add(s, "a.b.pdf")
}
f.Fuzz(func(t *testing.T, tmpl, name string) {
facts := Facts{
Name: name,
Captures: []string{name, "x", ".."},
ModTime: time.Date(2026, 8, 15, 12, 0, 0, 0, time.UTC),
Now: time.Date(2026, 9, 14, 0, 0, 0, 0, time.UTC),
}
a, errA := Expand(tmpl, facts)
b, errB := Expand(tmpl, facts)
if a != b || (errA == nil) != (errB == nil) {
t.Fatalf("Expand(%q) differs between two calls: %q/%v, %q/%v", tmpl, a, errA, b, errB)
}
if n, err := MaxIndex(tmpl); err == nil && n > 2 && errA == nil {
t.Fatalf("Expand(%q) = %q, though it uses {%d} and only 2 groups exist", tmpl, a, n)
}
})
}
|