]> Git Repo - qemu.git/blob - tests/check-qjson.c
Merge remote-tracking branch 'kwolf/for-anthony' into staging
[qemu.git] / tests / check-qjson.c
1 /*
2  * Copyright IBM, Corp. 2009
3  * Copyright (c) 2013 Red Hat Inc.
4  *
5  * Authors:
6  *  Anthony Liguori   <[email protected]>
7  *  Markus Armbruster <[email protected]>,
8  *
9  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10  * See the COPYING.LIB file in the top-level directory.
11  *
12  */
13 #include <glib.h>
14
15 #include "qapi/qmp/qstring.h"
16 #include "qapi/qmp/qint.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qapi/qmp/qlist.h"
19 #include "qapi/qmp/qfloat.h"
20 #include "qapi/qmp/qbool.h"
21 #include "qapi/qmp/qjson.h"
22
23 #include "qemu-common.h"
24
25 static void escaped_string(void)
26 {
27     int i;
28     struct {
29         const char *encoded;
30         const char *decoded;
31         int skip;
32     } test_cases[] = {
33         { "\"\\b\"", "\b" },
34         { "\"\\f\"", "\f" },
35         { "\"\\n\"", "\n" },
36         { "\"\\r\"", "\r" },
37         { "\"\\t\"", "\t" },
38         { "\"/\"", "/" },
39         { "\"\\/\"", "/", .skip = 1 },
40         { "\"\\\\\"", "\\" },
41         { "\"\\\"\"", "\"" },
42         { "\"hello world \\\"embedded string\\\"\"",
43           "hello world \"embedded string\"" },
44         { "\"hello world\\nwith new line\"", "hello world\nwith new line" },
45         { "\"single byte utf-8 \\u0020\"", "single byte utf-8  ", .skip = 1 },
46         { "\"double byte utf-8 \\u00A2\"", "double byte utf-8 \xc2\xa2" },
47         { "\"triple byte utf-8 \\u20AC\"", "triple byte utf-8 \xe2\x82\xac" },
48         {}
49     };
50
51     for (i = 0; test_cases[i].encoded; i++) {
52         QObject *obj;
53         QString *str;
54
55         obj = qobject_from_json(test_cases[i].encoded);
56
57         g_assert(obj != NULL);
58         g_assert(qobject_type(obj) == QTYPE_QSTRING);
59         
60         str = qobject_to_qstring(obj);
61         g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].decoded);
62
63         if (test_cases[i].skip == 0) {
64             str = qobject_to_json(obj);
65             g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].encoded);
66             qobject_decref(obj);
67         }
68
69         QDECREF(str);
70     }
71 }
72
73 static void simple_string(void)
74 {
75     int i;
76     struct {
77         const char *encoded;
78         const char *decoded;
79     } test_cases[] = {
80         { "\"hello world\"", "hello world" },
81         { "\"the quick brown fox jumped over the fence\"",
82           "the quick brown fox jumped over the fence" },
83         {}
84     };
85
86     for (i = 0; test_cases[i].encoded; i++) {
87         QObject *obj;
88         QString *str;
89
90         obj = qobject_from_json(test_cases[i].encoded);
91
92         g_assert(obj != NULL);
93         g_assert(qobject_type(obj) == QTYPE_QSTRING);
94         
95         str = qobject_to_qstring(obj);
96         g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
97
98         str = qobject_to_json(obj);
99         g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
100
101         qobject_decref(obj);
102         
103         QDECREF(str);
104     }
105 }
106
107 static void single_quote_string(void)
108 {
109     int i;
110     struct {
111         const char *encoded;
112         const char *decoded;
113     } test_cases[] = {
114         { "'hello world'", "hello world" },
115         { "'the quick brown fox \\' jumped over the fence'",
116           "the quick brown fox ' jumped over the fence" },
117         {}
118     };
119
120     for (i = 0; test_cases[i].encoded; i++) {
121         QObject *obj;
122         QString *str;
123
124         obj = qobject_from_json(test_cases[i].encoded);
125
126         g_assert(obj != NULL);
127         g_assert(qobject_type(obj) == QTYPE_QSTRING);
128         
129         str = qobject_to_qstring(obj);
130         g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
131
132         QDECREF(str);
133     }
134 }
135
136 static void utf8_string(void)
137 {
138     /*
139      * FIXME Current behavior for invalid UTF-8 sequences is
140      * incorrect.  This test expects current, incorrect results.
141      * They're all marked "bug:" below, and are to be replaced by
142      * correct ones as the bugs get fixed.
143      *
144      * The JSON parser rejects some invalid sequences, but accepts
145      * others without correcting the problem.
146      *
147      * The JSON formatter replaces some invalid sequences by U+FFFF (a
148      * noncharacter), and goes wonky for others.
149      *
150      * For both directions, we should either reject all invalid
151      * sequences, or minimize overlong sequences and replace all other
152      * invalid sequences by a suitable replacement character.  A
153      * common choice for replacement is U+FFFD.
154      *
155      * Problem: we can't easily deal with embedded U+0000.  Parsing
156      * the JSON string "this \\u0000" is fun" yields "this \0 is fun",
157      * which gets misinterpreted as NUL-terminated "this ".  We should
158      * consider using overlong encoding \xC0\x80 for U+0000 ("modified
159      * UTF-8").
160      *
161      * Test cases are scraped from Markus Kuhn's UTF-8 decoder
162      * capability and stress test at
163      * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
164      */
165     static const struct {
166         const char *json_in;
167         const char *utf8_out;
168         const char *json_out;   /* defaults to @json_in */
169         const char *utf8_in;    /* defaults to @utf8_out */
170     } test_cases[] = {
171         /*
172          * Bug markers used here:
173          * - bug: not corrected
174          *   JSON parser fails to correct invalid sequence(s)
175          * - bug: rejected
176          *   JSON parser rejects invalid sequence(s)
177          *   We may choose to define this as feature
178          * - bug: want "\"...\""
179          *   JSON formatter produces incorrect result, this is the
180          *   correct one, assuming replacement character U+FFFF
181          * - bug: want "..." (no \")
182          *   JSON parser produces incorrect result, this is the
183          *   correct one, assuming replacement character U+FFFF
184          *   We may choose to reject instead of replace
185          * Not marked explicitly, but trivial to find:
186          * - JSON formatter replacing invalid sequence by \\uFFFF is a
187          *   bug if we want it to fail for invalid sequences.
188          */
189
190         /* 1  Some correct UTF-8 text */
191         {
192             /* a bit of German */
193             "\"Falsches \xC3\x9C" "ben von Xylophonmusik qu\xC3\xA4lt"
194             " jeden gr\xC3\xB6\xC3\x9F" "eren Zwerg.\"",
195             "Falsches \xC3\x9C" "ben von Xylophonmusik qu\xC3\xA4lt"
196             " jeden gr\xC3\xB6\xC3\x9F" "eren Zwerg.",
197             "\"Falsches \\u00DCben von Xylophonmusik qu\\u00E4lt"
198             " jeden gr\\u00F6\\u00DFeren Zwerg.\"",
199         },
200         {
201             /* a bit of Greek */
202             "\"\xCE\xBA\xE1\xBD\xB9\xCF\x83\xCE\xBC\xCE\xB5\"",
203             "\xCE\xBA\xE1\xBD\xB9\xCF\x83\xCE\xBC\xCE\xB5",
204             "\"\\u03BA\\u1F79\\u03C3\\u03BC\\u03B5\"",
205         },
206         /* 2  Boundary condition test cases */
207         /* 2.1  First possible sequence of a certain length */
208         /* 2.1.1  1 byte U+0000 */
209         {
210             "\"\\u0000\"",
211             "",                 /* bug: want overlong "\xC0\x80" */
212             "\"\"",             /* bug: want "\"\\u0000\"" */
213         },
214         /* 2.1.2  2 bytes U+0080 */
215         {
216             "\"\xC2\x80\"",
217             "\xC2\x80",
218             "\"\\u0080\"",
219         },
220         /* 2.1.3  3 bytes U+0800 */
221         {
222             "\"\xE0\xA0\x80\"",
223             "\xE0\xA0\x80",
224             "\"\\u0800\"",
225         },
226         /* 2.1.4  4 bytes U+10000 */
227         {
228             "\"\xF0\x90\x80\x80\"",
229             "\xF0\x90\x80\x80",
230             "\"\\u0400\\uFFFF\"", /* bug: want "\"\\uD800\\uDC00\"" */
231         },
232         /* 2.1.5  5 bytes U+200000 */
233         {
234             "\"\xF8\x88\x80\x80\x80\"",
235             NULL,                        /* bug: rejected */
236             "\"\\u8200\\uFFFF\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
237             "\xF8\x88\x80\x80\x80",
238         },
239         /* 2.1.6  6 bytes U+4000000 */
240         {
241             "\"\xFC\x84\x80\x80\x80\x80\"",
242             NULL,                               /* bug: rejected */
243             "\"\\uC100\\uFFFF\\uFFFF\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
244             "\xFC\x84\x80\x80\x80\x80",
245         },
246         /* 2.2  Last possible sequence of a certain length */
247         /* 2.2.1  1 byte U+007F */
248         {
249             "\"\x7F\"",
250             "\x7F",
251             "\"\177\"",
252         },
253         /* 2.2.2  2 bytes U+07FF */
254         {
255             "\"\xDF\xBF\"",
256             "\xDF\xBF",
257             "\"\\u07FF\"",
258         },
259         /* 2.2.3  3 bytes U+FFFF */
260         {
261             "\"\xEF\xBF\xBF\"",
262             "\xEF\xBF\xBF",
263             "\"\\uFFFF\"",
264         },
265         /* 2.2.4  4 bytes U+1FFFFF */
266         {
267             "\"\xF7\xBF\xBF\xBF\"",
268             NULL,                 /* bug: rejected */
269             "\"\\u7FFF\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
270             "\xF7\xBF\xBF\xBF",
271         },
272         /* 2.2.5  5 bytes U+3FFFFFF */
273         {
274             "\"\xFB\xBF\xBF\xBF\xBF\"",
275             NULL,                        /* bug: rejected */
276             "\"\\uBFFF\\uFFFF\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
277             "\xFB\xBF\xBF\xBF\xBF",
278         },
279         /* 2.2.6  6 bytes U+7FFFFFFF */
280         {
281             "\"\xFD\xBF\xBF\xBF\xBF\xBF\"",
282             NULL,                               /* bug: rejected */
283             "\"\\uDFFF\\uFFFF\\uFFFF\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
284             "\xFD\xBF\xBF\xBF\xBF\xBF",
285         },
286         /* 2.3  Other boundary conditions */
287         {
288             /* U+D7FF */
289             "\"\xED\x9F\xBF\"",
290             "\xED\x9F\xBF",
291             "\"\\uD7FF\"",
292         },
293         {
294             /* U+E000 */
295             "\"\xEE\x80\x80\"",
296             "\xEE\x80\x80",
297             "\"\\uE000\"",
298         },
299         {
300             /* U+FFFD */
301             "\"\xEF\xBF\xBD\"",
302             "\xEF\xBF\xBD",
303             "\"\\uFFFD\"",
304         },
305         {
306             /* U+10FFFF */
307             "\"\xF4\x8F\xBF\xBF\"",
308             "\xF4\x8F\xBF\xBF",
309             "\"\\u43FF\\uFFFF\"", /* bug: want "\"\\uDBFF\\uDFFF\"" */
310         },
311         {
312             /* U+110000 */
313             "\"\xF4\x90\x80\x80\"",
314             "\xF4\x90\x80\x80",
315             "\"\\u4400\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
316         },
317         /* 3  Malformed sequences */
318         /* 3.1  Unexpected continuation bytes */
319         /* 3.1.1  First continuation byte */
320         {
321             "\"\x80\"",
322             "\x80",             /* bug: not corrected */
323             "\"\\uFFFF\"",
324         },
325         /* 3.1.2  Last continuation byte */
326         {
327             "\"\xBF\"",
328             "\xBF",             /* bug: not corrected */
329             "\"\\uFFFF\"",
330         },
331         /* 3.1.3  2 continuation bytes */
332         {
333             "\"\x80\xBF\"",
334             "\x80\xBF",         /* bug: not corrected */
335             "\"\\uFFFF\\uFFFF\"",
336         },
337         /* 3.1.4  3 continuation bytes */
338         {
339             "\"\x80\xBF\x80\"",
340             "\x80\xBF\x80",     /* bug: not corrected */
341             "\"\\uFFFF\\uFFFF\\uFFFF\"",
342         },
343         /* 3.1.5  4 continuation bytes */
344         {
345             "\"\x80\xBF\x80\xBF\"",
346             "\x80\xBF\x80\xBF", /* bug: not corrected */
347             "\"\\uFFFF\\uFFFF\\uFFFF\\uFFFF\"",
348         },
349         /* 3.1.6  5 continuation bytes */
350         {
351             "\"\x80\xBF\x80\xBF\x80\"",
352             "\x80\xBF\x80\xBF\x80", /* bug: not corrected */
353             "\"\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\"",
354         },
355         /* 3.1.7  6 continuation bytes */
356         {
357             "\"\x80\xBF\x80\xBF\x80\xBF\"",
358             "\x80\xBF\x80\xBF\x80\xBF", /* bug: not corrected */
359             "\"\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\"",
360         },
361         /* 3.1.8  7 continuation bytes */
362         {
363             "\"\x80\xBF\x80\xBF\x80\xBF\x80\"",
364             "\x80\xBF\x80\xBF\x80\xBF\x80", /* bug: not corrected */
365             "\"\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\"",
366         },
367         /* 3.1.9  Sequence of all 64 possible continuation bytes */
368         {
369             "\"\x80\x81\x82\x83\x84\x85\x86\x87"
370             "\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F"
371             "\x90\x91\x92\x93\x94\x95\x96\x97"
372             "\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F"
373             "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7"
374             "\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF"
375             "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7"
376             "\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\"",
377              /* bug: not corrected */
378             "\x80\x81\x82\x83\x84\x85\x86\x87"
379             "\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F"
380             "\x90\x91\x92\x93\x94\x95\x96\x97"
381             "\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F"
382             "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7"
383             "\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF"
384             "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7"
385             "\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF",
386             "\"\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF"
387             "\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF"
388             "\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF"
389             "\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF"
390             "\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF"
391             "\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF"
392             "\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF"
393             "\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\""
394         },
395         /* 3.2  Lonely start characters */
396         /* 3.2.1  All 32 first bytes of 2-byte sequences, followed by space */
397         {
398             "\"\xC0 \xC1 \xC2 \xC3 \xC4 \xC5 \xC6 \xC7 "
399             "\xC8 \xC9 \xCA \xCB \xCC \xCD \xCE \xCF "
400             "\xD0 \xD1 \xD2 \xD3 \xD4 \xD5 \xD6 \xD7 "
401             "\xD8 \xD9 \xDA \xDB \xDC \xDD \xDE \xDF \"",
402             NULL,               /* bug: rejected */
403             "\"\\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF "
404             "\\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF "
405             "\\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF "
406             "\\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \"",
407             "\xC0 \xC1 \xC2 \xC3 \xC4 \xC5 \xC6 \xC7 "
408             "\xC8 \xC9 \xCA \xCB \xCC \xCD \xCE \xCF "
409             "\xD0 \xD1 \xD2 \xD3 \xD4 \xD5 \xD6 \xD7 "
410             "\xD8 \xD9 \xDA \xDB \xDC \xDD \xDE \xDF ",
411         },
412         /* 3.2.2  All 16 first bytes of 3-byte sequences, followed by space */
413         {
414             "\"\xE0 \xE1 \xE2 \xE3 \xE4 \xE5 \xE6 \xE7 "
415             "\xE8 \xE9 \xEA \xEB \xEC \xED \xEE \xEF \"",
416             /* bug: not corrected */
417             "\xE0 \xE1 \xE2 \xE3 \xE4 \xE5 \xE6 \xE7 "
418             "\xE8 \xE9 \xEA \xEB \xEC \xED \xEE \xEF ",
419             "\"\\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF "
420             "\\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \"",
421         },
422         /* 3.2.3  All 8 first bytes of 4-byte sequences, followed by space */
423         {
424             "\"\xF0 \xF1 \xF2 \xF3 \xF4 \xF5 \xF6 \xF7 \"",
425             NULL,               /* bug: rejected */
426             "\"\\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \\uFFFF \"",
427             "\xF0 \xF1 \xF2 \xF3 \xF4 \xF5 \xF6 \xF7 ",
428         },
429         /* 3.2.4  All 4 first bytes of 5-byte sequences, followed by space */
430         {
431             "\"\xF8 \xF9 \xFA \xFB \"",
432             NULL,               /* bug: rejected */
433             "\"\\uFFFF \\uFFFF \\uFFFF \\uFFFF \"",
434             "\xF8 \xF9 \xFA \xFB ",
435         },
436         /* 3.2.5  All 2 first bytes of 6-byte sequences, followed by space */
437         {
438             "\"\xFC \xFD \"",
439             NULL,               /* bug: rejected */
440             "\"\\uFFFF \\uFFFF \"",
441             "\xFC \xFD ",
442         },
443         /* 3.3  Sequences with last continuation byte missing */
444         /* 3.3.1  2-byte sequence with last byte missing (U+0000) */
445         {
446             "\"\xC0\"",
447             NULL,               /* bug: rejected */
448             "\"\\uFFFF\"",
449             "\xC0",
450         },
451         /* 3.3.2  3-byte sequence with last byte missing (U+0000) */
452         {
453             "\"\xE0\x80\"",
454             "\xE0\x80",           /* bug: not corrected */
455             "\"\\uFFFF\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
456         },
457         /* 3.3.3  4-byte sequence with last byte missing (U+0000) */
458         {
459             "\"\xF0\x80\x80\"",
460             "\xF0\x80\x80",     /* bug: not corrected */
461             "\"\\u0000\"",      /* bug: want "\"\\uFFFF\"" */
462         },
463         /* 3.3.4  5-byte sequence with last byte missing (U+0000) */
464         {
465             /* invalid */
466             "\"\xF8\x80\x80\x80\"", /* bug: not corrected */
467             NULL,                   /* bug: rejected */
468             "\"\\u8000\\uFFFF\"",   /* bug: want "\"\\uFFFF\"" */
469             "\xF8\x80\x80\x80",
470         },
471         /* 3.3.5  6-byte sequence with last byte missing (U+0000) */
472         {
473             "\"\xFC\x80\x80\x80\x80\"",
474             NULL,                        /* bug: rejected */
475             "\"\\uC000\\uFFFF\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
476             "\xFC\x80\x80\x80\x80",
477         },
478         /* 3.3.6  2-byte sequence with last byte missing (U+07FF) */
479         {
480             "\"\xDF\"",
481             "\xDF",             /* bug: not corrected */
482             "\"\\uFFFF\"",
483         },
484         /* 3.3.7  3-byte sequence with last byte missing (U+FFFF) */
485         {
486             "\"\xEF\xBF\"",
487             "\xEF\xBF",           /* bug: not corrected */
488             "\"\\uFFFF\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
489         },
490         /* 3.3.8  4-byte sequence with last byte missing (U+1FFFFF) */
491         {
492             "\"\xF7\xBF\xBF\"",
493             NULL,               /* bug: rejected */
494             "\"\\u7FFF\"",      /* bug: want "\"\\uFFFF\"" */
495             "\xF7\xBF\xBF",
496         },
497         /* 3.3.9  5-byte sequence with last byte missing (U+3FFFFFF) */
498         {
499             "\"\xFB\xBF\xBF\xBF\"",
500             NULL,                 /* bug: rejected */
501             "\"\\uBFFF\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
502             "\xFB\xBF\xBF\xBF",
503         },
504         /* 3.3.10  6-byte sequence with last byte missing (U+7FFFFFFF) */
505         {
506             "\"\xFD\xBF\xBF\xBF\xBF\"",
507             NULL,                        /* bug: rejected */
508             "\"\\uDFFF\\uFFFF\\uFFFF\"", /* bug: want "\"\\uFFFF\"", */
509             "\xFD\xBF\xBF\xBF\xBF",
510         },
511         /* 3.4  Concatenation of incomplete sequences */
512         {
513             "\"\xC0\xE0\x80\xF0\x80\x80\xF8\x80\x80\x80\xFC\x80\x80\x80\x80"
514             "\xDF\xEF\xBF\xF7\xBF\xBF\xFB\xBF\xBF\xBF\xFD\xBF\xBF\xBF\xBF\"",
515             NULL,               /* bug: rejected */
516             /* bug: want "\"\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF"
517                "\\uFFFF\\uFFFF\\uFFFF\\uFFFF\\uFFFF\"" */
518             "\"\\u0020\\uFFFF\\u0000\\u8000\\uFFFF\\uC000\\uFFFF\\uFFFF"
519             "\\u07EF\\uFFFF\\u7FFF\\uBFFF\\uFFFF\\uDFFF\\uFFFF\\uFFFF\"",
520             "\xC0\xE0\x80\xF0\x80\x80\xF8\x80\x80\x80\xFC\x80\x80\x80\x80"
521             "\xDF\xEF\xBF\xF7\xBF\xBF\xFB\xBF\xBF\xBF\xFD\xBF\xBF\xBF\xBF",
522         },
523         /* 3.5  Impossible bytes */
524         {
525             "\"\xFE\"",
526             NULL,               /* bug: rejected */
527             "\"\\uFFFF\"",
528             "\xFE",
529         },
530         {
531             "\"\xFF\"",
532             NULL,               /* bug: rejected */
533             "\"\\uFFFF\"",
534             "\xFF",
535         },
536         {
537             "\"\xFE\xFE\xFF\xFF\"",
538             NULL,                 /* bug: rejected */
539             /* bug: want "\"\\uFFFF\\uFFFF\\uFFFF\\uFFFF\"" */
540             "\"\\uEFBF\\uFFFF\"",
541             "\xFE\xFE\xFF\xFF",
542         },
543         /* 4  Overlong sequences */
544         /* 4.1  Overlong '/' */
545         {
546             "\"\xC0\xAF\"",
547             NULL,               /* bug: rejected */
548             "\"\\u002F\"",      /* bug: want "\"/\"" */
549             "\xC0\xAF",
550         },
551         {
552             "\"\xE0\x80\xAF\"",
553             "\xE0\x80\xAF",     /* bug: not corrected */
554             "\"\\u002F\"",      /* bug: want "\"/\"" */
555         },
556         {
557             "\"\xF0\x80\x80\xAF\"",
558             "\xF0\x80\x80\xAF",  /* bug: not corrected */
559             "\"\\u0000\\uFFFF\"" /* bug: want "\"/\"" */
560         },
561         {
562             "\"\xF8\x80\x80\x80\xAF\"",
563             NULL,                        /* bug: rejected */
564             "\"\\u8000\\uFFFF\\uFFFF\"", /* bug: want "\"/\"" */
565             "\xF8\x80\x80\x80\xAF",
566         },
567         {
568             "\"\xFC\x80\x80\x80\x80\xAF\"",
569             NULL,                               /* bug: rejected */
570             "\"\\uC000\\uFFFF\\uFFFF\\uFFFF\"", /* bug: want "\"/\"" */
571             "\xFC\x80\x80\x80\x80\xAF",
572         },
573         /* 4.2  Maximum overlong sequences */
574         {
575             /* \U+007F */
576             "\"\xC1\xBF\"",
577             NULL,               /* bug: rejected */
578             "\"\\u007F\"",      /* bug: want "\"\177\"" */
579             "\xC1\xBF",
580         },
581         {
582             /* \U+07FF */
583             "\"\xE0\x9F\xBF\"",
584             "\xE0\x9F\xBF",     /* bug: not corrected */
585             "\"\\u07FF\"",
586         },
587         {
588             /* \U+FFFF */
589             "\"\xF0\x8F\xBF\xBF\"",
590             "\xF0\x8F\xBF\xBF",   /* bug: not corrected */
591             "\"\\u03FF\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
592         },
593         {
594             /* \U+1FFFFF */
595             "\"\xF8\x87\xBF\xBF\xBF\"",
596             NULL,                        /* bug: rejected */
597             "\"\\u81FF\\uFFFF\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
598             "\xF8\x87\xBF\xBF\xBF",
599         },
600         {
601             /* \U+3FFFFFF */
602             "\"\xFC\x83\xBF\xBF\xBF\xBF\"",
603             NULL,                               /* bug: rejected */
604             "\"\\uC0FF\\uFFFF\\uFFFF\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
605             "\xFC\x83\xBF\xBF\xBF\xBF",
606         },
607         /* 4.3  Overlong representation of the NUL character */
608         {
609             /* \U+0000 */
610             "\"\xC0\x80\"",
611             NULL,               /* bug: rejected */
612             "\"\\u0000\"",
613             "\xC0\x80",
614         },
615         {
616             /* \U+0000 */
617             "\"\xE0\x80\x80\"",
618             "\xE0\x80\x80",     /* bug: not corrected */
619             "\"\\u0000\"",
620         },
621         {
622             /* \U+0000 */
623             "\"\xF0\x80\x80\x80\"",
624             "\xF0\x80\x80\x80",   /* bug: not corrected */
625             "\"\\u0000\\uFFFF\"", /* bug: want "\"\\u0000\"" */
626         },
627         {
628             /* \U+0000 */
629             "\"\xF8\x80\x80\x80\x80\"",
630             NULL,                        /* bug: rejected */
631             "\"\\u8000\\uFFFF\\uFFFF\"", /* bug: want "\"\\u0000\"" */
632             "\xF8\x80\x80\x80\x80",
633         },
634         {
635             /* \U+0000 */
636             "\"\xFC\x80\x80\x80\x80\x80\"",
637             NULL,                               /* bug: rejected */
638             "\"\\uC000\\uFFFF\\uFFFF\\uFFFF\"", /* bug: want "\"\\u0000\"" */
639             "\xFC\x80\x80\x80\x80\x80",
640         },
641         /* 5  Illegal code positions */
642         /* 5.1  Single UTF-16 surrogates */
643         {
644             /* \U+D800 */
645             "\"\xED\xA0\x80\"",
646             "\xED\xA0\x80",     /* bug: not corrected */
647             "\"\\uD800\"",      /* bug: want "\"\\uFFFF\"" */
648         },
649         {
650             /* \U+DB7F */
651             "\"\xED\xAD\xBF\"",
652             "\xED\xAD\xBF",     /* bug: not corrected */
653             "\"\\uDB7F\"",      /* bug: want "\"\\uFFFF\"" */
654         },
655         {
656             /* \U+DB80 */
657             "\"\xED\xAE\x80\"",
658             "\xED\xAE\x80",     /* bug: not corrected */
659             "\"\\uDB80\"",      /* bug: want "\"\\uFFFF\"" */
660         },
661         {
662             /* \U+DBFF */
663             "\"\xED\xAF\xBF\"",
664             "\xED\xAF\xBF",     /* bug: not corrected */
665             "\"\\uDBFF\"",      /* bug: want "\"\\uFFFF\"" */
666         },
667         {
668             /* \U+DC00 */
669             "\"\xED\xB0\x80\"",
670             "\xED\xB0\x80",     /* bug: not corrected */
671             "\"\\uDC00\"",      /* bug: want "\"\\uFFFF\"" */
672         },
673         {
674             /* \U+DF80 */
675             "\"\xED\xBE\x80\"",
676             "\xED\xBE\x80",     /* bug: not corrected */
677             "\"\\uDF80\"",      /* bug: want "\"\\uFFFF\"" */
678         },
679         {
680             /* \U+DFFF */
681             "\"\xED\xBF\xBF\"",
682             "\xED\xBF\xBF",     /* bug: not corrected */
683             "\"\\uDFFF\"",      /* bug: want "\"\\uFFFF\"" */
684         },
685         /* 5.2  Paired UTF-16 surrogates */
686         {
687             /* \U+D800\U+DC00 */
688             "\"\xED\xA0\x80\xED\xB0\x80\"",
689             "\xED\xA0\x80\xED\xB0\x80", /* bug: not corrected */
690             "\"\\uD800\\uDC00\"", /* bug: want "\"\\uFFFF\\uFFFF\"" */
691         },
692         {
693             /* \U+D800\U+DFFF */
694             "\"\xED\xA0\x80\xED\xBF\xBF\"",
695             "\xED\xA0\x80\xED\xBF\xBF", /* bug: not corrected */
696             "\"\\uD800\\uDFFF\"", /* bug: want "\"\\uFFFF\\uFFFF\"" */
697         },
698         {
699             /* \U+DB7F\U+DC00 */
700             "\"\xED\xAD\xBF\xED\xB0\x80\"",
701             "\xED\xAD\xBF\xED\xB0\x80", /* bug: not corrected */
702             "\"\\uDB7F\\uDC00\"", /* bug: want "\"\\uFFFF\\uFFFF\"" */
703         },
704         {
705             /* \U+DB7F\U+DFFF */
706             "\"\xED\xAD\xBF\xED\xBF\xBF\"",
707             "\xED\xAD\xBF\xED\xBF\xBF", /* bug: not corrected */
708             "\"\\uDB7F\\uDFFF\"", /* bug: want "\"\\uFFFF\\uFFFF\"" */
709         },
710         {
711             /* \U+DB80\U+DC00 */
712             "\"\xED\xAE\x80\xED\xB0\x80\"",
713             "\xED\xAE\x80\xED\xB0\x80", /* bug: not corrected */
714             "\"\\uDB80\\uDC00\"", /* bug: want "\"\\uFFFF\\uFFFF\"" */
715         },
716         {
717             /* \U+DB80\U+DFFF */
718             "\"\xED\xAE\x80\xED\xBF\xBF\"",
719             "\xED\xAE\x80\xED\xBF\xBF", /* bug: not corrected */
720             "\"\\uDB80\\uDFFF\"", /* bug: want "\"\\uFFFF\\uFFFF\"" */
721         },
722         {
723             /* \U+DBFF\U+DC00 */
724             "\"\xED\xAF\xBF\xED\xB0\x80\"",
725             "\xED\xAF\xBF\xED\xB0\x80", /* bug: not corrected */
726             "\"\\uDBFF\\uDC00\"", /* bug: want "\"\\uFFFF\\uFFFF\"" */
727         },
728         {
729             /* \U+DBFF\U+DFFF */
730             "\"\xED\xAF\xBF\xED\xBF\xBF\"",
731             "\xED\xAF\xBF\xED\xBF\xBF", /* bug: not corrected */
732             "\"\\uDBFF\\uDFFF\"", /* bug: want "\"\\uFFFF\\uFFFF\"" */
733         },
734         /* 5.3  Other illegal code positions */
735         {
736             /* \U+FFFE */
737             "\"\xEF\xBF\xBE\"",
738             "\xEF\xBF\xBE",     /* bug: not corrected */
739             "\"\\uFFFE\"",      /* bug: not corrected */
740         },
741         {
742             /* \U+FFFF */
743             "\"\xEF\xBF\xBF\"",
744             "\xEF\xBF\xBF",     /* bug: not corrected */
745             "\"\\uFFFF\"",      /* bug: not corrected */
746         },
747         {}
748     };
749     int i;
750     QObject *obj;
751     QString *str;
752     const char *json_in, *utf8_out, *utf8_in, *json_out;
753
754     for (i = 0; test_cases[i].json_in; i++) {
755         json_in = test_cases[i].json_in;
756         utf8_out = test_cases[i].utf8_out;
757         utf8_in = test_cases[i].utf8_in ?: test_cases[i].utf8_out;
758         json_out = test_cases[i].json_out ?: test_cases[i].json_in;
759
760         obj = qobject_from_json(json_in);
761         if (utf8_out) {
762             g_assert(obj);
763             g_assert(qobject_type(obj) == QTYPE_QSTRING);
764             str = qobject_to_qstring(obj);
765             g_assert_cmpstr(qstring_get_str(str), ==, utf8_out);
766         } else {
767             g_assert(!obj);
768         }
769         qobject_decref(obj);
770
771         obj = QOBJECT(qstring_from_str(utf8_in));
772         str = qobject_to_json(obj);
773         if (json_out) {
774             g_assert(str);
775             g_assert_cmpstr(qstring_get_str(str), ==, json_out);
776         } else {
777             g_assert(!str);
778         }
779         QDECREF(str);
780         qobject_decref(obj);
781
782         /*
783          * Disabled, because json_out currently contains the crap
784          * qobject_to_json() produces.
785          * FIXME Enable once these bugs have been fixed.
786          */
787         if (0 && json_out != json_in) {
788             obj = qobject_from_json(json_out);
789             g_assert(obj);
790             g_assert(qobject_type(obj) == QTYPE_QSTRING);
791             str = qobject_to_qstring(obj);
792             g_assert_cmpstr(qstring_get_str(str), ==, utf8_out);
793         }
794     }
795 }
796
797 static void vararg_string(void)
798 {
799     int i;
800     struct {
801         const char *decoded;
802     } test_cases[] = {
803         { "hello world" },
804         { "the quick brown fox jumped over the fence" },
805         {}
806     };
807
808     for (i = 0; test_cases[i].decoded; i++) {
809         QObject *obj;
810         QString *str;
811
812         obj = qobject_from_jsonf("%s", test_cases[i].decoded);
813
814         g_assert(obj != NULL);
815         g_assert(qobject_type(obj) == QTYPE_QSTRING);
816         
817         str = qobject_to_qstring(obj);
818         g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
819
820         QDECREF(str);
821     }
822 }
823
824 static void simple_number(void)
825 {
826     int i;
827     struct {
828         const char *encoded;
829         int64_t decoded;
830         int skip;
831     } test_cases[] = {
832         { "0", 0 },
833         { "1234", 1234 },
834         { "1", 1 },
835         { "-32", -32 },
836         { "-0", 0, .skip = 1 },
837         { },
838     };
839
840     for (i = 0; test_cases[i].encoded; i++) {
841         QObject *obj;
842         QInt *qint;
843
844         obj = qobject_from_json(test_cases[i].encoded);
845         g_assert(obj != NULL);
846         g_assert(qobject_type(obj) == QTYPE_QINT);
847
848         qint = qobject_to_qint(obj);
849         g_assert(qint_get_int(qint) == test_cases[i].decoded);
850         if (test_cases[i].skip == 0) {
851             QString *str;
852
853             str = qobject_to_json(obj);
854             g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
855             QDECREF(str);
856         }
857
858         QDECREF(qint);
859     }
860 }
861
862 static void float_number(void)
863 {
864     int i;
865     struct {
866         const char *encoded;
867         double decoded;
868         int skip;
869     } test_cases[] = {
870         { "32.43", 32.43 },
871         { "0.222", 0.222 },
872         { "-32.12313", -32.12313 },
873         { "-32.20e-10", -32.20e-10, .skip = 1 },
874         { },
875     };
876
877     for (i = 0; test_cases[i].encoded; i++) {
878         QObject *obj;
879         QFloat *qfloat;
880
881         obj = qobject_from_json(test_cases[i].encoded);
882         g_assert(obj != NULL);
883         g_assert(qobject_type(obj) == QTYPE_QFLOAT);
884
885         qfloat = qobject_to_qfloat(obj);
886         g_assert(qfloat_get_double(qfloat) == test_cases[i].decoded);
887
888         if (test_cases[i].skip == 0) {
889             QString *str;
890
891             str = qobject_to_json(obj);
892             g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
893             QDECREF(str);
894         }
895
896         QDECREF(qfloat);
897     }
898 }
899
900 static void vararg_number(void)
901 {
902     QObject *obj;
903     QInt *qint;
904     QFloat *qfloat;
905     int value = 0x2342;
906     int64_t value64 = 0x2342342343LL;
907     double valuef = 2.323423423;
908
909     obj = qobject_from_jsonf("%d", value);
910     g_assert(obj != NULL);
911     g_assert(qobject_type(obj) == QTYPE_QINT);
912
913     qint = qobject_to_qint(obj);
914     g_assert(qint_get_int(qint) == value);
915
916     QDECREF(qint);
917
918     obj = qobject_from_jsonf("%" PRId64, value64);
919     g_assert(obj != NULL);
920     g_assert(qobject_type(obj) == QTYPE_QINT);
921
922     qint = qobject_to_qint(obj);
923     g_assert(qint_get_int(qint) == value64);
924
925     QDECREF(qint);
926
927     obj = qobject_from_jsonf("%f", valuef);
928     g_assert(obj != NULL);
929     g_assert(qobject_type(obj) == QTYPE_QFLOAT);
930
931     qfloat = qobject_to_qfloat(obj);
932     g_assert(qfloat_get_double(qfloat) == valuef);
933
934     QDECREF(qfloat);
935 }
936
937 static void keyword_literal(void)
938 {
939     QObject *obj;
940     QBool *qbool;
941     QString *str;
942
943     obj = qobject_from_json("true");
944     g_assert(obj != NULL);
945     g_assert(qobject_type(obj) == QTYPE_QBOOL);
946
947     qbool = qobject_to_qbool(obj);
948     g_assert(qbool_get_int(qbool) != 0);
949
950     str = qobject_to_json(obj);
951     g_assert(strcmp(qstring_get_str(str), "true") == 0);
952     QDECREF(str);
953
954     QDECREF(qbool);
955
956     obj = qobject_from_json("false");
957     g_assert(obj != NULL);
958     g_assert(qobject_type(obj) == QTYPE_QBOOL);
959
960     qbool = qobject_to_qbool(obj);
961     g_assert(qbool_get_int(qbool) == 0);
962
963     str = qobject_to_json(obj);
964     g_assert(strcmp(qstring_get_str(str), "false") == 0);
965     QDECREF(str);
966
967     QDECREF(qbool);
968
969     obj = qobject_from_jsonf("%i", false);
970     g_assert(obj != NULL);
971     g_assert(qobject_type(obj) == QTYPE_QBOOL);
972
973     qbool = qobject_to_qbool(obj);
974     g_assert(qbool_get_int(qbool) == 0);
975
976     QDECREF(qbool);
977     
978     obj = qobject_from_jsonf("%i", true);
979     g_assert(obj != NULL);
980     g_assert(qobject_type(obj) == QTYPE_QBOOL);
981
982     qbool = qobject_to_qbool(obj);
983     g_assert(qbool_get_int(qbool) != 0);
984
985     QDECREF(qbool);
986 }
987
988 typedef struct LiteralQDictEntry LiteralQDictEntry;
989 typedef struct LiteralQObject LiteralQObject;
990
991 struct LiteralQObject
992 {
993     int type;
994     union {
995         int64_t qint;
996         const char *qstr;
997         LiteralQDictEntry *qdict;
998         LiteralQObject *qlist;
999     } value;
1000 };
1001
1002 struct LiteralQDictEntry
1003 {
1004     const char *key;
1005     LiteralQObject value;
1006 };
1007
1008 #define QLIT_QINT(val) (LiteralQObject){.type = QTYPE_QINT, .value.qint = (val)}
1009 #define QLIT_QSTR(val) (LiteralQObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
1010 #define QLIT_QDICT(val) (LiteralQObject){.type = QTYPE_QDICT, .value.qdict = (val)}
1011 #define QLIT_QLIST(val) (LiteralQObject){.type = QTYPE_QLIST, .value.qlist = (val)}
1012
1013 typedef struct QListCompareHelper
1014 {
1015     int index;
1016     LiteralQObject *objs;
1017     int result;
1018 } QListCompareHelper;
1019
1020 static int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs);
1021
1022 static void compare_helper(QObject *obj, void *opaque)
1023 {
1024     QListCompareHelper *helper = opaque;
1025
1026     if (helper->result == 0) {
1027         return;
1028     }
1029
1030     if (helper->objs[helper->index].type == QTYPE_NONE) {
1031         helper->result = 0;
1032         return;
1033     }
1034
1035     helper->result = compare_litqobj_to_qobj(&helper->objs[helper->index++], obj);
1036 }
1037
1038 static int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs)
1039 {
1040     if (lhs->type != qobject_type(rhs)) {
1041         return 0;
1042     }
1043
1044     switch (lhs->type) {
1045     case QTYPE_QINT:
1046         return lhs->value.qint == qint_get_int(qobject_to_qint(rhs));
1047     case QTYPE_QSTRING:
1048         return (strcmp(lhs->value.qstr, qstring_get_str(qobject_to_qstring(rhs))) == 0);
1049     case QTYPE_QDICT: {
1050         int i;
1051
1052         for (i = 0; lhs->value.qdict[i].key; i++) {
1053             QObject *obj = qdict_get(qobject_to_qdict(rhs), lhs->value.qdict[i].key);
1054
1055             if (!compare_litqobj_to_qobj(&lhs->value.qdict[i].value, obj)) {
1056                 return 0;
1057             }
1058         }
1059
1060         return 1;
1061     }
1062     case QTYPE_QLIST: {
1063         QListCompareHelper helper;
1064
1065         helper.index = 0;
1066         helper.objs = lhs->value.qlist;
1067         helper.result = 1;
1068         
1069         qlist_iter(qobject_to_qlist(rhs), compare_helper, &helper);
1070
1071         return helper.result;
1072     }
1073     default:
1074         break;
1075     }
1076
1077     return 0;
1078 }
1079
1080 static void simple_dict(void)
1081 {
1082     int i;
1083     struct {
1084         const char *encoded;
1085         LiteralQObject decoded;
1086     } test_cases[] = {
1087         {
1088             .encoded = "{\"foo\": 42, \"bar\": \"hello world\"}",
1089             .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
1090                         { "foo", QLIT_QINT(42) },
1091                         { "bar", QLIT_QSTR("hello world") },
1092                         { }
1093                     })),
1094         }, {
1095             .encoded = "{}",
1096             .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
1097                         { }
1098                     })),
1099         }, {
1100             .encoded = "{\"foo\": 43}",
1101             .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
1102                         { "foo", QLIT_QINT(43) },
1103                         { }
1104                     })),
1105         },
1106         { }
1107     };
1108
1109     for (i = 0; test_cases[i].encoded; i++) {
1110         QObject *obj;
1111         QString *str;
1112
1113         obj = qobject_from_json(test_cases[i].encoded);
1114         g_assert(obj != NULL);
1115         g_assert(qobject_type(obj) == QTYPE_QDICT);
1116
1117         g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
1118
1119         str = qobject_to_json(obj);
1120         qobject_decref(obj);
1121
1122         obj = qobject_from_json(qstring_get_str(str));
1123         g_assert(obj != NULL);
1124         g_assert(qobject_type(obj) == QTYPE_QDICT);
1125
1126         g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
1127         qobject_decref(obj);
1128         QDECREF(str);
1129     }
1130 }
1131
1132 /*
1133  * this generates json of the form:
1134  * a(0,m) = [0, 1, ..., m-1]
1135  * a(n,m) = {
1136  *            'key0': a(0,m),
1137  *            'key1': a(1,m),
1138  *            ...
1139  *            'key(n-1)': a(n-1,m)
1140  *          }
1141  */
1142 static void gen_test_json(GString *gstr, int nest_level_max,
1143                           int elem_count)
1144 {
1145     int i;
1146
1147     g_assert(gstr);
1148     if (nest_level_max == 0) {
1149         g_string_append(gstr, "[");
1150         for (i = 0; i < elem_count; i++) {
1151             g_string_append_printf(gstr, "%d", i);
1152             if (i < elem_count - 1) {
1153                 g_string_append_printf(gstr, ", ");
1154             }
1155         }
1156         g_string_append(gstr, "]");
1157         return;
1158     }
1159
1160     g_string_append(gstr, "{");
1161     for (i = 0; i < nest_level_max; i++) {
1162         g_string_append_printf(gstr, "'key%d': ", i);
1163         gen_test_json(gstr, i, elem_count);
1164         if (i < nest_level_max - 1) {
1165             g_string_append(gstr, ",");
1166         }
1167     }
1168     g_string_append(gstr, "}");
1169 }
1170
1171 static void large_dict(void)
1172 {
1173     GString *gstr = g_string_new("");
1174     QObject *obj;
1175
1176     gen_test_json(gstr, 10, 100);
1177     obj = qobject_from_json(gstr->str);
1178     g_assert(obj != NULL);
1179
1180     qobject_decref(obj);
1181     g_string_free(gstr, true);
1182 }
1183
1184 static void simple_list(void)
1185 {
1186     int i;
1187     struct {
1188         const char *encoded;
1189         LiteralQObject decoded;
1190     } test_cases[] = {
1191         {
1192             .encoded = "[43,42]",
1193             .decoded = QLIT_QLIST(((LiteralQObject[]){
1194                         QLIT_QINT(43),
1195                         QLIT_QINT(42),
1196                         { }
1197                     })),
1198         },
1199         {
1200             .encoded = "[43]",
1201             .decoded = QLIT_QLIST(((LiteralQObject[]){
1202                         QLIT_QINT(43),
1203                         { }
1204                     })),
1205         },
1206         {
1207             .encoded = "[]",
1208             .decoded = QLIT_QLIST(((LiteralQObject[]){
1209                         { }
1210                     })),
1211         },
1212         {
1213             .encoded = "[{}]",
1214             .decoded = QLIT_QLIST(((LiteralQObject[]){
1215                         QLIT_QDICT(((LiteralQDictEntry[]){
1216                                     {},
1217                                         })),
1218                         {},
1219                             })),
1220         },
1221         { }
1222     };
1223
1224     for (i = 0; test_cases[i].encoded; i++) {
1225         QObject *obj;
1226         QString *str;
1227
1228         obj = qobject_from_json(test_cases[i].encoded);
1229         g_assert(obj != NULL);
1230         g_assert(qobject_type(obj) == QTYPE_QLIST);
1231
1232         g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
1233
1234         str = qobject_to_json(obj);
1235         qobject_decref(obj);
1236
1237         obj = qobject_from_json(qstring_get_str(str));
1238         g_assert(obj != NULL);
1239         g_assert(qobject_type(obj) == QTYPE_QLIST);
1240
1241         g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
1242         qobject_decref(obj);
1243         QDECREF(str);
1244     }
1245 }
1246
1247 static void simple_whitespace(void)
1248 {
1249     int i;
1250     struct {
1251         const char *encoded;
1252         LiteralQObject decoded;
1253     } test_cases[] = {
1254         {
1255             .encoded = " [ 43 , 42 ]",
1256             .decoded = QLIT_QLIST(((LiteralQObject[]){
1257                         QLIT_QINT(43),
1258                         QLIT_QINT(42),
1259                         { }
1260                     })),
1261         },
1262         {
1263             .encoded = " [ 43 , { 'h' : 'b' }, [ ], 42 ]",
1264             .decoded = QLIT_QLIST(((LiteralQObject[]){
1265                         QLIT_QINT(43),
1266                         QLIT_QDICT(((LiteralQDictEntry[]){
1267                                     { "h", QLIT_QSTR("b") },
1268                                     { }})),
1269                         QLIT_QLIST(((LiteralQObject[]){
1270                                     { }})),
1271                         QLIT_QINT(42),
1272                         { }
1273                     })),
1274         },
1275         {
1276             .encoded = " [ 43 , { 'h' : 'b' , 'a' : 32 }, [ ], 42 ]",
1277             .decoded = QLIT_QLIST(((LiteralQObject[]){
1278                         QLIT_QINT(43),
1279                         QLIT_QDICT(((LiteralQDictEntry[]){
1280                                     { "h", QLIT_QSTR("b") },
1281                                     { "a", QLIT_QINT(32) },
1282                                     { }})),
1283                         QLIT_QLIST(((LiteralQObject[]){
1284                                     { }})),
1285                         QLIT_QINT(42),
1286                         { }
1287                     })),
1288         },
1289         { }
1290     };
1291
1292     for (i = 0; test_cases[i].encoded; i++) {
1293         QObject *obj;
1294         QString *str;
1295
1296         obj = qobject_from_json(test_cases[i].encoded);
1297         g_assert(obj != NULL);
1298         g_assert(qobject_type(obj) == QTYPE_QLIST);
1299
1300         g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
1301
1302         str = qobject_to_json(obj);
1303         qobject_decref(obj);
1304
1305         obj = qobject_from_json(qstring_get_str(str));
1306         g_assert(obj != NULL);
1307         g_assert(qobject_type(obj) == QTYPE_QLIST);
1308
1309         g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
1310
1311         qobject_decref(obj);
1312         QDECREF(str);
1313     }
1314 }
1315
1316 static void simple_varargs(void)
1317 {
1318     QObject *embedded_obj;
1319     QObject *obj;
1320     LiteralQObject decoded = QLIT_QLIST(((LiteralQObject[]){
1321             QLIT_QINT(1),
1322             QLIT_QINT(2),
1323             QLIT_QLIST(((LiteralQObject[]){
1324                         QLIT_QINT(32),
1325                         QLIT_QINT(42),
1326                         {}})),
1327             {}}));
1328
1329     embedded_obj = qobject_from_json("[32, 42]");
1330     g_assert(embedded_obj != NULL);
1331
1332     obj = qobject_from_jsonf("[%d, 2, %p]", 1, embedded_obj);
1333     g_assert(obj != NULL);
1334
1335     g_assert(compare_litqobj_to_qobj(&decoded, obj) == 1);
1336
1337     qobject_decref(obj);
1338 }
1339
1340 static void empty_input(void)
1341 {
1342     const char *empty = "";
1343
1344     QObject *obj = qobject_from_json(empty);
1345     g_assert(obj == NULL);
1346 }
1347
1348 static void unterminated_string(void)
1349 {
1350     QObject *obj = qobject_from_json("\"abc");
1351     g_assert(obj == NULL);
1352 }
1353
1354 static void unterminated_sq_string(void)
1355 {
1356     QObject *obj = qobject_from_json("'abc");
1357     g_assert(obj == NULL);
1358 }
1359
1360 static void unterminated_escape(void)
1361 {
1362     QObject *obj = qobject_from_json("\"abc\\\"");
1363     g_assert(obj == NULL);
1364 }
1365
1366 static void unterminated_array(void)
1367 {
1368     QObject *obj = qobject_from_json("[32");
1369     g_assert(obj == NULL);
1370 }
1371
1372 static void unterminated_array_comma(void)
1373 {
1374     QObject *obj = qobject_from_json("[32,");
1375     g_assert(obj == NULL);
1376 }
1377
1378 static void invalid_array_comma(void)
1379 {
1380     QObject *obj = qobject_from_json("[32,}");
1381     g_assert(obj == NULL);
1382 }
1383
1384 static void unterminated_dict(void)
1385 {
1386     QObject *obj = qobject_from_json("{'abc':32");
1387     g_assert(obj == NULL);
1388 }
1389
1390 static void unterminated_dict_comma(void)
1391 {
1392     QObject *obj = qobject_from_json("{'abc':32,");
1393     g_assert(obj == NULL);
1394 }
1395
1396 static void invalid_dict_comma(void)
1397 {
1398     QObject *obj = qobject_from_json("{'abc':32,}");
1399     g_assert(obj == NULL);
1400 }
1401
1402 static void unterminated_literal(void)
1403 {
1404     QObject *obj = qobject_from_json("nul");
1405     g_assert(obj == NULL);
1406 }
1407
1408 int main(int argc, char **argv)
1409 {
1410     g_test_init(&argc, &argv, NULL);
1411
1412     g_test_add_func("/literals/string/simple", simple_string);
1413     g_test_add_func("/literals/string/escaped", escaped_string);
1414     g_test_add_func("/literals/string/utf8", utf8_string);
1415     g_test_add_func("/literals/string/single_quote", single_quote_string);
1416     g_test_add_func("/literals/string/vararg", vararg_string);
1417
1418     g_test_add_func("/literals/number/simple", simple_number);
1419     g_test_add_func("/literals/number/float", float_number);
1420     g_test_add_func("/literals/number/vararg", vararg_number);
1421
1422     g_test_add_func("/literals/keyword", keyword_literal);
1423
1424     g_test_add_func("/dicts/simple_dict", simple_dict);
1425     g_test_add_func("/dicts/large_dict", large_dict);
1426     g_test_add_func("/lists/simple_list", simple_list);
1427
1428     g_test_add_func("/whitespace/simple_whitespace", simple_whitespace);
1429
1430     g_test_add_func("/varargs/simple_varargs", simple_varargs);
1431
1432     g_test_add_func("/errors/empty_input", empty_input);
1433     g_test_add_func("/errors/unterminated/string", unterminated_string);
1434     g_test_add_func("/errors/unterminated/escape", unterminated_escape);
1435     g_test_add_func("/errors/unterminated/sq_string", unterminated_sq_string);
1436     g_test_add_func("/errors/unterminated/array", unterminated_array);
1437     g_test_add_func("/errors/unterminated/array_comma", unterminated_array_comma);
1438     g_test_add_func("/errors/unterminated/dict", unterminated_dict);
1439     g_test_add_func("/errors/unterminated/dict_comma", unterminated_dict_comma);
1440     g_test_add_func("/errors/invalid_array_comma", invalid_array_comma);
1441     g_test_add_func("/errors/invalid_dict_comma", invalid_dict_comma);
1442     g_test_add_func("/errors/unterminated/literal", unterminated_literal);
1443
1444     return g_test_run();
1445 }
This page took 0.102608 seconds and 4 git commands to generate.