check-qjson.c 45.0 KB
Newer Older
A
Anthony Liguori 已提交
1 2
/*
 * Copyright IBM, Corp. 2009
3
 * Copyright (c) 2013 Red Hat Inc.
A
Anthony Liguori 已提交
4 5 6
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
7
 *  Markus Armbruster <armbru@redhat.com>
A
Anthony Liguori 已提交
8 9 10 11 12
 *
 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
 * See the COPYING.LIB file in the top-level directory.
 *
 */
A
Anthony Liguori 已提交
13
#include <glib.h>
A
Anthony Liguori 已提交
14

15 16 17 18 19 20 21
#include "qapi/qmp/qstring.h"
#include "qapi/qmp/qint.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qlist.h"
#include "qapi/qmp/qfloat.h"
#include "qapi/qmp/qbool.h"
#include "qapi/qmp/qjson.h"
A
Anthony Liguori 已提交
22 23 24

#include "qemu-common.h"

A
Anthony Liguori 已提交
25
static void escaped_string(void)
A
Anthony Liguori 已提交
26 27 28 29 30
{
    int i;
    struct {
        const char *encoded;
        const char *decoded;
31
        int skip;
A
Anthony Liguori 已提交
32
    } test_cases[] = {
33 34 35 36 37
        { "\"\\b\"", "\b" },
        { "\"\\f\"", "\f" },
        { "\"\\n\"", "\n" },
        { "\"\\r\"", "\r" },
        { "\"\\t\"", "\t" },
J
Jan Kiszka 已提交
38 39
        { "\"/\"", "/" },
        { "\"\\/\"", "/", .skip = 1 },
40
        { "\"\\\\\"", "\\" },
A
Anthony Liguori 已提交
41 42 43 44
        { "\"\\\"\"", "\"" },
        { "\"hello world \\\"embedded string\\\"\"",
          "hello world \"embedded string\"" },
        { "\"hello world\\nwith new line\"", "hello world\nwith new line" },
45
        { "\"single byte utf-8 \\u0020\"", "single byte utf-8  ", .skip = 1 },
A
Anthony Liguori 已提交
46 47 48 49 50 51 52 53 54 55 56
        { "\"double byte utf-8 \\u00A2\"", "double byte utf-8 \xc2\xa2" },
        { "\"triple byte utf-8 \\u20AC\"", "triple byte utf-8 \xe2\x82\xac" },
        {}
    };

    for (i = 0; test_cases[i].encoded; i++) {
        QObject *obj;
        QString *str;

        obj = qobject_from_json(test_cases[i].encoded);

A
Anthony Liguori 已提交
57 58
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QSTRING);
A
Anthony Liguori 已提交
59 60
        
        str = qobject_to_qstring(obj);
A
Anthony Liguori 已提交
61
        g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].decoded);
A
Anthony Liguori 已提交
62

63 64
        if (test_cases[i].skip == 0) {
            str = qobject_to_json(obj);
A
Anthony Liguori 已提交
65
            g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].encoded);
66 67 68
            qobject_decref(obj);
        }

A
Anthony Liguori 已提交
69 70 71 72
        QDECREF(str);
    }
}

A
Anthony Liguori 已提交
73
static void simple_string(void)
A
Anthony Liguori 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
{
    int i;
    struct {
        const char *encoded;
        const char *decoded;
    } test_cases[] = {
        { "\"hello world\"", "hello world" },
        { "\"the quick brown fox jumped over the fence\"",
          "the quick brown fox jumped over the fence" },
        {}
    };

    for (i = 0; test_cases[i].encoded; i++) {
        QObject *obj;
        QString *str;

        obj = qobject_from_json(test_cases[i].encoded);

A
Anthony Liguori 已提交
92 93
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QSTRING);
A
Anthony Liguori 已提交
94 95
        
        str = qobject_to_qstring(obj);
A
Anthony Liguori 已提交
96
        g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
A
Anthony Liguori 已提交
97

98
        str = qobject_to_json(obj);
A
Anthony Liguori 已提交
99
        g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
100 101 102

        qobject_decref(obj);
        
A
Anthony Liguori 已提交
103 104 105 106
        QDECREF(str);
    }
}

A
Anthony Liguori 已提交
107
static void single_quote_string(void)
A
Anthony Liguori 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
{
    int i;
    struct {
        const char *encoded;
        const char *decoded;
    } test_cases[] = {
        { "'hello world'", "hello world" },
        { "'the quick brown fox \\' jumped over the fence'",
          "the quick brown fox ' jumped over the fence" },
        {}
    };

    for (i = 0; test_cases[i].encoded; i++) {
        QObject *obj;
        QString *str;

        obj = qobject_from_json(test_cases[i].encoded);

A
Anthony Liguori 已提交
126 127
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QSTRING);
A
Anthony Liguori 已提交
128 129
        
        str = qobject_to_qstring(obj);
A
Anthony Liguori 已提交
130
        g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
A
Anthony Liguori 已提交
131 132 133 134 135

        QDECREF(str);
    }
}

