summaryrefslogtreecommitdiff
path: root/tests/json.at
blob: 527ce8e6403305109d5d70cf1568e68c3063d6f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
m4_define([JSON_CHECK_POSITIVE_C],
  [AT_SETUP([$1])
   AT_KEYWORDS([json positive])
   AT_CHECK([printf %s "AS_ESCAPE([$2])" > input])
   AT_CAPTURE_FILE([input])
   AT_CHECK([ovstest test-json $4 input], [0], [stdout], [])
   AT_CHECK([cat stdout], [0], [$3
])
   AT_CLEANUP])

# JSON_CHECK_POSITIVE_PY(TITLE, INPUT, OUTPUT, TEST-JSON-ARGS)
#
m4_define([JSON_CHECK_POSITIVE_PY],
  [AT_SETUP([$1])
   AT_KEYWORDS([json positive Python])
   AT_CHECK([printf %s "AS_ESCAPE([$2])" > input])
   AT_CAPTURE_FILE([input])
   AT_CHECK([$PYTHON3 $srcdir/test-json.py $4 input], [0], [stdout], [])
   AT_CHECK([cat stdout], [0], [$3
])
   AT_CLEANUP])

m4_define([JSON_CHECK_POSITIVE],
  [JSON_CHECK_POSITIVE_C([$1 - C], [$2], [$3], [$4])
   JSON_CHECK_POSITIVE_PY([$1 - Python3], [$2], [$3], [$4])])

m4_define([JSON_CHECK_NEGATIVE_C],
  [AT_SETUP([$1])
   AT_KEYWORDS([json negative])
   AT_CHECK([printf %s "AS_ESCAPE([$2])" > input])
   AT_CAPTURE_FILE([input])
   AT_CHECK([ovstest test-json $4 input], [1], [stdout], [])
   AT_CHECK([[sed 's/^error: [^:]*:/error:/' < stdout]], [0], [$3
])
   AT_CLEANUP])

# JSON_CHECK_NEGATIVE_PY(TITLE, INPUT, OUTPUT, TEST-JSON-ARGS)
#
m4_define([JSON_CHECK_NEGATIVE_PY], 
  [AT_SETUP([$1])
   AT_KEYWORDS([json negative Python])
   AT_CHECK([printf %s "AS_ESCAPE([$2])" > input])
   AT_CAPTURE_FILE([input])
   AT_CHECK([$PYTHON3 $srcdir/test-json.py $4 input], [1], [stdout], [])
   AT_CHECK([[sed 's/^error: [^:]*:/error:/' < stdout]], [0], [$3
])
   AT_CLEANUP])

m4_define([JSON_CHECK_NEGATIVE],
  [JSON_CHECK_NEGATIVE_C([$1 - C], [$2], [$3], [$4])
   JSON_CHECK_NEGATIVE_PY([$1 - Python3], [$2], [$3], [$4])])

AT_BANNER([JSON -- arrays])

JSON_CHECK_POSITIVE([empty array], [[ [   ] ]], [[[]]])
JSON_CHECK_POSITIVE([single-element array], [[ [ 1 ] ]], [[[1]]])
JSON_CHECK_POSITIVE([2-element array], [[ [ 1, 2 ] ]], [[[1,2]]])
JSON_CHECK_POSITIVE([many-element array],
                    [[ [ 1, 2, 3, 4, 5 ] ]],
                    [[[1,2,3,4,5]]])
JSON_CHECK_NEGATIVE([missing comma], [[ [ 1, 2, 3 4, 5 ] ]],
                    [error: syntax error expecting '@:>@' or ','])
JSON_CHECK_NEGATIVE([trailing comma not allowed], 
                    [[[1,2,]]], [error: syntax error expecting value])
JSON_CHECK_NEGATIVE([doubled comma not allowed], 
                    [[[1,,2]]], [error: syntax error expecting value])

AT_BANNER([JSON -- strings])

JSON_CHECK_POSITIVE([empty string], [[[ "" ]]], [[[""]]])
JSON_CHECK_POSITIVE([1-character strings], 
                    [[[ "a", "b", "c" ]]],
                    [[["a","b","c"]]])
JSON_CHECK_POSITIVE([escape sequences], 
  [[[ " \" \\ \/ \b \f \n \r \t" ]]],
  [[[" \" \\ / \b \f \n \r \t"]]])
