check-qjson.c 46.2 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

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
        { "\"double byte utf-8 \\u00A2\"", "double byte utf-8 \xc2\xa2" },
        { "\"triple byte utf-8 \\u20AC\"", "triple byte utf-8 \xe2\x82\xac" },
48 49 50 51 52 53 54
        { "'\\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 已提交
55 56 57 58 59 60 61 62 63
        {}
    };

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

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

A
Anthony Liguori 已提交
64 65
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QSTRING);
A
Anthony Liguori 已提交
66 67
        
        str = qobject_to_qstring(obj);
A
Anthony Liguori 已提交
68
        g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].decoded);
A
Anthony Liguori 已提交
69

70 71
        if (test_cases[i].skip == 0) {
            str = qobject_to_json(obj);
A
Anthony Liguori 已提交
72
            g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].encoded);
73 74 75
            qobject_decref(obj);
        }

A
Anthony Liguori 已提交
76 77 78 79
        QDECREF(str);
    }
}

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

105
        str = qobject_to_json(obj);
A
Anthony Liguori 已提交
106
        g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
107 108 109

        qobject_decref(obj);
        
A
Anthony Liguori 已提交
110 111 112 113
        QDECREF(str);
    }
}

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

        QDECREF(str);
    }
}

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

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

        QDECREF(str);
    }
}

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

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

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
912 913
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QINT);
A
Anthony Liguori 已提交
914 915

        qint = qobject_to_qint(obj);
A
Anthony Liguori 已提交
916
        g_assert(qint_get_int(qint) == test_cases[i].decoded);
917 918 919 920
        if (test_cases[i].skip == 0) {
            QString *str;

            str = qobject_to_json(obj);
A
Anthony Liguori 已提交
921
            g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
922 923
            QDECREF(str);
        }
A
Anthony Liguori 已提交
924 925 926 927 928

        QDECREF(qint);
    }
}

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

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

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
949 950
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QFLOAT);
A
Anthony Liguori 已提交
951 952

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

955 956 957 958
        if (test_cases[i].skip == 0) {
            QString *str;

            str = qobject_to_json(obj);
A
Anthony Liguori 已提交
959
            g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
960 961 962
            QDECREF(str);
        }

A
Anthony Liguori 已提交
963 964 965 966
        QDECREF(qfloat);
    }
}

A
Anthony Liguori 已提交
967
static void vararg_number(void)
A
Anthony Liguori 已提交
968 969 970 971 972 973 974 975 976
{
    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 已提交
977 978
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QINT);
A
Anthony Liguori 已提交
979 980

    qint = qobject_to_qint(obj);
A
Anthony Liguori 已提交
981
    g_assert(qint_get_int(qint) == value);
A
Anthony Liguori 已提交
982 983 984 985

    QDECREF(qint);

    obj = qobject_from_jsonf("%" PRId64, value64);
A
Anthony Liguori 已提交
986 987
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QINT);
A
Anthony Liguori 已提交
988 989

    qint = qobject_to_qint(obj);
A
Anthony Liguori 已提交
990
    g_assert(qint_get_int(qint) == value64);
A
Anthony Liguori 已提交
991 992 993 994

    QDECREF(qint);

    obj = qobject_from_jsonf("%f", valuef);
A
Anthony Liguori 已提交
995 996
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QFLOAT);
A
Anthony Liguori 已提交
997 998

    qfloat = qobject_to_qfloat(obj);
A
Anthony Liguori 已提交
999
    g_assert(qfloat_get_double(qfloat) == valuef);
A
Anthony Liguori 已提交
1000 1001 1002 1003

    QDECREF(qfloat);
}

A
Anthony Liguori 已提交
1004
static void keyword_literal(void)
A
Anthony Liguori 已提交
1005 1006 1007
{
    QObject *obj;
    QBool *qbool;
E
Eric Blake 已提交
1008
    QObject *null;
1009
    QString *str;
A
Anthony Liguori 已提交
1010 1011

    obj = qobject_from_json("true");
A
Anthony Liguori 已提交
1012 1013
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QBOOL);
A
Anthony Liguori 已提交
1014 1015

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

1018
    str = qobject_to_json(obj);
A
Anthony Liguori 已提交
1019
    g_assert(strcmp(qstring_get_str(str), "true") == 0);
1020 1021
    QDECREF(str);

A
Anthony Liguori 已提交
1022 1023 1024
    QDECREF(qbool);

    obj = qobject_from_json("false");
A
Anthony Liguori 已提交
1025 1026
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QBOOL);
A
Anthony Liguori 已提交
1027 1028

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

1031
    str = qobject_to_json(obj);
A
Anthony Liguori 已提交
1032
    g_assert(strcmp(qstring_get_str(str), "false") == 0);
1033 1034
    QDECREF(str);

A
Anthony Liguori 已提交
1035 1036 1037
    QDECREF(qbool);

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

    qbool = qobject_to_qbool(obj);
E
Eric Blake 已提交
1042
    g_assert(qbool_get_bool(qbool) == false);
A
Anthony Liguori 已提交
1043 1044

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

