check-qjson.c 46.0 KB
Newer Older
A
Anthony Liguori 已提交
1 2
/*
 * Copyright IBM, Corp. 2009
E
Eric Blake 已提交
3
 * Copyright (c) 2013, 2015 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.
 *
 */
P
Peter Maydell 已提交
13
#include "qemu/osdep.h"
A
Anthony Liguori 已提交
14

E
Eric Blake 已提交
15
#include "qapi/qmp/types.h"
16
#include "qapi/qmp/qjson.h"
A
Anthony Liguori 已提交
17 18
#include "qemu-common.h"

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

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

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

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

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

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

A
Anthony Liguori 已提交
74
static void simple_string(void)
A
Anthony Liguori 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
{
    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 已提交
93 94
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QSTRING);
A
Anthony Liguori 已提交
95 96
        
        str = qobject_to_qstring(obj);
A
Anthony Liguori 已提交
97
        g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
A
Anthony Liguori 已提交
98

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

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

A
Anthony Liguori 已提交
108
static void single_quote_string(void)
A
Anthony Liguori 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
{
    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 已提交
127 128
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QSTRING);
A
Anthony Liguori 已提交
129 130
        
        str = qobject_to_qstring(obj);
A
Anthony Liguori 已提交
131
        g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
A
Anthony Liguori 已提交
132 133 134 135 136

        QDECREF(str);
    }
}

137 138 139 140 141 142 143 144 145 146 147
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.
     *
148 149 150 151
     * 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.
152 153 154 155 156 157 158
     *
     * 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").
     *
159
     * Most test cases are scraped from Markus Kuhn's UTF-8 decoder
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
     * 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
176
         * - bug: want "..."
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
         *   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" */
204 205
            "\"\\u0000\"",
            "\xC0\x80",
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        },
        /* 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",
223
            "\"\\uD800\\uDC00\"",
224 225 226 227
        },
        /* 2.1.5  5 bytes U+200000 */
        {
            "\"\xF8\x88\x80\x80\x80\"",
228 229
            NULL,               /* bug: rejected */
            "\"\\uFFFD\"",
230 231 232 233 234
            "\xF8\x88\x80\x80\x80",
        },
        /* 2.1.6  6 bytes U+4000000 */
        {
            "\"\xFC\x84\x80\x80\x80\x80\"",
235 236
            NULL,               /* bug: rejected */
            "\"\\uFFFD\"",
237 238 239 240 241 242 243
            "\xFC\x84\x80\x80\x80\x80",
        },
        /* 2.2  Last possible sequence of a certain length */
        /* 2.2.1  1 byte U+007F */
        {
            "\"\x7F\"",
            "\x7F",
244
            "\"\\u007F\"",
245 246 247 248 249 250 251
        },
        /* 2.2.2  2 bytes U+07FF */
        {
            "\"\xDF\xBF\"",
            "\xDF\xBF",
            "\"\\u07FF\"",
        },
252 253 254 255 256 257 258 259 260
        /*
         * 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.
         */
