blob: 074aeb61693b1bff5b26a4f02ae29463730276c2 [file] [log] [blame]
Damien Neilba23aa52018-12-07 14:38:17 -08001// Copyright 2018 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 Neil4be2fb42018-12-17 11:16:16 -08005package proto_test
Damien Neilba23aa52018-12-07 14:38:17 -08006
7import (
8 "fmt"
9 "reflect"
10 "testing"
11
12 protoV1 "github.com/golang/protobuf/proto"
Damien Neil5c5b5312019-05-14 12:44:37 -070013 "google.golang.org/protobuf/encoding/prototext"
Damien Neile89e6242019-05-13 23:55:40 -070014 "google.golang.org/protobuf/internal/encoding/pack"
15 "google.golang.org/protobuf/internal/errors"
16 "google.golang.org/protobuf/internal/scalar"
17 "google.golang.org/protobuf/proto"
18 pref "google.golang.org/protobuf/reflect/protoreflect"
19 "google.golang.org/protobuf/runtime/protolegacy"
Joe Tsai19058432019-02-27 21:46:29 -080020
Damien Neilc37adef2019-04-01 13:49:56 -070021 legacypb "google.golang.org/protobuf/internal/testprotos/legacy"
22 legacy1pb "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v0.0.0-20160225-2fc053c5"
Damien Neile89e6242019-05-13 23:55:40 -070023 testpb "google.golang.org/protobuf/internal/testprotos/test"
24 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Damien Neilba23aa52018-12-07 14:38:17 -080025)
26
27type testProto struct {
Damien Neil96c229a2019-04-03 12:17:24 -070028 desc string
29 decodeTo []proto.Message
30 wire []byte
31 partial bool
32 invalidExtensions bool
Damien Neilba23aa52018-12-07 14:38:17 -080033}
34
35func TestDecode(t *testing.T) {
36 for _, test := range testProtos {
37 for _, want := range test.decodeTo {
38 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070039 opts := proto.UnmarshalOptions{
40 AllowPartial: test.partial,
41 }
Damien Neilba23aa52018-12-07 14:38:17 -080042 wire := append(([]byte)(nil), test.wire...)
Damien Neil4be2fb42018-12-17 11:16:16 -080043 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
Damien Neil96c229a2019-04-03 12:17:24 -070044 if err := opts.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070045 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080046 return
47 }
48
49 // Aliasing check: Modifying the original wire bytes shouldn't
50 // affect the unmarshaled message.
51 for i := range wire {
52 wire[i] = 0
53 }
54
Damien Neil96c229a2019-04-03 12:17:24 -070055 if test.invalidExtensions {
56 // Equal doesn't work on messages containing invalid extension data.
57 return
58 }
Joe Tsaidb38ddd2019-05-07 15:14:40 -070059 if !proto.Equal(got, want) {
Damien Neil61e93c72019-03-27 09:23:20 -070060 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080061 }
62 })
63 }
64 }
65}
66
Damien Neil96c229a2019-04-03 12:17:24 -070067func TestDecodeRequiredFieldChecks(t *testing.T) {
68 for _, test := range testProtos {
69 if !test.partial {
70 continue
71 }
72 if test.invalidExtensions {
73 // Missing required fields in extensions just end up in the unknown fields.
74 continue
75 }
76 for _, m := range test.decodeTo {
77 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
78 got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
79 if err := proto.Unmarshal(test.wire, got); err == nil {
80 t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got))
81 }
82 })
83 }
84 }
85}
86
Damien Neilbc310b52019-04-11 11:46:55 -070087func TestDecodeInvalidUTF8(t *testing.T) {
88 for _, test := range invalidUTF8TestProtos {
89 for _, want := range test.decodeTo {
90 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
91 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
92 err := proto.Unmarshal(test.wire, got)
93 if !isErrInvalidUTF8(err) {
94 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
95 }
96 if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) {
97 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
98 }
99 })
100 }
101 }
102}
103
Damien Neilba23aa52018-12-07 14:38:17 -0800104var testProtos = []testProto{
105 {
106 desc: "basic scalar types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800107 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800108 OptionalInt32: scalar.Int32(1001),
109 OptionalInt64: scalar.Int64(1002),
110 OptionalUint32: scalar.Uint32(1003),
111 OptionalUint64: scalar.Uint64(1004),
112 OptionalSint32: scalar.Int32(1005),
113 OptionalSint64: scalar.Int64(1006),
114 OptionalFixed32: scalar.Uint32(1007),
115 OptionalFixed64: scalar.Uint64(1008),
116 OptionalSfixed32: scalar.Int32(1009),
117 OptionalSfixed64: scalar.Int64(1010),
118 OptionalFloat: scalar.Float32(1011.5),
119 OptionalDouble: scalar.Float64(1012.5),
120 OptionalBool: scalar.Bool(true),
121 OptionalString: scalar.String("string"),
122 OptionalBytes: []byte("bytes"),
123 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
Damien Neil3b46ade2019-03-26 13:55:02 -0700124 }, &test3pb.TestAllTypes{
125 OptionalInt32: 1001,
126 OptionalInt64: 1002,
127 OptionalUint32: 1003,
128 OptionalUint64: 1004,
129 OptionalSint32: 1005,
130 OptionalSint64: 1006,
131 OptionalFixed32: 1007,
132 OptionalFixed64: 1008,
133 OptionalSfixed32: 1009,
134 OptionalSfixed64: 1010,
135 OptionalFloat: 1011.5,
136 OptionalDouble: 1012.5,
137 OptionalBool: true,
138 OptionalString: "string",
139 OptionalBytes: []byte("bytes"),
140 OptionalNestedEnum: test3pb.TestAllTypes_BAR,
Damien Neilba23aa52018-12-07 14:38:17 -0800141 }, build(
142 &testpb.TestAllExtensions{},
143 extend(testpb.E_OptionalInt32Extension, scalar.Int32(1001)),
144 extend(testpb.E_OptionalInt64Extension, scalar.Int64(1002)),
145 extend(testpb.E_OptionalUint32Extension, scalar.Uint32(1003)),
146 extend(testpb.E_OptionalUint64Extension, scalar.Uint64(1004)),
147 extend(testpb.E_OptionalSint32Extension, scalar.Int32(1005)),
148 extend(testpb.E_OptionalSint64Extension, scalar.Int64(1006)),
149 extend(testpb.E_OptionalFixed32Extension, scalar.Uint32(1007)),
150 extend(testpb.E_OptionalFixed64Extension, scalar.Uint64(1008)),
151 extend(testpb.E_OptionalSfixed32Extension, scalar.Int32(1009)),
152 extend(testpb.E_OptionalSfixed64Extension, scalar.Int64(1010)),
153 extend(testpb.E_OptionalFloatExtension, scalar.Float32(1011.5)),
154 extend(testpb.E_OptionalDoubleExtension, scalar.Float64(1012.5)),
155 extend(testpb.E_OptionalBoolExtension, scalar.Bool(true)),
156 extend(testpb.E_OptionalStringExtension, scalar.String("string")),
157 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
158 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR.Enum()),
159 )},
160 wire: pack.Message{
161 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
162 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
163 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
164 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
165 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
166 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
167 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
168 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
169 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
170 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
171 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
172 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
173 pack.Tag{13, pack.VarintType}, pack.Bool(true),
174 pack.Tag{14, pack.BytesType}, pack.String("string"),
175 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
176 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
177 }.Marshal(),
178 },
179 {
180 desc: "groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800181 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800182 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
183 A: scalar.Int32(1017),
184 },
185 }, build(
186 &testpb.TestAllExtensions{},
187 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
188 A: scalar.Int32(1017),
189 }),
190 )},
191 wire: pack.Message{
192 pack.Tag{16, pack.StartGroupType},
193 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
194 pack.Tag{16, pack.EndGroupType},
195 }.Marshal(),
196 },
197 {
198 desc: "groups (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800199 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800200 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
201 A: scalar.Int32(2),
202 },
203 }, build(
204 &testpb.TestAllExtensions{},
205 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
206 A: scalar.Int32(2),
207 }),
208 )},
209 wire: pack.Message{
210 pack.Tag{16, pack.StartGroupType},
211 pack.Tag{17, pack.VarintType}, pack.Varint(1),
212 pack.Tag{16, pack.EndGroupType},
213 pack.Tag{16, pack.StartGroupType},
214 pack.Tag{17, pack.VarintType}, pack.Varint(2),
215 pack.Tag{16, pack.EndGroupType},
216 }.Marshal(),
217 },
218 {
219 desc: "messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800220 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800221 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
222 A: scalar.Int32(42),
223 Corecursive: &testpb.TestAllTypes{
224 OptionalInt32: scalar.Int32(43),
225 },
226 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700227 }, &test3pb.TestAllTypes{
228 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
229 A: 42,
230 Corecursive: &test3pb.TestAllTypes{
231 OptionalInt32: 43,
232 },
233 },
Damien Neilba23aa52018-12-07 14:38:17 -0800234 }, build(
235 &testpb.TestAllExtensions{},
236 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
237 A: scalar.Int32(42),
238 Corecursive: &testpb.TestAllTypes{
239 OptionalInt32: scalar.Int32(43),
240 },
241 }),
242 )},
243 wire: pack.Message{
244 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
245 pack.Tag{1, pack.VarintType}, pack.Varint(42),
246 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
247 pack.Tag{1, pack.VarintType}, pack.Varint(43),
248 }),
249 }),
250 }.Marshal(),
251 },
252 {
253 desc: "messages (split across multiple tags)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800254 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800255 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
256 A: scalar.Int32(42),
257 Corecursive: &testpb.TestAllTypes{
258 OptionalInt32: scalar.Int32(43),
259 },
260 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700261 }, &test3pb.TestAllTypes{
262 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
263 A: 42,
264 Corecursive: &test3pb.TestAllTypes{
265 OptionalInt32: 43,
266 },
267 },
Damien Neilba23aa52018-12-07 14:38:17 -0800268 }, build(
269 &testpb.TestAllExtensions{},
270 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
271 A: scalar.Int32(42),
272 Corecursive: &testpb.TestAllTypes{
273 OptionalInt32: scalar.Int32(43),
274 },
275 }),
276 )},
277 wire: pack.Message{
278 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
279 pack.Tag{1, pack.VarintType}, pack.Varint(42),
280 }),
281 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
282 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
283 pack.Tag{1, pack.VarintType}, pack.Varint(43),
284 }),
285 }),
286 }.Marshal(),
287 },
288 {
289 desc: "messages (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800290 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800291 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
292 A: scalar.Int32(2),
293 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700294 }, &test3pb.TestAllTypes{
295 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
296 A: 2,
297 },
Damien Neilba23aa52018-12-07 14:38:17 -0800298 }, build(
299 &testpb.TestAllExtensions{},
300 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
301 A: scalar.Int32(2),
302 }),
303 )},
304 wire: pack.Message{
305 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
306 pack.Tag{1, pack.VarintType}, pack.Varint(1),
307 }),
308 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
309 pack.Tag{1, pack.VarintType}, pack.Varint(2),
310 }),
311 }.Marshal(),
312 },
313 {
314 desc: "basic repeated types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800315 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800316 RepeatedInt32: []int32{1001, 2001},
317 RepeatedInt64: []int64{1002, 2002},
318 RepeatedUint32: []uint32{1003, 2003},
319 RepeatedUint64: []uint64{1004, 2004},
320 RepeatedSint32: []int32{1005, 2005},
321 RepeatedSint64: []int64{1006, 2006},
322 RepeatedFixed32: []uint32{1007, 2007},
323 RepeatedFixed64: []uint64{1008, 2008},
324 RepeatedSfixed32: []int32{1009, 2009},
325 RepeatedSfixed64: []int64{1010, 2010},
326 RepeatedFloat: []float32{1011.5, 2011.5},
327 RepeatedDouble: []float64{1012.5, 2012.5},
328 RepeatedBool: []bool{true, false},
329 RepeatedString: []string{"foo", "bar"},
330 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
331 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
332 testpb.TestAllTypes_FOO,
333 testpb.TestAllTypes_BAR,
334 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700335 }, &test3pb.TestAllTypes{
336 RepeatedInt32: []int32{1001, 2001},
337 RepeatedInt64: []int64{1002, 2002},
338 RepeatedUint32: []uint32{1003, 2003},
339 RepeatedUint64: []uint64{1004, 2004},
340 RepeatedSint32: []int32{1005, 2005},
341 RepeatedSint64: []int64{1006, 2006},
342 RepeatedFixed32: []uint32{1007, 2007},
343 RepeatedFixed64: []uint64{1008, 2008},
344 RepeatedSfixed32: []int32{1009, 2009},
345 RepeatedSfixed64: []int64{1010, 2010},
346 RepeatedFloat: []float32{1011.5, 2011.5},
347 RepeatedDouble: []float64{1012.5, 2012.5},
348 RepeatedBool: []bool{true, false},
349 RepeatedString: []string{"foo", "bar"},
350 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
351 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
352 test3pb.TestAllTypes_FOO,
353 test3pb.TestAllTypes_BAR,
354 },
Damien Neilba23aa52018-12-07 14:38:17 -0800355 }, build(
356 &testpb.TestAllExtensions{},
357 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
358 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
359 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
360 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
361 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
362 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
363 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
364 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
365 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
366 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
367 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
368 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
369 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
370 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
371 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
372 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
373 testpb.TestAllTypes_FOO,
374 testpb.TestAllTypes_BAR,
375 }),
376 )},
377 wire: pack.Message{
378 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
379 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
380 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
381 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
382 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
383 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
384 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
385 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
386 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
387 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
388 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
389 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
390 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
391 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
392 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
393 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
394 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
395 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
396 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
397 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
398 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
399 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
400 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
401 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
402 pack.Tag{43, pack.VarintType}, pack.Bool(true),
403 pack.Tag{43, pack.VarintType}, pack.Bool(false),
404 pack.Tag{44, pack.BytesType}, pack.String("foo"),
405 pack.Tag{44, pack.BytesType}, pack.String("bar"),
406 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
407 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
408 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
409 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
410 }.Marshal(),
411 },
412 {
413 desc: "basic repeated types (packed encoding)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800414 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800415 RepeatedInt32: []int32{1001, 2001},
416 RepeatedInt64: []int64{1002, 2002},
417 RepeatedUint32: []uint32{1003, 2003},
418 RepeatedUint64: []uint64{1004, 2004},
419 RepeatedSint32: []int32{1005, 2005},
420 RepeatedSint64: []int64{1006, 2006},
421 RepeatedFixed32: []uint32{1007, 2007},
422 RepeatedFixed64: []uint64{1008, 2008},
423 RepeatedSfixed32: []int32{1009, 2009},
424 RepeatedSfixed64: []int64{1010, 2010},
425 RepeatedFloat: []float32{1011.5, 2011.5},
426 RepeatedDouble: []float64{1012.5, 2012.5},
427 RepeatedBool: []bool{true, false},
428 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
429 testpb.TestAllTypes_FOO,
430 testpb.TestAllTypes_BAR,
431 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700432 }, &test3pb.TestAllTypes{
433 RepeatedInt32: []int32{1001, 2001},
434 RepeatedInt64: []int64{1002, 2002},
435 RepeatedUint32: []uint32{1003, 2003},
436 RepeatedUint64: []uint64{1004, 2004},
437 RepeatedSint32: []int32{1005, 2005},
438 RepeatedSint64: []int64{1006, 2006},
439 RepeatedFixed32: []uint32{1007, 2007},
440 RepeatedFixed64: []uint64{1008, 2008},
441 RepeatedSfixed32: []int32{1009, 2009},
442 RepeatedSfixed64: []int64{1010, 2010},
443 RepeatedFloat: []float32{1011.5, 2011.5},
444 RepeatedDouble: []float64{1012.5, 2012.5},
445 RepeatedBool: []bool{true, false},
446 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
447 test3pb.TestAllTypes_FOO,
448 test3pb.TestAllTypes_BAR,
449 },
Damien Neilba23aa52018-12-07 14:38:17 -0800450 }, build(
451 &testpb.TestAllExtensions{},
452 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
453 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
454 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
455 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
456 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
457 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
458 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
459 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
460 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
461 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
462 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
463 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
464 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
465 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
466 testpb.TestAllTypes_FOO,
467 testpb.TestAllTypes_BAR,
468 }),
469 )},
470 wire: pack.Message{
471 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
472 pack.Varint(1001), pack.Varint(2001),
473 },
474 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
475 pack.Varint(1002), pack.Varint(2002),
476 },
477 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
478 pack.Uvarint(1003), pack.Uvarint(2003),
479 },
480 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
481 pack.Uvarint(1004), pack.Uvarint(2004),
482 },
483 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
484 pack.Svarint(1005), pack.Svarint(2005),
485 },
486 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
487 pack.Svarint(1006), pack.Svarint(2006),
488 },
489 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
490 pack.Uint32(1007), pack.Uint32(2007),
491 },
492 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
493 pack.Uint64(1008), pack.Uint64(2008),
494 },
495 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
496 pack.Int32(1009), pack.Int32(2009),
497 },
498 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
499 pack.Int64(1010), pack.Int64(2010),
500 },
501 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
502 pack.Float32(1011.5), pack.Float32(2011.5),
503 },
504 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
505 pack.Float64(1012.5), pack.Float64(2012.5),
506 },
507 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
508 pack.Bool(true), pack.Bool(false),
509 },
510 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
511 pack.Varint(int(testpb.TestAllTypes_FOO)),
512 pack.Varint(int(testpb.TestAllTypes_BAR)),
513 },
514 }.Marshal(),
515 },
516 {
517 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800518 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800519 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
520 {A: scalar.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700521 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800522 {A: scalar.Int32(2)},
523 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700524 }, &test3pb.TestAllTypes{
525 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
526 {A: 1},
Damien Neilc37adef2019-04-01 13:49:56 -0700527 nil,
Damien Neil3b46ade2019-03-26 13:55:02 -0700528 {A: 2},
529 },
Damien Neilba23aa52018-12-07 14:38:17 -0800530 }, build(
531 &testpb.TestAllExtensions{},
532 extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
533 {A: scalar.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700534 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800535 {A: scalar.Int32(2)},
536 }),
537 )},
538 wire: pack.Message{
539 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
540 pack.Tag{1, pack.VarintType}, pack.Varint(1),
541 }),
Damien Neilc37adef2019-04-01 13:49:56 -0700542 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
Damien Neilba23aa52018-12-07 14:38:17 -0800543 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
544 pack.Tag{1, pack.VarintType}, pack.Varint(2),
545 }),
546 }.Marshal(),
547 },
548 {
549 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800550 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800551 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
552 {A: scalar.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700553 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800554 {A: scalar.Int32(2017)},
555 },
556 }, build(
557 &testpb.TestAllExtensions{},
558 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
559 {A: scalar.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700560 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800561 {A: scalar.Int32(2017)},
562 }),
563 )},
564 wire: pack.Message{
565 pack.Tag{46, pack.StartGroupType},
566 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
567 pack.Tag{46, pack.EndGroupType},
568 pack.Tag{46, pack.StartGroupType},
Damien Neilc37adef2019-04-01 13:49:56 -0700569 pack.Tag{46, pack.EndGroupType},
570 pack.Tag{46, pack.StartGroupType},
Damien Neilba23aa52018-12-07 14:38:17 -0800571 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
572 pack.Tag{46, pack.EndGroupType},
573 }.Marshal(),
574 },
575 {
576 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800577 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800578 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
579 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
580 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
581 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
582 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
583 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
584 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
585 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
586 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
587 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
588 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
589 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
590 MapBoolBool: map[bool]bool{true: false, false: true},
591 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
592 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
593 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
594 "71.1.key": {A: scalar.Int32(1171)},
595 "71.2.key": {A: scalar.Int32(2171)},
596 },
597 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
598 "73.1.key": testpb.TestAllTypes_FOO,
599 "73.2.key": testpb.TestAllTypes_BAR,
600 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700601 }, &test3pb.TestAllTypes{
602 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
603 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
604 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
605 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
606 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
607 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
608 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
609 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
610 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
611 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
612 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
613 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
614 MapBoolBool: map[bool]bool{true: false, false: true},
615 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
616 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
617 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
618 "71.1.key": {A: 1171},
619 "71.2.key": {A: 2171},
620 },
621 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
622 "73.1.key": test3pb.TestAllTypes_FOO,
623 "73.2.key": test3pb.TestAllTypes_BAR,
624 },
Damien Neilba23aa52018-12-07 14:38:17 -0800625 }},
626 wire: pack.Message{
627 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
628 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
629 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
630 }),
631 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
632 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
633 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
634 }),
635 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
636 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
637 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
638 }),
639 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
640 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
641 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
642 }),
643 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
644 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
645 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
646 }),
647 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
648 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
649 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
650 }),
651 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
652 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
653 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
654 }),
655 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
656 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
657 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
658 }),
659 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
660 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
661 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
662 }),
663 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
664 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
665 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
666 }),
667 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
668 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
669 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
670 }),
671 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
672 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
673 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
674 }),
675 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
676 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
677 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
678 }),
679 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
680 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
681 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
682 }),
683 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
684 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
685 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
686 }),
687 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
688 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
689 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
690 }),
691 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
692 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
693 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
694 }),
695 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
696 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
697 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
698 }),
699 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
700 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
701 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
702 }),
703 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
704 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
705 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
706 }),
707 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
708 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
709 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
710 }),
711 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
712 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
713 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
714 }),
715 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
716 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
717 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
718 }),
719 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
720 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
721 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
722 }),
723 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
724 pack.Tag{1, pack.VarintType}, pack.Bool(true),
725 pack.Tag{2, pack.VarintType}, pack.Bool(false),
726 }),
727 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
728 pack.Tag{1, pack.VarintType}, pack.Bool(false),
729 pack.Tag{2, pack.VarintType}, pack.Bool(true),
730 }),
731 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
732 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
733 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
734 }),
735 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
736 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
737 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
738 }),
739 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
740 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
741 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
742 }),
743 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
744 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
745 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
746 }),
747 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
748 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
749 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
750 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
751 }),
752 }),
753 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
754 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
755 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
756 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
757 }),
758 }),
759 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
760 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
761 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
762 }),
763 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
764 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
765 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
766 }),
767 }.Marshal(),
768 },
769 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700770 desc: "oneof (uint32)",
771 decodeTo: []proto.Message{
772 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
773 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
774 },
775 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800776 },
777 {
778 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700779 decodeTo: []proto.Message{
780 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
781 &testpb.TestAllTypes_NestedMessage{A: scalar.Int32(1112)},
782 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
783 &test3pb.TestAllTypes_NestedMessage{A: 1112},
784 }},
785 },
Damien Neilba23aa52018-12-07 14:38:17 -0800786 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
787 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
788 })}.Marshal(),
789 },
790 {
Damien Neilc37adef2019-04-01 13:49:56 -0700791 desc: "oneof (empty message)",
792 decodeTo: []proto.Message{
793 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
794 &testpb.TestAllTypes_NestedMessage{},
795 }},
796 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
797 &test3pb.TestAllTypes_NestedMessage{},
798 }},
799 },
800 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
801 },
802 {
Damien Neilba23aa52018-12-07 14:38:17 -0800803 desc: "oneof (overridden message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700804 decodeTo: []proto.Message{
805 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
806 &testpb.TestAllTypes_NestedMessage{
807 Corecursive: &testpb.TestAllTypes{
808 OptionalInt32: scalar.Int32(43),
809 },
Damien Neilba23aa52018-12-07 14:38:17 -0800810 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700811 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
812 &test3pb.TestAllTypes_NestedMessage{
813 Corecursive: &test3pb.TestAllTypes{
814 OptionalInt32: 43,
815 },
816 },
817 }}},
Damien Neilba23aa52018-12-07 14:38:17 -0800818 wire: pack.Message{
819 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
820 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
821 }),
822 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
823 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
824 pack.Tag{1, pack.VarintType}, pack.Varint(43),
825 }),
826 }),
827 }.Marshal(),
828 },
829 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700830 desc: "oneof (string)",
831 decodeTo: []proto.Message{
832 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
833 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
834 },
835 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800836 },
837 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700838 desc: "oneof (bytes)",
839 decodeTo: []proto.Message{
840 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
841 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
842 },
843 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800844 },
845 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700846 desc: "oneof (bool)",
847 decodeTo: []proto.Message{
848 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
849 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
850 },
851 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800852 },
853 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700854 desc: "oneof (uint64)",
855 decodeTo: []proto.Message{
856 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
857 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
858 },
859 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800860 },
861 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700862 desc: "oneof (float)",
863 decodeTo: []proto.Message{
864 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
865 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
866 },
867 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800868 },
869 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700870 desc: "oneof (double)",
871 decodeTo: []proto.Message{
872 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
873 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
874 },
875 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800876 },
877 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700878 desc: "oneof (enum)",
879 decodeTo: []proto.Message{
880 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
881 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
882 },
883 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800884 },
885 {
Damien Neilc37adef2019-04-01 13:49:56 -0700886 desc: "oneof (zero)",
887 decodeTo: []proto.Message{
888 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}},
889 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}},
890 },
891 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
892 },
893 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700894 desc: "oneof (overridden value)",
895 decodeTo: []proto.Message{
896 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
897 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
898 },
Damien Neilba23aa52018-12-07 14:38:17 -0800899 wire: pack.Message{
900 pack.Tag{111, pack.VarintType}, pack.Varint(1),
901 pack.Tag{116, pack.VarintType}, pack.Varint(2),
902 }.Marshal(),
903 },
904 // TODO: More unknown field tests for ordering, repeated fields, etc.
905 //
906 // It is currently impossible to produce results that the v1 Equal
907 // considers equivalent to those of the v1 decoder. Figure out if
908 // that's a problem or not.
909 {
910 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -0800911 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -0800912 &testpb.TestAllTypes{},
913 unknown(100000, pack.Message{
914 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
915 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -0700916 ), build(
917 &test3pb.TestAllTypes{},
918 unknown(100000, pack.Message{
919 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
920 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -0800921 )},
922 wire: pack.Message{
923 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
924 }.Marshal(),
925 },
926 {
927 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -0800928 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -0800929 &testpb.TestAllTypes{},
930 unknown(1, pack.Message{
931 pack.Tag{1, pack.BytesType}, pack.String("string"),
932 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -0700933 ), build(
934 &test3pb.TestAllTypes{},
935 unknown(1, pack.Message{
936 pack.Tag{1, pack.BytesType}, pack.String("string"),
937 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -0800938 )},
939 wire: pack.Message{
940 pack.Tag{1, pack.BytesType}, pack.String("string"),
941 }.Marshal(),
942 },
943 {
944 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -0800945 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -0800946 &testpb.TestAllTypes{
947 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -0700948 }, &test3pb.TestAllTypes{
949 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -0800950 },
951 },
952 wire: pack.Message{
953 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
954 pack.Tag{1, pack.VarintType}, pack.Varint(1),
955 pack.Tag{2, pack.BytesType}, pack.String("string"),
956 }),
957 }.Marshal(),
958 },
Damien Neil96c229a2019-04-03 12:17:24 -0700959 {
960 desc: "required field unset",
961 partial: true,
962 decodeTo: []proto.Message{&testpb.TestRequired{}},
963 },
964 {
965 desc: "required field set",
966 decodeTo: []proto.Message{&testpb.TestRequired{
967 RequiredField: scalar.Int32(1),
968 }},
969 wire: pack.Message{
970 pack.Tag{1, pack.VarintType}, pack.Varint(1),
971 }.Marshal(),
972 },
973 {
974 desc: "required field in optional message unset",
975 partial: true,
976 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
977 OptionalMessage: &testpb.TestRequired{},
978 }},
979 wire: pack.Message{
980 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
981 }.Marshal(),
982 },
983 {
984 desc: "required field in optional message set",
985 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
986 OptionalMessage: &testpb.TestRequired{
987 RequiredField: scalar.Int32(1),
988 },
989 }},
990 wire: pack.Message{
991 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
992 pack.Tag{1, pack.VarintType}, pack.Varint(1),
993 }),
994 }.Marshal(),
995 },
Damien Neil4686e232019-04-05 13:31:40 -0700996 {
997 desc: "required field in optional message set (split across multiple tags)",
998 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
999 OptionalMessage: &testpb.TestRequired{
1000 RequiredField: scalar.Int32(1),
1001 },
1002 }},
1003 wire: pack.Message{
1004 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1005 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1006 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1007 }),
1008 }.Marshal(),
1009 },
Damien Neil96c229a2019-04-03 12:17:24 -07001010 {
1011 desc: "required field in repeated message unset",
1012 partial: true,
1013 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1014 RepeatedMessage: []*testpb.TestRequired{
1015 {RequiredField: scalar.Int32(1)},
1016 {},
1017 },
1018 }},
1019 wire: pack.Message{
1020 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1021 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1022 }),
1023 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1024 }.Marshal(),
1025 },
1026 {
1027 desc: "required field in repeated message set",
1028 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1029 RepeatedMessage: []*testpb.TestRequired{
1030 {RequiredField: scalar.Int32(1)},
1031 {RequiredField: scalar.Int32(2)},
1032 },
1033 }},
1034 wire: pack.Message{
1035 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1036 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1037 }),
1038 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1039 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1040 }),
1041 }.Marshal(),
1042 },
1043 {
1044 desc: "required field in map message unset",
1045 partial: true,
1046 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1047 MapMessage: map[int32]*testpb.TestRequired{
1048 1: {RequiredField: scalar.Int32(1)},
1049 2: {},
1050 },
1051 }},
1052 wire: pack.Message{
1053 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1054 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1055 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1056 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1057 }),
1058 }),
1059 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1060 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1061 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1062 }),
1063 }.Marshal(),
1064 },
1065 {
1066 desc: "required field in map message set",
1067 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1068 MapMessage: map[int32]*testpb.TestRequired{
1069 1: {RequiredField: scalar.Int32(1)},
1070 2: {RequiredField: scalar.Int32(2)},
1071 },
1072 }},
1073 wire: pack.Message{
1074 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1075 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1076 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1077 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1078 }),
1079 }),
1080 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1081 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1082 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1083 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1084 }),
1085 }),
1086 }.Marshal(),
1087 },
1088 {
1089 desc: "required field in optional group unset",
1090 partial: true,
1091 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1092 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1093 }},
1094 wire: pack.Message{
1095 pack.Tag{1, pack.StartGroupType},
1096 pack.Tag{1, pack.EndGroupType},
1097 }.Marshal(),
1098 },
1099 {
1100 desc: "required field in optional group set",
1101 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1102 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
1103 A: scalar.Int32(1),
1104 },
1105 }},
1106 wire: pack.Message{
1107 pack.Tag{1, pack.StartGroupType},
1108 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1109 pack.Tag{1, pack.EndGroupType},
1110 }.Marshal(),
1111 },
1112 {
1113 desc: "required field in repeated group unset",
1114 partial: true,
1115 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1116 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1117 {A: scalar.Int32(1)},
1118 {},
1119 },
1120 }},
1121 wire: pack.Message{
1122 pack.Tag{3, pack.StartGroupType},
1123 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1124 pack.Tag{3, pack.EndGroupType},
1125 pack.Tag{3, pack.StartGroupType},
1126 pack.Tag{3, pack.EndGroupType},
1127 }.Marshal(),
1128 },
1129 {
1130 desc: "required field in repeated group set",
1131 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1132 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1133 {A: scalar.Int32(1)},
1134 {A: scalar.Int32(2)},
1135 },
1136 }},
1137 wire: pack.Message{
1138 pack.Tag{3, pack.StartGroupType},
1139 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1140 pack.Tag{3, pack.EndGroupType},
1141 pack.Tag{3, pack.StartGroupType},
1142 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1143 pack.Tag{3, pack.EndGroupType},
1144 }.Marshal(),
1145 },
1146 {
1147 desc: "required field in extension message unset",
1148 partial: true,
1149 invalidExtensions: true,
1150 decodeTo: []proto.Message{build(
1151 &testpb.TestAllExtensions{},
1152 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1153 )},
1154 wire: pack.Message{
1155 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1156 }.Marshal(),
1157 },
1158 {
1159 desc: "required field in extension message set",
1160 decodeTo: []proto.Message{build(
1161 &testpb.TestAllExtensions{},
1162 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
1163 RequiredField: scalar.Int32(1),
1164 }),
1165 )},
1166 wire: pack.Message{
1167 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1168 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1169 }),
1170 }.Marshal(),
1171 },
1172 {
1173 desc: "required field in repeated extension message unset",
1174 partial: true,
1175 invalidExtensions: true,
1176 decodeTo: []proto.Message{build(
1177 &testpb.TestAllExtensions{},
1178 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1179 {RequiredField: scalar.Int32(1)},
1180 {},
1181 }),
1182 )},
1183 wire: pack.Message{
1184 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1185 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1186 }),
1187 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1188 }.Marshal(),
1189 },
1190 {
1191 desc: "required field in repeated extension message set",
1192 decodeTo: []proto.Message{build(
1193 &testpb.TestAllExtensions{},
1194 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1195 {RequiredField: scalar.Int32(1)},
1196 {RequiredField: scalar.Int32(2)},
1197 }),
1198 )},
1199 wire: pack.Message{
1200 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1201 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1202 }),
1203 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1204 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1205 }),
1206 }.Marshal(),
1207 },
Damien Neilc37adef2019-04-01 13:49:56 -07001208 {
1209 desc: "legacy",
1210 partial: true,
1211 decodeTo: []proto.Message{
1212 &legacypb.Legacy{
1213 F1: &legacy1pb.Message{
1214 OptionalInt32: scalar.Int32(1),
1215 OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
1216 OptionalChildMessage: &legacy1pb.Message_ChildMessage{
1217 F1: scalar.String("x"),
1218 },
1219 Optionalgroup: &legacy1pb.Message_OptionalGroup{
1220 F1: scalar.String("x"),
1221 },
1222 RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
1223 {F1: scalar.String("x")},
1224 },
1225 Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
1226 {F1: scalar.String("x")},
1227 },
1228 MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
1229 true: {F1: scalar.String("x")},
1230 },
1231 OneofUnion: &legacy1pb.Message_OneofChildMessage{
1232 &legacy1pb.Message_ChildMessage{
1233 F1: scalar.String("x"),
1234 },
1235 },
1236 },
1237 },
1238 },
1239 wire: pack.Message{
1240 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1241 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1242 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1243 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1244 pack.Tag{1, pack.BytesType}, pack.String("x"),
1245 }),
1246 pack.Tag{120, pack.StartGroupType},
1247 pack.Tag{1, pack.BytesType}, pack.String("x"),
1248 pack.Tag{120, pack.EndGroupType},
1249 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1250 pack.Tag{1, pack.BytesType}, pack.String("x"),
1251 }),
1252 pack.Tag{520, pack.StartGroupType},
1253 pack.Tag{1, pack.BytesType}, pack.String("x"),
1254 pack.Tag{520, pack.EndGroupType},
1255 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1256 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1257 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1258 pack.Tag{1, pack.BytesType}, pack.String("x"),
1259 }),
1260 }),
1261 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1262 pack.Tag{1, pack.BytesType}, pack.String("x"),
1263 }),
1264 }),
1265 }.Marshal(),
1266 },
Damien Neilba23aa52018-12-07 14:38:17 -08001267}
1268
Damien Neilbc310b52019-04-11 11:46:55 -07001269var invalidUTF8TestProtos = []testProto{
1270 {
1271 desc: "invalid UTF-8 in optional string field",
1272 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1273 OptionalString: "abc\xff",
1274 }},
1275 wire: pack.Message{
1276 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1277 }.Marshal(),
1278 },
1279 {
1280 desc: "invalid UTF-8 in repeated string field",
1281 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1282 RepeatedString: []string{"foo", "abc\xff"},
1283 }},
1284 wire: pack.Message{
1285 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1286 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1287 }.Marshal(),
1288 },
1289 {
1290 desc: "invalid UTF-8 in nested message",
1291 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1292 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1293 Corecursive: &test3pb.TestAllTypes{
1294 OptionalString: "abc\xff",
1295 },
1296 },
1297 }},
1298 wire: pack.Message{
1299 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1300 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1301 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1302 }),
1303 }),
1304 }.Marshal(),
1305 },
1306 {
Damien Neilc37adef2019-04-01 13:49:56 -07001307 desc: "invalid UTF-8 in oneof field",
1308 decodeTo: []proto.Message{
1309 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
1310 },
1311 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1312 },
1313 {
Damien Neilbc310b52019-04-11 11:46:55 -07001314 desc: "invalid UTF-8 in map key",
1315 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1316 MapStringString: map[string]string{"key\xff": "val"},
1317 }},
1318 wire: pack.Message{
1319 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1320 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1321 pack.Tag{2, pack.BytesType}, pack.String("val"),
1322 }),
1323 }.Marshal(),
1324 },
1325 {
1326 desc: "invalid UTF-8 in map value",
1327 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1328 MapStringString: map[string]string{"key": "val\xff"},
1329 }},
1330 wire: pack.Message{
1331 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1332 pack.Tag{1, pack.BytesType}, pack.String("key"),
1333 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1334 }),
1335 }.Marshal(),
1336 },
1337}
1338
Damien Neil4be2fb42018-12-17 11:16:16 -08001339func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001340 for _, opt := range opts {
1341 opt(m)
1342 }
1343 return m
1344}
1345
Damien Neil4be2fb42018-12-17 11:16:16 -08001346type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001347
1348func unknown(num pref.FieldNumber, raw pref.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001349 return func(m proto.Message) {
Damien Neilba23aa52018-12-07 14:38:17 -08001350 m.ProtoReflect().UnknownFields().Set(num, raw)
1351 }
1352}
1353
Damien Neile6f060f2019-04-23 17:11:02 -07001354func registerExtension(desc *protoV1.ExtensionDesc) buildOpt {
1355 return func(m proto.Message) {
1356 et := protolegacy.X.ExtensionTypeFromDesc(desc)
1357 m.ProtoReflect().KnownFields().ExtensionTypes().Register(et)
1358 }
1359}
1360
Damien Neilba23aa52018-12-07 14:38:17 -08001361func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001362 return func(m proto.Message) {
Damien Neilba23aa52018-12-07 14:38:17 -08001363 if err := protoV1.SetExtension(m.(protoV1.Message), desc, value); err != nil {
1364 panic(err)
1365 }
1366 }
1367}
Damien Neil61e93c72019-03-27 09:23:20 -07001368
1369func marshalText(m proto.Message) string {
Damien Neil5c5b5312019-05-14 12:44:37 -07001370 b, _ := prototext.Marshal(m)
Damien Neil61e93c72019-03-27 09:23:20 -07001371 return string(b)
1372}
Damien Neilbc310b52019-04-11 11:46:55 -07001373
1374func isErrInvalidUTF8(err error) bool {
1375 nerr, ok := err.(errors.NonFatalErrors)
1376 if !ok || len(nerr) == 0 {
1377 return false
1378 }
1379 for _, err := range nerr {
1380 if e, ok := err.(interface{ InvalidUTF8() bool }); ok && e.InvalidUTF8() {
1381 continue
1382 }
1383 return false
1384 }
1385 return true
1386}