graph/formats/dot/internal: add tests for a fuzz data corpus
MODIFY
graph/formats/dot/internal/lexer/lexer_test.go
graph/formats/dot/internal/lexer/lexer_test.go
package lexer_test
import (
+ "archive/zip"
"bytes"
"io/ioutil"
+ "os"
"testing"
"gonum.org/v1/gonum/graph/formats/dot"
@@ -52,3 +54,35 @@ func TestParseFile(t *testing.T) {
}
}
}
+
+func TestParseFuzz(t *testing.T) {
+ r, err := zip.OpenReader("../../fuzz/corpus.zip")
+ if err != nil {
+ if os.IsNotExist(err) {
+ t.Skip("no corpus")
+ }
+ t.Fatalf("failed to open corpus: %v", err)
+ }
+ defer r.Close()
+
+ for _, f := range r.File {
+ rc, err := f.Open()
+ if err != nil {
+ t.Fatalf("failed to open %q: %v", f.Name, err)
+ }
+ func() {
+ defer func() {
+ p := recover()
+ if p != nil {
+ t.Errorf("unexpected panic parsing %q: %v", f.Name, p)
+ }
+ }()
+
+ _, err = dot.Parse(rc)
+ if err != nil {
+ t.Errorf("unexpected error parsing %q: %v", f.Name, err)
+ }
+ }()
+ rc.Close()
+ }
+}

MODIFY
graph/formats/dot/internal/parser/parser_test.go
graph/formats/dot/internal/parser/parser_test.go
package parser_test
import (
+ "archive/zip"
"bytes"
"io/ioutil"
+ "os"
"testing"
"gonum.org/v1/gonum/graph/formats/dot"
@@ -86,6 +88,38 @@ func TestParseFile(t *testing.T) {
}
}
+func TestParseFuzz(t *testing.T) {
+ r, err := zip.OpenReader("../../fuzz/corpus.zip")
+ if err != nil {
+ if os.IsNotExist(err) {
+ t.Skip("no corpus")
+ }
+ t.Fatalf("failed to open corpus: %v", err)
+ }
+ defer r.Close()
+
+ for _, f := range r.File {
+ rc, err := f.Open()
+ if err != nil {
+ t.Fatalf("failed to open %q: %v", f.Name, err)
+ }
+ func() {
+ defer func() {
+ p := recover()
+ if p != nil {
+ t.Errorf("unexpected panic parsing %q: %v", f.Name, p)
+ }
+ }()
+
+ _, err = dot.Parse(rc)
+ if err != nil {
+ t.Errorf("unexpected error parsing %q: %v", f.Name, err)
+ }
+ }()
+ rc.Close()
+ }
+}
+
func TestParseError(t *testing.T) {
golden := []struct {
path string

graph/formats/dot/internal: add tests for a fuzz data corpus