rng: switch request queue to QSIMPLEQ
[qemu/kevin.git] / tests / test-qmp-input-strict.c
blob6a33aa41e552bfa711ba86f35296f6e6e91a66a7
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 "qemu/osdep.h"
15 #include <glib.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 Visitor *v;
99 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
101 visit_type_TestStruct(v, NULL, &p, &error_abort);
102 g_free(p->string);
103 g_free(p);
106 static void test_validate_struct_nested(TestInputVisitorData *data,
107 const void *unused)
109 UserDefTwo *udp = NULL;
110 Visitor *v;
112 v = validate_test_init(data, "{ 'string0': 'string0', "
113 "'dict1': { 'string1': 'string1', "
114 "'dict2': { 'userdef': { 'integer': 42, "
115 "'string': 'string' }, 'string': 'string2'}}}");
117 visit_type_UserDefTwo(v, NULL, &udp, &error_abort);
118 qapi_free_UserDefTwo(udp);
121 static void test_validate_list(TestInputVisitorData *data,
122 const void *unused)
124 UserDefOneList *head = NULL;
125 Visitor *v;
127 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
129 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
130 qapi_free_UserDefOneList(head);
133 static void test_validate_union_native_list(TestInputVisitorData *data,
134 const void *unused)
136 UserDefNativeListUnion *tmp = NULL;
137 Visitor *v;
139 v = validate_test_init(data, "{ 'type': 'integer', 'data' : [ 1, 2 ] }");
141 visit_type_UserDefNativeListUnion(v, NULL, &tmp, &error_abort);
142 qapi_free_UserDefNativeListUnion(tmp);
145 static void test_validate_union_flat(TestInputVisitorData *data,
146 const void *unused)
148 UserDefFlatUnion *tmp = NULL;
149 Visitor *v;
151 v = validate_test_init(data,
152 "{ 'enum1': 'value1', "
153 "'integer': 41, "
154 "'string': 'str', "
155 "'boolean': true }");
157 visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort);
158 qapi_free_UserDefFlatUnion(tmp);
161 static void test_validate_alternate(TestInputVisitorData *data,
162 const void *unused)
164 UserDefAlternate *tmp = NULL;
165 Visitor *v;
167 v = validate_test_init(data, "42");
169 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
170 qapi_free_UserDefAlternate(tmp);
173 static void test_validate_fail_struct(TestInputVisitorData *data,
174 const void *unused)
176 TestStruct *p = NULL;
177 Error *err = NULL;
178 Visitor *v;
180 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
182 visit_type_TestStruct(v, NULL, &p, &err);
183 error_free_or_abort(&err);
184 if (p) {
185 g_free(p->string);
187 g_free(p);
190 static void test_validate_fail_struct_nested(TestInputVisitorData *data,
191 const void *unused)
193 UserDefTwo *udp = NULL;
194 Error *err = NULL;
195 Visitor *v;
197 v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
199 visit_type_UserDefTwo(v, NULL, &udp, &err);
200 error_free_or_abort(&err);
201 qapi_free_UserDefTwo(udp);
204 static void test_validate_fail_list(TestInputVisitorData *data,
205 const void *unused)
207 UserDefOneList *head = NULL;
208 Error *err = NULL;
209 Visitor *v;
211 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
213 visit_type_UserDefOneList(v, NULL, &head, &err);
214 error_free_or_abort(&err);
215 qapi_free_UserDefOneList(head);
218 static void test_validate_fail_union_native_list(TestInputVisitorData *data,
219 const void *unused)
221 UserDefNativeListUnion *tmp = NULL;
222 Error *err = NULL;
223 Visitor *v;
225 v = validate_test_init(data,
226 "{ 'type': 'integer', 'data' : [ 'string' ] }");
228 visit_type_UserDefNativeListUnion(v, NULL, &tmp, &err);
229 error_free_or_abort(&err);
230 qapi_free_UserDefNativeListUnion(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, NULL, &tmp, &err);
243 error_free_or_abort(&err);
244 qapi_free_UserDefFlatUnion(tmp);
247 static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData *data,
248 const void *unused)
250 UserDefFlatUnion2 *tmp = NULL;
251 Error *err = NULL;
252 Visitor *v;
254 /* test situation where discriminator field ('enum1' here) is missing */
255 v = validate_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
257 visit_type_UserDefFlatUnion2(v, NULL, &tmp, &err);
258 error_free_or_abort(&err);
259 qapi_free_UserDefFlatUnion2(tmp);
262 static void test_validate_fail_alternate(TestInputVisitorData *data,
263 const void *unused)
265 UserDefAlternate *tmp = NULL;
266 Visitor *v;
267 Error *err = NULL;
269 v = validate_test_init(data, "3.14");
271 visit_type_UserDefAlternate(v, NULL, &tmp, &err);
272 error_free_or_abort(&err);
273 qapi_free_UserDefAlternate(tmp);
276 static void do_test_validate_qmp_introspect(TestInputVisitorData *data,
277 const char *schema_json)
279 SchemaInfoList *schema = NULL;
280 Visitor *v;
282 v = validate_test_init_raw(data, schema_json);
284 visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
285 g_assert(schema);
287 qapi_free_SchemaInfoList(schema);
290 static void test_validate_qmp_introspect(TestInputVisitorData *data,
291 const void *unused)
293 do_test_validate_qmp_introspect(data, test_qmp_schema_json);
294 do_test_validate_qmp_introspect(data, qmp_schema_json);
297 static void validate_test_add(const char *testpath,
298 TestInputVisitorData *data,
299 void (*test_func)(TestInputVisitorData *data, const void *user_data))
301 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
302 validate_teardown);
305 int main(int argc, char **argv)
307 TestInputVisitorData testdata;
309 g_test_init(&argc, &argv, NULL);
311 validate_test_add("/visitor/input-strict/pass/struct",
312 &testdata, test_validate_struct);
313 validate_test_add("/visitor/input-strict/pass/struct-nested",
314 &testdata, test_validate_struct_nested);
315 validate_test_add("/visitor/input-strict/pass/list",
316 &testdata, test_validate_list);
317 validate_test_add("/visitor/input-strict/pass/union-flat",
318 &testdata, test_validate_union_flat);
319 validate_test_add("/visitor/input-strict/pass/alternate",
320 &testdata, test_validate_alternate);
321 validate_test_add("/visitor/input-strict/pass/union-native-list",
322 &testdata, test_validate_union_native_list);
323 validate_test_add("/visitor/input-strict/fail/struct",
324 &testdata, test_validate_fail_struct);
325 validate_test_add("/visitor/input-strict/fail/struct-nested",
326 &testdata, test_validate_fail_struct_nested);
327 validate_test_add("/visitor/input-strict/fail/list",
328 &testdata, test_validate_fail_list);
329 validate_test_add("/visitor/input-strict/fail/union-flat",
330 &testdata, test_validate_fail_union_flat);
331 validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator",
332 &testdata, test_validate_fail_union_flat_no_discrim);
333 validate_test_add("/visitor/input-strict/fail/alternate",
334 &testdata, test_validate_fail_alternate);
335 validate_test_add("/visitor/input-strict/fail/union-native-list",
336 &testdata, test_validate_fail_union_native_list);
337 validate_test_add("/visitor/input-strict/pass/qmp-introspect",
338 &testdata, test_validate_qmp_introspect);
340 g_test_run();
342 return 0;