blob: ce2e1af9d97b867501a3e2ea117a36fd1fef924f [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"
Joe Tsaic51e2e02019-07-13 00:44:41 -070015 "google.golang.org/protobuf/internal/filedesc"
16 "google.golang.org/protobuf/internal/flags"
Damien Neile89e6242019-05-13 23:55:40 -070017 "google.golang.org/protobuf/proto"
Joe Tsaic51e2e02019-07-13 00:44:41 -070018 "google.golang.org/protobuf/reflect/protodesc"
19 "google.golang.org/protobuf/reflect/protoreflect"
Damien Neile89e6242019-05-13 23:55:40 -070020 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsaic51e2e02019-07-13 00:44:41 -070021 "google.golang.org/protobuf/reflect/prototype"
22 "google.golang.org/protobuf/runtime/protoimpl"
Joe Tsai19058432019-02-27 21:46:29 -080023
Damien Neilc37adef2019-04-01 13:49:56 -070024 legacypb "google.golang.org/protobuf/internal/testprotos/legacy"
25 legacy1pb "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v0.0.0-20160225-2fc053c5"
Damien Neile89e6242019-05-13 23:55:40 -070026 testpb "google.golang.org/protobuf/internal/testprotos/test"
27 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Joe Tsaic51e2e02019-07-13 00:44:41 -070028 "google.golang.org/protobuf/types/descriptorpb"
Damien Neilba23aa52018-12-07 14:38:17 -080029)
30
31type testProto struct {
Joe Tsai09cef322019-07-11 22:13:49 -070032 desc string
33 decodeTo []proto.Message
34 wire []byte
35 partial bool
Damien Neilba23aa52018-12-07 14:38:17 -080036}
37
38func TestDecode(t *testing.T) {
39 for _, test := range testProtos {
40 for _, want := range test.decodeTo {
41 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070042 opts := proto.UnmarshalOptions{
43 AllowPartial: test.partial,
44 }
Damien Neilba23aa52018-12-07 14:38:17 -080045 wire := append(([]byte)(nil), test.wire...)
Damien Neil4be2fb42018-12-17 11:16:16 -080046 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
Damien Neil96c229a2019-04-03 12:17:24 -070047 if err := opts.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070048 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080049 return
50 }
51
52 // Aliasing check: Modifying the original wire bytes shouldn't
53 // affect the unmarshaled message.
54 for i := range wire {
55 wire[i] = 0
56 }
Joe Tsaidb38ddd2019-05-07 15:14:40 -070057 if !proto.Equal(got, want) {
Damien Neil61e93c72019-03-27 09:23:20 -070058 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080059 }
60 })
61 }
62 }
63}
64
Damien Neil96c229a2019-04-03 12:17:24 -070065func TestDecodeRequiredFieldChecks(t *testing.T) {
66 for _, test := range testProtos {
67 if !test.partial {
68 continue
69 }
Damien Neil96c229a2019-04-03 12:17:24 -070070 for _, m := range test.decodeTo {
71 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
72 got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
73 if err := proto.Unmarshal(test.wire, got); err == nil {
74 t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got))
75 }
76 })
77 }
78 }
79}
80
Damien Neilbc310b52019-04-11 11:46:55 -070081func TestDecodeInvalidUTF8(t *testing.T) {
82 for _, test := range invalidUTF8TestProtos {
83 for _, want := range test.decodeTo {
84 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
85 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
86 err := proto.Unmarshal(test.wire, got)
Damien Neil8c86fc52019-06-19 09:28:29 -070087 if err == nil {
Damien Neilbc310b52019-04-11 11:46:55 -070088 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
89 }
Damien Neilbc310b52019-04-11 11:46:55 -070090 })
91 }
92 }
93}
94
Joe Tsaic51e2e02019-07-13 00:44:41 -070095func TestDecodeNoEnforceUTF8(t *testing.T) {
96 for _, test := range noEnforceUTF8TestProtos {
97 for _, want := range test.decodeTo {
98 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
99 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
100 err := proto.Unmarshal(test.wire, got)
101 switch {
102 case flags.Proto1Legacy && err != nil:
103 t.Errorf("Unmarshal returned unexpected error: %v\nMessage:\n%v", err, marshalText(want))
104 case !flags.Proto1Legacy && err == nil:
105 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
106 }
107 })
108 }
109 }
110}
111
Damien Neilba23aa52018-12-07 14:38:17 -0800112var testProtos = []testProto{
113 {
114 desc: "basic scalar types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800115 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700116 OptionalInt32: proto.Int32(1001),
117 OptionalInt64: proto.Int64(1002),
118 OptionalUint32: proto.Uint32(1003),
119 OptionalUint64: proto.Uint64(1004),
120 OptionalSint32: proto.Int32(1005),
121 OptionalSint64: proto.Int64(1006),
122 OptionalFixed32: proto.Uint32(1007),
123 OptionalFixed64: proto.Uint64(1008),
124 OptionalSfixed32: proto.Int32(1009),
125 OptionalSfixed64: proto.Int64(1010),
126 OptionalFloat: proto.Float32(1011.5),
127 OptionalDouble: proto.Float64(1012.5),
128 OptionalBool: proto.Bool(true),
129 OptionalString: proto.String("string"),
Damien Neilba23aa52018-12-07 14:38:17 -0800130 OptionalBytes: []byte("bytes"),
131 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
Damien Neil3b46ade2019-03-26 13:55:02 -0700132 }, &test3pb.TestAllTypes{
133 OptionalInt32: 1001,
134 OptionalInt64: 1002,
135 OptionalUint32: 1003,
136 OptionalUint64: 1004,
137 OptionalSint32: 1005,
138 OptionalSint64: 1006,
139 OptionalFixed32: 1007,
140 OptionalFixed64: 1008,
141 OptionalSfixed32: 1009,
142 OptionalSfixed64: 1010,
143 OptionalFloat: 1011.5,
144 OptionalDouble: 1012.5,
145 OptionalBool: true,
146 OptionalString: "string",
147 OptionalBytes: []byte("bytes"),
148 OptionalNestedEnum: test3pb.TestAllTypes_BAR,
Damien Neilba23aa52018-12-07 14:38:17 -0800149 }, build(
150 &testpb.TestAllExtensions{},
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700151 extend(testpb.E_OptionalInt32Extension, int32(1001)),
152 extend(testpb.E_OptionalInt64Extension, int64(1002)),
153 extend(testpb.E_OptionalUint32Extension, uint32(1003)),
154 extend(testpb.E_OptionalUint64Extension, uint64(1004)),
155 extend(testpb.E_OptionalSint32Extension, int32(1005)),
156 extend(testpb.E_OptionalSint64Extension, int64(1006)),
157 extend(testpb.E_OptionalFixed32Extension, uint32(1007)),
158 extend(testpb.E_OptionalFixed64Extension, uint64(1008)),
159 extend(testpb.E_OptionalSfixed32Extension, int32(1009)),
160 extend(testpb.E_OptionalSfixed64Extension, int64(1010)),
161 extend(testpb.E_OptionalFloatExtension, float32(1011.5)),
162 extend(testpb.E_OptionalDoubleExtension, float64(1012.5)),
163 extend(testpb.E_OptionalBoolExtension, bool(true)),
164 extend(testpb.E_OptionalStringExtension, string("string")),
Damien Neilba23aa52018-12-07 14:38:17 -0800165 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700166 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR),
Damien Neilba23aa52018-12-07 14:38:17 -0800167 )},
168 wire: pack.Message{
169 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
170 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
171 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
172 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
173 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
174 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
175 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
176 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
177 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
178 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
179 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
180 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
181 pack.Tag{13, pack.VarintType}, pack.Bool(true),
182 pack.Tag{14, pack.BytesType}, pack.String("string"),
183 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
184 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
185 }.Marshal(),
186 },
187 {
188 desc: "groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800189 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800190 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700191 A: proto.Int32(1017),
Damien Neilba23aa52018-12-07 14:38:17 -0800192 },
193 }, build(
194 &testpb.TestAllExtensions{},
195 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700196 A: proto.Int32(1017),
Damien Neilba23aa52018-12-07 14:38:17 -0800197 }),
198 )},
199 wire: pack.Message{
200 pack.Tag{16, pack.StartGroupType},
201 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
202 pack.Tag{16, pack.EndGroupType},
203 }.Marshal(),
204 },
205 {
206 desc: "groups (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800207 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800208 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700209 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800210 },
211 }, build(
212 &testpb.TestAllExtensions{},
213 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700214 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800215 }),
216 )},
217 wire: pack.Message{
218 pack.Tag{16, pack.StartGroupType},
219 pack.Tag{17, pack.VarintType}, pack.Varint(1),
220 pack.Tag{16, pack.EndGroupType},
221 pack.Tag{16, pack.StartGroupType},
222 pack.Tag{17, pack.VarintType}, pack.Varint(2),
223 pack.Tag{16, pack.EndGroupType},
224 }.Marshal(),
225 },
226 {
227 desc: "messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800228 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800229 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700230 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800231 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700232 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800233 },
234 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700235 }, &test3pb.TestAllTypes{
236 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
237 A: 42,
238 Corecursive: &test3pb.TestAllTypes{
239 OptionalInt32: 43,
240 },
241 },
Damien Neilba23aa52018-12-07 14:38:17 -0800242 }, build(
243 &testpb.TestAllExtensions{},
244 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700245 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800246 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700247 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800248 },
249 }),
250 )},
251 wire: pack.Message{
252 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
253 pack.Tag{1, pack.VarintType}, pack.Varint(42),
254 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
255 pack.Tag{1, pack.VarintType}, pack.Varint(43),
256 }),
257 }),
258 }.Marshal(),
259 },
260 {
261 desc: "messages (split across multiple tags)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800262 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800263 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700264 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800265 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700266 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800267 },
268 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700269 }, &test3pb.TestAllTypes{
270 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
271 A: 42,
272 Corecursive: &test3pb.TestAllTypes{
273 OptionalInt32: 43,
274 },
275 },
Damien Neilba23aa52018-12-07 14:38:17 -0800276 }, build(
277 &testpb.TestAllExtensions{},
278 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700279 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800280 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700281 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800282 },
283 }),
284 )},
285 wire: pack.Message{
286 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
287 pack.Tag{1, pack.VarintType}, pack.Varint(42),
288 }),
289 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
290 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
291 pack.Tag{1, pack.VarintType}, pack.Varint(43),
292 }),
293 }),
294 }.Marshal(),
295 },
296 {
297 desc: "messages (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800298 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800299 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700300 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800301 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700302 }, &test3pb.TestAllTypes{
303 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
304 A: 2,
305 },
Damien Neilba23aa52018-12-07 14:38:17 -0800306 }, build(
307 &testpb.TestAllExtensions{},
308 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700309 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800310 }),
311 )},
312 wire: pack.Message{
313 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
314 pack.Tag{1, pack.VarintType}, pack.Varint(1),
315 }),
316 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
317 pack.Tag{1, pack.VarintType}, pack.Varint(2),
318 }),
319 }.Marshal(),
320 },
321 {
322 desc: "basic repeated types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800323 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800324 RepeatedInt32: []int32{1001, 2001},
325 RepeatedInt64: []int64{1002, 2002},
326 RepeatedUint32: []uint32{1003, 2003},
327 RepeatedUint64: []uint64{1004, 2004},
328 RepeatedSint32: []int32{1005, 2005},
329 RepeatedSint64: []int64{1006, 2006},
330 RepeatedFixed32: []uint32{1007, 2007},
331 RepeatedFixed64: []uint64{1008, 2008},
332 RepeatedSfixed32: []int32{1009, 2009},
333 RepeatedSfixed64: []int64{1010, 2010},
334 RepeatedFloat: []float32{1011.5, 2011.5},
335 RepeatedDouble: []float64{1012.5, 2012.5},
336 RepeatedBool: []bool{true, false},
337 RepeatedString: []string{"foo", "bar"},
338 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
339 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
340 testpb.TestAllTypes_FOO,
341 testpb.TestAllTypes_BAR,
342 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700343 }, &test3pb.TestAllTypes{
344 RepeatedInt32: []int32{1001, 2001},
345 RepeatedInt64: []int64{1002, 2002},
346 RepeatedUint32: []uint32{1003, 2003},
347 RepeatedUint64: []uint64{1004, 2004},
348 RepeatedSint32: []int32{1005, 2005},
349 RepeatedSint64: []int64{1006, 2006},
350 RepeatedFixed32: []uint32{1007, 2007},
351 RepeatedFixed64: []uint64{1008, 2008},
352 RepeatedSfixed32: []int32{1009, 2009},
353 RepeatedSfixed64: []int64{1010, 2010},
354 RepeatedFloat: []float32{1011.5, 2011.5},
355 RepeatedDouble: []float64{1012.5, 2012.5},
356 RepeatedBool: []bool{true, false},
357 RepeatedString: []string{"foo", "bar"},
358 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
359 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
360 test3pb.TestAllTypes_FOO,
361 test3pb.TestAllTypes_BAR,
362 },
Damien Neilba23aa52018-12-07 14:38:17 -0800363 }, build(
364 &testpb.TestAllExtensions{},
365 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
366 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
367 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
368 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
369 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
370 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
371 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
372 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
373 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
374 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
375 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
376 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
377 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
378 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
379 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
380 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
381 testpb.TestAllTypes_FOO,
382 testpb.TestAllTypes_BAR,
383 }),
384 )},
385 wire: pack.Message{
386 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
387 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
388 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
389 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
390 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
391 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
392 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
393 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
394 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
395 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
396 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
397 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
398 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
399 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
400 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
401 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
402 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
403 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
404 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
405 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
406 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
407 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
408 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
409 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
410 pack.Tag{43, pack.VarintType}, pack.Bool(true),
411 pack.Tag{43, pack.VarintType}, pack.Bool(false),
412 pack.Tag{44, pack.BytesType}, pack.String("foo"),
413 pack.Tag{44, pack.BytesType}, pack.String("bar"),
414 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
415 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
416 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
417 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
418 }.Marshal(),
419 },
420 {
421 desc: "basic repeated types (packed encoding)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800422 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800423 RepeatedInt32: []int32{1001, 2001},
424 RepeatedInt64: []int64{1002, 2002},
425 RepeatedUint32: []uint32{1003, 2003},
426 RepeatedUint64: []uint64{1004, 2004},
427 RepeatedSint32: []int32{1005, 2005},
428 RepeatedSint64: []int64{1006, 2006},
429 RepeatedFixed32: []uint32{1007, 2007},
430 RepeatedFixed64: []uint64{1008, 2008},
431 RepeatedSfixed32: []int32{1009, 2009},
432 RepeatedSfixed64: []int64{1010, 2010},
433 RepeatedFloat: []float32{1011.5, 2011.5},
434 RepeatedDouble: []float64{1012.5, 2012.5},
435 RepeatedBool: []bool{true, false},
436 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
437 testpb.TestAllTypes_FOO,
438 testpb.TestAllTypes_BAR,
439 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700440 }, &test3pb.TestAllTypes{
441 RepeatedInt32: []int32{1001, 2001},
442 RepeatedInt64: []int64{1002, 2002},
443 RepeatedUint32: []uint32{1003, 2003},
444 RepeatedUint64: []uint64{1004, 2004},
445 RepeatedSint32: []int32{1005, 2005},
446 RepeatedSint64: []int64{1006, 2006},
447 RepeatedFixed32: []uint32{1007, 2007},
448 RepeatedFixed64: []uint64{1008, 2008},
449 RepeatedSfixed32: []int32{1009, 2009},
450 RepeatedSfixed64: []int64{1010, 2010},
451 RepeatedFloat: []float32{1011.5, 2011.5},
452 RepeatedDouble: []float64{1012.5, 2012.5},
453 RepeatedBool: []bool{true, false},
454 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
455 test3pb.TestAllTypes_FOO,
456 test3pb.TestAllTypes_BAR,
457 },
Damien Neilba23aa52018-12-07 14:38:17 -0800458 }, build(
459 &testpb.TestAllExtensions{},
460 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
461 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
462 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
463 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
464 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
465 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
466 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
467 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
468 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
469 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
470 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
471 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
472 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
473 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
474 testpb.TestAllTypes_FOO,
475 testpb.TestAllTypes_BAR,
476 }),
477 )},
478 wire: pack.Message{
479 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
480 pack.Varint(1001), pack.Varint(2001),
481 },
482 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
483 pack.Varint(1002), pack.Varint(2002),
484 },
485 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
486 pack.Uvarint(1003), pack.Uvarint(2003),
487 },
488 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
489 pack.Uvarint(1004), pack.Uvarint(2004),
490 },
491 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
492 pack.Svarint(1005), pack.Svarint(2005),
493 },
494 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
495 pack.Svarint(1006), pack.Svarint(2006),
496 },
497 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
498 pack.Uint32(1007), pack.Uint32(2007),
499 },
500 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
501 pack.Uint64(1008), pack.Uint64(2008),
502 },
503 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
504 pack.Int32(1009), pack.Int32(2009),
505 },
506 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
507 pack.Int64(1010), pack.Int64(2010),
508 },
509 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
510 pack.Float32(1011.5), pack.Float32(2011.5),
511 },
512 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
513 pack.Float64(1012.5), pack.Float64(2012.5),
514 },
515 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
516 pack.Bool(true), pack.Bool(false),
517 },
518 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
519 pack.Varint(int(testpb.TestAllTypes_FOO)),
520 pack.Varint(int(testpb.TestAllTypes_BAR)),
521 },
522 }.Marshal(),
523 },
524 {
Damien Neil7492a092019-07-10 15:23:29 -0700525 desc: "packed repeated types",
526 decodeTo: []proto.Message{&testpb.TestPackedTypes{
527 PackedInt32: []int32{1001, 2001},
528 PackedInt64: []int64{1002, 2002},
529 PackedUint32: []uint32{1003, 2003},
530 PackedUint64: []uint64{1004, 2004},
531 PackedSint32: []int32{1005, 2005},
532 PackedSint64: []int64{1006, 2006},
533 PackedFixed32: []uint32{1007, 2007},
534 PackedFixed64: []uint64{1008, 2008},
535 PackedSfixed32: []int32{1009, 2009},
536 PackedSfixed64: []int64{1010, 2010},
537 PackedFloat: []float32{1011.5, 2011.5},
538 PackedDouble: []float64{1012.5, 2012.5},
539 PackedBool: []bool{true, false},
540 PackedEnum: []testpb.ForeignEnum{
541 testpb.ForeignEnum_FOREIGN_FOO,
542 testpb.ForeignEnum_FOREIGN_BAR,
543 },
544 }, build(
545 &testpb.TestPackedExtensions{},
546 extend(testpb.E_PackedInt32Extension, []int32{1001, 2001}),
547 extend(testpb.E_PackedInt64Extension, []int64{1002, 2002}),
548 extend(testpb.E_PackedUint32Extension, []uint32{1003, 2003}),
549 extend(testpb.E_PackedUint64Extension, []uint64{1004, 2004}),
550 extend(testpb.E_PackedSint32Extension, []int32{1005, 2005}),
551 extend(testpb.E_PackedSint64Extension, []int64{1006, 2006}),
552 extend(testpb.E_PackedFixed32Extension, []uint32{1007, 2007}),
553 extend(testpb.E_PackedFixed64Extension, []uint64{1008, 2008}),
554 extend(testpb.E_PackedSfixed32Extension, []int32{1009, 2009}),
555 extend(testpb.E_PackedSfixed64Extension, []int64{1010, 2010}),
556 extend(testpb.E_PackedFloatExtension, []float32{1011.5, 2011.5}),
557 extend(testpb.E_PackedDoubleExtension, []float64{1012.5, 2012.5}),
558 extend(testpb.E_PackedBoolExtension, []bool{true, false}),
559 extend(testpb.E_PackedEnumExtension, []testpb.ForeignEnum{
560 testpb.ForeignEnum_FOREIGN_FOO,
561 testpb.ForeignEnum_FOREIGN_BAR,
562 }),
563 )},
564 wire: pack.Message{
565 pack.Tag{90, pack.BytesType}, pack.LengthPrefix{
566 pack.Varint(1001), pack.Varint(2001),
567 },
568 pack.Tag{91, pack.BytesType}, pack.LengthPrefix{
569 pack.Varint(1002), pack.Varint(2002),
570 },
571 pack.Tag{92, pack.BytesType}, pack.LengthPrefix{
572 pack.Uvarint(1003), pack.Uvarint(2003),
573 },
574 pack.Tag{93, pack.BytesType}, pack.LengthPrefix{
575 pack.Uvarint(1004), pack.Uvarint(2004),
576 },
577 pack.Tag{94, pack.BytesType}, pack.LengthPrefix{
578 pack.Svarint(1005), pack.Svarint(2005),
579 },
580 pack.Tag{95, pack.BytesType}, pack.LengthPrefix{
581 pack.Svarint(1006), pack.Svarint(2006),
582 },
583 pack.Tag{96, pack.BytesType}, pack.LengthPrefix{
584 pack.Uint32(1007), pack.Uint32(2007),
585 },
586 pack.Tag{97, pack.BytesType}, pack.LengthPrefix{
587 pack.Uint64(1008), pack.Uint64(2008),
588 },
589 pack.Tag{98, pack.BytesType}, pack.LengthPrefix{
590 pack.Int32(1009), pack.Int32(2009),
591 },
592 pack.Tag{99, pack.BytesType}, pack.LengthPrefix{
593 pack.Int64(1010), pack.Int64(2010),
594 },
595 pack.Tag{100, pack.BytesType}, pack.LengthPrefix{
596 pack.Float32(1011.5), pack.Float32(2011.5),
597 },
598 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{
599 pack.Float64(1012.5), pack.Float64(2012.5),
600 },
601 pack.Tag{102, pack.BytesType}, pack.LengthPrefix{
602 pack.Bool(true), pack.Bool(false),
603 },
604 pack.Tag{103, pack.BytesType}, pack.LengthPrefix{
605 pack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
606 pack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
607 },
608 }.Marshal(),
609 },
610 {
Damien Neilba23aa52018-12-07 14:38:17 -0800611 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800612 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800613 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700614 {A: proto.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700615 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700616 {A: proto.Int32(2)},
Damien Neilba23aa52018-12-07 14:38:17 -0800617 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700618 }, &test3pb.TestAllTypes{
619 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
620 {A: 1},
Damien Neilc37adef2019-04-01 13:49:56 -0700621 nil,
Damien Neil3b46ade2019-03-26 13:55:02 -0700622 {A: 2},
623 },
Damien Neilba23aa52018-12-07 14:38:17 -0800624 }, build(
625 &testpb.TestAllExtensions{},
626 extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700627 {A: proto.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700628 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700629 {A: proto.Int32(2)},
Damien Neilba23aa52018-12-07 14:38:17 -0800630 }),
631 )},
632 wire: pack.Message{
633 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
634 pack.Tag{1, pack.VarintType}, pack.Varint(1),
635 }),
Damien Neilc37adef2019-04-01 13:49:56 -0700636 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
Damien Neilba23aa52018-12-07 14:38:17 -0800637 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
638 pack.Tag{1, pack.VarintType}, pack.Varint(2),
639 }),
640 }.Marshal(),
641 },
642 {
643 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800644 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800645 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700646 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700647 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700648 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800649 },
650 }, build(
651 &testpb.TestAllExtensions{},
652 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700653 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700654 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700655 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800656 }),
657 )},
658 wire: pack.Message{
659 pack.Tag{46, pack.StartGroupType},
660 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
661 pack.Tag{46, pack.EndGroupType},
662 pack.Tag{46, pack.StartGroupType},
Damien Neilc37adef2019-04-01 13:49:56 -0700663 pack.Tag{46, pack.EndGroupType},
664 pack.Tag{46, pack.StartGroupType},
Damien Neilba23aa52018-12-07 14:38:17 -0800665 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
666 pack.Tag{46, pack.EndGroupType},
667 }.Marshal(),
668 },
669 {
670 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800671 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800672 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
673 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
674 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
675 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
676 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
677 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
678 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
679 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
680 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
681 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
682 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
683 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
684 MapBoolBool: map[bool]bool{true: false, false: true},
685 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
686 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
687 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700688 "71.1.key": {A: proto.Int32(1171)},
689 "71.2.key": {A: proto.Int32(2171)},
Damien Neilba23aa52018-12-07 14:38:17 -0800690 },
691 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
692 "73.1.key": testpb.TestAllTypes_FOO,
693 "73.2.key": testpb.TestAllTypes_BAR,
694 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700695 }, &test3pb.TestAllTypes{
696 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
697 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
698 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
699 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
700 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
701 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
702 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
703 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
704 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
705 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
706 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
707 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
708 MapBoolBool: map[bool]bool{true: false, false: true},
709 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
710 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
711 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
712 "71.1.key": {A: 1171},
713 "71.2.key": {A: 2171},
714 },
715 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
716 "73.1.key": test3pb.TestAllTypes_FOO,
717 "73.2.key": test3pb.TestAllTypes_BAR,
718 },
Damien Neilba23aa52018-12-07 14:38:17 -0800719 }},
720 wire: pack.Message{
721 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
722 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
723 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
724 }),
725 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
726 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
727 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
728 }),
729 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
730 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
731 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
732 }),
733 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
734 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
735 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
736 }),
737 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
738 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
739 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
740 }),
741 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
742 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
743 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
744 }),
745 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
746 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
747 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
748 }),
749 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
750 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
751 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
752 }),
753 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
754 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
755 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
756 }),
757 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
758 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
759 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
760 }),
761 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
762 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
763 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
764 }),
765 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
766 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
767 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
768 }),
769 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
770 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
771 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
772 }),
773 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
774 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
775 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
776 }),
777 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
778 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
779 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
780 }),
781 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
782 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
783 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
784 }),
785 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
786 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
787 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
788 }),
789 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
790 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
791 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
792 }),
793 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
794 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
795 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
796 }),
797 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
798 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
799 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
800 }),
801 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
802 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
803 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
804 }),
805 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
806 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
807 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
808 }),
809 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
810 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
811 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
812 }),
813 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
814 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
815 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
816 }),
817 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
818 pack.Tag{1, pack.VarintType}, pack.Bool(true),
819 pack.Tag{2, pack.VarintType}, pack.Bool(false),
820 }),
821 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
822 pack.Tag{1, pack.VarintType}, pack.Bool(false),
823 pack.Tag{2, pack.VarintType}, pack.Bool(true),
824 }),
825 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
826 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
827 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
828 }),
829 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
830 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
831 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
832 }),
833 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
834 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
835 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
836 }),
837 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
838 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
839 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
840 }),
841 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
842 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
843 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
844 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
845 }),
846 }),
847 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
848 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
849 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
850 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
851 }),
852 }),
853 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
854 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
855 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
856 }),
857 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
858 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
859 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
860 }),
861 }.Marshal(),
862 },
863 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700864 desc: "oneof (uint32)",
865 decodeTo: []proto.Message{
866 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
867 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
868 },
869 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800870 },
871 {
872 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700873 decodeTo: []proto.Message{
874 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700875 &testpb.TestAllTypes_NestedMessage{A: proto.Int32(1112)},
Damien Neil3b46ade2019-03-26 13:55:02 -0700876 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
877 &test3pb.TestAllTypes_NestedMessage{A: 1112},
878 }},
879 },
Damien Neilba23aa52018-12-07 14:38:17 -0800880 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
881 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
882 })}.Marshal(),
883 },
884 {
Damien Neilc37adef2019-04-01 13:49:56 -0700885 desc: "oneof (empty message)",
886 decodeTo: []proto.Message{
887 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
888 &testpb.TestAllTypes_NestedMessage{},
889 }},
890 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
891 &test3pb.TestAllTypes_NestedMessage{},
892 }},
893 },
894 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
895 },
896 {
Joe Tsai6c286742019-07-11 23:15:05 -0700897 desc: "oneof (merged message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700898 decodeTo: []proto.Message{
899 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
900 &testpb.TestAllTypes_NestedMessage{
Joe Tsai0f81b382019-07-10 23:14:31 -0700901 A: proto.Int32(1),
Damien Neil3b46ade2019-03-26 13:55:02 -0700902 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700903 OptionalInt32: proto.Int32(43),
Damien Neil3b46ade2019-03-26 13:55:02 -0700904 },
Damien Neilba23aa52018-12-07 14:38:17 -0800905 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700906 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
907 &test3pb.TestAllTypes_NestedMessage{
Joe Tsai6c286742019-07-11 23:15:05 -0700908 A: 1,
Damien Neil3b46ade2019-03-26 13:55:02 -0700909 Corecursive: &test3pb.TestAllTypes{
910 OptionalInt32: 43,
911 },
912 },
913 }}},
Damien Neilba23aa52018-12-07 14:38:17 -0800914 wire: pack.Message{
915 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
916 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
917 }),
918 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
919 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
920 pack.Tag{1, pack.VarintType}, pack.Varint(43),
921 }),
922 }),
923 }.Marshal(),
924 },
925 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700926 desc: "oneof (string)",
927 decodeTo: []proto.Message{
928 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
929 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
930 },
931 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800932 },
933 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700934 desc: "oneof (bytes)",
935 decodeTo: []proto.Message{
936 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
937 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
938 },
939 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800940 },
941 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700942 desc: "oneof (bool)",
943 decodeTo: []proto.Message{
944 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
945 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
946 },
947 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800948 },
949 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700950 desc: "oneof (uint64)",
951 decodeTo: []proto.Message{
952 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
953 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
954 },
955 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800956 },
957 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700958 desc: "oneof (float)",
959 decodeTo: []proto.Message{
960 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
961 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
962 },
963 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800964 },
965 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700966 desc: "oneof (double)",
967 decodeTo: []proto.Message{
968 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
969 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
970 },
971 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800972 },
973 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700974 desc: "oneof (enum)",
975 decodeTo: []proto.Message{
976 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
977 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
978 },
979 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800980 },
981 {
Damien Neilc37adef2019-04-01 13:49:56 -0700982 desc: "oneof (zero)",
983 decodeTo: []proto.Message{
984 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}},
985 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}},
986 },
987 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
988 },
989 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700990 desc: "oneof (overridden value)",
991 decodeTo: []proto.Message{
992 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
993 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
994 },
Damien Neilba23aa52018-12-07 14:38:17 -0800995 wire: pack.Message{
996 pack.Tag{111, pack.VarintType}, pack.Varint(1),
997 pack.Tag{116, pack.VarintType}, pack.Varint(2),
998 }.Marshal(),
999 },
1000 // TODO: More unknown field tests for ordering, repeated fields, etc.
1001 //
1002 // It is currently impossible to produce results that the v1 Equal
1003 // considers equivalent to those of the v1 decoder. Figure out if
1004 // that's a problem or not.
1005 {
1006 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -08001007 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001008 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001009 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001010 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1011 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001012 ), build(
1013 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001014 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001015 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1016 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001017 )},
1018 wire: pack.Message{
1019 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1020 }.Marshal(),
1021 },
1022 {
1023 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001024 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001025 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001026 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001027 pack.Tag{1, pack.BytesType}, pack.String("string"),
1028 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001029 ), build(
1030 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001031 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001032 pack.Tag{1, pack.BytesType}, pack.String("string"),
1033 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001034 )},
1035 wire: pack.Message{
1036 pack.Tag{1, pack.BytesType}, pack.String("string"),
1037 }.Marshal(),
1038 },
1039 {
1040 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001041 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001042 &testpb.TestAllTypes{
1043 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -07001044 }, &test3pb.TestAllTypes{
1045 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -08001046 },
1047 },
1048 wire: pack.Message{
1049 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
1050 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1051 pack.Tag{2, pack.BytesType}, pack.String("string"),
1052 }),
1053 }.Marshal(),
1054 },
Damien Neil96c229a2019-04-03 12:17:24 -07001055 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001056 desc: "required field in nil message unset",
1057 partial: true,
1058 decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
1059 },
1060 {
Damien Neil96c229a2019-04-03 12:17:24 -07001061 desc: "required field unset",
1062 partial: true,
1063 decodeTo: []proto.Message{&testpb.TestRequired{}},
1064 },
1065 {
1066 desc: "required field set",
1067 decodeTo: []proto.Message{&testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001068 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001069 }},
1070 wire: pack.Message{
1071 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1072 }.Marshal(),
1073 },
1074 {
1075 desc: "required field in optional message unset",
1076 partial: true,
1077 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1078 OptionalMessage: &testpb.TestRequired{},
1079 }},
1080 wire: pack.Message{
1081 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1082 }.Marshal(),
1083 },
1084 {
1085 desc: "required field in optional message set",
1086 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1087 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001088 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001089 },
1090 }},
1091 wire: pack.Message{
1092 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1093 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1094 }),
1095 }.Marshal(),
1096 },
Damien Neil4686e232019-04-05 13:31:40 -07001097 {
1098 desc: "required field in optional message set (split across multiple tags)",
1099 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1100 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001101 RequiredField: proto.Int32(1),
Damien Neil4686e232019-04-05 13:31:40 -07001102 },
1103 }},
1104 wire: pack.Message{
1105 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1106 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1107 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1108 }),
1109 }.Marshal(),
1110 },
Damien Neil96c229a2019-04-03 12:17:24 -07001111 {
1112 desc: "required field in repeated message unset",
1113 partial: true,
1114 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1115 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001116 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001117 {},
1118 },
1119 }},
1120 wire: pack.Message{
1121 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1122 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1123 }),
1124 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1125 }.Marshal(),
1126 },
1127 {
1128 desc: "required field in repeated message set",
1129 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1130 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001131 {RequiredField: proto.Int32(1)},
1132 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001133 },
1134 }},
1135 wire: pack.Message{
1136 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1137 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1138 }),
1139 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1140 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1141 }),
1142 }.Marshal(),
1143 },
1144 {
1145 desc: "required field in map message unset",
1146 partial: true,
1147 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1148 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001149 1: {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001150 2: {},
1151 },
1152 }},
1153 wire: pack.Message{
1154 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1155 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1156 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1157 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1158 }),
1159 }),
1160 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1161 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1162 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1163 }),
1164 }.Marshal(),
1165 },
1166 {
1167 desc: "required field in map message set",
1168 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1169 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001170 1: {RequiredField: proto.Int32(1)},
1171 2: {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001172 },
1173 }},
1174 wire: pack.Message{
1175 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1176 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1177 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1178 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1179 }),
1180 }),
1181 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1182 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1183 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1184 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1185 }),
1186 }),
1187 }.Marshal(),
1188 },
1189 {
1190 desc: "required field in optional group unset",
1191 partial: true,
1192 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1193 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1194 }},
1195 wire: pack.Message{
1196 pack.Tag{1, pack.StartGroupType},
1197 pack.Tag{1, pack.EndGroupType},
1198 }.Marshal(),
1199 },
1200 {
1201 desc: "required field in optional group set",
1202 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1203 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001204 A: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001205 },
1206 }},
1207 wire: pack.Message{
1208 pack.Tag{1, pack.StartGroupType},
1209 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1210 pack.Tag{1, pack.EndGroupType},
1211 }.Marshal(),
1212 },
1213 {
1214 desc: "required field in repeated group unset",
1215 partial: true,
1216 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1217 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001218 {A: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001219 {},
1220 },
1221 }},
1222 wire: pack.Message{
1223 pack.Tag{3, pack.StartGroupType},
1224 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1225 pack.Tag{3, pack.EndGroupType},
1226 pack.Tag{3, pack.StartGroupType},
1227 pack.Tag{3, pack.EndGroupType},
1228 }.Marshal(),
1229 },
1230 {
1231 desc: "required field in repeated group set",
1232 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1233 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001234 {A: proto.Int32(1)},
1235 {A: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001236 },
1237 }},
1238 wire: pack.Message{
1239 pack.Tag{3, pack.StartGroupType},
1240 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1241 pack.Tag{3, pack.EndGroupType},
1242 pack.Tag{3, pack.StartGroupType},
1243 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1244 pack.Tag{3, pack.EndGroupType},
1245 }.Marshal(),
1246 },
1247 {
Damien Neil5322bdb2019-04-09 15:57:05 -07001248 desc: "required field in oneof message unset",
1249 partial: true,
1250 decodeTo: []proto.Message{
1251 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1252 &testpb.TestRequired{},
1253 }},
1254 },
1255 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1256 },
1257 {
1258 desc: "required field in oneof message set",
1259 decodeTo: []proto.Message{
1260 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1261 &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001262 RequiredField: proto.Int32(1),
Damien Neil5322bdb2019-04-09 15:57:05 -07001263 },
1264 }},
1265 },
1266 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
1267 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1268 })}.Marshal(),
1269 },
1270 {
Joe Tsai09cef322019-07-11 22:13:49 -07001271 desc: "required field in extension message unset",
1272 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001273 decodeTo: []proto.Message{build(
1274 &testpb.TestAllExtensions{},
1275 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1276 )},
1277 wire: pack.Message{
1278 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1279 }.Marshal(),
1280 },
1281 {
1282 desc: "required field in extension message set",
1283 decodeTo: []proto.Message{build(
1284 &testpb.TestAllExtensions{},
1285 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001286 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001287 }),
1288 )},
1289 wire: pack.Message{
1290 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1291 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1292 }),
1293 }.Marshal(),
1294 },
1295 {
Joe Tsai09cef322019-07-11 22:13:49 -07001296 desc: "required field in repeated extension message unset",
1297 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001298 decodeTo: []proto.Message{build(
1299 &testpb.TestAllExtensions{},
1300 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001301 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001302 {},
1303 }),
1304 )},
1305 wire: pack.Message{
1306 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1307 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1308 }),
1309 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1310 }.Marshal(),
1311 },
1312 {
1313 desc: "required field in repeated extension message set",
1314 decodeTo: []proto.Message{build(
1315 &testpb.TestAllExtensions{},
1316 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001317 {RequiredField: proto.Int32(1)},
1318 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001319 }),
1320 )},
1321 wire: pack.Message{
1322 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1323 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1324 }),
1325 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1326 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1327 }),
1328 }.Marshal(),
1329 },
Damien Neilc37adef2019-04-01 13:49:56 -07001330 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001331 desc: "nil messages",
1332 decodeTo: []proto.Message{
1333 (*testpb.TestAllTypes)(nil),
1334 (*test3pb.TestAllTypes)(nil),
1335 (*testpb.TestAllExtensions)(nil),
1336 },
1337 },
1338 {
Damien Neilc37adef2019-04-01 13:49:56 -07001339 desc: "legacy",
1340 partial: true,
1341 decodeTo: []proto.Message{
1342 &legacypb.Legacy{
1343 F1: &legacy1pb.Message{
Damien Neila8a2cea2019-07-10 16:17:16 -07001344 OptionalInt32: proto.Int32(1),
Damien Neilc37adef2019-04-01 13:49:56 -07001345 OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
1346 OptionalChildMessage: &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001347 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001348 },
1349 Optionalgroup: &legacy1pb.Message_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001350 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001351 },
1352 RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001353 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001354 },
1355 Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001356 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001357 },
1358 MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001359 true: {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001360 },
1361 OneofUnion: &legacy1pb.Message_OneofChildMessage{
1362 &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001363 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001364 },
1365 },
1366 },
1367 },
1368 },
1369 wire: pack.Message{
1370 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1371 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1372 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1373 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1374 pack.Tag{1, pack.BytesType}, pack.String("x"),
1375 }),
1376 pack.Tag{120, pack.StartGroupType},
1377 pack.Tag{1, pack.BytesType}, pack.String("x"),
1378 pack.Tag{120, pack.EndGroupType},
1379 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1380 pack.Tag{1, pack.BytesType}, pack.String("x"),
1381 }),
1382 pack.Tag{520, pack.StartGroupType},
1383 pack.Tag{1, pack.BytesType}, pack.String("x"),
1384 pack.Tag{520, pack.EndGroupType},
1385 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1386 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1387 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1388 pack.Tag{1, pack.BytesType}, pack.String("x"),
1389 }),
1390 }),
1391 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1392 pack.Tag{1, pack.BytesType}, pack.String("x"),
1393 }),
1394 }),
1395 }.Marshal(),
1396 },
Damien Neilba23aa52018-12-07 14:38:17 -08001397}
1398
Damien Neilbc310b52019-04-11 11:46:55 -07001399var invalidUTF8TestProtos = []testProto{
1400 {
1401 desc: "invalid UTF-8 in optional string field",
1402 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1403 OptionalString: "abc\xff",
1404 }},
1405 wire: pack.Message{
1406 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1407 }.Marshal(),
1408 },
1409 {
1410 desc: "invalid UTF-8 in repeated string field",
1411 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1412 RepeatedString: []string{"foo", "abc\xff"},
1413 }},
1414 wire: pack.Message{
1415 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1416 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1417 }.Marshal(),
1418 },
1419 {
1420 desc: "invalid UTF-8 in nested message",
1421 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1422 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1423 Corecursive: &test3pb.TestAllTypes{
1424 OptionalString: "abc\xff",
1425 },
1426 },
1427 }},
1428 wire: pack.Message{
1429 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1430 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1431 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1432 }),
1433 }),
1434 }.Marshal(),
1435 },
1436 {
Damien Neilc37adef2019-04-01 13:49:56 -07001437 desc: "invalid UTF-8 in oneof field",
1438 decodeTo: []proto.Message{
1439 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
1440 },
1441 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1442 },
1443 {
Damien Neilbc310b52019-04-11 11:46:55 -07001444 desc: "invalid UTF-8 in map key",
1445 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1446 MapStringString: map[string]string{"key\xff": "val"},
1447 }},
1448 wire: pack.Message{
1449 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1450 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1451 pack.Tag{2, pack.BytesType}, pack.String("val"),
1452 }),
1453 }.Marshal(),
1454 },
1455 {
1456 desc: "invalid UTF-8 in map value",
1457 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1458 MapStringString: map[string]string{"key": "val\xff"},
1459 }},
1460 wire: pack.Message{
1461 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1462 pack.Tag{1, pack.BytesType}, pack.String("key"),
1463 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1464 }),
1465 }.Marshal(),
1466 },
1467}
1468
Joe Tsaic51e2e02019-07-13 00:44:41 -07001469var noEnforceUTF8TestProtos = []testProto{
1470 {
1471 desc: "invalid UTF-8 in optional string field",
1472 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1473 OptionalString: string("abc\xff"),
1474 }},
1475 wire: pack.Message{
1476 pack.Tag{1, pack.BytesType}, pack.String("abc\xff"),
1477 }.Marshal(),
1478 },
1479 {
1480 desc: "invalid UTF-8 in optional string field of Go bytes",
1481 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1482 OptionalBytes: []byte("abc\xff"),
1483 }},
1484 wire: pack.Message{
1485 pack.Tag{2, pack.BytesType}, pack.String("abc\xff"),
1486 }.Marshal(),
1487 },
1488 {
1489 desc: "invalid UTF-8 in repeated string field",
1490 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1491 RepeatedString: []string{string("foo"), string("abc\xff")},
1492 }},
1493 wire: pack.Message{
1494 pack.Tag{3, pack.BytesType}, pack.String("foo"),
1495 pack.Tag{3, pack.BytesType}, pack.String("abc\xff"),
1496 }.Marshal(),
1497 },
1498 {
1499 desc: "invalid UTF-8 in repeated string field of Go bytes",
1500 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1501 RepeatedBytes: [][]byte{[]byte("foo"), []byte("abc\xff")},
1502 }},
1503 wire: pack.Message{
1504 pack.Tag{4, pack.BytesType}, pack.String("foo"),
1505 pack.Tag{4, pack.BytesType}, pack.String("abc\xff"),
1506 }.Marshal(),
1507 },
1508 {
1509 desc: "invalid UTF-8 in oneof string field",
1510 decodeTo: []proto.Message{
1511 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofString{string("abc\xff")}},
1512 },
1513 wire: pack.Message{pack.Tag{5, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1514 },
1515 {
1516 desc: "invalid UTF-8 in oneof string field of Go bytes",
1517 decodeTo: []proto.Message{
1518 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofBytes{[]byte("abc\xff")}},
1519 },
1520 wire: pack.Message{pack.Tag{6, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1521 },
1522}
1523
1524type TestNoEnforceUTF8 struct {
1525 OptionalString string `protobuf:"bytes,1,opt,name=optional_string"`
1526 OptionalBytes []byte `protobuf:"bytes,2,opt,name=optional_bytes"`
1527 RepeatedString []string `protobuf:"bytes,3,rep,name=repeated_string"`
1528 RepeatedBytes [][]byte `protobuf:"bytes,4,rep,name=repeated_bytes"`
1529 OneofField isOneofField `protobuf_oneof:"oneof_field"`
1530}
1531
1532type isOneofField interface{ isOneofField() }
1533
1534type TestNoEnforceUTF8_OneofString struct {
1535 OneofString string `protobuf:"bytes,5,opt,name=oneof_string,oneof"`
1536}
1537type TestNoEnforceUTF8_OneofBytes struct {
1538 OneofBytes []byte `protobuf:"bytes,6,opt,name=oneof_bytes,oneof"`
1539}
1540
1541func (*TestNoEnforceUTF8_OneofString) isOneofField() {}
1542func (*TestNoEnforceUTF8_OneofBytes) isOneofField() {}
1543
1544func (m *TestNoEnforceUTF8) ProtoReflect() pref.Message {
1545 return messageInfo_TestNoEnforceUTF8.MessageOf(m)
1546}
1547
1548var messageInfo_TestNoEnforceUTF8 = protoimpl.MessageInfo{
1549 GoType: reflect.TypeOf((*TestNoEnforceUTF8)(nil)),
1550 PBType: &prototype.Message{
1551 MessageDescriptor: func() protoreflect.MessageDescriptor {
1552 pb := new(descriptorpb.FileDescriptorProto)
1553 if err := prototext.Unmarshal([]byte(`
1554 syntax: "proto3"
1555 name: "test.proto"
1556 message_type: [{
1557 name: "TestNoEnforceUTF8"
1558 field: [
1559 {name:"optional_string" number:1 label:LABEL_OPTIONAL type:TYPE_STRING},
1560 {name:"optional_bytes" number:2 label:LABEL_OPTIONAL type:TYPE_STRING},
1561 {name:"repeated_string" number:3 label:LABEL_REPEATED type:TYPE_STRING},
1562 {name:"repeated_bytes" number:4 label:LABEL_REPEATED type:TYPE_STRING},
1563 {name:"oneof_string" number:5 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0},
1564 {name:"oneof_bytes" number:6 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0}
1565 ]
1566 oneof_decl: [{name:"oneof_field"}]
1567 }]
1568 `), pb); err != nil {
1569 panic(err)
1570 }
1571 fd, err := protodesc.NewFile(pb, nil)
1572 if err != nil {
1573 panic(err)
1574 }
1575 md := fd.Messages().Get(0)
1576 for i := 0; i < md.Fields().Len(); i++ {
1577 md.Fields().Get(i).(*filedesc.Field).L1.HasEnforceUTF8 = true
1578 md.Fields().Get(i).(*filedesc.Field).L1.EnforceUTF8 = false
1579 }
1580 return md
1581 }(),
1582 NewMessage: func() pref.Message {
1583 return pref.ProtoMessage(new(TestNoEnforceUTF8)).ProtoReflect()
1584 },
1585 },
1586 OneofWrappers: []interface{}{
1587 (*TestNoEnforceUTF8_OneofString)(nil),
1588 (*TestNoEnforceUTF8_OneofBytes)(nil),
1589 },
1590}
1591
Damien Neil4be2fb42018-12-17 11:16:16 -08001592func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001593 for _, opt := range opts {
1594 opt(m)
1595 }
1596 return m
1597}
1598
Damien Neil4be2fb42018-12-17 11:16:16 -08001599type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001600
Joe Tsai378c1322019-04-25 23:48:08 -07001601func unknown(raw pref.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001602 return func(m proto.Message) {
Joe Tsai378c1322019-04-25 23:48:08 -07001603 m.ProtoReflect().SetUnknown(raw)
Damien Neile6f060f2019-04-23 17:11:02 -07001604 }
1605}
1606
Damien Neilba23aa52018-12-07 14:38:17 -08001607func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001608 // TODO: Should ExtensionType.ValueOf accept []T instead of *[]T?
1609 t := reflect.TypeOf(value)
1610 if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
1611 v := reflect.New(t)
1612 v.Elem().Set(reflect.ValueOf(value))
1613 value = v.Interface()
1614 }
1615
Damien Neil4be2fb42018-12-17 11:16:16 -08001616 return func(m proto.Message) {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001617 xt := desc.Type
1618 m.ProtoReflect().Set(xt, xt.ValueOf(value))
Damien Neilba23aa52018-12-07 14:38:17 -08001619 }
1620}
Damien Neil61e93c72019-03-27 09:23:20 -07001621
1622func marshalText(m proto.Message) string {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001623 b, _ := prototext.MarshalOptions{Indent: "\t", AllowPartial: true}.Marshal(m)
Damien Neil61e93c72019-03-27 09:23:20 -07001624 return string(b)
1625}