261
        {
262 263 264
            "\"\xEF\xBF\xBC\"",
            "\xEF\xBF\xBC",
            "\"\\uFFFC\"",
265 266 267 268
        },
        /* 2.2.4  4 bytes U+1FFFFF */
        {
            "\"\xF7\xBF\xBF\xBF\"",
269 270
            NULL,               /* bug: rejected */
            "\"\\uFFFD\"",
271 272 273 274 275
            "\xF7\xBF\xBF\xBF",
        },
        /* 2.2.5  5 bytes U+3FFFFFF */
        {
            "\"\xFB\xBF\xBF\xBF\xBF\"",
276 277
            NULL,               /* bug: rejected */
            "\"\\uFFFD\"",
278 279 280 281 282
            "\xFB\xBF\xBF\xBF\xBF",
        },
        /* 2.2.6  6 bytes U+7FFFFFFF */
        {
            "\"\xFD\xBF\xBF\xBF\xBF\xBF\"",
283 284
            NULL,               /* bug: rejected */
            "\"\\uFFFD\"",
285 286 287 288
            "\xFD\xBF\xBF\xBF\xBF\xBF",
        },
        /* 2.3  Other boundary conditions */
        {
289
            /* last one before surrogate range: U+D7FF */
290 291 292 293 294
            "\"\xED\x9F\xBF\"",
            "\xED\x9F\xBF",
            "\"\\uD7FF\"",
        },
        {
295
            /* first one after surrogate range: U+E000 */
296 297 298 299 300
            "\"\xEE\x80\x80\"",
            "\xEE\x80\x80",
            "\"\\uE000\"",
        },
        {
301
            /* last one in BMP: U+FFFD */
302 303 304 305 306
            "\"\xEF\xBF\xBD\"",
            "\xEF\xBF\xBD",
            "\"\\uFFFD\"",
        },
        {
307 308 309
            /* last one in last plane: U+10FFFD */
            "\"\xF4\x8F\xBF\xBD\"",
            "\xF4\x8F\xBF\xBD",
310
            "\"\\uDBFF\\uDFFD\""
311 312
        },
        {
313
            /* first one beyond Unicode range: U+110000 */
314 315
            "\"\xF4\x90\x80\x80\"",
            "\xF4\x90\x80\x80",
316
            "\"\\uFFFD\"",
317 318 319 320 321 322 323
        },
        /* 3  Malformed sequences */
        /* 3.1  Unexpected continuation bytes */
        /* 3.1.1  First continuation byte */
        {
            "\"\x80\"",
            "\x80",             /* bug: not corrected */
324
            "\"\\uFFFD\"",
325 326 327 328 329
        },
        /* 3.1.2  Last continuation byte */
        {
            "\"\xBF\"",
            "\xBF",             /* bug: not corrected */
330
            "\"\\uFFFD\"",
331 332 333 334 335
        },
        /* 3.1.3  2 continuation bytes */
        {
            "\"\x80\xBF\"",
            "\x80\xBF",         /* bug: not corrected */
336
            "\"\\uFFFD\\uFFFD\"",
337 338 339 340 341
        },
        /* 3.1.4  3 continuation bytes */
        {
            "\"\x80\xBF\x80\"",
            "\x80\xBF\x80",     /* bug: not corrected */
342
            "\"\\uFFFD\\uFFFD\\uFFFD\"",
343 344 345 346 347
        },
        /* 3.1.5  4 continuation bytes */
        {
            "\"\x80\xBF\x80\xBF\"",
            "\x80\xBF\x80\xBF", /* bug: not corrected */
348
            "\"\\uFFFD\\uFFFD\\uFFFD\\uFFFD\"",
349 350 351 352 353
        },
        /* 3.1.6  5 continuation bytes */
        {
            "\"\x80\xBF\x80\xBF\x80\"",
            "\x80\xBF\x80\xBF\x80", /* bug: not corrected */
354
            "\"\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\"",
355 356 357 358 359
        },
        /* 3.1.7  6 continuation bytes */
        {
            "\"\x80\xBF\x80\xBF\x80\xBF\"",
            "\x80\xBF\x80\xBF\x80\xBF", /* bug: not corrected */
360
            "\"\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\"",
361 362 363 364 365
        },
        /* 3.1.8  7 continuation bytes */
        {
            "\"\x80\xBF\x80\xBF\x80\xBF\x80\"",
            "\x80\xBF\x80\xBF\x80\xBF\x80", /* bug: not corrected */
366
            "\"\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\"",
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
        },
        /* 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",
387 388 389 390 391 392 393 394
            "\"\\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\""
395 396 397 398 399 400 401 402 403
        },
        /* 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 */
404 405 406 407
            "\"\\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 \"",
408 409 410 411 412 413 414 415 416 417 418 419
            "\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 ",
420 421
            "\"\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD "
            "\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \"",
422 423 424 425 426
        },
        /* 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 */
427
            "\"\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \"",
428 429 430 431 432 433
            "\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 */
434
            "\"\\uFFFD \\uFFFD \\uFFFD \\uFFFD \"",
