11e8a1faeSThomas Huth /*
21e8a1faeSThomas Huth  * Device introspection test cases
31e8a1faeSThomas Huth  *
41e8a1faeSThomas Huth  * Copyright (c) 2015 Red Hat Inc.
51e8a1faeSThomas Huth  *
61e8a1faeSThomas Huth  * Authors:
71e8a1faeSThomas Huth  *  Markus Armbruster <armbru@redhat.com>,
81e8a1faeSThomas Huth  *
91e8a1faeSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
101e8a1faeSThomas Huth  * See the COPYING file in the top-level directory.
111e8a1faeSThomas Huth  */
121e8a1faeSThomas Huth 
131e8a1faeSThomas Huth /*
141e8a1faeSThomas Huth  * Covers QMP device-list-properties and HMP device_add help.  We
151e8a1faeSThomas Huth  * currently don't check that their output makes sense, only that QEMU
161e8a1faeSThomas Huth  * survives.  Useful since we've had an astounding number of crash
171e8a1faeSThomas Huth  * bugs around here.
181e8a1faeSThomas Huth  */
191e8a1faeSThomas Huth 
201e8a1faeSThomas Huth #include "qemu/osdep.h"
211e8a1faeSThomas Huth #include "qapi/qmp/qstring.h"
221e8a1faeSThomas Huth #include "qapi/qmp/qdict.h"
231e8a1faeSThomas Huth #include "qapi/qmp/qlist.h"
24*907b5105SMarc-André Lureau #include "libqtest.h"
251e8a1faeSThomas Huth 
261e8a1faeSThomas Huth const char common_args[] = "-nodefaults -machine none";
271e8a1faeSThomas Huth 
qom_list_types(QTestState * qts,const char * implements,bool abstract)281e8a1faeSThomas Huth static QList *qom_list_types(QTestState * qts, const char *implements,
291e8a1faeSThomas Huth                              bool abstract)
301e8a1faeSThomas Huth {
311e8a1faeSThomas Huth     QDict *resp;
321e8a1faeSThomas Huth     QList *ret;
331e8a1faeSThomas Huth     QDict *args = qdict_new();
341e8a1faeSThomas Huth 
351e8a1faeSThomas Huth     qdict_put_bool(args, "abstract", abstract);
361e8a1faeSThomas Huth     if (implements) {
371e8a1faeSThomas Huth         qdict_put_str(args, "implements", implements);
381e8a1faeSThomas Huth     }
391e8a1faeSThomas Huth     resp = qtest_qmp(qts, "{'execute': 'qom-list-types', 'arguments': %p }",
401e8a1faeSThomas Huth                      args);
411e8a1faeSThomas Huth     g_assert(qdict_haskey(resp, "return"));
421e8a1faeSThomas Huth     ret = qdict_get_qlist(resp, "return");
431e8a1faeSThomas Huth     qobject_ref(ret);
441e8a1faeSThomas Huth     qobject_unref(resp);
451e8a1faeSThomas Huth     return ret;
461e8a1faeSThomas Huth }
471e8a1faeSThomas Huth 
481e8a1faeSThomas Huth /* Build a name -> ObjectTypeInfo index from a ObjectTypeInfo list */
qom_type_index(QList * types)491e8a1faeSThomas Huth static QDict *qom_type_index(QList *types)
501e8a1faeSThomas Huth {
511e8a1faeSThomas Huth     QDict *index = qdict_new();
521e8a1faeSThomas Huth     QListEntry *e;
531e8a1faeSThomas Huth 
541e8a1faeSThomas Huth     QLIST_FOREACH_ENTRY(types, e) {
551e8a1faeSThomas Huth         QDict *d = qobject_to(QDict, qlist_entry_obj(e));
561e8a1faeSThomas Huth         const char *name = qdict_get_str(d, "name");
571e8a1faeSThomas Huth         qobject_ref(d);
581e8a1faeSThomas Huth         qdict_put(index, name, d);
591e8a1faeSThomas Huth     }
601e8a1faeSThomas Huth     return index;
611e8a1faeSThomas Huth }
621e8a1faeSThomas Huth 
631e8a1faeSThomas Huth /* Check if @parent is present in the parent chain of @type */
qom_has_parent(QDict * index,const char * type,const char * parent)641e8a1faeSThomas Huth static bool qom_has_parent(QDict *index, const char *type, const char *parent)
651e8a1faeSThomas Huth {
661e8a1faeSThomas Huth     while (type) {
671e8a1faeSThomas Huth         QDict *d = qdict_get_qdict(index, type);
681e8a1faeSThomas Huth         const char *p = d && qdict_haskey(d, "parent") ?
691e8a1faeSThomas Huth                         qdict_get_str(d, "parent") :
701e8a1faeSThomas Huth                         NULL;
711e8a1faeSThomas Huth 
721e8a1faeSThomas Huth         if (!strcmp(type, parent)) {
731e8a1faeSThomas Huth             return true;
741e8a1faeSThomas Huth         }
751e8a1faeSThomas Huth 
761e8a1faeSThomas Huth         type = p;
771e8a1faeSThomas Huth     }
781e8a1faeSThomas Huth 
791e8a1faeSThomas Huth     return false;
801e8a1faeSThomas Huth }
811e8a1faeSThomas Huth 
821e8a1faeSThomas Huth /* Find an entry on a list returned by qom-list-types */
type_list_find(QList * types,const char * name)831e8a1faeSThomas Huth static QDict *type_list_find(QList *types, const char *name)
841e8a1faeSThomas Huth {
851e8a1faeSThomas Huth     QListEntry *e;
861e8a1faeSThomas Huth 
871e8a1faeSThomas Huth     QLIST_FOREACH_ENTRY(types, e) {
881e8a1faeSThomas Huth         QDict *d = qobject_to(QDict, qlist_entry_obj(e));
891e8a1faeSThomas Huth         const char *ename = qdict_get_str(d, "name");
901e8a1faeSThomas Huth         if (!strcmp(ename, name)) {
911e8a1faeSThomas Huth             return d;
921e8a1faeSThomas Huth         }
931e8a1faeSThomas Huth     }
941e8a1faeSThomas Huth 
951e8a1faeSThomas Huth     return NULL;
961e8a1faeSThomas Huth }
971e8a1faeSThomas Huth 
device_type_list(QTestState * qts,bool abstract)981e8a1faeSThomas Huth static QList *device_type_list(QTestState *qts, bool abstract)
991e8a1faeSThomas Huth {
1001e8a1faeSThomas Huth     return qom_list_types(qts, "device", abstract);
1011e8a1faeSThomas Huth }
1021e8a1faeSThomas Huth 
test_one_device(QTestState * qts,const char * type)1031e8a1faeSThomas Huth static void test_one_device(QTestState *qts, const char *type)
1041e8a1faeSThomas Huth {
1051e8a1faeSThomas Huth     QDict *resp;
106e27bd498SPaolo Bonzini     char *help, *escaped;
107e27bd498SPaolo Bonzini     GRegex *comma;
1081e8a1faeSThomas Huth 
1091e8a1faeSThomas Huth     g_test_message("Testing device '%s'", type);
1101e8a1faeSThomas Huth 
1111e8a1faeSThomas Huth     resp = qtest_qmp(qts, "{'execute': 'device-list-properties',"
1121e8a1faeSThomas Huth                           " 'arguments': {'typename': %s}}",
1131e8a1faeSThomas Huth                type);
1141e8a1faeSThomas Huth     qobject_unref(resp);
1151e8a1faeSThomas Huth 
116e27bd498SPaolo Bonzini     comma = g_regex_new(",", 0, 0, NULL);
117e27bd498SPaolo Bonzini     escaped = g_regex_replace_literal(comma, type, -1, 0, ",,", 0, NULL);
118e27bd498SPaolo Bonzini     g_regex_unref(comma);
119e27bd498SPaolo Bonzini 
120e27bd498SPaolo Bonzini     help = qtest_hmp(qts, "device_add \"%s,help\"", escaped);
1211e8a1faeSThomas Huth     g_free(help);
122e27bd498SPaolo Bonzini     g_free(escaped);
1231e8a1faeSThomas Huth }
1241e8a1faeSThomas Huth 
test_device_intro_list(void)1251e8a1faeSThomas Huth static void test_device_intro_list(void)
1261e8a1faeSThomas Huth {
1271e8a1faeSThomas Huth     QList *types;
1281e8a1faeSThomas Huth     char *help;
1291e8a1faeSThomas Huth     QTestState *qts;
1301e8a1faeSThomas Huth 
1311e8a1faeSThomas Huth     qts = qtest_init(common_args);
1321e8a1faeSThomas Huth 
1331e8a1faeSThomas Huth     types = device_type_list(qts, true);
1341e8a1faeSThomas Huth     qobject_unref(types);
1351e8a1faeSThomas Huth 
1361e8a1faeSThomas Huth     help = qtest_hmp(qts, "device_add help");
1371e8a1faeSThomas Huth     g_free(help);
1381e8a1faeSThomas Huth 
1391e8a1faeSThomas Huth     qtest_quit(qts);
1401e8a1faeSThomas Huth }
1411e8a1faeSThomas Huth 
1421e8a1faeSThomas Huth /*
1431e8a1faeSThomas Huth  * Ensure all entries returned by qom-list-types implements=<parent>
1441e8a1faeSThomas Huth  * have <parent> as a parent.
1451e8a1faeSThomas Huth  */
test_qom_list_parents(QTestState * qts,const char * parent)1461e8a1faeSThomas Huth static void test_qom_list_parents(QTestState *qts, const char *parent)
1471e8a1faeSThomas Huth {
1481e8a1faeSThomas Huth     QList *types;
1491e8a1faeSThomas Huth     QListEntry *e;
1501e8a1faeSThomas Huth     QDict *index;
1511e8a1faeSThomas Huth 
1521e8a1faeSThomas Huth     types = qom_list_types(qts, parent, true);
1531e8a1faeSThomas Huth     index = qom_type_index(types);
1541e8a1faeSThomas Huth 
1551e8a1faeSThomas Huth     QLIST_FOREACH_ENTRY(types, e) {
1561e8a1faeSThomas Huth         QDict *d = qobject_to(QDict, qlist_entry_obj(e));
1571e8a1faeSThomas Huth         const char *name = qdict_get_str(d, "name");
1581e8a1faeSThomas Huth 
1591e8a1faeSThomas Huth         g_assert(qom_has_parent(index, name, parent));
1601e8a1faeSThomas Huth     }
1611e8a1faeSThomas Huth 
1621e8a1faeSThomas Huth     qobject_unref(types);
1631e8a1faeSThomas Huth     qobject_unref(index);
1641e8a1faeSThomas Huth }
1651e8a1faeSThomas Huth 
test_qom_list_fields(void)1661e8a1faeSThomas Huth static void test_qom_list_fields(void)
1671e8a1faeSThomas Huth {
1681e8a1faeSThomas Huth     QList *all_types;
1691e8a1faeSThomas Huth     QList *non_abstract;
1701e8a1faeSThomas Huth     QListEntry *e;
1711e8a1faeSThomas Huth     QTestState *qts;
1721e8a1faeSThomas Huth 
1731e8a1faeSThomas Huth     qts = qtest_init(common_args);
1741e8a1faeSThomas Huth 
1751e8a1faeSThomas Huth     all_types = qom_list_types(qts, NULL, true);
1761e8a1faeSThomas Huth     non_abstract = qom_list_types(qts, NULL, false);
1771e8a1faeSThomas Huth 
1781e8a1faeSThomas Huth     QLIST_FOREACH_ENTRY(all_types, e) {
1791e8a1faeSThomas Huth         QDict *d = qobject_to(QDict, qlist_entry_obj(e));
1801e8a1faeSThomas Huth         const char *name = qdict_get_str(d, "name");
1811e8a1faeSThomas Huth         bool abstract = qdict_haskey(d, "abstract") ?
1821e8a1faeSThomas Huth                         qdict_get_bool(d, "abstract") :
1831e8a1faeSThomas Huth                         false;
1841e8a1faeSThomas Huth         bool expected_abstract = !type_list_find(non_abstract, name);
1851e8a1faeSThomas Huth 
1861e8a1faeSThomas Huth         g_assert(abstract == expected_abstract);
1871e8a1faeSThomas Huth     }
1881e8a1faeSThomas Huth 
1891e8a1faeSThomas Huth     test_qom_list_parents(qts, "object");
1901e8a1faeSThomas Huth     test_qom_list_parents(qts, "device");
1911e8a1faeSThomas Huth     test_qom_list_parents(qts, "sys-bus-device");
1921e8a1faeSThomas Huth 
1931e8a1faeSThomas Huth     qobject_unref(all_types);
1941e8a1faeSThomas Huth     qobject_unref(non_abstract);
1951e8a1faeSThomas Huth     qtest_quit(qts);
1961e8a1faeSThomas Huth }
1971e8a1faeSThomas Huth 
test_device_intro_none(void)1981e8a1faeSThomas Huth static void test_device_intro_none(void)
1991e8a1faeSThomas Huth {
2001e8a1faeSThomas Huth     QTestState *qts = qtest_init(common_args);
2013e7b80f8SDaniel P. Berrangé     g_autofree char *qom_tree_start = qtest_hmp(qts, "info qom-tree");
2023e7b80f8SDaniel P. Berrangé     g_autofree char *qom_tree_end = NULL;
2033e7b80f8SDaniel P. Berrangé     g_autofree char *qtree_start = qtest_hmp(qts, "info qtree");
2043e7b80f8SDaniel P. Berrangé     g_autofree char *qtree_end = NULL;
2051e8a1faeSThomas Huth 
2061e8a1faeSThomas Huth     test_one_device(qts, "nonexistent");
2073e7b80f8SDaniel P. Berrangé 
2083e7b80f8SDaniel P. Berrangé     /* Make sure that really nothing changed in the trees */
2093e7b80f8SDaniel P. Berrangé     qom_tree_end = qtest_hmp(qts, "info qom-tree");
2103e7b80f8SDaniel P. Berrangé     g_assert_cmpstr(qom_tree_start, ==, qom_tree_end);
2113e7b80f8SDaniel P. Berrangé     qtree_end = qtest_hmp(qts, "info qtree");
2123e7b80f8SDaniel P. Berrangé     g_assert_cmpstr(qtree_start, ==, qtree_end);
2133e7b80f8SDaniel P. Berrangé 
2141e8a1faeSThomas Huth     qtest_quit(qts);
2151e8a1faeSThomas Huth }
2161e8a1faeSThomas Huth 
test_device_intro_abstract(void)2171e8a1faeSThomas Huth static void test_device_intro_abstract(void)
2181e8a1faeSThomas Huth {
2191e8a1faeSThomas Huth     QTestState *qts = qtest_init(common_args);
2203e7b80f8SDaniel P. Berrangé     g_autofree char *qom_tree_start = qtest_hmp(qts, "info qom-tree");
2213e7b80f8SDaniel P. Berrangé     g_autofree char *qom_tree_end = NULL;
2223e7b80f8SDaniel P. Berrangé     g_autofree char *qtree_start = qtest_hmp(qts, "info qtree");
2233e7b80f8SDaniel P. Berrangé     g_autofree char *qtree_end = NULL;
2241e8a1faeSThomas Huth 
2251e8a1faeSThomas Huth     test_one_device(qts, "device");
2263e7b80f8SDaniel P. Berrangé 
2273e7b80f8SDaniel P. Berrangé     /* Make sure that really nothing changed in the trees */
2283e7b80f8SDaniel P. Berrangé     qom_tree_end = qtest_hmp(qts, "info qom-tree");
2293e7b80f8SDaniel P. Berrangé     g_assert_cmpstr(qom_tree_start, ==, qom_tree_end);
2303e7b80f8SDaniel P. Berrangé     qtree_end = qtest_hmp(qts, "info qtree");
2313e7b80f8SDaniel P. Berrangé     g_assert_cmpstr(qtree_start, ==, qtree_end);
2323e7b80f8SDaniel P. Berrangé 
2331e8a1faeSThomas Huth     qtest_quit(qts);
2341e8a1faeSThomas Huth }
2351e8a1faeSThomas Huth 
test_device_intro_concrete(const void * args)2361e8a1faeSThomas Huth static void test_device_intro_concrete(const void *args)
2371e8a1faeSThomas Huth {
2381e8a1faeSThomas Huth     QList *types;
2391e8a1faeSThomas Huth     QListEntry *entry;
2401e8a1faeSThomas Huth     const char *type;
2413e7b80f8SDaniel P. Berrangé     QTestState *qts = qtest_init(args);
2423e7b80f8SDaniel P. Berrangé     g_autofree char *qom_tree_start = qtest_hmp(qts, "info qom-tree");
2433e7b80f8SDaniel P. Berrangé     g_autofree char *qom_tree_end = NULL;
2443e7b80f8SDaniel P. Berrangé     g_autofree char *qtree_start = qtest_hmp(qts, "info qtree");
2453e7b80f8SDaniel P. Berrangé     g_autofree char *qtree_end = NULL;
2461e8a1faeSThomas Huth 
2471e8a1faeSThomas Huth     types = device_type_list(qts, false);
2481e8a1faeSThomas Huth 
2491e8a1faeSThomas Huth     QLIST_FOREACH_ENTRY(types, entry) {
2501e8a1faeSThomas Huth         type = qdict_get_try_str(qobject_to(QDict, qlist_entry_obj(entry)),
2511e8a1faeSThomas Huth                                  "name");
2521e8a1faeSThomas Huth         g_assert(type);
2531e8a1faeSThomas Huth         test_one_device(qts, type);
2541e8a1faeSThomas Huth     }
2551e8a1faeSThomas Huth 
2563e7b80f8SDaniel P. Berrangé     /*
2573e7b80f8SDaniel P. Berrangé      * Some devices leave dangling pointers in QOM behind.
2583e7b80f8SDaniel P. Berrangé      * "info qom-tree" or "info qtree" have a good chance at crashing then.
2593e7b80f8SDaniel P. Berrangé      * Also make sure that the tree did not change.
2603e7b80f8SDaniel P. Berrangé      */
2613e7b80f8SDaniel P. Berrangé     qom_tree_end = qtest_hmp(qts, "info qom-tree");
2623e7b80f8SDaniel P. Berrangé     g_assert_cmpstr(qom_tree_start, ==, qom_tree_end);
2633e7b80f8SDaniel P. Berrangé 
2643e7b80f8SDaniel P. Berrangé     qtree_end = qtest_hmp(qts, "info qtree");
2653e7b80f8SDaniel P. Berrangé     g_assert_cmpstr(qtree_start, ==, qtree_end);
2663e7b80f8SDaniel P. Berrangé 
2671e8a1faeSThomas Huth     qobject_unref(types);
2681e8a1faeSThomas Huth     qtest_quit(qts);
2691e8a1faeSThomas Huth     g_free((void *)args);
2701e8a1faeSThomas Huth }
2711e8a1faeSThomas Huth 
test_abstract_interfaces(void)2721e8a1faeSThomas Huth static void test_abstract_interfaces(void)
2731e8a1faeSThomas Huth {
2741e8a1faeSThomas Huth     QList *all_types;
2751e8a1faeSThomas Huth     QListEntry *e;
2761e8a1faeSThomas Huth     QDict *index;
2771e8a1faeSThomas Huth     QTestState *qts;
2781e8a1faeSThomas Huth 
2791e8a1faeSThomas Huth     qts = qtest_init(common_args);
2801e8a1faeSThomas Huth 
2811e8a1faeSThomas Huth     all_types = qom_list_types(qts, "interface", true);
2821e8a1faeSThomas Huth     index = qom_type_index(all_types);
2831e8a1faeSThomas Huth 
2841e8a1faeSThomas Huth     QLIST_FOREACH_ENTRY(all_types, e) {
2851e8a1faeSThomas Huth         QDict *d = qobject_to(QDict, qlist_entry_obj(e));
2861e8a1faeSThomas Huth         const char *name = qdict_get_str(d, "name");
2871e8a1faeSThomas Huth 
2881e8a1faeSThomas Huth         /*
2891e8a1faeSThomas Huth          * qom-list-types implements=interface returns all types
2901e8a1faeSThomas Huth          * that implement _any_ interface (not just interface
2911e8a1faeSThomas Huth          * types), so skip the ones that don't have "interface"
2921e8a1faeSThomas Huth          * on the parent type chain.
2931e8a1faeSThomas Huth          */
2941e8a1faeSThomas Huth         if (!qom_has_parent(index, name, "interface")) {
2951e8a1faeSThomas Huth             /* Not an interface type */
2961e8a1faeSThomas Huth             continue;
2971e8a1faeSThomas Huth         }
2981e8a1faeSThomas Huth 
2991e8a1faeSThomas Huth         g_assert(qdict_haskey(d, "abstract") && qdict_get_bool(d, "abstract"));
3001e8a1faeSThomas Huth     }
3011e8a1faeSThomas Huth 
3021e8a1faeSThomas Huth     qobject_unref(all_types);
3031e8a1faeSThomas Huth     qobject_unref(index);
3041e8a1faeSThomas Huth     qtest_quit(qts);
3051e8a1faeSThomas Huth }
3061e8a1faeSThomas Huth 
add_machine_test_case(const char * mname)3071e8a1faeSThomas Huth static void add_machine_test_case(const char *mname)
3081e8a1faeSThomas Huth {
3091e8a1faeSThomas Huth     char *path, *args;
3101e8a1faeSThomas Huth 
3111e8a1faeSThomas Huth     path = g_strdup_printf("device/introspect/concrete/defaults/%s", mname);
3121e8a1faeSThomas Huth     args = g_strdup_printf("-M %s", mname);
3131e8a1faeSThomas Huth     qtest_add_data_func(path, args, test_device_intro_concrete);
3141e8a1faeSThomas Huth     g_free(path);
3151e8a1faeSThomas Huth 
3161e8a1faeSThomas Huth     path = g_strdup_printf("device/introspect/concrete/nodefaults/%s", mname);
3171e8a1faeSThomas Huth     args = g_strdup_printf("-nodefaults -M %s", mname);
3181e8a1faeSThomas Huth     qtest_add_data_func(path, args, test_device_intro_concrete);
3191e8a1faeSThomas Huth     g_free(path);
3201e8a1faeSThomas Huth }
3211e8a1faeSThomas Huth 
main(int argc,char ** argv)3221e8a1faeSThomas Huth int main(int argc, char **argv)
3231e8a1faeSThomas Huth {
3241e8a1faeSThomas Huth     g_test_init(&argc, &argv, NULL);
3251e8a1faeSThomas Huth 
3261e8a1faeSThomas Huth     qtest_add_func("device/introspect/list", test_device_intro_list);
3271e8a1faeSThomas Huth     qtest_add_func("device/introspect/list-fields", test_qom_list_fields);
3281e8a1faeSThomas Huth     qtest_add_func("device/introspect/none", test_device_intro_none);
3291e8a1faeSThomas Huth     qtest_add_func("device/introspect/abstract", test_device_intro_abstract);
3301e8a1faeSThomas Huth     qtest_add_func("device/introspect/abstract-interfaces", test_abstract_interfaces);
3311e8a1faeSThomas Huth     if (g_test_quick()) {
3321e8a1faeSThomas Huth         qtest_add_data_func("device/introspect/concrete/defaults/none",
3331e8a1faeSThomas Huth                             g_strdup(common_args), test_device_intro_concrete);
3341e8a1faeSThomas Huth     } else {
3351e8a1faeSThomas Huth         qtest_cb_for_every_machine(add_machine_test_case, true);
3361e8a1faeSThomas Huth     }
3371e8a1faeSThomas Huth 
3381e8a1faeSThomas Huth     return g_test_run();
3391e8a1faeSThomas Huth }
340