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 "sysemu/char.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)
28 nonblocking
= last_io
> 0;
29 last_io
= main_loop_wait(nonblocking
);
33 static int fe_can_read(void *opaque
)
35 FeHandler
*h
= opaque
;
37 return sizeof(h
->read_buf
) - h
->read_count
;
40 static void fe_read(void *opaque
, const uint8_t *buf
, int size
)
42 FeHandler
*h
= opaque
;
44 g_assert_cmpint(size
, <=, fe_can_read(opaque
));
46 memcpy(h
->read_buf
+ h
->read_count
, buf
, size
);
47 h
->read_count
+= size
;
51 static void fe_event(void *opaque
, int event
)
53 FeHandler
*h
= opaque
;
55 h
->last_event
= event
;
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
);
101 object_unparent(OBJECT(chr
));
104 static void char_stdio_test(void)
106 g_test_trap_subprocess("/char/stdio/subprocess", 0, 0);
107 g_test_trap_assert_passed();
108 g_test_trap_assert_stdout("buf");
112 static void char_ringbuf_test(void)
120 opts
= qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
122 qemu_opt_set(opts
, "backend", "ringbuf", &error_abort
);
124 qemu_opt_set(opts
, "size", "5", &error_abort
);
125 chr
= qemu_chr_new_from_opts(opts
, NULL
);
129 opts
= qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
131 qemu_opt_set(opts
, "backend", "ringbuf", &error_abort
);
132 qemu_opt_set(opts
, "size", "2", &error_abort
);
133 chr
= qemu_chr_new_from_opts(opts
, &error_abort
);
134 g_assert_nonnull(chr
);
137 qemu_chr_fe_init(&be
, chr
, &error_abort
);
138 ret
= qemu_chr_fe_write(&be
, (void *)"buff", 4);
139 g_assert_cmpint(ret
, ==, 4);
141 data
= qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort
);
142 g_assert_cmpstr(data
, ==, "ff");
145 data
= qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort
);
146 g_assert_cmpstr(data
, ==, "");
149 qemu_chr_fe_deinit(&be
);
150 object_unparent(OBJECT(chr
));
153 opts
= qemu_opts_create(qemu_find_opts("chardev"), "memory-label",
155 qemu_opt_set(opts
, "backend", "memory", &error_abort
);
156 qemu_opt_set(opts
, "size", "2", &error_abort
);
157 chr
= qemu_chr_new_from_opts(opts
, NULL
);
158 g_assert_nonnull(chr
);
159 object_unparent(OBJECT(chr
));
163 static void char_mux_test(void)
168 FeHandler h1
= { 0, }, h2
= { 0, };
169 CharBackend chr_be1
, chr_be2
;
171 opts
= qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
173 qemu_opt_set(opts
, "backend", "ringbuf", &error_abort
);
174 qemu_opt_set(opts
, "size", "128", &error_abort
);
175 qemu_opt_set(opts
, "mux", "on", &error_abort
);
176 chr
= qemu_chr_new_from_opts(opts
, &error_abort
);
177 g_assert_nonnull(chr
);
180 qemu_chr_fe_init(&chr_be1
, chr
, &error_abort
);
181 qemu_chr_fe_set_handlers(&chr_be1
,
188 qemu_chr_fe_init(&chr_be2
, chr
, &error_abort
);
189 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");
207 qemu_chr_be_write(base
, (void *)"\1c", 2);
209 qemu_chr_be_write(base
, (void *)"hello", 6);
210 g_assert_cmpint(h2
.read_count
, ==, 0);
211 g_assert_cmpint(h1
.read_count
, ==, 6);
212 g_assert_cmpstr(h1
.read_buf
, ==, "hello");
215 /* remove first handler */
216 qemu_chr_fe_set_handlers(&chr_be1
, NULL
, NULL
, NULL
, NULL
, NULL
, true);
217 qemu_chr_be_write(base
, (void *)"hello", 6);
218 g_assert_cmpint(h1
.read_count
, ==, 0);
219 g_assert_cmpint(h2
.read_count
, ==, 0);
221 qemu_chr_be_write(base
, (void *)"\1c", 2);
222 qemu_chr_be_write(base
, (void *)"hello", 6);
223 g_assert_cmpint(h1
.read_count
, ==, 0);
224 g_assert_cmpint(h2
.read_count
, ==, 6);
225 g_assert_cmpstr(h2
.read_buf
, ==, "hello");
229 qemu_chr_be_write(base
, (void *)"\1?", 2);
230 data
= qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort
);
231 g_assert_cmpint(strlen(data
), !=, 0);
234 qemu_chr_fe_deinit(&chr_be1
);
235 qemu_chr_fe_deinit(&chr_be2
);
236 object_unparent(OBJECT(chr
));
239 typedef struct SocketIdleData
{
244 CharBackend
*client_be
;
247 static gboolean
char_socket_test_idle(gpointer user_data
)
249 SocketIdleData
*data
= user_data
;
251 if (object_property_get_bool(OBJECT(data
->chr
), "connected", NULL
)
252 == data
->conn_expected
) {
260 static void socket_read(void *opaque
, const uint8_t *buf
, int size
)
262 SocketIdleData
*data
= opaque
;
264 g_assert_cmpint(size
, ==, 1);
265 g_assert_cmpint(*buf
, ==, 'Z');
267 size
= qemu_chr_fe_write(data
->be
, (const uint8_t *)"hello", 5);
268 g_assert_cmpint(size
, ==, 5);
271 static int socket_can_read(void *opaque
)
276 static void socket_read_hello(void *opaque
, const uint8_t *buf
, int size
)
278 g_assert_cmpint(size
, ==, 5);
279 g_assert(strncmp((char *)buf
, "hello", 5) == 0);
284 static int socket_can_read_hello(void *opaque
)
289 static void char_socket_test(void)
291 Chardev
*chr
= qemu_chr_new("server", "tcp:127.0.0.1:0,server,nowait");
296 SocketIdleData d
= { .chr
= chr
};
298 CharBackend client_be
;
304 g_assert_nonnull(chr
);
305 g_assert(!object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
307 addr
= object_property_get_qobject(OBJECT(chr
), "addr", &error_abort
);
308 qdict
= qobject_to_qdict(addr
);
309 port
= qdict_get_str(qdict
, "port");
310 tmp
= g_strdup_printf("tcp:127.0.0.1:%s", port
);
313 qemu_chr_fe_init(&be
, chr
, &error_abort
);
314 qemu_chr_fe_set_handlers(&be
, socket_can_read
, socket_read
,
315 NULL
, &d
, NULL
, true);
317 chr_client
= qemu_chr_new("client", tmp
);
318 qemu_chr_fe_init(&client_be
, chr_client
, &error_abort
);
319 qemu_chr_fe_set_handlers(&client_be
, socket_can_read_hello
,
321 NULL
, &d
, NULL
, true);
324 d
.conn_expected
= true;
325 guint id
= g_idle_add(char_socket_test_idle
, &d
);
326 g_source_set_name_by_id(id
, "test-idle");
327 g_assert_cmpint(id
, >, 0);
330 g_assert(object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
331 g_assert(object_property_get_bool(OBJECT(chr_client
),
332 "connected", &error_abort
));
334 qemu_chr_write_all(chr_client
, (const uint8_t *)"Z", 1);
337 object_unparent(OBJECT(chr_client
));
339 d
.conn_expected
= false;
340 g_idle_add(char_socket_test_idle
, &d
);
343 object_unparent(OBJECT(chr
));
347 static void char_pipe_test(void)
349 gchar
*tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
350 gchar
*tmp
, *in
, *out
, *pipe
= g_build_filename(tmp_path
, "pipe", NULL
);
355 FeHandler fe
= { 0, };
357 in
= g_strdup_printf("%s.in", pipe
);
358 if (mkfifo(in
, 0600) < 0) {
361 out
= g_strdup_printf("%s.out", pipe
);
362 if (mkfifo(out
, 0600) < 0) {
366 tmp
= g_strdup_printf("pipe:%s", pipe
);
367 chr
= qemu_chr_new("pipe", tmp
);
368 g_assert_nonnull(chr
);
371 qemu_chr_fe_init(&be
, chr
, &error_abort
);
373 ret
= qemu_chr_fe_write(&be
, (void *)"pipe-out", 9);
374 g_assert_cmpint(ret
, ==, 9);
376 fd
= open(out
, O_RDWR
);
377 ret
= read(fd
, buf
, sizeof(buf
));
378 g_assert_cmpint(ret
, ==, 9);
379 g_assert_cmpstr(buf
, ==, "pipe-out");
382 fd
= open(in
, O_WRONLY
);
383 ret
= write(fd
, "pipe-in", 8);
384 g_assert_cmpint(ret
, ==, 8);
387 qemu_chr_fe_set_handlers(&be
,
396 g_assert_cmpint(fe
.read_count
, ==, 8);
397 g_assert_cmpstr(fe
.read_buf
, ==, "pipe-in");
399 qemu_chr_fe_deinit(&be
);
400 object_unparent(OBJECT(chr
));
402 g_assert(g_unlink(in
) == 0);
403 g_assert(g_unlink(out
) == 0);
404 g_assert(g_rmdir(tmp_path
) == 0);
412 static void char_udp_test(void)
414 struct sockaddr_in addr
= { 0, }, other
;
415 SocketIdleData d
= { 0, };
418 socklen_t alen
= sizeof(addr
);
419 int ret
, sock
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
423 g_assert_cmpint(sock
, >, 0);
424 addr
.sin_family
= AF_INET
;
425 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
427 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
428 g_assert_cmpint(ret
, ==, 0);
429 ret
= getsockname(sock
, (struct sockaddr
*)&addr
, &alen
);
430 g_assert_cmpint(ret
, ==, 0);
432 tmp
= g_strdup_printf("udp:127.0.0.1:%d",
433 ntohs(addr
.sin_port
));
434 chr
= qemu_chr_new("client", tmp
);
435 g_assert_nonnull(chr
);
438 qemu_chr_fe_init(&be
, chr
, &error_abort
);
439 qemu_chr_fe_set_handlers(&be
, socket_can_read_hello
, socket_read_hello
,
440 NULL
, &d
, NULL
, true);
441 ret
= qemu_chr_write_all(chr
, (uint8_t *)"hello", 5);
442 g_assert_cmpint(ret
, ==, 5);
445 ret
= recvfrom(sock
, buf
, sizeof(buf
), 0,
446 (struct sockaddr
*)&other
, &alen
);
447 g_assert_cmpint(ret
, ==, 5);
448 ret
= sendto(sock
, buf
, 5, 0, (struct sockaddr
*)&other
, alen
);
449 g_assert_cmpint(ret
, ==, 5);
457 static void char_file_test(void)
459 char *tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
460 char *out
= g_build_filename(tmp_path
, "out", NULL
);
461 char *contents
= NULL
;
462 ChardevFile file
= { .out
= out
};
463 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
464 .u
.file
.data
= &file
};
469 chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_FILE
, &backend
,
471 ret
= qemu_chr_write_all(chr
, (uint8_t *)"hello!", 6);
472 g_assert_cmpint(ret
, ==, 6);
473 object_unref(OBJECT(chr
));
475 ret
= g_file_get_contents(out
, &contents
, &length
, NULL
);
476 g_assert(ret
== TRUE
);
477 g_assert_cmpint(length
, ==, 6);
478 g_assert(strncmp(contents
, "hello!", 6) == 0);
484 FeHandler fe
= { 0, };
485 char *fifo
= g_build_filename(tmp_path
, "fifo", NULL
);
488 if (mkfifo(fifo
, 0600) < 0) {
492 fd
= open(fifo
, O_RDWR
);
493 ret
= write(fd
, "fifo-in", 8);
494 g_assert_cmpint(ret
, ==, 8);
498 chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_FILE
, &backend
,
501 qemu_chr_fe_init(&be
, chr
, &error_abort
);
502 qemu_chr_fe_set_handlers(&be
,
512 g_assert_cmpint(fe
.read_count
, ==, 8);
513 g_assert_cmpstr(fe
.read_buf
, ==, "fifo-in");
514 qemu_chr_fe_deinit(&be
);
515 object_unref(OBJECT(chr
));
527 static void char_null_test(void)
534 chr
= qemu_chr_find("label-null");
537 chr
= qemu_chr_new("label-null", "null");
538 chr
= qemu_chr_find("label-null");
539 g_assert_nonnull(chr
);
541 g_assert(qemu_chr_has_feature(chr
,
542 QEMU_CHAR_FEATURE_FD_PASS
) == false);
543 g_assert(qemu_chr_has_feature(chr
,
544 QEMU_CHAR_FEATURE_RECONNECTABLE
) == false);
546 /* check max avail */
547 qemu_chr_fe_init(&be
, chr
, &error_abort
);
548 qemu_chr_fe_init(&be
, chr
, &err
);
549 error_free_or_abort(&err
);
551 /* deinit & reinit */
552 qemu_chr_fe_deinit(&be
);
553 qemu_chr_fe_init(&be
, chr
, &error_abort
);
555 qemu_chr_fe_set_open(&be
, true);
557 qemu_chr_fe_set_handlers(&be
,
563 ret
= qemu_chr_fe_write(&be
, (void *)"buf", 4);
564 g_assert_cmpint(ret
, ==, 4);
566 qemu_chr_fe_deinit(&be
);
567 object_unparent(OBJECT(chr
));
570 static void char_invalid_test(void)
574 chr
= qemu_chr_new("label-invalid", "invalid");
578 int main(int argc
, char **argv
)
580 qemu_init_main_loop(&error_abort
);
583 g_test_init(&argc
, &argv
, NULL
);
585 module_call_init(MODULE_INIT_QOM
);
586 qemu_add_opts(&qemu_chardev_opts
);
588 g_test_add_func("/char/null", char_null_test
);
589 g_test_add_func("/char/invalid", char_invalid_test
);
590 g_test_add_func("/char/ringbuf", char_ringbuf_test
);
591 g_test_add_func("/char/mux", char_mux_test
);
592 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
594 g_test_add_func("/char/console/subprocess", char_console_test_subprocess
);
595 g_test_add_func("/char/console", char_console_test
);
597 g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess
);
598 g_test_add_func("/char/stdio", char_stdio_test
);
601 g_test_add_func("/char/pipe", char_pipe_test
);
603 g_test_add_func("/char/file", char_file_test
);
604 g_test_add_func("/char/socket", char_socket_test
);
605 g_test_add_func("/char/udp", char_udp_test
);