slirp: reformat m_inc routine
[qemu/ar7.git] / tests / libqtest.c
blobe0ca19dbfea0d6b81bf0318cf184358de536c304
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 kill(s->qemu_pid, SIGTERM);
107 waitpid(s->qemu_pid, NULL, 0);
111 static void kill_qemu_hook_func(void *s)
113 kill_qemu(s);
116 static void sigabrt_handler(int signo)
118 g_hook_list_invoke(&abrt_hooks, FALSE);
121 static void setup_sigabrt_handler(void)
123 struct sigaction sigact;
125 /* Catch SIGABRT to clean up on g_assert() failure */
126 sigact = (struct sigaction){
127 .sa_handler = sigabrt_handler,
128 .sa_flags = SA_RESETHAND,
130 sigemptyset(&sigact.sa_mask);
131 sigaction(SIGABRT, &sigact, &sigact_old);
134 static void cleanup_sigabrt_handler(void)
136 sigaction(SIGABRT, &sigact_old, NULL);
139 void qtest_add_abrt_handler(GHookFunc fn, const void *data)
141 GHook *hook;
143 /* Only install SIGABRT handler once */
144 if (!abrt_hooks.is_setup) {
145 g_hook_list_init(&abrt_hooks, sizeof(GHook));
147 setup_sigabrt_handler();
149 hook = g_hook_alloc(&abrt_hooks);
150 hook->func = fn;
151 hook->data = (void *)data;
153 g_hook_prepend(&abrt_hooks, hook);
156 static const char *qtest_qemu_binary(void)
158 const char *qemu_bin;
160 qemu_bin = getenv("QTEST_QEMU_BINARY");
161 if (!qemu_bin) {
162 fprintf(stderr, "Environment variable QTEST_QEMU_BINARY required\n");
163 exit(1);
166 return qemu_bin;
169 QTestState *qtest_init_without_qmp_handshake(bool use_oob,
170 const char *extra_args)
172 QTestState *s;
173 int sock, qmpsock, i;
174 gchar *socket_path;
175 gchar *qmp_socket_path;
176 gchar *command;
177 const char *qemu_binary = qtest_qemu_binary();
179 s = g_new(QTestState, 1);
181 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
182 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
184 /* It's possible that if an earlier test run crashed it might
185 * have left a stale unix socket lying around. Delete any
186 * stale old socket to avoid spurious test failures with
187 * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
189 unlink(socket_path);
190 unlink(qmp_socket_path);
192 sock = init_socket(socket_path);
193 qmpsock = init_socket(qmp_socket_path);
195 qtest_add_abrt_handler(kill_qemu_hook_func, s);
197 s->qemu_pid = fork();
198 if (s->qemu_pid == 0) {
199 setenv("QEMU_AUDIO_DRV", "none", true);
200 command = g_strdup_printf("exec %s "
201 "-qtest unix:%s,nowait "
202 "-qtest-log %s "
203 "-chardev socket,path=%s,nowait,id=char0 "
204 "-mon chardev=char0,mode=control%s "
205 "-machine accel=qtest "
206 "-display none "
207 "%s", qemu_binary, socket_path,
208 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
209 qmp_socket_path, use_oob ? ",x-oob=on" : "",
210 extra_args ?: "");
211 execlp("/bin/sh", "sh", "-c", command, NULL);
212 exit(1);
215 s->fd = socket_accept(sock);
216 if (s->fd >= 0) {
217 s->qmp_fd = socket_accept(qmpsock);
219 unlink(socket_path);
220 unlink(qmp_socket_path);
221 g_free(socket_path);
222 g_free(qmp_socket_path);
224 g_assert(s->fd >= 0 && s->qmp_fd >= 0);
226 s->rx = g_string_new("");
227 for (i = 0; i < MAX_IRQ; i++) {
228 s->irq_level[i] = false;
231 if (getenv("QTEST_STOP")) {
232 kill(s->qemu_pid, SIGSTOP);
235 /* ask endianness of the target */
237 s->big_endian = qtest_query_target_endianness(s);
239 return s;
242 QTestState *qtest_init(const char *extra_args)
244 QTestState *s = qtest_init_without_qmp_handshake(false, extra_args);
246 /* Read the QMP greeting and then do the handshake */
247 qtest_qmp_discard_response(s, "");
248 qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
250 return s;
253 QTestState *qtest_vstartf(const char *fmt, va_list ap)
255 char *args = g_strdup_vprintf(fmt, ap);
256 QTestState *s;
258 s = qtest_start(args);
259 g_free(args);
260 global_qtest = NULL;
261 return s;
264 QTestState *qtest_startf(const char *fmt, ...)
266 va_list ap;
267 QTestState *s;
269 va_start(ap, fmt);
270 s = qtest_vstartf(fmt, ap);
271 va_end(ap);
272 return s;
275 void qtest_quit(QTestState *s)
277 g_hook_destroy_link(&abrt_hooks, g_hook_find_data(&abrt_hooks, TRUE, s));
279 /* Uninstall SIGABRT handler on last instance */
280 cleanup_sigabrt_handler();
282 kill_qemu(s);
283 close(s->fd);
284 close(s->qmp_fd);
285 g_string_free(s->rx, true);
286 g_free(s);
289 static void socket_send(int fd, const char *buf, size_t size)
291 size_t offset;
293 offset = 0;
294 while (offset < size) {
295 ssize_t len;
297 len = write(fd, buf + offset, size - offset);
298 if (len == -1 && errno == EINTR) {
299 continue;
302 g_assert_no_errno(len);
303 g_assert_cmpint(len, >, 0);
305 offset += len;
309 static void socket_sendf(int fd, const char *fmt, va_list ap)
311 gchar *str = g_strdup_vprintf(fmt, ap);
312 size_t size = strlen(str);
314 socket_send(fd, str, size);
315 g_free(str);
318 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
320 va_list ap;
322 va_start(ap, fmt);
323 socket_sendf(s->fd, fmt, ap);
324 va_end(ap);
327 static GString *qtest_recv_line(QTestState *s)
329 GString *line;
330 size_t offset;
331 char *eol;
333 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
334 ssize_t len;
335 char buffer[1024];
337 len = read(s->fd, buffer, sizeof(buffer));
338 if (len == -1 && errno == EINTR) {
339 continue;
342 if (len == -1 || len == 0) {
343 fprintf(stderr, "Broken pipe\n");
344 exit(1);
347 g_string_append_len(s->rx, buffer, len);
350 offset = eol - s->rx->str;
351 line = g_string_new_len(s->rx->str, offset);
352 g_string_erase(s->rx, 0, offset + 1);
354 return line;
357 static gchar **qtest_rsp(QTestState *s, int expected_args)
359 GString *line;
360 gchar **words;
361 int i;
363 redo:
364 line = qtest_recv_line(s);
365 words = g_strsplit(line->str, " ", 0);
366 g_string_free(line, TRUE);
368 if (strcmp(words[0], "IRQ") == 0) {
369 long irq;
370 int ret;
372 g_assert(words[1] != NULL);
373 g_assert(words[2] != NULL);
375 ret = qemu_strtol(words[2], NULL, 0, &irq);
376 g_assert(!ret);
377 g_assert_cmpint(irq, >=, 0);
378 g_assert_cmpint(irq, <, MAX_IRQ);
380 if (strcmp(words[1], "raise") == 0) {
381 s->irq_level[irq] = true;
382 } else {
383 s->irq_level[irq] = false;
386 g_strfreev(words);
387 goto redo;
390 g_assert(words[0] != NULL);
391 g_assert_cmpstr(words[0], ==, "OK");
393 if (expected_args) {
394 for (i = 0; i < expected_args; i++) {
395 g_assert(words[i] != NULL);
397 } else {
398 g_strfreev(words);
401 return words;
404 static int qtest_query_target_endianness(QTestState *s)
406 gchar **args;
407 int big_endian;
409 qtest_sendf(s, "endianness\n");
410 args = qtest_rsp(s, 1);
411 g_assert(strcmp(args[1], "big") == 0 || strcmp(args[1], "little") == 0);
412 big_endian = strcmp(args[1], "big") == 0;
413 g_strfreev(args);
415 return big_endian;
418 typedef struct {
419 JSONMessageParser parser;
420 QDict *response;
421 } QMPResponseParser;
423 static void qmp_response(JSONMessageParser *parser, GQueue *tokens)
425 QMPResponseParser *qmp = container_of(parser, QMPResponseParser, parser);
426 QObject *obj;
428 obj = json_parser_parse(tokens, NULL);
429 if (!obj) {
430 fprintf(stderr, "QMP JSON response parsing failed\n");
431 exit(1);
434 g_assert(!qmp->response);
435 qmp->response = qobject_to(QDict, obj);
436 g_assert(qmp->response);
439 QDict *qmp_fd_receive(int fd)
441 QMPResponseParser qmp;
442 bool log = getenv("QTEST_LOG") != NULL;
444 qmp.response = NULL;
445 json_message_parser_init(&qmp.parser, qmp_response);
446 while (!qmp.response) {
447 ssize_t len;
448 char c;
450 len = read(fd, &c, 1);
451 if (len == -1 && errno == EINTR) {
452 continue;
455 if (len == -1 || len == 0) {
456 fprintf(stderr, "Broken pipe\n");
457 exit(1);
460 if (log) {
461 len = write(2, &c, 1);
463 json_message_parser_feed(&qmp.parser, &c, 1);
465 json_message_parser_destroy(&qmp.parser);
467 return qmp.response;
470 QDict *qtest_qmp_receive(QTestState *s)
472 return qmp_fd_receive(s->qmp_fd);
476 * Allow users to send a message without waiting for the reply,
477 * in the case that they choose to discard all replies up until
478 * a particular EVENT is received.
480 void qmp_fd_sendv(int fd, const char *fmt, va_list ap)
482 va_list ap_copy;
483 QObject *qobj;
485 /* qobject_from_jsonv() silently eats leading 0xff as invalid
486 * JSON, but we want to test sending them over the wire to force
487 * resyncs */
488 if (*fmt == '\377') {
489 socket_send(fd, fmt, 1);
490 fmt++;
493 /* Going through qobject ensures we escape strings properly.
494 * This seemingly unnecessary copy is required in case va_list
495 * is an array type.
497 va_copy(ap_copy, ap);
498 qobj = qobject_from_jsonv(fmt, &ap_copy, &error_abort);
499 va_end(ap_copy);
501 /* No need to send anything for an empty QObject. */
502 if (qobj) {
503 int log = getenv("QTEST_LOG") != NULL;
504 QString *qstr = qobject_to_json(qobj);
505 const char *str;
508 * BUG: QMP doesn't react to input until it sees a newline, an
509 * object, or an array. Work-around: give it a newline.
511 qstring_append_chr(qstr, '\n');
512 str = qstring_get_str(qstr);
514 if (log) {
515 fprintf(stderr, "%s", str);
517 /* Send QMP request */
518 socket_send(fd, str, qstring_get_length(qstr));
520 qobject_unref(qstr);
521 qobject_unref(qobj);
525 void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap)
527 qmp_fd_sendv(s->qmp_fd, fmt, ap);
530 QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
532 qmp_fd_sendv(fd, fmt, ap);
534 return qmp_fd_receive(fd);
537 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
539 qtest_async_qmpv(s, fmt, ap);
541 /* Receive reply */
542 return qtest_qmp_receive(s);
545 QDict *qmp_fd(int fd, const char *fmt, ...)
547 va_list ap;
548 QDict *response;
550 va_start(ap, fmt);
551 response = qmp_fdv(fd, fmt, ap);
552 va_end(ap);
553 return response;
556 void qmp_fd_send(int fd, const char *fmt, ...)
558 va_list ap;
560 va_start(ap, fmt);
561 qmp_fd_sendv(fd, fmt, ap);
562 va_end(ap);
565 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
567 va_list ap;
568 QDict *response;
570 va_start(ap, fmt);
571 response = qtest_qmpv(s, fmt, ap);
572 va_end(ap);
573 return response;
576 void qtest_async_qmp(QTestState *s, const char *fmt, ...)
578 va_list ap;
580 va_start(ap, fmt);
581 qtest_async_qmpv(s, fmt, ap);
582 va_end(ap);
585 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
587 QDict *response = qtest_qmpv(s, fmt, ap);
588 qobject_unref(response);
591 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
593 va_list ap;
594 QDict *response;
596 va_start(ap, fmt);
597 response = qtest_qmpv(s, fmt, ap);
598 va_end(ap);
599 qobject_unref(response);
602 QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
604 QDict *response;
606 for (;;) {
607 response = qtest_qmp_receive(s);
608 if ((qdict_haskey(response, "event")) &&
609 (strcmp(qdict_get_str(response, "event"), event) == 0)) {
610 return response;
612 qobject_unref(response);
616 void qtest_qmp_eventwait(QTestState *s, const char *event)
618 QDict *response;
620 response = qtest_qmp_eventwait_ref(s, event);
621 qobject_unref(response);
624 char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)
626 char *cmd;
627 QDict *resp;
628 char *ret;
630 cmd = g_strdup_vprintf(fmt, ap);
631 resp = qtest_qmp(s, "{'execute': 'human-monitor-command',"
632 " 'arguments': {'command-line': %s}}",
633 cmd);
634 ret = g_strdup(qdict_get_try_str(resp, "return"));
635 while (ret == NULL && qdict_get_try_str(resp, "event")) {
636 /* Ignore asynchronous QMP events */
637 qobject_unref(resp);
638 resp = qtest_qmp_receive(s);
639 ret = g_strdup(qdict_get_try_str(resp, "return"));
641 g_assert(ret);
642 qobject_unref(resp);
643 g_free(cmd);
644 return ret;
647 char *qtest_hmp(QTestState *s, const char *fmt, ...)
649 va_list ap;
650 char *ret;
652 va_start(ap, fmt);
653 ret = qtest_hmpv(s, fmt, ap);
654 va_end(ap);
655 return ret;
658 const char *qtest_get_arch(void)
660 const char *qemu = qtest_qemu_binary();
661 const char *end = strrchr(qemu, '/');
663 return end + strlen("/qemu-system-");
666 bool qtest_get_irq(QTestState *s, int num)
668 /* dummy operation in order to make sure irq is up to date */
669 qtest_inb(s, 0);
671 return s->irq_level[num];
674 static int64_t qtest_clock_rsp(QTestState *s)
676 gchar **words;
677 int64_t clock;
678 words = qtest_rsp(s, 2);
679 clock = g_ascii_strtoll(words[1], NULL, 0);
680 g_strfreev(words);
681 return clock;
684 int64_t qtest_clock_step_next(QTestState *s)
686 qtest_sendf(s, "clock_step\n");
687 return qtest_clock_rsp(s);
690 int64_t qtest_clock_step(QTestState *s, int64_t step)
692 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
693 return qtest_clock_rsp(s);
696 int64_t qtest_clock_set(QTestState *s, int64_t val)
698 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
699 return qtest_clock_rsp(s);
702 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
704 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
705 qtest_rsp(s, 0);
708 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
710 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
711 qtest_rsp(s, 0);
714 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
716 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
717 qtest_rsp(s, 0);
720 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
722 qtest_out(s, "outb", addr, value);
725 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
727 qtest_out(s, "outw", addr, value);
730 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
732 qtest_out(s, "outl", addr, value);
735 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
737 gchar **args;
738 int ret;
739 unsigned long value;
741 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
742 args = qtest_rsp(s, 2);
743 ret = qemu_strtoul(args[1], NULL, 0, &value);
744 g_assert(!ret && value <= UINT32_MAX);
745 g_strfreev(args);
747 return value;
750 uint8_t qtest_inb(QTestState *s, uint16_t addr)
752 return qtest_in(s, "inb", addr);
755 uint16_t qtest_inw(QTestState *s, uint16_t addr)
757 return qtest_in(s, "inw", addr);
760 uint32_t qtest_inl(QTestState *s, uint16_t addr)
762 return qtest_in(s, "inl", addr);
765 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
766 uint64_t value)
768 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
769 qtest_rsp(s, 0);
772 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
774 qtest_write(s, "writeb", addr, value);
777 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
779 qtest_write(s, "writew", addr, value);
782 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
784 qtest_write(s, "writel", addr, value);
787 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
789 qtest_write(s, "writeq", addr, value);
792 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
794 gchar **args;
795 int ret;
796 uint64_t value;
798 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
799 args = qtest_rsp(s, 2);
800 ret = qemu_strtou64(args[1], NULL, 0, &value);
801 g_assert(!ret);
802 g_strfreev(args);
804 return value;
807 uint8_t qtest_readb(QTestState *s, uint64_t addr)
809 return qtest_read(s, "readb", addr);
812 uint16_t qtest_readw(QTestState *s, uint64_t addr)
814 return qtest_read(s, "readw", addr);
817 uint32_t qtest_readl(QTestState *s, uint64_t addr)
819 return qtest_read(s, "readl", addr);
822 uint64_t qtest_readq(QTestState *s, uint64_t addr)
824 return qtest_read(s, "readq", addr);
827 static int hex2nib(char ch)
829 if (ch >= '0' && ch <= '9') {
830 return ch - '0';
831 } else if (ch >= 'a' && ch <= 'f') {
832 return 10 + (ch - 'a');
833 } else if (ch >= 'A' && ch <= 'F') {
834 return 10 + (ch - 'a');
835 } else {
836 return -1;
840 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
842 uint8_t *ptr = data;
843 gchar **args;
844 size_t i;
846 if (!size) {
847 return;
850 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
851 args = qtest_rsp(s, 2);
853 for (i = 0; i < size; i++) {
854 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
855 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
858 g_strfreev(args);
861 uint64_t qtest_rtas_call(QTestState *s, const char *name,
862 uint32_t nargs, uint64_t args,
863 uint32_t nret, uint64_t ret)
865 qtest_sendf(s, "rtas %s %u 0x%"PRIx64" %u 0x%"PRIx64"\n",
866 name, nargs, args, nret, ret);
867 qtest_rsp(s, 0);
868 return 0;
871 void qtest_add_func(const char *str, void (*fn)(void))
873 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
874 g_test_add_func(path, fn);
875 g_free(path);
878 void qtest_add_data_func_full(const char *str, void *data,
879 void (*fn)(const void *),
880 GDestroyNotify data_free_func)
882 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
883 g_test_add_data_func_full(path, data, fn, data_free_func);
884 g_free(path);
887 void qtest_add_data_func(const char *str, const void *data,
888 void (*fn)(const void *))
890 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
891 g_test_add_data_func(path, data, fn);
892 g_free(path);
895 void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
897 gchar *bdata;
899 bdata = g_base64_encode(data, size);
900 qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size);
901 socket_send(s->fd, bdata, strlen(bdata));
902 socket_send(s->fd, "\n", 1);
903 qtest_rsp(s, 0);
904 g_free(bdata);
907 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
909 gchar **args;
910 size_t len;
912 qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size);
913 args = qtest_rsp(s, 2);
915 g_base64_decode_inplace(args[1], &len);
916 if (size != len) {
917 fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n",
918 size, len);
919 len = MIN(len, size);
922 memcpy(data, args[1], len);
923 g_strfreev(args);
926 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
928 const uint8_t *ptr = data;
929 size_t i;
930 char *enc;
932 if (!size) {
933 return;
936 enc = g_malloc(2 * size + 1);
938 for (i = 0; i < size; i++) {
939 sprintf(&enc[i * 2], "%02x", ptr[i]);
942 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
943 qtest_rsp(s, 0);
944 g_free(enc);
947 void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
949 qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
950 qtest_rsp(s, 0);
953 QDict *qmp(const char *fmt, ...)
955 va_list ap;
956 QDict *response;
958 va_start(ap, fmt);
959 response = qtest_qmpv(global_qtest, fmt, ap);
960 va_end(ap);
961 return response;
964 void qmp_async(const char *fmt, ...)
966 va_list ap;
968 va_start(ap, fmt);
969 qtest_async_qmpv(global_qtest, fmt, ap);
970 va_end(ap);
973 void qmp_discard_response(const char *fmt, ...)
975 va_list ap;
977 va_start(ap, fmt);
978 qtest_qmpv_discard_response(global_qtest, fmt, ap);
979 va_end(ap);
981 char *hmp(const char *fmt, ...)
983 va_list ap;
984 char *ret;
986 va_start(ap, fmt);
987 ret = qtest_hmpv(global_qtest, fmt, ap);
988 va_end(ap);
989 return ret;
992 bool qtest_big_endian(QTestState *s)
994 return s->big_endian;
997 void qtest_cb_for_every_machine(void (*cb)(const char *machine))
999 QDict *response, *minfo;
1000 QList *list;
1001 const QListEntry *p;
1002 QObject *qobj;
1003 QString *qstr;
1004 const char *mname;
1006 qtest_start("-machine none");
1007 response = qmp("{ 'execute': 'query-machines' }");
1008 g_assert(response);
1009 list = qdict_get_qlist(response, "return");
1010 g_assert(list);
1012 for (p = qlist_first(list); p; p = qlist_next(p)) {
1013 minfo = qobject_to(QDict, qlist_entry_obj(p));
1014 g_assert(minfo);
1015 qobj = qdict_get(minfo, "name");
1016 g_assert(qobj);
1017 qstr = qobject_to(QString, qobj);
1018 g_assert(qstr);
1019 mname = qstring_get_str(qstr);
1020 cb(mname);
1023 qtest_end();
1024 qobject_unref(response);
1028 * Generic hot-plugging test via the device_add QMP command.
1030 void qtest_qmp_device_add(const char *driver, const char *id, const char *fmt,
1031 ...)
1033 QDict *response;
1034 char *cmd, *opts = NULL;
1035 va_list va;
1037 if (fmt) {
1038 va_start(va, fmt);
1039 opts = g_strdup_vprintf(fmt, va);
1040 va_end(va);
1043 cmd = g_strdup_printf("{'execute': 'device_add',"
1044 " 'arguments': { 'driver': '%s', 'id': '%s'%s%s }}",
1045 driver, id, opts ? ", " : "", opts ? opts : "");
1046 g_free(opts);
1048 response = qmp(cmd);
1049 g_free(cmd);
1050 g_assert(response);
1051 g_assert(!qdict_haskey(response, "event")); /* We don't expect any events */
1052 g_assert(!qdict_haskey(response, "error"));
1053 qobject_unref(response);
1057 * Generic hot-unplugging test via the device_del QMP command.
1058 * Device deletion will get one response and one event. For example:
1060 * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
1062 * will get this one:
1064 * {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
1065 * "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
1066 * "path": "/machine/peripheral/scsi-hd"}}
1068 * and this one:
1070 * {"return": {}}
1072 * But the order of arrival may vary - so we've got to detect both.
1074 void qtest_qmp_device_del(const char *id)
1076 QDict *response1, *response2, *event = NULL;
1077 char *cmd;
1079 cmd = g_strdup_printf("{'execute': 'device_del',"
1080 " 'arguments': { 'id': '%s' }}", id);
1081 response1 = qmp(cmd);
1082 g_free(cmd);
1083 g_assert(response1);
1084 g_assert(!qdict_haskey(response1, "error"));
1086 response2 = qmp("");
1087 g_assert(response2);
1088 g_assert(!qdict_haskey(response2, "error"));
1090 if (qdict_haskey(response1, "event")) {
1091 event = response1;
1092 } else if (qdict_haskey(response2, "event")) {
1093 event = response2;
1095 g_assert(event);
1096 g_assert_cmpstr(qdict_get_str(event, "event"), ==, "DEVICE_DELETED");
1098 qobject_unref(response1);
1099 qobject_unref(response2);
1102 bool qmp_rsp_is_err(QDict *rsp)
1104 QDict *error = qdict_get_qdict(rsp, "error");
1105 qobject_unref(rsp);
1106 return !!error;