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

13 14 15 16 17 18 19
#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 已提交
20 21 22

#include "qemu-common.h"

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

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

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

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

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

A
Anthony Liguori 已提交
67 68 69 70
        QDECREF(str);
    }
}

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

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

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

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

        QDECREF(str);
    }
}

A
Anthony Liguori 已提交
134
static void vararg_string(void)
A
Anthony Liguori 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
{
    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 已提交
151 152
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QSTRING);
A
Anthony Liguori 已提交
153 154
        
        str = qobject_to_qstring(obj);
A
Anthony Liguori 已提交
155
        g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
A
Anthony Liguori 已提交
156 157 158 159 160

        QDECREF(str);
    }
}

A
Anthony Liguori 已提交
161
static void simple_number(void)
A
Anthony Liguori 已提交
162 163 164 165 166
{
    int i;
    struct {
        const char *encoded;
        int64_t decoded;
167
        int skip;
A
Anthony Liguori 已提交
168 169 170 171 172
    } test_cases[] = {
        { "0", 0 },
        { "1234", 1234 },
        { "1", 1 },
        { "-32", -32 },
173
        { "-0", 0, .skip = 1 },
A
Anthony Liguori 已提交
174 175 176 177 178 179 180 181
        { },
    };

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

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
182 183
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QINT);
A
Anthony Liguori 已提交
184 185

        qint = qobject_to_qint(obj);
A
Anthony Liguori 已提交
186
        g_assert(qint_get_int(qint) == test_cases[i].decoded);
187 188 189 190
        if (test_cases[i].skip == 0) {
            QString *str;

            str = qobject_to_json(obj);
A
Anthony Liguori 已提交
191
            g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
192 193
            QDECREF(str);
        }
A
Anthony Liguori 已提交
194 195 196 197 198

        QDECREF(qint);
    }
}

A
Anthony Liguori 已提交
199
static void float_number(void)
A
Anthony Liguori 已提交
200 201 202 203 204
{
    int i;
    struct {
        const char *encoded;
        double decoded;
205
        int skip;
A
Anthony Liguori 已提交
206 207 208 209
    } test_cases[] = {
        { "32.43", 32.43 },
        { "0.222", 0.222 },
        { "-32.12313", -32.12313 },
210
        { "-32.20e-10", -32.20e-10, .skip = 1 },
A
Anthony Liguori 已提交
211 212 213 214 215 216 217 218
        { },
    };

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

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
219 220
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QFLOAT);
A
Anthony Liguori 已提交
221 222

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

225 226 227 228
        if (test_cases[i].skip == 0) {
            QString *str;

            str = qobject_to_json(obj);
A
Anthony Liguori 已提交
229
            g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
230 231 232
            QDECREF(str);
        }

A
Anthony Liguori 已提交
233 234 235 236
        QDECREF(qfloat);
    }
}

A
Anthony Liguori 已提交
237
static void vararg_number(void)
A
Anthony Liguori 已提交
238 239 240 241 242 243 244 245 246
{
    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 已提交
247 248
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QINT);
A
Anthony Liguori 已提交
249 250

    qint = qobject_to_qint(obj);
A
Anthony Liguori 已提交
251
    g_assert(qint_get_int(qint) == value);
A
Anthony Liguori 已提交
252 253 254 255

    QDECREF(qint);

    obj = qobject_from_jsonf("%" PRId64, value64);
A
Anthony Liguori 已提交
256 257
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QINT);
A
Anthony Liguori 已提交
258 259

    qint = qobject_to_qint(obj);
A
Anthony Liguori 已提交
260
    g_assert(qint_get_int(qint) == value64);
A
Anthony Liguori 已提交
261 262 263 264

    QDECREF(qint);

    obj = qobject_from_jsonf("%f", valuef);
A
Anthony Liguori 已提交
265 266
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QFLOAT);
A
Anthony Liguori 已提交
267 268

    qfloat = qobject_to_qfloat(obj);
A
Anthony Liguori 已提交
269
    g_assert(qfloat_get_double(qfloat) == valuef);
A
Anthony Liguori 已提交
270 271 272 273

    QDECREF(qfloat);
}

A
Anthony Liguori 已提交
274
static void keyword_literal(void)
A
Anthony Liguori 已提交
275 276 277
{
    QObject *obj;
    QBool *qbool;
278
    QString *str;
A
Anthony Liguori 已提交
279 280

    obj = qobject_from_json("true");
A
Anthony Liguori 已提交
281 282
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QBOOL);
A
Anthony Liguori 已提交
283 284

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

