virjsontest.c 15.6 KB
Newer Older
1 2 3 4 5 6 7 8
#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "internal.h"
9
#include "virjson.h"
10 11 12 13
#include "testutils.h"

struct testInfo {
    const char *doc;
14
    const char *expect;
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    bool pass;
};


static int
testJSONFromString(const void *data)
{
    const struct testInfo *info = data;
    virJSONValuePtr json;
    int ret = -1;

    json = virJSONValueFromString(info->doc);

    if (info->pass) {
        if (!json) {
30
            VIR_TEST_VERBOSE("Fail to parse %s\n", info->doc);
31 32 33
            ret = -1;
            goto cleanup;
        } else {
34
            VIR_TEST_DEBUG("Parsed %s\n", info->doc);
35 36 37
        }
    } else {
        if (json) {
38
            VIR_TEST_VERBOSE("Should not have parsed %s\n", info->doc);
39 40 41
            ret = -1;
            goto cleanup;
        } else {
42
            VIR_TEST_DEBUG("Fail to parse %s\n", info->doc);
43 44 45 46 47
        }
    }

    ret = 0;

48
 cleanup:
49 50 51 52 53
    virJSONValueFree(json);
    return ret;
}


54 55 56 57 58
static int
testJSONAddRemove(const void *data)
{
    const struct testInfo *info = data;
    virJSONValuePtr json;
59
    virJSONValuePtr name = NULL;
60 61 62 63
    char *result = NULL;
    int ret = -1;

    json = virJSONValueFromString(info->doc);
64
    if (!json) {
65
        VIR_TEST_VERBOSE("Fail to parse %s\n", info->doc);
66 67 68
        ret = -1;
        goto cleanup;
    }
69 70 71 72

    switch (virJSONValueObjectRemoveKey(json, "name", &name)) {
    case 1:
        if (!info->pass) {
73
            VIR_TEST_VERBOSE("should not remove from non-object %s\n",
74
                             info->doc);
75 76 77 78 79 80
            goto cleanup;
        }
        break;
    case -1:
        if (!info->pass)
            ret = 0;
81 82
        else
            VIR_TEST_VERBOSE("Fail to recognize non-object %s\n", info->doc);
83 84
        goto cleanup;
    default:
85
        VIR_TEST_VERBOSE("unexpected result when removing from %s\n",
86
                         info->doc);
87 88 89
        goto cleanup;
    }
    if (STRNEQ_NULLABLE(virJSONValueGetString(name), "sample")) {
90
        VIR_TEST_VERBOSE("unexpected value after removing name: %s\n",
91
                         NULLSTR(virJSONValueGetString(name)));
92 93 94
        goto cleanup;
    }
    if (virJSONValueObjectRemoveKey(json, "name", NULL)) {
95
        VIR_TEST_VERBOSE("%s",
96
                         "unexpected success when removing missing key\n");
97 98 99
        goto cleanup;
    }
    if (virJSONValueObjectAppendString(json, "newname", "foo") < 0) {
100
        VIR_TEST_VERBOSE("%s", "unexpected failure adding new key\n");
101 102 103
        goto cleanup;
    }
    if (!(result = virJSONValueToString(json, false))) {
104
        VIR_TEST_VERBOSE("%s", "failed to stringize result\n");
105 106 107
        goto cleanup;
    }
    if (STRNEQ(info->expect, result)) {
108
        virTestDifference(stderr, info->expect, result);
109 110 111 112
        goto cleanup;
    }
    ret = 0;

113
 cleanup:
114 115 116 117 118 119 120
    virJSONValueFree(json);
    virJSONValueFree(name);
    VIR_FREE(result);
    return ret;
}


