blob: c696778c388bfcbb7cbaf29c2a3b56bf9a5c77fc [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 Neil7492a092019-07-10 15:23:29 -0700618 desc: "packed repeated types",
619 decodeTo: []proto.Message{&testpb.TestPackedTypes{
620 PackedInt32: []int32{1001, 2001},
621 PackedInt64: []int64{1002, 2002},
622 PackedUint32: []uint32{1003, 2003},
623 PackedUint64: []uint64{1004, 2004},
624 PackedSint32: []int32{1005, 2005},
625 PackedSint64: []int64{1006, 2006},
626 PackedFixed32: []uint32{1007, 2007},
627 PackedFixed64: []uint64{1008, 2008},
628 PackedSfixed32: []int32{1009, 2009},
629 PackedSfixed64: []int64{1010, 2010},
630 PackedFloat: []float32{1011.5, 2011.5},
631 PackedDouble: []float64{1012.5, 2012.5},
632 PackedBool: []bool{true, false},
633 PackedEnum: []testpb.ForeignEnum{
634 testpb.ForeignEnum_FOREIGN_FOO,
635 testpb.ForeignEnum_FOREIGN_BAR,
636 },
637 }, build(
638 &testpb.TestPackedExtensions{},
639 extend(testpb.E_PackedInt32Extension, []int32{1001, 2001}),
640 extend(testpb.E_PackedInt64Extension, []int64{1002, 2002}),
641 extend(testpb.E_PackedUint32Extension, []uint32{1003, 2003}),
642 extend(testpb.E_PackedUint64Extension, []uint64{1004, 2004}),
643 extend(testpb.E_PackedSint32Extension, []int32{1005, 2005}),
644 extend(testpb.E_PackedSint64Extension, []int64{1006, 2006}),
645 extend(testpb.E_PackedFixed32Extension, []uint32{1007, 2007}),
646 extend(testpb.E_PackedFixed64Extension, []uint64{1008, 2008}),
647 extend(testpb.E_PackedSfixed32Extension, []int32{1009, 2009}),
648 extend(testpb.E_PackedSfixed64Extension, []int64{1010, 2010}),
649 extend(testpb.E_PackedFloatExtension, []float32{1011.5, 2011.5}),
650 extend(testpb.E_PackedDoubleExtension, []float64{1012.5, 2012.5}),
651 extend(testpb.E_PackedBoolExtension, []bool{true, false}),
652 extend(testpb.E_PackedEnumExtension, []testpb.ForeignEnum{
653 testpb.ForeignEnum_FOREIGN_FOO,
654 testpb.ForeignEnum_FOREIGN_BAR,
655 }),
656 )},
657 wire: pack.Message{
658 pack.Tag{90, pack.BytesType}, pack.LengthPrefix{
659 pack.Varint(1001), pack.Varint(2001),
660 },
661 pack.Tag{91, pack.BytesType}, pack.LengthPrefix{
662 pack.Varint(1002), pack.Varint(2002),
663 },
664 pack.Tag{92, pack.BytesType}, pack.LengthPrefix{
665 pack.Uvarint(1003), pack.Uvarint(2003),
666 },
667 pack.Tag{93, pack.BytesType}, pack.LengthPrefix{
668 pack.Uvarint(1004), pack.Uvarint(2004),
669 },
670 pack.Tag{94, pack.BytesType}, pack.LengthPrefix{
671 pack.Svarint(1005), pack.Svarint(2005),
672 },
673 pack.Tag{95, pack.BytesType}, pack.LengthPrefix{
674 pack.Svarint(1006), pack.Svarint(2006),
675 },
676 pack.Tag{96, pack.BytesType}, pack.LengthPrefix{
677 pack.Uint32(1007), pack.Uint32(2007),
678 },
679 pack.Tag{97, pack.BytesType}, pack.LengthPrefix{
680 pack.Uint64(1008), pack.Uint64(2008),
681 },
682 pack.Tag{98, pack.BytesType}, pack.LengthPrefix{
683 pack.Int32(1009), pack.Int32(2009),
684 },
685 pack.Tag{99, pack.BytesType}, pack.LengthPrefix{
686 pack.Int64(1010), pack.Int64(2010),
687 },
688 pack.Tag{100, pack.BytesType}, pack.LengthPrefix{
689 pack.Float32(1011.5), pack.Float32(2011.5),
690 },
691 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{
692 pack.Float64(1012.5), pack.Float64(2012.5),
693 },
694 pack.Tag{102, pack.BytesType}, pack.LengthPrefix{
695 pack.Bool(true), pack.Bool(false),
696 },
697 pack.Tag{103, pack.BytesType}, pack.LengthPrefix{
698 pack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
699 pack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
700 },
701 }.Marshal(),
702 },
703 {
Damien Neilba23aa52018-12-07 14:38:17 -0800704 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800705 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800706 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700707 {A: proto.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700708 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700709 {A: proto.Int32(2)},
Damien Neilba23aa52018-12-07 14:38:17 -0800710 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700711 }, &test3pb.TestAllTypes{
712 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
713 {A: 1},
Damien Neilc37adef2019-04-01 13:49:56 -0700714 nil,
Damien Neil3b46ade2019-03-26 13:55:02 -0700715 {A: 2},
716 },
Damien Neilba23aa52018-12-07 14:38:17 -0800717 }, build(
718 &testpb.TestAllExtensions{},
719 extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700720 {A: proto.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700721 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700722 {A: proto.Int32(2)},
Damien Neilba23aa52018-12-07 14:38:17 -0800723 }),
724 )},
725 wire: pack.Message{
726 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
727 pack.Tag{1, pack.VarintType}, pack.Varint(1),
728 }),
Damien Neilc37adef2019-04-01 13:49:56 -0700729 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
Damien Neilba23aa52018-12-07 14:38:17 -0800730 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
731 pack.Tag{1, pack.VarintType}, pack.Varint(2),
732 }),
733 }.Marshal(),
734 },
735 {
736 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800737 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800738 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700739 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700740 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700741 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800742 },
743 }, build(
744 &testpb.TestAllExtensions{},
745 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700746 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700747 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700748 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800749 }),
750 )},
751 wire: pack.Message{
752 pack.Tag{46, pack.StartGroupType},
753 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
754 pack.Tag{46, pack.EndGroupType},
755 pack.Tag{46, pack.StartGroupType},
Damien Neilc37adef2019-04-01 13:49:56 -0700756 pack.Tag{46, pack.EndGroupType},
757 pack.Tag{46, pack.StartGroupType},
Damien Neilba23aa52018-12-07 14:38:17 -0800758 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
759 pack.Tag{46, pack.EndGroupType},
760 }.Marshal(),
761 },
762 {
763 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800764 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800765 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
766 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
767 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
768 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
769 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
770 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
771 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
772 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
773 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
774 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
775 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
776 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
777 MapBoolBool: map[bool]bool{true: false, false: true},
778 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
779 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
780 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700781 "71.1.key": {A: proto.Int32(1171)},
782 "71.2.key": {A: proto.Int32(2171)},
Damien Neilba23aa52018-12-07 14:38:17 -0800783 },
784 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
785 "73.1.key": testpb.TestAllTypes_FOO,
786 "73.2.key": testpb.TestAllTypes_BAR,
787 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700788 }, &test3pb.TestAllTypes{
789 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
790 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
791 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
792 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
793 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
794 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
795 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
796 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
797 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
798 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
799 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
800 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
801 MapBoolBool: map[bool]bool{true: false, false: true},
802 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
803 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
804 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
805 "71.1.key": {A: 1171},
806 "71.2.key": {A: 2171},
807 },
808 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
809 "73.1.key": test3pb.TestAllTypes_FOO,
810 "73.2.key": test3pb.TestAllTypes_BAR,
811 },
Damien Neilba23aa52018-12-07 14:38:17 -0800812 }},
813 wire: pack.Message{
814 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
815 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
816 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
817 }),
818 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
819 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
820 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
821 }),
822 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
823 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
824 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
825 }),
826 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
827 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
828 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
829 }),
830 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
831 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
832 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
833 }),
834 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
835 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
836 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
837 }),
838 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
839 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
840 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
841 }),
842 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
843 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
844 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
845 }),
846 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
847 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
848 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
849 }),
850 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
851 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
852 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
853 }),
854 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
855 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
856 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
857 }),
858 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
859 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
860 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
861 }),
862 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
863 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
864 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
865 }),
866 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
867 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
868 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
869 }),
870 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
871 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
872 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
873 }),
874 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
875 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
876 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
877 }),
878 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
879 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
880 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
881 }),
882 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
883 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
884 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
885 }),
886 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
887 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
888 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
889 }),
890 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
891 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
892 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
893 }),
894 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
895 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
896 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
897 }),
898 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
899 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
900 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
901 }),
902 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
903 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
904 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
905 }),
906 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
907 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
908 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
909 }),
910 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
911 pack.Tag{1, pack.VarintType}, pack.Bool(true),
912 pack.Tag{2, pack.VarintType}, pack.Bool(false),
913 }),
914 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
915 pack.Tag{1, pack.VarintType}, pack.Bool(false),
916 pack.Tag{2, pack.VarintType}, pack.Bool(true),
917 }),
918 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
919 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
920 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
921 }),
922 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
923 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
924 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
925 }),
926 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
927 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
928 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
929 }),
930 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
931 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
932 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
933 }),
934 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
935 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
936 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
937 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
938 }),
939 }),
940 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
941 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
942 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
943 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
944 }),
945 }),
946 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
947 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
948 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
949 }),
950 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
951 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
952 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
953 }),
954 }.Marshal(),
955 },
956 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700957 desc: "oneof (uint32)",
958 decodeTo: []proto.Message{
959 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
960 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
961 },
962 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800963 },
964 {
965 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700966 decodeTo: []proto.Message{
967 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700968 &testpb.TestAllTypes_NestedMessage{A: proto.Int32(1112)},
Damien Neil3b46ade2019-03-26 13:55:02 -0700969 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
970 &test3pb.TestAllTypes_NestedMessage{A: 1112},
971 }},
972 },
Damien Neilba23aa52018-12-07 14:38:17 -0800973 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
974 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
975 })}.Marshal(),
976 },
977 {
Damien Neilc37adef2019-04-01 13:49:56 -0700978 desc: "oneof (empty message)",
979 decodeTo: []proto.Message{
980 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
981 &testpb.TestAllTypes_NestedMessage{},
982 }},
983 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
984 &test3pb.TestAllTypes_NestedMessage{},
985 }},
986 },
987 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
988 },
989 {
Joe Tsai6c286742019-07-11 23:15:05 -0700990 desc: "oneof (merged message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700991 decodeTo: []proto.Message{
992 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
993 &testpb.TestAllTypes_NestedMessage{
Joe Tsai0f81b382019-07-10 23:14:31 -0700994 A: proto.Int32(1),
Damien Neil3b46ade2019-03-26 13:55:02 -0700995 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700996 OptionalInt32: proto.Int32(43),
Damien Neil3b46ade2019-03-26 13:55:02 -0700997 },
Damien Neilba23aa52018-12-07 14:38:17 -0800998 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700999 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
1000 &test3pb.TestAllTypes_NestedMessage{
Joe Tsai6c286742019-07-11 23:15:05 -07001001 A: 1,
Damien Neil3b46ade2019-03-26 13:55:02 -07001002 Corecursive: &test3pb.TestAllTypes{
1003 OptionalInt32: 43,
1004 },
1005 },
1006 }}},
Damien Neilba23aa52018-12-07 14:38:17 -08001007 wire: pack.Message{
1008 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
1009 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
1010 }),
1011 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
1012 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1013 pack.Tag{1, pack.VarintType}, pack.Varint(43),
1014 }),
1015 }),
1016 }.Marshal(),
1017 },
1018 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001019 desc: "oneof (string)",
1020 decodeTo: []proto.Message{
1021 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
1022 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
1023 },
1024 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001025 },
1026 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001027 desc: "oneof (bytes)",
1028 decodeTo: []proto.Message{
1029 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
1030 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
1031 },
1032 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001033 },
1034 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001035 desc: "oneof (bool)",
1036 decodeTo: []proto.Message{
1037 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
1038 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
1039 },
1040 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001041 },
1042 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001043 desc: "oneof (uint64)",
1044 decodeTo: []proto.Message{
1045 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
1046 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
1047 },
1048 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001049 },
1050 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001051 desc: "oneof (float)",
1052 decodeTo: []proto.Message{
1053 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
1054 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
1055 },
1056 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001057 },
1058 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001059 desc: "oneof (double)",
1060 decodeTo: []proto.Message{
1061 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
1062 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
1063 },
1064 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001065 },
1066 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001067 desc: "oneof (enum)",
1068 decodeTo: []proto.Message{
1069 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
1070 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
1071 },
1072 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001073 },
1074 {
Damien Neilc37adef2019-04-01 13:49:56 -07001075 desc: "oneof (zero)",
1076 decodeTo: []proto.Message{
1077 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}},
1078 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}},
1079 },
1080 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
1081 },
1082 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001083 desc: "oneof (overridden value)",
1084 decodeTo: []proto.Message{
1085 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
1086 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
1087 },
Damien Neilba23aa52018-12-07 14:38:17 -08001088 wire: pack.Message{
1089 pack.Tag{111, pack.VarintType}, pack.Varint(1),
1090 pack.Tag{116, pack.VarintType}, pack.Varint(2),
1091 }.Marshal(),
1092 },
1093 // TODO: More unknown field tests for ordering, repeated fields, etc.
1094 //
1095 // It is currently impossible to produce results that the v1 Equal
1096 // considers equivalent to those of the v1 decoder. Figure out if
1097 // that's a problem or not.
1098 {
1099 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -08001100 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001101 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001102 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001103 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1104 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001105 ), build(
1106 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001107 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001108 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1109 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001110 )},
1111 wire: pack.Message{
1112 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1113 }.Marshal(),
1114 },
1115 {
1116 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001117 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001118 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001119 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001120 pack.Tag{1, pack.BytesType}, pack.String("string"),
1121 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001122 ), build(
1123 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001124 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001125 pack.Tag{1, pack.BytesType}, pack.String("string"),
1126 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001127 )},
1128 wire: pack.Message{
1129 pack.Tag{1, pack.BytesType}, pack.String("string"),
1130 }.Marshal(),
1131 },
1132 {
1133 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001134 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001135 &testpb.TestAllTypes{
1136 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -07001137 }, &test3pb.TestAllTypes{
1138 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -08001139 },
1140 },
1141 wire: pack.Message{
1142 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
1143 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1144 pack.Tag{2, pack.BytesType}, pack.String("string"),
1145 }),
1146 }.Marshal(),
1147 },
Damien Neil96c229a2019-04-03 12:17:24 -07001148 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001149 desc: "required field in nil message unset",
1150 partial: true,
1151 decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
1152 },
1153 {
Damien Neil96c229a2019-04-03 12:17:24 -07001154 desc: "required field unset",
1155 partial: true,
1156 decodeTo: []proto.Message{&testpb.TestRequired{}},
1157 },
1158 {
1159 desc: "required field set",
1160 decodeTo: []proto.Message{&testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001161 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001162 }},
1163 wire: pack.Message{
1164 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1165 }.Marshal(),
1166 },
1167 {
1168 desc: "required field in optional message unset",
1169 partial: true,
1170 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1171 OptionalMessage: &testpb.TestRequired{},
1172 }},
1173 wire: pack.Message{
1174 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1175 }.Marshal(),
1176 },
1177 {
1178 desc: "required field in optional message set",
1179 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1180 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001181 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001182 },
1183 }},
1184 wire: pack.Message{
1185 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1186 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1187 }),
1188 }.Marshal(),
1189 },
Damien Neil4686e232019-04-05 13:31:40 -07001190 {
1191 desc: "required field in optional message set (split across multiple tags)",
1192 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1193 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001194 RequiredField: proto.Int32(1),
Damien Neil4686e232019-04-05 13:31:40 -07001195 },
1196 }},
1197 wire: pack.Message{
1198 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1199 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1200 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1201 }),
1202 }.Marshal(),
1203 },
Damien Neil96c229a2019-04-03 12:17:24 -07001204 {
1205 desc: "required field in repeated message unset",
1206 partial: true,
1207 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1208 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001209 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001210 {},
1211 },
1212 }},
1213 wire: pack.Message{
1214 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1215 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1216 }),
1217 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1218 }.Marshal(),
1219 },
1220 {
1221 desc: "required field in repeated message set",
1222 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1223 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001224 {RequiredField: proto.Int32(1)},
1225 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001226 },
1227 }},
1228 wire: pack.Message{
1229 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1230 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1231 }),
1232 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1233 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1234 }),
1235 }.Marshal(),
1236 },
1237 {
1238 desc: "required field in map message unset",
1239 partial: true,
1240 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1241 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001242 1: {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001243 2: {},
1244 },
1245 }},
1246 wire: pack.Message{
1247 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1248 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1249 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1250 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1251 }),
1252 }),
1253 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1254 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1255 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1256 }),
1257 }.Marshal(),
1258 },
1259 {
1260 desc: "required field in map message set",
1261 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1262 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001263 1: {RequiredField: proto.Int32(1)},
1264 2: {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001265 },
1266 }},
1267 wire: pack.Message{
1268 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1269 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1270 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1271 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1272 }),
1273 }),
1274 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1275 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1276 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1277 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1278 }),
1279 }),
1280 }.Marshal(),
1281 },
1282 {
1283 desc: "required field in optional group unset",
1284 partial: true,
1285 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1286 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1287 }},
1288 wire: pack.Message{
1289 pack.Tag{1, pack.StartGroupType},
1290 pack.Tag{1, pack.EndGroupType},
1291 }.Marshal(),
1292 },
1293 {
1294 desc: "required field in optional group set",
1295 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1296 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001297 A: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001298 },
1299 }},
1300 wire: pack.Message{
1301 pack.Tag{1, pack.StartGroupType},
1302 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1303 pack.Tag{1, pack.EndGroupType},
1304 }.Marshal(),
1305 },
1306 {
1307 desc: "required field in repeated group unset",
1308 partial: true,
1309 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1310 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001311 {A: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001312 {},
1313 },
1314 }},
1315 wire: pack.Message{
1316 pack.Tag{3, pack.StartGroupType},
1317 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1318 pack.Tag{3, pack.EndGroupType},
1319 pack.Tag{3, pack.StartGroupType},
1320 pack.Tag{3, pack.EndGroupType},
1321 }.Marshal(),
1322 },
1323 {
1324 desc: "required field in repeated group set",
1325 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1326 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001327 {A: proto.Int32(1)},
1328 {A: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001329 },
1330 }},
1331 wire: pack.Message{
1332 pack.Tag{3, pack.StartGroupType},
1333 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1334 pack.Tag{3, pack.EndGroupType},
1335 pack.Tag{3, pack.StartGroupType},
1336 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1337 pack.Tag{3, pack.EndGroupType},
1338 }.Marshal(),
1339 },
1340 {
Damien Neil5322bdb2019-04-09 15:57:05 -07001341 desc: "required field in oneof message unset",
1342 partial: true,
1343 decodeTo: []proto.Message{
1344 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1345 &testpb.TestRequired{},
1346 }},
1347 },
1348 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1349 },
1350 {
1351 desc: "required field in oneof message set",
1352 decodeTo: []proto.Message{
1353 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1354 &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001355 RequiredField: proto.Int32(1),
Damien Neil5322bdb2019-04-09 15:57:05 -07001356 },
1357 }},
1358 },
1359 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
1360 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1361 })}.Marshal(),
1362 },
1363 {
Joe Tsai09cef322019-07-11 22:13:49 -07001364 desc: "required field in extension message unset",
1365 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001366 decodeTo: []proto.Message{build(
1367 &testpb.TestAllExtensions{},
1368 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1369 )},
1370 wire: pack.Message{
1371 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1372 }.Marshal(),
1373 },
1374 {
1375 desc: "required field in extension message set",
1376 decodeTo: []proto.Message{build(
1377 &testpb.TestAllExtensions{},
1378 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001379 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001380 }),
1381 )},
1382 wire: pack.Message{
1383 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1384 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1385 }),
1386 }.Marshal(),
1387 },
1388 {
Joe Tsai09cef322019-07-11 22:13:49 -07001389 desc: "required field in repeated extension message unset",
1390 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001391 decodeTo: []proto.Message{build(
1392 &testpb.TestAllExtensions{},
1393 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001394 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001395 {},
1396 }),
1397 )},
1398 wire: pack.Message{
1399 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1400 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1401 }),
1402 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1403 }.Marshal(),
1404 },
1405 {
1406 desc: "required field in repeated extension message set",
1407 decodeTo: []proto.Message{build(
1408 &testpb.TestAllExtensions{},
1409 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001410 {RequiredField: proto.Int32(1)},
1411 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001412 }),
1413 )},
1414 wire: pack.Message{
1415 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1416 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1417 }),
1418 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1419 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1420 }),
1421 }.Marshal(),
1422 },
Damien Neilc37adef2019-04-01 13:49:56 -07001423 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001424 desc: "nil messages",
1425 decodeTo: []proto.Message{
1426 (*testpb.TestAllTypes)(nil),
1427 (*test3pb.TestAllTypes)(nil),
1428 (*testpb.TestAllExtensions)(nil),
1429 },
1430 },
1431 {
Damien Neilc37adef2019-04-01 13:49:56 -07001432 desc: "legacy",
1433 partial: true,
1434 decodeTo: []proto.Message{
1435 &legacypb.Legacy{
1436 F1: &legacy1pb.Message{
Damien Neila8a2cea2019-07-10 16:17:16 -07001437 OptionalInt32: proto.Int32(1),
Damien Neilc37adef2019-04-01 13:49:56 -07001438 OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
1439 OptionalChildMessage: &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001440 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001441 },
1442 Optionalgroup: &legacy1pb.Message_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001443 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001444 },
1445 RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001446 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001447 },
1448 Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001449 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001450 },
1451 MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001452 true: {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001453 },
1454 OneofUnion: &legacy1pb.Message_OneofChildMessage{
1455 &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001456 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001457 },
1458 },
1459 },
1460 },
1461 },
1462 wire: pack.Message{
1463 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1464 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1465 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1466 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1467 pack.Tag{1, pack.BytesType}, pack.String("x"),
1468 }),
1469 pack.Tag{120, pack.StartGroupType},
1470 pack.Tag{1, pack.BytesType}, pack.String("x"),
1471 pack.Tag{120, pack.EndGroupType},
1472 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1473 pack.Tag{1, pack.BytesType}, pack.String("x"),
1474 }),
1475 pack.Tag{520, pack.StartGroupType},
1476 pack.Tag{1, pack.BytesType}, pack.String("x"),
1477 pack.Tag{520, pack.EndGroupType},
1478 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1479 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1480 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1481 pack.Tag{1, pack.BytesType}, pack.String("x"),
1482 }),
1483 }),
1484 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1485 pack.Tag{1, pack.BytesType}, pack.String("x"),
1486 }),
1487 }),
1488 }.Marshal(),
1489 },
Damien Neilba23aa52018-12-07 14:38:17 -08001490}
1491
Damien Neilbc310b52019-04-11 11:46:55 -07001492var invalidUTF8TestProtos = []testProto{
1493 {
1494 desc: "invalid UTF-8 in optional string field",
1495 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1496 OptionalString: "abc\xff",
1497 }},
1498 wire: pack.Message{
1499 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1500 }.Marshal(),
1501 },
1502 {
1503 desc: "invalid UTF-8 in repeated string field",
1504 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1505 RepeatedString: []string{"foo", "abc\xff"},
1506 }},
1507 wire: pack.Message{
1508 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1509 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1510 }.Marshal(),
1511 },
1512 {
1513 desc: "invalid UTF-8 in nested message",
1514 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1515 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1516 Corecursive: &test3pb.TestAllTypes{
1517 OptionalString: "abc\xff",
1518 },
1519 },
1520 }},
1521 wire: pack.Message{
1522 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1523 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1524 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1525 }),
1526 }),
1527 }.Marshal(),
1528 },
1529 {
Damien Neilc37adef2019-04-01 13:49:56 -07001530 desc: "invalid UTF-8 in oneof field",
1531 decodeTo: []proto.Message{
1532 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
1533 },
1534 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1535 },
1536 {
Damien Neilbc310b52019-04-11 11:46:55 -07001537 desc: "invalid UTF-8 in map key",
1538 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1539 MapStringString: map[string]string{"key\xff": "val"},
1540 }},
1541 wire: pack.Message{
1542 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1543 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1544 pack.Tag{2, pack.BytesType}, pack.String("val"),
1545 }),
1546 }.Marshal(),
1547 },
1548 {
1549 desc: "invalid UTF-8 in map value",
1550 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1551 MapStringString: map[string]string{"key": "val\xff"},
1552 }},
1553 wire: pack.Message{
1554 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1555 pack.Tag{1, pack.BytesType}, pack.String("key"),
1556 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1557 }),
1558 }.Marshal(),
1559 },
1560}
1561
Joe Tsaic51e2e02019-07-13 00:44:41 -07001562var noEnforceUTF8TestProtos = []testProto{
1563 {
1564 desc: "invalid UTF-8 in optional string field",
1565 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1566 OptionalString: string("abc\xff"),
1567 }},
1568 wire: pack.Message{
1569 pack.Tag{1, pack.BytesType}, pack.String("abc\xff"),
1570 }.Marshal(),
1571 },
1572 {
1573 desc: "invalid UTF-8 in optional string field of Go bytes",
1574 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1575 OptionalBytes: []byte("abc\xff"),
1576 }},
1577 wire: pack.Message{
1578 pack.Tag{2, pack.BytesType}, pack.String("abc\xff"),
1579 }.Marshal(),
1580 },
1581 {
1582 desc: "invalid UTF-8 in repeated string field",
1583 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1584 RepeatedString: []string{string("foo"), string("abc\xff")},
1585 }},
1586 wire: pack.Message{
1587 pack.Tag{3, pack.BytesType}, pack.String("foo"),
1588 pack.Tag{3, pack.BytesType}, pack.String("abc\xff"),
1589 }.Marshal(),
1590 },
1591 {
1592 desc: "invalid UTF-8 in repeated string field of Go bytes",
1593 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1594 RepeatedBytes: [][]byte{[]byte("foo"), []byte("abc\xff")},
1595 }},
1596 wire: pack.Message{
1597 pack.Tag{4, pack.BytesType}, pack.String("foo"),
1598 pack.Tag{4, pack.BytesType}, pack.String("abc\xff"),
1599 }.Marshal(),
1600 },
1601 {
1602 desc: "invalid UTF-8 in oneof string field",
1603 decodeTo: []proto.Message{
1604 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofString{string("abc\xff")}},
1605 },
1606 wire: pack.Message{pack.Tag{5, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1607 },
1608 {
1609 desc: "invalid UTF-8 in oneof string field of Go bytes",
1610 decodeTo: []proto.Message{
1611 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofBytes{[]byte("abc\xff")}},
1612 },
1613 wire: pack.Message{pack.Tag{6, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1614 },
1615}
1616
1617type TestNoEnforceUTF8 struct {
1618 OptionalString string `protobuf:"bytes,1,opt,name=optional_string"`
1619 OptionalBytes []byte `protobuf:"bytes,2,opt,name=optional_bytes"`
1620 RepeatedString []string `protobuf:"bytes,3,rep,name=repeated_string"`
1621 RepeatedBytes [][]byte `protobuf:"bytes,4,rep,name=repeated_bytes"`
1622 OneofField isOneofField `protobuf_oneof:"oneof_field"`
1623}
1624
1625type isOneofField interface{ isOneofField() }
1626
1627type TestNoEnforceUTF8_OneofString struct {
1628 OneofString string `protobuf:"bytes,5,opt,name=oneof_string,oneof"`
1629}
1630type TestNoEnforceUTF8_OneofBytes struct {
1631 OneofBytes []byte `protobuf:"bytes,6,opt,name=oneof_bytes,oneof"`
1632}
1633
1634func (*TestNoEnforceUTF8_OneofString) isOneofField() {}
1635func (*TestNoEnforceUTF8_OneofBytes) isOneofField() {}
1636
Joe Tsai74615a32019-07-14 18:51:46 -07001637func (m *TestNoEnforceUTF8) ProtoReflect() protoreflect.Message {
Joe Tsaic51e2e02019-07-13 00:44:41 -07001638 return messageInfo_TestNoEnforceUTF8.MessageOf(m)
1639}
1640
1641var messageInfo_TestNoEnforceUTF8 = protoimpl.MessageInfo{
Damien Neil16163b42019-08-06 15:43:25 -07001642 GoReflectType: reflect.TypeOf((*TestNoEnforceUTF8)(nil)),
1643 Desc: func() protoreflect.MessageDescriptor {
1644 pb := new(descriptorpb.FileDescriptorProto)
1645 if err := prototext.Unmarshal([]byte(`
Joe Tsaic51e2e02019-07-13 00:44:41 -07001646 syntax: "proto3"
1647 name: "test.proto"
1648 message_type: [{
1649 name: "TestNoEnforceUTF8"
1650 field: [
1651 {name:"optional_string" number:1 label:LABEL_OPTIONAL type:TYPE_STRING},
1652 {name:"optional_bytes" number:2 label:LABEL_OPTIONAL type:TYPE_STRING},
1653 {name:"repeated_string" number:3 label:LABEL_REPEATED type:TYPE_STRING},
1654 {name:"repeated_bytes" number:4 label:LABEL_REPEATED type:TYPE_STRING},
1655 {name:"oneof_string" number:5 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0},
1656 {name:"oneof_bytes" number:6 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0}
1657 ]
1658 oneof_decl: [{name:"oneof_field"}]
1659 }]
1660 `), pb); err != nil {
Damien Neil16163b42019-08-06 15:43:25 -07001661 panic(err)
1662 }
1663 fd, err := protodesc.NewFile(pb, nil)
1664 if err != nil {
1665 panic(err)
1666 }
1667 md := fd.Messages().Get(0)
1668 for i := 0; i < md.Fields().Len(); i++ {
1669 md.Fields().Get(i).(*filedesc.Field).L1.HasEnforceUTF8 = true
1670 md.Fields().Get(i).(*filedesc.Field).L1.EnforceUTF8 = false
1671 }
1672 return md
1673 }(),
Joe Tsaic51e2e02019-07-13 00:44:41 -07001674 OneofWrappers: []interface{}{
1675 (*TestNoEnforceUTF8_OneofString)(nil),
1676 (*TestNoEnforceUTF8_OneofBytes)(nil),
1677 },
1678}
1679
Tuo Shan6e25d8c2019-08-22 18:52:43 -07001680var invalidFieldNumberTestProtos = []struct {
1681 desc string
1682 wire []byte
1683 allowed bool
1684}{
1685 {
1686 desc: "zero",
1687 wire: pack.Message{
1688 pack.Tag{pack.MinValidNumber - 1, pack.VarintType}, pack.Varint(1001),
1689 }.Marshal(),
1690 },
1691 {
1692 desc: "zero and one",
1693 wire: pack.Message{
1694 pack.Tag{pack.MinValidNumber - 1, pack.VarintType}, pack.Varint(1002),
1695 pack.Tag{pack.MinValidNumber, pack.VarintType}, pack.Varint(1003),
1696 }.Marshal(),
1697 },
1698 {
1699 desc: "first reserved",
1700 wire: pack.Message{
1701 pack.Tag{pack.FirstReservedNumber, pack.VarintType}, pack.Varint(1004),
1702 }.Marshal(),
1703 allowed: true,
1704 },
1705 {
1706 desc: "last reserved",
1707 wire: pack.Message{
1708 pack.Tag{pack.LastReservedNumber, pack.VarintType}, pack.Varint(1005),
1709 }.Marshal(),
1710 allowed: true,
1711 },
1712 {
1713 desc: "max and max+1",
1714 wire: pack.Message{
1715 pack.Tag{pack.MaxValidNumber, pack.VarintType}, pack.Varint(1006),
1716 pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(1007),
1717 }.Marshal(),
1718 allowed: flags.ProtoLegacy,
1719 },
1720 {
1721 desc: "max+1",
1722 wire: pack.Message{
1723 pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(1008),
1724 }.Marshal(),
1725 allowed: flags.ProtoLegacy,
1726 },
1727}
1728
Damien Neil4be2fb42018-12-17 11:16:16 -08001729func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001730 for _, opt := range opts {
1731 opt(m)
1732 }
1733 return m
1734}
1735
Damien Neil4be2fb42018-12-17 11:16:16 -08001736type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001737
Joe Tsai74615a32019-07-14 18:51:46 -07001738func unknown(raw protoreflect.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001739 return func(m proto.Message) {
Joe Tsai378c1322019-04-25 23:48:08 -07001740 m.ProtoReflect().SetUnknown(raw)
Damien Neile6f060f2019-04-23 17:11:02 -07001741 }
1742}
1743
Damien Neilf1e905b2019-08-08 15:45:59 -07001744func extend(desc protoreflect.ExtensionType, value interface{}) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001745 return func(m proto.Message) {
Damien Neil92f76182019-08-02 16:58:08 -07001746 proto.SetExtension(m, desc, value)
Damien Neilba23aa52018-12-07 14:38:17 -08001747 }
1748}
Damien Neil61e93c72019-03-27 09:23:20 -07001749
1750func marshalText(m proto.Message) string {
Joe Tsaicd4a31e2019-09-14 19:14:24 -07001751 b, _ := prototext.MarshalOptions{
1752 AllowPartial: true,
1753 EmitUnknown: true,
1754 Indent: "\t",
1755 }.Marshal(m)
Damien Neil61e93c72019-03-27 09:23:20 -07001756 return string(b)
1757}