2 * QMP Input 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.
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 "qemu-objects.h"
22 typedef struct TestInputVisitorData {
25 } TestInputVisitorData;
27 static void visitor_input_teardown(TestInputVisitorData *data,
30 qobject_decref(data->obj);
34 qmp_input_visitor_cleanup(data->qiv);
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
42 static GCC_FMT_ATTR(2, 3)
43 Visitor *visitor_input_test_init(TestInputVisitorData *data,
44 const char *json_string, ...)
49 va_start(ap, json_string);
50 data->obj = qobject_from_jsonv(json_string, &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);
64 static void test_visitor_in_int(TestInputVisitorData *data,
67 int64_t res = 0, value = -42;
71 v = visitor_input_test_init(data, "%" PRId64, value);
73 visit_type_int(v, &res, NULL, &errp);
74 g_assert(!error_is_set(&errp));
75 g_assert_cmpint(res, ==, value);
78 static void test_visitor_in_bool(TestInputVisitorData *data,
85 v = visitor_input_test_init(data, "true");
87 visit_type_bool(v, &res, NULL, &errp);
88 g_assert(!error_is_set(&errp));
89 g_assert_cmpint(res, ==, true);
92 static void test_visitor_in_number(TestInputVisitorData *data,
95 double res = 0, value = 3.14;
99 v = visitor_input_test_init(data, "%f", value);
101 visit_type_number(v, &res, NULL, &errp);
102 g_assert(!error_is_set(&errp));
103 g_assert_cmpfloat(res, ==, value);
106 static void test_visitor_in_string(TestInputVisitorData *data,
109 char *res = NULL, *value = (char *) "Q E M U";
113 v = visitor_input_test_init(data, "%s", value);
115 visit_type_str(v, &res, NULL, &errp);
116 g_assert(!error_is_set(&errp));
117 g_assert_cmpstr(res, ==, value);
122 static void test_visitor_in_enum(TestInputVisitorData *data,
129 for (i = 0; EnumOne_lookup[i]; i++) {
132 v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]);
134 visit_type_EnumOne(v, &res, NULL, &errp);
135 g_assert(!error_is_set(&errp));
136 g_assert_cmpint(i, ==, res);
138 visitor_input_teardown(data, NULL);
145 typedef struct TestStruct
152 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
153 const char *name, Error **errp)
156 if (!error_is_set(errp)) {
157 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
160 visit_type_int(v, &(*obj)->integer, "integer", &err);
161 visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
162 visit_type_str(v, &(*obj)->string, "string", &err);
164 /* Always call end_struct if start_struct succeeded. */
165 error_propagate(errp, err);
167 visit_end_struct(v, &err);
169 error_propagate(errp, err);
173 static void test_visitor_in_struct(TestInputVisitorData *data,
176 TestStruct *p = NULL;
180 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
182 visit_type_TestStruct(v, &p, NULL, &errp);
183 g_assert(!error_is_set(&errp));
184 g_assert_cmpint(p->integer, ==, -42);
185 g_assert(p->boolean == true);
186 g_assert_cmpstr(p->string, ==, "foo");
192 static void check_and_free_str(char *str, const char *cmp)
194 g_assert_cmpstr(str, ==, cmp);
198 static void test_visitor_in_struct_nested(TestInputVisitorData *data,
201 UserDefNested *udp = NULL;
205 v = visitor_input_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
207 visit_type_UserDefNested(v, &udp, NULL, &errp);
208 g_assert(!error_is_set(&errp));
210 check_and_free_str(udp->string0, "string0");
211 check_and_free_str(udp->dict1.string1, "string1");
212 g_assert_cmpint(udp->dict1.dict2.userdef1->integer, ==, 42);
213 check_and_free_str(udp->dict1.dict2.userdef1->string, "string");
214 check_and_free_str(udp->dict1.dict2.string2, "string2");
215 g_assert(udp->dict1.has_dict3 == false);
217 g_free(udp->dict1.dict2.userdef1);
221 static void test_visitor_in_list(TestInputVisitorData *data,
224 UserDefOneList *item, *head = NULL;
229 v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
231 visit_type_UserDefOneList(v, &head, NULL, &errp);
232 g_assert(!error_is_set(&errp));
233 g_assert(head != NULL);
235 for (i = 0, item = head; item; item = item->next, i++) {
238 snprintf(string, sizeof(string), "string%d", i);
239 g_assert_cmpstr(item->value->string, ==, string);
240 g_assert_cmpint(item->value->integer, ==, 42 + i);
243 qapi_free_UserDefOneList(head);
246 static void test_visitor_in_union(TestInputVisitorData *data,
253 v = visitor_input_test_init(data, "{ 'type': 'b', 'data' : { 'integer': 42 } }");
255 visit_type_UserDefUnion(v, &tmp, NULL, &err);
256 g_assert(err == NULL);
257 g_assert_cmpint(tmp->kind, ==, USER_DEF_UNION_KIND_B);
258 g_assert_cmpint(tmp->b->integer, ==, 42);
259 qapi_free_UserDefUnion(tmp);
262 static void input_visitor_test_add(const char *testpath,
263 TestInputVisitorData *data,
264 void (*test_func)(TestInputVisitorData *data, const void *user_data))
266 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
267 visitor_input_teardown);
270 static void test_visitor_in_errors(TestInputVisitorData *data,
273 TestStruct *p = NULL;
277 v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }");
279 visit_type_TestStruct(v, &p, NULL, &errp);
280 g_assert(error_is_set(&errp));
281 g_assert(p->string == NULL);
287 int main(int argc, char **argv)
289 TestInputVisitorData in_visitor_data;
291 g_test_init(&argc, &argv, NULL);
293 input_visitor_test_add("/visitor/input/int",
294 &in_visitor_data, test_visitor_in_int);
295 input_visitor_test_add("/visitor/input/bool",
296 &in_visitor_data, test_visitor_in_bool);
297 input_visitor_test_add("/visitor/input/number",
298 &in_visitor_data, test_visitor_in_number);
299 input_visitor_test_add("/visitor/input/string",
300 &in_visitor_data, test_visitor_in_string);
301 input_visitor_test_add("/visitor/input/enum",
302 &in_visitor_data, test_visitor_in_enum);
303 input_visitor_test_add("/visitor/input/struct",
304 &in_visitor_data, test_visitor_in_struct);
305 input_visitor_test_add("/visitor/input/struct-nested",
306 &in_visitor_data, test_visitor_in_struct_nested);
307 input_visitor_test_add("/visitor/input/list",
308 &in_visitor_data, test_visitor_in_list);
309 input_visitor_test_add("/visitor/input/union",
310 &in_visitor_data, test_visitor_in_union);
311 input_visitor_test_add("/visitor/input/errors",
312 &in_visitor_data, test_visitor_in_errors);