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) {
106 kill(s
->qemu_pid
, SIGTERM
);
107 waitpid(s
->qemu_pid
, NULL
, 0);
111 static void kill_qemu_hook_func(void *s
)
116 static void sigabrt_handler(int signo
)
118 g_hook_list_invoke(&abrt_hooks
, FALSE
);
121 static void setup_sigabrt_handler(void)
123 struct sigaction sigact
;
125 /* Catch SIGABRT to clean up on g_assert() failure */
126 sigact
= (struct sigaction
){
127 .sa_handler
= sigabrt_handler
,
128 .sa_flags
= SA_RESETHAND
,
130 sigemptyset(&sigact
.sa_mask
);
131 sigaction(SIGABRT
, &sigact
, &sigact_old
);
134 static void cleanup_sigabrt_handler(void)
136 sigaction(SIGABRT
, &sigact_old
, NULL
);
139 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
)
143 /* Only install SIGABRT handler once */
144 if (!abrt_hooks
.is_setup
) {
145 g_hook_list_init(&abrt_hooks
, sizeof(GHook
));
147 setup_sigabrt_handler();
149 hook
= g_hook_alloc(&abrt_hooks
);
151 hook
->data
= (void *)data
;
153 g_hook_prepend(&abrt_hooks
, hook
);
156 static const char *qtest_qemu_binary(void)
158 const char *qemu_bin
;
160 qemu_bin
= getenv("QTEST_QEMU_BINARY");
162 fprintf(stderr
, "Environment variable QTEST_QEMU_BINARY required\n");
169 QTestState
*qtest_init_without_qmp_handshake(bool use_oob
,
170 const char *extra_args
)
173 int sock
, qmpsock
, i
;
175 gchar
*qmp_socket_path
;
177 const char *qemu_binary
= qtest_qemu_binary();
179 s
= g_new(QTestState
, 1);
181 socket_path
= g_strdup_printf("/tmp/qtest-%d.sock", getpid());
182 qmp_socket_path
= g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
184 /* It's possible that if an earlier test run crashed it might
185 * have left a stale unix socket lying around. Delete any
186 * stale old socket to avoid spurious test failures with
187 * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
190 unlink(qmp_socket_path
);
192 sock
= init_socket(socket_path
);
193 qmpsock
= init_socket(qmp_socket_path
);
195 qtest_add_abrt_handler(kill_qemu_hook_func
, s
);
197 s
->qemu_pid
= fork();
198 if (s
->qemu_pid
== 0) {
199 setenv("QEMU_AUDIO_DRV", "none", true);
200 command
= g_strdup_printf("exec %s "
201 "-qtest unix:%s,nowait "
203 "-chardev socket,path=%s,nowait,id=char0 "
204 "-mon chardev=char0,mode=control%s "
205 "-machine accel=qtest "
207 "%s", qemu_binary
, socket_path
,
208 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
209 qmp_socket_path
, use_oob
? ",x-oob=on" : "",
211 execlp("/bin/sh", "sh", "-c", command
, NULL
);
215 s
->fd
= socket_accept(sock
);
217 s
->qmp_fd
= socket_accept(qmpsock
);
220 unlink(qmp_socket_path
);
222 g_free(qmp_socket_path
);
224 g_assert(s
->fd
>= 0 && s
->qmp_fd
>= 0);
226 s
->rx
= g_string_new("");
227 for (i
= 0; i
< MAX_IRQ
; i
++) {
228 s
->irq_level
[i
] = false;
231 if (getenv("QTEST_STOP")) {
232 kill(s
->qemu_pid
, SIGSTOP
);
235 /* ask endianness of the target */
237 s
->big_endian
= qtest_query_target_endianness(s
);
242 QTestState
*qtest_init(const char *extra_args
)
244 QTestState
*s
= qtest_init_without_qmp_handshake(false, extra_args
);
246 /* Read the QMP greeting and then do the handshake */
247 qtest_qmp_discard_response(s
, "");
248 qtest_qmp_discard_response(s
, "{ 'execute': 'qmp_capabilities' }");
253 QTestState
*qtest_vstartf(const char *fmt
, va_list ap
)
255 char *args
= g_strdup_vprintf(fmt
, ap
);
258 s
= qtest_start(args
);
264 QTestState
*qtest_startf(const char *fmt
, ...)
270 s
= qtest_vstartf(fmt
, ap
);
275 void qtest_quit(QTestState
*s
)
277 g_hook_destroy_link(&abrt_hooks
, g_hook_find_data(&abrt_hooks
, TRUE
, s
));
279 /* Uninstall SIGABRT handler on last instance */
280 cleanup_sigabrt_handler();
285 g_string_free(s
->rx
, true);
289 static void socket_send(int fd
, const char *buf
, size_t size
)
294 while (offset
< size
) {
297 len
= write(fd
, buf
+ offset
, size
- offset
);
298 if (len
== -1 && errno
== EINTR
) {
302 g_assert_no_errno(len
);
303 g_assert_cmpint(len
, >, 0);
309 static void socket_sendf(int fd
, const char *fmt
, va_list ap
)
311 gchar
*str
= g_strdup_vprintf(fmt
, ap
);
312 size_t size
= strlen(str
);
314 socket_send(fd
, str
, size
);
318 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState
*s
, const char *fmt
, ...)
323 socket_sendf(s
->fd
, fmt
, ap
);
327 static GString
*qtest_recv_line(QTestState
*s
)
333 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
337 len
= read(s
->fd
, buffer
, sizeof(buffer
));
338 if (len
== -1 && errno
== EINTR
) {
342 if (len
== -1 || len
== 0) {
343 fprintf(stderr
, "Broken pipe\n");
347 g_string_append_len(s
->rx
, buffer
, len
);
350 offset
= eol
- s
->rx
->str
;
351 line
= g_string_new_len(s
->rx
->str
, offset
);
352 g_string_erase(s
->rx
, 0, offset
+ 1);
357 static gchar
**qtest_rsp(QTestState
*s
, int expected_args
)
364 line
= qtest_recv_line(s
);
365 words
= g_strsplit(line
->str
, " ", 0);
366 g_string_free(line
, TRUE
);
368 if (strcmp(words
[0], "IRQ") == 0) {
372 g_assert(words
[1] != NULL
);
373 g_assert(words
[2] != NULL
);
375 ret
= qemu_strtol(words
[2], NULL
, 0, &irq
);
377 g_assert_cmpint(irq
, >=, 0);
378 g_assert_cmpint(irq
, <, MAX_IRQ
);
380 if (strcmp(words
[1], "raise") == 0) {
381 s
->irq_level
[irq
] = true;
383 s
->irq_level
[irq
] = false;
390 g_assert(words
[0] != NULL
);
391 g_assert_cmpstr(words
[0], ==, "OK");
394 for (i
= 0; i
< expected_args
; i
++) {
395 g_assert(words
[i
] != NULL
);
404 static int qtest_query_target_endianness(QTestState
*s
)
409 qtest_sendf(s
, "endianness\n");
410 args
= qtest_rsp(s
, 1);
411 g_assert(strcmp(args
[1], "big") == 0 || strcmp(args
[1], "little") == 0);
412 big_endian
= strcmp(args
[1], "big") == 0;
419 JSONMessageParser parser
;
423 static void qmp_response(JSONMessageParser
*parser
, GQueue
*tokens
)
425 QMPResponseParser
*qmp
= container_of(parser
, QMPResponseParser
, parser
);
428 obj
= json_parser_parse(tokens
, NULL
);
430 fprintf(stderr
, "QMP JSON response parsing failed\n");
434 g_assert(!qmp
->response
);
435 qmp
->response
= qobject_to(QDict
, obj
);
436 g_assert(qmp
->response
);
439 QDict
*qmp_fd_receive(int fd
)
441 QMPResponseParser qmp
;
442 bool log
= getenv("QTEST_LOG") != NULL
;
445 json_message_parser_init(&qmp
.parser
, qmp_response
);
446 while (!qmp
.response
) {
450 len
= read(fd
, &c
, 1);
451 if (len
== -1 && errno
== EINTR
) {
455 if (len
== -1 || len
== 0) {
456 fprintf(stderr
, "Broken pipe\n");
461 len
= write(2, &c
, 1);
463 json_message_parser_feed(&qmp
.parser
, &c
, 1);
465 json_message_parser_destroy(&qmp
.parser
);
470 QDict
*qtest_qmp_receive(QTestState
*s
)
472 return qmp_fd_receive(s
->qmp_fd
);
476 * Allow users to send a message without waiting for the reply,
477 * in the case that they choose to discard all replies up until
478 * a particular EVENT is received.
480 void qmp_fd_sendv(int fd
, const char *fmt
, va_list ap
)
485 /* qobject_from_jsonv() silently eats leading 0xff as invalid
486 * JSON, but we want to test sending them over the wire to force
488 if (*fmt
== '\377') {
489 socket_send(fd
, fmt
, 1);
493 /* Going through qobject ensures we escape strings properly.
494 * This seemingly unnecessary copy is required in case va_list
497 va_copy(ap_copy
, ap
);
498 qobj
= qobject_from_jsonv(fmt
, &ap_copy
, &error_abort
);
501 /* No need to send anything for an empty QObject. */
503 int log
= getenv("QTEST_LOG") != NULL
;
504 QString
*qstr
= qobject_to_json(qobj
);
508 * BUG: QMP doesn't react to input until it sees a newline, an
509 * object, or an array. Work-around: give it a newline.
511 qstring_append_chr(qstr
, '\n');
512 str
= qstring_get_str(qstr
);
515 fprintf(stderr
, "%s", str
);
517 /* Send QMP request */
518 socket_send(fd
, str
, qstring_get_length(qstr
));
525 void qtest_async_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
527 qmp_fd_sendv(s
->qmp_fd
, fmt
, ap
);
530 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
)
532 qmp_fd_sendv(fd
, fmt
, ap
);
534 return qmp_fd_receive(fd
);
537 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
539 qtest_async_qmpv(s
, fmt
, ap
);
542 return qtest_qmp_receive(s
);
545 QDict
*qmp_fd(int fd
, const char *fmt
, ...)
551 response
= qmp_fdv(fd
, fmt
, ap
);
556 void qmp_fd_send(int fd
, const char *fmt
, ...)
561 qmp_fd_sendv(fd
, fmt
, ap
);
565 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
571 response
= qtest_qmpv(s
, fmt
, ap
);
576 void qtest_async_qmp(QTestState
*s
, const char *fmt
, ...)
581 qtest_async_qmpv(s
, fmt
, ap
);
585 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
)
587 QDict
*response
= qtest_qmpv(s
, fmt
, ap
);
588 qobject_unref(response
);
591 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...)
597 response
= qtest_qmpv(s
, fmt
, ap
);
599 qobject_unref(response
);
602 QDict
*qtest_qmp_eventwait_ref(QTestState
*s
, const char *event
)
607 response
= qtest_qmp_receive(s
);
608 if ((qdict_haskey(response
, "event")) &&
609 (strcmp(qdict_get_str(response
, "event"), event
) == 0)) {
612 qobject_unref(response
);
616 void qtest_qmp_eventwait(QTestState
*s
, const char *event
)
620 response
= qtest_qmp_eventwait_ref(s
, event
);
621 qobject_unref(response
);
624 char *qtest_hmpv(QTestState
*s
, const char *fmt
, va_list ap
)
630 cmd
= g_strdup_vprintf(fmt
, ap
);
631 resp
= qtest_qmp(s
, "{'execute': 'human-monitor-command',"
632 " 'arguments': {'command-line': %s}}",
634 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
635 while (ret
== NULL
&& qdict_get_try_str(resp
, "event")) {
636 /* Ignore asynchronous QMP events */
638 resp
= qtest_qmp_receive(s
);
639 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
647 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...)
653 ret
= qtest_hmpv(s
, fmt
, ap
);
658 const char *qtest_get_arch(void)
660 const char *qemu
= qtest_qemu_binary();
661 const char *end
= strrchr(qemu
, '/');
663 return end
+ strlen("/qemu-system-");
666 bool qtest_get_irq(QTestState
*s
, int num
)
668 /* dummy operation in order to make sure irq is up to date */
671 return s
->irq_level
[num
];
674 static int64_t qtest_clock_rsp(QTestState
*s
)
678 words
= qtest_rsp(s
, 2);
679 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
684 int64_t qtest_clock_step_next(QTestState
*s
)
686 qtest_sendf(s
, "clock_step\n");
687 return qtest_clock_rsp(s
);
690 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
692 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
693 return qtest_clock_rsp(s
);
696 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
698 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
699 return qtest_clock_rsp(s
);
702 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
704 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
708 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
710 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
714 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
716 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
720 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
722 qtest_out(s
, "outb", addr
, value
);
725 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
727 qtest_out(s
, "outw", addr
, value
);
730 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
732 qtest_out(s
, "outl", addr
, value
);
735 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
741 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
742 args
= qtest_rsp(s
, 2);
743 ret
= qemu_strtoul(args
[1], NULL
, 0, &value
);
744 g_assert(!ret
&& value
<= UINT32_MAX
);
750 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
752 return qtest_in(s
, "inb", addr
);
755 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
757 return qtest_in(s
, "inw", addr
);
760 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
762 return qtest_in(s
, "inl", addr
);
765 static void qtest_write(QTestState
*s
, const char *cmd
, uint64_t addr
,
768 qtest_sendf(s
, "%s 0x%" PRIx64
" 0x%" PRIx64
"\n", cmd
, addr
, value
);
772 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
)
774 qtest_write(s
, "writeb", addr
, value
);
777 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
)
779 qtest_write(s
, "writew", addr
, value
);
782 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
)
784 qtest_write(s
, "writel", addr
, value
);
787 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
)
789 qtest_write(s
, "writeq", addr
, value
);
792 static uint64_t qtest_read(QTestState
*s
, const char *cmd
, uint64_t addr
)
798 qtest_sendf(s
, "%s 0x%" PRIx64
"\n", cmd
, addr
);
799 args
= qtest_rsp(s
, 2);
800 ret
= qemu_strtou64(args
[1], NULL
, 0, &value
);
807 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
)
809 return qtest_read(s
, "readb", addr
);
812 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
)
814 return qtest_read(s
, "readw", addr
);
817 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
)
819 return qtest_read(s
, "readl", addr
);
822 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
)
824 return qtest_read(s
, "readq", addr
);
827 static int hex2nib(char ch
)
829 if (ch
>= '0' && ch
<= '9') {
831 } else if (ch
>= 'a' && ch
<= 'f') {
832 return 10 + (ch
- 'a');
833 } else if (ch
>= 'A' && ch
<= 'F') {
834 return 10 + (ch
- 'a');
840 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
850 qtest_sendf(s
, "read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
851 args
= qtest_rsp(s
, 2);
853 for (i
= 0; i
< size
; i
++) {
854 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
855 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
861 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
862 uint32_t nargs
, uint64_t args
,
863 uint32_t nret
, uint64_t ret
)
865 qtest_sendf(s
, "rtas %s %u 0x%"PRIx64
" %u 0x%"PRIx64
"\n",
866 name
, nargs
, args
, nret
, ret
);
871 void qtest_add_func(const char *str
, void (*fn
)(void))
873 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
874 g_test_add_func(path
, fn
);
878 void qtest_add_data_func_full(const char *str
, void *data
,
879 void (*fn
)(const void *),
880 GDestroyNotify data_free_func
)
882 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
883 g_test_add_data_func_full(path
, data
, fn
, data_free_func
);
887 void qtest_add_data_func(const char *str
, const void *data
,
888 void (*fn
)(const void *))
890 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
891 g_test_add_data_func(path
, data
, fn
);
895 void qtest_bufwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
899 bdata
= g_base64_encode(data
, size
);
900 qtest_sendf(s
, "b64write 0x%" PRIx64
" 0x%zx ", addr
, size
);
901 socket_send(s
->fd
, bdata
, strlen(bdata
));
902 socket_send(s
->fd
, "\n", 1);
907 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
912 qtest_sendf(s
, "b64read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
913 args
= qtest_rsp(s
, 2);
915 g_base64_decode_inplace(args
[1], &len
);
917 fprintf(stderr
, "bufread: asked for %zu bytes but decoded %zu\n",
919 len
= MIN(len
, size
);
922 memcpy(data
, args
[1], len
);
926 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
928 const uint8_t *ptr
= data
;
936 enc
= g_malloc(2 * size
+ 1);
938 for (i
= 0; i
< size
; i
++) {
939 sprintf(&enc
[i
* 2], "%02x", ptr
[i
]);
942 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x%s\n", addr
, size
, enc
);
947 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t pattern
, size_t size
)
949 qtest_sendf(s
, "memset 0x%" PRIx64
" 0x%zx 0x%02x\n", addr
, size
, pattern
);
953 QDict
*qmp(const char *fmt
, ...)
959 response
= qtest_qmpv(global_qtest
, fmt
, ap
);
964 void qmp_async(const char *fmt
, ...)
969 qtest_async_qmpv(global_qtest
, fmt
, ap
);
973 void qmp_discard_response(const char *fmt
, ...)
978 qtest_qmpv_discard_response(global_qtest
, fmt
, ap
);
981 char *hmp(const char *fmt
, ...)
987 ret
= qtest_hmpv(global_qtest
, fmt
, ap
);
992 bool qtest_big_endian(QTestState
*s
)
994 return s
->big_endian
;
997 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
))
999 QDict
*response
, *minfo
;
1001 const QListEntry
*p
;
1006 qtest_start("-machine none");
1007 response
= qmp("{ 'execute': 'query-machines' }");
1009 list
= qdict_get_qlist(response
, "return");
1012 for (p
= qlist_first(list
); p
; p
= qlist_next(p
)) {
1013 minfo
= qobject_to(QDict
, qlist_entry_obj(p
));
1015 qobj
= qdict_get(minfo
, "name");
1017 qstr
= qobject_to(QString
, qobj
);
1019 mname
= qstring_get_str(qstr
);
1024 qobject_unref(response
);
1028 * Generic hot-plugging test via the device_add QMP command.
1030 void qtest_qmp_device_add(const char *driver
, const char *id
, const char *fmt
,
1034 char *cmd
, *opts
= NULL
;
1039 opts
= g_strdup_vprintf(fmt
, va
);
1043 cmd
= g_strdup_printf("{'execute': 'device_add',"
1044 " 'arguments': { 'driver': '%s', 'id': '%s'%s%s }}",
1045 driver
, id
, opts
? ", " : "", opts
? opts
: "");
1048 response
= qmp(cmd
);
1051 g_assert(!qdict_haskey(response
, "event")); /* We don't expect any events */
1052 g_assert(!qdict_haskey(response
, "error"));
1053 qobject_unref(response
);
1057 * Generic hot-unplugging test via the device_del QMP command.
1058 * Device deletion will get one response and one event. For example:
1060 * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
1062 * will get this one:
1064 * {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
1065 * "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
1066 * "path": "/machine/peripheral/scsi-hd"}}
1072 * But the order of arrival may vary - so we've got to detect both.
1074 void qtest_qmp_device_del(const char *id
)
1076 QDict
*response1
, *response2
, *event
= NULL
;
1079 cmd
= g_strdup_printf("{'execute': 'device_del',"
1080 " 'arguments': { 'id': '%s' }}", id
);
1081 response1
= qmp(cmd
);
1083 g_assert(response1
);
1084 g_assert(!qdict_haskey(response1
, "error"));
1086 response2
= qmp("");
1087 g_assert(response2
);
1088 g_assert(!qdict_haskey(response2
, "error"));
1090 if (qdict_haskey(response1
, "event")) {
1092 } else if (qdict_haskey(response2
, "event")) {
1096 g_assert_cmpstr(qdict_get_str(event
, "event"), ==, "DEVICE_DELETED");
1098 qobject_unref(response1
);
1099 qobject_unref(response2
);