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