super-graph/core/internal/psql/fuzz.go

135 lines
2.0 KiB
Go
Raw Normal View History

2020-02-02 07:43:09 +01:00
// +build gofuzz
package psql
import (
"encoding/json"
"github.com/dosco/super-graph/core/internal/qcode"
2020-02-02 07:43:09 +01:00
)
var (
qcompileTest, _ = qcode.NewCompiler(qcode.Config{})
2020-05-31 23:11:28 +02:00
schema, _ = GetTestSchema()
2020-02-02 07:43:09 +01:00
vars = map[string]string{
2020-02-02 07:43:09 +01:00
"admin_account_id": "5",
}
2020-02-02 07:43:09 +01:00
pcompileTest = NewCompiler(Config{
Schema: schema,
Vars: vars,
})
)
// FuzzerEntrypoint for Fuzzbuzz
func Fuzz(data []byte) int {
2020-05-31 23:11:28 +02:00
err1 := query(data)
err2 := insert(data)
err3 := update(data)
err4 := delete(data)
if err1 != nil || err2 != nil || err3 != nil || err4 != nil {
return 0
}
return 1
}
func query(data []byte) error {
gql := data
qc, err1 := qcompileTest.Compile(gql, "user")
vars := map[string]json.RawMessage{
"data": json.RawMessage(data),
}
_, _, err2 := pcompileTest.CompileEx(qc, vars)
if err1 != nil {
return err1
} else {
return err2
}
}
func insert(data []byte) error {
gql := `mutation {
product(insert: $data) {
id
name
user {
id
full_name
email
}
}
}`
qc, err := qcompileTest.Compile([]byte(gql), "user")
if err != nil {
panic("qcompile can't fail")
}
vars := map[string]json.RawMessage{
"data": json.RawMessage(data),
}
_, _, err = pcompileTest.CompileEx(qc, vars)
return err
}
func update(data []byte) error {
2020-02-02 07:43:09 +01:00
gql := `mutation {
product(insert: $data) {
id
name
user {
id
full_name
email
}
}
}`
qc, err := qcompileTest.Compile([]byte(gql), "user")
if err != nil {
panic("qcompile can't fail")
}
vars := map[string]json.RawMessage{
"data": json.RawMessage(data),
}
_, _, err = pcompileTest.CompileEx(qc, vars)
2020-05-31 23:11:28 +02:00
return err
}
func delete(data []byte) error {
gql := `mutation {
product(insert: $data) {
id
name
user {
id
full_name
email
}
}
}`
qc, err := qcompileTest.Compile([]byte(gql), "user")
2020-02-02 07:43:09 +01:00
if err != nil {
2020-05-31 23:11:28 +02:00
panic("qcompile can't fail")
2020-02-02 07:43:09 +01:00
}
2020-05-31 23:11:28 +02:00
vars := map[string]json.RawMessage{
"data": json.RawMessage(data),
}
_, _, err = pcompileTest.CompileEx(qc, vars)
return err
2020-02-02 07:43:09 +01:00
}