121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
static int
testJSONLookup(const void *data)
{
    const struct testInfo *info = data;
    virJSONValuePtr json;
    virJSONValuePtr value = NULL;
    char *result = NULL;
    int rc;
    int number;
    const char *str;
    int ret = -1;

    json = virJSONValueFromString(info->doc);
    if (!json) {
        VIR_TEST_VERBOSE("Fail to parse %s\n", info->doc);
        ret = -1;
        goto cleanup;
    }

    value = virJSONValueObjectGetObject(json, "a");
    if (value) {
        if (!info->pass) {
            VIR_TEST_VERBOSE("lookup for 'a' in '%s' should have failed\n",
                             info->doc);
            goto cleanup;
        } else {
            result = virJSONValueToString(value, false);
            if (STRNEQ_NULLABLE(result, "{}")) {
                VIR_TEST_VERBOSE("lookup for 'a' in '%s' found '%s' but "
                                 "should have found '{}'\n",
                                 info->doc, NULLSTR(result));
                goto cleanup;
            }
            VIR_FREE(result);
        }
    } else if (info->pass) {
        VIR_TEST_VERBOSE("lookup for 'a' in '%s' should have succeeded\n",
                         info->doc);
        goto cleanup;
    }

    number = 2;
    rc = virJSONValueObjectGetNumberInt(json, "b", &number);
    if (rc == 0) {
        if (!info->pass) {
            VIR_TEST_VERBOSE("lookup for 'b' in '%s' should have failed\n",
                             info->doc);
            goto cleanup;
        } else if (number != 1) {
            VIR_TEST_VERBOSE("lookup for 'b' in '%s' found %d but "
                             "should have found 1\n",
                             info->doc, number);
            goto cleanup;
        }
    } else if (info->pass) {
        VIR_TEST_VERBOSE("lookup for 'b' in '%s' should have succeeded\n",
                         info->doc);
        goto cleanup;
    }

    str = virJSONValueObjectGetString(json, "c");
    if (str) {
        if (!info->pass) {
            VIR_TEST_VERBOSE("lookup for 'c' in '%s' should have failed\n",
                             info->doc);
            goto cleanup;
        } else if (STRNEQ(str, "str")) {
            VIR_TEST_VERBOSE("lookup for 'c' in '%s' found '%s' but "
                             "should have found 'str'\n", info->doc, str);
                goto cleanup;
        }
    } else if (info->pass) {
        VIR_TEST_VERBOSE("lookup for 'c' in '%s' should have succeeded\n",
                         info->doc);
        goto cleanup;
    }

    value = virJSONValueObjectGetArray(json, "d");
    if (value) {
        if (!info->pass) {
            VIR_TEST_VERBOSE("lookup for 'd' in '%s' should have failed\n",
                             info->doc);
            goto cleanup;
        } else {
            result = virJSONValueToString(value, false);
            if (STRNEQ_NULLABLE(result, "[]")) {
                VIR_TEST_VERBOSE("lookup for 'd' in '%s' found '%s' but "
                                 "should have found '[]'\n",
                                 info->doc, NULLSTR(result));
                goto cleanup;
            }
            VIR_FREE(result);
        }
    } else if (info->pass) {
        VIR_TEST_VERBOSE("lookup for 'd' in '%s' should have succeeded\n",
                         info->doc);
        goto cleanup;
    }

    ret = 0;

 cleanup:
    virJSONValueFree(json);
    VIR_FREE(result);
    return ret;
}


