device-introspect-test.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. * Covers QMP device-list-properties and HMP device_add help. We
  14. * currently don't check that their output makes sense, only that QEMU
  15. * survives. Useful since we've had an astounding number of crash
  16. * bugs around here.
  17. */
  18. #include "qemu/osdep.h"
  19. #include "qemu-common.h"
  20. #include "qapi/qmp/qstring.h"
  21. #include "qapi/qmp/qdict.h"
  22. #include "qapi/qmp/qlist.h"
  23. #include "libqtest.h"
  24. const char common_args[] = "-nodefaults -machine none";
  25. static QList *qom_list_types(QTestState * qts, const char *implements,
  26. bool abstract)
  27. {
  28. QDict *resp;
  29. QList *ret;
  30. QDict *args = qdict_new();
  31. qdict_put_bool(args, "abstract", abstract);
  32. if (implements) {
  33. qdict_put_str(args, "implements", implements);
  34. }
  35. resp = qtest_qmp(qts, "{'execute': 'qom-list-types', 'arguments': %p }",
  36. args);
  37. g_assert(qdict_haskey(resp, "return"));
  38. ret = qdict_get_qlist(resp, "return");
  39. qobject_ref(ret);
  40. qobject_unref(resp);
  41. return ret;
  42. }
  43. /* Build a name -> ObjectTypeInfo index from a ObjectTypeInfo list */
  44. static QDict *qom_type_index(QList *types)
  45. {
  46. QDict *index = qdict_new();
  47. QListEntry *e;
  48. QLIST_FOREACH_ENTRY(types, e) {
  49. QDict *d = qobject_to(QDict, qlist_entry_obj(e));
  50. const char *name = qdict_get_str(d, "name");
  51. qobject_ref(d);
  52. qdict_put(index, name, d);
  53. }
  54. return index;
  55. }
  56. /* Check if @parent is present in the parent chain of @type */
  57. static bool qom_has_parent(QDict *index, const char *type, const char *parent)
  58. {
  59. while (type) {
  60. QDict *d = qdict_get_qdict(index, type);
  61. const char *p = d && qdict_haskey(d, "parent") ?
  62. qdict_get_str(d, "parent") :
  63. NULL;
  64. if (!strcmp(type, parent)) {
  65. return true;
  66. }
  67. type = p;
  68. }
  69. return false;
  70. }
  71. /* Find an entry on a list returned by qom-list-types */
  72. static QDict *type_list_find(QList *types, const char *name)
  73. {
  74. QListEntry *e;
  75. QLIST_FOREACH_ENTRY(types, e) {
  76. QDict *d = qobject_to(QDict, qlist_entry_obj(e));
  77. const char *ename = qdict_get_str(d, "name");
  78. if (!strcmp(ename, name)) {
  79. return d;
  80. }
  81. }
  82. return NULL;
  83. }
  84. static QList *device_type_list(QTestState *qts, bool abstract)
  85. {
  86. return qom_list_types(qts, "device", abstract);
  87. }
  88. static void test_one_device(QTestState *qts, const char *type)
  89. {
  90. QDict *resp;
  91. char *help;
  92. char *qom_tree_start, *qom_tree_end;
  93. char *qtree_start, *qtree_end;
  94. g_test_message("Testing device '%s'", type);
  95. qom_tree_start = qtest_hmp(qts, "info qom-tree");
  96. qtree_start = qtest_hmp(qts, "info qtree");
  97. resp = qtest_qmp(qts, "{'execute': 'device-list-properties',"
  98. " 'arguments': {'typename': %s}}",
  99. type);
  100. qobject_unref(resp);
  101. help = qtest_hmp(qts, "device_add \"%s,help\"", type);
  102. g_free(help);
  103. /*
  104. * Some devices leave dangling pointers in QOM behind.
  105. * "info qom-tree" or "info qtree" have a good chance at crashing then.
  106. * Also make sure that the tree did not change.
  107. */
  108. qom_tree_end = qtest_hmp(qts, "info qom-tree");
  109. g_assert_cmpstr(qom_tree_start, ==, qom_tree_end);
  110. g_free(qom_tree_start);
  111. g_free(qom_tree_end);
  112. qtree_end = qtest_hmp(qts, "info qtree");
  113. g_assert_cmpstr(qtree_start, ==, qtree_end);
  114. g_free(qtree_start);
  115. g_free(qtree_end);
  116. }
  117. static void test_device_intro_list(void)
  118. {
  119. QList *types;
  120. char *help;
  121. QTestState *qts;
  122. qts = qtest_init(common_args);
  123. types = device_type_list(qts, true);
  124. qobject_unref(types);
  125. help = qtest_hmp(qts, "device_add help");
  126. g_free(help);
  127. qtest_quit(qts);
  128. }
  129. /*
  130. * Ensure all entries returned by qom-list-types implements=<parent>
  131. * have <parent> as a parent.
  132. */
  133. static void test_qom_list_parents(QTestState *qts, const char *parent)
  134. {
  135. QList *types;
  136. QListEntry *e;
  137. QDict *index;
  138. types = qom_list_types(qts, parent, true);
  139. index = qom_type_index(types);
  140. QLIST_FOREACH_ENTRY(types, e) {
  141. QDict *d = qobject_to(QDict, qlist_entry_obj(e));
  142. const char *name = qdict_get_str(d, "name");
  143. g_assert(qom_has_parent(index, name, parent));
  144. }
  145. qobject_unref(types);
  146. qobject_unref(index);
  147. }
  148. static void test_qom_list_fields(void)
  149. {
  150. QList *all_types;
  151. QList *non_abstract;
  152. QListEntry *e;
  153. QTestState *qts;
  154. qts = qtest_init(common_args);
  155. all_types = qom_list_types(qts, NULL, true);
  156. non_abstract = qom_list_types(qts, NULL, false);
  157. QLIST_FOREACH_ENTRY(all_types, e) {
  158. QDict *d = qobject_to(QDict, qlist_entry_obj(e));
  159. const char *name = qdict_get_str(d, "name");
  160. bool abstract = qdict_haskey(d, "abstract") ?
  161. qdict_get_bool(d, "abstract") :
  162. false;
  163. bool expected_abstract = !type_list_find(non_abstract, name);
  164. g_assert(abstract == expected_abstract);
  165. }
  166. test_qom_list_parents(qts, "object");
  167. test_qom_list_parents(qts, "device");
  168. test_qom_list_parents(qts, "sys-bus-device");
  169. qobject_unref(all_types);
  170. qobject_unref(non_abstract);
  171. qtest_quit(qts);
  172. }
  173. static void test_device_intro_none(void)
  174. {
  175. QTestState *qts = qtest_init(common_args);
  176. test_one_device(qts, "nonexistent");
  177. qtest_quit(qts);
  178. }
  179. static void test_device_intro_abstract(void)
  180. {
  181. QTestState *qts = qtest_init(common_args);
  182. test_one_device(qts, "device");
  183. qtest_quit(qts);
  184. }
  185. static void test_device_intro_concrete(const void *args)
  186. {
  187. QList *types;
  188. QListEntry *entry;
  189. const char *type;
  190. QTestState *qts;
  191. qts = qtest_init(args);
  192. types = device_type_list(qts, false);
  193. QLIST_FOREACH_ENTRY(types, entry) {
  194. type = qdict_get_try_str(qobject_to(QDict, qlist_entry_obj(entry)),
  195. "name");
  196. g_assert(type);
  197. test_one_device(qts, type);
  198. }
  199. qobject_unref(types);
  200. qtest_quit(qts);
  201. g_free((void *)args);
  202. }
  203. static void test_abstract_interfaces(void)
  204. {
  205. QList *all_types;
  206. QListEntry *e;
  207. QDict *index;
  208. QTestState *qts;
  209. qts = qtest_init(common_args);
  210. all_types = qom_list_types(qts, "interface", true);
  211. index = qom_type_index(all_types);
  212. QLIST_FOREACH_ENTRY(all_types, e) {
  213. QDict *d = qobject_to(QDict, qlist_entry_obj(e));
  214. const char *name = qdict_get_str(d, "name");
  215. /*
  216. * qom-list-types implements=interface returns all types
  217. * that implement _any_ interface (not just interface
  218. * types), so skip the ones that don't have "interface"
  219. * on the parent type chain.
  220. */
  221. if (!qom_has_parent(index, name, "interface")) {
  222. /* Not an interface type */
  223. continue;
  224. }
  225. g_assert(qdict_haskey(d, "abstract") && qdict_get_bool(d, "abstract"));
  226. }
  227. qobject_unref(all_types);
  228. qobject_unref(index);
  229. qtest_quit(qts);
  230. }
  231. static void add_machine_test_case(const char *mname)
  232. {
  233. char *path, *args;
  234. /* Ignore blacklisted machines */
  235. if (g_str_equal("xenfv", mname) || g_str_equal("xenpv", mname)) {
  236. return;
  237. }
  238. path = g_strdup_printf("device/introspect/concrete/defaults/%s", mname);
  239. args = g_strdup_printf("-M %s", mname);
  240. qtest_add_data_func(path, args, test_device_intro_concrete);
  241. g_free(path);
  242. path = g_strdup_printf("device/introspect/concrete/nodefaults/%s", mname);
  243. args = g_strdup_printf("-nodefaults -M %s", mname);
  244. qtest_add_data_func(path, args, test_device_intro_concrete);
  245. g_free(path);
  246. }
  247. int main(int argc, char **argv)
  248. {
  249. g_test_init(&argc, &argv, NULL);
  250. qtest_add_func("device/introspect/list", test_device_intro_list);
  251. qtest_add_func("device/introspect/list-fields", test_qom_list_fields);
  252. qtest_add_func("device/introspect/none", test_device_intro_none);
  253. qtest_add_func("device/introspect/abstract", test_device_intro_abstract);
  254. qtest_add_func("device/introspect/abstract-interfaces", test_abstract_interfaces);
  255. if (g_test_quick()) {
  256. qtest_add_data_func("device/introspect/concrete/defaults/none",
  257. g_strdup(common_args), test_device_intro_concrete);
  258. } else {
  259. qtest_cb_for_every_machine(add_machine_test_case, true);
  260. }
  261. return g_test_run();
  262. }