2 * QObject Input Visitor unit-tests.
4 * Copyright (C) 2011-2016 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 * Paolo Bonzini <pbonzini@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/qobject-input-visitor.h"
19 #include "test-qapi-types.h"
20 #include "test-qapi-visit.h"
21 #include "qapi/qmp/types.h"
22 #include "qapi/qmp/qjson.h"
23 #include "test-qmp-introspect.h"
24 #include "qmp-introspect.h"
25 #include "qapi-visit.h"
27 typedef struct TestInputVisitorData
{
30 } TestInputVisitorData
;
32 static void visitor_input_teardown(TestInputVisitorData
*data
,
35 qobject_decref(data
->obj
);
39 visit_free(data
->qiv
);
44 /* The various test_init functions are provided instead of a test setup
45 function so that the JSON string used by the tests are kept in the test
46 functions (and not in main()). */
47 static Visitor
*visitor_input_test_init_internal(TestInputVisitorData
*data
,
49 const char *json_string
,
52 visitor_input_teardown(data
, NULL
);
54 data
->obj
= qobject_from_jsonv(json_string
, ap
, &error_abort
);
58 data
->qiv
= qobject_input_visitor_new_keyval(data
->obj
);
60 data
->qiv
= qobject_input_visitor_new(data
->obj
);
66 static GCC_FMT_ATTR(3, 4)
67 Visitor
*visitor_input_test_init_full(TestInputVisitorData
*data
,
69 const char *json_string
, ...)
74 va_start(ap
, json_string
);
75 v
= visitor_input_test_init_internal(data
, keyval
, json_string
, &ap
);
80 static GCC_FMT_ATTR(2, 3)
81 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
82 const char *json_string
, ...)
87 va_start(ap
, json_string
);
88 v
= visitor_input_test_init_internal(data
, false, json_string
, &ap
);
93 /* similar to visitor_input_test_init(), but does not expect a string
94 * literal/format json_string argument and so can be used for
95 * programatically generated strings (and we can't pass in programatically
96 * generated strings via %s format parameters since qobject_from_jsonv()
97 * will wrap those in double-quotes and treat the entire object as a
100 static Visitor
*visitor_input_test_init_raw(TestInputVisitorData
*data
,
101 const char *json_string
)
103 return visitor_input_test_init_internal(data
, false, json_string
, NULL
);
106 static void test_visitor_in_int(TestInputVisitorData
*data
,
114 v
= visitor_input_test_init(data
, "%d", value
);
116 visit_type_int(v
, NULL
, &res
, &error_abort
);
117 g_assert_cmpint(res
, ==, value
);
119 visit_type_number(v
, NULL
, &dbl
, &error_abort
);
120 g_assert_cmpfloat(dbl
, ==, -42.0);
123 static void test_visitor_in_uint(TestInputVisitorData
*data
,
132 v
= visitor_input_test_init(data
, "%d", value
);
134 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
135 g_assert_cmpuint(res
, ==, (uint64_t)value
);
137 visit_type_int(v
, NULL
, &i64
, &error_abort
);
138 g_assert_cmpint(i64
, ==, value
);
140 visit_type_number(v
, NULL
, &dbl
, &error_abort
);
141 g_assert_cmpfloat(dbl
, ==, value
);
143 /* BUG: value between INT64_MIN and -1 accepted modulo 2^64 */
144 v
= visitor_input_test_init(data
, "%d", -value
);
146 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
147 g_assert_cmpuint(res
, ==, (uint64_t)-value
);
149 v
= visitor_input_test_init(data
, "18446744073709551574");
151 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
152 g_assert_cmpuint(res
, ==, 18446744073709551574U);
154 visit_type_number(v
, NULL
, &dbl
, &error_abort
);
155 g_assert_cmpfloat(dbl
, ==, 18446744073709552000.0);
158 static void test_visitor_in_int_overflow(TestInputVisitorData
*data
,
166 * This will overflow a QNUM_I64, so should be deserialized into a
167 * QNUM_DOUBLE field instead, leading to an error if we pass it to
168 * visit_type_int(). Confirm this.
170 v
= visitor_input_test_init(data
, "%f", DBL_MAX
);
172 visit_type_int(v
, NULL
, &res
, &err
);
173 error_free_or_abort(&err
);
176 static void test_visitor_in_int_keyval(TestInputVisitorData
*data
,
179 int64_t res
= 0, value
= -42;
183 v
= visitor_input_test_init_full(data
, true, "%" PRId64
, value
);
184 visit_type_int(v
, NULL
, &res
, &err
);
185 error_free_or_abort(&err
);
188 static void test_visitor_in_int_str_keyval(TestInputVisitorData
*data
,
191 int64_t res
= 0, value
= -42;
194 v
= visitor_input_test_init_full(data
, true, "\"-42\"");
196 visit_type_int(v
, NULL
, &res
, &error_abort
);
197 g_assert_cmpint(res
, ==, value
);
200 static void test_visitor_in_int_str_fail(TestInputVisitorData
*data
,
207 v
= visitor_input_test_init(data
, "\"-42\"");
209 visit_type_int(v
, NULL
, &res
, &err
);
210 error_free_or_abort(&err
);
213 static void test_visitor_in_bool(TestInputVisitorData
*data
,
219 v
= visitor_input_test_init(data
, "true");
221 visit_type_bool(v
, NULL
, &res
, &error_abort
);
222 g_assert_cmpint(res
, ==, true);
225 static void test_visitor_in_bool_keyval(TestInputVisitorData
*data
,
232 v
= visitor_input_test_init_full(data
, true, "true");
234 visit_type_bool(v
, NULL
, &res
, &err
);
235 error_free_or_abort(&err
);
238 static void test_visitor_in_bool_str_keyval(TestInputVisitorData
*data
,
244 v
= visitor_input_test_init_full(data
, true, "\"on\"");
246 visit_type_bool(v
, NULL
, &res
, &error_abort
);
247 g_assert_cmpint(res
, ==, true);
250 static void test_visitor_in_bool_str_fail(TestInputVisitorData
*data
,
257 v
= visitor_input_test_init(data
, "\"true\"");
259 visit_type_bool(v
, NULL
, &res
, &err
);
260 error_free_or_abort(&err
);
263 static void test_visitor_in_number(TestInputVisitorData
*data
,
266 double res
= 0, value
= 3.14;
269 v
= visitor_input_test_init(data
, "%f", value
);
271 visit_type_number(v
, NULL
, &res
, &error_abort
);
272 g_assert_cmpfloat(res
, ==, value
);
275 static void test_visitor_in_large_number(TestInputVisitorData
*data
,
284 v
= visitor_input_test_init(data
, "-18446744073709551616"); /* -2^64 */
286 visit_type_number(v
, NULL
, &res
, &error_abort
);
287 g_assert_cmpfloat(res
, ==, -18446744073709552e3
);
289 visit_type_int(v
, NULL
, &i64
, &err
);
290 error_free_or_abort(&err
);
292 visit_type_uint64(v
, NULL
, &u64
, &err
);
293 error_free_or_abort(&err
);
296 static void test_visitor_in_number_keyval(TestInputVisitorData
*data
,
299 double res
= 0, value
= 3.14;
303 v
= visitor_input_test_init_full(data
, true, "%f", value
);
305 visit_type_number(v
, NULL
, &res
, &err
);
306 error_free_or_abort(&err
);
309 static void test_visitor_in_number_str_keyval(TestInputVisitorData
*data
,
312 double res
= 0, value
= 3.14;
316 v
= visitor_input_test_init_full(data
, true, "\"3.14\"");
318 visit_type_number(v
, NULL
, &res
, &error_abort
);
319 g_assert_cmpfloat(res
, ==, value
);
321 v
= visitor_input_test_init_full(data
, true, "\"inf\"");
323 visit_type_number(v
, NULL
, &res
, &err
);
324 error_free_or_abort(&err
);
327 static void test_visitor_in_number_str_fail(TestInputVisitorData
*data
,
334 v
= visitor_input_test_init(data
, "\"3.14\"");
336 visit_type_number(v
, NULL
, &res
, &err
);
337 error_free_or_abort(&err
);
340 static void test_visitor_in_size_str_keyval(TestInputVisitorData
*data
,
343 uint64_t res
, value
= 500 * 1024 * 1024;
346 v
= visitor_input_test_init_full(data
, true, "\"500M\"");
348 visit_type_size(v
, NULL
, &res
, &error_abort
);
349 g_assert_cmpfloat(res
, ==, value
);
352 static void test_visitor_in_size_str_fail(TestInputVisitorData
*data
,
359 v
= visitor_input_test_init(data
, "\"500M\"");
361 visit_type_size(v
, NULL
, &res
, &err
);
362 error_free_or_abort(&err
);
365 static void test_visitor_in_string(TestInputVisitorData
*data
,
368 char *res
= NULL
, *value
= (char *) "Q E M U";
371 v
= visitor_input_test_init(data
, "%s", value
);
373 visit_type_str(v
, NULL
, &res
, &error_abort
);
374 g_assert_cmpstr(res
, ==, value
);
379 static void test_visitor_in_enum(TestInputVisitorData
*data
,
385 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
388 v
= visitor_input_test_init(data
, "%s", EnumOne_lookup
[i
]);
390 visit_type_EnumOne(v
, NULL
, &res
, &error_abort
);
391 g_assert_cmpint(i
, ==, res
);
396 static void test_visitor_in_struct(TestInputVisitorData
*data
,
399 TestStruct
*p
= NULL
;
402 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
404 visit_type_TestStruct(v
, NULL
, &p
, &error_abort
);
405 g_assert_cmpint(p
->integer
, ==, -42);
406 g_assert(p
->boolean
== true);
407 g_assert_cmpstr(p
->string
, ==, "foo");
413 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
416 UserDefTwo
*udp
= NULL
;
419 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', "
420 "'dict1': { 'string1': 'string1', "
421 "'dict2': { 'userdef': { 'integer': 42, "
422 "'string': 'string' }, 'string': 'string2'}}}");
424 visit_type_UserDefTwo(v
, NULL
, &udp
, &error_abort
);
426 g_assert_cmpstr(udp
->string0
, ==, "string0");
427 g_assert_cmpstr(udp
->dict1
->string1
, ==, "string1");
428 g_assert_cmpint(udp
->dict1
->dict2
->userdef
->integer
, ==, 42);
429 g_assert_cmpstr(udp
->dict1
->dict2
->userdef
->string
, ==, "string");
430 g_assert_cmpstr(udp
->dict1
->dict2
->string
, ==, "string2");
431 g_assert(udp
->dict1
->has_dict3
== false);
433 qapi_free_UserDefTwo(udp
);
436 static void test_visitor_in_list(TestInputVisitorData
*data
,
439 UserDefOneList
*item
, *head
= NULL
;
443 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
445 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
446 g_assert(head
!= NULL
);
448 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
451 snprintf(string
, sizeof(string
), "string%d", i
);
452 g_assert_cmpstr(item
->value
->string
, ==, string
);
453 g_assert_cmpint(item
->value
->integer
, ==, 42 + i
);
456 qapi_free_UserDefOneList(head
);
459 /* An empty list is valid */
460 v
= visitor_input_test_init(data
, "[]");
461 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
465 static void test_visitor_in_any(TestInputVisitorData
*data
,
477 v
= visitor_input_test_init(data
, "-42");
478 visit_type_any(v
, NULL
, &res
, &error_abort
);
479 qnum
= qobject_to_qnum(res
);
481 g_assert(qnum_get_try_int(qnum
, &val
));
482 g_assert_cmpint(val
, ==, -42);
485 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
486 visit_type_any(v
, NULL
, &res
, &error_abort
);
487 qdict
= qobject_to_qdict(res
);
488 g_assert(qdict
&& qdict_size(qdict
) == 3);
489 qobj
= qdict_get(qdict
, "integer");
491 qnum
= qobject_to_qnum(qobj
);
493 g_assert(qnum_get_try_int(qnum
, &val
));
494 g_assert_cmpint(val
, ==, -42);
495 qobj
= qdict_get(qdict
, "boolean");
497 qbool
= qobject_to_qbool(qobj
);
499 g_assert(qbool_get_bool(qbool
) == true);
500 qobj
= qdict_get(qdict
, "string");
502 qstring
= qobject_to_qstring(qobj
);
504 g_assert_cmpstr(qstring_get_str(qstring
), ==, "foo");
508 static void test_visitor_in_null(TestInputVisitorData
*data
,
516 * FIXME: Since QAPI doesn't know the 'null' type yet, we can't
517 * test visit_type_null() by reading into a QAPI struct then
518 * checking that it was populated correctly. The best we can do
519 * for now is ensure that we consumed null from the input, proven
520 * by the fact that we can't re-read the key; and that we detect
521 * when input is not null.
524 v
= visitor_input_test_init_full(data
, false,
525 "{ 'a': null, 'b': '' }");
526 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
527 visit_type_null(v
, "a", &error_abort
);
528 visit_type_null(v
, "b", &err
);
529 error_free_or_abort(&err
);
530 visit_type_str(v
, "c", &tmp
, &err
);
532 error_free_or_abort(&err
);
533 visit_check_struct(v
, &error_abort
);
534 visit_end_struct(v
, NULL
);
537 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
541 UserDefFlatUnion
*tmp
;
542 UserDefUnionBase
*base
;
544 v
= visitor_input_test_init(data
,
545 "{ 'enum1': 'value1', "
548 "'boolean': true }");
550 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &error_abort
);
551 g_assert_cmpint(tmp
->enum1
, ==, ENUM_ONE_VALUE1
);
552 g_assert_cmpstr(tmp
->string
, ==, "str");
553 g_assert_cmpint(tmp
->integer
, ==, 41);
554 g_assert_cmpint(tmp
->u
.value1
.boolean
, ==, true);
556 base
= qapi_UserDefFlatUnion_base(tmp
);
557 g_assert(&base
->enum1
== &tmp
->enum1
);
559 qapi_free_UserDefFlatUnion(tmp
);
562 static void test_visitor_in_alternate(TestInputVisitorData
*data
,
567 UserDefAlternate
*tmp
;
570 v
= visitor_input_test_init(data
, "42");
571 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
572 g_assert_cmpint(tmp
->type
, ==, QTYPE_QNUM
);
573 g_assert_cmpint(tmp
->u
.i
, ==, 42);
574 qapi_free_UserDefAlternate(tmp
);
576 v
= visitor_input_test_init(data
, "'value1'");
577 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
578 g_assert_cmpint(tmp
->type
, ==, QTYPE_QSTRING
);
579 g_assert_cmpint(tmp
->u
.e
, ==, ENUM_ONE_VALUE1
);
580 qapi_free_UserDefAlternate(tmp
);
582 v
= visitor_input_test_init(data
, "{'integer':1, 'string':'str', "
583 "'enum1':'value1', 'boolean':true}");
584 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
585 g_assert_cmpint(tmp
->type
, ==, QTYPE_QDICT
);
586 g_assert_cmpint(tmp
->u
.udfu
.integer
, ==, 1);
587 g_assert_cmpstr(tmp
->u
.udfu
.string
, ==, "str");
588 g_assert_cmpint(tmp
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
589 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.boolean
, ==, true);
590 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
591 qapi_free_UserDefAlternate(tmp
);
593 v
= visitor_input_test_init(data
, "false");
594 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
595 error_free_or_abort(&err
);
596 qapi_free_UserDefAlternate(tmp
);
598 v
= visitor_input_test_init(data
, "{ 'alt': 42 }");
599 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
600 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QNUM
);
601 g_assert_cmpint(wrap
->alt
->u
.i
, ==, 42);
602 qapi_free_WrapAlternate(wrap
);
604 v
= visitor_input_test_init(data
, "{ 'alt': 'value1' }");
605 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
606 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QSTRING
);
607 g_assert_cmpint(wrap
->alt
->u
.e
, ==, ENUM_ONE_VALUE1
);
608 qapi_free_WrapAlternate(wrap
);
610 v
= visitor_input_test_init(data
, "{ 'alt': {'integer':1, 'string':'str', "
611 "'enum1':'value1', 'boolean':true} }");
612 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
613 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QDICT
);
614 g_assert_cmpint(wrap
->alt
->u
.udfu
.integer
, ==, 1);
615 g_assert_cmpstr(wrap
->alt
->u
.udfu
.string
, ==, "str");
616 g_assert_cmpint(wrap
->alt
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
617 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.boolean
, ==, true);
618 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
619 qapi_free_WrapAlternate(wrap
);
622 static void test_visitor_in_alternate_number(TestInputVisitorData
*data
,
634 v
= visitor_input_test_init(data
, "42");
635 visit_type_AltEnumBool(v
, NULL
, &aeb
, &err
);
636 error_free_or_abort(&err
);
637 qapi_free_AltEnumBool(aeb
);
639 v
= visitor_input_test_init(data
, "42");
640 visit_type_AltEnumNum(v
, NULL
, &aen
, &error_abort
);
641 g_assert_cmpint(aen
->type
, ==, QTYPE_QNUM
);
642 g_assert_cmpfloat(aen
->u
.n
, ==, 42);
643 qapi_free_AltEnumNum(aen
);
645 v
= visitor_input_test_init(data
, "42");
646 visit_type_AltNumEnum(v
, NULL
, &ans
, &error_abort
);
647 g_assert_cmpint(ans
->type
, ==, QTYPE_QNUM
);
648 g_assert_cmpfloat(ans
->u
.n
, ==, 42);
649 qapi_free_AltNumEnum(ans
);
651 v
= visitor_input_test_init(data
, "42");
652 visit_type_AltEnumInt(v
, NULL
, &asi
, &error_abort
);
653 g_assert_cmpint(asi
->type
, ==, QTYPE_QNUM
);
654 g_assert_cmpint(asi
->u
.i
, ==, 42);
655 qapi_free_AltEnumInt(asi
);
657 /* Parsing a double */
659 v
= visitor_input_test_init(data
, "42.5");
660 visit_type_AltEnumBool(v
, NULL
, &aeb
, &err
);
661 error_free_or_abort(&err
);
662 qapi_free_AltEnumBool(aeb
);
664 v
= visitor_input_test_init(data
, "42.5");
665 visit_type_AltEnumNum(v
, NULL
, &aen
, &error_abort
);
666 g_assert_cmpint(aen
->type
, ==, QTYPE_QNUM
);
667 g_assert_cmpfloat(aen
->u
.n
, ==, 42.5);
668 qapi_free_AltEnumNum(aen
);
670 v
= visitor_input_test_init(data
, "42.5");
671 visit_type_AltNumEnum(v
, NULL
, &ans
, &error_abort
);
672 g_assert_cmpint(ans
->type
, ==, QTYPE_QNUM
);
673 g_assert_cmpfloat(ans
->u
.n
, ==, 42.5);
674 qapi_free_AltNumEnum(ans
);
676 v
= visitor_input_test_init(data
, "42.5");
677 visit_type_AltEnumInt(v
, NULL
, &asi
, &err
);
678 error_free_or_abort(&err
);
679 qapi_free_AltEnumInt(asi
);
682 static void test_native_list_integer_helper(TestInputVisitorData
*data
,
684 UserDefNativeListUnionKind kind
)
686 UserDefNativeListUnion
*cvalue
= NULL
;
688 GString
*gstr_list
= g_string_new("");
689 GString
*gstr_union
= g_string_new("");
692 for (i
= 0; i
< 32; i
++) {
693 g_string_append_printf(gstr_list
, "%d", i
);
695 g_string_append(gstr_list
, ", ");
698 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
699 UserDefNativeListUnionKind_lookup
[kind
],
701 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
703 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
704 g_assert(cvalue
!= NULL
);
705 g_assert_cmpint(cvalue
->type
, ==, kind
);
708 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
: {
709 intList
*elem
= NULL
;
710 for (i
= 0, elem
= cvalue
->u
.integer
.data
;
711 elem
; elem
= elem
->next
, i
++) {
712 g_assert_cmpint(elem
->value
, ==, i
);
716 case USER_DEF_NATIVE_LIST_UNION_KIND_S8
: {
717 int8List
*elem
= NULL
;
718 for (i
= 0, elem
= cvalue
->u
.s8
.data
; elem
; elem
= elem
->next
, i
++) {
719 g_assert_cmpint(elem
->value
, ==, i
);
723 case USER_DEF_NATIVE_LIST_UNION_KIND_S16
: {
724 int16List
*elem
= NULL
;
725 for (i
= 0, elem
= cvalue
->u
.s16
.data
; elem
; elem
= elem
->next
, i
++) {
726 g_assert_cmpint(elem
->value
, ==, i
);
730 case USER_DEF_NATIVE_LIST_UNION_KIND_S32
: {
731 int32List
*elem
= NULL
;
732 for (i
= 0, elem
= cvalue
->u
.s32
.data
; elem
; elem
= elem
->next
, i
++) {
733 g_assert_cmpint(elem
->value
, ==, i
);
737 case USER_DEF_NATIVE_LIST_UNION_KIND_S64
: {
738 int64List
*elem
= NULL
;
739 for (i
= 0, elem
= cvalue
->u
.s64
.data
; elem
; elem
= elem
->next
, i
++) {
740 g_assert_cmpint(elem
->value
, ==, i
);
744 case USER_DEF_NATIVE_LIST_UNION_KIND_U8
: {
745 uint8List
*elem
= NULL
;
746 for (i
= 0, elem
= cvalue
->u
.u8
.data
; elem
; elem
= elem
->next
, i
++) {
747 g_assert_cmpint(elem
->value
, ==, i
);
751 case USER_DEF_NATIVE_LIST_UNION_KIND_U16
: {
752 uint16List
*elem
= NULL
;
753 for (i
= 0, elem
= cvalue
->u
.u16
.data
; elem
; elem
= elem
->next
, i
++) {
754 g_assert_cmpint(elem
->value
, ==, i
);
758 case USER_DEF_NATIVE_LIST_UNION_KIND_U32
: {
759 uint32List
*elem
= NULL
;
760 for (i
= 0, elem
= cvalue
->u
.u32
.data
; elem
; elem
= elem
->next
, i
++) {
761 g_assert_cmpint(elem
->value
, ==, i
);
765 case USER_DEF_NATIVE_LIST_UNION_KIND_U64
: {
766 uint64List
*elem
= NULL
;
767 for (i
= 0, elem
= cvalue
->u
.u64
.data
; elem
; elem
= elem
->next
, i
++) {
768 g_assert_cmpint(elem
->value
, ==, i
);
773 g_assert_not_reached();
776 g_string_free(gstr_union
, true);
777 g_string_free(gstr_list
, true);
778 qapi_free_UserDefNativeListUnion(cvalue
);
781 static void test_visitor_in_native_list_int(TestInputVisitorData
*data
,
784 test_native_list_integer_helper(data
, unused
,
785 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
);
788 static void test_visitor_in_native_list_int8(TestInputVisitorData
*data
,
791 test_native_list_integer_helper(data
, unused
,
792 USER_DEF_NATIVE_LIST_UNION_KIND_S8
);
795 static void test_visitor_in_native_list_int16(TestInputVisitorData
*data
,
798 test_native_list_integer_helper(data
, unused
,
799 USER_DEF_NATIVE_LIST_UNION_KIND_S16
);
802 static void test_visitor_in_native_list_int32(TestInputVisitorData
*data
,
805 test_native_list_integer_helper(data
, unused
,
806 USER_DEF_NATIVE_LIST_UNION_KIND_S32
);
809 static void test_visitor_in_native_list_int64(TestInputVisitorData
*data
,
812 test_native_list_integer_helper(data
, unused
,
813 USER_DEF_NATIVE_LIST_UNION_KIND_S64
);
816 static void test_visitor_in_native_list_uint8(TestInputVisitorData
*data
,
819 test_native_list_integer_helper(data
, unused
,
820 USER_DEF_NATIVE_LIST_UNION_KIND_U8
);
823 static void test_visitor_in_native_list_uint16(TestInputVisitorData
*data
,
826 test_native_list_integer_helper(data
, unused
,
827 USER_DEF_NATIVE_LIST_UNION_KIND_U16
);
830 static void test_visitor_in_native_list_uint32(TestInputVisitorData
*data
,
833 test_native_list_integer_helper(data
, unused
,
834 USER_DEF_NATIVE_LIST_UNION_KIND_U32
);
837 static void test_visitor_in_native_list_uint64(TestInputVisitorData
*data
,
840 test_native_list_integer_helper(data
, unused
,
841 USER_DEF_NATIVE_LIST_UNION_KIND_U64
);
844 static void test_visitor_in_native_list_bool(TestInputVisitorData
*data
,
847 UserDefNativeListUnion
*cvalue
= NULL
;
848 boolList
*elem
= NULL
;
850 GString
*gstr_list
= g_string_new("");
851 GString
*gstr_union
= g_string_new("");
854 for (i
= 0; i
< 32; i
++) {
855 g_string_append_printf(gstr_list
, "%s",
856 (i
% 3 == 0) ? "true" : "false");
858 g_string_append(gstr_list
, ", ");
861 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
863 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
865 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
866 g_assert(cvalue
!= NULL
);
867 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN
);
869 for (i
= 0, elem
= cvalue
->u
.boolean
.data
; elem
; elem
= elem
->next
, i
++) {
870 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
873 g_string_free(gstr_union
, true);
874 g_string_free(gstr_list
, true);
875 qapi_free_UserDefNativeListUnion(cvalue
);
878 static void test_visitor_in_native_list_string(TestInputVisitorData
*data
,
881 UserDefNativeListUnion
*cvalue
= NULL
;
882 strList
*elem
= NULL
;
884 GString
*gstr_list
= g_string_new("");
885 GString
*gstr_union
= g_string_new("");
888 for (i
= 0; i
< 32; i
++) {
889 g_string_append_printf(gstr_list
, "'%d'", i
);
891 g_string_append(gstr_list
, ", ");
894 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
896 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
898 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
899 g_assert(cvalue
!= NULL
);
900 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING
);
902 for (i
= 0, elem
= cvalue
->u
.string
.data
; elem
; elem
= elem
->next
, i
++) {
904 sprintf(str
, "%d", i
);
905 g_assert_cmpstr(elem
->value
, ==, str
);
908 g_string_free(gstr_union
, true);
909 g_string_free(gstr_list
, true);
910 qapi_free_UserDefNativeListUnion(cvalue
);
913 #define DOUBLE_STR_MAX 16
915 static void test_visitor_in_native_list_number(TestInputVisitorData
*data
,
918 UserDefNativeListUnion
*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_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
936 g_assert(cvalue
!= NULL
);
937 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_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);
953 qapi_free_UserDefNativeListUnion(cvalue
);
956 static void input_visitor_test_add(const char *testpath
,
957 const void *user_data
,
958 void (*test_func
)(TestInputVisitorData
*data
,
959 const void *user_data
))
961 g_test_add(testpath
, TestInputVisitorData
, user_data
, NULL
, test_func
,
962 visitor_input_teardown
);
965 static void test_visitor_in_errors(TestInputVisitorData
*data
,
968 TestStruct
*p
= NULL
;
972 UserDefTwo
*r
= NULL
;
973 WrapAlternate
*s
= NULL
;
975 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', "
978 visit_type_TestStruct(v
, NULL
, &p
, &err
);
979 error_free_or_abort(&err
);
982 v
= visitor_input_test_init(data
, "[ '1', '2', false, '3' ]");
983 visit_type_strList(v
, NULL
, &q
, &err
);
984 error_free_or_abort(&err
);
987 v
= visitor_input_test_init(data
, "{ 'str':'hi' }");
988 visit_type_UserDefTwo(v
, NULL
, &r
, &err
);
989 error_free_or_abort(&err
);
992 v
= visitor_input_test_init(data
, "{ }");
993 visit_type_WrapAlternate(v
, NULL
, &s
, &err
);
994 error_free_or_abort(&err
);
998 static void test_visitor_in_wrong_type(TestInputVisitorData
*data
,
1001 TestStruct
*p
= NULL
;
1007 /* Make sure arrays and structs cannot be confused */
1009 v
= visitor_input_test_init(data
, "[]");
1010 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1011 error_free_or_abort(&err
);
1014 v
= visitor_input_test_init(data
, "{}");
1015 visit_type_strList(v
, NULL
, &q
, &err
);
1016 error_free_or_abort(&err
);
1019 /* Make sure primitives and struct cannot be confused */
1021 v
= visitor_input_test_init(data
, "1");
1022 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1023 error_free_or_abort(&err
);
1026 v
= visitor_input_test_init(data
, "{}");
1027 visit_type_int(v
, NULL
, &i
, &err
);
1028 error_free_or_abort(&err
);
1030 /* Make sure primitives and arrays cannot be confused */
1032 v
= visitor_input_test_init(data
, "1");
1033 visit_type_strList(v
, NULL
, &q
, &err
);
1034 error_free_or_abort(&err
);
1037 v
= visitor_input_test_init(data
, "[]");
1038 visit_type_int(v
, NULL
, &i
, &err
);
1039 error_free_or_abort(&err
);
1042 static void test_visitor_in_fail_struct(TestInputVisitorData
*data
,
1045 TestStruct
*p
= NULL
;
1049 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
1051 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1052 error_free_or_abort(&err
);
1056 static void test_visitor_in_fail_struct_nested(TestInputVisitorData
*data
,
1059 UserDefTwo
*udp
= NULL
;
1063 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
1065 visit_type_UserDefTwo(v
, NULL
, &udp
, &err
);
1066 error_free_or_abort(&err
);
1070 static void test_visitor_in_fail_struct_in_list(TestInputVisitorData
*data
,
1073 UserDefOneList
*head
= NULL
;
1077 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
1079 visit_type_UserDefOneList(v
, NULL
, &head
, &err
);
1080 error_free_or_abort(&err
);
1084 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", &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_native_list(TestInputVisitorData
*data
,
1190 UserDefNativeListUnion
*tmp
= NULL
;
1194 v
= visitor_input_test_init(data
,
1195 "{ 'type': 'integer', 'data' : [ 'string' ] }");
1197 visit_type_UserDefNativeListUnion(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 char *schema_json
)
1248 SchemaInfoList
*schema
= NULL
;
1251 v
= visitor_input_test_init_raw(data
, schema_json
);
1253 visit_type_SchemaInfoList(v
, NULL
, &schema
, &error_abort
);
1256 qapi_free_SchemaInfoList(schema
);
1259 static void test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1262 do_test_visitor_in_qmp_introspect(data
, test_qmp_schema_json
);
1263 do_test_visitor_in_qmp_introspect(data
, qmp_schema_json
);
1266 int main(int argc
, char **argv
)
1268 g_test_init(&argc
, &argv
, NULL
);
1270 input_visitor_test_add("/visitor/input/int",
1271 NULL
, test_visitor_in_int
);
1272 input_visitor_test_add("/visitor/input/uint",
1273 NULL
, test_visitor_in_uint
);
1274 input_visitor_test_add("/visitor/input/int_overflow",
1275 NULL
, test_visitor_in_int_overflow
);
1276 input_visitor_test_add("/visitor/input/int_keyval",
1277 NULL
, test_visitor_in_int_keyval
);
1278 input_visitor_test_add("/visitor/input/int_str_keyval",
1279 NULL
, test_visitor_in_int_str_keyval
);
1280 input_visitor_test_add("/visitor/input/int_str_fail",
1281 NULL
, test_visitor_in_int_str_fail
);
1282 input_visitor_test_add("/visitor/input/bool",
1283 NULL
, test_visitor_in_bool
);
1284 input_visitor_test_add("/visitor/input/bool_keyval",
1285 NULL
, test_visitor_in_bool_keyval
);
1286 input_visitor_test_add("/visitor/input/bool_str_keyval",
1287 NULL
, test_visitor_in_bool_str_keyval
);
1288 input_visitor_test_add("/visitor/input/bool_str_fail",
1289 NULL
, test_visitor_in_bool_str_fail
);
1290 input_visitor_test_add("/visitor/input/number",
1291 NULL
, test_visitor_in_number
);
1292 input_visitor_test_add("/visitor/input/large_number",
1293 NULL
, test_visitor_in_large_number
);
1294 input_visitor_test_add("/visitor/input/number_keyval",
1295 NULL
, test_visitor_in_number_keyval
);
1296 input_visitor_test_add("/visitor/input/number_str_keyval",
1297 NULL
, test_visitor_in_number_str_keyval
);
1298 input_visitor_test_add("/visitor/input/number_str_fail",
1299 NULL
, test_visitor_in_number_str_fail
);
1300 input_visitor_test_add("/visitor/input/size_str_keyval",
1301 NULL
, test_visitor_in_size_str_keyval
);
1302 input_visitor_test_add("/visitor/input/size_str_fail",
1303 NULL
, test_visitor_in_size_str_fail
);
1304 input_visitor_test_add("/visitor/input/string",
1305 NULL
, test_visitor_in_string
);
1306 input_visitor_test_add("/visitor/input/enum",
1307 NULL
, test_visitor_in_enum
);
1308 input_visitor_test_add("/visitor/input/struct",
1309 NULL
, test_visitor_in_struct
);
1310 input_visitor_test_add("/visitor/input/struct-nested",
1311 NULL
, test_visitor_in_struct_nested
);
1312 input_visitor_test_add("/visitor/input/list",
1313 NULL
, test_visitor_in_list
);
1314 input_visitor_test_add("/visitor/input/any",
1315 NULL
, test_visitor_in_any
);
1316 input_visitor_test_add("/visitor/input/null",
1317 NULL
, test_visitor_in_null
);
1318 input_visitor_test_add("/visitor/input/union-flat",
1319 NULL
, test_visitor_in_union_flat
);
1320 input_visitor_test_add("/visitor/input/alternate",
1321 NULL
, test_visitor_in_alternate
);
1322 input_visitor_test_add("/visitor/input/errors",
1323 NULL
, test_visitor_in_errors
);
1324 input_visitor_test_add("/visitor/input/wrong-type",
1325 NULL
, test_visitor_in_wrong_type
);
1326 input_visitor_test_add("/visitor/input/alternate-number",
1327 NULL
, test_visitor_in_alternate_number
);
1328 input_visitor_test_add("/visitor/input/native_list/int",
1329 NULL
, test_visitor_in_native_list_int
);
1330 input_visitor_test_add("/visitor/input/native_list/int8",
1331 NULL
, test_visitor_in_native_list_int8
);
1332 input_visitor_test_add("/visitor/input/native_list/int16",
1333 NULL
, test_visitor_in_native_list_int16
);
1334 input_visitor_test_add("/visitor/input/native_list/int32",
1335 NULL
, test_visitor_in_native_list_int32
);
1336 input_visitor_test_add("/visitor/input/native_list/int64",
1337 NULL
, test_visitor_in_native_list_int64
);
1338 input_visitor_test_add("/visitor/input/native_list/uint8",
1339 NULL
, test_visitor_in_native_list_uint8
);
1340 input_visitor_test_add("/visitor/input/native_list/uint16",
1341 NULL
, test_visitor_in_native_list_uint16
);
1342 input_visitor_test_add("/visitor/input/native_list/uint32",
1343 NULL
, test_visitor_in_native_list_uint32
);
1344 input_visitor_test_add("/visitor/input/native_list/uint64",
1345 NULL
, test_visitor_in_native_list_uint64
);
1346 input_visitor_test_add("/visitor/input/native_list/bool",
1347 NULL
, test_visitor_in_native_list_bool
);
1348 input_visitor_test_add("/visitor/input/native_list/str",
1349 NULL
, test_visitor_in_native_list_string
);
1350 input_visitor_test_add("/visitor/input/native_list/number",
1351 NULL
, test_visitor_in_native_list_number
);
1352 input_visitor_test_add("/visitor/input/fail/struct",
1353 NULL
, test_visitor_in_fail_struct
);
1354 input_visitor_test_add("/visitor/input/fail/struct-nested",
1355 NULL
, test_visitor_in_fail_struct_nested
);
1356 input_visitor_test_add("/visitor/input/fail/struct-in-list",
1357 NULL
, test_visitor_in_fail_struct_in_list
);
1358 input_visitor_test_add("/visitor/input/fail/struct-missing",
1359 NULL
, test_visitor_in_fail_struct_missing
);
1360 input_visitor_test_add("/visitor/input/fail/list",
1361 NULL
, test_visitor_in_fail_list
);
1362 input_visitor_test_add("/visitor/input/fail/list-nested",
1363 NULL
, test_visitor_in_fail_list_nested
);
1364 input_visitor_test_add("/visitor/input/fail/union-flat",
1365 NULL
, test_visitor_in_fail_union_flat
);
1366 input_visitor_test_add("/visitor/input/fail/union-flat-no-discriminator",
1367 NULL
, test_visitor_in_fail_union_flat_no_discrim
);
1368 input_visitor_test_add("/visitor/input/fail/alternate",
1369 NULL
, test_visitor_in_fail_alternate
);
1370 input_visitor_test_add("/visitor/input/fail/union-native-list",
1371 NULL
, test_visitor_in_fail_union_native_list
);
1372 input_visitor_test_add("/visitor/input/qmp-introspect",
1373 NULL
, test_visitor_in_qmp_introspect
);