From 688483e17945c1ee11455371027522ab9081690a Mon Sep 17 00:00:00 2001 From: Billy Keyes Date: Sun, 12 Sep 2021 21:37:53 -0700 Subject: [PATCH] Add experimental fuzz test This uses the beta version of native fuzzing support, currently expected to ship in an initial form in go 1.18. For now, the test must be run with a development version of Go and is ignored otherwise. --- gitdiff/fuzz_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 gitdiff/fuzz_test.go diff --git a/gitdiff/fuzz_test.go b/gitdiff/fuzz_test.go new file mode 100644 index 0000000..19b5198 --- /dev/null +++ b/gitdiff/fuzz_test.go @@ -0,0 +1,38 @@ +// +build gofuzzbeta + +package gitdiff + +import ( + "bytes" + "io/fs" + "os" + "path/filepath" + "strings" + "testing" +) + +func FuzzParse(f *testing.F) { + // Use existing test files as the seed corpus + // TODO(bkeyes): once supported, should this use static files instead? + if err := filepath.WalkDir("testdata", func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if d.IsDir() || !strings.HasSuffix(path, ".patch") { + return nil + } + b, err := os.ReadFile(path) + if err != nil { + return err + } + f.Add(b) + return nil + }); err != nil { + f.Fatalf("error creating seed corpus: %v", err) + } + + f.Fuzz(func(t *testing.T, b []byte) { + t.Parallel() + Parse(bytes.NewReader(b)) + }) +}