Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1 | // 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 Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 5 | package proto_test |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "reflect" |
| 10 | "testing" |
| 11 | |
| 12 | protoV1 "github.com/golang/protobuf/proto" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame^] | 13 | "google.golang.org/protobuf/encoding/textpb" |
| 14 | "google.golang.org/protobuf/internal/encoding/pack" |
| 15 | "google.golang.org/protobuf/internal/errors" |
| 16 | "google.golang.org/protobuf/internal/scalar" |
| 17 | "google.golang.org/protobuf/proto" |
| 18 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 19 | "google.golang.org/protobuf/runtime/protolegacy" |
Joe Tsai | 1905843 | 2019-02-27 21:46:29 -0800 | [diff] [blame] | 20 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame^] | 21 | testpb "google.golang.org/protobuf/internal/testprotos/test" |
| 22 | test3pb "google.golang.org/protobuf/internal/testprotos/test3" |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | type testProto struct { |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 26 | desc string |
| 27 | decodeTo []proto.Message |
| 28 | wire []byte |
| 29 | partial bool |
| 30 | invalidExtensions bool |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | func TestDecode(t *testing.T) { |
| 34 | for _, test := range testProtos { |
| 35 | for _, want := range test.decodeTo { |
| 36 | t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) { |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 37 | opts := proto.UnmarshalOptions{ |
| 38 | AllowPartial: test.partial, |
| 39 | } |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 40 | wire := append(([]byte)(nil), test.wire...) |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 41 | got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message) |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 42 | if err := opts.Unmarshal(wire, got); err != nil { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 43 | t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want)) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 44 | return |
| 45 | } |
| 46 | |
| 47 | // Aliasing check: Modifying the original wire bytes shouldn't |
| 48 | // affect the unmarshaled message. |
| 49 | for i := range wire { |
| 50 | wire[i] = 0 |
| 51 | } |
| 52 | |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 53 | if test.invalidExtensions { |
| 54 | // Equal doesn't work on messages containing invalid extension data. |
| 55 | return |
| 56 | } |
Joe Tsai | db38ddd | 2019-05-07 15:14:40 -0700 | [diff] [blame] | 57 | if !proto.Equal(got, want) { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 58 | t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want)) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 59 | } |
| 60 | }) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 65 | func TestDecodeRequiredFieldChecks(t *testing.T) { |
| 66 | for _, test := range testProtos { |
| 67 | if !test.partial { |
| 68 | continue |
| 69 | } |
| 70 | if test.invalidExtensions { |
| 71 | // Missing required fields in extensions just end up in the unknown fields. |
| 72 | continue |
| 73 | } |
| 74 | for _, m := range test.decodeTo { |
| 75 | t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) { |
| 76 | got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message) |
| 77 | if err := proto.Unmarshal(test.wire, got); err == nil { |
| 78 | t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got)) |
| 79 | } |
| 80 | }) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
Damien Neil | bc310b5 | 2019-04-11 11:46:55 -0700 | [diff] [blame] | 85 | func TestDecodeInvalidUTF8(t *testing.T) { |
| 86 | for _, test := range invalidUTF8TestProtos { |
| 87 | for _, want := range test.decodeTo { |
| 88 | t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) { |
| 89 | got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message) |
| 90 | err := proto.Unmarshal(test.wire, got) |
| 91 | if !isErrInvalidUTF8(err) { |
| 92 | t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want)) |
| 93 | } |
| 94 | if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) { |
| 95 | t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want)) |
| 96 | } |
| 97 | }) |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 102 | var testProtos = []testProto{ |
| 103 | { |
| 104 | desc: "basic scalar types", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 105 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 106 | OptionalInt32: scalar.Int32(1001), |
| 107 | OptionalInt64: scalar.Int64(1002), |
| 108 | OptionalUint32: scalar.Uint32(1003), |
| 109 | OptionalUint64: scalar.Uint64(1004), |
| 110 | OptionalSint32: scalar.Int32(1005), |
| 111 | OptionalSint64: scalar.Int64(1006), |
| 112 | OptionalFixed32: scalar.Uint32(1007), |
| 113 | OptionalFixed64: scalar.Uint64(1008), |
| 114 | OptionalSfixed32: scalar.Int32(1009), |
| 115 | OptionalSfixed64: scalar.Int64(1010), |
| 116 | OptionalFloat: scalar.Float32(1011.5), |
| 117 | OptionalDouble: scalar.Float64(1012.5), |
| 118 | OptionalBool: scalar.Bool(true), |
| 119 | OptionalString: scalar.String("string"), |
| 120 | OptionalBytes: []byte("bytes"), |
| 121 | OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(), |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 122 | }, &test3pb.TestAllTypes{ |
| 123 | OptionalInt32: 1001, |
| 124 | OptionalInt64: 1002, |
| 125 | OptionalUint32: 1003, |
| 126 | OptionalUint64: 1004, |
| 127 | OptionalSint32: 1005, |
| 128 | OptionalSint64: 1006, |
| 129 | OptionalFixed32: 1007, |
| 130 | OptionalFixed64: 1008, |
| 131 | OptionalSfixed32: 1009, |
| 132 | OptionalSfixed64: 1010, |
| 133 | OptionalFloat: 1011.5, |
| 134 | OptionalDouble: 1012.5, |
| 135 | OptionalBool: true, |
| 136 | OptionalString: "string", |
| 137 | OptionalBytes: []byte("bytes"), |
| 138 | OptionalNestedEnum: test3pb.TestAllTypes_BAR, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 139 | }, build( |
| 140 | &testpb.TestAllExtensions{}, |
| 141 | extend(testpb.E_OptionalInt32Extension, scalar.Int32(1001)), |
| 142 | extend(testpb.E_OptionalInt64Extension, scalar.Int64(1002)), |
| 143 | extend(testpb.E_OptionalUint32Extension, scalar.Uint32(1003)), |
| 144 | extend(testpb.E_OptionalUint64Extension, scalar.Uint64(1004)), |
| 145 | extend(testpb.E_OptionalSint32Extension, scalar.Int32(1005)), |
| 146 | extend(testpb.E_OptionalSint64Extension, scalar.Int64(1006)), |
| 147 | extend(testpb.E_OptionalFixed32Extension, scalar.Uint32(1007)), |
| 148 | extend(testpb.E_OptionalFixed64Extension, scalar.Uint64(1008)), |
| 149 | extend(testpb.E_OptionalSfixed32Extension, scalar.Int32(1009)), |
| 150 | extend(testpb.E_OptionalSfixed64Extension, scalar.Int64(1010)), |
| 151 | extend(testpb.E_OptionalFloatExtension, scalar.Float32(1011.5)), |
| 152 | extend(testpb.E_OptionalDoubleExtension, scalar.Float64(1012.5)), |
| 153 | extend(testpb.E_OptionalBoolExtension, scalar.Bool(true)), |
| 154 | extend(testpb.E_OptionalStringExtension, scalar.String("string")), |
| 155 | extend(testpb.E_OptionalBytesExtension, []byte("bytes")), |
| 156 | extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR.Enum()), |
| 157 | )}, |
| 158 | wire: pack.Message{ |
| 159 | pack.Tag{1, pack.VarintType}, pack.Varint(1001), |
| 160 | pack.Tag{2, pack.VarintType}, pack.Varint(1002), |
| 161 | pack.Tag{3, pack.VarintType}, pack.Uvarint(1003), |
| 162 | pack.Tag{4, pack.VarintType}, pack.Uvarint(1004), |
| 163 | pack.Tag{5, pack.VarintType}, pack.Svarint(1005), |
| 164 | pack.Tag{6, pack.VarintType}, pack.Svarint(1006), |
| 165 | pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007), |
| 166 | pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008), |
| 167 | pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009), |
| 168 | pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010), |
| 169 | pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5), |
| 170 | pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5), |
| 171 | pack.Tag{13, pack.VarintType}, pack.Bool(true), |
| 172 | pack.Tag{14, pack.BytesType}, pack.String("string"), |
| 173 | pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")), |
| 174 | pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)), |
| 175 | }.Marshal(), |
| 176 | }, |
| 177 | { |
| 178 | desc: "groups", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 179 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 180 | Optionalgroup: &testpb.TestAllTypes_OptionalGroup{ |
| 181 | A: scalar.Int32(1017), |
| 182 | }, |
| 183 | }, build( |
| 184 | &testpb.TestAllExtensions{}, |
| 185 | extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{ |
| 186 | A: scalar.Int32(1017), |
| 187 | }), |
| 188 | )}, |
| 189 | wire: pack.Message{ |
| 190 | pack.Tag{16, pack.StartGroupType}, |
| 191 | pack.Tag{17, pack.VarintType}, pack.Varint(1017), |
| 192 | pack.Tag{16, pack.EndGroupType}, |
| 193 | }.Marshal(), |
| 194 | }, |
| 195 | { |
| 196 | desc: "groups (field overridden)", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 197 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 198 | Optionalgroup: &testpb.TestAllTypes_OptionalGroup{ |
| 199 | A: scalar.Int32(2), |
| 200 | }, |
| 201 | }, build( |
| 202 | &testpb.TestAllExtensions{}, |
| 203 | extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{ |
| 204 | A: scalar.Int32(2), |
| 205 | }), |
| 206 | )}, |
| 207 | wire: pack.Message{ |
| 208 | pack.Tag{16, pack.StartGroupType}, |
| 209 | pack.Tag{17, pack.VarintType}, pack.Varint(1), |
| 210 | pack.Tag{16, pack.EndGroupType}, |
| 211 | pack.Tag{16, pack.StartGroupType}, |
| 212 | pack.Tag{17, pack.VarintType}, pack.Varint(2), |
| 213 | pack.Tag{16, pack.EndGroupType}, |
| 214 | }.Marshal(), |
| 215 | }, |
| 216 | { |
| 217 | desc: "messages", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 218 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 219 | OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{ |
| 220 | A: scalar.Int32(42), |
| 221 | Corecursive: &testpb.TestAllTypes{ |
| 222 | OptionalInt32: scalar.Int32(43), |
| 223 | }, |
| 224 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 225 | }, &test3pb.TestAllTypes{ |
| 226 | OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{ |
| 227 | A: 42, |
| 228 | Corecursive: &test3pb.TestAllTypes{ |
| 229 | OptionalInt32: 43, |
| 230 | }, |
| 231 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 232 | }, build( |
| 233 | &testpb.TestAllExtensions{}, |
| 234 | extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{ |
| 235 | A: scalar.Int32(42), |
| 236 | Corecursive: &testpb.TestAllTypes{ |
| 237 | OptionalInt32: scalar.Int32(43), |
| 238 | }, |
| 239 | }), |
| 240 | )}, |
| 241 | wire: pack.Message{ |
| 242 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 243 | pack.Tag{1, pack.VarintType}, pack.Varint(42), |
| 244 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 245 | pack.Tag{1, pack.VarintType}, pack.Varint(43), |
| 246 | }), |
| 247 | }), |
| 248 | }.Marshal(), |
| 249 | }, |
| 250 | { |
| 251 | desc: "messages (split across multiple tags)", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 252 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 253 | OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{ |
| 254 | A: scalar.Int32(42), |
| 255 | Corecursive: &testpb.TestAllTypes{ |
| 256 | OptionalInt32: scalar.Int32(43), |
| 257 | }, |
| 258 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 259 | }, &test3pb.TestAllTypes{ |
| 260 | OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{ |
| 261 | A: 42, |
| 262 | Corecursive: &test3pb.TestAllTypes{ |
| 263 | OptionalInt32: 43, |
| 264 | }, |
| 265 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 266 | }, build( |
| 267 | &testpb.TestAllExtensions{}, |
| 268 | extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{ |
| 269 | A: scalar.Int32(42), |
| 270 | Corecursive: &testpb.TestAllTypes{ |
| 271 | OptionalInt32: scalar.Int32(43), |
| 272 | }, |
| 273 | }), |
| 274 | )}, |
| 275 | wire: pack.Message{ |
| 276 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 277 | pack.Tag{1, pack.VarintType}, pack.Varint(42), |
| 278 | }), |
| 279 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 280 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 281 | pack.Tag{1, pack.VarintType}, pack.Varint(43), |
| 282 | }), |
| 283 | }), |
| 284 | }.Marshal(), |
| 285 | }, |
| 286 | { |
| 287 | desc: "messages (field overridden)", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 288 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 289 | OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{ |
| 290 | A: scalar.Int32(2), |
| 291 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 292 | }, &test3pb.TestAllTypes{ |
| 293 | OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{ |
| 294 | A: 2, |
| 295 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 296 | }, build( |
| 297 | &testpb.TestAllExtensions{}, |
| 298 | extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{ |
| 299 | A: scalar.Int32(2), |
| 300 | }), |
| 301 | )}, |
| 302 | wire: pack.Message{ |
| 303 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 304 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 305 | }), |
| 306 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 307 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 308 | }), |
| 309 | }.Marshal(), |
| 310 | }, |
| 311 | { |
| 312 | desc: "basic repeated types", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 313 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 314 | RepeatedInt32: []int32{1001, 2001}, |
| 315 | RepeatedInt64: []int64{1002, 2002}, |
| 316 | RepeatedUint32: []uint32{1003, 2003}, |
| 317 | RepeatedUint64: []uint64{1004, 2004}, |
| 318 | RepeatedSint32: []int32{1005, 2005}, |
| 319 | RepeatedSint64: []int64{1006, 2006}, |
| 320 | RepeatedFixed32: []uint32{1007, 2007}, |
| 321 | RepeatedFixed64: []uint64{1008, 2008}, |
| 322 | RepeatedSfixed32: []int32{1009, 2009}, |
| 323 | RepeatedSfixed64: []int64{1010, 2010}, |
| 324 | RepeatedFloat: []float32{1011.5, 2011.5}, |
| 325 | RepeatedDouble: []float64{1012.5, 2012.5}, |
| 326 | RepeatedBool: []bool{true, false}, |
| 327 | RepeatedString: []string{"foo", "bar"}, |
| 328 | RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")}, |
| 329 | RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{ |
| 330 | testpb.TestAllTypes_FOO, |
| 331 | testpb.TestAllTypes_BAR, |
| 332 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 333 | }, &test3pb.TestAllTypes{ |
| 334 | RepeatedInt32: []int32{1001, 2001}, |
| 335 | RepeatedInt64: []int64{1002, 2002}, |
| 336 | RepeatedUint32: []uint32{1003, 2003}, |
| 337 | RepeatedUint64: []uint64{1004, 2004}, |
| 338 | RepeatedSint32: []int32{1005, 2005}, |
| 339 | RepeatedSint64: []int64{1006, 2006}, |
| 340 | RepeatedFixed32: []uint32{1007, 2007}, |
| 341 | RepeatedFixed64: []uint64{1008, 2008}, |
| 342 | RepeatedSfixed32: []int32{1009, 2009}, |
| 343 | RepeatedSfixed64: []int64{1010, 2010}, |
| 344 | RepeatedFloat: []float32{1011.5, 2011.5}, |
| 345 | RepeatedDouble: []float64{1012.5, 2012.5}, |
| 346 | RepeatedBool: []bool{true, false}, |
| 347 | RepeatedString: []string{"foo", "bar"}, |
| 348 | RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")}, |
| 349 | RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{ |
| 350 | test3pb.TestAllTypes_FOO, |
| 351 | test3pb.TestAllTypes_BAR, |
| 352 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 353 | }, build( |
| 354 | &testpb.TestAllExtensions{}, |
| 355 | extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}), |
| 356 | extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}), |
| 357 | extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}), |
| 358 | extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}), |
| 359 | extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}), |
| 360 | extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}), |
| 361 | extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}), |
| 362 | extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}), |
| 363 | extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}), |
| 364 | extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}), |
| 365 | extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}), |
| 366 | extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}), |
| 367 | extend(testpb.E_RepeatedBoolExtension, []bool{true, false}), |
| 368 | extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}), |
| 369 | extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}), |
| 370 | extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{ |
| 371 | testpb.TestAllTypes_FOO, |
| 372 | testpb.TestAllTypes_BAR, |
| 373 | }), |
| 374 | )}, |
| 375 | wire: pack.Message{ |
| 376 | pack.Tag{31, pack.VarintType}, pack.Varint(1001), |
| 377 | pack.Tag{31, pack.VarintType}, pack.Varint(2001), |
| 378 | pack.Tag{32, pack.VarintType}, pack.Varint(1002), |
| 379 | pack.Tag{32, pack.VarintType}, pack.Varint(2002), |
| 380 | pack.Tag{33, pack.VarintType}, pack.Uvarint(1003), |
| 381 | pack.Tag{33, pack.VarintType}, pack.Uvarint(2003), |
| 382 | pack.Tag{34, pack.VarintType}, pack.Uvarint(1004), |
| 383 | pack.Tag{34, pack.VarintType}, pack.Uvarint(2004), |
| 384 | pack.Tag{35, pack.VarintType}, pack.Svarint(1005), |
| 385 | pack.Tag{35, pack.VarintType}, pack.Svarint(2005), |
| 386 | pack.Tag{36, pack.VarintType}, pack.Svarint(1006), |
| 387 | pack.Tag{36, pack.VarintType}, pack.Svarint(2006), |
| 388 | pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007), |
| 389 | pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007), |
| 390 | pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008), |
| 391 | pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008), |
| 392 | pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009), |
| 393 | pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009), |
| 394 | pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010), |
| 395 | pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010), |
| 396 | pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5), |
| 397 | pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5), |
| 398 | pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5), |
| 399 | pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5), |
| 400 | pack.Tag{43, pack.VarintType}, pack.Bool(true), |
| 401 | pack.Tag{43, pack.VarintType}, pack.Bool(false), |
| 402 | pack.Tag{44, pack.BytesType}, pack.String("foo"), |
| 403 | pack.Tag{44, pack.BytesType}, pack.String("bar"), |
| 404 | pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")), |
| 405 | pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")), |
| 406 | pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)), |
| 407 | pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)), |
| 408 | }.Marshal(), |
| 409 | }, |
| 410 | { |
| 411 | desc: "basic repeated types (packed encoding)", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 412 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 413 | RepeatedInt32: []int32{1001, 2001}, |
| 414 | RepeatedInt64: []int64{1002, 2002}, |
| 415 | RepeatedUint32: []uint32{1003, 2003}, |
| 416 | RepeatedUint64: []uint64{1004, 2004}, |
| 417 | RepeatedSint32: []int32{1005, 2005}, |
| 418 | RepeatedSint64: []int64{1006, 2006}, |
| 419 | RepeatedFixed32: []uint32{1007, 2007}, |
| 420 | RepeatedFixed64: []uint64{1008, 2008}, |
| 421 | RepeatedSfixed32: []int32{1009, 2009}, |
| 422 | RepeatedSfixed64: []int64{1010, 2010}, |
| 423 | RepeatedFloat: []float32{1011.5, 2011.5}, |
| 424 | RepeatedDouble: []float64{1012.5, 2012.5}, |
| 425 | RepeatedBool: []bool{true, false}, |
| 426 | RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{ |
| 427 | testpb.TestAllTypes_FOO, |
| 428 | testpb.TestAllTypes_BAR, |
| 429 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 430 | }, &test3pb.TestAllTypes{ |
| 431 | RepeatedInt32: []int32{1001, 2001}, |
| 432 | RepeatedInt64: []int64{1002, 2002}, |
| 433 | RepeatedUint32: []uint32{1003, 2003}, |
| 434 | RepeatedUint64: []uint64{1004, 2004}, |
| 435 | RepeatedSint32: []int32{1005, 2005}, |
| 436 | RepeatedSint64: []int64{1006, 2006}, |
| 437 | RepeatedFixed32: []uint32{1007, 2007}, |
| 438 | RepeatedFixed64: []uint64{1008, 2008}, |
| 439 | RepeatedSfixed32: []int32{1009, 2009}, |
| 440 | RepeatedSfixed64: []int64{1010, 2010}, |
| 441 | RepeatedFloat: []float32{1011.5, 2011.5}, |
| 442 | RepeatedDouble: []float64{1012.5, 2012.5}, |
| 443 | RepeatedBool: []bool{true, false}, |
| 444 | RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{ |
| 445 | test3pb.TestAllTypes_FOO, |
| 446 | test3pb.TestAllTypes_BAR, |
| 447 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 448 | }, build( |
| 449 | &testpb.TestAllExtensions{}, |
| 450 | extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}), |
| 451 | extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}), |
| 452 | extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}), |
| 453 | extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}), |
| 454 | extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}), |
| 455 | extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}), |
| 456 | extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}), |
| 457 | extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}), |
| 458 | extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}), |
| 459 | extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}), |
| 460 | extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}), |
| 461 | extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}), |
| 462 | extend(testpb.E_RepeatedBoolExtension, []bool{true, false}), |
| 463 | extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{ |
| 464 | testpb.TestAllTypes_FOO, |
| 465 | testpb.TestAllTypes_BAR, |
| 466 | }), |
| 467 | )}, |
| 468 | wire: pack.Message{ |
| 469 | pack.Tag{31, pack.BytesType}, pack.LengthPrefix{ |
| 470 | pack.Varint(1001), pack.Varint(2001), |
| 471 | }, |
| 472 | pack.Tag{32, pack.BytesType}, pack.LengthPrefix{ |
| 473 | pack.Varint(1002), pack.Varint(2002), |
| 474 | }, |
| 475 | pack.Tag{33, pack.BytesType}, pack.LengthPrefix{ |
| 476 | pack.Uvarint(1003), pack.Uvarint(2003), |
| 477 | }, |
| 478 | pack.Tag{34, pack.BytesType}, pack.LengthPrefix{ |
| 479 | pack.Uvarint(1004), pack.Uvarint(2004), |
| 480 | }, |
| 481 | pack.Tag{35, pack.BytesType}, pack.LengthPrefix{ |
| 482 | pack.Svarint(1005), pack.Svarint(2005), |
| 483 | }, |
| 484 | pack.Tag{36, pack.BytesType}, pack.LengthPrefix{ |
| 485 | pack.Svarint(1006), pack.Svarint(2006), |
| 486 | }, |
| 487 | pack.Tag{37, pack.BytesType}, pack.LengthPrefix{ |
| 488 | pack.Uint32(1007), pack.Uint32(2007), |
| 489 | }, |
| 490 | pack.Tag{38, pack.BytesType}, pack.LengthPrefix{ |
| 491 | pack.Uint64(1008), pack.Uint64(2008), |
| 492 | }, |
| 493 | pack.Tag{39, pack.BytesType}, pack.LengthPrefix{ |
| 494 | pack.Int32(1009), pack.Int32(2009), |
| 495 | }, |
| 496 | pack.Tag{40, pack.BytesType}, pack.LengthPrefix{ |
| 497 | pack.Int64(1010), pack.Int64(2010), |
| 498 | }, |
| 499 | pack.Tag{41, pack.BytesType}, pack.LengthPrefix{ |
| 500 | pack.Float32(1011.5), pack.Float32(2011.5), |
| 501 | }, |
| 502 | pack.Tag{42, pack.BytesType}, pack.LengthPrefix{ |
| 503 | pack.Float64(1012.5), pack.Float64(2012.5), |
| 504 | }, |
| 505 | pack.Tag{43, pack.BytesType}, pack.LengthPrefix{ |
| 506 | pack.Bool(true), pack.Bool(false), |
| 507 | }, |
| 508 | pack.Tag{51, pack.BytesType}, pack.LengthPrefix{ |
| 509 | pack.Varint(int(testpb.TestAllTypes_FOO)), |
| 510 | pack.Varint(int(testpb.TestAllTypes_BAR)), |
| 511 | }, |
| 512 | }.Marshal(), |
| 513 | }, |
| 514 | { |
| 515 | desc: "repeated messages", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 516 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 517 | RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{ |
| 518 | {A: scalar.Int32(1)}, |
| 519 | {A: scalar.Int32(2)}, |
| 520 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 521 | }, &test3pb.TestAllTypes{ |
| 522 | RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{ |
| 523 | {A: 1}, |
| 524 | {A: 2}, |
| 525 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 526 | }, build( |
| 527 | &testpb.TestAllExtensions{}, |
| 528 | extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{ |
| 529 | {A: scalar.Int32(1)}, |
| 530 | {A: scalar.Int32(2)}, |
| 531 | }), |
| 532 | )}, |
| 533 | wire: pack.Message{ |
| 534 | pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 535 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 536 | }), |
| 537 | pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 538 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 539 | }), |
| 540 | }.Marshal(), |
| 541 | }, |
| 542 | { |
| 543 | desc: "repeated groups", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 544 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 545 | Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{ |
| 546 | {A: scalar.Int32(1017)}, |
| 547 | {A: scalar.Int32(2017)}, |
| 548 | }, |
| 549 | }, build( |
| 550 | &testpb.TestAllExtensions{}, |
| 551 | extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{ |
| 552 | {A: scalar.Int32(1017)}, |
| 553 | {A: scalar.Int32(2017)}, |
| 554 | }), |
| 555 | )}, |
| 556 | wire: pack.Message{ |
| 557 | pack.Tag{46, pack.StartGroupType}, |
| 558 | pack.Tag{47, pack.VarintType}, pack.Varint(1017), |
| 559 | pack.Tag{46, pack.EndGroupType}, |
| 560 | pack.Tag{46, pack.StartGroupType}, |
| 561 | pack.Tag{47, pack.VarintType}, pack.Varint(2017), |
| 562 | pack.Tag{46, pack.EndGroupType}, |
| 563 | }.Marshal(), |
| 564 | }, |
| 565 | { |
| 566 | desc: "maps", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 567 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 568 | MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156}, |
| 569 | MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157}, |
| 570 | MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158}, |
| 571 | MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159}, |
| 572 | MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160}, |
| 573 | MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161}, |
| 574 | MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162}, |
| 575 | MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163}, |
| 576 | MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164}, |
| 577 | MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165}, |
| 578 | MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5}, |
| 579 | MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5}, |
| 580 | MapBoolBool: map[bool]bool{true: false, false: true}, |
| 581 | MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"}, |
| 582 | MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")}, |
| 583 | MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{ |
| 584 | "71.1.key": {A: scalar.Int32(1171)}, |
| 585 | "71.2.key": {A: scalar.Int32(2171)}, |
| 586 | }, |
| 587 | MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{ |
| 588 | "73.1.key": testpb.TestAllTypes_FOO, |
| 589 | "73.2.key": testpb.TestAllTypes_BAR, |
| 590 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 591 | }, &test3pb.TestAllTypes{ |
| 592 | MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156}, |
| 593 | MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157}, |
| 594 | MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158}, |
| 595 | MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159}, |
| 596 | MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160}, |
| 597 | MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161}, |
| 598 | MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162}, |
| 599 | MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163}, |
| 600 | MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164}, |
| 601 | MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165}, |
| 602 | MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5}, |
| 603 | MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5}, |
| 604 | MapBoolBool: map[bool]bool{true: false, false: true}, |
| 605 | MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"}, |
| 606 | MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")}, |
| 607 | MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{ |
| 608 | "71.1.key": {A: 1171}, |
| 609 | "71.2.key": {A: 2171}, |
| 610 | }, |
| 611 | MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{ |
| 612 | "73.1.key": test3pb.TestAllTypes_FOO, |
| 613 | "73.2.key": test3pb.TestAllTypes_BAR, |
| 614 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 615 | }}, |
| 616 | wire: pack.Message{ |
| 617 | pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 618 | pack.Tag{1, pack.VarintType}, pack.Varint(1056), |
| 619 | pack.Tag{2, pack.VarintType}, pack.Varint(1156), |
| 620 | }), |
| 621 | pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 622 | pack.Tag{1, pack.VarintType}, pack.Varint(2056), |
| 623 | pack.Tag{2, pack.VarintType}, pack.Varint(2156), |
| 624 | }), |
| 625 | pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 626 | pack.Tag{1, pack.VarintType}, pack.Varint(1057), |
| 627 | pack.Tag{2, pack.VarintType}, pack.Varint(1157), |
| 628 | }), |
| 629 | pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 630 | pack.Tag{1, pack.VarintType}, pack.Varint(2057), |
| 631 | pack.Tag{2, pack.VarintType}, pack.Varint(2157), |
| 632 | }), |
| 633 | pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 634 | pack.Tag{1, pack.VarintType}, pack.Varint(1058), |
| 635 | pack.Tag{2, pack.VarintType}, pack.Varint(1158), |
| 636 | }), |
| 637 | pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 638 | pack.Tag{1, pack.VarintType}, pack.Varint(2058), |
| 639 | pack.Tag{2, pack.VarintType}, pack.Varint(2158), |
| 640 | }), |
| 641 | pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 642 | pack.Tag{1, pack.VarintType}, pack.Varint(1059), |
| 643 | pack.Tag{2, pack.VarintType}, pack.Varint(1159), |
| 644 | }), |
| 645 | pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 646 | pack.Tag{1, pack.VarintType}, pack.Varint(2059), |
| 647 | pack.Tag{2, pack.VarintType}, pack.Varint(2159), |
| 648 | }), |
| 649 | pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 650 | pack.Tag{1, pack.VarintType}, pack.Svarint(1060), |
| 651 | pack.Tag{2, pack.VarintType}, pack.Svarint(1160), |
| 652 | }), |
| 653 | pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 654 | pack.Tag{1, pack.VarintType}, pack.Svarint(2060), |
| 655 | pack.Tag{2, pack.VarintType}, pack.Svarint(2160), |
| 656 | }), |
| 657 | pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 658 | pack.Tag{1, pack.VarintType}, pack.Svarint(1061), |
| 659 | pack.Tag{2, pack.VarintType}, pack.Svarint(1161), |
| 660 | }), |
| 661 | pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 662 | pack.Tag{1, pack.VarintType}, pack.Svarint(2061), |
| 663 | pack.Tag{2, pack.VarintType}, pack.Svarint(2161), |
| 664 | }), |
| 665 | pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 666 | pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062), |
| 667 | pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162), |
| 668 | }), |
| 669 | pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 670 | pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062), |
| 671 | pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162), |
| 672 | }), |
| 673 | pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 674 | pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063), |
| 675 | pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163), |
| 676 | }), |
| 677 | pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 678 | pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063), |
| 679 | pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163), |
| 680 | }), |
| 681 | pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 682 | pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064), |
| 683 | pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164), |
| 684 | }), |
| 685 | pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 686 | pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064), |
| 687 | pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164), |
| 688 | }), |
| 689 | pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 690 | pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065), |
| 691 | pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165), |
| 692 | }), |
| 693 | pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 694 | pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065), |
| 695 | pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165), |
| 696 | }), |
| 697 | pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 698 | pack.Tag{1, pack.VarintType}, pack.Varint(1066), |
| 699 | pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5), |
| 700 | }), |
| 701 | pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 702 | pack.Tag{1, pack.VarintType}, pack.Varint(2066), |
| 703 | pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5), |
| 704 | }), |
| 705 | pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 706 | pack.Tag{1, pack.VarintType}, pack.Varint(1067), |
| 707 | pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5), |
| 708 | }), |
| 709 | pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 710 | pack.Tag{1, pack.VarintType}, pack.Varint(2067), |
| 711 | pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5), |
| 712 | }), |
| 713 | pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 714 | pack.Tag{1, pack.VarintType}, pack.Bool(true), |
| 715 | pack.Tag{2, pack.VarintType}, pack.Bool(false), |
| 716 | }), |
| 717 | pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 718 | pack.Tag{1, pack.VarintType}, pack.Bool(false), |
| 719 | pack.Tag{2, pack.VarintType}, pack.Bool(true), |
| 720 | }), |
| 721 | pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 722 | pack.Tag{1, pack.BytesType}, pack.String("69.1.key"), |
| 723 | pack.Tag{2, pack.BytesType}, pack.String("69.1.val"), |
| 724 | }), |
| 725 | pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 726 | pack.Tag{1, pack.BytesType}, pack.String("69.2.key"), |
| 727 | pack.Tag{2, pack.BytesType}, pack.String("69.2.val"), |
| 728 | }), |
| 729 | pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 730 | pack.Tag{1, pack.BytesType}, pack.String("70.1.key"), |
| 731 | pack.Tag{2, pack.BytesType}, pack.String("70.1.val"), |
| 732 | }), |
| 733 | pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 734 | pack.Tag{1, pack.BytesType}, pack.String("70.2.key"), |
| 735 | pack.Tag{2, pack.BytesType}, pack.String("70.2.val"), |
| 736 | }), |
| 737 | pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 738 | pack.Tag{1, pack.BytesType}, pack.String("71.1.key"), |
| 739 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 740 | pack.Tag{1, pack.VarintType}, pack.Varint(1171), |
| 741 | }), |
| 742 | }), |
| 743 | pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 744 | pack.Tag{1, pack.BytesType}, pack.String("71.2.key"), |
| 745 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 746 | pack.Tag{1, pack.VarintType}, pack.Varint(2171), |
| 747 | }), |
| 748 | }), |
| 749 | pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 750 | pack.Tag{1, pack.BytesType}, pack.String("73.1.key"), |
| 751 | pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)), |
| 752 | }), |
| 753 | pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 754 | pack.Tag{1, pack.BytesType}, pack.String("73.2.key"), |
| 755 | pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)), |
| 756 | }), |
| 757 | }.Marshal(), |
| 758 | }, |
| 759 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 760 | desc: "oneof (uint32)", |
| 761 | decodeTo: []proto.Message{ |
| 762 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}}, |
| 763 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}}, |
| 764 | }, |
| 765 | wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 766 | }, |
| 767 | { |
| 768 | desc: "oneof (message)", |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 769 | decodeTo: []proto.Message{ |
| 770 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{ |
| 771 | &testpb.TestAllTypes_NestedMessage{A: scalar.Int32(1112)}, |
| 772 | }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{ |
| 773 | &test3pb.TestAllTypes_NestedMessage{A: 1112}, |
| 774 | }}, |
| 775 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 776 | wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 777 | pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)}, |
| 778 | })}.Marshal(), |
| 779 | }, |
| 780 | { |
| 781 | desc: "oneof (overridden message)", |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 782 | decodeTo: []proto.Message{ |
| 783 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{ |
| 784 | &testpb.TestAllTypes_NestedMessage{ |
| 785 | Corecursive: &testpb.TestAllTypes{ |
| 786 | OptionalInt32: scalar.Int32(43), |
| 787 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 788 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 789 | }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{ |
| 790 | &test3pb.TestAllTypes_NestedMessage{ |
| 791 | Corecursive: &test3pb.TestAllTypes{ |
| 792 | OptionalInt32: 43, |
| 793 | }, |
| 794 | }, |
| 795 | }}}, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 796 | wire: pack.Message{ |
| 797 | pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 798 | pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)}, |
| 799 | }), |
| 800 | pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 801 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 802 | pack.Tag{1, pack.VarintType}, pack.Varint(43), |
| 803 | }), |
| 804 | }), |
| 805 | }.Marshal(), |
| 806 | }, |
| 807 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 808 | desc: "oneof (string)", |
| 809 | decodeTo: []proto.Message{ |
| 810 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}}, |
| 811 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}}, |
| 812 | }, |
| 813 | wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 814 | }, |
| 815 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 816 | desc: "oneof (bytes)", |
| 817 | decodeTo: []proto.Message{ |
| 818 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}}, |
| 819 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}}, |
| 820 | }, |
| 821 | wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 822 | }, |
| 823 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 824 | desc: "oneof (bool)", |
| 825 | decodeTo: []proto.Message{ |
| 826 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}}, |
| 827 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}}, |
| 828 | }, |
| 829 | wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 830 | }, |
| 831 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 832 | desc: "oneof (uint64)", |
| 833 | decodeTo: []proto.Message{ |
| 834 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}}, |
| 835 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}}, |
| 836 | }, |
| 837 | wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 838 | }, |
| 839 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 840 | desc: "oneof (float)", |
| 841 | decodeTo: []proto.Message{ |
| 842 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}}, |
| 843 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}}, |
| 844 | }, |
| 845 | wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 846 | }, |
| 847 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 848 | desc: "oneof (double)", |
| 849 | decodeTo: []proto.Message{ |
| 850 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}}, |
| 851 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}}, |
| 852 | }, |
| 853 | wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 854 | }, |
| 855 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 856 | desc: "oneof (enum)", |
| 857 | decodeTo: []proto.Message{ |
| 858 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}}, |
| 859 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}}, |
| 860 | }, |
| 861 | wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 862 | }, |
| 863 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 864 | desc: "oneof (overridden value)", |
| 865 | decodeTo: []proto.Message{ |
| 866 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}}, |
| 867 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}}, |
| 868 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 869 | wire: pack.Message{ |
| 870 | pack.Tag{111, pack.VarintType}, pack.Varint(1), |
| 871 | pack.Tag{116, pack.VarintType}, pack.Varint(2), |
| 872 | }.Marshal(), |
| 873 | }, |
| 874 | // TODO: More unknown field tests for ordering, repeated fields, etc. |
| 875 | // |
| 876 | // It is currently impossible to produce results that the v1 Equal |
| 877 | // considers equivalent to those of the v1 decoder. Figure out if |
| 878 | // that's a problem or not. |
| 879 | { |
| 880 | desc: "unknown fields", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 881 | decodeTo: []proto.Message{build( |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 882 | &testpb.TestAllTypes{}, |
| 883 | unknown(100000, pack.Message{ |
| 884 | pack.Tag{100000, pack.VarintType}, pack.Varint(1), |
| 885 | }.Marshal()), |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 886 | ), build( |
| 887 | &test3pb.TestAllTypes{}, |
| 888 | unknown(100000, pack.Message{ |
| 889 | pack.Tag{100000, pack.VarintType}, pack.Varint(1), |
| 890 | }.Marshal()), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 891 | )}, |
| 892 | wire: pack.Message{ |
| 893 | pack.Tag{100000, pack.VarintType}, pack.Varint(1), |
| 894 | }.Marshal(), |
| 895 | }, |
| 896 | { |
| 897 | desc: "field type mismatch", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 898 | decodeTo: []proto.Message{build( |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 899 | &testpb.TestAllTypes{}, |
| 900 | unknown(1, pack.Message{ |
| 901 | pack.Tag{1, pack.BytesType}, pack.String("string"), |
| 902 | }.Marshal()), |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 903 | ), build( |
| 904 | &test3pb.TestAllTypes{}, |
| 905 | unknown(1, pack.Message{ |
| 906 | pack.Tag{1, pack.BytesType}, pack.String("string"), |
| 907 | }.Marshal()), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 908 | )}, |
| 909 | wire: pack.Message{ |
| 910 | pack.Tag{1, pack.BytesType}, pack.String("string"), |
| 911 | }.Marshal(), |
| 912 | }, |
| 913 | { |
| 914 | desc: "map field element mismatch", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 915 | decodeTo: []proto.Message{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 916 | &testpb.TestAllTypes{ |
| 917 | MapInt32Int32: map[int32]int32{1: 0}, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 918 | }, &test3pb.TestAllTypes{ |
| 919 | MapInt32Int32: map[int32]int32{1: 0}, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 920 | }, |
| 921 | }, |
| 922 | wire: pack.Message{ |
| 923 | pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 924 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 925 | pack.Tag{2, pack.BytesType}, pack.String("string"), |
| 926 | }), |
| 927 | }.Marshal(), |
| 928 | }, |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 929 | { |
| 930 | desc: "required field unset", |
| 931 | partial: true, |
| 932 | decodeTo: []proto.Message{&testpb.TestRequired{}}, |
| 933 | }, |
| 934 | { |
| 935 | desc: "required field set", |
| 936 | decodeTo: []proto.Message{&testpb.TestRequired{ |
| 937 | RequiredField: scalar.Int32(1), |
| 938 | }}, |
| 939 | wire: pack.Message{ |
| 940 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 941 | }.Marshal(), |
| 942 | }, |
| 943 | { |
| 944 | desc: "required field in optional message unset", |
| 945 | partial: true, |
| 946 | decodeTo: []proto.Message{&testpb.TestRequiredForeign{ |
| 947 | OptionalMessage: &testpb.TestRequired{}, |
| 948 | }}, |
| 949 | wire: pack.Message{ |
| 950 | pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}), |
| 951 | }.Marshal(), |
| 952 | }, |
| 953 | { |
| 954 | desc: "required field in optional message set", |
| 955 | decodeTo: []proto.Message{&testpb.TestRequiredForeign{ |
| 956 | OptionalMessage: &testpb.TestRequired{ |
| 957 | RequiredField: scalar.Int32(1), |
| 958 | }, |
| 959 | }}, |
| 960 | wire: pack.Message{ |
| 961 | pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 962 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 963 | }), |
| 964 | }.Marshal(), |
| 965 | }, |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 966 | { |
| 967 | desc: "required field in optional message set (split across multiple tags)", |
| 968 | decodeTo: []proto.Message{&testpb.TestRequiredForeign{ |
| 969 | OptionalMessage: &testpb.TestRequired{ |
| 970 | RequiredField: scalar.Int32(1), |
| 971 | }, |
| 972 | }}, |
| 973 | wire: pack.Message{ |
| 974 | pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}), |
| 975 | pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 976 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 977 | }), |
| 978 | }.Marshal(), |
| 979 | }, |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 980 | { |
| 981 | desc: "required field in repeated message unset", |
| 982 | partial: true, |
| 983 | decodeTo: []proto.Message{&testpb.TestRequiredForeign{ |
| 984 | RepeatedMessage: []*testpb.TestRequired{ |
| 985 | {RequiredField: scalar.Int32(1)}, |
| 986 | {}, |
| 987 | }, |
| 988 | }}, |
| 989 | wire: pack.Message{ |
| 990 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 991 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 992 | }), |
| 993 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}), |
| 994 | }.Marshal(), |
| 995 | }, |
| 996 | { |
| 997 | desc: "required field in repeated message set", |
| 998 | decodeTo: []proto.Message{&testpb.TestRequiredForeign{ |
| 999 | RepeatedMessage: []*testpb.TestRequired{ |
| 1000 | {RequiredField: scalar.Int32(1)}, |
| 1001 | {RequiredField: scalar.Int32(2)}, |
| 1002 | }, |
| 1003 | }}, |
| 1004 | wire: pack.Message{ |
| 1005 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1006 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1007 | }), |
| 1008 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1009 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 1010 | }), |
| 1011 | }.Marshal(), |
| 1012 | }, |
| 1013 | { |
| 1014 | desc: "required field in map message unset", |
| 1015 | partial: true, |
| 1016 | decodeTo: []proto.Message{&testpb.TestRequiredForeign{ |
| 1017 | MapMessage: map[int32]*testpb.TestRequired{ |
| 1018 | 1: {RequiredField: scalar.Int32(1)}, |
| 1019 | 2: {}, |
| 1020 | }, |
| 1021 | }}, |
| 1022 | wire: pack.Message{ |
| 1023 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1024 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1025 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1026 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1027 | }), |
| 1028 | }), |
| 1029 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1030 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 1031 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}), |
| 1032 | }), |
| 1033 | }.Marshal(), |
| 1034 | }, |
| 1035 | { |
| 1036 | desc: "required field in map message set", |
| 1037 | decodeTo: []proto.Message{&testpb.TestRequiredForeign{ |
| 1038 | MapMessage: map[int32]*testpb.TestRequired{ |
| 1039 | 1: {RequiredField: scalar.Int32(1)}, |
| 1040 | 2: {RequiredField: scalar.Int32(2)}, |
| 1041 | }, |
| 1042 | }}, |
| 1043 | wire: pack.Message{ |
| 1044 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1045 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1046 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1047 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1048 | }), |
| 1049 | }), |
| 1050 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1051 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 1052 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1053 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 1054 | }), |
| 1055 | }), |
| 1056 | }.Marshal(), |
| 1057 | }, |
| 1058 | { |
| 1059 | desc: "required field in optional group unset", |
| 1060 | partial: true, |
| 1061 | decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{ |
| 1062 | Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{}, |
| 1063 | }}, |
| 1064 | wire: pack.Message{ |
| 1065 | pack.Tag{1, pack.StartGroupType}, |
| 1066 | pack.Tag{1, pack.EndGroupType}, |
| 1067 | }.Marshal(), |
| 1068 | }, |
| 1069 | { |
| 1070 | desc: "required field in optional group set", |
| 1071 | decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{ |
| 1072 | Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{ |
| 1073 | A: scalar.Int32(1), |
| 1074 | }, |
| 1075 | }}, |
| 1076 | wire: pack.Message{ |
| 1077 | pack.Tag{1, pack.StartGroupType}, |
| 1078 | pack.Tag{2, pack.VarintType}, pack.Varint(1), |
| 1079 | pack.Tag{1, pack.EndGroupType}, |
| 1080 | }.Marshal(), |
| 1081 | }, |
| 1082 | { |
| 1083 | desc: "required field in repeated group unset", |
| 1084 | partial: true, |
| 1085 | decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{ |
| 1086 | Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{ |
| 1087 | {A: scalar.Int32(1)}, |
| 1088 | {}, |
| 1089 | }, |
| 1090 | }}, |
| 1091 | wire: pack.Message{ |
| 1092 | pack.Tag{3, pack.StartGroupType}, |
| 1093 | pack.Tag{4, pack.VarintType}, pack.Varint(1), |
| 1094 | pack.Tag{3, pack.EndGroupType}, |
| 1095 | pack.Tag{3, pack.StartGroupType}, |
| 1096 | pack.Tag{3, pack.EndGroupType}, |
| 1097 | }.Marshal(), |
| 1098 | }, |
| 1099 | { |
| 1100 | desc: "required field in repeated group set", |
| 1101 | decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{ |
| 1102 | Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{ |
| 1103 | {A: scalar.Int32(1)}, |
| 1104 | {A: scalar.Int32(2)}, |
| 1105 | }, |
| 1106 | }}, |
| 1107 | wire: pack.Message{ |
| 1108 | pack.Tag{3, pack.StartGroupType}, |
| 1109 | pack.Tag{4, pack.VarintType}, pack.Varint(1), |
| 1110 | pack.Tag{3, pack.EndGroupType}, |
| 1111 | pack.Tag{3, pack.StartGroupType}, |
| 1112 | pack.Tag{4, pack.VarintType}, pack.Varint(2), |
| 1113 | pack.Tag{3, pack.EndGroupType}, |
| 1114 | }.Marshal(), |
| 1115 | }, |
| 1116 | { |
| 1117 | desc: "required field in extension message unset", |
| 1118 | partial: true, |
| 1119 | invalidExtensions: true, |
| 1120 | decodeTo: []proto.Message{build( |
| 1121 | &testpb.TestAllExtensions{}, |
| 1122 | extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}), |
| 1123 | )}, |
| 1124 | wire: pack.Message{ |
| 1125 | pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}), |
| 1126 | }.Marshal(), |
| 1127 | }, |
| 1128 | { |
| 1129 | desc: "required field in extension message set", |
| 1130 | decodeTo: []proto.Message{build( |
| 1131 | &testpb.TestAllExtensions{}, |
| 1132 | extend(testpb.E_TestRequired_Single, &testpb.TestRequired{ |
| 1133 | RequiredField: scalar.Int32(1), |
| 1134 | }), |
| 1135 | )}, |
| 1136 | wire: pack.Message{ |
| 1137 | pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1138 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1139 | }), |
| 1140 | }.Marshal(), |
| 1141 | }, |
| 1142 | { |
| 1143 | desc: "required field in repeated extension message unset", |
| 1144 | partial: true, |
| 1145 | invalidExtensions: true, |
| 1146 | decodeTo: []proto.Message{build( |
| 1147 | &testpb.TestAllExtensions{}, |
| 1148 | extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{ |
| 1149 | {RequiredField: scalar.Int32(1)}, |
| 1150 | {}, |
| 1151 | }), |
| 1152 | )}, |
| 1153 | wire: pack.Message{ |
| 1154 | pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1155 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1156 | }), |
| 1157 | pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}), |
| 1158 | }.Marshal(), |
| 1159 | }, |
| 1160 | { |
| 1161 | desc: "required field in repeated extension message set", |
| 1162 | decodeTo: []proto.Message{build( |
| 1163 | &testpb.TestAllExtensions{}, |
| 1164 | extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{ |
| 1165 | {RequiredField: scalar.Int32(1)}, |
| 1166 | {RequiredField: scalar.Int32(2)}, |
| 1167 | }), |
| 1168 | )}, |
| 1169 | wire: pack.Message{ |
| 1170 | pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1171 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1172 | }), |
| 1173 | pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1174 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 1175 | }), |
| 1176 | }.Marshal(), |
| 1177 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
Damien Neil | bc310b5 | 2019-04-11 11:46:55 -0700 | [diff] [blame] | 1180 | var invalidUTF8TestProtos = []testProto{ |
| 1181 | { |
| 1182 | desc: "invalid UTF-8 in optional string field", |
| 1183 | decodeTo: []proto.Message{&test3pb.TestAllTypes{ |
| 1184 | OptionalString: "abc\xff", |
| 1185 | }}, |
| 1186 | wire: pack.Message{ |
| 1187 | pack.Tag{14, pack.BytesType}, pack.String("abc\xff"), |
| 1188 | }.Marshal(), |
| 1189 | }, |
| 1190 | { |
| 1191 | desc: "invalid UTF-8 in repeated string field", |
| 1192 | decodeTo: []proto.Message{&test3pb.TestAllTypes{ |
| 1193 | RepeatedString: []string{"foo", "abc\xff"}, |
| 1194 | }}, |
| 1195 | wire: pack.Message{ |
| 1196 | pack.Tag{44, pack.BytesType}, pack.String("foo"), |
| 1197 | pack.Tag{44, pack.BytesType}, pack.String("abc\xff"), |
| 1198 | }.Marshal(), |
| 1199 | }, |
| 1200 | { |
| 1201 | desc: "invalid UTF-8 in nested message", |
| 1202 | decodeTo: []proto.Message{&test3pb.TestAllTypes{ |
| 1203 | OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{ |
| 1204 | Corecursive: &test3pb.TestAllTypes{ |
| 1205 | OptionalString: "abc\xff", |
| 1206 | }, |
| 1207 | }, |
| 1208 | }}, |
| 1209 | wire: pack.Message{ |
| 1210 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1211 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1212 | pack.Tag{14, pack.BytesType}, pack.String("abc\xff"), |
| 1213 | }), |
| 1214 | }), |
| 1215 | }.Marshal(), |
| 1216 | }, |
| 1217 | { |
| 1218 | desc: "invalid UTF-8 in map key", |
| 1219 | decodeTo: []proto.Message{&test3pb.TestAllTypes{ |
| 1220 | MapStringString: map[string]string{"key\xff": "val"}, |
| 1221 | }}, |
| 1222 | wire: pack.Message{ |
| 1223 | pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1224 | pack.Tag{1, pack.BytesType}, pack.String("key\xff"), |
| 1225 | pack.Tag{2, pack.BytesType}, pack.String("val"), |
| 1226 | }), |
| 1227 | }.Marshal(), |
| 1228 | }, |
| 1229 | { |
| 1230 | desc: "invalid UTF-8 in map value", |
| 1231 | decodeTo: []proto.Message{&test3pb.TestAllTypes{ |
| 1232 | MapStringString: map[string]string{"key": "val\xff"}, |
| 1233 | }}, |
| 1234 | wire: pack.Message{ |
| 1235 | pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1236 | pack.Tag{1, pack.BytesType}, pack.String("key"), |
| 1237 | pack.Tag{2, pack.BytesType}, pack.String("val\xff"), |
| 1238 | }), |
| 1239 | }.Marshal(), |
| 1240 | }, |
| 1241 | } |
| 1242 | |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 1243 | func build(m proto.Message, opts ...buildOpt) proto.Message { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1244 | for _, opt := range opts { |
| 1245 | opt(m) |
| 1246 | } |
| 1247 | return m |
| 1248 | } |
| 1249 | |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 1250 | type buildOpt func(proto.Message) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1251 | |
| 1252 | func unknown(num pref.FieldNumber, raw pref.RawFields) buildOpt { |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 1253 | return func(m proto.Message) { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1254 | m.ProtoReflect().UnknownFields().Set(num, raw) |
| 1255 | } |
| 1256 | } |
| 1257 | |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 1258 | func registerExtension(desc *protoV1.ExtensionDesc) buildOpt { |
| 1259 | return func(m proto.Message) { |
| 1260 | et := protolegacy.X.ExtensionTypeFromDesc(desc) |
| 1261 | m.ProtoReflect().KnownFields().ExtensionTypes().Register(et) |
| 1262 | } |
| 1263 | } |
| 1264 | |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1265 | func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt { |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 1266 | return func(m proto.Message) { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1267 | if err := protoV1.SetExtension(m.(protoV1.Message), desc, value); err != nil { |
| 1268 | panic(err) |
| 1269 | } |
| 1270 | } |
| 1271 | } |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 1272 | |
| 1273 | func marshalText(m proto.Message) string { |
| 1274 | b, _ := textpb.Marshal(m) |
| 1275 | return string(b) |
| 1276 | } |
Damien Neil | bc310b5 | 2019-04-11 11:46:55 -0700 | [diff] [blame] | 1277 | |
| 1278 | func isErrInvalidUTF8(err error) bool { |
| 1279 | nerr, ok := err.(errors.NonFatalErrors) |
| 1280 | if !ok || len(nerr) == 0 { |
| 1281 | return false |
| 1282 | } |
| 1283 | for _, err := range nerr { |
| 1284 | if e, ok := err.(interface{ InvalidUTF8() bool }); ok && e.InvalidUTF8() { |
| 1285 | continue |
| 1286 | } |
| 1287 | return false |
| 1288 | } |
| 1289 | return true |
| 1290 | } |