435 436 437 438 439 440
            "\xF8 \xF9 \xFA \xFB ",
        },
        /* 3.2.5  All 2 first bytes of 6-byte sequences, followed by space */
        {
            "\"\xFC \xFD \"",
            NULL,               /* bug: rejected */
441
            "\"\\uFFFD \\uFFFD \"",
442 443 444 445 446 447 448
            "\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 */
449
            "\"\\uFFFD\"",
450 451 452 453 454 455
            "\xC0",
        },
        /* 3.3.2  3-byte sequence with last byte missing (U+0000) */
        {
            "\"\xE0\x80\"",
            "\xE0\x80",           /* bug: not corrected */
456
            "\"\\uFFFD\"",
457 458 459 460 461
        },
        /* 3.3.3  4-byte sequence with last byte missing (U+0000) */
        {
            "\"\xF0\x80\x80\"",
            "\xF0\x80\x80",     /* bug: not corrected */
462
            "\"\\uFFFD\"",
463 464 465
        },
        /* 3.3.4  5-byte sequence with last byte missing (U+0000) */
        {
466
            "\"\xF8\x80\x80\x80\"",
467
            NULL,                   /* bug: rejected */
468
            "\"\\uFFFD\"",
469 470 471 472 473 474
            "\xF8\x80\x80\x80",
        },
        /* 3.3.5  6-byte sequence with last byte missing (U+0000) */
        {
            "\"\xFC\x80\x80\x80\x80\"",
            NULL,                        /* bug: rejected */
475
            "\"\\uFFFD\"",
476 477 478 479 480 481
            "\xFC\x80\x80\x80\x80",
        },
        /* 3.3.6  2-byte sequence with last byte missing (U+07FF) */
        {
            "\"\xDF\"",
            "\xDF",             /* bug: not corrected */
482
            "\"\\uFFFD\"",
483 484 485 486 487
        },
        /* 3.3.7  3-byte sequence with last byte missing (U+FFFF) */
        {
            "\"\xEF\xBF\"",
            "\xEF\xBF",           /* bug: not corrected */
488
            "\"\\uFFFD\"",
489 490 491 492 493
        },
        /* 3.3.8  4-byte sequence with last byte missing (U+1FFFFF) */
        {
            "\"\xF7\xBF\xBF\"",
            NULL,               /* bug: rejected */
494
            "\"\\uFFFD\"",
495 496 497 498 499 500
            "\xF7\xBF\xBF",
        },
        /* 3.3.9  5-byte sequence with last byte missing (U+3FFFFFF) */
        {
            "\"\xFB\xBF\xBF\xBF\"",
            NULL,                 /* bug: rejected */
501
            "\"\\uFFFD\"",
502 503 504 505 506 507
            "\xFB\xBF\xBF\xBF",
        },
        /* 3.3.10  6-byte sequence with last byte missing (U+7FFFFFFF) */
        {
            "\"\xFD\xBF\xBF\xBF\xBF\"",
            NULL,                        /* bug: rejected */
508
            "\"\\uFFFD\"",
509 510 511 512 513 514 515
            "\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 */
516 517
            "\"\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
            "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\"",
518 519 520 521 522 523 524
            "\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 */
525
            "\"\\uFFFD\"",
526 527 528 529 530
            "\xFE",
        },
        {
            "\"\xFF\"",
            NULL,               /* bug: rejected */
531
            "\"\\uFFFD\"",
532 533 534 535 536
            "\xFF",
        },
        {
            "\"\xFE\xFE\xFF\xFF\"",
            NULL,                 /* bug: rejected */
537
            "\"\\uFFFD\\uFFFD\\uFFFD\\uFFFD\"",
538 539 540 541 542 543 544
            "\xFE\xFE\xFF\xFF",
        },
        /* 4  Overlong sequences */
        /* 4.1  Overlong '/' */
        {
            "\"\xC0\xAF\"",
            NULL,               /* bug: rejected */
545
            "\"\\uFFFD\"",
546 547 548 549 550
            "\xC0\xAF",
        },
        {
            "\"\xE0\x80\xAF\"",
            "\xE0\x80\xAF",     /* bug: not corrected */
551
            "\"\\uFFFD\"",
552 553 554 555
        },
        {
            "\"\xF0\x80\x80\xAF\"",
            "\xF0\x80\x80\xAF",  /* bug: not corrected */
556
            "\"\\uFFFD\"",
557 558 559 560
        },
        {
            "\"\xF8\x80\x80\x80\xAF\"",
            NULL,                        /* bug: rejected */
561
            "\"\\uFFFD\"",
562 563 564 565 566
            "\xF8\x80\x80\x80\xAF",
        },
        {
            "\"\xFC\x80\x80\x80\x80\xAF\"",
            NULL,                               /* bug: rejected */
567
            "\"\\uFFFD\"",
568 569
            "\xFC\x80\x80\x80\x80\xAF",
        },
