blob: 6bacabf063222b62d22c40bd09b1c79b72100fe8 [file] [log] [blame]
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -02001/*
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +01002 * QObject Input Visitor unit-tests.
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -02003 *
Eric Blake68d07832016-02-17 23:48:18 -07004 * Copyright (C) 2011-2016 Red Hat Inc.
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -02005 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
Markus Armbruster77c47de2017-03-03 13:32:40 +01008 * Paolo Bonzini <pbonzini@redhat.com>
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -02009 *
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"
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -020015
Paolo Bonzini79ee7df2012-12-06 11:22:34 +010016#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010017#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060018#include "qapi/qapi-visit-introspect.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010019#include "qapi/qobject-input-visitor.h"
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -020020#include "test-qapi-visit.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010021#include "qapi/qmp/qbool.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010022#include "qapi/qmp/qdict.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010023#include "qapi/qmp/qnull.h"
24#include "qapi/qmp/qnum.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010025#include "qapi/qmp/qstring.h"
Eric Blakec7eb39c2016-06-09 10:48:32 -060026#include "qapi/qmp/qjson.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010027#include "test-qapi-introspect.h"
28#include "qapi/qapi-introspect.h"
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -020029
30typedef struct TestInputVisitorData {
31 QObject *obj;
Eric Blakeb70ce102016-06-09 10:48:38 -060032 Visitor *qiv;
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -020033} TestInputVisitorData;
34
35static void visitor_input_teardown(TestInputVisitorData *data,
36 const void *unused)
37{
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020038 qobject_unref(data->obj);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -020039 data->obj = NULL;
40
41 if (data->qiv) {
Eric Blakeb70ce102016-06-09 10:48:38 -060042 visit_free(data->qiv);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -020043 data->qiv = NULL;
44 }
45}
46
Eric Blake0920a172015-11-05 23:35:28 -070047/* The various test_init functions are provided instead of a test setup
48 function so that the JSON string used by the tests are kept in the test
49 functions (and not in main()). */
Markus Armbrustereac78bd2018-08-06 08:53:30 +020050
51static Visitor *test_init_internal(TestInputVisitorData *data, bool keyval,
52 QObject *obj)
Eric Blake0920a172015-11-05 23:35:28 -070053{
Eric Blakeb18f1142015-11-05 23:35:29 -070054 visitor_input_teardown(data, NULL);
55
Markus Armbrustereac78bd2018-08-06 08:53:30 +020056 data->obj = obj;
Eric Blake0920a172015-11-05 23:35:28 -070057
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +010058 if (keyval) {
59 data->qiv = qobject_input_visitor_new_keyval(data->obj);
60 } else {
61 data->qiv = qobject_input_visitor_new(data->obj);
62 }
Eric Blake0920a172015-11-05 23:35:28 -070063 g_assert(data->qiv);
Eric Blakeb70ce102016-06-09 10:48:38 -060064 return data->qiv;
Eric Blake0920a172015-11-05 23:35:28 -070065}
66
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +010067static GCC_FMT_ATTR(3, 4)
68Visitor *visitor_input_test_init_full(TestInputVisitorData *data,
69 bool keyval,
70 const char *json_string, ...)
71{
72 Visitor *v;
73 va_list ap;
74
75 va_start(ap, json_string);
Markus Armbrustereac78bd2018-08-06 08:53:30 +020076 v = test_init_internal(data, keyval,
77 qobject_from_vjsonf_nofail(json_string, ap));
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +010078 va_end(ap);
79 return v;
80}
81
Stefan Weilaba21072011-12-23 20:34:38 +010082static GCC_FMT_ATTR(2, 3)
83Visitor *visitor_input_test_init(TestInputVisitorData *data,
84 const char *json_string, ...)
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -020085{
86 Visitor *v;
87 va_list ap;
88
89 va_start(ap, json_string);
Markus Armbrustereac78bd2018-08-06 08:53:30 +020090 v = test_init_internal(data, false,
91 qobject_from_vjsonf_nofail(json_string, ap));
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -020092 va_end(ap);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -020093 return v;
94}
95
Michael Roth199e0f12013-05-10 17:46:10 -050096/* similar to visitor_input_test_init(), but does not expect a string
97 * literal/format json_string argument and so can be used for
98 * programatically generated strings (and we can't pass in programatically
99 * generated strings via %s format parameters since qobject_from_jsonv()
100 * will wrap those in double-quotes and treat the entire object as a
101 * string)
102 */
103static Visitor *visitor_input_test_init_raw(TestInputVisitorData *data,
104 const char *json_string)
105{
Markus Armbrustereac78bd2018-08-06 08:53:30 +0200106 return test_init_internal(data, false,
107 qobject_from_json(json_string, &error_abort));
Michael Roth199e0f12013-05-10 17:46:10 -0500108}
109
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200110static void test_visitor_in_int(TestInputVisitorData *data,
111 const void *unused)
112{
Eric Blake29a67312016-11-23 11:36:56 -0600113 int64_t res = 0;
Marc-André Lureauc1214ad2017-06-07 20:35:56 +0400114 double dbl;
Eric Blake29a67312016-11-23 11:36:56 -0600115 int value = -42;
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200116 Visitor *v;
117
Eric Blake29a67312016-11-23 11:36:56 -0600118 v = visitor_input_test_init(data, "%d", value);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200119
Eric Blake51e72bc2016-01-29 06:48:54 -0700120 visit_type_int(v, NULL, &res, &error_abort);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200121 g_assert_cmpint(res, ==, value);
Marc-André Lureauc1214ad2017-06-07 20:35:56 +0400122
123 visit_type_number(v, NULL, &dbl, &error_abort);
124 g_assert_cmpfloat(dbl, ==, -42.0);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200125}
126
Markus Armbruster4bc0c942017-03-21 18:44:50 +0100127static void test_visitor_in_uint(TestInputVisitorData *data,
128 const void *unused)
129{
Markus Armbruster4bc0c942017-03-21 18:44:50 +0100130 uint64_t res = 0;
Marc-André Lureauc1214ad2017-06-07 20:35:56 +0400131 int64_t i64;
132 double dbl;
Markus Armbruster4bc0c942017-03-21 18:44:50 +0100133 int value = 42;
134 Visitor *v;
135
136 v = visitor_input_test_init(data, "%d", value);
137
138 visit_type_uint64(v, NULL, &res, &error_abort);
139 g_assert_cmpuint(res, ==, (uint64_t)value);
140
Marc-André Lureauc1214ad2017-06-07 20:35:56 +0400141 visit_type_int(v, NULL, &i64, &error_abort);
142 g_assert_cmpint(i64, ==, value);
Markus Armbruster4bc0c942017-03-21 18:44:50 +0100143
Marc-André Lureauc1214ad2017-06-07 20:35:56 +0400144 visit_type_number(v, NULL, &dbl, &error_abort);
145 g_assert_cmpfloat(dbl, ==, value);
146
147 /* BUG: value between INT64_MIN and -1 accepted modulo 2^64 */
Markus Armbruster4bc0c942017-03-21 18:44:50 +0100148 v = visitor_input_test_init(data, "%d", -value);
149
150 visit_type_uint64(v, NULL, &res, &error_abort);
151 g_assert_cmpuint(res, ==, (uint64_t)-value);
152
Markus Armbruster4bc0c942017-03-21 18:44:50 +0100153 v = visitor_input_test_init(data, "18446744073709551574");
154
Marc-André Lureau5923f852017-06-07 20:36:03 +0400155 visit_type_uint64(v, NULL, &res, &error_abort);
156 g_assert_cmpuint(res, ==, 18446744073709551574U);
Marc-André Lureauc1214ad2017-06-07 20:35:56 +0400157
158 visit_type_number(v, NULL, &dbl, &error_abort);
159 g_assert_cmpfloat(dbl, ==, 18446744073709552000.0);
Markus Armbruster4bc0c942017-03-21 18:44:50 +0100160}
161
Michael Rothe92cfa02013-05-10 17:46:06 -0500162static void test_visitor_in_int_overflow(TestInputVisitorData *data,
163 const void *unused)
164{
165 int64_t res = 0;
Markus Armbrustere940f542014-05-02 13:26:29 +0200166 Error *err = NULL;
Michael Rothe92cfa02013-05-10 17:46:06 -0500167 Visitor *v;
168
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400169 /*
170 * This will overflow a QNUM_I64, so should be deserialized into a
171 * QNUM_DOUBLE field instead, leading to an error if we pass it to
172 * visit_type_int(). Confirm this.
Michael Rothe92cfa02013-05-10 17:46:06 -0500173 */
174 v = visitor_input_test_init(data, "%f", DBL_MAX);
175
Eric Blake51e72bc2016-01-29 06:48:54 -0700176 visit_type_int(v, NULL, &res, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700177 error_free_or_abort(&err);
Michael Rothe92cfa02013-05-10 17:46:06 -0500178}
179
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100180static void test_visitor_in_int_keyval(TestInputVisitorData *data,
181 const void *unused)
182{
183 int64_t res = 0, value = -42;
184 Error *err = NULL;
185 Visitor *v;
186
187 v = visitor_input_test_init_full(data, true, "%" PRId64, value);
188 visit_type_int(v, NULL, &res, &err);
189 error_free_or_abort(&err);
190}
191
192static void test_visitor_in_int_str_keyval(TestInputVisitorData *data,
193 const void *unused)
194{
195 int64_t res = 0, value = -42;
196 Visitor *v;
197
198 v = visitor_input_test_init_full(data, true, "\"-42\"");
199
200 visit_type_int(v, NULL, &res, &error_abort);
201 g_assert_cmpint(res, ==, value);
202}
203
204static void test_visitor_in_int_str_fail(TestInputVisitorData *data,
205 const void *unused)
206{
207 int64_t res = 0;
208 Visitor *v;
209 Error *err = NULL;
210
211 v = visitor_input_test_init(data, "\"-42\"");
212
213 visit_type_int(v, NULL, &res, &err);
214 error_free_or_abort(&err);
215}
216
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200217static void test_visitor_in_bool(TestInputVisitorData *data,
218 const void *unused)
219{
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200220 bool res = false;
221 Visitor *v;
222
223 v = visitor_input_test_init(data, "true");
224
Eric Blake51e72bc2016-01-29 06:48:54 -0700225 visit_type_bool(v, NULL, &res, &error_abort);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200226 g_assert_cmpint(res, ==, true);
227}
228
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100229static void test_visitor_in_bool_keyval(TestInputVisitorData *data,
230 const void *unused)
231{
232 bool res = false;
233 Error *err = NULL;
234 Visitor *v;
235
236 v = visitor_input_test_init_full(data, true, "true");
237
238 visit_type_bool(v, NULL, &res, &err);
239 error_free_or_abort(&err);
240}
241
242static void test_visitor_in_bool_str_keyval(TestInputVisitorData *data,
243 const void *unused)
244{
245 bool res = false;
246 Visitor *v;
247
248 v = visitor_input_test_init_full(data, true, "\"on\"");
249
250 visit_type_bool(v, NULL, &res, &error_abort);
251 g_assert_cmpint(res, ==, true);
252}
253
254static void test_visitor_in_bool_str_fail(TestInputVisitorData *data,
255 const void *unused)
256{
257 bool res = false;
258 Visitor *v;
259 Error *err = NULL;
260
261 v = visitor_input_test_init(data, "\"true\"");
262
263 visit_type_bool(v, NULL, &res, &err);
264 error_free_or_abort(&err);
265}
266
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200267static void test_visitor_in_number(TestInputVisitorData *data,
268 const void *unused)
269{
270 double res = 0, value = 3.14;
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200271 Visitor *v;
272
273 v = visitor_input_test_init(data, "%f", value);
274
Eric Blake51e72bc2016-01-29 06:48:54 -0700275 visit_type_number(v, NULL, &res, &error_abort);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200276 g_assert_cmpfloat(res, ==, value);
277}
278
Marc-André Lureauc1214ad2017-06-07 20:35:56 +0400279static void test_visitor_in_large_number(TestInputVisitorData *data,
280 const void *unused)
281{
282 Error *err = NULL;
283 double res = 0;
284 int64_t i64;
285 uint64_t u64;
286 Visitor *v;
287
288 v = visitor_input_test_init(data, "-18446744073709551616"); /* -2^64 */
289
290 visit_type_number(v, NULL, &res, &error_abort);
291 g_assert_cmpfloat(res, ==, -18446744073709552e3);
292
293 visit_type_int(v, NULL, &i64, &err);
294 error_free_or_abort(&err);
295
296 visit_type_uint64(v, NULL, &u64, &err);
297 error_free_or_abort(&err);
298}
299
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100300static void test_visitor_in_number_keyval(TestInputVisitorData *data,
301 const void *unused)
302{
303 double res = 0, value = 3.14;
304 Error *err = NULL;
305 Visitor *v;
306
307 v = visitor_input_test_init_full(data, true, "%f", value);
308
309 visit_type_number(v, NULL, &res, &err);
310 error_free_or_abort(&err);
311}
312
313static void test_visitor_in_number_str_keyval(TestInputVisitorData *data,
314 const void *unused)
315{
316 double res = 0, value = 3.14;
317 Visitor *v;
Markus Armbruster5891c382017-05-22 18:42:12 +0200318 Error *err = NULL;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100319
320 v = visitor_input_test_init_full(data, true, "\"3.14\"");
321
322 visit_type_number(v, NULL, &res, &error_abort);
323 g_assert_cmpfloat(res, ==, value);
Markus Armbruster5891c382017-05-22 18:42:12 +0200324
325 v = visitor_input_test_init_full(data, true, "\"inf\"");
326
327 visit_type_number(v, NULL, &res, &err);
328 error_free_or_abort(&err);
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100329}
330
331static void test_visitor_in_number_str_fail(TestInputVisitorData *data,
332 const void *unused)
333{
334 double res = 0;
335 Visitor *v;
336 Error *err = NULL;
337
338 v = visitor_input_test_init(data, "\"3.14\"");
339
340 visit_type_number(v, NULL, &res, &err);
341 error_free_or_abort(&err);
342}
343
344static void test_visitor_in_size_str_keyval(TestInputVisitorData *data,
345 const void *unused)
346{
347 uint64_t res, value = 500 * 1024 * 1024;
348 Visitor *v;
349
350 v = visitor_input_test_init_full(data, true, "\"500M\"");
351
352 visit_type_size(v, NULL, &res, &error_abort);
353 g_assert_cmpfloat(res, ==, value);
354}
355
356static void test_visitor_in_size_str_fail(TestInputVisitorData *data,
357 const void *unused)
358{
359 uint64_t res = 0;
360 Visitor *v;
361 Error *err = NULL;
362
363 v = visitor_input_test_init(data, "\"500M\"");
364
365 visit_type_size(v, NULL, &res, &err);
366 error_free_or_abort(&err);
367}
368
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200369static void test_visitor_in_string(TestInputVisitorData *data,
370 const void *unused)
371{
372 char *res = NULL, *value = (char *) "Q E M U";
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200373 Visitor *v;
374
375 v = visitor_input_test_init(data, "%s", value);
376
Eric Blake51e72bc2016-01-29 06:48:54 -0700377 visit_type_str(v, NULL, &res, &error_abort);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200378 g_assert_cmpstr(res, ==, value);
379
380 g_free(res);
381}
382
383static void test_visitor_in_enum(TestInputVisitorData *data,
384 const void *unused)
385{
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200386 Visitor *v;
387 EnumOne i;
388
Markus Armbruster1c236ba2017-08-24 10:46:06 +0200389 for (i = 0; i < ENUM_ONE__MAX; i++) {
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200390 EnumOne res = -1;
391
Markus Armbruster977c7362017-08-24 10:46:08 +0200392 v = visitor_input_test_init(data, "%s", EnumOne_str(i));
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200393
Eric Blake51e72bc2016-01-29 06:48:54 -0700394 visit_type_EnumOne(v, NULL, &res, &error_abort);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200395 g_assert_cmpint(i, ==, res);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200396 }
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200397}
398
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200399
400static void test_visitor_in_struct(TestInputVisitorData *data,
401 const void *unused)
402{
403 TestStruct *p = NULL;
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200404 Visitor *v;
405
406 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
407
Eric Blake51e72bc2016-01-29 06:48:54 -0700408 visit_type_TestStruct(v, NULL, &p, &error_abort);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200409 g_assert_cmpint(p->integer, ==, -42);
410 g_assert(p->boolean == true);
411 g_assert_cmpstr(p->string, ==, "foo");
412
413 g_free(p->string);
414 g_free(p);
415}
416
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200417static void test_visitor_in_struct_nested(TestInputVisitorData *data,
418 const void *unused)
419{
Eric Blakeb6fcf322015-05-04 09:05:29 -0600420 UserDefTwo *udp = NULL;
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200421 Visitor *v;
422
Eric Blakeb6fcf322015-05-04 09:05:29 -0600423 v = visitor_input_test_init(data, "{ 'string0': 'string0', "
424 "'dict1': { 'string1': 'string1', "
425 "'dict2': { 'userdef': { 'integer': 42, "
426 "'string': 'string' }, 'string': 'string2'}}}");
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200427
Eric Blake51e72bc2016-01-29 06:48:54 -0700428 visit_type_UserDefTwo(v, NULL, &udp, &error_abort);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200429
Eric Blakeb18f1142015-11-05 23:35:29 -0700430 g_assert_cmpstr(udp->string0, ==, "string0");
431 g_assert_cmpstr(udp->dict1->string1, ==, "string1");
Eric Blakeddf21902015-10-26 16:34:49 -0600432 g_assert_cmpint(udp->dict1->dict2->userdef->integer, ==, 42);
Eric Blakeb18f1142015-11-05 23:35:29 -0700433 g_assert_cmpstr(udp->dict1->dict2->userdef->string, ==, "string");
434 g_assert_cmpstr(udp->dict1->dict2->string, ==, "string2");
Eric Blake6446a592015-05-04 09:05:30 -0600435 g_assert(udp->dict1->has_dict3 == false);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200436
Eric Blakeb18f1142015-11-05 23:35:29 -0700437 qapi_free_UserDefTwo(udp);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200438}
439
440static void test_visitor_in_list(TestInputVisitorData *data,
441 const void *unused)
442{
443 UserDefOneList *item, *head = NULL;
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200444 Visitor *v;
445 int i;
446
447 v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
448
Eric Blake51e72bc2016-01-29 06:48:54 -0700449 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200450 g_assert(head != NULL);
451
452 for (i = 0, item = head; item; item = item->next, i++) {
453 char string[12];
454
455 snprintf(string, sizeof(string), "string%d", i);
456 g_assert_cmpstr(item->value->string, ==, string);
Eric Blakeddf21902015-10-26 16:34:49 -0600457 g_assert_cmpint(item->value->integer, ==, 42 + i);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200458 }
459
460 qapi_free_UserDefOneList(head);
Eric Blake25333772015-11-05 23:35:34 -0700461 head = NULL;
462
463 /* An empty list is valid */
464 v = visitor_input_test_init(data, "[]");
Eric Blake51e72bc2016-01-29 06:48:54 -0700465 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
Eric Blake25333772015-11-05 23:35:34 -0700466 g_assert(!head);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200467}
468
Markus Armbruster28770e02015-09-16 13:06:24 +0200469static void test_visitor_in_any(TestInputVisitorData *data,
470 const void *unused)
471{
472 QObject *res = NULL;
Markus Armbruster28770e02015-09-16 13:06:24 +0200473 Visitor *v;
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400474 QNum *qnum;
Markus Armbruster28770e02015-09-16 13:06:24 +0200475 QBool *qbool;
476 QString *qstring;
477 QDict *qdict;
478 QObject *qobj;
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400479 int64_t val;
Markus Armbruster28770e02015-09-16 13:06:24 +0200480
481 v = visitor_input_test_init(data, "-42");
Eric Blake51e72bc2016-01-29 06:48:54 -0700482 visit_type_any(v, NULL, &res, &error_abort);
Max Reitz7dc847e2018-02-24 16:40:29 +0100483 qnum = qobject_to(QNum, res);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400484 g_assert(qnum);
485 g_assert(qnum_get_try_int(qnum, &val));
486 g_assert_cmpint(val, ==, -42);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200487 qobject_unref(res);
Markus Armbruster28770e02015-09-16 13:06:24 +0200488
489 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
Eric Blake51e72bc2016-01-29 06:48:54 -0700490 visit_type_any(v, NULL, &res, &error_abort);
Max Reitz7dc847e2018-02-24 16:40:29 +0100491 qdict = qobject_to(QDict, res);
Markus Armbruster28770e02015-09-16 13:06:24 +0200492 g_assert(qdict && qdict_size(qdict) == 3);
493 qobj = qdict_get(qdict, "integer");
494 g_assert(qobj);
Max Reitz7dc847e2018-02-24 16:40:29 +0100495 qnum = qobject_to(QNum, qobj);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400496 g_assert(qnum);
497 g_assert(qnum_get_try_int(qnum, &val));
498 g_assert_cmpint(val, ==, -42);
Markus Armbruster28770e02015-09-16 13:06:24 +0200499 qobj = qdict_get(qdict, "boolean");
500 g_assert(qobj);
Max Reitz7dc847e2018-02-24 16:40:29 +0100501 qbool = qobject_to(QBool, qobj);
Markus Armbruster28770e02015-09-16 13:06:24 +0200502 g_assert(qbool);
503 g_assert(qbool_get_bool(qbool) == true);
504 qobj = qdict_get(qdict, "string");
505 g_assert(qobj);
Max Reitz7dc847e2018-02-24 16:40:29 +0100506 qstring = qobject_to(QString, qobj);
Markus Armbruster28770e02015-09-16 13:06:24 +0200507 g_assert(qstring);
508 g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200509 qobject_unref(res);
Markus Armbruster28770e02015-09-16 13:06:24 +0200510}
511
Eric Blake3df016f2016-04-28 15:45:23 -0600512static void test_visitor_in_null(TestInputVisitorData *data,
513 const void *unused)
514{
515 Visitor *v;
516 Error *err = NULL;
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200517 QNull *null;
Eric Blake3df016f2016-04-28 15:45:23 -0600518 char *tmp;
519
520 /*
521 * FIXME: Since QAPI doesn't know the 'null' type yet, we can't
522 * test visit_type_null() by reading into a QAPI struct then
523 * checking that it was populated correctly. The best we can do
524 * for now is ensure that we consumed null from the input, proven
525 * by the fact that we can't re-read the key; and that we detect
526 * when input is not null.
527 */
528
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100529 v = visitor_input_test_init_full(data, false,
530 "{ 'a': null, 'b': '' }");
Eric Blake3df016f2016-04-28 15:45:23 -0600531 visit_start_struct(v, NULL, NULL, 0, &error_abort);
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200532 visit_type_null(v, "a", &null, &error_abort);
533 g_assert(qobject_type(QOBJECT(null)) == QTYPE_QNULL);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200534 qobject_unref(null);
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200535 visit_type_null(v, "b", &null, &err);
Eric Blake3df016f2016-04-28 15:45:23 -0600536 error_free_or_abort(&err);
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200537 g_assert(!null);
Markus Armbrusterec95f612017-03-03 13:32:38 +0100538 visit_type_str(v, "c", &tmp, &err);
Markus Armbrusterec95f612017-03-03 13:32:38 +0100539 error_free_or_abort(&err);
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200540 g_assert(!tmp);
Eric Blake15c2f662016-04-28 15:45:27 -0600541 visit_check_struct(v, &error_abort);
Eric Blake1158bb22016-06-09 10:48:34 -0600542 visit_end_struct(v, NULL);
Eric Blake3df016f2016-04-28 15:45:23 -0600543}
544
Markus Armbruster2fc00432014-03-01 08:40:33 +0100545static void test_visitor_in_union_flat(TestInputVisitorData *data,
546 const void *unused)
547{
548 Visitor *v;
Markus Armbruster2fc00432014-03-01 08:40:33 +0100549 UserDefFlatUnion *tmp;
Eric Blake30594fe2015-10-26 16:34:48 -0600550 UserDefUnionBase *base;
Markus Armbruster2fc00432014-03-01 08:40:33 +0100551
Wenchao Xia52230702014-03-04 18:44:39 -0800552 v = visitor_input_test_init(data,
553 "{ 'enum1': 'value1', "
Markus Armbruster441cbac2015-09-16 13:06:10 +0200554 "'integer': 41, "
Wenchao Xia52230702014-03-04 18:44:39 -0800555 "'string': 'str', "
556 "'boolean': true }");
Markus Armbruster2fc00432014-03-01 08:40:33 +0100557
Eric Blake51e72bc2016-01-29 06:48:54 -0700558 visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort);
Markus Armbruster0f61af32015-07-31 10:30:04 +0200559 g_assert_cmpint(tmp->enum1, ==, ENUM_ONE_VALUE1);
Wenchao Xia52230702014-03-04 18:44:39 -0800560 g_assert_cmpstr(tmp->string, ==, "str");
Markus Armbruster441cbac2015-09-16 13:06:10 +0200561 g_assert_cmpint(tmp->integer, ==, 41);
Eric Blake544a3732016-02-17 23:48:27 -0700562 g_assert_cmpint(tmp->u.value1.boolean, ==, true);
Eric Blake30594fe2015-10-26 16:34:48 -0600563
564 base = qapi_UserDefFlatUnion_base(tmp);
565 g_assert(&base->enum1 == &tmp->enum1);
566
Markus Armbruster2fc00432014-03-01 08:40:33 +0100567 qapi_free_UserDefFlatUnion(tmp);
568}
569
Eric Blakeab045262015-05-04 09:05:11 -0600570static void test_visitor_in_alternate(TestInputVisitorData *data,
571 const void *unused)
Markus Armbruster2c38b602014-03-01 08:40:30 +0100572{
573 Visitor *v;
Eric Blakeab045262015-05-04 09:05:11 -0600574 UserDefAlternate *tmp;
Eric Blake68d07832016-02-17 23:48:18 -0700575 WrapAlternate *wrap;
Markus Armbruster2c38b602014-03-01 08:40:30 +0100576
577 v = visitor_input_test_init(data, "42");
Eric Blake51e72bc2016-01-29 06:48:54 -0700578 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400579 g_assert_cmpint(tmp->type, ==, QTYPE_QNUM);
Eric Blakec363ace2015-10-26 16:34:53 -0600580 g_assert_cmpint(tmp->u.i, ==, 42);
Eric Blakeab045262015-05-04 09:05:11 -0600581 qapi_free_UserDefAlternate(tmp);
Eric Blake9c51b442015-09-29 16:21:06 -0600582
Markus Armbruster8168ca82017-05-22 18:42:14 +0200583 v = visitor_input_test_init(data, "'value1'");
Eric Blake51e72bc2016-01-29 06:48:54 -0700584 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
Eric Blake0426d532015-12-01 22:20:48 -0700585 g_assert_cmpint(tmp->type, ==, QTYPE_QSTRING);
Markus Armbruster8168ca82017-05-22 18:42:14 +0200586 g_assert_cmpint(tmp->u.e, ==, ENUM_ONE_VALUE1);
Eric Blake9c51b442015-09-29 16:21:06 -0600587 qapi_free_UserDefAlternate(tmp);
Eric Blake9c51b442015-09-29 16:21:06 -0600588
Markus Armbruster4d2d5c42017-06-26 19:25:14 +0200589 v = visitor_input_test_init(data, "null");
590 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
591 g_assert_cmpint(tmp->type, ==, QTYPE_QNULL);
592 qapi_free_UserDefAlternate(tmp);
593
Eric Blake68d07832016-02-17 23:48:18 -0700594 v = visitor_input_test_init(data, "{'integer':1, 'string':'str', "
595 "'enum1':'value1', 'boolean':true}");
596 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
597 g_assert_cmpint(tmp->type, ==, QTYPE_QDICT);
Eric Blakebecceed2016-02-17 23:48:26 -0700598 g_assert_cmpint(tmp->u.udfu.integer, ==, 1);
599 g_assert_cmpstr(tmp->u.udfu.string, ==, "str");
600 g_assert_cmpint(tmp->u.udfu.enum1, ==, ENUM_ONE_VALUE1);
Eric Blake544a3732016-02-17 23:48:27 -0700601 g_assert_cmpint(tmp->u.udfu.u.value1.boolean, ==, true);
602 g_assert_cmpint(tmp->u.udfu.u.value1.has_a_b, ==, false);
Eric Blake68d07832016-02-17 23:48:18 -0700603 qapi_free_UserDefAlternate(tmp);
604
Eric Blake68d07832016-02-17 23:48:18 -0700605 v = visitor_input_test_init(data, "{ 'alt': 42 }");
606 visit_type_WrapAlternate(v, NULL, &wrap, &error_abort);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400607 g_assert_cmpint(wrap->alt->type, ==, QTYPE_QNUM);
Eric Blake68d07832016-02-17 23:48:18 -0700608 g_assert_cmpint(wrap->alt->u.i, ==, 42);
609 qapi_free_WrapAlternate(wrap);
610
Markus Armbruster8168ca82017-05-22 18:42:14 +0200611 v = visitor_input_test_init(data, "{ 'alt': 'value1' }");
Eric Blake68d07832016-02-17 23:48:18 -0700612 visit_type_WrapAlternate(v, NULL, &wrap, &error_abort);
613 g_assert_cmpint(wrap->alt->type, ==, QTYPE_QSTRING);
Markus Armbruster8168ca82017-05-22 18:42:14 +0200614 g_assert_cmpint(wrap->alt->u.e, ==, ENUM_ONE_VALUE1);
Eric Blake68d07832016-02-17 23:48:18 -0700615 qapi_free_WrapAlternate(wrap);
616
617 v = visitor_input_test_init(data, "{ 'alt': {'integer':1, 'string':'str', "
618 "'enum1':'value1', 'boolean':true} }");
619 visit_type_WrapAlternate(v, NULL, &wrap, &error_abort);
620 g_assert_cmpint(wrap->alt->type, ==, QTYPE_QDICT);
Eric Blakebecceed2016-02-17 23:48:26 -0700621 g_assert_cmpint(wrap->alt->u.udfu.integer, ==, 1);
622 g_assert_cmpstr(wrap->alt->u.udfu.string, ==, "str");
623 g_assert_cmpint(wrap->alt->u.udfu.enum1, ==, ENUM_ONE_VALUE1);
Eric Blake544a3732016-02-17 23:48:27 -0700624 g_assert_cmpint(wrap->alt->u.udfu.u.value1.boolean, ==, true);
625 g_assert_cmpint(wrap->alt->u.udfu.u.value1.has_a_b, ==, false);
Eric Blake68d07832016-02-17 23:48:18 -0700626 qapi_free_WrapAlternate(wrap);
Eric Blake9c51b442015-09-29 16:21:06 -0600627}
628
629static void test_visitor_in_alternate_number(TestInputVisitorData *data,
630 const void *unused)
631{
632 Visitor *v;
633 Error *err = NULL;
Markus Armbruster8168ca82017-05-22 18:42:14 +0200634 AltEnumBool *aeb;
635 AltEnumNum *aen;
636 AltNumEnum *ans;
637 AltEnumInt *asi;
Eric Blake9c51b442015-09-29 16:21:06 -0600638
639 /* Parsing an int */
640
641 v = visitor_input_test_init(data, "42");
Markus Armbruster8168ca82017-05-22 18:42:14 +0200642 visit_type_AltEnumBool(v, NULL, &aeb, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700643 error_free_or_abort(&err);
Markus Armbruster8168ca82017-05-22 18:42:14 +0200644 qapi_free_AltEnumBool(aeb);
Eric Blake9c51b442015-09-29 16:21:06 -0600645
Eric Blake9c51b442015-09-29 16:21:06 -0600646 v = visitor_input_test_init(data, "42");
Markus Armbruster8168ca82017-05-22 18:42:14 +0200647 visit_type_AltEnumNum(v, NULL, &aen, &error_abort);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400648 g_assert_cmpint(aen->type, ==, QTYPE_QNUM);
Markus Armbruster8168ca82017-05-22 18:42:14 +0200649 g_assert_cmpfloat(aen->u.n, ==, 42);
650 qapi_free_AltEnumNum(aen);
Eric Blake9c51b442015-09-29 16:21:06 -0600651
652 v = visitor_input_test_init(data, "42");
Markus Armbruster8168ca82017-05-22 18:42:14 +0200653 visit_type_AltNumEnum(v, NULL, &ans, &error_abort);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400654 g_assert_cmpint(ans->type, ==, QTYPE_QNUM);
Eric Blaked00341a2015-12-01 22:20:51 -0700655 g_assert_cmpfloat(ans->u.n, ==, 42);
Markus Armbruster8168ca82017-05-22 18:42:14 +0200656 qapi_free_AltNumEnum(ans);
Eric Blake9c51b442015-09-29 16:21:06 -0600657
658 v = visitor_input_test_init(data, "42");
Markus Armbruster8168ca82017-05-22 18:42:14 +0200659 visit_type_AltEnumInt(v, NULL, &asi, &error_abort);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400660 g_assert_cmpint(asi->type, ==, QTYPE_QNUM);
Eric Blakec363ace2015-10-26 16:34:53 -0600661 g_assert_cmpint(asi->u.i, ==, 42);
Markus Armbruster8168ca82017-05-22 18:42:14 +0200662 qapi_free_AltEnumInt(asi);
Eric Blake9c51b442015-09-29 16:21:06 -0600663
Eric Blake9c51b442015-09-29 16:21:06 -0600664 /* Parsing a double */
665
666 v = visitor_input_test_init(data, "42.5");
Markus Armbruster8168ca82017-05-22 18:42:14 +0200667 visit_type_AltEnumBool(v, NULL, &aeb, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700668 error_free_or_abort(&err);
Markus Armbruster8168ca82017-05-22 18:42:14 +0200669 qapi_free_AltEnumBool(aeb);
Eric Blake9c51b442015-09-29 16:21:06 -0600670
671 v = visitor_input_test_init(data, "42.5");
Markus Armbruster8168ca82017-05-22 18:42:14 +0200672 visit_type_AltEnumNum(v, NULL, &aen, &error_abort);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400673 g_assert_cmpint(aen->type, ==, QTYPE_QNUM);
Markus Armbruster8168ca82017-05-22 18:42:14 +0200674 g_assert_cmpfloat(aen->u.n, ==, 42.5);
675 qapi_free_AltEnumNum(aen);
Eric Blake9c51b442015-09-29 16:21:06 -0600676
677 v = visitor_input_test_init(data, "42.5");
Markus Armbruster8168ca82017-05-22 18:42:14 +0200678 visit_type_AltNumEnum(v, NULL, &ans, &error_abort);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400679 g_assert_cmpint(ans->type, ==, QTYPE_QNUM);
Eric Blakec363ace2015-10-26 16:34:53 -0600680 g_assert_cmpfloat(ans->u.n, ==, 42.5);
Markus Armbruster8168ca82017-05-22 18:42:14 +0200681 qapi_free_AltNumEnum(ans);
Eric Blake9c51b442015-09-29 16:21:06 -0600682
683 v = visitor_input_test_init(data, "42.5");
Markus Armbruster8168ca82017-05-22 18:42:14 +0200684 visit_type_AltEnumInt(v, NULL, &asi, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700685 error_free_or_abort(&err);
Markus Armbruster8168ca82017-05-22 18:42:14 +0200686 qapi_free_AltEnumInt(asi);
Markus Armbruster2c38b602014-03-01 08:40:30 +0100687}
688
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100689static void test_list_union_integer_helper(TestInputVisitorData *data,
690 const void *unused,
691 UserDefListUnionKind kind)
Michael Roth199e0f12013-05-10 17:46:10 -0500692{
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100693 UserDefListUnion *cvalue = NULL;
Michael Roth199e0f12013-05-10 17:46:10 -0500694 Visitor *v;
695 GString *gstr_list = g_string_new("");
696 GString *gstr_union = g_string_new("");
697 int i;
698
699 for (i = 0; i < 32; i++) {
700 g_string_append_printf(gstr_list, "%d", i);
701 if (i != 31) {
702 g_string_append(gstr_list, ", ");
703 }
704 }
705 g_string_append_printf(gstr_union, "{ 'type': '%s', 'data': [ %s ] }",
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100706 UserDefListUnionKind_str(kind),
Michael Roth199e0f12013-05-10 17:46:10 -0500707 gstr_list->str);
708 v = visitor_input_test_init_raw(data, gstr_union->str);
709
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100710 visit_type_UserDefListUnion(v, NULL, &cvalue, &error_abort);
Michael Roth199e0f12013-05-10 17:46:10 -0500711 g_assert(cvalue != NULL);
Eric Blakec363ace2015-10-26 16:34:53 -0600712 g_assert_cmpint(cvalue->type, ==, kind);
Michael Roth199e0f12013-05-10 17:46:10 -0500713
714 switch (kind) {
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100715 case USER_DEF_LIST_UNION_KIND_INTEGER: {
Michael Roth199e0f12013-05-10 17:46:10 -0500716 intList *elem = NULL;
Eric Blake32bafa82016-03-17 16:48:37 -0600717 for (i = 0, elem = cvalue->u.integer.data;
718 elem; elem = elem->next, i++) {
Michael Roth199e0f12013-05-10 17:46:10 -0500719 g_assert_cmpint(elem->value, ==, i);
720 }
721 break;
722 }
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100723 case USER_DEF_LIST_UNION_KIND_S8: {
Michael Roth199e0f12013-05-10 17:46:10 -0500724 int8List *elem = NULL;
Eric Blake32bafa82016-03-17 16:48:37 -0600725 for (i = 0, elem = cvalue->u.s8.data; elem; elem = elem->next, i++) {
Michael Roth199e0f12013-05-10 17:46:10 -0500726 g_assert_cmpint(elem->value, ==, i);
727 }
728 break;
729 }
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100730 case USER_DEF_LIST_UNION_KIND_S16: {
Michael Roth199e0f12013-05-10 17:46:10 -0500731 int16List *elem = NULL;
Eric Blake32bafa82016-03-17 16:48:37 -0600732 for (i = 0, elem = cvalue->u.s16.data; elem; elem = elem->next, i++) {
Michael Roth199e0f12013-05-10 17:46:10 -0500733 g_assert_cmpint(elem->value, ==, i);
734 }
735 break;
736 }
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100737 case USER_DEF_LIST_UNION_KIND_S32: {
Michael Roth199e0f12013-05-10 17:46:10 -0500738 int32List *elem = NULL;
Eric Blake32bafa82016-03-17 16:48:37 -0600739 for (i = 0, elem = cvalue->u.s32.data; elem; elem = elem->next, i++) {
Michael Roth199e0f12013-05-10 17:46:10 -0500740 g_assert_cmpint(elem->value, ==, i);
741 }
742 break;
743 }
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100744 case USER_DEF_LIST_UNION_KIND_S64: {
Michael Roth199e0f12013-05-10 17:46:10 -0500745 int64List *elem = NULL;
Eric Blake32bafa82016-03-17 16:48:37 -0600746 for (i = 0, elem = cvalue->u.s64.data; elem; elem = elem->next, i++) {
Michael Roth199e0f12013-05-10 17:46:10 -0500747 g_assert_cmpint(elem->value, ==, i);
748 }
749 break;
750 }
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100751 case USER_DEF_LIST_UNION_KIND_U8: {
Michael Roth199e0f12013-05-10 17:46:10 -0500752 uint8List *elem = NULL;
Eric Blake32bafa82016-03-17 16:48:37 -0600753 for (i = 0, elem = cvalue->u.u8.data; elem; elem = elem->next, i++) {
Michael Roth199e0f12013-05-10 17:46:10 -0500754 g_assert_cmpint(elem->value, ==, i);
755 }
756 break;
757 }
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100758 case USER_DEF_LIST_UNION_KIND_U16: {
Michael Roth199e0f12013-05-10 17:46:10 -0500759 uint16List *elem = NULL;
Eric Blake32bafa82016-03-17 16:48:37 -0600760 for (i = 0, elem = cvalue->u.u16.data; elem; elem = elem->next, i++) {
Michael Roth199e0f12013-05-10 17:46:10 -0500761 g_assert_cmpint(elem->value, ==, i);
762 }
763 break;
764 }
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100765 case USER_DEF_LIST_UNION_KIND_U32: {
Michael Roth199e0f12013-05-10 17:46:10 -0500766 uint32List *elem = NULL;
Eric Blake32bafa82016-03-17 16:48:37 -0600767 for (i = 0, elem = cvalue->u.u32.data; elem; elem = elem->next, i++) {
Michael Roth199e0f12013-05-10 17:46:10 -0500768 g_assert_cmpint(elem->value, ==, i);
769 }
770 break;
771 }
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100772 case USER_DEF_LIST_UNION_KIND_U64: {
Michael Roth199e0f12013-05-10 17:46:10 -0500773 uint64List *elem = NULL;
Eric Blake32bafa82016-03-17 16:48:37 -0600774 for (i = 0, elem = cvalue->u.u64.data; elem; elem = elem->next, i++) {
Michael Roth199e0f12013-05-10 17:46:10 -0500775 g_assert_cmpint(elem->value, ==, i);
776 }
777 break;
778 }
779 default:
Stefan Weildfc6f862013-07-25 18:21:28 +0200780 g_assert_not_reached();
Michael Roth199e0f12013-05-10 17:46:10 -0500781 }
782
783 g_string_free(gstr_union, true);
784 g_string_free(gstr_list, true);
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100785 qapi_free_UserDefListUnion(cvalue);
Michael Roth199e0f12013-05-10 17:46:10 -0500786}
787
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100788static void test_visitor_in_list_union_int(TestInputVisitorData *data,
789 const void *unused)
790{
791 test_list_union_integer_helper(data, unused,
792 USER_DEF_LIST_UNION_KIND_INTEGER);
793}
794
795static void test_visitor_in_list_union_int8(TestInputVisitorData *data,
Michael Roth199e0f12013-05-10 17:46:10 -0500796 const void *unused)
797{
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100798 test_list_union_integer_helper(data, unused,
799 USER_DEF_LIST_UNION_KIND_S8);
Michael Roth199e0f12013-05-10 17:46:10 -0500800}
801
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100802static void test_visitor_in_list_union_int16(TestInputVisitorData *data,
Michael Roth199e0f12013-05-10 17:46:10 -0500803 const void *unused)
804{
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100805 test_list_union_integer_helper(data, unused,
806 USER_DEF_LIST_UNION_KIND_S16);
Michael Roth199e0f12013-05-10 17:46:10 -0500807}
808
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100809static void test_visitor_in_list_union_int32(TestInputVisitorData *data,
Michael Roth199e0f12013-05-10 17:46:10 -0500810 const void *unused)
811{
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100812 test_list_union_integer_helper(data, unused,
813 USER_DEF_LIST_UNION_KIND_S32);
Michael Roth199e0f12013-05-10 17:46:10 -0500814}
815
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100816static void test_visitor_in_list_union_int64(TestInputVisitorData *data,
817 const void *unused)
Michael Roth199e0f12013-05-10 17:46:10 -0500818{
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100819 test_list_union_integer_helper(data, unused,
820 USER_DEF_LIST_UNION_KIND_S64);
Michael Roth199e0f12013-05-10 17:46:10 -0500821}
822
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100823static void test_visitor_in_list_union_uint8(TestInputVisitorData *data,
824 const void *unused)
Michael Roth199e0f12013-05-10 17:46:10 -0500825{
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100826 test_list_union_integer_helper(data, unused,
827 USER_DEF_LIST_UNION_KIND_U8);
Michael Roth199e0f12013-05-10 17:46:10 -0500828}
829
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100830static void test_visitor_in_list_union_uint16(TestInputVisitorData *data,
831 const void *unused)
Michael Roth199e0f12013-05-10 17:46:10 -0500832{
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100833 test_list_union_integer_helper(data, unused,
834 USER_DEF_LIST_UNION_KIND_U16);
Michael Roth199e0f12013-05-10 17:46:10 -0500835}
836
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100837static void test_visitor_in_list_union_uint32(TestInputVisitorData *data,
838 const void *unused)
839{
840 test_list_union_integer_helper(data, unused,
841 USER_DEF_LIST_UNION_KIND_U32);
842}
843
844static void test_visitor_in_list_union_uint64(TestInputVisitorData *data,
845 const void *unused)
846{
847 test_list_union_integer_helper(data, unused,
848 USER_DEF_LIST_UNION_KIND_U64);
849}
850
851static void test_visitor_in_list_union_bool(TestInputVisitorData *data,
Michael Roth199e0f12013-05-10 17:46:10 -0500852 const void *unused)
853{
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100854 UserDefListUnion *cvalue = NULL;
Michael Roth199e0f12013-05-10 17:46:10 -0500855 boolList *elem = NULL;
Michael Roth199e0f12013-05-10 17:46:10 -0500856 Visitor *v;
857 GString *gstr_list = g_string_new("");
858 GString *gstr_union = g_string_new("");
859 int i;
860
861 for (i = 0; i < 32; i++) {
862 g_string_append_printf(gstr_list, "%s",
863 (i % 3 == 0) ? "true" : "false");
864 if (i != 31) {
865 g_string_append(gstr_list, ", ");
866 }
867 }
868 g_string_append_printf(gstr_union, "{ 'type': 'boolean', 'data': [ %s ] }",
869 gstr_list->str);
870 v = visitor_input_test_init_raw(data, gstr_union->str);
871
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100872 visit_type_UserDefListUnion(v, NULL, &cvalue, &error_abort);
Michael Roth199e0f12013-05-10 17:46:10 -0500873 g_assert(cvalue != NULL);
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100874 g_assert_cmpint(cvalue->type, ==, USER_DEF_LIST_UNION_KIND_BOOLEAN);
Michael Roth199e0f12013-05-10 17:46:10 -0500875
Eric Blake32bafa82016-03-17 16:48:37 -0600876 for (i = 0, elem = cvalue->u.boolean.data; elem; elem = elem->next, i++) {
Michael Roth199e0f12013-05-10 17:46:10 -0500877 g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0);
878 }
879
880 g_string_free(gstr_union, true);
881 g_string_free(gstr_list, true);
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100882 qapi_free_UserDefListUnion(cvalue);
Michael Roth199e0f12013-05-10 17:46:10 -0500883}
884
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100885static void test_visitor_in_list_union_string(TestInputVisitorData *data,
886 const void *unused)
Michael Roth199e0f12013-05-10 17:46:10 -0500887{
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100888 UserDefListUnion *cvalue = NULL;
Michael Roth199e0f12013-05-10 17:46:10 -0500889 strList *elem = NULL;
Michael Roth199e0f12013-05-10 17:46:10 -0500890 Visitor *v;
891 GString *gstr_list = g_string_new("");
892 GString *gstr_union = g_string_new("");
893 int i;
894
895 for (i = 0; i < 32; i++) {
896 g_string_append_printf(gstr_list, "'%d'", i);
897 if (i != 31) {
898 g_string_append(gstr_list, ", ");
899 }
900 }
901 g_string_append_printf(gstr_union, "{ 'type': 'string', 'data': [ %s ] }",
902 gstr_list->str);
903 v = visitor_input_test_init_raw(data, gstr_union->str);
904
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100905 visit_type_UserDefListUnion(v, NULL, &cvalue, &error_abort);
Michael Roth199e0f12013-05-10 17:46:10 -0500906 g_assert(cvalue != NULL);
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100907 g_assert_cmpint(cvalue->type, ==, USER_DEF_LIST_UNION_KIND_STRING);
Michael Roth199e0f12013-05-10 17:46:10 -0500908
Eric Blake32bafa82016-03-17 16:48:37 -0600909 for (i = 0, elem = cvalue->u.string.data; elem; elem = elem->next, i++) {
Michael Roth199e0f12013-05-10 17:46:10 -0500910 gchar str[8];
911 sprintf(str, "%d", i);
912 g_assert_cmpstr(elem->value, ==, str);
913 }
914
915 g_string_free(gstr_union, true);
916 g_string_free(gstr_list, true);
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100917 qapi_free_UserDefListUnion(cvalue);
Michael Roth199e0f12013-05-10 17:46:10 -0500918}
919
920#define DOUBLE_STR_MAX 16
921
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100922static void test_visitor_in_list_union_number(TestInputVisitorData *data,
923 const void *unused)
Michael Roth199e0f12013-05-10 17:46:10 -0500924{
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100925 UserDefListUnion *cvalue = NULL;
Michael Roth199e0f12013-05-10 17:46:10 -0500926 numberList *elem = NULL;
Michael Roth199e0f12013-05-10 17:46:10 -0500927 Visitor *v;
928 GString *gstr_list = g_string_new("");
929 GString *gstr_union = g_string_new("");
930 int i;
931
932 for (i = 0; i < 32; i++) {
933 g_string_append_printf(gstr_list, "%f", (double)i / 3);
934 if (i != 31) {
935 g_string_append(gstr_list, ", ");
936 }
937 }
938 g_string_append_printf(gstr_union, "{ 'type': 'number', 'data': [ %s ] }",
939 gstr_list->str);
940 v = visitor_input_test_init_raw(data, gstr_union->str);
941
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100942 visit_type_UserDefListUnion(v, NULL, &cvalue, &error_abort);
Michael Roth199e0f12013-05-10 17:46:10 -0500943 g_assert(cvalue != NULL);
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100944 g_assert_cmpint(cvalue->type, ==, USER_DEF_LIST_UNION_KIND_NUMBER);
Michael Roth199e0f12013-05-10 17:46:10 -0500945
Eric Blake32bafa82016-03-17 16:48:37 -0600946 for (i = 0, elem = cvalue->u.number.data; elem; elem = elem->next, i++) {
Michael Roth199e0f12013-05-10 17:46:10 -0500947 GString *double_expected = g_string_new("");
948 GString *double_actual = g_string_new("");
949
950 g_string_printf(double_expected, "%.6f", (double)i / 3);
951 g_string_printf(double_actual, "%.6f", elem->value);
952 g_assert_cmpstr(double_expected->str, ==, double_actual->str);
953
954 g_string_free(double_expected, true);
955 g_string_free(double_actual, true);
956 }
957
958 g_string_free(gstr_union, true);
959 g_string_free(gstr_list, true);
Markus Armbrusterb359f4b2019-03-01 16:40:49 +0100960 qapi_free_UserDefListUnion(cvalue);
Michael Roth199e0f12013-05-10 17:46:10 -0500961}
962
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200963static void input_visitor_test_add(const char *testpath,
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +0100964 const void *user_data,
965 void (*test_func)(TestInputVisitorData *data,
966 const void *user_data))
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200967{
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +0100968 g_test_add(testpath, TestInputVisitorData, user_data, NULL, test_func,
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -0200969 visitor_input_teardown);
970}
971
Paolo Bonzini3dcf71f2012-03-22 12:51:03 +0100972static void test_visitor_in_errors(TestInputVisitorData *data,
973 const void *unused)
974{
975 TestStruct *p = NULL;
Markus Armbrustere940f542014-05-02 13:26:29 +0200976 Error *err = NULL;
Paolo Bonzini3dcf71f2012-03-22 12:51:03 +0100977 Visitor *v;
Eric Blakedd5ee2c2015-11-05 23:35:33 -0700978 strList *q = NULL;
Eric Blake9b4e38f2016-06-15 11:37:51 -0600979 UserDefTwo *r = NULL;
980 WrapAlternate *s = NULL;
Paolo Bonzini3dcf71f2012-03-22 12:51:03 +0100981
Eric Blakedd5ee2c2015-11-05 23:35:33 -0700982 v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', "
983 "'string': -42 }");
Paolo Bonzini3dcf71f2012-03-22 12:51:03 +0100984
Eric Blake51e72bc2016-01-29 06:48:54 -0700985 visit_type_TestStruct(v, NULL, &p, &err);
Eric Blakea12a5a12015-11-05 23:35:31 -0700986 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600987 g_assert(!p);
Eric Blakedd5ee2c2015-11-05 23:35:33 -0700988
989 v = visitor_input_test_init(data, "[ '1', '2', false, '3' ]");
Eric Blake51e72bc2016-01-29 06:48:54 -0700990 visit_type_strList(v, NULL, &q, &err);
Eric Blakedd5ee2c2015-11-05 23:35:33 -0700991 error_free_or_abort(&err);
Eric Blake68ab47e2016-04-28 15:45:32 -0600992 assert(!q);
Eric Blake9b4e38f2016-06-15 11:37:51 -0600993
994 v = visitor_input_test_init(data, "{ 'str':'hi' }");
995 visit_type_UserDefTwo(v, NULL, &r, &err);
996 error_free_or_abort(&err);
997 assert(!r);
998
999 v = visitor_input_test_init(data, "{ }");
1000 visit_type_WrapAlternate(v, NULL, &s, &err);
1001 error_free_or_abort(&err);
1002 assert(!s);
Paolo Bonzini3dcf71f2012-03-22 12:51:03 +01001003}
1004
Eric Blake25333772015-11-05 23:35:34 -07001005static void test_visitor_in_wrong_type(TestInputVisitorData *data,
1006 const void *unused)
1007{
1008 TestStruct *p = NULL;
1009 Visitor *v;
1010 strList *q = NULL;
1011 int64_t i;
1012 Error *err = NULL;
1013
1014 /* Make sure arrays and structs cannot be confused */
1015
1016 v = visitor_input_test_init(data, "[]");
Eric Blake51e72bc2016-01-29 06:48:54 -07001017 visit_type_TestStruct(v, NULL, &p, &err);
Eric Blake25333772015-11-05 23:35:34 -07001018 error_free_or_abort(&err);
1019 g_assert(!p);
1020
1021 v = visitor_input_test_init(data, "{}");
Eric Blake51e72bc2016-01-29 06:48:54 -07001022 visit_type_strList(v, NULL, &q, &err);
Eric Blake25333772015-11-05 23:35:34 -07001023 error_free_or_abort(&err);
1024 assert(!q);
1025
1026 /* Make sure primitives and struct cannot be confused */
1027
1028 v = visitor_input_test_init(data, "1");
Eric Blake51e72bc2016-01-29 06:48:54 -07001029 visit_type_TestStruct(v, NULL, &p, &err);
Eric Blake25333772015-11-05 23:35:34 -07001030 error_free_or_abort(&err);
1031 g_assert(!p);
1032
1033 v = visitor_input_test_init(data, "{}");
Eric Blake51e72bc2016-01-29 06:48:54 -07001034 visit_type_int(v, NULL, &i, &err);
Eric Blake25333772015-11-05 23:35:34 -07001035 error_free_or_abort(&err);
1036
1037 /* Make sure primitives and arrays cannot be confused */
1038
1039 v = visitor_input_test_init(data, "1");
Eric Blake51e72bc2016-01-29 06:48:54 -07001040 visit_type_strList(v, NULL, &q, &err);
Eric Blake25333772015-11-05 23:35:34 -07001041 error_free_or_abort(&err);
1042 assert(!q);
1043
1044 v = visitor_input_test_init(data, "[]");
Eric Blake51e72bc2016-01-29 06:48:54 -07001045 visit_type_int(v, NULL, &i, &err);
Eric Blake25333772015-11-05 23:35:34 -07001046 error_free_or_abort(&err);
1047}
1048
Markus Armbruster77c47de2017-03-03 13:32:40 +01001049static void test_visitor_in_fail_struct(TestInputVisitorData *data,
1050 const void *unused)
1051{
1052 TestStruct *p = NULL;
1053 Error *err = NULL;
1054 Visitor *v;
1055
1056 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
1057
1058 visit_type_TestStruct(v, NULL, &p, &err);
1059 error_free_or_abort(&err);
1060 g_assert(!p);
1061}
1062
1063static void test_visitor_in_fail_struct_nested(TestInputVisitorData *data,
1064 const void *unused)
1065{
1066 UserDefTwo *udp = NULL;
1067 Error *err = NULL;
1068 Visitor *v;
1069
1070 v = visitor_input_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
1071
1072 visit_type_UserDefTwo(v, NULL, &udp, &err);
1073 error_free_or_abort(&err);
1074 g_assert(!udp);
1075}
1076
1077static void test_visitor_in_fail_struct_in_list(TestInputVisitorData *data,
1078 const void *unused)
1079{
1080 UserDefOneList *head = NULL;
1081 Error *err = NULL;
1082 Visitor *v;
1083
1084 v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
1085
1086 visit_type_UserDefOneList(v, NULL, &head, &err);
1087 error_free_or_abort(&err);
1088 g_assert(!head);
1089}
1090
1091static void test_visitor_in_fail_struct_missing(TestInputVisitorData *data,
1092 const void *unused)
1093{
1094 Error *err = NULL;
1095 Visitor *v;
1096 QObject *any;
Markus Armbrusterd2f95f42017-06-26 18:22:59 +02001097 QNull *null;
Markus Armbruster77c47de2017-03-03 13:32:40 +01001098 GenericAlternate *alt;
1099 bool present;
1100 int en;
1101 int64_t i64;
1102 uint32_t u32;
1103 int8_t i8;
1104 char *str;
1105 double dbl;
1106
Markus Armbruster86ca0db2017-03-03 13:32:44 +01001107 v = visitor_input_test_init(data, "{ 'sub': [ {} ] }");
Markus Armbruster77c47de2017-03-03 13:32:40 +01001108 visit_start_struct(v, NULL, NULL, 0, &error_abort);
1109 visit_start_struct(v, "struct", NULL, 0, &err);
1110 error_free_or_abort(&err);
1111 visit_start_list(v, "list", NULL, 0, &err);
1112 error_free_or_abort(&err);
Marc-André Lureau60390d22017-06-07 20:35:59 +04001113 visit_start_alternate(v, "alternate", &alt, sizeof(*alt), &err);
Markus Armbruster77c47de2017-03-03 13:32:40 +01001114 error_free_or_abort(&err);
1115 visit_optional(v, "optional", &present);
1116 g_assert(!present);
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02001117 visit_type_enum(v, "enum", &en, &EnumOne_lookup, &err);
Markus Armbruster77c47de2017-03-03 13:32:40 +01001118 error_free_or_abort(&err);
1119 visit_type_int(v, "i64", &i64, &err);
1120 error_free_or_abort(&err);
1121 visit_type_uint32(v, "u32", &u32, &err);
1122 error_free_or_abort(&err);
1123 visit_type_int8(v, "i8", &i8, &err);
1124 error_free_or_abort(&err);
1125 visit_type_str(v, "i8", &str, &err);
1126 error_free_or_abort(&err);
1127 visit_type_number(v, "dbl", &dbl, &err);
1128 error_free_or_abort(&err);
1129 visit_type_any(v, "any", &any, &err);
1130 error_free_or_abort(&err);
Markus Armbrusterd2f95f42017-06-26 18:22:59 +02001131 visit_type_null(v, "null", &null, &err);
Markus Armbruster77c47de2017-03-03 13:32:40 +01001132 error_free_or_abort(&err);
Markus Armbruster86ca0db2017-03-03 13:32:44 +01001133 visit_start_list(v, "sub", NULL, 0, &error_abort);
1134 visit_start_struct(v, NULL, NULL, 0, &error_abort);
1135 visit_type_int(v, "i64", &i64, &err);
1136 error_free_or_abort(&err);
1137 visit_end_struct(v, NULL);
1138 visit_end_list(v, NULL);
Markus Armbruster77c47de2017-03-03 13:32:40 +01001139 visit_end_struct(v, NULL);
1140}
1141
Markus Armbruster9cb8ef32017-03-03 13:32:43 +01001142static void test_visitor_in_fail_list(TestInputVisitorData *data,
1143 const void *unused)
1144{
1145 int64_t i64 = -1;
Markus Armbrustera4a1c702017-03-03 13:32:45 +01001146 Error *err = NULL;
Markus Armbruster9cb8ef32017-03-03 13:32:43 +01001147 Visitor *v;
1148
1149 /* Unvisited list tail */
1150
1151 v = visitor_input_test_init(data, "[ 1, 2, 3 ]");
1152
1153 visit_start_list(v, NULL, NULL, 0, &error_abort);
1154 visit_type_int(v, NULL, &i64, &error_abort);
1155 g_assert_cmpint(i64, ==, 1);
1156 visit_type_int(v, NULL, &i64, &error_abort);
1157 g_assert_cmpint(i64, ==, 2);
Markus Armbrustera4a1c702017-03-03 13:32:45 +01001158 visit_check_list(v, &err);
1159 error_free_or_abort(&err);
Markus Armbruster9cb8ef32017-03-03 13:32:43 +01001160 visit_end_list(v, NULL);
Markus Armbrustera9416dc2017-03-03 13:32:46 +01001161
1162 /* Visit beyond end of list */
1163 v = visitor_input_test_init(data, "[]");
1164
1165 visit_start_list(v, NULL, NULL, 0, &error_abort);
Markus Armbrustera9416dc2017-03-03 13:32:46 +01001166 visit_type_int(v, NULL, &i64, &err);
1167 error_free_or_abort(&err);
Markus Armbrustera9416dc2017-03-03 13:32:46 +01001168 visit_end_list(v, NULL);
Markus Armbruster9cb8ef32017-03-03 13:32:43 +01001169}
1170
1171static void test_visitor_in_fail_list_nested(TestInputVisitorData *data,
1172 const void *unused)
1173{
1174 int64_t i64 = -1;
Markus Armbrustera4a1c702017-03-03 13:32:45 +01001175 Error *err = NULL;
Markus Armbruster9cb8ef32017-03-03 13:32:43 +01001176 Visitor *v;
1177
1178 /* Unvisited nested list tail */
1179
1180 v = visitor_input_test_init(data, "[ 0, [ 1, 2, 3 ] ]");
1181
1182 visit_start_list(v, NULL, NULL, 0, &error_abort);
1183 visit_type_int(v, NULL, &i64, &error_abort);
1184 g_assert_cmpint(i64, ==, 0);
1185 visit_start_list(v, NULL, NULL, 0, &error_abort);
1186 visit_type_int(v, NULL, &i64, &error_abort);
1187 g_assert_cmpint(i64, ==, 1);
Markus Armbrustera4a1c702017-03-03 13:32:45 +01001188 visit_check_list(v, &err);
1189 error_free_or_abort(&err);
Markus Armbruster9cb8ef32017-03-03 13:32:43 +01001190 visit_end_list(v, NULL);
Markus Armbrustera4a1c702017-03-03 13:32:45 +01001191 visit_check_list(v, &error_abort);
Markus Armbruster9cb8ef32017-03-03 13:32:43 +01001192 visit_end_list(v, NULL);
1193}
1194
Markus Armbrusterb359f4b2019-03-01 16:40:49 +01001195static void test_visitor_in_fail_union_list(TestInputVisitorData *data,
1196 const void *unused)
Markus Armbruster77c47de2017-03-03 13:32:40 +01001197{
Markus Armbrusterb359f4b2019-03-01 16:40:49 +01001198 UserDefListUnion *tmp = NULL;
Markus Armbruster77c47de2017-03-03 13:32:40 +01001199 Error *err = NULL;
1200 Visitor *v;
1201
1202 v = visitor_input_test_init(data,
1203 "{ 'type': 'integer', 'data' : [ 'string' ] }");
1204
Markus Armbrusterb359f4b2019-03-01 16:40:49 +01001205 visit_type_UserDefListUnion(v, NULL, &tmp, &err);
Markus Armbruster77c47de2017-03-03 13:32:40 +01001206 error_free_or_abort(&err);
1207 g_assert(!tmp);
1208}
1209
1210static void test_visitor_in_fail_union_flat(TestInputVisitorData *data,
1211 const void *unused)
1212{
1213 UserDefFlatUnion *tmp = NULL;
1214 Error *err = NULL;
1215 Visitor *v;
1216
1217 v = visitor_input_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
1218
1219 visit_type_UserDefFlatUnion(v, NULL, &tmp, &err);
1220 error_free_or_abort(&err);
1221 g_assert(!tmp);
1222}
1223
1224static void test_visitor_in_fail_union_flat_no_discrim(TestInputVisitorData *data,
1225 const void *unused)
1226{
1227 UserDefFlatUnion2 *tmp = NULL;
1228 Error *err = NULL;
1229 Visitor *v;
1230
1231 /* test situation where discriminator field ('enum1' here) is missing */
1232 v = visitor_input_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
1233
1234 visit_type_UserDefFlatUnion2(v, NULL, &tmp, &err);
1235 error_free_or_abort(&err);
1236 g_assert(!tmp);
1237}
1238
1239static void test_visitor_in_fail_alternate(TestInputVisitorData *data,
1240 const void *unused)
1241{
1242 UserDefAlternate *tmp;
1243 Visitor *v;
1244 Error *err = NULL;
1245
1246 v = visitor_input_test_init(data, "3.14");
1247
1248 visit_type_UserDefAlternate(v, NULL, &tmp, &err);
1249 error_free_or_abort(&err);
1250 g_assert(!tmp);
1251}
1252
1253static void do_test_visitor_in_qmp_introspect(TestInputVisitorData *data,
Marc-André Lureau7d0f9822018-03-05 18:29:51 +01001254 const QLitObject *qlit)
Markus Armbruster77c47de2017-03-03 13:32:40 +01001255{
1256 SchemaInfoList *schema = NULL;
Marc-André Lureau7d0f9822018-03-05 18:29:51 +01001257 QObject *obj = qobject_from_qlit(qlit);
Markus Armbruster77c47de2017-03-03 13:32:40 +01001258 Visitor *v;
1259
Marc-André Lureau7d0f9822018-03-05 18:29:51 +01001260 v = qobject_input_visitor_new(obj);
Markus Armbruster77c47de2017-03-03 13:32:40 +01001261
1262 visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
1263 g_assert(schema);
1264
1265 qapi_free_SchemaInfoList(schema);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001266 qobject_unref(obj);
Marc-André Lureau7d0f9822018-03-05 18:29:51 +01001267 visit_free(v);
Markus Armbruster77c47de2017-03-03 13:32:40 +01001268}
1269
1270static void test_visitor_in_qmp_introspect(TestInputVisitorData *data,
1271 const void *unused)
1272{
Marc-André Lureau7d0f9822018-03-05 18:29:51 +01001273 do_test_visitor_in_qmp_introspect(data, &test_qmp_schema_qlit);
Markus Armbruster77c47de2017-03-03 13:32:40 +01001274}
1275
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -02001276int main(int argc, char **argv)
1277{
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -02001278 g_test_init(&argc, &argv, NULL);
1279
1280 input_visitor_test_add("/visitor/input/int",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001281 NULL, test_visitor_in_int);
Markus Armbruster4bc0c942017-03-21 18:44:50 +01001282 input_visitor_test_add("/visitor/input/uint",
1283 NULL, test_visitor_in_uint);
Michael Rothe92cfa02013-05-10 17:46:06 -05001284 input_visitor_test_add("/visitor/input/int_overflow",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001285 NULL, test_visitor_in_int_overflow);
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +01001286 input_visitor_test_add("/visitor/input/int_keyval",
1287 NULL, test_visitor_in_int_keyval);
1288 input_visitor_test_add("/visitor/input/int_str_keyval",
1289 NULL, test_visitor_in_int_str_keyval);
1290 input_visitor_test_add("/visitor/input/int_str_fail",
1291 NULL, test_visitor_in_int_str_fail);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -02001292 input_visitor_test_add("/visitor/input/bool",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001293 NULL, test_visitor_in_bool);
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +01001294 input_visitor_test_add("/visitor/input/bool_keyval",
1295 NULL, test_visitor_in_bool_keyval);
1296 input_visitor_test_add("/visitor/input/bool_str_keyval",
1297 NULL, test_visitor_in_bool_str_keyval);
1298 input_visitor_test_add("/visitor/input/bool_str_fail",
1299 NULL, test_visitor_in_bool_str_fail);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -02001300 input_visitor_test_add("/visitor/input/number",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001301 NULL, test_visitor_in_number);
Marc-André Lureauc1214ad2017-06-07 20:35:56 +04001302 input_visitor_test_add("/visitor/input/large_number",
1303 NULL, test_visitor_in_large_number);
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +01001304 input_visitor_test_add("/visitor/input/number_keyval",
1305 NULL, test_visitor_in_number_keyval);
1306 input_visitor_test_add("/visitor/input/number_str_keyval",
1307 NULL, test_visitor_in_number_str_keyval);
1308 input_visitor_test_add("/visitor/input/number_str_fail",
1309 NULL, test_visitor_in_number_str_fail);
1310 input_visitor_test_add("/visitor/input/size_str_keyval",
1311 NULL, test_visitor_in_size_str_keyval);
1312 input_visitor_test_add("/visitor/input/size_str_fail",
1313 NULL, test_visitor_in_size_str_fail);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -02001314 input_visitor_test_add("/visitor/input/string",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001315 NULL, test_visitor_in_string);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -02001316 input_visitor_test_add("/visitor/input/enum",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001317 NULL, test_visitor_in_enum);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -02001318 input_visitor_test_add("/visitor/input/struct",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001319 NULL, test_visitor_in_struct);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -02001320 input_visitor_test_add("/visitor/input/struct-nested",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001321 NULL, test_visitor_in_struct_nested);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -02001322 input_visitor_test_add("/visitor/input/list",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001323 NULL, test_visitor_in_list);
Markus Armbruster28770e02015-09-16 13:06:24 +02001324 input_visitor_test_add("/visitor/input/any",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001325 NULL, test_visitor_in_any);
Eric Blake3df016f2016-04-28 15:45:23 -06001326 input_visitor_test_add("/visitor/input/null",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001327 NULL, test_visitor_in_null);
Markus Armbruster2fc00432014-03-01 08:40:33 +01001328 input_visitor_test_add("/visitor/input/union-flat",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001329 NULL, test_visitor_in_union_flat);
Eric Blakeab045262015-05-04 09:05:11 -06001330 input_visitor_test_add("/visitor/input/alternate",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001331 NULL, test_visitor_in_alternate);
Paolo Bonzini3dcf71f2012-03-22 12:51:03 +01001332 input_visitor_test_add("/visitor/input/errors",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001333 NULL, test_visitor_in_errors);
Eric Blake25333772015-11-05 23:35:34 -07001334 input_visitor_test_add("/visitor/input/wrong-type",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001335 NULL, test_visitor_in_wrong_type);
Eric Blake9c51b442015-09-29 16:21:06 -06001336 input_visitor_test_add("/visitor/input/alternate-number",
Daniel P. Berrangeb1d2e5f2016-09-30 15:45:29 +01001337 NULL, test_visitor_in_alternate_number);
Markus Armbrusterb359f4b2019-03-01 16:40:49 +01001338 input_visitor_test_add("/visitor/input/list_union/int",
1339 NULL, test_visitor_in_list_union_int);
1340 input_visitor_test_add("/visitor/input/list_union/int8",
1341 NULL, test_visitor_in_list_union_int8);
1342 input_visitor_test_add("/visitor/input/list_union/int16",
1343 NULL, test_visitor_in_list_union_int16);
1344 input_visitor_test_add("/visitor/input/list_union/int32",
1345 NULL, test_visitor_in_list_union_int32);
1346 input_visitor_test_add("/visitor/input/list_union/int64",
1347 NULL, test_visitor_in_list_union_int64);
1348 input_visitor_test_add("/visitor/input/list_union/uint8",
1349 NULL, test_visitor_in_list_union_uint8);
1350 input_visitor_test_add("/visitor/input/list_union/uint16",
1351 NULL, test_visitor_in_list_union_uint16);
1352 input_visitor_test_add("/visitor/input/list_union/uint32",
1353 NULL, test_visitor_in_list_union_uint32);
1354 input_visitor_test_add("/visitor/input/list_union/uint64",
1355 NULL, test_visitor_in_list_union_uint64);
1356 input_visitor_test_add("/visitor/input/list_union/bool",
1357 NULL, test_visitor_in_list_union_bool);
1358 input_visitor_test_add("/visitor/input/list_union/str",
1359 NULL, test_visitor_in_list_union_string);
1360 input_visitor_test_add("/visitor/input/list_union/number",
1361 NULL, test_visitor_in_list_union_number);
Markus Armbruster77c47de2017-03-03 13:32:40 +01001362 input_visitor_test_add("/visitor/input/fail/struct",
1363 NULL, test_visitor_in_fail_struct);
1364 input_visitor_test_add("/visitor/input/fail/struct-nested",
1365 NULL, test_visitor_in_fail_struct_nested);
1366 input_visitor_test_add("/visitor/input/fail/struct-in-list",
1367 NULL, test_visitor_in_fail_struct_in_list);
1368 input_visitor_test_add("/visitor/input/fail/struct-missing",
1369 NULL, test_visitor_in_fail_struct_missing);
Markus Armbruster9cb8ef32017-03-03 13:32:43 +01001370 input_visitor_test_add("/visitor/input/fail/list",
1371 NULL, test_visitor_in_fail_list);
1372 input_visitor_test_add("/visitor/input/fail/list-nested",
1373 NULL, test_visitor_in_fail_list_nested);
Markus Armbruster77c47de2017-03-03 13:32:40 +01001374 input_visitor_test_add("/visitor/input/fail/union-flat",
1375 NULL, test_visitor_in_fail_union_flat);
1376 input_visitor_test_add("/visitor/input/fail/union-flat-no-discriminator",
1377 NULL, test_visitor_in_fail_union_flat_no_discrim);
1378 input_visitor_test_add("/visitor/input/fail/alternate",
1379 NULL, test_visitor_in_fail_alternate);
Markus Armbrusterb359f4b2019-03-01 16:40:49 +01001380 input_visitor_test_add("/visitor/input/fail/union-list",
1381 NULL, test_visitor_in_fail_union_list);
Markus Armbrustereb815e22018-02-11 10:36:05 +01001382 input_visitor_test_add("/visitor/input/qapi-introspect",
Markus Armbruster77c47de2017-03-03 13:32:40 +01001383 NULL, test_visitor_in_qmp_introspect);
Luiz Capitulinod88f5fd2011-11-14 23:31:51 -02001384
1385 g_test_run();
1386
1387 return 0;
1388}