| 1 | package jsonparser |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | _ "fmt" |
| 8 | "reflect" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // Set it to non-empty value if want to run only specific test |
| 13 | var activeTest = "" |
| 14 | |
| 15 | // Test helper for SYS-REQ-006. |
| 16 | func toArray(data []byte) (result [][]byte) { |
| 17 | ArrayEach(data, func(value []byte, dataType ValueType, offset int, err error) { |
| 18 | result = append(result, value) |
| 19 | }) |
| 20 | |
| 21 | return |
| 22 | } |
| 23 | |
| 24 | // Test helper for SYS-REQ-006 and SYS-REQ-008. |
| 25 | func toStringArray(data []byte) (result []string) { |
| 26 | ArrayEach(data, func(value []byte, dataType ValueType, offset int, err error) { |
| 27 | result = append(result, string(value)) |
| 28 | }) |
| 29 | |
| 30 | return |
| 31 | } |
| 32 | |
| 33 | type GetTest struct { |
| 34 | desc string |
| 35 | json string |
| 36 | path []string |
| 37 | |
| 38 | isErr bool |
| 39 | isFound bool |
| 40 | |
| 41 | data interface{} |
| 42 | } |
| 43 | |
| 44 | type SetTest struct { |
| 45 | desc string |
| 46 | json string |
| 47 | setData string |
| 48 | path []string |
| 49 | |
| 50 | isErr bool |
| 51 | isFound bool |
| 52 | |
| 53 | data interface{} |
| 54 | } |
| 55 | |
| 56 | type DeleteTest struct { |
| 57 | desc string |
| 58 | json string |
| 59 | path []string |
| 60 | |
| 61 | data interface{} |
| 62 | } |
| 63 | |
| 64 | var deleteTests = []DeleteTest{ |
| 65 | { |
| 66 | desc: "Delete test key", |
| 67 | json: `{"test":"input"}`, |
| 68 | path: []string{"test"}, |
| 69 | data: `{}`, |
| 70 | }, |
| 71 | { |
| 72 | desc: "Delete object", |
| 73 | json: `{"test":"input"}`, |
| 74 | path: []string{}, |
| 75 | data: ``, |
| 76 | }, |
| 77 | { |
| 78 | desc: "Delete a nested object", |
| 79 | json: `{"test":"input","new.field":{"key": "new object"}}`, |
| 80 | path: []string{"new.field", "key"}, |
| 81 | data: `{"test":"input","new.field":{}}`, |
| 82 | }, |
| 83 | { |
| 84 | desc: "Deleting a key that doesn't exist should return the same object", |
| 85 | json: `{"test":"input"}`, |
| 86 | path: []string{"test2"}, |
| 87 | data: `{"test":"input"}`, |
| 88 | }, |
| 89 | { |
| 90 | desc: "Delete object in an array", |
| 91 | json: `{"test":[{"key":"val-obj1"}]}`, |
| 92 | path: []string{"test", "[0]"}, |
| 93 | data: `{"test":[]}`, |
| 94 | }, |
| 95 | { |
| 96 | desc: "Deleting a object in an array that doesn't exists should return the same object", |
| 97 | json: `{"test":[{"key":"val-obj1"}]}`, |
| 98 | path: []string{"test", "[1]"}, |
| 99 | data: `{"test":[{"key":"val-obj1"}]}`, |
| 100 | }, |
| 101 | { |
| 102 | desc: "Delete a complex object in a nested array", |
| 103 | json: `{"test":[{"key":[{"innerKey":"innerKeyValue"}]}]}`, |
| 104 | path: []string{"test", "[0]", "key", "[0]"}, |
| 105 | data: `{"test":[{"key":[]}]}`, |
| 106 | }, |
| 107 | { |
| 108 | desc: "Delete known key (simple type within nested array)", |
| 109 | json: `{"test":[{"key":["innerKey"]}]}`, |
| 110 | path: []string{"test", "[0]", "key", "[0]"}, |
| 111 | data: `{"test":[{"key":[]}]}`, |
| 112 | }, |
| 113 | { |
| 114 | desc: "Delete in empty json", |
| 115 | json: `{}`, |
| 116 | path: []string{}, |
| 117 | data: ``, |
| 118 | }, |
| 119 | { |
| 120 | desc: "Delete empty array", |
| 121 | json: `[]`, |
| 122 | path: []string{}, |
| 123 | data: ``, |
| 124 | }, |
| 125 | { |
| 126 | desc: "Deleting non json should return the same value", |
| 127 | json: `1.323`, |
| 128 | path: []string{"foo"}, |
| 129 | data: `1.323`, |
| 130 | }, |
| 131 | { |
| 132 | desc: "Delete known key (top level array)", |
| 133 | json: `[{"key":"val-obj1"}]`, |
| 134 | path: []string{"[0]"}, |
| 135 | data: `[]`, |
| 136 | }, |
| 137 | { // This test deletes the key instead of returning a parse error, as checking for the malformed JSON would reduce performance (this is not ideal) |
| 138 | desc: `malformed with trailing whitespace`, |
| 139 | json: `{"a":1 `, |
| 140 | path: []string{"a"}, |
| 141 | data: `{ `, |
| 142 | }, |
| 143 | { // This test dels the key instead of returning a parse error, as checking for the malformed JSON would reduce performance (this is not ideal) |
| 144 | desc: "malformed 'colon chain', delete b", |
| 145 | json: `{"a":"b":"c"}`, |
| 146 | path: []string{"b"}, |
| 147 | data: `{"a":}`, |
| 148 | }, |
| 149 | { |
| 150 | desc: "Delete object without inner array", |
| 151 | json: `{"a": {"b": 1}, "b": 2}`, |
| 152 | path: []string{"b"}, |
| 153 | data: `{"a": {"b": 1}}`, |
| 154 | }, |
| 155 | { |
| 156 | desc: "Delete object without inner array", |
| 157 | json: `{"a": [{"b": 1}], "b": 2}`, |
| 158 | path: []string{"b"}, |
| 159 | data: `{"a": [{"b": 1}]}`, |
| 160 | }, |
| 161 | { |
| 162 | desc: "Delete object without inner array", |
| 163 | json: `{"a": {"c": {"b": 3}, "b": 1}, "b": 2}`, |
| 164 | path: []string{"a", "b"}, |
| 165 | data: `{"a": {"c": {"b": 3}}, "b": 2}`, |
| 166 | }, |
| 167 | { |
| 168 | desc: "Delete object without inner array", |
| 169 | json: `{"a": [{"c": {"b": 3}, "b": 1}], "b": 2}`, |
| 170 | path: []string{"a", "[0]", "b"}, |
| 171 | data: `{"a": [{"c": {"b": 3}}], "b": 2}`, |
| 172 | }, |
| 173 | { |
| 174 | desc: "Remove trailing comma if last object is deleted", |
| 175 | json: `{"a": "1", "b": "2"}`, |
| 176 | path: []string{"b"}, |
| 177 | data: `{"a": "1"}`, |
| 178 | }, |
| 179 | { |
| 180 | desc: "Correctly delete first element with space-comma", |
| 181 | json: `{"a": "1" ,"b": "2" }`, |
| 182 | path: []string{"a"}, |
| 183 | data: `{"b": "2" }`, |
| 184 | }, |
| 185 | { |
| 186 | desc: "Correctly delete middle element with space-comma", |
| 187 | json: `{"a": "1" ,"b": "2" , "c": 3}`, |
| 188 | path: []string{"b"}, |
| 189 | data: `{"a": "1" , "c": 3}`, |
| 190 | }, |
| 191 | { |
| 192 | desc: "Delete non-last key", |
| 193 | json: `{"test":"input","test1":"input1"}`, |
| 194 | path: []string{"test"}, |
| 195 | data: `{"test1":"input1"}`, |
| 196 | }, |
| 197 | { |
| 198 | desc: "Delete non-exist key", |
| 199 | json: `{"test:":"input"}`, |
| 200 | path: []string{"test", "test1"}, |
| 201 | data: `{"test:":"input"}`, |
| 202 | }, |
| 203 | { |
| 204 | desc: "Delete non-last object in an array", |
| 205 | json: `[{"key":"val-obj1"},{"key2":"val-obj2"}]`, |
| 206 | path: []string{"[0]"}, |
| 207 | data: `[{"key2":"val-obj2"}]`, |
| 208 | }, |
| 209 | { |
| 210 | desc: "Delete non-first object in an array", |
| 211 | json: `[{"key":"val-obj1"},{"key2":"val-obj2"}]`, |
| 212 | path: []string{"[1]"}, |
| 213 | data: `[{"key":"val-obj1"}]`, |
| 214 | }, |
| 215 | { |
| 216 | desc: "Issue #188: infinite loop in Delete", |
| 217 | json: `^_�^C^A^@[`, |
| 218 | path: []string{""}, |
| 219 | data: `^_�^C^A^@[`, |
| 220 | }, |
| 221 | { |
| 222 | desc: "Issue #188: infinite loop in Delete", |
| 223 | json: `^_�^C^A^@{`, |
| 224 | path: []string{""}, |
| 225 | data: `^_�^C^A^@{`, |
| 226 | }, |
| 227 | { |
| 228 | desc: "Issue #150: leading space", |
| 229 | json: ` {"test":"input"}`, |
| 230 | path: []string{"test"}, |
| 231 | data: ` {}`, |
| 232 | }, |
| 233 | { |
| 234 | desc: "GO-2026-4514: malformed JSON without enclosing braces should not panic", |
| 235 | json: `"0":"0":`, |
| 236 | path: []string{"0"}, |
| 237 | data: `"0":"0":`, |
| 238 | }, |
| 239 | { |
| 240 | desc: "GO-2026-4514: malformed JSON with key but truncated value should not panic", |
| 241 | json: `{"a": `, |
| 242 | path: []string{"a"}, |
| 243 | data: `{"a": `, |
| 244 | }, |
| 245 | { |
| 246 | desc: "GO-2026-4514: malformed nested JSON with truncated value should not panic", |
| 247 | json: `{"a":{"b": `, |
| 248 | path: []string{"a", "b"}, |
| 249 | data: `{"a":{"b": `, |
| 250 | }, |
| 251 | } |
| 252 | |
| 253 | var setTests = []SetTest{ |
| 254 | { |
| 255 | desc: "set unknown key (string)", |
| 256 | json: `{"test":"input"}`, |
| 257 | isFound: true, |
| 258 | path: []string{"new.field"}, |
| 259 | setData: `"new value"`, |
| 260 | data: `{"test":"input","new.field":"new value"}`, |
| 261 | }, |
| 262 | { |
| 263 | desc: "set known key (string)", |
| 264 | json: `{"test":"input"}`, |
| 265 | isFound: true, |
| 266 | path: []string{"test"}, |
| 267 | setData: `"new value"`, |
| 268 | data: `{"test":"new value"}`, |
| 269 | }, |
| 270 | { |
| 271 | desc: "set unknown key (object)", |
| 272 | json: `{"test":"input"}`, |
| 273 | isFound: true, |
| 274 | path: []string{"new.field"}, |
| 275 | setData: `{"key": "new object"}`, |
| 276 | data: `{"test":"input","new.field":{"key": "new object"}}`, |
| 277 | }, |
| 278 | { |
| 279 | desc: "set known key (object)", |
| 280 | json: `{"test":"input"}`, |
| 281 | isFound: true, |
| 282 | path: []string{"test"}, |
| 283 | setData: `{"key": "new object"}`, |
| 284 | data: `{"test":{"key": "new object"}}`, |
| 285 | }, |
| 286 | { |
| 287 | desc: "set known key (object within array)", |
| 288 | json: `{"test":[{"key":"val-obj1"}]}`, |
| 289 | isFound: true, |
| 290 | path: []string{"test", "[0]"}, |
| 291 | setData: `{"key":"new object"}`, |
| 292 | data: `{"test":[{"key":"new object"}]}`, |
| 293 | }, |
| 294 | { |
| 295 | desc: "set unknown key (replace object)", |
| 296 | json: `{"test":[{"key":"val-obj1"}]}`, |
| 297 | isFound: true, |
| 298 | path: []string{"test", "newKey"}, |
| 299 | setData: `"new object"`, |
| 300 | data: `{"test":{"newKey":"new object"}}`, |
| 301 | }, |
| 302 | { |
| 303 | desc: "set unknown key (complex object within nested array)", |
| 304 | json: `{"test":[{"key":[{"innerKey":"innerKeyValue"}]}]}`, |
| 305 | isFound: true, |
| 306 | path: []string{"test", "[0]", "key", "[0]", "newInnerKey"}, |
| 307 | setData: `{"key":"new object"}`, |
| 308 | data: `{"test":[{"key":[{"innerKey":"innerKeyValue","newInnerKey":{"key":"new object"}}]}]}`, |
| 309 | }, |
| 310 | { |
| 311 | desc: "set known key (complex object within nested array)", |
| 312 | json: `{"test":[{"key":[{"innerKey":"innerKeyValue"}]}]}`, |
| 313 | isFound: true, |
| 314 | path: []string{"test", "[0]", "key", "[0]", "innerKey"}, |
| 315 | setData: `{"key":"new object"}`, |
| 316 | data: `{"test":[{"key":[{"innerKey":{"key":"new object"}}]}]}`, |
| 317 | }, |
| 318 | { |
| 319 | desc: "set unknown key (object, partial subtree exists)", |
| 320 | json: `{"test":{"input":"output"}}`, |
| 321 | isFound: true, |
| 322 | path: []string{"test", "new.field"}, |
| 323 | setData: `{"key":"new object"}`, |
| 324 | data: `{"test":{"input":"output","new.field":{"key":"new object"}}}`, |
| 325 | }, |
| 326 | { |
| 327 | desc: "set unknown key (object, empty partial subtree exists)", |
| 328 | json: `{"test":{}}`, |
| 329 | isFound: true, |
| 330 | path: []string{"test", "new.field"}, |
| 331 | setData: `{"key":"new object"}`, |
| 332 | data: `{"test":{"new.field":{"key":"new object"}}}`, |
| 333 | }, |
| 334 | { |
| 335 | desc: "set unknown key (object, no subtree exists)", |
| 336 | json: `{"test":"input"}`, |
| 337 | isFound: true, |
| 338 | path: []string{"new.field", "nested", "value"}, |
| 339 | setData: `{"key": "new object"}`, |
| 340 | data: `{"test":"input","new.field":{"nested":{"value":{"key": "new object"}}}}`, |
| 341 | }, |
| 342 | { |
| 343 | desc: "set in empty json", |
| 344 | json: `{}`, |
| 345 | isFound: true, |
| 346 | path: []string{"foo"}, |
| 347 | setData: `"null"`, |
| 348 | data: `{"foo":"null"}`, |
| 349 | }, |
| 350 | { |
| 351 | desc: "set subtree in empty json", |
| 352 | json: `{}`, |
| 353 | isFound: true, |
| 354 | path: []string{"foo", "bar"}, |
| 355 | setData: `"null"`, |
| 356 | data: `{"foo":{"bar":"null"}}`, |
| 357 | }, |
| 358 | { |
| 359 | desc: "set in empty string - not found", |
| 360 | json: ``, |
| 361 | isFound: false, |
| 362 | path: []string{"foo"}, |
| 363 | setData: `"null"`, |
| 364 | data: ``, |
| 365 | }, |
| 366 | { |
| 367 | desc: "set in Number - not found", |
| 368 | json: `1.323`, |
| 369 | isFound: false, |
| 370 | path: []string{"foo"}, |
| 371 | setData: `"null"`, |
| 372 | data: `1.323`, |
| 373 | }, |
| 374 | { |
| 375 | desc: "set known key (top level array)", |
| 376 | json: `[{"key":"val-obj1"}]`, |
| 377 | isFound: true, |
| 378 | path: []string{"[0]", "key"}, |
| 379 | setData: `"new object"`, |
| 380 | data: `[{"key":"new object"}]`, |
| 381 | }, |
| 382 | { |
| 383 | desc: "set unknown key (trailing whitespace)", |
| 384 | json: `{"key":"val-obj1"} `, |
| 385 | isFound: true, |
| 386 | path: []string{"alt-key"}, |
| 387 | setData: `"new object"`, |
| 388 | data: `{"key":"val-obj1","alt-key":"new object"} `, |
| 389 | }, |
| 390 | { // This test sets the key instead of returning a parse error, as checking for the malformed JSON would reduce performance (this is not ideal) |
| 391 | desc: `malformed with trailing whitespace`, |
| 392 | json: `{"a":1 `, |
| 393 | path: []string{"a"}, |
| 394 | setData: `2`, |
| 395 | isFound: true, |
| 396 | data: `{"a":2 `, |
| 397 | }, |
| 398 | { // This test sets the key instead of returning a parse error, as checking for the malformed JSON would reduce performance (this is not ideal) |
| 399 | desc: "malformed 'colon chain', set second string", |
| 400 | json: `{"a":"b":"c"}`, |
| 401 | path: []string{"b"}, |
| 402 | setData: `"d"`, |
| 403 | isFound: true, |
| 404 | data: `{"a":"b":"d"}`, |
| 405 | }, |
| 406 | { |
| 407 | desc: "set indexed path to object on empty JSON", |
| 408 | json: `{}`, |
| 409 | path: []string{"top", "[0]", "middle", "[0]", "bottom"}, |
| 410 | setData: `"value"`, |
| 411 | isFound: true, |
| 412 | data: `{"top":[{"middle":[{"bottom":"value"}]}]}`, |
| 413 | }, |
| 414 | { |
| 415 | desc: "set indexed path on existing object with object", |
| 416 | json: `{"top":[{"middle":[]}]}`, |
| 417 | path: []string{"top", "[0]", "middle", "[0]", "bottom"}, |
| 418 | setData: `"value"`, |
| 419 | isFound: true, |
| 420 | data: `{"top":[{"middle":[{"bottom":"value"}]}]}`, |
| 421 | }, |
| 422 | { |
| 423 | desc: "set indexed path on existing object with value", |
| 424 | json: `{"top":[{"middle":[]}]}`, |
| 425 | path: []string{"top", "[0]", "middle", "[0]"}, |
| 426 | setData: `"value"`, |
| 427 | isFound: true, |
| 428 | data: `{"top":[{"middle":["value"]}]}`, |
| 429 | }, |
| 430 | { |
| 431 | desc: "set indexed path on empty object with value", |
| 432 | json: `{}`, |
| 433 | path: []string{"top", "[0]", "middle", "[0]"}, |
| 434 | setData: `"value"`, |
| 435 | isFound: true, |
| 436 | data: `{"top":[{"middle":["value"]}]}`, |
| 437 | }, |
| 438 | { |
| 439 | desc: "set indexed path on object with existing array", |
| 440 | json: `{"top":["one", "two", "three"]}`, |
| 441 | path: []string{"top", "[2]"}, |
| 442 | setData: `"value"`, |
| 443 | isFound: true, |
| 444 | data: `{"top":["one", "two", "value"]}`, |
| 445 | }, |
| 446 | { |
| 447 | desc: "set non-exist key", |
| 448 | json: `{"test":"input"}`, |
| 449 | setData: `"new value"`, |
| 450 | isFound: false, |
| 451 | }, |
| 452 | { |
| 453 | desc: "set key in invalid json", |
| 454 | json: `{"test"::"input"}`, |
| 455 | path: []string{"test"}, |
| 456 | setData: "new value", |
| 457 | isErr: true, |
| 458 | }, |
| 459 | { |
| 460 | desc: "set unknown key (simple object within nested array)", |
| 461 | json: `{"test":{"key":[{"innerKey":"innerKeyValue", "innerKey2":"innerKeyValue2"}]}}`, |
| 462 | isFound: true, |
| 463 | path: []string{"test", "key", "[1]", "newInnerKey"}, |
| 464 | setData: `"new object"`, |
| 465 | data: `{"test":{"key":[{"innerKey":"innerKeyValue", "innerKey2":"innerKeyValue2"},{"newInnerKey":"new object"}]}}`, |
| 466 | }, |
| 467 | } |
| 468 | |
| 469 | var getTests = []GetTest{ |
| 470 | // Trivial tests |
| 471 | { |
| 472 | desc: "read string", |
| 473 | json: `""`, |
| 474 | isFound: true, |
| 475 | data: ``, |
| 476 | }, |
| 477 | { |
| 478 | desc: "read number", |
| 479 | json: `0`, |
| 480 | isFound: true, |
| 481 | data: `0`, |
| 482 | }, |
| 483 | { |
| 484 | desc: "read object", |
| 485 | json: `{}`, |
| 486 | isFound: true, |
| 487 | data: `{}`, |
| 488 | }, |
| 489 | { |
| 490 | desc: "read array", |
| 491 | json: `[]`, |
| 492 | isFound: true, |
| 493 | data: `[]`, |
| 494 | }, |
| 495 | { |
| 496 | desc: "read boolean", |
| 497 | json: `true`, |
| 498 | isFound: true, |
| 499 | data: `true`, |
| 500 | }, |
| 501 | |
| 502 | // Found key tests |
| 503 | { |
| 504 | desc: "handling multiple nested keys with same name", |
| 505 | json: `{"a":[{"b":1},{"b":2},3],"c":{"c":[1,2]}} }`, |
| 506 | path: []string{"c", "c"}, |
| 507 | isFound: true, |
| 508 | data: `[1,2]`, |
| 509 | }, |
| 510 | { |
| 511 | desc: "read basic key", |
| 512 | json: `{"a":"b"}`, |
| 513 | path: []string{"a"}, |
| 514 | isFound: true, |
| 515 | data: `b`, |
| 516 | }, |
| 517 | { |
| 518 | desc: "read basic key with space", |
| 519 | json: `{"a": "b"}`, |
| 520 | path: []string{"a"}, |
| 521 | isFound: true, |
| 522 | data: `b`, |
| 523 | }, |
| 524 | { |
| 525 | desc: "read composite key", |
| 526 | json: `{"a": { "b":{"c":"d" }}}`, |
| 527 | path: []string{"a", "b", "c"}, |
| 528 | isFound: true, |
| 529 | data: `d`, |
| 530 | }, |
| 531 | { |
| 532 | desc: `read numberic value as string`, |
| 533 | json: `{"a": "b", "c": 1}`, |
| 534 | path: []string{"c"}, |
| 535 | isFound: true, |
| 536 | data: `1`, |
| 537 | }, |
| 538 | { |
| 539 | desc: `handle multiple nested keys with same name`, |
| 540 | json: `{"a":[{"b":1},{"b":2},3],"c":{"c":[1,2]}} }`, |
| 541 | path: []string{"c", "c"}, |
| 542 | isFound: true, |
| 543 | data: `[1,2]`, |
| 544 | }, |
| 545 | { |
| 546 | desc: `read string values with quotes`, |
| 547 | json: `{"a": "string\"with\"quotes"}`, |
| 548 | path: []string{"a"}, |
| 549 | isFound: true, |
| 550 | data: `string\"with\"quotes`, |
| 551 | }, |
| 552 | { |
| 553 | desc: `read object`, |
| 554 | json: `{"a": { "b":{"c":"d" }}}`, |
| 555 | path: []string{"a", "b"}, |
| 556 | isFound: true, |
| 557 | data: `{"c":"d" }`, |
| 558 | }, |
| 559 | { |
| 560 | desc: `empty path`, |
| 561 | json: `{"c":"d" }`, |
| 562 | path: []string{}, |
| 563 | isFound: true, |
| 564 | data: `{"c":"d" }`, |
| 565 | }, |
| 566 | { |
| 567 | desc: `formatted JSON value`, |
| 568 | json: "{\n \"a\": \"b\"\n}", |
| 569 | path: []string{"a"}, |
| 570 | isFound: true, |
| 571 | data: `b`, |
| 572 | }, |
| 573 | { |
| 574 | desc: `formatted JSON value 2`, |
| 575 | json: "{\n \"a\":\n {\n\"b\":\n {\"c\":\"d\",\n\"e\": \"f\"}\n}\n}", |
| 576 | path: []string{"a", "b"}, |
| 577 | isFound: true, |
| 578 | data: "{\"c\":\"d\",\n\"e\": \"f\"}", |
| 579 | }, |
| 580 | { |
| 581 | desc: `whitespace`, |
| 582 | json: " \n\r\t{ \n\r\t\"whitespace\" \n\r\t: \n\r\t333 \n\r\t} \n\r\t", |
| 583 | path: []string{"whitespace"}, |
| 584 | isFound: true, |
| 585 | data: "333", |
| 586 | }, |
| 587 | { |
| 588 | desc: `escaped backslash quote`, |
| 589 | json: `{"a": "\\\""}`, |
| 590 | path: []string{"a"}, |
| 591 | isFound: true, |
| 592 | data: `\\\"`, |
| 593 | }, |
| 594 | { |
| 595 | desc: `unescaped backslash quote`, |
| 596 | json: `{"a": "\\"}`, |
| 597 | path: []string{"a"}, |
| 598 | isFound: true, |
| 599 | data: `\\`, |
| 600 | }, |
| 601 | { |
| 602 | desc: `unicode in JSON`, |
| 603 | json: `{"a": "15°C"}`, |
| 604 | path: []string{"a"}, |
| 605 | isFound: true, |
| 606 | data: `15°C`, |
| 607 | }, |
| 608 | { |
| 609 | desc: `no padding + nested`, |
| 610 | json: `{"a":{"a":"1"},"b":2}`, |
| 611 | path: []string{"b"}, |
| 612 | isFound: true, |
| 613 | data: `2`, |
| 614 | }, |
| 615 | { |
| 616 | desc: `no padding + nested + array`, |
| 617 | json: `{"a":{"b":[1,2]},"c":3}`, |
| 618 | path: []string{"c"}, |
| 619 | isFound: true, |
| 620 | data: `3`, |
| 621 | }, |
| 622 | { |
| 623 | desc: `empty key`, |
| 624 | json: `{"":{"":{"":true}}}`, |
| 625 | path: []string{"", "", ""}, |
| 626 | isFound: true, |
| 627 | data: `true`, |
| 628 | }, |
| 629 | |
| 630 | // Escaped key tests |
| 631 | { |
| 632 | desc: `key with simple escape`, |
| 633 | json: `{"a\\b":1}`, |
| 634 | path: []string{"a\\b"}, |
| 635 | isFound: true, |
| 636 | data: `1`, |
| 637 | }, |
| 638 | { |
| 639 | desc: `key and value with whitespace escapes`, |
| 640 | json: `{"key\b\f\n\r\tkey":"value\b\f\n\r\tvalue"}`, |
| 641 | path: []string{"key\b\f\n\r\tkey"}, |
| 642 | isFound: true, |
| 643 | data: `value\b\f\n\r\tvalue`, // value is not unescaped since this is Get(), but the key should work correctly |
| 644 | }, |
| 645 | { |
| 646 | desc: `key with Unicode escape`, |
| 647 | json: `{"a\u00B0b":1}`, |
| 648 | path: []string{"a\u00B0b"}, |
| 649 | isFound: true, |
| 650 | data: `1`, |
| 651 | }, |
| 652 | { |
| 653 | desc: `key with complex escape`, |
| 654 | json: `{"a\uD83D\uDE03b":1}`, |
| 655 | path: []string{"a\U0001F603b"}, |
| 656 | isFound: true, |
| 657 | data: `1`, |
| 658 | }, |
| 659 | |
| 660 | { // This test returns a match instead of a parse error, as checking for the malformed JSON would reduce performance |
| 661 | desc: `malformed with trailing whitespace`, |
| 662 | json: `{"a":1 `, |
| 663 | path: []string{"a"}, |
| 664 | isFound: true, |
| 665 | data: `1`, |
| 666 | }, |
| 667 | { // This test returns a match instead of a parse error, as checking for the malformed JSON would reduce performance |
| 668 | desc: `malformed with wrong closing bracket`, |
| 669 | json: `{"a":1]`, |
| 670 | path: []string{"a"}, |
| 671 | isFound: true, |
| 672 | data: `1`, |
| 673 | }, |
| 674 | |
| 675 | // Not found key tests |
| 676 | { |
| 677 | desc: `empty input`, |
| 678 | json: ``, |
| 679 | path: []string{"a"}, |
| 680 | isFound: false, |
| 681 | }, |
| 682 | { |
| 683 | desc: "non-existent key 1", |
| 684 | json: `{"a":"b"}`, |
| 685 | path: []string{"c"}, |
| 686 | isFound: false, |
| 687 | }, |
| 688 | { |
| 689 | desc: "non-existent key 2", |
| 690 | json: `{"a":"b"}`, |
| 691 | path: []string{"b"}, |
| 692 | isFound: false, |
| 693 | }, |
| 694 | { |
| 695 | desc: "non-existent key 3", |
| 696 | json: `{"aa":"b"}`, |
| 697 | path: []string{"a"}, |
| 698 | isFound: false, |
| 699 | }, |
| 700 | { |
| 701 | desc: "apply scope of parent when search for nested key", |
| 702 | json: `{"a": { "b": 1}, "c": 2 }`, |
| 703 | path: []string{"a", "b", "c"}, |
| 704 | isFound: false, |
| 705 | }, |
| 706 | { |
| 707 | desc: `apply scope to key level`, |
| 708 | json: `{"a": { "b": 1}, "c": 2 }`, |
| 709 | path: []string{"b"}, |
| 710 | isFound: false, |
| 711 | }, |
| 712 | { |
| 713 | desc: `handle escaped quote in key name in JSON`, |
| 714 | json: `{"key\"key": 1}`, |
| 715 | path: []string{"key"}, |
| 716 | isFound: false, |
| 717 | }, |
| 718 | { |
| 719 | desc: "handling multiple keys with different name", |
| 720 | json: `{"a":{"a":1},"b":{"a":3,"c":[1,2]}}`, |
| 721 | path: []string{"a", "c"}, |
| 722 | isFound: false, |
| 723 | }, |
| 724 | { |
| 725 | desc: "handling nested json", |
| 726 | json: `{"a":{"b":{"c":1},"d":4}}`, |
| 727 | path: []string{"a", "d"}, |
| 728 | isFound: true, |
| 729 | data: `4`, |
| 730 | }, |
| 731 | { // Issue #148 |
| 732 | desc: `missing key in different key same level`, |
| 733 | json: `{"s":"s","ic":2,"r":{"o":"invalid"}}`, |
| 734 | path: []string{"ic", "o"}, |
| 735 | isFound: false, |
| 736 | }, |
| 737 | |
| 738 | // Error/invalid tests |
| 739 | { |
| 740 | desc: `handle escaped quote in key name in JSON`, |
| 741 | json: `{"key\"key": 1}`, |
| 742 | path: []string{"key"}, |
| 743 | isFound: false, |
| 744 | }, |
| 745 | { |
| 746 | desc: `missing closing brace, but can still find key`, |
| 747 | json: `{"a":"b"`, |
| 748 | path: []string{"a"}, |
| 749 | isFound: true, |
| 750 | data: `b`, |
| 751 | }, |
| 752 | { |
| 753 | desc: `missing value closing quote`, |
| 754 | json: `{"a":"b`, |
| 755 | path: []string{"a"}, |
| 756 | isErr: true, |
| 757 | }, |
| 758 | { |
| 759 | desc: `missing value closing curly brace`, |
| 760 | json: `{"a": { "b": "c"`, |
| 761 | path: []string{"a"}, |
| 762 | isErr: true, |
| 763 | }, |
| 764 | { |
| 765 | desc: `missing value closing square bracket`, |
| 766 | json: `{"a": [1, 2, 3 }`, |
| 767 | path: []string{"a"}, |
| 768 | isErr: true, |
| 769 | }, |
| 770 | { |
| 771 | desc: `missing value 1`, |
| 772 | json: `{"a":`, |
| 773 | path: []string{"a"}, |
| 774 | isErr: true, |
| 775 | }, |
| 776 | { |
| 777 | desc: `missing value 2`, |
| 778 | json: `{"a": `, |
| 779 | path: []string{"a"}, |
| 780 | isErr: true, |
| 781 | }, |
| 782 | { |
| 783 | desc: `missing value 3`, |
| 784 | json: `{"a":}`, |
| 785 | path: []string{"a"}, |
| 786 | isErr: true, |
| 787 | }, |
| 788 | { |
| 789 | desc: `malformed array (no closing brace)`, |
| 790 | json: `{"a":[, "b":123}`, |
| 791 | path: []string{"b"}, |
| 792 | isFound: false, |
| 793 | }, |
| 794 | { // Issue #81 |
| 795 | desc: `missing key in object in array`, |
| 796 | json: `{"p":{"a":[{"u":"abc","t":"th"}]}}`, |
| 797 | path: []string{"p", "a", "[0]", "x"}, |
| 798 | isFound: false, |
| 799 | }, |
| 800 | { // Issue #81 counter test |
| 801 | desc: `existing key in object in array`, |
| 802 | json: `{"p":{"a":[{"u":"abc","t":"th"}]}}`, |
| 803 | path: []string{"p", "a", "[0]", "u"}, |
| 804 | isFound: true, |
| 805 | data: "abc", |
| 806 | }, |
| 807 | { // This test returns not found instead of a parse error, as checking for the malformed JSON would reduce performance |
| 808 | desc: "malformed key (followed by comma followed by colon)", |
| 809 | json: `{"a",:1}`, |
| 810 | path: []string{"a"}, |
| 811 | isFound: false, |
| 812 | }, |
| 813 | { // This test returns a match instead of a parse error, as checking for the malformed JSON would reduce performance (this is not ideal) |
| 814 | desc: "malformed 'colon chain', lookup first string", |
| 815 | json: `{"a":"b":"c"}`, |
| 816 | path: []string{"a"}, |
| 817 | isFound: true, |
| 818 | data: "b", |
| 819 | }, |
| 820 | { // This test returns a match instead of a parse error, as checking for the malformed JSON would reduce performance (this is not ideal) |
| 821 | desc: "malformed 'colon chain', lookup second string", |
| 822 | json: `{"a":"b":"c"}`, |
| 823 | path: []string{"b"}, |
| 824 | isFound: true, |
| 825 | data: "c", |
| 826 | }, |
| 827 | // Array index paths |
| 828 | { |
| 829 | desc: "last key in path is index", |
| 830 | json: `{"a":[{"b":1},{"b":"2"}, 3],"c":{"c":[1,2]}}`, |
| 831 | path: []string{"a", "[1]"}, |
| 832 | isFound: true, |
| 833 | data: `{"b":"2"}`, |
| 834 | }, |
| 835 | { |
| 836 | desc: "get string from array", |
| 837 | json: `{"a":[{"b":1},"foo", 3],"c":{"c":[1,2]}}`, |
| 838 | path: []string{"a", "[1]"}, |
| 839 | isFound: true, |
| 840 | data: "foo", |
| 841 | }, |
| 842 | { |
| 843 | desc: "key in path is index", |
| 844 | json: `{"a":[{"b":"1"},{"b":"2"},3],"c":{"c":[1,2]}}`, |
| 845 | path: []string{"a", "[0]", "b"}, |
| 846 | isFound: true, |
| 847 | data: `1`, |
| 848 | }, |
| 849 | { |
| 850 | desc: "last key in path is an index to value in array (formatted json)", |
| 851 | json: `{ |
| 852 | "a": [ |
| 853 | { |
| 854 | "b": 1 |
| 855 | }, |
| 856 | {"b":"2"}, |
| 857 | 3 |
| 858 | ], |
| 859 | "c": { |
| 860 | "c": [ |
| 861 | 1, |
| 862 | 2 |
| 863 | ] |
| 864 | } |
| 865 | }`, |
| 866 | path: []string{"a", "[1]"}, |
| 867 | isFound: true, |
| 868 | data: `{"b":"2"}`, |
| 869 | }, |
| 870 | { |
| 871 | desc: "key in path is index (formatted json)", |
| 872 | json: `{ |
| 873 | "a": [ |
| 874 | {"b": 1}, |
| 875 | {"b": "2"}, |
| 876 | 3 |
| 877 | ], |
| 878 | "c": { |
| 879 | "c": [ |
| 880 | 1, |
| 881 | 2 |
| 882 | ] |
| 883 | } |
| 884 | }`, |
| 885 | path: []string{"a", "[0]", "b"}, |
| 886 | isFound: true, |
| 887 | data: `1`, |
| 888 | }, |
| 889 | { |
| 890 | // Issue #178: Crash in searchKeys |
| 891 | desc: `invalid json`, |
| 892 | json: `{{{"":`, |
| 893 | path: []string{"a", "b"}, |
| 894 | isFound: false, |
| 895 | }, |
| 896 | { |
| 897 | desc: `opening brace instead of closing and without key`, |
| 898 | json: `{"a":1{`, |
| 899 | path: []string{"b"}, |
| 900 | isFound: false, |
| 901 | }, |
| 902 | } |
| 903 | |
| 904 | var getIntTests = []GetTest{ |
| 905 | { |
| 906 | desc: `read numeric value as number`, |
| 907 | json: `{"a": "b", "c": 1}`, |
| 908 | path: []string{"c"}, |
| 909 | isFound: true, |
| 910 | data: int64(1), |
| 911 | }, |
| 912 | { |
| 913 | desc: `read numeric value as number in formatted JSON`, |
| 914 | json: "{\"a\": \"b\", \"c\": 1 \n}", |
| 915 | path: []string{"c"}, |
| 916 | isFound: true, |
| 917 | data: int64(1), |
| 918 | }, |
| 919 | { // Issue #138: overflow detection |
| 920 | desc: `Fails because of overflow`, |
| 921 | json: `{"p":9223372036854775808}`, |
| 922 | path: []string{"p"}, |
| 923 | isErr: true, |
| 924 | }, |
| 925 | { // Issue #138: overflow detection |
| 926 | desc: `Fails because of underflow`, |
| 927 | json: `{"p":-9223372036854775809}`, |
| 928 | path: []string{"p"}, |
| 929 | isErr: true, |
| 930 | }, |
| 931 | { |
| 932 | desc: `read non-numeric value as integer`, |
| 933 | json: `{"a": "b", "c": "d"}`, |
| 934 | path: []string{"c"}, |
| 935 | isErr: true, |
| 936 | }, |
| 937 | { |
| 938 | desc: `null test`, |
| 939 | json: `{"a": "b", "c": null}`, |
| 940 | path: []string{"c"}, |
| 941 | isErr: true, |
| 942 | }, |
| 943 | } |
| 944 | |
| 945 | var getFloatTests = []GetTest{ |
| 946 | { |
| 947 | desc: `read numeric value as number`, |
| 948 | json: `{"a": "b", "c": 1.123}`, |
| 949 | path: []string{"c"}, |
| 950 | isFound: true, |
| 951 | data: float64(1.123), |
| 952 | }, |
| 953 | { |
| 954 | desc: `read numeric value as number in formatted JSON`, |
| 955 | json: "{\"a\": \"b\", \"c\": 23.41323 \n}", |
| 956 | path: []string{"c"}, |
| 957 | isFound: true, |
| 958 | data: float64(23.41323), |
| 959 | }, |
| 960 | { |
| 961 | desc: `read non-numeric value as float`, |
| 962 | json: `{"a": "b", "c": "d"}`, |
| 963 | path: []string{"c"}, |
| 964 | isErr: true, |
| 965 | }, |
| 966 | { |
| 967 | desc: `null test`, |
| 968 | json: `{"a": "b", "c": null}`, |
| 969 | path: []string{"c"}, |
| 970 | isErr: true, |
| 971 | }, |
| 972 | } |
| 973 | |
| 974 | var getStringTests = []GetTest{ |
| 975 | { |
| 976 | desc: `Translate Unicode symbols`, |
| 977 | json: `{"c": "test"}`, |
| 978 | path: []string{"c"}, |
| 979 | isFound: true, |
| 980 | data: `test`, |
| 981 | }, |
| 982 | { |
| 983 | desc: `Translate Unicode symbols`, |
| 984 | json: `{"c": "15\u00b0C"}`, |
| 985 | path: []string{"c"}, |
| 986 | isFound: true, |
| 987 | data: `15°C`, |
| 988 | }, |
| 989 | { |
| 990 | desc: `Translate supplementary Unicode symbols`, |
| 991 | json: `{"c": "\uD83D\uDE03"}`, // Smiley face (UTF16 surrogate pair) |
| 992 | path: []string{"c"}, |
| 993 | isFound: true, |
| 994 | data: "\U0001F603", // Smiley face |
| 995 | }, |
| 996 | { |
| 997 | desc: `Translate escape symbols`, |
| 998 | json: `{"c": "\\\""}`, |
| 999 | path: []string{"c"}, |
| 1000 | isFound: true, |
| 1001 | data: `\"`, |
| 1002 | }, |
| 1003 | { |
| 1004 | desc: `key and value with whitespace escapes`, |
| 1005 | json: `{"key\b\f\n\r\tkey":"value\b\f\n\r\tvalue"}`, |
| 1006 | path: []string{"key\b\f\n\r\tkey"}, |
| 1007 | isFound: true, |
| 1008 | data: "value\b\f\n\r\tvalue", // value is unescaped since this is GetString() |
| 1009 | }, |
| 1010 | { // This test checks we avoid an infinite loop for certain malformed JSON. We don't check for all malformed JSON as it would reduce performance. |
| 1011 | desc: `malformed with double quotes`, |
| 1012 | json: `{"a"":1}`, |
| 1013 | path: []string{"a"}, |
| 1014 | isFound: false, |
| 1015 | data: ``, |
| 1016 | }, |
| 1017 | { // More malformed JSON testing, to be sure we avoid an infinite loop. |
| 1018 | desc: `malformed with double quotes, and path does not exist`, |
| 1019 | json: `{"z":123,"y":{"x":7,"w":0},"v":{"u":"t","s":"r","q":0,"p":1558051800},"a":"b","c":"2016-11-02T20:10:11Z","d":"e","f":"g","h":{"i":"j""},"k":{"l":"m"}}`, |
| 1020 | path: []string{"o"}, |
| 1021 | isFound: false, |
| 1022 | data: ``, |
| 1023 | }, |
| 1024 | { |
| 1025 | desc: `read non-string as string`, |
| 1026 | json: `{"c": true}`, |
| 1027 | path: []string{"c"}, |
| 1028 | isErr: true, |
| 1029 | }, |
| 1030 | { |
| 1031 | desc: `empty array index`, |
| 1032 | json: `[""]`, |
| 1033 | path: []string{"[]"}, |
| 1034 | isFound: false, |
| 1035 | }, |
| 1036 | { |
| 1037 | desc: `malformed array index`, |
| 1038 | json: `[""]`, |
| 1039 | path: []string{"["}, |
| 1040 | isFound: false, |
| 1041 | }, |
| 1042 | { |
| 1043 | desc: `null test`, |
| 1044 | json: `{"c": null}`, |
| 1045 | path: []string{"c"}, |
| 1046 | isErr: true, |
| 1047 | }, |
| 1048 | } |
| 1049 | |
| 1050 | var getUnsafeStringTests = []GetTest{ |
| 1051 | { |
| 1052 | desc: `read empty string as unsafe string`, |
| 1053 | json: `{"c": ""}`, |
| 1054 | path: []string{"c"}, |
| 1055 | isFound: true, |
| 1056 | data: ``, |
| 1057 | }, |
| 1058 | { |
| 1059 | desc: `Do not translate Unicode symbols`, |
| 1060 | json: `{"c": "test"}`, |
| 1061 | path: []string{"c"}, |
| 1062 | isFound: true, |
| 1063 | data: `test`, |
| 1064 | }, |
| 1065 | { |
| 1066 | desc: `Do not translate Unicode symbols`, |
| 1067 | json: `{"c": "15\u00b0C"}`, |
| 1068 | path: []string{"c"}, |
| 1069 | isFound: true, |
| 1070 | data: `15\u00b0C`, |
| 1071 | }, |
| 1072 | { |
| 1073 | desc: `Do not translate supplementary Unicode symbols`, |
| 1074 | json: `{"c": "\uD83D\uDE03"}`, // Smiley face (UTF16 surrogate pair) |
| 1075 | path: []string{"c"}, |
| 1076 | isFound: true, |
| 1077 | data: `\uD83D\uDE03`, // Smiley face |
| 1078 | }, |
| 1079 | { |
| 1080 | desc: `Do not translate escape symbols`, |
| 1081 | json: `{"c": "\\\""}`, |
| 1082 | path: []string{"c"}, |
| 1083 | isFound: true, |
| 1084 | data: `\\\"`, |
| 1085 | }, |
| 1086 | { |
| 1087 | desc: `read boolean token as unsafe string`, |
| 1088 | json: `{"c": true}`, |
| 1089 | path: []string{"c"}, |
| 1090 | isFound: true, |
| 1091 | data: `true`, |
| 1092 | }, |
| 1093 | { |
| 1094 | desc: `missing key returns not found for unsafe string`, |
| 1095 | json: `{"c": "test"}`, |
| 1096 | path: []string{"missing"}, |
| 1097 | isFound: false, |
| 1098 | }, |
| 1099 | } |
| 1100 | |
| 1101 | var getBoolTests = []GetTest{ |
| 1102 | { |
| 1103 | desc: `read boolean true as boolean`, |
| 1104 | json: `{"a": "b", "c": true}`, |
| 1105 | path: []string{"c"}, |
| 1106 | isFound: true, |
| 1107 | data: true, |
| 1108 | }, |
| 1109 | { |
| 1110 | desc: `boolean true in formatted JSON`, |
| 1111 | json: "{\"a\": \"b\", \"c\": true \n}", |
| 1112 | path: []string{"c"}, |
| 1113 | isFound: true, |
| 1114 | data: true, |
| 1115 | }, |
| 1116 | { |
| 1117 | desc: `read boolean false as boolean`, |
| 1118 | json: `{"a": "b", "c": false}`, |
| 1119 | path: []string{"c"}, |
| 1120 | isFound: true, |
| 1121 | data: false, |
| 1122 | }, |
| 1123 | { |
| 1124 | desc: `boolean true in formatted JSON`, |
| 1125 | json: "{\"a\": \"b\", \"c\": false \n}", |
| 1126 | path: []string{"c"}, |
| 1127 | isFound: true, |
| 1128 | data: false, |
| 1129 | }, |
| 1130 | { |
| 1131 | desc: `read fake boolean true`, |
| 1132 | json: `{"a": txyz}`, |
| 1133 | path: []string{"a"}, |
| 1134 | isErr: true, |
| 1135 | }, |
| 1136 | { |
| 1137 | desc: `read fake boolean false`, |
| 1138 | json: `{"a": fwxyz}`, |
| 1139 | path: []string{"a"}, |
| 1140 | isErr: true, |
| 1141 | }, |
| 1142 | { |
| 1143 | desc: `read boolean true with whitespace and another key`, |
| 1144 | json: "{\r\t\n \"a\"\r\t\n :\r\t\n true\r\t\n ,\r\t\n \"b\": 1}", |
| 1145 | path: []string{"a"}, |
| 1146 | isFound: true, |
| 1147 | data: true, |
| 1148 | }, |
| 1149 | { |
| 1150 | desc: `null test`, |
| 1151 | json: `{"a": "b", "c": null}`, |
| 1152 | path: []string{"c"}, |
| 1153 | isFound: false, |
| 1154 | isErr: true, |
| 1155 | }, |
| 1156 | } |
| 1157 | |
| 1158 | var getArrayTests = []GetTest{ |
| 1159 | { |
| 1160 | desc: `read array of simple values`, |
| 1161 | json: `{"a": { "b":[1,2,3,4]}}`, |
| 1162 | path: []string{"a", "b"}, |
| 1163 | isFound: true, |
| 1164 | data: []string{`1`, `2`, `3`, `4`}, |
| 1165 | }, |
| 1166 | { |
| 1167 | desc: `read array via empty path`, |
| 1168 | json: `[1,2,3,4]`, |
| 1169 | path: []string{}, |
| 1170 | isFound: true, |
| 1171 | data: []string{`1`, `2`, `3`, `4`}, |
| 1172 | }, |
| 1173 | { |
| 1174 | desc: `read array of objects`, |
| 1175 | json: `{"a": { "b":[{"x":1},{"x":2},{"x":3},{"x":4}]}}`, |
| 1176 | path: []string{"a", "b"}, |
| 1177 | isFound: true, |
| 1178 | data: []string{`{"x":1}`, `{"x":2}`, `{"x":3}`, `{"x":4}`}, |
| 1179 | }, |
| 1180 | { |
| 1181 | desc: `read nested array`, |
| 1182 | json: `{"a": [[[1]],[[2]]]}`, |
| 1183 | path: []string{"a"}, |
| 1184 | isFound: true, |
| 1185 | data: []string{`[[1]]`, `[[2]]`}, |
| 1186 | }, |
| 1187 | } |
| 1188 | |
| 1189 | // checkFoundAndNoError checks the dataType and error return from Get*() against the test case expectations. |
| 1190 | // Returns true the test should proceed to checking the actual data returned from Get*(), or false if the test is finished. |
| 1191 | // Test helper for SYS-REQ-001, SYS-REQ-002, SYS-REQ-003, SYS-REQ-004, SYS-REQ-005, and SYS-REQ-011. |
| 1192 | func getTestCheckFoundAndNoError(t *testing.T, testKind string, test GetTest, jtype ValueType, value interface{}, err error) bool { |
| 1193 | isFound := (err != KeyPathNotFoundError) |
| 1194 | isErr := (err != nil && err != KeyPathNotFoundError) |
| 1195 | |
| 1196 | if test.isErr != isErr { |
| 1197 | // If the call didn't match the error expectation, fail |
| 1198 | t.Errorf("%s test '%s' isErr mismatch: expected %t, obtained %t (err %v). Value: %v", testKind, test.desc, test.isErr, isErr, err, value) |
| 1199 | return false |
| 1200 | } else if isErr { |
| 1201 | // Else, if there was an error, don't fail and don't check isFound or the value |
| 1202 | return false |
| 1203 | } else if test.isFound != isFound { |
| 1204 | // Else, if the call didn't match the is-found expectation, fail |
| 1205 | t.Errorf("%s test '%s' isFound mismatch: expected %t, obtained %t", testKind, test.desc, test.isFound, isFound) |
| 1206 | return false |
| 1207 | } else if !isFound { |
| 1208 | // Else, if no value was found, don't fail and don't check the value |
| 1209 | return false |
| 1210 | } else { |
| 1211 | // Else, there was no error and a value was found, so check the value |
| 1212 | return true |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | // Test helper for SYS-REQ-001, SYS-REQ-002, SYS-REQ-003, SYS-REQ-004, SYS-REQ-005, and SYS-REQ-011. |
| 1217 | func runGetTests(t *testing.T, testKind string, tests []GetTest, runner func(GetTest) (interface{}, ValueType, error), resultChecker func(GetTest, interface{}) (bool, interface{})) { |
| 1218 | for _, test := range tests { |
| 1219 | if activeTest != "" && test.desc != activeTest { |
| 1220 | continue |
| 1221 | } |
| 1222 | |
| 1223 | fmt.Println("Running:", test.desc) |
| 1224 | |
| 1225 | value, dataType, err := runner(test) |
| 1226 | |
| 1227 | if getTestCheckFoundAndNoError(t, testKind, test, dataType, value, err) { |
| 1228 | if test.data == nil { |
| 1229 | t.Errorf("MALFORMED TEST: %v", test) |
| 1230 | continue |
| 1231 | } |
| 1232 | |
| 1233 | if ok, expected := resultChecker(test, value); !ok { |
| 1234 | if expectedBytes, ok := expected.([]byte); ok { |
| 1235 | expected = string(expectedBytes) |
| 1236 | } |
| 1237 | if valueBytes, ok := value.([]byte); ok { |
| 1238 | value = string(valueBytes) |
| 1239 | } |
| 1240 | t.Errorf("%s test '%s' expected to return value %v, but did returned %v instead", testKind, test.desc, expected, value) |
| 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | // Test helper for SYS-REQ-009. |
| 1247 | func setTestCheckFoundAndNoError(t *testing.T, testKind string, test SetTest, value interface{}, err error) bool { |
| 1248 | isFound := (err != KeyPathNotFoundError) |
| 1249 | isErr := (err != nil && err != KeyPathNotFoundError) |
| 1250 | |
| 1251 | if test.isErr != isErr { |
| 1252 | // If the call didn't match the error expectation, fail |
| 1253 | t.Errorf("%s test '%s' isErr mismatch: expected %t, obtained %t (err %v). Value: %v", testKind, test.desc, test.isErr, isErr, err, value) |
| 1254 | return false |
| 1255 | } else if isErr { |
| 1256 | // Else, if there was an error, don't fail and don't check isFound or the value |
| 1257 | return false |
| 1258 | } else if test.isFound != isFound { |
| 1259 | // Else, if the call didn't match the is-found expectation, fail |
| 1260 | t.Errorf("%s test '%s' isFound mismatch: expected %t, obtained %t", testKind, test.desc, test.isFound, isFound) |
| 1261 | return false |
| 1262 | } else if !isFound { |
| 1263 | // Else, if no value was found, don't fail and don't check the value |
| 1264 | return false |
| 1265 | } else { |
| 1266 | // Else, there was no error and a value was found, so check the value |
| 1267 | return true |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | // Test helper for SYS-REQ-009. |
| 1272 | func runSetTests(t *testing.T, testKind string, tests []SetTest, runner func(SetTest) (interface{}, ValueType, error), resultChecker func(SetTest, interface{}) (bool, interface{})) { |
| 1273 | for _, test := range tests { |
| 1274 | if activeTest != "" && test.desc != activeTest { |
| 1275 | continue |
| 1276 | } |
| 1277 | |
| 1278 | fmt.Println("Running:", test.desc) |
| 1279 | |
| 1280 | value, _, err := runner(test) |
| 1281 | |
| 1282 | if setTestCheckFoundAndNoError(t, testKind, test, value, err) { |
| 1283 | if test.data == nil { |
| 1284 | t.Errorf("MALFORMED TEST: %v", test) |
| 1285 | continue |
| 1286 | } |
| 1287 | |
| 1288 | if string(value.([]byte)) != test.data { |
| 1289 | t.Errorf("Unexpected result on %s test '%s'", testKind, test.desc) |
| 1290 | t.Log("Got: ", string(value.([]byte))) |
| 1291 | t.Log("Expected:", test.data) |
| 1292 | t.Log("Error: ", err) |
| 1293 | } |
| 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | // Test helper for SYS-REQ-010. |
| 1299 | func runDeleteTests(t *testing.T, testKind string, tests []DeleteTest, runner func(DeleteTest) (interface{}, []byte), resultChecker func(DeleteTest, interface{}) (bool, interface{})) { |
| 1300 | for _, test := range tests { |
| 1301 | if activeTest != "" && test.desc != activeTest { |
| 1302 | continue |
| 1303 | } |
| 1304 | |
| 1305 | original := make([]byte, len(test.json)) |
| 1306 | copy(original, test.json) |
| 1307 | |
| 1308 | fmt.Println("Running:", test.desc) |
| 1309 | |
| 1310 | value, bytes := runner(test) |
| 1311 | |
| 1312 | if string(original) != string(bytes) { |
| 1313 | t.Errorf("ORIGINAL DATA MALFORMED: %v, %v", string(original), string(bytes)) |
| 1314 | continue |
| 1315 | } |
| 1316 | |
| 1317 | if test.data == nil { |
| 1318 | t.Errorf("MALFORMED TEST: %v", test) |
| 1319 | continue |
| 1320 | } |
| 1321 | |
| 1322 | if ok, expected := resultChecker(test, value); !ok { |
| 1323 | if expectedBytes, ok := expected.([]byte); ok { |
| 1324 | expected = string(expectedBytes) |
| 1325 | } |
| 1326 | if valueBytes, ok := value.([]byte); ok { |
| 1327 | value = string(valueBytes) |
| 1328 | } |
| 1329 | t.Errorf("%s test '%s' expected to return value %v, but did returned %v instead", testKind, test.desc, expected, value) |
| 1330 | } |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | // Verifies: SYS-REQ-010 [example] |
| 1335 | // MCDC SYS-REQ-010: delete_path_is_provided=F, delete_returns_empty_document_without_path=T => TRUE |
| 1336 | // Verifies: SYS-REQ-033 [example] |
| 1337 | // MCDC SYS-REQ-033: delete_path_is_provided=T, delete_target_exists=T, delete_returns_document_without_target=T => TRUE |
| 1338 | // Verifies: SYS-REQ-034 [example] |
| 1339 | // MCDC SYS-REQ-034: delete_path_is_provided=T, delete_target_exists=F, delete_input_is_unusable_for_requested_path=F, delete_preserves_input_when_target_missing=T => TRUE |
| 1340 | func TestDelete(t *testing.T) { |
| 1341 | runDeleteTests(t, "Delete()", deleteTests, |
| 1342 | func(test DeleteTest) (interface{}, []byte) { |
| 1343 | ba := []byte(test.json) |
| 1344 | return Delete(ba, test.path...), ba |
| 1345 | }, |
| 1346 | func(test DeleteTest, value interface{}) (bool, interface{}) { |
| 1347 | expected := []byte(test.data.(string)) |
| 1348 | return bytes.Equal(expected, value.([]byte)), expected |
| 1349 | }, |
| 1350 | ) |
| 1351 | } |
| 1352 | |
| 1353 | // Verifies: SYS-REQ-001 [example] |
| 1354 | // MCDC SYS-REQ-001: addressed_path_exists=F, json_input_is_well_formed=T, key_path_is_provided=T, returns_existing_path_lookup_result=F => TRUE |
| 1355 | // MCDC SYS-REQ-001: addressed_path_exists=T, json_input_is_well_formed=F, key_path_is_provided=T, returns_existing_path_lookup_result=F => TRUE |
| 1356 | // MCDC SYS-REQ-001: addressed_path_exists=T, json_input_is_well_formed=T, key_path_is_provided=F, returns_existing_path_lookup_result=F => TRUE |
| 1357 | // MCDC SYS-REQ-001: addressed_path_exists=T, json_input_is_well_formed=T, key_path_is_provided=T, returns_existing_path_lookup_result=F => FALSE |
| 1358 | // MCDC SYS-REQ-001: addressed_path_exists=T, json_input_is_well_formed=T, key_path_is_provided=T, returns_existing_path_lookup_result=T => TRUE |
| 1359 | func TestGet(t *testing.T) { |
| 1360 | runGetTests(t, "Get()", getTests, |
| 1361 | func(test GetTest) (value interface{}, dataType ValueType, err error) { |
| 1362 | value, dataType, _, err = Get([]byte(test.json), test.path...) |
| 1363 | return |
| 1364 | }, |
| 1365 | func(test GetTest, value interface{}) (bool, interface{}) { |
| 1366 | expected := []byte(test.data.(string)) |
| 1367 | return bytes.Equal(expected, value.([]byte)), expected |
| 1368 | }, |
| 1369 | ) |
| 1370 | } |
| 1371 | |
| 1372 | // Verifies: SYS-REQ-016 [boundary] |
| 1373 | // MCDC SYS-REQ-016: N/A |
| 1374 | // Verifies: SYS-REQ-017 [boundary] |
| 1375 | // MCDC SYS-REQ-017: N/A |
| 1376 | // Verifies: SYS-REQ-018 [boundary] |
| 1377 | // MCDC SYS-REQ-018: N/A |
| 1378 | // Verifies: SYS-REQ-019 [boundary] |
| 1379 | // MCDC SYS-REQ-019: N/A |
| 1380 | // Verifies: SYS-REQ-020 [boundary] |
| 1381 | // MCDC SYS-REQ-020: N/A |
| 1382 | // Verifies: SYS-REQ-021 [boundary] |
| 1383 | // MCDC SYS-REQ-021: N/A |
| 1384 | // Verifies: SYS-REQ-022 [boundary] |
| 1385 | // MCDC SYS-REQ-022: N/A |
| 1386 | // Verifies: SYS-REQ-023 [boundary] |
| 1387 | // MCDC SYS-REQ-023: N/A |
| 1388 | // Verifies: SYS-REQ-024 [boundary] |
| 1389 | // MCDC SYS-REQ-024: N/A |
| 1390 | // Verifies: SYS-REQ-025 [boundary] |
| 1391 | // MCDC SYS-REQ-025: N/A |
| 1392 | // Verifies: SYS-REQ-026 [boundary] |
| 1393 | // MCDC SYS-REQ-026: N/A |
| 1394 | // Verifies: SYS-REQ-027 [boundary] |
| 1395 | // MCDC SYS-REQ-027: N/A |
| 1396 | func TestGetRequirementSlices(t *testing.T) { |
| 1397 | t.Run("well formed missing path returns not found", func(t *testing.T) { |
| 1398 | value, dataType, offset, err := Get([]byte(`{"a":"b"}`), "missing") |
| 1399 | if !errors.Is(err, KeyPathNotFoundError) { |
| 1400 | t.Fatalf("expected KeyPathNotFoundError, got %v", err) |
| 1401 | } |
| 1402 | if dataType != NotExist || offset != -1 || value != nil { |
| 1403 | t.Fatalf("expected not-found tuple, got value=%v type=%v offset=%d", value, dataType, offset) |
| 1404 | } |
| 1405 | }) |
| 1406 | |
| 1407 | t.Run("incomplete input returns parse error", func(t *testing.T) { |
| 1408 | if _, _, _, err := Get([]byte(`{"a":`), "a"); err == nil { |
| 1409 | t.Fatal("expected parse-related error for incomplete input") |
| 1410 | } |
| 1411 | }) |
| 1412 | |
| 1413 | t.Run("no key path returns closest root value", func(t *testing.T) { |
| 1414 | value, dataType, _, err := Get([]byte(`{"a":1}`)) |
| 1415 | if err != nil { |
| 1416 | t.Fatalf("Get without key path returned error: %v", err) |
| 1417 | } |
| 1418 | if dataType != Object || string(value) != `{"a":1}` { |
| 1419 | t.Fatalf("unexpected root value result: value=%s type=%v", string(value), dataType) |
| 1420 | } |
| 1421 | }) |
| 1422 | |
| 1423 | t.Run("empty input with key path returns not found", func(t *testing.T) { |
| 1424 | _, dataType, offset, err := Get([]byte(""), "a") |
| 1425 | if !errors.Is(err, KeyPathNotFoundError) { |
| 1426 | t.Fatalf("expected KeyPathNotFoundError, got %v", err) |
| 1427 | } |
| 1428 | if dataType != NotExist || offset != -1 { |
| 1429 | t.Fatalf("expected empty-input not-found tuple, got type=%v offset=%d", dataType, offset) |
| 1430 | } |
| 1431 | }) |
| 1432 | |
| 1433 | t.Run("object key lookup respects current scope", func(t *testing.T) { |
| 1434 | value, dataType, _, err := Get([]byte(`{"a":{"b":1},"b":2}`), "a", "b") |
| 1435 | if err != nil { |
| 1436 | t.Fatalf("nested object lookup returned error: %v", err) |
| 1437 | } |
| 1438 | if dataType != Number || string(value) != "1" { |
| 1439 | t.Fatalf("unexpected nested object lookup result: value=%s type=%v", string(value), dataType) |
| 1440 | } |
| 1441 | }) |
| 1442 | |
| 1443 | t.Run("array index lookup returns in-bounds element", func(t *testing.T) { |
| 1444 | value, dataType, _, err := Get([]byte(`{"a":[{"b":1},"foo",3]}`), "a", "[1]") |
| 1445 | if err != nil { |
| 1446 | t.Fatalf("array index lookup returned error: %v", err) |
| 1447 | } |
| 1448 | if dataType != String || string(value) != "foo" { |
| 1449 | t.Fatalf("unexpected array lookup result: value=%s type=%v", string(value), dataType) |
| 1450 | } |
| 1451 | }) |
| 1452 | |
| 1453 | t.Run("invalid array index syntax returns not found", func(t *testing.T) { |
| 1454 | if _, _, _, err := Get([]byte(`{"a":[1,2]}`), "a", "["); !errors.Is(err, KeyPathNotFoundError) { |
| 1455 | t.Fatalf("expected KeyPathNotFoundError for malformed array index, got %v", err) |
| 1456 | } |
| 1457 | }) |
| 1458 | |
| 1459 | t.Run("out of bounds array index returns not found", func(t *testing.T) { |
| 1460 | if _, _, _, err := Get([]byte(`{"a":[1,2]}`), "a", "[9]"); !errors.Is(err, KeyPathNotFoundError) { |
| 1461 | t.Fatalf("expected KeyPathNotFoundError for out-of-bounds array index, got %v", err) |
| 1462 | } |
| 1463 | }) |
| 1464 | |
| 1465 | t.Run("escaped keys are matched after decoding", func(t *testing.T) { |
| 1466 | value, dataType, _, err := Get([]byte(`{"a\u00B0b":1}`), "a°b") |
| 1467 | if err != nil { |
| 1468 | t.Fatalf("escaped-key lookup returned error: %v", err) |
| 1469 | } |
| 1470 | if dataType != Number || string(value) != "1" { |
| 1471 | t.Fatalf("unexpected escaped-key lookup result: value=%s type=%v", string(value), dataType) |
| 1472 | } |
| 1473 | }) |
| 1474 | |
| 1475 | t.Run("string results are unquoted but not unescaped", func(t *testing.T) { |
| 1476 | value, dataType, _, err := Get([]byte(`{"a":"line\nbreak"}`), "a") |
| 1477 | if err != nil { |
| 1478 | t.Fatalf("string lookup returned error: %v", err) |
| 1479 | } |
| 1480 | if dataType != String || string(value) != `line\nbreak` { |
| 1481 | t.Fatalf("unexpected raw string result: value=%s type=%v", string(value), dataType) |
| 1482 | } |
| 1483 | }) |
| 1484 | |
| 1485 | t.Run("best effort lookup succeeds when malformed data is outside addressed token", func(t *testing.T) { |
| 1486 | value, dataType, _, err := Get([]byte(`{"a":1]`), "a") |
| 1487 | if err != nil { |
| 1488 | t.Fatalf("best-effort lookup returned error: %v", err) |
| 1489 | } |
| 1490 | if dataType != Number || string(value) != "1" { |
| 1491 | t.Fatalf("unexpected best-effort lookup result: value=%s type=%v", string(value), dataType) |
| 1492 | } |
| 1493 | }) |
| 1494 | |
| 1495 | t.Run("invalid addressed token shape returns value type error", func(t *testing.T) { |
| 1496 | if _, _, _, err := Get([]byte(`{"a":u}`), "a"); !errors.Is(err, UnknownValueTypeError) { |
| 1497 | t.Fatalf("expected UnknownValueTypeError, got %v", err) |
| 1498 | } |
| 1499 | }) |
| 1500 | } |
| 1501 | |
| 1502 | // Verifies: SYS-REQ-002 [example] |
| 1503 | // MCDC SYS-REQ-002: addressed_value_is_string=F, raw_string_token_is_well_formed=T, returns_getstring_decoded_value=F => TRUE |
| 1504 | // MCDC SYS-REQ-002: addressed_value_is_string=T, raw_string_token_is_well_formed=F, returns_getstring_decoded_value=F => TRUE |
| 1505 | // MCDC SYS-REQ-002: addressed_value_is_string=T, raw_string_token_is_well_formed=T, returns_getstring_decoded_value=F => FALSE |
| 1506 | // MCDC SYS-REQ-002: addressed_value_is_string=T, raw_string_token_is_well_formed=T, returns_getstring_decoded_value=T => TRUE |
| 1507 | func TestGetString(t *testing.T) { |
| 1508 | runGetTests(t, "GetString()", getStringTests, |
| 1509 | func(test GetTest) (value interface{}, dataType ValueType, err error) { |
| 1510 | value, err = GetString([]byte(test.json), test.path...) |
| 1511 | return value, String, err |
| 1512 | }, |
| 1513 | func(test GetTest, value interface{}) (bool, interface{}) { |
| 1514 | expected := test.data.(string) |
| 1515 | return expected == value.(string), expected |
| 1516 | }, |
| 1517 | ) |
| 1518 | } |
| 1519 | |
| 1520 | // Verifies: SYS-REQ-011 [example] |
| 1521 | // MCDC SYS-REQ-011: addressed_value_is_string=F, returns_unsafe_string_view=F => TRUE |
| 1522 | // MCDC SYS-REQ-011: addressed_value_is_string=T, returns_unsafe_string_view=F => FALSE |
| 1523 | // MCDC SYS-REQ-011: addressed_value_is_string=T, returns_unsafe_string_view=T => TRUE |
| 1524 | func TestGetUnsafeString(t *testing.T) { |
| 1525 | runGetTests(t, "GetUnsafeString()", getUnsafeStringTests, |
| 1526 | func(test GetTest) (value interface{}, dataType ValueType, err error) { |
| 1527 | value, err = GetUnsafeString([]byte(test.json), test.path...) |
| 1528 | return value, String, err |
| 1529 | }, |
| 1530 | func(test GetTest, value interface{}) (bool, interface{}) { |
| 1531 | expected := test.data.(string) |
| 1532 | return expected == value.(string), expected |
| 1533 | }, |
| 1534 | ) |
| 1535 | } |
| 1536 | |
| 1537 | // Verifies: SYS-REQ-003 [example] |
| 1538 | // MCDC SYS-REQ-003: addressed_value_is_number=F, raw_number_token_is_integer_parseable=T, returns_getint_value=F => TRUE |
| 1539 | // MCDC SYS-REQ-003: addressed_value_is_number=T, raw_number_token_is_integer_parseable=F, returns_getint_value=F => TRUE |
| 1540 | // MCDC SYS-REQ-003: addressed_value_is_number=T, raw_number_token_is_integer_parseable=T, returns_getint_value=F => FALSE |
| 1541 | // MCDC SYS-REQ-003: addressed_value_is_number=T, raw_number_token_is_integer_parseable=T, returns_getint_value=T => TRUE |
| 1542 | func TestGetInt(t *testing.T) { |
| 1543 | runGetTests(t, "GetInt()", getIntTests, |
| 1544 | func(test GetTest) (value interface{}, dataType ValueType, err error) { |
| 1545 | value, err = GetInt([]byte(test.json), test.path...) |
| 1546 | return value, Number, err |
| 1547 | }, |
| 1548 | func(test GetTest, value interface{}) (bool, interface{}) { |
| 1549 | expected := test.data.(int64) |
| 1550 | return expected == value.(int64), expected |
| 1551 | }, |
| 1552 | ) |
| 1553 | } |
| 1554 | |
| 1555 | // Verifies: SYS-REQ-004 [example] |
| 1556 | // MCDC SYS-REQ-004: addressed_value_is_number=F, raw_number_token_is_float_parseable=T, returns_getfloat_value=F => TRUE |
| 1557 | // MCDC SYS-REQ-004: addressed_value_is_number=T, raw_number_token_is_float_parseable=F, returns_getfloat_value=F => TRUE |
| 1558 | // MCDC SYS-REQ-004: addressed_value_is_number=T, raw_number_token_is_float_parseable=T, returns_getfloat_value=F => FALSE |
| 1559 | // MCDC SYS-REQ-004: addressed_value_is_number=T, raw_number_token_is_float_parseable=T, returns_getfloat_value=T => TRUE |
| 1560 | func TestGetFloat(t *testing.T) { |
| 1561 | runGetTests(t, "GetFloat()", getFloatTests, |
| 1562 | func(test GetTest) (value interface{}, dataType ValueType, err error) { |
| 1563 | value, err = GetFloat([]byte(test.json), test.path...) |
| 1564 | return value, Number, err |
| 1565 | }, |
| 1566 | func(test GetTest, value interface{}) (bool, interface{}) { |
| 1567 | expected := test.data.(float64) |
| 1568 | return expected == value.(float64), expected |
| 1569 | }, |
| 1570 | ) |
| 1571 | } |
| 1572 | |
| 1573 | // Verifies: SYS-REQ-005 [example] |
| 1574 | // MCDC SYS-REQ-005: addressed_value_is_boolean=F, raw_boolean_token_is_well_formed=T, returns_getboolean_value=F => TRUE |
| 1575 | // MCDC SYS-REQ-005: addressed_value_is_boolean=T, raw_boolean_token_is_well_formed=F, returns_getboolean_value=F => TRUE |
| 1576 | // MCDC SYS-REQ-005: addressed_value_is_boolean=T, raw_boolean_token_is_well_formed=T, returns_getboolean_value=F => FALSE |
| 1577 | // MCDC SYS-REQ-005: addressed_value_is_boolean=T, raw_boolean_token_is_well_formed=T, returns_getboolean_value=T => TRUE |
| 1578 | func TestGetBoolean(t *testing.T) { |
| 1579 | runGetTests(t, "GetBoolean()", getBoolTests, |
| 1580 | func(test GetTest) (value interface{}, dataType ValueType, err error) { |
| 1581 | value, err = GetBoolean([]byte(test.json), test.path...) |
| 1582 | return value, Boolean, err |
| 1583 | }, |
| 1584 | func(test GetTest, value interface{}) (bool, interface{}) { |
| 1585 | expected := test.data.(bool) |
| 1586 | return expected == value.(bool), expected |
| 1587 | }, |
| 1588 | ) |
| 1589 | } |
| 1590 | |
| 1591 | // Verifies: SYS-REQ-001 [example] |
| 1592 | // MCDC SYS-REQ-001: N/A |
| 1593 | func TestGetSlice(t *testing.T) { |
| 1594 | runGetTests(t, "Get()-for-arrays", getArrayTests, |
| 1595 | func(test GetTest) (value interface{}, dataType ValueType, err error) { |
| 1596 | value, dataType, _, err = Get([]byte(test.json), test.path...) |
| 1597 | return |
| 1598 | }, |
| 1599 | func(test GetTest, value interface{}) (bool, interface{}) { |
| 1600 | expected := test.data.([]string) |
| 1601 | return reflect.DeepEqual(expected, toStringArray(value.([]byte))), expected |
| 1602 | }, |
| 1603 | ) |
| 1604 | } |
| 1605 | |
| 1606 | // Verifies: SYS-REQ-006 [example] |
| 1607 | // MCDC SYS-REQ-006: addressed_array_is_empty=F, addressed_array_is_well_formed=T, array_callback_receives_elements_in_order=F => FALSE |
| 1608 | // MCDC SYS-REQ-006: addressed_array_is_empty=F, addressed_array_is_well_formed=T, array_callback_receives_elements_in_order=T => TRUE |
| 1609 | func TestArrayEach(t *testing.T) { |
| 1610 | mock := []byte(`{"a": { "b":[{"x": 1} ,{"x":2},{ "x":3}, {"x":4} ]}}`) |
| 1611 | count := 0 |
| 1612 | |
| 1613 | ArrayEach(mock, func(value []byte, dataType ValueType, offset int, err error) { |
| 1614 | count++ |
| 1615 | |
| 1616 | switch count { |
| 1617 | case 1: |
| 1618 | if string(value) != `{"x": 1}` { |
| 1619 | t.Errorf("Wrong first item: %s", string(value)) |
| 1620 | } |
| 1621 | case 2: |
| 1622 | if string(value) != `{"x":2}` { |
| 1623 | t.Errorf("Wrong second item: %s", string(value)) |
| 1624 | } |
| 1625 | case 3: |
| 1626 | if string(value) != `{ "x":3}` { |
| 1627 | t.Errorf("Wrong third item: %s", string(value)) |
| 1628 | } |
| 1629 | case 4: |
| 1630 | if string(value) != `{"x":4}` { |
| 1631 | t.Errorf("Wrong forth item: %s", string(value)) |
| 1632 | } |
| 1633 | default: |
| 1634 | t.Errorf("Should process only 4 items") |
| 1635 | } |
| 1636 | }, "a", "b") |
| 1637 | } |
| 1638 | |
| 1639 | // Verifies: SYS-REQ-029 [boundary] |
| 1640 | // MCDC SYS-REQ-029: addressed_array_is_well_formed=F, malformed_array_input_returns_error=T => TRUE |
| 1641 | func TestArrayEachWithWhiteSpace(t *testing.T) { |
| 1642 | // Issue #159 |
| 1643 | count := 0 |
| 1644 | funcError := func([]byte, ValueType, int, error) { t.Errorf("Run func not allow") } |
| 1645 | funcSuccess := func(value []byte, dataType ValueType, index int, err error) { |
| 1646 | count++ |
| 1647 | |
| 1648 | switch count { |
| 1649 | case 1: |
| 1650 | if string(value) != `AAA` { |
| 1651 | t.Errorf("Wrong first item: %s", string(value)) |
| 1652 | } |
| 1653 | case 2: |
| 1654 | if string(value) != `BBB` { |
| 1655 | t.Errorf("Wrong second item: %s", string(value)) |
| 1656 | } |
| 1657 | case 3: |
| 1658 | if string(value) != `CCC` { |
| 1659 | t.Errorf("Wrong third item: %s", string(value)) |
| 1660 | } |
| 1661 | default: |
| 1662 | t.Errorf("Should process only 3 items") |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | type args struct { |
| 1667 | data []byte |
| 1668 | cb func(value []byte, dataType ValueType, offset int, err error) |
| 1669 | keys []string |
| 1670 | } |
| 1671 | tests := []struct { |
| 1672 | name string |
| 1673 | args args |
| 1674 | wantErr bool |
| 1675 | }{ |
| 1676 | {"Array with white space", args{[]byte(` ["AAA", "BBB", "CCC"]`), funcSuccess, []string{}}, false}, |
| 1677 | {"Array with only one character after white space", args{[]byte(` 1`), funcError, []string{}}, true}, |
| 1678 | {"Only white space", args{[]byte(` `), funcError, []string{}}, true}, |
| 1679 | } |
| 1680 | for _, tt := range tests { |
| 1681 | t.Run(tt.name, func(t *testing.T) { |
| 1682 | _, err := ArrayEach(tt.args.data, tt.args.cb, tt.args.keys...) |
| 1683 | if (err != nil) != tt.wantErr { |
| 1684 | t.Errorf("ArrayEach() error = %v, wantErr %v", err, tt.wantErr) |
| 1685 | return |
| 1686 | } |
| 1687 | }) |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | // Verifies: SYS-REQ-028 [boundary] |
| 1692 | // MCDC SYS-REQ-028: addressed_array_is_empty=T, addressed_array_is_well_formed=T, empty_array_produces_no_callbacks=T => TRUE |
| 1693 | func TestArrayEachEmpty(t *testing.T) { |
| 1694 | funcError := func([]byte, ValueType, int, error) { t.Errorf("Run func not allow") } |
| 1695 | |
| 1696 | type args struct { |
| 1697 | data []byte |
| 1698 | cb func(value []byte, dataType ValueType, offset int, err error) |
| 1699 | keys []string |
| 1700 | } |
| 1701 | tests := []struct { |
| 1702 | name string |
| 1703 | args args |
| 1704 | wantOffset int |
| 1705 | wantErr bool |
| 1706 | }{ |
| 1707 | {"Empty array", args{[]byte("[]"), funcError, []string{}}, 1, false}, |
| 1708 | {"Empty array with space", args{[]byte("[ ]"), funcError, []string{}}, 2, false}, |
| 1709 | {"Empty array with \n", args{[]byte("[\n]"), funcError, []string{}}, 2, false}, |
| 1710 | {"Empty field array", args{[]byte("{\"data\": []}"), funcError, []string{"data"}}, 10, false}, |
| 1711 | {"Empty field array with space", args{[]byte("{\"data\": [ ]}"), funcError, []string{"data"}}, 11, false}, |
| 1712 | {"Empty field array with \n", args{[]byte("{\"data\": [\n]}"), funcError, []string{"data"}}, 11, false}, |
| 1713 | } |
| 1714 | for _, tt := range tests { |
| 1715 | t.Run(tt.name, func(t *testing.T) { |
| 1716 | gotOffset, err := ArrayEach(tt.args.data, tt.args.cb, tt.args.keys...) |
| 1717 | if (err != nil) != tt.wantErr { |
| 1718 | t.Errorf("ArrayEach() error = %v, wantErr %v", err, tt.wantErr) |
| 1719 | return |
| 1720 | } |
| 1721 | if gotOffset != tt.wantOffset { |
| 1722 | t.Errorf("ArrayEach() = %v, want %v", gotOffset, tt.wantOffset) |
| 1723 | } |
| 1724 | }) |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | type keyValueEntry struct { |
| 1729 | key string |
| 1730 | value string |
| 1731 | valueType ValueType |
| 1732 | } |
| 1733 | |
| 1734 | // Test helper for SYS-REQ-007. |
| 1735 | func (kv keyValueEntry) String() string { |
| 1736 | return fmt.Sprintf("[%s: %s (%s)]", kv.key, kv.value, kv.valueType) |
| 1737 | } |
| 1738 | |
| 1739 | type ObjectEachTest struct { |
| 1740 | desc string |
| 1741 | json string |
| 1742 | |
| 1743 | isErr bool |
| 1744 | entries []keyValueEntry |
| 1745 | } |
| 1746 | |
| 1747 | var objectEachTests = []ObjectEachTest{ |
| 1748 | { |
| 1749 | desc: "empty object", |
| 1750 | json: `{}`, |
| 1751 | entries: []keyValueEntry{}, |
| 1752 | }, |
| 1753 | { |
| 1754 | desc: "single key-value object", |
| 1755 | json: `{"key": "value"}`, |
| 1756 | entries: []keyValueEntry{ |
| 1757 | {"key", "value", String}, |
| 1758 | }, |
| 1759 | }, |
| 1760 | { |
| 1761 | desc: "multiple key-value object with many value types", |
| 1762 | json: `{ |
| 1763 | "key1": null, |
| 1764 | "key2": true, |
| 1765 | "key3": 1.23, |
| 1766 | "key4": "string value", |
| 1767 | "key5": [1,2,3], |
| 1768 | "key6": {"a":"b"} |
| 1769 | }`, |
| 1770 | entries: []keyValueEntry{ |
| 1771 | {"key1", "null", Null}, |
| 1772 | {"key2", "true", Boolean}, |
| 1773 | {"key3", "1.23", Number}, |
| 1774 | {"key4", "string value", String}, |
| 1775 | {"key5", "[1,2,3]", Array}, |
| 1776 | {"key6", `{"a":"b"}`, Object}, |
| 1777 | }, |
| 1778 | }, |
| 1779 | { |
| 1780 | desc: "escaped key", |
| 1781 | json: `{"key\"\\\/\b\f\n\r\t\u00B0": "value"}`, |
| 1782 | entries: []keyValueEntry{ |
| 1783 | {"key\"\\/\b\f\n\r\t\u00B0", "value", String}, |
| 1784 | }, |
| 1785 | }, |
| 1786 | // Error cases |
| 1787 | { |
| 1788 | desc: "no object present", |
| 1789 | json: ` \t\n\r`, |
| 1790 | isErr: true, |
| 1791 | }, |
| 1792 | { |
| 1793 | desc: "unmatched braces 1", |
| 1794 | json: `{`, |
| 1795 | isErr: true, |
| 1796 | }, |
| 1797 | { |
| 1798 | desc: "unmatched braces 2", |
| 1799 | json: `}`, |
| 1800 | isErr: true, |
| 1801 | }, |
| 1802 | { |
| 1803 | desc: "unmatched braces 3", |
| 1804 | json: `}{}`, |
| 1805 | isErr: true, |
| 1806 | }, |
| 1807 | { |
| 1808 | desc: "bad key (number)", |
| 1809 | json: `{123: "value"}`, |
| 1810 | isErr: true, |
| 1811 | }, |
| 1812 | { |
| 1813 | desc: "bad key (unclosed quote)", |
| 1814 | json: `{"key: 123}`, |
| 1815 | isErr: true, |
| 1816 | }, |
| 1817 | { |
| 1818 | desc: "bad value (no value)", |
| 1819 | json: `{"key":}`, |
| 1820 | isErr: true, |
| 1821 | }, |
| 1822 | { |
| 1823 | desc: "bad value (bogus value)", |
| 1824 | json: `{"key": notavalue}`, |
| 1825 | isErr: true, |
| 1826 | }, |
| 1827 | { |
| 1828 | desc: "bad entry (missing colon)", |
| 1829 | json: `{"key" "value"}`, |
| 1830 | isErr: true, |
| 1831 | }, |
| 1832 | { |
| 1833 | desc: "bad entry (no trailing comma)", |
| 1834 | json: `{"key": "value" "key2": "value2"}`, |
| 1835 | isErr: true, |
| 1836 | }, |
| 1837 | { |
| 1838 | desc: "bad entry (two commas)", |
| 1839 | json: `{"key": "value",, "key2": "value2"}`, |
| 1840 | isErr: true, |
| 1841 | }, |
| 1842 | } |
| 1843 | |
| 1844 | // Verifies: SYS-REQ-031 [example] |
| 1845 | // MCDC SYS-REQ-031: addressed_object_is_well_formed=F, malformed_object_input_returns_error=T => TRUE |
| 1846 | // Verifies: SYS-REQ-030 [example] |
| 1847 | // MCDC SYS-REQ-030: addressed_object_is_empty=T, addressed_object_is_well_formed=T, empty_object_produces_no_entries=T => TRUE |
| 1848 | // Verifies: SYS-REQ-031 [example] |
| 1849 | // MCDC SYS-REQ-031: addressed_object_is_well_formed=F, malformed_object_input_returns_error=T => TRUE |
| 1850 | // Verifies: SYS-REQ-030 [example] |
| 1851 | // MCDC SYS-REQ-030: addressed_object_is_empty=T, addressed_object_is_well_formed=T, empty_object_produces_no_entries=T => TRUE |
| 1852 | // Verifies: SYS-REQ-007 [example] |
| 1853 | // MCDC SYS-REQ-007: addressed_object_is_empty=F, addressed_object_is_well_formed=T, object_callback_receives_entries=F => FALSE |
| 1854 | // MCDC SYS-REQ-007: addressed_object_is_empty=F, addressed_object_is_well_formed=T, object_callback_receives_entries=T => TRUE |
| 1855 | func TestObjectEach(t *testing.T) { |
| 1856 | for _, test := range objectEachTests { |
| 1857 | if activeTest != "" && test.desc != activeTest { |
| 1858 | continue |
| 1859 | } |
| 1860 | |
| 1861 | // Execute ObjectEach and capture all of the entries visited, in order |
| 1862 | var entries []keyValueEntry |
| 1863 | err := ObjectEach([]byte(test.json), func(key, value []byte, valueType ValueType, off int) error { |
| 1864 | entries = append(entries, keyValueEntry{ |
| 1865 | key: string(key), |
| 1866 | value: string(value), |
| 1867 | valueType: valueType, |
| 1868 | }) |
| 1869 | return nil |
| 1870 | }) |
| 1871 | |
| 1872 | // Check the correctness of the result |
| 1873 | isErr := (err != nil) |
| 1874 | if test.isErr != isErr { |
| 1875 | // If the call didn't match the error expectation, fail |
| 1876 | t.Errorf("ObjectEach test '%s' isErr mismatch: expected %t, obtained %t (err %v)", test.desc, test.isErr, isErr, err) |
| 1877 | } else if isErr { |
| 1878 | // Else, if there was an expected error, don't fail and don't check anything further |
| 1879 | } else if len(test.entries) != len(entries) { |
| 1880 | t.Errorf("ObjectEach test '%s' mismatch in number of key-value entries: expected %d, obtained %d (entries found: %s)", test.desc, len(test.entries), len(entries), entries) |
| 1881 | } else { |
| 1882 | for i, entry := range entries { |
| 1883 | expectedEntry := test.entries[i] |
| 1884 | if expectedEntry.key != entry.key { |
| 1885 | t.Errorf("ObjectEach test '%s' key mismatch at entry %d: expected %s, obtained %s", test.desc, i, expectedEntry.key, entry.key) |
| 1886 | break |
| 1887 | } else if expectedEntry.value != entry.value { |
| 1888 | t.Errorf("ObjectEach test '%s' value mismatch at entry %d: expected %s, obtained %s", test.desc, i, expectedEntry.value, entry.value) |
| 1889 | break |
| 1890 | } else if expectedEntry.valueType != entry.valueType { |
| 1891 | t.Errorf("ObjectEach test '%s' value type mismatch at entry %d: expected %s, obtained %s", test.desc, i, expectedEntry.valueType, entry.valueType) |
| 1892 | break |
| 1893 | } else { |
| 1894 | // Success for this entry |
| 1895 | } |
| 1896 | } |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | // Verifies: SYS-REQ-032 [boundary] |
| 1902 | // MCDC SYS-REQ-032: addressed_object_is_well_formed=T, object_callback_returns_error=T, object_callback_error_is_returned=T => TRUE |
| 1903 | func TestObjectEachNestedPathAndCallbackError(t *testing.T) { |
| 1904 | t.Run("nested object path", func(t *testing.T) { |
| 1905 | var entries []keyValueEntry |
| 1906 | err := ObjectEach([]byte(`{"outer":{"a":1,"b":true}}`), func(key, value []byte, valueType ValueType, off int) error { |
| 1907 | entries = append(entries, keyValueEntry{ |
| 1908 | key: string(key), |
| 1909 | value: string(value), |
| 1910 | valueType: valueType, |
| 1911 | }) |
| 1912 | return nil |
| 1913 | }, "outer") |
| 1914 | if err != nil { |
| 1915 | t.Fatalf("ObjectEach nested path returned error: %v", err) |
| 1916 | } |
| 1917 | expected := []keyValueEntry{ |
| 1918 | {key: "a", value: "1", valueType: Number}, |
| 1919 | {key: "b", value: "true", valueType: Boolean}, |
| 1920 | } |
| 1921 | if !reflect.DeepEqual(expected, entries) { |
| 1922 | t.Fatalf("ObjectEach nested path entries mismatch: expected %#v, got %#v", expected, entries) |
| 1923 | } |
| 1924 | }) |
| 1925 | |
| 1926 | t.Run("callback error is returned", func(t *testing.T) { |
| 1927 | sentinel := errors.New("stop iteration") |
| 1928 | err := ObjectEach([]byte(`{"a":1}`), func(key, value []byte, valueType ValueType, off int) error { |
| 1929 | return sentinel |
| 1930 | }) |
| 1931 | if !errors.Is(err, sentinel) { |
| 1932 | t.Fatalf("ObjectEach callback error mismatch: expected %v, got %v", sentinel, err) |
| 1933 | } |
| 1934 | }) |
| 1935 | } |
| 1936 | |
| 1937 | var testJson = []byte(`{ |
| 1938 | "name": "Name", |
| 1939 | "order": "Order", |
| 1940 | "sum": 100, |
| 1941 | "len": 12, |
| 1942 | "isPaid": true, |
| 1943 | "nested": {"a":"test", "b":2, "nested3":{"a":"test3","b":4}, "c": "unknown"}, |
| 1944 | "nested2": { |
| 1945 | "a":"test2", |
| 1946 | "b":3 |
| 1947 | }, |
| 1948 | "arr": [ |
| 1949 | { |
| 1950 | "a":"zxc", |
| 1951 | "b": 1 |
| 1952 | }, |
| 1953 | { |
| 1954 | "a":"123", |
| 1955 | "b":2 |
| 1956 | } |
| 1957 | ], |
| 1958 | "arrInt": [1,2,3,4], |
| 1959 | "intPtr": 10, |
| 1960 | "a\n":{ |
| 1961 | "b\n":99 |
| 1962 | } |
| 1963 | }`) |
| 1964 | |
| 1965 | // Verifies: SYS-REQ-008 [example] |
| 1966 | // MCDC SYS-REQ-008: eachkey_callback_receives_found_values=F, eachkey_completes_requested_scan=F, eachkey_malformed_input_returns_error=F, missing_multipath_request_does_not_emit_callback=F, multipath_requests_are_provided=T => FALSE |
| 1967 | // MCDC SYS-REQ-008: eachkey_callback_receives_found_values=F, eachkey_completes_requested_scan=F, eachkey_malformed_input_returns_error=F, missing_multipath_request_does_not_emit_callback=T, multipath_requests_are_provided=T => TRUE |
| 1968 | // MCDC SYS-REQ-008: eachkey_callback_receives_found_values=F, eachkey_completes_requested_scan=T, eachkey_malformed_input_returns_error=F, missing_multipath_request_does_not_emit_callback=F, multipath_requests_are_provided=T => TRUE |
| 1969 | // MCDC SYS-REQ-008: eachkey_callback_receives_found_values=T, eachkey_completes_requested_scan=F, eachkey_malformed_input_returns_error=F, missing_multipath_request_does_not_emit_callback=F, multipath_requests_are_provided=T => TRUE |
| 1970 | func TestEachKey(t *testing.T) { |
| 1971 | paths := [][]string{ |
| 1972 | {"name"}, |
| 1973 | {"order"}, |
| 1974 | {"nested", "a"}, |
| 1975 | {"nested", "b"}, |
| 1976 | {"nested2", "a"}, |
| 1977 | {"nested", "nested3", "b"}, |
| 1978 | {"arr", "[1]", "b"}, |
| 1979 | {"arrInt", "[3]"}, |
| 1980 | {"arrInt", "[5]"}, // Should not find last key |
| 1981 | {"nested"}, |
| 1982 | {"arr", "["}, // issue#177 Invalid arguments |
| 1983 | {"a\n", "b\n"}, // issue#165 |
| 1984 | {"nested", "b"}, // Should find repeated key |
| 1985 | } |
| 1986 | |
| 1987 | keysFound := 0 |
| 1988 | |
| 1989 | EachKey(testJson, func(idx int, value []byte, vt ValueType, err error) { |
| 1990 | keysFound++ |
| 1991 | |
| 1992 | switch idx { |
| 1993 | case 0: |
| 1994 | if string(value) != "Name" { |
| 1995 | t.Error("Should find 1 key", string(value)) |
| 1996 | } |
| 1997 | case 1: |
| 1998 | if string(value) != "Order" { |
| 1999 | t.Errorf("Should find 2 key") |
| 2000 | } |
| 2001 | case 2: |
| 2002 | if string(value) != "test" { |
| 2003 | t.Errorf("Should find 3 key") |
| 2004 | } |
| 2005 | case 3: |
| 2006 | if string(value) != "2" { |
| 2007 | t.Errorf("Should find 4 key") |
| 2008 | } |
| 2009 | case 4: |
| 2010 | if string(value) != "test2" { |
| 2011 | t.Error("Should find 5 key", string(value)) |
| 2012 | } |
| 2013 | case 5: |
| 2014 | if string(value) != "4" { |
| 2015 | t.Errorf("Should find 6 key") |
| 2016 | } |
| 2017 | case 6: |
| 2018 | if string(value) != "2" { |
| 2019 | t.Errorf("Should find 7 key") |
| 2020 | } |
| 2021 | case 7: |
| 2022 | if string(value) != "4" { |
| 2023 | t.Error("Should find 8 key", string(value)) |
| 2024 | } |
| 2025 | case 8: |
| 2026 | t.Errorf("Found key #8 that should not be found") |
| 2027 | case 9: |
| 2028 | if string(value) != `{"a":"test", "b":2, "nested3":{"a":"test3","b":4}, "c": "unknown"}` { |
| 2029 | t.Error("Should find 9 key", string(value)) |
| 2030 | } |
| 2031 | case 10: |
| 2032 | t.Errorf("Found key #10 that should not be found") |
| 2033 | case 11: |
| 2034 | if string(value) != "99" { |
| 2035 | t.Error("Should find 10 key", string(value)) |
| 2036 | } |
| 2037 | case 12: |
| 2038 | if string(value) != "2" { |
| 2039 | t.Errorf("Should find 11 key") |
| 2040 | } |
| 2041 | default: |
| 2042 | t.Errorf("Should find only 10 keys, got %v key", idx) |
| 2043 | } |
| 2044 | }, paths...) |
| 2045 | |
| 2046 | if keysFound != 11 { |
| 2047 | t.Errorf("Should find 11 keys: %d", keysFound) |
| 2048 | } |
| 2049 | } |
| 2050 | |
| 2051 | type ParseTest struct { |
| 2052 | in string |
| 2053 | intype ValueType |
| 2054 | out interface{} |
| 2055 | isErr bool |
| 2056 | } |
| 2057 | |
| 2058 | var parseBoolTests = []ParseTest{ |
| 2059 | { |
| 2060 | in: "true", |
| 2061 | intype: Boolean, |
| 2062 | out: true, |
| 2063 | }, |
| 2064 | { |
| 2065 | in: "false", |
| 2066 | intype: Boolean, |
| 2067 | out: false, |
| 2068 | }, |
| 2069 | { |
| 2070 | in: "foo", |
| 2071 | intype: Boolean, |
| 2072 | isErr: true, |
| 2073 | }, |
| 2074 | { |
| 2075 | in: "trux", |
| 2076 | intype: Boolean, |
| 2077 | isErr: true, |
| 2078 | }, |
| 2079 | { |
| 2080 | in: "truex", |
| 2081 | intype: Boolean, |
| 2082 | isErr: true, |
| 2083 | }, |
| 2084 | { |
| 2085 | in: "", |
| 2086 | intype: Boolean, |
| 2087 | isErr: true, |
| 2088 | }, |
| 2089 | } |
| 2090 | |
| 2091 | var parseFloatTest = []ParseTest{ |
| 2092 | { |
| 2093 | in: "0", |
| 2094 | intype: Number, |
| 2095 | out: float64(0), |
| 2096 | }, |
| 2097 | { |
| 2098 | in: "0.0", |
| 2099 | intype: Number, |
| 2100 | out: float64(0.0), |
| 2101 | }, |
| 2102 | { |
| 2103 | in: "1", |
| 2104 | intype: Number, |
| 2105 | out: float64(1), |
| 2106 | }, |
| 2107 | { |
| 2108 | in: "1.234", |
| 2109 | intype: Number, |
| 2110 | out: float64(1.234), |
| 2111 | }, |
| 2112 | { |
| 2113 | in: "1.234e5", |
| 2114 | intype: Number, |
| 2115 | out: float64(1.234e5), |
| 2116 | }, |
| 2117 | { |
| 2118 | in: "-1.234e5", |
| 2119 | intype: Number, |
| 2120 | out: float64(-1.234e5), |
| 2121 | }, |
| 2122 | { |
| 2123 | in: "+1.234e5", // Note: + sign not allowed under RFC7159, but our parser accepts it since it uses strconv.ParseFloat |
| 2124 | intype: Number, |
| 2125 | out: float64(1.234e5), |
| 2126 | }, |
| 2127 | { |
| 2128 | in: "1.2.3", |
| 2129 | intype: Number, |
| 2130 | isErr: true, |
| 2131 | }, |
| 2132 | { |
| 2133 | in: "1..1", |
| 2134 | intype: Number, |
| 2135 | isErr: true, |
| 2136 | }, |
| 2137 | { |
| 2138 | in: "1a", |
| 2139 | intype: Number, |
| 2140 | isErr: true, |
| 2141 | }, |
| 2142 | { |
| 2143 | in: "", |
| 2144 | intype: Number, |
| 2145 | isErr: true, |
| 2146 | }, |
| 2147 | } |
| 2148 | |
| 2149 | // parseTestCheckNoError checks the error return from Parse*() against the test case expectations. |
| 2150 | // Returns true the test should proceed to checking the actual data returned from Parse*(), or false if the test is finished. |
| 2151 | // Test helper for SYS-REQ-012, SYS-REQ-013, SYS-REQ-014, and SYS-REQ-015. |
| 2152 | func parseTestCheckNoError(t *testing.T, testKind string, test ParseTest, value interface{}, err error) bool { |
| 2153 | if isErr := (err != nil); test.isErr != isErr { |
| 2154 | // If the call didn't match the error expectation, fail |
| 2155 | t.Errorf("%s test '%s' isErr mismatch: expected %t, obtained %t (err %v). Obtained value: %v", testKind, test.in, test.isErr, isErr, err, value) |
| 2156 | return false |
| 2157 | } else if isErr { |
| 2158 | // Else, if there was an error, don't fail and don't check isFound or the value |
| 2159 | return false |
| 2160 | } else { |
| 2161 | // Else, there was no error and a value was found, so check the value |
| 2162 | return true |
| 2163 | } |
| 2164 | } |
| 2165 | |
| 2166 | // Test helper for SYS-REQ-012, SYS-REQ-013, SYS-REQ-014, and SYS-REQ-015. |
| 2167 | func runParseTests(t *testing.T, testKind string, tests []ParseTest, runner func(ParseTest) (interface{}, error), resultChecker func(ParseTest, interface{}) (bool, interface{})) { |
| 2168 | for _, test := range tests { |
| 2169 | value, err := runner(test) |
| 2170 | |
| 2171 | if parseTestCheckNoError(t, testKind, test, value, err) { |
| 2172 | if test.out == nil { |
| 2173 | t.Errorf("MALFORMED TEST: %v", test) |
| 2174 | continue |
| 2175 | } |
| 2176 | |
| 2177 | if ok, expected := resultChecker(test, value); !ok { |
| 2178 | if expectedBytes, ok := expected.([]byte); ok { |
| 2179 | expected = string(expectedBytes) |
| 2180 | } |
| 2181 | if valueBytes, ok := value.([]byte); ok { |
| 2182 | value = string(valueBytes) |
| 2183 | } |
| 2184 | t.Errorf("%s test '%s' expected to return value %v, but did returned %v instead", testKind, test.in, expected, value) |
| 2185 | } |
| 2186 | } |
| 2187 | } |
| 2188 | } |
| 2189 | |
| 2190 | // Verifies: SYS-REQ-036 [example] |
| 2191 | // MCDC SYS-REQ-036: raw_boolean_literal_is_valid=F, returns_parseboolean_error=T => TRUE |
| 2192 | // Verifies: SYS-REQ-012 [example] |
| 2193 | // MCDC SYS-REQ-012: raw_boolean_literal_is_valid=F, returns_parseboolean_value=F => TRUE |
| 2194 | // MCDC SYS-REQ-012: raw_boolean_literal_is_valid=T, returns_parseboolean_value=F => FALSE |
| 2195 | // MCDC SYS-REQ-012: raw_boolean_literal_is_valid=T, returns_parseboolean_value=T => TRUE |
| 2196 | func TestParseBoolean(t *testing.T) { |
| 2197 | runParseTests(t, "ParseBoolean()", parseBoolTests, |
| 2198 | func(test ParseTest) (value interface{}, err error) { |
| 2199 | return ParseBoolean([]byte(test.in)) |
| 2200 | }, |
| 2201 | func(test ParseTest, obtained interface{}) (bool, interface{}) { |
| 2202 | expected := test.out.(bool) |
| 2203 | return obtained.(bool) == expected, expected |
| 2204 | }, |
| 2205 | ) |
| 2206 | } |
| 2207 | |
| 2208 | // Verifies: SYS-REQ-037 [example] |
| 2209 | // MCDC SYS-REQ-037: raw_float_token_is_well_formed=F, returns_parsefloat_error=T => TRUE |
| 2210 | // Verifies: SYS-REQ-013 [example] |
| 2211 | // MCDC SYS-REQ-013: raw_float_token_is_well_formed=F, returns_parsefloat_value=F => TRUE |
| 2212 | // MCDC SYS-REQ-013: raw_float_token_is_well_formed=T, returns_parsefloat_value=F => FALSE |
| 2213 | // MCDC SYS-REQ-013: raw_float_token_is_well_formed=T, returns_parsefloat_value=T => TRUE |
| 2214 | func TestParseFloat(t *testing.T) { |
| 2215 | runParseTests(t, "ParseFloat()", parseFloatTest, |
| 2216 | func(test ParseTest) (value interface{}, err error) { |
| 2217 | return ParseFloat([]byte(test.in)) |
| 2218 | }, |
| 2219 | func(test ParseTest, obtained interface{}) (bool, interface{}) { |
| 2220 | expected := test.out.(float64) |
| 2221 | return obtained.(float64) == expected, expected |
| 2222 | }, |
| 2223 | ) |
| 2224 | } |
| 2225 | |
| 2226 | // Verifies: SYS-REQ-013 [fuzz] |
| 2227 | // MCDC SYS-REQ-013: N/A |
| 2228 | func TestFuzzParseFloatHarnessCoverage(t *testing.T) { |
| 2229 | if got := FuzzParseFloat([]byte(`1.25`)); got != 1 { |
| 2230 | t.Fatalf("expected FuzzParseFloat success path to return 1, got %d", got) |
| 2231 | } |
| 2232 | if got := FuzzParseFloat([]byte(`1.2.3`)); got != 0 { |
| 2233 | t.Fatalf("expected FuzzParseFloat failure path to return 0, got %d", got) |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | // Verifies: STK-REQ-001 [boundary] |
| 2238 | // MCDC STK-REQ-001: N/A |
| 2239 | func TestValueTypeString(t *testing.T) { |
| 2240 | cases := []struct { |
| 2241 | value ValueType |
| 2242 | expected string |
| 2243 | }{ |
| 2244 | {NotExist, "non-existent"}, |
| 2245 | {String, "string"}, |
| 2246 | {Number, "number"}, |
| 2247 | {Object, "object"}, |
| 2248 | {Array, "array"}, |
| 2249 | {Boolean, "boolean"}, |
| 2250 | {Null, "null"}, |
| 2251 | {Unknown, "unknown"}, |
| 2252 | {ValueType(255), "unknown"}, |
| 2253 | } |
| 2254 | |
| 2255 | for _, tc := range cases { |
| 2256 | if got := tc.value.String(); got != tc.expected { |
| 2257 | t.Fatalf("ValueType(%d).String() = %q, want %q", tc.value, got, tc.expected) |
| 2258 | } |
| 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | // Verifies: STK-REQ-001 [boundary] |
| 2263 | // MCDC STK-REQ-001: N/A |
| 2264 | func TestTokenStart(t *testing.T) { |
| 2265 | cases := []struct { |
| 2266 | name string |
| 2267 | input string |
| 2268 | expected int |
| 2269 | }{ |
| 2270 | {name: "comma separator", input: `{"a":1,"b":2`, expected: 6}, |
| 2271 | {name: "array separator", input: `[1,2`, expected: 2}, |
| 2272 | {name: "opening object", input: `{"a":1`, expected: 0}, |
| 2273 | {name: "no separator", input: `value`, expected: 0}, |
| 2274 | } |
| 2275 | |
| 2276 | for _, tc := range cases { |
| 2277 | if got := tokenStart([]byte(tc.input)); got != tc.expected { |
| 2278 | t.Fatalf("%s: tokenStart(%q) = %d, want %d", tc.name, tc.input, got, tc.expected) |
| 2279 | } |
| 2280 | } |
| 2281 | } |
| 2282 | |
| 2283 | var parseStringTest = []ParseTest{ |
| 2284 | { |
| 2285 | in: ``, |
| 2286 | intype: String, |
| 2287 | out: "", |
| 2288 | }, |
| 2289 | { |
| 2290 | in: `\uFF11`, |
| 2291 | intype: String, |
| 2292 | out: "\uFF11", |
| 2293 | }, |
| 2294 | { |
| 2295 | in: `line\nbreak`, |
| 2296 | intype: String, |
| 2297 | out: "line\nbreak", |
| 2298 | }, |
| 2299 | { |
| 2300 | in: `\uFFFF`, |
| 2301 | intype: String, |
| 2302 | out: "\uFFFF", |
| 2303 | }, |
| 2304 | { |
| 2305 | in: `\uDF00`, |
| 2306 | intype: String, |
| 2307 | isErr: true, |
| 2308 | }, |
| 2309 | } |
| 2310 | |
| 2311 | // Verifies: SYS-REQ-038 [example] |
| 2312 | // MCDC SYS-REQ-038: raw_string_literal_is_well_formed=F, returns_parsestring_error=T => TRUE |
| 2313 | // Verifies: SYS-REQ-014 [example] |
| 2314 | // MCDC SYS-REQ-014: raw_string_literal_is_well_formed=F, returns_parsestring_value=F => TRUE |
| 2315 | // MCDC SYS-REQ-014: raw_string_literal_is_well_formed=T, returns_parsestring_value=F => FALSE |
| 2316 | // MCDC SYS-REQ-014: raw_string_literal_is_well_formed=T, returns_parsestring_value=T => TRUE |
| 2317 | func TestParseString(t *testing.T) { |
| 2318 | runParseTests(t, "ParseString()", parseStringTest, |
| 2319 | func(test ParseTest) (value interface{}, err error) { |
| 2320 | return ParseString([]byte(test.in)) |
| 2321 | }, |
| 2322 | func(test ParseTest, obtained interface{}) (bool, interface{}) { |
| 2323 | expected := test.out.(string) |
| 2324 | return obtained.(string) == expected, expected |
| 2325 | }, |
| 2326 | ) |
| 2327 | } |
| 2328 | |
| 2329 | // Verifies: SYS-REQ-040 [example] |
| 2330 | // MCDC SYS-REQ-040: raw_int_token_is_well_formed=F, raw_int_token_overflows_int64=F, returns_parseint_malformed_error=T => TRUE |
| 2331 | // Verifies: SYS-REQ-039 [example] |
| 2332 | // MCDC SYS-REQ-039: raw_int_token_overflows_int64=T, returns_parseint_overflow_error=T => TRUE |
| 2333 | // Verifies: SYS-REQ-015 [example] |
| 2334 | // MCDC SYS-REQ-015: raw_int_token_is_well_formed=F, returns_parseint_value=F => TRUE |
| 2335 | // MCDC SYS-REQ-015: raw_int_token_is_well_formed=T, returns_parseint_value=F => FALSE |
| 2336 | // MCDC SYS-REQ-015: raw_int_token_is_well_formed=T, returns_parseint_value=T => TRUE |
| 2337 | func TestParseInt(t *testing.T) { |
| 2338 | tests := []struct { |
| 2339 | name string |
| 2340 | in string |
| 2341 | want int64 |
| 2342 | wantErr error |
| 2343 | }{ |
| 2344 | { |
| 2345 | name: "zero", |
| 2346 | in: "0", |
| 2347 | want: 0, |
| 2348 | }, |
| 2349 | { |
| 2350 | name: "negative integer", |
| 2351 | in: "-12345", |
| 2352 | want: -12345, |
| 2353 | }, |
| 2354 | { |
| 2355 | name: "max int64", |
| 2356 | in: "9223372036854775807", |
| 2357 | want: 9223372036854775807, |
| 2358 | }, |
| 2359 | { |
| 2360 | name: "empty input", |
| 2361 | in: "", |
| 2362 | wantErr: MalformedValueError, |
| 2363 | }, |
| 2364 | { |
| 2365 | name: "fractional token", |
| 2366 | in: "1.2", |
| 2367 | wantErr: MalformedValueError, |
| 2368 | }, |
| 2369 | { |
| 2370 | name: "alpha suffix", |
| 2371 | in: "123x", |
| 2372 | wantErr: MalformedValueError, |
| 2373 | }, |
| 2374 | { |
| 2375 | name: "overflow", |
| 2376 | in: "9223372036854775808", |
| 2377 | wantErr: OverflowIntegerError, |
| 2378 | }, |
| 2379 | { |
| 2380 | name: "underflow", |
| 2381 | in: "-9223372036854775809", |
| 2382 | wantErr: OverflowIntegerError, |
| 2383 | }, |
| 2384 | } |
| 2385 | |
| 2386 | for _, test := range tests { |
| 2387 | t.Run(test.name, func(t *testing.T) { |
| 2388 | got, err := ParseInt([]byte(test.in)) |
| 2389 | if test.wantErr != nil { |
| 2390 | if !errors.Is(err, test.wantErr) { |
| 2391 | t.Fatalf("ParseInt(%q) error mismatch: expected %v, got %v", test.in, test.wantErr, err) |
| 2392 | } |
| 2393 | return |
| 2394 | } |
| 2395 | if err != nil { |
| 2396 | t.Fatalf("ParseInt(%q) returned unexpected error: %v", test.in, err) |
| 2397 | } |
| 2398 | if got != test.want { |
| 2399 | t.Fatalf("ParseInt(%q) value mismatch: expected %d, got %d", test.in, test.want, got) |
| 2400 | } |
| 2401 | }) |
| 2402 | } |
| 2403 | } |