blob: 40a627bd78a2f84a36c14edf0b349e6bc55cd72a [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"
Joe Tsai6e095992019-08-10 13:56:36 -070024 weakpb "google.golang.org/protobuf/internal/testprotos/test/weak1"
Damien Neile89e6242019-05-13 23:55:40 -070025 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Joe Tsaic51e2e02019-07-13 00:44:41 -070026 "google.golang.org/protobuf/types/descriptorpb"
Damien Neilba23aa52018-12-07 14:38:17 -080027)
28
29type testProto struct {
Joe Tsai09cef322019-07-11 22:13:49 -070030 desc string
31 decodeTo []proto.Message
32 wire []byte
33 partial bool
Damien Neilba23aa52018-12-07 14:38:17 -080034}
35
36func TestDecode(t *testing.T) {
37 for _, test := range testProtos {
38 for _, want := range test.decodeTo {
39 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070040 opts := proto.UnmarshalOptions{
41 AllowPartial: test.partial,
42 }
Damien Neilba23aa52018-12-07 14:38:17 -080043 wire := append(([]byte)(nil), test.wire...)
Damien Neil4be2fb42018-12-17 11:16:16 -080044 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
Damien Neil96c229a2019-04-03 12:17:24 -070045 if err := opts.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070046 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080047 return
48 }
49
50 // Aliasing check: Modifying the original wire bytes shouldn't
51 // affect the unmarshaled message.
52 for i := range wire {
53 wire[i] = 0
54 }
Joe Tsaidb38ddd2019-05-07 15:14:40 -070055 if !proto.Equal(got, want) {
Damien Neil61e93c72019-03-27 09:23:20 -070056 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080057 }
58 })
59 }
60 }
61}
62
Damien Neil96c229a2019-04-03 12:17:24 -070063func TestDecodeRequiredFieldChecks(t *testing.T) {
64 for _, test := range testProtos {
65 if !test.partial {
66 continue
67 }
Damien Neil96c229a2019-04-03 12:17:24 -070068 for _, m := range test.decodeTo {
69 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
70 got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
71 if err := proto.Unmarshal(test.wire, got); err == nil {
72 t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got))
73 }
74 })
75 }
76 }
77}
78
Damien Neilbc310b52019-04-11 11:46:55 -070079func TestDecodeInvalidUTF8(t *testing.T) {
80 for _, test := range invalidUTF8TestProtos {
81 for _, want := range test.decodeTo {
82 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
83 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
84 err := proto.Unmarshal(test.wire, got)
Damien Neil8c86fc52019-06-19 09:28:29 -070085 if err == nil {
Damien Neilbc310b52019-04-11 11:46:55 -070086 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
87 }
Damien Neilbc310b52019-04-11 11:46:55 -070088 })
89 }
90 }
91}
92
Joe Tsaic51e2e02019-07-13 00:44:41 -070093func TestDecodeNoEnforceUTF8(t *testing.T) {
94 for _, test := range noEnforceUTF8TestProtos {
95 for _, want := range test.decodeTo {
96 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
97 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
98 err := proto.Unmarshal(test.wire, got)
99 switch {
Joe Tsai1799d112019-08-08 13:31:59 -0700100 case flags.ProtoLegacy && err != nil:
Joe Tsaic51e2e02019-07-13 00:44:41 -0700101 t.Errorf("Unmarshal returned unexpected error: %v\nMessage:\n%v", err, marshalText(want))
Joe Tsai1799d112019-08-08 13:31:59 -0700102 case !flags.ProtoLegacy && err == nil:
Joe Tsaic51e2e02019-07-13 00:44:41 -0700103 t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
104 }
105 })
106 }
107 }
108}
109
Damien Neil8003f082019-08-02 15:13:00 -0700110func TestDecodeZeroLengthBytes(t *testing.T) {
111 // Verify that proto3 bytes fields don't give the mistaken
112 // impression that they preserve presence.
113 wire := pack.Message{
114 pack.Tag{15, pack.BytesType}, pack.Bytes(nil),
115 }.Marshal()
116 m := &test3pb.TestAllTypes{}
117 if err := proto.Unmarshal(wire, m); err != nil {
118 t.Fatal(err)
119 }
120 if m.OptionalBytes != nil {
121 t.Errorf("unmarshal zero-length proto3 bytes field: got %v, want nil", m.OptionalBytes)
122 }
123}
124
Joe Tsai9b22b932019-08-08 19:23:32 -0700125func TestDecodeOneofNilWrapper(t *testing.T) {
126 wire := pack.Message{
127 pack.Tag{111, pack.VarintType}, pack.Varint(1111),
128 }.Marshal()
129 m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)}
130 if err := proto.Unmarshal(wire, m); err != nil {
131 t.Fatal(err)
132 }
133 if got := m.GetOneofUint32(); got != 1111 {
134 t.Errorf("GetOneofUint32() = %v, want %v", got, 1111)
135 }
136}
137
Tuo Shan6e25d8c2019-08-22 18:52:43 -0700138func TestDecodeInvalidFieldNumbers(t *testing.T) {
139 for _, test := range invalidFieldNumberTestProtos {
140 t.Run(test.desc, func(t *testing.T) {
141 decoded := new(testpb.TestAllTypes) // type doesn't matter since we expect errors
142 err := proto.Unmarshal(test.wire, decoded)
143 if err == nil && !test.allowed {
144 t.Error("unmarshal: got nil want error")
145 } else if err != nil && test.allowed {
146 t.Errorf("unmarshal: got %v want nil since %s is allowed by Unmarshal", err, test.desc)
147 }
148 })
149 }
150}
151
Damien Neilba23aa52018-12-07 14:38:17 -0800152var testProtos = []testProto{
153 {
154 desc: "basic scalar types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800155 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700156 OptionalInt32: proto.Int32(1001),
157 OptionalInt64: proto.Int64(1002),
158 OptionalUint32: proto.Uint32(1003),
159 OptionalUint64: proto.Uint64(1004),
160 OptionalSint32: proto.Int32(1005),
161 OptionalSint64: proto.Int64(1006),
162 OptionalFixed32: proto.Uint32(1007),
163 OptionalFixed64: proto.Uint64(1008),
164 OptionalSfixed32: proto.Int32(1009),
165 OptionalSfixed64: proto.Int64(1010),
166 OptionalFloat: proto.Float32(1011.5),
167 OptionalDouble: proto.Float64(1012.5),
168 OptionalBool: proto.Bool(true),
169 OptionalString: proto.String("string"),
Damien Neilba23aa52018-12-07 14:38:17 -0800170 OptionalBytes: []byte("bytes"),
171 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
Damien Neil3b46ade2019-03-26 13:55:02 -0700172 }, &test3pb.TestAllTypes{
173 OptionalInt32: 1001,
174 OptionalInt64: 1002,
175 OptionalUint32: 1003,
176 OptionalUint64: 1004,
177 OptionalSint32: 1005,
178 OptionalSint64: 1006,
179 OptionalFixed32: 1007,
180 OptionalFixed64: 1008,
181 OptionalSfixed32: 1009,
182 OptionalSfixed64: 1010,
183 OptionalFloat: 1011.5,
184 OptionalDouble: 1012.5,
185 OptionalBool: true,
186 OptionalString: "string",
187 OptionalBytes: []byte("bytes"),
188 OptionalNestedEnum: test3pb.TestAllTypes_BAR,
Damien Neilba23aa52018-12-07 14:38:17 -0800189 }, build(
190 &testpb.TestAllExtensions{},
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700191 extend(testpb.E_OptionalInt32Extension, int32(1001)),
192 extend(testpb.E_OptionalInt64Extension, int64(1002)),
193 extend(testpb.E_OptionalUint32Extension, uint32(1003)),
194 extend(testpb.E_OptionalUint64Extension, uint64(1004)),
195 extend(testpb.E_OptionalSint32Extension, int32(1005)),
196 extend(testpb.E_OptionalSint64Extension, int64(1006)),
197 extend(testpb.E_OptionalFixed32Extension, uint32(1007)),
198 extend(testpb.E_OptionalFixed64Extension, uint64(1008)),
199 extend(testpb.E_OptionalSfixed32Extension, int32(1009)),
200 extend(testpb.E_OptionalSfixed64Extension, int64(1010)),
201 extend(testpb.E_OptionalFloatExtension, float32(1011.5)),
202 extend(testpb.E_OptionalDoubleExtension, float64(1012.5)),
203 extend(testpb.E_OptionalBoolExtension, bool(true)),
204 extend(testpb.E_OptionalStringExtension, string("string")),
Damien Neilba23aa52018-12-07 14:38:17 -0800205 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
Joe Tsai8d30bbe2019-05-16 15:53:25 -0700206 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR),
Damien Neilba23aa52018-12-07 14:38:17 -0800207 )},
208 wire: pack.Message{
209 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
210 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
211 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
212 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
213 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
214 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
215 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
216 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
217 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
218 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
219 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
220 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
221 pack.Tag{13, pack.VarintType}, pack.Bool(true),
222 pack.Tag{14, pack.BytesType}, pack.String("string"),
223 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
224 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
225 }.Marshal(),
226 },
227 {
Damien Neil8003f082019-08-02 15:13:00 -0700228 desc: "zero values",
229 decodeTo: []proto.Message{&testpb.TestAllTypes{
230 OptionalInt32: proto.Int32(0),
231 OptionalInt64: proto.Int64(0),
232 OptionalUint32: proto.Uint32(0),
233 OptionalUint64: proto.Uint64(0),
234 OptionalSint32: proto.Int32(0),
235 OptionalSint64: proto.Int64(0),
236 OptionalFixed32: proto.Uint32(0),
237 OptionalFixed64: proto.Uint64(0),
238 OptionalSfixed32: proto.Int32(0),
239 OptionalSfixed64: proto.Int64(0),
240 OptionalFloat: proto.Float32(0),
241 OptionalDouble: proto.Float64(0),
242 OptionalBool: proto.Bool(false),
243 OptionalString: proto.String(""),
244 OptionalBytes: []byte{},
245 }, &test3pb.TestAllTypes{}, build(
246 &testpb.TestAllExtensions{},
247 extend(testpb.E_OptionalInt32Extension, int32(0)),
248 extend(testpb.E_OptionalInt64Extension, int64(0)),
249 extend(testpb.E_OptionalUint32Extension, uint32(0)),
250 extend(testpb.E_OptionalUint64Extension, uint64(0)),
251 extend(testpb.E_OptionalSint32Extension, int32(0)),
252 extend(testpb.E_OptionalSint64Extension, int64(0)),
253 extend(testpb.E_OptionalFixed32Extension, uint32(0)),
254 extend(testpb.E_OptionalFixed64Extension, uint64(0)),
255 extend(testpb.E_OptionalSfixed32Extension, int32(0)),
256 extend(testpb.E_OptionalSfixed64Extension, int64(0)),
257 extend(testpb.E_OptionalFloatExtension, float32(0)),
258 extend(testpb.E_OptionalDoubleExtension, float64(0)),
259 extend(testpb.E_OptionalBoolExtension, bool(false)),
260 extend(testpb.E_OptionalStringExtension, string("")),
261 extend(testpb.E_OptionalBytesExtension, []byte{}),
262 )},
263 wire: pack.Message{
264 pack.Tag{1, pack.VarintType}, pack.Varint(0),
265 pack.Tag{2, pack.VarintType}, pack.Varint(0),
266 pack.Tag{3, pack.VarintType}, pack.Uvarint(0),
267 pack.Tag{4, pack.VarintType}, pack.Uvarint(0),
268 pack.Tag{5, pack.VarintType}, pack.Svarint(0),
269 pack.Tag{6, pack.VarintType}, pack.Svarint(0),
270 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(0),
271 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(0),
272 pack.Tag{9, pack.Fixed32Type}, pack.Int32(0),
273 pack.Tag{10, pack.Fixed64Type}, pack.Int64(0),
274 pack.Tag{11, pack.Fixed32Type}, pack.Float32(0),
275 pack.Tag{12, pack.Fixed64Type}, pack.Float64(0),
276 pack.Tag{13, pack.VarintType}, pack.Bool(false),
277 pack.Tag{14, pack.BytesType}, pack.String(""),
278 pack.Tag{15, pack.BytesType}, pack.Bytes(nil),
279 }.Marshal(),
280 },
281 {
Damien Neilba23aa52018-12-07 14:38:17 -0800282 desc: "groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800283 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800284 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700285 A: proto.Int32(1017),
Damien Neilba23aa52018-12-07 14:38:17 -0800286 },
287 }, build(
288 &testpb.TestAllExtensions{},
289 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700290 A: proto.Int32(1017),
Damien Neilba23aa52018-12-07 14:38:17 -0800291 }),
292 )},
293 wire: pack.Message{
294 pack.Tag{16, pack.StartGroupType},
295 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
296 pack.Tag{16, pack.EndGroupType},
297 }.Marshal(),
298 },
299 {
300 desc: "groups (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800301 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800302 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700303 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800304 },
305 }, build(
306 &testpb.TestAllExtensions{},
307 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700308 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800309 }),
310 )},
311 wire: pack.Message{
312 pack.Tag{16, pack.StartGroupType},
313 pack.Tag{17, pack.VarintType}, pack.Varint(1),
314 pack.Tag{16, pack.EndGroupType},
315 pack.Tag{16, pack.StartGroupType},
316 pack.Tag{17, pack.VarintType}, pack.Varint(2),
317 pack.Tag{16, pack.EndGroupType},
318 }.Marshal(),
319 },
320 {
321 desc: "messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800322 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800323 OptionalNestedMessage: &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 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700329 }, &test3pb.TestAllTypes{
330 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
331 A: 42,
332 Corecursive: &test3pb.TestAllTypes{
333 OptionalInt32: 43,
334 },
335 },
Damien Neilba23aa52018-12-07 14:38:17 -0800336 }, build(
337 &testpb.TestAllExtensions{},
338 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700339 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800340 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700341 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800342 },
343 }),
344 )},
345 wire: pack.Message{
346 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
347 pack.Tag{1, pack.VarintType}, pack.Varint(42),
348 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
349 pack.Tag{1, pack.VarintType}, pack.Varint(43),
350 }),
351 }),
352 }.Marshal(),
353 },
354 {
355 desc: "messages (split across multiple tags)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800356 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800357 OptionalNestedMessage: &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 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700363 }, &test3pb.TestAllTypes{
364 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
365 A: 42,
366 Corecursive: &test3pb.TestAllTypes{
367 OptionalInt32: 43,
368 },
369 },
Damien Neilba23aa52018-12-07 14:38:17 -0800370 }, build(
371 &testpb.TestAllExtensions{},
372 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700373 A: proto.Int32(42),
Damien Neilba23aa52018-12-07 14:38:17 -0800374 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700375 OptionalInt32: proto.Int32(43),
Damien Neilba23aa52018-12-07 14:38:17 -0800376 },
377 }),
378 )},
379 wire: pack.Message{
380 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
381 pack.Tag{1, pack.VarintType}, pack.Varint(42),
382 }),
383 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
384 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
385 pack.Tag{1, pack.VarintType}, pack.Varint(43),
386 }),
387 }),
388 }.Marshal(),
389 },
390 {
391 desc: "messages (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800392 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800393 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700394 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800395 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700396 }, &test3pb.TestAllTypes{
397 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
398 A: 2,
399 },
Damien Neilba23aa52018-12-07 14:38:17 -0800400 }, build(
401 &testpb.TestAllExtensions{},
402 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700403 A: proto.Int32(2),
Damien Neilba23aa52018-12-07 14:38:17 -0800404 }),
405 )},
406 wire: pack.Message{
407 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
408 pack.Tag{1, pack.VarintType}, pack.Varint(1),
409 }),
410 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
411 pack.Tag{1, pack.VarintType}, pack.Varint(2),
412 }),
413 }.Marshal(),
414 },
415 {
416 desc: "basic repeated types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800417 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800418 RepeatedInt32: []int32{1001, 2001},
419 RepeatedInt64: []int64{1002, 2002},
420 RepeatedUint32: []uint32{1003, 2003},
421 RepeatedUint64: []uint64{1004, 2004},
422 RepeatedSint32: []int32{1005, 2005},
423 RepeatedSint64: []int64{1006, 2006},
424 RepeatedFixed32: []uint32{1007, 2007},
425 RepeatedFixed64: []uint64{1008, 2008},
426 RepeatedSfixed32: []int32{1009, 2009},
427 RepeatedSfixed64: []int64{1010, 2010},
428 RepeatedFloat: []float32{1011.5, 2011.5},
429 RepeatedDouble: []float64{1012.5, 2012.5},
430 RepeatedBool: []bool{true, false},
431 RepeatedString: []string{"foo", "bar"},
432 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
433 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
434 testpb.TestAllTypes_FOO,
435 testpb.TestAllTypes_BAR,
436 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700437 }, &test3pb.TestAllTypes{
438 RepeatedInt32: []int32{1001, 2001},
439 RepeatedInt64: []int64{1002, 2002},
440 RepeatedUint32: []uint32{1003, 2003},
441 RepeatedUint64: []uint64{1004, 2004},
442 RepeatedSint32: []int32{1005, 2005},
443 RepeatedSint64: []int64{1006, 2006},
444 RepeatedFixed32: []uint32{1007, 2007},
445 RepeatedFixed64: []uint64{1008, 2008},
446 RepeatedSfixed32: []int32{1009, 2009},
447 RepeatedSfixed64: []int64{1010, 2010},
448 RepeatedFloat: []float32{1011.5, 2011.5},
449 RepeatedDouble: []float64{1012.5, 2012.5},
450 RepeatedBool: []bool{true, false},
451 RepeatedString: []string{"foo", "bar"},
452 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
453 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
454 test3pb.TestAllTypes_FOO,
455 test3pb.TestAllTypes_BAR,
456 },
Damien Neilba23aa52018-12-07 14:38:17 -0800457 }, build(
458 &testpb.TestAllExtensions{},
459 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
460 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
461 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
462 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
463 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
464 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
465 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
466 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
467 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
468 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
469 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
470 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
471 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
472 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
473 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
474 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
475 testpb.TestAllTypes_FOO,
476 testpb.TestAllTypes_BAR,
477 }),
478 )},
479 wire: pack.Message{
480 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
481 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
482 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
483 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
484 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
485 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
486 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
487 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
488 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
489 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
490 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
491 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
492 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
493 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
494 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
495 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
496 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
497 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
498 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
499 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
500 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
501 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
502 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
503 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
504 pack.Tag{43, pack.VarintType}, pack.Bool(true),
505 pack.Tag{43, pack.VarintType}, pack.Bool(false),
506 pack.Tag{44, pack.BytesType}, pack.String("foo"),
507 pack.Tag{44, pack.BytesType}, pack.String("bar"),
508 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
509 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
510 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
511 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
512 }.Marshal(),
513 },
514 {
515 desc: "basic repeated types (packed encoding)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800516 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800517 RepeatedInt32: []int32{1001, 2001},
518 RepeatedInt64: []int64{1002, 2002},
519 RepeatedUint32: []uint32{1003, 2003},
520 RepeatedUint64: []uint64{1004, 2004},
521 RepeatedSint32: []int32{1005, 2005},
522 RepeatedSint64: []int64{1006, 2006},
523 RepeatedFixed32: []uint32{1007, 2007},
524 RepeatedFixed64: []uint64{1008, 2008},
525 RepeatedSfixed32: []int32{1009, 2009},
526 RepeatedSfixed64: []int64{1010, 2010},
527 RepeatedFloat: []float32{1011.5, 2011.5},
528 RepeatedDouble: []float64{1012.5, 2012.5},
529 RepeatedBool: []bool{true, false},
530 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
531 testpb.TestAllTypes_FOO,
532 testpb.TestAllTypes_BAR,
533 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700534 }, &test3pb.TestAllTypes{
535 RepeatedInt32: []int32{1001, 2001},
536 RepeatedInt64: []int64{1002, 2002},
537 RepeatedUint32: []uint32{1003, 2003},
538 RepeatedUint64: []uint64{1004, 2004},
539 RepeatedSint32: []int32{1005, 2005},
540 RepeatedSint64: []int64{1006, 2006},
541 RepeatedFixed32: []uint32{1007, 2007},
542 RepeatedFixed64: []uint64{1008, 2008},
543 RepeatedSfixed32: []int32{1009, 2009},
544 RepeatedSfixed64: []int64{1010, 2010},
545 RepeatedFloat: []float32{1011.5, 2011.5},
546 RepeatedDouble: []float64{1012.5, 2012.5},
547 RepeatedBool: []bool{true, false},
548 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
549 test3pb.TestAllTypes_FOO,
550 test3pb.TestAllTypes_BAR,
551 },
Damien Neilba23aa52018-12-07 14:38:17 -0800552 }, build(
553 &testpb.TestAllExtensions{},
554 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
555 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
556 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
557 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
558 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
559 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
560 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
561 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
562 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
563 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
564 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
565 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
566 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
567 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
568 testpb.TestAllTypes_FOO,
569 testpb.TestAllTypes_BAR,
570 }),
571 )},
572 wire: pack.Message{
573 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
574 pack.Varint(1001), pack.Varint(2001),
575 },
576 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
577 pack.Varint(1002), pack.Varint(2002),
578 },
579 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
580 pack.Uvarint(1003), pack.Uvarint(2003),
581 },
582 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
583 pack.Uvarint(1004), pack.Uvarint(2004),
584 },
585 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
586 pack.Svarint(1005), pack.Svarint(2005),
587 },
588 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
589 pack.Svarint(1006), pack.Svarint(2006),
590 },
591 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
592 pack.Uint32(1007), pack.Uint32(2007),
593 },
594 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
595 pack.Uint64(1008), pack.Uint64(2008),
596 },
597 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
598 pack.Int32(1009), pack.Int32(2009),
599 },
600 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
601 pack.Int64(1010), pack.Int64(2010),
602 },
603 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
604 pack.Float32(1011.5), pack.Float32(2011.5),
605 },
606 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
607 pack.Float64(1012.5), pack.Float64(2012.5),
608 },
609 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
610 pack.Bool(true), pack.Bool(false),
611 },
612 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
613 pack.Varint(int(testpb.TestAllTypes_FOO)),
614 pack.Varint(int(testpb.TestAllTypes_BAR)),
615 },
616 }.Marshal(),
617 },
618 {
Damien Neil7492a092019-07-10 15:23:29 -0700619 desc: "packed repeated types",
620 decodeTo: []proto.Message{&testpb.TestPackedTypes{
621 PackedInt32: []int32{1001, 2001},
622 PackedInt64: []int64{1002, 2002},
623 PackedUint32: []uint32{1003, 2003},
624 PackedUint64: []uint64{1004, 2004},
625 PackedSint32: []int32{1005, 2005},
626 PackedSint64: []int64{1006, 2006},
627 PackedFixed32: []uint32{1007, 2007},
628 PackedFixed64: []uint64{1008, 2008},
629 PackedSfixed32: []int32{1009, 2009},
630 PackedSfixed64: []int64{1010, 2010},
631 PackedFloat: []float32{1011.5, 2011.5},
632 PackedDouble: []float64{1012.5, 2012.5},
633 PackedBool: []bool{true, false},
634 PackedEnum: []testpb.ForeignEnum{
635 testpb.ForeignEnum_FOREIGN_FOO,
636 testpb.ForeignEnum_FOREIGN_BAR,
637 },
638 }, build(
639 &testpb.TestPackedExtensions{},
640 extend(testpb.E_PackedInt32Extension, []int32{1001, 2001}),
641 extend(testpb.E_PackedInt64Extension, []int64{1002, 2002}),
642 extend(testpb.E_PackedUint32Extension, []uint32{1003, 2003}),
643 extend(testpb.E_PackedUint64Extension, []uint64{1004, 2004}),
644 extend(testpb.E_PackedSint32Extension, []int32{1005, 2005}),
645 extend(testpb.E_PackedSint64Extension, []int64{1006, 2006}),
646 extend(testpb.E_PackedFixed32Extension, []uint32{1007, 2007}),
647 extend(testpb.E_PackedFixed64Extension, []uint64{1008, 2008}),
648 extend(testpb.E_PackedSfixed32Extension, []int32{1009, 2009}),
649 extend(testpb.E_PackedSfixed64Extension, []int64{1010, 2010}),
650 extend(testpb.E_PackedFloatExtension, []float32{1011.5, 2011.5}),
651 extend(testpb.E_PackedDoubleExtension, []float64{1012.5, 2012.5}),
652 extend(testpb.E_PackedBoolExtension, []bool{true, false}),
653 extend(testpb.E_PackedEnumExtension, []testpb.ForeignEnum{
654 testpb.ForeignEnum_FOREIGN_FOO,
655 testpb.ForeignEnum_FOREIGN_BAR,
656 }),
657 )},
658 wire: pack.Message{
659 pack.Tag{90, pack.BytesType}, pack.LengthPrefix{
660 pack.Varint(1001), pack.Varint(2001),
661 },
662 pack.Tag{91, pack.BytesType}, pack.LengthPrefix{
663 pack.Varint(1002), pack.Varint(2002),
664 },
665 pack.Tag{92, pack.BytesType}, pack.LengthPrefix{
666 pack.Uvarint(1003), pack.Uvarint(2003),
667 },
668 pack.Tag{93, pack.BytesType}, pack.LengthPrefix{
669 pack.Uvarint(1004), pack.Uvarint(2004),
670 },
671 pack.Tag{94, pack.BytesType}, pack.LengthPrefix{
672 pack.Svarint(1005), pack.Svarint(2005),
673 },
674 pack.Tag{95, pack.BytesType}, pack.LengthPrefix{
675 pack.Svarint(1006), pack.Svarint(2006),
676 },
677 pack.Tag{96, pack.BytesType}, pack.LengthPrefix{
678 pack.Uint32(1007), pack.Uint32(2007),
679 },
680 pack.Tag{97, pack.BytesType}, pack.LengthPrefix{
681 pack.Uint64(1008), pack.Uint64(2008),
682 },
683 pack.Tag{98, pack.BytesType}, pack.LengthPrefix{
684 pack.Int32(1009), pack.Int32(2009),
685 },
686 pack.Tag{99, pack.BytesType}, pack.LengthPrefix{
687 pack.Int64(1010), pack.Int64(2010),
688 },
689 pack.Tag{100, pack.BytesType}, pack.LengthPrefix{
690 pack.Float32(1011.5), pack.Float32(2011.5),
691 },
692 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{
693 pack.Float64(1012.5), pack.Float64(2012.5),
694 },
695 pack.Tag{102, pack.BytesType}, pack.LengthPrefix{
696 pack.Bool(true), pack.Bool(false),
697 },
698 pack.Tag{103, pack.BytesType}, pack.LengthPrefix{
699 pack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
700 pack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
701 },
702 }.Marshal(),
703 },
704 {
Damien Neilba23aa52018-12-07 14:38:17 -0800705 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800706 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800707 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700708 {A: proto.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700709 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700710 {A: proto.Int32(2)},
Damien Neilba23aa52018-12-07 14:38:17 -0800711 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700712 }, &test3pb.TestAllTypes{
713 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
714 {A: 1},
Damien Neilc37adef2019-04-01 13:49:56 -0700715 nil,
Damien Neil3b46ade2019-03-26 13:55:02 -0700716 {A: 2},
717 },
Damien Neilba23aa52018-12-07 14:38:17 -0800718 }, build(
719 &testpb.TestAllExtensions{},
720 extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700721 {A: proto.Int32(1)},
Damien Neilc37adef2019-04-01 13:49:56 -0700722 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700723 {A: proto.Int32(2)},
Damien Neilba23aa52018-12-07 14:38:17 -0800724 }),
725 )},
726 wire: pack.Message{
727 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
728 pack.Tag{1, pack.VarintType}, pack.Varint(1),
729 }),
Damien Neilc37adef2019-04-01 13:49:56 -0700730 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
Damien Neilba23aa52018-12-07 14:38:17 -0800731 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
732 pack.Tag{1, pack.VarintType}, pack.Varint(2),
733 }),
734 }.Marshal(),
735 },
736 {
737 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800738 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800739 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -0700740 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700741 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700742 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800743 },
744 }, build(
745 &testpb.TestAllExtensions{},
746 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
Damien Neila8a2cea2019-07-10 16:17:16 -0700747 {A: proto.Int32(1017)},
Damien Neilc37adef2019-04-01 13:49:56 -0700748 nil,
Damien Neila8a2cea2019-07-10 16:17:16 -0700749 {A: proto.Int32(2017)},
Damien Neilba23aa52018-12-07 14:38:17 -0800750 }),
751 )},
752 wire: pack.Message{
753 pack.Tag{46, pack.StartGroupType},
754 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
755 pack.Tag{46, pack.EndGroupType},
756 pack.Tag{46, pack.StartGroupType},
Damien Neilc37adef2019-04-01 13:49:56 -0700757 pack.Tag{46, pack.EndGroupType},
758 pack.Tag{46, pack.StartGroupType},
Damien Neilba23aa52018-12-07 14:38:17 -0800759 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
760 pack.Tag{46, pack.EndGroupType},
761 }.Marshal(),
762 },
763 {
764 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800765 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800766 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
767 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
768 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
769 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
770 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
771 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
772 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
773 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
774 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
775 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
776 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
777 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
778 MapBoolBool: map[bool]bool{true: false, false: true},
779 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
780 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
781 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700782 "71.1.key": {A: proto.Int32(1171)},
783 "71.2.key": {A: proto.Int32(2171)},
Damien Neilba23aa52018-12-07 14:38:17 -0800784 },
785 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
786 "73.1.key": testpb.TestAllTypes_FOO,
787 "73.2.key": testpb.TestAllTypes_BAR,
788 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700789 }, &test3pb.TestAllTypes{
790 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
791 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
792 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
793 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
794 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
795 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
796 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
797 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
798 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
799 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
800 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
801 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
802 MapBoolBool: map[bool]bool{true: false, false: true},
803 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
804 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
805 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
806 "71.1.key": {A: 1171},
807 "71.2.key": {A: 2171},
808 },
809 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
810 "73.1.key": test3pb.TestAllTypes_FOO,
811 "73.2.key": test3pb.TestAllTypes_BAR,
812 },
Damien Neilba23aa52018-12-07 14:38:17 -0800813 }},
814 wire: pack.Message{
815 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
816 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
817 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
818 }),
819 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
820 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
821 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
822 }),
823 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
824 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
825 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
826 }),
827 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
828 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
829 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
830 }),
831 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
832 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
833 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
834 }),
835 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
836 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
837 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
838 }),
839 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
840 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
841 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
842 }),
843 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
844 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
845 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
846 }),
847 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
848 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
849 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
850 }),
851 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
852 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
853 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
854 }),
855 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
856 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
857 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
858 }),
859 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
860 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
861 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
862 }),
863 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
864 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
865 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
866 }),
867 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
868 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
869 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
870 }),
871 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
872 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
873 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
874 }),
875 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
876 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
877 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
878 }),
879 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
880 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
881 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
882 }),
883 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
884 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
885 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
886 }),
887 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
888 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
889 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
890 }),
891 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
892 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
893 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
894 }),
895 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
896 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
897 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
898 }),
899 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
900 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
901 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
902 }),
903 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
904 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
905 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
906 }),
907 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
908 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
909 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
910 }),
911 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
912 pack.Tag{1, pack.VarintType}, pack.Bool(true),
913 pack.Tag{2, pack.VarintType}, pack.Bool(false),
914 }),
915 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
916 pack.Tag{1, pack.VarintType}, pack.Bool(false),
917 pack.Tag{2, pack.VarintType}, pack.Bool(true),
918 }),
919 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
920 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
921 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
922 }),
923 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
924 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
925 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
926 }),
927 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
928 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
929 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
930 }),
931 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
932 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
933 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
934 }),
935 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
936 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
937 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
938 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
939 }),
940 }),
941 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
942 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
943 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
944 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
945 }),
946 }),
947 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
948 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
949 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
950 }),
951 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
952 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
953 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
954 }),
955 }.Marshal(),
956 },
957 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700958 desc: "oneof (uint32)",
959 decodeTo: []proto.Message{
960 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
961 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
962 },
963 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800964 },
965 {
966 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700967 decodeTo: []proto.Message{
968 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -0700969 &testpb.TestAllTypes_NestedMessage{A: proto.Int32(1112)},
Damien Neil3b46ade2019-03-26 13:55:02 -0700970 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
971 &test3pb.TestAllTypes_NestedMessage{A: 1112},
972 }},
973 },
Damien Neilba23aa52018-12-07 14:38:17 -0800974 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
975 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
976 })}.Marshal(),
977 },
978 {
Damien Neilc37adef2019-04-01 13:49:56 -0700979 desc: "oneof (empty message)",
980 decodeTo: []proto.Message{
981 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
982 &testpb.TestAllTypes_NestedMessage{},
983 }},
984 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
985 &test3pb.TestAllTypes_NestedMessage{},
986 }},
987 },
988 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
989 },
990 {
Joe Tsai6c286742019-07-11 23:15:05 -0700991 desc: "oneof (merged message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700992 decodeTo: []proto.Message{
993 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
994 &testpb.TestAllTypes_NestedMessage{
Joe Tsai0f81b382019-07-10 23:14:31 -0700995 A: proto.Int32(1),
Damien Neil3b46ade2019-03-26 13:55:02 -0700996 Corecursive: &testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -0700997 OptionalInt32: proto.Int32(43),
Damien Neil3b46ade2019-03-26 13:55:02 -0700998 },
Damien Neilba23aa52018-12-07 14:38:17 -0800999 },
Damien Neil3b46ade2019-03-26 13:55:02 -07001000 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
1001 &test3pb.TestAllTypes_NestedMessage{
Joe Tsai6c286742019-07-11 23:15:05 -07001002 A: 1,
Damien Neil3b46ade2019-03-26 13:55:02 -07001003 Corecursive: &test3pb.TestAllTypes{
1004 OptionalInt32: 43,
1005 },
1006 },
1007 }}},
Damien Neilba23aa52018-12-07 14:38:17 -08001008 wire: pack.Message{
1009 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
1010 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
1011 }),
1012 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
1013 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1014 pack.Tag{1, pack.VarintType}, pack.Varint(43),
1015 }),
1016 }),
1017 }.Marshal(),
1018 },
1019 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001020 desc: "oneof (string)",
1021 decodeTo: []proto.Message{
1022 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
1023 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
1024 },
1025 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001026 },
1027 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001028 desc: "oneof (bytes)",
1029 decodeTo: []proto.Message{
1030 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
1031 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
1032 },
1033 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001034 },
1035 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001036 desc: "oneof (bool)",
1037 decodeTo: []proto.Message{
1038 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
1039 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
1040 },
1041 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001042 },
1043 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001044 desc: "oneof (uint64)",
1045 decodeTo: []proto.Message{
1046 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
1047 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
1048 },
1049 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001050 },
1051 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001052 desc: "oneof (float)",
1053 decodeTo: []proto.Message{
1054 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
1055 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
1056 },
1057 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001058 },
1059 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001060 desc: "oneof (double)",
1061 decodeTo: []proto.Message{
1062 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
1063 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
1064 },
1065 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001066 },
1067 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001068 desc: "oneof (enum)",
1069 decodeTo: []proto.Message{
1070 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
1071 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
1072 },
1073 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -08001074 },
1075 {
Damien Neilc37adef2019-04-01 13:49:56 -07001076 desc: "oneof (zero)",
1077 decodeTo: []proto.Message{
1078 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}},
1079 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}},
1080 },
1081 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
1082 },
1083 {
Damien Neil3b46ade2019-03-26 13:55:02 -07001084 desc: "oneof (overridden value)",
1085 decodeTo: []proto.Message{
1086 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
1087 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
1088 },
Damien Neilba23aa52018-12-07 14:38:17 -08001089 wire: pack.Message{
1090 pack.Tag{111, pack.VarintType}, pack.Varint(1),
1091 pack.Tag{116, pack.VarintType}, pack.Varint(2),
1092 }.Marshal(),
1093 },
1094 // TODO: More unknown field tests for ordering, repeated fields, etc.
1095 //
1096 // It is currently impossible to produce results that the v1 Equal
1097 // considers equivalent to those of the v1 decoder. Figure out if
1098 // that's a problem or not.
1099 {
1100 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -08001101 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001102 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001103 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001104 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1105 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001106 ), build(
1107 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001108 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001109 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1110 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001111 )},
1112 wire: pack.Message{
1113 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
1114 }.Marshal(),
1115 },
1116 {
1117 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001118 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -08001119 &testpb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001120 unknown(pack.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001121 pack.Tag{1, pack.BytesType}, pack.String("string"),
1122 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -07001123 ), build(
1124 &test3pb.TestAllTypes{},
Joe Tsai378c1322019-04-25 23:48:08 -07001125 unknown(pack.Message{
Damien Neil3b46ade2019-03-26 13:55:02 -07001126 pack.Tag{1, pack.BytesType}, pack.String("string"),
1127 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -08001128 )},
1129 wire: pack.Message{
1130 pack.Tag{1, pack.BytesType}, pack.String("string"),
1131 }.Marshal(),
1132 },
1133 {
1134 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -08001135 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -08001136 &testpb.TestAllTypes{
1137 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -07001138 }, &test3pb.TestAllTypes{
1139 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -08001140 },
1141 },
1142 wire: pack.Message{
1143 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
1144 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1145 pack.Tag{2, pack.BytesType}, pack.String("string"),
1146 }),
1147 }.Marshal(),
1148 },
Damien Neil96c229a2019-04-03 12:17:24 -07001149 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001150 desc: "required field in nil message unset",
1151 partial: true,
1152 decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
1153 },
1154 {
Damien Neil96c229a2019-04-03 12:17:24 -07001155 desc: "required field unset",
1156 partial: true,
1157 decodeTo: []proto.Message{&testpb.TestRequired{}},
1158 },
1159 {
1160 desc: "required field set",
1161 decodeTo: []proto.Message{&testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001162 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001163 }},
1164 wire: pack.Message{
1165 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1166 }.Marshal(),
1167 },
1168 {
1169 desc: "required field in optional message unset",
1170 partial: true,
1171 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1172 OptionalMessage: &testpb.TestRequired{},
1173 }},
1174 wire: pack.Message{
1175 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1176 }.Marshal(),
1177 },
1178 {
1179 desc: "required field in optional message set",
1180 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1181 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001182 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001183 },
1184 }},
1185 wire: pack.Message{
1186 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1187 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1188 }),
1189 }.Marshal(),
1190 },
Damien Neil4686e232019-04-05 13:31:40 -07001191 {
1192 desc: "required field in optional message set (split across multiple tags)",
1193 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1194 OptionalMessage: &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001195 RequiredField: proto.Int32(1),
Damien Neil4686e232019-04-05 13:31:40 -07001196 },
1197 }},
1198 wire: pack.Message{
1199 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1200 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1201 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1202 }),
1203 }.Marshal(),
1204 },
Damien Neil96c229a2019-04-03 12:17:24 -07001205 {
1206 desc: "required field in repeated message unset",
1207 partial: true,
1208 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1209 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001210 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001211 {},
1212 },
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 }.Marshal(),
1220 },
1221 {
1222 desc: "required field in repeated message set",
1223 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1224 RepeatedMessage: []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001225 {RequiredField: proto.Int32(1)},
1226 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001227 },
1228 }},
1229 wire: pack.Message{
1230 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1231 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1232 }),
1233 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1234 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1235 }),
1236 }.Marshal(),
1237 },
1238 {
1239 desc: "required field in map message unset",
1240 partial: true,
1241 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1242 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001243 1: {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001244 2: {},
1245 },
1246 }},
1247 wire: pack.Message{
1248 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1249 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1250 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1251 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1252 }),
1253 }),
1254 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1255 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1256 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1257 }),
1258 }.Marshal(),
1259 },
1260 {
1261 desc: "required field in map message set",
1262 decodeTo: []proto.Message{&testpb.TestRequiredForeign{
1263 MapMessage: map[int32]*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001264 1: {RequiredField: proto.Int32(1)},
1265 2: {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001266 },
1267 }},
1268 wire: pack.Message{
1269 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1270 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1271 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1272 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1273 }),
1274 }),
1275 pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
1276 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1277 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1278 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1279 }),
1280 }),
1281 }.Marshal(),
1282 },
1283 {
1284 desc: "required field in optional group unset",
1285 partial: true,
1286 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1287 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
1288 }},
1289 wire: pack.Message{
1290 pack.Tag{1, pack.StartGroupType},
1291 pack.Tag{1, pack.EndGroupType},
1292 }.Marshal(),
1293 },
1294 {
1295 desc: "required field in optional group set",
1296 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1297 Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001298 A: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001299 },
1300 }},
1301 wire: pack.Message{
1302 pack.Tag{1, pack.StartGroupType},
1303 pack.Tag{2, pack.VarintType}, pack.Varint(1),
1304 pack.Tag{1, pack.EndGroupType},
1305 }.Marshal(),
1306 },
1307 {
1308 desc: "required field in repeated group unset",
1309 partial: true,
1310 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1311 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001312 {A: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001313 {},
1314 },
1315 }},
1316 wire: pack.Message{
1317 pack.Tag{3, pack.StartGroupType},
1318 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1319 pack.Tag{3, pack.EndGroupType},
1320 pack.Tag{3, pack.StartGroupType},
1321 pack.Tag{3, pack.EndGroupType},
1322 }.Marshal(),
1323 },
1324 {
1325 desc: "required field in repeated group set",
1326 decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
1327 Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001328 {A: proto.Int32(1)},
1329 {A: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001330 },
1331 }},
1332 wire: pack.Message{
1333 pack.Tag{3, pack.StartGroupType},
1334 pack.Tag{4, pack.VarintType}, pack.Varint(1),
1335 pack.Tag{3, pack.EndGroupType},
1336 pack.Tag{3, pack.StartGroupType},
1337 pack.Tag{4, pack.VarintType}, pack.Varint(2),
1338 pack.Tag{3, pack.EndGroupType},
1339 }.Marshal(),
1340 },
1341 {
Damien Neil5322bdb2019-04-09 15:57:05 -07001342 desc: "required field in oneof message unset",
1343 partial: true,
1344 decodeTo: []proto.Message{
1345 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1346 &testpb.TestRequired{},
1347 }},
1348 },
1349 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
1350 },
1351 {
1352 desc: "required field in oneof message set",
1353 decodeTo: []proto.Message{
1354 &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
1355 &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001356 RequiredField: proto.Int32(1),
Damien Neil5322bdb2019-04-09 15:57:05 -07001357 },
1358 }},
1359 },
1360 wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
1361 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1362 })}.Marshal(),
1363 },
1364 {
Joe Tsai09cef322019-07-11 22:13:49 -07001365 desc: "required field in extension message unset",
1366 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001367 decodeTo: []proto.Message{build(
1368 &testpb.TestAllExtensions{},
1369 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
1370 )},
1371 wire: pack.Message{
1372 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1373 }.Marshal(),
1374 },
1375 {
1376 desc: "required field in extension message set",
1377 decodeTo: []proto.Message{build(
1378 &testpb.TestAllExtensions{},
1379 extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001380 RequiredField: proto.Int32(1),
Damien Neil96c229a2019-04-03 12:17:24 -07001381 }),
1382 )},
1383 wire: pack.Message{
1384 pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
1385 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1386 }),
1387 }.Marshal(),
1388 },
1389 {
Joe Tsai09cef322019-07-11 22:13:49 -07001390 desc: "required field in repeated extension message unset",
1391 partial: true,
Damien Neil96c229a2019-04-03 12:17:24 -07001392 decodeTo: []proto.Message{build(
1393 &testpb.TestAllExtensions{},
1394 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001395 {RequiredField: proto.Int32(1)},
Damien Neil96c229a2019-04-03 12:17:24 -07001396 {},
1397 }),
1398 )},
1399 wire: pack.Message{
1400 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1401 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1402 }),
1403 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
1404 }.Marshal(),
1405 },
1406 {
1407 desc: "required field in repeated extension message set",
1408 decodeTo: []proto.Message{build(
1409 &testpb.TestAllExtensions{},
1410 extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
Damien Neila8a2cea2019-07-10 16:17:16 -07001411 {RequiredField: proto.Int32(1)},
1412 {RequiredField: proto.Int32(2)},
Damien Neil96c229a2019-04-03 12:17:24 -07001413 }),
1414 )},
1415 wire: pack.Message{
1416 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1417 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1418 }),
1419 pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
1420 pack.Tag{1, pack.VarintType}, pack.Varint(2),
1421 }),
1422 }.Marshal(),
1423 },
Damien Neilc37adef2019-04-01 13:49:56 -07001424 {
Damien Neil3d0706a2019-07-09 11:40:49 -07001425 desc: "nil messages",
1426 decodeTo: []proto.Message{
1427 (*testpb.TestAllTypes)(nil),
1428 (*test3pb.TestAllTypes)(nil),
1429 (*testpb.TestAllExtensions)(nil),
1430 },
1431 },
1432 {
Damien Neilc37adef2019-04-01 13:49:56 -07001433 desc: "legacy",
1434 partial: true,
1435 decodeTo: []proto.Message{
1436 &legacypb.Legacy{
1437 F1: &legacy1pb.Message{
Damien Neila8a2cea2019-07-10 16:17:16 -07001438 OptionalInt32: proto.Int32(1),
Damien Neilc37adef2019-04-01 13:49:56 -07001439 OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
1440 OptionalChildMessage: &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001441 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001442 },
1443 Optionalgroup: &legacy1pb.Message_OptionalGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001444 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001445 },
1446 RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001447 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001448 },
1449 Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
Damien Neila8a2cea2019-07-10 16:17:16 -07001450 {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001451 },
1452 MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001453 true: {F1: proto.String("x")},
Damien Neilc37adef2019-04-01 13:49:56 -07001454 },
1455 OneofUnion: &legacy1pb.Message_OneofChildMessage{
1456 &legacy1pb.Message_ChildMessage{
Damien Neila8a2cea2019-07-10 16:17:16 -07001457 F1: proto.String("x"),
Damien Neilc37adef2019-04-01 13:49:56 -07001458 },
1459 },
1460 },
1461 },
1462 },
1463 wire: pack.Message{
1464 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1465 pack.Tag{101, pack.VarintType}, pack.Varint(1),
1466 pack.Tag{115, pack.VarintType}, pack.Varint(0),
1467 pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
1468 pack.Tag{1, pack.BytesType}, pack.String("x"),
1469 }),
1470 pack.Tag{120, pack.StartGroupType},
1471 pack.Tag{1, pack.BytesType}, pack.String("x"),
1472 pack.Tag{120, pack.EndGroupType},
1473 pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
1474 pack.Tag{1, pack.BytesType}, pack.String("x"),
1475 }),
1476 pack.Tag{520, pack.StartGroupType},
1477 pack.Tag{1, pack.BytesType}, pack.String("x"),
1478 pack.Tag{520, pack.EndGroupType},
1479 pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
1480 pack.Tag{1, pack.VarintType}, pack.Varint(1),
1481 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1482 pack.Tag{1, pack.BytesType}, pack.String("x"),
1483 }),
1484 }),
1485 pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
1486 pack.Tag{1, pack.BytesType}, pack.String("x"),
1487 }),
1488 }),
1489 }.Marshal(),
1490 },
Damien Neilba23aa52018-12-07 14:38:17 -08001491}
1492
Damien Neilbc310b52019-04-11 11:46:55 -07001493var invalidUTF8TestProtos = []testProto{
1494 {
1495 desc: "invalid UTF-8 in optional string field",
1496 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1497 OptionalString: "abc\xff",
1498 }},
1499 wire: pack.Message{
1500 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1501 }.Marshal(),
1502 },
1503 {
1504 desc: "invalid UTF-8 in repeated string field",
1505 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1506 RepeatedString: []string{"foo", "abc\xff"},
1507 }},
1508 wire: pack.Message{
1509 pack.Tag{44, pack.BytesType}, pack.String("foo"),
1510 pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
1511 }.Marshal(),
1512 },
1513 {
1514 desc: "invalid UTF-8 in nested message",
1515 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1516 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
1517 Corecursive: &test3pb.TestAllTypes{
1518 OptionalString: "abc\xff",
1519 },
1520 },
1521 }},
1522 wire: pack.Message{
1523 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
1524 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1525 pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
1526 }),
1527 }),
1528 }.Marshal(),
1529 },
1530 {
Damien Neilc37adef2019-04-01 13:49:56 -07001531 desc: "invalid UTF-8 in oneof field",
1532 decodeTo: []proto.Message{
1533 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
1534 },
1535 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1536 },
1537 {
Damien Neilbc310b52019-04-11 11:46:55 -07001538 desc: "invalid UTF-8 in map key",
1539 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1540 MapStringString: map[string]string{"key\xff": "val"},
1541 }},
1542 wire: pack.Message{
1543 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1544 pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
1545 pack.Tag{2, pack.BytesType}, pack.String("val"),
1546 }),
1547 }.Marshal(),
1548 },
1549 {
1550 desc: "invalid UTF-8 in map value",
1551 decodeTo: []proto.Message{&test3pb.TestAllTypes{
1552 MapStringString: map[string]string{"key": "val\xff"},
1553 }},
1554 wire: pack.Message{
1555 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
1556 pack.Tag{1, pack.BytesType}, pack.String("key"),
1557 pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
1558 }),
1559 }.Marshal(),
1560 },
1561}
1562
Joe Tsaic51e2e02019-07-13 00:44:41 -07001563var noEnforceUTF8TestProtos = []testProto{
1564 {
1565 desc: "invalid UTF-8 in optional string field",
1566 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1567 OptionalString: string("abc\xff"),
1568 }},
1569 wire: pack.Message{
1570 pack.Tag{1, pack.BytesType}, pack.String("abc\xff"),
1571 }.Marshal(),
1572 },
1573 {
1574 desc: "invalid UTF-8 in optional string field of Go bytes",
1575 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1576 OptionalBytes: []byte("abc\xff"),
1577 }},
1578 wire: pack.Message{
1579 pack.Tag{2, pack.BytesType}, pack.String("abc\xff"),
1580 }.Marshal(),
1581 },
1582 {
1583 desc: "invalid UTF-8 in repeated string field",
1584 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1585 RepeatedString: []string{string("foo"), string("abc\xff")},
1586 }},
1587 wire: pack.Message{
1588 pack.Tag{3, pack.BytesType}, pack.String("foo"),
1589 pack.Tag{3, pack.BytesType}, pack.String("abc\xff"),
1590 }.Marshal(),
1591 },
1592 {
1593 desc: "invalid UTF-8 in repeated string field of Go bytes",
1594 decodeTo: []proto.Message{&TestNoEnforceUTF8{
1595 RepeatedBytes: [][]byte{[]byte("foo"), []byte("abc\xff")},
1596 }},
1597 wire: pack.Message{
1598 pack.Tag{4, pack.BytesType}, pack.String("foo"),
1599 pack.Tag{4, pack.BytesType}, pack.String("abc\xff"),
1600 }.Marshal(),
1601 },
1602 {
1603 desc: "invalid UTF-8 in oneof string field",
1604 decodeTo: []proto.Message{
1605 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofString{string("abc\xff")}},
1606 },
1607 wire: pack.Message{pack.Tag{5, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1608 },
1609 {
1610 desc: "invalid UTF-8 in oneof string field of Go bytes",
1611 decodeTo: []proto.Message{
1612 &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofBytes{[]byte("abc\xff")}},
1613 },
1614 wire: pack.Message{pack.Tag{6, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
1615 },
1616}
1617
1618type TestNoEnforceUTF8 struct {
1619 OptionalString string `protobuf:"bytes,1,opt,name=optional_string"`
1620 OptionalBytes []byte `protobuf:"bytes,2,opt,name=optional_bytes"`
1621 RepeatedString []string `protobuf:"bytes,3,rep,name=repeated_string"`
1622 RepeatedBytes [][]byte `protobuf:"bytes,4,rep,name=repeated_bytes"`
1623 OneofField isOneofField `protobuf_oneof:"oneof_field"`
1624}
1625
1626type isOneofField interface{ isOneofField() }
1627
1628type TestNoEnforceUTF8_OneofString struct {
1629 OneofString string `protobuf:"bytes,5,opt,name=oneof_string,oneof"`
1630}
1631type TestNoEnforceUTF8_OneofBytes struct {
1632 OneofBytes []byte `protobuf:"bytes,6,opt,name=oneof_bytes,oneof"`
1633}
1634
1635func (*TestNoEnforceUTF8_OneofString) isOneofField() {}
1636func (*TestNoEnforceUTF8_OneofBytes) isOneofField() {}
1637
Joe Tsai74615a32019-07-14 18:51:46 -07001638func (m *TestNoEnforceUTF8) ProtoReflect() protoreflect.Message {
Joe Tsaic51e2e02019-07-13 00:44:41 -07001639 return messageInfo_TestNoEnforceUTF8.MessageOf(m)
1640}
1641
1642var messageInfo_TestNoEnforceUTF8 = protoimpl.MessageInfo{
Damien Neil16163b42019-08-06 15:43:25 -07001643 GoReflectType: reflect.TypeOf((*TestNoEnforceUTF8)(nil)),
1644 Desc: func() protoreflect.MessageDescriptor {
1645 pb := new(descriptorpb.FileDescriptorProto)
1646 if err := prototext.Unmarshal([]byte(`
Joe Tsaic51e2e02019-07-13 00:44:41 -07001647 syntax: "proto3"
1648 name: "test.proto"
1649 message_type: [{
1650 name: "TestNoEnforceUTF8"
1651 field: [
1652 {name:"optional_string" number:1 label:LABEL_OPTIONAL type:TYPE_STRING},
1653 {name:"optional_bytes" number:2 label:LABEL_OPTIONAL type:TYPE_STRING},
1654 {name:"repeated_string" number:3 label:LABEL_REPEATED type:TYPE_STRING},
1655 {name:"repeated_bytes" number:4 label:LABEL_REPEATED type:TYPE_STRING},
1656 {name:"oneof_string" number:5 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0},
1657 {name:"oneof_bytes" number:6 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0}
1658 ]
1659 oneof_decl: [{name:"oneof_field"}]
1660 }]
1661 `), pb); err != nil {
Damien Neil16163b42019-08-06 15:43:25 -07001662 panic(err)
1663 }
1664 fd, err := protodesc.NewFile(pb, nil)
1665 if err != nil {
1666 panic(err)
1667 }
1668 md := fd.Messages().Get(0)
1669 for i := 0; i < md.Fields().Len(); i++ {
1670 md.Fields().Get(i).(*filedesc.Field).L1.HasEnforceUTF8 = true
1671 md.Fields().Get(i).(*filedesc.Field).L1.EnforceUTF8 = false
1672 }
1673 return md
1674 }(),
Joe Tsaic51e2e02019-07-13 00:44:41 -07001675 OneofWrappers: []interface{}{
1676 (*TestNoEnforceUTF8_OneofString)(nil),
1677 (*TestNoEnforceUTF8_OneofBytes)(nil),
1678 },
1679}
1680
Tuo Shan6e25d8c2019-08-22 18:52:43 -07001681var invalidFieldNumberTestProtos = []struct {
1682 desc string
1683 wire []byte
1684 allowed bool
1685}{
1686 {
1687 desc: "zero",
1688 wire: pack.Message{
1689 pack.Tag{pack.MinValidNumber - 1, pack.VarintType}, pack.Varint(1001),
1690 }.Marshal(),
1691 },
1692 {
1693 desc: "zero and one",
1694 wire: pack.Message{
1695 pack.Tag{pack.MinValidNumber - 1, pack.VarintType}, pack.Varint(1002),
1696 pack.Tag{pack.MinValidNumber, pack.VarintType}, pack.Varint(1003),
1697 }.Marshal(),
1698 },
1699 {
1700 desc: "first reserved",
1701 wire: pack.Message{
1702 pack.Tag{pack.FirstReservedNumber, pack.VarintType}, pack.Varint(1004),
1703 }.Marshal(),
1704 allowed: true,
1705 },
1706 {
1707 desc: "last reserved",
1708 wire: pack.Message{
1709 pack.Tag{pack.LastReservedNumber, pack.VarintType}, pack.Varint(1005),
1710 }.Marshal(),
1711 allowed: true,
1712 },
1713 {
1714 desc: "max and max+1",
1715 wire: pack.Message{
1716 pack.Tag{pack.MaxValidNumber, pack.VarintType}, pack.Varint(1006),
1717 pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(1007),
1718 }.Marshal(),
1719 allowed: flags.ProtoLegacy,
1720 },
1721 {
1722 desc: "max+1",
1723 wire: pack.Message{
1724 pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(1008),
1725 }.Marshal(),
1726 allowed: flags.ProtoLegacy,
1727 },
1728}
1729
Joe Tsai6e095992019-08-10 13:56:36 -07001730func TestWeak(t *testing.T) {
1731 if !flags.ProtoLegacy {
1732 t.SkipNow()
1733 }
1734
1735 m := new(testpb.TestWeak)
1736 b := pack.Message{
1737 pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
1738 pack.Tag{1, pack.VarintType}, pack.Varint(1000),
1739 }),
1740 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
1741 pack.Tag{1, pack.VarintType}, pack.Varint(2000),
1742 }),
1743 }.Marshal()
1744 if err := proto.Unmarshal(b, m); err != nil {
1745 t.Errorf("Unmarshal error: %v", err)
1746 }
1747
1748 mw := m.GetWeakMessage1().(*weakpb.WeakImportMessage1)
1749 if mw.GetA() != 1000 {
1750 t.Errorf("m.WeakMessage1.a = %d, want %d", mw.GetA(), 1000)
1751 }
1752
1753 if len(m.ProtoReflect().GetUnknown()) == 0 {
1754 t.Errorf("m has no unknown fields, expected at least something")
1755 }
1756
1757 if n := proto.Size(m); n != len(b) {
1758 t.Errorf("Size() = %d, want %d", n, len(b))
1759 }
1760
1761 b2, err := proto.Marshal(m)
1762 if err != nil {
1763 t.Errorf("Marshal error: %v", err)
1764 }
1765 if len(b2) != len(b) {
1766 t.Errorf("len(Marshal) = %d, want %d", len(b2), len(b))
1767 }
1768}
1769
Damien Neil4be2fb42018-12-17 11:16:16 -08001770func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -08001771 for _, opt := range opts {
1772 opt(m)
1773 }
1774 return m
1775}
1776
Damien Neil4be2fb42018-12-17 11:16:16 -08001777type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -08001778
Joe Tsai74615a32019-07-14 18:51:46 -07001779func unknown(raw protoreflect.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001780 return func(m proto.Message) {
Joe Tsai378c1322019-04-25 23:48:08 -07001781 m.ProtoReflect().SetUnknown(raw)
Damien Neile6f060f2019-04-23 17:11:02 -07001782 }
1783}
1784
Damien Neilf1e905b2019-08-08 15:45:59 -07001785func extend(desc protoreflect.ExtensionType, value interface{}) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -08001786 return func(m proto.Message) {
Damien Neil92f76182019-08-02 16:58:08 -07001787 proto.SetExtension(m, desc, value)
Damien Neilba23aa52018-12-07 14:38:17 -08001788 }
1789}
Damien Neil61e93c72019-03-27 09:23:20 -07001790
1791func marshalText(m proto.Message) string {
Joe Tsaif2c4ddc2019-09-19 21:28:52 -07001792 if m == nil {
1793 return "<nil>\n"
1794 }
Joe Tsaicd4a31e2019-09-14 19:14:24 -07001795 b, _ := prototext.MarshalOptions{
1796 AllowPartial: true,
1797 EmitUnknown: true,
1798 Indent: "\t",
1799 }.Marshal(m)
Damien Neil61e93c72019-03-27 09:23:20 -07001800 return string(b)
1801}