readline: Add missing GCC_FMT_ATTR
[qemu/ar7.git] / tests / test-qmp-input-visitor.c
blobd19b360aace85f573db54997bb1fa1b76ab49ff6
1 /*
2 * QMP Input Visitor unit-tests.
4 * Copyright (C) 2011 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include <glib.h>
14 #include <stdarg.h>
16 #include "qemu-common.h"
17 #include "qapi/qmp-input-visitor.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "qapi/qmp/types.h"
22 typedef struct TestInputVisitorData {
23 QObject *obj;
24 QmpInputVisitor *qiv;
25 } TestInputVisitorData;
27 static void visitor_input_teardown(TestInputVisitorData *data,
28 const void *unused)
30 qobject_decref(data->obj);
31 data->obj = NULL;
33 if (data->qiv) {
34 qmp_input_visitor_cleanup(data->qiv);
35 data->qiv = NULL;
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, ...)
46 Visitor *v;
47 va_list ap;
49 va_start(ap, json_string);
50 data->obj = qobject_from_jsonv(json_string, &ap);
51 va_end(ap);
53 g_assert(data->obj != NULL);
55 data->qiv = qmp_input_visitor_new(data->obj);
56 g_assert(data->qiv != NULL);
58 v = qmp_input_get_visitor(data->qiv);
59 g_assert(v != NULL);
61 return v;
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)
71 static GCC_FMT_ATTR(2, 0)
72 Visitor *visitor_input_test_init_raw(TestInputVisitorData *data,
73 const char *json_string)
75 Visitor *v;
77 data->obj = qobject_from_json(json_string);
79 g_assert(data->obj != NULL);
81 data->qiv = qmp_input_visitor_new(data->obj);
82 g_assert(data->qiv != NULL);
84 v = qmp_input_get_visitor(data->qiv);
85 g_assert(v != NULL);
87 return v;
90 static void test_visitor_in_int(TestInputVisitorData *data,
91 const void *unused)
93 int64_t res = 0, value = -42;
94 Error *errp = NULL;
95 Visitor *v;
97 v = visitor_input_test_init(data, "%" PRId64, value);
99 visit_type_int(v, &res, NULL, &errp);
100 g_assert(!error_is_set(&errp));
101 g_assert_cmpint(res, ==, value);
104 static void test_visitor_in_int_overflow(TestInputVisitorData *data,
105 const void *unused)
107 int64_t res = 0;
108 Error *errp = NULL;
109 Visitor *v;
111 /* this will overflow a Qint/int64, so should be deserialized into
112 * a QFloat/double field instead, leading to an error if we pass it
113 * to visit_type_int. confirm this.
115 v = visitor_input_test_init(data, "%f", DBL_MAX);
117 visit_type_int(v, &res, NULL, &errp);
118 g_assert(error_is_set(&errp));
119 error_free(errp);
122 static void test_visitor_in_bool(TestInputVisitorData *data,
123 const void *unused)
125 Error *errp = NULL;
126 bool res = false;
127 Visitor *v;
129 v = visitor_input_test_init(data, "true");
131 visit_type_bool(v, &res, NULL, &errp);
132 g_assert(!error_is_set(&errp));
133 g_assert_cmpint(res, ==, true);
136 static void test_visitor_in_number(TestInputVisitorData *data,
137 const void *unused)
139 double res = 0, value = 3.14;
140 Error *errp = NULL;
141 Visitor *v;
143 v = visitor_input_test_init(data, "%f", value);
145 visit_type_number(v, &res, NULL, &errp);
146 g_assert(!error_is_set(&errp));
147 g_assert_cmpfloat(res, ==, value);
150 static void test_visitor_in_string(TestInputVisitorData *data,
151 const void *unused)
153 char *res = NULL, *value = (char *) "Q E M U";
154 Error *errp = NULL;
155 Visitor *v;
157 v = visitor_input_test_init(data, "%s", value);
159 visit_type_str(v, &res, NULL, &errp);
160 g_assert(!error_is_set(&errp));
161 g_assert_cmpstr(res, ==, value);
163 g_free(res);
166 static void test_visitor_in_enum(TestInputVisitorData *data,
167 const void *unused)
169 Error *errp = NULL;
170 Visitor *v;
171 EnumOne i;
173 for (i = 0; EnumOne_lookup[i]; i++) {
174 EnumOne res = -1;
176 v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]);
178 visit_type_EnumOne(v, &res, NULL, &errp);
179 g_assert(!error_is_set(&errp));
180 g_assert_cmpint(i, ==, res);
182 visitor_input_teardown(data, NULL);
185 data->obj = NULL;
186 data->qiv = NULL;
189 typedef struct TestStruct
191 int64_t integer;
192 bool boolean;
193 char *string;
194 } TestStruct;
196 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
197 const char *name, Error **errp)
199 Error *err = NULL;
200 if (!error_is_set(errp)) {
201 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
202 &err);
203 if (!err) {
204 visit_type_int(v, &(*obj)->integer, "integer", &err);
205 visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
206 visit_type_str(v, &(*obj)->string, "string", &err);
208 /* Always call end_struct if start_struct succeeded. */
209 error_propagate(errp, err);
210 err = NULL;
211 visit_end_struct(v, &err);
213 error_propagate(errp, err);
217 static void test_visitor_in_struct(TestInputVisitorData *data,
218 const void *unused)
220 TestStruct *p = NULL;
221 Error *errp = NULL;
222 Visitor *v;
224 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
226 visit_type_TestStruct(v, &p, NULL, &errp);
227 g_assert(!error_is_set(&errp));
228 g_assert_cmpint(p->integer, ==, -42);
229 g_assert(p->boolean == true);
230 g_assert_cmpstr(p->string, ==, "foo");
232 g_free(p->string);
233 g_free(p);
236 static void check_and_free_str(char *str, const char *cmp)
238 g_assert_cmpstr(str, ==, cmp);
239 g_free(str);
242 static void test_visitor_in_struct_nested(TestInputVisitorData *data,
243 const void *unused)
245 UserDefNested *udp = NULL;
246 Error *errp = NULL;
247 Visitor *v;
249 v = visitor_input_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
251 visit_type_UserDefNested(v, &udp, NULL, &errp);
252 g_assert(!error_is_set(&errp));
254 check_and_free_str(udp->string0, "string0");
255 check_and_free_str(udp->dict1.string1, "string1");
256 g_assert_cmpint(udp->dict1.dict2.userdef1->integer, ==, 42);
257 check_and_free_str(udp->dict1.dict2.userdef1->string, "string");
258 check_and_free_str(udp->dict1.dict2.string2, "string2");
259 g_assert(udp->dict1.has_dict3 == false);
261 g_free(udp->dict1.dict2.userdef1);
262 g_free(udp);
265 static void test_visitor_in_list(TestInputVisitorData *data,
266 const void *unused)
268 UserDefOneList *item, *head = NULL;
269 Error *errp = NULL;
270 Visitor *v;
271 int i;
273 v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
275 visit_type_UserDefOneList(v, &head, NULL, &errp);
276 g_assert(!error_is_set(&errp));
277 g_assert(head != NULL);
279 for (i = 0, item = head; item; item = item->next, i++) {
280 char string[12];
282 snprintf(string, sizeof(string), "string%d", i);
283 g_assert_cmpstr(item->value->string, ==, string);
284 g_assert_cmpint(item->value->integer, ==, 42 + i);
287 qapi_free_UserDefOneList(head);
290 static void test_visitor_in_union(TestInputVisitorData *data,
291 const void *unused)
293 Visitor *v;
294 Error *err = NULL;
295 UserDefUnion *tmp;
297 v = visitor_input_test_init(data, "{ 'type': 'b', 'data' : { 'integer': 42 } }");
299 visit_type_UserDefUnion(v, &tmp, NULL, &err);
300 g_assert(err == NULL);
301 g_assert_cmpint(tmp->kind, ==, USER_DEF_UNION_KIND_B);
302 g_assert_cmpint(tmp->b->integer, ==, 42);
303 qapi_free_UserDefUnion(tmp);
306 static void test_native_list_integer_helper(TestInputVisitorData *data,
307 const void *unused,
308 UserDefNativeListUnionKind kind)
310 UserDefNativeListUnion *cvalue = NULL;
311 Error *err = NULL;
312 Visitor *v;
313 GString *gstr_list = g_string_new("");
314 GString *gstr_union = g_string_new("");
315 int i;
317 for (i = 0; i < 32; i++) {
318 g_string_append_printf(gstr_list, "%d", i);
319 if (i != 31) {
320 g_string_append(gstr_list, ", ");
323 g_string_append_printf(gstr_union, "{ 'type': '%s', 'data': [ %s ] }",
324 UserDefNativeListUnionKind_lookup[kind],
325 gstr_list->str);
326 v = visitor_input_test_init_raw(data, gstr_union->str);
328 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err);
329 g_assert(err == NULL);
330 g_assert(cvalue != NULL);
331 g_assert_cmpint(cvalue->kind, ==, kind);
333 switch (kind) {
334 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
335 intList *elem = NULL;
336 for (i = 0, elem = cvalue->integer; elem; elem = elem->next, i++) {
337 g_assert_cmpint(elem->value, ==, i);
339 break;
341 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
342 int8List *elem = NULL;
343 for (i = 0, elem = cvalue->s8; elem; elem = elem->next, i++) {
344 g_assert_cmpint(elem->value, ==, i);
346 break;
348 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
349 int16List *elem = NULL;
350 for (i = 0, elem = cvalue->s16; elem; elem = elem->next, i++) {
351 g_assert_cmpint(elem->value, ==, i);
353 break;
355 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
356 int32List *elem = NULL;
357 for (i = 0, elem = cvalue->s32; elem; elem = elem->next, i++) {
358 g_assert_cmpint(elem->value, ==, i);
360 break;
362 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
363 int64List *elem = NULL;
364 for (i = 0, elem = cvalue->s64; elem; elem = elem->next, i++) {
365 g_assert_cmpint(elem->value, ==, i);
367 break;
369 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
370 uint8List *elem = NULL;
371 for (i = 0, elem = cvalue->u8; elem; elem = elem->next, i++) {
372 g_assert_cmpint(elem->value, ==, i);
374 break;
376 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
377 uint16List *elem = NULL;
378 for (i = 0, elem = cvalue->u16; elem; elem = elem->next, i++) {
379 g_assert_cmpint(elem->value, ==, i);
381 break;
383 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
384 uint32List *elem = NULL;
385 for (i = 0, elem = cvalue->u32; elem; elem = elem->next, i++) {
386 g_assert_cmpint(elem->value, ==, i);
388 break;
390 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
391 uint64List *elem = NULL;
392 for (i = 0, elem = cvalue->u64; elem; elem = elem->next, i++) {
393 g_assert_cmpint(elem->value, ==, i);
395 break;
397 default:
398 g_assert_not_reached();
401 g_string_free(gstr_union, true);
402 g_string_free(gstr_list, true);
403 qapi_free_UserDefNativeListUnion(cvalue);
406 static void test_visitor_in_native_list_int(TestInputVisitorData *data,
407 const void *unused)
409 test_native_list_integer_helper(data, unused,
410 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
413 static void test_visitor_in_native_list_int8(TestInputVisitorData *data,
414 const void *unused)
416 test_native_list_integer_helper(data, unused,
417 USER_DEF_NATIVE_LIST_UNION_KIND_S8);
420 static void test_visitor_in_native_list_int16(TestInputVisitorData *data,
421 const void *unused)
423 test_native_list_integer_helper(data, unused,
424 USER_DEF_NATIVE_LIST_UNION_KIND_S16);
427 static void test_visitor_in_native_list_int32(TestInputVisitorData *data,
428 const void *unused)
430 test_native_list_integer_helper(data, unused,
431 USER_DEF_NATIVE_LIST_UNION_KIND_S32);
434 static void test_visitor_in_native_list_int64(TestInputVisitorData *data,
435 const void *unused)
437 test_native_list_integer_helper(data, unused,
438 USER_DEF_NATIVE_LIST_UNION_KIND_S64);
441 static void test_visitor_in_native_list_uint8(TestInputVisitorData *data,
442 const void *unused)
444 test_native_list_integer_helper(data, unused,
445 USER_DEF_NATIVE_LIST_UNION_KIND_U8);
448 static void test_visitor_in_native_list_uint16(TestInputVisitorData *data,
449 const void *unused)
451 test_native_list_integer_helper(data, unused,
452 USER_DEF_NATIVE_LIST_UNION_KIND_U16);
455 static void test_visitor_in_native_list_uint32(TestInputVisitorData *data,
456 const void *unused)
458 test_native_list_integer_helper(data, unused,
459 USER_DEF_NATIVE_LIST_UNION_KIND_U32);
462 static void test_visitor_in_native_list_uint64(TestInputVisitorData *data,
463 const void *unused)
465 test_native_list_integer_helper(data, unused,
466 USER_DEF_NATIVE_LIST_UNION_KIND_U64);
469 static void test_visitor_in_native_list_bool(TestInputVisitorData *data,
470 const void *unused)
472 UserDefNativeListUnion *cvalue = NULL;
473 boolList *elem = NULL;
474 Error *err = NULL;
475 Visitor *v;
476 GString *gstr_list = g_string_new("");
477 GString *gstr_union = g_string_new("");
478 int i;
480 for (i = 0; i < 32; i++) {
481 g_string_append_printf(gstr_list, "%s",
482 (i % 3 == 0) ? "true" : "false");
483 if (i != 31) {
484 g_string_append(gstr_list, ", ");
487 g_string_append_printf(gstr_union, "{ 'type': 'boolean', 'data': [ %s ] }",
488 gstr_list->str);
489 v = visitor_input_test_init_raw(data, gstr_union->str);
491 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err);
492 g_assert(err == NULL);
493 g_assert(cvalue != NULL);
494 g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
496 for (i = 0, elem = cvalue->boolean; elem; elem = elem->next, i++) {
497 g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0);
500 g_string_free(gstr_union, true);
501 g_string_free(gstr_list, true);
502 qapi_free_UserDefNativeListUnion(cvalue);
505 static void test_visitor_in_native_list_string(TestInputVisitorData *data,
506 const void *unused)
508 UserDefNativeListUnion *cvalue = NULL;
509 strList *elem = NULL;
510 Error *err = NULL;
511 Visitor *v;
512 GString *gstr_list = g_string_new("");
513 GString *gstr_union = g_string_new("");
514 int i;
516 for (i = 0; i < 32; i++) {
517 g_string_append_printf(gstr_list, "'%d'", i);
518 if (i != 31) {
519 g_string_append(gstr_list, ", ");
522 g_string_append_printf(gstr_union, "{ 'type': 'string', 'data': [ %s ] }",
523 gstr_list->str);
524 v = visitor_input_test_init_raw(data, gstr_union->str);
526 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err);
527 g_assert(err == NULL);
528 g_assert(cvalue != NULL);
529 g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
531 for (i = 0, elem = cvalue->string; elem; elem = elem->next, i++) {
532 gchar str[8];
533 sprintf(str, "%d", i);
534 g_assert_cmpstr(elem->value, ==, str);
537 g_string_free(gstr_union, true);
538 g_string_free(gstr_list, true);
539 qapi_free_UserDefNativeListUnion(cvalue);
542 #define DOUBLE_STR_MAX 16
544 static void test_visitor_in_native_list_number(TestInputVisitorData *data,
545 const void *unused)
547 UserDefNativeListUnion *cvalue = NULL;
548 numberList *elem = NULL;
549 Error *err = NULL;
550 Visitor *v;
551 GString *gstr_list = g_string_new("");
552 GString *gstr_union = g_string_new("");
553 int i;
555 for (i = 0; i < 32; i++) {
556 g_string_append_printf(gstr_list, "%f", (double)i / 3);
557 if (i != 31) {
558 g_string_append(gstr_list, ", ");
561 g_string_append_printf(gstr_union, "{ 'type': 'number', 'data': [ %s ] }",
562 gstr_list->str);
563 v = visitor_input_test_init_raw(data, gstr_union->str);
565 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err);
566 g_assert(err == NULL);
567 g_assert(cvalue != NULL);
568 g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
570 for (i = 0, elem = cvalue->number; elem; elem = elem->next, i++) {
571 GString *double_expected = g_string_new("");
572 GString *double_actual = g_string_new("");
574 g_string_printf(double_expected, "%.6f", (double)i / 3);
575 g_string_printf(double_actual, "%.6f", elem->value);
576 g_assert_cmpstr(double_expected->str, ==, double_actual->str);
578 g_string_free(double_expected, true);
579 g_string_free(double_actual, true);
582 g_string_free(gstr_union, true);
583 g_string_free(gstr_list, true);
584 qapi_free_UserDefNativeListUnion(cvalue);
587 static void input_visitor_test_add(const char *testpath,
588 TestInputVisitorData *data,
589 void (*test_func)(TestInputVisitorData *data, const void *user_data))
591 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
592 visitor_input_teardown);
595 static void test_visitor_in_errors(TestInputVisitorData *data,
596 const void *unused)
598 TestStruct *p = NULL;
599 Error *errp = NULL;
600 Visitor *v;
602 v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }");
604 visit_type_TestStruct(v, &p, NULL, &errp);
605 g_assert(error_is_set(&errp));
606 g_assert(p->string == NULL);
608 error_free(errp);
609 g_free(p->string);
610 g_free(p);
613 int main(int argc, char **argv)
615 TestInputVisitorData in_visitor_data;
617 g_test_init(&argc, &argv, NULL);
619 input_visitor_test_add("/visitor/input/int",
620 &in_visitor_data, test_visitor_in_int);
621 input_visitor_test_add("/visitor/input/int_overflow",
622 &in_visitor_data, test_visitor_in_int_overflow);
623 input_visitor_test_add("/visitor/input/bool",
624 &in_visitor_data, test_visitor_in_bool);
625 input_visitor_test_add("/visitor/input/number",
626 &in_visitor_data, test_visitor_in_number);
627 input_visitor_test_add("/visitor/input/string",
628 &in_visitor_data, test_visitor_in_string);
629 input_visitor_test_add("/visitor/input/enum",
630 &in_visitor_data, test_visitor_in_enum);
631 input_visitor_test_add("/visitor/input/struct",
632 &in_visitor_data, test_visitor_in_struct);
633 input_visitor_test_add("/visitor/input/struct-nested",
634 &in_visitor_data, test_visitor_in_struct_nested);
635 input_visitor_test_add("/visitor/input/list",
636 &in_visitor_data, test_visitor_in_list);
637 input_visitor_test_add("/visitor/input/union",
638 &in_visitor_data, test_visitor_in_union);
639 input_visitor_test_add("/visitor/input/errors",
640 &in_visitor_data, test_visitor_in_errors);
641 input_visitor_test_add("/visitor/input/native_list/int",
642 &in_visitor_data,
643 test_visitor_in_native_list_int);
644 input_visitor_test_add("/visitor/input/native_list/int8",
645 &in_visitor_data,
646 test_visitor_in_native_list_int8);
647 input_visitor_test_add("/visitor/input/native_list/int16",
648 &in_visitor_data,
649 test_visitor_in_native_list_int16);
650 input_visitor_test_add("/visitor/input/native_list/int32",
651 &in_visitor_data,
652 test_visitor_in_native_list_int32);
653 input_visitor_test_add("/visitor/input/native_list/int64",
654 &in_visitor_data,
655 test_visitor_in_native_list_int64);
656 input_visitor_test_add("/visitor/input/native_list/uint8",
657 &in_visitor_data,
658 test_visitor_in_native_list_uint8);
659 input_visitor_test_add("/visitor/input/native_list/uint16",
660 &in_visitor_data,
661 test_visitor_in_native_list_uint16);
662 input_visitor_test_add("/visitor/input/native_list/uint32",
663 &in_visitor_data,
664 test_visitor_in_native_list_uint32);
665 input_visitor_test_add("/visitor/input/native_list/uint64",
666 &in_visitor_data, test_visitor_in_native_list_uint64);
667 input_visitor_test_add("/visitor/input/native_list/bool",
668 &in_visitor_data, test_visitor_in_native_list_bool);
669 input_visitor_test_add("/visitor/input/native_list/str",
670 &in_visitor_data, test_visitor_in_native_list_string);
671 input_visitor_test_add("/visitor/input/native_list/number",
672 &in_visitor_data, test_visitor_in_native_list_number);
674 g_test_run();
676 return 0;