...

Source file src/google.golang.org/protobuf/proto/fuzz_test.go

Documentation: google.golang.org/protobuf/proto

     1  // Copyright 2024 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  // Go native fuzzing was added in go1.18. Remove this once we stop supporting
     6  // go1.17.
     7  //go:build go1.18
     8  
     9  package proto_test
    10  
    11  import (
    12  	"math"
    13  	"testing"
    14  
    15  	"github.com/google/go-cmp/cmp"
    16  	"google.golang.org/protobuf/proto"
    17  	"google.golang.org/protobuf/testing/protocmp"
    18  
    19  	testfuzzpb "google.golang.org/protobuf/internal/testprotos/editionsfuzztest"
    20  )
    21  
    22  // compareEquivalentProtos compares equivalent messages m0 and m1, where one is
    23  // typically a Protobuf Editions message and the other isn't. It unmarshals the
    24  // wireBytes into a message of type m0 and one of type m1 and compares the
    25  // resulting messages for equality (ignoring type names). m0 and m1 must
    26  // describe equivalent messages, meaning having the same field numbers and
    27  // types.
    28  func compareEquivalentProtos(t *testing.T, wireBytes []byte, m0, m1 proto.Message) {
    29  	t.Helper()
    30  	m0Instance := m0.ProtoReflect().Type().New().Interface()
    31  	errM0 := proto.Unmarshal(wireBytes, m0Instance)
    32  	m1Instance := m1.ProtoReflect().Type().New().Interface()
    33  	errM1 := proto.Unmarshal(wireBytes, m1Instance)
    34  
    35  	// Check that the error are the same (possible nil)
    36  	errorsMatch := (errM1 != nil) == (errM0 != nil)
    37  	if errM1 != nil && errM0 != nil {
    38  		errorsMatch = errM1.Error() == errM0.Error()
    39  	}
    40  	if !errorsMatch {
    41  		t.Fatalf("errors not equal:\n%T error: %v\n%T error:%v", m0, errM0, m1, errM1)
    42  	}
    43  
    44  	// Marshal the editions proto and unmarshal it into the equivalent proto2
    45  	// message to be able to compare the messages.
    46  	// This tests slightly more than necessary but should only lead to more
    47  	// coverage (unless the marshalling would undo errors of the unmarshalling
    48  	// which is very unlikely).
    49  	roundTrippedM0 := m0.ProtoReflect().Type().New().Interface()
    50  	err := roundTripMessage(roundTrippedM0, m1Instance)
    51  	if err != nil {
    52  		t.Fatalf("failed round tripping proto: %v", err)
    53  	}
    54  
    55  	// The cmp package does not deal with NaN on its own and will report
    56  	// NaN != NaN.
    57  	optNaN64 := cmp.Comparer(func(x, y float32) bool {
    58  		return (math.IsNaN(float64(x)) && math.IsNaN(float64(y))) || x == y
    59  	})
    60  	optNaN32 := cmp.Comparer(func(x, y float64) bool {
    61  		return (math.IsNaN(x) && math.IsNaN(y)) || x == y
    62  	})
    63  	if diff := cmp.Diff(m0Instance, roundTrippedM0, protocmp.Transform(), optNaN64, optNaN32); diff != "" {
    64  		t.Error(diff)
    65  	}
    66  
    67  	if sizeM0, sizeM1 := proto.Size(m0Instance), proto.Size(m1Instance); sizeM0 != sizeM1 {
    68  		t.Errorf("proto.Size() not equal:\n%T size = %v\n%T size = %v", m0, sizeM0, m1, sizeM1)
    69  	}
    70  }
    71  
    72  func FuzzProto2EditionConversion(f *testing.F) {
    73  	f.Add([]byte("Hello World!"))
    74  	f.Add([]byte("\x82\x01\x010"))
    75  	f.Fuzz(func(t *testing.T, in []byte) {
    76  		compareEquivalentProtos(t, in, (*testfuzzpb.TestAllTypesProto2)(nil), (*testfuzzpb.TestAllTypesProto2Editions)(nil))
    77  	})
    78  }
    79  
    80  func FuzzProto3EditionConversion(f *testing.F) {
    81  	f.Add([]byte("Hello World!"))
    82  	f.Add([]byte("\x82\x01\x010"))
    83  	f.Fuzz(func(t *testing.T, in []byte) {
    84  		compareEquivalentProtos(t, in, (*testfuzzpb.TestAllTypesProto3)(nil), (*testfuzzpb.TestAllTypesProto3Editions)(nil))
    85  	})
    86  }
    87  

View as plain text