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 GCC_FMT_ATTR(2, 0)
72 Visitor
*visitor_input_test_init_raw(TestInputVisitorData
*data
,
73 const char *json_string
)
77 data
->obj
= qobject_from_json(json_string
);
79 g_assert(data
->obj
!= NULL
);
81 data
->qiv
= qmp_input_visitor_new(data
->obj
);
82 g_assert(data
->qiv
!= NULL
);
84 v
= qmp_input_get_visitor(data
->qiv
);
90 static void test_visitor_in_int(TestInputVisitorData
*data
,
93 int64_t res
= 0, value
= -42;
97 v
= visitor_input_test_init(data
, "%" PRId64
, value
);
99 visit_type_int(v
, &res
, NULL
, &errp
);
100 g_assert(!error_is_set(&errp
));
101 g_assert_cmpint(res
, ==, value
);
104 static void test_visitor_in_int_overflow(TestInputVisitorData
*data
,
111 /* this will overflow a Qint/int64, so should be deserialized into
112 * a QFloat/double field instead, leading to an error if we pass it
113 * to visit_type_int. confirm this.
115 v
= visitor_input_test_init(data
, "%f", DBL_MAX
);
117 visit_type_int(v
, &res
, NULL
, &errp
);
118 g_assert(error_is_set(&errp
));
122 static void test_visitor_in_bool(TestInputVisitorData
*data
,
129 v
= visitor_input_test_init(data
, "true");
131 visit_type_bool(v
, &res
, NULL
, &errp
);
132 g_assert(!error_is_set(&errp
));
133 g_assert_cmpint(res
, ==, true);
136 static void test_visitor_in_number(TestInputVisitorData
*data
,
139 double res
= 0, value
= 3.14;
143 v
= visitor_input_test_init(data
, "%f", value
);
145 visit_type_number(v
, &res
, NULL
, &errp
);
146 g_assert(!error_is_set(&errp
));
147 g_assert_cmpfloat(res
, ==, value
);
150 static void test_visitor_in_string(TestInputVisitorData
*data
,
153 char *res
= NULL
, *value
= (char *) "Q E M U";
157 v
= visitor_input_test_init(data
, "%s", value
);
159 visit_type_str(v
, &res
, NULL
, &errp
);
160 g_assert(!error_is_set(&errp
));
161 g_assert_cmpstr(res
, ==, value
);
166 static void test_visitor_in_enum(TestInputVisitorData
*data
,
173 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
176 v
= visitor_input_test_init(data
, "%s", EnumOne_lookup
[i
]);
178 visit_type_EnumOne(v
, &res
, NULL
, &errp
);
179 g_assert(!error_is_set(&errp
));
180 g_assert_cmpint(i
, ==, res
);
182 visitor_input_teardown(data
, NULL
);
189 typedef struct TestStruct
196 static void visit_type_TestStruct(Visitor
*v
, TestStruct
**obj
,
197 const char *name
, Error
**errp
)
200 if (!error_is_set(errp
)) {
201 visit_start_struct(v
, (void **)obj
, "TestStruct", name
, sizeof(TestStruct
),
204 visit_type_int(v
, &(*obj
)->integer
, "integer", &err
);
205 visit_type_bool(v
, &(*obj
)->boolean
, "boolean", &err
);
206 visit_type_str(v
, &(*obj
)->string
, "string", &err
);
208 /* Always call end_struct if start_struct succeeded. */
209 error_propagate(errp
, err
);
211 visit_end_struct(v
, &err
);
213 error_propagate(errp
, err
);
217 static void test_visitor_in_struct(TestInputVisitorData
*data
,
220 TestStruct
*p
= NULL
;
224 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
226 visit_type_TestStruct(v
, &p
, NULL
, &errp
);
227 g_assert(!error_is_set(&errp
));
228 g_assert_cmpint(p
->integer
, ==, -42);
229 g_assert(p
->boolean
== true);
230 g_assert_cmpstr(p
->string
, ==, "foo");
236 static void check_and_free_str(char *str
, const char *cmp
)
238 g_assert_cmpstr(str
, ==, cmp
);
242 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
245 UserDefNested
*udp
= NULL
;
249 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
251 visit_type_UserDefNested(v
, &udp
, NULL
, &errp
);
252 g_assert(!error_is_set(&errp
));
254 check_and_free_str(udp
->string0
, "string0");
255 check_and_free_str(udp
->dict1
.string1
, "string1");
256 g_assert_cmpint(udp
->dict1
.dict2
.userdef1
->integer
, ==, 42);
257 check_and_free_str(udp
->dict1
.dict2
.userdef1
->string
, "string");
258 check_and_free_str(udp
->dict1
.dict2
.string2
, "string2");
259 g_assert(udp
->dict1
.has_dict3
== false);
261 g_free(udp
->dict1
.dict2
.userdef1
);
265 static void test_visitor_in_list(TestInputVisitorData
*data
,
268 UserDefOneList
*item
, *head
= NULL
;
273 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
275 visit_type_UserDefOneList(v
, &head
, NULL
, &errp
);
276 g_assert(!error_is_set(&errp
));
277 g_assert(head
!= NULL
);
279 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
282 snprintf(string
, sizeof(string
), "string%d", i
);
283 g_assert_cmpstr(item
->value
->string
, ==, string
);
284 g_assert_cmpint(item
->value
->integer
, ==, 42 + i
);
287 qapi_free_UserDefOneList(head
);
290 static void test_visitor_in_union(TestInputVisitorData
*data
,
297 v
= visitor_input_test_init(data
, "{ 'type': 'b', 'data' : { 'integer': 42 } }");
299 visit_type_UserDefUnion(v
, &tmp
, NULL
, &err
);
300 g_assert(err
== NULL
);
301 g_assert_cmpint(tmp
->kind
, ==, USER_DEF_UNION_KIND_B
);
302 g_assert_cmpint(tmp
->b
->integer
, ==, 42);
303 qapi_free_UserDefUnion(tmp
);
306 static void test_native_list_integer_helper(TestInputVisitorData
*data
,
308 UserDefNativeListUnionKind kind
)
310 UserDefNativeListUnion
*cvalue
= NULL
;
313 GString
*gstr_list
= g_string_new("");
314 GString
*gstr_union
= g_string_new("");
317 for (i
= 0; i
< 32; i
++) {
318 g_string_append_printf(gstr_list
, "%d", i
);
320 g_string_append(gstr_list
, ", ");
323 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
324 UserDefNativeListUnionKind_lookup
[kind
],
326 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
328 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
329 g_assert(err
== NULL
);
330 g_assert(cvalue
!= NULL
);
331 g_assert_cmpint(cvalue
->kind
, ==, kind
);
334 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
: {
335 intList
*elem
= NULL
;
336 for (i
= 0, elem
= cvalue
->integer
; elem
; elem
= elem
->next
, i
++) {
337 g_assert_cmpint(elem
->value
, ==, i
);
341 case USER_DEF_NATIVE_LIST_UNION_KIND_S8
: {
342 int8List
*elem
= NULL
;
343 for (i
= 0, elem
= cvalue
->s8
; elem
; elem
= elem
->next
, i
++) {
344 g_assert_cmpint(elem
->value
, ==, i
);
348 case USER_DEF_NATIVE_LIST_UNION_KIND_S16
: {
349 int16List
*elem
= NULL
;
350 for (i
= 0, elem
= cvalue
->s16
; elem
; elem
= elem
->next
, i
++) {
351 g_assert_cmpint(elem
->value
, ==, i
);
355 case USER_DEF_NATIVE_LIST_UNION_KIND_S32
: {
356 int32List
*elem
= NULL
;
357 for (i
= 0, elem
= cvalue
->s32
; elem
; elem
= elem
->next
, i
++) {
358 g_assert_cmpint(elem
->value
, ==, i
);
362 case USER_DEF_NATIVE_LIST_UNION_KIND_S64
: {
363 int64List
*elem
= NULL
;
364 for (i
= 0, elem
= cvalue
->s64
; elem
; elem
= elem
->next
, i
++) {
365 g_assert_cmpint(elem
->value
, ==, i
);
369 case USER_DEF_NATIVE_LIST_UNION_KIND_U8
: {
370 uint8List
*elem
= NULL
;
371 for (i
= 0, elem
= cvalue
->u8
; elem
; elem
= elem
->next
, i
++) {
372 g_assert_cmpint(elem
->value
, ==, i
);
376 case USER_DEF_NATIVE_LIST_UNION_KIND_U16
: {
377 uint16List
*elem
= NULL
;
378 for (i
= 0, elem
= cvalue
->u16
; elem
; elem
= elem
->next
, i
++) {
379 g_assert_cmpint(elem
->value
, ==, i
);
383 case USER_DEF_NATIVE_LIST_UNION_KIND_U32
: {
384 uint32List
*elem
= NULL
;
385 for (i
= 0, elem
= cvalue
->u32
; elem
; elem
= elem
->next
, i
++) {
386 g_assert_cmpint(elem
->value
, ==, i
);
390 case USER_DEF_NATIVE_LIST_UNION_KIND_U64
: {
391 uint64List
*elem
= NULL
;
392 for (i
= 0, elem
= cvalue
->u64
; elem
; elem
= elem
->next
, i
++) {
393 g_assert_cmpint(elem
->value
, ==, i
);
398 g_assert_not_reached();
401 g_string_free(gstr_union
, true);
402 g_string_free(gstr_list
, true);
403 qapi_free_UserDefNativeListUnion(cvalue
);
406 static void test_visitor_in_native_list_int(TestInputVisitorData
*data
,
409 test_native_list_integer_helper(data
, unused
,
410 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
);
413 static void test_visitor_in_native_list_int8(TestInputVisitorData
*data
,
416 test_native_list_integer_helper(data
, unused
,
417 USER_DEF_NATIVE_LIST_UNION_KIND_S8
);
420 static void test_visitor_in_native_list_int16(TestInputVisitorData
*data
,
423 test_native_list_integer_helper(data
, unused
,
424 USER_DEF_NATIVE_LIST_UNION_KIND_S16
);
427 static void test_visitor_in_native_list_int32(TestInputVisitorData
*data
,
430 test_native_list_integer_helper(data
, unused
,
431 USER_DEF_NATIVE_LIST_UNION_KIND_S32
);
434 static void test_visitor_in_native_list_int64(TestInputVisitorData
*data
,
437 test_native_list_integer_helper(data
, unused
,
438 USER_DEF_NATIVE_LIST_UNION_KIND_S64
);
441 static void test_visitor_in_native_list_uint8(TestInputVisitorData
*data
,
444 test_native_list_integer_helper(data
, unused
,
445 USER_DEF_NATIVE_LIST_UNION_KIND_U8
);
448 static void test_visitor_in_native_list_uint16(TestInputVisitorData
*data
,
451 test_native_list_integer_helper(data
, unused
,
452 USER_DEF_NATIVE_LIST_UNION_KIND_U16
);
455 static void test_visitor_in_native_list_uint32(TestInputVisitorData
*data
,
458 test_native_list_integer_helper(data
, unused
,
459 USER_DEF_NATIVE_LIST_UNION_KIND_U32
);
462 static void test_visitor_in_native_list_uint64(TestInputVisitorData
*data
,
465 test_native_list_integer_helper(data
, unused
,
466 USER_DEF_NATIVE_LIST_UNION_KIND_U64
);
469 static void test_visitor_in_native_list_bool(TestInputVisitorData
*data
,
472 UserDefNativeListUnion
*cvalue
= NULL
;
473 boolList
*elem
= NULL
;
476 GString
*gstr_list
= g_string_new("");
477 GString
*gstr_union
= g_string_new("");
480 for (i
= 0; i
< 32; i
++) {
481 g_string_append_printf(gstr_list
, "%s",
482 (i
% 3 == 0) ? "true" : "false");
484 g_string_append(gstr_list
, ", ");
487 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
489 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
491 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
492 g_assert(err
== NULL
);
493 g_assert(cvalue
!= NULL
);
494 g_assert_cmpint(cvalue
->kind
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN
);
496 for (i
= 0, elem
= cvalue
->boolean
; elem
; elem
= elem
->next
, i
++) {
497 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
500 g_string_free(gstr_union
, true);
501 g_string_free(gstr_list
, true);
502 qapi_free_UserDefNativeListUnion(cvalue
);
505 static void test_visitor_in_native_list_string(TestInputVisitorData
*data
,
508 UserDefNativeListUnion
*cvalue
= NULL
;
509 strList
*elem
= NULL
;
512 GString
*gstr_list
= g_string_new("");
513 GString
*gstr_union
= g_string_new("");
516 for (i
= 0; i
< 32; i
++) {
517 g_string_append_printf(gstr_list
, "'%d'", i
);
519 g_string_append(gstr_list
, ", ");
522 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
524 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
526 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
527 g_assert(err
== NULL
);
528 g_assert(cvalue
!= NULL
);
529 g_assert_cmpint(cvalue
->kind
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING
);
531 for (i
= 0, elem
= cvalue
->string
; elem
; elem
= elem
->next
, i
++) {
533 sprintf(str
, "%d", i
);
534 g_assert_cmpstr(elem
->value
, ==, str
);
537 g_string_free(gstr_union
, true);
538 g_string_free(gstr_list
, true);
539 qapi_free_UserDefNativeListUnion(cvalue
);
542 #define DOUBLE_STR_MAX 16
544 static void test_visitor_in_native_list_number(TestInputVisitorData
*data
,
547 UserDefNativeListUnion
*cvalue
= NULL
;
548 numberList
*elem
= NULL
;
551 GString
*gstr_list
= g_string_new("");
552 GString
*gstr_union
= g_string_new("");
555 for (i
= 0; i
< 32; i
++) {
556 g_string_append_printf(gstr_list
, "%f", (double)i
/ 3);
558 g_string_append(gstr_list
, ", ");
561 g_string_append_printf(gstr_union
, "{ 'type': 'number', 'data': [ %s ] }",
563 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
565 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
566 g_assert(err
== NULL
);
567 g_assert(cvalue
!= NULL
);
568 g_assert_cmpint(cvalue
->kind
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER
);
570 for (i
= 0, elem
= cvalue
->number
; elem
; elem
= elem
->next
, i
++) {
571 GString
*double_expected
= g_string_new("");
572 GString
*double_actual
= g_string_new("");
574 g_string_printf(double_expected
, "%.6f", (double)i
/ 3);
575 g_string_printf(double_actual
, "%.6f", elem
->value
);
576 g_assert_cmpstr(double_expected
->str
, ==, double_actual
->str
);
578 g_string_free(double_expected
, true);
579 g_string_free(double_actual
, true);
582 g_string_free(gstr_union
, true);
583 g_string_free(gstr_list
, true);
584 qapi_free_UserDefNativeListUnion(cvalue
);
587 static void input_visitor_test_add(const char *testpath
,
588 TestInputVisitorData
*data
,
589 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
591 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
592 visitor_input_teardown
);
595 static void test_visitor_in_errors(TestInputVisitorData
*data
,
598 TestStruct
*p
= NULL
;
602 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }");
604 visit_type_TestStruct(v
, &p
, NULL
, &errp
);
605 g_assert(error_is_set(&errp
));
606 g_assert(p
->string
== NULL
);
613 int main(int argc
, char **argv
)
615 TestInputVisitorData in_visitor_data
;
617 g_test_init(&argc
, &argv
, NULL
);
619 input_visitor_test_add("/visitor/input/int",
620 &in_visitor_data
, test_visitor_in_int
);
621 input_visitor_test_add("/visitor/input/int_overflow",
622 &in_visitor_data
, test_visitor_in_int_overflow
);
623 input_visitor_test_add("/visitor/input/bool",
624 &in_visitor_data
, test_visitor_in_bool
);
625 input_visitor_test_add("/visitor/input/number",
626 &in_visitor_data
, test_visitor_in_number
);
627 input_visitor_test_add("/visitor/input/string",
628 &in_visitor_data
, test_visitor_in_string
);
629 input_visitor_test_add("/visitor/input/enum",
630 &in_visitor_data
, test_visitor_in_enum
);
631 input_visitor_test_add("/visitor/input/struct",
632 &in_visitor_data
, test_visitor_in_struct
);
633 input_visitor_test_add("/visitor/input/struct-nested",
634 &in_visitor_data
, test_visitor_in_struct_nested
);
635 input_visitor_test_add("/visitor/input/list",
636 &in_visitor_data
, test_visitor_in_list
);
637 input_visitor_test_add("/visitor/input/union",
638 &in_visitor_data
, test_visitor_in_union
);
639 input_visitor_test_add("/visitor/input/errors",
640 &in_visitor_data
, test_visitor_in_errors
);
641 input_visitor_test_add("/visitor/input/native_list/int",
643 test_visitor_in_native_list_int
);
644 input_visitor_test_add("/visitor/input/native_list/int8",
646 test_visitor_in_native_list_int8
);
647 input_visitor_test_add("/visitor/input/native_list/int16",
649 test_visitor_in_native_list_int16
);
650 input_visitor_test_add("/visitor/input/native_list/int32",
652 test_visitor_in_native_list_int32
);
653 input_visitor_test_add("/visitor/input/native_list/int64",
655 test_visitor_in_native_list_int64
);
656 input_visitor_test_add("/visitor/input/native_list/uint8",
658 test_visitor_in_native_list_uint8
);
659 input_visitor_test_add("/visitor/input/native_list/uint16",
661 test_visitor_in_native_list_uint16
);
662 input_visitor_test_add("/visitor/input/native_list/uint32",
664 test_visitor_in_native_list_uint32
);
665 input_visitor_test_add("/visitor/input/native_list/uint64",
666 &in_visitor_data
, test_visitor_in_native_list_uint64
);
667 input_visitor_test_add("/visitor/input/native_list/bool",
668 &in_visitor_data
, test_visitor_in_native_list_bool
);
669 input_visitor_test_add("/visitor/input/native_list/str",
670 &in_visitor_data
, test_visitor_in_native_list_string
);
671 input_visitor_test_add("/visitor/input/native_list/number",
672 &in_visitor_data
, test_visitor_in_native_list_number
);