blob: e296fc22ef6ca4ae7766e3c3bf0f9f085628d1dd [file] [log] [blame]
Damien Neilba23aa52018-12-07 14:38:17 -08001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style.
3// license that can be found in the LICENSE file.
4
Damien Neil4be2fb42018-12-17 11:16:16 -08005package proto_test
Damien Neilba23aa52018-12-07 14:38:17 -08006
7import (
8 "fmt"
9 "reflect"
10 "testing"
11
Damien Neil5c5b5312019-05-14 12:44:37 -070012 "google.golang.org/protobuf/encoding/prototext"
Damien Neile89e6242019-05-13 23:55:40 -070013 "google.golang.org/protobuf/internal/encoding/pack"
Joe Tsaic51e2e02019-07-13 00:44:41 -070014 "google.golang.org/protobuf/internal/filedesc"
15 "google.golang.org/protobuf/internal/flags"
Damien Neile89e6242019-05-13 23:55:40 -070016 "google.golang.org/protobuf/proto"
Joe Tsaic51e2e02019-07-13 00:44:41 -070017 "google.golang.org/protobuf/reflect/protodesc"
18 "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsaic51e2e02019-07-13 00:44:41 -070019 "google.golang.org/protobuf/runtime/protoimpl"
Joe Tsai19058432019-02-27 21:46:29 -080020
Damien Neilc37adef2019-04-01 13:49:56 -070021 legacypb "google.golang.org/protobuf/internal/testprotos/legacy"
22 legacy1pb "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v0.0.0-20160225-2fc053c5"
Damien Neile89e6242019-05-13 23:55:40 -070023 testpb "google.golang.org/protobuf/internal/testprotos/test"
24 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Joe Tsaic51e2e02019-07-13 00:44:41 -070025 "google.golang.org/protobuf/types/descriptorpb"
Damien Neilba23aa52018-12-07 14:38:17 -080026)
27
28type testProto struct {
Joe Tsai09cef322019-07-11 22:13:49 -070029 desc string
30 decodeTo []proto.Message
31 wire []byte
32 partial bool
Damien Neilba23aa52018-12-07 14:38:17 -080033}
34
35func TestDecode(t *testing.T) {
36 for _, test := range testProtos {
37 for _, want := range test.decodeTo {
38 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070039 opts := proto.UnmarshalOptions{
40 AllowPartial: test.partial,
41 }
Damien Neilba23aa52018-12-07 14:38:17 -080042 wire := append(([]byte)(nil), test.wire...)
Damien Neil4be2fb42018-12-17 11:16:16 -080043 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
Damien Neil96c229a2019-04-03 12:17:24 -070044 if err := opts.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070045 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080046 return
47 }
48
49 // Aliasing check: Modifying the original wire bytes shouldn't
50 // affect the unmarshaled message.
51 for i := range wire {
52 wire[i] = 0
53 }
Joe Tsaidb38ddd2019-05-07 15:14:40 -070054 if !proto.Equal(got, want) {
Damien Neil61e93c72019-03-27 09:23:20 -070055 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080056 }
57 })
58 }
59 }
60}
61
Damien Neil96c229a2019-04-03 12:17:24 -070062func TestDecodeRequiredFieldChecks(t *testing.T) {
63 for _, test := range testProtos {
64 if !test.partial {
65 continue
66 }
Damien Neil96c229a2019-04-03 12:17:24 -070067 for _, m := range test.decodeTo {
68 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
69 got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
70 if err := proto.Unmarshal(test.wire, got); err == nil {
71 t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got))
72 }
73 })
74 }
75 }
76}
77
Damien Neilbc310b52019-04-11 11:46:55 -070078func TestDecodeInvalidUTF8(t *testing.T) {
79 for _, test := range invalidUTF8TestProtos {
80 for _, want := range test.decodeTo {
81 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
82 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
83 err := proto.Unmarshal(test.wire, got)
Damien Neil8c86fc52019-06-19 09:28:29 -070084 if err == nil {
Damien Neilbc310b52019-04-11 11:46:55 -070085 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
86 }
Damien Neilbc310b52019-04-11 11:46:55 -070087 })
88 }
89 }
90}
91
Joe Tsaic51e2e02019-07-13 00:44:41 -070092func TestDecodeNoEnforceUTF8(t *testing.T) {
93 for _, test := range noEnforceUTF8TestProtos {
94 for _, want := range test.decodeTo {
95 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
96 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
97 err := proto.Unmarshal(test.wire, got)
98 switch {
Joe Tsai1799d112019-08-08 13:31:59 -070099 case flags.ProtoLegacy && err != nil:
Joe Tsaic51e2e02019-07-13 00:44:41 -0700100 t.Errorf("Unmarshal returned unexpected error: %v\nMessage:\n%v", err, marshalText(want))
Joe Tsai1799d112019-08-08 13:31:59 -0700101 case !flags.ProtoLegacy && err == nil:
Joe Tsaic51e2e02019-07-13 00:44:41 -0700102 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
103 }
104 })
105 }
106 }
107}
108
Damien Neil8003f082019-08-02 15:13:00 -0700109func TestDecodeZeroLengthBytes(t *testing.T) {
110 // Verify that proto3 bytes fields don't give the mistaken
111 // impression that they preserve presence.
112 wire := pack.Message{
113 pack.Tag{15, pack.BytesType}, pack.Bytes(nil),
114 }.Marshal()
115 m := &test3pb.TestAllTypes{}
116 if err := proto.Unmarshal(wire, m); err != nil {
117 t.Fatal(err)
118 }
119 if m.OptionalBytes != nil {
120 t.Errorf("unmarshal zero-length proto3 bytes field: got %v, want nil", m.OptionalBytes)
121 }
122}
123
Joe Tsai9b22b932019-08-08 19:23:32 -0700124func TestDecodeOneofNilWrapper(t *testing.T) {
125 wire := pack.Message{
126 pack.Tag{111, pack.VarintType}, pack.Varint(1111),
127 }.Marshal()
128 m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)}
129 if err := proto.Unmarshal(wire, m); err != nil {
130 t.Fatal(err)
131 }
132 if got := m.GetOneofUint32(); got != 1111 {
133 t.Errorf("GetOneofUint32() = %v, want %v", got, 1111)
134 }
135}
136
Damien Neilba23aa52018-12-07 14:38:17 -0800137var testProtos = []testProto{
138 {
139 desc: "basic scalar types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800140 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700141 OptionalInt32: proto.Int32(1001),
142 OptionalInt64: proto.Int64(1002),
143 OptionalUint32: proto.Uint32(1003),
144 OptionalUint64: proto.Uint64(1004),
145 OptionalSint32: proto.Int32(1005),
146 OptionalSint64: proto.Int64(1006),
147 OptionalFixed32: proto.Uint32(1007),
148 OptionalFixed64: proto.Uint64(1008),
149 OptionalSfixed32: proto.Int32(1009),
150 OptionalSfixed64: proto.Int64(1010),
151 OptionalFloat: proto.Float32(1011.5),
152 OptionalDouble: proto.Float64(1012.5),
153 OptionalBool: proto.Bool(true),
154 OptionalString: proto.String("string"),
Damien Neilba23aa52018-12-07 14:38:17 -0800155 OptionalBytes: []byte("bytes"),
156 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
Damien Neil3b46ade2019-03-26 13:55:02 -0700157 }, &test3pb.TestAllTypes{
158 OptionalInt32: 1001,
159 OptionalInt64: 1002,
160 OptionalUint32: 1003,
161 OptionalUint64: 1004,
162 OptionalSint32: 1005,
163 OptionalSint64: 1006,
164 OptionalFixed32: 1007,
165 OptionalFixed64: 1008,
166 OptionalSfixed32: 1009,
167 OptionalSfixed64: 1010,
168 OptionalFloat: 1011.5,
169 OptionalDouble: 1012.5,
170 OptionalBool: true,
171 OptionalString: "string",
172 OptionalBytes: []byte("bytes"),
173 OptionalNestedEnum: test3pb.TestAllTypes_BAR,
Damien Neilba23aa52018-12-07 14:38:17 -0800174 }, build(
175 &testpb.TestAllExtensions{},
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700176 extend(testpb.E_OptionalInt32Extension, int32(1001)),
177 extend(testpb.E_OptionalInt64Extension, int64(1002)),
178 extend(testpb.E_OptionalUint32Extension, uint32(1003)),
179 extend(testpb.E_OptionalUint64Extension, uint64(1004)),
180 extend(testpb.E_OptionalSint32Extension, int32(1005)),
181 extend(testpb.E_OptionalSint64Extension, int64(1006)),
182 extend(testpb.E_OptionalFixed32Extension, uint32(1007)),
183 extend(testpb.E_OptionalFixed64Extension, uint64(1008)),
184 extend(testpb.E_OptionalSfixed32Extension, int32(1009)),
185 extend(testpb.E_OptionalSfixed64Extension, int64(1010)),
186 extend(testpb.E_OptionalFloatExtension, float32(1011.5)),
187 extend(testpb.E_OptionalDoubleExtension, float64(1012.5)),
188 extend(testpb.E_OptionalBoolExtension, bool(true)),
189 extend(testpb.E_OptionalStringExtension, string("string")),
Damien Neilba23aa52018-12-07 14:38:17 -0800190 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700191 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR),
Damien Neilba23aa52018-12-07 14:38:17 -0800192 )},
193 wire: pack.Message{
194 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
195 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
196 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
197 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
198 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
199 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
200 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
201 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
202 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
203 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
204 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
205 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
206 pack.Tag{13, pack.VarintType}, pack.Bool(true),
207 pack.Tag{14, pack.BytesType}, pack.String("string"),
208 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
209 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
210 }.Marshal(),
211 },
212 {
Damien Neil8003f082019-08-02 15:13:00 -0700213 desc: "zero values",
214 decodeTo: []proto.Message{&testpb.TestAllTypes{
215 OptionalInt32: proto.Int32(0),
216 OptionalInt64: proto.Int64(0),
217 OptionalUint32: proto.Uint32(0),
218 OptionalUint64: proto.Uint64(0),
219 OptionalSint32: proto.Int32(0),
220 OptionalSint64: proto.Int64(0),
221 OptionalFixed32: proto.Uint32(0),
222 OptionalFixed64: proto.Uint64(0),
223 OptionalSfixed32: proto.Int32(0),
224 OptionalSfixed64: proto.Int64(0),
225 OptionalFloat: proto.Float32(0),
226 OptionalDouble: proto.Float64(0),
227 OptionalBool: proto.Bool(false),
228 OptionalString: proto.String(""),
229 OptionalBytes: []byte{},
230 }, &test3pb.TestAllTypes{}, build(
231 &testpb.TestAllExtensions{},
232 extend(testpb.E_OptionalInt32Extension, int32(0)),
233 extend(testpb.E_OptionalInt64Extension, int64(0)),
234 extend(testpb.E_OptionalUint32Extension, uint32(0)),
235 extend(testpb.E_OptionalUint64Extension, uint64(0)),
236 extend(testpb.E_OptionalSint32Extension, int32(0)),
237 extend(testpb.E_OptionalSint64Extension, int64(0)),
238 extend(testpb.E_OptionalFixed32Extension, uint32(0)),
239 extend(testpb.E_OptionalFixed64Extension, uint64(0)),
240 extend(testpb.E_OptionalSfixed32Extension, int32(0)),
241 extend(testpb.E_OptionalSfixed64Extension, int64(0)),
242 extend(testpb.E_OptionalFloatExtension, float32(0)),
243 extend(testpb.E_OptionalDoubleExtension, float64(0)),
244 extend(testpb.E_OptionalBoolExtension, bool(false)),
245 extend(testpb.E_OptionalStringExtension, string("")),
246 extend(testpb.E_OptionalBytesExtension, []byte{}),
247 )},
248 wire: pack.Message{
249 pack.Tag{1, pack.VarintType}, pack.Varint(0),
250 pack.Tag{2, pack.VarintType}, pack.Varint(0),
251 pack.Tag{3, pack.VarintType}, pack.Uvarint(0),
252 pack.Tag{4, pack.VarintType}, pack.Uvarint(0),
253 pack.Tag{5, pack.VarintType}, pack.Svarint(0),
254 pack.Tag{6, pack.VarintType}, pack.Svarint(0),
255 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(0),
256 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(0),
257 pack.Tag{9, pack.Fixed32Type}, pack.Int32(0),
258 pack.Tag{10, pack.Fixed64Type}, pack.Int64(0),
259 pack.Tag{11, pack.Fixed32Type}, pack.Float32(0),
260 pack.Tag{12, pack.Fixed64Type}, pack.Float64(0),
261 pack.Tag{13, pack.VarintType}, pack.Bool(false),
262 pack.Tag{14, pack.BytesType}, pack.String(""),
263 pack.Tag{15, pack.BytesType}, pack.Bytes(nil),
264 }.Marshal(),
265 },
266 {
Damien Neilba23aa52018-12-07 14:38:17 -0800267 desc: "groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800268 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800269 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700270 A: proto.Int32(1017),
Damien Neilba23aa52018-12-07 14:38:17 -0800271 },
272 }, build(
273 &testpb.TestAllExtensions{},
274 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700275 A: proto.Int32(1017),
Damien Neilba23aa52018-12-07 14:38:17 -0800276 }),
277 )},
278 wire: pack.Message{
279 pack.Tag{16, pack.StartGroupType},
280 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
281 pack.Tag{16, pack.EndGroupType},
282 }.Marshal(),
283 },
284 {
285 desc: "groups (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800286 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800287 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700288 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800289 },
290 }, build(
291 &testpb.TestAllExtensions{},
292 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700293 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800294 }),
295 )},
296 wire: pack.Message{
297 pack.Tag{16, pack.StartGroupType},
298 pack.Tag{17, pack.VarintType}, pack.Varint(1),
299 pack.Tag{16, pack.EndGroupType},
300 pack.Tag{16, pack.StartGroupType},
301 pack.Tag{17, pack.VarintType}, pack.Varint(2),
302 pack.Tag{16, pack.EndGroupType},
303 }.Marshal(),
304 },
305 {
306 desc: "messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800307 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800308 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700309 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800310 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700311 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800312 },
313 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700314 }, &test3pb.TestAllTypes{
315 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
316 A: 42,
317 Corecursive: &test3pb.TestAllTypes{
318 OptionalInt32: 43,
319 },
320 },
Damien Neilba23aa52018-12-07 14:38:17 -0800321 }, build(
322 &testpb.TestAllExtensions{},
323 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700324 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800325 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700326 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800327 },
328 }),
329 )},
330 wire: pack.Message{
331 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
332 pack.Tag{1, pack.VarintType}, pack.Varint(42),
333 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
334 pack.Tag{1, pack.VarintType}, pack.Varint(43),
335 }),
336 }),
337 }.Marshal(),
338 },
339 {
340 desc: "messages (split across multiple tags)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800341 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800342 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700343 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800344 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700345 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800346 },
347 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700348 }, &test3pb.TestAllTypes{
349 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
350 A: 42,
351 Corecursive: &test3pb.TestAllTypes{
352 OptionalInt32: 43,
353 },
354 },
Damien Neilba23aa52018-12-07 14:38:17 -0800355 }, build(
356 &testpb.TestAllExtensions{},
357 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700358 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800359 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700360 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800361 },
362 }),
363 )},
364 wire: pack.Message{
365 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
366 pack.Tag{1, pack.VarintType}, pack.Varint(42),
367 }),
368 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
369 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
370 pack.Tag{1, pack.VarintType}, pack.Varint(43),
371 }),
372 }),
373 }.Marshal(),
374 },
375 {
376 desc: "messages (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800377 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800378 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700379 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800380 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700381 }, &test3pb.TestAllTypes{
382 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
383 A: 2,
384 },
Damien Neilba23aa52018-12-07 14:38:17 -0800385 }, build(
386 &testpb.TestAllExtensions{},
387 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700388 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800389 }),
390 )},
391 wire: pack.Message{
392 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
393 pack.Tag{1, pack.VarintType}, pack.Varint(1),
394 }),
395 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
396 pack.Tag{1, pack.VarintType}, pack.Varint(2),
397 }),
398 }.Marshal(),
399 },
400 {
401 desc: "basic repeated types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800402 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800403 RepeatedInt32: []int32{1001, 2001},
404 RepeatedInt64: []int64{1002, 2002},
405 RepeatedUint32: []uint32{1003, 2003},
406 RepeatedUint64: []uint64{1004, 2004},
407 RepeatedSint32: []int32{1005, 2005},
408 RepeatedSint64: []int64{1006, 2006},
409 RepeatedFixed32: []uint32{1007, 2007},
410 RepeatedFixed64: []uint64{1008, 2008},
411 RepeatedSfixed32: []int32{1009, 2009},
412 RepeatedSfixed64: []int64{1010, 2010},
413 RepeatedFloat: []float32{1011.5, 2011.5},
414 RepeatedDouble: []float64{1012.5, 2012.5},
415 RepeatedBool: []bool{true, false},
416 RepeatedString: []string{"foo", "bar"},
417 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
418 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
419 testpb.TestAllTypes_FOO,
420 testpb.TestAllTypes_BAR,
421 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700422 }, &test3pb.TestAllTypes{
423 RepeatedInt32: []int32{1001, 2001},
424 RepeatedInt64: []int64{1002, 2002},
425 RepeatedUint32: []uint32{1003, 2003},
426 RepeatedUint64: []uint64{1004, 2004},
427 RepeatedSint32: []int32{1005, 2005},
428 RepeatedSint64: []int64{1006, 2006},
429 RepeatedFixed32: []uint32{1007, 2007},
430 RepeatedFixed64: []uint64{1008, 2008},
431 RepeatedSfixed32: []int32{1009, 2009},
432 RepeatedSfixed64: []int64{1010, 2010},
433 RepeatedFloat: []float32{1011.5, 2011.5},
434 RepeatedDouble: []float64{1012.5, 2012.5},
435 RepeatedBool: []bool{true, false},
436 RepeatedString: []string{"foo", "bar"},
437 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
438 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
439 test3pb.TestAllTypes_FOO,
440 test3pb.TestAllTypes_BAR,
441 },
Damien Neilba23aa52018-12-07 14:38:17 -0800442 }, build(
443 &testpb.TestAllExtensions{},
444 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
445 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
446 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
447 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
448 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
449 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
450 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
451 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
452 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
453 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
454 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
455 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
456 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
457 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
458 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
459 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
460 testpb.TestAllTypes_FOO,
461 testpb.TestAllTypes_BAR,
462 }),
463 )},
464 wire: pack.Message{
465 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
466 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
467 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
468 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
469 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
470 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
471 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
472 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
473 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
474 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
475 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
476 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
477 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
478 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
479 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
480 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
481 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
482 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
483 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
484 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
485 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
486 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
487 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
488 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
489 pack.Tag{43, pack.VarintType}, pack.Bool(true),
490 pack.Tag{43, pack.VarintType}, pack.Bool(false),
491 pack.Tag{44, pack.BytesType}, pack.String("foo"),
492 pack.Tag{44, pack.BytesType}, pack.String("bar"),
493 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
494 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
495 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
496 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
497 }.Marshal(),
498 },
499 {
500 desc: "basic repeated types (packed encoding)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800501 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800502 RepeatedInt32: []int32{1001, 2001},
503 RepeatedInt64: []int64{1002, 2002},
504 RepeatedUint32: []uint32{1003, 2003},
505 RepeatedUint64: []uint64{1004, 2004},
506 RepeatedSint32: []int32{1005, 2005},
507 RepeatedSint64: []int64{1006, 2006},
508 RepeatedFixed32: []uint32{1007, 2007},
509 RepeatedFixed64: []uint64{1008, 2008},
510 RepeatedSfixed32: []int32{1009, 2009},
511 RepeatedSfixed64: []int64{1010, 2010},
512 RepeatedFloat: []float32{1011.5, 2011.5},
513 RepeatedDouble: []float64{1012.5, 2012.5},
514 RepeatedBool: []bool{true, false},
515 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
516 testpb.TestAllTypes_FOO,
517 testpb.TestAllTypes_BAR,
518 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700519 }, &test3pb.TestAllTypes{
520 RepeatedInt32: []int32{1001, 2001},
521 RepeatedInt64: []int64{1002, 2002},
522 RepeatedUint32: []uint32{1003, 2003},
523 RepeatedUint64: []uint64{1004, 2004},
524 RepeatedSint32: []int32{1005, 2005},
525 RepeatedSint64: []int64{1006, 2006},
526 RepeatedFixed32: []uint32{1007, 2007},
527 RepeatedFixed64: []uint64{1008, 2008},
528 RepeatedSfixed32: []int32{1009, 2009},
529 RepeatedSfixed64: []int64{1010, 2010},
530 RepeatedFloat: []float32{1011.5, 2011.5},
531 RepeatedDouble: []float64{1012.5, 2012.5},
532 RepeatedBool: []bool{true, false},
533 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
534 test3pb.TestAllTypes_FOO,
535 test3pb.TestAllTypes_BAR,
536 },
Damien Neilba23aa52018-12-07 14:38:17 -0800537 }, build(
538 &testpb.TestAllExtensions{},
539 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
540 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
541 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
542 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
543 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
544 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
545 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
546 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
547 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
548 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
549 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
550 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
551 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
552 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
553 testpb.TestAllTypes_FOO,
554 testpb.TestAllTypes_BAR,
555 }),
556 )},
557 wire: pack.Message{
558 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
559 pack.Varint(1001), pack.Varint(2001),
560 },
561 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
562 pack.Varint(1002), pack.Varint(2002),
563 },
564 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
565 pack.Uvarint(1003), pack.Uvarint(2003),
566 },
567 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
568 pack.Uvarint(1004), pack.Uvarint(2004),
569 },
570 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
571 pack.Svarint(1005), pack.Svarint(2005),
572 },
573 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
574 pack.Svarint(1006), pack.Svarint(2006),
575 },
576 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
577 pack.Uint32(1007), pack.Uint32(2007),
578 },
579 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
580 pack.Uint64(1008), pack.Uint64(2008),
581 },
582 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
583 pack.Int32(1009), pack.Int32(2009),
584 },
585 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
586 pack.Int64(1010), pack.Int64(2010),
587 },
588 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
589 pack.Float32(1011.5), pack.Float32(2011.5),
590 },
591 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
592 pack.Float64(1012.5), pack.Float64(2012.5),
593 },
594 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
595 pack.Bool(true), pack.Bool(false),
596 },
597 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
598 pack.Varint(int(testpb.TestAllTypes_FOO)),
599 pack.Varint(int(testpb.TestAllTypes_BAR)),
600 },
601 }.Marshal(),
602 },
603 {
Damien Neil7492a092019-07-10 15:23:29 -0700604 desc: "packed repeated types",
605 decodeTo: []proto.Message{&testpb.TestPackedTypes{
606 PackedInt32: []int32{1001, 2001},
607 PackedInt64: []int64{1002, 2002},
608 PackedUint32: []uint32{1003, 2003},
609 PackedUint64: []uint64{1004, 2004},
610 PackedSint32: []int32{1005, 2005},
611 PackedSint64: []int64{1006, 2006},
612 PackedFixed32: []uint32{1007, 2007},
613 PackedFixed64: []uint64{1008, 2008},
614 PackedSfixed32: []int32{1009, 2009},
615 PackedSfixed64: []int64{1010, 2010},
616 PackedFloat: []float32{1011.5, 2011.5},
617 PackedDouble: []float64{1012.5, 2012.5},
618 PackedBool: []bool{true, false},
619 PackedEnum: []testpb.ForeignEnum{
620 testpb.ForeignEnum_FOREIGN_FOO,
621 testpb.ForeignEnum_FOREIGN_BAR,
622 },
623 }, build(
624 &testpb.TestPackedExtensions{},
625 extend(testpb.E_PackedInt32Extension, []int32{1001, 2001}),
626 extend(testpb.E_PackedInt64Extension, []int64{1002, 2002}),
627 extend(testpb.E_PackedUint32Extension, []uint32{1003, 2003}),
628 extend(testpb.E_PackedUint64Extension, []uint64{1004, 2004}),
629 extend(testpb.E_PackedSint32Extension, []int32{1005, 2005}),
630 extend(testpb.E_PackedSint64Extension, []int64{1006, 2006}),
631 extend(testpb.E_PackedFixed32Extension, []uint32{1007, 2007}),
632 extend(testpb.E_PackedFixed64Extension, []uint64{1008, 2008}),
633 extend(testpb.E_PackedSfixed32Extension, []int32{1009, 2009}),
634 extend(testpb.E_PackedSfixed64Extension, []int64{1010, 2010}),
635 extend(testpb.E_PackedFloatExtension, []float32{1011.5, 2011.5}),
636 extend(testpb.E_PackedDoubleExtension, []float64{1012.5, 2012.5}),
637 extend(testpb.E_PackedBoolExtension, []bool{true, false}),
638 extend(testpb.E_PackedEnumExtension, []testpb.ForeignEnum{
639 testpb.ForeignEnum_FOREIGN_FOO,
640 testpb.ForeignEnum_FOREIGN_BAR,
641 }),
642 )},
643 wire: pack.Message{
644 pack.Tag{90, pack.BytesType}, pack.LengthPrefix{
645 pack.Varint(1001), pack.Varint(2001),
646 },
647 pack.Tag{91, pack.BytesType}, pack.LengthPrefix{
648 pack.Varint(1002), pack.Varint(2002),
649 },
650 pack.Tag{92, pack.BytesType}, pack.LengthPrefix{
651 pack.Uvarint(1003), pack.Uvarint(2003),
652 },
653 pack.Tag{93, pack.BytesType}, pack.LengthPrefix{
654 pack.Uvarint(1004), pack.Uvarint(2004),
655 },
656 pack.Tag{94, pack.BytesType}, pack.LengthPrefix{
657 pack.Svarint(1005), pack.Svarint(2005),
658 },
659 pack.Tag{95, pack.BytesType}, pack.LengthPrefix{
660 pack.Svarint(1006), pack.Svarint(2006),
661 },
662 pack.Tag{96, pack.BytesType}, pack.LengthPrefix{
663 pack.Uint32(1007), pack.Uint32(2007),
664 },
665 pack.Tag{97, pack.BytesType}, pack.LengthPrefix{
666 pack.Uint64(1008), pack.Uint64(2008),
667 },
668 pack.Tag{98, pack.BytesType}, pack.LengthPrefix{
669 pack.Int32(1009), pack.Int32(2009),
670 },
671 pack.Tag{99, pack.BytesType}, pack.LengthPrefix{
672 pack.Int64(1010), pack.Int64(2010),
673 },
674 pack.Tag{100, pack.BytesType}, pack.LengthPrefix{
675 pack.Float32(1011.5), pack.Float32(2011.5),
676 },
677 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{
678 pack.Float64(1012.5), pack.Float64(2012.5),
679 },
680 pack.Tag{102, pack.BytesType}, pack.LengthPrefix{
681 pack.Bool(true), pack.Bool(false),
682 },
683 pack.Tag{103, pack.BytesType}, pack.LengthPrefix{
684 pack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
685 pack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
686 },
687 }.Marshal(),
688 },
689 {
Damien Neilba23aa52018-12-07 14:38:17 -0800690 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800691 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800692 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700693 {A: proto.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700694 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700695 {A: proto.Int32(2)},
Damien Neilba23aa52018-12-07 14:38:17 -0800696 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700697 }, &test3pb.TestAllTypes{
698 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
699 {A: 1},
Damien Neilc37adef2019-04-01 13:49:56 -0700700 nil,
Damien Neil3b46ade2019-03-26 13:55:02 -0700701 {A: 2},
702 },
Damien Neilba23aa52018-12-07 14:38:17 -0800703 }, build(
704 &testpb.TestAllExtensions{},
705 extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700706 {A: proto.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700707 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700708 {A: proto.Int32(2)},
Damien Neilba23aa52018-12-07 14:38:17 -0800709 }),
710 )},
711 wire: pack.Message{
712 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
713 pack.Tag{1, pack.VarintType}, pack.Varint(1),
714 }),
Damien Neilc37adef2019-04-01 13:49:56 -0700715 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
Damien Neilba23aa52018-12-07 14:38:17 -0800716 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
717 pack.Tag{1, pack.VarintType}, pack.Varint(2),
718 }),
719 }.Marshal(),
720 },
721 {
722 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800723 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800724 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700725 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700726 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700727 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800728 },
729 }, build(
730 &testpb.TestAllExtensions{},
731 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700732 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700733 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700734 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800735 }),
736 )},
737 wire: pack.Message{
738 pack.Tag{46, pack.StartGroupType},
739 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
740 pack.Tag{46, pack.EndGroupType},
741 pack.Tag{46, pack.StartGroupType},
Damien Neilc37adef2019-04-01 13:49:56 -0700742 pack.Tag{46, pack.EndGroupType},
743 pack.Tag{46, pack.StartGroupType},
Damien Neilba23aa52018-12-07 14:38:17 -0800744 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
745 pack.Tag{46, pack.EndGroupType},
746 }.Marshal(),
747 },
748 {
749 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800750 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800751 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
752 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
753 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
754 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
755 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
756 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
757 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
758 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
759 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
760 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
761 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
762 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
763 MapBoolBool: map[bool]bool{true: false, false: true},
764 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
765 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
766 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700767 "71.1.key": {A: proto.Int32(1171)},
768 "71.2.key": {A: proto.Int32(2171)},
Damien Neilba23aa52018-12-07 14:38:17 -0800769 },
770 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
771 "73.1.key": testpb.TestAllTypes_FOO,
772 "73.2.key": testpb.TestAllTypes_BAR,
773 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700774 }, &test3pb.TestAllTypes{
775 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
776 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
777 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
778 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
779 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
780 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
781 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
782 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
783 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
784 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
785 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
786 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
787 MapBoolBool: map[bool]bool{true: false, false: true},
788 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
789 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
790 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
791 "71.1.key": {A: 1171},
792 "71.2.key": {A: 2171},
793 },
794 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
795 "73.1.key": test3pb.TestAllTypes_FOO,
796 "73.2.key": test3pb.TestAllTypes_BAR,
797 },
Damien Neilba23aa52018-12-07 14:38:17 -0800798 }},
799 wire: pack.Message{
800 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
801 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
802 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
803 }),
804 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
805 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
806 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
807 }),
808 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
809 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
810 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
811 }),
812 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
813 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
814 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
815 }),
816 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
817 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
818 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
819 }),
820 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
821 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
822 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
823 }),
824 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
825 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
826 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
827 }),
828 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
829 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
830 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
831 }),
832 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
833 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
834 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
835 }),
836 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
837 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
838 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
839 }),
840 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
841 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
842 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
843 }),
844 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
845 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
846 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
847 }),
848 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
849 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
850 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
851 }),
852 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
853 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
854 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
855 }),
856 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
857 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
858 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
859 }),
860 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
861 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
862 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
863 }),
864 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
865 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
866 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
867 }),
868 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
869 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
870 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
871 }),
872 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
873 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
874 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
875 }),
876 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
877 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
878 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
879 }),
880 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
881 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
882 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
883 }),
884 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
885 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
886 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
887 }),
888 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
889 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
890 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
891 }),
892 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
893 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
894 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
895 }),
896 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
897 pack.Tag{1, pack.VarintType}, pack.Bool(true),
898 pack.Tag{2, pack.VarintType}, pack.Bool(false),
899 }),
900 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
901 pack.Tag{1, pack.VarintType}, pack.Bool(false),
902 pack.Tag{2, pack.VarintType}, pack.Bool(true),
903 }),
904 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
905 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
906 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
907 }),
908 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
909 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
910 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
911 }),
912 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
913 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
914 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
915 }),
916 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
917 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
918 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
919 }),
920 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
921 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
922 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
923 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
924 }),
925 }),
926 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
927 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
928 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
929 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
930 }),
931 }),
932 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
933 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
934 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
935 }),
936 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
937 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
938 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
939 }),
940 }.Marshal(),
941 },
942 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700943 desc: "oneof (uint32)",
944 decodeTo: []proto.Message{
945 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
946 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
947 },
948 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800949 },
950 {
951 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700952 decodeTo: []proto.Message{
953 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700954 &testpb.TestAllTypes_NestedMessage{A: proto.Int32(1112)},
Damien Neil3b46ade2019-03-26 13:55:02 -0700955 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
956 &test3pb.TestAllTypes_NestedMessage{A: 1112},
957 }},
958 },
Damien Neilba23aa52018-12-07 14:38:17 -0800959 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
960 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
961 })}.Marshal(),
962 },
963 {
Damien Neilc37adef2019-04-01 13:49:56 -0700964 desc: "oneof (empty message)",
965 decodeTo: []proto.Message{
966 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
967 &testpb.TestAllTypes_NestedMessage{},
968 }},
969 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
970 &test3pb.TestAllTypes_NestedMessage{},
971 }},
972 },
973 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
974 },
975 {
Joe Tsai6c286742019-07-11 23:15:05 -0700976 desc: "oneof (merged message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700977 decodeTo: []proto.Message{
978 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
979 &testpb.TestAllTypes_NestedMessage{
Joe Tsai0f81b382019-07-10 23:14:31 -0700980 A: proto.Int32(1),
Damien Neil3b46ade2019-03-26 13:55:02 -0700981 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700982 OptionalInt32: proto.Int32(43),
Damien Neil3b46ade2019-03-26 13:55:02 -0700983 },
Damien Neilba23aa52018-12-07 14:38:17 -0800984 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700985 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
986 &test3pb.TestAllTypes_NestedMessage{
Joe Tsai6c286742019-07-11 23:15:05 -0700987 A: 1,
Damien Neil3b46ade2019-03-26 13:55:02 -0700988 Corecursive: &test3pb.TestAllTypes{
989 OptionalInt32: 43,
990 },
991 },
992 }}},
Damien Neilba23aa52018-12-07 14:38:17 -0800993 wire: pack.Message{
994 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
995 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
996 }),
997 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
998 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
999 pack.Tag{1, pack.VarintType}, pack.Varint(43),
1000 }),
1001 }),
1002 }.Marshal(),
1003 },
1004 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001005 desc: "oneof (string)",
1006 decodeTo: []proto.Message{
1007 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
1008 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
1009 },
1010 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001011 },
1012 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001013 desc: "oneof (bytes)",
1014 decodeTo: []proto.Message{
1015 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
1016 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
1017 },
1018 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001019 },
1020 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001021 desc: "oneof (bool)",
1022 decodeTo: []proto.Message{
1023 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
1024 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
1025 },
1026 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001027 },
1028 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001029 desc: "oneof (uint64)",
1030 decodeTo: []proto.Message{
1031 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
1032 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
1033 },
1034 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001035 },
1036 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001037 desc: "oneof (float)",
1038 decodeTo: []proto.Message{
1039 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
1040 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
1041 },
1042 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001043 },
1044 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001045 desc: "oneof (double)",
1046 decodeTo: []proto.Message{
1047 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
1048 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
1049 },
1050 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001051 },
1052 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001053 desc: "oneof (enum)",
1054 decodeTo: []proto.Message{
1055 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
1056 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
1057 },
1058 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001059 },
1060 {
Damien Neilc37adef2019-04-01 13:49:56 -07001061 desc: "oneof (zero)",
1062 decodeTo: []proto.Message{
1063 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}},
1064 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}},
1065 },
1066 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
1067 },
1068 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001069 desc: "oneof (overridden value)",
1070 decodeTo: []proto.Message{
1071 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
1072 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
1073 },
Damien Neilba23aa52018-12-07 14:38:17 -08001074 wire: pack.Message{
1075 pack.Tag{111, pack.VarintType}, pack.Varint(1),
1076 pack.Tag{116, pack.VarintType}, pack.Varint(2),
1077 }.Marshal(),
1078 },
1079 // TODO: More unknown field tests for ordering, repeated fields, etc.
1080 //
1081 // It is currently impossible to produce results that the v1 Equal
1082 // considers equivalent to those of the v1 decoder. Figure out if
1083 // that's a problem or not.
1084 {
1085 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -08001086 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001087 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001088 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001089 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1090 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001091 ), build(
1092 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001093 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001094 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1095 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001096 )},
1097 wire: pack.Message{
1098 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1099 }.Marshal(),
1100 },
1101 {
1102 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001103 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001104 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001105 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001106 pack.Tag{1, pack.BytesType}, pack.String("string"),
1107 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001108 ), build(
1109 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001110 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001111 pack.Tag{1, pack.BytesType}, pack.String("string"),
1112 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001113 )},
1114 wire: pack.Message{
1115 pack.Tag{1, pack.BytesType}, pack.String("string"),
1116 }.Marshal(),
1117 },
1118 {
1119 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001120 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001121 &testpb.TestAllTypes{
1122 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -07001123 }, &test3pb.TestAllTypes{
1124 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -08001125 },
1126 },
1127 wire: pack.Message{
1128 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
1129 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1130 pack.Tag{2, pack.BytesType}, pack.String("string"),
1131 }),
1132 }.Marshal(),
1133 },
Damien Neil96c229a2019-04-03 12:17:24 -07001134 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001135 desc: "required field in nil message unset",
1136 partial: true,
1137 decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
1138 },
1139 {
Damien Neil96c229a2019-04-03 12:17:24 -07001140 desc: "required field unset",
1141 partial: true,
1142 decodeTo: []proto.Message{&testpb.TestRequired{}},
1143 },
1144 {
1145 desc: "required field set",
1146 decodeTo: []proto.Message{&testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001147 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001148 }},
1149 wire: pack.Message{
1150 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1151 }.Marshal(),
1152 },
1153 {
1154 desc: "required field in optional message unset",
1155 partial: true,
1156 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1157 OptionalMessage: &testpb.TestRequired{},
1158 }},
1159 wire: pack.Message{
1160 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1161 }.Marshal(),
1162 },
1163 {
1164 desc: "required field in optional message set",
1165 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1166 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001167 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001168 },
1169 }},
1170 wire: pack.Message{
1171 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1172 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1173 }),
1174 }.Marshal(),
1175 },
Damien Neil4686e232019-04-05 13:31:40 -07001176 {
1177 desc: "required field in optional message set (split across multiple tags)",
1178 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1179 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001180 RequiredField: proto.Int32(1),
Damien Neil4686e232019-04-05 13:31:40 -07001181 },
1182 }},
1183 wire: pack.Message{
1184 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1185 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1186 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1187 }),
1188 }.Marshal(),
1189 },
Damien Neil96c229a2019-04-03 12:17:24 -07001190 {
1191 desc: "required field in repeated message unset",
1192 partial: true,
1193 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1194 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001195 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001196 {},
1197 },
1198 }},
1199 wire: pack.Message{
1200 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1201 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1202 }),
1203 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1204 }.Marshal(),
1205 },
1206 {
1207 desc: "required field in repeated message set",
1208 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1209 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001210 {RequiredField: proto.Int32(1)},
1211 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001212 },
1213 }},
1214 wire: pack.Message{
1215 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1216 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1217 }),
1218 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1219 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1220 }),
1221 }.Marshal(),
1222 },
1223 {
1224 desc: "required field in map message unset",
1225 partial: true,
1226 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1227 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001228 1: {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001229 2: {},
1230 },
1231 }},
1232 wire: pack.Message{
1233 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1234 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1235 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1236 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1237 }),
1238 }),
1239 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1240 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1241 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1242 }),
1243 }.Marshal(),
1244 },
1245 {
1246 desc: "required field in map message set",
1247 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1248 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001249 1: {RequiredField: proto.Int32(1)},
1250 2: {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001251 },
1252 }},
1253 wire: pack.Message{
1254 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1255 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1256 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1257 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1258 }),
1259 }),
1260 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1261 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1262 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1263 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1264 }),
1265 }),
1266 }.Marshal(),
1267 },
1268 {
1269 desc: "required field in optional group unset",
1270 partial: true,
1271 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1272 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1273 }},
1274 wire: pack.Message{
1275 pack.Tag{1, pack.StartGroupType},
1276 pack.Tag{1, pack.EndGroupType},
1277 }.Marshal(),
1278 },
1279 {
1280 desc: "required field in optional group set",
1281 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1282 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001283 A: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001284 },
1285 }},
1286 wire: pack.Message{
1287 pack.Tag{1, pack.StartGroupType},
1288 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1289 pack.Tag{1, pack.EndGroupType},
1290 }.Marshal(),
1291 },
1292 {
1293 desc: "required field in repeated group unset",
1294 partial: true,
1295 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1296 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001297 {A: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001298 {},
1299 },
1300 }},
1301 wire: pack.Message{
1302 pack.Tag{3, pack.StartGroupType},
1303 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1304 pack.Tag{3, pack.EndGroupType},
1305 pack.Tag{3, pack.StartGroupType},
1306 pack.Tag{3, pack.EndGroupType},
1307 }.Marshal(),
1308 },
1309 {
1310 desc: "required field in repeated group set",
1311 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1312 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001313 {A: proto.Int32(1)},
1314 {A: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001315 },
1316 }},
1317 wire: pack.Message{
1318 pack.Tag{3, pack.StartGroupType},
1319 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1320 pack.Tag{3, pack.EndGroupType},
1321 pack.Tag{3, pack.StartGroupType},
1322 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1323 pack.Tag{3, pack.EndGroupType},
1324 }.Marshal(),
1325 },
1326 {
Damien Neil5322bdb2019-04-09 15:57:05 -07001327 desc: "required field in oneof message unset",
1328 partial: true,
1329 decodeTo: []proto.Message{
1330 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1331 &testpb.TestRequired{},
1332 }},
1333 },
1334 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1335 },
1336 {
1337 desc: "required field in oneof message set",
1338 decodeTo: []proto.Message{
1339 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1340 &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001341 RequiredField: proto.Int32(1),
Damien Neil5322bdb2019-04-09 15:57:05 -07001342 },
1343 }},
1344 },
1345 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
1346 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1347 })}.Marshal(),
1348 },
1349 {
Joe Tsai09cef322019-07-11 22:13:49 -07001350 desc: "required field in extension message unset",
1351 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001352 decodeTo: []proto.Message{build(
1353 &testpb.TestAllExtensions{},
1354 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1355 )},
1356 wire: pack.Message{
1357 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1358 }.Marshal(),
1359 },
1360 {
1361 desc: "required field in extension message set",
1362 decodeTo: []proto.Message{build(
1363 &testpb.TestAllExtensions{},
1364 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001365 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001366 }),
1367 )},
1368 wire: pack.Message{
1369 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1370 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1371 }),
1372 }.Marshal(),
1373 },
1374 {
Joe Tsai09cef322019-07-11 22:13:49 -07001375 desc: "required field in repeated extension message unset",
1376 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001377 decodeTo: []proto.Message{build(
1378 &testpb.TestAllExtensions{},
1379 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001380 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001381 {},
1382 }),
1383 )},
1384 wire: pack.Message{
1385 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1386 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1387 }),
1388 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1389 }.Marshal(),
1390 },
1391 {
1392 desc: "required field in repeated extension message set",
1393 decodeTo: []proto.Message{build(
1394 &testpb.TestAllExtensions{},
1395 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001396 {RequiredField: proto.Int32(1)},
1397 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001398 }),
1399 )},
1400 wire: pack.Message{
1401 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1402 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1403 }),
1404 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1405 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1406 }),
1407 }.Marshal(),
1408 },
Damien Neilc37adef2019-04-01 13:49:56 -07001409 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001410 desc: "nil messages",
1411 decodeTo: []proto.Message{
1412 (*testpb.TestAllTypes)(nil),
1413 (*test3pb.TestAllTypes)(nil),
1414 (*testpb.TestAllExtensions)(nil),
1415 },
1416 },
1417 {
Damien Neilc37adef2019-04-01 13:49:56 -07001418 desc: "legacy",
1419 partial: true,
1420 decodeTo: []proto.Message{
1421 &legacypb.Legacy{
1422 F1: &legacy1pb.Message{
Damien Neila8a2cea2019-07-10 16:17:16 -07001423 OptionalInt32: proto.Int32(1),
Damien Neilc37adef2019-04-01 13:49:56 -07001424 OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
1425 OptionalChildMessage: &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001426 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001427 },
1428 Optionalgroup: &legacy1pb.Message_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001429 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001430 },
1431 RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001432 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001433 },
1434 Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001435 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001436 },
1437 MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001438 true: {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001439 },
1440 OneofUnion: &legacy1pb.Message_OneofChildMessage{
1441 &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001442 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001443 },
1444 },
1445 },
1446 },
1447 },
1448 wire: pack.Message{
1449 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1450 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1451 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1452 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1453 pack.Tag{1, pack.BytesType}, pack.String("x"),
1454 }),
1455 pack.Tag{120, pack.StartGroupType},
1456 pack.Tag{1, pack.BytesType}, pack.String("x"),
1457 pack.Tag{120, pack.EndGroupType},
1458 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1459 pack.Tag{1, pack.BytesType}, pack.String("x"),
1460 }),
1461 pack.Tag{520, pack.StartGroupType},
1462 pack.Tag{1, pack.BytesType}, pack.String("x"),
1463 pack.Tag{520, pack.EndGroupType},
1464 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1465 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1466 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1467 pack.Tag{1, pack.BytesType}, pack.String("x"),
1468 }),
1469 }),
1470 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1471 pack.Tag{1, pack.BytesType}, pack.String("x"),
1472 }),
1473 }),
1474 }.Marshal(),
1475 },
Damien Neilba23aa52018-12-07 14:38:17 -08001476}
1477
Damien Neilbc310b52019-04-11 11:46:55 -07001478var invalidUTF8TestProtos = []testProto{
1479 {
1480 desc: "invalid UTF-8 in optional string field",
1481 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1482 OptionalString: "abc\xff",
1483 }},
1484 wire: pack.Message{
1485 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1486 }.Marshal(),
1487 },
1488 {
1489 desc: "invalid UTF-8 in repeated string field",
1490 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1491 RepeatedString: []string{"foo", "abc\xff"},
1492 }},
1493 wire: pack.Message{
1494 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1495 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1496 }.Marshal(),
1497 },
1498 {
1499 desc: "invalid UTF-8 in nested message",
1500 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1501 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1502 Corecursive: &test3pb.TestAllTypes{
1503 OptionalString: "abc\xff",
1504 },
1505 },
1506 }},
1507 wire: pack.Message{
1508 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1509 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1510 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1511 }),
1512 }),
1513 }.Marshal(),
1514 },
1515 {
Damien Neilc37adef2019-04-01 13:49:56 -07001516 desc: "invalid UTF-8 in oneof field",
1517 decodeTo: []proto.Message{
1518 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
1519 },
1520 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1521 },
1522 {
Damien Neilbc310b52019-04-11 11:46:55 -07001523 desc: "invalid UTF-8 in map key",
1524 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1525 MapStringString: map[string]string{"key\xff": "val"},
1526 }},
1527 wire: pack.Message{
1528 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1529 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1530 pack.Tag{2, pack.BytesType}, pack.String("val"),
1531 }),
1532 }.Marshal(),
1533 },
1534 {
1535 desc: "invalid UTF-8 in map value",
1536 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1537 MapStringString: map[string]string{"key": "val\xff"},
1538 }},
1539 wire: pack.Message{
1540 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1541 pack.Tag{1, pack.BytesType}, pack.String("key"),
1542 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1543 }),
1544 }.Marshal(),
1545 },
1546}
1547
Joe Tsaic51e2e02019-07-13 00:44:41 -07001548var noEnforceUTF8TestProtos = []testProto{
1549 {
1550 desc: "invalid UTF-8 in optional string field",
1551 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1552 OptionalString: string("abc\xff"),
1553 }},
1554 wire: pack.Message{
1555 pack.Tag{1, pack.BytesType}, pack.String("abc\xff"),
1556 }.Marshal(),
1557 },
1558 {
1559 desc: "invalid UTF-8 in optional string field of Go bytes",
1560 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1561 OptionalBytes: []byte("abc\xff"),
1562 }},
1563 wire: pack.Message{
1564 pack.Tag{2, pack.BytesType}, pack.String("abc\xff"),
1565 }.Marshal(),
1566 },
1567 {
1568 desc: "invalid UTF-8 in repeated string field",
1569 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1570 RepeatedString: []string{string("foo"), string("abc\xff")},
1571 }},
1572 wire: pack.Message{
1573 pack.Tag{3, pack.BytesType}, pack.String("foo"),
1574 pack.Tag{3, pack.BytesType}, pack.String("abc\xff"),
1575 }.Marshal(),
1576 },
1577 {
1578 desc: "invalid UTF-8 in repeated string field of Go bytes",
1579 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1580 RepeatedBytes: [][]byte{[]byte("foo"), []byte("abc\xff")},
1581 }},
1582 wire: pack.Message{
1583 pack.Tag{4, pack.BytesType}, pack.String("foo"),
1584 pack.Tag{4, pack.BytesType}, pack.String("abc\xff"),
1585 }.Marshal(),
1586 },
1587 {
1588 desc: "invalid UTF-8 in oneof string field",
1589 decodeTo: []proto.Message{
1590 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofString{string("abc\xff")}},
1591 },
1592 wire: pack.Message{pack.Tag{5, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1593 },
1594 {
1595 desc: "invalid UTF-8 in oneof string field of Go bytes",
1596 decodeTo: []proto.Message{
1597 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofBytes{[]byte("abc\xff")}},
1598 },
1599 wire: pack.Message{pack.Tag{6, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1600 },
1601}
1602
1603type TestNoEnforceUTF8 struct {
1604 OptionalString string `protobuf:"bytes,1,opt,name=optional_string"`
1605 OptionalBytes []byte `protobuf:"bytes,2,opt,name=optional_bytes"`
1606 RepeatedString []string `protobuf:"bytes,3,rep,name=repeated_string"`
1607 RepeatedBytes [][]byte `protobuf:"bytes,4,rep,name=repeated_bytes"`
1608 OneofField isOneofField `protobuf_oneof:"oneof_field"`
1609}
1610
1611type isOneofField interface{ isOneofField() }
1612
1613type TestNoEnforceUTF8_OneofString struct {
1614 OneofString string `protobuf:"bytes,5,opt,name=oneof_string,oneof"`
1615}
1616type TestNoEnforceUTF8_OneofBytes struct {
1617 OneofBytes []byte `protobuf:"bytes,6,opt,name=oneof_bytes,oneof"`
1618}
1619
1620func (*TestNoEnforceUTF8_OneofString) isOneofField() {}
1621func (*TestNoEnforceUTF8_OneofBytes) isOneofField() {}
1622
Joe Tsai74615a32019-07-14 18:51:46 -07001623func (m *TestNoEnforceUTF8) ProtoReflect() protoreflect.Message {
Joe Tsaic51e2e02019-07-13 00:44:41 -07001624 return messageInfo_TestNoEnforceUTF8.MessageOf(m)
1625}
1626
1627var messageInfo_TestNoEnforceUTF8 = protoimpl.MessageInfo{
Damien Neil16163b42019-08-06 15:43:25 -07001628 GoReflectType: reflect.TypeOf((*TestNoEnforceUTF8)(nil)),
1629 Desc: func() protoreflect.MessageDescriptor {
1630 pb := new(descriptorpb.FileDescriptorProto)
1631 if err := prototext.Unmarshal([]byte(`
Joe Tsaic51e2e02019-07-13 00:44:41 -07001632 syntax: "proto3"
1633 name: "test.proto"
1634 message_type: [{
1635 name: "TestNoEnforceUTF8"
1636 field: [
1637 {name:"optional_string" number:1 label:LABEL_OPTIONAL type:TYPE_STRING},
1638 {name:"optional_bytes" number:2 label:LABEL_OPTIONAL type:TYPE_STRING},
1639 {name:"repeated_string" number:3 label:LABEL_REPEATED type:TYPE_STRING},
1640 {name:"repeated_bytes" number:4 label:LABEL_REPEATED type:TYPE_STRING},
1641 {name:"oneof_string" number:5 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0},
1642 {name:"oneof_bytes" number:6 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0}
1643 ]
1644 oneof_decl: [{name:"oneof_field"}]
1645 }]
1646 `), pb); err != nil {
Damien Neil16163b42019-08-06 15:43:25 -07001647 panic(err)
1648 }
1649 fd, err := protodesc.NewFile(pb, nil)
1650 if err != nil {
1651 panic(err)
1652 }
1653 md := fd.Messages().Get(0)
1654 for i := 0; i < md.Fields().Len(); i++ {
1655 md.Fields().Get(i).(*filedesc.Field).L1.HasEnforceUTF8 = true
1656 md.Fields().Get(i).(*filedesc.Field).L1.EnforceUTF8 = false
1657 }
1658 return md
1659 }(),
Joe Tsaic51e2e02019-07-13 00:44:41 -07001660 OneofWrappers: []interface{}{
1661 (*TestNoEnforceUTF8_OneofString)(nil),
1662 (*TestNoEnforceUTF8_OneofBytes)(nil),
1663 },
1664}
1665
Damien Neil4be2fb42018-12-17 11:16:16 -08001666func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001667 for _, opt := range opts {
1668 opt(m)
1669 }
1670 return m
1671}
1672
Damien Neil4be2fb42018-12-17 11:16:16 -08001673type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001674
Joe Tsai74615a32019-07-14 18:51:46 -07001675func unknown(raw protoreflect.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001676 return func(m proto.Message) {
Joe Tsai378c1322019-04-25 23:48:08 -07001677 m.ProtoReflect().SetUnknown(raw)
Damien Neile6f060f2019-04-23 17:11:02 -07001678 }
1679}
1680
Damien Neilf1e905b2019-08-08 15:45:59 -07001681func extend(desc protoreflect.ExtensionType, value interface{}) buildOpt {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001682 // TODO: Should ExtensionType.ValueOf accept []T instead of *[]T?
1683 t := reflect.TypeOf(value)
1684 if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
1685 v := reflect.New(t)
1686 v.Elem().Set(reflect.ValueOf(value))
1687 value = v.Interface()
1688 }
Damien Neil4be2fb42018-12-17 11:16:16 -08001689 return func(m proto.Message) {
Damien Neil92f76182019-08-02 16:58:08 -07001690 proto.SetExtension(m, desc, value)
Damien Neilba23aa52018-12-07 14:38:17 -08001691 }
1692}
Damien Neil61e93c72019-03-27 09:23:20 -07001693
1694func marshalText(m proto.Message) string {
Joe Tsai8d30bbe2019-05-16 15:53:25 -07001695 b, _ := prototext.MarshalOptions{Indent: "\t", AllowPartial: true}.Marshal(m)
Damien Neil61e93c72019-03-27 09:23:20 -07001696 return string(b)
1697}