hyper-v: introduce Hyper-V support infrastructure.
[qemu.git] / test-qmp-input-visitor.c
1 /*
2 * QMP Input Visitor unit-tests.
3 *
4 * Copyright (C) 2011 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
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 #include <stdarg.h>
15
16 #include "qapi/qmp-input-visitor.h"
17 #include "test-qapi-types.h"
18 #include "test-qapi-visit.h"
19 #include "qemu-objects.h"
20
21 typedef struct TestInputVisitorData {
22 QObject *obj;
23 QmpInputVisitor *qiv;
24 } TestInputVisitorData;
25
26 static void visitor_input_teardown(TestInputVisitorData *data,
27 const void *unused)
28 {
29 qobject_decref(data->obj);
30 data->obj = NULL;
31
32 if (data->qiv) {
33 qmp_input_visitor_cleanup(data->qiv);
34 data->qiv = NULL;
35 }
36 }
37
38 /* This is provided instead of a test setup function so that the JSON
39 string used by the tests are kept in the test functions (and not
40 int main()) */
41 static Visitor *visitor_input_test_init(TestInputVisitorData *data,
42 const char *json_string, ...)
43 {
44 Visitor *v;
45 va_list ap;
46
47 va_start(ap, json_string);
48 data->obj = qobject_from_jsonv(json_string, &ap);
49 va_end(ap);
50
51 g_assert(data->obj != NULL);
52
53 data->qiv = qmp_input_visitor_new(data->obj);
54 g_assert(data->qiv != NULL);
55
56 v = qmp_input_get_visitor(data->qiv);
57 g_assert(v != NULL);
58
59 return v;
60 }
61
62 static void test_visitor_in_int(TestInputVisitorData *data,
63 const void *unused)
64 {
65 int64_t res = 0, value = -42;
66 Error *errp = NULL;
67 Visitor *v;
68
69 v = visitor_input_test_init(data, "%d", value);
70
71 visit_type_int(v, &res, NULL, &errp);
72 g_assert(!error_is_set(&errp));
73 g_assert_cmpint(res, ==, value);
74 }
75
76 static void test_visitor_in_bool(TestInputVisitorData *data,
77 const void *unused)
78 {
79 Error *errp = NULL;
80 bool res = false;
81 Visitor *v;
82
83 v = visitor_input_test_init(data, "true");
84
85 visit_type_bool(v, &res, NULL, &errp);
86 g_assert(!error_is_set(&errp));
87 g_assert_cmpint(res, ==, true);
88 }
89
90 static void test_visitor_in_number(TestInputVisitorData *data,
91 const void *unused)
92 {
93 double res = 0, value = 3.14;
94 Error *errp = NULL;
95 Visitor *v;
96
97 v = visitor_input_test_init(data, "%f", value);
98
99 visit_type_number(v, &res, NULL, &errp);
100 g_assert(!error_is_set(&errp));
101 g_assert_cmpfloat(res, ==, value);
102 }
103
104 static void test_visitor_in_string(TestInputVisitorData *data,
105 const void *unused)
106 {
107 char *res = NULL, *value = (char *) "Q E M U";
108 Error *errp = NULL;
109 Visitor *v;
110
111 v = visitor_input_test_init(data, "%s", value);
112
113 visit_type_str(v, &res, NULL, &errp);
114 g_assert(!error_is_set(&errp));
115 g_assert_cmpstr(res, ==, value);
116
117 g_free(res);
118 }
119
120 static void test_visitor_in_enum(TestInputVisitorData *data,
121 const void *unused)
122 {
123 Error *errp = NULL;
124 Visitor *v;
125 EnumOne i;
126
127 for (i = 0; EnumOne_lookup[i]; i++) {
128 EnumOne res = -1;
129
130 v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]);
131
132 visit_type_EnumOne(v, &res, NULL, &errp);
133 g_assert(!error_is_set(&errp));
134 g_assert_cmpint(i, ==, res);
135
136 visitor_input_teardown(data, NULL);
137 }
138
139 data->obj = NULL;
140 data->qiv = NULL;
141 }
142
143 typedef struct TestStruct
144 {
145 int64_t integer;
146 bool boolean;
147 char *string;
148 } TestStruct;
149
150 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
151 const char *name, Error **errp)
152 {
153 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
154 errp);
155
156 visit_type_int(v, &(*obj)->integer, "integer", errp);
157 visit_type_bool(v, &(*obj)->boolean, "boolean", errp);
158 visit_type_str(v, &(*obj)->string, "string", errp);
159
160 visit_end_struct(v, errp);
161 }
162
163 static void test_visitor_in_struct(TestInputVisitorData *data,
164 const void *unused)
165 {
166 TestStruct *p = NULL;
167 Error *errp = NULL;
168 Visitor *v;
169
170 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
171
172 visit_type_TestStruct(v, &p, NULL, &errp);
173 g_assert(!error_is_set(&errp));
174 g_assert_cmpint(p->integer, ==, -42);
175 g_assert(p->boolean == true);
176 g_assert_cmpstr(p->string, ==, "foo");
177
178 g_free(p->string);
179 g_free(p);
180 }
181
182 static void check_and_free_str(char *str, const char *cmp)
183 {
184 g_assert_cmpstr(str, ==, cmp);
185 g_free(str);
186 }
187
188 static void test_visitor_in_struct_nested(TestInputVisitorData *data,
189 const void *unused)
190 {
191 UserDefNested *udp = NULL;
192 Error *errp = NULL;
193 Visitor *v;
194
195 v = visitor_input_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
196
197 visit_type_UserDefNested(v, &udp, NULL, &errp);
198 g_assert(!error_is_set(&errp));
199
200 check_and_free_str(udp->string0, "string0");
201 check_and_free_str(udp->dict1.string1, "string1");
202 g_assert_cmpint(udp->dict1.dict2.userdef1->integer, ==, 42);
203 check_and_free_str(udp->dict1.dict2.userdef1->string, "string");
204 check_and_free_str(udp->dict1.dict2.string2, "string2");
205 g_assert(udp->dict1.has_dict3 == false);
206
207 g_free(udp->dict1.dict2.userdef1);
208 g_free(udp);
209 }
210
211 static void test_visitor_in_list(TestInputVisitorData *data,
212 const void *unused)
213 {
214 UserDefOneList *item, *head = NULL;
215 Error *errp = NULL;
216 Visitor *v;
217 int i;
218
219 v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
220
221 visit_type_UserDefOneList(v, &head, NULL, &errp);
222 g_assert(!error_is_set(&errp));
223 g_assert(head != NULL);
224
225 for (i = 0, item = head; item; item = item->next, i++) {
226 char string[12];
227
228 snprintf(string, sizeof(string), "string%d", i);
229 g_assert_cmpstr(item->value->string, ==, string);
230 g_assert_cmpint(item->value->integer, ==, 42 + i);
231 }
232
233 qapi_free_UserDefOneList(head);
234 }
235
236 static void input_visitor_test_add(const char *testpath,
237 TestInputVisitorData *data,
238 void (*test_func)(TestInputVisitorData *data, const void *user_data))
239 {
240 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
241 visitor_input_teardown);
242 }
243
244 int main(int argc, char **argv)
245 {
246 TestInputVisitorData in_visitor_data;
247
248 g_test_init(&argc, &argv, NULL);
249
250 input_visitor_test_add("/visitor/input/int",
251 &in_visitor_data, test_visitor_in_int);
252 input_visitor_test_add("/visitor/input/bool",
253 &in_visitor_data, test_visitor_in_bool);
254 input_visitor_test_add("/visitor/input/number",
255 &in_visitor_data, test_visitor_in_number);
256 input_visitor_test_add("/visitor/input/string",
257 &in_visitor_data, test_visitor_in_string);
258 input_visitor_test_add("/visitor/input/enum",
259 &in_visitor_data, test_visitor_in_enum);
260 input_visitor_test_add("/visitor/input/struct",
261 &in_visitor_data, test_visitor_in_struct);
262 input_visitor_test_add("/visitor/input/struct-nested",
263 &in_visitor_data, test_visitor_in_struct_nested);
264 input_visitor_test_add("/visitor/input/list",
265 &in_visitor_data, test_visitor_in_list);
266
267 g_test_run();
268
269 return 0;
270 }