tcg: add the tcg-stub.c file into accel/stubs/
[qemu/ar7.git] / tests / test-char.c
blobb962063e56a5b703fb91ec87775de212a8f596e9
1 #include "qemu/osdep.h"
2 #include <glib/gstdio.h>
4 #include "qemu-common.h"
5 #include "qemu/config-file.h"
6 #include "qemu/sockets.h"
7 #include "chardev/char-fe.h"
8 #include "sysemu/sysemu.h"
9 #include "qapi/error.h"
10 #include "qom/qom-qobject.h"
11 #include "qmp-commands.h"
13 static bool quit;
15 typedef struct FeHandler {
16 int read_count;
17 int last_event;
18 char read_buf[128];
19 } FeHandler;
21 static void main_loop(void)
23 quit = false;
24 do {
25 main_loop_wait(false);
26 } while (!quit);
29 static int fe_can_read(void *opaque)
31 FeHandler *h = opaque;
33 return sizeof(h->read_buf) - h->read_count;
36 static void fe_read(void *opaque, const uint8_t *buf, int size)
38 FeHandler *h = opaque;
40 g_assert_cmpint(size, <=, fe_can_read(opaque));
42 memcpy(h->read_buf + h->read_count, buf, size);
43 h->read_count += size;
44 quit = true;
47 static void fe_event(void *opaque, int event)
49 FeHandler *h = opaque;
51 h->last_event = event;
52 if (event != CHR_EVENT_BREAK) {
53 quit = true;
57 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
58 #ifdef _WIN32
59 static void char_console_test_subprocess(void)
61 QemuOpts *opts;
62 Chardev *chr;
64 opts = qemu_opts_create(qemu_find_opts("chardev"), "console-label",
65 1, &error_abort);
66 qemu_opt_set(opts, "backend", "console", &error_abort);
68 chr = qemu_chr_new_from_opts(opts, NULL);
69 g_assert_nonnull(chr);
71 qemu_chr_write_all(chr, (const uint8_t *)"CONSOLE", 7);
73 qemu_opts_del(opts);
74 object_unparent(OBJECT(chr));
77 static void char_console_test(void)
79 g_test_trap_subprocess("/char/console/subprocess", 0, 0);
80 g_test_trap_assert_passed();
81 g_test_trap_assert_stdout("CONSOLE");
83 #endif
84 static void char_stdio_test_subprocess(void)
86 Chardev *chr;
87 CharBackend be;
88 int ret;
90 chr = qemu_chr_new("label", "stdio");
91 g_assert_nonnull(chr);
93 qemu_chr_fe_init(&be, chr, &error_abort);
94 qemu_chr_fe_set_open(&be, true);
95 ret = qemu_chr_fe_write(&be, (void *)"buf", 4);
96 g_assert_cmpint(ret, ==, 4);
98 qemu_chr_fe_deinit(&be, true);
101 static void char_stdio_test(void)
103 g_test_trap_subprocess("/char/stdio/subprocess", 0, 0);
104 g_test_trap_assert_passed();
105 g_test_trap_assert_stdout("buf");
107 #endif
109 static void char_ringbuf_test(void)
111 QemuOpts *opts;
112 Chardev *chr;
113 CharBackend be;
114 char *data;
115 int ret;
117 opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
118 1, &error_abort);
119 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
121 qemu_opt_set(opts, "size", "5", &error_abort);
122 chr = qemu_chr_new_from_opts(opts, NULL);
123 g_assert_null(chr);
124 qemu_opts_del(opts);
126 opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
127 1, &error_abort);
128 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
129 qemu_opt_set(opts, "size", "2", &error_abort);
130 chr = qemu_chr_new_from_opts(opts, &error_abort);
131 g_assert_nonnull(chr);
132 qemu_opts_del(opts);
134 qemu_chr_fe_init(&be, chr, &error_abort);
135 ret = qemu_chr_fe_write(&be, (void *)"buff", 4);
136 g_assert_cmpint(ret, ==, 4);
138 data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
139 g_assert_cmpstr(data, ==, "ff");
140 g_free(data);
142 data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
143 g_assert_cmpstr(data, ==, "");
144 g_free(data);
146 qemu_chr_fe_deinit(&be, true);
148 /* check alias */
149 opts = qemu_opts_create(qemu_find_opts("chardev"), "memory-label",
150 1, &error_abort);
151 qemu_opt_set(opts, "backend", "memory", &error_abort);
152 qemu_opt_set(opts, "size", "2", &error_abort);
153 chr = qemu_chr_new_from_opts(opts, NULL);
154 g_assert_nonnull(chr);
155 object_unparent(OBJECT(chr));
156 qemu_opts_del(opts);
159 static void char_mux_test(void)
161 QemuOpts *opts;
162 Chardev *chr, *base;
163 char *data;
164 FeHandler h1 = { 0, }, h2 = { 0, };
165 CharBackend chr_be1, chr_be2;
167 opts = qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
168 1, &error_abort);
169 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
170 qemu_opt_set(opts, "size", "128", &error_abort);
171 qemu_opt_set(opts, "mux", "on", &error_abort);
172 chr = qemu_chr_new_from_opts(opts, &error_abort);
173 g_assert_nonnull(chr);
174 qemu_opts_del(opts);
176 qemu_chr_fe_init(&chr_be1, chr, &error_abort);
177 qemu_chr_fe_set_handlers(&chr_be1,
178 fe_can_read,
179 fe_read,
180 fe_event,
181 &h1,
182 NULL, true);
184 qemu_chr_fe_init(&chr_be2, chr, &error_abort);
185 qemu_chr_fe_set_handlers(&chr_be2,
186 fe_can_read,
187 fe_read,
188 fe_event,
189 &h2,
190 NULL, true);
191 qemu_chr_fe_take_focus(&chr_be2);
193 base = qemu_chr_find("mux-label-base");
194 g_assert_cmpint(qemu_chr_be_can_write(base), !=, 0);
196 qemu_chr_be_write(base, (void *)"hello", 6);
197 g_assert_cmpint(h1.read_count, ==, 0);
198 g_assert_cmpint(h2.read_count, ==, 6);
199 g_assert_cmpstr(h2.read_buf, ==, "hello");
200 h2.read_count = 0;
202 /* switch focus */
203 qemu_chr_be_write(base, (void *)"\1c", 2);
205 qemu_chr_be_write(base, (void *)"hello", 6);
206 g_assert_cmpint(h2.read_count, ==, 0);
207 g_assert_cmpint(h1.read_count, ==, 6);
208 g_assert_cmpstr(h1.read_buf, ==, "hello");
209 h1.read_count = 0;
211 /* remove first handler */
212 qemu_chr_fe_set_handlers(&chr_be1, NULL, NULL, NULL, NULL, NULL, true);
213 qemu_chr_be_write(base, (void *)"hello", 6);
214 g_assert_cmpint(h1.read_count, ==, 0);
215 g_assert_cmpint(h2.read_count, ==, 0);
217 qemu_chr_be_write(base, (void *)"\1c", 2);
218 qemu_chr_be_write(base, (void *)"hello", 6);
219 g_assert_cmpint(h1.read_count, ==, 0);
220 g_assert_cmpint(h2.read_count, ==, 6);
221 g_assert_cmpstr(h2.read_buf, ==, "hello");
222 h2.read_count = 0;
224 /* print help */
225 qemu_chr_be_write(base, (void *)"\1?", 2);
226 data = qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort);
227 g_assert_cmpint(strlen(data), !=, 0);
228 g_free(data);
230 qemu_chr_fe_deinit(&chr_be1, false);
231 qemu_chr_fe_deinit(&chr_be2, true);
234 typedef struct SocketIdleData {
235 GMainLoop *loop;
236 Chardev *chr;
237 bool conn_expected;
238 CharBackend *be;
239 CharBackend *client_be;
240 } SocketIdleData;
242 static gboolean char_socket_test_idle(gpointer user_data)
244 SocketIdleData *data = user_data;
246 if (object_property_get_bool(OBJECT(data->chr), "connected", NULL)
247 == data->conn_expected) {
248 quit = true;
249 return FALSE;
252 return TRUE;
255 static void socket_read(void *opaque, const uint8_t *buf, int size)
257 SocketIdleData *data = opaque;
259 g_assert_cmpint(size, ==, 1);
260 g_assert_cmpint(*buf, ==, 'Z');
262 size = qemu_chr_fe_write(data->be, (const uint8_t *)"hello", 5);
263 g_assert_cmpint(size, ==, 5);
266 static int socket_can_read(void *opaque)
268 return 10;
271 static void socket_read_hello(void *opaque, const uint8_t *buf, int size)
273 g_assert_cmpint(size, ==, 5);
274 g_assert(strncmp((char *)buf, "hello", 5) == 0);
276 quit = true;
279 static int socket_can_read_hello(void *opaque)
281 return 10;
284 static void char_socket_test(void)
286 Chardev *chr = qemu_chr_new("server", "tcp:127.0.0.1:0,server,nowait");
287 Chardev *chr_client;
288 QObject *addr;
289 QDict *qdict;
290 const char *port;
291 SocketIdleData d = { .chr = chr };
292 CharBackend be;
293 CharBackend client_be;
294 char *tmp;
296 d.be = &be;
297 d.client_be = &be;
299 g_assert_nonnull(chr);
300 g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
302 addr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
303 qdict = qobject_to_qdict(addr);
304 port = qdict_get_str(qdict, "port");
305 tmp = g_strdup_printf("tcp:127.0.0.1:%s", port);
306 QDECREF(qdict);
308 qemu_chr_fe_init(&be, chr, &error_abort);
309 qemu_chr_fe_set_handlers(&be, socket_can_read, socket_read,
310 NULL, &d, NULL, true);
312 chr_client = qemu_chr_new("client", tmp);
313 qemu_chr_fe_init(&client_be, chr_client, &error_abort);
314 qemu_chr_fe_set_handlers(&client_be, socket_can_read_hello,
315 socket_read_hello,
316 NULL, &d, NULL, true);
317 g_free(tmp);
319 d.conn_expected = true;
320 guint id = g_idle_add(char_socket_test_idle, &d);
321 g_source_set_name_by_id(id, "test-idle");
322 g_assert_cmpint(id, >, 0);
323 main_loop();
325 g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
326 g_assert(object_property_get_bool(OBJECT(chr_client),
327 "connected", &error_abort));
329 qemu_chr_write_all(chr_client, (const uint8_t *)"Z", 1);
330 main_loop();
332 object_unparent(OBJECT(chr_client));
334 d.conn_expected = false;
335 g_idle_add(char_socket_test_idle, &d);
336 main_loop();
338 object_unparent(OBJECT(chr));
341 #ifndef _WIN32
342 static void char_pipe_test(void)
344 gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
345 gchar *tmp, *in, *out, *pipe = g_build_filename(tmp_path, "pipe", NULL);
346 Chardev *chr;
347 CharBackend be;
348 int ret, fd;
349 char buf[10];
350 FeHandler fe = { 0, };
352 in = g_strdup_printf("%s.in", pipe);
353 if (mkfifo(in, 0600) < 0) {
354 abort();
356 out = g_strdup_printf("%s.out", pipe);
357 if (mkfifo(out, 0600) < 0) {
358 abort();
361 tmp = g_strdup_printf("pipe:%s", pipe);
362 chr = qemu_chr_new("pipe", tmp);
363 g_assert_nonnull(chr);
364 g_free(tmp);
366 qemu_chr_fe_init(&be, chr, &error_abort);
368 ret = qemu_chr_fe_write(&be, (void *)"pipe-out", 9);
369 g_assert_cmpint(ret, ==, 9);
371 fd = open(out, O_RDWR);
372 ret = read(fd, buf, sizeof(buf));
373 g_assert_cmpint(ret, ==, 9);
374 g_assert_cmpstr(buf, ==, "pipe-out");
375 close(fd);
377 fd = open(in, O_WRONLY);
378 ret = write(fd, "pipe-in", 8);
379 g_assert_cmpint(ret, ==, 8);
380 close(fd);
382 qemu_chr_fe_set_handlers(&be,
383 fe_can_read,
384 fe_read,
385 fe_event,
386 &fe,
387 NULL, true);
389 main_loop();
391 g_assert_cmpint(fe.read_count, ==, 8);
392 g_assert_cmpstr(fe.read_buf, ==, "pipe-in");
394 qemu_chr_fe_deinit(&be, true);
396 g_assert(g_unlink(in) == 0);
397 g_assert(g_unlink(out) == 0);
398 g_assert(g_rmdir(tmp_path) == 0);
399 g_free(in);
400 g_free(out);
401 g_free(tmp_path);
402 g_free(pipe);
404 #endif
406 static void char_udp_test(void)
408 struct sockaddr_in addr = { 0, }, other;
409 SocketIdleData d = { 0, };
410 Chardev *chr;
411 CharBackend be;
412 socklen_t alen = sizeof(addr);
413 int ret, sock = qemu_socket(PF_INET, SOCK_DGRAM, 0);
414 char buf[10];
415 char *tmp;
417 g_assert_cmpint(sock, >, 0);
418 addr.sin_family = AF_INET ;
419 addr.sin_addr.s_addr = htonl(INADDR_ANY);
420 addr.sin_port = 0;
421 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
422 g_assert_cmpint(ret, ==, 0);
423 ret = getsockname(sock, (struct sockaddr *)&addr, &alen);
424 g_assert_cmpint(ret, ==, 0);
426 tmp = g_strdup_printf("udp:127.0.0.1:%d",
427 ntohs(addr.sin_port));
428 chr = qemu_chr_new("client", tmp);
429 g_assert_nonnull(chr);
431 d.chr = chr;
432 qemu_chr_fe_init(&be, chr, &error_abort);
433 qemu_chr_fe_set_handlers(&be, socket_can_read_hello, socket_read_hello,
434 NULL, &d, NULL, true);
435 ret = qemu_chr_write_all(chr, (uint8_t *)"hello", 5);
436 g_assert_cmpint(ret, ==, 5);
438 alen = sizeof(addr);
439 ret = recvfrom(sock, buf, sizeof(buf), 0,
440 (struct sockaddr *)&other, &alen);
441 g_assert_cmpint(ret, ==, 5);
442 ret = sendto(sock, buf, 5, 0, (struct sockaddr *)&other, alen);
443 g_assert_cmpint(ret, ==, 5);
445 main_loop();
447 close(sock);
448 g_free(tmp);
451 #ifdef HAVE_CHARDEV_SERIAL
452 static void char_serial_test(void)
454 QemuOpts *opts;
455 Chardev *chr;
457 opts = qemu_opts_create(qemu_find_opts("chardev"), "serial-id",
458 1, &error_abort);
459 qemu_opt_set(opts, "backend", "serial", &error_abort);
460 qemu_opt_set(opts, "path", "/dev/null", &error_abort);
462 chr = qemu_chr_new_from_opts(opts, NULL);
463 g_assert_nonnull(chr);
464 /* TODO: add more tests with a pty */
465 object_unparent(OBJECT(chr));
467 /* test tty alias */
468 qemu_opt_set(opts, "backend", "tty", &error_abort);
469 chr = qemu_chr_new_from_opts(opts, NULL);
470 g_assert_nonnull(chr);
471 object_unparent(OBJECT(chr));
473 qemu_opts_del(opts);
475 #endif
477 static void char_file_test(void)
479 char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
480 char *out = g_build_filename(tmp_path, "out", NULL);
481 char *contents = NULL;
482 ChardevFile file = { .out = out };
483 ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
484 .u.file.data = &file };
485 Chardev *chr;
486 gsize length;
487 int ret;
489 chr = qemu_chardev_new(NULL, TYPE_CHARDEV_FILE, &backend,
490 &error_abort);
491 ret = qemu_chr_write_all(chr, (uint8_t *)"hello!", 6);
492 g_assert_cmpint(ret, ==, 6);
493 object_unref(OBJECT(chr));
495 ret = g_file_get_contents(out, &contents, &length, NULL);
496 g_assert(ret == TRUE);
497 g_assert_cmpint(length, ==, 6);
498 g_assert(strncmp(contents, "hello!", 6) == 0);
499 g_free(contents);
501 #ifndef _WIN32
503 CharBackend be;
504 FeHandler fe = { 0, };
505 char *fifo = g_build_filename(tmp_path, "fifo", NULL);
506 int fd;
508 if (mkfifo(fifo, 0600) < 0) {
509 abort();
512 fd = open(fifo, O_RDWR);
513 ret = write(fd, "fifo-in", 8);
514 g_assert_cmpint(ret, ==, 8);
516 file.in = fifo;
517 file.has_in = true;
518 chr = qemu_chardev_new("label-file", TYPE_CHARDEV_FILE, &backend,
519 &error_abort);
521 qemu_chr_fe_init(&be, chr, &error_abort);
522 qemu_chr_fe_set_handlers(&be,
523 fe_can_read,
524 fe_read,
525 fe_event,
526 &fe, NULL, true);
528 g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK);
529 qmp_chardev_send_break("label-foo", NULL);
530 g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK);
531 qmp_chardev_send_break("label-file", NULL);
532 g_assert_cmpint(fe.last_event, ==, CHR_EVENT_BREAK);
534 main_loop();
536 close(fd);
538 g_assert_cmpint(fe.read_count, ==, 8);
539 g_assert_cmpstr(fe.read_buf, ==, "fifo-in");
540 qemu_chr_fe_deinit(&be, true);
541 g_unlink(fifo);
542 g_free(fifo);
544 #endif
546 g_unlink(out);
547 g_rmdir(tmp_path);
548 g_free(tmp_path);
549 g_free(out);
552 static void char_null_test(void)
554 Error *err = NULL;
555 Chardev *chr;
556 CharBackend be;
557 int ret;
559 chr = qemu_chr_find("label-null");
560 g_assert_null(chr);
562 chr = qemu_chr_new("label-null", "null");
563 chr = qemu_chr_find("label-null");
564 g_assert_nonnull(chr);
566 g_assert(qemu_chr_has_feature(chr,
567 QEMU_CHAR_FEATURE_FD_PASS) == false);
568 g_assert(qemu_chr_has_feature(chr,
569 QEMU_CHAR_FEATURE_RECONNECTABLE) == false);
571 /* check max avail */
572 qemu_chr_fe_init(&be, chr, &error_abort);
573 qemu_chr_fe_init(&be, chr, &err);
574 error_free_or_abort(&err);
576 /* deinit & reinit */
577 qemu_chr_fe_deinit(&be, false);
578 qemu_chr_fe_init(&be, chr, &error_abort);
580 qemu_chr_fe_set_open(&be, true);
582 qemu_chr_fe_set_handlers(&be,
583 fe_can_read,
584 fe_read,
585 fe_event,
586 NULL, NULL, true);
588 ret = qemu_chr_fe_write(&be, (void *)"buf", 4);
589 g_assert_cmpint(ret, ==, 4);
591 qemu_chr_fe_deinit(&be, true);
594 static void char_invalid_test(void)
596 Chardev *chr;
598 chr = qemu_chr_new("label-invalid", "invalid");
599 g_assert_null(chr);
602 int main(int argc, char **argv)
604 qemu_init_main_loop(&error_abort);
605 socket_init();
607 g_test_init(&argc, &argv, NULL);
609 module_call_init(MODULE_INIT_QOM);
610 qemu_add_opts(&qemu_chardev_opts);
612 g_test_add_func("/char/null", char_null_test);
613 g_test_add_func("/char/invalid", char_invalid_test);
614 g_test_add_func("/char/ringbuf", char_ringbuf_test);
615 g_test_add_func("/char/mux", char_mux_test);
616 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
617 #ifdef _WIN32
618 g_test_add_func("/char/console/subprocess", char_console_test_subprocess);
619 g_test_add_func("/char/console", char_console_test);
620 #endif
621 g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess);
622 g_test_add_func("/char/stdio", char_stdio_test);
623 #endif
624 #ifndef _WIN32
625 g_test_add_func("/char/pipe", char_pipe_test);
626 #endif
627 g_test_add_func("/char/file", char_file_test);
628 g_test_add_func("/char/socket", char_socket_test);
629 g_test_add_func("/char/udp", char_udp_test);
630 #ifdef HAVE_CHARDEV_SERIAL
631 g_test_add_func("/char/serial", char_serial_test);
632 #endif
634 return g_test_run();