136 137 138 139 140 141 142 143 144 145 146
static void utf8_string(void)
{
    /*
     * FIXME Current behavior for invalid UTF-8 sequences is
     * incorrect.  This test expects current, incorrect results.
     * They're all marked "bug:" below, and are to be replaced by
     * correct ones as the bugs get fixed.
     *
     * The JSON parser rejects some invalid sequences, but accepts
     * others without correcting the problem.
     *
147 148 149 150
     * We should either reject all invalid sequences, or minimize
     * overlong sequences and replace all other invalid sequences by a
     * suitable replacement character.  A common choice for
     * replacement is U+FFFD.
151 152 153 154 155 156 157
     *
     * Problem: we can't easily deal with embedded U+0000.  Parsing
     * the JSON string "this \\u0000" is fun" yields "this \0 is fun",
     * which gets misinterpreted as NUL-terminated "this ".  We should
     * consider using overlong encoding \xC0\x80 for U+0000 ("modified
     * UTF-8").
     *
158
     * Most test cases are scraped from Markus Kuhn's UTF-8 decoder
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
     * capability and stress test at
     * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
     */
    static const struct {
        const char *json_in;
        const char *utf8_out;
        const char *json_out;   /* defaults to @json_in */
        const char *utf8_in;    /* defaults to @utf8_out */
    } test_cases[] = {
        /*
         * Bug markers used here:
         * - bug: not corrected
         *   JSON parser fails to correct invalid sequence(s)
         * - bug: rejected
         *   JSON parser rejects invalid sequence(s)
         *   We may choose to define this as feature
175
         * - bug: want "..."
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
         *   JSON parser produces incorrect result, this is the
         *   correct one, assuming replacement character U+FFFF
         *   We may choose to reject instead of replace
         */

        /* 1  Some correct UTF-8 text */
        {
            /* a bit of German */
            "\"Falsches \xC3\x9C" "ben von Xylophonmusik qu\xC3\xA4lt"
            " jeden gr\xC3\xB6\xC3\x9F" "eren Zwerg.\"",
            "Falsches \xC3\x9C" "ben von Xylophonmusik qu\xC3\xA4lt"
            " jeden gr\xC3\xB6\xC3\x9F" "eren Zwerg.",
            "\"Falsches \\u00DCben von Xylophonmusik qu\\u00E4lt"
            " jeden gr\\u00F6\\u00DFeren Zwerg.\"",
        },
        {
            /* a bit of Greek */
            "\"\xCE\xBA\xE1\xBD\xB9\xCF\x83\xCE\xBC\xCE\xB5\"",
            "\xCE\xBA\xE1\xBD\xB9\xCF\x83\xCE\xBC\xCE\xB5",
            "\"\\u03BA\\u1F79\\u03C3\\u03BC\\u03B5\"",
        },
        /* 2  Boundary condition test cases */
        /* 2.1  First possible sequence of a certain length */
        /* 2.1.1  1 byte U+0000 */
        {
            "\"\\u0000\"",
            "",                 /* bug: want overlong "\xC0\x80" */
203 204
            "\"\\u0000\"",
            "\xC0\x80",
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        },
        /* 2.1.2  2 bytes U+0080 */
        {
            "\"\xC2\x80\"",
            "\xC2\x80",
            "\"\\u0080\"",
        },
        /* 2.1.3  3 bytes U+0800 */
        {
            "\"\xE0\xA0\x80\"",
            "\xE0\xA0\x80",
            "\"\\u0800\"",
        },
        /* 2.1.4  4 bytes U+10000 */
        {
            "\"\xF0\x90\x80\x80\"",
            "\xF0\x90\x80\x80",
222
            "\"\\uD800\\uDC00\"",
223 224 225 226
        },
        /* 2.1.5  5 bytes U+200000 */
        {
            "\"\xF8\x88\x80\x80\x80\"",
227 228
            NULL,               /* bug: rejected */
            "\"\\uFFFD\"",
229 230 231 232 233
            "\xF8\x88\x80\x80\x80",
        },
        /* 2.1.6  6 bytes U+4000000 */
        {
            "\"\xFC\x84\x80\x80\x80\x80\"",
234 235
            NULL,               /* bug: rejected */
            "\"\\uFFFD\"",
236 237 238 239 240 241 242
            "\xFC\x84\x80\x80\x80\x80",
        },
        /* 2.2  Last possible sequence of a certain length */
        /* 2.2.1  1 byte U+007F */
        {
            "\"\x7F\"",
            "\x7F",
243
            "\"\\u007F\"",
244 245 246 247 248 249 250
        },
        /* 2.2.2  2 bytes U+07FF */
        {
            "\"\xDF\xBF\"",
            "\xDF\xBF",
            "\"\\u07FF\"",
        },
251 252 253 254 255 256 257 258 259
        /*
         * 2.2.3  3 bytes U+FFFC
         * The last possible sequence is actually U+FFFF.  But that's
         * a noncharacter, and already covered by its own test case
         * under 5.3.  Same for U+FFFE.  U+FFFD is the last character
         * in the BMP, and covered under 2.3.  Because of U+FFFD's
         * special role as replacement character, it's worth testing
         * U+FFFC here.
         */
260
        {
261 262 263
            "\"\xEF\xBF\xBC\"",
            "\xEF\xBF\xBC",
            "\"\\uFFFC\"",
264 265 266 267
        },
        /* 2.2.4  4 bytes U+1FFFFF */
        {
            "\"\xF7\xBF\xBF\xBF\"",
268 269
            NULL,               /* bug: rejected */
            "\"\\uFFFD\"",
270 271 272 273 274
            "\xF7\xBF\xBF\xBF",
        },
        /* 2.2.5  5 bytes U+3FFFFFF */
        {
            "\"\xFB\xBF\xBF\xBF\xBF\"",
275 276
            NULL,               /* bug: rejected */
            "\"\\uFFFD\"",
277 278 279 280 281
            "\xFB\xBF\xBF\xBF\xBF",
        },
        /* 2.2.6  6 bytes U+7FFFFFFF */
        {
            "\"\xFD\xBF\xBF\xBF\xBF\xBF\"",
282 283
            NULL,               /* bug: rejected */
            "\"\\uFFFD\"",
284 285 286 287
            "\xFD\xBF\xBF\xBF\xBF\xBF",
        },
        /* 2.3  Other boundary conditions */
        {
288
            /* last one before surrogate range: U+D7FF */
289 290 291 292 293
            "\"\xED\x9F\xBF\"",
            "\xED\x9F\xBF",
            "\"\\uD7FF\"",
        },
        {
294
            /* first one after surrogate range: U+E000 */
295 296 297 298 299
            "\"\xEE\x80\x80\"",
            "\xEE\x80\x80",
            "\"\\uE000\"",
        },
        {
300
            /* last one in BMP: U+FFFD */
301 302 303 304 305
            "\"\xEF\xBF\xBD\"",
            "\xEF\xBF\xBD",
            "\"\\uFFFD\"",
        },
        {
306 307 308
            /* last one in last plane: U+10FFFD */
            "\"\xF4\x8F\xBF\xBD\"",
            "\xF4\x8F\xBF\xBD",
309
            "\"\\uDBFF\\uDFFD\""
310 311
        },
        {
312
            /* first one beyond Unicode range: U+110000 */
313 314
            "\"\xF4\x90\x80\x80\"",
            "\xF4\x90\x80\x80",
315
            "\"\\uFFFD\"",
316 317 318 319 320 321 322
        },
        /* 3  Malformed sequences */
        /* 3.1  Unexpected continuation bytes */
        /* 3.1.1  First continuation byte */
        {
            "\"\x80\"",
            "\x80",             /* bug: not corrected */
323
            "\"\\uFFFD\"",
324 325 326 327 328
        },
        /* 3.1.2  Last continuation byte */
        {
            "\"\xBF\"",
            "\xBF",             /* bug: not corrected */
329
            "\"\\uFFFD\"",
330 331 332 333 334
        },
        /* 3.1.3  2 continuation bytes */
        {
            "\"\x80\xBF\"",
            "\x80\xBF",         /* bug: not corrected */
335
            "\"\\uFFFD\\uFFFD\"",
336 337 338 339 340
        },
        /* 3.1.4  3 continuation bytes */
        {
            "\"\x80\xBF\x80\"",
            "\x80\xBF\x80",     /* bug: not corrected */
341
            "\"\\uFFFD\\uFFFD\\uFFFD\"",
342 343 344 345 346
        },
        /* 3.1.5  4 continuation bytes */
        {
            "\"\x80\xBF\x80\xBF\"",
            "\x80\xBF\x80\xBF", /* bug: not corrected */
347
            "\"\\uFFFD\\uFFFD\\uFFFD\\uFFFD\"",
348 349 350 351 352
        },
        /* 3.1.6  5 continuation bytes */
        {
            "\"\x80\xBF\x80\xBF\x80\"",
            "\x80\xBF\x80\xBF\x80", /* bug: not corrected */
353
            "\"\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\"",
354 355 356 357 358
        },
        /* 3.1.7  6 continuation bytes */
        {
            "\"\x80\xBF\x80\xBF\x80\xBF\"",
            "\x80\xBF\x80\xBF\x80\xBF", /* bug: not corrected */
359
            "\"\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\"",
360 361 362 363 364
        },
        /* 3.1.8  7 continuation bytes */
        {
            "\"\x80\xBF\x80\xBF\x80\xBF\x80\"",
            "\x80\xBF\x80\xBF\x80\xBF\x80", /* bug: not corrected */
365
            "\"\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\"",
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
        },
        /* 3.1.9  Sequence of all 64 possible continuation bytes */
        {
            "\"\x80\x81\x82\x83\x84\x85\x86\x87"
            "\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F"
            "\x90\x91\x92\x93\x94\x95\x96\x97"
            "\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F"
            "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7"
            "\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF"
            "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7"
            "\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\"",
             /* bug: not corrected */
            "\x80\x81\x82\x83\x84\x85\x86\x87"
            "\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F"
            "\x90\x91\x92\x93\x94\x95\x96\x97"
            "\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F"
            "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7"
            "\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF"
            "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7"
            "\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF",
386 387 388 389 390 391 392 393
            "\"\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
            "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
            "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
            "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
            "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
            "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
            "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
            "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\""
394 395 396 397 398 399 400 401 402
        },
        /* 3.2  Lonely start characters */
        /* 3.2.1  All 32 first bytes of 2-byte sequences, followed by space */
        {
            "\"\xC0 \xC1 \xC2 \xC3 \xC4 \xC5 \xC6 \xC7 "
            "\xC8 \xC9 \xCA \xCB \xCC \xCD \xCE \xCF "
            "\xD0 \xD1 \xD2 \xD3 \xD4 \xD5 \xD6 \xD7 "
            "\xD8 \xD9 \xDA \xDB \xDC \xDD \xDE \xDF \"",
            NULL,               /* bug: rejected */
403 404 405 406
            "\"\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD "
            "\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD "
            "\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD "
            "\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \"",
407 408 409 410 411 412 413 414 415 416 417 418
            "\xC0 \xC1 \xC2 \xC3 \xC4 \xC5 \xC6 \xC7 "
            "\xC8 \xC9 \xCA \xCB \xCC \xCD \xCE \xCF "
            "\xD0 \xD1 \xD2 \xD3 \xD4 \xD5 \xD6 \xD7 "
            "\xD8 \xD9 \xDA \xDB \xDC \xDD \xDE \xDF ",
        },
        /* 3.2.2  All 16 first bytes of 3-byte sequences, followed by space */
        {
            "\"\xE0 \xE1 \xE2 \xE3 \xE4 \xE5 \xE6 \xE7 "
            "\xE8 \xE9 \xEA \xEB \xEC \xED \xEE \xEF \"",
            /* bug: not corrected */
            "\xE0 \xE1 \xE2 \xE3 \xE4 \xE5 \xE6 \xE7 "
            "\xE8 \xE9 \xEA \xEB \xEC \xED \xEE \xEF ",
419 420
            "\"\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD "
            "\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \"",
421 422 423 424 425
        },
        /* 3.2.3  All 8 first bytes of 4-byte sequences, followed by space */
        {
            "\"\xF0 \xF1 \xF2 \xF3 \xF4 \xF5 \xF6 \xF7 \"",
            NULL,               /* bug: rejected */
426
            "\"\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \"",
427 428 429 430 431 432
            "\xF0 \xF1 \xF2 \xF3 \xF4 \xF5 \xF6 \xF7 ",
        },
        /* 3.2.4  All 4 first bytes of 5-byte sequences, followed by space */
        {
            "\"\xF8 \xF9 \xFA \xFB \"",
            NULL,               /* bug: rejected */
433
            "\"\\uFFFD \\uFFFD \\uFFFD \\uFFFD \"",
434 435 436 437 438 439
            "\xF8 \xF9 \xFA \xFB ",
        },
        /* 3.2.5  All 2 first bytes of 6-byte sequences, followed by space */
        {
            "\"\xFC \xFD \"",
            NULL,               /* bug: rejected */
440
            "\"\\uFFFD \\uFFFD \"",
441 442 443 444 445 446 447
            "\xFC \xFD ",
        },
        /* 3.3  Sequences with last continuation byte missing */
        /* 3.3.1  2-byte sequence with last byte missing (U+0000) */
        {
            "\"\xC0\"",
            NULL,               /* bug: rejected */
448
            "\"\\uFFFD\"",
449 450 451 452 453 454
            "\xC0",
        },
        /* 3.3.2  3-byte sequence with last byte missing (U+0000) */
        {
            "\"\xE0\x80\"",
            "\xE0\x80",           /* bug: not corrected */
455
            "\"\\uFFFD\"",
456 457 458 459 460
        },
        /* 3.3.3  4-byte sequence with last byte missing (U+0000) */
        {
            "\"\xF0\x80\x80\"",
            "\xF0\x80\x80",     /* bug: not corrected */
461
            "\"\\uFFFD\"",
462 463 464
        },
        /* 3.3.4  5-byte sequence with last byte missing (U+0000) */
        {
465
            "\"\xF8\x80\x80\x80\"",
466
            NULL,                   /* bug: rejected */
467
            "\"\\uFFFD\"",
468 469 470 471 472 473
            "\xF8\x80\x80\x80",
        },
        /* 3.3.5  6-byte sequence with last byte missing (U+0000) */
        {
            "\"\xFC\x80\x80\x80\x80\"",
            NULL,                        /* bug: rejected */
474
            "\"\\uFFFD\"",
475 476 477 478 479 480
            "\xFC\x80\x80\x80\x80",
        },
        /* 3.3.6  2-byte sequence with last byte missing (U+07FF) */
        {
            "\"\xDF\"",
            "\xDF",             /* bug: not corrected */
481
            "\"\\uFFFD\"",
482 483 484 485 486
        },
        /* 3.3.7  3-byte sequence with last byte missing (U+FFFF) */
        {
            "\"\xEF\xBF\"",
            "\xEF\xBF",           /* bug: not corrected */
487
            "\"\\uFFFD\"",
488 489 490 491 492
        },
        /* 3.3.8  4-byte sequence with last byte missing (U+1FFFFF) */
        {
            "\"\xF7\xBF\xBF\"",
            NULL,               /* bug: rejected */
493
            "\"\\uFFFD\"",
494 495 496 497 498 499
            "\xF7\xBF\xBF",
        },
        /* 3.3.9  5-byte sequence with last byte missing (U+3FFFFFF) */
        {
            "\"\xFB\xBF\xBF\xBF\"",
            NULL,                 /* bug: rejected */
500
            "\"\\uFFFD\"",
501 502 503 504 505 506
            "\xFB\xBF\xBF\xBF",
        },
        /* 3.3.10  6-byte sequence with last byte missing (U+7FFFFFFF) */
        {
            "\"\xFD\xBF\xBF\xBF\xBF\"",
            NULL,                        /* bug: rejected */
507
            "\"\\uFFFD\"",
508 509 510 511 512 513 514
            "\xFD\xBF\xBF\xBF\xBF",
        },
        /* 3.4  Concatenation of incomplete sequences */
        {
            "\"\xC0\xE0\x80\xF0\x80\x80\xF8\x80\x80\x80\xFC\x80\x80\x80\x80"
            "\xDF\xEF\xBF\xF7\xBF\xBF\xFB\xBF\xBF\xBF\xFD\xBF\xBF\xBF\xBF\"",
            NULL,               /* bug: rejected */
515 516
            "\"\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
            "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\"",
517 518 519 520 521 522 523
            "\xC0\xE0\x80\xF0\x80\x80\xF8\x80\x80\x80\xFC\x80\x80\x80\x80"
            "\xDF\xEF\xBF\xF7\xBF\xBF\xFB\xBF\xBF\xBF\xFD\xBF\xBF\xBF\xBF",
        },
        /* 3.5  Impossible bytes */
        {
            "\"\xFE\"",
            NULL,               /* bug: rejected */
524
            "\"\\uFFFD\"",
525 526 527 528 529
            "\xFE",
        },
        {
            "\"\xFF\"",
            NULL,               /* bug: rejected */
530
            "\"\\uFFFD\"",
531 532 533 534 535
            "\xFF",
        },
        {
            "\"\xFE\xFE\xFF\xFF\"",
            NULL,                 /* bug: rejected */
536
            "\"\\uFFFD\\uFFFD\\uFFFD\\uFFFD\"",
537 538 539 540 541 542 543
            "\xFE\xFE\xFF\xFF",
        },
        /* 4  Overlong sequences */
        /* 4.1  Overlong '/' */
        {
            "\"\xC0\xAF\"",
            NULL,               /* bug: rejected */
544
            "\"\\uFFFD\"",
545 546 547 548 549
            "\xC0\xAF",
        },
        {
            "\"\xE0\x80\xAF\"",
            "\xE0\x80\xAF",     /* bug: not corrected */
550
            "\"\\uFFFD\"",
551 552 553 554
        },
        {
            "\"\xF0\x80\x80\xAF\"",
            "\xF0\x80\x80\xAF",  /* bug: not corrected */
555
            "\"\\uFFFD\"",
556 557 558 559
        },
        {
            "\"\xF8\x80\x80\x80\xAF\"",
            NULL,                        /* bug: rejected */
560
            "\"\\uFFFD\"",
561 562 563 564 565
            "\xF8\x80\x80\x80\xAF",
        },
        {
            "\"\xFC\x80\x80\x80\x80\xAF\"",
            NULL,                               /* bug: rejected */
566
            "\"\\uFFFD\"",
567 568
            "\xFC\x80\x80\x80\x80\xAF",
        },
569 570 571 572 573 574
        /*
         * 4.2  Maximum overlong sequences
         * Highest Unicode value that is still resulting in an
         * overlong sequence if represented with the given number of
         * bytes.  This is a boundary test for safe UTF-8 decoders.
         */
575 576 577 578
        {
            /* \U+007F */
            "\"\xC1\xBF\"",
            NULL,               /* bug: rejected */
579
            "\"\\uFFFD\"",
580 581 582 583 584 585
            "\xC1\xBF",
        },
        {
            /* \U+07FF */
            "\"\xE0\x9F\xBF\"",
            "\xE0\x9F\xBF",     /* bug: not corrected */
586
            "\"\\uFFFD\"",
587 588
        },
        {
589 590 591 592 593 594 595 596
            /*
             * \U+FFFC
             * The actual maximum would be U+FFFF, but that's a
             * noncharacter.  Testing U+FFFC seems more useful.  See
             * also 2.2.3
             */
            "\"\xF0\x8F\xBF\xBC\"",
            "\xF0\x8F\xBF\xBC",   /* bug: not corrected */
597
            "\"\\uFFFD\"",
598 599 600 601 602
        },
        {
            /* \U+1FFFFF */
            "\"\xF8\x87\xBF\xBF\xBF\"",
            NULL,                        /* bug: rejected */
603
            "\"\\uFFFD\"",
604 605 606 607 608 609
            "\xF8\x87\xBF\xBF\xBF",
        },
        {
            /* \U+3FFFFFF */
            "\"\xFC\x83\xBF\xBF\xBF\xBF\"",
            NULL,                               /* bug: rejected */
610
            "\"\\uFFFD\"",
611 612 613 614 615 616 617 618 619 620 621 622 623 624
            "\xFC\x83\xBF\xBF\xBF\xBF",
        },
        /* 4.3  Overlong representation of the NUL character */
        {
            /* \U+0000 */
            "\"\xC0\x80\"",
            NULL,               /* bug: rejected */
            "\"\\u0000\"",
            "\xC0\x80",
        },
        {
            /* \U+0000 */
            "\"\xE0\x80\x80\"",
            "\xE0\x80\x80",     /* bug: not corrected */
625
            "\"\\uFFFD\"",
626 627 628 629 630
        },
        {
            /* \U+0000 */
            "\"\xF0\x80\x80\x80\"",
            "\xF0\x80\x80\x80",   /* bug: not corrected */
631
            "\"\\uFFFD\"",
632 633 634 635 636
        },
        {
            /* \U+0000 */
            "\"\xF8\x80\x80\x80\x80\"",
            NULL,                        /* bug: rejected */
637
            "\"\\uFFFD\"",
638 639 640 641 642 643
            "\xF8\x80\x80\x80\x80",
        },
        {
            /* \U+0000 */
            "\"\xFC\x80\x80\x80\x80\x80\"",
            NULL,                               /* bug: rejected */
644
            "\"\\uFFFD\"",
645 646 647 648 649 650 651 652
            "\xFC\x80\x80\x80\x80\x80",
        },
        /* 5  Illegal code positions */
        /* 5.1  Single UTF-16 surrogates */
        {
            /* \U+D800 */
            "\"\xED\xA0\x80\"",
            "\xED\xA0\x80",     /* bug: not corrected */
653
            "\"\\uFFFD\"",
654 655 656 657 658
        },
        {
            /* \U+DB7F */
            "\"\xED\xAD\xBF\"",
            "\xED\xAD\xBF",     /* bug: not corrected */
659
            "\"\\uFFFD\"",
660 661 662 663 664
        },
        {
            /* \U+DB80 */
            "\"\xED\xAE\x80\"",
            "\xED\xAE\x80",     /* bug: not corrected */
665
            "\"\\uFFFD\"",
666 667 668 669 670
        },
        {
            /* \U+DBFF */
            "\"\xED\xAF\xBF\"",
            "\xED\xAF\xBF",     /* bug: not corrected */
671
            "\"\\uFFFD\"",
672 673 674 675 676
        },
        {
            /* \U+DC00 */
            "\"\xED\xB0\x80\"",
            "\xED\xB0\x80",     /* bug: not corrected */
677
            "\"\\uFFFD\"",
678 679 680 681 682
        },
        {
            /* \U+DF80 */
            "\"\xED\xBE\x80\"",
            "\xED\xBE\x80",     /* bug: not corrected */
683
            "\"\\uFFFD\"",
684 685 686 687 688
        },
        {
            /* \U+DFFF */
            "\"\xED\xBF\xBF\"",
            "\xED\xBF\xBF",     /* bug: not corrected */
689
            "\"\\uFFFD\"",
690 691 692 693 694 695
        },
        /* 5.2  Paired UTF-16 surrogates */
        {
            /* \U+D800\U+DC00 */
            "\"\xED\xA0\x80\xED\xB0\x80\"",
            "\xED\xA0\x80\xED\xB0\x80", /* bug: not corrected */
696
            "\"\\uFFFD\\uFFFD\"",
697 698 699 700 701
        },
        {
            /* \U+D800\U+DFFF */
            "\"\xED\xA0\x80\xED\xBF\xBF\"",
            "\xED\xA0\x80\xED\xBF\xBF", /* bug: not corrected */
702
            "\"\\uFFFD\\uFFFD\"",
703 704 705 706 707
        },
        {
            /* \U+DB7F\U+DC00 */
            "\"\xED\xAD\xBF\xED\xB0\x80\"",
            "\xED\xAD\xBF\xED\xB0\x80", /* bug: not corrected */
708
            "\"\\uFFFD\\uFFFD\"",
709 710 711 712 713
        },
        {
            /* \U+DB7F\U+DFFF */
            "\"\xED\xAD\xBF\xED\xBF\xBF\"",
            "\xED\xAD\xBF\xED\xBF\xBF", /* bug: not corrected */
714
            "\"\\uFFFD\\uFFFD\"",
715 716 717 718 719
        },
        {
            /* \U+DB80\U+DC00 */
            "\"\xED\xAE\x80\xED\xB0\x80\"",
            "\xED\xAE\x80\xED\xB0\x80", /* bug: not corrected */
720
            "\"\\uFFFD\\uFFFD\"",
721 722 723 724 725
        },
        {
            /* \U+DB80\U+DFFF */
            "\"\xED\xAE\x80\xED\xBF\xBF\"",
            "\xED\xAE\x80\xED\xBF\xBF", /* bug: not corrected */
726
            "\"\\uFFFD\\uFFFD\"",
727 728 729 730 731
        },
        {
            /* \U+DBFF\U+DC00 */
            "\"\xED\xAF\xBF\xED\xB0\x80\"",
            "\xED\xAF\xBF\xED\xB0\x80", /* bug: not corrected */
732
            "\"\\uFFFD\\uFFFD\"",
733 734 735 736 737
        },
        {
            /* \U+DBFF\U+DFFF */
            "\"\xED\xAF\xBF\xED\xBF\xBF\"",
            "\xED\xAF\xBF\xED\xBF\xBF", /* bug: not corrected */
738
            "\"\\uFFFD\\uFFFD\"",
739 740
        },
        /* 5.3  Other illegal code positions */
741
        /* BMP noncharacters */
742 743 744 745
        {
            /* \U+FFFE */
            "\"\xEF\xBF\xBE\"",
            "\xEF\xBF\xBE",     /* bug: not corrected */
746
            "\"\\uFFFD\"",
747 748 749 750 751
        },
        {
            /* \U+FFFF */
            "\"\xEF\xBF\xBF\"",
            "\xEF\xBF\xBF",     /* bug: not corrected */
752
            "\"\\uFFFD\"",
753
        },
754 755 756 757
        {
            /* U+FDD0 */
            "\"\xEF\xB7\x90\"",
            "\xEF\xB7\x90",     /* bug: not corrected */
758
            "\"\\uFFFD\"",
759 760 761 762 763
        },
        {
            /* U+FDEF */
            "\"\xEF\xB7\xAF\"",
            "\xEF\xB7\xAF",     /* bug: not corrected */
764
            "\"\\uFFFD\"",
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
        },
        /* Plane 1 .. 16 noncharacters */
        {
            /* U+1FFFE U+1FFFF U+2FFFE U+2FFFF ... U+10FFFE U+10FFFF */
            "\"\xF0\x9F\xBF\xBE\xF0\x9F\xBF\xBF"
            "\xF0\xAF\xBF\xBE\xF0\xAF\xBF\xBF"
            "\xF0\xBF\xBF\xBE\xF0\xBF\xBF\xBF"
            "\xF1\x8F\xBF\xBE\xF1\x8F\xBF\xBF"
            "\xF1\x9F\xBF\xBE\xF1\x9F\xBF\xBF"
            "\xF1\xAF\xBF\xBE\xF1\xAF\xBF\xBF"
            "\xF1\xBF\xBF\xBE\xF1\xBF\xBF\xBF"
            "\xF2\x8F\xBF\xBE\xF2\x8F\xBF\xBF"
            "\xF2\x9F\xBF\xBE\xF2\x9F\xBF\xBF"
            "\xF2\xAF\xBF\xBE\xF2\xAF\xBF\xBF"
            "\xF2\xBF\xBF\xBE\xF2\xBF\xBF\xBF"
            "\xF3\x8F\xBF\xBE\xF3\x8F\xBF\xBF"
            "\xF3\x9F\xBF\xBE\xF3\x9F\xBF\xBF"
            "\xF3\xAF\xBF\xBE\xF3\xAF\xBF\xBF"
            "\xF3\xBF\xBF\xBE\xF3\xBF\xBF\xBF"
            "\xF4\x8F\xBF\xBE\xF4\x8F\xBF\xBF\"",
            /* bug: not corrected */
            "\xF0\x9F\xBF\xBE\xF0\x9F\xBF\xBF"
            "\xF0\xAF\xBF\xBE\xF0\xAF\xBF\xBF"
            "\xF0\xBF\xBF\xBE\xF0\xBF\xBF\xBF"
            "\xF1\x8F\xBF\xBE\xF1\x8F\xBF\xBF"
            "\xF1\x9F\xBF\xBE\xF1\x9F\xBF\xBF"
            "\xF1\xAF\xBF\xBE\xF1\xAF\xBF\xBF"
            "\xF1\xBF\xBF\xBE\xF1\xBF\xBF\xBF"
            "\xF2\x8F\xBF\xBE\xF2\x8F\xBF\xBF"
            "\xF2\x9F\xBF\xBE\xF2\x9F\xBF\xBF"
            "\xF2\xAF\xBF\xBE\xF2\xAF\xBF\xBF"
            "\xF2\xBF\xBF\xBE\xF2\xBF\xBF\xBF"
            "\xF3\x8F\xBF\xBE\xF3\x8F\xBF\xBF"
            "\xF3\x9F\xBF\xBE\xF3\x9F\xBF\xBF"
            "\xF3\xAF\xBF\xBE\xF3\xAF\xBF\xBF"
            "\xF3\xBF\xBF\xBE\xF3\xBF\xBF\xBF"
            "\xF4\x8F\xBF\xBE\xF4\x8F\xBF\xBF",
802 803 804 805
            "\"\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
            "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
            "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
            "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\"",
806
        },
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
        {}
    };
    int i;
    QObject *obj;
    QString *str;
    const char *json_in, *utf8_out, *utf8_in, *json_out;

    for (i = 0; test_cases[i].json_in; i++) {
        json_in = test_cases[i].json_in;
        utf8_out = test_cases[i].utf8_out;
        utf8_in = test_cases[i].utf8_in ?: test_cases[i].utf8_out;
        json_out = test_cases[i].json_out ?: test_cases[i].json_in;

        obj = qobject_from_json(json_in);
        if (utf8_out) {
            g_assert(obj);
            g_assert(qobject_type(obj) == QTYPE_QSTRING);
            str = qobject_to_qstring(obj);
            g_assert_cmpstr(qstring_get_str(str), ==, utf8_out);
        } else {
            g_assert(!obj);
        }
        qobject_decref(obj);

        obj = QOBJECT(qstring_from_str(utf8_in));
        str = qobject_to_json(obj);
        if (json_out) {
            g_assert(str);
            g_assert_cmpstr(qstring_get_str(str), ==, json_out);
        } else {
            g_assert(!str);
        }
        QDECREF(str);
        qobject_decref(obj);

        /*
843 844
         * Disabled, because qobject_from_json() is buggy, and I can't
         * be bothered to add the expected incorrect results.
845 846 847 848 849 850 851 852 853 854 855 856
         * FIXME Enable once these bugs have been fixed.
         */
        if (0 && json_out != json_in) {
            obj = qobject_from_json(json_out);
            g_assert(obj);
            g_assert(qobject_type(obj) == QTYPE_QSTRING);
            str = qobject_to_qstring(obj);
            g_assert_cmpstr(qstring_get_str(str), ==, utf8_out);
        }
    }
}

