blob: 2c95f6b26d49e9c4676de1369f3ba55859d02865 [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 Neilbc310b52019-04-11 11:46:55 -070015 "github.com/golang/protobuf/v2/internal/errors"
Damien Neilba23aa52018-12-07 14:38:17 -080016 "github.com/golang/protobuf/v2/internal/scalar"
Damien Neil4be2fb42018-12-17 11:16:16 -080017 "github.com/golang/protobuf/v2/proto"
Damien Neilba23aa52018-12-07 14:38:17 -080018 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
Joe Tsai19058432019-02-27 21:46:29 -080019
20 testpb "github.com/golang/protobuf/v2/internal/testprotos/test"
Damien Neil3b46ade2019-03-26 13:55:02 -070021 test3pb "github.com/golang/protobuf/v2/internal/testprotos/test3"
Damien Neilba23aa52018-12-07 14:38:17 -080022)
23
24type testProto struct {
Damien Neil96c229a2019-04-03 12:17:24 -070025 desc string
26 decodeTo []proto.Message
27 wire []byte
28 partial bool
29 invalidExtensions bool
Damien Neilba23aa52018-12-07 14:38:17 -080030}
31
32func TestDecode(t *testing.T) {
33 for _, test := range testProtos {
34 for _, want := range test.decodeTo {
35 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070036 opts := proto.UnmarshalOptions{
37 AllowPartial: test.partial,
38 }
Damien Neilba23aa52018-12-07 14:38:17 -080039 wire := append(([]byte)(nil), test.wire...)
Damien Neil4be2fb42018-12-17 11:16:16 -080040 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
Damien Neil96c229a2019-04-03 12:17:24 -070041 if err := opts.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070042 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080043 return
44 }
45
46 // Aliasing check: Modifying the original wire bytes shouldn't
47 // affect the unmarshaled message.
48 for i := range wire {
49 wire[i] = 0
50 }
51
Damien Neil96c229a2019-04-03 12:17:24 -070052 if test.invalidExtensions {
53 // Equal doesn't work on messages containing invalid extension data.
54 return
55 }
Damien Neilba23aa52018-12-07 14:38:17 -080056 if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) {
Damien Neil61e93c72019-03-27 09:23:20 -070057 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080058 }
59 })
60 }
61 }
62}
63
Damien Neil96c229a2019-04-03 12:17:24 -070064func TestDecodeRequiredFieldChecks(t *testing.T) {
65 for _, test := range testProtos {
66 if !test.partial {
67 continue
68 }
69 if test.invalidExtensions {
70 // Missing required fields in extensions just end up in the unknown fields.
71 continue
72 }
73 for _, m := range test.decodeTo {
74 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
75 got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
76 if err := proto.Unmarshal(test.wire, got); err == nil {
77 t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got))
78 }
79 })
80 }
81 }
82}
83
Damien Neilbc310b52019-04-11 11:46:55 -070084func TestDecodeInvalidUTF8(t *testing.T) {
85 for _, test := range invalidUTF8TestProtos {
86 for _, want := range test.decodeTo {
87 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
88 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
89 err := proto.Unmarshal(test.wire, got)
90 if !isErrInvalidUTF8(err) {
91 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
92 }
93 if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) {
94 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
95 }
96 })
97 }
98 }
99}
100
Damien Neilba23aa52018-12-07 14:38:17 -0800101var testProtos = []testProto{
102 {
103 desc: "basic scalar types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800104 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800105 OptionalInt32: scalar.Int32(1001),
106 OptionalInt64: scalar.Int64(1002),
107 OptionalUint32: scalar.Uint32(1003),
108 OptionalUint64: scalar.Uint64(1004),
109 OptionalSint32: scalar.Int32(1005),
110 OptionalSint64: scalar.Int64(1006),
111 OptionalFixed32: scalar.Uint32(1007),
112 OptionalFixed64: scalar.Uint64(1008),
113 OptionalSfixed32: scalar.Int32(1009),
114 OptionalSfixed64: scalar.Int64(1010),
115 OptionalFloat: scalar.Float32(1011.5),
116 OptionalDouble: scalar.Float64(1012.5),
117 OptionalBool: scalar.Bool(true),
118 OptionalString: scalar.String("string"),
119 OptionalBytes: []byte("bytes"),
120 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
Damien Neil3b46ade2019-03-26 13:55:02 -0700121 }, &test3pb.TestAllTypes{
122 OptionalInt32: 1001,
123 OptionalInt64: 1002,
124 OptionalUint32: 1003,
125 OptionalUint64: 1004,
126 OptionalSint32: 1005,
127 OptionalSint64: 1006,
128 OptionalFixed32: 1007,
129 OptionalFixed64: 1008,
130 OptionalSfixed32: 1009,
131 OptionalSfixed64: 1010,
132 OptionalFloat: 1011.5,
133 OptionalDouble: 1012.5,
134 OptionalBool: true,
135 OptionalString: "string",
136 OptionalBytes: []byte("bytes"),
137 OptionalNestedEnum: test3pb.TestAllTypes_BAR,
Damien Neilba23aa52018-12-07 14:38:17 -0800138 }, build(
139 &testpb.TestAllExtensions{},
140 extend(testpb.E_OptionalInt32Extension, scalar.Int32(1001)),
141 extend(testpb.E_OptionalInt64Extension, scalar.Int64(1002)),
142 extend(testpb.E_OptionalUint32Extension, scalar.Uint32(1003)),
143 extend(testpb.E_OptionalUint64Extension, scalar.Uint64(1004)),
144 extend(testpb.E_OptionalSint32Extension, scalar.Int32(1005)),
145 extend(testpb.E_OptionalSint64Extension, scalar.Int64(1006)),
146 extend(testpb.E_OptionalFixed32Extension, scalar.Uint32(1007)),
147 extend(testpb.E_OptionalFixed64Extension, scalar.Uint64(1008)),
148 extend(testpb.E_OptionalSfixed32Extension, scalar.Int32(1009)),
149 extend(testpb.E_OptionalSfixed64Extension, scalar.Int64(1010)),
150 extend(testpb.E_OptionalFloatExtension, scalar.Float32(1011.5)),
151 extend(testpb.E_OptionalDoubleExtension, scalar.Float64(1012.5)),
152 extend(testpb.E_OptionalBoolExtension, scalar.Bool(true)),
153 extend(testpb.E_OptionalStringExtension, scalar.String("string")),
154 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
155 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR.Enum()),
156 )},
157 wire: pack.Message{
158 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
159 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
160 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
161 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
162 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
163 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
164 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
165 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
166 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
167 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
168 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
169 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
170 pack.Tag{13, pack.VarintType}, pack.Bool(true),
171 pack.Tag{14, pack.BytesType}, pack.String("string"),
172 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
173 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
174 }.Marshal(),
175 },
176 {
177 desc: "groups",
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(1017),
181 },
182 }, build(
183 &testpb.TestAllExtensions{},
184 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
185 A: scalar.Int32(1017),
186 }),
187 )},
188 wire: pack.Message{
189 pack.Tag{16, pack.StartGroupType},
190 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
191 pack.Tag{16, pack.EndGroupType},
192 }.Marshal(),
193 },
194 {
195 desc: "groups (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800196 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800197 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
198 A: scalar.Int32(2),
199 },
200 }, build(
201 &testpb.TestAllExtensions{},
202 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
203 A: scalar.Int32(2),
204 }),
205 )},
206 wire: pack.Message{
207 pack.Tag{16, pack.StartGroupType},
208 pack.Tag{17, pack.VarintType}, pack.Varint(1),
209 pack.Tag{16, pack.EndGroupType},
210 pack.Tag{16, pack.StartGroupType},
211 pack.Tag{17, pack.VarintType}, pack.Varint(2),
212 pack.Tag{16, pack.EndGroupType},
213 }.Marshal(),
214 },
215 {
216 desc: "messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800217 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800218 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
219 A: scalar.Int32(42),
220 Corecursive: &testpb.TestAllTypes{
221 OptionalInt32: scalar.Int32(43),
222 },
223 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700224 }, &test3pb.TestAllTypes{
225 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
226 A: 42,
227 Corecursive: &test3pb.TestAllTypes{
228 OptionalInt32: 43,
229 },
230 },
Damien Neilba23aa52018-12-07 14:38:17 -0800231 }, build(
232 &testpb.TestAllExtensions{},
233 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
234 A: scalar.Int32(42),
235 Corecursive: &testpb.TestAllTypes{
236 OptionalInt32: scalar.Int32(43),
237 },
238 }),
239 )},
240 wire: pack.Message{
241 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
242 pack.Tag{1, pack.VarintType}, pack.Varint(42),
243 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
244 pack.Tag{1, pack.VarintType}, pack.Varint(43),
245 }),
246 }),
247 }.Marshal(),
248 },
249 {
250 desc: "messages (split across multiple tags)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800251 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800252 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
253 A: scalar.Int32(42),
254 Corecursive: &testpb.TestAllTypes{
255 OptionalInt32: scalar.Int32(43),
256 },
257 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700258 }, &test3pb.TestAllTypes{
259 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
260 A: 42,
261 Corecursive: &test3pb.TestAllTypes{
262 OptionalInt32: 43,
263 },
264 },
Damien Neilba23aa52018-12-07 14:38:17 -0800265 }, build(
266 &testpb.TestAllExtensions{},
267 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
268 A: scalar.Int32(42),
269 Corecursive: &testpb.TestAllTypes{
270 OptionalInt32: scalar.Int32(43),
271 },
272 }),
273 )},
274 wire: pack.Message{
275 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
276 pack.Tag{1, pack.VarintType}, pack.Varint(42),
277 }),
278 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
279 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
280 pack.Tag{1, pack.VarintType}, pack.Varint(43),
281 }),
282 }),
283 }.Marshal(),
284 },
285 {
286 desc: "messages (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800287 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800288 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
289 A: scalar.Int32(2),
290 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700291 }, &test3pb.TestAllTypes{
292 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
293 A: 2,
294 },
Damien Neilba23aa52018-12-07 14:38:17 -0800295 }, build(
296 &testpb.TestAllExtensions{},
297 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
298 A: scalar.Int32(2),
299 }),
300 )},
301 wire: pack.Message{
302 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
303 pack.Tag{1, pack.VarintType}, pack.Varint(1),
304 }),
305 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
306 pack.Tag{1, pack.VarintType}, pack.Varint(2),
307 }),
308 }.Marshal(),
309 },
310 {
311 desc: "basic repeated types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800312 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800313 RepeatedInt32: []int32{1001, 2001},
314 RepeatedInt64: []int64{1002, 2002},
315 RepeatedUint32: []uint32{1003, 2003},
316 RepeatedUint64: []uint64{1004, 2004},
317 RepeatedSint32: []int32{1005, 2005},
318 RepeatedSint64: []int64{1006, 2006},
319 RepeatedFixed32: []uint32{1007, 2007},
320 RepeatedFixed64: []uint64{1008, 2008},
321 RepeatedSfixed32: []int32{1009, 2009},
322 RepeatedSfixed64: []int64{1010, 2010},
323 RepeatedFloat: []float32{1011.5, 2011.5},
324 RepeatedDouble: []float64{1012.5, 2012.5},
325 RepeatedBool: []bool{true, false},
326 RepeatedString: []string{"foo", "bar"},
327 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
328 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
329 testpb.TestAllTypes_FOO,
330 testpb.TestAllTypes_BAR,
331 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700332 }, &test3pb.TestAllTypes{
333 RepeatedInt32: []int32{1001, 2001},
334 RepeatedInt64: []int64{1002, 2002},
335 RepeatedUint32: []uint32{1003, 2003},
336 RepeatedUint64: []uint64{1004, 2004},
337 RepeatedSint32: []int32{1005, 2005},
338 RepeatedSint64: []int64{1006, 2006},
339 RepeatedFixed32: []uint32{1007, 2007},
340 RepeatedFixed64: []uint64{1008, 2008},
341 RepeatedSfixed32: []int32{1009, 2009},
342 RepeatedSfixed64: []int64{1010, 2010},
343 RepeatedFloat: []float32{1011.5, 2011.5},
344 RepeatedDouble: []float64{1012.5, 2012.5},
345 RepeatedBool: []bool{true, false},
346 RepeatedString: []string{"foo", "bar"},
347 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
348 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
349 test3pb.TestAllTypes_FOO,
350 test3pb.TestAllTypes_BAR,
351 },
Damien Neilba23aa52018-12-07 14:38:17 -0800352 }, build(
353 &testpb.TestAllExtensions{},
354 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
355 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
356 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
357 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
358 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
359 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
360 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
361 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
362 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
363 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
364 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
365 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
366 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
367 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
368 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
369 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
370 testpb.TestAllTypes_FOO,
371 testpb.TestAllTypes_BAR,
372 }),
373 )},
374 wire: pack.Message{
375 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
376 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
377 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
378 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
379 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
380 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
381 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
382 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
383 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
384 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
385 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
386 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
387 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
388 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
389 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
390 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
391 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
392 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
393 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
394 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
395 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
396 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
397 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
398 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
399 pack.Tag{43, pack.VarintType}, pack.Bool(true),
400 pack.Tag{43, pack.VarintType}, pack.Bool(false),
401 pack.Tag{44, pack.BytesType}, pack.String("foo"),
402 pack.Tag{44, pack.BytesType}, pack.String("bar"),
403 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
404 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
405 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
406 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
407 }.Marshal(),
408 },
409 {
410 desc: "basic repeated types (packed encoding)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800411 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800412 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: []testpb.TestAllTypes_NestedEnum{
426 testpb.TestAllTypes_FOO,
427 testpb.TestAllTypes_BAR,
428 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700429 }, &test3pb.TestAllTypes{
430 RepeatedInt32: []int32{1001, 2001},
431 RepeatedInt64: []int64{1002, 2002},
432 RepeatedUint32: []uint32{1003, 2003},
433 RepeatedUint64: []uint64{1004, 2004},
434 RepeatedSint32: []int32{1005, 2005},
435 RepeatedSint64: []int64{1006, 2006},
436 RepeatedFixed32: []uint32{1007, 2007},
437 RepeatedFixed64: []uint64{1008, 2008},
438 RepeatedSfixed32: []int32{1009, 2009},
439 RepeatedSfixed64: []int64{1010, 2010},
440 RepeatedFloat: []float32{1011.5, 2011.5},
441 RepeatedDouble: []float64{1012.5, 2012.5},
442 RepeatedBool: []bool{true, false},
443 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
444 test3pb.TestAllTypes_FOO,
445 test3pb.TestAllTypes_BAR,
446 },
Damien Neilba23aa52018-12-07 14:38:17 -0800447 }, build(
448 &testpb.TestAllExtensions{},
449 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
450 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
451 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
452 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
453 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
454 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
455 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
456 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
457 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
458 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
459 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
460 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
461 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
462 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
463 testpb.TestAllTypes_FOO,
464 testpb.TestAllTypes_BAR,
465 }),
466 )},
467 wire: pack.Message{
468 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
469 pack.Varint(1001), pack.Varint(2001),
470 },
471 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
472 pack.Varint(1002), pack.Varint(2002),
473 },
474 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
475 pack.Uvarint(1003), pack.Uvarint(2003),
476 },
477 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
478 pack.Uvarint(1004), pack.Uvarint(2004),
479 },
480 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
481 pack.Svarint(1005), pack.Svarint(2005),
482 },
483 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
484 pack.Svarint(1006), pack.Svarint(2006),
485 },
486 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
487 pack.Uint32(1007), pack.Uint32(2007),
488 },
489 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
490 pack.Uint64(1008), pack.Uint64(2008),
491 },
492 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
493 pack.Int32(1009), pack.Int32(2009),
494 },
495 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
496 pack.Int64(1010), pack.Int64(2010),
497 },
498 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
499 pack.Float32(1011.5), pack.Float32(2011.5),
500 },
501 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
502 pack.Float64(1012.5), pack.Float64(2012.5),
503 },
504 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
505 pack.Bool(true), pack.Bool(false),
506 },
507 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
508 pack.Varint(int(testpb.TestAllTypes_FOO)),
509 pack.Varint(int(testpb.TestAllTypes_BAR)),
510 },
511 }.Marshal(),
512 },
513 {
514 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800515 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800516 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
517 {A: scalar.Int32(1)},
518 {A: scalar.Int32(2)},
519 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700520 }, &test3pb.TestAllTypes{
521 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
522 {A: 1},
523 {A: 2},
524 },
Damien Neilba23aa52018-12-07 14:38:17 -0800525 }, build(
526 &testpb.TestAllExtensions{},
527 extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
528 {A: scalar.Int32(1)},
529 {A: scalar.Int32(2)},
530 }),
531 )},
532 wire: pack.Message{
533 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
534 pack.Tag{1, pack.VarintType}, pack.Varint(1),
535 }),
536 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
537 pack.Tag{1, pack.VarintType}, pack.Varint(2),
538 }),
539 }.Marshal(),
540 },
541 {
542 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800543 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800544 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
545 {A: scalar.Int32(1017)},
546 {A: scalar.Int32(2017)},
547 },
548 }, build(
549 &testpb.TestAllExtensions{},
550 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
551 {A: scalar.Int32(1017)},
552 {A: scalar.Int32(2017)},
553 }),
554 )},
555 wire: pack.Message{
556 pack.Tag{46, pack.StartGroupType},
557 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
558 pack.Tag{46, pack.EndGroupType},
559 pack.Tag{46, pack.StartGroupType},
560 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
561 pack.Tag{46, pack.EndGroupType},
562 }.Marshal(),
563 },
564 {
565 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800566 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800567 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
568 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
569 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
570 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
571 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
572 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
573 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
574 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
575 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
576 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
577 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
578 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
579 MapBoolBool: map[bool]bool{true: false, false: true},
580 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
581 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
582 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
583 "71.1.key": {A: scalar.Int32(1171)},
584 "71.2.key": {A: scalar.Int32(2171)},
585 },
586 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
587 "73.1.key": testpb.TestAllTypes_FOO,
588 "73.2.key": testpb.TestAllTypes_BAR,
589 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700590 }, &test3pb.TestAllTypes{
591 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
592 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
593 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
594 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
595 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
596 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
597 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
598 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
599 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
600 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
601 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
602 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
603 MapBoolBool: map[bool]bool{true: false, false: true},
604 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
605 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
606 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
607 "71.1.key": {A: 1171},
608 "71.2.key": {A: 2171},
609 },
610 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
611 "73.1.key": test3pb.TestAllTypes_FOO,
612 "73.2.key": test3pb.TestAllTypes_BAR,
613 },
Damien Neilba23aa52018-12-07 14:38:17 -0800614 }},
615 wire: pack.Message{
616 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
617 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
618 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
619 }),
620 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
621 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
622 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
623 }),
624 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
625 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
626 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
627 }),
628 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
629 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
630 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
631 }),
632 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
633 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
634 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
635 }),
636 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
637 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
638 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
639 }),
640 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
641 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
642 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
643 }),
644 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
645 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
646 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
647 }),
648 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
649 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
650 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
651 }),
652 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
653 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
654 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
655 }),
656 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
657 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
658 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
659 }),
660 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
661 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
662 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
663 }),
664 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
665 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
666 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
667 }),
668 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
669 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
670 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
671 }),
672 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
673 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
674 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
675 }),
676 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
677 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
678 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
679 }),
680 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
681 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
682 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
683 }),
684 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
685 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
686 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
687 }),
688 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
689 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
690 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
691 }),
692 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
693 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
694 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
695 }),
696 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
697 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
698 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
699 }),
700 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
701 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
702 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
703 }),
704 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
705 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
706 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
707 }),
708 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
709 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
710 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
711 }),
712 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
713 pack.Tag{1, pack.VarintType}, pack.Bool(true),
714 pack.Tag{2, pack.VarintType}, pack.Bool(false),
715 }),
716 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
717 pack.Tag{1, pack.VarintType}, pack.Bool(false),
718 pack.Tag{2, pack.VarintType}, pack.Bool(true),
719 }),
720 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
721 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
722 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
723 }),
724 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
725 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
726 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
727 }),
728 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
729 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
730 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
731 }),
732 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
733 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
734 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
735 }),
736 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
737 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
738 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
739 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
740 }),
741 }),
742 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
743 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
744 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
745 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
746 }),
747 }),
748 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
749 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
750 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
751 }),
752 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
753 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
754 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
755 }),
756 }.Marshal(),
757 },
758 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700759 desc: "oneof (uint32)",
760 decodeTo: []proto.Message{
761 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
762 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
763 },
764 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800765 },
766 {
767 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700768 decodeTo: []proto.Message{
769 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
770 &testpb.TestAllTypes_NestedMessage{A: scalar.Int32(1112)},
771 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
772 &test3pb.TestAllTypes_NestedMessage{A: 1112},
773 }},
774 },
Damien Neilba23aa52018-12-07 14:38:17 -0800775 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
776 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
777 })}.Marshal(),
778 },
779 {
780 desc: "oneof (overridden message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700781 decodeTo: []proto.Message{
782 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
783 &testpb.TestAllTypes_NestedMessage{
784 Corecursive: &testpb.TestAllTypes{
785 OptionalInt32: scalar.Int32(43),
786 },
Damien Neilba23aa52018-12-07 14:38:17 -0800787 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700788 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
789 &test3pb.TestAllTypes_NestedMessage{
790 Corecursive: &test3pb.TestAllTypes{
791 OptionalInt32: 43,
792 },
793 },
794 }}},
Damien Neilba23aa52018-12-07 14:38:17 -0800795 wire: pack.Message{
796 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
797 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
798 }),
799 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
800 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
801 pack.Tag{1, pack.VarintType}, pack.Varint(43),
802 }),
803 }),
804 }.Marshal(),
805 },
806 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700807 desc: "oneof (string)",
808 decodeTo: []proto.Message{
809 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
810 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
811 },
812 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800813 },
814 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700815 desc: "oneof (bytes)",
816 decodeTo: []proto.Message{
817 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
818 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
819 },
820 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800821 },
822 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700823 desc: "oneof (bool)",
824 decodeTo: []proto.Message{
825 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
826 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
827 },
828 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800829 },
830 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700831 desc: "oneof (uint64)",
832 decodeTo: []proto.Message{
833 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
834 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
835 },
836 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800837 },
838 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700839 desc: "oneof (float)",
840 decodeTo: []proto.Message{
841 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
842 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
843 },
844 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800845 },
846 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700847 desc: "oneof (double)",
848 decodeTo: []proto.Message{
849 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
850 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
851 },
852 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800853 },
854 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700855 desc: "oneof (enum)",
856 decodeTo: []proto.Message{
857 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
858 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
859 },
860 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800861 },
862 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700863 desc: "oneof (overridden value)",
864 decodeTo: []proto.Message{
865 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
866 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
867 },
Damien Neilba23aa52018-12-07 14:38:17 -0800868 wire: pack.Message{
869 pack.Tag{111, pack.VarintType}, pack.Varint(1),
870 pack.Tag{116, pack.VarintType}, pack.Varint(2),
871 }.Marshal(),
872 },
873 // TODO: More unknown field tests for ordering, repeated fields, etc.
874 //
875 // It is currently impossible to produce results that the v1 Equal
876 // considers equivalent to those of the v1 decoder. Figure out if
877 // that's a problem or not.
878 {
879 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -0800880 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -0800881 &testpb.TestAllTypes{},
882 unknown(100000, pack.Message{
883 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
884 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -0700885 ), build(
886 &test3pb.TestAllTypes{},
887 unknown(100000, pack.Message{
888 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
889 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -0800890 )},
891 wire: pack.Message{
892 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
893 }.Marshal(),
894 },
895 {
896 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -0800897 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -0800898 &testpb.TestAllTypes{},
899 unknown(1, pack.Message{
900 pack.Tag{1, pack.BytesType}, pack.String("string"),
901 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -0700902 ), build(
903 &test3pb.TestAllTypes{},
904 unknown(1, pack.Message{
905 pack.Tag{1, pack.BytesType}, pack.String("string"),
906 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -0800907 )},
908 wire: pack.Message{
909 pack.Tag{1, pack.BytesType}, pack.String("string"),
910 }.Marshal(),
911 },
912 {
913 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -0800914 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -0800915 &testpb.TestAllTypes{
916 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -0700917 }, &test3pb.TestAllTypes{
918 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -0800919 },
920 },
921 wire: pack.Message{
922 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
923 pack.Tag{1, pack.VarintType}, pack.Varint(1),
924 pack.Tag{2, pack.BytesType}, pack.String("string"),
925 }),
926 }.Marshal(),
927 },
Damien Neil96c229a2019-04-03 12:17:24 -0700928 {
929 desc: "required field unset",
930 partial: true,
931 decodeTo: []proto.Message{&testpb.TestRequired{}},
932 },
933 {
934 desc: "required field set",
935 decodeTo: []proto.Message{&testpb.TestRequired{
936 RequiredField: scalar.Int32(1),
937 }},
938 wire: pack.Message{
939 pack.Tag{1, pack.VarintType}, pack.Varint(1),
940 }.Marshal(),
941 },
942 {
943 desc: "required field in optional message unset",
944 partial: true,
945 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
946 OptionalMessage: &testpb.TestRequired{},
947 }},
948 wire: pack.Message{
949 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
950 }.Marshal(),
951 },
952 {
953 desc: "required field in optional message set",
954 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
955 OptionalMessage: &testpb.TestRequired{
956 RequiredField: scalar.Int32(1),
957 },
958 }},
959 wire: pack.Message{
960 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
961 pack.Tag{1, pack.VarintType}, pack.Varint(1),
962 }),
963 }.Marshal(),
964 },
Damien Neil4686e232019-04-05 13:31:40 -0700965 {
966 desc: "required field in optional message set (split across multiple tags)",
967 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
968 OptionalMessage: &testpb.TestRequired{
969 RequiredField: scalar.Int32(1),
970 },
971 }},
972 wire: pack.Message{
973 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
974 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
975 pack.Tag{1, pack.VarintType}, pack.Varint(1),
976 }),
977 }.Marshal(),
978 },
Damien Neil96c229a2019-04-03 12:17:24 -0700979 {
980 desc: "required field in repeated message unset",
981 partial: true,
982 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
983 RepeatedMessage: []*testpb.TestRequired{
984 {RequiredField: scalar.Int32(1)},
985 {},
986 },
987 }},
988 wire: pack.Message{
989 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
990 pack.Tag{1, pack.VarintType}, pack.Varint(1),
991 }),
992 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
993 }.Marshal(),
994 },
995 {
996 desc: "required field in repeated message set",
997 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
998 RepeatedMessage: []*testpb.TestRequired{
999 {RequiredField: scalar.Int32(1)},
1000 {RequiredField: scalar.Int32(2)},
1001 },
1002 }},
1003 wire: pack.Message{
1004 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1005 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1006 }),
1007 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1008 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1009 }),
1010 }.Marshal(),
1011 },
1012 {
1013 desc: "required field in map message unset",
1014 partial: true,
1015 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1016 MapMessage: map[int32]*testpb.TestRequired{
1017 1: {RequiredField: scalar.Int32(1)},
1018 2: {},
1019 },
1020 }},
1021 wire: pack.Message{
1022 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1023 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1024 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1025 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1026 }),
1027 }),
1028 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1029 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1030 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1031 }),
1032 }.Marshal(),
1033 },
1034 {
1035 desc: "required field in map message set",
1036 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1037 MapMessage: map[int32]*testpb.TestRequired{
1038 1: {RequiredField: scalar.Int32(1)},
1039 2: {RequiredField: scalar.Int32(2)},
1040 },
1041 }},
1042 wire: pack.Message{
1043 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1044 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1045 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1046 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1047 }),
1048 }),
1049 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1050 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1051 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1052 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1053 }),
1054 }),
1055 }.Marshal(),
1056 },
1057 {
1058 desc: "required field in optional group unset",
1059 partial: true,
1060 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1061 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1062 }},
1063 wire: pack.Message{
1064 pack.Tag{1, pack.StartGroupType},
1065 pack.Tag{1, pack.EndGroupType},
1066 }.Marshal(),
1067 },
1068 {
1069 desc: "required field in optional group set",
1070 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1071 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
1072 A: scalar.Int32(1),
1073 },
1074 }},
1075 wire: pack.Message{
1076 pack.Tag{1, pack.StartGroupType},
1077 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1078 pack.Tag{1, pack.EndGroupType},
1079 }.Marshal(),
1080 },
1081 {
1082 desc: "required field in repeated group unset",
1083 partial: true,
1084 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1085 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1086 {A: scalar.Int32(1)},
1087 {},
1088 },
1089 }},
1090 wire: pack.Message{
1091 pack.Tag{3, pack.StartGroupType},
1092 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1093 pack.Tag{3, pack.EndGroupType},
1094 pack.Tag{3, pack.StartGroupType},
1095 pack.Tag{3, pack.EndGroupType},
1096 }.Marshal(),
1097 },
1098 {
1099 desc: "required field in repeated group set",
1100 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1101 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1102 {A: scalar.Int32(1)},
1103 {A: scalar.Int32(2)},
1104 },
1105 }},
1106 wire: pack.Message{
1107 pack.Tag{3, pack.StartGroupType},
1108 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1109 pack.Tag{3, pack.EndGroupType},
1110 pack.Tag{3, pack.StartGroupType},
1111 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1112 pack.Tag{3, pack.EndGroupType},
1113 }.Marshal(),
1114 },
1115 {
1116 desc: "required field in extension message unset",
1117 partial: true,
1118 invalidExtensions: true,
1119 decodeTo: []proto.Message{build(
1120 &testpb.TestAllExtensions{},
1121 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1122 )},
1123 wire: pack.Message{
1124 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1125 }.Marshal(),
1126 },
1127 {
1128 desc: "required field in extension message set",
1129 decodeTo: []proto.Message{build(
1130 &testpb.TestAllExtensions{},
1131 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
1132 RequiredField: scalar.Int32(1),
1133 }),
1134 )},
1135 wire: pack.Message{
1136 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1137 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1138 }),
1139 }.Marshal(),
1140 },
1141 {
1142 desc: "required field in repeated extension message unset",
1143 partial: true,
1144 invalidExtensions: true,
1145 decodeTo: []proto.Message{build(
1146 &testpb.TestAllExtensions{},
1147 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1148 {RequiredField: scalar.Int32(1)},
1149 {},
1150 }),
1151 )},
1152 wire: pack.Message{
1153 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1154 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1155 }),
1156 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1157 }.Marshal(),
1158 },
1159 {
1160 desc: "required field in repeated extension message set",
1161 decodeTo: []proto.Message{build(
1162 &testpb.TestAllExtensions{},
1163 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1164 {RequiredField: scalar.Int32(1)},
1165 {RequiredField: scalar.Int32(2)},
1166 }),
1167 )},
1168 wire: pack.Message{
1169 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1170 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1171 }),
1172 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1173 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1174 }),
1175 }.Marshal(),
1176 },
Damien Neilba23aa52018-12-07 14:38:17 -08001177}
1178
Damien Neilbc310b52019-04-11 11:46:55 -07001179var invalidUTF8TestProtos = []testProto{
1180 {
1181 desc: "invalid UTF-8 in optional string field",
1182 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1183 OptionalString: "abc\xff",
1184 }},
1185 wire: pack.Message{
1186 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1187 }.Marshal(),
1188 },
1189 {
1190 desc: "invalid UTF-8 in repeated string field",
1191 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1192 RepeatedString: []string{"foo", "abc\xff"},
1193 }},
1194 wire: pack.Message{
1195 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1196 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1197 }.Marshal(),
1198 },
1199 {
1200 desc: "invalid UTF-8 in nested message",
1201 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1202 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1203 Corecursive: &test3pb.TestAllTypes{
1204 OptionalString: "abc\xff",
1205 },
1206 },
1207 }},
1208 wire: pack.Message{
1209 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1210 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1211 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1212 }),
1213 }),
1214 }.Marshal(),
1215 },
1216 {
1217 desc: "invalid UTF-8 in map key",
1218 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1219 MapStringString: map[string]string{"key\xff": "val"},
1220 }},
1221 wire: pack.Message{
1222 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1223 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1224 pack.Tag{2, pack.BytesType}, pack.String("val"),
1225 }),
1226 }.Marshal(),
1227 },
1228 {
1229 desc: "invalid UTF-8 in map value",
1230 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1231 MapStringString: map[string]string{"key": "val\xff"},
1232 }},
1233 wire: pack.Message{
1234 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1235 pack.Tag{1, pack.BytesType}, pack.String("key"),
1236 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1237 }),
1238 }.Marshal(),
1239 },
1240}
1241
Damien Neil4be2fb42018-12-17 11:16:16 -08001242func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001243 for _, opt := range opts {
1244 opt(m)
1245 }
1246 return m
1247}
1248
Damien Neil4be2fb42018-12-17 11:16:16 -08001249type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001250
1251func unknown(num pref.FieldNumber, raw pref.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001252 return func(m proto.Message) {
Damien Neilba23aa52018-12-07 14:38:17 -08001253 m.ProtoReflect().UnknownFields().Set(num, raw)
1254 }
1255}
1256
1257func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001258 return func(m proto.Message) {
Damien Neilba23aa52018-12-07 14:38:17 -08001259 if err := protoV1.SetExtension(m.(protoV1.Message), desc, value); err != nil {
1260 panic(err)
1261 }
1262 }
1263}
Damien Neil61e93c72019-03-27 09:23:20 -07001264
1265func marshalText(m proto.Message) string {
1266 b, _ := textpb.Marshal(m)
1267 return string(b)
1268}
Damien Neilbc310b52019-04-11 11:46:55 -07001269
1270func isErrInvalidUTF8(err error) bool {
1271 nerr, ok := err.(errors.NonFatalErrors)
1272 if !ok || len(nerr) == 0 {
1273 return false
1274 }
1275 for _, err := range nerr {
1276 if e, ok := err.(interface{ InvalidUTF8() bool }); ok && e.InvalidUTF8() {
1277 continue
1278 }
1279 return false
1280 }
1281 return true
1282}