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