A
Anthony Liguori 已提交
857
static void vararg_string(void)
A
Anthony Liguori 已提交
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
{
    int i;
    struct {
        const char *decoded;
    } test_cases[] = {
        { "hello world" },
        { "the quick brown fox jumped over the fence" },
        {}
    };

    for (i = 0; test_cases[i].decoded; i++) {
        QObject *obj;
        QString *str;

        obj = qobject_from_jsonf("%s", test_cases[i].decoded);

A
Anthony Liguori 已提交
874 875
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QSTRING);
A
Anthony Liguori 已提交
876 877
        
        str = qobject_to_qstring(obj);
A
Anthony Liguori 已提交
878
        g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
A
Anthony Liguori 已提交
879 880 881 882 883

        QDECREF(str);
    }
}

A
Anthony Liguori 已提交
884
static void simple_number(void)
A
Anthony Liguori 已提交
885 886 887 888 889
{
    int i;
    struct {
        const char *encoded;
        int64_t decoded;
890
        int skip;
A
Anthony Liguori 已提交
891 892 893 894 895
    } test_cases[] = {
        { "0", 0 },
        { "1234", 1234 },
        { "1", 1 },
        { "-32", -32 },
896
        { "-0", 0, .skip = 1 },
A
Anthony Liguori 已提交
897 898 899 900 901 902 903 904
        { },
    };

    for (i = 0; test_cases[i].encoded; i++) {
        QObject *obj;
        QInt *qint;

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
905 906
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QINT);
A
Anthony Liguori 已提交
907 908

        qint = qobject_to_qint(obj);
A
Anthony Liguori 已提交
909
        g_assert(qint_get_int(qint) == test_cases[i].decoded);
910 911 912 913
        if (test_cases[i].skip == 0) {
            QString *str;

            str = qobject_to_json(obj);
A
Anthony Liguori 已提交
914
            g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
915 916
            QDECREF(str);
        }
A
Anthony Liguori 已提交
917 918 919 920 921

        QDECREF(qint);
    }
}

