From 3df64dfb8203c33d937593157f6000cd7961312d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Veiga?= Date: Sat, 11 Dec 2021 20:41:24 +0100 Subject: [PATCH] add fuzzing for schema.Exec, schema and query parsing --- graphql_test.go | 26 ++++++++++++++++++++++++++ internal/query/query_test.go | 12 ++++++++++++ internal/schema/schema_test.go | 7 +++++++ 3 files changed, 45 insertions(+) create mode 100644 internal/query/query_test.go diff --git a/graphql_test.go b/graphql_test.go index b2074275c..cb326644c 100644 --- a/graphql_test.go +++ b/graphql_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "strings" "sync" "testing" "time" @@ -4297,3 +4298,28 @@ func TestInterfaceImplementingInterface(t *testing.T) { `, }}) } + +type query struct{} + +func (_ *query) Hello() string { return "Hello, world!" } +func (_ *query) Hi(string, string) string { return "Hello, world!" } +func (_ *query) LaunchMissil(string, string) string { return "Hello, world!" } + +func FuzzSchemaExec(f *testing.F) { + f.Fuzz(func(t *testing.T, s string, queryString , operationName string) { + defer func(){ + if err := recover(); err != nil{ + if !strings.Contains(err.(error).Error(), "invalid syntax"){ + panic(err) + } + } + }() + ctx := context.Background() + variables := map[string]interface{}{} + schema,err := graphql.ParseSchema(s, &query{}) + if err != nil { + t.Skip() + } + schema.Exec(ctx, queryString, operationName, variables) + }) +} diff --git a/internal/query/query_test.go b/internal/query/query_test.go new file mode 100644 index 000000000..62e3537df --- /dev/null +++ b/internal/query/query_test.go @@ -0,0 +1,12 @@ +package query + +import ( + "testing" +) + +func FuzzParseQuery(f *testing.F) { + f.Fuzz(func(t *testing.T, queryStr string) { + Parse(queryStr) + }) +} + diff --git a/internal/schema/schema_test.go b/internal/schema/schema_test.go index 065e9917a..e66798f38 100644 --- a/internal/schema/schema_test.go +++ b/internal/schema/schema_test.go @@ -1007,3 +1007,10 @@ func TestInterfaceImplementsInterface(t *testing.T) { }) } } + +func FuzzParse(f *testing.F){ + f.Fuzz(func(t *testing.T, schemaString string, useStringDescriptions bool){ + s := schema.New() + schema.Parse(s, schemaString, useStringDescriptions) + }) +}