|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | + |
| 4 | +use PostgreSQL::Test::Utils; |
| 5 | +use Test::More; |
| 6 | + |
| 7 | +use File::Temp qw(tempfile); |
| 8 | + |
| 9 | +sub test |
| 10 | +{ |
| 11 | + local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 12 | + |
| 13 | + my ($name, $json, %params) = @_; |
| 14 | + my $exe = "test_json_parser_incremental"; |
| 15 | + my $chunk = length($json); |
| 16 | + |
| 17 | + if ($chunk > 64) |
| 18 | + { |
| 19 | + $chunk = 64; |
| 20 | + } |
| 21 | + |
| 22 | + my ($fh, $fname) = tempfile(UNLINK => 1); |
| 23 | + print $fh "$json"; |
| 24 | + close($fh); |
| 25 | + |
| 26 | + foreach my $size (reverse(1..$chunk)) |
| 27 | + { |
| 28 | + my ($stdout, $stderr) = run_command( [$exe, "-c", $size, $fname] ); |
| 29 | + |
| 30 | + if (defined($params{error})) |
| 31 | + { |
| 32 | + unlike($stdout, qr/SUCCESS/, "$name, chunk size $size: test fails"); |
| 33 | + like($stderr, $params{error}, "$name, chunk size $size: correct error output"); |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + like($stdout, qr/SUCCESS/, "$name, chunk size $size: test succeeds"); |
| 38 | + is($stderr, "", "$name, chunk size $size: no error output"); |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +test("number", "12345"); |
| 44 | +test("string", '"hello"'); |
| 45 | +test("false", "false"); |
| 46 | +test("true", "true"); |
| 47 | +test("null", "null"); |
| 48 | +test("empty object", "{}"); |
| 49 | +test("empty array", "[]"); |
| 50 | +test("array with number", "[12345]"); |
| 51 | +test("array with numbers", "[12345,67890]"); |
| 52 | +test("array with null", "[null]"); |
| 53 | +test("array with string", '["hello"]'); |
| 54 | +test("array with boolean", '[false]'); |
| 55 | +test("single pair", '{"key": "value"}'); |
| 56 | +test("heavily nested array", "[" x 3200 . "]" x 3200); |
| 57 | +test("serial escapes", '"\\\\\\\\\\\\\\\\"'); |
| 58 | +test("interrupted escapes", '"\\\\\\"\\\\\\\\\\"\\\\"'); |
| 59 | +test("whitespace", ' "" '); |
| 60 | + |
| 61 | +test("unclosed empty object", "{", error => qr/input string ended unexpectedly/); |
| 62 | +test("bad key", "{{", error => qr/Expected string or "}", but found "\{"/); |
| 63 | +test("bad key", "{{}", error => qr/Expected string or "}", but found "\{"/); |
| 64 | +test("numeric key", "{1234: 2}", error => qr/Expected string or "}", but found "1234"/); |
| 65 | +test("second numeric key", '{"a": "a", 1234: 2}', error => qr/Expected string, but found "1234"/); |
| 66 | +test("unclosed object with pair", '{"key": "value"', error => qr/input string ended unexpectedly/); |
| 67 | +test("missing key value", '{"key": }', error => qr/Expected JSON value, but found "}"/); |
| 68 | +test("missing colon", '{"key" 12345}', error => qr/Expected ":", but found "12345"/); |
| 69 | +test("missing comma", '{"key": 12345 12345}', error => qr/Expected "," or "}", but found "12345"/); |
| 70 | +test("overnested array", "[" x 6401, error => qr/maximum permitted depth is 6400/); |
| 71 | +test("overclosed array", "[]]", error => qr/Expected end of input, but found "]"/); |
| 72 | +test("unexpected token in array", "[ }}} ]", error => qr/Expected array element or "]", but found "}"/); |
| 73 | +test("junk punctuation", "[ ||| ]", error => qr/Token "|" is invalid/); |
| 74 | +test("missing comma in array", "[123 123]", error => qr/Expected "," or "]", but found "123"/); |
| 75 | +test("misspelled boolean", "tru", error => qr/Token "tru" is invalid/); |
| 76 | +test("misspelled boolean in array", "[tru]", error => qr/Token "tru" is invalid/); |
| 77 | +test("smashed top-level scalar", "12zz", error => qr/Token "12zz" is invalid/); |
| 78 | +test("smashed scalar in array", "[12zz]", error => qr/Token "12zz" is invalid/); |
| 79 | +test("unknown escape sequence", '"hello\vworld"', error => qr/Escape sequence "\\v" is invalid/); |
| 80 | +test("unescaped control", "\"hello\tworld\"", error => qr/Character with value 0x09 must be escaped/); |
| 81 | +test("incorrect escape count", '"\\\\\\\\\\\\\\"', error => qr/Token ""\\\\\\\\\\\\\\"" is invalid/); |
| 82 | + |
| 83 | +done_testing(); |
0 commit comments