Skip to content

Commit 692a253

Browse files
committed
Update fuzz_test.go
1 parent a3bd3a1 commit 692a253

13 files changed

+110
-3478
lines changed

fuzz_test.go

Lines changed: 94 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,121 @@
11
package expr_test
22

33
import (
4-
"os"
54
"regexp"
65
"strings"
76
"testing"
87

98
"github.com/antonmedv/expr"
10-
"github.com/antonmedv/expr/test/playground"
119
)
1210

1311
func FuzzExpr(f *testing.F) {
14-
env := playground.ExampleData()
15-
16-
b, err := os.ReadFile("testdata/corpus.txt")
17-
if err != nil {
18-
f.Fatal(err)
12+
env := map[string]interface{}{
13+
"i": 1,
14+
"j": 2,
15+
"a": []int{1, 2, 3},
16+
"m": map[string]interface{}{"a": 1, "b": 2, "m": map[string]int{"a": 1}},
17+
"s": "abc",
1918
}
20-
for _, testcase := range strings.Split(string(b), "\n") {
21-
f.Add(testcase)
19+
20+
fn := expr.Function(
21+
"fn",
22+
func(params ...interface{}) (interface{}, error) {
23+
return params[0], nil
24+
},
25+
new(func(int) int),
26+
)
27+
28+
corpus := []string{
29+
`.5 + .5`,
30+
`i + j`,
31+
`i - j`,
32+
`i * j`,
33+
`i / j`,
34+
`i % j`,
35+
`true || false`,
36+
`true && false`,
37+
`i == j`,
38+
`i != j`,
39+
`i > j`,
40+
`i >= j`,
41+
`i < j`,
42+
`i <= j`,
43+
`i in a`,
44+
`i not in a`,
45+
`i in m`,
46+
`m.a`,
47+
`m.m.a`,
48+
`a[0]`,
49+
`a[i]`,
50+
`a[i:j]`,
51+
`a[i:]`,
52+
`a[:j]`,
53+
`a[:]`,
54+
`a[1:-1]`,
55+
`len(a)`,
56+
`abs(-1)`,
57+
`int(0.5)`,
58+
`float(42)`,
59+
`string(i)`,
60+
`trim(" a ")`,
61+
`trim("_a_", "_")`,
62+
`trimPrefix(" a")`,
63+
`trimSuffix("a ")`,
64+
`upper("a")`,
65+
`lower("A")`,
66+
`split("a,b,c", ",")`,
67+
`replace("a,b,c", ",", "_")`,
68+
`repeat("a", 3)`,
69+
`join(["a", "b", "c"], ",")`,
70+
`indexOf("abc", "b")`,
71+
`max(1,2,3)`,
72+
`min(1,2,3)`,
73+
`toJSON(a)`,
74+
`fromJSON("[1,2,3]")`,
75+
`now()`,
76+
`duration("1s")`,
77+
`first(a)`,
78+
`last(a)`,
79+
`get(m, "a")`,
80+
`1..9 | filter(i > 5) | map(i * 2)`,
81+
`s startsWith "a"`,
82+
`s endsWith "c"`,
83+
`s contains "a"`,
84+
`s matches "a"`,
85+
`s matches "a+"`,
86+
`true ? 1 : 2`,
87+
`fn(1)`,
88+
`{a: 1, b: 2}`,
89+
`[1, 2, 3]`,
2290
}
2391

24-
f.Add(`Posts[0].Comments[0].AuthorEmail()`)
25-
f.Add(`Posts[0].Comments[0].Upvoted()`)
26-
f.Add(`Posts[0].Comments[0].CommentDate.Add(now())`)
27-
f.Add(`Posts[0].Comments[0].CommentDate.AddDate(0, 0, 1)`)
28-
f.Add(`Authors[Posts[0].Author.ID].Profile.Biography`)
29-
f.Add(`Authors[2].Profile.Age()`)
30-
f.Add(`Authors[2].Profile.Website`)
92+
for _, s := range corpus {
93+
f.Add(s)
94+
}
3195

3296
okCases := []*regexp.Regexp{
97+
regexp.MustCompile(`cannot fetch .* from .*`),
98+
regexp.MustCompile(`cannot get .* from .*`),
3399
regexp.MustCompile(`cannot slice`),
100+
regexp.MustCompile(`slice index out of range`),
101+
regexp.MustCompile(`error parsing regexp`),
34102
regexp.MustCompile(`integer divide by zero`),
35-
regexp.MustCompile(`invalid operation`),
36103
regexp.MustCompile(`interface conversion`),
37-
regexp.MustCompile(`memory budget exceeded`),
38-
regexp.MustCompile(`slice index out of range`),
39-
regexp.MustCompile(`cannot fetch .* from .*`),
40-
regexp.MustCompile(`cannot get .* from .*`),
41104
regexp.MustCompile(`invalid argument for .*`),
42-
regexp.MustCompile(`json: unsupported value`),
43-
regexp.MustCompile(`error parsing regexp`),
105+
regexp.MustCompile(`invalid character`),
106+
regexp.MustCompile(`invalid operation`),
107+
regexp.MustCompile(`invalid duration`),
44108
regexp.MustCompile(`time: missing unit in duration`),
109+
regexp.MustCompile(`time: unknown unit .* in duration`),
110+
regexp.MustCompile(`json: unsupported value`),
111+
regexp.MustCompile(`unexpected end of JSON input`),
112+
regexp.MustCompile(`memory budget exceeded`),
45113
regexp.MustCompile(`using interface \{} as type .*`),
46114
regexp.MustCompile(`reflect.Value.MapIndex: value of type .* is not assignable to type .*`),
47-
regexp.MustCompile(`reflect: call of reflect.Value.Call on zero Value`),
48-
regexp.MustCompile(`reflect: call of reflect.Value.Len on bool Value`),
49115
regexp.MustCompile(`reflect: Call using .* as type .*`),
116+
regexp.MustCompile(`reflect: call of reflect.Value.Call on .* Value`),
50117
regexp.MustCompile(`reflect: call of reflect.Value.Index on map Value`),
118+
regexp.MustCompile(`reflect: call of reflect.Value.Len on .* Value`),
51119
}
52120

53121
skipCode := []string{
@@ -62,7 +130,7 @@ func FuzzExpr(f *testing.F) {
62130
}
63131
}
64132

65-
program, err := expr.Compile(code, expr.Env(playground.Blog{}))
133+
program, err := expr.Compile(code, expr.Env(env), fn)
66134
if err != nil {
67135
t.Skip()
68136
}

0 commit comments

Comments
 (0)