Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.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
124 AT_SETUP([end of input in quoted string - C])
125 AT_KEYWORDS([json negative])
126 AT_CHECK([printf '"xxx' | ovstest test-json -], [1],
127   [error: line 0, column 4, byte 4: unexpected end of input in quoted string
128 ])
129 AT_CLEANUP
130
131 AT_SETUP([end of input in quoted string - Python])
132 AT_KEYWORDS([json negative Python])
133 AT_SKIP_IF([test $HAVE_PYTHON = no])
134 AT_CHECK([printf '"xxx' > input
135 $PYTHON $srcdir/test-json.py input], [1],
136   [error: line 0, column 4, byte 4: unexpected end of input in quoted string
137 ])
138 AT_CLEANUP
139
140 AT_BANNER([JSON -- objects])
141
142 JSON_CHECK_POSITIVE([empty object], [[{ }]], [[{}]])
143 JSON_CHECK_POSITIVE([simple object],
144                     [[{"b": 2, "a": 1, "c": 3}]],
145                     [[{"a":1,"b":2,"c":3}]])
146 JSON_CHECK_NEGATIVE([bad value], [[{"a": }, "b": 2]], 
147                     [error: syntax error expecting value])
148 JSON_CHECK_NEGATIVE([missing colon], [[{"b": 2, "a" 1, "c": 3}]],
149                     [error: syntax error parsing object expecting ':'])
150 JSON_CHECK_NEGATIVE([missing comma], [[{"b": 2 "a" 1, "c": 3}]],
151                     [error: syntax error expecting '}' or ','])
152 JSON_CHECK_NEGATIVE([trailing comma not allowed],
153                     [[{"b": 2, "a": 1, "c": 3, }]],
154                     [[error: syntax error parsing object expecting string]])
155 JSON_CHECK_NEGATIVE([doubled comma not allowed],
156                     [[{"b": 2, "a": 1,, "c": 3}]],
157                     [[error: syntax error parsing object expecting string]])
158 JSON_CHECK_NEGATIVE([names must be strings],
159                     [[{1: 2}]],
160                     [[error: syntax error parsing object expecting string]])
161
162 AT_BANNER([JSON -- literal names])
163
164 JSON_CHECK_POSITIVE([null], [[[ null ]]], [[[null]]])
165 JSON_CHECK_POSITIVE([false], [[[ false ]]], [[[false]]])
166 JSON_CHECK_POSITIVE([true], [[[ true ]]], [[[true]]])
167 JSON_CHECK_NEGATIVE([a literal by itself is not valid JSON], [null],
168                     [error: syntax error at beginning of input])
169 JSON_CHECK_NEGATIVE([nullify is invalid], [[[ nullify ]]], 
170                     [error: invalid keyword 'nullify'])
171 JSON_CHECK_NEGATIVE([nubs is invalid], [[[ nubs ]]],
172                     [error: invalid keyword 'nubs'])
173 JSON_CHECK_NEGATIVE([xxx is invalid], [[[ xxx ]]], 
174                     [error: invalid keyword 'xxx'])
175
176 AT_BANNER([JSON -- numbers])
177
178 JSON_CHECK_POSITIVE(
179   [integers expressed as reals],
180   [[[1.0000000000,
181      2.00000000000000000000000000000000000,
182      2e5,
183      2.1234e4,
184      2.1230e3,
185      0e-10000,
186      0e10000]]],
187   [[[1,2,200000,21234,2123,0,0]]])
188 JSON_CHECK_POSITIVE(
189   [large integers], 
190   [[[9223372036854775807, -9223372036854775808]]],
191   [[[9223372036854775807,-9223372036854775808]]])
192 JSON_CHECK_POSITIVE(
193   [large integers expressed as reals], 
194   [[[9223372036854775807.0, -9223372036854775808.0,
195      92233720.36854775807e11, -9.223372036854775808e18]]],
196   [[[9223372036854775807,-9223372036854775808,9223372036854775807,-9223372036854775808]]])
197 # It seems likely that the following test will fail on some system that
198 # rounds slightly differently in arithmetic or in printf, but I'd like
199 # to keep it this way until we run into such a system.
200 JSON_CHECK_POSITIVE(
201   [large integers that overflow to reals], 
202   [[[9223372036854775807000, -92233720368547758080000]]],
203   [[[9.22337203685478e+21,-9.22337203685478e+22]]])
204
205 JSON_CHECK_POSITIVE(
206   [negative zero],
207   [[[-0, -0.0, 1e-9999, -1e-9999]]],
208   [[[0,0,0,0]]])
209
210 JSON_CHECK_POSITIVE(
211   [reals], 
212   [[[0.0, 1.0, 2.0, 3.0, 3.5, 81.250]]],
213   [[[0,1,2,3,3.5,81.25]]])
214 JSON_CHECK_POSITIVE(
215   [scientific notation],
216   [[[1e3, 1E3, 2.5E2, 1e+3, 125e-3, 3.125e-2, 3125e-05, 1.525878906e-5]]],
217   [[[1000,1000,250,1000,0.125,0.03125,0.03125,1.525878906e-05]]])
218 # It seems likely that the following test will fail on some system that
219 # rounds slightly differently in arithmetic or in printf, but I'd like
220 # to keep it this way until we run into such a system.
221 JSON_CHECK_POSITIVE(
222   [+/- DBL_MAX],
223   [[[1.7976931348623157e+308, -1.7976931348623157e+308]]],
224   [[[1.79769313486232e+308,-1.79769313486232e+308]]])
225
226 JSON_CHECK_POSITIVE(
227   [negative reals], 
228   [[[-0, -1.0, -2.0, -3.0, -3.5, -8.1250]]],
229   [[[0,-1,-2,-3,-3.5,-8.125]]])
230 JSON_CHECK_POSITIVE(
231   [negative scientific notation],
232   [[[-1e3, -1E3, -2.5E2, -1e+3, -125e-3, -3.125e-2, -3125e-05, -1.525878906e-5]]],
233   [[[-1000,-1000,-250,-1000,-0.125,-0.03125,-0.03125,-1.525878906e-05]]])
234 JSON_CHECK_POSITIVE(
235   [1e-9999 underflows to 0],
236   [[[1e-9999]]],
237   [[[0]]])
238 JSON_CHECK_NEGATIVE([a number by itself is not valid JSON], [1],
239                     [error: syntax error at beginning of input])
240 JSON_CHECK_NEGATIVE(
241   [leading zeros not allowed],
242   [[[0123]]],
243   [error: leading zeros not allowed])
244 JSON_CHECK_NEGATIVE(
245   [1e9999 is too big],
246   [[[1e9999]]],
247   [error: number outside valid range])
248 JSON_CHECK_NEGATIVE(
249   [exponent bigger than INT_MAX],
250   [[[1e9999999999999999999]]],
251   [error: exponent outside valid range])
252 JSON_CHECK_NEGATIVE(
253   [decimal point must be followed by digit],
254   [[[1.]]],
255   [error: decimal point must be followed by digit])
256 JSON_CHECK_NEGATIVE(
257   [exponent must contain at least one digit (1)],
258   [[[1e]]],
259   [error: exponent must contain at least one digit])
260 JSON_CHECK_NEGATIVE(
261   [exponent must contain at least one digit (2)],
262   [[[1e+]]],
263   [error: exponent must contain at least one digit])
264 JSON_CHECK_NEGATIVE(
265   [exponent must contain at least one digit (3)],
266   [[[1e-]]],
267   [error: exponent must contain at least one digit])
268
269 AT_BANNER([JSON -- RFC 4627 examples])
270
271 JSON_CHECK_POSITIVE([RFC 4267 object example],
272 [[{
273    "Image": {
274        "Width":  800,
275        "Height": 600,
276        "Title":  "View from 15th Floor",
277        "Thumbnail": {
278            "Url":    "http://www.example.com/image/481989943",
279            "Height": 125,
280            "Width":  "100"
281        },
282        "IDs": [116, 943, 234, 38793]
283      }
284 }]],
285 [[{"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}}]])
286
287 JSON_CHECK_POSITIVE([RFC 4267 array example],
288 [[[
289    {
290       "precision": "zip",
291       "Latitude":  37.7668,
292       "Longitude": -122.3959,
293       "Address":   "",
294       "City":      "SAN FRANCISCO",
295       "State":     "CA",
296       "Zip":       "94107",
297       "Country":   "US"
298    },
299    {
300       "precision": "zip",
301       "Latitude":  37.371991,
302       "Longitude": -122.026020,
303       "Address":   "",
304       "City":      "SUNNYVALE",
305       "State":     "CA",
306       "Zip":       "94085",
307       "Country":   "US"
308    }
309 ]]],
310 [[[{"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"}]]])
311
312 AT_BANNER([JSON -- pathological cases])
313
314 JSON_CHECK_NEGATIVE([trailing garbage], [[[1]null]],
315                     [error: trailing garbage at end of input])
316 JSON_CHECK_NEGATIVE([formfeeds are not valid white space],
317                     [[[\f]]], [error: invalid character U+000c])
318 JSON_CHECK_NEGATIVE([';' is not a valid token],
319                     [;], [error: invalid character ';'])
320 JSON_CHECK_NEGATIVE([arrays nesting too deep],
321                     [m4_for([i], [0], [1002], [1], [@<:@])dnl
322                      m4_for([i], [0], [1002], [1], [@:>@])],
323                     [error: input exceeds maximum nesting depth 1000])
324 JSON_CHECK_NEGATIVE([objects nesting too deep],
325                     [m4_for([i], [0], [1002], [1], [{"x":])dnl
326                      m4_for([i], [0], [1002], [1], [}])],
327                     [error: input exceeds maximum nesting depth 1000])
328
329 AT_SETUP([input may not be empty])
330 AT_KEYWORDS([json negative])
331 AT_CHECK([ovstest test-json /dev/null], [1], [error: line 0, column 0, byte 0: empty input stream
332 ])
333 AT_CLEANUP
334
335 AT_BANNER([JSON -- multiple inputs])
336
337 JSON_CHECK_POSITIVE([multiple adjacent objects], [[{}{}{}]], [[{}
338 {}
339 {}]],
340   [--multiple])
341
342 JSON_CHECK_POSITIVE([multiple space-separated objects], [[{}  {}  {}]], [[{}
343 {}
344 {}]],
345   [--multiple])
346
347 JSON_CHECK_POSITIVE([multiple objects on separate lines], [[{}
348 {}
349 {}]], [[{}
350 {}
351 {}]],
352   [--multiple])
353
354 JSON_CHECK_POSITIVE([multiple objects and arrays], [[{}[]{}[]]], [[{}
355 []
356 {}
357 []]],
358   [--multiple])
359
360 JSON_CHECK_NEGATIVE([garbage between multiple objects], [[{}x{}]], [[{}
361 error: invalid keyword 'x'
362 {}]], [--multiple])
363
364 JSON_CHECK_NEGATIVE([garbage after multiple objects], [[{}{}x]], [[{}
365 {}
366 error: invalid keyword 'x']], [--multiple])