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