JSON_CHECK_POSITIVE([Unicode escape sequences], 
  [[[ " \u0022 \u005c \u002F \u0008 \u000c \u000A \u000d \u0009" ]]],
  [[[" \" \\ / \b \f \n \r \t"]]])
JSON_CHECK_POSITIVE_C([surrogate pairs - C],
  [[["\ud834\udd1e"]]],
  [[["𝄞"]]])
JSON_CHECK_NEGATIVE([a string by itself is not valid JSON], ["xxx"],
                    [error: syntax error at beginning of input])
JSON_CHECK_NEGATIVE([end of line in quoted string],
                    [[["xxx
"]]],
                    [error: U+000A must be escaped in quoted string])
JSON_CHECK_NEGATIVE([formfeed in quoted string],
                    [[["xxx"]]],
                    [error: U+000C must be escaped in quoted string])
JSON_CHECK_NEGATIVE([bad escape in quoted string],
                    [[["\x12"]]],
                    [error: bad escape \x])
JSON_CHECK_NEGATIVE([\u must be followed by 4 hex digits (1)],
                    [[["\u1x"]]],
                    [error: quoted string ends within \u escape])
JSON_CHECK_NEGATIVE([\u must be followed by 4 hex digits (2)],
                    [[["\u1xyz"]]],
                    [error: malformed \u escape])
JSON_CHECK_NEGATIVE([isolated leading surrogate not allowed],
                    [[["\ud834xxx"]]],
                    [error: malformed escaped surrogate pair])
JSON_CHECK_NEGATIVE([surrogatess must paired properly],
                    [[["\ud834\u1234"]]],
                    [error: second half of escaped surrogate pair is not trailing surrogate])
JSON_CHECK_NEGATIVE([null bytes not allowed], 
                    [[["\u0000"]]], 
                    [error: null bytes not supported in quoted strings])
dnl Check for regression against a prior bug.
JSON_CHECK_POSITIVE([properly quoted backslash at end of string],
  [[["\\"]]],
  [[["\\"]]])
JSON_CHECK_NEGATIVE([stray backslash at end of string],
  [[["abcd\"]]],
  [error: unexpected end of input in quoted string])

AT_SETUP([end of input in quoted string - C])
AT_KEYWORDS([json negative])
AT_CHECK([printf '"xxx' | ovstest test-json -], [1],
  [error: line 0, column 4, byte 4: unexpected end of input in quoted string
])
AT_CLEANUP

AT_BANNER([JSON -- objects])

JSON_CHECK_POSITIVE([empty object], [[{ }]], [[{}]])
JSON_CHECK_POSITIVE([simple object],
                    [[{"b": 2, "a": 1, "c": 3}]],
                    [[{"a":1,"b":2,"c":3}]])
JSON_CHECK_NEGATIVE([bad value], [[{"a": }, "b": 2]], 
                    [error: syntax error expecting value])
JSON_CHECK_NEGATIVE([missing colon], [[{"b": 2, "a" 1, "c": 3}]],
                    [error: syntax error parsing object expecting ':'])
JSON_CHECK_NEGATIVE([missing comma], [[{"b": 2 "a" 1, "c": 3}]],
                    [error: syntax error expecting '}' or ','])
JSON_CHECK_NEGATIVE([trailing comma not allowed],
                    [[{"b": 2, "a": 1, "c": 3, }]],
                    [[error: syntax error parsing object expecting string]])
JSON_CHECK_NEGATIVE([doubled comma not allowed],
                    [[{"b": 2, "a": 1,, "c": 3}]],
                    [[error: syntax error parsing object expecting string]])
JSON_CHECK_NEGATIVE([names must be strings],
                    [[{1: 2}]],
                    [[error: syntax error parsing object expecting string]])

AT_BANNER([JSON -- literal names])

JSON_CHECK_POSITIVE([null], [[[ null ]]], [[[null]]])
JSON_CHECK_POSITIVE([false], [[[ false ]]], [[[false]]])
JSON_CHECK_POSITIVE([true], [[[ true ]]], [[[true]]])
JSON_CHECK_NEGATIVE([a literal by itself is not valid JSON], [null],
                    [error: syntax error at beginning of input])
JSON_CHECK_NEGATIVE([nullify is invalid], [[[ nullify ]]], 
                    [error: invalid keyword 'nullify'])
JSON_CHECK_NEGATIVE([nubs is invalid], [[[ nubs ]]],
                    [error: invalid keyword 'nubs'])
JSON_CHECK_NEGATIVE([xxx is invalid], [[[ xxx ]]], 
                    [error: invalid keyword 'xxx'])

AT_BANNER([JSON -- numbers])

JSON_CHECK_POSITIVE(
  [integers expressed as reals],
  [[[1.0000000000,
     2.00000000000000000000000000000000000,
     2e5,
     2.1234e4,
     2.1230e3,
     0e-10000,
     0e10000]]],
  [[[1,2,200000,21234,2123,0,0]]])
JSON_CHECK_POSITIVE(
  [large integers], 
  [[[9223372036854775807, -9223372036854775808]]],
  [[[9223372036854775807,-9223372036854775808]]])
JSON_CHECK_POSITIVE(
  [large integers expressed as reals], 
  [[[9223372036854775807.0, -9223372036854775808.0,
     92233720.36854775807e11, -9.223372036854775808e18]]],
  [[[9223372036854775807,-9223372036854775808,9223372036854775807,-9223372036854775808]]])
# It seems likely that the following test will fail on some system that
# rounds slightly differently in arithmetic or in printf, but I'd like
# to keep it this way until we run into such a system.
JSON_CHECK_POSITIVE_C(
  [C - large integers that overflow to reals],
  [[[9223372036854775807000, -92233720368547758080000]]],
  [[[9.22337203685478e+21,-9.22337203685478e+22]]])
JSON_CHECK_POSITIVE_PY(
  [large integers that overflow to reals],
  [[[9223372036854775807000, -92233720368547758080000]]],
  [[[9.223372036854776e+21,-9.223372036854776e+22]]])

JSON_CHECK_POSITIVE(
  [negative zero],
  [[[-0, -0.0, 1e-9999, -1e-9999]]],
  [[[0,0,0,0]]])

JSON_CHECK_POSITIVE(
  [reals], 
  [[[0.0, 1.0, 2.0, 3.0, 3.5, 81.250]]],
  [[[0,1,2,3,3.5,81.25]]])
JSON_CHECK_POSITIVE(
  [scientific notation],
  [[[1e3, 1E3, 2.5E2, 1e+3, 125e-3, 3.125e-2, 3125e-05, 1.525878906e-5]]],
  [[[1000,1000,250,1000,0.125,0.03125,0.03125,1.525878906e-05]]])
# It seems likely that the following test will fail on some system that
# rounds slightly differently in arithmetic or in printf, but I'd like
# to keep it this way until we run into such a system.
JSON_CHECK_POSITIVE_C(
  [C - +/- DBL_MAX],
  [[[1.7976931348623157e+308, -1.7976931348623157e+308]]],
  [[[1.79769313486232e+308,-1.79769313486232e+308]]])
JSON_CHECK_POSITIVE_PY(
  [+/- DBL_MAX],
  [[[1.7976931348623157e+308, -1.7976931348623157e+308]]],
  [[[1.7976931348623157e+308,-1.7976931348623157e+308]]])

JSON_CHECK_POSITIVE(
  [negative reals], 
  [[[-0, -1.0, -2.0, -3.0, -3.5, -8.1250]]],
  [[[0,-1,-2,-3,-3.5,-8.125]]])
JSON_CHECK_POSITIVE(
  [negative scientific notation],
  [[[-1e3, -1E3, -2.5E2, -1e+3, -125e-3, -3.125e-2, -3125e-05, -1.525878906e-5]]],
  [[[-1000,-1000,-250,-1000,-0.125,-0.03125,-0.03125,-1.525878906e-05]]])
JSON_CHECK_POSITIVE(
  [1e-9999 underflows to 0],
  [[[1e-9999]]],
  [[[0]]])
JSON_CHECK_NEGATIVE([a number by itself is not valid JSON], [1],
                    [error: syntax error at beginning of input])
JSON_CHECK_NEGATIVE(
  [leading zeros not allowed],
  [[[0123]]],
  [error: leading zeros not allowed])
JSON_CHECK_NEGATIVE(
  [1e9999 is too big],
  [[[1e9999]]],
  [error: number outside valid range])
JSON_CHECK_NEGATIVE_C(
  [exponent bigger than INT_MAX],
  [[[1e9999999999999999999]]],
  [error: exponent outside valid range])
JSON_CHECK_NEGATIVE_C(
  [exponent smaller than INT_MIN],
  [[[1e-9999999999999999999]]],
  [error: exponent outside valid range])
JSON_CHECK_NEGATIVE_C(
  [accumulated exponent bigger than INT_MAX],
  [[[340282366920938463461761716499e2147483647]]],
  [error: exponent outside valid range])
JSON_CHECK_NEGATIVE_C(
  [accumulated exponent smaller than INT_MIN],
  [[[0.340282366920938463461761716499e-2147483648]]],
  [error: exponent outside valid range])
JSON_CHECK_NEGATIVE(
  [decimal point must be followed by digit],
  [[[1.]]],
  [error: decimal point must be followed by digit])
JSON_CHECK_NEGATIVE(
  [exponent must contain at least one digit (1)],
  [[[1e]]],
  [error: exponent must contain at least one digit])
JSON_CHECK_NEGATIVE(
  [exponent must contain at least one digit (2)],
  [[[1e+]]],
  [error: exponent must contain at least one digit])
JSON_CHECK_NEGATIVE(
  [exponent must contain at least one digit (3)],
  [[[1e-]]],
  [error: exponent must contain at least one digit])

AT_BANNER([JSON -- RFC 4627 examples])

JSON_CHECK_POSITIVE([RFC 4267 object example],
[[{
   "Image": {
       "Width":  800,
       "Height": 600,
       "Title":  "View from 15th Floor",
       "Thumbnail": {
           "Url":    "http://www.example.com/image/481989943",
           "Height": 125,
           "Width":  "100"
       },
       "IDs": [116, 943, 234, 38793]
     }
}]],
[[{"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}}]])

JSON_CHECK_POSITIVE([RFC 4267 array example],
[[[
   {
      "precision": "zip",
      "Latitude":  37.7668,
      "Longitude": -122.3959,
      "Address":   "",
      "City":      "SAN FRANCISCO",
      "State":     "CA",
      "Zip":       "94107",
      "Country":   "US"
   },
   {
      "precision": "zip",
      "Latitude":  37.371991,
      "Longitude": -122.026020,
      "Address":   "",
      "City":      "SUNNYVALE",
      "State":     "CA",
      "Zip":       "94085",
      "Country":   "US"
   }
]]],
[[[{"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"}]]])

AT_BANNER([JSON -- pathological cases])

JSON_CHECK_NEGATIVE([trailing garbage], [[[1]null]],
                    [error: trailing garbage at end of input])
JSON_CHECK_NEGATIVE([formfeeds are not valid white space],
                    [[[]]], [error: invalid character U+000c])
JSON_CHECK_NEGATIVE([';' is not a valid token],
                    [;], [error: invalid character ';'])
JSON_CHECK_NEGATIVE([arrays nesting too deep],
                    [m4_for([i], [0], [1002], [1], [@<:@])dnl
                     m4_for([i], [0], [1002], [1], [@:>@])],
                    [error: input exceeds maximum nesting depth 1000])
JSON_CHECK_NEGATIVE([objects nesting too deep],
                    [m4_for([i], [0], [1002], [1], [{"x":])dnl
                     m4_for([i], [0], [1002], [1], [}])],
                    [error: input exceeds maximum nesting depth 1000])

AT_SETUP([input may not be empty])
AT_KEYWORDS([json negative])
AT_CHECK([ovstest test-json /dev/null], [1], [error: line 0, column 0, byte 0: empty input stream
])
AT_CLEANUP

AT_BANNER([JSON -- multiple inputs])

JSON_CHECK_POSITIVE([multiple adjacent objects], [[{}{}{}]], [[{}
{}
{}]],
  [--multiple])

JSON_CHECK_POSITIVE([multiple space-separated objects], [[{}  {}  {}]], [[{}
{}
{}]],
  [--multiple])

JSON_CHECK_POSITIVE([multiple objects on separate lines], [[{}
{}
{}]], [[{}
{}
{}]],
  [--multiple])

JSON_CHECK_POSITIVE([multiple objects and arrays], [[{}[]{}[]]], [[{}
[]
{}
[]]],
  [--multiple])

JSON_CHECK_NEGATIVE([garbage between multiple objects], [[{}x{}]], [[{}
error: invalid keyword 'x'
{}]], [--multiple])

JSON_CHECK_NEGATIVE([garbage after multiple objects], [[{}{}x]], [[{}
{}
error: invalid keyword 'x']], [--multiple])