| 1 | /* |
| 2 | * Device introspection test cases |
| 3 | * |
| 4 | * Copyright (c) 2015 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Markus Armbruster <armbru@redhat.com>, |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 10 | * See the COPYING file in the top-level directory. |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * Covers QMP device-list-properties and HMP device_add help (if |
| 15 | * CONFIG_HMP). We currently don't check that the output makes sense, |
| 16 | * only that QEMU survives. Useful since we've had an astounding |
| 17 | * number of crash bugs around here. |
| 18 | */ |
| 19 | |
| 20 | #include "qemu/osdep.h" |
| 21 | #include "qobject/qstring.h" |
| 22 | #include "qobject/qdict.h" |
| 23 | #include "qobject/qlist.h" |
| 24 | #include "libqtest.h" |
| 25 | |
| 26 | const char common_args[] = "-nodefaults -machine none"; |
| 27 | |
| 28 | static QList *qom_list_types(QTestState * qts, const char *implements, |
| 29 | bool abstract) |
| 30 | { |
| 31 | QDict *resp; |
| 32 | QList *ret; |
| 33 | QDict *args = qdict_new(); |
| 34 | |
| 35 | qdict_put_bool(args, "abstract", abstract); |
| 36 | if (implements) { |
| 37 | qdict_put_str(args, "implements", implements); |
| 38 | } |
| 39 | resp = qtest_qmp(qts, "{'execute': 'qom-list-types', 'arguments': %p }", |
| 40 | args); |
| 41 | g_assert(qdict_haskey(resp, "return")); |
| 42 | ret = qdict_get_qlist(resp, "return"); |
| 43 | qobject_ref(ret); |
| 44 | qobject_unref(resp); |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | /* Build a name -> ObjectTypeInfo index from a ObjectTypeInfo list */ |
| 49 | static QDict *qom_type_index(QList *types) |
| 50 | { |
| 51 | QDict *index = qdict_new(); |
| 52 | QListEntry *e; |
| 53 | |
| 54 | QLIST_FOREACH_ENTRY(types, e) { |
| 55 | QDict *d = qobject_to(QDict, qlist_entry_obj(e)); |
| 56 | const char *name = qdict_get_str(d, "name"); |
| 57 | qobject_ref(d); |
| 58 | qdict_put(index, name, d); |
| 59 | } |
| 60 | return index; |
| 61 | } |
| 62 | |
| 63 | /* Check if @parent is present in the parent chain of @type */ |
| 64 | static bool qom_has_parent(QDict *index, const char *type, const char *parent) |
| 65 | { |
| 66 | while (type) { |
| 67 | QDict *d = qdict_get_qdict(index, type); |
| 68 | const char *p = d && qdict_haskey(d, "parent") ? |
| 69 | qdict_get_str(d, "parent") : |
| 70 | NULL; |
| 71 | |
| 72 | if (!strcmp(type, parent)) { |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | type = p; |
| 77 | } |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | /* Find an entry on a list returned by qom-list-types */ |
| 83 | static QDict *type_list_find(QList *types, const char *name) |
| 84 | { |
| 85 | QListEntry *e; |
| 86 | |
| 87 | QLIST_FOREACH_ENTRY(types, e) { |
| 88 | QDict *d = qobject_to(QDict, qlist_entry_obj(e)); |
| 89 | const char *ename = qdict_get_str(d, "name"); |
| 90 | if (!strcmp(ename, name)) { |
| 91 | return d; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | static QList *device_type_list(QTestState *qts, bool abstract) |
| 99 | { |
| 100 | return qom_list_types(qts, "device", abstract); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Recursively walk the QOM composition tree via qom-list and build a |
| 105 | * string representation. This serves two purposes: detecting dangling |
| 106 | * pointers (qom-list would crash QEMU) and detecting leaked objects |
| 107 | * (by comparing the output before and after device introspection). |
| 108 | */ |
| 109 | static void qom_tree_walk(QTestState *qts, const char *path, GString *result) |
| 110 | { |
| 111 | QDict *resp; |
| 112 | QList *list; |
| 113 | QListEntry *e; |
| 114 | GList *children = NULL; |
| 115 | |
| 116 | resp = qtest_qmp(qts, "{'execute': 'qom-list'," |
| 117 | " 'arguments': {'path': %s}}", path); |
| 118 | g_assert(qdict_haskey(resp, "return")); |
| 119 | list = qdict_get_qlist(resp, "return"); |
| 120 | |
| 121 | QLIST_FOREACH_ENTRY(list, e) { |
| 122 | QDict *prop = qobject_to(QDict, qlist_entry_obj(e)); |
| 123 | const char *type = qdict_get_str(prop, "type"); |
| 124 | if (g_str_has_prefix(type, "child<")) { |
| 125 | const char *name = qdict_get_str(prop, "name"); |
| 126 | children = g_list_prepend(children, g_strdup(name)); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | children = g_list_sort_with_data(children, (GCompareDataFunc)g_strcmp0, |
| 131 | NULL); |
| 132 | |
| 133 | for (GList *l = children; l; l = l->next) { |
| 134 | const char *name = l->data; |
| 135 | g_autofree char *child_path = (!strcmp(path, "/")) |
| 136 | ? g_strdup_printf("/%s", name) |
| 137 | : g_strdup_printf("%s/%s", path, name); |
| 138 | |
| 139 | g_string_append_printf(result, "%s\n", child_path); |
| 140 | qom_tree_walk(qts, child_path, result); |
| 141 | } |
| 142 | |
| 143 | g_list_free_full(children, g_free); |
| 144 | qobject_unref(resp); |
| 145 | } |
| 146 | |
| 147 | static char *qom_tree_str(QTestState *qts) |
| 148 | { |
| 149 | GString *result = g_string_new(""); |
| 150 | qom_tree_walk(qts, "/", result); |
| 151 | return g_string_free(result, FALSE); |
| 152 | } |
| 153 | |
| 154 | static void test_one_device(QTestState *qts, const char *type) |
| 155 | { |
| 156 | QDict *resp; |
| 157 | |
| 158 | g_test_message("Testing device '%s'", type); |
| 159 | |
| 160 | resp = qtest_qmp(qts, "{'execute': 'device-list-properties'," |
| 161 | " 'arguments': {'typename': %s}}", |
| 162 | type); |
| 163 | qobject_unref(resp); |
| 164 | |
| 165 | #ifdef CONFIG_HMP |
| 166 | { |
| 167 | g_autofree char *escaped = NULL; |
| 168 | g_autoptr(GRegex) comma = NULL; |
| 169 | |
| 170 | comma = g_regex_new(",", 0, 0, NULL); |
| 171 | escaped = g_regex_replace_literal(comma, type, -1, 0, ",,", 0, NULL); |
| 172 | g_free(qtest_hmp(qts, "device_add \"%s,help\"", escaped)); |
| 173 | } |
| 174 | #endif |
| 175 | } |
| 176 | |
| 177 | static void test_device_intro_list(void) |
| 178 | { |
| 179 | QList *types; |
| 180 | QTestState *qts; |
| 181 | |
| 182 | qts = qtest_init(common_args); |
| 183 | |
| 184 | types = device_type_list(qts, true); |
| 185 | qobject_unref(types); |
| 186 | |
| 187 | #ifdef CONFIG_HMP |
| 188 | { |
| 189 | g_free(qtest_hmp(qts, "device_add help")); |
| 190 | } |
| 191 | #endif |
| 192 | |
| 193 | qtest_quit(qts); |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Ensure all entries returned by qom-list-types implements=<parent> |
| 198 | * have <parent> as a parent. |
| 199 | */ |
| 200 | static void test_qom_list_parents(QTestState *qts, const char *parent) |
| 201 | { |
| 202 | QList *types; |
| 203 | QListEntry *e; |
| 204 | QDict *index; |
| 205 | |
| 206 | types = qom_list_types(qts, parent, true); |
| 207 | index = qom_type_index(types); |
| 208 | |
| 209 | QLIST_FOREACH_ENTRY(types, e) { |
| 210 | QDict *d = qobject_to(QDict, qlist_entry_obj(e)); |
| 211 | const char *name = qdict_get_str(d, "name"); |
| 212 | |
| 213 | g_assert(qom_has_parent(index, name, parent)); |
| 214 | } |
| 215 | |
| 216 | qobject_unref(types); |
| 217 | qobject_unref(index); |
| 218 | } |
| 219 | |
| 220 | static void test_qom_list_fields(void) |
| 221 | { |
| 222 | QList *all_types; |
| 223 | QList *non_abstract; |
| 224 | QListEntry *e; |
| 225 | QTestState *qts; |
| 226 | |
| 227 | qts = qtest_init(common_args); |
| 228 | |
| 229 | all_types = qom_list_types(qts, NULL, true); |
| 230 | non_abstract = qom_list_types(qts, NULL, false); |
| 231 | |
| 232 | QLIST_FOREACH_ENTRY(all_types, e) { |
| 233 | QDict *d = qobject_to(QDict, qlist_entry_obj(e)); |
| 234 | const char *name = qdict_get_str(d, "name"); |
| 235 | bool abstract = qdict_haskey(d, "abstract") ? |
| 236 | qdict_get_bool(d, "abstract") : |
| 237 | false; |
| 238 | bool expected_abstract = !type_list_find(non_abstract, name); |
| 239 | |
| 240 | g_assert(abstract == expected_abstract); |
| 241 | } |
| 242 | |
| 243 | test_qom_list_parents(qts, "object"); |
| 244 | test_qom_list_parents(qts, "device"); |
| 245 | test_qom_list_parents(qts, "sys-bus-device"); |
| 246 | |
| 247 | qobject_unref(all_types); |
| 248 | qobject_unref(non_abstract); |
| 249 | qtest_quit(qts); |
| 250 | } |
| 251 | |
| 252 | static void test_device_intro_none(void) |
| 253 | { |
| 254 | QTestState *qts = qtest_init(common_args); |
| 255 | g_autofree char *qom_tree_start = qom_tree_str(qts); |
| 256 | g_autofree char *qom_tree_end = NULL; |
| 257 | #ifdef CONFIG_HMP |
| 258 | g_autofree char *qtree_start = qtest_hmp(qts, "info qtree"); |
| 259 | g_autofree char *qtree_end = NULL; |
| 260 | #endif |
| 261 | |
| 262 | test_one_device(qts, "nonexistent"); |
| 263 | |
| 264 | /* Make sure that really nothing changed in the trees */ |
| 265 | qom_tree_end = qom_tree_str(qts); |
| 266 | g_assert_cmpstr(qom_tree_start, ==, qom_tree_end); |
| 267 | #ifdef CONFIG_HMP |
| 268 | qtree_end = qtest_hmp(qts, "info qtree"); |
| 269 | g_assert_cmpstr(qtree_start, ==, qtree_end); |
| 270 | #endif |
| 271 | |
| 272 | qtest_quit(qts); |
| 273 | } |
| 274 | |
| 275 | static void test_device_intro_abstract(void) |
| 276 | { |
| 277 | QTestState *qts = qtest_init(common_args); |
| 278 | g_autofree char *qom_tree_start = qom_tree_str(qts); |
| 279 | g_autofree char *qom_tree_end = NULL; |
| 280 | #ifdef CONFIG_HMP |
| 281 | g_autofree char *qtree_start = qtest_hmp(qts, "info qtree"); |
| 282 | g_autofree char *qtree_end = NULL; |
| 283 | #endif |
| 284 | |
| 285 | test_one_device(qts, "device"); |
| 286 | |
| 287 | /* Make sure that really nothing changed in the trees */ |
| 288 | qom_tree_end = qom_tree_str(qts); |
| 289 | g_assert_cmpstr(qom_tree_start, ==, qom_tree_end); |
| 290 | #ifdef CONFIG_HMP |
| 291 | qtree_end = qtest_hmp(qts, "info qtree"); |
| 292 | g_assert_cmpstr(qtree_start, ==, qtree_end); |
| 293 | #endif |
| 294 | |
| 295 | qtest_quit(qts); |
| 296 | } |
| 297 | |
| 298 | static void test_device_intro_concrete(const void *args) |
| 299 | { |
| 300 | QList *types; |
| 301 | QListEntry *entry; |
| 302 | const char *type; |
| 303 | QTestState *qts = qtest_init(args); |
| 304 | g_autofree char *qom_tree_start = qom_tree_str(qts); |
| 305 | g_autofree char *qom_tree_end = NULL; |
| 306 | #ifdef CONFIG_HMP |
| 307 | g_autofree char *qtree_start = qtest_hmp(qts, "info qtree"); |
| 308 | g_autofree char *qtree_end = NULL; |
| 309 | #endif |
| 310 | |
| 311 | types = device_type_list(qts, false); |
| 312 | |
| 313 | QLIST_FOREACH_ENTRY(types, entry) { |
| 314 | type = qdict_get_try_str(qobject_to(QDict, qlist_entry_obj(entry)), |
| 315 | "name"); |
| 316 | g_assert(type); |
| 317 | test_one_device(qts, type); |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Some devices leave dangling pointers in QOM behind. |
| 322 | * Walking the QOM tree via qom-list has a good chance at crashing then. |
| 323 | * Also make sure that the tree did not change. |
| 324 | */ |
| 325 | qom_tree_end = qom_tree_str(qts); |
| 326 | g_assert_cmpstr(qom_tree_start, ==, qom_tree_end); |
| 327 | #ifdef CONFIG_HMP |
| 328 | qtree_end = qtest_hmp(qts, "info qtree"); |
| 329 | g_assert_cmpstr(qtree_start, ==, qtree_end); |
| 330 | #endif |
| 331 | |
| 332 | qobject_unref(types); |
| 333 | qtest_quit(qts); |
| 334 | } |
| 335 | |
| 336 | static void test_abstract_interfaces(void) |
| 337 | { |
| 338 | QList *all_types; |
| 339 | QListEntry *e; |
| 340 | QDict *index; |
| 341 | QTestState *qts; |
| 342 | |
| 343 | qts = qtest_init(common_args); |
| 344 | |
| 345 | all_types = qom_list_types(qts, "interface", true); |
| 346 | index = qom_type_index(all_types); |
| 347 | |
| 348 | QLIST_FOREACH_ENTRY(all_types, e) { |
| 349 | QDict *d = qobject_to(QDict, qlist_entry_obj(e)); |
| 350 | const char *name = qdict_get_str(d, "name"); |
| 351 | |
| 352 | /* |
| 353 | * qom-list-types implements=interface returns all types |
| 354 | * that implement _any_ interface (not just interface |
| 355 | * types), so skip the ones that don't have "interface" |
| 356 | * on the parent type chain. |
| 357 | */ |
| 358 | if (!qom_has_parent(index, name, "interface")) { |
| 359 | /* Not an interface type */ |
| 360 | continue; |
| 361 | } |
| 362 | |
| 363 | g_assert(qdict_haskey(d, "abstract") && qdict_get_bool(d, "abstract")); |
| 364 | } |
| 365 | |
| 366 | qobject_unref(all_types); |
| 367 | qobject_unref(index); |
| 368 | qtest_quit(qts); |
| 369 | } |
| 370 | |
| 371 | static void add_machine_test_case(const char *mname) |
| 372 | { |
| 373 | char *path, *args; |
| 374 | |
| 375 | path = g_strdup_printf("device/introspect/concrete/defaults/%s", mname); |
| 376 | args = g_strdup_printf("-M %s", mname); |
| 377 | qtest_add_data_func_full(path, args, test_device_intro_concrete, g_free); |
| 378 | g_free(path); |
| 379 | |
| 380 | path = g_strdup_printf("device/introspect/concrete/nodefaults/%s", mname); |
| 381 | args = g_strdup_printf("-nodefaults -M %s", mname); |
| 382 | qtest_add_data_func_full(path, args, test_device_intro_concrete, g_free); |
| 383 | g_free(path); |
| 384 | } |
| 385 | |
| 386 | int main(int argc, char **argv) |
| 387 | { |
| 388 | g_test_init(&argc, &argv, NULL); |
| 389 | |
| 390 | qtest_add_func("device/introspect/list", test_device_intro_list); |
| 391 | qtest_add_func("device/introspect/list-fields", test_qom_list_fields); |
| 392 | qtest_add_func("device/introspect/none", test_device_intro_none); |
| 393 | qtest_add_func("device/introspect/abstract", test_device_intro_abstract); |
| 394 | qtest_add_func("device/introspect/abstract-interfaces", test_abstract_interfaces); |
| 395 | if (g_test_quick()) { |
| 396 | qtest_add_data_func("device/introspect/concrete/defaults/none", |
| 397 | common_args, test_device_intro_concrete); |
| 398 | } else { |
| 399 | qtest_cb_for_every_machine(add_machine_test_case, true); |
| 400 | } |
| 401 | |
| 402 | return g_test_run(); |
| 403 | } |