Revert "block: Remove dead deprecation warning code"
[qemu.git] / tests / libqtest.c
blob098af6aec44883b0be0983c8cba7aa4b01efae1a
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"
19 #include <sys/socket.h>
20 #include <sys/wait.h>
21 #include <sys/un.h>
23 #include "libqtest.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"
33 #define MAX_IRQ 256
34 #define SOCKET_TIMEOUT 50
36 QTestState *global_qtest;
38 struct QTestState
40 int fd;
41 int qmp_fd;
42 bool irq_level[MAX_IRQ];
43 GString *rx;
44 pid_t qemu_pid; /* our child QEMU process */
45 bool big_endian;
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); \
53 } while (0)
55 static int qtest_query_target_endianness(QTestState *s);
57 static int init_socket(const char *socket_path)
59 struct sockaddr_un addr;
60 int sock;
61 int ret;
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);
70 do {
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);
77 return sock;
80 static int socket_accept(int sock)
82 struct sockaddr_un addr;
83 socklen_t addrlen;
84 int ret;
85 struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT,
86 .tv_usec = 0 };
88 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout,
89 sizeof(timeout));
91 do {
92 addrlen = sizeof(addr);
93 ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
94 } while (ret == -1 && errno == EINTR);
95 if (ret == -1) {
96 fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
98 close(sock);
100 return ret;
103 static void kill_qemu(QTestState *s)
105 if (s->qemu_pid != -1) {
106 int wstatus = 0;
107 pid_t pid;
109 kill(s->qemu_pid, SIGTERM);
110 pid = waitpid(s->qemu_pid, &wstatus, 0);
112 if (pid == s->qemu_pid && WIFSIGNALED(wstatus)) {
113 assert(!WCOREDUMP(wstatus));
118 static void kill_qemu_hook_func(void *s)
120 kill_qemu(s);
123 static void sigabrt_handler(int signo)
125 g_hook_list_invoke(&abrt_hooks, FALSE);
128 static void setup_sigabrt_handler(void)
130 struct sigaction sigact;
132 /* Catch SIGABRT to clean up on g_assert() failure */
133 sigact = (struct sigaction){
134 .sa_handler = sigabrt_handler,
135 .sa_flags = SA_RESETHAND,
137 sigemptyset(&sigact.sa_mask);
138 sigaction(SIGABRT, &sigact, &sigact_old);
141 static void cleanup_sigabrt_handler(void)
143 sigaction(SIGABRT, &sigact_old, NULL);
146 void qtest_add_abrt_handler(GHookFunc fn, const void *data)
148 GHook *hook;
150 /* Only install SIGABRT handler once */
151 if (!abrt_hooks.is_setup) {
152 g_hook_list_init(&abrt_hooks, sizeof(GHook));
154 setup_sigabrt_handler();
156 hook = g_hook_alloc(&abrt_hooks);
157 hook->func = fn;
158 hook->data = (void *)data;
160 g_hook_prepend(&abrt_hooks, hook);
163 static const char *qtest_qemu_binary(void)
165 const char *qemu_bin;
167 qemu_bin = getenv("QTEST_QEMU_BINARY");
168 if (!qemu_bin) {
169 fprintf(stderr, "Environment variable QTEST_QEMU_BINARY required\n");
170 exit(1);
173 return qemu_bin;
176 QTestState *qtest_init_without_qmp_handshake(bool use_oob,
177 const char *extra_args)
179 QTestState *s;
180 int sock, qmpsock, i;
181 gchar *socket_path;
182 gchar *qmp_socket_path;
183 gchar *command;
184 const char *qemu_binary = qtest_qemu_binary();
186 s = g_new(QTestState, 1);
188 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
189 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
191 /* It's possible that if an earlier test run crashed it might
192 * have left a stale unix socket lying around. Delete any
193 * stale old socket to avoid spurious test failures with
194 * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
196 unlink(socket_path);
197 unlink(qmp_socket_path);
199 sock = init_socket(socket_path);
200 qmpsock = init_socket(qmp_socket_path);
202 qtest_add_abrt_handler(kill_qemu_hook_func, s);
204 s->qemu_pid = fork();
205 if (s->qemu_pid == 0) {
206 setenv("QEMU_AUDIO_DRV", "none", true);
207 command = g_strdup_printf("exec %s "
208 "-qtest unix:%s,nowait "
209 "-qtest-log %s "
210 "-chardev socket,path=%s,nowait,id=char0 "
211 "-mon chardev=char0,mode=control%s "
212 "-machine accel=qtest "
213 "-display none "
214 "%s", qemu_binary, socket_path,
215 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
216 qmp_socket_path, use_oob ? ",x-oob=on" : "",
217 extra_args ?: "");
218 execlp("/bin/sh", "sh", "-c", command, NULL);
219 exit(1);
222 s->fd = socket_accept(sock);
223 if (s->fd >= 0) {
224 s->qmp_fd = socket_accept(qmpsock);
226 unlink(socket_path);
227 unlink(qmp_socket_path);
228 g_free(socket_path);
229 g_free(qmp_socket_path);
231 g_assert(s->fd >= 0 && s->qmp_fd >= 0);
233 s->rx = g_string_new("");
234 for (i = 0; i < MAX_IRQ; i++) {
235 s->irq_level[i] = false;
238 if (getenv("QTEST_STOP")) {
239 kill(s->qemu_pid, SIGSTOP);
242 /* ask endianness of the target */
244 s->big_endian = qtest_query_target_endianness(s);
246 return s;
249 QTestState *qtest_init(const char *extra_args)
251 QTestState *s = qtest_init_without_qmp_handshake(false, extra_args);
253 /* Read the QMP greeting and then do the handshake */
254 qtest_qmp_discard_response(s, "");
255 qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
257 return s;
260 QTestState *qtest_vstartf(const char *fmt, va_list ap)
262 char *args = g_strdup_vprintf(fmt, ap);
263 QTestState *s;
265 s = qtest_start(args);
266 g_free(args);
267 global_qtest = NULL;
268 return s;
271 QTestState *qtest_startf(const char *fmt, ...)
273 va_list ap;
274 QTestState *s;
276 va_start(ap, fmt);
277 s = qtest_vstartf(fmt, ap);
278 va_end(ap);
279 return s;
282 void qtest_quit(QTestState *s)
284 g_hook_destroy_link(&abrt_hooks, g_hook_find_data(&abrt_hooks, TRUE, s));
286 /* Uninstall SIGABRT handler on last instance */
287 cleanup_sigabrt_handler();
289 kill_qemu(s);
290 close(s->fd);
291 close(s->qmp_fd);
292 g_string_free(s->rx, true);
293 g_free(s);
296 static void socket_send(int fd, const char *buf, size_t size)
298 size_t offset;
300 offset = 0;
301 while (offset < size) {
302 ssize_t len;
304 len = write(fd, buf + offset, size - offset);
305 if (len == -1 && errno == EINTR) {
306 continue;
309 g_assert_no_errno(len);
310 g_assert_cmpint(len, >, 0);
312 offset += len;
316 static void socket_sendf(int fd, const char *fmt, va_list ap)
318 gchar *str = g_strdup_vprintf(fmt, ap);
319 size_t size = strlen(str);
321 socket_send(fd, str, size);
322 g_free(str);
325 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
327 va_list ap;
329 va_start(ap, fmt);
330 socket_sendf(s->fd, fmt, ap);
331 va_end(ap);
334 static GString *qtest_recv_line(QTestState *s)
336 GString *line;
337 size_t offset;
338 char *eol;
340 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
341 ssize_t len;
342 char buffer[1024];
344 len = read(s->fd, buffer, sizeof(buffer));
345 if (len == -1 && errno == EINTR) {
346 continue;
349 if (len == -1 || len == 0) {
350 fprintf(stderr, "Broken pipe\n");
351 exit(1);
354 g_string_append_len(s->rx, buffer, len);
357 offset = eol - s->rx->str;
358 line = g_string_new_len(s->rx->str, offset);
359 g_string_erase(s->rx, 0, offset + 1);
361 return line;
364 static gchar **qtest_rsp(QTestState *s, int expected_args)
366 GString *line;
367 gchar **words;
368 int i;
370 redo:
371 line = qtest_recv_line(s);
372 words = g_strsplit(line->str, " ", 0);
373 g_string_free(line, TRUE);
375 if (strcmp(words[0], "IRQ") == 0) {
376 long irq;
377 int ret;
379 g_assert(words[1] != NULL);
380 g_assert(words[2] != NULL);
382 ret = qemu_strtol(words[2], NULL, 0, &irq);
383 g_assert(!ret);
384 g_assert_cmpint(irq, >=, 0);
385 g_assert_cmpint(irq, <, MAX_IRQ);
387 if (strcmp(words[1], "raise") == 0) {
388 s->irq_level[irq] = true;
389 } else {
390 s->irq_level[irq] = false;
393 g_strfreev(words);
394 goto redo;
397 g_assert(words[0] != NULL);
398 g_assert_cmpstr(words[0], ==, "OK");
400 if (expected_args) {
401 for (i = 0; i < expected_args; i++) {
402 g_assert(words[i] != NULL);
404 } else {
405 g_strfreev(words);
408 return words;
411 static int qtest_query_target_endianness(QTestState *s)
413 gchar **args;
414 int big_endian;
416 qtest_sendf(s, "endianness\n");
417 args = qtest_rsp(s, 1);
418 g_assert(strcmp(args[1], "big") == 0 || strcmp(args[1], "little") == 0);
419 big_endian = strcmp(args[1], "big") == 0;
420 g_strfreev(args);
422 return big_endian;
425 typedef struct {
426 JSONMessageParser parser;
427 QDict *response;
428 } QMPResponseParser;
430 static void qmp_response(JSONMessageParser *parser, GQueue *tokens)
432 QMPResponseParser *qmp = container_of(parser, QMPResponseParser, parser);
433 QObject *obj;
435 obj = json_parser_parse(tokens, NULL);
436 if (!obj) {
437 fprintf(stderr, "QMP JSON response parsing failed\n");
438 exit(1);
441 g_assert(!qmp->response);
442 qmp->response = qobject_to(QDict, obj);
443 g_assert(qmp->response);
446 QDict *qmp_fd_receive(int fd)
448 QMPResponseParser qmp;
449 bool log = getenv("QTEST_LOG") != NULL;
451 qmp.response = NULL;
452 json_message_parser_init(&qmp.parser, qmp_response);
453 while (!qmp.response) {
454 ssize_t len;
455 char c;
457 len = read(fd, &c, 1);
458 if (len == -1 && errno == EINTR) {
459 continue;
462 if (len == -1 || len == 0) {
463 fprintf(stderr, "Broken pipe\n");
464 exit(1);
467 if (log) {
468 len = write(2, &c, 1);
470 json_message_parser_feed(&qmp.parser, &c, 1);
472 json_message_parser_destroy(&qmp.parser);
474 return qmp.response;
477 QDict *qtest_qmp_receive(QTestState *s)
479 return qmp_fd_receive(s->qmp_fd);
483 * Allow users to send a message without waiting for the reply,
484 * in the case that they choose to discard all replies up until
485 * a particular EVENT is received.
487 void qmp_fd_sendv(int fd, const char *fmt, va_list ap)
489 va_list ap_copy;
490 QObject *qobj;
492 /* qobject_from_jsonv() silently eats leading 0xff as invalid
493 * JSON, but we want to test sending them over the wire to force
494 * resyncs */
495 if (*fmt == '\377') {
496 socket_send(fd, fmt, 1);
497 fmt++;
500 /* Going through qobject ensures we escape strings properly.
501 * This seemingly unnecessary copy is required in case va_list
502 * is an array type.
504 va_copy(ap_copy, ap);
505 qobj = qobject_from_jsonv(fmt, &ap_copy, &error_abort);
506 va_end(ap_copy);
508 /* No need to send anything for an empty QObject. */
509 if (qobj) {
510 int log = getenv("QTEST_LOG") != NULL;
511 QString *qstr = qobject_to_json(qobj);
512 const char *str;
515 * BUG: QMP doesn't react to input until it sees a newline, an
516 * object, or an array. Work-around: give it a newline.
518 qstring_append_chr(qstr, '\n');
519 str = qstring_get_str(qstr);
521 if (log) {
522 fprintf(stderr, "%s", str);
524 /* Send QMP request */
525 socket_send(fd, str, qstring_get_length(qstr));
527 qobject_unref(qstr);
528 qobject_unref(qobj);
532 void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap)
534 qmp_fd_sendv(s->qmp_fd, fmt, ap);
537 QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
539 qmp_fd_sendv(fd, fmt, ap);
541 return qmp_fd_receive(fd);
544 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
546 qtest_async_qmpv(s, fmt, ap);
548 /* Receive reply */
549 return qtest_qmp_receive(s);
552 QDict *qmp_fd(int fd, const char *fmt, ...)
554 va_list ap;
555 QDict *response;
557 va_start(ap, fmt);
558 response = qmp_fdv(fd, fmt, ap);
559 va_end(ap);
560 return response;
563 void qmp_fd_send(int fd, const char *fmt, ...)
565 va_list ap;
567 va_start(ap, fmt);
568 qmp_fd_sendv(fd, fmt, ap);
569 va_end(ap);
572 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
574 va_list ap;
575 QDict *response;
577 va_start(ap, fmt);
578 response = qtest_qmpv(s, fmt, ap);
579 va_end(ap);
580 return response;
583 void qtest_async_qmp(QTestState *s, const char *fmt, ...)
585 va_list ap;
587 va_start(ap, fmt);
588 qtest_async_qmpv(s, fmt, ap);
589 va_end(ap);
592 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
594 QDict *response = qtest_qmpv(s, fmt, ap);
595 qobject_unref(response);
598 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
600 va_list ap;
601 QDict *response;
603 va_start(ap, fmt);
604 response = qtest_qmpv(s, fmt, ap);
605 va_end(ap);
606 qobject_unref(response);
609 QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
611 QDict *response;
613 for (;;) {
614 response = qtest_qmp_receive(s);
615 if ((qdict_haskey(response, "event")) &&
616 (strcmp(qdict_get_str(response, "event"), event) == 0)) {
617 return response;
619 qobject_unref(response);
623 void qtest_qmp_eventwait(QTestState *s, const char *event)
625 QDict *response;
627 response = qtest_qmp_eventwait_ref(s, event);
628 qobject_unref(response);
631 char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)
633 char *cmd;
634 QDict *resp;
635 char *ret;
637 cmd = g_strdup_vprintf(fmt, ap);
638 resp = qtest_qmp(s, "{'execute': 'human-monitor-command',"
639 " 'arguments': {'command-line': %s}}",
640 cmd);
641 ret = g_strdup(qdict_get_try_str(resp, "return"));
642 while (ret == NULL && qdict_get_try_str(resp, "event")) {
643 /* Ignore asynchronous QMP events */
644 qobject_unref(resp);
645 resp = qtest_qmp_receive(s);
646 ret = g_strdup(qdict_get_try_str(resp, "return"));
648 g_assert(ret);
649 qobject_unref(resp);
650 g_free(cmd);
651 return ret;
654 char *qtest_hmp(QTestState *s, const char *fmt, ...)
656 va_list ap;
657 char *ret;
659 va_start(ap, fmt);
660 ret = qtest_hmpv(s, fmt, ap);
661 va_end(ap);
662 return ret;
665 const char *qtest_get_arch(void)
667 const char *qemu = qtest_qemu_binary();
668 const char *end = strrchr(qemu, '/');
670 return end + strlen("/qemu-system-");
673 bool qtest_get_irq(QTestState *s, int num)
675 /* dummy operation in order to make sure irq is up to date */
676 qtest_inb(s, 0);
678 return s->irq_level[num];
681 static int64_t qtest_clock_rsp(QTestState *s)
683 gchar **words;
684 int64_t clock;
685 words = qtest_rsp(s, 2);
686 clock = g_ascii_strtoll(words[1], NULL, 0);
687 g_strfreev(words);
688 return clock;
691 int64_t qtest_clock_step_next(QTestState *s)
693 qtest_sendf(s, "clock_step\n");
694 return qtest_clock_rsp(s);
697 int64_t qtest_clock_step(QTestState *s, int64_t step)
699 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
700 return qtest_clock_rsp(s);
703 int64_t qtest_clock_set(QTestState *s, int64_t val)
705 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
706 return qtest_clock_rsp(s);
709 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
711 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
712 qtest_rsp(s, 0);
715 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
717 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
718 qtest_rsp(s, 0);
721 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
723 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
724 qtest_rsp(s, 0);
727 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
729 qtest_out(s, "outb", addr, value);
732 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
734 qtest_out(s, "outw", addr, value);
737 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
739 qtest_out(s, "outl", addr, value);
742 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
744 gchar **args;
745 int ret;
746 unsigned long value;
748 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
749 args = qtest_rsp(s, 2);
750 ret = qemu_strtoul(args[1], NULL, 0, &value);
751 g_assert(!ret && value <= UINT32_MAX);
752 g_strfreev(args);
754 return value;
757 uint8_t qtest_inb(QTestState *s, uint16_t addr)
759 return qtest_in(s, "inb", addr);
762 uint16_t qtest_inw(QTestState *s, uint16_t addr)
764 return qtest_in(s, "inw", addr);
767 uint32_t qtest_inl(QTestState *s, uint16_t addr)
769 return qtest_in(s, "inl", addr);
772 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
773 uint64_t value)
775 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
776 qtest_rsp(s, 0);
779 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
781 qtest_write(s, "writeb", addr, value);
784 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
786 qtest_write(s, "writew", addr, value);
789 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
791 qtest_write(s, "writel", addr, value);
794 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
796 qtest_write(s, "writeq", addr, value);
799 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
801 gchar **args;
802 int ret;
803 uint64_t value;
805 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
806 args = qtest_rsp(s, 2);
807 ret = qemu_strtou64(args[1], NULL, 0, &value);
808 g_assert(!ret);
809 g_strfreev(args);
811 return value;
814 uint8_t qtest_readb(QTestState *s, uint64_t addr)
816 return qtest_read(s, "readb", addr);
819 uint16_t qtest_readw(QTestState *s, uint64_t addr)
821 return qtest_read(s, "readw", addr);
824 uint32_t qtest_readl(QTestState *s, uint64_t addr)
826 return qtest_read(s, "readl", addr);
829 uint64_t qtest_readq(QTestState *s, uint64_t addr)
831 return qtest_read(s, "readq", addr);
834 static int hex2nib(char ch)
836 if (ch >= '0' && ch <= '9') {
837 return ch - '0';
838 } else if (ch >= 'a' && ch <= 'f') {
839 return 10 + (ch - 'a');
840 } else if (ch >= 'A' && ch <= 'F') {
841 return 10 + (ch - 'a');
842 } else {
843 return -1;
847 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
849 uint8_t *ptr = data;
850 gchar **args;
851 size_t i;
853 if (!size) {
854 return;
857 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
858 args = qtest_rsp(s, 2);
860 for (i = 0; i < size; i++) {
861 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
862 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
865 g_strfreev(args);
868 uint64_t qtest_rtas_call(QTestState *s, const char *name,
869 uint32_t nargs, uint64_t args,
870 uint32_t nret, uint64_t ret)
872 qtest_sendf(s, "rtas %s %u 0x%"PRIx64" %u 0x%"PRIx64"\n",
873 name, nargs, args, nret, ret);
874 qtest_rsp(s, 0);
875 return 0;
878 void qtest_add_func(const char *str, void (*fn)(void))
880 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
881 g_test_add_func(path, fn);
882 g_free(path);
885 void qtest_add_data_func_full(const char *str, void *data,
886 void (*fn)(const void *),
887 GDestroyNotify data_free_func)
889 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
890 g_test_add_data_func_full(path, data, fn, data_free_func);
891 g_free(path);
894 void qtest_add_data_func(const char *str, const void *data,
895 void (*fn)(const void *))
897 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
898 g_test_add_data_func(path, data, fn);
899 g_free(path);
902 void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
904 gchar *bdata;
906 bdata = g_base64_encode(data, size);
907 qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size);
908 socket_send(s->fd, bdata, strlen(bdata));
909 socket_send(s->fd, "\n", 1);
910 qtest_rsp(s, 0);
911 g_free(bdata);
914 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
916 gchar **args;
917 size_t len;
919 qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size);
920 args = qtest_rsp(s, 2);
922 g_base64_decode_inplace(args[1], &len);
923 if (size != len) {
924 fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n",
925 size, len);
926 len = MIN(len, size);
929 memcpy(data, args[1], len);
930 g_strfreev(args);
933 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
935 const uint8_t *ptr = data;
936 size_t i;
937 char *enc;
939 if (!size) {
940 return;
943 enc = g_malloc(2 * size + 1);
945 for (i = 0; i < size; i++) {
946 sprintf(&enc[i * 2], "%02x", ptr[i]);
949 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
950 qtest_rsp(s, 0);
951 g_free(enc);
954 void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
956 qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
957 qtest_rsp(s, 0);
960 QDict *qmp(const char *fmt, ...)
962 va_list ap;
963 QDict *response;
965 va_start(ap, fmt);
966 response = qtest_qmpv(global_qtest, fmt, ap);
967 va_end(ap);
968 return response;
971 void qmp_async(const char *fmt, ...)
973 va_list ap;
975 va_start(ap, fmt);
976 qtest_async_qmpv(global_qtest, fmt, ap);
977 va_end(ap);
980 void qmp_discard_response(const char *fmt, ...)
982 va_list ap;
984 va_start(ap, fmt);
985 qtest_qmpv_discard_response(global_qtest, fmt, ap);
986 va_end(ap);
988 char *hmp(const char *fmt, ...)
990 va_list ap;
991 char *ret;
993 va_start(ap, fmt);
994 ret = qtest_hmpv(global_qtest, fmt, ap);
995 va_end(ap);
996 return ret;
999 bool qtest_big_endian(QTestState *s)
1001 return s->big_endian;
1004 void qtest_cb_for_every_machine(void (*cb)(const char *machine))
1006 QDict *response, *minfo;
1007 QList *list;
1008 const QListEntry *p;
1009 QObject *qobj;
1010 QString *qstr;
1011 const char *mname;
1013 qtest_start("-machine none");
1014 response = qmp("{ 'execute': 'query-machines' }");
1015 g_assert(response);
1016 list = qdict_get_qlist(response, "return");
1017 g_assert(list);
1019 for (p = qlist_first(list); p; p = qlist_next(p)) {
1020 minfo = qobject_to(QDict, qlist_entry_obj(p));
1021 g_assert(minfo);
1022 qobj = qdict_get(minfo, "name");
1023 g_assert(qobj);
1024 qstr = qobject_to(QString, qobj);
1025 g_assert(qstr);
1026 mname = qstring_get_str(qstr);
1027 cb(mname);
1030 qtest_end();
1031 qobject_unref(response);
1035 * Generic hot-plugging test via the device_add QMP command.
1037 void qtest_qmp_device_add(const char *driver, const char *id, const char *fmt,
1038 ...)
1040 QDict *response;
1041 char *cmd, *opts = NULL;
1042 va_list va;
1044 if (fmt) {
1045 va_start(va, fmt);
1046 opts = g_strdup_vprintf(fmt, va);
1047 va_end(va);
1050 cmd = g_strdup_printf("{'execute': 'device_add',"
1051 " 'arguments': { 'driver': '%s', 'id': '%s'%s%s }}",
1052 driver, id, opts ? ", " : "", opts ? opts : "");
1053 g_free(opts);
1055 response = qmp(cmd);
1056 g_free(cmd);
1057 g_assert(response);
1058 g_assert(!qdict_haskey(response, "event")); /* We don't expect any events */
1059 g_assert(!qdict_haskey(response, "error"));
1060 qobject_unref(response);
1064 * Generic hot-unplugging test via the device_del QMP command.
1065 * Device deletion will get one response and one event. For example:
1067 * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
1069 * will get this one:
1071 * {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
1072 * "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
1073 * "path": "/machine/peripheral/scsi-hd"}}
1075 * and this one:
1077 * {"return": {}}
1079 * But the order of arrival may vary - so we've got to detect both.
1081 void qtest_qmp_device_del(const char *id)
1083 QDict *response1, *response2, *event = NULL;
1084 char *cmd;
1086 cmd = g_strdup_printf("{'execute': 'device_del',"
1087 " 'arguments': { 'id': '%s' }}", id);
1088 response1 = qmp(cmd);
1089 g_free(cmd);
1090 g_assert(response1);
1091 g_assert(!qdict_haskey(response1, "error"));
1093 response2 = qmp("");
1094 g_assert(response2);
1095 g_assert(!qdict_haskey(response2, "error"));
1097 if (qdict_haskey(response1, "event")) {
1098 event = response1;
1099 } else if (qdict_haskey(response2, "event")) {
1100 event = response2;
1102 g_assert(event);
1103 g_assert_cmpstr(qdict_get_str(event, "event"), ==, "DEVICE_DELETED");
1105 qobject_unref(response1);
1106 qobject_unref(response2);
1109 bool qmp_rsp_is_err(QDict *rsp)
1111 QDict *error = qdict_get_qdict(rsp, "error");
1112 qobject_unref(rsp);
1113 return !!error;