s390x/kvm: Guest Migration TOD clock synchronization
[qemu.git] / tests / test-qmp-input-strict.c
blobd5360c6a8768481f682e73ff7b675df35b0bc20d
1 /*
2 * QMP Input Visitor unit-tests (strict mode).
4 * Copyright (C) 2011-2012 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 * Paolo Bonzini <pbonzini@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
14 #include <glib.h>
15 #include <stdarg.h>
17 #include "qemu-common.h"
18 #include "qapi/qmp-input-visitor.h"
19 #include "test-qapi-types.h"
20 #include "test-qapi-visit.h"
21 #include "qapi/qmp/types.h"
23 typedef struct TestInputVisitorData {
24 QObject *obj;
25 QmpInputVisitor *qiv;
26 } TestInputVisitorData;
28 static void validate_teardown(TestInputVisitorData *data,
29 const void *unused)
31 qobject_decref(data->obj);
32 data->obj = NULL;
34 if (data->qiv) {
35 qmp_input_visitor_cleanup(data->qiv);
36 data->qiv = NULL;
40 /* This is provided instead of a test setup function so that the JSON
41 string used by the tests are kept in the test functions (and not
42 int main()) */
43 static GCC_FMT_ATTR(2, 3)
44 Visitor *validate_test_init(TestInputVisitorData *data,
45 const char *json_string, ...)
47 Visitor *v;
48 va_list ap;
50 va_start(ap, json_string);
51 data->obj = qobject_from_jsonv(json_string, &ap);
52 va_end(ap);
54 g_assert(data->obj != NULL);
56 data->qiv = qmp_input_visitor_new_strict(data->obj);
57 g_assert(data->qiv != NULL);
59 v = qmp_input_get_visitor(data->qiv);
60 g_assert(v != NULL);
62 return v;
65 typedef struct TestStruct
67 int64_t integer;
68 bool boolean;
69 char *string;
70 } TestStruct;
72 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
73 const char *name, Error **errp)
75 Error *err = NULL;
77 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
78 &err);
79 if (err) {
80 goto out;
83 visit_type_int(v, &(*obj)->integer, "integer", &err);
84 if (err) {
85 goto out_end;
87 visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
88 if (err) {
89 goto out_end;
91 visit_type_str(v, &(*obj)->string, "string", &err);
93 out_end:
94 error_propagate(errp, err);
95 err = NULL;
96 visit_end_struct(v, &err);
97 out:
98 error_propagate(errp, err);
101 static void test_validate_struct(TestInputVisitorData *data,
102 const void *unused)
104 TestStruct *p = NULL;
105 Error *err = NULL;
106 Visitor *v;
108 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
110 visit_type_TestStruct(v, &p, NULL, &err);
111 g_assert(!err);
112 g_free(p->string);
113 g_free(p);
116 static void test_validate_struct_nested(TestInputVisitorData *data,
117 const void *unused)
119 UserDefNested *udp = NULL;
120 Error *err = NULL;
121 Visitor *v;
123 v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
125 visit_type_UserDefNested(v, &udp, NULL, &err);
126 g_assert(!err);
127 qapi_free_UserDefNested(udp);
130 static void test_validate_list(TestInputVisitorData *data,
131 const void *unused)
133 UserDefOneList *head = NULL;
134 Error *err = NULL;
135 Visitor *v;
137 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
139 visit_type_UserDefOneList(v, &head, NULL, &err);
140 g_assert(!err);
141 qapi_free_UserDefOneList(head);
144 static void test_validate_union(TestInputVisitorData *data,
145 const void *unused)
147 UserDefUnion *tmp = NULL;
148 Visitor *v;
149 Error *err = NULL;
151 v = validate_test_init(data, "{ 'type': 'b', 'integer': 41, 'data' : { 'integer': 42 } }");
153 visit_type_UserDefUnion(v, &tmp, NULL, &err);
154 g_assert(!err);
155 qapi_free_UserDefUnion(tmp);
158 static void test_validate_union_flat(TestInputVisitorData *data,
159 const void *unused)
161 UserDefFlatUnion *tmp = NULL;
162 Visitor *v;
163 Error *err = NULL;
165 v = validate_test_init(data,
166 "{ 'enum1': 'value1', "
167 "'string': 'str', "
168 "'boolean': true }");
169 /* TODO when generator bug is fixed, add 'integer': 41 */
171 visit_type_UserDefFlatUnion(v, &tmp, NULL, &err);
172 g_assert(!err);
173 qapi_free_UserDefFlatUnion(tmp);
176 static void test_validate_union_anon(TestInputVisitorData *data,
177 const void *unused)
179 UserDefAnonUnion *tmp = NULL;
180 Visitor *v;
181 Error *err = NULL;
183 v = validate_test_init(data, "42");
185 visit_type_UserDefAnonUnion(v, &tmp, NULL, &err);
186 g_assert(!err);
187 qapi_free_UserDefAnonUnion(tmp);
190 static void test_validate_fail_struct(TestInputVisitorData *data,
191 const void *unused)
193 TestStruct *p = NULL;
194 Error *err = NULL;
195 Visitor *v;
197 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
199 visit_type_TestStruct(v, &p, NULL, &err);
200 g_assert(err);
201 if (p) {
202 g_free(p->string);
204 g_free(p);
207 static void test_validate_fail_struct_nested(TestInputVisitorData *data,
208 const void *unused)
210 UserDefNested *udp = NULL;
211 Error *err = NULL;
212 Visitor *v;
214 v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
216 visit_type_UserDefNested(v, &udp, NULL, &err);
217 g_assert(err);
218 qapi_free_UserDefNested(udp);
221 static void test_validate_fail_list(TestInputVisitorData *data,
222 const void *unused)
224 UserDefOneList *head = NULL;
225 Error *err = NULL;
226 Visitor *v;
228 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
230 visit_type_UserDefOneList(v, &head, NULL, &err);
231 g_assert(err);
232 qapi_free_UserDefOneList(head);
235 static void test_validate_fail_union(TestInputVisitorData *data,
236 const void *unused)
238 UserDefUnion *tmp = NULL;
239 Error *err = NULL;
240 Visitor *v;
242 v = validate_test_init(data, "{ 'type': 'b', 'data' : { 'integer': 42 } }");
244 visit_type_UserDefUnion(v, &tmp, NULL, &err);
245 g_assert(err);
246 qapi_free_UserDefUnion(tmp);
249 static void test_validate_fail_union_flat(TestInputVisitorData *data,
250 const void *unused)
252 UserDefFlatUnion *tmp = NULL;
253 Error *err = NULL;
254 Visitor *v;
256 v = validate_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
258 visit_type_UserDefFlatUnion(v, &tmp, NULL, &err);
259 g_assert(err);
260 qapi_free_UserDefFlatUnion(tmp);
263 static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData *data,
264 const void *unused)
266 UserDefFlatUnion2 *tmp = NULL;
267 Error *err = NULL;
268 Visitor *v;
270 /* test situation where discriminator field ('enum1' here) is missing */
271 v = validate_test_init(data, "{ 'string': 'c', 'string1': 'd', 'string2': 'e' }");
273 visit_type_UserDefFlatUnion2(v, &tmp, NULL, &err);
274 g_assert(err);
275 qapi_free_UserDefFlatUnion2(tmp);
278 static void test_validate_fail_union_anon(TestInputVisitorData *data,
279 const void *unused)
281 UserDefAnonUnion *tmp = NULL;
282 Visitor *v;
283 Error *err = NULL;
285 v = validate_test_init(data, "3.14");
287 visit_type_UserDefAnonUnion(v, &tmp, NULL, &err);
288 g_assert(err);
289 qapi_free_UserDefAnonUnion(tmp);
292 static void validate_test_add(const char *testpath,
293 TestInputVisitorData *data,
294 void (*test_func)(TestInputVisitorData *data, const void *user_data))
296 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
297 validate_teardown);
300 int main(int argc, char **argv)
302 TestInputVisitorData testdata;
304 g_test_init(&argc, &argv, NULL);
306 validate_test_add("/visitor/input-strict/pass/struct",
307 &testdata, test_validate_struct);
308 validate_test_add("/visitor/input-strict/pass/struct-nested",
309 &testdata, test_validate_struct_nested);
310 validate_test_add("/visitor/input-strict/pass/list",
311 &testdata, test_validate_list);
312 validate_test_add("/visitor/input-strict/pass/union",
313 &testdata, test_validate_union);
314 validate_test_add("/visitor/input-strict/pass/union-flat",
315 &testdata, test_validate_union_flat);
316 validate_test_add("/visitor/input-strict/pass/union-anon",
317 &testdata, test_validate_union_anon);
318 validate_test_add("/visitor/input-strict/fail/struct",
319 &testdata, test_validate_fail_struct);
320 validate_test_add("/visitor/input-strict/fail/struct-nested",
321 &testdata, test_validate_fail_struct_nested);
322 validate_test_add("/visitor/input-strict/fail/list",
323 &testdata, test_validate_fail_list);
324 validate_test_add("/visitor/input-strict/fail/union",
325 &testdata, test_validate_fail_union);
326 validate_test_add("/visitor/input-strict/fail/union-flat",
327 &testdata, test_validate_fail_union_flat);
328 validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator",
329 &testdata, test_validate_fail_union_flat_no_discrim);
330 validate_test_add("/visitor/input-strict/fail/union-anon",
331 &testdata, test_validate_fail_union_anon);
333 g_test_run();
335 return 0;