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 *)"\1c", 2);
218 g_assert_cmpint(h1
.last_event
, ==, CHR_EVENT_MUX_IN
);
219 g_assert_cmpint(h2
.last_event
, ==, CHR_EVENT_MUX_OUT
);
220 qemu_chr_be_event(chr
, -1);
221 g_assert_cmpint(h1
.last_event
, ==, -1);
222 g_assert_cmpint(h2
.last_event
, ==, CHR_EVENT_MUX_OUT
);
224 qemu_chr_be_write(base
, (void *)"hello", 6);
225 g_assert_cmpint(h2
.read_count
, ==, 0);
226 g_assert_cmpint(h1
.read_count
, ==, 6);
227 g_assert_cmpstr(h1
.read_buf
, ==, "hello");
230 /* remove first handler */
231 qemu_chr_fe_set_handlers(&chr_be1
, NULL
, NULL
, NULL
, NULL
,
233 qemu_chr_be_write(base
, (void *)"hello", 6);
234 g_assert_cmpint(h1
.read_count
, ==, 0);
235 g_assert_cmpint(h2
.read_count
, ==, 0);
237 qemu_chr_be_write(base
, (void *)"\1c", 2);
238 qemu_chr_be_write(base
, (void *)"hello", 6);
239 g_assert_cmpint(h1
.read_count
, ==, 0);
240 g_assert_cmpint(h2
.read_count
, ==, 6);
241 g_assert_cmpstr(h2
.read_buf
, ==, "hello");
245 qemu_chr_be_write(base
, (void *)"\1?", 2);
246 data
= qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort
);
247 g_assert_cmpint(strlen(data
), !=, 0);
250 qemu_chr_fe_deinit(&chr_be1
, false);
251 qemu_chr_fe_deinit(&chr_be2
, true);
254 typedef struct SocketIdleData
{
259 CharBackend
*client_be
;
262 static gboolean
char_socket_test_idle(gpointer user_data
)
264 SocketIdleData
*data
= user_data
;
266 if (object_property_get_bool(OBJECT(data
->chr
), "connected", NULL
)
267 == data
->conn_expected
) {
275 static void socket_read(void *opaque
, const uint8_t *buf
, int size
)
277 SocketIdleData
*data
= opaque
;
279 g_assert_cmpint(size
, ==, 1);
280 g_assert_cmpint(*buf
, ==, 'Z');
282 size
= qemu_chr_fe_write(data
->be
, (const uint8_t *)"hello", 5);
283 g_assert_cmpint(size
, ==, 5);
286 static int socket_can_read(void *opaque
)
291 static void socket_read_hello(void *opaque
, const uint8_t *buf
, int size
)
293 g_assert_cmpint(size
, ==, 5);
294 g_assert(strncmp((char *)buf
, "hello", 5) == 0);
299 static int socket_can_read_hello(void *opaque
)
304 static void char_socket_test_common(Chardev
*chr
)
310 SocketIdleData d
= { .chr
= chr
};
312 CharBackend client_be
;
318 g_assert_nonnull(chr
);
319 g_assert(!object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
321 addr
= object_property_get_qobject(OBJECT(chr
), "addr", &error_abort
);
322 qdict
= qobject_to(QDict
, addr
);
323 port
= qdict_get_str(qdict
, "port");
324 tmp
= g_strdup_printf("tcp:127.0.0.1:%s", port
);
327 qemu_chr_fe_init(&be
, chr
, &error_abort
);
328 qemu_chr_fe_set_handlers(&be
, socket_can_read
, socket_read
,
329 NULL
, NULL
, &d
, NULL
, true);
331 chr_client
= qemu_chr_new("client", tmp
);
332 qemu_chr_fe_init(&client_be
, chr_client
, &error_abort
);
333 qemu_chr_fe_set_handlers(&client_be
, socket_can_read_hello
,
335 NULL
, NULL
, &d
, NULL
, true);
338 d
.conn_expected
= true;
339 guint id
= g_idle_add(char_socket_test_idle
, &d
);
340 g_source_set_name_by_id(id
, "test-idle");
341 g_assert_cmpint(id
, >, 0);
344 g_assert(object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
345 g_assert(object_property_get_bool(OBJECT(chr_client
),
346 "connected", &error_abort
));
348 qemu_chr_write_all(chr_client
, (const uint8_t *)"Z", 1);
351 object_unparent(OBJECT(chr_client
));
353 d
.conn_expected
= false;
354 g_idle_add(char_socket_test_idle
, &d
);
357 object_unparent(OBJECT(chr
));
361 static void char_socket_basic_test(void)
363 Chardev
*chr
= qemu_chr_new("server", "tcp:127.0.0.1:0,server,nowait");
365 char_socket_test_common(chr
);
369 static void char_socket_fdpass_test(void)
375 SocketAddress
*addr
= g_new0(SocketAddress
, 1);
377 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
378 addr
->u
.inet
.host
= g_strdup("127.0.0.1");
379 addr
->u
.inet
.port
= g_strdup("0");
381 fd
= socket_listen(addr
, &error_abort
);
384 qapi_free_SocketAddress(addr
);
386 optstr
= g_strdup_printf("socket,id=cdev,fd=%d,server,nowait", fd
);
388 opts
= qemu_opts_parse_noisily(qemu_find_opts("chardev"),
391 g_assert_nonnull(opts
);
393 chr
= qemu_chr_new_from_opts(opts
, &error_abort
);
397 char_socket_test_common(chr
);
402 static void char_pipe_test(void)
404 gchar
*tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
405 gchar
*tmp
, *in
, *out
, *pipe
= g_build_filename(tmp_path
, "pipe", NULL
);
410 FeHandler fe
= { 0, };
412 in
= g_strdup_printf("%s.in", pipe
);
413 if (mkfifo(in
, 0600) < 0) {
416 out
= g_strdup_printf("%s.out", pipe
);
417 if (mkfifo(out
, 0600) < 0) {
421 tmp
= g_strdup_printf("pipe:%s", pipe
);
422 chr
= qemu_chr_new("pipe", tmp
);
423 g_assert_nonnull(chr
);
426 qemu_chr_fe_init(&be
, chr
, &error_abort
);
428 ret
= qemu_chr_fe_write(&be
, (void *)"pipe-out", 9);
429 g_assert_cmpint(ret
, ==, 9);
431 fd
= open(out
, O_RDWR
);
432 ret
= read(fd
, buf
, sizeof(buf
));
433 g_assert_cmpint(ret
, ==, 9);
434 g_assert_cmpstr(buf
, ==, "pipe-out");
437 fd
= open(in
, O_WRONLY
);
438 ret
= write(fd
, "pipe-in", 8);
439 g_assert_cmpint(ret
, ==, 8);
442 qemu_chr_fe_set_handlers(&be
,
452 g_assert_cmpint(fe
.read_count
, ==, 8);
453 g_assert_cmpstr(fe
.read_buf
, ==, "pipe-in");
455 qemu_chr_fe_deinit(&be
, true);
457 g_assert(g_unlink(in
) == 0);
458 g_assert(g_unlink(out
) == 0);
459 g_assert(g_rmdir(tmp_path
) == 0);
467 static int make_udp_socket(int *port
)
469 struct sockaddr_in addr
= { 0, };
470 socklen_t alen
= sizeof(addr
);
471 int ret
, sock
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
473 g_assert_cmpint(sock
, >, 0);
474 addr
.sin_family
= AF_INET
;
475 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
477 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
478 g_assert_cmpint(ret
, ==, 0);
479 ret
= getsockname(sock
, (struct sockaddr
*)&addr
, &alen
);
480 g_assert_cmpint(ret
, ==, 0);
482 *port
= ntohs(addr
.sin_port
);
486 static void char_udp_test_internal(Chardev
*reuse_chr
, int sock
)
488 struct sockaddr_in other
;
489 SocketIdleData d
= { 0, };
492 socklen_t alen
= sizeof(other
);
502 sock
= make_udp_socket(&port
);
503 tmp
= g_strdup_printf("udp:127.0.0.1:%d", port
);
504 chr
= qemu_chr_new("client", tmp
);
505 g_assert_nonnull(chr
);
507 be
= g_alloca(sizeof(CharBackend
));
508 qemu_chr_fe_init(be
, chr
, &error_abort
);
512 qemu_chr_fe_set_handlers(be
, socket_can_read_hello
, socket_read_hello
,
513 NULL
, NULL
, &d
, NULL
, true);
514 ret
= qemu_chr_write_all(chr
, (uint8_t *)"hello", 5);
515 g_assert_cmpint(ret
, ==, 5);
517 ret
= recvfrom(sock
, buf
, sizeof(buf
), 0,
518 (struct sockaddr
*)&other
, &alen
);
519 g_assert_cmpint(ret
, ==, 5);
520 ret
= sendto(sock
, buf
, 5, 0, (struct sockaddr
*)&other
, alen
);
521 g_assert_cmpint(ret
, ==, 5);
527 qemu_chr_fe_deinit(be
, true);
532 static void char_udp_test(void)
534 char_udp_test_internal(NULL
, 0);
537 #ifdef HAVE_CHARDEV_SERIAL
538 static void char_serial_test(void)
543 opts
= qemu_opts_create(qemu_find_opts("chardev"), "serial-id",
545 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
546 qemu_opt_set(opts
, "path", "/dev/null", &error_abort
);
548 chr
= qemu_chr_new_from_opts(opts
, NULL
);
549 g_assert_nonnull(chr
);
550 /* TODO: add more tests with a pty */
551 object_unparent(OBJECT(chr
));
554 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
555 chr
= qemu_chr_new_from_opts(opts
, NULL
);
556 g_assert_nonnull(chr
);
557 object_unparent(OBJECT(chr
));
564 static void char_file_fifo_test(void)
568 char *tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
569 char *fifo
= g_build_filename(tmp_path
, "fifo", NULL
);
570 char *out
= g_build_filename(tmp_path
, "out", NULL
);
571 ChardevFile file
= { .in
= fifo
,
574 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
575 .u
.file
.data
= &file
};
576 FeHandler fe
= { 0, };
579 if (mkfifo(fifo
, 0600) < 0) {
583 fd
= open(fifo
, O_RDWR
);
584 ret
= write(fd
, "fifo-in", 8);
585 g_assert_cmpint(ret
, ==, 8);
587 chr
= qemu_chardev_new("label-file", TYPE_CHARDEV_FILE
, &backend
,
590 qemu_chr_fe_init(&be
, chr
, &error_abort
);
591 qemu_chr_fe_set_handlers(&be
,
598 g_assert_cmpint(fe
.last_event
, !=, CHR_EVENT_BREAK
);
599 qmp_chardev_send_break("label-foo", NULL
);
600 g_assert_cmpint(fe
.last_event
, !=, CHR_EVENT_BREAK
);
601 qmp_chardev_send_break("label-file", NULL
);
602 g_assert_cmpint(fe
.last_event
, ==, CHR_EVENT_BREAK
);
608 g_assert_cmpint(fe
.read_count
, ==, 8);
609 g_assert_cmpstr(fe
.read_buf
, ==, "fifo-in");
611 qemu_chr_fe_deinit(&be
, true);
622 static void char_file_test_internal(Chardev
*ext_chr
, const char *filepath
)
624 char *tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
627 char *contents
= NULL
;
628 ChardevFile file
= {};
629 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
630 .u
.file
.data
= &file
};
636 out
= g_strdup(filepath
);
639 out
= g_build_filename(tmp_path
, "out", NULL
);
641 chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_FILE
, &backend
,
644 ret
= qemu_chr_write_all(chr
, (uint8_t *)"hello!", 6);
645 g_assert_cmpint(ret
, ==, 6);
647 ret
= g_file_get_contents(out
, &contents
, &length
, NULL
);
648 g_assert(ret
== TRUE
);
649 g_assert_cmpint(length
, ==, 6);
650 g_assert(strncmp(contents
, "hello!", 6) == 0);
653 object_unref(OBJECT(chr
));
662 static void char_file_test(void)
664 char_file_test_internal(NULL
, NULL
);
667 static void char_null_test(void)
674 chr
= qemu_chr_find("label-null");
677 chr
= qemu_chr_new("label-null", "null");
678 chr
= qemu_chr_find("label-null");
679 g_assert_nonnull(chr
);
681 g_assert(qemu_chr_has_feature(chr
,
682 QEMU_CHAR_FEATURE_FD_PASS
) == false);
683 g_assert(qemu_chr_has_feature(chr
,
684 QEMU_CHAR_FEATURE_RECONNECTABLE
) == false);
686 /* check max avail */
687 qemu_chr_fe_init(&be
, chr
, &error_abort
);
688 qemu_chr_fe_init(&be
, chr
, &err
);
689 error_free_or_abort(&err
);
691 /* deinit & reinit */
692 qemu_chr_fe_deinit(&be
, false);
693 qemu_chr_fe_init(&be
, chr
, &error_abort
);
695 qemu_chr_fe_set_open(&be
, true);
697 qemu_chr_fe_set_handlers(&be
,
704 ret
= qemu_chr_fe_write(&be
, (void *)"buf", 4);
705 g_assert_cmpint(ret
, ==, 4);
707 qemu_chr_fe_deinit(&be
, true);
710 static void char_invalid_test(void)
714 chr
= qemu_chr_new("label-invalid", "invalid");
718 static int chardev_change(void *opaque
)
723 static int chardev_change_denied(void *opaque
)
728 static void char_hotswap_test(void)
734 gchar
*tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
735 char *filename
= g_build_filename(tmp_path
, "file", NULL
);
736 ChardevFile file
= { .out
= filename
};
737 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
738 .u
.file
.data
= &file
};
742 int sock
= make_udp_socket(&port
);
743 g_assert_cmpint(sock
, >, 0);
745 chr_args
= g_strdup_printf("udp:127.0.0.1:%d", port
);
747 chr
= qemu_chr_new("chardev", chr_args
);
748 qemu_chr_fe_init(&be
, chr
, &error_abort
);
750 /* check that chardev operates correctly */
751 char_udp_test_internal(chr
, sock
);
753 /* set the handler that denies the hotswap */
754 qemu_chr_fe_set_handlers(&be
, NULL
, NULL
,
755 NULL
, chardev_change_denied
, NULL
, NULL
, true);
757 /* now, change is denied and has to keep the old backend operating */
758 ret
= qmp_chardev_change("chardev", &backend
, NULL
);
760 g_assert(be
.chr
== chr
);
762 char_udp_test_internal(chr
, sock
);
764 /* now allow the change */
765 qemu_chr_fe_set_handlers(&be
, NULL
, NULL
,
766 NULL
, chardev_change
, NULL
, NULL
, true);
768 /* has to succeed now */
769 ret
= qmp_chardev_change("chardev", &backend
, &error_abort
);
770 g_assert(be
.chr
!= chr
);
775 /* run the file chardev test */
776 char_file_test_internal(chr
, filename
);
778 object_unparent(OBJECT(chr
));
780 qapi_free_ChardevReturn(ret
);
788 int main(int argc
, char **argv
)
790 qemu_init_main_loop(&error_abort
);
793 g_test_init(&argc
, &argv
, NULL
);
795 module_call_init(MODULE_INIT_QOM
);
796 qemu_add_opts(&qemu_chardev_opts
);
798 g_test_add_func("/char/null", char_null_test
);
799 g_test_add_func("/char/invalid", char_invalid_test
);
800 g_test_add_func("/char/ringbuf", char_ringbuf_test
);
801 g_test_add_func("/char/mux", char_mux_test
);
802 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
804 g_test_add_func("/char/console/subprocess", char_console_test_subprocess
);
805 g_test_add_func("/char/console", char_console_test
);
807 g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess
);
808 g_test_add_func("/char/stdio", char_stdio_test
);
811 g_test_add_func("/char/pipe", char_pipe_test
);
813 g_test_add_func("/char/file", char_file_test
);
815 g_test_add_func("/char/file-fifo", char_file_fifo_test
);
817 g_test_add_func("/char/socket/basic", char_socket_basic_test
);
818 g_test_add_func("/char/socket/fdpass", char_socket_fdpass_test
);
819 g_test_add_func("/char/udp", char_udp_test
);
820 #ifdef HAVE_CHARDEV_SERIAL
821 g_test_add_func("/char/serial", char_serial_test
);
823 g_test_add_func("/char/hotswap", char_hotswap_test
);