|
| 1 | +package vibes |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func FuzzCompileScriptDoesNotPanic(f *testing.F) { |
| 10 | + f.Add([]byte("")) |
| 11 | + f.Add([]byte("def run() 1 end")) |
| 12 | + f.Add([]byte("def broken(")) |
| 13 | + f.Add([]byte("begin\n raise(\"boom\")\nrescue\n 1\nend")) |
| 14 | + f.Add([]byte("require(\"../path\")")) |
| 15 | + |
| 16 | + f.Fuzz(func(t *testing.T, raw []byte) { |
| 17 | + engine := MustNewEngine(Config{}) |
| 18 | + _, _ = engine.Compile(string(raw)) |
| 19 | + }) |
| 20 | +} |
| 21 | + |
| 22 | +func FuzzRuntimeEdgeCasesDoNotPanic(f *testing.F) { |
| 23 | + engine := MustNewEngine(Config{}) |
| 24 | + script, err := engine.Compile(` |
| 25 | +def run(text, pattern) |
| 26 | + stable_groups = text.split("").group_by_stable do |char| |
| 27 | + if char == "" |
| 28 | + :empty |
| 29 | + else |
| 30 | + :present |
| 31 | + end |
| 32 | + end |
| 33 | +
|
| 34 | + { |
| 35 | + match: Regex.match(pattern, text), |
| 36 | + replace_all: Regex.replace_all(text, pattern, "x"), |
| 37 | + json: JSON.stringify({ text: text }), |
| 38 | + template: "value={{text}}".template({ text: text }), |
| 39 | + chunks: text.split("").chunk(4), |
| 40 | + windows: text.split("").window(2), |
| 41 | + stable_groups: stable_groups |
| 42 | + } |
| 43 | +end |
| 44 | +`) |
| 45 | + if err != nil { |
| 46 | + f.Fatalf("compile failed: %v", err) |
| 47 | + } |
| 48 | + |
| 49 | + f.Add("", "a*") |
| 50 | + f.Add("ID-12 ID-34", "ID-[0-9]+") |
| 51 | + f.Add(strings.Repeat("a", 256), "(") |
| 52 | + f.Add(" hello \n world ", "\\s+") |
| 53 | + f.Add("{}", ".*") |
| 54 | + |
| 55 | + f.Fuzz(func(t *testing.T, text string, pattern string) { |
| 56 | + if len(text) > 4096 { |
| 57 | + text = text[:4096] |
| 58 | + } |
| 59 | + if len(pattern) > 1024 { |
| 60 | + pattern = pattern[:1024] |
| 61 | + } |
| 62 | + _, _ = script.Call(context.Background(), "run", []Value{ |
| 63 | + NewString(text), |
| 64 | + NewString(pattern), |
| 65 | + }, CallOptions{}) |
| 66 | + }) |
| 67 | +} |
0 commit comments