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 opts
= qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
171 qemu_opt_set(opts
, "backend", "ringbuf", &error_abort
);
172 qemu_opt_set(opts
, "size", "128", &error_abort
);
173 qemu_opt_set(opts
, "mux", "on", &error_abort
);
174 chr
= qemu_chr_new_from_opts(opts
, &error_abort
);
175 g_assert_nonnull(chr
);
178 qemu_chr_fe_init(&chr_be1
, chr
, &error_abort
);
179 qemu_chr_fe_set_handlers(&chr_be1
,
187 qemu_chr_fe_init(&chr_be2
, chr
, &error_abort
);
188 qemu_chr_fe_set_handlers(&chr_be2
,
195 qemu_chr_fe_take_focus(&chr_be2
);
197 base
= qemu_chr_find("mux-label-base");
198 g_assert_cmpint(qemu_chr_be_can_write(base
), !=, 0);
200 qemu_chr_be_write(base
, (void *)"hello", 6);
201 g_assert_cmpint(h1
.read_count
, ==, 0);
202 g_assert_cmpint(h2
.read_count
, ==, 6);
203 g_assert_cmpstr(h2
.read_buf
, ==, "hello");
206 g_assert_cmpint(h1
.last_event
, !=, 42); /* should be MUX_OUT or OPENED */
207 g_assert_cmpint(h2
.last_event
, !=, 42); /* should be MUX_IN or OPENED */
208 /* sending event on the base broadcast to all fe, historical reasons? */
209 qemu_chr_be_event(base
, 42);
210 g_assert_cmpint(h1
.last_event
, ==, 42);
211 g_assert_cmpint(h2
.last_event
, ==, 42);
212 qemu_chr_be_event(chr
, -1);
213 g_assert_cmpint(h1
.last_event
, ==, 42);
214 g_assert_cmpint(h2
.last_event
, ==, -1);
217 qemu_chr_be_write(base
, (void *)"\1b", 2);
218 g_assert_cmpint(h1
.last_event
, ==, 42);
219 g_assert_cmpint(h2
.last_event
, ==, CHR_EVENT_BREAK
);
221 qemu_chr_be_write(base
, (void *)"\1c", 2);
222 g_assert_cmpint(h1
.last_event
, ==, CHR_EVENT_MUX_IN
);
223 g_assert_cmpint(h2
.last_event
, ==, CHR_EVENT_MUX_OUT
);
224 qemu_chr_be_event(chr
, -1);
225 g_assert_cmpint(h1
.last_event
, ==, -1);
226 g_assert_cmpint(h2
.last_event
, ==, CHR_EVENT_MUX_OUT
);
228 qemu_chr_be_write(base
, (void *)"hello", 6);
229 g_assert_cmpint(h2
.read_count
, ==, 0);
230 g_assert_cmpint(h1
.read_count
, ==, 6);
231 g_assert_cmpstr(h1
.read_buf
, ==, "hello");
234 qemu_chr_be_write(base
, (void *)"\1b", 2);
235 g_assert_cmpint(h1
.last_event
, ==, CHR_EVENT_BREAK
);
236 g_assert_cmpint(h2
.last_event
, ==, CHR_EVENT_MUX_OUT
);
238 /* remove first handler */
239 qemu_chr_fe_set_handlers(&chr_be1
, NULL
, NULL
, NULL
, NULL
,
241 qemu_chr_be_write(base
, (void *)"hello", 6);
242 g_assert_cmpint(h1
.read_count
, ==, 0);
243 g_assert_cmpint(h2
.read_count
, ==, 0);
245 qemu_chr_be_write(base
, (void *)"\1c", 2);
246 qemu_chr_be_write(base
, (void *)"hello", 6);
247 g_assert_cmpint(h1
.read_count
, ==, 0);
248 g_assert_cmpint(h2
.read_count
, ==, 6);
249 g_assert_cmpstr(h2
.read_buf
, ==, "hello");
253 qemu_chr_be_write(base
, (void *)"\1?", 2);
254 data
= qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort
);
255 g_assert_cmpint(strlen(data
), !=, 0);
258 qemu_chr_fe_deinit(&chr_be1
, false);
259 qemu_chr_fe_deinit(&chr_be2
, true);
262 typedef struct SocketIdleData
{
267 CharBackend
*client_be
;
270 static gboolean
char_socket_test_idle(gpointer user_data
)
272 SocketIdleData
*data
= user_data
;
274 if (object_property_get_bool(OBJECT(data
->chr
), "connected", NULL
)
275 == data
->conn_expected
) {
283 static void socket_read(void *opaque
, const uint8_t *buf
, int size
)
285 SocketIdleData
*data
= opaque
;
287 g_assert_cmpint(size
, ==, 1);
288 g_assert_cmpint(*buf
, ==, 'Z');
290 size
= qemu_chr_fe_write(data
->be
, (const uint8_t *)"hello", 5);
291 g_assert_cmpint(size
, ==, 5);
294 static int socket_can_read(void *opaque
)
299 static void socket_read_hello(void *opaque
, const uint8_t *buf
, int size
)
301 g_assert_cmpint(size
, ==, 5);
302 g_assert(strncmp((char *)buf
, "hello", 5) == 0);
307 static int socket_can_read_hello(void *opaque
)
312 static void char_socket_test_common(Chardev
*chr
)
318 SocketIdleData d
= { .chr
= chr
};
320 CharBackend client_be
;
326 g_assert_nonnull(chr
);
327 g_assert(!object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
329 addr
= object_property_get_qobject(OBJECT(chr
), "addr", &error_abort
);
330 qdict
= qobject_to(QDict
, addr
);
331 port
= qdict_get_str(qdict
, "port");
332 tmp
= g_strdup_printf("tcp:127.0.0.1:%s", port
);
333 qobject_unref(qdict
);
335 qemu_chr_fe_init(&be
, chr
, &error_abort
);
336 qemu_chr_fe_set_handlers(&be
, socket_can_read
, socket_read
,
337 NULL
, NULL
, &d
, NULL
, true);
339 chr_client
= qemu_chr_new("client", tmp
);
340 qemu_chr_fe_init(&client_be
, chr_client
, &error_abort
);
341 qemu_chr_fe_set_handlers(&client_be
, socket_can_read_hello
,
343 NULL
, NULL
, &d
, NULL
, true);
346 d
.conn_expected
= true;
347 guint id
= g_idle_add(char_socket_test_idle
, &d
);
348 g_source_set_name_by_id(id
, "test-idle");
349 g_assert_cmpint(id
, >, 0);
352 g_assert(object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
353 g_assert(object_property_get_bool(OBJECT(chr_client
),
354 "connected", &error_abort
));
356 qemu_chr_write_all(chr_client
, (const uint8_t *)"Z", 1);
359 object_unparent(OBJECT(chr_client
));
361 d
.conn_expected
= false;
362 g_idle_add(char_socket_test_idle
, &d
);
365 object_unparent(OBJECT(chr
));
369 static void char_socket_basic_test(void)
371 Chardev
*chr
= qemu_chr_new("server", "tcp:127.0.0.1:0,server,nowait");
373 char_socket_test_common(chr
);
377 static void char_socket_fdpass_test(void)
383 SocketAddress
*addr
= g_new0(SocketAddress
, 1);
385 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
386 addr
->u
.inet
.host
= g_strdup("127.0.0.1");
387 addr
->u
.inet
.port
= g_strdup("0");
389 fd
= socket_listen(addr
, &error_abort
);
392 qapi_free_SocketAddress(addr
);
394 optstr
= g_strdup_printf("socket,id=cdev,fd=%d,server,nowait", fd
);
396 opts
= qemu_opts_parse_noisily(qemu_find_opts("chardev"),
399 g_assert_nonnull(opts
);
401 chr
= qemu_chr_new_from_opts(opts
, &error_abort
);
405 char_socket_test_common(chr
);
410 static void char_pipe_test(void)
412 gchar
*tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
413 gchar
*tmp
, *in
, *out
, *pipe
= g_build_filename(tmp_path
, "pipe", NULL
);
418 FeHandler fe
= { 0, };
420 in
= g_strdup_printf("%s.in", pipe
);
421 if (mkfifo(in
, 0600) < 0) {
424 out
= g_strdup_printf("%s.out", pipe
);
425 if (mkfifo(out
, 0600) < 0) {
429 tmp
= g_strdup_printf("pipe:%s", pipe
);
430 chr
= qemu_chr_new("pipe", tmp
);
431 g_assert_nonnull(chr
);
434 qemu_chr_fe_init(&be
, chr
, &error_abort
);
436 ret
= qemu_chr_fe_write(&be
, (void *)"pipe-out", 9);
437 g_assert_cmpint(ret
, ==, 9);
439 fd
= open(out
, O_RDWR
);
440 ret
= read(fd
, buf
, sizeof(buf
));
441 g_assert_cmpint(ret
, ==, 9);
442 g_assert_cmpstr(buf
, ==, "pipe-out");
445 fd
= open(in
, O_WRONLY
);
446 ret
= write(fd
, "pipe-in", 8);
447 g_assert_cmpint(ret
, ==, 8);
450 qemu_chr_fe_set_handlers(&be
,
460 g_assert_cmpint(fe
.read_count
, ==, 8);
461 g_assert_cmpstr(fe
.read_buf
, ==, "pipe-in");
463 qemu_chr_fe_deinit(&be
, true);
465 g_assert(g_unlink(in
) == 0);
466 g_assert(g_unlink(out
) == 0);
467 g_assert(g_rmdir(tmp_path
) == 0);
475 static int make_udp_socket(int *port
)
477 struct sockaddr_in addr
= { 0, };
478 socklen_t alen
= sizeof(addr
);
479 int ret
, sock
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
481 g_assert_cmpint(sock
, >, 0);
482 addr
.sin_family
= AF_INET
;
483 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
485 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
486 g_assert_cmpint(ret
, ==, 0);
487 ret
= getsockname(sock
, (struct sockaddr
*)&addr
, &alen
);
488 g_assert_cmpint(ret
, ==, 0);
490 *port
= ntohs(addr
.sin_port
);
494 static void char_udp_test_internal(Chardev
*reuse_chr
, int sock
)
496 struct sockaddr_in other
;
497 SocketIdleData d
= { 0, };
500 socklen_t alen
= sizeof(other
);
510 sock
= make_udp_socket(&port
);
511 tmp
= g_strdup_printf("udp:127.0.0.1:%d", port
);
512 chr
= qemu_chr_new("client", tmp
);
513 g_assert_nonnull(chr
);
515 be
= g_alloca(sizeof(CharBackend
));
516 qemu_chr_fe_init(be
, chr
, &error_abort
);
520 qemu_chr_fe_set_handlers(be
, socket_can_read_hello
, socket_read_hello
,
521 NULL
, NULL
, &d
, NULL
, true);
522 ret
= qemu_chr_write_all(chr
, (uint8_t *)"hello", 5);
523 g_assert_cmpint(ret
, ==, 5);
525 ret
= recvfrom(sock
, buf
, sizeof(buf
), 0,
526 (struct sockaddr
*)&other
, &alen
);
527 g_assert_cmpint(ret
, ==, 5);
528 ret
= sendto(sock
, buf
, 5, 0, (struct sockaddr
*)&other
, alen
);
529 g_assert_cmpint(ret
, ==, 5);
535 qemu_chr_fe_deinit(be
, true);
540 static void char_udp_test(void)
542 char_udp_test_internal(NULL
, 0);
545 #ifdef HAVE_CHARDEV_SERIAL
546 static void char_serial_test(void)
551 opts
= qemu_opts_create(qemu_find_opts("chardev"), "serial-id",
553 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
554 qemu_opt_set(opts
, "path", "/dev/null", &error_abort
);
556 chr
= qemu_chr_new_from_opts(opts
, NULL
);
557 g_assert_nonnull(chr
);
558 /* TODO: add more tests with a pty */
559 object_unparent(OBJECT(chr
));
562 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
563 chr
= qemu_chr_new_from_opts(opts
, NULL
);
564 g_assert_nonnull(chr
);
565 object_unparent(OBJECT(chr
));
572 static void char_file_fifo_test(void)
576 char *tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
577 char *fifo
= g_build_filename(tmp_path
, "fifo", NULL
);
578 char *out
= g_build_filename(tmp_path
, "out", NULL
);
579 ChardevFile file
= { .in
= fifo
,
582 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
583 .u
.file
.data
= &file
};
584 FeHandler fe
= { 0, };
587 if (mkfifo(fifo
, 0600) < 0) {
591 fd
= open(fifo
, O_RDWR
);
592 ret
= write(fd
, "fifo-in", 8);
593 g_assert_cmpint(ret
, ==, 8);
595 chr
= qemu_chardev_new("label-file", TYPE_CHARDEV_FILE
, &backend
,
598 qemu_chr_fe_init(&be
, chr
, &error_abort
);
599 qemu_chr_fe_set_handlers(&be
,
606 g_assert_cmpint(fe
.last_event
, !=, CHR_EVENT_BREAK
);
607 qmp_chardev_send_break("label-foo", NULL
);
608 g_assert_cmpint(fe
.last_event
, !=, CHR_EVENT_BREAK
);
609 qmp_chardev_send_break("label-file", NULL
);
610 g_assert_cmpint(fe
.last_event
, ==, CHR_EVENT_BREAK
);
616 g_assert_cmpint(fe
.read_count
, ==, 8);
617 g_assert_cmpstr(fe
.read_buf
, ==, "fifo-in");
619 qemu_chr_fe_deinit(&be
, true);
630 static void char_file_test_internal(Chardev
*ext_chr
, const char *filepath
)
632 char *tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
635 char *contents
= NULL
;
636 ChardevFile file
= {};
637 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
638 .u
.file
.data
= &file
};
644 out
= g_strdup(filepath
);
647 out
= g_build_filename(tmp_path
, "out", NULL
);
649 chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_FILE
, &backend
,
652 ret
= qemu_chr_write_all(chr
, (uint8_t *)"hello!", 6);
653 g_assert_cmpint(ret
, ==, 6);
655 ret
= g_file_get_contents(out
, &contents
, &length
, NULL
);
656 g_assert(ret
== TRUE
);
657 g_assert_cmpint(length
, ==, 6);
658 g_assert(strncmp(contents
, "hello!", 6) == 0);
661 object_unref(OBJECT(chr
));
670 static void char_file_test(void)
672 char_file_test_internal(NULL
, NULL
);
675 static void char_null_test(void)
682 chr
= qemu_chr_find("label-null");
685 chr
= qemu_chr_new("label-null", "null");
686 chr
= qemu_chr_find("label-null");
687 g_assert_nonnull(chr
);
689 g_assert(qemu_chr_has_feature(chr
,
690 QEMU_CHAR_FEATURE_FD_PASS
) == false);
691 g_assert(qemu_chr_has_feature(chr
,
692 QEMU_CHAR_FEATURE_RECONNECTABLE
) == false);
694 /* check max avail */
695 qemu_chr_fe_init(&be
, chr
, &error_abort
);
696 qemu_chr_fe_init(&be
, chr
, &err
);
697 error_free_or_abort(&err
);
699 /* deinit & reinit */
700 qemu_chr_fe_deinit(&be
, false);
701 qemu_chr_fe_init(&be
, chr
, &error_abort
);
703 qemu_chr_fe_set_open(&be
, true);
705 qemu_chr_fe_set_handlers(&be
,
712 ret
= qemu_chr_fe_write(&be
, (void *)"buf", 4);
713 g_assert_cmpint(ret
, ==, 4);
715 qemu_chr_fe_deinit(&be
, true);
718 static void char_invalid_test(void)
722 chr
= qemu_chr_new("label-invalid", "invalid");
726 static int chardev_change(void *opaque
)
731 static int chardev_change_denied(void *opaque
)
736 static void char_hotswap_test(void)
742 gchar
*tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
743 char *filename
= g_build_filename(tmp_path
, "file", NULL
);
744 ChardevFile file
= { .out
= filename
};
745 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
746 .u
.file
.data
= &file
};
750 int sock
= make_udp_socket(&port
);
751 g_assert_cmpint(sock
, >, 0);
753 chr_args
= g_strdup_printf("udp:127.0.0.1:%d", port
);
755 chr
= qemu_chr_new("chardev", chr_args
);
756 qemu_chr_fe_init(&be
, chr
, &error_abort
);
758 /* check that chardev operates correctly */
759 char_udp_test_internal(chr
, sock
);
761 /* set the handler that denies the hotswap */
762 qemu_chr_fe_set_handlers(&be
, NULL
, NULL
,
763 NULL
, chardev_change_denied
, NULL
, NULL
, true);
765 /* now, change is denied and has to keep the old backend operating */
766 ret
= qmp_chardev_change("chardev", &backend
, NULL
);
768 g_assert(be
.chr
== chr
);
770 char_udp_test_internal(chr
, sock
);
772 /* now allow the change */
773 qemu_chr_fe_set_handlers(&be
, NULL
, NULL
,
774 NULL
, chardev_change
, NULL
, NULL
, true);
776 /* has to succeed now */
777 ret
= qmp_chardev_change("chardev", &backend
, &error_abort
);
778 g_assert(be
.chr
!= chr
);
783 /* run the file chardev test */
784 char_file_test_internal(chr
, filename
);
786 object_unparent(OBJECT(chr
));
788 qapi_free_ChardevReturn(ret
);
796 int main(int argc
, char **argv
)
798 qemu_init_main_loop(&error_abort
);
801 g_test_init(&argc
, &argv
, NULL
);
803 module_call_init(MODULE_INIT_QOM
);
804 qemu_add_opts(&qemu_chardev_opts
);
806 g_test_add_func("/char/null", char_null_test
);
807 g_test_add_func("/char/invalid", char_invalid_test
);
808 g_test_add_func("/char/ringbuf", char_ringbuf_test
);
809 g_test_add_func("/char/mux", char_mux_test
);
810 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
812 g_test_add_func("/char/console/subprocess", char_console_test_subprocess
);
813 g_test_add_func("/char/console", char_console_test
);
815 g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess
);
816 g_test_add_func("/char/stdio", char_stdio_test
);
819 g_test_add_func("/char/pipe", char_pipe_test
);
821 g_test_add_func("/char/file", char_file_test
);
823 g_test_add_func("/char/file-fifo", char_file_fifo_test
);
825 g_test_add_func("/char/socket/basic", char_socket_basic_test
);
826 g_test_add_func("/char/socket/fdpass", char_socket_fdpass_test
);
827 g_test_add_func("/char/udp", char_udp_test
);
828 #ifdef HAVE_CHARDEV_SERIAL
829 g_test_add_func("/char/serial", char_serial_test
);
831 g_test_add_func("/char/hotswap", char_hotswap_test
);