570 571 572 573 574 575
        /*
         * 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.
         */
576 577 578 579
        {
            /* \U+007F */
            "\"\xC1\xBF\"",
            NULL,               /* bug: rejected */
580
            "\"\\uFFFD\"",
581 582 583 584 585 586
            "\xC1\xBF",
        },
        {
            /* \U+07FF */
            "\"\xE0\x9F\xBF\"",
            "\xE0\x9F\xBF",     /* bug: not corrected */
587
            "\"\\uFFFD\"",
588 589
        },
        {
590 591 592 593 594 595 596 597
            /*
             * \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 */
598
            "\"\\uFFFD\"",
599 600 601 602 603
        },
        {
            /* \U+1FFFFF */
            "\"\xF8\x87\xBF\xBF\xBF\"",
            NULL,                        /* bug: rejected */
604
            "\"\\uFFFD\"",
605 606 607 608 609 610
            "\xF8\x87\xBF\xBF\xBF",
        },
        {
            /* \U+3FFFFFF */
            "\"\xFC\x83\xBF\xBF\xBF\xBF\"",
            NULL,                               /* bug: rejected */
611
            "\"\\uFFFD\"",
612 613 614 615 616 617 618 619 620 621 622 623 624 625
            "\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 */
626
            "\"\\uFFFD\"",
627 628 629 630 631
        },
        {
            /* \U+0000 */
            "\"\xF0\x80\x80\x80\"",
            "\xF0\x80\x80\x80",   /* bug: not corrected */
632
            "\"\\uFFFD\"",
633 634 635 636 637
        },
        {
            /* \U+0000 */
            "\"\xF8\x80\x80\x80\x80\"",
            NULL,                        /* bug: rejected */
638
            "\"\\uFFFD\"",
639 640 641 642 643 644
            "\xF8\x80\x80\x80\x80",
        },
        {
            /* \U+0000 */
            "\"\xFC\x80\x80\x80\x80\x80\"",
            NULL,                               /* bug: rejected */
645
            "\"\\uFFFD\"",
646 647 648 649 650 651 652 653
            "\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 */
654
            "\"\\uFFFD\"",
655 656 657 658 659
        },
        {
            /* \U+DB7F */
            "\"\xED\xAD\xBF\"",
            "\xED\xAD\xBF",     /* bug: not corrected */
660
            "\"\\uFFFD\"",
661 662 663 664 665
        },
        {
            /* \U+DB80 */
            "\"\xED\xAE\x80\"",
            "\xED\xAE\x80",     /* bug: not corrected */
666
            "\"\\uFFFD\"",
667 668 669 670 671
        },
        {
            /* \U+DBFF */
            "\"\xED\xAF\xBF\"",
            "\xED\xAF\xBF",     /* bug: not corrected */
672
            "\"\\uFFFD\"",
673 674 675 676 677
        },
        {
            /* \U+DC00 */
            "\"\xED\xB0\x80\"",
            "\xED\xB0\x80",     /* bug: not corrected */
678
            "\"\\uFFFD\"",
679 680 681 682 683
        },
        {
            /* \U+DF80 */
            "\"\xED\xBE\x80\"",
            "\xED\xBE\x80",     /* bug: not corrected */
684
            "\"\\uFFFD\"",
685 686 687 688 689
        },
        {
            /* \U+DFFF */
            "\"\xED\xBF\xBF\"",
            "\xED\xBF\xBF",     /* bug: not corrected */
690
            "\"\\uFFFD\"",
691 692 693 694 695 696
        },
        /* 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 */
697
            "\"\\uFFFD\\uFFFD\"",
