|
| 1 | +package cipher_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/dcadolph/cipher" |
| 9 | +) |
| 10 | + |
| 11 | +// FuzzMatchExt feeds arbitrary extension lists and paths into the |
| 12 | +// MatchExt builder. Any input that compiles must not panic when |
| 13 | +// Match is called. |
| 14 | +func FuzzMatchExt(f *testing.F) { |
| 15 | + f.Add("yaml,yml,json", "secrets/prod.yaml") |
| 16 | + f.Add("", "noext") |
| 17 | + f.Add(".YAML", "MIXED.yaml") |
| 18 | + f.Fuzz(func(t *testing.T, exts, path string) { |
| 19 | + parts := strings.Split(exts, ",") |
| 20 | + m := cipher.MatchExt(parts...) |
| 21 | + _ = m.Match(path) |
| 22 | + }) |
| 23 | +} |
| 24 | + |
| 25 | +// FuzzMatchGlob feeds arbitrary glob patterns and paths. Patterns |
| 26 | +// that fail to compile return an error and are skipped; patterns |
| 27 | +// that compile must never panic on Match. |
| 28 | +func FuzzMatchGlob(f *testing.F) { |
| 29 | + f.Add("**/*.yaml", "a/b/c.yaml") |
| 30 | + f.Add("secrets/*", "secrets/prod") |
| 31 | + f.Add("[", "anything") |
| 32 | + f.Fuzz(func(t *testing.T, pattern, path string) { |
| 33 | + m, err := cipher.MatchGlob(pattern) |
| 34 | + if err != nil { |
| 35 | + return |
| 36 | + } |
| 37 | + _ = m.Match(path) |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +// FuzzMatchRegex feeds arbitrary regexes and paths. Patterns that |
| 42 | +// fail to compile are skipped. Compiled patterns must never panic on |
| 43 | +// Match. |
| 44 | +func FuzzMatchRegex(f *testing.F) { |
| 45 | + f.Add(`\.yaml$`, "x.yaml") |
| 46 | + f.Add(`^secrets/`, "secrets/db") |
| 47 | + f.Add(`(?P<g>foo)`, "foo") |
| 48 | + f.Fuzz(func(t *testing.T, pattern, path string) { |
| 49 | + re, err := regexp.Compile(pattern) |
| 50 | + if err != nil { |
| 51 | + return |
| 52 | + } |
| 53 | + m := cipher.MatchRegex(re) |
| 54 | + _ = m.Match(path) |
| 55 | + }) |
| 56 | +} |
0 commit comments