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 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 static int qtest_query_target_endianness(QTestState
*s
);
53 static int init_socket(const char *socket_path
)
55 struct sockaddr_un addr
;
59 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
60 g_assert_cmpint(sock
, !=, -1);
62 addr
.sun_family
= AF_UNIX
;
63 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socket_path
);
64 qemu_set_cloexec(sock
);
67 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
68 } while (ret
== -1 && errno
== EINTR
);
69 g_assert_cmpint(ret
, !=, -1);
70 ret
= listen(sock
, 1);
71 g_assert_cmpint(ret
, !=, -1);
76 static int socket_accept(int sock
)
78 struct sockaddr_un addr
;
81 struct timeval timeout
= { .tv_sec
= SOCKET_TIMEOUT
,
84 setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, (void *)&timeout
,
88 addrlen
= sizeof(addr
);
89 ret
= accept(sock
, (struct sockaddr
*)&addr
, &addrlen
);
90 } while (ret
== -1 && errno
== EINTR
);
92 fprintf(stderr
, "%s failed: %s\n", __func__
, strerror(errno
));
99 static void kill_qemu(QTestState
*s
)
101 if (s
->qemu_pid
!= -1) {
105 kill(s
->qemu_pid
, SIGTERM
);
106 TFR(pid
= waitpid(s
->qemu_pid
, &wstatus
, 0));
108 assert(pid
== s
->qemu_pid
);
110 * We expect qemu to exit with status 0; anything else is
111 * fishy and should be logged with as much detail as possible.
114 if (WIFEXITED(wstatus
)) {
115 fprintf(stderr
, "%s:%d: kill_qemu() tried to terminate QEMU "
116 "process but encountered exit status %d\n",
117 __FILE__
, __LINE__
, WEXITSTATUS(wstatus
));
118 } else if (WIFSIGNALED(wstatus
)) {
119 int sig
= WTERMSIG(wstatus
);
120 const char *signame
= strsignal(sig
) ?: "unknown ???";
121 const char *dump
= WCOREDUMP(wstatus
) ? " (core dumped)" : "";
123 fprintf(stderr
, "%s:%d: kill_qemu() detected QEMU death "
124 "from signal %d (%s)%s\n",
125 __FILE__
, __LINE__
, sig
, signame
, dump
);
132 static void kill_qemu_hook_func(void *s
)
137 static void sigabrt_handler(int signo
)
139 g_hook_list_invoke(&abrt_hooks
, FALSE
);
142 static void setup_sigabrt_handler(void)
144 struct sigaction sigact
;
146 /* Catch SIGABRT to clean up on g_assert() failure */
147 sigact
= (struct sigaction
){
148 .sa_handler
= sigabrt_handler
,
149 .sa_flags
= SA_RESETHAND
,
151 sigemptyset(&sigact
.sa_mask
);
152 sigaction(SIGABRT
, &sigact
, &sigact_old
);
155 static void cleanup_sigabrt_handler(void)
157 sigaction(SIGABRT
, &sigact_old
, NULL
);
160 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
)
164 /* Only install SIGABRT handler once */
165 if (!abrt_hooks
.is_setup
) {
166 g_hook_list_init(&abrt_hooks
, sizeof(GHook
));
168 setup_sigabrt_handler();
170 hook
= g_hook_alloc(&abrt_hooks
);
172 hook
->data
= (void *)data
;
174 g_hook_prepend(&abrt_hooks
, hook
);
177 static const char *qtest_qemu_binary(void)
179 const char *qemu_bin
;
181 qemu_bin
= getenv("QTEST_QEMU_BINARY");
183 fprintf(stderr
, "Environment variable QTEST_QEMU_BINARY required\n");
190 QTestState
*qtest_init_without_qmp_handshake(const char *extra_args
)
193 int sock
, qmpsock
, i
;
195 gchar
*qmp_socket_path
;
197 const char *qemu_binary
= qtest_qemu_binary();
199 s
= g_new(QTestState
, 1);
201 socket_path
= g_strdup_printf("/tmp/qtest-%d.sock", getpid());
202 qmp_socket_path
= g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
204 /* It's possible that if an earlier test run crashed it might
205 * have left a stale unix socket lying around. Delete any
206 * stale old socket to avoid spurious test failures with
207 * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
210 unlink(qmp_socket_path
);
212 sock
= init_socket(socket_path
);
213 qmpsock
= init_socket(qmp_socket_path
);
215 qtest_add_abrt_handler(kill_qemu_hook_func
, s
);
217 command
= g_strdup_printf("exec %s "
218 "-qtest unix:%s,nowait "
220 "-chardev socket,path=%s,nowait,id=char0 "
221 "-mon chardev=char0,mode=control "
222 "-machine accel=qtest "
224 "%s", qemu_binary
, socket_path
,
225 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
229 g_test_message("starting QEMU: %s", command
);
231 s
->qemu_pid
= fork();
232 if (s
->qemu_pid
== 0) {
233 setenv("QEMU_AUDIO_DRV", "none", true);
234 execlp("/bin/sh", "sh", "-c", command
, NULL
);
239 s
->fd
= socket_accept(sock
);
241 s
->qmp_fd
= socket_accept(qmpsock
);
244 unlink(qmp_socket_path
);
246 g_free(qmp_socket_path
);
248 g_assert(s
->fd
>= 0 && s
->qmp_fd
>= 0);
250 s
->rx
= g_string_new("");
251 for (i
= 0; i
< MAX_IRQ
; i
++) {
252 s
->irq_level
[i
] = false;
255 if (getenv("QTEST_STOP")) {
256 kill(s
->qemu_pid
, SIGSTOP
);
259 /* ask endianness of the target */
261 s
->big_endian
= qtest_query_target_endianness(s
);
266 QTestState
*qtest_init(const char *extra_args
)
268 QTestState
*s
= qtest_init_without_qmp_handshake(extra_args
);
271 /* Read the QMP greeting and then do the handshake */
272 greeting
= qtest_qmp_receive(s
);
273 qobject_unref(greeting
);
274 qobject_unref(qtest_qmp(s
, "{ 'execute': 'qmp_capabilities' }"));
279 QTestState
*qtest_vinitf(const char *fmt
, va_list ap
)
281 char *args
= g_strdup_vprintf(fmt
, ap
);
284 s
= qtest_init(args
);
289 QTestState
*qtest_initf(const char *fmt
, ...)
295 s
= qtest_vinitf(fmt
, ap
);
300 void qtest_quit(QTestState
*s
)
302 g_hook_destroy_link(&abrt_hooks
, g_hook_find_data(&abrt_hooks
, TRUE
, s
));
304 /* Uninstall SIGABRT handler on last instance */
305 cleanup_sigabrt_handler();
310 g_string_free(s
->rx
, true);
314 static void socket_send(int fd
, const char *buf
, size_t size
)
319 while (offset
< size
) {
322 len
= write(fd
, buf
+ offset
, size
- offset
);
323 if (len
== -1 && errno
== EINTR
) {
327 g_assert_cmpint(len
, >, 0);
333 static void socket_sendf(int fd
, const char *fmt
, va_list ap
)
335 gchar
*str
= g_strdup_vprintf(fmt
, ap
);
336 size_t size
= strlen(str
);
338 socket_send(fd
, str
, size
);
342 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState
*s
, const char *fmt
, ...)
347 socket_sendf(s
->fd
, fmt
, ap
);
351 static GString
*qtest_recv_line(QTestState
*s
)
357 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
361 len
= read(s
->fd
, buffer
, sizeof(buffer
));
362 if (len
== -1 && errno
== EINTR
) {
366 if (len
== -1 || len
== 0) {
367 fprintf(stderr
, "Broken pipe\n");
371 g_string_append_len(s
->rx
, buffer
, len
);
374 offset
= eol
- s
->rx
->str
;
375 line
= g_string_new_len(s
->rx
->str
, offset
);
376 g_string_erase(s
->rx
, 0, offset
+ 1);
381 static gchar
**qtest_rsp(QTestState
*s
, int expected_args
)
388 line
= qtest_recv_line(s
);
389 words
= g_strsplit(line
->str
, " ", 0);
390 g_string_free(line
, TRUE
);
392 if (strcmp(words
[0], "IRQ") == 0) {
396 g_assert(words
[1] != NULL
);
397 g_assert(words
[2] != NULL
);
399 ret
= qemu_strtol(words
[2], NULL
, 0, &irq
);
401 g_assert_cmpint(irq
, >=, 0);
402 g_assert_cmpint(irq
, <, MAX_IRQ
);
404 if (strcmp(words
[1], "raise") == 0) {
405 s
->irq_level
[irq
] = true;
407 s
->irq_level
[irq
] = false;
414 g_assert(words
[0] != NULL
);
415 g_assert_cmpstr(words
[0], ==, "OK");
418 for (i
= 0; i
< expected_args
; i
++) {
419 g_assert(words
[i
] != NULL
);
428 static int qtest_query_target_endianness(QTestState
*s
)
433 qtest_sendf(s
, "endianness\n");
434 args
= qtest_rsp(s
, 1);
435 g_assert(strcmp(args
[1], "big") == 0 || strcmp(args
[1], "little") == 0);
436 big_endian
= strcmp(args
[1], "big") == 0;
443 JSONMessageParser parser
;
447 static void qmp_response(void *opaque
, QObject
*obj
, Error
*err
)
449 QMPResponseParser
*qmp
= opaque
;
451 assert(!obj
!= !err
);
454 error_prepend(&err
, "QMP JSON response parsing failed: ");
455 error_report_err(err
);
459 g_assert(!qmp
->response
);
460 qmp
->response
= qobject_to(QDict
, obj
);
461 g_assert(qmp
->response
);
464 QDict
*qmp_fd_receive(int fd
)
466 QMPResponseParser qmp
;
467 bool log
= getenv("QTEST_LOG") != NULL
;
470 json_message_parser_init(&qmp
.parser
, qmp_response
, &qmp
, NULL
);
471 while (!qmp
.response
) {
475 len
= read(fd
, &c
, 1);
476 if (len
== -1 && errno
== EINTR
) {
480 if (len
== -1 || len
== 0) {
481 fprintf(stderr
, "Broken pipe\n");
486 len
= write(2, &c
, 1);
488 json_message_parser_feed(&qmp
.parser
, &c
, 1);
490 json_message_parser_destroy(&qmp
.parser
);
495 QDict
*qtest_qmp_receive(QTestState
*s
)
497 return qmp_fd_receive(s
->qmp_fd
);
501 * Allow users to send a message without waiting for the reply,
502 * in the case that they choose to discard all replies up until
503 * a particular EVENT is received.
505 void qmp_fd_vsend(int fd
, const char *fmt
, va_list ap
)
509 /* Going through qobject ensures we escape strings properly */
510 qobj
= qobject_from_vjsonf_nofail(fmt
, ap
);
512 /* No need to send anything for an empty QObject. */
514 int log
= getenv("QTEST_LOG") != NULL
;
515 QString
*qstr
= qobject_to_json(qobj
);
519 * BUG: QMP doesn't react to input until it sees a newline, an
520 * object, or an array. Work-around: give it a newline.
522 qstring_append_chr(qstr
, '\n');
523 str
= qstring_get_str(qstr
);
526 fprintf(stderr
, "%s", str
);
528 /* Send QMP request */
529 socket_send(fd
, str
, qstring_get_length(qstr
));
536 void qtest_qmp_vsend(QTestState
*s
, const char *fmt
, va_list ap
)
538 qmp_fd_vsend(s
->qmp_fd
, fmt
, ap
);
541 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
)
543 qmp_fd_vsend(fd
, fmt
, ap
);
545 return qmp_fd_receive(fd
);
548 QDict
*qtest_vqmp(QTestState
*s
, const char *fmt
, va_list ap
)
550 qtest_qmp_vsend(s
, fmt
, ap
);
553 return qtest_qmp_receive(s
);
556 QDict
*qmp_fd(int fd
, const char *fmt
, ...)
562 response
= qmp_fdv(fd
, fmt
, ap
);
567 void qmp_fd_send(int fd
, const char *fmt
, ...)
572 qmp_fd_vsend(fd
, fmt
, ap
);
576 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
582 response
= qtest_vqmp(s
, fmt
, ap
);
587 void qtest_qmp_send(QTestState
*s
, const char *fmt
, ...)
592 qtest_qmp_vsend(s
, fmt
, ap
);
596 void qmp_fd_vsend_raw(int fd
, const char *fmt
, va_list ap
)
598 bool log
= getenv("QTEST_LOG") != NULL
;
599 char *str
= g_strdup_vprintf(fmt
, ap
);
602 fprintf(stderr
, "%s", str
);
604 socket_send(fd
, str
, strlen(str
));
608 void qmp_fd_send_raw(int fd
, const char *fmt
, ...)
613 qmp_fd_vsend_raw(fd
, fmt
, ap
);
617 void qtest_qmp_send_raw(QTestState
*s
, const char *fmt
, ...)
622 qmp_fd_vsend_raw(s
->qmp_fd
, fmt
, ap
);
626 QDict
*qtest_qmp_eventwait_ref(QTestState
*s
, const char *event
)
631 response
= qtest_qmp_receive(s
);
632 if ((qdict_haskey(response
, "event")) &&
633 (strcmp(qdict_get_str(response
, "event"), event
) == 0)) {
636 qobject_unref(response
);
640 void qtest_qmp_eventwait(QTestState
*s
, const char *event
)
644 response
= qtest_qmp_eventwait_ref(s
, event
);
645 qobject_unref(response
);
648 char *qtest_vhmp(QTestState
*s
, const char *fmt
, va_list ap
)
654 cmd
= g_strdup_vprintf(fmt
, ap
);
655 resp
= qtest_qmp(s
, "{'execute': 'human-monitor-command',"
656 " 'arguments': {'command-line': %s}}",
658 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
659 while (ret
== NULL
&& qdict_get_try_str(resp
, "event")) {
660 /* Ignore asynchronous QMP events */
662 resp
= qtest_qmp_receive(s
);
663 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
671 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...)
677 ret
= qtest_vhmp(s
, fmt
, ap
);
682 const char *qtest_get_arch(void)
684 const char *qemu
= qtest_qemu_binary();
685 const char *end
= strrchr(qemu
, '/');
687 return end
+ strlen("/qemu-system-");
690 bool qtest_get_irq(QTestState
*s
, int num
)
692 /* dummy operation in order to make sure irq is up to date */
695 return s
->irq_level
[num
];
698 static int64_t qtest_clock_rsp(QTestState
*s
)
702 words
= qtest_rsp(s
, 2);
703 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
708 int64_t qtest_clock_step_next(QTestState
*s
)
710 qtest_sendf(s
, "clock_step\n");
711 return qtest_clock_rsp(s
);
714 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
716 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
717 return qtest_clock_rsp(s
);
720 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
722 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
723 return qtest_clock_rsp(s
);
726 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
728 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
732 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
734 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
738 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
740 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
744 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
746 qtest_out(s
, "outb", addr
, value
);
749 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
751 qtest_out(s
, "outw", addr
, value
);
754 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
756 qtest_out(s
, "outl", addr
, value
);
759 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
765 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
766 args
= qtest_rsp(s
, 2);
767 ret
= qemu_strtoul(args
[1], NULL
, 0, &value
);
768 g_assert(!ret
&& value
<= UINT32_MAX
);
774 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
776 return qtest_in(s
, "inb", addr
);
779 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
781 return qtest_in(s
, "inw", addr
);
784 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
786 return qtest_in(s
, "inl", addr
);
789 static void qtest_write(QTestState
*s
, const char *cmd
, uint64_t addr
,
792 qtest_sendf(s
, "%s 0x%" PRIx64
" 0x%" PRIx64
"\n", cmd
, addr
, value
);
796 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
)
798 qtest_write(s
, "writeb", addr
, value
);
801 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
)
803 qtest_write(s
, "writew", addr
, value
);
806 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
)
808 qtest_write(s
, "writel", addr
, value
);
811 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
)
813 qtest_write(s
, "writeq", addr
, value
);
816 static uint64_t qtest_read(QTestState
*s
, const char *cmd
, uint64_t addr
)
822 qtest_sendf(s
, "%s 0x%" PRIx64
"\n", cmd
, addr
);
823 args
= qtest_rsp(s
, 2);
824 ret
= qemu_strtou64(args
[1], NULL
, 0, &value
);
831 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
)
833 return qtest_read(s
, "readb", addr
);
836 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
)
838 return qtest_read(s
, "readw", addr
);
841 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
)
843 return qtest_read(s
, "readl", addr
);
846 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
)
848 return qtest_read(s
, "readq", addr
);
851 static int hex2nib(char ch
)
853 if (ch
>= '0' && ch
<= '9') {
855 } else if (ch
>= 'a' && ch
<= 'f') {
856 return 10 + (ch
- 'a');
857 } else if (ch
>= 'A' && ch
<= 'F') {
858 return 10 + (ch
- 'a');
864 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
874 qtest_sendf(s
, "read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
875 args
= qtest_rsp(s
, 2);
877 for (i
= 0; i
< size
; i
++) {
878 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
879 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
885 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
886 uint32_t nargs
, uint64_t args
,
887 uint32_t nret
, uint64_t ret
)
889 qtest_sendf(s
, "rtas %s %u 0x%"PRIx64
" %u 0x%"PRIx64
"\n",
890 name
, nargs
, args
, nret
, ret
);
895 void qtest_add_func(const char *str
, void (*fn
)(void))
897 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
898 g_test_add_func(path
, fn
);
902 void qtest_add_data_func_full(const char *str
, void *data
,
903 void (*fn
)(const void *),
904 GDestroyNotify data_free_func
)
906 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
907 g_test_add_data_func_full(path
, data
, fn
, data_free_func
);
911 void qtest_add_data_func(const char *str
, const void *data
,
912 void (*fn
)(const void *))
914 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
915 g_test_add_data_func(path
, data
, fn
);
919 void qtest_bufwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
923 bdata
= g_base64_encode(data
, size
);
924 qtest_sendf(s
, "b64write 0x%" PRIx64
" 0x%zx ", addr
, size
);
925 socket_send(s
->fd
, bdata
, strlen(bdata
));
926 socket_send(s
->fd
, "\n", 1);
931 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
936 qtest_sendf(s
, "b64read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
937 args
= qtest_rsp(s
, 2);
939 g_base64_decode_inplace(args
[1], &len
);
941 fprintf(stderr
, "bufread: asked for %zu bytes but decoded %zu\n",
943 len
= MIN(len
, size
);
946 memcpy(data
, args
[1], len
);
950 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
952 const uint8_t *ptr
= data
;
960 enc
= g_malloc(2 * size
+ 1);
962 for (i
= 0; i
< size
; i
++) {
963 sprintf(&enc
[i
* 2], "%02x", ptr
[i
]);
966 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x%s\n", addr
, size
, enc
);
971 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t pattern
, size_t size
)
973 qtest_sendf(s
, "memset 0x%" PRIx64
" 0x%zx 0x%02x\n", addr
, size
, pattern
);
977 QDict
*qmp(const char *fmt
, ...)
983 response
= qtest_vqmp(global_qtest
, fmt
, ap
);
988 void qmp_send(const char *fmt
, ...)
993 qtest_qmp_vsend(global_qtest
, fmt
, ap
);
997 char *hmp(const char *fmt
, ...)
1003 ret
= qtest_vhmp(global_qtest
, fmt
, ap
);
1008 bool qtest_big_endian(QTestState
*s
)
1010 return s
->big_endian
;
1013 static bool qtest_check_machine_version(const char *mname
, const char *basename
,
1014 int major
, int minor
)
1019 newname
= g_strdup_printf("%s-%i.%i", basename
, major
, minor
);
1020 is_equal
= g_str_equal(mname
, newname
);
1026 static bool qtest_is_old_versioned_machine(const char *mname
)
1028 const char *dash
= strrchr(mname
, '-');
1029 const char *dot
= strrchr(mname
, '.');
1032 const int major
= QEMU_VERSION_MAJOR
;
1033 const int minor
= QEMU_VERSION_MINOR
;
1036 if (dash
&& dot
&& dot
> dash
) {
1037 for (chr
= dash
+ 1; *chr
; chr
++) {
1038 if (!qemu_isdigit(*chr
) && *chr
!= '.') {
1043 * Now check if it is one of the latest versions. Check major + 1
1044 * and minor + 1 versions as well, since they might already exist
1045 * in the development branch.
1047 bname
= g_strdup(mname
);
1048 bname
[dash
- mname
] = 0;
1049 res
= !qtest_check_machine_version(mname
, bname
, major
+ 1, 0) &&
1050 !qtest_check_machine_version(mname
, bname
, major
, minor
+ 1) &&
1051 !qtest_check_machine_version(mname
, bname
, major
, minor
);
1058 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
),
1059 bool skip_old_versioned
)
1061 QDict
*response
, *minfo
;
1063 const QListEntry
*p
;
1068 qtest_start("-machine none");
1069 response
= qmp("{ 'execute': 'query-machines' }");
1071 list
= qdict_get_qlist(response
, "return");
1074 for (p
= qlist_first(list
); p
; p
= qlist_next(p
)) {
1075 minfo
= qobject_to(QDict
, qlist_entry_obj(p
));
1077 qobj
= qdict_get(minfo
, "name");
1079 qstr
= qobject_to(QString
, qobj
);
1081 mname
= qstring_get_str(qstr
);
1082 if (!skip_old_versioned
|| !qtest_is_old_versioned_machine(mname
)) {
1088 qobject_unref(response
);
1091 QDict
*qtest_qmp_receive_success(QTestState
*s
,
1092 void (*event_cb
)(void *opaque
,
1097 QDict
*response
, *ret
, *data
;
1101 response
= qtest_qmp_receive(s
);
1102 g_assert(!qdict_haskey(response
, "error"));
1103 ret
= qdict_get_qdict(response
, "return");
1107 event
= qdict_get_str(response
, "event");
1108 data
= qdict_get_qdict(response
, "data");
1110 event_cb(opaque
, event
, data
);
1112 qobject_unref(response
);
1116 qobject_unref(response
);
1121 * Generic hot-plugging test via the device_add QMP command.
1123 void qtest_qmp_device_add(const char *driver
, const char *id
,
1124 const char *fmt
, ...)
1126 QDict
*args
, *response
;
1130 args
= qdict_from_vjsonf_nofail(fmt
, ap
);
1133 g_assert(!qdict_haskey(args
, "driver") && !qdict_haskey(args
, "id"));
1134 qdict_put_str(args
, "driver", driver
);
1135 qdict_put_str(args
, "id", id
);
1137 response
= qmp("{'execute': 'device_add', 'arguments': %p}", args
);
1139 g_assert(!qdict_haskey(response
, "event")); /* We don't expect any events */
1140 g_assert(!qdict_haskey(response
, "error"));
1141 qobject_unref(response
);
1144 static void device_deleted_cb(void *opaque
, const char *name
, QDict
*data
)
1146 bool *got_event
= opaque
;
1148 g_assert_cmpstr(name
, ==, "DEVICE_DELETED");
1153 * Generic hot-unplugging test via the device_del QMP command.
1154 * Device deletion will get one response and one event. For example:
1156 * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
1158 * will get this one:
1160 * {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
1161 * "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
1162 * "path": "/machine/peripheral/scsi-hd"}}
1168 * But the order of arrival may vary - so we've got to detect both.
1170 void qtest_qmp_device_del(const char *id
)
1172 bool got_event
= false;
1175 qtest_qmp_send(global_qtest
,
1176 "{'execute': 'device_del', 'arguments': {'id': %s}}",
1178 rsp
= qtest_qmp_receive_success(global_qtest
, device_deleted_cb
,
1182 rsp
= qmp_receive();
1183 g_assert_cmpstr(qdict_get_try_str(rsp
, "event"),
1184 ==, "DEVICE_DELETED");
1189 bool qmp_rsp_is_err(QDict
*rsp
)
1191 QDict
*error
= qdict_get_qdict(rsp
, "error");
1196 void qmp_assert_error_class(QDict
*rsp
, const char *class)
1198 QDict
*error
= qdict_get_qdict(rsp
, "error");
1200 g_assert_cmpstr(qdict_get_try_str(error
, "class"), ==, class);
1201 g_assert_nonnull(qdict_get_try_str(error
, "desc"));
1202 g_assert(!qdict_haskey(rsp
, "return"));