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 "chardev/char-mux.h"
9 #include "sysemu/sysemu.h"
10 #include "qapi/error.h"
11 #include "qom/qom-qobject.h"
12 #include "qmp-commands.h"
16 typedef struct FeHandler
{
22 static void main_loop(void)
26 main_loop_wait(false);
30 static int fe_can_read(void *opaque
)
32 FeHandler
*h
= opaque
;
34 return sizeof(h
->read_buf
) - h
->read_count
;
37 static void fe_read(void *opaque
, const uint8_t *buf
, int size
)
39 FeHandler
*h
= opaque
;
41 g_assert_cmpint(size
, <=, fe_can_read(opaque
));
43 memcpy(h
->read_buf
+ h
->read_count
, buf
, size
);
44 h
->read_count
+= size
;
48 static void fe_event(void *opaque
, int event
)
50 FeHandler
*h
= opaque
;
52 h
->last_event
= event
;
53 if (event
!= CHR_EVENT_BREAK
) {
58 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
60 static void char_console_test_subprocess(void)
65 opts
= qemu_opts_create(qemu_find_opts("chardev"), "console-label",
67 qemu_opt_set(opts
, "backend", "console", &error_abort
);
69 chr
= qemu_chr_new_from_opts(opts
, NULL
);
70 g_assert_nonnull(chr
);
72 qemu_chr_write_all(chr
, (const uint8_t *)"CONSOLE", 7);
75 object_unparent(OBJECT(chr
));
78 static void char_console_test(void)
80 g_test_trap_subprocess("/char/console/subprocess", 0, 0);
81 g_test_trap_assert_passed();
82 g_test_trap_assert_stdout("CONSOLE");
85 static void char_stdio_test_subprocess(void)
91 chr
= qemu_chr_new("label", "stdio");
92 g_assert_nonnull(chr
);
94 qemu_chr_fe_init(&be
, chr
, &error_abort
);
95 qemu_chr_fe_set_open(&be
, true);
96 ret
= qemu_chr_fe_write(&be
, (void *)"buf", 4);
97 g_assert_cmpint(ret
, ==, 4);
99 qemu_chr_fe_deinit(&be
, true);
102 static void char_stdio_test(void)
104 g_test_trap_subprocess("/char/stdio/subprocess", 0, 0);
105 g_test_trap_assert_passed();
106 g_test_trap_assert_stdout("buf");
110 static void char_ringbuf_test(void)
118 opts
= qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
120 qemu_opt_set(opts
, "backend", "ringbuf", &error_abort
);
122 qemu_opt_set(opts
, "size", "5", &error_abort
);
123 chr
= qemu_chr_new_from_opts(opts
, NULL
);
127 opts
= qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
129 qemu_opt_set(opts
, "backend", "ringbuf", &error_abort
);
130 qemu_opt_set(opts
, "size", "2", &error_abort
);
131 chr
= qemu_chr_new_from_opts(opts
, &error_abort
);
132 g_assert_nonnull(chr
);
135 qemu_chr_fe_init(&be
, chr
, &error_abort
);
136 ret
= qemu_chr_fe_write(&be
, (void *)"buff", 4);
137 g_assert_cmpint(ret
, ==, 4);
139 data
= qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort
);
140 g_assert_cmpstr(data
, ==, "ff");
143 data
= qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort
);
144 g_assert_cmpstr(data
, ==, "");
147 qemu_chr_fe_deinit(&be
, true);
150 opts
= qemu_opts_create(qemu_find_opts("chardev"), "memory-label",
152 qemu_opt_set(opts
, "backend", "memory", &error_abort
);
153 qemu_opt_set(opts
, "size", "2", &error_abort
);
154 chr
= qemu_chr_new_from_opts(opts
, NULL
);
155 g_assert_nonnull(chr
);
156 object_unparent(OBJECT(chr
));
160 static void char_mux_test(void)
165 FeHandler h1
= { 0, }, h2
= { 0, };
166 CharBackend chr_be1
, chr_be2
;
168 muxes_realized
= true; /* done after machine init */
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(void)
306 Chardev
*chr
= qemu_chr_new("server", "tcp:127.0.0.1:0,server,nowait");
311 SocketIdleData d
= { .chr
= chr
};
313 CharBackend client_be
;
319 g_assert_nonnull(chr
);
320 g_assert(!object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
322 addr
= object_property_get_qobject(OBJECT(chr
), "addr", &error_abort
);
323 qdict
= qobject_to_qdict(addr
);
324 port
= qdict_get_str(qdict
, "port");
325 tmp
= g_strdup_printf("tcp:127.0.0.1:%s", port
);
328 qemu_chr_fe_init(&be
, chr
, &error_abort
);
329 qemu_chr_fe_set_handlers(&be
, socket_can_read
, socket_read
,
330 NULL
, NULL
, &d
, NULL
, true);
332 chr_client
= qemu_chr_new("client", tmp
);
333 qemu_chr_fe_init(&client_be
, chr_client
, &error_abort
);
334 qemu_chr_fe_set_handlers(&client_be
, socket_can_read_hello
,
336 NULL
, NULL
, &d
, NULL
, true);
339 d
.conn_expected
= true;
340 guint id
= g_idle_add(char_socket_test_idle
, &d
);
341 g_source_set_name_by_id(id
, "test-idle");
342 g_assert_cmpint(id
, >, 0);
345 g_assert(object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
346 g_assert(object_property_get_bool(OBJECT(chr_client
),
347 "connected", &error_abort
));
349 qemu_chr_write_all(chr_client
, (const uint8_t *)"Z", 1);
352 object_unparent(OBJECT(chr_client
));
354 d
.conn_expected
= false;
355 g_idle_add(char_socket_test_idle
, &d
);
358 object_unparent(OBJECT(chr
));
362 static void char_pipe_test(void)
364 gchar
*tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
365 gchar
*tmp
, *in
, *out
, *pipe
= g_build_filename(tmp_path
, "pipe", NULL
);
370 FeHandler fe
= { 0, };
372 in
= g_strdup_printf("%s.in", pipe
);
373 if (mkfifo(in
, 0600) < 0) {
376 out
= g_strdup_printf("%s.out", pipe
);
377 if (mkfifo(out
, 0600) < 0) {
381 tmp
= g_strdup_printf("pipe:%s", pipe
);
382 chr
= qemu_chr_new("pipe", tmp
);
383 g_assert_nonnull(chr
);
386 qemu_chr_fe_init(&be
, chr
, &error_abort
);
388 ret
= qemu_chr_fe_write(&be
, (void *)"pipe-out", 9);
389 g_assert_cmpint(ret
, ==, 9);
391 fd
= open(out
, O_RDWR
);
392 ret
= read(fd
, buf
, sizeof(buf
));
393 g_assert_cmpint(ret
, ==, 9);
394 g_assert_cmpstr(buf
, ==, "pipe-out");
397 fd
= open(in
, O_WRONLY
);
398 ret
= write(fd
, "pipe-in", 8);
399 g_assert_cmpint(ret
, ==, 8);
402 qemu_chr_fe_set_handlers(&be
,
412 g_assert_cmpint(fe
.read_count
, ==, 8);
413 g_assert_cmpstr(fe
.read_buf
, ==, "pipe-in");
415 qemu_chr_fe_deinit(&be
, true);
417 g_assert(g_unlink(in
) == 0);
418 g_assert(g_unlink(out
) == 0);
419 g_assert(g_rmdir(tmp_path
) == 0);
427 static int make_udp_socket(int *port
)
429 struct sockaddr_in addr
= { 0, };
430 socklen_t alen
= sizeof(addr
);
431 int ret
, sock
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
433 g_assert_cmpint(sock
, >, 0);
434 addr
.sin_family
= AF_INET
;
435 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
437 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
438 g_assert_cmpint(ret
, ==, 0);
439 ret
= getsockname(sock
, (struct sockaddr
*)&addr
, &alen
);
440 g_assert_cmpint(ret
, ==, 0);
442 *port
= ntohs(addr
.sin_port
);
446 static void char_udp_test_internal(Chardev
*reuse_chr
, int sock
)
448 struct sockaddr_in other
;
449 SocketIdleData d
= { 0, };
452 socklen_t alen
= sizeof(other
);
462 sock
= make_udp_socket(&port
);
463 tmp
= g_strdup_printf("udp:127.0.0.1:%d", port
);
464 chr
= qemu_chr_new("client", tmp
);
465 g_assert_nonnull(chr
);
467 be
= g_alloca(sizeof(CharBackend
));
468 qemu_chr_fe_init(be
, chr
, &error_abort
);
472 qemu_chr_fe_set_handlers(be
, socket_can_read_hello
, socket_read_hello
,
473 NULL
, NULL
, &d
, NULL
, true);
474 ret
= qemu_chr_write_all(chr
, (uint8_t *)"hello", 5);
475 g_assert_cmpint(ret
, ==, 5);
477 ret
= recvfrom(sock
, buf
, sizeof(buf
), 0,
478 (struct sockaddr
*)&other
, &alen
);
479 g_assert_cmpint(ret
, ==, 5);
480 ret
= sendto(sock
, buf
, 5, 0, (struct sockaddr
*)&other
, alen
);
481 g_assert_cmpint(ret
, ==, 5);
487 qemu_chr_fe_deinit(be
, true);
492 static void char_udp_test(void)
494 char_udp_test_internal(NULL
, 0);
497 #ifdef HAVE_CHARDEV_SERIAL
498 static void char_serial_test(void)
503 opts
= qemu_opts_create(qemu_find_opts("chardev"), "serial-id",
505 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
506 qemu_opt_set(opts
, "path", "/dev/null", &error_abort
);
508 chr
= qemu_chr_new_from_opts(opts
, NULL
);
509 g_assert_nonnull(chr
);
510 /* TODO: add more tests with a pty */
511 object_unparent(OBJECT(chr
));
514 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
515 chr
= qemu_chr_new_from_opts(opts
, NULL
);
516 g_assert_nonnull(chr
);
517 object_unparent(OBJECT(chr
));
524 static void char_file_fifo_test(void)
528 char *tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
529 char *fifo
= g_build_filename(tmp_path
, "fifo", NULL
);
530 char *out
= g_build_filename(tmp_path
, "out", NULL
);
531 ChardevFile file
= { .in
= fifo
,
534 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
535 .u
.file
.data
= &file
};
536 FeHandler fe
= { 0, };
539 if (mkfifo(fifo
, 0600) < 0) {
543 fd
= open(fifo
, O_RDWR
);
544 ret
= write(fd
, "fifo-in", 8);
545 g_assert_cmpint(ret
, ==, 8);
547 chr
= qemu_chardev_new("label-file", TYPE_CHARDEV_FILE
, &backend
,
550 qemu_chr_fe_init(&be
, chr
, &error_abort
);
551 qemu_chr_fe_set_handlers(&be
,
558 g_assert_cmpint(fe
.last_event
, !=, CHR_EVENT_BREAK
);
559 qmp_chardev_send_break("label-foo", NULL
);
560 g_assert_cmpint(fe
.last_event
, !=, CHR_EVENT_BREAK
);
561 qmp_chardev_send_break("label-file", NULL
);
562 g_assert_cmpint(fe
.last_event
, ==, CHR_EVENT_BREAK
);
568 g_assert_cmpint(fe
.read_count
, ==, 8);
569 g_assert_cmpstr(fe
.read_buf
, ==, "fifo-in");
571 qemu_chr_fe_deinit(&be
, true);
582 static void char_file_test_internal(Chardev
*ext_chr
, const char *filepath
)
584 char *tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
587 char *contents
= NULL
;
588 ChardevFile file
= {};
589 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
590 .u
.file
.data
= &file
};
596 out
= g_strdup(filepath
);
599 out
= g_build_filename(tmp_path
, "out", NULL
);
601 chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_FILE
, &backend
,
604 ret
= qemu_chr_write_all(chr
, (uint8_t *)"hello!", 6);
605 g_assert_cmpint(ret
, ==, 6);
607 ret
= g_file_get_contents(out
, &contents
, &length
, NULL
);
608 g_assert(ret
== TRUE
);
609 g_assert_cmpint(length
, ==, 6);
610 g_assert(strncmp(contents
, "hello!", 6) == 0);
613 object_unref(OBJECT(chr
));
622 static void char_file_test(void)
624 char_file_test_internal(NULL
, NULL
);
627 static void char_null_test(void)
634 chr
= qemu_chr_find("label-null");
637 chr
= qemu_chr_new("label-null", "null");
638 chr
= qemu_chr_find("label-null");
639 g_assert_nonnull(chr
);
641 g_assert(qemu_chr_has_feature(chr
,
642 QEMU_CHAR_FEATURE_FD_PASS
) == false);
643 g_assert(qemu_chr_has_feature(chr
,
644 QEMU_CHAR_FEATURE_RECONNECTABLE
) == false);
646 /* check max avail */
647 qemu_chr_fe_init(&be
, chr
, &error_abort
);
648 qemu_chr_fe_init(&be
, chr
, &err
);
649 error_free_or_abort(&err
);
651 /* deinit & reinit */
652 qemu_chr_fe_deinit(&be
, false);
653 qemu_chr_fe_init(&be
, chr
, &error_abort
);
655 qemu_chr_fe_set_open(&be
, true);
657 qemu_chr_fe_set_handlers(&be
,
664 ret
= qemu_chr_fe_write(&be
, (void *)"buf", 4);
665 g_assert_cmpint(ret
, ==, 4);
667 qemu_chr_fe_deinit(&be
, true);
670 static void char_invalid_test(void)
674 chr
= qemu_chr_new("label-invalid", "invalid");
678 static int chardev_change(void *opaque
)
683 static int chardev_change_denied(void *opaque
)
688 static void char_hotswap_test(void)
694 gchar
*tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
695 char *filename
= g_build_filename(tmp_path
, "file", NULL
);
696 ChardevFile file
= { .out
= filename
};
697 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
698 .u
.file
.data
= &file
};
702 int sock
= make_udp_socket(&port
);
703 g_assert_cmpint(sock
, >, 0);
705 chr_args
= g_strdup_printf("udp:127.0.0.1:%d", port
);
707 chr
= qemu_chr_new("chardev", chr_args
);
708 qemu_chr_fe_init(&be
, chr
, &error_abort
);
710 /* check that chardev operates correctly */
711 char_udp_test_internal(chr
, sock
);
713 /* set the handler that denies the hotswap */
714 qemu_chr_fe_set_handlers(&be
, NULL
, NULL
,
715 NULL
, chardev_change_denied
, NULL
, NULL
, true);
717 /* now, change is denied and has to keep the old backend operating */
718 ret
= qmp_chardev_change("chardev", &backend
, NULL
);
720 g_assert(be
.chr
== chr
);
722 char_udp_test_internal(chr
, sock
);
724 /* now allow the change */
725 qemu_chr_fe_set_handlers(&be
, NULL
, NULL
,
726 NULL
, chardev_change
, NULL
, NULL
, true);
728 /* has to succeed now */
729 ret
= qmp_chardev_change("chardev", &backend
, &error_abort
);
730 g_assert(be
.chr
!= chr
);
735 /* run the file chardev test */
736 char_file_test_internal(chr
, filename
);
738 object_unparent(OBJECT(chr
));
740 qapi_free_ChardevReturn(ret
);
748 int main(int argc
, char **argv
)
750 qemu_init_main_loop(&error_abort
);
753 g_test_init(&argc
, &argv
, NULL
);
755 module_call_init(MODULE_INIT_QOM
);
756 qemu_add_opts(&qemu_chardev_opts
);
758 g_test_add_func("/char/null", char_null_test
);
759 g_test_add_func("/char/invalid", char_invalid_test
);
760 g_test_add_func("/char/ringbuf", char_ringbuf_test
);
761 g_test_add_func("/char/mux", char_mux_test
);
762 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
764 g_test_add_func("/char/console/subprocess", char_console_test_subprocess
);
765 g_test_add_func("/char/console", char_console_test
);
767 g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess
);
768 g_test_add_func("/char/stdio", char_stdio_test
);
771 g_test_add_func("/char/pipe", char_pipe_test
);
773 g_test_add_func("/char/file", char_file_test
);
775 g_test_add_func("/char/file-fifo", char_file_fifo_test
);
777 g_test_add_func("/char/socket", char_socket_test
);
778 g_test_add_func("/char/udp", char_udp_test
);
779 #ifdef HAVE_CHARDEV_SERIAL
780 g_test_add_func("/char/serial", char_serial_test
);
782 g_test_add_func("/char/hotswap", char_hotswap_test
);