diff options
| author | Katie Hockman <katie@golang.org> | 2020-09-15 15:13:31 -0400 |
|---|---|---|
| committer | Filippo Valsorda <filippo@golang.org> | 2020-12-04 19:17:29 +0100 |
| commit | dc8f16829a253dbbaffe278dcbf38534ced8403a (patch) | |
| tree | 117f406590f0cf6f172dbbdd2373d1887a16de02 /src/testing | |
| parent | 5ce83d3bdaa97012150e7e20941d1a0ceb2cd7db (diff) | |
| download | go-dc8f16829a253dbbaffe278dcbf38534ced8403a.tar.xz | |
[dev.fuzz] testing: add support for testing.F.Add of []byte
Change-Id: I183693fec6a643b2f27cc379a422e2b42d8eca90
Reviewed-on: https://go-review.googlesource.com/c/go/+/255339
Run-TryBot: Katie Hockman <katie@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Trust: Katie Hockman <katie@golang.org>
Diffstat (limited to 'src/testing')
| -rw-r--r-- | src/testing/fuzz.go | 19 | ||||
| -rw-r--r-- | src/testing/fuzz_test.go | 42 |
2 files changed, 58 insertions, 3 deletions
diff --git a/src/testing/fuzz.go b/src/testing/fuzz.go index ee7f68e544..d159f2e425 100644 --- a/src/testing/fuzz.go +++ b/src/testing/fuzz.go @@ -39,11 +39,24 @@ type corpusEntry struct { b []byte } -// Add will add the arguments to the seed corpus for the fuzz target. This -// cannot be invoked after or within the Fuzz function. The args must match +// Add will add the arguments to the seed corpus for the fuzz target. This will +// be a no-op if called after or within the Fuzz function. The args must match // those in the Fuzz function. func (f *F) Add(args ...interface{}) { - return + if len(args) == 0 { + panic("testing: Add must have at least one argument") + } + if len(args) != 1 { + // TODO: support more than one argument + panic("testing: Add only supports one argument currently") + } + switch v := args[0].(type) { + case []byte: + f.corpus = append(f.corpus, corpusEntry{v}) + // TODO: support other types + default: + panic("testing: Add only supports []byte currently") + } } // Fuzz runs the fuzz function, ff, for fuzz testing. It runs ff in a separate diff --git a/src/testing/fuzz_test.go b/src/testing/fuzz_test.go new file mode 100644 index 0000000000..77a7d5ea4e --- /dev/null +++ b/src/testing/fuzz_test.go @@ -0,0 +1,42 @@ +package testing_test + +import ( + "testing" +) + +func TestFuzzAdd(t *testing.T) { + matchFunc := func(a, b string) (bool, error) { return true, nil } + tests := []struct { + name string + fn func(f *testing.F) + ok bool + }{ + { + "empty", + func(f *testing.F) { f.Add() }, + false, + }, + { + "multiple arguments", + func(f *testing.F) { f.Add([]byte("hello"), []byte("bye")) }, + false, + }, + { + "string", + func(f *testing.F) { f.Add("hello") }, + false, + }, + { + "bytes", + func(f *testing.F) { f.Add([]byte("hello")) }, + true, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + if got, want := testing.RunFuzzTargets(matchFunc, []testing.InternalFuzzTarget{{Fn: tc.fn}}), tc.ok; got != want { + t.Errorf("testing.Add: ok %t, want %t", got, want) + } + }) + } +} |