A
Anthony Liguori 已提交
922
static void float_number(void)
A
Anthony Liguori 已提交
923 924 925 926 927
{
    int i;
    struct {
        const char *encoded;
        double decoded;
928
        int skip;
A
Anthony Liguori 已提交
929 930 931 932
    } test_cases[] = {
        { "32.43", 32.43 },
        { "0.222", 0.222 },
        { "-32.12313", -32.12313 },
933
        { "-32.20e-10", -32.20e-10, .skip = 1 },
A
Anthony Liguori 已提交
934 935 936 937 938 939 940 941
        { },
    };

    for (i = 0; test_cases[i].encoded; i++) {
        QObject *obj;
        QFloat *qfloat;

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
942 943
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QFLOAT);
A
Anthony Liguori 已提交
944 945

        qfloat = qobject_to_qfloat(obj);
A
Anthony Liguori 已提交
946
        g_assert(qfloat_get_double(qfloat) == test_cases[i].decoded);
A
Anthony Liguori 已提交
947

948 949 950 951
        if (test_cases[i].skip == 0) {
            QString *str;

            str = qobject_to_json(obj);
A
Anthony Liguori 已提交
952
            g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
953 954 955
            QDECREF(str);
        }

A
Anthony Liguori 已提交
956 957 958 959
        QDECREF(qfloat);
    }
}

