blob: 7a2eaad8d92204c1bd7384f49e7daf8c7f0fd6f0 [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 Neilc600d6c2020-01-21 15:00:33 -08001010 desc: "required field with incompatible wire type",
1011 checkFastInit: true,
1012 partial: true,
Damien Neilb0c26f12019-12-16 09:37:59 -08001013 decodeTo: []proto.Message{build(
1014 &testpb.TestRequired{},
1015 unknown(pack.Message{
1016 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2),
1017 }.Marshal()),
1018 )},
1019 wire: pack.Message{
1020 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2),
1021 }.Marshal(),
1022 },
1023 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001024 desc: "required field in optional message unset",
1025 checkFastInit: true,
1026 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001027 decodeTo: makeMessages(protobuild.Message{
1028 "optional_message": protobuild.Message{},
1029 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001030 wire: pack.Message{
1031 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1032 }.Marshal(),
1033 },
1034 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001035 desc: "required field in optional message set",
1036 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001037 decodeTo: makeMessages(protobuild.Message{
1038 "optional_message": protobuild.Message{
1039 "required_field": 1,
Damien Neild0b07492019-12-16 12:59:13 -08001040 },
Damien Neild025c952020-02-02 00:53:34 -08001041 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001042 wire: pack.Message{
1043 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1044 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1045 }),
1046 }.Marshal(),
1047 },
1048 {
Damien Neilcadb4ab2020-02-03 16:17:31 -08001049 desc: "required field in optional message set (split across multiple tags)",
1050 checkFastInit: false, // fast init checks don't handle split messages
1051 nocheckValidInit: true, // validation doesn't either
Damien Neild025c952020-02-02 00:53:34 -08001052 decodeTo: makeMessages(protobuild.Message{
1053 "optional_message": protobuild.Message{
1054 "required_field": 1,
Damien Neild0b07492019-12-16 12:59:13 -08001055 },
Damien Neild025c952020-02-02 00:53:34 -08001056 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001057 wire: pack.Message{
1058 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1059 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1060 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1061 }),
1062 }.Marshal(),
1063 },
1064 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001065 desc: "required field in repeated message unset",
1066 checkFastInit: true,
1067 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001068 decodeTo: makeMessages(protobuild.Message{
1069 "repeated_message": []protobuild.Message{
1070 {"required_field": 1},
Damien Neild0b07492019-12-16 12:59:13 -08001071 {},
1072 },
Damien Neild025c952020-02-02 00:53:34 -08001073 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001074 wire: pack.Message{
1075 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1076 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1077 }),
1078 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1079 }.Marshal(),
1080 },
1081 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001082 desc: "required field in repeated message set",
1083 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001084 decodeTo: makeMessages(protobuild.Message{
1085 "repeated_message": []protobuild.Message{
1086 {"required_field": 1},
1087 {"required_field": 2},
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{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1092 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1093 }),
1094 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1095 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1096 }),
1097 }.Marshal(),
1098 },
1099 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001100 desc: "required field in map message unset",
1101 checkFastInit: true,
1102 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001103 decodeTo: makeMessages(protobuild.Message{
1104 "map_message": map[int32]protobuild.Message{
1105 1: {"required_field": 1},
Damien Neild0b07492019-12-16 12:59:13 -08001106 2: {},
1107 },
Damien Neild025c952020-02-02 00:53:34 -08001108 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001109 wire: pack.Message{
1110 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1111 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1112 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1113 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1114 }),
1115 }),
1116 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1117 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1118 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1119 }),
1120 }.Marshal(),
1121 },
1122 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001123 desc: "required field in absent map message value",
1124 checkFastInit: true,
1125 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001126 decodeTo: makeMessages(protobuild.Message{
1127 "map_message": map[int32]protobuild.Message{
Damien Neil54a0a042020-01-08 17:53:16 -08001128 2: {},
1129 },
Damien Neild025c952020-02-02 00:53:34 -08001130 }, &testpb.TestRequiredForeign{}),
Damien Neil54a0a042020-01-08 17:53:16 -08001131 wire: pack.Message{
1132 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1133 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1134 }),
1135 }.Marshal(),
1136 },
1137 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001138 desc: "required field in map message set",
1139 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001140 decodeTo: makeMessages(protobuild.Message{
1141 "map_message": map[int32]protobuild.Message{
1142 1: {"required_field": 1},
1143 2: {"required_field": 2},
Damien Neild0b07492019-12-16 12:59:13 -08001144 },
Damien Neild025c952020-02-02 00:53:34 -08001145 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001146 wire: pack.Message{
1147 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1148 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1149 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1150 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1151 }),
1152 }),
1153 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1154 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1155 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1156 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1157 }),
1158 }),
1159 }.Marshal(),
1160 },
1161 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001162 desc: "required field in optional group unset",
1163 checkFastInit: true,
1164 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001165 decodeTo: makeMessages(protobuild.Message{
1166 "optionalgroup": protobuild.Message{},
1167 }, &testpb.TestRequiredGroupFields{}),
Damien Neild0b07492019-12-16 12:59:13 -08001168 wire: pack.Message{
1169 pack.Tag{1, pack.StartGroupType},
1170 pack.Tag{1, pack.EndGroupType},
1171 }.Marshal(),
1172 },
1173 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001174 desc: "required field in optional group set",
1175 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001176 decodeTo: makeMessages(protobuild.Message{
1177 "optionalgroup": protobuild.Message{
1178 "a": 1,
Damien Neild0b07492019-12-16 12:59:13 -08001179 },
Damien Neild025c952020-02-02 00:53:34 -08001180 }, &testpb.TestRequiredGroupFields{}),
Damien Neild0b07492019-12-16 12:59:13 -08001181 wire: pack.Message{
1182 pack.Tag{1, pack.StartGroupType},
1183 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1184 pack.Tag{1, pack.EndGroupType},
1185 }.Marshal(),
1186 },
1187 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001188 desc: "required field in repeated group unset",
1189 checkFastInit: true,
1190 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001191 decodeTo: makeMessages(protobuild.Message{
1192 "repeatedgroup": []protobuild.Message{
1193 {"a": 1},
Damien Neild0b07492019-12-16 12:59:13 -08001194 {},
1195 },
Damien Neild025c952020-02-02 00:53:34 -08001196 }, &testpb.TestRequiredGroupFields{}),
Damien Neild0b07492019-12-16 12:59:13 -08001197 wire: pack.Message{
1198 pack.Tag{3, pack.StartGroupType},
1199 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1200 pack.Tag{3, pack.EndGroupType},
1201 pack.Tag{3, pack.StartGroupType},
1202 pack.Tag{3, pack.EndGroupType},
1203 }.Marshal(),
1204 },
1205 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001206 desc: "required field in repeated group set",
1207 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001208 decodeTo: makeMessages(protobuild.Message{
1209 "repeatedgroup": []protobuild.Message{
1210 {"a": 1},
1211 {"a": 2},
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{3, pack.StartGroupType},
1216 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1217 pack.Tag{3, pack.EndGroupType},
1218 pack.Tag{3, pack.StartGroupType},
1219 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1220 pack.Tag{3, pack.EndGroupType},
1221 }.Marshal(),
1222 },
1223 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001224 desc: "required field in oneof message unset",
1225 checkFastInit: true,
1226 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001227 decodeTo: makeMessages(protobuild.Message{
1228 "oneof_message": protobuild.Message{},
1229 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001230 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1231 },
1232 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001233 desc: "required field in oneof message set",
1234 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001235 decodeTo: makeMessages(protobuild.Message{
1236 "oneof_message": protobuild.Message{
1237 "required_field": 1,
1238 },
1239 }, &testpb.TestRequiredForeign{}),
Damien Neild0b07492019-12-16 12:59:13 -08001240 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
1241 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1242 })}.Marshal(),
1243 },
1244 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001245 desc: "required field in extension message unset",
1246 checkFastInit: true,
1247 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001248 decodeTo: makeMessages(protobuild.Message{
1249 "single": protobuild.Message{},
1250 }, &testpb.TestAllExtensions{}),
Damien Neild0b07492019-12-16 12:59:13 -08001251 wire: pack.Message{
1252 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1253 }.Marshal(),
1254 },
1255 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001256 desc: "required field in extension message set",
1257 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001258 decodeTo: makeMessages(protobuild.Message{
1259 "single": protobuild.Message{
1260 "required_field": 1,
1261 },
1262 }, &testpb.TestAllExtensions{}),
Damien Neild0b07492019-12-16 12:59:13 -08001263 wire: pack.Message{
1264 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1265 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1266 }),
1267 }.Marshal(),
1268 },
1269 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001270 desc: "required field in repeated extension message unset",
1271 checkFastInit: true,
1272 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001273 decodeTo: makeMessages(protobuild.Message{
1274 "multi": []protobuild.Message{
1275 {"required_field": 1},
Damien Neild0b07492019-12-16 12:59:13 -08001276 {},
Damien Neild025c952020-02-02 00:53:34 -08001277 },
1278 }, &testpb.TestAllExtensions{}),
Damien Neild0b07492019-12-16 12:59:13 -08001279 wire: pack.Message{
1280 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1281 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1282 }),
1283 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1284 }.Marshal(),
1285 },
1286 {
Damien Neilc600d6c2020-01-21 15:00:33 -08001287 desc: "required field in repeated extension message set",
1288 checkFastInit: true,
Damien Neild025c952020-02-02 00:53:34 -08001289 decodeTo: makeMessages(protobuild.Message{
1290 "multi": []protobuild.Message{
1291 {"required_field": 1},
1292 {"required_field": 2},
1293 },
1294 }, &testpb.TestAllExtensions{}),
Damien Neild0b07492019-12-16 12:59:13 -08001295 wire: pack.Message{
1296 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1297 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1298 }),
1299 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1300 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1301 }),
1302 }.Marshal(),
1303 },
1304 {
1305 desc: "nil messages",
1306 decodeTo: []proto.Message{
1307 (*testpb.TestAllTypes)(nil),
1308 (*test3pb.TestAllTypes)(nil),
1309 (*testpb.TestAllExtensions)(nil),
1310 },
1311 },
1312 {
1313 desc: "legacy",
1314 partial: true,
Damien Neild025c952020-02-02 00:53:34 -08001315 decodeTo: makeMessages(protobuild.Message{
1316 "f1": protobuild.Message{
1317 "optional_int32": 1,
1318 "optional_child_enum": "ALPHA",
1319 "optional_child_message": protobuild.Message{
1320 "f1": "x",
1321 },
1322 "optionalgroup": protobuild.Message{
1323 "f1": "x",
1324 },
1325 "repeated_child_message": []protobuild.Message{
1326 {"f1": "x"},
1327 },
1328 "repeatedgroup": []protobuild.Message{
1329 {"f1": "x"},
1330 },
1331 "map_bool_child_message": map[bool]protobuild.Message{
1332 true: {"f1": "x"},
1333 },
1334 "oneof_child_message": protobuild.Message{
1335 "f1": "x",
Damien Neild0b07492019-12-16 12:59:13 -08001336 },
1337 },
Damien Neild025c952020-02-02 00:53:34 -08001338 }, &legacypb.Legacy{}),
Damien Neild0b07492019-12-16 12:59:13 -08001339 wire: pack.Message{
1340 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1341 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1342 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1343 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1344 pack.Tag{1, pack.BytesType}, pack.String("x"),
1345 }),
1346 pack.Tag{120, pack.StartGroupType},
1347 pack.Tag{1, pack.BytesType}, pack.String("x"),
1348 pack.Tag{120, pack.EndGroupType},
1349 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1350 pack.Tag{1, pack.BytesType}, pack.String("x"),
1351 }),
1352 pack.Tag{520, pack.StartGroupType},
1353 pack.Tag{1, pack.BytesType}, pack.String("x"),
1354 pack.Tag{520, pack.EndGroupType},
1355 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1356 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1357 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1358 pack.Tag{1, pack.BytesType}, pack.String("x"),
1359 }),
1360 }),
1361 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1362 pack.Tag{1, pack.BytesType}, pack.String("x"),
1363 }),
1364 }),
1365 }.Marshal(),
Damien Neilb0c26f12019-12-16 09:37:59 -08001366 validationStatus: impl.ValidationUnknown,
Damien Neild0b07492019-12-16 12:59:13 -08001367 },
1368 {
1369 desc: "first reserved field number",
Damien Neild025c952020-02-02 00:53:34 -08001370 decodeTo: makeMessages(protobuild.Message{
1371 protobuild.Unknown: pack.Message{
Damien Neild0b07492019-12-16 12:59:13 -08001372 pack.Tag{pack.FirstReservedNumber, pack.VarintType}, pack.Varint(1004),
Damien Neild025c952020-02-02 00:53:34 -08001373 }.Marshal(),
1374 }),
Damien Neild0b07492019-12-16 12:59:13 -08001375 wire: pack.Message{
1376 pack.Tag{pack.FirstReservedNumber, pack.VarintType}, pack.Varint(1004),
1377 }.Marshal(),
1378 },
1379 {
1380 desc: "last reserved field number",
Damien Neild025c952020-02-02 00:53:34 -08001381 decodeTo: makeMessages(protobuild.Message{
1382 protobuild.Unknown: pack.Message{
Damien Neild0b07492019-12-16 12:59:13 -08001383 pack.Tag{pack.LastReservedNumber, pack.VarintType}, pack.Varint(1005),
Damien Neild025c952020-02-02 00:53:34 -08001384 }.Marshal(),
1385 }),
Damien Neild0b07492019-12-16 12:59:13 -08001386 wire: pack.Message{
1387 pack.Tag{pack.LastReservedNumber, pack.VarintType}, pack.Varint(1005),
1388 }.Marshal(),
1389 },
Damien Neila60e7092020-01-28 14:53:44 -08001390 {
1391 desc: "nested unknown extension",
1392 unmarshalOptions: proto.UnmarshalOptions{
1393 DiscardUnknown: true,
Damien Neil1c33e112020-02-04 12:58:17 -08001394 Resolver: filterResolver{
1395 filter: func(name protoreflect.FullName) bool {
1396 switch name.Name() {
1397 case "optional_nested_message",
1398 "optional_int32":
1399 return true
1400 }
1401 return false
1402 },
1403 resolver: protoregistry.GlobalTypes,
1404 },
Damien Neila60e7092020-01-28 14:53:44 -08001405 },
Damien Neild025c952020-02-02 00:53:34 -08001406 decodeTo: makeMessages(protobuild.Message{
1407 "optional_nested_message": protobuild.Message{
1408 "corecursive": protobuild.Message{
1409 "optional_nested_message": protobuild.Message{
1410 "corecursive": protobuild.Message{
1411 "optional_int32": 42,
1412 },
1413 },
1414 },
1415 },
1416 }, &testpb.TestAllExtensions{}),
Damien Neila60e7092020-01-28 14:53:44 -08001417 wire: pack.Message{
1418 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1419 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1420 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1421 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1422 pack.Tag{1, pack.VarintType}, pack.Varint(42),
1423 pack.Tag{2, pack.VarintType}, pack.Varint(43),
1424 }),
1425 }),
1426 }),
1427 }),
1428 }.Marshal(),
1429 },
Damien Neild0b07492019-12-16 12:59:13 -08001430}
1431
1432var testInvalidMessages = []testProto{
1433 {
1434 desc: "invalid UTF-8 in optional string field",
Damien Neild025c952020-02-02 00:53:34 -08001435 decodeTo: makeMessages(protobuild.Message{
1436 "optional_string": "abc\xff",
1437 }, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -08001438 wire: pack.Message{
1439 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1440 }.Marshal(),
1441 },
1442 {
1443 desc: "invalid UTF-8 in repeated string field",
Damien Neild025c952020-02-02 00:53:34 -08001444 decodeTo: makeMessages(protobuild.Message{
1445 "repeated_string": []string{"foo", "abc\xff"},
1446 }, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -08001447 wire: pack.Message{
1448 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1449 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1450 }.Marshal(),
1451 },
1452 {
1453 desc: "invalid UTF-8 in nested message",
Damien Neild025c952020-02-02 00:53:34 -08001454 decodeTo: makeMessages(protobuild.Message{
1455 "optional_nested_message": protobuild.Message{
1456 "corecursive": protobuild.Message{
1457 "optional_string": "abc\xff",
Damien Neild0b07492019-12-16 12:59:13 -08001458 },
1459 },
Damien Neild025c952020-02-02 00:53:34 -08001460 }, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -08001461 wire: pack.Message{
1462 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1463 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1464 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1465 }),
1466 }),
1467 }.Marshal(),
1468 },
1469 {
1470 desc: "invalid UTF-8 in oneof field",
Damien Neild025c952020-02-02 00:53:34 -08001471 decodeTo: makeMessages(protobuild.Message{
1472 "oneof_string": "abc\xff",
1473 }, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -08001474 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1475 },
1476 {
1477 desc: "invalid UTF-8 in map key",
Damien Neild025c952020-02-02 00:53:34 -08001478 decodeTo: makeMessages(protobuild.Message{
1479 "map_string_string": map[string]string{"key\xff": "val"},
1480 }, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -08001481 wire: pack.Message{
1482 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1483 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1484 pack.Tag{2, pack.BytesType}, pack.String("val"),
1485 }),
1486 }.Marshal(),
1487 },
1488 {
1489 desc: "invalid UTF-8 in map value",
Damien Neild025c952020-02-02 00:53:34 -08001490 decodeTo: makeMessages(protobuild.Message{
1491 "map_string_string": map[string]string{"key": "val\xff"},
1492 }, &test3pb.TestAllTypes{}),
Damien Neild0b07492019-12-16 12:59:13 -08001493 wire: pack.Message{
1494 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1495 pack.Tag{1, pack.BytesType}, pack.String("key"),
1496 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1497 }),
1498 }.Marshal(),
1499 },
1500 {
Damien Neilb0c26f12019-12-16 09:37:59 -08001501 desc: "invalid field number zero",
1502 decodeTo: []proto.Message{
1503 (*testpb.TestAllTypes)(nil),
1504 (*testpb.TestAllExtensions)(nil),
1505 },
Damien Neild0b07492019-12-16 12:59:13 -08001506 wire: pack.Message{
1507 pack.Tag{pack.MinValidNumber - 1, pack.VarintType}, pack.Varint(1001),
1508 }.Marshal(),
1509 },
1510 {
Damien Neilb0c26f12019-12-16 09:37:59 -08001511 desc: "invalid field numbers zero and one",
1512 decodeTo: []proto.Message{
1513 (*testpb.TestAllTypes)(nil),
1514 (*testpb.TestAllExtensions)(nil),
1515 },
Damien Neild0b07492019-12-16 12:59:13 -08001516 wire: pack.Message{
1517 pack.Tag{pack.MinValidNumber - 1, pack.VarintType}, pack.Varint(1002),
1518 pack.Tag{pack.MinValidNumber, pack.VarintType}, pack.Varint(1003),
1519 }.Marshal(),
1520 },
1521 {
Damien Neilb0c26f12019-12-16 09:37:59 -08001522 desc: "invalid field numbers max and max+1",
1523 decodeTo: []proto.Message{
1524 (*testpb.TestAllTypes)(nil),
1525 (*testpb.TestAllExtensions)(nil),
1526 },
Damien Neild0b07492019-12-16 12:59:13 -08001527 wire: pack.Message{
1528 pack.Tag{pack.MaxValidNumber, pack.VarintType}, pack.Varint(1006),
1529 pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(1007),
1530 }.Marshal(),
1531 },
1532 {
Damien Neilb0c26f12019-12-16 09:37:59 -08001533 desc: "invalid field number max+1",
1534 decodeTo: []proto.Message{
1535 (*testpb.TestAllTypes)(nil),
1536 (*testpb.TestAllExtensions)(nil),
1537 },
Damien Neild0b07492019-12-16 12:59:13 -08001538 wire: pack.Message{
1539 pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(1008),
1540 }.Marshal(),
1541 },
Damien Neilf2427c02019-12-20 09:43:20 -08001542 {
Damien Neila522d5f2020-01-27 21:50:47 -08001543 desc: "invalid field number wraps int32",
1544 decodeTo: []proto.Message{
1545 (*testpb.TestAllTypes)(nil),
1546 (*testpb.TestAllExtensions)(nil),
1547 },
1548 wire: pack.Message{
1549 pack.Varint(2234993595104), pack.Varint(0),
1550 }.Marshal(),
1551 },
1552 {
Damien Neilf2427c02019-12-20 09:43:20 -08001553 desc: "invalid field number in map",
1554 decodeTo: []proto.Message{(*testpb.TestAllTypes)(nil)},
1555 wire: pack.Message{
1556 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
1557 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
1558 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
1559 pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(0),
1560 }),
1561 }.Marshal(),
1562 },
Damien Neilb0c26f12019-12-16 09:37:59 -08001563 {
1564 desc: "invalid tag varint",
1565 decodeTo: []proto.Message{
1566 (*testpb.TestAllTypes)(nil),
1567 (*testpb.TestAllExtensions)(nil),
1568 },
1569 wire: []byte{0xff},
1570 },
1571 {
1572 desc: "field number too small",
1573 decodeTo: []proto.Message{
1574 (*testpb.TestAllTypes)(nil),
1575 (*testpb.TestAllExtensions)(nil),
1576 },
1577 wire: pack.Message{
1578 pack.Tag{0, pack.VarintType}, pack.Varint(0),
1579 }.Marshal(),
1580 },
1581 {
1582 desc: "field number too large",
1583 decodeTo: []proto.Message{
1584 (*testpb.TestAllTypes)(nil),
1585 (*testpb.TestAllExtensions)(nil),
1586 },
1587 wire: pack.Message{
1588 pack.Tag{wire.MaxValidNumber + 1, pack.VarintType}, pack.Varint(0),
1589 }.Marshal(),
1590 },
1591 {
1592 desc: "invalid tag varint in message field",
1593 decodeTo: []proto.Message{
1594 (*testpb.TestAllTypes)(nil),
1595 (*testpb.TestAllExtensions)(nil),
1596 },
1597 wire: pack.Message{
1598 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1599 pack.Raw{0xff},
1600 }),
1601 }.Marshal(),
1602 },
1603 {
1604 desc: "invalid tag varint in repeated message field",
1605 decodeTo: []proto.Message{
1606 (*testpb.TestAllTypes)(nil),
1607 (*testpb.TestAllExtensions)(nil),
1608 },
1609 wire: pack.Message{
1610 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
1611 pack.Raw{0xff},
1612 }),
1613 }.Marshal(),
1614 },
1615 {
1616 desc: "invalid varint in group field",
1617 decodeTo: []proto.Message{
1618 (*testpb.TestAllTypes)(nil),
1619 (*testpb.TestAllExtensions)(nil),
1620 },
1621 wire: pack.Message{
1622 pack.Tag{16, pack.StartGroupType},
1623 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1624 pack.Raw{0xff},
1625 }),
1626 pack.Tag{16, pack.EndGroupType},
1627 }.Marshal(),
1628 },
1629 {
1630 desc: "invalid varint in repeated group field",
1631 decodeTo: []proto.Message{
1632 (*testpb.TestAllTypes)(nil),
1633 (*testpb.TestAllExtensions)(nil),
1634 },
1635 wire: pack.Message{
1636 pack.Tag{46, pack.StartGroupType},
1637 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1638 pack.Raw{0xff},
1639 }),
1640 pack.Tag{46, pack.EndGroupType},
1641 }.Marshal(),
1642 },
1643 {
1644 desc: "unterminated repeated group field",
1645 decodeTo: []proto.Message{
1646 (*testpb.TestAllTypes)(nil),
1647 (*testpb.TestAllExtensions)(nil),
1648 },
1649 wire: pack.Message{
1650 pack.Tag{46, pack.StartGroupType},
1651 }.Marshal(),
1652 },
1653 {
1654 desc: "invalid tag varint in map item",
1655 decodeTo: []proto.Message{
1656 (*testpb.TestAllTypes)(nil),
1657 },
1658 wire: pack.Message{
1659 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
1660 pack.Tag{1, pack.VarintType}, pack.Varint(0),
1661 pack.Tag{2, pack.VarintType}, pack.Varint(0),
1662 pack.Raw{0xff},
1663 }),
1664 }.Marshal(),
1665 },
1666 {
1667 desc: "invalid tag varint in map message value",
1668 decodeTo: []proto.Message{
1669 (*testpb.TestAllTypes)(nil),
1670 },
1671 wire: pack.Message{
1672 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
1673 pack.Tag{1, pack.VarintType}, pack.Varint(0),
1674 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1675 pack.Raw{0xff},
1676 }),
1677 }),
1678 }.Marshal(),
1679 },
1680 {
1681 desc: "invalid packed int32 field",
1682 decodeTo: []proto.Message{
1683 (*testpb.TestAllTypes)(nil),
1684 (*testpb.TestAllExtensions)(nil),
1685 },
1686 wire: pack.Message{
1687 pack.Tag{31, pack.BytesType}, pack.Bytes{0xff},
1688 }.Marshal(),
1689 },
1690 {
1691 desc: "invalid packed int64 field",
1692 decodeTo: []proto.Message{
1693 (*testpb.TestAllTypes)(nil),
1694 (*testpb.TestAllExtensions)(nil),
1695 },
1696 wire: pack.Message{
1697 pack.Tag{32, pack.BytesType}, pack.Bytes{0xff},
1698 }.Marshal(),
1699 },
1700 {
1701 desc: "invalid packed uint32 field",
1702 decodeTo: []proto.Message{
1703 (*testpb.TestAllTypes)(nil),
1704 (*testpb.TestAllExtensions)(nil),
1705 },
1706 wire: pack.Message{
1707 pack.Tag{33, pack.BytesType}, pack.Bytes{0xff},
1708 }.Marshal(),
1709 },
1710 {
1711 desc: "invalid packed uint64 field",
1712 decodeTo: []proto.Message{
1713 (*testpb.TestAllTypes)(nil),
1714 (*testpb.TestAllExtensions)(nil),
1715 },
1716 wire: pack.Message{
1717 pack.Tag{34, pack.BytesType}, pack.Bytes{0xff},
1718 }.Marshal(),
1719 },
1720 {
1721 desc: "invalid packed sint32 field",
1722 decodeTo: []proto.Message{
1723 (*testpb.TestAllTypes)(nil),
1724 (*testpb.TestAllExtensions)(nil),
1725 },
1726 wire: pack.Message{
1727 pack.Tag{35, pack.BytesType}, pack.Bytes{0xff},
1728 }.Marshal(),
1729 },
1730 {
1731 desc: "invalid packed sint64 field",
1732 decodeTo: []proto.Message{
1733 (*testpb.TestAllTypes)(nil),
1734 (*testpb.TestAllExtensions)(nil),
1735 },
1736 wire: pack.Message{
1737 pack.Tag{36, pack.BytesType}, pack.Bytes{0xff},
1738 }.Marshal(),
1739 },
1740 {
1741 desc: "invalid packed fixed32 field",
1742 decodeTo: []proto.Message{
1743 (*testpb.TestAllTypes)(nil),
1744 (*testpb.TestAllExtensions)(nil),
1745 },
1746 wire: pack.Message{
1747 pack.Tag{37, pack.BytesType}, pack.Bytes{0x00},
1748 }.Marshal(),
1749 },
1750 {
1751 desc: "invalid packed fixed64 field",
1752 decodeTo: []proto.Message{
1753 (*testpb.TestAllTypes)(nil),
1754 (*testpb.TestAllExtensions)(nil),
1755 },
1756 wire: pack.Message{
1757 pack.Tag{38, pack.BytesType}, pack.Bytes{0x00},
1758 }.Marshal(),
1759 },
1760 {
1761 desc: "invalid packed sfixed32 field",
1762 decodeTo: []proto.Message{
1763 (*testpb.TestAllTypes)(nil),
1764 (*testpb.TestAllExtensions)(nil),
1765 },
1766 wire: pack.Message{
1767 pack.Tag{39, pack.BytesType}, pack.Bytes{0x00},
1768 }.Marshal(),
1769 },
1770 {
1771 desc: "invalid packed sfixed64 field",
1772 decodeTo: []proto.Message{
1773 (*testpb.TestAllTypes)(nil),
1774 (*testpb.TestAllExtensions)(nil),
1775 },
1776 wire: pack.Message{
1777 pack.Tag{40, pack.BytesType}, pack.Bytes{0x00},
1778 }.Marshal(),
1779 },
1780 {
1781 desc: "invalid packed float field",
1782 decodeTo: []proto.Message{
1783 (*testpb.TestAllTypes)(nil),
1784 (*testpb.TestAllExtensions)(nil),
1785 },
1786 wire: pack.Message{
1787 pack.Tag{41, pack.BytesType}, pack.Bytes{0x00},
1788 }.Marshal(),
1789 },
1790 {
1791 desc: "invalid packed double field",
1792 decodeTo: []proto.Message{
1793 (*testpb.TestAllTypes)(nil),
1794 (*testpb.TestAllExtensions)(nil),
1795 },
1796 wire: pack.Message{
1797 pack.Tag{42, pack.BytesType}, pack.Bytes{0x00},
1798 }.Marshal(),
1799 },
1800 {
1801 desc: "invalid packed bool field",
1802 decodeTo: []proto.Message{
1803 (*testpb.TestAllTypes)(nil),
1804 (*testpb.TestAllExtensions)(nil),
1805 },
1806 wire: pack.Message{
1807 pack.Tag{43, pack.BytesType}, pack.Bytes{0xff},
1808 }.Marshal(),
1809 },
1810 {
1811 desc: "bytes field overruns message",
1812 decodeTo: []proto.Message{
1813 (*testpb.TestAllTypes)(nil),
1814 (*testpb.TestAllExtensions)(nil),
1815 },
1816 wire: pack.Message{
1817 pack.Tag{18, pack.BytesType}, pack.LengthPrefix{pack.Message{
1818 pack.Tag{2, pack.BytesType}, pack.LengthPrefix{pack.Message{
1819 pack.Tag{15, pack.BytesType}, pack.Varint(2),
1820 }},
1821 pack.Tag{1, pack.VarintType}, pack.Varint(0),
1822 }},
1823 }.Marshal(),
1824 },
Damien Neil6f297792020-01-29 15:55:53 -08001825 {
1826 desc: "varint field overruns message",
1827 decodeTo: []proto.Message{
1828 (*testpb.TestAllTypes)(nil),
1829 (*testpb.TestAllExtensions)(nil),
1830 },
1831 wire: pack.Message{
1832 pack.Tag{1, pack.VarintType},
1833 }.Marshal(),
1834 },
1835 {
1836 desc: "bytes field lacks size",
1837 decodeTo: []proto.Message{
1838 (*testpb.TestAllTypes)(nil),
1839 (*testpb.TestAllExtensions)(nil),
1840 },
1841 wire: pack.Message{
1842 pack.Tag{18, pack.BytesType},
1843 }.Marshal(),
1844 },
Damien Neil4d918162020-02-01 10:39:11 -08001845 {
1846 desc: "varint overflow",
1847 decodeTo: []proto.Message{
1848 (*testpb.TestAllTypes)(nil),
1849 (*testpb.TestAllExtensions)(nil),
1850 },
1851 wire: pack.Message{
1852 pack.Tag{1, pack.VarintType},
1853 pack.Raw("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x02"),
1854 }.Marshal(),
1855 },
Damien Neil0f783d82020-02-05 07:34:41 -08001856 {
1857 desc: "varint length overrun",
1858 decodeTo: []proto.Message{
1859 (*testpb.TestAllTypes)(nil),
1860 (*testpb.TestAllExtensions)(nil),
1861 },
1862 wire: pack.Message{
1863 pack.Tag{1, pack.VarintType},
1864 pack.Raw("\xff\xff\xff\xff\xff\xff\xff\xff\xff"),
1865 }.Marshal(),
1866 },
Damien Neild0b07492019-12-16 12:59:13 -08001867}
Damien Neil1c33e112020-02-04 12:58:17 -08001868
1869type filterResolver struct {
1870 filter func(name protoreflect.FullName) bool
1871 resolver protoregistry.ExtensionTypeResolver
1872}
1873
1874func (f filterResolver) FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error) {
1875 if !f.filter(field) {
1876 return nil, protoregistry.NotFound
1877 }
1878 return f.resolver.FindExtensionByName(field)
1879}
1880
1881func (f filterResolver) FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error) {
1882 xt, err := f.resolver.FindExtensionByNumber(message, field)
1883 if err != nil {
1884 return nil, err
1885 }
1886 if !f.filter(xt.TypeDescriptor().FullName()) {
1887 return nil, protoregistry.NotFound
1888 }
1889 return xt, nil
1890}