internal/fuzz/wire: add test, seed corpus

Test the fuzzer with a minimal seed corpus. (Currently one file
containing a valid TestAllTypes messge with most fields set.)

Change-Id: I8dcec4e26f1e8374993cc3a4bef0496f36cccd41
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/201639
Reviewed-by: Herbie Ong <herbie@google.com>
This commit is contained in:
Damien Neil 2019-10-17 09:42:03 -07:00
parent ae313d4af3
commit 3770776dcd
3 changed files with 36 additions and 2 deletions

View File

@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build gofuzz
// Package wire includes a fuzzer for the wire marshaler and unmarshaler.
package wire
@ -11,6 +9,7 @@ import (
"google.golang.org/protobuf/proto"
testpb "google.golang.org/protobuf/internal/testprotos/test"
test3pb "google.golang.org/protobuf/internal/testprotos/test3"
)
// Fuzz is a fuzzer for proto.Marshal and proto.Unmarshal.
@ -18,6 +17,8 @@ func Fuzz(data []byte) int {
score := 0
for _, newf := range []func() proto.Message{
func() proto.Message { return &testpb.TestAllTypes{} },
func() proto.Message { return &testpb.TestAllExtensions{} },
func() proto.Message { return &test3pb.TestAllTypes{} },
} {
m1 := newf()
if err := proto.Unmarshal(data, m1); err != nil {
@ -28,6 +29,9 @@ func Fuzz(data []byte) int {
if err != nil {
panic(err)
}
if proto.Size(m1) != len(data1) {
panic("size does not match output")
}
m2 := newf()
if err := proto.Unmarshal(data1, m2); err != nil {
panic(err)

View File

@ -0,0 +1,30 @@
package wire
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func Test(t *testing.T) {
dir, err := os.Open("corpus")
if err != nil {
t.Fatal(err)
}
infos, err := dir.Readdir(0)
if err != nil {
t.Fatal(err)
}
for _, info := range infos {
name := info.Name()
t.Run(name, func(t *testing.T) {
b, err := ioutil.ReadFile(filepath.Join("corpus", name))
if err != nil {
t.Fatal(err)
}
Fuzz(b)
})
}
}