A
Anthony Liguori 已提交
960
static void vararg_number(void)
A
Anthony Liguori 已提交
961 962 963 964 965 966 967 968 969
{
    QObject *obj;
    QInt *qint;
    QFloat *qfloat;
    int value = 0x2342;
    int64_t value64 = 0x2342342343LL;
    double valuef = 2.323423423;

    obj = qobject_from_jsonf("%d", value);
A
Anthony Liguori 已提交
970 971
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QINT);
A
Anthony Liguori 已提交
972 973

    qint = qobject_to_qint(obj);
A
Anthony Liguori 已提交
974
    g_assert(qint_get_int(qint) == value);
A
Anthony Liguori 已提交
975 976 977 978

    QDECREF(qint);

    obj = qobject_from_jsonf("%" PRId64, value64);
A
Anthony Liguori 已提交
979 980
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QINT);
A
Anthony Liguori 已提交
981 982

    qint = qobject_to_qint(obj);
A
Anthony Liguori 已提交
983
    g_assert(qint_get_int(qint) == value64);
A
Anthony Liguori 已提交
984 985 986 987

    QDECREF(qint);

    obj = qobject_from_jsonf("%f", valuef);
A
Anthony Liguori 已提交
988 989
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QFLOAT);
A
Anthony Liguori 已提交
990 991

    qfloat = qobject_to_qfloat(obj);
