Skip to content

Also fuzz test the JSONB code #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to keep this log statement in?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's very useful to see the generated AST when an input fails. It doesn't print anything for the cases that don't fail.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if it would output with -v, but it does not. 👍


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.

Loading