E
Eric Blake 已提交
1046 1047
    /* Test that non-zero values other than 1 get collapsed to true */
    obj = qobject_from_jsonf("%i", 2);
A
Anthony Liguori 已提交
1048 1049
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QBOOL);
A
Anthony Liguori 已提交
1050 1051

    qbool = qobject_to_qbool(obj);
E
Eric Blake 已提交
1052
    g_assert(qbool_get_bool(qbool) == true);
A
Anthony Liguori 已提交
1053 1054

    QDECREF(qbool);
E
Eric Blake 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

    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 已提交
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 1153 1154 1155 1156 1157 1158
}

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

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

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
1193 1194
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QDICT);
A
Anthony Liguori 已提交
1195

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

1198 1199 1200 1201
        str = qobject_to_json(obj);
        qobject_decref(obj);

        obj = qobject_from_json(qstring_get_str(str));
A
Anthony Liguori 已提交
1202 1203
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QDICT);
1204

A
Anthony Liguori 已提交
1205
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
A
Anthony Liguori 已提交
1206
        qobject_decref(obj);
1207
        QDECREF(str);
A
Anthony Liguori 已提交
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 1257 1258 1259 1260 1261 1262
/*
 * 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 已提交
1263
static void simple_list(void)
A
Anthony Liguori 已提交
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 1299 1300 1301 1302 1303 1304
{
    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;
1305
        QString *str;
A
Anthony Liguori 已提交
1306 1307

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
1308 1309
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
A
Anthony Liguori 已提交
1310

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

1313 1314 1315 1316
        str = qobject_to_json(obj);
        qobject_decref(obj);

        obj = qobject_from_json(qstring_get_str(str));
A
Anthony Liguori 已提交
1317 1318
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
1319

A
Anthony Liguori 已提交
1320
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
A
Anthony Liguori 已提交
1321
        qobject_decref(obj);
1322
        QDECREF(str);
A
Anthony Liguori 已提交
1323 1324 1325
    }
}

A
Anthony Liguori 已提交
1326
static void simple_whitespace(void)
A
Anthony Liguori 已提交
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 1367 1368 1369 1370 1371 1372
{
    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;
1373
        QString *str;
A
Anthony Liguori 已提交
1374 1375

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
1376 1377
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
A
Anthony Liguori 已提交
1378

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

1381
        str = qobject_to_json(obj);
A
Anthony Liguori 已提交
1382
        qobject_decref(obj);
1383 1384

        obj = qobject_from_json(qstring_get_str(str));
A
Anthony Liguori 已提交
1385 1386
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
1387

A
Anthony Liguori 已提交
1388
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
1389 1390 1391

        qobject_decref(obj);
        QDECREF(str);
A
Anthony Liguori 已提交
1392 1393 1394
    }
}

A
Anthony Liguori 已提交
1395
static void simple_varargs(void)
A
Anthony Liguori 已提交
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
{
    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 已提交
1409
    g_assert(embedded_obj != NULL);
A
Anthony Liguori 已提交
1410 1411

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

A
Anthony Liguori 已提交
1414
    g_assert(compare_litqobj_to_qobj(&decoded, obj) == 1);
A
Anthony Liguori 已提交
1415 1416 1417 1418

    qobject_decref(obj);
}

A
Anthony Liguori 已提交
1419
static void empty_input(void)
P
Paolo Bonzini 已提交
1420
{
1421 1422 1423
    const char *empty = "";

    QObject *obj = qobject_from_json(empty);
A
Anthony Liguori 已提交
1424
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1425 1426
}

A
Anthony Liguori 已提交
1427
static void unterminated_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_sq_string(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_escape(void)
P
Paolo Bonzini 已提交
1440 1441
{
    QObject *obj = qobject_from_json("\"abc\\\"");
A
Anthony Liguori 已提交
1442
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1443 1444
}

A
Anthony Liguori 已提交
1445
static void unterminated_array(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 unterminated_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 invalid_array_comma(void)
P
Paolo Bonzini 已提交
1458 1459
{
    QObject *obj = qobject_from_json("[32,}");
A
Anthony Liguori 已提交
1460
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1461 1462
}

A
Anthony Liguori 已提交
1463
static void unterminated_dict(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 unterminated_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 invalid_dict_comma(void)
P
Paolo Bonzini 已提交
1476 1477
{
    QObject *obj = qobject_from_json("{'abc':32,}");
A
Anthony Liguori 已提交
1478
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1479 1480
}

A
Anthony Liguori 已提交
1481
static void unterminated_literal(void)
P
Paolo Bonzini 已提交
1482 1483
{
    QObject *obj = qobject_from_json("nul");
A
Anthony Liguori 已提交
1484
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
1485 1486
}

1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
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 已提交
1511
int main(int argc, char **argv)
A
Anthony Liguori 已提交
1512
{
A
Anthony Liguori 已提交
1513 1514 1515 1516
    g_test_init(&argc, &argv, NULL);

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

A
Anthony Liguori 已提交
1548
    return g_test_run();
A
Anthony Liguori 已提交
1549
}