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 g_autoptr(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);
438 static void test_visitor_in_list(TestInputVisitorData
*data
,
441 UserDefOneList
*item
, *head
= NULL
;
445 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
447 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
448 g_assert(head
!= NULL
);
450 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
453 snprintf(string
, sizeof(string
), "string%d", i
);
454 g_assert_cmpstr(item
->value
->string
, ==, string
);
455 g_assert_cmpint(item
->value
->integer
, ==, 42 + i
);
458 qapi_free_UserDefOneList(head
);
461 /* An empty list is valid */
462 v
= visitor_input_test_init(data
, "[]");
463 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
467 static void test_visitor_in_any(TestInputVisitorData
*data
,
479 v
= visitor_input_test_init(data
, "-42");
480 visit_type_any(v
, NULL
, &res
, &error_abort
);
481 qnum
= qobject_to(QNum
, res
);
483 g_assert(qnum_get_try_int(qnum
, &val
));
484 g_assert_cmpint(val
, ==, -42);
487 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
488 visit_type_any(v
, NULL
, &res
, &error_abort
);
489 qdict
= qobject_to(QDict
, res
);
490 g_assert(qdict
&& qdict_size(qdict
) == 3);
491 qobj
= qdict_get(qdict
, "integer");
493 qnum
= qobject_to(QNum
, qobj
);
495 g_assert(qnum_get_try_int(qnum
, &val
));
496 g_assert_cmpint(val
, ==, -42);
497 qobj
= qdict_get(qdict
, "boolean");
499 qbool
= qobject_to(QBool
, qobj
);
501 g_assert(qbool_get_bool(qbool
) == true);
502 qobj
= qdict_get(qdict
, "string");
504 qstring
= qobject_to(QString
, qobj
);
506 g_assert_cmpstr(qstring_get_str(qstring
), ==, "foo");
510 static void test_visitor_in_null(TestInputVisitorData
*data
,
519 * FIXME: Since QAPI doesn't know the 'null' type yet, we can't
520 * test visit_type_null() by reading into a QAPI struct then
521 * checking that it was populated correctly. The best we can do
522 * for now is ensure that we consumed null from the input, proven
523 * by the fact that we can't re-read the key; and that we detect
524 * when input is not null.
527 v
= visitor_input_test_init_full(data
, false,
528 "{ 'a': null, 'b': '' }");
529 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
530 visit_type_null(v
, "a", &null
, &error_abort
);
531 g_assert(qobject_type(QOBJECT(null
)) == QTYPE_QNULL
);
533 visit_type_null(v
, "b", &null
, &err
);
534 error_free_or_abort(&err
);
536 visit_type_str(v
, "c", &tmp
, &err
);
537 error_free_or_abort(&err
);
539 visit_check_struct(v
, &error_abort
);
540 visit_end_struct(v
, NULL
);
543 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
547 g_autoptr(UserDefFlatUnion
) tmp
= NULL
;
548 UserDefUnionBase
*base
;
550 v
= visitor_input_test_init(data
,
551 "{ 'enum1': 'value1', "
554 "'boolean': true }");
556 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &error_abort
);
557 g_assert_cmpint(tmp
->enum1
, ==, ENUM_ONE_VALUE1
);
558 g_assert_cmpstr(tmp
->string
, ==, "str");
559 g_assert_cmpint(tmp
->integer
, ==, 41);
560 g_assert_cmpint(tmp
->u
.value1
.boolean
, ==, true);
562 base
= qapi_UserDefFlatUnion_base(tmp
);
563 g_assert(&base
->enum1
== &tmp
->enum1
);
566 static void test_visitor_in_alternate(TestInputVisitorData
*data
,
570 UserDefAlternate
*tmp
;
573 v
= visitor_input_test_init(data
, "42");
574 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
575 g_assert_cmpint(tmp
->type
, ==, QTYPE_QNUM
);
576 g_assert_cmpint(tmp
->u
.i
, ==, 42);
577 qapi_free_UserDefAlternate(tmp
);
579 v
= visitor_input_test_init(data
, "'value1'");
580 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
581 g_assert_cmpint(tmp
->type
, ==, QTYPE_QSTRING
);
582 g_assert_cmpint(tmp
->u
.e
, ==, ENUM_ONE_VALUE1
);
583 qapi_free_UserDefAlternate(tmp
);
585 v
= visitor_input_test_init(data
, "null");
586 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
587 g_assert_cmpint(tmp
->type
, ==, QTYPE_QNULL
);
588 qapi_free_UserDefAlternate(tmp
);
590 v
= visitor_input_test_init(data
, "{'integer':1, 'string':'str', "
591 "'enum1':'value1', 'boolean':true}");
592 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
593 g_assert_cmpint(tmp
->type
, ==, QTYPE_QDICT
);
594 g_assert_cmpint(tmp
->u
.udfu
.integer
, ==, 1);
595 g_assert_cmpstr(tmp
->u
.udfu
.string
, ==, "str");
596 g_assert_cmpint(tmp
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
597 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.boolean
, ==, true);
598 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
599 qapi_free_UserDefAlternate(tmp
);
601 v
= visitor_input_test_init(data
, "{ 'alt': 42 }");
602 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
603 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QNUM
);
604 g_assert_cmpint(wrap
->alt
->u
.i
, ==, 42);
605 qapi_free_WrapAlternate(wrap
);
607 v
= visitor_input_test_init(data
, "{ 'alt': 'value1' }");
608 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
609 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QSTRING
);
610 g_assert_cmpint(wrap
->alt
->u
.e
, ==, ENUM_ONE_VALUE1
);
611 qapi_free_WrapAlternate(wrap
);
613 v
= visitor_input_test_init(data
, "{ 'alt': {'integer':1, 'string':'str', "
614 "'enum1':'value1', 'boolean':true} }");
615 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
616 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QDICT
);
617 g_assert_cmpint(wrap
->alt
->u
.udfu
.integer
, ==, 1);
618 g_assert_cmpstr(wrap
->alt
->u
.udfu
.string
, ==, "str");
619 g_assert_cmpint(wrap
->alt
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
620 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.boolean
, ==, true);
621 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
622 qapi_free_WrapAlternate(wrap
);
625 static void test_visitor_in_alternate_number(TestInputVisitorData
*data
,
637 v
= visitor_input_test_init(data
, "42");
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");
643 visit_type_AltEnumNum(v
, NULL
, &aen
, &error_abort
);
644 g_assert_cmpint(aen
->type
, ==, QTYPE_QNUM
);
645 g_assert_cmpfloat(aen
->u
.n
, ==, 42);
646 qapi_free_AltEnumNum(aen
);
648 v
= visitor_input_test_init(data
, "42");
649 visit_type_AltNumEnum(v
, NULL
, &ans
, &error_abort
);
650 g_assert_cmpint(ans
->type
, ==, QTYPE_QNUM
);
651 g_assert_cmpfloat(ans
->u
.n
, ==, 42);
652 qapi_free_AltNumEnum(ans
);
654 v
= visitor_input_test_init(data
, "42");
655 visit_type_AltEnumInt(v
, NULL
, &asi
, &error_abort
);
656 g_assert_cmpint(asi
->type
, ==, QTYPE_QNUM
);
657 g_assert_cmpint(asi
->u
.i
, ==, 42);
658 qapi_free_AltEnumInt(asi
);
660 /* Parsing a double */
662 v
= visitor_input_test_init(data
, "42.5");
663 visit_type_AltEnumBool(v
, NULL
, &aeb
, &err
);
664 error_free_or_abort(&err
);
665 qapi_free_AltEnumBool(aeb
);
667 v
= visitor_input_test_init(data
, "42.5");
668 visit_type_AltEnumNum(v
, NULL
, &aen
, &error_abort
);
669 g_assert_cmpint(aen
->type
, ==, QTYPE_QNUM
);
670 g_assert_cmpfloat(aen
->u
.n
, ==, 42.5);
671 qapi_free_AltEnumNum(aen
);
673 v
= visitor_input_test_init(data
, "42.5");
674 visit_type_AltNumEnum(v
, NULL
, &ans
, &error_abort
);
675 g_assert_cmpint(ans
->type
, ==, QTYPE_QNUM
);
676 g_assert_cmpfloat(ans
->u
.n
, ==, 42.5);
677 qapi_free_AltNumEnum(ans
);
679 v
= visitor_input_test_init(data
, "42.5");
680 visit_type_AltEnumInt(v
, NULL
, &asi
, &err
);
681 error_free_or_abort(&err
);
682 qapi_free_AltEnumInt(asi
);
685 static void test_list_union_integer_helper(TestInputVisitorData
*data
,
687 UserDefListUnionKind kind
)
689 g_autoptr(UserDefListUnion
) cvalue
= NULL
;
691 GString
*gstr_list
= g_string_new("");
692 GString
*gstr_union
= g_string_new("");
695 for (i
= 0; i
< 32; i
++) {
696 g_string_append_printf(gstr_list
, "%d", i
);
698 g_string_append(gstr_list
, ", ");
701 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
702 UserDefListUnionKind_str(kind
),
704 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
706 visit_type_UserDefListUnion(v
, NULL
, &cvalue
, &error_abort
);
707 g_assert(cvalue
!= NULL
);
708 g_assert_cmpint(cvalue
->type
, ==, kind
);
711 case USER_DEF_LIST_UNION_KIND_INTEGER
: {
712 intList
*elem
= NULL
;
713 for (i
= 0, elem
= cvalue
->u
.integer
.data
;
714 elem
; elem
= elem
->next
, i
++) {
715 g_assert_cmpint(elem
->value
, ==, i
);
719 case USER_DEF_LIST_UNION_KIND_S8
: {
720 int8List
*elem
= NULL
;
721 for (i
= 0, elem
= cvalue
->u
.s8
.data
; elem
; elem
= elem
->next
, i
++) {
722 g_assert_cmpint(elem
->value
, ==, i
);
726 case USER_DEF_LIST_UNION_KIND_S16
: {
727 int16List
*elem
= NULL
;
728 for (i
= 0, elem
= cvalue
->u
.s16
.data
; elem
; elem
= elem
->next
, i
++) {
729 g_assert_cmpint(elem
->value
, ==, i
);
733 case USER_DEF_LIST_UNION_KIND_S32
: {
734 int32List
*elem
= NULL
;
735 for (i
= 0, elem
= cvalue
->u
.s32
.data
; elem
; elem
= elem
->next
, i
++) {
736 g_assert_cmpint(elem
->value
, ==, i
);
740 case USER_DEF_LIST_UNION_KIND_S64
: {
741 int64List
*elem
= NULL
;
742 for (i
= 0, elem
= cvalue
->u
.s64
.data
; elem
; elem
= elem
->next
, i
++) {
743 g_assert_cmpint(elem
->value
, ==, i
);
747 case USER_DEF_LIST_UNION_KIND_U8
: {
748 uint8List
*elem
= NULL
;
749 for (i
= 0, elem
= cvalue
->u
.u8
.data
; elem
; elem
= elem
->next
, i
++) {
750 g_assert_cmpint(elem
->value
, ==, i
);
754 case USER_DEF_LIST_UNION_KIND_U16
: {
755 uint16List
*elem
= NULL
;
756 for (i
= 0, elem
= cvalue
->u
.u16
.data
; elem
; elem
= elem
->next
, i
++) {
757 g_assert_cmpint(elem
->value
, ==, i
);
761 case USER_DEF_LIST_UNION_KIND_U32
: {
762 uint32List
*elem
= NULL
;
763 for (i
= 0, elem
= cvalue
->u
.u32
.data
; elem
; elem
= elem
->next
, i
++) {
764 g_assert_cmpint(elem
->value
, ==, i
);
768 case USER_DEF_LIST_UNION_KIND_U64
: {
769 uint64List
*elem
= NULL
;
770 for (i
= 0, elem
= cvalue
->u
.u64
.data
; elem
; elem
= elem
->next
, i
++) {
771 g_assert_cmpint(elem
->value
, ==, i
);
776 g_assert_not_reached();
779 g_string_free(gstr_union
, true);
780 g_string_free(gstr_list
, true);
783 static void test_visitor_in_list_union_int(TestInputVisitorData
*data
,
786 test_list_union_integer_helper(data
, unused
,
787 USER_DEF_LIST_UNION_KIND_INTEGER
);
790 static void test_visitor_in_list_union_int8(TestInputVisitorData
*data
,
793 test_list_union_integer_helper(data
, unused
,
794 USER_DEF_LIST_UNION_KIND_S8
);
797 static void test_visitor_in_list_union_int16(TestInputVisitorData
*data
,
800 test_list_union_integer_helper(data
, unused
,
801 USER_DEF_LIST_UNION_KIND_S16
);
804 static void test_visitor_in_list_union_int32(TestInputVisitorData
*data
,
807 test_list_union_integer_helper(data
, unused
,
808 USER_DEF_LIST_UNION_KIND_S32
);
811 static void test_visitor_in_list_union_int64(TestInputVisitorData
*data
,
814 test_list_union_integer_helper(data
, unused
,
815 USER_DEF_LIST_UNION_KIND_S64
);
818 static void test_visitor_in_list_union_uint8(TestInputVisitorData
*data
,
821 test_list_union_integer_helper(data
, unused
,
822 USER_DEF_LIST_UNION_KIND_U8
);
825 static void test_visitor_in_list_union_uint16(TestInputVisitorData
*data
,
828 test_list_union_integer_helper(data
, unused
,
829 USER_DEF_LIST_UNION_KIND_U16
);
832 static void test_visitor_in_list_union_uint32(TestInputVisitorData
*data
,
835 test_list_union_integer_helper(data
, unused
,
836 USER_DEF_LIST_UNION_KIND_U32
);
839 static void test_visitor_in_list_union_uint64(TestInputVisitorData
*data
,
842 test_list_union_integer_helper(data
, unused
,
843 USER_DEF_LIST_UNION_KIND_U64
);
846 static void test_visitor_in_list_union_bool(TestInputVisitorData
*data
,
849 g_autoptr(UserDefListUnion
) cvalue
= NULL
;
850 boolList
*elem
= NULL
;
852 GString
*gstr_list
= g_string_new("");
853 GString
*gstr_union
= g_string_new("");
856 for (i
= 0; i
< 32; i
++) {
857 g_string_append_printf(gstr_list
, "%s",
858 (i
% 3 == 0) ? "true" : "false");
860 g_string_append(gstr_list
, ", ");
863 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
865 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
867 visit_type_UserDefListUnion(v
, NULL
, &cvalue
, &error_abort
);
868 g_assert(cvalue
!= NULL
);
869 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_LIST_UNION_KIND_BOOLEAN
);
871 for (i
= 0, elem
= cvalue
->u
.boolean
.data
; elem
; elem
= elem
->next
, i
++) {
872 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
875 g_string_free(gstr_union
, true);
876 g_string_free(gstr_list
, true);
879 static void test_visitor_in_list_union_string(TestInputVisitorData
*data
,
882 g_autoptr(UserDefListUnion
) cvalue
= NULL
;
883 strList
*elem
= NULL
;
885 GString
*gstr_list
= g_string_new("");
886 GString
*gstr_union
= g_string_new("");
889 for (i
= 0; i
< 32; i
++) {
890 g_string_append_printf(gstr_list
, "'%d'", i
);
892 g_string_append(gstr_list
, ", ");
895 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
897 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
899 visit_type_UserDefListUnion(v
, NULL
, &cvalue
, &error_abort
);
900 g_assert(cvalue
!= NULL
);
901 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_LIST_UNION_KIND_STRING
);
903 for (i
= 0, elem
= cvalue
->u
.string
.data
; elem
; elem
= elem
->next
, i
++) {
905 sprintf(str
, "%d", i
);
906 g_assert_cmpstr(elem
->value
, ==, str
);
909 g_string_free(gstr_union
, true);
910 g_string_free(gstr_list
, true);
913 #define DOUBLE_STR_MAX 16
915 static void test_visitor_in_list_union_number(TestInputVisitorData
*data
,
918 g_autoptr(UserDefListUnion
) cvalue
= NULL
;
919 numberList
*elem
= NULL
;
921 GString
*gstr_list
= g_string_new("");
922 GString
*gstr_union
= g_string_new("");
925 for (i
= 0; i
< 32; i
++) {
926 g_string_append_printf(gstr_list
, "%f", (double)i
/ 3);
928 g_string_append(gstr_list
, ", ");
931 g_string_append_printf(gstr_union
, "{ 'type': 'number', 'data': [ %s ] }",
933 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
935 visit_type_UserDefListUnion(v
, NULL
, &cvalue
, &error_abort
);
936 g_assert(cvalue
!= NULL
);
937 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_LIST_UNION_KIND_NUMBER
);
939 for (i
= 0, elem
= cvalue
->u
.number
.data
; elem
; elem
= elem
->next
, i
++) {
940 GString
*double_expected
= g_string_new("");
941 GString
*double_actual
= g_string_new("");
943 g_string_printf(double_expected
, "%.6f", (double)i
/ 3);
944 g_string_printf(double_actual
, "%.6f", elem
->value
);
945 g_assert_cmpstr(double_expected
->str
, ==, double_actual
->str
);
947 g_string_free(double_expected
, true);
948 g_string_free(double_actual
, true);
951 g_string_free(gstr_union
, true);
952 g_string_free(gstr_list
, true);
955 static void input_visitor_test_add(const char *testpath
,
956 const void *user_data
,
957 void (*test_func
)(TestInputVisitorData
*data
,
958 const void *user_data
))
960 g_test_add(testpath
, TestInputVisitorData
, user_data
, NULL
, test_func
,
961 visitor_input_teardown
);
964 static void test_visitor_in_errors(TestInputVisitorData
*data
,
967 TestStruct
*p
= NULL
;
971 UserDefTwo
*r
= NULL
;
972 WrapAlternate
*s
= NULL
;
974 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', "
977 visit_type_TestStruct(v
, NULL
, &p
, &err
);
978 error_free_or_abort(&err
);
981 v
= visitor_input_test_init(data
, "[ '1', '2', false, '3' ]");
982 visit_type_strList(v
, NULL
, &q
, &err
);
983 error_free_or_abort(&err
);
986 v
= visitor_input_test_init(data
, "{ 'str':'hi' }");
987 visit_type_UserDefTwo(v
, NULL
, &r
, &err
);
988 error_free_or_abort(&err
);
991 v
= visitor_input_test_init(data
, "{ }");
992 visit_type_WrapAlternate(v
, NULL
, &s
, &err
);
993 error_free_or_abort(&err
);
997 static void test_visitor_in_wrong_type(TestInputVisitorData
*data
,
1000 TestStruct
*p
= NULL
;
1006 /* Make sure arrays and structs cannot be confused */
1008 v
= visitor_input_test_init(data
, "[]");
1009 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1010 error_free_or_abort(&err
);
1013 v
= visitor_input_test_init(data
, "{}");
1014 visit_type_strList(v
, NULL
, &q
, &err
);
1015 error_free_or_abort(&err
);
1018 /* Make sure primitives and struct cannot be confused */
1020 v
= visitor_input_test_init(data
, "1");
1021 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1022 error_free_or_abort(&err
);
1025 v
= visitor_input_test_init(data
, "{}");
1026 visit_type_int(v
, NULL
, &i
, &err
);
1027 error_free_or_abort(&err
);
1029 /* Make sure primitives and arrays cannot be confused */
1031 v
= visitor_input_test_init(data
, "1");
1032 visit_type_strList(v
, NULL
, &q
, &err
);
1033 error_free_or_abort(&err
);
1036 v
= visitor_input_test_init(data
, "[]");
1037 visit_type_int(v
, NULL
, &i
, &err
);
1038 error_free_or_abort(&err
);
1041 static void test_visitor_in_fail_struct(TestInputVisitorData
*data
,
1044 TestStruct
*p
= NULL
;
1048 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
1050 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1051 error_free_or_abort(&err
);
1055 static void test_visitor_in_fail_struct_nested(TestInputVisitorData
*data
,
1058 UserDefTwo
*udp
= NULL
;
1062 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
1064 visit_type_UserDefTwo(v
, NULL
, &udp
, &err
);
1065 error_free_or_abort(&err
);
1069 static void test_visitor_in_fail_struct_in_list(TestInputVisitorData
*data
,
1072 UserDefOneList
*head
= NULL
;
1076 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
1078 visit_type_UserDefOneList(v
, NULL
, &head
, &err
);
1079 error_free_or_abort(&err
);
1083 static void test_visitor_in_fail_struct_missing(TestInputVisitorData
*data
,
1090 GenericAlternate
*alt
;
1099 v
= visitor_input_test_init(data
, "{ 'sub': [ {} ] }");
1100 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
1101 visit_start_struct(v
, "struct", NULL
, 0, &err
);
1102 error_free_or_abort(&err
);
1103 visit_start_list(v
, "list", NULL
, 0, &err
);
1104 error_free_or_abort(&err
);
1105 visit_start_alternate(v
, "alternate", &alt
, sizeof(*alt
), &err
);
1106 error_free_or_abort(&err
);
1107 visit_optional(v
, "optional", &present
);
1109 visit_type_enum(v
, "enum", &en
, &EnumOne_lookup
, &err
);
1110 error_free_or_abort(&err
);
1111 visit_type_int(v
, "i64", &i64
, &err
);
1112 error_free_or_abort(&err
);
1113 visit_type_uint32(v
, "u32", &u32
, &err
);
1114 error_free_or_abort(&err
);
1115 visit_type_int8(v
, "i8", &i8
, &err
);
1116 error_free_or_abort(&err
);
1117 visit_type_str(v
, "i8", &str
, &err
);
1118 error_free_or_abort(&err
);
1119 visit_type_number(v
, "dbl", &dbl
, &err
);
1120 error_free_or_abort(&err
);
1121 visit_type_any(v
, "any", &any
, &err
);
1122 error_free_or_abort(&err
);
1123 visit_type_null(v
, "null", &null
, &err
);
1124 error_free_or_abort(&err
);
1125 visit_start_list(v
, "sub", NULL
, 0, &error_abort
);
1126 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
1127 visit_type_int(v
, "i64", &i64
, &err
);
1128 error_free_or_abort(&err
);
1129 visit_end_struct(v
, NULL
);
1130 visit_end_list(v
, NULL
);
1131 visit_end_struct(v
, NULL
);
1134 static void test_visitor_in_fail_list(TestInputVisitorData
*data
,
1141 /* Unvisited list tail */
1143 v
= visitor_input_test_init(data
, "[ 1, 2, 3 ]");
1145 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1146 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1147 g_assert_cmpint(i64
, ==, 1);
1148 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1149 g_assert_cmpint(i64
, ==, 2);
1150 visit_check_list(v
, &err
);
1151 error_free_or_abort(&err
);
1152 visit_end_list(v
, NULL
);
1154 /* Visit beyond end of list */
1155 v
= visitor_input_test_init(data
, "[]");
1157 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1158 visit_type_int(v
, NULL
, &i64
, &err
);
1159 error_free_or_abort(&err
);
1160 visit_end_list(v
, NULL
);
1163 static void test_visitor_in_fail_list_nested(TestInputVisitorData
*data
,
1170 /* Unvisited nested list tail */
1172 v
= visitor_input_test_init(data
, "[ 0, [ 1, 2, 3 ] ]");
1174 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1175 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1176 g_assert_cmpint(i64
, ==, 0);
1177 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1178 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1179 g_assert_cmpint(i64
, ==, 1);
1180 visit_check_list(v
, &err
);
1181 error_free_or_abort(&err
);
1182 visit_end_list(v
, NULL
);
1183 visit_check_list(v
, &error_abort
);
1184 visit_end_list(v
, NULL
);
1187 static void test_visitor_in_fail_union_list(TestInputVisitorData
*data
,
1190 UserDefListUnion
*tmp
= NULL
;
1194 v
= visitor_input_test_init(data
,
1195 "{ 'type': 'integer', 'data' : [ 'string' ] }");
1197 visit_type_UserDefListUnion(v
, NULL
, &tmp
, &err
);
1198 error_free_or_abort(&err
);
1202 static void test_visitor_in_fail_union_flat(TestInputVisitorData
*data
,
1205 UserDefFlatUnion
*tmp
= NULL
;
1209 v
= visitor_input_test_init(data
, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
1211 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &err
);
1212 error_free_or_abort(&err
);
1216 static void test_visitor_in_fail_union_flat_no_discrim(TestInputVisitorData
*data
,
1219 UserDefFlatUnion2
*tmp
= NULL
;
1223 /* test situation where discriminator field ('enum1' here) is missing */
1224 v
= visitor_input_test_init(data
, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
1226 visit_type_UserDefFlatUnion2(v
, NULL
, &tmp
, &err
);
1227 error_free_or_abort(&err
);
1231 static void test_visitor_in_fail_alternate(TestInputVisitorData
*data
,
1234 UserDefAlternate
*tmp
;
1238 v
= visitor_input_test_init(data
, "3.14");
1240 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
1241 error_free_or_abort(&err
);
1245 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1246 const QLitObject
*qlit
)
1248 g_autoptr(SchemaInfoList
) schema
= NULL
;
1249 QObject
*obj
= qobject_from_qlit(qlit
);
1252 v
= qobject_input_visitor_new(obj
);
1254 visit_type_SchemaInfoList(v
, NULL
, &schema
, &error_abort
);
1261 static void test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1264 do_test_visitor_in_qmp_introspect(data
, &test_qmp_schema_qlit
);
1267 int main(int argc
, char **argv
)
1269 g_test_init(&argc
, &argv
, NULL
);
1271 input_visitor_test_add("/visitor/input/int",
1272 NULL
, test_visitor_in_int
);
1273 input_visitor_test_add("/visitor/input/uint",
1274 NULL
, test_visitor_in_uint
);
1275 input_visitor_test_add("/visitor/input/int_overflow",
1276 NULL
, test_visitor_in_int_overflow
);
1277 input_visitor_test_add("/visitor/input/int_keyval",
1278 NULL
, test_visitor_in_int_keyval
);
1279 input_visitor_test_add("/visitor/input/int_str_keyval",
1280 NULL
, test_visitor_in_int_str_keyval
);
1281 input_visitor_test_add("/visitor/input/int_str_fail",
1282 NULL
, test_visitor_in_int_str_fail
);
1283 input_visitor_test_add("/visitor/input/bool",
1284 NULL
, test_visitor_in_bool
);
1285 input_visitor_test_add("/visitor/input/bool_keyval",
1286 NULL
, test_visitor_in_bool_keyval
);
1287 input_visitor_test_add("/visitor/input/bool_str_keyval",
1288 NULL
, test_visitor_in_bool_str_keyval
);
1289 input_visitor_test_add("/visitor/input/bool_str_fail",
1290 NULL
, test_visitor_in_bool_str_fail
);
1291 input_visitor_test_add("/visitor/input/number",
1292 NULL
, test_visitor_in_number
);
1293 input_visitor_test_add("/visitor/input/large_number",
1294 NULL
, test_visitor_in_large_number
);
1295 input_visitor_test_add("/visitor/input/number_keyval",
1296 NULL
, test_visitor_in_number_keyval
);
1297 input_visitor_test_add("/visitor/input/number_str_keyval",
1298 NULL
, test_visitor_in_number_str_keyval
);
1299 input_visitor_test_add("/visitor/input/number_str_fail",
1300 NULL
, test_visitor_in_number_str_fail
);
1301 input_visitor_test_add("/visitor/input/size_str_keyval",
1302 NULL
, test_visitor_in_size_str_keyval
);
1303 input_visitor_test_add("/visitor/input/size_str_fail",
1304 NULL
, test_visitor_in_size_str_fail
);
1305 input_visitor_test_add("/visitor/input/string",
1306 NULL
, test_visitor_in_string
);
1307 input_visitor_test_add("/visitor/input/enum",
1308 NULL
, test_visitor_in_enum
);
1309 input_visitor_test_add("/visitor/input/struct",
1310 NULL
, test_visitor_in_struct
);
1311 input_visitor_test_add("/visitor/input/struct-nested",
1312 NULL
, test_visitor_in_struct_nested
);
1313 input_visitor_test_add("/visitor/input/list",
1314 NULL
, test_visitor_in_list
);
1315 input_visitor_test_add("/visitor/input/any",
1316 NULL
, test_visitor_in_any
);
1317 input_visitor_test_add("/visitor/input/null",
1318 NULL
, test_visitor_in_null
);
1319 input_visitor_test_add("/visitor/input/union-flat",
1320 NULL
, test_visitor_in_union_flat
);
1321 input_visitor_test_add("/visitor/input/alternate",
1322 NULL
, test_visitor_in_alternate
);
1323 input_visitor_test_add("/visitor/input/errors",
1324 NULL
, test_visitor_in_errors
);
1325 input_visitor_test_add("/visitor/input/wrong-type",
1326 NULL
, test_visitor_in_wrong_type
);
1327 input_visitor_test_add("/visitor/input/alternate-number",
1328 NULL
, test_visitor_in_alternate_number
);
1329 input_visitor_test_add("/visitor/input/list_union/int",
1330 NULL
, test_visitor_in_list_union_int
);
1331 input_visitor_test_add("/visitor/input/list_union/int8",
1332 NULL
, test_visitor_in_list_union_int8
);
1333 input_visitor_test_add("/visitor/input/list_union/int16",
1334 NULL
, test_visitor_in_list_union_int16
);
1335 input_visitor_test_add("/visitor/input/list_union/int32",
1336 NULL
, test_visitor_in_list_union_int32
);
1337 input_visitor_test_add("/visitor/input/list_union/int64",
1338 NULL
, test_visitor_in_list_union_int64
);
1339 input_visitor_test_add("/visitor/input/list_union/uint8",
1340 NULL
, test_visitor_in_list_union_uint8
);
1341 input_visitor_test_add("/visitor/input/list_union/uint16",
1342 NULL
, test_visitor_in_list_union_uint16
);
1343 input_visitor_test_add("/visitor/input/list_union/uint32",
1344 NULL
, test_visitor_in_list_union_uint32
);
1345 input_visitor_test_add("/visitor/input/list_union/uint64",
1346 NULL
, test_visitor_in_list_union_uint64
);
1347 input_visitor_test_add("/visitor/input/list_union/bool",
1348 NULL
, test_visitor_in_list_union_bool
);
1349 input_visitor_test_add("/visitor/input/list_union/str",
1350 NULL
, test_visitor_in_list_union_string
);
1351 input_visitor_test_add("/visitor/input/list_union/number",
1352 NULL
, test_visitor_in_list_union_number
);
1353 input_visitor_test_add("/visitor/input/fail/struct",
1354 NULL
, test_visitor_in_fail_struct
);
1355 input_visitor_test_add("/visitor/input/fail/struct-nested",
1356 NULL
, test_visitor_in_fail_struct_nested
);
1357 input_visitor_test_add("/visitor/input/fail/struct-in-list",
1358 NULL
, test_visitor_in_fail_struct_in_list
);
1359 input_visitor_test_add("/visitor/input/fail/struct-missing",
1360 NULL
, test_visitor_in_fail_struct_missing
);
1361 input_visitor_test_add("/visitor/input/fail/list",
1362 NULL
, test_visitor_in_fail_list
);
1363 input_visitor_test_add("/visitor/input/fail/list-nested",
1364 NULL
, test_visitor_in_fail_list_nested
);
1365 input_visitor_test_add("/visitor/input/fail/union-flat",
1366 NULL
, test_visitor_in_fail_union_flat
);
1367 input_visitor_test_add("/visitor/input/fail/union-flat-no-discriminator",
1368 NULL
, test_visitor_in_fail_union_flat_no_discrim
);
1369 input_visitor_test_add("/visitor/input/fail/alternate",
1370 NULL
, test_visitor_in_fail_alternate
);
1371 input_visitor_test_add("/visitor/input/fail/union-list",
1372 NULL
, test_visitor_in_fail_union_list
);
1373 input_visitor_test_add("/visitor/input/qapi-introspect",
1374 NULL
, test_visitor_in_qmp_introspect
);