blob: acbbcb51a9aa2cae7db3d858110080ebdc6fcc80 [file] [log] [blame]
Damien Neilba23aa52018-12-07 14:38:17 -08001// Copyright 2018 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
Damien Neil4be2fb42018-12-17 11:16:16 -08005package proto_test
Damien Neilba23aa52018-12-07 14:38:17 -08006
7import (
8 "fmt"
9 "reflect"
10 "testing"
11
12 protoV1 "github.com/golang/protobuf/proto"
Damien Neil5c5b5312019-05-14 12:44:37 -070013 "google.golang.org/protobuf/encoding/prototext"
Damien Neile89e6242019-05-13 23:55:40 -070014 "google.golang.org/protobuf/internal/encoding/pack"
Damien Neile89e6242019-05-13 23:55:40 -070015 "google.golang.org/protobuf/internal/scalar"
16 "google.golang.org/protobuf/proto"
17 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsai19058432019-02-27 21:46:29 -080018
Damien Neilc37adef2019-04-01 13:49:56 -070019 legacypb "google.golang.org/protobuf/internal/testprotos/legacy"
20 legacy1pb "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v0.0.0-20160225-2fc053c5"
Damien Neile89e6242019-05-13 23:55:40 -070021 testpb "google.golang.org/protobuf/internal/testprotos/test"
22 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Damien Neilba23aa52018-12-07 14:38:17 -080023)
24
25type testProto struct {
Damien Neil96c229a2019-04-03 12:17:24 -070026 desc string
27 decodeTo []proto.Message
28 wire []byte
29 partial bool
30 invalidExtensions bool
Damien Neilba23aa52018-12-07 14:38:17 -080031}
32
33func TestDecode(t *testing.T) {
34 for _, test := range testProtos {
35 for _, want := range test.decodeTo {
36 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070037 opts := proto.UnmarshalOptions{
38 AllowPartial: test.partial,
39 }
Damien Neilba23aa52018-12-07 14:38:17 -080040 wire := append(([]byte)(nil), test.wire...)
Damien Neil4be2fb42018-12-17 11:16:16 -080041 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
Damien Neil96c229a2019-04-03 12:17:24 -070042 if err := opts.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070043 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080044 return
45 }
46
47 // Aliasing check: Modifying the original wire bytes shouldn't
48 // affect the unmarshaled message.
49 for i := range wire {
50 wire[i] = 0
51 }
52
Damien Neil96c229a2019-04-03 12:17:24 -070053 if test.invalidExtensions {
54 // Equal doesn't work on messages containing invalid extension data.
55 return
56 }
Joe Tsaidb38ddd2019-05-07 15:14:40 -070057 if !proto.Equal(got, want) {
Damien Neil61e93c72019-03-27 09:23:20 -070058 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080059 }
60 })
61 }
62 }
63}
64
Damien Neil96c229a2019-04-03 12:17:24 -070065func TestDecodeRequiredFieldChecks(t *testing.T) {
66 for _, test := range testProtos {
67 if !test.partial {
68 continue
69 }
70 if test.invalidExtensions {
71 // Missing required fields in extensions just end up in the unknown fields.
72 continue
73 }
74 for _, m := range test.decodeTo {
75 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
76 got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
77 if err := proto.Unmarshal(test.wire, got); err == nil {
78 t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got))
79 }
80 })
81 }
82 }
83}
84
Damien Neilbc310b52019-04-11 11:46:55 -070085func TestDecodeInvalidUTF8(t *testing.T) {
86 for _, test := range invalidUTF8TestProtos {
87 for _, want := range test.decodeTo {
88 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
89 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
90 err := proto.Unmarshal(test.wire, got)
Damien Neil8c86fc52019-06-19 09:28:29 -070091 if err == nil {
Damien Neilbc310b52019-04-11 11:46:55 -070092 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
93 }
Damien Neilbc310b52019-04-11 11:46:55 -070094 })
95 }
96 }
97}
98
Damien Neilba23aa52018-12-07 14:38:17 -080099var testProtos = []testProto{
100 {
101 desc: "basic scalar types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800102 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800103 OptionalInt32: scalar.Int32(1001),
104 OptionalInt64: scalar.Int64(1002),
105 OptionalUint32: scalar.Uint32(1003),
106 OptionalUint64: scalar.Uint64(1004),
107 OptionalSint32: scalar.Int32(1005),
108 OptionalSint64: scalar.Int64(1006),
109 OptionalFixed32: scalar.Uint32(1007),
110 OptionalFixed64: scalar.Uint64(1008),
111 OptionalSfixed32: scalar.Int32(1009),
112 OptionalSfixed64: scalar.Int64(1010),
113 OptionalFloat: scalar.Float32(1011.5),
114 OptionalDouble: scalar.Float64(1012.5),
115 OptionalBool: scalar.Bool(true),
116 OptionalString: scalar.String("string"),
117 OptionalBytes: []byte("bytes"),
118 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
Damien Neil3b46ade2019-03-26 13:55:02 -0700119 }, &test3pb.TestAllTypes{
120 OptionalInt32: 1001,
121 OptionalInt64: 1002,
122 OptionalUint32: 1003,
123 OptionalUint64: 1004,
124 OptionalSint32: 1005,
125 OptionalSint64: 1006,
126 OptionalFixed32: 1007,
127 OptionalFixed64: 1008,
128 OptionalSfixed32: 1009,
129 OptionalSfixed64: 1010,
130 OptionalFloat: 1011.5,
131 OptionalDouble: 1012.5,
132 OptionalBool: true,
133 OptionalString: "string",
134 OptionalBytes: []byte("bytes"),
135 OptionalNestedEnum: test3pb.TestAllTypes_BAR,
Damien Neilba23aa52018-12-07 14:38:17 -0800136 }, build(
137 &testpb.TestAllExtensions{},
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700138 extend(testpb.E_OptionalInt32Extension, int32(1001)),
139 extend(testpb.E_OptionalInt64Extension, int64(1002)),
140 extend(testpb.E_OptionalUint32Extension, uint32(1003)),
141 extend(testpb.E_OptionalUint64Extension, uint64(1004)),
142 extend(testpb.E_OptionalSint32Extension, int32(1005)),
143 extend(testpb.E_OptionalSint64Extension, int64(1006)),
144 extend(testpb.E_OptionalFixed32Extension, uint32(1007)),
145 extend(testpb.E_OptionalFixed64Extension, uint64(1008)),
146 extend(testpb.E_OptionalSfixed32Extension, int32(1009)),
147 extend(testpb.E_OptionalSfixed64Extension, int64(1010)),
148 extend(testpb.E_OptionalFloatExtension, float32(1011.5)),
149 extend(testpb.E_OptionalDoubleExtension, float64(1012.5)),
150 extend(testpb.E_OptionalBoolExtension, bool(true)),
151 extend(testpb.E_OptionalStringExtension, string("string")),
Damien Neilba23aa52018-12-07 14:38:17 -0800152 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700153 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR),
Damien Neilba23aa52018-12-07 14:38:17 -0800154 )},
155 wire: pack.Message{
156 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
157 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
158 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
159 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
160 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
161 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
162 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
163 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
164 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
165 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
166 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
167 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
168 pack.Tag{13, pack.VarintType}, pack.Bool(true),
169 pack.Tag{14, pack.BytesType}, pack.String("string"),
170 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
171 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
172 }.Marshal(),
173 },
174 {
175 desc: "groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800176 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800177 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
178 A: scalar.Int32(1017),
179 },
180 }, build(
181 &testpb.TestAllExtensions{},
182 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
183 A: scalar.Int32(1017),
184 }),
185 )},
186 wire: pack.Message{
187 pack.Tag{16, pack.StartGroupType},
188 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
189 pack.Tag{16, pack.EndGroupType},
190 }.Marshal(),
191 },
192 {
193 desc: "groups (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800194 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800195 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
196 A: scalar.Int32(2),
197 },
198 }, build(
199 &testpb.TestAllExtensions{},
200 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
201 A: scalar.Int32(2),
202 }),
203 )},
204 wire: pack.Message{
205 pack.Tag{16, pack.StartGroupType},
206 pack.Tag{17, pack.VarintType}, pack.Varint(1),
207 pack.Tag{16, pack.EndGroupType},
208 pack.Tag{16, pack.StartGroupType},
209 pack.Tag{17, pack.VarintType}, pack.Varint(2),
210 pack.Tag{16, pack.EndGroupType},
211 }.Marshal(),
212 },
213 {
214 desc: "messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800215 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800216 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
217 A: scalar.Int32(42),
218 Corecursive: &testpb.TestAllTypes{
219 OptionalInt32: scalar.Int32(43),
220 },
221 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700222 }, &test3pb.TestAllTypes{
223 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
224 A: 42,
225 Corecursive: &test3pb.TestAllTypes{
226 OptionalInt32: 43,
227 },
228 },
Damien Neilba23aa52018-12-07 14:38:17 -0800229 }, build(
230 &testpb.TestAllExtensions{},
231 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
232 A: scalar.Int32(42),
233 Corecursive: &testpb.TestAllTypes{
234 OptionalInt32: scalar.Int32(43),
235 },
236 }),
237 )},
238 wire: pack.Message{
239 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
240 pack.Tag{1, pack.VarintType}, pack.Varint(42),
241 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
242 pack.Tag{1, pack.VarintType}, pack.Varint(43),
243 }),
244 }),
245 }.Marshal(),
246 },
247 {
248 desc: "messages (split across multiple tags)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800249 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800250 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
251 A: scalar.Int32(42),
252 Corecursive: &testpb.TestAllTypes{
253 OptionalInt32: scalar.Int32(43),
254 },
255 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700256 }, &test3pb.TestAllTypes{
257 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
258 A: 42,
259 Corecursive: &test3pb.TestAllTypes{
260 OptionalInt32: 43,
261 },
262 },
Damien Neilba23aa52018-12-07 14:38:17 -0800263 }, build(
264 &testpb.TestAllExtensions{},
265 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
266 A: scalar.Int32(42),
267 Corecursive: &testpb.TestAllTypes{
268 OptionalInt32: scalar.Int32(43),
269 },
270 }),
271 )},
272 wire: pack.Message{
273 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
274 pack.Tag{1, pack.VarintType}, pack.Varint(42),
275 }),
276 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
277 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
278 pack.Tag{1, pack.VarintType}, pack.Varint(43),
279 }),
280 }),
281 }.Marshal(),
282 },
283 {
284 desc: "messages (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800285 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800286 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
287 A: scalar.Int32(2),
288 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700289 }, &test3pb.TestAllTypes{
290 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
291 A: 2,
292 },
Damien Neilba23aa52018-12-07 14:38:17 -0800293 }, build(
294 &testpb.TestAllExtensions{},
295 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
296 A: scalar.Int32(2),
297 }),
298 )},
299 wire: pack.Message{
300 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
301 pack.Tag{1, pack.VarintType}, pack.Varint(1),
302 }),
303 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
304 pack.Tag{1, pack.VarintType}, pack.Varint(2),
305 }),
306 }.Marshal(),
307 },
308 {
309 desc: "basic repeated types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800310 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800311 RepeatedInt32: []int32{1001, 2001},
312 RepeatedInt64: []int64{1002, 2002},
313 RepeatedUint32: []uint32{1003, 2003},
314 RepeatedUint64: []uint64{1004, 2004},
315 RepeatedSint32: []int32{1005, 2005},
316 RepeatedSint64: []int64{1006, 2006},
317 RepeatedFixed32: []uint32{1007, 2007},
318 RepeatedFixed64: []uint64{1008, 2008},
319 RepeatedSfixed32: []int32{1009, 2009},
320 RepeatedSfixed64: []int64{1010, 2010},
321 RepeatedFloat: []float32{1011.5, 2011.5},
322 RepeatedDouble: []float64{1012.5, 2012.5},
323 RepeatedBool: []bool{true, false},
324 RepeatedString: []string{"foo", "bar"},
325 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
326 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
327 testpb.TestAllTypes_FOO,
328 testpb.TestAllTypes_BAR,
329 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700330 }, &test3pb.TestAllTypes{
331 RepeatedInt32: []int32{1001, 2001},
332 RepeatedInt64: []int64{1002, 2002},
333 RepeatedUint32: []uint32{1003, 2003},
334 RepeatedUint64: []uint64{1004, 2004},
335 RepeatedSint32: []int32{1005, 2005},
336 RepeatedSint64: []int64{1006, 2006},
337 RepeatedFixed32: []uint32{1007, 2007},
338 RepeatedFixed64: []uint64{1008, 2008},
339 RepeatedSfixed32: []int32{1009, 2009},
340 RepeatedSfixed64: []int64{1010, 2010},
341 RepeatedFloat: []float32{1011.5, 2011.5},
342 RepeatedDouble: []float64{1012.5, 2012.5},
343 RepeatedBool: []bool{true, false},
344 RepeatedString: []string{"foo", "bar"},
345 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
346 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
347 test3pb.TestAllTypes_FOO,
348 test3pb.TestAllTypes_BAR,
349 },
Damien Neilba23aa52018-12-07 14:38:17 -0800350 }, build(
351 &testpb.TestAllExtensions{},
352 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
353 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
354 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
355 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
356 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
357 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
358 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
359 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
360 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
361 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
362 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
363 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
364 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
365 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
366 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
367 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
368 testpb.TestAllTypes_FOO,
369 testpb.TestAllTypes_BAR,
370 }),
371 )},
372 wire: pack.Message{
373 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
374 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
375 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
376 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
377 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
378 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
379 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
380 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
381 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
382 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
383 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
384 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
385 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
386 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
387 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
388 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
389 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
390 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
391 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
392 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
393 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
394 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
395 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
396 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
397 pack.Tag{43, pack.VarintType}, pack.Bool(true),
398 pack.Tag{43, pack.VarintType}, pack.Bool(false),
399 pack.Tag{44, pack.BytesType}, pack.String("foo"),
400 pack.Tag{44, pack.BytesType}, pack.String("bar"),
401 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
402 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
403 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
404 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
405 }.Marshal(),
406 },
407 {
408 desc: "basic repeated types (packed encoding)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800409 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800410 RepeatedInt32: []int32{1001, 2001},
411 RepeatedInt64: []int64{1002, 2002},
412 RepeatedUint32: []uint32{1003, 2003},
413 RepeatedUint64: []uint64{1004, 2004},
414 RepeatedSint32: []int32{1005, 2005},
415 RepeatedSint64: []int64{1006, 2006},
416 RepeatedFixed32: []uint32{1007, 2007},
417 RepeatedFixed64: []uint64{1008, 2008},
418 RepeatedSfixed32: []int32{1009, 2009},
419 RepeatedSfixed64: []int64{1010, 2010},
420 RepeatedFloat: []float32{1011.5, 2011.5},
421 RepeatedDouble: []float64{1012.5, 2012.5},
422 RepeatedBool: []bool{true, false},
423 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
424 testpb.TestAllTypes_FOO,
425 testpb.TestAllTypes_BAR,
426 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700427 }, &test3pb.TestAllTypes{
428 RepeatedInt32: []int32{1001, 2001},
429 RepeatedInt64: []int64{1002, 2002},
430 RepeatedUint32: []uint32{1003, 2003},
431 RepeatedUint64: []uint64{1004, 2004},
432 RepeatedSint32: []int32{1005, 2005},
433 RepeatedSint64: []int64{1006, 2006},
434 RepeatedFixed32: []uint32{1007, 2007},
435 RepeatedFixed64: []uint64{1008, 2008},
436 RepeatedSfixed32: []int32{1009, 2009},
437 RepeatedSfixed64: []int64{1010, 2010},
438 RepeatedFloat: []float32{1011.5, 2011.5},
439 RepeatedDouble: []float64{1012.5, 2012.5},
440 RepeatedBool: []bool{true, false},
441 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
442 test3pb.TestAllTypes_FOO,
443 test3pb.TestAllTypes_BAR,
444 },
Damien Neilba23aa52018-12-07 14:38:17 -0800445 }, build(
446 &testpb.TestAllExtensions{},
447 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
448 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
449 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
450 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
451 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
452 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
453 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
454 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
455 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
456 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
457 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
458 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
459 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
460 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
461 testpb.TestAllTypes_FOO,
462 testpb.TestAllTypes_BAR,
463 }),
464 )},
465 wire: pack.Message{
466 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
467 pack.Varint(1001), pack.Varint(2001),
468 },
469 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
470 pack.Varint(1002), pack.Varint(2002),
471 },
472 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
473 pack.Uvarint(1003), pack.Uvarint(2003),
474 },
475 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
476 pack.Uvarint(1004), pack.Uvarint(2004),
477 },
478 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
479 pack.Svarint(1005), pack.Svarint(2005),
480 },
481 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
482 pack.Svarint(1006), pack.Svarint(2006),
483 },
484 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
485 pack.Uint32(1007), pack.Uint32(2007),
486 },
487 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
488 pack.Uint64(1008), pack.Uint64(2008),
489 },
490 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
491 pack.Int32(1009), pack.Int32(2009),
492 },
493 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
494 pack.Int64(1010), pack.Int64(2010),
495 },
496 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
497 pack.Float32(1011.5), pack.Float32(2011.5),
498 },
499 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
500 pack.Float64(1012.5), pack.Float64(2012.5),
501 },
502 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
503 pack.Bool(true), pack.Bool(false),
504 },
505 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
506 pack.Varint(int(testpb.TestAllTypes_FOO)),
507 pack.Varint(int(testpb.TestAllTypes_BAR)),
508 },
509 }.Marshal(),
510 },
511 {
Damien Neil7492a092019-07-10 15:23:29 -0700512 desc: "packed repeated types",
513 decodeTo: []proto.Message{&testpb.TestPackedTypes{
514 PackedInt32: []int32{1001, 2001},
515 PackedInt64: []int64{1002, 2002},
516 PackedUint32: []uint32{1003, 2003},
517 PackedUint64: []uint64{1004, 2004},
518 PackedSint32: []int32{1005, 2005},
519 PackedSint64: []int64{1006, 2006},
520 PackedFixed32: []uint32{1007, 2007},
521 PackedFixed64: []uint64{1008, 2008},
522 PackedSfixed32: []int32{1009, 2009},
523 PackedSfixed64: []int64{1010, 2010},
524 PackedFloat: []float32{1011.5, 2011.5},
525 PackedDouble: []float64{1012.5, 2012.5},
526 PackedBool: []bool{true, false},
527 PackedEnum: []testpb.ForeignEnum{
528 testpb.ForeignEnum_FOREIGN_FOO,
529 testpb.ForeignEnum_FOREIGN_BAR,
530 },
531 }, build(
532 &testpb.TestPackedExtensions{},
533 extend(testpb.E_PackedInt32Extension, []int32{1001, 2001}),
534 extend(testpb.E_PackedInt64Extension, []int64{1002, 2002}),
535 extend(testpb.E_PackedUint32Extension, []uint32{1003, 2003}),
536 extend(testpb.E_PackedUint64Extension, []uint64{1004, 2004}),
537 extend(testpb.E_PackedSint32Extension, []int32{1005, 2005}),
538 extend(testpb.E_PackedSint64Extension, []int64{1006, 2006}),
539 extend(testpb.E_PackedFixed32Extension, []uint32{1007, 2007}),
540 extend(testpb.E_PackedFixed64Extension, []uint64{1008, 2008}),
541 extend(testpb.E_PackedSfixed32Extension, []int32{1009, 2009}),
542 extend(testpb.E_PackedSfixed64Extension, []int64{1010, 2010}),
543 extend(testpb.E_PackedFloatExtension, []float32{1011.5, 2011.5}),
544 extend(testpb.E_PackedDoubleExtension, []float64{1012.5, 2012.5}),
545 extend(testpb.E_PackedBoolExtension, []bool{true, false}),
546 extend(testpb.E_PackedEnumExtension, []testpb.ForeignEnum{
547 testpb.ForeignEnum_FOREIGN_FOO,
548 testpb.ForeignEnum_FOREIGN_BAR,
549 }),
550 )},
551 wire: pack.Message{
552 pack.Tag{90, pack.BytesType}, pack.LengthPrefix{
553 pack.Varint(1001), pack.Varint(2001),
554 },
555 pack.Tag{91, pack.BytesType}, pack.LengthPrefix{
556 pack.Varint(1002), pack.Varint(2002),
557 },
558 pack.Tag{92, pack.BytesType}, pack.LengthPrefix{
559 pack.Uvarint(1003), pack.Uvarint(2003),
560 },
561 pack.Tag{93, pack.BytesType}, pack.LengthPrefix{
562 pack.Uvarint(1004), pack.Uvarint(2004),
563 },
564 pack.Tag{94, pack.BytesType}, pack.LengthPrefix{
565 pack.Svarint(1005), pack.Svarint(2005),
566 },
567 pack.Tag{95, pack.BytesType}, pack.LengthPrefix{
568 pack.Svarint(1006), pack.Svarint(2006),
569 },
570 pack.Tag{96, pack.BytesType}, pack.LengthPrefix{
571 pack.Uint32(1007), pack.Uint32(2007),
572 },
573 pack.Tag{97, pack.BytesType}, pack.LengthPrefix{
574 pack.Uint64(1008), pack.Uint64(2008),
575 },
576 pack.Tag{98, pack.BytesType}, pack.LengthPrefix{
577 pack.Int32(1009), pack.Int32(2009),
578 },
579 pack.Tag{99, pack.BytesType}, pack.LengthPrefix{
580 pack.Int64(1010), pack.Int64(2010),
581 },
582 pack.Tag{100, pack.BytesType}, pack.LengthPrefix{
583 pack.Float32(1011.5), pack.Float32(2011.5),
584 },
585 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{
586 pack.Float64(1012.5), pack.Float64(2012.5),
587 },
588 pack.Tag{102, pack.BytesType}, pack.LengthPrefix{
589 pack.Bool(true), pack.Bool(false),
590 },
591 pack.Tag{103, pack.BytesType}, pack.LengthPrefix{
592 pack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
593 pack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
594 },
595 }.Marshal(),
596 },
597 {
Damien Neilba23aa52018-12-07 14:38:17 -0800598 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800599 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800600 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
601 {A: scalar.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700602 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800603 {A: scalar.Int32(2)},
604 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700605 }, &test3pb.TestAllTypes{
606 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
607 {A: 1},
Damien Neilc37adef2019-04-01 13:49:56 -0700608 nil,
Damien Neil3b46ade2019-03-26 13:55:02 -0700609 {A: 2},
610 },
Damien Neilba23aa52018-12-07 14:38:17 -0800611 }, build(
612 &testpb.TestAllExtensions{},
613 extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
614 {A: scalar.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700615 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800616 {A: scalar.Int32(2)},
617 }),
618 )},
619 wire: pack.Message{
620 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
621 pack.Tag{1, pack.VarintType}, pack.Varint(1),
622 }),
Damien Neilc37adef2019-04-01 13:49:56 -0700623 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
Damien Neilba23aa52018-12-07 14:38:17 -0800624 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
625 pack.Tag{1, pack.VarintType}, pack.Varint(2),
626 }),
627 }.Marshal(),
628 },
629 {
630 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800631 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800632 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
633 {A: scalar.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700634 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800635 {A: scalar.Int32(2017)},
636 },
637 }, build(
638 &testpb.TestAllExtensions{},
639 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
640 {A: scalar.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700641 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800642 {A: scalar.Int32(2017)},
643 }),
644 )},
645 wire: pack.Message{
646 pack.Tag{46, pack.StartGroupType},
647 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
648 pack.Tag{46, pack.EndGroupType},
649 pack.Tag{46, pack.StartGroupType},
Damien Neilc37adef2019-04-01 13:49:56 -0700650 pack.Tag{46, pack.EndGroupType},
651 pack.Tag{46, pack.StartGroupType},
Damien Neilba23aa52018-12-07 14:38:17 -0800652 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
653 pack.Tag{46, pack.EndGroupType},
654 }.Marshal(),
655 },
656 {
657 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800658 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800659 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
660 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
661 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
662 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
663 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
664 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
665 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
666 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
667 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
668 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
669 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
670 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
671 MapBoolBool: map[bool]bool{true: false, false: true},
672 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
673 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
674 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
675 "71.1.key": {A: scalar.Int32(1171)},
676 "71.2.key": {A: scalar.Int32(2171)},
677 },
678 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
679 "73.1.key": testpb.TestAllTypes_FOO,
680 "73.2.key": testpb.TestAllTypes_BAR,
681 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700682 }, &test3pb.TestAllTypes{
683 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
684 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
685 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
686 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
687 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
688 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
689 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
690 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
691 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
692 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
693 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
694 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
695 MapBoolBool: map[bool]bool{true: false, false: true},
696 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
697 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
698 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
699 "71.1.key": {A: 1171},
700 "71.2.key": {A: 2171},
701 },
702 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
703 "73.1.key": test3pb.TestAllTypes_FOO,
704 "73.2.key": test3pb.TestAllTypes_BAR,
705 },
Damien Neilba23aa52018-12-07 14:38:17 -0800706 }},
707 wire: pack.Message{
708 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
709 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
710 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
711 }),
712 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
713 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
714 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
715 }),
716 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
717 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
718 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
719 }),
720 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
721 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
722 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
723 }),
724 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
725 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
726 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
727 }),
728 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
729 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
730 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
731 }),
732 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
733 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
734 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
735 }),
736 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
737 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
738 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
739 }),
740 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
741 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
742 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
743 }),
744 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
745 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
746 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
747 }),
748 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
749 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
750 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
751 }),
752 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
753 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
754 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
755 }),
756 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
757 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
758 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
759 }),
760 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
761 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
762 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
763 }),
764 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
765 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
766 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
767 }),
768 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
769 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
770 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
771 }),
772 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
773 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
774 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
775 }),
776 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
777 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
778 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
779 }),
780 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
781 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
782 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
783 }),
784 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
785 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
786 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
787 }),
788 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
789 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
790 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
791 }),
792 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
793 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
794 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
795 }),
796 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
797 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
798 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
799 }),
800 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
801 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
802 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
803 }),
804 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
805 pack.Tag{1, pack.VarintType}, pack.Bool(true),
806 pack.Tag{2, pack.VarintType}, pack.Bool(false),
807 }),
808 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
809 pack.Tag{1, pack.VarintType}, pack.Bool(false),
810 pack.Tag{2, pack.VarintType}, pack.Bool(true),
811 }),
812 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
813 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
814 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
815 }),
816 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
817 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
818 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
819 }),
820 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
821 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
822 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
823 }),
824 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
825 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
826 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
827 }),
828 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
829 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
830 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
831 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
832 }),
833 }),
834 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
835 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
836 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
837 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
838 }),
839 }),
840 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
841 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
842 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
843 }),
844 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
845 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
846 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
847 }),
848 }.Marshal(),
849 },
850 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700851 desc: "oneof (uint32)",
852 decodeTo: []proto.Message{
853 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
854 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
855 },
856 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800857 },
858 {
859 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700860 decodeTo: []proto.Message{
861 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
862 &testpb.TestAllTypes_NestedMessage{A: scalar.Int32(1112)},
863 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
864 &test3pb.TestAllTypes_NestedMessage{A: 1112},
865 }},
866 },
Damien Neilba23aa52018-12-07 14:38:17 -0800867 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
868 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
869 })}.Marshal(),
870 },
871 {
Damien Neilc37adef2019-04-01 13:49:56 -0700872 desc: "oneof (empty message)",
873 decodeTo: []proto.Message{
874 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
875 &testpb.TestAllTypes_NestedMessage{},
876 }},
877 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
878 &test3pb.TestAllTypes_NestedMessage{},
879 }},
880 },
881 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
882 },
883 {
Damien Neilba23aa52018-12-07 14:38:17 -0800884 desc: "oneof (overridden message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700885 decodeTo: []proto.Message{
886 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
887 &testpb.TestAllTypes_NestedMessage{
888 Corecursive: &testpb.TestAllTypes{
889 OptionalInt32: scalar.Int32(43),
890 },
Damien Neilba23aa52018-12-07 14:38:17 -0800891 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700892 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
893 &test3pb.TestAllTypes_NestedMessage{
894 Corecursive: &test3pb.TestAllTypes{
895 OptionalInt32: 43,
896 },
897 },
898 }}},
Damien Neilba23aa52018-12-07 14:38:17 -0800899 wire: pack.Message{
900 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
901 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
902 }),
903 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
904 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
905 pack.Tag{1, pack.VarintType}, pack.Varint(43),
906 }),
907 }),
908 }.Marshal(),
909 },
910 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700911 desc: "oneof (string)",
912 decodeTo: []proto.Message{
913 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
914 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
915 },
916 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800917 },
918 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700919 desc: "oneof (bytes)",
920 decodeTo: []proto.Message{
921 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
922 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
923 },
924 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800925 },
926 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700927 desc: "oneof (bool)",
928 decodeTo: []proto.Message{
929 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
930 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
931 },
932 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800933 },
934 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700935 desc: "oneof (uint64)",
936 decodeTo: []proto.Message{
937 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
938 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
939 },
940 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800941 },
942 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700943 desc: "oneof (float)",
944 decodeTo: []proto.Message{
945 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
946 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
947 },
948 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800949 },
950 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700951 desc: "oneof (double)",
952 decodeTo: []proto.Message{
953 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
954 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
955 },
956 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800957 },
958 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700959 desc: "oneof (enum)",
960 decodeTo: []proto.Message{
961 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
962 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
963 },
964 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800965 },
966 {
Damien Neilc37adef2019-04-01 13:49:56 -0700967 desc: "oneof (zero)",
968 decodeTo: []proto.Message{
969 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}},
970 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}},
971 },
972 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
973 },
974 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700975 desc: "oneof (overridden value)",
976 decodeTo: []proto.Message{
977 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
978 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
979 },
Damien Neilba23aa52018-12-07 14:38:17 -0800980 wire: pack.Message{
981 pack.Tag{111, pack.VarintType}, pack.Varint(1),
982 pack.Tag{116, pack.VarintType}, pack.Varint(2),
983 }.Marshal(),
984 },
985 // TODO: More unknown field tests for ordering, repeated fields, etc.
986 //
987 // It is currently impossible to produce results that the v1 Equal
988 // considers equivalent to those of the v1 decoder. Figure out if
989 // that's a problem or not.
990 {
991 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -0800992 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -0800993 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -0700994 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -0800995 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
996 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -0700997 ), build(
998 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -0700999 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001000 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1001 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001002 )},
1003 wire: pack.Message{
1004 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1005 }.Marshal(),
1006 },
1007 {
1008 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001009 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001010 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001011 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001012 pack.Tag{1, pack.BytesType}, pack.String("string"),
1013 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001014 ), build(
1015 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001016 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001017 pack.Tag{1, pack.BytesType}, pack.String("string"),
1018 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001019 )},
1020 wire: pack.Message{
1021 pack.Tag{1, pack.BytesType}, pack.String("string"),
1022 }.Marshal(),
1023 },
1024 {
1025 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001026 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001027 &testpb.TestAllTypes{
1028 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -07001029 }, &test3pb.TestAllTypes{
1030 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -08001031 },
1032 },
1033 wire: pack.Message{
1034 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
1035 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1036 pack.Tag{2, pack.BytesType}, pack.String("string"),
1037 }),
1038 }.Marshal(),
1039 },
Damien Neil96c229a2019-04-03 12:17:24 -07001040 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001041 desc: "required field in nil message unset",
1042 partial: true,
1043 decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
1044 },
1045 {
Damien Neil96c229a2019-04-03 12:17:24 -07001046 desc: "required field unset",
1047 partial: true,
1048 decodeTo: []proto.Message{&testpb.TestRequired{}},
1049 },
1050 {
1051 desc: "required field set",
1052 decodeTo: []proto.Message{&testpb.TestRequired{
1053 RequiredField: scalar.Int32(1),
1054 }},
1055 wire: pack.Message{
1056 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1057 }.Marshal(),
1058 },
1059 {
1060 desc: "required field in optional message unset",
1061 partial: true,
1062 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1063 OptionalMessage: &testpb.TestRequired{},
1064 }},
1065 wire: pack.Message{
1066 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1067 }.Marshal(),
1068 },
1069 {
1070 desc: "required field in optional message set",
1071 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1072 OptionalMessage: &testpb.TestRequired{
1073 RequiredField: scalar.Int32(1),
1074 },
1075 }},
1076 wire: pack.Message{
1077 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1078 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1079 }),
1080 }.Marshal(),
1081 },
Damien Neil4686e232019-04-05 13:31:40 -07001082 {
1083 desc: "required field in optional message set (split across multiple tags)",
1084 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1085 OptionalMessage: &testpb.TestRequired{
1086 RequiredField: scalar.Int32(1),
1087 },
1088 }},
1089 wire: pack.Message{
1090 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1091 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1092 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1093 }),
1094 }.Marshal(),
1095 },
Damien Neil96c229a2019-04-03 12:17:24 -07001096 {
1097 desc: "required field in repeated message unset",
1098 partial: true,
1099 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1100 RepeatedMessage: []*testpb.TestRequired{
1101 {RequiredField: scalar.Int32(1)},
1102 {},
1103 },
1104 }},
1105 wire: pack.Message{
1106 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1107 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1108 }),
1109 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1110 }.Marshal(),
1111 },
1112 {
1113 desc: "required field in repeated message set",
1114 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1115 RepeatedMessage: []*testpb.TestRequired{
1116 {RequiredField: scalar.Int32(1)},
1117 {RequiredField: scalar.Int32(2)},
1118 },
1119 }},
1120 wire: pack.Message{
1121 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1122 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1123 }),
1124 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1125 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1126 }),
1127 }.Marshal(),
1128 },
1129 {
1130 desc: "required field in map message unset",
1131 partial: true,
1132 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1133 MapMessage: map[int32]*testpb.TestRequired{
1134 1: {RequiredField: scalar.Int32(1)},
1135 2: {},
1136 },
1137 }},
1138 wire: pack.Message{
1139 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1140 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1141 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1142 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1143 }),
1144 }),
1145 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1146 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1147 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1148 }),
1149 }.Marshal(),
1150 },
1151 {
1152 desc: "required field in map message set",
1153 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1154 MapMessage: map[int32]*testpb.TestRequired{
1155 1: {RequiredField: scalar.Int32(1)},
1156 2: {RequiredField: scalar.Int32(2)},
1157 },
1158 }},
1159 wire: pack.Message{
1160 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1161 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1162 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1163 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1164 }),
1165 }),
1166 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1167 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1168 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1169 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1170 }),
1171 }),
1172 }.Marshal(),
1173 },
1174 {
1175 desc: "required field in optional group unset",
1176 partial: true,
1177 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1178 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1179 }},
1180 wire: pack.Message{
1181 pack.Tag{1, pack.StartGroupType},
1182 pack.Tag{1, pack.EndGroupType},
1183 }.Marshal(),
1184 },
1185 {
1186 desc: "required field in optional group set",
1187 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1188 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
1189 A: scalar.Int32(1),
1190 },
1191 }},
1192 wire: pack.Message{
1193 pack.Tag{1, pack.StartGroupType},
1194 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1195 pack.Tag{1, pack.EndGroupType},
1196 }.Marshal(),
1197 },
1198 {
1199 desc: "required field in repeated group unset",
1200 partial: true,
1201 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1202 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1203 {A: scalar.Int32(1)},
1204 {},
1205 },
1206 }},
1207 wire: pack.Message{
1208 pack.Tag{3, pack.StartGroupType},
1209 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1210 pack.Tag{3, pack.EndGroupType},
1211 pack.Tag{3, pack.StartGroupType},
1212 pack.Tag{3, pack.EndGroupType},
1213 }.Marshal(),
1214 },
1215 {
1216 desc: "required field in repeated group set",
1217 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1218 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1219 {A: scalar.Int32(1)},
1220 {A: scalar.Int32(2)},
1221 },
1222 }},
1223 wire: pack.Message{
1224 pack.Tag{3, pack.StartGroupType},
1225 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1226 pack.Tag{3, pack.EndGroupType},
1227 pack.Tag{3, pack.StartGroupType},
1228 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1229 pack.Tag{3, pack.EndGroupType},
1230 }.Marshal(),
1231 },
1232 {
Damien Neil5322bdb2019-04-09 15:57:05 -07001233 desc: "required field in oneof message unset",
1234 partial: true,
1235 decodeTo: []proto.Message{
1236 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1237 &testpb.TestRequired{},
1238 }},
1239 },
1240 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1241 },
1242 {
1243 desc: "required field in oneof message set",
1244 decodeTo: []proto.Message{
1245 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1246 &testpb.TestRequired{
1247 RequiredField: scalar.Int32(1),
1248 },
1249 }},
1250 },
1251 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
1252 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1253 })}.Marshal(),
1254 },
1255 {
Damien Neil96c229a2019-04-03 12:17:24 -07001256 desc: "required field in extension message unset",
1257 partial: true,
1258 invalidExtensions: true,
1259 decodeTo: []proto.Message{build(
1260 &testpb.TestAllExtensions{},
1261 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1262 )},
1263 wire: pack.Message{
1264 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1265 }.Marshal(),
1266 },
1267 {
1268 desc: "required field in extension message set",
1269 decodeTo: []proto.Message{build(
1270 &testpb.TestAllExtensions{},
1271 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
1272 RequiredField: scalar.Int32(1),
1273 }),
1274 )},
1275 wire: pack.Message{
1276 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1277 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1278 }),
1279 }.Marshal(),
1280 },
1281 {
1282 desc: "required field in repeated extension message unset",
1283 partial: true,
1284 invalidExtensions: true,
1285 decodeTo: []proto.Message{build(
1286 &testpb.TestAllExtensions{},
1287 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1288 {RequiredField: scalar.Int32(1)},
1289 {},
1290 }),
1291 )},
1292 wire: pack.Message{
1293 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1294 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1295 }),
1296 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1297 }.Marshal(),
1298 },
1299 {
1300 desc: "required field in repeated extension message set",
1301 decodeTo: []proto.Message{build(
1302 &testpb.TestAllExtensions{},
1303 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1304 {RequiredField: scalar.Int32(1)},
1305 {RequiredField: scalar.Int32(2)},
1306 }),
1307 )},
1308 wire: pack.Message{
1309 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1310 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1311 }),
1312 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1313 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1314 }),
1315 }.Marshal(),
1316 },
Damien Neilc37adef2019-04-01 13:49:56 -07001317 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001318 desc: "nil messages",
1319 decodeTo: []proto.Message{
1320 (*testpb.TestAllTypes)(nil),
1321 (*test3pb.TestAllTypes)(nil),
1322 (*testpb.TestAllExtensions)(nil),
1323 },
1324 },
1325 {
Damien Neilc37adef2019-04-01 13:49:56 -07001326 desc: "legacy",
1327 partial: true,
1328 decodeTo: []proto.Message{
1329 &legacypb.Legacy{
1330 F1: &legacy1pb.Message{
1331 OptionalInt32: scalar.Int32(1),
1332 OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
1333 OptionalChildMessage: &legacy1pb.Message_ChildMessage{
1334 F1: scalar.String("x"),
1335 },
1336 Optionalgroup: &legacy1pb.Message_OptionalGroup{
1337 F1: scalar.String("x"),
1338 },
1339 RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
1340 {F1: scalar.String("x")},
1341 },
1342 Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
1343 {F1: scalar.String("x")},
1344 },
1345 MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
1346 true: {F1: scalar.String("x")},
1347 },
1348 OneofUnion: &legacy1pb.Message_OneofChildMessage{
1349 &legacy1pb.Message_ChildMessage{
1350 F1: scalar.String("x"),
1351 },
1352 },
1353 },
1354 },
1355 },
1356 wire: pack.Message{
1357 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1358 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1359 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1360 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1361 pack.Tag{1, pack.BytesType}, pack.String("x"),
1362 }),
1363 pack.Tag{120, pack.StartGroupType},
1364 pack.Tag{1, pack.BytesType}, pack.String("x"),
1365 pack.Tag{120, pack.EndGroupType},
1366 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1367 pack.Tag{1, pack.BytesType}, pack.String("x"),
1368 }),
1369 pack.Tag{520, pack.StartGroupType},
1370 pack.Tag{1, pack.BytesType}, pack.String("x"),
1371 pack.Tag{520, pack.EndGroupType},
1372 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1373 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1374 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1375 pack.Tag{1, pack.BytesType}, pack.String("x"),
1376 }),
1377 }),
1378 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1379 pack.Tag{1, pack.BytesType}, pack.String("x"),
1380 }),
1381 }),
1382 }.Marshal(),
1383 },
Damien Neilba23aa52018-12-07 14:38:17 -08001384}
1385
Damien Neilbc310b52019-04-11 11:46:55 -07001386var invalidUTF8TestProtos = []testProto{
1387 {
1388 desc: "invalid UTF-8 in optional string field",
1389 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1390 OptionalString: "abc\xff",
1391 }},
1392 wire: pack.Message{
1393 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1394 }.Marshal(),
1395 },
1396 {
1397 desc: "invalid UTF-8 in repeated string field",
1398 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1399 RepeatedString: []string{"foo", "abc\xff"},
1400 }},
1401 wire: pack.Message{
1402 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1403 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1404 }.Marshal(),
1405 },
1406 {
1407 desc: "invalid UTF-8 in nested message",
1408 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1409 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1410 Corecursive: &test3pb.TestAllTypes{
1411 OptionalString: "abc\xff",
1412 },
1413 },
1414 }},
1415 wire: pack.Message{
1416 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1417 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1418 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1419 }),
1420 }),
1421 }.Marshal(),
1422 },
1423 {
Damien Neilc37adef2019-04-01 13:49:56 -07001424 desc: "invalid UTF-8 in oneof field",
1425 decodeTo: []proto.Message{
1426 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
1427 },
1428 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1429 },
1430 {
Damien Neilbc310b52019-04-11 11:46:55 -07001431 desc: "invalid UTF-8 in map key",
1432 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1433 MapStringString: map[string]string{"key\xff": "val"},
1434 }},
1435 wire: pack.Message{
1436 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1437 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1438 pack.Tag{2, pack.BytesType}, pack.String("val"),
1439 }),
1440 }.Marshal(),
1441 },
1442 {
1443 desc: "invalid UTF-8 in map value",
1444 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1445 MapStringString: map[string]string{"key": "val\xff"},
1446 }},
1447 wire: pack.Message{
1448 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1449 pack.Tag{1, pack.BytesType}, pack.String("key"),
1450 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1451 }),
1452 }.Marshal(),
1453 },
1454}
1455
Damien Neil4be2fb42018-12-17 11:16:16 -08001456func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001457 for _, opt := range opts {
1458 opt(m)
1459 }
1460 return m
1461}
1462
Damien Neil4be2fb42018-12-17 11:16:16 -08001463type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001464
Joe Tsai378c1322019-04-25 23:48:08 -07001465func unknown(raw pref.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001466 return func(m proto.Message) {
Joe Tsai378c1322019-04-25 23:48:08 -07001467 m.ProtoReflect().SetUnknown(raw)
Damien Neile6f060f2019-04-23 17:11:02 -07001468 }
1469}
1470
Damien Neilba23aa52018-12-07 14:38:17 -08001471func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001472 // TODO: Should ExtensionType.ValueOf accept []T instead of *[]T?
1473 t := reflect.TypeOf(value)
1474 if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
1475 v := reflect.New(t)
1476 v.Elem().Set(reflect.ValueOf(value))
1477 value = v.Interface()
1478 }
1479
Damien Neil4be2fb42018-12-17 11:16:16 -08001480 return func(m proto.Message) {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001481 xt := desc.Type
1482 m.ProtoReflect().Set(xt, xt.ValueOf(value))
Damien Neilba23aa52018-12-07 14:38:17 -08001483 }
1484}
Damien Neil61e93c72019-03-27 09:23:20 -07001485
1486func marshalText(m proto.Message) string {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001487 b, _ := prototext.MarshalOptions{Indent: "\t", AllowPartial: true}.Marshal(m)
Damien Neil61e93c72019-03-27 09:23:20 -07001488 return string(b)
1489}