linux-user: Support for restarting system calls for OpenRISC targets
[qemu.git] / tests / test-qmp-input-visitor.c
1 /*
2 * QMP Input Visitor unit-tests.
3 *
4 * Copyright (C) 2011-2016 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 *
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.
11 */
12
13 #include "qemu/osdep.h"
14 #include <glib.h>
15
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/qmp-input-visitor.h"
19 #include "test-qapi-types.h"
20 #include "test-qapi-visit.h"
21 #include "qapi/qmp/types.h"
22
23 typedef struct TestInputVisitorData {
24 QObject *obj;
25 QmpInputVisitor *qiv;
26 } TestInputVisitorData;
27
28 static void visitor_input_teardown(TestInputVisitorData *data,
29 const void *unused)
30 {
31 qobject_decref(data->obj);
32 data->obj = NULL;
33
34 if (data->qiv) {
35 qmp_input_visitor_cleanup(data->qiv);
36 data->qiv = NULL;
37 }
38 }
39
40 /* The various test_init functions are provided instead of a test setup
41 function so that the JSON string used by the tests are kept in the test
42 functions (and not in main()). */
43 static Visitor *visitor_input_test_init_internal(TestInputVisitorData *data,
44 const char *json_string,
45 va_list *ap)
46 {
47 Visitor *v;
48
49 visitor_input_teardown(data, NULL);
50
51 data->obj = qobject_from_jsonv(json_string, ap);
52 g_assert(data->obj);
53
54 data->qiv = qmp_input_visitor_new(data->obj, false);
55 g_assert(data->qiv);
56
57 v = qmp_input_get_visitor(data->qiv);
58 g_assert(v);
59
60 return v;
61 }
62
63 static GCC_FMT_ATTR(2, 3)
64 Visitor *visitor_input_test_init(TestInputVisitorData *data,
65 const char *json_string, ...)
66 {
67 Visitor *v;
68 va_list ap;
69
70 va_start(ap, json_string);
71 v = visitor_input_test_init_internal(data, json_string, &ap);
72 va_end(ap);
73 return v;
74 }
75
76 /* similar to visitor_input_test_init(), but does not expect a string
77 * literal/format json_string argument and so can be used for
78 * programatically generated strings (and we can't pass in programatically
79 * generated strings via %s format parameters since qobject_from_jsonv()
80 * will wrap those in double-quotes and treat the entire object as a
81 * string)
82 */
83 static Visitor *visitor_input_test_init_raw(TestInputVisitorData *data,
84 const char *json_string)
85 {
86 return visitor_input_test_init_internal(data, json_string, NULL);
87 }
88
89 static void test_visitor_in_int(TestInputVisitorData *data,
90 const void *unused)
91 {
92 int64_t res = 0, value = -42;
93 Visitor *v;
94
95 v = visitor_input_test_init(data, "%" PRId64, value);
96
97 visit_type_int(v, NULL, &res, &error_abort);
98 g_assert_cmpint(res, ==, value);
99 }
100
101 static void test_visitor_in_int_overflow(TestInputVisitorData *data,
102 const void *unused)
103 {
104 int64_t res = 0;
105 Error *err = NULL;
106 Visitor *v;
107
108 /* this will overflow a Qint/int64, so should be deserialized into
109 * a QFloat/double field instead, leading to an error if we pass it
110 * to visit_type_int. confirm this.
111 */
112 v = visitor_input_test_init(data, "%f", DBL_MAX);
113
114 visit_type_int(v, NULL, &res, &err);
115 error_free_or_abort(&err);
116 }
117
118 static void test_visitor_in_bool(TestInputVisitorData *data,
119 const void *unused)
120 {
121 bool res = false;
122 Visitor *v;
123
124 v = visitor_input_test_init(data, "true");
125
126 visit_type_bool(v, NULL, &res, &error_abort);
127 g_assert_cmpint(res, ==, true);
128 }
129
130 static void test_visitor_in_number(TestInputVisitorData *data,
131 const void *unused)
132 {
133 double res = 0, value = 3.14;
134 Visitor *v;
135
136 v = visitor_input_test_init(data, "%f", value);
137
138 visit_type_number(v, NULL, &res, &error_abort);
139 g_assert_cmpfloat(res, ==, value);
140 }
141
142 static void test_visitor_in_string(TestInputVisitorData *data,
143 const void *unused)
144 {
145 char *res = NULL, *value = (char *) "Q E M U";
146 Visitor *v;
147
148 v = visitor_input_test_init(data, "%s", value);
149
150 visit_type_str(v, NULL, &res, &error_abort);
151 g_assert_cmpstr(res, ==, value);
152
153 g_free(res);
154 }
155
156 static void test_visitor_in_enum(TestInputVisitorData *data,
157 const void *unused)
158 {
159 Visitor *v;
160 EnumOne i;
161
162 for (i = 0; EnumOne_lookup[i]; i++) {
163 EnumOne res = -1;
164
165 v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]);
166
167 visit_type_EnumOne(v, NULL, &res, &error_abort);
168 g_assert_cmpint(i, ==, res);
169 }
170 }
171
172
173 static void test_visitor_in_struct(TestInputVisitorData *data,
174 const void *unused)
175 {
176 TestStruct *p = NULL;
177 Visitor *v;
178
179 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
180
181 visit_type_TestStruct(v, NULL, &p, &error_abort);
182 g_assert_cmpint(p->integer, ==, -42);
183 g_assert(p->boolean == true);
184 g_assert_cmpstr(p->string, ==, "foo");
185
186 g_free(p->string);
187 g_free(p);
188 }
189
190 static void test_visitor_in_struct_nested(TestInputVisitorData *data,
191 const void *unused)
192 {
193 UserDefTwo *udp = NULL;
194 Visitor *v;
195
196 v = visitor_input_test_init(data, "{ 'string0': 'string0', "
197 "'dict1': { 'string1': 'string1', "
198 "'dict2': { 'userdef': { 'integer': 42, "
199 "'string': 'string' }, 'string': 'string2'}}}");
200
201 visit_type_UserDefTwo(v, NULL, &udp, &error_abort);
202
203 g_assert_cmpstr(udp->string0, ==, "string0");
204 g_assert_cmpstr(udp->dict1->string1, ==, "string1");
205 g_assert_cmpint(udp->dict1->dict2->userdef->integer, ==, 42);
206 g_assert_cmpstr(udp->dict1->dict2->userdef->string, ==, "string");
207 g_assert_cmpstr(udp->dict1->dict2->string, ==, "string2");
208 g_assert(udp->dict1->has_dict3 == false);
209
210 qapi_free_UserDefTwo(udp);
211 }
212
213 static void test_visitor_in_list(TestInputVisitorData *data,
214 const void *unused)
215 {
216 UserDefOneList *item, *head = NULL;
217 Visitor *v;
218 int i;
219
220 v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
221
222 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
223 g_assert(head != NULL);
224
225 for (i = 0, item = head; item; item = item->next, i++) {
226 char string[12];
227
228 snprintf(string, sizeof(string), "string%d", i);
229 g_assert_cmpstr(item->value->string, ==, string);
230 g_assert_cmpint(item->value->integer, ==, 42 + i);
231 }
232
233 qapi_free_UserDefOneList(head);
234 head = NULL;
235
236 /* An empty list is valid */
237 v = visitor_input_test_init(data, "[]");
238 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
239 g_assert(!head);
240 }
241
242 static void test_visitor_in_any(TestInputVisitorData *data,
243 const void *unused)
244 {
245 QObject *res = NULL;
246 Visitor *v;
247 QInt *qint;
248 QBool *qbool;
249 QString *qstring;
250 QDict *qdict;
251 QObject *qobj;
252
253 v = visitor_input_test_init(data, "-42");
254 visit_type_any(v, NULL, &res, &error_abort);
255 qint = qobject_to_qint(res);
256 g_assert(qint);
257 g_assert_cmpint(qint_get_int(qint), ==, -42);
258 qobject_decref(res);
259
260 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
261 visit_type_any(v, NULL, &res, &error_abort);
262 qdict = qobject_to_qdict(res);
263 g_assert(qdict && qdict_size(qdict) == 3);
264 qobj = qdict_get(qdict, "integer");
265 g_assert(qobj);
266 qint = qobject_to_qint(qobj);
267 g_assert(qint);
268 g_assert_cmpint(qint_get_int(qint), ==, -42);
269 qobj = qdict_get(qdict, "boolean");
270 g_assert(qobj);
271 qbool = qobject_to_qbool(qobj);
272 g_assert(qbool);
273 g_assert(qbool_get_bool(qbool) == true);
274 qobj = qdict_get(qdict, "string");
275 g_assert(qobj);
276 qstring = qobject_to_qstring(qobj);
277 g_assert(qstring);
278 g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
279 qobject_decref(res);
280 }
281
282 static void test_visitor_in_null(TestInputVisitorData *data,
283 const void *unused)
284 {
285 Visitor *v;
286 Error *err = NULL;
287 char *tmp;
288
289 /*
290 * FIXME: Since QAPI doesn't know the 'null' type yet, we can't
291 * test visit_type_null() by reading into a QAPI struct then
292 * checking that it was populated correctly. The best we can do
293 * for now is ensure that we consumed null from the input, proven
294 * by the fact that we can't re-read the key; and that we detect
295 * when input is not null.
296 */
297
298 v = visitor_input_test_init(data, "{ 'a': null, 'b': '' }");
299 visit_start_struct(v, NULL, NULL, 0, &error_abort);
300 visit_type_null(v, "a", &error_abort);
301 visit_type_str(v, "a", &tmp, &err);
302 g_assert(!tmp);
303 error_free_or_abort(&err);
304 visit_type_null(v, "b", &err);
305 error_free_or_abort(&err);
306 visit_check_struct(v, &error_abort);
307 visit_end_struct(v);
308 }
309
310 static void test_visitor_in_union_flat(TestInputVisitorData *data,
311 const void *unused)
312 {
313 Visitor *v;
314 UserDefFlatUnion *tmp;
315 UserDefUnionBase *base;
316
317 v = visitor_input_test_init(data,
318 "{ 'enum1': 'value1', "
319 "'integer': 41, "
320 "'string': 'str', "
321 "'boolean': true }");
322
323 visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort);
324 g_assert_cmpint(tmp->enum1, ==, ENUM_ONE_VALUE1);
325 g_assert_cmpstr(tmp->string, ==, "str");
326 g_assert_cmpint(tmp->integer, ==, 41);
327 g_assert_cmpint(tmp->u.value1.boolean, ==, true);
328
329 base = qapi_UserDefFlatUnion_base(tmp);
330 g_assert(&base->enum1 == &tmp->enum1);
331
332 qapi_free_UserDefFlatUnion(tmp);
333 }
334
335 static void test_visitor_in_alternate(TestInputVisitorData *data,
336 const void *unused)
337 {
338 Visitor *v;
339 Error *err = NULL;
340 UserDefAlternate *tmp;
341 WrapAlternate *wrap;
342
343 v = visitor_input_test_init(data, "42");
344 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
345 g_assert_cmpint(tmp->type, ==, QTYPE_QINT);
346 g_assert_cmpint(tmp->u.i, ==, 42);
347 qapi_free_UserDefAlternate(tmp);
348
349 v = visitor_input_test_init(data, "'string'");
350 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
351 g_assert_cmpint(tmp->type, ==, QTYPE_QSTRING);
352 g_assert_cmpstr(tmp->u.s, ==, "string");
353 qapi_free_UserDefAlternate(tmp);
354
355 v = visitor_input_test_init(data, "{'integer':1, 'string':'str', "
356 "'enum1':'value1', 'boolean':true}");
357 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
358 g_assert_cmpint(tmp->type, ==, QTYPE_QDICT);
359 g_assert_cmpint(tmp->u.udfu.integer, ==, 1);
360 g_assert_cmpstr(tmp->u.udfu.string, ==, "str");
361 g_assert_cmpint(tmp->u.udfu.enum1, ==, ENUM_ONE_VALUE1);
362 g_assert_cmpint(tmp->u.udfu.u.value1.boolean, ==, true);
363 g_assert_cmpint(tmp->u.udfu.u.value1.has_a_b, ==, false);
364 qapi_free_UserDefAlternate(tmp);
365
366 v = visitor_input_test_init(data, "false");
367 visit_type_UserDefAlternate(v, NULL, &tmp, &err);
368 error_free_or_abort(&err);
369 qapi_free_UserDefAlternate(tmp);
370
371 v = visitor_input_test_init(data, "{ 'alt': 42 }");
372 visit_type_WrapAlternate(v, NULL, &wrap, &error_abort);
373 g_assert_cmpint(wrap->alt->type, ==, QTYPE_QINT);
374 g_assert_cmpint(wrap->alt->u.i, ==, 42);
375 qapi_free_WrapAlternate(wrap);
376
377 v = visitor_input_test_init(data, "{ 'alt': 'string' }");
378 visit_type_WrapAlternate(v, NULL, &wrap, &error_abort);
379 g_assert_cmpint(wrap->alt->type, ==, QTYPE_QSTRING);
380 g_assert_cmpstr(wrap->alt->u.s, ==, "string");
381 qapi_free_WrapAlternate(wrap);
382
383 v = visitor_input_test_init(data, "{ 'alt': {'integer':1, 'string':'str', "
384 "'enum1':'value1', 'boolean':true} }");
385 visit_type_WrapAlternate(v, NULL, &wrap, &error_abort);
386 g_assert_cmpint(wrap->alt->type, ==, QTYPE_QDICT);
387 g_assert_cmpint(wrap->alt->u.udfu.integer, ==, 1);
388 g_assert_cmpstr(wrap->alt->u.udfu.string, ==, "str");
389 g_assert_cmpint(wrap->alt->u.udfu.enum1, ==, ENUM_ONE_VALUE1);
390 g_assert_cmpint(wrap->alt->u.udfu.u.value1.boolean, ==, true);
391 g_assert_cmpint(wrap->alt->u.udfu.u.value1.has_a_b, ==, false);
392 qapi_free_WrapAlternate(wrap);
393 }
394
395 static void test_visitor_in_alternate_number(TestInputVisitorData *data,
396 const void *unused)
397 {
398 Visitor *v;
399 Error *err = NULL;
400 AltStrBool *asb;
401 AltStrNum *asn;
402 AltNumStr *ans;
403 AltStrInt *asi;
404 AltIntNum *ain;
405 AltNumInt *ani;
406
407 /* Parsing an int */
408
409 v = visitor_input_test_init(data, "42");
410 visit_type_AltStrBool(v, NULL, &asb, &err);
411 error_free_or_abort(&err);
412 qapi_free_AltStrBool(asb);
413
414 v = visitor_input_test_init(data, "42");
415 visit_type_AltStrNum(v, NULL, &asn, &error_abort);
416 g_assert_cmpint(asn->type, ==, QTYPE_QFLOAT);
417 g_assert_cmpfloat(asn->u.n, ==, 42);
418 qapi_free_AltStrNum(asn);
419
420 v = visitor_input_test_init(data, "42");
421 visit_type_AltNumStr(v, NULL, &ans, &error_abort);
422 g_assert_cmpint(ans->type, ==, QTYPE_QFLOAT);
423 g_assert_cmpfloat(ans->u.n, ==, 42);
424 qapi_free_AltNumStr(ans);
425
426 v = visitor_input_test_init(data, "42");
427 visit_type_AltStrInt(v, NULL, &asi, &error_abort);
428 g_assert_cmpint(asi->type, ==, QTYPE_QINT);
429 g_assert_cmpint(asi->u.i, ==, 42);
430 qapi_free_AltStrInt(asi);
431
432 v = visitor_input_test_init(data, "42");
433 visit_type_AltIntNum(v, NULL, &ain, &error_abort);
434 g_assert_cmpint(ain->type, ==, QTYPE_QINT);
435 g_assert_cmpint(ain->u.i, ==, 42);
436 qapi_free_AltIntNum(ain);
437
438 v = visitor_input_test_init(data, "42");
439 visit_type_AltNumInt(v, NULL, &ani, &error_abort);
440 g_assert_cmpint(ani->type, ==, QTYPE_QINT);
441 g_assert_cmpint(ani->u.i, ==, 42);
442 qapi_free_AltNumInt(ani);
443
444 /* Parsing a double */
445
446 v = visitor_input_test_init(data, "42.5");
447 visit_type_AltStrBool(v, NULL, &asb, &err);
448 error_free_or_abort(&err);
449 qapi_free_AltStrBool(asb);
450
451 v = visitor_input_test_init(data, "42.5");
452 visit_type_AltStrNum(v, NULL, &asn, &error_abort);
453 g_assert_cmpint(asn->type, ==, QTYPE_QFLOAT);
454 g_assert_cmpfloat(asn->u.n, ==, 42.5);
455 qapi_free_AltStrNum(asn);
456
457 v = visitor_input_test_init(data, "42.5");
458 visit_type_AltNumStr(v, NULL, &ans, &error_abort);
459 g_assert_cmpint(ans->type, ==, QTYPE_QFLOAT);
460 g_assert_cmpfloat(ans->u.n, ==, 42.5);
461 qapi_free_AltNumStr(ans);
462
463 v = visitor_input_test_init(data, "42.5");
464 visit_type_AltStrInt(v, NULL, &asi, &err);
465 error_free_or_abort(&err);
466 qapi_free_AltStrInt(asi);
467
468 v = visitor_input_test_init(data, "42.5");
469 visit_type_AltIntNum(v, NULL, &ain, &error_abort);
470 g_assert_cmpint(ain->type, ==, QTYPE_QFLOAT);
471 g_assert_cmpfloat(ain->u.n, ==, 42.5);
472 qapi_free_AltIntNum(ain);
473
474 v = visitor_input_test_init(data, "42.5");
475 visit_type_AltNumInt(v, NULL, &ani, &error_abort);
476 g_assert_cmpint(ani->type, ==, QTYPE_QFLOAT);
477 g_assert_cmpfloat(ani->u.n, ==, 42.5);
478 qapi_free_AltNumInt(ani);
479 }
480
481 static void test_native_list_integer_helper(TestInputVisitorData *data,
482 const void *unused,
483 UserDefNativeListUnionKind kind)
484 {
485 UserDefNativeListUnion *cvalue = NULL;
486 Visitor *v;
487 GString *gstr_list = g_string_new("");
488 GString *gstr_union = g_string_new("");
489 int i;
490
491 for (i = 0; i < 32; i++) {
492 g_string_append_printf(gstr_list, "%d", i);
493 if (i != 31) {
494 g_string_append(gstr_list, ", ");
495 }
496 }
497 g_string_append_printf(gstr_union, "{ 'type': '%s', 'data': [ %s ] }",
498 UserDefNativeListUnionKind_lookup[kind],
499 gstr_list->str);
500 v = visitor_input_test_init_raw(data, gstr_union->str);
501
502 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
503 g_assert(cvalue != NULL);
504 g_assert_cmpint(cvalue->type, ==, kind);
505
506 switch (kind) {
507 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
508 intList *elem = NULL;
509 for (i = 0, elem = cvalue->u.integer.data;
510 elem; elem = elem->next, i++) {
511 g_assert_cmpint(elem->value, ==, i);
512 }
513 break;
514 }
515 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
516 int8List *elem = NULL;
517 for (i = 0, elem = cvalue->u.s8.data; elem; elem = elem->next, i++) {
518 g_assert_cmpint(elem->value, ==, i);
519 }
520 break;
521 }
522 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
523 int16List *elem = NULL;
524 for (i = 0, elem = cvalue->u.s16.data; elem; elem = elem->next, i++) {
525 g_assert_cmpint(elem->value, ==, i);
526 }
527 break;
528 }
529 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
530 int32List *elem = NULL;
531 for (i = 0, elem = cvalue->u.s32.data; elem; elem = elem->next, i++) {
532 g_assert_cmpint(elem->value, ==, i);
533 }
534 break;
535 }
536 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
537 int64List *elem = NULL;
538 for (i = 0, elem = cvalue->u.s64.data; elem; elem = elem->next, i++) {
539 g_assert_cmpint(elem->value, ==, i);
540 }
541 break;
542 }
543 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
544 uint8List *elem = NULL;
545 for (i = 0, elem = cvalue->u.u8.data; elem; elem = elem->next, i++) {
546 g_assert_cmpint(elem->value, ==, i);
547 }
548 break;
549 }
550 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
551 uint16List *elem = NULL;
552 for (i = 0, elem = cvalue->u.u16.data; elem; elem = elem->next, i++) {
553 g_assert_cmpint(elem->value, ==, i);
554 }
555 break;
556 }
557 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
558 uint32List *elem = NULL;
559 for (i = 0, elem = cvalue->u.u32.data; elem; elem = elem->next, i++) {
560 g_assert_cmpint(elem->value, ==, i);
561 }
562 break;
563 }
564 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
565 uint64List *elem = NULL;
566 for (i = 0, elem = cvalue->u.u64.data; elem; elem = elem->next, i++) {
567 g_assert_cmpint(elem->value, ==, i);
568 }
569 break;
570 }
571 default:
572 g_assert_not_reached();
573 }
574
575 g_string_free(gstr_union, true);
576 g_string_free(gstr_list, true);
577 qapi_free_UserDefNativeListUnion(cvalue);
578 }
579
580 static void test_visitor_in_native_list_int(TestInputVisitorData *data,
581 const void *unused)
582 {
583 test_native_list_integer_helper(data, unused,
584 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
585 }
586
587 static void test_visitor_in_native_list_int8(TestInputVisitorData *data,
588 const void *unused)
589 {
590 test_native_list_integer_helper(data, unused,
591 USER_DEF_NATIVE_LIST_UNION_KIND_S8);
592 }
593
594 static void test_visitor_in_native_list_int16(TestInputVisitorData *data,
595 const void *unused)
596 {
597 test_native_list_integer_helper(data, unused,
598 USER_DEF_NATIVE_LIST_UNION_KIND_S16);
599 }
600
601 static void test_visitor_in_native_list_int32(TestInputVisitorData *data,
602 const void *unused)
603 {
604 test_native_list_integer_helper(data, unused,
605 USER_DEF_NATIVE_LIST_UNION_KIND_S32);
606 }
607
608 static void test_visitor_in_native_list_int64(TestInputVisitorData *data,
609 const void *unused)
610 {
611 test_native_list_integer_helper(data, unused,
612 USER_DEF_NATIVE_LIST_UNION_KIND_S64);
613 }
614
615 static void test_visitor_in_native_list_uint8(TestInputVisitorData *data,
616 const void *unused)
617 {
618 test_native_list_integer_helper(data, unused,
619 USER_DEF_NATIVE_LIST_UNION_KIND_U8);
620 }
621
622 static void test_visitor_in_native_list_uint16(TestInputVisitorData *data,
623 const void *unused)
624 {
625 test_native_list_integer_helper(data, unused,
626 USER_DEF_NATIVE_LIST_UNION_KIND_U16);
627 }
628
629 static void test_visitor_in_native_list_uint32(TestInputVisitorData *data,
630 const void *unused)
631 {
632 test_native_list_integer_helper(data, unused,
633 USER_DEF_NATIVE_LIST_UNION_KIND_U32);
634 }
635
636 static void test_visitor_in_native_list_uint64(TestInputVisitorData *data,
637 const void *unused)
638 {
639 test_native_list_integer_helper(data, unused,
640 USER_DEF_NATIVE_LIST_UNION_KIND_U64);
641 }
642
643 static void test_visitor_in_native_list_bool(TestInputVisitorData *data,
644 const void *unused)
645 {
646 UserDefNativeListUnion *cvalue = NULL;
647 boolList *elem = NULL;
648 Visitor *v;
649 GString *gstr_list = g_string_new("");
650 GString *gstr_union = g_string_new("");
651 int i;
652
653 for (i = 0; i < 32; i++) {
654 g_string_append_printf(gstr_list, "%s",
655 (i % 3 == 0) ? "true" : "false");
656 if (i != 31) {
657 g_string_append(gstr_list, ", ");
658 }
659 }
660 g_string_append_printf(gstr_union, "{ 'type': 'boolean', 'data': [ %s ] }",
661 gstr_list->str);
662 v = visitor_input_test_init_raw(data, gstr_union->str);
663
664 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
665 g_assert(cvalue != NULL);
666 g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
667
668 for (i = 0, elem = cvalue->u.boolean.data; elem; elem = elem->next, i++) {
669 g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0);
670 }
671
672 g_string_free(gstr_union, true);
673 g_string_free(gstr_list, true);
674 qapi_free_UserDefNativeListUnion(cvalue);
675 }
676
677 static void test_visitor_in_native_list_string(TestInputVisitorData *data,
678 const void *unused)
679 {
680 UserDefNativeListUnion *cvalue = NULL;
681 strList *elem = NULL;
682 Visitor *v;
683 GString *gstr_list = g_string_new("");
684 GString *gstr_union = g_string_new("");
685 int i;
686
687 for (i = 0; i < 32; i++) {
688 g_string_append_printf(gstr_list, "'%d'", i);
689 if (i != 31) {
690 g_string_append(gstr_list, ", ");
691 }
692 }
693 g_string_append_printf(gstr_union, "{ 'type': 'string', 'data': [ %s ] }",
694 gstr_list->str);
695 v = visitor_input_test_init_raw(data, gstr_union->str);
696
697 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
698 g_assert(cvalue != NULL);
699 g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
700
701 for (i = 0, elem = cvalue->u.string.data; elem; elem = elem->next, i++) {
702 gchar str[8];
703 sprintf(str, "%d", i);
704 g_assert_cmpstr(elem->value, ==, str);
705 }
706
707 g_string_free(gstr_union, true);
708 g_string_free(gstr_list, true);
709 qapi_free_UserDefNativeListUnion(cvalue);
710 }
711
712 #define DOUBLE_STR_MAX 16
713
714 static void test_visitor_in_native_list_number(TestInputVisitorData *data,
715 const void *unused)
716 {
717 UserDefNativeListUnion *cvalue = NULL;
718 numberList *elem = NULL;
719 Visitor *v;
720 GString *gstr_list = g_string_new("");
721 GString *gstr_union = g_string_new("");
722 int i;
723
724 for (i = 0; i < 32; i++) {
725 g_string_append_printf(gstr_list, "%f", (double)i / 3);
726 if (i != 31) {
727 g_string_append(gstr_list, ", ");
728 }
729 }
730 g_string_append_printf(gstr_union, "{ 'type': 'number', 'data': [ %s ] }",
731 gstr_list->str);
732 v = visitor_input_test_init_raw(data, gstr_union->str);
733
734 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
735 g_assert(cvalue != NULL);
736 g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
737
738 for (i = 0, elem = cvalue->u.number.data; elem; elem = elem->next, i++) {
739 GString *double_expected = g_string_new("");
740 GString *double_actual = g_string_new("");
741
742 g_string_printf(double_expected, "%.6f", (double)i / 3);
743 g_string_printf(double_actual, "%.6f", elem->value);
744 g_assert_cmpstr(double_expected->str, ==, double_actual->str);
745
746 g_string_free(double_expected, true);
747 g_string_free(double_actual, true);
748 }
749
750 g_string_free(gstr_union, true);
751 g_string_free(gstr_list, true);
752 qapi_free_UserDefNativeListUnion(cvalue);
753 }
754
755 static void input_visitor_test_add(const char *testpath,
756 TestInputVisitorData *data,
757 void (*test_func)(TestInputVisitorData *data, const void *user_data))
758 {
759 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
760 visitor_input_teardown);
761 }
762
763 static void test_visitor_in_errors(TestInputVisitorData *data,
764 const void *unused)
765 {
766 TestStruct *p = NULL;
767 Error *err = NULL;
768 Visitor *v;
769 strList *q = NULL;
770
771 v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', "
772 "'string': -42 }");
773
774 visit_type_TestStruct(v, NULL, &p, &err);
775 error_free_or_abort(&err);
776 g_assert(!p);
777
778 v = visitor_input_test_init(data, "[ '1', '2', false, '3' ]");
779 visit_type_strList(v, NULL, &q, &err);
780 error_free_or_abort(&err);
781 assert(!q);
782 }
783
784 static void test_visitor_in_wrong_type(TestInputVisitorData *data,
785 const void *unused)
786 {
787 TestStruct *p = NULL;
788 Visitor *v;
789 strList *q = NULL;
790 int64_t i;
791 Error *err = NULL;
792
793 /* Make sure arrays and structs cannot be confused */
794
795 v = visitor_input_test_init(data, "[]");
796 visit_type_TestStruct(v, NULL, &p, &err);
797 error_free_or_abort(&err);
798 g_assert(!p);
799
800 v = visitor_input_test_init(data, "{}");
801 visit_type_strList(v, NULL, &q, &err);
802 error_free_or_abort(&err);
803 assert(!q);
804
805 /* Make sure primitives and struct cannot be confused */
806
807 v = visitor_input_test_init(data, "1");
808 visit_type_TestStruct(v, NULL, &p, &err);
809 error_free_or_abort(&err);
810 g_assert(!p);
811
812 v = visitor_input_test_init(data, "{}");
813 visit_type_int(v, NULL, &i, &err);
814 error_free_or_abort(&err);
815
816 /* Make sure primitives and arrays cannot be confused */
817
818 v = visitor_input_test_init(data, "1");
819 visit_type_strList(v, NULL, &q, &err);
820 error_free_or_abort(&err);
821 assert(!q);
822
823 v = visitor_input_test_init(data, "[]");
824 visit_type_int(v, NULL, &i, &err);
825 error_free_or_abort(&err);
826 }
827
828 int main(int argc, char **argv)
829 {
830 TestInputVisitorData in_visitor_data;
831
832 g_test_init(&argc, &argv, NULL);
833
834 input_visitor_test_add("/visitor/input/int",
835 &in_visitor_data, test_visitor_in_int);
836 input_visitor_test_add("/visitor/input/int_overflow",
837 &in_visitor_data, test_visitor_in_int_overflow);
838 input_visitor_test_add("/visitor/input/bool",
839 &in_visitor_data, test_visitor_in_bool);
840 input_visitor_test_add("/visitor/input/number",
841 &in_visitor_data, test_visitor_in_number);
842 input_visitor_test_add("/visitor/input/string",
843 &in_visitor_data, test_visitor_in_string);
844 input_visitor_test_add("/visitor/input/enum",
845 &in_visitor_data, test_visitor_in_enum);
846 input_visitor_test_add("/visitor/input/struct",
847 &in_visitor_data, test_visitor_in_struct);
848 input_visitor_test_add("/visitor/input/struct-nested",
849 &in_visitor_data, test_visitor_in_struct_nested);
850 input_visitor_test_add("/visitor/input/list",
851 &in_visitor_data, test_visitor_in_list);
852 input_visitor_test_add("/visitor/input/any",
853 &in_visitor_data, test_visitor_in_any);
854 input_visitor_test_add("/visitor/input/null",
855 &in_visitor_data, test_visitor_in_null);
856 input_visitor_test_add("/visitor/input/union-flat",
857 &in_visitor_data, test_visitor_in_union_flat);
858 input_visitor_test_add("/visitor/input/alternate",
859 &in_visitor_data, test_visitor_in_alternate);
860 input_visitor_test_add("/visitor/input/errors",
861 &in_visitor_data, test_visitor_in_errors);
862 input_visitor_test_add("/visitor/input/wrong-type",
863 &in_visitor_data, test_visitor_in_wrong_type);
864 input_visitor_test_add("/visitor/input/alternate-number",
865 &in_visitor_data, test_visitor_in_alternate_number);
866 input_visitor_test_add("/visitor/input/native_list/int",
867 &in_visitor_data,
868 test_visitor_in_native_list_int);
869 input_visitor_test_add("/visitor/input/native_list/int8",
870 &in_visitor_data,
871 test_visitor_in_native_list_int8);
872 input_visitor_test_add("/visitor/input/native_list/int16",
873 &in_visitor_data,
874 test_visitor_in_native_list_int16);
875 input_visitor_test_add("/visitor/input/native_list/int32",
876 &in_visitor_data,
877 test_visitor_in_native_list_int32);
878 input_visitor_test_add("/visitor/input/native_list/int64",
879 &in_visitor_data,
880 test_visitor_in_native_list_int64);
881 input_visitor_test_add("/visitor/input/native_list/uint8",
882 &in_visitor_data,
883 test_visitor_in_native_list_uint8);
884 input_visitor_test_add("/visitor/input/native_list/uint16",
885 &in_visitor_data,
886 test_visitor_in_native_list_uint16);
887 input_visitor_test_add("/visitor/input/native_list/uint32",
888 &in_visitor_data,
889 test_visitor_in_native_list_uint32);
890 input_visitor_test_add("/visitor/input/native_list/uint64",
891 &in_visitor_data,
892 test_visitor_in_native_list_uint64);
893 input_visitor_test_add("/visitor/input/native_list/bool",
894 &in_visitor_data, test_visitor_in_native_list_bool);
895 input_visitor_test_add("/visitor/input/native_list/str",
896 &in_visitor_data,
897 test_visitor_in_native_list_string);
898 input_visitor_test_add("/visitor/input/native_list/number",
899 &in_visitor_data,
900 test_visitor_in_native_list_number);
901
902 g_test_run();
903
904 return 0;
905 }