block/iscsi: allow fall back to WRITE SAME without UNMAP
[qemu/ar7.git] / tests / libqtest.c
blob815569584813c8f31d847690b88bbf01bf1f5e8b
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 "libqtest.h"
19 #include <glib.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/un.h>
24 #include <inttypes.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
31 #include "qemu/compiler.h"
32 #include "qemu/osdep.h"
33 #include "qapi/qmp/json-streamer.h"
34 #include "qapi/qmp/json-parser.h"
36 #define MAX_IRQ 256
37 #define SOCKET_TIMEOUT 5
39 QTestState *global_qtest;
41 struct QTestState
43 int fd;
44 int qmp_fd;
45 bool irq_level[MAX_IRQ];
46 GString *rx;
47 pid_t qemu_pid; /* our child QEMU process */
48 struct sigaction sigact_old; /* restored on exit */
51 static GList *qtest_instances;
52 static struct sigaction sigact_old;
54 #define g_assert_no_errno(ret) do { \
55 g_assert_cmpint(ret, !=, -1); \
56 } while (0)
58 static int init_socket(const char *socket_path)
60 struct sockaddr_un addr;
61 int sock;
62 int ret;
64 sock = socket(PF_UNIX, SOCK_STREAM, 0);
65 g_assert_no_errno(sock);
67 addr.sun_family = AF_UNIX;
68 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
69 qemu_set_cloexec(sock);
71 do {
72 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
73 } while (ret == -1 && errno == EINTR);
74 g_assert_no_errno(ret);
75 listen(sock, 1);
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 addrlen = sizeof(addr);
92 do {
93 ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
94 } while (ret == -1 && errno == EINTR);
95 close(sock);
97 return ret;
100 static void kill_qemu(QTestState *s)
102 if (s->qemu_pid != -1) {
103 kill(s->qemu_pid, SIGTERM);
104 waitpid(s->qemu_pid, NULL, 0);
108 static void sigabrt_handler(int signo)
110 GList *elem;
111 for (elem = qtest_instances; elem; elem = elem->next) {
112 kill_qemu(elem->data);
116 static void setup_sigabrt_handler(void)
118 struct sigaction sigact;
120 /* Catch SIGABRT to clean up on g_assert() failure */
121 sigact = (struct sigaction){
122 .sa_handler = sigabrt_handler,
123 .sa_flags = SA_RESETHAND,
125 sigemptyset(&sigact.sa_mask);
126 sigaction(SIGABRT, &sigact, &sigact_old);
129 static void cleanup_sigabrt_handler(void)
131 sigaction(SIGABRT, &sigact_old, NULL);
134 QTestState *qtest_init(const char *extra_args)
136 QTestState *s;
137 int sock, qmpsock, i;
138 gchar *socket_path;
139 gchar *qmp_socket_path;
140 gchar *command;
141 const char *qemu_binary;
143 qemu_binary = getenv("QTEST_QEMU_BINARY");
144 g_assert(qemu_binary != NULL);
146 s = g_malloc(sizeof(*s));
148 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
149 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
151 sock = init_socket(socket_path);
152 qmpsock = init_socket(qmp_socket_path);
154 /* Only install SIGABRT handler once */
155 if (!qtest_instances) {
156 setup_sigabrt_handler();
159 qtest_instances = g_list_prepend(qtest_instances, s);
161 s->qemu_pid = fork();
162 if (s->qemu_pid == 0) {
163 command = g_strdup_printf("exec %s "
164 "-qtest unix:%s,nowait "
165 "-qtest-log /dev/null "
166 "-qmp unix:%s,nowait "
167 "-machine accel=qtest "
168 "-display none "
169 "%s", qemu_binary, socket_path,
170 qmp_socket_path,
171 extra_args ?: "");
172 execlp("/bin/sh", "sh", "-c", command, NULL);
173 exit(1);
176 s->fd = socket_accept(sock);
177 if (s->fd >= 0) {
178 s->qmp_fd = socket_accept(qmpsock);
180 unlink(socket_path);
181 unlink(qmp_socket_path);
182 g_free(socket_path);
183 g_free(qmp_socket_path);
185 g_assert(s->fd >= 0 && s->qmp_fd >= 0);
187 s->rx = g_string_new("");
188 for (i = 0; i < MAX_IRQ; i++) {
189 s->irq_level[i] = false;
192 /* Read the QMP greeting and then do the handshake */
193 qtest_qmp_discard_response(s, "");
194 qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
196 if (getenv("QTEST_STOP")) {
197 kill(s->qemu_pid, SIGSTOP);
200 return s;
203 void qtest_quit(QTestState *s)
205 /* Uninstall SIGABRT handler on last instance */
206 if (qtest_instances && !qtest_instances->next) {
207 cleanup_sigabrt_handler();
210 qtest_instances = g_list_remove(qtest_instances, s);
212 kill_qemu(s);
213 close(s->fd);
214 close(s->qmp_fd);
215 g_string_free(s->rx, true);
216 g_free(s);
219 static void socket_sendf(int fd, const char *fmt, va_list ap)
221 gchar *str;
222 size_t size, offset;
224 str = g_strdup_vprintf(fmt, ap);
225 size = strlen(str);
227 offset = 0;
228 while (offset < size) {
229 ssize_t len;
231 len = write(fd, str + offset, size - offset);
232 if (len == -1 && errno == EINTR) {
233 continue;
236 g_assert_no_errno(len);
237 g_assert_cmpint(len, >, 0);
239 offset += len;
243 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
245 va_list ap;
247 va_start(ap, fmt);
248 socket_sendf(s->fd, fmt, ap);
249 va_end(ap);
252 static GString *qtest_recv_line(QTestState *s)
254 GString *line;
255 size_t offset;
256 char *eol;
258 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
259 ssize_t len;
260 char buffer[1024];
262 len = read(s->fd, buffer, sizeof(buffer));
263 if (len == -1 && errno == EINTR) {
264 continue;
267 if (len == -1 || len == 0) {
268 fprintf(stderr, "Broken pipe\n");
269 exit(1);
272 g_string_append_len(s->rx, buffer, len);
275 offset = eol - s->rx->str;
276 line = g_string_new_len(s->rx->str, offset);
277 g_string_erase(s->rx, 0, offset + 1);
279 return line;
282 static gchar **qtest_rsp(QTestState *s, int expected_args)
284 GString *line;
285 gchar **words;
286 int i;
288 redo:
289 line = qtest_recv_line(s);
290 words = g_strsplit(line->str, " ", 0);
291 g_string_free(line, TRUE);
293 if (strcmp(words[0], "IRQ") == 0) {
294 int irq;
296 g_assert(words[1] != NULL);
297 g_assert(words[2] != NULL);
299 irq = strtoul(words[2], NULL, 0);
300 g_assert_cmpint(irq, >=, 0);
301 g_assert_cmpint(irq, <, MAX_IRQ);
303 if (strcmp(words[1], "raise") == 0) {
304 s->irq_level[irq] = true;
305 } else {
306 s->irq_level[irq] = false;
309 g_strfreev(words);
310 goto redo;
313 g_assert(words[0] != NULL);
314 g_assert_cmpstr(words[0], ==, "OK");
316 if (expected_args) {
317 for (i = 0; i < expected_args; i++) {
318 g_assert(words[i] != NULL);
320 } else {
321 g_strfreev(words);
324 return words;
327 typedef struct {
328 JSONMessageParser parser;
329 QDict *response;
330 } QMPResponseParser;
332 static void qmp_response(JSONMessageParser *parser, QList *tokens)
334 QMPResponseParser *qmp = container_of(parser, QMPResponseParser, parser);
335 QObject *obj;
337 obj = json_parser_parse(tokens, NULL);
338 if (!obj) {
339 fprintf(stderr, "QMP JSON response parsing failed\n");
340 exit(1);
343 g_assert(qobject_type(obj) == QTYPE_QDICT);
344 g_assert(!qmp->response);
345 qmp->response = (QDict *)obj;
348 QDict *qtest_qmp_receive(QTestState *s)
350 QMPResponseParser qmp;
352 qmp.response = NULL;
353 json_message_parser_init(&qmp.parser, qmp_response);
354 while (!qmp.response) {
355 ssize_t len;
356 char c;
358 len = read(s->qmp_fd, &c, 1);
359 if (len == -1 && errno == EINTR) {
360 continue;
363 if (len == -1 || len == 0) {
364 fprintf(stderr, "Broken pipe\n");
365 exit(1);
368 json_message_parser_feed(&qmp.parser, &c, 1);
370 json_message_parser_destroy(&qmp.parser);
372 return qmp.response;
375 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
377 /* Send QMP request */
378 socket_sendf(s->qmp_fd, fmt, ap);
380 /* Receive reply */
381 return qtest_qmp_receive(s);
384 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
386 va_list ap;
387 QDict *response;
389 va_start(ap, fmt);
390 response = qtest_qmpv(s, fmt, ap);
391 va_end(ap);
392 return response;
395 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
397 QDict *response = qtest_qmpv(s, fmt, ap);
398 QDECREF(response);
401 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
403 va_list ap;
404 QDict *response;
406 va_start(ap, fmt);
407 response = qtest_qmpv(s, fmt, ap);
408 va_end(ap);
409 QDECREF(response);
412 const char *qtest_get_arch(void)
414 const char *qemu = getenv("QTEST_QEMU_BINARY");
415 const char *end = strrchr(qemu, '/');
417 return end + strlen("/qemu-system-");
420 bool qtest_get_irq(QTestState *s, int num)
422 /* dummy operation in order to make sure irq is up to date */
423 qtest_inb(s, 0);
425 return s->irq_level[num];
428 static int64_t qtest_clock_rsp(QTestState *s)
430 gchar **words;
431 int64_t clock;
432 words = qtest_rsp(s, 2);
433 clock = g_ascii_strtoll(words[1], NULL, 0);
434 g_strfreev(words);
435 return clock;
438 int64_t qtest_clock_step_next(QTestState *s)
440 qtest_sendf(s, "clock_step\n");
441 return qtest_clock_rsp(s);
444 int64_t qtest_clock_step(QTestState *s, int64_t step)
446 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
447 return qtest_clock_rsp(s);
450 int64_t qtest_clock_set(QTestState *s, int64_t val)
452 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
453 return qtest_clock_rsp(s);
456 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
458 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
459 qtest_rsp(s, 0);
462 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
464 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
465 qtest_rsp(s, 0);
468 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
470 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
471 qtest_rsp(s, 0);
474 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
476 qtest_out(s, "outb", addr, value);
479 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
481 qtest_out(s, "outw", addr, value);
484 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
486 qtest_out(s, "outl", addr, value);
489 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
491 gchar **args;
492 uint32_t value;
494 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
495 args = qtest_rsp(s, 2);
496 value = strtoul(args[1], NULL, 0);
497 g_strfreev(args);
499 return value;
502 uint8_t qtest_inb(QTestState *s, uint16_t addr)
504 return qtest_in(s, "inb", addr);
507 uint16_t qtest_inw(QTestState *s, uint16_t addr)
509 return qtest_in(s, "inw", addr);
512 uint32_t qtest_inl(QTestState *s, uint16_t addr)
514 return qtest_in(s, "inl", addr);
517 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
518 uint64_t value)
520 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
521 qtest_rsp(s, 0);
524 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
526 qtest_write(s, "writeb", addr, value);
529 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
531 qtest_write(s, "writew", addr, value);
534 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
536 qtest_write(s, "writel", addr, value);
539 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
541 qtest_write(s, "writeq", addr, value);
544 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
546 gchar **args;
547 uint64_t value;
549 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
550 args = qtest_rsp(s, 2);
551 value = strtoull(args[1], NULL, 0);
552 g_strfreev(args);
554 return value;
557 uint8_t qtest_readb(QTestState *s, uint64_t addr)
559 return qtest_read(s, "readb", addr);
562 uint16_t qtest_readw(QTestState *s, uint64_t addr)
564 return qtest_read(s, "readw", addr);
567 uint32_t qtest_readl(QTestState *s, uint64_t addr)
569 return qtest_read(s, "readl", addr);
572 uint64_t qtest_readq(QTestState *s, uint64_t addr)
574 return qtest_read(s, "readq", addr);
577 static int hex2nib(char ch)
579 if (ch >= '0' && ch <= '9') {
580 return ch - '0';
581 } else if (ch >= 'a' && ch <= 'f') {
582 return 10 + (ch - 'a');
583 } else if (ch >= 'A' && ch <= 'F') {
584 return 10 + (ch - 'a');
585 } else {
586 return -1;
590 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
592 uint8_t *ptr = data;
593 gchar **args;
594 size_t i;
596 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
597 args = qtest_rsp(s, 2);
599 for (i = 0; i < size; i++) {
600 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
601 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
604 g_strfreev(args);
607 void qtest_add_func(const char *str, void (*fn))
609 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
610 g_test_add_func(path, fn);
613 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
615 const uint8_t *ptr = data;
616 size_t i;
618 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x", addr, size);
619 for (i = 0; i < size; i++) {
620 qtest_sendf(s, "%02x", ptr[i]);
622 qtest_sendf(s, "\n");
623 qtest_rsp(s, 0);
626 QDict *qmp(const char *fmt, ...)
628 va_list ap;
629 QDict *response;
631 va_start(ap, fmt);
632 response = qtest_qmpv(global_qtest, fmt, ap);
633 va_end(ap);
634 return response;
637 void qmp_discard_response(const char *fmt, ...)
639 va_list ap;
641 va_start(ap, fmt);
642 qtest_qmpv_discard_response(global_qtest, fmt, ap);
643 va_end(ap);