MCPcopy
hub / github.com/protocolbuffers/protobuf-go / compareEquivalentProtos

Function compareEquivalentProtos

proto/fuzz_test.go:24–66  ·  view source on GitHub ↗

compareEquivalentProtos compares equivalent messages m0 and m1, where one is typically a Protobuf Editions message and the other isn't. It unmarshals the wireBytes into a message of type m0 and one of type m1 and compares the resulting messages for equality (ignoring type names). m0 and m1 must desc

(t *testing.T, wireBytes []byte, m0, m1 proto.Message)

Source from the content-addressed store, hash-verified

22// describe equivalent messages, meaning having the same field numbers and
23// types.
24func compareEquivalentProtos(t *testing.T, wireBytes []byte, m0, m1 proto.Message) {
25 t.Helper()
26 m0Instance := m0.ProtoReflect().Type().New().Interface()
27 errM0 := proto.Unmarshal(wireBytes, m0Instance)
28 m1Instance := m1.ProtoReflect().Type().New().Interface()
29 errM1 := proto.Unmarshal(wireBytes, m1Instance)
30
31 // Check that the error are the same (possible nil)
32 errorsMatch := (errM1 != nil) == (errM0 != nil)
33 if errM1 != nil && errM0 != nil {
34 errorsMatch = errM1.Error() == errM0.Error()
35 }
36 if !errorsMatch {
37 t.Fatalf("errors not equal:\n%T error: %v\n%T error:%v", m0, errM0, m1, errM1)
38 }
39
40 // Marshal the editions proto and unmarshal it into the equivalent proto2
41 // message to be able to compare the messages.
42 // This tests slightly more than necessary but should only lead to more
43 // coverage (unless the marshalling would undo errors of the unmarshalling
44 // which is very unlikely).
45 roundTrippedM0 := m0.ProtoReflect().Type().New().Interface()
46 err := roundTripMessage(roundTrippedM0, m1Instance)
47 if err != nil {
48 t.Fatalf("failed round tripping proto: %v", err)
49 }
50
51 // The cmp package does not deal with NaN on its own and will report
52 // NaN != NaN.
53 optNaN64 := cmp.Comparer(func(x, y float32) bool {
54 return (math.IsNaN(float64(x)) && math.IsNaN(float64(y))) || x == y
55 })
56 optNaN32 := cmp.Comparer(func(x, y float64) bool {
57 return (math.IsNaN(x) && math.IsNaN(y)) || x == y
58 })
59 if diff := cmp.Diff(m0Instance, roundTrippedM0, protocmp.Transform(), optNaN64, optNaN32); diff != "" {
60 t.Error(diff)
61 }
62
63 if sizeM0, sizeM1 := proto.Size(m0Instance), proto.Size(m1Instance); sizeM0 != sizeM1 {
64 t.Errorf("proto.Size() not equal:\n%T size = %v\n%T size = %v", m0, sizeM0, m1, sizeM1)
65 }
66}
67
68func FuzzProto2EditionConversion(f *testing.F) {
69 f.Add([]byte("Hello World!"))

Callers 2

Calls 9

UnmarshalFunction · 0.92
TransformFunction · 0.92
SizeFunction · 0.92
roundTripMessageFunction · 0.85
InterfaceMethod · 0.65
NewMethod · 0.65
TypeMethod · 0.65
ProtoReflectMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected