Merge remote-tracking branch 'remotes/armbru/tags/pull-qapi-2017-02-28' into staging
[qemu/ar7.git] / tests / test-qobject-input-visitor.c
blob94305f58ca7d3494356031c4cccb53ec492b72ed
1 /*
2 * QObject Input Visitor unit-tests.
4 * Copyright (C) 2011-2016 Red Hat Inc.
6 * Authors:
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 {
28 QObject *obj;
29 Visitor *qiv;
30 } TestInputVisitorData;
32 static void visitor_input_teardown(TestInputVisitorData *data,
33 const void *unused)
35 qobject_decref(data->obj);
36 data->obj = NULL;
38 if (data->qiv) {
39 visit_free(data->qiv);
40 data->qiv = NULL;
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,
49 va_list *ap)
51 visitor_input_teardown(data, NULL);
53 data->obj = qobject_from_jsonv(json_string, ap);
54 g_assert(data->obj);
56 data->qiv = qobject_input_visitor_new(data->obj);
57 g_assert(data->qiv);
58 return data->qiv;
61 static GCC_FMT_ATTR(2, 3)
62 Visitor *visitor_input_test_init(TestInputVisitorData *data,
63 const char *json_string, ...)
65 Visitor *v;
66 va_list ap;
68 va_start(ap, json_string);
69 v = visitor_input_test_init_internal(data, json_string, &ap);
70 va_end(ap);
71 return v;
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
79 * string)
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,
88 const void *unused)
90 int64_t res = 0;
91 int value = -42;
92 Visitor *v;
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,
101 const void *unused)
103 int64_t res = 0;
104 Error *err = NULL;
105 Visitor *v;
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,
118 const void *unused)
120 bool res = false;
121 Visitor *v;
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,
130 const void *unused)
132 double res = 0, value = 3.14;
133 Visitor *v;
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,
142 const void *unused)
144 char *res = NULL, *value = (char *) "Q E M U";
145 Visitor *v;
147 v = visitor_input_test_init(data, "%s", value);
149 visit_type_str(v, NULL, &res, &error_abort);
150 g_assert_cmpstr(res, ==, value);
152 g_free(res);
155 static void test_visitor_in_enum(TestInputVisitorData *data,
156 const void *unused)
158 Visitor *v;
159 EnumOne i;
161 for (i = 0; EnumOne_lookup[i]; i++) {
162 EnumOne res = -1;
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,
173 const void *unused)
175 TestStruct *p = NULL;
176 Visitor *v;
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");
185 g_free(p->string);
186 g_free(p);
189 static void test_visitor_in_struct_nested(TestInputVisitorData *data,
190 const void *unused)
192 UserDefTwo *udp = NULL;
193 Visitor *v;
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,
213 const void *unused)
215 UserDefOneList *item, *head = NULL;
216 Visitor *v;
217 int i;
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++) {
225 char string[12];
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);
233 head = NULL;
235 /* An empty list is valid */
236 v = visitor_input_test_init(data, "[]");
237 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
238 g_assert(!head);
241 static void test_visitor_in_any(TestInputVisitorData *data,
242 const void *unused)
244 QObject *res = NULL;
245 Visitor *v;
246 QInt *qint;
247 QBool *qbool;
248 QString *qstring;
249 QDict *qdict;
250 QObject *qobj;
252 v = visitor_input_test_init(data, "-42");
253 visit_type_any(v, NULL, &res, &error_abort);
254 qint = qobject_to_qint(res);
255 g_assert(qint);
256 g_assert_cmpint(qint_get_int(qint), ==, -42);
257 qobject_decref(res);
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");
264 g_assert(qobj);
265 qint = qobject_to_qint(qobj);
266 g_assert(qint);
267 g_assert_cmpint(qint_get_int(qint), ==, -42);
268 qobj = qdict_get(qdict, "boolean");
269 g_assert(qobj);
270 qbool = qobject_to_qbool(qobj);
271 g_assert(qbool);
272 g_assert(qbool_get_bool(qbool) == true);
273 qobj = qdict_get(qdict, "string");
274 g_assert(qobj);
275 qstring = qobject_to_qstring(qobj);
276 g_assert(qstring);
277 g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
278 qobject_decref(res);
281 static void test_visitor_in_null(TestInputVisitorData *data,
282 const void *unused)
284 Visitor *v;
285 Error *err = NULL;
286 char *tmp;
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);
303 g_assert(!tmp);
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,
310 const void *unused)
312 Visitor *v;
313 UserDefFlatUnion *tmp;
314 UserDefUnionBase *base;
316 v = visitor_input_test_init(data,
317 "{ 'enum1': 'value1', "
318 "'integer': 41, "
319 "'string': 'str', "
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,
335 const void *unused)
337 Visitor *v;
338 Error *err = NULL;
339 UserDefAlternate *tmp;
340 WrapAlternate *wrap;
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,
395 const void *unused)
397 Visitor *v;
398 Error *err = NULL;
399 AltStrBool *asb;
400 AltStrNum *asn;
401 AltNumStr *ans;
402 AltStrInt *asi;
403 AltIntNum *ain;
404 AltNumInt *ani;
406 /* Parsing an int */
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,
481 const void *unused,
482 UserDefNativeListUnionKind kind)
484 UserDefNativeListUnion *cvalue = NULL;
485 Visitor *v;
486 GString *gstr_list = g_string_new("");
487 GString *gstr_union = g_string_new("");
488 int i;
490 for (i = 0; i < 32; i++) {
491 g_string_append_printf(gstr_list, "%d", i);
492 if (i != 31) {
493 g_string_append(gstr_list, ", ");
496 g_string_append_printf(gstr_union, "{ 'type': '%s', 'data': [ %s ] }",
497 UserDefNativeListUnionKind_lookup[kind],
498 gstr_list->str);
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);
505 switch (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);
512 break;
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);
519 break;
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);
526 break;
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);
533 break;
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);
540 break;
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);
547 break;
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);
554 break;
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);
561 break;
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);
568 break;
570 default:
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,
580 const void *unused)
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,
587 const void *unused)
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,
594 const void *unused)
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,
601 const void *unused)
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,
608 const void *unused)
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,
615 const void *unused)
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,
622 const void *unused)
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,
629 const void *unused)
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,
636 const void *unused)
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,
643 const void *unused)
645 UserDefNativeListUnion *cvalue = NULL;
646 boolList *elem = NULL;
647 Visitor *v;
648 GString *gstr_list = g_string_new("");
649 GString *gstr_union = g_string_new("");
650 int i;
652 for (i = 0; i < 32; i++) {
653 g_string_append_printf(gstr_list, "%s",
654 (i % 3 == 0) ? "true" : "false");
655 if (i != 31) {
656 g_string_append(gstr_list, ", ");
659 g_string_append_printf(gstr_union, "{ 'type': 'boolean', 'data': [ %s ] }",
660 gstr_list->str);
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,
677 const void *unused)
679 UserDefNativeListUnion *cvalue = NULL;
680 strList *elem = NULL;
681 Visitor *v;
682 GString *gstr_list = g_string_new("");
683 GString *gstr_union = g_string_new("");
684 int i;
686 for (i = 0; i < 32; i++) {
687 g_string_append_printf(gstr_list, "'%d'", i);
688 if (i != 31) {
689 g_string_append(gstr_list, ", ");
692 g_string_append_printf(gstr_union, "{ 'type': 'string', 'data': [ %s ] }",
693 gstr_list->str);
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++) {
701 gchar str[8];
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,
714 const void *unused)
716 UserDefNativeListUnion *cvalue = NULL;
717 numberList *elem = NULL;
718 Visitor *v;
719 GString *gstr_list = g_string_new("");
720 GString *gstr_union = g_string_new("");
721 int i;
723 for (i = 0; i < 32; i++) {
724 g_string_append_printf(gstr_list, "%f", (double)i / 3);
725 if (i != 31) {
726 g_string_append(gstr_list, ", ");
729 g_string_append_printf(gstr_union, "{ 'type': 'number', 'data': [ %s ] }",
730 gstr_list->str);
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,
764 const void *unused)
766 TestStruct *p = NULL;
767 Error *err = NULL;
768 Visitor *v;
769 strList *q = NULL;
770 UserDefTwo *r = NULL;
771 WrapAlternate *s = NULL;
773 v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', "
774 "'string': -42 }");
776 visit_type_TestStruct(v, NULL, &p, &err);
777 error_free_or_abort(&err);
778 g_assert(!p);
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);
783 assert(!q);
785 v = visitor_input_test_init(data, "{ 'str':'hi' }");
786 visit_type_UserDefTwo(v, NULL, &r, &err);
787 error_free_or_abort(&err);
788 assert(!r);
790 v = visitor_input_test_init(data, "{ }");
791 visit_type_WrapAlternate(v, NULL, &s, &err);
792 error_free_or_abort(&err);
793 assert(!s);
796 static void test_visitor_in_wrong_type(TestInputVisitorData *data,
797 const void *unused)
799 TestStruct *p = NULL;
800 Visitor *v;
801 strList *q = NULL;
802 int64_t i;
803 Error *err = 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);
810 g_assert(!p);
812 v = visitor_input_test_init(data, "{}");
813 visit_type_strList(v, NULL, &q, &err);
814 error_free_or_abort(&err);
815 assert(!q);
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);
822 g_assert(!p);
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);
833 assert(!q);
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,
841 const void *unused)
843 TestStruct *p = NULL;
844 Error *err = NULL;
845 Visitor *v;
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);
851 g_assert(!p);
854 static void test_visitor_in_fail_struct_nested(TestInputVisitorData *data,
855 const void *unused)
857 UserDefTwo *udp = NULL;
858 Error *err = NULL;
859 Visitor *v;
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);
865 g_assert(!udp);
868 static void test_visitor_in_fail_struct_in_list(TestInputVisitorData *data,
869 const void *unused)
871 UserDefOneList *head = NULL;
872 Error *err = NULL;
873 Visitor *v;
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);
879 g_assert(!head);
882 static void test_visitor_in_fail_struct_missing(TestInputVisitorData *data,
883 const void *unused)
885 Error *err = NULL;
886 Visitor *v;
887 QObject *any;
888 GenericAlternate *alt;
889 bool present;
890 int en;
891 int64_t i64;
892 uint32_t u32;
893 int8_t i8;
894 char *str;
895 double dbl;
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);
906 g_assert(!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,
933 const void *unused)
935 int64_t i64 = -1;
936 Error *err = NULL;
937 Visitor *v;
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,
962 const void *unused)
964 int64_t i64 = -1;
965 Error *err = NULL;
966 Visitor *v;
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,
986 const void *unused)
988 UserDefNativeListUnion *tmp = NULL;
989 Error *err = NULL;
990 Visitor *v;
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);
997 g_assert(!tmp);
1000 static void test_visitor_in_fail_union_flat(TestInputVisitorData *data,
1001 const void *unused)
1003 UserDefFlatUnion *tmp = NULL;
1004 Error *err = NULL;
1005 Visitor *v;
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);
1011 g_assert(!tmp);
1014 static void test_visitor_in_fail_union_flat_no_discrim(TestInputVisitorData *data,
1015 const void *unused)
1017 UserDefFlatUnion2 *tmp = NULL;
1018 Error *err = NULL;
1019 Visitor *v;
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);
1026 g_assert(!tmp);
1029 static void test_visitor_in_fail_alternate(TestInputVisitorData *data,
1030 const void *unused)
1032 UserDefAlternate *tmp;
1033 Visitor *v;
1034 Error *err = NULL;
1036 v = visitor_input_test_init(data, "3.14");
1038 visit_type_UserDefAlternate(v, NULL, &tmp, &err);
1039 error_free_or_abort(&err);
1040 g_assert(!tmp);
1043 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData *data,
1044 const char *schema_json)
1046 SchemaInfoList *schema = NULL;
1047 Visitor *v;
1049 v = visitor_input_test_init_raw(data, schema_json);
1051 visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
1052 g_assert(schema);
1054 qapi_free_SchemaInfoList(schema);
1057 static void test_visitor_in_qmp_introspect(TestInputVisitorData *data,
1058 const void *unused)
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);
1147 g_test_run();
1149 return 0;