build: Fix linking failure for qemu-ga
[qemu/ar7.git] / tests / test-qmp-output-visitor.c
blob24a635950473ac995e4224d5e13950f31689d911
1 /*
2 * QMP Output 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>
15 #include "qapi/qmp-output-visitor.h"
16 #include "test-qapi-types.h"
17 #include "test-qapi-visit.h"
18 #include "qemu-objects.h"
20 typedef struct TestOutputVisitorData {
21 QmpOutputVisitor *qov;
22 Visitor *ov;
23 } TestOutputVisitorData;
25 static void visitor_output_setup(TestOutputVisitorData *data,
26 const void *unused)
28 data->qov = qmp_output_visitor_new();
29 g_assert(data->qov != NULL);
31 data->ov = qmp_output_get_visitor(data->qov);
32 g_assert(data->ov != NULL);
35 static void visitor_output_teardown(TestOutputVisitorData *data,
36 const void *unused)
38 qmp_output_visitor_cleanup(data->qov);
39 data->qov = NULL;
40 data->ov = NULL;
43 static void test_visitor_out_int(TestOutputVisitorData *data,
44 const void *unused)
46 int64_t value = -42;
47 Error *errp = NULL;
48 QObject *obj;
50 visit_type_int(data->ov, &value, NULL, &errp);
51 g_assert(error_is_set(&errp) == 0);
53 obj = qmp_output_get_qobject(data->qov);
54 g_assert(obj != NULL);
55 g_assert(qobject_type(obj) == QTYPE_QINT);
56 g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, value);
58 qobject_decref(obj);
61 static void test_visitor_out_bool(TestOutputVisitorData *data,
62 const void *unused)
64 Error *errp = NULL;
65 bool value = true;
66 QObject *obj;
68 visit_type_bool(data->ov, &value, NULL, &errp);
69 g_assert(error_is_set(&errp) == 0);
71 obj = qmp_output_get_qobject(data->qov);
72 g_assert(obj != NULL);
73 g_assert(qobject_type(obj) == QTYPE_QBOOL);
74 g_assert(qbool_get_int(qobject_to_qbool(obj)) == value);
76 qobject_decref(obj);
79 static void test_visitor_out_number(TestOutputVisitorData *data,
80 const void *unused)
82 double value = 3.14;
83 Error *errp = NULL;
84 QObject *obj;
86 visit_type_number(data->ov, &value, NULL, &errp);
87 g_assert(error_is_set(&errp) == 0);
89 obj = qmp_output_get_qobject(data->qov);
90 g_assert(obj != NULL);
91 g_assert(qobject_type(obj) == QTYPE_QFLOAT);
92 g_assert(qfloat_get_double(qobject_to_qfloat(obj)) == value);
94 qobject_decref(obj);
97 static void test_visitor_out_string(TestOutputVisitorData *data,
98 const void *unused)
100 char *string = (char *) "Q E M U";
101 Error *errp = NULL;
102 QObject *obj;
104 visit_type_str(data->ov, &string, NULL, &errp);
105 g_assert(error_is_set(&errp) == 0);
107 obj = qmp_output_get_qobject(data->qov);
108 g_assert(obj != NULL);
109 g_assert(qobject_type(obj) == QTYPE_QSTRING);
110 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string);
112 qobject_decref(obj);
115 static void test_visitor_out_no_string(TestOutputVisitorData *data,
116 const void *unused)
118 char *string = NULL;
119 Error *errp = NULL;
120 QObject *obj;
122 /* A null string should return "" */
123 visit_type_str(data->ov, &string, NULL, &errp);
124 g_assert(error_is_set(&errp) == 0);
126 obj = qmp_output_get_qobject(data->qov);
127 g_assert(obj != NULL);
128 g_assert(qobject_type(obj) == QTYPE_QSTRING);
129 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, "");
131 qobject_decref(obj);
134 static void test_visitor_out_enum(TestOutputVisitorData *data,
135 const void *unused)
137 Error *errp = NULL;
138 QObject *obj;
139 EnumOne i;
141 for (i = 0; i < ENUM_ONE_MAX; i++) {
142 visit_type_EnumOne(data->ov, &i, "unused", &errp);
143 g_assert(!error_is_set(&errp));
145 obj = qmp_output_get_qobject(data->qov);
146 g_assert(obj != NULL);
147 g_assert(qobject_type(obj) == QTYPE_QSTRING);
148 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==,
149 EnumOne_lookup[i]);
150 qobject_decref(obj);
154 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
155 const void *unused)
157 EnumOne i, bad_values[] = { ENUM_ONE_MAX, -1 };
158 Error *errp;
160 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
161 errp = NULL;
162 visit_type_EnumOne(data->ov, &bad_values[i], "unused", &errp);
163 g_assert(error_is_set(&errp) == true);
164 error_free(errp);
168 typedef struct TestStruct
170 int64_t integer;
171 bool boolean;
172 char *string;
173 } TestStruct;
175 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
176 const char *name, Error **errp)
178 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
179 errp);
181 visit_type_int(v, &(*obj)->integer, "integer", errp);
182 visit_type_bool(v, &(*obj)->boolean, "boolean", errp);
183 visit_type_str(v, &(*obj)->string, "string", errp);
185 visit_end_struct(v, errp);
188 static void test_visitor_out_struct(TestOutputVisitorData *data,
189 const void *unused)
191 TestStruct test_struct = { .integer = 42,
192 .boolean = false,
193 .string = (char *) "foo"};
194 TestStruct *p = &test_struct;
195 Error *errp = NULL;
196 QObject *obj;
197 QDict *qdict;
199 visit_type_TestStruct(data->ov, &p, NULL, &errp);
200 g_assert(!error_is_set(&errp));
202 obj = qmp_output_get_qobject(data->qov);
203 g_assert(obj != NULL);
204 g_assert(qobject_type(obj) == QTYPE_QDICT);
206 qdict = qobject_to_qdict(obj);
207 g_assert_cmpint(qdict_size(qdict), ==, 3);
208 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42);
209 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, 0);
210 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo");
212 QDECREF(qdict);
215 static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
216 const void *unused)
218 int64_t value = 42;
219 Error *errp = NULL;
220 UserDefNested *ud2;
221 QObject *obj;
222 QDict *qdict, *dict1, *dict2, *dict3, *userdef;
223 const char *string = "user def string";
224 const char *strings[] = { "forty two", "forty three", "forty four",
225 "forty five" };
227 ud2 = g_malloc0(sizeof(*ud2));
228 ud2->string0 = g_strdup(strings[0]);
230 ud2->dict1.string1 = g_strdup(strings[1]);
231 ud2->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));
232 ud2->dict1.dict2.userdef1->string = g_strdup(string);
233 ud2->dict1.dict2.userdef1->integer = value;
234 ud2->dict1.dict2.string2 = g_strdup(strings[2]);
236 ud2->dict1.has_dict3 = true;
237 ud2->dict1.dict3.userdef2 = g_malloc0(sizeof(UserDefOne));
238 ud2->dict1.dict3.userdef2->string = g_strdup(string);
239 ud2->dict1.dict3.userdef2->integer = value;
240 ud2->dict1.dict3.string3 = g_strdup(strings[3]);
242 visit_type_UserDefNested(data->ov, &ud2, "unused", &errp);
243 g_assert(!error_is_set(&errp));
245 obj = qmp_output_get_qobject(data->qov);
246 g_assert(obj != NULL);
247 g_assert(qobject_type(obj) == QTYPE_QDICT);
249 qdict = qobject_to_qdict(obj);
250 g_assert_cmpint(qdict_size(qdict), ==, 2);
251 g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]);
253 dict1 = qdict_get_qdict(qdict, "dict1");
254 g_assert_cmpint(qdict_size(dict1), ==, 3);
255 g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]);
257 dict2 = qdict_get_qdict(dict1, "dict2");
258 g_assert_cmpint(qdict_size(dict2), ==, 2);
259 g_assert_cmpstr(qdict_get_str(dict2, "string2"), ==, strings[2]);
260 userdef = qdict_get_qdict(dict2, "userdef1");
261 g_assert_cmpint(qdict_size(userdef), ==, 2);
262 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
263 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
265 dict3 = qdict_get_qdict(dict1, "dict3");
266 g_assert_cmpint(qdict_size(dict3), ==, 2);
267 g_assert_cmpstr(qdict_get_str(dict3, "string3"), ==, strings[3]);
268 userdef = qdict_get_qdict(dict3, "userdef2");
269 g_assert_cmpint(qdict_size(userdef), ==, 2);
270 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
271 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
273 QDECREF(qdict);
274 qapi_free_UserDefNested(ud2);
277 static void test_visitor_out_struct_errors(TestOutputVisitorData *data,
278 const void *unused)
280 EnumOne bad_values[] = { ENUM_ONE_MAX, -1 };
281 UserDefOne u = { 0 }, *pu = &u;
282 Error *errp;
283 int i;
285 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
286 errp = NULL;
287 u.has_enum1 = true;
288 u.enum1 = bad_values[i];
289 visit_type_UserDefOne(data->ov, &pu, "unused", &errp);
290 g_assert(error_is_set(&errp) == true);
291 error_free(errp);
295 typedef struct TestStructList
297 TestStruct *value;
298 struct TestStructList *next;
299 } TestStructList;
301 static void visit_type_TestStructList(Visitor *v, TestStructList **obj,
302 const char *name, Error **errp)
304 GenericList *i, **head = (GenericList **)obj;
306 visit_start_list(v, name, errp);
308 for (*head = i = visit_next_list(v, head, errp); i; i = visit_next_list(v, &i, errp)) {
309 TestStructList *native_i = (TestStructList *)i;
310 visit_type_TestStruct(v, &native_i->value, NULL, errp);
313 visit_end_list(v, errp);
316 static void test_visitor_out_list(TestOutputVisitorData *data,
317 const void *unused)
319 char *value_str = (char *) "list value";
320 TestStructList *p, *head = NULL;
321 const int max_items = 10;
322 bool value_bool = true;
323 int value_int = 10;
324 Error *errp = NULL;
325 QListEntry *entry;
326 QObject *obj;
327 QList *qlist;
328 int i;
330 for (i = 0; i < max_items; i++) {
331 p = g_malloc0(sizeof(*p));
332 p->value = g_malloc0(sizeof(*p->value));
333 p->value->integer = value_int;
334 p->value->boolean = value_bool;
335 p->value->string = value_str;
337 p->next = head;
338 head = p;
341 visit_type_TestStructList(data->ov, &head, NULL, &errp);
342 g_assert(!error_is_set(&errp));
344 obj = qmp_output_get_qobject(data->qov);
345 g_assert(obj != NULL);
346 g_assert(qobject_type(obj) == QTYPE_QLIST);
348 qlist = qobject_to_qlist(obj);
349 g_assert(!qlist_empty(qlist));
351 i = 0;
352 QLIST_FOREACH_ENTRY(qlist, entry) {
353 QDict *qdict;
355 g_assert(qobject_type(entry->value) == QTYPE_QDICT);
356 qdict = qobject_to_qdict(entry->value);
357 g_assert_cmpint(qdict_size(qdict), ==, 3);
358 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int);
359 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool);
360 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str);
361 i++;
363 g_assert_cmpint(i, ==, max_items);
365 QDECREF(qlist);
367 for (p = head; p;) {
368 TestStructList *tmp = p->next;
369 g_free(p->value);
370 g_free(p);
371 p = tmp;
375 static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,
376 const void *unused)
378 UserDefNestedList *p, *head = NULL;
379 const char string[] = "foo bar";
380 int i, max_count = 1024;
382 for (i = 0; i < max_count; i++) {
383 p = g_malloc0(sizeof(*p));
384 p->value = g_malloc0(sizeof(*p->value));
386 p->value->string0 = g_strdup(string);
387 p->value->dict1.string1 = g_strdup(string);
388 p->value->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));
389 p->value->dict1.dict2.userdef1->string = g_strdup(string);
390 p->value->dict1.dict2.userdef1->integer = 42;
391 p->value->dict1.dict2.string2 = g_strdup(string);
392 p->value->dict1.has_dict3 = false;
394 p->next = head;
395 head = p;
398 qapi_free_UserDefNestedList(head);
401 static void test_visitor_out_union(TestOutputVisitorData *data,
402 const void *unused)
404 QObject *arg, *qvalue;
405 QDict *qdict, *value;
407 Error *err = NULL;
409 UserDefUnion *tmp = g_malloc0(sizeof(UserDefUnion));
410 tmp->kind = USER_DEF_UNION_KIND_A;
411 tmp->a = g_malloc0(sizeof(UserDefA));
412 tmp->a->boolean = true;
414 visit_type_UserDefUnion(data->ov, &tmp, NULL, &err);
415 g_assert(err == NULL);
416 arg = qmp_output_get_qobject(data->qov);
418 g_assert(qobject_type(arg) == QTYPE_QDICT);
419 qdict = qobject_to_qdict(arg);
421 g_assert_cmpstr(qdict_get_str(qdict, "type"), ==, "a");
423 qvalue = qdict_get(qdict, "data");
424 g_assert(data != NULL);
425 g_assert(qobject_type(qvalue) == QTYPE_QDICT);
426 value = qobject_to_qdict(qvalue);
427 g_assert_cmpint(qdict_get_bool(value, "boolean"), ==, true);
429 qapi_free_UserDefUnion(tmp);
430 QDECREF(qdict);
433 static void output_visitor_test_add(const char *testpath,
434 TestOutputVisitorData *data,
435 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
437 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
438 test_func, visitor_output_teardown);
441 int main(int argc, char **argv)
443 TestOutputVisitorData out_visitor_data;
445 g_test_init(&argc, &argv, NULL);
447 output_visitor_test_add("/visitor/output/int",
448 &out_visitor_data, test_visitor_out_int);
449 output_visitor_test_add("/visitor/output/bool",
450 &out_visitor_data, test_visitor_out_bool);
451 output_visitor_test_add("/visitor/output/number",
452 &out_visitor_data, test_visitor_out_number);
453 output_visitor_test_add("/visitor/output/string",
454 &out_visitor_data, test_visitor_out_string);
455 output_visitor_test_add("/visitor/output/no-string",
456 &out_visitor_data, test_visitor_out_no_string);
457 output_visitor_test_add("/visitor/output/enum",
458 &out_visitor_data, test_visitor_out_enum);
459 output_visitor_test_add("/visitor/output/enum-errors",
460 &out_visitor_data, test_visitor_out_enum_errors);
461 output_visitor_test_add("/visitor/output/struct",
462 &out_visitor_data, test_visitor_out_struct);
463 output_visitor_test_add("/visitor/output/struct-nested",
464 &out_visitor_data, test_visitor_out_struct_nested);
465 output_visitor_test_add("/visitor/output/struct-errors",
466 &out_visitor_data, test_visitor_out_struct_errors);
467 output_visitor_test_add("/visitor/output/list",
468 &out_visitor_data, test_visitor_out_list);
469 output_visitor_test_add("/visitor/output/list-qapi-free",
470 &out_visitor_data, test_visitor_out_list_qapi_free);
471 output_visitor_test_add("/visitor/output/union",
472 &out_visitor_data, test_visitor_out_union);
474 g_test_run();
476 return 0;