4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
9 * Anthony Liguori <aliguori@us.ibm.com>
10 * Paolo Bonzini <pbonzini@redhat.com>
11 * Andreas Färber <afaerber@suse.de>
13 * This work is licensed under the terms of the GNU GPL, version 2 or later.
14 * See the COPYING file in the top-level directory.
17 #include "qemu/osdep.h"
19 #include <sys/socket.h>
24 #include "qemu/cutils.h"
25 #include "qapi/error.h"
26 #include "qapi/qmp/json-parser.h"
27 #include "qapi/qmp/json-streamer.h"
28 #include "qapi/qmp/qdict.h"
29 #include "qapi/qmp/qjson.h"
30 #include "qapi/qmp/qlist.h"
31 #include "qapi/qmp/qstring.h"
34 #define SOCKET_TIMEOUT 50
36 QTestState
*global_qtest
;
42 bool irq_level
[MAX_IRQ
];
44 pid_t qemu_pid
; /* our child QEMU process */
48 static GHookList abrt_hooks
;
49 static struct sigaction sigact_old
;
51 #define g_assert_no_errno(ret) do { \
52 g_assert_cmpint(ret, !=, -1); \
55 static int qtest_query_target_endianness(QTestState
*s
);
57 static int init_socket(const char *socket_path
)
59 struct sockaddr_un addr
;
63 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
64 g_assert_no_errno(sock
);
66 addr
.sun_family
= AF_UNIX
;
67 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socket_path
);
68 qemu_set_cloexec(sock
);
71 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
72 } while (ret
== -1 && errno
== EINTR
);
73 g_assert_no_errno(ret
);
74 ret
= listen(sock
, 1);
75 g_assert_no_errno(ret
);
80 static int socket_accept(int sock
)
82 struct sockaddr_un addr
;
85 struct timeval timeout
= { .tv_sec
= SOCKET_TIMEOUT
,
88 setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, (void *)&timeout
,
92 addrlen
= sizeof(addr
);
93 ret
= accept(sock
, (struct sockaddr
*)&addr
, &addrlen
);
94 } while (ret
== -1 && errno
== EINTR
);
96 fprintf(stderr
, "%s failed: %s\n", __func__
, strerror(errno
));
103 static void kill_qemu(QTestState
*s
)
105 if (s
->qemu_pid
!= -1) {
109 kill(s
->qemu_pid
, SIGTERM
);
110 pid
= waitpid(s
->qemu_pid
, &wstatus
, 0);
112 if (pid
== s
->qemu_pid
&& WIFSIGNALED(wstatus
)) {
113 assert(!WCOREDUMP(wstatus
));
118 static void kill_qemu_hook_func(void *s
)
123 static void sigabrt_handler(int signo
)
125 g_hook_list_invoke(&abrt_hooks
, FALSE
);
128 static void setup_sigabrt_handler(void)
130 struct sigaction sigact
;
132 /* Catch SIGABRT to clean up on g_assert() failure */
133 sigact
= (struct sigaction
){
134 .sa_handler
= sigabrt_handler
,
135 .sa_flags
= SA_RESETHAND
,
137 sigemptyset(&sigact
.sa_mask
);
138 sigaction(SIGABRT
, &sigact
, &sigact_old
);
141 static void cleanup_sigabrt_handler(void)
143 sigaction(SIGABRT
, &sigact_old
, NULL
);
146 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
)
150 /* Only install SIGABRT handler once */
151 if (!abrt_hooks
.is_setup
) {
152 g_hook_list_init(&abrt_hooks
, sizeof(GHook
));
154 setup_sigabrt_handler();
156 hook
= g_hook_alloc(&abrt_hooks
);
158 hook
->data
= (void *)data
;
160 g_hook_prepend(&abrt_hooks
, hook
);
163 static const char *qtest_qemu_binary(void)
165 const char *qemu_bin
;
167 qemu_bin
= getenv("QTEST_QEMU_BINARY");
169 fprintf(stderr
, "Environment variable QTEST_QEMU_BINARY required\n");
176 QTestState
*qtest_init_without_qmp_handshake(bool use_oob
,
177 const char *extra_args
)
180 int sock
, qmpsock
, i
;
182 gchar
*qmp_socket_path
;
184 const char *qemu_binary
= qtest_qemu_binary();
186 s
= g_new(QTestState
, 1);
188 socket_path
= g_strdup_printf("/tmp/qtest-%d.sock", getpid());
189 qmp_socket_path
= g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
191 /* It's possible that if an earlier test run crashed it might
192 * have left a stale unix socket lying around. Delete any
193 * stale old socket to avoid spurious test failures with
194 * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
197 unlink(qmp_socket_path
);
199 sock
= init_socket(socket_path
);
200 qmpsock
= init_socket(qmp_socket_path
);
202 qtest_add_abrt_handler(kill_qemu_hook_func
, s
);
204 s
->qemu_pid
= fork();
205 if (s
->qemu_pid
== 0) {
206 setenv("QEMU_AUDIO_DRV", "none", true);
207 command
= g_strdup_printf("exec %s "
208 "-qtest unix:%s,nowait "
210 "-chardev socket,path=%s,nowait,id=char0 "
211 "-mon chardev=char0,mode=control%s "
212 "-machine accel=qtest "
214 "%s", qemu_binary
, socket_path
,
215 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
216 qmp_socket_path
, use_oob
? ",x-oob=on" : "",
218 execlp("/bin/sh", "sh", "-c", command
, NULL
);
222 s
->fd
= socket_accept(sock
);
224 s
->qmp_fd
= socket_accept(qmpsock
);
227 unlink(qmp_socket_path
);
229 g_free(qmp_socket_path
);
231 g_assert(s
->fd
>= 0 && s
->qmp_fd
>= 0);
233 s
->rx
= g_string_new("");
234 for (i
= 0; i
< MAX_IRQ
; i
++) {
235 s
->irq_level
[i
] = false;
238 if (getenv("QTEST_STOP")) {
239 kill(s
->qemu_pid
, SIGSTOP
);
242 /* ask endianness of the target */
244 s
->big_endian
= qtest_query_target_endianness(s
);
249 QTestState
*qtest_init(const char *extra_args
)
251 QTestState
*s
= qtest_init_without_qmp_handshake(false, extra_args
);
253 /* Read the QMP greeting and then do the handshake */
254 qtest_qmp_discard_response(s
, "");
255 qtest_qmp_discard_response(s
, "{ 'execute': 'qmp_capabilities' }");
260 QTestState
*qtest_vstartf(const char *fmt
, va_list ap
)
262 char *args
= g_strdup_vprintf(fmt
, ap
);
265 s
= qtest_start(args
);
271 QTestState
*qtest_startf(const char *fmt
, ...)
277 s
= qtest_vstartf(fmt
, ap
);
282 void qtest_quit(QTestState
*s
)
284 g_hook_destroy_link(&abrt_hooks
, g_hook_find_data(&abrt_hooks
, TRUE
, s
));
286 /* Uninstall SIGABRT handler on last instance */
287 cleanup_sigabrt_handler();
292 g_string_free(s
->rx
, true);
296 static void socket_send(int fd
, const char *buf
, size_t size
)
301 while (offset
< size
) {
304 len
= write(fd
, buf
+ offset
, size
- offset
);
305 if (len
== -1 && errno
== EINTR
) {
309 g_assert_no_errno(len
);
310 g_assert_cmpint(len
, >, 0);
316 static void socket_sendf(int fd
, const char *fmt
, va_list ap
)
318 gchar
*str
= g_strdup_vprintf(fmt
, ap
);
319 size_t size
= strlen(str
);
321 socket_send(fd
, str
, size
);
325 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState
*s
, const char *fmt
, ...)
330 socket_sendf(s
->fd
, fmt
, ap
);
334 static GString
*qtest_recv_line(QTestState
*s
)
340 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
344 len
= read(s
->fd
, buffer
, sizeof(buffer
));
345 if (len
== -1 && errno
== EINTR
) {
349 if (len
== -1 || len
== 0) {
350 fprintf(stderr
, "Broken pipe\n");
354 g_string_append_len(s
->rx
, buffer
, len
);
357 offset
= eol
- s
->rx
->str
;
358 line
= g_string_new_len(s
->rx
->str
, offset
);
359 g_string_erase(s
->rx
, 0, offset
+ 1);
364 static gchar
**qtest_rsp(QTestState
*s
, int expected_args
)
371 line
= qtest_recv_line(s
);
372 words
= g_strsplit(line
->str
, " ", 0);
373 g_string_free(line
, TRUE
);
375 if (strcmp(words
[0], "IRQ") == 0) {
379 g_assert(words
[1] != NULL
);
380 g_assert(words
[2] != NULL
);
382 ret
= qemu_strtol(words
[2], NULL
, 0, &irq
);
384 g_assert_cmpint(irq
, >=, 0);
385 g_assert_cmpint(irq
, <, MAX_IRQ
);
387 if (strcmp(words
[1], "raise") == 0) {
388 s
->irq_level
[irq
] = true;
390 s
->irq_level
[irq
] = false;
397 g_assert(words
[0] != NULL
);
398 g_assert_cmpstr(words
[0], ==, "OK");
401 for (i
= 0; i
< expected_args
; i
++) {
402 g_assert(words
[i
] != NULL
);
411 static int qtest_query_target_endianness(QTestState
*s
)
416 qtest_sendf(s
, "endianness\n");
417 args
= qtest_rsp(s
, 1);
418 g_assert(strcmp(args
[1], "big") == 0 || strcmp(args
[1], "little") == 0);
419 big_endian
= strcmp(args
[1], "big") == 0;
426 JSONMessageParser parser
;
430 static void qmp_response(JSONMessageParser
*parser
, GQueue
*tokens
)
432 QMPResponseParser
*qmp
= container_of(parser
, QMPResponseParser
, parser
);
435 obj
= json_parser_parse(tokens
, NULL
);
437 fprintf(stderr
, "QMP JSON response parsing failed\n");
441 g_assert(!qmp
->response
);
442 qmp
->response
= qobject_to(QDict
, obj
);
443 g_assert(qmp
->response
);
446 QDict
*qmp_fd_receive(int fd
)
448 QMPResponseParser qmp
;
449 bool log
= getenv("QTEST_LOG") != NULL
;
452 json_message_parser_init(&qmp
.parser
, qmp_response
);
453 while (!qmp
.response
) {
457 len
= read(fd
, &c
, 1);
458 if (len
== -1 && errno
== EINTR
) {
462 if (len
== -1 || len
== 0) {
463 fprintf(stderr
, "Broken pipe\n");
468 len
= write(2, &c
, 1);
470 json_message_parser_feed(&qmp
.parser
, &c
, 1);
472 json_message_parser_destroy(&qmp
.parser
);
477 QDict
*qtest_qmp_receive(QTestState
*s
)
479 return qmp_fd_receive(s
->qmp_fd
);
483 * Allow users to send a message without waiting for the reply,
484 * in the case that they choose to discard all replies up until
485 * a particular EVENT is received.
487 void qmp_fd_sendv(int fd
, const char *fmt
, va_list ap
)
492 /* qobject_from_jsonv() silently eats leading 0xff as invalid
493 * JSON, but we want to test sending them over the wire to force
495 if (*fmt
== '\377') {
496 socket_send(fd
, fmt
, 1);
500 /* Going through qobject ensures we escape strings properly.
501 * This seemingly unnecessary copy is required in case va_list
504 va_copy(ap_copy
, ap
);
505 qobj
= qobject_from_jsonv(fmt
, &ap_copy
, &error_abort
);
508 /* No need to send anything for an empty QObject. */
510 int log
= getenv("QTEST_LOG") != NULL
;
511 QString
*qstr
= qobject_to_json(qobj
);
515 * BUG: QMP doesn't react to input until it sees a newline, an
516 * object, or an array. Work-around: give it a newline.
518 qstring_append_chr(qstr
, '\n');
519 str
= qstring_get_str(qstr
);
522 fprintf(stderr
, "%s", str
);
524 /* Send QMP request */
525 socket_send(fd
, str
, qstring_get_length(qstr
));
532 void qtest_async_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
534 qmp_fd_sendv(s
->qmp_fd
, fmt
, ap
);
537 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
)
539 qmp_fd_sendv(fd
, fmt
, ap
);
541 return qmp_fd_receive(fd
);
544 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
546 qtest_async_qmpv(s
, fmt
, ap
);
549 return qtest_qmp_receive(s
);
552 QDict
*qmp_fd(int fd
, const char *fmt
, ...)
558 response
= qmp_fdv(fd
, fmt
, ap
);
563 void qmp_fd_send(int fd
, const char *fmt
, ...)
568 qmp_fd_sendv(fd
, fmt
, ap
);
572 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
578 response
= qtest_qmpv(s
, fmt
, ap
);
583 void qtest_async_qmp(QTestState
*s
, const char *fmt
, ...)
588 qtest_async_qmpv(s
, fmt
, ap
);
592 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
)
594 QDict
*response
= qtest_qmpv(s
, fmt
, ap
);
595 qobject_unref(response
);
598 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...)
604 response
= qtest_qmpv(s
, fmt
, ap
);
606 qobject_unref(response
);
609 QDict
*qtest_qmp_eventwait_ref(QTestState
*s
, const char *event
)
614 response
= qtest_qmp_receive(s
);
615 if ((qdict_haskey(response
, "event")) &&
616 (strcmp(qdict_get_str(response
, "event"), event
) == 0)) {
619 qobject_unref(response
);
623 void qtest_qmp_eventwait(QTestState
*s
, const char *event
)
627 response
= qtest_qmp_eventwait_ref(s
, event
);
628 qobject_unref(response
);
631 char *qtest_hmpv(QTestState
*s
, const char *fmt
, va_list ap
)
637 cmd
= g_strdup_vprintf(fmt
, ap
);
638 resp
= qtest_qmp(s
, "{'execute': 'human-monitor-command',"
639 " 'arguments': {'command-line': %s}}",
641 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
642 while (ret
== NULL
&& qdict_get_try_str(resp
, "event")) {
643 /* Ignore asynchronous QMP events */
645 resp
= qtest_qmp_receive(s
);
646 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
654 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...)
660 ret
= qtest_hmpv(s
, fmt
, ap
);
665 const char *qtest_get_arch(void)
667 const char *qemu
= qtest_qemu_binary();
668 const char *end
= strrchr(qemu
, '/');
670 return end
+ strlen("/qemu-system-");
673 bool qtest_get_irq(QTestState
*s
, int num
)
675 /* dummy operation in order to make sure irq is up to date */
678 return s
->irq_level
[num
];
681 static int64_t qtest_clock_rsp(QTestState
*s
)
685 words
= qtest_rsp(s
, 2);
686 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
691 int64_t qtest_clock_step_next(QTestState
*s
)
693 qtest_sendf(s
, "clock_step\n");
694 return qtest_clock_rsp(s
);
697 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
699 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
700 return qtest_clock_rsp(s
);
703 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
705 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
706 return qtest_clock_rsp(s
);
709 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
711 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
715 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
717 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
721 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
723 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
727 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
729 qtest_out(s
, "outb", addr
, value
);
732 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
734 qtest_out(s
, "outw", addr
, value
);
737 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
739 qtest_out(s
, "outl", addr
, value
);
742 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
748 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
749 args
= qtest_rsp(s
, 2);
750 ret
= qemu_strtoul(args
[1], NULL
, 0, &value
);
751 g_assert(!ret
&& value
<= UINT32_MAX
);
757 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
759 return qtest_in(s
, "inb", addr
);
762 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
764 return qtest_in(s
, "inw", addr
);
767 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
769 return qtest_in(s
, "inl", addr
);
772 static void qtest_write(QTestState
*s
, const char *cmd
, uint64_t addr
,
775 qtest_sendf(s
, "%s 0x%" PRIx64
" 0x%" PRIx64
"\n", cmd
, addr
, value
);
779 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
)
781 qtest_write(s
, "writeb", addr
, value
);
784 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
)
786 qtest_write(s
, "writew", addr
, value
);
789 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
)
791 qtest_write(s
, "writel", addr
, value
);
794 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
)
796 qtest_write(s
, "writeq", addr
, value
);
799 static uint64_t qtest_read(QTestState
*s
, const char *cmd
, uint64_t addr
)
805 qtest_sendf(s
, "%s 0x%" PRIx64
"\n", cmd
, addr
);
806 args
= qtest_rsp(s
, 2);
807 ret
= qemu_strtou64(args
[1], NULL
, 0, &value
);
814 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
)
816 return qtest_read(s
, "readb", addr
);
819 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
)
821 return qtest_read(s
, "readw", addr
);
824 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
)
826 return qtest_read(s
, "readl", addr
);
829 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
)
831 return qtest_read(s
, "readq", addr
);
834 static int hex2nib(char ch
)
836 if (ch
>= '0' && ch
<= '9') {
838 } else if (ch
>= 'a' && ch
<= 'f') {
839 return 10 + (ch
- 'a');
840 } else if (ch
>= 'A' && ch
<= 'F') {
841 return 10 + (ch
- 'a');
847 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
857 qtest_sendf(s
, "read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
858 args
= qtest_rsp(s
, 2);
860 for (i
= 0; i
< size
; i
++) {
861 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
862 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
868 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
869 uint32_t nargs
, uint64_t args
,
870 uint32_t nret
, uint64_t ret
)
872 qtest_sendf(s
, "rtas %s %u 0x%"PRIx64
" %u 0x%"PRIx64
"\n",
873 name
, nargs
, args
, nret
, ret
);
878 void qtest_add_func(const char *str
, void (*fn
)(void))
880 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
881 g_test_add_func(path
, fn
);
885 void qtest_add_data_func_full(const char *str
, void *data
,
886 void (*fn
)(const void *),
887 GDestroyNotify data_free_func
)
889 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
890 g_test_add_data_func_full(path
, data
, fn
, data_free_func
);
894 void qtest_add_data_func(const char *str
, const void *data
,
895 void (*fn
)(const void *))
897 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
898 g_test_add_data_func(path
, data
, fn
);
902 void qtest_bufwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
906 bdata
= g_base64_encode(data
, size
);
907 qtest_sendf(s
, "b64write 0x%" PRIx64
" 0x%zx ", addr
, size
);
908 socket_send(s
->fd
, bdata
, strlen(bdata
));
909 socket_send(s
->fd
, "\n", 1);
914 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
919 qtest_sendf(s
, "b64read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
920 args
= qtest_rsp(s
, 2);
922 g_base64_decode_inplace(args
[1], &len
);
924 fprintf(stderr
, "bufread: asked for %zu bytes but decoded %zu\n",
926 len
= MIN(len
, size
);
929 memcpy(data
, args
[1], len
);
933 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
935 const uint8_t *ptr
= data
;
943 enc
= g_malloc(2 * size
+ 1);
945 for (i
= 0; i
< size
; i
++) {
946 sprintf(&enc
[i
* 2], "%02x", ptr
[i
]);
949 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x%s\n", addr
, size
, enc
);
954 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t pattern
, size_t size
)
956 qtest_sendf(s
, "memset 0x%" PRIx64
" 0x%zx 0x%02x\n", addr
, size
, pattern
);
960 QDict
*qmp(const char *fmt
, ...)
966 response
= qtest_qmpv(global_qtest
, fmt
, ap
);
971 void qmp_async(const char *fmt
, ...)
976 qtest_async_qmpv(global_qtest
, fmt
, ap
);
980 void qmp_discard_response(const char *fmt
, ...)
985 qtest_qmpv_discard_response(global_qtest
, fmt
, ap
);
988 char *hmp(const char *fmt
, ...)
994 ret
= qtest_hmpv(global_qtest
, fmt
, ap
);
999 bool qtest_big_endian(QTestState
*s
)
1001 return s
->big_endian
;
1004 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
))
1006 QDict
*response
, *minfo
;
1008 const QListEntry
*p
;
1013 qtest_start("-machine none");
1014 response
= qmp("{ 'execute': 'query-machines' }");
1016 list
= qdict_get_qlist(response
, "return");
1019 for (p
= qlist_first(list
); p
; p
= qlist_next(p
)) {
1020 minfo
= qobject_to(QDict
, qlist_entry_obj(p
));
1022 qobj
= qdict_get(minfo
, "name");
1024 qstr
= qobject_to(QString
, qobj
);
1026 mname
= qstring_get_str(qstr
);
1031 qobject_unref(response
);
1035 * Generic hot-plugging test via the device_add QMP command.
1037 void qtest_qmp_device_add(const char *driver
, const char *id
, const char *fmt
,
1041 char *cmd
, *opts
= NULL
;
1046 opts
= g_strdup_vprintf(fmt
, va
);
1050 cmd
= g_strdup_printf("{'execute': 'device_add',"
1051 " 'arguments': { 'driver': '%s', 'id': '%s'%s%s }}",
1052 driver
, id
, opts
? ", " : "", opts
? opts
: "");
1055 response
= qmp(cmd
);
1058 g_assert(!qdict_haskey(response
, "event")); /* We don't expect any events */
1059 g_assert(!qdict_haskey(response
, "error"));
1060 qobject_unref(response
);
1064 * Generic hot-unplugging test via the device_del QMP command.
1065 * Device deletion will get one response and one event. For example:
1067 * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
1069 * will get this one:
1071 * {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
1072 * "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
1073 * "path": "/machine/peripheral/scsi-hd"}}
1079 * But the order of arrival may vary - so we've got to detect both.
1081 void qtest_qmp_device_del(const char *id
)
1083 QDict
*response1
, *response2
, *event
= NULL
;
1086 cmd
= g_strdup_printf("{'execute': 'device_del',"
1087 " 'arguments': { 'id': '%s' }}", id
);
1088 response1
= qmp(cmd
);
1090 g_assert(response1
);
1091 g_assert(!qdict_haskey(response1
, "error"));
1093 response2
= qmp("");
1094 g_assert(response2
);
1095 g_assert(!qdict_haskey(response2
, "error"));
1097 if (qdict_haskey(response1
, "event")) {
1099 } else if (qdict_haskey(response2
, "event")) {
1103 g_assert_cmpstr(qdict_get_str(event
, "event"), ==, "DEVICE_DELETED");
1105 qobject_unref(response1
);
1106 qobject_unref(response2
);
1109 bool qmp_rsp_is_err(QDict
*rsp
)
1111 QDict
*error
= qdict_get_qdict(rsp
, "error");