]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-qmp-input-strict.c
qapi: Improve use of qmp/types.h
[mirror_qemu.git] / tests / test-qmp-input-strict.c
CommitLineData
e38ac962
PB
1/*
2 * QMP Input Visitor unit-tests (strict mode).
3 *
805017b7 4 * Copyright (C) 2011-2012, 2015 Red Hat Inc.
e38ac962
PB
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 * Paolo Bonzini <pbonzini@redhat.com>
9 *
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.
12 */
13
681c28a3 14#include "qemu/osdep.h"
e38ac962 15
79ee7df8 16#include "qemu-common.h"
da34e65c 17#include "qapi/error.h"
e38ac962
PB
18#include "qapi/qmp-input-visitor.h"
19#include "test-qapi-types.h"
20#include "test-qapi-visit.h"
7b1b5d19 21#include "qapi/qmp/types.h"
c7eb39cb 22#include "qapi/qmp/qjson.h"
39a18158
MA
23#include "test-qmp-introspect.h"
24#include "qmp-introspect.h"
25#include "qapi-visit.h"
e38ac962
PB
26
27typedef struct TestInputVisitorData {
28 QObject *obj;
29 QmpInputVisitor *qiv;
30} TestInputVisitorData;
31
32static void validate_teardown(TestInputVisitorData *data,
33 const void *unused)
34{
35 qobject_decref(data->obj);
36 data->obj = NULL;
37
38 if (data->qiv) {
39 qmp_input_visitor_cleanup(data->qiv);
40 data->qiv = NULL;
41 }
42}
43
0920a171
EB
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()). */
47static Visitor *validate_test_init_internal(TestInputVisitorData *data,
48 const char *json_string,
49 va_list *ap)
50{
51 Visitor *v;
52
b18f1141
EB
53 validate_teardown(data, NULL);
54
0920a171
EB
55 data->obj = qobject_from_jsonv(json_string, ap);
56 g_assert(data->obj);
57
fc471c18 58 data->qiv = qmp_input_visitor_new(data->obj, true);
0920a171
EB
59 g_assert(data->qiv);
60
61 v = qmp_input_get_visitor(data->qiv);
62 g_assert(v);
63
64 return v;
65}
66
e38ac962
PB
67static GCC_FMT_ATTR(2, 3)
68Visitor *validate_test_init(TestInputVisitorData *data,
69 const char *json_string, ...)
70{
71 Visitor *v;
72 va_list ap;
73
74 va_start(ap, json_string);
0920a171 75 v = validate_test_init_internal(data, json_string, &ap);
e38ac962 76 va_end(ap);
e38ac962
PB
77 return v;
78}
79
39a18158
MA
80/* similar to validate_test_init(), but does not expect a string
81 * literal/format json_string argument and so can be used for
82 * programatically generated strings (and we can't pass in programatically
83 * generated strings via %s format parameters since qobject_from_jsonv()
84 * will wrap those in double-quotes and treat the entire object as a
85 * string)
86 */
87static Visitor *validate_test_init_raw(TestInputVisitorData *data,
88 const char *json_string)
89{
0920a171 90 return validate_test_init_internal(data, json_string, NULL);
39a18158
MA
91}
92
e38ac962
PB
93
94static void test_validate_struct(TestInputVisitorData *data,
95 const void *unused)
96{
97 TestStruct *p = NULL;
e38ac962
PB
98 Visitor *v;
99
100 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
101
51e72bc1 102 visit_type_TestStruct(v, NULL, &p, &error_abort);
e38ac962
PB
103 g_free(p->string);
104 g_free(p);
105}
106
107static void test_validate_struct_nested(TestInputVisitorData *data,
108 const void *unused)
109{
b6fcf32d 110 UserDefTwo *udp = NULL;
e38ac962
PB
111 Visitor *v;
112
b6fcf32d
EB
113 v = validate_test_init(data, "{ 'string0': 'string0', "
114 "'dict1': { 'string1': 'string1', "
115 "'dict2': { 'userdef': { 'integer': 42, "
116 "'string': 'string' }, 'string': 'string2'}}}");
e38ac962 117
51e72bc1 118 visit_type_UserDefTwo(v, NULL, &udp, &error_abort);
b6fcf32d 119 qapi_free_UserDefTwo(udp);
e38ac962
PB
120}
121
122static void test_validate_list(TestInputVisitorData *data,
123 const void *unused)
124{
125 UserDefOneList *head = NULL;
e38ac962
PB
126 Visitor *v;
127
128 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
129
51e72bc1 130 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
e38ac962
PB
131 qapi_free_UserDefOneList(head);
132}
133
805017b7
EB
134static void test_validate_union_native_list(TestInputVisitorData *data,
135 const void *unused)
e38ac962 136{
805017b7 137 UserDefNativeListUnion *tmp = NULL;
e38ac962 138 Visitor *v;
e38ac962 139
805017b7 140 v = validate_test_init(data, "{ 'type': 'integer', 'data' : [ 1, 2 ] }");
e38ac962 141
51e72bc1 142 visit_type_UserDefNativeListUnion(v, NULL, &tmp, &error_abort);
805017b7 143 qapi_free_UserDefNativeListUnion(tmp);
e38ac962
PB
144}
145
2fc00432
MA
146static void test_validate_union_flat(TestInputVisitorData *data,
147 const void *unused)
148{
149 UserDefFlatUnion *tmp = NULL;
150 Visitor *v;
2fc00432 151
5223070c
WX
152 v = validate_test_init(data,
153 "{ 'enum1': 'value1', "
441cbac0 154 "'integer': 41, "
5223070c
WX
155 "'string': 'str', "
156 "'boolean': true }");
2fc00432 157
51e72bc1 158 visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort);
2fc00432
MA
159 qapi_free_UserDefFlatUnion(tmp);
160}
161
ab045267
EB
162static void test_validate_alternate(TestInputVisitorData *data,
163 const void *unused)
2c38b600 164{
ab045267 165 UserDefAlternate *tmp = NULL;
2c38b600 166 Visitor *v;
2c38b600
MA
167
168 v = validate_test_init(data, "42");
169
51e72bc1 170 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
ab045267 171 qapi_free_UserDefAlternate(tmp);
2c38b600
MA
172}
173
e38ac962
PB
174static void test_validate_fail_struct(TestInputVisitorData *data,
175 const void *unused)
176{
177 TestStruct *p = NULL;
e940f543 178 Error *err = NULL;
e38ac962
PB
179 Visitor *v;
180
181 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
182
51e72bc1 183 visit_type_TestStruct(v, NULL, &p, &err);
a12a5a1a 184 error_free_or_abort(&err);
68ab47e4 185 g_assert(!p);
e38ac962
PB
186}
187
188static void test_validate_fail_struct_nested(TestInputVisitorData *data,
189 const void *unused)
190{
b6fcf32d 191 UserDefTwo *udp = NULL;
e940f543 192 Error *err = NULL;
e38ac962
PB
193 Visitor *v;
194
195 v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
196
51e72bc1 197 visit_type_UserDefTwo(v, NULL, &udp, &err);
a12a5a1a 198 error_free_or_abort(&err);
68ab47e4 199 g_assert(!udp);
e38ac962
PB
200}
201
202static void test_validate_fail_list(TestInputVisitorData *data,
203 const void *unused)
204{
205 UserDefOneList *head = NULL;
e940f543 206 Error *err = NULL;
e38ac962
PB
207 Visitor *v;
208
209 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
210
51e72bc1 211 visit_type_UserDefOneList(v, NULL, &head, &err);
a12a5a1a 212 error_free_or_abort(&err);
68ab47e4 213 g_assert(!head);
e38ac962
PB
214}
215
805017b7
EB
216static void test_validate_fail_union_native_list(TestInputVisitorData *data,
217 const void *unused)
e38ac962 218{
805017b7 219 UserDefNativeListUnion *tmp = NULL;
e940f543 220 Error *err = NULL;
e38ac962
PB
221 Visitor *v;
222
805017b7
EB
223 v = validate_test_init(data,
224 "{ 'type': 'integer', 'data' : [ 'string' ] }");
e38ac962 225
51e72bc1 226 visit_type_UserDefNativeListUnion(v, NULL, &tmp, &err);
a12a5a1a 227 error_free_or_abort(&err);
68ab47e4 228 g_assert(!tmp);
e38ac962
PB
229}
230
2fc00432
MA
231static void test_validate_fail_union_flat(TestInputVisitorData *data,
232 const void *unused)
233{
234 UserDefFlatUnion *tmp = NULL;
e940f543 235 Error *err = NULL;
2fc00432
MA
236 Visitor *v;
237
238 v = validate_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
239
51e72bc1 240 visit_type_UserDefFlatUnion(v, NULL, &tmp, &err);
a12a5a1a 241 error_free_or_abort(&err);
68ab47e4 242 g_assert(!tmp);
2fc00432
MA
243}
244
cb55111b
MR
245static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData *data,
246 const void *unused)
247{
248 UserDefFlatUnion2 *tmp = NULL;
249 Error *err = NULL;
250 Visitor *v;
251
252 /* test situation where discriminator field ('enum1' here) is missing */
441cbac0 253 v = validate_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
cb55111b 254
51e72bc1 255 visit_type_UserDefFlatUnion2(v, NULL, &tmp, &err);
a12a5a1a 256 error_free_or_abort(&err);
68ab47e4 257 g_assert(!tmp);
cb55111b
MR
258}
259
ab045267
EB
260static void test_validate_fail_alternate(TestInputVisitorData *data,
261 const void *unused)
2c38b600 262{
e58d695e 263 UserDefAlternate *tmp;
2c38b600 264 Visitor *v;
e940f543 265 Error *err = NULL;
2c38b600
MA
266
267 v = validate_test_init(data, "3.14");
268
51e72bc1 269 visit_type_UserDefAlternate(v, NULL, &tmp, &err);
a12a5a1a 270 error_free_or_abort(&err);
68ab47e4 271 g_assert(!tmp);
2c38b600
MA
272}
273
39a18158
MA
274static void do_test_validate_qmp_introspect(TestInputVisitorData *data,
275 const char *schema_json)
276{
277 SchemaInfoList *schema = NULL;
39a18158
MA
278 Visitor *v;
279
280 v = validate_test_init_raw(data, schema_json);
281
51e72bc1 282 visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
39a18158
MA
283 g_assert(schema);
284
285 qapi_free_SchemaInfoList(schema);
286}
287
288static void test_validate_qmp_introspect(TestInputVisitorData *data,
289 const void *unused)
290{
291 do_test_validate_qmp_introspect(data, test_qmp_schema_json);
292 do_test_validate_qmp_introspect(data, qmp_schema_json);
293}
294
e38ac962
PB
295static void validate_test_add(const char *testpath,
296 TestInputVisitorData *data,
297 void (*test_func)(TestInputVisitorData *data, const void *user_data))
298{
299 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
300 validate_teardown);
301}
302
303int main(int argc, char **argv)
304{
305 TestInputVisitorData testdata;
306
307 g_test_init(&argc, &argv, NULL);
308
309 validate_test_add("/visitor/input-strict/pass/struct",
805017b7 310 &testdata, test_validate_struct);
e38ac962 311 validate_test_add("/visitor/input-strict/pass/struct-nested",
805017b7 312 &testdata, test_validate_struct_nested);
e38ac962 313 validate_test_add("/visitor/input-strict/pass/list",
805017b7 314 &testdata, test_validate_list);
2fc00432 315 validate_test_add("/visitor/input-strict/pass/union-flat",
805017b7 316 &testdata, test_validate_union_flat);
ab045267
EB
317 validate_test_add("/visitor/input-strict/pass/alternate",
318 &testdata, test_validate_alternate);
805017b7
EB
319 validate_test_add("/visitor/input-strict/pass/union-native-list",
320 &testdata, test_validate_union_native_list);
e38ac962 321 validate_test_add("/visitor/input-strict/fail/struct",
805017b7 322 &testdata, test_validate_fail_struct);
e38ac962 323 validate_test_add("/visitor/input-strict/fail/struct-nested",
805017b7 324 &testdata, test_validate_fail_struct_nested);
e38ac962 325 validate_test_add("/visitor/input-strict/fail/list",
805017b7 326 &testdata, test_validate_fail_list);
2fc00432 327 validate_test_add("/visitor/input-strict/fail/union-flat",
805017b7 328 &testdata, test_validate_fail_union_flat);
cb55111b 329 validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator",
805017b7 330 &testdata, test_validate_fail_union_flat_no_discrim);
ab045267
EB
331 validate_test_add("/visitor/input-strict/fail/alternate",
332 &testdata, test_validate_fail_alternate);
805017b7
EB
333 validate_test_add("/visitor/input-strict/fail/union-native-list",
334 &testdata, test_validate_fail_union_native_list);
39a18158
MA
335 validate_test_add("/visitor/input-strict/pass/qmp-introspect",
336 &testdata, test_validate_qmp_introspect);
e38ac962
PB
337
338 g_test_run();
339
340 return 0;
341}