1 #include "qemu/osdep.h"
2 #include <glib/gstdio.h>
4 #include "qemu/config-file.h"
5 #include "qemu/option.h"
6 #include "qemu/sockets.h"
7 #include "chardev/char-fe.h"
8 #include "chardev/char-mux.h"
9 #include "sysemu/sysemu.h"
10 #include "qapi/error.h"
11 #include "qapi/qapi-commands-char.h"
12 #include "qapi/qmp/qdict.h"
13 #include "qom/qom-qobject.h"
17 typedef struct FeHandler {
23 static void main_loop(void)
27 main_loop_wait(false);
31 static int fe_can_read(void *opaque)
33 FeHandler *h = opaque;
35 return sizeof(h->read_buf) - h->read_count;
38 static void fe_read(void *opaque, const uint8_t *buf, int size)
40 FeHandler *h = opaque;
42 g_assert_cmpint(size, <=, fe_can_read(opaque));
44 memcpy(h->read_buf + h->read_count, buf, size);
45 h->read_count += size;
49 static void fe_event(void *opaque, int event)
51 FeHandler *h = opaque;
53 h->last_event = event;
54 if (event != CHR_EVENT_BREAK) {
59 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
61 static void char_console_test_subprocess(void)
66 opts = qemu_opts_create(qemu_find_opts("chardev"), "console-label",
68 qemu_opt_set(opts, "backend", "console", &error_abort);
70 chr = qemu_chr_new_from_opts(opts, NULL);
71 g_assert_nonnull(chr);
73 qemu_chr_write_all(chr, (const uint8_t *)"CONSOLE", 7);
76 object_unparent(OBJECT(chr));
79 static void char_console_test(void)
81 g_test_trap_subprocess("/char/console/subprocess", 0, 0);
82 g_test_trap_assert_passed();
83 g_test_trap_assert_stdout("CONSOLE");
86 static void char_stdio_test_subprocess(void)
92 chr = qemu_chr_new("label", "stdio");
93 g_assert_nonnull(chr);
95 qemu_chr_fe_init(&be, chr, &error_abort);
96 qemu_chr_fe_set_open(&be, true);
97 ret = qemu_chr_fe_write(&be, (void *)"buf", 4);
98 g_assert_cmpint(ret, ==, 4);
100 qemu_chr_fe_deinit(&be, true);
103 static void char_stdio_test(void)
105 g_test_trap_subprocess("/char/stdio/subprocess", 0, 0);
106 g_test_trap_assert_passed();
107 g_test_trap_assert_stdout("buf");
111 static void char_ringbuf_test(void)
119 opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
121 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
123 qemu_opt_set(opts, "size", "5", &error_abort);
124 chr = qemu_chr_new_from_opts(opts, NULL);
128 opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
130 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
131 qemu_opt_set(opts, "size", "2", &error_abort);
132 chr = qemu_chr_new_from_opts(opts, &error_abort);
133 g_assert_nonnull(chr);
136 qemu_chr_fe_init(&be, chr, &error_abort);
137 ret = qemu_chr_fe_write(&be, (void *)"buff", 4);
138 g_assert_cmpint(ret, ==, 4);
140 data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
141 g_assert_cmpstr(data, ==, "ff");
144 data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
145 g_assert_cmpstr(data, ==, "");
148 qemu_chr_fe_deinit(&be, true);
151 opts = qemu_opts_create(qemu_find_opts("chardev"), "memory-label",
153 qemu_opt_set(opts, "backend", "memory", &error_abort);
154 qemu_opt_set(opts, "size", "2", &error_abort);
155 chr = qemu_chr_new_from_opts(opts, NULL);
156 g_assert_nonnull(chr);
157 object_unparent(OBJECT(chr));
161 static void char_mux_test(void)
166 FeHandler h1 = { 0, }, h2 = { 0, };
167 CharBackend chr_be1, chr_be2;
169 muxes_realized = true; /* done after machine init */
170 opts = qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
172 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
173 qemu_opt_set(opts, "size", "128", &error_abort);
174 qemu_opt_set(opts, "mux", "on", &error_abort);
175 chr = qemu_chr_new_from_opts(opts, &error_abort);
176 g_assert_nonnull(chr);
179 qemu_chr_fe_init(&chr_be1, chr, &error_abort);
180 qemu_chr_fe_set_handlers(&chr_be1,
188 qemu_chr_fe_init(&chr_be2, chr, &error_abort);
189 qemu_chr_fe_set_handlers(&chr_be2,
196 qemu_chr_fe_take_focus(&chr_be2);
198 base = qemu_chr_find("mux-label-base");
199 g_assert_cmpint(qemu_chr_be_can_write(base), !=, 0);
201 qemu_chr_be_write(base, (void *)"hello", 6);
202 g_assert_cmpint(h1.read_count, ==, 0);
203 g_assert_cmpint(h2.read_count, ==, 6);
204 g_assert_cmpstr(h2.read_buf, ==, "hello");
207 g_assert_cmpint(h1.last_event, !=, 42); /* should be MUX_OUT or OPENED */
208 g_assert_cmpint(h2.last_event, !=, 42); /* should be MUX_IN or OPENED */
209 /* sending event on the base broadcast to all fe, historical reasons? */
210 qemu_chr_be_event(base, 42);
211 g_assert_cmpint(h1.last_event, ==, 42);
212 g_assert_cmpint(h2.last_event, ==, 42);
213 qemu_chr_be_event(chr, -1);
214 g_assert_cmpint(h1.last_event, ==, 42);
215 g_assert_cmpint(h2.last_event, ==, -1);
218 qemu_chr_be_write(base, (void *)"\1c", 2);
219 g_assert_cmpint(h1.last_event, ==, CHR_EVENT_MUX_IN);
220 g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT);
221 qemu_chr_be_event(chr, -1);
222 g_assert_cmpint(h1.last_event, ==, -1);
223 g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT);
225 qemu_chr_be_write(base, (void *)"hello", 6);
226 g_assert_cmpint(h2.read_count, ==, 0);
227 g_assert_cmpint(h1.read_count, ==, 6);
228 g_assert_cmpstr(h1.read_buf, ==, "hello");
231 /* remove first handler */
232 qemu_chr_fe_set_handlers(&chr_be1, NULL, NULL, NULL, NULL,
234 qemu_chr_be_write(base, (void *)"hello", 6);
235 g_assert_cmpint(h1.read_count, ==, 0);
236 g_assert_cmpint(h2.read_count, ==, 0);
238 qemu_chr_be_write(base, (void *)"\1c", 2);
239 qemu_chr_be_write(base, (void *)"hello", 6);
240 g_assert_cmpint(h1.read_count, ==, 0);
241 g_assert_cmpint(h2.read_count, ==, 6);
242 g_assert_cmpstr(h2.read_buf, ==, "hello");
246 qemu_chr_be_write(base, (void *)"\1?", 2);
247 data = qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort);
248 g_assert_cmpint(strlen(data), !=, 0);
251 qemu_chr_fe_deinit(&chr_be1, false);
252 qemu_chr_fe_deinit(&chr_be2, true);
255 typedef struct SocketIdleData {
260 CharBackend *client_be;
263 static gboolean char_socket_test_idle(gpointer user_data)
265 SocketIdleData *data = user_data;
267 if (object_property_get_bool(OBJECT(data->chr), "connected", NULL)
268 == data->conn_expected) {
276 static void socket_read(void *opaque, const uint8_t *buf, int size)
278 SocketIdleData *data = opaque;
280 g_assert_cmpint(size, ==, 1);
281 g_assert_cmpint(*buf, ==, 'Z');
283 size = qemu_chr_fe_write(data->be, (const uint8_t *)"hello", 5);
284 g_assert_cmpint(size, ==, 5);
287 static int socket_can_read(void *opaque)
292 static void socket_read_hello(void *opaque, const uint8_t *buf, int size)
294 g_assert_cmpint(size, ==, 5);
295 g_assert(strncmp((char *)buf, "hello", 5) == 0);
300 static int socket_can_read_hello(void *opaque)
305 static void char_socket_test(void)
307 Chardev *chr = qemu_chr_new("server", "tcp:127.0.0.1:0,server,nowait");
312 SocketIdleData d = { .chr = chr };
314 CharBackend client_be;
320 g_assert_nonnull(chr);
321 g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
323 addr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
324 qdict = qobject_to_qdict(addr);
325 port = qdict_get_str(qdict, "port");
326 tmp = g_strdup_printf("tcp:127.0.0.1:%s", port);
329 qemu_chr_fe_init(&be, chr, &error_abort);
330 qemu_chr_fe_set_handlers(&be, socket_can_read, socket_read,
331 NULL, NULL, &d, NULL, true);
333 chr_client = qemu_chr_new("client", tmp);
334 qemu_chr_fe_init(&client_be, chr_client, &error_abort);
335 qemu_chr_fe_set_handlers(&client_be, socket_can_read_hello,
337 NULL, NULL, &d, NULL, true);
340 d.conn_expected = true;
341 guint id = g_idle_add(char_socket_test_idle, &d);
342 g_source_set_name_by_id(id, "test-idle");
343 g_assert_cmpint(id, >, 0);
346 g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
347 g_assert(object_property_get_bool(OBJECT(chr_client),
348 "connected", &error_abort));
350 qemu_chr_write_all(chr_client, (const uint8_t *)"Z", 1);
353 object_unparent(OBJECT(chr_client));
355 d.conn_expected = false;
356 g_idle_add(char_socket_test_idle, &d);
359 object_unparent(OBJECT(chr));
363 static void char_pipe_test(void)
365 gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
366 gchar *tmp, *in, *out, *pipe = g_build_filename(tmp_path, "pipe", NULL);
371 FeHandler fe = { 0, };
373 in = g_strdup_printf("%s.in", pipe);
374 if (mkfifo(in, 0600) < 0) {
377 out = g_strdup_printf("%s.out", pipe);
378 if (mkfifo(out, 0600) < 0) {
382 tmp = g_strdup_printf("pipe:%s", pipe);
383 chr = qemu_chr_new("pipe", tmp);
384 g_assert_nonnull(chr);
387 qemu_chr_fe_init(&be, chr, &error_abort);
389 ret = qemu_chr_fe_write(&be, (void *)"pipe-out", 9);
390 g_assert_cmpint(ret, ==, 9);
392 fd = open(out, O_RDWR);
393 ret = read(fd, buf, sizeof(buf));
394 g_assert_cmpint(ret, ==, 9);
395 g_assert_cmpstr(buf, ==, "pipe-out");
398 fd = open(in, O_WRONLY);
399 ret = write(fd, "pipe-in", 8);
400 g_assert_cmpint(ret, ==, 8);
403 qemu_chr_fe_set_handlers(&be,
413 g_assert_cmpint(fe.read_count, ==, 8);
414 g_assert_cmpstr(fe.read_buf, ==, "pipe-in");
416 qemu_chr_fe_deinit(&be, true);
418 g_assert(g_unlink(in) == 0);
419 g_assert(g_unlink(out) == 0);
420 g_assert(g_rmdir(tmp_path) == 0);
428 static int make_udp_socket(int *port)
430 struct sockaddr_in addr = { 0, };
431 socklen_t alen = sizeof(addr);
432 int ret, sock = qemu_socket(PF_INET, SOCK_DGRAM, 0);
434 g_assert_cmpint(sock, >, 0);
435 addr.sin_family = AF_INET ;
436 addr.sin_addr.s_addr = htonl(INADDR_ANY);
438 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
439 g_assert_cmpint(ret, ==, 0);
440 ret = getsockname(sock, (struct sockaddr *)&addr, &alen);
441 g_assert_cmpint(ret, ==, 0);
443 *port = ntohs(addr.sin_port);
447 static void char_udp_test_internal(Chardev *reuse_chr, int sock)
449 struct sockaddr_in other;
450 SocketIdleData d = { 0, };
453 socklen_t alen = sizeof(other);
463 sock = make_udp_socket(&port);
464 tmp = g_strdup_printf("udp:127.0.0.1:%d", port);
465 chr = qemu_chr_new("client", tmp);
466 g_assert_nonnull(chr);
468 be = g_alloca(sizeof(CharBackend));
469 qemu_chr_fe_init(be, chr, &error_abort);
473 qemu_chr_fe_set_handlers(be, socket_can_read_hello, socket_read_hello,
474 NULL, NULL, &d, NULL, true);
475 ret = qemu_chr_write_all(chr, (uint8_t *)"hello", 5);
476 g_assert_cmpint(ret, ==, 5);
478 ret = recvfrom(sock, buf, sizeof(buf), 0,
479 (struct sockaddr *)&other, &alen);
480 g_assert_cmpint(ret, ==, 5);
481 ret = sendto(sock, buf, 5, 0, (struct sockaddr *)&other, alen);
482 g_assert_cmpint(ret, ==, 5);
488 qemu_chr_fe_deinit(be, true);
493 static void char_udp_test(void)
495 char_udp_test_internal(NULL, 0);
498 #ifdef HAVE_CHARDEV_SERIAL
499 static void char_serial_test(void)
504 opts = qemu_opts_create(qemu_find_opts("chardev"), "serial-id",
506 qemu_opt_set(opts, "backend", "serial", &error_abort);
507 qemu_opt_set(opts, "path", "/dev/null", &error_abort);
509 chr = qemu_chr_new_from_opts(opts, NULL);
510 g_assert_nonnull(chr);
511 /* TODO: add more tests with a pty */
512 object_unparent(OBJECT(chr));
515 qemu_opt_set(opts, "backend", "tty", &error_abort);
516 chr = qemu_chr_new_from_opts(opts, NULL);
517 g_assert_nonnull(chr);
518 object_unparent(OBJECT(chr));
525 static void char_file_fifo_test(void)
529 char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
530 char *fifo = g_build_filename(tmp_path, "fifo", NULL);
531 char *out = g_build_filename(tmp_path, "out", NULL);
532 ChardevFile file = { .in = fifo,
535 ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
536 .u.file.data = &file };
537 FeHandler fe = { 0, };
540 if (mkfifo(fifo, 0600) < 0) {
544 fd = open(fifo, O_RDWR);
545 ret = write(fd, "fifo-in", 8);
546 g_assert_cmpint(ret, ==, 8);
548 chr = qemu_chardev_new("label-file", TYPE_CHARDEV_FILE, &backend,
551 qemu_chr_fe_init(&be, chr, &error_abort);
552 qemu_chr_fe_set_handlers(&be,
559 g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK);
560 qmp_chardev_send_break("label-foo", NULL);
561 g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK);
562 qmp_chardev_send_break("label-file", NULL);
563 g_assert_cmpint(fe.last_event, ==, CHR_EVENT_BREAK);
569 g_assert_cmpint(fe.read_count, ==, 8);
570 g_assert_cmpstr(fe.read_buf, ==, "fifo-in");
572 qemu_chr_fe_deinit(&be, true);
583 static void char_file_test_internal(Chardev *ext_chr, const char *filepath)
585 char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
588 char *contents = NULL;
589 ChardevFile file = {};
590 ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
591 .u.file.data = &file };
597 out = g_strdup(filepath);
600 out = g_build_filename(tmp_path, "out", NULL);
602 chr = qemu_chardev_new(NULL, TYPE_CHARDEV_FILE, &backend,
605 ret = qemu_chr_write_all(chr, (uint8_t *)"hello!", 6);
606 g_assert_cmpint(ret, ==, 6);
608 ret = g_file_get_contents(out, &contents, &length, NULL);
609 g_assert(ret == TRUE);
610 g_assert_cmpint(length, ==, 6);
611 g_assert(strncmp(contents, "hello!", 6) == 0);
614 object_unref(OBJECT(chr));
623 static void char_file_test(void)
625 char_file_test_internal(NULL, NULL);
628 static void char_null_test(void)
635 chr = qemu_chr_find("label-null");
638 chr = qemu_chr_new("label-null", "null");
639 chr = qemu_chr_find("label-null");
640 g_assert_nonnull(chr);
642 g_assert(qemu_chr_has_feature(chr,
643 QEMU_CHAR_FEATURE_FD_PASS) == false);
644 g_assert(qemu_chr_has_feature(chr,
645 QEMU_CHAR_FEATURE_RECONNECTABLE) == false);
647 /* check max avail */
648 qemu_chr_fe_init(&be, chr, &error_abort);
649 qemu_chr_fe_init(&be, chr, &err);
650 error_free_or_abort(&err);
652 /* deinit & reinit */
653 qemu_chr_fe_deinit(&be, false);
654 qemu_chr_fe_init(&be, chr, &error_abort);
656 qemu_chr_fe_set_open(&be, true);
658 qemu_chr_fe_set_handlers(&be,
665 ret = qemu_chr_fe_write(&be, (void *)"buf", 4);
666 g_assert_cmpint(ret, ==, 4);
668 qemu_chr_fe_deinit(&be, true);
671 static void char_invalid_test(void)
675 chr = qemu_chr_new("label-invalid", "invalid");
679 static int chardev_change(void *opaque)
684 static int chardev_change_denied(void *opaque)
689 static void char_hotswap_test(void)
695 gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
696 char *filename = g_build_filename(tmp_path, "file", NULL);
697 ChardevFile file = { .out = filename };
698 ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
699 .u.file.data = &file };
703 int sock = make_udp_socket(&port);
704 g_assert_cmpint(sock, >, 0);
706 chr_args = g_strdup_printf("udp:127.0.0.1:%d", port);
708 chr = qemu_chr_new("chardev", chr_args);
709 qemu_chr_fe_init(&be, chr, &error_abort);
711 /* check that chardev operates correctly */
712 char_udp_test_internal(chr, sock);
714 /* set the handler that denies the hotswap */
715 qemu_chr_fe_set_handlers(&be, NULL, NULL,
716 NULL, chardev_change_denied, NULL, NULL, true);
718 /* now, change is denied and has to keep the old backend operating */
719 ret = qmp_chardev_change("chardev", &backend, NULL);
721 g_assert(be.chr == chr);
723 char_udp_test_internal(chr, sock);
725 /* now allow the change */
726 qemu_chr_fe_set_handlers(&be, NULL, NULL,
727 NULL, chardev_change, NULL, NULL, true);
729 /* has to succeed now */
730 ret = qmp_chardev_change("chardev", &backend, &error_abort);
731 g_assert(be.chr != chr);
736 /* run the file chardev test */
737 char_file_test_internal(chr, filename);
739 object_unparent(OBJECT(chr));
741 qapi_free_ChardevReturn(ret);
749 int main(int argc, char **argv)
751 qemu_init_main_loop(&error_abort);
754 g_test_init(&argc, &argv, NULL);
756 module_call_init(MODULE_INIT_QOM);
757 qemu_add_opts(&qemu_chardev_opts);
759 g_test_add_func("/char/null", char_null_test);
760 g_test_add_func("/char/invalid", char_invalid_test);
761 g_test_add_func("/char/ringbuf", char_ringbuf_test);
762 g_test_add_func("/char/mux", char_mux_test);
763 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
765 g_test_add_func("/char/console/subprocess", char_console_test_subprocess);
766 g_test_add_func("/char/console", char_console_test);
768 g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess);
769 g_test_add_func("/char/stdio", char_stdio_test);
772 g_test_add_func("/char/pipe", char_pipe_test);
774 g_test_add_func("/char/file", char_file_test);
776 g_test_add_func("/char/file-fifo", char_file_fifo_test);
778 g_test_add_func("/char/socket", char_socket_test);
779 g_test_add_func("/char/udp", char_udp_test);
780 #ifdef HAVE_CHARDEV_SERIAL
781 g_test_add_func("/char/serial", char_serial_test);
783 g_test_add_func("/char/hotswap", char_hotswap_test);