blob: 4087ea3dd3ebd1434c599207bb955a092eb37d71 [file] [log] [blame]
Paolo Bonzinie38ac962012-03-22 12:51:10 +01001/*
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +01002 * QObject Input Visitor unit-tests (strict mode).
Paolo Bonzinie38ac962012-03-22 12:51:10 +01003 *
Eric Blake805017b2015-05-04 09:05:06 -06004 * Copyright (C) 2011-2012, 2015 Red Hat Inc.
Paolo Bonzinie38ac962012-03-22 12:51:10 +01005 *
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
Peter Maydell681c28a2016-02-08 18:08:51 +000014#include "qemu/osdep.h"
Paolo Bonzinie38ac962012-03-22 12:51:10 +010015
Paolo Bonzini79ee7df2012-12-06 11:22:34 +010016#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010017#include "qapi/error.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010018#include "qapi/qobject-input-visitor.h"
Paolo Bonzinie38ac962012-03-22 12:51:10 +010019#include "test-qapi-types.h"
20#include "test-qapi-visit.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010021#include "qapi/qmp/types.h"
Eric Blakec7eb39c2016-06-09 10:48:32 -060022#include "qapi/qmp/qjson.h"
Markus Armbruster39a18152015-09-16 13:06:28 +020023#include "test-qmp-introspect.h"
24#include "qmp-introspect.h"
25#include "qapi-visit.h"
Paolo Bonzinie38ac962012-03-22 12:51:10 +010026
27typedef struct TestInputVisitorData {
28 QObject *obj;
Eric Blakeb70ce102016-06-09 10:48:38 -060029 Visitor *qiv;
Paolo Bonzinie38ac962012-03-22 12:51:10 +010030} 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) {
Eric Blakeb70ce102016-06-09 10:48:38 -060039 visit_free(data->qiv);
Paolo Bonzinie38ac962012-03-22 12:51:10 +010040 data->qiv = NULL;
41 }
42}
43
Eric Blake0920a172015-11-05 23:35:28 -070044/* 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{
Eric Blakeb18f1142015-11-05 23:35:29 -070051 validate_teardown(data, NULL);
52
Eric Blake0920a172015-11-05 23:35:28 -070053 data->obj = qobject_from_jsonv(json_string, ap);
54 g_assert(data->obj);
55
Daniel P. Berrange09e68362016-09-30 15:45:27 +010056 data->qiv = qobject_input_visitor_new(data->obj, true);
Eric Blake0920a172015-11-05 23:35:28 -070057 g_assert(data->qiv);
Eric Blakeb70ce102016-06-09 10:48:38 -060058 return data->qiv;
Eric Blake0920a172015-11-05 23:35:28 -070059}
60
Paolo Bonzinie38ac962012-03-22 12:51:10 +010061static 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);
Eric Blake0920a172015-11-05 23:35:28 -070069 v = validate_test_init_internal(data, json_string, &ap);
Paolo Bonzinie38ac962012-03-22 12:51:10 +010070 va_end(ap);
Paolo Bonzinie38ac962012-03-22 12:51:10 +010071 return v;
72}
73
Markus Armbruster39a18152015-09-16 13:06:28 +020074/* 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{
Eric Blake0920a172015-11-05 23:35:28 -070084 return validate_test_init_internal(data, json_string, NULL);
Markus Armbruster39a18152015-09-16 13:06:28 +020085}
86
Paolo Bonzinie38ac962012-03-22 12:51:10 +010087
88static void test_validate_struct(TestInputVisitorData *data,
89 const void *unused)
90{
91 TestStruct *p = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +010092 Visitor *v;
93
94 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
95
Eric Blake51e72bc2016-01-29 06:48:54 -070096 visit_type_TestStruct(v, NULL, &p, &error_abort);
Paolo Bonzinie38ac962012-03-22 12:51:10 +010097 g_free(p->string);
98 g_free(p);
99}
100
101static void test_validate_struct_nested(TestInputVisitorData *data,
102 const void *unused)
103{
Eric Blakeb6fcf322015-05-04 09:05:29 -0600104 UserDefTwo *udp = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100105 Visitor *v;
106
Eric Blakeb6fcf322015-05-04 09:05:29 -0600107 v = validate_test_init(data, "{ 'string0': 'string0', "
108 "'dict1': { 'string1': 'string1', "
109 "'dict2': { 'userdef': { 'integer': 42, "
110 "'string': 'string' }, 'string': 'string2'}}}");
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100111
Eric Blake51e72bc2016-01-29 06:48:54 -0700112 visit_type_UserDefTwo(v, NULL, &udp, &error_abort);
Eric Blakeb6fcf322015-05-04 09:05:29 -0600113 qapi_free_UserDefTwo(udp);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100114}
115
116static void test_validate_list(TestInputVisitorData *data,
117 const void *unused)
118{
119 UserDefOneList *head = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100120 Visitor *v;
121
122 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
123
Eric Blake51e72bc2016-01-29 06:48:54 -0700124 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100125 qapi_free_UserDefOneList(head);
126}
127
Eric Blake805017b2015-05-04 09:05:06 -0600128static void test_validate_union_native_list(TestInputVisitorData *data,
129 const void *unused)
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100130{
Eric Blake805017b2015-05-04 09:05:06 -0600131 UserDefNativeListUnion *tmp = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100132 Visitor *v;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100133
Eric Blake805017b2015-05-04 09:05:06 -0600134 v = validate_test_init(data, "{ 'type': 'integer', 'data' : [ 1, 2 ] }");
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100135
Eric Blake51e72bc2016-01-29 06:48:54 -0700136 visit_type_UserDefNativeListUnion(v, NULL, &tmp, &error_abort);
Eric Blake805017b2015-05-04 09:05:06 -0600137 qapi_free_UserDefNativeListUnion(tmp);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100138}
139
Markus Armbruster2fc00432014-03-01 08:40:33 +0100140static void test_validate_union_flat(TestInputVisitorData *data,
141 const void *unused)
142{
143 UserDefFlatUnion *tmp = NULL;
144 Visitor *v;
Markus Armbruster2fc00432014-03-01 08:40:33 +0100145
Wenchao Xia52230702014-03-04 18:44:39 -0800146 v = validate_test_init(data,
147 "{ 'enum1': 'value1', "
Markus Armbruster441cbac2015-09-16 13:06:10 +0200148 "'integer': 41, "
Wenchao Xia52230702014-03-04 18:44:39 -0800149 "'string': 'str', "
150 "'boolean': true }");
Markus Armbruster2fc00432014-03-01 08:40:33 +0100151
Eric Blake51e72bc2016-01-29 06:48:54 -0700152 visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort);
Markus Armbruster2fc00432014-03-01 08:40:33 +0100153 qapi_free_UserDefFlatUnion(tmp);
154}
155
Eric Blakeab045262015-05-04 09:05:11 -0600156static void test_validate_alternate(TestInputVisitorData *data,
157 const void *unused)
Markus Armbruster2c38b602014-03-01 08:40:30 +0100158{
Eric Blakeab045262015-05-04 09:05:11 -0600159 UserDefAlternate *tmp = NULL;
Markus Armbruster2c38b602014-03-01 08:40:30 +0100160 Visitor *v;
Markus Armbruster2c38b602014-03-01 08:40:30 +0100161
162 v = validate_test_init(data, "42");
163
Eric Blake51e72bc2016-01-29 06:48:54 -0700164 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
Eric Blakeab045262015-05-04 09:05:11 -0600165 qapi_free_UserDefAlternate(tmp);
Markus Armbruster2c38b602014-03-01 08:40:30 +0100166}
167
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100168static void test_validate_fail_struct(TestInputVisitorData *data,
169 const void *unused)
170{
171 TestStruct *p = NULL;
Markus Armbrustere940f542014-05-02 13:26:29 +0200172 Error *err = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100173 Visitor *v;
174
175 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
176
Eric Blake51e72bc2016-01-29 06:48:54 -0700177 visit_type_TestStruct(v, NULL, &p, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700178 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600179 g_assert(!p);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100180}
181
182static void test_validate_fail_struct_nested(TestInputVisitorData *data,
183 const void *unused)
184{
Eric Blakeb6fcf322015-05-04 09:05:29 -0600185 UserDefTwo *udp = NULL;
Markus Armbrustere940f542014-05-02 13:26:29 +0200186 Error *err = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100187 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
Eric Blake51e72bc2016-01-29 06:48:54 -0700191 visit_type_UserDefTwo(v, NULL, &udp, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700192 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600193 g_assert(!udp);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100194}
195
Markus Armbrusterbce30352016-10-04 17:23:50 +0200196static 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
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100240static void test_validate_fail_list(TestInputVisitorData *data,
241 const void *unused)
242{
243 UserDefOneList *head = NULL;
Markus Armbrustere940f542014-05-02 13:26:29 +0200244 Error *err = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100245 Visitor *v;
246
247 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
248
Eric Blake51e72bc2016-01-29 06:48:54 -0700249 visit_type_UserDefOneList(v, NULL, &head, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700250 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600251 g_assert(!head);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100252}
253
Eric Blake805017b2015-05-04 09:05:06 -0600254static void test_validate_fail_union_native_list(TestInputVisitorData *data,
255 const void *unused)
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100256{
Eric Blake805017b2015-05-04 09:05:06 -0600257 UserDefNativeListUnion *tmp = NULL;
Markus Armbrustere940f542014-05-02 13:26:29 +0200258 Error *err = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100259 Visitor *v;
260
Eric Blake805017b2015-05-04 09:05:06 -0600261 v = validate_test_init(data,
262 "{ 'type': 'integer', 'data' : [ 'string' ] }");
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100263
Eric Blake51e72bc2016-01-29 06:48:54 -0700264 visit_type_UserDefNativeListUnion(v, NULL, &tmp, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700265 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600266 g_assert(!tmp);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100267}
268
Markus Armbruster2fc00432014-03-01 08:40:33 +0100269static void test_validate_fail_union_flat(TestInputVisitorData *data,
270 const void *unused)
271{
272 UserDefFlatUnion *tmp = NULL;
Markus Armbrustere940f542014-05-02 13:26:29 +0200273 Error *err = NULL;
Markus Armbruster2fc00432014-03-01 08:40:33 +0100274 Visitor *v;
275
276 v = validate_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
277
Eric Blake51e72bc2016-01-29 06:48:54 -0700278 visit_type_UserDefFlatUnion(v, NULL, &tmp, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700279 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600280 g_assert(!tmp);
Markus Armbruster2fc00432014-03-01 08:40:33 +0100281}
282
Michael Rothcb551112014-09-18 15:36:42 -0500283static 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 */
Markus Armbruster441cbac2015-09-16 13:06:10 +0200291 v = validate_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
Michael Rothcb551112014-09-18 15:36:42 -0500292
Eric Blake51e72bc2016-01-29 06:48:54 -0700293 visit_type_UserDefFlatUnion2(v, NULL, &tmp, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700294 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600295 g_assert(!tmp);
Michael Rothcb551112014-09-18 15:36:42 -0500296}
297
Eric Blakeab045262015-05-04 09:05:11 -0600298static void test_validate_fail_alternate(TestInputVisitorData *data,
299 const void *unused)
Markus Armbruster2c38b602014-03-01 08:40:30 +0100300{
Eric Blakee58d6952016-04-28 15:45:10 -0600301 UserDefAlternate *tmp;
Markus Armbruster2c38b602014-03-01 08:40:30 +0100302 Visitor *v;
Markus Armbrustere940f542014-05-02 13:26:29 +0200303 Error *err = NULL;
Markus Armbruster2c38b602014-03-01 08:40:30 +0100304
305 v = validate_test_init(data, "3.14");
306
Eric Blake51e72bc2016-01-29 06:48:54 -0700307 visit_type_UserDefAlternate(v, NULL, &tmp, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700308 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600309 g_assert(!tmp);
Markus Armbruster2c38b602014-03-01 08:40:30 +0100310}
311
Markus Armbruster39a18152015-09-16 13:06:28 +0200312static void do_test_validate_qmp_introspect(TestInputVisitorData *data,
313 const char *schema_json)
314{
315 SchemaInfoList *schema = NULL;
Markus Armbruster39a18152015-09-16 13:06:28 +0200316 Visitor *v;
317
318 v = validate_test_init_raw(data, schema_json);
319
Eric Blake51e72bc2016-01-29 06:48:54 -0700320 visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
Markus Armbruster39a18152015-09-16 13:06:28 +0200321 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
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100333static 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",
Eric Blake805017b2015-05-04 09:05:06 -0600348 &testdata, test_validate_struct);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100349 validate_test_add("/visitor/input-strict/pass/struct-nested",
Eric Blake805017b2015-05-04 09:05:06 -0600350 &testdata, test_validate_struct_nested);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100351 validate_test_add("/visitor/input-strict/pass/list",
Eric Blake805017b2015-05-04 09:05:06 -0600352 &testdata, test_validate_list);
Markus Armbruster2fc00432014-03-01 08:40:33 +0100353 validate_test_add("/visitor/input-strict/pass/union-flat",
Eric Blake805017b2015-05-04 09:05:06 -0600354 &testdata, test_validate_union_flat);
Eric Blakeab045262015-05-04 09:05:11 -0600355 validate_test_add("/visitor/input-strict/pass/alternate",
356 &testdata, test_validate_alternate);
Eric Blake805017b2015-05-04 09:05:06 -0600357 validate_test_add("/visitor/input-strict/pass/union-native-list",
358 &testdata, test_validate_union_native_list);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100359 validate_test_add("/visitor/input-strict/fail/struct",
Eric Blake805017b2015-05-04 09:05:06 -0600360 &testdata, test_validate_fail_struct);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100361 validate_test_add("/visitor/input-strict/fail/struct-nested",
Eric Blake805017b2015-05-04 09:05:06 -0600362 &testdata, test_validate_fail_struct_nested);
Markus Armbrusterbce30352016-10-04 17:23:50 +0200363 validate_test_add("/visitor/input-strict/fail/struct-missing",
364 &testdata, test_validate_fail_struct_missing);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100365 validate_test_add("/visitor/input-strict/fail/list",
Eric Blake805017b2015-05-04 09:05:06 -0600366 &testdata, test_validate_fail_list);
Markus Armbruster2fc00432014-03-01 08:40:33 +0100367 validate_test_add("/visitor/input-strict/fail/union-flat",
Eric Blake805017b2015-05-04 09:05:06 -0600368 &testdata, test_validate_fail_union_flat);
Michael Rothcb551112014-09-18 15:36:42 -0500369 validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator",
Eric Blake805017b2015-05-04 09:05:06 -0600370 &testdata, test_validate_fail_union_flat_no_discrim);
Eric Blakeab045262015-05-04 09:05:11 -0600371 validate_test_add("/visitor/input-strict/fail/alternate",
372 &testdata, test_validate_fail_alternate);
Eric Blake805017b2015-05-04 09:05:06 -0600373 validate_test_add("/visitor/input-strict/fail/union-native-list",
374 &testdata, test_validate_fail_union_native_list);
Markus Armbruster39a18152015-09-16 13:06:28 +0200375 validate_test_add("/visitor/input-strict/pass/qmp-introspect",
376 &testdata, test_validate_qmp_introspect);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100377
378 g_test_run();
379
380 return 0;
381}