]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-qmp-input-strict.c
qcow2: Inform block layer about discard boundaries
[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;
b70ce101 29 Visitor *qiv;
e38ac962
PB
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) {
b70ce101 39 visit_free(data->qiv);
e38ac962
PB
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{
b18f1141
EB
51 validate_teardown(data, NULL);
52
0920a171
EB
53 data->obj = qobject_from_jsonv(json_string, ap);
54 g_assert(data->obj);
55
fc471c18 56 data->qiv = qmp_input_visitor_new(data->obj, true);
0920a171 57 g_assert(data->qiv);
b70ce101 58 return data->qiv;
0920a171
EB
59}
60
e38ac962
PB
61static GCC_FMT_ATTR(2, 3)
62Visitor *validate_test_init(TestInputVisitorData *data,
63 const char *json_string, ...)
64{
65 Visitor *v;
66 va_list ap;
67
68 va_start(ap, json_string);
0920a171 69 v = validate_test_init_internal(data, json_string, &ap);
e38ac962 70 va_end(ap);
e38ac962
PB
71 return v;
72}
73
39a18158
MA
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)
80 */
81static Visitor *validate_test_init_raw(TestInputVisitorData *data,
82 const char *json_string)
83{
0920a171 84 return validate_test_init_internal(data, json_string, NULL);
39a18158
MA
85}
86
e38ac962
PB
87
88static void test_validate_struct(TestInputVisitorData *data,
89 const void *unused)
90{
91 TestStruct *p = NULL;
e38ac962
PB
92 Visitor *v;
93
94 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
95
51e72bc1 96 visit_type_TestStruct(v, NULL, &p, &error_abort);
e38ac962
PB
97 g_free(p->string);
98 g_free(p);
99}
100
101static void test_validate_struct_nested(TestInputVisitorData *data,
102 const void *unused)
103{
b6fcf32d 104 UserDefTwo *udp = NULL;
e38ac962
PB
105 Visitor *v;
106
b6fcf32d
EB
107 v = validate_test_init(data, "{ 'string0': 'string0', "
108 "'dict1': { 'string1': 'string1', "
109 "'dict2': { 'userdef': { 'integer': 42, "
110 "'string': 'string' }, 'string': 'string2'}}}");
e38ac962 111
51e72bc1 112 visit_type_UserDefTwo(v, NULL, &udp, &error_abort);
b6fcf32d 113 qapi_free_UserDefTwo(udp);
e38ac962
PB
114}
115
116static void test_validate_list(TestInputVisitorData *data,
117 const void *unused)
118{
119 UserDefOneList *head = NULL;
e38ac962
PB
120 Visitor *v;
121
122 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
123
51e72bc1 124 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
e38ac962
PB
125 qapi_free_UserDefOneList(head);
126}
127
805017b7
EB
128static void test_validate_union_native_list(TestInputVisitorData *data,
129 const void *unused)
e38ac962 130{
805017b7 131 UserDefNativeListUnion *tmp = NULL;
e38ac962 132 Visitor *v;
e38ac962 133
805017b7 134 v = validate_test_init(data, "{ 'type': 'integer', 'data' : [ 1, 2 ] }");
e38ac962 135
51e72bc1 136 visit_type_UserDefNativeListUnion(v, NULL, &tmp, &error_abort);
805017b7 137 qapi_free_UserDefNativeListUnion(tmp);
e38ac962
PB
138}
139
2fc00432
MA
140static void test_validate_union_flat(TestInputVisitorData *data,
141 const void *unused)
142{
143 UserDefFlatUnion *tmp = NULL;
144 Visitor *v;
2fc00432 145
5223070c
WX
146 v = validate_test_init(data,
147 "{ 'enum1': 'value1', "
441cbac0 148 "'integer': 41, "
5223070c
WX
149 "'string': 'str', "
150 "'boolean': true }");
2fc00432 151
51e72bc1 152 visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort);
2fc00432
MA
153 qapi_free_UserDefFlatUnion(tmp);
154}
155
ab045267
EB
156static void test_validate_alternate(TestInputVisitorData *data,
157 const void *unused)
2c38b600 158{
ab045267 159 UserDefAlternate *tmp = NULL;
2c38b600 160 Visitor *v;
2c38b600
MA
161
162 v = validate_test_init(data, "42");
163
51e72bc1 164 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
ab045267 165 qapi_free_UserDefAlternate(tmp);
2c38b600
MA
166}
167
e38ac962
PB
168static void test_validate_fail_struct(TestInputVisitorData *data,
169 const void *unused)
170{
171 TestStruct *p = NULL;
e940f543 172 Error *err = NULL;
e38ac962
PB
173 Visitor *v;
174
175 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
176
51e72bc1 177 visit_type_TestStruct(v, NULL, &p, &err);
a12a5a1a 178 error_free_or_abort(&err);
68ab47e4 179 g_assert(!p);
e38ac962
PB
180}
181
182static void test_validate_fail_struct_nested(TestInputVisitorData *data,
183 const void *unused)
184{
b6fcf32d 185 UserDefTwo *udp = NULL;
e940f543 186 Error *err = NULL;
e38ac962
PB
187 Visitor *v;
188
189 v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
190
51e72bc1 191 visit_type_UserDefTwo(v, NULL, &udp, &err);
a12a5a1a 192 error_free_or_abort(&err);
68ab47e4 193 g_assert(!udp);
e38ac962
PB
194}
195
99837b0d
MA
196static void test_validate_fail_struct_missing(TestInputVisitorData *data,
197 const void *unused)
198{
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;
210
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);
238}
239
e38ac962
PB
240static void test_validate_fail_list(TestInputVisitorData *data,
241 const void *unused)
242{
243 UserDefOneList *head = NULL;
e940f543 244 Error *err = NULL;
e38ac962
PB
245 Visitor *v;
246
247 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
248
51e72bc1 249 visit_type_UserDefOneList(v, NULL, &head, &err);
a12a5a1a 250 error_free_or_abort(&err);
68ab47e4 251 g_assert(!head);
e38ac962
PB
252}
253
805017b7
EB
254static void test_validate_fail_union_native_list(TestInputVisitorData *data,
255 const void *unused)
e38ac962 256{
805017b7 257 UserDefNativeListUnion *tmp = NULL;
e940f543 258 Error *err = NULL;
e38ac962
PB
259 Visitor *v;
260
805017b7
EB
261 v = validate_test_init(data,
262 "{ 'type': 'integer', 'data' : [ 'string' ] }");
e38ac962 263
51e72bc1 264 visit_type_UserDefNativeListUnion(v, NULL, &tmp, &err);
a12a5a1a 265 error_free_or_abort(&err);
68ab47e4 266 g_assert(!tmp);
e38ac962
PB
267}
268
2fc00432
MA
269static void test_validate_fail_union_flat(TestInputVisitorData *data,
270 const void *unused)
271{
272 UserDefFlatUnion *tmp = NULL;
e940f543 273 Error *err = NULL;
2fc00432
MA
274 Visitor *v;
275
276 v = validate_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
277
51e72bc1 278 visit_type_UserDefFlatUnion(v, NULL, &tmp, &err);
a12a5a1a 279 error_free_or_abort(&err);
68ab47e4 280 g_assert(!tmp);
2fc00432
MA
281}
282
cb55111b
MR
283static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData *data,
284 const void *unused)
285{
286 UserDefFlatUnion2 *tmp = NULL;
287 Error *err = NULL;
288 Visitor *v;
289
290 /* test situation where discriminator field ('enum1' here) is missing */
441cbac0 291 v = validate_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
cb55111b 292
51e72bc1 293 visit_type_UserDefFlatUnion2(v, NULL, &tmp, &err);
a12a5a1a 294 error_free_or_abort(&err);
68ab47e4 295 g_assert(!tmp);
cb55111b
MR
296}
297
ab045267
EB
298static void test_validate_fail_alternate(TestInputVisitorData *data,
299 const void *unused)
2c38b600 300{
e58d695e 301 UserDefAlternate *tmp;
2c38b600 302 Visitor *v;
e940f543 303 Error *err = NULL;
2c38b600
MA
304
305 v = validate_test_init(data, "3.14");
306
51e72bc1 307 visit_type_UserDefAlternate(v, NULL, &tmp, &err);
a12a5a1a 308 error_free_or_abort(&err);
68ab47e4 309 g_assert(!tmp);
2c38b600
MA
310}
311
39a18158
MA
312static void do_test_validate_qmp_introspect(TestInputVisitorData *data,
313 const char *schema_json)
314{
315 SchemaInfoList *schema = NULL;
39a18158
MA
316 Visitor *v;
317
318 v = validate_test_init_raw(data, schema_json);
319
51e72bc1 320 visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
39a18158
MA
321 g_assert(schema);
322
323 qapi_free_SchemaInfoList(schema);
324}
325
326static void test_validate_qmp_introspect(TestInputVisitorData *data,
327 const void *unused)
328{
329 do_test_validate_qmp_introspect(data, test_qmp_schema_json);
330 do_test_validate_qmp_introspect(data, qmp_schema_json);
331}
332
e38ac962
PB
333static void validate_test_add(const char *testpath,
334 TestInputVisitorData *data,
335 void (*test_func)(TestInputVisitorData *data, const void *user_data))
336{
337 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
338 validate_teardown);
339}
340
341int main(int argc, char **argv)
342{
343 TestInputVisitorData testdata;
344
345 g_test_init(&argc, &argv, NULL);
346
347 validate_test_add("/visitor/input-strict/pass/struct",
805017b7 348 &testdata, test_validate_struct);
e38ac962 349 validate_test_add("/visitor/input-strict/pass/struct-nested",
805017b7 350 &testdata, test_validate_struct_nested);
e38ac962 351 validate_test_add("/visitor/input-strict/pass/list",
805017b7 352 &testdata, test_validate_list);
2fc00432 353 validate_test_add("/visitor/input-strict/pass/union-flat",
805017b7 354 &testdata, test_validate_union_flat);
ab045267
EB
355 validate_test_add("/visitor/input-strict/pass/alternate",
356 &testdata, test_validate_alternate);
805017b7
EB
357 validate_test_add("/visitor/input-strict/pass/union-native-list",
358 &testdata, test_validate_union_native_list);
e38ac962 359 validate_test_add("/visitor/input-strict/fail/struct",
805017b7 360 &testdata, test_validate_fail_struct);
e38ac962 361 validate_test_add("/visitor/input-strict/fail/struct-nested",
805017b7 362 &testdata, test_validate_fail_struct_nested);
99837b0d
MA
363 validate_test_add("/visitor/input-strict/fail/struct-missing",
364 &testdata, test_validate_fail_struct_missing);
e38ac962 365 validate_test_add("/visitor/input-strict/fail/list",
805017b7 366 &testdata, test_validate_fail_list);
2fc00432 367 validate_test_add("/visitor/input-strict/fail/union-flat",
805017b7 368 &testdata, test_validate_fail_union_flat);
cb55111b 369 validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator",
805017b7 370 &testdata, test_validate_fail_union_flat_no_discrim);
ab045267
EB
371 validate_test_add("/visitor/input-strict/fail/alternate",
372 &testdata, test_validate_fail_alternate);
805017b7
EB
373 validate_test_add("/visitor/input-strict/fail/union-native-list",
374 &testdata, test_validate_fail_union_native_list);
39a18158
MA
375 validate_test_add("/visitor/input-strict/pass/qmp-introspect",
376 &testdata, test_validate_qmp_introspect);
e38ac962
PB
377
378 g_test_run();
379
380 return 0;
381}