1
2
3
4
5 package proto_test
6
7 import (
8 "google.golang.org/protobuf/encoding/protowire"
9 "google.golang.org/protobuf/internal/impl"
10 "google.golang.org/protobuf/internal/protobuild"
11 "google.golang.org/protobuf/proto"
12 "google.golang.org/protobuf/reflect/protoreflect"
13 "google.golang.org/protobuf/reflect/protoregistry"
14 "google.golang.org/protobuf/testing/protopack"
15
16 legacypb "google.golang.org/protobuf/internal/testprotos/legacy"
17 requiredpb "google.golang.org/protobuf/internal/testprotos/required"
18 testpb "google.golang.org/protobuf/internal/testprotos/test"
19 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
20 testeditionspb "google.golang.org/protobuf/internal/testprotos/testeditions"
21 )
22
23 type testProto struct {
24 desc string
25 decodeTo []proto.Message
26 wire []byte
27 partial bool
28 noEncode bool
29 checkFastInit bool
30 unmarshalOptions proto.UnmarshalOptions
31 validationStatus impl.ValidationStatus
32 nocheckValidInit bool
33 }
34
35 func makeMessages(in protobuild.Message, messages ...proto.Message) []proto.Message {
36 if len(messages) == 0 {
37 messages = []proto.Message{
38 &testpb.TestAllTypes{},
39 &test3pb.TestAllTypes{},
40 &testpb.TestAllExtensions{},
41 &testeditionspb.TestAllTypes{},
42 }
43 }
44 for _, m := range messages {
45 in.Build(m.ProtoReflect())
46 }
47 return messages
48 }
49
50 func templateMessages(messages ...proto.Message) []protoreflect.MessageType {
51 if len(messages) == 0 {
52 messages = []proto.Message{
53 (*testpb.TestAllTypes)(nil),
54 (*test3pb.TestAllTypes)(nil),
55 (*testpb.TestAllExtensions)(nil),
56 (*testeditionspb.TestAllTypes)(nil),
57 }
58 }
59 var out []protoreflect.MessageType
60 for _, m := range messages {
61 out = append(out, m.ProtoReflect().Type())
62 }
63 return out
64
65 }
66
67 var testValidMessages = []testProto{
68 {
69 desc: "basic scalar types",
70 checkFastInit: true,
71 decodeTo: makeMessages(protobuild.Message{
72 "optional_int32": 1001,
73 "optional_int64": 1002,
74 "optional_uint32": 1003,
75 "optional_uint64": 1004,
76 "optional_sint32": 1005,
77 "optional_sint64": 1006,
78 "optional_fixed32": 1007,
79 "optional_fixed64": 1008,
80 "optional_sfixed32": 1009,
81 "optional_sfixed64": 1010,
82 "optional_float": 1011.5,
83 "optional_double": 1012.5,
84 "optional_bool": true,
85 "optional_string": "string",
86 "optional_bytes": []byte("bytes"),
87 "optional_nested_enum": "BAR",
88 }),
89 wire: protopack.Message{
90 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1001),
91 protopack.Tag{2, protopack.VarintType}, protopack.Varint(1002),
92 protopack.Tag{3, protopack.VarintType}, protopack.Uvarint(1003),
93 protopack.Tag{4, protopack.VarintType}, protopack.Uvarint(1004),
94 protopack.Tag{5, protopack.VarintType}, protopack.Svarint(1005),
95 protopack.Tag{6, protopack.VarintType}, protopack.Svarint(1006),
96 protopack.Tag{7, protopack.Fixed32Type}, protopack.Uint32(1007),
97 protopack.Tag{8, protopack.Fixed64Type}, protopack.Uint64(1008),
98 protopack.Tag{9, protopack.Fixed32Type}, protopack.Int32(1009),
99 protopack.Tag{10, protopack.Fixed64Type}, protopack.Int64(1010),
100 protopack.Tag{11, protopack.Fixed32Type}, protopack.Float32(1011.5),
101 protopack.Tag{12, protopack.Fixed64Type}, protopack.Float64(1012.5),
102 protopack.Tag{13, protopack.VarintType}, protopack.Bool(true),
103 protopack.Tag{14, protopack.BytesType}, protopack.String("string"),
104 protopack.Tag{15, protopack.BytesType}, protopack.Bytes([]byte("bytes")),
105 protopack.Tag{21, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_BAR)),
106 }.Marshal(),
107 },
108
109 {
110 desc: "zero values",
111 decodeTo: makeMessages(protobuild.Message{
112 "optional_int32": 0,
113 "optional_int64": 0,
114 "optional_uint32": 0,
115 "optional_uint64": 0,
116 "optional_sint32": 0,
117 "optional_sint64": 0,
118 "optional_fixed32": 0,
119 "optional_fixed64": 0,
120 "optional_sfixed32": 0,
121 "optional_sfixed64": 0,
122 "optional_float": 0,
123 "optional_double": 0,
124 "optional_bool": false,
125 "optional_string": "",
126 "optional_bytes": []byte{},
127 }),
128 wire: protopack.Message{
129 protopack.Tag{1, protopack.VarintType}, protopack.Varint(0),
130 protopack.Tag{2, protopack.VarintType}, protopack.Varint(0),
131 protopack.Tag{3, protopack.VarintType}, protopack.Uvarint(0),
132 protopack.Tag{4, protopack.VarintType}, protopack.Uvarint(0),
133 protopack.Tag{5, protopack.VarintType}, protopack.Svarint(0),
134 protopack.Tag{6, protopack.VarintType}, protopack.Svarint(0),
135 protopack.Tag{7, protopack.Fixed32Type}, protopack.Uint32(0),
136 protopack.Tag{8, protopack.Fixed64Type}, protopack.Uint64(0),
137 protopack.Tag{9, protopack.Fixed32Type}, protopack.Int32(0),
138 protopack.Tag{10, protopack.Fixed64Type}, protopack.Int64(0),
139 protopack.Tag{11, protopack.Fixed32Type}, protopack.Float32(0),
140 protopack.Tag{12, protopack.Fixed64Type}, protopack.Float64(0),
141 protopack.Tag{13, protopack.VarintType}, protopack.Bool(false),
142 protopack.Tag{14, protopack.BytesType}, protopack.String(""),
143 protopack.Tag{15, protopack.BytesType}, protopack.Bytes(nil),
144 }.Marshal(),
145 },
146
147 {
148 desc: "editions zero values on implicit fields",
149 decodeTo: makeMessages(protobuild.Message{
150 "singular_int32": 0,
151 "singular_int64": 0,
152 "singular_uint32": 0,
153 "singular_uint64": 0,
154 "singular_sint32": 0,
155 "singular_sint64": 0,
156 "singular_fixed32": 0,
157 "singular_fixed64": 0,
158 "singular_sfixed32": 0,
159 "singular_sfixed64": 0,
160 "singular_float": 0,
161 "singular_double": 0,
162 "singular_bool": false,
163 "singular_string": "",
164 "singular_bytes": []byte{},
165 }, &testeditionspb.TestAllTypes{}),
166 wire: protopack.Message{
167 protopack.Tag{124, protopack.VarintType}, protopack.Varint(0),
168 protopack.Tag{125, protopack.VarintType}, protopack.Varint(0),
169 protopack.Tag{126, protopack.VarintType}, protopack.Uvarint(0),
170 protopack.Tag{127, protopack.VarintType}, protopack.Uvarint(0),
171 protopack.Tag{128, protopack.VarintType}, protopack.Svarint(0),
172 protopack.Tag{129, protopack.VarintType}, protopack.Svarint(0),
173 protopack.Tag{130, protopack.Fixed32Type}, protopack.Uint32(0),
174 protopack.Tag{131, protopack.Fixed64Type}, protopack.Uint64(0),
175 protopack.Tag{132, protopack.Fixed32Type}, protopack.Int32(0),
176 protopack.Tag{133, protopack.Fixed64Type}, protopack.Int64(0),
177 protopack.Tag{134, protopack.Fixed32Type}, protopack.Float32(0),
178 protopack.Tag{135, protopack.Fixed64Type}, protopack.Float64(0),
179 protopack.Tag{136, protopack.VarintType}, protopack.Bool(false),
180 protopack.Tag{137, protopack.BytesType}, protopack.String(""),
181 protopack.Tag{138, protopack.BytesType}, protopack.Bytes(nil),
182 }.Marshal(),
183 },
184
185 {
186 desc: "groups",
187 decodeTo: makeMessages(protobuild.Message{
188 "optionalgroup": protobuild.Message{
189 "a": 1017,
190 "same_field_number": 1016,
191 },
192 }, &testpb.TestAllTypes{}, &testpb.TestAllExtensions{}, &testeditionspb.TestAllTypes{}, &testeditionspb.TestAllExtensions{}),
193 wire: protopack.Message{
194 protopack.Tag{16, protopack.StartGroupType},
195 protopack.Tag{17, protopack.VarintType}, protopack.Varint(1017),
196 protopack.Tag{16, protopack.VarintType}, protopack.Varint(1016),
197 protopack.Tag{16, protopack.EndGroupType},
198 }.Marshal(),
199 },
200
201 {
202 desc: "groups (field overridden)",
203 decodeTo: makeMessages(protobuild.Message{
204 "optionalgroup": protobuild.Message{
205 "a": 2,
206 },
207 }, &testpb.TestAllTypes{}, &testpb.TestAllExtensions{}, &testeditionspb.TestAllTypes{}, &testeditionspb.TestAllExtensions{}),
208 wire: protopack.Message{
209 protopack.Tag{16, protopack.StartGroupType},
210 protopack.Tag{17, protopack.VarintType}, protopack.Varint(1),
211 protopack.Tag{16, protopack.EndGroupType},
212 protopack.Tag{16, protopack.StartGroupType},
213 protopack.Tag{17, protopack.VarintType}, protopack.Varint(2),
214 protopack.Tag{16, protopack.EndGroupType},
215 }.Marshal(),
216 },
217
218 {
219 desc: "messages",
220 decodeTo: makeMessages(protobuild.Message{
221 "optional_nested_message": protobuild.Message{
222 "a": 42,
223 "corecursive": protobuild.Message{
224 "optional_int32": 43,
225 },
226 },
227 }),
228 wire: protopack.Message{
229 protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
230 protopack.Tag{1, protopack.VarintType}, protopack.Varint(42),
231 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
232 protopack.Tag{1, protopack.VarintType}, protopack.Varint(43),
233 }),
234 }),
235 }.Marshal(),
236 },
237
238 {
239 desc: "messages (split across multiple tags)",
240 decodeTo: makeMessages(protobuild.Message{
241 "optional_nested_message": protobuild.Message{
242 "a": 42,
243 "corecursive": protobuild.Message{
244 "optional_int32": 43,
245 },
246 },
247 }),
248 wire: protopack.Message{
249 protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
250 protopack.Tag{1, protopack.VarintType}, protopack.Varint(42),
251 }),
252 protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
253 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
254 protopack.Tag{1, protopack.VarintType}, protopack.Varint(43),
255 }),
256 }),
257 }.Marshal(),
258 },
259
260 {
261 desc: "messages (field overridden)",
262 decodeTo: makeMessages(protobuild.Message{
263 "optional_nested_message": protobuild.Message{
264 "a": 2,
265 },
266 }),
267 wire: protopack.Message{
268 protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
269 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
270 }),
271 protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
272 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
273 }),
274 }.Marshal(),
275 },
276
277 {
278 desc: "basic repeated types",
279 decodeTo: makeMessages(protobuild.Message{
280 "repeated_int32": []int32{1001, 2001},
281 "repeated_int64": []int64{1002, 2002},
282 "repeated_uint32": []uint32{1003, 2003},
283 "repeated_uint64": []uint64{1004, 2004},
284 "repeated_sint32": []int32{1005, 2005},
285 "repeated_sint64": []int64{1006, 2006},
286 "repeated_fixed32": []uint32{1007, 2007},
287 "repeated_fixed64": []uint64{1008, 2008},
288 "repeated_sfixed32": []int32{1009, 2009},
289 "repeated_sfixed64": []int64{1010, 2010},
290 "repeated_float": []float32{1011.5, 2011.5},
291 "repeated_double": []float64{1012.5, 2012.5},
292 "repeated_bool": []bool{true, false},
293 "repeated_string": []string{"foo", "bar"},
294 "repeated_bytes": []string{"FOO", "BAR"},
295 "repeated_nested_enum": []string{"FOO", "BAR"},
296 }),
297 wire: protopack.Message{
298 protopack.Tag{31, protopack.VarintType}, protopack.Varint(1001),
299 protopack.Tag{31, protopack.VarintType}, protopack.Varint(2001),
300 protopack.Tag{32, protopack.VarintType}, protopack.Varint(1002),
301 protopack.Tag{32, protopack.VarintType}, protopack.Varint(2002),
302 protopack.Tag{33, protopack.VarintType}, protopack.Uvarint(1003),
303 protopack.Tag{33, protopack.VarintType}, protopack.Uvarint(2003),
304 protopack.Tag{34, protopack.VarintType}, protopack.Uvarint(1004),
305 protopack.Tag{34, protopack.VarintType}, protopack.Uvarint(2004),
306 protopack.Tag{35, protopack.VarintType}, protopack.Svarint(1005),
307 protopack.Tag{35, protopack.VarintType}, protopack.Svarint(2005),
308 protopack.Tag{36, protopack.VarintType}, protopack.Svarint(1006),
309 protopack.Tag{36, protopack.VarintType}, protopack.Svarint(2006),
310 protopack.Tag{37, protopack.Fixed32Type}, protopack.Uint32(1007),
311 protopack.Tag{37, protopack.Fixed32Type}, protopack.Uint32(2007),
312 protopack.Tag{38, protopack.Fixed64Type}, protopack.Uint64(1008),
313 protopack.Tag{38, protopack.Fixed64Type}, protopack.Uint64(2008),
314 protopack.Tag{39, protopack.Fixed32Type}, protopack.Int32(1009),
315 protopack.Tag{39, protopack.Fixed32Type}, protopack.Int32(2009),
316 protopack.Tag{40, protopack.Fixed64Type}, protopack.Int64(1010),
317 protopack.Tag{40, protopack.Fixed64Type}, protopack.Int64(2010),
318 protopack.Tag{41, protopack.Fixed32Type}, protopack.Float32(1011.5),
319 protopack.Tag{41, protopack.Fixed32Type}, protopack.Float32(2011.5),
320 protopack.Tag{42, protopack.Fixed64Type}, protopack.Float64(1012.5),
321 protopack.Tag{42, protopack.Fixed64Type}, protopack.Float64(2012.5),
322 protopack.Tag{43, protopack.VarintType}, protopack.Bool(true),
323 protopack.Tag{43, protopack.VarintType}, protopack.Bool(false),
324 protopack.Tag{44, protopack.BytesType}, protopack.String("foo"),
325 protopack.Tag{44, protopack.BytesType}, protopack.String("bar"),
326 protopack.Tag{45, protopack.BytesType}, protopack.Bytes([]byte("FOO")),
327 protopack.Tag{45, protopack.BytesType}, protopack.Bytes([]byte("BAR")),
328 protopack.Tag{51, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_FOO)),
329 protopack.Tag{51, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_BAR)),
330 }.Marshal(),
331 },
332
333 {
334 desc: "basic repeated types (packed encoding)",
335 decodeTo: makeMessages(protobuild.Message{
336 "repeated_int32": []int32{1001, 2001},
337 "repeated_int64": []int64{1002, 2002},
338 "repeated_uint32": []uint32{1003, 2003},
339 "repeated_uint64": []uint64{1004, 2004},
340 "repeated_sint32": []int32{1005, 2005},
341 "repeated_sint64": []int64{1006, 2006},
342 "repeated_fixed32": []uint32{1007, 2007},
343 "repeated_fixed64": []uint64{1008, 2008},
344 "repeated_sfixed32": []int32{1009, 2009},
345 "repeated_sfixed64": []int64{1010, 2010},
346 "repeated_float": []float32{1011.5, 2011.5},
347 "repeated_double": []float64{1012.5, 2012.5},
348 "repeated_bool": []bool{true, false},
349 "repeated_nested_enum": []string{"FOO", "BAR"},
350 }),
351 wire: protopack.Message{
352 protopack.Tag{31, protopack.BytesType}, protopack.LengthPrefix{
353 protopack.Varint(1001), protopack.Varint(2001),
354 },
355 protopack.Tag{32, protopack.BytesType}, protopack.LengthPrefix{
356 protopack.Varint(1002), protopack.Varint(2002),
357 },
358 protopack.Tag{33, protopack.BytesType}, protopack.LengthPrefix{
359 protopack.Uvarint(1003), protopack.Uvarint(2003),
360 },
361 protopack.Tag{34, protopack.BytesType}, protopack.LengthPrefix{
362 protopack.Uvarint(1004), protopack.Uvarint(2004),
363 },
364 protopack.Tag{35, protopack.BytesType}, protopack.LengthPrefix{
365 protopack.Svarint(1005), protopack.Svarint(2005),
366 },
367 protopack.Tag{36, protopack.BytesType}, protopack.LengthPrefix{
368 protopack.Svarint(1006), protopack.Svarint(2006),
369 },
370 protopack.Tag{37, protopack.BytesType}, protopack.LengthPrefix{
371 protopack.Uint32(1007), protopack.Uint32(2007),
372 },
373 protopack.Tag{38, protopack.BytesType}, protopack.LengthPrefix{
374 protopack.Uint64(1008), protopack.Uint64(2008),
375 },
376 protopack.Tag{39, protopack.BytesType}, protopack.LengthPrefix{
377 protopack.Int32(1009), protopack.Int32(2009),
378 },
379 protopack.Tag{40, protopack.BytesType}, protopack.LengthPrefix{
380 protopack.Int64(1010), protopack.Int64(2010),
381 },
382 protopack.Tag{41, protopack.BytesType}, protopack.LengthPrefix{
383 protopack.Float32(1011.5), protopack.Float32(2011.5),
384 },
385 protopack.Tag{42, protopack.BytesType}, protopack.LengthPrefix{
386 protopack.Float64(1012.5), protopack.Float64(2012.5),
387 },
388 protopack.Tag{43, protopack.BytesType}, protopack.LengthPrefix{
389 protopack.Bool(true), protopack.Bool(false),
390 },
391 protopack.Tag{51, protopack.BytesType}, protopack.LengthPrefix{
392 protopack.Varint(int(testpb.TestAllTypes_FOO)),
393 protopack.Varint(int(testpb.TestAllTypes_BAR)),
394 },
395 }.Marshal(),
396 },
397
398 {
399 desc: "basic repeated types (zero-length packed encoding)",
400 decodeTo: makeMessages(protobuild.Message{
401 "repeated_int32": []int32{},
402 "repeated_int64": []int64{},
403 "repeated_uint32": []uint32{},
404 "repeated_uint64": []uint64{},
405 "repeated_sint32": []int32{},
406 "repeated_sint64": []int64{},
407 "repeated_fixed32": []uint32{},
408 "repeated_fixed64": []uint64{},
409 "repeated_sfixed32": []int32{},
410 "repeated_sfixed64": []int64{},
411 "repeated_float": []float32{},
412 "repeated_double": []float64{},
413 "repeated_bool": []bool{},
414 "repeated_nested_enum": []string{},
415 }),
416 wire: protopack.Message{
417 protopack.Tag{31, protopack.BytesType}, protopack.LengthPrefix{},
418 protopack.Tag{32, protopack.BytesType}, protopack.LengthPrefix{},
419 protopack.Tag{33, protopack.BytesType}, protopack.LengthPrefix{},
420 protopack.Tag{34, protopack.BytesType}, protopack.LengthPrefix{},
421 protopack.Tag{35, protopack.BytesType}, protopack.LengthPrefix{},
422 protopack.Tag{36, protopack.BytesType}, protopack.LengthPrefix{},
423 protopack.Tag{37, protopack.BytesType}, protopack.LengthPrefix{},
424 protopack.Tag{38, protopack.BytesType}, protopack.LengthPrefix{},
425 protopack.Tag{39, protopack.BytesType}, protopack.LengthPrefix{},
426 protopack.Tag{40, protopack.BytesType}, protopack.LengthPrefix{},
427 protopack.Tag{41, protopack.BytesType}, protopack.LengthPrefix{},
428 protopack.Tag{42, protopack.BytesType}, protopack.LengthPrefix{},
429 protopack.Tag{43, protopack.BytesType}, protopack.LengthPrefix{},
430 protopack.Tag{51, protopack.BytesType}, protopack.LengthPrefix{},
431 }.Marshal(),
432 },
433
434 {
435 desc: "packed repeated types",
436 decodeTo: makeMessages(protobuild.Message{
437 "packed_int32": []int32{1001, 2001},
438 "packed_int64": []int64{1002, 2002},
439 "packed_uint32": []uint32{1003, 2003},
440 "packed_uint64": []uint64{1004, 2004},
441 "packed_sint32": []int32{1005, 2005},
442 "packed_sint64": []int64{1006, 2006},
443 "packed_fixed32": []uint32{1007, 2007},
444 "packed_fixed64": []uint64{1008, 2008},
445 "packed_sfixed32": []int32{1009, 2009},
446 "packed_sfixed64": []int64{1010, 2010},
447 "packed_float": []float32{1011.5, 2011.5},
448 "packed_double": []float64{1012.5, 2012.5},
449 "packed_bool": []bool{true, false},
450 "packed_enum": []string{"FOREIGN_FOO", "FOREIGN_BAR"},
451 }, &testpb.TestPackedTypes{}, &testpb.TestPackedExtensions{}),
452 wire: protopack.Message{
453 protopack.Tag{90, protopack.BytesType}, protopack.LengthPrefix{
454 protopack.Varint(1001), protopack.Varint(2001),
455 },
456 protopack.Tag{91, protopack.BytesType}, protopack.LengthPrefix{
457 protopack.Varint(1002), protopack.Varint(2002),
458 },
459 protopack.Tag{92, protopack.BytesType}, protopack.LengthPrefix{
460 protopack.Uvarint(1003), protopack.Uvarint(2003),
461 },
462 protopack.Tag{93, protopack.BytesType}, protopack.LengthPrefix{
463 protopack.Uvarint(1004), protopack.Uvarint(2004),
464 },
465 protopack.Tag{94, protopack.BytesType}, protopack.LengthPrefix{
466 protopack.Svarint(1005), protopack.Svarint(2005),
467 },
468 protopack.Tag{95, protopack.BytesType}, protopack.LengthPrefix{
469 protopack.Svarint(1006), protopack.Svarint(2006),
470 },
471 protopack.Tag{96, protopack.BytesType}, protopack.LengthPrefix{
472 protopack.Uint32(1007), protopack.Uint32(2007),
473 },
474 protopack.Tag{97, protopack.BytesType}, protopack.LengthPrefix{
475 protopack.Uint64(1008), protopack.Uint64(2008),
476 },
477 protopack.Tag{98, protopack.BytesType}, protopack.LengthPrefix{
478 protopack.Int32(1009), protopack.Int32(2009),
479 },
480 protopack.Tag{99, protopack.BytesType}, protopack.LengthPrefix{
481 protopack.Int64(1010), protopack.Int64(2010),
482 },
483 protopack.Tag{100, protopack.BytesType}, protopack.LengthPrefix{
484 protopack.Float32(1011.5), protopack.Float32(2011.5),
485 },
486 protopack.Tag{101, protopack.BytesType}, protopack.LengthPrefix{
487 protopack.Float64(1012.5), protopack.Float64(2012.5),
488 },
489 protopack.Tag{102, protopack.BytesType}, protopack.LengthPrefix{
490 protopack.Bool(true), protopack.Bool(false),
491 },
492 protopack.Tag{103, protopack.BytesType}, protopack.LengthPrefix{
493 protopack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
494 protopack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
495 },
496 }.Marshal(),
497 },
498
499 {
500 desc: "packed repeated types (zero length)",
501 decodeTo: makeMessages(protobuild.Message{
502 "packed_int32": []int32{},
503 "packed_int64": []int64{},
504 "packed_uint32": []uint32{},
505 "packed_uint64": []uint64{},
506 "packed_sint32": []int32{},
507 "packed_sint64": []int64{},
508 "packed_fixed32": []uint32{},
509 "packed_fixed64": []uint64{},
510 "packed_sfixed32": []int32{},
511 "packed_sfixed64": []int64{},
512 "packed_float": []float32{},
513 "packed_double": []float64{},
514 "packed_bool": []bool{},
515 "packed_enum": []string{},
516 }, &testpb.TestPackedTypes{}, &testpb.TestPackedExtensions{}, &testeditionspb.TestPackedTypes{}, &testeditionspb.TestPackedExtensions{}),
517 wire: protopack.Message{
518 protopack.Tag{90, protopack.BytesType}, protopack.LengthPrefix{},
519 protopack.Tag{91, protopack.BytesType}, protopack.LengthPrefix{},
520 protopack.Tag{92, protopack.BytesType}, protopack.LengthPrefix{},
521 protopack.Tag{93, protopack.BytesType}, protopack.LengthPrefix{},
522 protopack.Tag{94, protopack.BytesType}, protopack.LengthPrefix{},
523 protopack.Tag{95, protopack.BytesType}, protopack.LengthPrefix{},
524 protopack.Tag{96, protopack.BytesType}, protopack.LengthPrefix{},
525 protopack.Tag{97, protopack.BytesType}, protopack.LengthPrefix{},
526 protopack.Tag{98, protopack.BytesType}, protopack.LengthPrefix{},
527 protopack.Tag{99, protopack.BytesType}, protopack.LengthPrefix{},
528 protopack.Tag{100, protopack.BytesType}, protopack.LengthPrefix{},
529 protopack.Tag{101, protopack.BytesType}, protopack.LengthPrefix{},
530 protopack.Tag{102, protopack.BytesType}, protopack.LengthPrefix{},
531 protopack.Tag{103, protopack.BytesType}, protopack.LengthPrefix{},
532 }.Marshal(),
533 },
534
535 {
536 desc: "repeated messages",
537 decodeTo: makeMessages(protobuild.Message{
538 "repeated_nested_message": []protobuild.Message{
539 {"a": 1},
540 {},
541 {"a": 2},
542 },
543 }),
544 wire: protopack.Message{
545 protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
546 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
547 }),
548 protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
549 protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
550 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
551 }),
552 }.Marshal(),
553 },
554
555 {
556 desc: "repeated nil messages",
557 decodeTo: []proto.Message{&testpb.TestAllTypes{
558 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
559 {A: proto.Int32(1)},
560 nil,
561 {A: proto.Int32(2)},
562 },
563 }, &testeditionspb.TestAllTypes{
564 RepeatedNestedMessage: []*testeditionspb.TestAllTypes_NestedMessage{
565 {A: proto.Int32(1)},
566 nil,
567 {A: proto.Int32(2)},
568 },
569 }, &test3pb.TestAllTypes{
570 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
571 {A: 1},
572 nil,
573 {A: 2},
574 },
575 }, build(
576 &testpb.TestAllExtensions{},
577 extend(testpb.E_RepeatedNestedMessage, []*testpb.TestAllExtensions_NestedMessage{
578 {A: proto.Int32(1)},
579 nil,
580 {A: proto.Int32(2)},
581 }),
582 ), build(
583 &testeditionspb.TestAllExtensions{},
584 extend(testeditionspb.E_RepeatedNestedMessage, []*testeditionspb.TestAllExtensions_NestedMessage{
585 {A: proto.Int32(1)},
586 nil,
587 {A: proto.Int32(2)},
588 }),
589 )},
590 wire: protopack.Message{
591 protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
592 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
593 }),
594 protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
595 protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
596 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
597 }),
598 }.Marshal(),
599 },
600
601 {
602 desc: "repeated groups",
603 decodeTo: makeMessages(protobuild.Message{
604 "repeatedgroup": []protobuild.Message{
605 {"a": 1017},
606 {},
607 {"a": 2017},
608 },
609 }, &testpb.TestAllTypes{}, &testpb.TestAllExtensions{}, &testeditionspb.TestAllTypes{}, &testeditionspb.TestAllExtensions{}),
610 wire: protopack.Message{
611 protopack.Tag{46, protopack.StartGroupType},
612 protopack.Tag{47, protopack.VarintType}, protopack.Varint(1017),
613 protopack.Tag{46, protopack.EndGroupType},
614 protopack.Tag{46, protopack.StartGroupType},
615 protopack.Tag{46, protopack.EndGroupType},
616 protopack.Tag{46, protopack.StartGroupType},
617 protopack.Tag{47, protopack.VarintType}, protopack.Varint(2017),
618 protopack.Tag{46, protopack.EndGroupType},
619 }.Marshal(),
620 },
621
622 {
623 desc: "repeated nil groups",
624 decodeTo: []proto.Message{&testpb.TestAllTypes{
625 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
626 {A: proto.Int32(1017)},
627 nil,
628 {A: proto.Int32(2017)},
629 },
630 }, &testeditionspb.TestAllTypes{
631 Repeatedgroup: []*testeditionspb.TestAllTypes_RepeatedGroup{
632 {A: proto.Int32(1017)},
633 nil,
634 {A: proto.Int32(2017)},
635 },
636 }, build(
637 &testpb.TestAllExtensions{},
638 extend(testpb.E_Repeatedgroup, []*testpb.RepeatedGroup{
639 {A: proto.Int32(1017)},
640 nil,
641 {A: proto.Int32(2017)},
642 }),
643 ), build(
644 &testeditionspb.TestAllExtensions{},
645 extend(testeditionspb.E_Repeatedgroup, []*testeditionspb.RepeatedGroup{
646 {A: proto.Int32(1017)},
647 nil,
648 {A: proto.Int32(2017)},
649 }),
650 )},
651 wire: protopack.Message{
652 protopack.Tag{46, protopack.StartGroupType},
653 protopack.Tag{47, protopack.VarintType}, protopack.Varint(1017),
654 protopack.Tag{46, protopack.EndGroupType},
655 protopack.Tag{46, protopack.StartGroupType},
656 protopack.Tag{46, protopack.EndGroupType},
657 protopack.Tag{46, protopack.StartGroupType},
658 protopack.Tag{47, protopack.VarintType}, protopack.Varint(2017),
659 protopack.Tag{46, protopack.EndGroupType},
660 }.Marshal(),
661 },
662
663 {
664 desc: "maps",
665 decodeTo: makeMessages(protobuild.Message{
666 "map_int32_int32": map[int32]int32{1056: 1156, 2056: 2156},
667 "map_int64_int64": map[int64]int64{1057: 1157, 2057: 2157},
668 "map_uint32_uint32": map[uint32]uint32{1058: 1158, 2058: 2158},
669 "map_uint64_uint64": map[uint64]uint64{1059: 1159, 2059: 2159},
670 "map_sint32_sint32": map[int32]int32{1060: 1160, 2060: 2160},
671 "map_sint64_sint64": map[int64]int64{1061: 1161, 2061: 2161},
672 "map_fixed32_fixed32": map[uint32]uint32{1062: 1162, 2062: 2162},
673 "map_fixed64_fixed64": map[uint64]uint64{1063: 1163, 2063: 2163},
674 "map_sfixed32_sfixed32": map[int32]int32{1064: 1164, 2064: 2164},
675 "map_sfixed64_sfixed64": map[int64]int64{1065: 1165, 2065: 2165},
676 "map_int32_float": map[int32]float32{1066: 1166.5, 2066: 2166.5},
677 "map_int32_double": map[int32]float64{1067: 1167.5, 2067: 2167.5},
678 "map_bool_bool": map[bool]bool{true: false, false: true},
679 "map_string_string": map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
680 "map_string_bytes": map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
681 "map_string_nested_message": map[string]protobuild.Message{
682 "71.1.key": {"a": 1171},
683 "71.2.key": {"a": 2171},
684 },
685 "map_string_nested_enum": map[string]string{"73.1.key": "FOO", "73.2.key": "BAR"},
686 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
687 wire: protopack.Message{
688 protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
689 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1056),
690 protopack.Tag{2, protopack.VarintType}, protopack.Varint(1156),
691 }),
692 protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
693 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2056),
694 protopack.Tag{2, protopack.VarintType}, protopack.Varint(2156),
695 }),
696 protopack.Tag{57, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
697 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1057),
698 protopack.Tag{2, protopack.VarintType}, protopack.Varint(1157),
699 }),
700 protopack.Tag{57, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
701 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2057),
702 protopack.Tag{2, protopack.VarintType}, protopack.Varint(2157),
703 }),
704 protopack.Tag{58, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
705 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1058),
706 protopack.Tag{2, protopack.VarintType}, protopack.Varint(1158),
707 }),
708 protopack.Tag{58, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
709 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2058),
710 protopack.Tag{2, protopack.VarintType}, protopack.Varint(2158),
711 }),
712 protopack.Tag{59, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
713 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1059),
714 protopack.Tag{2, protopack.VarintType}, protopack.Varint(1159),
715 }),
716 protopack.Tag{59, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
717 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2059),
718 protopack.Tag{2, protopack.VarintType}, protopack.Varint(2159),
719 }),
720 protopack.Tag{60, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
721 protopack.Tag{1, protopack.VarintType}, protopack.Svarint(1060),
722 protopack.Tag{2, protopack.VarintType}, protopack.Svarint(1160),
723 }),
724 protopack.Tag{60, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
725 protopack.Tag{1, protopack.VarintType}, protopack.Svarint(2060),
726 protopack.Tag{2, protopack.VarintType}, protopack.Svarint(2160),
727 }),
728 protopack.Tag{61, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
729 protopack.Tag{1, protopack.VarintType}, protopack.Svarint(1061),
730 protopack.Tag{2, protopack.VarintType}, protopack.Svarint(1161),
731 }),
732 protopack.Tag{61, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
733 protopack.Tag{1, protopack.VarintType}, protopack.Svarint(2061),
734 protopack.Tag{2, protopack.VarintType}, protopack.Svarint(2161),
735 }),
736 protopack.Tag{62, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
737 protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(1062),
738 protopack.Tag{2, protopack.Fixed32Type}, protopack.Int32(1162),
739 }),
740 protopack.Tag{62, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
741 protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(2062),
742 protopack.Tag{2, protopack.Fixed32Type}, protopack.Int32(2162),
743 }),
744 protopack.Tag{63, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
745 protopack.Tag{1, protopack.Fixed64Type}, protopack.Int64(1063),
746 protopack.Tag{2, protopack.Fixed64Type}, protopack.Int64(1163),
747 }),
748 protopack.Tag{63, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
749 protopack.Tag{1, protopack.Fixed64Type}, protopack.Int64(2063),
750 protopack.Tag{2, protopack.Fixed64Type}, protopack.Int64(2163),
751 }),
752 protopack.Tag{64, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
753 protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(1064),
754 protopack.Tag{2, protopack.Fixed32Type}, protopack.Int32(1164),
755 }),
756 protopack.Tag{64, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
757 protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(2064),
758 protopack.Tag{2, protopack.Fixed32Type}, protopack.Int32(2164),
759 }),
760 protopack.Tag{65, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
761 protopack.Tag{1, protopack.Fixed64Type}, protopack.Int64(1065),
762 protopack.Tag{2, protopack.Fixed64Type}, protopack.Int64(1165),
763 }),
764 protopack.Tag{65, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
765 protopack.Tag{1, protopack.Fixed64Type}, protopack.Int64(2065),
766 protopack.Tag{2, protopack.Fixed64Type}, protopack.Int64(2165),
767 }),
768 protopack.Tag{66, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
769 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1066),
770 protopack.Tag{2, protopack.Fixed32Type}, protopack.Float32(1166.5),
771 }),
772 protopack.Tag{66, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
773 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2066),
774 protopack.Tag{2, protopack.Fixed32Type}, protopack.Float32(2166.5),
775 }),
776 protopack.Tag{67, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
777 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1067),
778 protopack.Tag{2, protopack.Fixed64Type}, protopack.Float64(1167.5),
779 }),
780 protopack.Tag{67, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
781 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2067),
782 protopack.Tag{2, protopack.Fixed64Type}, protopack.Float64(2167.5),
783 }),
784 protopack.Tag{68, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
785 protopack.Tag{1, protopack.VarintType}, protopack.Bool(true),
786 protopack.Tag{2, protopack.VarintType}, protopack.Bool(false),
787 }),
788 protopack.Tag{68, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
789 protopack.Tag{1, protopack.VarintType}, protopack.Bool(false),
790 protopack.Tag{2, protopack.VarintType}, protopack.Bool(true),
791 }),
792 protopack.Tag{69, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
793 protopack.Tag{1, protopack.BytesType}, protopack.String("69.1.key"),
794 protopack.Tag{2, protopack.BytesType}, protopack.String("69.1.val"),
795 }),
796 protopack.Tag{69, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
797 protopack.Tag{1, protopack.BytesType}, protopack.String("69.2.key"),
798 protopack.Tag{2, protopack.BytesType}, protopack.String("69.2.val"),
799 }),
800 protopack.Tag{70, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
801 protopack.Tag{1, protopack.BytesType}, protopack.String("70.1.key"),
802 protopack.Tag{2, protopack.BytesType}, protopack.String("70.1.val"),
803 }),
804 protopack.Tag{70, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
805 protopack.Tag{1, protopack.BytesType}, protopack.String("70.2.key"),
806 protopack.Tag{2, protopack.BytesType}, protopack.String("70.2.val"),
807 }),
808 protopack.Tag{71, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
809 protopack.Tag{1, protopack.BytesType}, protopack.String("71.1.key"),
810 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
811 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1171),
812 }),
813 }),
814 protopack.Tag{71, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
815 protopack.Tag{1, protopack.BytesType}, protopack.String("71.2.key"),
816 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
817 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2171),
818 }),
819 }),
820 protopack.Tag{73, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
821 protopack.Tag{1, protopack.BytesType}, protopack.String("73.1.key"),
822 protopack.Tag{2, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_FOO)),
823 }),
824 protopack.Tag{73, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
825 protopack.Tag{1, protopack.BytesType}, protopack.String("73.2.key"),
826 protopack.Tag{2, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_BAR)),
827 }),
828 }.Marshal(),
829 },
830
831 {
832 desc: "map with value before key",
833 decodeTo: makeMessages(protobuild.Message{
834 "map_int32_int32": map[int32]int32{1056: 1156},
835 "map_string_nested_message": map[string]protobuild.Message{
836 "71.1.key": {"a": 1171},
837 },
838 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
839 wire: protopack.Message{
840 protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
841 protopack.Tag{2, protopack.VarintType}, protopack.Varint(1156),
842 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1056),
843 }),
844 protopack.Tag{71, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
845 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
846 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1171),
847 }),
848 protopack.Tag{1, protopack.BytesType}, protopack.String("71.1.key"),
849 }),
850 }.Marshal(),
851 },
852
853 {
854 desc: "map with repeated key and value",
855 decodeTo: makeMessages(protobuild.Message{
856 "map_int32_int32": map[int32]int32{1056: 1156},
857 "map_string_nested_message": map[string]protobuild.Message{
858 "71.1.key": {"a": 1171},
859 },
860 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
861 wire: protopack.Message{
862 protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
863 protopack.Tag{1, protopack.VarintType}, protopack.Varint(0),
864 protopack.Tag{2, protopack.VarintType}, protopack.Varint(0),
865 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1056),
866 protopack.Tag{2, protopack.VarintType}, protopack.Varint(1156),
867 }),
868 protopack.Tag{71, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
869 protopack.Tag{1, protopack.BytesType}, protopack.String(""),
870 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
871 protopack.Tag{1, protopack.BytesType}, protopack.String("71.1.key"),
872 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
873 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1171),
874 }),
875 }),
876 }.Marshal(),
877 },
878
879 {
880 desc: "oneof (uint32)",
881 decodeTo: makeMessages(protobuild.Message{
882 "oneof_uint32": 1111,
883 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
884 wire: protopack.Message{protopack.Tag{111, protopack.VarintType}, protopack.Varint(1111)}.Marshal(),
885 },
886
887 {
888 desc: "oneof (message)",
889 decodeTo: makeMessages(protobuild.Message{
890 "oneof_nested_message": protobuild.Message{
891 "a": 1112,
892 },
893 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
894 wire: protopack.Message{protopack.Tag{112, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
895 protopack.Message{protopack.Tag{1, protopack.VarintType}, protopack.Varint(1112)},
896 })}.Marshal(),
897 },
898
899 {
900 desc: "oneof (empty message)",
901 decodeTo: makeMessages(protobuild.Message{
902 "oneof_nested_message": protobuild.Message{},
903 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
904 wire: protopack.Message{protopack.Tag{112, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{})}.Marshal(),
905 },
906
907 {
908 desc: "oneof (merged message)",
909 decodeTo: makeMessages(protobuild.Message{
910 "oneof_nested_message": protobuild.Message{
911 "a": 1,
912 "corecursive": protobuild.Message{
913 "optional_int32": 43,
914 },
915 },
916 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
917 wire: protopack.Message{
918 protopack.Tag{112, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
919 protopack.Message{protopack.Tag{1, protopack.VarintType}, protopack.Varint(1)},
920 }),
921 protopack.Tag{112, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
922 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
923 protopack.Tag{1, protopack.VarintType}, protopack.Varint(43),
924 }),
925 }),
926 }.Marshal(),
927 },
928
929 {
930 desc: "oneof (group)",
931 decodeTo: makeMessages(protobuild.Message{
932 "oneofgroup": protobuild.Message{
933 "a": 1,
934 },
935 }, &testpb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
936 wire: protopack.Message{
937 protopack.Tag{121, protopack.StartGroupType},
938 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
939 protopack.Tag{121, protopack.EndGroupType},
940 }.Marshal(),
941 },
942
943 {
944 desc: "oneof (empty group)",
945 decodeTo: makeMessages(protobuild.Message{
946 "oneofgroup": protobuild.Message{},
947 }, &testpb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
948 wire: protopack.Message{
949 protopack.Tag{121, protopack.StartGroupType},
950 protopack.Tag{121, protopack.EndGroupType},
951 }.Marshal(),
952 },
953
954 {
955 desc: "oneof (merged group)",
956 decodeTo: makeMessages(protobuild.Message{
957 "oneofgroup": protobuild.Message{
958 "a": 1,
959 "b": 2,
960 },
961 }, &testpb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
962 wire: protopack.Message{
963 protopack.Tag{121, protopack.StartGroupType},
964 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
965 protopack.Tag{121, protopack.EndGroupType},
966 protopack.Tag{121, protopack.StartGroupType},
967 protopack.Tag{2, protopack.VarintType}, protopack.Varint(2),
968 protopack.Tag{121, protopack.EndGroupType},
969 }.Marshal(),
970 },
971
972 {
973 desc: "oneof (string)",
974 decodeTo: makeMessages(protobuild.Message{
975 "oneof_string": "1113",
976 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
977 wire: protopack.Message{protopack.Tag{113, protopack.BytesType}, protopack.String("1113")}.Marshal(),
978 },
979
980 {
981 desc: "oneof (bytes)",
982 decodeTo: makeMessages(protobuild.Message{
983 "oneof_bytes": "1114",
984 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
985 wire: protopack.Message{protopack.Tag{114, protopack.BytesType}, protopack.String("1114")}.Marshal(),
986 },
987
988 {
989 desc: "oneof (bool)",
990 decodeTo: makeMessages(protobuild.Message{
991 "oneof_bool": true,
992 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
993 wire: protopack.Message{protopack.Tag{115, protopack.VarintType}, protopack.Bool(true)}.Marshal(),
994 },
995
996 {
997 desc: "oneof (uint64)",
998 decodeTo: makeMessages(protobuild.Message{
999 "oneof_uint64": 116,
1000 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
1001 wire: protopack.Message{protopack.Tag{116, protopack.VarintType}, protopack.Varint(116)}.Marshal(),
1002 },
1003
1004 {
1005 desc: "oneof (float)",
1006 decodeTo: makeMessages(protobuild.Message{
1007 "oneof_float": 117.5,
1008 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
1009 wire: protopack.Message{protopack.Tag{117, protopack.Fixed32Type}, protopack.Float32(117.5)}.Marshal(),
1010 },
1011
1012 {
1013 desc: "oneof (double)",
1014 decodeTo: makeMessages(protobuild.Message{
1015 "oneof_double": 118.5,
1016 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
1017 wire: protopack.Message{protopack.Tag{118, protopack.Fixed64Type}, protopack.Float64(118.5)}.Marshal(),
1018 },
1019
1020 {
1021 desc: "oneof (enum)",
1022 decodeTo: makeMessages(protobuild.Message{
1023 "oneof_enum": "BAR",
1024 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
1025 wire: protopack.Message{protopack.Tag{119, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
1026 },
1027
1028 {
1029 desc: "oneof (zero)",
1030 decodeTo: makeMessages(protobuild.Message{
1031 "oneof_uint64": 0,
1032 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
1033 wire: protopack.Message{protopack.Tag{116, protopack.VarintType}, protopack.Varint(0)}.Marshal(),
1034 },
1035
1036 {
1037 desc: "oneof (overridden value)",
1038 decodeTo: makeMessages(protobuild.Message{
1039 "oneof_uint64": 2,
1040 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
1041 wire: protopack.Message{
1042 protopack.Tag{111, protopack.VarintType}, protopack.Varint(1),
1043 protopack.Tag{116, protopack.VarintType}, protopack.Varint(2),
1044 }.Marshal(),
1045 },
1046
1047
1048
1049
1050
1051
1052
1053 {
1054 desc: "unknown fields",
1055 checkFastInit: true,
1056 decodeTo: makeMessages(protobuild.Message{
1057 protobuild.Unknown: protopack.Message{
1058 protopack.Tag{100000, protopack.VarintType}, protopack.Varint(1),
1059 }.Marshal(),
1060 }),
1061 wire: protopack.Message{
1062 protopack.Tag{100000, protopack.VarintType}, protopack.Varint(1),
1063 }.Marshal(),
1064 },
1065
1066 {
1067 desc: "discarded unknown fields",
1068 unmarshalOptions: proto.UnmarshalOptions{
1069 DiscardUnknown: true,
1070 },
1071 decodeTo: makeMessages(protobuild.Message{}),
1072 wire: protopack.Message{
1073 protopack.Tag{100000, protopack.VarintType}, protopack.Varint(1),
1074 }.Marshal(),
1075 },
1076
1077 {
1078 desc: "field type mismatch",
1079 decodeTo: makeMessages(protobuild.Message{
1080 protobuild.Unknown: protopack.Message{
1081 protopack.Tag{1, protopack.BytesType}, protopack.String("string"),
1082 }.Marshal(),
1083 }),
1084 wire: protopack.Message{
1085 protopack.Tag{1, protopack.BytesType}, protopack.String("string"),
1086 }.Marshal(),
1087 },
1088
1089 {
1090 desc: "map field element mismatch",
1091 decodeTo: makeMessages(protobuild.Message{
1092 "map_int32_int32": map[int32]int32{1: 0},
1093 }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
1094 wire: protopack.Message{
1095 protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1096 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1097 protopack.Tag{2, protopack.BytesType}, protopack.String("string"),
1098 }),
1099 }.Marshal(),
1100 },
1101
1102 {
1103 desc: "required field in nil message unset",
1104 checkFastInit: true,
1105 partial: true,
1106 decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
1107 },
1108
1109 {
1110 desc: "required int32 unset",
1111 checkFastInit: true,
1112 partial: true,
1113 decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Int32{}),
1114 },
1115
1116 {
1117 desc: "required int32 set",
1118 checkFastInit: true,
1119 decodeTo: makeMessages(protobuild.Message{
1120 "v": 1,
1121 }, &requiredpb.Int32{}),
1122 wire: protopack.Message{
1123 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1124 }.Marshal(),
1125 },
1126
1127 {
1128 desc: "required fixed32 unset",
1129 checkFastInit: true,
1130 partial: true,
1131 decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Fixed32{}),
1132 },
1133
1134 {
1135 desc: "required fixed32 set",
1136 checkFastInit: true,
1137 decodeTo: makeMessages(protobuild.Message{
1138 "v": 1,
1139 }, &requiredpb.Fixed32{}),
1140 wire: protopack.Message{
1141 protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(1),
1142 }.Marshal(),
1143 },
1144
1145 {
1146 desc: "required fixed64 unset",
1147 checkFastInit: true,
1148 partial: true,
1149 decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Fixed64{}),
1150 },
1151
1152 {
1153 desc: "required fixed64 set",
1154 checkFastInit: true,
1155 decodeTo: makeMessages(protobuild.Message{
1156 "v": 1,
1157 }, &requiredpb.Fixed64{}),
1158 wire: protopack.Message{
1159 protopack.Tag{1, protopack.Fixed64Type}, protopack.Int64(1),
1160 }.Marshal(),
1161 },
1162
1163 {
1164 desc: "required bytes unset",
1165 checkFastInit: true,
1166 partial: true,
1167 decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Bytes{}),
1168 },
1169
1170 {
1171 desc: "required bytes set",
1172 checkFastInit: true,
1173 decodeTo: makeMessages(protobuild.Message{
1174 "v": "",
1175 }, &requiredpb.Bytes{}),
1176 wire: protopack.Message{
1177 protopack.Tag{1, protopack.BytesType}, protopack.Bytes(nil),
1178 }.Marshal(),
1179 },
1180
1181 {
1182 desc: "required message unset",
1183 checkFastInit: true,
1184 partial: true,
1185 decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Message{}),
1186 },
1187
1188 {
1189 desc: "required message set",
1190 checkFastInit: true,
1191 decodeTo: makeMessages(protobuild.Message{
1192 "v": protobuild.Message{},
1193 }, &requiredpb.Message{}),
1194 wire: protopack.Message{
1195 protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
1196 }.Marshal(),
1197 },
1198
1199 {
1200 desc: "required group unset",
1201 checkFastInit: true,
1202 partial: true,
1203 decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Group{}),
1204 },
1205
1206 {
1207 desc: "required group set",
1208 checkFastInit: true,
1209 decodeTo: makeMessages(protobuild.Message{
1210 "group": protobuild.Message{},
1211 }, &requiredpb.Group{}),
1212 wire: protopack.Message{
1213 protopack.Tag{1, protopack.StartGroupType},
1214 protopack.Tag{1, protopack.EndGroupType},
1215 }.Marshal(),
1216 },
1217
1218 {
1219 desc: "required field with incompatible wire type",
1220 checkFastInit: true,
1221 partial: true,
1222 decodeTo: []proto.Message{build(
1223 &testpb.TestRequired{},
1224 unknown(protopack.Message{
1225 protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(2),
1226 }.Marshal()),
1227 ), build(
1228 &testeditionspb.TestRequired{},
1229 unknown(protopack.Message{
1230 protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(2),
1231 }.Marshal()),
1232 )},
1233 wire: protopack.Message{
1234 protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(2),
1235 }.Marshal(),
1236 },
1237
1238 {
1239 desc: "required field in optional message unset",
1240 checkFastInit: true,
1241 partial: true,
1242 decodeTo: makeMessages(protobuild.Message{
1243 "optional_message": protobuild.Message{},
1244 }, &testpb.TestRequiredForeign{}, &testeditionspb.TestRequiredForeign{}),
1245 wire: protopack.Message{
1246 protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
1247 }.Marshal(),
1248 },
1249
1250 {
1251 desc: "required field in optional message set",
1252 checkFastInit: true,
1253 decodeTo: makeMessages(protobuild.Message{
1254 "optional_message": protobuild.Message{
1255 "required_field": 1,
1256 },
1257 }, &testpb.TestRequiredForeign{}, &testeditionspb.TestRequiredForeign{}),
1258 wire: protopack.Message{
1259 protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1260 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1261 }),
1262 }.Marshal(),
1263 },
1264
1265 {
1266 desc: "required field in optional message set (split across multiple tags)",
1267 checkFastInit: false,
1268 nocheckValidInit: true,
1269 decodeTo: makeMessages(protobuild.Message{
1270 "optional_message": protobuild.Message{
1271 "required_field": 1,
1272 },
1273 }, &testpb.TestRequiredForeign{}, &testeditionspb.TestRequiredForeign{}),
1274 wire: protopack.Message{
1275 protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
1276 protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1277 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1278 }),
1279 }.Marshal(),
1280 },
1281
1282 {
1283 desc: "required field in repeated message unset",
1284 checkFastInit: true,
1285 partial: true,
1286 decodeTo: makeMessages(protobuild.Message{
1287 "repeated_message": []protobuild.Message{
1288 {"required_field": 1},
1289 {},
1290 },
1291 }, &testpb.TestRequiredForeign{}, &testeditionspb.TestRequiredForeign{}),
1292 wire: protopack.Message{
1293 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1294 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1295 }),
1296 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
1297 }.Marshal(),
1298 },
1299
1300 {
1301 desc: "required field in repeated message set",
1302 checkFastInit: true,
1303 decodeTo: makeMessages(protobuild.Message{
1304 "repeated_message": []protobuild.Message{
1305 {"required_field": 1},
1306 {"required_field": 2},
1307 },
1308 }, &testpb.TestRequiredForeign{}, &testeditionspb.TestRequiredForeign{}),
1309 wire: protopack.Message{
1310 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1311 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1312 }),
1313 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1314 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
1315 }),
1316 }.Marshal(),
1317 },
1318
1319 {
1320 desc: "required field in map message unset",
1321 checkFastInit: true,
1322 partial: true,
1323 decodeTo: makeMessages(protobuild.Message{
1324 "map_message": map[int32]protobuild.Message{
1325 1: {"required_field": 1},
1326 2: {},
1327 },
1328 }, &testpb.TestRequiredForeign{}, &testeditionspb.TestRequiredForeign{}),
1329 wire: protopack.Message{
1330 protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1331 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1332 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1333 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1334 }),
1335 }),
1336 protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1337 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
1338 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
1339 }),
1340 }.Marshal(),
1341 },
1342
1343 {
1344 desc: "required field in absent map message value",
1345 checkFastInit: true,
1346 partial: true,
1347 decodeTo: makeMessages(protobuild.Message{
1348 "map_message": map[int32]protobuild.Message{
1349 2: {},
1350 },
1351 }, &testpb.TestRequiredForeign{}, &testeditionspb.TestRequiredForeign{}),
1352 wire: protopack.Message{
1353 protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1354 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
1355 }),
1356 }.Marshal(),
1357 },
1358
1359 {
1360 desc: "required field in map message set",
1361 checkFastInit: true,
1362 decodeTo: makeMessages(protobuild.Message{
1363 "map_message": map[int32]protobuild.Message{
1364 1: {"required_field": 1},
1365 2: {"required_field": 2},
1366 },
1367 }, &testpb.TestRequiredForeign{}, &testeditionspb.TestRequiredForeign{}),
1368 wire: protopack.Message{
1369 protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1370 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1371 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1372 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1373 }),
1374 }),
1375 protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1376 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
1377 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1378 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
1379 }),
1380 }),
1381 }.Marshal(),
1382 },
1383
1384 {
1385 desc: "required field in optional group unset",
1386 checkFastInit: true,
1387 partial: true,
1388 decodeTo: makeMessages(protobuild.Message{
1389 "optionalgroup": protobuild.Message{},
1390 }, &testpb.TestRequiredGroupFields{}, &testeditionspb.TestRequiredGroupFields{}),
1391 wire: protopack.Message{
1392 protopack.Tag{1, protopack.StartGroupType},
1393 protopack.Tag{1, protopack.EndGroupType},
1394 }.Marshal(),
1395 },
1396
1397 {
1398 desc: "required field in optional group set",
1399 checkFastInit: true,
1400 decodeTo: makeMessages(protobuild.Message{
1401 "optionalgroup": protobuild.Message{
1402 "a": 1,
1403 },
1404 }, &testpb.TestRequiredGroupFields{}, &testeditionspb.TestRequiredGroupFields{}),
1405 wire: protopack.Message{
1406 protopack.Tag{1, protopack.StartGroupType},
1407 protopack.Tag{2, protopack.VarintType}, protopack.Varint(1),
1408 protopack.Tag{1, protopack.EndGroupType},
1409 }.Marshal(),
1410 },
1411
1412 {
1413 desc: "required field in repeated group unset",
1414 checkFastInit: true,
1415 partial: true,
1416 decodeTo: makeMessages(protobuild.Message{
1417 "repeatedgroup": []protobuild.Message{
1418 {"a": 1},
1419 {},
1420 },
1421 }, &testpb.TestRequiredGroupFields{}, &testeditionspb.TestRequiredGroupFields{}),
1422 wire: protopack.Message{
1423 protopack.Tag{3, protopack.StartGroupType},
1424 protopack.Tag{4, protopack.VarintType}, protopack.Varint(1),
1425 protopack.Tag{3, protopack.EndGroupType},
1426 protopack.Tag{3, protopack.StartGroupType},
1427 protopack.Tag{3, protopack.EndGroupType},
1428 }.Marshal(),
1429 },
1430
1431 {
1432 desc: "required field in repeated group set",
1433 checkFastInit: true,
1434 decodeTo: makeMessages(protobuild.Message{
1435 "repeatedgroup": []protobuild.Message{
1436 {"a": 1},
1437 {"a": 2},
1438 },
1439 }, &testpb.TestRequiredGroupFields{}, &testeditionspb.TestRequiredGroupFields{}),
1440 wire: protopack.Message{
1441 protopack.Tag{3, protopack.StartGroupType},
1442 protopack.Tag{4, protopack.VarintType}, protopack.Varint(1),
1443 protopack.Tag{3, protopack.EndGroupType},
1444 protopack.Tag{3, protopack.StartGroupType},
1445 protopack.Tag{4, protopack.VarintType}, protopack.Varint(2),
1446 protopack.Tag{3, protopack.EndGroupType},
1447 }.Marshal(),
1448 },
1449
1450 {
1451 desc: "required field in oneof message unset",
1452 checkFastInit: true,
1453 partial: true,
1454 decodeTo: makeMessages(protobuild.Message{
1455 "oneof_message": protobuild.Message{},
1456 }, &testpb.TestRequiredForeign{}, &testeditionspb.TestRequiredForeign{}),
1457 wire: protopack.Message{protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{})}.Marshal(),
1458 },
1459
1460 {
1461 desc: "required field in oneof message set",
1462 checkFastInit: true,
1463 decodeTo: makeMessages(protobuild.Message{
1464 "oneof_message": protobuild.Message{
1465 "required_field": 1,
1466 },
1467 }, &testpb.TestRequiredForeign{}, &testeditionspb.TestRequiredForeign{}),
1468 wire: protopack.Message{protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1469 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1470 })}.Marshal(),
1471 },
1472
1473 {
1474 desc: "required field in extension message unset",
1475 checkFastInit: true,
1476 partial: true,
1477 decodeTo: makeMessages(protobuild.Message{
1478 "single": protobuild.Message{},
1479 }, &testpb.TestAllExtensions{}, &testeditionspb.TestAllExtensions{}),
1480 wire: protopack.Message{
1481 protopack.Tag{1000, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
1482 }.Marshal(),
1483 },
1484
1485 {
1486 desc: "required field in extension message set",
1487 checkFastInit: true,
1488 decodeTo: makeMessages(protobuild.Message{
1489 "single": protobuild.Message{
1490 "required_field": 1,
1491 },
1492 }, &testpb.TestAllExtensions{}, &testeditionspb.TestAllExtensions{}),
1493 wire: protopack.Message{
1494 protopack.Tag{1000, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1495 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1496 }),
1497 }.Marshal(),
1498 },
1499
1500 {
1501 desc: "required field in repeated extension message unset",
1502 checkFastInit: true,
1503 partial: true,
1504 decodeTo: makeMessages(protobuild.Message{
1505 "multi": []protobuild.Message{
1506 {"required_field": 1},
1507 {},
1508 },
1509 }, &testpb.TestAllExtensions{}, &testeditionspb.TestAllExtensions{}),
1510 wire: protopack.Message{
1511 protopack.Tag{1001, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1512 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1513 }),
1514 protopack.Tag{1001, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
1515 }.Marshal(),
1516 },
1517
1518 {
1519 desc: "required field in repeated extension message set",
1520 checkFastInit: true,
1521 decodeTo: makeMessages(protobuild.Message{
1522 "multi": []protobuild.Message{
1523 {"required_field": 1},
1524 {"required_field": 2},
1525 },
1526 }, &testpb.TestAllExtensions{}, &testeditionspb.TestAllExtensions{}),
1527 wire: protopack.Message{
1528 protopack.Tag{1001, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1529 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1530 }),
1531 protopack.Tag{1001, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1532 protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
1533 }),
1534 }.Marshal(),
1535 },
1536
1537 {
1538 desc: "nil messages",
1539 decodeTo: []proto.Message{
1540 (*testpb.TestAllTypes)(nil),
1541 (*test3pb.TestAllTypes)(nil),
1542 (*testeditionspb.TestAllTypes)(nil),
1543 (*testpb.TestAllExtensions)(nil),
1544 },
1545 },
1546
1547 {
1548 desc: "legacy",
1549 partial: true,
1550 decodeTo: makeMessages(protobuild.Message{
1551 "f1": protobuild.Message{
1552 "optional_int32": 1,
1553 "optional_child_enum": "ALPHA",
1554 "optional_child_message": protobuild.Message{
1555 "f1": "x",
1556 },
1557 "optionalgroup": protobuild.Message{
1558 "f1": "x",
1559 },
1560 "repeated_child_message": []protobuild.Message{
1561 {"f1": "x"},
1562 },
1563 "repeatedgroup": []protobuild.Message{
1564 {"f1": "x"},
1565 },
1566 "map_bool_child_message": map[bool]protobuild.Message{
1567 true: {"f1": "x"},
1568 },
1569 "oneof_child_message": protobuild.Message{
1570 "f1": "x",
1571 },
1572 },
1573 }, &legacypb.Legacy{}),
1574 wire: protopack.Message{
1575 protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1576 protopack.Tag{101, protopack.VarintType}, protopack.Varint(1),
1577 protopack.Tag{115, protopack.VarintType}, protopack.Varint(0),
1578 protopack.Tag{116, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1579 protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
1580 }),
1581 protopack.Tag{120, protopack.StartGroupType},
1582 protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
1583 protopack.Tag{120, protopack.EndGroupType},
1584 protopack.Tag{516, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1585 protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
1586 }),
1587 protopack.Tag{520, protopack.StartGroupType},
1588 protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
1589 protopack.Tag{520, protopack.EndGroupType},
1590 protopack.Tag{616, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1591 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1592 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1593 protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
1594 }),
1595 }),
1596 protopack.Tag{716, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1597 protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
1598 }),
1599 }),
1600 }.Marshal(),
1601 validationStatus: impl.ValidationUnknown,
1602 },
1603
1604 {
1605 desc: "first reserved field number",
1606 decodeTo: makeMessages(protobuild.Message{
1607 protobuild.Unknown: protopack.Message{
1608 protopack.Tag{protopack.FirstReservedNumber, protopack.VarintType}, protopack.Varint(1004),
1609 }.Marshal(),
1610 }),
1611 wire: protopack.Message{
1612 protopack.Tag{protopack.FirstReservedNumber, protopack.VarintType}, protopack.Varint(1004),
1613 }.Marshal(),
1614 },
1615
1616 {
1617 desc: "last reserved field number",
1618 decodeTo: makeMessages(protobuild.Message{
1619 protobuild.Unknown: protopack.Message{
1620 protopack.Tag{protopack.LastReservedNumber, protopack.VarintType}, protopack.Varint(1005),
1621 }.Marshal(),
1622 }),
1623 wire: protopack.Message{
1624 protopack.Tag{protopack.LastReservedNumber, protopack.VarintType}, protopack.Varint(1005),
1625 }.Marshal(),
1626 },
1627
1628 {
1629 desc: "nested unknown extension",
1630 unmarshalOptions: proto.UnmarshalOptions{
1631 DiscardUnknown: true,
1632 Resolver: filterResolver{
1633 filter: func(name protoreflect.FullName) bool {
1634 switch name.Name() {
1635 case "optional_nested_message",
1636 "optional_int32":
1637 return true
1638 }
1639 return false
1640 },
1641 resolver: protoregistry.GlobalTypes,
1642 },
1643 },
1644 decodeTo: makeMessages(protobuild.Message{
1645 "optional_nested_message": protobuild.Message{
1646 "corecursive": protobuild.Message{
1647 "optional_nested_message": protobuild.Message{
1648 "corecursive": protobuild.Message{
1649 "optional_int32": 42,
1650 },
1651 },
1652 },
1653 },
1654 }, &testpb.TestAllExtensions{}, &testeditionspb.TestAllExtensions{}),
1655 wire: protopack.Message{
1656 protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1657 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1658 protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1659 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1660 protopack.Tag{1, protopack.VarintType}, protopack.Varint(42),
1661 protopack.Tag{2, protopack.VarintType}, protopack.Varint(43),
1662 }),
1663 }),
1664 }),
1665 }),
1666 }.Marshal(),
1667 },
1668 }
1669
1670 var testInvalidMessages = []testProto{
1671 {
1672 desc: "invalid UTF-8 in optional string field",
1673 decodeTo: makeMessages(protobuild.Message{
1674 "optional_string": "abc\xff",
1675 }, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
1676 wire: protopack.Message{
1677 protopack.Tag{14, protopack.BytesType}, protopack.String("abc\xff"),
1678 }.Marshal(),
1679 },
1680 {
1681 desc: "invalid UTF-8 in singular string field",
1682 decodeTo: makeMessages(protobuild.Message{
1683 "singular_string": "abc\xff",
1684 }, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
1685 wire: protopack.Message{
1686 protopack.Tag{94, protopack.BytesType}, protopack.String("abc\xff"),
1687 }.Marshal(),
1688 },
1689 {
1690 desc: "invalid UTF-8 in repeated string field",
1691 decodeTo: makeMessages(protobuild.Message{
1692 "repeated_string": []string{"foo", "abc\xff"},
1693 }, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
1694 wire: protopack.Message{
1695 protopack.Tag{44, protopack.BytesType}, protopack.String("foo"),
1696 protopack.Tag{44, protopack.BytesType}, protopack.String("abc\xff"),
1697 }.Marshal(),
1698 },
1699 {
1700 desc: "invalid UTF-8 in nested message",
1701 decodeTo: makeMessages(protobuild.Message{
1702 "optional_nested_message": protobuild.Message{
1703 "corecursive": protobuild.Message{
1704 "singular_string": "abc\xff",
1705 },
1706 },
1707 }, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
1708 wire: protopack.Message{
1709 protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1710 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1711 protopack.Tag{94, protopack.BytesType}, protopack.String("abc\xff"),
1712 }),
1713 }),
1714 }.Marshal(),
1715 },
1716 {
1717 desc: "invalid UTF-8 in oneof field",
1718 decodeTo: makeMessages(protobuild.Message{
1719 "oneof_string": "abc\xff",
1720 }, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
1721 wire: protopack.Message{protopack.Tag{113, protopack.BytesType}, protopack.String("abc\xff")}.Marshal(),
1722 },
1723 {
1724 desc: "invalid UTF-8 in map key",
1725 decodeTo: makeMessages(protobuild.Message{
1726 "map_string_string": map[string]string{"key\xff": "val"},
1727 }, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
1728 wire: protopack.Message{
1729 protopack.Tag{69, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1730 protopack.Tag{1, protopack.BytesType}, protopack.String("key\xff"),
1731 protopack.Tag{2, protopack.BytesType}, protopack.String("val"),
1732 }),
1733 }.Marshal(),
1734 },
1735 {
1736 desc: "invalid UTF-8 in map value",
1737 decodeTo: makeMessages(protobuild.Message{
1738 "map_string_string": map[string]string{"key": "val\xff"},
1739 }, &test3pb.TestAllTypes{}, &testeditionspb.TestAllTypes{}),
1740 wire: protopack.Message{
1741 protopack.Tag{69, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1742 protopack.Tag{1, protopack.BytesType}, protopack.String("key"),
1743 protopack.Tag{2, protopack.BytesType}, protopack.String("val\xff"),
1744 }),
1745 }.Marshal(),
1746 },
1747 {
1748 desc: "invalid field number zero",
1749 decodeTo: []proto.Message{
1750 (*testpb.TestAllTypes)(nil),
1751 (*testeditionspb.TestAllTypes)(nil),
1752 (*testpb.TestAllExtensions)(nil),
1753 (*testeditionspb.TestAllExtensions)(nil),
1754 },
1755 wire: protopack.Message{
1756 protopack.Tag{protopack.MinValidNumber - 1, protopack.VarintType}, protopack.Varint(1001),
1757 }.Marshal(),
1758 },
1759 {
1760 desc: "invalid field numbers zero and one",
1761 decodeTo: []proto.Message{
1762 (*testpb.TestAllTypes)(nil),
1763 (*testeditionspb.TestAllTypes)(nil),
1764 (*testpb.TestAllExtensions)(nil),
1765 (*testeditionspb.TestAllExtensions)(nil),
1766 },
1767 wire: protopack.Message{
1768 protopack.Tag{protopack.MinValidNumber - 1, protopack.VarintType}, protopack.Varint(1002),
1769 protopack.Tag{protopack.MinValidNumber, protopack.VarintType}, protopack.Varint(1003),
1770 }.Marshal(),
1771 },
1772 {
1773 desc: "invalid field numbers max and max+1",
1774 decodeTo: []proto.Message{
1775 (*testpb.TestAllTypes)(nil),
1776 (*testeditionspb.TestAllTypes)(nil),
1777 (*testpb.TestAllExtensions)(nil),
1778 (*testeditionspb.TestAllExtensions)(nil),
1779 },
1780 wire: protopack.Message{
1781 protopack.Tag{protopack.MaxValidNumber, protopack.VarintType}, protopack.Varint(1006),
1782 protopack.Tag{protopack.MaxValidNumber + 1, protopack.VarintType}, protopack.Varint(1007),
1783 }.Marshal(),
1784 },
1785 {
1786 desc: "invalid field number max+1",
1787 decodeTo: []proto.Message{
1788 (*testpb.TestAllTypes)(nil),
1789 (*testeditionspb.TestAllTypes)(nil),
1790 (*testpb.TestAllExtensions)(nil),
1791 (*testeditionspb.TestAllExtensions)(nil),
1792 },
1793 wire: protopack.Message{
1794 protopack.Tag{protopack.MaxValidNumber + 1, protopack.VarintType}, protopack.Varint(1008),
1795 }.Marshal(),
1796 },
1797 {
1798 desc: "invalid field number wraps int32",
1799 decodeTo: []proto.Message{
1800 (*testpb.TestAllTypes)(nil),
1801 (*testeditionspb.TestAllTypes)(nil),
1802 (*testpb.TestAllExtensions)(nil),
1803 (*testeditionspb.TestAllExtensions)(nil),
1804 },
1805 wire: protopack.Message{
1806 protopack.Varint(2234993595104), protopack.Varint(0),
1807 }.Marshal(),
1808 },
1809 {
1810 desc: "invalid field number in map",
1811 decodeTo: []proto.Message{(*testpb.TestAllTypes)(nil), (*testeditionspb.TestAllTypes)(nil)},
1812 wire: protopack.Message{
1813 protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1814 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1056),
1815 protopack.Tag{2, protopack.VarintType}, protopack.Varint(1156),
1816 protopack.Tag{protopack.MaxValidNumber + 1, protopack.VarintType}, protopack.Varint(0),
1817 }),
1818 }.Marshal(),
1819 },
1820 {
1821 desc: "invalid tag varint",
1822 decodeTo: []proto.Message{
1823 (*testpb.TestAllTypes)(nil),
1824 (*testeditionspb.TestAllTypes)(nil),
1825 (*testpb.TestAllExtensions)(nil),
1826 (*testeditionspb.TestAllExtensions)(nil),
1827 },
1828 wire: []byte{0xff},
1829 },
1830 {
1831 desc: "field number too small",
1832 decodeTo: []proto.Message{
1833 (*testpb.TestAllTypes)(nil),
1834 (*testeditionspb.TestAllTypes)(nil),
1835 (*testpb.TestAllExtensions)(nil),
1836 (*testeditionspb.TestAllExtensions)(nil),
1837 },
1838 wire: protopack.Message{
1839 protopack.Tag{0, protopack.VarintType}, protopack.Varint(0),
1840 }.Marshal(),
1841 },
1842 {
1843 desc: "field number too large",
1844 decodeTo: []proto.Message{
1845 (*testpb.TestAllTypes)(nil),
1846 (*testeditionspb.TestAllTypes)(nil),
1847 (*testpb.TestAllExtensions)(nil),
1848 (*testeditionspb.TestAllExtensions)(nil),
1849 },
1850 wire: protopack.Message{
1851 protopack.Tag{protowire.MaxValidNumber + 1, protopack.VarintType}, protopack.Varint(0),
1852 }.Marshal(),
1853 },
1854 {
1855 desc: "invalid tag varint in message field",
1856 decodeTo: []proto.Message{
1857 (*testpb.TestAllTypes)(nil),
1858 (*testeditionspb.TestAllTypes)(nil),
1859 (*testpb.TestAllExtensions)(nil),
1860 (*testeditionspb.TestAllExtensions)(nil),
1861 },
1862 wire: protopack.Message{
1863 protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1864 protopack.Raw{0xff},
1865 }),
1866 }.Marshal(),
1867 },
1868 {
1869 desc: "invalid tag varint in repeated message field",
1870 decodeTo: []proto.Message{
1871 (*testpb.TestAllTypes)(nil),
1872 (*testeditionspb.TestAllTypes)(nil),
1873 (*testpb.TestAllExtensions)(nil),
1874 (*testeditionspb.TestAllExtensions)(nil),
1875 },
1876 wire: protopack.Message{
1877 protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1878 protopack.Raw{0xff},
1879 }),
1880 }.Marshal(),
1881 },
1882 {
1883 desc: "invalid varint in group field",
1884 decodeTo: []proto.Message{
1885 (*testpb.TestAllTypes)(nil),
1886 (*testeditionspb.TestAllTypes)(nil),
1887 (*testpb.TestAllExtensions)(nil),
1888 (*testeditionspb.TestAllExtensions)(nil),
1889 },
1890 wire: protopack.Message{
1891 protopack.Tag{16, protopack.StartGroupType},
1892 protopack.Tag{1000, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1893 protopack.Raw{0xff},
1894 }),
1895 protopack.Tag{16, protopack.EndGroupType},
1896 }.Marshal(),
1897 },
1898 {
1899 desc: "invalid varint in repeated group field",
1900 decodeTo: []proto.Message{
1901 (*testpb.TestAllTypes)(nil),
1902 (*testeditionspb.TestAllTypes)(nil),
1903 (*testpb.TestAllExtensions)(nil),
1904 (*testeditionspb.TestAllExtensions)(nil),
1905 },
1906 wire: protopack.Message{
1907 protopack.Tag{46, protopack.StartGroupType},
1908 protopack.Tag{1001, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1909 protopack.Raw{0xff},
1910 }),
1911 protopack.Tag{46, protopack.EndGroupType},
1912 }.Marshal(),
1913 },
1914 {
1915 desc: "unterminated repeated group field",
1916 decodeTo: []proto.Message{
1917 (*testpb.TestAllTypes)(nil),
1918 (*testeditionspb.TestAllTypes)(nil),
1919 (*testpb.TestAllExtensions)(nil),
1920 (*testeditionspb.TestAllExtensions)(nil),
1921 },
1922 wire: protopack.Message{
1923 protopack.Tag{46, protopack.StartGroupType},
1924 }.Marshal(),
1925 },
1926 {
1927 desc: "invalid tag varint in map item",
1928 decodeTo: []proto.Message{
1929 (*testpb.TestAllTypes)(nil),
1930 (*testeditionspb.TestAllTypes)(nil),
1931 },
1932 wire: protopack.Message{
1933 protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1934 protopack.Tag{1, protopack.VarintType}, protopack.Varint(0),
1935 protopack.Tag{2, protopack.VarintType}, protopack.Varint(0),
1936 protopack.Raw{0xff},
1937 }),
1938 }.Marshal(),
1939 },
1940 {
1941 desc: "invalid tag varint in map message value",
1942 decodeTo: []proto.Message{
1943 (*testpb.TestAllTypes)(nil),
1944 (*testeditionspb.TestAllTypes)(nil),
1945 },
1946 wire: protopack.Message{
1947 protopack.Tag{71, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1948 protopack.Tag{1, protopack.VarintType}, protopack.Varint(0),
1949 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1950 protopack.Raw{0xff},
1951 }),
1952 }),
1953 }.Marshal(),
1954 },
1955 {
1956 desc: "invalid packed int32 field",
1957 decodeTo: []proto.Message{
1958 (*testpb.TestAllTypes)(nil),
1959 (*testeditionspb.TestAllTypes)(nil),
1960 (*testpb.TestAllExtensions)(nil),
1961 (*testeditionspb.TestAllExtensions)(nil),
1962 },
1963 wire: protopack.Message{
1964 protopack.Tag{31, protopack.BytesType}, protopack.Bytes{0xff},
1965 }.Marshal(),
1966 },
1967 {
1968 desc: "invalid packed int64 field",
1969 decodeTo: []proto.Message{
1970 (*testpb.TestAllTypes)(nil),
1971 (*testeditionspb.TestAllTypes)(nil),
1972 (*testpb.TestAllExtensions)(nil),
1973 (*testeditionspb.TestAllExtensions)(nil),
1974 },
1975 wire: protopack.Message{
1976 protopack.Tag{32, protopack.BytesType}, protopack.Bytes{0xff},
1977 }.Marshal(),
1978 },
1979 {
1980 desc: "invalid packed uint32 field",
1981 decodeTo: []proto.Message{
1982 (*testpb.TestAllTypes)(nil),
1983 (*testeditionspb.TestAllTypes)(nil),
1984 (*testpb.TestAllExtensions)(nil),
1985 (*testeditionspb.TestAllExtensions)(nil),
1986 },
1987 wire: protopack.Message{
1988 protopack.Tag{33, protopack.BytesType}, protopack.Bytes{0xff},
1989 }.Marshal(),
1990 },
1991 {
1992 desc: "invalid packed uint64 field",
1993 decodeTo: []proto.Message{
1994 (*testpb.TestAllTypes)(nil),
1995 (*testeditionspb.TestAllTypes)(nil),
1996 (*testpb.TestAllExtensions)(nil),
1997 (*testeditionspb.TestAllExtensions)(nil),
1998 },
1999 wire: protopack.Message{
2000 protopack.Tag{34, protopack.BytesType}, protopack.Bytes{0xff},
2001 }.Marshal(),
2002 },
2003 {
2004 desc: "invalid packed sint32 field",
2005 decodeTo: []proto.Message{
2006 (*testpb.TestAllTypes)(nil),
2007 (*testeditionspb.TestAllTypes)(nil),
2008 (*testpb.TestAllExtensions)(nil),
2009 (*testeditionspb.TestAllExtensions)(nil),
2010 },
2011 wire: protopack.Message{
2012 protopack.Tag{35, protopack.BytesType}, protopack.Bytes{0xff},
2013 }.Marshal(),
2014 },
2015 {
2016 desc: "invalid packed sint64 field",
2017 decodeTo: []proto.Message{
2018 (*testpb.TestAllTypes)(nil),
2019 (*testeditionspb.TestAllTypes)(nil),
2020 (*testpb.TestAllExtensions)(nil),
2021 (*testeditionspb.TestAllExtensions)(nil),
2022 },
2023 wire: protopack.Message{
2024 protopack.Tag{36, protopack.BytesType}, protopack.Bytes{0xff},
2025 }.Marshal(),
2026 },
2027 {
2028 desc: "invalid packed fixed32 field",
2029 decodeTo: []proto.Message{
2030 (*testpb.TestAllTypes)(nil),
2031 (*testeditionspb.TestAllTypes)(nil),
2032 (*testpb.TestAllExtensions)(nil),
2033 (*testeditionspb.TestAllExtensions)(nil),
2034 },
2035 wire: protopack.Message{
2036 protopack.Tag{37, protopack.BytesType}, protopack.Bytes{0x00},
2037 }.Marshal(),
2038 },
2039 {
2040 desc: "invalid packed fixed64 field",
2041 decodeTo: []proto.Message{
2042 (*testpb.TestAllTypes)(nil),
2043 (*testeditionspb.TestAllTypes)(nil),
2044 (*testpb.TestAllExtensions)(nil),
2045 (*testeditionspb.TestAllExtensions)(nil),
2046 },
2047 wire: protopack.Message{
2048 protopack.Tag{38, protopack.BytesType}, protopack.Bytes{0x00},
2049 }.Marshal(),
2050 },
2051 {
2052 desc: "invalid packed sfixed32 field",
2053 decodeTo: []proto.Message{
2054 (*testpb.TestAllTypes)(nil),
2055 (*testeditionspb.TestAllTypes)(nil),
2056 (*testpb.TestAllExtensions)(nil),
2057 (*testeditionspb.TestAllExtensions)(nil),
2058 },
2059 wire: protopack.Message{
2060 protopack.Tag{39, protopack.BytesType}, protopack.Bytes{0x00},
2061 }.Marshal(),
2062 },
2063 {
2064 desc: "invalid packed sfixed64 field",
2065 decodeTo: []proto.Message{
2066 (*testpb.TestAllTypes)(nil),
2067 (*testeditionspb.TestAllTypes)(nil),
2068 (*testpb.TestAllExtensions)(nil),
2069 (*testeditionspb.TestAllExtensions)(nil),
2070 },
2071 wire: protopack.Message{
2072 protopack.Tag{40, protopack.BytesType}, protopack.Bytes{0x00},
2073 }.Marshal(),
2074 },
2075 {
2076 desc: "invalid packed float field",
2077 decodeTo: []proto.Message{
2078 (*testpb.TestAllTypes)(nil),
2079 (*testeditionspb.TestAllTypes)(nil),
2080 (*testpb.TestAllExtensions)(nil),
2081 (*testeditionspb.TestAllExtensions)(nil),
2082 },
2083 wire: protopack.Message{
2084 protopack.Tag{41, protopack.BytesType}, protopack.Bytes{0x00},
2085 }.Marshal(),
2086 },
2087 {
2088 desc: "invalid packed double field",
2089 decodeTo: []proto.Message{
2090 (*testpb.TestAllTypes)(nil),
2091 (*testeditionspb.TestAllTypes)(nil),
2092 (*testpb.TestAllExtensions)(nil),
2093 (*testeditionspb.TestAllExtensions)(nil),
2094 },
2095 wire: protopack.Message{
2096 protopack.Tag{42, protopack.BytesType}, protopack.Bytes{0x00},
2097 }.Marshal(),
2098 },
2099 {
2100 desc: "invalid packed bool field",
2101 decodeTo: []proto.Message{
2102 (*testpb.TestAllTypes)(nil),
2103 (*testeditionspb.TestAllTypes)(nil),
2104 (*testpb.TestAllExtensions)(nil),
2105 (*testeditionspb.TestAllExtensions)(nil),
2106 },
2107 wire: protopack.Message{
2108 protopack.Tag{43, protopack.BytesType}, protopack.Bytes{0xff},
2109 }.Marshal(),
2110 },
2111 {
2112 desc: "bytes field overruns message",
2113 decodeTo: []proto.Message{
2114 (*testpb.TestAllTypes)(nil),
2115 (*testeditionspb.TestAllTypes)(nil),
2116 (*testpb.TestAllExtensions)(nil),
2117 (*testeditionspb.TestAllExtensions)(nil),
2118 },
2119 wire: protopack.Message{
2120 protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix{protopack.Message{
2121 protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix{protopack.Message{
2122 protopack.Tag{15, protopack.BytesType}, protopack.Varint(2),
2123 }},
2124 protopack.Tag{1, protopack.VarintType}, protopack.Varint(0),
2125 }},
2126 }.Marshal(),
2127 },
2128 {
2129 desc: "varint field overruns message",
2130 decodeTo: []proto.Message{
2131 (*testpb.TestAllTypes)(nil),
2132 (*testeditionspb.TestAllTypes)(nil),
2133 (*testeditionspb.TestAllExtensions)(nil),
2134 (*testpb.TestAllExtensions)(nil),
2135 },
2136 wire: protopack.Message{
2137 protopack.Tag{1, protopack.VarintType},
2138 }.Marshal(),
2139 },
2140 {
2141 desc: "bytes field lacks size",
2142 decodeTo: []proto.Message{
2143 (*testpb.TestAllTypes)(nil),
2144 (*testeditionspb.TestAllTypes)(nil),
2145 (*testpb.TestAllExtensions)(nil),
2146 (*testeditionspb.TestAllExtensions)(nil),
2147 },
2148 wire: protopack.Message{
2149 protopack.Tag{18, protopack.BytesType},
2150 }.Marshal(),
2151 },
2152 {
2153 desc: "varint overflow",
2154 decodeTo: []proto.Message{
2155 (*testpb.TestAllTypes)(nil),
2156 (*testeditionspb.TestAllTypes)(nil),
2157 (*testpb.TestAllExtensions)(nil),
2158 (*testeditionspb.TestAllExtensions)(nil),
2159 },
2160 wire: protopack.Message{
2161 protopack.Tag{1, protopack.VarintType},
2162 protopack.Raw("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x02"),
2163 }.Marshal(),
2164 },
2165 {
2166 desc: "varint length overrun",
2167 decodeTo: []proto.Message{
2168 (*testpb.TestAllTypes)(nil),
2169 (*testeditionspb.TestAllTypes)(nil),
2170 (*testpb.TestAllExtensions)(nil),
2171 (*testeditionspb.TestAllExtensions)(nil),
2172 },
2173 wire: protopack.Message{
2174 protopack.Tag{1, protopack.VarintType},
2175 protopack.Raw("\xff\xff\xff\xff\xff\xff\xff\xff\xff"),
2176 }.Marshal(),
2177 },
2178 }
2179
2180 type filterResolver struct {
2181 filter func(name protoreflect.FullName) bool
2182 resolver protoregistry.ExtensionTypeResolver
2183 }
2184
2185 func (f filterResolver) FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error) {
2186 if !f.filter(field) {
2187 return nil, protoregistry.NotFound
2188 }
2189 return f.resolver.FindExtensionByName(field)
2190 }
2191
2192 func (f filterResolver) FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error) {
2193 xt, err := f.resolver.FindExtensionByNumber(message, field)
2194 if err != nil {
2195 return nil, err
2196 }
2197 if !f.filter(xt.TypeDescriptor().FullName()) {
2198 return nil, protoregistry.NotFound
2199 }
2200 return xt, nil
2201 }
2202
2203 func roundTripMessage(dst, src proto.Message) error {
2204 b, err := proto.Marshal(src)
2205 if err != nil {
2206 return err
2207 }
2208 return proto.Unmarshal(b, dst)
2209 }
2210
View as plain text