Damien Neil | 26451c0 | 2019-12-19 14:17:21 -0800 | [diff] [blame^] | 1 | // Copyright 2019 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package textfuzz includes a fuzzer for the text marshaler and unmarshaler. |
| 6 | package textfuzz |
| 7 | |
| 8 | import ( |
| 9 | "google.golang.org/protobuf/encoding/prototext" |
| 10 | "google.golang.org/protobuf/proto" |
| 11 | |
| 12 | fuzzpb "google.golang.org/protobuf/internal/testprotos/fuzz" |
| 13 | ) |
| 14 | |
| 15 | // Fuzz is a fuzzer for proto.Marshal and proto.Unmarshal. |
| 16 | func Fuzz(data []byte) (score int) { |
| 17 | m1 := &fuzzpb.Fuzz{} |
| 18 | if err := (prototext.UnmarshalOptions{ |
| 19 | AllowPartial: true, |
| 20 | }).Unmarshal(data, m1); err != nil { |
| 21 | return 0 |
| 22 | } |
| 23 | data1, err := prototext.MarshalOptions{ |
| 24 | AllowPartial: true, |
| 25 | }.Marshal(m1) |
| 26 | if err != nil { |
| 27 | panic(err) |
| 28 | } |
| 29 | m2 := &fuzzpb.Fuzz{} |
| 30 | if err := (prototext.UnmarshalOptions{ |
| 31 | AllowPartial: true, |
| 32 | }).Unmarshal(data1, m2); err != nil { |
| 33 | return 0 |
| 34 | } |
| 35 | if !proto.Equal(m1, m2) { |
| 36 | panic("not equal") |
| 37 | } |
| 38 | return 1 |
| 39 | } |