migration: show average throughput when migration finishes
[qemu/ar7.git] / tests / test-qmp-input-strict.c
blob449d285e56abfceddc729a658538be1fe8126e4b
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 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
76 errp);
78 visit_type_int(v, &(*obj)->integer, "integer", errp);
79 visit_type_bool(v, &(*obj)->boolean, "boolean", errp);
80 visit_type_str(v, &(*obj)->string, "string", errp);
82 visit_end_struct(v, errp);
85 static void test_validate_struct(TestInputVisitorData *data,
86 const void *unused)
88 TestStruct *p = NULL;
89 Error *err = NULL;
90 Visitor *v;
92 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
94 visit_type_TestStruct(v, &p, NULL, &err);
95 g_assert(!err);
96 g_free(p->string);
97 g_free(p);
100 static void test_validate_struct_nested(TestInputVisitorData *data,
101 const void *unused)
103 UserDefNested *udp = NULL;
104 Error *err = NULL;
105 Visitor *v;
107 v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
109 visit_type_UserDefNested(v, &udp, NULL, &err);
110 g_assert(!err);
111 qapi_free_UserDefNested(udp);
114 static void test_validate_list(TestInputVisitorData *data,
115 const void *unused)
117 UserDefOneList *head = NULL;
118 Error *err = NULL;
119 Visitor *v;
121 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
123 visit_type_UserDefOneList(v, &head, NULL, &err);
124 g_assert(!err);
125 qapi_free_UserDefOneList(head);
128 static void test_validate_union(TestInputVisitorData *data,
129 const void *unused)
131 UserDefUnion *tmp = NULL;
132 Visitor *v;
133 Error *err = NULL;
135 v = validate_test_init(data, "{ 'type': 'b', 'integer': 41, 'data' : { 'integer': 42 } }");
137 visit_type_UserDefUnion(v, &tmp, NULL, &err);
138 g_assert(!err);
139 qapi_free_UserDefUnion(tmp);
142 static void test_validate_union_flat(TestInputVisitorData *data,
143 const void *unused)
145 UserDefFlatUnion *tmp = NULL;
146 Visitor *v;
147 Error *err = NULL;
149 v = validate_test_init(data,
150 "{ 'enum1': 'value1', "
151 "'string': 'str', "
152 "'boolean': true }");
153 /* TODO when generator bug is fixed, add 'integer': 41 */
155 visit_type_UserDefFlatUnion(v, &tmp, NULL, &err);
156 g_assert(!err);
157 qapi_free_UserDefFlatUnion(tmp);
160 static void test_validate_union_anon(TestInputVisitorData *data,
161 const void *unused)
163 UserDefAnonUnion *tmp = NULL;
164 Visitor *v;
165 Error *err = NULL;
167 v = validate_test_init(data, "42");
169 visit_type_UserDefAnonUnion(v, &tmp, NULL, &err);
170 g_assert(!err);
171 qapi_free_UserDefAnonUnion(tmp);
174 static void test_validate_fail_struct(TestInputVisitorData *data,
175 const void *unused)
177 TestStruct *p = NULL;
178 Error *err = NULL;
179 Visitor *v;
181 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
183 visit_type_TestStruct(v, &p, NULL, &err);
184 g_assert(err);
185 if (p) {
186 g_free(p->string);
188 g_free(p);
191 static void test_validate_fail_struct_nested(TestInputVisitorData *data,
192 const void *unused)
194 UserDefNested *udp = NULL;
195 Error *err = NULL;
196 Visitor *v;
198 v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
200 visit_type_UserDefNested(v, &udp, NULL, &err);
201 g_assert(err);
202 qapi_free_UserDefNested(udp);
205 static void test_validate_fail_list(TestInputVisitorData *data,
206 const void *unused)
208 UserDefOneList *head = NULL;
209 Error *err = NULL;
210 Visitor *v;
212 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
214 visit_type_UserDefOneList(v, &head, NULL, &err);
215 g_assert(err);
216 qapi_free_UserDefOneList(head);
219 static void test_validate_fail_union(TestInputVisitorData *data,
220 const void *unused)
222 UserDefUnion *tmp = NULL;
223 Error *err = NULL;
224 Visitor *v;
226 v = validate_test_init(data, "{ 'type': 'b', 'data' : { 'integer': 42 } }");
228 visit_type_UserDefUnion(v, &tmp, NULL, &err);
229 g_assert(err);
230 qapi_free_UserDefUnion(tmp);
233 static void test_validate_fail_union_flat(TestInputVisitorData *data,
234 const void *unused)
236 UserDefFlatUnion *tmp = NULL;
237 Error *err = NULL;
238 Visitor *v;
240 v = validate_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
242 visit_type_UserDefFlatUnion(v, &tmp, NULL, &err);
243 g_assert(err);
244 qapi_free_UserDefFlatUnion(tmp);
247 static void test_validate_fail_union_anon(TestInputVisitorData *data,
248 const void *unused)
250 UserDefAnonUnion *tmp = NULL;
251 Visitor *v;
252 Error *err = NULL;
254 v = validate_test_init(data, "3.14");
256 visit_type_UserDefAnonUnion(v, &tmp, NULL, &err);
257 g_assert(err);
258 qapi_free_UserDefAnonUnion(tmp);
261 static void validate_test_add(const char *testpath,
262 TestInputVisitorData *data,
263 void (*test_func)(TestInputVisitorData *data, const void *user_data))
265 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
266 validate_teardown);
269 int main(int argc, char **argv)
271 TestInputVisitorData testdata;
273 g_test_init(&argc, &argv, NULL);
275 validate_test_add("/visitor/input-strict/pass/struct",
276 &testdata, test_validate_struct);
277 validate_test_add("/visitor/input-strict/pass/struct-nested",
278 &testdata, test_validate_struct_nested);
279 validate_test_add("/visitor/input-strict/pass/list",
280 &testdata, test_validate_list);
281 validate_test_add("/visitor/input-strict/pass/union",
282 &testdata, test_validate_union);
283 validate_test_add("/visitor/input-strict/pass/union-flat",
284 &testdata, test_validate_union_flat);
285 validate_test_add("/visitor/input-strict/pass/union-anon",
286 &testdata, test_validate_union_anon);
287 validate_test_add("/visitor/input-strict/fail/struct",
288 &testdata, test_validate_fail_struct);
289 validate_test_add("/visitor/input-strict/fail/struct-nested",
290 &testdata, test_validate_fail_struct_nested);
291 validate_test_add("/visitor/input-strict/fail/list",
292 &testdata, test_validate_fail_list);
293 validate_test_add("/visitor/input-strict/fail/union",
294 &testdata, test_validate_fail_union);
295 validate_test_add("/visitor/input-strict/fail/union-flat",
296 &testdata, test_validate_fail_union_flat);
297 validate_test_add("/visitor/input-strict/fail/union-anon",
298 &testdata, test_validate_fail_union_anon);
300 g_test_run();
302 return 0;