blob: a01da16f1f8be2e72d1f8dd24daa29efee085a65 [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/runtime/protoimpl"
Joe Tsai19058432019-02-27 21:46:29 -080020
Damien Neilc37adef2019-04-01 13:49:56 -070021 legacypb "google.golang.org/protobuf/internal/testprotos/legacy"
22 legacy1pb "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v0.0.0-20160225-2fc053c5"
Damien Neile89e6242019-05-13 23:55:40 -070023 testpb "google.golang.org/protobuf/internal/testprotos/test"
24 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Joe Tsaic51e2e02019-07-13 00:44:41 -070025 "google.golang.org/protobuf/types/descriptorpb"
Damien Neilba23aa52018-12-07 14:38:17 -080026)
27
28type testProto struct {
Joe Tsai09cef322019-07-11 22:13:49 -070029 desc string
30 decodeTo []proto.Message
31 wire []byte
32 partial bool
Damien Neilba23aa52018-12-07 14:38:17 -080033}
34
35func TestDecode(t *testing.T) {
36 for _, test := range testProtos {
37 for _, want := range test.decodeTo {
38 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070039 opts := proto.UnmarshalOptions{
40 AllowPartial: test.partial,
41 }
Damien Neilba23aa52018-12-07 14:38:17 -080042 wire := append(([]byte)(nil), test.wire...)
Damien Neil4be2fb42018-12-17 11:16:16 -080043 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
Damien Neil96c229a2019-04-03 12:17:24 -070044 if err := opts.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070045 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080046 return
47 }
48
49 // Aliasing check: Modifying the original wire bytes shouldn't
50 // affect the unmarshaled message.
51 for i := range wire {
52 wire[i] = 0
53 }
Joe Tsaidb38ddd2019-05-07 15:14:40 -070054 if !proto.Equal(got, want) {
Damien Neil61e93c72019-03-27 09:23:20 -070055 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080056 }
57 })
58 }
59 }
60}
61
Damien Neil96c229a2019-04-03 12:17:24 -070062func TestDecodeRequiredFieldChecks(t *testing.T) {
63 for _, test := range testProtos {
64 if !test.partial {
65 continue
66 }
Damien Neil96c229a2019-04-03 12:17:24 -070067 for _, m := range test.decodeTo {
68 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
69 got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
70 if err := proto.Unmarshal(test.wire, got); err == nil {
71 t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got))
72 }
73 })
74 }
75 }
76}
77
Damien Neilbc310b52019-04-11 11:46:55 -070078func TestDecodeInvalidUTF8(t *testing.T) {
79 for _, test := range invalidUTF8TestProtos {
80 for _, want := range test.decodeTo {
81 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
82 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
83 err := proto.Unmarshal(test.wire, got)
Damien Neil8c86fc52019-06-19 09:28:29 -070084 if err == nil {
Damien Neilbc310b52019-04-11 11:46:55 -070085 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
86 }
Damien Neilbc310b52019-04-11 11:46:55 -070087 })
88 }
89 }
90}
91
Joe Tsaic51e2e02019-07-13 00:44:41 -070092func TestDecodeNoEnforceUTF8(t *testing.T) {
93 for _, test := range noEnforceUTF8TestProtos {
94 for _, want := range test.decodeTo {
95 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
96 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
97 err := proto.Unmarshal(test.wire, got)
98 switch {
Joe Tsai1799d112019-08-08 13:31:59 -070099 case flags.ProtoLegacy && err != nil:
Joe Tsaic51e2e02019-07-13 00:44:41 -0700100 t.Errorf("Unmarshal returned unexpected error: %v\nMessage:\n%v", err, marshalText(want))
Joe Tsai1799d112019-08-08 13:31:59 -0700101 case !flags.ProtoLegacy && err == nil:
Joe Tsaic51e2e02019-07-13 00:44:41 -0700102 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
103 }
104 })
105 }
106 }
107}
108
Damien Neil8003f082019-08-02 15:13:00 -0700109func TestDecodeZeroLengthBytes(t *testing.T) {
110 // Verify that proto3 bytes fields don't give the mistaken
111 // impression that they preserve presence.
112 wire := pack.Message{
113 pack.Tag{15, pack.BytesType}, pack.Bytes(nil),
114 }.Marshal()
115 m := &test3pb.TestAllTypes{}
116 if err := proto.Unmarshal(wire, m); err != nil {
117 t.Fatal(err)
118 }
119 if m.OptionalBytes != nil {
120 t.Errorf("unmarshal zero-length proto3 bytes field: got %v, want nil", m.OptionalBytes)
121 }
122}
123
Joe Tsai9b22b932019-08-08 19:23:32 -0700124func TestDecodeOneofNilWrapper(t *testing.T) {
125 wire := pack.Message{
126 pack.Tag{111, pack.VarintType}, pack.Varint(1111),
127 }.Marshal()
128 m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)}
129 if err := proto.Unmarshal(wire, m); err != nil {
130 t.Fatal(err)
131 }
132 if got := m.GetOneofUint32(); got != 1111 {
133 t.Errorf("GetOneofUint32() = %v, want %v", got, 1111)
134 }
135}
136
Tuo Shan6e25d8c2019-08-22 18:52:43 -0700137func TestDecodeInvalidFieldNumbers(t *testing.T) {
138 for _, test := range invalidFieldNumberTestProtos {
139 t.Run(test.desc, func(t *testing.T) {
140 decoded := new(testpb.TestAllTypes) // type doesn't matter since we expect errors
141 err := proto.Unmarshal(test.wire, decoded)
142 if err == nil && !test.allowed {
143 t.Error("unmarshal: got nil want error")
144 } else if err != nil && test.allowed {
145 t.Errorf("unmarshal: got %v want nil since %s is allowed by Unmarshal", err, test.desc)
146 }
147 })
148 }
149}
150
Damien Neilba23aa52018-12-07 14:38:17 -0800151var testProtos = []testProto{
152 {
153 desc: "basic scalar types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800154 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700155 OptionalInt32: proto.Int32(1001),
156 OptionalInt64: proto.Int64(1002),
157 OptionalUint32: proto.Uint32(1003),
158 OptionalUint64: proto.Uint64(1004),
159 OptionalSint32: proto.Int32(1005),
160 OptionalSint64: proto.Int64(1006),
161 OptionalFixed32: proto.Uint32(1007),
162 OptionalFixed64: proto.Uint64(1008),
163 OptionalSfixed32: proto.Int32(1009),
164 OptionalSfixed64: proto.Int64(1010),
165 OptionalFloat: proto.Float32(1011.5),
166 OptionalDouble: proto.Float64(1012.5),
167 OptionalBool: proto.Bool(true),
168 OptionalString: proto.String("string"),
Damien Neilba23aa52018-12-07 14:38:17 -0800169 OptionalBytes: []byte("bytes"),
170 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
Damien Neil3b46ade2019-03-26 13:55:02 -0700171 }, &test3pb.TestAllTypes{
172 OptionalInt32: 1001,
173 OptionalInt64: 1002,
174 OptionalUint32: 1003,
175 OptionalUint64: 1004,
176 OptionalSint32: 1005,
177 OptionalSint64: 1006,
178 OptionalFixed32: 1007,
179 OptionalFixed64: 1008,
180 OptionalSfixed32: 1009,
181 OptionalSfixed64: 1010,
182 OptionalFloat: 1011.5,
183 OptionalDouble: 1012.5,
184 OptionalBool: true,
185 OptionalString: "string",
186 OptionalBytes: []byte("bytes"),
187 OptionalNestedEnum: test3pb.TestAllTypes_BAR,
Damien Neilba23aa52018-12-07 14:38:17 -0800188 }, build(
189 &testpb.TestAllExtensions{},
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700190 extend(testpb.E_OptionalInt32Extension, int32(1001)),
191 extend(testpb.E_OptionalInt64Extension, int64(1002)),
192 extend(testpb.E_OptionalUint32Extension, uint32(1003)),
193 extend(testpb.E_OptionalUint64Extension, uint64(1004)),
194 extend(testpb.E_OptionalSint32Extension, int32(1005)),
195 extend(testpb.E_OptionalSint64Extension, int64(1006)),
196 extend(testpb.E_OptionalFixed32Extension, uint32(1007)),
197 extend(testpb.E_OptionalFixed64Extension, uint64(1008)),
198 extend(testpb.E_OptionalSfixed32Extension, int32(1009)),
199 extend(testpb.E_OptionalSfixed64Extension, int64(1010)),
200 extend(testpb.E_OptionalFloatExtension, float32(1011.5)),
201 extend(testpb.E_OptionalDoubleExtension, float64(1012.5)),
202 extend(testpb.E_OptionalBoolExtension, bool(true)),
203 extend(testpb.E_OptionalStringExtension, string("string")),
Damien Neilba23aa52018-12-07 14:38:17 -0800204 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700205 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR),
Damien Neilba23aa52018-12-07 14:38:17 -0800206 )},
207 wire: pack.Message{
208 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
209 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
210 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
211 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
212 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
213 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
214 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
215 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
216 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
217 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
218 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
219 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
220 pack.Tag{13, pack.VarintType}, pack.Bool(true),
221 pack.Tag{14, pack.BytesType}, pack.String("string"),
222 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
223 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
224 }.Marshal(),
225 },
226 {
Damien Neil8003f082019-08-02 15:13:00 -0700227 desc: "zero values",
228 decodeTo: []proto.Message{&testpb.TestAllTypes{
229 OptionalInt32: proto.Int32(0),
230 OptionalInt64: proto.Int64(0),
231 OptionalUint32: proto.Uint32(0),
232 OptionalUint64: proto.Uint64(0),
233 OptionalSint32: proto.Int32(0),
234 OptionalSint64: proto.Int64(0),
235 OptionalFixed32: proto.Uint32(0),
236 OptionalFixed64: proto.Uint64(0),
237 OptionalSfixed32: proto.Int32(0),
238 OptionalSfixed64: proto.Int64(0),
239 OptionalFloat: proto.Float32(0),
240 OptionalDouble: proto.Float64(0),
241 OptionalBool: proto.Bool(false),
242 OptionalString: proto.String(""),
243 OptionalBytes: []byte{},
244 }, &test3pb.TestAllTypes{}, build(
245 &testpb.TestAllExtensions{},
246 extend(testpb.E_OptionalInt32Extension, int32(0)),
247 extend(testpb.E_OptionalInt64Extension, int64(0)),
248 extend(testpb.E_OptionalUint32Extension, uint32(0)),
249 extend(testpb.E_OptionalUint64Extension, uint64(0)),
250 extend(testpb.E_OptionalSint32Extension, int32(0)),
251 extend(testpb.E_OptionalSint64Extension, int64(0)),
252 extend(testpb.E_OptionalFixed32Extension, uint32(0)),
253 extend(testpb.E_OptionalFixed64Extension, uint64(0)),
254 extend(testpb.E_OptionalSfixed32Extension, int32(0)),
255 extend(testpb.E_OptionalSfixed64Extension, int64(0)),
256 extend(testpb.E_OptionalFloatExtension, float32(0)),
257 extend(testpb.E_OptionalDoubleExtension, float64(0)),
258 extend(testpb.E_OptionalBoolExtension, bool(false)),
259 extend(testpb.E_OptionalStringExtension, string("")),
260 extend(testpb.E_OptionalBytesExtension, []byte{}),
261 )},
262 wire: pack.Message{
263 pack.Tag{1, pack.VarintType}, pack.Varint(0),
264 pack.Tag{2, pack.VarintType}, pack.Varint(0),
265 pack.Tag{3, pack.VarintType}, pack.Uvarint(0),
266 pack.Tag{4, pack.VarintType}, pack.Uvarint(0),
267 pack.Tag{5, pack.VarintType}, pack.Svarint(0),
268 pack.Tag{6, pack.VarintType}, pack.Svarint(0),
269 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(0),
270 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(0),
271 pack.Tag{9, pack.Fixed32Type}, pack.Int32(0),
272 pack.Tag{10, pack.Fixed64Type}, pack.Int64(0),
273 pack.Tag{11, pack.Fixed32Type}, pack.Float32(0),
274 pack.Tag{12, pack.Fixed64Type}, pack.Float64(0),
275 pack.Tag{13, pack.VarintType}, pack.Bool(false),
276 pack.Tag{14, pack.BytesType}, pack.String(""),
277 pack.Tag{15, pack.BytesType}, pack.Bytes(nil),
278 }.Marshal(),
279 },
280 {
Damien Neilba23aa52018-12-07 14:38:17 -0800281 desc: "groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800282 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800283 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700284 A: proto.Int32(1017),
Damien Neilba23aa52018-12-07 14:38:17 -0800285 },
286 }, build(
287 &testpb.TestAllExtensions{},
288 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700289 A: proto.Int32(1017),
Damien Neilba23aa52018-12-07 14:38:17 -0800290 }),
291 )},
292 wire: pack.Message{
293 pack.Tag{16, pack.StartGroupType},
294 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
295 pack.Tag{16, pack.EndGroupType},
296 }.Marshal(),
297 },
298 {
299 desc: "groups (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800300 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800301 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700302 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800303 },
304 }, build(
305 &testpb.TestAllExtensions{},
306 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700307 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800308 }),
309 )},
310 wire: pack.Message{
311 pack.Tag{16, pack.StartGroupType},
312 pack.Tag{17, pack.VarintType}, pack.Varint(1),
313 pack.Tag{16, pack.EndGroupType},
314 pack.Tag{16, pack.StartGroupType},
315 pack.Tag{17, pack.VarintType}, pack.Varint(2),
316 pack.Tag{16, pack.EndGroupType},
317 }.Marshal(),
318 },
319 {
320 desc: "messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800321 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800322 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700323 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800324 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700325 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800326 },
327 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700328 }, &test3pb.TestAllTypes{
329 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
330 A: 42,
331 Corecursive: &test3pb.TestAllTypes{
332 OptionalInt32: 43,
333 },
334 },
Damien Neilba23aa52018-12-07 14:38:17 -0800335 }, build(
336 &testpb.TestAllExtensions{},
337 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700338 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800339 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700340 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800341 },
342 }),
343 )},
344 wire: pack.Message{
345 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
346 pack.Tag{1, pack.VarintType}, pack.Varint(42),
347 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
348 pack.Tag{1, pack.VarintType}, pack.Varint(43),
349 }),
350 }),
351 }.Marshal(),
352 },
353 {
354 desc: "messages (split across multiple tags)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800355 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800356 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700357 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800358 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700359 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800360 },
361 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700362 }, &test3pb.TestAllTypes{
363 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
364 A: 42,
365 Corecursive: &test3pb.TestAllTypes{
366 OptionalInt32: 43,
367 },
368 },
Damien Neilba23aa52018-12-07 14:38:17 -0800369 }, build(
370 &testpb.TestAllExtensions{},
371 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700372 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800373 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700374 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800375 },
376 }),
377 )},
378 wire: pack.Message{
379 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
380 pack.Tag{1, pack.VarintType}, pack.Varint(42),
381 }),
382 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
383 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
384 pack.Tag{1, pack.VarintType}, pack.Varint(43),
385 }),
386 }),
387 }.Marshal(),
388 },
389 {
390 desc: "messages (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800391 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800392 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700393 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800394 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700395 }, &test3pb.TestAllTypes{
396 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
397 A: 2,
398 },
Damien Neilba23aa52018-12-07 14:38:17 -0800399 }, build(
400 &testpb.TestAllExtensions{},
401 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700402 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800403 }),
404 )},
405 wire: pack.Message{
406 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
407 pack.Tag{1, pack.VarintType}, pack.Varint(1),
408 }),
409 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
410 pack.Tag{1, pack.VarintType}, pack.Varint(2),
411 }),
412 }.Marshal(),
413 },
414 {
415 desc: "basic repeated types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800416 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800417 RepeatedInt32: []int32{1001, 2001},
418 RepeatedInt64: []int64{1002, 2002},
419 RepeatedUint32: []uint32{1003, 2003},
420 RepeatedUint64: []uint64{1004, 2004},
421 RepeatedSint32: []int32{1005, 2005},
422 RepeatedSint64: []int64{1006, 2006},
423 RepeatedFixed32: []uint32{1007, 2007},
424 RepeatedFixed64: []uint64{1008, 2008},
425 RepeatedSfixed32: []int32{1009, 2009},
426 RepeatedSfixed64: []int64{1010, 2010},
427 RepeatedFloat: []float32{1011.5, 2011.5},
428 RepeatedDouble: []float64{1012.5, 2012.5},
429 RepeatedBool: []bool{true, false},
430 RepeatedString: []string{"foo", "bar"},
431 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
432 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
433 testpb.TestAllTypes_FOO,
434 testpb.TestAllTypes_BAR,
435 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700436 }, &test3pb.TestAllTypes{
437 RepeatedInt32: []int32{1001, 2001},
438 RepeatedInt64: []int64{1002, 2002},
439 RepeatedUint32: []uint32{1003, 2003},
440 RepeatedUint64: []uint64{1004, 2004},
441 RepeatedSint32: []int32{1005, 2005},
442 RepeatedSint64: []int64{1006, 2006},
443 RepeatedFixed32: []uint32{1007, 2007},
444 RepeatedFixed64: []uint64{1008, 2008},
445 RepeatedSfixed32: []int32{1009, 2009},
446 RepeatedSfixed64: []int64{1010, 2010},
447 RepeatedFloat: []float32{1011.5, 2011.5},
448 RepeatedDouble: []float64{1012.5, 2012.5},
449 RepeatedBool: []bool{true, false},
450 RepeatedString: []string{"foo", "bar"},
451 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
452 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
453 test3pb.TestAllTypes_FOO,
454 test3pb.TestAllTypes_BAR,
455 },
Damien Neilba23aa52018-12-07 14:38:17 -0800456 }, build(
457 &testpb.TestAllExtensions{},
458 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
459 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
460 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
461 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
462 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
463 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
464 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
465 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
466 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
467 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
468 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
469 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
470 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
471 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
472 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
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.VarintType}, pack.Varint(1001),
480 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
481 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
482 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
483 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
484 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
485 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
486 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
487 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
488 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
489 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
490 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
491 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
492 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
493 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
494 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
495 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
496 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
497 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
498 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
499 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
500 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
501 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
502 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
503 pack.Tag{43, pack.VarintType}, pack.Bool(true),
504 pack.Tag{43, pack.VarintType}, pack.Bool(false),
505 pack.Tag{44, pack.BytesType}, pack.String("foo"),
506 pack.Tag{44, pack.BytesType}, pack.String("bar"),
507 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
508 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
509 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
510 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
511 }.Marshal(),
512 },
513 {
514 desc: "basic repeated types (packed encoding)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800515 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800516 RepeatedInt32: []int32{1001, 2001},
517 RepeatedInt64: []int64{1002, 2002},
518 RepeatedUint32: []uint32{1003, 2003},
519 RepeatedUint64: []uint64{1004, 2004},
520 RepeatedSint32: []int32{1005, 2005},
521 RepeatedSint64: []int64{1006, 2006},
522 RepeatedFixed32: []uint32{1007, 2007},
523 RepeatedFixed64: []uint64{1008, 2008},
524 RepeatedSfixed32: []int32{1009, 2009},
525 RepeatedSfixed64: []int64{1010, 2010},
526 RepeatedFloat: []float32{1011.5, 2011.5},
527 RepeatedDouble: []float64{1012.5, 2012.5},
528 RepeatedBool: []bool{true, false},
529 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
530 testpb.TestAllTypes_FOO,
531 testpb.TestAllTypes_BAR,
532 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700533 }, &test3pb.TestAllTypes{
534 RepeatedInt32: []int32{1001, 2001},
535 RepeatedInt64: []int64{1002, 2002},
536 RepeatedUint32: []uint32{1003, 2003},
537 RepeatedUint64: []uint64{1004, 2004},
538 RepeatedSint32: []int32{1005, 2005},
539 RepeatedSint64: []int64{1006, 2006},
540 RepeatedFixed32: []uint32{1007, 2007},
541 RepeatedFixed64: []uint64{1008, 2008},
542 RepeatedSfixed32: []int32{1009, 2009},
543 RepeatedSfixed64: []int64{1010, 2010},
544 RepeatedFloat: []float32{1011.5, 2011.5},
545 RepeatedDouble: []float64{1012.5, 2012.5},
546 RepeatedBool: []bool{true, false},
547 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
548 test3pb.TestAllTypes_FOO,
549 test3pb.TestAllTypes_BAR,
550 },
Damien Neilba23aa52018-12-07 14:38:17 -0800551 }, build(
552 &testpb.TestAllExtensions{},
553 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
554 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
555 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
556 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
557 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
558 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
559 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
560 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
561 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
562 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
563 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
564 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
565 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
566 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
567 testpb.TestAllTypes_FOO,
568 testpb.TestAllTypes_BAR,
569 }),
570 )},
571 wire: pack.Message{
572 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
573 pack.Varint(1001), pack.Varint(2001),
574 },
575 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
576 pack.Varint(1002), pack.Varint(2002),
577 },
578 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
579 pack.Uvarint(1003), pack.Uvarint(2003),
580 },
581 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
582 pack.Uvarint(1004), pack.Uvarint(2004),
583 },
584 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
585 pack.Svarint(1005), pack.Svarint(2005),
586 },
587 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
588 pack.Svarint(1006), pack.Svarint(2006),
589 },
590 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
591 pack.Uint32(1007), pack.Uint32(2007),
592 },
593 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
594 pack.Uint64(1008), pack.Uint64(2008),
595 },
596 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
597 pack.Int32(1009), pack.Int32(2009),
598 },
599 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
600 pack.Int64(1010), pack.Int64(2010),
601 },
602 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
603 pack.Float32(1011.5), pack.Float32(2011.5),
604 },
605 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
606 pack.Float64(1012.5), pack.Float64(2012.5),
607 },
608 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
609 pack.Bool(true), pack.Bool(false),
610 },
611 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
612 pack.Varint(int(testpb.TestAllTypes_FOO)),
613 pack.Varint(int(testpb.TestAllTypes_BAR)),
614 },
615 }.Marshal(),
616 },
617 {
Damien Neila0a54b82019-11-01 15:18:36 -0700618 desc: "basic repeated types (zero-length packed encoding)",
619 decodeTo: []proto.Message{
620 &testpb.TestAllTypes{},
621 &test3pb.TestAllTypes{},
622 &testpb.TestAllExtensions{},
623 },
624 wire: pack.Message{
625 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{},
626 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{},
627 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{},
628 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{},
629 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{},
630 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{},
631 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{},
632 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{},
633 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{},
634 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{},
635 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{},
636 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{},
637 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{},
638 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{},
639 }.Marshal(),
640 },
641 {
Damien Neil7492a092019-07-10 15:23:29 -0700642 desc: "packed repeated types",
643 decodeTo: []proto.Message{&testpb.TestPackedTypes{
644 PackedInt32: []int32{1001, 2001},
645 PackedInt64: []int64{1002, 2002},
646 PackedUint32: []uint32{1003, 2003},
647 PackedUint64: []uint64{1004, 2004},
648 PackedSint32: []int32{1005, 2005},
649 PackedSint64: []int64{1006, 2006},
650 PackedFixed32: []uint32{1007, 2007},
651 PackedFixed64: []uint64{1008, 2008},
652 PackedSfixed32: []int32{1009, 2009},
653 PackedSfixed64: []int64{1010, 2010},
654 PackedFloat: []float32{1011.5, 2011.5},
655 PackedDouble: []float64{1012.5, 2012.5},
656 PackedBool: []bool{true, false},
657 PackedEnum: []testpb.ForeignEnum{
658 testpb.ForeignEnum_FOREIGN_FOO,
659 testpb.ForeignEnum_FOREIGN_BAR,
660 },
661 }, build(
662 &testpb.TestPackedExtensions{},
663 extend(testpb.E_PackedInt32Extension, []int32{1001, 2001}),
664 extend(testpb.E_PackedInt64Extension, []int64{1002, 2002}),
665 extend(testpb.E_PackedUint32Extension, []uint32{1003, 2003}),
666 extend(testpb.E_PackedUint64Extension, []uint64{1004, 2004}),
667 extend(testpb.E_PackedSint32Extension, []int32{1005, 2005}),
668 extend(testpb.E_PackedSint64Extension, []int64{1006, 2006}),
669 extend(testpb.E_PackedFixed32Extension, []uint32{1007, 2007}),
670 extend(testpb.E_PackedFixed64Extension, []uint64{1008, 2008}),
671 extend(testpb.E_PackedSfixed32Extension, []int32{1009, 2009}),
672 extend(testpb.E_PackedSfixed64Extension, []int64{1010, 2010}),
673 extend(testpb.E_PackedFloatExtension, []float32{1011.5, 2011.5}),
674 extend(testpb.E_PackedDoubleExtension, []float64{1012.5, 2012.5}),
675 extend(testpb.E_PackedBoolExtension, []bool{true, false}),
676 extend(testpb.E_PackedEnumExtension, []testpb.ForeignEnum{
677 testpb.ForeignEnum_FOREIGN_FOO,
678 testpb.ForeignEnum_FOREIGN_BAR,
679 }),
680 )},
681 wire: pack.Message{
682 pack.Tag{90, pack.BytesType}, pack.LengthPrefix{
683 pack.Varint(1001), pack.Varint(2001),
684 },
685 pack.Tag{91, pack.BytesType}, pack.LengthPrefix{
686 pack.Varint(1002), pack.Varint(2002),
687 },
688 pack.Tag{92, pack.BytesType}, pack.LengthPrefix{
689 pack.Uvarint(1003), pack.Uvarint(2003),
690 },
691 pack.Tag{93, pack.BytesType}, pack.LengthPrefix{
692 pack.Uvarint(1004), pack.Uvarint(2004),
693 },
694 pack.Tag{94, pack.BytesType}, pack.LengthPrefix{
695 pack.Svarint(1005), pack.Svarint(2005),
696 },
697 pack.Tag{95, pack.BytesType}, pack.LengthPrefix{
698 pack.Svarint(1006), pack.Svarint(2006),
699 },
700 pack.Tag{96, pack.BytesType}, pack.LengthPrefix{
701 pack.Uint32(1007), pack.Uint32(2007),
702 },
703 pack.Tag{97, pack.BytesType}, pack.LengthPrefix{
704 pack.Uint64(1008), pack.Uint64(2008),
705 },
706 pack.Tag{98, pack.BytesType}, pack.LengthPrefix{
707 pack.Int32(1009), pack.Int32(2009),
708 },
709 pack.Tag{99, pack.BytesType}, pack.LengthPrefix{
710 pack.Int64(1010), pack.Int64(2010),
711 },
712 pack.Tag{100, pack.BytesType}, pack.LengthPrefix{
713 pack.Float32(1011.5), pack.Float32(2011.5),
714 },
715 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{
716 pack.Float64(1012.5), pack.Float64(2012.5),
717 },
718 pack.Tag{102, pack.BytesType}, pack.LengthPrefix{
719 pack.Bool(true), pack.Bool(false),
720 },
721 pack.Tag{103, pack.BytesType}, pack.LengthPrefix{
722 pack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
723 pack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
724 },
725 }.Marshal(),
726 },
727 {
Damien Neila0a54b82019-11-01 15:18:36 -0700728 desc: "packed repeated types (zero length)",
729 decodeTo: []proto.Message{
730 &testpb.TestPackedTypes{},
731 &testpb.TestPackedExtensions{},
732 },
733 wire: pack.Message{
734 pack.Tag{90, pack.BytesType}, pack.LengthPrefix{},
735 pack.Tag{91, pack.BytesType}, pack.LengthPrefix{},
736 pack.Tag{92, pack.BytesType}, pack.LengthPrefix{},
737 pack.Tag{93, pack.BytesType}, pack.LengthPrefix{},
738 pack.Tag{94, pack.BytesType}, pack.LengthPrefix{},
739 pack.Tag{95, pack.BytesType}, pack.LengthPrefix{},
740 pack.Tag{96, pack.BytesType}, pack.LengthPrefix{},
741 pack.Tag{97, pack.BytesType}, pack.LengthPrefix{},
742 pack.Tag{98, pack.BytesType}, pack.LengthPrefix{},
743 pack.Tag{99, pack.BytesType}, pack.LengthPrefix{},
744 pack.Tag{100, pack.BytesType}, pack.LengthPrefix{},
745 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{},
746 pack.Tag{102, pack.BytesType}, pack.LengthPrefix{},
747 pack.Tag{103, pack.BytesType}, pack.LengthPrefix{},
748 }.Marshal(),
749 },
750 {
Damien Neilba23aa52018-12-07 14:38:17 -0800751 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800752 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800753 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700754 {A: proto.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700755 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700756 {A: proto.Int32(2)},
Damien Neilba23aa52018-12-07 14:38:17 -0800757 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700758 }, &test3pb.TestAllTypes{
759 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
760 {A: 1},
Damien Neilc37adef2019-04-01 13:49:56 -0700761 nil,
Damien Neil3b46ade2019-03-26 13:55:02 -0700762 {A: 2},
763 },
Damien Neilba23aa52018-12-07 14:38:17 -0800764 }, build(
765 &testpb.TestAllExtensions{},
766 extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700767 {A: proto.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700768 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700769 {A: proto.Int32(2)},
Damien Neilba23aa52018-12-07 14:38:17 -0800770 }),
771 )},
772 wire: pack.Message{
773 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
774 pack.Tag{1, pack.VarintType}, pack.Varint(1),
775 }),
Damien Neilc37adef2019-04-01 13:49:56 -0700776 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
Damien Neilba23aa52018-12-07 14:38:17 -0800777 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
778 pack.Tag{1, pack.VarintType}, pack.Varint(2),
779 }),
780 }.Marshal(),
781 },
782 {
783 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800784 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800785 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700786 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700787 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700788 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800789 },
790 }, build(
791 &testpb.TestAllExtensions{},
792 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700793 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700794 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700795 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800796 }),
797 )},
798 wire: pack.Message{
799 pack.Tag{46, pack.StartGroupType},
800 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
801 pack.Tag{46, pack.EndGroupType},
802 pack.Tag{46, pack.StartGroupType},
Damien Neilc37adef2019-04-01 13:49:56 -0700803 pack.Tag{46, pack.EndGroupType},
804 pack.Tag{46, pack.StartGroupType},
Damien Neilba23aa52018-12-07 14:38:17 -0800805 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
806 pack.Tag{46, pack.EndGroupType},
807 }.Marshal(),
808 },
809 {
810 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800811 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800812 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
813 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
814 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
815 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
816 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
817 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
818 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
819 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
820 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
821 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
822 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
823 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
824 MapBoolBool: map[bool]bool{true: false, false: true},
825 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
826 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
827 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700828 "71.1.key": {A: proto.Int32(1171)},
829 "71.2.key": {A: proto.Int32(2171)},
Damien Neilba23aa52018-12-07 14:38:17 -0800830 },
831 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
832 "73.1.key": testpb.TestAllTypes_FOO,
833 "73.2.key": testpb.TestAllTypes_BAR,
834 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700835 }, &test3pb.TestAllTypes{
836 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
837 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
838 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
839 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
840 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
841 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
842 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
843 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
844 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
845 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
846 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
847 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
848 MapBoolBool: map[bool]bool{true: false, false: true},
849 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
850 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
851 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
852 "71.1.key": {A: 1171},
853 "71.2.key": {A: 2171},
854 },
855 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
856 "73.1.key": test3pb.TestAllTypes_FOO,
857 "73.2.key": test3pb.TestAllTypes_BAR,
858 },
Damien Neilba23aa52018-12-07 14:38:17 -0800859 }},
860 wire: pack.Message{
861 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
862 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
863 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
864 }),
865 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
866 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
867 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
868 }),
869 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
870 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
871 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
872 }),
873 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
874 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
875 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
876 }),
877 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
878 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
879 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
880 }),
881 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
882 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
883 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
884 }),
885 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
886 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
887 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
888 }),
889 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
890 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
891 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
892 }),
893 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
894 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
895 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
896 }),
897 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
898 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
899 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
900 }),
901 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
902 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
903 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
904 }),
905 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
906 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
907 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
908 }),
909 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
910 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
911 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
912 }),
913 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
914 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
915 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
916 }),
917 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
918 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
919 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
920 }),
921 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
922 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
923 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
924 }),
925 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
926 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
927 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
928 }),
929 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
930 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
931 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
932 }),
933 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
934 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
935 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
936 }),
937 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
938 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
939 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
940 }),
941 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
942 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
943 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
944 }),
945 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
946 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
947 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
948 }),
949 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
950 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
951 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
952 }),
953 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
954 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
955 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
956 }),
957 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
958 pack.Tag{1, pack.VarintType}, pack.Bool(true),
959 pack.Tag{2, pack.VarintType}, pack.Bool(false),
960 }),
961 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
962 pack.Tag{1, pack.VarintType}, pack.Bool(false),
963 pack.Tag{2, pack.VarintType}, pack.Bool(true),
964 }),
965 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
966 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
967 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
968 }),
969 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
970 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
971 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
972 }),
973 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
974 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
975 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
976 }),
977 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
978 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
979 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
980 }),
981 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
982 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
983 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
984 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
985 }),
986 }),
987 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
988 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
989 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
990 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
991 }),
992 }),
993 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
994 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
995 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
996 }),
997 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
998 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
999 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
1000 }),
1001 }.Marshal(),
1002 },
1003 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001004 desc: "oneof (uint32)",
1005 decodeTo: []proto.Message{
1006 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
1007 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
1008 },
1009 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001010 },
1011 {
1012 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -07001013 decodeTo: []proto.Message{
1014 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001015 &testpb.TestAllTypes_NestedMessage{A: proto.Int32(1112)},
Damien Neil3b46ade2019-03-26 13:55:02 -07001016 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
1017 &test3pb.TestAllTypes_NestedMessage{A: 1112},
1018 }},
1019 },
Damien Neilba23aa52018-12-07 14:38:17 -08001020 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
1021 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
1022 })}.Marshal(),
1023 },
1024 {
Damien Neilc37adef2019-04-01 13:49:56 -07001025 desc: "oneof (empty message)",
1026 decodeTo: []proto.Message{
1027 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
1028 &testpb.TestAllTypes_NestedMessage{},
1029 }},
1030 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
1031 &test3pb.TestAllTypes_NestedMessage{},
1032 }},
1033 },
1034 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1035 },
1036 {
Joe Tsai6c286742019-07-11 23:15:05 -07001037 desc: "oneof (merged message)",
Damien Neil3b46ade2019-03-26 13:55:02 -07001038 decodeTo: []proto.Message{
1039 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
1040 &testpb.TestAllTypes_NestedMessage{
Joe Tsai0f81b382019-07-10 23:14:31 -07001041 A: proto.Int32(1),
Damien Neil3b46ade2019-03-26 13:55:02 -07001042 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -07001043 OptionalInt32: proto.Int32(43),
Damien Neil3b46ade2019-03-26 13:55:02 -07001044 },
Damien Neilba23aa52018-12-07 14:38:17 -08001045 },
Damien Neil3b46ade2019-03-26 13:55:02 -07001046 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
1047 &test3pb.TestAllTypes_NestedMessage{
Joe Tsai6c286742019-07-11 23:15:05 -07001048 A: 1,
Damien Neil3b46ade2019-03-26 13:55:02 -07001049 Corecursive: &test3pb.TestAllTypes{
1050 OptionalInt32: 43,
1051 },
1052 },
1053 }}},
Damien Neilba23aa52018-12-07 14:38:17 -08001054 wire: pack.Message{
1055 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
1056 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
1057 }),
1058 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
1059 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1060 pack.Tag{1, pack.VarintType}, pack.Varint(43),
1061 }),
1062 }),
1063 }.Marshal(),
1064 },
1065 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001066 desc: "oneof (string)",
1067 decodeTo: []proto.Message{
1068 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
1069 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
1070 },
1071 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001072 },
1073 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001074 desc: "oneof (bytes)",
1075 decodeTo: []proto.Message{
1076 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
1077 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
1078 },
1079 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001080 },
1081 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001082 desc: "oneof (bool)",
1083 decodeTo: []proto.Message{
1084 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
1085 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
1086 },
1087 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001088 },
1089 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001090 desc: "oneof (uint64)",
1091 decodeTo: []proto.Message{
1092 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
1093 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
1094 },
1095 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001096 },
1097 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001098 desc: "oneof (float)",
1099 decodeTo: []proto.Message{
1100 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
1101 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
1102 },
1103 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001104 },
1105 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001106 desc: "oneof (double)",
1107 decodeTo: []proto.Message{
1108 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
1109 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
1110 },
1111 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001112 },
1113 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001114 desc: "oneof (enum)",
1115 decodeTo: []proto.Message{
1116 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
1117 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
1118 },
1119 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001120 },
1121 {
Damien Neilc37adef2019-04-01 13:49:56 -07001122 desc: "oneof (zero)",
1123 decodeTo: []proto.Message{
1124 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}},
1125 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}},
1126 },
1127 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
1128 },
1129 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001130 desc: "oneof (overridden value)",
1131 decodeTo: []proto.Message{
1132 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
1133 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
1134 },
Damien Neilba23aa52018-12-07 14:38:17 -08001135 wire: pack.Message{
1136 pack.Tag{111, pack.VarintType}, pack.Varint(1),
1137 pack.Tag{116, pack.VarintType}, pack.Varint(2),
1138 }.Marshal(),
1139 },
1140 // TODO: More unknown field tests for ordering, repeated fields, etc.
1141 //
1142 // It is currently impossible to produce results that the v1 Equal
1143 // considers equivalent to those of the v1 decoder. Figure out if
1144 // that's a problem or not.
1145 {
1146 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -08001147 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001148 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001149 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001150 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1151 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001152 ), build(
1153 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001154 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001155 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1156 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001157 )},
1158 wire: pack.Message{
1159 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1160 }.Marshal(),
1161 },
1162 {
1163 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001164 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001165 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001166 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001167 pack.Tag{1, pack.BytesType}, pack.String("string"),
1168 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001169 ), build(
1170 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001171 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001172 pack.Tag{1, pack.BytesType}, pack.String("string"),
1173 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001174 )},
1175 wire: pack.Message{
1176 pack.Tag{1, pack.BytesType}, pack.String("string"),
1177 }.Marshal(),
1178 },
1179 {
1180 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001181 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001182 &testpb.TestAllTypes{
1183 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -07001184 }, &test3pb.TestAllTypes{
1185 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -08001186 },
1187 },
1188 wire: pack.Message{
1189 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
1190 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1191 pack.Tag{2, pack.BytesType}, pack.String("string"),
1192 }),
1193 }.Marshal(),
1194 },
Damien Neil96c229a2019-04-03 12:17:24 -07001195 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001196 desc: "required field in nil message unset",
1197 partial: true,
1198 decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
1199 },
1200 {
Damien Neil96c229a2019-04-03 12:17:24 -07001201 desc: "required field unset",
1202 partial: true,
1203 decodeTo: []proto.Message{&testpb.TestRequired{}},
1204 },
1205 {
1206 desc: "required field set",
1207 decodeTo: []proto.Message{&testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001208 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001209 }},
1210 wire: pack.Message{
1211 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1212 }.Marshal(),
1213 },
1214 {
1215 desc: "required field in optional message unset",
1216 partial: true,
1217 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1218 OptionalMessage: &testpb.TestRequired{},
1219 }},
1220 wire: pack.Message{
1221 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1222 }.Marshal(),
1223 },
1224 {
1225 desc: "required field in optional message set",
1226 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1227 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001228 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001229 },
1230 }},
1231 wire: pack.Message{
1232 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1233 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1234 }),
1235 }.Marshal(),
1236 },
Damien Neil4686e232019-04-05 13:31:40 -07001237 {
1238 desc: "required field in optional message set (split across multiple tags)",
1239 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1240 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001241 RequiredField: proto.Int32(1),
Damien Neil4686e232019-04-05 13:31:40 -07001242 },
1243 }},
1244 wire: pack.Message{
1245 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1246 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1247 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1248 }),
1249 }.Marshal(),
1250 },
Damien Neil96c229a2019-04-03 12:17:24 -07001251 {
1252 desc: "required field in repeated message unset",
1253 partial: true,
1254 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1255 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001256 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001257 {},
1258 },
1259 }},
1260 wire: pack.Message{
1261 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1262 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1263 }),
1264 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1265 }.Marshal(),
1266 },
1267 {
1268 desc: "required field in repeated message set",
1269 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1270 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001271 {RequiredField: proto.Int32(1)},
1272 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001273 },
1274 }},
1275 wire: pack.Message{
1276 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1277 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1278 }),
1279 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1280 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1281 }),
1282 }.Marshal(),
1283 },
1284 {
1285 desc: "required field in map message unset",
1286 partial: true,
1287 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1288 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001289 1: {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001290 2: {},
1291 },
1292 }},
1293 wire: pack.Message{
1294 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1295 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1296 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1297 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1298 }),
1299 }),
1300 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1301 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1302 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1303 }),
1304 }.Marshal(),
1305 },
1306 {
1307 desc: "required field in map message set",
1308 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1309 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001310 1: {RequiredField: proto.Int32(1)},
1311 2: {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001312 },
1313 }},
1314 wire: pack.Message{
1315 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1316 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1317 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1318 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1319 }),
1320 }),
1321 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1322 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1323 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1324 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1325 }),
1326 }),
1327 }.Marshal(),
1328 },
1329 {
1330 desc: "required field in optional group unset",
1331 partial: true,
1332 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1333 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1334 }},
1335 wire: pack.Message{
1336 pack.Tag{1, pack.StartGroupType},
1337 pack.Tag{1, pack.EndGroupType},
1338 }.Marshal(),
1339 },
1340 {
1341 desc: "required field in optional group set",
1342 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1343 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001344 A: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001345 },
1346 }},
1347 wire: pack.Message{
1348 pack.Tag{1, pack.StartGroupType},
1349 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1350 pack.Tag{1, pack.EndGroupType},
1351 }.Marshal(),
1352 },
1353 {
1354 desc: "required field in repeated group unset",
1355 partial: true,
1356 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1357 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001358 {A: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001359 {},
1360 },
1361 }},
1362 wire: pack.Message{
1363 pack.Tag{3, pack.StartGroupType},
1364 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1365 pack.Tag{3, pack.EndGroupType},
1366 pack.Tag{3, pack.StartGroupType},
1367 pack.Tag{3, pack.EndGroupType},
1368 }.Marshal(),
1369 },
1370 {
1371 desc: "required field in repeated group set",
1372 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1373 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001374 {A: proto.Int32(1)},
1375 {A: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001376 },
1377 }},
1378 wire: pack.Message{
1379 pack.Tag{3, pack.StartGroupType},
1380 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1381 pack.Tag{3, pack.EndGroupType},
1382 pack.Tag{3, pack.StartGroupType},
1383 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1384 pack.Tag{3, pack.EndGroupType},
1385 }.Marshal(),
1386 },
1387 {
Damien Neil5322bdb2019-04-09 15:57:05 -07001388 desc: "required field in oneof message unset",
1389 partial: true,
1390 decodeTo: []proto.Message{
1391 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1392 &testpb.TestRequired{},
1393 }},
1394 },
1395 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1396 },
1397 {
1398 desc: "required field in oneof message set",
1399 decodeTo: []proto.Message{
1400 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1401 &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001402 RequiredField: proto.Int32(1),
Damien Neil5322bdb2019-04-09 15:57:05 -07001403 },
1404 }},
1405 },
1406 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
1407 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1408 })}.Marshal(),
1409 },
1410 {
Joe Tsai09cef322019-07-11 22:13:49 -07001411 desc: "required field in extension message unset",
1412 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001413 decodeTo: []proto.Message{build(
1414 &testpb.TestAllExtensions{},
1415 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1416 )},
1417 wire: pack.Message{
1418 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1419 }.Marshal(),
1420 },
1421 {
1422 desc: "required field in extension message set",
1423 decodeTo: []proto.Message{build(
1424 &testpb.TestAllExtensions{},
1425 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001426 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001427 }),
1428 )},
1429 wire: pack.Message{
1430 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1431 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1432 }),
1433 }.Marshal(),
1434 },
1435 {
Joe Tsai09cef322019-07-11 22:13:49 -07001436 desc: "required field in repeated extension message unset",
1437 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001438 decodeTo: []proto.Message{build(
1439 &testpb.TestAllExtensions{},
1440 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001441 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001442 {},
1443 }),
1444 )},
1445 wire: pack.Message{
1446 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1447 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1448 }),
1449 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1450 }.Marshal(),
1451 },
1452 {
1453 desc: "required field in repeated extension message set",
1454 decodeTo: []proto.Message{build(
1455 &testpb.TestAllExtensions{},
1456 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001457 {RequiredField: proto.Int32(1)},
1458 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001459 }),
1460 )},
1461 wire: pack.Message{
1462 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1463 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1464 }),
1465 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1466 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1467 }),
1468 }.Marshal(),
1469 },
Damien Neilc37adef2019-04-01 13:49:56 -07001470 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001471 desc: "nil messages",
1472 decodeTo: []proto.Message{
1473 (*testpb.TestAllTypes)(nil),
1474 (*test3pb.TestAllTypes)(nil),
1475 (*testpb.TestAllExtensions)(nil),
1476 },
1477 },
1478 {
Damien Neilc37adef2019-04-01 13:49:56 -07001479 desc: "legacy",
1480 partial: true,
1481 decodeTo: []proto.Message{
1482 &legacypb.Legacy{
1483 F1: &legacy1pb.Message{
Damien Neila8a2cea2019-07-10 16:17:16 -07001484 OptionalInt32: proto.Int32(1),
Damien Neilc37adef2019-04-01 13:49:56 -07001485 OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
1486 OptionalChildMessage: &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001487 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001488 },
1489 Optionalgroup: &legacy1pb.Message_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001490 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001491 },
1492 RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001493 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001494 },
1495 Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001496 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001497 },
1498 MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001499 true: {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001500 },
1501 OneofUnion: &legacy1pb.Message_OneofChildMessage{
1502 &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001503 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001504 },
1505 },
1506 },
1507 },
1508 },
1509 wire: pack.Message{
1510 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1511 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1512 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1513 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1514 pack.Tag{1, pack.BytesType}, pack.String("x"),
1515 }),
1516 pack.Tag{120, pack.StartGroupType},
1517 pack.Tag{1, pack.BytesType}, pack.String("x"),
1518 pack.Tag{120, pack.EndGroupType},
1519 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1520 pack.Tag{1, pack.BytesType}, pack.String("x"),
1521 }),
1522 pack.Tag{520, pack.StartGroupType},
1523 pack.Tag{1, pack.BytesType}, pack.String("x"),
1524 pack.Tag{520, pack.EndGroupType},
1525 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1526 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1527 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1528 pack.Tag{1, pack.BytesType}, pack.String("x"),
1529 }),
1530 }),
1531 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1532 pack.Tag{1, pack.BytesType}, pack.String("x"),
1533 }),
1534 }),
1535 }.Marshal(),
1536 },
Damien Neilba23aa52018-12-07 14:38:17 -08001537}
1538
Damien Neilbc310b52019-04-11 11:46:55 -07001539var invalidUTF8TestProtos = []testProto{
1540 {
1541 desc: "invalid UTF-8 in optional string field",
1542 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1543 OptionalString: "abc\xff",
1544 }},
1545 wire: pack.Message{
1546 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1547 }.Marshal(),
1548 },
1549 {
1550 desc: "invalid UTF-8 in repeated string field",
1551 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1552 RepeatedString: []string{"foo", "abc\xff"},
1553 }},
1554 wire: pack.Message{
1555 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1556 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1557 }.Marshal(),
1558 },
1559 {
1560 desc: "invalid UTF-8 in nested message",
1561 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1562 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1563 Corecursive: &test3pb.TestAllTypes{
1564 OptionalString: "abc\xff",
1565 },
1566 },
1567 }},
1568 wire: pack.Message{
1569 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1570 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1571 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1572 }),
1573 }),
1574 }.Marshal(),
1575 },
1576 {
Damien Neilc37adef2019-04-01 13:49:56 -07001577 desc: "invalid UTF-8 in oneof field",
1578 decodeTo: []proto.Message{
1579 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
1580 },
1581 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1582 },
1583 {
Damien Neilbc310b52019-04-11 11:46:55 -07001584 desc: "invalid UTF-8 in map key",
1585 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1586 MapStringString: map[string]string{"key\xff": "val"},
1587 }},
1588 wire: pack.Message{
1589 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1590 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1591 pack.Tag{2, pack.BytesType}, pack.String("val"),
1592 }),
1593 }.Marshal(),
1594 },
1595 {
1596 desc: "invalid UTF-8 in map value",
1597 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1598 MapStringString: map[string]string{"key": "val\xff"},
1599 }},
1600 wire: pack.Message{
1601 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1602 pack.Tag{1, pack.BytesType}, pack.String("key"),
1603 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1604 }),
1605 }.Marshal(),
1606 },
1607}
1608
Joe Tsaic51e2e02019-07-13 00:44:41 -07001609var noEnforceUTF8TestProtos = []testProto{
1610 {
1611 desc: "invalid UTF-8 in optional string field",
1612 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1613 OptionalString: string("abc\xff"),
1614 }},
1615 wire: pack.Message{
1616 pack.Tag{1, pack.BytesType}, pack.String("abc\xff"),
1617 }.Marshal(),
1618 },
1619 {
1620 desc: "invalid UTF-8 in optional string field of Go bytes",
1621 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1622 OptionalBytes: []byte("abc\xff"),
1623 }},
1624 wire: pack.Message{
1625 pack.Tag{2, pack.BytesType}, pack.String("abc\xff"),
1626 }.Marshal(),
1627 },
1628 {
1629 desc: "invalid UTF-8 in repeated string field",
1630 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1631 RepeatedString: []string{string("foo"), string("abc\xff")},
1632 }},
1633 wire: pack.Message{
1634 pack.Tag{3, pack.BytesType}, pack.String("foo"),
1635 pack.Tag{3, pack.BytesType}, pack.String("abc\xff"),
1636 }.Marshal(),
1637 },
1638 {
1639 desc: "invalid UTF-8 in repeated string field of Go bytes",
1640 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1641 RepeatedBytes: [][]byte{[]byte("foo"), []byte("abc\xff")},
1642 }},
1643 wire: pack.Message{
1644 pack.Tag{4, pack.BytesType}, pack.String("foo"),
1645 pack.Tag{4, pack.BytesType}, pack.String("abc\xff"),
1646 }.Marshal(),
1647 },
1648 {
1649 desc: "invalid UTF-8 in oneof string field",
1650 decodeTo: []proto.Message{
1651 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofString{string("abc\xff")}},
1652 },
1653 wire: pack.Message{pack.Tag{5, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1654 },
1655 {
1656 desc: "invalid UTF-8 in oneof string field of Go bytes",
1657 decodeTo: []proto.Message{
1658 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofBytes{[]byte("abc\xff")}},
1659 },
1660 wire: pack.Message{pack.Tag{6, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1661 },
1662}
1663
1664type TestNoEnforceUTF8 struct {
1665 OptionalString string `protobuf:"bytes,1,opt,name=optional_string"`
1666 OptionalBytes []byte `protobuf:"bytes,2,opt,name=optional_bytes"`
1667 RepeatedString []string `protobuf:"bytes,3,rep,name=repeated_string"`
1668 RepeatedBytes [][]byte `protobuf:"bytes,4,rep,name=repeated_bytes"`
1669 OneofField isOneofField `protobuf_oneof:"oneof_field"`
1670}
1671
1672type isOneofField interface{ isOneofField() }
1673
1674type TestNoEnforceUTF8_OneofString struct {
1675 OneofString string `protobuf:"bytes,5,opt,name=oneof_string,oneof"`
1676}
1677type TestNoEnforceUTF8_OneofBytes struct {
1678 OneofBytes []byte `protobuf:"bytes,6,opt,name=oneof_bytes,oneof"`
1679}
1680
1681func (*TestNoEnforceUTF8_OneofString) isOneofField() {}
1682func (*TestNoEnforceUTF8_OneofBytes) isOneofField() {}
1683
Joe Tsai74615a32019-07-14 18:51:46 -07001684func (m *TestNoEnforceUTF8) ProtoReflect() protoreflect.Message {
Joe Tsaic51e2e02019-07-13 00:44:41 -07001685 return messageInfo_TestNoEnforceUTF8.MessageOf(m)
1686}
1687
1688var messageInfo_TestNoEnforceUTF8 = protoimpl.MessageInfo{
Damien Neil16163b42019-08-06 15:43:25 -07001689 GoReflectType: reflect.TypeOf((*TestNoEnforceUTF8)(nil)),
1690 Desc: func() protoreflect.MessageDescriptor {
1691 pb := new(descriptorpb.FileDescriptorProto)
1692 if err := prototext.Unmarshal([]byte(`
Joe Tsaic51e2e02019-07-13 00:44:41 -07001693 syntax: "proto3"
1694 name: "test.proto"
1695 message_type: [{
1696 name: "TestNoEnforceUTF8"
1697 field: [
1698 {name:"optional_string" number:1 label:LABEL_OPTIONAL type:TYPE_STRING},
1699 {name:"optional_bytes" number:2 label:LABEL_OPTIONAL type:TYPE_STRING},
1700 {name:"repeated_string" number:3 label:LABEL_REPEATED type:TYPE_STRING},
1701 {name:"repeated_bytes" number:4 label:LABEL_REPEATED type:TYPE_STRING},
1702 {name:"oneof_string" number:5 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0},
1703 {name:"oneof_bytes" number:6 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0}
1704 ]
1705 oneof_decl: [{name:"oneof_field"}]
1706 }]
1707 `), pb); err != nil {
Damien Neil16163b42019-08-06 15:43:25 -07001708 panic(err)
1709 }
1710 fd, err := protodesc.NewFile(pb, nil)
1711 if err != nil {
1712 panic(err)
1713 }
1714 md := fd.Messages().Get(0)
1715 for i := 0; i < md.Fields().Len(); i++ {
1716 md.Fields().Get(i).(*filedesc.Field).L1.HasEnforceUTF8 = true
1717 md.Fields().Get(i).(*filedesc.Field).L1.EnforceUTF8 = false
1718 }
1719 return md
1720 }(),
Joe Tsaic51e2e02019-07-13 00:44:41 -07001721 OneofWrappers: []interface{}{
1722 (*TestNoEnforceUTF8_OneofString)(nil),
1723 (*TestNoEnforceUTF8_OneofBytes)(nil),
1724 },
1725}
1726
Tuo Shan6e25d8c2019-08-22 18:52:43 -07001727var invalidFieldNumberTestProtos = []struct {
1728 desc string
1729 wire []byte
1730 allowed bool
1731}{
1732 {
1733 desc: "zero",
1734 wire: pack.Message{
1735 pack.Tag{pack.MinValidNumber - 1, pack.VarintType}, pack.Varint(1001),
1736 }.Marshal(),
1737 },
1738 {
1739 desc: "zero and one",
1740 wire: pack.Message{
1741 pack.Tag{pack.MinValidNumber - 1, pack.VarintType}, pack.Varint(1002),
1742 pack.Tag{pack.MinValidNumber, pack.VarintType}, pack.Varint(1003),
1743 }.Marshal(),
1744 },
1745 {
1746 desc: "first reserved",
1747 wire: pack.Message{
1748 pack.Tag{pack.FirstReservedNumber, pack.VarintType}, pack.Varint(1004),
1749 }.Marshal(),
1750 allowed: true,
1751 },
1752 {
1753 desc: "last reserved",
1754 wire: pack.Message{
1755 pack.Tag{pack.LastReservedNumber, pack.VarintType}, pack.Varint(1005),
1756 }.Marshal(),
1757 allowed: true,
1758 },
1759 {
1760 desc: "max and max+1",
1761 wire: pack.Message{
1762 pack.Tag{pack.MaxValidNumber, pack.VarintType}, pack.Varint(1006),
1763 pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(1007),
1764 }.Marshal(),
1765 allowed: flags.ProtoLegacy,
1766 },
1767 {
1768 desc: "max+1",
1769 wire: pack.Message{
1770 pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(1008),
1771 }.Marshal(),
1772 allowed: flags.ProtoLegacy,
1773 },
1774}
1775
Damien Neil4be2fb42018-12-17 11:16:16 -08001776func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001777 for _, opt := range opts {
1778 opt(m)
1779 }
1780 return m
1781}
1782
Damien Neil4be2fb42018-12-17 11:16:16 -08001783type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001784
Joe Tsai74615a32019-07-14 18:51:46 -07001785func unknown(raw protoreflect.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001786 return func(m proto.Message) {
Joe Tsai378c1322019-04-25 23:48:08 -07001787 m.ProtoReflect().SetUnknown(raw)
Damien Neile6f060f2019-04-23 17:11:02 -07001788 }
1789}
1790
Damien Neilf1e905b2019-08-08 15:45:59 -07001791func extend(desc protoreflect.ExtensionType, value interface{}) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001792 return func(m proto.Message) {
Damien Neil92f76182019-08-02 16:58:08 -07001793 proto.SetExtension(m, desc, value)
Damien Neilba23aa52018-12-07 14:38:17 -08001794 }
1795}
Damien Neil61e93c72019-03-27 09:23:20 -07001796
1797func marshalText(m proto.Message) string {
Joe Tsaif2c4ddc2019-09-19 21:28:52 -07001798 if m == nil {
1799 return "<nil>\n"
1800 }
Joe Tsaicd4a31e2019-09-14 19:14:24 -07001801 b, _ := prototext.MarshalOptions{
1802 AllowPartial: true,
1803 EmitUnknown: true,
1804 Indent: "\t",
1805 }.Marshal(m)
Damien Neil61e93c72019-03-27 09:23:20 -07001806 return string(b)
1807}