check-qjson.c 18.7 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 20 21 22

#include "qstring.h"
#include "qint.h"
#include "qdict.h"
#include "qlist.h"
#include "qfloat.h"
#include "qbool.h"
#include "qjson.h"

#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
    }
}

A
Anthony Liguori 已提交
469
static void simple_list(void)
A
Anthony Liguori 已提交
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
{
    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;
511
        QString *str;
A
Anthony Liguori 已提交
512 513

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
514 515
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
A
Anthony Liguori 已提交
516

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

519 520 521 522
        str = qobject_to_json(obj);
        qobject_decref(obj);

        obj = qobject_from_json(qstring_get_str(str));
A
Anthony Liguori 已提交
523 524
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
525

A
Anthony Liguori 已提交
526
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
A
Anthony Liguori 已提交
527
        qobject_decref(obj);
528
        QDECREF(str);
A
Anthony Liguori 已提交
529 530 531
    }
}

A
Anthony Liguori 已提交
532
static void simple_whitespace(void)
A
Anthony Liguori 已提交
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 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
{
    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;
579
        QString *str;
A
Anthony Liguori 已提交
580 581

        obj = qobject_from_json(test_cases[i].encoded);
A
Anthony Liguori 已提交
582 583
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
A
Anthony Liguori 已提交
584

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

587
        str = qobject_to_json(obj);
A
Anthony Liguori 已提交
588
        qobject_decref(obj);
589 590

        obj = qobject_from_json(qstring_get_str(str));
A
Anthony Liguori 已提交
591 592
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QLIST);
593

A
Anthony Liguori 已提交
594
        g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
595 596 597

        qobject_decref(obj);
        QDECREF(str);
A
Anthony Liguori 已提交
598 599 600
    }
}

A
Anthony Liguori 已提交
601
static void simple_varargs(void)
A
Anthony Liguori 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614
{
    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 已提交
615
    g_assert(embedded_obj != NULL);
A
Anthony Liguori 已提交
616 617

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

A
Anthony Liguori 已提交
620
    g_assert(compare_litqobj_to_qobj(&decoded, obj) == 1);
A
Anthony Liguori 已提交
621 622 623 624

    qobject_decref(obj);
}

A
Anthony Liguori 已提交
625
static void empty_input(void)
P
Paolo Bonzini 已提交
626
{
627 628 629
    const char *empty = "";

    QObject *obj = qobject_from_json(empty);
A
Anthony Liguori 已提交
630
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
631 632
}

A
Anthony Liguori 已提交
633
static void unterminated_string(void)
P
Paolo Bonzini 已提交
634 635
{
    QObject *obj = qobject_from_json("\"abc");
A
Anthony Liguori 已提交
636
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
637 638
}

A
Anthony Liguori 已提交
639
static void unterminated_sq_string(void)
P
Paolo Bonzini 已提交
640 641
{
    QObject *obj = qobject_from_json("'abc");
A
Anthony Liguori 已提交
642
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
643 644
}

A
Anthony Liguori 已提交
645
static void unterminated_escape(void)
P
Paolo Bonzini 已提交
646 647
{
    QObject *obj = qobject_from_json("\"abc\\\"");
A
Anthony Liguori 已提交
648
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
649 650
}

A
Anthony Liguori 已提交
651
static void unterminated_array(void)
P
Paolo Bonzini 已提交
652 653
{
    QObject *obj = qobject_from_json("[32");
A
Anthony Liguori 已提交
654
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
655 656
}

A
Anthony Liguori 已提交
657
static void unterminated_array_comma(void)
P
Paolo Bonzini 已提交
658 659
{
    QObject *obj = qobject_from_json("[32,");
A
Anthony Liguori 已提交
660
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
661 662
}

A
Anthony Liguori 已提交
663
static void invalid_array_comma(void)
P
Paolo Bonzini 已提交
664 665
{
    QObject *obj = qobject_from_json("[32,}");
A
Anthony Liguori 已提交
666
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
667 668
}

A
Anthony Liguori 已提交
669
static void unterminated_dict(void)
P
Paolo Bonzini 已提交
670 671
{
    QObject *obj = qobject_from_json("{'abc':32");
A
Anthony Liguori 已提交
672
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
673 674
}

A
Anthony Liguori 已提交
675
static void unterminated_dict_comma(void)
P
Paolo Bonzini 已提交
676 677
{
    QObject *obj = qobject_from_json("{'abc':32,");
A
Anthony Liguori 已提交
678
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
679 680
}

A
Anthony Liguori 已提交
681
static void invalid_dict_comma(void)
P
Paolo Bonzini 已提交
682 683
{
    QObject *obj = qobject_from_json("{'abc':32,}");
A
Anthony Liguori 已提交
684
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
685 686
}

A
Anthony Liguori 已提交
687
static void unterminated_literal(void)
P
Paolo Bonzini 已提交
688 689
{
    QObject *obj = qobject_from_json("nul");
A
Anthony Liguori 已提交
690
    g_assert(obj == NULL);
P
Paolo Bonzini 已提交
691 692
}

A
Anthony Liguori 已提交
693
int main(int argc, char **argv)
A
Anthony Liguori 已提交
694
{
A
Anthony Liguori 已提交
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
    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);
    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 已提交
726

A
Anthony Liguori 已提交
727
    return g_test_run();
A
Anthony Liguori 已提交
728
}