]> Cypherpunks.ru repositories - gostls13.git/commitdiff
[dev.fuzz] testing, internal/fuzz: multiple small fixes
authorJay Conrod <jayconrod@google.com>
Fri, 26 Feb 2021 15:54:11 +0000 (10:54 -0500)
committerJay Conrod <jayconrod@google.com>
Tue, 9 Mar 2021 18:36:29 +0000 (18:36 +0000)
* Run gofmt with go1.17 build constraint changes.
* Tighten regular expressions used in tests. "ok" got some false
  positives with verbose output, so make sure it appears at the start
  of a line.
* Return err in deps.RunFuzzWorker instead of nil.
* Call common.Helper from F methods. This prevents F methods from
  appearing in stack traces.

Change-Id: I839c70ec8a9f313c1a4ea68e6bb34a4d801dd36f
Reviewed-on: https://go-review.googlesource.com/c/go/+/297032
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/genflags.go
src/cmd/go/testdata/script/test_fuzz_match.txt
src/cmd/go/testdata/script/test_fuzz_mutator.txt
src/internal/fuzz/sys_posix.go
src/internal/fuzz/sys_unimplemented.go
src/testing/fuzz.go
src/testing/internal/testdeps/deps.go

index 949d65ae808b5f25bb99ef25b77a2af89da3ecd6..645aae68b17d7ec25d67de3b8991eca79e02d8eb 100644 (file)
@@ -2,6 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+//go:build ignore
 // +build ignore
 
 package main
index 5ead41411fdee94f8621001549d36c276e643ddd..4ea2fe2540efae3b6e7d6c442a67b84931440242 100644 (file)
@@ -14,7 +14,7 @@ stdout '^ok'
 # Matches none for fuzzing but will run the fuzz target as a test.
 go test -fuzz ThisWillNotMatch -fuzztime 5s -parallel 1 standalone_fuzz_test.go
 ! stdout '^ok.*\[no tests to run\]'
-stdout ok
+stdout '^ok'
 stdout '\[no targets to fuzz\]'
 
 [short] stop
index b94fa902452668ccba2444a79c0da8916946f50f..4a33eba339ded0d12bb4e6a2a00f05b48cfd9c27 100644 (file)
@@ -15,7 +15,7 @@ go run check_logs.go fuzz fuzz.worker
 
 # Test that the mutator is good enough to find several unique mutations.
 ! go test -fuzz=Fuzz -parallel=1 -fuzztime=30s mutator_test.go
-! stdout ok
+! stdout '^ok'
 stdout FAIL
 stdout 'mutator found enough unique mutations'
 
index d29ff40e8d22018b941e4cd066ed99ab076080b1..3fbbb478691b66ee834f4205e49d972ad484d888 100644 (file)
@@ -2,6 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+//go:build darwin || linux
 // +build darwin linux
 
 package fuzz
index 331b8761d025a63a0eb4ffea0588ec791ebb658b..5f80379c223893d5f4be3b95b353e82e388cc977 100644 (file)
@@ -3,6 +3,7 @@
 // license that can be found in the LICENSE file.
 
 // TODO(jayconrod): support more platforms.
+//go:build !darwin && !linux && !windows
 // +build !darwin,!linux,!windows
 
 package fuzz
index f670ef4546b61ed7486b323e97a8f556634829c5..1a634dbe8b0912a031d92340d4f642b9742dbef2 100644 (file)
@@ -70,6 +70,7 @@ func (f *F) Cleanup(fn func()) {
        if f.inFuzzFn {
                panic("testing: f.Cleanup was called inside the f.Fuzz function")
        }
+       f.common.Helper()
        f.common.Cleanup(fn)
 }
 
@@ -78,6 +79,7 @@ func (f *F) Error(args ...interface{}) {
        if f.inFuzzFn {
                panic("testing: f.Error was called inside the f.Fuzz function")
        }
+       f.common.Helper()
        f.common.Error(args...)
 }
 
