Skip to content

Commit

Permalink
Also fuzz test the JSONB code (#8)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
erikdubbelboer authored Jun 15, 2024
1 parent fac4721 commit 6348ca9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
56 changes: 53 additions & 3 deletions fuzz/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fuzz

import (
"encoding/json"
"strings"
"testing"

Expand Down Expand Up @@ -33,20 +34,69 @@ func FuzzConverter(f *testing.F) {
`{"name": {}}`,
`{"$or": []}`,
`{"status": {"$in": []}}`,
`{"$or": [{}, {}]}`,
`{"\"bla = 1 --": 1}`,
}
for _, tc := range tcs {
f.Add(tc)
f.Add(tc, true)
f.Add(tc, false)
}

f.Fuzz(func(t *testing.T, in string) {
c := filter.NewConverter(filter.WithArrayDriver(pq.Array))
f.Fuzz(func(t *testing.T, in string, jsonb bool) {
options := []filter.Option{
filter.WithArrayDriver(pq.Array),
}
if jsonb {
options = append(options, filter.WithNestedJSONB("meta"))
}
c := filter.NewConverter(options...)
conditions, _, err := c.Convert([]byte(in), 1)
if err == nil && conditions != "" {
j, err := pg_query.ParseToJSON("SELECT * FROM test WHERE 1 AND " + conditions)
if err != nil {
t.Fatalf("%q %q %v", in, conditions, err)
}

t.Log(j)

var q struct {
Stmts []struct {
Stmt struct {
SelectStmt struct {
FromClause []struct {
RangeVar struct {
Relname string `json:"relname"`
} `json:"RangeVar"`
} `json:"fromClause"`

WhereClause struct {
BoolExpr struct {
Boolop string `json:"boolop"`
Args []any `json:"args"`
} `json:"BoolExpr"`
} `json:"whereClause"`
} `json:"SelectStmt"`
} `json:"stmt"`
} `json:"stmts"`
}
if err := json.Unmarshal([]byte(j), &q); err != nil {
t.Fatal(err)
}
if len(q.Stmts) != 1 {
t.Fatal(conditions, "len(q.Stmts) != 1")
}
if len(q.Stmts[0].Stmt.SelectStmt.FromClause) != 1 {
t.Fatal(conditions, "len(q.Stmts[0].Stmt.SelectStmt.FromClause) != 1")
}
if q.Stmts[0].Stmt.SelectStmt.FromClause[0].RangeVar.Relname != "test" {
t.Fatal(conditions, "q.Stmts[0].Stmt.SelectStmt.FromClause[0].RangeVar.Relname != test")
}
if q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Boolop != "AND_EXPR" {
t.Fatal(conditions, "q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Boolop != AND_EXPR")
}
if len(q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Args) != 2 {
t.Fatal(conditions, "len(q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Args) != 2")
}
if strings.Contains(j, "CommentStmt") {
t.Fatal(conditions, "CommentStmt found")
}
Expand Down
2 changes: 0 additions & 2 deletions fuzz/testdata/fuzz/FuzzConverter/3e813b4e3bbd8eca

This file was deleted.

2 changes: 0 additions & 2 deletions fuzz/testdata/fuzz/FuzzConverter/439ca8de24f74c60

This file was deleted.

2 changes: 0 additions & 2 deletions fuzz/testdata/fuzz/FuzzConverter/cbbad75732661f48

This file was deleted.

2 changes: 0 additions & 2 deletions fuzz/testdata/fuzz/FuzzConverter/f7947d6c886ffd04

This file was deleted.

0 comments on commit 6348ca9

Please sign in to comment.