From 6f401df36680526d7e6eabb70ce8c4dd986273ef Mon Sep 17 00:00:00 2001 From: Jay Conrod Date: Tue, 9 Feb 2021 10:15:02 -0500 Subject: [PATCH] [dev.fuzz] cmd/go: call F.Fuzz from all fuzz script tests Fuzz targets must call F.Skip, F.Fail, or F.Fuzz. F.Fuzz must not be called more than once. If a fuzz target panics, calls runtime.Goexit, or returns normally without calling one of those functions, the target should panic, and 'go test' should exit with a non-zero status. For now, this isn't checked. It will be fixed in a follow-up CL. Change-Id: Ibb905954462b64af15332c285124d78a998f7762 Reviewed-on: https://go-review.googlesource.com/c/go/+/290689 Trust: Jay Conrod Run-TryBot: Jay Conrod TryBot-Result: Go Bot Reviewed-by: Katie Hockman --- src/cmd/go/testdata/script/test_fuzz.txt | 32 ++++++++++++++++--- .../go/testdata/script/test_fuzz_chatty.txt | 1 + .../go/testdata/script/test_fuzz_match.txt | 5 ++- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/cmd/go/testdata/script/test_fuzz.txt b/src/cmd/go/testdata/script/test_fuzz.txt index eb65e0db2a..ccdae830a5 100644 --- a/src/cmd/go/testdata/script/test_fuzz.txt +++ b/src/cmd/go/testdata/script/test_fuzz.txt @@ -1,6 +1,20 @@ # TODO(jayconrod): support shared memory on more platforms. [!darwin] [!linux] [!windows] skip +# Test that running a fuzz target that returns without failing or calling +# f.Fuzz fails and causes a non-zero exit status. +# BUG(jayconrod): for now, it passes. +go test noop_fuzz_test.go +stdout ok +! stdout FAIL + +# Test that fuzzing a fuzz target that returns without failing or calling +# f.Fuzz fails and causes a non-zero exit status. +# BUG(jayconrod): for now, it passes. +go test -fuzz=Fuzz -fuzztime=5s -parallel=1 noop_fuzz_test.go +stdout ok +! stdout FAIL + # Test that calling f.Error in a fuzz target causes a non-zero exit status. ! go test -fuzz=Fuzz -fuzztime=5s -parallel=1 error_fuzz_test.go ! stdout ^ok @@ -13,6 +27,11 @@ stdout FAIL # Test that successful test exits cleanly. go test success_fuzz_test.go +stdout ^ok +! stdout FAIL + +# Test that successful fuzzing exits cleanly. +go test -fuzz=Fuzz -fuzztime=5s -parallel=1 success_fuzz_test.go stdout ok ! stdout FAIL @@ -21,11 +40,6 @@ stdout ok ! stdout ^ok stdout FAIL -# Test that successful fuzzing exits cleanly. -go test -fuzz=Fuzz -fuzztime=5s -parallel=1 success_fuzz_test.go -stdout ok -! stdout FAIL - # Test error with seed corpus in f.Fuzz ! go test -run FuzzError fuzz_add_test.go ! stdout ^ok @@ -122,6 +136,13 @@ stdout ok ! stdout FAIL ! stdout 'fatal here' +-- noop_fuzz_test.go -- +package noop_fuzz + +import "testing" + +func Fuzz(f *testing.F) {} + -- error_fuzz_test.go -- package error_fuzz @@ -155,6 +176,7 @@ package success_fuzz import "testing" func Fuzz(f *testing.F) { + f.Fuzz(func (*testing.T, []byte) {}) } -- skipped_fuzz_test.go -- diff --git a/src/cmd/go/testdata/script/test_fuzz_chatty.txt b/src/cmd/go/testdata/script/test_fuzz_chatty.txt index b70bb9e49f..aaf385f293 100644 --- a/src/cmd/go/testdata/script/test_fuzz_chatty.txt +++ b/src/cmd/go/testdata/script/test_fuzz_chatty.txt @@ -77,4 +77,5 @@ import "testing" func Fuzz(f *testing.F) { f.Log("all good here") + f.Fuzz(func(*testing.T, []byte) {}) } diff --git a/src/cmd/go/testdata/script/test_fuzz_match.txt b/src/cmd/go/testdata/script/test_fuzz_match.txt index 44ebf0bf66..5ead41411f 100644 --- a/src/cmd/go/testdata/script/test_fuzz_match.txt +++ b/src/cmd/go/testdata/script/test_fuzz_match.txt @@ -43,6 +43,7 @@ package standalone_fuzz import "testing" func Fuzz(f *testing.F) { + f.Fuzz(func (*testing.T, []byte) {}) } -- multiple_fuzz_test.go -- @@ -51,7 +52,9 @@ package multiple_fuzz import "testing" func FuzzA(f *testing.F) { + f.Fuzz(func (*testing.T, []byte) {}) } func FuzzB(f *testing.F) { -} \ No newline at end of file + f.Fuzz(func (*testing.T, []byte) {}) +} -- 2.48.1