Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 1 | // Copyright 2019 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 | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 5 | package protojson_test |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "math" |
| 9 | "testing" |
| 10 | |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 11 | "google.golang.org/protobuf/encoding/protojson" |
Joe Tsai | d47ea19 | 2019-07-09 22:38:15 -0700 | [diff] [blame] | 12 | "google.golang.org/protobuf/internal/flags" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 13 | pimpl "google.golang.org/protobuf/internal/impl" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 14 | "google.golang.org/protobuf/proto" |
| 15 | preg "google.golang.org/protobuf/reflect/protoregistry" |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 16 | |
Joe Tsai | d47ea19 | 2019-07-09 22:38:15 -0700 | [diff] [blame] | 17 | "google.golang.org/protobuf/encoding/testprotos/pb2" |
| 18 | "google.golang.org/protobuf/encoding/testprotos/pb3" |
| 19 | testpb "google.golang.org/protobuf/internal/testprotos/test" |
| 20 | weakpb "google.golang.org/protobuf/internal/testprotos/test/weak1" |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 21 | "google.golang.org/protobuf/types/known/anypb" |
| 22 | "google.golang.org/protobuf/types/known/durationpb" |
| 23 | "google.golang.org/protobuf/types/known/emptypb" |
| 24 | "google.golang.org/protobuf/types/known/fieldmaskpb" |
| 25 | "google.golang.org/protobuf/types/known/structpb" |
| 26 | "google.golang.org/protobuf/types/known/timestamppb" |
| 27 | "google.golang.org/protobuf/types/known/wrapperspb" |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | func TestUnmarshal(t *testing.T) { |
| 31 | tests := []struct { |
| 32 | desc string |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 33 | umo protojson.UnmarshalOptions |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 34 | inputMessage proto.Message |
| 35 | inputText string |
| 36 | wantMessage proto.Message |
| 37 | // TODO: verify expected error message substring. |
| 38 | wantErr bool |
Joe Tsai | d47ea19 | 2019-07-09 22:38:15 -0700 | [diff] [blame] | 39 | skip bool |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 40 | }{{ |
| 41 | desc: "proto2 empty message", |
| 42 | inputMessage: &pb2.Scalars{}, |
| 43 | inputText: "{}", |
| 44 | wantMessage: &pb2.Scalars{}, |
| 45 | }, { |
| 46 | desc: "unexpected value instead of EOF", |
| 47 | inputMessage: &pb2.Scalars{}, |
| 48 | inputText: "{} {}", |
| 49 | wantErr: true, |
| 50 | }, { |
| 51 | desc: "proto2 optional scalars set to zero values", |
| 52 | inputMessage: &pb2.Scalars{}, |
| 53 | inputText: `{ |
| 54 | "optBool": false, |
| 55 | "optInt32": 0, |
| 56 | "optInt64": 0, |
| 57 | "optUint32": 0, |
| 58 | "optUint64": 0, |
| 59 | "optSint32": 0, |
| 60 | "optSint64": 0, |
| 61 | "optFixed32": 0, |
| 62 | "optFixed64": 0, |
| 63 | "optSfixed32": 0, |
| 64 | "optSfixed64": 0, |
| 65 | "optFloat": 0, |
| 66 | "optDouble": 0, |
| 67 | "optBytes": "", |
| 68 | "optString": "" |
| 69 | }`, |
| 70 | wantMessage: &pb2.Scalars{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 71 | OptBool: proto.Bool(false), |
| 72 | OptInt32: proto.Int32(0), |
| 73 | OptInt64: proto.Int64(0), |
| 74 | OptUint32: proto.Uint32(0), |
| 75 | OptUint64: proto.Uint64(0), |
| 76 | OptSint32: proto.Int32(0), |
| 77 | OptSint64: proto.Int64(0), |
| 78 | OptFixed32: proto.Uint32(0), |
| 79 | OptFixed64: proto.Uint64(0), |
| 80 | OptSfixed32: proto.Int32(0), |
| 81 | OptSfixed64: proto.Int64(0), |
| 82 | OptFloat: proto.Float32(0), |
| 83 | OptDouble: proto.Float64(0), |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 84 | OptBytes: []byte{}, |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 85 | OptString: proto.String(""), |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 86 | }, |
| 87 | }, { |
| 88 | desc: "proto3 scalars set to zero values", |
| 89 | inputMessage: &pb3.Scalars{}, |
| 90 | inputText: `{ |
| 91 | "sBool": false, |
| 92 | "sInt32": 0, |
| 93 | "sInt64": 0, |
| 94 | "sUint32": 0, |
| 95 | "sUint64": 0, |
| 96 | "sSint32": 0, |
| 97 | "sSint64": 0, |
| 98 | "sFixed32": 0, |
| 99 | "sFixed64": 0, |
| 100 | "sSfixed32": 0, |
| 101 | "sSfixed64": 0, |
| 102 | "sFloat": 0, |
| 103 | "sDouble": 0, |
| 104 | "sBytes": "", |
| 105 | "sString": "" |
| 106 | }`, |
| 107 | wantMessage: &pb3.Scalars{}, |
| 108 | }, { |
| 109 | desc: "proto2 optional scalars set to null", |
| 110 | inputMessage: &pb2.Scalars{}, |
| 111 | inputText: `{ |
| 112 | "optBool": null, |
| 113 | "optInt32": null, |
| 114 | "optInt64": null, |
| 115 | "optUint32": null, |
| 116 | "optUint64": null, |
| 117 | "optSint32": null, |
| 118 | "optSint64": null, |
| 119 | "optFixed32": null, |
| 120 | "optFixed64": null, |
| 121 | "optSfixed32": null, |
| 122 | "optSfixed64": null, |
| 123 | "optFloat": null, |
| 124 | "optDouble": null, |
| 125 | "optBytes": null, |
| 126 | "optString": null |
| 127 | }`, |
| 128 | wantMessage: &pb2.Scalars{}, |
| 129 | }, { |
| 130 | desc: "proto3 scalars set to null", |
| 131 | inputMessage: &pb3.Scalars{}, |
| 132 | inputText: `{ |
| 133 | "sBool": null, |
| 134 | "sInt32": null, |
| 135 | "sInt64": null, |
| 136 | "sUint32": null, |
| 137 | "sUint64": null, |
| 138 | "sSint32": null, |
| 139 | "sSint64": null, |
| 140 | "sFixed32": null, |
| 141 | "sFixed64": null, |
| 142 | "sSfixed32": null, |
| 143 | "sSfixed64": null, |
| 144 | "sFloat": null, |
| 145 | "sDouble": null, |
| 146 | "sBytes": null, |
| 147 | "sString": null |
| 148 | }`, |
| 149 | wantMessage: &pb3.Scalars{}, |
| 150 | }, { |
| 151 | desc: "boolean", |
| 152 | inputMessage: &pb3.Scalars{}, |
| 153 | inputText: `{"sBool": true}`, |
| 154 | wantMessage: &pb3.Scalars{ |
| 155 | SBool: true, |
| 156 | }, |
| 157 | }, { |
| 158 | desc: "not boolean", |
| 159 | inputMessage: &pb3.Scalars{}, |
| 160 | inputText: `{"sBool": "true"}`, |
| 161 | wantErr: true, |
| 162 | }, { |
| 163 | desc: "float and double", |
| 164 | inputMessage: &pb3.Scalars{}, |
| 165 | inputText: `{ |
| 166 | "sFloat": 1.234, |
| 167 | "sDouble": 5.678 |
| 168 | }`, |
| 169 | wantMessage: &pb3.Scalars{ |
| 170 | SFloat: 1.234, |
| 171 | SDouble: 5.678, |
| 172 | }, |
| 173 | }, { |
| 174 | desc: "float and double in string", |
| 175 | inputMessage: &pb3.Scalars{}, |
| 176 | inputText: `{ |
| 177 | "sFloat": "1.234", |
| 178 | "sDouble": "5.678" |
| 179 | }`, |
| 180 | wantMessage: &pb3.Scalars{ |
| 181 | SFloat: 1.234, |
| 182 | SDouble: 5.678, |
| 183 | }, |
| 184 | }, { |
| 185 | desc: "float and double in E notation", |
| 186 | inputMessage: &pb3.Scalars{}, |
| 187 | inputText: `{ |
| 188 | "sFloat": 12.34E-1, |
| 189 | "sDouble": 5.678e4 |
| 190 | }`, |
| 191 | wantMessage: &pb3.Scalars{ |
| 192 | SFloat: 1.234, |
| 193 | SDouble: 56780, |
| 194 | }, |
| 195 | }, { |
| 196 | desc: "float and double in string E notation", |
| 197 | inputMessage: &pb3.Scalars{}, |
| 198 | inputText: `{ |
| 199 | "sFloat": "12.34E-1", |
| 200 | "sDouble": "5.678e4" |
| 201 | }`, |
| 202 | wantMessage: &pb3.Scalars{ |
| 203 | SFloat: 1.234, |
| 204 | SDouble: 56780, |
| 205 | }, |
| 206 | }, { |
| 207 | desc: "float exceeds limit", |
| 208 | inputMessage: &pb3.Scalars{}, |
| 209 | inputText: `{"sFloat": 3.4e39}`, |
| 210 | wantErr: true, |
| 211 | }, { |
| 212 | desc: "float in string exceeds limit", |
| 213 | inputMessage: &pb3.Scalars{}, |
| 214 | inputText: `{"sFloat": "-3.4e39"}`, |
| 215 | wantErr: true, |
| 216 | }, { |
| 217 | desc: "double exceeds limit", |
| 218 | inputMessage: &pb3.Scalars{}, |
| 219 | inputText: `{"sFloat": -1.79e+309}`, |
| 220 | wantErr: true, |
| 221 | }, { |
| 222 | desc: "double in string exceeds limit", |
| 223 | inputMessage: &pb3.Scalars{}, |
| 224 | inputText: `{"sFloat": "1.79e+309"}`, |
| 225 | wantErr: true, |
| 226 | }, { |
| 227 | desc: "infinites", |
| 228 | inputMessage: &pb3.Scalars{}, |
| 229 | inputText: `{"sFloat": "Infinity", "sDouble": "-Infinity"}`, |
| 230 | wantMessage: &pb3.Scalars{ |
| 231 | SFloat: float32(math.Inf(+1)), |
| 232 | SDouble: math.Inf(-1), |
| 233 | }, |
| 234 | }, { |
Herbie Ong | 06a6b0b | 2019-04-24 21:15:45 -0700 | [diff] [blame] | 235 | desc: "float string with leading space", |
| 236 | inputMessage: &pb3.Scalars{}, |
| 237 | inputText: `{"sFloat": " 1.234"}`, |
| 238 | wantErr: true, |
| 239 | }, { |
| 240 | desc: "double string with trailing space", |
| 241 | inputMessage: &pb3.Scalars{}, |
| 242 | inputText: `{"sDouble": "5.678 "}`, |
| 243 | wantErr: true, |
| 244 | }, { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 245 | desc: "not float", |
| 246 | inputMessage: &pb3.Scalars{}, |
| 247 | inputText: `{"sFloat": true}`, |
| 248 | wantErr: true, |
| 249 | }, { |
| 250 | desc: "not double", |
| 251 | inputMessage: &pb3.Scalars{}, |
| 252 | inputText: `{"sDouble": "not a number"}`, |
| 253 | wantErr: true, |
| 254 | }, { |
| 255 | desc: "integers", |
| 256 | inputMessage: &pb3.Scalars{}, |
| 257 | inputText: `{ |
| 258 | "sInt32": 1234, |
| 259 | "sInt64": -1234, |
| 260 | "sUint32": 1e2, |
| 261 | "sUint64": 100E-2, |
| 262 | "sSint32": 1.0, |
| 263 | "sSint64": -1.0, |
| 264 | "sFixed32": 1.234e+5, |
| 265 | "sFixed64": 1200E-2, |
| 266 | "sSfixed32": -1.234e05, |
| 267 | "sSfixed64": -1200e-02 |
| 268 | }`, |
| 269 | wantMessage: &pb3.Scalars{ |
| 270 | SInt32: 1234, |
| 271 | SInt64: -1234, |
| 272 | SUint32: 100, |
| 273 | SUint64: 1, |
| 274 | SSint32: 1, |
| 275 | SSint64: -1, |
| 276 | SFixed32: 123400, |
| 277 | SFixed64: 12, |
| 278 | SSfixed32: -123400, |
| 279 | SSfixed64: -12, |
| 280 | }, |
| 281 | }, { |
| 282 | desc: "integers in string", |
| 283 | inputMessage: &pb3.Scalars{}, |
| 284 | inputText: `{ |
| 285 | "sInt32": "1234", |
| 286 | "sInt64": "-1234", |
| 287 | "sUint32": "1e2", |
| 288 | "sUint64": "100E-2", |
| 289 | "sSint32": "1.0", |
| 290 | "sSint64": "-1.0", |
| 291 | "sFixed32": "1.234e+5", |
| 292 | "sFixed64": "1200E-2", |
| 293 | "sSfixed32": "-1.234e05", |
| 294 | "sSfixed64": "-1200e-02" |
| 295 | }`, |
| 296 | wantMessage: &pb3.Scalars{ |
| 297 | SInt32: 1234, |
| 298 | SInt64: -1234, |
| 299 | SUint32: 100, |
| 300 | SUint64: 1, |
| 301 | SSint32: 1, |
| 302 | SSint64: -1, |
| 303 | SFixed32: 123400, |
| 304 | SFixed64: 12, |
| 305 | SSfixed32: -123400, |
| 306 | SSfixed64: -12, |
| 307 | }, |
| 308 | }, { |
| 309 | desc: "integers in escaped string", |
| 310 | inputMessage: &pb3.Scalars{}, |
| 311 | inputText: `{"sInt32": "\u0031\u0032"}`, |
| 312 | wantMessage: &pb3.Scalars{ |
| 313 | SInt32: 12, |
| 314 | }, |
| 315 | }, { |
Herbie Ong | 06a6b0b | 2019-04-24 21:15:45 -0700 | [diff] [blame] | 316 | desc: "integer string with leading space", |
| 317 | inputMessage: &pb3.Scalars{}, |
| 318 | inputText: `{"sInt32": " 1234"}`, |
| 319 | wantErr: true, |
| 320 | }, { |
| 321 | desc: "integer string with trailing space", |
| 322 | inputMessage: &pb3.Scalars{}, |
| 323 | inputText: `{"sUint32": "1e2 "}`, |
| 324 | wantErr: true, |
| 325 | }, { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 326 | desc: "number is not an integer", |
| 327 | inputMessage: &pb3.Scalars{}, |
| 328 | inputText: `{"sInt32": 1.001}`, |
| 329 | wantErr: true, |
| 330 | }, { |
| 331 | desc: "32-bit int exceeds limit", |
| 332 | inputMessage: &pb3.Scalars{}, |
| 333 | inputText: `{"sInt32": 2e10}`, |
| 334 | wantErr: true, |
| 335 | }, { |
| 336 | desc: "64-bit int exceeds limit", |
| 337 | inputMessage: &pb3.Scalars{}, |
| 338 | inputText: `{"sSfixed64": -9e19}`, |
| 339 | wantErr: true, |
| 340 | }, { |
| 341 | desc: "not integer", |
| 342 | inputMessage: &pb3.Scalars{}, |
| 343 | inputText: `{"sInt32": "not a number"}`, |
| 344 | wantErr: true, |
| 345 | }, { |
| 346 | desc: "not unsigned integer", |
| 347 | inputMessage: &pb3.Scalars{}, |
| 348 | inputText: `{"sUint32": "not a number"}`, |
| 349 | wantErr: true, |
| 350 | }, { |
| 351 | desc: "number is not an unsigned integer", |
| 352 | inputMessage: &pb3.Scalars{}, |
| 353 | inputText: `{"sUint32": -1}`, |
| 354 | wantErr: true, |
| 355 | }, { |
| 356 | desc: "string", |
| 357 | inputMessage: &pb2.Scalars{}, |
| 358 | inputText: `{"optString": "è°·æŒ"}`, |
| 359 | wantMessage: &pb2.Scalars{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 360 | OptString: proto.String("è°·æŒ"), |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 361 | }, |
| 362 | }, { |
| 363 | desc: "string with invalid UTF-8", |
| 364 | inputMessage: &pb3.Scalars{}, |
| 365 | inputText: "{\"sString\": \"\xff\"}", |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 366 | wantErr: true, |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 367 | }, { |
| 368 | desc: "not string", |
| 369 | inputMessage: &pb2.Scalars{}, |
| 370 | inputText: `{"optString": 42}`, |
| 371 | wantErr: true, |
| 372 | }, { |
| 373 | desc: "bytes", |
| 374 | inputMessage: &pb3.Scalars{}, |
| 375 | inputText: `{"sBytes": "aGVsbG8gd29ybGQ"}`, |
| 376 | wantMessage: &pb3.Scalars{ |
| 377 | SBytes: []byte("hello world"), |
| 378 | }, |
| 379 | }, { |
| 380 | desc: "bytes padded", |
| 381 | inputMessage: &pb3.Scalars{}, |
| 382 | inputText: `{"sBytes": "aGVsbG8gd29ybGQ="}`, |
| 383 | wantMessage: &pb3.Scalars{ |
| 384 | SBytes: []byte("hello world"), |
| 385 | }, |
| 386 | }, { |
| 387 | desc: "not bytes", |
| 388 | inputMessage: &pb3.Scalars{}, |
| 389 | inputText: `{"sBytes": true}`, |
| 390 | wantErr: true, |
| 391 | }, { |
| 392 | desc: "proto2 enum", |
| 393 | inputMessage: &pb2.Enums{}, |
| 394 | inputText: `{ |
| 395 | "optEnum": "ONE", |
| 396 | "optNestedEnum": "UNO" |
| 397 | }`, |
| 398 | wantMessage: &pb2.Enums{ |
| 399 | OptEnum: pb2.Enum_ONE.Enum(), |
| 400 | OptNestedEnum: pb2.Enums_UNO.Enum(), |
| 401 | }, |
| 402 | }, { |
| 403 | desc: "proto3 enum", |
| 404 | inputMessage: &pb3.Enums{}, |
| 405 | inputText: `{ |
| 406 | "sEnum": "ONE", |
| 407 | "sNestedEnum": "DIEZ" |
| 408 | }`, |
| 409 | wantMessage: &pb3.Enums{ |
| 410 | SEnum: pb3.Enum_ONE, |
| 411 | SNestedEnum: pb3.Enums_DIEZ, |
| 412 | }, |
| 413 | }, { |
| 414 | desc: "enum numeric value", |
| 415 | inputMessage: &pb3.Enums{}, |
| 416 | inputText: `{ |
| 417 | "sEnum": 2, |
| 418 | "sNestedEnum": 2 |
| 419 | }`, |
| 420 | wantMessage: &pb3.Enums{ |
| 421 | SEnum: pb3.Enum_TWO, |
| 422 | SNestedEnum: pb3.Enums_DOS, |
| 423 | }, |
| 424 | }, { |
| 425 | desc: "enum unnamed numeric value", |
| 426 | inputMessage: &pb3.Enums{}, |
| 427 | inputText: `{ |
| 428 | "sEnum": 101, |
| 429 | "sNestedEnum": -101 |
| 430 | }`, |
| 431 | wantMessage: &pb3.Enums{ |
| 432 | SEnum: 101, |
| 433 | SNestedEnum: -101, |
| 434 | }, |
| 435 | }, { |
| 436 | desc: "enum set to number string", |
| 437 | inputMessage: &pb3.Enums{}, |
| 438 | inputText: `{ |
Herbie Ong | 300b9fe | 2019-03-29 15:42:20 -0700 | [diff] [blame] | 439 | "sEnum": "1" |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 440 | }`, |
| 441 | wantErr: true, |
| 442 | }, { |
| 443 | desc: "enum set to invalid named", |
| 444 | inputMessage: &pb3.Enums{}, |
| 445 | inputText: `{ |
Herbie Ong | 300b9fe | 2019-03-29 15:42:20 -0700 | [diff] [blame] | 446 | "sEnum": "UNNAMED" |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 447 | }`, |
| 448 | wantErr: true, |
| 449 | }, { |
| 450 | desc: "enum set to not enum", |
| 451 | inputMessage: &pb3.Enums{}, |
| 452 | inputText: `{ |
Herbie Ong | 300b9fe | 2019-03-29 15:42:20 -0700 | [diff] [blame] | 453 | "sEnum": true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 454 | }`, |
| 455 | wantErr: true, |
| 456 | }, { |
Herbie Ong | 300b9fe | 2019-03-29 15:42:20 -0700 | [diff] [blame] | 457 | desc: "enum set to JSON null", |
| 458 | inputMessage: &pb3.Enums{}, |
| 459 | inputText: `{ |
| 460 | "sEnum": null |
| 461 | }`, |
| 462 | wantMessage: &pb3.Enums{}, |
| 463 | }, { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 464 | desc: "proto name", |
| 465 | inputMessage: &pb3.JSONNames{}, |
| 466 | inputText: `{ |
| 467 | "s_string": "proto name used" |
| 468 | }`, |
| 469 | wantMessage: &pb3.JSONNames{ |
| 470 | SString: "proto name used", |
| 471 | }, |
| 472 | }, { |
Joe Tsai | 7fa1ee5 | 2019-09-15 00:32:55 -0700 | [diff] [blame^] | 473 | desc: "proto group name", |
| 474 | inputMessage: &pb2.Nests{}, |
| 475 | inputText: `{ |
| 476 | "OptGroup": {"optString": "hello"}, |
| 477 | "RptGroup": [{"rptString": ["goodbye"]}] |
| 478 | }`, |
| 479 | wantMessage: &pb2.Nests{ |
| 480 | Optgroup: &pb2.Nests_OptGroup{OptString: proto.String("hello")}, |
| 481 | Rptgroup: []*pb2.Nests_RptGroup{{RptString: []string{"goodbye"}}}, |
| 482 | }, |
| 483 | }, { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 484 | desc: "json_name", |
| 485 | inputMessage: &pb3.JSONNames{}, |
| 486 | inputText: `{ |
| 487 | "foo_bar": "json_name used" |
| 488 | }`, |
| 489 | wantMessage: &pb3.JSONNames{ |
| 490 | SString: "json_name used", |
| 491 | }, |
| 492 | }, { |
| 493 | desc: "camelCase name", |
| 494 | inputMessage: &pb3.JSONNames{}, |
| 495 | inputText: `{ |
| 496 | "sString": "camelcase used" |
| 497 | }`, |
| 498 | wantErr: true, |
| 499 | }, { |
| 500 | desc: "proto name and json_name", |
| 501 | inputMessage: &pb3.JSONNames{}, |
| 502 | inputText: `{ |
| 503 | "foo_bar": "json_name used", |
| 504 | "s_string": "proto name used" |
| 505 | }`, |
| 506 | wantErr: true, |
| 507 | }, { |
| 508 | desc: "duplicate field names", |
| 509 | inputMessage: &pb3.JSONNames{}, |
| 510 | inputText: `{ |
| 511 | "foo_bar": "one", |
| 512 | "foo_bar": "two", |
| 513 | }`, |
| 514 | wantErr: true, |
| 515 | }, { |
| 516 | desc: "null message", |
| 517 | inputMessage: &pb2.Nests{}, |
| 518 | inputText: "null", |
| 519 | wantErr: true, |
| 520 | }, { |
| 521 | desc: "proto2 nested message not set", |
| 522 | inputMessage: &pb2.Nests{}, |
| 523 | inputText: "{}", |
| 524 | wantMessage: &pb2.Nests{}, |
| 525 | }, { |
| 526 | desc: "proto2 nested message set to null", |
| 527 | inputMessage: &pb2.Nests{}, |
| 528 | inputText: `{ |
| 529 | "optNested": null, |
| 530 | "optgroup": null |
| 531 | }`, |
| 532 | wantMessage: &pb2.Nests{}, |
| 533 | }, { |
| 534 | desc: "proto2 nested message set to empty", |
| 535 | inputMessage: &pb2.Nests{}, |
| 536 | inputText: `{ |
| 537 | "optNested": {}, |
| 538 | "optgroup": {} |
| 539 | }`, |
| 540 | wantMessage: &pb2.Nests{ |
| 541 | OptNested: &pb2.Nested{}, |
| 542 | Optgroup: &pb2.Nests_OptGroup{}, |
| 543 | }, |
| 544 | }, { |
| 545 | desc: "proto2 nested messages", |
| 546 | inputMessage: &pb2.Nests{}, |
| 547 | inputText: `{ |
| 548 | "optNested": { |
| 549 | "optString": "nested message", |
| 550 | "optNested": { |
| 551 | "optString": "another nested message" |
| 552 | } |
| 553 | } |
| 554 | }`, |
| 555 | wantMessage: &pb2.Nests{ |
| 556 | OptNested: &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 557 | OptString: proto.String("nested message"), |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 558 | OptNested: &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 559 | OptString: proto.String("another nested message"), |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 560 | }, |
| 561 | }, |
| 562 | }, |
| 563 | }, { |
| 564 | desc: "proto2 groups", |
| 565 | inputMessage: &pb2.Nests{}, |
| 566 | inputText: `{ |
| 567 | "optgroup": { |
| 568 | "optString": "inside a group", |
| 569 | "optNested": { |
| 570 | "optString": "nested message inside a group" |
| 571 | }, |
| 572 | "optnestedgroup": { |
| 573 | "optFixed32": 47 |
| 574 | } |
| 575 | } |
| 576 | }`, |
| 577 | wantMessage: &pb2.Nests{ |
| 578 | Optgroup: &pb2.Nests_OptGroup{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 579 | OptString: proto.String("inside a group"), |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 580 | OptNested: &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 581 | OptString: proto.String("nested message inside a group"), |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 582 | }, |
| 583 | Optnestedgroup: &pb2.Nests_OptGroup_OptNestedGroup{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 584 | OptFixed32: proto.Uint32(47), |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 585 | }, |
| 586 | }, |
| 587 | }, |
| 588 | }, { |
| 589 | desc: "proto3 nested message not set", |
| 590 | inputMessage: &pb3.Nests{}, |
| 591 | inputText: "{}", |
| 592 | wantMessage: &pb3.Nests{}, |
| 593 | }, { |
| 594 | desc: "proto3 nested message set to null", |
| 595 | inputMessage: &pb3.Nests{}, |
| 596 | inputText: `{"sNested": null}`, |
| 597 | wantMessage: &pb3.Nests{}, |
| 598 | }, { |
| 599 | desc: "proto3 nested message set to empty", |
| 600 | inputMessage: &pb3.Nests{}, |
| 601 | inputText: `{"sNested": {}}`, |
| 602 | wantMessage: &pb3.Nests{ |
| 603 | SNested: &pb3.Nested{}, |
| 604 | }, |
| 605 | }, { |
| 606 | desc: "proto3 nested message", |
| 607 | inputMessage: &pb3.Nests{}, |
| 608 | inputText: `{ |
| 609 | "sNested": { |
| 610 | "sString": "nested message", |
| 611 | "sNested": { |
| 612 | "sString": "another nested message" |
| 613 | } |
| 614 | } |
| 615 | }`, |
| 616 | wantMessage: &pb3.Nests{ |
| 617 | SNested: &pb3.Nested{ |
| 618 | SString: "nested message", |
| 619 | SNested: &pb3.Nested{ |
| 620 | SString: "another nested message", |
| 621 | }, |
| 622 | }, |
| 623 | }, |
| 624 | }, { |
| 625 | desc: "message set to non-message", |
| 626 | inputMessage: &pb3.Nests{}, |
| 627 | inputText: `"not valid"`, |
| 628 | wantErr: true, |
| 629 | }, { |
| 630 | desc: "nested message set to non-message", |
| 631 | inputMessage: &pb3.Nests{}, |
| 632 | inputText: `{"sNested": true}`, |
| 633 | wantErr: true, |
| 634 | }, { |
| 635 | desc: "oneof not set", |
| 636 | inputMessage: &pb3.Oneofs{}, |
| 637 | inputText: "{}", |
| 638 | wantMessage: &pb3.Oneofs{}, |
| 639 | }, { |
| 640 | desc: "oneof set to empty string", |
| 641 | inputMessage: &pb3.Oneofs{}, |
| 642 | inputText: `{"oneofString": ""}`, |
| 643 | wantMessage: &pb3.Oneofs{ |
| 644 | Union: &pb3.Oneofs_OneofString{}, |
| 645 | }, |
| 646 | }, { |
| 647 | desc: "oneof set to string", |
| 648 | inputMessage: &pb3.Oneofs{}, |
| 649 | inputText: `{"oneofString": "hello"}`, |
| 650 | wantMessage: &pb3.Oneofs{ |
| 651 | Union: &pb3.Oneofs_OneofString{ |
| 652 | OneofString: "hello", |
| 653 | }, |
| 654 | }, |
| 655 | }, { |
| 656 | desc: "oneof set to enum", |
| 657 | inputMessage: &pb3.Oneofs{}, |
| 658 | inputText: `{"oneofEnum": "ZERO"}`, |
| 659 | wantMessage: &pb3.Oneofs{ |
| 660 | Union: &pb3.Oneofs_OneofEnum{ |
| 661 | OneofEnum: pb3.Enum_ZERO, |
| 662 | }, |
| 663 | }, |
| 664 | }, { |
| 665 | desc: "oneof set to empty message", |
| 666 | inputMessage: &pb3.Oneofs{}, |
| 667 | inputText: `{"oneofNested": {}}`, |
| 668 | wantMessage: &pb3.Oneofs{ |
| 669 | Union: &pb3.Oneofs_OneofNested{ |
| 670 | OneofNested: &pb3.Nested{}, |
| 671 | }, |
| 672 | }, |
| 673 | }, { |
| 674 | desc: "oneof set to message", |
| 675 | inputMessage: &pb3.Oneofs{}, |
| 676 | inputText: `{ |
| 677 | "oneofNested": { |
| 678 | "sString": "nested message" |
| 679 | } |
| 680 | }`, |
| 681 | wantMessage: &pb3.Oneofs{ |
| 682 | Union: &pb3.Oneofs_OneofNested{ |
| 683 | OneofNested: &pb3.Nested{ |
| 684 | SString: "nested message", |
| 685 | }, |
| 686 | }, |
| 687 | }, |
| 688 | }, { |
Herbie Ong | 8a1d460 | 2019-04-02 20:19:36 -0700 | [diff] [blame] | 689 | desc: "oneof set to more than one field", |
| 690 | inputMessage: &pb3.Oneofs{}, |
| 691 | inputText: `{ |
| 692 | "oneofEnum": "ZERO", |
| 693 | "oneofString": "hello" |
| 694 | }`, |
| 695 | wantErr: true, |
| 696 | }, { |
| 697 | desc: "oneof set to null and value", |
| 698 | inputMessage: &pb3.Oneofs{}, |
| 699 | inputText: `{ |
| 700 | "oneofEnum": "ZERO", |
| 701 | "oneofString": null |
| 702 | }`, |
| 703 | wantMessage: &pb3.Oneofs{ |
| 704 | Union: &pb3.Oneofs_OneofEnum{ |
| 705 | OneofEnum: pb3.Enum_ZERO, |
| 706 | }, |
| 707 | }, |
| 708 | }, { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 709 | desc: "repeated null fields", |
| 710 | inputMessage: &pb2.Repeats{}, |
| 711 | inputText: `{ |
| 712 | "rptString": null, |
| 713 | "rptInt32" : null, |
| 714 | "rptFloat" : null, |
| 715 | "rptBytes" : null |
| 716 | }`, |
| 717 | wantMessage: &pb2.Repeats{}, |
| 718 | }, { |
| 719 | desc: "repeated scalars", |
| 720 | inputMessage: &pb2.Repeats{}, |
| 721 | inputText: `{ |
| 722 | "rptString": ["hello", "world"], |
| 723 | "rptInt32" : [-1, 0, 1], |
| 724 | "rptBool" : [false, true] |
| 725 | }`, |
| 726 | wantMessage: &pb2.Repeats{ |
| 727 | RptString: []string{"hello", "world"}, |
| 728 | RptInt32: []int32{-1, 0, 1}, |
| 729 | RptBool: []bool{false, true}, |
| 730 | }, |
| 731 | }, { |
| 732 | desc: "repeated enums", |
| 733 | inputMessage: &pb2.Enums{}, |
| 734 | inputText: `{ |
| 735 | "rptEnum" : ["TEN", 1, 42], |
| 736 | "rptNestedEnum": ["DOS", 2, -47] |
| 737 | }`, |
| 738 | wantMessage: &pb2.Enums{ |
| 739 | RptEnum: []pb2.Enum{pb2.Enum_TEN, pb2.Enum_ONE, 42}, |
| 740 | RptNestedEnum: []pb2.Enums_NestedEnum{pb2.Enums_DOS, pb2.Enums_DOS, -47}, |
| 741 | }, |
| 742 | }, { |
| 743 | desc: "repeated messages", |
| 744 | inputMessage: &pb2.Nests{}, |
| 745 | inputText: `{ |
| 746 | "rptNested": [ |
| 747 | { |
| 748 | "optString": "repeat nested one" |
| 749 | }, |
| 750 | { |
| 751 | "optString": "repeat nested two", |
| 752 | "optNested": { |
| 753 | "optString": "inside repeat nested two" |
| 754 | } |
| 755 | }, |
| 756 | {} |
| 757 | ] |
| 758 | }`, |
| 759 | wantMessage: &pb2.Nests{ |
| 760 | RptNested: []*pb2.Nested{ |
| 761 | { |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 762 | OptString: proto.String("repeat nested one"), |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 763 | }, |
| 764 | { |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 765 | OptString: proto.String("repeat nested two"), |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 766 | OptNested: &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 767 | OptString: proto.String("inside repeat nested two"), |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 768 | }, |
| 769 | }, |
| 770 | {}, |
| 771 | }, |
| 772 | }, |
| 773 | }, { |
| 774 | desc: "repeated groups", |
| 775 | inputMessage: &pb2.Nests{}, |
| 776 | inputText: `{ |
| 777 | "rptgroup": [ |
| 778 | { |
| 779 | "rptString": ["hello", "world"] |
| 780 | }, |
| 781 | {} |
| 782 | ] |
| 783 | } |
| 784 | `, |
| 785 | wantMessage: &pb2.Nests{ |
| 786 | Rptgroup: []*pb2.Nests_RptGroup{ |
| 787 | { |
| 788 | RptString: []string{"hello", "world"}, |
| 789 | }, |
| 790 | {}, |
| 791 | }, |
| 792 | }, |
| 793 | }, { |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 794 | desc: "repeated string contains invalid UTF8", |
| 795 | inputMessage: &pb2.Repeats{}, |
| 796 | inputText: `{"rptString": ["` + "abc\xff" + `"]}`, |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 797 | wantErr: true, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 798 | }, { |
| 799 | desc: "repeated messages contain invalid UTF8", |
| 800 | inputMessage: &pb2.Nests{}, |
| 801 | inputText: `{"rptNested": [{"optString": "` + "abc\xff" + `"}]}`, |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 802 | wantErr: true, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 803 | }, { |
| 804 | desc: "repeated scalars contain invalid type", |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 805 | inputMessage: &pb2.Repeats{}, |
| 806 | inputText: `{"rptString": ["hello", null, "world"]}`, |
| 807 | wantErr: true, |
| 808 | }, { |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 809 | desc: "repeated messages contain invalid type", |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 810 | inputMessage: &pb2.Nests{}, |
| 811 | inputText: `{"rptNested": [{}, null]}`, |
| 812 | wantErr: true, |
| 813 | }, { |
| 814 | desc: "map fields 1", |
| 815 | inputMessage: &pb3.Maps{}, |
| 816 | inputText: `{ |
| 817 | "int32ToStr": { |
| 818 | "-101": "-101", |
| 819 | "0" : "zero", |
| 820 | "255" : "0xff" |
| 821 | }, |
| 822 | "boolToUint32": { |
| 823 | "false": 101, |
| 824 | "true" : "42" |
| 825 | } |
| 826 | }`, |
| 827 | wantMessage: &pb3.Maps{ |
| 828 | Int32ToStr: map[int32]string{ |
| 829 | -101: "-101", |
| 830 | 0xff: "0xff", |
| 831 | 0: "zero", |
| 832 | }, |
| 833 | BoolToUint32: map[bool]uint32{ |
| 834 | true: 42, |
| 835 | false: 101, |
| 836 | }, |
| 837 | }, |
| 838 | }, { |
| 839 | desc: "map fields 2", |
| 840 | inputMessage: &pb3.Maps{}, |
| 841 | inputText: `{ |
| 842 | "uint64ToEnum": { |
| 843 | "1" : "ONE", |
| 844 | "2" : 2, |
| 845 | "10": 101 |
| 846 | } |
| 847 | }`, |
| 848 | wantMessage: &pb3.Maps{ |
| 849 | Uint64ToEnum: map[uint64]pb3.Enum{ |
| 850 | 1: pb3.Enum_ONE, |
| 851 | 2: pb3.Enum_TWO, |
| 852 | 10: 101, |
| 853 | }, |
| 854 | }, |
| 855 | }, { |
| 856 | desc: "map fields 3", |
| 857 | inputMessage: &pb3.Maps{}, |
| 858 | inputText: `{ |
| 859 | "strToNested": { |
| 860 | "nested_one": { |
| 861 | "sString": "nested in a map" |
| 862 | }, |
| 863 | "nested_two": {} |
| 864 | } |
| 865 | }`, |
| 866 | wantMessage: &pb3.Maps{ |
| 867 | StrToNested: map[string]*pb3.Nested{ |
| 868 | "nested_one": { |
| 869 | SString: "nested in a map", |
| 870 | }, |
| 871 | "nested_two": {}, |
| 872 | }, |
| 873 | }, |
| 874 | }, { |
| 875 | desc: "map fields 4", |
| 876 | inputMessage: &pb3.Maps{}, |
| 877 | inputText: `{ |
| 878 | "strToOneofs": { |
| 879 | "nested": { |
| 880 | "oneofNested": { |
| 881 | "sString": "nested oneof in map field value" |
| 882 | } |
| 883 | }, |
| 884 | "string": { |
| 885 | "oneofString": "hello" |
| 886 | } |
| 887 | } |
| 888 | }`, |
| 889 | wantMessage: &pb3.Maps{ |
| 890 | StrToOneofs: map[string]*pb3.Oneofs{ |
| 891 | "string": { |
| 892 | Union: &pb3.Oneofs_OneofString{ |
| 893 | OneofString: "hello", |
| 894 | }, |
| 895 | }, |
| 896 | "nested": { |
| 897 | Union: &pb3.Oneofs_OneofNested{ |
| 898 | OneofNested: &pb3.Nested{ |
| 899 | SString: "nested oneof in map field value", |
| 900 | }, |
| 901 | }, |
| 902 | }, |
| 903 | }, |
| 904 | }, |
| 905 | }, { |
| 906 | desc: "map contains duplicate keys", |
| 907 | inputMessage: &pb3.Maps{}, |
| 908 | inputText: `{ |
| 909 | "int32ToStr": { |
| 910 | "0": "cero", |
| 911 | "0": "zero" |
| 912 | } |
| 913 | } |
| 914 | `, |
| 915 | wantErr: true, |
| 916 | }, { |
| 917 | desc: "map key empty string", |
| 918 | inputMessage: &pb3.Maps{}, |
| 919 | inputText: `{ |
| 920 | "strToNested": { |
| 921 | "": {} |
| 922 | } |
| 923 | }`, |
| 924 | wantMessage: &pb3.Maps{ |
| 925 | StrToNested: map[string]*pb3.Nested{ |
| 926 | "": {}, |
| 927 | }, |
| 928 | }, |
| 929 | }, { |
| 930 | desc: "map contains invalid key 1", |
| 931 | inputMessage: &pb3.Maps{}, |
| 932 | inputText: `{ |
| 933 | "int32ToStr": { |
| 934 | "invalid": "cero" |
| 935 | }`, |
| 936 | wantErr: true, |
| 937 | }, { |
| 938 | desc: "map contains invalid key 2", |
| 939 | inputMessage: &pb3.Maps{}, |
| 940 | inputText: `{ |
| 941 | "int32ToStr": { |
| 942 | "1.02": "float" |
| 943 | }`, |
| 944 | wantErr: true, |
| 945 | }, { |
| 946 | desc: "map contains invalid key 3", |
| 947 | inputMessage: &pb3.Maps{}, |
| 948 | inputText: `{ |
| 949 | "int32ToStr": { |
| 950 | "2147483648": "exceeds 32-bit integer max limit" |
| 951 | }`, |
| 952 | wantErr: true, |
| 953 | }, { |
| 954 | desc: "map contains invalid key 4", |
| 955 | inputMessage: &pb3.Maps{}, |
| 956 | inputText: `{ |
| 957 | "uint64ToEnum": { |
| 958 | "-1": 0 |
| 959 | } |
| 960 | }`, |
| 961 | wantErr: true, |
| 962 | }, { |
| 963 | desc: "map contains invalid value", |
| 964 | inputMessage: &pb3.Maps{}, |
| 965 | inputText: `{ |
| 966 | "int32ToStr": { |
| 967 | "101": true |
| 968 | }`, |
| 969 | wantErr: true, |
| 970 | }, { |
| 971 | desc: "map contains null for scalar value", |
| 972 | inputMessage: &pb3.Maps{}, |
| 973 | inputText: `{ |
| 974 | "int32ToStr": { |
| 975 | "101": null |
| 976 | }`, |
| 977 | wantErr: true, |
| 978 | }, { |
| 979 | desc: "map contains null for message value", |
| 980 | inputMessage: &pb3.Maps{}, |
| 981 | inputText: `{ |
| 982 | "strToNested": { |
| 983 | "hello": null |
| 984 | } |
| 985 | }`, |
| 986 | wantErr: true, |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 987 | }, { |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 988 | desc: "map contains contains message value with invalid UTF8", |
| 989 | inputMessage: &pb3.Maps{}, |
| 990 | inputText: `{ |
| 991 | "strToNested": { |
| 992 | "hello": { |
| 993 | "sString": "` + "abc\xff" + `" |
| 994 | } |
| 995 | } |
| 996 | }`, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 997 | wantErr: true, |
| 998 | }, { |
| 999 | desc: "map key contains invalid UTF8", |
| 1000 | inputMessage: &pb3.Maps{}, |
| 1001 | inputText: `{ |
| 1002 | "strToNested": { |
| 1003 | "` + "abc\xff" + `": {} |
| 1004 | } |
| 1005 | }`, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1006 | wantErr: true, |
| 1007 | }, { |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 1008 | desc: "required fields not set", |
| 1009 | inputMessage: &pb2.Requireds{}, |
| 1010 | wantErr: true, |
| 1011 | }, { |
| 1012 | desc: "required field set", |
| 1013 | inputMessage: &pb2.PartialRequired{}, |
| 1014 | inputText: `{ |
| 1015 | "reqString": "this is required" |
| 1016 | }`, |
| 1017 | wantMessage: &pb2.PartialRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1018 | ReqString: proto.String("this is required"), |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 1019 | }, |
| 1020 | }, { |
| 1021 | desc: "required fields partially set", |
| 1022 | inputMessage: &pb2.Requireds{}, |
| 1023 | inputText: `{ |
| 1024 | "reqBool": false, |
| 1025 | "reqSfixed64": 42, |
| 1026 | "reqString": "hello", |
| 1027 | "reqEnum": "ONE" |
| 1028 | }`, |
| 1029 | wantMessage: &pb2.Requireds{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1030 | ReqBool: proto.Bool(false), |
| 1031 | ReqSfixed64: proto.Int64(42), |
| 1032 | ReqString: proto.String("hello"), |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 1033 | ReqEnum: pb2.Enum_ONE.Enum(), |
| 1034 | }, |
| 1035 | wantErr: true, |
| 1036 | }, { |
| 1037 | desc: "required fields partially set with AllowPartial", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1038 | umo: protojson.UnmarshalOptions{AllowPartial: true}, |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 1039 | inputMessage: &pb2.Requireds{}, |
| 1040 | inputText: `{ |
| 1041 | "reqBool": false, |
| 1042 | "reqSfixed64": 42, |
| 1043 | "reqString": "hello", |
| 1044 | "reqEnum": "ONE" |
| 1045 | }`, |
| 1046 | wantMessage: &pb2.Requireds{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1047 | ReqBool: proto.Bool(false), |
| 1048 | ReqSfixed64: proto.Int64(42), |
| 1049 | ReqString: proto.String("hello"), |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 1050 | ReqEnum: pb2.Enum_ONE.Enum(), |
| 1051 | }, |
| 1052 | }, { |
| 1053 | desc: "required fields all set", |
| 1054 | inputMessage: &pb2.Requireds{}, |
| 1055 | inputText: `{ |
| 1056 | "reqBool": false, |
| 1057 | "reqSfixed64": 42, |
| 1058 | "reqDouble": 1.23, |
| 1059 | "reqString": "hello", |
| 1060 | "reqEnum": "ONE", |
| 1061 | "reqNested": {} |
| 1062 | }`, |
| 1063 | wantMessage: &pb2.Requireds{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1064 | ReqBool: proto.Bool(false), |
| 1065 | ReqSfixed64: proto.Int64(42), |
| 1066 | ReqDouble: proto.Float64(1.23), |
| 1067 | ReqString: proto.String("hello"), |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 1068 | ReqEnum: pb2.Enum_ONE.Enum(), |
| 1069 | ReqNested: &pb2.Nested{}, |
| 1070 | }, |
| 1071 | }, { |
| 1072 | desc: "indirect required field", |
| 1073 | inputMessage: &pb2.IndirectRequired{}, |
| 1074 | inputText: `{ |
| 1075 | "optNested": {} |
| 1076 | }`, |
| 1077 | wantMessage: &pb2.IndirectRequired{ |
| 1078 | OptNested: &pb2.NestedWithRequired{}, |
| 1079 | }, |
| 1080 | wantErr: true, |
| 1081 | }, { |
| 1082 | desc: "indirect required field with AllowPartial", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1083 | umo: protojson.UnmarshalOptions{AllowPartial: true}, |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 1084 | inputMessage: &pb2.IndirectRequired{}, |
| 1085 | inputText: `{ |
| 1086 | "optNested": {} |
| 1087 | }`, |
| 1088 | wantMessage: &pb2.IndirectRequired{ |
| 1089 | OptNested: &pb2.NestedWithRequired{}, |
| 1090 | }, |
| 1091 | }, { |
| 1092 | desc: "indirect required field in repeated", |
| 1093 | inputMessage: &pb2.IndirectRequired{}, |
| 1094 | inputText: `{ |
| 1095 | "rptNested": [ |
| 1096 | {"reqString": "one"}, |
| 1097 | {} |
| 1098 | ] |
| 1099 | }`, |
| 1100 | wantMessage: &pb2.IndirectRequired{ |
| 1101 | RptNested: []*pb2.NestedWithRequired{ |
| 1102 | { |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1103 | ReqString: proto.String("one"), |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 1104 | }, |
| 1105 | {}, |
| 1106 | }, |
| 1107 | }, |
| 1108 | wantErr: true, |
| 1109 | }, { |
| 1110 | desc: "indirect required field in repeated with AllowPartial", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1111 | umo: protojson.UnmarshalOptions{AllowPartial: true}, |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 1112 | inputMessage: &pb2.IndirectRequired{}, |
| 1113 | inputText: `{ |
| 1114 | "rptNested": [ |
| 1115 | {"reqString": "one"}, |
| 1116 | {} |
| 1117 | ] |
| 1118 | }`, |
| 1119 | wantMessage: &pb2.IndirectRequired{ |
| 1120 | RptNested: []*pb2.NestedWithRequired{ |
| 1121 | { |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1122 | ReqString: proto.String("one"), |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 1123 | }, |
| 1124 | {}, |
| 1125 | }, |
| 1126 | }, |
| 1127 | }, { |
| 1128 | desc: "indirect required field in map", |
| 1129 | inputMessage: &pb2.IndirectRequired{}, |
| 1130 | inputText: `{ |
| 1131 | "strToNested": { |
| 1132 | "missing": {}, |
| 1133 | "contains": { |
| 1134 | "reqString": "here" |
| 1135 | } |
| 1136 | } |
| 1137 | }`, |
| 1138 | wantMessage: &pb2.IndirectRequired{ |
| 1139 | StrToNested: map[string]*pb2.NestedWithRequired{ |
| 1140 | "missing": &pb2.NestedWithRequired{}, |
| 1141 | "contains": &pb2.NestedWithRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1142 | ReqString: proto.String("here"), |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 1143 | }, |
| 1144 | }, |
| 1145 | }, |
| 1146 | wantErr: true, |
| 1147 | }, { |
| 1148 | desc: "indirect required field in map with AllowPartial", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1149 | umo: protojson.UnmarshalOptions{AllowPartial: true}, |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 1150 | inputMessage: &pb2.IndirectRequired{}, |
| 1151 | inputText: `{ |
| 1152 | "strToNested": { |
| 1153 | "missing": {}, |
| 1154 | "contains": { |
| 1155 | "reqString": "here" |
| 1156 | } |
| 1157 | } |
| 1158 | }`, |
| 1159 | wantMessage: &pb2.IndirectRequired{ |
| 1160 | StrToNested: map[string]*pb2.NestedWithRequired{ |
| 1161 | "missing": &pb2.NestedWithRequired{}, |
| 1162 | "contains": &pb2.NestedWithRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1163 | ReqString: proto.String("here"), |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 1164 | }, |
| 1165 | }, |
| 1166 | }, |
| 1167 | }, { |
| 1168 | desc: "indirect required field in oneof", |
| 1169 | inputMessage: &pb2.IndirectRequired{}, |
| 1170 | inputText: `{ |
| 1171 | "oneofNested": {} |
| 1172 | }`, |
| 1173 | wantMessage: &pb2.IndirectRequired{ |
| 1174 | Union: &pb2.IndirectRequired_OneofNested{ |
| 1175 | OneofNested: &pb2.NestedWithRequired{}, |
| 1176 | }, |
| 1177 | }, |
| 1178 | wantErr: true, |
| 1179 | }, { |
| 1180 | desc: "indirect required field in oneof with AllowPartial", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1181 | umo: protojson.UnmarshalOptions{AllowPartial: true}, |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 1182 | inputMessage: &pb2.IndirectRequired{}, |
| 1183 | inputText: `{ |
| 1184 | "oneofNested": {} |
| 1185 | }`, |
| 1186 | wantMessage: &pb2.IndirectRequired{ |
| 1187 | Union: &pb2.IndirectRequired_OneofNested{ |
| 1188 | OneofNested: &pb2.NestedWithRequired{}, |
| 1189 | }, |
| 1190 | }, |
| 1191 | }, { |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1192 | desc: "extensions of non-repeated fields", |
| 1193 | inputMessage: &pb2.Extensions{}, |
| 1194 | inputText: `{ |
| 1195 | "optString": "non-extension field", |
| 1196 | "optBool": true, |
| 1197 | "optInt32": 42, |
| 1198 | "[pb2.opt_ext_bool]": true, |
| 1199 | "[pb2.opt_ext_nested]": { |
| 1200 | "optString": "nested in an extension", |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1201 | "optNested": { |
| 1202 | "optString": "another nested in an extension" |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1203 | } |
| 1204 | }, |
| 1205 | "[pb2.opt_ext_string]": "extension field", |
| 1206 | "[pb2.opt_ext_enum]": "TEN" |
| 1207 | }`, |
| 1208 | wantMessage: func() proto.Message { |
| 1209 | m := &pb2.Extensions{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1210 | OptString: proto.String("non-extension field"), |
| 1211 | OptBool: proto.Bool(true), |
| 1212 | OptInt32: proto.Int32(42), |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1213 | } |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 1214 | proto.SetExtension(m, pb2.E_OptExtBool, true) |
| 1215 | proto.SetExtension(m, pb2.E_OptExtString, "extension field") |
| 1216 | proto.SetExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN) |
| 1217 | proto.SetExtension(m, pb2.E_OptExtNested, &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1218 | OptString: proto.String("nested in an extension"), |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1219 | OptNested: &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1220 | OptString: proto.String("another nested in an extension"), |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1221 | }, |
| 1222 | }) |
| 1223 | return m |
| 1224 | }(), |
| 1225 | }, { |
| 1226 | desc: "extensions of repeated fields", |
| 1227 | inputMessage: &pb2.Extensions{}, |
| 1228 | inputText: `{ |
| 1229 | "[pb2.rpt_ext_enum]": ["TEN", 101, "ONE"], |
| 1230 | "[pb2.rpt_ext_fixed32]": [42, 47], |
| 1231 | "[pb2.rpt_ext_nested]": [ |
| 1232 | {"optString": "one"}, |
| 1233 | {"optString": "two"}, |
| 1234 | {"optString": "three"} |
| 1235 | ] |
| 1236 | }`, |
| 1237 | wantMessage: func() proto.Message { |
| 1238 | m := &pb2.Extensions{} |
Damien Neil | 293dc76 | 2019-08-29 11:42:57 -0700 | [diff] [blame] | 1239 | proto.SetExtension(m, pb2.E_RptExtEnum, []pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE}) |
| 1240 | proto.SetExtension(m, pb2.E_RptExtFixed32, []uint32{42, 47}) |
| 1241 | proto.SetExtension(m, pb2.E_RptExtNested, []*pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1242 | &pb2.Nested{OptString: proto.String("one")}, |
| 1243 | &pb2.Nested{OptString: proto.String("two")}, |
| 1244 | &pb2.Nested{OptString: proto.String("three")}, |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1245 | }) |
| 1246 | return m |
| 1247 | }(), |
| 1248 | }, { |
| 1249 | desc: "extensions of non-repeated fields in another message", |
| 1250 | inputMessage: &pb2.Extensions{}, |
| 1251 | inputText: `{ |
| 1252 | "[pb2.ExtensionsContainer.opt_ext_bool]": true, |
| 1253 | "[pb2.ExtensionsContainer.opt_ext_enum]": "TEN", |
| 1254 | "[pb2.ExtensionsContainer.opt_ext_nested]": { |
| 1255 | "optString": "nested in an extension", |
| 1256 | "optNested": { |
| 1257 | "optString": "another nested in an extension" |
| 1258 | } |
| 1259 | }, |
| 1260 | "[pb2.ExtensionsContainer.opt_ext_string]": "extension field" |
| 1261 | }`, |
| 1262 | wantMessage: func() proto.Message { |
| 1263 | m := &pb2.Extensions{} |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 1264 | proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true) |
| 1265 | proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field") |
| 1266 | proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN) |
| 1267 | proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1268 | OptString: proto.String("nested in an extension"), |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1269 | OptNested: &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1270 | OptString: proto.String("another nested in an extension"), |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1271 | }, |
| 1272 | }) |
| 1273 | return m |
| 1274 | }(), |
| 1275 | }, { |
| 1276 | desc: "extensions of repeated fields in another message", |
| 1277 | inputMessage: &pb2.Extensions{}, |
| 1278 | inputText: `{ |
| 1279 | "optString": "non-extension field", |
| 1280 | "optBool": true, |
| 1281 | "optInt32": 42, |
| 1282 | "[pb2.ExtensionsContainer.rpt_ext_nested]": [ |
| 1283 | {"optString": "one"}, |
| 1284 | {"optString": "two"}, |
| 1285 | {"optString": "three"} |
| 1286 | ], |
| 1287 | "[pb2.ExtensionsContainer.rpt_ext_enum]": ["TEN", 101, "ONE"], |
| 1288 | "[pb2.ExtensionsContainer.rpt_ext_string]": ["hello", "world"] |
| 1289 | }`, |
| 1290 | wantMessage: func() proto.Message { |
| 1291 | m := &pb2.Extensions{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1292 | OptString: proto.String("non-extension field"), |
| 1293 | OptBool: proto.Bool(true), |
| 1294 | OptInt32: proto.Int32(42), |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1295 | } |
Damien Neil | 293dc76 | 2019-08-29 11:42:57 -0700 | [diff] [blame] | 1296 | proto.SetExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, []pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE}) |
| 1297 | proto.SetExtension(m, pb2.E_ExtensionsContainer_RptExtString, []string{"hello", "world"}) |
| 1298 | proto.SetExtension(m, pb2.E_ExtensionsContainer_RptExtNested, []*pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1299 | &pb2.Nested{OptString: proto.String("one")}, |
| 1300 | &pb2.Nested{OptString: proto.String("two")}, |
| 1301 | &pb2.Nested{OptString: proto.String("three")}, |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1302 | }) |
| 1303 | return m |
| 1304 | }(), |
| 1305 | }, { |
| 1306 | desc: "invalid extension field name", |
| 1307 | inputMessage: &pb2.Extensions{}, |
| 1308 | inputText: `{ "[pb2.invalid_message_field]": true }`, |
| 1309 | wantErr: true, |
| 1310 | }, { |
Joe Tsai | 5ae10aa | 2019-07-11 18:23:08 -0700 | [diff] [blame] | 1311 | desc: "extensions of repeated field contains null", |
| 1312 | inputMessage: &pb2.Extensions{}, |
| 1313 | inputText: `{ |
| 1314 | "[pb2.ExtensionsContainer.rpt_ext_nested]": [ |
| 1315 | {"optString": "one"}, |
| 1316 | null, |
| 1317 | {"optString": "three"} |
| 1318 | ], |
| 1319 | }`, |
| 1320 | wantErr: true, |
| 1321 | }, { |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1322 | desc: "MessageSet", |
| 1323 | inputMessage: &pb2.MessageSet{}, |
| 1324 | inputText: `{ |
| 1325 | "[pb2.MessageSetExtension]": { |
| 1326 | "optString": "a messageset extension" |
| 1327 | }, |
| 1328 | "[pb2.MessageSetExtension.ext_nested]": { |
| 1329 | "optString": "just a regular extension" |
| 1330 | }, |
| 1331 | "[pb2.MessageSetExtension.not_message_set_extension]": { |
| 1332 | "optString": "not a messageset extension" |
| 1333 | } |
| 1334 | }`, |
| 1335 | wantMessage: func() proto.Message { |
| 1336 | m := &pb2.MessageSet{} |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 1337 | proto.SetExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1338 | OptString: proto.String("a messageset extension"), |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1339 | }) |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 1340 | proto.SetExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1341 | OptString: proto.String("not a messageset extension"), |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1342 | }) |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 1343 | proto.SetExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1344 | OptString: proto.String("just a regular extension"), |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1345 | }) |
| 1346 | return m |
| 1347 | }(), |
Joe Tsai | 1799d11 | 2019-08-08 13:31:59 -0700 | [diff] [blame] | 1348 | skip: !flags.ProtoLegacy, |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1349 | }, { |
| 1350 | desc: "not real MessageSet 1", |
| 1351 | inputMessage: &pb2.FakeMessageSet{}, |
| 1352 | inputText: `{ |
| 1353 | "[pb2.FakeMessageSetExtension.message_set_extension]": { |
| 1354 | "optString": "not a messageset extension" |
| 1355 | } |
| 1356 | }`, |
| 1357 | wantMessage: func() proto.Message { |
| 1358 | m := &pb2.FakeMessageSet{} |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 1359 | proto.SetExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1360 | OptString: proto.String("not a messageset extension"), |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1361 | }) |
| 1362 | return m |
| 1363 | }(), |
Joe Tsai | 1799d11 | 2019-08-08 13:31:59 -0700 | [diff] [blame] | 1364 | skip: !flags.ProtoLegacy, |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1365 | }, { |
| 1366 | desc: "not real MessageSet 2", |
| 1367 | inputMessage: &pb2.FakeMessageSet{}, |
| 1368 | inputText: `{ |
| 1369 | "[pb2.FakeMessageSetExtension]": { |
| 1370 | "optString": "not a messageset extension" |
| 1371 | } |
| 1372 | }`, |
| 1373 | wantErr: true, |
Joe Tsai | 1799d11 | 2019-08-08 13:31:59 -0700 | [diff] [blame] | 1374 | skip: !flags.ProtoLegacy, |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1375 | }, { |
| 1376 | desc: "not real MessageSet 3", |
| 1377 | inputMessage: &pb2.MessageSet{}, |
| 1378 | inputText: `{ |
| 1379 | "[pb2.message_set_extension]": { |
| 1380 | "optString": "another not a messageset extension" |
| 1381 | } |
| 1382 | }`, |
| 1383 | wantMessage: func() proto.Message { |
| 1384 | m := &pb2.MessageSet{} |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 1385 | proto.SetExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1386 | OptString: proto.String("another not a messageset extension"), |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 1387 | }) |
| 1388 | return m |
| 1389 | }(), |
Joe Tsai | 1799d11 | 2019-08-08 13:31:59 -0700 | [diff] [blame] | 1390 | skip: !flags.ProtoLegacy, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1391 | }, { |
| 1392 | desc: "Empty", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1393 | inputMessage: &emptypb.Empty{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1394 | inputText: `{}`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1395 | wantMessage: &emptypb.Empty{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1396 | }, { |
| 1397 | desc: "Empty contains unknown", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1398 | inputMessage: &emptypb.Empty{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1399 | inputText: `{"unknown": null}`, |
| 1400 | wantErr: true, |
| 1401 | }, { |
| 1402 | desc: "BoolValue false", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1403 | inputMessage: &wrapperspb.BoolValue{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1404 | inputText: `false`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1405 | wantMessage: &wrapperspb.BoolValue{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1406 | }, { |
| 1407 | desc: "BoolValue true", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1408 | inputMessage: &wrapperspb.BoolValue{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1409 | inputText: `true`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1410 | wantMessage: &wrapperspb.BoolValue{Value: true}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1411 | }, { |
| 1412 | desc: "BoolValue invalid value", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1413 | inputMessage: &wrapperspb.BoolValue{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1414 | inputText: `{}`, |
| 1415 | wantErr: true, |
| 1416 | }, { |
| 1417 | desc: "Int32Value", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1418 | inputMessage: &wrapperspb.Int32Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1419 | inputText: `42`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1420 | wantMessage: &wrapperspb.Int32Value{Value: 42}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1421 | }, { |
| 1422 | desc: "Int32Value in JSON string", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1423 | inputMessage: &wrapperspb.Int32Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1424 | inputText: `"1.23e3"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1425 | wantMessage: &wrapperspb.Int32Value{Value: 1230}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1426 | }, { |
| 1427 | desc: "Int64Value", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1428 | inputMessage: &wrapperspb.Int64Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1429 | inputText: `"42"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1430 | wantMessage: &wrapperspb.Int64Value{Value: 42}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1431 | }, { |
| 1432 | desc: "UInt32Value", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1433 | inputMessage: &wrapperspb.UInt32Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1434 | inputText: `42`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1435 | wantMessage: &wrapperspb.UInt32Value{Value: 42}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1436 | }, { |
| 1437 | desc: "UInt64Value", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1438 | inputMessage: &wrapperspb.UInt64Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1439 | inputText: `"42"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1440 | wantMessage: &wrapperspb.UInt64Value{Value: 42}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1441 | }, { |
| 1442 | desc: "FloatValue", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1443 | inputMessage: &wrapperspb.FloatValue{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1444 | inputText: `1.02`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1445 | wantMessage: &wrapperspb.FloatValue{Value: 1.02}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1446 | }, { |
| 1447 | desc: "FloatValue exceeds max limit", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1448 | inputMessage: &wrapperspb.FloatValue{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1449 | inputText: `1.23+40`, |
| 1450 | wantErr: true, |
| 1451 | }, { |
| 1452 | desc: "FloatValue Infinity", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1453 | inputMessage: &wrapperspb.FloatValue{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1454 | inputText: `"-Infinity"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1455 | wantMessage: &wrapperspb.FloatValue{Value: float32(math.Inf(-1))}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1456 | }, { |
| 1457 | desc: "DoubleValue", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1458 | inputMessage: &wrapperspb.DoubleValue{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1459 | inputText: `1.02`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1460 | wantMessage: &wrapperspb.DoubleValue{Value: 1.02}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1461 | }, { |
| 1462 | desc: "DoubleValue Infinity", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1463 | inputMessage: &wrapperspb.DoubleValue{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1464 | inputText: `"Infinity"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1465 | wantMessage: &wrapperspb.DoubleValue{Value: math.Inf(+1)}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1466 | }, { |
| 1467 | desc: "StringValue empty", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1468 | inputMessage: &wrapperspb.StringValue{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1469 | inputText: `""`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1470 | wantMessage: &wrapperspb.StringValue{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1471 | }, { |
| 1472 | desc: "StringValue", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1473 | inputMessage: &wrapperspb.StringValue{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1474 | inputText: `"è°·æŒ"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1475 | wantMessage: &wrapperspb.StringValue{Value: "è°·æŒ"}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1476 | }, { |
| 1477 | desc: "StringValue with invalid UTF8 error", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1478 | inputMessage: &wrapperspb.StringValue{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1479 | inputText: "\"abc\xff\"", |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1480 | wantErr: true, |
| 1481 | }, { |
| 1482 | desc: "StringValue field with invalid UTF8 error", |
| 1483 | inputMessage: &pb2.KnownTypes{}, |
| 1484 | inputText: "{\n \"optString\": \"abc\xff\"\n}", |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 1485 | wantErr: true, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1486 | }, { |
Herbie Ong | 300b9fe | 2019-03-29 15:42:20 -0700 | [diff] [blame] | 1487 | desc: "NullValue field with JSON null", |
| 1488 | inputMessage: &pb2.KnownTypes{}, |
| 1489 | inputText: `{ |
| 1490 | "optNull": null |
| 1491 | }`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1492 | wantMessage: &pb2.KnownTypes{OptNull: new(structpb.NullValue)}, |
Herbie Ong | 300b9fe | 2019-03-29 15:42:20 -0700 | [diff] [blame] | 1493 | }, { |
| 1494 | desc: "NullValue field with string", |
| 1495 | inputMessage: &pb2.KnownTypes{}, |
| 1496 | inputText: `{ |
| 1497 | "optNull": "NULL_VALUE" |
| 1498 | }`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1499 | wantMessage: &pb2.KnownTypes{OptNull: new(structpb.NullValue)}, |
Herbie Ong | 300b9fe | 2019-03-29 15:42:20 -0700 | [diff] [blame] | 1500 | }, { |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1501 | desc: "BytesValue", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1502 | inputMessage: &wrapperspb.BytesValue{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1503 | inputText: `"aGVsbG8="`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1504 | wantMessage: &wrapperspb.BytesValue{Value: []byte("hello")}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1505 | }, { |
| 1506 | desc: "Value null", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1507 | inputMessage: &structpb.Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1508 | inputText: `null`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1509 | wantMessage: &structpb.Value{Kind: &structpb.Value_NullValue{}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1510 | }, { |
| 1511 | desc: "Value field null", |
| 1512 | inputMessage: &pb2.KnownTypes{}, |
| 1513 | inputText: `{ |
| 1514 | "optValue": null |
| 1515 | }`, |
| 1516 | wantMessage: &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1517 | OptValue: &structpb.Value{Kind: &structpb.Value_NullValue{}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1518 | }, |
| 1519 | }, { |
| 1520 | desc: "Value bool", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1521 | inputMessage: &structpb.Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1522 | inputText: `false`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1523 | wantMessage: &structpb.Value{Kind: &structpb.Value_BoolValue{}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1524 | }, { |
| 1525 | desc: "Value field bool", |
| 1526 | inputMessage: &pb2.KnownTypes{}, |
| 1527 | inputText: `{ |
| 1528 | "optValue": true |
| 1529 | }`, |
| 1530 | wantMessage: &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1531 | OptValue: &structpb.Value{Kind: &structpb.Value_BoolValue{true}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1532 | }, |
| 1533 | }, { |
| 1534 | desc: "Value number", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1535 | inputMessage: &structpb.Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1536 | inputText: `1.02`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1537 | wantMessage: &structpb.Value{Kind: &structpb.Value_NumberValue{1.02}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1538 | }, { |
| 1539 | desc: "Value field number", |
| 1540 | inputMessage: &pb2.KnownTypes{}, |
| 1541 | inputText: `{ |
| 1542 | "optValue": 1.02 |
| 1543 | }`, |
| 1544 | wantMessage: &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1545 | OptValue: &structpb.Value{Kind: &structpb.Value_NumberValue{1.02}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1546 | }, |
| 1547 | }, { |
| 1548 | desc: "Value string", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1549 | inputMessage: &structpb.Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1550 | inputText: `"hello"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1551 | wantMessage: &structpb.Value{Kind: &structpb.Value_StringValue{"hello"}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1552 | }, { |
| 1553 | desc: "Value string with invalid UTF8", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1554 | inputMessage: &structpb.Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1555 | inputText: "\"\xff\"", |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1556 | wantErr: true, |
| 1557 | }, { |
| 1558 | desc: "Value field string", |
| 1559 | inputMessage: &pb2.KnownTypes{}, |
| 1560 | inputText: `{ |
| 1561 | "optValue": "NaN" |
| 1562 | }`, |
| 1563 | wantMessage: &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1564 | OptValue: &structpb.Value{Kind: &structpb.Value_StringValue{"NaN"}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1565 | }, |
| 1566 | }, { |
| 1567 | desc: "Value field string with invalid UTF8", |
| 1568 | inputMessage: &pb2.KnownTypes{}, |
| 1569 | inputText: `{ |
| 1570 | "optValue": "` + "\xff" + `" |
| 1571 | }`, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1572 | wantErr: true, |
| 1573 | }, { |
| 1574 | desc: "Value empty struct", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1575 | inputMessage: &structpb.Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1576 | inputText: `{}`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1577 | wantMessage: &structpb.Value{ |
| 1578 | Kind: &structpb.Value_StructValue{ |
| 1579 | &structpb.Struct{Fields: map[string]*structpb.Value{}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1580 | }, |
| 1581 | }, |
| 1582 | }, { |
| 1583 | desc: "Value struct", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1584 | inputMessage: &structpb.Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1585 | inputText: `{ |
| 1586 | "string": "hello", |
| 1587 | "number": 123, |
| 1588 | "null": null, |
| 1589 | "bool": false, |
| 1590 | "struct": { |
| 1591 | "string": "world" |
| 1592 | }, |
| 1593 | "list": [] |
| 1594 | }`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1595 | wantMessage: &structpb.Value{ |
| 1596 | Kind: &structpb.Value_StructValue{ |
| 1597 | &structpb.Struct{ |
| 1598 | Fields: map[string]*structpb.Value{ |
| 1599 | "string": {Kind: &structpb.Value_StringValue{"hello"}}, |
| 1600 | "number": {Kind: &structpb.Value_NumberValue{123}}, |
| 1601 | "null": {Kind: &structpb.Value_NullValue{}}, |
| 1602 | "bool": {Kind: &structpb.Value_BoolValue{false}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1603 | "struct": { |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1604 | Kind: &structpb.Value_StructValue{ |
| 1605 | &structpb.Struct{ |
| 1606 | Fields: map[string]*structpb.Value{ |
| 1607 | "string": {Kind: &structpb.Value_StringValue{"world"}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1608 | }, |
| 1609 | }, |
| 1610 | }, |
| 1611 | }, |
| 1612 | "list": { |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1613 | Kind: &structpb.Value_ListValue{&structpb.ListValue{}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1614 | }, |
| 1615 | }, |
| 1616 | }, |
| 1617 | }, |
| 1618 | }, |
| 1619 | }, { |
| 1620 | desc: "Value struct with invalid UTF8 string", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1621 | inputMessage: &structpb.Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1622 | inputText: "{\"string\": \"abc\xff\"}", |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 1623 | wantErr: true, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1624 | }, { |
| 1625 | desc: "Value field struct", |
| 1626 | inputMessage: &pb2.KnownTypes{}, |
| 1627 | inputText: `{ |
| 1628 | "optValue": { |
| 1629 | "string": "hello" |
| 1630 | } |
| 1631 | }`, |
| 1632 | wantMessage: &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1633 | OptValue: &structpb.Value{ |
| 1634 | Kind: &structpb.Value_StructValue{ |
| 1635 | &structpb.Struct{ |
| 1636 | Fields: map[string]*structpb.Value{ |
| 1637 | "string": {Kind: &structpb.Value_StringValue{"hello"}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1638 | }, |
| 1639 | }, |
| 1640 | }, |
| 1641 | }, |
| 1642 | }, |
| 1643 | }, { |
| 1644 | desc: "Value empty list", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1645 | inputMessage: &structpb.Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1646 | inputText: `[]`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1647 | wantMessage: &structpb.Value{ |
| 1648 | Kind: &structpb.Value_ListValue{ |
| 1649 | &structpb.ListValue{Values: []*structpb.Value{}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1650 | }, |
| 1651 | }, |
| 1652 | }, { |
| 1653 | desc: "Value list", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1654 | inputMessage: &structpb.Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1655 | inputText: `[ |
| 1656 | "string", |
| 1657 | 123, |
| 1658 | null, |
| 1659 | true, |
| 1660 | {}, |
| 1661 | [ |
| 1662 | "string", |
| 1663 | 1.23, |
| 1664 | null, |
| 1665 | false |
| 1666 | ] |
| 1667 | ]`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1668 | wantMessage: &structpb.Value{ |
| 1669 | Kind: &structpb.Value_ListValue{ |
| 1670 | &structpb.ListValue{ |
| 1671 | Values: []*structpb.Value{ |
| 1672 | {Kind: &structpb.Value_StringValue{"string"}}, |
| 1673 | {Kind: &structpb.Value_NumberValue{123}}, |
| 1674 | {Kind: &structpb.Value_NullValue{}}, |
| 1675 | {Kind: &structpb.Value_BoolValue{true}}, |
| 1676 | {Kind: &structpb.Value_StructValue{&structpb.Struct{}}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1677 | { |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1678 | Kind: &structpb.Value_ListValue{ |
| 1679 | &structpb.ListValue{ |
| 1680 | Values: []*structpb.Value{ |
| 1681 | {Kind: &structpb.Value_StringValue{"string"}}, |
| 1682 | {Kind: &structpb.Value_NumberValue{1.23}}, |
| 1683 | {Kind: &structpb.Value_NullValue{}}, |
| 1684 | {Kind: &structpb.Value_BoolValue{false}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1685 | }, |
| 1686 | }, |
| 1687 | }, |
| 1688 | }, |
| 1689 | }, |
| 1690 | }, |
| 1691 | }, |
| 1692 | }, |
| 1693 | }, { |
| 1694 | desc: "Value list with invalid UTF8 string", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1695 | inputMessage: &structpb.Value{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1696 | inputText: "[\"abc\xff\"]", |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 1697 | wantErr: true, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1698 | }, { |
| 1699 | desc: "Value field list with invalid UTF8 string", |
| 1700 | inputMessage: &pb2.KnownTypes{}, |
| 1701 | inputText: `{ |
| 1702 | "optValue": [ "` + "abc\xff" + `"] |
| 1703 | }`, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1704 | wantErr: true, |
| 1705 | }, { |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1706 | desc: "Duration empty string", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1707 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1708 | inputText: `""`, |
| 1709 | wantErr: true, |
| 1710 | }, { |
| 1711 | desc: "Duration with secs", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1712 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1713 | inputText: `"3s"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1714 | wantMessage: &durationpb.Duration{Seconds: 3}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1715 | }, { |
| 1716 | desc: "Duration with escaped unicode", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1717 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1718 | inputText: `"\u0033s"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1719 | wantMessage: &durationpb.Duration{Seconds: 3}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1720 | }, { |
| 1721 | desc: "Duration with -secs", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1722 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1723 | inputText: `"-3s"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1724 | wantMessage: &durationpb.Duration{Seconds: -3}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1725 | }, { |
Herbie Ong | 17523eb | 2019-03-29 17:46:57 -0700 | [diff] [blame] | 1726 | desc: "Duration with plus sign", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1727 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | 17523eb | 2019-03-29 17:46:57 -0700 | [diff] [blame] | 1728 | inputText: `"+3s"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1729 | wantMessage: &durationpb.Duration{Seconds: 3}, |
Herbie Ong | 17523eb | 2019-03-29 17:46:57 -0700 | [diff] [blame] | 1730 | }, { |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1731 | desc: "Duration with nanos", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1732 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1733 | inputText: `"0.001s"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1734 | wantMessage: &durationpb.Duration{Nanos: 1e6}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1735 | }, { |
| 1736 | desc: "Duration with -nanos", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1737 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1738 | inputText: `"-0.001s"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1739 | wantMessage: &durationpb.Duration{Nanos: -1e6}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1740 | }, { |
Herbie Ong | 17523eb | 2019-03-29 17:46:57 -0700 | [diff] [blame] | 1741 | desc: "Duration with -nanos", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1742 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | 17523eb | 2019-03-29 17:46:57 -0700 | [diff] [blame] | 1743 | inputText: `"-.001s"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1744 | wantMessage: &durationpb.Duration{Nanos: -1e6}, |
Herbie Ong | 17523eb | 2019-03-29 17:46:57 -0700 | [diff] [blame] | 1745 | }, { |
| 1746 | desc: "Duration with +nanos", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1747 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | 17523eb | 2019-03-29 17:46:57 -0700 | [diff] [blame] | 1748 | inputText: `"+.001s"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1749 | wantMessage: &durationpb.Duration{Nanos: 1e6}, |
Herbie Ong | 17523eb | 2019-03-29 17:46:57 -0700 | [diff] [blame] | 1750 | }, { |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1751 | desc: "Duration with -secs -nanos", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1752 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1753 | inputText: `"-123.000000450s"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1754 | wantMessage: &durationpb.Duration{Seconds: -123, Nanos: -450}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1755 | }, { |
| 1756 | desc: "Duration with large secs", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1757 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1758 | inputText: `"10000000000.000000001s"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1759 | wantMessage: &durationpb.Duration{Seconds: 1e10, Nanos: 1}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1760 | }, { |
| 1761 | desc: "Duration with decimal without fractional", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1762 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1763 | inputText: `"3.s"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1764 | wantMessage: &durationpb.Duration{Seconds: 3}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1765 | }, { |
| 1766 | desc: "Duration with decimal without integer", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1767 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1768 | inputText: `"0.5s"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1769 | wantMessage: &durationpb.Duration{Nanos: 5e8}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1770 | }, { |
Herbie Ong | ad9c125 | 2019-04-24 20:51:28 -0700 | [diff] [blame] | 1771 | desc: "Duration max value", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1772 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | ad9c125 | 2019-04-24 20:51:28 -0700 | [diff] [blame] | 1773 | inputText: `"315576000000.999999999s"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1774 | wantMessage: &durationpb.Duration{Seconds: 315576000000, Nanos: 999999999}, |
Herbie Ong | ad9c125 | 2019-04-24 20:51:28 -0700 | [diff] [blame] | 1775 | }, { |
| 1776 | desc: "Duration min value", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1777 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | ad9c125 | 2019-04-24 20:51:28 -0700 | [diff] [blame] | 1778 | inputText: `"-315576000000.999999999s"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1779 | wantMessage: &durationpb.Duration{Seconds: -315576000000, Nanos: -999999999}, |
Herbie Ong | ad9c125 | 2019-04-24 20:51:28 -0700 | [diff] [blame] | 1780 | }, { |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1781 | desc: "Duration with +secs out of range", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1782 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1783 | inputText: `"315576000001s"`, |
| 1784 | wantErr: true, |
| 1785 | }, { |
| 1786 | desc: "Duration with -secs out of range", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1787 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1788 | inputText: `"-315576000001s"`, |
| 1789 | wantErr: true, |
| 1790 | }, { |
| 1791 | desc: "Duration with nanos beyond 9 digits", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1792 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | 17523eb | 2019-03-29 17:46:57 -0700 | [diff] [blame] | 1793 | inputText: `"0.1000000000s"`, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1794 | wantErr: true, |
| 1795 | }, { |
| 1796 | desc: "Duration without suffix s", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1797 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1798 | inputText: `"123"`, |
| 1799 | wantErr: true, |
| 1800 | }, { |
Herbie Ong | 17523eb | 2019-03-29 17:46:57 -0700 | [diff] [blame] | 1801 | desc: "Duration invalid signed fraction", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1802 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | 17523eb | 2019-03-29 17:46:57 -0700 | [diff] [blame] | 1803 | inputText: `"123.+123s"`, |
| 1804 | wantErr: true, |
| 1805 | }, { |
| 1806 | desc: "Duration invalid multiple .", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1807 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | 17523eb | 2019-03-29 17:46:57 -0700 | [diff] [blame] | 1808 | inputText: `"123.123.s"`, |
| 1809 | wantErr: true, |
| 1810 | }, { |
| 1811 | desc: "Duration invalid integer", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1812 | inputMessage: &durationpb.Duration{}, |
Herbie Ong | 17523eb | 2019-03-29 17:46:57 -0700 | [diff] [blame] | 1813 | inputText: `"01s"`, |
| 1814 | wantErr: true, |
| 1815 | }, { |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1816 | desc: "Timestamp zero", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1817 | inputMessage: ×tamppb.Timestamp{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1818 | inputText: `"1970-01-01T00:00:00Z"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1819 | wantMessage: ×tamppb.Timestamp{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1820 | }, { |
| 1821 | desc: "Timestamp with tz adjustment", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1822 | inputMessage: ×tamppb.Timestamp{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1823 | inputText: `"1970-01-01T00:00:00+01:00"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1824 | wantMessage: ×tamppb.Timestamp{Seconds: -3600}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1825 | }, { |
| 1826 | desc: "Timestamp UTC", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1827 | inputMessage: ×tamppb.Timestamp{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1828 | inputText: `"2019-03-19T23:03:21Z"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1829 | wantMessage: ×tamppb.Timestamp{Seconds: 1553036601}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1830 | }, { |
| 1831 | desc: "Timestamp with escaped unicode", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1832 | inputMessage: ×tamppb.Timestamp{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1833 | inputText: `"2019-0\u0033-19T23:03:21Z"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1834 | wantMessage: ×tamppb.Timestamp{Seconds: 1553036601}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1835 | }, { |
| 1836 | desc: "Timestamp with nanos", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1837 | inputMessage: ×tamppb.Timestamp{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1838 | inputText: `"2019-03-19T23:03:21.000000001Z"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1839 | wantMessage: ×tamppb.Timestamp{Seconds: 1553036601, Nanos: 1}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1840 | }, { |
Herbie Ong | ad9c125 | 2019-04-24 20:51:28 -0700 | [diff] [blame] | 1841 | desc: "Timestamp max value", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1842 | inputMessage: ×tamppb.Timestamp{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1843 | inputText: `"9999-12-31T23:59:59.999999999Z"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1844 | wantMessage: ×tamppb.Timestamp{Seconds: 253402300799, Nanos: 999999999}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1845 | }, { |
Herbie Ong | ad9c125 | 2019-04-24 20:51:28 -0700 | [diff] [blame] | 1846 | desc: "Timestamp above max value", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1847 | inputMessage: ×tamppb.Timestamp{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1848 | inputText: `"9999-12-31T23:59:59-01:00"`, |
| 1849 | wantErr: true, |
| 1850 | }, { |
Herbie Ong | ad9c125 | 2019-04-24 20:51:28 -0700 | [diff] [blame] | 1851 | desc: "Timestamp min value", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1852 | inputMessage: ×tamppb.Timestamp{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1853 | inputText: `"0001-01-01T00:00:00Z"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1854 | wantMessage: ×tamppb.Timestamp{Seconds: -62135596800}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1855 | }, { |
Herbie Ong | ad9c125 | 2019-04-24 20:51:28 -0700 | [diff] [blame] | 1856 | desc: "Timestamp below min value", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1857 | inputMessage: ×tamppb.Timestamp{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1858 | inputText: `"0001-01-01T00:00:00+01:00"`, |
| 1859 | wantErr: true, |
| 1860 | }, { |
| 1861 | desc: "Timestamp with nanos beyond 9 digits", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1862 | inputMessage: ×tamppb.Timestamp{}, |
Herbie Ong | c445037 | 2019-03-27 09:59:51 -0700 | [diff] [blame] | 1863 | inputText: `"1970-01-01T00:00:00.0000000001Z"`, |
| 1864 | wantErr: true, |
| 1865 | }, { |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1866 | desc: "FieldMask empty", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1867 | inputMessage: &fieldmaskpb.FieldMask{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1868 | inputText: `""`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1869 | wantMessage: &fieldmaskpb.FieldMask{Paths: []string{}}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1870 | }, { |
| 1871 | desc: "FieldMask", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1872 | inputMessage: &fieldmaskpb.FieldMask{}, |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1873 | inputText: `"foo,fooBar , foo.barQux ,Foo"`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1874 | wantMessage: &fieldmaskpb.FieldMask{ |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1875 | Paths: []string{ |
| 1876 | "foo", |
| 1877 | "foo_bar", |
| 1878 | "foo.bar_qux", |
| 1879 | "_foo", |
| 1880 | }, |
| 1881 | }, |
| 1882 | }, { |
| 1883 | desc: "FieldMask field", |
| 1884 | inputMessage: &pb2.KnownTypes{}, |
| 1885 | inputText: `{ |
| 1886 | "optFieldmask": "foo, qux.fooBar" |
| 1887 | }`, |
| 1888 | wantMessage: &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1889 | OptFieldmask: &fieldmaskpb.FieldMask{ |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1890 | Paths: []string{ |
| 1891 | "foo", |
| 1892 | "qux.foo_bar", |
| 1893 | }, |
| 1894 | }, |
| 1895 | }, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1896 | }, { |
| 1897 | desc: "Any empty", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1898 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1899 | inputText: `{}`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1900 | wantMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1901 | }, { |
| 1902 | desc: "Any with non-custom message", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1903 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 1904 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1905 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1906 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1907 | inputText: `{ |
| 1908 | "@type": "foo/pb2.Nested", |
| 1909 | "optString": "embedded inside Any", |
| 1910 | "optNested": { |
| 1911 | "optString": "inception" |
| 1912 | } |
| 1913 | }`, |
| 1914 | wantMessage: func() proto.Message { |
| 1915 | m := &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1916 | OptString: proto.String("embedded inside Any"), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1917 | OptNested: &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1918 | OptString: proto.String("inception"), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1919 | }, |
| 1920 | } |
| 1921 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 1922 | if err != nil { |
| 1923 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1924 | } |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1925 | return &anypb.Any{ |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1926 | TypeUrl: "foo/pb2.Nested", |
| 1927 | Value: b, |
| 1928 | } |
| 1929 | }(), |
| 1930 | }, { |
| 1931 | desc: "Any with empty embedded message", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1932 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 1933 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1934 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1935 | inputMessage: &anypb.Any{}, |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 1936 | inputText: `{"@type": "foo/pb2.Nested"}`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1937 | wantMessage: &anypb.Any{TypeUrl: "foo/pb2.Nested"}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1938 | }, { |
| 1939 | desc: "Any without registered type", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1940 | umo: protojson.UnmarshalOptions{Resolver: preg.NewTypes()}, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1941 | inputMessage: &anypb.Any{}, |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 1942 | inputText: `{"@type": "foo/pb2.Nested"}`, |
| 1943 | wantErr: true, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1944 | }, { |
Damien Neil | 0c9f0a9 | 2019-06-19 10:41:09 -0700 | [diff] [blame] | 1945 | desc: "Any with missing required", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1946 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 1947 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1948 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1949 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1950 | inputText: `{ |
| 1951 | "@type": "pb2.PartialRequired", |
| 1952 | "optString": "embedded inside Any" |
| 1953 | }`, |
| 1954 | wantMessage: func() proto.Message { |
| 1955 | m := &pb2.PartialRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1956 | OptString: proto.String("embedded inside Any"), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1957 | } |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1958 | b, err := proto.MarshalOptions{ |
| 1959 | Deterministic: true, |
| 1960 | AllowPartial: true, |
| 1961 | }.Marshal(m) |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1962 | if err != nil { |
| 1963 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1964 | } |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1965 | return &anypb.Any{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 1966 | TypeUrl: string(m.ProtoReflect().Descriptor().FullName()), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1967 | Value: b, |
| 1968 | } |
| 1969 | }(), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1970 | }, { |
| 1971 | desc: "Any with partial required and AllowPartial", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1972 | umo: protojson.UnmarshalOptions{ |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1973 | AllowPartial: true, |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 1974 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1975 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1976 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1977 | inputText: `{ |
| 1978 | "@type": "pb2.PartialRequired", |
| 1979 | "optString": "embedded inside Any" |
| 1980 | }`, |
| 1981 | wantMessage: func() proto.Message { |
| 1982 | m := &pb2.PartialRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1983 | OptString: proto.String("embedded inside Any"), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1984 | } |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1985 | b, err := proto.MarshalOptions{ |
| 1986 | Deterministic: true, |
| 1987 | AllowPartial: true, |
| 1988 | }.Marshal(m) |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1989 | if err != nil { |
| 1990 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1991 | } |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1992 | return &anypb.Any{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 1993 | TypeUrl: string(m.ProtoReflect().Descriptor().FullName()), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1994 | Value: b, |
| 1995 | } |
| 1996 | }(), |
| 1997 | }, { |
| 1998 | desc: "Any with invalid UTF8", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1999 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 2000 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2001 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2002 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2003 | inputText: `{ |
| 2004 | "optString": "` + "abc\xff" + `", |
| 2005 | "@type": "foo/pb2.Nested" |
| 2006 | }`, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2007 | wantErr: true, |
| 2008 | }, { |
| 2009 | desc: "Any with BoolValue", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2010 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2011 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2012 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2013 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2014 | inputText: `{ |
| 2015 | "@type": "type.googleapis.com/google.protobuf.BoolValue", |
| 2016 | "value": true |
| 2017 | }`, |
| 2018 | wantMessage: func() proto.Message { |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2019 | m := &wrapperspb.BoolValue{Value: true} |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2020 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 2021 | if err != nil { |
| 2022 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 2023 | } |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2024 | return &anypb.Any{ |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2025 | TypeUrl: "type.googleapis.com/google.protobuf.BoolValue", |
| 2026 | Value: b, |
| 2027 | } |
| 2028 | }(), |
| 2029 | }, { |
| 2030 | desc: "Any with Empty", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2031 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2032 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2033 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2034 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2035 | inputText: `{ |
| 2036 | "value": {}, |
| 2037 | "@type": "type.googleapis.com/google.protobuf.Empty" |
| 2038 | }`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2039 | wantMessage: &anypb.Any{ |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2040 | TypeUrl: "type.googleapis.com/google.protobuf.Empty", |
| 2041 | }, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2042 | }, { |
| 2043 | desc: "Any with missing Empty", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2044 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2045 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2046 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2047 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2048 | inputText: `{ |
| 2049 | "@type": "type.googleapis.com/google.protobuf.Empty" |
| 2050 | }`, |
| 2051 | wantErr: true, |
| 2052 | }, { |
| 2053 | desc: "Any with StringValue containing invalid UTF8", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2054 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2055 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2056 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2057 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2058 | inputText: `{ |
| 2059 | "@type": "google.protobuf.StringValue", |
| 2060 | "value": "` + "abc\xff" + `" |
| 2061 | }`, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2062 | wantErr: true, |
| 2063 | }, { |
| 2064 | desc: "Any with Int64Value", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2065 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2066 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.Int64Value{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2067 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2068 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2069 | inputText: `{ |
| 2070 | "@type": "google.protobuf.Int64Value", |
| 2071 | "value": "42" |
| 2072 | }`, |
| 2073 | wantMessage: func() proto.Message { |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2074 | m := &wrapperspb.Int64Value{Value: 42} |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2075 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 2076 | if err != nil { |
| 2077 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 2078 | } |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2079 | return &anypb.Any{ |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2080 | TypeUrl: "google.protobuf.Int64Value", |
| 2081 | Value: b, |
| 2082 | } |
| 2083 | }(), |
| 2084 | }, { |
| 2085 | desc: "Any with invalid Int64Value", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2086 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2087 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.Int64Value{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2088 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2089 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2090 | inputText: `{ |
| 2091 | "@type": "google.protobuf.Int64Value", |
| 2092 | "value": "forty-two" |
| 2093 | }`, |
| 2094 | wantErr: true, |
| 2095 | }, { |
| 2096 | desc: "Any with invalid UInt64Value", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2097 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2098 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.UInt64Value{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2099 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2100 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2101 | inputText: `{ |
| 2102 | "@type": "google.protobuf.UInt64Value", |
| 2103 | "value": -42 |
| 2104 | }`, |
| 2105 | wantErr: true, |
| 2106 | }, { |
| 2107 | desc: "Any with Duration", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2108 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2109 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&durationpb.Duration{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2110 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2111 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2112 | inputText: `{ |
| 2113 | "@type": "type.googleapis.com/google.protobuf.Duration", |
| 2114 | "value": "0s" |
| 2115 | }`, |
| 2116 | wantMessage: func() proto.Message { |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2117 | m := &durationpb.Duration{} |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2118 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 2119 | if err != nil { |
| 2120 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 2121 | } |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2122 | return &anypb.Any{ |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2123 | TypeUrl: "type.googleapis.com/google.protobuf.Duration", |
| 2124 | Value: b, |
| 2125 | } |
| 2126 | }(), |
| 2127 | }, { |
| 2128 | desc: "Any with Value of StringValue", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2129 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2130 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2131 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2132 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2133 | inputText: `{ |
| 2134 | "@type": "google.protobuf.Value", |
| 2135 | "value": "` + "abc\xff" + `" |
| 2136 | }`, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2137 | wantErr: true, |
| 2138 | }, { |
| 2139 | desc: "Any with Value of NullValue", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2140 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2141 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2142 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2143 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2144 | inputText: `{ |
| 2145 | "@type": "google.protobuf.Value", |
| 2146 | "value": null |
| 2147 | }`, |
| 2148 | wantMessage: func() proto.Message { |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2149 | m := &structpb.Value{Kind: &structpb.Value_NullValue{}} |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2150 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 2151 | if err != nil { |
| 2152 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 2153 | } |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2154 | return &anypb.Any{ |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2155 | TypeUrl: "google.protobuf.Value", |
| 2156 | Value: b, |
| 2157 | } |
| 2158 | }(), |
| 2159 | }, { |
| 2160 | desc: "Any with Struct", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2161 | umo: protojson.UnmarshalOptions{ |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2162 | Resolver: preg.NewTypes( |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2163 | pimpl.Export{}.MessageTypeOf(&structpb.Struct{}), |
| 2164 | pimpl.Export{}.MessageTypeOf(&structpb.Value{}), |
| 2165 | pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{}), |
| 2166 | pimpl.Export{}.EnumTypeOf(structpb.NullValue_NULL_VALUE), |
| 2167 | pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{}), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2168 | ), |
| 2169 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2170 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2171 | inputText: `{ |
| 2172 | "@type": "google.protobuf.Struct", |
| 2173 | "value": { |
| 2174 | "bool": true, |
| 2175 | "null": null, |
| 2176 | "string": "hello", |
| 2177 | "struct": { |
| 2178 | "string": "world" |
| 2179 | } |
| 2180 | } |
| 2181 | }`, |
| 2182 | wantMessage: func() proto.Message { |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2183 | m := &structpb.Struct{ |
| 2184 | Fields: map[string]*structpb.Value{ |
| 2185 | "bool": {Kind: &structpb.Value_BoolValue{true}}, |
| 2186 | "null": {Kind: &structpb.Value_NullValue{}}, |
| 2187 | "string": {Kind: &structpb.Value_StringValue{"hello"}}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2188 | "struct": { |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2189 | Kind: &structpb.Value_StructValue{ |
| 2190 | &structpb.Struct{ |
| 2191 | Fields: map[string]*structpb.Value{ |
| 2192 | "string": {Kind: &structpb.Value_StringValue{"world"}}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2193 | }, |
| 2194 | }, |
| 2195 | }, |
| 2196 | }, |
| 2197 | }, |
| 2198 | } |
| 2199 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 2200 | if err != nil { |
| 2201 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 2202 | } |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2203 | return &anypb.Any{ |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2204 | TypeUrl: "google.protobuf.Struct", |
| 2205 | Value: b, |
| 2206 | } |
| 2207 | }(), |
| 2208 | }, { |
| 2209 | desc: "Any with missing @type", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2210 | umo: protojson.UnmarshalOptions{}, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2211 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2212 | inputText: `{ |
| 2213 | "value": {} |
| 2214 | }`, |
| 2215 | wantErr: true, |
| 2216 | }, { |
| 2217 | desc: "Any with empty @type", |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2218 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2219 | inputText: `{ |
| 2220 | "@type": "" |
| 2221 | }`, |
| 2222 | wantErr: true, |
| 2223 | }, { |
| 2224 | desc: "Any with duplicate @type", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2225 | umo: protojson.UnmarshalOptions{ |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2226 | Resolver: preg.NewTypes( |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 2227 | pimpl.Export{}.MessageTypeOf(&pb2.Nested{}), |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2228 | pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{}), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2229 | ), |
| 2230 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2231 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2232 | inputText: `{ |
| 2233 | "@type": "google.protobuf.StringValue", |
| 2234 | "value": "hello", |
| 2235 | "@type": "pb2.Nested" |
| 2236 | }`, |
| 2237 | wantErr: true, |
| 2238 | }, { |
| 2239 | desc: "Any with duplicate value", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2240 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2241 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2242 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2243 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2244 | inputText: `{ |
| 2245 | "@type": "google.protobuf.StringValue", |
| 2246 | "value": "hello", |
| 2247 | "value": "world" |
| 2248 | }`, |
| 2249 | wantErr: true, |
| 2250 | }, { |
| 2251 | desc: "Any with unknown field", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2252 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 2253 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2254 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2255 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2256 | inputText: `{ |
| 2257 | "@type": "pb2.Nested", |
| 2258 | "optString": "hello", |
| 2259 | "unknown": "world" |
| 2260 | }`, |
| 2261 | wantErr: true, |
| 2262 | }, { |
| 2263 | desc: "Any with embedded type containing Any", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2264 | umo: protojson.UnmarshalOptions{ |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2265 | Resolver: preg.NewTypes( |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 2266 | pimpl.Export{}.MessageTypeOf(&pb2.KnownTypes{}), |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2267 | pimpl.Export{}.MessageTypeOf(&anypb.Any{}), |
| 2268 | pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{}), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2269 | ), |
| 2270 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2271 | inputMessage: &anypb.Any{}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2272 | inputText: `{ |
| 2273 | "@type": "pb2.KnownTypes", |
| 2274 | "optAny": { |
| 2275 | "@type": "google.protobuf.StringValue", |
| 2276 | "value": "` + "abc\xff" + `" |
| 2277 | } |
| 2278 | }`, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2279 | wantErr: true, |
| 2280 | }, { |
| 2281 | desc: "well known types as field values", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2282 | umo: protojson.UnmarshalOptions{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2283 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})), |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2284 | }, |
| 2285 | inputMessage: &pb2.KnownTypes{}, |
| 2286 | inputText: `{ |
| 2287 | "optBool": false, |
| 2288 | "optInt32": 42, |
| 2289 | "optInt64": "42", |
| 2290 | "optUint32": 42, |
| 2291 | "optUint64": "42", |
| 2292 | "optFloat": 1.23, |
| 2293 | "optDouble": 3.1415, |
| 2294 | "optString": "hello", |
| 2295 | "optBytes": "aGVsbG8=", |
| 2296 | "optDuration": "123s", |
| 2297 | "optTimestamp": "2019-03-19T23:03:21Z", |
| 2298 | "optStruct": { |
| 2299 | "string": "hello" |
| 2300 | }, |
| 2301 | "optList": [ |
| 2302 | null, |
| 2303 | "", |
| 2304 | {}, |
| 2305 | [] |
| 2306 | ], |
| 2307 | "optValue": "world", |
| 2308 | "optEmpty": {}, |
| 2309 | "optAny": { |
| 2310 | "@type": "google.protobuf.Empty", |
| 2311 | "value": {} |
| 2312 | }, |
| 2313 | "optFieldmask": "fooBar,barFoo" |
| 2314 | }`, |
| 2315 | wantMessage: &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2316 | OptBool: &wrapperspb.BoolValue{Value: false}, |
| 2317 | OptInt32: &wrapperspb.Int32Value{Value: 42}, |
| 2318 | OptInt64: &wrapperspb.Int64Value{Value: 42}, |
| 2319 | OptUint32: &wrapperspb.UInt32Value{Value: 42}, |
| 2320 | OptUint64: &wrapperspb.UInt64Value{Value: 42}, |
| 2321 | OptFloat: &wrapperspb.FloatValue{Value: 1.23}, |
| 2322 | OptDouble: &wrapperspb.DoubleValue{Value: 3.1415}, |
| 2323 | OptString: &wrapperspb.StringValue{Value: "hello"}, |
| 2324 | OptBytes: &wrapperspb.BytesValue{Value: []byte("hello")}, |
| 2325 | OptDuration: &durationpb.Duration{Seconds: 123}, |
| 2326 | OptTimestamp: ×tamppb.Timestamp{Seconds: 1553036601}, |
| 2327 | OptStruct: &structpb.Struct{ |
| 2328 | Fields: map[string]*structpb.Value{ |
| 2329 | "string": {Kind: &structpb.Value_StringValue{"hello"}}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2330 | }, |
| 2331 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2332 | OptList: &structpb.ListValue{ |
| 2333 | Values: []*structpb.Value{ |
| 2334 | {Kind: &structpb.Value_NullValue{}}, |
| 2335 | {Kind: &structpb.Value_StringValue{}}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2336 | { |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2337 | Kind: &structpb.Value_StructValue{ |
| 2338 | &structpb.Struct{Fields: map[string]*structpb.Value{}}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2339 | }, |
| 2340 | }, |
| 2341 | { |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2342 | Kind: &structpb.Value_ListValue{ |
| 2343 | &structpb.ListValue{Values: []*structpb.Value{}}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2344 | }, |
| 2345 | }, |
| 2346 | }, |
| 2347 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2348 | OptValue: &structpb.Value{ |
| 2349 | Kind: &structpb.Value_StringValue{"world"}, |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2350 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2351 | OptEmpty: &emptypb.Empty{}, |
| 2352 | OptAny: &anypb.Any{ |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2353 | TypeUrl: "google.protobuf.Empty", |
| 2354 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2355 | OptFieldmask: &fieldmaskpb.FieldMask{ |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 2356 | Paths: []string{"foo_bar", "bar_foo"}, |
| 2357 | }, |
| 2358 | }, |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2359 | }, { |
| 2360 | desc: "DiscardUnknown: regular messages", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2361 | umo: protojson.UnmarshalOptions{DiscardUnknown: true}, |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2362 | inputMessage: &pb3.Nests{}, |
| 2363 | inputText: `{ |
| 2364 | "sNested": { |
| 2365 | "unknown": { |
| 2366 | "foo": 1, |
| 2367 | "bar": [1, 2, 3] |
| 2368 | } |
| 2369 | }, |
| 2370 | "unknown": "not known" |
| 2371 | }`, |
| 2372 | wantMessage: &pb3.Nests{SNested: &pb3.Nested{}}, |
| 2373 | }, { |
| 2374 | desc: "DiscardUnknown: repeated", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2375 | umo: protojson.UnmarshalOptions{DiscardUnknown: true}, |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2376 | inputMessage: &pb2.Nests{}, |
| 2377 | inputText: `{ |
| 2378 | "rptNested": [ |
| 2379 | {"unknown": "blah"}, |
| 2380 | {"optString": "hello"} |
| 2381 | ] |
| 2382 | }`, |
| 2383 | wantMessage: &pb2.Nests{ |
| 2384 | RptNested: []*pb2.Nested{ |
| 2385 | {}, |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 2386 | {OptString: proto.String("hello")}, |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2387 | }, |
| 2388 | }, |
| 2389 | }, { |
| 2390 | desc: "DiscardUnknown: map", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2391 | umo: protojson.UnmarshalOptions{DiscardUnknown: true}, |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2392 | inputMessage: &pb3.Maps{}, |
| 2393 | inputText: `{ |
| 2394 | "strToNested": { |
| 2395 | "nested_one": { |
| 2396 | "unknown": "what you see is not" |
| 2397 | } |
| 2398 | } |
| 2399 | }`, |
| 2400 | wantMessage: &pb3.Maps{ |
| 2401 | StrToNested: map[string]*pb3.Nested{ |
| 2402 | "nested_one": {}, |
| 2403 | }, |
| 2404 | }, |
| 2405 | }, { |
| 2406 | desc: "DiscardUnknown: extension", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2407 | umo: protojson.UnmarshalOptions{DiscardUnknown: true}, |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2408 | inputMessage: &pb2.Extensions{}, |
| 2409 | inputText: `{ |
| 2410 | "[pb2.opt_ext_nested]": { |
| 2411 | "unknown": [] |
| 2412 | } |
| 2413 | }`, |
| 2414 | wantMessage: func() proto.Message { |
| 2415 | m := &pb2.Extensions{} |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 2416 | proto.SetExtension(m, pb2.E_OptExtNested, &pb2.Nested{}) |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2417 | return m |
| 2418 | }(), |
| 2419 | }, { |
| 2420 | desc: "DiscardUnknown: Empty", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2421 | umo: protojson.UnmarshalOptions{DiscardUnknown: true}, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2422 | inputMessage: &emptypb.Empty{}, |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2423 | inputText: `{"unknown": "something"}`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2424 | wantMessage: &emptypb.Empty{}, |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2425 | }, { |
| 2426 | desc: "DiscardUnknown: Any without type", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2427 | umo: protojson.UnmarshalOptions{DiscardUnknown: true}, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2428 | inputMessage: &anypb.Any{}, |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2429 | inputText: `{ |
| 2430 | "value": {"foo": "bar"}, |
| 2431 | "unknown": true |
| 2432 | }`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2433 | wantMessage: &anypb.Any{}, |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2434 | }, { |
| 2435 | desc: "DiscardUnknown: Any", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2436 | umo: protojson.UnmarshalOptions{ |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2437 | DiscardUnknown: true, |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 2438 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})), |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2439 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2440 | inputMessage: &anypb.Any{}, |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2441 | inputText: `{ |
| 2442 | "@type": "foo/pb2.Nested", |
| 2443 | "unknown": "none" |
| 2444 | }`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2445 | wantMessage: &anypb.Any{ |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2446 | TypeUrl: "foo/pb2.Nested", |
| 2447 | }, |
| 2448 | }, { |
| 2449 | desc: "DiscardUnknown: Any with Empty", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 2450 | umo: protojson.UnmarshalOptions{ |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2451 | DiscardUnknown: true, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2452 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})), |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2453 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2454 | inputMessage: &anypb.Any{}, |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2455 | inputText: `{ |
| 2456 | "@type": "type.googleapis.com/google.protobuf.Empty", |
| 2457 | "value": {"unknown": 47} |
| 2458 | }`, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 2459 | wantMessage: &anypb.Any{ |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 2460 | TypeUrl: "type.googleapis.com/google.protobuf.Empty", |
| 2461 | }, |
Joe Tsai | d47ea19 | 2019-07-09 22:38:15 -0700 | [diff] [blame] | 2462 | }, { |
| 2463 | desc: "weak fields", |
| 2464 | inputMessage: &testpb.TestWeak{}, |
| 2465 | inputText: `{"weak_message1":{"a":1}}`, |
| 2466 | wantMessage: func() *testpb.TestWeak { |
| 2467 | m := new(testpb.TestWeak) |
| 2468 | m.SetWeakMessage1(&weakpb.WeakImportMessage1{A: proto.Int32(1)}) |
| 2469 | return m |
| 2470 | }(), |
Joe Tsai | 1799d11 | 2019-08-08 13:31:59 -0700 | [diff] [blame] | 2471 | skip: !flags.ProtoLegacy, |
Joe Tsai | d47ea19 | 2019-07-09 22:38:15 -0700 | [diff] [blame] | 2472 | }, { |
| 2473 | desc: "weak fields; unknown field", |
| 2474 | inputMessage: &testpb.TestWeak{}, |
| 2475 | inputText: `{"weak_message1":{"a":1}, "weak_message2":{"a":1}}`, |
| 2476 | wantErr: true, // weak_message2 is unknown since the package containing it is not imported |
Joe Tsai | 1799d11 | 2019-08-08 13:31:59 -0700 | [diff] [blame] | 2477 | skip: !flags.ProtoLegacy, |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 2478 | }} |
| 2479 | |
| 2480 | for _, tt := range tests { |
| 2481 | tt := tt |
Joe Tsai | d47ea19 | 2019-07-09 22:38:15 -0700 | [diff] [blame] | 2482 | if tt.skip { |
| 2483 | continue |
| 2484 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 2485 | t.Run(tt.desc, func(t *testing.T) { |
Joe Tsai | cdb7773 | 2019-05-14 16:05:06 -0700 | [diff] [blame] | 2486 | err := tt.umo.Unmarshal([]byte(tt.inputText), tt.inputMessage) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 2487 | if err != nil && !tt.wantErr { |
| 2488 | t.Errorf("Unmarshal() returned error: %v\n\n", err) |
| 2489 | } |
| 2490 | if err == nil && tt.wantErr { |
| 2491 | t.Error("Unmarshal() got nil error, want error\n\n") |
| 2492 | } |
Joe Tsai | 8d30bbe | 2019-05-16 15:53:25 -0700 | [diff] [blame] | 2493 | if tt.wantMessage != nil && !proto.Equal(tt.inputMessage, tt.wantMessage) { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 2494 | t.Errorf("Unmarshal()\n<got>\n%v\n<want>\n%v\n", tt.inputMessage, tt.wantMessage) |
| 2495 | } |
| 2496 | }) |
| 2497 | } |
| 2498 | } |