event_notifier: remove event_notifier_test
[qemu-kvm.git] / tests / libqtest.c
blob071b6be521886d38e1c0da51e20db4fcf540bace
1 /*
2 * QTest
4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
15 #include "libqtest.h"
17 #include <glib.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/wait.h>
21 #include <sys/un.h>
22 #include <inttypes.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
29 #include "compiler.h"
30 #include "osdep.h"
32 #define MAX_IRQ 256
34 QTestState *global_qtest;
36 struct QTestState
38 int fd;
39 int qmp_fd;
40 bool irq_level[MAX_IRQ];
41 GString *rx;
42 gchar *pid_file;
45 #define g_assert_no_errno(ret) do { \
46 g_assert_cmpint(ret, !=, -1); \
47 } while (0)
49 static int init_socket(const char *socket_path)
51 struct sockaddr_un addr;
52 int sock;
53 int ret;
55 sock = socket(PF_UNIX, SOCK_STREAM, 0);
56 g_assert_no_errno(sock);
58 addr.sun_family = AF_UNIX;
59 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
60 qemu_set_cloexec(sock);
62 do {
63 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
64 } while (ret == -1 && errno == EINTR);
65 g_assert_no_errno(ret);
66 listen(sock, 1);
68 return sock;
71 static int socket_accept(int sock)
73 struct sockaddr_un addr;
74 socklen_t addrlen;
75 int ret;
77 addrlen = sizeof(addr);
78 do {
79 ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
80 } while (ret == -1 && errno == EINTR);
81 g_assert_no_errno(ret);
82 close(sock);
84 return ret;
87 QTestState *qtest_init(const char *extra_args)
89 QTestState *s;
90 int sock, qmpsock, ret, i;
91 gchar *socket_path;
92 gchar *qmp_socket_path;
93 gchar *pid_file;
94 gchar *command;
95 const char *qemu_binary;
96 pid_t pid;
98 qemu_binary = getenv("QTEST_QEMU_BINARY");
99 g_assert(qemu_binary != NULL);
101 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
102 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
103 pid_file = g_strdup_printf("/tmp/qtest-%d.pid", getpid());
105 s = g_malloc(sizeof(*s));
107 sock = init_socket(socket_path);
108 qmpsock = init_socket(qmp_socket_path);
110 pid = fork();
111 if (pid == 0) {
112 command = g_strdup_printf("%s "
113 "-qtest unix:%s,nowait "
114 "-qtest-log /dev/null "
115 "-qmp unix:%s,nowait "
116 "-pidfile %s "
117 "-machine accel=qtest "
118 "%s", qemu_binary, socket_path,
119 qmp_socket_path, pid_file,
120 extra_args ?: "");
122 ret = system(command);
123 exit(ret);
124 g_free(command);
127 s->fd = socket_accept(sock);
128 s->qmp_fd = socket_accept(qmpsock);
130 s->rx = g_string_new("");
131 s->pid_file = pid_file;
132 for (i = 0; i < MAX_IRQ; i++) {
133 s->irq_level[i] = false;
136 g_free(socket_path);
137 g_free(qmp_socket_path);
139 /* Read the QMP greeting and then do the handshake */
140 qtest_qmp(s, "");
141 qtest_qmp(s, "{ 'execute': 'qmp_capabilities' }");
143 return s;
146 void qtest_quit(QTestState *s)
148 FILE *f;
149 char buffer[1024];
151 f = fopen(s->pid_file, "r");
152 if (f) {
153 if (fgets(buffer, sizeof(buffer), f)) {
154 pid_t pid = atoi(buffer);
155 int status = 0;
157 kill(pid, SIGTERM);
158 waitpid(pid, &status, 0);
161 fclose(f);
165 static void socket_sendf(int fd, const char *fmt, va_list ap)
167 gchar *str;
168 size_t size, offset;
170 str = g_strdup_vprintf(fmt, ap);
171 size = strlen(str);
173 offset = 0;
174 while (offset < size) {
175 ssize_t len;
177 len = write(fd, str + offset, size - offset);
178 if (len == -1 && errno == EINTR) {
179 continue;
182 g_assert_no_errno(len);
183 g_assert_cmpint(len, >, 0);
185 offset += len;
189 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
191 va_list ap;
193 va_start(ap, fmt);
194 socket_sendf(s->fd, fmt, ap);
195 va_end(ap);
198 static GString *qtest_recv_line(QTestState *s)
200 GString *line;
201 size_t offset;
202 char *eol;
204 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
205 ssize_t len;
206 char buffer[1024];
208 len = read(s->fd, buffer, sizeof(buffer));
209 if (len == -1 && errno == EINTR) {
210 continue;
213 if (len == -1 || len == 0) {
214 fprintf(stderr, "Broken pipe\n");
215 exit(1);
218 g_string_append_len(s->rx, buffer, len);
221 offset = eol - s->rx->str;
222 line = g_string_new_len(s->rx->str, offset);
223 g_string_erase(s->rx, 0, offset + 1);
225 return line;
228 static gchar **qtest_rsp(QTestState *s, int expected_args)
230 GString *line;
231 gchar **words;
232 int i;
234 redo:
235 line = qtest_recv_line(s);
236 words = g_strsplit(line->str, " ", 0);
237 g_string_free(line, TRUE);
239 if (strcmp(words[0], "IRQ") == 0) {
240 int irq;
242 g_assert(words[1] != NULL);
243 g_assert(words[2] != NULL);
245 irq = strtoul(words[2], NULL, 0);
246 g_assert_cmpint(irq, >=, 0);
247 g_assert_cmpint(irq, <, MAX_IRQ);
249 if (strcmp(words[1], "raise") == 0) {
250 s->irq_level[irq] = true;
251 } else {
252 s->irq_level[irq] = false;
255 g_strfreev(words);
256 goto redo;
259 g_assert(words[0] != NULL);
260 g_assert_cmpstr(words[0], ==, "OK");
262 if (expected_args) {
263 for (i = 0; i < expected_args; i++) {
264 g_assert(words[i] != NULL);
266 } else {
267 g_strfreev(words);
270 return words;
273 void qtest_qmp(QTestState *s, const char *fmt, ...)
275 va_list ap;
276 bool has_reply = false;
277 int nesting = 0;
279 /* Send QMP request */
280 va_start(ap, fmt);
281 socket_sendf(s->qmp_fd, fmt, ap);
282 va_end(ap);
284 /* Receive reply */
285 while (!has_reply || nesting > 0) {
286 ssize_t len;
287 char c;
289 len = read(s->qmp_fd, &c, 1);
290 if (len == -1 && errno == EINTR) {
291 continue;
294 if (len == -1 || len == 0) {
295 fprintf(stderr, "Broken pipe\n");
296 exit(1);
299 switch (c) {
300 case '{':
301 nesting++;
302 has_reply = true;
303 break;
304 case '}':
305 nesting--;
306 break;
311 const char *qtest_get_arch(void)
313 const char *qemu = getenv("QTEST_QEMU_BINARY");
314 const char *end = strrchr(qemu, '/');
316 return end + strlen("/qemu-system-");
319 bool qtest_get_irq(QTestState *s, int num)
321 /* dummy operation in order to make sure irq is up to date */
322 qtest_inb(s, 0);
324 return s->irq_level[num];
327 static int64_t qtest_clock_rsp(QTestState *s)
329 gchar **words;
330 int64_t clock;
331 words = qtest_rsp(s, 2);
332 clock = g_ascii_strtoll(words[1], NULL, 0);
333 g_strfreev(words);
334 return clock;
337 int64_t qtest_clock_step_next(QTestState *s)
339 qtest_sendf(s, "clock_step\n");
340 return qtest_clock_rsp(s);
343 int64_t qtest_clock_step(QTestState *s, int64_t step)
345 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
346 return qtest_clock_rsp(s);
349 int64_t qtest_clock_set(QTestState *s, int64_t val)
351 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
352 return qtest_clock_rsp(s);
355 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
357 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
358 qtest_rsp(s, 0);
361 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
363 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
364 qtest_rsp(s, 0);
367 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
369 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
370 qtest_rsp(s, 0);
373 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
375 qtest_out(s, "outb", addr, value);
378 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
380 qtest_out(s, "outw", addr, value);
383 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
385 qtest_out(s, "outl", addr, value);
388 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
390 gchar **args;
391 uint32_t value;
393 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
394 args = qtest_rsp(s, 2);
395 value = strtoul(args[1], NULL, 0);
396 g_strfreev(args);
398 return value;
401 uint8_t qtest_inb(QTestState *s, uint16_t addr)
403 return qtest_in(s, "inb", addr);
406 uint16_t qtest_inw(QTestState *s, uint16_t addr)
408 return qtest_in(s, "inw", addr);
411 uint32_t qtest_inl(QTestState *s, uint16_t addr)
413 return qtest_in(s, "inl", addr);
416 static int hex2nib(char ch)
418 if (ch >= '0' && ch <= '9') {
419 return ch - '0';
420 } else if (ch >= 'a' && ch <= 'f') {
421 return 10 + (ch - 'a');
422 } else if (ch >= 'A' && ch <= 'F') {
423 return 10 + (ch - 'a');
424 } else {
425 return -1;
429 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
431 uint8_t *ptr = data;
432 gchar **args;
433 size_t i;
435 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
436 args = qtest_rsp(s, 2);
438 for (i = 0; i < size; i++) {
439 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
440 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
443 g_strfreev(args);
446 void qtest_add_func(const char *str, void (*fn))
448 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
449 g_test_add_func(path, fn);
452 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
454 const uint8_t *ptr = data;
455 size_t i;
457 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x", addr, size);
458 for (i = 0; i < size; i++) {
459 qtest_sendf(s, "%02x", ptr[i]);
461 qtest_sendf(s, "\n");
462 qtest_rsp(s, 0);