Skip to content

Commit 7e6fe4d

Browse files
author
Joao Veiga
committed
add fuzz tests for github.com/graph-gophers/graphql-go/internal/query
1 parent dae41bd commit 7e6fe4d

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

.github/workflows/ci.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
on: [push, pull_request]
2+
jobs:
3+
test:
4+
runs-on: ubuntu-16.04
5+
steps:
6+
- uses: actions/checkout@v1
7+
- uses: actions/setup-go@v2
8+
with:
9+
go-version: '^1.13'
10+
- run: go test ./...

go.mod

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
module github.com/graph-gophers/graphql-go
22

3-
require github.com/opentracing/opentracing-go v1.1.0
3+
require (
4+
github.com/google/gofuzz v1.1.0
5+
github.com/opentracing/opentracing-go v1.1.0
6+
github.com/stretchr/testify v1.3.0
7+
)
48

59
go 1.13

go.sum

+10
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
4+
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
15
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
26
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
7+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
10+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
11+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
12+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

internal/query/query_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package query
2+
3+
import (
4+
"log"
5+
"testing"
6+
"unicode"
7+
8+
fuzz "github.com/google/gofuzz"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestFuzzQueryNonASCII(t *testing.T) {
13+
t.Parallel()
14+
f := fuzz.New()
15+
var query string
16+
for i := 0; i < 100000; {
17+
f.Fuzz(&query)
18+
if !isASCII([]byte(query)) {
19+
continue
20+
}
21+
require.NotPanics(t, func() {
22+
Parse(query)
23+
}, "panicked with input %s", string(query))
24+
i++
25+
}
26+
}
27+
28+
func TestFuzzQueryASCII(t *testing.T) {
29+
t.Parallel()
30+
f := fuzz.New()
31+
var query []byte
32+
for i := 0; i < 100000; {
33+
f.Fuzz(&query)
34+
if isASCII(query) {
35+
continue
36+
}
37+
require.NotPanics(t, func() {
38+
Parse(string(query))
39+
}, "panicked with input %s", string(query))
40+
i++
41+
}
42+
}
43+
44+
func TestFuzzRegressions(t *testing.T) {
45+
crashers := []string{`query($~\344\334\234\344\334\344\234�d44\201"`}
46+
for _, crash := range crashers {
47+
require.NotPanics(t, func() {
48+
_, err := Parse(crash)
49+
if err == nil {
50+
log.Fatalf("found a regression with %s", crash)
51+
}
52+
}, "panicked for query: %s", crash)
53+
}
54+
}
55+
56+
func isASCII(b []byte) bool {
57+
for i := range b {
58+
if b[i] > unicode.MaxASCII {
59+
return false
60+
}
61+
}
62+
return true
63+
}

0 commit comments

Comments
 (0)