blob: 9560ab89bc0a9479ffee52f2a7ffae49a356d6b4 [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
5package proto
6
7import (
8 "fmt"
9 "reflect"
10 "testing"
11
12 protoV1 "github.com/golang/protobuf/proto"
13 "github.com/golang/protobuf/v2/internal/encoding/pack"
14 _ "github.com/golang/protobuf/v2/internal/legacy"
15 "github.com/golang/protobuf/v2/internal/scalar"
16 testpb "github.com/golang/protobuf/v2/internal/testprotos/test"
17 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
18)
19
20type testProto struct {
21 desc string
22 decodeTo []Message
23 wire []byte
24}
25
26func TestDecode(t *testing.T) {
27 for _, test := range testProtos {
28 for _, want := range test.decodeTo {
29 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
30 wire := append(([]byte)(nil), test.wire...)
31 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(Message)
32 if err := Unmarshal(wire, got); err != nil {
33 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, protoV1.MarshalTextString(want.(protoV1.Message)))
34 return
35 }
36
37 // Aliasing check: Modifying the original wire bytes shouldn't
38 // affect the unmarshaled message.
39 for i := range wire {
40 wire[i] = 0
41 }
42
43 if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) {
44 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", protoV1.MarshalTextString(got.(protoV1.Message)), protoV1.MarshalTextString(want.(protoV1.Message)))
45 }
46 })
47 }
48 }
49}
50
51var testProtos = []testProto{
52 {
53 desc: "basic scalar types",
54 decodeTo: []Message{&testpb.TestAllTypes{
55 OptionalInt32: scalar.Int32(1001),
56 OptionalInt64: scalar.Int64(1002),
57 OptionalUint32: scalar.Uint32(1003),
58 OptionalUint64: scalar.Uint64(1004),
59 OptionalSint32: scalar.Int32(1005),
60 OptionalSint64: scalar.Int64(1006),
61 OptionalFixed32: scalar.Uint32(1007),
62 OptionalFixed64: scalar.Uint64(1008),
63 OptionalSfixed32: scalar.Int32(1009),
64 OptionalSfixed64: scalar.Int64(1010),
65 OptionalFloat: scalar.Float32(1011.5),
66 OptionalDouble: scalar.Float64(1012.5),
67 OptionalBool: scalar.Bool(true),
68 OptionalString: scalar.String("string"),
69 OptionalBytes: []byte("bytes"),
70 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
71 }, build(
72 &testpb.TestAllExtensions{},
73 extend(testpb.E_OptionalInt32Extension, scalar.Int32(1001)),
74 extend(testpb.E_OptionalInt64Extension, scalar.Int64(1002)),
75 extend(testpb.E_OptionalUint32Extension, scalar.Uint32(1003)),
76 extend(testpb.E_OptionalUint64Extension, scalar.Uint64(1004)),
77 extend(testpb.E_OptionalSint32Extension, scalar.Int32(1005)),
78 extend(testpb.E_OptionalSint64Extension, scalar.Int64(1006)),
79 extend(testpb.E_OptionalFixed32Extension, scalar.Uint32(1007)),
80 extend(testpb.E_OptionalFixed64Extension, scalar.Uint64(1008)),
81 extend(testpb.E_OptionalSfixed32Extension, scalar.Int32(1009)),
82 extend(testpb.E_OptionalSfixed64Extension, scalar.Int64(1010)),
83 extend(testpb.E_OptionalFloatExtension, scalar.Float32(1011.5)),
84 extend(testpb.E_OptionalDoubleExtension, scalar.Float64(1012.5)),
85 extend(testpb.E_OptionalBoolExtension, scalar.Bool(true)),
86 extend(testpb.E_OptionalStringExtension, scalar.String("string")),
87 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
88 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR.Enum()),
89 )},
90 wire: pack.Message{
91 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
92 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
93 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
94 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
95 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
96 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
97 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
98 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
99 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
100 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
101 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
102 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
103 pack.Tag{13, pack.VarintType}, pack.Bool(true),
104 pack.Tag{14, pack.BytesType}, pack.String("string"),
105 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
106 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
107 }.Marshal(),
108 },
109 {
110 desc: "groups",
111 decodeTo: []Message{&testpb.TestAllTypes{
112 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
113 A: scalar.Int32(1017),
114 },
115 }, build(
116 &testpb.TestAllExtensions{},
117 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
118 A: scalar.Int32(1017),
119 }),
120 )},
121 wire: pack.Message{
122 pack.Tag{16, pack.StartGroupType},
123 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
124 pack.Tag{16, pack.EndGroupType},
125 }.Marshal(),
126 },
127 {
128 desc: "groups (field overridden)",
129 decodeTo: []Message{&testpb.TestAllTypes{
130 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
131 A: scalar.Int32(2),
132 },
133 }, build(
134 &testpb.TestAllExtensions{},
135 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
136 A: scalar.Int32(2),
137 }),
138 )},
139 wire: pack.Message{
140 pack.Tag{16, pack.StartGroupType},
141 pack.Tag{17, pack.VarintType}, pack.Varint(1),
142 pack.Tag{16, pack.EndGroupType},
143 pack.Tag{16, pack.StartGroupType},
144 pack.Tag{17, pack.VarintType}, pack.Varint(2),
145 pack.Tag{16, pack.EndGroupType},
146 }.Marshal(),
147 },
148 {
149 desc: "messages",
150 decodeTo: []Message{&testpb.TestAllTypes{
151 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
152 A: scalar.Int32(42),
153 Corecursive: &testpb.TestAllTypes{
154 OptionalInt32: scalar.Int32(43),
155 },
156 },
157 }, build(
158 &testpb.TestAllExtensions{},
159 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
160 A: scalar.Int32(42),
161 Corecursive: &testpb.TestAllTypes{
162 OptionalInt32: scalar.Int32(43),
163 },
164 }),
165 )},
166 wire: pack.Message{
167 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
168 pack.Tag{1, pack.VarintType}, pack.Varint(42),
169 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
170 pack.Tag{1, pack.VarintType}, pack.Varint(43),
171 }),
172 }),
173 }.Marshal(),
174 },
175 {
176 desc: "messages (split across multiple tags)",
177 decodeTo: []Message{&testpb.TestAllTypes{
178 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
179 A: scalar.Int32(42),
180 Corecursive: &testpb.TestAllTypes{
181 OptionalInt32: scalar.Int32(43),
182 },
183 },
184 }, build(
185 &testpb.TestAllExtensions{},
186 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
187 A: scalar.Int32(42),
188 Corecursive: &testpb.TestAllTypes{
189 OptionalInt32: scalar.Int32(43),
190 },
191 }),
192 )},
193 wire: pack.Message{
194 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
195 pack.Tag{1, pack.VarintType}, pack.Varint(42),
196 }),
197 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
198 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
199 pack.Tag{1, pack.VarintType}, pack.Varint(43),
200 }),
201 }),
202 }.Marshal(),
203 },
204 {
205 desc: "messages (field overridden)",
206 decodeTo: []Message{&testpb.TestAllTypes{
207 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
208 A: scalar.Int32(2),
209 },
210 }, build(
211 &testpb.TestAllExtensions{},
212 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
213 A: scalar.Int32(2),
214 }),
215 )},
216 wire: pack.Message{
217 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
218 pack.Tag{1, pack.VarintType}, pack.Varint(1),
219 }),
220 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
221 pack.Tag{1, pack.VarintType}, pack.Varint(2),
222 }),
223 }.Marshal(),
224 },
225 {
226 desc: "basic repeated types",
227 decodeTo: []Message{&testpb.TestAllTypes{
228 RepeatedInt32: []int32{1001, 2001},
229 RepeatedInt64: []int64{1002, 2002},
230 RepeatedUint32: []uint32{1003, 2003},
231 RepeatedUint64: []uint64{1004, 2004},
232 RepeatedSint32: []int32{1005, 2005},
233 RepeatedSint64: []int64{1006, 2006},
234 RepeatedFixed32: []uint32{1007, 2007},
235 RepeatedFixed64: []uint64{1008, 2008},
236 RepeatedSfixed32: []int32{1009, 2009},
237 RepeatedSfixed64: []int64{1010, 2010},
238 RepeatedFloat: []float32{1011.5, 2011.5},
239 RepeatedDouble: []float64{1012.5, 2012.5},
240 RepeatedBool: []bool{true, false},
241 RepeatedString: []string{"foo", "bar"},
242 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
243 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
244 testpb.TestAllTypes_FOO,
245 testpb.TestAllTypes_BAR,
246 },
247 }, build(
248 &testpb.TestAllExtensions{},
249 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
250 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
251 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
252 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
253 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
254 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
255 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
256 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
257 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
258 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
259 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
260 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
261 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
262 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
263 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
264 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
265 testpb.TestAllTypes_FOO,
266 testpb.TestAllTypes_BAR,
267 }),
268 )},
269 wire: pack.Message{
270 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
271 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
272 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
273 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
274 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
275 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
276 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
277 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
278 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
279 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
280 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
281 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
282 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
283 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
284 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
285 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
286 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
287 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
288 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
289 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
290 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
291 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
292 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
293 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
294 pack.Tag{43, pack.VarintType}, pack.Bool(true),
295 pack.Tag{43, pack.VarintType}, pack.Bool(false),
296 pack.Tag{44, pack.BytesType}, pack.String("foo"),
297 pack.Tag{44, pack.BytesType}, pack.String("bar"),
298 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
299 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
300 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
301 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
302 }.Marshal(),
303 },
304 {
305 desc: "basic repeated types (packed encoding)",
306 decodeTo: []Message{&testpb.TestAllTypes{
307 RepeatedInt32: []int32{1001, 2001},
308 RepeatedInt64: []int64{1002, 2002},
309 RepeatedUint32: []uint32{1003, 2003},
310 RepeatedUint64: []uint64{1004, 2004},
311 RepeatedSint32: []int32{1005, 2005},
312 RepeatedSint64: []int64{1006, 2006},
313 RepeatedFixed32: []uint32{1007, 2007},
314 RepeatedFixed64: []uint64{1008, 2008},
315 RepeatedSfixed32: []int32{1009, 2009},
316 RepeatedSfixed64: []int64{1010, 2010},
317 RepeatedFloat: []float32{1011.5, 2011.5},
318 RepeatedDouble: []float64{1012.5, 2012.5},
319 RepeatedBool: []bool{true, false},
320 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
321 testpb.TestAllTypes_FOO,
322 testpb.TestAllTypes_BAR,
323 },
324 }, build(
325 &testpb.TestAllExtensions{},
326 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
327 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
328 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
329 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
330 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
331 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
332 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
333 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
334 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
335 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
336 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
337 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
338 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
339 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
340 testpb.TestAllTypes_FOO,
341 testpb.TestAllTypes_BAR,
342 }),
343 )},
344 wire: pack.Message{
345 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
346 pack.Varint(1001), pack.Varint(2001),
347 },
348 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
349 pack.Varint(1002), pack.Varint(2002),
350 },
351 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
352 pack.Uvarint(1003), pack.Uvarint(2003),
353 },
354 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
355 pack.Uvarint(1004), pack.Uvarint(2004),
356 },
357 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
358 pack.Svarint(1005), pack.Svarint(2005),
359 },
360 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
361 pack.Svarint(1006), pack.Svarint(2006),
362 },
363 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
364 pack.Uint32(1007), pack.Uint32(2007),
365 },
366 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
367 pack.Uint64(1008), pack.Uint64(2008),
368 },
369 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
370 pack.Int32(1009), pack.Int32(2009),
371 },
372 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
373 pack.Int64(1010), pack.Int64(2010),
374 },
375 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
376 pack.Float32(1011.5), pack.Float32(2011.5),
377 },
378 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
379 pack.Float64(1012.5), pack.Float64(2012.5),
380 },
381 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
382 pack.Bool(true), pack.Bool(false),
383 },
384 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
385 pack.Varint(int(testpb.TestAllTypes_FOO)),
386 pack.Varint(int(testpb.TestAllTypes_BAR)),
387 },
388 }.Marshal(),
389 },
390 {
391 desc: "repeated messages",
392 decodeTo: []Message{&testpb.TestAllTypes{
393 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
394 {A: scalar.Int32(1)},
395 {A: scalar.Int32(2)},
396 },
397 }, build(
398 &testpb.TestAllExtensions{},
399 extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
400 {A: scalar.Int32(1)},
401 {A: scalar.Int32(2)},
402 }),
403 )},
404 wire: pack.Message{
405 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
406 pack.Tag{1, pack.VarintType}, pack.Varint(1),
407 }),
408 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
409 pack.Tag{1, pack.VarintType}, pack.Varint(2),
410 }),
411 }.Marshal(),
412 },
413 {
414 desc: "repeated groups",
415 decodeTo: []Message{&testpb.TestAllTypes{
416 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
417 {A: scalar.Int32(1017)},
418 {A: scalar.Int32(2017)},
419 },
420 }, build(
421 &testpb.TestAllExtensions{},
422 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
423 {A: scalar.Int32(1017)},
424 {A: scalar.Int32(2017)},
425 }),
426 )},
427 wire: pack.Message{
428 pack.Tag{46, pack.StartGroupType},
429 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
430 pack.Tag{46, pack.EndGroupType},
431 pack.Tag{46, pack.StartGroupType},
432 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
433 pack.Tag{46, pack.EndGroupType},
434 }.Marshal(),
435 },
436 {
437 desc: "maps",
438 decodeTo: []Message{&testpb.TestAllTypes{
439 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
440 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
441 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
442 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
443 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
444 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
445 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
446 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
447 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
448 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
449 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
450 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
451 MapBoolBool: map[bool]bool{true: false, false: true},
452 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
453 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
454 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
455 "71.1.key": {A: scalar.Int32(1171)},
456 "71.2.key": {A: scalar.Int32(2171)},
457 },
458 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
459 "73.1.key": testpb.TestAllTypes_FOO,
460 "73.2.key": testpb.TestAllTypes_BAR,
461 },
462 }},
463 wire: pack.Message{
464 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
465 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
466 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
467 }),
468 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
469 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
470 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
471 }),
472 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
473 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
474 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
475 }),
476 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
477 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
478 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
479 }),
480 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
481 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
482 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
483 }),
484 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
485 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
486 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
487 }),
488 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
489 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
490 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
491 }),
492 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
493 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
494 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
495 }),
496 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
497 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
498 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
499 }),
500 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
501 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
502 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
503 }),
504 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
505 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
506 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
507 }),
508 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
509 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
510 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
511 }),
512 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
513 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
514 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
515 }),
516 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
517 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
518 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
519 }),
520 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
521 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
522 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
523 }),
524 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
525 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
526 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
527 }),
528 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
529 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
530 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
531 }),
532 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
533 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
534 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
535 }),
536 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
537 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
538 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
539 }),
540 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
541 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
542 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
543 }),
544 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
545 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
546 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
547 }),
548 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
549 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
550 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
551 }),
552 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
553 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
554 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
555 }),
556 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
557 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
558 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
559 }),
560 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
561 pack.Tag{1, pack.VarintType}, pack.Bool(true),
562 pack.Tag{2, pack.VarintType}, pack.Bool(false),
563 }),
564 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
565 pack.Tag{1, pack.VarintType}, pack.Bool(false),
566 pack.Tag{2, pack.VarintType}, pack.Bool(true),
567 }),
568 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
569 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
570 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
571 }),
572 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
573 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
574 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
575 }),
576 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
577 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
578 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
579 }),
580 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
581 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
582 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
583 }),
584 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
585 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
586 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
587 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
588 }),
589 }),
590 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
591 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
592 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
593 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
594 }),
595 }),
596 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
597 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
598 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
599 }),
600 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
601 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
602 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
603 }),
604 }.Marshal(),
605 },
606 {
607 desc: "oneof (uint32)",
608 decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}}},
609 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
610 },
611 {
612 desc: "oneof (message)",
613 decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
614 &testpb.TestAllTypes_NestedMessage{A: scalar.Int32(1112)},
615 }}},
616 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
617 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
618 })}.Marshal(),
619 },
620 {
621 desc: "oneof (overridden message)",
622 decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
623 &testpb.TestAllTypes_NestedMessage{
624 Corecursive: &testpb.TestAllTypes{
625 OptionalInt32: scalar.Int32(43),
626 },
627 },
628 }}},
629 wire: pack.Message{
630 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
631 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
632 }),
633 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
634 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
635 pack.Tag{1, pack.VarintType}, pack.Varint(43),
636 }),
637 }),
638 }.Marshal(),
639 },
640 {
641 desc: "oneof (string)",
642 decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}}},
643 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
644 },
645 {
646 desc: "oneof (bytes)",
647 decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}}},
648 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
649 },
650 {
651 desc: "oneof (bool)",
652 decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}}},
653 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
654 },
655 {
656 desc: "oneof (uint64)",
657 decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}}},
658 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
659 },
660 {
661 desc: "oneof (float)",
662 decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}}},
663 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
664 },
665 {
666 desc: "oneof (double)",
667 decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}}},
668 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
669 },
670 {
671 desc: "oneof (enum)",
672 decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}}},
673 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
674 },
675 {
676 desc: "oneof (overridden value)",
677 decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}}},
678 wire: pack.Message{
679 pack.Tag{111, pack.VarintType}, pack.Varint(1),
680 pack.Tag{116, pack.VarintType}, pack.Varint(2),
681 }.Marshal(),
682 },
683 // TODO: More unknown field tests for ordering, repeated fields, etc.
684 //
685 // It is currently impossible to produce results that the v1 Equal
686 // considers equivalent to those of the v1 decoder. Figure out if
687 // that's a problem or not.
688 {
689 desc: "unknown fields",
690 decodeTo: []Message{build(
691 &testpb.TestAllTypes{},
692 unknown(100000, pack.Message{
693 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
694 }.Marshal()),
695 )},
696 wire: pack.Message{
697 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
698 }.Marshal(),
699 },
700 {
701 desc: "field type mismatch",
702 decodeTo: []Message{build(
703 &testpb.TestAllTypes{},
704 unknown(1, pack.Message{
705 pack.Tag{1, pack.BytesType}, pack.String("string"),
706 }.Marshal()),
707 )},
708 wire: pack.Message{
709 pack.Tag{1, pack.BytesType}, pack.String("string"),
710 }.Marshal(),
711 },
712 {
713 desc: "map field element mismatch",
714 decodeTo: []Message{
715 &testpb.TestAllTypes{
716 MapInt32Int32: map[int32]int32{1: 0},
717 },
718 },
719 wire: pack.Message{
720 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
721 pack.Tag{1, pack.VarintType}, pack.Varint(1),
722 pack.Tag{2, pack.BytesType}, pack.String("string"),
723 }),
724 }.Marshal(),
725 },
726}
727
728func build(m Message, opts ...buildOpt) Message {
729 for _, opt := range opts {
730 opt(m)
731 }
732 return m
733}
734
735type buildOpt func(Message)
736
737func unknown(num pref.FieldNumber, raw pref.RawFields) buildOpt {
738 return func(m Message) {
739 m.ProtoReflect().UnknownFields().Set(num, raw)
740 }
741}
742
743func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt {
744 return func(m Message) {
745 if err := protoV1.SetExtension(m.(protoV1.Message), desc, value); err != nil {
746 panic(err)
747 }
748 }
749}