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 "qapi/error.h"
17 #include "qapi/qapi-visit-introspect.h"
18 #include "qapi/qobject-input-visitor.h"
19 #include "test-qapi-visit.h"
20 #include "qapi/qmp/qbool.h"
21 #include "qapi/qmp/qdict.h"
22 #include "qapi/qmp/qnull.h"
23 #include "qapi/qmp/qnum.h"
24 #include "qapi/qmp/qstring.h"
25 #include "qapi/qmp/qjson.h"
26 #include "test-qapi-introspect.h"
27 #include "qapi/qapi-introspect.h"
29 typedef struct TestInputVisitorData
{
32 } TestInputVisitorData
;
34 static void visitor_input_teardown(TestInputVisitorData
*data
,
37 qobject_unref(data
->obj
);
41 visit_free(data
->qiv
);
46 /* The various test_init functions are provided instead of a test setup
47 function so that the JSON string used by the tests are kept in the test
48 functions (and not in main()). */
50 static Visitor
*test_init_internal(TestInputVisitorData
*data
, bool keyval
,
53 visitor_input_teardown(data
, NULL
);
58 data
->qiv
= qobject_input_visitor_new_keyval(data
->obj
);
60 data
->qiv
= qobject_input_visitor_new(data
->obj
);
66 static G_GNUC_PRINTF(3, 4)
67 Visitor
*visitor_input_test_init_full(TestInputVisitorData
*data
,
69 const char *json_string
, ...)
74 va_start(ap
, json_string
);
75 v
= test_init_internal(data
, keyval
,
76 qobject_from_vjsonf_nofail(json_string
, ap
));
81 static G_GNUC_PRINTF(2, 3)
82 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
83 const char *json_string
, ...)
88 va_start(ap
, json_string
);
89 v
= test_init_internal(data
, false,
90 qobject_from_vjsonf_nofail(json_string
, ap
));
95 /* similar to visitor_input_test_init(), but does not expect a string
96 * literal/format json_string argument and so can be used for
97 * programatically generated strings (and we can't pass in programatically
98 * generated strings via %s format parameters since qobject_from_jsonv()
99 * will wrap those in double-quotes and treat the entire object as a
102 static Visitor
*visitor_input_test_init_raw(TestInputVisitorData
*data
,
103 const char *json_string
)
105 return test_init_internal(data
, false,
106 qobject_from_json(json_string
, &error_abort
));
109 static void test_visitor_in_int(TestInputVisitorData
*data
,
117 v
= visitor_input_test_init(data
, "%d", value
);
119 visit_type_int(v
, NULL
, &res
, &error_abort
);
120 g_assert_cmpint(res
, ==, value
);
122 visit_type_number(v
, NULL
, &dbl
, &error_abort
);
123 g_assert_cmpfloat(dbl
, ==, -42.0);
126 static void test_visitor_in_uint(TestInputVisitorData
*data
,
135 v
= visitor_input_test_init(data
, "%d", value
);
137 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
138 g_assert_cmpuint(res
, ==, (uint64_t)value
);
140 visit_type_int(v
, NULL
, &i64
, &error_abort
);
141 g_assert_cmpint(i64
, ==, value
);
143 visit_type_number(v
, NULL
, &dbl
, &error_abort
);
144 g_assert_cmpfloat(dbl
, ==, value
);
146 /* BUG: value between INT64_MIN and -1 accepted modulo 2^64 */
147 v
= visitor_input_test_init(data
, "%d", -value
);
149 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
150 g_assert_cmpuint(res
, ==, (uint64_t)-value
);
152 v
= visitor_input_test_init(data
, "18446744073709551574");
154 visit_type_uint64(v
, NULL
, &res
, &error_abort
);
155 g_assert_cmpuint(res
, ==, 18446744073709551574U);
157 visit_type_number(v
, NULL
, &dbl
, &error_abort
);
158 g_assert_cmpfloat(dbl
, ==, 18446744073709552000.0);
161 static void test_visitor_in_int_overflow(TestInputVisitorData
*data
,
169 * This will overflow a QNUM_I64, so should be deserialized into a
170 * QNUM_DOUBLE field instead, leading to an error if we pass it to
171 * visit_type_int(). Confirm this.
173 v
= visitor_input_test_init(data
, "%f", DBL_MAX
);
175 visit_type_int(v
, NULL
, &res
, &err
);
176 error_free_or_abort(&err
);
179 static void test_visitor_in_int_keyval(TestInputVisitorData
*data
,
182 int64_t res
= 0, value
= -42;
186 v
= visitor_input_test_init_full(data
, true, "%" PRId64
, value
);
187 visit_type_int(v
, NULL
, &res
, &err
);
188 error_free_or_abort(&err
);
191 static void test_visitor_in_int_str_keyval(TestInputVisitorData
*data
,
194 int64_t res
= 0, value
= -42;
197 v
= visitor_input_test_init_full(data
, true, "\"-42\"");
199 visit_type_int(v
, NULL
, &res
, &error_abort
);
200 g_assert_cmpint(res
, ==, value
);
203 static void test_visitor_in_int_str_fail(TestInputVisitorData
*data
,
210 v
= visitor_input_test_init(data
, "\"-42\"");
212 visit_type_int(v
, NULL
, &res
, &err
);
213 error_free_or_abort(&err
);
216 static void test_visitor_in_bool(TestInputVisitorData
*data
,
222 v
= visitor_input_test_init(data
, "true");
224 visit_type_bool(v
, NULL
, &res
, &error_abort
);
225 g_assert_cmpint(res
, ==, true);
228 static void test_visitor_in_bool_keyval(TestInputVisitorData
*data
,
235 v
= visitor_input_test_init_full(data
, true, "true");
237 visit_type_bool(v
, NULL
, &res
, &err
);
238 error_free_or_abort(&err
);
241 static void test_visitor_in_bool_str_keyval(TestInputVisitorData
*data
,
247 v
= visitor_input_test_init_full(data
, true, "\"on\"");
249 visit_type_bool(v
, NULL
, &res
, &error_abort
);
250 g_assert_cmpint(res
, ==, true);
253 static void test_visitor_in_bool_str_fail(TestInputVisitorData
*data
,
260 v
= visitor_input_test_init(data
, "\"true\"");
262 visit_type_bool(v
, NULL
, &res
, &err
);
263 error_free_or_abort(&err
);
266 static void test_visitor_in_number(TestInputVisitorData
*data
,
269 double res
= 0, value
= 3.14;
272 v
= visitor_input_test_init(data
, "%f", value
);
274 visit_type_number(v
, NULL
, &res
, &error_abort
);
275 g_assert_cmpfloat(res
, ==, value
);
278 static void test_visitor_in_large_number(TestInputVisitorData
*data
,
287 v
= visitor_input_test_init(data
, "-18446744073709551616"); /* -2^64 */
289 visit_type_number(v
, NULL
, &res
, &error_abort
);
290 g_assert_cmpfloat(res
, ==, -18446744073709552e3
);
292 visit_type_int(v
, NULL
, &i64
, &err
);
293 error_free_or_abort(&err
);
295 visit_type_uint64(v
, NULL
, &u64
, &err
);
296 error_free_or_abort(&err
);
299 static void test_visitor_in_number_keyval(TestInputVisitorData
*data
,
302 double res
= 0, value
= 3.14;
306 v
= visitor_input_test_init_full(data
, true, "%f", value
);
308 visit_type_number(v
, NULL
, &res
, &err
);
309 error_free_or_abort(&err
);
312 static void test_visitor_in_number_str_keyval(TestInputVisitorData
*data
,
315 double res
= 0, value
= 3.14;
319 v
= visitor_input_test_init_full(data
, true, "\"3.14\"");
321 visit_type_number(v
, NULL
, &res
, &error_abort
);
322 g_assert_cmpfloat(res
, ==, value
);
324 v
= visitor_input_test_init_full(data
, true, "\"inf\"");
326 visit_type_number(v
, NULL
, &res
, &err
);
327 error_free_or_abort(&err
);
330 static void test_visitor_in_number_str_fail(TestInputVisitorData
*data
,
337 v
= visitor_input_test_init(data
, "\"3.14\"");
339 visit_type_number(v
, NULL
, &res
, &err
);
340 error_free_or_abort(&err
);
343 static void test_visitor_in_size_str_keyval(TestInputVisitorData
*data
,
346 uint64_t res
, value
= 500 * 1024 * 1024;
349 v
= visitor_input_test_init_full(data
, true, "\"500M\"");
351 visit_type_size(v
, NULL
, &res
, &error_abort
);
352 g_assert_cmpfloat(res
, ==, value
);
355 static void test_visitor_in_size_str_fail(TestInputVisitorData
*data
,
362 v
= visitor_input_test_init(data
, "\"500M\"");
364 visit_type_size(v
, NULL
, &res
, &err
);
365 error_free_or_abort(&err
);
368 static void test_visitor_in_string(TestInputVisitorData
*data
,
371 char *res
= NULL
, *value
= (char *) "Q E M U";
374 v
= visitor_input_test_init(data
, "%s", value
);
376 visit_type_str(v
, NULL
, &res
, &error_abort
);
377 g_assert_cmpstr(res
, ==, value
);
382 static void test_visitor_in_enum(TestInputVisitorData
*data
,
388 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
391 v
= visitor_input_test_init(data
, "%s", EnumOne_str(i
));
393 visit_type_EnumOne(v
, NULL
, &res
, &error_abort
);
394 g_assert_cmpint(i
, ==, res
);
399 static void test_visitor_in_struct(TestInputVisitorData
*data
,
402 TestStruct
*p
= NULL
;
405 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
407 visit_type_TestStruct(v
, NULL
, &p
, &error_abort
);
408 g_assert_cmpint(p
->integer
, ==, -42);
409 g_assert(p
->boolean
== true);
410 g_assert_cmpstr(p
->string
, ==, "foo");
416 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
419 g_autoptr(UserDefTwo
) udp
= NULL
;
422 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', "
423 "'dict1': { 'string1': 'string1', "
424 "'dict2': { 'userdef': { 'integer': 42, "
425 "'string': 'string' }, 'string': 'string2'}}}");
427 visit_type_UserDefTwo(v
, NULL
, &udp
, &error_abort
);
429 g_assert_cmpstr(udp
->string0
, ==, "string0");
430 g_assert_cmpstr(udp
->dict1
->string1
, ==, "string1");
431 g_assert_cmpint(udp
->dict1
->dict2
->userdef
->integer
, ==, 42);
432 g_assert_cmpstr(udp
->dict1
->dict2
->userdef
->string
, ==, "string");
433 g_assert_cmpstr(udp
->dict1
->dict2
->string
, ==, "string2");
434 g_assert(!udp
->dict1
->dict3
);
437 static void test_visitor_in_list(TestInputVisitorData
*data
,
440 UserDefOneList
*item
, *head
= NULL
;
444 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
446 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
447 g_assert(head
!= NULL
);
449 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
450 g_autofree
char *string
= g_strdup_printf("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_list_struct(TestInputVisitorData
*data
,
468 const char *int_member
[] = {
469 "integer", "s8", "s16", "s32", "s64", "u8", "u16", "u32", "u64" };
470 g_autoptr(GString
) json
= g_string_new("");
473 g_autoptr(ArrayStruct
) arrs
= NULL
;
481 uint16List
*u16_list
;
482 uint32List
*u32_list
;
483 uint64List
*u64_list
;
484 numberList
*num_list
;
488 g_string_append_printf(json
, "{");
490 for (i
= 0; i
< G_N_ELEMENTS(int_member
); i
++) {
491 g_string_append_printf(json
, "'%s': [", int_member
[i
]);
493 for (j
= 0; j
< 32; j
++) {
494 g_string_append_printf(json
, "%s%d", sep
, j
);
497 g_string_append_printf(json
, "], ");
500 g_string_append_printf(json
, "'number': [");
502 for (i
= 0; i
< 32; i
++) {
503 g_string_append_printf(json
, "%s%f", sep
, (double)i
/ 3);
506 g_string_append_printf(json
, "], ");
508 g_string_append_printf(json
, "'boolean': [");
510 for (i
= 0; i
< 32; i
++) {
511 g_string_append_printf(json
, "%s%s",
512 sep
, i
% 3 == 0 ? "true" : "false");
515 g_string_append_printf(json
, "], ");
517 g_string_append_printf(json
, "'string': [");
519 for (i
= 0; i
< 32; i
++) {
520 g_string_append_printf(json
, "%s'%d'", sep
, i
);
523 g_string_append_printf(json
, "]");
525 g_string_append_printf(json
, "}");
527 v
= visitor_input_test_init_raw(data
, json
->str
);
528 visit_type_ArrayStruct(v
, NULL
, &arrs
, &error_abort
);
531 for (int_list
= arrs
->integer
; int_list
; int_list
= int_list
->next
) {
532 g_assert_cmpint(int_list
->value
, ==, i
);
537 for (s8_list
= arrs
->s8
; s8_list
; s8_list
= s8_list
->next
) {
538 g_assert_cmpint(s8_list
->value
, ==, i
);
543 for (s16_list
= arrs
->s16
; s16_list
; s16_list
= s16_list
->next
) {
544 g_assert_cmpint(s16_list
->value
, ==, i
);
549 for (s32_list
= arrs
->s32
; s32_list
; s32_list
= s32_list
->next
) {
550 g_assert_cmpint(s32_list
->value
, ==, i
);
555 for (s64_list
= arrs
->s64
; s64_list
; s64_list
= s64_list
->next
) {
556 g_assert_cmpint(s64_list
->value
, ==, i
);
561 for (u8_list
= arrs
->u8
; u8_list
; u8_list
= u8_list
->next
) {
562 g_assert_cmpint(u8_list
->value
, ==, i
);
567 for (u16_list
= arrs
->u16
; u16_list
; u16_list
= u16_list
->next
) {
568 g_assert_cmpint(u16_list
->value
, ==, i
);
573 for (u32_list
= arrs
->u32
; u32_list
; u32_list
= u32_list
->next
) {
574 g_assert_cmpint(u32_list
->value
, ==, i
);
579 for (u64_list
= arrs
->u64
; u64_list
; u64_list
= u64_list
->next
) {
580 g_assert_cmpint(u64_list
->value
, ==, i
);
585 for (num_list
= arrs
->number
; num_list
; num_list
= num_list
->next
) {
586 char expected
[32], actual
[32];
588 sprintf(expected
, "%.6f", (double)i
/ 3);
589 sprintf(actual
, "%.6f", num_list
->value
);
590 g_assert_cmpstr(expected
, ==, actual
);
595 for (bool_list
= arrs
->boolean
; bool_list
; bool_list
= bool_list
->next
) {
596 g_assert_cmpint(bool_list
->value
, ==, i
% 3 == 0);
601 for (str_list
= arrs
->string
; str_list
; str_list
= str_list
->next
) {
604 sprintf(expected
, "%d", i
);
605 g_assert_cmpstr(str_list
->value
, ==, expected
);
610 static void test_visitor_in_any(TestInputVisitorData
*data
,
622 v
= visitor_input_test_init(data
, "-42");
623 visit_type_any(v
, NULL
, &res
, &error_abort
);
624 qnum
= qobject_to(QNum
, res
);
626 g_assert(qnum_get_try_int(qnum
, &val
));
627 g_assert_cmpint(val
, ==, -42);
630 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
631 visit_type_any(v
, NULL
, &res
, &error_abort
);
632 qdict
= qobject_to(QDict
, res
);
633 g_assert(qdict
&& qdict_size(qdict
) == 3);
634 qobj
= qdict_get(qdict
, "integer");
636 qnum
= qobject_to(QNum
, qobj
);
638 g_assert(qnum_get_try_int(qnum
, &val
));
639 g_assert_cmpint(val
, ==, -42);
640 qobj
= qdict_get(qdict
, "boolean");
642 qbool
= qobject_to(QBool
, qobj
);
644 g_assert(qbool_get_bool(qbool
) == true);
645 qobj
= qdict_get(qdict
, "string");
647 qstring
= qobject_to(QString
, qobj
);
649 g_assert_cmpstr(qstring_get_str(qstring
), ==, "foo");
653 static void test_visitor_in_null(TestInputVisitorData
*data
,
662 * FIXME: Since QAPI doesn't know the 'null' type yet, we can't
663 * test visit_type_null() by reading into a QAPI struct then
664 * checking that it was populated correctly. The best we can do
665 * for now is ensure that we consumed null from the input, proven
666 * by the fact that we can't re-read the key; and that we detect
667 * when input is not null.
670 v
= visitor_input_test_init_full(data
, false,
671 "{ 'a': null, 'b': '' }");
672 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
673 visit_type_null(v
, "a", &null
, &error_abort
);
674 g_assert(qobject_type(QOBJECT(null
)) == QTYPE_QNULL
);
676 visit_type_null(v
, "b", &null
, &err
);
677 error_free_or_abort(&err
);
679 visit_type_str(v
, "c", &tmp
, &err
);
680 error_free_or_abort(&err
);
682 visit_check_struct(v
, &error_abort
);
683 visit_end_struct(v
, NULL
);
686 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
690 g_autoptr(UserDefFlatUnion
) tmp
= NULL
;
691 UserDefUnionBase
*base
;
693 v
= visitor_input_test_init(data
,
694 "{ 'enum1': 'value1', "
697 "'boolean': true }");
699 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &error_abort
);
700 g_assert_cmpint(tmp
->enum1
, ==, ENUM_ONE_VALUE1
);
701 g_assert_cmpstr(tmp
->string
, ==, "str");
702 g_assert_cmpint(tmp
->integer
, ==, 41);
703 g_assert_cmpint(tmp
->u
.value1
.boolean
, ==, true);
705 base
= qapi_UserDefFlatUnion_base(tmp
);
706 g_assert(&base
->enum1
== &tmp
->enum1
);
709 static void test_visitor_in_union_in_union(TestInputVisitorData
*data
,
713 g_autoptr(TestUnionInUnion
) tmp
= NULL
;
715 v
= visitor_input_test_init(data
,
716 "{ 'type': 'value-a', "
717 " 'type-a': 'value-a1', "
719 " 'name': 'fish' }");
721 visit_type_TestUnionInUnion(v
, NULL
, &tmp
, &error_abort
);
722 g_assert_cmpint(tmp
->type
, ==, TEST_UNION_ENUM_VALUE_A
);
723 g_assert_cmpint(tmp
->u
.value_a
.type_a
, ==, TEST_UNION_ENUMA_VALUE_A1
);
724 g_assert_cmpint(tmp
->u
.value_a
.u
.value_a1
.integer
, ==, 2);
725 g_assert_cmpint(strcmp(tmp
->u
.value_a
.u
.value_a1
.name
, "fish"), ==, 0);
727 qapi_free_TestUnionInUnion(tmp
);
729 v
= visitor_input_test_init(data
,
730 "{ 'type': 'value-a', "
731 " 'type-a': 'value-a2', "
733 " 'size': 87539319 }");
735 visit_type_TestUnionInUnion(v
, NULL
, &tmp
, &error_abort
);
736 g_assert_cmpint(tmp
->type
, ==, TEST_UNION_ENUM_VALUE_A
);
737 g_assert_cmpint(tmp
->u
.value_a
.type_a
, ==, TEST_UNION_ENUMA_VALUE_A2
);
738 g_assert_cmpint(tmp
->u
.value_a
.u
.value_a2
.integer
, ==, 1729);
739 g_assert_cmpint(tmp
->u
.value_a
.u
.value_a2
.size
, ==, 87539319);
741 qapi_free_TestUnionInUnion(tmp
);
743 v
= visitor_input_test_init(data
,
744 "{ 'type': 'value-b', "
748 visit_type_TestUnionInUnion(v
, NULL
, &tmp
, &error_abort
);
749 g_assert_cmpint(tmp
->type
, ==, TEST_UNION_ENUM_VALUE_B
);
750 g_assert_cmpint(tmp
->u
.value_b
.integer
, ==, 1729);
751 g_assert_cmpint(tmp
->u
.value_b
.onoff
, ==, true);
754 static void test_visitor_in_alternate(TestInputVisitorData
*data
,
758 UserDefAlternate
*tmp
;
761 v
= visitor_input_test_init(data
, "42");
762 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
763 g_assert_cmpint(tmp
->type
, ==, QTYPE_QNUM
);
764 g_assert_cmpint(tmp
->u
.i
, ==, 42);
765 qapi_free_UserDefAlternate(tmp
);
767 v
= visitor_input_test_init(data
, "'value1'");
768 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
769 g_assert_cmpint(tmp
->type
, ==, QTYPE_QSTRING
);
770 g_assert_cmpint(tmp
->u
.e
, ==, ENUM_ONE_VALUE1
);
771 qapi_free_UserDefAlternate(tmp
);
773 v
= visitor_input_test_init(data
, "null");
774 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
775 g_assert_cmpint(tmp
->type
, ==, QTYPE_QNULL
);
776 qapi_free_UserDefAlternate(tmp
);
778 v
= visitor_input_test_init(data
, "{'integer':1, 'string':'str', "
779 "'enum1':'value1', 'boolean':true}");
780 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
781 g_assert_cmpint(tmp
->type
, ==, QTYPE_QDICT
);
782 g_assert_cmpint(tmp
->u
.udfu
.integer
, ==, 1);
783 g_assert_cmpstr(tmp
->u
.udfu
.string
, ==, "str");
784 g_assert_cmpint(tmp
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
785 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.boolean
, ==, true);
786 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
787 qapi_free_UserDefAlternate(tmp
);
789 v
= visitor_input_test_init(data
, "{ 'alt': 42 }");
790 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
791 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QNUM
);
792 g_assert_cmpint(wrap
->alt
->u
.i
, ==, 42);
793 qapi_free_WrapAlternate(wrap
);
795 v
= visitor_input_test_init(data
, "{ 'alt': 'value1' }");
796 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
797 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QSTRING
);
798 g_assert_cmpint(wrap
->alt
->u
.e
, ==, ENUM_ONE_VALUE1
);
799 qapi_free_WrapAlternate(wrap
);
801 v
= visitor_input_test_init(data
, "{ 'alt': {'integer':1, 'string':'str', "
802 "'enum1':'value1', 'boolean':true} }");
803 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
804 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QDICT
);
805 g_assert_cmpint(wrap
->alt
->u
.udfu
.integer
, ==, 1);
806 g_assert_cmpstr(wrap
->alt
->u
.udfu
.string
, ==, "str");
807 g_assert_cmpint(wrap
->alt
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
808 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.boolean
, ==, true);
809 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
810 qapi_free_WrapAlternate(wrap
);
813 static void test_visitor_in_alternate_number(TestInputVisitorData
*data
,
826 v
= visitor_input_test_init(data
, "42");
827 visit_type_AltEnumBool(v
, NULL
, &aeb
, &err
);
828 error_free_or_abort(&err
);
829 qapi_free_AltEnumBool(aeb
);
831 v
= visitor_input_test_init(data
, "42");
832 visit_type_AltEnumNum(v
, NULL
, &aen
, &error_abort
);
833 g_assert_cmpint(aen
->type
, ==, QTYPE_QNUM
);
834 g_assert_cmpfloat(aen
->u
.n
, ==, 42);
835 qapi_free_AltEnumNum(aen
);
837 v
= visitor_input_test_init(data
, "42");
838 visit_type_AltNumEnum(v
, NULL
, &ans
, &error_abort
);
839 g_assert_cmpint(ans
->type
, ==, QTYPE_QNUM
);
840 g_assert_cmpfloat(ans
->u
.n
, ==, 42);
841 qapi_free_AltNumEnum(ans
);
843 v
= visitor_input_test_init(data
, "42");
844 visit_type_AltEnumInt(v
, NULL
, &asi
, &error_abort
);
845 g_assert_cmpint(asi
->type
, ==, QTYPE_QNUM
);
846 g_assert_cmpint(asi
->u
.i
, ==, 42);
847 qapi_free_AltEnumInt(asi
);
849 v
= visitor_input_test_init(data
, "42");
850 visit_type_AltListInt(v
, NULL
, &ali
, &error_abort
);
851 g_assert_cmpint(ali
->type
, ==, QTYPE_QNUM
);
852 g_assert_cmpint(ali
->u
.i
, ==, 42);
853 qapi_free_AltListInt(ali
);
855 /* Parsing a double */
857 v
= visitor_input_test_init(data
, "42.5");
858 visit_type_AltEnumBool(v
, NULL
, &aeb
, &err
);
859 error_free_or_abort(&err
);
860 qapi_free_AltEnumBool(aeb
);
862 v
= visitor_input_test_init(data
, "42.5");
863 visit_type_AltEnumNum(v
, NULL
, &aen
, &error_abort
);
864 g_assert_cmpint(aen
->type
, ==, QTYPE_QNUM
);
865 g_assert_cmpfloat(aen
->u
.n
, ==, 42.5);
866 qapi_free_AltEnumNum(aen
);
868 v
= visitor_input_test_init(data
, "42.5");
869 visit_type_AltNumEnum(v
, NULL
, &ans
, &error_abort
);
870 g_assert_cmpint(ans
->type
, ==, QTYPE_QNUM
);
871 g_assert_cmpfloat(ans
->u
.n
, ==, 42.5);
872 qapi_free_AltNumEnum(ans
);
874 v
= visitor_input_test_init(data
, "42.5");
875 visit_type_AltEnumInt(v
, NULL
, &asi
, &err
);
876 error_free_or_abort(&err
);
877 qapi_free_AltEnumInt(asi
);
880 static void test_visitor_in_alternate_list(TestInputVisitorData
*data
,
888 v
= visitor_input_test_init(data
, "[ 42, 43, 44 ]");
889 visit_type_AltListInt(v
, NULL
, &ali
, &error_abort
);
890 g_assert(ali
!= NULL
);
892 g_assert_cmpint(ali
->type
, ==, QTYPE_QLIST
);
893 for (i
= 0, item
= ali
->u
.l
; item
; item
= item
->next
, i
++) {
894 g_assert_cmpint(item
->value
, ==, 42 + i
);
897 qapi_free_AltListInt(ali
);
900 /* An empty list is valid */
901 v
= visitor_input_test_init(data
, "[]");
902 visit_type_AltListInt(v
, NULL
, &ali
, &error_abort
);
903 g_assert(ali
!= NULL
);
905 g_assert_cmpint(ali
->type
, ==, QTYPE_QLIST
);
907 qapi_free_AltListInt(ali
);
911 static void input_visitor_test_add(const char *testpath
,
912 const void *user_data
,
913 void (*test_func
)(TestInputVisitorData
*data
,
914 const void *user_data
))
916 g_test_add(testpath
, TestInputVisitorData
, user_data
, NULL
, test_func
,
917 visitor_input_teardown
);
920 static void test_visitor_in_errors(TestInputVisitorData
*data
,
923 TestStruct
*p
= NULL
;
927 UserDefTwo
*r
= NULL
;
928 WrapAlternate
*s
= NULL
;
930 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', "
933 visit_type_TestStruct(v
, NULL
, &p
, &err
);
934 error_free_or_abort(&err
);
937 v
= visitor_input_test_init(data
, "[ '1', '2', false, '3' ]");
938 visit_type_strList(v
, NULL
, &q
, &err
);
939 error_free_or_abort(&err
);
942 v
= visitor_input_test_init(data
, "{ 'str':'hi' }");
943 visit_type_UserDefTwo(v
, NULL
, &r
, &err
);
944 error_free_or_abort(&err
);
947 v
= visitor_input_test_init(data
, "{ }");
948 visit_type_WrapAlternate(v
, NULL
, &s
, &err
);
949 error_free_or_abort(&err
);
953 static void test_visitor_in_wrong_type(TestInputVisitorData
*data
,
956 TestStruct
*p
= NULL
;
962 /* Make sure arrays and structs cannot be confused */
964 v
= visitor_input_test_init(data
, "[]");
965 visit_type_TestStruct(v
, NULL
, &p
, &err
);
966 error_free_or_abort(&err
);
969 v
= visitor_input_test_init(data
, "{}");
970 visit_type_strList(v
, NULL
, &q
, &err
);
971 error_free_or_abort(&err
);
974 /* Make sure primitives and struct cannot be confused */
976 v
= visitor_input_test_init(data
, "1");
977 visit_type_TestStruct(v
, NULL
, &p
, &err
);
978 error_free_or_abort(&err
);
981 v
= visitor_input_test_init(data
, "{}");
982 visit_type_int(v
, NULL
, &i
, &err
);
983 error_free_or_abort(&err
);
985 /* Make sure primitives and arrays cannot be confused */
987 v
= visitor_input_test_init(data
, "1");
988 visit_type_strList(v
, NULL
, &q
, &err
);
989 error_free_or_abort(&err
);
992 v
= visitor_input_test_init(data
, "[]");
993 visit_type_int(v
, NULL
, &i
, &err
);
994 error_free_or_abort(&err
);
997 static void test_visitor_in_fail_struct(TestInputVisitorData
*data
,
1000 TestStruct
*p
= NULL
;
1004 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
1006 visit_type_TestStruct(v
, NULL
, &p
, &err
);
1007 error_free_or_abort(&err
);
1011 static void test_visitor_in_fail_struct_nested(TestInputVisitorData
*data
,
1014 UserDefTwo
*udp
= NULL
;
1018 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
1020 visit_type_UserDefTwo(v
, NULL
, &udp
, &err
);
1021 error_free_or_abort(&err
);
1025 static void test_visitor_in_fail_struct_in_list(TestInputVisitorData
*data
,
1028 UserDefOneList
*head
= NULL
;
1032 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
1034 visit_type_UserDefOneList(v
, NULL
, &head
, &err
);
1035 error_free_or_abort(&err
);
1039 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
), &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", &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_flat(TestInputVisitorData
*data
,
1146 UserDefFlatUnion
*tmp
= NULL
;
1150 v
= visitor_input_test_init(data
, "{ 'enum1': 'value2', 'string': 'c', 'integer': 41, 'boolean': true }");
1152 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &err
);
1153 error_free_or_abort(&err
);
1157 static void test_visitor_in_fail_union_flat_no_discrim(TestInputVisitorData
*data
,
1160 UserDefFlatUnion2
*tmp
= NULL
;
1164 /* test situation where discriminator field ('enum1' here) is missing */
1165 v
= visitor_input_test_init(data
, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
1167 visit_type_UserDefFlatUnion2(v
, NULL
, &tmp
, &err
);
1168 error_free_or_abort(&err
);
1172 static void test_visitor_in_fail_alternate(TestInputVisitorData
*data
,
1175 UserDefAlternate
*tmp
;
1179 v
= visitor_input_test_init(data
, "3.14");
1181 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
1182 error_free_or_abort(&err
);
1186 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1187 const QLitObject
*qlit
)
1189 g_autoptr(SchemaInfoList
) schema
= NULL
;
1190 QObject
*obj
= qobject_from_qlit(qlit
);
1193 v
= qobject_input_visitor_new(obj
);
1195 visit_type_SchemaInfoList(v
, NULL
, &schema
, &error_abort
);
1202 static void test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1205 do_test_visitor_in_qmp_introspect(data
, &test_qmp_schema_qlit
);
1208 int main(int argc
, char **argv
)
1210 g_test_init(&argc
, &argv
, NULL
);
1212 input_visitor_test_add("/visitor/input/int",
1213 NULL
, test_visitor_in_int
);
1214 input_visitor_test_add("/visitor/input/uint",
1215 NULL
, test_visitor_in_uint
);
1216 input_visitor_test_add("/visitor/input/int_overflow",
1217 NULL
, test_visitor_in_int_overflow
);
1218 input_visitor_test_add("/visitor/input/int_keyval",
1219 NULL
, test_visitor_in_int_keyval
);
1220 input_visitor_test_add("/visitor/input/int_str_keyval",
1221 NULL
, test_visitor_in_int_str_keyval
);
1222 input_visitor_test_add("/visitor/input/int_str_fail",
1223 NULL
, test_visitor_in_int_str_fail
);
1224 input_visitor_test_add("/visitor/input/bool",
1225 NULL
, test_visitor_in_bool
);
1226 input_visitor_test_add("/visitor/input/bool_keyval",
1227 NULL
, test_visitor_in_bool_keyval
);
1228 input_visitor_test_add("/visitor/input/bool_str_keyval",
1229 NULL
, test_visitor_in_bool_str_keyval
);
1230 input_visitor_test_add("/visitor/input/bool_str_fail",
1231 NULL
, test_visitor_in_bool_str_fail
);
1232 input_visitor_test_add("/visitor/input/number",
1233 NULL
, test_visitor_in_number
);
1234 input_visitor_test_add("/visitor/input/large_number",
1235 NULL
, test_visitor_in_large_number
);
1236 input_visitor_test_add("/visitor/input/number_keyval",
1237 NULL
, test_visitor_in_number_keyval
);
1238 input_visitor_test_add("/visitor/input/number_str_keyval",
1239 NULL
, test_visitor_in_number_str_keyval
);
1240 input_visitor_test_add("/visitor/input/number_str_fail",
1241 NULL
, test_visitor_in_number_str_fail
);
1242 input_visitor_test_add("/visitor/input/size_str_keyval",
1243 NULL
, test_visitor_in_size_str_keyval
);
1244 input_visitor_test_add("/visitor/input/size_str_fail",
1245 NULL
, test_visitor_in_size_str_fail
);
1246 input_visitor_test_add("/visitor/input/string",
1247 NULL
, test_visitor_in_string
);
1248 input_visitor_test_add("/visitor/input/enum",
1249 NULL
, test_visitor_in_enum
);
1250 input_visitor_test_add("/visitor/input/struct",
1251 NULL
, test_visitor_in_struct
);
1252 input_visitor_test_add("/visitor/input/struct-nested",
1253 NULL
, test_visitor_in_struct_nested
);
1254 input_visitor_test_add("/visitor/input/list2",
1255 NULL
, test_visitor_in_list_struct
);
1256 input_visitor_test_add("/visitor/input/list",
1257 NULL
, test_visitor_in_list
);
1258 input_visitor_test_add("/visitor/input/any",
1259 NULL
, test_visitor_in_any
);
1260 input_visitor_test_add("/visitor/input/null",
1261 NULL
, test_visitor_in_null
);
1262 input_visitor_test_add("/visitor/input/union-flat",
1263 NULL
, test_visitor_in_union_flat
);
1264 input_visitor_test_add("/visitor/input/union-in-union",
1265 NULL
, test_visitor_in_union_in_union
);
1266 input_visitor_test_add("/visitor/input/alternate",
1267 NULL
, test_visitor_in_alternate
);
1268 input_visitor_test_add("/visitor/input/errors",
1269 NULL
, test_visitor_in_errors
);
1270 input_visitor_test_add("/visitor/input/wrong-type",
1271 NULL
, test_visitor_in_wrong_type
);
1272 input_visitor_test_add("/visitor/input/alternate-number",
1273 NULL
, test_visitor_in_alternate_number
);
1274 input_visitor_test_add("/visitor/input/alternate-list",
1275 NULL
, test_visitor_in_alternate_list
);
1276 input_visitor_test_add("/visitor/input/fail/struct",
1277 NULL
, test_visitor_in_fail_struct
);
1278 input_visitor_test_add("/visitor/input/fail/struct-nested",
1279 NULL
, test_visitor_in_fail_struct_nested
);
1280 input_visitor_test_add("/visitor/input/fail/struct-in-list",
1281 NULL
, test_visitor_in_fail_struct_in_list
);
1282 input_visitor_test_add("/visitor/input/fail/struct-missing",
1283 NULL
, test_visitor_in_fail_struct_missing
);
1284 input_visitor_test_add("/visitor/input/fail/list",
1285 NULL
, test_visitor_in_fail_list
);
1286 input_visitor_test_add("/visitor/input/fail/list-nested",
1287 NULL
, test_visitor_in_fail_list_nested
);
1288 input_visitor_test_add("/visitor/input/fail/union-flat",
1289 NULL
, test_visitor_in_fail_union_flat
);
1290 input_visitor_test_add("/visitor/input/fail/union-flat-no-discriminator",
1291 NULL
, test_visitor_in_fail_union_flat_no_discrim
);
1292 input_visitor_test_add("/visitor/input/fail/alternate",
1293 NULL
, test_visitor_in_fail_alternate
);
1294 input_visitor_test_add("/visitor/input/qapi-introspect",
1295 NULL
, test_visitor_in_qmp_introspect
);