2 * QObject Input Visitor unit-tests.
4 * Copyright (C) 2011-2016 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 * Paolo Bonzini <pbonzini@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/qobject-input-visitor.h"
19 #include "test-qapi-types.h"
20 #include "test-qapi-visit.h"
21 #include "qapi/qmp/types.h"
22 #include "qapi/qmp/qjson.h"
23 #include "test-qmp-introspect.h"
24 #include "qmp-introspect.h"
25 #include "qapi-visit.h"
27 typedef struct TestInputVisitorData
{
30 } TestInputVisitorData
;
32 static void visitor_input_teardown(TestInputVisitorData
*data
,
35 qobject_decref(data
->obj
);
39 visit_free(data
->qiv
);
44 /* The various test_init functions are provided instead of a test setup
45 function so that the JSON string used by the tests are kept in the test
46 functions (and not in main()). */
47 static Visitor
*visitor_input_test_init_internal(TestInputVisitorData
*data
,
49 const char *json_string
,
52 visitor_input_teardown(data
, NULL
);
54 data
->obj
= qobject_from_jsonv(json_string
, ap
, &error_abort
);
58 data
->qiv
= qobject_input_visitor_new_keyval(data
->obj
);
60 data
->qiv
= qobject_input_visitor_new(data
->obj
);
66 static GCC_FMT_ATTR(3, 4)
67 Visitor
*visitor_input_test_init_full(TestInputVisitorData
*data
,
69 const char *json_string
, ...)
74 va_start(ap
, json_string
);
75 v
= visitor_input_test_init_internal(data
, keyval
, json_string
, &ap
);
80 static GCC_FMT_ATTR(2, 3)
81 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
82 const char *json_string
, ...)
87 va_start(ap
, json_string
);
88 v
= visitor_input_test_init_internal(data
, false, json_string
, &ap
);
93 /* similar to visitor_input_test_init(), but does not expect a string
94 * literal/format json_string argument and so can be used for
95 * programatically generated strings (and we can't pass in programatically
96 * generated strings via %s format parameters since qobject_from_jsonv()
97 * will wrap those in double-quotes and treat the entire object as a
100 static Visitor
*visitor_input_test_init_raw(TestInputVisitorData
*data
,
101 const char *json_string
)
103 return visitor_input_test_init_internal(data
, false, json_string
, NULL
);
106 static void test_visitor_in_int(TestInputVisitorData
*data
,
113 v
= visitor_input_test_init(data
, "%d", value
);
115 visit_type_int(v
, NULL
, &res
, &error_abort
);
116 g_assert_cmpint(res
, ==, value
);
119 static void test_visitor_in_uint(TestInputVisitorData
*data
,
127 v
= visitor_input_test_init(data
, "%d", value
);
129 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
130 g_assert_cmpuint(res
, ==, (uint64_t)value
);
132 /* BUG: value between INT64_MIN and -1 accepted modulo 2^64 */
134 v
= visitor_input_test_init(data
, "%d", -value
);
136 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
137 g_assert_cmpuint(res
, ==, (uint64_t)-value
);
139 /* BUG: value between INT64_MAX+1 and UINT64_MAX rejected */
141 v
= visitor_input_test_init(data
, "18446744073709551574");
143 visit_type_uint64(v
, NULL
, &res
, &err
);
144 error_free_or_abort(&err
);
147 static void test_visitor_in_int_overflow(TestInputVisitorData
*data
,
154 /* this will overflow a Qint/int64, so should be deserialized into
155 * a QFloat/double field instead, leading to an error if we pass it
156 * to visit_type_int. confirm this.
158 v
= visitor_input_test_init(data
, "%f", DBL_MAX
);
160 visit_type_int(v
, NULL
, &res
, &err
);
161 error_free_or_abort(&err
);
164 static void test_visitor_in_int_keyval(TestInputVisitorData
*data
,
167 int64_t res
= 0, value
= -42;
171 v
= visitor_input_test_init_full(data
, true, "%" PRId64
, value
);
172 visit_type_int(v
, NULL
, &res
, &err
);
173 error_free_or_abort(&err
);
176 static void test_visitor_in_int_str_keyval(TestInputVisitorData
*data
,
179 int64_t res
= 0, value
= -42;
182 v
= visitor_input_test_init_full(data
, true, "\"-42\"");
184 visit_type_int(v
, NULL
, &res
, &error_abort
);
185 g_assert_cmpint(res
, ==, value
);
188 static void test_visitor_in_int_str_fail(TestInputVisitorData
*data
,
195 v
= visitor_input_test_init(data
, "\"-42\"");
197 visit_type_int(v
, NULL
, &res
, &err
);
198 error_free_or_abort(&err
);
201 static void test_visitor_in_bool(TestInputVisitorData
*data
,
207 v
= visitor_input_test_init(data
, "true");
209 visit_type_bool(v
, NULL
, &res
, &error_abort
);
210 g_assert_cmpint(res
, ==, true);
213 static void test_visitor_in_bool_keyval(TestInputVisitorData
*data
,
220 v
= visitor_input_test_init_full(data
, true, "true");
222 visit_type_bool(v
, NULL
, &res
, &err
);
223 error_free_or_abort(&err
);
226 static void test_visitor_in_bool_str_keyval(TestInputVisitorData
*data
,
232 v
= visitor_input_test_init_full(data
, true, "\"on\"");
234 visit_type_bool(v
, NULL
, &res
, &error_abort
);
235 g_assert_cmpint(res
, ==, true);
238 static void test_visitor_in_bool_str_fail(TestInputVisitorData
*data
,
245 v
= visitor_input_test_init(data
, "\"true\"");
247 visit_type_bool(v
, NULL
, &res
, &err
);
248 error_free_or_abort(&err
);
251 static void test_visitor_in_number(TestInputVisitorData
*data
,
254 double res
= 0, value
= 3.14;
257 v
= visitor_input_test_init(data
, "%f", value
);
259 visit_type_number(v
, NULL
, &res
, &error_abort
);
260 g_assert_cmpfloat(res
, ==, value
);
263 static void test_visitor_in_number_keyval(TestInputVisitorData
*data
,
266 double res
= 0, value
= 3.14;
270 v
= visitor_input_test_init_full(data
, true, "%f", value
);
272 visit_type_number(v
, NULL
, &res
, &err
);
273 error_free_or_abort(&err
);
276 static void test_visitor_in_number_str_keyval(TestInputVisitorData
*data
,
279 double res
= 0, value
= 3.14;
283 v
= visitor_input_test_init_full(data
, true, "\"3.14\"");
285 visit_type_number(v
, NULL
, &res
, &error_abort
);
286 g_assert_cmpfloat(res
, ==, value
);
288 v
= visitor_input_test_init_full(data
, true, "\"inf\"");
290 visit_type_number(v
, NULL
, &res
, &err
);
291 error_free_or_abort(&err
);
294 static void test_visitor_in_number_str_fail(TestInputVisitorData
*data
,
301 v
= visitor_input_test_init(data
, "\"3.14\"");
303 visit_type_number(v
, NULL
, &res
, &err
);
304 error_free_or_abort(&err
);
307 static void test_visitor_in_size_str_keyval(TestInputVisitorData
*data
,
310 uint64_t res
, value
= 500 * 1024 * 1024;
313 v
= visitor_input_test_init_full(data
, true, "\"500M\"");
315 visit_type_size(v
, NULL
, &res
, &error_abort
);
316 g_assert_cmpfloat(res
, ==, value
);
319 static void test_visitor_in_size_str_fail(TestInputVisitorData
*data
,
326 v
= visitor_input_test_init(data
, "\"500M\"");
328 visit_type_size(v
, NULL
, &res
, &err
);
329 error_free_or_abort(&err
);
332 static void test_visitor_in_string(TestInputVisitorData
*data
,
335 char *res
= NULL
, *value
= (char *) "Q E M U";
338 v
= visitor_input_test_init(data
, "%s", value
);
340 visit_type_str(v
, NULL
, &res
, &error_abort
);
341 g_assert_cmpstr(res
, ==, value
);
346 static void test_visitor_in_enum(TestInputVisitorData
*data
,
352 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
355 v
= visitor_input_test_init(data
, "%s", EnumOne_lookup
[i
]);
357 visit_type_EnumOne(v
, NULL
, &res
, &error_abort
);
358 g_assert_cmpint(i
, ==, res
);
363 static void test_visitor_in_struct(TestInputVisitorData
*data
,
366 TestStruct
*p
= NULL
;
369 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
371 visit_type_TestStruct(v
, NULL
, &p
, &error_abort
);
372 g_assert_cmpint(p
->integer
, ==, -42);
373 g_assert(p
->boolean
== true);
374 g_assert_cmpstr(p
->string
, ==, "foo");
380 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
383 UserDefTwo
*udp
= NULL
;
386 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', "
387 "'dict1': { 'string1': 'string1', "
388 "'dict2': { 'userdef': { 'integer': 42, "
389 "'string': 'string' }, 'string': 'string2'}}}");
391 visit_type_UserDefTwo(v
, NULL
, &udp
, &error_abort
);
393 g_assert_cmpstr(udp
->string0
, ==, "string0");
394 g_assert_cmpstr(udp
->dict1
->string1
, ==, "string1");
395 g_assert_cmpint(udp
->dict1
->dict2
->userdef
->integer
, ==, 42);
396 g_assert_cmpstr(udp
->dict1
->dict2
->userdef
->string
, ==, "string");
397 g_assert_cmpstr(udp
->dict1
->dict2
->string
, ==, "string2");
398 g_assert(udp
->dict1
->has_dict3
== false);
400 qapi_free_UserDefTwo(udp
);
403 static void test_visitor_in_list(TestInputVisitorData
*data
,
406 UserDefOneList
*item
, *head
= NULL
;
410 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
412 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
413 g_assert(head
!= NULL
);
415 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
418 snprintf(string
, sizeof(string
), "string%d", i
);
419 g_assert_cmpstr(item
->value
->string
, ==, string
);
420 g_assert_cmpint(item
->value
->integer
, ==, 42 + i
);
423 qapi_free_UserDefOneList(head
);
426 /* An empty list is valid */
427 v
= visitor_input_test_init(data
, "[]");
428 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
432 static void test_visitor_in_any(TestInputVisitorData
*data
,
443 v
= visitor_input_test_init(data
, "-42");
444 visit_type_any(v
, NULL
, &res
, &error_abort
);
445 qint
= qobject_to_qint(res
);
447 g_assert_cmpint(qint_get_int(qint
), ==, -42);
450 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
451 visit_type_any(v
, NULL
, &res
, &error_abort
);
452 qdict
= qobject_to_qdict(res
);
453 g_assert(qdict
&& qdict_size(qdict
) == 3);
454 qobj
= qdict_get(qdict
, "integer");
456 qint
= qobject_to_qint(qobj
);
458 g_assert_cmpint(qint_get_int(qint
), ==, -42);
459 qobj
= qdict_get(qdict
, "boolean");
461 qbool
= qobject_to_qbool(qobj
);
463 g_assert(qbool_get_bool(qbool
) == true);
464 qobj
= qdict_get(qdict
, "string");
466 qstring
= qobject_to_qstring(qobj
);
468 g_assert_cmpstr(qstring_get_str(qstring
), ==, "foo");
472 static void test_visitor_in_null(TestInputVisitorData
*data
,
480 * FIXME: Since QAPI doesn't know the 'null' type yet, we can't
481 * test visit_type_null() by reading into a QAPI struct then
482 * checking that it was populated correctly. The best we can do
483 * for now is ensure that we consumed null from the input, proven
484 * by the fact that we can't re-read the key; and that we detect
485 * when input is not null.
488 v
= visitor_input_test_init_full(data
, false,
489 "{ 'a': null, 'b': '' }");
490 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
491 visit_type_null(v
, "a", &error_abort
);
492 visit_type_null(v
, "b", &err
);
493 error_free_or_abort(&err
);
494 visit_type_str(v
, "c", &tmp
, &err
);
496 error_free_or_abort(&err
);
497 visit_check_struct(v
, &error_abort
);
498 visit_end_struct(v
, NULL
);
501 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
505 UserDefFlatUnion
*tmp
;
506 UserDefUnionBase
*base
;
508 v
= visitor_input_test_init(data
,
509 "{ 'enum1': 'value1', "
512 "'boolean': true }");
514 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &error_abort
);
515 g_assert_cmpint(tmp
->enum1
, ==, ENUM_ONE_VALUE1
);
516 g_assert_cmpstr(tmp
->string
, ==, "str");
517 g_assert_cmpint(tmp
->integer
, ==, 41);
518 g_assert_cmpint(tmp
->u
.value1
.boolean
, ==, true);
520 base
= qapi_UserDefFlatUnion_base(tmp
);
521 g_assert(&base
->enum1
== &tmp
->enum1
);
523 qapi_free_UserDefFlatUnion(tmp
);
526 static void test_visitor_in_alternate(TestInputVisitorData
*data
,
531 UserDefAlternate
*tmp
;
534 v
= visitor_input_test_init(data
, "42");
535 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
536 g_assert_cmpint(tmp
->type
, ==, QTYPE_QINT
);
537 g_assert_cmpint(tmp
->u
.i
, ==, 42);
538 qapi_free_UserDefAlternate(tmp
);
540 v
= visitor_input_test_init(data
, "'value1'");
541 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
542 g_assert_cmpint(tmp
->type
, ==, QTYPE_QSTRING
);
543 g_assert_cmpint(tmp
->u
.e
, ==, ENUM_ONE_VALUE1
);
544 qapi_free_UserDefAlternate(tmp
);
546 v
= visitor_input_test_init(data
, "{'integer':1, 'string':'str', "
547 "'enum1':'value1', 'boolean':true}");
548 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
549 g_assert_cmpint(tmp
->type
, ==, QTYPE_QDICT
);
550 g_assert_cmpint(tmp
->u
.udfu
.integer
, ==, 1);
551 g_assert_cmpstr(tmp
->u
.udfu
.string
, ==, "str");
552 g_assert_cmpint(tmp
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
553 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.boolean
, ==, true);
554 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
555 qapi_free_UserDefAlternate(tmp
);
557 v
= visitor_input_test_init(data
, "false");
558 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
559 error_free_or_abort(&err
);
560 qapi_free_UserDefAlternate(tmp
);
562 v
= visitor_input_test_init(data
, "{ 'alt': 42 }");
563 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
564 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QINT
);
565 g_assert_cmpint(wrap
->alt
->u
.i
, ==, 42);
566 qapi_free_WrapAlternate(wrap
);
568 v
= visitor_input_test_init(data
, "{ 'alt': 'value1' }");
569 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
570 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QSTRING
);
571 g_assert_cmpint(wrap
->alt
->u
.e
, ==, ENUM_ONE_VALUE1
);
572 qapi_free_WrapAlternate(wrap
);
574 v
= visitor_input_test_init(data
, "{ 'alt': {'integer':1, 'string':'str', "
575 "'enum1':'value1', 'boolean':true} }");
576 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
577 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QDICT
);
578 g_assert_cmpint(wrap
->alt
->u
.udfu
.integer
, ==, 1);
579 g_assert_cmpstr(wrap
->alt
->u
.udfu
.string
, ==, "str");
580 g_assert_cmpint(wrap
->alt
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
581 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.boolean
, ==, true);
582 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
583 qapi_free_WrapAlternate(wrap
);
586 static void test_visitor_in_alternate_number(TestInputVisitorData
*data
,
600 v
= visitor_input_test_init(data
, "42");
601 visit_type_AltEnumBool(v
, NULL
, &aeb
, &err
);
602 error_free_or_abort(&err
);
603 qapi_free_AltEnumBool(aeb
);
605 v
= visitor_input_test_init(data
, "42");
606 visit_type_AltEnumNum(v
, NULL
, &aen
, &error_abort
);
607 g_assert_cmpint(aen
->type
, ==, QTYPE_QFLOAT
);
608 g_assert_cmpfloat(aen
->u
.n
, ==, 42);
609 qapi_free_AltEnumNum(aen
);
611 v
= visitor_input_test_init(data
, "42");
612 visit_type_AltNumEnum(v
, NULL
, &ans
, &error_abort
);
613 g_assert_cmpint(ans
->type
, ==, QTYPE_QFLOAT
);
614 g_assert_cmpfloat(ans
->u
.n
, ==, 42);
615 qapi_free_AltNumEnum(ans
);
617 v
= visitor_input_test_init(data
, "42");
618 visit_type_AltEnumInt(v
, NULL
, &asi
, &error_abort
);
619 g_assert_cmpint(asi
->type
, ==, QTYPE_QINT
);
620 g_assert_cmpint(asi
->u
.i
, ==, 42);
621 qapi_free_AltEnumInt(asi
);
623 v
= visitor_input_test_init(data
, "42");
624 visit_type_AltIntNum(v
, NULL
, &ain
, &error_abort
);
625 g_assert_cmpint(ain
->type
, ==, QTYPE_QINT
);
626 g_assert_cmpint(ain
->u
.i
, ==, 42);
627 qapi_free_AltIntNum(ain
);
629 v
= visitor_input_test_init(data
, "42");
630 visit_type_AltNumInt(v
, NULL
, &ani
, &error_abort
);
631 g_assert_cmpint(ani
->type
, ==, QTYPE_QINT
);
632 g_assert_cmpint(ani
->u
.i
, ==, 42);
633 qapi_free_AltNumInt(ani
);
635 /* Parsing a double */
637 v
= visitor_input_test_init(data
, "42.5");
638 visit_type_AltEnumBool(v
, NULL
, &aeb
, &err
);
639 error_free_or_abort(&err
);
640 qapi_free_AltEnumBool(aeb
);
642 v
= visitor_input_test_init(data
, "42.5");
643 visit_type_AltEnumNum(v
, NULL
, &aen
, &error_abort
);
644 g_assert_cmpint(aen
->type
, ==, QTYPE_QFLOAT
);
645 g_assert_cmpfloat(aen
->u
.n
, ==, 42.5);
646 qapi_free_AltEnumNum(aen
);
648 v
= visitor_input_test_init(data
, "42.5");
649 visit_type_AltNumEnum(v
, NULL
, &ans
, &error_abort
);
650 g_assert_cmpint(ans
->type
, ==, QTYPE_QFLOAT
);
651 g_assert_cmpfloat(ans
->u
.n
, ==, 42.5);
652 qapi_free_AltNumEnum(ans
);
654 v
= visitor_input_test_init(data
, "42.5");
655 visit_type_AltEnumInt(v
, NULL
, &asi
, &err
);
656 error_free_or_abort(&err
);
657 qapi_free_AltEnumInt(asi
);
659 v
= visitor_input_test_init(data
, "42.5");
660 visit_type_AltIntNum(v
, NULL
, &ain
, &error_abort
);
661 g_assert_cmpint(ain
->type
, ==, QTYPE_QFLOAT
);
662 g_assert_cmpfloat(ain
->u
.n
, ==, 42.5);
663 qapi_free_AltIntNum(ain
);
665 v
= visitor_input_test_init(data
, "42.5");
666 visit_type_AltNumInt(v
, NULL
, &ani
, &error_abort
);
667 g_assert_cmpint(ani
->type
, ==, QTYPE_QFLOAT
);
668 g_assert_cmpfloat(ani
->u
.n
, ==, 42.5);
669 qapi_free_AltNumInt(ani
);
672 static void test_native_list_integer_helper(TestInputVisitorData
*data
,
674 UserDefNativeListUnionKind kind
)
676 UserDefNativeListUnion
*cvalue
= NULL
;
678 GString
*gstr_list
= g_string_new("");
679 GString
*gstr_union
= g_string_new("");
682 for (i
= 0; i
< 32; i
++) {
683 g_string_append_printf(gstr_list
, "%d", i
);
685 g_string_append(gstr_list
, ", ");
688 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
689 UserDefNativeListUnionKind_lookup
[kind
],
691 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
693 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
694 g_assert(cvalue
!= NULL
);
695 g_assert_cmpint(cvalue
->type
, ==, kind
);
698 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
: {
699 intList
*elem
= NULL
;
700 for (i
= 0, elem
= cvalue
->u
.integer
.data
;
701 elem
; elem
= elem
->next
, i
++) {
702 g_assert_cmpint(elem
->value
, ==, i
);
706 case USER_DEF_NATIVE_LIST_UNION_KIND_S8
: {
707 int8List
*elem
= NULL
;
708 for (i
= 0, elem
= cvalue
->u
.s8
.data
; elem
; elem
= elem
->next
, i
++) {
709 g_assert_cmpint(elem
->value
, ==, i
);
713 case USER_DEF_NATIVE_LIST_UNION_KIND_S16
: {
714 int16List
*elem
= NULL
;
715 for (i
= 0, elem
= cvalue
->u
.s16
.data
; elem
; elem
= elem
->next
, i
++) {
716 g_assert_cmpint(elem
->value
, ==, i
);
720 case USER_DEF_NATIVE_LIST_UNION_KIND_S32
: {
721 int32List
*elem
= NULL
;
722 for (i
= 0, elem
= cvalue
->u
.s32
.data
; elem
; elem
= elem
->next
, i
++) {
723 g_assert_cmpint(elem
->value
, ==, i
);
727 case USER_DEF_NATIVE_LIST_UNION_KIND_S64
: {
728 int64List
*elem
= NULL
;
729 for (i
= 0, elem
= cvalue
->u
.s64
.data
; elem
; elem
= elem
->next
, i
++) {
730 g_assert_cmpint(elem
->value
, ==, i
);
734 case USER_DEF_NATIVE_LIST_UNION_KIND_U8
: {
735 uint8List
*elem
= NULL
;
736 for (i
= 0, elem
= cvalue
->u
.u8
.data
; elem
; elem
= elem
->next
, i
++) {
737 g_assert_cmpint(elem
->value
, ==, i
);
741 case USER_DEF_NATIVE_LIST_UNION_KIND_U16
: {
742 uint16List
*elem
= NULL
;
743 for (i
= 0, elem
= cvalue
->u
.u16
.data
; elem
; elem
= elem
->next
, i
++) {
744 g_assert_cmpint(elem
->value
, ==, i
);
748 case USER_DEF_NATIVE_LIST_UNION_KIND_U32
: {
749 uint32List
*elem
= NULL
;
750 for (i
= 0, elem
= cvalue
->u
.u32
.data
; elem
; elem
= elem
->next
, i
++) {
751 g_assert_cmpint(elem
->value
, ==, i
);
755 case USER_DEF_NATIVE_LIST_UNION_KIND_U64
: {
756 uint64List
*elem
= NULL
;
757 for (i
= 0, elem
= cvalue
->u
.u64
.data
; elem
; elem
= elem
->next
, i
++) {
758 g_assert_cmpint(elem
->value
, ==, i
);
763 g_assert_not_reached();
766 g_string_free(gstr_union
, true);
767 g_string_free(gstr_list
, true);
768 qapi_free_UserDefNativeListUnion(cvalue
);
771 static void test_visitor_in_native_list_int(TestInputVisitorData
*data
,
774 test_native_list_integer_helper(data
, unused
,
775 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
);
778 static void test_visitor_in_native_list_int8(TestInputVisitorData
*data
,
781 test_native_list_integer_helper(data
, unused
,
782 USER_DEF_NATIVE_LIST_UNION_KIND_S8
);
785 static void test_visitor_in_native_list_int16(TestInputVisitorData
*data
,
788 test_native_list_integer_helper(data
, unused
,
789 USER_DEF_NATIVE_LIST_UNION_KIND_S16
);
792 static void test_visitor_in_native_list_int32(TestInputVisitorData
*data
,
795 test_native_list_integer_helper(data
, unused
,
796 USER_DEF_NATIVE_LIST_UNION_KIND_S32
);
799 static void test_visitor_in_native_list_int64(TestInputVisitorData
*data
,
802 test_native_list_integer_helper(data
, unused
,
803 USER_DEF_NATIVE_LIST_UNION_KIND_S64
);
806 static void test_visitor_in_native_list_uint8(TestInputVisitorData
*data
,
809 test_native_list_integer_helper(data
, unused
,
810 USER_DEF_NATIVE_LIST_UNION_KIND_U8
);
813 static void test_visitor_in_native_list_uint16(TestInputVisitorData
*data
,
816 test_native_list_integer_helper(data
, unused
,
817 USER_DEF_NATIVE_LIST_UNION_KIND_U16
);
820 static void test_visitor_in_native_list_uint32(TestInputVisitorData
*data
,
823 test_native_list_integer_helper(data
, unused
,
824 USER_DEF_NATIVE_LIST_UNION_KIND_U32
);
827 static void test_visitor_in_native_list_uint64(TestInputVisitorData
*data
,
830 test_native_list_integer_helper(data
, unused
,
831 USER_DEF_NATIVE_LIST_UNION_KIND_U64
);
834 static void test_visitor_in_native_list_bool(TestInputVisitorData
*data
,
837 UserDefNativeListUnion
*cvalue
= NULL
;
838 boolList
*elem
= NULL
;
840 GString
*gstr_list
= g_string_new("");
841 GString
*gstr_union
= g_string_new("");
844 for (i
= 0; i
< 32; i
++) {
845 g_string_append_printf(gstr_list
, "%s",
846 (i
% 3 == 0) ? "true" : "false");
848 g_string_append(gstr_list
, ", ");
851 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
853 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
855 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
856 g_assert(cvalue
!= NULL
);
857 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN
);
859 for (i
= 0, elem
= cvalue
->u
.boolean
.data
; elem
; elem
= elem
->next
, i
++) {
860 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
863 g_string_free(gstr_union
, true);
864 g_string_free(gstr_list
, true);
865 qapi_free_UserDefNativeListUnion(cvalue
);
868 static void test_visitor_in_native_list_string(TestInputVisitorData
*data
,
871 UserDefNativeListUnion
*cvalue
= NULL
;
872 strList
*elem
= NULL
;
874 GString
*gstr_list
= g_string_new("");
875 GString
*gstr_union
= g_string_new("");
878 for (i
= 0; i
< 32; i
++) {
879 g_string_append_printf(gstr_list
, "'%d'", i
);
881 g_string_append(gstr_list
, ", ");
884 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
886 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
888 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
889 g_assert(cvalue
!= NULL
);
890 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING
);
892 for (i
= 0, elem
= cvalue
->u
.string
.data
; elem
; elem
= elem
->next
, i
++) {
894 sprintf(str
, "%d", i
);
895 g_assert_cmpstr(elem
->value
, ==, str
);
898 g_string_free(gstr_union
, true);
899 g_string_free(gstr_list
, true);
900 qapi_free_UserDefNativeListUnion(cvalue
);
903 #define DOUBLE_STR_MAX 16
905 static void test_visitor_in_native_list_number(TestInputVisitorData
*data
,
908 UserDefNativeListUnion
*cvalue
= NULL
;
909 numberList
*elem
= NULL
;
911 GString
*gstr_list
= g_string_new("");
912 GString
*gstr_union
= g_string_new("");
915 for (i
= 0; i
< 32; i
++) {
916 g_string_append_printf(gstr_list
, "%f", (double)i
/ 3);
918 g_string_append(gstr_list
, ", ");
921 g_string_append_printf(gstr_union
, "{ 'type': 'number', 'data': [ %s ] }",
923 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
925 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
926 g_assert(cvalue
!= NULL
);
927 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER
);
929 for (i
= 0, elem
= cvalue
->u
.number
.data
; elem
; elem
= elem
->next
, i
++) {
930 GString
*double_expected
= g_string_new("");
931 GString
*double_actual
= g_string_new("");
933 g_string_printf(double_expected
, "%.6f", (double)i
/ 3);
934 g_string_printf(double_actual
, "%.6f", elem
->value
);
935 g_assert_cmpstr(double_expected
->str
, ==, double_actual
->str
);
937 g_string_free(double_expected
, true);
938 g_string_free(double_actual
, true);
941 g_string_free(gstr_union
, true);
942 g_string_free(gstr_list
, true);
943 qapi_free_UserDefNativeListUnion(cvalue
);
946 static void input_visitor_test_add(const char *testpath
,
947 const void *user_data
,
948 void (*test_func
)(TestInputVisitorData
*data
,
949 const void *user_data
))
951 g_test_add(testpath
, TestInputVisitorData
, user_data
, NULL
, test_func
,
952 visitor_input_teardown
);
955 static void test_visitor_in_errors(TestInputVisitorData
*data
,
958 TestStruct
*p
= NULL
;
962 UserDefTwo
*r
= NULL
;
963 WrapAlternate
*s
= NULL
;
965 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', "
968 visit_type_TestStruct(v
, NULL
, &p
, &err
);
969 error_free_or_abort(&err
);
972 v
= visitor_input_test_init(data
, "[ '1', '2', false, '3' ]");
973 visit_type_strList(v
, NULL
, &q
, &err
);
974 error_free_or_abort(&err
);
977 v
= visitor_input_test_init(data
, "{ 'str':'hi' }");
978 visit_type_UserDefTwo(v
, NULL
, &r
, &err
);
979 error_free_or_abort(&err
);
982 v
= visitor_input_test_init(data
, "{ }");
983 visit_type_WrapAlternate(v
, NULL
, &s
, &err
);
984 error_free_or_abort(&err
);
988 static void test_visitor_in_wrong_type(TestInputVisitorData
*data
,
991 TestStruct
*p
= NULL
;
997 /* Make sure arrays and structs cannot be confused */
999 v
= visitor_input_test_init(data
, "[]");
1000 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1001 error_free_or_abort(&err
);
1004 v
= visitor_input_test_init(data
, "{}");
1005 visit_type_strList(v
, NULL
, &q
, &err
);
1006 error_free_or_abort(&err
);
1009 /* Make sure primitives and struct cannot be confused */
1011 v
= visitor_input_test_init(data
, "1");
1012 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1013 error_free_or_abort(&err
);
1016 v
= visitor_input_test_init(data
, "{}");
1017 visit_type_int(v
, NULL
, &i
, &err
);
1018 error_free_or_abort(&err
);
1020 /* Make sure primitives and arrays cannot be confused */
1022 v
= visitor_input_test_init(data
, "1");
1023 visit_type_strList(v
, NULL
, &q
, &err
);
1024 error_free_or_abort(&err
);
1027 v
= visitor_input_test_init(data
, "[]");
1028 visit_type_int(v
, NULL
, &i
, &err
);
1029 error_free_or_abort(&err
);
1032 static void test_visitor_in_fail_struct(TestInputVisitorData
*data
,
1035 TestStruct
*p
= NULL
;
1039 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
1041 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1042 error_free_or_abort(&err
);
1046 static void test_visitor_in_fail_struct_nested(TestInputVisitorData
*data
,
1049 UserDefTwo
*udp
= NULL
;
1053 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
1055 visit_type_UserDefTwo(v
, NULL
, &udp
, &err
);
1056 error_free_or_abort(&err
);
1060 static void test_visitor_in_fail_struct_in_list(TestInputVisitorData
*data
,
1063 UserDefOneList
*head
= NULL
;
1067 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
1069 visit_type_UserDefOneList(v
, NULL
, &head
, &err
);
1070 error_free_or_abort(&err
);
1074 static void test_visitor_in_fail_struct_missing(TestInputVisitorData
*data
,
1080 GenericAlternate
*alt
;
1089 v
= visitor_input_test_init(data
, "{ 'sub': [ {} ] }");
1090 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
1091 visit_start_struct(v
, "struct", NULL
, 0, &err
);
1092 error_free_or_abort(&err
);
1093 visit_start_list(v
, "list", NULL
, 0, &err
);
1094 error_free_or_abort(&err
);
1095 visit_start_alternate(v
, "alternate", &alt
, sizeof(*alt
), false, &err
);
1096 error_free_or_abort(&err
);
1097 visit_optional(v
, "optional", &present
);
1099 visit_type_enum(v
, "enum", &en
, EnumOne_lookup
, &err
);
1100 error_free_or_abort(&err
);
1101 visit_type_int(v
, "i64", &i64
, &err
);
1102 error_free_or_abort(&err
);
1103 visit_type_uint32(v
, "u32", &u32
, &err
);
1104 error_free_or_abort(&err
);
1105 visit_type_int8(v
, "i8", &i8
, &err
);
1106 error_free_or_abort(&err
);
1107 visit_type_str(v
, "i8", &str
, &err
);
1108 error_free_or_abort(&err
);
1109 visit_type_number(v
, "dbl", &dbl
, &err
);
1110 error_free_or_abort(&err
);
1111 visit_type_any(v
, "any", &any
, &err
);
1112 error_free_or_abort(&err
);
1113 visit_type_null(v
, "null", &err
);
1114 error_free_or_abort(&err
);
1115 visit_start_list(v
, "sub", NULL
, 0, &error_abort
);
1116 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
1117 visit_type_int(v
, "i64", &i64
, &err
);
1118 error_free_or_abort(&err
);
1119 visit_end_struct(v
, NULL
);
1120 visit_end_list(v
, NULL
);
1121 visit_end_struct(v
, NULL
);
1124 static void test_visitor_in_fail_list(TestInputVisitorData
*data
,
1131 /* Unvisited list tail */
1133 v
= visitor_input_test_init(data
, "[ 1, 2, 3 ]");
1135 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1136 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1137 g_assert_cmpint(i64
, ==, 1);
1138 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1139 g_assert_cmpint(i64
, ==, 2);
1140 visit_check_list(v
, &err
);
1141 error_free_or_abort(&err
);
1142 visit_end_list(v
, NULL
);
1144 /* Visit beyond end of list */
1145 v
= visitor_input_test_init(data
, "[]");
1147 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1148 visit_type_int(v
, NULL
, &i64
, &err
);
1149 error_free_or_abort(&err
);
1150 visit_end_list(v
, NULL
);
1153 static void test_visitor_in_fail_list_nested(TestInputVisitorData
*data
,
1160 /* Unvisited nested list tail */
1162 v
= visitor_input_test_init(data
, "[ 0, [ 1, 2, 3 ] ]");
1164 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1165 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1166 g_assert_cmpint(i64
, ==, 0);
1167 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1168 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1169 g_assert_cmpint(i64
, ==, 1);
1170 visit_check_list(v
, &err
);
1171 error_free_or_abort(&err
);
1172 visit_end_list(v
, NULL
);
1173 visit_check_list(v
, &error_abort
);
1174 visit_end_list(v
, NULL
);
1177 static void test_visitor_in_fail_union_native_list(TestInputVisitorData
*data
,
1180 UserDefNativeListUnion
*tmp
= NULL
;
1184 v
= visitor_input_test_init(data
,
1185 "{ 'type': 'integer', 'data' : [ 'string' ] }");
1187 visit_type_UserDefNativeListUnion(v
, NULL
, &tmp
, &err
);
1188 error_free_or_abort(&err
);
1192 static void test_visitor_in_fail_union_flat(TestInputVisitorData
*data
,
1195 UserDefFlatUnion
*tmp
= NULL
;
1199 v
= visitor_input_test_init(data
, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
1201 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &err
);
1202 error_free_or_abort(&err
);
1206 static void test_visitor_in_fail_union_flat_no_discrim(TestInputVisitorData
*data
,
1209 UserDefFlatUnion2
*tmp
= NULL
;
1213 /* test situation where discriminator field ('enum1' here) is missing */
1214 v
= visitor_input_test_init(data
, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
1216 visit_type_UserDefFlatUnion2(v
, NULL
, &tmp
, &err
);
1217 error_free_or_abort(&err
);
1221 static void test_visitor_in_fail_alternate(TestInputVisitorData
*data
,
1224 UserDefAlternate
*tmp
;
1228 v
= visitor_input_test_init(data
, "3.14");
1230 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
1231 error_free_or_abort(&err
);
1235 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1236 const char *schema_json
)
1238 SchemaInfoList
*schema
= NULL
;
1241 v
= visitor_input_test_init_raw(data
, schema_json
);
1243 visit_type_SchemaInfoList(v
, NULL
, &schema
, &error_abort
);
1246 qapi_free_SchemaInfoList(schema
);
1249 static void test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1252 do_test_visitor_in_qmp_introspect(data
, test_qmp_schema_json
);
1253 do_test_visitor_in_qmp_introspect(data
, qmp_schema_json
);
1256 int main(int argc
, char **argv
)
1258 g_test_init(&argc
, &argv
, NULL
);
1260 input_visitor_test_add("/visitor/input/int",
1261 NULL
, test_visitor_in_int
);
1262 input_visitor_test_add("/visitor/input/uint",
1263 NULL
, test_visitor_in_uint
);
1264 input_visitor_test_add("/visitor/input/int_overflow",
1265 NULL
, test_visitor_in_int_overflow
);
1266 input_visitor_test_add("/visitor/input/int_keyval",
1267 NULL
, test_visitor_in_int_keyval
);
1268 input_visitor_test_add("/visitor/input/int_str_keyval",
1269 NULL
, test_visitor_in_int_str_keyval
);
1270 input_visitor_test_add("/visitor/input/int_str_fail",
1271 NULL
, test_visitor_in_int_str_fail
);
1272 input_visitor_test_add("/visitor/input/bool",
1273 NULL
, test_visitor_in_bool
);
1274 input_visitor_test_add("/visitor/input/bool_keyval",
1275 NULL
, test_visitor_in_bool_keyval
);
1276 input_visitor_test_add("/visitor/input/bool_str_keyval",
1277 NULL
, test_visitor_in_bool_str_keyval
);
1278 input_visitor_test_add("/visitor/input/bool_str_fail",
1279 NULL
, test_visitor_in_bool_str_fail
);
1280 input_visitor_test_add("/visitor/input/number",
1281 NULL
, test_visitor_in_number
);
1282 input_visitor_test_add("/visitor/input/number_keyval",
1283 NULL
, test_visitor_in_number_keyval
);
1284 input_visitor_test_add("/visitor/input/number_str_keyval",
1285 NULL
, test_visitor_in_number_str_keyval
);
1286 input_visitor_test_add("/visitor/input/number_str_fail",
1287 NULL
, test_visitor_in_number_str_fail
);
1288 input_visitor_test_add("/visitor/input/size_str_keyval",
1289 NULL
, test_visitor_in_size_str_keyval
);
1290 input_visitor_test_add("/visitor/input/size_str_fail",
1291 NULL
, test_visitor_in_size_str_fail
);
1292 input_visitor_test_add("/visitor/input/string",
1293 NULL
, test_visitor_in_string
);
1294 input_visitor_test_add("/visitor/input/enum",
1295 NULL
, test_visitor_in_enum
);
1296 input_visitor_test_add("/visitor/input/struct",
1297 NULL
, test_visitor_in_struct
);
1298 input_visitor_test_add("/visitor/input/struct-nested",
1299 NULL
, test_visitor_in_struct_nested
);
1300 input_visitor_test_add("/visitor/input/list",
1301 NULL
, test_visitor_in_list
);
1302 input_visitor_test_add("/visitor/input/any",
1303 NULL
, test_visitor_in_any
);
1304 input_visitor_test_add("/visitor/input/null",
1305 NULL
, test_visitor_in_null
);
1306 input_visitor_test_add("/visitor/input/union-flat",
1307 NULL
, test_visitor_in_union_flat
);
1308 input_visitor_test_add("/visitor/input/alternate",
1309 NULL
, test_visitor_in_alternate
);
1310 input_visitor_test_add("/visitor/input/errors",
1311 NULL
, test_visitor_in_errors
);
1312 input_visitor_test_add("/visitor/input/wrong-type",
1313 NULL
, test_visitor_in_wrong_type
);
1314 input_visitor_test_add("/visitor/input/alternate-number",
1315 NULL
, test_visitor_in_alternate_number
);
1316 input_visitor_test_add("/visitor/input/native_list/int",
1317 NULL
, test_visitor_in_native_list_int
);
1318 input_visitor_test_add("/visitor/input/native_list/int8",
1319 NULL
, test_visitor_in_native_list_int8
);
1320 input_visitor_test_add("/visitor/input/native_list/int16",
1321 NULL
, test_visitor_in_native_list_int16
);
1322 input_visitor_test_add("/visitor/input/native_list/int32",
1323 NULL
, test_visitor_in_native_list_int32
);
1324 input_visitor_test_add("/visitor/input/native_list/int64",
1325 NULL
, test_visitor_in_native_list_int64
);
1326 input_visitor_test_add("/visitor/input/native_list/uint8",
1327 NULL
, test_visitor_in_native_list_uint8
);
1328 input_visitor_test_add("/visitor/input/native_list/uint16",
1329 NULL
, test_visitor_in_native_list_uint16
);
1330 input_visitor_test_add("/visitor/input/native_list/uint32",
1331 NULL
, test_visitor_in_native_list_uint32
);
1332 input_visitor_test_add("/visitor/input/native_list/uint64",
1333 NULL
, test_visitor_in_native_list_uint64
);
1334 input_visitor_test_add("/visitor/input/native_list/bool",
1335 NULL
, test_visitor_in_native_list_bool
);
1336 input_visitor_test_add("/visitor/input/native_list/str",
1337 NULL
, test_visitor_in_native_list_string
);
1338 input_visitor_test_add("/visitor/input/native_list/number",
1339 NULL
, test_visitor_in_native_list_number
);
1340 input_visitor_test_add("/visitor/input/fail/struct",
1341 NULL
, test_visitor_in_fail_struct
);
1342 input_visitor_test_add("/visitor/input/fail/struct-nested",
1343 NULL
, test_visitor_in_fail_struct_nested
);
1344 input_visitor_test_add("/visitor/input/fail/struct-in-list",
1345 NULL
, test_visitor_in_fail_struct_in_list
);
1346 input_visitor_test_add("/visitor/input/fail/struct-missing",
1347 NULL
, test_visitor_in_fail_struct_missing
);
1348 input_visitor_test_add("/visitor/input/fail/list",
1349 NULL
, test_visitor_in_fail_list
);
1350 input_visitor_test_add("/visitor/input/fail/list-nested",
1351 NULL
, test_visitor_in_fail_list_nested
);
1352 input_visitor_test_add("/visitor/input/fail/union-flat",
1353 NULL
, test_visitor_in_fail_union_flat
);
1354 input_visitor_test_add("/visitor/input/fail/union-flat-no-discriminator",
1355 NULL
, test_visitor_in_fail_union_flat_no_discrim
);
1356 input_visitor_test_add("/visitor/input/fail/alternate",
1357 NULL
, test_visitor_in_fail_alternate
);
1358 input_visitor_test_add("/visitor/input/fail/union-native-list",
1359 NULL
, test_visitor_in_fail_union_native_list
);
1360 input_visitor_test_add("/visitor/input/qmp-introspect",
1361 NULL
, test_visitor_in_qmp_introspect
);