json: Accurately parse very large real numbers.
[openvswitch] / tests / json.at
1 m4_define([JSON_CHECK_POSITIVE], 
2   [AT_SETUP([$1])
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
8 ])
9    AT_CLEANUP])
10
11 m4_define([JSON_CHECK_NEGATIVE], 
12   [AT_SETUP([$1])
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
18 ])
19    AT_CLEANUP])
20
21 AT_BANNER([JSON -- arrays])
22
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 ] ]],
28                     [[[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])
35
36 AT_BANNER([JSON -- strings])
37
38 JSON_CHECK_POSITIVE([empty string], [[[ "" ]]], [[[""]]])
39 JSON_CHECK_POSITIVE([1-character strings], 
40                     [[[ "a", "b", "c" ]]],
41                     [[["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],
49   [[["\ud834\udd1e"]]],
50   [[["𝄞"]]])
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],
54                     [[["xxx
55 "]]],
56                     [error: U+000A must be escaped in quoted string])
57 JSON_CHECK_NEGATIVE([formfeed in quoted string],
58                     [[["xxx\f"]]],
59                     [error: U+000C must be escaped in quoted string])
60 JSON_CHECK_NEGATIVE([bad escape in quoted string],
61                     [[["\x12"]]],
62                     [error: bad escape \x])
63 JSON_CHECK_NEGATIVE([\u must be followed by 4 hex digits],
64                     [[["\u1x"]]],
65                     [error: malformed \u escape])
66 JSON_CHECK_NEGATIVE([isolated leading surrogate not allowed],
67                     [[["\ud834xxx"]]],
68                     [error: malformed escaped surrogate pair])
69 JSON_CHECK_NEGATIVE([surrogatess must paired properly],
70                     [[["\ud834\u1234"]]],
71                     [error: second half of escaped surrogate pair is not trailing surrogate])
72 JSON_CHECK_NEGATIVE([null bytes not allowed], 
73                     [[["\u0000"]]], 
74                     [error: null bytes not supported in quoted strings])
75
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
80 ])
81 AT_CLEANUP
82
83 AT_BANNER([JSON -- objects])
84
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],
102                     [[{1: 2}]],
103                     [[error: syntax error parsing object expecting string]])
104
105 AT_BANNER([JSON -- literal names])
106
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'])
118
119 AT_BANNER([JSON -- numbers])
120
121 JSON_CHECK_POSITIVE(
122   [integers expressed as reals],
123   [[[1.0000000000,
124      2.00000000000000000000000000000000000,
125      2e5,
126      2.1234e4,
127      2.1230e3,
128      0e-10000,
129      0e10000]]],
130   [[[1,2,200000,21234,2123,0,0]]])
131 JSON_CHECK_POSITIVE(
132   [large integers], 
133   [[[9223372036854775807, -9223372036854775808]]],
134   [[[9223372036854775807,-9223372036854775808]]])
135 JSON_CHECK_POSITIVE(
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.
143 JSON_CHECK_POSITIVE(
144   [large integers that overflow to reals], 
145   [[[9223372036854775807000, -92233720368547758080000]]],
146   [[[9.22337203685478e+21,-9.22337203685478e+22]]])
147
148 JSON_CHECK_POSITIVE(
149   [negative zero],
150   [[[-0, -0.0, 1e-9999, -1e-9999]]],
151   [[[0,0,0,0]]])
152
153 JSON_CHECK_POSITIVE(
154   [reals], 
155   [[[0.0, 1.0, 2.0, 3.0, 3.5, 81.250]]],
156   [[[0,1,2,3,3.5,81.25]]])
157 JSON_CHECK_POSITIVE(
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]]])
161 # It seems likely that the following test will fail on some system that
162 # rounds slightly differently in arithmetic or in printf, but I'd like
163 # to keep it this way until we run into such a system.
164 JSON_CHECK_POSITIVE(
165   [+/- DBL_MAX],
166   [[[1.7976931348623157e+308, -1.7976931348623157e+308]]],
167   [[[1.79769313486232e+308,-1.79769313486232e+308]]])
168
169 JSON_CHECK_POSITIVE(
170   [negative reals], 
171   [[[-0, -1.0, -2.0, -3.0, -3.5, -8.1250]]],
172   [[[0,-1,-2,-3,-3.5,-8.125]]])
173 JSON_CHECK_POSITIVE(
174   [negative scientific notation],
175   [[[-1e3, -1E3, -2.5E2, -1e+3, -125e-3, -3.125e-2, -3125e-05, -1.525878906e-5]]],
176   [[[-1000,-1000,-250,-1000,-0.125,-0.03125,-0.03125,-1.525878906e-05]]])
177 JSON_CHECK_POSITIVE(
178   [1e-9999 underflows to 0],
179   [[[1e-9999]]],
180   [[[0]]])
181 JSON_CHECK_NEGATIVE([a number by itself is not valid JSON], [1],
182                     [error: syntax error at beginning of input])
183 JSON_CHECK_NEGATIVE(
184   [leading zeros not allowed],
185   [[[0123]]],
186   [error: leading zeros not allowed])
187 JSON_CHECK_NEGATIVE(
188   [1e9999 is too big],
189   [[[1e9999]]],
190   [error: number outside valid range])
191 JSON_CHECK_NEGATIVE(
192   [exponent bigger than INT_MAX],
193   [[[1e9999999999999999999]]],
194   [error: exponent outside valid range])
195 JSON_CHECK_NEGATIVE(
196   [decimal point must be followed by digit],
197   [[[1.]]],
198   [error: decimal point must be followed by digit])
199 JSON_CHECK_NEGATIVE(
200   [exponent must contain at least one digit (1)],
201   [[[1e]]],
202   [error: exponent must contain at least one digit])
203 JSON_CHECK_NEGATIVE(
204   [exponent must contain at least one digit (2)],
205   [[[1e+]]],
206   [error: exponent must contain at least one digit])
207 JSON_CHECK_NEGATIVE(
208   [exponent must contain at least one digit (3)],
209   [[[1e-]]],
210   [error: exponent must contain at least one digit])
211
212 AT_BANNER([JSON -- RFC 4627 examples])
213
214 JSON_CHECK_POSITIVE([RFC 4267 object example],
215 [[{
216    "Image": {
217        "Width":  800,
218        "Height": 600,
219        "Title":  "View from 15th Floor",
220        "Thumbnail": {
221            "Url":    "http://www.example.com/image/481989943",
222            "Height": 125,
223            "Width":  "100"
224        },
225        "IDs": [116, 943, 234, 38793]
226      }
227 }]],
228 [[{"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}}]])
229
230 JSON_CHECK_POSITIVE([RFC 4267 array example],
231 [[[
232    {
233       "precision": "zip",
234       "Latitude":  37.7668,
235       "Longitude": -122.3959,
236       "Address":   "",
237       "City":      "SAN FRANCISCO",
238       "State":     "CA",
239       "Zip":       "94107",
240       "Country":   "US"
241    },
242    {
243       "precision": "zip",
244       "Latitude":  37.371991,
245       "Longitude": -122.026020,
246       "Address":   "",
247       "City":      "SUNNYVALE",
248       "State":     "CA",
249       "Zip":       "94085",
250       "Country":   "US"
251    }
252 ]]],
253 [[[{"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"}]]])
254
255 AT_BANNER([JSON -- pathological cases])
256
257 JSON_CHECK_NEGATIVE([trailing garbage], [[[1]null]],
258                     [error: trailing garbage at end of input])
259 JSON_CHECK_NEGATIVE([formfeeds are not valid white space],
260                     [[[\f]]], [error: invalid character U+000c])
261 JSON_CHECK_NEGATIVE([';' is not a valid token],
262                     [;], [error: invalid character ';'])
263 JSON_CHECK_NEGATIVE([arrays nesting too deep],
264                     [m4_for([i], [0], [1002], [1], [@<:@])dnl
265                      m4_for([i], [0], [1002], [1], [@:>@])],
266                     [error: input exceeds maximum nesting depth 1000])
267 JSON_CHECK_NEGATIVE([objects nesting too deep],
268                     [m4_for([i], [0], [1002], [1], [{"x":])dnl
269                      m4_for([i], [0], [1002], [1], [}])],
270                     [error: input exceeds maximum nesting depth 1000])
271
272 AT_SETUP([input may not be empty])
273 AT_KEYWORDS([json negative])
274 AT_CHECK([test-json /dev/null], [1], [error: line 0, column 0, byte 0: empty input stream
275 ])
276 AT_CLEANUP
277
278 AT_BANNER([JSON -- multiple inputs])
279
280 JSON_CHECK_POSITIVE([multiple adjacent objects], [[{}{}{}]], [[{}
281 {}
282 {}]],
283   [--multiple])
284
285 JSON_CHECK_POSITIVE([multiple space-separated objects], [[{}  {}  {}]], [[{}
286 {}
287 {}]],
288   [--multiple])
289
290 JSON_CHECK_POSITIVE([multiple objects on separate lines], [[{}
291 {}
292 {}]], [[{}
293 {}
294 {}]],
295   [--multiple])
296
297 JSON_CHECK_POSITIVE([multiple objects and arrays], [[{}[]{}[]]], [[{}
298 []
299 {}
300 []]],
301   [--multiple])
302
303 JSON_CHECK_NEGATIVE([garbage between multiple objects], [[{}x{}]], [[{}
304 error: invalid keyword 'x'
305 {}]], [--multiple])
306
307 JSON_CHECK_NEGATIVE([garbage after multiple objects], [[{}{}x]], [[{}
308 {}
309 error: invalid keyword 'x']], [--multiple])