blob: 03f337a00c85f2796d2f8351e2726fdec1ddda50 [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 {
512 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800513 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800514 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
515 {A: scalar.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700516 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800517 {A: scalar.Int32(2)},
518 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700519 }, &test3pb.TestAllTypes{
520 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
521 {A: 1},
Damien Neilc37adef2019-04-01 13:49:56 -0700522 nil,
Damien Neil3b46ade2019-03-26 13:55:02 -0700523 {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)},
Damien Neilc37adef2019-04-01 13:49:56 -0700529 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800530 {A: scalar.Int32(2)},
531 }),
532 )},
533 wire: pack.Message{
534 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
535 pack.Tag{1, pack.VarintType}, pack.Varint(1),
536 }),
Damien Neilc37adef2019-04-01 13:49:56 -0700537 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
Damien Neilba23aa52018-12-07 14:38:17 -0800538 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
539 pack.Tag{1, pack.VarintType}, pack.Varint(2),
540 }),
541 }.Marshal(),
542 },
543 {
544 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800545 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800546 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
547 {A: scalar.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700548 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800549 {A: scalar.Int32(2017)},
550 },
551 }, build(
552 &testpb.TestAllExtensions{},
553 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
554 {A: scalar.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700555 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800556 {A: scalar.Int32(2017)},
557 }),
558 )},
559 wire: pack.Message{
560 pack.Tag{46, pack.StartGroupType},
561 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
562 pack.Tag{46, pack.EndGroupType},
563 pack.Tag{46, pack.StartGroupType},
Damien Neilc37adef2019-04-01 13:49:56 -0700564 pack.Tag{46, pack.EndGroupType},
565 pack.Tag{46, pack.StartGroupType},
Damien Neilba23aa52018-12-07 14:38:17 -0800566 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
567 pack.Tag{46, pack.EndGroupType},
568 }.Marshal(),
569 },
570 {
571 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800572 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800573 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
574 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
575 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
576 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
577 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
578 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
579 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
580 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
581 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
582 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
583 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
584 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
585 MapBoolBool: map[bool]bool{true: false, false: true},
586 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
587 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
588 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
589 "71.1.key": {A: scalar.Int32(1171)},
590 "71.2.key": {A: scalar.Int32(2171)},
591 },
592 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
593 "73.1.key": testpb.TestAllTypes_FOO,
594 "73.2.key": testpb.TestAllTypes_BAR,
595 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700596 }, &test3pb.TestAllTypes{
597 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
598 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
599 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
600 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
601 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
602 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
603 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
604 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
605 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
606 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
607 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
608 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
609 MapBoolBool: map[bool]bool{true: false, false: true},
610 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
611 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
612 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
613 "71.1.key": {A: 1171},
614 "71.2.key": {A: 2171},
615 },
616 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
617 "73.1.key": test3pb.TestAllTypes_FOO,
618 "73.2.key": test3pb.TestAllTypes_BAR,
619 },
Damien Neilba23aa52018-12-07 14:38:17 -0800620 }},
621 wire: pack.Message{
622 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
623 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
624 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
625 }),
626 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
627 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
628 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
629 }),
630 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
631 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
632 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
633 }),
634 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
635 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
636 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
637 }),
638 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
639 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
640 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
641 }),
642 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
643 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
644 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
645 }),
646 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
647 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
648 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
649 }),
650 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
651 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
652 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
653 }),
654 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
655 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
656 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
657 }),
658 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
659 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
660 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
661 }),
662 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
663 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
664 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
665 }),
666 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
667 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
668 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
669 }),
670 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
671 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
672 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
673 }),
674 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
675 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
676 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
677 }),
678 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
679 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
680 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
681 }),
682 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
683 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
684 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
685 }),
686 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
687 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
688 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
689 }),
690 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
691 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
692 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
693 }),
694 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
695 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
696 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
697 }),
698 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
699 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
700 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
701 }),
702 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
703 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
704 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
705 }),
706 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
707 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
708 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
709 }),
710 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
711 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
712 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
713 }),
714 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
715 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
716 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
717 }),
718 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
719 pack.Tag{1, pack.VarintType}, pack.Bool(true),
720 pack.Tag{2, pack.VarintType}, pack.Bool(false),
721 }),
722 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
723 pack.Tag{1, pack.VarintType}, pack.Bool(false),
724 pack.Tag{2, pack.VarintType}, pack.Bool(true),
725 }),
726 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
727 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
728 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
729 }),
730 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
731 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
732 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
733 }),
734 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
735 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
736 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
737 }),
738 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
739 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
740 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
741 }),
742 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
743 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
744 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
745 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
746 }),
747 }),
748 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
749 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
750 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
751 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
752 }),
753 }),
754 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
755 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
756 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
757 }),
758 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
759 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
760 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
761 }),
762 }.Marshal(),
763 },
764 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700765 desc: "oneof (uint32)",
766 decodeTo: []proto.Message{
767 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
768 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
769 },
770 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800771 },
772 {
773 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700774 decodeTo: []proto.Message{
775 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
776 &testpb.TestAllTypes_NestedMessage{A: scalar.Int32(1112)},
777 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
778 &test3pb.TestAllTypes_NestedMessage{A: 1112},
779 }},
780 },
Damien Neilba23aa52018-12-07 14:38:17 -0800781 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
782 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
783 })}.Marshal(),
784 },
785 {
Damien Neilc37adef2019-04-01 13:49:56 -0700786 desc: "oneof (empty message)",
787 decodeTo: []proto.Message{
788 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
789 &testpb.TestAllTypes_NestedMessage{},
790 }},
791 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
792 &test3pb.TestAllTypes_NestedMessage{},
793 }},
794 },
795 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
796 },
797 {
Damien Neilba23aa52018-12-07 14:38:17 -0800798 desc: "oneof (overridden message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700799 decodeTo: []proto.Message{
800 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
801 &testpb.TestAllTypes_NestedMessage{
802 Corecursive: &testpb.TestAllTypes{
803 OptionalInt32: scalar.Int32(43),
804 },
Damien Neilba23aa52018-12-07 14:38:17 -0800805 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700806 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
807 &test3pb.TestAllTypes_NestedMessage{
808 Corecursive: &test3pb.TestAllTypes{
809 OptionalInt32: 43,
810 },
811 },
812 }}},
Damien Neilba23aa52018-12-07 14:38:17 -0800813 wire: pack.Message{
814 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
815 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
816 }),
817 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
818 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
819 pack.Tag{1, pack.VarintType}, pack.Varint(43),
820 }),
821 }),
822 }.Marshal(),
823 },
824 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700825 desc: "oneof (string)",
826 decodeTo: []proto.Message{
827 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
828 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
829 },
830 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800831 },
832 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700833 desc: "oneof (bytes)",
834 decodeTo: []proto.Message{
835 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
836 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
837 },
838 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800839 },
840 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700841 desc: "oneof (bool)",
842 decodeTo: []proto.Message{
843 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
844 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
845 },
846 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800847 },
848 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700849 desc: "oneof (uint64)",
850 decodeTo: []proto.Message{
851 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
852 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
853 },
854 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800855 },
856 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700857 desc: "oneof (float)",
858 decodeTo: []proto.Message{
859 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
860 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
861 },
862 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800863 },
864 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700865 desc: "oneof (double)",
866 decodeTo: []proto.Message{
867 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
868 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
869 },
870 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800871 },
872 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700873 desc: "oneof (enum)",
874 decodeTo: []proto.Message{
875 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
876 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
877 },
878 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800879 },
880 {
Damien Neilc37adef2019-04-01 13:49:56 -0700881 desc: "oneof (zero)",
882 decodeTo: []proto.Message{
883 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}},
884 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}},
885 },
886 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
887 },
888 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700889 desc: "oneof (overridden value)",
890 decodeTo: []proto.Message{
891 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
892 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
893 },
Damien Neilba23aa52018-12-07 14:38:17 -0800894 wire: pack.Message{
895 pack.Tag{111, pack.VarintType}, pack.Varint(1),
896 pack.Tag{116, pack.VarintType}, pack.Varint(2),
897 }.Marshal(),
898 },
899 // TODO: More unknown field tests for ordering, repeated fields, etc.
900 //
901 // It is currently impossible to produce results that the v1 Equal
902 // considers equivalent to those of the v1 decoder. Figure out if
903 // that's a problem or not.
904 {
905 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -0800906 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -0800907 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -0700908 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -0800909 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
910 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -0700911 ), build(
912 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -0700913 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -0700914 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
915 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -0800916 )},
917 wire: pack.Message{
918 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
919 }.Marshal(),
920 },
921 {
922 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -0800923 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -0800924 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -0700925 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -0800926 pack.Tag{1, pack.BytesType}, pack.String("string"),
927 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -0700928 ), build(
929 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -0700930 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -0700931 pack.Tag{1, pack.BytesType}, pack.String("string"),
932 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -0800933 )},
934 wire: pack.Message{
935 pack.Tag{1, pack.BytesType}, pack.String("string"),
936 }.Marshal(),
937 },
938 {
939 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -0800940 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -0800941 &testpb.TestAllTypes{
942 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -0700943 }, &test3pb.TestAllTypes{
944 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -0800945 },
946 },
947 wire: pack.Message{
948 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
949 pack.Tag{1, pack.VarintType}, pack.Varint(1),
950 pack.Tag{2, pack.BytesType}, pack.String("string"),
951 }),
952 }.Marshal(),
953 },
Damien Neil96c229a2019-04-03 12:17:24 -0700954 {
Damien Neil3d0706a2019-07-09 11:40:49 -0700955 desc: "required field in nil message unset",
956 partial: true,
957 decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
958 },
959 {
Damien Neil96c229a2019-04-03 12:17:24 -0700960 desc: "required field unset",
961 partial: true,
962 decodeTo: []proto.Message{&testpb.TestRequired{}},
963 },
964 {
965 desc: "required field set",
966 decodeTo: []proto.Message{&testpb.TestRequired{
967 RequiredField: scalar.Int32(1),
968 }},
969 wire: pack.Message{
970 pack.Tag{1, pack.VarintType}, pack.Varint(1),
971 }.Marshal(),
972 },
973 {
974 desc: "required field in optional message unset",
975 partial: true,
976 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
977 OptionalMessage: &testpb.TestRequired{},
978 }},
979 wire: pack.Message{
980 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
981 }.Marshal(),
982 },
983 {
984 desc: "required field in optional message set",
985 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
986 OptionalMessage: &testpb.TestRequired{
987 RequiredField: scalar.Int32(1),
988 },
989 }},
990 wire: pack.Message{
991 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
992 pack.Tag{1, pack.VarintType}, pack.Varint(1),
993 }),
994 }.Marshal(),
995 },
Damien Neil4686e232019-04-05 13:31:40 -0700996 {
997 desc: "required field in optional message set (split across multiple tags)",
998 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
999 OptionalMessage: &testpb.TestRequired{
1000 RequiredField: scalar.Int32(1),
1001 },
1002 }},
1003 wire: pack.Message{
1004 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1005 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1006 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1007 }),
1008 }.Marshal(),
1009 },
Damien Neil96c229a2019-04-03 12:17:24 -07001010 {
1011 desc: "required field in repeated message unset",
1012 partial: true,
1013 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1014 RepeatedMessage: []*testpb.TestRequired{
1015 {RequiredField: scalar.Int32(1)},
1016 {},
1017 },
1018 }},
1019 wire: pack.Message{
1020 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1021 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1022 }),
1023 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1024 }.Marshal(),
1025 },
1026 {
1027 desc: "required field in repeated message set",
1028 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1029 RepeatedMessage: []*testpb.TestRequired{
1030 {RequiredField: scalar.Int32(1)},
1031 {RequiredField: scalar.Int32(2)},
1032 },
1033 }},
1034 wire: pack.Message{
1035 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1036 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1037 }),
1038 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1039 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1040 }),
1041 }.Marshal(),
1042 },
1043 {
1044 desc: "required field in map message unset",
1045 partial: true,
1046 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1047 MapMessage: map[int32]*testpb.TestRequired{
1048 1: {RequiredField: scalar.Int32(1)},
1049 2: {},
1050 },
1051 }},
1052 wire: pack.Message{
1053 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1054 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1055 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1056 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1057 }),
1058 }),
1059 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1060 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1061 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1062 }),
1063 }.Marshal(),
1064 },
1065 {
1066 desc: "required field in map message set",
1067 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1068 MapMessage: map[int32]*testpb.TestRequired{
1069 1: {RequiredField: scalar.Int32(1)},
1070 2: {RequiredField: scalar.Int32(2)},
1071 },
1072 }},
1073 wire: pack.Message{
1074 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1075 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1076 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1077 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1078 }),
1079 }),
1080 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1081 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1082 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1083 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1084 }),
1085 }),
1086 }.Marshal(),
1087 },
1088 {
1089 desc: "required field in optional group unset",
1090 partial: true,
1091 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1092 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1093 }},
1094 wire: pack.Message{
1095 pack.Tag{1, pack.StartGroupType},
1096 pack.Tag{1, pack.EndGroupType},
1097 }.Marshal(),
1098 },
1099 {
1100 desc: "required field in optional group set",
1101 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1102 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
1103 A: scalar.Int32(1),
1104 },
1105 }},
1106 wire: pack.Message{
1107 pack.Tag{1, pack.StartGroupType},
1108 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1109 pack.Tag{1, pack.EndGroupType},
1110 }.Marshal(),
1111 },
1112 {
1113 desc: "required field in repeated group unset",
1114 partial: true,
1115 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1116 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1117 {A: scalar.Int32(1)},
1118 {},
1119 },
1120 }},
1121 wire: pack.Message{
1122 pack.Tag{3, pack.StartGroupType},
1123 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1124 pack.Tag{3, pack.EndGroupType},
1125 pack.Tag{3, pack.StartGroupType},
1126 pack.Tag{3, pack.EndGroupType},
1127 }.Marshal(),
1128 },
1129 {
1130 desc: "required field in repeated group set",
1131 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1132 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1133 {A: scalar.Int32(1)},
1134 {A: scalar.Int32(2)},
1135 },
1136 }},
1137 wire: pack.Message{
1138 pack.Tag{3, pack.StartGroupType},
1139 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1140 pack.Tag{3, pack.EndGroupType},
1141 pack.Tag{3, pack.StartGroupType},
1142 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1143 pack.Tag{3, pack.EndGroupType},
1144 }.Marshal(),
1145 },
1146 {
Damien Neil5322bdb2019-04-09 15:57:05 -07001147 desc: "required field in oneof message unset",
1148 partial: true,
1149 decodeTo: []proto.Message{
1150 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1151 &testpb.TestRequired{},
1152 }},
1153 },
1154 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1155 },
1156 {
1157 desc: "required field in oneof message set",
1158 decodeTo: []proto.Message{
1159 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1160 &testpb.TestRequired{
1161 RequiredField: scalar.Int32(1),
1162 },
1163 }},
1164 },
1165 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
1166 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1167 })}.Marshal(),
1168 },
1169 {
Damien Neil96c229a2019-04-03 12:17:24 -07001170 desc: "required field in extension message unset",
1171 partial: true,
1172 invalidExtensions: true,
1173 decodeTo: []proto.Message{build(
1174 &testpb.TestAllExtensions{},
1175 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1176 )},
1177 wire: pack.Message{
1178 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1179 }.Marshal(),
1180 },
1181 {
1182 desc: "required field in extension message set",
1183 decodeTo: []proto.Message{build(
1184 &testpb.TestAllExtensions{},
1185 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
1186 RequiredField: scalar.Int32(1),
1187 }),
1188 )},
1189 wire: pack.Message{
1190 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1191 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1192 }),
1193 }.Marshal(),
1194 },
1195 {
1196 desc: "required field in repeated extension message unset",
1197 partial: true,
1198 invalidExtensions: true,
1199 decodeTo: []proto.Message{build(
1200 &testpb.TestAllExtensions{},
1201 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1202 {RequiredField: scalar.Int32(1)},
1203 {},
1204 }),
1205 )},
1206 wire: pack.Message{
1207 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1208 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1209 }),
1210 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1211 }.Marshal(),
1212 },
1213 {
1214 desc: "required field in repeated extension message set",
1215 decodeTo: []proto.Message{build(
1216 &testpb.TestAllExtensions{},
1217 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1218 {RequiredField: scalar.Int32(1)},
1219 {RequiredField: scalar.Int32(2)},
1220 }),
1221 )},
1222 wire: pack.Message{
1223 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1224 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1225 }),
1226 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1227 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1228 }),
1229 }.Marshal(),
1230 },
Damien Neilc37adef2019-04-01 13:49:56 -07001231 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001232 desc: "nil messages",
1233 decodeTo: []proto.Message{
1234 (*testpb.TestAllTypes)(nil),
1235 (*test3pb.TestAllTypes)(nil),
1236 (*testpb.TestAllExtensions)(nil),
1237 },
1238 },
1239 {
Damien Neilc37adef2019-04-01 13:49:56 -07001240 desc: "legacy",
1241 partial: true,
1242 decodeTo: []proto.Message{
1243 &legacypb.Legacy{
1244 F1: &legacy1pb.Message{
1245 OptionalInt32: scalar.Int32(1),
1246 OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
1247 OptionalChildMessage: &legacy1pb.Message_ChildMessage{
1248 F1: scalar.String("x"),
1249 },
1250 Optionalgroup: &legacy1pb.Message_OptionalGroup{
1251 F1: scalar.String("x"),
1252 },
1253 RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
1254 {F1: scalar.String("x")},
1255 },
1256 Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
1257 {F1: scalar.String("x")},
1258 },
1259 MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
1260 true: {F1: scalar.String("x")},
1261 },
1262 OneofUnion: &legacy1pb.Message_OneofChildMessage{
1263 &legacy1pb.Message_ChildMessage{
1264 F1: scalar.String("x"),
1265 },
1266 },
1267 },
1268 },
1269 },
1270 wire: pack.Message{
1271 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1272 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1273 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1274 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1275 pack.Tag{1, pack.BytesType}, pack.String("x"),
1276 }),
1277 pack.Tag{120, pack.StartGroupType},
1278 pack.Tag{1, pack.BytesType}, pack.String("x"),
1279 pack.Tag{120, pack.EndGroupType},
1280 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1281 pack.Tag{1, pack.BytesType}, pack.String("x"),
1282 }),
1283 pack.Tag{520, pack.StartGroupType},
1284 pack.Tag{1, pack.BytesType}, pack.String("x"),
1285 pack.Tag{520, pack.EndGroupType},
1286 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1287 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1288 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1289 pack.Tag{1, pack.BytesType}, pack.String("x"),
1290 }),
1291 }),
1292 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1293 pack.Tag{1, pack.BytesType}, pack.String("x"),
1294 }),
1295 }),
1296 }.Marshal(),
1297 },
Damien Neilba23aa52018-12-07 14:38:17 -08001298}
1299
Damien Neilbc310b52019-04-11 11:46:55 -07001300var invalidUTF8TestProtos = []testProto{
1301 {
1302 desc: "invalid UTF-8 in optional string field",
1303 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1304 OptionalString: "abc\xff",
1305 }},
1306 wire: pack.Message{
1307 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1308 }.Marshal(),
1309 },
1310 {
1311 desc: "invalid UTF-8 in repeated string field",
1312 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1313 RepeatedString: []string{"foo", "abc\xff"},
1314 }},
1315 wire: pack.Message{
1316 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1317 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1318 }.Marshal(),
1319 },
1320 {
1321 desc: "invalid UTF-8 in nested message",
1322 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1323 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1324 Corecursive: &test3pb.TestAllTypes{
1325 OptionalString: "abc\xff",
1326 },
1327 },
1328 }},
1329 wire: pack.Message{
1330 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1331 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1332 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1333 }),
1334 }),
1335 }.Marshal(),
1336 },
1337 {
Damien Neilc37adef2019-04-01 13:49:56 -07001338 desc: "invalid UTF-8 in oneof field",
1339 decodeTo: []proto.Message{
1340 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
1341 },
1342 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1343 },
1344 {
Damien Neilbc310b52019-04-11 11:46:55 -07001345 desc: "invalid UTF-8 in map key",
1346 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1347 MapStringString: map[string]string{"key\xff": "val"},
1348 }},
1349 wire: pack.Message{
1350 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1351 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1352 pack.Tag{2, pack.BytesType}, pack.String("val"),
1353 }),
1354 }.Marshal(),
1355 },
1356 {
1357 desc: "invalid UTF-8 in map value",
1358 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1359 MapStringString: map[string]string{"key": "val\xff"},
1360 }},
1361 wire: pack.Message{
1362 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1363 pack.Tag{1, pack.BytesType}, pack.String("key"),
1364 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1365 }),
1366 }.Marshal(),
1367 },
1368}
1369
Damien Neil4be2fb42018-12-17 11:16:16 -08001370func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001371 for _, opt := range opts {
1372 opt(m)
1373 }
1374 return m
1375}
1376
Damien Neil4be2fb42018-12-17 11:16:16 -08001377type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001378
Joe Tsai378c1322019-04-25 23:48:08 -07001379func unknown(raw pref.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001380 return func(m proto.Message) {
Joe Tsai378c1322019-04-25 23:48:08 -07001381 m.ProtoReflect().SetUnknown(raw)
Damien Neile6f060f2019-04-23 17:11:02 -07001382 }
1383}
1384
Damien Neilba23aa52018-12-07 14:38:17 -08001385func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001386 // TODO: Should ExtensionType.ValueOf accept []T instead of *[]T?
1387 t := reflect.TypeOf(value)
1388 if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
1389 v := reflect.New(t)
1390 v.Elem().Set(reflect.ValueOf(value))
1391 value = v.Interface()
1392 }
1393
Damien Neil4be2fb42018-12-17 11:16:16 -08001394 return func(m proto.Message) {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001395 xt := desc.Type
1396 m.ProtoReflect().Set(xt, xt.ValueOf(value))
Damien Neilba23aa52018-12-07 14:38:17 -08001397 }
1398}
Damien Neil61e93c72019-03-27 09:23:20 -07001399
1400func marshalText(m proto.Message) string {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001401 b, _ := prototext.MarshalOptions{Indent: "\t", AllowPartial: true}.Marshal(m)
Damien Neil61e93c72019-03-27 09:23:20 -07001402 return string(b)
1403}