287
    str = qobject_to_json(obj);
A
Anthony Liguori 已提交
288
    g_assert(strcmp(qstring_get_str(str), "true") == 0);
289 290
    QDECREF(str);

A
Anthony Liguori 已提交
291 292 293
    QDECREF(qbool);

    obj = qobject_from_json("false");
A
Anthony Liguori 已提交
294 295
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QBOOL);
A
Anthony Liguori 已提交
296 297

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

300
    str = qobject_to_json(obj);
A
Anthony Liguori 已提交
301
    g_assert(strcmp(qstring_get_str(str), "false") == 0);
302 303
    QDECREF(str);

A
Anthony Liguori 已提交
304 305 306
    QDECREF(qbool);

    obj = qobject_from_jsonf("%i", false);
A
Anthony Liguori 已提交
307 308
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QBOOL);
A
Anthony Liguori 已提交
309 310

    qbool = qobject_to_qbool(obj);
A
Anthony Liguori 已提交
311
    g_assert(qbool_get_int(qbool) == 0);
A
Anthony Liguori 已提交
312 313 314 315

    QDECREF(qbool);
    
    obj = qobject_from_jsonf("%i", true);
A
Anthony Liguori 已提交
316 317
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QBOOL);
A
Anthony Liguori 已提交
318 319

    qbool = qobject_to_qbool(obj);
A
Anthony Liguori 已提交
320
    g_assert(qbool_get_int(qbool) != 0);
A
Anthony Liguori 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

    QDECREF(qbool);
}

typedef struct LiteralQDictEntry LiteralQDictEntry;
typedef struct LiteralQObject LiteralQObject;

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

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

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

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

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

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

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

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

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

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

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

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

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

        return 1;
    }
    case QTYPE_QLIST: {
        QListCompareHelper helper;

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

        return helper.result;
    }
    default:
        break;
    }

    return 0;
}

A
Anthony Liguori 已提交
417
static void simple_dict(void)
A
Anthony Liguori 已提交
418 419 420 421 422 423 424
{
    int i;
    struct {
        const char *encoded;
        LiteralQObject decoded;
    } test_cases[] = {
        {
425
            .encoded = "{\"foo\": 42, \"bar\": \"hello world\"}",
A
Anthony Liguori 已提交
426 427 428 429 430 431 432 433 434 435 436
            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
                        { "foo", QLIT_QINT(42) },
                        { "bar", QLIT_QSTR("hello world") },
                        { }
                    })),
        }, {
            .encoded = "{}",
            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
                        { }
                    })),
        }, {
437
            .encoded = "{\"foo\": 43}",
A
Anthony Liguori 已提交
438 439 440 441 442 443 444 445 446 447
            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
                        { "foo", QLIT_QINT(43) },
                        { }
                    })),
        },
        { }
    };

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

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
451 452
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QDICT);
A
Anthony Liguori 已提交
453

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

456 457 458 459
        str = qobject_to_json(obj);
        qobject_decref(obj);

        obj = qobject_from_json(qstring_get_str(str));
A
Anthony Liguori 已提交
460 461
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QDICT);
462

A
Anthony Liguori 已提交
463
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
A
Anthony Liguori 已提交
464
        qobject_decref(obj);
465
        QDECREF(str);