698 699 700 701 702
        },
        {
            /* \U+D800\U+DFFF */
            "\"\xED\xA0\x80\xED\xBF\xBF\"",
            "\xED\xA0\x80\xED\xBF\xBF", /* bug: not corrected */
703
            "\"\\uFFFD\\uFFFD\"",
704 705 706 707 708
        },
        {
            /* \U+DB7F\U+DC00 */
            "\"\xED\xAD\xBF\xED\xB0\x80\"",
            "\xED\xAD\xBF\xED\xB0\x80", /* bug: not corrected */
709
            "\"\\uFFFD\\uFFFD\"",
710 711 712 713 714
        },
        {
            /* \U+DB7F\U+DFFF */
            "\"\xED\xAD\xBF\xED\xBF\xBF\"",
            "\xED\xAD\xBF\xED\xBF\xBF", /* bug: not corrected */
715
            "\"\\uFFFD\\uFFFD\"",
716 717 718 719 720
        },
        {
            /* \U+DB80\U+DC00 */
            "\"\xED\xAE\x80\xED\xB0\x80\"",
            "\xED\xAE\x80\xED\xB0\x80", /* bug: not corrected */
721
            "\"\\uFFFD\\uFFFD\"",
722 723 724 725 726
        },
        {
            /* \U+DB80\U+DFFF */
            "\"\xED\xAE\x80\xED\xBF\xBF\"",
            "\xED\xAE\x80\xED\xBF\xBF", /* bug: not corrected */
727
            "\"\\uFFFD\\uFFFD\"",
728 729 730 731 732
        },
        {
            /* \U+DBFF\U+DC00 */
            "\"\xED\xAF\xBF\xED\xB0\x80\"",
            "\xED\xAF\xBF\xED\xB0\x80", /* bug: not corrected */
733
            "\"\\uFFFD\\uFFFD\"",
734 735 736 737 738
        },
        {
            /* \U+DBFF\U+DFFF */
            "\"\xED\xAF\xBF\xED\xBF\xBF\"",
            "\xED\xAF\xBF\xED\xBF\xBF", /* bug: not corrected */
739
            "\"\\uFFFD\\uFFFD\"",
740 741
        },
        /* 5.3  Other illegal code positions */
742
        /* BMP noncharacters */
743 744 745 746
        {
            /* \U+FFFE */
            "\"\xEF\xBF\xBE\"",
            "\xEF\xBF\xBE",     /* bug: not corrected */
747
            "\"\\uFFFD\"",
748 749 750 751 752
        },
        {
            /* \U+FFFF */
            "\"\xEF\xBF\xBF\"",
            "\xEF\xBF\xBF",     /* bug: not corrected */
753
            "\"\\uFFFD\"",
754
        },
755 756 757 758
        {
            /* U+FDD0 */
            "\"\xEF\xB7\x90\"",
            "\xEF\xB7\x90",     /* bug: not corrected */
759
            "\"\\uFFFD\"",
760 761 762 763 764
        },
        {
            /* U+FDEF */
            "\"\xEF\xB7\xAF\"",
            "\xEF\xB7\xAF",     /* bug: not corrected */
765
            "\"\\uFFFD\"",
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 802
        },
        /* 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",
803 804 805 806
            "\"\\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\"",
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 843
        {}
    };
    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);

        /*
844 845
         * Disabled, because qobject_from_json() is buggy, and I can't
         * be bothered to add the expected incorrect results.
846 847 848 849 850 851 852 853 854 855 856 857
         * 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 已提交
858
static void vararg_string(void)
A
Anthony Liguori 已提交
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
{
    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 已提交
875 876
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QSTRING);
A
Anthony Liguori 已提交
877 878
        
        str = qobject_to_qstring(obj);
A
Anthony Liguori 已提交
879
        g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
A
Anthony Liguori 已提交
880 881 882 883 884

        QDECREF(str);
    }
}

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

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

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

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

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

        QDECREF(qint);
    }
}

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

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

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

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

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

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

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

A
Anthony Liguori 已提交
961
static void vararg_number(void)
A
Anthony Liguori 已提交
962 963 964 965 966 967 968 969 970
{
    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 已提交
971 972
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QINT);
A
Anthony Liguori 已提交
973 974

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

    QDECREF(qint);

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

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

    QDECREF(qint);

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

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

    QDECREF(qfloat);
}

A
Anthony Liguori 已提交
998
static void keyword_literal(void)
A
Anthony Liguori 已提交
999 1000 1001
{
    QObject *obj;
    QBool *qbool;
E
Eric Blake 已提交
1002
    QObject *null;
1003
    QString *str;
A
Anthony Liguori 已提交
1004 1005

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

    qbool = qobject_to_qbool(obj);
E
Eric Blake 已提交
1010
    g_assert(qbool_get_bool(qbool) == true);
A
Anthony Liguori 已提交
1011

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

A
Anthony Liguori 已提交
1016 1017 1018
    QDECREF(qbool);

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

    qbool = qobject_to_qbool(obj);
E
Eric Blake 已提交
1023
    g_assert(qbool_get_bool(qbool) == false);
A
Anthony Liguori 已提交
1024

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

A
Anthony Liguori 已提交
1029 1030 1031
    QDECREF(qbool);

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

    qbool = qobject_to_qbool(obj);
E
Eric Blake 已提交
1036
    g_assert(qbool_get_bool(qbool) == false);
A
Anthony Liguori 已提交
1037 1038

    QDECREF(qbool);
E
Eric Blake 已提交
1039

E
Eric Blake 已提交
1040 1041
    /* Test that non-zero values other than 1 get collapsed to true */
    obj = qobject_from_jsonf("%i", 2);
