| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test success or failure of the incremental (table-driven) JSON parser |
| 5 |
# for a variety of small inputs. |
| 6 |
|
| 7 |
use strict; |
| 8 |
use warnings FATAL => 'all'; |
| 9 |
|
| 10 |
use PostgreSQL::Test::Utils; |
| 11 |
use Test::More; |
| 12 |
|
| 13 |
use File::Temp qw(tempfile); |
| 14 |
|
| 15 |
my $dir = PostgreSQL::Test::Utils::tempdir; |
| 16 |
my @exe; |
| 17 |
|
| 18 |
sub test |
| 19 |
{ |
| 20 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 21 |
|
| 22 |
my ($name, $json, %params) = @_; |
| 23 |
my $chunk = length($json); |
| 24 |
|
| 25 |
# Test the input with chunk sizes from max(input_size, 64) down to 1 |
| 26 |
|
| 27 |
if ($chunk > 64) |
| 28 |
{ |
| 29 |
$chunk = 64; |
| 30 |
} |
| 31 |
|
| 32 |
my ($fh, $fname) = tempfile(DIR => $dir); |
| 33 |
print $fh "$json"; |
| 34 |
close($fh); |
| 35 |
|
| 36 |
# The -r mode runs the parser in a loop, with output separated by nulls. |
| 37 |
# Unpack that as a list of null-terminated ASCII strings (Z*) and check that |
| 38 |
# each run produces the same result. |
| 39 |
my ($all_stdout, $all_stderr) = |
| 40 |
run_command([ @exe, "-r", $chunk, $fname ]); |
| 41 |
|
| 42 |
my @stdout = unpack("(Z*)*", $all_stdout); |
| 43 |
my @stderr = unpack("(Z*)*", $all_stderr); |
| 44 |
|
| 45 |
is(scalar @stdout, $chunk, "$name: stdout has correct number of entries"); |
| 46 |
is(scalar @stderr, $chunk, "$name: stderr has correct number of entries"); |
| 47 |
|
| 48 |
my $i = 0; |
| 49 |
|
| 50 |
foreach my $size (reverse(1 .. $chunk)) |
| 51 |
{ |
| 52 |
if (defined($params{error})) |
| 53 |
{ |
| 54 |
unlike($stdout[$i], qr/SUCCESS/, |
| 55 |
"$name, chunk size $size: test fails"); |
| 56 |
like($stderr[$i], $params{error}, |
| 57 |
"$name, chunk size $size: correct error output"); |
| 58 |
} |
| 59 |
else |
| 60 |
{ |
| 61 |
like($stdout[$i], qr/SUCCESS/, |
| 62 |
"$name, chunk size $size: test succeeds"); |
| 63 |
is($stderr[$i], "", "$name, chunk size $size: no error output"); |
| 64 |
} |
| 65 |
|
| 66 |
$i++; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
my @exes = ( |
| 71 |
[ "test_json_parser_incremental", ], |
| 72 |
[ "test_json_parser_incremental", "-o", ], |
| 73 |
[ "test_json_parser_incremental_shlib", ], |
| 74 |
[ "test_json_parser_incremental_shlib", "-o", ]); |
| 75 |
|
| 76 |
foreach (@exes) |
| 77 |
{ |
| 78 |
@exe = @$_; |
| 79 |
note "testing executable @exe"; |
| 80 |
|
| 81 |
test("number", "12345"); |
| 82 |
test("string", '"hello"'); |
| 83 |
test("false", "false"); |
| 84 |
test("true", "true"); |
| 85 |
test("null", "null"); |
| 86 |
test("empty object", "{}"); |
| 87 |
test("empty array", "[]"); |
| 88 |
test("array with number", "[12345]"); |
| 89 |
test("array with numbers", "[12345,67890]"); |
| 90 |
test("array with null", "[null]"); |
| 91 |
test("array with string", '["hello"]'); |
| 92 |
test("array with boolean", '[false]'); |
| 93 |
test("single pair", '{"key": "value"}'); |
| 94 |
test("heavily nested array", "[" x 3200 . "]" x 3200); |
| 95 |
test("serial escapes", '"\\\\\\\\\\\\\\\\"'); |
| 96 |
test("interrupted escapes", '"\\\\\\"\\\\\\\\\\"\\\\"'); |
| 97 |
test("whitespace", ' "" '); |
| 98 |
|
| 99 |
test("unclosed empty object", |
| 100 |
"{", error => qr/input string ended unexpectedly/); |
| 101 |
test("bad key", "{{", |
| 102 |
error => qr/Expected string or "}", but found "\{"/); |
| 103 |
test("bad key", "{{}", |
| 104 |
error => qr/Expected string or "}", but found "\{"/); |
| 105 |
test("numeric key", "{1234: 2}", |
| 106 |
error => qr/Expected string or "}", but found "1234"/); |
| 107 |
test( |
| 108 |
"second numeric key", |
| 109 |
'{"a": "a", 1234: 2}', |
| 110 |
error => qr/Expected string, but found "1234"/); |
| 111 |
test( |
| 112 |
"unclosed object with pair", |
| 113 |
'{"key": "value"', |
| 114 |
error => qr/input string ended unexpectedly/); |
| 115 |
test("missing key value", |
| 116 |
'{"key": }', error => qr/Expected JSON value, but found "}"/); |
| 117 |
test( |
| 118 |
"missing colon", |
| 119 |
'{"key" 12345}', |
| 120 |
error => qr/Expected ":", but found "12345"/); |
| 121 |
test( |
| 122 |
"missing comma", |
| 123 |
'{"key": 12345 12345}', |
| 124 |
error => qr/Expected "," or "}", but found "12345"/); |
| 125 |
test("overnested array", |
| 126 |
"[" x 6401, error => qr/maximum permitted depth is 6400/); |
| 127 |
test("overclosed array", |
| 128 |
"[]]", error => qr/Expected end of input, but found "]"/); |
| 129 |
test("unexpected token in array", |
| 130 |
"[ }}} ]", error => qr/Expected array element or "]", but found "}"/); |
| 131 |
test("junk punctuation", "[ ||| ]", error => qr/Token "|" is invalid/); |
| 132 |
test("missing comma in array", |
| 133 |
"[123 123]", error => qr/Expected "," or "]", but found "123"/); |
| 134 |
test("misspelled boolean", "tru", error => qr/Token "tru" is invalid/); |
| 135 |
test( |
| 136 |
"misspelled boolean in array", |
| 137 |
"[tru]", |
| 138 |
error => qr/Token "tru" is invalid/); |
| 139 |
test( |
| 140 |
"smashed top-level scalar", |
| 141 |
"12zz", |
| 142 |
error => qr/Token "12zz" is invalid/); |
| 143 |
test( |
| 144 |
"smashed scalar in array", |
| 145 |
"[12zz]", |
| 146 |
error => qr/Token "12zz" is invalid/); |
| 147 |
test( |
| 148 |
"unknown escape sequence", |
| 149 |
'"hello\vworld"', |
| 150 |
error => qr/Escape sequence "\\v" is invalid/); |
| 151 |
test("unescaped control", |
| 152 |
"\"hello\tworld\"", |
| 153 |
error => qr/Character with value 0x09 must be escaped/); |
| 154 |
test( |
| 155 |
"incorrect escape count", |
| 156 |
'"\\\\\\\\\\\\\\"', |
| 157 |
error => qr/Token ""\\\\\\\\\\\\\\"" is invalid/); |
| 158 |
|
| 159 |
# Case with three bytes: double-quote, backslash and <f5>. |
| 160 |
# Both invalid-token and invalid-escape are possible errors, because for |
| 161 |
# smaller chunk sizes the incremental parser skips the string parsing when |
| 162 |
# it cannot find an ending quote. |
| 163 |
test("incomplete UTF-8 sequence", |
| 164 |
"\"\\\x{F5}", |
| 165 |
error => qr/(Token|Escape sequence) ""?\\\x{F5}" is invalid/); |
| 166 |
} |
| 167 |
|
| 168 |
done_testing(); |