8846ac95308cd4a9b19c9d62bc6e28c80270ca1e
[cascardo/ovs.git] / tests / json.at
1 m4_define([JSON_CHECK_POSITIVE_C], 
2   [AT_SETUP([$1])
3    AT_KEYWORDS([json positive])
4    AT_CHECK([printf %s "AS_ESCAPE([$2])" > input])
5    AT_CAPTURE_FILE([input])
6    AT_CHECK([ovstest test-json $4 input], [0], [stdout], [])
7    AT_CHECK([cat stdout], [0], [$3
8 ])
9    AT_CLEANUP])
10
11 m4_define([JSON_CHECK_POSITIVE_PY], 
12   [AT_SETUP([$1])
13    AT_KEYWORDS([json positive Python])
14    AT_SKIP_IF([test $HAVE_PYTHON = no])
15    AT_CHECK([printf %s "AS_ESCAPE([$2])" > input])
16    AT_CAPTURE_FILE([input])
17    AT_CHECK([$PYTHON $srcdir/test-json.py $4 input], [0], [stdout], [])
18    AT_CHECK([cat stdout], [0], [$3
19 ])
20    AT_CLEANUP])
21
22 m4_define([JSON_CHECK_POSITIVE_UCS4PY],
23   [AT_SETUP([$1])
24    AT_KEYWORDS([json positive Python])
25    AT_SKIP_IF([test $HAVE_PYTHON = no])
26    AT_XFAIL_IF([$PYTHON -c "exit(len(u'\U00010800'))"; test $? -ne 1])
27    AT_CHECK([printf %s "AS_ESCAPE([$2])" > input])
28    AT_CAPTURE_FILE([input])
29    AT_CHECK([$PYTHON $srcdir/test-json.py $4 input], [0], [stdout], [])
30    AT_CHECK([cat stdout], [0], [$3
31 ])
32    AT_CLEANUP])
33
34 m4_define([JSON_CHECK_POSITIVE],
35   [JSON_CHECK_POSITIVE_C([$1 - C], [$2], [$3], [$4])
36    JSON_CHECK_POSITIVE_PY([$1 - Python], [$2], [$3], [$4])])
37
38 m4_define([JSON_CHECK_NEGATIVE_C],
39   [AT_SETUP([$1])
40    AT_KEYWORDS([json negative])
41    AT_CHECK([printf %s "AS_ESCAPE([$2])" > input])
42    AT_CAPTURE_FILE([input])
43    AT_CHECK([ovstest test-json $4 input], [1], [stdout], [])
44    AT_CHECK([[sed 's/^error: [^:]*:/error:/' < stdout]], [0], [$3
45 ])
46    AT_CLEANUP])
47
48 m4_define([JSON_CHECK_NEGATIVE_PY], 
49   [AT_SETUP([$1])
50    AT_KEYWORDS([json negative Python])
51    AT_SKIP_IF([test $HAVE_PYTHON = no])
52    AT_CHECK([printf %s "AS_ESCAPE([$2])" > input])
53    AT_CAPTURE_FILE([input])
54    AT_CHECK([$PYTHON $srcdir/test-json.py $4 input], [1], [stdout], [])
55    AT_CHECK([[sed 's/^error: [^:]*:/error:/' < stdout]], [0], [$3
56 ])
57    AT_CLEANUP])
58
59 m4_define([JSON_CHECK_NEGATIVE],
60   [JSON_CHECK_NEGATIVE_C([$1 - C], [$2], [$3], [$4])
61    JSON_CHECK_NEGATIVE_PY([$1 - Python], [$2], [$3], [$4])])
62
63 AT_BANNER([JSON -- arrays])
64
65 JSON_CHECK_POSITIVE([empty array], [[ [   ] ]], [[[]]])
66 JSON_CHECK_POSITIVE([single-element array], [[ [ 1 ] ]], [[[1]]])
67 JSON_CHECK_POSITIVE([2-element array], [[ [ 1, 2 ] ]], [[[1,2]]])
68 JSON_CHECK_POSITIVE([many-element array],
69                     [[ [ 1, 2, 3, 4, 5 ] ]],
70                     [[[1,2,3,4,5]]])
71 JSON_CHECK_NEGATIVE([missing comma], [[ [ 1, 2, 3 4, 5 ] ]],
72                     [error: syntax error expecting '@:>@' or ','])
73 JSON_CHECK_NEGATIVE([trailing comma not allowed], 
74                     [[[1,2,]]], [error: syntax error expecting value])
75 JSON_CHECK_NEGATIVE([doubled comma not allowed], 
76                     [[[1,,2]]], [error: syntax error expecting value])
77
78 AT_BANNER([JSON -- strings])
79
80 JSON_CHECK_POSITIVE([empty string], [[[ "" ]]], [[[""]]])
81 JSON_CHECK_POSITIVE([1-character strings], 
82                     [[[ "a", "b", "c" ]]],
83                     [[["a","b","c"]]])
84 JSON_CHECK_POSITIVE([escape sequences], 
85   [[[ " \" \\ \/ \b \f \n \r \t" ]]],
86   [[[" \" \\ / \b \f \n \r \t"]]])
87 JSON_CHECK_POSITIVE([Unicode escape sequences], 
88   [[[ " \u0022 \u005c \u002F \u0008 \u000c \u000A \u000d \u0009" ]]],
89   [[[" \" \\ / \b \f \n \r \t"]]])
90 JSON_CHECK_POSITIVE_C([surrogate pairs - C],
91   [[["\ud834\udd1e"]]],
92   [[["𝄞"]]])
93 JSON_CHECK_POSITIVE_UCS4PY([surrogate pairs - Python],
94   [[["\ud834\udd1e"]]],
95   [[["𝄞"]]])
96 JSON_CHECK_NEGATIVE([a string by itself is not valid JSON], ["xxx"],
97                     [error: syntax error at beginning of input])
98 JSON_CHECK_NEGATIVE([end of line in quoted string],
99                     [[["xxx
100 "]]],
101                     [error: U+000A must be escaped in quoted string])
102 JSON_CHECK_NEGATIVE([formfeed in quoted string],
103                     [[["xxx\f"]]],
104                     [error: U+000C must be escaped in quoted string])
105 JSON_CHECK_NEGATIVE([bad escape in quoted string],
106                     [[["\x12"]]],
107                     [error: bad escape \x])
108 JSON_CHECK_NEGATIVE([\u must be followed by 4 hex digits (1)],
109                     [[["\u1x"]]],
110                     [error: quoted string ends within \u escape])
111 JSON_CHECK_NEGATIVE([\u must be followed by 4 hex digits (2)],
112                     [[["\u1xyz"]]],
113                     [error: malformed \u escape])
114 JSON_CHECK_NEGATIVE([isolated leading surrogate not allowed],
115                     [[["\ud834xxx"]]],
116                     [error: malformed escaped surrogate pair])
117 JSON_CHECK_NEGATIVE([surrogatess must paired properly],
118                     [[["\ud834\u1234"]]],
119                     [error: second half of escaped surrogate pair is not trailing surrogate])
120 JSON_CHECK_NEGATIVE([null bytes not allowed], 
121                     [[["\u0000"]]], 
122                     [error: null bytes not supported in quoted strings])
123 dnl Check for regression against a prior bug.
124 JSON_CHECK_POSITIVE([properly quoted backslash at end of string],
125   [[["\\"]]],
126   [[["\\"]]])
127 JSON_CHECK_NEGATIVE([stray backslash at end of string],
128   [[["abcd\"]]],
129   [error: unexpected end of input in quoted string])
130
131 AT_SETUP([end of input in quoted string - C])
132 AT_KEYWORDS([json negative])
133 AT_CHECK([printf '"xxx' | ovstest test-json -], [1],
134   [error: line 0, column 4, byte 4: unexpected end of input in quoted string
135 ])
136 AT_CLEANUP
137
138 AT_SETUP([end of input in quoted string - Python])
139 AT_KEYWORDS([json negative Python])
140 AT_SKIP_IF([test $HAVE_PYTHON = no])
141 AT_CHECK([printf '"xxx' > input
142 $PYTHON $srcdir/test-json.py input], [1],
143   [error: line 0, column 4, byte 4: unexpected end of input in quoted string
144 ])
145 AT_CLEANUP
146
147 AT_BANNER([JSON -- objects])
148
149 JSON_CHECK_POSITIVE([empty object], [[{ }]], [[{}]])
150 JSON_CHECK_POSITIVE([simple object],
151                     [[{"b": 2, "a": 1, "c": 3}]],
152                     [[{"a":1,"b":2,"c":3}]])
153 JSON_CHECK_NEGATIVE([bad value], [[{"a": }, "b": 2]], 
154                     [error: syntax error expecting value])
155 JSON_CHECK_NEGATIVE([missing colon], [[{"b": 2, "a" 1, "c": 3}]],
156                     [error: syntax error parsing object expecting ':'])
157 JSON_CHECK_NEGATIVE([missing comma], [[{"b": 2 "a" 1, "c": 3}]],
158                     [error: syntax error expecting '}' or ','])
159 JSON_CHECK_NEGATIVE([trailing comma not allowed],
160                     [[{"b": 2, "a": 1, "c": 3, }]],
161                     [[error: syntax error parsing object expecting string]])
162 JSON_CHECK_NEGATIVE([doubled comma not allowed],
163                     [[{"b": 2, "a": 1,, "c": 3}]],
164                     [[error: syntax error parsing object expecting string]])
165 JSON_CHECK_NEGATIVE([names must be strings],
166                     [[{1: 2}]],
167                     [[error: syntax error parsing object expecting string]])
168
169 AT_BANNER([JSON -- literal names])
170
171 JSON_CHECK_POSITIVE([null], [[[ null ]]], [[[null]]])
172 JSON_CHECK_POSITIVE([false], [[[ false ]]], [[[false]]])
173 JSON_CHECK_POSITIVE([true], [[[ true ]]], [[[true]]])
174 JSON_CHECK_NEGATIVE([a literal by itself is not valid JSON], [null],
175                     [error: syntax error at beginning of input])
176 JSON_CHECK_NEGATIVE([nullify is invalid], [[[ nullify ]]], 
177                     [error: invalid keyword 'nullify'])
178 JSON_CHECK_NEGATIVE([nubs is invalid], [[[ nubs ]]],
179                     [error: invalid keyword 'nubs'])
180 JSON_CHECK_NEGATIVE([xxx is invalid], [[[ xxx ]]], 
181                     [error: invalid keyword 'xxx'])
182
183 AT_BANNER([JSON -- numbers])
184
185 JSON_CHECK_POSITIVE(
186   [integers expressed as reals],
187   [[[1.0000000000,
188      2.00000000000000000000000000000000000,
189      2e5,
190      2.1234e4,
191      2.1230e3,
192      0e-10000,
193      0e10000]]],
194   [[[1,2,200000,21234,2123,0,0]]])
195 JSON_CHECK_POSITIVE(
196   [large integers], 
197   [[[9223372036854775807, -9223372036854775808]]],
198   [[[9223372036854775807,-9223372036854775808]]])
199 JSON_CHECK_POSITIVE(
200   [large integers expressed as reals], 
201   [[[9223372036854775807.0, -9223372036854775808.0,
202      92233720.36854775807e11, -9.223372036854775808e18]]],
203   [[[9223372036854775807,-9223372036854775808,9223372036854775807,-9223372036854775808]]])
204 # It seems likely that the following test will fail on some system that
205 # rounds slightly differently in arithmetic or in printf, but I'd like
206 # to keep it this way until we run into such a system.
207 JSON_CHECK_POSITIVE(
208   [large integers that overflow to reals], 
209   [[[9223372036854775807000, -92233720368547758080000]]],
210   [[[9.22337203685478e+21,-9.22337203685478e+22]]])
211
212 JSON_CHECK_POSITIVE(
213   [negative zero],
214   [[[-0, -0.0, 1e-9999, -1e-9999]]],
215   [[[0,0,0,0]]])
216
217 JSON_CHECK_POSITIVE(
218   [reals], 
219   [[[0.0, 1.0, 2.0, 3.0, 3.5, 81.250]]],
220   [[[0,1,2,3,3.5,81.25]]])
221 JSON_CHECK_POSITIVE(
222   [scientific notation],
223   [[[1e3, 1E3, 2.5E2, 1e+3, 125e-3, 3.125e-2, 3125e-05, 1.525878906e-5]]],
224   [[[1000,1000,250,1000,0.125,0.03125,0.03125,1.525878906e-05]]])
225 # It seems likely that the following test will fail on some system that
226 # rounds slightly differently in arithmetic or in printf, but I'd like
227 # to keep it this way until we run into such a system.
228 JSON_CHECK_POSITIVE(
229   [+/- DBL_MAX],
230   [[[1.7976931348623157e+308, -1.7976931348623157e+308]]],
231   [[[1.79769313486232e+308,-1.79769313486232e+308]]])
232
233 JSON_CHECK_POSITIVE(
234   [negative reals], 
235   [[[-0, -1.0, -2.0, -3.0, -3.5, -8.1250]]],
236   [[[0,-1,-2,-3,-3.5,-8.125]]])
237 JSON_CHECK_POSITIVE(
238   [negative scientific notation],
239   [[[-1e3, -1E3, -2.5E2, -1e+3, -125e-3, -3.125e-2, -3125e-05, -1.525878906e-5]]],
240   [[[-1000,-1000,-250,-1000,-0.125,-0.03125,-0.03125,-1.525878906e-05]]])
241 JSON_CHECK_POSITIVE(
242   [1e-9999 underflows to 0],
243   [[[1e-9999]]],
244   [[[0]]])
245 JSON_CHECK_NEGATIVE([a number by itself is not valid JSON], [1],
246                     [error: syntax error at beginning of input])
247 JSON_CHECK_NEGATIVE(
248   [leading zeros not allowed],
249   [[[0123]]],
250   [error: leading zeros not allowed])
251 JSON_CHECK_NEGATIVE(
252   [1e9999 is too big],
253   [[[1e9999]]],
254   [error: number outside valid range])
255 JSON_CHECK_NEGATIVE(
256   [exponent bigger than INT_MAX],
257   [[[1e9999999999999999999]]],
258   [error: exponent outside valid range])
259 JSON_CHECK_NEGATIVE(
260   [decimal point must be followed by digit],
261   [[[1.]]],
262   [error: decimal point must be followed by digit])
263 JSON_CHECK_NEGATIVE(
264   [exponent must contain at least one digit (1)],
265   [[[1e]]],
266   [error: exponent must contain at least one digit])
267 JSON_CHECK_NEGATIVE(
268   [exponent must contain at least one digit (2)],
269   [[[1e+]]],
270   [error: exponent must contain at least one digit])
271 JSON_CHECK_NEGATIVE(
272   [exponent must contain at least one digit (3)],
273   [[[1e-]]],
274   [error: exponent must contain at least one digit])
275
276 AT_BANNER([JSON -- RFC 4627 examples])
277
278 JSON_CHECK_POSITIVE([RFC 4267 object example],
279 [[{
280    "Image": {
281        "Width":  800,
282        "Height": 600,
283        "Title":  "View from 15th Floor",
284        "Thumbnail": {
285            "Url":    "http://www.example.com/image/481989943",
286            "Height": 125,
287            "Width":  "100"
288        },
289        "IDs": [116, 943, 234, 38793]
290      }
291 }]],
292 [[{"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}}]])
293
294 JSON_CHECK_POSITIVE([RFC 4267 array example],
295 [[[
296    {
297       "precision": "zip",
298       "Latitude":  37.7668,
299       "Longitude": -122.3959,
300       "Address":   "",
301       "City":      "SAN FRANCISCO",
302       "State":     "CA",
303       "Zip":       "94107",
304       "Country":   "US"
305    },
306    {
307       "precision": "zip",
308       "Latitude":  37.371991,
309       "Longitude": -122.026020,
310       "Address":   "",
311       "City":      "SUNNYVALE",
312       "State":     "CA",
313       "Zip":       "94085",
314       "Country":   "US"
315    }
316 ]]],
317 [[[{"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"}]]])
318
319 AT_BANNER([JSON -- pathological cases])
320
321 JSON_CHECK_NEGATIVE([trailing garbage], [[[1]null]],
322                     [error: trailing garbage at end of input])
323 JSON_CHECK_NEGATIVE([formfeeds are not valid white space],
324                     [[[\f]]], [error: invalid character U+000c])
325 JSON_CHECK_NEGATIVE([';' is not a valid token],
326                     [;], [error: invalid character ';'])
327 JSON_CHECK_NEGATIVE([arrays nesting too deep],
328                     [m4_for([i], [0], [1002], [1], [@<:@])dnl
329                      m4_for([i], [0], [1002], [1], [@:>@])],
330                     [error: input exceeds maximum nesting depth 1000])
331 JSON_CHECK_NEGATIVE([objects nesting too deep],
332                     [m4_for([i], [0], [1002], [1], [{"x":])dnl
333                      m4_for([i], [0], [1002], [1], [}])],
334                     [error: input exceeds maximum nesting depth 1000])
335
336 AT_SETUP([input may not be empty])
337 AT_KEYWORDS([json negative])
338 AT_CHECK([ovstest test-json /dev/null], [1], [error: line 0, column 0, byte 0: empty input stream
339 ])
340 AT_CLEANUP
341
342 AT_BANNER([JSON -- multiple inputs])
343
344 JSON_CHECK_POSITIVE([multiple adjacent objects], [[{}{}{}]], [[{}
345 {}
346 {}]],
347   [--multiple])
348
349 JSON_CHECK_POSITIVE([multiple space-separated objects], [[{}  {}  {}]], [[{}
350 {}
351 {}]],
352   [--multiple])
353
354 JSON_CHECK_POSITIVE([multiple objects on separate lines], [[{}
355 {}
356 {}]], [[{}
357 {}
358 {}]],
359   [--multiple])
360
361 JSON_CHECK_POSITIVE([multiple objects and arrays], [[{}[]{}[]]], [[{}
362 []
363 {}
364 []]],
365   [--multiple])
366
367 JSON_CHECK_NEGATIVE([garbage between multiple objects], [[{}x{}]], [[{}
368 error: invalid keyword 'x'
369 {}]], [--multiple])
370
371 JSON_CHECK_NEGATIVE([garbage after multiple objects], [[{}{}x]], [[{}
372 {}
373 error: invalid keyword 'x']], [--multiple])