@@ -86,6 +88,7 @@ func (f *F) Errorf(format string, args ...interface{}) {
        if f.inFuzzFn {
                panic("testing: f.Errorf was called inside the f.Fuzz function")
        }
+       f.common.Helper()
        f.common.Errorf(format, args...)
 }
 
@@ -94,6 +97,7 @@ func (f *F) Fail() {
        if f.inFuzzFn {
                panic("testing: f.Fail was called inside the f.Fuzz function")
        }
+       f.common.Helper()
        f.common.Fail()
 }
 
@@ -109,6 +113,7 @@ func (f *F) FailNow() {
        if f.inFuzzFn {
                panic("testing: f.FailNow was called inside the f.Fuzz function")
        }
+       f.common.Helper()
        f.common.FailNow()
 }
 
@@ -117,6 +122,7 @@ func (f *F) Fatal(args ...interface{}) {
        if f.inFuzzFn {
                panic("testing: f.Fatal was called inside the f.Fuzz function")
        }
+       f.common.Helper()
        f.common.Fatal(args...)
 }
 
@@ -125,6 +131,7 @@ func (f *F) Fatalf(format string, args ...interface{}) {
        if f.inFuzzFn {
                panic("testing: f.Fatalf was called inside the f.Fuzz function")
        }
+       f.common.Helper()
        f.common.Fatalf(format, args...)
 }
 
@@ -135,7 +142,25 @@ func (f *F) Helper() {
        if f.inFuzzFn {
                panic("testing: f.Helper was called inside the f.Fuzz function")
        }
-       f.common.Helper()
+
+       // common.Helper is inlined here.
+       // If we called it, it would mark F.Helper as the helper
+       // instead of the caller.
+       f.mu.Lock()
+       defer f.mu.Unlock()
+       if f.helperPCs == nil {
+               f.helperPCs = make(map[uintptr]struct{})
+       }
+       // repeating code from callerName here to save walking a stack frame
+       var pc [1]uintptr
+       n := runtime.Callers(2, pc[:]) // skip runtime.Callers + Helper
+       if n == 0 {
+               panic("testing: zero callers found")
+       }
+       if _, found := f.helperPCs[pc[0]]; !found {
+               f.helperPCs[pc[0]] = struct{}{}
+               f.helperNames = nil // map will be recreated next time it is needed
+       }
 }
 
 // Skip is equivalent to Log followed by SkipNow.
@@ -143,6 +168,7 @@ func (f *F) Skip(args ...interface{}) {
        if f.inFuzzFn {
                panic("testing: f.Skip was called inside the f.Fuzz function")
        }
+       f.common.Helper()
        f.common.Skip(args...)
 }
 
@@ -158,6 +184,7 @@ func (f *F) SkipNow() {
        if f.inFuzzFn {
                panic("testing: f.SkipNow was called inside the f.Fuzz function")
        }
+       f.common.Helper()
        f.common.SkipNow()
 }
 
@@ -166,6 +193,7 @@ func (f *F) Skipf(format string, args ...interface{}) {
        if f.inFuzzFn {
                panic("testing: f.Skipf was called inside the f.Fuzz function")
        }
+       f.common.Helper()
        f.common.Skipf(format, args...)
 }
 
@@ -178,6 +206,7 @@ func (f *F) TempDir() string {
        if f.inFuzzFn {
                panic("testing: f.TempDir was called inside the f.Fuzz function")
        }
+       f.common.Helper()
        return f.common.TempDir()
 }
 
index d5481d6608189fb7a127f447aef204ddfdc48ce9..8f587b2e1d366d2ecf5443f915d10b752b24d9e1 100644 (file)
@@ -166,7 +166,7 @@ func (TestDeps) RunFuzzWorker(fn func(fuzz.CorpusEntry) error) error {
        if err == ctx.Err() {
                return nil
        }
-       return nil
+       return err
 }
 
 func (TestDeps) ReadCorpus(dir string, types []reflect.Type) ([]fuzz.CorpusEntry, error) {