2 * QObject Input Visitor unit-tests.
4 * Copyright (C) 2011-2016 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 * Paolo Bonzini <pbonzini@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/qapi-visit-introspect.h"
19 #include "qapi/qobject-input-visitor.h"
20 #include "test-qapi-visit.h"
21 #include "qapi/qmp/qbool.h"
22 #include "qapi/qmp/qdict.h"
23 #include "qapi/qmp/qnull.h"
24 #include "qapi/qmp/qnum.h"
25 #include "qapi/qmp/qstring.h"
26 #include "qapi/qmp/qjson.h"
27 #include "test-qapi-introspect.h"
28 #include "qapi/qapi-introspect.h"
30 typedef struct TestInputVisitorData
{
33 } TestInputVisitorData
;
35 static void visitor_input_teardown(TestInputVisitorData
*data
,
38 qobject_unref(data
->obj
);
42 visit_free(data
->qiv
);
47 /* The various test_init functions are provided instead of a test setup
48 function so that the JSON string used by the tests are kept in the test
49 functions (and not in main()). */
51 static Visitor
*test_init_internal(TestInputVisitorData
*data
, bool keyval
,
54 visitor_input_teardown(data
, NULL
);
59 data
->qiv
= qobject_input_visitor_new_keyval(data
->obj
);
61 data
->qiv
= qobject_input_visitor_new(data
->obj
);
67 static GCC_FMT_ATTR(3, 4)
68 Visitor
*visitor_input_test_init_full(TestInputVisitorData
*data
,
70 const char *json_string
, ...)
75 va_start(ap
, json_string
);
76 v
= test_init_internal(data
, keyval
,
77 qobject_from_vjsonf_nofail(json_string
, ap
));
82 static GCC_FMT_ATTR(2, 3)
83 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
84 const char *json_string
, ...)
89 va_start(ap
, json_string
);
90 v
= test_init_internal(data
, false,
91 qobject_from_vjsonf_nofail(json_string
, ap
));
96 /* similar to visitor_input_test_init(), but does not expect a string
97 * literal/format json_string argument and so can be used for
98 * programatically generated strings (and we can't pass in programatically
99 * generated strings via %s format parameters since qobject_from_jsonv()
100 * will wrap those in double-quotes and treat the entire object as a
103 static Visitor
*visitor_input_test_init_raw(TestInputVisitorData
*data
,
104 const char *json_string
)
106 return test_init_internal(data
, false,
107 qobject_from_json(json_string
, &error_abort
));
110 static void test_visitor_in_int(TestInputVisitorData
*data
,
118 v
= visitor_input_test_init(data
, "%d", value
);
120 visit_type_int(v
, NULL
, &res
, &error_abort
);
121 g_assert_cmpint(res
, ==, value
);
123 visit_type_number(v
, NULL
, &dbl
, &error_abort
);
124 g_assert_cmpfloat(dbl
, ==, -42.0);
127 static void test_visitor_in_uint(TestInputVisitorData
*data
,
136 v
= visitor_input_test_init(data
, "%d", value
);
138 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
139 g_assert_cmpuint(res
, ==, (uint64_t)value
);
141 visit_type_int(v
, NULL
, &i64
, &error_abort
);
142 g_assert_cmpint(i64
, ==, value
);
144 visit_type_number(v
, NULL
, &dbl
, &error_abort
);
145 g_assert_cmpfloat(dbl
, ==, value
);
147 /* BUG: value between INT64_MIN and -1 accepted modulo 2^64 */
148 v
= visitor_input_test_init(data
, "%d", -value
);
150 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
151 g_assert_cmpuint(res
, ==, (uint64_t)-value
);
153 v
= visitor_input_test_init(data
, "18446744073709551574");
155 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
156 g_assert_cmpuint(res
, ==, 18446744073709551574U);
158 visit_type_number(v
, NULL
, &dbl
, &error_abort
);
159 g_assert_cmpfloat(dbl
, ==, 18446744073709552000.0);
162 static void test_visitor_in_int_overflow(TestInputVisitorData
*data
,
170 * This will overflow a QNUM_I64, so should be deserialized into a
171 * QNUM_DOUBLE field instead, leading to an error if we pass it to
172 * visit_type_int(). Confirm this.
174 v
= visitor_input_test_init(data
, "%f", DBL_MAX
);
176 visit_type_int(v
, NULL
, &res
, &err
);
177 error_free_or_abort(&err
);
180 static void test_visitor_in_int_keyval(TestInputVisitorData
*data
,
183 int64_t res
= 0, value
= -42;
187 v
= visitor_input_test_init_full(data
, true, "%" PRId64
, value
);
188 visit_type_int(v
, NULL
, &res
, &err
);
189 error_free_or_abort(&err
);
192 static void test_visitor_in_int_str_keyval(TestInputVisitorData
*data
,
195 int64_t res
= 0, value
= -42;
198 v
= visitor_input_test_init_full(data
, true, "\"-42\"");
200 visit_type_int(v
, NULL
, &res
, &error_abort
);
201 g_assert_cmpint(res
, ==, value
);
204 static void test_visitor_in_int_str_fail(TestInputVisitorData
*data
,
211 v
= visitor_input_test_init(data
, "\"-42\"");
213 visit_type_int(v
, NULL
, &res
, &err
);
214 error_free_or_abort(&err
);
217 static void test_visitor_in_bool(TestInputVisitorData
*data
,
223 v
= visitor_input_test_init(data
, "true");
225 visit_type_bool(v
, NULL
, &res
, &error_abort
);
226 g_assert_cmpint(res
, ==, true);
229 static void test_visitor_in_bool_keyval(TestInputVisitorData
*data
,
236 v
= visitor_input_test_init_full(data
, true, "true");
238 visit_type_bool(v
, NULL
, &res
, &err
);
239 error_free_or_abort(&err
);
242 static void test_visitor_in_bool_str_keyval(TestInputVisitorData
*data
,
248 v
= visitor_input_test_init_full(data
, true, "\"on\"");
250 visit_type_bool(v
, NULL
, &res
, &error_abort
);
251 g_assert_cmpint(res
, ==, true);
254 static void test_visitor_in_bool_str_fail(TestInputVisitorData
*data
,
261 v
= visitor_input_test_init(data
, "\"true\"");
263 visit_type_bool(v
, NULL
, &res
, &err
);
264 error_free_or_abort(&err
);
267 static void test_visitor_in_number(TestInputVisitorData
*data
,
270 double res
= 0, value
= 3.14;
273 v
= visitor_input_test_init(data
, "%f", value
);
275 visit_type_number(v
, NULL
, &res
, &error_abort
);
276 g_assert_cmpfloat(res
, ==, value
);
279 static void test_visitor_in_large_number(TestInputVisitorData
*data
,
288 v
= visitor_input_test_init(data
, "-18446744073709551616"); /* -2^64 */
290 visit_type_number(v
, NULL
, &res
, &error_abort
);
291 g_assert_cmpfloat(res
, ==, -18446744073709552e3
);
293 visit_type_int(v
, NULL
, &i64
, &err
);
294 error_free_or_abort(&err
);
296 visit_type_uint64(v
, NULL
, &u64
, &err
);
297 error_free_or_abort(&err
);
300 static void test_visitor_in_number_keyval(TestInputVisitorData
*data
,
303 double res
= 0, value
= 3.14;
307 v
= visitor_input_test_init_full(data
, true, "%f", value
);
309 visit_type_number(v
, NULL
, &res
, &err
);
310 error_free_or_abort(&err
);
313 static void test_visitor_in_number_str_keyval(TestInputVisitorData
*data
,
316 double res
= 0, value
= 3.14;
320 v
= visitor_input_test_init_full(data
, true, "\"3.14\"");
322 visit_type_number(v
, NULL
, &res
, &error_abort
);
323 g_assert_cmpfloat(res
, ==, value
);
325 v
= visitor_input_test_init_full(data
, true, "\"inf\"");
327 visit_type_number(v
, NULL
, &res
, &err
);
328 error_free_or_abort(&err
);
331 static void test_visitor_in_number_str_fail(TestInputVisitorData
*data
,
338 v
= visitor_input_test_init(data
, "\"3.14\"");
340 visit_type_number(v
, NULL
, &res
, &err
);
341 error_free_or_abort(&err
);
344 static void test_visitor_in_size_str_keyval(TestInputVisitorData
*data
,
347 uint64_t res
, value
= 500 * 1024 * 1024;
350 v
= visitor_input_test_init_full(data
, true, "\"500M\"");
352 visit_type_size(v
, NULL
, &res
, &error_abort
);
353 g_assert_cmpfloat(res
, ==, value
);
356 static void test_visitor_in_size_str_fail(TestInputVisitorData
*data
,
363 v
= visitor_input_test_init(data
, "\"500M\"");
365 visit_type_size(v
, NULL
, &res
, &err
);
366 error_free_or_abort(&err
);
369 static void test_visitor_in_string(TestInputVisitorData
*data
,
372 char *res
= NULL
, *value
= (char *) "Q E M U";
375 v
= visitor_input_test_init(data
, "%s", value
);
377 visit_type_str(v
, NULL
, &res
, &error_abort
);
378 g_assert_cmpstr(res
, ==, value
);
383 static void test_visitor_in_enum(TestInputVisitorData
*data
,
389 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
392 v
= visitor_input_test_init(data
, "%s", EnumOne_str(i
));
394 visit_type_EnumOne(v
, NULL
, &res
, &error_abort
);
395 g_assert_cmpint(i
, ==, res
);
400 static void test_visitor_in_struct(TestInputVisitorData
*data
,
403 TestStruct
*p
= NULL
;
406 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
408 visit_type_TestStruct(v
, NULL
, &p
, &error_abort
);
409 g_assert_cmpint(p
->integer
, ==, -42);
410 g_assert(p
->boolean
== true);
411 g_assert_cmpstr(p
->string
, ==, "foo");
417 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
420 g_autoptr(UserDefTwo
) udp
= NULL
;
423 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', "
424 "'dict1': { 'string1': 'string1', "
425 "'dict2': { 'userdef': { 'integer': 42, "
426 "'string': 'string' }, 'string': 'string2'}}}");
428 visit_type_UserDefTwo(v
, NULL
, &udp
, &error_abort
);
430 g_assert_cmpstr(udp
->string0
, ==, "string0");
431 g_assert_cmpstr(udp
->dict1
->string1
, ==, "string1");
432 g_assert_cmpint(udp
->dict1
->dict2
->userdef
->integer
, ==, 42);
433 g_assert_cmpstr(udp
->dict1
->dict2
->userdef
->string
, ==, "string");
434 g_assert_cmpstr(udp
->dict1
->dict2
->string
, ==, "string2");
435 g_assert(udp
->dict1
->has_dict3
== false);
438 static void test_visitor_in_list(TestInputVisitorData
*data
,
441 UserDefOneList
*item
, *head
= NULL
;
445 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
447 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
448 g_assert(head
!= NULL
);
450 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
453 snprintf(string
, sizeof(string
), "string%d", i
);
454 g_assert_cmpstr(item
->value
->string
, ==, string
);
455 g_assert_cmpint(item
->value
->integer
, ==, 42 + i
);
458 qapi_free_UserDefOneList(head
);
461 /* An empty list is valid */
462 v
= visitor_input_test_init(data
, "[]");
463 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
467 static void test_visitor_in_list_struct(TestInputVisitorData
*data
,
470 const char *int_member
[] = {
471 "integer", "s8", "s16", "s32", "s64", "u8", "u16", "u32", "u64" };
472 g_autoptr(GString
) json
= g_string_new("");
475 g_autoptr(ArrayStruct
) arrs
= NULL
;
483 uint16List
*u16_list
;
484 uint32List
*u32_list
;
485 uint64List
*u64_list
;
486 numberList
*num_list
;
490 g_string_append_printf(json
, "{");
492 for (i
= 0; i
< G_N_ELEMENTS(int_member
); i
++) {
493 g_string_append_printf(json
, "'%s': [", int_member
[i
]);
495 for (j
= 0; j
< 32; j
++) {
496 g_string_append_printf(json
, "%s%d", sep
, j
);
499 g_string_append_printf(json
, "], ");
502 g_string_append_printf(json
, "'number': [");
504 for (i
= 0; i
< 32; i
++) {
505 g_string_append_printf(json
, "%s%f", sep
, (double)i
/ 3);
508 g_string_append_printf(json
, "], ");
510 g_string_append_printf(json
, "'boolean': [");
512 for (i
= 0; i
< 32; i
++) {
513 g_string_append_printf(json
, "%s%s",
514 sep
, i
% 3 == 0 ? "true" : "false");
517 g_string_append_printf(json
, "], ");
519 g_string_append_printf(json
, "'string': [");
521 for (i
= 0; i
< 32; i
++) {
522 g_string_append_printf(json
, "%s'%d'", sep
, i
);
525 g_string_append_printf(json
, "]");
527 g_string_append_printf(json
, "}");
529 v
= visitor_input_test_init_raw(data
, json
->str
);
530 visit_type_ArrayStruct(v
, NULL
, &arrs
, &error_abort
);
533 for (int_list
= arrs
->integer
; int_list
; int_list
= int_list
->next
) {
534 g_assert_cmpint(int_list
->value
, ==, i
);
539 for (s8_list
= arrs
->s8
; s8_list
; s8_list
= s8_list
->next
) {
540 g_assert_cmpint(s8_list
->value
, ==, i
);
545 for (s16_list
= arrs
->s16
; s16_list
; s16_list
= s16_list
->next
) {
546 g_assert_cmpint(s16_list
->value
, ==, i
);
551 for (s32_list
= arrs
->s32
; s32_list
; s32_list
= s32_list
->next
) {
552 g_assert_cmpint(s32_list
->value
, ==, i
);
557 for (s64_list
= arrs
->s64
; s64_list
; s64_list
= s64_list
->next
) {
558 g_assert_cmpint(s64_list
->value
, ==, i
);
563 for (u8_list
= arrs
->u8
; u8_list
; u8_list
= u8_list
->next
) {
564 g_assert_cmpint(u8_list
->value
, ==, i
);
569 for (u16_list
= arrs
->u16
; u16_list
; u16_list
= u16_list
->next
) {
570 g_assert_cmpint(u16_list
->value
, ==, i
);
575 for (u32_list
= arrs
->u32
; u32_list
; u32_list
= u32_list
->next
) {
576 g_assert_cmpint(u32_list
->value
, ==, i
);
581 for (u64_list
= arrs
->u64
; u64_list
; u64_list
= u64_list
->next
) {
582 g_assert_cmpint(u64_list
->value
, ==, i
);
587 for (num_list
= arrs
->number
; num_list
; num_list
= num_list
->next
) {
588 char expected
[32], actual
[32];
590 sprintf(expected
, "%.6f", (double)i
/ 3);
591 sprintf(actual
, "%.6f", num_list
->value
);
592 g_assert_cmpstr(expected
, ==, actual
);
597 for (bool_list
= arrs
->boolean
; bool_list
; bool_list
= bool_list
->next
) {
598 g_assert_cmpint(bool_list
->value
, ==, i
% 3 == 0);
603 for (str_list
= arrs
->string
; str_list
; str_list
= str_list
->next
) {
606 sprintf(expected
, "%d", i
);
607 g_assert_cmpstr(str_list
->value
, ==, expected
);
612 static void test_visitor_in_any(TestInputVisitorData
*data
,
624 v
= visitor_input_test_init(data
, "-42");
625 visit_type_any(v
, NULL
, &res
, &error_abort
);
626 qnum
= qobject_to(QNum
, res
);
628 g_assert(qnum_get_try_int(qnum
, &val
));
629 g_assert_cmpint(val
, ==, -42);
632 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
633 visit_type_any(v
, NULL
, &res
, &error_abort
);
634 qdict
= qobject_to(QDict
, res
);
635 g_assert(qdict
&& qdict_size(qdict
) == 3);
636 qobj
= qdict_get(qdict
, "integer");
638 qnum
= qobject_to(QNum
, qobj
);
640 g_assert(qnum_get_try_int(qnum
, &val
));
641 g_assert_cmpint(val
, ==, -42);
642 qobj
= qdict_get(qdict
, "boolean");
644 qbool
= qobject_to(QBool
, qobj
);
646 g_assert(qbool_get_bool(qbool
) == true);
647 qobj
= qdict_get(qdict
, "string");
649 qstring
= qobject_to(QString
, qobj
);
651 g_assert_cmpstr(qstring_get_str(qstring
), ==, "foo");
655 static void test_visitor_in_null(TestInputVisitorData
*data
,
664 * FIXME: Since QAPI doesn't know the 'null' type yet, we can't
665 * test visit_type_null() by reading into a QAPI struct then
666 * checking that it was populated correctly. The best we can do
667 * for now is ensure that we consumed null from the input, proven
668 * by the fact that we can't re-read the key; and that we detect
669 * when input is not null.
672 v
= visitor_input_test_init_full(data
, false,
673 "{ 'a': null, 'b': '' }");
674 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
675 visit_type_null(v
, "a", &null
, &error_abort
);
676 g_assert(qobject_type(QOBJECT(null
)) == QTYPE_QNULL
);
678 visit_type_null(v
, "b", &null
, &err
);
679 error_free_or_abort(&err
);
681 visit_type_str(v
, "c", &tmp
, &err
);
682 error_free_or_abort(&err
);
684 visit_check_struct(v
, &error_abort
);
685 visit_end_struct(v
, NULL
);
688 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
692 g_autoptr(UserDefFlatUnion
) tmp
= NULL
;
693 UserDefUnionBase
*base
;
695 v
= visitor_input_test_init(data
,
696 "{ 'enum1': 'value1', "
699 "'boolean': true }");
701 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &error_abort
);
702 g_assert_cmpint(tmp
->enum1
, ==, ENUM_ONE_VALUE1
);
703 g_assert_cmpstr(tmp
->string
, ==, "str");
704 g_assert_cmpint(tmp
->integer
, ==, 41);
705 g_assert_cmpint(tmp
->u
.value1
.boolean
, ==, true);
707 base
= qapi_UserDefFlatUnion_base(tmp
);
708 g_assert(&base
->enum1
== &tmp
->enum1
);
711 static void test_visitor_in_alternate(TestInputVisitorData
*data
,
715 UserDefAlternate
*tmp
;
718 v
= visitor_input_test_init(data
, "42");
719 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
720 g_assert_cmpint(tmp
->type
, ==, QTYPE_QNUM
);
721 g_assert_cmpint(tmp
->u
.i
, ==, 42);
722 qapi_free_UserDefAlternate(tmp
);
724 v
= visitor_input_test_init(data
, "'value1'");
725 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
726 g_assert_cmpint(tmp
->type
, ==, QTYPE_QSTRING
);
727 g_assert_cmpint(tmp
->u
.e
, ==, ENUM_ONE_VALUE1
);
728 qapi_free_UserDefAlternate(tmp
);
730 v
= visitor_input_test_init(data
, "null");
731 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
732 g_assert_cmpint(tmp
->type
, ==, QTYPE_QNULL
);
733 qapi_free_UserDefAlternate(tmp
);
735 v
= visitor_input_test_init(data
, "{'integer':1, 'string':'str', "
736 "'enum1':'value1', 'boolean':true}");
737 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
738 g_assert_cmpint(tmp
->type
, ==, QTYPE_QDICT
);
739 g_assert_cmpint(tmp
->u
.udfu
.integer
, ==, 1);
740 g_assert_cmpstr(tmp
->u
.udfu
.string
, ==, "str");
741 g_assert_cmpint(tmp
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
742 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.boolean
, ==, true);
743 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
744 qapi_free_UserDefAlternate(tmp
);
746 v
= visitor_input_test_init(data
, "{ 'alt': 42 }");
747 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
748 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QNUM
);
749 g_assert_cmpint(wrap
->alt
->u
.i
, ==, 42);
750 qapi_free_WrapAlternate(wrap
);
752 v
= visitor_input_test_init(data
, "{ 'alt': 'value1' }");
753 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
754 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QSTRING
);
755 g_assert_cmpint(wrap
->alt
->u
.e
, ==, ENUM_ONE_VALUE1
);
756 qapi_free_WrapAlternate(wrap
);
758 v
= visitor_input_test_init(data
, "{ 'alt': {'integer':1, 'string':'str', "
759 "'enum1':'value1', 'boolean':true} }");
760 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
761 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QDICT
);
762 g_assert_cmpint(wrap
->alt
->u
.udfu
.integer
, ==, 1);
763 g_assert_cmpstr(wrap
->alt
->u
.udfu
.string
, ==, "str");
764 g_assert_cmpint(wrap
->alt
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
765 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.boolean
, ==, true);
766 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
767 qapi_free_WrapAlternate(wrap
);
770 static void test_visitor_in_alternate_number(TestInputVisitorData
*data
,
782 v
= visitor_input_test_init(data
, "42");
783 visit_type_AltEnumBool(v
, NULL
, &aeb
, &err
);
784 error_free_or_abort(&err
);
785 qapi_free_AltEnumBool(aeb
);
787 v
= visitor_input_test_init(data
, "42");
788 visit_type_AltEnumNum(v
, NULL
, &aen
, &error_abort
);
789 g_assert_cmpint(aen
->type
, ==, QTYPE_QNUM
);
790 g_assert_cmpfloat(aen
->u
.n
, ==, 42);
791 qapi_free_AltEnumNum(aen
);
793 v
= visitor_input_test_init(data
, "42");
794 visit_type_AltNumEnum(v
, NULL
, &ans
, &error_abort
);
795 g_assert_cmpint(ans
->type
, ==, QTYPE_QNUM
);
796 g_assert_cmpfloat(ans
->u
.n
, ==, 42);
797 qapi_free_AltNumEnum(ans
);
799 v
= visitor_input_test_init(data
, "42");
800 visit_type_AltEnumInt(v
, NULL
, &asi
, &error_abort
);
801 g_assert_cmpint(asi
->type
, ==, QTYPE_QNUM
);
802 g_assert_cmpint(asi
->u
.i
, ==, 42);
803 qapi_free_AltEnumInt(asi
);
805 /* Parsing a double */
807 v
= visitor_input_test_init(data
, "42.5");
808 visit_type_AltEnumBool(v
, NULL
, &aeb
, &err
);
809 error_free_or_abort(&err
);
810 qapi_free_AltEnumBool(aeb
);
812 v
= visitor_input_test_init(data
, "42.5");
813 visit_type_AltEnumNum(v
, NULL
, &aen
, &error_abort
);
814 g_assert_cmpint(aen
->type
, ==, QTYPE_QNUM
);
815 g_assert_cmpfloat(aen
->u
.n
, ==, 42.5);
816 qapi_free_AltEnumNum(aen
);
818 v
= visitor_input_test_init(data
, "42.5");
819 visit_type_AltNumEnum(v
, NULL
, &ans
, &error_abort
);
820 g_assert_cmpint(ans
->type
, ==, QTYPE_QNUM
);
821 g_assert_cmpfloat(ans
->u
.n
, ==, 42.5);
822 qapi_free_AltNumEnum(ans
);
824 v
= visitor_input_test_init(data
, "42.5");
825 visit_type_AltEnumInt(v
, NULL
, &asi
, &err
);
826 error_free_or_abort(&err
);
827 qapi_free_AltEnumInt(asi
);
830 static void input_visitor_test_add(const char *testpath
,
831 const void *user_data
,
832 void (*test_func
)(TestInputVisitorData
*data
,
833 const void *user_data
))
835 g_test_add(testpath
, TestInputVisitorData
, user_data
, NULL
, test_func
,
836 visitor_input_teardown
);
839 static void test_visitor_in_errors(TestInputVisitorData
*data
,
842 TestStruct
*p
= NULL
;
846 UserDefTwo
*r
= NULL
;
847 WrapAlternate
*s
= NULL
;
849 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', "
852 visit_type_TestStruct(v
, NULL
, &p
, &err
);
853 error_free_or_abort(&err
);
856 v
= visitor_input_test_init(data
, "[ '1', '2', false, '3' ]");
857 visit_type_strList(v
, NULL
, &q
, &err
);
858 error_free_or_abort(&err
);
861 v
= visitor_input_test_init(data
, "{ 'str':'hi' }");
862 visit_type_UserDefTwo(v
, NULL
, &r
, &err
);
863 error_free_or_abort(&err
);
866 v
= visitor_input_test_init(data
, "{ }");
867 visit_type_WrapAlternate(v
, NULL
, &s
, &err
);
868 error_free_or_abort(&err
);
872 static void test_visitor_in_wrong_type(TestInputVisitorData
*data
,
875 TestStruct
*p
= NULL
;
881 /* Make sure arrays and structs cannot be confused */
883 v
= visitor_input_test_init(data
, "[]");
884 visit_type_TestStruct(v
, NULL
, &p
, &err
);
885 error_free_or_abort(&err
);
888 v
= visitor_input_test_init(data
, "{}");
889 visit_type_strList(v
, NULL
, &q
, &err
);
890 error_free_or_abort(&err
);
893 /* Make sure primitives and struct cannot be confused */
895 v
= visitor_input_test_init(data
, "1");
896 visit_type_TestStruct(v
, NULL
, &p
, &err
);
897 error_free_or_abort(&err
);
900 v
= visitor_input_test_init(data
, "{}");
901 visit_type_int(v
, NULL
, &i
, &err
);
902 error_free_or_abort(&err
);
904 /* Make sure primitives and arrays cannot be confused */
906 v
= visitor_input_test_init(data
, "1");
907 visit_type_strList(v
, NULL
, &q
, &err
);
908 error_free_or_abort(&err
);
911 v
= visitor_input_test_init(data
, "[]");
912 visit_type_int(v
, NULL
, &i
, &err
);
913 error_free_or_abort(&err
);
916 static void test_visitor_in_fail_struct(TestInputVisitorData
*data
,
919 TestStruct
*p
= NULL
;
923 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
925 visit_type_TestStruct(v
, NULL
, &p
, &err
);
926 error_free_or_abort(&err
);
930 static void test_visitor_in_fail_struct_nested(TestInputVisitorData
*data
,
933 UserDefTwo
*udp
= NULL
;
937 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
939 visit_type_UserDefTwo(v
, NULL
, &udp
, &err
);
940 error_free_or_abort(&err
);
944 static void test_visitor_in_fail_struct_in_list(TestInputVisitorData
*data
,
947 UserDefOneList
*head
= NULL
;
951 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
953 visit_type_UserDefOneList(v
, NULL
, &head
, &err
);
954 error_free_or_abort(&err
);
958 static void test_visitor_in_fail_struct_missing(TestInputVisitorData
*data
,
965 GenericAlternate
*alt
;
974 v
= visitor_input_test_init(data
, "{ 'sub': [ {} ] }");
975 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
976 visit_start_struct(v
, "struct", NULL
, 0, &err
);
977 error_free_or_abort(&err
);
978 visit_start_list(v
, "list", NULL
, 0, &err
);
979 error_free_or_abort(&err
);
980 visit_start_alternate(v
, "alternate", &alt
, sizeof(*alt
), &err
);
981 error_free_or_abort(&err
);
982 visit_optional(v
, "optional", &present
);
984 visit_type_enum(v
, "enum", &en
, &EnumOne_lookup
, &err
);
985 error_free_or_abort(&err
);
986 visit_type_int(v
, "i64", &i64
, &err
);
987 error_free_or_abort(&err
);
988 visit_type_uint32(v
, "u32", &u32
, &err
);
989 error_free_or_abort(&err
);
990 visit_type_int8(v
, "i8", &i8
, &err
);
991 error_free_or_abort(&err
);
992 visit_type_str(v
, "i8", &str
, &err
);
993 error_free_or_abort(&err
);
994 visit_type_number(v
, "dbl", &dbl
, &err
);
995 error_free_or_abort(&err
);
996 visit_type_any(v
, "any", &any
, &err
);
997 error_free_or_abort(&err
);
998 visit_type_null(v
, "null", &null
, &err
);
999 error_free_or_abort(&err
);
1000 visit_start_list(v
, "sub", NULL
, 0, &error_abort
);
1001 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
1002 visit_type_int(v
, "i64", &i64
, &err
);
1003 error_free_or_abort(&err
);
1004 visit_end_struct(v
, NULL
);
1005 visit_end_list(v
, NULL
);
1006 visit_end_struct(v
, NULL
);
1009 static void test_visitor_in_fail_list(TestInputVisitorData
*data
,
1016 /* Unvisited list tail */
1018 v
= visitor_input_test_init(data
, "[ 1, 2, 3 ]");
1020 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1021 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1022 g_assert_cmpint(i64
, ==, 1);
1023 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1024 g_assert_cmpint(i64
, ==, 2);
1025 visit_check_list(v
, &err
);
1026 error_free_or_abort(&err
);
1027 visit_end_list(v
, NULL
);
1029 /* Visit beyond end of list */
1030 v
= visitor_input_test_init(data
, "[]");
1032 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1033 visit_type_int(v
, NULL
, &i64
, &err
);
1034 error_free_or_abort(&err
);
1035 visit_end_list(v
, NULL
);
1038 static void test_visitor_in_fail_list_nested(TestInputVisitorData
*data
,
1045 /* Unvisited nested list tail */
1047 v
= visitor_input_test_init(data
, "[ 0, [ 1, 2, 3 ] ]");
1049 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1050 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1051 g_assert_cmpint(i64
, ==, 0);
1052 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
1053 visit_type_int(v
, NULL
, &i64
, &error_abort
);
1054 g_assert_cmpint(i64
, ==, 1);
1055 visit_check_list(v
, &err
);
1056 error_free_or_abort(&err
);
1057 visit_end_list(v
, NULL
);
1058 visit_check_list(v
, &error_abort
);
1059 visit_end_list(v
, NULL
);
1062 static void test_visitor_in_fail_union_flat(TestInputVisitorData
*data
,
1065 UserDefFlatUnion
*tmp
= NULL
;
1069 v
= visitor_input_test_init(data
, "{ 'enum1': 'value2', 'string': 'c', 'integer': 41, 'boolean': true }");
1071 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &err
);
1072 error_free_or_abort(&err
);
1076 static void test_visitor_in_fail_union_flat_no_discrim(TestInputVisitorData
*data
,
1079 UserDefFlatUnion2
*tmp
= NULL
;
1083 /* test situation where discriminator field ('enum1' here) is missing */
1084 v
= visitor_input_test_init(data
, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
1086 visit_type_UserDefFlatUnion2(v
, NULL
, &tmp
, &err
);
1087 error_free_or_abort(&err
);
1091 static void test_visitor_in_fail_alternate(TestInputVisitorData
*data
,
1094 UserDefAlternate
*tmp
;
1098 v
= visitor_input_test_init(data
, "3.14");
1100 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
1101 error_free_or_abort(&err
);
1105 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1106 const QLitObject
*qlit
)
1108 g_autoptr(SchemaInfoList
) schema
= NULL
;
1109 QObject
*obj
= qobject_from_qlit(qlit
);
1112 v
= qobject_input_visitor_new(obj
);
1114 visit_type_SchemaInfoList(v
, NULL
, &schema
, &error_abort
);
1121 static void test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1124 do_test_visitor_in_qmp_introspect(data
, &test_qmp_schema_qlit
);
1127 int main(int argc
, char **argv
)
1129 g_test_init(&argc
, &argv
, NULL
);
1131 input_visitor_test_add("/visitor/input/int",
1132 NULL
, test_visitor_in_int
);
1133 input_visitor_test_add("/visitor/input/uint",
1134 NULL
, test_visitor_in_uint
);
1135 input_visitor_test_add("/visitor/input/int_overflow",
1136 NULL
, test_visitor_in_int_overflow
);
1137 input_visitor_test_add("/visitor/input/int_keyval",
1138 NULL
, test_visitor_in_int_keyval
);
1139 input_visitor_test_add("/visitor/input/int_str_keyval",
1140 NULL
, test_visitor_in_int_str_keyval
);
1141 input_visitor_test_add("/visitor/input/int_str_fail",
1142 NULL
, test_visitor_in_int_str_fail
);
1143 input_visitor_test_add("/visitor/input/bool",
1144 NULL
, test_visitor_in_bool
);
1145 input_visitor_test_add("/visitor/input/bool_keyval",
1146 NULL
, test_visitor_in_bool_keyval
);
1147 input_visitor_test_add("/visitor/input/bool_str_keyval",
1148 NULL
, test_visitor_in_bool_str_keyval
);
1149 input_visitor_test_add("/visitor/input/bool_str_fail",
1150 NULL
, test_visitor_in_bool_str_fail
);
1151 input_visitor_test_add("/visitor/input/number",
1152 NULL
, test_visitor_in_number
);
1153 input_visitor_test_add("/visitor/input/large_number",
1154 NULL
, test_visitor_in_large_number
);
1155 input_visitor_test_add("/visitor/input/number_keyval",
1156 NULL
, test_visitor_in_number_keyval
);
1157 input_visitor_test_add("/visitor/input/number_str_keyval",
1158 NULL
, test_visitor_in_number_str_keyval
);
1159 input_visitor_test_add("/visitor/input/number_str_fail",
1160 NULL
, test_visitor_in_number_str_fail
);
1161 input_visitor_test_add("/visitor/input/size_str_keyval",
1162 NULL
, test_visitor_in_size_str_keyval
);
1163 input_visitor_test_add("/visitor/input/size_str_fail",
1164 NULL
, test_visitor_in_size_str_fail
);
1165 input_visitor_test_add("/visitor/input/string",
1166 NULL
, test_visitor_in_string
);
1167 input_visitor_test_add("/visitor/input/enum",
1168 NULL
, test_visitor_in_enum
);
1169 input_visitor_test_add("/visitor/input/struct",
1170 NULL
, test_visitor_in_struct
);
1171 input_visitor_test_add("/visitor/input/struct-nested",
1172 NULL
, test_visitor_in_struct_nested
);
1173 input_visitor_test_add("/visitor/input/list2",
1174 NULL
, test_visitor_in_list_struct
);
1175 input_visitor_test_add("/visitor/input/list",
1176 NULL
, test_visitor_in_list
);
1177 input_visitor_test_add("/visitor/input/any",
1178 NULL
, test_visitor_in_any
);
1179 input_visitor_test_add("/visitor/input/null",
1180 NULL
, test_visitor_in_null
);
1181 input_visitor_test_add("/visitor/input/union-flat",
1182 NULL
, test_visitor_in_union_flat
);
1183 input_visitor_test_add("/visitor/input/alternate",
1184 NULL
, test_visitor_in_alternate
);
1185 input_visitor_test_add("/visitor/input/errors",
1186 NULL
, test_visitor_in_errors
);
1187 input_visitor_test_add("/visitor/input/wrong-type",
1188 NULL
, test_visitor_in_wrong_type
);
1189 input_visitor_test_add("/visitor/input/alternate-number",
1190 NULL
, test_visitor_in_alternate_number
);
1191 input_visitor_test_add("/visitor/input/fail/struct",
1192 NULL
, test_visitor_in_fail_struct
);
1193 input_visitor_test_add("/visitor/input/fail/struct-nested",
1194 NULL
, test_visitor_in_fail_struct_nested
);
1195 input_visitor_test_add("/visitor/input/fail/struct-in-list",
1196 NULL
, test_visitor_in_fail_struct_in_list
);
1197 input_visitor_test_add("/visitor/input/fail/struct-missing",
1198 NULL
, test_visitor_in_fail_struct_missing
);
1199 input_visitor_test_add("/visitor/input/fail/list",
1200 NULL
, test_visitor_in_fail_list
);
1201 input_visitor_test_add("/visitor/input/fail/list-nested",
1202 NULL
, test_visitor_in_fail_list_nested
);
1203 input_visitor_test_add("/visitor/input/fail/union-flat",
1204 NULL
, test_visitor_in_fail_union_flat
);
1205 input_visitor_test_add("/visitor/input/fail/union-flat-no-discriminator",
1206 NULL
, test_visitor_in_fail_union_flat_no_discrim
);
1207 input_visitor_test_add("/visitor/input/fail/alternate",
1208 NULL
, test_visitor_in_fail_alternate
);
1209 input_visitor_test_add("/visitor/input/qapi-introspect",
1210 NULL
, test_visitor_in_qmp_introspect
);