]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.fuzz] cmd/go: in 'go test' don't allow multiple packages with -fuzz
authorJay Conrod <jayconrod@google.com>
Wed, 15 Sep 2021 22:30:18 +0000 (15:30 -0700)
committerJay Conrod <jayconrod@google.com>
Mon, 20 Sep 2021 18:42:32 +0000 (18:42 +0000)
Until we have a system for managing load across multiple fuzz targets
in multiple test executables, we'll only support fuzzing one target in
one package at a time. Users can still run multiple 'go test -fuzz'
commands concurrently, but this may overwhelm some systems unless
-parallel and -p are set carefully.

For #46312

Change-Id: If84c58d1b3e60498ce955eae5ad4d52100dbd4b7
Reviewed-on: https://go-review.googlesource.com/c/go/+/350156
Trust: Jay Conrod <jayconrod@google.com>
Trust: Katie Hockman <katie@golang.org>
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Katie Hockman <katie@golang.org>
src/cmd/go/internal/test/test.go
src/cmd/go/testdata/script/test_fuzz_match.txt
src/cmd/go/testdata/script/test_fuzz_multiple.txt [new file with mode: 0644]

index c36cb0b221f266b1fede9adfa2def0741eee82d7..173e8a2ee47980d289a4f361244cca0480f6abe1 100644 (file)
@@ -671,6 +671,9 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
        if testO != "" && len(pkgs) != 1 {
                base.Fatalf("cannot use -o flag with multiple packages")
        }
+       if testFuzz != "" && len(pkgs) != 1 {
+               base.Fatalf("cannot use -fuzz flag with multiple packages")
+       }
        if testProfile() != "" && len(pkgs) != 1 {
                base.Fatalf("cannot use %s flag with multiple packages", testProfile())
        }
index 47e143952acd185e7bc7302bea7604e84bfb301a..3a2ca631add184341578fae2e95db9bfe92e7f02 100644 (file)
@@ -29,13 +29,6 @@ go test -run ThisWillNotMatch standalone_fuzz_test.go
 stdout '^ok.*no tests to run'
 ! stdout 'no targets to fuzz'
 
-# Matches more than one fuzz target for fuzzing.
-! go test -fuzz Fuzz -fuzztime 1x multiple_fuzz_test.go
-! stdout 'no tests to run'
-! stdout 'no targets to fuzz'
-stdout FAIL
-stdout 'will not fuzz, -fuzz matches more than one target'
-
 -- standalone_fuzz_test.go --
 package standalone_fuzz
 
@@ -44,16 +37,3 @@ import "testing"
 func Fuzz(f *testing.F) {
        f.Fuzz(func (*testing.T, []byte) {})
 }
-
--- multiple_fuzz_test.go --
-package multiple_fuzz
-
-import "testing"
-
-func FuzzA(f *testing.F) {
-       f.Fuzz(func (*testing.T, []byte) {})
-}
-
-func FuzzB(f *testing.F) {
-       f.Fuzz(func (*testing.T, []byte) {})
-}
diff --git a/src/cmd/go/testdata/script/test_fuzz_multiple.txt b/src/cmd/go/testdata/script/test_fuzz_multiple.txt
new file mode 100644 (file)
index 0000000..6a7732f
--- /dev/null
@@ -0,0 +1,51 @@
+# This test checks that 'go test' prints a reasonable error when fuzzing is
+# enabled, and multiple package or multiple fuzz targets match.
+# TODO(#46312): support fuzzing multiple targets in multiple packages.
+
+# TODO(jayconrod): support shared memory on more platforms.
+[!darwin] [!linux] [!windows] skip
+
+[short] skip
+
+# With fuzzing disabled, multiple targets can be tested.
+go test ./...
+
+# With fuzzing enabled, at most one package may be tested,
+# even if only one package contains fuzz targets.
+! go test -fuzz=. ./...
+stderr '^cannot use -fuzz flag with multiple packages$'
+! go test -fuzz=. ./zero ./one
+stderr '^cannot use -fuzz flag with multiple packages$'
+go test -fuzz=. -fuzztime=1x ./one
+
+# With fuzzing enabled, at most one target in the same package may match.
+! go test -fuzz=. ./two
+stdout '^testing: will not fuzz, -fuzz matches more than one target: \[FuzzOne FuzzTwo\]$'
+go test -fuzz=FuzzTwo -fuzztime=1x ./two
+
+-- go.mod --
+module fuzz
+
+go 1.18
+-- zero/zero.go --
+package zero
+-- one/one_test.go --
+package one
+
+import "testing"
+
+func FuzzOne(f *testing.F) {
+  f.Fuzz(func(*testing.T, []byte) {})
+}
+-- two/two_test.go --
+package two
+
+import "testing"
+
+func FuzzOne(f *testing.F) {
+  f.Fuzz(func(*testing.T, []byte) {})
+}
+
+func FuzzTwo(f *testing.F) {
+  f.Fuzz(func(*testing.T, []byte) {})
+}