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