2 * QMP Output Visitor unit-tests.
4 * Copyright (C) 2011 Red Hat Inc.
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.
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
;
23 } TestOutputVisitorData
;
25 static void visitor_output_setup(TestOutputVisitorData
*data
,
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
,
38 qmp_output_visitor_cleanup(data
->qov
);
43 static void test_visitor_out_int(TestOutputVisitorData
*data
,
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
);
61 static void test_visitor_out_bool(TestOutputVisitorData
*data
,
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
);
79 static void test_visitor_out_number(TestOutputVisitorData
*data
,
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
);
97 static void test_visitor_out_string(TestOutputVisitorData
*data
,
100 char *string
= (char *) "Q E M U";
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
);
115 static void test_visitor_out_no_string(TestOutputVisitorData
*data
,
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
)), ==, "");
134 static void test_visitor_out_enum(TestOutputVisitorData
*data
,
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
)), ==,
154 static void test_visitor_out_enum_errors(TestOutputVisitorData
*data
,
157 EnumOne i
, bad_values
[] = { ENUM_ONE_MAX
, -1 };
160 for (i
= 0; i
< ARRAY_SIZE(bad_values
) ; i
++) {
162 visit_type_EnumOne(data
->ov
, &bad_values
[i
], "unused", &errp
);
163 g_assert(error_is_set(&errp
) == true);
168 typedef struct 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
),
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
,
191 TestStruct test_struct
= { .integer
= 42,
193 .string
= (char *) "foo"};
194 TestStruct
*p
= &test_struct
;
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");
215 static void test_visitor_out_struct_nested(TestOutputVisitorData
*data
,
222 QDict
*qdict
, *dict1
, *dict2
, *dict3
, *userdef
;
223 const char *string
= "user def string";
224 const char *strings
[] = { "forty two", "forty three", "forty four",
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
);
274 qapi_free_UserDefNested(ud2
);
277 typedef struct TestStructList
280 struct TestStructList
*next
;
283 static void visit_type_TestStructList(Visitor
*v
, TestStructList
**obj
,
284 const char *name
, Error
**errp
)
286 GenericList
*i
, **head
= (GenericList
**)obj
;
288 visit_start_list(v
, name
, errp
);
290 for (*head
= i
= visit_next_list(v
, head
, errp
); i
; i
= visit_next_list(v
, &i
, errp
)) {
291 TestStructList
*native_i
= (TestStructList
*)i
;
292 visit_type_TestStruct(v
, &native_i
->value
, NULL
, errp
);
295 visit_end_list(v
, errp
);
298 static void test_visitor_out_list(TestOutputVisitorData
*data
,
301 char *value_str
= (char *) "list value";
302 TestStructList
*p
, *head
= NULL
;
303 const int max_items
= 10;
304 bool value_bool
= true;
312 for (i
= 0; i
< max_items
; i
++) {
313 p
= g_malloc0(sizeof(*p
));
314 p
->value
= g_malloc0(sizeof(*p
->value
));
315 p
->value
->integer
= value_int
;
316 p
->value
->boolean
= value_bool
;
317 p
->value
->string
= value_str
;
323 visit_type_TestStructList(data
->ov
, &head
, NULL
, &errp
);
324 g_assert(!error_is_set(&errp
));
326 obj
= qmp_output_get_qobject(data
->qov
);
327 g_assert(obj
!= NULL
);
328 g_assert(qobject_type(obj
) == QTYPE_QLIST
);
330 qlist
= qobject_to_qlist(obj
);
331 g_assert(!qlist_empty(qlist
));
334 QLIST_FOREACH_ENTRY(qlist
, entry
) {
337 g_assert(qobject_type(entry
->value
) == QTYPE_QDICT
);
338 qdict
= qobject_to_qdict(entry
->value
);
339 g_assert_cmpint(qdict_size(qdict
), ==, 3);
340 g_assert_cmpint(qdict_get_int(qdict
, "integer"), ==, value_int
);
341 g_assert_cmpint(qdict_get_bool(qdict
, "boolean"), ==, value_bool
);
342 g_assert_cmpstr(qdict_get_str(qdict
, "string"), ==, value_str
);
345 g_assert_cmpint(i
, ==, max_items
);
350 TestStructList
*tmp
= p
->next
;
357 static void test_visitor_out_list_qapi_free(TestOutputVisitorData
*data
,
360 UserDefNestedList
*p
, *head
= NULL
;
361 const char string
[] = "foo bar";
362 int i
, max_count
= 1024;
364 for (i
= 0; i
< max_count
; i
++) {
365 p
= g_malloc0(sizeof(*p
));
366 p
->value
= g_malloc0(sizeof(*p
->value
));
368 p
->value
->string0
= g_strdup(string
);
369 p
->value
->dict1
.string1
= g_strdup(string
);
370 p
->value
->dict1
.dict2
.userdef1
= g_malloc0(sizeof(UserDefOne
));
371 p
->value
->dict1
.dict2
.userdef1
->string
= g_strdup(string
);
372 p
->value
->dict1
.dict2
.userdef1
->integer
= 42;
373 p
->value
->dict1
.dict2
.string2
= g_strdup(string
);
374 p
->value
->dict1
.has_dict3
= false;
380 qapi_free_UserDefNestedList(head
);
383 static void test_visitor_out_union(TestOutputVisitorData
*data
,
386 QObject
*arg
, *qvalue
;
387 QDict
*qdict
, *value
;
391 UserDefUnion
*tmp
= g_malloc0(sizeof(UserDefUnion
));
392 tmp
->kind
= USER_DEF_UNION_KIND_A
;
393 tmp
->a
= g_malloc0(sizeof(UserDefA
));
394 tmp
->a
->boolean
= true;
396 visit_type_UserDefUnion(data
->ov
, &tmp
, NULL
, &err
);
397 g_assert(err
== NULL
);
398 arg
= qmp_output_get_qobject(data
->qov
);
400 g_assert(qobject_type(arg
) == QTYPE_QDICT
);
401 qdict
= qobject_to_qdict(arg
);
403 g_assert_cmpstr(qdict_get_str(qdict
, "type"), ==, "a");
405 qvalue
= qdict_get(qdict
, "data");
406 g_assert(data
!= NULL
);
407 g_assert(qobject_type(qvalue
) == QTYPE_QDICT
);
408 value
= qobject_to_qdict(qvalue
);
409 g_assert_cmpint(qdict_get_bool(value
, "boolean"), ==, true);
411 qapi_free_UserDefUnion(tmp
);
415 static void output_visitor_test_add(const char *testpath
,
416 TestOutputVisitorData
*data
,
417 void (*test_func
)(TestOutputVisitorData
*data
, const void *user_data
))
419 g_test_add(testpath
, TestOutputVisitorData
, data
, visitor_output_setup
,
420 test_func
, visitor_output_teardown
);
423 int main(int argc
, char **argv
)
425 TestOutputVisitorData out_visitor_data
;
427 g_test_init(&argc
, &argv
, NULL
);
429 output_visitor_test_add("/visitor/output/int",
430 &out_visitor_data
, test_visitor_out_int
);
431 output_visitor_test_add("/visitor/output/bool",
432 &out_visitor_data
, test_visitor_out_bool
);
433 output_visitor_test_add("/visitor/output/number",
434 &out_visitor_data
, test_visitor_out_number
);
435 output_visitor_test_add("/visitor/output/string",
436 &out_visitor_data
, test_visitor_out_string
);
437 output_visitor_test_add("/visitor/output/no-string",
438 &out_visitor_data
, test_visitor_out_no_string
);
439 output_visitor_test_add("/visitor/output/enum",
440 &out_visitor_data
, test_visitor_out_enum
);
441 output_visitor_test_add("/visitor/output/enum-errors",
442 &out_visitor_data
, test_visitor_out_enum_errors
);
443 output_visitor_test_add("/visitor/output/struct",
444 &out_visitor_data
, test_visitor_out_struct
);
445 output_visitor_test_add("/visitor/output/struct-nested",
446 &out_visitor_data
, test_visitor_out_struct_nested
);
447 output_visitor_test_add("/visitor/output/list",
448 &out_visitor_data
, test_visitor_out_list
);
449 output_visitor_test_add("/visitor/output/list-qapi-free",
450 &out_visitor_data
, test_visitor_out_list_qapi_free
);
451 output_visitor_test_add("/visitor/output/union",
452 &out_visitor_data
, test_visitor_out_union
);