M
Martin Kletzander 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
static int
testJSONCopy(const void *data)
{
    const struct testInfo *info = data;
    virJSONValuePtr json = NULL;
    virJSONValuePtr jsonCopy = NULL;
    char *result = NULL;
    char *resultCopy = NULL;
    int ret = -1;

    json = virJSONValueFromString(info->doc);
    if (!json) {
        if (virTestGetVerbose())
            fprintf(stderr, "Failed to parse %s\n", info->doc);
        ret = -1;
        goto cleanup;
    }

    jsonCopy = virJSONValueCopy(json);
    if (!jsonCopy) {
        if (virTestGetVerbose())
            fprintf(stderr, "Failed to copy JSON data\n");
        ret = -1;
        goto cleanup;
    }

    result = virJSONValueToString(json, false);
    if (!result) {
        if (virTestGetVerbose())
            fprintf(stderr, "Failed to format original JSON data\n");
        ret = -1;
        goto cleanup;
    }

    resultCopy = virJSONValueToString(json, false);
    if (!resultCopy) {
        if (virTestGetVerbose())
            fprintf(stderr, "Failed to format copied JSON data\n");
        ret = -1;
        goto cleanup;
    }

    if (STRNEQ(result, resultCopy)) {
        if (virTestGetVerbose())
273
            virTestDifference(stderr, result, resultCopy);
M
Martin Kletzander 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
        ret = -1;
        goto cleanup;
    }

    VIR_FREE(result);
    VIR_FREE(resultCopy);

    result = virJSONValueToString(json, true);
    if (!result) {
        if (virTestGetVerbose())
            fprintf(stderr, "Failed to format original JSON data\n");
        ret = -1;
        goto cleanup;
    }

    resultCopy = virJSONValueToString(json, true);
    if (!resultCopy) {
        if (virTestGetVerbose())
            fprintf(stderr, "Failed to format copied JSON data\n");
        ret = -1;
        goto cleanup;
    }

    if (STRNEQ(result, resultCopy)) {
        if (virTestGetVerbose())
299
            virTestDifference(stderr, result, resultCopy);
M
Martin Kletzander 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313
        ret = -1;
        goto cleanup;
    }

    ret = 0;
 cleanup:
    VIR_FREE(result);
    VIR_FREE(resultCopy);
    virJSONValueFree(json);
    virJSONValueFree(jsonCopy);
    return ret;
}


