internal/fuzz: add fuzzers for prototext and protojson packages
Change-Id: Iee065070e6a983c303a3551a67fc32f0e94b649e
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/212219
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/internal/fuzz/textfuzz/fuzz.go b/internal/fuzz/textfuzz/fuzz.go
new file mode 100644
index 0000000..bd89da0
--- /dev/null
+++ b/internal/fuzz/textfuzz/fuzz.go
@@ -0,0 +1,39 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package textfuzz includes a fuzzer for the text marshaler and unmarshaler.
+package textfuzz
+
+import (
+ "google.golang.org/protobuf/encoding/prototext"
+ "google.golang.org/protobuf/proto"
+
+ fuzzpb "google.golang.org/protobuf/internal/testprotos/fuzz"
+)
+
+// Fuzz is a fuzzer for proto.Marshal and proto.Unmarshal.
+func Fuzz(data []byte) (score int) {
+ m1 := &fuzzpb.Fuzz{}
+ if err := (prototext.UnmarshalOptions{
+ AllowPartial: true,
+ }).Unmarshal(data, m1); err != nil {
+ return 0
+ }
+ data1, err := prototext.MarshalOptions{
+ AllowPartial: true,
+ }.Marshal(m1)
+ if err != nil {
+ panic(err)
+ }
+ m2 := &fuzzpb.Fuzz{}
+ if err := (prototext.UnmarshalOptions{
+ AllowPartial: true,
+ }).Unmarshal(data1, m2); err != nil {
+ return 0
+ }
+ if !proto.Equal(m1, m2) {
+ panic("not equal")
+ }
+ return 1
+}