]>
Commit | Line | Data |
---|---|---|
f294f82a LC |
1 | /* |
2 | * QMP Output Visitor unit-tests. | |
3 | * | |
4 | * Copyright (C) 2011 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Luiz Capitulino <[email protected]> | |
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 | ||
79ee7df8 | 15 | #include "qemu-common.h" |
f294f82a LC |
16 | #include "qapi/qmp-output-visitor.h" |
17 | #include "test-qapi-types.h" | |
18 | #include "test-qapi-visit.h" | |
19 | #include "qemu-objects.h" | |
20 | ||
21 | typedef struct TestOutputVisitorData { | |
22 | QmpOutputVisitor *qov; | |
23 | Visitor *ov; | |
24 | } TestOutputVisitorData; | |
25 | ||
26 | static void visitor_output_setup(TestOutputVisitorData *data, | |
27 | const void *unused) | |
28 | { | |
29 | data->qov = qmp_output_visitor_new(); | |
30 | g_assert(data->qov != NULL); | |
31 | ||
32 | data->ov = qmp_output_get_visitor(data->qov); | |
33 | g_assert(data->ov != NULL); | |
34 | } | |
35 | ||
36 | static void visitor_output_teardown(TestOutputVisitorData *data, | |
37 | const void *unused) | |
38 | { | |
39 | qmp_output_visitor_cleanup(data->qov); | |
40 | data->qov = NULL; | |
41 | data->ov = NULL; | |
42 | } | |
43 | ||
44 | static void test_visitor_out_int(TestOutputVisitorData *data, | |
45 | const void *unused) | |
46 | { | |
47 | int64_t value = -42; | |
48 | Error *errp = NULL; | |
49 | QObject *obj; | |
50 | ||
51 | visit_type_int(data->ov, &value, NULL, &errp); | |
52 | g_assert(error_is_set(&errp) == 0); | |
53 | ||
54 | obj = qmp_output_get_qobject(data->qov); | |
55 | g_assert(obj != NULL); | |
56 | g_assert(qobject_type(obj) == QTYPE_QINT); | |
57 | g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, value); | |
58 | ||
59 | qobject_decref(obj); | |
60 | } | |
61 | ||
62 | static void test_visitor_out_bool(TestOutputVisitorData *data, | |
63 | const void *unused) | |
64 | { | |
65 | Error *errp = NULL; | |
66 | bool value = true; | |
67 | QObject *obj; | |
68 | ||
69 | visit_type_bool(data->ov, &value, NULL, &errp); | |
70 | g_assert(error_is_set(&errp) == 0); | |
71 | ||
72 | obj = qmp_output_get_qobject(data->qov); | |
73 | g_assert(obj != NULL); | |
74 | g_assert(qobject_type(obj) == QTYPE_QBOOL); | |
75 | g_assert(qbool_get_int(qobject_to_qbool(obj)) == value); | |
76 | ||
77 | qobject_decref(obj); | |
78 | } | |
79 | ||
80 | static void test_visitor_out_number(TestOutputVisitorData *data, | |
81 | const void *unused) | |
82 | { | |
83 | double value = 3.14; | |
84 | Error *errp = NULL; | |
85 | QObject *obj; | |
86 | ||
87 | visit_type_number(data->ov, &value, NULL, &errp); | |
88 | g_assert(error_is_set(&errp) == 0); | |
89 | ||
90 | obj = qmp_output_get_qobject(data->qov); | |
91 | g_assert(obj != NULL); | |
92 | g_assert(qobject_type(obj) == QTYPE_QFLOAT); | |
93 | g_assert(qfloat_get_double(qobject_to_qfloat(obj)) == value); | |
94 | ||
95 | qobject_decref(obj); | |
96 | } | |
97 | ||
98 | static void test_visitor_out_string(TestOutputVisitorData *data, | |
99 | const void *unused) | |
100 | { | |
101 | char *string = (char *) "Q E M U"; | |
102 | Error *errp = NULL; | |
103 | QObject *obj; | |
104 | ||
105 | visit_type_str(data->ov, &string, NULL, &errp); | |
106 | g_assert(error_is_set(&errp) == 0); | |
107 | ||
108 | obj = qmp_output_get_qobject(data->qov); | |
109 | g_assert(obj != NULL); | |
110 | g_assert(qobject_type(obj) == QTYPE_QSTRING); | |
111 | g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string); | |
112 | ||
113 | qobject_decref(obj); | |
114 | } | |
115 | ||
116 | static void test_visitor_out_no_string(TestOutputVisitorData *data, | |
117 | const void *unused) | |
118 | { | |
119 | char *string = NULL; | |
120 | Error *errp = NULL; | |
121 | QObject *obj; | |
122 | ||
123 | /* A null string should return "" */ | |
124 | visit_type_str(data->ov, &string, NULL, &errp); | |
125 | g_assert(error_is_set(&errp) == 0); | |
126 | ||
127 | obj = qmp_output_get_qobject(data->qov); | |
128 | g_assert(obj != NULL); | |
129 | g_assert(qobject_type(obj) == QTYPE_QSTRING); | |
130 | g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, ""); | |
131 | ||
132 | qobject_decref(obj); | |
133 | } | |
134 | ||
135 | static void test_visitor_out_enum(TestOutputVisitorData *data, | |
136 | const void *unused) | |
137 | { | |
138 | Error *errp = NULL; | |
139 | QObject *obj; | |
140 | EnumOne i; | |
141 | ||
142 | for (i = 0; i < ENUM_ONE_MAX; i++) { | |
143 | visit_type_EnumOne(data->ov, &i, "unused", &errp); | |
144 | g_assert(!error_is_set(&errp)); | |
145 | ||
146 | obj = qmp_output_get_qobject(data->qov); | |
147 | g_assert(obj != NULL); | |
148 | g_assert(qobject_type(obj) == QTYPE_QSTRING); | |
149 | g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, | |
150 | EnumOne_lookup[i]); | |
151 | qobject_decref(obj); | |
152 | } | |
153 | } | |
154 | ||
155 | static void test_visitor_out_enum_errors(TestOutputVisitorData *data, | |
156 | const void *unused) | |
157 | { | |
158 | EnumOne i, bad_values[] = { ENUM_ONE_MAX, -1 }; | |
159 | Error *errp; | |
160 | ||
161 | for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) { | |
162 | errp = NULL; | |
163 | visit_type_EnumOne(data->ov, &bad_values[i], "unused", &errp); | |
164 | g_assert(error_is_set(&errp) == true); | |
165 | error_free(errp); | |
166 | } | |
167 | } | |
168 | ||
169 | typedef struct TestStruct | |
170 | { | |
171 | int64_t integer; | |
172 | bool boolean; | |
173 | char *string; | |
174 | } TestStruct; | |
175 | ||
176 | static void visit_type_TestStruct(Visitor *v, TestStruct **obj, | |
177 | const char *name, Error **errp) | |
178 | { | |
179 | visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct), | |
180 | errp); | |
181 | ||
182 | visit_type_int(v, &(*obj)->integer, "integer", errp); | |
183 | visit_type_bool(v, &(*obj)->boolean, "boolean", errp); | |
184 | visit_type_str(v, &(*obj)->string, "string", errp); | |
185 | ||
186 | visit_end_struct(v, errp); | |
187 | } | |
188 | ||
189 | static void test_visitor_out_struct(TestOutputVisitorData *data, | |
190 | const void *unused) | |
191 | { | |
192 | TestStruct test_struct = { .integer = 42, | |
193 | .boolean = false, | |
194 | .string = (char *) "foo"}; | |
195 | TestStruct *p = &test_struct; | |
196 | Error *errp = NULL; | |
197 | QObject *obj; | |
198 | QDict *qdict; | |
199 | ||
200 | visit_type_TestStruct(data->ov, &p, NULL, &errp); | |
201 | g_assert(!error_is_set(&errp)); | |
202 | ||
203 | obj = qmp_output_get_qobject(data->qov); | |
204 | g_assert(obj != NULL); | |
205 | g_assert(qobject_type(obj) == QTYPE_QDICT); | |
206 | ||
207 | qdict = qobject_to_qdict(obj); | |
208 | g_assert_cmpint(qdict_size(qdict), ==, 3); | |
209 | g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42); | |
210 | g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, 0); | |
211 | g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo"); | |
212 | ||
213 | QDECREF(qdict); | |
214 | } | |
215 | ||
216 | static void test_visitor_out_struct_nested(TestOutputVisitorData *data, | |
217 | const void *unused) | |
218 | { | |
219 | int64_t value = 42; | |
220 | Error *errp = NULL; | |
221 | UserDefNested *ud2; | |
222 | QObject *obj; | |
223 | QDict *qdict, *dict1, *dict2, *dict3, *userdef; | |
224 | const char *string = "user def string"; | |
beae9d79 SW |
225 | const char *strings[] = { "forty two", "forty three", "forty four", |
226 | "forty five" }; | |
f294f82a LC |
227 | |
228 | ud2 = g_malloc0(sizeof(*ud2)); | |
229 | ud2->string0 = g_strdup(strings[0]); | |
230 | ||
231 | ud2->dict1.string1 = g_strdup(strings[1]); | |
232 | ud2->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne)); | |
233 | ud2->dict1.dict2.userdef1->string = g_strdup(string); | |
234 | ud2->dict1.dict2.userdef1->integer = value; | |
235 | ud2->dict1.dict2.string2 = g_strdup(strings[2]); | |
236 | ||
237 | ud2->dict1.has_dict3 = true; | |
238 | ud2->dict1.dict3.userdef2 = g_malloc0(sizeof(UserDefOne)); | |
239 | ud2->dict1.dict3.userdef2->string = g_strdup(string); | |
240 | ud2->dict1.dict3.userdef2->integer = value; | |
241 | ud2->dict1.dict3.string3 = g_strdup(strings[3]); | |
242 | ||
243 | visit_type_UserDefNested(data->ov, &ud2, "unused", &errp); | |
244 | g_assert(!error_is_set(&errp)); | |
245 | ||
246 | obj = qmp_output_get_qobject(data->qov); | |
247 | g_assert(obj != NULL); | |
248 | g_assert(qobject_type(obj) == QTYPE_QDICT); | |
249 | ||
250 | qdict = qobject_to_qdict(obj); | |
251 | g_assert_cmpint(qdict_size(qdict), ==, 2); | |
252 | g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]); | |
253 | ||
254 | dict1 = qdict_get_qdict(qdict, "dict1"); | |
255 | g_assert_cmpint(qdict_size(dict1), ==, 3); | |
256 | g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]); | |
257 | ||
258 | dict2 = qdict_get_qdict(dict1, "dict2"); | |
259 | g_assert_cmpint(qdict_size(dict2), ==, 2); | |
260 | g_assert_cmpstr(qdict_get_str(dict2, "string2"), ==, strings[2]); | |
261 | userdef = qdict_get_qdict(dict2, "userdef1"); | |
262 | g_assert_cmpint(qdict_size(userdef), ==, 2); | |
263 | g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value); | |
264 | g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string); | |
265 | ||
266 | dict3 = qdict_get_qdict(dict1, "dict3"); | |
267 | g_assert_cmpint(qdict_size(dict3), ==, 2); | |
268 | g_assert_cmpstr(qdict_get_str(dict3, "string3"), ==, strings[3]); | |
269 | userdef = qdict_get_qdict(dict3, "userdef2"); | |
270 | g_assert_cmpint(qdict_size(userdef), ==, 2); | |
271 | g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value); | |
272 | g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string); | |
273 | ||
274 | QDECREF(qdict); | |
275 | qapi_free_UserDefNested(ud2); | |
276 | } | |
277 | ||
9e9eace8 PB |
278 | static void test_visitor_out_struct_errors(TestOutputVisitorData *data, |
279 | const void *unused) | |
280 | { | |
281 | EnumOne bad_values[] = { ENUM_ONE_MAX, -1 }; | |
282 | UserDefOne u = { 0 }, *pu = &u; | |
283 | Error *errp; | |
284 | int i; | |
285 | ||
286 | for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) { | |
287 | errp = NULL; | |
288 | u.has_enum1 = true; | |
289 | u.enum1 = bad_values[i]; | |
290 | visit_type_UserDefOne(data->ov, &pu, "unused", &errp); | |
291 | g_assert(error_is_set(&errp) == true); | |
292 | error_free(errp); | |
293 | } | |
294 | } | |
295 | ||
f294f82a LC |
296 | typedef struct TestStructList |
297 | { | |
298 | TestStruct *value; | |
299 | struct TestStructList *next; | |
300 | } TestStructList; | |
301 | ||
302 | static void visit_type_TestStructList(Visitor *v, TestStructList **obj, | |
303 | const char *name, Error **errp) | |
304 | { | |
305 | GenericList *i, **head = (GenericList **)obj; | |
306 | ||
307 | visit_start_list(v, name, errp); | |
308 | ||
309 | for (*head = i = visit_next_list(v, head, errp); i; i = visit_next_list(v, &i, errp)) { | |
310 | TestStructList *native_i = (TestStructList *)i; | |
311 | visit_type_TestStruct(v, &native_i->value, NULL, errp); | |
312 | } | |
313 | ||
314 | visit_end_list(v, errp); | |
315 | } | |
316 | ||
317 | static void test_visitor_out_list(TestOutputVisitorData *data, | |
318 | const void *unused) | |
319 | { | |
320 | char *value_str = (char *) "list value"; | |
321 | TestStructList *p, *head = NULL; | |
322 | const int max_items = 10; | |
323 | bool value_bool = true; | |
324 | int value_int = 10; | |
325 | Error *errp = NULL; | |
326 | QListEntry *entry; | |
327 | QObject *obj; | |
328 | QList *qlist; | |
329 | int i; | |
330 | ||
331 | for (i = 0; i < max_items; i++) { | |
332 | p = g_malloc0(sizeof(*p)); | |
333 | p->value = g_malloc0(sizeof(*p->value)); | |
334 | p->value->integer = value_int; | |
335 | p->value->boolean = value_bool; | |
336 | p->value->string = value_str; | |
337 | ||
338 | p->next = head; | |
339 | head = p; | |
340 | } | |
341 | ||
342 | visit_type_TestStructList(data->ov, &head, NULL, &errp); | |
343 | g_assert(!error_is_set(&errp)); | |
344 | ||
345 | obj = qmp_output_get_qobject(data->qov); | |
346 | g_assert(obj != NULL); | |
347 | g_assert(qobject_type(obj) == QTYPE_QLIST); | |
348 | ||
349 | qlist = qobject_to_qlist(obj); | |
350 | g_assert(!qlist_empty(qlist)); | |
351 | ||
352 | i = 0; | |
353 | QLIST_FOREACH_ENTRY(qlist, entry) { | |
354 | QDict *qdict; | |
355 | ||
356 | g_assert(qobject_type(entry->value) == QTYPE_QDICT); | |
357 | qdict = qobject_to_qdict(entry->value); | |
358 | g_assert_cmpint(qdict_size(qdict), ==, 3); | |
359 | g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int); | |
360 | g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool); | |
361 | g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str); | |
362 | i++; | |
363 | } | |
364 | g_assert_cmpint(i, ==, max_items); | |
365 | ||
366 | QDECREF(qlist); | |
367 | ||
368 | for (p = head; p;) { | |
369 | TestStructList *tmp = p->next; | |
370 | g_free(p->value); | |
371 | g_free(p); | |
372 | p = tmp; | |
373 | } | |
374 | } | |
375 | ||
376 | static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data, | |
377 | const void *unused) | |
378 | { | |
379 | UserDefNestedList *p, *head = NULL; | |
380 | const char string[] = "foo bar"; | |
381 | int i, max_count = 1024; | |
382 | ||
383 | for (i = 0; i < max_count; i++) { | |
384 | p = g_malloc0(sizeof(*p)); | |
385 | p->value = g_malloc0(sizeof(*p->value)); | |
386 | ||
387 | p->value->string0 = g_strdup(string); | |
388 | p->value->dict1.string1 = g_strdup(string); | |
389 | p->value->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne)); | |
390 | p->value->dict1.dict2.userdef1->string = g_strdup(string); | |
391 | p->value->dict1.dict2.userdef1->integer = 42; | |
392 | p->value->dict1.dict2.string2 = g_strdup(string); | |
393 | p->value->dict1.has_dict3 = false; | |
394 | ||
395 | p->next = head; | |
396 | head = p; | |
397 | } | |
398 | ||
399 | qapi_free_UserDefNestedList(head); | |
400 | } | |
401 | ||
dc8fb6df PB |
402 | static void test_visitor_out_union(TestOutputVisitorData *data, |
403 | const void *unused) | |
404 | { | |
405 | QObject *arg, *qvalue; | |
406 | QDict *qdict, *value; | |
407 | ||
408 | Error *err = NULL; | |
409 | ||
410 | UserDefUnion *tmp = g_malloc0(sizeof(UserDefUnion)); | |
411 | tmp->kind = USER_DEF_UNION_KIND_A; | |
412 | tmp->a = g_malloc0(sizeof(UserDefA)); | |
413 | tmp->a->boolean = true; | |
414 | ||
415 | visit_type_UserDefUnion(data->ov, &tmp, NULL, &err); | |
416 | g_assert(err == NULL); | |
417 | arg = qmp_output_get_qobject(data->qov); | |
418 | ||
419 | g_assert(qobject_type(arg) == QTYPE_QDICT); | |
420 | qdict = qobject_to_qdict(arg); | |
421 | ||
422 | g_assert_cmpstr(qdict_get_str(qdict, "type"), ==, "a"); | |
423 | ||
424 | qvalue = qdict_get(qdict, "data"); | |
425 | g_assert(data != NULL); | |
426 | g_assert(qobject_type(qvalue) == QTYPE_QDICT); | |
427 | value = qobject_to_qdict(qvalue); | |
428 | g_assert_cmpint(qdict_get_bool(value, "boolean"), ==, true); | |
429 | ||
430 | qapi_free_UserDefUnion(tmp); | |
431 | QDECREF(qdict); | |
432 | } | |
433 | ||
f294f82a LC |
434 | static void output_visitor_test_add(const char *testpath, |
435 | TestOutputVisitorData *data, | |
436 | void (*test_func)(TestOutputVisitorData *data, const void *user_data)) | |
437 | { | |
438 | g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup, | |
439 | test_func, visitor_output_teardown); | |
440 | } | |
441 | ||
442 | int main(int argc, char **argv) | |
443 | { | |
444 | TestOutputVisitorData out_visitor_data; | |
445 | ||
446 | g_test_init(&argc, &argv, NULL); | |
447 | ||
448 | output_visitor_test_add("/visitor/output/int", | |
449 | &out_visitor_data, test_visitor_out_int); | |
450 | output_visitor_test_add("/visitor/output/bool", | |
451 | &out_visitor_data, test_visitor_out_bool); | |
452 | output_visitor_test_add("/visitor/output/number", | |
453 | &out_visitor_data, test_visitor_out_number); | |
454 | output_visitor_test_add("/visitor/output/string", | |
455 | &out_visitor_data, test_visitor_out_string); | |
456 | output_visitor_test_add("/visitor/output/no-string", | |
457 | &out_visitor_data, test_visitor_out_no_string); | |
458 | output_visitor_test_add("/visitor/output/enum", | |
459 | &out_visitor_data, test_visitor_out_enum); | |
460 | output_visitor_test_add("/visitor/output/enum-errors", | |
461 | &out_visitor_data, test_visitor_out_enum_errors); | |
462 | output_visitor_test_add("/visitor/output/struct", | |
463 | &out_visitor_data, test_visitor_out_struct); | |
464 | output_visitor_test_add("/visitor/output/struct-nested", | |
465 | &out_visitor_data, test_visitor_out_struct_nested); | |
9e9eace8 PB |
466 | output_visitor_test_add("/visitor/output/struct-errors", |
467 | &out_visitor_data, test_visitor_out_struct_errors); | |
f294f82a LC |
468 | output_visitor_test_add("/visitor/output/list", |
469 | &out_visitor_data, test_visitor_out_list); | |
470 | output_visitor_test_add("/visitor/output/list-qapi-free", | |
471 | &out_visitor_data, test_visitor_out_list_qapi_free); | |
dc8fb6df PB |
472 | output_visitor_test_add("/visitor/output/union", |
473 | &out_visitor_data, test_visitor_out_union); | |
f294f82a LC |
474 | |
475 | g_test_run(); | |
476 | ||
477 | return 0; | |
478 | } |