A
Anthony Liguori 已提交
992
    g_assert(qfloat_get_double(qfloat) == valuef);
A
Anthony Liguori 已提交
993 994 995 996

    QDECREF(qfloat);
}

A
Anthony Liguori 已提交
997
static void keyword_literal(void)
A
Anthony Liguori 已提交
998 999 1000
{
    QObject *obj;
    QBool *qbool;
1001
    QString *str;
A
Anthony Liguori 已提交
1002 1003

    obj = qobject_from_json("true");
A
Anthony Liguori 已提交
1004 1005
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QBOOL);
A
Anthony Liguori 已提交
1006 1007

    qbool = qobject_to_qbool(obj);
A
Anthony Liguori 已提交
1008
    g_assert(qbool_get_int(qbool) != 0);
A
Anthony Liguori 已提交
1009

1010
    str = qobject_to_json(obj);
A
Anthony Liguori 已提交
1011
    g_assert(strcmp(qstring_get_str(str), "true") == 0);
1012 1013
    QDECREF(str);

A
Anthony Liguori 已提交
1014 1015 1016
    QDECREF(qbool);

    obj = qobject_from_json("false");
A
Anthony Liguori 已提交
1017 1018
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QBOOL);
A
Anthony Liguori 已提交
1019 1020

    qbool = qobject_to_qbool(obj);
A
Anthony Liguori 已提交
1021
    g_assert(qbool_get_int(qbool) == 0);
A
Anthony Liguori 已提交
1022

1023
    str = qobject_to_json(obj);
A
Anthony Liguori 已提交
1024
    g_assert(strcmp(qstring_get_str(str), "false") == 0);
1025 1026
    QDECREF(str);

A
Anthony Liguori 已提交
1027 1028 1029
    QDECREF(qbool);

    obj = qobject_from_jsonf("%i", false);
A
Anthony Liguori 已提交
1030 1031
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QBOOL);
A
Anthony Liguori 已提交
1032 1033

    qbool = qobject_to_qbool(obj);
A
Anthony Liguori 已提交
1034
    g_assert(qbool_get_int(qbool) == 0);
A
Anthony Liguori 已提交
1035 1036 1037 1038

    QDECREF(qbool);
    
    obj = qobject_from_jsonf("%i", true);
A
Anthony Liguori 已提交
1039 1040
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QBOOL);
A
Anthony Liguori 已提交
1041 1042

    qbool = qobject_to_qbool(obj);
A
Anthony Liguori 已提交
1043
    g_assert(qbool_get_int(qbool) != 0);
A
Anthony Liguori 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139

    QDECREF(qbool);
}

typedef struct LiteralQDictEntry LiteralQDictEntry;
typedef struct LiteralQObject LiteralQObject;

struct LiteralQObject
{
    int type;
    union {
        int64_t qint;
        const char *qstr;
        LiteralQDictEntry *qdict;
        LiteralQObject *qlist;
    } value;
};

struct LiteralQDictEntry
{
    const char *key;
    LiteralQObject value;
};

#define QLIT_QINT(val) (LiteralQObject){.type = QTYPE_QINT, .value.qint = (val)}
#define QLIT_QSTR(val) (LiteralQObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
#define QLIT_QDICT(val) (LiteralQObject){.type = QTYPE_QDICT, .value.qdict = (val)}
#define QLIT_QLIST(val) (LiteralQObject){.type = QTYPE_QLIST, .value.qlist = (val)}

