2 * QObject Input Visitor unit-tests.
4 * Copyright (C) 2011-2016 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 * Paolo Bonzini <pbonzini@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/qobject-input-visitor.h"
19 #include "test-qapi-types.h"
20 #include "test-qapi-visit.h"
21 #include "qapi/qmp/types.h"
22 #include "qapi/qmp/qjson.h"
23 #include "test-qmp-introspect.h"
24 #include "qmp-introspect.h"
25 #include "qapi-visit.h"
27 typedef struct TestInputVisitorData
{
30 } TestInputVisitorData
;
32 static void visitor_input_teardown(TestInputVisitorData
*data
,
35 qobject_decref(data
->obj
);
39 visit_free(data
->qiv
);
44 /* The various test_init functions are provided instead of a test setup
45 function so that the JSON string used by the tests are kept in the test
46 functions (and not in main()). */
47 static Visitor
*visitor_input_test_init_internal(TestInputVisitorData
*data
,
49 const char *json_string
,
52 visitor_input_teardown(data
, NULL
);
54 data
->obj
= qobject_from_jsonv(json_string
, ap
, &error_abort
);
58 data
->qiv
= qobject_input_visitor_new_keyval(data
->obj
);
60 data
->qiv
= qobject_input_visitor_new(data
->obj
);
66 static GCC_FMT_ATTR(3, 4)
67 Visitor
*visitor_input_test_init_full(TestInputVisitorData
*data
,
69 const char *json_string
, ...)
74 va_start(ap
, json_string
);
75 v
= visitor_input_test_init_internal(data
, keyval
, json_string
, &ap
);
80 static GCC_FMT_ATTR(2, 3)
81 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
82 const char *json_string
, ...)
87 va_start(ap
, json_string
);
88 v
= visitor_input_test_init_internal(data
, false, json_string
, &ap
);
93 /* similar to visitor_input_test_init(), but does not expect a string
94 * literal/format json_string argument and so can be used for
95 * programatically generated strings (and we can't pass in programatically
96 * generated strings via %s format parameters since qobject_from_jsonv()
97 * will wrap those in double-quotes and treat the entire object as a
100 static Visitor
*visitor_input_test_init_raw(TestInputVisitorData
*data
,
101 const char *json_string
)
103 return visitor_input_test_init_internal(data
, false, json_string
, NULL
);
106 static void test_visitor_in_int(TestInputVisitorData
*data
,
113 v
= visitor_input_test_init(data
, "%d", value
);
115 visit_type_int(v
, NULL
, &res
, &error_abort
);
116 g_assert_cmpint(res
, ==, value
);
119 static void test_visitor_in_int_overflow(TestInputVisitorData
*data
,
126 /* this will overflow a Qint/int64, so should be deserialized into
127 * a QFloat/double field instead, leading to an error if we pass it
128 * to visit_type_int. confirm this.
130 v
= visitor_input_test_init(data
, "%f", DBL_MAX
);
132 visit_type_int(v
, NULL
, &res
, &err
);
133 error_free_or_abort(&err
);
136 static void test_visitor_in_int_keyval(TestInputVisitorData
*data
,
139 int64_t res
= 0, value
= -42;
143 v
= visitor_input_test_init_full(data
, true, "%" PRId64
, value
);
144 visit_type_int(v
, NULL
, &res
, &err
);
145 error_free_or_abort(&err
);
148 static void test_visitor_in_int_str_keyval(TestInputVisitorData
*data
,
151 int64_t res
= 0, value
= -42;
154 v
= visitor_input_test_init_full(data
, true, "\"-42\"");
156 visit_type_int(v
, NULL
, &res
, &error_abort
);
157 g_assert_cmpint(res
, ==, value
);
160 static void test_visitor_in_int_str_fail(TestInputVisitorData
*data
,
167 v
= visitor_input_test_init(data
, "\"-42\"");
169 visit_type_int(v
, NULL
, &res
, &err
);
170 error_free_or_abort(&err
);
173 static void test_visitor_in_bool(TestInputVisitorData
*data
,
179 v
= visitor_input_test_init(data
, "true");
181 visit_type_bool(v
, NULL
, &res
, &error_abort
);
182 g_assert_cmpint(res
, ==, true);
185 static void test_visitor_in_bool_keyval(TestInputVisitorData
*data
,
192 v
= visitor_input_test_init_full(data
, true, "true");
194 visit_type_bool(v
, NULL
, &res
, &err
);
195 error_free_or_abort(&err
);
198 static void test_visitor_in_bool_str_keyval(TestInputVisitorData
*data
,
204 v
= visitor_input_test_init_full(data
, true, "\"on\"");
206 visit_type_bool(v
, NULL
, &res
, &error_abort
);
207 g_assert_cmpint(res
, ==, true);
210 static void test_visitor_in_bool_str_fail(TestInputVisitorData
*data
,
217 v
= visitor_input_test_init(data
, "\"true\"");
219 visit_type_bool(v
, NULL
, &res
, &err
);
220 error_free_or_abort(&err
);
223 static void test_visitor_in_number(TestInputVisitorData
*data
,
226 double res
= 0, value
= 3.14;
229 v
= visitor_input_test_init(data
, "%f", value
);
231 visit_type_number(v
, NULL
, &res
, &error_abort
);
232 g_assert_cmpfloat(res
, ==, value
);
235 static void test_visitor_in_number_keyval(TestInputVisitorData
*data
,
238 double res
= 0, value
= 3.14;
242 v
= visitor_input_test_init_full(data
, true, "%f", value
);
244 visit_type_number(v
, NULL
, &res
, &err
);
245 error_free_or_abort(&err
);
248 static void test_visitor_in_number_str_keyval(TestInputVisitorData
*data
,
251 double res
= 0, value
= 3.14;
254 v
= visitor_input_test_init_full(data
, true, "\"3.14\"");
256 visit_type_number(v
, NULL
, &res
, &error_abort
);
257 g_assert_cmpfloat(res
, ==, value
);
260 static void test_visitor_in_number_str_fail(TestInputVisitorData
*data
,
267 v
= visitor_input_test_init(data
, "\"3.14\"");
269 visit_type_number(v
, NULL
, &res
, &err
);
270 error_free_or_abort(&err
);
273 static void test_visitor_in_size_str_keyval(TestInputVisitorData
*data
,
276 uint64_t res
, value
= 500 * 1024 * 1024;
279 v
= visitor_input_test_init_full(data
, true, "\"500M\"");
281 visit_type_size(v
, NULL
, &res
, &error_abort
);
282 g_assert_cmpfloat(res
, ==, value
);
285 static void test_visitor_in_size_str_fail(TestInputVisitorData
*data
,
292 v
= visitor_input_test_init(data
, "\"500M\"");
294 visit_type_size(v
, NULL
, &res
, &err
);
295 error_free_or_abort(&err
);
298 static void test_visitor_in_string(TestInputVisitorData
*data
,
301 char *res
= NULL
, *value
= (char *) "Q E M U";
304 v
= visitor_input_test_init(data
, "%s", value
);
306 visit_type_str(v
, NULL
, &res
, &error_abort
);
307 g_assert_cmpstr(res
, ==, value
);
312 static void test_visitor_in_enum(TestInputVisitorData
*data
,
318 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
321 v
= visitor_input_test_init(data
, "%s", EnumOne_lookup
[i
]);
323 visit_type_EnumOne(v
, NULL
, &res
, &error_abort
);
324 g_assert_cmpint(i
, ==, res
);
329 static void test_visitor_in_struct(TestInputVisitorData
*data
,
332 TestStruct
*p
= NULL
;
335 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
337 visit_type_TestStruct(v
, NULL
, &p
, &error_abort
);
338 g_assert_cmpint(p
->integer
, ==, -42);
339 g_assert(p
->boolean
== true);
340 g_assert_cmpstr(p
->string
, ==, "foo");
346 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
349 UserDefTwo
*udp
= NULL
;
352 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', "
353 "'dict1': { 'string1': 'string1', "
354 "'dict2': { 'userdef': { 'integer': 42, "
355 "'string': 'string' }, 'string': 'string2'}}}");
357 visit_type_UserDefTwo(v
, NULL
, &udp
, &error_abort
);
359 g_assert_cmpstr(udp
->string0
, ==, "string0");
360 g_assert_cmpstr(udp
->dict1
->string1
, ==, "string1");
361 g_assert_cmpint(udp
->dict1
->dict2
->userdef
->integer
, ==, 42);
362 g_assert_cmpstr(udp
->dict1
->dict2
->userdef
->string
, ==, "string");
363 g_assert_cmpstr(udp
->dict1
->dict2
->string
, ==, "string2");
364 g_assert(udp
->dict1
->has_dict3
== false);
366 qapi_free_UserDefTwo(udp
);
369 static void test_visitor_in_list(TestInputVisitorData
*data
,
372 UserDefOneList
*item
, *head
= NULL
;
376 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
378 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
379 g_assert(head
!= NULL
);
381 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
384 snprintf(string
, sizeof(string
), "string%d", i
);
385 g_assert_cmpstr(item
->value
->string
, ==, string
);
386 g_assert_cmpint(item
->value
->integer
, ==, 42 + i
);
389 qapi_free_UserDefOneList(head
);
392 /* An empty list is valid */
393 v
= visitor_input_test_init(data
, "[]");
394 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
398 static void test_visitor_in_any(TestInputVisitorData
*data
,
409 v
= visitor_input_test_init(data
, "-42");
410 visit_type_any(v
, NULL
, &res
, &error_abort
);
411 qint
= qobject_to_qint(res
);
413 g_assert_cmpint(qint_get_int(qint
), ==, -42);
416 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
417 visit_type_any(v
, NULL
, &res
, &error_abort
);
418 qdict
= qobject_to_qdict(res
);
419 g_assert(qdict
&& qdict_size(qdict
) == 3);
420 qobj
= qdict_get(qdict
, "integer");
422 qint
= qobject_to_qint(qobj
);
424 g_assert_cmpint(qint_get_int(qint
), ==, -42);
425 qobj
= qdict_get(qdict
, "boolean");
427 qbool
= qobject_to_qbool(qobj
);
429 g_assert(qbool_get_bool(qbool
) == true);
430 qobj
= qdict_get(qdict
, "string");
432 qstring
= qobject_to_qstring(qobj
);
434 g_assert_cmpstr(qstring_get_str(qstring
), ==, "foo");
438 static void test_visitor_in_null(TestInputVisitorData
*data
,
446 * FIXME: Since QAPI doesn't know the 'null' type yet, we can't
447 * test visit_type_null() by reading into a QAPI struct then
448 * checking that it was populated correctly. The best we can do
449 * for now is ensure that we consumed null from the input, proven
450 * by the fact that we can't re-read the key; and that we detect
451 * when input is not null.
454 v
= visitor_input_test_init_full(data
, false,
455 "{ 'a': null, 'b': '' }");
456 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
457 visit_type_null(v
, "a", &error_abort
);
458 visit_type_null(v
, "b", &err
);
459 error_free_or_abort(&err
);
460 visit_type_str(v
, "c", &tmp
, &err
);
462 error_free_or_abort(&err
);
463 visit_check_struct(v
, &error_abort
);
464 visit_end_struct(v
, NULL
);
467 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
471 UserDefFlatUnion
*tmp
;
472 UserDefUnionBase
*base
;
474 v
= visitor_input_test_init(data
,
475 "{ 'enum1': 'value1', "
478 "'boolean': true }");
480 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &error_abort
);
481 g_assert_cmpint(tmp
->enum1
, ==, ENUM_ONE_VALUE1
);
482 g_assert_cmpstr(tmp
->string
, ==, "str");
483 g_assert_cmpint(tmp
->integer
, ==, 41);
484 g_assert_cmpint(tmp
->u
.value1
.boolean
, ==, true);
486 base
= qapi_UserDefFlatUnion_base(tmp
);
487 g_assert(&base
->enum1
== &tmp
->enum1
);
489 qapi_free_UserDefFlatUnion(tmp
);
492 static void test_visitor_in_alternate(TestInputVisitorData
*data
,
497 UserDefAlternate
*tmp
;
500 v
= visitor_input_test_init(data
, "42");
501 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
502 g_assert_cmpint(tmp
->type
, ==, QTYPE_QINT
);
503 g_assert_cmpint(tmp
->u
.i
, ==, 42);
504 qapi_free_UserDefAlternate(tmp
);
506 v
= visitor_input_test_init(data
, "'string'");
507 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
508 g_assert_cmpint(tmp
->type
, ==, QTYPE_QSTRING
);
509 g_assert_cmpstr(tmp
->u
.s
, ==, "string");
510 qapi_free_UserDefAlternate(tmp
);
512 v
= visitor_input_test_init(data
, "{'integer':1, 'string':'str', "
513 "'enum1':'value1', 'boolean':true}");
514 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
515 g_assert_cmpint(tmp
->type
, ==, QTYPE_QDICT
);
516 g_assert_cmpint(tmp
->u
.udfu
.integer
, ==, 1);
517 g_assert_cmpstr(tmp
->u
.udfu
.string
, ==, "str");
518 g_assert_cmpint(tmp
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
519 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.boolean
, ==, true);
520 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
521 qapi_free_UserDefAlternate(tmp
);
523 v
= visitor_input_test_init(data
, "false");
524 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
525 error_free_or_abort(&err
);
526 qapi_free_UserDefAlternate(tmp
);
528 v
= visitor_input_test_init(data
, "{ 'alt': 42 }");
529 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
530 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QINT
);
531 g_assert_cmpint(wrap
->alt
->u
.i
, ==, 42);
532 qapi_free_WrapAlternate(wrap
);
534 v
= visitor_input_test_init(data
, "{ 'alt': 'string' }");
535 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
536 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QSTRING
);
537 g_assert_cmpstr(wrap
->alt
->u
.s
, ==, "string");
538 qapi_free_WrapAlternate(wrap
);
540 v
= visitor_input_test_init(data
, "{ 'alt': {'integer':1, 'string':'str', "
541 "'enum1':'value1', 'boolean':true} }");
542 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
543 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QDICT
);
544 g_assert_cmpint(wrap
->alt
->u
.udfu
.integer
, ==, 1);
545 g_assert_cmpstr(wrap
->alt
->u
.udfu
.string
, ==, "str");
546 g_assert_cmpint(wrap
->alt
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
547 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.boolean
, ==, true);
548 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
549 qapi_free_WrapAlternate(wrap
);
552 static void test_visitor_in_alternate_number(TestInputVisitorData
*data
,
566 v
= visitor_input_test_init(data
, "42");
567 visit_type_AltStrBool(v
, NULL
, &asb
, &err
);
568 error_free_or_abort(&err
);
569 qapi_free_AltStrBool(asb
);
571 v
= visitor_input_test_init(data
, "42");
572 visit_type_AltStrNum(v
, NULL
, &asn
, &error_abort
);
573 g_assert_cmpint(asn
->type
, ==, QTYPE_QFLOAT
);
574 g_assert_cmpfloat(asn
->u
.n
, ==, 42);
575 qapi_free_AltStrNum(asn
);
577 v
= visitor_input_test_init(data
, "42");
578 visit_type_AltNumStr(v
, NULL
, &ans
, &error_abort
);
579 g_assert_cmpint(ans
->type
, ==, QTYPE_QFLOAT
);
580 g_assert_cmpfloat(ans
->u
.n
, ==, 42);
581 qapi_free_AltNumStr(ans
);
583 v
= visitor_input_test_init(data
, "42");
584 visit_type_AltStrInt(v
, NULL
, &asi
, &error_abort
);
585 g_assert_cmpint(asi
->type
, ==, QTYPE_QINT
);
586 g_assert_cmpint(asi
->u
.i
, ==, 42);
587 qapi_free_AltStrInt(asi
);
589 v
= visitor_input_test_init(data
, "42");
590 visit_type_AltIntNum(v
, NULL
, &ain
, &error_abort
);
591 g_assert_cmpint(ain
->type
, ==, QTYPE_QINT
);
592 g_assert_cmpint(ain
->u
.i
, ==, 42);
593 qapi_free_AltIntNum(ain
);
595 v
= visitor_input_test_init(data
, "42");
596 visit_type_AltNumInt(v
, NULL
, &ani
, &error_abort
);
597 g_assert_cmpint(ani
->type
, ==, QTYPE_QINT
);
598 g_assert_cmpint(ani
->u
.i
, ==, 42);
599 qapi_free_AltNumInt(ani
);
601 /* Parsing a double */
603 v
= visitor_input_test_init(data
, "42.5");
604 visit_type_AltStrBool(v
, NULL
, &asb
, &err
);
605 error_free_or_abort(&err
);
606 qapi_free_AltStrBool(asb
);
608 v
= visitor_input_test_init(data
, "42.5");
609 visit_type_AltStrNum(v
, NULL
, &asn
, &error_abort
);
610 g_assert_cmpint(asn
->type
, ==, QTYPE_QFLOAT
);
611 g_assert_cmpfloat(asn
->u
.n
, ==, 42.5);
612 qapi_free_AltStrNum(asn
);
614 v
= visitor_input_test_init(data
, "42.5");
615 visit_type_AltNumStr(v
, NULL
, &ans
, &error_abort
);
616 g_assert_cmpint(ans
->type
, ==, QTYPE_QFLOAT
);
617 g_assert_cmpfloat(ans
->u
.n
, ==, 42.5);
618 qapi_free_AltNumStr(ans
);
620 v
= visitor_input_test_init(data
, "42.5");
621 visit_type_AltStrInt(v
, NULL
, &asi
, &err
);
622 error_free_or_abort(&err
);
623 qapi_free_AltStrInt(asi
);
625 v
= visitor_input_test_init(data
, "42.5");
626 visit_type_AltIntNum(v
, NULL
, &ain
, &error_abort
);
627 g_assert_cmpint(ain
->type
, ==, QTYPE_QFLOAT
);
628 g_assert_cmpfloat(ain
->u
.n
, ==, 42.5);
629 qapi_free_AltIntNum(ain
);
631 v
= visitor_input_test_init(data
, "42.5");
632 visit_type_AltNumInt(v
, NULL
, &ani
, &error_abort
);
633 g_assert_cmpint(ani
->type
, ==, QTYPE_QFLOAT
);
634 g_assert_cmpfloat(ani
->u
.n
, ==, 42.5);
635 qapi_free_AltNumInt(ani
);
638 static void test_native_list_integer_helper(TestInputVisitorData
*data
,
640 UserDefNativeListUnionKind kind
)
642 UserDefNativeListUnion
*cvalue
= NULL
;
644 GString
*gstr_list
= g_string_new("");
645 GString
*gstr_union
= g_string_new("");
648 for (i
= 0; i
< 32; i
++) {
649 g_string_append_printf(gstr_list
, "%d", i
);
651 g_string_append(gstr_list
, ", ");
654 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
655 UserDefNativeListUnionKind_lookup
[kind
],
657 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
659 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
660 g_assert(cvalue
!= NULL
);
661 g_assert_cmpint(cvalue
->type
, ==, kind
);
664 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
: {
665 intList
*elem
= NULL
;
666 for (i
= 0, elem
= cvalue
->u
.integer
.data
;
667 elem
; elem
= elem
->next
, i
++) {
668 g_assert_cmpint(elem
->value
, ==, i
);
672 case USER_DEF_NATIVE_LIST_UNION_KIND_S8
: {
673 int8List
*elem
= NULL
;
674 for (i
= 0, elem
= cvalue
->u
.s8
.data
; elem
; elem
= elem
->next
, i
++) {
675 g_assert_cmpint(elem
->value
, ==, i
);
679 case USER_DEF_NATIVE_LIST_UNION_KIND_S16
: {
680 int16List
*elem
= NULL
;
681 for (i
= 0, elem
= cvalue
->u
.s16
.data
; elem
; elem
= elem
->next
, i
++) {
682 g_assert_cmpint(elem
->value
, ==, i
);
686 case USER_DEF_NATIVE_LIST_UNION_KIND_S32
: {
687 int32List
*elem
= NULL
;
688 for (i
= 0, elem
= cvalue
->u
.s32
.data
; elem
; elem
= elem
->next
, i
++) {
689 g_assert_cmpint(elem
->value
, ==, i
);
693 case USER_DEF_NATIVE_LIST_UNION_KIND_S64
: {
694 int64List
*elem
= NULL
;
695 for (i
= 0, elem
= cvalue
->u
.s64
.data
; elem
; elem
= elem
->next
, i
++) {
696 g_assert_cmpint(elem
->value
, ==, i
);
700 case USER_DEF_NATIVE_LIST_UNION_KIND_U8
: {
701 uint8List
*elem
= NULL
;
702 for (i
= 0, elem
= cvalue
->u
.u8
.data
; elem
; elem
= elem
->next
, i
++) {
703 g_assert_cmpint(elem
->value
, ==, i
);
707 case USER_DEF_NATIVE_LIST_UNION_KIND_U16
: {
708 uint16List
*elem
= NULL
;
709 for (i
= 0, elem
= cvalue
->u
.u16
.data
; elem
; elem
= elem
->next
, i
++) {
710 g_assert_cmpint(elem
->value
, ==, i
);
714 case USER_DEF_NATIVE_LIST_UNION_KIND_U32
: {
715 uint32List
*elem
= NULL
;
716 for (i
= 0, elem
= cvalue
->u
.u32
.data
; elem
; elem
= elem
->next
, i
++) {
717 g_assert_cmpint(elem
->value
, ==, i
);
721 case USER_DEF_NATIVE_LIST_UNION_KIND_U64
: {
722 uint64List
*elem
= NULL
;
723 for (i
= 0, elem
= cvalue
->u
.u64
.data
; elem
; elem
= elem
->next
, i
++) {
724 g_assert_cmpint(elem
->value
, ==, i
);
729 g_assert_not_reached();
732 g_string_free(gstr_union
, true);
733 g_string_free(gstr_list
, true);
734 qapi_free_UserDefNativeListUnion(cvalue
);
737 static void test_visitor_in_native_list_int(TestInputVisitorData
*data
,
740 test_native_list_integer_helper(data
, unused
,
741 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
);
744 static void test_visitor_in_native_list_int8(TestInputVisitorData
*data
,
747 test_native_list_integer_helper(data
, unused
,
748 USER_DEF_NATIVE_LIST_UNION_KIND_S8
);
751 static void test_visitor_in_native_list_int16(TestInputVisitorData
*data
,
754 test_native_list_integer_helper(data
, unused
,
755 USER_DEF_NATIVE_LIST_UNION_KIND_S16
);
758 static void test_visitor_in_native_list_int32(TestInputVisitorData
*data
,
761 test_native_list_integer_helper(data
, unused
,
762 USER_DEF_NATIVE_LIST_UNION_KIND_S32
);
765 static void test_visitor_in_native_list_int64(TestInputVisitorData
*data
,
768 test_native_list_integer_helper(data
, unused
,
769 USER_DEF_NATIVE_LIST_UNION_KIND_S64
);
772 static void test_visitor_in_native_list_uint8(TestInputVisitorData
*data
,
775 test_native_list_integer_helper(data
, unused
,
776 USER_DEF_NATIVE_LIST_UNION_KIND_U8
);
779 static void test_visitor_in_native_list_uint16(TestInputVisitorData
*data
,
782 test_native_list_integer_helper(data
, unused
,
783 USER_DEF_NATIVE_LIST_UNION_KIND_U16
);
786 static void test_visitor_in_native_list_uint32(TestInputVisitorData
*data
,
789 test_native_list_integer_helper(data
, unused
,
790 USER_DEF_NATIVE_LIST_UNION_KIND_U32
);
793 static void test_visitor_in_native_list_uint64(TestInputVisitorData
*data
,
796 test_native_list_integer_helper(data
, unused
,
797 USER_DEF_NATIVE_LIST_UNION_KIND_U64
);
800 static void test_visitor_in_native_list_bool(TestInputVisitorData
*data
,
803 UserDefNativeListUnion
*cvalue
= NULL
;
804 boolList
*elem
= NULL
;
806 GString
*gstr_list
= g_string_new("");
807 GString
*gstr_union
= g_string_new("");
810 for (i
= 0; i
< 32; i
++) {
811 g_string_append_printf(gstr_list
, "%s",
812 (i
% 3 == 0) ? "true" : "false");
814 g_string_append(gstr_list
, ", ");
817 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
819 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
821 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
822 g_assert(cvalue
!= NULL
);
823 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN
);
825 for (i
= 0, elem
= cvalue
->u
.boolean
.data
; elem
; elem
= elem
->next
, i
++) {
826 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
829 g_string_free(gstr_union
, true);
830 g_string_free(gstr_list
, true);
831 qapi_free_UserDefNativeListUnion(cvalue
);
834 static void test_visitor_in_native_list_string(TestInputVisitorData
*data
,
837 UserDefNativeListUnion
*cvalue
= NULL
;
838 strList
*elem
= NULL
;
840 GString
*gstr_list
= g_string_new("");
841 GString
*gstr_union
= g_string_new("");
844 for (i
= 0; i
< 32; i
++) {
845 g_string_append_printf(gstr_list
, "'%d'", i
);
847 g_string_append(gstr_list
, ", ");
850 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
852 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
854 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
855 g_assert(cvalue
!= NULL
);
856 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING
);
858 for (i
= 0, elem
= cvalue
->u
.string
.data
; elem
; elem
= elem
->next
, i
++) {
860 sprintf(str
, "%d", i
);
861 g_assert_cmpstr(elem
->value
, ==, str
);
864 g_string_free(gstr_union
, true);
865 g_string_free(gstr_list
, true);
866 qapi_free_UserDefNativeListUnion(cvalue
);
869 #define DOUBLE_STR_MAX 16
871 static void test_visitor_in_native_list_number(TestInputVisitorData
*data
,
874 UserDefNativeListUnion
*cvalue
= NULL
;
875 numberList
*elem
= NULL
;
877 GString
*gstr_list
= g_string_new("");
878 GString
*gstr_union
= g_string_new("");
881 for (i
= 0; i
< 32; i
++) {
882 g_string_append_printf(gstr_list
, "%f", (double)i
/ 3);
884 g_string_append(gstr_list
, ", ");
887 g_string_append_printf(gstr_union
, "{ 'type': 'number', 'data': [ %s ] }",
889 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
891 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
892 g_assert(cvalue
!= NULL
);
893 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER
);
895 for (i
= 0, elem
= cvalue
->u
.number
.data
; elem
; elem
= elem
->next
, i
++) {
896 GString
*double_expected
= g_string_new("");
897 GString
*double_actual
= g_string_new("");
899 g_string_printf(double_expected
, "%.6f", (double)i
/ 3);
900 g_string_printf(double_actual
, "%.6f", elem
->value
);
901 g_assert_cmpstr(double_expected
->str
, ==, double_actual
->str
);
903 g_string_free(double_expected
, true);
904 g_string_free(double_actual
, true);
907 g_string_free(gstr_union
, true);
908 g_string_free(gstr_list
, true);
909 qapi_free_UserDefNativeListUnion(cvalue
);
912 static void input_visitor_test_add(const char *testpath
,
913 const void *user_data
,
914 void (*test_func
)(TestInputVisitorData
*data
,
915 const void *user_data
))
917 g_test_add(testpath
, TestInputVisitorData
, user_data
, NULL
, test_func
,
918 visitor_input_teardown
);
921 static void test_visitor_in_errors(TestInputVisitorData
*data
,
924 TestStruct
*p
= NULL
;
928 UserDefTwo
*r
= NULL
;
929 WrapAlternate
*s
= NULL
;
931 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', "
934 visit_type_TestStruct(v
, NULL
, &p
, &err
);
935 error_free_or_abort(&err
);
938 v
= visitor_input_test_init(data
, "[ '1', '2', false, '3' ]");
939 visit_type_strList(v
, NULL
, &q
, &err
);
940 error_free_or_abort(&err
);
943 v
= visitor_input_test_init(data
, "{ 'str':'hi' }");
944 visit_type_UserDefTwo(v
, NULL
, &r
, &err
);
945 error_free_or_abort(&err
);
948 v
= visitor_input_test_init(data
, "{ }");
949 visit_type_WrapAlternate(v
, NULL
, &s
, &err
);
950 error_free_or_abort(&err
);
954 static void test_visitor_in_wrong_type(TestInputVisitorData
*data
,
957 TestStruct
*p
= NULL
;
963 /* Make sure arrays and structs cannot be confused */
965 v
= visitor_input_test_init(data
, "[]");
966 visit_type_TestStruct(v
, NULL
, &p
, &err
);
967 error_free_or_abort(&err
);
970 v
= visitor_input_test_init(data
, "{}");
971 visit_type_strList(v
, NULL
, &q
, &err
);
972 error_free_or_abort(&err
);
975 /* Make sure primitives and struct cannot be confused */
977 v
= visitor_input_test_init(data
, "1");
978 visit_type_TestStruct(v
, NULL
, &p
, &err
);
979 error_free_or_abort(&err
);
982 v
= visitor_input_test_init(data
, "{}");
983 visit_type_int(v
, NULL
, &i
, &err
);
984 error_free_or_abort(&err
);
986 /* Make sure primitives and arrays cannot be confused */
988 v
= visitor_input_test_init(data
, "1");
989 visit_type_strList(v
, NULL
, &q
, &err
);
990 error_free_or_abort(&err
);
993 v
= visitor_input_test_init(data
, "[]");
994 visit_type_int(v
, NULL
, &i
, &err
);
995 error_free_or_abort(&err
);
998 static void test_visitor_in_fail_struct(TestInputVisitorData
*data
,
1001 TestStruct
*p
= NULL
;
1005 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
1007 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1008 error_free_or_abort(&err
);
1012 static void test_visitor_in_fail_struct_nested(TestInputVisitorData
*data
,
1015 UserDefTwo
*udp
= NULL
;
1019 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
1021 visit_type_UserDefTwo(v
, NULL
, &udp
, &err
);
1022 error_free_or_abort(&err
);
1026 static void test_visitor_in_fail_struct_in_list(TestInputVisitorData
*data
,
1029 UserDefOneList
*head
= NULL
;
1033 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
1035 visit_type_UserDefOneList(v
, NULL
, &head
, &err
);
1036 error_free_or_abort(&err
);
1040 static void test_visitor_in_fail_struct_missing(TestInputVisitorData
*data
,
1046 GenericAlternate
*alt
;
1055 v
= visitor_input_test_init(data
, "{ 'sub': [ {} ] }");
1056 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
1057 visit_start_struct(v
, "struct", NULL
, 0, &err
);
1058 error_free_or_abort(&err
);
1059 visit_start_list(v
, "list", NULL
, 0, &err
);
1060 error_free_or_abort(&err
);
1061 visit_start_alternate(v
, "alternate", &alt
, sizeof(*alt
), false, &err
);
1062 error_free_or_abort(&err
);
1063 visit_optional(v
, "optional", &present
);
1065 visit_type_enum(v
, "enum", &en
, EnumOne_lookup
, &err
);
1066 error_free_or_abort(&err
);
1067 visit_type_int(v
, "i64", &i64
, &err
);
1068 error_free_or_abort(&err
);
1069 visit_type_uint32(v
, "u32", &u32
, &err
);
1070 error_free_or_abort(&err
);
1071 visit_type_int8(v
, "i8", &i8
, &err
);
1072 error_free_or_abort(&err
);
1073 visit_type_str(v
, "i8", &str
, &err
);
1074 error_free_or_abort(&err
);
1075 visit_type_number(v
, "dbl", &dbl
, &err
);
1076 error_free_or_abort(&err
);
1077 visit_type_any(v
, "any", &any
, &err
);
1078 error_free_or_abort(&err
);
1079 visit_type_null(v
, "null", &err
);
1080 error_free_or_abort(&err
);
1081 visit_start_list(v
, "sub", NULL
, 0, &error_abort
);
1082 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
1083 visit_type_int(v
, "i64", &i64
, &err
);
1084 error_free_or_abort(&err
);
1085 visit_end_struct(v
, NULL
);
1086 visit_end_list(v
, NULL
);
1087 visit_end_struct(v
, NULL
);
1090 static void test_visitor_in_fail_list(TestInputVisitorData
*data
,
1097 /* Unvisited list tail */
1099 v
= visitor_input_test_init(data
, "[ 1, 2, 3 ]");
1101 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1102 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1103 g_assert_cmpint(i64
, ==, 1);
1104 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1105 g_assert_cmpint(i64
, ==, 2);
1106 visit_check_list(v
, &err
);
1107 error_free_or_abort(&err
);
1108 visit_end_list(v
, NULL
);
1110 /* Visit beyond end of list */
1111 v
= visitor_input_test_init(data
, "[]");
1113 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1114 visit_type_int(v
, NULL
, &i64
, &err
);
1115 error_free_or_abort(&err
);
1116 visit_end_list(v
, NULL
);
1119 static void test_visitor_in_fail_list_nested(TestInputVisitorData
*data
,
1126 /* Unvisited nested list tail */
1128 v
= visitor_input_test_init(data
, "[ 0, [ 1, 2, 3 ] ]");
1130 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1131 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1132 g_assert_cmpint(i64
, ==, 0);
1133 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1134 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1135 g_assert_cmpint(i64
, ==, 1);
1136 visit_check_list(v
, &err
);
1137 error_free_or_abort(&err
);
1138 visit_end_list(v
, NULL
);
1139 visit_check_list(v
, &error_abort
);
1140 visit_end_list(v
, NULL
);
1143 static void test_visitor_in_fail_union_native_list(TestInputVisitorData
*data
,
1146 UserDefNativeListUnion
*tmp
= NULL
;
1150 v
= visitor_input_test_init(data
,
1151 "{ 'type': 'integer', 'data' : [ 'string' ] }");
1153 visit_type_UserDefNativeListUnion(v
, NULL
, &tmp
, &err
);
1154 error_free_or_abort(&err
);
1158 static void test_visitor_in_fail_union_flat(TestInputVisitorData
*data
,
1161 UserDefFlatUnion
*tmp
= NULL
;
1165 v
= visitor_input_test_init(data
, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
1167 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &err
);
1168 error_free_or_abort(&err
);
1172 static void test_visitor_in_fail_union_flat_no_discrim(TestInputVisitorData
*data
,
1175 UserDefFlatUnion2
*tmp
= NULL
;
1179 /* test situation where discriminator field ('enum1' here) is missing */
1180 v
= visitor_input_test_init(data
, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
1182 visit_type_UserDefFlatUnion2(v
, NULL
, &tmp
, &err
);
1183 error_free_or_abort(&err
);
1187 static void test_visitor_in_fail_alternate(TestInputVisitorData
*data
,
1190 UserDefAlternate
*tmp
;
1194 v
= visitor_input_test_init(data
, "3.14");
1196 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
1197 error_free_or_abort(&err
);
1201 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1202 const char *schema_json
)
1204 SchemaInfoList
*schema
= NULL
;
1207 v
= visitor_input_test_init_raw(data
, schema_json
);
1209 visit_type_SchemaInfoList(v
, NULL
, &schema
, &error_abort
);
1212 qapi_free_SchemaInfoList(schema
);
1215 static void test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1218 do_test_visitor_in_qmp_introspect(data
, test_qmp_schema_json
);
1219 do_test_visitor_in_qmp_introspect(data
, qmp_schema_json
);
1222 int main(int argc
, char **argv
)
1224 g_test_init(&argc
, &argv
, NULL
);
1226 input_visitor_test_add("/visitor/input/int",
1227 NULL
, test_visitor_in_int
);
1228 input_visitor_test_add("/visitor/input/int_overflow",
1229 NULL
, test_visitor_in_int_overflow
);
1230 input_visitor_test_add("/visitor/input/int_keyval",
1231 NULL
, test_visitor_in_int_keyval
);
1232 input_visitor_test_add("/visitor/input/int_str_keyval",
1233 NULL
, test_visitor_in_int_str_keyval
);
1234 input_visitor_test_add("/visitor/input/int_str_fail",
1235 NULL
, test_visitor_in_int_str_fail
);
1236 input_visitor_test_add("/visitor/input/bool",
1237 NULL
, test_visitor_in_bool
);
1238 input_visitor_test_add("/visitor/input/bool_keyval",
1239 NULL
, test_visitor_in_bool_keyval
);
1240 input_visitor_test_add("/visitor/input/bool_str_keyval",
1241 NULL
, test_visitor_in_bool_str_keyval
);
1242 input_visitor_test_add("/visitor/input/bool_str_fail",
1243 NULL
, test_visitor_in_bool_str_fail
);
1244 input_visitor_test_add("/visitor/input/number",
1245 NULL
, test_visitor_in_number
);
1246 input_visitor_test_add("/visitor/input/number_keyval",
1247 NULL
, test_visitor_in_number_keyval
);
1248 input_visitor_test_add("/visitor/input/number_str_keyval",
1249 NULL
, test_visitor_in_number_str_keyval
);
1250 input_visitor_test_add("/visitor/input/number_str_fail",
1251 NULL
, test_visitor_in_number_str_fail
);
1252 input_visitor_test_add("/visitor/input/size_str_keyval",
1253 NULL
, test_visitor_in_size_str_keyval
);
1254 input_visitor_test_add("/visitor/input/size_str_fail",
1255 NULL
, test_visitor_in_size_str_fail
);
1256 input_visitor_test_add("/visitor/input/string",
1257 NULL
, test_visitor_in_string
);
1258 input_visitor_test_add("/visitor/input/enum",
1259 NULL
, test_visitor_in_enum
);
1260 input_visitor_test_add("/visitor/input/struct",
1261 NULL
, test_visitor_in_struct
);
1262 input_visitor_test_add("/visitor/input/struct-nested",
1263 NULL
, test_visitor_in_struct_nested
);
1264 input_visitor_test_add("/visitor/input/list",
1265 NULL
, test_visitor_in_list
);
1266 input_visitor_test_add("/visitor/input/any",
1267 NULL
, test_visitor_in_any
);
1268 input_visitor_test_add("/visitor/input/null",
1269 NULL
, test_visitor_in_null
);
1270 input_visitor_test_add("/visitor/input/union-flat",
1271 NULL
, test_visitor_in_union_flat
);
1272 input_visitor_test_add("/visitor/input/alternate",
1273 NULL
, test_visitor_in_alternate
);
1274 input_visitor_test_add("/visitor/input/errors",
1275 NULL
, test_visitor_in_errors
);
1276 input_visitor_test_add("/visitor/input/wrong-type",
1277 NULL
, test_visitor_in_wrong_type
);
1278 input_visitor_test_add("/visitor/input/alternate-number",
1279 NULL
, test_visitor_in_alternate_number
);
1280 input_visitor_test_add("/visitor/input/native_list/int",
1281 NULL
, test_visitor_in_native_list_int
);
1282 input_visitor_test_add("/visitor/input/native_list/int8",
1283 NULL
, test_visitor_in_native_list_int8
);
1284 input_visitor_test_add("/visitor/input/native_list/int16",
1285 NULL
, test_visitor_in_native_list_int16
);
1286 input_visitor_test_add("/visitor/input/native_list/int32",
1287 NULL
, test_visitor_in_native_list_int32
);
1288 input_visitor_test_add("/visitor/input/native_list/int64",
1289 NULL
, test_visitor_in_native_list_int64
);
1290 input_visitor_test_add("/visitor/input/native_list/uint8",
1291 NULL
, test_visitor_in_native_list_uint8
);
1292 input_visitor_test_add("/visitor/input/native_list/uint16",
1293 NULL
, test_visitor_in_native_list_uint16
);
1294 input_visitor_test_add("/visitor/input/native_list/uint32",
1295 NULL
, test_visitor_in_native_list_uint32
);
1296 input_visitor_test_add("/visitor/input/native_list/uint64",
1297 NULL
, test_visitor_in_native_list_uint64
);
1298 input_visitor_test_add("/visitor/input/native_list/bool",
1299 NULL
, test_visitor_in_native_list_bool
);
1300 input_visitor_test_add("/visitor/input/native_list/str",
1301 NULL
, test_visitor_in_native_list_string
);
1302 input_visitor_test_add("/visitor/input/native_list/number",
1303 NULL
, test_visitor_in_native_list_number
);
1304 input_visitor_test_add("/visitor/input/fail/struct",
1305 NULL
, test_visitor_in_fail_struct
);
1306 input_visitor_test_add("/visitor/input/fail/struct-nested",
1307 NULL
, test_visitor_in_fail_struct_nested
);
1308 input_visitor_test_add("/visitor/input/fail/struct-in-list",
1309 NULL
, test_visitor_in_fail_struct_in_list
);
1310 input_visitor_test_add("/visitor/input/fail/struct-missing",
1311 NULL
, test_visitor_in_fail_struct_missing
);
1312 input_visitor_test_add("/visitor/input/fail/list",
1313 NULL
, test_visitor_in_fail_list
);
1314 input_visitor_test_add("/visitor/input/fail/list-nested",
1315 NULL
, test_visitor_in_fail_list_nested
);
1316 input_visitor_test_add("/visitor/input/fail/union-flat",
1317 NULL
, test_visitor_in_fail_union_flat
);
1318 input_visitor_test_add("/visitor/input/fail/union-flat-no-discriminator",
1319 NULL
, test_visitor_in_fail_union_flat_no_discrim
);
1320 input_visitor_test_add("/visitor/input/fail/alternate",
1321 NULL
, test_visitor_in_fail_alternate
);
1322 input_visitor_test_add("/visitor/input/fail/union-native-list",
1323 NULL
, test_visitor_in_fail_union_native_list
);
1324 input_visitor_test_add("/visitor/input/qmp-introspect",
1325 NULL
, test_visitor_in_qmp_introspect
);