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
),
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 UserDefNested
*udp
= NULL
;
255 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
257 visit_type_UserDefNested(v
, &udp
, NULL
, &err
);
260 check_and_free_str(udp
->string0
, "string0");
261 check_and_free_str(udp
->dict1
.string1
, "string1");
262 g_assert_cmpint(udp
->dict1
.dict2
.userdef1
->base
->integer
, ==, 42);
263 check_and_free_str(udp
->dict1
.dict2
.userdef1
->string
, "string");
264 check_and_free_str(udp
->dict1
.dict2
.string2
, "string2");
265 g_assert(udp
->dict1
.has_dict3
== false);
267 g_free(udp
->dict1
.dict2
.userdef1
);
271 static void test_visitor_in_list(TestInputVisitorData
*data
,
274 UserDefOneList
*item
, *head
= NULL
;
279 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
281 visit_type_UserDefOneList(v
, &head
, NULL
, &err
);
283 g_assert(head
!= NULL
);
285 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
288 snprintf(string
, sizeof(string
), "string%d", i
);
289 g_assert_cmpstr(item
->value
->string
, ==, string
);
290 g_assert_cmpint(item
->value
->base
->integer
, ==, 42 + i
);
293 qapi_free_UserDefOneList(head
);
296 static void test_visitor_in_union(TestInputVisitorData
*data
,
303 v
= visitor_input_test_init(data
, "{ 'type': 'b', 'integer': 41, 'data' : { 'integer': 42 } }");
305 visit_type_UserDefUnion(v
, &tmp
, NULL
, &err
);
306 g_assert(err
== NULL
);
307 g_assert_cmpint(tmp
->kind
, ==, USER_DEF_UNION_KIND_B
);
308 g_assert_cmpint(tmp
->integer
, ==, 41);
309 g_assert_cmpint(tmp
->b
->integer
, ==, 42);
310 qapi_free_UserDefUnion(tmp
);
313 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
318 UserDefFlatUnion
*tmp
;
320 v
= visitor_input_test_init(data
,
321 "{ 'enum1': 'value1', "
323 "'boolean': true }");
324 /* TODO when generator bug is fixed, add 'integer': 41 */
326 visit_type_UserDefFlatUnion(v
, &tmp
, NULL
, &err
);
327 g_assert(err
== NULL
);
328 g_assert_cmpint(tmp
->kind
, ==, ENUM_ONE_VALUE1
);
329 g_assert_cmpstr(tmp
->string
, ==, "str");
330 /* TODO g_assert_cmpint(tmp->integer, ==, 41); */
331 g_assert_cmpint(tmp
->value1
->boolean
, ==, true);
332 qapi_free_UserDefFlatUnion(tmp
);
335 static void test_visitor_in_union_anon(TestInputVisitorData
*data
,
340 UserDefAnonUnion
*tmp
;
342 v
= visitor_input_test_init(data
, "42");
344 visit_type_UserDefAnonUnion(v
, &tmp
, NULL
, &err
);
345 g_assert(err
== NULL
);
346 g_assert_cmpint(tmp
->kind
, ==, USER_DEF_ANON_UNION_KIND_I
);
347 g_assert_cmpint(tmp
->i
, ==, 42);
348 qapi_free_UserDefAnonUnion(tmp
);
351 static void test_native_list_integer_helper(TestInputVisitorData
*data
,
353 UserDefNativeListUnionKind kind
)
355 UserDefNativeListUnion
*cvalue
= NULL
;
358 GString
*gstr_list
= g_string_new("");
359 GString
*gstr_union
= g_string_new("");
362 for (i
= 0; i
< 32; i
++) {
363 g_string_append_printf(gstr_list
, "%d", i
);
365 g_string_append(gstr_list
, ", ");
368 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
369 UserDefNativeListUnionKind_lookup
[kind
],
371 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
373 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
374 g_assert(err
== NULL
);
375 g_assert(cvalue
!= NULL
);
376 g_assert_cmpint(cvalue
->kind
, ==, kind
);
379 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
: {
380 intList
*elem
= NULL
;
381 for (i
= 0, elem
= cvalue
->integer
; elem
; elem
= elem
->next
, i
++) {
382 g_assert_cmpint(elem
->value
, ==, i
);
386 case USER_DEF_NATIVE_LIST_UNION_KIND_S8
: {
387 int8List
*elem
= NULL
;
388 for (i
= 0, elem
= cvalue
->s8
; elem
; elem
= elem
->next
, i
++) {
389 g_assert_cmpint(elem
->value
, ==, i
);
393 case USER_DEF_NATIVE_LIST_UNION_KIND_S16
: {
394 int16List
*elem
= NULL
;
395 for (i
= 0, elem
= cvalue
->s16
; elem
; elem
= elem
->next
, i
++) {
396 g_assert_cmpint(elem
->value
, ==, i
);
400 case USER_DEF_NATIVE_LIST_UNION_KIND_S32
: {
401 int32List
*elem
= NULL
;
402 for (i
= 0, elem
= cvalue
->s32
; elem
; elem
= elem
->next
, i
++) {
403 g_assert_cmpint(elem
->value
, ==, i
);
407 case USER_DEF_NATIVE_LIST_UNION_KIND_S64
: {
408 int64List
*elem
= NULL
;
409 for (i
= 0, elem
= cvalue
->s64
; elem
; elem
= elem
->next
, i
++) {
410 g_assert_cmpint(elem
->value
, ==, i
);
414 case USER_DEF_NATIVE_LIST_UNION_KIND_U8
: {
415 uint8List
*elem
= NULL
;
416 for (i
= 0, elem
= cvalue
->u8
; elem
; elem
= elem
->next
, i
++) {
417 g_assert_cmpint(elem
->value
, ==, i
);
421 case USER_DEF_NATIVE_LIST_UNION_KIND_U16
: {
422 uint16List
*elem
= NULL
;
423 for (i
= 0, elem
= cvalue
->u16
; elem
; elem
= elem
->next
, i
++) {
424 g_assert_cmpint(elem
->value
, ==, i
);
428 case USER_DEF_NATIVE_LIST_UNION_KIND_U32
: {
429 uint32List
*elem
= NULL
;
430 for (i
= 0, elem
= cvalue
->u32
; elem
; elem
= elem
->next
, i
++) {
431 g_assert_cmpint(elem
->value
, ==, i
);
435 case USER_DEF_NATIVE_LIST_UNION_KIND_U64
: {
436 uint64List
*elem
= NULL
;
437 for (i
= 0, elem
= cvalue
->u64
; elem
; elem
= elem
->next
, i
++) {
438 g_assert_cmpint(elem
->value
, ==, i
);
443 g_assert_not_reached();
446 g_string_free(gstr_union
, true);
447 g_string_free(gstr_list
, true);
448 qapi_free_UserDefNativeListUnion(cvalue
);
451 static void test_visitor_in_native_list_int(TestInputVisitorData
*data
,
454 test_native_list_integer_helper(data
, unused
,
455 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
);
458 static void test_visitor_in_native_list_int8(TestInputVisitorData
*data
,
461 test_native_list_integer_helper(data
, unused
,
462 USER_DEF_NATIVE_LIST_UNION_KIND_S8
);
465 static void test_visitor_in_native_list_int16(TestInputVisitorData
*data
,
468 test_native_list_integer_helper(data
, unused
,
469 USER_DEF_NATIVE_LIST_UNION_KIND_S16
);
472 static void test_visitor_in_native_list_int32(TestInputVisitorData
*data
,
475 test_native_list_integer_helper(data
, unused
,
476 USER_DEF_NATIVE_LIST_UNION_KIND_S32
);
479 static void test_visitor_in_native_list_int64(TestInputVisitorData
*data
,
482 test_native_list_integer_helper(data
, unused
,
483 USER_DEF_NATIVE_LIST_UNION_KIND_S64
);
486 static void test_visitor_in_native_list_uint8(TestInputVisitorData
*data
,
489 test_native_list_integer_helper(data
, unused
,
490 USER_DEF_NATIVE_LIST_UNION_KIND_U8
);
493 static void test_visitor_in_native_list_uint16(TestInputVisitorData
*data
,
496 test_native_list_integer_helper(data
, unused
,
497 USER_DEF_NATIVE_LIST_UNION_KIND_U16
);
500 static void test_visitor_in_native_list_uint32(TestInputVisitorData
*data
,
503 test_native_list_integer_helper(data
, unused
,
504 USER_DEF_NATIVE_LIST_UNION_KIND_U32
);
507 static void test_visitor_in_native_list_uint64(TestInputVisitorData
*data
,
510 test_native_list_integer_helper(data
, unused
,
511 USER_DEF_NATIVE_LIST_UNION_KIND_U64
);
514 static void test_visitor_in_native_list_bool(TestInputVisitorData
*data
,
517 UserDefNativeListUnion
*cvalue
= NULL
;
518 boolList
*elem
= NULL
;
521 GString
*gstr_list
= g_string_new("");
522 GString
*gstr_union
= g_string_new("");
525 for (i
= 0; i
< 32; i
++) {
526 g_string_append_printf(gstr_list
, "%s",
527 (i
% 3 == 0) ? "true" : "false");
529 g_string_append(gstr_list
, ", ");
532 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
534 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
536 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
537 g_assert(err
== NULL
);
538 g_assert(cvalue
!= NULL
);
539 g_assert_cmpint(cvalue
->kind
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN
);
541 for (i
= 0, elem
= cvalue
->boolean
; elem
; elem
= elem
->next
, i
++) {
542 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
545 g_string_free(gstr_union
, true);
546 g_string_free(gstr_list
, true);
547 qapi_free_UserDefNativeListUnion(cvalue
);
550 static void test_visitor_in_native_list_string(TestInputVisitorData
*data
,
553 UserDefNativeListUnion
*cvalue
= NULL
;
554 strList
*elem
= NULL
;
557 GString
*gstr_list
= g_string_new("");
558 GString
*gstr_union
= g_string_new("");
561 for (i
= 0; i
< 32; i
++) {
562 g_string_append_printf(gstr_list
, "'%d'", i
);
564 g_string_append(gstr_list
, ", ");
567 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
569 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
571 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
572 g_assert(err
== NULL
);
573 g_assert(cvalue
!= NULL
);
574 g_assert_cmpint(cvalue
->kind
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING
);
576 for (i
= 0, elem
= cvalue
->string
; elem
; elem
= elem
->next
, i
++) {
578 sprintf(str
, "%d", i
);
579 g_assert_cmpstr(elem
->value
, ==, str
);
582 g_string_free(gstr_union
, true);
583 g_string_free(gstr_list
, true);
584 qapi_free_UserDefNativeListUnion(cvalue
);
587 #define DOUBLE_STR_MAX 16
589 static void test_visitor_in_native_list_number(TestInputVisitorData
*data
,
592 UserDefNativeListUnion
*cvalue
= NULL
;
593 numberList
*elem
= NULL
;
596 GString
*gstr_list
= g_string_new("");
597 GString
*gstr_union
= g_string_new("");
600 for (i
= 0; i
< 32; i
++) {
601 g_string_append_printf(gstr_list
, "%f", (double)i
/ 3);
603 g_string_append(gstr_list
, ", ");
606 g_string_append_printf(gstr_union
, "{ 'type': 'number', 'data': [ %s ] }",
608 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
610 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
611 g_assert(err
== NULL
);
612 g_assert(cvalue
!= NULL
);
613 g_assert_cmpint(cvalue
->kind
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER
);
615 for (i
= 0, elem
= cvalue
->number
; elem
; elem
= elem
->next
, i
++) {
616 GString
*double_expected
= g_string_new("");
617 GString
*double_actual
= g_string_new("");
619 g_string_printf(double_expected
, "%.6f", (double)i
/ 3);
620 g_string_printf(double_actual
, "%.6f", elem
->value
);
621 g_assert_cmpstr(double_expected
->str
, ==, double_actual
->str
);
623 g_string_free(double_expected
, true);
624 g_string_free(double_actual
, true);
627 g_string_free(gstr_union
, true);
628 g_string_free(gstr_list
, true);
629 qapi_free_UserDefNativeListUnion(cvalue
);
632 static void input_visitor_test_add(const char *testpath
,
633 TestInputVisitorData
*data
,
634 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
636 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
637 visitor_input_teardown
);
640 static void test_visitor_in_errors(TestInputVisitorData
*data
,
643 TestStruct
*p
= NULL
;
647 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }");
649 visit_type_TestStruct(v
, &p
, NULL
, &err
);
651 g_assert(p
->string
== NULL
);
658 int main(int argc
, char **argv
)
660 TestInputVisitorData in_visitor_data
;
662 g_test_init(&argc
, &argv
, NULL
);
664 input_visitor_test_add("/visitor/input/int",
665 &in_visitor_data
, test_visitor_in_int
);
666 input_visitor_test_add("/visitor/input/int_overflow",
667 &in_visitor_data
, test_visitor_in_int_overflow
);
668 input_visitor_test_add("/visitor/input/bool",
669 &in_visitor_data
, test_visitor_in_bool
);
670 input_visitor_test_add("/visitor/input/number",
671 &in_visitor_data
, test_visitor_in_number
);
672 input_visitor_test_add("/visitor/input/string",
673 &in_visitor_data
, test_visitor_in_string
);
674 input_visitor_test_add("/visitor/input/enum",
675 &in_visitor_data
, test_visitor_in_enum
);
676 input_visitor_test_add("/visitor/input/struct",
677 &in_visitor_data
, test_visitor_in_struct
);
678 input_visitor_test_add("/visitor/input/struct-nested",
679 &in_visitor_data
, test_visitor_in_struct_nested
);
680 input_visitor_test_add("/visitor/input/list",
681 &in_visitor_data
, test_visitor_in_list
);
682 input_visitor_test_add("/visitor/input/union",
683 &in_visitor_data
, test_visitor_in_union
);
684 input_visitor_test_add("/visitor/input/union-flat",
685 &in_visitor_data
, test_visitor_in_union_flat
);
686 input_visitor_test_add("/visitor/input/union-anon",
687 &in_visitor_data
, test_visitor_in_union_anon
);
688 input_visitor_test_add("/visitor/input/errors",
689 &in_visitor_data
, test_visitor_in_errors
);
690 input_visitor_test_add("/visitor/input/native_list/int",
692 test_visitor_in_native_list_int
);
693 input_visitor_test_add("/visitor/input/native_list/int8",
695 test_visitor_in_native_list_int8
);
696 input_visitor_test_add("/visitor/input/native_list/int16",
698 test_visitor_in_native_list_int16
);
699 input_visitor_test_add("/visitor/input/native_list/int32",
701 test_visitor_in_native_list_int32
);
702 input_visitor_test_add("/visitor/input/native_list/int64",
704 test_visitor_in_native_list_int64
);
705 input_visitor_test_add("/visitor/input/native_list/uint8",
707 test_visitor_in_native_list_uint8
);
708 input_visitor_test_add("/visitor/input/native_list/uint16",
710 test_visitor_in_native_list_uint16
);
711 input_visitor_test_add("/visitor/input/native_list/uint32",
713 test_visitor_in_native_list_uint32
);
714 input_visitor_test_add("/visitor/input/native_list/uint64",
715 &in_visitor_data
, test_visitor_in_native_list_uint64
);
716 input_visitor_test_add("/visitor/input/native_list/bool",
717 &in_visitor_data
, test_visitor_in_native_list_bool
);
718 input_visitor_test_add("/visitor/input/native_list/str",
719 &in_visitor_data
, test_visitor_in_native_list_string
);
720 input_visitor_test_add("/visitor/input/native_list/number",
721 &in_visitor_data
, test_visitor_in_native_list_number
);