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
,
184 qemu_chr_fe_init(&chr_be2
, chr
, &error_abort
);
185 qemu_chr_fe_set_handlers(&chr_be2
,
191 qemu_chr_fe_take_focus(&chr_be2
);
193 base
= qemu_chr_find("mux-label-base");
194 g_assert_cmpint(qemu_chr_be_can_write(base
), !=, 0);
196 qemu_chr_be_write(base
, (void *)"hello", 6);
197 g_assert_cmpint(h1
.read_count
, ==, 0);
198 g_assert_cmpint(h2
.read_count
, ==, 6);
199 g_assert_cmpstr(h2
.read_buf
, ==, "hello");
203 qemu_chr_be_write(base
, (void *)"\1c", 2);
205 qemu_chr_be_write(base
, (void *)"hello", 6);
206 g_assert_cmpint(h2
.read_count
, ==, 0);
207 g_assert_cmpint(h1
.read_count
, ==, 6);
208 g_assert_cmpstr(h1
.read_buf
, ==, "hello");
211 /* remove first handler */
212 qemu_chr_fe_set_handlers(&chr_be1
, NULL
, NULL
, NULL
, NULL
, NULL
, true);
213 qemu_chr_be_write(base
, (void *)"hello", 6);
214 g_assert_cmpint(h1
.read_count
, ==, 0);
215 g_assert_cmpint(h2
.read_count
, ==, 0);
217 qemu_chr_be_write(base
, (void *)"\1c", 2);
218 qemu_chr_be_write(base
, (void *)"hello", 6);
219 g_assert_cmpint(h1
.read_count
, ==, 0);
220 g_assert_cmpint(h2
.read_count
, ==, 6);
221 g_assert_cmpstr(h2
.read_buf
, ==, "hello");
225 qemu_chr_be_write(base
, (void *)"\1?", 2);
226 data
= qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort
);
227 g_assert_cmpint(strlen(data
), !=, 0);
230 qemu_chr_fe_deinit(&chr_be1
, false);
231 qemu_chr_fe_deinit(&chr_be2
, true);
234 typedef struct SocketIdleData
{
239 CharBackend
*client_be
;
242 static gboolean
char_socket_test_idle(gpointer user_data
)
244 SocketIdleData
*data
= user_data
;
246 if (object_property_get_bool(OBJECT(data
->chr
), "connected", NULL
)
247 == data
->conn_expected
) {
255 static void socket_read(void *opaque
, const uint8_t *buf
, int size
)
257 SocketIdleData
*data
= opaque
;
259 g_assert_cmpint(size
, ==, 1);
260 g_assert_cmpint(*buf
, ==, 'Z');
262 size
= qemu_chr_fe_write(data
->be
, (const uint8_t *)"hello", 5);
263 g_assert_cmpint(size
, ==, 5);
266 static int socket_can_read(void *opaque
)
271 static void socket_read_hello(void *opaque
, const uint8_t *buf
, int size
)
273 g_assert_cmpint(size
, ==, 5);
274 g_assert(strncmp((char *)buf
, "hello", 5) == 0);
279 static int socket_can_read_hello(void *opaque
)
284 static void char_socket_test(void)
286 Chardev
*chr
= qemu_chr_new("server", "tcp:127.0.0.1:0,server,nowait");
291 SocketIdleData d
= { .chr
= chr
};
293 CharBackend client_be
;
299 g_assert_nonnull(chr
);
300 g_assert(!object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
302 addr
= object_property_get_qobject(OBJECT(chr
), "addr", &error_abort
);
303 qdict
= qobject_to_qdict(addr
);
304 port
= qdict_get_str(qdict
, "port");
305 tmp
= g_strdup_printf("tcp:127.0.0.1:%s", port
);
308 qemu_chr_fe_init(&be
, chr
, &error_abort
);
309 qemu_chr_fe_set_handlers(&be
, socket_can_read
, socket_read
,
310 NULL
, &d
, NULL
, true);
312 chr_client
= qemu_chr_new("client", tmp
);
313 qemu_chr_fe_init(&client_be
, chr_client
, &error_abort
);
314 qemu_chr_fe_set_handlers(&client_be
, socket_can_read_hello
,
316 NULL
, &d
, NULL
, true);
319 d
.conn_expected
= true;
320 guint id
= g_idle_add(char_socket_test_idle
, &d
);
321 g_source_set_name_by_id(id
, "test-idle");
322 g_assert_cmpint(id
, >, 0);
325 g_assert(object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
326 g_assert(object_property_get_bool(OBJECT(chr_client
),
327 "connected", &error_abort
));
329 qemu_chr_write_all(chr_client
, (const uint8_t *)"Z", 1);
332 object_unparent(OBJECT(chr_client
));
334 d
.conn_expected
= false;
335 g_idle_add(char_socket_test_idle
, &d
);
338 object_unparent(OBJECT(chr
));
342 static void char_pipe_test(void)
344 gchar
*tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
345 gchar
*tmp
, *in
, *out
, *pipe
= g_build_filename(tmp_path
, "pipe", NULL
);
350 FeHandler fe
= { 0, };
352 in
= g_strdup_printf("%s.in", pipe
);
353 if (mkfifo(in
, 0600) < 0) {
356 out
= g_strdup_printf("%s.out", pipe
);
357 if (mkfifo(out
, 0600) < 0) {
361 tmp
= g_strdup_printf("pipe:%s", pipe
);
362 chr
= qemu_chr_new("pipe", tmp
);
363 g_assert_nonnull(chr
);
366 qemu_chr_fe_init(&be
, chr
, &error_abort
);
368 ret
= qemu_chr_fe_write(&be
, (void *)"pipe-out", 9);
369 g_assert_cmpint(ret
, ==, 9);
371 fd
= open(out
, O_RDWR
);
372 ret
= read(fd
, buf
, sizeof(buf
));
373 g_assert_cmpint(ret
, ==, 9);
374 g_assert_cmpstr(buf
, ==, "pipe-out");
377 fd
= open(in
, O_WRONLY
);
378 ret
= write(fd
, "pipe-in", 8);
379 g_assert_cmpint(ret
, ==, 8);
382 qemu_chr_fe_set_handlers(&be
,
391 g_assert_cmpint(fe
.read_count
, ==, 8);
392 g_assert_cmpstr(fe
.read_buf
, ==, "pipe-in");
394 qemu_chr_fe_deinit(&be
, true);
396 g_assert(g_unlink(in
) == 0);
397 g_assert(g_unlink(out
) == 0);
398 g_assert(g_rmdir(tmp_path
) == 0);
406 static void char_udp_test(void)
408 struct sockaddr_in addr
= { 0, }, other
;
409 SocketIdleData d
= { 0, };
412 socklen_t alen
= sizeof(addr
);
413 int ret
, sock
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
417 g_assert_cmpint(sock
, >, 0);
418 addr
.sin_family
= AF_INET
;
419 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
421 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
422 g_assert_cmpint(ret
, ==, 0);
423 ret
= getsockname(sock
, (struct sockaddr
*)&addr
, &alen
);
424 g_assert_cmpint(ret
, ==, 0);
426 tmp
= g_strdup_printf("udp:127.0.0.1:%d",
427 ntohs(addr
.sin_port
));
428 chr
= qemu_chr_new("client", tmp
);
429 g_assert_nonnull(chr
);
432 qemu_chr_fe_init(&be
, chr
, &error_abort
);
433 qemu_chr_fe_set_handlers(&be
, socket_can_read_hello
, socket_read_hello
,
434 NULL
, &d
, NULL
, true);
435 ret
= qemu_chr_write_all(chr
, (uint8_t *)"hello", 5);
436 g_assert_cmpint(ret
, ==, 5);
439 ret
= recvfrom(sock
, buf
, sizeof(buf
), 0,
440 (struct sockaddr
*)&other
, &alen
);
441 g_assert_cmpint(ret
, ==, 5);
442 ret
= sendto(sock
, buf
, 5, 0, (struct sockaddr
*)&other
, alen
);
443 g_assert_cmpint(ret
, ==, 5);
451 #ifdef HAVE_CHARDEV_SERIAL
452 static void char_serial_test(void)
457 opts
= qemu_opts_create(qemu_find_opts("chardev"), "serial-id",
459 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
460 qemu_opt_set(opts
, "path", "/dev/null", &error_abort
);
462 chr
= qemu_chr_new_from_opts(opts
, NULL
);
463 g_assert_nonnull(chr
);
464 /* TODO: add more tests with a pty */
465 object_unparent(OBJECT(chr
));
468 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
469 chr
= qemu_chr_new_from_opts(opts
, NULL
);
470 g_assert_nonnull(chr
);
471 object_unparent(OBJECT(chr
));
477 static void char_file_test(void)
479 char *tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
480 char *out
= g_build_filename(tmp_path
, "out", NULL
);
481 char *contents
= NULL
;
482 ChardevFile file
= { .out
= out
};
483 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
484 .u
.file
.data
= &file
};
489 chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_FILE
, &backend
,
491 ret
= qemu_chr_write_all(chr
, (uint8_t *)"hello!", 6);
492 g_assert_cmpint(ret
, ==, 6);
493 object_unref(OBJECT(chr
));
495 ret
= g_file_get_contents(out
, &contents
, &length
, NULL
);
496 g_assert(ret
== TRUE
);
497 g_assert_cmpint(length
, ==, 6);
498 g_assert(strncmp(contents
, "hello!", 6) == 0);
504 FeHandler fe
= { 0, };
505 char *fifo
= g_build_filename(tmp_path
, "fifo", NULL
);
508 if (mkfifo(fifo
, 0600) < 0) {
512 fd
= open(fifo
, O_RDWR
);
513 ret
= write(fd
, "fifo-in", 8);
514 g_assert_cmpint(ret
, ==, 8);
518 chr
= qemu_chardev_new("label-file", TYPE_CHARDEV_FILE
, &backend
,
521 qemu_chr_fe_init(&be
, chr
, &error_abort
);
522 qemu_chr_fe_set_handlers(&be
,
528 g_assert_cmpint(fe
.last_event
, !=, CHR_EVENT_BREAK
);
529 qmp_chardev_send_break("label-foo", NULL
);
530 g_assert_cmpint(fe
.last_event
, !=, CHR_EVENT_BREAK
);
531 qmp_chardev_send_break("label-file", NULL
);
532 g_assert_cmpint(fe
.last_event
, ==, CHR_EVENT_BREAK
);
538 g_assert_cmpint(fe
.read_count
, ==, 8);
539 g_assert_cmpstr(fe
.read_buf
, ==, "fifo-in");
540 qemu_chr_fe_deinit(&be
, true);
552 static void char_null_test(void)
559 chr
= qemu_chr_find("label-null");
562 chr
= qemu_chr_new("label-null", "null");
563 chr
= qemu_chr_find("label-null");
564 g_assert_nonnull(chr
);
566 g_assert(qemu_chr_has_feature(chr
,
567 QEMU_CHAR_FEATURE_FD_PASS
) == false);
568 g_assert(qemu_chr_has_feature(chr
,
569 QEMU_CHAR_FEATURE_RECONNECTABLE
) == false);
571 /* check max avail */
572 qemu_chr_fe_init(&be
, chr
, &error_abort
);
573 qemu_chr_fe_init(&be
, chr
, &err
);
574 error_free_or_abort(&err
);
576 /* deinit & reinit */
577 qemu_chr_fe_deinit(&be
, false);
578 qemu_chr_fe_init(&be
, chr
, &error_abort
);
580 qemu_chr_fe_set_open(&be
, true);
582 qemu_chr_fe_set_handlers(&be
,
588 ret
= qemu_chr_fe_write(&be
, (void *)"buf", 4);
589 g_assert_cmpint(ret
, ==, 4);
591 qemu_chr_fe_deinit(&be
, true);
594 static void char_invalid_test(void)
598 chr
= qemu_chr_new("label-invalid", "invalid");
602 int main(int argc
, char **argv
)
604 qemu_init_main_loop(&error_abort
);
607 g_test_init(&argc
, &argv
, NULL
);
609 module_call_init(MODULE_INIT_QOM
);
610 qemu_add_opts(&qemu_chardev_opts
);
612 g_test_add_func("/char/null", char_null_test
);
613 g_test_add_func("/char/invalid", char_invalid_test
);
614 g_test_add_func("/char/ringbuf", char_ringbuf_test
);
615 g_test_add_func("/char/mux", char_mux_test
);
616 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
618 g_test_add_func("/char/console/subprocess", char_console_test_subprocess
);
619 g_test_add_func("/char/console", char_console_test
);
621 g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess
);
622 g_test_add_func("/char/stdio", char_stdio_test
);
625 g_test_add_func("/char/pipe", char_pipe_test
);
627 g_test_add_func("/char/file", char_file_test
);
628 g_test_add_func("/char/socket", char_socket_test
);
629 g_test_add_func("/char/udp", char_udp_test
);
630 #ifdef HAVE_CHARDEV_SERIAL
631 g_test_add_func("/char/serial", char_serial_test
);