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 #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 TFR(pid
= waitpid(s
->qemu_pid
, &wstatus
, 0));
112 assert(pid
== s
->qemu_pid
);
114 * We expect qemu to exit with status 0; anything else is
115 * fishy and should be logged with as much detail as possible.
118 if (WIFEXITED(wstatus
)) {
119 fprintf(stderr
, "%s:%d: kill_qemu() tried to terminate QEMU "
120 "process but encountered exit status %d\n",
121 __FILE__
, __LINE__
, WEXITSTATUS(wstatus
));
122 } else if (WIFSIGNALED(wstatus
)) {
123 int sig
= WTERMSIG(wstatus
);
124 const char *signame
= strsignal(sig
) ?: "unknown ???";
125 const char *dump
= WCOREDUMP(wstatus
) ? " (core dumped)" : "";
127 fprintf(stderr
, "%s:%d: kill_qemu() detected QEMU death "
128 "from signal %d (%s)%s\n",
129 __FILE__
, __LINE__
, sig
, signame
, dump
);
136 static void kill_qemu_hook_func(void *s
)
141 static void sigabrt_handler(int signo
)
143 g_hook_list_invoke(&abrt_hooks
, FALSE
);
146 static void setup_sigabrt_handler(void)
148 struct sigaction sigact
;
150 /* Catch SIGABRT to clean up on g_assert() failure */
151 sigact
= (struct sigaction
){
152 .sa_handler
= sigabrt_handler
,
153 .sa_flags
= SA_RESETHAND
,
155 sigemptyset(&sigact
.sa_mask
);
156 sigaction(SIGABRT
, &sigact
, &sigact_old
);
159 static void cleanup_sigabrt_handler(void)
161 sigaction(SIGABRT
, &sigact_old
, NULL
);
164 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
)
168 /* Only install SIGABRT handler once */
169 if (!abrt_hooks
.is_setup
) {
170 g_hook_list_init(&abrt_hooks
, sizeof(GHook
));
172 setup_sigabrt_handler();
174 hook
= g_hook_alloc(&abrt_hooks
);
176 hook
->data
= (void *)data
;
178 g_hook_prepend(&abrt_hooks
, hook
);
181 static const char *qtest_qemu_binary(void)
183 const char *qemu_bin
;
185 qemu_bin
= getenv("QTEST_QEMU_BINARY");
187 fprintf(stderr
, "Environment variable QTEST_QEMU_BINARY required\n");
194 QTestState
*qtest_init_without_qmp_handshake(bool use_oob
,
195 const char *extra_args
)
198 int sock
, qmpsock
, i
;
200 gchar
*qmp_socket_path
;
202 const char *qemu_binary
= qtest_qemu_binary();
204 s
= g_new(QTestState
, 1);
206 socket_path
= g_strdup_printf("/tmp/qtest-%d.sock", getpid());
207 qmp_socket_path
= g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
209 /* It's possible that if an earlier test run crashed it might
210 * have left a stale unix socket lying around. Delete any
211 * stale old socket to avoid spurious test failures with
212 * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
215 unlink(qmp_socket_path
);
217 sock
= init_socket(socket_path
);
218 qmpsock
= init_socket(qmp_socket_path
);
220 qtest_add_abrt_handler(kill_qemu_hook_func
, s
);
222 s
->qemu_pid
= fork();
223 if (s
->qemu_pid
== 0) {
224 setenv("QEMU_AUDIO_DRV", "none", true);
225 command
= g_strdup_printf("exec %s "
226 "-qtest unix:%s,nowait "
228 "-chardev socket,path=%s,nowait,id=char0 "
229 "-mon chardev=char0,mode=control%s "
230 "-machine accel=qtest "
232 "%s", qemu_binary
, socket_path
,
233 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
234 qmp_socket_path
, use_oob
? ",x-oob=on" : "",
236 execlp("/bin/sh", "sh", "-c", command
, NULL
);
240 s
->fd
= socket_accept(sock
);
242 s
->qmp_fd
= socket_accept(qmpsock
);
245 unlink(qmp_socket_path
);
247 g_free(qmp_socket_path
);
249 g_assert(s
->fd
>= 0 && s
->qmp_fd
>= 0);
251 s
->rx
= g_string_new("");
252 for (i
= 0; i
< MAX_IRQ
; i
++) {
253 s
->irq_level
[i
] = false;
256 if (getenv("QTEST_STOP")) {
257 kill(s
->qemu_pid
, SIGSTOP
);
260 /* ask endianness of the target */
262 s
->big_endian
= qtest_query_target_endianness(s
);
267 QTestState
*qtest_init(const char *extra_args
)
269 QTestState
*s
= qtest_init_without_qmp_handshake(false, extra_args
);
272 /* Read the QMP greeting and then do the handshake */
273 greeting
= qtest_qmp_receive(s
);
274 qobject_unref(greeting
);
275 qobject_unref(qtest_qmp(s
, "{ 'execute': 'qmp_capabilities' }"));
280 QTestState
*qtest_vinitf(const char *fmt
, va_list ap
)
282 char *args
= g_strdup_vprintf(fmt
, ap
);
285 s
= qtest_init(args
);
290 QTestState
*qtest_initf(const char *fmt
, ...)
296 s
= qtest_vinitf(fmt
, ap
);
301 void qtest_quit(QTestState
*s
)
303 g_hook_destroy_link(&abrt_hooks
, g_hook_find_data(&abrt_hooks
, TRUE
, s
));
305 /* Uninstall SIGABRT handler on last instance */
306 cleanup_sigabrt_handler();
311 g_string_free(s
->rx
, true);
315 static void socket_send(int fd
, const char *buf
, size_t size
)
320 while (offset
< size
) {
323 len
= write(fd
, buf
+ offset
, size
- offset
);
324 if (len
== -1 && errno
== EINTR
) {
328 g_assert_no_errno(len
);
329 g_assert_cmpint(len
, >, 0);
335 static void socket_sendf(int fd
, const char *fmt
, va_list ap
)
337 gchar
*str
= g_strdup_vprintf(fmt
, ap
);
338 size_t size
= strlen(str
);
340 socket_send(fd
, str
, size
);
344 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState
*s
, const char *fmt
, ...)
349 socket_sendf(s
->fd
, fmt
, ap
);
353 static GString
*qtest_recv_line(QTestState
*s
)
359 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
363 len
= read(s
->fd
, buffer
, sizeof(buffer
));
364 if (len
== -1 && errno
== EINTR
) {
368 if (len
== -1 || len
== 0) {
369 fprintf(stderr
, "Broken pipe\n");
373 g_string_append_len(s
->rx
, buffer
, len
);
376 offset
= eol
- s
->rx
->str
;
377 line
= g_string_new_len(s
->rx
->str
, offset
);
378 g_string_erase(s
->rx
, 0, offset
+ 1);
383 static gchar
**qtest_rsp(QTestState
*s
, int expected_args
)
390 line
= qtest_recv_line(s
);
391 words
= g_strsplit(line
->str
, " ", 0);
392 g_string_free(line
, TRUE
);
394 if (strcmp(words
[0], "IRQ") == 0) {
398 g_assert(words
[1] != NULL
);
399 g_assert(words
[2] != NULL
);
401 ret
= qemu_strtol(words
[2], NULL
, 0, &irq
);
403 g_assert_cmpint(irq
, >=, 0);
404 g_assert_cmpint(irq
, <, MAX_IRQ
);
406 if (strcmp(words
[1], "raise") == 0) {
407 s
->irq_level
[irq
] = true;
409 s
->irq_level
[irq
] = false;
416 g_assert(words
[0] != NULL
);
417 g_assert_cmpstr(words
[0], ==, "OK");
420 for (i
= 0; i
< expected_args
; i
++) {
421 g_assert(words
[i
] != NULL
);
430 static int qtest_query_target_endianness(QTestState
*s
)
435 qtest_sendf(s
, "endianness\n");
436 args
= qtest_rsp(s
, 1);
437 g_assert(strcmp(args
[1], "big") == 0 || strcmp(args
[1], "little") == 0);
438 big_endian
= strcmp(args
[1], "big") == 0;
445 JSONMessageParser parser
;
449 static void qmp_response(void *opaque
, QObject
*obj
, Error
*err
)
451 QMPResponseParser
*qmp
= opaque
;
453 assert(!obj
!= !err
);
456 error_prepend(&err
, "QMP JSON response parsing failed: ");
457 error_report_err(err
);
461 g_assert(!qmp
->response
);
462 qmp
->response
= qobject_to(QDict
, obj
);
463 g_assert(qmp
->response
);
466 QDict
*qmp_fd_receive(int fd
)
468 QMPResponseParser qmp
;
469 bool log
= getenv("QTEST_LOG") != NULL
;
472 json_message_parser_init(&qmp
.parser
, qmp_response
, &qmp
, NULL
);
473 while (!qmp
.response
) {
477 len
= read(fd
, &c
, 1);
478 if (len
== -1 && errno
== EINTR
) {
482 if (len
== -1 || len
== 0) {
483 fprintf(stderr
, "Broken pipe\n");
488 len
= write(2, &c
, 1);
490 json_message_parser_feed(&qmp
.parser
, &c
, 1);
492 json_message_parser_destroy(&qmp
.parser
);
497 QDict
*qtest_qmp_receive(QTestState
*s
)
499 return qmp_fd_receive(s
->qmp_fd
);
503 * Allow users to send a message without waiting for the reply,
504 * in the case that they choose to discard all replies up until
505 * a particular EVENT is received.
507 void qmp_fd_vsend(int fd
, const char *fmt
, va_list ap
)
511 /* Going through qobject ensures we escape strings properly */
512 qobj
= qobject_from_vjsonf_nofail(fmt
, ap
);
514 /* No need to send anything for an empty QObject. */
516 int log
= getenv("QTEST_LOG") != NULL
;
517 QString
*qstr
= qobject_to_json(qobj
);
521 * BUG: QMP doesn't react to input until it sees a newline, an
522 * object, or an array. Work-around: give it a newline.
524 qstring_append_chr(qstr
, '\n');
525 str
= qstring_get_str(qstr
);
528 fprintf(stderr
, "%s", str
);
530 /* Send QMP request */
531 socket_send(fd
, str
, qstring_get_length(qstr
));
538 void qtest_qmp_vsend(QTestState
*s
, const char *fmt
, va_list ap
)
540 qmp_fd_vsend(s
->qmp_fd
, fmt
, ap
);
543 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
)
545 qmp_fd_vsend(fd
, fmt
, ap
);
547 return qmp_fd_receive(fd
);
550 QDict
*qtest_vqmp(QTestState
*s
, const char *fmt
, va_list ap
)
552 qtest_qmp_vsend(s
, fmt
, ap
);
555 return qtest_qmp_receive(s
);
558 QDict
*qmp_fd(int fd
, const char *fmt
, ...)
564 response
= qmp_fdv(fd
, fmt
, ap
);
569 void qmp_fd_send(int fd
, const char *fmt
, ...)
574 qmp_fd_vsend(fd
, fmt
, ap
);
578 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
584 response
= qtest_vqmp(s
, fmt
, ap
);
589 void qtest_qmp_send(QTestState
*s
, const char *fmt
, ...)
594 qtest_qmp_vsend(s
, fmt
, ap
);
598 void qmp_fd_vsend_raw(int fd
, const char *fmt
, va_list ap
)
600 bool log
= getenv("QTEST_LOG") != NULL
;
601 char *str
= g_strdup_vprintf(fmt
, ap
);
604 fprintf(stderr
, "%s", str
);
606 socket_send(fd
, str
, strlen(str
));
610 void qmp_fd_send_raw(int fd
, const char *fmt
, ...)
615 qmp_fd_vsend_raw(fd
, fmt
, ap
);
619 void qtest_qmp_send_raw(QTestState
*s
, const char *fmt
, ...)
624 qmp_fd_vsend_raw(s
->qmp_fd
, fmt
, ap
);
628 QDict
*qtest_qmp_eventwait_ref(QTestState
*s
, const char *event
)
633 response
= qtest_qmp_receive(s
);
634 if ((qdict_haskey(response
, "event")) &&
635 (strcmp(qdict_get_str(response
, "event"), event
) == 0)) {
638 qobject_unref(response
);
642 void qtest_qmp_eventwait(QTestState
*s
, const char *event
)
646 response
= qtest_qmp_eventwait_ref(s
, event
);
647 qobject_unref(response
);
650 char *qtest_vhmp(QTestState
*s
, const char *fmt
, va_list ap
)
656 cmd
= g_strdup_vprintf(fmt
, ap
);
657 resp
= qtest_qmp(s
, "{'execute': 'human-monitor-command',"
658 " 'arguments': {'command-line': %s}}",
660 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
661 while (ret
== NULL
&& qdict_get_try_str(resp
, "event")) {
662 /* Ignore asynchronous QMP events */
664 resp
= qtest_qmp_receive(s
);
665 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
673 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...)
679 ret
= qtest_vhmp(s
, fmt
, ap
);
684 const char *qtest_get_arch(void)
686 const char *qemu
= qtest_qemu_binary();
687 const char *end
= strrchr(qemu
, '/');
689 return end
+ strlen("/qemu-system-");
692 bool qtest_get_irq(QTestState
*s
, int num
)
694 /* dummy operation in order to make sure irq is up to date */
697 return s
->irq_level
[num
];
700 static int64_t qtest_clock_rsp(QTestState
*s
)
704 words
= qtest_rsp(s
, 2);
705 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
710 int64_t qtest_clock_step_next(QTestState
*s
)
712 qtest_sendf(s
, "clock_step\n");
713 return qtest_clock_rsp(s
);
716 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
718 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
719 return qtest_clock_rsp(s
);
722 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
724 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
725 return qtest_clock_rsp(s
);
728 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
730 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
734 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
736 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
740 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
742 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
746 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
748 qtest_out(s
, "outb", addr
, value
);
751 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
753 qtest_out(s
, "outw", addr
, value
);
756 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
758 qtest_out(s
, "outl", addr
, value
);
761 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
767 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
768 args
= qtest_rsp(s
, 2);
769 ret
= qemu_strtoul(args
[1], NULL
, 0, &value
);
770 g_assert(!ret
&& value
<= UINT32_MAX
);
776 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
778 return qtest_in(s
, "inb", addr
);
781 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
783 return qtest_in(s
, "inw", addr
);
786 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
788 return qtest_in(s
, "inl", addr
);
791 static void qtest_write(QTestState
*s
, const char *cmd
, uint64_t addr
,
794 qtest_sendf(s
, "%s 0x%" PRIx64
" 0x%" PRIx64
"\n", cmd
, addr
, value
);
798 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
)
800 qtest_write(s
, "writeb", addr
, value
);
803 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
)
805 qtest_write(s
, "writew", addr
, value
);
808 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
)
810 qtest_write(s
, "writel", addr
, value
);
813 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
)
815 qtest_write(s
, "writeq", addr
, value
);
818 static uint64_t qtest_read(QTestState
*s
, const char *cmd
, uint64_t addr
)
824 qtest_sendf(s
, "%s 0x%" PRIx64
"\n", cmd
, addr
);
825 args
= qtest_rsp(s
, 2);
826 ret
= qemu_strtou64(args
[1], NULL
, 0, &value
);
833 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
)
835 return qtest_read(s
, "readb", addr
);
838 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
)
840 return qtest_read(s
, "readw", addr
);
843 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
)
845 return qtest_read(s
, "readl", addr
);
848 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
)
850 return qtest_read(s
, "readq", addr
);
853 static int hex2nib(char ch
)
855 if (ch
>= '0' && ch
<= '9') {
857 } else if (ch
>= 'a' && ch
<= 'f') {
858 return 10 + (ch
- 'a');
859 } else if (ch
>= 'A' && ch
<= 'F') {
860 return 10 + (ch
- 'a');
866 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
876 qtest_sendf(s
, "read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
877 args
= qtest_rsp(s
, 2);
879 for (i
= 0; i
< size
; i
++) {
880 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
881 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
887 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
888 uint32_t nargs
, uint64_t args
,
889 uint32_t nret
, uint64_t ret
)
891 qtest_sendf(s
, "rtas %s %u 0x%"PRIx64
" %u 0x%"PRIx64
"\n",
892 name
, nargs
, args
, nret
, ret
);
897 void qtest_add_func(const char *str
, void (*fn
)(void))
899 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
900 g_test_add_func(path
, fn
);
904 void qtest_add_data_func_full(const char *str
, void *data
,
905 void (*fn
)(const void *),
906 GDestroyNotify data_free_func
)
908 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
909 g_test_add_data_func_full(path
, data
, fn
, data_free_func
);
913 void qtest_add_data_func(const char *str
, const void *data
,
914 void (*fn
)(const void *))
916 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
917 g_test_add_data_func(path
, data
, fn
);
921 void qtest_bufwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
925 bdata
= g_base64_encode(data
, size
);
926 qtest_sendf(s
, "b64write 0x%" PRIx64
" 0x%zx ", addr
, size
);
927 socket_send(s
->fd
, bdata
, strlen(bdata
));
928 socket_send(s
->fd
, "\n", 1);
933 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
938 qtest_sendf(s
, "b64read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
939 args
= qtest_rsp(s
, 2);
941 g_base64_decode_inplace(args
[1], &len
);
943 fprintf(stderr
, "bufread: asked for %zu bytes but decoded %zu\n",
945 len
= MIN(len
, size
);
948 memcpy(data
, args
[1], len
);
952 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
954 const uint8_t *ptr
= data
;
962 enc
= g_malloc(2 * size
+ 1);
964 for (i
= 0; i
< size
; i
++) {
965 sprintf(&enc
[i
* 2], "%02x", ptr
[i
]);
968 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x%s\n", addr
, size
, enc
);
973 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t pattern
, size_t size
)
975 qtest_sendf(s
, "memset 0x%" PRIx64
" 0x%zx 0x%02x\n", addr
, size
, pattern
);
979 QDict
*qmp(const char *fmt
, ...)
985 response
= qtest_vqmp(global_qtest
, fmt
, ap
);
990 void qmp_send(const char *fmt
, ...)
995 qtest_qmp_vsend(global_qtest
, fmt
, ap
);
999 char *hmp(const char *fmt
, ...)
1005 ret
= qtest_vhmp(global_qtest
, fmt
, ap
);
1010 bool qtest_big_endian(QTestState
*s
)
1012 return s
->big_endian
;
1015 static bool qtest_check_machine_version(const char *mname
, const char *basename
,
1016 int major
, int minor
)
1021 newname
= g_strdup_printf("%s-%i.%i", basename
, major
, minor
);
1022 is_equal
= g_str_equal(mname
, newname
);
1028 static bool qtest_is_old_versioned_machine(const char *mname
)
1030 const char *dash
= strrchr(mname
, '-');
1031 const char *dot
= strrchr(mname
, '.');
1034 const int major
= QEMU_VERSION_MAJOR
;
1035 const int minor
= QEMU_VERSION_MINOR
;
1038 if (dash
&& dot
&& dot
> dash
) {
1039 for (chr
= dash
+ 1; *chr
; chr
++) {
1040 if (!qemu_isdigit(*chr
) && *chr
!= '.') {
1045 * Now check if it is one of the latest versions. Check major + 1
1046 * and minor + 1 versions as well, since they might already exist
1047 * in the development branch.
1049 bname
= g_strdup(mname
);
1050 bname
[dash
- mname
] = 0;
1051 res
= !qtest_check_machine_version(mname
, bname
, major
+ 1, 0) &&
1052 !qtest_check_machine_version(mname
, bname
, major
, minor
+ 1) &&
1053 !qtest_check_machine_version(mname
, bname
, major
, minor
);
1060 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
),
1061 bool skip_old_versioned
)
1063 QDict
*response
, *minfo
;
1065 const QListEntry
*p
;
1070 qtest_start("-machine none");
1071 response
= qmp("{ 'execute': 'query-machines' }");
1073 list
= qdict_get_qlist(response
, "return");
1076 for (p
= qlist_first(list
); p
; p
= qlist_next(p
)) {
1077 minfo
= qobject_to(QDict
, qlist_entry_obj(p
));
1079 qobj
= qdict_get(minfo
, "name");
1081 qstr
= qobject_to(QString
, qobj
);
1083 mname
= qstring_get_str(qstr
);
1084 if (!skip_old_versioned
|| !qtest_is_old_versioned_machine(mname
)) {
1090 qobject_unref(response
);
1093 QDict
*qtest_qmp_receive_success(QTestState
*s
,
1094 void (*event_cb
)(void *opaque
,
1099 QDict
*response
, *ret
, *data
;
1103 response
= qtest_qmp_receive(s
);
1104 g_assert(!qdict_haskey(response
, "error"));
1105 ret
= qdict_get_qdict(response
, "return");
1109 event
= qdict_get_str(response
, "event");
1110 data
= qdict_get_qdict(response
, "data");
1112 event_cb(opaque
, event
, data
);
1114 qobject_unref(response
);
1118 qobject_unref(response
);
1123 * Generic hot-plugging test via the device_add QMP command.
1125 void qtest_qmp_device_add(const char *driver
, const char *id
,
1126 const char *fmt
, ...)
1128 QDict
*args
, *response
;
1132 args
= qdict_from_vjsonf_nofail(fmt
, ap
);
1135 g_assert(!qdict_haskey(args
, "driver") && !qdict_haskey(args
, "id"));
1136 qdict_put_str(args
, "driver", driver
);
1137 qdict_put_str(args
, "id", id
);
1139 response
= qmp("{'execute': 'device_add', 'arguments': %p}", args
);
1141 g_assert(!qdict_haskey(response
, "event")); /* We don't expect any events */
1142 g_assert(!qdict_haskey(response
, "error"));
1143 qobject_unref(response
);
1146 static void device_deleted_cb(void *opaque
, const char *name
, QDict
*data
)
1148 bool *got_event
= opaque
;
1150 g_assert_cmpstr(name
, ==, "DEVICE_DELETED");
1155 * Generic hot-unplugging test via the device_del QMP command.
1156 * Device deletion will get one response and one event. For example:
1158 * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
1160 * will get this one:
1162 * {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
1163 * "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
1164 * "path": "/machine/peripheral/scsi-hd"}}
1170 * But the order of arrival may vary - so we've got to detect both.
1172 void qtest_qmp_device_del(const char *id
)
1174 bool got_event
= false;
1177 qtest_qmp_send(global_qtest
,
1178 "{'execute': 'device_del', 'arguments': {'id': %s}}",
1180 rsp
= qtest_qmp_receive_success(global_qtest
, device_deleted_cb
,
1184 rsp
= qmp_receive();
1185 g_assert_cmpstr(qdict_get_try_str(rsp
, "event"),
1186 ==, "DEVICE_DELETED");
1191 bool qmp_rsp_is_err(QDict
*rsp
)
1193 QDict
*error
= qdict_get_qdict(rsp
, "error");
1198 void qmp_assert_error_class(QDict
*rsp
, const char *class)
1200 QDict
*error
= qdict_get_qdict(rsp
, "error");
1202 g_assert_cmpstr(qdict_get_try_str(error
, "class"), ==, class);
1203 g_assert_nonnull(qdict_get_try_str(error
, "desc"));
1204 g_assert(!qdict_haskey(rsp
, "return"));