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