-
Notifications
You must be signed in to change notification settings - Fork 570
Detect and execute fuzz targets as tests #1132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4845bda
Refactor test functions finding into a separate package and add tests.
nevkontakte b19a3f1
Detect and execute Fuzz targets with seed inputs.
nevkontakte f13b14b
Fix generics-related error in net/netip fuzz tests.
nevkontakte 2db03cc
Update VFS.
nevkontakte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,10 @@ | ||
//go:build js | ||
|
||
package netip_test | ||
|
||
import "testing" | ||
|
||
func checkStringParseRoundTrip(t *testing.T, x interface{}, parse interface{}) { | ||
// TODO(nevkontakte): This function requires generics to function. | ||
// Re-enable after https://github.com/gopherjs/gopherjs/issues/1013 is resolved. | ||
} |
This file contains hidden or 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 hidden or 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,16 @@ | ||
package testpkg_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestYyy(t *testing.T) {} | ||
|
||
func BenchmarkYyy(b *testing.B) {} | ||
|
||
func FuzzYyy(f *testing.F) { f.Skip() } | ||
|
||
func ExampleYyy() { | ||
fmt.Println("hello") // Output: hello | ||
} |
This file contains hidden or 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,13 @@ | ||
package testpkg | ||
|
||
import "testing" | ||
|
||
func TestXxx(t *testing.T) {} | ||
|
||
func BenchmarkXxx(b *testing.B) {} | ||
|
||
func FuzzXxx(f *testing.F) { f.Skip() } | ||
|
||
func ExampleXxx() {} | ||
|
||
func TestMain(m *testing.M) { m.Run() } |
This file contains hidden or 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,4 @@ | ||
package testpkg | ||
|
||
// Xxx is an sample function. | ||
func Xxx() {} |
This file contains hidden or 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,307 @@ | ||
package testmain | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"go/ast" | ||
gobuild "go/build" | ||
"go/doc" | ||
"go/parser" | ||
"go/token" | ||
"path" | ||
"sort" | ||
"strings" | ||
"text/template" | ||
"unicode" | ||
"unicode/utf8" | ||
|
||
"github.com/gopherjs/gopherjs/build" | ||
"golang.org/x/tools/go/buildutil" | ||
) | ||
|
||
// FuncLocation describes whether a test function is in-package or external | ||
// (i.e. in the xxx_test package). | ||
type FuncLocation uint8 | ||
|
||
const ( | ||
// LocUnknown is the default, invalid value of the PkgType. | ||
LocUnknown FuncLocation = iota | ||
// LocInPackage is an in-package test. | ||
LocInPackage | ||
// LocExternal is an external test (i.e. in the xxx_test package). | ||
LocExternal | ||
) | ||
|
||
func (tl FuncLocation) String() string { | ||
switch tl { | ||
case LocInPackage: | ||
return "_test" | ||
case LocExternal: | ||
return "_xtest" | ||
default: | ||
return "<unknown>" | ||
} | ||
} | ||
|
||
// TestFunc describes a single test/benchmark/fuzz function in a package. | ||
type TestFunc struct { | ||
Location FuncLocation // Where the function is defined. | ||
Name string // Function name. | ||
} | ||
|
||
// ExampleFunc describes an example. | ||
type ExampleFunc struct { | ||
Location FuncLocation // Where the function is defined. | ||
Name string // Function name. | ||
Output string // Expected output. | ||
Unordered bool // Output is allowed to be unordered. | ||
EmptyOutput bool // Whether the output is expected to be empty. | ||
} | ||
|
||
// Executable returns true if the example function should be executed with tests. | ||
func (ef ExampleFunc) Executable() bool { | ||
return ef.EmptyOutput || ef.Output != "" | ||
} | ||
|
||
// TestMain is a helper type responsible for generation of the test main package. | ||
type TestMain struct { | ||
Package *build.PackageData | ||
Tests []TestFunc | ||
Benchmarks []TestFunc | ||
Fuzz []TestFunc | ||
Examples []ExampleFunc | ||
TestMain *TestFunc | ||
} | ||
|
||
// Scan package for tests functions. | ||
func (tm *TestMain) Scan(fset *token.FileSet) error { | ||
if err := tm.scanPkg(fset, tm.Package.TestGoFiles, LocInPackage); err != nil { | ||
return err | ||
} | ||
if err := tm.scanPkg(fset, tm.Package.XTestGoFiles, LocExternal); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (tm *TestMain) scanPkg(fset *token.FileSet, files []string, loc FuncLocation) error { | ||
for _, name := range files { | ||
srcPath := path.Join(tm.Package.Dir, name) | ||
f, err := buildutil.OpenFile(tm.Package.InternalBuildContext(), srcPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to open source file %q: %w", srcPath, err) | ||
} | ||
defer f.Close() | ||
nevkontakte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parsed, err := parser.ParseFile(fset, srcPath, f, parser.ParseComments) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse %q: %w", srcPath, err) | ||
} | ||
|
||
if err := tm.scanFile(parsed, loc); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (tm *TestMain) scanFile(f *ast.File, loc FuncLocation) error { | ||
for _, d := range f.Decls { | ||
n, ok := d.(*ast.FuncDecl) | ||
if !ok { | ||
continue | ||
} | ||
if n.Recv != nil { | ||
continue | ||
} | ||
name := n.Name.String() | ||
switch { | ||
case isTestMain(n): | ||
if tm.TestMain != nil { | ||
return errors.New("multiple definitions of TestMain") | ||
} | ||
tm.TestMain = &TestFunc{ | ||
Location: loc, | ||
Name: name, | ||
} | ||
case isTest(name, "Test"): | ||
tm.Tests = append(tm.Tests, TestFunc{ | ||
Location: loc, | ||
Name: name, | ||
}) | ||
case isTest(name, "Benchmark"): | ||
tm.Benchmarks = append(tm.Benchmarks, TestFunc{ | ||
Location: loc, | ||
Name: name, | ||
}) | ||
case isTest(name, "Fuzz"): | ||
tm.Fuzz = append(tm.Fuzz, TestFunc{ | ||
Location: loc, | ||
Name: name, | ||
}) | ||
} | ||
} | ||
|
||
ex := doc.Examples(f) | ||
sort.Slice(ex, func(i, j int) bool { return ex[i].Order < ex[j].Order }) | ||
for _, e := range ex { | ||
tm.Examples = append(tm.Examples, ExampleFunc{ | ||
Location: loc, | ||
Name: "Example" + e.Name, | ||
Output: e.Output, | ||
Unordered: e.Unordered, | ||
EmptyOutput: e.EmptyOutput, | ||
}) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Synthesize main package for the tests. | ||
func (tm *TestMain) Synthesize(fset *token.FileSet) (*build.PackageData, *ast.File, error) { | ||
buf := &bytes.Buffer{} | ||
if err := testmainTmpl.Execute(buf, tm); err != nil { | ||
return nil, nil, fmt.Errorf("failed to generate testmain source for package %s: %w", tm.Package.ImportPath, err) | ||
} | ||
src, err := parser.ParseFile(fset, "_testmain.go", buf, 0) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to parse testmain source for package %s: %w", tm.Package.ImportPath, err) | ||
} | ||
pkg := &build.PackageData{ | ||
Package: &gobuild.Package{ | ||
ImportPath: tm.Package.ImportPath + ".testmain", | ||
Name: "main", | ||
GoFiles: []string{"_testmain.go"}, | ||
}, | ||
} | ||
return pkg, src, nil | ||
} | ||
|
||
func (tm *TestMain) hasTests(loc FuncLocation, executableOnly bool) bool { | ||
if tm.TestMain != nil && tm.TestMain.Location == loc { | ||
return true | ||
} | ||
// Tests, Benchmarks and Fuzz targets are always executable. | ||
all := []TestFunc{} | ||
all = append(all, tm.Tests...) | ||
all = append(all, tm.Benchmarks...) | ||
|
||
for _, t := range all { | ||
if t.Location == loc { | ||
return true | ||
} | ||
} | ||
|
||
for _, e := range tm.Examples { | ||
if e.Location == loc && (e.Executable() || !executableOnly) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// ImportTest returns true if in-package test package needs to be imported. | ||
func (tm *TestMain) ImportTest() bool { return tm.hasTests(LocInPackage, false) } | ||
|
||
// ImportXTest returns true if external test package needs to be imported. | ||
func (tm *TestMain) ImportXTest() bool { return tm.hasTests(LocExternal, false) } | ||
|
||
// ExecutesTest returns true if in-package test package has executable tests. | ||
func (tm *TestMain) ExecutesTest() bool { return tm.hasTests(LocInPackage, true) } | ||
|
||
// ExecutesXTest returns true if external package test package has executable tests. | ||
func (tm *TestMain) ExecutesXTest() bool { return tm.hasTests(LocExternal, true) } | ||
|
||
// isTestMain tells whether fn is a TestMain(m *testing.M) function. | ||
func isTestMain(fn *ast.FuncDecl) bool { | ||
if fn.Name.String() != "TestMain" || | ||
fn.Type.Results != nil && len(fn.Type.Results.List) > 0 || | ||
fn.Type.Params == nil || | ||
len(fn.Type.Params.List) != 1 || | ||
len(fn.Type.Params.List[0].Names) > 1 { | ||
return false | ||
} | ||
ptr, ok := fn.Type.Params.List[0].Type.(*ast.StarExpr) | ||
if !ok { | ||
return false | ||
} | ||
// We can't easily check that the type is *testing.M | ||
// because we don't know how testing has been imported, | ||
// but at least check that it's *M or *something.M. | ||
if name, ok := ptr.X.(*ast.Ident); ok && name.Name == "M" { | ||
return true | ||
} | ||
if sel, ok := ptr.X.(*ast.SelectorExpr); ok && sel.Sel.Name == "M" { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// isTest tells whether name looks like a test (or benchmark, according to prefix). | ||
// It is a Test (say) if there is a character after Test that is not a lower-case letter. | ||
// We don't want TesticularCancer. | ||
nevkontakte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func isTest(name, prefix string) bool { | ||
if !strings.HasPrefix(name, prefix) { | ||
return false | ||
} | ||
if len(name) == len(prefix) { // "Test" is ok | ||
return true | ||
} | ||
rune, _ := utf8.DecodeRuneInString(name[len(prefix):]) | ||
return !unicode.IsLower(rune) | ||
} | ||
|
||
var testmainTmpl = template.Must(template.New("main").Parse(` | ||
package main | ||
|
||
import ( | ||
{{if not .TestMain}} | ||
"os" | ||
{{end}} | ||
"testing" | ||
"testing/internal/testdeps" | ||
|
||
{{if .ImportTest}} | ||
{{if .ExecutesTest}}_test{{else}}_{{end}} {{.Package.ImportPath | printf "%q"}} | ||
{{end -}} | ||
{{- if .ImportXTest -}} | ||
{{if .ExecutesXTest}}_xtest{{else}}_{{end}} {{.Package.ImportPath | printf "%s_test" | printf "%q"}} | ||
{{end}} | ||
) | ||
|
||
var tests = []testing.InternalTest{ | ||
{{- range .Tests}} | ||
{"{{.Name}}", {{.Location}}.{{.Name}}}, | ||
{{- end}} | ||
} | ||
|
||
var benchmarks = []testing.InternalBenchmark{ | ||
{{- range .Benchmarks}} | ||
{"{{.Name}}", {{.Location}}.{{.Name}}}, | ||
{{- end}} | ||
} | ||
|
||
var fuzzTargets = []testing.InternalFuzzTarget{ | ||
{{- range .Fuzz}} | ||
{"{{.Name}}", {{.Location}}.{{.Name}}}, | ||
{{- end}} | ||
} | ||
|
||
var examples = []testing.InternalExample{ | ||
{{- range .Examples }} | ||
{{- if .Executable }} | ||
{"{{.Name}}", {{.Location}}.{{.Name}}, {{.Output | printf "%q"}}, {{.Unordered}}}, | ||
{{- end }} | ||
{{- end }} | ||
} | ||
|
||
func main() { | ||
m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, fuzzTargets, examples) | ||
{{with .TestMain}} | ||
{{.Location}}.{{.Name}}(m) | ||
{{else}} | ||
os.Exit(m.Run()) | ||
{{end -}} | ||
} | ||
|
||
`)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.