blob: 2e722f876beb9a8a1fee50aef01844fcc9931bbe [file] [log] [blame]
Damien Neild0b07492019-12-16 12:59:13 -08001// 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
5package proto_test
6
7import (
8 "google.golang.org/protobuf/internal/encoding/pack"
9 "google.golang.org/protobuf/proto"
10
11 legacypb "google.golang.org/protobuf/internal/testprotos/legacy"
12 legacy1pb "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v0.0.0-20160225-2fc053c5"
13 testpb "google.golang.org/protobuf/internal/testprotos/test"
14 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
15)
16
17type testProto struct {
18 desc string
19 decodeTo []proto.Message
20 wire []byte
21 partial bool
22 noEncode bool
23}
24
25var testValidMessages = []testProto{
26 {
27 desc: "basic scalar types",
28 decodeTo: []proto.Message{&testpb.TestAllTypes{
29 OptionalInt32: proto.Int32(1001),
30 OptionalInt64: proto.Int64(1002),
31 OptionalUint32: proto.Uint32(1003),
32 OptionalUint64: proto.Uint64(1004),
33 OptionalSint32: proto.Int32(1005),
34 OptionalSint64: proto.Int64(1006),
35 OptionalFixed32: proto.Uint32(1007),
36 OptionalFixed64: proto.Uint64(1008),
37 OptionalSfixed32: proto.Int32(1009),
38 OptionalSfixed64: proto.Int64(1010),
39 OptionalFloat: proto.Float32(1011.5),
40 OptionalDouble: proto.Float64(1012.5),
41 OptionalBool: proto.Bool(true),
42 OptionalString: proto.String("string"),
43 OptionalBytes: []byte("bytes"),
44 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
45 }, &test3pb.TestAllTypes{
46 OptionalInt32: 1001,
47 OptionalInt64: 1002,
48 OptionalUint32: 1003,
49 OptionalUint64: 1004,
50 OptionalSint32: 1005,
51 OptionalSint64: 1006,
52 OptionalFixed32: 1007,
53 OptionalFixed64: 1008,
54 OptionalSfixed32: 1009,
55 OptionalSfixed64: 1010,
56 OptionalFloat: 1011.5,
57 OptionalDouble: 1012.5,
58 OptionalBool: true,
59 OptionalString: "string",
60 OptionalBytes: []byte("bytes"),
61 OptionalNestedEnum: test3pb.TestAllTypes_BAR,
62 }, build(
63 &testpb.TestAllExtensions{},
64 extend(testpb.E_OptionalInt32Extension, int32(1001)),
65 extend(testpb.E_OptionalInt64Extension, int64(1002)),
66 extend(testpb.E_OptionalUint32Extension, uint32(1003)),
67 extend(testpb.E_OptionalUint64Extension, uint64(1004)),
68 extend(testpb.E_OptionalSint32Extension, int32(1005)),
69 extend(testpb.E_OptionalSint64Extension, int64(1006)),
70 extend(testpb.E_OptionalFixed32Extension, uint32(1007)),
71 extend(testpb.E_OptionalFixed64Extension, uint64(1008)),
72 extend(testpb.E_OptionalSfixed32Extension, int32(1009)),
73 extend(testpb.E_OptionalSfixed64Extension, int64(1010)),
74 extend(testpb.E_OptionalFloatExtension, float32(1011.5)),
75 extend(testpb.E_OptionalDoubleExtension, float64(1012.5)),
76 extend(testpb.E_OptionalBoolExtension, bool(true)),
77 extend(testpb.E_OptionalStringExtension, string("string")),
78 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
79 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR),
80 )},
81 wire: pack.Message{
82 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
83 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
84 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
85 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
86 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
87 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
88 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
89 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
90 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
91 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
92 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
93 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
94 pack.Tag{13, pack.VarintType}, pack.Bool(true),
95 pack.Tag{14, pack.BytesType}, pack.String("string"),
96 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
97 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
98 }.Marshal(),
99 },
100 {
101 desc: "zero values",
102 decodeTo: []proto.Message{&testpb.TestAllTypes{
103 OptionalInt32: proto.Int32(0),
104 OptionalInt64: proto.Int64(0),
105 OptionalUint32: proto.Uint32(0),
106 OptionalUint64: proto.Uint64(0),
107 OptionalSint32: proto.Int32(0),
108 OptionalSint64: proto.Int64(0),
109 OptionalFixed32: proto.Uint32(0),
110 OptionalFixed64: proto.Uint64(0),
111 OptionalSfixed32: proto.Int32(0),
112 OptionalSfixed64: proto.Int64(0),
113 OptionalFloat: proto.Float32(0),
114 OptionalDouble: proto.Float64(0),
115 OptionalBool: proto.Bool(false),
116 OptionalString: proto.String(""),
117 OptionalBytes: []byte{},
118 }, &test3pb.TestAllTypes{}, build(
119 &testpb.TestAllExtensions{},
120 extend(testpb.E_OptionalInt32Extension, int32(0)),
121 extend(testpb.E_OptionalInt64Extension, int64(0)),
122 extend(testpb.E_OptionalUint32Extension, uint32(0)),
123 extend(testpb.E_OptionalUint64Extension, uint64(0)),
124 extend(testpb.E_OptionalSint32Extension, int32(0)),
125 extend(testpb.E_OptionalSint64Extension, int64(0)),
126 extend(testpb.E_OptionalFixed32Extension, uint32(0)),
127 extend(testpb.E_OptionalFixed64Extension, uint64(0)),
128 extend(testpb.E_OptionalSfixed32Extension, int32(0)),
129 extend(testpb.E_OptionalSfixed64Extension, int64(0)),
130 extend(testpb.E_OptionalFloatExtension, float32(0)),
131 extend(testpb.E_OptionalDoubleExtension, float64(0)),
132 extend(testpb.E_OptionalBoolExtension, bool(false)),
133 extend(testpb.E_OptionalStringExtension, string("")),
134 extend(testpb.E_OptionalBytesExtension, []byte{}),
135 )},
136 wire: pack.Message{
137 pack.Tag{1, pack.VarintType}, pack.Varint(0),
138 pack.Tag{2, pack.VarintType}, pack.Varint(0),
139 pack.Tag{3, pack.VarintType}, pack.Uvarint(0),
140 pack.Tag{4, pack.VarintType}, pack.Uvarint(0),
141 pack.Tag{5, pack.VarintType}, pack.Svarint(0),
142 pack.Tag{6, pack.VarintType}, pack.Svarint(0),
143 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(0),
144 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(0),
145 pack.Tag{9, pack.Fixed32Type}, pack.Int32(0),
146 pack.Tag{10, pack.Fixed64Type}, pack.Int64(0),
147 pack.Tag{11, pack.Fixed32Type}, pack.Float32(0),
148 pack.Tag{12, pack.Fixed64Type}, pack.Float64(0),
149 pack.Tag{13, pack.VarintType}, pack.Bool(false),
150 pack.Tag{14, pack.BytesType}, pack.String(""),
151 pack.Tag{15, pack.BytesType}, pack.Bytes(nil),
152 }.Marshal(),
153 },
154 {
155 desc: "groups",
156 decodeTo: []proto.Message{&testpb.TestAllTypes{
157 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
158 A: proto.Int32(1017),
159 },
160 }, build(
161 &testpb.TestAllExtensions{},
162 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
163 A: proto.Int32(1017),
164 }),
165 )},
166 wire: pack.Message{
167 pack.Tag{16, pack.StartGroupType},
168 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
169 pack.Tag{16, pack.EndGroupType},
170 }.Marshal(),
171 },
172 {
173 desc: "groups (field overridden)",
174 decodeTo: []proto.Message{&testpb.TestAllTypes{
175 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
176 A: proto.Int32(2),
177 },
178 }, build(
179 &testpb.TestAllExtensions{},
180 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
181 A: proto.Int32(2),
182 }),
183 )},
184 wire: pack.Message{
185 pack.Tag{16, pack.StartGroupType},
186 pack.Tag{17, pack.VarintType}, pack.Varint(1),
187 pack.Tag{16, pack.EndGroupType},
188 pack.Tag{16, pack.StartGroupType},
189 pack.Tag{17, pack.VarintType}, pack.Varint(2),
190 pack.Tag{16, pack.EndGroupType},
191 }.Marshal(),
192 },
193 {
194 desc: "messages",
195 decodeTo: []proto.Message{&testpb.TestAllTypes{
196 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
197 A: proto.Int32(42),
198 Corecursive: &testpb.TestAllTypes{
199 OptionalInt32: proto.Int32(43),
200 },
201 },
202 }, &test3pb.TestAllTypes{
203 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
204 A: 42,
205 Corecursive: &test3pb.TestAllTypes{
206 OptionalInt32: 43,
207 },
208 },
209 }, build(
210 &testpb.TestAllExtensions{},
211 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
212 A: proto.Int32(42),
213 Corecursive: &testpb.TestAllTypes{
214 OptionalInt32: proto.Int32(43),
215 },
216 }),
217 )},
218 wire: pack.Message{
219 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
220 pack.Tag{1, pack.VarintType}, pack.Varint(42),
221 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
222 pack.Tag{1, pack.VarintType}, pack.Varint(43),
223 }),
224 }),
225 }.Marshal(),
226 },
227 {
228 desc: "messages (split across multiple tags)",
229 decodeTo: []proto.Message{&testpb.TestAllTypes{
230 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
231 A: proto.Int32(42),
232 Corecursive: &testpb.TestAllTypes{
233 OptionalInt32: proto.Int32(43),
234 },
235 },
236 }, &test3pb.TestAllTypes{
237 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
238 A: 42,
239 Corecursive: &test3pb.TestAllTypes{
240 OptionalInt32: 43,
241 },
242 },
243 }, build(
244 &testpb.TestAllExtensions{},
245 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
246 A: proto.Int32(42),
247 Corecursive: &testpb.TestAllTypes{
248 OptionalInt32: proto.Int32(43),
249 },
250 }),
251 )},
252 wire: pack.Message{
253 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
254 pack.Tag{1, pack.VarintType}, pack.Varint(42),
255 }),
256 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
257 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
258 pack.Tag{1, pack.VarintType}, pack.Varint(43),
259 }),
260 }),
261 }.Marshal(),
262 },
263 {
264 desc: "messages (field overridden)",
265 decodeTo: []proto.Message{&testpb.TestAllTypes{
266 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
267 A: proto.Int32(2),
268 },
269 }, &test3pb.TestAllTypes{
270 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
271 A: 2,
272 },
273 }, build(
274 &testpb.TestAllExtensions{},
275 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
276 A: proto.Int32(2),
277 }),
278 )},
279 wire: pack.Message{
280 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
281 pack.Tag{1, pack.VarintType}, pack.Varint(1),
282 }),
283 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
284 pack.Tag{1, pack.VarintType}, pack.Varint(2),
285 }),
286 }.Marshal(),
287 },
288 {
289 desc: "basic repeated types",
290 decodeTo: []proto.Message{&testpb.TestAllTypes{
291 RepeatedInt32: []int32{1001, 2001},
292 RepeatedInt64: []int64{1002, 2002},
293 RepeatedUint32: []uint32{1003, 2003},
294 RepeatedUint64: []uint64{1004, 2004},
295 RepeatedSint32: []int32{1005, 2005},
296 RepeatedSint64: []int64{1006, 2006},
297 RepeatedFixed32: []uint32{1007, 2007},
298 RepeatedFixed64: []uint64{1008, 2008},
299 RepeatedSfixed32: []int32{1009, 2009},
300 RepeatedSfixed64: []int64{1010, 2010},
301 RepeatedFloat: []float32{1011.5, 2011.5},
302 RepeatedDouble: []float64{1012.5, 2012.5},
303 RepeatedBool: []bool{true, false},
304 RepeatedString: []string{"foo", "bar"},
305 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
306 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
307 testpb.TestAllTypes_FOO,
308 testpb.TestAllTypes_BAR,
309 },
310 }, &test3pb.TestAllTypes{
311 RepeatedInt32: []int32{1001, 2001},
312 RepeatedInt64: []int64{1002, 2002},
313 RepeatedUint32: []uint32{1003, 2003},
314 RepeatedUint64: []uint64{1004, 2004},
315 RepeatedSint32: []int32{1005, 2005},
316 RepeatedSint64: []int64{1006, 2006},
317 RepeatedFixed32: []uint32{1007, 2007},
318 RepeatedFixed64: []uint64{1008, 2008},
319 RepeatedSfixed32: []int32{1009, 2009},
320 RepeatedSfixed64: []int64{1010, 2010},
321 RepeatedFloat: []float32{1011.5, 2011.5},
322 RepeatedDouble: []float64{1012.5, 2012.5},
323 RepeatedBool: []bool{true, false},
324 RepeatedString: []string{"foo", "bar"},
325 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
326 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
327 test3pb.TestAllTypes_FOO,
328 test3pb.TestAllTypes_BAR,
329 },
330 }, build(
331 &testpb.TestAllExtensions{},
332 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
333 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
334 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
335 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
336 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
337 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
338 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
339 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
340 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
341 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
342 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
343 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
344 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
345 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
346 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
347 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
348 testpb.TestAllTypes_FOO,
349 testpb.TestAllTypes_BAR,
350 }),
351 )},
352 wire: pack.Message{
353 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
354 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
355 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
356 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
357 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
358 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
359 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
360 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
361 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
362 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
363 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
364 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
365 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
366 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
367 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
368 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
369 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
370 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
371 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
372 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
373 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
374 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
375 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
376 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
377 pack.Tag{43, pack.VarintType}, pack.Bool(true),
378 pack.Tag{43, pack.VarintType}, pack.Bool(false),
379 pack.Tag{44, pack.BytesType}, pack.String("foo"),
380 pack.Tag{44, pack.BytesType}, pack.String("bar"),
381 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
382 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
383 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
384 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
385 }.Marshal(),
386 },
387 {
388 desc: "basic repeated types (packed encoding)",
389 decodeTo: []proto.Message{&testpb.TestAllTypes{
390 RepeatedInt32: []int32{1001, 2001},
391 RepeatedInt64: []int64{1002, 2002},
392 RepeatedUint32: []uint32{1003, 2003},
393 RepeatedUint64: []uint64{1004, 2004},
394 RepeatedSint32: []int32{1005, 2005},
395 RepeatedSint64: []int64{1006, 2006},
396 RepeatedFixed32: []uint32{1007, 2007},
397 RepeatedFixed64: []uint64{1008, 2008},
398 RepeatedSfixed32: []int32{1009, 2009},
399 RepeatedSfixed64: []int64{1010, 2010},
400 RepeatedFloat: []float32{1011.5, 2011.5},
401 RepeatedDouble: []float64{1012.5, 2012.5},
402 RepeatedBool: []bool{true, false},
403 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
404 testpb.TestAllTypes_FOO,
405 testpb.TestAllTypes_BAR,
406 },
407 }, &test3pb.TestAllTypes{
408 RepeatedInt32: []int32{1001, 2001},
409 RepeatedInt64: []int64{1002, 2002},
410 RepeatedUint32: []uint32{1003, 2003},
411 RepeatedUint64: []uint64{1004, 2004},
412 RepeatedSint32: []int32{1005, 2005},
413 RepeatedSint64: []int64{1006, 2006},
414 RepeatedFixed32: []uint32{1007, 2007},
415 RepeatedFixed64: []uint64{1008, 2008},
416 RepeatedSfixed32: []int32{1009, 2009},
417 RepeatedSfixed64: []int64{1010, 2010},
418 RepeatedFloat: []float32{1011.5, 2011.5},
419 RepeatedDouble: []float64{1012.5, 2012.5},
420 RepeatedBool: []bool{true, false},
421 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
422 test3pb.TestAllTypes_FOO,
423 test3pb.TestAllTypes_BAR,
424 },
425 }, build(
426 &testpb.TestAllExtensions{},
427 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
428 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
429 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
430 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
431 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
432 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
433 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
434 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
435 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
436 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
437 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
438 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
439 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
440 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
441 testpb.TestAllTypes_FOO,
442 testpb.TestAllTypes_BAR,
443 }),
444 )},
445 wire: pack.Message{
446 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
447 pack.Varint(1001), pack.Varint(2001),
448 },
449 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
450 pack.Varint(1002), pack.Varint(2002),
451 },
452 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
453 pack.Uvarint(1003), pack.Uvarint(2003),
454 },
455 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
456 pack.Uvarint(1004), pack.Uvarint(2004),
457 },
458 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
459 pack.Svarint(1005), pack.Svarint(2005),
460 },
461 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
462 pack.Svarint(1006), pack.Svarint(2006),
463 },
464 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
465 pack.Uint32(1007), pack.Uint32(2007),
466 },
467 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
468 pack.Uint64(1008), pack.Uint64(2008),
469 },
470 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
471 pack.Int32(1009), pack.Int32(2009),
472 },
473 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
474 pack.Int64(1010), pack.Int64(2010),
475 },
476 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
477 pack.Float32(1011.5), pack.Float32(2011.5),
478 },
479 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
480 pack.Float64(1012.5), pack.Float64(2012.5),
481 },
482 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
483 pack.Bool(true), pack.Bool(false),
484 },
485 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
486 pack.Varint(int(testpb.TestAllTypes_FOO)),
487 pack.Varint(int(testpb.TestAllTypes_BAR)),
488 },
489 }.Marshal(),
490 },
491 {
492 desc: "basic repeated types (zero-length packed encoding)",
493 decodeTo: []proto.Message{
494 &testpb.TestAllTypes{},
495 &test3pb.TestAllTypes{},
496 &testpb.TestAllExtensions{},
497 },
498 wire: pack.Message{
499 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{},
500 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{},
501 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{},
502 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{},
503 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{},
504 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{},
505 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{},
506 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{},
507 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{},
508 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{},
509 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{},
510 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{},
511 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{},
512 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{},
513 }.Marshal(),
514 },
515 {
516 desc: "packed repeated types",
517 decodeTo: []proto.Message{&testpb.TestPackedTypes{
518 PackedInt32: []int32{1001, 2001},
519 PackedInt64: []int64{1002, 2002},
520 PackedUint32: []uint32{1003, 2003},
521 PackedUint64: []uint64{1004, 2004},
522 PackedSint32: []int32{1005, 2005},
523 PackedSint64: []int64{1006, 2006},
524 PackedFixed32: []uint32{1007, 2007},
525 PackedFixed64: []uint64{1008, 2008},
526 PackedSfixed32: []int32{1009, 2009},
527 PackedSfixed64: []int64{1010, 2010},
528 PackedFloat: []float32{1011.5, 2011.5},
529 PackedDouble: []float64{1012.5, 2012.5},
530 PackedBool: []bool{true, false},
531 PackedEnum: []testpb.ForeignEnum{
532 testpb.ForeignEnum_FOREIGN_FOO,
533 testpb.ForeignEnum_FOREIGN_BAR,
534 },
535 }, build(
536 &testpb.TestPackedExtensions{},
537 extend(testpb.E_PackedInt32Extension, []int32{1001, 2001}),
538 extend(testpb.E_PackedInt64Extension, []int64{1002, 2002}),
539 extend(testpb.E_PackedUint32Extension, []uint32{1003, 2003}),
540 extend(testpb.E_PackedUint64Extension, []uint64{1004, 2004}),
541 extend(testpb.E_PackedSint32Extension, []int32{1005, 2005}),
542 extend(testpb.E_PackedSint64Extension, []int64{1006, 2006}),
543 extend(testpb.E_PackedFixed32Extension, []uint32{1007, 2007}),
544 extend(testpb.E_PackedFixed64Extension, []uint64{1008, 2008}),
545 extend(testpb.E_PackedSfixed32Extension, []int32{1009, 2009}),
546 extend(testpb.E_PackedSfixed64Extension, []int64{1010, 2010}),
547 extend(testpb.E_PackedFloatExtension, []float32{1011.5, 2011.5}),
548 extend(testpb.E_PackedDoubleExtension, []float64{1012.5, 2012.5}),
549 extend(testpb.E_PackedBoolExtension, []bool{true, false}),
550 extend(testpb.E_PackedEnumExtension, []testpb.ForeignEnum{
551 testpb.ForeignEnum_FOREIGN_FOO,
552 testpb.ForeignEnum_FOREIGN_BAR,
553 }),
554 )},
555 wire: pack.Message{
556 pack.Tag{90, pack.BytesType}, pack.LengthPrefix{
557 pack.Varint(1001), pack.Varint(2001),
558 },
559 pack.Tag{91, pack.BytesType}, pack.LengthPrefix{
560 pack.Varint(1002), pack.Varint(2002),
561 },
562 pack.Tag{92, pack.BytesType}, pack.LengthPrefix{
563 pack.Uvarint(1003), pack.Uvarint(2003),
564 },
565 pack.Tag{93, pack.BytesType}, pack.LengthPrefix{
566 pack.Uvarint(1004), pack.Uvarint(2004),
567 },
568 pack.Tag{94, pack.BytesType}, pack.LengthPrefix{
569 pack.Svarint(1005), pack.Svarint(2005),
570 },
571 pack.Tag{95, pack.BytesType}, pack.LengthPrefix{
572 pack.Svarint(1006), pack.Svarint(2006),
573 },
574 pack.Tag{96, pack.BytesType}, pack.LengthPrefix{
575 pack.Uint32(1007), pack.Uint32(2007),
576 },
577 pack.Tag{97, pack.BytesType}, pack.LengthPrefix{
578 pack.Uint64(1008), pack.Uint64(2008),
579 },
580 pack.Tag{98, pack.BytesType}, pack.LengthPrefix{
581 pack.Int32(1009), pack.Int32(2009),
582 },
583 pack.Tag{99, pack.BytesType}, pack.LengthPrefix{
584 pack.Int64(1010), pack.Int64(2010),
585 },
586 pack.Tag{100, pack.BytesType}, pack.LengthPrefix{
587 pack.Float32(1011.5), pack.Float32(2011.5),
588 },
589 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{
590 pack.Float64(1012.5), pack.Float64(2012.5),
591 },
592 pack.Tag{102, pack.BytesType}, pack.LengthPrefix{
593 pack.Bool(true), pack.Bool(false),
594 },
595 pack.Tag{103, pack.BytesType}, pack.LengthPrefix{
596 pack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
597 pack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
598 },
599 }.Marshal(),
600 },
601 {
602 desc: "packed repeated types (zero length)",
603 decodeTo: []proto.Message{
604 &testpb.TestPackedTypes{},
605 &testpb.TestPackedExtensions{},
606 },
607 wire: pack.Message{
608 pack.Tag{90, pack.BytesType}, pack.LengthPrefix{},
609 pack.Tag{91, pack.BytesType}, pack.LengthPrefix{},
610 pack.Tag{92, pack.BytesType}, pack.LengthPrefix{},
611 pack.Tag{93, pack.BytesType}, pack.LengthPrefix{},
612 pack.Tag{94, pack.BytesType}, pack.LengthPrefix{},
613 pack.Tag{95, pack.BytesType}, pack.LengthPrefix{},
614 pack.Tag{96, pack.BytesType}, pack.LengthPrefix{},
615 pack.Tag{97, pack.BytesType}, pack.LengthPrefix{},
616 pack.Tag{98, pack.BytesType}, pack.LengthPrefix{},
617 pack.Tag{99, pack.BytesType}, pack.LengthPrefix{},
618 pack.Tag{100, pack.BytesType}, pack.LengthPrefix{},
619 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{},
620 pack.Tag{102, pack.BytesType}, pack.LengthPrefix{},
621 pack.Tag{103, pack.BytesType}, pack.LengthPrefix{},
622 }.Marshal(),
623 },
624 {
625 desc: "repeated messages",
626 decodeTo: []proto.Message{&testpb.TestAllTypes{
627 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
628 {A: proto.Int32(1)},
629 nil,
630 {A: proto.Int32(2)},
631 },
632 }, &test3pb.TestAllTypes{
633 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
634 {A: 1},
635 nil,
636 {A: 2},
637 },
638 }, build(
639 &testpb.TestAllExtensions{},
640 extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
641 {A: proto.Int32(1)},
642 nil,
643 {A: proto.Int32(2)},
644 }),
645 )},
646 wire: pack.Message{
647 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
648 pack.Tag{1, pack.VarintType}, pack.Varint(1),
649 }),
650 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
651 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
652 pack.Tag{1, pack.VarintType}, pack.Varint(2),
653 }),
654 }.Marshal(),
655 },
656 {
657 desc: "repeated groups",
658 decodeTo: []proto.Message{&testpb.TestAllTypes{
659 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
660 {A: proto.Int32(1017)},
661 nil,
662 {A: proto.Int32(2017)},
663 },
664 }, build(
665 &testpb.TestAllExtensions{},
666 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
667 {A: proto.Int32(1017)},
668 nil,
669 {A: proto.Int32(2017)},
670 }),
671 )},
672 wire: pack.Message{
673 pack.Tag{46, pack.StartGroupType},
674 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
675 pack.Tag{46, pack.EndGroupType},
676 pack.Tag{46, pack.StartGroupType},
677 pack.Tag{46, pack.EndGroupType},
678 pack.Tag{46, pack.StartGroupType},
679 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
680 pack.Tag{46, pack.EndGroupType},
681 }.Marshal(),
682 },
683 {
684 desc: "maps",
685 decodeTo: []proto.Message{&testpb.TestAllTypes{
686 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
687 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
688 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
689 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
690 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
691 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
692 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
693 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
694 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
695 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
696 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
697 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
698 MapBoolBool: map[bool]bool{true: false, false: true},
699 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
700 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
701 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
702 "71.1.key": {A: proto.Int32(1171)},
703 "71.2.key": {A: proto.Int32(2171)},
704 },
705 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
706 "73.1.key": testpb.TestAllTypes_FOO,
707 "73.2.key": testpb.TestAllTypes_BAR,
708 },
709 }, &test3pb.TestAllTypes{
710 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
711 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
712 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
713 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
714 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
715 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
716 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
717 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
718 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
719 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
720 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
721 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
722 MapBoolBool: map[bool]bool{true: false, false: true},
723 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
724 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
725 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
726 "71.1.key": {A: 1171},
727 "71.2.key": {A: 2171},
728 },
729 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
730 "73.1.key": test3pb.TestAllTypes_FOO,
731 "73.2.key": test3pb.TestAllTypes_BAR,
732 },
733 }},
734 wire: pack.Message{
735 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
736 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
737 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
738 }),
739 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
740 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
741 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
742 }),
743 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
744 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
745 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
746 }),
747 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
748 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
749 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
750 }),
751 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
752 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
753 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
754 }),
755 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
756 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
757 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
758 }),
759 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
760 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
761 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
762 }),
763 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
764 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
765 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
766 }),
767 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
768 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
769 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
770 }),
771 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
772 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
773 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
774 }),
775 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
776 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
777 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
778 }),
779 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
780 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
781 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
782 }),
783 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
784 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
785 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
786 }),
787 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
788 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
789 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
790 }),
791 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
792 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
793 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
794 }),
795 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
796 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
797 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
798 }),
799 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
800 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
801 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
802 }),
803 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
804 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
805 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
806 }),
807 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
808 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
809 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
810 }),
811 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
812 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
813 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
814 }),
815 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
816 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
817 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
818 }),
819 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
820 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
821 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
822 }),
823 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
824 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
825 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
826 }),
827 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
828 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
829 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
830 }),
831 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
832 pack.Tag{1, pack.VarintType}, pack.Bool(true),
833 pack.Tag{2, pack.VarintType}, pack.Bool(false),
834 }),
835 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
836 pack.Tag{1, pack.VarintType}, pack.Bool(false),
837 pack.Tag{2, pack.VarintType}, pack.Bool(true),
838 }),
839 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
840 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
841 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
842 }),
843 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
844 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
845 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
846 }),
847 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
848 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
849 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
850 }),
851 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
852 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
853 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
854 }),
855 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
856 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
857 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
858 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
859 }),
860 }),
861 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
862 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
863 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
864 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
865 }),
866 }),
867 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
868 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
869 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
870 }),
871 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
872 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
873 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
874 }),
875 }.Marshal(),
876 },
877 {
Damien Neil7e690b52019-12-18 09:35:01 -0800878 desc: "map with value before key",
879 decodeTo: []proto.Message{&testpb.TestAllTypes{
880 MapInt32Int32: map[int32]int32{1056: 1156},
881 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
882 "71.1.key": {A: proto.Int32(1171)},
883 },
884 }},
885 wire: pack.Message{
886 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
887 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
888 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
889 }),
890 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
891 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
892 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
893 }),
894 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
895 }),
896 }.Marshal(),
897 },
898 {
899 desc: "map with repeated key and value",
900 decodeTo: []proto.Message{&testpb.TestAllTypes{
901 MapInt32Int32: map[int32]int32{1056: 1156},
902 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
903 "71.1.key": {A: proto.Int32(1171)},
904 },
905 }},
906 wire: pack.Message{
907 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
908 pack.Tag{1, pack.VarintType}, pack.Varint(0),
909 pack.Tag{2, pack.VarintType}, pack.Varint(0),
910 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
911 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
912 }),
913 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
914 pack.Tag{1, pack.BytesType}, pack.String(0),
915 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
916 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
917 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
918 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
919 }),
920 }),
921 }.Marshal(),
922 },
923 {
Damien Neild0b07492019-12-16 12:59:13 -0800924 desc: "oneof (uint32)",
925 decodeTo: []proto.Message{
926 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
927 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
928 },
929 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
930 },
931 {
932 desc: "oneof (message)",
933 decodeTo: []proto.Message{
934 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
935 &testpb.TestAllTypes_NestedMessage{A: proto.Int32(1112)},
936 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
937 &test3pb.TestAllTypes_NestedMessage{A: 1112},
938 }},
939 },
940 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
941 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
942 })}.Marshal(),
943 },
944 {
945 desc: "oneof (empty message)",
946 decodeTo: []proto.Message{
947 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
948 &testpb.TestAllTypes_NestedMessage{},
949 }},
950 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
951 &test3pb.TestAllTypes_NestedMessage{},
952 }},
953 },
954 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
955 },
956 {
957 desc: "oneof (merged message)",
958 decodeTo: []proto.Message{
959 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
960 &testpb.TestAllTypes_NestedMessage{
961 A: proto.Int32(1),
962 Corecursive: &testpb.TestAllTypes{
963 OptionalInt32: proto.Int32(43),
964 },
965 },
966 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
967 &test3pb.TestAllTypes_NestedMessage{
968 A: 1,
969 Corecursive: &test3pb.TestAllTypes{
970 OptionalInt32: 43,
971 },
972 },
973 }}},
974 wire: pack.Message{
975 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
976 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
977 }),
978 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
979 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
980 pack.Tag{1, pack.VarintType}, pack.Varint(43),
981 }),
982 }),
983 }.Marshal(),
984 },
985 {
986 desc: "oneof (string)",
987 decodeTo: []proto.Message{
988 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
989 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
990 },
991 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
992 },
993 {
994 desc: "oneof (bytes)",
995 decodeTo: []proto.Message{
996 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
997 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
998 },
999 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
1000 },
1001 {
1002 desc: "oneof (bool)",
1003 decodeTo: []proto.Message{
1004 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
1005 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
1006 },
1007 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
1008 },
1009 {
1010 desc: "oneof (uint64)",
1011 decodeTo: []proto.Message{
1012 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
1013 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
1014 },
1015 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
1016 },
1017 {
1018 desc: "oneof (float)",
1019 decodeTo: []proto.Message{
1020 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
1021 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
1022 },
1023 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
1024 },
1025 {
1026 desc: "oneof (double)",
1027 decodeTo: []proto.Message{
1028 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
1029 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
1030 },
1031 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
1032 },
1033 {
1034 desc: "oneof (enum)",
1035 decodeTo: []proto.Message{
1036 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
1037 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
1038 },
1039 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
1040 },
1041 {
1042 desc: "oneof (zero)",
1043 decodeTo: []proto.Message{
1044 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}},
1045 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}},
1046 },
1047 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
1048 },
1049 {
1050 desc: "oneof (overridden value)",
1051 decodeTo: []proto.Message{
1052 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
1053 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
1054 },
1055 wire: pack.Message{
1056 pack.Tag{111, pack.VarintType}, pack.Varint(1),
1057 pack.Tag{116, pack.VarintType}, pack.Varint(2),
1058 }.Marshal(),
1059 },
1060 // TODO: More unknown field tests for ordering, repeated fields, etc.
1061 //
1062 // It is currently impossible to produce results that the v1 Equal
1063 // considers equivalent to those of the v1 decoder. Figure out if
1064 // that's a problem or not.
1065 {
1066 desc: "unknown fields",
1067 decodeTo: []proto.Message{build(
1068 &testpb.TestAllTypes{},
1069 unknown(pack.Message{
1070 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1071 }.Marshal()),
1072 ), build(
1073 &test3pb.TestAllTypes{},
1074 unknown(pack.Message{
1075 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1076 }.Marshal()),
1077 )},
1078 wire: pack.Message{
1079 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1080 }.Marshal(),
1081 },
1082 {
1083 desc: "field type mismatch",
1084 decodeTo: []proto.Message{build(
1085 &testpb.TestAllTypes{},
1086 unknown(pack.Message{
1087 pack.Tag{1, pack.BytesType}, pack.String("string"),
1088 }.Marshal()),
1089 ), build(
1090 &test3pb.TestAllTypes{},
1091 unknown(pack.Message{
1092 pack.Tag{1, pack.BytesType}, pack.String("string"),
1093 }.Marshal()),
1094 )},
1095 wire: pack.Message{
1096 pack.Tag{1, pack.BytesType}, pack.String("string"),
1097 }.Marshal(),
1098 },
1099 {
1100 desc: "map field element mismatch",
1101 decodeTo: []proto.Message{
1102 &testpb.TestAllTypes{
1103 MapInt32Int32: map[int32]int32{1: 0},
1104 }, &test3pb.TestAllTypes{
1105 MapInt32Int32: map[int32]int32{1: 0},
1106 },
1107 },
1108 wire: pack.Message{
1109 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
1110 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1111 pack.Tag{2, pack.BytesType}, pack.String("string"),
1112 }),
1113 }.Marshal(),
1114 },
1115 {
1116 desc: "required field in nil message unset",
1117 partial: true,
1118 decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
1119 },
1120 {
1121 desc: "required field unset",
1122 partial: true,
1123 decodeTo: []proto.Message{&testpb.TestRequired{}},
1124 },
1125 {
1126 desc: "required field set",
1127 decodeTo: []proto.Message{&testpb.TestRequired{
1128 RequiredField: proto.Int32(1),
1129 }},
1130 wire: pack.Message{
1131 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1132 }.Marshal(),
1133 },
1134 {
1135 desc: "required field in optional message unset",
1136 partial: true,
1137 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1138 OptionalMessage: &testpb.TestRequired{},
1139 }},
1140 wire: pack.Message{
1141 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1142 }.Marshal(),
1143 },
1144 {
1145 desc: "required field in optional message set",
1146 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1147 OptionalMessage: &testpb.TestRequired{
1148 RequiredField: proto.Int32(1),
1149 },
1150 }},
1151 wire: pack.Message{
1152 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1153 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1154 }),
1155 }.Marshal(),
1156 },
1157 {
1158 desc: "required field in optional message set (split across multiple tags)",
1159 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1160 OptionalMessage: &testpb.TestRequired{
1161 RequiredField: proto.Int32(1),
1162 },
1163 }},
1164 wire: pack.Message{
1165 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1166 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1167 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1168 }),
1169 }.Marshal(),
1170 },
1171 {
1172 desc: "required field in repeated message unset",
1173 partial: true,
1174 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1175 RepeatedMessage: []*testpb.TestRequired{
1176 {RequiredField: proto.Int32(1)},
1177 {},
1178 },
1179 }},
1180 wire: pack.Message{
1181 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1182 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1183 }),
1184 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1185 }.Marshal(),
1186 },
1187 {
1188 desc: "required field in repeated message set",
1189 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1190 RepeatedMessage: []*testpb.TestRequired{
1191 {RequiredField: proto.Int32(1)},
1192 {RequiredField: proto.Int32(2)},
1193 },
1194 }},
1195 wire: pack.Message{
1196 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1197 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1198 }),
1199 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1200 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1201 }),
1202 }.Marshal(),
1203 },
1204 {
1205 desc: "required field in map message unset",
1206 partial: true,
1207 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1208 MapMessage: map[int32]*testpb.TestRequired{
1209 1: {RequiredField: proto.Int32(1)},
1210 2: {},
1211 },
1212 }},
1213 wire: pack.Message{
1214 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1215 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1216 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1217 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1218 }),
1219 }),
1220 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1221 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1222 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1223 }),
1224 }.Marshal(),
1225 },
1226 {
1227 desc: "required field in map message set",
1228 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1229 MapMessage: map[int32]*testpb.TestRequired{
1230 1: {RequiredField: proto.Int32(1)},
1231 2: {RequiredField: proto.Int32(2)},
1232 },
1233 }},
1234 wire: pack.Message{
1235 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1236 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1237 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1238 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1239 }),
1240 }),
1241 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1242 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1243 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1244 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1245 }),
1246 }),
1247 }.Marshal(),
1248 },
1249 {
1250 desc: "required field in optional group unset",
1251 partial: true,
1252 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1253 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1254 }},
1255 wire: pack.Message{
1256 pack.Tag{1, pack.StartGroupType},
1257 pack.Tag{1, pack.EndGroupType},
1258 }.Marshal(),
1259 },
1260 {
1261 desc: "required field in optional group set",
1262 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1263 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
1264 A: proto.Int32(1),
1265 },
1266 }},
1267 wire: pack.Message{
1268 pack.Tag{1, pack.StartGroupType},
1269 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1270 pack.Tag{1, pack.EndGroupType},
1271 }.Marshal(),
1272 },
1273 {
1274 desc: "required field in repeated group unset",
1275 partial: true,
1276 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1277 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1278 {A: proto.Int32(1)},
1279 {},
1280 },
1281 }},
1282 wire: pack.Message{
1283 pack.Tag{3, pack.StartGroupType},
1284 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1285 pack.Tag{3, pack.EndGroupType},
1286 pack.Tag{3, pack.StartGroupType},
1287 pack.Tag{3, pack.EndGroupType},
1288 }.Marshal(),
1289 },
1290 {
1291 desc: "required field in repeated group set",
1292 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1293 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1294 {A: proto.Int32(1)},
1295 {A: proto.Int32(2)},
1296 },
1297 }},
1298 wire: pack.Message{
1299 pack.Tag{3, pack.StartGroupType},
1300 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1301 pack.Tag{3, pack.EndGroupType},
1302 pack.Tag{3, pack.StartGroupType},
1303 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1304 pack.Tag{3, pack.EndGroupType},
1305 }.Marshal(),
1306 },
1307 {
1308 desc: "required field in oneof message unset",
1309 partial: true,
1310 decodeTo: []proto.Message{
1311 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1312 &testpb.TestRequired{},
1313 }},
1314 },
1315 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1316 },
1317 {
1318 desc: "required field in oneof message set",
1319 decodeTo: []proto.Message{
1320 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1321 &testpb.TestRequired{
1322 RequiredField: proto.Int32(1),
1323 },
1324 }},
1325 },
1326 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
1327 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1328 })}.Marshal(),
1329 },
1330 {
1331 desc: "required field in extension message unset",
1332 partial: true,
1333 decodeTo: []proto.Message{build(
1334 &testpb.TestAllExtensions{},
1335 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1336 )},
1337 wire: pack.Message{
1338 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1339 }.Marshal(),
1340 },
1341 {
1342 desc: "required field in extension message set",
1343 decodeTo: []proto.Message{build(
1344 &testpb.TestAllExtensions{},
1345 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
1346 RequiredField: proto.Int32(1),
1347 }),
1348 )},
1349 wire: pack.Message{
1350 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1351 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1352 }),
1353 }.Marshal(),
1354 },
1355 {
1356 desc: "required field in repeated extension message unset",
1357 partial: true,
1358 decodeTo: []proto.Message{build(
1359 &testpb.TestAllExtensions{},
1360 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1361 {RequiredField: proto.Int32(1)},
1362 {},
1363 }),
1364 )},
1365 wire: pack.Message{
1366 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1367 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1368 }),
1369 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1370 }.Marshal(),
1371 },
1372 {
1373 desc: "required field in repeated extension message set",
1374 decodeTo: []proto.Message{build(
1375 &testpb.TestAllExtensions{},
1376 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1377 {RequiredField: proto.Int32(1)},
1378 {RequiredField: proto.Int32(2)},
1379 }),
1380 )},
1381 wire: pack.Message{
1382 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1383 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1384 }),
1385 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1386 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1387 }),
1388 }.Marshal(),
1389 },
1390 {
1391 desc: "nil messages",
1392 decodeTo: []proto.Message{
1393 (*testpb.TestAllTypes)(nil),
1394 (*test3pb.TestAllTypes)(nil),
1395 (*testpb.TestAllExtensions)(nil),
1396 },
1397 },
1398 {
1399 desc: "legacy",
1400 partial: true,
1401 decodeTo: []proto.Message{
1402 &legacypb.Legacy{
1403 F1: &legacy1pb.Message{
1404 OptionalInt32: proto.Int32(1),
1405 OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
1406 OptionalChildMessage: &legacy1pb.Message_ChildMessage{
1407 F1: proto.String("x"),
1408 },
1409 Optionalgroup: &legacy1pb.Message_OptionalGroup{
1410 F1: proto.String("x"),
1411 },
1412 RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
1413 {F1: proto.String("x")},
1414 },
1415 Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
1416 {F1: proto.String("x")},
1417 },
1418 MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
1419 true: {F1: proto.String("x")},
1420 },
1421 OneofUnion: &legacy1pb.Message_OneofChildMessage{
1422 &legacy1pb.Message_ChildMessage{
1423 F1: proto.String("x"),
1424 },
1425 },
1426 },
1427 },
1428 },
1429 wire: pack.Message{
1430 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1431 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1432 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1433 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1434 pack.Tag{1, pack.BytesType}, pack.String("x"),
1435 }),
1436 pack.Tag{120, pack.StartGroupType},
1437 pack.Tag{1, pack.BytesType}, pack.String("x"),
1438 pack.Tag{120, pack.EndGroupType},
1439 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1440 pack.Tag{1, pack.BytesType}, pack.String("x"),
1441 }),
1442 pack.Tag{520, pack.StartGroupType},
1443 pack.Tag{1, pack.BytesType}, pack.String("x"),
1444 pack.Tag{520, pack.EndGroupType},
1445 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1446 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1447 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1448 pack.Tag{1, pack.BytesType}, pack.String("x"),
1449 }),
1450 }),
1451 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1452 pack.Tag{1, pack.BytesType}, pack.String("x"),
1453 }),
1454 }),
1455 }.Marshal(),
1456 },
1457 {
1458 desc: "first reserved field number",
1459 decodeTo: []proto.Message{build(
1460 &testpb.TestAllTypes{},
1461 unknown(pack.Message{
1462 pack.Tag{pack.FirstReservedNumber, pack.VarintType}, pack.Varint(1004),
1463 }.Marshal()),
1464 )},
1465 wire: pack.Message{
1466 pack.Tag{pack.FirstReservedNumber, pack.VarintType}, pack.Varint(1004),
1467 }.Marshal(),
1468 },
1469 {
1470 desc: "last reserved field number",
1471 decodeTo: []proto.Message{build(
1472 &testpb.TestAllTypes{},
1473 unknown(pack.Message{
1474 pack.Tag{pack.LastReservedNumber, pack.VarintType}, pack.Varint(1005),
1475 }.Marshal()),
1476 )},
1477 wire: pack.Message{
1478 pack.Tag{pack.LastReservedNumber, pack.VarintType}, pack.Varint(1005),
1479 }.Marshal(),
1480 },
1481}
1482
1483var testInvalidMessages = []testProto{
1484 {
1485 desc: "invalid UTF-8 in optional string field",
1486 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1487 OptionalString: "abc\xff",
1488 }},
1489 wire: pack.Message{
1490 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1491 }.Marshal(),
1492 },
1493 {
1494 desc: "invalid UTF-8 in repeated string field",
1495 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1496 RepeatedString: []string{"foo", "abc\xff"},
1497 }},
1498 wire: pack.Message{
1499 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1500 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1501 }.Marshal(),
1502 },
1503 {
1504 desc: "invalid UTF-8 in nested message",
1505 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1506 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1507 Corecursive: &test3pb.TestAllTypes{
1508 OptionalString: "abc\xff",
1509 },
1510 },
1511 }},
1512 wire: pack.Message{
1513 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1514 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1515 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1516 }),
1517 }),
1518 }.Marshal(),
1519 },
1520 {
1521 desc: "invalid UTF-8 in oneof field",
1522 decodeTo: []proto.Message{
1523 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
1524 },
1525 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1526 },
1527 {
1528 desc: "invalid UTF-8 in map key",
1529 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1530 MapStringString: map[string]string{"key\xff": "val"},
1531 }},
1532 wire: pack.Message{
1533 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1534 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1535 pack.Tag{2, pack.BytesType}, pack.String("val"),
1536 }),
1537 }.Marshal(),
1538 },
1539 {
1540 desc: "invalid UTF-8 in map value",
1541 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1542 MapStringString: map[string]string{"key": "val\xff"},
1543 }},
1544 wire: pack.Message{
1545 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1546 pack.Tag{1, pack.BytesType}, pack.String("key"),
1547 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1548 }),
1549 }.Marshal(),
1550 },
1551 {
1552 desc: "invalid field number zero",
1553 decodeTo: []proto.Message{(*testpb.TestAllTypes)(nil)},
1554 wire: pack.Message{
1555 pack.Tag{pack.MinValidNumber - 1, pack.VarintType}, pack.Varint(1001),
1556 }.Marshal(),
1557 },
1558 {
1559 desc: "invalid field numbers zero and one",
1560 decodeTo: []proto.Message{(*testpb.TestAllTypes)(nil)},
1561 wire: pack.Message{
1562 pack.Tag{pack.MinValidNumber - 1, pack.VarintType}, pack.Varint(1002),
1563 pack.Tag{pack.MinValidNumber, pack.VarintType}, pack.Varint(1003),
1564 }.Marshal(),
1565 },
1566 {
1567 desc: "invalid field numbers max and max+1",
1568 decodeTo: []proto.Message{(*testpb.TestAllTypes)(nil)},
1569 wire: pack.Message{
1570 pack.Tag{pack.MaxValidNumber, pack.VarintType}, pack.Varint(1006),
1571 pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(1007),
1572 }.Marshal(),
1573 },
1574 {
1575 desc: "invalid field number max+1",
1576 decodeTo: []proto.Message{(*testpb.TestAllTypes)(nil)},
1577 wire: pack.Message{
1578 pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(1008),
1579 }.Marshal(),
1580 },
1581}