2 * QMP Input Visitor unit-tests.
4 * Copyright (C) 2011, 2015 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
16 #include "qemu-common.h"
17 #include "qapi/qmp-input-visitor.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "qapi/qmp/types.h"
22 typedef struct TestInputVisitorData
{
25 } TestInputVisitorData
;
27 static void visitor_input_teardown(TestInputVisitorData
*data
,
30 qobject_decref(data
->obj
);
34 qmp_input_visitor_cleanup(data
->qiv
);
39 /* This is provided instead of a test setup function so that the JSON
40 string used by the tests are kept in the test functions (and not
42 static GCC_FMT_ATTR(2, 3)
43 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
44 const char *json_string
, ...)
49 va_start(ap
, json_string
);
50 data
->obj
= qobject_from_jsonv(json_string
, &ap
);
53 g_assert(data
->obj
!= NULL
);
55 data
->qiv
= qmp_input_visitor_new(data
->obj
);
56 g_assert(data
->qiv
!= NULL
);
58 v
= qmp_input_get_visitor(data
->qiv
);
64 /* similar to visitor_input_test_init(), but does not expect a string
65 * literal/format json_string argument and so can be used for
66 * programatically generated strings (and we can't pass in programatically
67 * generated strings via %s format parameters since qobject_from_jsonv()
68 * will wrap those in double-quotes and treat the entire object as a
71 static Visitor
*visitor_input_test_init_raw(TestInputVisitorData
*data
,
72 const char *json_string
)
76 data
->obj
= qobject_from_json(json_string
);
78 g_assert(data
->obj
!= NULL
);
80 data
->qiv
= qmp_input_visitor_new(data
->obj
);
81 g_assert(data
->qiv
!= NULL
);
83 v
= qmp_input_get_visitor(data
->qiv
);
89 static void test_visitor_in_int(TestInputVisitorData
*data
,
92 int64_t res
= 0, value
= -42;
96 v
= visitor_input_test_init(data
, "%" PRId64
, value
);
98 visit_type_int(v
, &res
, NULL
, &err
);
100 g_assert_cmpint(res
, ==, value
);
103 static void test_visitor_in_int_overflow(TestInputVisitorData
*data
,
110 /* this will overflow a Qint/int64, so should be deserialized into
111 * a QFloat/double field instead, leading to an error if we pass it
112 * to visit_type_int. confirm this.
114 v
= visitor_input_test_init(data
, "%f", DBL_MAX
);
116 visit_type_int(v
, &res
, NULL
, &err
);
121 static void test_visitor_in_bool(TestInputVisitorData
*data
,
128 v
= visitor_input_test_init(data
, "true");
130 visit_type_bool(v
, &res
, NULL
, &err
);
132 g_assert_cmpint(res
, ==, true);
135 static void test_visitor_in_number(TestInputVisitorData
*data
,
138 double res
= 0, value
= 3.14;
142 v
= visitor_input_test_init(data
, "%f", value
);
144 visit_type_number(v
, &res
, NULL
, &err
);
146 g_assert_cmpfloat(res
, ==, value
);
149 static void test_visitor_in_string(TestInputVisitorData
*data
,
152 char *res
= NULL
, *value
= (char *) "Q E M U";
156 v
= visitor_input_test_init(data
, "%s", value
);
158 visit_type_str(v
, &res
, NULL
, &err
);
160 g_assert_cmpstr(res
, ==, value
);
165 static void test_visitor_in_enum(TestInputVisitorData
*data
,
172 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
175 v
= visitor_input_test_init(data
, "%s", EnumOne_lookup
[i
]);
177 visit_type_EnumOne(v
, &res
, NULL
, &err
);
179 g_assert_cmpint(i
, ==, res
);
181 visitor_input_teardown(data
, NULL
);
188 typedef struct TestStruct
195 static void visit_type_TestStruct(Visitor
*v
, TestStruct
**obj
,
196 const char *name
, Error
**errp
)
200 visit_start_struct(v
, (void **)obj
, "TestStruct", name
, sizeof(TestStruct
),
205 visit_type_int(v
, &(*obj
)->integer
, "integer", &err
);
209 visit_type_bool(v
, &(*obj
)->boolean
, "boolean", &err
);
213 visit_type_str(v
, &(*obj
)->string
, "string", &err
);
216 error_propagate(errp
, err
);
218 visit_end_struct(v
, &err
);
220 error_propagate(errp
, err
);
223 static void test_visitor_in_struct(TestInputVisitorData
*data
,
226 TestStruct
*p
= NULL
;
230 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
232 visit_type_TestStruct(v
, &p
, NULL
, &err
);
234 g_assert_cmpint(p
->integer
, ==, -42);
235 g_assert(p
->boolean
== true);
236 g_assert_cmpstr(p
->string
, ==, "foo");
242 static void check_and_free_str(char *str
, const char *cmp
)
244 g_assert_cmpstr(str
, ==, cmp
);
248 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
251 UserDefTwo
*udp
= NULL
;
255 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', "
256 "'dict1': { 'string1': 'string1', "
257 "'dict2': { 'userdef': { 'integer': 42, "
258 "'string': 'string' }, 'string': 'string2'}}}");
260 visit_type_UserDefTwo(v
, &udp
, NULL
, &err
);
263 check_and_free_str(udp
->string0
, "string0");
264 check_and_free_str(udp
->dict1
->string1
, "string1");
265 g_assert_cmpint(udp
->dict1
->dict2
->userdef
->base
->integer
, ==, 42);
266 check_and_free_str(udp
->dict1
->dict2
->userdef
->string
, "string");
267 check_and_free_str(udp
->dict1
->dict2
->string
, "string2");
268 g_assert(udp
->dict1
->has_dict3
== false);
270 g_free(udp
->dict1
->dict2
->userdef
);
271 g_free(udp
->dict1
->dict2
);
276 static void test_visitor_in_list(TestInputVisitorData
*data
,
279 UserDefOneList
*item
, *head
= NULL
;
284 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
286 visit_type_UserDefOneList(v
, &head
, NULL
, &err
);
288 g_assert(head
!= NULL
);
290 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
293 snprintf(string
, sizeof(string
), "string%d", i
);
294 g_assert_cmpstr(item
->value
->string
, ==, string
);
295 g_assert_cmpint(item
->value
->base
->integer
, ==, 42 + i
);
298 qapi_free_UserDefOneList(head
);
301 static void test_visitor_in_any(TestInputVisitorData
*data
,
313 v
= visitor_input_test_init(data
, "-42");
314 visit_type_any(v
, &res
, NULL
, &err
);
316 qint
= qobject_to_qint(res
);
318 g_assert_cmpint(qint_get_int(qint
), ==, -42);
321 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
322 visit_type_any(v
, &res
, NULL
, &err
);
324 qdict
= qobject_to_qdict(res
);
325 g_assert(qdict
&& qdict_size(qdict
) == 3);
326 qobj
= qdict_get(qdict
, "integer");
328 qint
= qobject_to_qint(qobj
);
330 g_assert_cmpint(qint_get_int(qint
), ==, -42);
331 qobj
= qdict_get(qdict
, "boolean");
333 qbool
= qobject_to_qbool(qobj
);
335 g_assert(qbool_get_bool(qbool
) == true);
336 qobj
= qdict_get(qdict
, "string");
338 qstring
= qobject_to_qstring(qobj
);
340 g_assert_cmpstr(qstring_get_str(qstring
), ==, "foo");
344 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
349 UserDefFlatUnion
*tmp
;
351 v
= visitor_input_test_init(data
,
352 "{ 'enum1': 'value1', "
355 "'boolean': true }");
357 visit_type_UserDefFlatUnion(v
, &tmp
, NULL
, &err
);
358 g_assert(err
== NULL
);
359 g_assert_cmpint(tmp
->enum1
, ==, ENUM_ONE_VALUE1
);
360 g_assert_cmpstr(tmp
->string
, ==, "str");
361 g_assert_cmpint(tmp
->integer
, ==, 41);
362 g_assert_cmpint(tmp
->value1
->boolean
, ==, true);
363 qapi_free_UserDefFlatUnion(tmp
);
366 static void test_visitor_in_alternate(TestInputVisitorData
*data
,
371 UserDefAlternate
*tmp
;
373 v
= visitor_input_test_init(data
, "42");
375 visit_type_UserDefAlternate(v
, &tmp
, NULL
, &err
);
376 g_assert(err
== NULL
);
377 g_assert_cmpint(tmp
->kind
, ==, USER_DEF_ALTERNATE_KIND_I
);
378 g_assert_cmpint(tmp
->i
, ==, 42);
379 qapi_free_UserDefAlternate(tmp
);
382 static void test_native_list_integer_helper(TestInputVisitorData
*data
,
384 UserDefNativeListUnionKind kind
)
386 UserDefNativeListUnion
*cvalue
= NULL
;
389 GString
*gstr_list
= g_string_new("");
390 GString
*gstr_union
= g_string_new("");
393 for (i
= 0; i
< 32; i
++) {
394 g_string_append_printf(gstr_list
, "%d", i
);
396 g_string_append(gstr_list
, ", ");
399 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
400 UserDefNativeListUnionKind_lookup
[kind
],
402 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
404 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
405 g_assert(err
== NULL
);
406 g_assert(cvalue
!= NULL
);
407 g_assert_cmpint(cvalue
->kind
, ==, kind
);
410 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
: {
411 intList
*elem
= NULL
;
412 for (i
= 0, elem
= cvalue
->integer
; elem
; elem
= elem
->next
, i
++) {
413 g_assert_cmpint(elem
->value
, ==, i
);
417 case USER_DEF_NATIVE_LIST_UNION_KIND_S8
: {
418 int8List
*elem
= NULL
;
419 for (i
= 0, elem
= cvalue
->s8
; elem
; elem
= elem
->next
, i
++) {
420 g_assert_cmpint(elem
->value
, ==, i
);
424 case USER_DEF_NATIVE_LIST_UNION_KIND_S16
: {
425 int16List
*elem
= NULL
;
426 for (i
= 0, elem
= cvalue
->s16
; elem
; elem
= elem
->next
, i
++) {
427 g_assert_cmpint(elem
->value
, ==, i
);
431 case USER_DEF_NATIVE_LIST_UNION_KIND_S32
: {
432 int32List
*elem
= NULL
;
433 for (i
= 0, elem
= cvalue
->s32
; elem
; elem
= elem
->next
, i
++) {
434 g_assert_cmpint(elem
->value
, ==, i
);
438 case USER_DEF_NATIVE_LIST_UNION_KIND_S64
: {
439 int64List
*elem
= NULL
;
440 for (i
= 0, elem
= cvalue
->s64
; elem
; elem
= elem
->next
, i
++) {
441 g_assert_cmpint(elem
->value
, ==, i
);
445 case USER_DEF_NATIVE_LIST_UNION_KIND_U8
: {
446 uint8List
*elem
= NULL
;
447 for (i
= 0, elem
= cvalue
->u8
; elem
; elem
= elem
->next
, i
++) {
448 g_assert_cmpint(elem
->value
, ==, i
);
452 case USER_DEF_NATIVE_LIST_UNION_KIND_U16
: {
453 uint16List
*elem
= NULL
;
454 for (i
= 0, elem
= cvalue
->u16
; elem
; elem
= elem
->next
, i
++) {
455 g_assert_cmpint(elem
->value
, ==, i
);
459 case USER_DEF_NATIVE_LIST_UNION_KIND_U32
: {
460 uint32List
*elem
= NULL
;
461 for (i
= 0, elem
= cvalue
->u32
; elem
; elem
= elem
->next
, i
++) {
462 g_assert_cmpint(elem
->value
, ==, i
);
466 case USER_DEF_NATIVE_LIST_UNION_KIND_U64
: {
467 uint64List
*elem
= NULL
;
468 for (i
= 0, elem
= cvalue
->u64
; elem
; elem
= elem
->next
, i
++) {
469 g_assert_cmpint(elem
->value
, ==, i
);
474 g_assert_not_reached();
477 g_string_free(gstr_union
, true);
478 g_string_free(gstr_list
, true);
479 qapi_free_UserDefNativeListUnion(cvalue
);
482 static void test_visitor_in_native_list_int(TestInputVisitorData
*data
,
485 test_native_list_integer_helper(data
, unused
,
486 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
);
489 static void test_visitor_in_native_list_int8(TestInputVisitorData
*data
,
492 test_native_list_integer_helper(data
, unused
,
493 USER_DEF_NATIVE_LIST_UNION_KIND_S8
);
496 static void test_visitor_in_native_list_int16(TestInputVisitorData
*data
,
499 test_native_list_integer_helper(data
, unused
,
500 USER_DEF_NATIVE_LIST_UNION_KIND_S16
);
503 static void test_visitor_in_native_list_int32(TestInputVisitorData
*data
,
506 test_native_list_integer_helper(data
, unused
,
507 USER_DEF_NATIVE_LIST_UNION_KIND_S32
);
510 static void test_visitor_in_native_list_int64(TestInputVisitorData
*data
,
513 test_native_list_integer_helper(data
, unused
,
514 USER_DEF_NATIVE_LIST_UNION_KIND_S64
);
517 static void test_visitor_in_native_list_uint8(TestInputVisitorData
*data
,
520 test_native_list_integer_helper(data
, unused
,
521 USER_DEF_NATIVE_LIST_UNION_KIND_U8
);
524 static void test_visitor_in_native_list_uint16(TestInputVisitorData
*data
,
527 test_native_list_integer_helper(data
, unused
,
528 USER_DEF_NATIVE_LIST_UNION_KIND_U16
);
531 static void test_visitor_in_native_list_uint32(TestInputVisitorData
*data
,
534 test_native_list_integer_helper(data
, unused
,
535 USER_DEF_NATIVE_LIST_UNION_KIND_U32
);
538 static void test_visitor_in_native_list_uint64(TestInputVisitorData
*data
,
541 test_native_list_integer_helper(data
, unused
,
542 USER_DEF_NATIVE_LIST_UNION_KIND_U64
);
545 static void test_visitor_in_native_list_bool(TestInputVisitorData
*data
,
548 UserDefNativeListUnion
*cvalue
= NULL
;
549 boolList
*elem
= NULL
;
552 GString
*gstr_list
= g_string_new("");
553 GString
*gstr_union
= g_string_new("");
556 for (i
= 0; i
< 32; i
++) {
557 g_string_append_printf(gstr_list
, "%s",
558 (i
% 3 == 0) ? "true" : "false");
560 g_string_append(gstr_list
, ", ");
563 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
565 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
567 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
568 g_assert(err
== NULL
);
569 g_assert(cvalue
!= NULL
);
570 g_assert_cmpint(cvalue
->kind
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN
);
572 for (i
= 0, elem
= cvalue
->boolean
; elem
; elem
= elem
->next
, i
++) {
573 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
576 g_string_free(gstr_union
, true);
577 g_string_free(gstr_list
, true);
578 qapi_free_UserDefNativeListUnion(cvalue
);
581 static void test_visitor_in_native_list_string(TestInputVisitorData
*data
,
584 UserDefNativeListUnion
*cvalue
= NULL
;
585 strList
*elem
= NULL
;
588 GString
*gstr_list
= g_string_new("");
589 GString
*gstr_union
= g_string_new("");
592 for (i
= 0; i
< 32; i
++) {
593 g_string_append_printf(gstr_list
, "'%d'", i
);
595 g_string_append(gstr_list
, ", ");
598 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
600 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
602 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
603 g_assert(err
== NULL
);
604 g_assert(cvalue
!= NULL
);
605 g_assert_cmpint(cvalue
->kind
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING
);
607 for (i
= 0, elem
= cvalue
->string
; elem
; elem
= elem
->next
, i
++) {
609 sprintf(str
, "%d", i
);
610 g_assert_cmpstr(elem
->value
, ==, str
);
613 g_string_free(gstr_union
, true);
614 g_string_free(gstr_list
, true);
615 qapi_free_UserDefNativeListUnion(cvalue
);
618 #define DOUBLE_STR_MAX 16
620 static void test_visitor_in_native_list_number(TestInputVisitorData
*data
,
623 UserDefNativeListUnion
*cvalue
= NULL
;
624 numberList
*elem
= NULL
;
627 GString
*gstr_list
= g_string_new("");
628 GString
*gstr_union
= g_string_new("");
631 for (i
= 0; i
< 32; i
++) {
632 g_string_append_printf(gstr_list
, "%f", (double)i
/ 3);
634 g_string_append(gstr_list
, ", ");
637 g_string_append_printf(gstr_union
, "{ 'type': 'number', 'data': [ %s ] }",
639 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
641 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
642 g_assert(err
== NULL
);
643 g_assert(cvalue
!= NULL
);
644 g_assert_cmpint(cvalue
->kind
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER
);
646 for (i
= 0, elem
= cvalue
->number
; elem
; elem
= elem
->next
, i
++) {
647 GString
*double_expected
= g_string_new("");
648 GString
*double_actual
= g_string_new("");
650 g_string_printf(double_expected
, "%.6f", (double)i
/ 3);
651 g_string_printf(double_actual
, "%.6f", elem
->value
);
652 g_assert_cmpstr(double_expected
->str
, ==, double_actual
->str
);
654 g_string_free(double_expected
, true);
655 g_string_free(double_actual
, true);
658 g_string_free(gstr_union
, true);
659 g_string_free(gstr_list
, true);
660 qapi_free_UserDefNativeListUnion(cvalue
);
663 static void input_visitor_test_add(const char *testpath
,
664 TestInputVisitorData
*data
,
665 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
667 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
668 visitor_input_teardown
);
671 static void test_visitor_in_errors(TestInputVisitorData
*data
,
674 TestStruct
*p
= NULL
;
678 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }");
680 visit_type_TestStruct(v
, &p
, NULL
, &err
);
682 /* FIXME - a failed parse should not leave a partially-allocated p
683 * for us to clean up; this could cause callers to leak memory. */
684 g_assert(p
->string
== NULL
);
691 int main(int argc
, char **argv
)
693 TestInputVisitorData in_visitor_data
;
695 g_test_init(&argc
, &argv
, NULL
);
697 input_visitor_test_add("/visitor/input/int",
698 &in_visitor_data
, test_visitor_in_int
);
699 input_visitor_test_add("/visitor/input/int_overflow",
700 &in_visitor_data
, test_visitor_in_int_overflow
);
701 input_visitor_test_add("/visitor/input/bool",
702 &in_visitor_data
, test_visitor_in_bool
);
703 input_visitor_test_add("/visitor/input/number",
704 &in_visitor_data
, test_visitor_in_number
);
705 input_visitor_test_add("/visitor/input/string",
706 &in_visitor_data
, test_visitor_in_string
);
707 input_visitor_test_add("/visitor/input/enum",
708 &in_visitor_data
, test_visitor_in_enum
);
709 input_visitor_test_add("/visitor/input/struct",
710 &in_visitor_data
, test_visitor_in_struct
);
711 input_visitor_test_add("/visitor/input/struct-nested",
712 &in_visitor_data
, test_visitor_in_struct_nested
);
713 input_visitor_test_add("/visitor/input/list",
714 &in_visitor_data
, test_visitor_in_list
);
715 input_visitor_test_add("/visitor/input/any",
716 &in_visitor_data
, test_visitor_in_any
);
717 input_visitor_test_add("/visitor/input/union-flat",
718 &in_visitor_data
, test_visitor_in_union_flat
);
719 input_visitor_test_add("/visitor/input/alternate",
720 &in_visitor_data
, test_visitor_in_alternate
);
721 input_visitor_test_add("/visitor/input/errors",
722 &in_visitor_data
, test_visitor_in_errors
);
723 input_visitor_test_add("/visitor/input/native_list/int",
725 test_visitor_in_native_list_int
);
726 input_visitor_test_add("/visitor/input/native_list/int8",
728 test_visitor_in_native_list_int8
);
729 input_visitor_test_add("/visitor/input/native_list/int16",
731 test_visitor_in_native_list_int16
);
732 input_visitor_test_add("/visitor/input/native_list/int32",
734 test_visitor_in_native_list_int32
);
735 input_visitor_test_add("/visitor/input/native_list/int64",
737 test_visitor_in_native_list_int64
);
738 input_visitor_test_add("/visitor/input/native_list/uint8",
740 test_visitor_in_native_list_uint8
);
741 input_visitor_test_add("/visitor/input/native_list/uint16",
743 test_visitor_in_native_list_uint16
);
744 input_visitor_test_add("/visitor/input/native_list/uint32",
746 test_visitor_in_native_list_uint32
);
747 input_visitor_test_add("/visitor/input/native_list/uint64",
749 test_visitor_in_native_list_uint64
);
750 input_visitor_test_add("/visitor/input/native_list/bool",
751 &in_visitor_data
, test_visitor_in_native_list_bool
);
752 input_visitor_test_add("/visitor/input/native_list/str",
754 test_visitor_in_native_list_string
);
755 input_visitor_test_add("/visitor/input/native_list/number",
757 test_visitor_in_native_list_number
);