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};
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 #ifdef HAVE_CHARDEV_SERIAL
1008 static void char_serial_test(void)
1013 opts
= qemu_opts_create(qemu_find_opts("chardev"), "serial-id",
1015 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
1016 qemu_opt_set(opts
, "path", "/dev/null", &error_abort
);
1018 chr
= qemu_chr_new_from_opts(opts
, NULL
, NULL
);
1019 g_assert_nonnull(chr
);
1020 /* TODO: add more tests with a pty */
1021 object_unparent(OBJECT(chr
));
1023 /* test tty alias */
1024 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
1025 chr
= qemu_chr_new_from_opts(opts
, NULL
, NULL
);
1026 g_assert_nonnull(chr
);
1027 object_unparent(OBJECT(chr
));
1029 qemu_opts_del(opts
);
1034 static void char_file_fifo_test(void)
1038 char *tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
1039 char *fifo
= g_build_filename(tmp_path
, "fifo", NULL
);
1040 char *out
= g_build_filename(tmp_path
, "out", NULL
);
1041 ChardevFile file
= { .in
= fifo
,
1044 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
1045 .u
.file
.data
= &file
};
1046 FeHandler fe
= { 0, };
1049 if (mkfifo(fifo
, 0600) < 0) {
1053 fd
= open(fifo
, O_RDWR
);
1054 ret
= write(fd
, "fifo-in", 8);
1055 g_assert_cmpint(ret
, ==, 8);
1057 chr
= qemu_chardev_new("label-file", TYPE_CHARDEV_FILE
, &backend
,
1058 NULL
, &error_abort
);
1060 qemu_chr_fe_init(&be
, chr
, &error_abort
);
1061 qemu_chr_fe_set_handlers(&be
,
1068 g_assert_cmpint(fe
.last_event
, !=, CHR_EVENT_BREAK
);
1069 qmp_chardev_send_break("label-foo", NULL
);
1070 g_assert_cmpint(fe
.last_event
, !=, CHR_EVENT_BREAK
);
1071 qmp_chardev_send_break("label-file", NULL
);
1072 g_assert_cmpint(fe
.last_event
, ==, CHR_EVENT_BREAK
);
1078 g_assert_cmpint(fe
.read_count
, ==, 8);
1079 g_assert_cmpstr(fe
.read_buf
, ==, "fifo-in");
1081 qemu_chr_fe_deinit(&be
, true);
1092 static void char_file_test_internal(Chardev
*ext_chr
, const char *filepath
)
1094 char *tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
1097 char *contents
= NULL
;
1098 ChardevFile file
= {};
1099 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
1100 .u
.file
.data
= &file
};
1106 out
= g_strdup(filepath
);
1109 out
= g_build_filename(tmp_path
, "out", NULL
);
1111 chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_FILE
, &backend
,
1112 NULL
, &error_abort
);
1114 ret
= qemu_chr_write_all(chr
, (uint8_t *)"hello!", 6);
1115 g_assert_cmpint(ret
, ==, 6);
1117 ret
= g_file_get_contents(out
, &contents
, &length
, NULL
);
1118 g_assert(ret
== TRUE
);
1119 g_assert_cmpint(length
, ==, 6);
1120 g_assert(strncmp(contents
, "hello!", 6) == 0);
1123 object_unref(OBJECT(chr
));
1132 static void char_file_test(void)
1134 char_file_test_internal(NULL
, NULL
);
1137 static void char_null_test(void)
1144 chr
= qemu_chr_find("label-null");
1147 chr
= qemu_chr_new("label-null", "null", NULL
);
1148 chr
= qemu_chr_find("label-null");
1149 g_assert_nonnull(chr
);
1151 g_assert(qemu_chr_has_feature(chr
,
1152 QEMU_CHAR_FEATURE_FD_PASS
) == false);
1153 g_assert(qemu_chr_has_feature(chr
,
1154 QEMU_CHAR_FEATURE_RECONNECTABLE
) == false);
1156 /* check max avail */
1157 qemu_chr_fe_init(&be
, chr
, &error_abort
);
1158 qemu_chr_fe_init(&be
, chr
, &err
);
1159 error_free_or_abort(&err
);
1161 /* deinit & reinit */
1162 qemu_chr_fe_deinit(&be
, false);
1163 qemu_chr_fe_init(&be
, chr
, &error_abort
);
1165 qemu_chr_fe_set_open(&be
, true);
1167 qemu_chr_fe_set_handlers(&be
,
1174 ret
= qemu_chr_fe_write(&be
, (void *)"buf", 4);
1175 g_assert_cmpint(ret
, ==, 4);
1177 qemu_chr_fe_deinit(&be
, true);
1180 static void char_invalid_test(void)
1183 g_setenv("QTEST_SILENT_ERRORS", "1", 1);
1184 chr
= qemu_chr_new("label-invalid", "invalid", NULL
);
1186 g_unsetenv("QTEST_SILENT_ERRORS");
1189 static int chardev_change(void *opaque
)
1194 static int chardev_change_denied(void *opaque
)
1199 static void char_hotswap_test(void)
1205 gchar
*tmp_path
= g_dir_make_tmp("qemu-test-char.XXXXXX", NULL
);
1206 char *filename
= g_build_filename(tmp_path
, "file", NULL
);
1207 ChardevFile file
= { .out
= filename
};
1208 ChardevBackend backend
= { .type
= CHARDEV_BACKEND_KIND_FILE
,
1209 .u
.file
.data
= &file
};
1213 int sock
= make_udp_socket(&port
);
1214 g_assert_cmpint(sock
, >, 0);
1216 chr_args
= g_strdup_printf("udp:127.0.0.1:%d", port
);
1218 chr
= qemu_chr_new("chardev", chr_args
, NULL
);
1219 qemu_chr_fe_init(&be
, chr
, &error_abort
);
1221 /* check that chardev operates correctly */
1222 char_udp_test_internal(chr
, sock
);
1224 /* set the handler that denies the hotswap */
1225 qemu_chr_fe_set_handlers(&be
, NULL
, NULL
,
1226 NULL
, chardev_change_denied
, NULL
, NULL
, true);
1228 /* now, change is denied and has to keep the old backend operating */
1229 ret
= qmp_chardev_change("chardev", &backend
, NULL
);
1231 g_assert(be
.chr
== chr
);
1233 char_udp_test_internal(chr
, sock
);
1235 /* now allow the change */
1236 qemu_chr_fe_set_handlers(&be
, NULL
, NULL
,
1237 NULL
, chardev_change
, NULL
, NULL
, true);
1239 /* has to succeed now */
1240 ret
= qmp_chardev_change("chardev", &backend
, &error_abort
);
1241 g_assert(be
.chr
!= chr
);
1246 /* run the file chardev test */
1247 char_file_test_internal(chr
, filename
);
1249 object_unparent(OBJECT(chr
));
1251 qapi_free_ChardevReturn(ret
);
1259 int main(int argc
, char **argv
)
1261 qemu_init_main_loop(&error_abort
);
1264 g_test_init(&argc
, &argv
, NULL
);
1266 module_call_init(MODULE_INIT_QOM
);
1267 qemu_add_opts(&qemu_chardev_opts
);
1269 g_test_add_func("/char/null", char_null_test
);
1270 g_test_add_func("/char/invalid", char_invalid_test
);
1271 g_test_add_func("/char/ringbuf", char_ringbuf_test
);
1272 g_test_add_func("/char/mux", char_mux_test
);
1274 g_test_add_func("/char/console/subprocess", char_console_test_subprocess
);
1275 g_test_add_func("/char/console", char_console_test
);
1277 g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess
);
1278 g_test_add_func("/char/stdio", char_stdio_test
);
1280 g_test_add_func("/char/pipe", char_pipe_test
);
1282 g_test_add_func("/char/file", char_file_test
);
1284 g_test_add_func("/char/file-fifo", char_file_fifo_test
);
1287 SocketAddress tcpaddr
= {
1288 .type
= SOCKET_ADDRESS_TYPE_INET
,
1289 .u
.inet
.host
= (char *)"127.0.0.1",
1290 .u
.inet
.port
= (char *)"0",
1293 SocketAddress unixaddr
= {
1294 .type
= SOCKET_ADDRESS_TYPE_UNIX
,
1295 .u
.q_unix
.path
= (char *)"test-char.sock",
1299 #define SOCKET_SERVER_TEST(name, addr) \
1300 CharSocketServerTestConfig server1 ## name = \
1301 { addr, false, false }; \
1302 CharSocketServerTestConfig server2 ## name = \
1303 { addr, true, false }; \
1304 CharSocketServerTestConfig server3 ## name = \
1305 { addr, false, true }; \
1306 CharSocketServerTestConfig server4 ## name = \
1307 { addr, true, true }; \
1308 g_test_add_data_func("/char/socket/server/mainloop/" # name, \
1309 &server1 ##name, char_socket_server_test); \
1310 g_test_add_data_func("/char/socket/server/wait-conn/" # name, \
1311 &server2 ##name, char_socket_server_test); \
1312 g_test_add_data_func("/char/socket/server/mainloop-fdpass/" # name, \
1313 &server3 ##name, char_socket_server_test); \
1314 g_test_add_data_func("/char/socket/server/wait-conn-fdpass/" # name, \
1315 &server4 ##name, char_socket_server_test)
1317 #define SOCKET_CLIENT_TEST(name, addr) \
1318 CharSocketClientTestConfig client1 ## name = \
1319 { addr, NULL, false, false }; \
1320 CharSocketClientTestConfig client2 ## name = \
1321 { addr, NULL, true, false }; \
1322 CharSocketClientTestConfig client3 ## name = \
1323 { addr, ",reconnect=1", false }; \
1324 CharSocketClientTestConfig client4 ## name = \
1325 { addr, ",reconnect=1", true }; \
1326 CharSocketClientTestConfig client5 ## name = \
1327 { addr, NULL, false, true }; \
1328 CharSocketClientTestConfig client6 ## name = \
1329 { addr, NULL, true, true }; \
1330 g_test_add_data_func("/char/socket/client/mainloop/" # name, \
1331 &client1 ##name, char_socket_client_test); \
1332 g_test_add_data_func("/char/socket/client/wait-conn/" # name, \
1333 &client2 ##name, char_socket_client_test); \
1334 g_test_add_data_func("/char/socket/client/mainloop-reconnect/" # name, \
1335 &client3 ##name, char_socket_client_test); \
1336 g_test_add_data_func("/char/socket/client/wait-conn-reconnect/" # name, \
1337 &client4 ##name, char_socket_client_test); \
1338 g_test_add_data_func("/char/socket/client/mainloop-fdpass/" # name, \
1339 &client5 ##name, char_socket_client_test); \
1340 g_test_add_data_func("/char/socket/client/wait-conn-fdpass/" # name, \
1341 &client6 ##name, char_socket_client_test)
1343 SOCKET_SERVER_TEST(tcp
, &tcpaddr
);
1344 SOCKET_CLIENT_TEST(tcp
, &tcpaddr
);
1346 SOCKET_SERVER_TEST(unix
, &unixaddr
);
1347 SOCKET_CLIENT_TEST(unix
, &unixaddr
);
1351 g_test_add_func("/char/udp", char_udp_test
);
1352 #ifdef HAVE_CHARDEV_SERIAL
1353 g_test_add_func("/char/serial", char_serial_test
);
1355 g_test_add_func("/char/hotswap", char_hotswap_test
);
1356 g_test_add_func("/char/websocket", char_websock_test
);
1358 return g_test_run();