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"
15 typedef struct FeHandler
{
21 static void main_loop(void)
25 main_loop_wait(false);
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
;
47 static void fe_event(void *opaque
, int event
)
49 FeHandler
*h
= opaque
;
51 h
->last_event
= event
;
52 if (event
!= CHR_EVENT_BREAK
) {
57 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
59 static void char_console_test_subprocess(void)
64 opts
= qemu_opts_create(qemu_find_opts("chardev"), "console-label",
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);
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");
84 static void char_stdio_test_subprocess(void)
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");
109 static void char_ringbuf_test(void)
117 opts
= qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
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
);
126 opts
= qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
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
);
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");
142 data
= qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort
);
143 g_assert_cmpstr(data
, ==, "");
146 qemu_chr_fe_deinit(&be
, true);
149 opts
= qemu_opts_create(qemu_find_opts("chardev"), "memory-label",
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
));
159 static void char_mux_test(void)
164 FeHandler h1
= { 0, }, h2
= { 0, };
165 CharBackend chr_be1
, chr_be2
;
167 opts
= qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
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
);
176 qemu_chr_fe_init(&chr_be1
, chr
, &error_abort
);
177 qemu_chr_fe_set_handlers(&chr_be1
,
185 qemu_chr_fe_init(&chr_be2
, chr
, &error_abort
);
186 qemu_chr_fe_set_handlers(&chr_be2
,
193 qemu_chr_fe_take_focus(&chr_be2
);
195 base
= qemu_chr_find("mux-label-base");
196 g_assert_cmpint(qemu_chr_be_can_write(base
), !=, 0);
198 qemu_chr_be_write(base
, (void *)"hello", 6);
199 g_assert_cmpint(h1
.read_count
, ==, 0);
200 g_assert_cmpint(h2
.read_count
, ==, 6);
201 g_assert_cmpstr(h2
.read_buf
, ==, "hello");
205 qemu_chr_be_write(base
, (void *)"\1c", 2);
207 qemu_chr_be_write(base
, (void *)"hello", 6);
208 g_assert_cmpint(h2
.read_count
, ==, 0);
209 g_assert_cmpint(h1
.read_count
, ==, 6);
210 g_assert_cmpstr(h1
.read_buf
, ==, "hello");
213 /* remove first handler */
214 qemu_chr_fe_set_handlers(&chr_be1
, NULL
, NULL
, NULL
, NULL
,
216 qemu_chr_be_write(base
, (void *)"hello", 6);
217 g_assert_cmpint(h1
.read_count
, ==, 0);
218 g_assert_cmpint(h2
.read_count
, ==, 0);
220 qemu_chr_be_write(base
, (void *)"\1c", 2);
221 qemu_chr_be_write(base
, (void *)"hello", 6);
222 g_assert_cmpint(h1
.read_count
, ==, 0);
223 g_assert_cmpint(h2
.read_count
, ==, 6);
224 g_assert_cmpstr(h2
.read_buf
, ==, "hello");
228 qemu_chr_be_write(base
, (void *)"\1?", 2);
229 data
= qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort
);
230 g_assert_cmpint(strlen(data
), !=, 0);
233 qemu_chr_fe_deinit(&chr_be1
, false);
234 qemu_chr_fe_deinit(&chr_be2
, true);
237 typedef struct SocketIdleData
{
242 CharBackend
*client_be
;
245 static gboolean
char_socket_test_idle(gpointer user_data
)
247 SocketIdleData
*data
= user_data
;
249 if (object_property_get_bool(OBJECT(data
->chr
), "connected", NULL
)
250 == data
->conn_expected
) {
258 static void socket_read(void *opaque
, const uint8_t *buf
, int size
)
260 SocketIdleData
*data
= opaque
;
262 g_assert_cmpint(size
, ==, 1);
263 g_assert_cmpint(*buf
, ==, 'Z');
265 size
= qemu_chr_fe_write(data
->be
, (const uint8_t *)"hello", 5);
266 g_assert_cmpint(size
, ==, 5);
269 static int socket_can_read(void *opaque
)
274 static void socket_read_hello(void *opaque
, const uint8_t *buf
, int size
)
276 g_assert_cmpint(size
, ==, 5);
277 g_assert(strncmp((char *)buf
, "hello", 5) == 0);
282 static int socket_can_read_hello(void *opaque
)
287 static void char_socket_test(void)
289 Chardev
*chr
= qemu_chr_new("server", "tcp:127.0.0.1:0,server,nowait");
294 SocketIdleData d
= { .chr
= chr
};
296 CharBackend client_be
;
302 g_assert_nonnull(chr
);
303 g_assert(!object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
305 addr
= object_property_get_qobject(OBJECT(chr
), "addr", &error_abort
);
306 qdict
= qobject_to_qdict(addr
);
307 port
= qdict_get_str(qdict
, "port");
308 tmp
= g_strdup_printf("tcp:127.0.0.1:%s", port
);
311 qemu_chr_fe_init(&be
, chr
, &error_abort
);
312 qemu_chr_fe_set_handlers(&be
, socket_can_read
, socket_read
,
313 NULL
, NULL
, &d
, NULL
, true);
315 chr_client
= qemu_chr_new("client", tmp
);
316 qemu_chr_fe_init(&client_be
, chr_client
, &error_abort
);
317 qemu_chr_fe_set_handlers(&client_be
, socket_can_read_hello
,
319 NULL
, NULL
, &d
, NULL
, true);
322 d
.conn_expected
= true;
323 guint id
= g_idle_add(char_socket_test_idle
, &d
);
324 g_source_set_name_by_id(id
, "test-idle");
325 g_assert_cmpint(id
, >, 0);
328 g_assert(object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
329 g_assert(object_property_get_bool(OBJECT(chr_client
),
330 "connected", &error_abort
));
332 qemu_chr_write_all(chr_client
, (const uint8_t *)"Z", 1);
335 object_unparent(OBJECT(chr_client
));
337 d
.conn_expected
= false;
338 g_idle_add(char_socket_test_idle
, &d
);
341 object_unparent(OBJECT(chr
));
345 static void char_pipe_test(void)
347 gchar
*tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
348 gchar
*tmp
, *in
, *out
, *pipe
= g_build_filename(tmp_path
, "pipe", NULL
);
353 FeHandler fe
= { 0, };
355 in
= g_strdup_printf("%s.in", pipe
);
356 if (mkfifo(in
, 0600) < 0) {
359 out
= g_strdup_printf("%s.out", pipe
);
360 if (mkfifo(out
, 0600) < 0) {
364 tmp
= g_strdup_printf("pipe:%s", pipe
);
365 chr
= qemu_chr_new("pipe", tmp
);
366 g_assert_nonnull(chr
);
369 qemu_chr_fe_init(&be
, chr
, &error_abort
);
371 ret
= qemu_chr_fe_write(&be
, (void *)"pipe-out", 9);
372 g_assert_cmpint(ret
, ==, 9);
374 fd
= open(out
, O_RDWR
);
375 ret
= read(fd
, buf
, sizeof(buf
));
376 g_assert_cmpint(ret
, ==, 9);
377 g_assert_cmpstr(buf
, ==, "pipe-out");
380 fd
= open(in
, O_WRONLY
);
381 ret
= write(fd
, "pipe-in", 8);
382 g_assert_cmpint(ret
, ==, 8);
385 qemu_chr_fe_set_handlers(&be
,
395 g_assert_cmpint(fe
.read_count
, ==, 8);
396 g_assert_cmpstr(fe
.read_buf
, ==, "pipe-in");
398 qemu_chr_fe_deinit(&be
, true);
400 g_assert(g_unlink(in
) == 0);
401 g_assert(g_unlink(out
) == 0);
402 g_assert(g_rmdir(tmp_path
) == 0);
410 static int make_udp_socket(int *port
)
412 struct sockaddr_in addr
= { 0, };
413 socklen_t alen
= sizeof(addr
);
414 int ret
, sock
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
416 g_assert_cmpint(sock
, >, 0);
417 addr
.sin_family
= AF_INET
;
418 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
420 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
421 g_assert_cmpint(ret
, ==, 0);
422 ret
= getsockname(sock
, (struct sockaddr
*)&addr
, &alen
);
423 g_assert_cmpint(ret
, ==, 0);
425 *port
= ntohs(addr
.sin_port
);
429 static void char_udp_test_internal(Chardev
*reuse_chr
, int sock
)
431 struct sockaddr_in other
;
432 SocketIdleData d
= { 0, };
435 socklen_t alen
= sizeof(other
);
445 sock
= make_udp_socket(&port
);
446 tmp
= g_strdup_printf("udp:127.0.0.1:%d", port
);
447 chr
= qemu_chr_new("client", tmp
);
448 g_assert_nonnull(chr
);
450 be
= g_alloca(sizeof(CharBackend
));
451 qemu_chr_fe_init(be
, chr
, &error_abort
);
455 qemu_chr_fe_set_handlers(be
, socket_can_read_hello
, socket_read_hello
,
456 NULL
, NULL
, &d
, NULL
, true);
457 ret
= qemu_chr_write_all(chr
, (uint8_t *)"hello", 5);
458 g_assert_cmpint(ret
, ==, 5);
460 ret
= recvfrom(sock
, buf
, sizeof(buf
), 0,
461 (struct sockaddr
*)&other
, &alen
);
462 g_assert_cmpint(ret
, ==, 5);
463 ret
= sendto(sock
, buf
, 5, 0, (struct sockaddr
*)&other
, alen
);
464 g_assert_cmpint(ret
, ==, 5);
470 qemu_chr_fe_deinit(be
, true);
475 static void char_udp_test(void)
477 char_udp_test_internal(NULL
, 0);
480 #ifdef HAVE_CHARDEV_SERIAL
481 static void char_serial_test(void)
486 opts
= qemu_opts_create(qemu_find_opts("chardev"), "serial-id",
488 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
489 qemu_opt_set(opts
, "path", "/dev/null", &error_abort
);
491 chr
= qemu_chr_new_from_opts(opts
, NULL
);
492 g_assert_nonnull(chr
);
493 /* TODO: add more tests with a pty */
494 object_unparent(OBJECT(chr
));
497 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
498 chr
= qemu_chr_new_from_opts(opts
, NULL
);
499 g_assert_nonnull(chr
);
500 object_unparent(OBJECT(chr
));
507 static void char_file_fifo_test(void)
511 char *tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
512 char *fifo
= g_build_filename(tmp_path
, "fifo", NULL
);
513 char *out
= g_build_filename(tmp_path
, "out", NULL
);
514 ChardevFile file
= { .in
= fifo
,
517 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
518 .u
.file
.data
= &file
};
519 FeHandler fe
= { 0, };
522 if (mkfifo(fifo
, 0600) < 0) {
526 fd
= open(fifo
, O_RDWR
);
527 ret
= write(fd
, "fifo-in", 8);
528 g_assert_cmpint(ret
, ==, 8);
530 chr
= qemu_chardev_new("label-file", TYPE_CHARDEV_FILE
, &backend
,
533 qemu_chr_fe_init(&be
, chr
, &error_abort
);
534 qemu_chr_fe_set_handlers(&be
,
541 g_assert_cmpint(fe
.last_event
, !=, CHR_EVENT_BREAK
);
542 qmp_chardev_send_break("label-foo", NULL
);
543 g_assert_cmpint(fe
.last_event
, !=, CHR_EVENT_BREAK
);
544 qmp_chardev_send_break("label-file", NULL
);
545 g_assert_cmpint(fe
.last_event
, ==, CHR_EVENT_BREAK
);
551 g_assert_cmpint(fe
.read_count
, ==, 8);
552 g_assert_cmpstr(fe
.read_buf
, ==, "fifo-in");
554 qemu_chr_fe_deinit(&be
, true);
565 static void char_file_test_internal(Chardev
*ext_chr
, const char *filepath
)
567 char *tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
570 char *contents
= NULL
;
571 ChardevFile file
= {};
572 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
573 .u
.file
.data
= &file
};
579 out
= g_strdup(filepath
);
582 out
= g_build_filename(tmp_path
, "out", NULL
);
584 chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_FILE
, &backend
,
587 ret
= qemu_chr_write_all(chr
, (uint8_t *)"hello!", 6);
588 g_assert_cmpint(ret
, ==, 6);
590 ret
= g_file_get_contents(out
, &contents
, &length
, NULL
);
591 g_assert(ret
== TRUE
);
592 g_assert_cmpint(length
, ==, 6);
593 g_assert(strncmp(contents
, "hello!", 6) == 0);
596 object_unref(OBJECT(chr
));
605 static void char_file_test(void)
607 char_file_test_internal(NULL
, NULL
);
610 static void char_null_test(void)
617 chr
= qemu_chr_find("label-null");
620 chr
= qemu_chr_new("label-null", "null");
621 chr
= qemu_chr_find("label-null");
622 g_assert_nonnull(chr
);
624 g_assert(qemu_chr_has_feature(chr
,
625 QEMU_CHAR_FEATURE_FD_PASS
) == false);
626 g_assert(qemu_chr_has_feature(chr
,
627 QEMU_CHAR_FEATURE_RECONNECTABLE
) == false);
629 /* check max avail */
630 qemu_chr_fe_init(&be
, chr
, &error_abort
);
631 qemu_chr_fe_init(&be
, chr
, &err
);
632 error_free_or_abort(&err
);
634 /* deinit & reinit */
635 qemu_chr_fe_deinit(&be
, false);
636 qemu_chr_fe_init(&be
, chr
, &error_abort
);
638 qemu_chr_fe_set_open(&be
, true);
640 qemu_chr_fe_set_handlers(&be
,
647 ret
= qemu_chr_fe_write(&be
, (void *)"buf", 4);
648 g_assert_cmpint(ret
, ==, 4);
650 qemu_chr_fe_deinit(&be
, true);
653 static void char_invalid_test(void)
657 chr
= qemu_chr_new("label-invalid", "invalid");
661 static int chardev_change(void *opaque
)
666 static int chardev_change_denied(void *opaque
)
671 static void char_hotswap_test(void)
677 gchar
*tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
678 char *filename
= g_build_filename(tmp_path
, "file", NULL
);
679 ChardevFile file
= { .out
= filename
};
680 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
681 .u
.file
.data
= &file
};
685 int sock
= make_udp_socket(&port
);
686 g_assert_cmpint(sock
, >, 0);
688 chr_args
= g_strdup_printf("udp:127.0.0.1:%d", port
);
690 chr
= qemu_chr_new("chardev", chr_args
);
691 qemu_chr_fe_init(&be
, chr
, &error_abort
);
693 /* check that chardev operates correctly */
694 char_udp_test_internal(chr
, sock
);
696 /* set the handler that denies the hotswap */
697 qemu_chr_fe_set_handlers(&be
, NULL
, NULL
,
698 NULL
, chardev_change_denied
, NULL
, NULL
, true);
700 /* now, change is denied and has to keep the old backend operating */
701 ret
= qmp_chardev_change("chardev", &backend
, NULL
);
703 g_assert(be
.chr
== chr
);
705 char_udp_test_internal(chr
, sock
);
707 /* now allow the change */
708 qemu_chr_fe_set_handlers(&be
, NULL
, NULL
,
709 NULL
, chardev_change
, NULL
, NULL
, true);
711 /* has to succeed now */
712 ret
= qmp_chardev_change("chardev", &backend
, &error_abort
);
713 g_assert(be
.chr
!= chr
);
718 /* run the file chardev test */
719 char_file_test_internal(chr
, filename
);
721 object_unparent(OBJECT(chr
));
723 qapi_free_ChardevReturn(ret
);
731 int main(int argc
, char **argv
)
733 qemu_init_main_loop(&error_abort
);
736 g_test_init(&argc
, &argv
, NULL
);
738 module_call_init(MODULE_INIT_QOM
);
739 qemu_add_opts(&qemu_chardev_opts
);
741 g_test_add_func("/char/null", char_null_test
);
742 g_test_add_func("/char/invalid", char_invalid_test
);
743 g_test_add_func("/char/ringbuf", char_ringbuf_test
);
744 g_test_add_func("/char/mux", char_mux_test
);
745 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
747 g_test_add_func("/char/console/subprocess", char_console_test_subprocess
);
748 g_test_add_func("/char/console", char_console_test
);
750 g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess
);
751 g_test_add_func("/char/stdio", char_stdio_test
);
754 g_test_add_func("/char/pipe", char_pipe_test
);
756 g_test_add_func("/char/file", char_file_test
);
758 g_test_add_func("/char/file-fifo", char_file_fifo_test
);
760 g_test_add_func("/char/socket", char_socket_test
);
761 g_test_add_func("/char/udp", char_udp_test
);
762 #ifdef HAVE_CHARDEV_SERIAL
763 g_test_add_func("/char/serial", char_serial_test
);
765 g_test_add_func("/char/hotswap", char_hotswap_test
);