blob: d93723e4800503209eff679dbe4d48cf72e9a179 [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"
15 "google.golang.org/protobuf/internal/errors"
16 "google.golang.org/protobuf/internal/scalar"
17 "google.golang.org/protobuf/proto"
18 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsai19058432019-02-27 21:46:29 -080019
Damien Neilc37adef2019-04-01 13:49:56 -070020 legacypb "google.golang.org/protobuf/internal/testprotos/legacy"
21 legacy1pb "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v0.0.0-20160225-2fc053c5"
Damien Neile89e6242019-05-13 23:55:40 -070022 testpb "google.golang.org/protobuf/internal/testprotos/test"
23 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Damien Neilba23aa52018-12-07 14:38:17 -080024)
25
26type testProto struct {
Damien Neil96c229a2019-04-03 12:17:24 -070027 desc string
28 decodeTo []proto.Message
29 wire []byte
30 partial bool
31 invalidExtensions bool
Damien Neilba23aa52018-12-07 14:38:17 -080032}
33
34func TestDecode(t *testing.T) {
35 for _, test := range testProtos {
36 for _, want := range test.decodeTo {
37 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070038 opts := proto.UnmarshalOptions{
39 AllowPartial: test.partial,
40 }
Damien Neilba23aa52018-12-07 14:38:17 -080041 wire := append(([]byte)(nil), test.wire...)
Damien Neil4be2fb42018-12-17 11:16:16 -080042 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
Damien Neil96c229a2019-04-03 12:17:24 -070043 if err := opts.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070044 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080045 return
46 }
47
48 // Aliasing check: Modifying the original wire bytes shouldn't
49 // affect the unmarshaled message.
50 for i := range wire {
51 wire[i] = 0
52 }
53
Damien Neil96c229a2019-04-03 12:17:24 -070054 if test.invalidExtensions {
55 // Equal doesn't work on messages containing invalid extension data.
56 return
57 }
Joe Tsaidb38ddd2019-05-07 15:14:40 -070058 if !proto.Equal(got, want) {
Damien Neil61e93c72019-03-27 09:23:20 -070059 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080060 }
61 })
62 }
63 }
64}
65
Damien Neil96c229a2019-04-03 12:17:24 -070066func TestDecodeRequiredFieldChecks(t *testing.T) {
67 for _, test := range testProtos {
68 if !test.partial {
69 continue
70 }
71 if test.invalidExtensions {
72 // Missing required fields in extensions just end up in the unknown fields.
73 continue
74 }
75 for _, m := range test.decodeTo {
76 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
77 got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
78 if err := proto.Unmarshal(test.wire, got); err == nil {
79 t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got))
80 }
81 })
82 }
83 }
84}
85
Damien Neilbc310b52019-04-11 11:46:55 -070086func TestDecodeInvalidUTF8(t *testing.T) {
87 for _, test := range invalidUTF8TestProtos {
88 for _, want := range test.decodeTo {
89 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
90 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
91 err := proto.Unmarshal(test.wire, got)
92 if !isErrInvalidUTF8(err) {
93 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
94 }
95 if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) {
96 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
97 }
98 })
99 }
100 }
101}
102
Damien Neilba23aa52018-12-07 14:38:17 -0800103var testProtos = []testProto{
104 {
105 desc: "basic scalar types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800106 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800107 OptionalInt32: scalar.Int32(1001),
108 OptionalInt64: scalar.Int64(1002),
109 OptionalUint32: scalar.Uint32(1003),
110 OptionalUint64: scalar.Uint64(1004),
111 OptionalSint32: scalar.Int32(1005),
112 OptionalSint64: scalar.Int64(1006),
113 OptionalFixed32: scalar.Uint32(1007),
114 OptionalFixed64: scalar.Uint64(1008),
115 OptionalSfixed32: scalar.Int32(1009),
116 OptionalSfixed64: scalar.Int64(1010),
117 OptionalFloat: scalar.Float32(1011.5),
118 OptionalDouble: scalar.Float64(1012.5),
119 OptionalBool: scalar.Bool(true),
120 OptionalString: scalar.String("string"),
121 OptionalBytes: []byte("bytes"),
122 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
Damien Neil3b46ade2019-03-26 13:55:02 -0700123 }, &test3pb.TestAllTypes{
124 OptionalInt32: 1001,
125 OptionalInt64: 1002,
126 OptionalUint32: 1003,
127 OptionalUint64: 1004,
128 OptionalSint32: 1005,
129 OptionalSint64: 1006,
130 OptionalFixed32: 1007,
131 OptionalFixed64: 1008,
132 OptionalSfixed32: 1009,
133 OptionalSfixed64: 1010,
134 OptionalFloat: 1011.5,
135 OptionalDouble: 1012.5,
136 OptionalBool: true,
137 OptionalString: "string",
138 OptionalBytes: []byte("bytes"),
139 OptionalNestedEnum: test3pb.TestAllTypes_BAR,
Damien Neilba23aa52018-12-07 14:38:17 -0800140 }, build(
141 &testpb.TestAllExtensions{},
142 extend(testpb.E_OptionalInt32Extension, scalar.Int32(1001)),
143 extend(testpb.E_OptionalInt64Extension, scalar.Int64(1002)),
144 extend(testpb.E_OptionalUint32Extension, scalar.Uint32(1003)),
145 extend(testpb.E_OptionalUint64Extension, scalar.Uint64(1004)),
146 extend(testpb.E_OptionalSint32Extension, scalar.Int32(1005)),
147 extend(testpb.E_OptionalSint64Extension, scalar.Int64(1006)),
148 extend(testpb.E_OptionalFixed32Extension, scalar.Uint32(1007)),
149 extend(testpb.E_OptionalFixed64Extension, scalar.Uint64(1008)),
150 extend(testpb.E_OptionalSfixed32Extension, scalar.Int32(1009)),
151 extend(testpb.E_OptionalSfixed64Extension, scalar.Int64(1010)),
152 extend(testpb.E_OptionalFloatExtension, scalar.Float32(1011.5)),
153 extend(testpb.E_OptionalDoubleExtension, scalar.Float64(1012.5)),
154 extend(testpb.E_OptionalBoolExtension, scalar.Bool(true)),
155 extend(testpb.E_OptionalStringExtension, scalar.String("string")),
156 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
157 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR.Enum()),
158 )},
159 wire: pack.Message{
160 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
161 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
162 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
163 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
164 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
165 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
166 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
167 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
168 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
169 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
170 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
171 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
172 pack.Tag{13, pack.VarintType}, pack.Bool(true),
173 pack.Tag{14, pack.BytesType}, pack.String("string"),
174 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
175 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
176 }.Marshal(),
177 },
178 {
179 desc: "groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800180 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800181 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
182 A: scalar.Int32(1017),
183 },
184 }, build(
185 &testpb.TestAllExtensions{},
186 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
187 A: scalar.Int32(1017),
188 }),
189 )},
190 wire: pack.Message{
191 pack.Tag{16, pack.StartGroupType},
192 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
193 pack.Tag{16, pack.EndGroupType},
194 }.Marshal(),
195 },
196 {
197 desc: "groups (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800198 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800199 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
200 A: scalar.Int32(2),
201 },
202 }, build(
203 &testpb.TestAllExtensions{},
204 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
205 A: scalar.Int32(2),
206 }),
207 )},
208 wire: pack.Message{
209 pack.Tag{16, pack.StartGroupType},
210 pack.Tag{17, pack.VarintType}, pack.Varint(1),
211 pack.Tag{16, pack.EndGroupType},
212 pack.Tag{16, pack.StartGroupType},
213 pack.Tag{17, pack.VarintType}, pack.Varint(2),
214 pack.Tag{16, pack.EndGroupType},
215 }.Marshal(),
216 },
217 {
218 desc: "messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800219 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800220 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
221 A: scalar.Int32(42),
222 Corecursive: &testpb.TestAllTypes{
223 OptionalInt32: scalar.Int32(43),
224 },
225 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700226 }, &test3pb.TestAllTypes{
227 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
228 A: 42,
229 Corecursive: &test3pb.TestAllTypes{
230 OptionalInt32: 43,
231 },
232 },
Damien Neilba23aa52018-12-07 14:38:17 -0800233 }, build(
234 &testpb.TestAllExtensions{},
235 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
236 A: scalar.Int32(42),
237 Corecursive: &testpb.TestAllTypes{
238 OptionalInt32: scalar.Int32(43),
239 },
240 }),
241 )},
242 wire: pack.Message{
243 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
244 pack.Tag{1, pack.VarintType}, pack.Varint(42),
245 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
246 pack.Tag{1, pack.VarintType}, pack.Varint(43),
247 }),
248 }),
249 }.Marshal(),
250 },
251 {
252 desc: "messages (split across multiple tags)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800253 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800254 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
255 A: scalar.Int32(42),
256 Corecursive: &testpb.TestAllTypes{
257 OptionalInt32: scalar.Int32(43),
258 },
259 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700260 }, &test3pb.TestAllTypes{
261 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
262 A: 42,
263 Corecursive: &test3pb.TestAllTypes{
264 OptionalInt32: 43,
265 },
266 },
Damien Neilba23aa52018-12-07 14:38:17 -0800267 }, build(
268 &testpb.TestAllExtensions{},
269 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
270 A: scalar.Int32(42),
271 Corecursive: &testpb.TestAllTypes{
272 OptionalInt32: scalar.Int32(43),
273 },
274 }),
275 )},
276 wire: pack.Message{
277 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
278 pack.Tag{1, pack.VarintType}, pack.Varint(42),
279 }),
280 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
281 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
282 pack.Tag{1, pack.VarintType}, pack.Varint(43),
283 }),
284 }),
285 }.Marshal(),
286 },
287 {
288 desc: "messages (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800289 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800290 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
291 A: scalar.Int32(2),
292 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700293 }, &test3pb.TestAllTypes{
294 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
295 A: 2,
296 },
Damien Neilba23aa52018-12-07 14:38:17 -0800297 }, build(
298 &testpb.TestAllExtensions{},
299 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
300 A: scalar.Int32(2),
301 }),
302 )},
303 wire: pack.Message{
304 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
305 pack.Tag{1, pack.VarintType}, pack.Varint(1),
306 }),
307 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
308 pack.Tag{1, pack.VarintType}, pack.Varint(2),
309 }),
310 }.Marshal(),
311 },
312 {
313 desc: "basic repeated types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800314 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800315 RepeatedInt32: []int32{1001, 2001},
316 RepeatedInt64: []int64{1002, 2002},
317 RepeatedUint32: []uint32{1003, 2003},
318 RepeatedUint64: []uint64{1004, 2004},
319 RepeatedSint32: []int32{1005, 2005},
320 RepeatedSint64: []int64{1006, 2006},
321 RepeatedFixed32: []uint32{1007, 2007},
322 RepeatedFixed64: []uint64{1008, 2008},
323 RepeatedSfixed32: []int32{1009, 2009},
324 RepeatedSfixed64: []int64{1010, 2010},
325 RepeatedFloat: []float32{1011.5, 2011.5},
326 RepeatedDouble: []float64{1012.5, 2012.5},
327 RepeatedBool: []bool{true, false},
328 RepeatedString: []string{"foo", "bar"},
329 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
330 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
331 testpb.TestAllTypes_FOO,
332 testpb.TestAllTypes_BAR,
333 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700334 }, &test3pb.TestAllTypes{
335 RepeatedInt32: []int32{1001, 2001},
336 RepeatedInt64: []int64{1002, 2002},
337 RepeatedUint32: []uint32{1003, 2003},
338 RepeatedUint64: []uint64{1004, 2004},
339 RepeatedSint32: []int32{1005, 2005},
340 RepeatedSint64: []int64{1006, 2006},
341 RepeatedFixed32: []uint32{1007, 2007},
342 RepeatedFixed64: []uint64{1008, 2008},
343 RepeatedSfixed32: []int32{1009, 2009},
344 RepeatedSfixed64: []int64{1010, 2010},
345 RepeatedFloat: []float32{1011.5, 2011.5},
346 RepeatedDouble: []float64{1012.5, 2012.5},
347 RepeatedBool: []bool{true, false},
348 RepeatedString: []string{"foo", "bar"},
349 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
350 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
351 test3pb.TestAllTypes_FOO,
352 test3pb.TestAllTypes_BAR,
353 },
Damien Neilba23aa52018-12-07 14:38:17 -0800354 }, build(
355 &testpb.TestAllExtensions{},
356 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
357 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
358 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
359 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
360 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
361 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
362 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
363 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
364 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
365 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
366 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
367 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
368 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
369 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
370 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
371 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
372 testpb.TestAllTypes_FOO,
373 testpb.TestAllTypes_BAR,
374 }),
375 )},
376 wire: pack.Message{
377 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
378 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
379 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
380 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
381 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
382 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
383 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
384 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
385 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
386 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
387 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
388 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
389 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
390 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
391 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
392 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
393 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
394 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
395 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
396 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
397 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
398 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
399 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
400 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
401 pack.Tag{43, pack.VarintType}, pack.Bool(true),
402 pack.Tag{43, pack.VarintType}, pack.Bool(false),
403 pack.Tag{44, pack.BytesType}, pack.String("foo"),
404 pack.Tag{44, pack.BytesType}, pack.String("bar"),
405 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
406 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
407 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
408 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
409 }.Marshal(),
410 },
411 {
412 desc: "basic repeated types (packed encoding)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800413 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800414 RepeatedInt32: []int32{1001, 2001},
415 RepeatedInt64: []int64{1002, 2002},
416 RepeatedUint32: []uint32{1003, 2003},
417 RepeatedUint64: []uint64{1004, 2004},
418 RepeatedSint32: []int32{1005, 2005},
419 RepeatedSint64: []int64{1006, 2006},
420 RepeatedFixed32: []uint32{1007, 2007},
421 RepeatedFixed64: []uint64{1008, 2008},
422 RepeatedSfixed32: []int32{1009, 2009},
423 RepeatedSfixed64: []int64{1010, 2010},
424 RepeatedFloat: []float32{1011.5, 2011.5},
425 RepeatedDouble: []float64{1012.5, 2012.5},
426 RepeatedBool: []bool{true, false},
427 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
428 testpb.TestAllTypes_FOO,
429 testpb.TestAllTypes_BAR,
430 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700431 }, &test3pb.TestAllTypes{
432 RepeatedInt32: []int32{1001, 2001},
433 RepeatedInt64: []int64{1002, 2002},
434 RepeatedUint32: []uint32{1003, 2003},
435 RepeatedUint64: []uint64{1004, 2004},
436 RepeatedSint32: []int32{1005, 2005},
437 RepeatedSint64: []int64{1006, 2006},
438 RepeatedFixed32: []uint32{1007, 2007},
439 RepeatedFixed64: []uint64{1008, 2008},
440 RepeatedSfixed32: []int32{1009, 2009},
441 RepeatedSfixed64: []int64{1010, 2010},
442 RepeatedFloat: []float32{1011.5, 2011.5},
443 RepeatedDouble: []float64{1012.5, 2012.5},
444 RepeatedBool: []bool{true, false},
445 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
446 test3pb.TestAllTypes_FOO,
447 test3pb.TestAllTypes_BAR,
448 },
Damien Neilba23aa52018-12-07 14:38:17 -0800449 }, build(
450 &testpb.TestAllExtensions{},
451 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
452 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
453 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
454 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
455 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
456 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
457 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
458 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
459 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
460 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
461 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
462 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
463 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
464 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
465 testpb.TestAllTypes_FOO,
466 testpb.TestAllTypes_BAR,
467 }),
468 )},
469 wire: pack.Message{
470 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
471 pack.Varint(1001), pack.Varint(2001),
472 },
473 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
474 pack.Varint(1002), pack.Varint(2002),
475 },
476 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
477 pack.Uvarint(1003), pack.Uvarint(2003),
478 },
479 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
480 pack.Uvarint(1004), pack.Uvarint(2004),
481 },
482 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
483 pack.Svarint(1005), pack.Svarint(2005),
484 },
485 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
486 pack.Svarint(1006), pack.Svarint(2006),
487 },
488 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
489 pack.Uint32(1007), pack.Uint32(2007),
490 },
491 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
492 pack.Uint64(1008), pack.Uint64(2008),
493 },
494 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
495 pack.Int32(1009), pack.Int32(2009),
496 },
497 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
498 pack.Int64(1010), pack.Int64(2010),
499 },
500 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
501 pack.Float32(1011.5), pack.Float32(2011.5),
502 },
503 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
504 pack.Float64(1012.5), pack.Float64(2012.5),
505 },
506 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
507 pack.Bool(true), pack.Bool(false),
508 },
509 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
510 pack.Varint(int(testpb.TestAllTypes_FOO)),
511 pack.Varint(int(testpb.TestAllTypes_BAR)),
512 },
513 }.Marshal(),
514 },
515 {
516 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800517 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800518 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
519 {A: scalar.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700520 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800521 {A: scalar.Int32(2)},
522 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700523 }, &test3pb.TestAllTypes{
524 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
525 {A: 1},
Damien Neilc37adef2019-04-01 13:49:56 -0700526 nil,
Damien Neil3b46ade2019-03-26 13:55:02 -0700527 {A: 2},
528 },
Damien Neilba23aa52018-12-07 14:38:17 -0800529 }, build(
530 &testpb.TestAllExtensions{},
531 extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
532 {A: scalar.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700533 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800534 {A: scalar.Int32(2)},
535 }),
536 )},
537 wire: pack.Message{
538 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
539 pack.Tag{1, pack.VarintType}, pack.Varint(1),
540 }),
Damien Neilc37adef2019-04-01 13:49:56 -0700541 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
Damien Neilba23aa52018-12-07 14:38:17 -0800542 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
543 pack.Tag{1, pack.VarintType}, pack.Varint(2),
544 }),
545 }.Marshal(),
546 },
547 {
548 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800549 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800550 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
551 {A: scalar.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700552 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800553 {A: scalar.Int32(2017)},
554 },
555 }, build(
556 &testpb.TestAllExtensions{},
557 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
558 {A: scalar.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700559 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800560 {A: scalar.Int32(2017)},
561 }),
562 )},
563 wire: pack.Message{
564 pack.Tag{46, pack.StartGroupType},
565 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
566 pack.Tag{46, pack.EndGroupType},
567 pack.Tag{46, pack.StartGroupType},
Damien Neilc37adef2019-04-01 13:49:56 -0700568 pack.Tag{46, pack.EndGroupType},
569 pack.Tag{46, pack.StartGroupType},
Damien Neilba23aa52018-12-07 14:38:17 -0800570 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
571 pack.Tag{46, pack.EndGroupType},
572 }.Marshal(),
573 },
574 {
575 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800576 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800577 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
578 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
579 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
580 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
581 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
582 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
583 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
584 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
585 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
586 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
587 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
588 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
589 MapBoolBool: map[bool]bool{true: false, false: true},
590 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
591 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
592 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
593 "71.1.key": {A: scalar.Int32(1171)},
594 "71.2.key": {A: scalar.Int32(2171)},
595 },
596 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
597 "73.1.key": testpb.TestAllTypes_FOO,
598 "73.2.key": testpb.TestAllTypes_BAR,
599 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700600 }, &test3pb.TestAllTypes{
601 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
602 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
603 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
604 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
605 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
606 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
607 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
608 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
609 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
610 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
611 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
612 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
613 MapBoolBool: map[bool]bool{true: false, false: true},
614 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
615 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
616 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
617 "71.1.key": {A: 1171},
618 "71.2.key": {A: 2171},
619 },
620 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
621 "73.1.key": test3pb.TestAllTypes_FOO,
622 "73.2.key": test3pb.TestAllTypes_BAR,
623 },
Damien Neilba23aa52018-12-07 14:38:17 -0800624 }},
625 wire: pack.Message{
626 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
627 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
628 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
629 }),
630 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
631 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
632 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
633 }),
634 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
635 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
636 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
637 }),
638 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
639 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
640 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
641 }),
642 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
643 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
644 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
645 }),
646 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
647 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
648 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
649 }),
650 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
651 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
652 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
653 }),
654 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
655 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
656 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
657 }),
658 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
659 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
660 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
661 }),
662 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
663 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
664 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
665 }),
666 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
667 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
668 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
669 }),
670 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
671 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
672 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
673 }),
674 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
675 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
676 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
677 }),
678 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
679 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
680 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
681 }),
682 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
683 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
684 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
685 }),
686 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
687 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
688 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
689 }),
690 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
691 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
692 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
693 }),
694 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
695 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
696 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
697 }),
698 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
699 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
700 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
701 }),
702 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
703 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
704 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
705 }),
706 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
707 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
708 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
709 }),
710 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
711 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
712 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
713 }),
714 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
715 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
716 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
717 }),
718 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
719 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
720 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
721 }),
722 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
723 pack.Tag{1, pack.VarintType}, pack.Bool(true),
724 pack.Tag{2, pack.VarintType}, pack.Bool(false),
725 }),
726 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
727 pack.Tag{1, pack.VarintType}, pack.Bool(false),
728 pack.Tag{2, pack.VarintType}, pack.Bool(true),
729 }),
730 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
731 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
732 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
733 }),
734 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
735 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
736 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
737 }),
738 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
739 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
740 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
741 }),
742 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
743 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
744 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
745 }),
746 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
747 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
748 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
749 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
750 }),
751 }),
752 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
753 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
754 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
755 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
756 }),
757 }),
758 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
759 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
760 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
761 }),
762 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
763 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
764 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
765 }),
766 }.Marshal(),
767 },
768 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700769 desc: "oneof (uint32)",
770 decodeTo: []proto.Message{
771 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
772 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
773 },
774 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800775 },
776 {
777 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700778 decodeTo: []proto.Message{
779 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
780 &testpb.TestAllTypes_NestedMessage{A: scalar.Int32(1112)},
781 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
782 &test3pb.TestAllTypes_NestedMessage{A: 1112},
783 }},
784 },
Damien Neilba23aa52018-12-07 14:38:17 -0800785 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
786 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
787 })}.Marshal(),
788 },
789 {
Damien Neilc37adef2019-04-01 13:49:56 -0700790 desc: "oneof (empty message)",
791 decodeTo: []proto.Message{
792 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
793 &testpb.TestAllTypes_NestedMessage{},
794 }},
795 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
796 &test3pb.TestAllTypes_NestedMessage{},
797 }},
798 },
799 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
800 },
801 {
Damien Neilba23aa52018-12-07 14:38:17 -0800802 desc: "oneof (overridden message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700803 decodeTo: []proto.Message{
804 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
805 &testpb.TestAllTypes_NestedMessage{
806 Corecursive: &testpb.TestAllTypes{
807 OptionalInt32: scalar.Int32(43),
808 },
Damien Neilba23aa52018-12-07 14:38:17 -0800809 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700810 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
811 &test3pb.TestAllTypes_NestedMessage{
812 Corecursive: &test3pb.TestAllTypes{
813 OptionalInt32: 43,
814 },
815 },
816 }}},
Damien Neilba23aa52018-12-07 14:38:17 -0800817 wire: pack.Message{
818 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
819 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
820 }),
821 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
822 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
823 pack.Tag{1, pack.VarintType}, pack.Varint(43),
824 }),
825 }),
826 }.Marshal(),
827 },
828 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700829 desc: "oneof (string)",
830 decodeTo: []proto.Message{
831 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
832 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
833 },
834 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800835 },
836 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700837 desc: "oneof (bytes)",
838 decodeTo: []proto.Message{
839 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
840 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
841 },
842 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800843 },
844 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700845 desc: "oneof (bool)",
846 decodeTo: []proto.Message{
847 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
848 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
849 },
850 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800851 },
852 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700853 desc: "oneof (uint64)",
854 decodeTo: []proto.Message{
855 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
856 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
857 },
858 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800859 },
860 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700861 desc: "oneof (float)",
862 decodeTo: []proto.Message{
863 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
864 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
865 },
866 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800867 },
868 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700869 desc: "oneof (double)",
870 decodeTo: []proto.Message{
871 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
872 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
873 },
874 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800875 },
876 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700877 desc: "oneof (enum)",
878 decodeTo: []proto.Message{
879 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
880 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
881 },
882 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800883 },
884 {
Damien Neilc37adef2019-04-01 13:49:56 -0700885 desc: "oneof (zero)",
886 decodeTo: []proto.Message{
887 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}},
888 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}},
889 },
890 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
891 },
892 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700893 desc: "oneof (overridden value)",
894 decodeTo: []proto.Message{
895 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
896 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
897 },
Damien Neilba23aa52018-12-07 14:38:17 -0800898 wire: pack.Message{
899 pack.Tag{111, pack.VarintType}, pack.Varint(1),
900 pack.Tag{116, pack.VarintType}, pack.Varint(2),
901 }.Marshal(),
902 },
903 // TODO: More unknown field tests for ordering, repeated fields, etc.
904 //
905 // It is currently impossible to produce results that the v1 Equal
906 // considers equivalent to those of the v1 decoder. Figure out if
907 // that's a problem or not.
908 {
909 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -0800910 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -0800911 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -0700912 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -0800913 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
914 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -0700915 ), build(
916 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -0700917 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -0700918 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
919 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -0800920 )},
921 wire: pack.Message{
922 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
923 }.Marshal(),
924 },
925 {
926 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -0800927 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -0800928 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -0700929 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -0800930 pack.Tag{1, pack.BytesType}, pack.String("string"),
931 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -0700932 ), build(
933 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -0700934 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -0700935 pack.Tag{1, pack.BytesType}, pack.String("string"),
936 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -0800937 )},
938 wire: pack.Message{
939 pack.Tag{1, pack.BytesType}, pack.String("string"),
940 }.Marshal(),
941 },
942 {
943 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -0800944 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -0800945 &testpb.TestAllTypes{
946 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -0700947 }, &test3pb.TestAllTypes{
948 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -0800949 },
950 },
951 wire: pack.Message{
952 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
953 pack.Tag{1, pack.VarintType}, pack.Varint(1),
954 pack.Tag{2, pack.BytesType}, pack.String("string"),
955 }),
956 }.Marshal(),
957 },
Damien Neil96c229a2019-04-03 12:17:24 -0700958 {
959 desc: "required field unset",
960 partial: true,
961 decodeTo: []proto.Message{&testpb.TestRequired{}},
962 },
963 {
964 desc: "required field set",
965 decodeTo: []proto.Message{&testpb.TestRequired{
966 RequiredField: scalar.Int32(1),
967 }},
968 wire: pack.Message{
969 pack.Tag{1, pack.VarintType}, pack.Varint(1),
970 }.Marshal(),
971 },
972 {
973 desc: "required field in optional message unset",
974 partial: true,
975 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
976 OptionalMessage: &testpb.TestRequired{},
977 }},
978 wire: pack.Message{
979 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
980 }.Marshal(),
981 },
982 {
983 desc: "required field in optional message set",
984 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
985 OptionalMessage: &testpb.TestRequired{
986 RequiredField: scalar.Int32(1),
987 },
988 }},
989 wire: pack.Message{
990 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
991 pack.Tag{1, pack.VarintType}, pack.Varint(1),
992 }),
993 }.Marshal(),
994 },
Damien Neil4686e232019-04-05 13:31:40 -0700995 {
996 desc: "required field in optional message set (split across multiple tags)",
997 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
998 OptionalMessage: &testpb.TestRequired{
999 RequiredField: scalar.Int32(1),
1000 },
1001 }},
1002 wire: pack.Message{
1003 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1004 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1005 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1006 }),
1007 }.Marshal(),
1008 },
Damien Neil96c229a2019-04-03 12:17:24 -07001009 {
1010 desc: "required field in repeated message unset",
1011 partial: true,
1012 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1013 RepeatedMessage: []*testpb.TestRequired{
1014 {RequiredField: scalar.Int32(1)},
1015 {},
1016 },
1017 }},
1018 wire: pack.Message{
1019 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1020 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1021 }),
1022 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1023 }.Marshal(),
1024 },
1025 {
1026 desc: "required field in repeated message set",
1027 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1028 RepeatedMessage: []*testpb.TestRequired{
1029 {RequiredField: scalar.Int32(1)},
1030 {RequiredField: scalar.Int32(2)},
1031 },
1032 }},
1033 wire: pack.Message{
1034 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1035 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1036 }),
1037 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1038 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1039 }),
1040 }.Marshal(),
1041 },
1042 {
1043 desc: "required field in map message unset",
1044 partial: true,
1045 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1046 MapMessage: map[int32]*testpb.TestRequired{
1047 1: {RequiredField: scalar.Int32(1)},
1048 2: {},
1049 },
1050 }},
1051 wire: pack.Message{
1052 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1053 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1054 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1055 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1056 }),
1057 }),
1058 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1059 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1060 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1061 }),
1062 }.Marshal(),
1063 },
1064 {
1065 desc: "required field in map message set",
1066 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1067 MapMessage: map[int32]*testpb.TestRequired{
1068 1: {RequiredField: scalar.Int32(1)},
1069 2: {RequiredField: scalar.Int32(2)},
1070 },
1071 }},
1072 wire: pack.Message{
1073 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1074 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1075 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1076 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1077 }),
1078 }),
1079 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1080 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1081 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1082 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1083 }),
1084 }),
1085 }.Marshal(),
1086 },
1087 {
1088 desc: "required field in optional group unset",
1089 partial: true,
1090 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1091 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1092 }},
1093 wire: pack.Message{
1094 pack.Tag{1, pack.StartGroupType},
1095 pack.Tag{1, pack.EndGroupType},
1096 }.Marshal(),
1097 },
1098 {
1099 desc: "required field in optional group set",
1100 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1101 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
1102 A: scalar.Int32(1),
1103 },
1104 }},
1105 wire: pack.Message{
1106 pack.Tag{1, pack.StartGroupType},
1107 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1108 pack.Tag{1, pack.EndGroupType},
1109 }.Marshal(),
1110 },
1111 {
1112 desc: "required field in repeated group unset",
1113 partial: true,
1114 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1115 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1116 {A: scalar.Int32(1)},
1117 {},
1118 },
1119 }},
1120 wire: pack.Message{
1121 pack.Tag{3, pack.StartGroupType},
1122 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1123 pack.Tag{3, pack.EndGroupType},
1124 pack.Tag{3, pack.StartGroupType},
1125 pack.Tag{3, pack.EndGroupType},
1126 }.Marshal(),
1127 },
1128 {
1129 desc: "required field in repeated group set",
1130 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1131 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1132 {A: scalar.Int32(1)},
1133 {A: scalar.Int32(2)},
1134 },
1135 }},
1136 wire: pack.Message{
1137 pack.Tag{3, pack.StartGroupType},
1138 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1139 pack.Tag{3, pack.EndGroupType},
1140 pack.Tag{3, pack.StartGroupType},
1141 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1142 pack.Tag{3, pack.EndGroupType},
1143 }.Marshal(),
1144 },
1145 {
1146 desc: "required field in extension message unset",
1147 partial: true,
1148 invalidExtensions: true,
1149 decodeTo: []proto.Message{build(
1150 &testpb.TestAllExtensions{},
1151 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1152 )},
1153 wire: pack.Message{
1154 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1155 }.Marshal(),
1156 },
1157 {
1158 desc: "required field in extension message set",
1159 decodeTo: []proto.Message{build(
1160 &testpb.TestAllExtensions{},
1161 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
1162 RequiredField: scalar.Int32(1),
1163 }),
1164 )},
1165 wire: pack.Message{
1166 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1167 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1168 }),
1169 }.Marshal(),
1170 },
1171 {
1172 desc: "required field in repeated extension message unset",
1173 partial: true,
1174 invalidExtensions: true,
1175 decodeTo: []proto.Message{build(
1176 &testpb.TestAllExtensions{},
1177 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1178 {RequiredField: scalar.Int32(1)},
1179 {},
1180 }),
1181 )},
1182 wire: pack.Message{
1183 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1184 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1185 }),
1186 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1187 }.Marshal(),
1188 },
1189 {
1190 desc: "required field in repeated extension message set",
1191 decodeTo: []proto.Message{build(
1192 &testpb.TestAllExtensions{},
1193 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1194 {RequiredField: scalar.Int32(1)},
1195 {RequiredField: scalar.Int32(2)},
1196 }),
1197 )},
1198 wire: pack.Message{
1199 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1200 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1201 }),
1202 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1203 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1204 }),
1205 }.Marshal(),
1206 },
Damien Neilc37adef2019-04-01 13:49:56 -07001207 {
1208 desc: "legacy",
1209 partial: true,
1210 decodeTo: []proto.Message{
1211 &legacypb.Legacy{
1212 F1: &legacy1pb.Message{
1213 OptionalInt32: scalar.Int32(1),
1214 OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
1215 OptionalChildMessage: &legacy1pb.Message_ChildMessage{
1216 F1: scalar.String("x"),
1217 },
1218 Optionalgroup: &legacy1pb.Message_OptionalGroup{
1219 F1: scalar.String("x"),
1220 },
1221 RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
1222 {F1: scalar.String("x")},
1223 },
1224 Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
1225 {F1: scalar.String("x")},
1226 },
1227 MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
1228 true: {F1: scalar.String("x")},
1229 },
1230 OneofUnion: &legacy1pb.Message_OneofChildMessage{
1231 &legacy1pb.Message_ChildMessage{
1232 F1: scalar.String("x"),
1233 },
1234 },
1235 },
1236 },
1237 },
1238 wire: pack.Message{
1239 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1240 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1241 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1242 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1243 pack.Tag{1, pack.BytesType}, pack.String("x"),
1244 }),
1245 pack.Tag{120, pack.StartGroupType},
1246 pack.Tag{1, pack.BytesType}, pack.String("x"),
1247 pack.Tag{120, pack.EndGroupType},
1248 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1249 pack.Tag{1, pack.BytesType}, pack.String("x"),
1250 }),
1251 pack.Tag{520, pack.StartGroupType},
1252 pack.Tag{1, pack.BytesType}, pack.String("x"),
1253 pack.Tag{520, pack.EndGroupType},
1254 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1255 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1256 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1257 pack.Tag{1, pack.BytesType}, pack.String("x"),
1258 }),
1259 }),
1260 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1261 pack.Tag{1, pack.BytesType}, pack.String("x"),
1262 }),
1263 }),
1264 }.Marshal(),
1265 },
Damien Neilba23aa52018-12-07 14:38:17 -08001266}
1267
Damien Neilbc310b52019-04-11 11:46:55 -07001268var invalidUTF8TestProtos = []testProto{
1269 {
1270 desc: "invalid UTF-8 in optional string field",
1271 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1272 OptionalString: "abc\xff",
1273 }},
1274 wire: pack.Message{
1275 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1276 }.Marshal(),
1277 },
1278 {
1279 desc: "invalid UTF-8 in repeated string field",
1280 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1281 RepeatedString: []string{"foo", "abc\xff"},
1282 }},
1283 wire: pack.Message{
1284 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1285 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1286 }.Marshal(),
1287 },
1288 {
1289 desc: "invalid UTF-8 in nested message",
1290 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1291 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1292 Corecursive: &test3pb.TestAllTypes{
1293 OptionalString: "abc\xff",
1294 },
1295 },
1296 }},
1297 wire: pack.Message{
1298 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1299 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1300 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1301 }),
1302 }),
1303 }.Marshal(),
1304 },
1305 {
Damien Neilc37adef2019-04-01 13:49:56 -07001306 desc: "invalid UTF-8 in oneof field",
1307 decodeTo: []proto.Message{
1308 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
1309 },
1310 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1311 },
1312 {
Damien Neilbc310b52019-04-11 11:46:55 -07001313 desc: "invalid UTF-8 in map key",
1314 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1315 MapStringString: map[string]string{"key\xff": "val"},
1316 }},
1317 wire: pack.Message{
1318 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1319 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1320 pack.Tag{2, pack.BytesType}, pack.String("val"),
1321 }),
1322 }.Marshal(),
1323 },
1324 {
1325 desc: "invalid UTF-8 in map value",
1326 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1327 MapStringString: map[string]string{"key": "val\xff"},
1328 }},
1329 wire: pack.Message{
1330 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1331 pack.Tag{1, pack.BytesType}, pack.String("key"),
1332 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1333 }),
1334 }.Marshal(),
1335 },
1336}
1337
Damien Neil4be2fb42018-12-17 11:16:16 -08001338func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001339 for _, opt := range opts {
1340 opt(m)
1341 }
1342 return m
1343}
1344
Damien Neil4be2fb42018-12-17 11:16:16 -08001345type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001346
Joe Tsai378c1322019-04-25 23:48:08 -07001347func unknown(raw pref.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001348 return func(m proto.Message) {
Joe Tsai378c1322019-04-25 23:48:08 -07001349 m.ProtoReflect().SetUnknown(raw)
Damien Neile6f060f2019-04-23 17:11:02 -07001350 }
1351}
1352
Damien Neilba23aa52018-12-07 14:38:17 -08001353func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001354 return func(m proto.Message) {
Damien Neilba23aa52018-12-07 14:38:17 -08001355 if err := protoV1.SetExtension(m.(protoV1.Message), desc, value); err != nil {
1356 panic(err)
1357 }
1358 }
1359}
Damien Neil61e93c72019-03-27 09:23:20 -07001360
1361func marshalText(m proto.Message) string {
Damien Neil5c5b5312019-05-14 12:44:37 -07001362 b, _ := prototext.Marshal(m)
Damien Neil61e93c72019-03-27 09:23:20 -07001363 return string(b)
1364}
Damien Neilbc310b52019-04-11 11:46:55 -07001365
1366func isErrInvalidUTF8(err error) bool {
1367 nerr, ok := err.(errors.NonFatalErrors)
1368 if !ok || len(nerr) == 0 {
1369 return false
1370 }
1371 for _, err := range nerr {
1372 if e, ok := err.(interface{ InvalidUTF8() bool }); ok && e.InvalidUTF8() {
1373 continue
1374 }
1375 return false
1376 }
1377 return true
1378}