dmg: including dmg-lzfse module inside dmg block driver.
[qemu/ar7.git] / tests / libqtest.c
blob43be078518c86815354f1857c42d45e4f40948b4
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-common.h"
25 #include "qemu/cutils.h"
26 #include "qapi/error.h"
27 #include "qapi/qmp/json-parser.h"
28 #include "qapi/qmp/qdict.h"
29 #include "qapi/qmp/qjson.h"
30 #include "qapi/qmp/qlist.h"
31 #include "qapi/qmp/qstring.h"
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 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_cmpint(sock, !=, -1);
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_cmpint(ret, !=, -1);
70 ret = listen(sock, 1);
71 g_assert_cmpint(ret, !=, -1);
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 int wstatus = 0;
103 pid_t pid;
105 kill(s->qemu_pid, SIGTERM);
106 TFR(pid = waitpid(s->qemu_pid, &wstatus, 0));
108 assert(pid == s->qemu_pid);
110 * We expect qemu to exit with status 0; anything else is
111 * fishy and should be logged with as much detail as possible.
113 if (wstatus) {
114 if (WIFEXITED(wstatus)) {
115 fprintf(stderr, "%s:%d: kill_qemu() tried to terminate QEMU "
116 "process but encountered exit status %d\n",
117 __FILE__, __LINE__, WEXITSTATUS(wstatus));
118 } else if (WIFSIGNALED(wstatus)) {
119 int sig = WTERMSIG(wstatus);
120 const char *signame = strsignal(sig) ?: "unknown ???";
121 const char *dump = WCOREDUMP(wstatus) ? " (core dumped)" : "";
123 fprintf(stderr, "%s:%d: kill_qemu() detected QEMU death "
124 "from signal %d (%s)%s\n",
125 __FILE__, __LINE__, sig, signame, dump);
127 abort();
132 static void kill_qemu_hook_func(void *s)
134 kill_qemu(s);
137 static void sigabrt_handler(int signo)
139 g_hook_list_invoke(&abrt_hooks, FALSE);
142 static void setup_sigabrt_handler(void)
144 struct sigaction sigact;
146 /* Catch SIGABRT to clean up on g_assert() failure */
147 sigact = (struct sigaction){
148 .sa_handler = sigabrt_handler,
149 .sa_flags = SA_RESETHAND,
151 sigemptyset(&sigact.sa_mask);
152 sigaction(SIGABRT, &sigact, &sigact_old);
155 static void cleanup_sigabrt_handler(void)
157 sigaction(SIGABRT, &sigact_old, NULL);
160 void qtest_add_abrt_handler(GHookFunc fn, const void *data)
162 GHook *hook;
164 /* Only install SIGABRT handler once */
165 if (!abrt_hooks.is_setup) {
166 g_hook_list_init(&abrt_hooks, sizeof(GHook));
168 setup_sigabrt_handler();
170 hook = g_hook_alloc(&abrt_hooks);
171 hook->func = fn;
172 hook->data = (void *)data;
174 g_hook_prepend(&abrt_hooks, hook);
177 static const char *qtest_qemu_binary(void)
179 const char *qemu_bin;
181 qemu_bin = getenv("QTEST_QEMU_BINARY");
182 if (!qemu_bin) {
183 fprintf(stderr, "Environment variable QTEST_QEMU_BINARY required\n");
184 exit(1);
187 return qemu_bin;
190 QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
192 QTestState *s;
193 int sock, qmpsock, i;
194 gchar *socket_path;
195 gchar *qmp_socket_path;
196 gchar *command;
197 const char *qemu_binary = qtest_qemu_binary();
199 s = g_new(QTestState, 1);
201 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
202 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
204 /* It's possible that if an earlier test run crashed it might
205 * have left a stale unix socket lying around. Delete any
206 * stale old socket to avoid spurious test failures with
207 * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
209 unlink(socket_path);
210 unlink(qmp_socket_path);
212 sock = init_socket(socket_path);
213 qmpsock = init_socket(qmp_socket_path);
215 qtest_add_abrt_handler(kill_qemu_hook_func, s);
217 command = g_strdup_printf("exec %s "
218 "-qtest unix:%s,nowait "
219 "-qtest-log %s "
220 "-chardev socket,path=%s,nowait,id=char0 "
221 "-mon chardev=char0,mode=control "
222 "-machine accel=qtest "
223 "-display none "
224 "%s", qemu_binary, socket_path,
225 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
226 qmp_socket_path,
227 extra_args ?: "");
229 g_test_message("starting QEMU: %s", command);
231 s->qemu_pid = fork();
232 if (s->qemu_pid == 0) {
233 setenv("QEMU_AUDIO_DRV", "none", true);
234 execlp("/bin/sh", "sh", "-c", command, NULL);
235 exit(1);
238 g_free(command);
239 s->fd = socket_accept(sock);
240 if (s->fd >= 0) {
241 s->qmp_fd = socket_accept(qmpsock);
243 unlink(socket_path);
244 unlink(qmp_socket_path);
245 g_free(socket_path);
246 g_free(qmp_socket_path);
248 g_assert(s->fd >= 0 && s->qmp_fd >= 0);
250 s->rx = g_string_new("");
251 for (i = 0; i < MAX_IRQ; i++) {
252 s->irq_level[i] = false;
255 if (getenv("QTEST_STOP")) {
256 kill(s->qemu_pid, SIGSTOP);
259 /* ask endianness of the target */
261 s->big_endian = qtest_query_target_endianness(s);
263 return s;
266 QTestState *qtest_init(const char *extra_args)
268 QTestState *s = qtest_init_without_qmp_handshake(extra_args);
269 QDict *greeting;
271 /* Read the QMP greeting and then do the handshake */
272 greeting = qtest_qmp_receive(s);
273 qobject_unref(greeting);
274 qobject_unref(qtest_qmp(s, "{ 'execute': 'qmp_capabilities' }"));
276 return s;
279 QTestState *qtest_vinitf(const char *fmt, va_list ap)
281 char *args = g_strdup_vprintf(fmt, ap);
282 QTestState *s;
284 s = qtest_init(args);
285 g_free(args);
286 return s;
289 QTestState *qtest_initf(const char *fmt, ...)
291 va_list ap;
292 QTestState *s;
294 va_start(ap, fmt);
295 s = qtest_vinitf(fmt, ap);
296 va_end(ap);
297 return s;
300 void qtest_quit(QTestState *s)
302 g_hook_destroy_link(&abrt_hooks, g_hook_find_data(&abrt_hooks, TRUE, s));
304 /* Uninstall SIGABRT handler on last instance */
305 cleanup_sigabrt_handler();
307 kill_qemu(s);
308 close(s->fd);
309 close(s->qmp_fd);
310 g_string_free(s->rx, true);
311 g_free(s);
314 static void socket_send(int fd, const char *buf, size_t size)
316 size_t offset;
318 offset = 0;
319 while (offset < size) {
320 ssize_t len;
322 len = write(fd, buf + offset, size - offset);
323 if (len == -1 && errno == EINTR) {
324 continue;
327 g_assert_cmpint(len, >, 0);
329 offset += len;
333 static void socket_sendf(int fd, const char *fmt, va_list ap)
335 gchar *str = g_strdup_vprintf(fmt, ap);
336 size_t size = strlen(str);
338 socket_send(fd, str, size);
339 g_free(str);
342 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
344 va_list ap;
346 va_start(ap, fmt);
347 socket_sendf(s->fd, fmt, ap);
348 va_end(ap);
351 static GString *qtest_recv_line(QTestState *s)
353 GString *line;
354 size_t offset;
355 char *eol;
357 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
358 ssize_t len;
359 char buffer[1024];
361 len = read(s->fd, buffer, sizeof(buffer));
362 if (len == -1 && errno == EINTR) {
363 continue;
366 if (len == -1 || len == 0) {
367 fprintf(stderr, "Broken pipe\n");
368 abort();
371 g_string_append_len(s->rx, buffer, len);
374 offset = eol - s->rx->str;
375 line = g_string_new_len(s->rx->str, offset);
376 g_string_erase(s->rx, 0, offset + 1);
378 return line;
381 static gchar **qtest_rsp(QTestState *s, int expected_args)
383 GString *line;
384 gchar **words;
385 int i;
387 redo:
388 line = qtest_recv_line(s);
389 words = g_strsplit(line->str, " ", 0);
390 g_string_free(line, TRUE);
392 if (strcmp(words[0], "IRQ") == 0) {
393 long irq;
394 int ret;
396 g_assert(words[1] != NULL);
397 g_assert(words[2] != NULL);
399 ret = qemu_strtol(words[2], NULL, 0, &irq);
400 g_assert(!ret);
401 g_assert_cmpint(irq, >=, 0);
402 g_assert_cmpint(irq, <, MAX_IRQ);
404 if (strcmp(words[1], "raise") == 0) {
405 s->irq_level[irq] = true;
406 } else {
407 s->irq_level[irq] = false;
410 g_strfreev(words);
411 goto redo;
414 g_assert(words[0] != NULL);
415 g_assert_cmpstr(words[0], ==, "OK");
417 if (expected_args) {
418 for (i = 0; i < expected_args; i++) {
419 g_assert(words[i] != NULL);
421 } else {
422 g_strfreev(words);
425 return words;
428 static int qtest_query_target_endianness(QTestState *s)
430 gchar **args;
431 int big_endian;
433 qtest_sendf(s, "endianness\n");
434 args = qtest_rsp(s, 1);
435 g_assert(strcmp(args[1], "big") == 0 || strcmp(args[1], "little") == 0);
436 big_endian = strcmp(args[1], "big") == 0;
437 g_strfreev(args);
439 return big_endian;
442 typedef struct {
443 JSONMessageParser parser;
444 QDict *response;
445 } QMPResponseParser;
447 static void qmp_response(void *opaque, QObject *obj, Error *err)
449 QMPResponseParser *qmp = opaque;
451 assert(!obj != !err);
453 if (err) {
454 error_prepend(&err, "QMP JSON response parsing failed: ");
455 error_report_err(err);
456 abort();
459 g_assert(!qmp->response);
460 qmp->response = qobject_to(QDict, obj);
461 g_assert(qmp->response);
464 QDict *qmp_fd_receive(int fd)
466 QMPResponseParser qmp;
467 bool log = getenv("QTEST_LOG") != NULL;
469 qmp.response = NULL;
470 json_message_parser_init(&qmp.parser, qmp_response, &qmp, NULL);
471 while (!qmp.response) {
472 ssize_t len;
473 char c;
475 len = read(fd, &c, 1);
476 if (len == -1 && errno == EINTR) {
477 continue;
480 if (len == -1 || len == 0) {
481 fprintf(stderr, "Broken pipe\n");
482 abort();
485 if (log) {
486 len = write(2, &c, 1);
488 json_message_parser_feed(&qmp.parser, &c, 1);
490 json_message_parser_destroy(&qmp.parser);
492 return qmp.response;
495 QDict *qtest_qmp_receive(QTestState *s)
497 return qmp_fd_receive(s->qmp_fd);
501 * Allow users to send a message without waiting for the reply,
502 * in the case that they choose to discard all replies up until
503 * a particular EVENT is received.
505 void qmp_fd_vsend(int fd, const char *fmt, va_list ap)
507 QObject *qobj;
509 /* Going through qobject ensures we escape strings properly */
510 qobj = qobject_from_vjsonf_nofail(fmt, ap);
512 /* No need to send anything for an empty QObject. */
513 if (qobj) {
514 int log = getenv("QTEST_LOG") != NULL;
515 QString *qstr = qobject_to_json(qobj);
516 const char *str;
519 * BUG: QMP doesn't react to input until it sees a newline, an
520 * object, or an array. Work-around: give it a newline.
522 qstring_append_chr(qstr, '\n');
523 str = qstring_get_str(qstr);
525 if (log) {
526 fprintf(stderr, "%s", str);
528 /* Send QMP request */
529 socket_send(fd, str, qstring_get_length(qstr));
531 qobject_unref(qstr);
532 qobject_unref(qobj);
536 void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap)
538 qmp_fd_vsend(s->qmp_fd, fmt, ap);
541 QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
543 qmp_fd_vsend(fd, fmt, ap);
545 return qmp_fd_receive(fd);
548 QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap)
550 qtest_qmp_vsend(s, fmt, ap);
552 /* Receive reply */
553 return qtest_qmp_receive(s);
556 QDict *qmp_fd(int fd, const char *fmt, ...)
558 va_list ap;
559 QDict *response;
561 va_start(ap, fmt);
562 response = qmp_fdv(fd, fmt, ap);
563 va_end(ap);
564 return response;
567 void qmp_fd_send(int fd, const char *fmt, ...)
569 va_list ap;
571 va_start(ap, fmt);
572 qmp_fd_vsend(fd, fmt, ap);
573 va_end(ap);
576 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
578 va_list ap;
579 QDict *response;
581 va_start(ap, fmt);
582 response = qtest_vqmp(s, fmt, ap);
583 va_end(ap);
584 return response;
587 void qtest_qmp_send(QTestState *s, const char *fmt, ...)
589 va_list ap;
591 va_start(ap, fmt);
592 qtest_qmp_vsend(s, fmt, ap);
593 va_end(ap);
596 void qmp_fd_vsend_raw(int fd, const char *fmt, va_list ap)
598 bool log = getenv("QTEST_LOG") != NULL;
599 char *str = g_strdup_vprintf(fmt, ap);
601 if (log) {
602 fprintf(stderr, "%s", str);
604 socket_send(fd, str, strlen(str));
605 g_free(str);
608 void qmp_fd_send_raw(int fd, const char *fmt, ...)
610 va_list ap;
612 va_start(ap, fmt);
613 qmp_fd_vsend_raw(fd, fmt, ap);
614 va_end(ap);
617 void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...)
619 va_list ap;
621 va_start(ap, fmt);
622 qmp_fd_vsend_raw(s->qmp_fd, fmt, ap);
623 va_end(ap);
626 QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
628 QDict *response;
630 for (;;) {
631 response = qtest_qmp_receive(s);
632 if ((qdict_haskey(response, "event")) &&
633 (strcmp(qdict_get_str(response, "event"), event) == 0)) {
634 return response;
636 qobject_unref(response);
640 void qtest_qmp_eventwait(QTestState *s, const char *event)
642 QDict *response;
644 response = qtest_qmp_eventwait_ref(s, event);
645 qobject_unref(response);
648 char *qtest_vhmp(QTestState *s, const char *fmt, va_list ap)
650 char *cmd;
651 QDict *resp;
652 char *ret;
654 cmd = g_strdup_vprintf(fmt, ap);
655 resp = qtest_qmp(s, "{'execute': 'human-monitor-command',"
656 " 'arguments': {'command-line': %s}}",
657 cmd);
658 ret = g_strdup(qdict_get_try_str(resp, "return"));
659 while (ret == NULL && qdict_get_try_str(resp, "event")) {
660 /* Ignore asynchronous QMP events */
661 qobject_unref(resp);
662 resp = qtest_qmp_receive(s);
663 ret = g_strdup(qdict_get_try_str(resp, "return"));
665 g_assert(ret);
666 qobject_unref(resp);
667 g_free(cmd);
668 return ret;
671 char *qtest_hmp(QTestState *s, const char *fmt, ...)
673 va_list ap;
674 char *ret;
676 va_start(ap, fmt);
677 ret = qtest_vhmp(s, fmt, ap);
678 va_end(ap);
679 return ret;
682 const char *qtest_get_arch(void)
684 const char *qemu = qtest_qemu_binary();
685 const char *end = strrchr(qemu, '/');
687 return end + strlen("/qemu-system-");
690 bool qtest_get_irq(QTestState *s, int num)
692 /* dummy operation in order to make sure irq is up to date */
693 qtest_inb(s, 0);
695 return s->irq_level[num];
698 static int64_t qtest_clock_rsp(QTestState *s)
700 gchar **words;
701 int64_t clock;
702 words = qtest_rsp(s, 2);
703 clock = g_ascii_strtoll(words[1], NULL, 0);
704 g_strfreev(words);
705 return clock;
708 int64_t qtest_clock_step_next(QTestState *s)
710 qtest_sendf(s, "clock_step\n");
711 return qtest_clock_rsp(s);
714 int64_t qtest_clock_step(QTestState *s, int64_t step)
716 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
717 return qtest_clock_rsp(s);
720 int64_t qtest_clock_set(QTestState *s, int64_t val)
722 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
723 return qtest_clock_rsp(s);
726 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
728 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
729 qtest_rsp(s, 0);
732 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
734 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
735 qtest_rsp(s, 0);
738 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
740 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
741 qtest_rsp(s, 0);
744 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
746 qtest_out(s, "outb", addr, value);
749 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
751 qtest_out(s, "outw", addr, value);
754 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
756 qtest_out(s, "outl", addr, value);
759 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
761 gchar **args;
762 int ret;
763 unsigned long value;
765 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
766 args = qtest_rsp(s, 2);
767 ret = qemu_strtoul(args[1], NULL, 0, &value);
768 g_assert(!ret && value <= UINT32_MAX);
769 g_strfreev(args);
771 return value;
774 uint8_t qtest_inb(QTestState *s, uint16_t addr)
776 return qtest_in(s, "inb", addr);
779 uint16_t qtest_inw(QTestState *s, uint16_t addr)
781 return qtest_in(s, "inw", addr);
784 uint32_t qtest_inl(QTestState *s, uint16_t addr)
786 return qtest_in(s, "inl", addr);
789 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
790 uint64_t value)
792 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
793 qtest_rsp(s, 0);
796 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
798 qtest_write(s, "writeb", addr, value);
801 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
803 qtest_write(s, "writew", addr, value);
806 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
808 qtest_write(s, "writel", addr, value);
811 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
813 qtest_write(s, "writeq", addr, value);
816 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
818 gchar **args;
819 int ret;
820 uint64_t value;
822 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
823 args = qtest_rsp(s, 2);
824 ret = qemu_strtou64(args[1], NULL, 0, &value);
825 g_assert(!ret);
826 g_strfreev(args);
828 return value;
831 uint8_t qtest_readb(QTestState *s, uint64_t addr)
833 return qtest_read(s, "readb", addr);
836 uint16_t qtest_readw(QTestState *s, uint64_t addr)
838 return qtest_read(s, "readw", addr);
841 uint32_t qtest_readl(QTestState *s, uint64_t addr)
843 return qtest_read(s, "readl", addr);
846 uint64_t qtest_readq(QTestState *s, uint64_t addr)
848 return qtest_read(s, "readq", addr);
851 static int hex2nib(char ch)
853 if (ch >= '0' && ch <= '9') {
854 return ch - '0';
855 } else if (ch >= 'a' && ch <= 'f') {
856 return 10 + (ch - 'a');
857 } else if (ch >= 'A' && ch <= 'F') {
858 return 10 + (ch - 'a');
859 } else {
860 return -1;
864 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
866 uint8_t *ptr = data;
867 gchar **args;
868 size_t i;
870 if (!size) {
871 return;
874 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
875 args = qtest_rsp(s, 2);
877 for (i = 0; i < size; i++) {
878 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
879 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
882 g_strfreev(args);
885 uint64_t qtest_rtas_call(QTestState *s, const char *name,
886 uint32_t nargs, uint64_t args,
887 uint32_t nret, uint64_t ret)
889 qtest_sendf(s, "rtas %s %u 0x%"PRIx64" %u 0x%"PRIx64"\n",
890 name, nargs, args, nret, ret);
891 qtest_rsp(s, 0);
892 return 0;
895 void qtest_add_func(const char *str, void (*fn)(void))
897 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
898 g_test_add_func(path, fn);
899 g_free(path);
902 void qtest_add_data_func_full(const char *str, void *data,
903 void (*fn)(const void *),
904 GDestroyNotify data_free_func)
906 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
907 g_test_add_data_func_full(path, data, fn, data_free_func);
908 g_free(path);
911 void qtest_add_data_func(const char *str, const void *data,
912 void (*fn)(const void *))
914 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
915 g_test_add_data_func(path, data, fn);
916 g_free(path);
919 void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
921 gchar *bdata;
923 bdata = g_base64_encode(data, size);
924 qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size);
925 socket_send(s->fd, bdata, strlen(bdata));
926 socket_send(s->fd, "\n", 1);
927 qtest_rsp(s, 0);
928 g_free(bdata);
931 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
933 gchar **args;
934 size_t len;
936 qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size);
937 args = qtest_rsp(s, 2);
939 g_base64_decode_inplace(args[1], &len);
940 if (size != len) {
941 fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n",
942 size, len);
943 len = MIN(len, size);
946 memcpy(data, args[1], len);
947 g_strfreev(args);
950 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
952 const uint8_t *ptr = data;
953 size_t i;
954 char *enc;
956 if (!size) {
957 return;
960 enc = g_malloc(2 * size + 1);
962 for (i = 0; i < size; i++) {
963 sprintf(&enc[i * 2], "%02x", ptr[i]);
966 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
967 qtest_rsp(s, 0);
968 g_free(enc);
971 void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
973 qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
974 qtest_rsp(s, 0);
977 QDict *qmp(const char *fmt, ...)
979 va_list ap;
980 QDict *response;
982 va_start(ap, fmt);
983 response = qtest_vqmp(global_qtest, fmt, ap);
984 va_end(ap);
985 return response;
988 void qmp_send(const char *fmt, ...)
990 va_list ap;
992 va_start(ap, fmt);
993 qtest_qmp_vsend(global_qtest, fmt, ap);
994 va_end(ap);
997 char *hmp(const char *fmt, ...)
999 va_list ap;
1000 char *ret;
1002 va_start(ap, fmt);
1003 ret = qtest_vhmp(global_qtest, fmt, ap);
1004 va_end(ap);
1005 return ret;
1008 bool qtest_big_endian(QTestState *s)
1010 return s->big_endian;
1013 static bool qtest_check_machine_version(const char *mname, const char *basename,
1014 int major, int minor)
1016 char *newname;
1017 bool is_equal;
1019 newname = g_strdup_printf("%s-%i.%i", basename, major, minor);
1020 is_equal = g_str_equal(mname, newname);
1021 g_free(newname);
1023 return is_equal;
1026 static bool qtest_is_old_versioned_machine(const char *mname)
1028 const char *dash = strrchr(mname, '-');
1029 const char *dot = strrchr(mname, '.');
1030 const char *chr;
1031 char *bname;
1032 const int major = QEMU_VERSION_MAJOR;
1033 const int minor = QEMU_VERSION_MINOR;
1034 bool res = false;
1036 if (dash && dot && dot > dash) {
1037 for (chr = dash + 1; *chr; chr++) {
1038 if (!qemu_isdigit(*chr) && *chr != '.') {
1039 return false;
1043 * Now check if it is one of the latest versions. Check major + 1
1044 * and minor + 1 versions as well, since they might already exist
1045 * in the development branch.
1047 bname = g_strdup(mname);
1048 bname[dash - mname] = 0;
1049 res = !qtest_check_machine_version(mname, bname, major + 1, 0) &&
1050 !qtest_check_machine_version(mname, bname, major, minor + 1) &&
1051 !qtest_check_machine_version(mname, bname, major, minor);
1052 g_free(bname);
1055 return res;
1058 void qtest_cb_for_every_machine(void (*cb)(const char *machine),
1059 bool skip_old_versioned)
1061 QDict *response, *minfo;
1062 QList *list;
1063 const QListEntry *p;
1064 QObject *qobj;
1065 QString *qstr;
1066 const char *mname;
1068 qtest_start("-machine none");
1069 response = qmp("{ 'execute': 'query-machines' }");
1070 g_assert(response);
1071 list = qdict_get_qlist(response, "return");
1072 g_assert(list);
1074 for (p = qlist_first(list); p; p = qlist_next(p)) {
1075 minfo = qobject_to(QDict, qlist_entry_obj(p));
1076 g_assert(minfo);
1077 qobj = qdict_get(minfo, "name");
1078 g_assert(qobj);
1079 qstr = qobject_to(QString, qobj);
1080 g_assert(qstr);
1081 mname = qstring_get_str(qstr);
1082 if (!skip_old_versioned || !qtest_is_old_versioned_machine(mname)) {
1083 cb(mname);
1087 qtest_end();
1088 qobject_unref(response);
1091 QDict *qtest_qmp_receive_success(QTestState *s,
1092 void (*event_cb)(void *opaque,
1093 const char *event,
1094 QDict *data),
1095 void *opaque)
1097 QDict *response, *ret, *data;
1098 const char *event;
1100 for (;;) {
1101 response = qtest_qmp_receive(s);
1102 g_assert(!qdict_haskey(response, "error"));
1103 ret = qdict_get_qdict(response, "return");
1104 if (ret) {
1105 break;
1107 event = qdict_get_str(response, "event");
1108 data = qdict_get_qdict(response, "data");
1109 if (event_cb) {
1110 event_cb(opaque, event, data);
1112 qobject_unref(response);
1115 qobject_ref(ret);
1116 qobject_unref(response);
1117 return ret;
1121 * Generic hot-plugging test via the device_add QMP command.
1123 void qtest_qmp_device_add(const char *driver, const char *id,
1124 const char *fmt, ...)
1126 QDict *args, *response;
1127 va_list ap;
1129 va_start(ap, fmt);
1130 args = qdict_from_vjsonf_nofail(fmt, ap);
1131 va_end(ap);
1133 g_assert(!qdict_haskey(args, "driver") && !qdict_haskey(args, "id"));
1134 qdict_put_str(args, "driver", driver);
1135 qdict_put_str(args, "id", id);
1137 response = qmp("{'execute': 'device_add', 'arguments': %p}", args);
1138 g_assert(response);
1139 g_assert(!qdict_haskey(response, "event")); /* We don't expect any events */
1140 g_assert(!qdict_haskey(response, "error"));
1141 qobject_unref(response);
1144 static void device_deleted_cb(void *opaque, const char *name, QDict *data)
1146 bool *got_event = opaque;
1148 g_assert_cmpstr(name, ==, "DEVICE_DELETED");
1149 *got_event = true;
1153 * Generic hot-unplugging test via the device_del QMP command.
1154 * Device deletion will get one response and one event. For example:
1156 * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
1158 * will get this one:
1160 * {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
1161 * "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
1162 * "path": "/machine/peripheral/scsi-hd"}}
1164 * and this one:
1166 * {"return": {}}
1168 * But the order of arrival may vary - so we've got to detect both.
1170 void qtest_qmp_device_del(const char *id)
1172 bool got_event = false;
1173 QDict *rsp;
1175 qtest_qmp_send(global_qtest,
1176 "{'execute': 'device_del', 'arguments': {'id': %s}}",
1177 id);
1178 rsp = qtest_qmp_receive_success(global_qtest, device_deleted_cb,
1179 &got_event);
1180 qobject_unref(rsp);
1181 if (!got_event) {
1182 rsp = qmp_receive();
1183 g_assert_cmpstr(qdict_get_try_str(rsp, "event"),
1184 ==, "DEVICE_DELETED");
1185 qobject_unref(rsp);
1189 bool qmp_rsp_is_err(QDict *rsp)
1191 QDict *error = qdict_get_qdict(rsp, "error");
1192 qobject_unref(rsp);
1193 return !!error;
1196 void qmp_assert_error_class(QDict *rsp, const char *class)
1198 QDict *error = qdict_get_qdict(rsp, "error");
1200 g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, class);
1201 g_assert_nonnull(qdict_get_try_str(error, "desc"));
1202 g_assert(!qdict_haskey(rsp, "return"));
1204 qobject_unref(rsp);