blob: 3d7d6b62309937ab272a3ad51b529886f4494fa7 [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 Tsai74615a32019-07-14 18:51:46 -070019 "google.golang.org/protobuf/runtime/protoiface"
Joe Tsaic51e2e02019-07-13 00:44:41 -070020 "google.golang.org/protobuf/runtime/protoimpl"
Joe Tsai19058432019-02-27 21:46:29 -080021
Damien Neilc37adef2019-04-01 13:49:56 -070022 legacypb "google.golang.org/protobuf/internal/testprotos/legacy"
23 legacy1pb "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v0.0.0-20160225-2fc053c5"
Damien Neile89e6242019-05-13 23:55:40 -070024 testpb "google.golang.org/protobuf/internal/testprotos/test"
25 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Joe Tsaic51e2e02019-07-13 00:44:41 -070026 "google.golang.org/protobuf/types/descriptorpb"
Damien Neilba23aa52018-12-07 14:38:17 -080027)
28
29type testProto struct {
Joe Tsai09cef322019-07-11 22:13:49 -070030 desc string
31 decodeTo []proto.Message
32 wire []byte
33 partial bool
Damien Neilba23aa52018-12-07 14:38:17 -080034}
35
36func TestDecode(t *testing.T) {
37 for _, test := range testProtos {
38 for _, want := range test.decodeTo {
39 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070040 opts := proto.UnmarshalOptions{
41 AllowPartial: test.partial,
42 }
Damien Neilba23aa52018-12-07 14:38:17 -080043 wire := append(([]byte)(nil), test.wire...)
Damien Neil4be2fb42018-12-17 11:16:16 -080044 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
Damien Neil96c229a2019-04-03 12:17:24 -070045 if err := opts.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070046 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080047 return
48 }
49
50 // Aliasing check: Modifying the original wire bytes shouldn't
51 // affect the unmarshaled message.
52 for i := range wire {
53 wire[i] = 0
54 }
Joe Tsaidb38ddd2019-05-07 15:14:40 -070055 if !proto.Equal(got, want) {
Damien Neil61e93c72019-03-27 09:23:20 -070056 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080057 }
58 })
59 }
60 }
61}
62
Damien Neil96c229a2019-04-03 12:17:24 -070063func TestDecodeRequiredFieldChecks(t *testing.T) {
64 for _, test := range testProtos {
65 if !test.partial {
66 continue
67 }
Damien Neil96c229a2019-04-03 12:17:24 -070068 for _, m := range test.decodeTo {
69 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
70 got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
71 if err := proto.Unmarshal(test.wire, got); err == nil {
72 t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got))
73 }
74 })
75 }
76 }
77}
78
Damien Neilbc310b52019-04-11 11:46:55 -070079func TestDecodeInvalidUTF8(t *testing.T) {
80 for _, test := range invalidUTF8TestProtos {
81 for _, want := range test.decodeTo {
82 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
83 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
84 err := proto.Unmarshal(test.wire, got)
Damien Neil8c86fc52019-06-19 09:28:29 -070085 if err == nil {
Damien Neilbc310b52019-04-11 11:46:55 -070086 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
87 }
Damien Neilbc310b52019-04-11 11:46:55 -070088 })
89 }
90 }
91}
92
Joe Tsaic51e2e02019-07-13 00:44:41 -070093func TestDecodeNoEnforceUTF8(t *testing.T) {
94 for _, test := range noEnforceUTF8TestProtos {
95 for _, want := range test.decodeTo {
96 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
97 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
98 err := proto.Unmarshal(test.wire, got)
99 switch {
Joe Tsai1799d112019-08-08 13:31:59 -0700100 case flags.ProtoLegacy && err != nil:
Joe Tsaic51e2e02019-07-13 00:44:41 -0700101 t.Errorf("Unmarshal returned unexpected error: %v\nMessage:\n%v", err, marshalText(want))
Joe Tsai1799d112019-08-08 13:31:59 -0700102 case !flags.ProtoLegacy && err == nil:
Joe Tsaic51e2e02019-07-13 00:44:41 -0700103 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
104 }
105 })
106 }
107 }
108}
109
Damien Neil8003f082019-08-02 15:13:00 -0700110func TestDecodeZeroLengthBytes(t *testing.T) {
111 // Verify that proto3 bytes fields don't give the mistaken
112 // impression that they preserve presence.
113 wire := pack.Message{
114 pack.Tag{15, pack.BytesType}, pack.Bytes(nil),
115 }.Marshal()
116 m := &test3pb.TestAllTypes{}
117 if err := proto.Unmarshal(wire, m); err != nil {
118 t.Fatal(err)
119 }
120 if m.OptionalBytes != nil {
121 t.Errorf("unmarshal zero-length proto3 bytes field: got %v, want nil", m.OptionalBytes)
122 }
123}
124
Joe Tsai9b22b932019-08-08 19:23:32 -0700125func TestDecodeOneofNilWrapper(t *testing.T) {
126 wire := pack.Message{
127 pack.Tag{111, pack.VarintType}, pack.Varint(1111),
128 }.Marshal()
129 m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)}
130 if err := proto.Unmarshal(wire, m); err != nil {
131 t.Fatal(err)
132 }
133 if got := m.GetOneofUint32(); got != 1111 {
134 t.Errorf("GetOneofUint32() = %v, want %v", got, 1111)
135 }
136}
137
Damien Neilba23aa52018-12-07 14:38:17 -0800138var testProtos = []testProto{
139 {
140 desc: "basic scalar types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800141 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700142 OptionalInt32: proto.Int32(1001),
143 OptionalInt64: proto.Int64(1002),
144 OptionalUint32: proto.Uint32(1003),
145 OptionalUint64: proto.Uint64(1004),
146 OptionalSint32: proto.Int32(1005),
147 OptionalSint64: proto.Int64(1006),
148 OptionalFixed32: proto.Uint32(1007),
149 OptionalFixed64: proto.Uint64(1008),
150 OptionalSfixed32: proto.Int32(1009),
151 OptionalSfixed64: proto.Int64(1010),
152 OptionalFloat: proto.Float32(1011.5),
153 OptionalDouble: proto.Float64(1012.5),
154 OptionalBool: proto.Bool(true),
155 OptionalString: proto.String("string"),
Damien Neilba23aa52018-12-07 14:38:17 -0800156 OptionalBytes: []byte("bytes"),
157 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
Damien Neil3b46ade2019-03-26 13:55:02 -0700158 }, &test3pb.TestAllTypes{
159 OptionalInt32: 1001,
160 OptionalInt64: 1002,
161 OptionalUint32: 1003,
162 OptionalUint64: 1004,
163 OptionalSint32: 1005,
164 OptionalSint64: 1006,
165 OptionalFixed32: 1007,
166 OptionalFixed64: 1008,
167 OptionalSfixed32: 1009,
168 OptionalSfixed64: 1010,
169 OptionalFloat: 1011.5,
170 OptionalDouble: 1012.5,
171 OptionalBool: true,
172 OptionalString: "string",
173 OptionalBytes: []byte("bytes"),
174 OptionalNestedEnum: test3pb.TestAllTypes_BAR,
Damien Neilba23aa52018-12-07 14:38:17 -0800175 }, build(
176 &testpb.TestAllExtensions{},
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700177 extend(testpb.E_OptionalInt32Extension, int32(1001)),
178 extend(testpb.E_OptionalInt64Extension, int64(1002)),
179 extend(testpb.E_OptionalUint32Extension, uint32(1003)),
180 extend(testpb.E_OptionalUint64Extension, uint64(1004)),
181 extend(testpb.E_OptionalSint32Extension, int32(1005)),
182 extend(testpb.E_OptionalSint64Extension, int64(1006)),
183 extend(testpb.E_OptionalFixed32Extension, uint32(1007)),
184 extend(testpb.E_OptionalFixed64Extension, uint64(1008)),
185 extend(testpb.E_OptionalSfixed32Extension, int32(1009)),
186 extend(testpb.E_OptionalSfixed64Extension, int64(1010)),
187 extend(testpb.E_OptionalFloatExtension, float32(1011.5)),
188 extend(testpb.E_OptionalDoubleExtension, float64(1012.5)),
189 extend(testpb.E_OptionalBoolExtension, bool(true)),
190 extend(testpb.E_OptionalStringExtension, string("string")),
Damien Neilba23aa52018-12-07 14:38:17 -0800191 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700192 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR),
Damien Neilba23aa52018-12-07 14:38:17 -0800193 )},
194 wire: pack.Message{
195 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
196 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
197 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
198 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
199 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
200 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
201 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
202 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
203 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
204 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
205 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
206 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
207 pack.Tag{13, pack.VarintType}, pack.Bool(true),
208 pack.Tag{14, pack.BytesType}, pack.String("string"),
209 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
210 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
211 }.Marshal(),
212 },
213 {
Damien Neil8003f082019-08-02 15:13:00 -0700214 desc: "zero values",
215 decodeTo: []proto.Message{&testpb.TestAllTypes{
216 OptionalInt32: proto.Int32(0),
217 OptionalInt64: proto.Int64(0),
218 OptionalUint32: proto.Uint32(0),
219 OptionalUint64: proto.Uint64(0),
220 OptionalSint32: proto.Int32(0),
221 OptionalSint64: proto.Int64(0),
222 OptionalFixed32: proto.Uint32(0),
223 OptionalFixed64: proto.Uint64(0),
224 OptionalSfixed32: proto.Int32(0),
225 OptionalSfixed64: proto.Int64(0),
226 OptionalFloat: proto.Float32(0),
227 OptionalDouble: proto.Float64(0),
228 OptionalBool: proto.Bool(false),
229 OptionalString: proto.String(""),
230 OptionalBytes: []byte{},
231 }, &test3pb.TestAllTypes{}, build(
232 &testpb.TestAllExtensions{},
233 extend(testpb.E_OptionalInt32Extension, int32(0)),
234 extend(testpb.E_OptionalInt64Extension, int64(0)),
235 extend(testpb.E_OptionalUint32Extension, uint32(0)),
236 extend(testpb.E_OptionalUint64Extension, uint64(0)),
237 extend(testpb.E_OptionalSint32Extension, int32(0)),
238 extend(testpb.E_OptionalSint64Extension, int64(0)),
239 extend(testpb.E_OptionalFixed32Extension, uint32(0)),
240 extend(testpb.E_OptionalFixed64Extension, uint64(0)),
241 extend(testpb.E_OptionalSfixed32Extension, int32(0)),
242 extend(testpb.E_OptionalSfixed64Extension, int64(0)),
243 extend(testpb.E_OptionalFloatExtension, float32(0)),
244 extend(testpb.E_OptionalDoubleExtension, float64(0)),
245 extend(testpb.E_OptionalBoolExtension, bool(false)),
246 extend(testpb.E_OptionalStringExtension, string("")),
247 extend(testpb.E_OptionalBytesExtension, []byte{}),
248 )},
249 wire: pack.Message{
250 pack.Tag{1, pack.VarintType}, pack.Varint(0),
251 pack.Tag{2, pack.VarintType}, pack.Varint(0),
252 pack.Tag{3, pack.VarintType}, pack.Uvarint(0),
253 pack.Tag{4, pack.VarintType}, pack.Uvarint(0),
254 pack.Tag{5, pack.VarintType}, pack.Svarint(0),
255 pack.Tag{6, pack.VarintType}, pack.Svarint(0),
256 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(0),
257 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(0),
258 pack.Tag{9, pack.Fixed32Type}, pack.Int32(0),
259 pack.Tag{10, pack.Fixed64Type}, pack.Int64(0),
260 pack.Tag{11, pack.Fixed32Type}, pack.Float32(0),
261 pack.Tag{12, pack.Fixed64Type}, pack.Float64(0),
262 pack.Tag{13, pack.VarintType}, pack.Bool(false),
263 pack.Tag{14, pack.BytesType}, pack.String(""),
264 pack.Tag{15, pack.BytesType}, pack.Bytes(nil),
265 }.Marshal(),
266 },
267 {
Damien Neilba23aa52018-12-07 14:38:17 -0800268 desc: "groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800269 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800270 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700271 A: proto.Int32(1017),
Damien Neilba23aa52018-12-07 14:38:17 -0800272 },
273 }, build(
274 &testpb.TestAllExtensions{},
275 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700276 A: proto.Int32(1017),
Damien Neilba23aa52018-12-07 14:38:17 -0800277 }),
278 )},
279 wire: pack.Message{
280 pack.Tag{16, pack.StartGroupType},
281 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
282 pack.Tag{16, pack.EndGroupType},
283 }.Marshal(),
284 },
285 {
286 desc: "groups (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800287 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800288 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700289 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800290 },
291 }, build(
292 &testpb.TestAllExtensions{},
293 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700294 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800295 }),
296 )},
297 wire: pack.Message{
298 pack.Tag{16, pack.StartGroupType},
299 pack.Tag{17, pack.VarintType}, pack.Varint(1),
300 pack.Tag{16, pack.EndGroupType},
301 pack.Tag{16, pack.StartGroupType},
302 pack.Tag{17, pack.VarintType}, pack.Varint(2),
303 pack.Tag{16, pack.EndGroupType},
304 }.Marshal(),
305 },
306 {
307 desc: "messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800308 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800309 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700310 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800311 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700312 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800313 },
314 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700315 }, &test3pb.TestAllTypes{
316 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
317 A: 42,
318 Corecursive: &test3pb.TestAllTypes{
319 OptionalInt32: 43,
320 },
321 },
Damien Neilba23aa52018-12-07 14:38:17 -0800322 }, build(
323 &testpb.TestAllExtensions{},
324 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700325 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800326 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700327 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800328 },
329 }),
330 )},
331 wire: pack.Message{
332 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
333 pack.Tag{1, pack.VarintType}, pack.Varint(42),
334 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
335 pack.Tag{1, pack.VarintType}, pack.Varint(43),
336 }),
337 }),
338 }.Marshal(),
339 },
340 {
341 desc: "messages (split across multiple tags)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800342 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800343 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700344 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800345 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700346 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800347 },
348 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700349 }, &test3pb.TestAllTypes{
350 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
351 A: 42,
352 Corecursive: &test3pb.TestAllTypes{
353 OptionalInt32: 43,
354 },
355 },
Damien Neilba23aa52018-12-07 14:38:17 -0800356 }, build(
357 &testpb.TestAllExtensions{},
358 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700359 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800360 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700361 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800362 },
363 }),
364 )},
365 wire: pack.Message{
366 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
367 pack.Tag{1, pack.VarintType}, pack.Varint(42),
368 }),
369 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
370 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
371 pack.Tag{1, pack.VarintType}, pack.Varint(43),
372 }),
373 }),
374 }.Marshal(),
375 },
376 {
377 desc: "messages (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800378 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800379 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700380 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800381 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700382 }, &test3pb.TestAllTypes{
383 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
384 A: 2,
385 },
Damien Neilba23aa52018-12-07 14:38:17 -0800386 }, build(
387 &testpb.TestAllExtensions{},
388 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700389 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800390 }),
391 )},
392 wire: pack.Message{
393 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
394 pack.Tag{1, pack.VarintType}, pack.Varint(1),
395 }),
396 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
397 pack.Tag{1, pack.VarintType}, pack.Varint(2),
398 }),
399 }.Marshal(),
400 },
401 {
402 desc: "basic repeated types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800403 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800404 RepeatedInt32: []int32{1001, 2001},
405 RepeatedInt64: []int64{1002, 2002},
406 RepeatedUint32: []uint32{1003, 2003},
407 RepeatedUint64: []uint64{1004, 2004},
408 RepeatedSint32: []int32{1005, 2005},
409 RepeatedSint64: []int64{1006, 2006},
410 RepeatedFixed32: []uint32{1007, 2007},
411 RepeatedFixed64: []uint64{1008, 2008},
412 RepeatedSfixed32: []int32{1009, 2009},
413 RepeatedSfixed64: []int64{1010, 2010},
414 RepeatedFloat: []float32{1011.5, 2011.5},
415 RepeatedDouble: []float64{1012.5, 2012.5},
416 RepeatedBool: []bool{true, false},
417 RepeatedString: []string{"foo", "bar"},
418 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
419 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
420 testpb.TestAllTypes_FOO,
421 testpb.TestAllTypes_BAR,
422 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700423 }, &test3pb.TestAllTypes{
424 RepeatedInt32: []int32{1001, 2001},
425 RepeatedInt64: []int64{1002, 2002},
426 RepeatedUint32: []uint32{1003, 2003},
427 RepeatedUint64: []uint64{1004, 2004},
428 RepeatedSint32: []int32{1005, 2005},
429 RepeatedSint64: []int64{1006, 2006},
430 RepeatedFixed32: []uint32{1007, 2007},
431 RepeatedFixed64: []uint64{1008, 2008},
432 RepeatedSfixed32: []int32{1009, 2009},
433 RepeatedSfixed64: []int64{1010, 2010},
434 RepeatedFloat: []float32{1011.5, 2011.5},
435 RepeatedDouble: []float64{1012.5, 2012.5},
436 RepeatedBool: []bool{true, false},
437 RepeatedString: []string{"foo", "bar"},
438 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
439 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
440 test3pb.TestAllTypes_FOO,
441 test3pb.TestAllTypes_BAR,
442 },
Damien Neilba23aa52018-12-07 14:38:17 -0800443 }, build(
444 &testpb.TestAllExtensions{},
445 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
446 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
447 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
448 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
449 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
450 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
451 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
452 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
453 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
454 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
455 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
456 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
457 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
458 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
459 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
460 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
461 testpb.TestAllTypes_FOO,
462 testpb.TestAllTypes_BAR,
463 }),
464 )},
465 wire: pack.Message{
466 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
467 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
468 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
469 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
470 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
471 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
472 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
473 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
474 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
475 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
476 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
477 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
478 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
479 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
480 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
481 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
482 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
483 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
484 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
485 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
486 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
487 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
488 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
489 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
490 pack.Tag{43, pack.VarintType}, pack.Bool(true),
491 pack.Tag{43, pack.VarintType}, pack.Bool(false),
492 pack.Tag{44, pack.BytesType}, pack.String("foo"),
493 pack.Tag{44, pack.BytesType}, pack.String("bar"),
494 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
495 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
496 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
497 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
498 }.Marshal(),
499 },
500 {
501 desc: "basic repeated types (packed encoding)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800502 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800503 RepeatedInt32: []int32{1001, 2001},
504 RepeatedInt64: []int64{1002, 2002},
505 RepeatedUint32: []uint32{1003, 2003},
506 RepeatedUint64: []uint64{1004, 2004},
507 RepeatedSint32: []int32{1005, 2005},
508 RepeatedSint64: []int64{1006, 2006},
509 RepeatedFixed32: []uint32{1007, 2007},
510 RepeatedFixed64: []uint64{1008, 2008},
511 RepeatedSfixed32: []int32{1009, 2009},
512 RepeatedSfixed64: []int64{1010, 2010},
513 RepeatedFloat: []float32{1011.5, 2011.5},
514 RepeatedDouble: []float64{1012.5, 2012.5},
515 RepeatedBool: []bool{true, false},
516 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
517 testpb.TestAllTypes_FOO,
518 testpb.TestAllTypes_BAR,
519 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700520 }, &test3pb.TestAllTypes{
521 RepeatedInt32: []int32{1001, 2001},
522 RepeatedInt64: []int64{1002, 2002},
523 RepeatedUint32: []uint32{1003, 2003},
524 RepeatedUint64: []uint64{1004, 2004},
525 RepeatedSint32: []int32{1005, 2005},
526 RepeatedSint64: []int64{1006, 2006},
527 RepeatedFixed32: []uint32{1007, 2007},
528 RepeatedFixed64: []uint64{1008, 2008},
529 RepeatedSfixed32: []int32{1009, 2009},
530 RepeatedSfixed64: []int64{1010, 2010},
531 RepeatedFloat: []float32{1011.5, 2011.5},
532 RepeatedDouble: []float64{1012.5, 2012.5},
533 RepeatedBool: []bool{true, false},
534 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
535 test3pb.TestAllTypes_FOO,
536 test3pb.TestAllTypes_BAR,
537 },
Damien Neilba23aa52018-12-07 14:38:17 -0800538 }, build(
539 &testpb.TestAllExtensions{},
540 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
541 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
542 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
543 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
544 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
545 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
546 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
547 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
548 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
549 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
550 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
551 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
552 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
553 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
554 testpb.TestAllTypes_FOO,
555 testpb.TestAllTypes_BAR,
556 }),
557 )},
558 wire: pack.Message{
559 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
560 pack.Varint(1001), pack.Varint(2001),
561 },
562 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
563 pack.Varint(1002), pack.Varint(2002),
564 },
565 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
566 pack.Uvarint(1003), pack.Uvarint(2003),
567 },
568 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
569 pack.Uvarint(1004), pack.Uvarint(2004),
570 },
571 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
572 pack.Svarint(1005), pack.Svarint(2005),
573 },
574 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
575 pack.Svarint(1006), pack.Svarint(2006),
576 },
577 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
578 pack.Uint32(1007), pack.Uint32(2007),
579 },
580 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
581 pack.Uint64(1008), pack.Uint64(2008),
582 },
583 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
584 pack.Int32(1009), pack.Int32(2009),
585 },
586 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
587 pack.Int64(1010), pack.Int64(2010),
588 },
589 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
590 pack.Float32(1011.5), pack.Float32(2011.5),
591 },
592 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
593 pack.Float64(1012.5), pack.Float64(2012.5),
594 },
595 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
596 pack.Bool(true), pack.Bool(false),
597 },
598 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
599 pack.Varint(int(testpb.TestAllTypes_FOO)),
600 pack.Varint(int(testpb.TestAllTypes_BAR)),
601 },
602 }.Marshal(),
603 },
604 {
Damien Neil7492a092019-07-10 15:23:29 -0700605 desc: "packed repeated types",
606 decodeTo: []proto.Message{&testpb.TestPackedTypes{
607 PackedInt32: []int32{1001, 2001},
608 PackedInt64: []int64{1002, 2002},
609 PackedUint32: []uint32{1003, 2003},
610 PackedUint64: []uint64{1004, 2004},
611 PackedSint32: []int32{1005, 2005},
612 PackedSint64: []int64{1006, 2006},
613 PackedFixed32: []uint32{1007, 2007},
614 PackedFixed64: []uint64{1008, 2008},
615 PackedSfixed32: []int32{1009, 2009},
616 PackedSfixed64: []int64{1010, 2010},
617 PackedFloat: []float32{1011.5, 2011.5},
618 PackedDouble: []float64{1012.5, 2012.5},
619 PackedBool: []bool{true, false},
620 PackedEnum: []testpb.ForeignEnum{
621 testpb.ForeignEnum_FOREIGN_FOO,
622 testpb.ForeignEnum_FOREIGN_BAR,
623 },
624 }, build(
625 &testpb.TestPackedExtensions{},
626 extend(testpb.E_PackedInt32Extension, []int32{1001, 2001}),
627 extend(testpb.E_PackedInt64Extension, []int64{1002, 2002}),
628 extend(testpb.E_PackedUint32Extension, []uint32{1003, 2003}),
629 extend(testpb.E_PackedUint64Extension, []uint64{1004, 2004}),
630 extend(testpb.E_PackedSint32Extension, []int32{1005, 2005}),
631 extend(testpb.E_PackedSint64Extension, []int64{1006, 2006}),
632 extend(testpb.E_PackedFixed32Extension, []uint32{1007, 2007}),
633 extend(testpb.E_PackedFixed64Extension, []uint64{1008, 2008}),
634 extend(testpb.E_PackedSfixed32Extension, []int32{1009, 2009}),
635 extend(testpb.E_PackedSfixed64Extension, []int64{1010, 2010}),
636 extend(testpb.E_PackedFloatExtension, []float32{1011.5, 2011.5}),
637 extend(testpb.E_PackedDoubleExtension, []float64{1012.5, 2012.5}),
638 extend(testpb.E_PackedBoolExtension, []bool{true, false}),
639 extend(testpb.E_PackedEnumExtension, []testpb.ForeignEnum{
640 testpb.ForeignEnum_FOREIGN_FOO,
641 testpb.ForeignEnum_FOREIGN_BAR,
642 }),
643 )},
644 wire: pack.Message{
645 pack.Tag{90, pack.BytesType}, pack.LengthPrefix{
646 pack.Varint(1001), pack.Varint(2001),
647 },
648 pack.Tag{91, pack.BytesType}, pack.LengthPrefix{
649 pack.Varint(1002), pack.Varint(2002),
650 },
651 pack.Tag{92, pack.BytesType}, pack.LengthPrefix{
652 pack.Uvarint(1003), pack.Uvarint(2003),
653 },
654 pack.Tag{93, pack.BytesType}, pack.LengthPrefix{
655 pack.Uvarint(1004), pack.Uvarint(2004),
656 },
657 pack.Tag{94, pack.BytesType}, pack.LengthPrefix{
658 pack.Svarint(1005), pack.Svarint(2005),
659 },
660 pack.Tag{95, pack.BytesType}, pack.LengthPrefix{
661 pack.Svarint(1006), pack.Svarint(2006),
662 },
663 pack.Tag{96, pack.BytesType}, pack.LengthPrefix{
664 pack.Uint32(1007), pack.Uint32(2007),
665 },
666 pack.Tag{97, pack.BytesType}, pack.LengthPrefix{
667 pack.Uint64(1008), pack.Uint64(2008),
668 },
669 pack.Tag{98, pack.BytesType}, pack.LengthPrefix{
670 pack.Int32(1009), pack.Int32(2009),
671 },
672 pack.Tag{99, pack.BytesType}, pack.LengthPrefix{
673 pack.Int64(1010), pack.Int64(2010),
674 },
675 pack.Tag{100, pack.BytesType}, pack.LengthPrefix{
676 pack.Float32(1011.5), pack.Float32(2011.5),
677 },
678 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{
679 pack.Float64(1012.5), pack.Float64(2012.5),
680 },
681 pack.Tag{102, pack.BytesType}, pack.LengthPrefix{
682 pack.Bool(true), pack.Bool(false),
683 },
684 pack.Tag{103, pack.BytesType}, pack.LengthPrefix{
685 pack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
686 pack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
687 },
688 }.Marshal(),
689 },
690 {
Damien Neilba23aa52018-12-07 14:38:17 -0800691 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800692 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800693 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700694 {A: proto.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700695 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700696 {A: proto.Int32(2)},
Damien Neilba23aa52018-12-07 14:38:17 -0800697 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700698 }, &test3pb.TestAllTypes{
699 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
700 {A: 1},
Damien Neilc37adef2019-04-01 13:49:56 -0700701 nil,
Damien Neil3b46ade2019-03-26 13:55:02 -0700702 {A: 2},
703 },
Damien Neilba23aa52018-12-07 14:38:17 -0800704 }, build(
705 &testpb.TestAllExtensions{},
706 extend(testpb.E_RepeatedNestedMessageExtension, []*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 }),
711 )},
712 wire: pack.Message{
713 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
714 pack.Tag{1, pack.VarintType}, pack.Varint(1),
715 }),
Damien Neilc37adef2019-04-01 13:49:56 -0700716 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
Damien Neilba23aa52018-12-07 14:38:17 -0800717 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
718 pack.Tag{1, pack.VarintType}, pack.Varint(2),
719 }),
720 }.Marshal(),
721 },
722 {
723 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800724 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800725 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700726 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700727 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700728 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800729 },
730 }, build(
731 &testpb.TestAllExtensions{},
732 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700733 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700734 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700735 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800736 }),
737 )},
738 wire: pack.Message{
739 pack.Tag{46, pack.StartGroupType},
740 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
741 pack.Tag{46, pack.EndGroupType},
742 pack.Tag{46, pack.StartGroupType},
Damien Neilc37adef2019-04-01 13:49:56 -0700743 pack.Tag{46, pack.EndGroupType},
744 pack.Tag{46, pack.StartGroupType},
Damien Neilba23aa52018-12-07 14:38:17 -0800745 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
746 pack.Tag{46, pack.EndGroupType},
747 }.Marshal(),
748 },
749 {
750 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800751 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800752 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
753 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
754 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
755 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
756 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
757 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
758 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
759 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
760 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
761 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
762 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
763 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
764 MapBoolBool: map[bool]bool{true: false, false: true},
765 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
766 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
767 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700768 "71.1.key": {A: proto.Int32(1171)},
769 "71.2.key": {A: proto.Int32(2171)},
Damien Neilba23aa52018-12-07 14:38:17 -0800770 },
771 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
772 "73.1.key": testpb.TestAllTypes_FOO,
773 "73.2.key": testpb.TestAllTypes_BAR,
774 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700775 }, &test3pb.TestAllTypes{
776 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
777 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
778 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
779 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
780 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
781 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
782 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
783 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
784 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
785 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
786 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
787 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
788 MapBoolBool: map[bool]bool{true: false, false: true},
789 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
790 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
791 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
792 "71.1.key": {A: 1171},
793 "71.2.key": {A: 2171},
794 },
795 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
796 "73.1.key": test3pb.TestAllTypes_FOO,
797 "73.2.key": test3pb.TestAllTypes_BAR,
798 },
Damien Neilba23aa52018-12-07 14:38:17 -0800799 }},
800 wire: pack.Message{
801 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
802 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
803 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
804 }),
805 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
806 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
807 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
808 }),
809 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
810 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
811 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
812 }),
813 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
814 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
815 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
816 }),
817 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
818 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
819 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
820 }),
821 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
822 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
823 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
824 }),
825 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
826 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
827 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
828 }),
829 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
830 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
831 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
832 }),
833 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
834 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
835 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
836 }),
837 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
838 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
839 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
840 }),
841 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
842 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
843 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
844 }),
845 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
846 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
847 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
848 }),
849 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
850 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
851 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
852 }),
853 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
854 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
855 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
856 }),
857 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
858 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
859 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
860 }),
861 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
862 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
863 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
864 }),
865 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
866 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
867 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
868 }),
869 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
870 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
871 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
872 }),
873 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
874 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
875 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
876 }),
877 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
878 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
879 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
880 }),
881 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
882 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
883 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
884 }),
885 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
886 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
887 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
888 }),
889 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
890 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
891 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
892 }),
893 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
894 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
895 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
896 }),
897 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
898 pack.Tag{1, pack.VarintType}, pack.Bool(true),
899 pack.Tag{2, pack.VarintType}, pack.Bool(false),
900 }),
901 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
902 pack.Tag{1, pack.VarintType}, pack.Bool(false),
903 pack.Tag{2, pack.VarintType}, pack.Bool(true),
904 }),
905 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
906 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
907 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
908 }),
909 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
910 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
911 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
912 }),
913 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
914 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
915 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
916 }),
917 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
918 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
919 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
920 }),
921 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
922 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
923 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
924 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
925 }),
926 }),
927 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
928 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
929 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
930 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
931 }),
932 }),
933 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
934 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
935 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
936 }),
937 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
938 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
939 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
940 }),
941 }.Marshal(),
942 },
943 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700944 desc: "oneof (uint32)",
945 decodeTo: []proto.Message{
946 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
947 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
948 },
949 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800950 },
951 {
952 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700953 decodeTo: []proto.Message{
954 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700955 &testpb.TestAllTypes_NestedMessage{A: proto.Int32(1112)},
Damien Neil3b46ade2019-03-26 13:55:02 -0700956 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
957 &test3pb.TestAllTypes_NestedMessage{A: 1112},
958 }},
959 },
Damien Neilba23aa52018-12-07 14:38:17 -0800960 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
961 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
962 })}.Marshal(),
963 },
964 {
Damien Neilc37adef2019-04-01 13:49:56 -0700965 desc: "oneof (empty message)",
966 decodeTo: []proto.Message{
967 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
968 &testpb.TestAllTypes_NestedMessage{},
969 }},
970 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
971 &test3pb.TestAllTypes_NestedMessage{},
972 }},
973 },
974 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
975 },
976 {
Joe Tsai6c286742019-07-11 23:15:05 -0700977 desc: "oneof (merged message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700978 decodeTo: []proto.Message{
979 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
980 &testpb.TestAllTypes_NestedMessage{
Joe Tsai0f81b382019-07-10 23:14:31 -0700981 A: proto.Int32(1),
Damien Neil3b46ade2019-03-26 13:55:02 -0700982 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700983 OptionalInt32: proto.Int32(43),
Damien Neil3b46ade2019-03-26 13:55:02 -0700984 },
Damien Neilba23aa52018-12-07 14:38:17 -0800985 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700986 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
987 &test3pb.TestAllTypes_NestedMessage{
Joe Tsai6c286742019-07-11 23:15:05 -0700988 A: 1,
Damien Neil3b46ade2019-03-26 13:55:02 -0700989 Corecursive: &test3pb.TestAllTypes{
990 OptionalInt32: 43,
991 },
992 },
993 }}},
Damien Neilba23aa52018-12-07 14:38:17 -0800994 wire: pack.Message{
995 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
996 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
997 }),
998 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
999 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1000 pack.Tag{1, pack.VarintType}, pack.Varint(43),
1001 }),
1002 }),
1003 }.Marshal(),
1004 },
1005 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001006 desc: "oneof (string)",
1007 decodeTo: []proto.Message{
1008 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
1009 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
1010 },
1011 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001012 },
1013 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001014 desc: "oneof (bytes)",
1015 decodeTo: []proto.Message{
1016 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
1017 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
1018 },
1019 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001020 },
1021 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001022 desc: "oneof (bool)",
1023 decodeTo: []proto.Message{
1024 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
1025 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
1026 },
1027 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001028 },
1029 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001030 desc: "oneof (uint64)",
1031 decodeTo: []proto.Message{
1032 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
1033 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
1034 },
1035 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001036 },
1037 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001038 desc: "oneof (float)",
1039 decodeTo: []proto.Message{
1040 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
1041 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
1042 },
1043 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001044 },
1045 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001046 desc: "oneof (double)",
1047 decodeTo: []proto.Message{
1048 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
1049 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
1050 },
1051 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001052 },
1053 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001054 desc: "oneof (enum)",
1055 decodeTo: []proto.Message{
1056 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
1057 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
1058 },
1059 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001060 },
1061 {
Damien Neilc37adef2019-04-01 13:49:56 -07001062 desc: "oneof (zero)",
1063 decodeTo: []proto.Message{
1064 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}},
1065 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}},
1066 },
1067 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
1068 },
1069 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001070 desc: "oneof (overridden value)",
1071 decodeTo: []proto.Message{
1072 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
1073 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
1074 },
Damien Neilba23aa52018-12-07 14:38:17 -08001075 wire: pack.Message{
1076 pack.Tag{111, pack.VarintType}, pack.Varint(1),
1077 pack.Tag{116, pack.VarintType}, pack.Varint(2),
1078 }.Marshal(),
1079 },
1080 // TODO: More unknown field tests for ordering, repeated fields, etc.
1081 //
1082 // It is currently impossible to produce results that the v1 Equal
1083 // considers equivalent to those of the v1 decoder. Figure out if
1084 // that's a problem or not.
1085 {
1086 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -08001087 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001088 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001089 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001090 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1091 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001092 ), build(
1093 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001094 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001095 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1096 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001097 )},
1098 wire: pack.Message{
1099 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1100 }.Marshal(),
1101 },
1102 {
1103 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001104 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001105 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001106 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001107 pack.Tag{1, pack.BytesType}, pack.String("string"),
1108 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001109 ), build(
1110 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001111 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001112 pack.Tag{1, pack.BytesType}, pack.String("string"),
1113 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001114 )},
1115 wire: pack.Message{
1116 pack.Tag{1, pack.BytesType}, pack.String("string"),
1117 }.Marshal(),
1118 },
1119 {
1120 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001121 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001122 &testpb.TestAllTypes{
1123 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -07001124 }, &test3pb.TestAllTypes{
1125 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -08001126 },
1127 },
1128 wire: pack.Message{
1129 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
1130 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1131 pack.Tag{2, pack.BytesType}, pack.String("string"),
1132 }),
1133 }.Marshal(),
1134 },
Damien Neil96c229a2019-04-03 12:17:24 -07001135 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001136 desc: "required field in nil message unset",
1137 partial: true,
1138 decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
1139 },
1140 {
Damien Neil96c229a2019-04-03 12:17:24 -07001141 desc: "required field unset",
1142 partial: true,
1143 decodeTo: []proto.Message{&testpb.TestRequired{}},
1144 },
1145 {
1146 desc: "required field set",
1147 decodeTo: []proto.Message{&testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001148 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001149 }},
1150 wire: pack.Message{
1151 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1152 }.Marshal(),
1153 },
1154 {
1155 desc: "required field in optional message unset",
1156 partial: true,
1157 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1158 OptionalMessage: &testpb.TestRequired{},
1159 }},
1160 wire: pack.Message{
1161 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1162 }.Marshal(),
1163 },
1164 {
1165 desc: "required field in optional message set",
1166 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1167 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001168 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001169 },
1170 }},
1171 wire: pack.Message{
1172 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1173 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1174 }),
1175 }.Marshal(),
1176 },
Damien Neil4686e232019-04-05 13:31:40 -07001177 {
1178 desc: "required field in optional message set (split across multiple tags)",
1179 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1180 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001181 RequiredField: proto.Int32(1),
Damien Neil4686e232019-04-05 13:31:40 -07001182 },
1183 }},
1184 wire: pack.Message{
1185 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1186 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1187 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1188 }),
1189 }.Marshal(),
1190 },
Damien Neil96c229a2019-04-03 12:17:24 -07001191 {
1192 desc: "required field in repeated message unset",
1193 partial: true,
1194 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1195 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001196 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001197 {},
1198 },
1199 }},
1200 wire: pack.Message{
1201 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1202 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1203 }),
1204 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1205 }.Marshal(),
1206 },
1207 {
1208 desc: "required field in repeated message set",
1209 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1210 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001211 {RequiredField: proto.Int32(1)},
1212 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001213 },
1214 }},
1215 wire: pack.Message{
1216 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1217 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1218 }),
1219 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1220 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1221 }),
1222 }.Marshal(),
1223 },
1224 {
1225 desc: "required field in map message unset",
1226 partial: true,
1227 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1228 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001229 1: {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001230 2: {},
1231 },
1232 }},
1233 wire: pack.Message{
1234 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1235 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1236 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1237 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1238 }),
1239 }),
1240 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1241 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1242 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1243 }),
1244 }.Marshal(),
1245 },
1246 {
1247 desc: "required field in map message set",
1248 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1249 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001250 1: {RequiredField: proto.Int32(1)},
1251 2: {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001252 },
1253 }},
1254 wire: pack.Message{
1255 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1256 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1257 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1258 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1259 }),
1260 }),
1261 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1262 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1263 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1264 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1265 }),
1266 }),
1267 }.Marshal(),
1268 },
1269 {
1270 desc: "required field in optional group unset",
1271 partial: true,
1272 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1273 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1274 }},
1275 wire: pack.Message{
1276 pack.Tag{1, pack.StartGroupType},
1277 pack.Tag{1, pack.EndGroupType},
1278 }.Marshal(),
1279 },
1280 {
1281 desc: "required field in optional group set",
1282 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1283 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001284 A: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001285 },
1286 }},
1287 wire: pack.Message{
1288 pack.Tag{1, pack.StartGroupType},
1289 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1290 pack.Tag{1, pack.EndGroupType},
1291 }.Marshal(),
1292 },
1293 {
1294 desc: "required field in repeated group unset",
1295 partial: true,
1296 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1297 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001298 {A: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001299 {},
1300 },
1301 }},
1302 wire: pack.Message{
1303 pack.Tag{3, pack.StartGroupType},
1304 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1305 pack.Tag{3, pack.EndGroupType},
1306 pack.Tag{3, pack.StartGroupType},
1307 pack.Tag{3, pack.EndGroupType},
1308 }.Marshal(),
1309 },
1310 {
1311 desc: "required field in repeated group set",
1312 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1313 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001314 {A: proto.Int32(1)},
1315 {A: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001316 },
1317 }},
1318 wire: pack.Message{
1319 pack.Tag{3, pack.StartGroupType},
1320 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1321 pack.Tag{3, pack.EndGroupType},
1322 pack.Tag{3, pack.StartGroupType},
1323 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1324 pack.Tag{3, pack.EndGroupType},
1325 }.Marshal(),
1326 },
1327 {
Damien Neil5322bdb2019-04-09 15:57:05 -07001328 desc: "required field in oneof message unset",
1329 partial: true,
1330 decodeTo: []proto.Message{
1331 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1332 &testpb.TestRequired{},
1333 }},
1334 },
1335 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1336 },
1337 {
1338 desc: "required field in oneof message set",
1339 decodeTo: []proto.Message{
1340 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1341 &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001342 RequiredField: proto.Int32(1),
Damien Neil5322bdb2019-04-09 15:57:05 -07001343 },
1344 }},
1345 },
1346 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
1347 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1348 })}.Marshal(),
1349 },
1350 {
Joe Tsai09cef322019-07-11 22:13:49 -07001351 desc: "required field in extension message unset",
1352 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001353 decodeTo: []proto.Message{build(
1354 &testpb.TestAllExtensions{},
1355 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1356 )},
1357 wire: pack.Message{
1358 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1359 }.Marshal(),
1360 },
1361 {
1362 desc: "required field in extension message set",
1363 decodeTo: []proto.Message{build(
1364 &testpb.TestAllExtensions{},
1365 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001366 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001367 }),
1368 )},
1369 wire: pack.Message{
1370 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1371 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1372 }),
1373 }.Marshal(),
1374 },
1375 {
Joe Tsai09cef322019-07-11 22:13:49 -07001376 desc: "required field in repeated extension message unset",
1377 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001378 decodeTo: []proto.Message{build(
1379 &testpb.TestAllExtensions{},
1380 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001381 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001382 {},
1383 }),
1384 )},
1385 wire: pack.Message{
1386 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1387 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1388 }),
1389 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1390 }.Marshal(),
1391 },
1392 {
1393 desc: "required field in repeated extension message set",
1394 decodeTo: []proto.Message{build(
1395 &testpb.TestAllExtensions{},
1396 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001397 {RequiredField: proto.Int32(1)},
1398 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001399 }),
1400 )},
1401 wire: pack.Message{
1402 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1403 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1404 }),
1405 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1406 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1407 }),
1408 }.Marshal(),
1409 },
Damien Neilc37adef2019-04-01 13:49:56 -07001410 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001411 desc: "nil messages",
1412 decodeTo: []proto.Message{
1413 (*testpb.TestAllTypes)(nil),
1414 (*test3pb.TestAllTypes)(nil),
1415 (*testpb.TestAllExtensions)(nil),
1416 },
1417 },
1418 {
Damien Neilc37adef2019-04-01 13:49:56 -07001419 desc: "legacy",
1420 partial: true,
1421 decodeTo: []proto.Message{
1422 &legacypb.Legacy{
1423 F1: &legacy1pb.Message{
Damien Neila8a2cea2019-07-10 16:17:16 -07001424 OptionalInt32: proto.Int32(1),
Damien Neilc37adef2019-04-01 13:49:56 -07001425 OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
1426 OptionalChildMessage: &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001427 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001428 },
1429 Optionalgroup: &legacy1pb.Message_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001430 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001431 },
1432 RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001433 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001434 },
1435 Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001436 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001437 },
1438 MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001439 true: {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001440 },
1441 OneofUnion: &legacy1pb.Message_OneofChildMessage{
1442 &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001443 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001444 },
1445 },
1446 },
1447 },
1448 },
1449 wire: pack.Message{
1450 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1451 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1452 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1453 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1454 pack.Tag{1, pack.BytesType}, pack.String("x"),
1455 }),
1456 pack.Tag{120, pack.StartGroupType},
1457 pack.Tag{1, pack.BytesType}, pack.String("x"),
1458 pack.Tag{120, pack.EndGroupType},
1459 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1460 pack.Tag{1, pack.BytesType}, pack.String("x"),
1461 }),
1462 pack.Tag{520, pack.StartGroupType},
1463 pack.Tag{1, pack.BytesType}, pack.String("x"),
1464 pack.Tag{520, pack.EndGroupType},
1465 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1466 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1467 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1468 pack.Tag{1, pack.BytesType}, pack.String("x"),
1469 }),
1470 }),
1471 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1472 pack.Tag{1, pack.BytesType}, pack.String("x"),
1473 }),
1474 }),
1475 }.Marshal(),
1476 },
Damien Neilba23aa52018-12-07 14:38:17 -08001477}
1478
Damien Neilbc310b52019-04-11 11:46:55 -07001479var invalidUTF8TestProtos = []testProto{
1480 {
1481 desc: "invalid UTF-8 in optional string field",
1482 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1483 OptionalString: "abc\xff",
1484 }},
1485 wire: pack.Message{
1486 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1487 }.Marshal(),
1488 },
1489 {
1490 desc: "invalid UTF-8 in repeated string field",
1491 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1492 RepeatedString: []string{"foo", "abc\xff"},
1493 }},
1494 wire: pack.Message{
1495 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1496 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1497 }.Marshal(),
1498 },
1499 {
1500 desc: "invalid UTF-8 in nested message",
1501 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1502 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1503 Corecursive: &test3pb.TestAllTypes{
1504 OptionalString: "abc\xff",
1505 },
1506 },
1507 }},
1508 wire: pack.Message{
1509 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1510 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1511 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1512 }),
1513 }),
1514 }.Marshal(),
1515 },
1516 {
Damien Neilc37adef2019-04-01 13:49:56 -07001517 desc: "invalid UTF-8 in oneof field",
1518 decodeTo: []proto.Message{
1519 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
1520 },
1521 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1522 },
1523 {
Damien Neilbc310b52019-04-11 11:46:55 -07001524 desc: "invalid UTF-8 in map key",
1525 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1526 MapStringString: map[string]string{"key\xff": "val"},
1527 }},
1528 wire: pack.Message{
1529 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1530 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1531 pack.Tag{2, pack.BytesType}, pack.String("val"),
1532 }),
1533 }.Marshal(),
1534 },
1535 {
1536 desc: "invalid UTF-8 in map value",
1537 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1538 MapStringString: map[string]string{"key": "val\xff"},
1539 }},
1540 wire: pack.Message{
1541 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1542 pack.Tag{1, pack.BytesType}, pack.String("key"),
1543 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1544 }),
1545 }.Marshal(),
1546 },
1547}
1548
Joe Tsaic51e2e02019-07-13 00:44:41 -07001549var noEnforceUTF8TestProtos = []testProto{
1550 {
1551 desc: "invalid UTF-8 in optional string field",
1552 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1553 OptionalString: string("abc\xff"),
1554 }},
1555 wire: pack.Message{
1556 pack.Tag{1, pack.BytesType}, pack.String("abc\xff"),
1557 }.Marshal(),
1558 },
1559 {
1560 desc: "invalid UTF-8 in optional string field of Go bytes",
1561 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1562 OptionalBytes: []byte("abc\xff"),
1563 }},
1564 wire: pack.Message{
1565 pack.Tag{2, pack.BytesType}, pack.String("abc\xff"),
1566 }.Marshal(),
1567 },
1568 {
1569 desc: "invalid UTF-8 in repeated string field",
1570 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1571 RepeatedString: []string{string("foo"), string("abc\xff")},
1572 }},
1573 wire: pack.Message{
1574 pack.Tag{3, pack.BytesType}, pack.String("foo"),
1575 pack.Tag{3, pack.BytesType}, pack.String("abc\xff"),
1576 }.Marshal(),
1577 },
1578 {
1579 desc: "invalid UTF-8 in repeated string field of Go bytes",
1580 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1581 RepeatedBytes: [][]byte{[]byte("foo"), []byte("abc\xff")},
1582 }},
1583 wire: pack.Message{
1584 pack.Tag{4, pack.BytesType}, pack.String("foo"),
1585 pack.Tag{4, pack.BytesType}, pack.String("abc\xff"),
1586 }.Marshal(),
1587 },
1588 {
1589 desc: "invalid UTF-8 in oneof string field",
1590 decodeTo: []proto.Message{
1591 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofString{string("abc\xff")}},
1592 },
1593 wire: pack.Message{pack.Tag{5, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1594 },
1595 {
1596 desc: "invalid UTF-8 in oneof string field of Go bytes",
1597 decodeTo: []proto.Message{
1598 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofBytes{[]byte("abc\xff")}},
1599 },
1600 wire: pack.Message{pack.Tag{6, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1601 },
1602}
1603
1604type TestNoEnforceUTF8 struct {
1605 OptionalString string `protobuf:"bytes,1,opt,name=optional_string"`
1606 OptionalBytes []byte `protobuf:"bytes,2,opt,name=optional_bytes"`
1607 RepeatedString []string `protobuf:"bytes,3,rep,name=repeated_string"`
1608 RepeatedBytes [][]byte `protobuf:"bytes,4,rep,name=repeated_bytes"`
1609 OneofField isOneofField `protobuf_oneof:"oneof_field"`
1610}
1611
1612type isOneofField interface{ isOneofField() }
1613
1614type TestNoEnforceUTF8_OneofString struct {
1615 OneofString string `protobuf:"bytes,5,opt,name=oneof_string,oneof"`
1616}
1617type TestNoEnforceUTF8_OneofBytes struct {
1618 OneofBytes []byte `protobuf:"bytes,6,opt,name=oneof_bytes,oneof"`
1619}
1620
1621func (*TestNoEnforceUTF8_OneofString) isOneofField() {}
1622func (*TestNoEnforceUTF8_OneofBytes) isOneofField() {}
1623
Joe Tsai74615a32019-07-14 18:51:46 -07001624func (m *TestNoEnforceUTF8) ProtoReflect() protoreflect.Message {
Joe Tsaic51e2e02019-07-13 00:44:41 -07001625 return messageInfo_TestNoEnforceUTF8.MessageOf(m)
1626}
1627
1628var messageInfo_TestNoEnforceUTF8 = protoimpl.MessageInfo{
Damien Neil16163b42019-08-06 15:43:25 -07001629 GoReflectType: reflect.TypeOf((*TestNoEnforceUTF8)(nil)),
1630 Desc: func() protoreflect.MessageDescriptor {
1631 pb := new(descriptorpb.FileDescriptorProto)
1632 if err := prototext.Unmarshal([]byte(`
Joe Tsaic51e2e02019-07-13 00:44:41 -07001633 syntax: "proto3"
1634 name: "test.proto"
1635 message_type: [{
1636 name: "TestNoEnforceUTF8"
1637 field: [
1638 {name:"optional_string" number:1 label:LABEL_OPTIONAL type:TYPE_STRING},
1639 {name:"optional_bytes" number:2 label:LABEL_OPTIONAL type:TYPE_STRING},
1640 {name:"repeated_string" number:3 label:LABEL_REPEATED type:TYPE_STRING},
1641 {name:"repeated_bytes" number:4 label:LABEL_REPEATED type:TYPE_STRING},
1642 {name:"oneof_string" number:5 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0},
1643 {name:"oneof_bytes" number:6 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0}
1644 ]
1645 oneof_decl: [{name:"oneof_field"}]
1646 }]
1647 `), pb); err != nil {
Damien Neil16163b42019-08-06 15:43:25 -07001648 panic(err)
1649 }
1650 fd, err := protodesc.NewFile(pb, nil)
1651 if err != nil {
1652 panic(err)
1653 }
1654 md := fd.Messages().Get(0)
1655 for i := 0; i < md.Fields().Len(); i++ {
1656 md.Fields().Get(i).(*filedesc.Field).L1.HasEnforceUTF8 = true
1657 md.Fields().Get(i).(*filedesc.Field).L1.EnforceUTF8 = false
1658 }
1659 return md
1660 }(),
Joe Tsaic51e2e02019-07-13 00:44:41 -07001661 OneofWrappers: []interface{}{
1662 (*TestNoEnforceUTF8_OneofString)(nil),
1663 (*TestNoEnforceUTF8_OneofBytes)(nil),
1664 },
1665}
1666
Damien Neil4be2fb42018-12-17 11:16:16 -08001667func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001668 for _, opt := range opts {
1669 opt(m)
1670 }
1671 return m
1672}
1673
Damien Neil4be2fb42018-12-17 11:16:16 -08001674type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001675
Joe Tsai74615a32019-07-14 18:51:46 -07001676func unknown(raw protoreflect.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001677 return func(m proto.Message) {
Joe Tsai378c1322019-04-25 23:48:08 -07001678 m.ProtoReflect().SetUnknown(raw)
Damien Neile6f060f2019-04-23 17:11:02 -07001679 }
1680}
1681
Joe Tsai74615a32019-07-14 18:51:46 -07001682func extend(desc *protoiface.ExtensionDescV1, value interface{}) buildOpt {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001683 // TODO: Should ExtensionType.ValueOf accept []T instead of *[]T?
1684 t := reflect.TypeOf(value)
1685 if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
1686 v := reflect.New(t)
1687 v.Elem().Set(reflect.ValueOf(value))
1688 value = v.Interface()
1689 }
Damien Neil4be2fb42018-12-17 11:16:16 -08001690 return func(m proto.Message) {
Damien Neil92f76182019-08-02 16:58:08 -07001691 proto.SetExtension(m, desc, value)
Damien Neilba23aa52018-12-07 14:38:17 -08001692 }
1693}
Damien Neil61e93c72019-03-27 09:23:20 -07001694
1695func marshalText(m proto.Message) string {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001696 b, _ := prototext.MarshalOptions{Indent: "\t", AllowPartial: true}.Marshal(m)
Damien Neil61e93c72019-03-27 09:23:20 -07001697 return string(b)
1698}