test-hmp.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Test HMP commands.
  3. *
  4. * Copyright (c) 2017 Red Hat Inc.
  5. *
  6. * Author:
  7. * Thomas Huth <thuth@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2
  10. * or later. See the COPYING file in the top-level directory.
  11. *
  12. * This test calls some HMP commands for all machines that the current
  13. * QEMU binary provides, to check whether they terminate successfully
  14. * (i.e. do not crash QEMU).
  15. */
  16. #include "qemu/osdep.h"
  17. #include "libqtest.h"
  18. static int verbose;
  19. static const char *hmp_cmds[] = {
  20. "announce_self",
  21. "boot_set ndc",
  22. "chardev-add null,id=testchardev1",
  23. "chardev-send-break testchardev1",
  24. "chardev-change testchardev1 ringbuf",
  25. "chardev-remove testchardev1",
  26. "commit all",
  27. "cpu-add 1",
  28. "cpu 0",
  29. "device_add ?",
  30. "device_add usb-mouse,id=mouse1",
  31. "drive_add ignored format=help",
  32. "mouse_button 7",
  33. "mouse_move 10 10",
  34. "mouse_button 0",
  35. "device_del mouse1",
  36. "dump-guest-memory /dev/null 0 4096",
  37. "dump-guest-memory /dev/null",
  38. "gdbserver",
  39. "gva2gpa 0",
  40. "hostfwd_add tcp::43210-:43210",
  41. "hostfwd_remove tcp::43210-:43210",
  42. "i /w 0",
  43. "log all",
  44. "log none",
  45. "memsave 0 4096 \"/dev/null\"",
  46. "migrate_set_cache_size 1",
  47. "migrate_set_downtime 1",
  48. "migrate_set_speed 1",
  49. "netdev_add user,id=net1",
  50. "set_link net1 off",
  51. "set_link net1 on",
  52. "netdev_del net1",
  53. "nmi",
  54. "o /w 0 0x1234",
  55. "object_add memory-backend-ram,id=mem1,size=256M",
  56. "object_del mem1",
  57. "pmemsave 0 4096 \"/dev/null\"",
  58. "p $pc + 8",
  59. "qom-list /",
  60. "qom-set /machine initrd test",
  61. "screendump /dev/null",
  62. "sendkey x",
  63. "singlestep on",
  64. "wavcapture /dev/null",
  65. "stopcapture 0",
  66. "sum 0 512",
  67. "x /8i 0x100",
  68. "xp /16x 0",
  69. NULL
  70. };
  71. /* Run through the list of pre-defined commands */
  72. static void test_commands(QTestState *qts)
  73. {
  74. char *response;
  75. int i;
  76. for (i = 0; hmp_cmds[i] != NULL; i++) {
  77. response = qtest_hmp(qts, "%s", hmp_cmds[i]);
  78. if (verbose) {
  79. fprintf(stderr,
  80. "\texecute HMP command: %s\n"
  81. "\tresult : %s\n",
  82. hmp_cmds[i], response);
  83. }
  84. g_free(response);
  85. }
  86. }
  87. /* Run through all info commands and call them blindly (without arguments) */
  88. static void test_info_commands(QTestState *qts)
  89. {
  90. char *resp, *info, *info_buf, *endp;
  91. info_buf = info = qtest_hmp(qts, "help info");
  92. while (*info) {
  93. /* Extract the info command, ignore parameters and description */
  94. g_assert(strncmp(info, "info ", 5) == 0);
  95. endp = strchr(&info[5], ' ');
  96. g_assert(endp != NULL);
  97. *endp = '\0';
  98. /* Now run the info command */
  99. if (verbose) {
  100. fprintf(stderr, "\t%s\n", info);
  101. }
  102. resp = qtest_hmp(qts, "%s", info);
  103. g_free(resp);
  104. /* And move forward to the next line */
  105. info = strchr(endp + 1, '\n');
  106. if (!info) {
  107. break;
  108. }
  109. info += 1;
  110. }
  111. g_free(info_buf);
  112. }
  113. static void test_machine(gconstpointer data)
  114. {
  115. const char *machine = data;
  116. char *args;
  117. QTestState *qts;
  118. args = g_strdup_printf("-S -M %s", machine);
  119. qts = qtest_init(args);
  120. test_info_commands(qts);
  121. test_commands(qts);
  122. qtest_quit(qts);
  123. g_free(args);
  124. g_free((void *)data);
  125. }
  126. static void add_machine_test_case(const char *mname)
  127. {
  128. char *path;
  129. /* Ignore blacklisted machines that have known problems */
  130. if (!strcmp("xenfv", mname) || !strcmp("xenpv", mname)) {
  131. return;
  132. }
  133. path = g_strdup_printf("hmp/%s", mname);
  134. qtest_add_data_func(path, g_strdup(mname), test_machine);
  135. g_free(path);
  136. }
  137. int main(int argc, char **argv)
  138. {
  139. char *v_env = getenv("V");
  140. if (v_env && *v_env >= '2') {
  141. verbose = true;
  142. }
  143. g_test_init(&argc, &argv, NULL);
  144. qtest_cb_for_every_machine(add_machine_test_case, g_test_quick());
  145. /* as none machine has no memory by default, add a test case with memory */
  146. qtest_add_data_func("hmp/none+2MB", g_strdup("none -m 2"), test_machine);
  147. return g_test_run();
  148. }