2 * QObject Input Visitor unit-tests.
4 * Copyright (C) 2011-2016 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 * Paolo Bonzini <pbonzini@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/qobject-input-visitor.h"
19 #include "test-qapi-types.h"
20 #include "test-qapi-visit.h"
21 #include "qapi/qmp/types.h"
22 #include "qapi/qmp/qjson.h"
23 #include "test-qmp-introspect.h"
24 #include "qmp-introspect.h"
25 #include "qapi-visit.h"
27 typedef struct TestInputVisitorData
{
30 } TestInputVisitorData
;
32 static void visitor_input_teardown(TestInputVisitorData
*data
,
35 qobject_decref(data
->obj
);
39 visit_free(data
->qiv
);
44 /* The various test_init functions are provided instead of a test setup
45 function so that the JSON string used by the tests are kept in the test
46 functions (and not in main()). */
47 static Visitor
*visitor_input_test_init_internal(TestInputVisitorData
*data
,
48 const char *json_string
,
51 visitor_input_teardown(data
, NULL
);
53 data
->obj
= qobject_from_jsonv(json_string
, ap
);
56 data
->qiv
= qobject_input_visitor_new(data
->obj
);
61 static GCC_FMT_ATTR(2, 3)
62 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
63 const char *json_string
, ...)
68 va_start(ap
, json_string
);
69 v
= visitor_input_test_init_internal(data
, json_string
, &ap
);
74 /* similar to visitor_input_test_init(), but does not expect a string
75 * literal/format json_string argument and so can be used for
76 * programatically generated strings (and we can't pass in programatically
77 * generated strings via %s format parameters since qobject_from_jsonv()
78 * will wrap those in double-quotes and treat the entire object as a
81 static Visitor
*visitor_input_test_init_raw(TestInputVisitorData
*data
,
82 const char *json_string
)
84 return visitor_input_test_init_internal(data
, json_string
, NULL
);
87 static void test_visitor_in_int(TestInputVisitorData
*data
,
94 v
= visitor_input_test_init(data
, "%d", value
);
96 visit_type_int(v
, NULL
, &res
, &error_abort
);
97 g_assert_cmpint(res
, ==, value
);
100 static void test_visitor_in_int_overflow(TestInputVisitorData
*data
,
107 /* this will overflow a Qint/int64, so should be deserialized into
108 * a QFloat/double field instead, leading to an error if we pass it
109 * to visit_type_int. confirm this.
111 v
= visitor_input_test_init(data
, "%f", DBL_MAX
);
113 visit_type_int(v
, NULL
, &res
, &err
);
114 error_free_or_abort(&err
);
117 static void test_visitor_in_bool(TestInputVisitorData
*data
,
123 v
= visitor_input_test_init(data
, "true");
125 visit_type_bool(v
, NULL
, &res
, &error_abort
);
126 g_assert_cmpint(res
, ==, true);
129 static void test_visitor_in_number(TestInputVisitorData
*data
,
132 double res
= 0, value
= 3.14;
135 v
= visitor_input_test_init(data
, "%f", value
);
137 visit_type_number(v
, NULL
, &res
, &error_abort
);
138 g_assert_cmpfloat(res
, ==, value
);
141 static void test_visitor_in_string(TestInputVisitorData
*data
,
144 char *res
= NULL
, *value
= (char *) "Q E M U";
147 v
= visitor_input_test_init(data
, "%s", value
);
149 visit_type_str(v
, NULL
, &res
, &error_abort
);
150 g_assert_cmpstr(res
, ==, value
);
155 static void test_visitor_in_enum(TestInputVisitorData
*data
,
161 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
164 v
= visitor_input_test_init(data
, "%s", EnumOne_lookup
[i
]);
166 visit_type_EnumOne(v
, NULL
, &res
, &error_abort
);
167 g_assert_cmpint(i
, ==, res
);
172 static void test_visitor_in_struct(TestInputVisitorData
*data
,
175 TestStruct
*p
= NULL
;
178 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
180 visit_type_TestStruct(v
, NULL
, &p
, &error_abort
);
181 g_assert_cmpint(p
->integer
, ==, -42);
182 g_assert(p
->boolean
== true);
183 g_assert_cmpstr(p
->string
, ==, "foo");
189 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
192 UserDefTwo
*udp
= NULL
;
195 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', "
196 "'dict1': { 'string1': 'string1', "
197 "'dict2': { 'userdef': { 'integer': 42, "
198 "'string': 'string' }, 'string': 'string2'}}}");
200 visit_type_UserDefTwo(v
, NULL
, &udp
, &error_abort
);
202 g_assert_cmpstr(udp
->string0
, ==, "string0");
203 g_assert_cmpstr(udp
->dict1
->string1
, ==, "string1");
204 g_assert_cmpint(udp
->dict1
->dict2
->userdef
->integer
, ==, 42);
205 g_assert_cmpstr(udp
->dict1
->dict2
->userdef
->string
, ==, "string");
206 g_assert_cmpstr(udp
->dict1
->dict2
->string
, ==, "string2");
207 g_assert(udp
->dict1
->has_dict3
== false);
209 qapi_free_UserDefTwo(udp
);
212 static void test_visitor_in_list(TestInputVisitorData
*data
,
215 UserDefOneList
*item
, *head
= NULL
;
219 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
221 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
222 g_assert(head
!= NULL
);
224 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
227 snprintf(string
, sizeof(string
), "string%d", i
);
228 g_assert_cmpstr(item
->value
->string
, ==, string
);
229 g_assert_cmpint(item
->value
->integer
, ==, 42 + i
);
232 qapi_free_UserDefOneList(head
);
235 /* An empty list is valid */
236 v
= visitor_input_test_init(data
, "[]");
237 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
241 static void test_visitor_in_any(TestInputVisitorData
*data
,
252 v
= visitor_input_test_init(data
, "-42");
253 visit_type_any(v
, NULL
, &res
, &error_abort
);
254 qint
= qobject_to_qint(res
);
256 g_assert_cmpint(qint_get_int(qint
), ==, -42);
259 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
260 visit_type_any(v
, NULL
, &res
, &error_abort
);
261 qdict
= qobject_to_qdict(res
);
262 g_assert(qdict
&& qdict_size(qdict
) == 3);
263 qobj
= qdict_get(qdict
, "integer");
265 qint
= qobject_to_qint(qobj
);
267 g_assert_cmpint(qint_get_int(qint
), ==, -42);
268 qobj
= qdict_get(qdict
, "boolean");
270 qbool
= qobject_to_qbool(qobj
);
272 g_assert(qbool_get_bool(qbool
) == true);
273 qobj
= qdict_get(qdict
, "string");
275 qstring
= qobject_to_qstring(qobj
);
277 g_assert_cmpstr(qstring_get_str(qstring
), ==, "foo");
281 static void test_visitor_in_null(TestInputVisitorData
*data
,
289 * FIXME: Since QAPI doesn't know the 'null' type yet, we can't
290 * test visit_type_null() by reading into a QAPI struct then
291 * checking that it was populated correctly. The best we can do
292 * for now is ensure that we consumed null from the input, proven
293 * by the fact that we can't re-read the key; and that we detect
294 * when input is not null.
297 v
= visitor_input_test_init(data
, "{ 'a': null, 'b': '', 'c': null }");
298 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
299 visit_type_null(v
, "a", &error_abort
);
300 visit_type_null(v
, "b", &err
);
301 error_free_or_abort(&err
);
302 visit_type_str(v
, "c", &tmp
, &err
);
304 error_free_or_abort(&err
);
305 visit_check_struct(v
, &error_abort
);
306 visit_end_struct(v
, NULL
);
309 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
313 UserDefFlatUnion
*tmp
;
314 UserDefUnionBase
*base
;
316 v
= visitor_input_test_init(data
,
317 "{ 'enum1': 'value1', "
320 "'boolean': true }");
322 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &error_abort
);
323 g_assert_cmpint(tmp
->enum1
, ==, ENUM_ONE_VALUE1
);
324 g_assert_cmpstr(tmp
->string
, ==, "str");
325 g_assert_cmpint(tmp
->integer
, ==, 41);
326 g_assert_cmpint(tmp
->u
.value1
.boolean
, ==, true);
328 base
= qapi_UserDefFlatUnion_base(tmp
);
329 g_assert(&base
->enum1
== &tmp
->enum1
);
331 qapi_free_UserDefFlatUnion(tmp
);
334 static void test_visitor_in_alternate(TestInputVisitorData
*data
,
339 UserDefAlternate
*tmp
;
342 v
= visitor_input_test_init(data
, "42");
343 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
344 g_assert_cmpint(tmp
->type
, ==, QTYPE_QINT
);
345 g_assert_cmpint(tmp
->u
.i
, ==, 42);
346 qapi_free_UserDefAlternate(tmp
);
348 v
= visitor_input_test_init(data
, "'string'");
349 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
350 g_assert_cmpint(tmp
->type
, ==, QTYPE_QSTRING
);
351 g_assert_cmpstr(tmp
->u
.s
, ==, "string");
352 qapi_free_UserDefAlternate(tmp
);
354 v
= visitor_input_test_init(data
, "{'integer':1, 'string':'str', "
355 "'enum1':'value1', 'boolean':true}");
356 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
357 g_assert_cmpint(tmp
->type
, ==, QTYPE_QDICT
);
358 g_assert_cmpint(tmp
->u
.udfu
.integer
, ==, 1);
359 g_assert_cmpstr(tmp
->u
.udfu
.string
, ==, "str");
360 g_assert_cmpint(tmp
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
361 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.boolean
, ==, true);
362 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
363 qapi_free_UserDefAlternate(tmp
);
365 v
= visitor_input_test_init(data
, "false");
366 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
367 error_free_or_abort(&err
);
368 qapi_free_UserDefAlternate(tmp
);
370 v
= visitor_input_test_init(data
, "{ 'alt': 42 }");
371 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
372 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QINT
);
373 g_assert_cmpint(wrap
->alt
->u
.i
, ==, 42);
374 qapi_free_WrapAlternate(wrap
);
376 v
= visitor_input_test_init(data
, "{ 'alt': 'string' }");
377 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
378 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QSTRING
);
379 g_assert_cmpstr(wrap
->alt
->u
.s
, ==, "string");
380 qapi_free_WrapAlternate(wrap
);
382 v
= visitor_input_test_init(data
, "{ 'alt': {'integer':1, 'string':'str', "
383 "'enum1':'value1', 'boolean':true} }");
384 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
385 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QDICT
);
386 g_assert_cmpint(wrap
->alt
->u
.udfu
.integer
, ==, 1);
387 g_assert_cmpstr(wrap
->alt
->u
.udfu
.string
, ==, "str");
388 g_assert_cmpint(wrap
->alt
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
389 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.boolean
, ==, true);
390 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
391 qapi_free_WrapAlternate(wrap
);
394 static void test_visitor_in_alternate_number(TestInputVisitorData
*data
,
408 v
= visitor_input_test_init(data
, "42");
409 visit_type_AltStrBool(v
, NULL
, &asb
, &err
);
410 error_free_or_abort(&err
);
411 qapi_free_AltStrBool(asb
);
413 v
= visitor_input_test_init(data
, "42");
414 visit_type_AltStrNum(v
, NULL
, &asn
, &error_abort
);
415 g_assert_cmpint(asn
->type
, ==, QTYPE_QFLOAT
);
416 g_assert_cmpfloat(asn
->u
.n
, ==, 42);
417 qapi_free_AltStrNum(asn
);
419 v
= visitor_input_test_init(data
, "42");
420 visit_type_AltNumStr(v
, NULL
, &ans
, &error_abort
);
421 g_assert_cmpint(ans
->type
, ==, QTYPE_QFLOAT
);
422 g_assert_cmpfloat(ans
->u
.n
, ==, 42);
423 qapi_free_AltNumStr(ans
);
425 v
= visitor_input_test_init(data
, "42");
426 visit_type_AltStrInt(v
, NULL
, &asi
, &error_abort
);
427 g_assert_cmpint(asi
->type
, ==, QTYPE_QINT
);
428 g_assert_cmpint(asi
->u
.i
, ==, 42);
429 qapi_free_AltStrInt(asi
);
431 v
= visitor_input_test_init(data
, "42");
432 visit_type_AltIntNum(v
, NULL
, &ain
, &error_abort
);
433 g_assert_cmpint(ain
->type
, ==, QTYPE_QINT
);
434 g_assert_cmpint(ain
->u
.i
, ==, 42);
435 qapi_free_AltIntNum(ain
);
437 v
= visitor_input_test_init(data
, "42");
438 visit_type_AltNumInt(v
, NULL
, &ani
, &error_abort
);
439 g_assert_cmpint(ani
->type
, ==, QTYPE_QINT
);
440 g_assert_cmpint(ani
->u
.i
, ==, 42);
441 qapi_free_AltNumInt(ani
);
443 /* Parsing a double */
445 v
= visitor_input_test_init(data
, "42.5");
446 visit_type_AltStrBool(v
, NULL
, &asb
, &err
);
447 error_free_or_abort(&err
);
448 qapi_free_AltStrBool(asb
);
450 v
= visitor_input_test_init(data
, "42.5");
451 visit_type_AltStrNum(v
, NULL
, &asn
, &error_abort
);
452 g_assert_cmpint(asn
->type
, ==, QTYPE_QFLOAT
);
453 g_assert_cmpfloat(asn
->u
.n
, ==, 42.5);
454 qapi_free_AltStrNum(asn
);
456 v
= visitor_input_test_init(data
, "42.5");
457 visit_type_AltNumStr(v
, NULL
, &ans
, &error_abort
);
458 g_assert_cmpint(ans
->type
, ==, QTYPE_QFLOAT
);
459 g_assert_cmpfloat(ans
->u
.n
, ==, 42.5);
460 qapi_free_AltNumStr(ans
);
462 v
= visitor_input_test_init(data
, "42.5");
463 visit_type_AltStrInt(v
, NULL
, &asi
, &err
);
464 error_free_or_abort(&err
);
465 qapi_free_AltStrInt(asi
);
467 v
= visitor_input_test_init(data
, "42.5");
468 visit_type_AltIntNum(v
, NULL
, &ain
, &error_abort
);
469 g_assert_cmpint(ain
->type
, ==, QTYPE_QFLOAT
);
470 g_assert_cmpfloat(ain
->u
.n
, ==, 42.5);
471 qapi_free_AltIntNum(ain
);
473 v
= visitor_input_test_init(data
, "42.5");
474 visit_type_AltNumInt(v
, NULL
, &ani
, &error_abort
);
475 g_assert_cmpint(ani
->type
, ==, QTYPE_QFLOAT
);
476 g_assert_cmpfloat(ani
->u
.n
, ==, 42.5);
477 qapi_free_AltNumInt(ani
);
480 static void test_native_list_integer_helper(TestInputVisitorData
*data
,
482 UserDefNativeListUnionKind kind
)
484 UserDefNativeListUnion
*cvalue
= NULL
;
486 GString
*gstr_list
= g_string_new("");
487 GString
*gstr_union
= g_string_new("");
490 for (i
= 0; i
< 32; i
++) {
491 g_string_append_printf(gstr_list
, "%d", i
);
493 g_string_append(gstr_list
, ", ");
496 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
497 UserDefNativeListUnionKind_lookup
[kind
],
499 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
501 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
502 g_assert(cvalue
!= NULL
);
503 g_assert_cmpint(cvalue
->type
, ==, kind
);
506 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
: {
507 intList
*elem
= NULL
;
508 for (i
= 0, elem
= cvalue
->u
.integer
.data
;
509 elem
; elem
= elem
->next
, i
++) {
510 g_assert_cmpint(elem
->value
, ==, i
);
514 case USER_DEF_NATIVE_LIST_UNION_KIND_S8
: {
515 int8List
*elem
= NULL
;
516 for (i
= 0, elem
= cvalue
->u
.s8
.data
; elem
; elem
= elem
->next
, i
++) {
517 g_assert_cmpint(elem
->value
, ==, i
);
521 case USER_DEF_NATIVE_LIST_UNION_KIND_S16
: {
522 int16List
*elem
= NULL
;
523 for (i
= 0, elem
= cvalue
->u
.s16
.data
; elem
; elem
= elem
->next
, i
++) {
524 g_assert_cmpint(elem
->value
, ==, i
);
528 case USER_DEF_NATIVE_LIST_UNION_KIND_S32
: {
529 int32List
*elem
= NULL
;
530 for (i
= 0, elem
= cvalue
->u
.s32
.data
; elem
; elem
= elem
->next
, i
++) {
531 g_assert_cmpint(elem
->value
, ==, i
);
535 case USER_DEF_NATIVE_LIST_UNION_KIND_S64
: {
536 int64List
*elem
= NULL
;
537 for (i
= 0, elem
= cvalue
->u
.s64
.data
; elem
; elem
= elem
->next
, i
++) {
538 g_assert_cmpint(elem
->value
, ==, i
);
542 case USER_DEF_NATIVE_LIST_UNION_KIND_U8
: {
543 uint8List
*elem
= NULL
;
544 for (i
= 0, elem
= cvalue
->u
.u8
.data
; elem
; elem
= elem
->next
, i
++) {
545 g_assert_cmpint(elem
->value
, ==, i
);
549 case USER_DEF_NATIVE_LIST_UNION_KIND_U16
: {
550 uint16List
*elem
= NULL
;
551 for (i
= 0, elem
= cvalue
->u
.u16
.data
; elem
; elem
= elem
->next
, i
++) {
552 g_assert_cmpint(elem
->value
, ==, i
);
556 case USER_DEF_NATIVE_LIST_UNION_KIND_U32
: {
557 uint32List
*elem
= NULL
;
558 for (i
= 0, elem
= cvalue
->u
.u32
.data
; elem
; elem
= elem
->next
, i
++) {
559 g_assert_cmpint(elem
->value
, ==, i
);
563 case USER_DEF_NATIVE_LIST_UNION_KIND_U64
: {
564 uint64List
*elem
= NULL
;
565 for (i
= 0, elem
= cvalue
->u
.u64
.data
; elem
; elem
= elem
->next
, i
++) {
566 g_assert_cmpint(elem
->value
, ==, i
);
571 g_assert_not_reached();
574 g_string_free(gstr_union
, true);
575 g_string_free(gstr_list
, true);
576 qapi_free_UserDefNativeListUnion(cvalue
);
579 static void test_visitor_in_native_list_int(TestInputVisitorData
*data
,
582 test_native_list_integer_helper(data
, unused
,
583 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
);
586 static void test_visitor_in_native_list_int8(TestInputVisitorData
*data
,
589 test_native_list_integer_helper(data
, unused
,
590 USER_DEF_NATIVE_LIST_UNION_KIND_S8
);
593 static void test_visitor_in_native_list_int16(TestInputVisitorData
*data
,
596 test_native_list_integer_helper(data
, unused
,
597 USER_DEF_NATIVE_LIST_UNION_KIND_S16
);
600 static void test_visitor_in_native_list_int32(TestInputVisitorData
*data
,
603 test_native_list_integer_helper(data
, unused
,
604 USER_DEF_NATIVE_LIST_UNION_KIND_S32
);
607 static void test_visitor_in_native_list_int64(TestInputVisitorData
*data
,
610 test_native_list_integer_helper(data
, unused
,
611 USER_DEF_NATIVE_LIST_UNION_KIND_S64
);
614 static void test_visitor_in_native_list_uint8(TestInputVisitorData
*data
,
617 test_native_list_integer_helper(data
, unused
,
618 USER_DEF_NATIVE_LIST_UNION_KIND_U8
);
621 static void test_visitor_in_native_list_uint16(TestInputVisitorData
*data
,
624 test_native_list_integer_helper(data
, unused
,
625 USER_DEF_NATIVE_LIST_UNION_KIND_U16
);
628 static void test_visitor_in_native_list_uint32(TestInputVisitorData
*data
,
631 test_native_list_integer_helper(data
, unused
,
632 USER_DEF_NATIVE_LIST_UNION_KIND_U32
);
635 static void test_visitor_in_native_list_uint64(TestInputVisitorData
*data
,
638 test_native_list_integer_helper(data
, unused
,
639 USER_DEF_NATIVE_LIST_UNION_KIND_U64
);
642 static void test_visitor_in_native_list_bool(TestInputVisitorData
*data
,
645 UserDefNativeListUnion
*cvalue
= NULL
;
646 boolList
*elem
= NULL
;
648 GString
*gstr_list
= g_string_new("");
649 GString
*gstr_union
= g_string_new("");
652 for (i
= 0; i
< 32; i
++) {
653 g_string_append_printf(gstr_list
, "%s",
654 (i
% 3 == 0) ? "true" : "false");
656 g_string_append(gstr_list
, ", ");
659 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
661 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
663 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
664 g_assert(cvalue
!= NULL
);
665 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN
);
667 for (i
= 0, elem
= cvalue
->u
.boolean
.data
; elem
; elem
= elem
->next
, i
++) {
668 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
671 g_string_free(gstr_union
, true);
672 g_string_free(gstr_list
, true);
673 qapi_free_UserDefNativeListUnion(cvalue
);
676 static void test_visitor_in_native_list_string(TestInputVisitorData
*data
,
679 UserDefNativeListUnion
*cvalue
= NULL
;
680 strList
*elem
= NULL
;
682 GString
*gstr_list
= g_string_new("");
683 GString
*gstr_union
= g_string_new("");
686 for (i
= 0; i
< 32; i
++) {
687 g_string_append_printf(gstr_list
, "'%d'", i
);
689 g_string_append(gstr_list
, ", ");
692 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
694 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
696 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
697 g_assert(cvalue
!= NULL
);
698 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING
);
700 for (i
= 0, elem
= cvalue
->u
.string
.data
; elem
; elem
= elem
->next
, i
++) {
702 sprintf(str
, "%d", i
);
703 g_assert_cmpstr(elem
->value
, ==, str
);
706 g_string_free(gstr_union
, true);
707 g_string_free(gstr_list
, true);
708 qapi_free_UserDefNativeListUnion(cvalue
);
711 #define DOUBLE_STR_MAX 16
713 static void test_visitor_in_native_list_number(TestInputVisitorData
*data
,
716 UserDefNativeListUnion
*cvalue
= NULL
;
717 numberList
*elem
= NULL
;
719 GString
*gstr_list
= g_string_new("");
720 GString
*gstr_union
= g_string_new("");
723 for (i
= 0; i
< 32; i
++) {
724 g_string_append_printf(gstr_list
, "%f", (double)i
/ 3);
726 g_string_append(gstr_list
, ", ");
729 g_string_append_printf(gstr_union
, "{ 'type': 'number', 'data': [ %s ] }",
731 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
733 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
734 g_assert(cvalue
!= NULL
);
735 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER
);
737 for (i
= 0, elem
= cvalue
->u
.number
.data
; elem
; elem
= elem
->next
, i
++) {
738 GString
*double_expected
= g_string_new("");
739 GString
*double_actual
= g_string_new("");
741 g_string_printf(double_expected
, "%.6f", (double)i
/ 3);
742 g_string_printf(double_actual
, "%.6f", elem
->value
);
743 g_assert_cmpstr(double_expected
->str
, ==, double_actual
->str
);
745 g_string_free(double_expected
, true);
746 g_string_free(double_actual
, true);
749 g_string_free(gstr_union
, true);
750 g_string_free(gstr_list
, true);
751 qapi_free_UserDefNativeListUnion(cvalue
);
754 static void input_visitor_test_add(const char *testpath
,
755 const void *user_data
,
756 void (*test_func
)(TestInputVisitorData
*data
,
757 const void *user_data
))
759 g_test_add(testpath
, TestInputVisitorData
, user_data
, NULL
, test_func
,
760 visitor_input_teardown
);
763 static void test_visitor_in_errors(TestInputVisitorData
*data
,
766 TestStruct
*p
= NULL
;
770 UserDefTwo
*r
= NULL
;
771 WrapAlternate
*s
= NULL
;
773 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', "
776 visit_type_TestStruct(v
, NULL
, &p
, &err
);
777 error_free_or_abort(&err
);
780 v
= visitor_input_test_init(data
, "[ '1', '2', false, '3' ]");
781 visit_type_strList(v
, NULL
, &q
, &err
);
782 error_free_or_abort(&err
);
785 v
= visitor_input_test_init(data
, "{ 'str':'hi' }");
786 visit_type_UserDefTwo(v
, NULL
, &r
, &err
);
787 error_free_or_abort(&err
);
790 v
= visitor_input_test_init(data
, "{ }");
791 visit_type_WrapAlternate(v
, NULL
, &s
, &err
);
792 error_free_or_abort(&err
);
796 static void test_visitor_in_wrong_type(TestInputVisitorData
*data
,
799 TestStruct
*p
= NULL
;
805 /* Make sure arrays and structs cannot be confused */
807 v
= visitor_input_test_init(data
, "[]");
808 visit_type_TestStruct(v
, NULL
, &p
, &err
);
809 error_free_or_abort(&err
);
812 v
= visitor_input_test_init(data
, "{}");
813 visit_type_strList(v
, NULL
, &q
, &err
);
814 error_free_or_abort(&err
);
817 /* Make sure primitives and struct cannot be confused */
819 v
= visitor_input_test_init(data
, "1");
820 visit_type_TestStruct(v
, NULL
, &p
, &err
);
821 error_free_or_abort(&err
);
824 v
= visitor_input_test_init(data
, "{}");
825 visit_type_int(v
, NULL
, &i
, &err
);
826 error_free_or_abort(&err
);
828 /* Make sure primitives and arrays cannot be confused */
830 v
= visitor_input_test_init(data
, "1");
831 visit_type_strList(v
, NULL
, &q
, &err
);
832 error_free_or_abort(&err
);
835 v
= visitor_input_test_init(data
, "[]");
836 visit_type_int(v
, NULL
, &i
, &err
);
837 error_free_or_abort(&err
);
840 static void test_visitor_in_fail_struct(TestInputVisitorData
*data
,
843 TestStruct
*p
= NULL
;
847 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
849 visit_type_TestStruct(v
, NULL
, &p
, &err
);
850 error_free_or_abort(&err
);
854 static void test_visitor_in_fail_struct_nested(TestInputVisitorData
*data
,
857 UserDefTwo
*udp
= NULL
;
861 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
863 visit_type_UserDefTwo(v
, NULL
, &udp
, &err
);
864 error_free_or_abort(&err
);
868 static void test_visitor_in_fail_struct_in_list(TestInputVisitorData
*data
,
871 UserDefOneList
*head
= NULL
;
875 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
877 visit_type_UserDefOneList(v
, NULL
, &head
, &err
);
878 error_free_or_abort(&err
);
882 static void test_visitor_in_fail_struct_missing(TestInputVisitorData
*data
,
888 GenericAlternate
*alt
;
897 v
= visitor_input_test_init(data
, "{ 'sub': [ {} ] }");
898 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
899 visit_start_struct(v
, "struct", NULL
, 0, &err
);
900 error_free_or_abort(&err
);
901 visit_start_list(v
, "list", NULL
, 0, &err
);
902 error_free_or_abort(&err
);
903 visit_start_alternate(v
, "alternate", &alt
, sizeof(*alt
), false, &err
);
904 error_free_or_abort(&err
);
905 visit_optional(v
, "optional", &present
);
907 visit_type_enum(v
, "enum", &en
, EnumOne_lookup
, &err
);
908 error_free_or_abort(&err
);
909 visit_type_int(v
, "i64", &i64
, &err
);
910 error_free_or_abort(&err
);
911 visit_type_uint32(v
, "u32", &u32
, &err
);
912 error_free_or_abort(&err
);
913 visit_type_int8(v
, "i8", &i8
, &err
);
914 error_free_or_abort(&err
);
915 visit_type_str(v
, "i8", &str
, &err
);
916 error_free_or_abort(&err
);
917 visit_type_number(v
, "dbl", &dbl
, &err
);
918 error_free_or_abort(&err
);
919 visit_type_any(v
, "any", &any
, &err
);
920 error_free_or_abort(&err
);
921 visit_type_null(v
, "null", &err
);
922 error_free_or_abort(&err
);
923 visit_start_list(v
, "sub", NULL
, 0, &error_abort
);
924 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
925 visit_type_int(v
, "i64", &i64
, &err
);
926 error_free_or_abort(&err
);
927 visit_end_struct(v
, NULL
);
928 visit_end_list(v
, NULL
);
929 visit_end_struct(v
, NULL
);
932 static void test_visitor_in_fail_list(TestInputVisitorData
*data
,
939 /* Unvisited list tail */
941 v
= visitor_input_test_init(data
, "[ 1, 2, 3 ]");
943 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
944 visit_type_int(v
, NULL
, &i64
, &error_abort
);
945 g_assert_cmpint(i64
, ==, 1);
946 visit_type_int(v
, NULL
, &i64
, &error_abort
);
947 g_assert_cmpint(i64
, ==, 2);
948 visit_check_list(v
, &err
);
949 error_free_or_abort(&err
);
950 visit_end_list(v
, NULL
);
952 /* Visit beyond end of list */
953 v
= visitor_input_test_init(data
, "[]");
955 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
956 visit_type_int(v
, NULL
, &i64
, &err
);
957 error_free_or_abort(&err
);
958 visit_end_list(v
, NULL
);
961 static void test_visitor_in_fail_list_nested(TestInputVisitorData
*data
,
968 /* Unvisited nested list tail */
970 v
= visitor_input_test_init(data
, "[ 0, [ 1, 2, 3 ] ]");
972 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
973 visit_type_int(v
, NULL
, &i64
, &error_abort
);
974 g_assert_cmpint(i64
, ==, 0);
975 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
976 visit_type_int(v
, NULL
, &i64
, &error_abort
);
977 g_assert_cmpint(i64
, ==, 1);
978 visit_check_list(v
, &err
);
979 error_free_or_abort(&err
);
980 visit_end_list(v
, NULL
);
981 visit_check_list(v
, &error_abort
);
982 visit_end_list(v
, NULL
);
985 static void test_visitor_in_fail_union_native_list(TestInputVisitorData
*data
,
988 UserDefNativeListUnion
*tmp
= NULL
;
992 v
= visitor_input_test_init(data
,
993 "{ 'type': 'integer', 'data' : [ 'string' ] }");
995 visit_type_UserDefNativeListUnion(v
, NULL
, &tmp
, &err
);
996 error_free_or_abort(&err
);
1000 static void test_visitor_in_fail_union_flat(TestInputVisitorData
*data
,
1003 UserDefFlatUnion
*tmp
= NULL
;
1007 v
= visitor_input_test_init(data
, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
1009 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &err
);
1010 error_free_or_abort(&err
);
1014 static void test_visitor_in_fail_union_flat_no_discrim(TestInputVisitorData
*data
,
1017 UserDefFlatUnion2
*tmp
= NULL
;
1021 /* test situation where discriminator field ('enum1' here) is missing */
1022 v
= visitor_input_test_init(data
, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
1024 visit_type_UserDefFlatUnion2(v
, NULL
, &tmp
, &err
);
1025 error_free_or_abort(&err
);
1029 static void test_visitor_in_fail_alternate(TestInputVisitorData
*data
,
1032 UserDefAlternate
*tmp
;
1036 v
= visitor_input_test_init(data
, "3.14");
1038 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
1039 error_free_or_abort(&err
);
1043 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1044 const char *schema_json
)
1046 SchemaInfoList
*schema
= NULL
;
1049 v
= visitor_input_test_init_raw(data
, schema_json
);
1051 visit_type_SchemaInfoList(v
, NULL
, &schema
, &error_abort
);
1054 qapi_free_SchemaInfoList(schema
);
1057 static void test_visitor_in_qmp_introspect(TestInputVisitorData
*data
,
1060 do_test_visitor_in_qmp_introspect(data
, test_qmp_schema_json
);
1061 do_test_visitor_in_qmp_introspect(data
, qmp_schema_json
);
1064 int main(int argc
, char **argv
)
1066 g_test_init(&argc
, &argv
, NULL
);
1068 input_visitor_test_add("/visitor/input/int",
1069 NULL
, test_visitor_in_int
);
1070 input_visitor_test_add("/visitor/input/int_overflow",
1071 NULL
, test_visitor_in_int_overflow
);
1072 input_visitor_test_add("/visitor/input/bool",
1073 NULL
, test_visitor_in_bool
);
1074 input_visitor_test_add("/visitor/input/number",
1075 NULL
, test_visitor_in_number
);
1076 input_visitor_test_add("/visitor/input/string",
1077 NULL
, test_visitor_in_string
);
1078 input_visitor_test_add("/visitor/input/enum",
1079 NULL
, test_visitor_in_enum
);
1080 input_visitor_test_add("/visitor/input/struct",
1081 NULL
, test_visitor_in_struct
);
1082 input_visitor_test_add("/visitor/input/struct-nested",
1083 NULL
, test_visitor_in_struct_nested
);
1084 input_visitor_test_add("/visitor/input/list",
1085 NULL
, test_visitor_in_list
);
1086 input_visitor_test_add("/visitor/input/any",
1087 NULL
, test_visitor_in_any
);
1088 input_visitor_test_add("/visitor/input/null",
1089 NULL
, test_visitor_in_null
);
1090 input_visitor_test_add("/visitor/input/union-flat",
1091 NULL
, test_visitor_in_union_flat
);
1092 input_visitor_test_add("/visitor/input/alternate",
1093 NULL
, test_visitor_in_alternate
);
1094 input_visitor_test_add("/visitor/input/errors",
1095 NULL
, test_visitor_in_errors
);
1096 input_visitor_test_add("/visitor/input/wrong-type",
1097 NULL
, test_visitor_in_wrong_type
);
1098 input_visitor_test_add("/visitor/input/alternate-number",
1099 NULL
, test_visitor_in_alternate_number
);
1100 input_visitor_test_add("/visitor/input/native_list/int",
1101 NULL
, test_visitor_in_native_list_int
);
1102 input_visitor_test_add("/visitor/input/native_list/int8",
1103 NULL
, test_visitor_in_native_list_int8
);
1104 input_visitor_test_add("/visitor/input/native_list/int16",
1105 NULL
, test_visitor_in_native_list_int16
);
1106 input_visitor_test_add("/visitor/input/native_list/int32",
1107 NULL
, test_visitor_in_native_list_int32
);
1108 input_visitor_test_add("/visitor/input/native_list/int64",
1109 NULL
, test_visitor_in_native_list_int64
);
1110 input_visitor_test_add("/visitor/input/native_list/uint8",
1111 NULL
, test_visitor_in_native_list_uint8
);
1112 input_visitor_test_add("/visitor/input/native_list/uint16",
1113 NULL
, test_visitor_in_native_list_uint16
);
1114 input_visitor_test_add("/visitor/input/native_list/uint32",
1115 NULL
, test_visitor_in_native_list_uint32
);
1116 input_visitor_test_add("/visitor/input/native_list/uint64",
1117 NULL
, test_visitor_in_native_list_uint64
);
1118 input_visitor_test_add("/visitor/input/native_list/bool",
1119 NULL
, test_visitor_in_native_list_bool
);
1120 input_visitor_test_add("/visitor/input/native_list/str",
1121 NULL
, test_visitor_in_native_list_string
);
1122 input_visitor_test_add("/visitor/input/native_list/number",
1123 NULL
, test_visitor_in_native_list_number
);
1124 input_visitor_test_add("/visitor/input/fail/struct",
1125 NULL
, test_visitor_in_fail_struct
);
1126 input_visitor_test_add("/visitor/input/fail/struct-nested",
1127 NULL
, test_visitor_in_fail_struct_nested
);
1128 input_visitor_test_add("/visitor/input/fail/struct-in-list",
1129 NULL
, test_visitor_in_fail_struct_in_list
);
1130 input_visitor_test_add("/visitor/input/fail/struct-missing",
1131 NULL
, test_visitor_in_fail_struct_missing
);
1132 input_visitor_test_add("/visitor/input/fail/list",
1133 NULL
, test_visitor_in_fail_list
);
1134 input_visitor_test_add("/visitor/input/fail/list-nested",
1135 NULL
, test_visitor_in_fail_list_nested
);
1136 input_visitor_test_add("/visitor/input/fail/union-flat",
1137 NULL
, test_visitor_in_fail_union_flat
);
1138 input_visitor_test_add("/visitor/input/fail/union-flat-no-discriminator",
1139 NULL
, test_visitor_in_fail_union_flat_no_discrim
);
1140 input_visitor_test_add("/visitor/input/fail/alternate",
1141 NULL
, test_visitor_in_fail_alternate
);
1142 input_visitor_test_add("/visitor/input/fail/union-native-list",
1143 NULL
, test_visitor_in_fail_union_native_list
);
1144 input_visitor_test_add("/visitor/input/qmp-introspect",
1145 NULL
, test_visitor_in_qmp_introspect
);