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