]> Git Repo - qemu.git/blob - tests/libqtest.c
Merge remote-tracking branch 'mjt/trivial-patches' into staging
[qemu.git] / tests / libqtest.c
1 /*
2  * QTest
3  *
4  * Copyright IBM, Corp. 2012
5  * Copyright Red Hat, Inc. 2012
6  * Copyright SUSE LINUX Products GmbH 2013
7  *
8  * Authors:
9  *  Anthony Liguori   <[email protected]>
10  *  Paolo Bonzini     <[email protected]>
11  *  Andreas Färber    <[email protected]>
12  *
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.
15  *
16  */
17 #include "libqtest.h"
18
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>
30
31 #include "qemu/compiler.h"
32 #include "qemu/osdep.h"
33
34 #define MAX_IRQ 256
35
36 QTestState *global_qtest;
37
38 struct QTestState
39 {
40     int fd;
41     int qmp_fd;
42     bool irq_level[MAX_IRQ];
43     GString *rx;
44     gchar *pid_file; /* QEMU PID file */
45     int child_pid;   /* Child process created to execute QEMU */
46     char *socket_path, *qmp_socket_path;
47 };
48
49 #define g_assert_no_errno(ret) do { \
50     g_assert_cmpint(ret, !=, -1); \
51 } while (0)
52
53 static int init_socket(const char *socket_path)
54 {
55     struct sockaddr_un addr;
56     int sock;
57     int ret;
58
59     sock = socket(PF_UNIX, SOCK_STREAM, 0);
60     g_assert_no_errno(sock);
61
62     addr.sun_family = AF_UNIX;
63     snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
64     qemu_set_cloexec(sock);
65
66     do {
67         ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
68     } while (ret == -1 && errno == EINTR);
69     g_assert_no_errno(ret);
70     listen(sock, 1);
71
72     return sock;
73 }
74
75 static int socket_accept(int sock)
76 {
77     struct sockaddr_un addr;
78     socklen_t addrlen;
79     int ret;
80
81     addrlen = sizeof(addr);
82     do {
83         ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
84     } while (ret == -1 && errno == EINTR);
85     g_assert_no_errno(ret);
86     close(sock);
87
88     return ret;
89 }
90
91 static pid_t qtest_qemu_pid(QTestState *s)
92 {
93     FILE *f;
94     char buffer[1024];
95     pid_t pid = -1;
96
97     f = fopen(s->pid_file, "r");
98     if (f) {
99         if (fgets(buffer, sizeof(buffer), f)) {
100             pid = atoi(buffer);
101         }
102         fclose(f);
103     }
104     return pid;
105 }
106
107 QTestState *qtest_init(const char *extra_args)
108 {
109     QTestState *s;
110     int sock, qmpsock, i;
111     gchar *pid_file;
112     gchar *command;
113     const char *qemu_binary;
114     pid_t pid;
115
116     qemu_binary = getenv("QTEST_QEMU_BINARY");
117     g_assert(qemu_binary != NULL);
118
119     s = g_malloc(sizeof(*s));
120
121     s->socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
122     s->qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
123     pid_file = g_strdup_printf("/tmp/qtest-%d.pid", getpid());
124
125     sock = init_socket(s->socket_path);
126     qmpsock = init_socket(s->qmp_socket_path);
127
128     pid = fork();
129     if (pid == 0) {
130         command = g_strdup_printf("%s "
131                                   "-qtest unix:%s,nowait "
132                                   "-qtest-log /dev/null "
133                                   "-qmp unix:%s,nowait "
134                                   "-pidfile %s "
135                                   "-machine accel=qtest "
136                                   "%s", qemu_binary, s->socket_path,
137                                   s->qmp_socket_path, pid_file,
138                                   extra_args ?: "");
139         execlp("/bin/sh", "sh", "-c", command, NULL);
140         exit(1);
141     }
142
143     s->fd = socket_accept(sock);
144     s->qmp_fd = socket_accept(qmpsock);
145
146     s->rx = g_string_new("");
147     s->pid_file = pid_file;
148     s->child_pid = pid;
149     for (i = 0; i < MAX_IRQ; i++) {
150         s->irq_level[i] = false;
151     }
152
153     /* Read the QMP greeting and then do the handshake */
154     qtest_qmp(s, "");
155     qtest_qmp(s, "{ 'execute': 'qmp_capabilities' }");
156
157     if (getenv("QTEST_STOP")) {
158         kill(qtest_qemu_pid(s), SIGSTOP);
159     }
160
161     return s;
162 }
163
164 void qtest_quit(QTestState *s)
165 {
166     int status;
167
168     pid_t pid = qtest_qemu_pid(s);
169     if (pid != -1) {
170         kill(pid, SIGTERM);
171         waitpid(pid, &status, 0);
172     }
173
174     close(s->fd);
175     close(s->qmp_fd);
176     g_string_free(s->rx, true);
177     unlink(s->pid_file);
178     unlink(s->socket_path);
179     unlink(s->qmp_socket_path);
180     g_free(s->pid_file);
181     g_free(s->socket_path);
182     g_free(s->qmp_socket_path);
183     g_free(s);
184 }
185
186 static void socket_sendf(int fd, const char *fmt, va_list ap)
187 {
188     gchar *str;
189     size_t size, offset;
190
191     str = g_strdup_vprintf(fmt, ap);
192     size = strlen(str);
193
194     offset = 0;
195     while (offset < size) {
196         ssize_t len;
197
198         len = write(fd, str + offset, size - offset);
199         if (len == -1 && errno == EINTR) {
200             continue;
201         }
202
203         g_assert_no_errno(len);
204         g_assert_cmpint(len, >, 0);
205
206         offset += len;
207     }
208 }
209
210 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
211 {
212     va_list ap;
213
214     va_start(ap, fmt);
215     socket_sendf(s->fd, fmt, ap);
216     va_end(ap);
217 }
218
219 static GString *qtest_recv_line(QTestState *s)
220 {
221     GString *line;
222     size_t offset;
223     char *eol;
224
225     while ((eol = strchr(s->rx->str, '\n')) == NULL) {
226         ssize_t len;
227         char buffer[1024];
228
229         len = read(s->fd, buffer, sizeof(buffer));
230         if (len == -1 && errno == EINTR) {
231             continue;
232         }
233
234         if (len == -1 || len == 0) {
235             fprintf(stderr, "Broken pipe\n");
236             exit(1);
237         }
238
239         g_string_append_len(s->rx, buffer, len);
240     }
241
242     offset = eol - s->rx->str;
243     line = g_string_new_len(s->rx->str, offset);
244     g_string_erase(s->rx, 0, offset + 1);
245
246     return line;
247 }
248
249 static gchar **qtest_rsp(QTestState *s, int expected_args)
250 {
251     GString *line;
252     gchar **words;
253     int i;
254
255 redo:
256     line = qtest_recv_line(s);
257     words = g_strsplit(line->str, " ", 0);
258     g_string_free(line, TRUE);
259
260     if (strcmp(words[0], "IRQ") == 0) {
261         int irq;
262
263         g_assert(words[1] != NULL);
264         g_assert(words[2] != NULL);
265
266         irq = strtoul(words[2], NULL, 0);
267         g_assert_cmpint(irq, >=, 0);
268         g_assert_cmpint(irq, <, MAX_IRQ);
269
270         if (strcmp(words[1], "raise") == 0) {
271             s->irq_level[irq] = true;
272         } else {
273             s->irq_level[irq] = false;
274         }
275
276         g_strfreev(words);
277         goto redo;
278     }
279
280     g_assert(words[0] != NULL);
281     g_assert_cmpstr(words[0], ==, "OK");
282
283     if (expected_args) {
284         for (i = 0; i < expected_args; i++) {
285             g_assert(words[i] != NULL);
286         }
287     } else {
288         g_strfreev(words);
289     }
290
291     return words;
292 }
293
294 void qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
295 {
296     bool has_reply = false;
297     int nesting = 0;
298
299     /* Send QMP request */
300     socket_sendf(s->qmp_fd, fmt, ap);
301
302     /* Receive reply */
303     while (!has_reply || nesting > 0) {
304         ssize_t len;
305         char c;
306
307         len = read(s->qmp_fd, &c, 1);
308         if (len == -1 && errno == EINTR) {
309             continue;
310         }
311
312         if (len == -1 || len == 0) {
313             fprintf(stderr, "Broken pipe\n");
314             exit(1);
315         }
316
317         switch (c) {
318         case '{':
319             nesting++;
320             has_reply = true;
321             break;
322         case '}':
323             nesting--;
324             break;
325         }
326     }
327 }
328
329 void qtest_qmp(QTestState *s, const char *fmt, ...)
330 {
331     va_list ap;
332
333     va_start(ap, fmt);
334     qtest_qmpv(s, fmt, ap);
335     va_end(ap);
336 }
337
338 const char *qtest_get_arch(void)
339 {
340     const char *qemu = getenv("QTEST_QEMU_BINARY");
341     const char *end = strrchr(qemu, '/');
342
343     return end + strlen("/qemu-system-");
344 }
345
346 bool qtest_get_irq(QTestState *s, int num)
347 {
348     /* dummy operation in order to make sure irq is up to date */
349     qtest_inb(s, 0);
350
351     return s->irq_level[num];
352 }
353
354 static int64_t qtest_clock_rsp(QTestState *s)
355 {
356     gchar **words;
357     int64_t clock;
358     words = qtest_rsp(s, 2);
359     clock = g_ascii_strtoll(words[1], NULL, 0);
360     g_strfreev(words);
361     return clock;
362 }
363
364 int64_t qtest_clock_step_next(QTestState *s)
365 {
366     qtest_sendf(s, "clock_step\n");
367     return qtest_clock_rsp(s);
368 }
369
370 int64_t qtest_clock_step(QTestState *s, int64_t step)
371 {
372     qtest_sendf(s, "clock_step %"PRIi64"\n", step);
373     return qtest_clock_rsp(s);
374 }
375
376 int64_t qtest_clock_set(QTestState *s, int64_t val)
377 {
378     qtest_sendf(s, "clock_set %"PRIi64"\n", val);
379     return qtest_clock_rsp(s);
380 }
381
382 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
383 {
384     qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
385     qtest_rsp(s, 0);
386 }
387
388 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
389 {
390     qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
391     qtest_rsp(s, 0);
392 }
393
394 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
395 {
396     qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
397     qtest_rsp(s, 0);
398 }
399
400 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
401 {
402     qtest_out(s, "outb", addr, value);
403 }
404
405 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
406 {
407     qtest_out(s, "outw", addr, value);
408 }
409
410 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
411 {
412     qtest_out(s, "outl", addr, value);
413 }
414
415 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
416 {
417     gchar **args;
418     uint32_t value;
419
420     qtest_sendf(s, "%s 0x%x\n", cmd, addr);
421     args = qtest_rsp(s, 2);
422     value = strtoul(args[1], NULL, 0);
423     g_strfreev(args);
424
425     return value;
426 }
427
428 uint8_t qtest_inb(QTestState *s, uint16_t addr)
429 {
430     return qtest_in(s, "inb", addr);
431 }
432
433 uint16_t qtest_inw(QTestState *s, uint16_t addr)
434 {
435     return qtest_in(s, "inw", addr);
436 }
437
438 uint32_t qtest_inl(QTestState *s, uint16_t addr)
439 {
440     return qtest_in(s, "inl", addr);
441 }
442
443 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
444                         uint64_t value)
445 {
446     qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
447     qtest_rsp(s, 0);
448 }
449
450 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
451 {
452     qtest_write(s, "writeb", addr, value);
453 }
454
455 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
456 {
457     qtest_write(s, "writew", addr, value);
458 }
459
460 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
461 {
462     qtest_write(s, "writel", addr, value);
463 }
464
465 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
466 {
467     qtest_write(s, "writeq", addr, value);
468 }
469
470 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
471 {
472     gchar **args;
473     uint64_t value;
474
475     qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
476     args = qtest_rsp(s, 2);
477     value = strtoull(args[1], NULL, 0);
478     g_strfreev(args);
479
480     return value;
481 }
482
483 uint8_t qtest_readb(QTestState *s, uint64_t addr)
484 {
485     return qtest_read(s, "readb", addr);
486 }
487
488 uint16_t qtest_readw(QTestState *s, uint64_t addr)
489 {
490     return qtest_read(s, "readw", addr);
491 }
492
493 uint32_t qtest_readl(QTestState *s, uint64_t addr)
494 {
495     return qtest_read(s, "readl", addr);
496 }
497
498 uint64_t qtest_readq(QTestState *s, uint64_t addr)
499 {
500     return qtest_read(s, "readq", addr);
501 }
502
503 static int hex2nib(char ch)
504 {
505     if (ch >= '0' && ch <= '9') {
506         return ch - '0';
507     } else if (ch >= 'a' && ch <= 'f') {
508         return 10 + (ch - 'a');
509     } else if (ch >= 'A' && ch <= 'F') {
510         return 10 + (ch - 'a');
511     } else {
512         return -1;
513     }
514 }
515
516 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
517 {
518     uint8_t *ptr = data;
519     gchar **args;
520     size_t i;
521
522     qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
523     args = qtest_rsp(s, 2);
524
525     for (i = 0; i < size; i++) {
526         ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
527         ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
528     }
529
530     g_strfreev(args);
531 }
532
533 void qtest_add_func(const char *str, void (*fn))
534 {
535     gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
536     g_test_add_func(path, fn);
537 }
538
539 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
540 {
541     const uint8_t *ptr = data;
542     size_t i;
543
544     qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x", addr, size);
545     for (i = 0; i < size; i++) {
546         qtest_sendf(s, "%02x", ptr[i]);
547     }
548     qtest_sendf(s, "\n");
549     qtest_rsp(s, 0);
550 }
This page took 0.053346 seconds and 4 git commands to generate.