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/qapi-visit-introspect.h"
19 #include "qapi/qobject-input-visitor.h"
20 #include "test-qapi-visit.h"
21 #include "qapi/qmp/qbool.h"
22 #include "qapi/qmp/qdict.h"
23 #include "qapi/qmp/qnull.h"
24 #include "qapi/qmp/qnum.h"
25 #include "qapi/qmp/qstring.h"
26 #include "qapi/qmp/qjson.h"
27 #include "test-qapi-introspect.h"
28 #include "qapi/qapi-introspect.h"
30 typedef struct TestInputVisitorData
{
33 } TestInputVisitorData
;
35 static void visitor_input_teardown(TestInputVisitorData
*data
,
38 qobject_unref(data
->obj
);
42 visit_free(data
->qiv
);
47 /* The various test_init functions are provided instead of a test setup
48 function so that the JSON string used by the tests are kept in the test
49 functions (and not in main()). */
51 static Visitor
*test_init_internal(TestInputVisitorData
*data
, bool keyval
,
54 visitor_input_teardown(data
, NULL
);
59 data
->qiv
= qobject_input_visitor_new_keyval(data
->obj
);
61 data
->qiv
= qobject_input_visitor_new(data
->obj
);
67 static GCC_FMT_ATTR(3, 4)
68 Visitor
*visitor_input_test_init_full(TestInputVisitorData
*data
,
70 const char *json_string
, ...)
75 va_start(ap
, json_string
);
76 v
= test_init_internal(data
, keyval
,
77 qobject_from_vjsonf_nofail(json_string
, ap
));
82 static GCC_FMT_ATTR(2, 3)
83 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
84 const char *json_string
, ...)
89 va_start(ap
, json_string
);
90 v
= test_init_internal(data
, false,
91 qobject_from_vjsonf_nofail(json_string
, ap
));
96 /* similar to visitor_input_test_init(), but does not expect a string
97 * literal/format json_string argument and so can be used for
98 * programatically generated strings (and we can't pass in programatically
99 * generated strings via %s format parameters since qobject_from_jsonv()
100 * will wrap those in double-quotes and treat the entire object as a
103 static Visitor
*visitor_input_test_init_raw(TestInputVisitorData
*data
,
104 const char *json_string
)
106 return test_init_internal(data
, false,
107 qobject_from_json(json_string
, &error_abort
));
110 static void test_visitor_in_int(TestInputVisitorData
*data
,
118 v
= visitor_input_test_init(data
, "%d", value
);
120 visit_type_int(v
, NULL
, &res
, &error_abort
);
121 g_assert_cmpint(res
, ==, value
);
123 visit_type_number(v
, NULL
, &dbl
, &error_abort
);
124 g_assert_cmpfloat(dbl
, ==, -42.0);
127 static void test_visitor_in_uint(TestInputVisitorData
*data
,
136 v
= visitor_input_test_init(data
, "%d", value
);
138 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
139 g_assert_cmpuint(res
, ==, (uint64_t)value
);
141 visit_type_int(v
, NULL
, &i64
, &error_abort
);
142 g_assert_cmpint(i64
, ==, value
);
144 visit_type_number(v
, NULL
, &dbl
, &error_abort
);
145 g_assert_cmpfloat(dbl
, ==, value
);
147 /* BUG: value between INT64_MIN and -1 accepted modulo 2^64 */
148 v
= visitor_input_test_init(data
, "%d", -value
);
150 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
151 g_assert_cmpuint(res
, ==, (uint64_t)-value
);
153 v
= visitor_input_test_init(data
, "18446744073709551574");
155 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
156 g_assert_cmpuint(res
, ==, 18446744073709551574U);
158 visit_type_number(v
, NULL
, &dbl
, &error_abort
);
159 g_assert_cmpfloat(dbl
, ==, 18446744073709552000.0);
162 static void test_visitor_in_int_overflow(TestInputVisitorData
*data
,
170 * This will overflow a QNUM_I64, so should be deserialized into a
171 * QNUM_DOUBLE field instead, leading to an error if we pass it to
172 * visit_type_int(). Confirm this.
174 v
= visitor_input_test_init(data
, "%f", DBL_MAX
);
176 visit_type_int(v
, NULL
, &res
, &err
);
177 error_free_or_abort(&err
);
180 static void test_visitor_in_int_keyval(TestInputVisitorData
*data
,
183 int64_t res
= 0, value
= -42;
187 v
= visitor_input_test_init_full(data
, true, "%" PRId64
, value
);
188 visit_type_int(v
, NULL
, &res
, &err
);
189 error_free_or_abort(&err
);
192 static void test_visitor_in_int_str_keyval(TestInputVisitorData
*data
,
195 int64_t res
= 0, value
= -42;
198 v
= visitor_input_test_init_full(data
, true, "\"-42\"");
200 visit_type_int(v
, NULL
, &res
, &error_abort
);
201 g_assert_cmpint(res
, ==, value
);
204 static void test_visitor_in_int_str_fail(TestInputVisitorData
*data
,
211 v
= visitor_input_test_init(data
, "\"-42\"");
213 visit_type_int(v
, NULL
, &res
, &err
);
214 error_free_or_abort(&err
);
217 static void test_visitor_in_bool(TestInputVisitorData
*data
,
223 v
= visitor_input_test_init(data
, "true");
225 visit_type_bool(v
, NULL
, &res
, &error_abort
);
226 g_assert_cmpint(res
, ==, true);
229 static void test_visitor_in_bool_keyval(TestInputVisitorData
*data
,
236 v
= visitor_input_test_init_full(data
, true, "true");
238 visit_type_bool(v
, NULL
, &res
, &err
);
239 error_free_or_abort(&err
);
242 static void test_visitor_in_bool_str_keyval(TestInputVisitorData
*data
,
248 v
= visitor_input_test_init_full(data
, true, "\"on\"");
250 visit_type_bool(v
, NULL
, &res
, &error_abort
);
251 g_assert_cmpint(res
, ==, true);
254 static void test_visitor_in_bool_str_fail(TestInputVisitorData
*data
,
261 v
= visitor_input_test_init(data
, "\"true\"");
263 visit_type_bool(v
, NULL
, &res
, &err
);
264 error_free_or_abort(&err
);
267 static void test_visitor_in_number(TestInputVisitorData
*data
,
270 double res
= 0, value
= 3.14;
273 v
= visitor_input_test_init(data
, "%f", value
);
275 visit_type_number(v
, NULL
, &res
, &error_abort
);
276 g_assert_cmpfloat(res
, ==, value
);
279 static void test_visitor_in_large_number(TestInputVisitorData
*data
,
288 v
= visitor_input_test_init(data
, "-18446744073709551616"); /* -2^64 */
290 visit_type_number(v
, NULL
, &res
, &error_abort
);
291 g_assert_cmpfloat(res
, ==, -18446744073709552e3
);
293 visit_type_int(v
, NULL
, &i64
, &err
);
294 error_free_or_abort(&err
);
296 visit_type_uint64(v
, NULL
, &u64
, &err
);
297 error_free_or_abort(&err
);
300 static void test_visitor_in_number_keyval(TestInputVisitorData
*data
,
303 double res
= 0, value
= 3.14;
307 v
= visitor_input_test_init_full(data
, true, "%f", value
);
309 visit_type_number(v
, NULL
, &res
, &err
);
310 error_free_or_abort(&err
);
313 static void test_visitor_in_number_str_keyval(TestInputVisitorData
*data
,
316 double res
= 0, value
= 3.14;
320 v
= visitor_input_test_init_full(data
, true, "\"3.14\"");
322 visit_type_number(v
, NULL
, &res
, &error_abort
);
323 g_assert_cmpfloat(res
, ==, value
);
325 v
= visitor_input_test_init_full(data
, true, "\"inf\"");
327 visit_type_number(v
, NULL
, &res
, &err
);
328 error_free_or_abort(&err
);
331 static void test_visitor_in_number_str_fail(TestInputVisitorData
*data
,
338 v
= visitor_input_test_init(data
, "\"3.14\"");
340 visit_type_number(v
, NULL
, &res
, &err
);
341 error_free_or_abort(&err
);
344 static void test_visitor_in_size_str_keyval(TestInputVisitorData
*data
,
347 uint64_t res
, value
= 500 * 1024 * 1024;
350 v
= visitor_input_test_init_full(data
, true, "\"500M\"");
352 visit_type_size(v
, NULL
, &res
, &error_abort
);
353 g_assert_cmpfloat(res
, ==, value
);
356 static void test_visitor_in_size_str_fail(TestInputVisitorData
*data
,
363 v
= visitor_input_test_init(data
, "\"500M\"");
365 visit_type_size(v
, NULL
, &res
, &err
);
366 error_free_or_abort(&err
);
369 static void test_visitor_in_string(TestInputVisitorData
*data
,
372 char *res
= NULL
, *value
= (char *) "Q E M U";
375 v
= visitor_input_test_init(data
, "%s", value
);
377 visit_type_str(v
, NULL
, &res
, &error_abort
);
378 g_assert_cmpstr(res
, ==, value
);
383 static void test_visitor_in_enum(TestInputVisitorData
*data
,
389 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
392 v
= visitor_input_test_init(data
, "%s", EnumOne_str(i
));
394 visit_type_EnumOne(v
, NULL
, &res
, &error_abort
);
395 g_assert_cmpint(i
, ==, res
);
400 static void test_visitor_in_struct(TestInputVisitorData
*data
,
403 TestStruct
*p
= NULL
;
406 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
408 visit_type_TestStruct(v
, NULL
, &p
, &error_abort
);
409 g_assert_cmpint(p
->integer
, ==, -42);
410 g_assert(p
->boolean
== true);
411 g_assert_cmpstr(p
->string
, ==, "foo");
417 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
420 UserDefTwo
*udp
= NULL
;
423 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', "
424 "'dict1': { 'string1': 'string1', "
425 "'dict2': { 'userdef': { 'integer': 42, "
426 "'string': 'string' }, 'string': 'string2'}}}");
428 visit_type_UserDefTwo(v
, NULL
, &udp
, &error_abort
);
430 g_assert_cmpstr(udp
->string0
, ==, "string0");
431 g_assert_cmpstr(udp
->dict1
->string1
, ==, "string1");
432 g_assert_cmpint(udp
->dict1
->dict2
->userdef
->integer
, ==, 42);
433 g_assert_cmpstr(udp
->dict1
->dict2
->userdef
->string
, ==, "string");
434 g_assert_cmpstr(udp
->dict1
->dict2
->string
, ==, "string2");
435 g_assert(udp
->dict1
->has_dict3
== false);
437 qapi_free_UserDefTwo(udp
);
440 static void test_visitor_in_list(TestInputVisitorData
*data
,
443 UserDefOneList
*item
, *head
= NULL
;
447 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
449 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
450 g_assert(head
!= NULL
);
452 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
455 snprintf(string
, sizeof(string
), "string%d", i
);
456 g_assert_cmpstr(item
->value
->string
, ==, string
);
457 g_assert_cmpint(item
->value
->integer
, ==, 42 + i
);
460 qapi_free_UserDefOneList(head
);
463 /* An empty list is valid */
464 v
= visitor_input_test_init(data
, "[]");
465 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
469 static void test_visitor_in_any(TestInputVisitorData
*data
,
481 v
= visitor_input_test_init(data
, "-42");
482 visit_type_any(v
, NULL
, &res
, &error_abort
);
483 qnum
= qobject_to(QNum
, res
);
485 g_assert(qnum_get_try_int(qnum
, &val
));
486 g_assert_cmpint(val
, ==, -42);
489 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
490 visit_type_any(v
, NULL
, &res
, &error_abort
);
491 qdict
= qobject_to(QDict
, res
);
492 g_assert(qdict
&& qdict_size(qdict
) == 3);
493 qobj
= qdict_get(qdict
, "integer");
495 qnum
= qobject_to(QNum
, qobj
);
497 g_assert(qnum_get_try_int(qnum
, &val
));
498 g_assert_cmpint(val
, ==, -42);
499 qobj
= qdict_get(qdict
, "boolean");
501 qbool
= qobject_to(QBool
, qobj
);
503 g_assert(qbool_get_bool(qbool
) == true);
504 qobj
= qdict_get(qdict
, "string");
506 qstring
= qobject_to(QString
, qobj
);
508 g_assert_cmpstr(qstring_get_str(qstring
), ==, "foo");
512 static void test_visitor_in_null(TestInputVisitorData
*data
,
521 * FIXME: Since QAPI doesn't know the 'null' type yet, we can't
522 * test visit_type_null() by reading into a QAPI struct then
523 * checking that it was populated correctly. The best we can do
524 * for now is ensure that we consumed null from the input, proven
525 * by the fact that we can't re-read the key; and that we detect
526 * when input is not null.
529 v
= visitor_input_test_init_full(data
, false,
530 "{ 'a': null, 'b': '' }");
531 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
532 visit_type_null(v
, "a", &null
, &error_abort
);
533 g_assert(qobject_type(QOBJECT(null
)) == QTYPE_QNULL
);
535 visit_type_null(v
, "b", &null
, &err
);
536 error_free_or_abort(&err
);
538 visit_type_str(v
, "c", &tmp
, &err
);
539 error_free_or_abort(&err
);
541 visit_check_struct(v
, &error_abort
);
542 visit_end_struct(v
, NULL
);
545 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
549 UserDefFlatUnion
*tmp
;
550 UserDefUnionBase
*base
;
552 v
= visitor_input_test_init(data
,
553 "{ 'enum1': 'value1', "
556 "'boolean': true }");
558 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &error_abort
);
559 g_assert_cmpint(tmp
->enum1
, ==, ENUM_ONE_VALUE1
);
560 g_assert_cmpstr(tmp
->string
, ==, "str");
561 g_assert_cmpint(tmp
->integer
, ==, 41);
562 g_assert_cmpint(tmp
->u
.value1
.boolean
, ==, true);
564 base
= qapi_UserDefFlatUnion_base(tmp
);
565 g_assert(&base
->enum1
== &tmp
->enum1
);
567 qapi_free_UserDefFlatUnion(tmp
);
570 static void test_visitor_in_alternate(TestInputVisitorData
*data
,
574 UserDefAlternate
*tmp
;
577 v
= visitor_input_test_init(data
, "42");
578 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
579 g_assert_cmpint(tmp
->type
, ==, QTYPE_QNUM
);
580 g_assert_cmpint(tmp
->u
.i
, ==, 42);
581 qapi_free_UserDefAlternate(tmp
);
583 v
= visitor_input_test_init(data
, "'value1'");
584 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
585 g_assert_cmpint(tmp
->type
, ==, QTYPE_QSTRING
);
586 g_assert_cmpint(tmp
->u
.e
, ==, ENUM_ONE_VALUE1
);
587 qapi_free_UserDefAlternate(tmp
);
589 v
= visitor_input_test_init(data
, "null");
590 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
591 g_assert_cmpint(tmp
->type
, ==, QTYPE_QNULL
);
592 qapi_free_UserDefAlternate(tmp
);
594 v
= visitor_input_test_init(data
, "{'integer':1, 'string':'str', "
595 "'enum1':'value1', 'boolean':true}");
596 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
597 g_assert_cmpint(tmp
->type
, ==, QTYPE_QDICT
);
598 g_assert_cmpint(tmp
->u
.udfu
.integer
, ==, 1);
599 g_assert_cmpstr(tmp
->u
.udfu
.string
, ==, "str");
600 g_assert_cmpint(tmp
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
601 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.boolean
, ==, true);
602 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
603 qapi_free_UserDefAlternate(tmp
);
605 v
= visitor_input_test_init(data
, "{ 'alt': 42 }");
606 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
607 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QNUM
);
608 g_assert_cmpint(wrap
->alt
->u
.i
, ==, 42);
609 qapi_free_WrapAlternate(wrap
);
611 v
= visitor_input_test_init(data
, "{ 'alt': 'value1' }");
612 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
613 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QSTRING
);
614 g_assert_cmpint(wrap
->alt
->u
.e
, ==, ENUM_ONE_VALUE1
);
615 qapi_free_WrapAlternate(wrap
);
617 v
= visitor_input_test_init(data
, "{ 'alt': {'integer':1, 'string':'str', "
618 "'enum1':'value1', 'boolean':true} }");
619 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
620 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QDICT
);
621 g_assert_cmpint(wrap
->alt
->u
.udfu
.integer
, ==, 1);
622 g_assert_cmpstr(wrap
->alt
->u
.udfu
.string
, ==, "str");
623 g_assert_cmpint(wrap
->alt
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
624 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.boolean
, ==, true);
625 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
626 qapi_free_WrapAlternate(wrap
);
629 static void test_visitor_in_alternate_number(TestInputVisitorData
*data
,
641 v
= visitor_input_test_init(data
, "42");
642 visit_type_AltEnumBool(v
, NULL
, &aeb
, &err
);
643 error_free_or_abort(&err
);
644 qapi_free_AltEnumBool(aeb
);
646 v
= visitor_input_test_init(data
, "42");
647 visit_type_AltEnumNum(v
, NULL
, &aen
, &error_abort
);
648 g_assert_cmpint(aen
->type
, ==, QTYPE_QNUM
);
649 g_assert_cmpfloat(aen
->u
.n
, ==, 42);
650 qapi_free_AltEnumNum(aen
);
652 v
= visitor_input_test_init(data
, "42");
653 visit_type_AltNumEnum(v
, NULL
, &ans
, &error_abort
);
654 g_assert_cmpint(ans
->type
, ==, QTYPE_QNUM
);
655 g_assert_cmpfloat(ans
->u
.n
, ==, 42);
656 qapi_free_AltNumEnum(ans
);
658 v
= visitor_input_test_init(data
, "42");
659 visit_type_AltEnumInt(v
, NULL
, &asi
, &error_abort
);
660 g_assert_cmpint(asi
->type
, ==, QTYPE_QNUM
);
661 g_assert_cmpint(asi
->u
.i
, ==, 42);
662 qapi_free_AltEnumInt(asi
);
664 /* Parsing a double */
666 v
= visitor_input_test_init(data
, "42.5");
667 visit_type_AltEnumBool(v
, NULL
, &aeb
, &err
);
668 error_free_or_abort(&err
);
669 qapi_free_AltEnumBool(aeb
);
671 v
= visitor_input_test_init(data
, "42.5");
672 visit_type_AltEnumNum(v
, NULL
, &aen
, &error_abort
);
673 g_assert_cmpint(aen
->type
, ==, QTYPE_QNUM
);
674 g_assert_cmpfloat(aen
->u
.n
, ==, 42.5);
675 qapi_free_AltEnumNum(aen
);
677 v
= visitor_input_test_init(data
, "42.5");
678 visit_type_AltNumEnum(v
, NULL
, &ans
, &error_abort
);
679 g_assert_cmpint(ans
->type
, ==, QTYPE_QNUM
);
680 g_assert_cmpfloat(ans
->u
.n
, ==, 42.5);
681 qapi_free_AltNumEnum(ans
);
683 v
= visitor_input_test_init(data
, "42.5");
684 visit_type_AltEnumInt(v
, NULL
, &asi
, &err
);
685 error_free_or_abort(&err
);
686 qapi_free_AltEnumInt(asi
);
689 static void test_list_union_integer_helper(TestInputVisitorData
*data
,
691 UserDefListUnionKind kind
)
693 UserDefListUnion
*cvalue
= NULL
;
695 GString
*gstr_list
= g_string_new("");
696 GString
*gstr_union
= g_string_new("");
699 for (i
= 0; i
< 32; i
++) {
700 g_string_append_printf(gstr_list
, "%d", i
);
702 g_string_append(gstr_list
, ", ");
705 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
706 UserDefListUnionKind_str(kind
),
708 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
710 visit_type_UserDefListUnion(v
, NULL
, &cvalue
, &error_abort
);
711 g_assert(cvalue
!= NULL
);
712 g_assert_cmpint(cvalue
->type
, ==, kind
);
715 case USER_DEF_LIST_UNION_KIND_INTEGER
: {
716 intList
*elem
= NULL
;
717 for (i
= 0, elem
= cvalue
->u
.integer
.data
;
718 elem
; elem
= elem
->next
, i
++) {
719 g_assert_cmpint(elem
->value
, ==, i
);
723 case USER_DEF_LIST_UNION_KIND_S8
: {
724 int8List
*elem
= NULL
;
725 for (i
= 0, elem
= cvalue
->u
.s8
.data
; elem
; elem
= elem
->next
, i
++) {
726 g_assert_cmpint(elem
->value
, ==, i
);
730 case USER_DEF_LIST_UNION_KIND_S16
: {
731 int16List
*elem
= NULL
;
732 for (i
= 0, elem
= cvalue
->u
.s16
.data
; elem
; elem
= elem
->next
, i
++) {
733 g_assert_cmpint(elem
->value
, ==, i
);
737 case USER_DEF_LIST_UNION_KIND_S32
: {
738 int32List
*elem
= NULL
;
739 for (i
= 0, elem
= cvalue
->u
.s32
.data
; elem
; elem
= elem
->next
, i
++) {
740 g_assert_cmpint(elem
->value
, ==, i
);
744 case USER_DEF_LIST_UNION_KIND_S64
: {
745 int64List
*elem
= NULL
;
746 for (i
= 0, elem
= cvalue
->u
.s64
.data
; elem
; elem
= elem
->next
, i
++) {
747 g_assert_cmpint(elem
->value
, ==, i
);
751 case USER_DEF_LIST_UNION_KIND_U8
: {
752 uint8List
*elem
= NULL
;
753 for (i
= 0, elem
= cvalue
->u
.u8
.data
; elem
; elem
= elem
->next
, i
++) {
754 g_assert_cmpint(elem
->value
, ==, i
);
758 case USER_DEF_LIST_UNION_KIND_U16
: {
759 uint16List
*elem
= NULL
;
760 for (i
= 0, elem
= cvalue
->u
.u16
.data
; elem
; elem
= elem
->next
, i
++) {
761 g_assert_cmpint(elem
->value
, ==, i
);
765 case USER_DEF_LIST_UNION_KIND_U32
: {
766 uint32List
*elem
= NULL
;
767 for (i
= 0, elem
= cvalue
->u
.u32
.data
; elem
; elem
= elem
->next
, i
++) {
768 g_assert_cmpint(elem
->value
, ==, i
);
772 case USER_DEF_LIST_UNION_KIND_U64
: {
773 uint64List
*elem
= NULL
;
774 for (i
= 0, elem
= cvalue
->u
.u64
.data
; elem
; elem
= elem
->next
, i
++) {
775 g_assert_cmpint(elem
->value
, ==, i
);
780 g_assert_not_reached();
783 g_string_free(gstr_union
, true);
784 g_string_free(gstr_list
, true);
785 qapi_free_UserDefListUnion(cvalue
);
788 static void test_visitor_in_list_union_int(TestInputVisitorData
*data
,
791 test_list_union_integer_helper(data
, unused
,
792 USER_DEF_LIST_UNION_KIND_INTEGER
);
795 static void test_visitor_in_list_union_int8(TestInputVisitorData
*data
,
798 test_list_union_integer_helper(data
, unused
,
799 USER_DEF_LIST_UNION_KIND_S8
);
802 static void test_visitor_in_list_union_int16(TestInputVisitorData
*data
,
805 test_list_union_integer_helper(data
, unused
,
806 USER_DEF_LIST_UNION_KIND_S16
);
809 static void test_visitor_in_list_union_int32(TestInputVisitorData
*data
,
812 test_list_union_integer_helper(data
, unused
,
813 USER_DEF_LIST_UNION_KIND_S32
);
816 static void test_visitor_in_list_union_int64(TestInputVisitorData
*data
,
819 test_list_union_integer_helper(data
, unused
,
820 USER_DEF_LIST_UNION_KIND_S64
);
823 static void test_visitor_in_list_union_uint8(TestInputVisitorData
*data
,
826 test_list_union_integer_helper(data
, unused
,
827 USER_DEF_LIST_UNION_KIND_U8
);
830 static void test_visitor_in_list_union_uint16(TestInputVisitorData
*data
,
833 test_list_union_integer_helper(data
, unused
,
834 USER_DEF_LIST_UNION_KIND_U16
);
837 static void test_visitor_in_list_union_uint32(TestInputVisitorData
*data
,
840 test_list_union_integer_helper(data
, unused
,
841 USER_DEF_LIST_UNION_KIND_U32
);
844 static void test_visitor_in_list_union_uint64(TestInputVisitorData
*data
,
847 test_list_union_integer_helper(data
, unused
,
848 USER_DEF_LIST_UNION_KIND_U64
);
851 static void test_visitor_in_list_union_bool(TestInputVisitorData
*data
,
854 UserDefListUnion
*cvalue
= NULL
;
855 boolList
*elem
= NULL
;
857 GString
*gstr_list
= g_string_new("");
858 GString
*gstr_union
= g_string_new("");
861 for (i
= 0; i
< 32; i
++) {
862 g_string_append_printf(gstr_list
, "%s",
863 (i
% 3 == 0) ? "true" : "false");
865 g_string_append(gstr_list
, ", ");
868 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
870 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
872 visit_type_UserDefListUnion(v
, NULL
, &cvalue
, &error_abort
);
873 g_assert(cvalue
!= NULL
);
874 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_LIST_UNION_KIND_BOOLEAN
);
876 for (i
= 0, elem
= cvalue
->u
.boolean
.data
; elem
; elem
= elem
->next
, i
++) {
877 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
880 g_string_free(gstr_union
, true);
881 g_string_free(gstr_list
, true);
882 qapi_free_UserDefListUnion(cvalue
);
885 static void test_visitor_in_list_union_string(TestInputVisitorData
*data
,
888 UserDefListUnion
*cvalue
= NULL
;
889 strList
*elem
= NULL
;
891 GString
*gstr_list
= g_string_new("");
892 GString
*gstr_union
= g_string_new("");
895 for (i
= 0; i
< 32; i
++) {
896 g_string_append_printf(gstr_list
, "'%d'", i
);
898 g_string_append(gstr_list
, ", ");
901 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
903 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
905 visit_type_UserDefListUnion(v
, NULL
, &cvalue
, &error_abort
);
906 g_assert(cvalue
!= NULL
);
907 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_LIST_UNION_KIND_STRING
);
909 for (i
= 0, elem
= cvalue
->u
.string
.data
; elem
; elem
= elem
->next
, i
++) {
911 sprintf(str
, "%d", i
);
912 g_assert_cmpstr(elem
->value
, ==, str
);
915 g_string_free(gstr_union
, true);
916 g_string_free(gstr_list
, true);
917 qapi_free_UserDefListUnion(cvalue
);
920 #define DOUBLE_STR_MAX 16
922 static void test_visitor_in_list_union_number(TestInputVisitorData
*data
,
925 UserDefListUnion
*cvalue
= NULL
;
926 numberList
*elem
= NULL
;
928 GString
*gstr_list
= g_string_new("");
929 GString
*gstr_union
= g_string_new("");
932 for (i
= 0; i
< 32; i
++) {
933 g_string_append_printf(gstr_list
, "%f", (double)i
/ 3);
935 g_string_append(gstr_list
, ", ");
938 g_string_append_printf(gstr_union
, "{ 'type': 'number', 'data': [ %s ] }",
940 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
942 visit_type_UserDefListUnion(v
, NULL
, &cvalue
, &error_abort
);
943 g_assert(cvalue
!= NULL
);
944 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_LIST_UNION_KIND_NUMBER
);
946 for (i
= 0, elem
= cvalue
->u
.number
.data
; elem
; elem
= elem
->next
, i
++) {
947 GString
*double_expected
= g_string_new("");
948 GString
*double_actual
= g_string_new("");
950 g_string_printf(double_expected
, "%.6f", (double)i
/ 3);
951 g_string_printf(double_actual
, "%.6f", elem
->value
);
952 g_assert_cmpstr(double_expected
->str
, ==, double_actual
->str
);
954 g_string_free(double_expected
, true);
955 g_string_free(double_actual
, true);
958 g_string_free(gstr_union
, true);
959 g_string_free(gstr_list
, true);
960 qapi_free_UserDefListUnion(cvalue
);
963 static void input_visitor_test_add(const char *testpath
,
964 const void *user_data
,
965 void (*test_func
)(TestInputVisitorData
*data
,
966 const void *user_data
))
968 g_test_add(testpath
, TestInputVisitorData
, user_data
, NULL
, test_func
,
969 visitor_input_teardown
);
972 static void test_visitor_in_errors(TestInputVisitorData
*data
,
975 TestStruct
*p
= NULL
;
979 UserDefTwo
*r
= NULL
;
980 WrapAlternate
*s
= NULL
;
982 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', "
985 visit_type_TestStruct(v
, NULL
, &p
, &err
);
986 error_free_or_abort(&err
);
989 v
= visitor_input_test_init(data
, "[ '1', '2', false, '3' ]");
990 visit_type_strList(v
, NULL
, &q
, &err
);
991 error_free_or_abort(&err
);
994 v
= visitor_input_test_init(data
, "{ 'str':'hi' }");
995 visit_type_UserDefTwo(v
, NULL
, &r
, &err
);
996 error_free_or_abort(&err
);
999 v
= visitor_input_test_init(data
, "{ }");
1000 visit_type_WrapAlternate(v
, NULL
, &s
, &err
);
1001 error_free_or_abort(&err
);
1005 static void test_visitor_in_wrong_type(TestInputVisitorData
*data
,
1008 TestStruct
*p
= NULL
;
1014 /* Make sure arrays and structs cannot be confused */
1016 v
= visitor_input_test_init(data
, "[]");
1017 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1018 error_free_or_abort(&err
);
1021 v
= visitor_input_test_init(data
, "{}");
1022 visit_type_strList(v
, NULL
, &q
, &err
);
1023 error_free_or_abort(&err
);
1026 /* Make sure primitives and struct cannot be confused */
1028 v
= visitor_input_test_init(data
, "1");
1029 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1030 error_free_or_abort(&err
);
1033 v
= visitor_input_test_init(data
, "{}");
1034 visit_type_int(v
, NULL
, &i
, &err
);
1035 error_free_or_abort(&err
);
1037 /* Make sure primitives and arrays cannot be confused */
1039 v
= visitor_input_test_init(data
, "1");
1040 visit_type_strList(v
, NULL
, &q
, &err
);
1041 error_free_or_abort(&err
);
1044 v
= visitor_input_test_init(data
, "[]");
1045 visit_type_int(v
, NULL
, &i
, &err
);
1046 error_free_or_abort(&err
);
1049 static void test_visitor_in_fail_struct(TestInputVisitorData
*data
,
1052 TestStruct
*p
= NULL
;
1056 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
1058 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1059 error_free_or_abort(&err
);
1063 static void test_visitor_in_fail_struct_nested(TestInputVisitorData
*data
,
1066 UserDefTwo
*udp
= NULL
;
1070 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
1072 visit_type_UserDefTwo(v
, NULL
, &udp
, &err
);
1073 error_free_or_abort(&err
);
1077 static void test_visitor_in_fail_struct_in_list(TestInputVisitorData
*data
,
1080 UserDefOneList
*head
= NULL
;
1084 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
1086 visit_type_UserDefOneList(v
, NULL
, &head
, &err
);
1087 error_free_or_abort(&err
);
1091 static void test_visitor_in_fail_struct_missing(TestInputVisitorData
*data
,
1098 GenericAlternate
*alt
;
1107 v
= visitor_input_test_init(data
, "{ 'sub': [ {} ] }");
1108 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
1109 visit_start_struct(v
, "struct", NULL
, 0, &err
);
1110 error_free_or_abort(&err
);
1111 visit_start_list(v
, "list", NULL
, 0, &err
);
1112 error_free_or_abort(&err
);
1113 visit_start_alternate(v
, "alternate", &alt
, sizeof(*alt
), &err
);
1114 error_free_or_abort(&err
);
1115 visit_optional(v
, "optional", &present
);
1117 visit_type_enum(v
, "enum", &en
, &EnumOne_lookup
, &err
);
1118 error_free_or_abort(&err
);
1119 visit_type_int(v
, "i64", &i64
, &err
);
1120 error_free_or_abort(&err
);
1121 visit_type_uint32(v
, "u32", &u32
, &err
);
1122 error_free_or_abort(&err
);
1123 visit_type_int8(v
, "i8", &i8
, &err
);
1124 error_free_or_abort(&err
);
1125 visit_type_str(v
, "i8", &str
, &err
);
1126 error_free_or_abort(&err
);
1127 visit_type_number(v
, "dbl", &dbl
, &err
);
1128 error_free_or_abort(&err
);
1129 visit_type_any(v
, "any", &any
, &err
);
1130 error_free_or_abort(&err
);
1131 visit_type_null(v
, "null", &null
, &err
);
1132 error_free_or_abort(&err
);
1133 visit_start_list(v
, "sub", NULL
, 0, &error_abort
);
1134 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
1135 visit_type_int(v
, "i64", &i64
, &err
);
1136 error_free_or_abort(&err
);
1137 visit_end_struct(v
, NULL
);
1138 visit_end_list(v
, NULL
);
1139 visit_end_struct(v
, NULL
);
1142 static void test_visitor_in_fail_list(TestInputVisitorData
*data
,
1149 /* Unvisited list tail */
1151 v
= visitor_input_test_init(data
, "[ 1, 2, 3 ]");
1153 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1154 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1155 g_assert_cmpint(i64
, ==, 1);
1156 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1157 g_assert_cmpint(i64
, ==, 2);
1158 visit_check_list(v
, &err
);
1159 error_free_or_abort(&err
);
1160 visit_end_list(v
, NULL
);
1162 /* Visit beyond end of list */
1163 v
= visitor_input_test_init(data
, "[]");
1165 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1166 visit_type_int(v
, NULL
, &i64
, &err
);
1167 error_free_or_abort(&err
);
1168 visit_end_list(v
, NULL
);
1171 static void test_visitor_in_fail_list_nested(TestInputVisitorData
*data
,
1178 /* Unvisited nested list tail */
1180 v
= visitor_input_test_init(data
, "[ 0, [ 1, 2, 3 ] ]");
1182 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1183 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1184 g_assert_cmpint(i64
, ==, 0);
1185 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1186 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1187 g_assert_cmpint(i64
, ==, 1);
1188 visit_check_list(v
, &err
);
1189 error_free_or_abort(&err
);
1190 visit_end_list(v
, NULL
);
1191 visit_check_list(v
, &error_abort
);
1192 visit_end_list(v
, NULL
);
1195 static void test_visitor_in_fail_union_list(TestInputVisitorData
*data
,
1198 UserDefListUnion
*tmp
= NULL
;
1202 v
= visitor_input_test_init(data
,
1203 "{ 'type': 'integer', 'data' : [ 'string' ] }");
1205 visit_type_UserDefListUnion(v
, NULL
, &tmp
, &err
);
1206 error_free_or_abort(&err
);
1210 static void test_visitor_in_fail_union_flat(TestInputVisitorData
*data
,
1213 UserDefFlatUnion
*tmp
= NULL
;
1217 v
= visitor_input_test_init(data
, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
1219 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &err
);
1220 error_free_or_abort(&err
);
1224 static void test_visitor_in_fail_union_flat_no_discrim(TestInputVisitorData
*data
,
1227 UserDefFlatUnion2
*tmp
= NULL
;
1231 /* test situation where discriminator field ('enum1' here) is missing */
1232 v
= visitor_input_test_init(data
, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
1234 visit_type_UserDefFlatUnion2(v
, NULL
, &tmp
, &err
);
1235 error_free_or_abort(&err
);
1239 static void test_visitor_in_fail_alternate(TestInputVisitorData
*data
,
1242 UserDefAlternate
*tmp
;
1246 v
= visitor_input_test_init(data
, "3.14");
1248 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
1249 error_free_or_abort(&err
);
1253 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1254 const QLitObject
*qlit
)
1256 SchemaInfoList
*schema
= NULL
;
1257 QObject
*obj
= qobject_from_qlit(qlit
);
1260 v
= qobject_input_visitor_new(obj
);
1262 visit_type_SchemaInfoList(v
, NULL
, &schema
, &error_abort
);
1265 qapi_free_SchemaInfoList(schema
);
1270 static void test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1273 do_test_visitor_in_qmp_introspect(data
, &test_qmp_schema_qlit
);
1276 int main(int argc
, char **argv
)
1278 g_test_init(&argc
, &argv
, NULL
);
1280 input_visitor_test_add("/visitor/input/int",
1281 NULL
, test_visitor_in_int
);
1282 input_visitor_test_add("/visitor/input/uint",
1283 NULL
, test_visitor_in_uint
);
1284 input_visitor_test_add("/visitor/input/int_overflow",
1285 NULL
, test_visitor_in_int_overflow
);
1286 input_visitor_test_add("/visitor/input/int_keyval",
1287 NULL
, test_visitor_in_int_keyval
);
1288 input_visitor_test_add("/visitor/input/int_str_keyval",
1289 NULL
, test_visitor_in_int_str_keyval
);
1290 input_visitor_test_add("/visitor/input/int_str_fail",
1291 NULL
, test_visitor_in_int_str_fail
);
1292 input_visitor_test_add("/visitor/input/bool",
1293 NULL
, test_visitor_in_bool
);
1294 input_visitor_test_add("/visitor/input/bool_keyval",
1295 NULL
, test_visitor_in_bool_keyval
);
1296 input_visitor_test_add("/visitor/input/bool_str_keyval",
1297 NULL
, test_visitor_in_bool_str_keyval
);
1298 input_visitor_test_add("/visitor/input/bool_str_fail",
1299 NULL
, test_visitor_in_bool_str_fail
);
1300 input_visitor_test_add("/visitor/input/number",
1301 NULL
, test_visitor_in_number
);
1302 input_visitor_test_add("/visitor/input/large_number",
1303 NULL
, test_visitor_in_large_number
);
1304 input_visitor_test_add("/visitor/input/number_keyval",
1305 NULL
, test_visitor_in_number_keyval
);
1306 input_visitor_test_add("/visitor/input/number_str_keyval",
1307 NULL
, test_visitor_in_number_str_keyval
);
1308 input_visitor_test_add("/visitor/input/number_str_fail",
1309 NULL
, test_visitor_in_number_str_fail
);
1310 input_visitor_test_add("/visitor/input/size_str_keyval",
1311 NULL
, test_visitor_in_size_str_keyval
);
1312 input_visitor_test_add("/visitor/input/size_str_fail",
1313 NULL
, test_visitor_in_size_str_fail
);
1314 input_visitor_test_add("/visitor/input/string",
1315 NULL
, test_visitor_in_string
);
1316 input_visitor_test_add("/visitor/input/enum",
1317 NULL
, test_visitor_in_enum
);
1318 input_visitor_test_add("/visitor/input/struct",
1319 NULL
, test_visitor_in_struct
);
1320 input_visitor_test_add("/visitor/input/struct-nested",
1321 NULL
, test_visitor_in_struct_nested
);
1322 input_visitor_test_add("/visitor/input/list",
1323 NULL
, test_visitor_in_list
);
1324 input_visitor_test_add("/visitor/input/any",
1325 NULL
, test_visitor_in_any
);
1326 input_visitor_test_add("/visitor/input/null",
1327 NULL
, test_visitor_in_null
);
1328 input_visitor_test_add("/visitor/input/union-flat",
1329 NULL
, test_visitor_in_union_flat
);
1330 input_visitor_test_add("/visitor/input/alternate",
1331 NULL
, test_visitor_in_alternate
);
1332 input_visitor_test_add("/visitor/input/errors",
1333 NULL
, test_visitor_in_errors
);
1334 input_visitor_test_add("/visitor/input/wrong-type",
1335 NULL
, test_visitor_in_wrong_type
);
1336 input_visitor_test_add("/visitor/input/alternate-number",
1337 NULL
, test_visitor_in_alternate_number
);
1338 input_visitor_test_add("/visitor/input/list_union/int",
1339 NULL
, test_visitor_in_list_union_int
);
1340 input_visitor_test_add("/visitor/input/list_union/int8",
1341 NULL
, test_visitor_in_list_union_int8
);
1342 input_visitor_test_add("/visitor/input/list_union/int16",
1343 NULL
, test_visitor_in_list_union_int16
);
1344 input_visitor_test_add("/visitor/input/list_union/int32",
1345 NULL
, test_visitor_in_list_union_int32
);
1346 input_visitor_test_add("/visitor/input/list_union/int64",
1347 NULL
, test_visitor_in_list_union_int64
);
1348 input_visitor_test_add("/visitor/input/list_union/uint8",
1349 NULL
, test_visitor_in_list_union_uint8
);
1350 input_visitor_test_add("/visitor/input/list_union/uint16",
1351 NULL
, test_visitor_in_list_union_uint16
);
1352 input_visitor_test_add("/visitor/input/list_union/uint32",
1353 NULL
, test_visitor_in_list_union_uint32
);
1354 input_visitor_test_add("/visitor/input/list_union/uint64",
1355 NULL
, test_visitor_in_list_union_uint64
);
1356 input_visitor_test_add("/visitor/input/list_union/bool",
1357 NULL
, test_visitor_in_list_union_bool
);
1358 input_visitor_test_add("/visitor/input/list_union/str",
1359 NULL
, test_visitor_in_list_union_string
);
1360 input_visitor_test_add("/visitor/input/list_union/number",
1361 NULL
, test_visitor_in_list_union_number
);
1362 input_visitor_test_add("/visitor/input/fail/struct",
1363 NULL
, test_visitor_in_fail_struct
);
1364 input_visitor_test_add("/visitor/input/fail/struct-nested",
1365 NULL
, test_visitor_in_fail_struct_nested
);
1366 input_visitor_test_add("/visitor/input/fail/struct-in-list",
1367 NULL
, test_visitor_in_fail_struct_in_list
);
1368 input_visitor_test_add("/visitor/input/fail/struct-missing",
1369 NULL
, test_visitor_in_fail_struct_missing
);
1370 input_visitor_test_add("/visitor/input/fail/list",
1371 NULL
, test_visitor_in_fail_list
);
1372 input_visitor_test_add("/visitor/input/fail/list-nested",
1373 NULL
, test_visitor_in_fail_list_nested
);
1374 input_visitor_test_add("/visitor/input/fail/union-flat",
1375 NULL
, test_visitor_in_fail_union_flat
);
1376 input_visitor_test_add("/visitor/input/fail/union-flat-no-discriminator",
1377 NULL
, test_visitor_in_fail_union_flat_no_discrim
);
1378 input_visitor_test_add("/visitor/input/fail/alternate",
1379 NULL
, test_visitor_in_fail_alternate
);
1380 input_visitor_test_add("/visitor/input/fail/union-list",
1381 NULL
, test_visitor_in_fail_union_list
);
1382 input_visitor_test_add("/visitor/input/qapi-introspect",
1383 NULL
, test_visitor_in_qmp_introspect
);