qapi: Plug leaks in test-qmp-*
[qemu/cris-port.git] / tests / test-qmp-input-strict.c
blob821efe025ce1ed484c842c307308e8d0f6fbb2fe
1 /*
2 * QMP Input Visitor unit-tests (strict mode).
4 * Copyright (C) 2011-2012, 2015 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"
22 #include "test-qmp-introspect.h"
23 #include "qmp-introspect.h"
24 #include "qapi-visit.h"
26 typedef struct TestInputVisitorData {
27 QObject *obj;
28 QmpInputVisitor *qiv;
29 } TestInputVisitorData;
31 static void validate_teardown(TestInputVisitorData *data,
32 const void *unused)
34 qobject_decref(data->obj);
35 data->obj = NULL;
37 if (data->qiv) {
38 qmp_input_visitor_cleanup(data->qiv);
39 data->qiv = NULL;
43 /* The various test_init functions are provided instead of a test setup
44 function so that the JSON string used by the tests are kept in the test
45 functions (and not in main()). */
46 static Visitor *validate_test_init_internal(TestInputVisitorData *data,
47 const char *json_string,
48 va_list *ap)
50 Visitor *v;
52 validate_teardown(data, NULL);
54 data->obj = qobject_from_jsonv(json_string, ap);
55 g_assert(data->obj);
57 data->qiv = qmp_input_visitor_new_strict(data->obj);
58 g_assert(data->qiv);
60 v = qmp_input_get_visitor(data->qiv);
61 g_assert(v);
63 return v;
66 static GCC_FMT_ATTR(2, 3)
67 Visitor *validate_test_init(TestInputVisitorData *data,
68 const char *json_string, ...)
70 Visitor *v;
71 va_list ap;
73 va_start(ap, json_string);
74 v = validate_test_init_internal(data, json_string, &ap);
75 va_end(ap);
76 return v;
79 /* similar to validate_test_init(), but does not expect a string
80 * literal/format json_string argument and so can be used for
81 * programatically generated strings (and we can't pass in programatically
82 * generated strings via %s format parameters since qobject_from_jsonv()
83 * will wrap those in double-quotes and treat the entire object as a
84 * string)
86 static Visitor *validate_test_init_raw(TestInputVisitorData *data,
87 const char *json_string)
89 return validate_test_init_internal(data, json_string, NULL);
93 static void test_validate_struct(TestInputVisitorData *data,
94 const void *unused)
96 TestStruct *p = NULL;
97 Error *err = NULL;
98 Visitor *v;
100 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
102 visit_type_TestStruct(v, &p, NULL, &err);
103 g_assert(!err);
104 g_free(p->string);
105 g_free(p);
108 static void test_validate_struct_nested(TestInputVisitorData *data,
109 const void *unused)
111 UserDefTwo *udp = NULL;
112 Error *err = NULL;
113 Visitor *v;
115 v = validate_test_init(data, "{ 'string0': 'string0', "
116 "'dict1': { 'string1': 'string1', "
117 "'dict2': { 'userdef': { 'integer': 42, "
118 "'string': 'string' }, 'string': 'string2'}}}");
120 visit_type_UserDefTwo(v, &udp, NULL, &err);
121 g_assert(!err);
122 qapi_free_UserDefTwo(udp);
125 static void test_validate_list(TestInputVisitorData *data,
126 const void *unused)
128 UserDefOneList *head = NULL;
129 Error *err = NULL;
130 Visitor *v;
132 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
134 visit_type_UserDefOneList(v, &head, NULL, &err);
135 g_assert(!err);
136 qapi_free_UserDefOneList(head);
139 static void test_validate_union_native_list(TestInputVisitorData *data,
140 const void *unused)
142 UserDefNativeListUnion *tmp = NULL;
143 Visitor *v;
144 Error *err = NULL;
146 v = validate_test_init(data, "{ 'type': 'integer', 'data' : [ 1, 2 ] }");
148 visit_type_UserDefNativeListUnion(v, &tmp, NULL, &err);
149 g_assert(!err);
150 qapi_free_UserDefNativeListUnion(tmp);
153 static void test_validate_union_flat(TestInputVisitorData *data,
154 const void *unused)
156 UserDefFlatUnion *tmp = NULL;
157 Visitor *v;
158 Error *err = NULL;
160 v = validate_test_init(data,
161 "{ 'enum1': 'value1', "
162 "'integer': 41, "
163 "'string': 'str', "
164 "'boolean': true }");
166 visit_type_UserDefFlatUnion(v, &tmp, NULL, &err);
167 g_assert(!err);
168 qapi_free_UserDefFlatUnion(tmp);
171 static void test_validate_alternate(TestInputVisitorData *data,
172 const void *unused)
174 UserDefAlternate *tmp = NULL;
175 Visitor *v;
176 Error *err = NULL;
178 v = validate_test_init(data, "42");
180 visit_type_UserDefAlternate(v, &tmp, NULL, &err);
181 g_assert(!err);
182 qapi_free_UserDefAlternate(tmp);
185 static void test_validate_fail_struct(TestInputVisitorData *data,
186 const void *unused)
188 TestStruct *p = NULL;
189 Error *err = NULL;
190 Visitor *v;
192 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
194 visit_type_TestStruct(v, &p, NULL, &err);
195 g_assert(err);
196 error_free(err);
197 if (p) {
198 g_free(p->string);
200 g_free(p);
203 static void test_validate_fail_struct_nested(TestInputVisitorData *data,
204 const void *unused)
206 UserDefTwo *udp = NULL;
207 Error *err = NULL;
208 Visitor *v;
210 v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
212 visit_type_UserDefTwo(v, &udp, NULL, &err);
213 g_assert(err);
214 error_free(err);
215 qapi_free_UserDefTwo(udp);
218 static void test_validate_fail_list(TestInputVisitorData *data,
219 const void *unused)
221 UserDefOneList *head = NULL;
222 Error *err = NULL;
223 Visitor *v;
225 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
227 visit_type_UserDefOneList(v, &head, NULL, &err);
228 g_assert(err);
229 error_free(err);
230 qapi_free_UserDefOneList(head);
233 static void test_validate_fail_union_native_list(TestInputVisitorData *data,
234 const void *unused)
236 UserDefNativeListUnion *tmp = NULL;
237 Error *err = NULL;
238 Visitor *v;
240 v = validate_test_init(data,
241 "{ 'type': 'integer', 'data' : [ 'string' ] }");
243 visit_type_UserDefNativeListUnion(v, &tmp, NULL, &err);
244 g_assert(err);
245 error_free(err);
246 qapi_free_UserDefNativeListUnion(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 error_free(err);
261 qapi_free_UserDefFlatUnion(tmp);
264 static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData *data,
265 const void *unused)
267 UserDefFlatUnion2 *tmp = NULL;
268 Error *err = NULL;
269 Visitor *v;
271 /* test situation where discriminator field ('enum1' here) is missing */
272 v = validate_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
274 visit_type_UserDefFlatUnion2(v, &tmp, NULL, &err);
275 g_assert(err);
276 error_free(err);
277 qapi_free_UserDefFlatUnion2(tmp);
280 static void test_validate_fail_alternate(TestInputVisitorData *data,
281 const void *unused)
283 UserDefAlternate *tmp = NULL;
284 Visitor *v;
285 Error *err = NULL;
287 v = validate_test_init(data, "3.14");
289 visit_type_UserDefAlternate(v, &tmp, NULL, &err);
290 g_assert(err);
291 error_free(err);
292 qapi_free_UserDefAlternate(tmp);
295 static void do_test_validate_qmp_introspect(TestInputVisitorData *data,
296 const char *schema_json)
298 SchemaInfoList *schema = NULL;
299 Error *err = NULL;
300 Visitor *v;
302 v = validate_test_init_raw(data, schema_json);
304 visit_type_SchemaInfoList(v, &schema, NULL, &err);
305 if (err) {
306 fprintf(stderr, "%s", error_get_pretty(err));
308 g_assert(!err);
309 g_assert(schema);
311 qapi_free_SchemaInfoList(schema);
314 static void test_validate_qmp_introspect(TestInputVisitorData *data,
315 const void *unused)
317 do_test_validate_qmp_introspect(data, test_qmp_schema_json);
318 do_test_validate_qmp_introspect(data, qmp_schema_json);
321 static void validate_test_add(const char *testpath,
322 TestInputVisitorData *data,
323 void (*test_func)(TestInputVisitorData *data, const void *user_data))
325 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
326 validate_teardown);
329 int main(int argc, char **argv)
331 TestInputVisitorData testdata;
333 g_test_init(&argc, &argv, NULL);
335 validate_test_add("/visitor/input-strict/pass/struct",
336 &testdata, test_validate_struct);
337 validate_test_add("/visitor/input-strict/pass/struct-nested",
338 &testdata, test_validate_struct_nested);
339 validate_test_add("/visitor/input-strict/pass/list",
340 &testdata, test_validate_list);
341 validate_test_add("/visitor/input-strict/pass/union-flat",
342 &testdata, test_validate_union_flat);
343 validate_test_add("/visitor/input-strict/pass/alternate",
344 &testdata, test_validate_alternate);
345 validate_test_add("/visitor/input-strict/pass/union-native-list",
346 &testdata, test_validate_union_native_list);
347 validate_test_add("/visitor/input-strict/fail/struct",
348 &testdata, test_validate_fail_struct);
349 validate_test_add("/visitor/input-strict/fail/struct-nested",
350 &testdata, test_validate_fail_struct_nested);
351 validate_test_add("/visitor/input-strict/fail/list",
352 &testdata, test_validate_fail_list);
353 validate_test_add("/visitor/input-strict/fail/union-flat",
354 &testdata, test_validate_fail_union_flat);
355 validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator",
356 &testdata, test_validate_fail_union_flat_no_discrim);
357 validate_test_add("/visitor/input-strict/fail/alternate",
358 &testdata, test_validate_fail_alternate);
359 validate_test_add("/visitor/input-strict/fail/union-native-list",
360 &testdata, test_validate_fail_union_native_list);
361 validate_test_add("/visitor/input-strict/pass/qmp-introspect",
362 &testdata, test_validate_qmp_introspect);
364 g_test_run();
366 return 0;