ui/gtk: Fix build with older versions of gtk
[qemu/ar7.git] / tests / test-qobject-input-strict.c
blob4087ea3dd3ebd1434c599207bb955a092eb37d71
1 /*
2 * QObject 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"
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/qobject-input-visitor.h"
19 #include "test-qapi-types.h"
20 #include "test-qapi-visit.h"
21 #include "qapi/qmp/types.h"
22 #include "qapi/qmp/qjson.h"
23 #include "test-qmp-introspect.h"
24 #include "qmp-introspect.h"
25 #include "qapi-visit.h"
27 typedef struct TestInputVisitorData {
28 QObject *obj;
29 Visitor *qiv;
30 } TestInputVisitorData;
32 static void validate_teardown(TestInputVisitorData *data,
33 const void *unused)
35 qobject_decref(data->obj);
36 data->obj = NULL;
38 if (data->qiv) {
39 visit_free(data->qiv);
40 data->qiv = NULL;
44 /* The various test_init functions are provided instead of a test setup
45 function so that the JSON string used by the tests are kept in the test
46 functions (and not in main()). */
47 static Visitor *validate_test_init_internal(TestInputVisitorData *data,
48 const char *json_string,
49 va_list *ap)
51 validate_teardown(data, NULL);
53 data->obj = qobject_from_jsonv(json_string, ap);
54 g_assert(data->obj);
56 data->qiv = qobject_input_visitor_new(data->obj, true);
57 g_assert(data->qiv);
58 return data->qiv;
61 static GCC_FMT_ATTR(2, 3)
62 Visitor *validate_test_init(TestInputVisitorData *data,
63 const char *json_string, ...)
65 Visitor *v;
66 va_list ap;
68 va_start(ap, json_string);
69 v = validate_test_init_internal(data, json_string, &ap);
70 va_end(ap);
71 return v;
74 /* similar to validate_test_init(), but does not expect a string
75 * literal/format json_string argument and so can be used for
76 * programatically generated strings (and we can't pass in programatically
77 * generated strings via %s format parameters since qobject_from_jsonv()
78 * will wrap those in double-quotes and treat the entire object as a
79 * string)
81 static Visitor *validate_test_init_raw(TestInputVisitorData *data,
82 const char *json_string)
84 return validate_test_init_internal(data, json_string, NULL);
88 static void test_validate_struct(TestInputVisitorData *data,
89 const void *unused)
91 TestStruct *p = NULL;
92 Visitor *v;
94 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
96 visit_type_TestStruct(v, NULL, &p, &error_abort);
97 g_free(p->string);
98 g_free(p);
101 static void test_validate_struct_nested(TestInputVisitorData *data,
102 const void *unused)
104 UserDefTwo *udp = NULL;
105 Visitor *v;
107 v = validate_test_init(data, "{ 'string0': 'string0', "
108 "'dict1': { 'string1': 'string1', "
109 "'dict2': { 'userdef': { 'integer': 42, "
110 "'string': 'string' }, 'string': 'string2'}}}");
112 visit_type_UserDefTwo(v, NULL, &udp, &error_abort);
113 qapi_free_UserDefTwo(udp);
116 static void test_validate_list(TestInputVisitorData *data,
117 const void *unused)
119 UserDefOneList *head = NULL;
120 Visitor *v;
122 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
124 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
125 qapi_free_UserDefOneList(head);
128 static void test_validate_union_native_list(TestInputVisitorData *data,
129 const void *unused)
131 UserDefNativeListUnion *tmp = NULL;
132 Visitor *v;
134 v = validate_test_init(data, "{ 'type': 'integer', 'data' : [ 1, 2 ] }");
136 visit_type_UserDefNativeListUnion(v, NULL, &tmp, &error_abort);
137 qapi_free_UserDefNativeListUnion(tmp);
140 static void test_validate_union_flat(TestInputVisitorData *data,
141 const void *unused)
143 UserDefFlatUnion *tmp = NULL;
144 Visitor *v;
146 v = validate_test_init(data,
147 "{ 'enum1': 'value1', "
148 "'integer': 41, "
149 "'string': 'str', "
150 "'boolean': true }");
152 visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort);
153 qapi_free_UserDefFlatUnion(tmp);
156 static void test_validate_alternate(TestInputVisitorData *data,
157 const void *unused)
159 UserDefAlternate *tmp = NULL;
160 Visitor *v;
162 v = validate_test_init(data, "42");
164 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
165 qapi_free_UserDefAlternate(tmp);
168 static void test_validate_fail_struct(TestInputVisitorData *data,
169 const void *unused)
171 TestStruct *p = NULL;
172 Error *err = NULL;
173 Visitor *v;
175 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
177 visit_type_TestStruct(v, NULL, &p, &err);
178 error_free_or_abort(&err);
179 g_assert(!p);
182 static void test_validate_fail_struct_nested(TestInputVisitorData *data,
183 const void *unused)
185 UserDefTwo *udp = NULL;
186 Error *err = NULL;
187 Visitor *v;
189 v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
191 visit_type_UserDefTwo(v, NULL, &udp, &err);
192 error_free_or_abort(&err);
193 g_assert(!udp);
196 static void test_validate_fail_struct_missing(TestInputVisitorData *data,
197 const void *unused)
199 Error *err = NULL;
200 Visitor *v;
201 QObject *any;
202 GenericAlternate *alt;
203 bool present;
204 int en;
205 int64_t i64;
206 uint32_t u32;
207 int8_t i8;
208 char *str;
209 double dbl;
211 v = validate_test_init(data, "{}");
212 visit_start_struct(v, NULL, NULL, 0, &error_abort);
213 visit_start_struct(v, "struct", NULL, 0, &err);
214 error_free_or_abort(&err);
215 visit_start_list(v, "list", NULL, 0, &err);
216 error_free_or_abort(&err);
217 visit_start_alternate(v, "alternate", &alt, sizeof(*alt), false, &err);
218 error_free_or_abort(&err);
219 visit_optional(v, "optional", &present);
220 g_assert(!present);
221 visit_type_enum(v, "enum", &en, EnumOne_lookup, &err);
222 error_free_or_abort(&err);
223 visit_type_int(v, "i64", &i64, &err);
224 error_free_or_abort(&err);
225 visit_type_uint32(v, "u32", &u32, &err);
226 error_free_or_abort(&err);
227 visit_type_int8(v, "i8", &i8, &err);
228 error_free_or_abort(&err);
229 visit_type_str(v, "i8", &str, &err);
230 error_free_or_abort(&err);
231 visit_type_number(v, "dbl", &dbl, &err);
232 error_free_or_abort(&err);
233 visit_type_any(v, "any", &any, &err);
234 error_free_or_abort(&err);
235 visit_type_null(v, "null", &err);
236 error_free_or_abort(&err);
237 visit_end_struct(v, NULL);
240 static void test_validate_fail_list(TestInputVisitorData *data,
241 const void *unused)
243 UserDefOneList *head = NULL;
244 Error *err = NULL;
245 Visitor *v;
247 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
249 visit_type_UserDefOneList(v, NULL, &head, &err);
250 error_free_or_abort(&err);
251 g_assert(!head);
254 static void test_validate_fail_union_native_list(TestInputVisitorData *data,
255 const void *unused)
257 UserDefNativeListUnion *tmp = NULL;
258 Error *err = NULL;
259 Visitor *v;
261 v = validate_test_init(data,
262 "{ 'type': 'integer', 'data' : [ 'string' ] }");
264 visit_type_UserDefNativeListUnion(v, NULL, &tmp, &err);
265 error_free_or_abort(&err);
266 g_assert(!tmp);
269 static void test_validate_fail_union_flat(TestInputVisitorData *data,
270 const void *unused)
272 UserDefFlatUnion *tmp = NULL;
273 Error *err = NULL;
274 Visitor *v;
276 v = validate_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
278 visit_type_UserDefFlatUnion(v, NULL, &tmp, &err);
279 error_free_or_abort(&err);
280 g_assert(!tmp);
283 static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData *data,
284 const void *unused)
286 UserDefFlatUnion2 *tmp = NULL;
287 Error *err = NULL;
288 Visitor *v;
290 /* test situation where discriminator field ('enum1' here) is missing */
291 v = validate_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
293 visit_type_UserDefFlatUnion2(v, NULL, &tmp, &err);
294 error_free_or_abort(&err);
295 g_assert(!tmp);
298 static void test_validate_fail_alternate(TestInputVisitorData *data,
299 const void *unused)
301 UserDefAlternate *tmp;
302 Visitor *v;
303 Error *err = NULL;
305 v = validate_test_init(data, "3.14");
307 visit_type_UserDefAlternate(v, NULL, &tmp, &err);
308 error_free_or_abort(&err);
309 g_assert(!tmp);
312 static void do_test_validate_qmp_introspect(TestInputVisitorData *data,
313 const char *schema_json)
315 SchemaInfoList *schema = NULL;
316 Visitor *v;
318 v = validate_test_init_raw(data, schema_json);
320 visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
321 g_assert(schema);
323 qapi_free_SchemaInfoList(schema);
326 static void test_validate_qmp_introspect(TestInputVisitorData *data,
327 const void *unused)
329 do_test_validate_qmp_introspect(data, test_qmp_schema_json);
330 do_test_validate_qmp_introspect(data, qmp_schema_json);
333 static void validate_test_add(const char *testpath,
334 TestInputVisitorData *data,
335 void (*test_func)(TestInputVisitorData *data, const void *user_data))
337 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
338 validate_teardown);
341 int main(int argc, char **argv)
343 TestInputVisitorData testdata;
345 g_test_init(&argc, &argv, NULL);
347 validate_test_add("/visitor/input-strict/pass/struct",
348 &testdata, test_validate_struct);
349 validate_test_add("/visitor/input-strict/pass/struct-nested",
350 &testdata, test_validate_struct_nested);
351 validate_test_add("/visitor/input-strict/pass/list",
352 &testdata, test_validate_list);
353 validate_test_add("/visitor/input-strict/pass/union-flat",
354 &testdata, test_validate_union_flat);
355 validate_test_add("/visitor/input-strict/pass/alternate",
356 &testdata, test_validate_alternate);
357 validate_test_add("/visitor/input-strict/pass/union-native-list",
358 &testdata, test_validate_union_native_list);
359 validate_test_add("/visitor/input-strict/fail/struct",
360 &testdata, test_validate_fail_struct);
361 validate_test_add("/visitor/input-strict/fail/struct-nested",
362 &testdata, test_validate_fail_struct_nested);
363 validate_test_add("/visitor/input-strict/fail/struct-missing",
364 &testdata, test_validate_fail_struct_missing);
365 validate_test_add("/visitor/input-strict/fail/list",
366 &testdata, test_validate_fail_list);
367 validate_test_add("/visitor/input-strict/fail/union-flat",
368 &testdata, test_validate_fail_union_flat);
369 validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator",
370 &testdata, test_validate_fail_union_flat_no_discrim);
371 validate_test_add("/visitor/input-strict/fail/alternate",
372 &testdata, test_validate_fail_alternate);
373 validate_test_add("/visitor/input-strict/fail/union-native-list",
374 &testdata, test_validate_fail_union_native_list);
375 validate_test_add("/visitor/input-strict/pass/qmp-introspect",
376 &testdata, test_validate_qmp_introspect);
378 g_test_run();
380 return 0;