xics/kvm: synchonize state before 'info pic'
[qemu.git] / tests / libqtest.c
blob0ec8af2923717270736074b95417b4bcdabec6b8
1 /*
2 * QTest
4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
8 * Authors:
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"
18 #include "libqtest.h"
20 #include <sys/socket.h>
21 #include <sys/wait.h>
22 #include <sys/un.h>
24 #include "qapi/error.h"
25 #include "qapi/qmp/json-parser.h"
26 #include "qapi/qmp/json-streamer.h"
27 #include "qapi/qmp/qjson.h"
29 #define MAX_IRQ 256
30 #define SOCKET_TIMEOUT 50
32 QTestState *global_qtest;
34 struct QTestState
36 int fd;
37 int qmp_fd;
38 bool irq_level[MAX_IRQ];
39 GString *rx;
40 pid_t qemu_pid; /* our child QEMU process */
41 bool big_endian;
44 static GHookList abrt_hooks;
45 static struct sigaction sigact_old;
47 #define g_assert_no_errno(ret) do { \
48 g_assert_cmpint(ret, !=, -1); \
49 } while (0)
51 static int qtest_query_target_endianness(QTestState *s);
53 static int init_socket(const char *socket_path)
55 struct sockaddr_un addr;
56 int sock;
57 int ret;
59 sock = socket(PF_UNIX, SOCK_STREAM, 0);
60 g_assert_no_errno(sock);
62 addr.sun_family = AF_UNIX;
63 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
64 qemu_set_cloexec(sock);
66 do {
67 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
68 } while (ret == -1 && errno == EINTR);
69 g_assert_no_errno(ret);
70 ret = listen(sock, 1);
71 g_assert_no_errno(ret);
73 return sock;
76 static int socket_accept(int sock)
78 struct sockaddr_un addr;
79 socklen_t addrlen;
80 int ret;
81 struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT,
82 .tv_usec = 0 };
84 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout,
85 sizeof(timeout));
87 do {
88 addrlen = sizeof(addr);
89 ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
90 } while (ret == -1 && errno == EINTR);
91 if (ret == -1) {
92 fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
94 close(sock);
96 return ret;
99 static void kill_qemu(QTestState *s)
101 if (s->qemu_pid != -1) {
102 kill(s->qemu_pid, SIGTERM);
103 waitpid(s->qemu_pid, NULL, 0);
107 static void kill_qemu_hook_func(void *s)
109 kill_qemu(s);
112 static void sigabrt_handler(int signo)
114 g_hook_list_invoke(&abrt_hooks, FALSE);
117 static void setup_sigabrt_handler(void)
119 struct sigaction sigact;
121 /* Catch SIGABRT to clean up on g_assert() failure */
122 sigact = (struct sigaction){
123 .sa_handler = sigabrt_handler,
124 .sa_flags = SA_RESETHAND,
126 sigemptyset(&sigact.sa_mask);
127 sigaction(SIGABRT, &sigact, &sigact_old);
130 static void cleanup_sigabrt_handler(void)
132 sigaction(SIGABRT, &sigact_old, NULL);
135 void qtest_add_abrt_handler(GHookFunc fn, const void *data)
137 GHook *hook;
139 /* Only install SIGABRT handler once */
140 if (!abrt_hooks.is_setup) {
141 g_hook_list_init(&abrt_hooks, sizeof(GHook));
143 setup_sigabrt_handler();
145 hook = g_hook_alloc(&abrt_hooks);
146 hook->func = fn;
147 hook->data = (void *)data;
149 g_hook_prepend(&abrt_hooks, hook);
152 static const char *qtest_qemu_binary(void)
154 const char *qemu_bin;
156 qemu_bin = getenv("QTEST_QEMU_BINARY");
157 if (!qemu_bin) {
158 fprintf(stderr, "Environment variable QTEST_QEMU_BINARY required\n");
159 exit(1);
162 return qemu_bin;
165 QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
167 QTestState *s;
168 int sock, qmpsock, i;
169 gchar *socket_path;
170 gchar *qmp_socket_path;
171 gchar *command;
172 const char *qemu_binary = qtest_qemu_binary();
174 s = g_new(QTestState, 1);
176 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
177 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
179 /* It's possible that if an earlier test run crashed it might
180 * have left a stale unix socket lying around. Delete any
181 * stale old socket to avoid spurious test failures with
182 * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
184 unlink(socket_path);
185 unlink(qmp_socket_path);
187 sock = init_socket(socket_path);
188 qmpsock = init_socket(qmp_socket_path);
190 qtest_add_abrt_handler(kill_qemu_hook_func, s);
192 s->qemu_pid = fork();
193 if (s->qemu_pid == 0) {
194 setenv("QEMU_AUDIO_DRV", "none", true);
195 command = g_strdup_printf("exec %s "
196 "-qtest unix:%s,nowait "
197 "-qtest-log %s "
198 "-qmp unix:%s,nowait "
199 "-machine accel=qtest "
200 "-display none "
201 "%s", qemu_binary, socket_path,
202 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
203 qmp_socket_path,
204 extra_args ?: "");
205 execlp("/bin/sh", "sh", "-c", command, NULL);
206 exit(1);
209 s->fd = socket_accept(sock);
210 if (s->fd >= 0) {
211 s->qmp_fd = socket_accept(qmpsock);
213 unlink(socket_path);
214 unlink(qmp_socket_path);
215 g_free(socket_path);
216 g_free(qmp_socket_path);
218 g_assert(s->fd >= 0 && s->qmp_fd >= 0);
220 s->rx = g_string_new("");
221 for (i = 0; i < MAX_IRQ; i++) {
222 s->irq_level[i] = false;
225 if (getenv("QTEST_STOP")) {
226 kill(s->qemu_pid, SIGSTOP);
229 /* ask endianness of the target */
231 s->big_endian = qtest_query_target_endianness(s);
233 return s;
236 QTestState *qtest_init(const char *extra_args)
238 QTestState *s = qtest_init_without_qmp_handshake(extra_args);
240 /* Read the QMP greeting and then do the handshake */
241 qtest_qmp_discard_response(s, "");
242 qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
244 return s;
247 QTestState *qtest_vstartf(const char *fmt, va_list ap)
249 char *args = g_strdup_vprintf(fmt, ap);
250 QTestState *s;
252 s = qtest_start(args);
253 g_free(args);
254 global_qtest = NULL;
255 return s;
258 QTestState *qtest_startf(const char *fmt, ...)
260 va_list ap;
261 QTestState *s;
263 va_start(ap, fmt);
264 s = qtest_vstartf(fmt, ap);
265 va_end(ap);
266 return s;
269 void qtest_quit(QTestState *s)
271 g_hook_destroy_link(&abrt_hooks, g_hook_find_data(&abrt_hooks, TRUE, s));
273 /* Uninstall SIGABRT handler on last instance */
274 cleanup_sigabrt_handler();
276 kill_qemu(s);
277 close(s->fd);
278 close(s->qmp_fd);
279 g_string_free(s->rx, true);
280 g_free(s);
283 static void socket_send(int fd, const char *buf, size_t size)
285 size_t offset;
287 offset = 0;
288 while (offset < size) {
289 ssize_t len;
291 len = write(fd, buf + offset, size - offset);
292 if (len == -1 && errno == EINTR) {
293 continue;
296 g_assert_no_errno(len);
297 g_assert_cmpint(len, >, 0);
299 offset += len;
303 static void socket_sendf(int fd, const char *fmt, va_list ap)
305 gchar *str = g_strdup_vprintf(fmt, ap);
306 size_t size = strlen(str);
308 socket_send(fd, str, size);
309 g_free(str);
312 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
314 va_list ap;
316 va_start(ap, fmt);
317 socket_sendf(s->fd, fmt, ap);
318 va_end(ap);
321 static GString *qtest_recv_line(QTestState *s)
323 GString *line;
324 size_t offset;
325 char *eol;
327 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
328 ssize_t len;
329 char buffer[1024];
331 len = read(s->fd, buffer, sizeof(buffer));
332 if (len == -1 && errno == EINTR) {
333 continue;
336 if (len == -1 || len == 0) {
337 fprintf(stderr, "Broken pipe\n");
338 exit(1);
341 g_string_append_len(s->rx, buffer, len);
344 offset = eol - s->rx->str;
345 line = g_string_new_len(s->rx->str, offset);
346 g_string_erase(s->rx, 0, offset + 1);
348 return line;
351 static gchar **qtest_rsp(QTestState *s, int expected_args)
353 GString *line;
354 gchar **words;
355 int i;
357 redo:
358 line = qtest_recv_line(s);
359 words = g_strsplit(line->str, " ", 0);
360 g_string_free(line, TRUE);
362 if (strcmp(words[0], "IRQ") == 0) {
363 int irq;
365 g_assert(words[1] != NULL);
366 g_assert(words[2] != NULL);
368 irq = strtoul(words[2], NULL, 0);
369 g_assert_cmpint(irq, >=, 0);
370 g_assert_cmpint(irq, <, MAX_IRQ);
372 if (strcmp(words[1], "raise") == 0) {
373 s->irq_level[irq] = true;
374 } else {
375 s->irq_level[irq] = false;
378 g_strfreev(words);
379 goto redo;
382 g_assert(words[0] != NULL);
383 g_assert_cmpstr(words[0], ==, "OK");
385 if (expected_args) {
386 for (i = 0; i < expected_args; i++) {
387 g_assert(words[i] != NULL);
389 } else {
390 g_strfreev(words);
393 return words;
396 static int qtest_query_target_endianness(QTestState *s)
398 gchar **args;
399 int big_endian;
401 qtest_sendf(s, "endianness\n");
402 args = qtest_rsp(s, 1);
403 g_assert(strcmp(args[1], "big") == 0 || strcmp(args[1], "little") == 0);
404 big_endian = strcmp(args[1], "big") == 0;
405 g_strfreev(args);
407 return big_endian;
410 typedef struct {
411 JSONMessageParser parser;
412 QDict *response;
413 } QMPResponseParser;
415 static void qmp_response(JSONMessageParser *parser, GQueue *tokens)
417 QMPResponseParser *qmp = container_of(parser, QMPResponseParser, parser);
418 QObject *obj;
420 obj = json_parser_parse(tokens, NULL);
421 if (!obj) {
422 fprintf(stderr, "QMP JSON response parsing failed\n");
423 exit(1);
426 g_assert(!qmp->response);
427 qmp->response = qobject_to_qdict(obj);
428 g_assert(qmp->response);
431 QDict *qmp_fd_receive(int fd)
433 QMPResponseParser qmp;
434 bool log = getenv("QTEST_LOG") != NULL;
436 qmp.response = NULL;
437 json_message_parser_init(&qmp.parser, qmp_response);
438 while (!qmp.response) {
439 ssize_t len;
440 char c;
442 len = read(fd, &c, 1);
443 if (len == -1 && errno == EINTR) {
444 continue;
447 if (len == -1 || len == 0) {
448 fprintf(stderr, "Broken pipe\n");
449 exit(1);
452 if (log) {
453 len = write(2, &c, 1);
455 json_message_parser_feed(&qmp.parser, &c, 1);
457 json_message_parser_destroy(&qmp.parser);
459 return qmp.response;
462 QDict *qtest_qmp_receive(QTestState *s)
464 return qmp_fd_receive(s->qmp_fd);
468 * Allow users to send a message without waiting for the reply,
469 * in the case that they choose to discard all replies up until
470 * a particular EVENT is received.
472 void qmp_fd_sendv(int fd, const char *fmt, va_list ap)
474 va_list ap_copy;
475 QObject *qobj;
477 /* qobject_from_jsonv() silently eats leading 0xff as invalid
478 * JSON, but we want to test sending them over the wire to force
479 * resyncs */
480 if (*fmt == '\377') {
481 socket_send(fd, fmt, 1);
482 fmt++;
485 /* Going through qobject ensures we escape strings properly.
486 * This seemingly unnecessary copy is required in case va_list
487 * is an array type.
489 va_copy(ap_copy, ap);
490 qobj = qobject_from_jsonv(fmt, &ap_copy, &error_abort);
491 va_end(ap_copy);
493 /* No need to send anything for an empty QObject. */
494 if (qobj) {
495 int log = getenv("QTEST_LOG") != NULL;
496 QString *qstr = qobject_to_json(qobj);
497 const char *str;
500 * BUG: QMP doesn't react to input until it sees a newline, an
501 * object, or an array. Work-around: give it a newline.
503 qstring_append_chr(qstr, '\n');
504 str = qstring_get_str(qstr);
506 if (log) {
507 fprintf(stderr, "%s", str);
509 /* Send QMP request */
510 socket_send(fd, str, qstring_get_length(qstr));
512 QDECREF(qstr);
513 qobject_decref(qobj);
517 void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap)
519 qmp_fd_sendv(s->qmp_fd, fmt, ap);
522 QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
524 qmp_fd_sendv(fd, fmt, ap);
526 return qmp_fd_receive(fd);
529 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
531 qtest_async_qmpv(s, fmt, ap);
533 /* Receive reply */
534 return qtest_qmp_receive(s);
537 QDict *qmp_fd(int fd, const char *fmt, ...)
539 va_list ap;
540 QDict *response;
542 va_start(ap, fmt);
543 response = qmp_fdv(fd, fmt, ap);
544 va_end(ap);
545 return response;
548 void qmp_fd_send(int fd, const char *fmt, ...)
550 va_list ap;
552 va_start(ap, fmt);
553 qmp_fd_sendv(fd, fmt, ap);
554 va_end(ap);
557 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
559 va_list ap;
560 QDict *response;
562 va_start(ap, fmt);
563 response = qtest_qmpv(s, fmt, ap);
564 va_end(ap);
565 return response;
568 void qtest_async_qmp(QTestState *s, const char *fmt, ...)
570 va_list ap;
572 va_start(ap, fmt);
573 qtest_async_qmpv(s, fmt, ap);
574 va_end(ap);
577 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
579 QDict *response = qtest_qmpv(s, fmt, ap);
580 QDECREF(response);
583 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
585 va_list ap;
586 QDict *response;
588 va_start(ap, fmt);
589 response = qtest_qmpv(s, fmt, ap);
590 va_end(ap);
591 QDECREF(response);
594 QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
596 QDict *response;
598 for (;;) {
599 response = qtest_qmp_receive(s);
600 if ((qdict_haskey(response, "event")) &&
601 (strcmp(qdict_get_str(response, "event"), event) == 0)) {
602 return response;
604 QDECREF(response);
608 void qtest_qmp_eventwait(QTestState *s, const char *event)
610 QDict *response;
612 response = qtest_qmp_eventwait_ref(s, event);
613 QDECREF(response);
616 char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)
618 char *cmd;
619 QDict *resp;
620 char *ret;
622 cmd = g_strdup_vprintf(fmt, ap);
623 resp = qtest_qmp(s, "{'execute': 'human-monitor-command',"
624 " 'arguments': {'command-line': %s}}",
625 cmd);
626 ret = g_strdup(qdict_get_try_str(resp, "return"));
627 while (ret == NULL && qdict_get_try_str(resp, "event")) {
628 /* Ignore asynchronous QMP events */
629 QDECREF(resp);
630 resp = qtest_qmp_receive(s);
631 ret = g_strdup(qdict_get_try_str(resp, "return"));
633 g_assert(ret);
634 QDECREF(resp);
635 g_free(cmd);
636 return ret;
639 char *qtest_hmp(QTestState *s, const char *fmt, ...)
641 va_list ap;
642 char *ret;
644 va_start(ap, fmt);
645 ret = qtest_hmpv(s, fmt, ap);
646 va_end(ap);
647 return ret;
650 const char *qtest_get_arch(void)
652 const char *qemu = qtest_qemu_binary();
653 const char *end = strrchr(qemu, '/');
655 return end + strlen("/qemu-system-");
658 bool qtest_get_irq(QTestState *s, int num)
660 /* dummy operation in order to make sure irq is up to date */
661 qtest_inb(s, 0);
663 return s->irq_level[num];
666 static int64_t qtest_clock_rsp(QTestState *s)
668 gchar **words;
669 int64_t clock;
670 words = qtest_rsp(s, 2);
671 clock = g_ascii_strtoll(words[1], NULL, 0);
672 g_strfreev(words);
673 return clock;
676 int64_t qtest_clock_step_next(QTestState *s)
678 qtest_sendf(s, "clock_step\n");
679 return qtest_clock_rsp(s);
682 int64_t qtest_clock_step(QTestState *s, int64_t step)
684 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
685 return qtest_clock_rsp(s);
688 int64_t qtest_clock_set(QTestState *s, int64_t val)
690 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
691 return qtest_clock_rsp(s);
694 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
696 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
697 qtest_rsp(s, 0);
700 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
702 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
703 qtest_rsp(s, 0);
706 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
708 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
709 qtest_rsp(s, 0);
712 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
714 qtest_out(s, "outb", addr, value);
717 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
719 qtest_out(s, "outw", addr, value);
722 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
724 qtest_out(s, "outl", addr, value);
727 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
729 gchar **args;
730 uint32_t value;
732 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
733 args = qtest_rsp(s, 2);
734 value = strtoul(args[1], NULL, 0);
735 g_strfreev(args);
737 return value;
740 uint8_t qtest_inb(QTestState *s, uint16_t addr)
742 return qtest_in(s, "inb", addr);
745 uint16_t qtest_inw(QTestState *s, uint16_t addr)
747 return qtest_in(s, "inw", addr);
750 uint32_t qtest_inl(QTestState *s, uint16_t addr)
752 return qtest_in(s, "inl", addr);
755 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
756 uint64_t value)
758 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
759 qtest_rsp(s, 0);
762 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
764 qtest_write(s, "writeb", addr, value);
767 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
769 qtest_write(s, "writew", addr, value);
772 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
774 qtest_write(s, "writel", addr, value);
777 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
779 qtest_write(s, "writeq", addr, value);
782 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
784 gchar **args;
785 uint64_t value;
787 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
788 args = qtest_rsp(s, 2);
789 value = strtoull(args[1], NULL, 0);
790 g_strfreev(args);
792 return value;
795 uint8_t qtest_readb(QTestState *s, uint64_t addr)
797 return qtest_read(s, "readb", addr);
800 uint16_t qtest_readw(QTestState *s, uint64_t addr)
802 return qtest_read(s, "readw", addr);
805 uint32_t qtest_readl(QTestState *s, uint64_t addr)
807 return qtest_read(s, "readl", addr);
810 uint64_t qtest_readq(QTestState *s, uint64_t addr)
812 return qtest_read(s, "readq", addr);
815 static int hex2nib(char ch)
817 if (ch >= '0' && ch <= '9') {
818 return ch - '0';
819 } else if (ch >= 'a' && ch <= 'f') {
820 return 10 + (ch - 'a');
821 } else if (ch >= 'A' && ch <= 'F') {
822 return 10 + (ch - 'a');
823 } else {
824 return -1;
828 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
830 uint8_t *ptr = data;
831 gchar **args;
832 size_t i;
834 if (!size) {
835 return;
838 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
839 args = qtest_rsp(s, 2);
841 for (i = 0; i < size; i++) {
842 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
843 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
846 g_strfreev(args);
849 uint64_t qtest_rtas_call(QTestState *s, const char *name,
850 uint32_t nargs, uint64_t args,
851 uint32_t nret, uint64_t ret)
853 qtest_sendf(s, "rtas %s %u 0x%"PRIx64" %u 0x%"PRIx64"\n",
854 name, nargs, args, nret, ret);
855 qtest_rsp(s, 0);
856 return 0;
859 void qtest_add_func(const char *str, void (*fn)(void))
861 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
862 g_test_add_func(path, fn);
863 g_free(path);
866 void qtest_add_data_func_full(const char *str, void *data,
867 void (*fn)(const void *),
868 GDestroyNotify data_free_func)
870 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
871 g_test_add_data_func_full(path, data, fn, data_free_func);
872 g_free(path);
875 void qtest_add_data_func(const char *str, const void *data,
876 void (*fn)(const void *))
878 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
879 g_test_add_data_func(path, data, fn);
880 g_free(path);
883 void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
885 gchar *bdata;
887 bdata = g_base64_encode(data, size);
888 qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size);
889 socket_send(s->fd, bdata, strlen(bdata));
890 socket_send(s->fd, "\n", 1);
891 qtest_rsp(s, 0);
892 g_free(bdata);
895 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
897 gchar **args;
898 size_t len;
900 qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size);
901 args = qtest_rsp(s, 2);
903 g_base64_decode_inplace(args[1], &len);
904 if (size != len) {
905 fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n",
906 size, len);
907 len = MIN(len, size);
910 memcpy(data, args[1], len);
911 g_strfreev(args);
914 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
916 const uint8_t *ptr = data;
917 size_t i;
918 char *enc;
920 if (!size) {
921 return;
924 enc = g_malloc(2 * size + 1);
926 for (i = 0; i < size; i++) {
927 sprintf(&enc[i * 2], "%02x", ptr[i]);
930 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
931 qtest_rsp(s, 0);
932 g_free(enc);
935 void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
937 qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
938 qtest_rsp(s, 0);
941 QDict *qmp(const char *fmt, ...)
943 va_list ap;
944 QDict *response;
946 va_start(ap, fmt);
947 response = qtest_qmpv(global_qtest, fmt, ap);
948 va_end(ap);
949 return response;
952 void qmp_async(const char *fmt, ...)
954 va_list ap;
956 va_start(ap, fmt);
957 qtest_async_qmpv(global_qtest, fmt, ap);
958 va_end(ap);
961 void qmp_discard_response(const char *fmt, ...)
963 va_list ap;
965 va_start(ap, fmt);
966 qtest_qmpv_discard_response(global_qtest, fmt, ap);
967 va_end(ap);
969 char *hmp(const char *fmt, ...)
971 va_list ap;
972 char *ret;
974 va_start(ap, fmt);
975 ret = qtest_hmpv(global_qtest, fmt, ap);
976 va_end(ap);
977 return ret;
980 bool qtest_big_endian(QTestState *s)
982 return s->big_endian;
985 void qtest_cb_for_every_machine(void (*cb)(const char *machine))
987 QDict *response, *minfo;
988 QList *list;
989 const QListEntry *p;
990 QObject *qobj;
991 QString *qstr;
992 const char *mname;
994 qtest_start("-machine none");
995 response = qmp("{ 'execute': 'query-machines' }");
996 g_assert(response);
997 list = qdict_get_qlist(response, "return");
998 g_assert(list);
1000 for (p = qlist_first(list); p; p = qlist_next(p)) {
1001 minfo = qobject_to_qdict(qlist_entry_obj(p));
1002 g_assert(minfo);
1003 qobj = qdict_get(minfo, "name");
1004 g_assert(qobj);
1005 qstr = qobject_to_qstring(qobj);
1006 g_assert(qstr);
1007 mname = qstring_get_str(qstr);
1008 cb(mname);
1011 qtest_end();
1012 QDECREF(response);
1016 * Generic hot-plugging test via the device_add QMP command.
1018 void qtest_qmp_device_add(const char *driver, const char *id, const char *fmt,
1019 ...)
1021 QDict *response;
1022 char *cmd, *opts = NULL;
1023 va_list va;
1025 if (fmt) {
1026 va_start(va, fmt);
1027 opts = g_strdup_vprintf(fmt, va);
1028 va_end(va);
1031 cmd = g_strdup_printf("{'execute': 'device_add',"
1032 " 'arguments': { 'driver': '%s', 'id': '%s'%s%s }}",
1033 driver, id, opts ? ", " : "", opts ? opts : "");
1034 g_free(opts);
1036 response = qmp(cmd);
1037 g_free(cmd);
1038 g_assert(response);
1039 g_assert(!qdict_haskey(response, "event")); /* We don't expect any events */
1040 g_assert(!qdict_haskey(response, "error"));
1041 QDECREF(response);
1045 * Generic hot-unplugging test via the device_del QMP command.
1046 * Device deletion will get one response and one event. For example:
1048 * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
1050 * will get this one:
1052 * {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
1053 * "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
1054 * "path": "/machine/peripheral/scsi-hd"}}
1056 * and this one:
1058 * {"return": {}}
1060 * But the order of arrival may vary - so we've got to detect both.
1062 void qtest_qmp_device_del(const char *id)
1064 QDict *response1, *response2, *event = NULL;
1065 char *cmd;
1067 cmd = g_strdup_printf("{'execute': 'device_del',"
1068 " 'arguments': { 'id': '%s' }}", id);
1069 response1 = qmp(cmd);
1070 g_free(cmd);
1071 g_assert(response1);
1072 g_assert(!qdict_haskey(response1, "error"));
1074 response2 = qmp("");
1075 g_assert(response2);
1076 g_assert(!qdict_haskey(response2, "error"));
1078 if (qdict_haskey(response1, "event")) {
1079 event = response1;
1080 } else if (qdict_haskey(response2, "event")) {
1081 event = response2;
1083 g_assert(event);
1084 g_assert_cmpstr(qdict_get_str(event, "event"), ==, "DEVICE_DELETED");
1086 QDECREF(response1);
1087 QDECREF(response2);