1 m4_define([JSON_CHECK_POSITIVE],
3 AT_KEYWORDS([json positive])
4 AT_CHECK([printf %s "AS_ESCAPE([$2])" > input])
5 OVS_CHECK_LCOV([test-json $4 input], [0], [$3
9 m4_define([JSON_CHECK_NEGATIVE],
11 AT_KEYWORDS([json negative])
12 AT_CHECK([printf %s "AS_ESCAPE([$2])" > input])
13 OVS_CHECK_LCOV([test-json $4 input], [1], [$3
17 AT_BANNER([JSON -- arrays])
19 JSON_CHECK_POSITIVE([empty array], [[ [ ] ]], [[[]]])
20 JSON_CHECK_POSITIVE([single-element array], [[ [ 1 ] ]], [[[1]]])
21 JSON_CHECK_POSITIVE([2-element array], [[ [ 1, 2 ] ]], [[[1,2]]])
22 JSON_CHECK_POSITIVE([many-element array],
23 [[ [ 1, 2, 3, 4, 5 ] ]],
25 JSON_CHECK_NEGATIVE([missing comma], [[ [ 1, 2, 3 4, 5 ] ]],
26 [error: syntax error expecting '@:>@' or ','])
27 JSON_CHECK_NEGATIVE([trailing comma not allowed],
28 [[[1,2,]]], [error: syntax error expecting value])
29 JSON_CHECK_NEGATIVE([doubled comma not allowed],
30 [[[1,,2]]], [error: syntax error expecting value])
32 AT_BANNER([JSON -- strings])
34 JSON_CHECK_POSITIVE([empty string], [[[ "" ]]], [[[""]]])
35 JSON_CHECK_POSITIVE([1-character strings],
36 [[[ "a", "b", "c" ]]],
38 JSON_CHECK_POSITIVE([escape sequences],
39 [[[ " \" \\ \/ \b \f \n \r \t" ]]],
40 [[[" \" \\ / \b \f \n \r \t"]]])
41 JSON_CHECK_POSITIVE([Unicode escape sequences],
42 [[[ " \u0022 \u005c \u002F \u0008 \u000c \u000A \u000d \u0009" ]]],
43 [[[" \" \\ / \b \f \n \r \t"]]])
44 JSON_CHECK_POSITIVE([surrogate pairs],
47 JSON_CHECK_NEGATIVE([a string by itself is not valid JSON], ["xxx"],
48 [error: syntax error at beginning of input])
49 JSON_CHECK_NEGATIVE([end of line in quoted string],
52 [error: U+000A must be escaped in quoted string])
53 JSON_CHECK_NEGATIVE([formfeed in quoted string],
55 [error: U+000C must be escaped in quoted string])
56 JSON_CHECK_NEGATIVE([bad escape in quoted string],
58 [error: bad escape \x])
59 JSON_CHECK_NEGATIVE([\u must be followed by 4 hex digits],
61 [error: malformed \u escape])
62 JSON_CHECK_NEGATIVE([isolated leading surrogate not allowed],
64 [error: malformed escaped surrogate pair])
65 JSON_CHECK_NEGATIVE([surrogatess must paired properly],
67 [error: second half of escaped surrogate pair is not trailing surrogate])
68 JSON_CHECK_NEGATIVE([null bytes not allowed],
70 [error: null bytes not supported in quoted strings])
72 AT_SETUP([end of input in quoted string])
73 AT_KEYWORDS([json negative])
74 AT_CHECK([printf '\"xxx' | test-json -], [1],
75 [error: unexpected end of input in quoted string
79 AT_BANNER([JSON -- objects])
81 JSON_CHECK_POSITIVE([empty object], [[{ }]], [[{}]])
82 JSON_CHECK_POSITIVE([simple object],
83 [[{"b": 2, "a": 1, "c": 3}]],
84 [[{"a":1,"b":2,"c":3}]])
85 JSON_CHECK_NEGATIVE([bad value], [[{"a": }, "b": 2]],
86 [error: syntax error expecting value])
87 JSON_CHECK_NEGATIVE([missing colon], [[{"b": 2, "a" 1, "c": 3}]],
88 [error: syntax error parsing object expecting ':'])
89 JSON_CHECK_NEGATIVE([missing comma], [[{"b": 2 "a" 1, "c": 3}]],
90 [error: syntax error expecting '}' or ','])
91 JSON_CHECK_NEGATIVE([trailing comma not allowed],
92 [[{"b": 2, "a": 1, "c": 3, }]],
93 [[error: syntax error parsing object expecting string]])
94 JSON_CHECK_NEGATIVE([doubled comma not allowed],
95 [[{"b": 2, "a": 1,, "c": 3}]],
96 [[error: syntax error parsing object expecting string]])
97 JSON_CHECK_NEGATIVE([names must be strings],
99 [[error: syntax error parsing object expecting string]])
101 AT_BANNER([JSON -- literal names])
103 JSON_CHECK_POSITIVE([null], [[[ null ]]], [[[null]]])
104 JSON_CHECK_POSITIVE([false], [[[ false ]]], [[[false]]])
105 JSON_CHECK_POSITIVE([true], [[[ true ]]], [[[true]]])
106 JSON_CHECK_NEGATIVE([a literal by itself is not valid JSON], [null],
107 [error: syntax error at beginning of input])
108 JSON_CHECK_NEGATIVE([nullify is invalid], [[[ nullify ]]],
109 [error: invalid keyword 'nullify'])
110 JSON_CHECK_NEGATIVE([nubs is invalid], [[[ nubs ]]],
111 [error: invalid keyword 'nubs'])
112 JSON_CHECK_NEGATIVE([xxx is invalid], [[[ xxx ]]],
113 [error: invalid keyword 'xxx'])
115 AT_BANNER([JSON -- numbers])
118 [integers expressed as reals],
120 2.00000000000000000000000000000000000,
126 [[[1,2,200000,21234,2123,0,0]]])
129 [[[9223372036854775807, -9223372036854775808]]],
130 [[[9223372036854775807,-9223372036854775808]]])
132 [large integers expressed as reals],
133 [[[9223372036854775807.0, -9223372036854775808.0,
134 92233720.36854775807e11, -9.223372036854775808e18]]],
135 [[[9223372036854775807,-9223372036854775808,9223372036854775807,-9223372036854775808]]])
136 # It seems likely that the following test will fail on some system that
137 # rounds slightly differently in arithmetic or in printf, but I'd like
138 # to keep it this way until we run into such a system.
140 [large integers that overflow to reals],
141 [[[9223372036854775807000, -92233720368547758080000]]],
142 [[[9.22337203685478e+21,-9.22337203685478e+22]]])
146 [[[-0, -0.0, 1e-9999, -1e-9999]]],
151 [[[0.0, 1.0, 2.0, 3.0, 3.5, 81.250]]],
152 [[[0,1,2,3,3.5,81.25]]])
154 [scientific notation],
155 [[[1e3, 1E3, 2.5E2, 1e+3, 125e-3, 3.125e-2, 3125e-05, 1.525878906e-5]]],
156 [[[1000,1000,250,1000,0.125,0.03125,0.03125,1.525878906e-05]]])
159 [[[-0, -1.0, -2.0, -3.0, -3.5, -8.1250]]],
160 [[[0,-1,-2,-3,-3.5,-8.125]]])
162 [negative scientific notation],
163 [[[-1e3, -1E3, -2.5E2, -1e+3, -125e-3, -3.125e-2, -3125e-05, -1.525878906e-5]]],
164 [[[-1000,-1000,-250,-1000,-0.125,-0.03125,-0.03125,-1.525878906e-05]]])
166 [1e-9999 underflows to 0],
169 JSON_CHECK_NEGATIVE([a number by itself is not valid JSON], [1],
170 [error: syntax error at beginning of input])
172 [leading zeros not allowed],
174 [error: leading zeros not allowed])
178 [error: number outside valid range])
180 [exponent bigger than INT_MAX],
181 [[[1e9999999999999999999]]],
182 [error: exponent outside valid range])
184 [decimal point must be followed by digit],
186 [error: decimal point must be followed by digit])
188 [exponent must contain at least one digit (1)],
190 [error: exponent must contain at least one digit])
192 [exponent must contain at least one digit (2)],
194 [error: exponent must contain at least one digit])
196 [exponent must contain at least one digit (3)],
198 [error: exponent must contain at least one digit])
200 AT_BANNER([JSON -- RFC 4627 examples])
202 JSON_CHECK_POSITIVE([RFC 4267 object example],
207 "Title": "View from 15th Floor",
209 "Url": "http://www.example.com/image/481989943",
213 "IDs": [116, 943, 234, 38793]
216 [[{"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}}]])
218 JSON_CHECK_POSITIVE([RFC 4267 array example],
223 "Longitude": -122.3959,
225 "City": "SAN FRANCISCO",
232 "Latitude": 37.371991,
233 "Longitude": -122.026020,
241 [[[{"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"}]]])
243 AT_BANNER([JSON -- pathological cases])
245 JSON_CHECK_NEGATIVE([trailing garbage], [[[1]null]],
246 [error: trailing garbage at end of input])
247 JSON_CHECK_NEGATIVE([formfeeds are not valid white space],
248 [[[
\f]]], [error: invalid character U+000c])
249 JSON_CHECK_NEGATIVE([';' is not a valid token],
250 [;], [error: invalid character ';'])
251 JSON_CHECK_NEGATIVE([arrays nesting too deep],
252 [m4_for([i], [0], [1002], [1], [@<:@])dnl
253 m4_for([i], [0], [1002], [1], [@:>@])],
254 [error: input exceeds maximum nesting depth 1000])
255 JSON_CHECK_NEGATIVE([objects nesting too deep],
256 [m4_for([i], [0], [1002], [1], [{"x":])dnl
257 m4_for([i], [0], [1002], [1], [}])],
258 [error: input exceeds maximum nesting depth 1000])
260 AT_SETUP([input may not be empty])
261 AT_KEYWORDS([json negative])
262 AT_CHECK([test-json /dev/null], [1], [error: empty input stream
266 AT_BANNER([JSON -- multiple inputs])
268 JSON_CHECK_POSITIVE([multiple adjacent objects], [[{}{}{}]], [[{}
273 JSON_CHECK_POSITIVE([multiple space-separated objects], [[{} {} {}]], [[{}
278 JSON_CHECK_POSITIVE([multiple objects on separate lines], [[{}
285 JSON_CHECK_POSITIVE([multiple objects and arrays], [[{}[]{}[]]], [[{}
291 JSON_CHECK_NEGATIVE([garbage between multiple objects], [[{}x{}]], [[{}
292 error: invalid keyword 'x'
295 JSON_CHECK_NEGATIVE([garbage after multiple objects], [[{}{}x]], [[{}
297 error: invalid keyword 'x']], [--multiple])