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;
282 v
= visitor_input_test_init_full(data
, true, "\"3.14\"");
284 visit_type_number(v
, NULL
, &res
, &error_abort
);
285 g_assert_cmpfloat(res
, ==, value
);
288 static void test_visitor_in_number_str_fail(TestInputVisitorData
*data
,
295 v
= visitor_input_test_init(data
, "\"3.14\"");
297 visit_type_number(v
, NULL
, &res
, &err
);
298 error_free_or_abort(&err
);
301 static void test_visitor_in_size_str_keyval(TestInputVisitorData
*data
,
304 uint64_t res
, value
= 500 * 1024 * 1024;
307 v
= visitor_input_test_init_full(data
, true, "\"500M\"");
309 visit_type_size(v
, NULL
, &res
, &error_abort
);
310 g_assert_cmpfloat(res
, ==, value
);
313 static void test_visitor_in_size_str_fail(TestInputVisitorData
*data
,
320 v
= visitor_input_test_init(data
, "\"500M\"");
322 visit_type_size(v
, NULL
, &res
, &err
);
323 error_free_or_abort(&err
);
326 static void test_visitor_in_string(TestInputVisitorData
*data
,
329 char *res
= NULL
, *value
= (char *) "Q E M U";
332 v
= visitor_input_test_init(data
, "%s", value
);
334 visit_type_str(v
, NULL
, &res
, &error_abort
);
335 g_assert_cmpstr(res
, ==, value
);
340 static void test_visitor_in_enum(TestInputVisitorData
*data
,
346 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
349 v
= visitor_input_test_init(data
, "%s", EnumOne_lookup
[i
]);
351 visit_type_EnumOne(v
, NULL
, &res
, &error_abort
);
352 g_assert_cmpint(i
, ==, res
);
357 static void test_visitor_in_struct(TestInputVisitorData
*data
,
360 TestStruct
*p
= NULL
;
363 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
365 visit_type_TestStruct(v
, NULL
, &p
, &error_abort
);
366 g_assert_cmpint(p
->integer
, ==, -42);
367 g_assert(p
->boolean
== true);
368 g_assert_cmpstr(p
->string
, ==, "foo");
374 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
377 UserDefTwo
*udp
= NULL
;
380 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', "
381 "'dict1': { 'string1': 'string1', "
382 "'dict2': { 'userdef': { 'integer': 42, "
383 "'string': 'string' }, 'string': 'string2'}}}");
385 visit_type_UserDefTwo(v
, NULL
, &udp
, &error_abort
);
387 g_assert_cmpstr(udp
->string0
, ==, "string0");
388 g_assert_cmpstr(udp
->dict1
->string1
, ==, "string1");
389 g_assert_cmpint(udp
->dict1
->dict2
->userdef
->integer
, ==, 42);
390 g_assert_cmpstr(udp
->dict1
->dict2
->userdef
->string
, ==, "string");
391 g_assert_cmpstr(udp
->dict1
->dict2
->string
, ==, "string2");
392 g_assert(udp
->dict1
->has_dict3
== false);
394 qapi_free_UserDefTwo(udp
);
397 static void test_visitor_in_list(TestInputVisitorData
*data
,
400 UserDefOneList
*item
, *head
= NULL
;
404 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
406 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
407 g_assert(head
!= NULL
);
409 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
412 snprintf(string
, sizeof(string
), "string%d", i
);
413 g_assert_cmpstr(item
->value
->string
, ==, string
);
414 g_assert_cmpint(item
->value
->integer
, ==, 42 + i
);
417 qapi_free_UserDefOneList(head
);
420 /* An empty list is valid */
421 v
= visitor_input_test_init(data
, "[]");
422 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
426 static void test_visitor_in_any(TestInputVisitorData
*data
,
437 v
= visitor_input_test_init(data
, "-42");
438 visit_type_any(v
, NULL
, &res
, &error_abort
);
439 qint
= qobject_to_qint(res
);
441 g_assert_cmpint(qint_get_int(qint
), ==, -42);
444 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
445 visit_type_any(v
, NULL
, &res
, &error_abort
);
446 qdict
= qobject_to_qdict(res
);
447 g_assert(qdict
&& qdict_size(qdict
) == 3);
448 qobj
= qdict_get(qdict
, "integer");
450 qint
= qobject_to_qint(qobj
);
452 g_assert_cmpint(qint_get_int(qint
), ==, -42);
453 qobj
= qdict_get(qdict
, "boolean");
455 qbool
= qobject_to_qbool(qobj
);
457 g_assert(qbool_get_bool(qbool
) == true);
458 qobj
= qdict_get(qdict
, "string");
460 qstring
= qobject_to_qstring(qobj
);
462 g_assert_cmpstr(qstring_get_str(qstring
), ==, "foo");
466 static void test_visitor_in_null(TestInputVisitorData
*data
,
474 * FIXME: Since QAPI doesn't know the 'null' type yet, we can't
475 * test visit_type_null() by reading into a QAPI struct then
476 * checking that it was populated correctly. The best we can do
477 * for now is ensure that we consumed null from the input, proven
478 * by the fact that we can't re-read the key; and that we detect
479 * when input is not null.
482 v
= visitor_input_test_init_full(data
, false,
483 "{ 'a': null, 'b': '' }");
484 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
485 visit_type_null(v
, "a", &error_abort
);
486 visit_type_null(v
, "b", &err
);
487 error_free_or_abort(&err
);
488 visit_type_str(v
, "c", &tmp
, &err
);
490 error_free_or_abort(&err
);
491 visit_check_struct(v
, &error_abort
);
492 visit_end_struct(v
, NULL
);
495 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
499 UserDefFlatUnion
*tmp
;
500 UserDefUnionBase
*base
;
502 v
= visitor_input_test_init(data
,
503 "{ 'enum1': 'value1', "
506 "'boolean': true }");
508 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &error_abort
);
509 g_assert_cmpint(tmp
->enum1
, ==, ENUM_ONE_VALUE1
);
510 g_assert_cmpstr(tmp
->string
, ==, "str");
511 g_assert_cmpint(tmp
->integer
, ==, 41);
512 g_assert_cmpint(tmp
->u
.value1
.boolean
, ==, true);
514 base
= qapi_UserDefFlatUnion_base(tmp
);
515 g_assert(&base
->enum1
== &tmp
->enum1
);
517 qapi_free_UserDefFlatUnion(tmp
);
520 static void test_visitor_in_alternate(TestInputVisitorData
*data
,
525 UserDefAlternate
*tmp
;
528 v
= visitor_input_test_init(data
, "42");
529 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
530 g_assert_cmpint(tmp
->type
, ==, QTYPE_QINT
);
531 g_assert_cmpint(tmp
->u
.i
, ==, 42);
532 qapi_free_UserDefAlternate(tmp
);
534 v
= visitor_input_test_init(data
, "'string'");
535 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
536 g_assert_cmpint(tmp
->type
, ==, QTYPE_QSTRING
);
537 g_assert_cmpstr(tmp
->u
.s
, ==, "string");
538 qapi_free_UserDefAlternate(tmp
);
540 v
= visitor_input_test_init(data
, "{'integer':1, 'string':'str', "
541 "'enum1':'value1', 'boolean':true}");
542 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
543 g_assert_cmpint(tmp
->type
, ==, QTYPE_QDICT
);
544 g_assert_cmpint(tmp
->u
.udfu
.integer
, ==, 1);
545 g_assert_cmpstr(tmp
->u
.udfu
.string
, ==, "str");
546 g_assert_cmpint(tmp
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
547 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.boolean
, ==, true);
548 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
549 qapi_free_UserDefAlternate(tmp
);
551 v
= visitor_input_test_init(data
, "false");
552 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
553 error_free_or_abort(&err
);
554 qapi_free_UserDefAlternate(tmp
);
556 v
= visitor_input_test_init(data
, "{ 'alt': 42 }");
557 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
558 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QINT
);
559 g_assert_cmpint(wrap
->alt
->u
.i
, ==, 42);
560 qapi_free_WrapAlternate(wrap
);
562 v
= visitor_input_test_init(data
, "{ 'alt': 'string' }");
563 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
564 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QSTRING
);
565 g_assert_cmpstr(wrap
->alt
->u
.s
, ==, "string");
566 qapi_free_WrapAlternate(wrap
);
568 v
= visitor_input_test_init(data
, "{ 'alt': {'integer':1, 'string':'str', "
569 "'enum1':'value1', 'boolean':true} }");
570 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
571 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QDICT
);
572 g_assert_cmpint(wrap
->alt
->u
.udfu
.integer
, ==, 1);
573 g_assert_cmpstr(wrap
->alt
->u
.udfu
.string
, ==, "str");
574 g_assert_cmpint(wrap
->alt
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
575 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.boolean
, ==, true);
576 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
577 qapi_free_WrapAlternate(wrap
);
580 static void test_visitor_in_alternate_number(TestInputVisitorData
*data
,
594 v
= visitor_input_test_init(data
, "42");
595 visit_type_AltStrBool(v
, NULL
, &asb
, &err
);
596 error_free_or_abort(&err
);
597 qapi_free_AltStrBool(asb
);
599 v
= visitor_input_test_init(data
, "42");
600 visit_type_AltStrNum(v
, NULL
, &asn
, &error_abort
);
601 g_assert_cmpint(asn
->type
, ==, QTYPE_QFLOAT
);
602 g_assert_cmpfloat(asn
->u
.n
, ==, 42);
603 qapi_free_AltStrNum(asn
);
605 v
= visitor_input_test_init(data
, "42");
606 visit_type_AltNumStr(v
, NULL
, &ans
, &error_abort
);
607 g_assert_cmpint(ans
->type
, ==, QTYPE_QFLOAT
);
608 g_assert_cmpfloat(ans
->u
.n
, ==, 42);
609 qapi_free_AltNumStr(ans
);
611 v
= visitor_input_test_init(data
, "42");
612 visit_type_AltStrInt(v
, NULL
, &asi
, &error_abort
);
613 g_assert_cmpint(asi
->type
, ==, QTYPE_QINT
);
614 g_assert_cmpint(asi
->u
.i
, ==, 42);
615 qapi_free_AltStrInt(asi
);
617 v
= visitor_input_test_init(data
, "42");
618 visit_type_AltIntNum(v
, NULL
, &ain
, &error_abort
);
619 g_assert_cmpint(ain
->type
, ==, QTYPE_QINT
);
620 g_assert_cmpint(ain
->u
.i
, ==, 42);
621 qapi_free_AltIntNum(ain
);
623 v
= visitor_input_test_init(data
, "42");
624 visit_type_AltNumInt(v
, NULL
, &ani
, &error_abort
);
625 g_assert_cmpint(ani
->type
, ==, QTYPE_QINT
);
626 g_assert_cmpint(ani
->u
.i
, ==, 42);
627 qapi_free_AltNumInt(ani
);
629 /* Parsing a double */
631 v
= visitor_input_test_init(data
, "42.5");
632 visit_type_AltStrBool(v
, NULL
, &asb
, &err
);
633 error_free_or_abort(&err
);
634 qapi_free_AltStrBool(asb
);
636 v
= visitor_input_test_init(data
, "42.5");
637 visit_type_AltStrNum(v
, NULL
, &asn
, &error_abort
);
638 g_assert_cmpint(asn
->type
, ==, QTYPE_QFLOAT
);
639 g_assert_cmpfloat(asn
->u
.n
, ==, 42.5);
640 qapi_free_AltStrNum(asn
);
642 v
= visitor_input_test_init(data
, "42.5");
643 visit_type_AltNumStr(v
, NULL
, &ans
, &error_abort
);
644 g_assert_cmpint(ans
->type
, ==, QTYPE_QFLOAT
);
645 g_assert_cmpfloat(ans
->u
.n
, ==, 42.5);
646 qapi_free_AltNumStr(ans
);
648 v
= visitor_input_test_init(data
, "42.5");
649 visit_type_AltStrInt(v
, NULL
, &asi
, &err
);
650 error_free_or_abort(&err
);
651 qapi_free_AltStrInt(asi
);
653 v
= visitor_input_test_init(data
, "42.5");
654 visit_type_AltIntNum(v
, NULL
, &ain
, &error_abort
);
655 g_assert_cmpint(ain
->type
, ==, QTYPE_QFLOAT
);
656 g_assert_cmpfloat(ain
->u
.n
, ==, 42.5);
657 qapi_free_AltIntNum(ain
);
659 v
= visitor_input_test_init(data
, "42.5");
660 visit_type_AltNumInt(v
, NULL
, &ani
, &error_abort
);
661 g_assert_cmpint(ani
->type
, ==, QTYPE_QFLOAT
);
662 g_assert_cmpfloat(ani
->u
.n
, ==, 42.5);
663 qapi_free_AltNumInt(ani
);
666 static void test_native_list_integer_helper(TestInputVisitorData
*data
,
668 UserDefNativeListUnionKind kind
)
670 UserDefNativeListUnion
*cvalue
= NULL
;
672 GString
*gstr_list
= g_string_new("");
673 GString
*gstr_union
= g_string_new("");
676 for (i
= 0; i
< 32; i
++) {
677 g_string_append_printf(gstr_list
, "%d", i
);
679 g_string_append(gstr_list
, ", ");
682 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
683 UserDefNativeListUnionKind_lookup
[kind
],
685 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
687 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
688 g_assert(cvalue
!= NULL
);
689 g_assert_cmpint(cvalue
->type
, ==, kind
);
692 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
: {
693 intList
*elem
= NULL
;
694 for (i
= 0, elem
= cvalue
->u
.integer
.data
;
695 elem
; elem
= elem
->next
, i
++) {
696 g_assert_cmpint(elem
->value
, ==, i
);
700 case USER_DEF_NATIVE_LIST_UNION_KIND_S8
: {
701 int8List
*elem
= NULL
;
702 for (i
= 0, elem
= cvalue
->u
.s8
.data
; elem
; elem
= elem
->next
, i
++) {
703 g_assert_cmpint(elem
->value
, ==, i
);
707 case USER_DEF_NATIVE_LIST_UNION_KIND_S16
: {
708 int16List
*elem
= NULL
;
709 for (i
= 0, elem
= cvalue
->u
.s16
.data
; elem
; elem
= elem
->next
, i
++) {
710 g_assert_cmpint(elem
->value
, ==, i
);
714 case USER_DEF_NATIVE_LIST_UNION_KIND_S32
: {
715 int32List
*elem
= NULL
;
716 for (i
= 0, elem
= cvalue
->u
.s32
.data
; elem
; elem
= elem
->next
, i
++) {
717 g_assert_cmpint(elem
->value
, ==, i
);
721 case USER_DEF_NATIVE_LIST_UNION_KIND_S64
: {
722 int64List
*elem
= NULL
;
723 for (i
= 0, elem
= cvalue
->u
.s64
.data
; elem
; elem
= elem
->next
, i
++) {
724 g_assert_cmpint(elem
->value
, ==, i
);
728 case USER_DEF_NATIVE_LIST_UNION_KIND_U8
: {
729 uint8List
*elem
= NULL
;
730 for (i
= 0, elem
= cvalue
->u
.u8
.data
; elem
; elem
= elem
->next
, i
++) {
731 g_assert_cmpint(elem
->value
, ==, i
);
735 case USER_DEF_NATIVE_LIST_UNION_KIND_U16
: {
736 uint16List
*elem
= NULL
;
737 for (i
= 0, elem
= cvalue
->u
.u16
.data
; elem
; elem
= elem
->next
, i
++) {
738 g_assert_cmpint(elem
->value
, ==, i
);
742 case USER_DEF_NATIVE_LIST_UNION_KIND_U32
: {
743 uint32List
*elem
= NULL
;
744 for (i
= 0, elem
= cvalue
->u
.u32
.data
; elem
; elem
= elem
->next
, i
++) {
745 g_assert_cmpint(elem
->value
, ==, i
);
749 case USER_DEF_NATIVE_LIST_UNION_KIND_U64
: {
750 uint64List
*elem
= NULL
;
751 for (i
= 0, elem
= cvalue
->u
.u64
.data
; elem
; elem
= elem
->next
, i
++) {
752 g_assert_cmpint(elem
->value
, ==, i
);
757 g_assert_not_reached();
760 g_string_free(gstr_union
, true);
761 g_string_free(gstr_list
, true);
762 qapi_free_UserDefNativeListUnion(cvalue
);
765 static void test_visitor_in_native_list_int(TestInputVisitorData
*data
,
768 test_native_list_integer_helper(data
, unused
,
769 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
);
772 static void test_visitor_in_native_list_int8(TestInputVisitorData
*data
,
775 test_native_list_integer_helper(data
, unused
,
776 USER_DEF_NATIVE_LIST_UNION_KIND_S8
);
779 static void test_visitor_in_native_list_int16(TestInputVisitorData
*data
,
782 test_native_list_integer_helper(data
, unused
,
783 USER_DEF_NATIVE_LIST_UNION_KIND_S16
);
786 static void test_visitor_in_native_list_int32(TestInputVisitorData
*data
,
789 test_native_list_integer_helper(data
, unused
,
790 USER_DEF_NATIVE_LIST_UNION_KIND_S32
);
793 static void test_visitor_in_native_list_int64(TestInputVisitorData
*data
,
796 test_native_list_integer_helper(data
, unused
,
797 USER_DEF_NATIVE_LIST_UNION_KIND_S64
);
800 static void test_visitor_in_native_list_uint8(TestInputVisitorData
*data
,
803 test_native_list_integer_helper(data
, unused
,
804 USER_DEF_NATIVE_LIST_UNION_KIND_U8
);
807 static void test_visitor_in_native_list_uint16(TestInputVisitorData
*data
,
810 test_native_list_integer_helper(data
, unused
,
811 USER_DEF_NATIVE_LIST_UNION_KIND_U16
);
814 static void test_visitor_in_native_list_uint32(TestInputVisitorData
*data
,
817 test_native_list_integer_helper(data
, unused
,
818 USER_DEF_NATIVE_LIST_UNION_KIND_U32
);
821 static void test_visitor_in_native_list_uint64(TestInputVisitorData
*data
,
824 test_native_list_integer_helper(data
, unused
,
825 USER_DEF_NATIVE_LIST_UNION_KIND_U64
);
828 static void test_visitor_in_native_list_bool(TestInputVisitorData
*data
,
831 UserDefNativeListUnion
*cvalue
= NULL
;
832 boolList
*elem
= NULL
;
834 GString
*gstr_list
= g_string_new("");
835 GString
*gstr_union
= g_string_new("");
838 for (i
= 0; i
< 32; i
++) {
839 g_string_append_printf(gstr_list
, "%s",
840 (i
% 3 == 0) ? "true" : "false");
842 g_string_append(gstr_list
, ", ");
845 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
847 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
849 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
850 g_assert(cvalue
!= NULL
);
851 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN
);
853 for (i
= 0, elem
= cvalue
->u
.boolean
.data
; elem
; elem
= elem
->next
, i
++) {
854 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
857 g_string_free(gstr_union
, true);
858 g_string_free(gstr_list
, true);
859 qapi_free_UserDefNativeListUnion(cvalue
);
862 static void test_visitor_in_native_list_string(TestInputVisitorData
*data
,
865 UserDefNativeListUnion
*cvalue
= NULL
;
866 strList
*elem
= NULL
;
868 GString
*gstr_list
= g_string_new("");
869 GString
*gstr_union
= g_string_new("");
872 for (i
= 0; i
< 32; i
++) {
873 g_string_append_printf(gstr_list
, "'%d'", i
);
875 g_string_append(gstr_list
, ", ");
878 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
880 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
882 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
883 g_assert(cvalue
!= NULL
);
884 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING
);
886 for (i
= 0, elem
= cvalue
->u
.string
.data
; elem
; elem
= elem
->next
, i
++) {
888 sprintf(str
, "%d", i
);
889 g_assert_cmpstr(elem
->value
, ==, str
);
892 g_string_free(gstr_union
, true);
893 g_string_free(gstr_list
, true);
894 qapi_free_UserDefNativeListUnion(cvalue
);
897 #define DOUBLE_STR_MAX 16
899 static void test_visitor_in_native_list_number(TestInputVisitorData
*data
,
902 UserDefNativeListUnion
*cvalue
= NULL
;
903 numberList
*elem
= NULL
;
905 GString
*gstr_list
= g_string_new("");
906 GString
*gstr_union
= g_string_new("");
909 for (i
= 0; i
< 32; i
++) {
910 g_string_append_printf(gstr_list
, "%f", (double)i
/ 3);
912 g_string_append(gstr_list
, ", ");
915 g_string_append_printf(gstr_union
, "{ 'type': 'number', 'data': [ %s ] }",
917 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
919 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
920 g_assert(cvalue
!= NULL
);
921 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER
);
923 for (i
= 0, elem
= cvalue
->u
.number
.data
; elem
; elem
= elem
->next
, i
++) {
924 GString
*double_expected
= g_string_new("");
925 GString
*double_actual
= g_string_new("");
927 g_string_printf(double_expected
, "%.6f", (double)i
/ 3);
928 g_string_printf(double_actual
, "%.6f", elem
->value
);
929 g_assert_cmpstr(double_expected
->str
, ==, double_actual
->str
);
931 g_string_free(double_expected
, true);
932 g_string_free(double_actual
, true);
935 g_string_free(gstr_union
, true);
936 g_string_free(gstr_list
, true);
937 qapi_free_UserDefNativeListUnion(cvalue
);
940 static void input_visitor_test_add(const char *testpath
,
941 const void *user_data
,
942 void (*test_func
)(TestInputVisitorData
*data
,
943 const void *user_data
))
945 g_test_add(testpath
, TestInputVisitorData
, user_data
, NULL
, test_func
,
946 visitor_input_teardown
);
949 static void test_visitor_in_errors(TestInputVisitorData
*data
,
952 TestStruct
*p
= NULL
;
956 UserDefTwo
*r
= NULL
;
957 WrapAlternate
*s
= NULL
;
959 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', "
962 visit_type_TestStruct(v
, NULL
, &p
, &err
);
963 error_free_or_abort(&err
);
966 v
= visitor_input_test_init(data
, "[ '1', '2', false, '3' ]");
967 visit_type_strList(v
, NULL
, &q
, &err
);
968 error_free_or_abort(&err
);
971 v
= visitor_input_test_init(data
, "{ 'str':'hi' }");
972 visit_type_UserDefTwo(v
, NULL
, &r
, &err
);
973 error_free_or_abort(&err
);
976 v
= visitor_input_test_init(data
, "{ }");
977 visit_type_WrapAlternate(v
, NULL
, &s
, &err
);
978 error_free_or_abort(&err
);
982 static void test_visitor_in_wrong_type(TestInputVisitorData
*data
,
985 TestStruct
*p
= NULL
;
991 /* Make sure arrays and structs cannot be confused */
993 v
= visitor_input_test_init(data
, "[]");
994 visit_type_TestStruct(v
, NULL
, &p
, &err
);
995 error_free_or_abort(&err
);
998 v
= visitor_input_test_init(data
, "{}");
999 visit_type_strList(v
, NULL
, &q
, &err
);
1000 error_free_or_abort(&err
);
1003 /* Make sure primitives and struct cannot be confused */
1005 v
= visitor_input_test_init(data
, "1");
1006 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1007 error_free_or_abort(&err
);
1010 v
= visitor_input_test_init(data
, "{}");
1011 visit_type_int(v
, NULL
, &i
, &err
);
1012 error_free_or_abort(&err
);
1014 /* Make sure primitives and arrays cannot be confused */
1016 v
= visitor_input_test_init(data
, "1");
1017 visit_type_strList(v
, NULL
, &q
, &err
);
1018 error_free_or_abort(&err
);
1021 v
= visitor_input_test_init(data
, "[]");
1022 visit_type_int(v
, NULL
, &i
, &err
);
1023 error_free_or_abort(&err
);
1026 static void test_visitor_in_fail_struct(TestInputVisitorData
*data
,
1029 TestStruct
*p
= NULL
;
1033 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
1035 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1036 error_free_or_abort(&err
);
1040 static void test_visitor_in_fail_struct_nested(TestInputVisitorData
*data
,
1043 UserDefTwo
*udp
= NULL
;
1047 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
1049 visit_type_UserDefTwo(v
, NULL
, &udp
, &err
);
1050 error_free_or_abort(&err
);
1054 static void test_visitor_in_fail_struct_in_list(TestInputVisitorData
*data
,
1057 UserDefOneList
*head
= NULL
;
1061 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
1063 visit_type_UserDefOneList(v
, NULL
, &head
, &err
);
1064 error_free_or_abort(&err
);
1068 static void test_visitor_in_fail_struct_missing(TestInputVisitorData
*data
,
1074 GenericAlternate
*alt
;
1083 v
= visitor_input_test_init(data
, "{ 'sub': [ {} ] }");
1084 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
1085 visit_start_struct(v
, "struct", NULL
, 0, &err
);
1086 error_free_or_abort(&err
);
1087 visit_start_list(v
, "list", NULL
, 0, &err
);
1088 error_free_or_abort(&err
);
1089 visit_start_alternate(v
, "alternate", &alt
, sizeof(*alt
), false, &err
);
1090 error_free_or_abort(&err
);
1091 visit_optional(v
, "optional", &present
);
1093 visit_type_enum(v
, "enum", &en
, EnumOne_lookup
, &err
);
1094 error_free_or_abort(&err
);
1095 visit_type_int(v
, "i64", &i64
, &err
);
1096 error_free_or_abort(&err
);
1097 visit_type_uint32(v
, "u32", &u32
, &err
);
1098 error_free_or_abort(&err
);
1099 visit_type_int8(v
, "i8", &i8
, &err
);
1100 error_free_or_abort(&err
);
1101 visit_type_str(v
, "i8", &str
, &err
);
1102 error_free_or_abort(&err
);
1103 visit_type_number(v
, "dbl", &dbl
, &err
);
1104 error_free_or_abort(&err
);
1105 visit_type_any(v
, "any", &any
, &err
);
1106 error_free_or_abort(&err
);
1107 visit_type_null(v
, "null", &err
);
1108 error_free_or_abort(&err
);
1109 visit_start_list(v
, "sub", NULL
, 0, &error_abort
);
1110 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
1111 visit_type_int(v
, "i64", &i64
, &err
);
1112 error_free_or_abort(&err
);
1113 visit_end_struct(v
, NULL
);
1114 visit_end_list(v
, NULL
);
1115 visit_end_struct(v
, NULL
);
1118 static void test_visitor_in_fail_list(TestInputVisitorData
*data
,
1125 /* Unvisited list tail */
1127 v
= visitor_input_test_init(data
, "[ 1, 2, 3 ]");
1129 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1130 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1131 g_assert_cmpint(i64
, ==, 1);
1132 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1133 g_assert_cmpint(i64
, ==, 2);
1134 visit_check_list(v
, &err
);
1135 error_free_or_abort(&err
);
1136 visit_end_list(v
, NULL
);
1138 /* Visit beyond end of list */
1139 v
= visitor_input_test_init(data
, "[]");
1141 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1142 visit_type_int(v
, NULL
, &i64
, &err
);
1143 error_free_or_abort(&err
);
1144 visit_end_list(v
, NULL
);
1147 static void test_visitor_in_fail_list_nested(TestInputVisitorData
*data
,
1154 /* Unvisited nested list tail */
1156 v
= visitor_input_test_init(data
, "[ 0, [ 1, 2, 3 ] ]");
1158 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1159 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1160 g_assert_cmpint(i64
, ==, 0);
1161 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1162 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1163 g_assert_cmpint(i64
, ==, 1);
1164 visit_check_list(v
, &err
);
1165 error_free_or_abort(&err
);
1166 visit_end_list(v
, NULL
);
1167 visit_check_list(v
, &error_abort
);
1168 visit_end_list(v
, NULL
);
1171 static void test_visitor_in_fail_union_native_list(TestInputVisitorData
*data
,
1174 UserDefNativeListUnion
*tmp
= NULL
;
1178 v
= visitor_input_test_init(data
,
1179 "{ 'type': 'integer', 'data' : [ 'string' ] }");
1181 visit_type_UserDefNativeListUnion(v
, NULL
, &tmp
, &err
);
1182 error_free_or_abort(&err
);
1186 static void test_visitor_in_fail_union_flat(TestInputVisitorData
*data
,
1189 UserDefFlatUnion
*tmp
= NULL
;
1193 v
= visitor_input_test_init(data
, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
1195 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &err
);
1196 error_free_or_abort(&err
);
1200 static void test_visitor_in_fail_union_flat_no_discrim(TestInputVisitorData
*data
,
1203 UserDefFlatUnion2
*tmp
= NULL
;
1207 /* test situation where discriminator field ('enum1' here) is missing */
1208 v
= visitor_input_test_init(data
, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
1210 visit_type_UserDefFlatUnion2(v
, NULL
, &tmp
, &err
);
1211 error_free_or_abort(&err
);
1215 static void test_visitor_in_fail_alternate(TestInputVisitorData
*data
,
1218 UserDefAlternate
*tmp
;
1222 v
= visitor_input_test_init(data
, "3.14");
1224 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
1225 error_free_or_abort(&err
);
1229 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1230 const char *schema_json
)
1232 SchemaInfoList
*schema
= NULL
;
1235 v
= visitor_input_test_init_raw(data
, schema_json
);
1237 visit_type_SchemaInfoList(v
, NULL
, &schema
, &error_abort
);
1240 qapi_free_SchemaInfoList(schema
);
1243 static void test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1246 do_test_visitor_in_qmp_introspect(data
, test_qmp_schema_json
);
1247 do_test_visitor_in_qmp_introspect(data
, qmp_schema_json
);
1250 int main(int argc
, char **argv
)
1252 g_test_init(&argc
, &argv
, NULL
);
1254 input_visitor_test_add("/visitor/input/int",
1255 NULL
, test_visitor_in_int
);
1256 input_visitor_test_add("/visitor/input/uint",
1257 NULL
, test_visitor_in_uint
);
1258 input_visitor_test_add("/visitor/input/int_overflow",
1259 NULL
, test_visitor_in_int_overflow
);
1260 input_visitor_test_add("/visitor/input/int_keyval",
1261 NULL
, test_visitor_in_int_keyval
);
1262 input_visitor_test_add("/visitor/input/int_str_keyval",
1263 NULL
, test_visitor_in_int_str_keyval
);
1264 input_visitor_test_add("/visitor/input/int_str_fail",
1265 NULL
, test_visitor_in_int_str_fail
);
1266 input_visitor_test_add("/visitor/input/bool",
1267 NULL
, test_visitor_in_bool
);
1268 input_visitor_test_add("/visitor/input/bool_keyval",
1269 NULL
, test_visitor_in_bool_keyval
);
1270 input_visitor_test_add("/visitor/input/bool_str_keyval",
1271 NULL
, test_visitor_in_bool_str_keyval
);
1272 input_visitor_test_add("/visitor/input/bool_str_fail",
1273 NULL
, test_visitor_in_bool_str_fail
);
1274 input_visitor_test_add("/visitor/input/number",
1275 NULL
, test_visitor_in_number
);
1276 input_visitor_test_add("/visitor/input/number_keyval",
1277 NULL
, test_visitor_in_number_keyval
);
1278 input_visitor_test_add("/visitor/input/number_str_keyval",
1279 NULL
, test_visitor_in_number_str_keyval
);
1280 input_visitor_test_add("/visitor/input/number_str_fail",
1281 NULL
, test_visitor_in_number_str_fail
);
1282 input_visitor_test_add("/visitor/input/size_str_keyval",
1283 NULL
, test_visitor_in_size_str_keyval
);
1284 input_visitor_test_add("/visitor/input/size_str_fail",
1285 NULL
, test_visitor_in_size_str_fail
);
1286 input_visitor_test_add("/visitor/input/string",
1287 NULL
, test_visitor_in_string
);
1288 input_visitor_test_add("/visitor/input/enum",
1289 NULL
, test_visitor_in_enum
);
1290 input_visitor_test_add("/visitor/input/struct",
1291 NULL
, test_visitor_in_struct
);
1292 input_visitor_test_add("/visitor/input/struct-nested",
1293 NULL
, test_visitor_in_struct_nested
);
1294 input_visitor_test_add("/visitor/input/list",
1295 NULL
, test_visitor_in_list
);
1296 input_visitor_test_add("/visitor/input/any",
1297 NULL
, test_visitor_in_any
);
1298 input_visitor_test_add("/visitor/input/null",
1299 NULL
, test_visitor_in_null
);
1300 input_visitor_test_add("/visitor/input/union-flat",
1301 NULL
, test_visitor_in_union_flat
);
1302 input_visitor_test_add("/visitor/input/alternate",
1303 NULL
, test_visitor_in_alternate
);
1304 input_visitor_test_add("/visitor/input/errors",
1305 NULL
, test_visitor_in_errors
);
1306 input_visitor_test_add("/visitor/input/wrong-type",
1307 NULL
, test_visitor_in_wrong_type
);
1308 input_visitor_test_add("/visitor/input/alternate-number",
1309 NULL
, test_visitor_in_alternate_number
);
1310 input_visitor_test_add("/visitor/input/native_list/int",
1311 NULL
, test_visitor_in_native_list_int
);
1312 input_visitor_test_add("/visitor/input/native_list/int8",
1313 NULL
, test_visitor_in_native_list_int8
);
1314 input_visitor_test_add("/visitor/input/native_list/int16",
1315 NULL
, test_visitor_in_native_list_int16
);
1316 input_visitor_test_add("/visitor/input/native_list/int32",
1317 NULL
, test_visitor_in_native_list_int32
);
1318 input_visitor_test_add("/visitor/input/native_list/int64",
1319 NULL
, test_visitor_in_native_list_int64
);
1320 input_visitor_test_add("/visitor/input/native_list/uint8",
1321 NULL
, test_visitor_in_native_list_uint8
);
1322 input_visitor_test_add("/visitor/input/native_list/uint16",
1323 NULL
, test_visitor_in_native_list_uint16
);
1324 input_visitor_test_add("/visitor/input/native_list/uint32",
1325 NULL
, test_visitor_in_native_list_uint32
);
1326 input_visitor_test_add("/visitor/input/native_list/uint64",
1327 NULL
, test_visitor_in_native_list_uint64
);
1328 input_visitor_test_add("/visitor/input/native_list/bool",
1329 NULL
, test_visitor_in_native_list_bool
);
1330 input_visitor_test_add("/visitor/input/native_list/str",
1331 NULL
, test_visitor_in_native_list_string
);
1332 input_visitor_test_add("/visitor/input/native_list/number",
1333 NULL
, test_visitor_in_native_list_number
);
1334 input_visitor_test_add("/visitor/input/fail/struct",
1335 NULL
, test_visitor_in_fail_struct
);
1336 input_visitor_test_add("/visitor/input/fail/struct-nested",
1337 NULL
, test_visitor_in_fail_struct_nested
);
1338 input_visitor_test_add("/visitor/input/fail/struct-in-list",
1339 NULL
, test_visitor_in_fail_struct_in_list
);
1340 input_visitor_test_add("/visitor/input/fail/struct-missing",
1341 NULL
, test_visitor_in_fail_struct_missing
);
1342 input_visitor_test_add("/visitor/input/fail/list",
1343 NULL
, test_visitor_in_fail_list
);
1344 input_visitor_test_add("/visitor/input/fail/list-nested",
1345 NULL
, test_visitor_in_fail_list_nested
);
1346 input_visitor_test_add("/visitor/input/fail/union-flat",
1347 NULL
, test_visitor_in_fail_union_flat
);
1348 input_visitor_test_add("/visitor/input/fail/union-flat-no-discriminator",
1349 NULL
, test_visitor_in_fail_union_flat_no_discrim
);
1350 input_visitor_test_add("/visitor/input/fail/alternate",
1351 NULL
, test_visitor_in_fail_alternate
);
1352 input_visitor_test_add("/visitor/input/fail/union-native-list",
1353 NULL
, test_visitor_in_fail_union_native_list
);
1354 input_visitor_test_add("/visitor/input/qmp-introspect",
1355 NULL
, test_visitor_in_qmp_introspect
);