]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-qobject-input-visitor.c
qapi: Separate type QNull from QObject
[mirror_qemu.git] / tests / test-qobject-input-visitor.c
CommitLineData
d88f5fd1 1/*
b3db211f 2 * QObject Input Visitor unit-tests.
d88f5fd1 3 *
68d07839 4 * Copyright (C) 2011-2016 Red Hat Inc.
d88f5fd1
LC
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
77c47de2 8 * Paolo Bonzini <pbonzini@redhat.com>
d88f5fd1
LC
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
12 */
13
681c28a3 14#include "qemu/osdep.h"
d88f5fd1 15
79ee7df8 16#include "qemu-common.h"
da34e65c 17#include "qapi/error.h"
b3db211f 18#include "qapi/qobject-input-visitor.h"
d88f5fd1
LC
19#include "test-qapi-types.h"
20#include "test-qapi-visit.h"
7b1b5d19 21#include "qapi/qmp/types.h"
c7eb39cb 22#include "qapi/qmp/qjson.h"
77c47de2
MA
23#include "test-qmp-introspect.h"
24#include "qmp-introspect.h"
25#include "qapi-visit.h"
d88f5fd1
LC
26
27typedef struct TestInputVisitorData {
28 QObject *obj;
b70ce101 29 Visitor *qiv;
d88f5fd1
LC
30} TestInputVisitorData;
31
32static void visitor_input_teardown(TestInputVisitorData *data,
33 const void *unused)
34{
35 qobject_decref(data->obj);
36 data->obj = NULL;
37
38 if (data->qiv) {
b70ce101 39 visit_free(data->qiv);
d88f5fd1
LC
40 data->qiv = NULL;
41 }
42}
43
0920a171
EB
44/* The various test_init functions are provided instead of a test setup
45 function so that the JSON string used by the tests are kept in the test
46 functions (and not in main()). */
47static Visitor *visitor_input_test_init_internal(TestInputVisitorData *data,
cbd8acf3 48 bool keyval,
0920a171
EB
49 const char *json_string,
50 va_list *ap)
51{
b18f1141
EB
52 visitor_input_teardown(data, NULL);
53
bff17e84 54 data->obj = qobject_from_jsonv(json_string, ap, &error_abort);
0920a171
EB
55 g_assert(data->obj);
56
cbd8acf3
DB
57 if (keyval) {
58 data->qiv = qobject_input_visitor_new_keyval(data->obj);
59 } else {
60 data->qiv = qobject_input_visitor_new(data->obj);
61 }
0920a171 62 g_assert(data->qiv);
b70ce101 63 return data->qiv;
0920a171
EB
64}
65
cbd8acf3
DB
66static GCC_FMT_ATTR(3, 4)
67Visitor *visitor_input_test_init_full(TestInputVisitorData *data,
68 bool keyval,
69 const char *json_string, ...)
70{
71 Visitor *v;
72 va_list ap;
73
74 va_start(ap, json_string);
75 v = visitor_input_test_init_internal(data, keyval, json_string, &ap);
76 va_end(ap);
77 return v;
78}
79
aba2107a
SW
80static GCC_FMT_ATTR(2, 3)
81Visitor *visitor_input_test_init(TestInputVisitorData *data,
82 const char *json_string, ...)
d88f5fd1
LC
83{
84 Visitor *v;
85 va_list ap;
86
87 va_start(ap, json_string);
cbd8acf3 88 v = visitor_input_test_init_internal(data, false, json_string, &ap);
d88f5fd1 89 va_end(ap);
d88f5fd1
LC
90 return v;
91}
92
199e0f17
MR
93/* similar to visitor_input_test_init(), but does not expect a string
94 * literal/format json_string argument and so can be used for
95 * programatically generated strings (and we can't pass in programatically
96 * generated strings via %s format parameters since qobject_from_jsonv()
97 * will wrap those in double-quotes and treat the entire object as a
98 * string)
99 */
100static Visitor *visitor_input_test_init_raw(TestInputVisitorData *data,
101 const char *json_string)
102{
cbd8acf3 103 return visitor_input_test_init_internal(data, false, json_string, NULL);
199e0f17
MR
104}
105
d88f5fd1
LC
106static void test_visitor_in_int(TestInputVisitorData *data,
107 const void *unused)
108{
29a6731a 109 int64_t res = 0;
c1214ad3 110 double dbl;
29a6731a 111 int value = -42;
d88f5fd1
LC
112 Visitor *v;
113
29a6731a 114 v = visitor_input_test_init(data, "%d", value);
d88f5fd1 115
51e72bc1 116 visit_type_int(v, NULL, &res, &error_abort);
d88f5fd1 117 g_assert_cmpint(res, ==, value);
c1214ad3
MAL
118
119 visit_type_number(v, NULL, &dbl, &error_abort);
120 g_assert_cmpfloat(dbl, ==, -42.0);
d88f5fd1
LC
121}
122
4bc0c94d
MA
123static void test_visitor_in_uint(TestInputVisitorData *data,
124 const void *unused)
125{
4bc0c94d 126 uint64_t res = 0;
c1214ad3
MAL
127 int64_t i64;
128 double dbl;
4bc0c94d
MA
129 int value = 42;
130 Visitor *v;
131
132 v = visitor_input_test_init(data, "%d", value);
133
134 visit_type_uint64(v, NULL, &res, &error_abort);
135 g_assert_cmpuint(res, ==, (uint64_t)value);
136
c1214ad3
MAL
137 visit_type_int(v, NULL, &i64, &error_abort);
138 g_assert_cmpint(i64, ==, value);
139
140 visit_type_number(v, NULL, &dbl, &error_abort);
141 g_assert_cmpfloat(dbl, ==, value);
4bc0c94d 142
c1214ad3 143 /* BUG: value between INT64_MIN and -1 accepted modulo 2^64 */
4bc0c94d
MA
144 v = visitor_input_test_init(data, "%d", -value);
145
146 visit_type_uint64(v, NULL, &res, &error_abort);
147 g_assert_cmpuint(res, ==, (uint64_t)-value);
148
4bc0c94d
MA
149 v = visitor_input_test_init(data, "18446744073709551574");
150
5923f85f
MAL
151 visit_type_uint64(v, NULL, &res, &error_abort);
152 g_assert_cmpuint(res, ==, 18446744073709551574U);
c1214ad3
MAL
153
154 visit_type_number(v, NULL, &dbl, &error_abort);
155 g_assert_cmpfloat(dbl, ==, 18446744073709552000.0);
4bc0c94d
MA
156}
157
e92cfa0d
MR
158static void test_visitor_in_int_overflow(TestInputVisitorData *data,
159 const void *unused)
160{
161 int64_t res = 0;
e940f543 162 Error *err = NULL;
e92cfa0d
MR
163 Visitor *v;
164
01b2ffce
MAL
165 /*
166 * This will overflow a QNUM_I64, so should be deserialized into a
167 * QNUM_DOUBLE field instead, leading to an error if we pass it to
168 * visit_type_int(). Confirm this.
e92cfa0d
MR
169 */
170 v = visitor_input_test_init(data, "%f", DBL_MAX);
171
51e72bc1 172 visit_type_int(v, NULL, &res, &err);
a12a5a1a 173 error_free_or_abort(&err);
e92cfa0d
MR
174}
175
cbd8acf3
DB
176static void test_visitor_in_int_keyval(TestInputVisitorData *data,
177 const void *unused)
178{
179 int64_t res = 0, value = -42;
180 Error *err = NULL;
181 Visitor *v;
182
183 v = visitor_input_test_init_full(data, true, "%" PRId64, value);
184 visit_type_int(v, NULL, &res, &err);
185 error_free_or_abort(&err);
186}
187
188static void test_visitor_in_int_str_keyval(TestInputVisitorData *data,
189 const void *unused)
190{
191 int64_t res = 0, value = -42;
192 Visitor *v;
193
194 v = visitor_input_test_init_full(data, true, "\"-42\"");
195
196 visit_type_int(v, NULL, &res, &error_abort);
197 g_assert_cmpint(res, ==, value);
198}
199
200static void test_visitor_in_int_str_fail(TestInputVisitorData *data,
201 const void *unused)
202{
203 int64_t res = 0;
204 Visitor *v;
205 Error *err = NULL;
206
207 v = visitor_input_test_init(data, "\"-42\"");
208
209 visit_type_int(v, NULL, &res, &err);
210 error_free_or_abort(&err);
211}
212
d88f5fd1
LC
213static void test_visitor_in_bool(TestInputVisitorData *data,
214 const void *unused)
215{
d88f5fd1
LC
216 bool res = false;
217 Visitor *v;
218
219 v = visitor_input_test_init(data, "true");
220
51e72bc1 221 visit_type_bool(v, NULL, &res, &error_abort);
d88f5fd1
LC
222 g_assert_cmpint(res, ==, true);
223}
224
cbd8acf3
DB
225static void test_visitor_in_bool_keyval(TestInputVisitorData *data,
226 const void *unused)
227{
228 bool res = false;
229 Error *err = NULL;
230 Visitor *v;
231
232 v = visitor_input_test_init_full(data, true, "true");
233
234 visit_type_bool(v, NULL, &res, &err);
235 error_free_or_abort(&err);
236}
237
238static void test_visitor_in_bool_str_keyval(TestInputVisitorData *data,
239 const void *unused)
240{
241 bool res = false;
242 Visitor *v;
243
244 v = visitor_input_test_init_full(data, true, "\"on\"");
245
246 visit_type_bool(v, NULL, &res, &error_abort);
247 g_assert_cmpint(res, ==, true);
248}
249
250static void test_visitor_in_bool_str_fail(TestInputVisitorData *data,
251 const void *unused)
252{
253 bool res = false;
254 Visitor *v;
255 Error *err = NULL;
256
257 v = visitor_input_test_init(data, "\"true\"");
258
259 visit_type_bool(v, NULL, &res, &err);
260 error_free_or_abort(&err);
261}
262
d88f5fd1
LC
263static void test_visitor_in_number(TestInputVisitorData *data,
264 const void *unused)
265{
266 double res = 0, value = 3.14;
d88f5fd1
LC
267 Visitor *v;
268
269 v = visitor_input_test_init(data, "%f", value);
270
51e72bc1 271 visit_type_number(v, NULL, &res, &error_abort);
d88f5fd1
LC
272 g_assert_cmpfloat(res, ==, value);
273}
274
c1214ad3
MAL
275static void test_visitor_in_large_number(TestInputVisitorData *data,
276 const void *unused)
277{
278 Error *err = NULL;
279 double res = 0;
280 int64_t i64;
281 uint64_t u64;
282 Visitor *v;
283
284 v = visitor_input_test_init(data, "-18446744073709551616"); /* -2^64 */
285
286 visit_type_number(v, NULL, &res, &error_abort);
287 g_assert_cmpfloat(res, ==, -18446744073709552e3);
288
289 visit_type_int(v, NULL, &i64, &err);
290 error_free_or_abort(&err);
291
292 visit_type_uint64(v, NULL, &u64, &err);
293 error_free_or_abort(&err);
294}
295
cbd8acf3
DB
296static void test_visitor_in_number_keyval(TestInputVisitorData *data,
297 const void *unused)
298{
299 double res = 0, value = 3.14;
300 Error *err = NULL;
301 Visitor *v;
302
303 v = visitor_input_test_init_full(data, true, "%f", value);
304
305 visit_type_number(v, NULL, &res, &err);
306 error_free_or_abort(&err);
307}
308
309static void test_visitor_in_number_str_keyval(TestInputVisitorData *data,
310 const void *unused)
311{
312 double res = 0, value = 3.14;
313 Visitor *v;
5891c388 314 Error *err = NULL;
cbd8acf3
DB
315
316 v = visitor_input_test_init_full(data, true, "\"3.14\"");
317
318 visit_type_number(v, NULL, &res, &error_abort);
319 g_assert_cmpfloat(res, ==, value);
5891c388
MA
320
321 v = visitor_input_test_init_full(data, true, "\"inf\"");
322
323 visit_type_number(v, NULL, &res, &err);
324 error_free_or_abort(&err);
cbd8acf3
DB
325}
326
327static void test_visitor_in_number_str_fail(TestInputVisitorData *data,
328 const void *unused)
329{
330 double res = 0;
331 Visitor *v;
332 Error *err = NULL;
333
334 v = visitor_input_test_init(data, "\"3.14\"");
335
336 visit_type_number(v, NULL, &res, &err);
337 error_free_or_abort(&err);
338}
339
340static void test_visitor_in_size_str_keyval(TestInputVisitorData *data,
341 const void *unused)
342{
343 uint64_t res, value = 500 * 1024 * 1024;
344 Visitor *v;
345
346 v = visitor_input_test_init_full(data, true, "\"500M\"");
347
348 visit_type_size(v, NULL, &res, &error_abort);
349 g_assert_cmpfloat(res, ==, value);
350}
351
352static void test_visitor_in_size_str_fail(TestInputVisitorData *data,
353 const void *unused)
354{
355 uint64_t res = 0;
356 Visitor *v;
357 Error *err = NULL;
358
359 v = visitor_input_test_init(data, "\"500M\"");
360
361 visit_type_size(v, NULL, &res, &err);
362 error_free_or_abort(&err);
363}
364
d88f5fd1
LC
365static void test_visitor_in_string(TestInputVisitorData *data,
366 const void *unused)
367{
368 char *res = NULL, *value = (char *) "Q E M U";
d88f5fd1
LC
369 Visitor *v;
370
371 v = visitor_input_test_init(data, "%s", value);
372
51e72bc1 373 visit_type_str(v, NULL, &res, &error_abort);
d88f5fd1
LC
374 g_assert_cmpstr(res, ==, value);
375
376 g_free(res);
377}
378
379static void test_visitor_in_enum(TestInputVisitorData *data,
380 const void *unused)
381{
d88f5fd1
LC
382 Visitor *v;
383 EnumOne i;
384
385 for (i = 0; EnumOne_lookup[i]; i++) {
386 EnumOne res = -1;
387
388 v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]);
389
51e72bc1 390 visit_type_EnumOne(v, NULL, &res, &error_abort);
d88f5fd1 391 g_assert_cmpint(i, ==, res);
d88f5fd1 392 }
d88f5fd1
LC
393}
394
d88f5fd1
LC
395
396static void test_visitor_in_struct(TestInputVisitorData *data,
397 const void *unused)
398{
399 TestStruct *p = NULL;
d88f5fd1
LC
400 Visitor *v;
401
402 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
403
51e72bc1 404 visit_type_TestStruct(v, NULL, &p, &error_abort);
d88f5fd1
LC
405 g_assert_cmpint(p->integer, ==, -42);
406 g_assert(p->boolean == true);
407 g_assert_cmpstr(p->string, ==, "foo");
408
409 g_free(p->string);
410 g_free(p);
411}
412
d88f5fd1
LC
413static void test_visitor_in_struct_nested(TestInputVisitorData *data,
414 const void *unused)
415{
b6fcf32d 416 UserDefTwo *udp = NULL;
d88f5fd1
LC
417 Visitor *v;
418
b6fcf32d
EB
419 v = visitor_input_test_init(data, "{ 'string0': 'string0', "
420 "'dict1': { 'string1': 'string1', "
421 "'dict2': { 'userdef': { 'integer': 42, "
422 "'string': 'string' }, 'string': 'string2'}}}");
d88f5fd1 423
51e72bc1 424 visit_type_UserDefTwo(v, NULL, &udp, &error_abort);
d88f5fd1 425
b18f1141
EB
426 g_assert_cmpstr(udp->string0, ==, "string0");
427 g_assert_cmpstr(udp->dict1->string1, ==, "string1");
ddf21908 428 g_assert_cmpint(udp->dict1->dict2->userdef->integer, ==, 42);
b18f1141
EB
429 g_assert_cmpstr(udp->dict1->dict2->userdef->string, ==, "string");
430 g_assert_cmpstr(udp->dict1->dict2->string, ==, "string2");
6446a592
EB
431 g_assert(udp->dict1->has_dict3 == false);
432
b18f1141 433 qapi_free_UserDefTwo(udp);
d88f5fd1
LC
434}
435
436static void test_visitor_in_list(TestInputVisitorData *data,
437 const void *unused)
438{
439 UserDefOneList *item, *head = NULL;
d88f5fd1
LC
440 Visitor *v;
441 int i;
442
443 v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
444
51e72bc1 445 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
d88f5fd1
LC
446 g_assert(head != NULL);
447
448 for (i = 0, item = head; item; item = item->next, i++) {
449 char string[12];
450
451 snprintf(string, sizeof(string), "string%d", i);
452 g_assert_cmpstr(item->value->string, ==, string);
ddf21908 453 g_assert_cmpint(item->value->integer, ==, 42 + i);
d88f5fd1
LC
454 }
455
456 qapi_free_UserDefOneList(head);
2533377c
EB
457 head = NULL;
458
459 /* An empty list is valid */
460 v = visitor_input_test_init(data, "[]");
51e72bc1 461 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
2533377c 462 g_assert(!head);
d88f5fd1
LC
463}
464
28770e05
MA
465static void test_visitor_in_any(TestInputVisitorData *data,
466 const void *unused)
467{
468 QObject *res = NULL;
28770e05 469 Visitor *v;
01b2ffce 470 QNum *qnum;
28770e05
MA
471 QBool *qbool;
472 QString *qstring;
473 QDict *qdict;
474 QObject *qobj;
01b2ffce 475 int64_t val;
28770e05
MA
476
477 v = visitor_input_test_init(data, "-42");
51e72bc1 478 visit_type_any(v, NULL, &res, &error_abort);
01b2ffce
MAL
479 qnum = qobject_to_qnum(res);
480 g_assert(qnum);
481 g_assert(qnum_get_try_int(qnum, &val));
482 g_assert_cmpint(val, ==, -42);
28770e05
MA
483 qobject_decref(res);
484
485 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
51e72bc1 486 visit_type_any(v, NULL, &res, &error_abort);
28770e05
MA
487 qdict = qobject_to_qdict(res);
488 g_assert(qdict && qdict_size(qdict) == 3);
489 qobj = qdict_get(qdict, "integer");
490 g_assert(qobj);
01b2ffce
MAL
491 qnum = qobject_to_qnum(qobj);
492 g_assert(qnum);
493 g_assert(qnum_get_try_int(qnum, &val));
494 g_assert_cmpint(val, ==, -42);
28770e05
MA
495 qobj = qdict_get(qdict, "boolean");
496 g_assert(qobj);
497 qbool = qobject_to_qbool(qobj);
498 g_assert(qbool);
499 g_assert(qbool_get_bool(qbool) == true);
500 qobj = qdict_get(qdict, "string");
501 g_assert(qobj);
502 qstring = qobject_to_qstring(qobj);
503 g_assert(qstring);
504 g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
505 qobject_decref(res);
506}
507
3df016f1
EB
508static void test_visitor_in_null(TestInputVisitorData *data,
509 const void *unused)
510{
511 Visitor *v;
512 Error *err = NULL;
513 char *tmp;
514
515 /*
516 * FIXME: Since QAPI doesn't know the 'null' type yet, we can't
517 * test visit_type_null() by reading into a QAPI struct then
518 * checking that it was populated correctly. The best we can do
519 * for now is ensure that we consumed null from the input, proven
520 * by the fact that we can't re-read the key; and that we detect
521 * when input is not null.
522 */
523
cbd8acf3
DB
524 v = visitor_input_test_init_full(data, false,
525 "{ 'a': null, 'b': '' }");
3df016f1
EB
526 visit_start_struct(v, NULL, NULL, 0, &error_abort);
527 visit_type_null(v, "a", &error_abort);
3df016f1
EB
528 visit_type_null(v, "b", &err);
529 error_free_or_abort(&err);
ec95f614
MA
530 visit_type_str(v, "c", &tmp, &err);
531 g_assert(!tmp);
532 error_free_or_abort(&err);
15c2f669 533 visit_check_struct(v, &error_abort);
1158bb2a 534 visit_end_struct(v, NULL);
3df016f1
EB
535}
536
2fc00432
MA
537static void test_visitor_in_union_flat(TestInputVisitorData *data,
538 const void *unused)
539{
540 Visitor *v;
2fc00432 541 UserDefFlatUnion *tmp;
30594fe1 542 UserDefUnionBase *base;
2fc00432 543
5223070c
WX
544 v = visitor_input_test_init(data,
545 "{ 'enum1': 'value1', "
441cbac0 546 "'integer': 41, "
5223070c
WX
547 "'string': 'str', "
548 "'boolean': true }");
2fc00432 549
51e72bc1 550 visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort);
0f61af3e 551 g_assert_cmpint(tmp->enum1, ==, ENUM_ONE_VALUE1);
5223070c 552 g_assert_cmpstr(tmp->string, ==, "str");
441cbac0 553 g_assert_cmpint(tmp->integer, ==, 41);
544a3731 554 g_assert_cmpint(tmp->u.value1.boolean, ==, true);
30594fe1
EB
555
556 base = qapi_UserDefFlatUnion_base(tmp);
557 g_assert(&base->enum1 == &tmp->enum1);
558
2fc00432
MA
559 qapi_free_UserDefFlatUnion(tmp);
560}
561
ab045267
EB
562static void test_visitor_in_alternate(TestInputVisitorData *data,
563 const void *unused)
2c38b600
MA
564{
565 Visitor *v;
566 Error *err = NULL;
ab045267 567 UserDefAlternate *tmp;
68d07839 568 WrapAlternate *wrap;
2c38b600
MA
569
570 v = visitor_input_test_init(data, "42");
51e72bc1 571 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
01b2ffce 572 g_assert_cmpint(tmp->type, ==, QTYPE_QNUM);
c363acef 573 g_assert_cmpint(tmp->u.i, ==, 42);
ab045267 574 qapi_free_UserDefAlternate(tmp);
9c51b441 575
8168ca8e 576 v = visitor_input_test_init(data, "'value1'");
51e72bc1 577 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
0426d53c 578 g_assert_cmpint(tmp->type, ==, QTYPE_QSTRING);
8168ca8e 579 g_assert_cmpint(tmp->u.e, ==, ENUM_ONE_VALUE1);
9c51b441 580 qapi_free_UserDefAlternate(tmp);
9c51b441 581
68d07839
EB
582 v = visitor_input_test_init(data, "{'integer':1, 'string':'str', "
583 "'enum1':'value1', 'boolean':true}");
584 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
585 g_assert_cmpint(tmp->type, ==, QTYPE_QDICT);
becceedc
EB
586 g_assert_cmpint(tmp->u.udfu.integer, ==, 1);
587 g_assert_cmpstr(tmp->u.udfu.string, ==, "str");
588 g_assert_cmpint(tmp->u.udfu.enum1, ==, ENUM_ONE_VALUE1);
544a3731
EB
589 g_assert_cmpint(tmp->u.udfu.u.value1.boolean, ==, true);
590 g_assert_cmpint(tmp->u.udfu.u.value1.has_a_b, ==, false);
68d07839
EB
591 qapi_free_UserDefAlternate(tmp);
592
9c51b441 593 v = visitor_input_test_init(data, "false");
51e72bc1 594 visit_type_UserDefAlternate(v, NULL, &tmp, &err);
a12a5a1a 595 error_free_or_abort(&err);
9c51b441 596 qapi_free_UserDefAlternate(tmp);
68d07839
EB
597
598 v = visitor_input_test_init(data, "{ 'alt': 42 }");
599 visit_type_WrapAlternate(v, NULL, &wrap, &error_abort);
01b2ffce 600 g_assert_cmpint(wrap->alt->type, ==, QTYPE_QNUM);
68d07839
EB
601 g_assert_cmpint(wrap->alt->u.i, ==, 42);
602 qapi_free_WrapAlternate(wrap);
603
8168ca8e 604 v = visitor_input_test_init(data, "{ 'alt': 'value1' }");
68d07839
EB
605 visit_type_WrapAlternate(v, NULL, &wrap, &error_abort);
606 g_assert_cmpint(wrap->alt->type, ==, QTYPE_QSTRING);
8168ca8e 607 g_assert_cmpint(wrap->alt->u.e, ==, ENUM_ONE_VALUE1);
68d07839
EB
608 qapi_free_WrapAlternate(wrap);
609
610 v = visitor_input_test_init(data, "{ 'alt': {'integer':1, 'string':'str', "
611 "'enum1':'value1', 'boolean':true} }");
612 visit_type_WrapAlternate(v, NULL, &wrap, &error_abort);
613 g_assert_cmpint(wrap->alt->type, ==, QTYPE_QDICT);
becceedc
EB
614 g_assert_cmpint(wrap->alt->u.udfu.integer, ==, 1);
615 g_assert_cmpstr(wrap->alt->u.udfu.string, ==, "str");
616 g_assert_cmpint(wrap->alt->u.udfu.enum1, ==, ENUM_ONE_VALUE1);
544a3731
EB
617 g_assert_cmpint(wrap->alt->u.udfu.u.value1.boolean, ==, true);
618 g_assert_cmpint(wrap->alt->u.udfu.u.value1.has_a_b, ==, false);
68d07839 619 qapi_free_WrapAlternate(wrap);
9c51b441
EB
620}
621
622static void test_visitor_in_alternate_number(TestInputVisitorData *data,
623 const void *unused)
624{
625 Visitor *v;
626 Error *err = NULL;
8168ca8e
MA
627 AltEnumBool *aeb;
628 AltEnumNum *aen;
629 AltNumEnum *ans;
630 AltEnumInt *asi;
9c51b441
EB
631
632 /* Parsing an int */
633
634 v = visitor_input_test_init(data, "42");
8168ca8e 635 visit_type_AltEnumBool(v, NULL, &aeb, &err);
a12a5a1a 636 error_free_or_abort(&err);
8168ca8e 637 qapi_free_AltEnumBool(aeb);
9c51b441 638
9c51b441 639 v = visitor_input_test_init(data, "42");
8168ca8e 640 visit_type_AltEnumNum(v, NULL, &aen, &error_abort);
01b2ffce 641 g_assert_cmpint(aen->type, ==, QTYPE_QNUM);
8168ca8e
MA
642 g_assert_cmpfloat(aen->u.n, ==, 42);
643 qapi_free_AltEnumNum(aen);
9c51b441
EB
644
645 v = visitor_input_test_init(data, "42");
8168ca8e 646 visit_type_AltNumEnum(v, NULL, &ans, &error_abort);
01b2ffce 647 g_assert_cmpint(ans->type, ==, QTYPE_QNUM);
d00341af 648 g_assert_cmpfloat(ans->u.n, ==, 42);
8168ca8e 649 qapi_free_AltNumEnum(ans);
9c51b441
EB
650
651 v = visitor_input_test_init(data, "42");
8168ca8e 652 visit_type_AltEnumInt(v, NULL, &asi, &error_abort);
01b2ffce 653 g_assert_cmpint(asi->type, ==, QTYPE_QNUM);
c363acef 654 g_assert_cmpint(asi->u.i, ==, 42);
8168ca8e 655 qapi_free_AltEnumInt(asi);
9c51b441 656
9c51b441
EB
657 /* Parsing a double */
658
659 v = visitor_input_test_init(data, "42.5");
8168ca8e 660 visit_type_AltEnumBool(v, NULL, &aeb, &err);
a12a5a1a 661 error_free_or_abort(&err);
8168ca8e 662 qapi_free_AltEnumBool(aeb);
9c51b441
EB
663
664 v = visitor_input_test_init(data, "42.5");
8168ca8e 665 visit_type_AltEnumNum(v, NULL, &aen, &error_abort);
01b2ffce 666 g_assert_cmpint(aen->type, ==, QTYPE_QNUM);
8168ca8e
MA
667 g_assert_cmpfloat(aen->u.n, ==, 42.5);
668 qapi_free_AltEnumNum(aen);
9c51b441
EB
669
670 v = visitor_input_test_init(data, "42.5");
8168ca8e 671 visit_type_AltNumEnum(v, NULL, &ans, &error_abort);
01b2ffce 672 g_assert_cmpint(ans->type, ==, QTYPE_QNUM);
c363acef 673 g_assert_cmpfloat(ans->u.n, ==, 42.5);
8168ca8e 674 qapi_free_AltNumEnum(ans);
9c51b441
EB
675
676 v = visitor_input_test_init(data, "42.5");
8168ca8e 677 visit_type_AltEnumInt(v, NULL, &asi, &err);
a12a5a1a 678 error_free_or_abort(&err);
8168ca8e 679 qapi_free_AltEnumInt(asi);
2c38b600
MA
680}
681
199e0f17
MR
682static void test_native_list_integer_helper(TestInputVisitorData *data,
683 const void *unused,
684 UserDefNativeListUnionKind kind)
685{
686 UserDefNativeListUnion *cvalue = NULL;
199e0f17
MR
687 Visitor *v;
688 GString *gstr_list = g_string_new("");
689 GString *gstr_union = g_string_new("");
690 int i;
691
692 for (i = 0; i < 32; i++) {
693 g_string_append_printf(gstr_list, "%d", i);
694 if (i != 31) {
695 g_string_append(gstr_list, ", ");
696 }
697 }
698 g_string_append_printf(gstr_union, "{ 'type': '%s', 'data': [ %s ] }",
699 UserDefNativeListUnionKind_lookup[kind],
700 gstr_list->str);
701 v = visitor_input_test_init_raw(data, gstr_union->str);
702
51e72bc1 703 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
199e0f17 704 g_assert(cvalue != NULL);
c363acef 705 g_assert_cmpint(cvalue->type, ==, kind);
199e0f17
MR
706
707 switch (kind) {
708 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
709 intList *elem = NULL;
32bafa8f
EB
710 for (i = 0, elem = cvalue->u.integer.data;
711 elem; elem = elem->next, i++) {
199e0f17
MR
712 g_assert_cmpint(elem->value, ==, i);
713 }
714 break;
715 }
716 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
717 int8List *elem = NULL;
32bafa8f 718 for (i = 0, elem = cvalue->u.s8.data; elem; elem = elem->next, i++) {
199e0f17
MR
719 g_assert_cmpint(elem->value, ==, i);
720 }
721 break;
722 }
723 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
724 int16List *elem = NULL;
32bafa8f 725 for (i = 0, elem = cvalue->u.s16.data; elem; elem = elem->next, i++) {
199e0f17
MR
726 g_assert_cmpint(elem->value, ==, i);
727 }
728 break;
729 }
730 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
731 int32List *elem = NULL;
32bafa8f 732 for (i = 0, elem = cvalue->u.s32.data; elem; elem = elem->next, i++) {
199e0f17
MR
733 g_assert_cmpint(elem->value, ==, i);
734 }
735 break;
736 }
737 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
738 int64List *elem = NULL;
32bafa8f 739 for (i = 0, elem = cvalue->u.s64.data; elem; elem = elem->next, i++) {
199e0f17
MR
740 g_assert_cmpint(elem->value, ==, i);
741 }
742 break;
743 }
744 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
745 uint8List *elem = NULL;
32bafa8f 746 for (i = 0, elem = cvalue->u.u8.data; elem; elem = elem->next, i++) {
199e0f17
MR
747 g_assert_cmpint(elem->value, ==, i);
748 }
749 break;
750 }
751 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
752 uint16List *elem = NULL;
32bafa8f 753 for (i = 0, elem = cvalue->u.u16.data; elem; elem = elem->next, i++) {
199e0f17
MR
754 g_assert_cmpint(elem->value, ==, i);
755 }
756 break;
757 }
758 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
759 uint32List *elem = NULL;
32bafa8f 760 for (i = 0, elem = cvalue->u.u32.data; elem; elem = elem->next, i++) {
199e0f17
MR
761 g_assert_cmpint(elem->value, ==, i);
762 }
763 break;
764 }
765 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
766 uint64List *elem = NULL;
32bafa8f 767 for (i = 0, elem = cvalue->u.u64.data; elem; elem = elem->next, i++) {
199e0f17
MR
768 g_assert_cmpint(elem->value, ==, i);
769 }
770 break;
771 }
772 default:
dfc6f865 773 g_assert_not_reached();
199e0f17
MR
774 }
775
776 g_string_free(gstr_union, true);
777 g_string_free(gstr_list, true);
778 qapi_free_UserDefNativeListUnion(cvalue);
779}
780
781static void test_visitor_in_native_list_int(TestInputVisitorData *data,
782 const void *unused)
783{
784 test_native_list_integer_helper(data, unused,
785 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
786}
787
788static void test_visitor_in_native_list_int8(TestInputVisitorData *data,
789 const void *unused)
790{
791 test_native_list_integer_helper(data, unused,
792 USER_DEF_NATIVE_LIST_UNION_KIND_S8);
793}
794
795static void test_visitor_in_native_list_int16(TestInputVisitorData *data,
796 const void *unused)
797{
798 test_native_list_integer_helper(data, unused,
799 USER_DEF_NATIVE_LIST_UNION_KIND_S16);
800}
801
802static void test_visitor_in_native_list_int32(TestInputVisitorData *data,
803 const void *unused)
804{
805 test_native_list_integer_helper(data, unused,
806 USER_DEF_NATIVE_LIST_UNION_KIND_S32);
807}
808
809static void test_visitor_in_native_list_int64(TestInputVisitorData *data,
810 const void *unused)
811{
812 test_native_list_integer_helper(data, unused,
813 USER_DEF_NATIVE_LIST_UNION_KIND_S64);
814}
815
816static void test_visitor_in_native_list_uint8(TestInputVisitorData *data,
817 const void *unused)
818{
819 test_native_list_integer_helper(data, unused,
820 USER_DEF_NATIVE_LIST_UNION_KIND_U8);
821}
822
823static void test_visitor_in_native_list_uint16(TestInputVisitorData *data,
824 const void *unused)
825{
826 test_native_list_integer_helper(data, unused,
827 USER_DEF_NATIVE_LIST_UNION_KIND_U16);
828}
829
830static void test_visitor_in_native_list_uint32(TestInputVisitorData *data,
831 const void *unused)
832{
833 test_native_list_integer_helper(data, unused,
834 USER_DEF_NATIVE_LIST_UNION_KIND_U32);
835}
836
837static void test_visitor_in_native_list_uint64(TestInputVisitorData *data,
838 const void *unused)
839{
840 test_native_list_integer_helper(data, unused,
841 USER_DEF_NATIVE_LIST_UNION_KIND_U64);
842}
843
844static void test_visitor_in_native_list_bool(TestInputVisitorData *data,
845 const void *unused)
846{
847 UserDefNativeListUnion *cvalue = NULL;
848 boolList *elem = NULL;
199e0f17
MR
849 Visitor *v;
850 GString *gstr_list = g_string_new("");
851 GString *gstr_union = g_string_new("");
852 int i;
853
854 for (i = 0; i < 32; i++) {
855 g_string_append_printf(gstr_list, "%s",
856 (i % 3 == 0) ? "true" : "false");
857 if (i != 31) {
858 g_string_append(gstr_list, ", ");
859 }
860 }
861 g_string_append_printf(gstr_union, "{ 'type': 'boolean', 'data': [ %s ] }",
862 gstr_list->str);
863 v = visitor_input_test_init_raw(data, gstr_union->str);
864
51e72bc1 865 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
199e0f17 866 g_assert(cvalue != NULL);
c363acef 867 g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
199e0f17 868
32bafa8f 869 for (i = 0, elem = cvalue->u.boolean.data; elem; elem = elem->next, i++) {
199e0f17
MR
870 g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0);
871 }
872
873 g_string_free(gstr_union, true);
874 g_string_free(gstr_list, true);
875 qapi_free_UserDefNativeListUnion(cvalue);
876}
877
878static void test_visitor_in_native_list_string(TestInputVisitorData *data,
879 const void *unused)
880{
881 UserDefNativeListUnion *cvalue = NULL;
882 strList *elem = NULL;
199e0f17
MR
883 Visitor *v;
884 GString *gstr_list = g_string_new("");
885 GString *gstr_union = g_string_new("");
886 int i;
887
888 for (i = 0; i < 32; i++) {
889 g_string_append_printf(gstr_list, "'%d'", i);
890 if (i != 31) {
891 g_string_append(gstr_list, ", ");
892 }
893 }
894 g_string_append_printf(gstr_union, "{ 'type': 'string', 'data': [ %s ] }",
895 gstr_list->str);
896 v = visitor_input_test_init_raw(data, gstr_union->str);
897
51e72bc1 898 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
199e0f17 899 g_assert(cvalue != NULL);
c363acef 900 g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
199e0f17 901
32bafa8f 902 for (i = 0, elem = cvalue->u.string.data; elem; elem = elem->next, i++) {
199e0f17
MR
903 gchar str[8];
904 sprintf(str, "%d", i);
905 g_assert_cmpstr(elem->value, ==, str);
906 }
907
908 g_string_free(gstr_union, true);
909 g_string_free(gstr_list, true);
910 qapi_free_UserDefNativeListUnion(cvalue);
911}
912
913#define DOUBLE_STR_MAX 16
914
915static void test_visitor_in_native_list_number(TestInputVisitorData *data,
916 const void *unused)
917{
918 UserDefNativeListUnion *cvalue = NULL;
919 numberList *elem = NULL;
199e0f17
MR
920 Visitor *v;
921 GString *gstr_list = g_string_new("");
922 GString *gstr_union = g_string_new("");
923 int i;
924
925 for (i = 0; i < 32; i++) {
926 g_string_append_printf(gstr_list, "%f", (double)i / 3);
927 if (i != 31) {
928 g_string_append(gstr_list, ", ");
929 }
930 }
931 g_string_append_printf(gstr_union, "{ 'type': 'number', 'data': [ %s ] }",
932 gstr_list->str);
933 v = visitor_input_test_init_raw(data, gstr_union->str);
934
51e72bc1 935 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
199e0f17 936 g_assert(cvalue != NULL);
c363acef 937 g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
199e0f17 938
32bafa8f 939 for (i = 0, elem = cvalue->u.number.data; elem; elem = elem->next, i++) {
199e0f17
MR
940 GString *double_expected = g_string_new("");
941 GString *double_actual = g_string_new("");
942
943 g_string_printf(double_expected, "%.6f", (double)i / 3);
944 g_string_printf(double_actual, "%.6f", elem->value);
945 g_assert_cmpstr(double_expected->str, ==, double_actual->str);
946
947 g_string_free(double_expected, true);
948 g_string_free(double_actual, true);
949 }
950
951 g_string_free(gstr_union, true);
952 g_string_free(gstr_list, true);
953 qapi_free_UserDefNativeListUnion(cvalue);
954}
955
d88f5fd1 956static void input_visitor_test_add(const char *testpath,
b1d2e5f1
DB
957 const void *user_data,
958 void (*test_func)(TestInputVisitorData *data,
959 const void *user_data))
d88f5fd1 960{
b1d2e5f1 961 g_test_add(testpath, TestInputVisitorData, user_data, NULL, test_func,
d88f5fd1
LC
962 visitor_input_teardown);
963}
964
3dcf71f6
PB
965static void test_visitor_in_errors(TestInputVisitorData *data,
966 const void *unused)
967{
968 TestStruct *p = NULL;
e940f543 969 Error *err = NULL;
3dcf71f6 970 Visitor *v;
dd5ee2c2 971 strList *q = NULL;
9b4e38fe
EB
972 UserDefTwo *r = NULL;
973 WrapAlternate *s = NULL;
3dcf71f6 974
dd5ee2c2
EB
975 v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', "
976 "'string': -42 }");
3dcf71f6 977
51e72bc1 978 visit_type_TestStruct(v, NULL, &p, &err);
a12a5a1a 979 error_free_or_abort(&err);
68ab47e4 980 g_assert(!p);
dd5ee2c2
EB
981
982 v = visitor_input_test_init(data, "[ '1', '2', false, '3' ]");
51e72bc1 983 visit_type_strList(v, NULL, &q, &err);
dd5ee2c2 984 error_free_or_abort(&err);
68ab47e4 985 assert(!q);
9b4e38fe
EB
986
987 v = visitor_input_test_init(data, "{ 'str':'hi' }");
988 visit_type_UserDefTwo(v, NULL, &r, &err);
989 error_free_or_abort(&err);
990 assert(!r);
991
992 v = visitor_input_test_init(data, "{ }");
993 visit_type_WrapAlternate(v, NULL, &s, &err);
994 error_free_or_abort(&err);
995 assert(!s);
3dcf71f6
PB
996}
997
2533377c
EB
998static void test_visitor_in_wrong_type(TestInputVisitorData *data,
999 const void *unused)
1000{
1001 TestStruct *p = NULL;
1002 Visitor *v;
1003 strList *q = NULL;
1004 int64_t i;
1005 Error *err = NULL;
1006
1007 /* Make sure arrays and structs cannot be confused */
1008
1009 v = visitor_input_test_init(data, "[]");
51e72bc1 1010 visit_type_TestStruct(v, NULL, &p, &err);
2533377c
EB
1011 error_free_or_abort(&err);
1012 g_assert(!p);
1013
1014 v = visitor_input_test_init(data, "{}");
51e72bc1 1015 visit_type_strList(v, NULL, &q, &err);
2533377c
EB
1016 error_free_or_abort(&err);
1017 assert(!q);
1018
1019 /* Make sure primitives and struct cannot be confused */
1020
1021 v = visitor_input_test_init(data, "1");
51e72bc1 1022 visit_type_TestStruct(v, NULL, &p, &err);
2533377c
EB
1023 error_free_or_abort(&err);
1024 g_assert(!p);
1025
1026 v = visitor_input_test_init(data, "{}");
51e72bc1 1027 visit_type_int(v, NULL, &i, &err);
2533377c
EB
1028 error_free_or_abort(&err);
1029
1030 /* Make sure primitives and arrays cannot be confused */
1031
1032 v = visitor_input_test_init(data, "1");
51e72bc1 1033 visit_type_strList(v, NULL, &q, &err);
2533377c
EB
1034 error_free_or_abort(&err);
1035 assert(!q);
1036
1037 v = visitor_input_test_init(data, "[]");
51e72bc1 1038 visit_type_int(v, NULL, &i, &err);
2533377c
EB
1039 error_free_or_abort(&err);
1040}
1041
77c47de2
MA
1042static void test_visitor_in_fail_struct(TestInputVisitorData *data,
1043 const void *unused)
1044{
1045 TestStruct *p = NULL;
1046 Error *err = NULL;
1047 Visitor *v;
1048
1049 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
1050
1051 visit_type_TestStruct(v, NULL, &p, &err);
1052 error_free_or_abort(&err);
1053 g_assert(!p);
1054}
1055
1056static void test_visitor_in_fail_struct_nested(TestInputVisitorData *data,
1057 const void *unused)
1058{
1059 UserDefTwo *udp = NULL;
1060 Error *err = NULL;
1061 Visitor *v;
1062
1063 v = visitor_input_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
1064
1065 visit_type_UserDefTwo(v, NULL, &udp, &err);
1066 error_free_or_abort(&err);
1067 g_assert(!udp);
1068}
1069
1070static void test_visitor_in_fail_struct_in_list(TestInputVisitorData *data,
1071 const void *unused)
1072{
1073 UserDefOneList *head = NULL;
1074 Error *err = NULL;
1075 Visitor *v;
1076
1077 v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
1078
1079 visit_type_UserDefOneList(v, NULL, &head, &err);
1080 error_free_or_abort(&err);
1081 g_assert(!head);
1082}
1083
1084static void test_visitor_in_fail_struct_missing(TestInputVisitorData *data,
1085 const void *unused)
1086{
1087 Error *err = NULL;
1088 Visitor *v;
1089 QObject *any;
1090 GenericAlternate *alt;
1091 bool present;
1092 int en;
1093 int64_t i64;
1094 uint32_t u32;
1095 int8_t i8;
1096 char *str;
1097 double dbl;
1098
86ca0dbe 1099 v = visitor_input_test_init(data, "{ 'sub': [ {} ] }");
77c47de2
MA
1100 visit_start_struct(v, NULL, NULL, 0, &error_abort);
1101 visit_start_struct(v, "struct", NULL, 0, &err);
1102 error_free_or_abort(&err);
1103 visit_start_list(v, "list", NULL, 0, &err);
1104 error_free_or_abort(&err);
60390d2d 1105 visit_start_alternate(v, "alternate", &alt, sizeof(*alt), &err);
77c47de2
MA
1106 error_free_or_abort(&err);
1107 visit_optional(v, "optional", &present);
1108 g_assert(!present);
1109 visit_type_enum(v, "enum", &en, EnumOne_lookup, &err);
1110 error_free_or_abort(&err);
1111 visit_type_int(v, "i64", &i64, &err);
1112 error_free_or_abort(&err);
1113 visit_type_uint32(v, "u32", &u32, &err);
1114 error_free_or_abort(&err);
1115 visit_type_int8(v, "i8", &i8, &err);
1116 error_free_or_abort(&err);
1117 visit_type_str(v, "i8", &str, &err);
1118 error_free_or_abort(&err);
1119 visit_type_number(v, "dbl", &dbl, &err);
1120 error_free_or_abort(&err);
1121 visit_type_any(v, "any", &any, &err);
1122 error_free_or_abort(&err);
1123 visit_type_null(v, "null", &err);
1124 error_free_or_abort(&err);
86ca0dbe
MA
1125 visit_start_list(v, "sub", NULL, 0, &error_abort);
1126 visit_start_struct(v, NULL, NULL, 0, &error_abort);
1127 visit_type_int(v, "i64", &i64, &err);
1128 error_free_or_abort(&err);
1129 visit_end_struct(v, NULL);
1130 visit_end_list(v, NULL);
77c47de2
MA
1131 visit_end_struct(v, NULL);
1132}
1133
9cb8ef36
MA
1134static void test_visitor_in_fail_list(TestInputVisitorData *data,
1135 const void *unused)
1136{
1137 int64_t i64 = -1;
a4a1c70d 1138 Error *err = NULL;
9cb8ef36
MA
1139 Visitor *v;
1140
1141 /* Unvisited list tail */
1142
1143 v = visitor_input_test_init(data, "[ 1, 2, 3 ]");
1144
1145 visit_start_list(v, NULL, NULL, 0, &error_abort);
1146 visit_type_int(v, NULL, &i64, &error_abort);
1147 g_assert_cmpint(i64, ==, 1);
1148 visit_type_int(v, NULL, &i64, &error_abort);
1149 g_assert_cmpint(i64, ==, 2);
a4a1c70d
MA
1150 visit_check_list(v, &err);
1151 error_free_or_abort(&err);
9cb8ef36 1152 visit_end_list(v, NULL);
a9416dc6
MA
1153
1154 /* Visit beyond end of list */
1155 v = visitor_input_test_init(data, "[]");
1156
1157 visit_start_list(v, NULL, NULL, 0, &error_abort);
a9416dc6
MA
1158 visit_type_int(v, NULL, &i64, &err);
1159 error_free_or_abort(&err);
a9416dc6 1160 visit_end_list(v, NULL);
9cb8ef36
MA
1161}
1162
1163static void test_visitor_in_fail_list_nested(TestInputVisitorData *data,
1164 const void *unused)
1165{
1166 int64_t i64 = -1;
a4a1c70d 1167 Error *err = NULL;
9cb8ef36
MA
1168 Visitor *v;
1169
1170 /* Unvisited nested list tail */
1171
1172 v = visitor_input_test_init(data, "[ 0, [ 1, 2, 3 ] ]");
1173
1174 visit_start_list(v, NULL, NULL, 0, &error_abort);
1175 visit_type_int(v, NULL, &i64, &error_abort);
1176 g_assert_cmpint(i64, ==, 0);
1177 visit_start_list(v, NULL, NULL, 0, &error_abort);
1178 visit_type_int(v, NULL, &i64, &error_abort);
1179 g_assert_cmpint(i64, ==, 1);
a4a1c70d
MA
1180 visit_check_list(v, &err);
1181 error_free_or_abort(&err);
9cb8ef36 1182 visit_end_list(v, NULL);
a4a1c70d 1183 visit_check_list(v, &error_abort);
9cb8ef36
MA
1184 visit_end_list(v, NULL);
1185}
1186
77c47de2
MA
1187static void test_visitor_in_fail_union_native_list(TestInputVisitorData *data,
1188 const void *unused)
1189{
1190 UserDefNativeListUnion *tmp = NULL;
1191 Error *err = NULL;
1192 Visitor *v;
1193
1194 v = visitor_input_test_init(data,
1195 "{ 'type': 'integer', 'data' : [ 'string' ] }");
1196
1197 visit_type_UserDefNativeListUnion(v, NULL, &tmp, &err);
1198 error_free_or_abort(&err);
1199 g_assert(!tmp);
1200}
1201
1202static void test_visitor_in_fail_union_flat(TestInputVisitorData *data,
1203 const void *unused)
1204{
1205 UserDefFlatUnion *tmp = NULL;
1206 Error *err = NULL;
1207 Visitor *v;
1208
1209 v = visitor_input_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
1210
1211 visit_type_UserDefFlatUnion(v, NULL, &tmp, &err);
1212 error_free_or_abort(&err);
1213 g_assert(!tmp);
1214}
1215
1216static void test_visitor_in_fail_union_flat_no_discrim(TestInputVisitorData *data,
1217 const void *unused)
1218{
1219 UserDefFlatUnion2 *tmp = NULL;
1220 Error *err = NULL;
1221 Visitor *v;
1222
1223 /* test situation where discriminator field ('enum1' here) is missing */
1224 v = visitor_input_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
1225
1226 visit_type_UserDefFlatUnion2(v, NULL, &tmp, &err);
1227 error_free_or_abort(&err);
1228 g_assert(!tmp);
1229}
1230
1231static void test_visitor_in_fail_alternate(TestInputVisitorData *data,
1232 const void *unused)
1233{
1234 UserDefAlternate *tmp;
1235 Visitor *v;
1236 Error *err = NULL;
1237
1238 v = visitor_input_test_init(data, "3.14");
1239
1240 visit_type_UserDefAlternate(v, NULL, &tmp, &err);
1241 error_free_or_abort(&err);
1242 g_assert(!tmp);
1243}
1244
1245static void do_test_visitor_in_qmp_introspect(TestInputVisitorData *data,
1246 const char *schema_json)
1247{
1248 SchemaInfoList *schema = NULL;
1249 Visitor *v;
1250
1251 v = visitor_input_test_init_raw(data, schema_json);
1252
1253 visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
1254 g_assert(schema);
1255
1256 qapi_free_SchemaInfoList(schema);
1257}
1258
1259static void test_visitor_in_qmp_introspect(TestInputVisitorData *data,
1260 const void *unused)
1261{
1262 do_test_visitor_in_qmp_introspect(data, test_qmp_schema_json);
1263 do_test_visitor_in_qmp_introspect(data, qmp_schema_json);
1264}
1265
d88f5fd1
LC
1266int main(int argc, char **argv)
1267{
d88f5fd1
LC
1268 g_test_init(&argc, &argv, NULL);
1269
1270 input_visitor_test_add("/visitor/input/int",
b1d2e5f1 1271 NULL, test_visitor_in_int);
4bc0c94d
MA
1272 input_visitor_test_add("/visitor/input/uint",
1273 NULL, test_visitor_in_uint);
e92cfa0d 1274 input_visitor_test_add("/visitor/input/int_overflow",
b1d2e5f1 1275 NULL, test_visitor_in_int_overflow);
cbd8acf3
DB
1276 input_visitor_test_add("/visitor/input/int_keyval",
1277 NULL, test_visitor_in_int_keyval);
1278 input_visitor_test_add("/visitor/input/int_str_keyval",
1279 NULL, test_visitor_in_int_str_keyval);
1280 input_visitor_test_add("/visitor/input/int_str_fail",
1281 NULL, test_visitor_in_int_str_fail);
d88f5fd1 1282 input_visitor_test_add("/visitor/input/bool",
b1d2e5f1 1283 NULL, test_visitor_in_bool);
cbd8acf3
DB
1284 input_visitor_test_add("/visitor/input/bool_keyval",
1285 NULL, test_visitor_in_bool_keyval);
1286 input_visitor_test_add("/visitor/input/bool_str_keyval",
1287 NULL, test_visitor_in_bool_str_keyval);
1288 input_visitor_test_add("/visitor/input/bool_str_fail",
1289 NULL, test_visitor_in_bool_str_fail);
d88f5fd1 1290 input_visitor_test_add("/visitor/input/number",
b1d2e5f1 1291 NULL, test_visitor_in_number);
c1214ad3
MAL
1292 input_visitor_test_add("/visitor/input/large_number",
1293 NULL, test_visitor_in_large_number);
cbd8acf3
DB
1294 input_visitor_test_add("/visitor/input/number_keyval",
1295 NULL, test_visitor_in_number_keyval);
1296 input_visitor_test_add("/visitor/input/number_str_keyval",
1297 NULL, test_visitor_in_number_str_keyval);
1298 input_visitor_test_add("/visitor/input/number_str_fail",
1299 NULL, test_visitor_in_number_str_fail);
1300 input_visitor_test_add("/visitor/input/size_str_keyval",
1301 NULL, test_visitor_in_size_str_keyval);
1302 input_visitor_test_add("/visitor/input/size_str_fail",
1303 NULL, test_visitor_in_size_str_fail);
d88f5fd1 1304 input_visitor_test_add("/visitor/input/string",
b1d2e5f1 1305 NULL, test_visitor_in_string);
d88f5fd1 1306 input_visitor_test_add("/visitor/input/enum",
b1d2e5f1 1307 NULL, test_visitor_in_enum);
d88f5fd1 1308 input_visitor_test_add("/visitor/input/struct",
b1d2e5f1 1309 NULL, test_visitor_in_struct);
d88f5fd1 1310 input_visitor_test_add("/visitor/input/struct-nested",
b1d2e5f1 1311 NULL, test_visitor_in_struct_nested);
d88f5fd1 1312 input_visitor_test_add("/visitor/input/list",
b1d2e5f1 1313 NULL, test_visitor_in_list);
28770e05 1314 input_visitor_test_add("/visitor/input/any",
b1d2e5f1 1315 NULL, test_visitor_in_any);
3df016f1 1316 input_visitor_test_add("/visitor/input/null",
b1d2e5f1 1317 NULL, test_visitor_in_null);
2fc00432 1318 input_visitor_test_add("/visitor/input/union-flat",
b1d2e5f1 1319 NULL, test_visitor_in_union_flat);
ab045267 1320 input_visitor_test_add("/visitor/input/alternate",
b1d2e5f1 1321 NULL, test_visitor_in_alternate);
3dcf71f6 1322 input_visitor_test_add("/visitor/input/errors",
b1d2e5f1 1323 NULL, test_visitor_in_errors);
2533377c 1324 input_visitor_test_add("/visitor/input/wrong-type",
b1d2e5f1 1325 NULL, test_visitor_in_wrong_type);
9c51b441 1326 input_visitor_test_add("/visitor/input/alternate-number",
b1d2e5f1 1327 NULL, test_visitor_in_alternate_number);
199e0f17 1328 input_visitor_test_add("/visitor/input/native_list/int",
b1d2e5f1 1329 NULL, test_visitor_in_native_list_int);
199e0f17 1330 input_visitor_test_add("/visitor/input/native_list/int8",
b1d2e5f1 1331 NULL, test_visitor_in_native_list_int8);
199e0f17 1332 input_visitor_test_add("/visitor/input/native_list/int16",
b1d2e5f1 1333 NULL, test_visitor_in_native_list_int16);
199e0f17 1334 input_visitor_test_add("/visitor/input/native_list/int32",
b1d2e5f1 1335 NULL, test_visitor_in_native_list_int32);
199e0f17 1336 input_visitor_test_add("/visitor/input/native_list/int64",
b1d2e5f1 1337 NULL, test_visitor_in_native_list_int64);
199e0f17 1338 input_visitor_test_add("/visitor/input/native_list/uint8",
b1d2e5f1 1339 NULL, test_visitor_in_native_list_uint8);
199e0f17 1340 input_visitor_test_add("/visitor/input/native_list/uint16",
b1d2e5f1 1341 NULL, test_visitor_in_native_list_uint16);
199e0f17 1342 input_visitor_test_add("/visitor/input/native_list/uint32",
b1d2e5f1 1343 NULL, test_visitor_in_native_list_uint32);
199e0f17 1344 input_visitor_test_add("/visitor/input/native_list/uint64",
b1d2e5f1 1345 NULL, test_visitor_in_native_list_uint64);
199e0f17 1346 input_visitor_test_add("/visitor/input/native_list/bool",
b1d2e5f1 1347 NULL, test_visitor_in_native_list_bool);
199e0f17 1348 input_visitor_test_add("/visitor/input/native_list/str",
b1d2e5f1 1349 NULL, test_visitor_in_native_list_string);
199e0f17 1350 input_visitor_test_add("/visitor/input/native_list/number",
b1d2e5f1 1351 NULL, test_visitor_in_native_list_number);
77c47de2
MA
1352 input_visitor_test_add("/visitor/input/fail/struct",
1353 NULL, test_visitor_in_fail_struct);
1354 input_visitor_test_add("/visitor/input/fail/struct-nested",
1355 NULL, test_visitor_in_fail_struct_nested);
1356 input_visitor_test_add("/visitor/input/fail/struct-in-list",
1357 NULL, test_visitor_in_fail_struct_in_list);
1358 input_visitor_test_add("/visitor/input/fail/struct-missing",
1359 NULL, test_visitor_in_fail_struct_missing);
9cb8ef36
MA
1360 input_visitor_test_add("/visitor/input/fail/list",
1361 NULL, test_visitor_in_fail_list);
1362 input_visitor_test_add("/visitor/input/fail/list-nested",
1363 NULL, test_visitor_in_fail_list_nested);
77c47de2
MA
1364 input_visitor_test_add("/visitor/input/fail/union-flat",
1365 NULL, test_visitor_in_fail_union_flat);
1366 input_visitor_test_add("/visitor/input/fail/union-flat-no-discriminator",
1367 NULL, test_visitor_in_fail_union_flat_no_discrim);
1368 input_visitor_test_add("/visitor/input/fail/alternate",
1369 NULL, test_visitor_in_fail_alternate);
1370 input_visitor_test_add("/visitor/input/fail/union-native-list",
1371 NULL, test_visitor_in_fail_union_native_list);
1372 input_visitor_test_add("/visitor/input/qmp-introspect",
1373 NULL, test_visitor_in_qmp_introspect);
d88f5fd1
LC
1374
1375 g_test_run();
1376
1377 return 0;
1378}