trap signals for "-serial mon:stdio"
[qemu.git] / tests / test-qmp-input-visitor.c
1 /*
2 * QMP Input Visitor unit-tests.
3 *
4 * Copyright (C) 2011 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 <glib.h>
14 #include <stdarg.h>
15
16 #include "qemu-common.h"
17 #include "qapi/qmp-input-visitor.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "qapi/qmp/types.h"
21
22 typedef struct TestInputVisitorData {
23 QObject *obj;
24 QmpInputVisitor *qiv;
25 } TestInputVisitorData;
26
27 static void visitor_input_teardown(TestInputVisitorData *data,
28 const void *unused)
29 {
30 qobject_decref(data->obj);
31 data->obj = NULL;
32
33 if (data->qiv) {
34 qmp_input_visitor_cleanup(data->qiv);
35 data->qiv = NULL;
36 }
37 }
38
39 /* This is provided instead of a test setup function so that the JSON
40 string used by the tests are kept in the test functions (and not
41 int main()) */
42 static GCC_FMT_ATTR(2, 3)
43 Visitor *visitor_input_test_init(TestInputVisitorData *data,
44 const char *json_string, ...)
45 {
46 Visitor *v;
47 va_list ap;
48
49 va_start(ap, json_string);
50 data->obj = qobject_from_jsonv(json_string, &ap);
51 va_end(ap);
52
53 g_assert(data->obj != NULL);
54
55 data->qiv = qmp_input_visitor_new(data->obj);
56 g_assert(data->qiv != NULL);
57
58 v = qmp_input_get_visitor(data->qiv);
59 g_assert(v != NULL);
60
61 return v;
62 }
63
64 /* similar to visitor_input_test_init(), but does not expect a string
65 * literal/format json_string argument and so can be used for
66 * programatically generated strings (and we can't pass in programatically
67 * generated strings via %s format parameters since qobject_from_jsonv()
68 * will wrap those in double-quotes and treat the entire object as a
69 * string)
70 */
71 static Visitor *visitor_input_test_init_raw(TestInputVisitorData *data,
72 const char *json_string)
73 {
74 Visitor *v;
75
76 data->obj = qobject_from_json(json_string);
77
78 g_assert(data->obj != NULL);
79
80 data->qiv = qmp_input_visitor_new(data->obj);
81 g_assert(data->qiv != NULL);
82
83 v = qmp_input_get_visitor(data->qiv);
84 g_assert(v != NULL);
85
86 return v;
87 }
88
89 static void test_visitor_in_int(TestInputVisitorData *data,
90 const void *unused)
91 {
92 int64_t res = 0, value = -42;
93 Error *errp = NULL;
94 Visitor *v;
95
96 v = visitor_input_test_init(data, "%" PRId64, value);
97
98 visit_type_int(v, &res, NULL, &errp);
99 g_assert(!error_is_set(&errp));
100 g_assert_cmpint(res, ==, value);
101 }
102
103 static void test_visitor_in_int_overflow(TestInputVisitorData *data,
104 const void *unused)
105 {
106 int64_t res = 0;
107 Error *errp = NULL;
108 Visitor *v;
109
110 /* this will overflow a Qint/int64, so should be deserialized into
111 * a QFloat/double field instead, leading to an error if we pass it
112 * to visit_type_int. confirm this.
113 */
114 v = visitor_input_test_init(data, "%f", DBL_MAX);
115
116 visit_type_int(v, &res, NULL, &errp);
117 g_assert(error_is_set(&errp));
118 error_free(errp);
119 }
120
121 static void test_visitor_in_bool(TestInputVisitorData *data,
122 const void *unused)
123 {
124 Error *errp = NULL;
125 bool res = false;
126 Visitor *v;
127
128 v = visitor_input_test_init(data, "true");
129
130 visit_type_bool(v, &res, NULL, &errp);
131 g_assert(!error_is_set(&errp));
132 g_assert_cmpint(res, ==, true);
133 }
134
135 static void test_visitor_in_number(TestInputVisitorData *data,
136 const void *unused)
137 {
138 double res = 0, value = 3.14;
139 Error *errp = NULL;
140 Visitor *v;
141
142 v = visitor_input_test_init(data, "%f", value);
143
144 visit_type_number(v, &res, NULL, &errp);
145 g_assert(!error_is_set(&errp));
146 g_assert_cmpfloat(res, ==, value);
147 }
148
149 static void test_visitor_in_string(TestInputVisitorData *data,
150 const void *unused)
151 {
152 char *res = NULL, *value = (char *) "Q E M U";
153 Error *errp = NULL;
154 Visitor *v;
155
156 v = visitor_input_test_init(data, "%s", value);
157
158 visit_type_str(v, &res, NULL, &errp);
159 g_assert(!error_is_set(&errp));
160 g_assert_cmpstr(res, ==, value);
161
162 g_free(res);
163 }
164
165 static void test_visitor_in_enum(TestInputVisitorData *data,
166 const void *unused)
167 {
168 Error *errp = NULL;
169 Visitor *v;
170 EnumOne i;
171
172 for (i = 0; EnumOne_lookup[i]; i++) {
173 EnumOne res = -1;
174
175 v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]);
176
177 visit_type_EnumOne(v, &res, NULL, &errp);
178 g_assert(!error_is_set(&errp));
179 g_assert_cmpint(i, ==, res);
180
181 visitor_input_teardown(data, NULL);
182 }
183
184 data->obj = NULL;
185 data->qiv = NULL;
186 }
187
188 typedef struct TestStruct
189 {
190 int64_t integer;
191 bool boolean;
192 char *string;
193 } TestStruct;
194
195 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
196 const char *name, Error **errp)
197 {
198 Error *err = NULL;
199 if (!error_is_set(errp)) {
200 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
201 &err);
202 if (!err) {
203 visit_type_int(v, &(*obj)->integer, "integer", &err);
204 visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
205 visit_type_str(v, &(*obj)->string, "string", &err);
206
207 /* Always call end_struct if start_struct succeeded. */
208 error_propagate(errp, err);
209 err = NULL;
210 visit_end_struct(v, &err);
211 }
212 error_propagate(errp, err);
213 }
214 }
215
216 static void test_visitor_in_struct(TestInputVisitorData *data,
217 const void *unused)
218 {
219 TestStruct *p = NULL;
220 Error *errp = NULL;
221 Visitor *v;
222
223 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
224
225 visit_type_TestStruct(v, &p, NULL, &errp);
226 g_assert(!error_is_set(&errp));
227 g_assert_cmpint(p->integer, ==, -42);
228 g_assert(p->boolean == true);
229 g_assert_cmpstr(p->string, ==, "foo");
230
231 g_free(p->string);
232 g_free(p);
233 }
234
235 static void check_and_free_str(char *str, const char *cmp)
236 {
237 g_assert_cmpstr(str, ==, cmp);
238 g_free(str);
239 }
240
241 static void test_visitor_in_struct_nested(TestInputVisitorData *data,
242 const void *unused)
243 {
244 UserDefNested *udp = NULL;
245 Error *errp = NULL;
246 Visitor *v;
247
248 v = visitor_input_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
249
250 visit_type_UserDefNested(v, &udp, NULL, &errp);
251 g_assert(!error_is_set(&errp));
252
253 check_and_free_str(udp->string0, "string0");
254 check_and_free_str(udp->dict1.string1, "string1");
255 g_assert_cmpint(udp->dict1.dict2.userdef1->integer, ==, 42);
256 check_and_free_str(udp->dict1.dict2.userdef1->string, "string");
257 check_and_free_str(udp->dict1.dict2.string2, "string2");
258 g_assert(udp->dict1.has_dict3 == false);
259
260 g_free(udp->dict1.dict2.userdef1);
261 g_free(udp);
262 }
263
264 static void test_visitor_in_list(TestInputVisitorData *data,
265 const void *unused)
266 {
267 UserDefOneList *item, *head = NULL;
268 Error *errp = NULL;
269 Visitor *v;
270 int i;
271
272 v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
273
274 visit_type_UserDefOneList(v, &head, NULL, &errp);
275 g_assert(!error_is_set(&errp));
276 g_assert(head != NULL);
277
278 for (i = 0, item = head; item; item = item->next, i++) {
279 char string[12];
280
281 snprintf(string, sizeof(string), "string%d", i);
282 g_assert_cmpstr(item->value->string, ==, string);
283 g_assert_cmpint(item->value->integer, ==, 42 + i);
284 }
285
286 qapi_free_UserDefOneList(head);
287 }
288
289 static void test_visitor_in_union(TestInputVisitorData *data,
290 const void *unused)
291 {
292 Visitor *v;
293 Error *err = NULL;
294 UserDefUnion *tmp;
295
296 v = visitor_input_test_init(data, "{ 'type': 'b', 'data' : { 'integer': 42 } }");
297
298 visit_type_UserDefUnion(v, &tmp, NULL, &err);
299 g_assert(err == NULL);
300 g_assert_cmpint(tmp->kind, ==, USER_DEF_UNION_KIND_B);
301 g_assert_cmpint(tmp->b->integer, ==, 42);
302 qapi_free_UserDefUnion(tmp);
303 }
304
305 static void test_native_list_integer_helper(TestInputVisitorData *data,
306 const void *unused,
307 UserDefNativeListUnionKind kind)
308 {
309 UserDefNativeListUnion *cvalue = NULL;
310 Error *err = NULL;
311 Visitor *v;
312 GString *gstr_list = g_string_new("");
313 GString *gstr_union = g_string_new("");
314 int i;
315
316 for (i = 0; i < 32; i++) {
317 g_string_append_printf(gstr_list, "%d", i);
318 if (i != 31) {
319 g_string_append(gstr_list, ", ");
320 }
321 }
322 g_string_append_printf(gstr_union, "{ 'type': '%s', 'data': [ %s ] }",
323 UserDefNativeListUnionKind_lookup[kind],
324 gstr_list->str);
325 v = visitor_input_test_init_raw(data, gstr_union->str);
326
327 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err);
328 g_assert(err == NULL);
329 g_assert(cvalue != NULL);
330 g_assert_cmpint(cvalue->kind, ==, kind);
331
332 switch (kind) {
333 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
334 intList *elem = NULL;
335 for (i = 0, elem = cvalue->integer; elem; elem = elem->next, i++) {
336 g_assert_cmpint(elem->value, ==, i);
337 }
338 break;
339 }
340 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
341 int8List *elem = NULL;
342 for (i = 0, elem = cvalue->s8; elem; elem = elem->next, i++) {
343 g_assert_cmpint(elem->value, ==, i);
344 }
345 break;
346 }
347 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
348 int16List *elem = NULL;
349 for (i = 0, elem = cvalue->s16; elem; elem = elem->next, i++) {
350 g_assert_cmpint(elem->value, ==, i);
351 }
352 break;
353 }
354 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
355 int32List *elem = NULL;
356 for (i = 0, elem = cvalue->s32; elem; elem = elem->next, i++) {
357 g_assert_cmpint(elem->value, ==, i);
358 }
359 break;
360 }
361 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
362 int64List *elem = NULL;
363 for (i = 0, elem = cvalue->s64; elem; elem = elem->next, i++) {
364 g_assert_cmpint(elem->value, ==, i);
365 }
366 break;
367 }
368 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
369 uint8List *elem = NULL;
370 for (i = 0, elem = cvalue->u8; elem; elem = elem->next, i++) {
371 g_assert_cmpint(elem->value, ==, i);
372 }
373 break;
374 }
375 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
376 uint16List *elem = NULL;
377 for (i = 0, elem = cvalue->u16; elem; elem = elem->next, i++) {
378 g_assert_cmpint(elem->value, ==, i);
379 }
380 break;
381 }
382 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
383 uint32List *elem = NULL;
384 for (i = 0, elem = cvalue->u32; elem; elem = elem->next, i++) {
385 g_assert_cmpint(elem->value, ==, i);
386 }
387 break;
388 }
389 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
390 uint64List *elem = NULL;
391 for (i = 0, elem = cvalue->u64; elem; elem = elem->next, i++) {
392 g_assert_cmpint(elem->value, ==, i);
393 }
394 break;
395 }
396 default:
397 g_assert(false);
398 }
399
400 g_string_free(gstr_union, true);
401 g_string_free(gstr_list, true);
402 qapi_free_UserDefNativeListUnion(cvalue);
403 }
404
405 static void test_visitor_in_native_list_int(TestInputVisitorData *data,
406 const void *unused)
407 {
408 test_native_list_integer_helper(data, unused,
409 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
410 }
411
412 static void test_visitor_in_native_list_int8(TestInputVisitorData *data,
413 const void *unused)
414 {
415 test_native_list_integer_helper(data, unused,
416 USER_DEF_NATIVE_LIST_UNION_KIND_S8);
417 }
418
419 static void test_visitor_in_native_list_int16(TestInputVisitorData *data,
420 const void *unused)
421 {
422 test_native_list_integer_helper(data, unused,
423 USER_DEF_NATIVE_LIST_UNION_KIND_S16);
424 }
425
426 static void test_visitor_in_native_list_int32(TestInputVisitorData *data,
427 const void *unused)
428 {
429 test_native_list_integer_helper(data, unused,
430 USER_DEF_NATIVE_LIST_UNION_KIND_S32);
431 }
432
433 static void test_visitor_in_native_list_int64(TestInputVisitorData *data,
434 const void *unused)
435 {
436 test_native_list_integer_helper(data, unused,
437 USER_DEF_NATIVE_LIST_UNION_KIND_S64);
438 }
439
440 static void test_visitor_in_native_list_uint8(TestInputVisitorData *data,
441 const void *unused)
442 {
443 test_native_list_integer_helper(data, unused,
444 USER_DEF_NATIVE_LIST_UNION_KIND_U8);
445 }
446
447 static void test_visitor_in_native_list_uint16(TestInputVisitorData *data,
448 const void *unused)
449 {
450 test_native_list_integer_helper(data, unused,
451 USER_DEF_NATIVE_LIST_UNION_KIND_U16);
452 }
453
454 static void test_visitor_in_native_list_uint32(TestInputVisitorData *data,
455 const void *unused)
456 {
457 test_native_list_integer_helper(data, unused,
458 USER_DEF_NATIVE_LIST_UNION_KIND_U32);
459 }
460
461 static void test_visitor_in_native_list_uint64(TestInputVisitorData *data,
462 const void *unused)
463 {
464 test_native_list_integer_helper(data, unused,
465 USER_DEF_NATIVE_LIST_UNION_KIND_U64);
466 }
467
468 static void test_visitor_in_native_list_bool(TestInputVisitorData *data,
469 const void *unused)
470 {
471 UserDefNativeListUnion *cvalue = NULL;
472 boolList *elem = NULL;
473 Error *err = NULL;
474 Visitor *v;
475 GString *gstr_list = g_string_new("");
476 GString *gstr_union = g_string_new("");
477 int i;
478
479 for (i = 0; i < 32; i++) {
480 g_string_append_printf(gstr_list, "%s",
481 (i % 3 == 0) ? "true" : "false");
482 if (i != 31) {
483 g_string_append(gstr_list, ", ");
484 }
485 }
486 g_string_append_printf(gstr_union, "{ 'type': 'boolean', 'data': [ %s ] }",
487 gstr_list->str);
488 v = visitor_input_test_init_raw(data, gstr_union->str);
489
490 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err);
491 g_assert(err == NULL);
492 g_assert(cvalue != NULL);
493 g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
494
495 for (i = 0, elem = cvalue->boolean; elem; elem = elem->next, i++) {
496 g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0);
497 }
498
499 g_string_free(gstr_union, true);
500 g_string_free(gstr_list, true);
501 qapi_free_UserDefNativeListUnion(cvalue);
502 }
503
504 static void test_visitor_in_native_list_string(TestInputVisitorData *data,
505 const void *unused)
506 {
507 UserDefNativeListUnion *cvalue = NULL;
508 strList *elem = NULL;
509 Error *err = NULL;
510 Visitor *v;
511 GString *gstr_list = g_string_new("");
512 GString *gstr_union = g_string_new("");
513 int i;
514
515 for (i = 0; i < 32; i++) {
516 g_string_append_printf(gstr_list, "'%d'", i);
517 if (i != 31) {
518 g_string_append(gstr_list, ", ");
519 }
520 }
521 g_string_append_printf(gstr_union, "{ 'type': 'string', 'data': [ %s ] }",
522 gstr_list->str);
523 v = visitor_input_test_init_raw(data, gstr_union->str);
524
525 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err);
526 g_assert(err == NULL);
527 g_assert(cvalue != NULL);
528 g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
529
530 for (i = 0, elem = cvalue->string; elem; elem = elem->next, i++) {
531 gchar str[8];
532 sprintf(str, "%d", i);
533 g_assert_cmpstr(elem->value, ==, str);
534 }
535
536 g_string_free(gstr_union, true);
537 g_string_free(gstr_list, true);
538 qapi_free_UserDefNativeListUnion(cvalue);
539 }
540
541 #define DOUBLE_STR_MAX 16
542
543 static void test_visitor_in_native_list_number(TestInputVisitorData *data,
544 const void *unused)
545 {
546 UserDefNativeListUnion *cvalue = NULL;
547 numberList *elem = NULL;
548 Error *err = NULL;
549 Visitor *v;
550 GString *gstr_list = g_string_new("");
551 GString *gstr_union = g_string_new("");
552 int i;
553
554 for (i = 0; i < 32; i++) {
555 g_string_append_printf(gstr_list, "%f", (double)i / 3);
556 if (i != 31) {
557 g_string_append(gstr_list, ", ");
558 }
559 }
560 g_string_append_printf(gstr_union, "{ 'type': 'number', 'data': [ %s ] }",
561 gstr_list->str);
562 v = visitor_input_test_init_raw(data, gstr_union->str);
563
564 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err);
565 g_assert(err == NULL);
566 g_assert(cvalue != NULL);
567 g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
568
569 for (i = 0, elem = cvalue->number; elem; elem = elem->next, i++) {
570 GString *double_expected = g_string_new("");
571 GString *double_actual = g_string_new("");
572
573 g_string_printf(double_expected, "%.6f", (double)i / 3);
574 g_string_printf(double_actual, "%.6f", elem->value);
575 g_assert_cmpstr(double_expected->str, ==, double_actual->str);
576
577 g_string_free(double_expected, true);
578 g_string_free(double_actual, true);
579 }
580
581 g_string_free(gstr_union, true);
582 g_string_free(gstr_list, true);
583 qapi_free_UserDefNativeListUnion(cvalue);
584 }
585
586 static void input_visitor_test_add(const char *testpath,
587 TestInputVisitorData *data,
588 void (*test_func)(TestInputVisitorData *data, const void *user_data))
589 {
590 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
591 visitor_input_teardown);
592 }
593
594 static void test_visitor_in_errors(TestInputVisitorData *data,
595 const void *unused)
596 {
597 TestStruct *p = NULL;
598 Error *errp = NULL;
599 Visitor *v;
600
601 v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }");
602
603 visit_type_TestStruct(v, &p, NULL, &errp);
604 g_assert(error_is_set(&errp));
605 g_assert(p->string == NULL);
606
607 g_free(p->string);
608 g_free(p);
609 }
610
611 int main(int argc, char **argv)
612 {
613 TestInputVisitorData in_visitor_data;
614
615 g_test_init(&argc, &argv, NULL);
616
617 input_visitor_test_add("/visitor/input/int",
618 &in_visitor_data, test_visitor_in_int);
619 input_visitor_test_add("/visitor/input/int_overflow",
620 &in_visitor_data, test_visitor_in_int_overflow);
621 input_visitor_test_add("/visitor/input/bool",
622 &in_visitor_data, test_visitor_in_bool);
623 input_visitor_test_add("/visitor/input/number",
624 &in_visitor_data, test_visitor_in_number);
625 input_visitor_test_add("/visitor/input/string",
626 &in_visitor_data, test_visitor_in_string);
627 input_visitor_test_add("/visitor/input/enum",
628 &in_visitor_data, test_visitor_in_enum);
629 input_visitor_test_add("/visitor/input/struct",
630 &in_visitor_data, test_visitor_in_struct);
631 input_visitor_test_add("/visitor/input/struct-nested",
632 &in_visitor_data, test_visitor_in_struct_nested);
633 input_visitor_test_add("/visitor/input/list",
634 &in_visitor_data, test_visitor_in_list);
635 input_visitor_test_add("/visitor/input/union",
636 &in_visitor_data, test_visitor_in_union);
637 input_visitor_test_add("/visitor/input/errors",
638 &in_visitor_data, test_visitor_in_errors);
639 input_visitor_test_add("/visitor/input/native_list/int",
640 &in_visitor_data,
641 test_visitor_in_native_list_int);
642 input_visitor_test_add("/visitor/input/native_list/int8",
643 &in_visitor_data,
644 test_visitor_in_native_list_int8);
645 input_visitor_test_add("/visitor/input/native_list/int16",
646 &in_visitor_data,
647 test_visitor_in_native_list_int16);
648 input_visitor_test_add("/visitor/input/native_list/int32",
649 &in_visitor_data,
650 test_visitor_in_native_list_int32);
651 input_visitor_test_add("/visitor/input/native_list/int64",
652 &in_visitor_data,
653 test_visitor_in_native_list_int64);
654 input_visitor_test_add("/visitor/input/native_list/uint8",
655 &in_visitor_data,
656 test_visitor_in_native_list_uint8);
657 input_visitor_test_add("/visitor/input/native_list/uint16",
658 &in_visitor_data,
659 test_visitor_in_native_list_uint16);
660 input_visitor_test_add("/visitor/input/native_list/uint32",
661 &in_visitor_data,
662 test_visitor_in_native_list_uint32);
663 input_visitor_test_add("/visitor/input/native_list/uint64",
664 &in_visitor_data, test_visitor_in_native_list_uint64);
665 input_visitor_test_add("/visitor/input/native_list/bool",
666 &in_visitor_data, test_visitor_in_native_list_bool);
667 input_visitor_test_add("/visitor/input/native_list/str",
668 &in_visitor_data, test_visitor_in_native_list_string);
669 input_visitor_test_add("/visitor/input/native_list/number",
670 &in_visitor_data, test_visitor_in_native_list_number);
671
672 g_test_run();
673
674 return 0;
675 }