net/vde: print error on vde_open() failure
[qemu/kevin.git] / tests / libqtest.c
blob200b2b9e92a6be21aa7c470709afc1f02220b46a
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(const char *extra_args)
171 QTestState *s;
172 int sock, qmpsock, i;
173 gchar *socket_path;
174 gchar *qmp_socket_path;
175 gchar *command;
176 const char *qemu_binary = qtest_qemu_binary();
178 s = g_new(QTestState, 1);
180 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
181 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
183 /* It's possible that if an earlier test run crashed it might
184 * have left a stale unix socket lying around. Delete any
185 * stale old socket to avoid spurious test failures with
186 * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
188 unlink(socket_path);
189 unlink(qmp_socket_path);
191 sock = init_socket(socket_path);
192 qmpsock = init_socket(qmp_socket_path);
194 qtest_add_abrt_handler(kill_qemu_hook_func, s);
196 s->qemu_pid = fork();
197 if (s->qemu_pid == 0) {
198 setenv("QEMU_AUDIO_DRV", "none", true);
199 command = g_strdup_printf("exec %s "
200 "-qtest unix:%s,nowait "
201 "-qtest-log %s "
202 "-qmp unix:%s,nowait "
203 "-machine accel=qtest "
204 "-display none "
205 "%s", qemu_binary, socket_path,
206 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
207 qmp_socket_path,
208 extra_args ?: "");
209 execlp("/bin/sh", "sh", "-c", command, NULL);
210 exit(1);
213 s->fd = socket_accept(sock);
214 if (s->fd >= 0) {
215 s->qmp_fd = socket_accept(qmpsock);
217 unlink(socket_path);
218 unlink(qmp_socket_path);
219 g_free(socket_path);
220 g_free(qmp_socket_path);
222 g_assert(s->fd >= 0 && s->qmp_fd >= 0);
224 s->rx = g_string_new("");
225 for (i = 0; i < MAX_IRQ; i++) {
226 s->irq_level[i] = false;
229 if (getenv("QTEST_STOP")) {
230 kill(s->qemu_pid, SIGSTOP);
233 /* ask endianness of the target */
235 s->big_endian = qtest_query_target_endianness(s);
237 return s;
240 QTestState *qtest_init(const char *extra_args)
242 QTestState *s = qtest_init_without_qmp_handshake(extra_args);
244 /* Read the QMP greeting and then do the handshake */
245 qtest_qmp_discard_response(s, "");
246 qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
248 return s;
251 QTestState *qtest_vstartf(const char *fmt, va_list ap)
253 char *args = g_strdup_vprintf(fmt, ap);
254 QTestState *s;
256 s = qtest_start(args);
257 g_free(args);
258 global_qtest = NULL;
259 return s;
262 QTestState *qtest_startf(const char *fmt, ...)
264 va_list ap;
265 QTestState *s;
267 va_start(ap, fmt);
268 s = qtest_vstartf(fmt, ap);
269 va_end(ap);
270 return s;
273 void qtest_quit(QTestState *s)
275 g_hook_destroy_link(&abrt_hooks, g_hook_find_data(&abrt_hooks, TRUE, s));
277 /* Uninstall SIGABRT handler on last instance */
278 cleanup_sigabrt_handler();
280 kill_qemu(s);
281 close(s->fd);
282 close(s->qmp_fd);
283 g_string_free(s->rx, true);
284 g_free(s);
287 static void socket_send(int fd, const char *buf, size_t size)
289 size_t offset;
291 offset = 0;
292 while (offset < size) {
293 ssize_t len;
295 len = write(fd, buf + offset, size - offset);
296 if (len == -1 && errno == EINTR) {
297 continue;
300 g_assert_no_errno(len);
301 g_assert_cmpint(len, >, 0);
303 offset += len;
307 static void socket_sendf(int fd, const char *fmt, va_list ap)
309 gchar *str = g_strdup_vprintf(fmt, ap);
310 size_t size = strlen(str);
312 socket_send(fd, str, size);
313 g_free(str);
316 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
318 va_list ap;
320 va_start(ap, fmt);
321 socket_sendf(s->fd, fmt, ap);
322 va_end(ap);
325 static GString *qtest_recv_line(QTestState *s)
327 GString *line;
328 size_t offset;
329 char *eol;
331 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
332 ssize_t len;
333 char buffer[1024];
335 len = read(s->fd, buffer, sizeof(buffer));
336 if (len == -1 && errno == EINTR) {
337 continue;
340 if (len == -1 || len == 0) {
341 fprintf(stderr, "Broken pipe\n");
342 exit(1);
345 g_string_append_len(s->rx, buffer, len);
348 offset = eol - s->rx->str;
349 line = g_string_new_len(s->rx->str, offset);
350 g_string_erase(s->rx, 0, offset + 1);
352 return line;
355 static gchar **qtest_rsp(QTestState *s, int expected_args)
357 GString *line;
358 gchar **words;
359 int i;
361 redo:
362 line = qtest_recv_line(s);
363 words = g_strsplit(line->str, " ", 0);
364 g_string_free(line, TRUE);
366 if (strcmp(words[0], "IRQ") == 0) {
367 long irq;
368 int ret;
370 g_assert(words[1] != NULL);
371 g_assert(words[2] != NULL);
373 ret = qemu_strtol(words[2], NULL, 0, &irq);
374 g_assert(!ret);
375 g_assert_cmpint(irq, >=, 0);
376 g_assert_cmpint(irq, <, MAX_IRQ);
378 if (strcmp(words[1], "raise") == 0) {
379 s->irq_level[irq] = true;
380 } else {
381 s->irq_level[irq] = false;
384 g_strfreev(words);
385 goto redo;
388 g_assert(words[0] != NULL);
389 g_assert_cmpstr(words[0], ==, "OK");
391 if (expected_args) {
392 for (i = 0; i < expected_args; i++) {
393 g_assert(words[i] != NULL);
395 } else {
396 g_strfreev(words);
399 return words;
402 static int qtest_query_target_endianness(QTestState *s)
404 gchar **args;
405 int big_endian;
407 qtest_sendf(s, "endianness\n");
408 args = qtest_rsp(s, 1);
409 g_assert(strcmp(args[1], "big") == 0 || strcmp(args[1], "little") == 0);
410 big_endian = strcmp(args[1], "big") == 0;
411 g_strfreev(args);
413 return big_endian;
416 typedef struct {
417 JSONMessageParser parser;
418 QDict *response;
419 } QMPResponseParser;
421 static void qmp_response(JSONMessageParser *parser, GQueue *tokens)
423 QMPResponseParser *qmp = container_of(parser, QMPResponseParser, parser);
424 QObject *obj;
426 obj = json_parser_parse(tokens, NULL);
427 if (!obj) {
428 fprintf(stderr, "QMP JSON response parsing failed\n");
429 exit(1);
432 g_assert(!qmp->response);
433 qmp->response = qobject_to(QDict, obj);
434 g_assert(qmp->response);
437 QDict *qmp_fd_receive(int fd)
439 QMPResponseParser qmp;
440 bool log = getenv("QTEST_LOG") != NULL;
442 qmp.response = NULL;
443 json_message_parser_init(&qmp.parser, qmp_response);
444 while (!qmp.response) {
445 ssize_t len;
446 char c;
448 len = read(fd, &c, 1);
449 if (len == -1 && errno == EINTR) {
450 continue;
453 if (len == -1 || len == 0) {
454 fprintf(stderr, "Broken pipe\n");
455 exit(1);
458 if (log) {
459 len = write(2, &c, 1);
461 json_message_parser_feed(&qmp.parser, &c, 1);
463 json_message_parser_destroy(&qmp.parser);
465 return qmp.response;
468 QDict *qtest_qmp_receive(QTestState *s)
470 return qmp_fd_receive(s->qmp_fd);
474 * Allow users to send a message without waiting for the reply,
475 * in the case that they choose to discard all replies up until
476 * a particular EVENT is received.
478 void qmp_fd_sendv(int fd, const char *fmt, va_list ap)
480 va_list ap_copy;
481 QObject *qobj;
483 /* qobject_from_jsonv() silently eats leading 0xff as invalid
484 * JSON, but we want to test sending them over the wire to force
485 * resyncs */
486 if (*fmt == '\377') {
487 socket_send(fd, fmt, 1);
488 fmt++;
491 /* Going through qobject ensures we escape strings properly.
492 * This seemingly unnecessary copy is required in case va_list
493 * is an array type.
495 va_copy(ap_copy, ap);
496 qobj = qobject_from_jsonv(fmt, &ap_copy, &error_abort);
497 va_end(ap_copy);
499 /* No need to send anything for an empty QObject. */
500 if (qobj) {
501 int log = getenv("QTEST_LOG") != NULL;
502 QString *qstr = qobject_to_json(qobj);
503 const char *str;
506 * BUG: QMP doesn't react to input until it sees a newline, an
507 * object, or an array. Work-around: give it a newline.
509 qstring_append_chr(qstr, '\n');
510 str = qstring_get_str(qstr);
512 if (log) {
513 fprintf(stderr, "%s", str);
515 /* Send QMP request */
516 socket_send(fd, str, qstring_get_length(qstr));
518 QDECREF(qstr);
519 qobject_decref(qobj);
523 void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap)
525 qmp_fd_sendv(s->qmp_fd, fmt, ap);
528 QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
530 qmp_fd_sendv(fd, fmt, ap);
532 return qmp_fd_receive(fd);
535 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
537 qtest_async_qmpv(s, fmt, ap);
539 /* Receive reply */
540 return qtest_qmp_receive(s);
543 QDict *qmp_fd(int fd, const char *fmt, ...)
545 va_list ap;
546 QDict *response;
548 va_start(ap, fmt);
549 response = qmp_fdv(fd, fmt, ap);
550 va_end(ap);
551 return response;
554 void qmp_fd_send(int fd, const char *fmt, ...)
556 va_list ap;
558 va_start(ap, fmt);
559 qmp_fd_sendv(fd, fmt, ap);
560 va_end(ap);
563 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
565 va_list ap;
566 QDict *response;
568 va_start(ap, fmt);
569 response = qtest_qmpv(s, fmt, ap);
570 va_end(ap);
571 return response;
574 void qtest_async_qmp(QTestState *s, const char *fmt, ...)
576 va_list ap;
578 va_start(ap, fmt);
579 qtest_async_qmpv(s, fmt, ap);
580 va_end(ap);
583 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
585 QDict *response = qtest_qmpv(s, fmt, ap);
586 QDECREF(response);
589 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
591 va_list ap;
592 QDict *response;
594 va_start(ap, fmt);
595 response = qtest_qmpv(s, fmt, ap);
596 va_end(ap);
597 QDECREF(response);
600 QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
602 QDict *response;
604 for (;;) {
605 response = qtest_qmp_receive(s);
606 if ((qdict_haskey(response, "event")) &&
607 (strcmp(qdict_get_str(response, "event"), event) == 0)) {
608 return response;
610 QDECREF(response);
614 void qtest_qmp_eventwait(QTestState *s, const char *event)
616 QDict *response;
618 response = qtest_qmp_eventwait_ref(s, event);
619 QDECREF(response);
622 char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)
624 char *cmd;
625 QDict *resp;
626 char *ret;
628 cmd = g_strdup_vprintf(fmt, ap);
629 resp = qtest_qmp(s, "{'execute': 'human-monitor-command',"
630 " 'arguments': {'command-line': %s}}",
631 cmd);
632 ret = g_strdup(qdict_get_try_str(resp, "return"));
633 while (ret == NULL && qdict_get_try_str(resp, "event")) {
634 /* Ignore asynchronous QMP events */
635 QDECREF(resp);
636 resp = qtest_qmp_receive(s);
637 ret = g_strdup(qdict_get_try_str(resp, "return"));
639 g_assert(ret);
640 QDECREF(resp);
641 g_free(cmd);
642 return ret;
645 char *qtest_hmp(QTestState *s, const char *fmt, ...)
647 va_list ap;
648 char *ret;
650 va_start(ap, fmt);
651 ret = qtest_hmpv(s, fmt, ap);
652 va_end(ap);
653 return ret;
656 const char *qtest_get_arch(void)
658 const char *qemu = qtest_qemu_binary();
659 const char *end = strrchr(qemu, '/');
661 return end + strlen("/qemu-system-");
664 bool qtest_get_irq(QTestState *s, int num)
666 /* dummy operation in order to make sure irq is up to date */
667 qtest_inb(s, 0);
669 return s->irq_level[num];
672 static int64_t qtest_clock_rsp(QTestState *s)
674 gchar **words;
675 int64_t clock;
676 words = qtest_rsp(s, 2);
677 clock = g_ascii_strtoll(words[1], NULL, 0);
678 g_strfreev(words);
679 return clock;
682 int64_t qtest_clock_step_next(QTestState *s)
684 qtest_sendf(s, "clock_step\n");
685 return qtest_clock_rsp(s);
688 int64_t qtest_clock_step(QTestState *s, int64_t step)
690 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
691 return qtest_clock_rsp(s);
694 int64_t qtest_clock_set(QTestState *s, int64_t val)
696 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
697 return qtest_clock_rsp(s);
700 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
702 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
703 qtest_rsp(s, 0);
706 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
708 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
709 qtest_rsp(s, 0);
712 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
714 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
715 qtest_rsp(s, 0);
718 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
720 qtest_out(s, "outb", addr, value);
723 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
725 qtest_out(s, "outw", addr, value);
728 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
730 qtest_out(s, "outl", addr, value);
733 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
735 gchar **args;
736 int ret;
737 unsigned long value;
739 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
740 args = qtest_rsp(s, 2);
741 ret = qemu_strtoul(args[1], NULL, 0, &value);
742 g_assert(!ret && value <= UINT32_MAX);
743 g_strfreev(args);
745 return value;
748 uint8_t qtest_inb(QTestState *s, uint16_t addr)
750 return qtest_in(s, "inb", addr);
753 uint16_t qtest_inw(QTestState *s, uint16_t addr)
755 return qtest_in(s, "inw", addr);
758 uint32_t qtest_inl(QTestState *s, uint16_t addr)
760 return qtest_in(s, "inl", addr);
763 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
764 uint64_t value)
766 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
767 qtest_rsp(s, 0);
770 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
772 qtest_write(s, "writeb", addr, value);
775 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
777 qtest_write(s, "writew", addr, value);
780 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
782 qtest_write(s, "writel", addr, value);
785 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
787 qtest_write(s, "writeq", addr, value);
790 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
792 gchar **args;
793 int ret;
794 uint64_t value;
796 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
797 args = qtest_rsp(s, 2);
798 ret = qemu_strtou64(args[1], NULL, 0, &value);
799 g_assert(!ret);
800 g_strfreev(args);
802 return value;
805 uint8_t qtest_readb(QTestState *s, uint64_t addr)
807 return qtest_read(s, "readb", addr);
810 uint16_t qtest_readw(QTestState *s, uint64_t addr)
812 return qtest_read(s, "readw", addr);
815 uint32_t qtest_readl(QTestState *s, uint64_t addr)
817 return qtest_read(s, "readl", addr);
820 uint64_t qtest_readq(QTestState *s, uint64_t addr)
822 return qtest_read(s, "readq", addr);
825 static int hex2nib(char ch)
827 if (ch >= '0' && ch <= '9') {
828 return ch - '0';
829 } else if (ch >= 'a' && ch <= 'f') {
830 return 10 + (ch - 'a');
831 } else if (ch >= 'A' && ch <= 'F') {
832 return 10 + (ch - 'a');
833 } else {
834 return -1;
838 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
840 uint8_t *ptr = data;
841 gchar **args;
842 size_t i;
844 if (!size) {
845 return;
848 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
849 args = qtest_rsp(s, 2);
851 for (i = 0; i < size; i++) {
852 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
853 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
856 g_strfreev(args);
859 uint64_t qtest_rtas_call(QTestState *s, const char *name,
860 uint32_t nargs, uint64_t args,
861 uint32_t nret, uint64_t ret)
863 qtest_sendf(s, "rtas %s %u 0x%"PRIx64" %u 0x%"PRIx64"\n",
864 name, nargs, args, nret, ret);
865 qtest_rsp(s, 0);
866 return 0;
869 void qtest_add_func(const char *str, void (*fn)(void))
871 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
872 g_test_add_func(path, fn);
873 g_free(path);
876 void qtest_add_data_func_full(const char *str, void *data,
877 void (*fn)(const void *),
878 GDestroyNotify data_free_func)
880 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
881 g_test_add_data_func_full(path, data, fn, data_free_func);
882 g_free(path);
885 void qtest_add_data_func(const char *str, const void *data,
886 void (*fn)(const void *))
888 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
889 g_test_add_data_func(path, data, fn);
890 g_free(path);
893 void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
895 gchar *bdata;
897 bdata = g_base64_encode(data, size);
898 qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size);
899 socket_send(s->fd, bdata, strlen(bdata));
900 socket_send(s->fd, "\n", 1);
901 qtest_rsp(s, 0);
902 g_free(bdata);
905 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
907 gchar **args;
908 size_t len;
910 qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size);
911 args = qtest_rsp(s, 2);
913 g_base64_decode_inplace(args[1], &len);
914 if (size != len) {
915 fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n",
916 size, len);
917 len = MIN(len, size);
920 memcpy(data, args[1], len);
921 g_strfreev(args);
924 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
926 const uint8_t *ptr = data;
927 size_t i;
928 char *enc;
930 if (!size) {
931 return;
934 enc = g_malloc(2 * size + 1);
936 for (i = 0; i < size; i++) {
937 sprintf(&enc[i * 2], "%02x", ptr[i]);
940 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
941 qtest_rsp(s, 0);
942 g_free(enc);
945 void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
947 qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
948 qtest_rsp(s, 0);
951 QDict *qmp(const char *fmt, ...)
953 va_list ap;
954 QDict *response;
956 va_start(ap, fmt);
957 response = qtest_qmpv(global_qtest, fmt, ap);
958 va_end(ap);
959 return response;
962 void qmp_async(const char *fmt, ...)
964 va_list ap;
966 va_start(ap, fmt);
967 qtest_async_qmpv(global_qtest, fmt, ap);
968 va_end(ap);
971 void qmp_discard_response(const char *fmt, ...)
973 va_list ap;
975 va_start(ap, fmt);
976 qtest_qmpv_discard_response(global_qtest, fmt, ap);
977 va_end(ap);
979 char *hmp(const char *fmt, ...)
981 va_list ap;
982 char *ret;
984 va_start(ap, fmt);
985 ret = qtest_hmpv(global_qtest, fmt, ap);
986 va_end(ap);
987 return ret;
990 bool qtest_big_endian(QTestState *s)
992 return s->big_endian;
995 void qtest_cb_for_every_machine(void (*cb)(const char *machine))
997 QDict *response, *minfo;
998 QList *list;
999 const QListEntry *p;
1000 QObject *qobj;
1001 QString *qstr;
1002 const char *mname;
1004 qtest_start("-machine none");
1005 response = qmp("{ 'execute': 'query-machines' }");
1006 g_assert(response);
1007 list = qdict_get_qlist(response, "return");
1008 g_assert(list);
1010 for (p = qlist_first(list); p; p = qlist_next(p)) {
1011 minfo = qobject_to(QDict, qlist_entry_obj(p));
1012 g_assert(minfo);
1013 qobj = qdict_get(minfo, "name");
1014 g_assert(qobj);
1015 qstr = qobject_to(QString, qobj);
1016 g_assert(qstr);
1017 mname = qstring_get_str(qstr);
1018 cb(mname);
1021 qtest_end();
1022 QDECREF(response);
1026 * Generic hot-plugging test via the device_add QMP command.
1028 void qtest_qmp_device_add(const char *driver, const char *id, const char *fmt,
1029 ...)
1031 QDict *response;
1032 char *cmd, *opts = NULL;
1033 va_list va;
1035 if (fmt) {
1036 va_start(va, fmt);
1037 opts = g_strdup_vprintf(fmt, va);
1038 va_end(va);
1041 cmd = g_strdup_printf("{'execute': 'device_add',"
1042 " 'arguments': { 'driver': '%s', 'id': '%s'%s%s }}",
1043 driver, id, opts ? ", " : "", opts ? opts : "");
1044 g_free(opts);
1046 response = qmp(cmd);
1047 g_free(cmd);
1048 g_assert(response);
1049 g_assert(!qdict_haskey(response, "event")); /* We don't expect any events */
1050 g_assert(!qdict_haskey(response, "error"));
1051 QDECREF(response);
1055 * Generic hot-unplugging test via the device_del QMP command.
1056 * Device deletion will get one response and one event. For example:
1058 * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
1060 * will get this one:
1062 * {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
1063 * "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
1064 * "path": "/machine/peripheral/scsi-hd"}}
1066 * and this one:
1068 * {"return": {}}
1070 * But the order of arrival may vary - so we've got to detect both.
1072 void qtest_qmp_device_del(const char *id)
1074 QDict *response1, *response2, *event = NULL;
1075 char *cmd;
1077 cmd = g_strdup_printf("{'execute': 'device_del',"
1078 " 'arguments': { 'id': '%s' }}", id);
1079 response1 = qmp(cmd);
1080 g_free(cmd);
1081 g_assert(response1);
1082 g_assert(!qdict_haskey(response1, "error"));
1084 response2 = qmp("");
1085 g_assert(response2);
1086 g_assert(!qdict_haskey(response2, "error"));
1088 if (qdict_haskey(response1, "event")) {
1089 event = response1;
1090 } else if (qdict_haskey(response2, "event")) {
1091 event = response2;
1093 g_assert(event);
1094 g_assert_cmpstr(qdict_get_str(event, "event"), ==, "DEVICE_DELETED");
1096 QDECREF(response1);
1097 QDECREF(response2);