Skip to content

Commit 7601c63

Browse files
committed
Add fuzz dict and corpus
1 parent 32bcae1 commit 7601c63

File tree

4 files changed

+178
-75
lines changed

4 files changed

+178
-75
lines changed

fuzz_test.go

Lines changed: 6 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package expr_test
22

33
import (
4+
"os"
45
"regexp"
6+
"strings"
57
"testing"
68

79
"github.com/antonmedv/expr"
@@ -27,82 +29,11 @@ func FuzzExpr(f *testing.F) {
2729
new(func(int) int),
2830
)
2931

30-
corpus := []string{
31-
`.5 + .5`,
32-
`i + j`,
33-
`i - j`,
34-
`i * j`,
35-
`i / j`,
36-
`i % j`,
37-
`true || false`,
38-
`true && false`,
39-
`i == j`,
40-
`i != j`,
41-
`i > j`,
42-
`i >= j`,
43-
`i < j`,
44-
`i <= j`,
45-
`i in a`,
46-
`i not in a`,
47-
`s in m`,
48-
`m.a`,
49-
`m.m.a`,
50-
`a[0]`,
51-
`a[i]`,
52-
`a[i:j]`,
53-
`a[i:]`,
54-
`a[:j]`,
55-
`a[:]`,
56-
`a[1:-1]`,
57-
`len(a)`,
58-
`type(a)`,
59-
`abs(-1)`,
60-
`int(0.5)`,
61-
`float(42)`,
62-
`string(i)`,
63-
`trim(" a ")`,
64-
`trim("_a_", "_")`,
65-
`trimPrefix(" a", " ")`,
66-
`trimSuffix("a ")`,
67-
`upper("a")`,
68-
`lower("A")`,
69-
`split("a,b,c", ",")`,
70-
`replace("a,b,c", ",", "_")`,
71-
`repeat("a", 3)`,
72-
`join(["a", "b", "c"], ",")`,
73-
`indexOf("abc", "b")`,
74-
`max(1,2,3)`,
75-
`min(1,2,3)`,
76-
`toJSON(a)`,
77-
`fromJSON("[1,2,3]")`,
78-
`now()`,
79-
`duration("1s")`,
80-
`first(a)`,
81-
`last(a)`,
82-
`get(m, "a")`,
83-
`1..9 | filter(i > 5) | map(i * 2)`,
84-
`s startsWith "a"`,
85-
`s endsWith "c"`,
86-
`s contains "a"`,
87-
`s matches "a"`,
88-
`s matches "a+"`,
89-
`true ? 1 : 2`,
90-
`false ? 1 : 2`,
91-
`b ?? true`,
92-
`head(1)`,
93-
`{a: 1, b: 2}`,
94-
`[1, 2, 3]`,
95-
`type(1)`,
96-
`type("a")`,
97-
`type([1, 2, 3])`,
98-
`type({a: 1, b: 2})`,
99-
`type(head)`,
100-
`keys(m)`,
101-
`values(m)`,
102-
`foo.bar.a`,
103-
`foo.bar.b`,
32+
b, err := os.ReadFile("./testdata/fuzz_corpus.txt")
33+
if err != nil {
34+
panic(err)
10435
}
105-
36+
corpus := strings.Split(strings.TrimSpace(string(b)), "\n")
10637
for _, s := range corpus {
10738
f.Add(s)
10839
}

testdata/fuzz_corpus.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
inputfile=./fuzz_corpus.txt
4+
zipname=fuzz_expr_seed_corpus.zip
5+
6+
if [ ! -f "$inputfile" ]; then
7+
echo "Error: File $inputfile not found!"
8+
exit 1
9+
fi
10+
11+
lineno=1
12+
while IFS= read -r line; do
13+
echo "$line" >"file_${lineno}.txt"
14+
((lineno++))
15+
done <"$inputfile"
16+
17+
zip "$zipname" file_*.txt
18+
19+
rm file_*.txt
20+
21+
echo "Done!"

testdata/fuzz_corpus.txt

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
.5 + .5
2+
i + j
3+
i - j
4+
i * j
5+
i / j
6+
i % j
7+
true || false
8+
true && false
9+
i == j
10+
i != j
11+
i > j
12+
i >= j
13+
i < j
14+
i <= j
15+
i in a
16+
i not in a
17+
s in m
18+
m.a
19+
m.m.a
20+
a[0]
21+
a[i]
22+
a[i:j]
23+
a[i:]
24+
a[:j]
25+
a[:]
26+
a[1:-1]
27+
len(a)
28+
type(a)
29+
abs(-1)
30+
int(0.5)
31+
float(42)
32+
string(i)
33+
trim(" a ")
34+
trim("_a_", "_")
35+
trimPrefix(" a", " ")
36+
trimSuffix("a ")
37+
upper("a")
38+
lower("A")
39+
split("a,b,c", ",")
40+
replace("a,b,c", ",", "_")
41+
repeat("a", 3)
42+
join(["a", "b", "c"], ",")
43+
indexOf("abc", "b")
44+
max(1,2,3)
45+
min(1,2,3)
46+
toJSON(a)
47+
fromJSON("[1,2,3]")
48+
now()
49+
duration("1s")
50+
first(a)
51+
last(a)
52+
get(m, "a")
53+
1..9 | filter(i > 5) | map(i * 2)
54+
s startsWith "a"
55+
s endsWith "c"
56+
s contains "a"
57+
s matches "a"
58+
s matches "a+"
59+
true ? 1 : 2
60+
false ? 1 : 2
61+
b ?? true
62+
head(1)
63+
{a: 1, b: 2}
64+
[1, 2, 3]
65+
type(1)
66+
type("a")
67+
type([1, 2, 3])
68+
type({a: 1, b: 2})
69+
type(head)
70+
keys(m)
71+
values(m)
72+
foo.bar.a
73+
foo.bar.b

testdata/fuzz_expr.dict

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"{"
2+
"}"
3+
","
4+
"["
5+
"]"
6+
"("
7+
")"
8+
":"
9+
"'"
10+
"\""
11+
"0.1"
12+
"1.2"
13+
"2.3"
14+
"-3.4"
15+
"-4.5"
16+
"-5.6"
17+
"1e2"
18+
"2e3"
19+
"-3e4"
20+
"-4e5"
21+
"0"
22+
"1"
23+
"2"
24+
"-3"
25+
"-4"
26+
"0x"
27+
"true"
28+
"false"
29+
"map"
30+
"filter"
31+
"all"
32+
"any"
33+
"none"
34+
"one"
35+
"and"
36+
"or"
37+
"in"
38+
"not"
39+
"in"
40+
"contains"
41+
"matches"
42+
"startsWith"
43+
"endsWith"
44+
"nil"
45+
"len"
46+
"type"
47+
"abs"
48+
"int"
49+
"float"
50+
"string"
51+
"trim"
52+
"trimPrefix"
53+
"trimSuffix"
54+
"upper"
55+
"lower"
56+
"split"
57+
"splitAfter"
58+
"replace"
59+
"repeat"
60+
"join"
61+
"indexOf"
62+
"lastIndexOf"
63+
"hasPrefix"
64+
"hasSuffix"
65+
"max"
66+
"min"
67+
"toJSON"
68+
"fromJSON"
69+
"toBase64"
70+
"fromBase64"
71+
"now"
72+
"duration"
73+
"date"
74+
"first"
75+
"last"
76+
"get"
77+
"keys"
78+
"values"

0 commit comments

Comments
 (0)