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 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
, &err
);
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
, &err
);
122 static void test_visitor_in_bool(TestInputVisitorData
*data
,
129 v
= visitor_input_test_init(data
, "true");
131 visit_type_bool(v
, &res
, NULL
, &err
);
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
, &err
);
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
, &err
);
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
, &err
);
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
)
201 visit_start_struct(v
, (void **)obj
, "TestStruct", name
, sizeof(TestStruct
),
206 visit_type_int(v
, &(*obj
)->integer
, "integer", &err
);
210 visit_type_bool(v
, &(*obj
)->boolean
, "boolean", &err
);
214 visit_type_str(v
, &(*obj
)->string
, "string", &err
);
217 error_propagate(errp
, err
);
219 visit_end_struct(v
, &err
);
221 error_propagate(errp
, err
);
224 static void test_visitor_in_struct(TestInputVisitorData
*data
,
227 TestStruct
*p
= NULL
;
231 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
233 visit_type_TestStruct(v
, &p
, NULL
, &err
);
235 g_assert_cmpint(p
->integer
, ==, -42);
236 g_assert(p
->boolean
== true);
237 g_assert_cmpstr(p
->string
, ==, "foo");
243 static void check_and_free_str(char *str
, const char *cmp
)
245 g_assert_cmpstr(str
, ==, cmp
);
249 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
252 UserDefTwo
*udp
= NULL
;
256 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', "
257 "'dict1': { 'string1': 'string1', "
258 "'dict2': { 'userdef': { 'integer': 42, "
259 "'string': 'string' }, 'string': 'string2'}}}");
261 visit_type_UserDefTwo(v
, &udp
, NULL
, &err
);
264 check_and_free_str(udp
->string0
, "string0");
265 check_and_free_str(udp
->dict1
->string1
, "string1");
266 g_assert_cmpint(udp
->dict1
->dict2
->userdef
->base
->integer
, ==, 42);
267 check_and_free_str(udp
->dict1
->dict2
->userdef
->string
, "string");
268 check_and_free_str(udp
->dict1
->dict2
->string
, "string2");
269 g_assert(udp
->dict1
->has_dict3
== false);
271 g_free(udp
->dict1
->dict2
->userdef
);
272 g_free(udp
->dict1
->dict2
);
277 static void test_visitor_in_list(TestInputVisitorData
*data
,
280 UserDefOneList
*item
, *head
= NULL
;
285 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
287 visit_type_UserDefOneList(v
, &head
, NULL
, &err
);
289 g_assert(head
!= NULL
);
291 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
294 snprintf(string
, sizeof(string
), "string%d", i
);
295 g_assert_cmpstr(item
->value
->string
, ==, string
);
296 g_assert_cmpint(item
->value
->base
->integer
, ==, 42 + i
);
299 qapi_free_UserDefOneList(head
);
302 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
307 UserDefFlatUnion
*tmp
;
309 v
= visitor_input_test_init(data
,
310 "{ 'enum1': 'value1', "
312 "'boolean': true }");
313 /* TODO when generator bug is fixed, add 'integer': 41 */
315 visit_type_UserDefFlatUnion(v
, &tmp
, NULL
, &err
);
316 g_assert(err
== NULL
);
317 g_assert_cmpint(tmp
->enum1
, ==, ENUM_ONE_VALUE1
);
318 g_assert_cmpstr(tmp
->string
, ==, "str");
319 /* TODO g_assert_cmpint(tmp->integer, ==, 41); */
320 g_assert_cmpint(tmp
->value1
->boolean
, ==, true);
321 qapi_free_UserDefFlatUnion(tmp
);
324 static void test_visitor_in_alternate(TestInputVisitorData
*data
,
329 UserDefAlternate
*tmp
;
331 v
= visitor_input_test_init(data
, "42");
333 visit_type_UserDefAlternate(v
, &tmp
, NULL
, &err
);
334 g_assert(err
== NULL
);
335 g_assert_cmpint(tmp
->kind
, ==, USER_DEF_ALTERNATE_KIND_I
);
336 g_assert_cmpint(tmp
->i
, ==, 42);
337 qapi_free_UserDefAlternate(tmp
);
340 static void test_native_list_integer_helper(TestInputVisitorData
*data
,
342 UserDefNativeListUnionKind kind
)
344 UserDefNativeListUnion
*cvalue
= NULL
;
347 GString
*gstr_list
= g_string_new("");
348 GString
*gstr_union
= g_string_new("");
351 for (i
= 0; i
< 32; i
++) {
352 g_string_append_printf(gstr_list
, "%d", i
);
354 g_string_append(gstr_list
, ", ");
357 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
358 UserDefNativeListUnionKind_lookup
[kind
],
360 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
362 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
363 g_assert(err
== NULL
);
364 g_assert(cvalue
!= NULL
);
365 g_assert_cmpint(cvalue
->kind
, ==, kind
);
368 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
: {
369 intList
*elem
= NULL
;
370 for (i
= 0, elem
= cvalue
->integer
; elem
; elem
= elem
->next
, i
++) {
371 g_assert_cmpint(elem
->value
, ==, i
);
375 case USER_DEF_NATIVE_LIST_UNION_KIND_S8
: {
376 int8List
*elem
= NULL
;
377 for (i
= 0, elem
= cvalue
->s8
; elem
; elem
= elem
->next
, i
++) {
378 g_assert_cmpint(elem
->value
, ==, i
);
382 case USER_DEF_NATIVE_LIST_UNION_KIND_S16
: {
383 int16List
*elem
= NULL
;
384 for (i
= 0, elem
= cvalue
->s16
; elem
; elem
= elem
->next
, i
++) {
385 g_assert_cmpint(elem
->value
, ==, i
);
389 case USER_DEF_NATIVE_LIST_UNION_KIND_S32
: {
390 int32List
*elem
= NULL
;
391 for (i
= 0, elem
= cvalue
->s32
; elem
; elem
= elem
->next
, i
++) {
392 g_assert_cmpint(elem
->value
, ==, i
);
396 case USER_DEF_NATIVE_LIST_UNION_KIND_S64
: {
397 int64List
*elem
= NULL
;
398 for (i
= 0, elem
= cvalue
->s64
; elem
; elem
= elem
->next
, i
++) {
399 g_assert_cmpint(elem
->value
, ==, i
);
403 case USER_DEF_NATIVE_LIST_UNION_KIND_U8
: {
404 uint8List
*elem
= NULL
;
405 for (i
= 0, elem
= cvalue
->u8
; elem
; elem
= elem
->next
, i
++) {
406 g_assert_cmpint(elem
->value
, ==, i
);
410 case USER_DEF_NATIVE_LIST_UNION_KIND_U16
: {
411 uint16List
*elem
= NULL
;
412 for (i
= 0, elem
= cvalue
->u16
; elem
; elem
= elem
->next
, i
++) {
413 g_assert_cmpint(elem
->value
, ==, i
);
417 case USER_DEF_NATIVE_LIST_UNION_KIND_U32
: {
418 uint32List
*elem
= NULL
;
419 for (i
= 0, elem
= cvalue
->u32
; elem
; elem
= elem
->next
, i
++) {
420 g_assert_cmpint(elem
->value
, ==, i
);
424 case USER_DEF_NATIVE_LIST_UNION_KIND_U64
: {
425 uint64List
*elem
= NULL
;
426 for (i
= 0, elem
= cvalue
->u64
; elem
; elem
= elem
->next
, i
++) {
427 g_assert_cmpint(elem
->value
, ==, i
);
432 g_assert_not_reached();
435 g_string_free(gstr_union
, true);
436 g_string_free(gstr_list
, true);
437 qapi_free_UserDefNativeListUnion(cvalue
);
440 static void test_visitor_in_native_list_int(TestInputVisitorData
*data
,
443 test_native_list_integer_helper(data
, unused
,
444 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
);
447 static void test_visitor_in_native_list_int8(TestInputVisitorData
*data
,
450 test_native_list_integer_helper(data
, unused
,
451 USER_DEF_NATIVE_LIST_UNION_KIND_S8
);
454 static void test_visitor_in_native_list_int16(TestInputVisitorData
*data
,
457 test_native_list_integer_helper(data
, unused
,
458 USER_DEF_NATIVE_LIST_UNION_KIND_S16
);
461 static void test_visitor_in_native_list_int32(TestInputVisitorData
*data
,
464 test_native_list_integer_helper(data
, unused
,
465 USER_DEF_NATIVE_LIST_UNION_KIND_S32
);
468 static void test_visitor_in_native_list_int64(TestInputVisitorData
*data
,
471 test_native_list_integer_helper(data
, unused
,
472 USER_DEF_NATIVE_LIST_UNION_KIND_S64
);
475 static void test_visitor_in_native_list_uint8(TestInputVisitorData
*data
,
478 test_native_list_integer_helper(data
, unused
,
479 USER_DEF_NATIVE_LIST_UNION_KIND_U8
);
482 static void test_visitor_in_native_list_uint16(TestInputVisitorData
*data
,
485 test_native_list_integer_helper(data
, unused
,
486 USER_DEF_NATIVE_LIST_UNION_KIND_U16
);
489 static void test_visitor_in_native_list_uint32(TestInputVisitorData
*data
,
492 test_native_list_integer_helper(data
, unused
,
493 USER_DEF_NATIVE_LIST_UNION_KIND_U32
);
496 static void test_visitor_in_native_list_uint64(TestInputVisitorData
*data
,
499 test_native_list_integer_helper(data
, unused
,
500 USER_DEF_NATIVE_LIST_UNION_KIND_U64
);
503 static void test_visitor_in_native_list_bool(TestInputVisitorData
*data
,
506 UserDefNativeListUnion
*cvalue
= NULL
;
507 boolList
*elem
= NULL
;
510 GString
*gstr_list
= g_string_new("");
511 GString
*gstr_union
= g_string_new("");
514 for (i
= 0; i
< 32; i
++) {
515 g_string_append_printf(gstr_list
, "%s",
516 (i
% 3 == 0) ? "true" : "false");
518 g_string_append(gstr_list
, ", ");
521 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
523 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
525 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
526 g_assert(err
== NULL
);
527 g_assert(cvalue
!= NULL
);
528 g_assert_cmpint(cvalue
->kind
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN
);
530 for (i
= 0, elem
= cvalue
->boolean
; elem
; elem
= elem
->next
, i
++) {
531 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
534 g_string_free(gstr_union
, true);
535 g_string_free(gstr_list
, true);
536 qapi_free_UserDefNativeListUnion(cvalue
);
539 static void test_visitor_in_native_list_string(TestInputVisitorData
*data
,
542 UserDefNativeListUnion
*cvalue
= NULL
;
543 strList
*elem
= NULL
;
546 GString
*gstr_list
= g_string_new("");
547 GString
*gstr_union
= g_string_new("");
550 for (i
= 0; i
< 32; i
++) {
551 g_string_append_printf(gstr_list
, "'%d'", i
);
553 g_string_append(gstr_list
, ", ");
556 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
558 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
560 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
561 g_assert(err
== NULL
);
562 g_assert(cvalue
!= NULL
);
563 g_assert_cmpint(cvalue
->kind
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING
);
565 for (i
= 0, elem
= cvalue
->string
; elem
; elem
= elem
->next
, i
++) {
567 sprintf(str
, "%d", i
);
568 g_assert_cmpstr(elem
->value
, ==, str
);
571 g_string_free(gstr_union
, true);
572 g_string_free(gstr_list
, true);
573 qapi_free_UserDefNativeListUnion(cvalue
);
576 #define DOUBLE_STR_MAX 16
578 static void test_visitor_in_native_list_number(TestInputVisitorData
*data
,
581 UserDefNativeListUnion
*cvalue
= NULL
;
582 numberList
*elem
= NULL
;
585 GString
*gstr_list
= g_string_new("");
586 GString
*gstr_union
= g_string_new("");
589 for (i
= 0; i
< 32; i
++) {
590 g_string_append_printf(gstr_list
, "%f", (double)i
/ 3);
592 g_string_append(gstr_list
, ", ");
595 g_string_append_printf(gstr_union
, "{ 'type': 'number', 'data': [ %s ] }",
597 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
599 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
600 g_assert(err
== NULL
);
601 g_assert(cvalue
!= NULL
);
602 g_assert_cmpint(cvalue
->kind
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER
);
604 for (i
= 0, elem
= cvalue
->number
; elem
; elem
= elem
->next
, i
++) {
605 GString
*double_expected
= g_string_new("");
606 GString
*double_actual
= g_string_new("");
608 g_string_printf(double_expected
, "%.6f", (double)i
/ 3);
609 g_string_printf(double_actual
, "%.6f", elem
->value
);
610 g_assert_cmpstr(double_expected
->str
, ==, double_actual
->str
);
612 g_string_free(double_expected
, true);
613 g_string_free(double_actual
, true);
616 g_string_free(gstr_union
, true);
617 g_string_free(gstr_list
, true);
618 qapi_free_UserDefNativeListUnion(cvalue
);
621 static void input_visitor_test_add(const char *testpath
,
622 TestInputVisitorData
*data
,
623 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
625 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
626 visitor_input_teardown
);
629 static void test_visitor_in_errors(TestInputVisitorData
*data
,
632 TestStruct
*p
= NULL
;
636 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }");
638 visit_type_TestStruct(v
, &p
, NULL
, &err
);
640 /* FIXME - a failed parse should not leave a partially-allocated p
641 * for us to clean up; this could cause callers to leak memory. */
642 g_assert(p
->string
== NULL
);
649 int main(int argc
, char **argv
)
651 TestInputVisitorData in_visitor_data
;
653 g_test_init(&argc
, &argv
, NULL
);
655 input_visitor_test_add("/visitor/input/int",
656 &in_visitor_data
, test_visitor_in_int
);
657 input_visitor_test_add("/visitor/input/int_overflow",
658 &in_visitor_data
, test_visitor_in_int_overflow
);
659 input_visitor_test_add("/visitor/input/bool",
660 &in_visitor_data
, test_visitor_in_bool
);
661 input_visitor_test_add("/visitor/input/number",
662 &in_visitor_data
, test_visitor_in_number
);
663 input_visitor_test_add("/visitor/input/string",
664 &in_visitor_data
, test_visitor_in_string
);
665 input_visitor_test_add("/visitor/input/enum",
666 &in_visitor_data
, test_visitor_in_enum
);
667 input_visitor_test_add("/visitor/input/struct",
668 &in_visitor_data
, test_visitor_in_struct
);
669 input_visitor_test_add("/visitor/input/struct-nested",
670 &in_visitor_data
, test_visitor_in_struct_nested
);
671 input_visitor_test_add("/visitor/input/list",
672 &in_visitor_data
, test_visitor_in_list
);
673 input_visitor_test_add("/visitor/input/union-flat",
674 &in_visitor_data
, test_visitor_in_union_flat
);
675 input_visitor_test_add("/visitor/input/alternate",
676 &in_visitor_data
, test_visitor_in_alternate
);
677 input_visitor_test_add("/visitor/input/errors",
678 &in_visitor_data
, test_visitor_in_errors
);
679 input_visitor_test_add("/visitor/input/native_list/int",
681 test_visitor_in_native_list_int
);
682 input_visitor_test_add("/visitor/input/native_list/int8",
684 test_visitor_in_native_list_int8
);
685 input_visitor_test_add("/visitor/input/native_list/int16",
687 test_visitor_in_native_list_int16
);
688 input_visitor_test_add("/visitor/input/native_list/int32",
690 test_visitor_in_native_list_int32
);
691 input_visitor_test_add("/visitor/input/native_list/int64",
693 test_visitor_in_native_list_int64
);
694 input_visitor_test_add("/visitor/input/native_list/uint8",
696 test_visitor_in_native_list_uint8
);
697 input_visitor_test_add("/visitor/input/native_list/uint16",
699 test_visitor_in_native_list_uint16
);
700 input_visitor_test_add("/visitor/input/native_list/uint32",
702 test_visitor_in_native_list_uint32
);
703 input_visitor_test_add("/visitor/input/native_list/uint64",
705 test_visitor_in_native_list_uint64
);
706 input_visitor_test_add("/visitor/input/native_list/bool",
707 &in_visitor_data
, test_visitor_in_native_list_bool
);
708 input_visitor_test_add("/visitor/input/native_list/str",
710 test_visitor_in_native_list_string
);
711 input_visitor_test_add("/visitor/input/native_list/number",
713 test_visitor_in_native_list_number
);