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-common.h"
25 #include "qemu/cutils.h"
26 #include "qapi/error.h"
27 #include "qapi/qmp/json-parser.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 pid_t qemu_pid
; /* our child QEMU process */
45 bool irq_level
[MAX_IRQ
];
49 static GHookList abrt_hooks
;
50 static struct sigaction sigact_old
;
52 static int qtest_query_target_endianness(QTestState
*s
);
54 static int init_socket(const char *socket_path
)
56 struct sockaddr_un addr
;
60 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
61 g_assert_cmpint(sock
, !=, -1);
63 addr
.sun_family
= AF_UNIX
;
64 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socket_path
);
65 qemu_set_cloexec(sock
);
68 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
69 } while (ret
== -1 && errno
== EINTR
);
70 g_assert_cmpint(ret
, !=, -1);
71 ret
= listen(sock
, 1);
72 g_assert_cmpint(ret
, !=, -1);
77 static int socket_accept(int sock
)
79 struct sockaddr_un addr
;
82 struct timeval timeout
= { .tv_sec
= SOCKET_TIMEOUT
,
85 setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, (void *)&timeout
,
89 addrlen
= sizeof(addr
);
90 ret
= accept(sock
, (struct sockaddr
*)&addr
, &addrlen
);
91 } while (ret
== -1 && errno
== EINTR
);
93 fprintf(stderr
, "%s failed: %s\n", __func__
, strerror(errno
));
100 bool qtest_probe_child(QTestState
*s
)
102 pid_t pid
= s
->qemu_pid
;
105 pid
= waitpid(pid
, &s
->wstatus
, WNOHANG
);
114 static void kill_qemu(QTestState
*s
)
116 pid_t pid
= s
->qemu_pid
;
119 /* Skip wait if qtest_probe_child already reaped. */
122 TFR(pid
= waitpid(s
->qemu_pid
, &s
->wstatus
, 0));
123 assert(pid
== s
->qemu_pid
);
127 * We expect qemu to exit with status 0; anything else is
128 * fishy and should be logged with as much detail as possible.
130 wstatus
= s
->wstatus
;
132 if (WIFEXITED(wstatus
)) {
133 fprintf(stderr
, "%s:%d: kill_qemu() tried to terminate QEMU "
134 "process but encountered exit status %d\n",
135 __FILE__
, __LINE__
, WEXITSTATUS(wstatus
));
136 } else if (WIFSIGNALED(wstatus
)) {
137 int sig
= WTERMSIG(wstatus
);
138 const char *signame
= strsignal(sig
) ?: "unknown ???";
139 const char *dump
= WCOREDUMP(wstatus
) ? " (core dumped)" : "";
141 fprintf(stderr
, "%s:%d: kill_qemu() detected QEMU death "
142 "from signal %d (%s)%s\n",
143 __FILE__
, __LINE__
, sig
, signame
, dump
);
149 static void kill_qemu_hook_func(void *s
)
154 static void sigabrt_handler(int signo
)
156 g_hook_list_invoke(&abrt_hooks
, FALSE
);
159 static void setup_sigabrt_handler(void)
161 struct sigaction sigact
;
163 /* Catch SIGABRT to clean up on g_assert() failure */
164 sigact
= (struct sigaction
){
165 .sa_handler
= sigabrt_handler
,
166 .sa_flags
= SA_RESETHAND
,
168 sigemptyset(&sigact
.sa_mask
);
169 sigaction(SIGABRT
, &sigact
, &sigact_old
);
172 static void cleanup_sigabrt_handler(void)
174 sigaction(SIGABRT
, &sigact_old
, NULL
);
177 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
)
181 /* Only install SIGABRT handler once */
182 if (!abrt_hooks
.is_setup
) {
183 g_hook_list_init(&abrt_hooks
, sizeof(GHook
));
185 setup_sigabrt_handler();
187 hook
= g_hook_alloc(&abrt_hooks
);
189 hook
->data
= (void *)data
;
191 g_hook_prepend(&abrt_hooks
, hook
);
194 static const char *qtest_qemu_binary(void)
196 const char *qemu_bin
;
198 qemu_bin
= getenv("QTEST_QEMU_BINARY");
200 fprintf(stderr
, "Environment variable QTEST_QEMU_BINARY required\n");
207 QTestState
*qtest_init_without_qmp_handshake(const char *extra_args
)
210 int sock
, qmpsock
, i
;
212 gchar
*qmp_socket_path
;
214 const char *qemu_binary
= qtest_qemu_binary();
216 s
= g_new(QTestState
, 1);
218 socket_path
= g_strdup_printf("/tmp/qtest-%d.sock", getpid());
219 qmp_socket_path
= g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
221 /* It's possible that if an earlier test run crashed it might
222 * have left a stale unix socket lying around. Delete any
223 * stale old socket to avoid spurious test failures with
224 * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
227 unlink(qmp_socket_path
);
229 sock
= init_socket(socket_path
);
230 qmpsock
= init_socket(qmp_socket_path
);
232 qtest_add_abrt_handler(kill_qemu_hook_func
, s
);
234 command
= g_strdup_printf("exec %s "
235 "-qtest unix:%s,nowait "
237 "-chardev socket,path=%s,nowait,id=char0 "
238 "-mon chardev=char0,mode=control "
239 "-machine accel=qtest "
241 "%s", qemu_binary
, socket_path
,
242 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
246 g_test_message("starting QEMU: %s", command
);
249 s
->qemu_pid
= fork();
250 if (s
->qemu_pid
== 0) {
251 setenv("QEMU_AUDIO_DRV", "none", true);
252 execlp("/bin/sh", "sh", "-c", command
, NULL
);
257 s
->fd
= socket_accept(sock
);
259 s
->qmp_fd
= socket_accept(qmpsock
);
262 unlink(qmp_socket_path
);
264 g_free(qmp_socket_path
);
266 g_assert(s
->fd
>= 0 && s
->qmp_fd
>= 0);
268 s
->rx
= g_string_new("");
269 for (i
= 0; i
< MAX_IRQ
; i
++) {
270 s
->irq_level
[i
] = false;
273 if (getenv("QTEST_STOP")) {
274 kill(s
->qemu_pid
, SIGSTOP
);
277 /* ask endianness of the target */
279 s
->big_endian
= qtest_query_target_endianness(s
);
284 QTestState
*qtest_init(const char *extra_args
)
286 QTestState
*s
= qtest_init_without_qmp_handshake(extra_args
);
289 /* Read the QMP greeting and then do the handshake */
290 greeting
= qtest_qmp_receive(s
);
291 qobject_unref(greeting
);
292 qobject_unref(qtest_qmp(s
, "{ 'execute': 'qmp_capabilities' }"));
297 QTestState
*qtest_vinitf(const char *fmt
, va_list ap
)
299 char *args
= g_strdup_vprintf(fmt
, ap
);
302 s
= qtest_init(args
);
307 QTestState
*qtest_initf(const char *fmt
, ...)
313 s
= qtest_vinitf(fmt
, ap
);
318 void qtest_quit(QTestState
*s
)
320 g_hook_destroy_link(&abrt_hooks
, g_hook_find_data(&abrt_hooks
, TRUE
, s
));
322 /* Uninstall SIGABRT handler on last instance */
323 cleanup_sigabrt_handler();
328 g_string_free(s
->rx
, true);
332 static void socket_send(int fd
, const char *buf
, size_t size
)
337 while (offset
< size
) {
340 len
= write(fd
, buf
+ offset
, size
- offset
);
341 if (len
== -1 && errno
== EINTR
) {
345 g_assert_cmpint(len
, >, 0);
351 static void socket_sendf(int fd
, const char *fmt
, va_list ap
)
353 gchar
*str
= g_strdup_vprintf(fmt
, ap
);
354 size_t size
= strlen(str
);
356 socket_send(fd
, str
, size
);
360 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState
*s
, const char *fmt
, ...)
365 socket_sendf(s
->fd
, fmt
, ap
);
369 static GString
*qtest_recv_line(QTestState
*s
)
375 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
379 len
= read(s
->fd
, buffer
, sizeof(buffer
));
380 if (len
== -1 && errno
== EINTR
) {
384 if (len
== -1 || len
== 0) {
385 fprintf(stderr
, "Broken pipe\n");
389 g_string_append_len(s
->rx
, buffer
, len
);
392 offset
= eol
- s
->rx
->str
;
393 line
= g_string_new_len(s
->rx
->str
, offset
);
394 g_string_erase(s
->rx
, 0, offset
+ 1);
399 static gchar
**qtest_rsp(QTestState
*s
, int expected_args
)
406 line
= qtest_recv_line(s
);
407 words
= g_strsplit(line
->str
, " ", 0);
408 g_string_free(line
, TRUE
);
410 if (strcmp(words
[0], "IRQ") == 0) {
414 g_assert(words
[1] != NULL
);
415 g_assert(words
[2] != NULL
);
417 ret
= qemu_strtol(words
[2], NULL
, 0, &irq
);
419 g_assert_cmpint(irq
, >=, 0);
420 g_assert_cmpint(irq
, <, MAX_IRQ
);
422 if (strcmp(words
[1], "raise") == 0) {
423 s
->irq_level
[irq
] = true;
425 s
->irq_level
[irq
] = false;
432 g_assert(words
[0] != NULL
);
433 g_assert_cmpstr(words
[0], ==, "OK");
436 for (i
= 0; i
< expected_args
; i
++) {
437 g_assert(words
[i
] != NULL
);
446 static int qtest_query_target_endianness(QTestState
*s
)
451 qtest_sendf(s
, "endianness\n");
452 args
= qtest_rsp(s
, 1);
453 g_assert(strcmp(args
[1], "big") == 0 || strcmp(args
[1], "little") == 0);
454 big_endian
= strcmp(args
[1], "big") == 0;
461 JSONMessageParser parser
;
465 static void qmp_response(void *opaque
, QObject
*obj
, Error
*err
)
467 QMPResponseParser
*qmp
= opaque
;
469 assert(!obj
!= !err
);
472 error_prepend(&err
, "QMP JSON response parsing failed: ");
473 error_report_err(err
);
477 g_assert(!qmp
->response
);
478 qmp
->response
= qobject_to(QDict
, obj
);
479 g_assert(qmp
->response
);
482 QDict
*qmp_fd_receive(int fd
)
484 QMPResponseParser qmp
;
485 bool log
= getenv("QTEST_LOG") != NULL
;
488 json_message_parser_init(&qmp
.parser
, qmp_response
, &qmp
, NULL
);
489 while (!qmp
.response
) {
493 len
= read(fd
, &c
, 1);
494 if (len
== -1 && errno
== EINTR
) {
498 if (len
== -1 || len
== 0) {
499 fprintf(stderr
, "Broken pipe\n");
504 len
= write(2, &c
, 1);
506 json_message_parser_feed(&qmp
.parser
, &c
, 1);
508 json_message_parser_destroy(&qmp
.parser
);
513 QDict
*qtest_qmp_receive(QTestState
*s
)
515 return qmp_fd_receive(s
->qmp_fd
);
519 * Allow users to send a message without waiting for the reply,
520 * in the case that they choose to discard all replies up until
521 * a particular EVENT is received.
523 void qmp_fd_vsend(int fd
, const char *fmt
, va_list ap
)
527 /* Going through qobject ensures we escape strings properly */
528 qobj
= qobject_from_vjsonf_nofail(fmt
, ap
);
530 /* No need to send anything for an empty QObject. */
532 int log
= getenv("QTEST_LOG") != NULL
;
533 QString
*qstr
= qobject_to_json(qobj
);
537 * BUG: QMP doesn't react to input until it sees a newline, an
538 * object, or an array. Work-around: give it a newline.
540 qstring_append_chr(qstr
, '\n');
541 str
= qstring_get_str(qstr
);
544 fprintf(stderr
, "%s", str
);
546 /* Send QMP request */
547 socket_send(fd
, str
, qstring_get_length(qstr
));
554 void qtest_qmp_vsend(QTestState
*s
, const char *fmt
, va_list ap
)
556 qmp_fd_vsend(s
->qmp_fd
, fmt
, ap
);
559 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
)
561 qmp_fd_vsend(fd
, fmt
, ap
);
563 return qmp_fd_receive(fd
);
566 QDict
*qtest_vqmp(QTestState
*s
, const char *fmt
, va_list ap
)
568 qtest_qmp_vsend(s
, fmt
, ap
);
571 return qtest_qmp_receive(s
);
574 QDict
*qmp_fd(int fd
, const char *fmt
, ...)
580 response
= qmp_fdv(fd
, fmt
, ap
);
585 void qmp_fd_send(int fd
, const char *fmt
, ...)
590 qmp_fd_vsend(fd
, fmt
, ap
);
594 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
600 response
= qtest_vqmp(s
, fmt
, ap
);
605 void qtest_qmp_send(QTestState
*s
, const char *fmt
, ...)
610 qtest_qmp_vsend(s
, fmt
, ap
);
614 void qmp_fd_vsend_raw(int fd
, const char *fmt
, va_list ap
)
616 bool log
= getenv("QTEST_LOG") != NULL
;
617 char *str
= g_strdup_vprintf(fmt
, ap
);
620 fprintf(stderr
, "%s", str
);
622 socket_send(fd
, str
, strlen(str
));
626 void qmp_fd_send_raw(int fd
, const char *fmt
, ...)
631 qmp_fd_vsend_raw(fd
, fmt
, ap
);
635 void qtest_qmp_send_raw(QTestState
*s
, const char *fmt
, ...)
640 qmp_fd_vsend_raw(s
->qmp_fd
, fmt
, ap
);
644 QDict
*qtest_qmp_eventwait_ref(QTestState
*s
, const char *event
)
649 response
= qtest_qmp_receive(s
);
650 if ((qdict_haskey(response
, "event")) &&
651 (strcmp(qdict_get_str(response
, "event"), event
) == 0)) {
654 qobject_unref(response
);
658 void qtest_qmp_eventwait(QTestState
*s
, const char *event
)
662 response
= qtest_qmp_eventwait_ref(s
, event
);
663 qobject_unref(response
);
666 char *qtest_vhmp(QTestState
*s
, const char *fmt
, va_list ap
)
672 cmd
= g_strdup_vprintf(fmt
, ap
);
673 resp
= qtest_qmp(s
, "{'execute': 'human-monitor-command',"
674 " 'arguments': {'command-line': %s}}",
676 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
677 while (ret
== NULL
&& qdict_get_try_str(resp
, "event")) {
678 /* Ignore asynchronous QMP events */
680 resp
= qtest_qmp_receive(s
);
681 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
689 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...)
695 ret
= qtest_vhmp(s
, fmt
, ap
);
700 const char *qtest_get_arch(void)
702 const char *qemu
= qtest_qemu_binary();
703 const char *end
= strrchr(qemu
, '/');
705 return end
+ strlen("/qemu-system-");
708 bool qtest_get_irq(QTestState
*s
, int num
)
710 /* dummy operation in order to make sure irq is up to date */
713 return s
->irq_level
[num
];
716 static int64_t qtest_clock_rsp(QTestState
*s
)
720 words
= qtest_rsp(s
, 2);
721 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
726 int64_t qtest_clock_step_next(QTestState
*s
)
728 qtest_sendf(s
, "clock_step\n");
729 return qtest_clock_rsp(s
);
732 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
734 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
735 return qtest_clock_rsp(s
);
738 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
740 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
741 return qtest_clock_rsp(s
);
744 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
746 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
750 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
752 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
756 void qtest_set_irq_in(QTestState
*s
, const char *qom_path
, const char *name
,
760 name
= "unnamed-gpio-in";
762 qtest_sendf(s
, "set_irq_in %s %s %d %d\n", qom_path
, name
, num
, level
);
766 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
768 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
772 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
774 qtest_out(s
, "outb", addr
, value
);
777 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
779 qtest_out(s
, "outw", addr
, value
);
782 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
784 qtest_out(s
, "outl", addr
, value
);
787 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
793 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
794 args
= qtest_rsp(s
, 2);
795 ret
= qemu_strtoul(args
[1], NULL
, 0, &value
);
796 g_assert(!ret
&& value
<= UINT32_MAX
);
802 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
804 return qtest_in(s
, "inb", addr
);
807 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
809 return qtest_in(s
, "inw", addr
);
812 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
814 return qtest_in(s
, "inl", addr
);
817 static void qtest_write(QTestState
*s
, const char *cmd
, uint64_t addr
,
820 qtest_sendf(s
, "%s 0x%" PRIx64
" 0x%" PRIx64
"\n", cmd
, addr
, value
);
824 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
)
826 qtest_write(s
, "writeb", addr
, value
);
829 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
)
831 qtest_write(s
, "writew", addr
, value
);
834 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
)
836 qtest_write(s
, "writel", addr
, value
);
839 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
)
841 qtest_write(s
, "writeq", addr
, value
);
844 static uint64_t qtest_read(QTestState
*s
, const char *cmd
, uint64_t addr
)
850 qtest_sendf(s
, "%s 0x%" PRIx64
"\n", cmd
, addr
);
851 args
= qtest_rsp(s
, 2);
852 ret
= qemu_strtou64(args
[1], NULL
, 0, &value
);
859 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
)
861 return qtest_read(s
, "readb", addr
);
864 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
)
866 return qtest_read(s
, "readw", addr
);
869 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
)
871 return qtest_read(s
, "readl", addr
);
874 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
)
876 return qtest_read(s
, "readq", addr
);
879 static int hex2nib(char ch
)
881 if (ch
>= '0' && ch
<= '9') {
883 } else if (ch
>= 'a' && ch
<= 'f') {
884 return 10 + (ch
- 'a');
885 } else if (ch
>= 'A' && ch
<= 'F') {
886 return 10 + (ch
- 'a');
892 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
902 qtest_sendf(s
, "read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
903 args
= qtest_rsp(s
, 2);
905 for (i
= 0; i
< size
; i
++) {
906 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
907 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
913 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
914 uint32_t nargs
, uint64_t args
,
915 uint32_t nret
, uint64_t ret
)
917 qtest_sendf(s
, "rtas %s %u 0x%"PRIx64
" %u 0x%"PRIx64
"\n",
918 name
, nargs
, args
, nret
, ret
);
923 void qtest_add_func(const char *str
, void (*fn
)(void))
925 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
926 g_test_add_func(path
, fn
);
930 void qtest_add_data_func_full(const char *str
, void *data
,
931 void (*fn
)(const void *),
932 GDestroyNotify data_free_func
)
934 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
935 g_test_add_data_func_full(path
, data
, fn
, data_free_func
);
939 void qtest_add_data_func(const char *str
, const void *data
,
940 void (*fn
)(const void *))
942 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
943 g_test_add_data_func(path
, data
, fn
);
947 void qtest_bufwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
951 bdata
= g_base64_encode(data
, size
);
952 qtest_sendf(s
, "b64write 0x%" PRIx64
" 0x%zx ", addr
, size
);
953 socket_send(s
->fd
, bdata
, strlen(bdata
));
954 socket_send(s
->fd
, "\n", 1);
959 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
964 qtest_sendf(s
, "b64read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
965 args
= qtest_rsp(s
, 2);
967 g_base64_decode_inplace(args
[1], &len
);
969 fprintf(stderr
, "bufread: asked for %zu bytes but decoded %zu\n",
971 len
= MIN(len
, size
);
974 memcpy(data
, args
[1], len
);
978 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
980 const uint8_t *ptr
= data
;
988 enc
= g_malloc(2 * size
+ 1);
990 for (i
= 0; i
< size
; i
++) {
991 sprintf(&enc
[i
* 2], "%02x", ptr
[i
]);
994 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x%s\n", addr
, size
, enc
);
999 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t pattern
, size_t size
)
1001 qtest_sendf(s
, "memset 0x%" PRIx64
" 0x%zx 0x%02x\n", addr
, size
, pattern
);
1005 QDict
*qmp(const char *fmt
, ...)
1011 response
= qtest_vqmp(global_qtest
, fmt
, ap
);
1016 void qmp_send(const char *fmt
, ...)
1021 qtest_qmp_vsend(global_qtest
, fmt
, ap
);
1025 char *hmp(const char *fmt
, ...)
1031 ret
= qtest_vhmp(global_qtest
, fmt
, ap
);
1036 bool qtest_big_endian(QTestState
*s
)
1038 return s
->big_endian
;
1041 static bool qtest_check_machine_version(const char *mname
, const char *basename
,
1042 int major
, int minor
)
1047 newname
= g_strdup_printf("%s-%i.%i", basename
, major
, minor
);
1048 is_equal
= g_str_equal(mname
, newname
);
1054 static bool qtest_is_old_versioned_machine(const char *mname
)
1056 const char *dash
= strrchr(mname
, '-');
1057 const char *dot
= strrchr(mname
, '.');
1060 const int major
= QEMU_VERSION_MAJOR
;
1061 const int minor
= QEMU_VERSION_MINOR
;
1064 if (dash
&& dot
&& dot
> dash
) {
1065 for (chr
= dash
+ 1; *chr
; chr
++) {
1066 if (!qemu_isdigit(*chr
) && *chr
!= '.') {
1071 * Now check if it is one of the latest versions. Check major + 1
1072 * and minor + 1 versions as well, since they might already exist
1073 * in the development branch.
1075 bname
= g_strdup(mname
);
1076 bname
[dash
- mname
] = 0;
1077 res
= !qtest_check_machine_version(mname
, bname
, major
+ 1, 0) &&
1078 !qtest_check_machine_version(mname
, bname
, major
, minor
+ 1) &&
1079 !qtest_check_machine_version(mname
, bname
, major
, minor
);
1086 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
),
1087 bool skip_old_versioned
)
1089 QDict
*response
, *minfo
;
1091 const QListEntry
*p
;
1096 qtest_start("-machine none");
1097 response
= qmp("{ 'execute': 'query-machines' }");
1099 list
= qdict_get_qlist(response
, "return");
1102 for (p
= qlist_first(list
); p
; p
= qlist_next(p
)) {
1103 minfo
= qobject_to(QDict
, qlist_entry_obj(p
));
1105 qobj
= qdict_get(minfo
, "name");
1107 qstr
= qobject_to(QString
, qobj
);
1109 mname
= qstring_get_str(qstr
);
1110 if (!skip_old_versioned
|| !qtest_is_old_versioned_machine(mname
)) {
1116 qobject_unref(response
);
1119 QDict
*qtest_qmp_receive_success(QTestState
*s
,
1120 void (*event_cb
)(void *opaque
,
1125 QDict
*response
, *ret
, *data
;
1129 response
= qtest_qmp_receive(s
);
1130 g_assert(!qdict_haskey(response
, "error"));
1131 ret
= qdict_get_qdict(response
, "return");
1135 event
= qdict_get_str(response
, "event");
1136 data
= qdict_get_qdict(response
, "data");
1138 event_cb(opaque
, event
, data
);
1140 qobject_unref(response
);
1144 qobject_unref(response
);
1149 * Generic hot-plugging test via the device_add QMP command.
1151 void qtest_qmp_device_add(const char *driver
, const char *id
,
1152 const char *fmt
, ...)
1154 QDict
*args
, *response
;
1158 args
= qdict_from_vjsonf_nofail(fmt
, ap
);
1161 g_assert(!qdict_haskey(args
, "driver") && !qdict_haskey(args
, "id"));
1162 qdict_put_str(args
, "driver", driver
);
1163 qdict_put_str(args
, "id", id
);
1165 response
= qmp("{'execute': 'device_add', 'arguments': %p}", args
);
1167 g_assert(!qdict_haskey(response
, "event")); /* We don't expect any events */
1168 g_assert(!qdict_haskey(response
, "error"));
1169 qobject_unref(response
);
1172 static void device_deleted_cb(void *opaque
, const char *name
, QDict
*data
)
1174 bool *got_event
= opaque
;
1176 g_assert_cmpstr(name
, ==, "DEVICE_DELETED");
1181 * Generic hot-unplugging test via the device_del QMP command.
1182 * Device deletion will get one response and one event. For example:
1184 * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
1186 * will get this one:
1188 * {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
1189 * "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
1190 * "path": "/machine/peripheral/scsi-hd"}}
1196 * But the order of arrival may vary - so we've got to detect both.
1198 void qtest_qmp_device_del(const char *id
)
1200 bool got_event
= false;
1203 qtest_qmp_send(global_qtest
,
1204 "{'execute': 'device_del', 'arguments': {'id': %s}}",
1206 rsp
= qtest_qmp_receive_success(global_qtest
, device_deleted_cb
,
1210 rsp
= qmp_receive();
1211 g_assert_cmpstr(qdict_get_try_str(rsp
, "event"),
1212 ==, "DEVICE_DELETED");
1217 bool qmp_rsp_is_err(QDict
*rsp
)
1219 QDict
*error
= qdict_get_qdict(rsp
, "error");
1224 void qmp_assert_error_class(QDict
*rsp
, const char *class)
1226 QDict
*error
= qdict_get_qdict(rsp
, "error");
1228 g_assert_cmpstr(qdict_get_try_str(error
, "class"), ==, class);
1229 g_assert_nonnull(qdict_get_try_str(error
, "desc"));
1230 g_assert(!qdict_haskey(rsp
, "return"));