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"
14 #include "io/channel-socket.h"
15 #include "qapi/qobject-input-visitor.h"
16 #include "qapi/qapi-visit-sockets.h"
20 typedef struct FeHandler
{
24 bool openclose_mismatch
;
29 static void main_loop(void)
33 main_loop_wait(false);
37 static int fe_can_read(void *opaque
)
39 FeHandler
*h
= opaque
;
41 return sizeof(h
->read_buf
) - h
->read_count
;
44 static void fe_read(void *opaque
, const uint8_t *buf
, int size
)
46 FeHandler
*h
= opaque
;
48 g_assert_cmpint(size
, <=, fe_can_read(opaque
));
50 memcpy(h
->read_buf
+ h
->read_count
, buf
, size
);
51 h
->read_count
+= size
;
55 static void fe_event(void *opaque
, int event
)
57 FeHandler
*h
= opaque
;
60 h
->last_event
= event
;
64 case CHR_EVENT_OPENED
:
65 case CHR_EVENT_CLOSED
:
67 new_open_state
= (event
== CHR_EVENT_OPENED
);
68 if (h
->is_open
== new_open_state
) {
69 h
->openclose_mismatch
= true;
71 h
->is_open
= new_open_state
;
80 static void char_console_test_subprocess(void)
85 opts
= qemu_opts_create(qemu_find_opts("chardev"), "console-label",
87 qemu_opt_set(opts
, "backend", "console", &error_abort
);
89 chr
= qemu_chr_new_from_opts(opts
, NULL
, NULL
);
90 g_assert_nonnull(chr
);
92 qemu_chr_write_all(chr
, (const uint8_t *)"CONSOLE", 7);
95 object_unparent(OBJECT(chr
));
98 static void char_console_test(void)
100 g_test_trap_subprocess("/char/console/subprocess", 0, 0);
101 g_test_trap_assert_passed();
102 g_test_trap_assert_stdout("CONSOLE");
105 static void char_stdio_test_subprocess(void)
111 chr
= qemu_chr_new("label", "stdio", NULL
);
112 g_assert_nonnull(chr
);
114 qemu_chr_fe_init(&be
, chr
, &error_abort
);
115 qemu_chr_fe_set_open(&be
, true);
116 ret
= qemu_chr_fe_write(&be
, (void *)"buf", 4);
117 g_assert_cmpint(ret
, ==, 4);
119 qemu_chr_fe_deinit(&be
, true);
122 static void char_stdio_test(void)
124 g_test_trap_subprocess("/char/stdio/subprocess", 0, 0);
125 g_test_trap_assert_passed();
126 g_test_trap_assert_stdout("buf");
129 static void char_ringbuf_test(void)
137 opts
= qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
139 qemu_opt_set(opts
, "backend", "ringbuf", &error_abort
);
141 qemu_opt_set(opts
, "size", "5", &error_abort
);
142 chr
= qemu_chr_new_from_opts(opts
, NULL
, NULL
);
146 opts
= qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
148 qemu_opt_set(opts
, "backend", "ringbuf", &error_abort
);
149 qemu_opt_set(opts
, "size", "2", &error_abort
);
150 chr
= qemu_chr_new_from_opts(opts
, NULL
, &error_abort
);
151 g_assert_nonnull(chr
);
154 qemu_chr_fe_init(&be
, chr
, &error_abort
);
155 ret
= qemu_chr_fe_write(&be
, (void *)"buff", 4);
156 g_assert_cmpint(ret
, ==, 4);
158 data
= qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort
);
159 g_assert_cmpstr(data
, ==, "ff");
162 data
= qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort
);
163 g_assert_cmpstr(data
, ==, "");
166 qemu_chr_fe_deinit(&be
, true);
169 opts
= qemu_opts_create(qemu_find_opts("chardev"), "memory-label",
171 qemu_opt_set(opts
, "backend", "memory", &error_abort
);
172 qemu_opt_set(opts
, "size", "2", &error_abort
);
173 chr
= qemu_chr_new_from_opts(opts
, NULL
, NULL
);
174 g_assert_nonnull(chr
);
175 object_unparent(OBJECT(chr
));
179 static void char_mux_test(void)
184 FeHandler h1
= { 0, false, 0, false, }, h2
= { 0, false, 0, false, };
185 CharBackend chr_be1
, chr_be2
;
187 opts
= qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
189 qemu_opt_set(opts
, "backend", "ringbuf", &error_abort
);
190 qemu_opt_set(opts
, "size", "128", &error_abort
);
191 qemu_opt_set(opts
, "mux", "on", &error_abort
);
192 chr
= qemu_chr_new_from_opts(opts
, NULL
, &error_abort
);
193 g_assert_nonnull(chr
);
196 qemu_chr_fe_init(&chr_be1
, chr
, &error_abort
);
197 qemu_chr_fe_set_handlers(&chr_be1
,
205 qemu_chr_fe_init(&chr_be2
, chr
, &error_abort
);
206 qemu_chr_fe_set_handlers(&chr_be2
,
213 qemu_chr_fe_take_focus(&chr_be2
);
215 base
= qemu_chr_find("mux-label-base");
216 g_assert_cmpint(qemu_chr_be_can_write(base
), !=, 0);
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");
224 g_assert_cmpint(h1
.last_event
, !=, 42); /* should be MUX_OUT or OPENED */
225 g_assert_cmpint(h2
.last_event
, !=, 42); /* should be MUX_IN or OPENED */
226 /* sending event on the base broadcast to all fe, historical reasons? */
227 qemu_chr_be_event(base
, 42);
228 g_assert_cmpint(h1
.last_event
, ==, 42);
229 g_assert_cmpint(h2
.last_event
, ==, 42);
230 qemu_chr_be_event(chr
, -1);
231 g_assert_cmpint(h1
.last_event
, ==, 42);
232 g_assert_cmpint(h2
.last_event
, ==, -1);
235 qemu_chr_be_write(base
, (void *)"\1b", 2);
236 g_assert_cmpint(h1
.last_event
, ==, 42);
237 g_assert_cmpint(h2
.last_event
, ==, CHR_EVENT_BREAK
);
239 qemu_chr_be_write(base
, (void *)"\1c", 2);
240 g_assert_cmpint(h1
.last_event
, ==, CHR_EVENT_MUX_IN
);
241 g_assert_cmpint(h2
.last_event
, ==, CHR_EVENT_MUX_OUT
);
242 qemu_chr_be_event(chr
, -1);
243 g_assert_cmpint(h1
.last_event
, ==, -1);
244 g_assert_cmpint(h2
.last_event
, ==, CHR_EVENT_MUX_OUT
);
246 qemu_chr_be_write(base
, (void *)"hello", 6);
247 g_assert_cmpint(h2
.read_count
, ==, 0);
248 g_assert_cmpint(h1
.read_count
, ==, 6);
249 g_assert_cmpstr(h1
.read_buf
, ==, "hello");
252 qemu_chr_be_write(base
, (void *)"\1b", 2);
253 g_assert_cmpint(h1
.last_event
, ==, CHR_EVENT_BREAK
);
254 g_assert_cmpint(h2
.last_event
, ==, CHR_EVENT_MUX_OUT
);
256 /* open/close state and corresponding events */
257 g_assert_true(qemu_chr_fe_backend_open(&chr_be1
));
258 g_assert_true(qemu_chr_fe_backend_open(&chr_be2
));
259 g_assert_true(h1
.is_open
);
260 g_assert_false(h1
.openclose_mismatch
);
261 g_assert_true(h2
.is_open
);
262 g_assert_false(h2
.openclose_mismatch
);
264 h1
.openclose_count
= h2
.openclose_count
= 0;
266 qemu_chr_fe_set_handlers(&chr_be1
, NULL
, NULL
, NULL
, NULL
,
268 qemu_chr_fe_set_handlers(&chr_be2
, NULL
, NULL
, NULL
, NULL
,
270 g_assert_cmpint(h1
.openclose_count
, ==, 0);
271 g_assert_cmpint(h2
.openclose_count
, ==, 0);
273 h1
.is_open
= h2
.is_open
= false;
274 qemu_chr_fe_set_handlers(&chr_be1
,
281 qemu_chr_fe_set_handlers(&chr_be2
,
288 g_assert_cmpint(h1
.openclose_count
, ==, 1);
289 g_assert_false(h1
.openclose_mismatch
);
290 g_assert_cmpint(h2
.openclose_count
, ==, 1);
291 g_assert_false(h2
.openclose_mismatch
);
293 qemu_chr_be_event(base
, CHR_EVENT_CLOSED
);
294 qemu_chr_be_event(base
, CHR_EVENT_OPENED
);
295 g_assert_cmpint(h1
.openclose_count
, ==, 3);
296 g_assert_false(h1
.openclose_mismatch
);
297 g_assert_cmpint(h2
.openclose_count
, ==, 3);
298 g_assert_false(h2
.openclose_mismatch
);
300 qemu_chr_fe_set_handlers(&chr_be2
,
307 qemu_chr_fe_set_handlers(&chr_be1
,
315 /* remove first handler */
316 qemu_chr_fe_set_handlers(&chr_be1
, NULL
, NULL
, NULL
, NULL
,
318 qemu_chr_be_write(base
, (void *)"hello", 6);
319 g_assert_cmpint(h1
.read_count
, ==, 0);
320 g_assert_cmpint(h2
.read_count
, ==, 0);
322 qemu_chr_be_write(base
, (void *)"\1c", 2);
323 qemu_chr_be_write(base
, (void *)"hello", 6);
324 g_assert_cmpint(h1
.read_count
, ==, 0);
325 g_assert_cmpint(h2
.read_count
, ==, 6);
326 g_assert_cmpstr(h2
.read_buf
, ==, "hello");
330 qemu_chr_be_write(base
, (void *)"\1?", 2);
331 data
= qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort
);
332 g_assert_cmpint(strlen(data
), !=, 0);
335 qemu_chr_fe_deinit(&chr_be1
, false);
336 qemu_chr_fe_deinit(&chr_be2
, true);
340 static void websock_server_read(void *opaque
, const uint8_t *buf
, int size
)
342 g_assert_cmpint(size
, ==, 5);
343 g_assert(memcmp(buf
, "world", size
) == 0);
348 static int websock_server_can_read(void *opaque
)
354 static bool websock_check_http_headers(char *buf
, int size
)
357 const char *ans
[] = { "HTTP/1.1 101 Switching Protocols\r\n",
358 "Server: QEMU VNC\r\n",
359 "Upgrade: websocket\r\n",
360 "Connection: Upgrade\r\n",
361 "Sec-WebSocket-Accept:",
362 "Sec-WebSocket-Protocol: binary\r\n" };
364 for (i
= 0; i
< 6; i
++) {
365 if (g_strstr_len(buf
, size
, ans
[i
]) == NULL
) {
374 static void websock_client_read(void *opaque
, const uint8_t *buf
, int size
)
376 const uint8_t ping
[] = { 0x89, 0x85, /* Ping header */
377 0x07, 0x77, 0x9e, 0xf9, /* Masking key */
378 0x6f, 0x12, 0xf2, 0x95, 0x68 /* "hello" */ };
380 const uint8_t binary
[] = { 0x82, 0x85, /* Binary header */
381 0x74, 0x90, 0xb9, 0xdf, /* Masking key */
382 0x03, 0xff, 0xcb, 0xb3, 0x10 /* "world" */ };
383 Chardev
*chr_client
= opaque
;
385 if (websock_check_http_headers((char *) buf
, size
)) {
386 qemu_chr_fe_write(chr_client
->be
, ping
, sizeof(ping
));
387 } else if (buf
[0] == 0x8a && buf
[1] == 0x05) {
388 g_assert(strncmp((char *) buf
+ 2, "hello", 5) == 0);
389 qemu_chr_fe_write(chr_client
->be
, binary
, sizeof(binary
));
391 g_assert(buf
[0] == 0x88 && buf
[1] == 0x16);
392 g_assert(strncmp((char *) buf
+ 4, "peer requested close", 10) == 0);
398 static int websock_client_can_read(void *opaque
)
404 static void char_websock_test(void)
410 char *handshake_port
;
412 CharBackend client_be
;
414 Chardev
*chr
= qemu_chr_new("server",
415 "websocket:127.0.0.1:0,server,nowait", NULL
);
416 const char handshake
[] = "GET / HTTP/1.1\r\n"
417 "Upgrade: websocket\r\n"
418 "Connection: Upgrade\r\n"
419 "Host: localhost:%s\r\n"
420 "Origin: http://localhost:%s\r\n"
421 "Sec-WebSocket-Key: o9JHNiS3/0/0zYE1wa3yIw==\r\n"
422 "Sec-WebSocket-Version: 13\r\n"
423 "Sec-WebSocket-Protocol: binary\r\n\r\n";
424 const uint8_t close
[] = { 0x88, 0x82, /* Close header */
425 0xef, 0xaa, 0xc5, 0x97, /* Masking key */
426 0xec, 0x42 /* Status code */ };
428 addr
= object_property_get_qobject(OBJECT(chr
), "addr", &error_abort
);
429 qdict
= qobject_to(QDict
, addr
);
430 port
= qdict_get_str(qdict
, "port");
431 tmp
= g_strdup_printf("tcp:127.0.0.1:%s", port
);
432 handshake_port
= g_strdup_printf(handshake
, port
, port
);
433 qobject_unref(qdict
);
435 qemu_chr_fe_init(&be
, chr
, &error_abort
);
436 qemu_chr_fe_set_handlers(&be
, websock_server_can_read
, websock_server_read
,
437 NULL
, NULL
, chr
, NULL
, true);
439 chr_client
= qemu_chr_new("client", tmp
, NULL
);
440 qemu_chr_fe_init(&client_be
, chr_client
, &error_abort
);
441 qemu_chr_fe_set_handlers(&client_be
, websock_client_can_read
,
443 NULL
, NULL
, chr_client
, NULL
, true);
446 qemu_chr_write_all(chr_client
,
447 (uint8_t *) handshake_port
,
448 strlen(handshake_port
));
449 g_free(handshake_port
);
452 g_assert(object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
453 g_assert(object_property_get_bool(OBJECT(chr_client
),
454 "connected", &error_abort
));
456 qemu_chr_write_all(chr_client
, close
, sizeof(close
));
459 object_unparent(OBJECT(chr_client
));
460 object_unparent(OBJECT(chr
));
465 static void char_pipe_test(void)
467 gchar
*tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
468 gchar
*tmp
, *in
, *out
, *pipe
= g_build_filename(tmp_path
, "pipe", NULL
);
473 FeHandler fe
= { 0, };
475 in
= g_strdup_printf("%s.in", pipe
);
476 if (mkfifo(in
, 0600) < 0) {
479 out
= g_strdup_printf("%s.out", pipe
);
480 if (mkfifo(out
, 0600) < 0) {
484 tmp
= g_strdup_printf("pipe:%s", pipe
);
485 chr
= qemu_chr_new("pipe", tmp
, NULL
);
486 g_assert_nonnull(chr
);
489 qemu_chr_fe_init(&be
, chr
, &error_abort
);
491 ret
= qemu_chr_fe_write(&be
, (void *)"pipe-out", 9);
492 g_assert_cmpint(ret
, ==, 9);
494 fd
= open(out
, O_RDWR
);
495 ret
= read(fd
, buf
, sizeof(buf
));
496 g_assert_cmpint(ret
, ==, 9);
497 g_assert_cmpstr(buf
, ==, "pipe-out");
500 fd
= open(in
, O_WRONLY
);
501 ret
= write(fd
, "pipe-in", 8);
502 g_assert_cmpint(ret
, ==, 8);
505 qemu_chr_fe_set_handlers(&be
,
515 g_assert_cmpint(fe
.read_count
, ==, 8);
516 g_assert_cmpstr(fe
.read_buf
, ==, "pipe-in");
518 qemu_chr_fe_deinit(&be
, true);
520 g_assert(g_unlink(in
) == 0);
521 g_assert(g_unlink(out
) == 0);
522 g_assert(g_rmdir(tmp_path
) == 0);
530 typedef struct SocketIdleData
{
535 CharBackend
*client_be
;
539 static void socket_read_hello(void *opaque
, const uint8_t *buf
, int size
)
541 g_assert_cmpint(size
, ==, 5);
542 g_assert(strncmp((char *)buf
, "hello", 5) == 0);
547 static int socket_can_read_hello(void *opaque
)
552 static int make_udp_socket(int *port
)
554 struct sockaddr_in addr
= { 0, };
555 socklen_t alen
= sizeof(addr
);
556 int ret
, sock
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
558 g_assert_cmpint(sock
, >, 0);
559 addr
.sin_family
= AF_INET
;
560 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
562 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
563 g_assert_cmpint(ret
, ==, 0);
564 ret
= getsockname(sock
, (struct sockaddr
*)&addr
, &alen
);
565 g_assert_cmpint(ret
, ==, 0);
567 *port
= ntohs(addr
.sin_port
);
571 static void char_udp_test_internal(Chardev
*reuse_chr
, int sock
)
573 struct sockaddr_in other
;
574 SocketIdleData d
= { 0, };
577 socklen_t alen
= sizeof(other
);
587 sock
= make_udp_socket(&port
);
588 tmp
= g_strdup_printf("udp:127.0.0.1:%d", port
);
589 chr
= qemu_chr_new("client", tmp
, NULL
);
590 g_assert_nonnull(chr
);
592 be
= g_alloca(sizeof(CharBackend
));
593 qemu_chr_fe_init(be
, chr
, &error_abort
);
597 qemu_chr_fe_set_handlers(be
, socket_can_read_hello
, socket_read_hello
,
598 NULL
, NULL
, &d
, NULL
, true);
599 ret
= qemu_chr_write_all(chr
, (uint8_t *)"hello", 5);
600 g_assert_cmpint(ret
, ==, 5);
602 ret
= recvfrom(sock
, buf
, sizeof(buf
), 0,
603 (struct sockaddr
*)&other
, &alen
);
604 g_assert_cmpint(ret
, ==, 5);
605 ret
= sendto(sock
, buf
, 5, 0, (struct sockaddr
*)&other
, alen
);
606 g_assert_cmpint(ret
, ==, 5);
612 qemu_chr_fe_deinit(be
, true);
617 static void char_udp_test(void)
619 char_udp_test_internal(NULL
, 0);
626 } CharSocketTestData
;
629 #define SOCKET_PING "Hello"
630 #define SOCKET_PONG "World"
634 char_socket_event(void *opaque
, int event
)
636 CharSocketTestData
*data
= opaque
;
642 char_socket_read(void *opaque
, const uint8_t *buf
, int size
)
644 CharSocketTestData
*data
= opaque
;
645 g_assert_cmpint(size
, ==, sizeof(SOCKET_PONG
));
646 g_assert(memcmp(buf
, SOCKET_PONG
, size
) == 0);
647 data
->got_pong
= true;
652 char_socket_can_read(void *opaque
)
654 return sizeof(SOCKET_PONG
);
659 char_socket_addr_to_opt_str(SocketAddress
*addr
, bool fd_pass
,
660 const char *reconnect
, bool is_listen
)
663 QIOChannelSocket
*ioc
= qio_channel_socket_new();
666 g_assert(!reconnect
);
668 qio_channel_socket_listen_sync(ioc
, addr
, &error_abort
);
670 qio_channel_socket_connect_sync(ioc
, addr
, &error_abort
);
674 optstr
= g_strdup_printf("socket,id=cdev0,fd=%d%s",
675 fd
, is_listen
? ",server,nowait" : "");
676 object_unref(OBJECT(ioc
));
679 switch (addr
->type
) {
680 case SOCKET_ADDRESS_TYPE_INET
:
681 return g_strdup_printf("socket,id=cdev0,host=%s,port=%s%s%s",
684 reconnect
? reconnect
: "",
685 is_listen
? ",server,nowait" : "");
687 case SOCKET_ADDRESS_TYPE_UNIX
:
688 return g_strdup_printf("socket,id=cdev0,path=%s%s%s",
690 reconnect
? reconnect
: "",
691 is_listen
? ",server,nowait" : "");
694 g_assert_not_reached();
701 char_socket_ping_pong(QIOChannel
*ioc
)
703 char greeting
[sizeof(SOCKET_PING
)];
704 const char *response
= SOCKET_PONG
;
706 qio_channel_read_all(ioc
, greeting
, sizeof(greeting
), &error_abort
);
708 g_assert(memcmp(greeting
, SOCKET_PING
, sizeof(greeting
)) == 0);
710 qio_channel_write_all(ioc
, response
, sizeof(SOCKET_PONG
), &error_abort
);
712 object_unref(OBJECT(ioc
));
717 char_socket_server_client_thread(gpointer data
)
719 SocketAddress
*addr
= data
;
720 QIOChannelSocket
*ioc
= qio_channel_socket_new();
722 qio_channel_socket_connect_sync(ioc
, addr
, &error_abort
);
724 char_socket_ping_pong(QIO_CHANNEL(ioc
));
734 } CharSocketServerTestConfig
;
737 static void char_socket_server_test(gconstpointer opaque
)
739 const CharSocketServerTestConfig
*config
= opaque
;
741 CharBackend be
= {0};
742 CharSocketTestData data
= {0};
748 bool reconnected
= false;
752 g_setenv("QTEST_SILENT_ERRORS", "1", 1);
754 * We rely on config->addr containing "nowait", otherwise
755 * qemu_chr_new() will block until a client connects. We
756 * can't spawn our client thread though, because until
757 * qemu_chr_new() returns we don't know what TCP port was
758 * allocated by the OS
760 optstr
= char_socket_addr_to_opt_str(config
->addr
,
764 opts
= qemu_opts_parse_noisily(qemu_find_opts("chardev"),
766 g_assert_nonnull(opts
);
767 chr
= qemu_chr_new_from_opts(opts
, NULL
, &error_abort
);
769 g_assert_nonnull(chr
);
770 g_assert(!object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
772 qaddr
= object_property_get_qobject(OBJECT(chr
), "addr", &error_abort
);
773 g_assert_nonnull(qaddr
);
775 v
= qobject_input_visitor_new(qaddr
);
776 visit_type_SocketAddress(v
, "addr", &addr
, &error_abort
);
778 qobject_unref(qaddr
);
780 qemu_chr_fe_init(&be
, chr
, &error_abort
);
784 qemu_chr_fe_set_handlers(&be
, NULL
, NULL
,
785 char_socket_event
, NULL
,
787 g_assert(data
.event
== -1);
790 * Kick off a thread to act as the "remote" client
791 * which just plays ping-pong with us
793 qemu_thread_create(&thread
, "client",
794 char_socket_server_client_thread
,
795 addr
, QEMU_THREAD_JOINABLE
);
796 g_assert(data
.event
== -1);
798 if (config
->wait_connected
) {
799 /* Synchronously accept a connection */
800 qemu_chr_wait_connected(chr
, &error_abort
);
803 * Asynchronously accept a connection when the evnt
804 * loop reports the listener socket as readable
806 while (data
.event
== -1) {
807 main_loop_wait(false);
810 g_assert(object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
811 g_assert(data
.event
== CHR_EVENT_OPENED
);
814 /* Send a greeting to the client */
815 ret
= qemu_chr_fe_write_all(&be
, (const uint8_t *)SOCKET_PING
,
816 sizeof(SOCKET_PING
));
817 g_assert_cmpint(ret
, ==, sizeof(SOCKET_PING
));
818 g_assert(data
.event
== -1);
820 /* Setup a callback to receive the reply to our greeting */
821 qemu_chr_fe_set_handlers(&be
, char_socket_can_read
,
823 char_socket_event
, NULL
,
825 g_assert(data
.event
== CHR_EVENT_OPENED
);
828 /* Wait for the client to go away */
829 while (data
.event
== -1) {
830 main_loop_wait(false);
832 g_assert(!object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
833 g_assert(data
.event
== CHR_EVENT_CLOSED
);
834 g_assert(data
.got_pong
);
836 qemu_thread_join(&thread
);
843 qapi_free_SocketAddress(addr
);
844 object_unparent(OBJECT(chr
));
846 g_unsetenv("QTEST_SILENT_ERRORS");
851 char_socket_client_server_thread(gpointer data
)
853 QIOChannelSocket
*ioc
= data
;
854 QIOChannelSocket
*cioc
;
856 cioc
= qio_channel_socket_accept(ioc
, &error_abort
);
857 g_assert_nonnull(cioc
);
859 char_socket_ping_pong(QIO_CHANNEL(cioc
));
867 const char *reconnect
;
870 } CharSocketClientTestConfig
;
873 static void char_socket_client_test(gconstpointer opaque
)
875 const CharSocketClientTestConfig
*config
= opaque
;
876 QIOChannelSocket
*ioc
;
879 CharBackend be
= {0};
880 CharSocketTestData data
= {0};
884 bool reconnected
= false;
888 * Setup a listener socket and determine get its address
889 * so we know the TCP port for the client later
891 ioc
= qio_channel_socket_new();
892 g_assert_nonnull(ioc
);
893 qio_channel_socket_listen_sync(ioc
, config
->addr
, &error_abort
);
894 addr
= qio_channel_socket_get_local_address(ioc
, &error_abort
);
895 g_assert_nonnull(addr
);
898 * Kick off a thread to act as the "remote" client
899 * which just plays ping-pong with us
901 qemu_thread_create(&thread
, "client",
902 char_socket_client_server_thread
,
903 ioc
, QEMU_THREAD_JOINABLE
);
906 * Populate the chardev address based on what the server
907 * is actually listening on
909 optstr
= char_socket_addr_to_opt_str(addr
,
914 opts
= qemu_opts_parse_noisily(qemu_find_opts("chardev"),
916 g_assert_nonnull(opts
);
917 chr
= qemu_chr_new_from_opts(opts
, NULL
, &error_abort
);
919 g_assert_nonnull(chr
);
921 if (config
->reconnect
) {
923 * If reconnect is set, the connection will be
924 * established in a background thread and we won't
925 * see the "connected" status updated until we
926 * run the main event loop, or call qemu_chr_wait_connected
928 g_assert(!object_property_get_bool(OBJECT(chr
), "connected",
931 g_assert(object_property_get_bool(OBJECT(chr
), "connected",
935 qemu_chr_fe_init(&be
, chr
, &error_abort
);
939 qemu_chr_fe_set_handlers(&be
, NULL
, NULL
,
940 char_socket_event
, NULL
,
942 if (config
->reconnect
) {
943 g_assert(data
.event
== -1);
945 g_assert(data
.event
== CHR_EVENT_OPENED
);
948 if (config
->wait_connected
) {
950 * Synchronously wait for the connection to complete
951 * This should be a no-op if reconnect is not set.
953 qemu_chr_wait_connected(chr
, &error_abort
);
956 * Asynchronously wait for the connection to be reported
957 * as complete when the background thread reports its
959 * The loop will short-circuit if reconnect was set
961 while (data
.event
== -1) {
962 main_loop_wait(false);
965 g_assert(data
.event
== CHR_EVENT_OPENED
);
967 g_assert(object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
969 /* Send a greeting to the server */
970 ret
= qemu_chr_fe_write_all(&be
, (const uint8_t *)SOCKET_PING
,
971 sizeof(SOCKET_PING
));
972 g_assert_cmpint(ret
, ==, sizeof(SOCKET_PING
));
973 g_assert(data
.event
== -1);
975 /* Setup a callback to receive the reply to our greeting */
976 qemu_chr_fe_set_handlers(&be
, char_socket_can_read
,
978 char_socket_event
, NULL
,
980 g_assert(data
.event
== CHR_EVENT_OPENED
);
983 /* Wait for the server to go away */
984 while (data
.event
== -1) {
985 main_loop_wait(false);
987 g_assert(data
.event
== CHR_EVENT_CLOSED
);
988 g_assert(!object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
989 g_assert(data
.got_pong
);
990 qemu_thread_join(&thread
);
992 if (config
->reconnect
&& !reconnected
) {
994 qemu_thread_create(&thread
, "client",
995 char_socket_client_server_thread
,
996 ioc
, QEMU_THREAD_JOINABLE
);
1000 object_unref(OBJECT(ioc
));
1001 object_unparent(OBJECT(chr
));
1002 qapi_free_SocketAddress(addr
);
1007 count_closed_event(void *opaque
, int event
)
1009 int *count
= opaque
;
1010 if (event
== CHR_EVENT_CLOSED
) {
1016 char_socket_discard_read(void *opaque
, const uint8_t *buf
, int size
)
1020 static void char_socket_server_two_clients_test(gconstpointer opaque
)
1022 SocketAddress
*incoming_addr
= (gpointer
) opaque
;
1024 CharBackend be
= {0};
1026 SocketAddress
*addr
;
1030 QIOChannelSocket
*ioc1
, *ioc2
;
1033 g_setenv("QTEST_SILENT_ERRORS", "1", 1);
1035 * We rely on addr containing "nowait", otherwise
1036 * qemu_chr_new() will block until a client connects. We
1037 * can't spawn our client thread though, because until
1038 * qemu_chr_new() returns we don't know what TCP port was
1039 * allocated by the OS
1041 optstr
= char_socket_addr_to_opt_str(incoming_addr
,
1045 opts
= qemu_opts_parse_noisily(qemu_find_opts("chardev"),
1047 g_assert_nonnull(opts
);
1048 chr
= qemu_chr_new_from_opts(opts
, NULL
, &error_abort
);
1049 qemu_opts_del(opts
);
1050 g_assert_nonnull(chr
);
1051 g_assert(!object_property_get_bool(OBJECT(chr
), "connected", &error_abort
));
1053 qaddr
= object_property_get_qobject(OBJECT(chr
), "addr", &error_abort
);
1054 g_assert_nonnull(qaddr
);
1056 v
= qobject_input_visitor_new(qaddr
);
1057 visit_type_SocketAddress(v
, "addr", &addr
, &error_abort
);
1059 qobject_unref(qaddr
);
1061 qemu_chr_fe_init(&be
, chr
, &error_abort
);
1063 qemu_chr_fe_set_handlers(&be
, char_socket_can_read
, char_socket_discard_read
,
1064 count_closed_event
, NULL
,
1065 &closed
, NULL
, true);
1067 ioc1
= qio_channel_socket_new();
1068 qio_channel_socket_connect_sync(ioc1
, addr
, &error_abort
);
1069 qemu_chr_wait_connected(chr
, &error_abort
);
1071 /* switch the chardev to another context */
1072 GMainContext
*ctx
= g_main_context_new();
1073 qemu_chr_fe_set_handlers(&be
, char_socket_can_read
, char_socket_discard_read
,
1074 count_closed_event
, NULL
,
1075 &closed
, ctx
, true);
1077 /* Start a second connection while the first is still connected.
1078 * It will be placed in the listen() backlog, and connect() will
1079 * succeed immediately.
1081 ioc2
= qio_channel_socket_new();
1082 qio_channel_socket_connect_sync(ioc2
, addr
, &error_abort
);
1084 object_unref(OBJECT(ioc1
));
1085 /* The two connections should now be processed serially. */
1086 while (g_main_context_iteration(ctx
, TRUE
)) {
1087 if (closed
== 1 && ioc2
) {
1088 object_unref(OBJECT(ioc2
));
1096 qapi_free_SocketAddress(addr
);
1097 object_unparent(OBJECT(chr
));
1098 g_main_context_unref(ctx
);
1100 g_unsetenv("QTEST_SILENT_ERRORS");
1104 #ifdef HAVE_CHARDEV_SERIAL
1105 static void char_serial_test(void)
1110 opts
= qemu_opts_create(qemu_find_opts("chardev"), "serial-id",
1112 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
1113 qemu_opt_set(opts
, "path", "/dev/null", &error_abort
);
1115 chr
= qemu_chr_new_from_opts(opts
, NULL
, NULL
);
1116 g_assert_nonnull(chr
);
1117 /* TODO: add more tests with a pty */
1118 object_unparent(OBJECT(chr
));
1120 /* test tty alias */
1121 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
1122 chr
= qemu_chr_new_from_opts(opts
, NULL
, NULL
);
1123 g_assert_nonnull(chr
);
1124 object_unparent(OBJECT(chr
));
1126 qemu_opts_del(opts
);
1131 static void char_file_fifo_test(void)
1135 char *tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
1136 char *fifo
= g_build_filename(tmp_path
, "fifo", NULL
);
1137 char *out
= g_build_filename(tmp_path
, "out", NULL
);
1138 ChardevFile file
= { .in
= fifo
,
1141 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
1142 .u
.file
.data
= &file
};
1143 FeHandler fe
= { 0, };
1146 if (mkfifo(fifo
, 0600) < 0) {
1150 fd
= open(fifo
, O_RDWR
);
1151 ret
= write(fd
, "fifo-in", 8);
1152 g_assert_cmpint(ret
, ==, 8);
1154 chr
= qemu_chardev_new("label-file", TYPE_CHARDEV_FILE
, &backend
,
1155 NULL
, &error_abort
);
1157 qemu_chr_fe_init(&be
, chr
, &error_abort
);
1158 qemu_chr_fe_set_handlers(&be
,
1165 g_assert_cmpint(fe
.last_event
, !=, CHR_EVENT_BREAK
);
1166 qmp_chardev_send_break("label-foo", NULL
);
1167 g_assert_cmpint(fe
.last_event
, !=, CHR_EVENT_BREAK
);
1168 qmp_chardev_send_break("label-file", NULL
);
1169 g_assert_cmpint(fe
.last_event
, ==, CHR_EVENT_BREAK
);
1175 g_assert_cmpint(fe
.read_count
, ==, 8);
1176 g_assert_cmpstr(fe
.read_buf
, ==, "fifo-in");
1178 qemu_chr_fe_deinit(&be
, true);
1189 static void char_file_test_internal(Chardev
*ext_chr
, const char *filepath
)
1191 char *tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
1194 char *contents
= NULL
;
1195 ChardevFile file
= {};
1196 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
1197 .u
.file
.data
= &file
};
1203 out
= g_strdup(filepath
);
1206 out
= g_build_filename(tmp_path
, "out", NULL
);
1208 chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_FILE
, &backend
,
1209 NULL
, &error_abort
);
1211 ret
= qemu_chr_write_all(chr
, (uint8_t *)"hello!", 6);
1212 g_assert_cmpint(ret
, ==, 6);
1214 ret
= g_file_get_contents(out
, &contents
, &length
, NULL
);
1215 g_assert(ret
== TRUE
);
1216 g_assert_cmpint(length
, ==, 6);
1217 g_assert(strncmp(contents
, "hello!", 6) == 0);
1220 object_unref(OBJECT(chr
));
1229 static void char_file_test(void)
1231 char_file_test_internal(NULL
, NULL
);
1234 static void char_null_test(void)
1241 chr
= qemu_chr_find("label-null");
1244 chr
= qemu_chr_new("label-null", "null", NULL
);
1245 chr
= qemu_chr_find("label-null");
1246 g_assert_nonnull(chr
);
1248 g_assert(qemu_chr_has_feature(chr
,
1249 QEMU_CHAR_FEATURE_FD_PASS
) == false);
1250 g_assert(qemu_chr_has_feature(chr
,
1251 QEMU_CHAR_FEATURE_RECONNECTABLE
) == false);
1253 /* check max avail */
1254 qemu_chr_fe_init(&be
, chr
, &error_abort
);
1255 qemu_chr_fe_init(&be
, chr
, &err
);
1256 error_free_or_abort(&err
);
1258 /* deinit & reinit */
1259 qemu_chr_fe_deinit(&be
, false);
1260 qemu_chr_fe_init(&be
, chr
, &error_abort
);
1262 qemu_chr_fe_set_open(&be
, true);
1264 qemu_chr_fe_set_handlers(&be
,
1271 ret
= qemu_chr_fe_write(&be
, (void *)"buf", 4);
1272 g_assert_cmpint(ret
, ==, 4);
1274 qemu_chr_fe_deinit(&be
, true);
1277 static void char_invalid_test(void)
1280 g_setenv("QTEST_SILENT_ERRORS", "1", 1);
1281 chr
= qemu_chr_new("label-invalid", "invalid", NULL
);
1283 g_unsetenv("QTEST_SILENT_ERRORS");
1286 static int chardev_change(void *opaque
)
1291 static int chardev_change_denied(void *opaque
)
1296 static void char_hotswap_test(void)
1302 gchar
*tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
1303 char *filename
= g_build_filename(tmp_path
, "file", NULL
);
1304 ChardevFile file
= { .out
= filename
};
1305 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
1306 .u
.file
.data
= &file
};
1310 int sock
= make_udp_socket(&port
);
1311 g_assert_cmpint(sock
, >, 0);
1313 chr_args
= g_strdup_printf("udp:127.0.0.1:%d", port
);
1315 chr
= qemu_chr_new("chardev", chr_args
, NULL
);
1316 qemu_chr_fe_init(&be
, chr
, &error_abort
);
1318 /* check that chardev operates correctly */
1319 char_udp_test_internal(chr
, sock
);
1321 /* set the handler that denies the hotswap */
1322 qemu_chr_fe_set_handlers(&be
, NULL
, NULL
,
1323 NULL
, chardev_change_denied
, NULL
, NULL
, true);
1325 /* now, change is denied and has to keep the old backend operating */
1326 ret
= qmp_chardev_change("chardev", &backend
, NULL
);
1328 g_assert(be
.chr
== chr
);
1330 char_udp_test_internal(chr
, sock
);
1332 /* now allow the change */
1333 qemu_chr_fe_set_handlers(&be
, NULL
, NULL
,
1334 NULL
, chardev_change
, NULL
, NULL
, true);
1336 /* has to succeed now */
1337 ret
= qmp_chardev_change("chardev", &backend
, &error_abort
);
1338 g_assert(be
.chr
!= chr
);
1343 /* run the file chardev test */
1344 char_file_test_internal(chr
, filename
);
1346 object_unparent(OBJECT(chr
));
1348 qapi_free_ChardevReturn(ret
);
1356 int main(int argc
, char **argv
)
1358 qemu_init_main_loop(&error_abort
);
1361 g_test_init(&argc
, &argv
, NULL
);
1363 module_call_init(MODULE_INIT_QOM
);
1364 qemu_add_opts(&qemu_chardev_opts
);
1366 g_test_add_func("/char/null", char_null_test
);
1367 g_test_add_func("/char/invalid", char_invalid_test
);
1368 g_test_add_func("/char/ringbuf", char_ringbuf_test
);
1369 g_test_add_func("/char/mux", char_mux_test
);
1371 g_test_add_func("/char/console/subprocess", char_console_test_subprocess
);
1372 g_test_add_func("/char/console", char_console_test
);
1374 g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess
);
1375 g_test_add_func("/char/stdio", char_stdio_test
);
1377 g_test_add_func("/char/pipe", char_pipe_test
);
1379 g_test_add_func("/char/file", char_file_test
);
1381 g_test_add_func("/char/file-fifo", char_file_fifo_test
);
1384 SocketAddress tcpaddr
= {
1385 .type
= SOCKET_ADDRESS_TYPE_INET
,
1386 .u
.inet
.host
= (char *)"127.0.0.1",
1387 .u
.inet
.port
= (char *)"0",
1390 SocketAddress unixaddr
= {
1391 .type
= SOCKET_ADDRESS_TYPE_UNIX
,
1392 .u
.q_unix
.path
= (char *)"test-char.sock",
1396 #define SOCKET_SERVER_TEST(name, addr) \
1397 CharSocketServerTestConfig server1 ## name = \
1398 { addr, false, false }; \
1399 CharSocketServerTestConfig server2 ## name = \
1400 { addr, true, false }; \
1401 CharSocketServerTestConfig server3 ## name = \
1402 { addr, false, true }; \
1403 CharSocketServerTestConfig server4 ## name = \
1404 { addr, true, true }; \
1405 g_test_add_data_func("/char/socket/server/mainloop/" # name, \
1406 &server1 ##name, char_socket_server_test); \
1407 g_test_add_data_func("/char/socket/server/wait-conn/" # name, \
1408 &server2 ##name, char_socket_server_test); \
1409 g_test_add_data_func("/char/socket/server/mainloop-fdpass/" # name, \
1410 &server3 ##name, char_socket_server_test); \
1411 g_test_add_data_func("/char/socket/server/wait-conn-fdpass/" # name, \
1412 &server4 ##name, char_socket_server_test)
1414 #define SOCKET_CLIENT_TEST(name, addr) \
1415 CharSocketClientTestConfig client1 ## name = \
1416 { addr, NULL, false, false }; \
1417 CharSocketClientTestConfig client2 ## name = \
1418 { addr, NULL, true, false }; \
1419 CharSocketClientTestConfig client3 ## name = \
1420 { addr, ",reconnect=1", false }; \
1421 CharSocketClientTestConfig client4 ## name = \
1422 { addr, ",reconnect=1", true }; \
1423 CharSocketClientTestConfig client5 ## name = \
1424 { addr, NULL, false, true }; \
1425 CharSocketClientTestConfig client6 ## name = \
1426 { addr, NULL, true, true }; \
1427 g_test_add_data_func("/char/socket/client/mainloop/" # name, \
1428 &client1 ##name, char_socket_client_test); \
1429 g_test_add_data_func("/char/socket/client/wait-conn/" # name, \
1430 &client2 ##name, char_socket_client_test); \
1431 g_test_add_data_func("/char/socket/client/mainloop-reconnect/" # name, \
1432 &client3 ##name, char_socket_client_test); \
1433 g_test_add_data_func("/char/socket/client/wait-conn-reconnect/" # name, \
1434 &client4 ##name, char_socket_client_test); \
1435 g_test_add_data_func("/char/socket/client/mainloop-fdpass/" # name, \
1436 &client5 ##name, char_socket_client_test); \
1437 g_test_add_data_func("/char/socket/client/wait-conn-fdpass/" # name, \
1438 &client6 ##name, char_socket_client_test)
1440 SOCKET_SERVER_TEST(tcp
, &tcpaddr
);
1441 SOCKET_CLIENT_TEST(tcp
, &tcpaddr
);
1442 g_test_add_data_func("/char/socket/server/two-clients/tcp", &tcpaddr
,
1443 char_socket_server_two_clients_test
);
1445 SOCKET_SERVER_TEST(unix
, &unixaddr
);
1446 SOCKET_CLIENT_TEST(unix
, &unixaddr
);
1447 g_test_add_data_func("/char/socket/server/two-clients/unix", &unixaddr
,
1448 char_socket_server_two_clients_test
);
1451 g_test_add_func("/char/udp", char_udp_test
);
1452 #ifdef HAVE_CHARDEV_SERIAL
1453 g_test_add_func("/char/serial", char_serial_test
);
1455 g_test_add_func("/char/hotswap", char_hotswap_test
);
1456 g_test_add_func("/char/websocket", char_websock_test
);
1458 return g_test_run();