1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
package graphql_test
import (
"context"
"testing"
"github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/example/starwars"
)
func FuzzSchemaExec(f *testing.F) {
resolver := &starwars.Resolver{}
opts := []graphql.SchemaOpt{graphql.MaxDepth(3)}
schema, err := graphql.ParseSchema(starwars.Schema, resolver, opts...)
if err != nil {
f.Errorf("ParseSchema: %v", err)
return
}
// Seed the fuzzing corpus with a variety of valid GraphQL queries.
queries := []string{
`{ hero { name } }`,
`{ hero { name appearsIn } }`,
`{ hero { name appearsIn friends { name } } }`,
`{ hero(episode: EMPIRE) { name } }`,
`{ episode(episode: EMPIRE) { title characters { name } reviews { stars commentary } } }`,
`{ episode(episode: EMPIRE) { title characters { name friends { name } reviews { stars commentary } } }`,
`query { episode(episode: EMPIRE) { title characters { name friends { name } } } }`,
`query HeroName { hero { name } }`,
`query HeroNameAndFriends { hero { name friends { name } } }`,
`mutation { createReview(episode: EMPIRE, review: { stars: 5, commentary: "Great!" }) }`,
}
for _, q := range queries {
f.Add(q)
}
f.Fuzz(func(t *testing.T, query string) {
// ignore invalid queries in order to test only the execution against the schema
errs := schema.Validate(query)
if len(errs) > 0 {
t.Skip()
}
res := schema.Exec(context.Background(), query, "", nil)
if res.Data != nil && len(res.Errors) > 0 {
t.Errorf("Exec(%q) returned both data and errors: %v", query, res.Errors)
}
if res.Errors != nil {
t.Logf("Exec(%q) returned errors: %v", query, res.Errors)
}
if res.Data == nil && len(res.Errors) == 0 {
t.Errorf("Exec(%q) returned nil data and no errors", query)
}
})
}
|