forked from golang/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/analysis/passes/tests: Check malformed fuzz target.
This will validate that first letter after FuzzFoo() should be uppercase Also, following validation will be performed for f.Fuzz() calls : 1. f.Fuzz() should call a function and it should be of type (*testing.F).Fuzz(). 2. The called function in f.Fuzz(func(){}) should not return result. 3. First argument of func() should be of type *testing.T 4. Second argument onwards should be of type []byte, string, bool, byte, rune, float32, float64, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64 For golang/go#50198 Change-Id: I540daf635f0fe03d954b010b9b5f8616fd5df47a Reviewed-on: https://go-review.googlesource.com/c/tools/+/374495 Reviewed-by: Robert Findley <[email protected]> Trust: Peter Weinberger <[email protected]>
- Loading branch information
Showing
5 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
package a | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Fuzzfoo(*testing.F) {} // want "first letter after 'Fuzz' must not be lowercase" | ||
|
||
func FuzzBoo(*testing.F) {} // OK because first letter after 'Fuzz' is Uppercase. | ||
|
||
func FuzzCallDifferentFunc(f *testing.F) { | ||
f.Name() //OK | ||
} | ||
|
||
func FuzzFunc(f *testing.F) { | ||
f.Fuzz(func(t *testing.T) {}) // OK "first argument is of type *testing.T" | ||
} | ||
|
||
func FuzzFuncWithArgs(f *testing.F) { | ||
f.Fuzz(func(t *testing.T, i int, b []byte) {}) // OK "arguments in func are allowed" | ||
} | ||
|
||
func FuzzArgFunc(f *testing.F) { | ||
f.Fuzz(0) // want "argument to Fuzz must be a function" | ||
} | ||
|
||
func FuzzFuncWithReturn(f *testing.F) { | ||
f.Fuzz(func(t *testing.T) bool { return true }) // want "fuzz target must not return any value" | ||
} | ||
|
||
func FuzzFuncNoArg(f *testing.F) { | ||
f.Fuzz(func() {}) // want "fuzz target must have 1 or more argument" | ||
} | ||
|
||
func FuzzFuncFirstArgNotTesting(f *testing.F) { | ||
f.Fuzz(func(i int64) {}) // want "the first parameter of a fuzz target must be \\*testing.T" | ||
} | ||
|
||
func FuzzFuncFirstArgTestingNotT(f *testing.F) { | ||
f.Fuzz(func(t *testing.F) {}) // want "the first parameter of a fuzz target must be \\*testing.T" | ||
} | ||
|
||
func FuzzFuncSecondArgNotAllowed(f *testing.F) { | ||
f.Fuzz(func(t *testing.T, i complex64) {}) // want "fuzzing arguments can only have the following types: string, bool, float32, float64, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, \\[\\]byte" | ||
} | ||
|
||
func FuzzFuncSecondArgArrNotAllowed(f *testing.F) { | ||
f.Fuzz(func(t *testing.T, i []int) {}) // want "fuzzing arguments can only have the following types: string, bool, float32, float64, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, \\[\\]byte" | ||
} | ||
|
||
func FuzzFuncInner(f *testing.F) { | ||
innerFunc := func(t *testing.T, i float32) {} | ||
f.Fuzz(innerFunc) // ok | ||
} | ||
|
||
func FuzzArrayOfFunc(f *testing.F) { | ||
var funcs = []func(t *testing.T, i int){func(t *testing.T, i int) {}} | ||
f.Fuzz(funcs[0]) // ok | ||
} | ||
|
||
type GenericSlice[T any] []T | ||
|
||
func FuzzGenericFunc(f *testing.F) { | ||
g := GenericSlice[func(t *testing.T, i int)]{func(t *testing.T, i int) {}} | ||
f.Fuzz(g[0]) // ok | ||
} | ||
|
||
type F func(t *testing.T, i int32) | ||
|
||
type myType struct { | ||
myVar F | ||
} | ||
|
||
func FuzzObjectMethod(f *testing.F) { | ||
obj := myType{ | ||
myVar: func(t *testing.T, i int32) {}, | ||
} | ||
f.Fuzz(obj.myVar) // ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters