2 * QMP Input Visitor unit-tests.
4 * Copyright (C) 2011 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
),
203 visit_type_int(v
, &(*obj
)->integer
, "integer", &err
);
204 visit_type_bool(v
, &(*obj
)->boolean
, "boolean", &err
);
205 visit_type_str(v
, &(*obj
)->string
, "string", &err
);
207 /* Always call end_struct if start_struct succeeded. */
208 error_propagate(errp
, err
);
210 visit_end_struct(v
, &err
);
212 error_propagate(errp
, err
);
215 static void test_visitor_in_struct(TestInputVisitorData
*data
,
218 TestStruct
*p
= NULL
;
222 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
224 visit_type_TestStruct(v
, &p
, NULL
, &err
);
226 g_assert_cmpint(p
->integer
, ==, -42);
227 g_assert(p
->boolean
== true);
228 g_assert_cmpstr(p
->string
, ==, "foo");
234 static void check_and_free_str(char *str
, const char *cmp
)
236 g_assert_cmpstr(str
, ==, cmp
);
240 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
243 UserDefNested
*udp
= NULL
;
247 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
249 visit_type_UserDefNested(v
, &udp
, NULL
, &err
);
252 check_and_free_str(udp
->string0
, "string0");
253 check_and_free_str(udp
->dict1
.string1
, "string1");
254 g_assert_cmpint(udp
->dict1
.dict2
.userdef1
->base
->integer
, ==, 42);
255 check_and_free_str(udp
->dict1
.dict2
.userdef1
->string
, "string");
256 check_and_free_str(udp
->dict1
.dict2
.string2
, "string2");
257 g_assert(udp
->dict1
.has_dict3
== false);
259 g_free(udp
->dict1
.dict2
.userdef1
);
263 static void test_visitor_in_list(TestInputVisitorData
*data
,
266 UserDefOneList
*item
, *head
= NULL
;
271 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
273 visit_type_UserDefOneList(v
, &head
, NULL
, &err
);
275 g_assert(head
!= NULL
);
277 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
280 snprintf(string
, sizeof(string
), "string%d", i
);
281 g_assert_cmpstr(item
->value
->string
, ==, string
);
282 g_assert_cmpint(item
->value
->base
->integer
, ==, 42 + i
);
285 qapi_free_UserDefOneList(head
);
288 static void test_visitor_in_union(TestInputVisitorData
*data
,
295 v
= visitor_input_test_init(data
, "{ 'type': 'b', 'integer': 41, 'data' : { 'integer': 42 } }");
297 visit_type_UserDefUnion(v
, &tmp
, NULL
, &err
);
298 g_assert(err
== NULL
);
299 g_assert_cmpint(tmp
->kind
, ==, USER_DEF_UNION_KIND_B
);
300 g_assert_cmpint(tmp
->integer
, ==, 41);
301 g_assert_cmpint(tmp
->b
->integer
, ==, 42);
302 qapi_free_UserDefUnion(tmp
);
305 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
310 UserDefFlatUnion
*tmp
;
312 v
= visitor_input_test_init(data
,
313 "{ 'enum1': 'value1', "
315 "'boolean': true }");
316 /* TODO when generator bug is fixed, add 'integer': 41 */
318 visit_type_UserDefFlatUnion(v
, &tmp
, NULL
, &err
);
319 g_assert(err
== NULL
);
320 g_assert_cmpint(tmp
->kind
, ==, ENUM_ONE_VALUE1
);
321 g_assert_cmpstr(tmp
->string
, ==, "str");
322 /* TODO g_assert_cmpint(tmp->integer, ==, 41); */
323 g_assert_cmpint(tmp
->value1
->boolean
, ==, true);
324 qapi_free_UserDefFlatUnion(tmp
);
327 static void test_visitor_in_union_anon(TestInputVisitorData
*data
,
332 UserDefAnonUnion
*tmp
;
334 v
= visitor_input_test_init(data
, "42");
336 visit_type_UserDefAnonUnion(v
, &tmp
, NULL
, &err
);
337 g_assert(err
== NULL
);
338 g_assert_cmpint(tmp
->kind
, ==, USER_DEF_ANON_UNION_KIND_I
);
339 g_assert_cmpint(tmp
->i
, ==, 42);
340 qapi_free_UserDefAnonUnion(tmp
);
343 static void test_native_list_integer_helper(TestInputVisitorData
*data
,
345 UserDefNativeListUnionKind kind
)
347 UserDefNativeListUnion
*cvalue
= NULL
;
350 GString
*gstr_list
= g_string_new("");
351 GString
*gstr_union
= g_string_new("");
354 for (i
= 0; i
< 32; i
++) {
355 g_string_append_printf(gstr_list
, "%d", i
);
357 g_string_append(gstr_list
, ", ");
360 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
361 UserDefNativeListUnionKind_lookup
[kind
],
363 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
365 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
366 g_assert(err
== NULL
);
367 g_assert(cvalue
!= NULL
);
368 g_assert_cmpint(cvalue
->kind
, ==, kind
);
371 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
: {
372 intList
*elem
= NULL
;
373 for (i
= 0, elem
= cvalue
->integer
; elem
; elem
= elem
->next
, i
++) {
374 g_assert_cmpint(elem
->value
, ==, i
);
378 case USER_DEF_NATIVE_LIST_UNION_KIND_S8
: {
379 int8List
*elem
= NULL
;
380 for (i
= 0, elem
= cvalue
->s8
; elem
; elem
= elem
->next
, i
++) {
381 g_assert_cmpint(elem
->value
, ==, i
);
385 case USER_DEF_NATIVE_LIST_UNION_KIND_S16
: {
386 int16List
*elem
= NULL
;
387 for (i
= 0, elem
= cvalue
->s16
; elem
; elem
= elem
->next
, i
++) {
388 g_assert_cmpint(elem
->value
, ==, i
);
392 case USER_DEF_NATIVE_LIST_UNION_KIND_S32
: {
393 int32List
*elem
= NULL
;
394 for (i
= 0, elem
= cvalue
->s32
; elem
; elem
= elem
->next
, i
++) {
395 g_assert_cmpint(elem
->value
, ==, i
);
399 case USER_DEF_NATIVE_LIST_UNION_KIND_S64
: {
400 int64List
*elem
= NULL
;
401 for (i
= 0, elem
= cvalue
->s64
; elem
; elem
= elem
->next
, i
++) {
402 g_assert_cmpint(elem
->value
, ==, i
);
406 case USER_DEF_NATIVE_LIST_UNION_KIND_U8
: {
407 uint8List
*elem
= NULL
;
408 for (i
= 0, elem
= cvalue
->u8
; elem
; elem
= elem
->next
, i
++) {
409 g_assert_cmpint(elem
->value
, ==, i
);
413 case USER_DEF_NATIVE_LIST_UNION_KIND_U16
: {
414 uint16List
*elem
= NULL
;
415 for (i
= 0, elem
= cvalue
->u16
; elem
; elem
= elem
->next
, i
++) {
416 g_assert_cmpint(elem
->value
, ==, i
);
420 case USER_DEF_NATIVE_LIST_UNION_KIND_U32
: {
421 uint32List
*elem
= NULL
;
422 for (i
= 0, elem
= cvalue
->u32
; elem
; elem
= elem
->next
, i
++) {
423 g_assert_cmpint(elem
->value
, ==, i
);
427 case USER_DEF_NATIVE_LIST_UNION_KIND_U64
: {
428 uint64List
*elem
= NULL
;
429 for (i
= 0, elem
= cvalue
->u64
; elem
; elem
= elem
->next
, i
++) {
430 g_assert_cmpint(elem
->value
, ==, i
);
435 g_assert_not_reached();
438 g_string_free(gstr_union
, true);
439 g_string_free(gstr_list
, true);
440 qapi_free_UserDefNativeListUnion(cvalue
);
443 static void test_visitor_in_native_list_int(TestInputVisitorData
*data
,
446 test_native_list_integer_helper(data
, unused
,
447 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
);
450 static void test_visitor_in_native_list_int8(TestInputVisitorData
*data
,
453 test_native_list_integer_helper(data
, unused
,
454 USER_DEF_NATIVE_LIST_UNION_KIND_S8
);
457 static void test_visitor_in_native_list_int16(TestInputVisitorData
*data
,
460 test_native_list_integer_helper(data
, unused
,
461 USER_DEF_NATIVE_LIST_UNION_KIND_S16
);
464 static void test_visitor_in_native_list_int32(TestInputVisitorData
*data
,
467 test_native_list_integer_helper(data
, unused
,
468 USER_DEF_NATIVE_LIST_UNION_KIND_S32
);
471 static void test_visitor_in_native_list_int64(TestInputVisitorData
*data
,
474 test_native_list_integer_helper(data
, unused
,
475 USER_DEF_NATIVE_LIST_UNION_KIND_S64
);
478 static void test_visitor_in_native_list_uint8(TestInputVisitorData
*data
,
481 test_native_list_integer_helper(data
, unused
,
482 USER_DEF_NATIVE_LIST_UNION_KIND_U8
);
485 static void test_visitor_in_native_list_uint16(TestInputVisitorData
*data
,
488 test_native_list_integer_helper(data
, unused
,
489 USER_DEF_NATIVE_LIST_UNION_KIND_U16
);
492 static void test_visitor_in_native_list_uint32(TestInputVisitorData
*data
,
495 test_native_list_integer_helper(data
, unused
,
496 USER_DEF_NATIVE_LIST_UNION_KIND_U32
);
499 static void test_visitor_in_native_list_uint64(TestInputVisitorData
*data
,
502 test_native_list_integer_helper(data
, unused
,
503 USER_DEF_NATIVE_LIST_UNION_KIND_U64
);
506 static void test_visitor_in_native_list_bool(TestInputVisitorData
*data
,
509 UserDefNativeListUnion
*cvalue
= NULL
;
510 boolList
*elem
= NULL
;
513 GString
*gstr_list
= g_string_new("");
514 GString
*gstr_union
= g_string_new("");
517 for (i
= 0; i
< 32; i
++) {
518 g_string_append_printf(gstr_list
, "%s",
519 (i
% 3 == 0) ? "true" : "false");
521 g_string_append(gstr_list
, ", ");
524 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
526 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
528 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
529 g_assert(err
== NULL
);
530 g_assert(cvalue
!= NULL
);
531 g_assert_cmpint(cvalue
->kind
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN
);
533 for (i
= 0, elem
= cvalue
->boolean
; elem
; elem
= elem
->next
, i
++) {
534 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
537 g_string_free(gstr_union
, true);
538 g_string_free(gstr_list
, true);
539 qapi_free_UserDefNativeListUnion(cvalue
);
542 static void test_visitor_in_native_list_string(TestInputVisitorData
*data
,
545 UserDefNativeListUnion
*cvalue
= NULL
;
546 strList
*elem
= NULL
;
549 GString
*gstr_list
= g_string_new("");
550 GString
*gstr_union
= g_string_new("");
553 for (i
= 0; i
< 32; i
++) {
554 g_string_append_printf(gstr_list
, "'%d'", i
);
556 g_string_append(gstr_list
, ", ");
559 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
561 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
563 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
564 g_assert(err
== NULL
);
565 g_assert(cvalue
!= NULL
);
566 g_assert_cmpint(cvalue
->kind
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING
);
568 for (i
= 0, elem
= cvalue
->string
; elem
; elem
= elem
->next
, i
++) {
570 sprintf(str
, "%d", i
);
571 g_assert_cmpstr(elem
->value
, ==, str
);
574 g_string_free(gstr_union
, true);
575 g_string_free(gstr_list
, true);
576 qapi_free_UserDefNativeListUnion(cvalue
);
579 #define DOUBLE_STR_MAX 16
581 static void test_visitor_in_native_list_number(TestInputVisitorData
*data
,
584 UserDefNativeListUnion
*cvalue
= NULL
;
585 numberList
*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
, "%f", (double)i
/ 3);
595 g_string_append(gstr_list
, ", ");
598 g_string_append_printf(gstr_union
, "{ 'type': 'number', '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_NUMBER
);
607 for (i
= 0, elem
= cvalue
->number
; elem
; elem
= elem
->next
, i
++) {
608 GString
*double_expected
= g_string_new("");
609 GString
*double_actual
= g_string_new("");
611 g_string_printf(double_expected
, "%.6f", (double)i
/ 3);
612 g_string_printf(double_actual
, "%.6f", elem
->value
);
613 g_assert_cmpstr(double_expected
->str
, ==, double_actual
->str
);
615 g_string_free(double_expected
, true);
616 g_string_free(double_actual
, true);
619 g_string_free(gstr_union
, true);
620 g_string_free(gstr_list
, true);
621 qapi_free_UserDefNativeListUnion(cvalue
);
624 static void input_visitor_test_add(const char *testpath
,
625 TestInputVisitorData
*data
,
626 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
628 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
629 visitor_input_teardown
);
632 static void test_visitor_in_errors(TestInputVisitorData
*data
,
635 TestStruct
*p
= NULL
;
639 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }");
641 visit_type_TestStruct(v
, &p
, NULL
, &err
);
643 g_assert(p
->string
== NULL
);
650 int main(int argc
, char **argv
)
652 TestInputVisitorData in_visitor_data
;
654 g_test_init(&argc
, &argv
, NULL
);
656 input_visitor_test_add("/visitor/input/int",
657 &in_visitor_data
, test_visitor_in_int
);
658 input_visitor_test_add("/visitor/input/int_overflow",
659 &in_visitor_data
, test_visitor_in_int_overflow
);
660 input_visitor_test_add("/visitor/input/bool",
661 &in_visitor_data
, test_visitor_in_bool
);
662 input_visitor_test_add("/visitor/input/number",
663 &in_visitor_data
, test_visitor_in_number
);
664 input_visitor_test_add("/visitor/input/string",
665 &in_visitor_data
, test_visitor_in_string
);
666 input_visitor_test_add("/visitor/input/enum",
667 &in_visitor_data
, test_visitor_in_enum
);
668 input_visitor_test_add("/visitor/input/struct",
669 &in_visitor_data
, test_visitor_in_struct
);
670 input_visitor_test_add("/visitor/input/struct-nested",
671 &in_visitor_data
, test_visitor_in_struct_nested
);
672 input_visitor_test_add("/visitor/input/list",
673 &in_visitor_data
, test_visitor_in_list
);
674 input_visitor_test_add("/visitor/input/union",
675 &in_visitor_data
, test_visitor_in_union
);
676 input_visitor_test_add("/visitor/input/union-flat",
677 &in_visitor_data
, test_visitor_in_union_flat
);
678 input_visitor_test_add("/visitor/input/union-anon",
679 &in_visitor_data
, test_visitor_in_union_anon
);
680 input_visitor_test_add("/visitor/input/errors",
681 &in_visitor_data
, test_visitor_in_errors
);
682 input_visitor_test_add("/visitor/input/native_list/int",
684 test_visitor_in_native_list_int
);
685 input_visitor_test_add("/visitor/input/native_list/int8",
687 test_visitor_in_native_list_int8
);
688 input_visitor_test_add("/visitor/input/native_list/int16",
690 test_visitor_in_native_list_int16
);
691 input_visitor_test_add("/visitor/input/native_list/int32",
693 test_visitor_in_native_list_int32
);
694 input_visitor_test_add("/visitor/input/native_list/int64",
696 test_visitor_in_native_list_int64
);
697 input_visitor_test_add("/visitor/input/native_list/uint8",
699 test_visitor_in_native_list_uint8
);
700 input_visitor_test_add("/visitor/input/native_list/uint16",
702 test_visitor_in_native_list_uint16
);
703 input_visitor_test_add("/visitor/input/native_list/uint32",
705 test_visitor_in_native_list_uint32
);
706 input_visitor_test_add("/visitor/input/native_list/uint64",
707 &in_visitor_data
, test_visitor_in_native_list_uint64
);
708 input_visitor_test_add("/visitor/input/native_list/bool",
709 &in_visitor_data
, test_visitor_in_native_list_bool
);
710 input_visitor_test_add("/visitor/input/native_list/str",
711 &in_visitor_data
, test_visitor_in_native_list_string
);
712 input_visitor_test_add("/visitor/input/native_list/number",
713 &in_visitor_data
, test_visitor_in_native_list_number
);