A
Anthony Liguori 已提交
466 467 468
    }
}

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
/*
 * 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 已提交
521
static void simple_list(void)
A
Anthony Liguori 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
{
    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;
563
        QString *str;
A
Anthony Liguori 已提交
564 565

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
566 567
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
A
Anthony Liguori 已提交
568

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

571 572 573 574
        str = qobject_to_json(obj);
        qobject_decref(obj);

        obj = qobject_from_json(qstring_get_str(str));
A
Anthony Liguori 已提交
575 576
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
577

A
Anthony Liguori 已提交
578
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
A
Anthony Liguori 已提交
579
        qobject_decref(obj);
580
        QDECREF(str);
A
Anthony Liguori 已提交
581 582 583
    }
}

A
Anthony Liguori 已提交
584
static void simple_whitespace(void)
A
Anthony Liguori 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
{
    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;
631
        QString *str;
A
Anthony Liguori 已提交
632 633

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
634 635
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
A
Anthony Liguori 已提交
636

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

639
        str = qobject_to_json(obj);
A
Anthony Liguori 已提交
640
        qobject_decref(obj);
641 642

        obj = qobject_from_json(qstring_get_str(str));
A
Anthony Liguori 已提交
643 644
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
645

A
Anthony Liguori 已提交
646
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
647 648 649

        qobject_decref(obj);
        QDECREF(str);
A
Anthony Liguori 已提交
650 651 652
    }
}

A
Anthony Liguori 已提交
653
static void simple_varargs(void)
A
Anthony Liguori 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666
{
    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 已提交
667
    g_assert(embedded_obj != NULL);
A
Anthony Liguori 已提交
668 669

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

A
Anthony Liguori 已提交
672
    g_assert(compare_litqobj_to_qobj(&decoded, obj) == 1);
A
Anthony Liguori 已提交
673 674 675 676

    qobject_decref(obj);
}

A
Anthony Liguori 已提交
677
static void empty_input(void)
P
Paolo Bonzini 已提交
678
{
679 680 681
    const char *empty = "";

    QObject *obj = qobject_from_json(empty);
A
Anthony Liguori 已提交
682
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
683 684
}

A
Anthony Liguori 已提交
685
static void unterminated_string(void)
P
Paolo Bonzini 已提交
686 687
{
    QObject *obj = qobject_from_json("\"abc");
A
Anthony Liguori 已提交
688
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
689 690
}

A
Anthony Liguori 已提交
691
static void unterminated_sq_string(void)
P
Paolo Bonzini 已提交
692 693
{
    QObject *obj = qobject_from_json("'abc");
A
Anthony Liguori 已提交
694
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
695 696
}

A
Anthony Liguori 已提交
697
static void unterminated_escape(void)
P
Paolo Bonzini 已提交
698 699
{
    QObject *obj = qobject_from_json("\"abc\\\"");
A
Anthony Liguori 已提交
700
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
701 702
}

A
Anthony Liguori 已提交
703
static void unterminated_array(void)
P
Paolo Bonzini 已提交
704 705
{
    QObject *obj = qobject_from_json("[32");
A
Anthony Liguori 已提交
706
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
707 708
}

A
Anthony Liguori 已提交
709
static void unterminated_array_comma(void)
P
Paolo Bonzini 已提交
710 711
{
    QObject *obj = qobject_from_json("[32,");
A
Anthony Liguori 已提交
712
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
713 714
}

A
Anthony Liguori 已提交
715
static void invalid_array_comma(void)
P
Paolo Bonzini 已提交
716 717
{
    QObject *obj = qobject_from_json("[32,}");
A
Anthony Liguori 已提交
718
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
719 720
}

A
Anthony Liguori 已提交
721
static void unterminated_dict(void)
P
Paolo Bonzini 已提交
722 723
{
    QObject *obj = qobject_from_json("{'abc':32");
A
Anthony Liguori 已提交
724
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
725 726
}

A
Anthony Liguori 已提交
727
static void unterminated_dict_comma(void)
P
Paolo Bonzini 已提交
728 729
{
    QObject *obj = qobject_from_json("{'abc':32,");
A
Anthony Liguori 已提交
730
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
731 732
}

A
Anthony Liguori 已提交
733
static void invalid_dict_comma(void)
P
Paolo Bonzini 已提交
734 735
{
    QObject *obj = qobject_from_json("{'abc':32,}");
A
Anthony Liguori 已提交
736
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
737 738
}

A
Anthony Liguori 已提交
739
static void unterminated_literal(void)
P
Paolo Bonzini 已提交
740 741
{
    QObject *obj = qobject_from_json("nul");
A
Anthony Liguori 已提交
742
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
743 744
}

A
Anthony Liguori 已提交
745
int main(int argc, char **argv)
A
Anthony Liguori 已提交
746
{
A
Anthony Liguori 已提交
747 748 749 750 751 752 753 754 755 756 757 758 759 760
    g_test_init(&argc, &argv, NULL);

    g_test_add_func("/literals/string/simple", simple_string);
    g_test_add_func("/literals/string/escaped", escaped_string);
    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);
761
    g_test_add_func("/dicts/large_dict", large_dict);
A
Anthony Liguori 已提交
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
    g_test_add_func("/lists/simple_list", simple_list);

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

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

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

A
Anthony Liguori 已提交
780
    return g_test_run();
A
Anthony Liguori 已提交
781
}