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()). */
50 static Visitor
*visitor_input_test_init_internal(TestInputVisitorData
*data
,
52 const char *json_string
,
55 visitor_input_teardown(data
, NULL
);
57 data
->obj
= qobject_from_jsonv(json_string
, ap
, &error_abort
);
61 data
->qiv
= qobject_input_visitor_new_keyval(data
->obj
);
63 data
->qiv
= qobject_input_visitor_new(data
->obj
);
69 static GCC_FMT_ATTR(3, 4)
70 Visitor
*visitor_input_test_init_full(TestInputVisitorData
*data
,
72 const char *json_string
, ...)
77 va_start(ap
, json_string
);
78 v
= visitor_input_test_init_internal(data
, keyval
, json_string
, &ap
);
83 static GCC_FMT_ATTR(2, 3)
84 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
85 const char *json_string
, ...)
90 va_start(ap
, json_string
);
91 v
= visitor_input_test_init_internal(data
, false, 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 visitor_input_test_init_internal(data
, false, json_string
, NULL
);
109 static void test_visitor_in_int(TestInputVisitorData
*data
,
117 v
= visitor_input_test_init(data
, "%d", value
);
119 visit_type_int(v
, NULL
, &res
, &error_abort
);
120 g_assert_cmpint(res
, ==, value
);
122 visit_type_number(v
, NULL
, &dbl
, &error_abort
);
123 g_assert_cmpfloat(dbl
, ==, -42.0);
126 static void test_visitor_in_uint(TestInputVisitorData
*data
,
135 v
= visitor_input_test_init(data
, "%d", value
);
137 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
138 g_assert_cmpuint(res
, ==, (uint64_t)value
);
140 visit_type_int(v
, NULL
, &i64
, &error_abort
);
141 g_assert_cmpint(i64
, ==, value
);
143 visit_type_number(v
, NULL
, &dbl
, &error_abort
);
144 g_assert_cmpfloat(dbl
, ==, value
);
146 /* BUG: value between INT64_MIN and -1 accepted modulo 2^64 */
147 v
= visitor_input_test_init(data
, "%d", -value
);
149 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
150 g_assert_cmpuint(res
, ==, (uint64_t)-value
);
152 v
= visitor_input_test_init(data
, "18446744073709551574");
154 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
155 g_assert_cmpuint(res
, ==, 18446744073709551574U);
157 visit_type_number(v
, NULL
, &dbl
, &error_abort
);
158 g_assert_cmpfloat(dbl
, ==, 18446744073709552000.0);
161 static void test_visitor_in_int_overflow(TestInputVisitorData
*data
,
169 * This will overflow a QNUM_I64, so should be deserialized into a
170 * QNUM_DOUBLE field instead, leading to an error if we pass it to
171 * visit_type_int(). Confirm this.
173 v
= visitor_input_test_init(data
, "%f", DBL_MAX
);
175 visit_type_int(v
, NULL
, &res
, &err
);
176 error_free_or_abort(&err
);
179 static void test_visitor_in_int_keyval(TestInputVisitorData
*data
,
182 int64_t res
= 0, value
= -42;
186 v
= visitor_input_test_init_full(data
, true, "%" PRId64
, value
);
187 visit_type_int(v
, NULL
, &res
, &err
);
188 error_free_or_abort(&err
);
191 static void test_visitor_in_int_str_keyval(TestInputVisitorData
*data
,
194 int64_t res
= 0, value
= -42;
197 v
= visitor_input_test_init_full(data
, true, "\"-42\"");
199 visit_type_int(v
, NULL
, &res
, &error_abort
);
200 g_assert_cmpint(res
, ==, value
);
203 static void test_visitor_in_int_str_fail(TestInputVisitorData
*data
,
210 v
= visitor_input_test_init(data
, "\"-42\"");
212 visit_type_int(v
, NULL
, &res
, &err
);
213 error_free_or_abort(&err
);
216 static void test_visitor_in_bool(TestInputVisitorData
*data
,
222 v
= visitor_input_test_init(data
, "true");
224 visit_type_bool(v
, NULL
, &res
, &error_abort
);
225 g_assert_cmpint(res
, ==, true);
228 static void test_visitor_in_bool_keyval(TestInputVisitorData
*data
,
235 v
= visitor_input_test_init_full(data
, true, "true");
237 visit_type_bool(v
, NULL
, &res
, &err
);
238 error_free_or_abort(&err
);
241 static void test_visitor_in_bool_str_keyval(TestInputVisitorData
*data
,
247 v
= visitor_input_test_init_full(data
, true, "\"on\"");
249 visit_type_bool(v
, NULL
, &res
, &error_abort
);
250 g_assert_cmpint(res
, ==, true);
253 static void test_visitor_in_bool_str_fail(TestInputVisitorData
*data
,
260 v
= visitor_input_test_init(data
, "\"true\"");
262 visit_type_bool(v
, NULL
, &res
, &err
);
263 error_free_or_abort(&err
);
266 static void test_visitor_in_number(TestInputVisitorData
*data
,
269 double res
= 0, value
= 3.14;
272 v
= visitor_input_test_init(data
, "%f", value
);
274 visit_type_number(v
, NULL
, &res
, &error_abort
);
275 g_assert_cmpfloat(res
, ==, value
);
278 static void test_visitor_in_large_number(TestInputVisitorData
*data
,
287 v
= visitor_input_test_init(data
, "-18446744073709551616"); /* -2^64 */
289 visit_type_number(v
, NULL
, &res
, &error_abort
);
290 g_assert_cmpfloat(res
, ==, -18446744073709552e3
);
292 visit_type_int(v
, NULL
, &i64
, &err
);
293 error_free_or_abort(&err
);
295 visit_type_uint64(v
, NULL
, &u64
, &err
);
296 error_free_or_abort(&err
);
299 static void test_visitor_in_number_keyval(TestInputVisitorData
*data
,
302 double res
= 0, value
= 3.14;
306 v
= visitor_input_test_init_full(data
, true, "%f", value
);
308 visit_type_number(v
, NULL
, &res
, &err
);
309 error_free_or_abort(&err
);
312 static void test_visitor_in_number_str_keyval(TestInputVisitorData
*data
,
315 double res
= 0, value
= 3.14;
319 v
= visitor_input_test_init_full(data
, true, "\"3.14\"");
321 visit_type_number(v
, NULL
, &res
, &error_abort
);
322 g_assert_cmpfloat(res
, ==, value
);
324 v
= visitor_input_test_init_full(data
, true, "\"inf\"");
326 visit_type_number(v
, NULL
, &res
, &err
);
327 error_free_or_abort(&err
);
330 static void test_visitor_in_number_str_fail(TestInputVisitorData
*data
,
337 v
= visitor_input_test_init(data
, "\"3.14\"");
339 visit_type_number(v
, NULL
, &res
, &err
);
340 error_free_or_abort(&err
);
343 static void test_visitor_in_size_str_keyval(TestInputVisitorData
*data
,
346 uint64_t res
, value
= 500 * 1024 * 1024;
349 v
= visitor_input_test_init_full(data
, true, "\"500M\"");
351 visit_type_size(v
, NULL
, &res
, &error_abort
);
352 g_assert_cmpfloat(res
, ==, value
);
355 static void test_visitor_in_size_str_fail(TestInputVisitorData
*data
,
362 v
= visitor_input_test_init(data
, "\"500M\"");
364 visit_type_size(v
, NULL
, &res
, &err
);
365 error_free_or_abort(&err
);
368 static void test_visitor_in_string(TestInputVisitorData
*data
,
371 char *res
= NULL
, *value
= (char *) "Q E M U";
374 v
= visitor_input_test_init(data
, "%s", value
);
376 visit_type_str(v
, NULL
, &res
, &error_abort
);
377 g_assert_cmpstr(res
, ==, value
);
382 static void test_visitor_in_enum(TestInputVisitorData
*data
,
388 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
391 v
= visitor_input_test_init(data
, "%s", EnumOne_str(i
));
393 visit_type_EnumOne(v
, NULL
, &res
, &error_abort
);
394 g_assert_cmpint(i
, ==, res
);
399 static void test_visitor_in_struct(TestInputVisitorData
*data
,
402 TestStruct
*p
= NULL
;
405 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
407 visit_type_TestStruct(v
, NULL
, &p
, &error_abort
);
408 g_assert_cmpint(p
->integer
, ==, -42);
409 g_assert(p
->boolean
== true);
410 g_assert_cmpstr(p
->string
, ==, "foo");
416 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
419 UserDefTwo
*udp
= NULL
;
422 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', "
423 "'dict1': { 'string1': 'string1', "
424 "'dict2': { 'userdef': { 'integer': 42, "
425 "'string': 'string' }, 'string': 'string2'}}}");
427 visit_type_UserDefTwo(v
, NULL
, &udp
, &error_abort
);
429 g_assert_cmpstr(udp
->string0
, ==, "string0");
430 g_assert_cmpstr(udp
->dict1
->string1
, ==, "string1");
431 g_assert_cmpint(udp
->dict1
->dict2
->userdef
->integer
, ==, 42);
432 g_assert_cmpstr(udp
->dict1
->dict2
->userdef
->string
, ==, "string");
433 g_assert_cmpstr(udp
->dict1
->dict2
->string
, ==, "string2");
434 g_assert(udp
->dict1
->has_dict3
== false);
436 qapi_free_UserDefTwo(udp
);
439 static void test_visitor_in_list(TestInputVisitorData
*data
,
442 UserDefOneList
*item
, *head
= NULL
;
446 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
448 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
449 g_assert(head
!= NULL
);
451 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
454 snprintf(string
, sizeof(string
), "string%d", i
);
455 g_assert_cmpstr(item
->value
->string
, ==, string
);
456 g_assert_cmpint(item
->value
->integer
, ==, 42 + i
);
459 qapi_free_UserDefOneList(head
);
462 /* An empty list is valid */
463 v
= visitor_input_test_init(data
, "[]");
464 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
468 static void test_visitor_in_any(TestInputVisitorData
*data
,
480 v
= visitor_input_test_init(data
, "-42");
481 visit_type_any(v
, NULL
, &res
, &error_abort
);
482 qnum
= qobject_to(QNum
, res
);
484 g_assert(qnum_get_try_int(qnum
, &val
));
485 g_assert_cmpint(val
, ==, -42);
488 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
489 visit_type_any(v
, NULL
, &res
, &error_abort
);
490 qdict
= qobject_to(QDict
, res
);
491 g_assert(qdict
&& qdict_size(qdict
) == 3);
492 qobj
= qdict_get(qdict
, "integer");
494 qnum
= qobject_to(QNum
, qobj
);
496 g_assert(qnum_get_try_int(qnum
, &val
));
497 g_assert_cmpint(val
, ==, -42);
498 qobj
= qdict_get(qdict
, "boolean");
500 qbool
= qobject_to(QBool
, qobj
);
502 g_assert(qbool_get_bool(qbool
) == true);
503 qobj
= qdict_get(qdict
, "string");
505 qstring
= qobject_to(QString
, qobj
);
507 g_assert_cmpstr(qstring_get_str(qstring
), ==, "foo");
511 static void test_visitor_in_null(TestInputVisitorData
*data
,
520 * FIXME: Since QAPI doesn't know the 'null' type yet, we can't
521 * test visit_type_null() by reading into a QAPI struct then
522 * checking that it was populated correctly. The best we can do
523 * for now is ensure that we consumed null from the input, proven
524 * by the fact that we can't re-read the key; and that we detect
525 * when input is not null.
528 v
= visitor_input_test_init_full(data
, false,
529 "{ 'a': null, 'b': '' }");
530 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
531 visit_type_null(v
, "a", &null
, &error_abort
);
532 g_assert(qobject_type(QOBJECT(null
)) == QTYPE_QNULL
);
534 visit_type_null(v
, "b", &null
, &err
);
535 error_free_or_abort(&err
);
537 visit_type_str(v
, "c", &tmp
, &err
);
538 error_free_or_abort(&err
);
540 visit_check_struct(v
, &error_abort
);
541 visit_end_struct(v
, NULL
);
544 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
548 UserDefFlatUnion
*tmp
;
549 UserDefUnionBase
*base
;
551 v
= visitor_input_test_init(data
,
552 "{ 'enum1': 'value1', "
555 "'boolean': true }");
557 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &error_abort
);
558 g_assert_cmpint(tmp
->enum1
, ==, ENUM_ONE_VALUE1
);
559 g_assert_cmpstr(tmp
->string
, ==, "str");
560 g_assert_cmpint(tmp
->integer
, ==, 41);
561 g_assert_cmpint(tmp
->u
.value1
.boolean
, ==, true);
563 base
= qapi_UserDefFlatUnion_base(tmp
);
564 g_assert(&base
->enum1
== &tmp
->enum1
);
566 qapi_free_UserDefFlatUnion(tmp
);
569 static void test_visitor_in_alternate(TestInputVisitorData
*data
,
573 UserDefAlternate
*tmp
;
576 v
= visitor_input_test_init(data
, "42");
577 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
578 g_assert_cmpint(tmp
->type
, ==, QTYPE_QNUM
);
579 g_assert_cmpint(tmp
->u
.i
, ==, 42);
580 qapi_free_UserDefAlternate(tmp
);
582 v
= visitor_input_test_init(data
, "'value1'");
583 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
584 g_assert_cmpint(tmp
->type
, ==, QTYPE_QSTRING
);
585 g_assert_cmpint(tmp
->u
.e
, ==, ENUM_ONE_VALUE1
);
586 qapi_free_UserDefAlternate(tmp
);
588 v
= visitor_input_test_init(data
, "null");
589 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
590 g_assert_cmpint(tmp
->type
, ==, QTYPE_QNULL
);
591 qapi_free_UserDefAlternate(tmp
);
593 v
= visitor_input_test_init(data
, "{'integer':1, 'string':'str', "
594 "'enum1':'value1', 'boolean':true}");
595 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
596 g_assert_cmpint(tmp
->type
, ==, QTYPE_QDICT
);
597 g_assert_cmpint(tmp
->u
.udfu
.integer
, ==, 1);
598 g_assert_cmpstr(tmp
->u
.udfu
.string
, ==, "str");
599 g_assert_cmpint(tmp
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
600 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.boolean
, ==, true);
601 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
602 qapi_free_UserDefAlternate(tmp
);
604 v
= visitor_input_test_init(data
, "{ 'alt': 42 }");
605 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
606 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QNUM
);
607 g_assert_cmpint(wrap
->alt
->u
.i
, ==, 42);
608 qapi_free_WrapAlternate(wrap
);
610 v
= visitor_input_test_init(data
, "{ 'alt': 'value1' }");
611 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
612 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QSTRING
);
613 g_assert_cmpint(wrap
->alt
->u
.e
, ==, ENUM_ONE_VALUE1
);
614 qapi_free_WrapAlternate(wrap
);
616 v
= visitor_input_test_init(data
, "{ 'alt': {'integer':1, 'string':'str', "
617 "'enum1':'value1', 'boolean':true} }");
618 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
619 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QDICT
);
620 g_assert_cmpint(wrap
->alt
->u
.udfu
.integer
, ==, 1);
621 g_assert_cmpstr(wrap
->alt
->u
.udfu
.string
, ==, "str");
622 g_assert_cmpint(wrap
->alt
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
623 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.boolean
, ==, true);
624 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
625 qapi_free_WrapAlternate(wrap
);
628 static void test_visitor_in_alternate_number(TestInputVisitorData
*data
,
640 v
= visitor_input_test_init(data
, "42");
641 visit_type_AltEnumBool(v
, NULL
, &aeb
, &err
);
642 error_free_or_abort(&err
);
643 qapi_free_AltEnumBool(aeb
);
645 v
= visitor_input_test_init(data
, "42");
646 visit_type_AltEnumNum(v
, NULL
, &aen
, &error_abort
);
647 g_assert_cmpint(aen
->type
, ==, QTYPE_QNUM
);
648 g_assert_cmpfloat(aen
->u
.n
, ==, 42);
649 qapi_free_AltEnumNum(aen
);
651 v
= visitor_input_test_init(data
, "42");
652 visit_type_AltNumEnum(v
, NULL
, &ans
, &error_abort
);
653 g_assert_cmpint(ans
->type
, ==, QTYPE_QNUM
);
654 g_assert_cmpfloat(ans
->u
.n
, ==, 42);
655 qapi_free_AltNumEnum(ans
);
657 v
= visitor_input_test_init(data
, "42");
658 visit_type_AltEnumInt(v
, NULL
, &asi
, &error_abort
);
659 g_assert_cmpint(asi
->type
, ==, QTYPE_QNUM
);
660 g_assert_cmpint(asi
->u
.i
, ==, 42);
661 qapi_free_AltEnumInt(asi
);
663 /* Parsing a double */
665 v
= visitor_input_test_init(data
, "42.5");
666 visit_type_AltEnumBool(v
, NULL
, &aeb
, &err
);
667 error_free_or_abort(&err
);
668 qapi_free_AltEnumBool(aeb
);
670 v
= visitor_input_test_init(data
, "42.5");
671 visit_type_AltEnumNum(v
, NULL
, &aen
, &error_abort
);
672 g_assert_cmpint(aen
->type
, ==, QTYPE_QNUM
);
673 g_assert_cmpfloat(aen
->u
.n
, ==, 42.5);
674 qapi_free_AltEnumNum(aen
);
676 v
= visitor_input_test_init(data
, "42.5");
677 visit_type_AltNumEnum(v
, NULL
, &ans
, &error_abort
);
678 g_assert_cmpint(ans
->type
, ==, QTYPE_QNUM
);
679 g_assert_cmpfloat(ans
->u
.n
, ==, 42.5);
680 qapi_free_AltNumEnum(ans
);
682 v
= visitor_input_test_init(data
, "42.5");
683 visit_type_AltEnumInt(v
, NULL
, &asi
, &err
);
684 error_free_or_abort(&err
);
685 qapi_free_AltEnumInt(asi
);
688 static void test_native_list_integer_helper(TestInputVisitorData
*data
,
690 UserDefNativeListUnionKind kind
)
692 UserDefNativeListUnion
*cvalue
= NULL
;
694 GString
*gstr_list
= g_string_new("");
695 GString
*gstr_union
= g_string_new("");
698 for (i
= 0; i
< 32; i
++) {
699 g_string_append_printf(gstr_list
, "%d", i
);
701 g_string_append(gstr_list
, ", ");
704 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
705 UserDefNativeListUnionKind_str(kind
),
707 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
709 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
710 g_assert(cvalue
!= NULL
);
711 g_assert_cmpint(cvalue
->type
, ==, kind
);
714 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
: {
715 intList
*elem
= NULL
;
716 for (i
= 0, elem
= cvalue
->u
.integer
.data
;
717 elem
; elem
= elem
->next
, i
++) {
718 g_assert_cmpint(elem
->value
, ==, i
);
722 case USER_DEF_NATIVE_LIST_UNION_KIND_S8
: {
723 int8List
*elem
= NULL
;
724 for (i
= 0, elem
= cvalue
->u
.s8
.data
; elem
; elem
= elem
->next
, i
++) {
725 g_assert_cmpint(elem
->value
, ==, i
);
729 case USER_DEF_NATIVE_LIST_UNION_KIND_S16
: {
730 int16List
*elem
= NULL
;
731 for (i
= 0, elem
= cvalue
->u
.s16
.data
; elem
; elem
= elem
->next
, i
++) {
732 g_assert_cmpint(elem
->value
, ==, i
);
736 case USER_DEF_NATIVE_LIST_UNION_KIND_S32
: {
737 int32List
*elem
= NULL
;
738 for (i
= 0, elem
= cvalue
->u
.s32
.data
; elem
; elem
= elem
->next
, i
++) {
739 g_assert_cmpint(elem
->value
, ==, i
);
743 case USER_DEF_NATIVE_LIST_UNION_KIND_S64
: {
744 int64List
*elem
= NULL
;
745 for (i
= 0, elem
= cvalue
->u
.s64
.data
; elem
; elem
= elem
->next
, i
++) {
746 g_assert_cmpint(elem
->value
, ==, i
);
750 case USER_DEF_NATIVE_LIST_UNION_KIND_U8
: {
751 uint8List
*elem
= NULL
;
752 for (i
= 0, elem
= cvalue
->u
.u8
.data
; elem
; elem
= elem
->next
, i
++) {
753 g_assert_cmpint(elem
->value
, ==, i
);
757 case USER_DEF_NATIVE_LIST_UNION_KIND_U16
: {
758 uint16List
*elem
= NULL
;
759 for (i
= 0, elem
= cvalue
->u
.u16
.data
; elem
; elem
= elem
->next
, i
++) {
760 g_assert_cmpint(elem
->value
, ==, i
);
764 case USER_DEF_NATIVE_LIST_UNION_KIND_U32
: {
765 uint32List
*elem
= NULL
;
766 for (i
= 0, elem
= cvalue
->u
.u32
.data
; elem
; elem
= elem
->next
, i
++) {
767 g_assert_cmpint(elem
->value
, ==, i
);
771 case USER_DEF_NATIVE_LIST_UNION_KIND_U64
: {
772 uint64List
*elem
= NULL
;
773 for (i
= 0, elem
= cvalue
->u
.u64
.data
; elem
; elem
= elem
->next
, i
++) {
774 g_assert_cmpint(elem
->value
, ==, i
);
779 g_assert_not_reached();
782 g_string_free(gstr_union
, true);
783 g_string_free(gstr_list
, true);
784 qapi_free_UserDefNativeListUnion(cvalue
);
787 static void test_visitor_in_native_list_int(TestInputVisitorData
*data
,
790 test_native_list_integer_helper(data
, unused
,
791 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
);
794 static void test_visitor_in_native_list_int8(TestInputVisitorData
*data
,
797 test_native_list_integer_helper(data
, unused
,
798 USER_DEF_NATIVE_LIST_UNION_KIND_S8
);
801 static void test_visitor_in_native_list_int16(TestInputVisitorData
*data
,
804 test_native_list_integer_helper(data
, unused
,
805 USER_DEF_NATIVE_LIST_UNION_KIND_S16
);
808 static void test_visitor_in_native_list_int32(TestInputVisitorData
*data
,
811 test_native_list_integer_helper(data
, unused
,
812 USER_DEF_NATIVE_LIST_UNION_KIND_S32
);
815 static void test_visitor_in_native_list_int64(TestInputVisitorData
*data
,
818 test_native_list_integer_helper(data
, unused
,
819 USER_DEF_NATIVE_LIST_UNION_KIND_S64
);
822 static void test_visitor_in_native_list_uint8(TestInputVisitorData
*data
,
825 test_native_list_integer_helper(data
, unused
,
826 USER_DEF_NATIVE_LIST_UNION_KIND_U8
);
829 static void test_visitor_in_native_list_uint16(TestInputVisitorData
*data
,
832 test_native_list_integer_helper(data
, unused
,
833 USER_DEF_NATIVE_LIST_UNION_KIND_U16
);
836 static void test_visitor_in_native_list_uint32(TestInputVisitorData
*data
,
839 test_native_list_integer_helper(data
, unused
,
840 USER_DEF_NATIVE_LIST_UNION_KIND_U32
);
843 static void test_visitor_in_native_list_uint64(TestInputVisitorData
*data
,
846 test_native_list_integer_helper(data
, unused
,
847 USER_DEF_NATIVE_LIST_UNION_KIND_U64
);
850 static void test_visitor_in_native_list_bool(TestInputVisitorData
*data
,
853 UserDefNativeListUnion
*cvalue
= NULL
;
854 boolList
*elem
= NULL
;
856 GString
*gstr_list
= g_string_new("");
857 GString
*gstr_union
= g_string_new("");
860 for (i
= 0; i
< 32; i
++) {
861 g_string_append_printf(gstr_list
, "%s",
862 (i
% 3 == 0) ? "true" : "false");
864 g_string_append(gstr_list
, ", ");
867 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
869 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
871 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
872 g_assert(cvalue
!= NULL
);
873 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN
);
875 for (i
= 0, elem
= cvalue
->u
.boolean
.data
; elem
; elem
= elem
->next
, i
++) {
876 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
879 g_string_free(gstr_union
, true);
880 g_string_free(gstr_list
, true);
881 qapi_free_UserDefNativeListUnion(cvalue
);
884 static void test_visitor_in_native_list_string(TestInputVisitorData
*data
,
887 UserDefNativeListUnion
*cvalue
= NULL
;
888 strList
*elem
= NULL
;
890 GString
*gstr_list
= g_string_new("");
891 GString
*gstr_union
= g_string_new("");
894 for (i
= 0; i
< 32; i
++) {
895 g_string_append_printf(gstr_list
, "'%d'", i
);
897 g_string_append(gstr_list
, ", ");
900 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
902 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
904 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
905 g_assert(cvalue
!= NULL
);
906 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING
);
908 for (i
= 0, elem
= cvalue
->u
.string
.data
; elem
; elem
= elem
->next
, i
++) {
910 sprintf(str
, "%d", i
);
911 g_assert_cmpstr(elem
->value
, ==, str
);
914 g_string_free(gstr_union
, true);
915 g_string_free(gstr_list
, true);
916 qapi_free_UserDefNativeListUnion(cvalue
);
919 #define DOUBLE_STR_MAX 16
921 static void test_visitor_in_native_list_number(TestInputVisitorData
*data
,
924 UserDefNativeListUnion
*cvalue
= NULL
;
925 numberList
*elem
= NULL
;
927 GString
*gstr_list
= g_string_new("");
928 GString
*gstr_union
= g_string_new("");
931 for (i
= 0; i
< 32; i
++) {
932 g_string_append_printf(gstr_list
, "%f", (double)i
/ 3);
934 g_string_append(gstr_list
, ", ");
937 g_string_append_printf(gstr_union
, "{ 'type': 'number', 'data': [ %s ] }",
939 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
941 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
942 g_assert(cvalue
!= NULL
);
943 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER
);
945 for (i
= 0, elem
= cvalue
->u
.number
.data
; elem
; elem
= elem
->next
, i
++) {
946 GString
*double_expected
= g_string_new("");
947 GString
*double_actual
= g_string_new("");
949 g_string_printf(double_expected
, "%.6f", (double)i
/ 3);
950 g_string_printf(double_actual
, "%.6f", elem
->value
);
951 g_assert_cmpstr(double_expected
->str
, ==, double_actual
->str
);
953 g_string_free(double_expected
, true);
954 g_string_free(double_actual
, true);
957 g_string_free(gstr_union
, true);
958 g_string_free(gstr_list
, true);
959 qapi_free_UserDefNativeListUnion(cvalue
);
962 static void input_visitor_test_add(const char *testpath
,
963 const void *user_data
,
964 void (*test_func
)(TestInputVisitorData
*data
,
965 const void *user_data
))
967 g_test_add(testpath
, TestInputVisitorData
, user_data
, NULL
, test_func
,
968 visitor_input_teardown
);
971 static void test_visitor_in_errors(TestInputVisitorData
*data
,
974 TestStruct
*p
= NULL
;
978 UserDefTwo
*r
= NULL
;
979 WrapAlternate
*s
= NULL
;
981 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', "
984 visit_type_TestStruct(v
, NULL
, &p
, &err
);
985 error_free_or_abort(&err
);
988 v
= visitor_input_test_init(data
, "[ '1', '2', false, '3' ]");
989 visit_type_strList(v
, NULL
, &q
, &err
);
990 error_free_or_abort(&err
);
993 v
= visitor_input_test_init(data
, "{ 'str':'hi' }");
994 visit_type_UserDefTwo(v
, NULL
, &r
, &err
);
995 error_free_or_abort(&err
);
998 v
= visitor_input_test_init(data
, "{ }");
999 visit_type_WrapAlternate(v
, NULL
, &s
, &err
);
1000 error_free_or_abort(&err
);
1004 static void test_visitor_in_wrong_type(TestInputVisitorData
*data
,
1007 TestStruct
*p
= NULL
;
1013 /* Make sure arrays and structs cannot be confused */
1015 v
= visitor_input_test_init(data
, "[]");
1016 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1017 error_free_or_abort(&err
);
1020 v
= visitor_input_test_init(data
, "{}");
1021 visit_type_strList(v
, NULL
, &q
, &err
);
1022 error_free_or_abort(&err
);
1025 /* Make sure primitives and struct cannot be confused */
1027 v
= visitor_input_test_init(data
, "1");
1028 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1029 error_free_or_abort(&err
);
1032 v
= visitor_input_test_init(data
, "{}");
1033 visit_type_int(v
, NULL
, &i
, &err
);
1034 error_free_or_abort(&err
);
1036 /* Make sure primitives and arrays cannot be confused */
1038 v
= visitor_input_test_init(data
, "1");
1039 visit_type_strList(v
, NULL
, &q
, &err
);
1040 error_free_or_abort(&err
);
1043 v
= visitor_input_test_init(data
, "[]");
1044 visit_type_int(v
, NULL
, &i
, &err
);
1045 error_free_or_abort(&err
);
1048 static void test_visitor_in_fail_struct(TestInputVisitorData
*data
,
1051 TestStruct
*p
= NULL
;
1055 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
1057 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1058 error_free_or_abort(&err
);
1062 static void test_visitor_in_fail_struct_nested(TestInputVisitorData
*data
,
1065 UserDefTwo
*udp
= NULL
;
1069 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
1071 visit_type_UserDefTwo(v
, NULL
, &udp
, &err
);
1072 error_free_or_abort(&err
);
1076 static void test_visitor_in_fail_struct_in_list(TestInputVisitorData
*data
,
1079 UserDefOneList
*head
= NULL
;
1083 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
1085 visit_type_UserDefOneList(v
, NULL
, &head
, &err
);
1086 error_free_or_abort(&err
);
1090 static void test_visitor_in_fail_struct_missing(TestInputVisitorData
*data
,
1097 GenericAlternate
*alt
;
1106 v
= visitor_input_test_init(data
, "{ 'sub': [ {} ] }");
1107 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
1108 visit_start_struct(v
, "struct", NULL
, 0, &err
);
1109 error_free_or_abort(&err
);
1110 visit_start_list(v
, "list", NULL
, 0, &err
);
1111 error_free_or_abort(&err
);
1112 visit_start_alternate(v
, "alternate", &alt
, sizeof(*alt
), &err
);
1113 error_free_or_abort(&err
);
1114 visit_optional(v
, "optional", &present
);
1116 visit_type_enum(v
, "enum", &en
, &EnumOne_lookup
, &err
);
1117 error_free_or_abort(&err
);
1118 visit_type_int(v
, "i64", &i64
, &err
);
1119 error_free_or_abort(&err
);
1120 visit_type_uint32(v
, "u32", &u32
, &err
);
1121 error_free_or_abort(&err
);
1122 visit_type_int8(v
, "i8", &i8
, &err
);
1123 error_free_or_abort(&err
);
1124 visit_type_str(v
, "i8", &str
, &err
);
1125 error_free_or_abort(&err
);
1126 visit_type_number(v
, "dbl", &dbl
, &err
);
1127 error_free_or_abort(&err
);
1128 visit_type_any(v
, "any", &any
, &err
);
1129 error_free_or_abort(&err
);
1130 visit_type_null(v
, "null", &null
, &err
);
1131 error_free_or_abort(&err
);
1132 visit_start_list(v
, "sub", NULL
, 0, &error_abort
);
1133 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
1134 visit_type_int(v
, "i64", &i64
, &err
);
1135 error_free_or_abort(&err
);
1136 visit_end_struct(v
, NULL
);
1137 visit_end_list(v
, NULL
);
1138 visit_end_struct(v
, NULL
);
1141 static void test_visitor_in_fail_list(TestInputVisitorData
*data
,
1148 /* Unvisited list tail */
1150 v
= visitor_input_test_init(data
, "[ 1, 2, 3 ]");
1152 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1153 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1154 g_assert_cmpint(i64
, ==, 1);
1155 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1156 g_assert_cmpint(i64
, ==, 2);
1157 visit_check_list(v
, &err
);
1158 error_free_or_abort(&err
);
1159 visit_end_list(v
, NULL
);
1161 /* Visit beyond end of list */
1162 v
= visitor_input_test_init(data
, "[]");
1164 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1165 visit_type_int(v
, NULL
, &i64
, &err
);
1166 error_free_or_abort(&err
);
1167 visit_end_list(v
, NULL
);
1170 static void test_visitor_in_fail_list_nested(TestInputVisitorData
*data
,
1177 /* Unvisited nested list tail */
1179 v
= visitor_input_test_init(data
, "[ 0, [ 1, 2, 3 ] ]");
1181 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1182 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1183 g_assert_cmpint(i64
, ==, 0);
1184 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1185 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1186 g_assert_cmpint(i64
, ==, 1);
1187 visit_check_list(v
, &err
);
1188 error_free_or_abort(&err
);
1189 visit_end_list(v
, NULL
);
1190 visit_check_list(v
, &error_abort
);
1191 visit_end_list(v
, NULL
);
1194 static void test_visitor_in_fail_union_native_list(TestInputVisitorData
*data
,
1197 UserDefNativeListUnion
*tmp
= NULL
;
1201 v
= visitor_input_test_init(data
,
1202 "{ 'type': 'integer', 'data' : [ 'string' ] }");
1204 visit_type_UserDefNativeListUnion(v
, NULL
, &tmp
, &err
);
1205 error_free_or_abort(&err
);
1209 static void test_visitor_in_fail_union_flat(TestInputVisitorData
*data
,
1212 UserDefFlatUnion
*tmp
= NULL
;
1216 v
= visitor_input_test_init(data
, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
1218 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &err
);
1219 error_free_or_abort(&err
);
1223 static void test_visitor_in_fail_union_flat_no_discrim(TestInputVisitorData
*data
,
1226 UserDefFlatUnion2
*tmp
= NULL
;
1230 /* test situation where discriminator field ('enum1' here) is missing */
1231 v
= visitor_input_test_init(data
, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
1233 visit_type_UserDefFlatUnion2(v
, NULL
, &tmp
, &err
);
1234 error_free_or_abort(&err
);
1238 static void test_visitor_in_fail_alternate(TestInputVisitorData
*data
,
1241 UserDefAlternate
*tmp
;
1245 v
= visitor_input_test_init(data
, "3.14");
1247 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
1248 error_free_or_abort(&err
);
1252 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1253 const QLitObject
*qlit
)
1255 SchemaInfoList
*schema
= NULL
;
1256 QObject
*obj
= qobject_from_qlit(qlit
);
1259 v
= qobject_input_visitor_new(obj
);
1261 visit_type_SchemaInfoList(v
, NULL
, &schema
, &error_abort
);
1264 qapi_free_SchemaInfoList(schema
);
1269 static void test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1272 do_test_visitor_in_qmp_introspect(data
, &test_qmp_schema_qlit
);
1273 do_test_visitor_in_qmp_introspect(data
, &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/native_list/int",
1339 NULL
, test_visitor_in_native_list_int
);
1340 input_visitor_test_add("/visitor/input/native_list/int8",
1341 NULL
, test_visitor_in_native_list_int8
);
1342 input_visitor_test_add("/visitor/input/native_list/int16",
1343 NULL
, test_visitor_in_native_list_int16
);
1344 input_visitor_test_add("/visitor/input/native_list/int32",
1345 NULL
, test_visitor_in_native_list_int32
);
1346 input_visitor_test_add("/visitor/input/native_list/int64",
1347 NULL
, test_visitor_in_native_list_int64
);
1348 input_visitor_test_add("/visitor/input/native_list/uint8",
1349 NULL
, test_visitor_in_native_list_uint8
);
1350 input_visitor_test_add("/visitor/input/native_list/uint16",
1351 NULL
, test_visitor_in_native_list_uint16
);
1352 input_visitor_test_add("/visitor/input/native_list/uint32",
1353 NULL
, test_visitor_in_native_list_uint32
);
1354 input_visitor_test_add("/visitor/input/native_list/uint64",
1355 NULL
, test_visitor_in_native_list_uint64
);
1356 input_visitor_test_add("/visitor/input/native_list/bool",
1357 NULL
, test_visitor_in_native_list_bool
);
1358 input_visitor_test_add("/visitor/input/native_list/str",
1359 NULL
, test_visitor_in_native_list_string
);
1360 input_visitor_test_add("/visitor/input/native_list/number",
1361 NULL
, test_visitor_in_native_list_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-native-list",
1381 NULL
, test_visitor_in_fail_union_native_list
);
1382 input_visitor_test_add("/visitor/input/qapi-introspect",
1383 NULL
, test_visitor_in_qmp_introspect
);