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