Skip to content

Commit ea9d3be

Browse files
author
João Veiga
committed
add fuzzing for schema.Exec, schema and query parsing
1 parent 21d1871 commit ea9d3be

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ module github.com/graph-gophers/graphql-go
22

33
require github.com/opentracing/opentracing-go v1.1.0
44

5+
56
go 1.13

graphql_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"strings"
78
"sync"
89
"testing"
910
"time"
@@ -4297,3 +4298,28 @@ func TestInterfaceImplementingInterface(t *testing.T) {
42974298
`,
42984299
}})
42994300
}
4301+
4302+
type query struct{}
4303+
4304+
func (_ *query) Hello() string { return "Hello, world!" }
4305+
func (_ *query) Hi(string, string) string { return "Hello, world!" }
4306+
func (_ *query) LaunchMissil(string, string) string { return "Hello, world!" }
4307+
4308+
func FuzzSchemaExec(f *testing.F) {
4309+
f.Fuzz(func(t *testing.T, s string, queryString , operationName string) {
4310+
defer func(){
4311+
if err := recover(); err != nil{
4312+
if !strings.Contains(err.(error).Error(), "invalid syntax"){
4313+
panic(err)
4314+
}
4315+
}
4316+
}()
4317+
ctx := context.Background()
4318+
variables := map[string]interface{}{}
4319+
schema,err := graphql.ParseSchema(s, &query{})
4320+
if err != nil {
4321+
t.Skip()
4322+
}
4323+
schema.Exec(ctx, queryString, operationName, variables)
4324+
})
4325+
}

internal/query/query_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package query
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func FuzzParseQuery(f *testing.F) {
8+
f.Fuzz(func(t *testing.T, queryStr string) {
9+
Parse(queryStr)
10+
})
11+
}
12+

internal/schema/schema_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,3 +1007,10 @@ func TestInterfaceImplementsInterface(t *testing.T) {
10071007
})
10081008
}
10091009
}
1010+
1011+
func FuzzParse(f *testing.F){
1012+
f.Fuzz(func(t *testing.T, schemaString string, useStringDescriptions bool){
1013+
s := schema.New()
1014+
schema.Parse(s, schemaString, useStringDescriptions)
1015+
})
1016+
}

0 commit comments

Comments
 (0)