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