blob: dda4db1d78ea2efccd7d99ce528c2d1333d809bd [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 Neil61e93c72019-03-27 09:23:20 -070013 "github.com/golang/protobuf/v2/encoding/textpb"
Damien Neilba23aa52018-12-07 14:38:17 -080014 "github.com/golang/protobuf/v2/internal/encoding/pack"
Damien Neilba23aa52018-12-07 14:38:17 -080015 "github.com/golang/protobuf/v2/internal/scalar"
Damien Neil4be2fb42018-12-17 11:16:16 -080016 "github.com/golang/protobuf/v2/proto"
Damien Neilba23aa52018-12-07 14:38:17 -080017 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
Joe Tsai19058432019-02-27 21:46:29 -080018
19 testpb "github.com/golang/protobuf/v2/internal/testprotos/test"
Damien Neil3b46ade2019-03-26 13:55:02 -070020 test3pb "github.com/golang/protobuf/v2/internal/testprotos/test3"
Damien Neilba23aa52018-12-07 14:38:17 -080021)
22
23type testProto struct {
Damien Neil96c229a2019-04-03 12:17:24 -070024 desc string
25 decodeTo []proto.Message
26 wire []byte
27 partial bool
28 invalidExtensions bool
Damien Neilba23aa52018-12-07 14:38:17 -080029}
30
31func TestDecode(t *testing.T) {
32 for _, test := range testProtos {
33 for _, want := range test.decodeTo {
34 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070035 opts := proto.UnmarshalOptions{
36 AllowPartial: test.partial,
37 }
Damien Neilba23aa52018-12-07 14:38:17 -080038 wire := append(([]byte)(nil), test.wire...)
Damien Neil4be2fb42018-12-17 11:16:16 -080039 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
Damien Neil96c229a2019-04-03 12:17:24 -070040 if err := opts.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070041 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080042 return
43 }
44
45 // Aliasing check: Modifying the original wire bytes shouldn't
46 // affect the unmarshaled message.
47 for i := range wire {
48 wire[i] = 0
49 }
50
Damien Neil96c229a2019-04-03 12:17:24 -070051 if test.invalidExtensions {
52 // Equal doesn't work on messages containing invalid extension data.
53 return
54 }
Damien Neilba23aa52018-12-07 14:38:17 -080055 if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) {
Damien Neil61e93c72019-03-27 09:23:20 -070056 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080057 }
58 })
59 }
60 }
61}
62
Damien Neil96c229a2019-04-03 12:17:24 -070063func TestDecodeRequiredFieldChecks(t *testing.T) {
64 for _, test := range testProtos {
65 if !test.partial {
66 continue
67 }
68 if test.invalidExtensions {
69 // Missing required fields in extensions just end up in the unknown fields.
70 continue
71 }
72 for _, m := range test.decodeTo {
73 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
74 got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
75 if err := proto.Unmarshal(test.wire, got); err == nil {
76 t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got))
77 }
78 })
79 }
80 }
81}
82
Damien Neilba23aa52018-12-07 14:38:17 -080083var testProtos = []testProto{
84 {
85 desc: "basic scalar types",
Damien Neil4be2fb42018-12-17 11:16:16 -080086 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -080087 OptionalInt32: scalar.Int32(1001),
88 OptionalInt64: scalar.Int64(1002),
89 OptionalUint32: scalar.Uint32(1003),
90 OptionalUint64: scalar.Uint64(1004),
91 OptionalSint32: scalar.Int32(1005),
92 OptionalSint64: scalar.Int64(1006),
93 OptionalFixed32: scalar.Uint32(1007),
94 OptionalFixed64: scalar.Uint64(1008),
95 OptionalSfixed32: scalar.Int32(1009),
96 OptionalSfixed64: scalar.Int64(1010),
97 OptionalFloat: scalar.Float32(1011.5),
98 OptionalDouble: scalar.Float64(1012.5),
99 OptionalBool: scalar.Bool(true),
100 OptionalString: scalar.String("string"),
101 OptionalBytes: []byte("bytes"),
102 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
Damien Neil3b46ade2019-03-26 13:55:02 -0700103 }, &test3pb.TestAllTypes{
104 OptionalInt32: 1001,
105 OptionalInt64: 1002,
106 OptionalUint32: 1003,
107 OptionalUint64: 1004,
108 OptionalSint32: 1005,
109 OptionalSint64: 1006,
110 OptionalFixed32: 1007,
111 OptionalFixed64: 1008,
112 OptionalSfixed32: 1009,
113 OptionalSfixed64: 1010,
114 OptionalFloat: 1011.5,
115 OptionalDouble: 1012.5,
116 OptionalBool: true,
117 OptionalString: "string",
118 OptionalBytes: []byte("bytes"),
119 OptionalNestedEnum: test3pb.TestAllTypes_BAR,
Damien Neilba23aa52018-12-07 14:38:17 -0800120 }, build(
121 &testpb.TestAllExtensions{},
122 extend(testpb.E_OptionalInt32Extension, scalar.Int32(1001)),
123 extend(testpb.E_OptionalInt64Extension, scalar.Int64(1002)),
124 extend(testpb.E_OptionalUint32Extension, scalar.Uint32(1003)),
125 extend(testpb.E_OptionalUint64Extension, scalar.Uint64(1004)),
126 extend(testpb.E_OptionalSint32Extension, scalar.Int32(1005)),
127 extend(testpb.E_OptionalSint64Extension, scalar.Int64(1006)),
128 extend(testpb.E_OptionalFixed32Extension, scalar.Uint32(1007)),
129 extend(testpb.E_OptionalFixed64Extension, scalar.Uint64(1008)),
130 extend(testpb.E_OptionalSfixed32Extension, scalar.Int32(1009)),
131 extend(testpb.E_OptionalSfixed64Extension, scalar.Int64(1010)),
132 extend(testpb.E_OptionalFloatExtension, scalar.Float32(1011.5)),
133 extend(testpb.E_OptionalDoubleExtension, scalar.Float64(1012.5)),
134 extend(testpb.E_OptionalBoolExtension, scalar.Bool(true)),
135 extend(testpb.E_OptionalStringExtension, scalar.String("string")),
136 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
137 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR.Enum()),
138 )},
139 wire: pack.Message{
140 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
141 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
142 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
143 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
144 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
145 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
146 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
147 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
148 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
149 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
150 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
151 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
152 pack.Tag{13, pack.VarintType}, pack.Bool(true),
153 pack.Tag{14, pack.BytesType}, pack.String("string"),
154 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
155 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
156 }.Marshal(),
157 },
158 {
159 desc: "groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800160 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800161 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
162 A: scalar.Int32(1017),
163 },
164 }, build(
165 &testpb.TestAllExtensions{},
166 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
167 A: scalar.Int32(1017),
168 }),
169 )},
170 wire: pack.Message{
171 pack.Tag{16, pack.StartGroupType},
172 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
173 pack.Tag{16, pack.EndGroupType},
174 }.Marshal(),
175 },
176 {
177 desc: "groups (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800178 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800179 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
180 A: scalar.Int32(2),
181 },
182 }, build(
183 &testpb.TestAllExtensions{},
184 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
185 A: scalar.Int32(2),
186 }),
187 )},
188 wire: pack.Message{
189 pack.Tag{16, pack.StartGroupType},
190 pack.Tag{17, pack.VarintType}, pack.Varint(1),
191 pack.Tag{16, pack.EndGroupType},
192 pack.Tag{16, pack.StartGroupType},
193 pack.Tag{17, pack.VarintType}, pack.Varint(2),
194 pack.Tag{16, pack.EndGroupType},
195 }.Marshal(),
196 },
197 {
198 desc: "messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800199 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800200 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
201 A: scalar.Int32(42),
202 Corecursive: &testpb.TestAllTypes{
203 OptionalInt32: scalar.Int32(43),
204 },
205 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700206 }, &test3pb.TestAllTypes{
207 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
208 A: 42,
209 Corecursive: &test3pb.TestAllTypes{
210 OptionalInt32: 43,
211 },
212 },
Damien Neilba23aa52018-12-07 14:38:17 -0800213 }, build(
214 &testpb.TestAllExtensions{},
215 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
216 A: scalar.Int32(42),
217 Corecursive: &testpb.TestAllTypes{
218 OptionalInt32: scalar.Int32(43),
219 },
220 }),
221 )},
222 wire: pack.Message{
223 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
224 pack.Tag{1, pack.VarintType}, pack.Varint(42),
225 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
226 pack.Tag{1, pack.VarintType}, pack.Varint(43),
227 }),
228 }),
229 }.Marshal(),
230 },
231 {
232 desc: "messages (split across multiple tags)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800233 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800234 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
235 A: scalar.Int32(42),
236 Corecursive: &testpb.TestAllTypes{
237 OptionalInt32: scalar.Int32(43),
238 },
239 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700240 }, &test3pb.TestAllTypes{
241 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
242 A: 42,
243 Corecursive: &test3pb.TestAllTypes{
244 OptionalInt32: 43,
245 },
246 },
Damien Neilba23aa52018-12-07 14:38:17 -0800247 }, build(
248 &testpb.TestAllExtensions{},
249 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
250 A: scalar.Int32(42),
251 Corecursive: &testpb.TestAllTypes{
252 OptionalInt32: scalar.Int32(43),
253 },
254 }),
255 )},
256 wire: pack.Message{
257 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
258 pack.Tag{1, pack.VarintType}, pack.Varint(42),
259 }),
260 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
261 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
262 pack.Tag{1, pack.VarintType}, pack.Varint(43),
263 }),
264 }),
265 }.Marshal(),
266 },
267 {
268 desc: "messages (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800269 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800270 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
271 A: scalar.Int32(2),
272 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700273 }, &test3pb.TestAllTypes{
274 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
275 A: 2,
276 },
Damien Neilba23aa52018-12-07 14:38:17 -0800277 }, build(
278 &testpb.TestAllExtensions{},
279 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
280 A: scalar.Int32(2),
281 }),
282 )},
283 wire: pack.Message{
284 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
285 pack.Tag{1, pack.VarintType}, pack.Varint(1),
286 }),
287 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
288 pack.Tag{1, pack.VarintType}, pack.Varint(2),
289 }),
290 }.Marshal(),
291 },
292 {
293 desc: "basic repeated types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800294 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800295 RepeatedInt32: []int32{1001, 2001},
296 RepeatedInt64: []int64{1002, 2002},
297 RepeatedUint32: []uint32{1003, 2003},
298 RepeatedUint64: []uint64{1004, 2004},
299 RepeatedSint32: []int32{1005, 2005},
300 RepeatedSint64: []int64{1006, 2006},
301 RepeatedFixed32: []uint32{1007, 2007},
302 RepeatedFixed64: []uint64{1008, 2008},
303 RepeatedSfixed32: []int32{1009, 2009},
304 RepeatedSfixed64: []int64{1010, 2010},
305 RepeatedFloat: []float32{1011.5, 2011.5},
306 RepeatedDouble: []float64{1012.5, 2012.5},
307 RepeatedBool: []bool{true, false},
308 RepeatedString: []string{"foo", "bar"},
309 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
310 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
311 testpb.TestAllTypes_FOO,
312 testpb.TestAllTypes_BAR,
313 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700314 }, &test3pb.TestAllTypes{
315 RepeatedInt32: []int32{1001, 2001},
316 RepeatedInt64: []int64{1002, 2002},
317 RepeatedUint32: []uint32{1003, 2003},
318 RepeatedUint64: []uint64{1004, 2004},
319 RepeatedSint32: []int32{1005, 2005},
320 RepeatedSint64: []int64{1006, 2006},
321 RepeatedFixed32: []uint32{1007, 2007},
322 RepeatedFixed64: []uint64{1008, 2008},
323 RepeatedSfixed32: []int32{1009, 2009},
324 RepeatedSfixed64: []int64{1010, 2010},
325 RepeatedFloat: []float32{1011.5, 2011.5},
326 RepeatedDouble: []float64{1012.5, 2012.5},
327 RepeatedBool: []bool{true, false},
328 RepeatedString: []string{"foo", "bar"},
329 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
330 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
331 test3pb.TestAllTypes_FOO,
332 test3pb.TestAllTypes_BAR,
333 },
Damien Neilba23aa52018-12-07 14:38:17 -0800334 }, build(
335 &testpb.TestAllExtensions{},
336 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
337 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
338 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
339 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
340 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
341 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
342 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
343 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
344 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
345 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
346 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
347 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
348 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
349 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
350 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
351 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
352 testpb.TestAllTypes_FOO,
353 testpb.TestAllTypes_BAR,
354 }),
355 )},
356 wire: pack.Message{
357 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
358 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
359 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
360 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
361 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
362 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
363 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
364 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
365 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
366 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
367 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
368 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
369 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
370 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
371 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
372 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
373 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
374 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
375 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
376 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
377 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
378 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
379 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
380 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
381 pack.Tag{43, pack.VarintType}, pack.Bool(true),
382 pack.Tag{43, pack.VarintType}, pack.Bool(false),
383 pack.Tag{44, pack.BytesType}, pack.String("foo"),
384 pack.Tag{44, pack.BytesType}, pack.String("bar"),
385 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
386 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
387 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
388 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
389 }.Marshal(),
390 },
391 {
392 desc: "basic repeated types (packed encoding)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800393 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800394 RepeatedInt32: []int32{1001, 2001},
395 RepeatedInt64: []int64{1002, 2002},
396 RepeatedUint32: []uint32{1003, 2003},
397 RepeatedUint64: []uint64{1004, 2004},
398 RepeatedSint32: []int32{1005, 2005},
399 RepeatedSint64: []int64{1006, 2006},
400 RepeatedFixed32: []uint32{1007, 2007},
401 RepeatedFixed64: []uint64{1008, 2008},
402 RepeatedSfixed32: []int32{1009, 2009},
403 RepeatedSfixed64: []int64{1010, 2010},
404 RepeatedFloat: []float32{1011.5, 2011.5},
405 RepeatedDouble: []float64{1012.5, 2012.5},
406 RepeatedBool: []bool{true, false},
407 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
408 testpb.TestAllTypes_FOO,
409 testpb.TestAllTypes_BAR,
410 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700411 }, &test3pb.TestAllTypes{
412 RepeatedInt32: []int32{1001, 2001},
413 RepeatedInt64: []int64{1002, 2002},
414 RepeatedUint32: []uint32{1003, 2003},
415 RepeatedUint64: []uint64{1004, 2004},
416 RepeatedSint32: []int32{1005, 2005},
417 RepeatedSint64: []int64{1006, 2006},
418 RepeatedFixed32: []uint32{1007, 2007},
419 RepeatedFixed64: []uint64{1008, 2008},
420 RepeatedSfixed32: []int32{1009, 2009},
421 RepeatedSfixed64: []int64{1010, 2010},
422 RepeatedFloat: []float32{1011.5, 2011.5},
423 RepeatedDouble: []float64{1012.5, 2012.5},
424 RepeatedBool: []bool{true, false},
425 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
426 test3pb.TestAllTypes_FOO,
427 test3pb.TestAllTypes_BAR,
428 },
Damien Neilba23aa52018-12-07 14:38:17 -0800429 }, build(
430 &testpb.TestAllExtensions{},
431 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
432 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
433 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
434 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
435 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
436 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
437 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
438 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
439 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
440 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
441 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
442 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
443 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
444 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
445 testpb.TestAllTypes_FOO,
446 testpb.TestAllTypes_BAR,
447 }),
448 )},
449 wire: pack.Message{
450 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
451 pack.Varint(1001), pack.Varint(2001),
452 },
453 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
454 pack.Varint(1002), pack.Varint(2002),
455 },
456 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
457 pack.Uvarint(1003), pack.Uvarint(2003),
458 },
459 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
460 pack.Uvarint(1004), pack.Uvarint(2004),
461 },
462 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
463 pack.Svarint(1005), pack.Svarint(2005),
464 },
465 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
466 pack.Svarint(1006), pack.Svarint(2006),
467 },
468 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
469 pack.Uint32(1007), pack.Uint32(2007),
470 },
471 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
472 pack.Uint64(1008), pack.Uint64(2008),
473 },
474 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
475 pack.Int32(1009), pack.Int32(2009),
476 },
477 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
478 pack.Int64(1010), pack.Int64(2010),
479 },
480 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
481 pack.Float32(1011.5), pack.Float32(2011.5),
482 },
483 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
484 pack.Float64(1012.5), pack.Float64(2012.5),
485 },
486 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
487 pack.Bool(true), pack.Bool(false),
488 },
489 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
490 pack.Varint(int(testpb.TestAllTypes_FOO)),
491 pack.Varint(int(testpb.TestAllTypes_BAR)),
492 },
493 }.Marshal(),
494 },
495 {
496 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800497 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800498 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
499 {A: scalar.Int32(1)},
500 {A: scalar.Int32(2)},
501 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700502 }, &test3pb.TestAllTypes{
503 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
504 {A: 1},
505 {A: 2},
506 },
Damien Neilba23aa52018-12-07 14:38:17 -0800507 }, build(
508 &testpb.TestAllExtensions{},
509 extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
510 {A: scalar.Int32(1)},
511 {A: scalar.Int32(2)},
512 }),
513 )},
514 wire: pack.Message{
515 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
516 pack.Tag{1, pack.VarintType}, pack.Varint(1),
517 }),
518 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
519 pack.Tag{1, pack.VarintType}, pack.Varint(2),
520 }),
521 }.Marshal(),
522 },
523 {
524 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800525 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800526 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
527 {A: scalar.Int32(1017)},
528 {A: scalar.Int32(2017)},
529 },
530 }, build(
531 &testpb.TestAllExtensions{},
532 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
533 {A: scalar.Int32(1017)},
534 {A: scalar.Int32(2017)},
535 }),
536 )},
537 wire: pack.Message{
538 pack.Tag{46, pack.StartGroupType},
539 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
540 pack.Tag{46, pack.EndGroupType},
541 pack.Tag{46, pack.StartGroupType},
542 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
543 pack.Tag{46, pack.EndGroupType},
544 }.Marshal(),
545 },
546 {
547 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800548 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800549 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
550 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
551 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
552 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
553 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
554 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
555 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
556 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
557 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
558 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
559 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
560 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
561 MapBoolBool: map[bool]bool{true: false, false: true},
562 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
563 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
564 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
565 "71.1.key": {A: scalar.Int32(1171)},
566 "71.2.key": {A: scalar.Int32(2171)},
567 },
568 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
569 "73.1.key": testpb.TestAllTypes_FOO,
570 "73.2.key": testpb.TestAllTypes_BAR,
571 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700572 }, &test3pb.TestAllTypes{
573 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
574 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
575 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
576 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
577 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
578 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
579 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
580 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
581 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
582 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
583 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
584 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
585 MapBoolBool: map[bool]bool{true: false, false: true},
586 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
587 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
588 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
589 "71.1.key": {A: 1171},
590 "71.2.key": {A: 2171},
591 },
592 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
593 "73.1.key": test3pb.TestAllTypes_FOO,
594 "73.2.key": test3pb.TestAllTypes_BAR,
595 },
Damien Neilba23aa52018-12-07 14:38:17 -0800596 }},
597 wire: pack.Message{
598 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
599 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
600 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
601 }),
602 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
603 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
604 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
605 }),
606 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
607 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
608 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
609 }),
610 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
611 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
612 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
613 }),
614 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
615 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
616 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
617 }),
618 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
619 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
620 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
621 }),
622 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
623 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
624 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
625 }),
626 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
627 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
628 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
629 }),
630 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
631 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
632 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
633 }),
634 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
635 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
636 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
637 }),
638 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
639 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
640 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
641 }),
642 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
643 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
644 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
645 }),
646 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
647 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
648 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
649 }),
650 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
651 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
652 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
653 }),
654 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
655 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
656 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
657 }),
658 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
659 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
660 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
661 }),
662 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
663 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
664 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
665 }),
666 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
667 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
668 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
669 }),
670 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
671 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
672 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
673 }),
674 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
675 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
676 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
677 }),
678 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
679 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
680 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
681 }),
682 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
683 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
684 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
685 }),
686 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
687 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
688 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
689 }),
690 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
691 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
692 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
693 }),
694 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
695 pack.Tag{1, pack.VarintType}, pack.Bool(true),
696 pack.Tag{2, pack.VarintType}, pack.Bool(false),
697 }),
698 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
699 pack.Tag{1, pack.VarintType}, pack.Bool(false),
700 pack.Tag{2, pack.VarintType}, pack.Bool(true),
701 }),
702 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
703 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
704 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
705 }),
706 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
707 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
708 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
709 }),
710 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
711 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
712 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
713 }),
714 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
715 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
716 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
717 }),
718 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
719 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
720 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
721 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
722 }),
723 }),
724 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
725 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
726 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
727 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
728 }),
729 }),
730 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
731 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
732 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
733 }),
734 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
735 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
736 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
737 }),
738 }.Marshal(),
739 },
740 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700741 desc: "oneof (uint32)",
742 decodeTo: []proto.Message{
743 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
744 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
745 },
746 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800747 },
748 {
749 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700750 decodeTo: []proto.Message{
751 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
752 &testpb.TestAllTypes_NestedMessage{A: scalar.Int32(1112)},
753 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
754 &test3pb.TestAllTypes_NestedMessage{A: 1112},
755 }},
756 },
Damien Neilba23aa52018-12-07 14:38:17 -0800757 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
758 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
759 })}.Marshal(),
760 },
761 {
762 desc: "oneof (overridden message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700763 decodeTo: []proto.Message{
764 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
765 &testpb.TestAllTypes_NestedMessage{
766 Corecursive: &testpb.TestAllTypes{
767 OptionalInt32: scalar.Int32(43),
768 },
Damien Neilba23aa52018-12-07 14:38:17 -0800769 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700770 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
771 &test3pb.TestAllTypes_NestedMessage{
772 Corecursive: &test3pb.TestAllTypes{
773 OptionalInt32: 43,
774 },
775 },
776 }}},
Damien Neilba23aa52018-12-07 14:38:17 -0800777 wire: pack.Message{
778 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
779 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
780 }),
781 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
782 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
783 pack.Tag{1, pack.VarintType}, pack.Varint(43),
784 }),
785 }),
786 }.Marshal(),
787 },
788 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700789 desc: "oneof (string)",
790 decodeTo: []proto.Message{
791 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
792 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
793 },
794 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800795 },
796 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700797 desc: "oneof (bytes)",
798 decodeTo: []proto.Message{
799 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
800 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
801 },
802 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800803 },
804 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700805 desc: "oneof (bool)",
806 decodeTo: []proto.Message{
807 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
808 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
809 },
810 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800811 },
812 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700813 desc: "oneof (uint64)",
814 decodeTo: []proto.Message{
815 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
816 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
817 },
818 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800819 },
820 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700821 desc: "oneof (float)",
822 decodeTo: []proto.Message{
823 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
824 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
825 },
826 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800827 },
828 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700829 desc: "oneof (double)",
830 decodeTo: []proto.Message{
831 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
832 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
833 },
834 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800835 },
836 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700837 desc: "oneof (enum)",
838 decodeTo: []proto.Message{
839 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
840 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
841 },
842 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800843 },
844 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700845 desc: "oneof (overridden value)",
846 decodeTo: []proto.Message{
847 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
848 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
849 },
Damien Neilba23aa52018-12-07 14:38:17 -0800850 wire: pack.Message{
851 pack.Tag{111, pack.VarintType}, pack.Varint(1),
852 pack.Tag{116, pack.VarintType}, pack.Varint(2),
853 }.Marshal(),
854 },
855 // TODO: More unknown field tests for ordering, repeated fields, etc.
856 //
857 // It is currently impossible to produce results that the v1 Equal
858 // considers equivalent to those of the v1 decoder. Figure out if
859 // that's a problem or not.
860 {
861 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -0800862 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -0800863 &testpb.TestAllTypes{},
864 unknown(100000, pack.Message{
865 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
866 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -0700867 ), build(
868 &test3pb.TestAllTypes{},
869 unknown(100000, pack.Message{
870 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
871 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -0800872 )},
873 wire: pack.Message{
874 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
875 }.Marshal(),
876 },
877 {
878 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -0800879 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -0800880 &testpb.TestAllTypes{},
881 unknown(1, pack.Message{
882 pack.Tag{1, pack.BytesType}, pack.String("string"),
883 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -0700884 ), build(
885 &test3pb.TestAllTypes{},
886 unknown(1, pack.Message{
887 pack.Tag{1, pack.BytesType}, pack.String("string"),
888 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -0800889 )},
890 wire: pack.Message{
891 pack.Tag{1, pack.BytesType}, pack.String("string"),
892 }.Marshal(),
893 },
894 {
895 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -0800896 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -0800897 &testpb.TestAllTypes{
898 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -0700899 }, &test3pb.TestAllTypes{
900 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -0800901 },
902 },
903 wire: pack.Message{
904 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
905 pack.Tag{1, pack.VarintType}, pack.Varint(1),
906 pack.Tag{2, pack.BytesType}, pack.String("string"),
907 }),
908 }.Marshal(),
909 },
Damien Neil96c229a2019-04-03 12:17:24 -0700910 {
911 desc: "required field unset",
912 partial: true,
913 decodeTo: []proto.Message{&testpb.TestRequired{}},
914 },
915 {
916 desc: "required field set",
917 decodeTo: []proto.Message{&testpb.TestRequired{
918 RequiredField: scalar.Int32(1),
919 }},
920 wire: pack.Message{
921 pack.Tag{1, pack.VarintType}, pack.Varint(1),
922 }.Marshal(),
923 },
924 {
925 desc: "required field in optional message unset",
926 partial: true,
927 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
928 OptionalMessage: &testpb.TestRequired{},
929 }},
930 wire: pack.Message{
931 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
932 }.Marshal(),
933 },
934 {
935 desc: "required field in optional message set",
936 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
937 OptionalMessage: &testpb.TestRequired{
938 RequiredField: scalar.Int32(1),
939 },
940 }},
941 wire: pack.Message{
942 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
943 pack.Tag{1, pack.VarintType}, pack.Varint(1),
944 }),
945 }.Marshal(),
946 },
Damien Neil4686e232019-04-05 13:31:40 -0700947 {
948 desc: "required field in optional message set (split across multiple tags)",
949 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
950 OptionalMessage: &testpb.TestRequired{
951 RequiredField: scalar.Int32(1),
952 },
953 }},
954 wire: pack.Message{
955 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
956 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
957 pack.Tag{1, pack.VarintType}, pack.Varint(1),
958 }),
959 }.Marshal(),
960 },
Damien Neil96c229a2019-04-03 12:17:24 -0700961 {
962 desc: "required field in repeated message unset",
963 partial: true,
964 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
965 RepeatedMessage: []*testpb.TestRequired{
966 {RequiredField: scalar.Int32(1)},
967 {},
968 },
969 }},
970 wire: pack.Message{
971 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
972 pack.Tag{1, pack.VarintType}, pack.Varint(1),
973 }),
974 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
975 }.Marshal(),
976 },
977 {
978 desc: "required field in repeated message set",
979 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
980 RepeatedMessage: []*testpb.TestRequired{
981 {RequiredField: scalar.Int32(1)},
982 {RequiredField: scalar.Int32(2)},
983 },
984 }},
985 wire: pack.Message{
986 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
987 pack.Tag{1, pack.VarintType}, pack.Varint(1),
988 }),
989 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
990 pack.Tag{1, pack.VarintType}, pack.Varint(2),
991 }),
992 }.Marshal(),
993 },
994 {
995 desc: "required field in map message unset",
996 partial: true,
997 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
998 MapMessage: map[int32]*testpb.TestRequired{
999 1: {RequiredField: scalar.Int32(1)},
1000 2: {},
1001 },
1002 }},
1003 wire: pack.Message{
1004 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1005 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1006 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1007 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1008 }),
1009 }),
1010 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1011 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1012 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1013 }),
1014 }.Marshal(),
1015 },
1016 {
1017 desc: "required field in map message set",
1018 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1019 MapMessage: map[int32]*testpb.TestRequired{
1020 1: {RequiredField: scalar.Int32(1)},
1021 2: {RequiredField: scalar.Int32(2)},
1022 },
1023 }},
1024 wire: pack.Message{
1025 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1026 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1027 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1028 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1029 }),
1030 }),
1031 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1032 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1033 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1034 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1035 }),
1036 }),
1037 }.Marshal(),
1038 },
1039 {
1040 desc: "required field in optional group unset",
1041 partial: true,
1042 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1043 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1044 }},
1045 wire: pack.Message{
1046 pack.Tag{1, pack.StartGroupType},
1047 pack.Tag{1, pack.EndGroupType},
1048 }.Marshal(),
1049 },
1050 {
1051 desc: "required field in optional group set",
1052 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1053 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
1054 A: scalar.Int32(1),
1055 },
1056 }},
1057 wire: pack.Message{
1058 pack.Tag{1, pack.StartGroupType},
1059 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1060 pack.Tag{1, pack.EndGroupType},
1061 }.Marshal(),
1062 },
1063 {
1064 desc: "required field in repeated group unset",
1065 partial: true,
1066 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1067 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1068 {A: scalar.Int32(1)},
1069 {},
1070 },
1071 }},
1072 wire: pack.Message{
1073 pack.Tag{3, pack.StartGroupType},
1074 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1075 pack.Tag{3, pack.EndGroupType},
1076 pack.Tag{3, pack.StartGroupType},
1077 pack.Tag{3, pack.EndGroupType},
1078 }.Marshal(),
1079 },
1080 {
1081 desc: "required field in repeated group set",
1082 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1083 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1084 {A: scalar.Int32(1)},
1085 {A: scalar.Int32(2)},
1086 },
1087 }},
1088 wire: pack.Message{
1089 pack.Tag{3, pack.StartGroupType},
1090 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1091 pack.Tag{3, pack.EndGroupType},
1092 pack.Tag{3, pack.StartGroupType},
1093 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1094 pack.Tag{3, pack.EndGroupType},
1095 }.Marshal(),
1096 },
1097 {
1098 desc: "required field in extension message unset",
1099 partial: true,
1100 invalidExtensions: true,
1101 decodeTo: []proto.Message{build(
1102 &testpb.TestAllExtensions{},
1103 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1104 )},
1105 wire: pack.Message{
1106 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1107 }.Marshal(),
1108 },
1109 {
1110 desc: "required field in extension message set",
1111 decodeTo: []proto.Message{build(
1112 &testpb.TestAllExtensions{},
1113 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
1114 RequiredField: scalar.Int32(1),
1115 }),
1116 )},
1117 wire: pack.Message{
1118 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1119 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1120 }),
1121 }.Marshal(),
1122 },
1123 {
1124 desc: "required field in repeated extension message unset",
1125 partial: true,
1126 invalidExtensions: true,
1127 decodeTo: []proto.Message{build(
1128 &testpb.TestAllExtensions{},
1129 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1130 {RequiredField: scalar.Int32(1)},
1131 {},
1132 }),
1133 )},
1134 wire: pack.Message{
1135 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1136 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1137 }),
1138 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1139 }.Marshal(),
1140 },
1141 {
1142 desc: "required field in repeated extension message set",
1143 decodeTo: []proto.Message{build(
1144 &testpb.TestAllExtensions{},
1145 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1146 {RequiredField: scalar.Int32(1)},
1147 {RequiredField: scalar.Int32(2)},
1148 }),
1149 )},
1150 wire: pack.Message{
1151 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1152 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1153 }),
1154 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1155 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1156 }),
1157 }.Marshal(),
1158 },
Damien Neilba23aa52018-12-07 14:38:17 -08001159}
1160
Damien Neil4be2fb42018-12-17 11:16:16 -08001161func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001162 for _, opt := range opts {
1163 opt(m)
1164 }
1165 return m
1166}
1167
Damien Neil4be2fb42018-12-17 11:16:16 -08001168type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001169
1170func unknown(num pref.FieldNumber, raw pref.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001171 return func(m proto.Message) {
Damien Neilba23aa52018-12-07 14:38:17 -08001172 m.ProtoReflect().UnknownFields().Set(num, raw)
1173 }
1174}
1175
1176func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001177 return func(m proto.Message) {
Damien Neilba23aa52018-12-07 14:38:17 -08001178 if err := protoV1.SetExtension(m.(protoV1.Message), desc, value); err != nil {
1179 panic(err)
1180 }
1181 }
1182}
Damien Neil61e93c72019-03-27 09:23:20 -07001183
1184func marshalText(m proto.Message) string {
1185 b, _ := textpb.Marshal(m)
1186 return string(b)
1187}