1 m4_define([JSON_CHECK_POSITIVE],
3 AT_KEYWORDS([json positive])
4 AT_CHECK([printf %s "AS_ESCAPE([$2])" > input])
5 AT_CAPTURE_FILE([input])
6 AT_CHECK([test-json $4 input], [0], [stdout], [])
7 AT_CHECK([cat stdout], [0], [$3
11 m4_define([JSON_CHECK_NEGATIVE],
13 AT_KEYWORDS([json negative])
14 AT_CHECK([printf %s "AS_ESCAPE([$2])" > input])
15 AT_CAPTURE_FILE([input])
16 AT_CHECK([test-json $4 input], [1], [stdout], [])
17 AT_CHECK([[sed 's/^error: [^:]*:/error:/' < stdout]], [0], [$3
21 AT_BANNER([JSON -- arrays])
23 JSON_CHECK_POSITIVE([empty array], [[ [ ] ]], [[[]]])
24 JSON_CHECK_POSITIVE([single-element array], [[ [ 1 ] ]], [[[1]]])
25 JSON_CHECK_POSITIVE([2-element array], [[ [ 1, 2 ] ]], [[[1,2]]])
26 JSON_CHECK_POSITIVE([many-element array],
27 [[ [ 1, 2, 3, 4, 5 ] ]],
29 JSON_CHECK_NEGATIVE([missing comma], [[ [ 1, 2, 3 4, 5 ] ]],
30 [error: syntax error expecting '@:>@' or ','])
31 JSON_CHECK_NEGATIVE([trailing comma not allowed],
32 [[[1,2,]]], [error: syntax error expecting value])
33 JSON_CHECK_NEGATIVE([doubled comma not allowed],
34 [[[1,,2]]], [error: syntax error expecting value])
36 AT_BANNER([JSON -- strings])
38 JSON_CHECK_POSITIVE([empty string], [[[ "" ]]], [[[""]]])
39 JSON_CHECK_POSITIVE([1-character strings],
40 [[[ "a", "b", "c" ]]],
42 JSON_CHECK_POSITIVE([escape sequences],
43 [[[ " \" \\ \/ \b \f \n \r \t" ]]],
44 [[[" \" \\ / \b \f \n \r \t"]]])
45 JSON_CHECK_POSITIVE([Unicode escape sequences],
46 [[[ " \u0022 \u005c \u002F \u0008 \u000c \u000A \u000d \u0009" ]]],
47 [[[" \" \\ / \b \f \n \r \t"]]])
48 JSON_CHECK_POSITIVE([surrogate pairs],
51 JSON_CHECK_NEGATIVE([a string by itself is not valid JSON], ["xxx"],
52 [error: syntax error at beginning of input])
53 JSON_CHECK_NEGATIVE([end of line in quoted string],
56 [error: U+000A must be escaped in quoted string])
57 JSON_CHECK_NEGATIVE([formfeed in quoted string],
59 [error: U+000C must be escaped in quoted string])
60 JSON_CHECK_NEGATIVE([bad escape in quoted string],
62 [error: bad escape \x])
63 JSON_CHECK_NEGATIVE([\u must be followed by 4 hex digits (1)],
65 [error: quoted string ends within \u escape])
66 JSON_CHECK_NEGATIVE([\u must be followed by 4 hex digits (2)],
68 [error: malformed \u escape])
69 JSON_CHECK_NEGATIVE([isolated leading surrogate not allowed],
71 [error: malformed escaped surrogate pair])
72 JSON_CHECK_NEGATIVE([surrogatess must paired properly],
74 [error: second half of escaped surrogate pair is not trailing surrogate])
75 JSON_CHECK_NEGATIVE([null bytes not allowed],
77 [error: null bytes not supported in quoted strings])
79 AT_SETUP([end of input in quoted string])
80 AT_KEYWORDS([json negative])
81 AT_CHECK([printf '\"xxx' | test-json -], [1],
82 [error: line 0, column 4, byte 4: unexpected end of input in quoted string
86 AT_BANNER([JSON -- objects])
88 JSON_CHECK_POSITIVE([empty object], [[{ }]], [[{}]])
89 JSON_CHECK_POSITIVE([simple object],
90 [[{"b": 2, "a": 1, "c": 3}]],
91 [[{"a":1,"b":2,"c":3}]])
92 JSON_CHECK_NEGATIVE([bad value], [[{"a": }, "b": 2]],
93 [error: syntax error expecting value])
94 JSON_CHECK_NEGATIVE([missing colon], [[{"b": 2, "a" 1, "c": 3}]],
95 [error: syntax error parsing object expecting ':'])
96 JSON_CHECK_NEGATIVE([missing comma], [[{"b": 2 "a" 1, "c": 3}]],
97 [error: syntax error expecting '}' or ','])
98 JSON_CHECK_NEGATIVE([trailing comma not allowed],
99 [[{"b": 2, "a": 1, "c": 3, }]],
100 [[error: syntax error parsing object expecting string]])
101 JSON_CHECK_NEGATIVE([doubled comma not allowed],
102 [[{"b": 2, "a": 1,, "c": 3}]],
103 [[error: syntax error parsing object expecting string]])
104 JSON_CHECK_NEGATIVE([names must be strings],
106 [[error: syntax error parsing object expecting string]])
108 AT_BANNER([JSON -- literal names])
110 JSON_CHECK_POSITIVE([null], [[[ null ]]], [[[null]]])
111 JSON_CHECK_POSITIVE([false], [[[ false ]]], [[[false]]])
112 JSON_CHECK_POSITIVE([true], [[[ true ]]], [[[true]]])
113 JSON_CHECK_NEGATIVE([a literal by itself is not valid JSON], [null],
114 [error: syntax error at beginning of input])
115 JSON_CHECK_NEGATIVE([nullify is invalid], [[[ nullify ]]],
116 [error: invalid keyword 'nullify'])
117 JSON_CHECK_NEGATIVE([nubs is invalid], [[[ nubs ]]],
118 [error: invalid keyword 'nubs'])
119 JSON_CHECK_NEGATIVE([xxx is invalid], [[[ xxx ]]],
120 [error: invalid keyword 'xxx'])
122 AT_BANNER([JSON -- numbers])
125 [integers expressed as reals],
127 2.00000000000000000000000000000000000,
133 [[[1,2,200000,21234,2123,0,0]]])
136 [[[9223372036854775807, -9223372036854775808]]],
137 [[[9223372036854775807,-9223372036854775808]]])
139 [large integers expressed as reals],
140 [[[9223372036854775807.0, -9223372036854775808.0,
141 92233720.36854775807e11, -9.223372036854775808e18]]],
142 [[[9223372036854775807,-9223372036854775808,9223372036854775807,-9223372036854775808]]])
143 # It seems likely that the following test will fail on some system that
144 # rounds slightly differently in arithmetic or in printf, but I'd like
145 # to keep it this way until we run into such a system.
147 [large integers that overflow to reals],
148 [[[9223372036854775807000, -92233720368547758080000]]],
149 [[[9.22337203685478e+21,-9.22337203685478e+22]]])
153 [[[-0, -0.0, 1e-9999, -1e-9999]]],
158 [[[0.0, 1.0, 2.0, 3.0, 3.5, 81.250]]],
159 [[[0,1,2,3,3.5,81.25]]])
161 [scientific notation],
162 [[[1e3, 1E3, 2.5E2, 1e+3, 125e-3, 3.125e-2, 3125e-05, 1.525878906e-5]]],
163 [[[1000,1000,250,1000,0.125,0.03125,0.03125,1.525878906e-05]]])
164 # It seems likely that the following test will fail on some system that
165 # rounds slightly differently in arithmetic or in printf, but I'd like
166 # to keep it this way until we run into such a system.
169 [[[1.7976931348623157e+308, -1.7976931348623157e+308]]],
170 [[[1.79769313486232e+308,-1.79769313486232e+308]]])
174 [[[-0, -1.0, -2.0, -3.0, -3.5, -8.1250]]],
175 [[[0,-1,-2,-3,-3.5,-8.125]]])
177 [negative scientific notation],
178 [[[-1e3, -1E3, -2.5E2, -1e+3, -125e-3, -3.125e-2, -3125e-05, -1.525878906e-5]]],
179 [[[-1000,-1000,-250,-1000,-0.125,-0.03125,-0.03125,-1.525878906e-05]]])
181 [1e-9999 underflows to 0],
184 JSON_CHECK_NEGATIVE([a number by itself is not valid JSON], [1],
185 [error: syntax error at beginning of input])
187 [leading zeros not allowed],
189 [error: leading zeros not allowed])
193 [error: number outside valid range])
195 [exponent bigger than INT_MAX],
196 [[[1e9999999999999999999]]],
197 [error: exponent outside valid range])
199 [decimal point must be followed by digit],
201 [error: decimal point must be followed by digit])
203 [exponent must contain at least one digit (1)],
205 [error: exponent must contain at least one digit])
207 [exponent must contain at least one digit (2)],
209 [error: exponent must contain at least one digit])
211 [exponent must contain at least one digit (3)],
213 [error: exponent must contain at least one digit])
215 AT_BANNER([JSON -- RFC 4627 examples])
217 JSON_CHECK_POSITIVE([RFC 4267 object example],
222 "Title": "View from 15th Floor",
224 "Url": "http://www.example.com/image/481989943",
228 "IDs": [116, 943, 234, 38793]
231 [[{"Image":{"Height":600,"IDs":[116,943,234,38793],"Thumbnail":{"Height":125,"Url":"http://www.example.com/image/481989943","Width":"100"},"Title":"View from 15th Floor","Width":800}}]])
233 JSON_CHECK_POSITIVE([RFC 4267 array example],
238 "Longitude": -122.3959,
240 "City": "SAN FRANCISCO",
247 "Latitude": 37.371991,
248 "Longitude": -122.026020,
256 [[[{"Address":"","City":"SAN FRANCISCO","Country":"US","Latitude":37.7668,"Longitude":-122.3959,"State":"CA","Zip":"94107","precision":"zip"},{"Address":"","City":"SUNNYVALE","Country":"US","Latitude":37.371991,"Longitude":-122.02602,"State":"CA","Zip":"94085","precision":"zip"}]]])
258 AT_BANNER([JSON -- pathological cases])
260 JSON_CHECK_NEGATIVE([trailing garbage], [[[1]null]],
261 [error: trailing garbage at end of input])
262 JSON_CHECK_NEGATIVE([formfeeds are not valid white space],
263 [[[
\f]]], [error: invalid character U+000c])
264 JSON_CHECK_NEGATIVE([';' is not a valid token],
265 [;], [error: invalid character ';'])
266 JSON_CHECK_NEGATIVE([arrays nesting too deep],
267 [m4_for([i], [0], [1002], [1], [@<:@])dnl
268 m4_for([i], [0], [1002], [1], [@:>@])],
269 [error: input exceeds maximum nesting depth 1000])
270 JSON_CHECK_NEGATIVE([objects nesting too deep],
271 [m4_for([i], [0], [1002], [1], [{"x":])dnl
272 m4_for([i], [0], [1002], [1], [}])],
273 [error: input exceeds maximum nesting depth 1000])
275 AT_SETUP([input may not be empty])
276 AT_KEYWORDS([json negative])
277 AT_CHECK([test-json /dev/null], [1], [error: line 0, column 0, byte 0: empty input stream
281 AT_BANNER([JSON -- multiple inputs])
283 JSON_CHECK_POSITIVE([multiple adjacent objects], [[{}{}{}]], [[{}
288 JSON_CHECK_POSITIVE([multiple space-separated objects], [[{} {} {}]], [[{}
293 JSON_CHECK_POSITIVE([multiple objects on separate lines], [[{}
300 JSON_CHECK_POSITIVE([multiple objects and arrays], [[{}[]{}[]]], [[{}
306 JSON_CHECK_NEGATIVE([garbage between multiple objects], [[{}x{}]], [[{}
307 error: invalid keyword 'x'
310 JSON_CHECK_NEGATIVE([garbage after multiple objects], [[{}{}x]], [[{}
312 error: invalid keyword 'x']], [--multiple])