blob: c32c94ce1c18d41485cf297a11eb8159c4feddab [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
Damien Neil5c5b5312019-05-14 12:44:37 -070012 "google.golang.org/protobuf/encoding/prototext"
Damien Neile89e6242019-05-13 23:55:40 -070013 "google.golang.org/protobuf/internal/encoding/pack"
Joe Tsaic51e2e02019-07-13 00:44:41 -070014 "google.golang.org/protobuf/internal/filedesc"
15 "google.golang.org/protobuf/internal/flags"
Damien Neile89e6242019-05-13 23:55:40 -070016 "google.golang.org/protobuf/proto"
Joe Tsaic51e2e02019-07-13 00:44:41 -070017 "google.golang.org/protobuf/reflect/protodesc"
18 "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsaic51e2e02019-07-13 00:44:41 -070019 "google.golang.org/protobuf/reflect/prototype"
Joe Tsai74615a32019-07-14 18:51:46 -070020 "google.golang.org/protobuf/runtime/protoiface"
Joe Tsaic51e2e02019-07-13 00:44:41 -070021 "google.golang.org/protobuf/runtime/protoimpl"
Joe Tsai19058432019-02-27 21:46:29 -080022
Damien Neilc37adef2019-04-01 13:49:56 -070023 legacypb "google.golang.org/protobuf/internal/testprotos/legacy"
24 legacy1pb "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v0.0.0-20160225-2fc053c5"
Damien Neile89e6242019-05-13 23:55:40 -070025 testpb "google.golang.org/protobuf/internal/testprotos/test"
26 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Joe Tsaic51e2e02019-07-13 00:44:41 -070027 "google.golang.org/protobuf/types/descriptorpb"
Damien Neilba23aa52018-12-07 14:38:17 -080028)
29
30type testProto struct {
Joe Tsai09cef322019-07-11 22:13:49 -070031 desc string
32 decodeTo []proto.Message
33 wire []byte
34 partial bool
Damien Neilba23aa52018-12-07 14:38:17 -080035}
36
37func TestDecode(t *testing.T) {
38 for _, test := range testProtos {
39 for _, want := range test.decodeTo {
40 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070041 opts := proto.UnmarshalOptions{
42 AllowPartial: test.partial,
43 }
Damien Neilba23aa52018-12-07 14:38:17 -080044 wire := append(([]byte)(nil), test.wire...)
Damien Neil4be2fb42018-12-17 11:16:16 -080045 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
Damien Neil96c229a2019-04-03 12:17:24 -070046 if err := opts.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070047 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080048 return
49 }
50
51 // Aliasing check: Modifying the original wire bytes shouldn't
52 // affect the unmarshaled message.
53 for i := range wire {
54 wire[i] = 0
55 }
Joe Tsaidb38ddd2019-05-07 15:14:40 -070056 if !proto.Equal(got, want) {
Damien Neil61e93c72019-03-27 09:23:20 -070057 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080058 }
59 })
60 }
61 }
62}
63
Damien Neil96c229a2019-04-03 12:17:24 -070064func TestDecodeRequiredFieldChecks(t *testing.T) {
65 for _, test := range testProtos {
66 if !test.partial {
67 continue
68 }
Damien Neil96c229a2019-04-03 12:17:24 -070069 for _, m := range test.decodeTo {
70 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
71 got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
72 if err := proto.Unmarshal(test.wire, got); err == nil {
73 t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got))
74 }
75 })
76 }
77 }
78}
79
Damien Neilbc310b52019-04-11 11:46:55 -070080func TestDecodeInvalidUTF8(t *testing.T) {
81 for _, test := range invalidUTF8TestProtos {
82 for _, want := range test.decodeTo {
83 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
84 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
85 err := proto.Unmarshal(test.wire, got)
Damien Neil8c86fc52019-06-19 09:28:29 -070086 if err == nil {
Damien Neilbc310b52019-04-11 11:46:55 -070087 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
88 }
Damien Neilbc310b52019-04-11 11:46:55 -070089 })
90 }
91 }
92}
93
Joe Tsaic51e2e02019-07-13 00:44:41 -070094func TestDecodeNoEnforceUTF8(t *testing.T) {
95 for _, test := range noEnforceUTF8TestProtos {
96 for _, want := range test.decodeTo {
97 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
98 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
99 err := proto.Unmarshal(test.wire, got)
100 switch {
101 case flags.Proto1Legacy && err != nil:
102 t.Errorf("Unmarshal returned unexpected error: %v\nMessage:\n%v", err, marshalText(want))
103 case !flags.Proto1Legacy && err == nil:
104 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
105 }
106 })
107 }
108 }
109}
110
Damien Neilba23aa52018-12-07 14:38:17 -0800111var testProtos = []testProto{
112 {
113 desc: "basic scalar types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800114 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700115 OptionalInt32: proto.Int32(1001),
116 OptionalInt64: proto.Int64(1002),
117 OptionalUint32: proto.Uint32(1003),
118 OptionalUint64: proto.Uint64(1004),
119 OptionalSint32: proto.Int32(1005),
120 OptionalSint64: proto.Int64(1006),
121 OptionalFixed32: proto.Uint32(1007),
122 OptionalFixed64: proto.Uint64(1008),
123 OptionalSfixed32: proto.Int32(1009),
124 OptionalSfixed64: proto.Int64(1010),
125 OptionalFloat: proto.Float32(1011.5),
126 OptionalDouble: proto.Float64(1012.5),
127 OptionalBool: proto.Bool(true),
128 OptionalString: proto.String("string"),
Damien Neilba23aa52018-12-07 14:38:17 -0800129 OptionalBytes: []byte("bytes"),
130 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
Damien Neil3b46ade2019-03-26 13:55:02 -0700131 }, &test3pb.TestAllTypes{
132 OptionalInt32: 1001,
133 OptionalInt64: 1002,
134 OptionalUint32: 1003,
135 OptionalUint64: 1004,
136 OptionalSint32: 1005,
137 OptionalSint64: 1006,
138 OptionalFixed32: 1007,
139 OptionalFixed64: 1008,
140 OptionalSfixed32: 1009,
141 OptionalSfixed64: 1010,
142 OptionalFloat: 1011.5,
143 OptionalDouble: 1012.5,
144 OptionalBool: true,
145 OptionalString: "string",
146 OptionalBytes: []byte("bytes"),
147 OptionalNestedEnum: test3pb.TestAllTypes_BAR,
Damien Neilba23aa52018-12-07 14:38:17 -0800148 }, build(
149 &testpb.TestAllExtensions{},
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700150 extend(testpb.E_OptionalInt32Extension, int32(1001)),
151 extend(testpb.E_OptionalInt64Extension, int64(1002)),
152 extend(testpb.E_OptionalUint32Extension, uint32(1003)),
153 extend(testpb.E_OptionalUint64Extension, uint64(1004)),
154 extend(testpb.E_OptionalSint32Extension, int32(1005)),
155 extend(testpb.E_OptionalSint64Extension, int64(1006)),
156 extend(testpb.E_OptionalFixed32Extension, uint32(1007)),
157 extend(testpb.E_OptionalFixed64Extension, uint64(1008)),
158 extend(testpb.E_OptionalSfixed32Extension, int32(1009)),
159 extend(testpb.E_OptionalSfixed64Extension, int64(1010)),
160 extend(testpb.E_OptionalFloatExtension, float32(1011.5)),
161 extend(testpb.E_OptionalDoubleExtension, float64(1012.5)),
162 extend(testpb.E_OptionalBoolExtension, bool(true)),
163 extend(testpb.E_OptionalStringExtension, string("string")),
Damien Neilba23aa52018-12-07 14:38:17 -0800164 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700165 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR),
Damien Neilba23aa52018-12-07 14:38:17 -0800166 )},
167 wire: pack.Message{
168 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
169 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
170 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
171 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
172 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
173 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
174 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
175 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
176 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
177 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
178 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
179 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
180 pack.Tag{13, pack.VarintType}, pack.Bool(true),
181 pack.Tag{14, pack.BytesType}, pack.String("string"),
182 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
183 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
184 }.Marshal(),
185 },
186 {
187 desc: "groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800188 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800189 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700190 A: proto.Int32(1017),
Damien Neilba23aa52018-12-07 14:38:17 -0800191 },
192 }, build(
193 &testpb.TestAllExtensions{},
194 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700195 A: proto.Int32(1017),
Damien Neilba23aa52018-12-07 14:38:17 -0800196 }),
197 )},
198 wire: pack.Message{
199 pack.Tag{16, pack.StartGroupType},
200 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
201 pack.Tag{16, pack.EndGroupType},
202 }.Marshal(),
203 },
204 {
205 desc: "groups (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800206 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800207 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700208 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800209 },
210 }, build(
211 &testpb.TestAllExtensions{},
212 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700213 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800214 }),
215 )},
216 wire: pack.Message{
217 pack.Tag{16, pack.StartGroupType},
218 pack.Tag{17, pack.VarintType}, pack.Varint(1),
219 pack.Tag{16, pack.EndGroupType},
220 pack.Tag{16, pack.StartGroupType},
221 pack.Tag{17, pack.VarintType}, pack.Varint(2),
222 pack.Tag{16, pack.EndGroupType},
223 }.Marshal(),
224 },
225 {
226 desc: "messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800227 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800228 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700229 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800230 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700231 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800232 },
233 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700234 }, &test3pb.TestAllTypes{
235 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
236 A: 42,
237 Corecursive: &test3pb.TestAllTypes{
238 OptionalInt32: 43,
239 },
240 },
Damien Neilba23aa52018-12-07 14:38:17 -0800241 }, build(
242 &testpb.TestAllExtensions{},
243 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700244 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800245 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700246 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800247 },
248 }),
249 )},
250 wire: pack.Message{
251 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
252 pack.Tag{1, pack.VarintType}, pack.Varint(42),
253 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
254 pack.Tag{1, pack.VarintType}, pack.Varint(43),
255 }),
256 }),
257 }.Marshal(),
258 },
259 {
260 desc: "messages (split across multiple tags)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800261 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800262 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700263 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800264 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700265 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800266 },
267 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700268 }, &test3pb.TestAllTypes{
269 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
270 A: 42,
271 Corecursive: &test3pb.TestAllTypes{
272 OptionalInt32: 43,
273 },
274 },
Damien Neilba23aa52018-12-07 14:38:17 -0800275 }, build(
276 &testpb.TestAllExtensions{},
277 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700278 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800279 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700280 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800281 },
282 }),
283 )},
284 wire: pack.Message{
285 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
286 pack.Tag{1, pack.VarintType}, pack.Varint(42),
287 }),
288 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
289 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
290 pack.Tag{1, pack.VarintType}, pack.Varint(43),
291 }),
292 }),
293 }.Marshal(),
294 },
295 {
296 desc: "messages (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800297 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800298 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700299 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800300 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700301 }, &test3pb.TestAllTypes{
302 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
303 A: 2,
304 },
Damien Neilba23aa52018-12-07 14:38:17 -0800305 }, build(
306 &testpb.TestAllExtensions{},
307 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700308 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800309 }),
310 )},
311 wire: pack.Message{
312 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
313 pack.Tag{1, pack.VarintType}, pack.Varint(1),
314 }),
315 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
316 pack.Tag{1, pack.VarintType}, pack.Varint(2),
317 }),
318 }.Marshal(),
319 },
320 {
321 desc: "basic repeated types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800322 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800323 RepeatedInt32: []int32{1001, 2001},
324 RepeatedInt64: []int64{1002, 2002},
325 RepeatedUint32: []uint32{1003, 2003},
326 RepeatedUint64: []uint64{1004, 2004},
327 RepeatedSint32: []int32{1005, 2005},
328 RepeatedSint64: []int64{1006, 2006},
329 RepeatedFixed32: []uint32{1007, 2007},
330 RepeatedFixed64: []uint64{1008, 2008},
331 RepeatedSfixed32: []int32{1009, 2009},
332 RepeatedSfixed64: []int64{1010, 2010},
333 RepeatedFloat: []float32{1011.5, 2011.5},
334 RepeatedDouble: []float64{1012.5, 2012.5},
335 RepeatedBool: []bool{true, false},
336 RepeatedString: []string{"foo", "bar"},
337 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
338 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
339 testpb.TestAllTypes_FOO,
340 testpb.TestAllTypes_BAR,
341 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700342 }, &test3pb.TestAllTypes{
343 RepeatedInt32: []int32{1001, 2001},
344 RepeatedInt64: []int64{1002, 2002},
345 RepeatedUint32: []uint32{1003, 2003},
346 RepeatedUint64: []uint64{1004, 2004},
347 RepeatedSint32: []int32{1005, 2005},
348 RepeatedSint64: []int64{1006, 2006},
349 RepeatedFixed32: []uint32{1007, 2007},
350 RepeatedFixed64: []uint64{1008, 2008},
351 RepeatedSfixed32: []int32{1009, 2009},
352 RepeatedSfixed64: []int64{1010, 2010},
353 RepeatedFloat: []float32{1011.5, 2011.5},
354 RepeatedDouble: []float64{1012.5, 2012.5},
355 RepeatedBool: []bool{true, false},
356 RepeatedString: []string{"foo", "bar"},
357 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
358 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
359 test3pb.TestAllTypes_FOO,
360 test3pb.TestAllTypes_BAR,
361 },
Damien Neilba23aa52018-12-07 14:38:17 -0800362 }, build(
363 &testpb.TestAllExtensions{},
364 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
365 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
366 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
367 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
368 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
369 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
370 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
371 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
372 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
373 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
374 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
375 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
376 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
377 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
378 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
379 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
380 testpb.TestAllTypes_FOO,
381 testpb.TestAllTypes_BAR,
382 }),
383 )},
384 wire: pack.Message{
385 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
386 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
387 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
388 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
389 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
390 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
391 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
392 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
393 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
394 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
395 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
396 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
397 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
398 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
399 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
400 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
401 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
402 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
403 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
404 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
405 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
406 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
407 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
408 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
409 pack.Tag{43, pack.VarintType}, pack.Bool(true),
410 pack.Tag{43, pack.VarintType}, pack.Bool(false),
411 pack.Tag{44, pack.BytesType}, pack.String("foo"),
412 pack.Tag{44, pack.BytesType}, pack.String("bar"),
413 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
414 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
415 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
416 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
417 }.Marshal(),
418 },
419 {
420 desc: "basic repeated types (packed encoding)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800421 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800422 RepeatedInt32: []int32{1001, 2001},
423 RepeatedInt64: []int64{1002, 2002},
424 RepeatedUint32: []uint32{1003, 2003},
425 RepeatedUint64: []uint64{1004, 2004},
426 RepeatedSint32: []int32{1005, 2005},
427 RepeatedSint64: []int64{1006, 2006},
428 RepeatedFixed32: []uint32{1007, 2007},
429 RepeatedFixed64: []uint64{1008, 2008},
430 RepeatedSfixed32: []int32{1009, 2009},
431 RepeatedSfixed64: []int64{1010, 2010},
432 RepeatedFloat: []float32{1011.5, 2011.5},
433 RepeatedDouble: []float64{1012.5, 2012.5},
434 RepeatedBool: []bool{true, false},
435 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
436 testpb.TestAllTypes_FOO,
437 testpb.TestAllTypes_BAR,
438 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700439 }, &test3pb.TestAllTypes{
440 RepeatedInt32: []int32{1001, 2001},
441 RepeatedInt64: []int64{1002, 2002},
442 RepeatedUint32: []uint32{1003, 2003},
443 RepeatedUint64: []uint64{1004, 2004},
444 RepeatedSint32: []int32{1005, 2005},
445 RepeatedSint64: []int64{1006, 2006},
446 RepeatedFixed32: []uint32{1007, 2007},
447 RepeatedFixed64: []uint64{1008, 2008},
448 RepeatedSfixed32: []int32{1009, 2009},
449 RepeatedSfixed64: []int64{1010, 2010},
450 RepeatedFloat: []float32{1011.5, 2011.5},
451 RepeatedDouble: []float64{1012.5, 2012.5},
452 RepeatedBool: []bool{true, false},
453 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
454 test3pb.TestAllTypes_FOO,
455 test3pb.TestAllTypes_BAR,
456 },
Damien Neilba23aa52018-12-07 14:38:17 -0800457 }, build(
458 &testpb.TestAllExtensions{},
459 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
460 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
461 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
462 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
463 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
464 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
465 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
466 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
467 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
468 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
469 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
470 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
471 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
472 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
473 testpb.TestAllTypes_FOO,
474 testpb.TestAllTypes_BAR,
475 }),
476 )},
477 wire: pack.Message{
478 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
479 pack.Varint(1001), pack.Varint(2001),
480 },
481 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
482 pack.Varint(1002), pack.Varint(2002),
483 },
484 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
485 pack.Uvarint(1003), pack.Uvarint(2003),
486 },
487 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
488 pack.Uvarint(1004), pack.Uvarint(2004),
489 },
490 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
491 pack.Svarint(1005), pack.Svarint(2005),
492 },
493 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
494 pack.Svarint(1006), pack.Svarint(2006),
495 },
496 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
497 pack.Uint32(1007), pack.Uint32(2007),
498 },
499 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
500 pack.Uint64(1008), pack.Uint64(2008),
501 },
502 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
503 pack.Int32(1009), pack.Int32(2009),
504 },
505 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
506 pack.Int64(1010), pack.Int64(2010),
507 },
508 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
509 pack.Float32(1011.5), pack.Float32(2011.5),
510 },
511 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
512 pack.Float64(1012.5), pack.Float64(2012.5),
513 },
514 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
515 pack.Bool(true), pack.Bool(false),
516 },
517 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
518 pack.Varint(int(testpb.TestAllTypes_FOO)),
519 pack.Varint(int(testpb.TestAllTypes_BAR)),
520 },
521 }.Marshal(),
522 },
523 {
Damien Neil7492a092019-07-10 15:23:29 -0700524 desc: "packed repeated types",
525 decodeTo: []proto.Message{&testpb.TestPackedTypes{
526 PackedInt32: []int32{1001, 2001},
527 PackedInt64: []int64{1002, 2002},
528 PackedUint32: []uint32{1003, 2003},
529 PackedUint64: []uint64{1004, 2004},
530 PackedSint32: []int32{1005, 2005},
531 PackedSint64: []int64{1006, 2006},
532 PackedFixed32: []uint32{1007, 2007},
533 PackedFixed64: []uint64{1008, 2008},
534 PackedSfixed32: []int32{1009, 2009},
535 PackedSfixed64: []int64{1010, 2010},
536 PackedFloat: []float32{1011.5, 2011.5},
537 PackedDouble: []float64{1012.5, 2012.5},
538 PackedBool: []bool{true, false},
539 PackedEnum: []testpb.ForeignEnum{
540 testpb.ForeignEnum_FOREIGN_FOO,
541 testpb.ForeignEnum_FOREIGN_BAR,
542 },
543 }, build(
544 &testpb.TestPackedExtensions{},
545 extend(testpb.E_PackedInt32Extension, []int32{1001, 2001}),
546 extend(testpb.E_PackedInt64Extension, []int64{1002, 2002}),
547 extend(testpb.E_PackedUint32Extension, []uint32{1003, 2003}),
548 extend(testpb.E_PackedUint64Extension, []uint64{1004, 2004}),
549 extend(testpb.E_PackedSint32Extension, []int32{1005, 2005}),
550 extend(testpb.E_PackedSint64Extension, []int64{1006, 2006}),
551 extend(testpb.E_PackedFixed32Extension, []uint32{1007, 2007}),
552 extend(testpb.E_PackedFixed64Extension, []uint64{1008, 2008}),
553 extend(testpb.E_PackedSfixed32Extension, []int32{1009, 2009}),
554 extend(testpb.E_PackedSfixed64Extension, []int64{1010, 2010}),
555 extend(testpb.E_PackedFloatExtension, []float32{1011.5, 2011.5}),
556 extend(testpb.E_PackedDoubleExtension, []float64{1012.5, 2012.5}),
557 extend(testpb.E_PackedBoolExtension, []bool{true, false}),
558 extend(testpb.E_PackedEnumExtension, []testpb.ForeignEnum{
559 testpb.ForeignEnum_FOREIGN_FOO,
560 testpb.ForeignEnum_FOREIGN_BAR,
561 }),
562 )},
563 wire: pack.Message{
564 pack.Tag{90, pack.BytesType}, pack.LengthPrefix{
565 pack.Varint(1001), pack.Varint(2001),
566 },
567 pack.Tag{91, pack.BytesType}, pack.LengthPrefix{
568 pack.Varint(1002), pack.Varint(2002),
569 },
570 pack.Tag{92, pack.BytesType}, pack.LengthPrefix{
571 pack.Uvarint(1003), pack.Uvarint(2003),
572 },
573 pack.Tag{93, pack.BytesType}, pack.LengthPrefix{
574 pack.Uvarint(1004), pack.Uvarint(2004),
575 },
576 pack.Tag{94, pack.BytesType}, pack.LengthPrefix{
577 pack.Svarint(1005), pack.Svarint(2005),
578 },
579 pack.Tag{95, pack.BytesType}, pack.LengthPrefix{
580 pack.Svarint(1006), pack.Svarint(2006),
581 },
582 pack.Tag{96, pack.BytesType}, pack.LengthPrefix{
583 pack.Uint32(1007), pack.Uint32(2007),
584 },
585 pack.Tag{97, pack.BytesType}, pack.LengthPrefix{
586 pack.Uint64(1008), pack.Uint64(2008),
587 },
588 pack.Tag{98, pack.BytesType}, pack.LengthPrefix{
589 pack.Int32(1009), pack.Int32(2009),
590 },
591 pack.Tag{99, pack.BytesType}, pack.LengthPrefix{
592 pack.Int64(1010), pack.Int64(2010),
593 },
594 pack.Tag{100, pack.BytesType}, pack.LengthPrefix{
595 pack.Float32(1011.5), pack.Float32(2011.5),
596 },
597 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{
598 pack.Float64(1012.5), pack.Float64(2012.5),
599 },
600 pack.Tag{102, pack.BytesType}, pack.LengthPrefix{
601 pack.Bool(true), pack.Bool(false),
602 },
603 pack.Tag{103, pack.BytesType}, pack.LengthPrefix{
604 pack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
605 pack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
606 },
607 }.Marshal(),
608 },
609 {
Damien Neilba23aa52018-12-07 14:38:17 -0800610 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800611 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800612 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700613 {A: proto.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700614 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700615 {A: proto.Int32(2)},
Damien Neilba23aa52018-12-07 14:38:17 -0800616 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700617 }, &test3pb.TestAllTypes{
618 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
619 {A: 1},
Damien Neilc37adef2019-04-01 13:49:56 -0700620 nil,
Damien Neil3b46ade2019-03-26 13:55:02 -0700621 {A: 2},
622 },
Damien Neilba23aa52018-12-07 14:38:17 -0800623 }, build(
624 &testpb.TestAllExtensions{},
625 extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700626 {A: proto.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700627 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700628 {A: proto.Int32(2)},
Damien Neilba23aa52018-12-07 14:38:17 -0800629 }),
630 )},
631 wire: pack.Message{
632 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
633 pack.Tag{1, pack.VarintType}, pack.Varint(1),
634 }),
Damien Neilc37adef2019-04-01 13:49:56 -0700635 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
Damien Neilba23aa52018-12-07 14:38:17 -0800636 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
637 pack.Tag{1, pack.VarintType}, pack.Varint(2),
638 }),
639 }.Marshal(),
640 },
641 {
642 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800643 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800644 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700645 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700646 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700647 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800648 },
649 }, build(
650 &testpb.TestAllExtensions{},
651 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700652 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700653 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700654 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800655 }),
656 )},
657 wire: pack.Message{
658 pack.Tag{46, pack.StartGroupType},
659 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
660 pack.Tag{46, pack.EndGroupType},
661 pack.Tag{46, pack.StartGroupType},
Damien Neilc37adef2019-04-01 13:49:56 -0700662 pack.Tag{46, pack.EndGroupType},
663 pack.Tag{46, pack.StartGroupType},
Damien Neilba23aa52018-12-07 14:38:17 -0800664 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
665 pack.Tag{46, pack.EndGroupType},
666 }.Marshal(),
667 },
668 {
669 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800670 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800671 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
672 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
673 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
674 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
675 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
676 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
677 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
678 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
679 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
680 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
681 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
682 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
683 MapBoolBool: map[bool]bool{true: false, false: true},
684 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
685 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
686 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700687 "71.1.key": {A: proto.Int32(1171)},
688 "71.2.key": {A: proto.Int32(2171)},
Damien Neilba23aa52018-12-07 14:38:17 -0800689 },
690 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
691 "73.1.key": testpb.TestAllTypes_FOO,
692 "73.2.key": testpb.TestAllTypes_BAR,
693 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700694 }, &test3pb.TestAllTypes{
695 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
696 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
697 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
698 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
699 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
700 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
701 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
702 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
703 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
704 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
705 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
706 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
707 MapBoolBool: map[bool]bool{true: false, false: true},
708 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
709 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
710 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
711 "71.1.key": {A: 1171},
712 "71.2.key": {A: 2171},
713 },
714 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
715 "73.1.key": test3pb.TestAllTypes_FOO,
716 "73.2.key": test3pb.TestAllTypes_BAR,
717 },
Damien Neilba23aa52018-12-07 14:38:17 -0800718 }},
719 wire: pack.Message{
720 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
721 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
722 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
723 }),
724 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
725 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
726 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
727 }),
728 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
729 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
730 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
731 }),
732 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
733 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
734 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
735 }),
736 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
737 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
738 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
739 }),
740 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
741 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
742 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
743 }),
744 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
745 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
746 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
747 }),
748 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
749 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
750 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
751 }),
752 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
753 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
754 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
755 }),
756 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
757 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
758 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
759 }),
760 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
761 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
762 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
763 }),
764 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
765 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
766 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
767 }),
768 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
769 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
770 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
771 }),
772 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
773 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
774 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
775 }),
776 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
777 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
778 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
779 }),
780 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
781 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
782 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
783 }),
784 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
785 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
786 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
787 }),
788 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
789 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
790 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
791 }),
792 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
793 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
794 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
795 }),
796 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
797 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
798 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
799 }),
800 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
801 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
802 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
803 }),
804 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
805 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
806 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
807 }),
808 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
809 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
810 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
811 }),
812 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
813 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
814 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
815 }),
816 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
817 pack.Tag{1, pack.VarintType}, pack.Bool(true),
818 pack.Tag{2, pack.VarintType}, pack.Bool(false),
819 }),
820 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
821 pack.Tag{1, pack.VarintType}, pack.Bool(false),
822 pack.Tag{2, pack.VarintType}, pack.Bool(true),
823 }),
824 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
825 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
826 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
827 }),
828 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
829 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
830 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
831 }),
832 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
833 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
834 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
835 }),
836 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
837 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
838 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
839 }),
840 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
841 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
842 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
843 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
844 }),
845 }),
846 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
847 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
848 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
849 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
850 }),
851 }),
852 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
853 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
854 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
855 }),
856 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
857 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
858 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
859 }),
860 }.Marshal(),
861 },
862 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700863 desc: "oneof (uint32)",
864 decodeTo: []proto.Message{
865 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
866 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
867 },
868 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800869 },
870 {
871 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700872 decodeTo: []proto.Message{
873 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700874 &testpb.TestAllTypes_NestedMessage{A: proto.Int32(1112)},
Damien Neil3b46ade2019-03-26 13:55:02 -0700875 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
876 &test3pb.TestAllTypes_NestedMessage{A: 1112},
877 }},
878 },
Damien Neilba23aa52018-12-07 14:38:17 -0800879 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
880 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
881 })}.Marshal(),
882 },
883 {
Damien Neilc37adef2019-04-01 13:49:56 -0700884 desc: "oneof (empty message)",
885 decodeTo: []proto.Message{
886 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
887 &testpb.TestAllTypes_NestedMessage{},
888 }},
889 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
890 &test3pb.TestAllTypes_NestedMessage{},
891 }},
892 },
893 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
894 },
895 {
Joe Tsai6c286742019-07-11 23:15:05 -0700896 desc: "oneof (merged message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700897 decodeTo: []proto.Message{
898 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
899 &testpb.TestAllTypes_NestedMessage{
Joe Tsai0f81b382019-07-10 23:14:31 -0700900 A: proto.Int32(1),
Damien Neil3b46ade2019-03-26 13:55:02 -0700901 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700902 OptionalInt32: proto.Int32(43),
Damien Neil3b46ade2019-03-26 13:55:02 -0700903 },
Damien Neilba23aa52018-12-07 14:38:17 -0800904 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700905 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
906 &test3pb.TestAllTypes_NestedMessage{
Joe Tsai6c286742019-07-11 23:15:05 -0700907 A: 1,
Damien Neil3b46ade2019-03-26 13:55:02 -0700908 Corecursive: &test3pb.TestAllTypes{
909 OptionalInt32: 43,
910 },
911 },
912 }}},
Damien Neilba23aa52018-12-07 14:38:17 -0800913 wire: pack.Message{
914 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
915 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
916 }),
917 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
918 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
919 pack.Tag{1, pack.VarintType}, pack.Varint(43),
920 }),
921 }),
922 }.Marshal(),
923 },
924 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700925 desc: "oneof (string)",
926 decodeTo: []proto.Message{
927 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
928 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
929 },
930 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800931 },
932 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700933 desc: "oneof (bytes)",
934 decodeTo: []proto.Message{
935 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
936 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
937 },
938 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800939 },
940 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700941 desc: "oneof (bool)",
942 decodeTo: []proto.Message{
943 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
944 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
945 },
946 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800947 },
948 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700949 desc: "oneof (uint64)",
950 decodeTo: []proto.Message{
951 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
952 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
953 },
954 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800955 },
956 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700957 desc: "oneof (float)",
958 decodeTo: []proto.Message{
959 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
960 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
961 },
962 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800963 },
964 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700965 desc: "oneof (double)",
966 decodeTo: []proto.Message{
967 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
968 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
969 },
970 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800971 },
972 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700973 desc: "oneof (enum)",
974 decodeTo: []proto.Message{
975 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
976 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
977 },
978 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800979 },
980 {
Damien Neilc37adef2019-04-01 13:49:56 -0700981 desc: "oneof (zero)",
982 decodeTo: []proto.Message{
983 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}},
984 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}},
985 },
986 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
987 },
988 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700989 desc: "oneof (overridden value)",
990 decodeTo: []proto.Message{
991 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
992 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
993 },
Damien Neilba23aa52018-12-07 14:38:17 -0800994 wire: pack.Message{
995 pack.Tag{111, pack.VarintType}, pack.Varint(1),
996 pack.Tag{116, pack.VarintType}, pack.Varint(2),
997 }.Marshal(),
998 },
999 // TODO: More unknown field tests for ordering, repeated fields, etc.
1000 //
1001 // It is currently impossible to produce results that the v1 Equal
1002 // considers equivalent to those of the v1 decoder. Figure out if
1003 // that's a problem or not.
1004 {
1005 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -08001006 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001007 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001008 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001009 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1010 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001011 ), build(
1012 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001013 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001014 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1015 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001016 )},
1017 wire: pack.Message{
1018 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1019 }.Marshal(),
1020 },
1021 {
1022 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001023 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001024 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001025 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001026 pack.Tag{1, pack.BytesType}, pack.String("string"),
1027 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001028 ), build(
1029 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001030 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001031 pack.Tag{1, pack.BytesType}, pack.String("string"),
1032 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001033 )},
1034 wire: pack.Message{
1035 pack.Tag{1, pack.BytesType}, pack.String("string"),
1036 }.Marshal(),
1037 },
1038 {
1039 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001040 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001041 &testpb.TestAllTypes{
1042 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -07001043 }, &test3pb.TestAllTypes{
1044 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -08001045 },
1046 },
1047 wire: pack.Message{
1048 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
1049 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1050 pack.Tag{2, pack.BytesType}, pack.String("string"),
1051 }),
1052 }.Marshal(),
1053 },
Damien Neil96c229a2019-04-03 12:17:24 -07001054 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001055 desc: "required field in nil message unset",
1056 partial: true,
1057 decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
1058 },
1059 {
Damien Neil96c229a2019-04-03 12:17:24 -07001060 desc: "required field unset",
1061 partial: true,
1062 decodeTo: []proto.Message{&testpb.TestRequired{}},
1063 },
1064 {
1065 desc: "required field set",
1066 decodeTo: []proto.Message{&testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001067 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001068 }},
1069 wire: pack.Message{
1070 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1071 }.Marshal(),
1072 },
1073 {
1074 desc: "required field in optional message unset",
1075 partial: true,
1076 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1077 OptionalMessage: &testpb.TestRequired{},
1078 }},
1079 wire: pack.Message{
1080 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1081 }.Marshal(),
1082 },
1083 {
1084 desc: "required field in optional message set",
1085 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1086 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001087 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001088 },
1089 }},
1090 wire: pack.Message{
1091 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1092 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1093 }),
1094 }.Marshal(),
1095 },
Damien Neil4686e232019-04-05 13:31:40 -07001096 {
1097 desc: "required field in optional message set (split across multiple tags)",
1098 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1099 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001100 RequiredField: proto.Int32(1),
Damien Neil4686e232019-04-05 13:31:40 -07001101 },
1102 }},
1103 wire: pack.Message{
1104 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1105 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1106 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1107 }),
1108 }.Marshal(),
1109 },
Damien Neil96c229a2019-04-03 12:17:24 -07001110 {
1111 desc: "required field in repeated message unset",
1112 partial: true,
1113 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1114 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001115 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001116 {},
1117 },
1118 }},
1119 wire: pack.Message{
1120 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1121 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1122 }),
1123 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1124 }.Marshal(),
1125 },
1126 {
1127 desc: "required field in repeated message set",
1128 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1129 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001130 {RequiredField: proto.Int32(1)},
1131 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001132 },
1133 }},
1134 wire: pack.Message{
1135 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1136 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1137 }),
1138 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1139 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1140 }),
1141 }.Marshal(),
1142 },
1143 {
1144 desc: "required field in map message unset",
1145 partial: true,
1146 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1147 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001148 1: {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001149 2: {},
1150 },
1151 }},
1152 wire: pack.Message{
1153 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1154 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1155 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1156 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1157 }),
1158 }),
1159 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1160 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1161 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1162 }),
1163 }.Marshal(),
1164 },
1165 {
1166 desc: "required field in map message set",
1167 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1168 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001169 1: {RequiredField: proto.Int32(1)},
1170 2: {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001171 },
1172 }},
1173 wire: pack.Message{
1174 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1175 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1176 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1177 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1178 }),
1179 }),
1180 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1181 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1182 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1183 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1184 }),
1185 }),
1186 }.Marshal(),
1187 },
1188 {
1189 desc: "required field in optional group unset",
1190 partial: true,
1191 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1192 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1193 }},
1194 wire: pack.Message{
1195 pack.Tag{1, pack.StartGroupType},
1196 pack.Tag{1, pack.EndGroupType},
1197 }.Marshal(),
1198 },
1199 {
1200 desc: "required field in optional group set",
1201 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1202 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001203 A: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001204 },
1205 }},
1206 wire: pack.Message{
1207 pack.Tag{1, pack.StartGroupType},
1208 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1209 pack.Tag{1, pack.EndGroupType},
1210 }.Marshal(),
1211 },
1212 {
1213 desc: "required field in repeated group unset",
1214 partial: true,
1215 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1216 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001217 {A: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001218 {},
1219 },
1220 }},
1221 wire: pack.Message{
1222 pack.Tag{3, pack.StartGroupType},
1223 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1224 pack.Tag{3, pack.EndGroupType},
1225 pack.Tag{3, pack.StartGroupType},
1226 pack.Tag{3, pack.EndGroupType},
1227 }.Marshal(),
1228 },
1229 {
1230 desc: "required field in repeated group set",
1231 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1232 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001233 {A: proto.Int32(1)},
1234 {A: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001235 },
1236 }},
1237 wire: pack.Message{
1238 pack.Tag{3, pack.StartGroupType},
1239 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1240 pack.Tag{3, pack.EndGroupType},
1241 pack.Tag{3, pack.StartGroupType},
1242 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1243 pack.Tag{3, pack.EndGroupType},
1244 }.Marshal(),
1245 },
1246 {
Damien Neil5322bdb2019-04-09 15:57:05 -07001247 desc: "required field in oneof message unset",
1248 partial: true,
1249 decodeTo: []proto.Message{
1250 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1251 &testpb.TestRequired{},
1252 }},
1253 },
1254 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1255 },
1256 {
1257 desc: "required field in oneof message set",
1258 decodeTo: []proto.Message{
1259 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1260 &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001261 RequiredField: proto.Int32(1),
Damien Neil5322bdb2019-04-09 15:57:05 -07001262 },
1263 }},
1264 },
1265 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
1266 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1267 })}.Marshal(),
1268 },
1269 {
Joe Tsai09cef322019-07-11 22:13:49 -07001270 desc: "required field in extension message unset",
1271 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001272 decodeTo: []proto.Message{build(
1273 &testpb.TestAllExtensions{},
1274 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1275 )},
1276 wire: pack.Message{
1277 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1278 }.Marshal(),
1279 },
1280 {
1281 desc: "required field in extension message set",
1282 decodeTo: []proto.Message{build(
1283 &testpb.TestAllExtensions{},
1284 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001285 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001286 }),
1287 )},
1288 wire: pack.Message{
1289 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1290 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1291 }),
1292 }.Marshal(),
1293 },
1294 {
Joe Tsai09cef322019-07-11 22:13:49 -07001295 desc: "required field in repeated extension message unset",
1296 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001297 decodeTo: []proto.Message{build(
1298 &testpb.TestAllExtensions{},
1299 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001300 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001301 {},
1302 }),
1303 )},
1304 wire: pack.Message{
1305 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1306 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1307 }),
1308 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1309 }.Marshal(),
1310 },
1311 {
1312 desc: "required field in repeated extension message set",
1313 decodeTo: []proto.Message{build(
1314 &testpb.TestAllExtensions{},
1315 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001316 {RequiredField: proto.Int32(1)},
1317 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001318 }),
1319 )},
1320 wire: pack.Message{
1321 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1322 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1323 }),
1324 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1325 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1326 }),
1327 }.Marshal(),
1328 },
Damien Neilc37adef2019-04-01 13:49:56 -07001329 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001330 desc: "nil messages",
1331 decodeTo: []proto.Message{
1332 (*testpb.TestAllTypes)(nil),
1333 (*test3pb.TestAllTypes)(nil),
1334 (*testpb.TestAllExtensions)(nil),
1335 },
1336 },
1337 {
Damien Neilc37adef2019-04-01 13:49:56 -07001338 desc: "legacy",
1339 partial: true,
1340 decodeTo: []proto.Message{
1341 &legacypb.Legacy{
1342 F1: &legacy1pb.Message{
Damien Neila8a2cea2019-07-10 16:17:16 -07001343 OptionalInt32: proto.Int32(1),
Damien Neilc37adef2019-04-01 13:49:56 -07001344 OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
1345 OptionalChildMessage: &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001346 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001347 },
1348 Optionalgroup: &legacy1pb.Message_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001349 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001350 },
1351 RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001352 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001353 },
1354 Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001355 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001356 },
1357 MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001358 true: {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001359 },
1360 OneofUnion: &legacy1pb.Message_OneofChildMessage{
1361 &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001362 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001363 },
1364 },
1365 },
1366 },
1367 },
1368 wire: pack.Message{
1369 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1370 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1371 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1372 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1373 pack.Tag{1, pack.BytesType}, pack.String("x"),
1374 }),
1375 pack.Tag{120, pack.StartGroupType},
1376 pack.Tag{1, pack.BytesType}, pack.String("x"),
1377 pack.Tag{120, pack.EndGroupType},
1378 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1379 pack.Tag{1, pack.BytesType}, pack.String("x"),
1380 }),
1381 pack.Tag{520, pack.StartGroupType},
1382 pack.Tag{1, pack.BytesType}, pack.String("x"),
1383 pack.Tag{520, pack.EndGroupType},
1384 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1385 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1386 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1387 pack.Tag{1, pack.BytesType}, pack.String("x"),
1388 }),
1389 }),
1390 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1391 pack.Tag{1, pack.BytesType}, pack.String("x"),
1392 }),
1393 }),
1394 }.Marshal(),
1395 },
Damien Neilba23aa52018-12-07 14:38:17 -08001396}
1397
Damien Neilbc310b52019-04-11 11:46:55 -07001398var invalidUTF8TestProtos = []testProto{
1399 {
1400 desc: "invalid UTF-8 in optional string field",
1401 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1402 OptionalString: "abc\xff",
1403 }},
1404 wire: pack.Message{
1405 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1406 }.Marshal(),
1407 },
1408 {
1409 desc: "invalid UTF-8 in repeated string field",
1410 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1411 RepeatedString: []string{"foo", "abc\xff"},
1412 }},
1413 wire: pack.Message{
1414 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1415 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1416 }.Marshal(),
1417 },
1418 {
1419 desc: "invalid UTF-8 in nested message",
1420 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1421 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1422 Corecursive: &test3pb.TestAllTypes{
1423 OptionalString: "abc\xff",
1424 },
1425 },
1426 }},
1427 wire: pack.Message{
1428 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1429 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1430 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1431 }),
1432 }),
1433 }.Marshal(),
1434 },
1435 {
Damien Neilc37adef2019-04-01 13:49:56 -07001436 desc: "invalid UTF-8 in oneof field",
1437 decodeTo: []proto.Message{
1438 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
1439 },
1440 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1441 },
1442 {
Damien Neilbc310b52019-04-11 11:46:55 -07001443 desc: "invalid UTF-8 in map key",
1444 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1445 MapStringString: map[string]string{"key\xff": "val"},
1446 }},
1447 wire: pack.Message{
1448 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1449 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1450 pack.Tag{2, pack.BytesType}, pack.String("val"),
1451 }),
1452 }.Marshal(),
1453 },
1454 {
1455 desc: "invalid UTF-8 in map value",
1456 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1457 MapStringString: map[string]string{"key": "val\xff"},
1458 }},
1459 wire: pack.Message{
1460 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1461 pack.Tag{1, pack.BytesType}, pack.String("key"),
1462 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1463 }),
1464 }.Marshal(),
1465 },
1466}
1467
Joe Tsaic51e2e02019-07-13 00:44:41 -07001468var noEnforceUTF8TestProtos = []testProto{
1469 {
1470 desc: "invalid UTF-8 in optional string field",
1471 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1472 OptionalString: string("abc\xff"),
1473 }},
1474 wire: pack.Message{
1475 pack.Tag{1, pack.BytesType}, pack.String("abc\xff"),
1476 }.Marshal(),
1477 },
1478 {
1479 desc: "invalid UTF-8 in optional string field of Go bytes",
1480 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1481 OptionalBytes: []byte("abc\xff"),
1482 }},
1483 wire: pack.Message{
1484 pack.Tag{2, pack.BytesType}, pack.String("abc\xff"),
1485 }.Marshal(),
1486 },
1487 {
1488 desc: "invalid UTF-8 in repeated string field",
1489 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1490 RepeatedString: []string{string("foo"), string("abc\xff")},
1491 }},
1492 wire: pack.Message{
1493 pack.Tag{3, pack.BytesType}, pack.String("foo"),
1494 pack.Tag{3, pack.BytesType}, pack.String("abc\xff"),
1495 }.Marshal(),
1496 },
1497 {
1498 desc: "invalid UTF-8 in repeated string field of Go bytes",
1499 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1500 RepeatedBytes: [][]byte{[]byte("foo"), []byte("abc\xff")},
1501 }},
1502 wire: pack.Message{
1503 pack.Tag{4, pack.BytesType}, pack.String("foo"),
1504 pack.Tag{4, pack.BytesType}, pack.String("abc\xff"),
1505 }.Marshal(),
1506 },
1507 {
1508 desc: "invalid UTF-8 in oneof string field",
1509 decodeTo: []proto.Message{
1510 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofString{string("abc\xff")}},
1511 },
1512 wire: pack.Message{pack.Tag{5, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1513 },
1514 {
1515 desc: "invalid UTF-8 in oneof string field of Go bytes",
1516 decodeTo: []proto.Message{
1517 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofBytes{[]byte("abc\xff")}},
1518 },
1519 wire: pack.Message{pack.Tag{6, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1520 },
1521}
1522
1523type TestNoEnforceUTF8 struct {
1524 OptionalString string `protobuf:"bytes,1,opt,name=optional_string"`
1525 OptionalBytes []byte `protobuf:"bytes,2,opt,name=optional_bytes"`
1526 RepeatedString []string `protobuf:"bytes,3,rep,name=repeated_string"`
1527 RepeatedBytes [][]byte `protobuf:"bytes,4,rep,name=repeated_bytes"`
1528 OneofField isOneofField `protobuf_oneof:"oneof_field"`
1529}
1530
1531type isOneofField interface{ isOneofField() }
1532
1533type TestNoEnforceUTF8_OneofString struct {
1534 OneofString string `protobuf:"bytes,5,opt,name=oneof_string,oneof"`
1535}
1536type TestNoEnforceUTF8_OneofBytes struct {
1537 OneofBytes []byte `protobuf:"bytes,6,opt,name=oneof_bytes,oneof"`
1538}
1539
1540func (*TestNoEnforceUTF8_OneofString) isOneofField() {}
1541func (*TestNoEnforceUTF8_OneofBytes) isOneofField() {}
1542
Joe Tsai74615a32019-07-14 18:51:46 -07001543func (m *TestNoEnforceUTF8) ProtoReflect() protoreflect.Message {
Joe Tsaic51e2e02019-07-13 00:44:41 -07001544 return messageInfo_TestNoEnforceUTF8.MessageOf(m)
1545}
1546
1547var messageInfo_TestNoEnforceUTF8 = protoimpl.MessageInfo{
1548 GoType: reflect.TypeOf((*TestNoEnforceUTF8)(nil)),
1549 PBType: &prototype.Message{
1550 MessageDescriptor: func() protoreflect.MessageDescriptor {
1551 pb := new(descriptorpb.FileDescriptorProto)
1552 if err := prototext.Unmarshal([]byte(`
1553 syntax: "proto3"
1554 name: "test.proto"
1555 message_type: [{
1556 name: "TestNoEnforceUTF8"
1557 field: [
1558 {name:"optional_string" number:1 label:LABEL_OPTIONAL type:TYPE_STRING},
1559 {name:"optional_bytes" number:2 label:LABEL_OPTIONAL type:TYPE_STRING},
1560 {name:"repeated_string" number:3 label:LABEL_REPEATED type:TYPE_STRING},
1561 {name:"repeated_bytes" number:4 label:LABEL_REPEATED type:TYPE_STRING},
1562 {name:"oneof_string" number:5 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0},
1563 {name:"oneof_bytes" number:6 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0}
1564 ]
1565 oneof_decl: [{name:"oneof_field"}]
1566 }]
1567 `), pb); err != nil {
1568 panic(err)
1569 }
1570 fd, err := protodesc.NewFile(pb, nil)
1571 if err != nil {
1572 panic(err)
1573 }
1574 md := fd.Messages().Get(0)
1575 for i := 0; i < md.Fields().Len(); i++ {
1576 md.Fields().Get(i).(*filedesc.Field).L1.HasEnforceUTF8 = true
1577 md.Fields().Get(i).(*filedesc.Field).L1.EnforceUTF8 = false
1578 }
1579 return md
1580 }(),
Joe Tsai74615a32019-07-14 18:51:46 -07001581 NewMessage: func() protoreflect.Message {
1582 return protoreflect.ProtoMessage(new(TestNoEnforceUTF8)).ProtoReflect()
Joe Tsaic51e2e02019-07-13 00:44:41 -07001583 },
1584 },
1585 OneofWrappers: []interface{}{
1586 (*TestNoEnforceUTF8_OneofString)(nil),
1587 (*TestNoEnforceUTF8_OneofBytes)(nil),
1588 },
1589}
1590
Damien Neil4be2fb42018-12-17 11:16:16 -08001591func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001592 for _, opt := range opts {
1593 opt(m)
1594 }
1595 return m
1596}
1597
Damien Neil4be2fb42018-12-17 11:16:16 -08001598type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001599
Joe Tsai74615a32019-07-14 18:51:46 -07001600func unknown(raw protoreflect.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001601 return func(m proto.Message) {
Joe Tsai378c1322019-04-25 23:48:08 -07001602 m.ProtoReflect().SetUnknown(raw)
Damien Neile6f060f2019-04-23 17:11:02 -07001603 }
1604}
1605
Joe Tsai74615a32019-07-14 18:51:46 -07001606func extend(desc *protoiface.ExtensionDescV1, value interface{}) buildOpt {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001607 // TODO: Should ExtensionType.ValueOf accept []T instead of *[]T?
1608 t := reflect.TypeOf(value)
1609 if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
1610 v := reflect.New(t)
1611 v.Elem().Set(reflect.ValueOf(value))
1612 value = v.Interface()
1613 }
1614
Damien Neil4be2fb42018-12-17 11:16:16 -08001615 return func(m proto.Message) {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001616 xt := desc.Type
1617 m.ProtoReflect().Set(xt, xt.ValueOf(value))
Damien Neilba23aa52018-12-07 14:38:17 -08001618 }
1619}
Damien Neil61e93c72019-03-27 09:23:20 -07001620
1621func marshalText(m proto.Message) string {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001622 b, _ := prototext.MarshalOptions{Indent: "\t", AllowPartial: true}.Marshal(m)
Damien Neil61e93c72019-03-27 09:23:20 -07001623 return string(b)
1624}