314 315 316 317 318
static int
mymain(void)
{
    int ret = 0;

319
#define DO_TEST_FULL(name, cmd, doc, expect, pass)                  \
320
    do {                                                            \
321
        struct testInfo info = { doc, expect, pass };               \
322
        if (virTestRun(name, testJSON ## cmd, &info) < 0)           \
323 324 325 326
            ret = -1;                                               \
    } while (0)

#define DO_TEST_PARSE(name, doc)                \
327
    DO_TEST_FULL(name, FromString, doc, NULL, true)
328

329 330 331 332
#define DO_TEST_PARSE_FAIL(name, doc)           \
    DO_TEST_FULL(name, FromString, doc, NULL, false)


333 334
    DO_TEST_PARSE("Simple", "{\"return\": {}, \"id\": \"libvirt-1\"}");
    DO_TEST_PARSE("NotSoSimple", "{\"QMP\": {\"version\": {\"qemu\":"
335 336
                  "{\"micro\": 91, \"minor\": 13, \"major\": 0},"
                  "\"package\": \" (qemu-kvm-devel)\"}, \"capabilities\": []}}");
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


    DO_TEST_PARSE("Harder", "{\"return\": [{\"filename\": "
                  "\"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server\","
                  "\"label\": \"charmonitor\"}, {\"filename\": \"pty:/dev/pts/158\","
                  "\"label\": \"charserial0\"}], \"id\": \"libvirt-3\"}");

    DO_TEST_PARSE("VeryHard", "{\"return\": [{\"name\": \"quit\"}, {\"name\":"
                  "\"eject\"}, {\"name\": \"change\"}, {\"name\": \"screendump\"},"
                  "{\"name\": \"stop\"}, {\"name\": \"cont\"}, {\"name\": "
                  "\"system_reset\"}, {\"name\": \"system_powerdown\"}, "
                  "{\"name\": \"device_add\"}, {\"name\": \"device_del\"}, "
                  "{\"name\": \"cpu\"}, {\"name\": \"memsave\"}, {\"name\": "
                  "\"pmemsave\"}, {\"name\": \"migrate\"}, {\"name\": "
                  "\"migrate_cancel\"}, {\"name\": \"migrate_set_speed\"},"
                  "{\"name\": \"client_migrate_info\"}, {\"name\": "
                  "\"migrate_set_downtime\"}, {\"name\": \"netdev_add\"}, "
                  "{\"name\": \"netdev_del\"}, {\"name\": \"block_resize\"},"
                  "{\"name\": \"balloon\"}, {\"name\": \"set_link\"}, {\"name\":"
                  "\"getfd\"}, {\"name\": \"closefd\"}, {\"name\": \"block_passwd\"},"
                  "{\"name\": \"set_password\"}, {\"name\": \"expire_password\"},"
                  "{\"name\": \"qmp_capabilities\"}, {\"name\": "
                  "\"human-monitor-command\"}, {\"name\": \"query-version\"},"
                  "{\"name\": \"query-commands\"}, {\"name\": \"query-chardev\"},"
                  "{\"name\": \"query-block\"}, {\"name\": \"query-blockstats\"}, "
                  "{\"name\": \"query-cpus\"}, {\"name\": \"query-pci\"}, {\"name\":"
                  "\"query-kvm\"}, {\"name\": \"query-status\"}, {\"name\": "
                  "\"query-mice\"}, {\"name\": \"query-vnc\"}, {\"name\": "
                  "\"query-spice\"}, {\"name\": \"query-name\"}, {\"name\": "
                  "\"query-uuid\"}, {\"name\": \"query-migrate\"}, {\"name\": "
                  "\"query-balloon\"}], \"id\": \"libvirt-2\"}");

369 370 371 372 373 374 375
    DO_TEST_FULL("add and remove", AddRemove,
                 "{\"name\": \"sample\", \"value\": true}",
                 "{\"value\":true,\"newname\":\"foo\"}",
                 true);
    DO_TEST_FULL("add and remove", AddRemove,
                 "[ 1 ]", NULL, false);

M
Martin Kletzander 已提交
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
    DO_TEST_FULL("copy and free", Copy,
                 "{\"return\": [{\"name\": \"quit\"}, {\"name\": \"eject\"},"
                 "{\"name\": \"change\"}, {\"name\": \"screendump\"},"
                 "{\"name\": \"stop\"}, {\"name\": \"cont\"}, {\"name\": "
                 "\"system_reset\"}, {\"name\": \"system_powerdown\"}, "
                 "{\"name\": \"device_add\"}, {\"name\": \"device_del\"}, "
                 "{\"name\": \"cpu\"}, {\"name\": \"memsave\"}, {\"name\": "
                 "\"pmemsave\"}, {\"name\": \"migrate\"}, {\"name\": "
                 "\"migrate_cancel\"}, {\"name\": \"migrate_set_speed\"},"
                 "{\"name\": \"client_migrate_info\"}, {\"name\": "
                 "\"migrate_set_downtime\"}, {\"name\": \"netdev_add\"}, "
                 "{\"name\": \"netdev_del\"}, {\"name\": \"block_resize\"},"
                 "{\"name\": \"balloon\"}, {\"name\": \"set_link\"}, {\"name\":"
                 "\"getfd\"}, {\"name\": \"closefd\"}, {\"name\": \"block_passwd\"},"
                 "{\"name\": \"set_password\"}, {\"name\": \"expire_password\"},"
                 "{\"name\": \"qmp_capabilities\"}, {\"name\": "
                 "\"human-monitor-command\"}, {\"name\": \"query-version\"},"
                 "{\"name\": \"query-commands\"}, {\"name\": \"query-chardev\"},"
                 "{\"name\": \"query-block\"}, {\"name\": \"query-blockstats\"}, "
                 "{\"name\": \"query-cpus\"}, {\"name\": \"query-pci\"}, {\"name\":"
                 "\"query-kvm\"}, {\"name\": \"query-status\"}, {\"name\": "
                 "\"query-mice\"}, {\"name\": \"query-vnc\"}, {\"name\": "
                 "\"query-spice\"}, {\"name\": \"query-name\"}, {\"name\": "
                 "\"query-uuid\"}, {\"name\": \"query-migrate\"}, {\"name\": "
                 "\"query-balloon\"}], \"id\": \"libvirt-2\"}", NULL, true);

402 403 404 405 406 407 408 409 410 411 412

    DO_TEST_PARSE("almost nothing", "[]");
    DO_TEST_PARSE_FAIL("nothing", "");

    DO_TEST_PARSE("number without garbage", "[ 234545 ]");
    DO_TEST_PARSE_FAIL("number with garbage", "[ 2345b45 ]");

    DO_TEST_PARSE("float without garbage", "[ 0.0314159e+100 ]");
    DO_TEST_PARSE_FAIL("float with garbage", "[ 0.0314159ee+100 ]");

    DO_TEST_PARSE("string", "[ \"The meaning of life\" ]");
413 414
    DO_TEST_PARSE_FAIL("unterminated string", "[ \"The meaning of lif ]");

E
Eric Blake 已提交
415 416 417 418
    DO_TEST_PARSE("integer", "1");
    DO_TEST_PARSE("boolean", "true");
    DO_TEST_PARSE("null", "null");
    DO_TEST_PARSE_FAIL("incomplete keyword", "tr");
419
    DO_TEST_PARSE_FAIL("overdone keyword", "[ truest ]");
E
Eric Blake 已提交
420
    DO_TEST_PARSE_FAIL("unknown keyword", "huh");
E
Eric Blake 已提交
421
    DO_TEST_PARSE_FAIL("comments", "[ /* nope */\n1 // not this either\n]");
E
Eric Blake 已提交
422 423
    DO_TEST_PARSE_FAIL("trailing garbage", "[] []");
    DO_TEST_PARSE_FAIL("list without array", "1, 1");
424
    DO_TEST_PARSE_FAIL("parser abuse", "1] [2");
E
Eric Blake 已提交
425
    DO_TEST_PARSE_FAIL("invalid UTF-8", "\"\x80\"");
426 427

    DO_TEST_PARSE_FAIL("object with numeric keys", "{ 1:1, 2:1, 3:2 }");
428 429 430
    DO_TEST_PARSE_FAIL("unterminated object", "{ \"1\":1, \"2\":1, \"3\":2");
    DO_TEST_PARSE_FAIL("unterminated array of objects",
                       "[ {\"name\": \"John\"}, {\"name\": \"Paul\"}, ");
431 432
    DO_TEST_PARSE_FAIL("array of an object with an array as a key",
                       "[ {[\"key1\", \"key2\"]: \"value\"} ]");
433
    DO_TEST_PARSE_FAIL("object with unterminated key", "{ \"key:7 }");
E
Eric Blake 已提交
434
    DO_TEST_PARSE_FAIL("duplicate key", "{ \"a\": 1, \"a\": 1 }");
435

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
    DO_TEST_FULL("lookup on array", Lookup,
                 "[ 1 ]", NULL, false);
    DO_TEST_FULL("lookup on string", Lookup,
                 "\"str\"", NULL, false);
    DO_TEST_FULL("lookup on integer", Lookup,
                 "1", NULL, false);
    DO_TEST_FULL("lookup with missing key", Lookup,
                 "{ }", NULL, false);
    DO_TEST_FULL("lookup with wrong type", Lookup,
                 "{ \"a\": 1, \"b\": \"str\", \"c\": [], \"d\": {} }",
                 NULL, false);
    DO_TEST_FULL("lookup with correct type", Lookup,
                 "{ \"a\": {}, \"b\": 1, \"c\": \"str\", \"d\": [] }",
                 NULL, true);

451 452 453
    return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

454
VIR_TEST_MAIN(mymain)