blob: db7c8f9cc391978191bd2ec859db6c5c5b107ccd [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 {
Joe Tsai09cef322019-07-11 22:13:49 -070026 desc string
27 decodeTo []proto.Message
28 wire []byte
29 partial bool
Damien Neilba23aa52018-12-07 14:38:17 -080030}
31
32func TestDecode(t *testing.T) {
33 for _, test := range testProtos {
34 for _, want := range test.decodeTo {
35 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070036 opts := proto.UnmarshalOptions{
37 AllowPartial: test.partial,
38 }
Damien Neilba23aa52018-12-07 14:38:17 -080039 wire := append(([]byte)(nil), test.wire...)
Damien Neil4be2fb42018-12-17 11:16:16 -080040 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
Damien Neil96c229a2019-04-03 12:17:24 -070041 if err := opts.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070042 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080043 return
44 }
45
46 // Aliasing check: Modifying the original wire bytes shouldn't
47 // affect the unmarshaled message.
48 for i := range wire {
49 wire[i] = 0
50 }
Joe Tsaidb38ddd2019-05-07 15:14:40 -070051 if !proto.Equal(got, want) {
Damien Neil61e93c72019-03-27 09:23:20 -070052 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080053 }
54 })
55 }
56 }
57}
58
Damien Neil96c229a2019-04-03 12:17:24 -070059func TestDecodeRequiredFieldChecks(t *testing.T) {
60 for _, test := range testProtos {
61 if !test.partial {
62 continue
63 }
Damien Neil96c229a2019-04-03 12:17:24 -070064 for _, m := range test.decodeTo {
65 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
66 got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
67 if err := proto.Unmarshal(test.wire, got); err == nil {
68 t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got))
69 }
70 })
71 }
72 }
73}
74
Damien Neilbc310b52019-04-11 11:46:55 -070075func TestDecodeInvalidUTF8(t *testing.T) {
76 for _, test := range invalidUTF8TestProtos {
77 for _, want := range test.decodeTo {
78 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
79 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
80 err := proto.Unmarshal(test.wire, got)
Damien Neil8c86fc52019-06-19 09:28:29 -070081 if err == nil {
Damien Neilbc310b52019-04-11 11:46:55 -070082 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
83 }
Damien Neilbc310b52019-04-11 11:46:55 -070084 })
85 }
86 }
87}
88
Damien Neilba23aa52018-12-07 14:38:17 -080089var testProtos = []testProto{
90 {
91 desc: "basic scalar types",
Damien Neil4be2fb42018-12-17 11:16:16 -080092 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -080093 OptionalInt32: scalar.Int32(1001),
94 OptionalInt64: scalar.Int64(1002),
95 OptionalUint32: scalar.Uint32(1003),
96 OptionalUint64: scalar.Uint64(1004),
97 OptionalSint32: scalar.Int32(1005),
98 OptionalSint64: scalar.Int64(1006),
99 OptionalFixed32: scalar.Uint32(1007),
100 OptionalFixed64: scalar.Uint64(1008),
101 OptionalSfixed32: scalar.Int32(1009),
102 OptionalSfixed64: scalar.Int64(1010),
103 OptionalFloat: scalar.Float32(1011.5),
104 OptionalDouble: scalar.Float64(1012.5),
105 OptionalBool: scalar.Bool(true),
106 OptionalString: scalar.String("string"),
107 OptionalBytes: []byte("bytes"),
108 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
Damien Neil3b46ade2019-03-26 13:55:02 -0700109 }, &test3pb.TestAllTypes{
110 OptionalInt32: 1001,
111 OptionalInt64: 1002,
112 OptionalUint32: 1003,
113 OptionalUint64: 1004,
114 OptionalSint32: 1005,
115 OptionalSint64: 1006,
116 OptionalFixed32: 1007,
117 OptionalFixed64: 1008,
118 OptionalSfixed32: 1009,
119 OptionalSfixed64: 1010,
120 OptionalFloat: 1011.5,
121 OptionalDouble: 1012.5,
122 OptionalBool: true,
123 OptionalString: "string",
124 OptionalBytes: []byte("bytes"),
125 OptionalNestedEnum: test3pb.TestAllTypes_BAR,
Damien Neilba23aa52018-12-07 14:38:17 -0800126 }, build(
127 &testpb.TestAllExtensions{},
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700128 extend(testpb.E_OptionalInt32Extension, int32(1001)),
129 extend(testpb.E_OptionalInt64Extension, int64(1002)),
130 extend(testpb.E_OptionalUint32Extension, uint32(1003)),
131 extend(testpb.E_OptionalUint64Extension, uint64(1004)),
132 extend(testpb.E_OptionalSint32Extension, int32(1005)),
133 extend(testpb.E_OptionalSint64Extension, int64(1006)),
134 extend(testpb.E_OptionalFixed32Extension, uint32(1007)),
135 extend(testpb.E_OptionalFixed64Extension, uint64(1008)),
136 extend(testpb.E_OptionalSfixed32Extension, int32(1009)),
137 extend(testpb.E_OptionalSfixed64Extension, int64(1010)),
138 extend(testpb.E_OptionalFloatExtension, float32(1011.5)),
139 extend(testpb.E_OptionalDoubleExtension, float64(1012.5)),
140 extend(testpb.E_OptionalBoolExtension, bool(true)),
141 extend(testpb.E_OptionalStringExtension, string("string")),
Damien Neilba23aa52018-12-07 14:38:17 -0800142 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700143 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR),
Damien Neilba23aa52018-12-07 14:38:17 -0800144 )},
145 wire: pack.Message{
146 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
147 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
148 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
149 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
150 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
151 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
152 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
153 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
154 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
155 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
156 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
157 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
158 pack.Tag{13, pack.VarintType}, pack.Bool(true),
159 pack.Tag{14, pack.BytesType}, pack.String("string"),
160 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
161 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
162 }.Marshal(),
163 },
164 {
165 desc: "groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800166 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800167 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
168 A: scalar.Int32(1017),
169 },
170 }, build(
171 &testpb.TestAllExtensions{},
172 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
173 A: scalar.Int32(1017),
174 }),
175 )},
176 wire: pack.Message{
177 pack.Tag{16, pack.StartGroupType},
178 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
179 pack.Tag{16, pack.EndGroupType},
180 }.Marshal(),
181 },
182 {
183 desc: "groups (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800184 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800185 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
186 A: scalar.Int32(2),
187 },
188 }, build(
189 &testpb.TestAllExtensions{},
190 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
191 A: scalar.Int32(2),
192 }),
193 )},
194 wire: pack.Message{
195 pack.Tag{16, pack.StartGroupType},
196 pack.Tag{17, pack.VarintType}, pack.Varint(1),
197 pack.Tag{16, pack.EndGroupType},
198 pack.Tag{16, pack.StartGroupType},
199 pack.Tag{17, pack.VarintType}, pack.Varint(2),
200 pack.Tag{16, pack.EndGroupType},
201 }.Marshal(),
202 },
203 {
204 desc: "messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800205 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800206 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
207 A: scalar.Int32(42),
208 Corecursive: &testpb.TestAllTypes{
209 OptionalInt32: scalar.Int32(43),
210 },
211 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700212 }, &test3pb.TestAllTypes{
213 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
214 A: 42,
215 Corecursive: &test3pb.TestAllTypes{
216 OptionalInt32: 43,
217 },
218 },
Damien Neilba23aa52018-12-07 14:38:17 -0800219 }, build(
220 &testpb.TestAllExtensions{},
221 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
222 A: scalar.Int32(42),
223 Corecursive: &testpb.TestAllTypes{
224 OptionalInt32: scalar.Int32(43),
225 },
226 }),
227 )},
228 wire: pack.Message{
229 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
230 pack.Tag{1, pack.VarintType}, pack.Varint(42),
231 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
232 pack.Tag{1, pack.VarintType}, pack.Varint(43),
233 }),
234 }),
235 }.Marshal(),
236 },
237 {
238 desc: "messages (split across multiple tags)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800239 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800240 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
241 A: scalar.Int32(42),
242 Corecursive: &testpb.TestAllTypes{
243 OptionalInt32: scalar.Int32(43),
244 },
245 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700246 }, &test3pb.TestAllTypes{
247 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
248 A: 42,
249 Corecursive: &test3pb.TestAllTypes{
250 OptionalInt32: 43,
251 },
252 },
Damien Neilba23aa52018-12-07 14:38:17 -0800253 }, build(
254 &testpb.TestAllExtensions{},
255 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
256 A: scalar.Int32(42),
257 Corecursive: &testpb.TestAllTypes{
258 OptionalInt32: scalar.Int32(43),
259 },
260 }),
261 )},
262 wire: pack.Message{
263 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
264 pack.Tag{1, pack.VarintType}, pack.Varint(42),
265 }),
266 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
267 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
268 pack.Tag{1, pack.VarintType}, pack.Varint(43),
269 }),
270 }),
271 }.Marshal(),
272 },
273 {
274 desc: "messages (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800275 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800276 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
277 A: scalar.Int32(2),
278 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700279 }, &test3pb.TestAllTypes{
280 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
281 A: 2,
282 },
Damien Neilba23aa52018-12-07 14:38:17 -0800283 }, build(
284 &testpb.TestAllExtensions{},
285 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
286 A: scalar.Int32(2),
287 }),
288 )},
289 wire: pack.Message{
290 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
291 pack.Tag{1, pack.VarintType}, pack.Varint(1),
292 }),
293 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
294 pack.Tag{1, pack.VarintType}, pack.Varint(2),
295 }),
296 }.Marshal(),
297 },
298 {
299 desc: "basic repeated types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800300 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800301 RepeatedInt32: []int32{1001, 2001},
302 RepeatedInt64: []int64{1002, 2002},
303 RepeatedUint32: []uint32{1003, 2003},
304 RepeatedUint64: []uint64{1004, 2004},
305 RepeatedSint32: []int32{1005, 2005},
306 RepeatedSint64: []int64{1006, 2006},
307 RepeatedFixed32: []uint32{1007, 2007},
308 RepeatedFixed64: []uint64{1008, 2008},
309 RepeatedSfixed32: []int32{1009, 2009},
310 RepeatedSfixed64: []int64{1010, 2010},
311 RepeatedFloat: []float32{1011.5, 2011.5},
312 RepeatedDouble: []float64{1012.5, 2012.5},
313 RepeatedBool: []bool{true, false},
314 RepeatedString: []string{"foo", "bar"},
315 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
316 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
317 testpb.TestAllTypes_FOO,
318 testpb.TestAllTypes_BAR,
319 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700320 }, &test3pb.TestAllTypes{
321 RepeatedInt32: []int32{1001, 2001},
322 RepeatedInt64: []int64{1002, 2002},
323 RepeatedUint32: []uint32{1003, 2003},
324 RepeatedUint64: []uint64{1004, 2004},
325 RepeatedSint32: []int32{1005, 2005},
326 RepeatedSint64: []int64{1006, 2006},
327 RepeatedFixed32: []uint32{1007, 2007},
328 RepeatedFixed64: []uint64{1008, 2008},
329 RepeatedSfixed32: []int32{1009, 2009},
330 RepeatedSfixed64: []int64{1010, 2010},
331 RepeatedFloat: []float32{1011.5, 2011.5},
332 RepeatedDouble: []float64{1012.5, 2012.5},
333 RepeatedBool: []bool{true, false},
334 RepeatedString: []string{"foo", "bar"},
335 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
336 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
337 test3pb.TestAllTypes_FOO,
338 test3pb.TestAllTypes_BAR,
339 },
Damien Neilba23aa52018-12-07 14:38:17 -0800340 }, build(
341 &testpb.TestAllExtensions{},
342 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
343 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
344 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
345 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
346 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
347 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
348 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
349 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
350 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
351 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
352 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
353 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
354 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
355 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
356 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
357 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
358 testpb.TestAllTypes_FOO,
359 testpb.TestAllTypes_BAR,
360 }),
361 )},
362 wire: pack.Message{
363 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
364 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
365 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
366 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
367 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
368 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
369 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
370 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
371 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
372 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
373 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
374 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
375 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
376 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
377 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
378 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
379 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
380 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
381 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
382 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
383 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
384 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
385 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
386 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
387 pack.Tag{43, pack.VarintType}, pack.Bool(true),
388 pack.Tag{43, pack.VarintType}, pack.Bool(false),
389 pack.Tag{44, pack.BytesType}, pack.String("foo"),
390 pack.Tag{44, pack.BytesType}, pack.String("bar"),
391 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
392 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
393 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
394 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
395 }.Marshal(),
396 },
397 {
398 desc: "basic repeated types (packed encoding)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800399 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800400 RepeatedInt32: []int32{1001, 2001},
401 RepeatedInt64: []int64{1002, 2002},
402 RepeatedUint32: []uint32{1003, 2003},
403 RepeatedUint64: []uint64{1004, 2004},
404 RepeatedSint32: []int32{1005, 2005},
405 RepeatedSint64: []int64{1006, 2006},
406 RepeatedFixed32: []uint32{1007, 2007},
407 RepeatedFixed64: []uint64{1008, 2008},
408 RepeatedSfixed32: []int32{1009, 2009},
409 RepeatedSfixed64: []int64{1010, 2010},
410 RepeatedFloat: []float32{1011.5, 2011.5},
411 RepeatedDouble: []float64{1012.5, 2012.5},
412 RepeatedBool: []bool{true, false},
413 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
414 testpb.TestAllTypes_FOO,
415 testpb.TestAllTypes_BAR,
416 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700417 }, &test3pb.TestAllTypes{
418 RepeatedInt32: []int32{1001, 2001},
419 RepeatedInt64: []int64{1002, 2002},
420 RepeatedUint32: []uint32{1003, 2003},
421 RepeatedUint64: []uint64{1004, 2004},
422 RepeatedSint32: []int32{1005, 2005},
423 RepeatedSint64: []int64{1006, 2006},
424 RepeatedFixed32: []uint32{1007, 2007},
425 RepeatedFixed64: []uint64{1008, 2008},
426 RepeatedSfixed32: []int32{1009, 2009},
427 RepeatedSfixed64: []int64{1010, 2010},
428 RepeatedFloat: []float32{1011.5, 2011.5},
429 RepeatedDouble: []float64{1012.5, 2012.5},
430 RepeatedBool: []bool{true, false},
431 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
432 test3pb.TestAllTypes_FOO,
433 test3pb.TestAllTypes_BAR,
434 },
Damien Neilba23aa52018-12-07 14:38:17 -0800435 }, build(
436 &testpb.TestAllExtensions{},
437 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
438 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
439 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
440 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
441 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
442 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
443 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
444 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
445 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
446 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
447 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
448 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
449 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
450 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
451 testpb.TestAllTypes_FOO,
452 testpb.TestAllTypes_BAR,
453 }),
454 )},
455 wire: pack.Message{
456 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
457 pack.Varint(1001), pack.Varint(2001),
458 },
459 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
460 pack.Varint(1002), pack.Varint(2002),
461 },
462 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
463 pack.Uvarint(1003), pack.Uvarint(2003),
464 },
465 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
466 pack.Uvarint(1004), pack.Uvarint(2004),
467 },
468 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
469 pack.Svarint(1005), pack.Svarint(2005),
470 },
471 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
472 pack.Svarint(1006), pack.Svarint(2006),
473 },
474 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
475 pack.Uint32(1007), pack.Uint32(2007),
476 },
477 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
478 pack.Uint64(1008), pack.Uint64(2008),
479 },
480 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
481 pack.Int32(1009), pack.Int32(2009),
482 },
483 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
484 pack.Int64(1010), pack.Int64(2010),
485 },
486 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
487 pack.Float32(1011.5), pack.Float32(2011.5),
488 },
489 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
490 pack.Float64(1012.5), pack.Float64(2012.5),
491 },
492 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
493 pack.Bool(true), pack.Bool(false),
494 },
495 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
496 pack.Varint(int(testpb.TestAllTypes_FOO)),
497 pack.Varint(int(testpb.TestAllTypes_BAR)),
498 },
499 }.Marshal(),
500 },
501 {
Damien Neil7492a092019-07-10 15:23:29 -0700502 desc: "packed repeated types",
503 decodeTo: []proto.Message{&testpb.TestPackedTypes{
504 PackedInt32: []int32{1001, 2001},
505 PackedInt64: []int64{1002, 2002},
506 PackedUint32: []uint32{1003, 2003},
507 PackedUint64: []uint64{1004, 2004},
508 PackedSint32: []int32{1005, 2005},
509 PackedSint64: []int64{1006, 2006},
510 PackedFixed32: []uint32{1007, 2007},
511 PackedFixed64: []uint64{1008, 2008},
512 PackedSfixed32: []int32{1009, 2009},
513 PackedSfixed64: []int64{1010, 2010},
514 PackedFloat: []float32{1011.5, 2011.5},
515 PackedDouble: []float64{1012.5, 2012.5},
516 PackedBool: []bool{true, false},
517 PackedEnum: []testpb.ForeignEnum{
518 testpb.ForeignEnum_FOREIGN_FOO,
519 testpb.ForeignEnum_FOREIGN_BAR,
520 },
521 }, build(
522 &testpb.TestPackedExtensions{},
523 extend(testpb.E_PackedInt32Extension, []int32{1001, 2001}),
524 extend(testpb.E_PackedInt64Extension, []int64{1002, 2002}),
525 extend(testpb.E_PackedUint32Extension, []uint32{1003, 2003}),
526 extend(testpb.E_PackedUint64Extension, []uint64{1004, 2004}),
527 extend(testpb.E_PackedSint32Extension, []int32{1005, 2005}),
528 extend(testpb.E_PackedSint64Extension, []int64{1006, 2006}),
529 extend(testpb.E_PackedFixed32Extension, []uint32{1007, 2007}),
530 extend(testpb.E_PackedFixed64Extension, []uint64{1008, 2008}),
531 extend(testpb.E_PackedSfixed32Extension, []int32{1009, 2009}),
532 extend(testpb.E_PackedSfixed64Extension, []int64{1010, 2010}),
533 extend(testpb.E_PackedFloatExtension, []float32{1011.5, 2011.5}),
534 extend(testpb.E_PackedDoubleExtension, []float64{1012.5, 2012.5}),
535 extend(testpb.E_PackedBoolExtension, []bool{true, false}),
536 extend(testpb.E_PackedEnumExtension, []testpb.ForeignEnum{
537 testpb.ForeignEnum_FOREIGN_FOO,
538 testpb.ForeignEnum_FOREIGN_BAR,
539 }),
540 )},
541 wire: pack.Message{
542 pack.Tag{90, pack.BytesType}, pack.LengthPrefix{
543 pack.Varint(1001), pack.Varint(2001),
544 },
545 pack.Tag{91, pack.BytesType}, pack.LengthPrefix{
546 pack.Varint(1002), pack.Varint(2002),
547 },
548 pack.Tag{92, pack.BytesType}, pack.LengthPrefix{
549 pack.Uvarint(1003), pack.Uvarint(2003),
550 },
551 pack.Tag{93, pack.BytesType}, pack.LengthPrefix{
552 pack.Uvarint(1004), pack.Uvarint(2004),
553 },
554 pack.Tag{94, pack.BytesType}, pack.LengthPrefix{
555 pack.Svarint(1005), pack.Svarint(2005),
556 },
557 pack.Tag{95, pack.BytesType}, pack.LengthPrefix{
558 pack.Svarint(1006), pack.Svarint(2006),
559 },
560 pack.Tag{96, pack.BytesType}, pack.LengthPrefix{
561 pack.Uint32(1007), pack.Uint32(2007),
562 },
563 pack.Tag{97, pack.BytesType}, pack.LengthPrefix{
564 pack.Uint64(1008), pack.Uint64(2008),
565 },
566 pack.Tag{98, pack.BytesType}, pack.LengthPrefix{
567 pack.Int32(1009), pack.Int32(2009),
568 },
569 pack.Tag{99, pack.BytesType}, pack.LengthPrefix{
570 pack.Int64(1010), pack.Int64(2010),
571 },
572 pack.Tag{100, pack.BytesType}, pack.LengthPrefix{
573 pack.Float32(1011.5), pack.Float32(2011.5),
574 },
575 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{
576 pack.Float64(1012.5), pack.Float64(2012.5),
577 },
578 pack.Tag{102, pack.BytesType}, pack.LengthPrefix{
579 pack.Bool(true), pack.Bool(false),
580 },
581 pack.Tag{103, pack.BytesType}, pack.LengthPrefix{
582 pack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
583 pack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
584 },
585 }.Marshal(),
586 },
587 {
Damien Neilba23aa52018-12-07 14:38:17 -0800588 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800589 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800590 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
591 {A: scalar.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700592 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800593 {A: scalar.Int32(2)},
594 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700595 }, &test3pb.TestAllTypes{
596 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
597 {A: 1},
Damien Neilc37adef2019-04-01 13:49:56 -0700598 nil,
Damien Neil3b46ade2019-03-26 13:55:02 -0700599 {A: 2},
600 },
Damien Neilba23aa52018-12-07 14:38:17 -0800601 }, build(
602 &testpb.TestAllExtensions{},
603 extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
604 {A: scalar.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700605 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800606 {A: scalar.Int32(2)},
607 }),
608 )},
609 wire: pack.Message{
610 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
611 pack.Tag{1, pack.VarintType}, pack.Varint(1),
612 }),
Damien Neilc37adef2019-04-01 13:49:56 -0700613 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
Damien Neilba23aa52018-12-07 14:38:17 -0800614 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
615 pack.Tag{1, pack.VarintType}, pack.Varint(2),
616 }),
617 }.Marshal(),
618 },
619 {
620 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800621 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800622 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
623 {A: scalar.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700624 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800625 {A: scalar.Int32(2017)},
626 },
627 }, build(
628 &testpb.TestAllExtensions{},
629 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
630 {A: scalar.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700631 nil,
Damien Neilba23aa52018-12-07 14:38:17 -0800632 {A: scalar.Int32(2017)},
633 }),
634 )},
635 wire: pack.Message{
636 pack.Tag{46, pack.StartGroupType},
637 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
638 pack.Tag{46, pack.EndGroupType},
639 pack.Tag{46, pack.StartGroupType},
Damien Neilc37adef2019-04-01 13:49:56 -0700640 pack.Tag{46, pack.EndGroupType},
641 pack.Tag{46, pack.StartGroupType},
Damien Neilba23aa52018-12-07 14:38:17 -0800642 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
643 pack.Tag{46, pack.EndGroupType},
644 }.Marshal(),
645 },
646 {
647 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800648 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800649 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
650 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
651 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
652 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
653 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
654 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
655 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
656 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
657 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
658 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
659 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
660 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
661 MapBoolBool: map[bool]bool{true: false, false: true},
662 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
663 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
664 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
665 "71.1.key": {A: scalar.Int32(1171)},
666 "71.2.key": {A: scalar.Int32(2171)},
667 },
668 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
669 "73.1.key": testpb.TestAllTypes_FOO,
670 "73.2.key": testpb.TestAllTypes_BAR,
671 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700672 }, &test3pb.TestAllTypes{
673 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
674 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
675 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
676 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
677 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
678 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
679 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
680 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
681 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
682 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
683 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
684 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
685 MapBoolBool: map[bool]bool{true: false, false: true},
686 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
687 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
688 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
689 "71.1.key": {A: 1171},
690 "71.2.key": {A: 2171},
691 },
692 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
693 "73.1.key": test3pb.TestAllTypes_FOO,
694 "73.2.key": test3pb.TestAllTypes_BAR,
695 },
Damien Neilba23aa52018-12-07 14:38:17 -0800696 }},
697 wire: pack.Message{
698 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
699 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
700 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
701 }),
702 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
703 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
704 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
705 }),
706 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
707 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
708 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
709 }),
710 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
711 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
712 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
713 }),
714 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
715 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
716 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
717 }),
718 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
719 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
720 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
721 }),
722 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
723 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
724 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
725 }),
726 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
727 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
728 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
729 }),
730 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
731 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
732 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
733 }),
734 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
735 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
736 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
737 }),
738 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
739 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
740 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
741 }),
742 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
743 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
744 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
745 }),
746 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
747 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
748 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
749 }),
750 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
751 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
752 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
753 }),
754 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
755 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
756 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
757 }),
758 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
759 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
760 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
761 }),
762 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
763 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
764 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
765 }),
766 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
767 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
768 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
769 }),
770 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
771 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
772 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
773 }),
774 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
775 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
776 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
777 }),
778 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
779 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
780 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
781 }),
782 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
783 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
784 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
785 }),
786 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
787 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
788 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
789 }),
790 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
791 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
792 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
793 }),
794 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
795 pack.Tag{1, pack.VarintType}, pack.Bool(true),
796 pack.Tag{2, pack.VarintType}, pack.Bool(false),
797 }),
798 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
799 pack.Tag{1, pack.VarintType}, pack.Bool(false),
800 pack.Tag{2, pack.VarintType}, pack.Bool(true),
801 }),
802 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
803 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
804 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
805 }),
806 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
807 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
808 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
809 }),
810 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
811 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
812 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
813 }),
814 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
815 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
816 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
817 }),
818 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
819 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
820 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
821 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
822 }),
823 }),
824 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
825 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
826 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
827 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
828 }),
829 }),
830 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
831 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
832 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
833 }),
834 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
835 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
836 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
837 }),
838 }.Marshal(),
839 },
840 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700841 desc: "oneof (uint32)",
842 decodeTo: []proto.Message{
843 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
844 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
845 },
846 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800847 },
848 {
849 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700850 decodeTo: []proto.Message{
851 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
852 &testpb.TestAllTypes_NestedMessage{A: scalar.Int32(1112)},
853 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
854 &test3pb.TestAllTypes_NestedMessage{A: 1112},
855 }},
856 },
Damien Neilba23aa52018-12-07 14:38:17 -0800857 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
858 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
859 })}.Marshal(),
860 },
861 {
Damien Neilc37adef2019-04-01 13:49:56 -0700862 desc: "oneof (empty message)",
863 decodeTo: []proto.Message{
864 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
865 &testpb.TestAllTypes_NestedMessage{},
866 }},
867 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
868 &test3pb.TestAllTypes_NestedMessage{},
869 }},
870 },
871 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
872 },
873 {
Joe Tsai6c286742019-07-11 23:15:05 -0700874 desc: "oneof (merged message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700875 decodeTo: []proto.Message{
876 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
877 &testpb.TestAllTypes_NestedMessage{
Joe Tsai6c286742019-07-11 23:15:05 -0700878 A: scalar.Int32(1),
Damien Neil3b46ade2019-03-26 13:55:02 -0700879 Corecursive: &testpb.TestAllTypes{
880 OptionalInt32: scalar.Int32(43),
881 },
Damien Neilba23aa52018-12-07 14:38:17 -0800882 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700883 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
884 &test3pb.TestAllTypes_NestedMessage{
Joe Tsai6c286742019-07-11 23:15:05 -0700885 A: 1,
Damien Neil3b46ade2019-03-26 13:55:02 -0700886 Corecursive: &test3pb.TestAllTypes{
887 OptionalInt32: 43,
888 },
889 },
890 }}},
Damien Neilba23aa52018-12-07 14:38:17 -0800891 wire: pack.Message{
892 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
893 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
894 }),
895 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
896 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
897 pack.Tag{1, pack.VarintType}, pack.Varint(43),
898 }),
899 }),
900 }.Marshal(),
901 },
902 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700903 desc: "oneof (string)",
904 decodeTo: []proto.Message{
905 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
906 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
907 },
908 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800909 },
910 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700911 desc: "oneof (bytes)",
912 decodeTo: []proto.Message{
913 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
914 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
915 },
916 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800917 },
918 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700919 desc: "oneof (bool)",
920 decodeTo: []proto.Message{
921 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
922 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
923 },
924 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800925 },
926 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700927 desc: "oneof (uint64)",
928 decodeTo: []proto.Message{
929 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
930 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
931 },
932 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800933 },
934 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700935 desc: "oneof (float)",
936 decodeTo: []proto.Message{
937 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
938 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
939 },
940 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800941 },
942 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700943 desc: "oneof (double)",
944 decodeTo: []proto.Message{
945 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
946 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
947 },
948 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800949 },
950 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700951 desc: "oneof (enum)",
952 decodeTo: []proto.Message{
953 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
954 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
955 },
956 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800957 },
958 {
Damien Neilc37adef2019-04-01 13:49:56 -0700959 desc: "oneof (zero)",
960 decodeTo: []proto.Message{
961 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}},
962 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}},
963 },
964 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
965 },
966 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700967 desc: "oneof (overridden value)",
968 decodeTo: []proto.Message{
969 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
970 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
971 },
Damien Neilba23aa52018-12-07 14:38:17 -0800972 wire: pack.Message{
973 pack.Tag{111, pack.VarintType}, pack.Varint(1),
974 pack.Tag{116, pack.VarintType}, pack.Varint(2),
975 }.Marshal(),
976 },
977 // TODO: More unknown field tests for ordering, repeated fields, etc.
978 //
979 // It is currently impossible to produce results that the v1 Equal
980 // considers equivalent to those of the v1 decoder. Figure out if
981 // that's a problem or not.
982 {
983 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -0800984 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -0800985 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -0700986 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -0800987 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
988 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -0700989 ), build(
990 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -0700991 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -0700992 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
993 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -0800994 )},
995 wire: pack.Message{
996 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
997 }.Marshal(),
998 },
999 {
1000 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001001 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001002 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001003 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001004 pack.Tag{1, pack.BytesType}, pack.String("string"),
1005 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001006 ), build(
1007 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001008 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001009 pack.Tag{1, pack.BytesType}, pack.String("string"),
1010 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001011 )},
1012 wire: pack.Message{
1013 pack.Tag{1, pack.BytesType}, pack.String("string"),
1014 }.Marshal(),
1015 },
1016 {
1017 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001018 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001019 &testpb.TestAllTypes{
1020 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -07001021 }, &test3pb.TestAllTypes{
1022 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -08001023 },
1024 },
1025 wire: pack.Message{
1026 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
1027 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1028 pack.Tag{2, pack.BytesType}, pack.String("string"),
1029 }),
1030 }.Marshal(),
1031 },
Damien Neil96c229a2019-04-03 12:17:24 -07001032 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001033 desc: "required field in nil message unset",
1034 partial: true,
1035 decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
1036 },
1037 {
Damien Neil96c229a2019-04-03 12:17:24 -07001038 desc: "required field unset",
1039 partial: true,
1040 decodeTo: []proto.Message{&testpb.TestRequired{}},
1041 },
1042 {
1043 desc: "required field set",
1044 decodeTo: []proto.Message{&testpb.TestRequired{
1045 RequiredField: scalar.Int32(1),
1046 }},
1047 wire: pack.Message{
1048 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1049 }.Marshal(),
1050 },
1051 {
1052 desc: "required field in optional message unset",
1053 partial: true,
1054 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1055 OptionalMessage: &testpb.TestRequired{},
1056 }},
1057 wire: pack.Message{
1058 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1059 }.Marshal(),
1060 },
1061 {
1062 desc: "required field in optional message set",
1063 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1064 OptionalMessage: &testpb.TestRequired{
1065 RequiredField: scalar.Int32(1),
1066 },
1067 }},
1068 wire: pack.Message{
1069 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1070 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1071 }),
1072 }.Marshal(),
1073 },
Damien Neil4686e232019-04-05 13:31:40 -07001074 {
1075 desc: "required field in optional message set (split across multiple tags)",
1076 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1077 OptionalMessage: &testpb.TestRequired{
1078 RequiredField: scalar.Int32(1),
1079 },
1080 }},
1081 wire: pack.Message{
1082 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1083 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1084 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1085 }),
1086 }.Marshal(),
1087 },
Damien Neil96c229a2019-04-03 12:17:24 -07001088 {
1089 desc: "required field in repeated message unset",
1090 partial: true,
1091 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1092 RepeatedMessage: []*testpb.TestRequired{
1093 {RequiredField: scalar.Int32(1)},
1094 {},
1095 },
1096 }},
1097 wire: pack.Message{
1098 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1099 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1100 }),
1101 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1102 }.Marshal(),
1103 },
1104 {
1105 desc: "required field in repeated message set",
1106 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1107 RepeatedMessage: []*testpb.TestRequired{
1108 {RequiredField: scalar.Int32(1)},
1109 {RequiredField: scalar.Int32(2)},
1110 },
1111 }},
1112 wire: pack.Message{
1113 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1114 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1115 }),
1116 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1117 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1118 }),
1119 }.Marshal(),
1120 },
1121 {
1122 desc: "required field in map message unset",
1123 partial: true,
1124 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1125 MapMessage: map[int32]*testpb.TestRequired{
1126 1: {RequiredField: scalar.Int32(1)},
1127 2: {},
1128 },
1129 }},
1130 wire: pack.Message{
1131 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1132 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1133 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1134 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1135 }),
1136 }),
1137 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1138 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1139 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1140 }),
1141 }.Marshal(),
1142 },
1143 {
1144 desc: "required field in map message set",
1145 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1146 MapMessage: map[int32]*testpb.TestRequired{
1147 1: {RequiredField: scalar.Int32(1)},
1148 2: {RequiredField: scalar.Int32(2)},
1149 },
1150 }},
1151 wire: pack.Message{
1152 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1153 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1154 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1155 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1156 }),
1157 }),
1158 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1159 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1160 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1161 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1162 }),
1163 }),
1164 }.Marshal(),
1165 },
1166 {
1167 desc: "required field in optional group unset",
1168 partial: true,
1169 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1170 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1171 }},
1172 wire: pack.Message{
1173 pack.Tag{1, pack.StartGroupType},
1174 pack.Tag{1, pack.EndGroupType},
1175 }.Marshal(),
1176 },
1177 {
1178 desc: "required field in optional group set",
1179 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1180 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
1181 A: scalar.Int32(1),
1182 },
1183 }},
1184 wire: pack.Message{
1185 pack.Tag{1, pack.StartGroupType},
1186 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1187 pack.Tag{1, pack.EndGroupType},
1188 }.Marshal(),
1189 },
1190 {
1191 desc: "required field in repeated group unset",
1192 partial: true,
1193 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1194 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1195 {A: scalar.Int32(1)},
1196 {},
1197 },
1198 }},
1199 wire: pack.Message{
1200 pack.Tag{3, pack.StartGroupType},
1201 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1202 pack.Tag{3, pack.EndGroupType},
1203 pack.Tag{3, pack.StartGroupType},
1204 pack.Tag{3, pack.EndGroupType},
1205 }.Marshal(),
1206 },
1207 {
1208 desc: "required field in repeated group set",
1209 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1210 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
1211 {A: scalar.Int32(1)},
1212 {A: scalar.Int32(2)},
1213 },
1214 }},
1215 wire: pack.Message{
1216 pack.Tag{3, pack.StartGroupType},
1217 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1218 pack.Tag{3, pack.EndGroupType},
1219 pack.Tag{3, pack.StartGroupType},
1220 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1221 pack.Tag{3, pack.EndGroupType},
1222 }.Marshal(),
1223 },
1224 {
Damien Neil5322bdb2019-04-09 15:57:05 -07001225 desc: "required field in oneof message unset",
1226 partial: true,
1227 decodeTo: []proto.Message{
1228 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1229 &testpb.TestRequired{},
1230 }},
1231 },
1232 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1233 },
1234 {
1235 desc: "required field in oneof message set",
1236 decodeTo: []proto.Message{
1237 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1238 &testpb.TestRequired{
1239 RequiredField: scalar.Int32(1),
1240 },
1241 }},
1242 },
1243 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
1244 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1245 })}.Marshal(),
1246 },
1247 {
Joe Tsai09cef322019-07-11 22:13:49 -07001248 desc: "required field in extension message unset",
1249 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001250 decodeTo: []proto.Message{build(
1251 &testpb.TestAllExtensions{},
1252 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1253 )},
1254 wire: pack.Message{
1255 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1256 }.Marshal(),
1257 },
1258 {
1259 desc: "required field in extension message set",
1260 decodeTo: []proto.Message{build(
1261 &testpb.TestAllExtensions{},
1262 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
1263 RequiredField: scalar.Int32(1),
1264 }),
1265 )},
1266 wire: pack.Message{
1267 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1268 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1269 }),
1270 }.Marshal(),
1271 },
1272 {
Joe Tsai09cef322019-07-11 22:13:49 -07001273 desc: "required field in repeated extension message unset",
1274 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001275 decodeTo: []proto.Message{build(
1276 &testpb.TestAllExtensions{},
1277 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1278 {RequiredField: scalar.Int32(1)},
1279 {},
1280 }),
1281 )},
1282 wire: pack.Message{
1283 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1284 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1285 }),
1286 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1287 }.Marshal(),
1288 },
1289 {
1290 desc: "required field in repeated extension message set",
1291 decodeTo: []proto.Message{build(
1292 &testpb.TestAllExtensions{},
1293 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
1294 {RequiredField: scalar.Int32(1)},
1295 {RequiredField: scalar.Int32(2)},
1296 }),
1297 )},
1298 wire: pack.Message{
1299 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1300 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1301 }),
1302 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1303 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1304 }),
1305 }.Marshal(),
1306 },
Damien Neilc37adef2019-04-01 13:49:56 -07001307 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001308 desc: "nil messages",
1309 decodeTo: []proto.Message{
1310 (*testpb.TestAllTypes)(nil),
1311 (*test3pb.TestAllTypes)(nil),
1312 (*testpb.TestAllExtensions)(nil),
1313 },
1314 },
1315 {
Damien Neilc37adef2019-04-01 13:49:56 -07001316 desc: "legacy",
1317 partial: true,
1318 decodeTo: []proto.Message{
1319 &legacypb.Legacy{
1320 F1: &legacy1pb.Message{
1321 OptionalInt32: scalar.Int32(1),
1322 OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
1323 OptionalChildMessage: &legacy1pb.Message_ChildMessage{
1324 F1: scalar.String("x"),
1325 },
1326 Optionalgroup: &legacy1pb.Message_OptionalGroup{
1327 F1: scalar.String("x"),
1328 },
1329 RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
1330 {F1: scalar.String("x")},
1331 },
1332 Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
1333 {F1: scalar.String("x")},
1334 },
1335 MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
1336 true: {F1: scalar.String("x")},
1337 },
1338 OneofUnion: &legacy1pb.Message_OneofChildMessage{
1339 &legacy1pb.Message_ChildMessage{
1340 F1: scalar.String("x"),
1341 },
1342 },
1343 },
1344 },
1345 },
1346 wire: pack.Message{
1347 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1348 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1349 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1350 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1351 pack.Tag{1, pack.BytesType}, pack.String("x"),
1352 }),
1353 pack.Tag{120, pack.StartGroupType},
1354 pack.Tag{1, pack.BytesType}, pack.String("x"),
1355 pack.Tag{120, pack.EndGroupType},
1356 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1357 pack.Tag{1, pack.BytesType}, pack.String("x"),
1358 }),
1359 pack.Tag{520, pack.StartGroupType},
1360 pack.Tag{1, pack.BytesType}, pack.String("x"),
1361 pack.Tag{520, pack.EndGroupType},
1362 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1363 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1364 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1365 pack.Tag{1, pack.BytesType}, pack.String("x"),
1366 }),
1367 }),
1368 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1369 pack.Tag{1, pack.BytesType}, pack.String("x"),
1370 }),
1371 }),
1372 }.Marshal(),
1373 },
Damien Neilba23aa52018-12-07 14:38:17 -08001374}
1375
Damien Neilbc310b52019-04-11 11:46:55 -07001376var invalidUTF8TestProtos = []testProto{
1377 {
1378 desc: "invalid UTF-8 in optional string field",
1379 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1380 OptionalString: "abc\xff",
1381 }},
1382 wire: pack.Message{
1383 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1384 }.Marshal(),
1385 },
1386 {
1387 desc: "invalid UTF-8 in repeated string field",
1388 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1389 RepeatedString: []string{"foo", "abc\xff"},
1390 }},
1391 wire: pack.Message{
1392 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1393 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1394 }.Marshal(),
1395 },
1396 {
1397 desc: "invalid UTF-8 in nested message",
1398 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1399 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1400 Corecursive: &test3pb.TestAllTypes{
1401 OptionalString: "abc\xff",
1402 },
1403 },
1404 }},
1405 wire: pack.Message{
1406 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1407 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1408 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1409 }),
1410 }),
1411 }.Marshal(),
1412 },
1413 {
Damien Neilc37adef2019-04-01 13:49:56 -07001414 desc: "invalid UTF-8 in oneof field",
1415 decodeTo: []proto.Message{
1416 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
1417 },
1418 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1419 },
1420 {
Damien Neilbc310b52019-04-11 11:46:55 -07001421 desc: "invalid UTF-8 in map key",
1422 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1423 MapStringString: map[string]string{"key\xff": "val"},
1424 }},
1425 wire: pack.Message{
1426 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1427 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1428 pack.Tag{2, pack.BytesType}, pack.String("val"),
1429 }),
1430 }.Marshal(),
1431 },
1432 {
1433 desc: "invalid UTF-8 in map value",
1434 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1435 MapStringString: map[string]string{"key": "val\xff"},
1436 }},
1437 wire: pack.Message{
1438 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1439 pack.Tag{1, pack.BytesType}, pack.String("key"),
1440 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1441 }),
1442 }.Marshal(),
1443 },
1444}
1445
Damien Neil4be2fb42018-12-17 11:16:16 -08001446func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001447 for _, opt := range opts {
1448 opt(m)
1449 }
1450 return m
1451}
1452
Damien Neil4be2fb42018-12-17 11:16:16 -08001453type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001454
Joe Tsai378c1322019-04-25 23:48:08 -07001455func unknown(raw pref.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001456 return func(m proto.Message) {
Joe Tsai378c1322019-04-25 23:48:08 -07001457 m.ProtoReflect().SetUnknown(raw)
Damien Neile6f060f2019-04-23 17:11:02 -07001458 }
1459}
1460
Damien Neilba23aa52018-12-07 14:38:17 -08001461func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001462 // TODO: Should ExtensionType.ValueOf accept []T instead of *[]T?
1463 t := reflect.TypeOf(value)
1464 if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
1465 v := reflect.New(t)
1466 v.Elem().Set(reflect.ValueOf(value))
1467 value = v.Interface()
1468 }
1469
Damien Neil4be2fb42018-12-17 11:16:16 -08001470 return func(m proto.Message) {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001471 xt := desc.Type
1472 m.ProtoReflect().Set(xt, xt.ValueOf(value))
Damien Neilba23aa52018-12-07 14:38:17 -08001473 }
1474}
Damien Neil61e93c72019-03-27 09:23:20 -07001475
1476func marshalText(m proto.Message) string {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001477 b, _ := prototext.MarshalOptions{Indent: "\t", AllowPartial: true}.Marshal(m)
Damien Neil61e93c72019-03-27 09:23:20 -07001478 return string(b)
1479}