blob: 3b919aa9097c881602a881905bbf20cb9180f382 [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 {
955 desc: "required field unset",
956 partial: true,
957 decodeTo: []proto.Message{&testpb.TestRequired{}},
958 },
959 {
960 desc: "required field set",
961 decodeTo: []proto.Message{&testpb.TestRequired{
962 RequiredField: scalar.Int32(1),
963 }},
964 wire: pack.Message{
965 pack.Tag{1, pack.VarintType}, pack.Varint(1),
966 }.Marshal(),
967 },
968 {
969 desc: "required field in optional message unset",
970 partial: true,
971 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
972 OptionalMessage: &testpb.TestRequired{},
973 }},
974 wire: pack.Message{
975 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
976 }.Marshal(),
977 },
978 {
979 desc: "required field in optional message set",
980 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
981 OptionalMessage: &testpb.TestRequired{
982 RequiredField: scalar.Int32(1),
983 },
984 }},
985 wire: pack.Message{
986 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
987 pack.Tag{1, pack.VarintType}, pack.Varint(1),
988 }),
989 }.Marshal(),
990 },
Damien Neil4686e232019-04-05 13:31:40 -0700991 {
992 desc: "required field in optional message set (split across multiple tags)",
993 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
994 OptionalMessage: &testpb.TestRequired{
995 RequiredField: scalar.Int32(1),
996 },
997 }},
998 wire: pack.Message{
999 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1000 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1001 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1002 }),
1003 }.Marshal(),
1004 },
Damien Neil96c229a2019-04-03 12:17:24 -07001005 {
1006 desc: "required field in repeated message unset",
1007 partial: true,
1008 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1009 RepeatedMessage: []*testpb.TestRequired{
1010 {RequiredField: scalar.Int32(1)},
1011 {},
1012 },
1013 }},
1014 wire: pack.Message{
1015 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1016 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1017 }),
1018 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1019 }.Marshal(),
1020 },
1021 {
1022 desc: "required field in repeated message set",
1023 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1024 RepeatedMessage: []*testpb.TestRequired{
1025 {RequiredField: scalar.Int32(1)},
1026 {RequiredField: scalar.Int32(2)},
1027 },
1028 }},
1029 wire: pack.Message{
1030 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1031 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1032 }),
1033 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1034 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1035 }),
1036 }.Marshal(),
1037 },
1038 {
1039 desc: "required field in map message unset",
1040 partial: true,
1041 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1042 MapMessage: map[int32]*testpb.TestRequired{
1043 1: {RequiredField: scalar.Int32(1)},
1044 2: {},
1045 },
1046 }},
1047 wire: pack.Message{
1048 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1049 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1050 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1051 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1052 }),
1053 }),
1054 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1055 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1056 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1057 }),
1058 }.Marshal(),
1059 },
1060 {
1061 desc: "required field in map message set",
1062 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1063 MapMessage: map[int32]*testpb.TestRequired{
1064 1: {RequiredField: scalar.Int32(1)},
1065 2: {RequiredField: scalar.Int32(2)},
1066 },
1067 }},
1068 wire: pack.Message{
1069 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1070 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1071 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1072 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1073 }),
1074 }),
1075 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1076 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1077 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1078 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1079 }),
1080 }),
1081 }.Marshal(),
1082 },
1083 {
1084 desc: "required field in optional group unset",
1085 partial: true,
1086 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1087 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1088 }},
1089 wire: pack.Message{
1090 pack.Tag{1, pack.StartGroupType},
1091 pack.Tag{1, pack.EndGroupType},
1092 }.Marshal(),
1093 },
1094 {
1095 desc: "required field in optional group set",
1096 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1097 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
1098 A: scalar.Int32(1),
1099 },
1100 }},
1101 wire: pack.Message{
1102 pack.Tag{1, pack.StartGroupType},
1103 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1104 pack.Tag{1, pack.EndGroupType},
1105 }.Marshal(),
1106 },
1107 {
1108 desc: "required field in repeated group unset",
1109 partial: true,
1110 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1111 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1112 {A: scalar.Int32(1)},
1113 {},
1114 },
1115 }},
1116 wire: pack.Message{
1117 pack.Tag{3, pack.StartGroupType},
1118 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1119 pack.Tag{3, pack.EndGroupType},
1120 pack.Tag{3, pack.StartGroupType},
1121 pack.Tag{3, pack.EndGroupType},
1122 }.Marshal(),
1123 },
1124 {
1125 desc: "required field in repeated group set",
1126 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1127 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1128 {A: scalar.Int32(1)},
1129 {A: scalar.Int32(2)},
1130 },
1131 }},
1132 wire: pack.Message{
1133 pack.Tag{3, pack.StartGroupType},
1134 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1135 pack.Tag{3, pack.EndGroupType},
1136 pack.Tag{3, pack.StartGroupType},
1137 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1138 pack.Tag{3, pack.EndGroupType},
1139 }.Marshal(),
1140 },
1141 {
Damien Neil5322bdb2019-04-09 15:57:05 -07001142 desc: "required field in oneof message unset",
1143 partial: true,
1144 decodeTo: []proto.Message{
1145 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1146 &testpb.TestRequired{},
1147 }},
1148 },
1149 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1150 },
1151 {
1152 desc: "required field in oneof message set",
1153 decodeTo: []proto.Message{
1154 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1155 &testpb.TestRequired{
1156 RequiredField: scalar.Int32(1),
1157 },
1158 }},
1159 },
1160 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
1161 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1162 })}.Marshal(),
1163 },
1164 {
Damien Neil96c229a2019-04-03 12:17:24 -07001165 desc: "required field in extension message unset",
1166 partial: true,
1167 invalidExtensions: true,
1168 decodeTo: []proto.Message{build(
1169 &testpb.TestAllExtensions{},
1170 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1171 )},
1172 wire: pack.Message{
1173 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1174 }.Marshal(),
1175 },
1176 {
1177 desc: "required field in extension message set",
1178 decodeTo: []proto.Message{build(
1179 &testpb.TestAllExtensions{},
1180 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
1181 RequiredField: scalar.Int32(1),
1182 }),
1183 )},
1184 wire: pack.Message{
1185 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1186 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1187 }),
1188 }.Marshal(),
1189 },
1190 {
1191 desc: "required field in repeated extension message unset",
1192 partial: true,
1193 invalidExtensions: true,
1194 decodeTo: []proto.Message{build(
1195 &testpb.TestAllExtensions{},
1196 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1197 {RequiredField: scalar.Int32(1)},
1198 {},
1199 }),
1200 )},
1201 wire: pack.Message{
1202 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1203 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1204 }),
1205 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1206 }.Marshal(),
1207 },
1208 {
1209 desc: "required field in repeated extension message set",
1210 decodeTo: []proto.Message{build(
1211 &testpb.TestAllExtensions{},
1212 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1213 {RequiredField: scalar.Int32(1)},
1214 {RequiredField: scalar.Int32(2)},
1215 }),
1216 )},
1217 wire: pack.Message{
1218 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1219 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1220 }),
1221 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1222 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1223 }),
1224 }.Marshal(),
1225 },
Damien Neilc37adef2019-04-01 13:49:56 -07001226 {
1227 desc: "legacy",
1228 partial: true,
1229 decodeTo: []proto.Message{
1230 &legacypb.Legacy{
1231 F1: &legacy1pb.Message{
1232 OptionalInt32: scalar.Int32(1),
1233 OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
1234 OptionalChildMessage: &legacy1pb.Message_ChildMessage{
1235 F1: scalar.String("x"),
1236 },
1237 Optionalgroup: &legacy1pb.Message_OptionalGroup{
1238 F1: scalar.String("x"),
1239 },
1240 RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
1241 {F1: scalar.String("x")},
1242 },
1243 Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
1244 {F1: scalar.String("x")},
1245 },
1246 MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
1247 true: {F1: scalar.String("x")},
1248 },
1249 OneofUnion: &legacy1pb.Message_OneofChildMessage{
1250 &legacy1pb.Message_ChildMessage{
1251 F1: scalar.String("x"),
1252 },
1253 },
1254 },
1255 },
1256 },
1257 wire: pack.Message{
1258 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1259 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1260 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1261 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1262 pack.Tag{1, pack.BytesType}, pack.String("x"),
1263 }),
1264 pack.Tag{120, pack.StartGroupType},
1265 pack.Tag{1, pack.BytesType}, pack.String("x"),
1266 pack.Tag{120, pack.EndGroupType},
1267 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1268 pack.Tag{1, pack.BytesType}, pack.String("x"),
1269 }),
1270 pack.Tag{520, pack.StartGroupType},
1271 pack.Tag{1, pack.BytesType}, pack.String("x"),
1272 pack.Tag{520, pack.EndGroupType},
1273 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1274 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1275 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1276 pack.Tag{1, pack.BytesType}, pack.String("x"),
1277 }),
1278 }),
1279 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1280 pack.Tag{1, pack.BytesType}, pack.String("x"),
1281 }),
1282 }),
1283 }.Marshal(),
1284 },
Damien Neilba23aa52018-12-07 14:38:17 -08001285}
1286
Damien Neilbc310b52019-04-11 11:46:55 -07001287var invalidUTF8TestProtos = []testProto{
1288 {
1289 desc: "invalid UTF-8 in optional string field",
1290 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1291 OptionalString: "abc\xff",
1292 }},
1293 wire: pack.Message{
1294 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1295 }.Marshal(),
1296 },
1297 {
1298 desc: "invalid UTF-8 in repeated string field",
1299 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1300 RepeatedString: []string{"foo", "abc\xff"},
1301 }},
1302 wire: pack.Message{
1303 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1304 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1305 }.Marshal(),
1306 },
1307 {
1308 desc: "invalid UTF-8 in nested message",
1309 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1310 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1311 Corecursive: &test3pb.TestAllTypes{
1312 OptionalString: "abc\xff",
1313 },
1314 },
1315 }},
1316 wire: pack.Message{
1317 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1318 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1319 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1320 }),
1321 }),
1322 }.Marshal(),
1323 },
1324 {
Damien Neilc37adef2019-04-01 13:49:56 -07001325 desc: "invalid UTF-8 in oneof field",
1326 decodeTo: []proto.Message{
1327 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
1328 },
1329 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1330 },
1331 {
Damien Neilbc310b52019-04-11 11:46:55 -07001332 desc: "invalid UTF-8 in map key",
1333 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1334 MapStringString: map[string]string{"key\xff": "val"},
1335 }},
1336 wire: pack.Message{
1337 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1338 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1339 pack.Tag{2, pack.BytesType}, pack.String("val"),
1340 }),
1341 }.Marshal(),
1342 },
1343 {
1344 desc: "invalid UTF-8 in map value",
1345 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1346 MapStringString: map[string]string{"key": "val\xff"},
1347 }},
1348 wire: pack.Message{
1349 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1350 pack.Tag{1, pack.BytesType}, pack.String("key"),
1351 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1352 }),
1353 }.Marshal(),
1354 },
1355}
1356
Damien Neil4be2fb42018-12-17 11:16:16 -08001357func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001358 for _, opt := range opts {
1359 opt(m)
1360 }
1361 return m
1362}
1363
Damien Neil4be2fb42018-12-17 11:16:16 -08001364type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001365
Joe Tsai378c1322019-04-25 23:48:08 -07001366func unknown(raw pref.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001367 return func(m proto.Message) {
Joe Tsai378c1322019-04-25 23:48:08 -07001368 m.ProtoReflect().SetUnknown(raw)
Damien Neile6f060f2019-04-23 17:11:02 -07001369 }
1370}
1371
Damien Neilba23aa52018-12-07 14:38:17 -08001372func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001373 // TODO: Should ExtensionType.ValueOf accept []T instead of *[]T?
1374 t := reflect.TypeOf(value)
1375 if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
1376 v := reflect.New(t)
1377 v.Elem().Set(reflect.ValueOf(value))
1378 value = v.Interface()
1379 }
1380
Damien Neil4be2fb42018-12-17 11:16:16 -08001381 return func(m proto.Message) {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001382 xt := desc.Type
1383 m.ProtoReflect().Set(xt, xt.ValueOf(value))
Damien Neilba23aa52018-12-07 14:38:17 -08001384 }
1385}
Damien Neil61e93c72019-03-27 09:23:20 -07001386
1387func marshalText(m proto.Message) string {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001388 b, _ := prototext.MarshalOptions{Indent: "\t", AllowPartial: true}.Marshal(m)
Damien Neil61e93c72019-03-27 09:23:20 -07001389 return string(b)
1390}