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 OVS_CHECK_LCOV([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 OVS_CHECK_LCOV([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],
65 [error: malformed \u escape])
66 JSON_CHECK_NEGATIVE([isolated leading surrogate not allowed],
68 [error: malformed escaped surrogate pair])
69 JSON_CHECK_NEGATIVE([surrogatess must paired properly],
71 [error: second half of escaped surrogate pair is not trailing surrogate])
72 JSON_CHECK_NEGATIVE([null bytes not allowed],
74 [error: null bytes not supported in quoted strings])
76 AT_SETUP([end of input in quoted string])
77 AT_KEYWORDS([json negative])
78 AT_CHECK([printf '\"xxx' | test-json -], [1],
79 [error: line 0, column 4, byte 4: unexpected end of input in quoted string
83 AT_BANNER([JSON -- objects])
85 JSON_CHECK_POSITIVE([empty object], [[{ }]], [[{}]])
86 JSON_CHECK_POSITIVE([simple object],
87 [[{"b": 2, "a": 1, "c": 3}]],
88 [[{"a":1,"b":2,"c":3}]])
89 JSON_CHECK_NEGATIVE([bad value], [[{"a": }, "b": 2]],
90 [error: syntax error expecting value])
91 JSON_CHECK_NEGATIVE([missing colon], [[{"b": 2, "a" 1, "c": 3}]],
92 [error: syntax error parsing object expecting ':'])
93 JSON_CHECK_NEGATIVE([missing comma], [[{"b": 2 "a" 1, "c": 3}]],
94 [error: syntax error expecting '}' or ','])
95 JSON_CHECK_NEGATIVE([trailing comma not allowed],
96 [[{"b": 2, "a": 1, "c": 3, }]],
97 [[error: syntax error parsing object expecting string]])
98 JSON_CHECK_NEGATIVE([doubled comma not allowed],
99 [[{"b": 2, "a": 1,, "c": 3}]],
100 [[error: syntax error parsing object expecting string]])
101 JSON_CHECK_NEGATIVE([names must be strings],
103 [[error: syntax error parsing object expecting string]])
105 AT_BANNER([JSON -- literal names])
107 JSON_CHECK_POSITIVE([null], [[[ null ]]], [[[null]]])
108 JSON_CHECK_POSITIVE([false], [[[ false ]]], [[[false]]])
109 JSON_CHECK_POSITIVE([true], [[[ true ]]], [[[true]]])
110 JSON_CHECK_NEGATIVE([a literal by itself is not valid JSON], [null],
111 [error: syntax error at beginning of input])
112 JSON_CHECK_NEGATIVE([nullify is invalid], [[[ nullify ]]],
113 [error: invalid keyword 'nullify'])
114 JSON_CHECK_NEGATIVE([nubs is invalid], [[[ nubs ]]],
115 [error: invalid keyword 'nubs'])
116 JSON_CHECK_NEGATIVE([xxx is invalid], [[[ xxx ]]],
117 [error: invalid keyword 'xxx'])
119 AT_BANNER([JSON -- numbers])
122 [integers expressed as reals],
124 2.00000000000000000000000000000000000,
130 [[[1,2,200000,21234,2123,0,0]]])
133 [[[9223372036854775807, -9223372036854775808]]],
134 [[[9223372036854775807,-9223372036854775808]]])
136 [large integers expressed as reals],
137 [[[9223372036854775807.0, -9223372036854775808.0,
138 92233720.36854775807e11, -9.223372036854775808e18]]],
139 [[[9223372036854775807,-9223372036854775808,9223372036854775807,-9223372036854775808]]])
140 # It seems likely that the following test will fail on some system that
141 # rounds slightly differently in arithmetic or in printf, but I'd like
142 # to keep it this way until we run into such a system.
144 [large integers that overflow to reals],
145 [[[9223372036854775807000, -92233720368547758080000]]],
146 [[[9.22337203685478e+21,-9.22337203685478e+22]]])
150 [[[-0, -0.0, 1e-9999, -1e-9999]]],
155 [[[0.0, 1.0, 2.0, 3.0, 3.5, 81.250]]],
156 [[[0,1,2,3,3.5,81.25]]])
158 [scientific notation],
159 [[[1e3, 1E3, 2.5E2, 1e+3, 125e-3, 3.125e-2, 3125e-05, 1.525878906e-5]]],
160 [[[1000,1000,250,1000,0.125,0.03125,0.03125,1.525878906e-05]]])
163 [[[-0, -1.0, -2.0, -3.0, -3.5, -8.1250]]],
164 [[[0,-1,-2,-3,-3.5,-8.125]]])
166 [negative scientific notation],
167 [[[-1e3, -1E3, -2.5E2, -1e+3, -125e-3, -3.125e-2, -3125e-05, -1.525878906e-5]]],
168 [[[-1000,-1000,-250,-1000,-0.125,-0.03125,-0.03125,-1.525878906e-05]]])
170 [1e-9999 underflows to 0],
173 JSON_CHECK_NEGATIVE([a number by itself is not valid JSON], [1],
174 [error: syntax error at beginning of input])
176 [leading zeros not allowed],
178 [error: leading zeros not allowed])
182 [error: number outside valid range])
184 [exponent bigger than INT_MAX],
185 [[[1e9999999999999999999]]],
186 [error: exponent outside valid range])
188 [decimal point must be followed by digit],
190 [error: decimal point must be followed by digit])
192 [exponent must contain at least one digit (1)],
194 [error: exponent must contain at least one digit])
196 [exponent must contain at least one digit (2)],
198 [error: exponent must contain at least one digit])
200 [exponent must contain at least one digit (3)],
202 [error: exponent must contain at least one digit])
204 AT_BANNER([JSON -- RFC 4627 examples])
206 JSON_CHECK_POSITIVE([RFC 4267 object example],
211 "Title": "View from 15th Floor",
213 "Url": "http://www.example.com/image/481989943",
217 "IDs": [116, 943, 234, 38793]
220 [[{"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}}]])
222 JSON_CHECK_POSITIVE([RFC 4267 array example],
227 "Longitude": -122.3959,
229 "City": "SAN FRANCISCO",
236 "Latitude": 37.371991,
237 "Longitude": -122.026020,
245 [[[{"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"}]]])
247 AT_BANNER([JSON -- pathological cases])
249 JSON_CHECK_NEGATIVE([trailing garbage], [[[1]null]],
250 [error: trailing garbage at end of input])
251 JSON_CHECK_NEGATIVE([formfeeds are not valid white space],
252 [[[
\f]]], [error: invalid character U+000c])
253 JSON_CHECK_NEGATIVE([';' is not a valid token],
254 [;], [error: invalid character ';'])
255 JSON_CHECK_NEGATIVE([arrays nesting too deep],
256 [m4_for([i], [0], [1002], [1], [@<:@])dnl
257 m4_for([i], [0], [1002], [1], [@:>@])],
258 [error: input exceeds maximum nesting depth 1000])
259 JSON_CHECK_NEGATIVE([objects nesting too deep],
260 [m4_for([i], [0], [1002], [1], [{"x":])dnl
261 m4_for([i], [0], [1002], [1], [}])],
262 [error: input exceeds maximum nesting depth 1000])
264 AT_SETUP([input may not be empty])
265 AT_KEYWORDS([json negative])
266 AT_CHECK([test-json /dev/null], [1], [error: line 0, column 0, byte 0: empty input stream
270 AT_BANNER([JSON -- multiple inputs])
272 JSON_CHECK_POSITIVE([multiple adjacent objects], [[{}{}{}]], [[{}
277 JSON_CHECK_POSITIVE([multiple space-separated objects], [[{} {} {}]], [[{}
282 JSON_CHECK_POSITIVE([multiple objects on separate lines], [[{}
289 JSON_CHECK_POSITIVE([multiple objects and arrays], [[{}[]{}[]]], [[{}
295 JSON_CHECK_NEGATIVE([garbage between multiple objects], [[{}x{}]], [[{}
296 error: invalid keyword 'x'
299 JSON_CHECK_NEGATIVE([garbage after multiple objects], [[{}{}x]], [[{}
301 error: invalid keyword 'x']], [--multiple])