typedef struct QListCompareHelper
{
    int index;
    LiteralQObject *objs;
    int result;
} QListCompareHelper;

static int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs);

static void compare_helper(QObject *obj, void *opaque)
{
    QListCompareHelper *helper = opaque;

    if (helper->result == 0) {
        return;
    }

    if (helper->objs[helper->index].type == QTYPE_NONE) {
        helper->result = 0;
        return;
    }

    helper->result = compare_litqobj_to_qobj(&helper->objs[helper->index++], obj);
}

static int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs)
{
    if (lhs->type != qobject_type(rhs)) {
        return 0;
    }

    switch (lhs->type) {
    case QTYPE_QINT:
        return lhs->value.qint == qint_get_int(qobject_to_qint(rhs));
    case QTYPE_QSTRING:
        return (strcmp(lhs->value.qstr, qstring_get_str(qobject_to_qstring(rhs))) == 0);
    case QTYPE_QDICT: {
        int i;

        for (i = 0; lhs->value.qdict[i].key; i++) {
            QObject *obj = qdict_get(qobject_to_qdict(rhs), lhs->value.qdict[i].key);

            if (!compare_litqobj_to_qobj(&lhs->value.qdict[i].value, obj)) {
                return 0;
            }
        }

        return 1;
    }
    case QTYPE_QLIST: {
        QListCompareHelper helper;

        helper.index = 0;
        helper.objs = lhs->value.qlist;
        helper.result = 1;
        
        qlist_iter(qobject_to_qlist(rhs), compare_helper, &helper);

        return helper.result;
    }
    default:
        break;
    }

    return 0;
}

A
Anthony Liguori 已提交
1140
static void simple_dict(void)
A
Anthony Liguori 已提交
1141 1142 1143 1144 1145 1146 1147
{
    int i;
    struct {
        const char *encoded;
        LiteralQObject decoded;
    } test_cases[] = {
        {
1148
            .encoded = "{\"foo\": 42, \"bar\": \"hello world\"}",
A
Anthony Liguori 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
                        { "foo", QLIT_QINT(42) },
                        { "bar", QLIT_QSTR("hello world") },
                        { }
                    })),
        }, {
            .encoded = "{}",
            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
                        { }
                    })),
        }, {
1160
            .encoded = "{\"foo\": 43}",
A
Anthony Liguori 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
                        { "foo", QLIT_QINT(43) },
                        { }
                    })),
        },
        { }
    };

    for (i = 0; test_cases[i].encoded; i++) {
        QObject *obj;
1171
        QString *str;
A
Anthony Liguori 已提交
1172 1173

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
1174 1175
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QDICT);
A
Anthony Liguori 已提交
1176

A
Anthony Liguori 已提交
1177
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
A
Anthony Liguori 已提交
1178

1179 1180 1181 1182
        str = qobject_to_json(obj);
        qobject_decref(obj);

        obj = qobject_from_json(qstring_get_str(str));
A
Anthony Liguori 已提交
1183 1184
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QDICT);
1185

A
Anthony Liguori 已提交
1186
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
A
Anthony Liguori 已提交
1187
        qobject_decref(obj);
1188
        QDECREF(str);
A
Anthony Liguori 已提交
1189 1190 1191
    }
}

1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
/*
 * this generates json of the form:
 * a(0,m) = [0, 1, ..., m-1]
 * a(n,m) = {
 *            'key0': a(0,m),
 *            'key1': a(1,m),
 *            ...
 *            'key(n-1)': a(n-1,m)
 *          }
 */
static void gen_test_json(GString *gstr, int nest_level_max,
                          int elem_count)
{
    int i;

    g_assert(gstr);
    if (nest_level_max == 0) {
        g_string_append(gstr, "[");
        for (i = 0; i < elem_count; i++) {
            g_string_append_printf(gstr, "%d", i);
            if (i < elem_count - 1) {
                g_string_append_printf(gstr, ", ");
            }
        }
        g_string_append(gstr, "]");
        return;
    }

    g_string_append(gstr, "{");
    for (i = 0; i < nest_level_max; i++) {
        g_string_append_printf(gstr, "'key%d': ", i);
        gen_test_json(gstr, i, elem_count);
        if (i < nest_level_max - 1) {
            g_string_append(gstr, ",");
        }
    }
    g_string_append(gstr, "}");
}

static void large_dict(void)
{
    GString *gstr = g_string_new("");
    QObject *obj;

    gen_test_json(gstr, 10, 100);
    obj = qobject_from_json(gstr->str);
    g_assert(obj != NULL);

    qobject_decref(obj);
    g_string_free(gstr, true);
}

A
Anthony Liguori 已提交
1244
static void simple_list(void)
A
Anthony Liguori 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
{
    int i;
    struct {
        const char *encoded;
        LiteralQObject decoded;
    } test_cases[] = {
        {
            .encoded = "[43,42]",
            .decoded = QLIT_QLIST(((LiteralQObject[]){
                        QLIT_QINT(43),
                        QLIT_QINT(42),
                        { }
                    })),
        },
        {
            .encoded = "[43]",
            .decoded = QLIT_QLIST(((LiteralQObject[]){
                        QLIT_QINT(43),
                        { }
                    })),
        },
        {
            .encoded = "[]",
            .decoded = QLIT_QLIST(((LiteralQObject[]){
                        { }
                    })),
        },
        {
            .encoded = "[{}]",
            .decoded = QLIT_QLIST(((LiteralQObject[]){
                        QLIT_QDICT(((LiteralQDictEntry[]){
                                    {},
                                        })),
                        {},
                            })),
        },
        { }
    };

    for (i = 0; test_cases[i].encoded; i++) {
        QObject *obj;
1286
        QString *str;
A
Anthony Liguori 已提交
1287 1288

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
1289 1290
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
A
Anthony Liguori 已提交
1291

A
Anthony Liguori 已提交
1292
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
A
Anthony Liguori 已提交
1293

1294 1295 1296 1297
        str = qobject_to_json(obj);
        qobject_decref(obj);

        obj = qobject_from_json(qstring_get_str(str));
A
Anthony Liguori 已提交
1298 1299
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
1300

A
Anthony Liguori 已提交
1301
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
A
Anthony Liguori 已提交
1302
        qobject_decref(obj);
1303
        QDECREF(str);
A
Anthony Liguori 已提交
1304 1305 1306
    }
}

