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