-
Notifications
You must be signed in to change notification settings - Fork 20
/
json_test.cpp
467 lines (444 loc) · 15.6 KB
/
json_test.cpp
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 Mozilla Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "json.h"
#include <atomic>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#define ARRAYLEN(A) \
((sizeof(A) / sizeof(*(A))) / ((unsigned)!(sizeof(A) % sizeof(*(A)))))
#define STRING(sl) std::string(sl, sizeof(sl) - 1)
using jt::Json;
static const char kHuge[] = R"([
"JSON Test Pattern pass1",
{"object with 1 member":["array with 1 element"]},
{},
[],
-42,
true,
false,
null,
{
"integer": 1234567890,
"real": -9876.543210,
"e": 0.123456789e-12,
"E": 1.234567890E+34,
"": 23456789012E66,
"zero": 0,
"one": 1,
"space": " ",
"quote": "\"",
"backslash": "\\",
"controls": "\b\f\n\r\t",
"slash": "/ & \/",
"alpha": "abcdefghijklmnopqrstuvwyz",
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
"digit": "0123456789",
"0123456789": "digit",
"special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
"true": true,
"false": false,
"null": null,
"array":[ ],
"object":{ },
"address": "50 St. James Street",
"url": "http://www.JSON.org/",
"comment": "// /* <!-- --",
"# -- --> */": " ",
" s p a c e d " :[1,2 , 3
,
4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],
"jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
"quotes": "" \u0022 %22 0x22 034 "",
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
: "A key can be any string"
},
0.5 ,98.6
,
99.44
,
1066,
1e1,
0.1e1,
1e-1,
1e00,2e+00,2e-00
,"rosebud"])";
#define BENCH(ITERATIONS, WORK_PER_RUN, CODE) \
do { \
auto start = std::chrono::high_resolution_clock::now(); \
for (int __i = 0; __i < ITERATIONS; ++__i) { \
std::atomic_signal_fence(std::memory_order_acq_rel); \
CODE; \
} \
auto end = std::chrono::high_resolution_clock::now(); \
auto duration = \
std::chrono::duration_cast<std::chrono::nanoseconds>(end - start); \
long long work = (WORK_PER_RUN) * (ITERATIONS); \
double nanos = (duration.count() + work - 1) / (double)work; \
printf("%10g ns %2dx %s\n", nanos, (ITERATIONS), #CODE); \
} while (0)
void
object_test()
{
Json obj;
obj["content"] = "hello";
if (obj.toString() != "{\"content\":\"hello\"}")
exit(1);
}
void
deep_test()
{
Json A1;
A1[0] = 0;
A1[1] = 10;
A1[2] = 20;
A1[3] = 3.14;
A1[4] = 40;
Json A2;
A2[0] = std::move(A1);
Json A3;
A3[0] = std::move(A2);
Json obj;
obj["content"] = std::move(A3);
if (obj.toString() != "{\"content\":[[[0,10,20,3.14,40]]]}")
exit(2);
}
void
parse_test()
{
std::pair<Json::Status, Json> res =
Json::parse("{ \"content\":[[[0,10,20,3.14,40]]]}");
if (res.first != Json::success)
exit(3);
if (res.second.toString() != "{\"content\":[[[0,10,20,3.14,40]]]}")
exit(4);
if (res.second.toStringPretty() !=
R"({"content": [[[0, 10, 20, 3.14, 40]]]})")
exit(5);
res = Json::parse("{ \"a\": 1, \"b\": [2, 3]}");
if (res.second.toString() != R"({"a":1,"b":[2,3]})")
exit(6);
if (res.second.toStringPretty() !=
R"({
"a": 1,
"b": [2, 3]
})")
exit(7);
}
static const struct
{
std::string before;
std::string after;
} kRoundTrip[] = {
// types
{ "0", "0" },
{ "[]", "[]" },
{ "{}", "{}" },
{ "0.1", "0.1" },
{ "\"\"", "\"\"" },
{ "null", "null" },
{ "true", "true" },
{ "false", "false" },
// valid utf16 sequences
{ " [\"\\u0020\"] ", "[\" \"]" },
{ " [\"\\u00A0\"] ", "[\"\\u00a0\"]" },
// when we encounter invalid utf16 sequences
// we turn them into ascii
{ "[\"\\uDFAA\"]", "[\"\\\\uDFAA\"]" },
{ " [\"\\uDd1e\\uD834\"] ", "[\"\\\\uDd1e\\\\uD834\"]" },
{ " [\"\\ud800abc\"] ", "[\"\\\\ud800abc\"]" },
{ " [\"\\ud800\"] ", "[\"\\\\ud800\"]" },
{ " [\"\\uD800\\uD800\\n\"] ", "[\"\\\\uD800\\\\uD800\\n\"]" },
{ " [\"\\uDd1ea\"] ", "[\"\\\\uDd1ea\"]" },
{ " [\"\\uD800\\n\"] ", "[\"\\\\uD800\\n\"]" },
// underflow and overflow
{ " [123.456e-789] ", "[0]" },
{ " [0."
"4e0066999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999969999999006] ",
"[1e5000]" },
{ " [1.5e+9999] ", "[1e5000]" },
{ " [-1.5e+9999] ", "[-1e5000]" },
{ " [-123123123123123123123123123123] ", "[-1.2312312312312312e+29]" },
};
// https://github.com/nst/JSONTestSuite/
static const struct
{
Json::Status error;
std::string json;
} kJsonTestSuite[] = {
{ Json::absent_value, "" },
{ Json::trailing_content, "[] []" },
{ Json::illegal_character, "[nan]" },
{ Json::bad_negative, "[-nan]" },
{ Json::illegal_character, "[+NaN]" },
{ Json::trailing_content,
"{\"Extra value after close\": true} \"misplaced quoted value\"" },
{ Json::illegal_character, "{\"Illegal expression\": 1 + 2}" },
{ Json::illegal_character, "{\"Illegal invocation\": alert()}" },
{ Json::unexpected_octal, "{\"Numbers cannot have leading zeroes\": 013}" },
{ Json::illegal_character, "{\"Numbers cannot be hex\": 0x14}" },
{ Json::hex_escape_not_printable, "[\"Illegal backslash escape: \\x15\"]" },
{ Json::illegal_character, "[\\naked]" },
{ Json::invalid_escape_character, "[\"Illegal backslash escape: \\017\"]" },
{ Json::depth_exceeded,
"[[[[[[[[[[[[[[[[[[[[\"Too deep\"]]]]]]]]]]]]]]]]]]]]" },
{ Json::missing_colon, "{\"Missing colon\" null}" },
{ Json::unexpected_colon, "{\"Double colon\":: null}" },
{ Json::unexpected_comma, "{\"Comma instead of colon\", null}" },
{ Json::unexpected_colon, "[\"Colon instead of comma\": false]" },
{ Json::illegal_character, "[\"Bad value\", truth]" },
{ Json::illegal_character, "[\'single quote\']" },
{ Json::non_del_c0_control_code_in_string,
"[\"\ttab\tcharacter\tin\tstring\t\"]" },
{ Json::invalid_escape_character,
"[\"tab\\ character\\ in\\ string\\ \"]" },
{ Json::non_del_c0_control_code_in_string, "[\"line\nbreak\"]" },
{ Json::invalid_escape_character, "[\"line\\\nbreak\"]" },
{ Json::bad_exponent, "[0e]" },
{ Json::unexpected_eof, "[\"Unclosed array\"" },
{ Json::bad_exponent, "[0e+]" },
{ Json::bad_exponent, "[0e+-1]" },
{ Json::unexpected_eof, "{\"Comma instead if closing brace\": true," },
{ Json::unexpected_end_of_object, "[\"mismatch\"}" },
{ Json::illegal_character, "{unquoted_key: \"keys must be quoted\"}" },
{ Json::unexpected_end_of_array, "[\"extra comma\",]" },
{ Json::unexpected_comma, "[\"double extra comma\",,]" },
{ Json::unexpected_comma, "[ , \"<-- missing value\"]" },
{ Json::trailing_content, "[\"Comma after the close\"]," },
{ Json::trailing_content, "[\"Extra close\"]]" },
{ Json::unexpected_end_of_object, "{\"Extra comma\": true,}" },
{ Json::unexpected_eof, " {\"a\" " },
{ Json::unexpected_eof, " {\"a\": " },
{ Json::unexpected_colon, " {:\"b\" " },
{ Json::illegal_character, " {\"a\" b} " },
{ Json::illegal_character, " {key: 'value'} " },
{ Json::object_key_must_be_string, " {\"a\":\"a\" 123} " },
{ Json::illegal_character, " \x7b\xf0\x9f\x87\xa8\xf0\x9f\x87\xad\x7d " },
{ Json::object_key_must_be_string, " {[: \"x\"} " },
{ Json::illegal_character, " [1.8011670033376514H-308] " },
{ Json::illegal_character, " [1.2a-3] " },
{ Json::illegal_character, " [.123] " },
{ Json::bad_exponent, " [1e\xe5] " },
{ Json::bad_exponent, " [1ea] " },
{ Json::illegal_character, " [-1x] " },
{ Json::bad_negative, " [-.123] " },
{ Json::bad_negative, " [-foo] " },
{ Json::bad_negative, " [-Infinity] " },
{ Json::illegal_character, " \x5b\x30\xe5\x5d " },
{ Json::illegal_character, " \x5b\x31\x65\x31\xe5\x5d " },
{ Json::illegal_character, " \x5b\x31\x32\x33\xe5\x5d " },
{ Json::missing_comma,
" \x5b\x2d\x31\x32\x33\x2e\x31\x32\x33\x66\x6f\x6f\x5d " },
{ Json::bad_exponent, " [0e+-1] " },
{ Json::illegal_character, " [Infinity] " },
{ Json::illegal_character, " [0x42] " },
{ Json::illegal_character, " [0x1] " },
{ Json::illegal_character, " [1+2] " },
{ Json::illegal_character, " \x5b\xef\xbc\x91\x5d " },
{ Json::illegal_character, " [NaN] " },
{ Json::illegal_character, " [Inf] " },
{ Json::bad_double, " [9.e+] " },
{ Json::bad_exponent, " [1eE2] " },
{ Json::bad_exponent, " [1e0e] " },
{ Json::bad_exponent, " [1.0e-] " },
{ Json::bad_exponent, " [1.0e+] " },
{ Json::bad_exponent, " [0e] " },
{ Json::bad_exponent, " [0e+] " },
{ Json::bad_exponent, " [0E] " },
{ Json::bad_exponent, " [0E+] " },
{ Json::bad_exponent, " [0.3e] " },
{ Json::bad_exponent, " [0.3e+] " },
{ Json::illegal_character, " [0.1.2] " },
{ Json::illegal_character, " [.2e-3] " },
{ Json::illegal_character, " [.-1] " },
{ Json::bad_negative, " [-NaN] " },
{ Json::illegal_character, " [+Inf] " },
{ Json::illegal_character, " [+1] " },
{ Json::illegal_character, " [++1234] " },
{ Json::illegal_character, " [tru] " },
{ Json::illegal_character, " [nul] " },
{ Json::illegal_character, " [fals] " },
{ Json::unexpected_eof, " [{} " },
{ Json::unexpected_eof, "\n[1,\n1\n,1 " },
{ Json::unexpected_eof, " [1, " },
{ Json::unexpected_eof, " [\"\" " },
{ Json::illegal_character, " [* " },
{ Json::non_del_c0_control_code_in_string,
" \x5b\x22\x0b\x61\x22\x5c\x66\x5d " },
{ Json::unexpected_eof, "[\"a\",\n4\n,1,1 " },
{ Json::unexpected_colon, " [1:2] " },
{ Json::illegal_character, " \x5b\xff\x5d " },
{ Json::illegal_character, " \x5b\x78 " },
{ Json::unexpected_eof, " [\"x\" " },
{ Json::unexpected_colon, " [\"\": 1] " },
{ Json::illegal_character, " [a\xe5] " },
{ Json::unexpected_comma, " {\"x\", null} " },
{ Json::illegal_character, " [\"x\", truth] " },
{ Json::illegal_character, STRING("\x00") },
{ Json::trailing_content, "\n[\"x\"]]" },
{ Json::unexpected_octal, " [012] " },
{ Json::unexpected_octal, " [-012] " },
{ Json::missing_comma, " [1 000.0] " },
{ Json::unexpected_octal, " [-01] " },
{ Json::bad_negative, " [- 1] " },
{ Json::bad_negative, " [-] " },
{ Json::illegal_utf8_character, " {\"\xb9\":\"0\",} " },
{ Json::unexpected_colon, " {\"x\"::\"b\"} " },
{ Json::unexpected_comma, " [1,,] " },
{ Json::unexpected_end_of_array, " [1,] " },
{ Json::unexpected_comma, " [1,,2] " },
{ Json::unexpected_comma, " [,1] " },
{ Json::missing_comma, " [ 3[ 4]] " },
{ Json::missing_comma, " [1 true] " },
{ Json::missing_comma, " [\"a\" \"b\"] " },
{ Json::bad_negative, " [--2.] " },
{ Json::bad_double, " [1.] " },
{ Json::bad_double, " [2.e3] " },
{ Json::bad_double, " [2.e-3] " },
{ Json::bad_double, " [2.e+3] " },
{ Json::bad_double, " [0.e1] " },
{ Json::bad_double, " [-2.] " },
{ Json::illegal_character, " \xef\xbb\xbf{} " },
{ Json::illegal_character, STRING(" [\x00\"\x00\xe9\x00\"\x00]\x00 ") },
{ Json::illegal_character, STRING(" \x00[\x00\"\x00\xe9\x00\"\x00] ") },
{ Json::malformed_utf8, " [\"\xe0\xff\"] " },
{ Json::illegal_utf8_character, " [\"\xfc\x80\x80\x80\x80\x80\"] " },
{ Json::illegal_utf8_character, " [\"\xfc\x83\xbf\xbf\xbf\xbf\"] " },
{ Json::overlong_ascii, " [\"\xc0\xaf\"] " },
{ Json::utf8_exceeds_utf16_range, " [\"\xf4\xbf\xbf\xbf\"] " },
{ Json::c1_control_code_in_string, " [\"\x81\"] " },
{ Json::malformed_utf8, " [\"\xe9\"] " },
{ Json::illegal_utf8_character, " [\"\xff\"] " },
{ Json::success, kHuge },
{ Json::success,
R"([[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]])" },
{ Json::success, R"({
"JSON Test Pattern pass3": {
"The outermost value": "must be an object or array.",
"In this test": "It is an object."
}
}
)" },
};
void
round_trip_test()
{
for (size_t i = 0; i < ARRAYLEN(kRoundTrip); ++i) {
std::pair<Json::Status, Json> res = Json::parse(kRoundTrip[i].before);
if (res.first != Json::success) {
printf(
"error: Json::parse returned Json::%s but wanted Json::%s: %s\n",
Json::StatusToString(res.first),
Json::StatusToString(Json::success),
kRoundTrip[i].before.c_str());
exit(10);
}
if (res.second.toString() != kRoundTrip[i].after) {
printf("error: Json::parse(%s).toString() was %s but should have "
"been %s\n",
kRoundTrip[i].before.c_str(),
res.second.toString().c_str(),
kRoundTrip[i].after.c_str());
exit(11);
}
}
}
void
json_test_suite()
{
for (size_t i = 0; i < ARRAYLEN(kJsonTestSuite); ++i) {
std::pair<Json::Status, Json> res = Json::parse(kJsonTestSuite[i].json);
if (res.first != kJsonTestSuite[i].error) {
printf(
"error: Json::parse returned Json::%s but wanted Json::%s: %s\n",
Json::StatusToString(res.first),
Json::StatusToString(kJsonTestSuite[i].error),
kJsonTestSuite[i].json.c_str());
exit(12);
}
}
}
void
afl_regression()
{
Json::parse("[{\"\":1,3:14,]\n");
Json::parse("[\n"
"\n"
"3E14,\n"
"{\"!\":4,733:4,[\n"
"\n"
"3EL%,3E14,\n"
"{][1][1,,]");
Json::parse("[\n"
"null,\n"
"1,\n"
"3.14,\n"
"{\"a\": \"b\",\n"
"3:14,ull}\n"
"]");
Json::parse("[\n"
"\n"
"3E14,\n"
"{\"a!!!!!!!!!!!!!!!!!!\":4, \n"
"\n"
"3:1,,\n"
"3[\n"
"\n"
"]");
Json::parse("[\n"
"\n"
"3E14,\n"
"{\"a!!:!!!!!!!!!!!!!!!\":4, \n"
"\n"
"3E1:4, \n"
"\n"
"3E1,,\n"
",,\n"
"3[\n"
"\n"
"]");
Json::parse("[\n"
"\n"
"3E14,\n"
"{\"!\":4,733:4,[\n"
"\n"
"3E1%,][1,,]");
Json::parse("[\n"
"\n"
"3E14,\n"
"{\"!\":4,733:4,[\n"
"\n"
"3EL%,3E14,\n"
"{][1][1,,]");
}
int
main()
{
object_test();
deep_test();
parse_test();
round_trip_test();
afl_regression();
json_test_suite();
BENCH(2000, 1, object_test());
BENCH(2000, 1, deep_test());
BENCH(2000, 1, parse_test());
BENCH(2000, 1, round_trip_test());
BENCH(2000, 1, json_test_suite());
}