Skip to content

Commit efd6120

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 efd6120

File tree

5 files changed

+51
-11
lines changed

5 files changed

+51
-11
lines changed

fuzz/fuzz_test.go

Lines changed: 51 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

@@ -35,18 +36,65 @@ func FuzzConverter(f *testing.F) {
3536
`{"status": {"$in": []}}`,
3637
}
3738
for _, tc := range tcs {
38-
f.Add(tc)
39+
f.Add(tc, true)
40+
f.Add(tc, false)
3941
}
4042

41-
f.Fuzz(func(t *testing.T, in string) {
42-
c := filter.NewConverter(filter.WithArrayDriver(pq.Array))
43+
f.Fuzz(func(t *testing.T, in string, jsonb bool) {
44+
options := []filter.Option{
45+
filter.WithArrayDriver(pq.Array),
46+
}
47+
if jsonb {
48+
options = append(options, filter.WithNestedJSONB("meta"))
49+
}
50+
c := filter.NewConverter(options...)
4351
conditions, _, err := c.Convert([]byte(in), 1)
4452
if err == nil && conditions != "" {
4553
j, err := pg_query.ParseToJSON("SELECT * FROM test WHERE 1 AND " + conditions)
4654
if err != nil {
4755
t.Fatalf("%q %q %v", in, conditions, err)
4856
}
4957

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

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)