blob: 4602529ea04d862e35572f4ad5fd001742f0e474 [file] [log] [blame]
Paolo Bonzinie38ac962012-03-22 12:51:10 +01001/*
2 * QMP Input Visitor unit-tests (strict mode).
3 *
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#include <glib.h>
Paolo Bonzinie38ac962012-03-22 12:51:10 +010016
Paolo Bonzini79ee7df2012-12-06 11:22:34 +010017#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010018#include "qapi/error.h"
Paolo Bonzinie38ac962012-03-22 12:51:10 +010019#include "qapi/qmp-input-visitor.h"
20#include "test-qapi-types.h"
21#include "test-qapi-visit.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010022#include "qapi/qmp/types.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;
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
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{
51 Visitor *v;
52
Eric Blakeb18f1142015-11-05 23:35:29 -070053 validate_teardown(data, NULL);
54
Eric Blake0920a172015-11-05 23:35:28 -070055 data->obj = qobject_from_jsonv(json_string, ap);
56 g_assert(data->obj);
57
Eric Blakefc471c12016-04-28 15:45:13 -060058 data->qiv = qmp_input_visitor_new(data->obj, true);
Eric Blake0920a172015-11-05 23:35:28 -070059 g_assert(data->qiv);
60
61 v = qmp_input_get_visitor(data->qiv);
62 g_assert(v);
63
64 return v;
65}
66
Paolo Bonzinie38ac962012-03-22 12:51:10 +010067static 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);
Eric Blake0920a172015-11-05 23:35:28 -070075 v = validate_test_init_internal(data, json_string, &ap);
Paolo Bonzinie38ac962012-03-22 12:51:10 +010076 va_end(ap);
Paolo Bonzinie38ac962012-03-22 12:51:10 +010077 return v;
78}
79
Markus Armbruster39a18152015-09-16 13:06:28 +020080/* 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{
Eric Blake0920a172015-11-05 23:35:28 -070090 return validate_test_init_internal(data, json_string, NULL);
Markus Armbruster39a18152015-09-16 13:06:28 +020091}
92
Paolo Bonzinie38ac962012-03-22 12:51:10 +010093
94static void test_validate_struct(TestInputVisitorData *data,
95 const void *unused)
96{
97 TestStruct *p = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +010098 Visitor *v;
99
100 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
101
Eric Blake51e72bc2016-01-29 06:48:54 -0700102 visit_type_TestStruct(v, NULL, &p, &error_abort);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100103 g_free(p->string);
104 g_free(p);
105}
106
107static void test_validate_struct_nested(TestInputVisitorData *data,
108 const void *unused)
109{
Eric Blakeb6fcf322015-05-04 09:05:29 -0600110 UserDefTwo *udp = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100111 Visitor *v;
112
Eric Blakeb6fcf322015-05-04 09:05:29 -0600113 v = validate_test_init(data, "{ 'string0': 'string0', "
114 "'dict1': { 'string1': 'string1', "
115 "'dict2': { 'userdef': { 'integer': 42, "
116 "'string': 'string' }, 'string': 'string2'}}}");
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100117
Eric Blake51e72bc2016-01-29 06:48:54 -0700118 visit_type_UserDefTwo(v, NULL, &udp, &error_abort);
Eric Blakeb6fcf322015-05-04 09:05:29 -0600119 qapi_free_UserDefTwo(udp);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100120}
121
122static void test_validate_list(TestInputVisitorData *data,
123 const void *unused)
124{
125 UserDefOneList *head = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100126 Visitor *v;
127
128 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
129
Eric Blake51e72bc2016-01-29 06:48:54 -0700130 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100131 qapi_free_UserDefOneList(head);
132}
133
Eric Blake805017b2015-05-04 09:05:06 -0600134static void test_validate_union_native_list(TestInputVisitorData *data,
135 const void *unused)
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100136{
Eric Blake805017b2015-05-04 09:05:06 -0600137 UserDefNativeListUnion *tmp = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100138 Visitor *v;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100139
Eric Blake805017b2015-05-04 09:05:06 -0600140 v = validate_test_init(data, "{ 'type': 'integer', 'data' : [ 1, 2 ] }");
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100141
Eric Blake51e72bc2016-01-29 06:48:54 -0700142 visit_type_UserDefNativeListUnion(v, NULL, &tmp, &error_abort);
Eric Blake805017b2015-05-04 09:05:06 -0600143 qapi_free_UserDefNativeListUnion(tmp);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100144}
145
Markus Armbruster2fc00432014-03-01 08:40:33 +0100146static void test_validate_union_flat(TestInputVisitorData *data,
147 const void *unused)
148{
149 UserDefFlatUnion *tmp = NULL;
150 Visitor *v;
Markus Armbruster2fc00432014-03-01 08:40:33 +0100151
Wenchao Xia52230702014-03-04 18:44:39 -0800152 v = validate_test_init(data,
153 "{ 'enum1': 'value1', "
Markus Armbruster441cbac2015-09-16 13:06:10 +0200154 "'integer': 41, "
Wenchao Xia52230702014-03-04 18:44:39 -0800155 "'string': 'str', "
156 "'boolean': true }");
Markus Armbruster2fc00432014-03-01 08:40:33 +0100157
Eric Blake51e72bc2016-01-29 06:48:54 -0700158 visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort);
Markus Armbruster2fc00432014-03-01 08:40:33 +0100159 qapi_free_UserDefFlatUnion(tmp);
160}
161
Eric Blakeab045262015-05-04 09:05:11 -0600162static void test_validate_alternate(TestInputVisitorData *data,
163 const void *unused)
Markus Armbruster2c38b602014-03-01 08:40:30 +0100164{
Eric Blakeab045262015-05-04 09:05:11 -0600165 UserDefAlternate *tmp = NULL;
Markus Armbruster2c38b602014-03-01 08:40:30 +0100166 Visitor *v;
Markus Armbruster2c38b602014-03-01 08:40:30 +0100167
168 v = validate_test_init(data, "42");
169
Eric Blake51e72bc2016-01-29 06:48:54 -0700170 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
Eric Blakeab045262015-05-04 09:05:11 -0600171 qapi_free_UserDefAlternate(tmp);
Markus Armbruster2c38b602014-03-01 08:40:30 +0100172}
173
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100174static void test_validate_fail_struct(TestInputVisitorData *data,
175 const void *unused)
176{
177 TestStruct *p = NULL;
Markus Armbrustere940f542014-05-02 13:26:29 +0200178 Error *err = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100179 Visitor *v;
180
181 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
182
Eric Blake51e72bc2016-01-29 06:48:54 -0700183 visit_type_TestStruct(v, NULL, &p, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700184 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600185 g_assert(!p);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100186}
187
188static void test_validate_fail_struct_nested(TestInputVisitorData *data,
189 const void *unused)
190{
Eric Blakeb6fcf322015-05-04 09:05:29 -0600191 UserDefTwo *udp = NULL;
Markus Armbrustere940f542014-05-02 13:26:29 +0200192 Error *err = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100193 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
Eric Blake51e72bc2016-01-29 06:48:54 -0700197 visit_type_UserDefTwo(v, NULL, &udp, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700198 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600199 g_assert(!udp);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100200}
201
202static void test_validate_fail_list(TestInputVisitorData *data,
203 const void *unused)
204{
205 UserDefOneList *head = NULL;
Markus Armbrustere940f542014-05-02 13:26:29 +0200206 Error *err = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100207 Visitor *v;
208
209 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
210
Eric Blake51e72bc2016-01-29 06:48:54 -0700211 visit_type_UserDefOneList(v, NULL, &head, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700212 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600213 g_assert(!head);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100214}
215
Eric Blake805017b2015-05-04 09:05:06 -0600216static void test_validate_fail_union_native_list(TestInputVisitorData *data,
217 const void *unused)
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100218{
Eric Blake805017b2015-05-04 09:05:06 -0600219 UserDefNativeListUnion *tmp = NULL;
Markus Armbrustere940f542014-05-02 13:26:29 +0200220 Error *err = NULL;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100221 Visitor *v;
222
Eric Blake805017b2015-05-04 09:05:06 -0600223 v = validate_test_init(data,
224 "{ 'type': 'integer', 'data' : [ 'string' ] }");
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100225
Eric Blake51e72bc2016-01-29 06:48:54 -0700226 visit_type_UserDefNativeListUnion(v, NULL, &tmp, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700227 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600228 g_assert(!tmp);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100229}
230
Markus Armbruster2fc00432014-03-01 08:40:33 +0100231static void test_validate_fail_union_flat(TestInputVisitorData *data,
232 const void *unused)
233{
234 UserDefFlatUnion *tmp = NULL;
Markus Armbrustere940f542014-05-02 13:26:29 +0200235 Error *err = NULL;
Markus Armbruster2fc00432014-03-01 08:40:33 +0100236 Visitor *v;
237
238 v = validate_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
239
Eric Blake51e72bc2016-01-29 06:48:54 -0700240 visit_type_UserDefFlatUnion(v, NULL, &tmp, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700241 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600242 g_assert(!tmp);
Markus Armbruster2fc00432014-03-01 08:40:33 +0100243}
244
Michael Rothcb551112014-09-18 15:36:42 -0500245static 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 */
Markus Armbruster441cbac2015-09-16 13:06:10 +0200253 v = validate_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
Michael Rothcb551112014-09-18 15:36:42 -0500254
Eric Blake51e72bc2016-01-29 06:48:54 -0700255 visit_type_UserDefFlatUnion2(v, NULL, &tmp, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700256 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600257 g_assert(!tmp);
Michael Rothcb551112014-09-18 15:36:42 -0500258}
259
Eric Blakeab045262015-05-04 09:05:11 -0600260static void test_validate_fail_alternate(TestInputVisitorData *data,
261 const void *unused)
Markus Armbruster2c38b602014-03-01 08:40:30 +0100262{
Eric Blakee58d6952016-04-28 15:45:10 -0600263 UserDefAlternate *tmp;
Markus Armbruster2c38b602014-03-01 08:40:30 +0100264 Visitor *v;
Markus Armbrustere940f542014-05-02 13:26:29 +0200265 Error *err = NULL;
Markus Armbruster2c38b602014-03-01 08:40:30 +0100266
267 v = validate_test_init(data, "3.14");
268
Eric Blake51e72bc2016-01-29 06:48:54 -0700269 visit_type_UserDefAlternate(v, NULL, &tmp, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700270 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600271 g_assert(!tmp);
Markus Armbruster2c38b602014-03-01 08:40:30 +0100272}
273
Markus Armbruster39a18152015-09-16 13:06:28 +0200274static void do_test_validate_qmp_introspect(TestInputVisitorData *data,
275 const char *schema_json)
276{
277 SchemaInfoList *schema = NULL;
Markus Armbruster39a18152015-09-16 13:06:28 +0200278 Visitor *v;
279
280 v = validate_test_init_raw(data, schema_json);
281
Eric Blake51e72bc2016-01-29 06:48:54 -0700282 visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
Markus Armbruster39a18152015-09-16 13:06:28 +0200283 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
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100295static 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",
Eric Blake805017b2015-05-04 09:05:06 -0600310 &testdata, test_validate_struct);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100311 validate_test_add("/visitor/input-strict/pass/struct-nested",
Eric Blake805017b2015-05-04 09:05:06 -0600312 &testdata, test_validate_struct_nested);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100313 validate_test_add("/visitor/input-strict/pass/list",
Eric Blake805017b2015-05-04 09:05:06 -0600314 &testdata, test_validate_list);
Markus Armbruster2fc00432014-03-01 08:40:33 +0100315 validate_test_add("/visitor/input-strict/pass/union-flat",
Eric Blake805017b2015-05-04 09:05:06 -0600316 &testdata, test_validate_union_flat);
Eric Blakeab045262015-05-04 09:05:11 -0600317 validate_test_add("/visitor/input-strict/pass/alternate",
318 &testdata, test_validate_alternate);
Eric Blake805017b2015-05-04 09:05:06 -0600319 validate_test_add("/visitor/input-strict/pass/union-native-list",
320 &testdata, test_validate_union_native_list);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100321 validate_test_add("/visitor/input-strict/fail/struct",
Eric Blake805017b2015-05-04 09:05:06 -0600322 &testdata, test_validate_fail_struct);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100323 validate_test_add("/visitor/input-strict/fail/struct-nested",
Eric Blake805017b2015-05-04 09:05:06 -0600324 &testdata, test_validate_fail_struct_nested);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100325 validate_test_add("/visitor/input-strict/fail/list",
Eric Blake805017b2015-05-04 09:05:06 -0600326 &testdata, test_validate_fail_list);
Markus Armbruster2fc00432014-03-01 08:40:33 +0100327 validate_test_add("/visitor/input-strict/fail/union-flat",
Eric Blake805017b2015-05-04 09:05:06 -0600328 &testdata, test_validate_fail_union_flat);
Michael Rothcb551112014-09-18 15:36:42 -0500329 validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator",
Eric Blake805017b2015-05-04 09:05:06 -0600330 &testdata, test_validate_fail_union_flat_no_discrim);
Eric Blakeab045262015-05-04 09:05:11 -0600331 validate_test_add("/visitor/input-strict/fail/alternate",
332 &testdata, test_validate_fail_alternate);
Eric Blake805017b2015-05-04 09:05:06 -0600333 validate_test_add("/visitor/input-strict/fail/union-native-list",
334 &testdata, test_validate_fail_union_native_list);
Markus Armbruster39a18152015-09-16 13:06:28 +0200335 validate_test_add("/visitor/input-strict/pass/qmp-introspect",
336 &testdata, test_validate_qmp_introspect);
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100337
338 g_test_run();
339
340 return 0;
341}