blob: 348d9005d801061dfc3736619b3a618b54370232 [file] [log] [blame]
Damien Neilc37adef2019-04-01 13:49:56 -07001// 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
Damien Neil99f24c32019-03-13 17:06:42 -07005package proto_test
6
7import (
8 "bytes"
9 "fmt"
Damien Neil01c0e8d2019-11-12 12:33:12 -080010 "reflect"
Damien Neil99f24c32019-03-13 17:06:42 -070011 "testing"
12
Damien Neil99f24c32019-03-13 17:06:42 -070013 "github.com/google/go-cmp/cmp"
Damien Neil01c0e8d2019-11-12 12:33:12 -080014 "google.golang.org/protobuf/internal/encoding/wire"
Joe Tsaic51e2e02019-07-13 00:44:41 -070015 "google.golang.org/protobuf/internal/flags"
Damien Neile89e6242019-05-13 23:55:40 -070016 "google.golang.org/protobuf/proto"
Damien Neil01c0e8d2019-11-12 12:33:12 -080017 pref "google.golang.org/protobuf/reflect/protoreflect"
Damien Neil3016b732019-04-07 12:43:10 -070018
Damien Neil01c0e8d2019-11-12 12:33:12 -080019 orderpb "google.golang.org/protobuf/internal/testprotos/order"
Joe Tsai9b22b932019-08-08 19:23:32 -070020 testpb "google.golang.org/protobuf/internal/testprotos/test"
Damien Neile89e6242019-05-13 23:55:40 -070021 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Damien Neil99f24c32019-03-13 17:06:42 -070022)
23
24func TestEncode(t *testing.T) {
25 for _, test := range testProtos {
26 for _, want := range test.decodeTo {
27 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070028 opts := proto.MarshalOptions{
29 AllowPartial: test.partial,
30 }
31 wire, err := opts.Marshal(want)
Damien Neil99f24c32019-03-13 17:06:42 -070032 if err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070033 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
34 }
35
36 size := proto.Size(want)
37 if size != len(wire) {
38 t.Errorf("Size and marshal disagree: Size(m)=%v; len(Marshal(m))=%v\nMessage:\n%v", size, len(wire), marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070039 }
40
Joe Tsai378c1322019-04-25 23:48:08 -070041 got := want.ProtoReflect().New().Interface()
Damien Neil96c229a2019-04-03 12:17:24 -070042 uopts := proto.UnmarshalOptions{
43 AllowPartial: test.partial,
44 }
45 if err := uopts.Unmarshal(wire, got); err != nil {
Joe Tsai8d30bbe2019-05-16 15:53:25 -070046 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070047 return
48 }
Damien Neile6f060f2019-04-23 17:11:02 -070049 if !proto.Equal(got, want) {
Joe Tsai8d30bbe2019-05-16 15:53:25 -070050 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070051 }
52 })
53 }
54 }
55}
56
57func TestEncodeDeterministic(t *testing.T) {
58 for _, test := range testProtos {
59 for _, want := range test.decodeTo {
60 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070061 opts := proto.MarshalOptions{
62 Deterministic: true,
63 AllowPartial: test.partial,
64 }
65 wire, err := opts.Marshal(want)
Damien Neil99f24c32019-03-13 17:06:42 -070066 if err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070067 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070068 }
Damien Neil96c229a2019-04-03 12:17:24 -070069 wire2, err := opts.Marshal(want)
Damien Neil99f24c32019-03-13 17:06:42 -070070 if err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070071 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070072 }
Damien Neil99f24c32019-03-13 17:06:42 -070073 if !bytes.Equal(wire, wire2) {
74 t.Fatalf("deterministic marshal returned varying results:\n%v", cmp.Diff(wire, wire2))
75 }
76
Joe Tsai378c1322019-04-25 23:48:08 -070077 got := want.ProtoReflect().New().Interface()
Damien Neil96c229a2019-04-03 12:17:24 -070078 uopts := proto.UnmarshalOptions{
79 AllowPartial: test.partial,
80 }
81 if err := uopts.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070082 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070083 return
84 }
Damien Neile6f060f2019-04-23 17:11:02 -070085 if !proto.Equal(got, want) {
Damien Neil61e93c72019-03-27 09:23:20 -070086 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070087 }
88 })
89 }
90 }
91}
Damien Neil96c229a2019-04-03 12:17:24 -070092
Damien Neilbc310b52019-04-11 11:46:55 -070093func TestEncodeInvalidUTF8(t *testing.T) {
94 for _, test := range invalidUTF8TestProtos {
95 for _, want := range test.decodeTo {
96 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil8c86fc52019-06-19 09:28:29 -070097 _, err := proto.Marshal(want)
98 if err == nil {
Damien Neilbc310b52019-04-11 11:46:55 -070099 t.Errorf("Marshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
100 }
Damien Neilbc310b52019-04-11 11:46:55 -0700101 })
102 }
103 }
104}
105
Joe Tsaic51e2e02019-07-13 00:44:41 -0700106func TestEncodeNoEnforceUTF8(t *testing.T) {
107 for _, test := range noEnforceUTF8TestProtos {
108 for _, want := range test.decodeTo {
109 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
110 _, err := proto.Marshal(want)
111 switch {
Joe Tsai1799d112019-08-08 13:31:59 -0700112 case flags.ProtoLegacy && err != nil:
Joe Tsaic51e2e02019-07-13 00:44:41 -0700113 t.Errorf("Marshal returned unexpected error: %v\nMessage:\n%v", err, marshalText(want))
Joe Tsai1799d112019-08-08 13:31:59 -0700114 case !flags.ProtoLegacy && err == nil:
Joe Tsaic51e2e02019-07-13 00:44:41 -0700115 t.Errorf("Marshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
116 }
117 })
118 }
119 }
120}
121
Damien Neil96c229a2019-04-03 12:17:24 -0700122func TestEncodeRequiredFieldChecks(t *testing.T) {
123 for _, test := range testProtos {
124 if !test.partial {
125 continue
126 }
127 for _, m := range test.decodeTo {
128 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
129 _, err := proto.Marshal(m)
130 if err == nil {
131 t.Fatalf("Marshal succeeded (want error)\nMessage:\n%v", marshalText(m))
132 }
133 })
134 }
135 }
136}
Damien Neil3016b732019-04-07 12:43:10 -0700137
Joe Tsai9b22b932019-08-08 19:23:32 -0700138func TestEncodeAppend(t *testing.T) {
Damien Neil3016b732019-04-07 12:43:10 -0700139 want := []byte("prefix")
140 got := append([]byte(nil), want...)
141 got, err := proto.MarshalOptions{}.MarshalAppend(got, &test3pb.TestAllTypes{
142 OptionalString: "value",
143 })
144 if err != nil {
145 t.Fatal(err)
146 }
147 if !bytes.HasPrefix(got, want) {
148 t.Fatalf("MarshalAppend modified prefix: got %v, want prefix %v", got, want)
149 }
150}
Joe Tsai9b22b932019-08-08 19:23:32 -0700151
152func TestEncodeOneofNilWrapper(t *testing.T) {
153 m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)}
154 b, err := proto.Marshal(m)
155 if err != nil {
156 t.Fatal(err)
157 }
158 if len(b) > 0 {
159 t.Errorf("Marshal return non-empty, want empty")
160 }
161}
Damien Neil1e5516a2019-09-27 14:31:10 -0700162
163func TestMarshalAppendAllocations(t *testing.T) {
164 m := &test3pb.TestAllTypes{OptionalInt32: 1}
165 size := proto.Size(m)
166 const count = 1000
167 b := make([]byte, size)
168 // AllocsPerRun returns an integral value.
169 marshalAllocs := testing.AllocsPerRun(count, func() {
170 _, err := proto.MarshalOptions{}.MarshalAppend(b[:0], m)
171 if err != nil {
172 t.Fatal(err)
173 }
174 })
175 b = nil
176 marshalAppendAllocs := testing.AllocsPerRun(count, func() {
177 var err error
178 b, err = proto.MarshalOptions{}.MarshalAppend(b, m)
179 if err != nil {
180 t.Fatal(err)
181 }
182 })
183 if marshalAllocs != marshalAppendAllocs {
184 t.Errorf("%v allocs/op when writing to a preallocated buffer", marshalAllocs)
185 t.Errorf("%v allocs/op when repeatedly appending to a slice", marshalAppendAllocs)
186 t.Errorf("expect amortized allocs/op to be identical")
187 }
188}
Damien Neil01c0e8d2019-11-12 12:33:12 -0800189
190func TestEncodeOrder(t *testing.T) {
191 // We make no guarantees about the stability of wire marshal output.
192 // The order in which fields are marshaled may change over time.
193 // If deterministic marshaling is not enabled, it may change over
194 // successive calls to proto.Marshal in the same binary.
195 //
196 // Unfortunately, many users have come to rely on the specific current
197 // wire marshal output. Perhaps someday we will choose to deliberately
198 // change the marshal output; until that day comes, this test verifies
199 // that we don't unintentionally change it.
200 m := &orderpb.Message{
201 Field_1: proto.String("one"),
202 Field_2: proto.String("two"),
203 Field_20: proto.String("twenty"),
204 Oneof_1: &orderpb.Message_Field_10{"ten"},
205 }
206 proto.SetExtension(m, orderpb.E_Field_30, "thirty")
207 proto.SetExtension(m, orderpb.E_Field_31, "thirty-one")
208 proto.SetExtension(m, orderpb.E_Field_32, "thirty-two")
209 want := []pref.FieldNumber{
210 30, 31, 32, // extensions first, in number order
211 1, 2, 20, // non-extension, non-oneof in number order
212 10, // oneofs last, undefined order
213 }
214
215 // Test with deterministic serialization, since fields are not sorted without
216 // it when -tags=protoreflect.
217 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
218 if err != nil {
219 t.Fatal(err)
220 }
221 var got []pref.FieldNumber
222 for len(b) > 0 {
223 num, _, n := wire.ConsumeField(b)
224 if n < 0 {
225 t.Fatal(wire.ParseError(n))
226 }
227 b = b[n:]
228 got = append(got, num)
229 }
230 if !reflect.DeepEqual(got, want) {
231 t.Errorf("unexpected field marshal order:\ngot: %v\nwant: %v\nmessage:\n%v", got, want, m)
232 }
233}