Skip to content

Commit 9eb6a19

Browse files
Also fuzz test the JSONB code
Before this we only fuzz tested the code on normal table columns. But with the code for the normal columns and jsonb fields getting some differences it's good to test both.
1 parent 0b1842c commit 9eb6a19

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

fuzz/fuzz_test.go

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fuzz
22

33
import (
4+
"encoding/json"
45
"strings"
56
"testing"
67

@@ -33,20 +34,69 @@ func FuzzConverter(f *testing.F) {
3334
`{"name": {}}`,
3435
`{"$or": []}`,
3536
`{"status": {"$in": []}}`,
37+
`{"$or": [{}, {}]}`,
38+
`{"\"bla = 1 --": 1}`,
3639
}
3740
for _, tc := range tcs {
38-
f.Add(tc)
41+
f.Add(tc, true)
42+
f.Add(tc, false)
3943
}
4044

41-
f.Fuzz(func(t *testing.T, in string) {
42-
c := filter.NewConverter(filter.WithArrayDriver(pq.Array))
45+
f.Fuzz(func(t *testing.T, in string, jsonb bool) {
46+
options := []filter.Option{
47+
filter.WithArrayDriver(pq.Array),
48+
}
49+
if jsonb {
50+
options = append(options, filter.WithNestedJSONB("meta"))
51+
}
52+
c := filter.NewConverter(options...)
4353
conditions, _, err := c.Convert([]byte(in), 1)
4454
if err == nil && conditions != "" {
4555
j, err := pg_query.ParseToJSON("SELECT * FROM test WHERE 1 AND " + conditions)
4656
if err != nil {
4757
t.Fatalf("%q %q %v", in, conditions, err)
4858
}
4959

60+
t.Log(j)
61+
62+
var q struct {
63+
Stmts []struct {
64+
Stmt struct {
65+
SelectStmt struct {
66+
FromClause []struct {
67+
RangeVar struct {
68+
Relname string `json:"relname"`
69+
} `json:"RangeVar"`
70+
} `json:"fromClause"`
71+
72+
WhereClause struct {
73+
BoolExpr struct {
74+
Boolop string `json:"boolop"`
75+
Args []any `json:"args"`
76+
} `json:"BoolExpr"`
77+
} `json:"whereClause"`
78+
} `json:"SelectStmt"`
79+
} `json:"stmt"`
80+
} `json:"stmts"`
81+
}
82+
if err := json.Unmarshal([]byte(j), &q); err != nil {
83+
t.Fatal(err)
84+
}
85+
if len(q.Stmts) != 1 {
86+
t.Fatal(conditions, "len(q.Stmts) != 1")
87+
}
88+
if len(q.Stmts[0].Stmt.SelectStmt.FromClause) != 1 {
89+
t.Fatal(conditions, "len(q.Stmts[0].Stmt.SelectStmt.FromClause) != 1")
90+
}
91+
if q.Stmts[0].Stmt.SelectStmt.FromClause[0].RangeVar.Relname != "test" {
92+
t.Fatal(conditions, "q.Stmts[0].Stmt.SelectStmt.FromClause[0].RangeVar.Relname != test")
93+
}
94+
if q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Boolop != "AND_EXPR" {
95+
t.Fatal(conditions, "q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Boolop != AND_EXPR")
96+
}
97+
if len(q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Args) != 2 {
98+
t.Fatal(conditions, "len(q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Args) != 2")
99+
}
50100
if strings.Contains(j, "CommentStmt") {
51101
t.Fatal(conditions, "CommentStmt found")
52102
}

fuzz/testdata/fuzz/FuzzConverter/3e813b4e3bbd8eca

Lines changed: 0 additions & 2 deletions
This file was deleted.

fuzz/testdata/fuzz/FuzzConverter/439ca8de24f74c60

Lines changed: 0 additions & 2 deletions
This file was deleted.

fuzz/testdata/fuzz/FuzzConverter/cbbad75732661f48

Lines changed: 0 additions & 2 deletions
This file was deleted.

fuzz/testdata/fuzz/FuzzConverter/f7947d6c886ffd04

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)