1da668aa1SThomas Huth /* 2da668aa1SThomas Huth * QObject Input Visitor unit-tests. 3da668aa1SThomas Huth * 4da668aa1SThomas Huth * Copyright (C) 2011-2016 Red Hat Inc. 5da668aa1SThomas Huth * 6da668aa1SThomas Huth * Authors: 7da668aa1SThomas Huth * Luiz Capitulino <lcapitulino@redhat.com> 8da668aa1SThomas Huth * Paolo Bonzini <pbonzini@redhat.com> 9da668aa1SThomas Huth * 10da668aa1SThomas Huth * This work is licensed under the terms of the GNU GPL, version 2 or later. 11da668aa1SThomas Huth * See the COPYING file in the top-level directory. 12da668aa1SThomas Huth */ 13da668aa1SThomas Huth 14da668aa1SThomas Huth #include "qemu/osdep.h" 15da668aa1SThomas Huth 16da668aa1SThomas Huth #include "qapi/error.h" 17da668aa1SThomas Huth #include "qapi/qapi-visit-introspect.h" 18da668aa1SThomas Huth #include "qapi/qobject-input-visitor.h" 19da668aa1SThomas Huth #include "test-qapi-visit.h" 20da668aa1SThomas Huth #include "qapi/qmp/qbool.h" 21da668aa1SThomas Huth #include "qapi/qmp/qdict.h" 22da668aa1SThomas Huth #include "qapi/qmp/qnull.h" 23da668aa1SThomas Huth #include "qapi/qmp/qnum.h" 24da668aa1SThomas Huth #include "qapi/qmp/qstring.h" 25da668aa1SThomas Huth #include "qapi/qmp/qjson.h" 26da668aa1SThomas Huth #include "test-qapi-introspect.h" 27da668aa1SThomas Huth #include "qapi/qapi-introspect.h" 28da668aa1SThomas Huth 29da668aa1SThomas Huth typedef struct TestInputVisitorData { 30da668aa1SThomas Huth QObject *obj; 31da668aa1SThomas Huth Visitor *qiv; 32da668aa1SThomas Huth } TestInputVisitorData; 33da668aa1SThomas Huth 34da668aa1SThomas Huth static void visitor_input_teardown(TestInputVisitorData *data, 35da668aa1SThomas Huth const void *unused) 36da668aa1SThomas Huth { 37da668aa1SThomas Huth qobject_unref(data->obj); 38da668aa1SThomas Huth data->obj = NULL; 39da668aa1SThomas Huth 40da668aa1SThomas Huth if (data->qiv) { 41da668aa1SThomas Huth visit_free(data->qiv); 42da668aa1SThomas Huth data->qiv = NULL; 43da668aa1SThomas Huth } 44da668aa1SThomas Huth } 45da668aa1SThomas Huth 46da668aa1SThomas Huth /* The various test_init functions are provided instead of a test setup 47da668aa1SThomas Huth function so that the JSON string used by the tests are kept in the test 48da668aa1SThomas Huth functions (and not in main()). */ 49da668aa1SThomas Huth 50da668aa1SThomas Huth static Visitor *test_init_internal(TestInputVisitorData *data, bool keyval, 51da668aa1SThomas Huth QObject *obj) 52da668aa1SThomas Huth { 53da668aa1SThomas Huth visitor_input_teardown(data, NULL); 54da668aa1SThomas Huth 55da668aa1SThomas Huth data->obj = obj; 56da668aa1SThomas Huth 57da668aa1SThomas Huth if (keyval) { 58da668aa1SThomas Huth data->qiv = qobject_input_visitor_new_keyval(data->obj); 59da668aa1SThomas Huth } else { 60da668aa1SThomas Huth data->qiv = qobject_input_visitor_new(data->obj); 61da668aa1SThomas Huth } 62da668aa1SThomas Huth g_assert(data->qiv); 63da668aa1SThomas Huth return data->qiv; 64da668aa1SThomas Huth } 65da668aa1SThomas Huth 669edc6313SMarc-André Lureau static G_GNUC_PRINTF(3, 4) 67da668aa1SThomas Huth Visitor *visitor_input_test_init_full(TestInputVisitorData *data, 68da668aa1SThomas Huth bool keyval, 69da668aa1SThomas Huth const char *json_string, ...) 70da668aa1SThomas Huth { 71da668aa1SThomas Huth Visitor *v; 72da668aa1SThomas Huth va_list ap; 73da668aa1SThomas Huth 74da668aa1SThomas Huth va_start(ap, json_string); 75da668aa1SThomas Huth v = test_init_internal(data, keyval, 76da668aa1SThomas Huth qobject_from_vjsonf_nofail(json_string, ap)); 77da668aa1SThomas Huth va_end(ap); 78da668aa1SThomas Huth return v; 79da668aa1SThomas Huth } 80da668aa1SThomas Huth 819edc6313SMarc-André Lureau static G_GNUC_PRINTF(2, 3) 82da668aa1SThomas Huth Visitor *visitor_input_test_init(TestInputVisitorData *data, 83da668aa1SThomas Huth const char *json_string, ...) 84da668aa1SThomas Huth { 85da668aa1SThomas Huth Visitor *v; 86da668aa1SThomas Huth va_list ap; 87da668aa1SThomas Huth 88da668aa1SThomas Huth va_start(ap, json_string); 89da668aa1SThomas Huth v = test_init_internal(data, false, 90da668aa1SThomas Huth qobject_from_vjsonf_nofail(json_string, ap)); 91da668aa1SThomas Huth va_end(ap); 92da668aa1SThomas Huth return v; 93da668aa1SThomas Huth } 94da668aa1SThomas Huth 95da668aa1SThomas Huth /* similar to visitor_input_test_init(), but does not expect a string 96da668aa1SThomas Huth * literal/format json_string argument and so can be used for 97da668aa1SThomas Huth * programatically generated strings (and we can't pass in programatically 98da668aa1SThomas Huth * generated strings via %s format parameters since qobject_from_jsonv() 99da668aa1SThomas Huth * will wrap those in double-quotes and treat the entire object as a 100da668aa1SThomas Huth * string) 101da668aa1SThomas Huth */ 102da668aa1SThomas Huth static Visitor *visitor_input_test_init_raw(TestInputVisitorData *data, 103da668aa1SThomas Huth const char *json_string) 104da668aa1SThomas Huth { 105da668aa1SThomas Huth return test_init_internal(data, false, 106da668aa1SThomas Huth qobject_from_json(json_string, &error_abort)); 107da668aa1SThomas Huth } 108da668aa1SThomas Huth 109da668aa1SThomas Huth static void test_visitor_in_int(TestInputVisitorData *data, 110da668aa1SThomas Huth const void *unused) 111da668aa1SThomas Huth { 112da668aa1SThomas Huth int64_t res = 0; 113da668aa1SThomas Huth double dbl; 114da668aa1SThomas Huth int value = -42; 115da668aa1SThomas Huth Visitor *v; 116da668aa1SThomas Huth 117da668aa1SThomas Huth v = visitor_input_test_init(data, "%d", value); 118da668aa1SThomas Huth 119da668aa1SThomas Huth visit_type_int(v, NULL, &res, &error_abort); 120da668aa1SThomas Huth g_assert_cmpint(res, ==, value); 121da668aa1SThomas Huth 122da668aa1SThomas Huth visit_type_number(v, NULL, &dbl, &error_abort); 123da668aa1SThomas Huth g_assert_cmpfloat(dbl, ==, -42.0); 124da668aa1SThomas Huth } 125da668aa1SThomas Huth 126da668aa1SThomas Huth static void test_visitor_in_uint(TestInputVisitorData *data, 127da668aa1SThomas Huth const void *unused) 128da668aa1SThomas Huth { 129da668aa1SThomas Huth uint64_t res = 0; 130da668aa1SThomas Huth int64_t i64; 131da668aa1SThomas Huth double dbl; 132da668aa1SThomas Huth int value = 42; 133da668aa1SThomas Huth Visitor *v; 134da668aa1SThomas Huth 135da668aa1SThomas Huth v = visitor_input_test_init(data, "%d", value); 136da668aa1SThomas Huth 137da668aa1SThomas Huth visit_type_uint64(v, NULL, &res, &error_abort); 138da668aa1SThomas Huth g_assert_cmpuint(res, ==, (uint64_t)value); 139da668aa1SThomas Huth 140da668aa1SThomas Huth visit_type_int(v, NULL, &i64, &error_abort); 141da668aa1SThomas Huth g_assert_cmpint(i64, ==, value); 142da668aa1SThomas Huth 143da668aa1SThomas Huth visit_type_number(v, NULL, &dbl, &error_abort); 144da668aa1SThomas Huth g_assert_cmpfloat(dbl, ==, value); 145da668aa1SThomas Huth 146da668aa1SThomas Huth /* BUG: value between INT64_MIN and -1 accepted modulo 2^64 */ 147da668aa1SThomas Huth v = visitor_input_test_init(data, "%d", -value); 148da668aa1SThomas Huth 149da668aa1SThomas Huth visit_type_uint64(v, NULL, &res, &error_abort); 150da668aa1SThomas Huth g_assert_cmpuint(res, ==, (uint64_t)-value); 151da668aa1SThomas Huth 152da668aa1SThomas Huth v = visitor_input_test_init(data, "18446744073709551574"); 153da668aa1SThomas Huth 154da668aa1SThomas Huth visit_type_uint64(v, NULL, &res, &error_abort); 155da668aa1SThomas Huth g_assert_cmpuint(res, ==, 18446744073709551574U); 156da668aa1SThomas Huth 157da668aa1SThomas Huth visit_type_number(v, NULL, &dbl, &error_abort); 158da668aa1SThomas Huth g_assert_cmpfloat(dbl, ==, 18446744073709552000.0); 159da668aa1SThomas Huth } 160da668aa1SThomas Huth 161da668aa1SThomas Huth static void test_visitor_in_int_overflow(TestInputVisitorData *data, 162da668aa1SThomas Huth const void *unused) 163da668aa1SThomas Huth { 164da668aa1SThomas Huth int64_t res = 0; 165da668aa1SThomas Huth Error *err = NULL; 166da668aa1SThomas Huth Visitor *v; 167da668aa1SThomas Huth 168da668aa1SThomas Huth /* 169da668aa1SThomas Huth * This will overflow a QNUM_I64, so should be deserialized into a 170da668aa1SThomas Huth * QNUM_DOUBLE field instead, leading to an error if we pass it to 171da668aa1SThomas Huth * visit_type_int(). Confirm this. 172da668aa1SThomas Huth */ 173da668aa1SThomas Huth v = visitor_input_test_init(data, "%f", DBL_MAX); 174da668aa1SThomas Huth 175da668aa1SThomas Huth visit_type_int(v, NULL, &res, &err); 176da668aa1SThomas Huth error_free_or_abort(&err); 177da668aa1SThomas Huth } 178da668aa1SThomas Huth 179da668aa1SThomas Huth static void test_visitor_in_int_keyval(TestInputVisitorData *data, 180da668aa1SThomas Huth const void *unused) 181da668aa1SThomas Huth { 182da668aa1SThomas Huth int64_t res = 0, value = -42; 183da668aa1SThomas Huth Error *err = NULL; 184da668aa1SThomas Huth Visitor *v; 185da668aa1SThomas Huth 186da668aa1SThomas Huth v = visitor_input_test_init_full(data, true, "%" PRId64, value); 187da668aa1SThomas Huth visit_type_int(v, NULL, &res, &err); 188da668aa1SThomas Huth error_free_or_abort(&err); 189da668aa1SThomas Huth } 190da668aa1SThomas Huth 191da668aa1SThomas Huth static void test_visitor_in_int_str_keyval(TestInputVisitorData *data, 192da668aa1SThomas Huth const void *unused) 193da668aa1SThomas Huth { 194da668aa1SThomas Huth int64_t res = 0, value = -42; 195da668aa1SThomas Huth Visitor *v; 196da668aa1SThomas Huth 197da668aa1SThomas Huth v = visitor_input_test_init_full(data, true, "\"-42\""); 198da668aa1SThomas Huth 199da668aa1SThomas Huth visit_type_int(v, NULL, &res, &error_abort); 200da668aa1SThomas Huth g_assert_cmpint(res, ==, value); 201da668aa1SThomas Huth } 202da668aa1SThomas Huth 203da668aa1SThomas Huth static void test_visitor_in_int_str_fail(TestInputVisitorData *data, 204da668aa1SThomas Huth const void *unused) 205da668aa1SThomas Huth { 206da668aa1SThomas Huth int64_t res = 0; 207da668aa1SThomas Huth Visitor *v; 208da668aa1SThomas Huth Error *err = NULL; 209da668aa1SThomas Huth 210da668aa1SThomas Huth v = visitor_input_test_init(data, "\"-42\""); 211da668aa1SThomas Huth 212da668aa1SThomas Huth visit_type_int(v, NULL, &res, &err); 213da668aa1SThomas Huth error_free_or_abort(&err); 214da668aa1SThomas Huth } 215da668aa1SThomas Huth 216da668aa1SThomas Huth static void test_visitor_in_bool(TestInputVisitorData *data, 217da668aa1SThomas Huth const void *unused) 218da668aa1SThomas Huth { 219da668aa1SThomas Huth bool res = false; 220da668aa1SThomas Huth Visitor *v; 221da668aa1SThomas Huth 222da668aa1SThomas Huth v = visitor_input_test_init(data, "true"); 223da668aa1SThomas Huth 224da668aa1SThomas Huth visit_type_bool(v, NULL, &res, &error_abort); 225da668aa1SThomas Huth g_assert_cmpint(res, ==, true); 226da668aa1SThomas Huth } 227da668aa1SThomas Huth 228da668aa1SThomas Huth static void test_visitor_in_bool_keyval(TestInputVisitorData *data, 229da668aa1SThomas Huth const void *unused) 230da668aa1SThomas Huth { 231da668aa1SThomas Huth bool res = false; 232da668aa1SThomas Huth Error *err = NULL; 233da668aa1SThomas Huth Visitor *v; 234da668aa1SThomas Huth 235da668aa1SThomas Huth v = visitor_input_test_init_full(data, true, "true"); 236da668aa1SThomas Huth 237da668aa1SThomas Huth visit_type_bool(v, NULL, &res, &err); 238da668aa1SThomas Huth error_free_or_abort(&err); 239da668aa1SThomas Huth } 240da668aa1SThomas Huth 241da668aa1SThomas Huth static void test_visitor_in_bool_str_keyval(TestInputVisitorData *data, 242da668aa1SThomas Huth const void *unused) 243da668aa1SThomas Huth { 244da668aa1SThomas Huth bool res = false; 245da668aa1SThomas Huth Visitor *v; 246da668aa1SThomas Huth 247da668aa1SThomas Huth v = visitor_input_test_init_full(data, true, "\"on\""); 248da668aa1SThomas Huth 249da668aa1SThomas Huth visit_type_bool(v, NULL, &res, &error_abort); 250da668aa1SThomas Huth g_assert_cmpint(res, ==, true); 251da668aa1SThomas Huth } 252da668aa1SThomas Huth 253da668aa1SThomas Huth static void test_visitor_in_bool_str_fail(TestInputVisitorData *data, 254da668aa1SThomas Huth const void *unused) 255da668aa1SThomas Huth { 256da668aa1SThomas Huth bool res = false; 257da668aa1SThomas Huth Visitor *v; 258da668aa1SThomas Huth Error *err = NULL; 259da668aa1SThomas Huth 260da668aa1SThomas Huth v = visitor_input_test_init(data, "\"true\""); 261da668aa1SThomas Huth 262da668aa1SThomas Huth visit_type_bool(v, NULL, &res, &err); 263da668aa1SThomas Huth error_free_or_abort(&err); 264da668aa1SThomas Huth } 265da668aa1SThomas Huth 266da668aa1SThomas Huth static void test_visitor_in_number(TestInputVisitorData *data, 267da668aa1SThomas Huth const void *unused) 268da668aa1SThomas Huth { 269da668aa1SThomas Huth double res = 0, value = 3.14; 270da668aa1SThomas Huth Visitor *v; 271da668aa1SThomas Huth 272da668aa1SThomas Huth v = visitor_input_test_init(data, "%f", value); 273da668aa1SThomas Huth 274da668aa1SThomas Huth visit_type_number(v, NULL, &res, &error_abort); 275da668aa1SThomas Huth g_assert_cmpfloat(res, ==, value); 276da668aa1SThomas Huth } 277da668aa1SThomas Huth 278da668aa1SThomas Huth static void test_visitor_in_large_number(TestInputVisitorData *data, 279da668aa1SThomas Huth const void *unused) 280da668aa1SThomas Huth { 281da668aa1SThomas Huth Error *err = NULL; 282da668aa1SThomas Huth double res = 0; 283da668aa1SThomas Huth int64_t i64; 284da668aa1SThomas Huth uint64_t u64; 285da668aa1SThomas Huth Visitor *v; 286da668aa1SThomas Huth 287da668aa1SThomas Huth v = visitor_input_test_init(data, "-18446744073709551616"); /* -2^64 */ 288da668aa1SThomas Huth 289da668aa1SThomas Huth visit_type_number(v, NULL, &res, &error_abort); 290da668aa1SThomas Huth g_assert_cmpfloat(res, ==, -18446744073709552e3); 291da668aa1SThomas Huth 292da668aa1SThomas Huth visit_type_int(v, NULL, &i64, &err); 293da668aa1SThomas Huth error_free_or_abort(&err); 294da668aa1SThomas Huth 295da668aa1SThomas Huth visit_type_uint64(v, NULL, &u64, &err); 296da668aa1SThomas Huth error_free_or_abort(&err); 297da668aa1SThomas Huth } 298da668aa1SThomas Huth 299da668aa1SThomas Huth static void test_visitor_in_number_keyval(TestInputVisitorData *data, 300da668aa1SThomas Huth const void *unused) 301da668aa1SThomas Huth { 302da668aa1SThomas Huth double res = 0, value = 3.14; 303da668aa1SThomas Huth Error *err = NULL; 304da668aa1SThomas Huth Visitor *v; 305da668aa1SThomas Huth 306da668aa1SThomas Huth v = visitor_input_test_init_full(data, true, "%f", value); 307da668aa1SThomas Huth 308da668aa1SThomas Huth visit_type_number(v, NULL, &res, &err); 309da668aa1SThomas Huth error_free_or_abort(&err); 310da668aa1SThomas Huth } 311da668aa1SThomas Huth 312da668aa1SThomas Huth static void test_visitor_in_number_str_keyval(TestInputVisitorData *data, 313da668aa1SThomas Huth const void *unused) 314da668aa1SThomas Huth { 315da668aa1SThomas Huth double res = 0, value = 3.14; 316da668aa1SThomas Huth Visitor *v; 317da668aa1SThomas Huth Error *err = NULL; 318da668aa1SThomas Huth 319da668aa1SThomas Huth v = visitor_input_test_init_full(data, true, "\"3.14\""); 320da668aa1SThomas Huth 321da668aa1SThomas Huth visit_type_number(v, NULL, &res, &error_abort); 322da668aa1SThomas Huth g_assert_cmpfloat(res, ==, value); 323da668aa1SThomas Huth 324da668aa1SThomas Huth v = visitor_input_test_init_full(data, true, "\"inf\""); 325da668aa1SThomas Huth 326da668aa1SThomas Huth visit_type_number(v, NULL, &res, &err); 327da668aa1SThomas Huth error_free_or_abort(&err); 328da668aa1SThomas Huth } 329da668aa1SThomas Huth 330da668aa1SThomas Huth static void test_visitor_in_number_str_fail(TestInputVisitorData *data, 331da668aa1SThomas Huth const void *unused) 332da668aa1SThomas Huth { 333da668aa1SThomas Huth double res = 0; 334da668aa1SThomas Huth Visitor *v; 335da668aa1SThomas Huth Error *err = NULL; 336da668aa1SThomas Huth 337da668aa1SThomas Huth v = visitor_input_test_init(data, "\"3.14\""); 338da668aa1SThomas Huth 339da668aa1SThomas Huth visit_type_number(v, NULL, &res, &err); 340da668aa1SThomas Huth error_free_or_abort(&err); 341da668aa1SThomas Huth } 342da668aa1SThomas Huth 343da668aa1SThomas Huth static void test_visitor_in_size_str_keyval(TestInputVisitorData *data, 344da668aa1SThomas Huth const void *unused) 345da668aa1SThomas Huth { 346da668aa1SThomas Huth uint64_t res, value = 500 * 1024 * 1024; 347da668aa1SThomas Huth Visitor *v; 348da668aa1SThomas Huth 349da668aa1SThomas Huth v = visitor_input_test_init_full(data, true, "\"500M\""); 350da668aa1SThomas Huth 351da668aa1SThomas Huth visit_type_size(v, NULL, &res, &error_abort); 352da668aa1SThomas Huth g_assert_cmpfloat(res, ==, value); 353da668aa1SThomas Huth } 354da668aa1SThomas Huth 355da668aa1SThomas Huth static void test_visitor_in_size_str_fail(TestInputVisitorData *data, 356da668aa1SThomas Huth const void *unused) 357da668aa1SThomas Huth { 358da668aa1SThomas Huth uint64_t res = 0; 359da668aa1SThomas Huth Visitor *v; 360da668aa1SThomas Huth Error *err = NULL; 361da668aa1SThomas Huth 362da668aa1SThomas Huth v = visitor_input_test_init(data, "\"500M\""); 363da668aa1SThomas Huth 364da668aa1SThomas Huth visit_type_size(v, NULL, &res, &err); 365da668aa1SThomas Huth error_free_or_abort(&err); 366da668aa1SThomas Huth } 367da668aa1SThomas Huth 368da668aa1SThomas Huth static void test_visitor_in_string(TestInputVisitorData *data, 369da668aa1SThomas Huth const void *unused) 370da668aa1SThomas Huth { 371da668aa1SThomas Huth char *res = NULL, *value = (char *) "Q E M U"; 372da668aa1SThomas Huth Visitor *v; 373da668aa1SThomas Huth 374da668aa1SThomas Huth v = visitor_input_test_init(data, "%s", value); 375da668aa1SThomas Huth 376da668aa1SThomas Huth visit_type_str(v, NULL, &res, &error_abort); 377da668aa1SThomas Huth g_assert_cmpstr(res, ==, value); 378da668aa1SThomas Huth 379da668aa1SThomas Huth g_free(res); 380da668aa1SThomas Huth } 381da668aa1SThomas Huth 382da668aa1SThomas Huth static void test_visitor_in_enum(TestInputVisitorData *data, 383da668aa1SThomas Huth const void *unused) 384da668aa1SThomas Huth { 385da668aa1SThomas Huth Visitor *v; 386da668aa1SThomas Huth EnumOne i; 387da668aa1SThomas Huth 388da668aa1SThomas Huth for (i = 0; i < ENUM_ONE__MAX; i++) { 389da668aa1SThomas Huth EnumOne res = -1; 390da668aa1SThomas Huth 391da668aa1SThomas Huth v = visitor_input_test_init(data, "%s", EnumOne_str(i)); 392da668aa1SThomas Huth 393da668aa1SThomas Huth visit_type_EnumOne(v, NULL, &res, &error_abort); 394da668aa1SThomas Huth g_assert_cmpint(i, ==, res); 395da668aa1SThomas Huth } 396da668aa1SThomas Huth } 397da668aa1SThomas Huth 398da668aa1SThomas Huth 399da668aa1SThomas Huth static void test_visitor_in_struct(TestInputVisitorData *data, 400da668aa1SThomas Huth const void *unused) 401da668aa1SThomas Huth { 402da668aa1SThomas Huth TestStruct *p = NULL; 403da668aa1SThomas Huth Visitor *v; 404da668aa1SThomas Huth 405da668aa1SThomas Huth v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }"); 406da668aa1SThomas Huth 407da668aa1SThomas Huth visit_type_TestStruct(v, NULL, &p, &error_abort); 408da668aa1SThomas Huth g_assert_cmpint(p->integer, ==, -42); 409da668aa1SThomas Huth g_assert(p->boolean == true); 410da668aa1SThomas Huth g_assert_cmpstr(p->string, ==, "foo"); 411da668aa1SThomas Huth 412da668aa1SThomas Huth g_free(p->string); 413da668aa1SThomas Huth g_free(p); 414da668aa1SThomas Huth } 415da668aa1SThomas Huth 416da668aa1SThomas Huth static void test_visitor_in_struct_nested(TestInputVisitorData *data, 417da668aa1SThomas Huth const void *unused) 418da668aa1SThomas Huth { 419da668aa1SThomas Huth g_autoptr(UserDefTwo) udp = NULL; 420da668aa1SThomas Huth Visitor *v; 421da668aa1SThomas Huth 422da668aa1SThomas Huth v = visitor_input_test_init(data, "{ 'string0': 'string0', " 423da668aa1SThomas Huth "'dict1': { 'string1': 'string1', " 424da668aa1SThomas Huth "'dict2': { 'userdef': { 'integer': 42, " 425da668aa1SThomas Huth "'string': 'string' }, 'string': 'string2'}}}"); 426da668aa1SThomas Huth 427da668aa1SThomas Huth visit_type_UserDefTwo(v, NULL, &udp, &error_abort); 428da668aa1SThomas Huth 429da668aa1SThomas Huth g_assert_cmpstr(udp->string0, ==, "string0"); 430da668aa1SThomas Huth g_assert_cmpstr(udp->dict1->string1, ==, "string1"); 431da668aa1SThomas Huth g_assert_cmpint(udp->dict1->dict2->userdef->integer, ==, 42); 432da668aa1SThomas Huth g_assert_cmpstr(udp->dict1->dict2->userdef->string, ==, "string"); 433da668aa1SThomas Huth g_assert_cmpstr(udp->dict1->dict2->string, ==, "string2"); 434da668aa1SThomas Huth g_assert(udp->dict1->has_dict3 == false); 435da668aa1SThomas Huth } 436da668aa1SThomas Huth 437da668aa1SThomas Huth static void test_visitor_in_list(TestInputVisitorData *data, 438da668aa1SThomas Huth const void *unused) 439da668aa1SThomas Huth { 440da668aa1SThomas Huth UserDefOneList *item, *head = NULL; 441da668aa1SThomas Huth Visitor *v; 442da668aa1SThomas Huth int i; 443da668aa1SThomas Huth 444da668aa1SThomas Huth v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]"); 445da668aa1SThomas Huth 446da668aa1SThomas Huth visit_type_UserDefOneList(v, NULL, &head, &error_abort); 447da668aa1SThomas Huth g_assert(head != NULL); 448da668aa1SThomas Huth 449da668aa1SThomas Huth for (i = 0, item = head; item; item = item->next, i++) { 450da668aa1SThomas Huth char string[12]; 451da668aa1SThomas Huth 452da668aa1SThomas Huth snprintf(string, sizeof(string), "string%d", i); 453da668aa1SThomas Huth g_assert_cmpstr(item->value->string, ==, string); 454da668aa1SThomas Huth g_assert_cmpint(item->value->integer, ==, 42 + i); 455da668aa1SThomas Huth } 456da668aa1SThomas Huth 457da668aa1SThomas Huth qapi_free_UserDefOneList(head); 458da668aa1SThomas Huth head = NULL; 459da668aa1SThomas Huth 460da668aa1SThomas Huth /* An empty list is valid */ 461da668aa1SThomas Huth v = visitor_input_test_init(data, "[]"); 462da668aa1SThomas Huth visit_type_UserDefOneList(v, NULL, &head, &error_abort); 463da668aa1SThomas Huth g_assert(!head); 464da668aa1SThomas Huth } 465da668aa1SThomas Huth 466e7a76fe2SMarkus Armbruster static void test_visitor_in_list_struct(TestInputVisitorData *data, 467e7a76fe2SMarkus Armbruster const void *unused) 468e7a76fe2SMarkus Armbruster { 469e7a76fe2SMarkus Armbruster const char *int_member[] = { 470e7a76fe2SMarkus Armbruster "integer", "s8", "s16", "s32", "s64", "u8", "u16", "u32", "u64" }; 471e7a76fe2SMarkus Armbruster g_autoptr(GString) json = g_string_new(""); 472e7a76fe2SMarkus Armbruster int i, j; 473e7a76fe2SMarkus Armbruster const char *sep; 474e7a76fe2SMarkus Armbruster g_autoptr(ArrayStruct) arrs = NULL; 475e7a76fe2SMarkus Armbruster Visitor *v; 476e7a76fe2SMarkus Armbruster intList *int_list; 477e7a76fe2SMarkus Armbruster int8List *s8_list; 478e7a76fe2SMarkus Armbruster int16List *s16_list; 479e7a76fe2SMarkus Armbruster int32List *s32_list; 480e7a76fe2SMarkus Armbruster int64List *s64_list; 481e7a76fe2SMarkus Armbruster uint8List *u8_list; 482e7a76fe2SMarkus Armbruster uint16List *u16_list; 483e7a76fe2SMarkus Armbruster uint32List *u32_list; 484e7a76fe2SMarkus Armbruster uint64List *u64_list; 485e7a76fe2SMarkus Armbruster numberList *num_list; 486e7a76fe2SMarkus Armbruster boolList *bool_list; 487e7a76fe2SMarkus Armbruster strList *str_list; 488e7a76fe2SMarkus Armbruster 489e7a76fe2SMarkus Armbruster g_string_append_printf(json, "{"); 490e7a76fe2SMarkus Armbruster 491e7a76fe2SMarkus Armbruster for (i = 0; i < G_N_ELEMENTS(int_member); i++) { 492e7a76fe2SMarkus Armbruster g_string_append_printf(json, "'%s': [", int_member[i]); 493e7a76fe2SMarkus Armbruster sep = ""; 494e7a76fe2SMarkus Armbruster for (j = 0; j < 32; j++) { 495e7a76fe2SMarkus Armbruster g_string_append_printf(json, "%s%d", sep, j); 496e7a76fe2SMarkus Armbruster sep = ", "; 497e7a76fe2SMarkus Armbruster } 498e7a76fe2SMarkus Armbruster g_string_append_printf(json, "], "); 499e7a76fe2SMarkus Armbruster } 500e7a76fe2SMarkus Armbruster 501e7a76fe2SMarkus Armbruster g_string_append_printf(json, "'number': ["); 502e7a76fe2SMarkus Armbruster sep = ""; 503e7a76fe2SMarkus Armbruster for (i = 0; i < 32; i++) { 504e7a76fe2SMarkus Armbruster g_string_append_printf(json, "%s%f", sep, (double)i / 3); 505e7a76fe2SMarkus Armbruster sep = ", "; 506e7a76fe2SMarkus Armbruster } 507e7a76fe2SMarkus Armbruster g_string_append_printf(json, "], "); 508e7a76fe2SMarkus Armbruster 509e7a76fe2SMarkus Armbruster g_string_append_printf(json, "'boolean': ["); 510e7a76fe2SMarkus Armbruster sep = ""; 511e7a76fe2SMarkus Armbruster for (i = 0; i < 32; i++) { 512e7a76fe2SMarkus Armbruster g_string_append_printf(json, "%s%s", 513e7a76fe2SMarkus Armbruster sep, i % 3 == 0 ? "true" : "false"); 514e7a76fe2SMarkus Armbruster sep = ", "; 515e7a76fe2SMarkus Armbruster } 516e7a76fe2SMarkus Armbruster g_string_append_printf(json, "], "); 517e7a76fe2SMarkus Armbruster 518e7a76fe2SMarkus Armbruster g_string_append_printf(json, "'string': ["); 519e7a76fe2SMarkus Armbruster sep = ""; 520e7a76fe2SMarkus Armbruster for (i = 0; i < 32; i++) { 521e7a76fe2SMarkus Armbruster g_string_append_printf(json, "%s'%d'", sep, i); 522e7a76fe2SMarkus Armbruster sep = ", "; 523e7a76fe2SMarkus Armbruster } 524e7a76fe2SMarkus Armbruster g_string_append_printf(json, "]"); 525e7a76fe2SMarkus Armbruster 526e7a76fe2SMarkus Armbruster g_string_append_printf(json, "}"); 527e7a76fe2SMarkus Armbruster 528e7a76fe2SMarkus Armbruster v = visitor_input_test_init_raw(data, json->str); 529e7a76fe2SMarkus Armbruster visit_type_ArrayStruct(v, NULL, &arrs, &error_abort); 530e7a76fe2SMarkus Armbruster 531e7a76fe2SMarkus Armbruster i = 0; 532e7a76fe2SMarkus Armbruster for (int_list = arrs->integer; int_list; int_list = int_list->next) { 533e7a76fe2SMarkus Armbruster g_assert_cmpint(int_list->value, ==, i); 534e7a76fe2SMarkus Armbruster i++; 535e7a76fe2SMarkus Armbruster } 536e7a76fe2SMarkus Armbruster 537e7a76fe2SMarkus Armbruster i = 0; 538e7a76fe2SMarkus Armbruster for (s8_list = arrs->s8; s8_list; s8_list = s8_list->next) { 539e7a76fe2SMarkus Armbruster g_assert_cmpint(s8_list->value, ==, i); 540e7a76fe2SMarkus Armbruster i++; 541e7a76fe2SMarkus Armbruster } 542e7a76fe2SMarkus Armbruster 543e7a76fe2SMarkus Armbruster i = 0; 544e7a76fe2SMarkus Armbruster for (s16_list = arrs->s16; s16_list; s16_list = s16_list->next) { 545e7a76fe2SMarkus Armbruster g_assert_cmpint(s16_list->value, ==, i); 546e7a76fe2SMarkus Armbruster i++; 547e7a76fe2SMarkus Armbruster } 548e7a76fe2SMarkus Armbruster 549e7a76fe2SMarkus Armbruster i = 0; 550e7a76fe2SMarkus Armbruster for (s32_list = arrs->s32; s32_list; s32_list = s32_list->next) { 551e7a76fe2SMarkus Armbruster g_assert_cmpint(s32_list->value, ==, i); 552e7a76fe2SMarkus Armbruster i++; 553e7a76fe2SMarkus Armbruster } 554e7a76fe2SMarkus Armbruster 555e7a76fe2SMarkus Armbruster i = 0; 556e7a76fe2SMarkus Armbruster for (s64_list = arrs->s64; s64_list; s64_list = s64_list->next) { 557e7a76fe2SMarkus Armbruster g_assert_cmpint(s64_list->value, ==, i); 558e7a76fe2SMarkus Armbruster i++; 559e7a76fe2SMarkus Armbruster } 560e7a76fe2SMarkus Armbruster 561e7a76fe2SMarkus Armbruster i = 0; 562e7a76fe2SMarkus Armbruster for (u8_list = arrs->u8; u8_list; u8_list = u8_list->next) { 563e7a76fe2SMarkus Armbruster g_assert_cmpint(u8_list->value, ==, i); 564e7a76fe2SMarkus Armbruster i++; 565e7a76fe2SMarkus Armbruster } 566e7a76fe2SMarkus Armbruster 567e7a76fe2SMarkus Armbruster i = 0; 568e7a76fe2SMarkus Armbruster for (u16_list = arrs->u16; u16_list; u16_list = u16_list->next) { 569e7a76fe2SMarkus Armbruster g_assert_cmpint(u16_list->value, ==, i); 570e7a76fe2SMarkus Armbruster i++; 571e7a76fe2SMarkus Armbruster } 572e7a76fe2SMarkus Armbruster 573e7a76fe2SMarkus Armbruster i = 0; 574e7a76fe2SMarkus Armbruster for (u32_list = arrs->u32; u32_list; u32_list = u32_list->next) { 575e7a76fe2SMarkus Armbruster g_assert_cmpint(u32_list->value, ==, i); 576e7a76fe2SMarkus Armbruster i++; 577e7a76fe2SMarkus Armbruster } 578e7a76fe2SMarkus Armbruster 579e7a76fe2SMarkus Armbruster i = 0; 580e7a76fe2SMarkus Armbruster for (u64_list = arrs->u64; u64_list; u64_list = u64_list->next) { 581e7a76fe2SMarkus Armbruster g_assert_cmpint(u64_list->value, ==, i); 582e7a76fe2SMarkus Armbruster i++; 583e7a76fe2SMarkus Armbruster } 584e7a76fe2SMarkus Armbruster 585e7a76fe2SMarkus Armbruster i = 0; 586e7a76fe2SMarkus Armbruster for (num_list = arrs->number; num_list; num_list = num_list->next) { 587e7a76fe2SMarkus Armbruster char expected[32], actual[32]; 588e7a76fe2SMarkus Armbruster 589e7a76fe2SMarkus Armbruster sprintf(expected, "%.6f", (double)i / 3); 590e7a76fe2SMarkus Armbruster sprintf(actual, "%.6f", num_list->value); 591e7a76fe2SMarkus Armbruster g_assert_cmpstr(expected, ==, actual); 592e7a76fe2SMarkus Armbruster i++; 593e7a76fe2SMarkus Armbruster } 594e7a76fe2SMarkus Armbruster 595e7a76fe2SMarkus Armbruster i = 0; 596e7a76fe2SMarkus Armbruster for (bool_list = arrs->boolean; bool_list; bool_list = bool_list->next) { 597e7a76fe2SMarkus Armbruster g_assert_cmpint(bool_list->value, ==, i % 3 == 0); 598e7a76fe2SMarkus Armbruster i++; 599e7a76fe2SMarkus Armbruster } 600e7a76fe2SMarkus Armbruster 601e7a76fe2SMarkus Armbruster i = 0; 602e7a76fe2SMarkus Armbruster for (str_list = arrs->string; str_list; str_list = str_list->next) { 603e7a76fe2SMarkus Armbruster char expected[32]; 604e7a76fe2SMarkus Armbruster 605e7a76fe2SMarkus Armbruster sprintf(expected, "%d", i); 606e7a76fe2SMarkus Armbruster g_assert_cmpstr(str_list->value, ==, expected); 607e7a76fe2SMarkus Armbruster i++; 608e7a76fe2SMarkus Armbruster } 609e7a76fe2SMarkus Armbruster } 610e7a76fe2SMarkus Armbruster 611da668aa1SThomas Huth static void test_visitor_in_any(TestInputVisitorData *data, 612da668aa1SThomas Huth const void *unused) 613da668aa1SThomas Huth { 614da668aa1SThomas Huth QObject *res = NULL; 615da668aa1SThomas Huth Visitor *v; 616da668aa1SThomas Huth QNum *qnum; 617da668aa1SThomas Huth QBool *qbool; 618da668aa1SThomas Huth QString *qstring; 619da668aa1SThomas Huth QDict *qdict; 620da668aa1SThomas Huth QObject *qobj; 621da668aa1SThomas Huth int64_t val; 622da668aa1SThomas Huth 623da668aa1SThomas Huth v = visitor_input_test_init(data, "-42"); 624da668aa1SThomas Huth visit_type_any(v, NULL, &res, &error_abort); 625da668aa1SThomas Huth qnum = qobject_to(QNum, res); 626da668aa1SThomas Huth g_assert(qnum); 627da668aa1SThomas Huth g_assert(qnum_get_try_int(qnum, &val)); 628da668aa1SThomas Huth g_assert_cmpint(val, ==, -42); 629da668aa1SThomas Huth qobject_unref(res); 630da668aa1SThomas Huth 631da668aa1SThomas Huth v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }"); 632da668aa1SThomas Huth visit_type_any(v, NULL, &res, &error_abort); 633da668aa1SThomas Huth qdict = qobject_to(QDict, res); 634da668aa1SThomas Huth g_assert(qdict && qdict_size(qdict) == 3); 635da668aa1SThomas Huth qobj = qdict_get(qdict, "integer"); 636da668aa1SThomas Huth g_assert(qobj); 637da668aa1SThomas Huth qnum = qobject_to(QNum, qobj); 638da668aa1SThomas Huth g_assert(qnum); 639da668aa1SThomas Huth g_assert(qnum_get_try_int(qnum, &val)); 640da668aa1SThomas Huth g_assert_cmpint(val, ==, -42); 641da668aa1SThomas Huth qobj = qdict_get(qdict, "boolean"); 642da668aa1SThomas Huth g_assert(qobj); 643da668aa1SThomas Huth qbool = qobject_to(QBool, qobj); 644da668aa1SThomas Huth g_assert(qbool); 645da668aa1SThomas Huth g_assert(qbool_get_bool(qbool) == true); 646da668aa1SThomas Huth qobj = qdict_get(qdict, "string"); 647da668aa1SThomas Huth g_assert(qobj); 648da668aa1SThomas Huth qstring = qobject_to(QString, qobj); 649da668aa1SThomas Huth g_assert(qstring); 650da668aa1SThomas Huth g_assert_cmpstr(qstring_get_str(qstring), ==, "foo"); 651da668aa1SThomas Huth qobject_unref(res); 652da668aa1SThomas Huth } 653da668aa1SThomas Huth 654da668aa1SThomas Huth static void test_visitor_in_null(TestInputVisitorData *data, 655da668aa1SThomas Huth const void *unused) 656da668aa1SThomas Huth { 657da668aa1SThomas Huth Visitor *v; 658da668aa1SThomas Huth Error *err = NULL; 659da668aa1SThomas Huth QNull *null; 660da668aa1SThomas Huth char *tmp; 661da668aa1SThomas Huth 662da668aa1SThomas Huth /* 663da668aa1SThomas Huth * FIXME: Since QAPI doesn't know the 'null' type yet, we can't 664da668aa1SThomas Huth * test visit_type_null() by reading into a QAPI struct then 665da668aa1SThomas Huth * checking that it was populated correctly. The best we can do 666da668aa1SThomas Huth * for now is ensure that we consumed null from the input, proven 667da668aa1SThomas Huth * by the fact that we can't re-read the key; and that we detect 668da668aa1SThomas Huth * when input is not null. 669da668aa1SThomas Huth */ 670da668aa1SThomas Huth 671da668aa1SThomas Huth v = visitor_input_test_init_full(data, false, 672da668aa1SThomas Huth "{ 'a': null, 'b': '' }"); 673da668aa1SThomas Huth visit_start_struct(v, NULL, NULL, 0, &error_abort); 674da668aa1SThomas Huth visit_type_null(v, "a", &null, &error_abort); 675da668aa1SThomas Huth g_assert(qobject_type(QOBJECT(null)) == QTYPE_QNULL); 676da668aa1SThomas Huth qobject_unref(null); 677da668aa1SThomas Huth visit_type_null(v, "b", &null, &err); 678da668aa1SThomas Huth error_free_or_abort(&err); 679da668aa1SThomas Huth g_assert(!null); 680da668aa1SThomas Huth visit_type_str(v, "c", &tmp, &err); 681da668aa1SThomas Huth error_free_or_abort(&err); 682da668aa1SThomas Huth g_assert(!tmp); 683da668aa1SThomas Huth visit_check_struct(v, &error_abort); 684da668aa1SThomas Huth visit_end_struct(v, NULL); 685da668aa1SThomas Huth } 686da668aa1SThomas Huth 687da668aa1SThomas Huth static void test_visitor_in_union_flat(TestInputVisitorData *data, 688da668aa1SThomas Huth const void *unused) 689da668aa1SThomas Huth { 690da668aa1SThomas Huth Visitor *v; 691da668aa1SThomas Huth g_autoptr(UserDefFlatUnion) tmp = NULL; 692da668aa1SThomas Huth UserDefUnionBase *base; 693da668aa1SThomas Huth 694da668aa1SThomas Huth v = visitor_input_test_init(data, 695da668aa1SThomas Huth "{ 'enum1': 'value1', " 696da668aa1SThomas Huth "'integer': 41, " 697da668aa1SThomas Huth "'string': 'str', " 698da668aa1SThomas Huth "'boolean': true }"); 699da668aa1SThomas Huth 700da668aa1SThomas Huth visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort); 701da668aa1SThomas Huth g_assert_cmpint(tmp->enum1, ==, ENUM_ONE_VALUE1); 702da668aa1SThomas Huth g_assert_cmpstr(tmp->string, ==, "str"); 703da668aa1SThomas Huth g_assert_cmpint(tmp->integer, ==, 41); 704da668aa1SThomas Huth g_assert_cmpint(tmp->u.value1.boolean, ==, true); 705da668aa1SThomas Huth 706da668aa1SThomas Huth base = qapi_UserDefFlatUnion_base(tmp); 707da668aa1SThomas Huth g_assert(&base->enum1 == &tmp->enum1); 708da668aa1SThomas Huth } 709da668aa1SThomas Huth 710da668aa1SThomas Huth static void test_visitor_in_alternate(TestInputVisitorData *data, 711da668aa1SThomas Huth const void *unused) 712da668aa1SThomas Huth { 713da668aa1SThomas Huth Visitor *v; 714da668aa1SThomas Huth UserDefAlternate *tmp; 715da668aa1SThomas Huth WrapAlternate *wrap; 716da668aa1SThomas Huth 717da668aa1SThomas Huth v = visitor_input_test_init(data, "42"); 718da668aa1SThomas Huth visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort); 719da668aa1SThomas Huth g_assert_cmpint(tmp->type, ==, QTYPE_QNUM); 720da668aa1SThomas Huth g_assert_cmpint(tmp->u.i, ==, 42); 721da668aa1SThomas Huth qapi_free_UserDefAlternate(tmp); 722da668aa1SThomas Huth 723da668aa1SThomas Huth v = visitor_input_test_init(data, "'value1'"); 724da668aa1SThomas Huth visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort); 725da668aa1SThomas Huth g_assert_cmpint(tmp->type, ==, QTYPE_QSTRING); 726da668aa1SThomas Huth g_assert_cmpint(tmp->u.e, ==, ENUM_ONE_VALUE1); 727da668aa1SThomas Huth qapi_free_UserDefAlternate(tmp); 728da668aa1SThomas Huth 729da668aa1SThomas Huth v = visitor_input_test_init(data, "null"); 730da668aa1SThomas Huth visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort); 731da668aa1SThomas Huth g_assert_cmpint(tmp->type, ==, QTYPE_QNULL); 732da668aa1SThomas Huth qapi_free_UserDefAlternate(tmp); 733da668aa1SThomas Huth 734da668aa1SThomas Huth v = visitor_input_test_init(data, "{'integer':1, 'string':'str', " 735da668aa1SThomas Huth "'enum1':'value1', 'boolean':true}"); 736da668aa1SThomas Huth visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort); 737da668aa1SThomas Huth g_assert_cmpint(tmp->type, ==, QTYPE_QDICT); 738da668aa1SThomas Huth g_assert_cmpint(tmp->u.udfu.integer, ==, 1); 739da668aa1SThomas Huth g_assert_cmpstr(tmp->u.udfu.string, ==, "str"); 740da668aa1SThomas Huth g_assert_cmpint(tmp->u.udfu.enum1, ==, ENUM_ONE_VALUE1); 741da668aa1SThomas Huth g_assert_cmpint(tmp->u.udfu.u.value1.boolean, ==, true); 742da668aa1SThomas Huth g_assert_cmpint(tmp->u.udfu.u.value1.has_a_b, ==, false); 743da668aa1SThomas Huth qapi_free_UserDefAlternate(tmp); 744da668aa1SThomas Huth 745da668aa1SThomas Huth v = visitor_input_test_init(data, "{ 'alt': 42 }"); 746da668aa1SThomas Huth visit_type_WrapAlternate(v, NULL, &wrap, &error_abort); 747da668aa1SThomas Huth g_assert_cmpint(wrap->alt->type, ==, QTYPE_QNUM); 748da668aa1SThomas Huth g_assert_cmpint(wrap->alt->u.i, ==, 42); 749da668aa1SThomas Huth qapi_free_WrapAlternate(wrap); 750da668aa1SThomas Huth 751da668aa1SThomas Huth v = visitor_input_test_init(data, "{ 'alt': 'value1' }"); 752da668aa1SThomas Huth visit_type_WrapAlternate(v, NULL, &wrap, &error_abort); 753da668aa1SThomas Huth g_assert_cmpint(wrap->alt->type, ==, QTYPE_QSTRING); 754da668aa1SThomas Huth g_assert_cmpint(wrap->alt->u.e, ==, ENUM_ONE_VALUE1); 755da668aa1SThomas Huth qapi_free_WrapAlternate(wrap); 756da668aa1SThomas Huth 757da668aa1SThomas Huth v = visitor_input_test_init(data, "{ 'alt': {'integer':1, 'string':'str', " 758da668aa1SThomas Huth "'enum1':'value1', 'boolean':true} }"); 759da668aa1SThomas Huth visit_type_WrapAlternate(v, NULL, &wrap, &error_abort); 760da668aa1SThomas Huth g_assert_cmpint(wrap->alt->type, ==, QTYPE_QDICT); 761da668aa1SThomas Huth g_assert_cmpint(wrap->alt->u.udfu.integer, ==, 1); 762da668aa1SThomas Huth g_assert_cmpstr(wrap->alt->u.udfu.string, ==, "str"); 763da668aa1SThomas Huth g_assert_cmpint(wrap->alt->u.udfu.enum1, ==, ENUM_ONE_VALUE1); 764da668aa1SThomas Huth g_assert_cmpint(wrap->alt->u.udfu.u.value1.boolean, ==, true); 765da668aa1SThomas Huth g_assert_cmpint(wrap->alt->u.udfu.u.value1.has_a_b, ==, false); 766da668aa1SThomas Huth qapi_free_WrapAlternate(wrap); 767da668aa1SThomas Huth } 768da668aa1SThomas Huth 769da668aa1SThomas Huth static void test_visitor_in_alternate_number(TestInputVisitorData *data, 770da668aa1SThomas Huth const void *unused) 771da668aa1SThomas Huth { 772da668aa1SThomas Huth Visitor *v; 773da668aa1SThomas Huth Error *err = NULL; 774da668aa1SThomas Huth AltEnumBool *aeb; 775da668aa1SThomas Huth AltEnumNum *aen; 776da668aa1SThomas Huth AltNumEnum *ans; 777da668aa1SThomas Huth AltEnumInt *asi; 778*79db9948SPaolo Bonzini AltListInt *ali; 779da668aa1SThomas Huth 780da668aa1SThomas Huth /* Parsing an int */ 781da668aa1SThomas Huth 782da668aa1SThomas Huth v = visitor_input_test_init(data, "42"); 783da668aa1SThomas Huth visit_type_AltEnumBool(v, NULL, &aeb, &err); 784da668aa1SThomas Huth error_free_or_abort(&err); 785da668aa1SThomas Huth qapi_free_AltEnumBool(aeb); 786da668aa1SThomas Huth 787da668aa1SThomas Huth v = visitor_input_test_init(data, "42"); 788da668aa1SThomas Huth visit_type_AltEnumNum(v, NULL, &aen, &error_abort); 789da668aa1SThomas Huth g_assert_cmpint(aen->type, ==, QTYPE_QNUM); 790da668aa1SThomas Huth g_assert_cmpfloat(aen->u.n, ==, 42); 791da668aa1SThomas Huth qapi_free_AltEnumNum(aen); 792da668aa1SThomas Huth 793da668aa1SThomas Huth v = visitor_input_test_init(data, "42"); 794da668aa1SThomas Huth visit_type_AltNumEnum(v, NULL, &ans, &error_abort); 795da668aa1SThomas Huth g_assert_cmpint(ans->type, ==, QTYPE_QNUM); 796da668aa1SThomas Huth g_assert_cmpfloat(ans->u.n, ==, 42); 797da668aa1SThomas Huth qapi_free_AltNumEnum(ans); 798da668aa1SThomas Huth 799da668aa1SThomas Huth v = visitor_input_test_init(data, "42"); 800da668aa1SThomas Huth visit_type_AltEnumInt(v, NULL, &asi, &error_abort); 801da668aa1SThomas Huth g_assert_cmpint(asi->type, ==, QTYPE_QNUM); 802da668aa1SThomas Huth g_assert_cmpint(asi->u.i, ==, 42); 803da668aa1SThomas Huth qapi_free_AltEnumInt(asi); 804da668aa1SThomas Huth 805*79db9948SPaolo Bonzini v = visitor_input_test_init(data, "42"); 806*79db9948SPaolo Bonzini visit_type_AltListInt(v, NULL, &ali, &error_abort); 807*79db9948SPaolo Bonzini g_assert_cmpint(ali->type, ==, QTYPE_QNUM); 808*79db9948SPaolo Bonzini g_assert_cmpint(ali->u.i, ==, 42); 809*79db9948SPaolo Bonzini qapi_free_AltListInt(ali); 810*79db9948SPaolo Bonzini 811da668aa1SThomas Huth /* Parsing a double */ 812da668aa1SThomas Huth 813da668aa1SThomas Huth v = visitor_input_test_init(data, "42.5"); 814da668aa1SThomas Huth visit_type_AltEnumBool(v, NULL, &aeb, &err); 815da668aa1SThomas Huth error_free_or_abort(&err); 816da668aa1SThomas Huth qapi_free_AltEnumBool(aeb); 817da668aa1SThomas Huth 818da668aa1SThomas Huth v = visitor_input_test_init(data, "42.5"); 819da668aa1SThomas Huth visit_type_AltEnumNum(v, NULL, &aen, &error_abort); 820da668aa1SThomas Huth g_assert_cmpint(aen->type, ==, QTYPE_QNUM); 821da668aa1SThomas Huth g_assert_cmpfloat(aen->u.n, ==, 42.5); 822da668aa1SThomas Huth qapi_free_AltEnumNum(aen); 823da668aa1SThomas Huth 824da668aa1SThomas Huth v = visitor_input_test_init(data, "42.5"); 825da668aa1SThomas Huth visit_type_AltNumEnum(v, NULL, &ans, &error_abort); 826da668aa1SThomas Huth g_assert_cmpint(ans->type, ==, QTYPE_QNUM); 827da668aa1SThomas Huth g_assert_cmpfloat(ans->u.n, ==, 42.5); 828da668aa1SThomas Huth qapi_free_AltNumEnum(ans); 829da668aa1SThomas Huth 830da668aa1SThomas Huth v = visitor_input_test_init(data, "42.5"); 831da668aa1SThomas Huth visit_type_AltEnumInt(v, NULL, &asi, &err); 832da668aa1SThomas Huth error_free_or_abort(&err); 833da668aa1SThomas Huth qapi_free_AltEnumInt(asi); 834da668aa1SThomas Huth } 835da668aa1SThomas Huth 836*79db9948SPaolo Bonzini static void test_visitor_in_alternate_list(TestInputVisitorData *data, 837*79db9948SPaolo Bonzini const void *unused) 838*79db9948SPaolo Bonzini { 839*79db9948SPaolo Bonzini intList *item; 840*79db9948SPaolo Bonzini Visitor *v; 841*79db9948SPaolo Bonzini AltListInt *ali; 842*79db9948SPaolo Bonzini int i; 843*79db9948SPaolo Bonzini 844*79db9948SPaolo Bonzini v = visitor_input_test_init(data, "[ 42, 43, 44 ]"); 845*79db9948SPaolo Bonzini visit_type_AltListInt(v, NULL, &ali, &error_abort); 846*79db9948SPaolo Bonzini g_assert(ali != NULL); 847*79db9948SPaolo Bonzini 848*79db9948SPaolo Bonzini g_assert_cmpint(ali->type, ==, QTYPE_QLIST); 849*79db9948SPaolo Bonzini for (i = 0, item = ali->u.l; item; item = item->next, i++) { 850*79db9948SPaolo Bonzini g_assert_cmpint(item->value, ==, 42 + i); 851*79db9948SPaolo Bonzini } 852*79db9948SPaolo Bonzini 853*79db9948SPaolo Bonzini qapi_free_AltListInt(ali); 854*79db9948SPaolo Bonzini ali = NULL; 855*79db9948SPaolo Bonzini 856*79db9948SPaolo Bonzini /* An empty list is valid */ 857*79db9948SPaolo Bonzini v = visitor_input_test_init(data, "[]"); 858*79db9948SPaolo Bonzini visit_type_AltListInt(v, NULL, &ali, &error_abort); 859*79db9948SPaolo Bonzini g_assert(ali != NULL); 860*79db9948SPaolo Bonzini 861*79db9948SPaolo Bonzini g_assert_cmpint(ali->type, ==, QTYPE_QLIST); 862*79db9948SPaolo Bonzini g_assert(!ali->u.l); 863*79db9948SPaolo Bonzini qapi_free_AltListInt(ali); 864*79db9948SPaolo Bonzini ali = NULL; 865*79db9948SPaolo Bonzini } 866*79db9948SPaolo Bonzini 867da668aa1SThomas Huth static void input_visitor_test_add(const char *testpath, 868da668aa1SThomas Huth const void *user_data, 869da668aa1SThomas Huth void (*test_func)(TestInputVisitorData *data, 870da668aa1SThomas Huth const void *user_data)) 871da668aa1SThomas Huth { 872da668aa1SThomas Huth g_test_add(testpath, TestInputVisitorData, user_data, NULL, test_func, 873da668aa1SThomas Huth visitor_input_teardown); 874da668aa1SThomas Huth } 875da668aa1SThomas Huth 876da668aa1SThomas Huth static void test_visitor_in_errors(TestInputVisitorData *data, 877da668aa1SThomas Huth const void *unused) 878da668aa1SThomas Huth { 879da668aa1SThomas Huth TestStruct *p = NULL; 880da668aa1SThomas Huth Error *err = NULL; 881da668aa1SThomas Huth Visitor *v; 882da668aa1SThomas Huth strList *q = NULL; 883da668aa1SThomas Huth UserDefTwo *r = NULL; 884da668aa1SThomas Huth WrapAlternate *s = NULL; 885da668aa1SThomas Huth 886da668aa1SThomas Huth v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', " 887da668aa1SThomas Huth "'string': -42 }"); 888da668aa1SThomas Huth 889da668aa1SThomas Huth visit_type_TestStruct(v, NULL, &p, &err); 890da668aa1SThomas Huth error_free_or_abort(&err); 891da668aa1SThomas Huth g_assert(!p); 892da668aa1SThomas Huth 893da668aa1SThomas Huth v = visitor_input_test_init(data, "[ '1', '2', false, '3' ]"); 894da668aa1SThomas Huth visit_type_strList(v, NULL, &q, &err); 895da668aa1SThomas Huth error_free_or_abort(&err); 896da668aa1SThomas Huth assert(!q); 897da668aa1SThomas Huth 898da668aa1SThomas Huth v = visitor_input_test_init(data, "{ 'str':'hi' }"); 899da668aa1SThomas Huth visit_type_UserDefTwo(v, NULL, &r, &err); 900da668aa1SThomas Huth error_free_or_abort(&err); 901da668aa1SThomas Huth assert(!r); 902da668aa1SThomas Huth 903da668aa1SThomas Huth v = visitor_input_test_init(data, "{ }"); 904da668aa1SThomas Huth visit_type_WrapAlternate(v, NULL, &s, &err); 905da668aa1SThomas Huth error_free_or_abort(&err); 906da668aa1SThomas Huth assert(!s); 907da668aa1SThomas Huth } 908da668aa1SThomas Huth 909da668aa1SThomas Huth static void test_visitor_in_wrong_type(TestInputVisitorData *data, 910da668aa1SThomas Huth const void *unused) 911da668aa1SThomas Huth { 912da668aa1SThomas Huth TestStruct *p = NULL; 913da668aa1SThomas Huth Visitor *v; 914da668aa1SThomas Huth strList *q = NULL; 915da668aa1SThomas Huth int64_t i; 916da668aa1SThomas Huth Error *err = NULL; 917da668aa1SThomas Huth 918da668aa1SThomas Huth /* Make sure arrays and structs cannot be confused */ 919da668aa1SThomas Huth 920da668aa1SThomas Huth v = visitor_input_test_init(data, "[]"); 921da668aa1SThomas Huth visit_type_TestStruct(v, NULL, &p, &err); 922da668aa1SThomas Huth error_free_or_abort(&err); 923da668aa1SThomas Huth g_assert(!p); 924da668aa1SThomas Huth 925da668aa1SThomas Huth v = visitor_input_test_init(data, "{}"); 926da668aa1SThomas Huth visit_type_strList(v, NULL, &q, &err); 927da668aa1SThomas Huth error_free_or_abort(&err); 928da668aa1SThomas Huth assert(!q); 929da668aa1SThomas Huth 930da668aa1SThomas Huth /* Make sure primitives and struct cannot be confused */ 931da668aa1SThomas Huth 932da668aa1SThomas Huth v = visitor_input_test_init(data, "1"); 933da668aa1SThomas Huth visit_type_TestStruct(v, NULL, &p, &err); 934da668aa1SThomas Huth error_free_or_abort(&err); 935da668aa1SThomas Huth g_assert(!p); 936da668aa1SThomas Huth 937da668aa1SThomas Huth v = visitor_input_test_init(data, "{}"); 938da668aa1SThomas Huth visit_type_int(v, NULL, &i, &err); 939da668aa1SThomas Huth error_free_or_abort(&err); 940da668aa1SThomas Huth 941da668aa1SThomas Huth /* Make sure primitives and arrays cannot be confused */ 942da668aa1SThomas Huth 943da668aa1SThomas Huth v = visitor_input_test_init(data, "1"); 944da668aa1SThomas Huth visit_type_strList(v, NULL, &q, &err); 945da668aa1SThomas Huth error_free_or_abort(&err); 946da668aa1SThomas Huth assert(!q); 947da668aa1SThomas Huth 948da668aa1SThomas Huth v = visitor_input_test_init(data, "[]"); 949da668aa1SThomas Huth visit_type_int(v, NULL, &i, &err); 950da668aa1SThomas Huth error_free_or_abort(&err); 951da668aa1SThomas Huth } 952da668aa1SThomas Huth 953da668aa1SThomas Huth static void test_visitor_in_fail_struct(TestInputVisitorData *data, 954da668aa1SThomas Huth const void *unused) 955da668aa1SThomas Huth { 956da668aa1SThomas Huth TestStruct *p = NULL; 957da668aa1SThomas Huth Error *err = NULL; 958da668aa1SThomas Huth Visitor *v; 959da668aa1SThomas Huth 960da668aa1SThomas Huth v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }"); 961da668aa1SThomas Huth 962da668aa1SThomas Huth visit_type_TestStruct(v, NULL, &p, &err); 963da668aa1SThomas Huth error_free_or_abort(&err); 964da668aa1SThomas Huth g_assert(!p); 965da668aa1SThomas Huth } 966da668aa1SThomas Huth 967da668aa1SThomas Huth static void test_visitor_in_fail_struct_nested(TestInputVisitorData *data, 968da668aa1SThomas Huth const void *unused) 969da668aa1SThomas Huth { 970da668aa1SThomas Huth UserDefTwo *udp = NULL; 971da668aa1SThomas Huth Error *err = NULL; 972da668aa1SThomas Huth Visitor *v; 973da668aa1SThomas Huth 974da668aa1SThomas Huth v = visitor_input_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}"); 975da668aa1SThomas Huth 976da668aa1SThomas Huth visit_type_UserDefTwo(v, NULL, &udp, &err); 977da668aa1SThomas Huth error_free_or_abort(&err); 978da668aa1SThomas Huth g_assert(!udp); 979da668aa1SThomas Huth } 980da668aa1SThomas Huth 981da668aa1SThomas Huth static void test_visitor_in_fail_struct_in_list(TestInputVisitorData *data, 982da668aa1SThomas Huth const void *unused) 983da668aa1SThomas Huth { 984da668aa1SThomas Huth UserDefOneList *head = NULL; 985da668aa1SThomas Huth Error *err = NULL; 986da668aa1SThomas Huth Visitor *v; 987da668aa1SThomas Huth 988da668aa1SThomas Huth v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]"); 989da668aa1SThomas Huth 990da668aa1SThomas Huth visit_type_UserDefOneList(v, NULL, &head, &err); 991da668aa1SThomas Huth error_free_or_abort(&err); 992da668aa1SThomas Huth g_assert(!head); 993da668aa1SThomas Huth } 994da668aa1SThomas Huth 995da668aa1SThomas Huth static void test_visitor_in_fail_struct_missing(TestInputVisitorData *data, 996da668aa1SThomas Huth const void *unused) 997da668aa1SThomas Huth { 998da668aa1SThomas Huth Error *err = NULL; 999da668aa1SThomas Huth Visitor *v; 1000da668aa1SThomas Huth QObject *any; 1001da668aa1SThomas Huth QNull *null; 1002da668aa1SThomas Huth GenericAlternate *alt; 1003da668aa1SThomas Huth bool present; 1004da668aa1SThomas Huth int en; 1005da668aa1SThomas Huth int64_t i64; 1006da668aa1SThomas Huth uint32_t u32; 1007da668aa1SThomas Huth int8_t i8; 1008da668aa1SThomas Huth char *str; 1009da668aa1SThomas Huth double dbl; 1010da668aa1SThomas Huth 1011da668aa1SThomas Huth v = visitor_input_test_init(data, "{ 'sub': [ {} ] }"); 1012da668aa1SThomas Huth visit_start_struct(v, NULL, NULL, 0, &error_abort); 1013da668aa1SThomas Huth visit_start_struct(v, "struct", NULL, 0, &err); 1014da668aa1SThomas Huth error_free_or_abort(&err); 1015da668aa1SThomas Huth visit_start_list(v, "list", NULL, 0, &err); 1016da668aa1SThomas Huth error_free_or_abort(&err); 1017da668aa1SThomas Huth visit_start_alternate(v, "alternate", &alt, sizeof(*alt), &err); 1018da668aa1SThomas Huth error_free_or_abort(&err); 1019da668aa1SThomas Huth visit_optional(v, "optional", &present); 1020da668aa1SThomas Huth g_assert(!present); 1021da668aa1SThomas Huth visit_type_enum(v, "enum", &en, &EnumOne_lookup, &err); 1022da668aa1SThomas Huth error_free_or_abort(&err); 1023da668aa1SThomas Huth visit_type_int(v, "i64", &i64, &err); 1024da668aa1SThomas Huth error_free_or_abort(&err); 1025da668aa1SThomas Huth visit_type_uint32(v, "u32", &u32, &err); 1026da668aa1SThomas Huth error_free_or_abort(&err); 1027da668aa1SThomas Huth visit_type_int8(v, "i8", &i8, &err); 1028da668aa1SThomas Huth error_free_or_abort(&err); 1029da668aa1SThomas Huth visit_type_str(v, "i8", &str, &err); 1030da668aa1SThomas Huth error_free_or_abort(&err); 1031da668aa1SThomas Huth visit_type_number(v, "dbl", &dbl, &err); 1032da668aa1SThomas Huth error_free_or_abort(&err); 1033da668aa1SThomas Huth visit_type_any(v, "any", &any, &err); 1034da668aa1SThomas Huth error_free_or_abort(&err); 1035da668aa1SThomas Huth visit_type_null(v, "null", &null, &err); 1036da668aa1SThomas Huth error_free_or_abort(&err); 1037da668aa1SThomas Huth visit_start_list(v, "sub", NULL, 0, &error_abort); 1038da668aa1SThomas Huth visit_start_struct(v, NULL, NULL, 0, &error_abort); 1039da668aa1SThomas Huth visit_type_int(v, "i64", &i64, &err); 1040da668aa1SThomas Huth error_free_or_abort(&err); 1041da668aa1SThomas Huth visit_end_struct(v, NULL); 1042da668aa1SThomas Huth visit_end_list(v, NULL); 1043da668aa1SThomas Huth visit_end_struct(v, NULL); 1044da668aa1SThomas Huth } 1045da668aa1SThomas Huth 1046da668aa1SThomas Huth static void test_visitor_in_fail_list(TestInputVisitorData *data, 1047da668aa1SThomas Huth const void *unused) 1048da668aa1SThomas Huth { 1049da668aa1SThomas Huth int64_t i64 = -1; 1050da668aa1SThomas Huth Error *err = NULL; 1051da668aa1SThomas Huth Visitor *v; 1052da668aa1SThomas Huth 1053da668aa1SThomas Huth /* Unvisited list tail */ 1054da668aa1SThomas Huth 1055da668aa1SThomas Huth v = visitor_input_test_init(data, "[ 1, 2, 3 ]"); 1056da668aa1SThomas Huth 1057da668aa1SThomas Huth visit_start_list(v, NULL, NULL, 0, &error_abort); 1058da668aa1SThomas Huth visit_type_int(v, NULL, &i64, &error_abort); 1059da668aa1SThomas Huth g_assert_cmpint(i64, ==, 1); 1060da668aa1SThomas Huth visit_type_int(v, NULL, &i64, &error_abort); 1061da668aa1SThomas Huth g_assert_cmpint(i64, ==, 2); 1062da668aa1SThomas Huth visit_check_list(v, &err); 1063da668aa1SThomas Huth error_free_or_abort(&err); 1064da668aa1SThomas Huth visit_end_list(v, NULL); 1065da668aa1SThomas Huth 1066da668aa1SThomas Huth /* Visit beyond end of list */ 1067da668aa1SThomas Huth v = visitor_input_test_init(data, "[]"); 1068da668aa1SThomas Huth 1069da668aa1SThomas Huth visit_start_list(v, NULL, NULL, 0, &error_abort); 1070da668aa1SThomas Huth visit_type_int(v, NULL, &i64, &err); 1071da668aa1SThomas Huth error_free_or_abort(&err); 1072da668aa1SThomas Huth visit_end_list(v, NULL); 1073da668aa1SThomas Huth } 1074da668aa1SThomas Huth 1075da668aa1SThomas Huth static void test_visitor_in_fail_list_nested(TestInputVisitorData *data, 1076da668aa1SThomas Huth const void *unused) 1077da668aa1SThomas Huth { 1078da668aa1SThomas Huth int64_t i64 = -1; 1079da668aa1SThomas Huth Error *err = NULL; 1080da668aa1SThomas Huth Visitor *v; 1081da668aa1SThomas Huth 1082da668aa1SThomas Huth /* Unvisited nested list tail */ 1083da668aa1SThomas Huth 1084da668aa1SThomas Huth v = visitor_input_test_init(data, "[ 0, [ 1, 2, 3 ] ]"); 1085da668aa1SThomas Huth 1086da668aa1SThomas Huth visit_start_list(v, NULL, NULL, 0, &error_abort); 1087da668aa1SThomas Huth visit_type_int(v, NULL, &i64, &error_abort); 1088da668aa1SThomas Huth g_assert_cmpint(i64, ==, 0); 1089da668aa1SThomas Huth visit_start_list(v, NULL, NULL, 0, &error_abort); 1090da668aa1SThomas Huth visit_type_int(v, NULL, &i64, &error_abort); 1091da668aa1SThomas Huth g_assert_cmpint(i64, ==, 1); 1092da668aa1SThomas Huth visit_check_list(v, &err); 1093da668aa1SThomas Huth error_free_or_abort(&err); 1094da668aa1SThomas Huth visit_end_list(v, NULL); 1095da668aa1SThomas Huth visit_check_list(v, &error_abort); 1096da668aa1SThomas Huth visit_end_list(v, NULL); 1097da668aa1SThomas Huth } 1098da668aa1SThomas Huth 1099da668aa1SThomas Huth static void test_visitor_in_fail_union_flat(TestInputVisitorData *data, 1100da668aa1SThomas Huth const void *unused) 1101da668aa1SThomas Huth { 1102da668aa1SThomas Huth UserDefFlatUnion *tmp = NULL; 1103da668aa1SThomas Huth Error *err = NULL; 1104da668aa1SThomas Huth Visitor *v; 1105da668aa1SThomas Huth 1106e7a76fe2SMarkus Armbruster v = visitor_input_test_init(data, "{ 'enum1': 'value2', 'string': 'c', 'integer': 41, 'boolean': true }"); 1107da668aa1SThomas Huth 1108da668aa1SThomas Huth visit_type_UserDefFlatUnion(v, NULL, &tmp, &err); 1109da668aa1SThomas Huth error_free_or_abort(&err); 1110da668aa1SThomas Huth g_assert(!tmp); 1111da668aa1SThomas Huth } 1112da668aa1SThomas Huth 1113da668aa1SThomas Huth static void test_visitor_in_fail_union_flat_no_discrim(TestInputVisitorData *data, 1114da668aa1SThomas Huth const void *unused) 1115da668aa1SThomas Huth { 1116da668aa1SThomas Huth UserDefFlatUnion2 *tmp = NULL; 1117da668aa1SThomas Huth Error *err = NULL; 1118da668aa1SThomas Huth Visitor *v; 1119da668aa1SThomas Huth 1120da668aa1SThomas Huth /* test situation where discriminator field ('enum1' here) is missing */ 1121da668aa1SThomas Huth v = visitor_input_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }"); 1122da668aa1SThomas Huth 1123da668aa1SThomas Huth visit_type_UserDefFlatUnion2(v, NULL, &tmp, &err); 1124da668aa1SThomas Huth error_free_or_abort(&err); 1125da668aa1SThomas Huth g_assert(!tmp); 1126da668aa1SThomas Huth } 1127da668aa1SThomas Huth 1128da668aa1SThomas Huth static void test_visitor_in_fail_alternate(TestInputVisitorData *data, 1129da668aa1SThomas Huth const void *unused) 1130da668aa1SThomas Huth { 1131da668aa1SThomas Huth UserDefAlternate *tmp; 1132da668aa1SThomas Huth Visitor *v; 1133da668aa1SThomas Huth Error *err = NULL; 1134da668aa1SThomas Huth 1135da668aa1SThomas Huth v = visitor_input_test_init(data, "3.14"); 1136da668aa1SThomas Huth 1137da668aa1SThomas Huth visit_type_UserDefAlternate(v, NULL, &tmp, &err); 1138da668aa1SThomas Huth error_free_or_abort(&err); 1139da668aa1SThomas Huth g_assert(!tmp); 1140da668aa1SThomas Huth } 1141da668aa1SThomas Huth 1142da668aa1SThomas Huth static void do_test_visitor_in_qmp_introspect(TestInputVisitorData *data, 1143da668aa1SThomas Huth const QLitObject *qlit) 1144da668aa1SThomas Huth { 1145da668aa1SThomas Huth g_autoptr(SchemaInfoList) schema = NULL; 1146da668aa1SThomas Huth QObject *obj = qobject_from_qlit(qlit); 1147da668aa1SThomas Huth Visitor *v; 1148da668aa1SThomas Huth 1149da668aa1SThomas Huth v = qobject_input_visitor_new(obj); 1150da668aa1SThomas Huth 1151da668aa1SThomas Huth visit_type_SchemaInfoList(v, NULL, &schema, &error_abort); 1152da668aa1SThomas Huth g_assert(schema); 1153da668aa1SThomas Huth 1154da668aa1SThomas Huth qobject_unref(obj); 1155da668aa1SThomas Huth visit_free(v); 1156da668aa1SThomas Huth } 1157da668aa1SThomas Huth 1158da668aa1SThomas Huth static void test_visitor_in_qmp_introspect(TestInputVisitorData *data, 1159da668aa1SThomas Huth const void *unused) 1160da668aa1SThomas Huth { 1161da668aa1SThomas Huth do_test_visitor_in_qmp_introspect(data, &test_qmp_schema_qlit); 1162da668aa1SThomas Huth } 1163da668aa1SThomas Huth 1164da668aa1SThomas Huth int main(int argc, char **argv) 1165da668aa1SThomas Huth { 1166da668aa1SThomas Huth g_test_init(&argc, &argv, NULL); 1167da668aa1SThomas Huth 1168da668aa1SThomas Huth input_visitor_test_add("/visitor/input/int", 1169da668aa1SThomas Huth NULL, test_visitor_in_int); 1170da668aa1SThomas Huth input_visitor_test_add("/visitor/input/uint", 1171da668aa1SThomas Huth NULL, test_visitor_in_uint); 1172da668aa1SThomas Huth input_visitor_test_add("/visitor/input/int_overflow", 1173da668aa1SThomas Huth NULL, test_visitor_in_int_overflow); 1174da668aa1SThomas Huth input_visitor_test_add("/visitor/input/int_keyval", 1175da668aa1SThomas Huth NULL, test_visitor_in_int_keyval); 1176da668aa1SThomas Huth input_visitor_test_add("/visitor/input/int_str_keyval", 1177da668aa1SThomas Huth NULL, test_visitor_in_int_str_keyval); 1178da668aa1SThomas Huth input_visitor_test_add("/visitor/input/int_str_fail", 1179da668aa1SThomas Huth NULL, test_visitor_in_int_str_fail); 1180da668aa1SThomas Huth input_visitor_test_add("/visitor/input/bool", 1181da668aa1SThomas Huth NULL, test_visitor_in_bool); 1182da668aa1SThomas Huth input_visitor_test_add("/visitor/input/bool_keyval", 1183da668aa1SThomas Huth NULL, test_visitor_in_bool_keyval); 1184da668aa1SThomas Huth input_visitor_test_add("/visitor/input/bool_str_keyval", 1185da668aa1SThomas Huth NULL, test_visitor_in_bool_str_keyval); 1186da668aa1SThomas Huth input_visitor_test_add("/visitor/input/bool_str_fail", 1187da668aa1SThomas Huth NULL, test_visitor_in_bool_str_fail); 1188da668aa1SThomas Huth input_visitor_test_add("/visitor/input/number", 1189da668aa1SThomas Huth NULL, test_visitor_in_number); 1190da668aa1SThomas Huth input_visitor_test_add("/visitor/input/large_number", 1191da668aa1SThomas Huth NULL, test_visitor_in_large_number); 1192da668aa1SThomas Huth input_visitor_test_add("/visitor/input/number_keyval", 1193da668aa1SThomas Huth NULL, test_visitor_in_number_keyval); 1194da668aa1SThomas Huth input_visitor_test_add("/visitor/input/number_str_keyval", 1195da668aa1SThomas Huth NULL, test_visitor_in_number_str_keyval); 1196da668aa1SThomas Huth input_visitor_test_add("/visitor/input/number_str_fail", 1197da668aa1SThomas Huth NULL, test_visitor_in_number_str_fail); 1198da668aa1SThomas Huth input_visitor_test_add("/visitor/input/size_str_keyval", 1199da668aa1SThomas Huth NULL, test_visitor_in_size_str_keyval); 1200da668aa1SThomas Huth input_visitor_test_add("/visitor/input/size_str_fail", 1201da668aa1SThomas Huth NULL, test_visitor_in_size_str_fail); 1202da668aa1SThomas Huth input_visitor_test_add("/visitor/input/string", 1203da668aa1SThomas Huth NULL, test_visitor_in_string); 1204da668aa1SThomas Huth input_visitor_test_add("/visitor/input/enum", 1205da668aa1SThomas Huth NULL, test_visitor_in_enum); 1206da668aa1SThomas Huth input_visitor_test_add("/visitor/input/struct", 1207da668aa1SThomas Huth NULL, test_visitor_in_struct); 1208da668aa1SThomas Huth input_visitor_test_add("/visitor/input/struct-nested", 1209da668aa1SThomas Huth NULL, test_visitor_in_struct_nested); 1210e7a76fe2SMarkus Armbruster input_visitor_test_add("/visitor/input/list2", 1211e7a76fe2SMarkus Armbruster NULL, test_visitor_in_list_struct); 1212da668aa1SThomas Huth input_visitor_test_add("/visitor/input/list", 1213da668aa1SThomas Huth NULL, test_visitor_in_list); 1214da668aa1SThomas Huth input_visitor_test_add("/visitor/input/any", 1215da668aa1SThomas Huth NULL, test_visitor_in_any); 1216da668aa1SThomas Huth input_visitor_test_add("/visitor/input/null", 1217da668aa1SThomas Huth NULL, test_visitor_in_null); 1218da668aa1SThomas Huth input_visitor_test_add("/visitor/input/union-flat", 1219da668aa1SThomas Huth NULL, test_visitor_in_union_flat); 1220da668aa1SThomas Huth input_visitor_test_add("/visitor/input/alternate", 1221da668aa1SThomas Huth NULL, test_visitor_in_alternate); 1222da668aa1SThomas Huth input_visitor_test_add("/visitor/input/errors", 1223da668aa1SThomas Huth NULL, test_visitor_in_errors); 1224da668aa1SThomas Huth input_visitor_test_add("/visitor/input/wrong-type", 1225da668aa1SThomas Huth NULL, test_visitor_in_wrong_type); 1226da668aa1SThomas Huth input_visitor_test_add("/visitor/input/alternate-number", 1227da668aa1SThomas Huth NULL, test_visitor_in_alternate_number); 1228*79db9948SPaolo Bonzini input_visitor_test_add("/visitor/input/alternate-list", 1229*79db9948SPaolo Bonzini NULL, test_visitor_in_alternate_list); 1230da668aa1SThomas Huth input_visitor_test_add("/visitor/input/fail/struct", 1231da668aa1SThomas Huth NULL, test_visitor_in_fail_struct); 1232da668aa1SThomas Huth input_visitor_test_add("/visitor/input/fail/struct-nested", 1233da668aa1SThomas Huth NULL, test_visitor_in_fail_struct_nested); 1234da668aa1SThomas Huth input_visitor_test_add("/visitor/input/fail/struct-in-list", 1235da668aa1SThomas Huth NULL, test_visitor_in_fail_struct_in_list); 1236da668aa1SThomas Huth input_visitor_test_add("/visitor/input/fail/struct-missing", 1237da668aa1SThomas Huth NULL, test_visitor_in_fail_struct_missing); 1238da668aa1SThomas Huth input_visitor_test_add("/visitor/input/fail/list", 1239da668aa1SThomas Huth NULL, test_visitor_in_fail_list); 1240da668aa1SThomas Huth input_visitor_test_add("/visitor/input/fail/list-nested", 1241da668aa1SThomas Huth NULL, test_visitor_in_fail_list_nested); 1242da668aa1SThomas Huth input_visitor_test_add("/visitor/input/fail/union-flat", 1243da668aa1SThomas Huth NULL, test_visitor_in_fail_union_flat); 1244da668aa1SThomas Huth input_visitor_test_add("/visitor/input/fail/union-flat-no-discriminator", 1245da668aa1SThomas Huth NULL, test_visitor_in_fail_union_flat_no_discrim); 1246da668aa1SThomas Huth input_visitor_test_add("/visitor/input/fail/alternate", 1247da668aa1SThomas Huth NULL, test_visitor_in_fail_alternate); 1248da668aa1SThomas Huth input_visitor_test_add("/visitor/input/qapi-introspect", 1249da668aa1SThomas Huth NULL, test_visitor_in_qmp_introspect); 1250da668aa1SThomas Huth 1251da668aa1SThomas Huth g_test_run(); 1252da668aa1SThomas Huth 1253da668aa1SThomas Huth return 0; 1254da668aa1SThomas Huth } 1255