A
Anthony Liguori 已提交
1307
static void simple_whitespace(void)
A
Anthony Liguori 已提交
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
{
    int i;
    struct {
        const char *encoded;
        LiteralQObject decoded;
    } test_cases[] = {
        {
            .encoded = " [ 43 , 42 ]",
            .decoded = QLIT_QLIST(((LiteralQObject[]){
                        QLIT_QINT(43),
                        QLIT_QINT(42),
                        { }
                    })),
        },
        {
            .encoded = " [ 43 , { 'h' : 'b' }, [ ], 42 ]",
            .decoded = QLIT_QLIST(((LiteralQObject[]){
                        QLIT_QINT(43),
                        QLIT_QDICT(((LiteralQDictEntry[]){
                                    { "h", QLIT_QSTR("b") },
                                    { }})),
                        QLIT_QLIST(((LiteralQObject[]){
                                    { }})),
                        QLIT_QINT(42),
                        { }
                    })),
        },
        {
            .encoded = " [ 43 , { 'h' : 'b' , 'a' : 32 }, [ ], 42 ]",
            .decoded = QLIT_QLIST(((LiteralQObject[]){
                        QLIT_QINT(43),
                        QLIT_QDICT(((LiteralQDictEntry[]){
                                    { "h", QLIT_QSTR("b") },
                                    { "a", QLIT_QINT(32) },
                                    { }})),
                        QLIT_QLIST(((LiteralQObject[]){
                                    { }})),
                        QLIT_QINT(42),
                        { }
                    })),
        },
        { }
    };

    for (i = 0; test_cases[i].encoded; i++) {
        QObject *obj;
1354
        QString *str;
A
Anthony Liguori 已提交
1355 1356

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
1357 1358
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
A
Anthony Liguori 已提交
1359

A
Anthony Liguori 已提交
1360
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
A
Anthony Liguori 已提交
1361

1362
        str = qobject_to_json(obj);
A
Anthony Liguori 已提交
1363
        qobject_decref(obj);
1364 1365

        obj = qobject_from_json(qstring_get_str(str));
A
Anthony Liguori 已提交
1366 1367
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
1368

A
Anthony Liguori 已提交
1369
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
1370 1371 1372

        qobject_decref(obj);
        QDECREF(str);
A
Anthony Liguori 已提交
1373 1374 1375
    }
}

A
Anthony Liguori 已提交
1376
static void simple_varargs(void)
A
Anthony Liguori 已提交
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
{
    QObject *embedded_obj;
    QObject *obj;
    LiteralQObject decoded = QLIT_QLIST(((LiteralQObject[]){
            QLIT_QINT(1),
            QLIT_QINT(2),
            QLIT_QLIST(((LiteralQObject[]){
                        QLIT_QINT(32),
                        QLIT_QINT(42),
                        {}})),
            {}}));

    embedded_obj = qobject_from_json("[32, 42]");
A
Anthony Liguori 已提交
1390
    g_assert(embedded_obj != NULL);
A
Anthony Liguori 已提交
1391 1392

    obj = qobject_from_jsonf("[%d, 2, %p]", 1, embedded_obj);
A
Anthony Liguori 已提交
1393
    g_assert(obj != NULL);
A
Anthony Liguori 已提交
1394

A
Anthony Liguori 已提交
1395
    g_assert(compare_litqobj_to_qobj(&decoded, obj) == 1);
A
Anthony Liguori 已提交
1396 1397 1398 1399

    qobject_decref(obj);
}

A
Anthony Liguori 已提交
1400
static void empty_input(void)
P
Paolo Bonzini 已提交
1401
{
1402 1403 1404
    const char *empty = "";

    QObject *obj = qobject_from_json(empty);
A
Anthony Liguori 已提交
1405
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1406 1407
}

A
Anthony Liguori 已提交
1408
static void unterminated_string(void)
P
Paolo Bonzini 已提交
1409 1410
{
    QObject *obj = qobject_from_json("\"abc");
A
Anthony Liguori 已提交
1411
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1412 1413
}

A
Anthony Liguori 已提交
1414
static void unterminated_sq_string(void)
P
Paolo Bonzini 已提交
1415 1416
{
    QObject *obj = qobject_from_json("'abc");
A
Anthony Liguori 已提交
1417
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1418 1419
}

A
Anthony Liguori 已提交
1420
static void unterminated_escape(void)
P
Paolo Bonzini 已提交
1421 1422
{
    QObject *obj = qobject_from_json("\"abc\\\"");
A
Anthony Liguori 已提交
1423
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1424 1425
}

A
Anthony Liguori 已提交
1426
static void unterminated_array(void)
P
Paolo Bonzini 已提交
1427 1428
{
    QObject *obj = qobject_from_json("[32");
A
Anthony Liguori 已提交
1429
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1430 1431
}

A
Anthony Liguori 已提交
1432
static void unterminated_array_comma(void)
P
Paolo Bonzini 已提交
1433 1434
{
    QObject *obj = qobject_from_json("[32,");
A
Anthony Liguori 已提交
1435
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1436 1437
}

A
Anthony Liguori 已提交
1438
static void invalid_array_comma(void)
P
Paolo Bonzini 已提交
1439 1440
{
    QObject *obj = qobject_from_json("[32,}");
A
Anthony Liguori 已提交
1441
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1442 1443
}

A
Anthony Liguori 已提交
1444
static void unterminated_dict(void)
P
Paolo Bonzini 已提交
1445 1446
{
    QObject *obj = qobject_from_json("{'abc':32");
A
Anthony Liguori 已提交
1447
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1448 1449
}

A
Anthony Liguori 已提交
1450
static void unterminated_dict_comma(void)
P
Paolo Bonzini 已提交
1451 1452
{
    QObject *obj = qobject_from_json("{'abc':32,");
A
Anthony Liguori 已提交
1453
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1454 1455
}

A
Anthony Liguori 已提交
1456
static void invalid_dict_comma(void)
P
Paolo Bonzini 已提交
1457 1458
{
    QObject *obj = qobject_from_json("{'abc':32,}");
A
Anthony Liguori 已提交
1459
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1460 1461
}

A
Anthony Liguori 已提交
1462
static void unterminated_literal(void)
P
Paolo Bonzini 已提交
1463 1464
{
    QObject *obj = qobject_from_json("nul");
A
Anthony Liguori 已提交
1465
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1466 1467
}

A
Anthony Liguori 已提交
1468
int main(int argc, char **argv)
A
Anthony Liguori 已提交
1469
{
A
Anthony Liguori 已提交
1470 1471 1472 1473
    g_test_init(&argc, &argv, NULL);

    g_test_add_func("/literals/string/simple", simple_string);
    g_test_add_func("/literals/string/escaped", escaped_string);
1474
    g_test_add_func("/literals/string/utf8", utf8_string);
A
Anthony Liguori 已提交
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
    g_test_add_func("/literals/string/single_quote", single_quote_string);
    g_test_add_func("/literals/string/vararg", vararg_string);

    g_test_add_func("/literals/number/simple", simple_number);
    g_test_add_func("/literals/number/float", float_number);
    g_test_add_func("/literals/number/vararg", vararg_number);

    g_test_add_func("/literals/keyword", keyword_literal);

    g_test_add_func("/dicts/simple_dict", simple_dict);
1485
    g_test_add_func("/dicts/large_dict", large_dict);
A
Anthony Liguori 已提交
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
    g_test_add_func("/lists/simple_list", simple_list);

    g_test_add_func("/whitespace/simple_whitespace", simple_whitespace);

    g_test_add_func("/varargs/simple_varargs", simple_varargs);

    g_test_add_func("/errors/empty_input", empty_input);
    g_test_add_func("/errors/unterminated/string", unterminated_string);
    g_test_add_func("/errors/unterminated/escape", unterminated_escape);
    g_test_add_func("/errors/unterminated/sq_string", unterminated_sq_string);
    g_test_add_func("/errors/unterminated/array", unterminated_array);
    g_test_add_func("/errors/unterminated/array_comma", unterminated_array_comma);
    g_test_add_func("/errors/unterminated/dict", unterminated_dict);
    g_test_add_func("/errors/unterminated/dict_comma", unterminated_dict_comma);
    g_test_add_func("/errors/invalid_array_comma", invalid_array_comma);
    g_test_add_func("/errors/invalid_dict_comma", invalid_dict_comma);
    g_test_add_func("/errors/unterminated/literal", unterminated_literal);
P
Paolo Bonzini 已提交
1503

A
Anthony Liguori 已提交
1504
    return g_test_run();
A
Anthony Liguori 已提交
1505
}