fuzz.go 910 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. // +build gofuzz
  5. // Package wire includes a fuzzer for the wire marshaler and unmarshaler.
  6. package wire
  7. import (
  8. "google.golang.org/protobuf/proto"
  9. testpb "google.golang.org/protobuf/internal/testprotos/test"
  10. )
  11. // Fuzz is a fuzzer for proto.Marshal and proto.Unmarshal.
  12. func Fuzz(data []byte) int {
  13. score := 0
  14. for _, newf := range []func() proto.Message{
  15. func() proto.Message { return &testpb.TestAllTypes{} },
  16. } {
  17. m1 := newf()
  18. if err := proto.Unmarshal(data, m1); err != nil {
  19. continue
  20. }
  21. score = 1
  22. data1, err := proto.Marshal(m1)
  23. if err != nil {
  24. panic(err)
  25. }
  26. m2 := newf()
  27. if err := proto.Unmarshal(data1, m2); err != nil {
  28. panic(err)
  29. }
  30. if !proto.Equal(m1, m2) {
  31. panic("not equal")
  32. }
  33. }
  34. return score
  35. }