blob: a1c1d1ec9987b5035d2f1adad2e81a5c56b4d8ac [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 Neild025c952020-02-02 00:53:34 -080011 "google.golang.org/protobuf/internal/protobuild"
Damien Neild0b07492019-12-16 12:59:13 -080012 "google.golang.org/protobuf/proto"
Damien Neil1c33e112020-02-04 12:58:17 -080013 "google.golang.org/protobuf/reflect/protoreflect"
Damien Neila60e7092020-01-28 14:53:44 -080014 "google.golang.org/protobuf/reflect/protoregistry"
Damien Neild0b07492019-12-16 12:59:13 -080015
16 legacypb "google.golang.org/protobuf/internal/testprotos/legacy"
Damien Neil6635e7d2020-01-15 15:08:57 -080017 requiredpb "google.golang.org/protobuf/internal/testprotos/required"
Damien Neild0b07492019-12-16 12:59:13 -080018 testpb "google.golang.org/protobuf/internal/testprotos/test"
19 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
20)
21
22type testProto struct {
Damien Neilb0c26f12019-12-16 09:37:59 -080023 desc string
24 decodeTo []proto.Message
25 wire []byte
26 partial bool
27 noEncode bool
Damien Neilc600d6c2020-01-21 15:00:33 -080028 checkFastInit bool
Damien Neila60e7092020-01-28 14:53:44 -080029 unmarshalOptions proto.UnmarshalOptions
Damien Neilb0c26f12019-12-16 09:37:59 -080030 validationStatus impl.ValidationStatus
Damien Neilcadb4ab2020-02-03 16:17:31 -080031 nocheckValidInit bool
Damien Neild0b07492019-12-16 12:59:13 -080032}
33
Damien Neild025c952020-02-02 00:53:34 -080034func makeMessages(in protobuild.Message, messages ...proto.Message) []proto.Message {
35 if len(messages) == 0 {
36 messages = []proto.Message{
37 &testpb.TestAllTypes{},
38 &test3pb.TestAllTypes{},
39 &testpb.TestAllExtensions{},
40 }
41 }
42 for _, m := range messages {
43 in.Build(m.ProtoReflect())
44 }
45 return messages
46}
47
Damien Neild0b07492019-12-16 12:59:13 -080048var testValidMessages = []testProto{
49 {
Damien Neil1887ff72020-01-29 16:40:05 -080050 desc: "basic scalar types",
51 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -080052 decodeTo: makeMessages(protobuild.Message{
53 "optional_int32": 1001,
54 "optional_int64": 1002,
55 "optional_uint32": 1003,
56 "optional_uint64": 1004,
57 "optional_sint32": 1005,
58 "optional_sint64": 1006,
59 "optional_fixed32": 1007,
60 "optional_fixed64": 1008,
61 "optional_sfixed32": 1009,
62 "optional_sfixed64": 1010,
63 "optional_float": 1011.5,
64 "optional_double": 1012.5,
65 "optional_bool": true,
66 "optional_string": "string",
67 "optional_bytes": []byte("bytes"),
68 "optional_nested_enum": "BAR",
69 }),
Damien Neild0b07492019-12-16 12:59:13 -080070 wire: pack.Message{
71 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
72 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
73 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
74 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
75 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
76 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
77 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
78 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
79 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
80 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
81 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
82 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
83 pack.Tag{13, pack.VarintType}, pack.Bool(true),
84 pack.Tag{14, pack.BytesType}, pack.String("string"),
85 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
86 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
87 }.Marshal(),
88 },
89 {
90 desc: "zero values",
Damien Neild025c952020-02-02 00:53:34 -080091 decodeTo: makeMessages(protobuild.Message{
92 "optional_int32": 0,
93 "optional_int64": 0,
94 "optional_uint32": 0,
95 "optional_uint64": 0,
96 "optional_sint32": 0,
97 "optional_sint64": 0,
98 "optional_fixed32": 0,
99 "optional_fixed64": 0,
100 "optional_sfixed32": 0,
101 "optional_sfixed64": 0,
102 "optional_float": 0,
103 "optional_double": 0,
104 "optional_bool": false,
105 "optional_string": "",
106 "optional_bytes": []byte{},
107 }),
Damien Neild0b07492019-12-16 12:59:13 -0800108 wire: pack.Message{
109 pack.Tag{1, pack.VarintType}, pack.Varint(0),
110 pack.Tag{2, pack.VarintType}, pack.Varint(0),
111 pack.Tag{3, pack.VarintType}, pack.Uvarint(0),
112 pack.Tag{4, pack.VarintType}, pack.Uvarint(0),
113 pack.Tag{5, pack.VarintType}, pack.Svarint(0),
114 pack.Tag{6, pack.VarintType}, pack.Svarint(0),
115 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(0),
116 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(0),
117 pack.Tag{9, pack.Fixed32Type}, pack.Int32(0),
118 pack.Tag{10, pack.Fixed64Type}, pack.Int64(0),
119 pack.Tag{11, pack.Fixed32Type}, pack.Float32(0),
120 pack.Tag{12, pack.Fixed64Type}, pack.Float64(0),
121 pack.Tag{13, pack.VarintType}, pack.Bool(false),
122 pack.Tag{14, pack.BytesType}, pack.String(""),
123 pack.Tag{15, pack.BytesType}, pack.Bytes(nil),
124 }.Marshal(),
125 },
126 {
127 desc: "groups",
Damien Neild025c952020-02-02 00:53:34 -0800128 decodeTo: makeMessages(protobuild.Message{
129 "optionalgroup": protobuild.Message{
130 "a": 1017,
131 "same_field_number": 1016,
Damien Neild0b07492019-12-16 12:59:13 -0800132 },
Damien Neild025c952020-02-02 00:53:34 -0800133 }, &testpb.TestAllTypes{}, &testpb.TestAllExtensions{}),
Damien Neild0b07492019-12-16 12:59:13 -0800134 wire: pack.Message{
135 pack.Tag{16, pack.StartGroupType},
136 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
Damien Neil2ae60932020-01-14 11:12:21 -0800137 pack.Tag{16, pack.VarintType}, pack.Varint(1016),
Damien Neild0b07492019-12-16 12:59:13 -0800138 pack.Tag{16, pack.EndGroupType},
139 }.Marshal(),
140 },
141 {
142 desc: "groups (field overridden)",
Damien Neild025c952020-02-02 00:53:34 -0800143 decodeTo: makeMessages(protobuild.Message{
144 "optionalgroup": protobuild.Message{
145 "a": 2,
Damien Neild0b07492019-12-16 12:59:13 -0800146 },
Damien Neild025c952020-02-02 00:53:34 -0800147 }, &testpb.TestAllTypes{}, &testpb.TestAllExtensions{}),
Damien Neild0b07492019-12-16 12:59:13 -0800148 wire: pack.Message{
149 pack.Tag{16, pack.StartGroupType},
150 pack.Tag{17, pack.VarintType}, pack.Varint(1),
151 pack.Tag{16, pack.EndGroupType},
152 pack.Tag{16, pack.StartGroupType},
153 pack.Tag{17, pack.VarintType}, pack.Varint(2),
154 pack.Tag{16, pack.EndGroupType},
155 }.Marshal(),
156 },
157 {
158 desc: "messages",
Damien Neild025c952020-02-02 00:53:34 -0800159 decodeTo: makeMessages(protobuild.Message{
160 "optional_nested_message": protobuild.Message{
161 "a": 42,
162 "corecursive": protobuild.Message{
163 "optional_int32": 43,
Damien Neild0b07492019-12-16 12:59:13 -0800164 },
165 },
Damien Neild025c952020-02-02 00:53:34 -0800166 }),
Damien Neild0b07492019-12-16 12:59:13 -0800167 wire: pack.Message{
168 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
169 pack.Tag{1, pack.VarintType}, pack.Varint(42),
170 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
171 pack.Tag{1, pack.VarintType}, pack.Varint(43),
172 }),
173 }),
174 }.Marshal(),
175 },
176 {
177 desc: "messages (split across multiple tags)",
Damien Neild025c952020-02-02 00:53:34 -0800178 decodeTo: makeMessages(protobuild.Message{
179 "optional_nested_message": protobuild.Message{
180 "a": 42,
181 "corecursive": protobuild.Message{
182 "optional_int32": 43,
Damien Neild0b07492019-12-16 12:59:13 -0800183 },
184 },
Damien Neild025c952020-02-02 00:53:34 -0800185 }),
Damien Neild0b07492019-12-16 12:59:13 -0800186 wire: pack.Message{
187 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
188 pack.Tag{1, pack.VarintType}, pack.Varint(42),
189 }),
190 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
191 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
192 pack.Tag{1, pack.VarintType}, pack.Varint(43),
193 }),
194 }),
195 }.Marshal(),
196 },
197 {
198 desc: "messages (field overridden)",
Damien Neild025c952020-02-02 00:53:34 -0800199 decodeTo: makeMessages(protobuild.Message{
200 "optional_nested_message": protobuild.Message{
201 "a": 2,
Damien Neild0b07492019-12-16 12:59:13 -0800202 },
Damien Neild025c952020-02-02 00:53:34 -0800203 }),
Damien Neild0b07492019-12-16 12:59:13 -0800204 wire: pack.Message{
205 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
206 pack.Tag{1, pack.VarintType}, pack.Varint(1),
207 }),
208 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
209 pack.Tag{1, pack.VarintType}, pack.Varint(2),
210 }),
211 }.Marshal(),
212 },
213 {
214 desc: "basic repeated types",
Damien Neild025c952020-02-02 00:53:34 -0800215 decodeTo: makeMessages(protobuild.Message{
216 "repeated_int32": []int32{1001, 2001},
217 "repeated_int64": []int64{1002, 2002},
218 "repeated_uint32": []uint32{1003, 2003},
219 "repeated_uint64": []uint64{1004, 2004},
220 "repeated_sint32": []int32{1005, 2005},
221 "repeated_sint64": []int64{1006, 2006},
222 "repeated_fixed32": []uint32{1007, 2007},
223 "repeated_fixed64": []uint64{1008, 2008},
224 "repeated_sfixed32": []int32{1009, 2009},
225 "repeated_sfixed64": []int64{1010, 2010},
226 "repeated_float": []float32{1011.5, 2011.5},
227 "repeated_double": []float64{1012.5, 2012.5},
228 "repeated_bool": []bool{true, false},
229 "repeated_string": []string{"foo", "bar"},
230 "repeated_bytes": []string{"FOO", "BAR"},
231 "repeated_nested_enum": []string{"FOO", "BAR"},
232 }),
Damien Neild0b07492019-12-16 12:59:13 -0800233 wire: pack.Message{
234 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
235 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
236 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
237 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
238 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
239 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
240 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
241 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
242 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
243 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
244 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
245 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
246 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
247 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
248 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
249 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
250 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
251 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
252 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
253 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
254 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
255 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
256 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
257 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
258 pack.Tag{43, pack.VarintType}, pack.Bool(true),
259 pack.Tag{43, pack.VarintType}, pack.Bool(false),
260 pack.Tag{44, pack.BytesType}, pack.String("foo"),
261 pack.Tag{44, pack.BytesType}, pack.String("bar"),
262 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
263 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
264 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
265 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
266 }.Marshal(),
267 },
268 {
269 desc: "basic repeated types (packed encoding)",
Damien Neild025c952020-02-02 00:53:34 -0800270 decodeTo: makeMessages(protobuild.Message{
271 "repeated_int32": []int32{1001, 2001},
272 "repeated_int64": []int64{1002, 2002},
273 "repeated_uint32": []uint32{1003, 2003},
274 "repeated_uint64": []uint64{1004, 2004},
275 "repeated_sint32": []int32{1005, 2005},
276 "repeated_sint64": []int64{1006, 2006},
277 "repeated_fixed32": []uint32{1007, 2007},
278 "repeated_fixed64": []uint64{1008, 2008},
279 "repeated_sfixed32": []int32{1009, 2009},
280 "repeated_sfixed64": []int64{1010, 2010},
281 "repeated_float": []float32{1011.5, 2011.5},
282 "repeated_double": []float64{1012.5, 2012.5},
283 "repeated_bool": []bool{true, false},
284 "repeated_nested_enum": []string{"FOO", "BAR"},
285 }),
Damien Neild0b07492019-12-16 12:59:13 -0800286 wire: pack.Message{
287 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
288 pack.Varint(1001), pack.Varint(2001),
289 },
290 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
291 pack.Varint(1002), pack.Varint(2002),
292 },
293 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
294 pack.Uvarint(1003), pack.Uvarint(2003),
295 },
296 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
297 pack.Uvarint(1004), pack.Uvarint(2004),
298 },
299 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
300 pack.Svarint(1005), pack.Svarint(2005),
301 },
302 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
303 pack.Svarint(1006), pack.Svarint(2006),
304 },
305 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
306 pack.Uint32(1007), pack.Uint32(2007),
307 },
308 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
309 pack.Uint64(1008), pack.Uint64(2008),
310 },
311 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
312 pack.Int32(1009), pack.Int32(2009),
313 },
314 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
315 pack.Int64(1010), pack.Int64(2010),
316 },
317 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
318 pack.Float32(1011.5), pack.Float32(2011.5),
319 },
320 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
321 pack.Float64(1012.5), pack.Float64(2012.5),
322 },
323 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
324 pack.Bool(true), pack.Bool(false),
325 },
326 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
327 pack.Varint(int(testpb.TestAllTypes_FOO)),
328 pack.Varint(int(testpb.TestAllTypes_BAR)),
329 },
330 }.Marshal(),
331 },
332 {
333 desc: "basic repeated types (zero-length packed encoding)",
Damien Neild025c952020-02-02 00:53:34 -0800334 decodeTo: makeMessages(protobuild.Message{
335 "repeated_int32": []int32{},
336 "repeated_int64": []int64{},
337 "repeated_uint32": []uint32{},
338 "repeated_uint64": []uint64{},
339 "repeated_sint32": []int32{},
340 "repeated_sint64": []int64{},
341 "repeated_fixed32": []uint32{},
342 "repeated_fixed64": []uint64{},
343 "repeated_sfixed32": []int32{},
344 "repeated_sfixed64": []int64{},
345 "repeated_float": []float32{},
346 "repeated_double": []float64{},
347 "repeated_bool": []bool{},
348 "repeated_nested_enum": []string{},
349 }),
Damien Neild0b07492019-12-16 12:59:13 -0800350 wire: pack.Message{
351 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{},
352 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{},
353 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{},
354 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{},
355 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{},
356 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{},
357 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{},
358 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{},
359 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{},
360 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{},
361 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{},
362 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{},
363 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{},
364 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{},
365 }.Marshal(),
366 },
367 {
368 desc: "packed repeated types",
Damien Neild025c952020-02-02 00:53:34 -0800369 decodeTo: makeMessages(protobuild.Message{
370 "packed_int32": []int32{1001, 2001},
371 "packed_int64": []int64{1002, 2002},
372 "packed_uint32": []uint32{1003, 2003},
373 "packed_uint64": []uint64{1004, 2004},
374 "packed_sint32": []int32{1005, 2005},
375 "packed_sint64": []int64{1006, 2006},
376 "packed_fixed32": []uint32{1007, 2007},
377 "packed_fixed64": []uint64{1008, 2008},
378 "packed_sfixed32": []int32{1009, 2009},
379 "packed_sfixed64": []int64{1010, 2010},
380 "packed_float": []float32{1011.5, 2011.5},
381 "packed_double": []float64{1012.5, 2012.5},
382 "packed_bool": []bool{true, false},
383 "packed_enum": []string{"FOREIGN_FOO", "FOREIGN_BAR"},
384 }, &testpb.TestPackedTypes{}, &testpb.TestPackedExtensions{}),
Damien Neild0b07492019-12-16 12:59:13 -0800385 wire: pack.Message{
386 pack.Tag{90, pack.BytesType}, pack.LengthPrefix{
387 pack.Varint(1001), pack.Varint(2001),
388 },
389 pack.Tag{91, pack.BytesType}, pack.LengthPrefix{
390 pack.Varint(1002), pack.Varint(2002),
391 },
392 pack.Tag{92, pack.BytesType}, pack.LengthPrefix{
393 pack.Uvarint(1003), pack.Uvarint(2003),
394 },
395 pack.Tag{93, pack.BytesType}, pack.LengthPrefix{
396 pack.Uvarint(1004), pack.Uvarint(2004),
397 },
398 pack.Tag{94, pack.BytesType}, pack.LengthPrefix{
399 pack.Svarint(1005), pack.Svarint(2005),
400 },
401 pack.Tag{95, pack.BytesType}, pack.LengthPrefix{
402 pack.Svarint(1006), pack.Svarint(2006),
403 },
404 pack.Tag{96, pack.BytesType}, pack.LengthPrefix{
405 pack.Uint32(1007), pack.Uint32(2007),
406 },
407 pack.Tag{97, pack.BytesType}, pack.LengthPrefix{
408 pack.Uint64(1008), pack.Uint64(2008),
409 },
410 pack.Tag{98, pack.BytesType}, pack.LengthPrefix{
411 pack.Int32(1009), pack.Int32(2009),
412 },
413 pack.Tag{99, pack.BytesType}, pack.LengthPrefix{
414 pack.Int64(1010), pack.Int64(2010),
415 },
416 pack.Tag{100, pack.BytesType}, pack.LengthPrefix{
417 pack.Float32(1011.5), pack.Float32(2011.5),
418 },
419 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{
420 pack.Float64(1012.5), pack.Float64(2012.5),
421 },
422 pack.Tag{102, pack.BytesType}, pack.LengthPrefix{
423 pack.Bool(true), pack.Bool(false),
424 },
425 pack.Tag{103, pack.BytesType}, pack.LengthPrefix{
426 pack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
427 pack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
428 },
429 }.Marshal(),
430 },
431 {
432 desc: "packed repeated types (zero length)",
Damien Neild025c952020-02-02 00:53:34 -0800433 decodeTo: makeMessages(protobuild.Message{
434 "packed_int32": []int32{},
435 "packed_int64": []int64{},
436 "packed_uint32": []uint32{},
437 "packed_uint64": []uint64{},
438 "packed_sint32": []int32{},
439 "packed_sint64": []int64{},
440 "packed_fixed32": []uint32{},
441 "packed_fixed64": []uint64{},
442 "packed_sfixed32": []int32{},
443 "packed_sfixed64": []int64{},
444 "packed_float": []float32{},
445 "packed_double": []float64{},
446 "packed_bool": []bool{},
447 "packed_enum": []string{},
448 }, &testpb.TestPackedTypes{}, &testpb.TestPackedExtensions{}),
Damien Neild0b07492019-12-16 12:59:13 -0800449 wire: pack.Message{
450 pack.Tag{90, pack.BytesType}, pack.LengthPrefix{},
451 pack.Tag{91, pack.BytesType}, pack.LengthPrefix{},
452 pack.Tag{92, pack.BytesType}, pack.LengthPrefix{},
453 pack.Tag{93, pack.BytesType}, pack.LengthPrefix{},
454 pack.Tag{94, pack.BytesType}, pack.LengthPrefix{},
455 pack.Tag{95, pack.BytesType}, pack.LengthPrefix{},
456 pack.Tag{96, pack.BytesType}, pack.LengthPrefix{},
457 pack.Tag{97, pack.BytesType}, pack.LengthPrefix{},
458 pack.Tag{98, pack.BytesType}, pack.LengthPrefix{},
459 pack.Tag{99, pack.BytesType}, pack.LengthPrefix{},
460 pack.Tag{100, pack.BytesType}, pack.LengthPrefix{},
461 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{},
462 pack.Tag{102, pack.BytesType}, pack.LengthPrefix{},
463 pack.Tag{103, pack.BytesType}, pack.LengthPrefix{},
464 }.Marshal(),
465 },
466 {
467 desc: "repeated messages",
Damien Neild025c952020-02-02 00:53:34 -0800468 decodeTo: makeMessages(protobuild.Message{
469 "repeated_nested_message": []protobuild.Message{
470 {"a": 1},
471 {},
472 {"a": 2},
473 },
474 }),
475 wire: pack.Message{
476 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
477 pack.Tag{1, pack.VarintType}, pack.Varint(1),
478 }),
479 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
480 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
481 pack.Tag{1, pack.VarintType}, pack.Varint(2),
482 }),
483 }.Marshal(),
484 },
485 {
486 desc: "repeated nil messages",
Damien Neild0b07492019-12-16 12:59:13 -0800487 decodeTo: []proto.Message{&testpb.TestAllTypes{
488 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
489 {A: proto.Int32(1)},
490 nil,
491 {A: proto.Int32(2)},
492 },
493 }, &test3pb.TestAllTypes{
494 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
495 {A: 1},
496 nil,
497 {A: 2},
498 },
499 }, build(
500 &testpb.TestAllExtensions{},
Damien Neild025c952020-02-02 00:53:34 -0800501 extend(testpb.E_RepeatedNestedMessage, []*testpb.TestAllExtensions_NestedMessage{
Damien Neild0b07492019-12-16 12:59:13 -0800502 {A: proto.Int32(1)},
503 nil,
504 {A: proto.Int32(2)},
505 }),
506 )},
507 wire: pack.Message{
508 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
509 pack.Tag{1, pack.VarintType}, pack.Varint(1),
510 }),
511 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
512 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
513 pack.Tag{1, pack.VarintType}, pack.Varint(2),
514 }),
515 }.Marshal(),
516 },
517 {
518 desc: "repeated groups",
Damien Neild025c952020-02-02 00:53:34 -0800519 decodeTo: makeMessages(protobuild.Message{
520 "repeatedgroup": []protobuild.Message{
521 {"a": 1017},
522 {},
523 {"a": 2017},
524 },
525 }, &testpb.TestAllTypes{}, &testpb.TestAllExtensions{}),
526 wire: pack.Message{
527 pack.Tag{46, pack.StartGroupType},
528 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
529 pack.Tag{46, pack.EndGroupType},
530 pack.Tag{46, pack.StartGroupType},
531 pack.Tag{46, pack.EndGroupType},
532 pack.Tag{46, pack.StartGroupType},
533 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
534 pack.Tag{46, pack.EndGroupType},
535 }.Marshal(),
536 },
537 {
538 desc: "repeated nil groups",
Damien Neild0b07492019-12-16 12:59:13 -0800539 decodeTo: []proto.Message{&testpb.TestAllTypes{
540 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
541 {A: proto.Int32(1017)},
542 nil,
543 {A: proto.Int32(2017)},
544 },
545 }, build(
546 &testpb.TestAllExtensions{},
Damien Neild025c952020-02-02 00:53:34 -0800547 extend(testpb.E_Repeatedgroup, []*testpb.RepeatedGroup{
Damien Neild0b07492019-12-16 12:59:13 -0800548 {A: proto.Int32(1017)},
549 nil,
550 {A: proto.Int32(2017)},
551 }),
552 )},
553 wire: pack.Message{
554 pack.Tag{46, pack.StartGroupType},
555 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
556 pack.Tag{46, pack.EndGroupType},
557 pack.Tag{46, pack.StartGroupType},
558 pack.Tag{46, pack.EndGroupType},
559 pack.Tag{46, pack.StartGroupType},
560 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
561 pack.Tag{46, pack.EndGroupType},
562 }.Marshal(),
563 },
564 {
565 desc: "maps",
Damien Neild025c952020-02-02 00:53:34 -0800566 decodeTo: makeMessages(protobuild.Message{
567 "map_int32_int32": map[int32]int32{1056: 1156, 2056: 2156},
568 "map_int64_int64": map[int64]int64{1057: 1157, 2057: 2157},
569 "map_uint32_uint32": map[uint32]uint32{1058: 1158, 2058: 2158},
570 "map_uint64_uint64": map[uint64]uint64{1059: 1159, 2059: 2159},
571 "map_sint32_sint32": map[int32]int32{1060: 1160, 2060: 2160},
572 "map_sint64_sint64": map[int64]int64{1061: 1161, 2061: 2161},
573 "map_fixed32_fixed32": map[uint32]uint32{1062: 1162, 2062: 2162},
574 "map_fixed64_fixed64": map[uint64]uint64{1063: 1163, 2063: 2163},
575 "map_sfixed32_sfixed32": map[int32]int32{1064: 1164, 2064: 2164},
576 "map_sfixed64_sfixed64": map[int64]int64{1065: 1165, 2065: 2165},
577 "map_int32_float": map[int32]float32{1066: 1166.5, 2066: 2166.5},
578 "map_int32_double": map[int32]float64{1067: 1167.5, 2067: 2167.5},
579 "map_bool_bool": map[bool]bool{true: false, false: true},
580 "map_string_string": map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
581 "map_string_bytes": map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
582 "map_string_nested_message": map[string]protobuild.Message{
583 "71.1.key": {"a": 1171},
584 "71.2.key": {"a": 2171},
Damien Neild0b07492019-12-16 12:59:13 -0800585 },
Damien Neild025c952020-02-02 00:53:34 -0800586 "map_string_nested_enum": map[string]string{"73.1.key": "FOO", "73.2.key": "BAR"},
587 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -0800588 wire: pack.Message{
589 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
590 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
591 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
592 }),
593 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
594 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
595 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
596 }),
597 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
598 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
599 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
600 }),
601 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
602 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
603 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
604 }),
605 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
606 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
607 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
608 }),
609 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
610 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
611 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
612 }),
613 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
614 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
615 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
616 }),
617 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
618 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
619 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
620 }),
621 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
622 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
623 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
624 }),
625 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
626 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
627 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
628 }),
629 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
630 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
631 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
632 }),
633 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
634 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
635 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
636 }),
637 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
638 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
639 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
640 }),
641 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
642 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
643 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
644 }),
645 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
646 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
647 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
648 }),
649 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
650 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
651 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
652 }),
653 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
654 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
655 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
656 }),
657 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
658 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
659 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
660 }),
661 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
662 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
663 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
664 }),
665 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
666 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
667 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
668 }),
669 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
670 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
671 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
672 }),
673 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
674 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
675 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
676 }),
677 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
678 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
679 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
680 }),
681 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
682 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
683 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
684 }),
685 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
686 pack.Tag{1, pack.VarintType}, pack.Bool(true),
687 pack.Tag{2, pack.VarintType}, pack.Bool(false),
688 }),
689 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
690 pack.Tag{1, pack.VarintType}, pack.Bool(false),
691 pack.Tag{2, pack.VarintType}, pack.Bool(true),
692 }),
693 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
694 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
695 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
696 }),
697 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
698 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
699 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
700 }),
701 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
702 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
703 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
704 }),
705 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
706 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
707 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
708 }),
709 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
710 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
711 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
712 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
713 }),
714 }),
715 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
716 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
717 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
718 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
719 }),
720 }),
721 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
722 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
723 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
724 }),
725 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
726 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
727 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
728 }),
729 }.Marshal(),
730 },
731 {
Damien Neil7e690b52019-12-18 09:35:01 -0800732 desc: "map with value before key",
Damien Neild025c952020-02-02 00:53:34 -0800733 decodeTo: makeMessages(protobuild.Message{
734 "map_int32_int32": map[int32]int32{1056: 1156},
735 "map_string_nested_message": map[string]protobuild.Message{
736 "71.1.key": {"a": 1171},
Damien Neil7e690b52019-12-18 09:35:01 -0800737 },
Damien Neild025c952020-02-02 00:53:34 -0800738 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neil7e690b52019-12-18 09:35:01 -0800739 wire: pack.Message{
740 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
741 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
742 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
743 }),
744 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
745 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
746 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
747 }),
748 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
749 }),
750 }.Marshal(),
751 },
752 {
753 desc: "map with repeated key and value",
Damien Neild025c952020-02-02 00:53:34 -0800754 decodeTo: makeMessages(protobuild.Message{
755 "map_int32_int32": map[int32]int32{1056: 1156},
756 "map_string_nested_message": map[string]protobuild.Message{
757 "71.1.key": {"a": 1171},
Damien Neil7e690b52019-12-18 09:35:01 -0800758 },
Damien Neild025c952020-02-02 00:53:34 -0800759 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neil7e690b52019-12-18 09:35:01 -0800760 wire: pack.Message{
761 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
762 pack.Tag{1, pack.VarintType}, pack.Varint(0),
763 pack.Tag{2, pack.VarintType}, pack.Varint(0),
764 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
765 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
766 }),
767 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
768 pack.Tag{1, pack.BytesType}, pack.String(0),
769 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
770 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
771 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
772 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
773 }),
774 }),
775 }.Marshal(),
776 },
777 {
Damien Neild0b07492019-12-16 12:59:13 -0800778 desc: "oneof (uint32)",
Damien Neild025c952020-02-02 00:53:34 -0800779 decodeTo: makeMessages(protobuild.Message{
780 "oneof_uint32": 1111,
781 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -0800782 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
783 },
784 {
785 desc: "oneof (message)",
Damien Neild025c952020-02-02 00:53:34 -0800786 decodeTo: makeMessages(protobuild.Message{
787 "oneof_nested_message": protobuild.Message{
788 "a": 1112,
789 },
790 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -0800791 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
792 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
793 })}.Marshal(),
794 },
795 {
796 desc: "oneof (empty message)",
Damien Neild025c952020-02-02 00:53:34 -0800797 decodeTo: makeMessages(protobuild.Message{
798 "oneof_nested_message": protobuild.Message{},
799 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -0800800 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
801 },
802 {
803 desc: "oneof (merged message)",
Damien Neild025c952020-02-02 00:53:34 -0800804 decodeTo: makeMessages(protobuild.Message{
805 "oneof_nested_message": protobuild.Message{
806 "a": 1,
807 "corecursive": protobuild.Message{
808 "optional_int32": 43,
Damien Neild0b07492019-12-16 12:59:13 -0800809 },
Damien Neild025c952020-02-02 00:53:34 -0800810 },
811 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -0800812 wire: pack.Message{
813 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
814 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
815 }),
816 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
817 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
818 pack.Tag{1, pack.VarintType}, pack.Varint(43),
819 }),
820 }),
821 }.Marshal(),
822 },
823 {
824 desc: "oneof (string)",
Damien Neild025c952020-02-02 00:53:34 -0800825 decodeTo: makeMessages(protobuild.Message{
826 "oneof_string": "1113",
827 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -0800828 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
829 },
830 {
831 desc: "oneof (bytes)",
Damien Neild025c952020-02-02 00:53:34 -0800832 decodeTo: makeMessages(protobuild.Message{
833 "oneof_bytes": "1114",
834 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -0800835 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
836 },
837 {
838 desc: "oneof (bool)",
Damien Neild025c952020-02-02 00:53:34 -0800839 decodeTo: makeMessages(protobuild.Message{
840 "oneof_bool": true,
841 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -0800842 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
843 },
844 {
845 desc: "oneof (uint64)",
Damien Neild025c952020-02-02 00:53:34 -0800846 decodeTo: makeMessages(protobuild.Message{
847 "oneof_uint64": 116,
848 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -0800849 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
850 },
851 {
852 desc: "oneof (float)",
Damien Neild025c952020-02-02 00:53:34 -0800853 decodeTo: makeMessages(protobuild.Message{
854 "oneof_float": 117.5,
855 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -0800856 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
857 },
858 {
859 desc: "oneof (double)",
Damien Neild025c952020-02-02 00:53:34 -0800860 decodeTo: makeMessages(protobuild.Message{
861 "oneof_double": 118.5,
862 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -0800863 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
864 },
865 {
866 desc: "oneof (enum)",
Damien Neild025c952020-02-02 00:53:34 -0800867 decodeTo: makeMessages(protobuild.Message{
868 "oneof_enum": "BAR",
869 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -0800870 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
871 },
872 {
873 desc: "oneof (zero)",
Damien Neild025c952020-02-02 00:53:34 -0800874 decodeTo: makeMessages(protobuild.Message{
875 "oneof_uint64": 0,
876 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -0800877 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
878 },
879 {
880 desc: "oneof (overridden value)",
Damien Neild025c952020-02-02 00:53:34 -0800881 decodeTo: makeMessages(protobuild.Message{
882 "oneof_uint64": 2,
883 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -0800884 wire: pack.Message{
885 pack.Tag{111, pack.VarintType}, pack.Varint(1),
886 pack.Tag{116, pack.VarintType}, pack.Varint(2),
887 }.Marshal(),
888 },
889 // TODO: More unknown field tests for ordering, repeated fields, etc.
890 //
891 // It is currently impossible to produce results that the v1 Equal
892 // considers equivalent to those of the v1 decoder. Figure out if
893 // that's a problem or not.
894 {
Damien Neil1887ff72020-01-29 16:40:05 -0800895 desc: "unknown fields",
896 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -0800897 decodeTo: makeMessages(protobuild.Message{
898 protobuild.Unknown: pack.Message{
Damien Neild0b07492019-12-16 12:59:13 -0800899 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
Damien Neild025c952020-02-02 00:53:34 -0800900 }.Marshal(),
901 }),
Damien Neild0b07492019-12-16 12:59:13 -0800902 wire: pack.Message{
903 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
904 }.Marshal(),
905 },
906 {
Damien Neila60e7092020-01-28 14:53:44 -0800907 desc: "discarded unknown fields",
908 unmarshalOptions: proto.UnmarshalOptions{
909 DiscardUnknown: true,
910 },
Damien Neild025c952020-02-02 00:53:34 -0800911 decodeTo: makeMessages(protobuild.Message{}),
Damien Neila60e7092020-01-28 14:53:44 -0800912 wire: pack.Message{
913 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
914 }.Marshal(),
915 },
916 {
Damien Neild0b07492019-12-16 12:59:13 -0800917 desc: "field type mismatch",
Damien Neild025c952020-02-02 00:53:34 -0800918 decodeTo: makeMessages(protobuild.Message{
919 protobuild.Unknown: pack.Message{
Damien Neild0b07492019-12-16 12:59:13 -0800920 pack.Tag{1, pack.BytesType}, pack.String("string"),
Damien Neild025c952020-02-02 00:53:34 -0800921 }.Marshal(),
922 }),
Damien Neild0b07492019-12-16 12:59:13 -0800923 wire: pack.Message{
924 pack.Tag{1, pack.BytesType}, pack.String("string"),
925 }.Marshal(),
926 },
927 {
928 desc: "map field element mismatch",
Damien Neild025c952020-02-02 00:53:34 -0800929 decodeTo: makeMessages(protobuild.Message{
930 "map_int32_int32": map[int32]int32{1: 0},
931 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -0800932 wire: pack.Message{
933 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
934 pack.Tag{1, pack.VarintType}, pack.Varint(1),
935 pack.Tag{2, pack.BytesType}, pack.String("string"),
936 }),
937 }.Marshal(),
938 },
939 {
Damien Neilc600d6c2020-01-21 15:00:33 -0800940 desc: "required field in nil message unset",
941 checkFastInit: true,
942 partial: true,
943 decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
Damien Neild0b07492019-12-16 12:59:13 -0800944 },
945 {
Damien Neilc600d6c2020-01-21 15:00:33 -0800946 desc: "required int32 unset",
947 checkFastInit: true,
948 partial: true,
Damien Neild025c952020-02-02 00:53:34 -0800949 decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Int32{}),
Damien Neild0b07492019-12-16 12:59:13 -0800950 },
951 {
Damien Neilc600d6c2020-01-21 15:00:33 -0800952 desc: "required int32 set",
953 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -0800954 decodeTo: makeMessages(protobuild.Message{
955 "v": 1,
956 }, &requiredpb.Int32{}),
Damien Neild0b07492019-12-16 12:59:13 -0800957 wire: pack.Message{
958 pack.Tag{1, pack.VarintType}, pack.Varint(1),
959 }.Marshal(),
960 },
961 {
Damien Neilc600d6c2020-01-21 15:00:33 -0800962 desc: "required fixed32 unset",
963 checkFastInit: true,
964 partial: true,
Damien Neild025c952020-02-02 00:53:34 -0800965 decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Fixed32{}),
Damien Neil6635e7d2020-01-15 15:08:57 -0800966 },
967 {
Damien Neilc600d6c2020-01-21 15:00:33 -0800968 desc: "required fixed32 set",
969 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -0800970 decodeTo: makeMessages(protobuild.Message{
971 "v": 1,
972 }, &requiredpb.Fixed32{}),
Damien Neil6635e7d2020-01-15 15:08:57 -0800973 wire: pack.Message{
974 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1),
975 }.Marshal(),
976 },
977 {
Damien Neilc600d6c2020-01-21 15:00:33 -0800978 desc: "required fixed64 unset",
979 checkFastInit: true,
980 partial: true,
Damien Neild025c952020-02-02 00:53:34 -0800981 decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Fixed64{}),
Damien Neil6635e7d2020-01-15 15:08:57 -0800982 },
983 {
Damien Neilc600d6c2020-01-21 15:00:33 -0800984 desc: "required fixed64 set",
985 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -0800986 decodeTo: makeMessages(protobuild.Message{
987 "v": 1,
988 }, &requiredpb.Fixed64{}),
Damien Neil6635e7d2020-01-15 15:08:57 -0800989 wire: pack.Message{
990 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1),
991 }.Marshal(),
992 },
993 {
Damien Neilc600d6c2020-01-21 15:00:33 -0800994 desc: "required bytes unset",
995 checkFastInit: true,
996 partial: true,
Damien Neild025c952020-02-02 00:53:34 -0800997 decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Bytes{}),
Damien Neil6635e7d2020-01-15 15:08:57 -0800998 },
999 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001000 desc: "required bytes set",
1001 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001002 decodeTo: makeMessages(protobuild.Message{
1003 "v": "",
1004 }, &requiredpb.Bytes{}),
Damien Neil6635e7d2020-01-15 15:08:57 -08001005 wire: pack.Message{
1006 pack.Tag{1, pack.BytesType}, pack.Bytes(nil),
1007 }.Marshal(),
1008 },
1009 {
Damien Neilf9d4fdf2020-02-07 11:42:45 -08001010 desc: "required message unset",
1011 checkFastInit: true,
1012 partial: true,
1013 decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Message{}),
1014 },
1015 {
1016 desc: "required message set",
1017 checkFastInit: true,
1018 decodeTo: makeMessages(protobuild.Message{
1019 "v": protobuild.Message{},
1020 }, &requiredpb.Message{}),
1021 wire: pack.Message{
1022 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1023 }.Marshal(),
1024 },
1025 {
1026 desc: "required group unset",
1027 checkFastInit: true,
1028 partial: true,
1029 decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Group{}),
1030 },
1031 {
1032 desc: "required group set",
1033 checkFastInit: true,
1034 decodeTo: makeMessages(protobuild.Message{
1035 "group": protobuild.Message{},
1036 }, &requiredpb.Group{}),
1037 wire: pack.Message{
1038 pack.Tag{1, pack.StartGroupType},
1039 pack.Tag{1, pack.EndGroupType},
1040 }.Marshal(),
1041 },
1042 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001043 desc: "required field with incompatible wire type",
1044 checkFastInit: true,
1045 partial: true,
Damien Neilb0c26f12019-12-16 09:37:59 -08001046 decodeTo: []proto.Message{build(
1047 &testpb.TestRequired{},
1048 unknown(pack.Message{
1049 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2),
1050 }.Marshal()),
1051 )},
1052 wire: pack.Message{
1053 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2),
1054 }.Marshal(),
1055 },
1056 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001057 desc: "required field in optional message unset",
1058 checkFastInit: true,
1059 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001060 decodeTo: makeMessages(protobuild.Message{
1061 "optional_message": protobuild.Message{},
1062 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001063 wire: pack.Message{
1064 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1065 }.Marshal(),
1066 },
1067 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001068 desc: "required field in optional message set",
1069 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001070 decodeTo: makeMessages(protobuild.Message{
1071 "optional_message": protobuild.Message{
1072 "required_field": 1,
Damien Neild0b07492019-12-16 12:59:13 -08001073 },
Damien Neild025c952020-02-02 00:53:34 -08001074 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001075 wire: pack.Message{
1076 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1077 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1078 }),
1079 }.Marshal(),
1080 },
1081 {
Damien Neilcadb4ab2020-02-03 16:17:31 -08001082 desc: "required field in optional message set (split across multiple tags)",
1083 checkFastInit: false, // fast init checks don't handle split messages
1084 nocheckValidInit: true, // validation doesn't either
Damien Neild025c952020-02-02 00:53:34 -08001085 decodeTo: makeMessages(protobuild.Message{
1086 "optional_message": protobuild.Message{
1087 "required_field": 1,
Damien Neild0b07492019-12-16 12:59:13 -08001088 },
Damien Neild025c952020-02-02 00:53:34 -08001089 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001090 wire: pack.Message{
1091 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1092 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1093 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1094 }),
1095 }.Marshal(),
1096 },
1097 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001098 desc: "required field in repeated message unset",
1099 checkFastInit: true,
1100 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001101 decodeTo: makeMessages(protobuild.Message{
1102 "repeated_message": []protobuild.Message{
1103 {"required_field": 1},
Damien Neild0b07492019-12-16 12:59:13 -08001104 {},
1105 },
Damien Neild025c952020-02-02 00:53:34 -08001106 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001107 wire: pack.Message{
1108 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1109 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1110 }),
1111 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1112 }.Marshal(),
1113 },
1114 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001115 desc: "required field in repeated message set",
1116 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001117 decodeTo: makeMessages(protobuild.Message{
1118 "repeated_message": []protobuild.Message{
1119 {"required_field": 1},
1120 {"required_field": 2},
Damien Neild0b07492019-12-16 12:59:13 -08001121 },
Damien Neild025c952020-02-02 00:53:34 -08001122 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001123 wire: pack.Message{
1124 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1125 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1126 }),
1127 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1128 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1129 }),
1130 }.Marshal(),
1131 },
1132 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001133 desc: "required field in map message unset",
1134 checkFastInit: true,
1135 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001136 decodeTo: makeMessages(protobuild.Message{
1137 "map_message": map[int32]protobuild.Message{
1138 1: {"required_field": 1},
Damien Neild0b07492019-12-16 12:59:13 -08001139 2: {},
1140 },
Damien Neild025c952020-02-02 00:53:34 -08001141 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001142 wire: pack.Message{
1143 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1144 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1145 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1146 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1147 }),
1148 }),
1149 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1150 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1151 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1152 }),
1153 }.Marshal(),
1154 },
1155 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001156 desc: "required field in absent map message value",
1157 checkFastInit: true,
1158 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001159 decodeTo: makeMessages(protobuild.Message{
1160 "map_message": map[int32]protobuild.Message{
Damien Neil54a0a042020-01-08 17:53:16 -08001161 2: {},
1162 },
Damien Neild025c952020-02-02 00:53:34 -08001163 }, &testpb.TestRequiredForeign{}),
Damien Neil54a0a042020-01-08 17:53:16 -08001164 wire: pack.Message{
1165 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1166 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1167 }),
1168 }.Marshal(),
1169 },
1170 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001171 desc: "required field in map message set",
1172 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001173 decodeTo: makeMessages(protobuild.Message{
1174 "map_message": map[int32]protobuild.Message{
1175 1: {"required_field": 1},
1176 2: {"required_field": 2},
Damien Neild0b07492019-12-16 12:59:13 -08001177 },
Damien Neild025c952020-02-02 00:53:34 -08001178 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001179 wire: pack.Message{
1180 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1181 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1182 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1183 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1184 }),
1185 }),
1186 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1187 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1188 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1189 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1190 }),
1191 }),
1192 }.Marshal(),
1193 },
1194 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001195 desc: "required field in optional group unset",
1196 checkFastInit: true,
1197 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001198 decodeTo: makeMessages(protobuild.Message{
1199 "optionalgroup": protobuild.Message{},
1200 }, &testpb.TestRequiredGroupFields{}),
Damien Neild0b07492019-12-16 12:59:13 -08001201 wire: pack.Message{
1202 pack.Tag{1, pack.StartGroupType},
1203 pack.Tag{1, pack.EndGroupType},
1204 }.Marshal(),
1205 },
1206 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001207 desc: "required field in optional group set",
1208 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001209 decodeTo: makeMessages(protobuild.Message{
1210 "optionalgroup": protobuild.Message{
1211 "a": 1,
Damien Neild0b07492019-12-16 12:59:13 -08001212 },
Damien Neild025c952020-02-02 00:53:34 -08001213 }, &testpb.TestRequiredGroupFields{}),
Damien Neild0b07492019-12-16 12:59:13 -08001214 wire: pack.Message{
1215 pack.Tag{1, pack.StartGroupType},
1216 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1217 pack.Tag{1, pack.EndGroupType},
1218 }.Marshal(),
1219 },
1220 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001221 desc: "required field in repeated group unset",
1222 checkFastInit: true,
1223 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001224 decodeTo: makeMessages(protobuild.Message{
1225 "repeatedgroup": []protobuild.Message{
1226 {"a": 1},
Damien Neild0b07492019-12-16 12:59:13 -08001227 {},
1228 },
Damien Neild025c952020-02-02 00:53:34 -08001229 }, &testpb.TestRequiredGroupFields{}),
Damien Neild0b07492019-12-16 12:59:13 -08001230 wire: pack.Message{
1231 pack.Tag{3, pack.StartGroupType},
1232 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1233 pack.Tag{3, pack.EndGroupType},
1234 pack.Tag{3, pack.StartGroupType},
1235 pack.Tag{3, pack.EndGroupType},
1236 }.Marshal(),
1237 },
1238 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001239 desc: "required field in repeated group set",
1240 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001241 decodeTo: makeMessages(protobuild.Message{
1242 "repeatedgroup": []protobuild.Message{
1243 {"a": 1},
1244 {"a": 2},
Damien Neild0b07492019-12-16 12:59:13 -08001245 },
Damien Neild025c952020-02-02 00:53:34 -08001246 }, &testpb.TestRequiredGroupFields{}),
Damien Neild0b07492019-12-16 12:59:13 -08001247 wire: pack.Message{
1248 pack.Tag{3, pack.StartGroupType},
1249 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1250 pack.Tag{3, pack.EndGroupType},
1251 pack.Tag{3, pack.StartGroupType},
1252 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1253 pack.Tag{3, pack.EndGroupType},
1254 }.Marshal(),
1255 },
1256 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001257 desc: "required field in oneof message unset",
1258 checkFastInit: true,
1259 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001260 decodeTo: makeMessages(protobuild.Message{
1261 "oneof_message": protobuild.Message{},
1262 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001263 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1264 },
1265 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001266 desc: "required field in oneof message set",
1267 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001268 decodeTo: makeMessages(protobuild.Message{
1269 "oneof_message": protobuild.Message{
1270 "required_field": 1,
1271 },
1272 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001273 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
1274 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1275 })}.Marshal(),
1276 },
1277 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001278 desc: "required field in extension message unset",
1279 checkFastInit: true,
1280 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001281 decodeTo: makeMessages(protobuild.Message{
1282 "single": protobuild.Message{},
1283 }, &testpb.TestAllExtensions{}),
Damien Neild0b07492019-12-16 12:59:13 -08001284 wire: pack.Message{
1285 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1286 }.Marshal(),
1287 },
1288 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001289 desc: "required field in extension message set",
1290 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001291 decodeTo: makeMessages(protobuild.Message{
1292 "single": protobuild.Message{
1293 "required_field": 1,
1294 },
1295 }, &testpb.TestAllExtensions{}),
Damien Neild0b07492019-12-16 12:59:13 -08001296 wire: pack.Message{
1297 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1298 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1299 }),
1300 }.Marshal(),
1301 },
1302 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001303 desc: "required field in repeated extension message unset",
1304 checkFastInit: true,
1305 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001306 decodeTo: makeMessages(protobuild.Message{
1307 "multi": []protobuild.Message{
1308 {"required_field": 1},
Damien Neild0b07492019-12-16 12:59:13 -08001309 {},
Damien Neild025c952020-02-02 00:53:34 -08001310 },
1311 }, &testpb.TestAllExtensions{}),
Damien Neild0b07492019-12-16 12:59:13 -08001312 wire: pack.Message{
1313 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1314 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1315 }),
1316 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1317 }.Marshal(),
1318 },
1319 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001320 desc: "required field in repeated extension message set",
1321 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001322 decodeTo: makeMessages(protobuild.Message{
1323 "multi": []protobuild.Message{
1324 {"required_field": 1},
1325 {"required_field": 2},
1326 },
1327 }, &testpb.TestAllExtensions{}),
Damien Neild0b07492019-12-16 12:59:13 -08001328 wire: pack.Message{
1329 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1330 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1331 }),
1332 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1333 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1334 }),
1335 }.Marshal(),
1336 },
1337 {
1338 desc: "nil messages",
1339 decodeTo: []proto.Message{
1340 (*testpb.TestAllTypes)(nil),
1341 (*test3pb.TestAllTypes)(nil),
1342 (*testpb.TestAllExtensions)(nil),
1343 },
1344 },
1345 {
1346 desc: "legacy",
1347 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001348 decodeTo: makeMessages(protobuild.Message{
1349 "f1": protobuild.Message{
1350 "optional_int32": 1,
1351 "optional_child_enum": "ALPHA",
1352 "optional_child_message": protobuild.Message{
1353 "f1": "x",
1354 },
1355 "optionalgroup": protobuild.Message{
1356 "f1": "x",
1357 },
1358 "repeated_child_message": []protobuild.Message{
1359 {"f1": "x"},
1360 },
1361 "repeatedgroup": []protobuild.Message{
1362 {"f1": "x"},
1363 },
1364 "map_bool_child_message": map[bool]protobuild.Message{
1365 true: {"f1": "x"},
1366 },
1367 "oneof_child_message": protobuild.Message{
1368 "f1": "x",
Damien Neild0b07492019-12-16 12:59:13 -08001369 },
1370 },
Damien Neild025c952020-02-02 00:53:34 -08001371 }, &legacypb.Legacy{}),
Damien Neild0b07492019-12-16 12:59:13 -08001372 wire: pack.Message{
1373 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1374 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1375 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1376 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1377 pack.Tag{1, pack.BytesType}, pack.String("x"),
1378 }),
1379 pack.Tag{120, pack.StartGroupType},
1380 pack.Tag{1, pack.BytesType}, pack.String("x"),
1381 pack.Tag{120, pack.EndGroupType},
1382 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1383 pack.Tag{1, pack.BytesType}, pack.String("x"),
1384 }),
1385 pack.Tag{520, pack.StartGroupType},
1386 pack.Tag{1, pack.BytesType}, pack.String("x"),
1387 pack.Tag{520, pack.EndGroupType},
1388 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1389 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1390 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1391 pack.Tag{1, pack.BytesType}, pack.String("x"),
1392 }),
1393 }),
1394 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1395 pack.Tag{1, pack.BytesType}, pack.String("x"),
1396 }),
1397 }),
1398 }.Marshal(),
Damien Neilb0c26f12019-12-16 09:37:59 -08001399 validationStatus: impl.ValidationUnknown,
Damien Neild0b07492019-12-16 12:59:13 -08001400 },
1401 {
1402 desc: "first reserved field number",
Damien Neild025c952020-02-02 00:53:34 -08001403 decodeTo: makeMessages(protobuild.Message{
1404 protobuild.Unknown: pack.Message{
Damien Neild0b07492019-12-16 12:59:13 -08001405 pack.Tag{pack.FirstReservedNumber, pack.VarintType}, pack.Varint(1004),
Damien Neild025c952020-02-02 00:53:34 -08001406 }.Marshal(),
1407 }),
Damien Neild0b07492019-12-16 12:59:13 -08001408 wire: pack.Message{
1409 pack.Tag{pack.FirstReservedNumber, pack.VarintType}, pack.Varint(1004),
1410 }.Marshal(),
1411 },
1412 {
1413 desc: "last reserved field number",
Damien Neild025c952020-02-02 00:53:34 -08001414 decodeTo: makeMessages(protobuild.Message{
1415 protobuild.Unknown: pack.Message{
Damien Neild0b07492019-12-16 12:59:13 -08001416 pack.Tag{pack.LastReservedNumber, pack.VarintType}, pack.Varint(1005),
Damien Neild025c952020-02-02 00:53:34 -08001417 }.Marshal(),
1418 }),
Damien Neild0b07492019-12-16 12:59:13 -08001419 wire: pack.Message{
1420 pack.Tag{pack.LastReservedNumber, pack.VarintType}, pack.Varint(1005),
1421 }.Marshal(),
1422 },
Damien Neila60e7092020-01-28 14:53:44 -08001423 {
1424 desc: "nested unknown extension",
1425 unmarshalOptions: proto.UnmarshalOptions{
1426 DiscardUnknown: true,
Damien Neil1c33e112020-02-04 12:58:17 -08001427 Resolver: filterResolver{
1428 filter: func(name protoreflect.FullName) bool {
1429 switch name.Name() {
1430 case "optional_nested_message",
1431 "optional_int32":
1432 return true
1433 }
1434 return false
1435 },
1436 resolver: protoregistry.GlobalTypes,
1437 },
Damien Neila60e7092020-01-28 14:53:44 -08001438 },
Damien Neild025c952020-02-02 00:53:34 -08001439 decodeTo: makeMessages(protobuild.Message{
1440 "optional_nested_message": protobuild.Message{
1441 "corecursive": protobuild.Message{
1442 "optional_nested_message": protobuild.Message{
1443 "corecursive": protobuild.Message{
1444 "optional_int32": 42,
1445 },
1446 },
1447 },
1448 },
1449 }, &testpb.TestAllExtensions{}),
Damien Neila60e7092020-01-28 14:53:44 -08001450 wire: pack.Message{
1451 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1452 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1453 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1454 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1455 pack.Tag{1, pack.VarintType}, pack.Varint(42),
1456 pack.Tag{2, pack.VarintType}, pack.Varint(43),
1457 }),
1458 }),
1459 }),
1460 }),
1461 }.Marshal(),
1462 },
Damien Neild0b07492019-12-16 12:59:13 -08001463}
1464
1465var testInvalidMessages = []testProto{
1466 {
1467 desc: "invalid UTF-8 in optional string field",
Damien Neild025c952020-02-02 00:53:34 -08001468 decodeTo: makeMessages(protobuild.Message{
1469 "optional_string": "abc\xff",
1470 }, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -08001471 wire: pack.Message{
1472 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1473 }.Marshal(),
1474 },
1475 {
1476 desc: "invalid UTF-8 in repeated string field",
Damien Neild025c952020-02-02 00:53:34 -08001477 decodeTo: makeMessages(protobuild.Message{
1478 "repeated_string": []string{"foo", "abc\xff"},
1479 }, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -08001480 wire: pack.Message{
1481 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1482 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1483 }.Marshal(),
1484 },
1485 {
1486 desc: "invalid UTF-8 in nested message",
Damien Neild025c952020-02-02 00:53:34 -08001487 decodeTo: makeMessages(protobuild.Message{
1488 "optional_nested_message": protobuild.Message{
1489 "corecursive": protobuild.Message{
1490 "optional_string": "abc\xff",
Damien Neild0b07492019-12-16 12:59:13 -08001491 },
1492 },
Damien Neild025c952020-02-02 00:53:34 -08001493 }, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -08001494 wire: pack.Message{
1495 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1496 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1497 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1498 }),
1499 }),
1500 }.Marshal(),
1501 },
1502 {
1503 desc: "invalid UTF-8 in oneof field",
Damien Neild025c952020-02-02 00:53:34 -08001504 decodeTo: makeMessages(protobuild.Message{
1505 "oneof_string": "abc\xff",
1506 }, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -08001507 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1508 },
1509 {
1510 desc: "invalid UTF-8 in map key",
Damien Neild025c952020-02-02 00:53:34 -08001511 decodeTo: makeMessages(protobuild.Message{
1512 "map_string_string": map[string]string{"key\xff": "val"},
1513 }, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -08001514 wire: pack.Message{
1515 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1516 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1517 pack.Tag{2, pack.BytesType}, pack.String("val"),
1518 }),
1519 }.Marshal(),
1520 },
1521 {
1522 desc: "invalid UTF-8 in map value",
Damien Neild025c952020-02-02 00:53:34 -08001523 decodeTo: makeMessages(protobuild.Message{
1524 "map_string_string": map[string]string{"key": "val\xff"},
1525 }, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -08001526 wire: pack.Message{
1527 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1528 pack.Tag{1, pack.BytesType}, pack.String("key"),
1529 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1530 }),
1531 }.Marshal(),
1532 },
1533 {
Damien Neilb0c26f12019-12-16 09:37:59 -08001534 desc: "invalid field number zero",
1535 decodeTo: []proto.Message{
1536 (*testpb.TestAllTypes)(nil),
1537 (*testpb.TestAllExtensions)(nil),
1538 },
Damien Neild0b07492019-12-16 12:59:13 -08001539 wire: pack.Message{
1540 pack.Tag{pack.MinValidNumber - 1, pack.VarintType}, pack.Varint(1001),
1541 }.Marshal(),
1542 },
1543 {
Damien Neilb0c26f12019-12-16 09:37:59 -08001544 desc: "invalid field numbers zero and one",
1545 decodeTo: []proto.Message{
1546 (*testpb.TestAllTypes)(nil),
1547 (*testpb.TestAllExtensions)(nil),
1548 },
Damien Neild0b07492019-12-16 12:59:13 -08001549 wire: pack.Message{
1550 pack.Tag{pack.MinValidNumber - 1, pack.VarintType}, pack.Varint(1002),
1551 pack.Tag{pack.MinValidNumber, pack.VarintType}, pack.Varint(1003),
1552 }.Marshal(),
1553 },
1554 {
Damien Neilb0c26f12019-12-16 09:37:59 -08001555 desc: "invalid field numbers max and max+1",
1556 decodeTo: []proto.Message{
1557 (*testpb.TestAllTypes)(nil),
1558 (*testpb.TestAllExtensions)(nil),
1559 },
Damien Neild0b07492019-12-16 12:59:13 -08001560 wire: pack.Message{
1561 pack.Tag{pack.MaxValidNumber, pack.VarintType}, pack.Varint(1006),
1562 pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(1007),
1563 }.Marshal(),
1564 },
1565 {
Damien Neilb0c26f12019-12-16 09:37:59 -08001566 desc: "invalid field number max+1",
1567 decodeTo: []proto.Message{
1568 (*testpb.TestAllTypes)(nil),
1569 (*testpb.TestAllExtensions)(nil),
1570 },
Damien Neild0b07492019-12-16 12:59:13 -08001571 wire: pack.Message{
1572 pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(1008),
1573 }.Marshal(),
1574 },
Damien Neilf2427c02019-12-20 09:43:20 -08001575 {
Damien Neila522d5f2020-01-27 21:50:47 -08001576 desc: "invalid field number wraps int32",
1577 decodeTo: []proto.Message{
1578 (*testpb.TestAllTypes)(nil),
1579 (*testpb.TestAllExtensions)(nil),
1580 },
1581 wire: pack.Message{
1582 pack.Varint(2234993595104), pack.Varint(0),
1583 }.Marshal(),
1584 },
1585 {
Damien Neilf2427c02019-12-20 09:43:20 -08001586 desc: "invalid field number in map",
1587 decodeTo: []proto.Message{(*testpb.TestAllTypes)(nil)},
1588 wire: pack.Message{
1589 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
1590 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
1591 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
1592 pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(0),
1593 }),
1594 }.Marshal(),
1595 },
Damien Neilb0c26f12019-12-16 09:37:59 -08001596 {
1597 desc: "invalid tag varint",
1598 decodeTo: []proto.Message{
1599 (*testpb.TestAllTypes)(nil),
1600 (*testpb.TestAllExtensions)(nil),
1601 },
1602 wire: []byte{0xff},
1603 },
1604 {
1605 desc: "field number too small",
1606 decodeTo: []proto.Message{
1607 (*testpb.TestAllTypes)(nil),
1608 (*testpb.TestAllExtensions)(nil),
1609 },
1610 wire: pack.Message{
1611 pack.Tag{0, pack.VarintType}, pack.Varint(0),
1612 }.Marshal(),
1613 },
1614 {
1615 desc: "field number too large",
1616 decodeTo: []proto.Message{
1617 (*testpb.TestAllTypes)(nil),
1618 (*testpb.TestAllExtensions)(nil),
1619 },
1620 wire: pack.Message{
1621 pack.Tag{wire.MaxValidNumber + 1, pack.VarintType}, pack.Varint(0),
1622 }.Marshal(),
1623 },
1624 {
1625 desc: "invalid tag varint in message field",
1626 decodeTo: []proto.Message{
1627 (*testpb.TestAllTypes)(nil),
1628 (*testpb.TestAllExtensions)(nil),
1629 },
1630 wire: pack.Message{
1631 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1632 pack.Raw{0xff},
1633 }),
1634 }.Marshal(),
1635 },
1636 {
1637 desc: "invalid tag varint in repeated message field",
1638 decodeTo: []proto.Message{
1639 (*testpb.TestAllTypes)(nil),
1640 (*testpb.TestAllExtensions)(nil),
1641 },
1642 wire: pack.Message{
1643 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
1644 pack.Raw{0xff},
1645 }),
1646 }.Marshal(),
1647 },
1648 {
1649 desc: "invalid varint in group field",
1650 decodeTo: []proto.Message{
1651 (*testpb.TestAllTypes)(nil),
1652 (*testpb.TestAllExtensions)(nil),
1653 },
1654 wire: pack.Message{
1655 pack.Tag{16, pack.StartGroupType},
1656 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1657 pack.Raw{0xff},
1658 }),
1659 pack.Tag{16, pack.EndGroupType},
1660 }.Marshal(),
1661 },
1662 {
1663 desc: "invalid varint in repeated group field",
1664 decodeTo: []proto.Message{
1665 (*testpb.TestAllTypes)(nil),
1666 (*testpb.TestAllExtensions)(nil),
1667 },
1668 wire: pack.Message{
1669 pack.Tag{46, pack.StartGroupType},
1670 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1671 pack.Raw{0xff},
1672 }),
1673 pack.Tag{46, pack.EndGroupType},
1674 }.Marshal(),
1675 },
1676 {
1677 desc: "unterminated repeated group field",
1678 decodeTo: []proto.Message{
1679 (*testpb.TestAllTypes)(nil),
1680 (*testpb.TestAllExtensions)(nil),
1681 },
1682 wire: pack.Message{
1683 pack.Tag{46, pack.StartGroupType},
1684 }.Marshal(),
1685 },
1686 {
1687 desc: "invalid tag varint in map item",
1688 decodeTo: []proto.Message{
1689 (*testpb.TestAllTypes)(nil),
1690 },
1691 wire: pack.Message{
1692 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
1693 pack.Tag{1, pack.VarintType}, pack.Varint(0),
1694 pack.Tag{2, pack.VarintType}, pack.Varint(0),
1695 pack.Raw{0xff},
1696 }),
1697 }.Marshal(),
1698 },
1699 {
1700 desc: "invalid tag varint in map message value",
1701 decodeTo: []proto.Message{
1702 (*testpb.TestAllTypes)(nil),
1703 },
1704 wire: pack.Message{
1705 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
1706 pack.Tag{1, pack.VarintType}, pack.Varint(0),
1707 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1708 pack.Raw{0xff},
1709 }),
1710 }),
1711 }.Marshal(),
1712 },
1713 {
1714 desc: "invalid packed int32 field",
1715 decodeTo: []proto.Message{
1716 (*testpb.TestAllTypes)(nil),
1717 (*testpb.TestAllExtensions)(nil),
1718 },
1719 wire: pack.Message{
1720 pack.Tag{31, pack.BytesType}, pack.Bytes{0xff},
1721 }.Marshal(),
1722 },
1723 {
1724 desc: "invalid packed int64 field",
1725 decodeTo: []proto.Message{
1726 (*testpb.TestAllTypes)(nil),
1727 (*testpb.TestAllExtensions)(nil),
1728 },
1729 wire: pack.Message{
1730 pack.Tag{32, pack.BytesType}, pack.Bytes{0xff},
1731 }.Marshal(),
1732 },
1733 {
1734 desc: "invalid packed uint32 field",
1735 decodeTo: []proto.Message{
1736 (*testpb.TestAllTypes)(nil),
1737 (*testpb.TestAllExtensions)(nil),
1738 },
1739 wire: pack.Message{
1740 pack.Tag{33, pack.BytesType}, pack.Bytes{0xff},
1741 }.Marshal(),
1742 },
1743 {
1744 desc: "invalid packed uint64 field",
1745 decodeTo: []proto.Message{
1746 (*testpb.TestAllTypes)(nil),
1747 (*testpb.TestAllExtensions)(nil),
1748 },
1749 wire: pack.Message{
1750 pack.Tag{34, pack.BytesType}, pack.Bytes{0xff},
1751 }.Marshal(),
1752 },
1753 {
1754 desc: "invalid packed sint32 field",
1755 decodeTo: []proto.Message{
1756 (*testpb.TestAllTypes)(nil),
1757 (*testpb.TestAllExtensions)(nil),
1758 },
1759 wire: pack.Message{
1760 pack.Tag{35, pack.BytesType}, pack.Bytes{0xff},
1761 }.Marshal(),
1762 },
1763 {
1764 desc: "invalid packed sint64 field",
1765 decodeTo: []proto.Message{
1766 (*testpb.TestAllTypes)(nil),
1767 (*testpb.TestAllExtensions)(nil),
1768 },
1769 wire: pack.Message{
1770 pack.Tag{36, pack.BytesType}, pack.Bytes{0xff},
1771 }.Marshal(),
1772 },
1773 {
1774 desc: "invalid packed fixed32 field",
1775 decodeTo: []proto.Message{
1776 (*testpb.TestAllTypes)(nil),
1777 (*testpb.TestAllExtensions)(nil),
1778 },
1779 wire: pack.Message{
1780 pack.Tag{37, pack.BytesType}, pack.Bytes{0x00},
1781 }.Marshal(),
1782 },
1783 {
1784 desc: "invalid packed fixed64 field",
1785 decodeTo: []proto.Message{
1786 (*testpb.TestAllTypes)(nil),
1787 (*testpb.TestAllExtensions)(nil),
1788 },
1789 wire: pack.Message{
1790 pack.Tag{38, pack.BytesType}, pack.Bytes{0x00},
1791 }.Marshal(),
1792 },
1793 {
1794 desc: "invalid packed sfixed32 field",
1795 decodeTo: []proto.Message{
1796 (*testpb.TestAllTypes)(nil),
1797 (*testpb.TestAllExtensions)(nil),
1798 },
1799 wire: pack.Message{
1800 pack.Tag{39, pack.BytesType}, pack.Bytes{0x00},
1801 }.Marshal(),
1802 },
1803 {
1804 desc: "invalid packed sfixed64 field",
1805 decodeTo: []proto.Message{
1806 (*testpb.TestAllTypes)(nil),
1807 (*testpb.TestAllExtensions)(nil),
1808 },
1809 wire: pack.Message{
1810 pack.Tag{40, pack.BytesType}, pack.Bytes{0x00},
1811 }.Marshal(),
1812 },
1813 {
1814 desc: "invalid packed float field",
1815 decodeTo: []proto.Message{
1816 (*testpb.TestAllTypes)(nil),
1817 (*testpb.TestAllExtensions)(nil),
1818 },
1819 wire: pack.Message{
1820 pack.Tag{41, pack.BytesType}, pack.Bytes{0x00},
1821 }.Marshal(),
1822 },
1823 {
1824 desc: "invalid packed double field",
1825 decodeTo: []proto.Message{
1826 (*testpb.TestAllTypes)(nil),
1827 (*testpb.TestAllExtensions)(nil),
1828 },
1829 wire: pack.Message{
1830 pack.Tag{42, pack.BytesType}, pack.Bytes{0x00},
1831 }.Marshal(),
1832 },
1833 {
1834 desc: "invalid packed bool field",
1835 decodeTo: []proto.Message{
1836 (*testpb.TestAllTypes)(nil),
1837 (*testpb.TestAllExtensions)(nil),
1838 },
1839 wire: pack.Message{
1840 pack.Tag{43, pack.BytesType}, pack.Bytes{0xff},
1841 }.Marshal(),
1842 },
1843 {
1844 desc: "bytes field overruns message",
1845 decodeTo: []proto.Message{
1846 (*testpb.TestAllTypes)(nil),
1847 (*testpb.TestAllExtensions)(nil),
1848 },
1849 wire: pack.Message{
1850 pack.Tag{18, pack.BytesType}, pack.LengthPrefix{pack.Message{
1851 pack.Tag{2, pack.BytesType}, pack.LengthPrefix{pack.Message{
1852 pack.Tag{15, pack.BytesType}, pack.Varint(2),
1853 }},
1854 pack.Tag{1, pack.VarintType}, pack.Varint(0),
1855 }},
1856 }.Marshal(),
1857 },
Damien Neil6f297792020-01-29 15:55:53 -08001858 {
1859 desc: "varint field overruns message",
1860 decodeTo: []proto.Message{
1861 (*testpb.TestAllTypes)(nil),
1862 (*testpb.TestAllExtensions)(nil),
1863 },
1864 wire: pack.Message{
1865 pack.Tag{1, pack.VarintType},
1866 }.Marshal(),
1867 },
1868 {
1869 desc: "bytes field lacks size",
1870 decodeTo: []proto.Message{
1871 (*testpb.TestAllTypes)(nil),
1872 (*testpb.TestAllExtensions)(nil),
1873 },
1874 wire: pack.Message{
1875 pack.Tag{18, pack.BytesType},
1876 }.Marshal(),
1877 },
Damien Neil4d918162020-02-01 10:39:11 -08001878 {
1879 desc: "varint overflow",
1880 decodeTo: []proto.Message{
1881 (*testpb.TestAllTypes)(nil),
1882 (*testpb.TestAllExtensions)(nil),
1883 },
1884 wire: pack.Message{
1885 pack.Tag{1, pack.VarintType},
1886 pack.Raw("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x02"),
1887 }.Marshal(),
1888 },
Damien Neil0f783d82020-02-05 07:34:41 -08001889 {
1890 desc: "varint length overrun",
1891 decodeTo: []proto.Message{
1892 (*testpb.TestAllTypes)(nil),
1893 (*testpb.TestAllExtensions)(nil),
1894 },
1895 wire: pack.Message{
1896 pack.Tag{1, pack.VarintType},
1897 pack.Raw("\xff\xff\xff\xff\xff\xff\xff\xff\xff"),
1898 }.Marshal(),
1899 },
Damien Neild0b07492019-12-16 12:59:13 -08001900}
Damien Neil1c33e112020-02-04 12:58:17 -08001901
1902type filterResolver struct {
1903 filter func(name protoreflect.FullName) bool
1904 resolver protoregistry.ExtensionTypeResolver
1905}
1906
1907func (f filterResolver) FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error) {
1908 if !f.filter(field) {
1909 return nil, protoregistry.NotFound
1910 }
1911 return f.resolver.FindExtensionByName(field)
1912}
1913
1914func (f filterResolver) FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error) {
1915 xt, err := f.resolver.FindExtensionByNumber(message, field)
1916 if err != nil {
1917 return nil, err
1918 }
1919 if !f.filter(xt.TypeDescriptor().FullName()) {
1920 return nil, protoregistry.NotFound
1921 }
1922 return xt, nil
1923}