A
Anthony Liguori 已提交
1042 1043
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QBOOL);
A
Anthony Liguori 已提交
1044 1045

    qbool = qobject_to_qbool(obj);
E
Eric Blake 已提交
1046
    g_assert(qbool_get_bool(qbool) == true);
A
Anthony Liguori 已提交
1047 1048

    QDECREF(qbool);
E
Eric Blake 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058

    obj = qobject_from_json("null");
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QNULL);

    null = qnull();
    g_assert(null == obj);

    qobject_decref(obj);
    qobject_decref(null);
A
Anthony Liguori 已提交
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 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
}

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 已提交
1153
static void simple_dict(void)
A
Anthony Liguori 已提交
1154 1155 1156 1157 1158 1159 1160
{
    int i;
    struct {
        const char *encoded;
        LiteralQObject decoded;
    } test_cases[] = {
        {
1161
            .encoded = "{\"foo\": 42, \"bar\": \"hello world\"}",
A
Anthony Liguori 已提交
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
                        { "foo", QLIT_QINT(42) },
                        { "bar", QLIT_QSTR("hello world") },
                        { }
                    })),
        }, {
            .encoded = "{}",
            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
                        { }
                    })),
        }, {
1173
            .encoded = "{\"foo\": 43}",
A
Anthony Liguori 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
                        { "foo", QLIT_QINT(43) },
                        { }
                    })),
        },
        { }
    };

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

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
1187 1188
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QDICT);
A
Anthony Liguori 已提交
1189

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

1192 1193 1194 1195
        str = qobject_to_json(obj);
        qobject_decref(obj);

        obj = qobject_from_json(qstring_get_str(str));
A
Anthony Liguori 已提交
1196 1197
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QDICT);
1198

A
Anthony Liguori 已提交
1199
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
A
Anthony Liguori 已提交
1200
        qobject_decref(obj);
1201
        QDECREF(str);
