blob: 9819838edec05e42177accbbc67454a9a3b9ef64 [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
Damien Neilba23aa52018-12-07 14:38:17 -0800125var testProtos = []testProto{
126 {
127 desc: "basic scalar types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800128 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700129 OptionalInt32: proto.Int32(1001),
130 OptionalInt64: proto.Int64(1002),
131 OptionalUint32: proto.Uint32(1003),
132 OptionalUint64: proto.Uint64(1004),
133 OptionalSint32: proto.Int32(1005),
134 OptionalSint64: proto.Int64(1006),
135 OptionalFixed32: proto.Uint32(1007),
136 OptionalFixed64: proto.Uint64(1008),
137 OptionalSfixed32: proto.Int32(1009),
138 OptionalSfixed64: proto.Int64(1010),
139 OptionalFloat: proto.Float32(1011.5),
140 OptionalDouble: proto.Float64(1012.5),
141 OptionalBool: proto.Bool(true),
142 OptionalString: proto.String("string"),
Damien Neilba23aa52018-12-07 14:38:17 -0800143 OptionalBytes: []byte("bytes"),
144 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
Damien Neil3b46ade2019-03-26 13:55:02 -0700145 }, &test3pb.TestAllTypes{
146 OptionalInt32: 1001,
147 OptionalInt64: 1002,
148 OptionalUint32: 1003,
149 OptionalUint64: 1004,
150 OptionalSint32: 1005,
151 OptionalSint64: 1006,
152 OptionalFixed32: 1007,
153 OptionalFixed64: 1008,
154 OptionalSfixed32: 1009,
155 OptionalSfixed64: 1010,
156 OptionalFloat: 1011.5,
157 OptionalDouble: 1012.5,
158 OptionalBool: true,
159 OptionalString: "string",
160 OptionalBytes: []byte("bytes"),
161 OptionalNestedEnum: test3pb.TestAllTypes_BAR,
Damien Neilba23aa52018-12-07 14:38:17 -0800162 }, build(
163 &testpb.TestAllExtensions{},
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700164 extend(testpb.E_OptionalInt32Extension, int32(1001)),
165 extend(testpb.E_OptionalInt64Extension, int64(1002)),
166 extend(testpb.E_OptionalUint32Extension, uint32(1003)),
167 extend(testpb.E_OptionalUint64Extension, uint64(1004)),
168 extend(testpb.E_OptionalSint32Extension, int32(1005)),
169 extend(testpb.E_OptionalSint64Extension, int64(1006)),
170 extend(testpb.E_OptionalFixed32Extension, uint32(1007)),
171 extend(testpb.E_OptionalFixed64Extension, uint64(1008)),
172 extend(testpb.E_OptionalSfixed32Extension, int32(1009)),
173 extend(testpb.E_OptionalSfixed64Extension, int64(1010)),
174 extend(testpb.E_OptionalFloatExtension, float32(1011.5)),
175 extend(testpb.E_OptionalDoubleExtension, float64(1012.5)),
176 extend(testpb.E_OptionalBoolExtension, bool(true)),
177 extend(testpb.E_OptionalStringExtension, string("string")),
Damien Neilba23aa52018-12-07 14:38:17 -0800178 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700179 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR),
Damien Neilba23aa52018-12-07 14:38:17 -0800180 )},
181 wire: pack.Message{
182 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
183 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
184 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
185 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
186 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
187 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
188 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
189 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
190 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
191 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
192 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
193 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
194 pack.Tag{13, pack.VarintType}, pack.Bool(true),
195 pack.Tag{14, pack.BytesType}, pack.String("string"),
196 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
197 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
198 }.Marshal(),
199 },
200 {
Damien Neil8003f082019-08-02 15:13:00 -0700201 desc: "zero values",
202 decodeTo: []proto.Message{&testpb.TestAllTypes{
203 OptionalInt32: proto.Int32(0),
204 OptionalInt64: proto.Int64(0),
205 OptionalUint32: proto.Uint32(0),
206 OptionalUint64: proto.Uint64(0),
207 OptionalSint32: proto.Int32(0),
208 OptionalSint64: proto.Int64(0),
209 OptionalFixed32: proto.Uint32(0),
210 OptionalFixed64: proto.Uint64(0),
211 OptionalSfixed32: proto.Int32(0),
212 OptionalSfixed64: proto.Int64(0),
213 OptionalFloat: proto.Float32(0),
214 OptionalDouble: proto.Float64(0),
215 OptionalBool: proto.Bool(false),
216 OptionalString: proto.String(""),
217 OptionalBytes: []byte{},
218 }, &test3pb.TestAllTypes{}, build(
219 &testpb.TestAllExtensions{},
220 extend(testpb.E_OptionalInt32Extension, int32(0)),
221 extend(testpb.E_OptionalInt64Extension, int64(0)),
222 extend(testpb.E_OptionalUint32Extension, uint32(0)),
223 extend(testpb.E_OptionalUint64Extension, uint64(0)),
224 extend(testpb.E_OptionalSint32Extension, int32(0)),
225 extend(testpb.E_OptionalSint64Extension, int64(0)),
226 extend(testpb.E_OptionalFixed32Extension, uint32(0)),
227 extend(testpb.E_OptionalFixed64Extension, uint64(0)),
228 extend(testpb.E_OptionalSfixed32Extension, int32(0)),
229 extend(testpb.E_OptionalSfixed64Extension, int64(0)),
230 extend(testpb.E_OptionalFloatExtension, float32(0)),
231 extend(testpb.E_OptionalDoubleExtension, float64(0)),
232 extend(testpb.E_OptionalBoolExtension, bool(false)),
233 extend(testpb.E_OptionalStringExtension, string("")),
234 extend(testpb.E_OptionalBytesExtension, []byte{}),
235 )},
236 wire: pack.Message{
237 pack.Tag{1, pack.VarintType}, pack.Varint(0),
238 pack.Tag{2, pack.VarintType}, pack.Varint(0),
239 pack.Tag{3, pack.VarintType}, pack.Uvarint(0),
240 pack.Tag{4, pack.VarintType}, pack.Uvarint(0),
241 pack.Tag{5, pack.VarintType}, pack.Svarint(0),
242 pack.Tag{6, pack.VarintType}, pack.Svarint(0),
243 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(0),
244 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(0),
245 pack.Tag{9, pack.Fixed32Type}, pack.Int32(0),
246 pack.Tag{10, pack.Fixed64Type}, pack.Int64(0),
247 pack.Tag{11, pack.Fixed32Type}, pack.Float32(0),
248 pack.Tag{12, pack.Fixed64Type}, pack.Float64(0),
249 pack.Tag{13, pack.VarintType}, pack.Bool(false),
250 pack.Tag{14, pack.BytesType}, pack.String(""),
251 pack.Tag{15, pack.BytesType}, pack.Bytes(nil),
252 }.Marshal(),
253 },
254 {
Damien Neilba23aa52018-12-07 14:38:17 -0800255 desc: "groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800256 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800257 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700258 A: proto.Int32(1017),
Damien Neilba23aa52018-12-07 14:38:17 -0800259 },
260 }, build(
261 &testpb.TestAllExtensions{},
262 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700263 A: proto.Int32(1017),
Damien Neilba23aa52018-12-07 14:38:17 -0800264 }),
265 )},
266 wire: pack.Message{
267 pack.Tag{16, pack.StartGroupType},
268 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
269 pack.Tag{16, pack.EndGroupType},
270 }.Marshal(),
271 },
272 {
273 desc: "groups (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800274 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800275 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700276 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800277 },
278 }, build(
279 &testpb.TestAllExtensions{},
280 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700281 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800282 }),
283 )},
284 wire: pack.Message{
285 pack.Tag{16, pack.StartGroupType},
286 pack.Tag{17, pack.VarintType}, pack.Varint(1),
287 pack.Tag{16, pack.EndGroupType},
288 pack.Tag{16, pack.StartGroupType},
289 pack.Tag{17, pack.VarintType}, pack.Varint(2),
290 pack.Tag{16, pack.EndGroupType},
291 }.Marshal(),
292 },
293 {
294 desc: "messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800295 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800296 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700297 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800298 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700299 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800300 },
301 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700302 }, &test3pb.TestAllTypes{
303 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
304 A: 42,
305 Corecursive: &test3pb.TestAllTypes{
306 OptionalInt32: 43,
307 },
308 },
Damien Neilba23aa52018-12-07 14:38:17 -0800309 }, build(
310 &testpb.TestAllExtensions{},
311 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700312 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800313 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700314 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800315 },
316 }),
317 )},
318 wire: pack.Message{
319 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
320 pack.Tag{1, pack.VarintType}, pack.Varint(42),
321 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
322 pack.Tag{1, pack.VarintType}, pack.Varint(43),
323 }),
324 }),
325 }.Marshal(),
326 },
327 {
328 desc: "messages (split across multiple tags)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800329 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800330 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700331 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800332 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700333 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800334 },
335 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700336 }, &test3pb.TestAllTypes{
337 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
338 A: 42,
339 Corecursive: &test3pb.TestAllTypes{
340 OptionalInt32: 43,
341 },
342 },
Damien Neilba23aa52018-12-07 14:38:17 -0800343 }, build(
344 &testpb.TestAllExtensions{},
345 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700346 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800347 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700348 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800349 },
350 }),
351 )},
352 wire: pack.Message{
353 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
354 pack.Tag{1, pack.VarintType}, pack.Varint(42),
355 }),
356 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
357 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
358 pack.Tag{1, pack.VarintType}, pack.Varint(43),
359 }),
360 }),
361 }.Marshal(),
362 },
363 {
364 desc: "messages (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800365 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800366 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700367 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800368 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700369 }, &test3pb.TestAllTypes{
370 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
371 A: 2,
372 },
Damien Neilba23aa52018-12-07 14:38:17 -0800373 }, build(
374 &testpb.TestAllExtensions{},
375 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700376 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800377 }),
378 )},
379 wire: pack.Message{
380 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
381 pack.Tag{1, pack.VarintType}, pack.Varint(1),
382 }),
383 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
384 pack.Tag{1, pack.VarintType}, pack.Varint(2),
385 }),
386 }.Marshal(),
387 },
388 {
389 desc: "basic repeated types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800390 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800391 RepeatedInt32: []int32{1001, 2001},
392 RepeatedInt64: []int64{1002, 2002},
393 RepeatedUint32: []uint32{1003, 2003},
394 RepeatedUint64: []uint64{1004, 2004},
395 RepeatedSint32: []int32{1005, 2005},
396 RepeatedSint64: []int64{1006, 2006},
397 RepeatedFixed32: []uint32{1007, 2007},
398 RepeatedFixed64: []uint64{1008, 2008},
399 RepeatedSfixed32: []int32{1009, 2009},
400 RepeatedSfixed64: []int64{1010, 2010},
401 RepeatedFloat: []float32{1011.5, 2011.5},
402 RepeatedDouble: []float64{1012.5, 2012.5},
403 RepeatedBool: []bool{true, false},
404 RepeatedString: []string{"foo", "bar"},
405 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
406 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
407 testpb.TestAllTypes_FOO,
408 testpb.TestAllTypes_BAR,
409 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700410 }, &test3pb.TestAllTypes{
411 RepeatedInt32: []int32{1001, 2001},
412 RepeatedInt64: []int64{1002, 2002},
413 RepeatedUint32: []uint32{1003, 2003},
414 RepeatedUint64: []uint64{1004, 2004},
415 RepeatedSint32: []int32{1005, 2005},
416 RepeatedSint64: []int64{1006, 2006},
417 RepeatedFixed32: []uint32{1007, 2007},
418 RepeatedFixed64: []uint64{1008, 2008},
419 RepeatedSfixed32: []int32{1009, 2009},
420 RepeatedSfixed64: []int64{1010, 2010},
421 RepeatedFloat: []float32{1011.5, 2011.5},
422 RepeatedDouble: []float64{1012.5, 2012.5},
423 RepeatedBool: []bool{true, false},
424 RepeatedString: []string{"foo", "bar"},
425 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
426 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
427 test3pb.TestAllTypes_FOO,
428 test3pb.TestAllTypes_BAR,
429 },
Damien Neilba23aa52018-12-07 14:38:17 -0800430 }, build(
431 &testpb.TestAllExtensions{},
432 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
433 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
434 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
435 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
436 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
437 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
438 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
439 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
440 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
441 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
442 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
443 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
444 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
445 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
446 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
447 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
448 testpb.TestAllTypes_FOO,
449 testpb.TestAllTypes_BAR,
450 }),
451 )},
452 wire: pack.Message{
453 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
454 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
455 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
456 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
457 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
458 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
459 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
460 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
461 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
462 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
463 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
464 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
465 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
466 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
467 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
468 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
469 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
470 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
471 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
472 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
473 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
474 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
475 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
476 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
477 pack.Tag{43, pack.VarintType}, pack.Bool(true),
478 pack.Tag{43, pack.VarintType}, pack.Bool(false),
479 pack.Tag{44, pack.BytesType}, pack.String("foo"),
480 pack.Tag{44, pack.BytesType}, pack.String("bar"),
481 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
482 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
483 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
484 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
485 }.Marshal(),
486 },
487 {
488 desc: "basic repeated types (packed encoding)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800489 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800490 RepeatedInt32: []int32{1001, 2001},
491 RepeatedInt64: []int64{1002, 2002},
492 RepeatedUint32: []uint32{1003, 2003},
493 RepeatedUint64: []uint64{1004, 2004},
494 RepeatedSint32: []int32{1005, 2005},
495 RepeatedSint64: []int64{1006, 2006},
496 RepeatedFixed32: []uint32{1007, 2007},
497 RepeatedFixed64: []uint64{1008, 2008},
498 RepeatedSfixed32: []int32{1009, 2009},
499 RepeatedSfixed64: []int64{1010, 2010},
500 RepeatedFloat: []float32{1011.5, 2011.5},
501 RepeatedDouble: []float64{1012.5, 2012.5},
502 RepeatedBool: []bool{true, false},
503 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
504 testpb.TestAllTypes_FOO,
505 testpb.TestAllTypes_BAR,
506 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700507 }, &test3pb.TestAllTypes{
508 RepeatedInt32: []int32{1001, 2001},
509 RepeatedInt64: []int64{1002, 2002},
510 RepeatedUint32: []uint32{1003, 2003},
511 RepeatedUint64: []uint64{1004, 2004},
512 RepeatedSint32: []int32{1005, 2005},
513 RepeatedSint64: []int64{1006, 2006},
514 RepeatedFixed32: []uint32{1007, 2007},
515 RepeatedFixed64: []uint64{1008, 2008},
516 RepeatedSfixed32: []int32{1009, 2009},
517 RepeatedSfixed64: []int64{1010, 2010},
518 RepeatedFloat: []float32{1011.5, 2011.5},
519 RepeatedDouble: []float64{1012.5, 2012.5},
520 RepeatedBool: []bool{true, false},
521 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
522 test3pb.TestAllTypes_FOO,
523 test3pb.TestAllTypes_BAR,
524 },
Damien Neilba23aa52018-12-07 14:38:17 -0800525 }, build(
526 &testpb.TestAllExtensions{},
527 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
528 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
529 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
530 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
531 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
532 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
533 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
534 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
535 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
536 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
537 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
538 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
539 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
540 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
541 testpb.TestAllTypes_FOO,
542 testpb.TestAllTypes_BAR,
543 }),
544 )},
545 wire: pack.Message{
546 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
547 pack.Varint(1001), pack.Varint(2001),
548 },
549 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
550 pack.Varint(1002), pack.Varint(2002),
551 },
552 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
553 pack.Uvarint(1003), pack.Uvarint(2003),
554 },
555 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
556 pack.Uvarint(1004), pack.Uvarint(2004),
557 },
558 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
559 pack.Svarint(1005), pack.Svarint(2005),
560 },
561 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
562 pack.Svarint(1006), pack.Svarint(2006),
563 },
564 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
565 pack.Uint32(1007), pack.Uint32(2007),
566 },
567 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
568 pack.Uint64(1008), pack.Uint64(2008),
569 },
570 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
571 pack.Int32(1009), pack.Int32(2009),
572 },
573 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
574 pack.Int64(1010), pack.Int64(2010),
575 },
576 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
577 pack.Float32(1011.5), pack.Float32(2011.5),
578 },
579 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
580 pack.Float64(1012.5), pack.Float64(2012.5),
581 },
582 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
583 pack.Bool(true), pack.Bool(false),
584 },
585 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
586 pack.Varint(int(testpb.TestAllTypes_FOO)),
587 pack.Varint(int(testpb.TestAllTypes_BAR)),
588 },
589 }.Marshal(),
590 },
591 {
Damien Neil7492a092019-07-10 15:23:29 -0700592 desc: "packed repeated types",
593 decodeTo: []proto.Message{&testpb.TestPackedTypes{
594 PackedInt32: []int32{1001, 2001},
595 PackedInt64: []int64{1002, 2002},
596 PackedUint32: []uint32{1003, 2003},
597 PackedUint64: []uint64{1004, 2004},
598 PackedSint32: []int32{1005, 2005},
599 PackedSint64: []int64{1006, 2006},
600 PackedFixed32: []uint32{1007, 2007},
601 PackedFixed64: []uint64{1008, 2008},
602 PackedSfixed32: []int32{1009, 2009},
603 PackedSfixed64: []int64{1010, 2010},
604 PackedFloat: []float32{1011.5, 2011.5},
605 PackedDouble: []float64{1012.5, 2012.5},
606 PackedBool: []bool{true, false},
607 PackedEnum: []testpb.ForeignEnum{
608 testpb.ForeignEnum_FOREIGN_FOO,
609 testpb.ForeignEnum_FOREIGN_BAR,
610 },
611 }, build(
612 &testpb.TestPackedExtensions{},
613 extend(testpb.E_PackedInt32Extension, []int32{1001, 2001}),
614 extend(testpb.E_PackedInt64Extension, []int64{1002, 2002}),
615 extend(testpb.E_PackedUint32Extension, []uint32{1003, 2003}),
616 extend(testpb.E_PackedUint64Extension, []uint64{1004, 2004}),
617 extend(testpb.E_PackedSint32Extension, []int32{1005, 2005}),
618 extend(testpb.E_PackedSint64Extension, []int64{1006, 2006}),
619 extend(testpb.E_PackedFixed32Extension, []uint32{1007, 2007}),
620 extend(testpb.E_PackedFixed64Extension, []uint64{1008, 2008}),
621 extend(testpb.E_PackedSfixed32Extension, []int32{1009, 2009}),
622 extend(testpb.E_PackedSfixed64Extension, []int64{1010, 2010}),
623 extend(testpb.E_PackedFloatExtension, []float32{1011.5, 2011.5}),
624 extend(testpb.E_PackedDoubleExtension, []float64{1012.5, 2012.5}),
625 extend(testpb.E_PackedBoolExtension, []bool{true, false}),
626 extend(testpb.E_PackedEnumExtension, []testpb.ForeignEnum{
627 testpb.ForeignEnum_FOREIGN_FOO,
628 testpb.ForeignEnum_FOREIGN_BAR,
629 }),
630 )},
631 wire: pack.Message{
632 pack.Tag{90, pack.BytesType}, pack.LengthPrefix{
633 pack.Varint(1001), pack.Varint(2001),
634 },
635 pack.Tag{91, pack.BytesType}, pack.LengthPrefix{
636 pack.Varint(1002), pack.Varint(2002),
637 },
638 pack.Tag{92, pack.BytesType}, pack.LengthPrefix{
639 pack.Uvarint(1003), pack.Uvarint(2003),
640 },
641 pack.Tag{93, pack.BytesType}, pack.LengthPrefix{
642 pack.Uvarint(1004), pack.Uvarint(2004),
643 },
644 pack.Tag{94, pack.BytesType}, pack.LengthPrefix{
645 pack.Svarint(1005), pack.Svarint(2005),
646 },
647 pack.Tag{95, pack.BytesType}, pack.LengthPrefix{
648 pack.Svarint(1006), pack.Svarint(2006),
649 },
650 pack.Tag{96, pack.BytesType}, pack.LengthPrefix{
651 pack.Uint32(1007), pack.Uint32(2007),
652 },
653 pack.Tag{97, pack.BytesType}, pack.LengthPrefix{
654 pack.Uint64(1008), pack.Uint64(2008),
655 },
656 pack.Tag{98, pack.BytesType}, pack.LengthPrefix{
657 pack.Int32(1009), pack.Int32(2009),
658 },
659 pack.Tag{99, pack.BytesType}, pack.LengthPrefix{
660 pack.Int64(1010), pack.Int64(2010),
661 },
662 pack.Tag{100, pack.BytesType}, pack.LengthPrefix{
663 pack.Float32(1011.5), pack.Float32(2011.5),
664 },
665 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{
666 pack.Float64(1012.5), pack.Float64(2012.5),
667 },
668 pack.Tag{102, pack.BytesType}, pack.LengthPrefix{
669 pack.Bool(true), pack.Bool(false),
670 },
671 pack.Tag{103, pack.BytesType}, pack.LengthPrefix{
672 pack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
673 pack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
674 },
675 }.Marshal(),
676 },
677 {
Damien Neilba23aa52018-12-07 14:38:17 -0800678 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800679 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800680 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700681 {A: proto.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700682 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700683 {A: proto.Int32(2)},
Damien Neilba23aa52018-12-07 14:38:17 -0800684 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700685 }, &test3pb.TestAllTypes{
686 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
687 {A: 1},
Damien Neilc37adef2019-04-01 13:49:56 -0700688 nil,
Damien Neil3b46ade2019-03-26 13:55:02 -0700689 {A: 2},
690 },
Damien Neilba23aa52018-12-07 14:38:17 -0800691 }, build(
692 &testpb.TestAllExtensions{},
693 extend(testpb.E_RepeatedNestedMessageExtension, []*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 }),
698 )},
699 wire: pack.Message{
700 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
701 pack.Tag{1, pack.VarintType}, pack.Varint(1),
702 }),
Damien Neilc37adef2019-04-01 13:49:56 -0700703 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
Damien Neilba23aa52018-12-07 14:38:17 -0800704 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
705 pack.Tag{1, pack.VarintType}, pack.Varint(2),
706 }),
707 }.Marshal(),
708 },
709 {
710 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800711 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800712 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700713 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700714 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700715 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800716 },
717 }, build(
718 &testpb.TestAllExtensions{},
719 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700720 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700721 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700722 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800723 }),
724 )},
725 wire: pack.Message{
726 pack.Tag{46, pack.StartGroupType},
727 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
728 pack.Tag{46, pack.EndGroupType},
729 pack.Tag{46, pack.StartGroupType},
Damien Neilc37adef2019-04-01 13:49:56 -0700730 pack.Tag{46, pack.EndGroupType},
731 pack.Tag{46, pack.StartGroupType},
Damien Neilba23aa52018-12-07 14:38:17 -0800732 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
733 pack.Tag{46, pack.EndGroupType},
734 }.Marshal(),
735 },
736 {
737 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800738 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800739 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
740 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
741 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
742 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
743 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
744 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
745 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
746 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
747 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
748 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
749 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
750 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
751 MapBoolBool: map[bool]bool{true: false, false: true},
752 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
753 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
754 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700755 "71.1.key": {A: proto.Int32(1171)},
756 "71.2.key": {A: proto.Int32(2171)},
Damien Neilba23aa52018-12-07 14:38:17 -0800757 },
758 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
759 "73.1.key": testpb.TestAllTypes_FOO,
760 "73.2.key": testpb.TestAllTypes_BAR,
761 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700762 }, &test3pb.TestAllTypes{
763 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
764 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
765 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
766 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
767 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
768 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
769 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
770 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
771 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
772 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
773 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
774 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
775 MapBoolBool: map[bool]bool{true: false, false: true},
776 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
777 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
778 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
779 "71.1.key": {A: 1171},
780 "71.2.key": {A: 2171},
781 },
782 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
783 "73.1.key": test3pb.TestAllTypes_FOO,
784 "73.2.key": test3pb.TestAllTypes_BAR,
785 },
Damien Neilba23aa52018-12-07 14:38:17 -0800786 }},
787 wire: pack.Message{
788 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
789 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
790 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
791 }),
792 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
793 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
794 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
795 }),
796 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
797 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
798 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
799 }),
800 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
801 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
802 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
803 }),
804 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
805 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
806 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
807 }),
808 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
809 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
810 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
811 }),
812 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
813 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
814 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
815 }),
816 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
817 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
818 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
819 }),
820 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
821 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
822 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
823 }),
824 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
825 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
826 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
827 }),
828 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
829 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
830 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
831 }),
832 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
833 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
834 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
835 }),
836 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
837 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
838 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
839 }),
840 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
841 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
842 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
843 }),
844 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
845 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
846 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
847 }),
848 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
849 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
850 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
851 }),
852 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
853 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
854 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
855 }),
856 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
857 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
858 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
859 }),
860 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
861 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
862 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
863 }),
864 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
865 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
866 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
867 }),
868 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
869 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
870 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
871 }),
872 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
873 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
874 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
875 }),
876 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
877 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
878 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
879 }),
880 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
881 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
882 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
883 }),
884 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
885 pack.Tag{1, pack.VarintType}, pack.Bool(true),
886 pack.Tag{2, pack.VarintType}, pack.Bool(false),
887 }),
888 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
889 pack.Tag{1, pack.VarintType}, pack.Bool(false),
890 pack.Tag{2, pack.VarintType}, pack.Bool(true),
891 }),
892 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
893 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
894 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
895 }),
896 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
897 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
898 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
899 }),
900 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
901 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
902 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
903 }),
904 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
905 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
906 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
907 }),
908 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
909 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
910 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
911 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
912 }),
913 }),
914 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
915 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
916 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
917 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
918 }),
919 }),
920 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
921 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
922 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
923 }),
924 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
925 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
926 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
927 }),
928 }.Marshal(),
929 },
930 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700931 desc: "oneof (uint32)",
932 decodeTo: []proto.Message{
933 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
934 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
935 },
936 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800937 },
938 {
939 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700940 decodeTo: []proto.Message{
941 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700942 &testpb.TestAllTypes_NestedMessage{A: proto.Int32(1112)},
Damien Neil3b46ade2019-03-26 13:55:02 -0700943 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
944 &test3pb.TestAllTypes_NestedMessage{A: 1112},
945 }},
946 },
Damien Neilba23aa52018-12-07 14:38:17 -0800947 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
948 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
949 })}.Marshal(),
950 },
951 {
Damien Neilc37adef2019-04-01 13:49:56 -0700952 desc: "oneof (empty message)",
953 decodeTo: []proto.Message{
954 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
955 &testpb.TestAllTypes_NestedMessage{},
956 }},
957 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
958 &test3pb.TestAllTypes_NestedMessage{},
959 }},
960 },
961 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
962 },
963 {
Joe Tsai6c286742019-07-11 23:15:05 -0700964 desc: "oneof (merged message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700965 decodeTo: []proto.Message{
966 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
967 &testpb.TestAllTypes_NestedMessage{
Joe Tsai0f81b382019-07-10 23:14:31 -0700968 A: proto.Int32(1),
Damien Neil3b46ade2019-03-26 13:55:02 -0700969 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700970 OptionalInt32: proto.Int32(43),
Damien Neil3b46ade2019-03-26 13:55:02 -0700971 },
Damien Neilba23aa52018-12-07 14:38:17 -0800972 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700973 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
974 &test3pb.TestAllTypes_NestedMessage{
Joe Tsai6c286742019-07-11 23:15:05 -0700975 A: 1,
Damien Neil3b46ade2019-03-26 13:55:02 -0700976 Corecursive: &test3pb.TestAllTypes{
977 OptionalInt32: 43,
978 },
979 },
980 }}},
Damien Neilba23aa52018-12-07 14:38:17 -0800981 wire: pack.Message{
982 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
983 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
984 }),
985 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
986 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
987 pack.Tag{1, pack.VarintType}, pack.Varint(43),
988 }),
989 }),
990 }.Marshal(),
991 },
992 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700993 desc: "oneof (string)",
994 decodeTo: []proto.Message{
995 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
996 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
997 },
998 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800999 },
1000 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001001 desc: "oneof (bytes)",
1002 decodeTo: []proto.Message{
1003 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
1004 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
1005 },
1006 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001007 },
1008 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001009 desc: "oneof (bool)",
1010 decodeTo: []proto.Message{
1011 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
1012 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
1013 },
1014 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001015 },
1016 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001017 desc: "oneof (uint64)",
1018 decodeTo: []proto.Message{
1019 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
1020 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
1021 },
1022 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001023 },
1024 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001025 desc: "oneof (float)",
1026 decodeTo: []proto.Message{
1027 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
1028 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
1029 },
1030 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001031 },
1032 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001033 desc: "oneof (double)",
1034 decodeTo: []proto.Message{
1035 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
1036 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
1037 },
1038 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001039 },
1040 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001041 desc: "oneof (enum)",
1042 decodeTo: []proto.Message{
1043 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
1044 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
1045 },
1046 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001047 },
1048 {
Damien Neilc37adef2019-04-01 13:49:56 -07001049 desc: "oneof (zero)",
1050 decodeTo: []proto.Message{
1051 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}},
1052 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}},
1053 },
1054 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
1055 },
1056 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001057 desc: "oneof (overridden value)",
1058 decodeTo: []proto.Message{
1059 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
1060 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
1061 },
Damien Neilba23aa52018-12-07 14:38:17 -08001062 wire: pack.Message{
1063 pack.Tag{111, pack.VarintType}, pack.Varint(1),
1064 pack.Tag{116, pack.VarintType}, pack.Varint(2),
1065 }.Marshal(),
1066 },
1067 // TODO: More unknown field tests for ordering, repeated fields, etc.
1068 //
1069 // It is currently impossible to produce results that the v1 Equal
1070 // considers equivalent to those of the v1 decoder. Figure out if
1071 // that's a problem or not.
1072 {
1073 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -08001074 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001075 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001076 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001077 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1078 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001079 ), build(
1080 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001081 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001082 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1083 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001084 )},
1085 wire: pack.Message{
1086 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1087 }.Marshal(),
1088 },
1089 {
1090 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001091 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001092 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001093 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001094 pack.Tag{1, pack.BytesType}, pack.String("string"),
1095 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001096 ), build(
1097 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001098 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001099 pack.Tag{1, pack.BytesType}, pack.String("string"),
1100 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001101 )},
1102 wire: pack.Message{
1103 pack.Tag{1, pack.BytesType}, pack.String("string"),
1104 }.Marshal(),
1105 },
1106 {
1107 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001108 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001109 &testpb.TestAllTypes{
1110 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -07001111 }, &test3pb.TestAllTypes{
1112 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -08001113 },
1114 },
1115 wire: pack.Message{
1116 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
1117 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1118 pack.Tag{2, pack.BytesType}, pack.String("string"),
1119 }),
1120 }.Marshal(),
1121 },
Damien Neil96c229a2019-04-03 12:17:24 -07001122 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001123 desc: "required field in nil message unset",
1124 partial: true,
1125 decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
1126 },
1127 {
Damien Neil96c229a2019-04-03 12:17:24 -07001128 desc: "required field unset",
1129 partial: true,
1130 decodeTo: []proto.Message{&testpb.TestRequired{}},
1131 },
1132 {
1133 desc: "required field set",
1134 decodeTo: []proto.Message{&testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001135 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001136 }},
1137 wire: pack.Message{
1138 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1139 }.Marshal(),
1140 },
1141 {
1142 desc: "required field in optional message unset",
1143 partial: true,
1144 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1145 OptionalMessage: &testpb.TestRequired{},
1146 }},
1147 wire: pack.Message{
1148 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1149 }.Marshal(),
1150 },
1151 {
1152 desc: "required field in optional message set",
1153 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1154 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001155 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001156 },
1157 }},
1158 wire: pack.Message{
1159 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1160 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1161 }),
1162 }.Marshal(),
1163 },
Damien Neil4686e232019-04-05 13:31:40 -07001164 {
1165 desc: "required field in optional message set (split across multiple tags)",
1166 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1167 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001168 RequiredField: proto.Int32(1),
Damien Neil4686e232019-04-05 13:31:40 -07001169 },
1170 }},
1171 wire: pack.Message{
1172 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1173 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1174 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1175 }),
1176 }.Marshal(),
1177 },
Damien Neil96c229a2019-04-03 12:17:24 -07001178 {
1179 desc: "required field in repeated message unset",
1180 partial: true,
1181 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1182 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001183 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001184 {},
1185 },
1186 }},
1187 wire: pack.Message{
1188 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1189 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1190 }),
1191 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1192 }.Marshal(),
1193 },
1194 {
1195 desc: "required field in repeated message set",
1196 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1197 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001198 {RequiredField: proto.Int32(1)},
1199 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001200 },
1201 }},
1202 wire: pack.Message{
1203 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1204 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1205 }),
1206 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1207 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1208 }),
1209 }.Marshal(),
1210 },
1211 {
1212 desc: "required field in map message unset",
1213 partial: true,
1214 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1215 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001216 1: {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001217 2: {},
1218 },
1219 }},
1220 wire: pack.Message{
1221 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1222 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1223 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1224 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1225 }),
1226 }),
1227 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1228 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1229 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1230 }),
1231 }.Marshal(),
1232 },
1233 {
1234 desc: "required field in map message set",
1235 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1236 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001237 1: {RequiredField: proto.Int32(1)},
1238 2: {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001239 },
1240 }},
1241 wire: pack.Message{
1242 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1243 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1244 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1245 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1246 }),
1247 }),
1248 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1249 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1250 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1251 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1252 }),
1253 }),
1254 }.Marshal(),
1255 },
1256 {
1257 desc: "required field in optional group unset",
1258 partial: true,
1259 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1260 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1261 }},
1262 wire: pack.Message{
1263 pack.Tag{1, pack.StartGroupType},
1264 pack.Tag{1, pack.EndGroupType},
1265 }.Marshal(),
1266 },
1267 {
1268 desc: "required field in optional group set",
1269 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1270 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001271 A: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001272 },
1273 }},
1274 wire: pack.Message{
1275 pack.Tag{1, pack.StartGroupType},
1276 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1277 pack.Tag{1, pack.EndGroupType},
1278 }.Marshal(),
1279 },
1280 {
1281 desc: "required field in repeated group unset",
1282 partial: true,
1283 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1284 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001285 {A: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001286 {},
1287 },
1288 }},
1289 wire: pack.Message{
1290 pack.Tag{3, pack.StartGroupType},
1291 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1292 pack.Tag{3, pack.EndGroupType},
1293 pack.Tag{3, pack.StartGroupType},
1294 pack.Tag{3, pack.EndGroupType},
1295 }.Marshal(),
1296 },
1297 {
1298 desc: "required field in repeated group set",
1299 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1300 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001301 {A: proto.Int32(1)},
1302 {A: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001303 },
1304 }},
1305 wire: pack.Message{
1306 pack.Tag{3, pack.StartGroupType},
1307 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1308 pack.Tag{3, pack.EndGroupType},
1309 pack.Tag{3, pack.StartGroupType},
1310 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1311 pack.Tag{3, pack.EndGroupType},
1312 }.Marshal(),
1313 },
1314 {
Damien Neil5322bdb2019-04-09 15:57:05 -07001315 desc: "required field in oneof message unset",
1316 partial: true,
1317 decodeTo: []proto.Message{
1318 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1319 &testpb.TestRequired{},
1320 }},
1321 },
1322 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1323 },
1324 {
1325 desc: "required field in oneof message set",
1326 decodeTo: []proto.Message{
1327 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1328 &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001329 RequiredField: proto.Int32(1),
Damien Neil5322bdb2019-04-09 15:57:05 -07001330 },
1331 }},
1332 },
1333 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
1334 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1335 })}.Marshal(),
1336 },
1337 {
Joe Tsai09cef322019-07-11 22:13:49 -07001338 desc: "required field in extension message unset",
1339 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001340 decodeTo: []proto.Message{build(
1341 &testpb.TestAllExtensions{},
1342 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1343 )},
1344 wire: pack.Message{
1345 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1346 }.Marshal(),
1347 },
1348 {
1349 desc: "required field in extension message set",
1350 decodeTo: []proto.Message{build(
1351 &testpb.TestAllExtensions{},
1352 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001353 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001354 }),
1355 )},
1356 wire: pack.Message{
1357 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1358 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1359 }),
1360 }.Marshal(),
1361 },
1362 {
Joe Tsai09cef322019-07-11 22:13:49 -07001363 desc: "required field in repeated extension message unset",
1364 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001365 decodeTo: []proto.Message{build(
1366 &testpb.TestAllExtensions{},
1367 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001368 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001369 {},
1370 }),
1371 )},
1372 wire: pack.Message{
1373 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1374 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1375 }),
1376 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1377 }.Marshal(),
1378 },
1379 {
1380 desc: "required field in repeated extension message set",
1381 decodeTo: []proto.Message{build(
1382 &testpb.TestAllExtensions{},
1383 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001384 {RequiredField: proto.Int32(1)},
1385 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001386 }),
1387 )},
1388 wire: pack.Message{
1389 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1390 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1391 }),
1392 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1393 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1394 }),
1395 }.Marshal(),
1396 },
Damien Neilc37adef2019-04-01 13:49:56 -07001397 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001398 desc: "nil messages",
1399 decodeTo: []proto.Message{
1400 (*testpb.TestAllTypes)(nil),
1401 (*test3pb.TestAllTypes)(nil),
1402 (*testpb.TestAllExtensions)(nil),
1403 },
1404 },
1405 {
Damien Neilc37adef2019-04-01 13:49:56 -07001406 desc: "legacy",
1407 partial: true,
1408 decodeTo: []proto.Message{
1409 &legacypb.Legacy{
1410 F1: &legacy1pb.Message{
Damien Neila8a2cea2019-07-10 16:17:16 -07001411 OptionalInt32: proto.Int32(1),
Damien Neilc37adef2019-04-01 13:49:56 -07001412 OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
1413 OptionalChildMessage: &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001414 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001415 },
1416 Optionalgroup: &legacy1pb.Message_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001417 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001418 },
1419 RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001420 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001421 },
1422 Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001423 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001424 },
1425 MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001426 true: {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001427 },
1428 OneofUnion: &legacy1pb.Message_OneofChildMessage{
1429 &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001430 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001431 },
1432 },
1433 },
1434 },
1435 },
1436 wire: pack.Message{
1437 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1438 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1439 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1440 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1441 pack.Tag{1, pack.BytesType}, pack.String("x"),
1442 }),
1443 pack.Tag{120, pack.StartGroupType},
1444 pack.Tag{1, pack.BytesType}, pack.String("x"),
1445 pack.Tag{120, pack.EndGroupType},
1446 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1447 pack.Tag{1, pack.BytesType}, pack.String("x"),
1448 }),
1449 pack.Tag{520, pack.StartGroupType},
1450 pack.Tag{1, pack.BytesType}, pack.String("x"),
1451 pack.Tag{520, pack.EndGroupType},
1452 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1453 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1454 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1455 pack.Tag{1, pack.BytesType}, pack.String("x"),
1456 }),
1457 }),
1458 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1459 pack.Tag{1, pack.BytesType}, pack.String("x"),
1460 }),
1461 }),
1462 }.Marshal(),
1463 },
Damien Neilba23aa52018-12-07 14:38:17 -08001464}
1465
Damien Neilbc310b52019-04-11 11:46:55 -07001466var invalidUTF8TestProtos = []testProto{
1467 {
1468 desc: "invalid UTF-8 in optional string field",
1469 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1470 OptionalString: "abc\xff",
1471 }},
1472 wire: pack.Message{
1473 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1474 }.Marshal(),
1475 },
1476 {
1477 desc: "invalid UTF-8 in repeated string field",
1478 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1479 RepeatedString: []string{"foo", "abc\xff"},
1480 }},
1481 wire: pack.Message{
1482 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1483 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1484 }.Marshal(),
1485 },
1486 {
1487 desc: "invalid UTF-8 in nested message",
1488 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1489 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1490 Corecursive: &test3pb.TestAllTypes{
1491 OptionalString: "abc\xff",
1492 },
1493 },
1494 }},
1495 wire: pack.Message{
1496 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1497 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1498 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1499 }),
1500 }),
1501 }.Marshal(),
1502 },
1503 {
Damien Neilc37adef2019-04-01 13:49:56 -07001504 desc: "invalid UTF-8 in oneof field",
1505 decodeTo: []proto.Message{
1506 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
1507 },
1508 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1509 },
1510 {
Damien Neilbc310b52019-04-11 11:46:55 -07001511 desc: "invalid UTF-8 in map key",
1512 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1513 MapStringString: map[string]string{"key\xff": "val"},
1514 }},
1515 wire: pack.Message{
1516 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1517 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1518 pack.Tag{2, pack.BytesType}, pack.String("val"),
1519 }),
1520 }.Marshal(),
1521 },
1522 {
1523 desc: "invalid UTF-8 in map value",
1524 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1525 MapStringString: map[string]string{"key": "val\xff"},
1526 }},
1527 wire: pack.Message{
1528 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1529 pack.Tag{1, pack.BytesType}, pack.String("key"),
1530 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1531 }),
1532 }.Marshal(),
1533 },
1534}
1535
Joe Tsaic51e2e02019-07-13 00:44:41 -07001536var noEnforceUTF8TestProtos = []testProto{
1537 {
1538 desc: "invalid UTF-8 in optional string field",
1539 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1540 OptionalString: string("abc\xff"),
1541 }},
1542 wire: pack.Message{
1543 pack.Tag{1, pack.BytesType}, pack.String("abc\xff"),
1544 }.Marshal(),
1545 },
1546 {
1547 desc: "invalid UTF-8 in optional string field of Go bytes",
1548 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1549 OptionalBytes: []byte("abc\xff"),
1550 }},
1551 wire: pack.Message{
1552 pack.Tag{2, pack.BytesType}, pack.String("abc\xff"),
1553 }.Marshal(),
1554 },
1555 {
1556 desc: "invalid UTF-8 in repeated string field",
1557 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1558 RepeatedString: []string{string("foo"), string("abc\xff")},
1559 }},
1560 wire: pack.Message{
1561 pack.Tag{3, pack.BytesType}, pack.String("foo"),
1562 pack.Tag{3, pack.BytesType}, pack.String("abc\xff"),
1563 }.Marshal(),
1564 },
1565 {
1566 desc: "invalid UTF-8 in repeated string field of Go bytes",
1567 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1568 RepeatedBytes: [][]byte{[]byte("foo"), []byte("abc\xff")},
1569 }},
1570 wire: pack.Message{
1571 pack.Tag{4, pack.BytesType}, pack.String("foo"),
1572 pack.Tag{4, pack.BytesType}, pack.String("abc\xff"),
1573 }.Marshal(),
1574 },
1575 {
1576 desc: "invalid UTF-8 in oneof string field",
1577 decodeTo: []proto.Message{
1578 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofString{string("abc\xff")}},
1579 },
1580 wire: pack.Message{pack.Tag{5, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1581 },
1582 {
1583 desc: "invalid UTF-8 in oneof string field of Go bytes",
1584 decodeTo: []proto.Message{
1585 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofBytes{[]byte("abc\xff")}},
1586 },
1587 wire: pack.Message{pack.Tag{6, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1588 },
1589}
1590
1591type TestNoEnforceUTF8 struct {
1592 OptionalString string `protobuf:"bytes,1,opt,name=optional_string"`
1593 OptionalBytes []byte `protobuf:"bytes,2,opt,name=optional_bytes"`
1594 RepeatedString []string `protobuf:"bytes,3,rep,name=repeated_string"`
1595 RepeatedBytes [][]byte `protobuf:"bytes,4,rep,name=repeated_bytes"`
1596 OneofField isOneofField `protobuf_oneof:"oneof_field"`
1597}
1598
1599type isOneofField interface{ isOneofField() }
1600
1601type TestNoEnforceUTF8_OneofString struct {
1602 OneofString string `protobuf:"bytes,5,opt,name=oneof_string,oneof"`
1603}
1604type TestNoEnforceUTF8_OneofBytes struct {
1605 OneofBytes []byte `protobuf:"bytes,6,opt,name=oneof_bytes,oneof"`
1606}
1607
1608func (*TestNoEnforceUTF8_OneofString) isOneofField() {}
1609func (*TestNoEnforceUTF8_OneofBytes) isOneofField() {}
1610
Joe Tsai74615a32019-07-14 18:51:46 -07001611func (m *TestNoEnforceUTF8) ProtoReflect() protoreflect.Message {
Joe Tsaic51e2e02019-07-13 00:44:41 -07001612 return messageInfo_TestNoEnforceUTF8.MessageOf(m)
1613}
1614
1615var messageInfo_TestNoEnforceUTF8 = protoimpl.MessageInfo{
Damien Neil16163b42019-08-06 15:43:25 -07001616 GoReflectType: reflect.TypeOf((*TestNoEnforceUTF8)(nil)),
1617 Desc: func() protoreflect.MessageDescriptor {
1618 pb := new(descriptorpb.FileDescriptorProto)
1619 if err := prototext.Unmarshal([]byte(`
Joe Tsaic51e2e02019-07-13 00:44:41 -07001620 syntax: "proto3"
1621 name: "test.proto"
1622 message_type: [{
1623 name: "TestNoEnforceUTF8"
1624 field: [
1625 {name:"optional_string" number:1 label:LABEL_OPTIONAL type:TYPE_STRING},
1626 {name:"optional_bytes" number:2 label:LABEL_OPTIONAL type:TYPE_STRING},
1627 {name:"repeated_string" number:3 label:LABEL_REPEATED type:TYPE_STRING},
1628 {name:"repeated_bytes" number:4 label:LABEL_REPEATED type:TYPE_STRING},
1629 {name:"oneof_string" number:5 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0},
1630 {name:"oneof_bytes" number:6 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0}
1631 ]
1632 oneof_decl: [{name:"oneof_field"}]
1633 }]
1634 `), pb); err != nil {
Damien Neil16163b42019-08-06 15:43:25 -07001635 panic(err)
1636 }
1637 fd, err := protodesc.NewFile(pb, nil)
1638 if err != nil {
1639 panic(err)
1640 }
1641 md := fd.Messages().Get(0)
1642 for i := 0; i < md.Fields().Len(); i++ {
1643 md.Fields().Get(i).(*filedesc.Field).L1.HasEnforceUTF8 = true
1644 md.Fields().Get(i).(*filedesc.Field).L1.EnforceUTF8 = false
1645 }
1646 return md
1647 }(),
Joe Tsaic51e2e02019-07-13 00:44:41 -07001648 OneofWrappers: []interface{}{
1649 (*TestNoEnforceUTF8_OneofString)(nil),
1650 (*TestNoEnforceUTF8_OneofBytes)(nil),
1651 },
1652}
1653
Damien Neil4be2fb42018-12-17 11:16:16 -08001654func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001655 for _, opt := range opts {
1656 opt(m)
1657 }
1658 return m
1659}
1660
Damien Neil4be2fb42018-12-17 11:16:16 -08001661type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001662
Joe Tsai74615a32019-07-14 18:51:46 -07001663func unknown(raw protoreflect.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001664 return func(m proto.Message) {
Joe Tsai378c1322019-04-25 23:48:08 -07001665 m.ProtoReflect().SetUnknown(raw)
Damien Neile6f060f2019-04-23 17:11:02 -07001666 }
1667}
1668
Joe Tsai74615a32019-07-14 18:51:46 -07001669func extend(desc *protoiface.ExtensionDescV1, value interface{}) buildOpt {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001670 // TODO: Should ExtensionType.ValueOf accept []T instead of *[]T?
1671 t := reflect.TypeOf(value)
1672 if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
1673 v := reflect.New(t)
1674 v.Elem().Set(reflect.ValueOf(value))
1675 value = v.Interface()
1676 }
Damien Neil4be2fb42018-12-17 11:16:16 -08001677 return func(m proto.Message) {
Damien Neil92f76182019-08-02 16:58:08 -07001678 proto.SetExtension(m, desc, value)
Damien Neilba23aa52018-12-07 14:38:17 -08001679 }
1680}
Damien Neil61e93c72019-03-27 09:23:20 -07001681
1682func marshalText(m proto.Message) string {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001683 b, _ := prototext.MarshalOptions{Indent: "\t", AllowPartial: true}.Marshal(m)
Damien Neil61e93c72019-03-27 09:23:20 -07001684 return string(b)
1685}