qemu/tests/test-keyval.c
<<
>>
Prefs
   1/*
   2 * Unit tests for parsing of KEY=VALUE,... strings
   3 *
   4 * Copyright (C) 2017 Red Hat Inc.
   5 *
   6 * Authors:
   7 *  Markus Armbruster <armbru@redhat.com>,
   8 *
   9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10 * See the COPYING file in the top-level directory.
  11 */
  12
  13#include "qemu/osdep.h"
  14#include "qemu/units.h"
  15#include "qapi/error.h"
  16#include "qapi/qmp/qdict.h"
  17#include "qapi/qmp/qlist.h"
  18#include "qapi/qmp/qstring.h"
  19#include "qapi/qobject-input-visitor.h"
  20#include "test-qapi-visit.h"
  21#include "qemu/cutils.h"
  22#include "qemu/option.h"
  23
  24static void test_keyval_parse(void)
  25{
  26    Error *err = NULL;
  27    QDict *qdict, *sub_qdict;
  28    char long_key[129];
  29    char *params;
  30    bool help;
  31
  32    /* Nothing */
  33    qdict = keyval_parse("", NULL, NULL, &error_abort);
  34    g_assert_cmpuint(qdict_size(qdict), ==, 0);
  35    qobject_unref(qdict);
  36
  37    /* Empty key (qemu_opts_parse() accepts this) */
  38    qdict = keyval_parse("=val", NULL, NULL, &err);
  39    error_free_or_abort(&err);
  40    g_assert(!qdict);
  41
  42    /* Empty key fragment */
  43    qdict = keyval_parse(".", NULL, NULL, &err);
  44    error_free_or_abort(&err);
  45    g_assert(!qdict);
  46    qdict = keyval_parse("key.", NULL, NULL, &err);
  47    error_free_or_abort(&err);
  48    g_assert(!qdict);
  49
  50    /* Invalid non-empty key (qemu_opts_parse() doesn't care) */
  51    qdict = keyval_parse("7up=val", NULL, NULL, &err);
  52    error_free_or_abort(&err);
  53    g_assert(!qdict);
  54
  55    /* Overlong key */
  56    memset(long_key, 'a', 127);
  57    long_key[127] = 'z';
  58    long_key[128] = 0;
  59    params = g_strdup_printf("k.%s=v", long_key);
  60    qdict = keyval_parse(params + 2, NULL, NULL, &err);
  61    error_free_or_abort(&err);
  62    g_assert(!qdict);
  63
  64    /* Overlong key fragment */
  65    qdict = keyval_parse(params, NULL, NULL, &err);
  66    error_free_or_abort(&err);
  67    g_assert(!qdict);
  68    g_free(params);
  69
  70    /* Long key (qemu_opts_parse() accepts and truncates silently) */
  71    params = g_strdup_printf("k.%s=v", long_key + 1);
  72    qdict = keyval_parse(params + 2, NULL, NULL, &error_abort);
  73    g_assert_cmpuint(qdict_size(qdict), ==, 1);
  74    g_assert_cmpstr(qdict_get_try_str(qdict, long_key + 1), ==, "v");
  75    qobject_unref(qdict);
  76
  77    /* Long key fragment */
  78    qdict = keyval_parse(params, NULL, NULL, &error_abort);
  79    g_assert_cmpuint(qdict_size(qdict), ==, 1);
  80    sub_qdict = qdict_get_qdict(qdict, "k");
  81    g_assert(sub_qdict);
  82    g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
  83    g_assert_cmpstr(qdict_get_try_str(sub_qdict, long_key + 1), ==, "v");
  84    qobject_unref(qdict);
  85    g_free(params);
  86
  87    /* Crap after valid key */
  88    qdict = keyval_parse("key[0]=val", NULL, NULL, &err);
  89    error_free_or_abort(&err);
  90    g_assert(!qdict);
  91
  92    /* Multiple keys, last one wins */
  93    qdict = keyval_parse("a=1,b=2,,x,a=3", NULL, NULL, &error_abort);
  94    g_assert_cmpuint(qdict_size(qdict), ==, 2);
  95    g_assert_cmpstr(qdict_get_try_str(qdict, "a"), ==, "3");
  96    g_assert_cmpstr(qdict_get_try_str(qdict, "b"), ==, "2,x");
  97    qobject_unref(qdict);
  98
  99    /* Even when it doesn't in qemu_opts_parse() */
 100    qdict = keyval_parse("id=foo,id=bar", NULL, NULL, &error_abort);
 101    g_assert_cmpuint(qdict_size(qdict), ==, 1);
 102    g_assert_cmpstr(qdict_get_try_str(qdict, "id"), ==, "bar");
 103    qobject_unref(qdict);
 104
 105    /* Dotted keys */
 106    qdict = keyval_parse("a.b.c=1,a.b.c=2,d=3", NULL, NULL, &error_abort);
 107    g_assert_cmpuint(qdict_size(qdict), ==, 2);
 108    sub_qdict = qdict_get_qdict(qdict, "a");
 109    g_assert(sub_qdict);
 110    g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
 111    sub_qdict = qdict_get_qdict(sub_qdict, "b");
 112    g_assert(sub_qdict);
 113    g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
 114    g_assert_cmpstr(qdict_get_try_str(sub_qdict, "c"), ==, "2");
 115    g_assert_cmpstr(qdict_get_try_str(qdict, "d"), ==, "3");
 116    qobject_unref(qdict);
 117
 118    /* Inconsistent dotted keys */
 119    qdict = keyval_parse("a.b=1,a=2", NULL, NULL, &err);
 120    error_free_or_abort(&err);
 121    g_assert(!qdict);
 122    qdict = keyval_parse("a.b=1,a.b.c=2", NULL, NULL, &err);
 123    error_free_or_abort(&err);
 124    g_assert(!qdict);
 125
 126    /* Trailing comma is ignored */
 127    qdict = keyval_parse("x=y,", NULL, NULL, &error_abort);
 128    g_assert_cmpuint(qdict_size(qdict), ==, 1);
 129    g_assert_cmpstr(qdict_get_try_str(qdict, "x"), ==, "y");
 130    qobject_unref(qdict);
 131
 132    /* Except when it isn't */
 133    qdict = keyval_parse(",", NULL, NULL, &err);
 134    error_free_or_abort(&err);
 135    g_assert(!qdict);
 136
 137    /* Value containing ,id= not misinterpreted as qemu_opts_parse() does */
 138    qdict = keyval_parse("x=,,id=bar", NULL, NULL, &error_abort);
 139    g_assert_cmpuint(qdict_size(qdict), ==, 1);
 140    g_assert_cmpstr(qdict_get_try_str(qdict, "x"), ==, ",id=bar");
 141    qobject_unref(qdict);
 142
 143    /* Anti-social ID is left to caller (qemu_opts_parse() rejects it) */
 144    qdict = keyval_parse("id=666", NULL, NULL, &error_abort);
 145    g_assert_cmpuint(qdict_size(qdict), ==, 1);
 146    g_assert_cmpstr(qdict_get_try_str(qdict, "id"), ==, "666");
 147    qobject_unref(qdict);
 148
 149    /* Implied value not supported (unlike qemu_opts_parse()) */
 150    qdict = keyval_parse("an,noaus,noaus=", NULL, NULL, &err);
 151    error_free_or_abort(&err);
 152    g_assert(!qdict);
 153
 154    /* Implied value, key "no" (qemu_opts_parse(): negated empty key) */
 155    qdict = keyval_parse("no", NULL, NULL, &err);
 156    error_free_or_abort(&err);
 157    g_assert(!qdict);
 158
 159    /* Implied key */
 160    qdict = keyval_parse("an,aus=off,noaus=", "implied", NULL, &error_abort);
 161    g_assert_cmpuint(qdict_size(qdict), ==, 3);
 162    g_assert_cmpstr(qdict_get_try_str(qdict, "implied"), ==, "an");
 163    g_assert_cmpstr(qdict_get_try_str(qdict, "aus"), ==, "off");
 164    g_assert_cmpstr(qdict_get_try_str(qdict, "noaus"), ==, "");
 165    qobject_unref(qdict);
 166
 167    /* Implied dotted key */
 168    qdict = keyval_parse("val", "eins.zwei", NULL, &error_abort);
 169    g_assert_cmpuint(qdict_size(qdict), ==, 1);
 170    sub_qdict = qdict_get_qdict(qdict, "eins");
 171    g_assert(sub_qdict);
 172    g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
 173    g_assert_cmpstr(qdict_get_try_str(sub_qdict, "zwei"), ==, "val");
 174    qobject_unref(qdict);
 175
 176    /* Implied key with empty value (qemu_opts_parse() accepts this) */
 177    qdict = keyval_parse(",", "implied", NULL, &err);
 178    error_free_or_abort(&err);
 179    g_assert(!qdict);
 180
 181    /* Likewise (qemu_opts_parse(): implied key with comma value) */
 182    qdict = keyval_parse(",,,a=1", "implied", NULL, &err);
 183    error_free_or_abort(&err);
 184    g_assert(!qdict);
 185
 186    /* Implied key's value can't have comma (qemu_opts_parse(): it can) */
 187    qdict = keyval_parse("val,,ue", "implied", NULL, &err);
 188    error_free_or_abort(&err);
 189    g_assert(!qdict);
 190
 191    /* Empty key is not an implied key */
 192    qdict = keyval_parse("=val", "implied", NULL, &err);
 193    error_free_or_abort(&err);
 194    g_assert(!qdict);
 195
 196    /* "help" by itself, without implied key */
 197    qdict = keyval_parse("help", NULL, &help, &error_abort);
 198    g_assert_cmpuint(qdict_size(qdict), ==, 0);
 199    g_assert(help);
 200    qobject_unref(qdict);
 201
 202    /* "help" by itself, with implied key */
 203    qdict = keyval_parse("help", "implied", &help, &error_abort);
 204    g_assert_cmpuint(qdict_size(qdict), ==, 0);
 205    g_assert(help);
 206    qobject_unref(qdict);
 207
 208    /* "help" when no help is available, without implied key */
 209    qdict = keyval_parse("help", NULL, NULL, &err);
 210    error_free_or_abort(&err);
 211    g_assert(!qdict);
 212
 213    /* "help" when no help is available, with implied key */
 214    qdict = keyval_parse("help", "implied", NULL, &err);
 215    error_free_or_abort(&err);
 216    g_assert(!qdict);
 217
 218    /* Key "help" */
 219    qdict = keyval_parse("help=on", NULL, &help, &error_abort);
 220    g_assert_cmpuint(qdict_size(qdict), ==, 1);
 221    g_assert_cmpstr(qdict_get_try_str(qdict, "help"), ==, "on");
 222    g_assert(!help);
 223    qobject_unref(qdict);
 224
 225    /* "help" followed by crap, without implied key */
 226    qdict = keyval_parse("help.abc", NULL, &help, &err);
 227    error_free_or_abort(&err);
 228    g_assert(!qdict);
 229
 230    /* "help" followed by crap, with implied key */
 231    qdict = keyval_parse("help.abc", "implied", &help, &err);
 232    g_assert_cmpuint(qdict_size(qdict), ==, 1);
 233    g_assert_cmpstr(qdict_get_try_str(qdict, "implied"), ==, "help.abc");
 234    g_assert(!help);
 235    qobject_unref(qdict);
 236
 237    /* "help" with other stuff, without implied key */
 238    qdict = keyval_parse("number=42,help,foo=bar", NULL, &help, &error_abort);
 239    g_assert_cmpuint(qdict_size(qdict), ==, 2);
 240    g_assert_cmpstr(qdict_get_try_str(qdict, "number"), ==, "42");
 241    g_assert_cmpstr(qdict_get_try_str(qdict, "foo"), ==, "bar");
 242    g_assert(help);
 243    qobject_unref(qdict);
 244
 245    /* "help" with other stuff, with implied key */
 246    qdict = keyval_parse("val,help,foo=bar", "implied", &help, &error_abort);
 247    g_assert_cmpuint(qdict_size(qdict), ==, 2);
 248    g_assert_cmpstr(qdict_get_try_str(qdict, "implied"), ==, "val");
 249    g_assert_cmpstr(qdict_get_try_str(qdict, "foo"), ==, "bar");
 250    g_assert(help);
 251    qobject_unref(qdict);
 252}
 253
 254static void check_list012(QList *qlist)
 255{
 256    static const char *expected[] = { "null", "eins", "zwei" };
 257    int i;
 258    QString *qstr;
 259
 260    g_assert(qlist);
 261    for (i = 0; i < ARRAY_SIZE(expected); i++) {
 262        qstr = qobject_to(QString, qlist_pop(qlist));
 263        g_assert(qstr);
 264        g_assert_cmpstr(qstring_get_str(qstr), ==, expected[i]);
 265        qobject_unref(qstr);
 266    }
 267    g_assert(qlist_empty(qlist));
 268}
 269
 270static void test_keyval_parse_list(void)
 271{
 272    Error *err = NULL;
 273    QDict *qdict, *sub_qdict;
 274
 275    /* Root can't be a list */
 276    qdict = keyval_parse("0=1", NULL, NULL, &err);
 277    error_free_or_abort(&err);
 278    g_assert(!qdict);
 279
 280    /* List elements need not be in order */
 281    qdict = keyval_parse("list.0=null,list.2=zwei,list.1=eins", NULL, NULL,
 282                         &error_abort);
 283    g_assert_cmpint(qdict_size(qdict), ==, 1);
 284    check_list012(qdict_get_qlist(qdict, "list"));
 285    qobject_unref(qdict);
 286
 287    /* Multiple indexes, last one wins */
 288    qdict = keyval_parse("list.1=goner,list.0=null,list.01=eins,list.2=zwei",
 289                         NULL, NULL, &error_abort);
 290    g_assert_cmpint(qdict_size(qdict), ==, 1);
 291    check_list012(qdict_get_qlist(qdict, "list"));
 292    qobject_unref(qdict);
 293
 294    /* List at deeper nesting */
 295    qdict = keyval_parse("a.list.1=eins,a.list.00=null,a.list.2=zwei", NULL,
 296                         NULL, &error_abort);
 297    g_assert_cmpint(qdict_size(qdict), ==, 1);
 298    sub_qdict = qdict_get_qdict(qdict, "a");
 299    g_assert_cmpint(qdict_size(sub_qdict), ==, 1);
 300    check_list012(qdict_get_qlist(sub_qdict, "list"));
 301    qobject_unref(qdict);
 302
 303    /* Inconsistent dotted keys: both list and dictionary */
 304    qdict = keyval_parse("a.b.c=1,a.b.0=2", NULL, NULL, &err);
 305    error_free_or_abort(&err);
 306    g_assert(!qdict);
 307    qdict = keyval_parse("a.0.c=1,a.b.c=2", NULL, NULL, &err);
 308    error_free_or_abort(&err);
 309    g_assert(!qdict);
 310
 311    /* Missing list indexes */
 312    qdict = keyval_parse("list.1=lonely", NULL, NULL, &err);
 313    error_free_or_abort(&err);
 314    g_assert(!qdict);
 315    qdict = keyval_parse("list.0=null,list.2=eins,list.02=zwei", NULL, NULL,
 316                         &err);
 317    error_free_or_abort(&err);
 318    g_assert(!qdict);
 319}
 320
 321static void test_keyval_visit_bool(void)
 322{
 323    Error *err = NULL;
 324    Visitor *v;
 325    QDict *qdict;
 326    bool b;
 327
 328    qdict = keyval_parse("bool1=on,bool2=off", NULL, NULL, &error_abort);
 329    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 330    qobject_unref(qdict);
 331    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 332    visit_type_bool(v, "bool1", &b, &error_abort);
 333    g_assert(b);
 334    visit_type_bool(v, "bool2", &b, &error_abort);
 335    g_assert(!b);
 336    visit_check_struct(v, &error_abort);
 337    visit_end_struct(v, NULL);
 338    visit_free(v);
 339
 340    qdict = keyval_parse("bool1=offer", NULL, NULL, &error_abort);
 341    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 342    qobject_unref(qdict);
 343    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 344    visit_type_bool(v, "bool1", &b, &err);
 345    error_free_or_abort(&err);
 346    visit_end_struct(v, NULL);
 347    visit_free(v);
 348}
 349
 350static void test_keyval_visit_number(void)
 351{
 352    Error *err = NULL;
 353    Visitor *v;
 354    QDict *qdict;
 355    uint64_t u;
 356
 357    /* Lower limit zero */
 358    qdict = keyval_parse("number1=0", NULL, NULL, &error_abort);
 359    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 360    qobject_unref(qdict);
 361    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 362    visit_type_uint64(v, "number1", &u, &error_abort);
 363    g_assert_cmpuint(u, ==, 0);
 364    visit_check_struct(v, &error_abort);
 365    visit_end_struct(v, NULL);
 366    visit_free(v);
 367
 368    /* Upper limit 2^64-1 */
 369    qdict = keyval_parse("number1=18446744073709551615,number2=-1", NULL,
 370                         NULL, &error_abort);
 371    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 372    qobject_unref(qdict);
 373    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 374    visit_type_uint64(v, "number1", &u, &error_abort);
 375    g_assert_cmphex(u, ==, UINT64_MAX);
 376    visit_type_uint64(v, "number2", &u, &error_abort);
 377    g_assert_cmphex(u, ==, UINT64_MAX);
 378    visit_check_struct(v, &error_abort);
 379    visit_end_struct(v, NULL);
 380    visit_free(v);
 381
 382    /* Above upper limit */
 383    qdict = keyval_parse("number1=18446744073709551616", NULL, NULL,
 384                         &error_abort);
 385    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 386    qobject_unref(qdict);
 387    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 388    visit_type_uint64(v, "number1", &u, &err);
 389    error_free_or_abort(&err);
 390    visit_end_struct(v, NULL);
 391    visit_free(v);
 392
 393    /* Below lower limit */
 394    qdict = keyval_parse("number1=-18446744073709551616", NULL, NULL,
 395                         &error_abort);
 396    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 397    qobject_unref(qdict);
 398    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 399    visit_type_uint64(v, "number1", &u, &err);
 400    error_free_or_abort(&err);
 401    visit_end_struct(v, NULL);
 402    visit_free(v);
 403
 404    /* Hex and octal */
 405    qdict = keyval_parse("number1=0x2a,number2=052", NULL, NULL, &error_abort);
 406    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 407    qobject_unref(qdict);
 408    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 409    visit_type_uint64(v, "number1", &u, &error_abort);
 410    g_assert_cmpuint(u, ==, 42);
 411    visit_type_uint64(v, "number2", &u, &error_abort);
 412    g_assert_cmpuint(u, ==, 42);
 413    visit_check_struct(v, &error_abort);
 414    visit_end_struct(v, NULL);
 415    visit_free(v);
 416
 417    /* Trailing crap */
 418    qdict = keyval_parse("number1=3.14,number2=08", NULL, NULL, &error_abort);
 419    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 420    qobject_unref(qdict);
 421    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 422    visit_type_uint64(v, "number1", &u, &err);
 423    error_free_or_abort(&err);
 424    visit_type_uint64(v, "number2", &u, &err);
 425    error_free_or_abort(&err);
 426    visit_end_struct(v, NULL);
 427    visit_free(v);
 428}
 429
 430static void test_keyval_visit_size(void)
 431{
 432    Error *err = NULL;
 433    Visitor *v;
 434    QDict *qdict;
 435    uint64_t sz;
 436
 437    /* Lower limit zero */
 438    qdict = keyval_parse("sz1=0", NULL, NULL, &error_abort);
 439    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 440    qobject_unref(qdict);
 441    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 442    visit_type_size(v, "sz1", &sz, &error_abort);
 443    g_assert_cmpuint(sz, ==, 0);
 444    visit_check_struct(v, &error_abort);
 445    visit_end_struct(v, NULL);
 446    visit_free(v);
 447
 448    /* Note: precision is 53 bits since we're parsing with strtod() */
 449
 450    /* Around limit of precision: 2^53-1, 2^53, 2^53+1 */
 451    qdict = keyval_parse("sz1=9007199254740991,"
 452                         "sz2=9007199254740992,"
 453                         "sz3=9007199254740993",
 454                         NULL, NULL, &error_abort);
 455    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 456    qobject_unref(qdict);
 457    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 458    visit_type_size(v, "sz1", &sz, &error_abort);
 459    g_assert_cmphex(sz, ==, 0x1fffffffffffff);
 460    visit_type_size(v, "sz2", &sz, &error_abort);
 461    g_assert_cmphex(sz, ==, 0x20000000000000);
 462    visit_type_size(v, "sz3", &sz, &error_abort);
 463    g_assert_cmphex(sz, ==, 0x20000000000000);
 464    visit_check_struct(v, &error_abort);
 465    visit_end_struct(v, NULL);
 466    visit_free(v);
 467
 468    /* Close to signed upper limit 0x7ffffffffffffc00 (53 msbs set) */
 469    qdict = keyval_parse("sz1=9223372036854774784," /* 7ffffffffffffc00 */
 470                         "sz2=9223372036854775295", /* 7ffffffffffffdff */
 471                         NULL, NULL, &error_abort);
 472    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 473    qobject_unref(qdict);
 474    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 475    visit_type_size(v, "sz1", &sz, &error_abort);
 476    g_assert_cmphex(sz, ==, 0x7ffffffffffffc00);
 477    visit_type_size(v, "sz2", &sz, &error_abort);
 478    g_assert_cmphex(sz, ==, 0x7ffffffffffffc00);
 479    visit_check_struct(v, &error_abort);
 480    visit_end_struct(v, NULL);
 481    visit_free(v);
 482
 483    /* Close to actual upper limit 0xfffffffffffff800 (53 msbs set) */
 484    qdict = keyval_parse("sz1=18446744073709549568," /* fffffffffffff800 */
 485                         "sz2=18446744073709550591", /* fffffffffffffbff */
 486                         NULL, NULL, &error_abort);
 487    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 488    qobject_unref(qdict);
 489    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 490    visit_type_size(v, "sz1", &sz, &error_abort);
 491    g_assert_cmphex(sz, ==, 0xfffffffffffff800);
 492    visit_type_size(v, "sz2", &sz, &error_abort);
 493    g_assert_cmphex(sz, ==, 0xfffffffffffff800);
 494    visit_check_struct(v, &error_abort);
 495    visit_end_struct(v, NULL);
 496    visit_free(v);
 497
 498    /* Beyond limits */
 499    qdict = keyval_parse("sz1=-1,"
 500                         "sz2=18446744073709550592", /* fffffffffffffc00 */
 501                         NULL, NULL, &error_abort);
 502    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 503    qobject_unref(qdict);
 504    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 505    visit_type_size(v, "sz1", &sz, &err);
 506    error_free_or_abort(&err);
 507    visit_type_size(v, "sz2", &sz, &err);
 508    error_free_or_abort(&err);
 509    visit_end_struct(v, NULL);
 510    visit_free(v);
 511
 512    /* Suffixes */
 513    qdict = keyval_parse("sz1=8b,sz2=1.5k,sz3=2M,sz4=0.1G,sz5=16777215T",
 514                         NULL, NULL, &error_abort);
 515    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 516    qobject_unref(qdict);
 517    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 518    visit_type_size(v, "sz1", &sz, &error_abort);
 519    g_assert_cmpuint(sz, ==, 8);
 520    visit_type_size(v, "sz2", &sz, &error_abort);
 521    g_assert_cmpuint(sz, ==, 1536);
 522    visit_type_size(v, "sz3", &sz, &error_abort);
 523    g_assert_cmphex(sz, ==, 2 * MiB);
 524    visit_type_size(v, "sz4", &sz, &error_abort);
 525    g_assert_cmphex(sz, ==, GiB / 10);
 526    visit_type_size(v, "sz5", &sz, &error_abort);
 527    g_assert_cmphex(sz, ==, 16777215ULL * TiB);
 528    visit_check_struct(v, &error_abort);
 529    visit_end_struct(v, NULL);
 530    visit_free(v);
 531
 532    /* Beyond limit with suffix */
 533    qdict = keyval_parse("sz1=16777216T", NULL, NULL, &error_abort);
 534    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 535    qobject_unref(qdict);
 536    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 537    visit_type_size(v, "sz1", &sz, &err);
 538    error_free_or_abort(&err);
 539    visit_end_struct(v, NULL);
 540    visit_free(v);
 541
 542    /* Trailing crap */
 543    qdict = keyval_parse("sz1=0Z,sz2=16Gi", NULL, NULL, &error_abort);
 544    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 545    qobject_unref(qdict);
 546    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 547    visit_type_size(v, "sz1", &sz, &err);
 548    error_free_or_abort(&err);
 549    visit_type_size(v, "sz2", &sz, &err);
 550    error_free_or_abort(&err);
 551    visit_end_struct(v, NULL);
 552    visit_free(v);
 553}
 554
 555static void test_keyval_visit_dict(void)
 556{
 557    Error *err = NULL;
 558    Visitor *v;
 559    QDict *qdict;
 560    int64_t i;
 561
 562    qdict = keyval_parse("a.b.c=1,a.b.c=2,d=3", NULL, NULL, &error_abort);
 563    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 564    qobject_unref(qdict);
 565    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 566    visit_start_struct(v, "a", NULL, 0, &error_abort);
 567    visit_start_struct(v, "b", NULL, 0, &error_abort);
 568    visit_type_int(v, "c", &i, &error_abort);
 569    g_assert_cmpint(i, ==, 2);
 570    visit_check_struct(v, &error_abort);
 571    visit_end_struct(v, NULL);
 572    visit_check_struct(v, &error_abort);
 573    visit_end_struct(v, NULL);
 574    visit_type_int(v, "d", &i, &error_abort);
 575    g_assert_cmpint(i, ==, 3);
 576    visit_check_struct(v, &error_abort);
 577    visit_end_struct(v, NULL);
 578    visit_free(v);
 579
 580    qdict = keyval_parse("a.b=", NULL, NULL, &error_abort);
 581    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 582    qobject_unref(qdict);
 583    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 584    visit_start_struct(v, "a", NULL, 0, &error_abort);
 585    visit_type_int(v, "c", &i, &err);   /* a.c missing */
 586    error_free_or_abort(&err);
 587    visit_check_struct(v, &err);
 588    error_free_or_abort(&err);          /* a.b unexpected */
 589    visit_end_struct(v, NULL);
 590    visit_check_struct(v, &error_abort);
 591    visit_end_struct(v, NULL);
 592    visit_free(v);
 593}
 594
 595static void test_keyval_visit_list(void)
 596{
 597    Error *err = NULL;
 598    Visitor *v;
 599    QDict *qdict;
 600    char *s;
 601
 602    qdict = keyval_parse("a.0=,a.1=I,a.2.0=II", NULL, NULL, &error_abort);
 603    /* TODO empty list */
 604    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 605    qobject_unref(qdict);
 606    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 607    visit_start_list(v, "a", NULL, 0, &error_abort);
 608    visit_type_str(v, NULL, &s, &error_abort);
 609    g_assert_cmpstr(s, ==, "");
 610    g_free(s);
 611    visit_type_str(v, NULL, &s, &error_abort);
 612    g_assert_cmpstr(s, ==, "I");
 613    g_free(s);
 614    visit_start_list(v, NULL, NULL, 0, &error_abort);
 615    visit_type_str(v, NULL, &s, &error_abort);
 616    g_assert_cmpstr(s, ==, "II");
 617    g_free(s);
 618    visit_check_list(v, &error_abort);
 619    visit_end_list(v, NULL);
 620    visit_check_list(v, &error_abort);
 621    visit_end_list(v, NULL);
 622    visit_check_struct(v, &error_abort);
 623    visit_end_struct(v, NULL);
 624    visit_free(v);
 625
 626    qdict = keyval_parse("a.0=,b.0.0=head", NULL, NULL, &error_abort);
 627    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 628    qobject_unref(qdict);
 629    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 630    visit_start_list(v, "a", NULL, 0, &error_abort);
 631    visit_check_list(v, &err);  /* a[0] unexpected */
 632    error_free_or_abort(&err);
 633    visit_end_list(v, NULL);
 634    visit_start_list(v, "b", NULL, 0, &error_abort);
 635    visit_start_list(v, NULL, NULL, 0, &error_abort);
 636    visit_type_str(v, NULL, &s, &error_abort);
 637    g_assert_cmpstr(s, ==, "head");
 638    g_free(s);
 639    visit_type_str(v, NULL, &s, &err); /* b[0][1] missing */
 640    error_free_or_abort(&err);
 641    visit_end_list(v, NULL);
 642    visit_end_list(v, NULL);
 643    visit_check_struct(v, &error_abort);
 644    visit_end_struct(v, NULL);
 645    visit_free(v);
 646}
 647
 648static void test_keyval_visit_optional(void)
 649{
 650    Visitor *v;
 651    QDict *qdict;
 652    bool present;
 653    int64_t i;
 654
 655    qdict = keyval_parse("a.b=1", NULL, NULL, &error_abort);
 656    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 657    qobject_unref(qdict);
 658    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 659    visit_optional(v, "b", &present);
 660    g_assert(!present);         /* b missing */
 661    visit_optional(v, "a", &present);
 662    g_assert(present);          /* a present */
 663    visit_start_struct(v, "a", NULL, 0, &error_abort);
 664    visit_optional(v, "b", &present);
 665    g_assert(present);          /* a.b present */
 666    visit_type_int(v, "b", &i, &error_abort);
 667    g_assert_cmpint(i, ==, 1);
 668    visit_optional(v, "a", &present);
 669    g_assert(!present);         /* a.a missing */
 670    visit_check_struct(v, &error_abort);
 671    visit_end_struct(v, NULL);
 672    visit_check_struct(v, &error_abort);
 673    visit_end_struct(v, NULL);
 674    visit_free(v);
 675}
 676
 677static void test_keyval_visit_alternate(void)
 678{
 679    Error *err = NULL;
 680    Visitor *v;
 681    QDict *qdict;
 682    AltStrObj *aso;
 683    AltNumEnum *ane;
 684    AltEnumBool *aeb;
 685
 686    /*
 687     * Can't do scalar alternate variants other than string.  You get
 688     * the string variant if there is one, else an error.
 689     * TODO make it work for unambiguous cases like AltEnumBool below
 690     */
 691    qdict = keyval_parse("a=1,b=2,c=on", NULL, NULL, &error_abort);
 692    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 693    qobject_unref(qdict);
 694    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 695    visit_type_AltStrObj(v, "a", &aso, &error_abort);
 696    g_assert_cmpint(aso->type, ==, QTYPE_QSTRING);
 697    g_assert_cmpstr(aso->u.s, ==, "1");
 698    qapi_free_AltStrObj(aso);
 699    visit_type_AltNumEnum(v, "b", &ane, &err);
 700    error_free_or_abort(&err);
 701    visit_type_AltEnumBool(v, "c", &aeb, &err);
 702    error_free_or_abort(&err);
 703    visit_end_struct(v, NULL);
 704    visit_free(v);
 705}
 706
 707static void test_keyval_visit_any(void)
 708{
 709    Visitor *v;
 710    QDict *qdict;
 711    QObject *any;
 712    QList *qlist;
 713    QString *qstr;
 714
 715    qdict = keyval_parse("a.0=null,a.1=1", NULL, NULL, &error_abort);
 716    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
 717    qobject_unref(qdict);
 718    visit_start_struct(v, NULL, NULL, 0, &error_abort);
 719    visit_type_any(v, "a", &any, &error_abort);
 720    qlist = qobject_to(QList, any);
 721    g_assert(qlist);
 722    qstr = qobject_to(QString, qlist_pop(qlist));
 723    g_assert_cmpstr(qstring_get_str(qstr), ==, "null");
 724    qobject_unref(qstr);
 725    qstr = qobject_to(QString, qlist_pop(qlist));
 726    g_assert_cmpstr(qstring_get_str(qstr), ==, "1");
 727    g_assert(qlist_empty(qlist));
 728    qobject_unref(qstr);
 729    qobject_unref(any);
 730    visit_check_struct(v, &error_abort);
 731    visit_end_struct(v, NULL);
 732    visit_free(v);
 733}
 734
 735int main(int argc, char *argv[])
 736{
 737    g_test_init(&argc, &argv, NULL);
 738    g_test_add_func("/keyval/keyval_parse", test_keyval_parse);
 739    g_test_add_func("/keyval/keyval_parse/list", test_keyval_parse_list);
 740    g_test_add_func("/keyval/visit/bool", test_keyval_visit_bool);
 741    g_test_add_func("/keyval/visit/number", test_keyval_visit_number);
 742    g_test_add_func("/keyval/visit/size", test_keyval_visit_size);
 743    g_test_add_func("/keyval/visit/dict", test_keyval_visit_dict);
 744    g_test_add_func("/keyval/visit/list", test_keyval_visit_list);
 745    g_test_add_func("/keyval/visit/optional", test_keyval_visit_optional);
 746    g_test_add_func("/keyval/visit/alternate", test_keyval_visit_alternate);
 747    g_test_add_func("/keyval/visit/any", test_keyval_visit_any);
 748    g_test_run();
 749    return 0;
 750}
 751