A
Anthony Liguori 已提交
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 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
/*
 * 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 已提交
1257
static void simple_list(void)
A
Anthony Liguori 已提交
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 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
{
    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;
1299
        QString *str;
A
Anthony Liguori 已提交
1300 1301

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
1302 1303
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
A
Anthony Liguori 已提交
1304

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

1307 1308 1309 1310
        str = qobject_to_json(obj);
        qobject_decref(obj);

        obj = qobject_from_json(qstring_get_str(str));
A
Anthony Liguori 已提交
1311 1312
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
1313

A
Anthony Liguori 已提交
1314
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
A
Anthony Liguori 已提交
1315
        qobject_decref(obj);
1316
        QDECREF(str);
A
Anthony Liguori 已提交
1317 1318 1319
    }
}

A
Anthony Liguori 已提交
1320
static void simple_whitespace(void)
A
Anthony Liguori 已提交
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 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
{
    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;
1367
        QString *str;
A
Anthony Liguori 已提交
1368 1369

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
1370 1371
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
A
Anthony Liguori 已提交
1372

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

1375
        str = qobject_to_json(obj);
A
Anthony Liguori 已提交
1376
        qobject_decref(obj);
1377 1378

        obj = qobject_from_json(qstring_get_str(str));
A
Anthony Liguori 已提交
1379 1380
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
1381

A
Anthony Liguori 已提交
1382
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
1383 1384 1385

        qobject_decref(obj);
        QDECREF(str);
A
Anthony Liguori 已提交
1386 1387 1388
    }
}

A
Anthony Liguori 已提交
1389
static void simple_varargs(void)
A
Anthony Liguori 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
{
    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 已提交
1403
    g_assert(embedded_obj != NULL);
A
Anthony Liguori 已提交
1404 1405

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

A
Anthony Liguori 已提交
1408
    g_assert(compare_litqobj_to_qobj(&decoded, obj) == 1);
A
Anthony Liguori 已提交
1409 1410 1411 1412

    qobject_decref(obj);
}

A
Anthony Liguori 已提交
1413
static void empty_input(void)
P
Paolo Bonzini 已提交
1414
{
1415 1416 1417
    const char *empty = "";

    QObject *obj = qobject_from_json(empty);
A
Anthony Liguori 已提交
1418
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1419 1420
}

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

A
Anthony Liguori 已提交
1427
static void unterminated_sq_string(void)
P
Paolo Bonzini 已提交
1428 1429
{
    QObject *obj = qobject_from_json("'abc");
A
Anthony Liguori 已提交
1430
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1431 1432
}

A
Anthony Liguori 已提交
1433
static void unterminated_escape(void)
P
Paolo Bonzini 已提交
1434 1435
{
    QObject *obj = qobject_from_json("\"abc\\\"");
A
Anthony Liguori 已提交
1436
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1437 1438
}

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

A
Anthony Liguori 已提交
1445
static void unterminated_array_comma(void)
P
Paolo Bonzini 已提交
1446 1447
{
    QObject *obj = qobject_from_json("[32,");
A
Anthony Liguori 已提交
1448
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1449 1450
}

A
Anthony Liguori 已提交
1451
static void invalid_array_comma(void)
P
Paolo Bonzini 已提交
1452 1453
{
    QObject *obj = qobject_from_json("[32,}");
A
Anthony Liguori 已提交
1454
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1455 1456
}

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

A
Anthony Liguori 已提交
1463
static void unterminated_dict_comma(void)
P
Paolo Bonzini 已提交
1464 1465
{
    QObject *obj = qobject_from_json("{'abc':32,");
A
Anthony Liguori 已提交
1466
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1467 1468
}

A
Anthony Liguori 已提交
1469
static void invalid_dict_comma(void)
P
Paolo Bonzini 已提交
1470 1471
{
    QObject *obj = qobject_from_json("{'abc':32,}");
A
Anthony Liguori 已提交
1472
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1473 1474
}

A
Anthony Liguori 已提交
1475
static void unterminated_literal(void)
P
Paolo Bonzini 已提交
1476 1477
{
    QObject *obj = qobject_from_json("nul");
A
Anthony Liguori 已提交
1478
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1479 1480
}

1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
static char *make_nest(char *buf, size_t cnt)
{
    memset(buf, '[', cnt - 1);
    buf[cnt - 1] = '{';
    buf[cnt] = '}';
    memset(buf + cnt + 1, ']', cnt - 1);
    buf[2 * cnt] = 0;
    return buf;
}

static void limits_nesting(void)
{
    enum { max_nesting = 1024 }; /* see qobject/json-streamer.c */
    char buf[2 * (max_nesting + 1) + 1];
    QObject *obj;

    obj = qobject_from_json(make_nest(buf, max_nesting));
    g_assert(obj != NULL);
    qobject_decref(obj);

    obj = qobject_from_json(make_nest(buf, max_nesting + 1));
    g_assert(obj == NULL);
}

A
Anthony Liguori 已提交
1505
int main(int argc, char **argv)
A
Anthony Liguori 已提交
1506
{
A
Anthony Liguori 已提交
1507 1508 1509 1510
    g_test_init(&argc, &argv, NULL);

    g_test_add_func("/literals/string/simple", simple_string);
    g_test_add_func("/literals/string/escaped", escaped_string);
1511
    g_test_add_func("/literals/string/utf8", utf8_string);
A
Anthony Liguori 已提交
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
    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);
1522
    g_test_add_func("/dicts/large_dict", large_dict);
A
Anthony Liguori 已提交
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
    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);
1540
    g_test_add_func("/errors/limits/nesting", limits_nesting);
P
Paolo Bonzini 已提交
1541

A
Anthony Liguori 已提交
1542
    return g_test_run();
A
Anthony Liguori 已提交
1543
}