1 #include "qemu/osdep.h"
2 #include <glib/gstdio.h>
4 #include "qemu/config-file.h"
5 #include "qemu/module.h"
6 #include "qemu/option.h"
7 #include "qemu/sockets.h"
8 #include "chardev/char-fe.h"
9 #include "chardev/char-mux.h"
10 #include "sysemu/sysemu.h"
11 #include "qapi/error.h"
12 #include "qapi/qapi-commands-char.h"
13 #include "qapi/qmp/qdict.h"
14 #include "qom/qom-qobject.h"
15 #include "io/channel-socket.h"
16 #include "qapi/qobject-input-visitor.h"
17 #include "qapi/qapi-visit-sockets.h"
18 #include "socket-helpers.h"
22 typedef struct FeHandler {
26 bool openclose_mismatch;
31 static void main_loop(void)
35 main_loop_wait(false);
39 static int fe_can_read(void *opaque)
41 FeHandler *h = opaque;
43 return sizeof(h->read_buf) - h->read_count;
46 static void fe_read(void *opaque, const uint8_t *buf, int size)
48 FeHandler *h = opaque;
50 g_assert_cmpint(size, <=, fe_can_read(opaque));
52 memcpy(h->read_buf + h->read_count, buf, size);
53 h->read_count += size;
57 static void fe_event(void *opaque, int event)
59 FeHandler *h = opaque;
62 h->last_event = event;
66 case CHR_EVENT_OPENED:
67 case CHR_EVENT_CLOSED:
69 new_open_state = (event == CHR_EVENT_OPENED);
70 if (h->is_open == new_open_state) {
71 h->openclose_mismatch = true;
73 h->is_open = new_open_state;
82 static void char_console_test_subprocess(void)
87 opts = qemu_opts_create(qemu_find_opts("chardev"), "console-label",
89 qemu_opt_set(opts, "backend", "console", &error_abort);
91 chr = qemu_chr_new_from_opts(opts, NULL, NULL);
92 g_assert_nonnull(chr);
94 qemu_chr_write_all(chr, (const uint8_t *)"CONSOLE", 7);
97 object_unparent(OBJECT(chr));
100 static void char_console_test(void)
102 g_test_trap_subprocess("/char/console/subprocess", 0, 0);
103 g_test_trap_assert_passed();
104 g_test_trap_assert_stdout("CONSOLE");
107 static void char_stdio_test_subprocess(void)
113 chr = qemu_chr_new("label", "stdio", NULL);
114 g_assert_nonnull(chr);
116 qemu_chr_fe_init(&be, chr, &error_abort);
117 qemu_chr_fe_set_open(&be, true);
118 ret = qemu_chr_fe_write(&be, (void *)"buf", 4);
119 g_assert_cmpint(ret, ==, 4);
121 qemu_chr_fe_deinit(&be, true);
124 static void char_stdio_test(void)
126 g_test_trap_subprocess("/char/stdio/subprocess", 0, 0);
127 g_test_trap_assert_passed();
128 g_test_trap_assert_stdout("buf");
131 static void char_ringbuf_test(void)
139 opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
141 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
143 qemu_opt_set(opts, "size", "5", &error_abort);
144 chr = qemu_chr_new_from_opts(opts, NULL, NULL);
148 opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
150 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
151 qemu_opt_set(opts, "size", "2", &error_abort);
152 chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
153 g_assert_nonnull(chr);
156 qemu_chr_fe_init(&be, chr, &error_abort);
157 ret = qemu_chr_fe_write(&be, (void *)"buff", 4);
158 g_assert_cmpint(ret, ==, 4);
160 data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
161 g_assert_cmpstr(data, ==, "ff");
164 data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
165 g_assert_cmpstr(data, ==, "");
168 qemu_chr_fe_deinit(&be, true);
171 opts = qemu_opts_create(qemu_find_opts("chardev"), "memory-label",
173 qemu_opt_set(opts, "backend", "memory", &error_abort);
174 qemu_opt_set(opts, "size", "2", &error_abort);
175 chr = qemu_chr_new_from_opts(opts, NULL, NULL);
176 g_assert_nonnull(chr);
177 object_unparent(OBJECT(chr));
181 static void char_mux_test(void)
186 FeHandler h1 = { 0, false, 0, false, }, h2 = { 0, false, 0, false, };
187 CharBackend chr_be1, chr_be2;
189 opts = qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
191 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
192 qemu_opt_set(opts, "size", "128", &error_abort);
193 qemu_opt_set(opts, "mux", "on", &error_abort);
194 chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
195 g_assert_nonnull(chr);
198 qemu_chr_fe_init(&chr_be1, chr, &error_abort);
199 qemu_chr_fe_set_handlers(&chr_be1,
207 qemu_chr_fe_init(&chr_be2, chr, &error_abort);
208 qemu_chr_fe_set_handlers(&chr_be2,
215 qemu_chr_fe_take_focus(&chr_be2);
217 base = qemu_chr_find("mux-label-base");
218 g_assert_cmpint(qemu_chr_be_can_write(base), !=, 0);
220 qemu_chr_be_write(base, (void *)"hello", 6);
221 g_assert_cmpint(h1.read_count, ==, 0);
222 g_assert_cmpint(h2.read_count, ==, 6);
223 g_assert_cmpstr(h2.read_buf, ==, "hello");
226 g_assert_cmpint(h1.last_event, !=, 42); /* should be MUX_OUT or OPENED */
227 g_assert_cmpint(h2.last_event, !=, 42); /* should be MUX_IN or OPENED */
228 /* sending event on the base broadcast to all fe, historical reasons? */
229 qemu_chr_be_event(base, 42);
230 g_assert_cmpint(h1.last_event, ==, 42);
231 g_assert_cmpint(h2.last_event, ==, 42);
232 qemu_chr_be_event(chr, -1);
233 g_assert_cmpint(h1.last_event, ==, 42);
234 g_assert_cmpint(h2.last_event, ==, -1);
237 qemu_chr_be_write(base, (void *)"\1b", 2);
238 g_assert_cmpint(h1.last_event, ==, 42);
239 g_assert_cmpint(h2.last_event, ==, CHR_EVENT_BREAK);
241 qemu_chr_be_write(base, (void *)"\1c", 2);
242 g_assert_cmpint(h1.last_event, ==, CHR_EVENT_MUX_IN);
243 g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT);
244 qemu_chr_be_event(chr, -1);
245 g_assert_cmpint(h1.last_event, ==, -1);
246 g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT);
248 qemu_chr_be_write(base, (void *)"hello", 6);
249 g_assert_cmpint(h2.read_count, ==, 0);
250 g_assert_cmpint(h1.read_count, ==, 6);
251 g_assert_cmpstr(h1.read_buf, ==, "hello");
254 qemu_chr_be_write(base, (void *)"\1b", 2);
255 g_assert_cmpint(h1.last_event, ==, CHR_EVENT_BREAK);
256 g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT);
258 /* open/close state and corresponding events */
259 g_assert_true(qemu_chr_fe_backend_open(&chr_be1));
260 g_assert_true(qemu_chr_fe_backend_open(&chr_be2));
261 g_assert_true(h1.is_open);
262 g_assert_false(h1.openclose_mismatch);
263 g_assert_true(h2.is_open);
264 g_assert_false(h2.openclose_mismatch);
266 h1.openclose_count = h2.openclose_count = 0;
268 qemu_chr_fe_set_handlers(&chr_be1, NULL, NULL, NULL, NULL,
270 qemu_chr_fe_set_handlers(&chr_be2, NULL, NULL, NULL, NULL,
272 g_assert_cmpint(h1.openclose_count, ==, 0);
273 g_assert_cmpint(h2.openclose_count, ==, 0);
275 h1.is_open = h2.is_open = false;
276 qemu_chr_fe_set_handlers(&chr_be1,
283 qemu_chr_fe_set_handlers(&chr_be2,
290 g_assert_cmpint(h1.openclose_count, ==, 1);
291 g_assert_false(h1.openclose_mismatch);
292 g_assert_cmpint(h2.openclose_count, ==, 1);
293 g_assert_false(h2.openclose_mismatch);
295 qemu_chr_be_event(base, CHR_EVENT_CLOSED);
296 qemu_chr_be_event(base, CHR_EVENT_OPENED);
297 g_assert_cmpint(h1.openclose_count, ==, 3);
298 g_assert_false(h1.openclose_mismatch);
299 g_assert_cmpint(h2.openclose_count, ==, 3);
300 g_assert_false(h2.openclose_mismatch);
302 qemu_chr_fe_set_handlers(&chr_be2,
309 qemu_chr_fe_set_handlers(&chr_be1,
317 /* remove first handler */
318 qemu_chr_fe_set_handlers(&chr_be1, NULL, NULL, NULL, NULL,
320 qemu_chr_be_write(base, (void *)"hello", 6);
321 g_assert_cmpint(h1.read_count, ==, 0);
322 g_assert_cmpint(h2.read_count, ==, 0);
324 qemu_chr_be_write(base, (void *)"\1c", 2);
325 qemu_chr_be_write(base, (void *)"hello", 6);
326 g_assert_cmpint(h1.read_count, ==, 0);
327 g_assert_cmpint(h2.read_count, ==, 6);
328 g_assert_cmpstr(h2.read_buf, ==, "hello");
332 qemu_chr_be_write(base, (void *)"\1?", 2);
333 data = qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort);
334 g_assert_cmpint(strlen(data), !=, 0);
337 qemu_chr_fe_deinit(&chr_be1, false);
338 qemu_chr_fe_deinit(&chr_be2, true);
342 static void websock_server_read(void *opaque, const uint8_t *buf, int size)
344 g_assert_cmpint(size, ==, 5);
345 g_assert(memcmp(buf, "world", size) == 0);
350 static int websock_server_can_read(void *opaque)
356 static bool websock_check_http_headers(char *buf, int size)
359 const char *ans[] = { "HTTP/1.1 101 Switching Protocols\r\n",
360 "Server: QEMU VNC\r\n",
361 "Upgrade: websocket\r\n",
362 "Connection: Upgrade\r\n",
363 "Sec-WebSocket-Accept:",
364 "Sec-WebSocket-Protocol: binary\r\n" };
366 for (i = 0; i < 6; i++) {
367 if (g_strstr_len(buf, size, ans[i]) == NULL) {
376 static void websock_client_read(void *opaque, const uint8_t *buf, int size)
378 const uint8_t ping[] = { 0x89, 0x85, /* Ping header */
379 0x07, 0x77, 0x9e, 0xf9, /* Masking key */
380 0x6f, 0x12, 0xf2, 0x95, 0x68 /* "hello" */ };
382 const uint8_t binary[] = { 0x82, 0x85, /* Binary header */
383 0x74, 0x90, 0xb9, 0xdf, /* Masking key */
384 0x03, 0xff, 0xcb, 0xb3, 0x10 /* "world" */ };
385 Chardev *chr_client = opaque;
387 if (websock_check_http_headers((char *) buf, size)) {
388 qemu_chr_fe_write(chr_client->be, ping, sizeof(ping));
389 } else if (buf[0] == 0x8a && buf[1] == 0x05) {
390 g_assert(strncmp((char *) buf + 2, "hello", 5) == 0);
391 qemu_chr_fe_write(chr_client->be, binary, sizeof(binary));
393 g_assert(buf[0] == 0x88 && buf[1] == 0x16);
394 g_assert(strncmp((char *) buf + 4, "peer requested close", 10) == 0);
400 static int websock_client_can_read(void *opaque)
406 static void char_websock_test(void)
412 char *handshake_port;
414 CharBackend client_be;
416 Chardev *chr = qemu_chr_new("server",
417 "websocket:127.0.0.1:0,server,nowait", NULL);
418 const char handshake[] = "GET / HTTP/1.1\r\n"
419 "Upgrade: websocket\r\n"
420 "Connection: Upgrade\r\n"
421 "Host: localhost:%s\r\n"
422 "Origin: http://localhost:%s\r\n"
423 "Sec-WebSocket-Key: o9JHNiS3/0/0zYE1wa3yIw==\r\n"
424 "Sec-WebSocket-Version: 13\r\n"
425 "Sec-WebSocket-Protocol: binary\r\n\r\n";
426 const uint8_t close[] = { 0x88, 0x82, /* Close header */
427 0xef, 0xaa, 0xc5, 0x97, /* Masking key */
428 0xec, 0x42 /* Status code */ };
430 addr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
431 qdict = qobject_to(QDict, addr);
432 port = qdict_get_str(qdict, "port");
433 tmp = g_strdup_printf("tcp:127.0.0.1:%s", port);
434 handshake_port = g_strdup_printf(handshake, port, port);
435 qobject_unref(qdict);
437 qemu_chr_fe_init(&be, chr, &error_abort);
438 qemu_chr_fe_set_handlers(&be, websock_server_can_read, websock_server_read,
439 NULL, NULL, chr, NULL, true);
441 chr_client = qemu_chr_new("client", tmp, NULL);
442 qemu_chr_fe_init(&client_be, chr_client, &error_abort);
443 qemu_chr_fe_set_handlers(&client_be, websock_client_can_read,
445 NULL, NULL, chr_client, NULL, true);
448 qemu_chr_write_all(chr_client,
449 (uint8_t *) handshake_port,
450 strlen(handshake_port));
451 g_free(handshake_port);
454 g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
455 g_assert(object_property_get_bool(OBJECT(chr_client),
456 "connected", &error_abort));
458 qemu_chr_write_all(chr_client, close, sizeof(close));
461 object_unparent(OBJECT(chr_client));
462 object_unparent(OBJECT(chr));
467 static void char_pipe_test(void)
469 gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
470 gchar *tmp, *in, *out, *pipe = g_build_filename(tmp_path, "pipe", NULL);
475 FeHandler fe = { 0, };
477 in = g_strdup_printf("%s.in", pipe);
478 if (mkfifo(in, 0600) < 0) {
481 out = g_strdup_printf("%s.out", pipe);
482 if (mkfifo(out, 0600) < 0) {
486 tmp = g_strdup_printf("pipe:%s", pipe);
487 chr = qemu_chr_new("pipe", tmp, NULL);
488 g_assert_nonnull(chr);
491 qemu_chr_fe_init(&be, chr, &error_abort);
493 ret = qemu_chr_fe_write(&be, (void *)"pipe-out", 9);
494 g_assert_cmpint(ret, ==, 9);
496 fd = open(out, O_RDWR);
497 ret = read(fd, buf, sizeof(buf));
498 g_assert_cmpint(ret, ==, 9);
499 g_assert_cmpstr(buf, ==, "pipe-out");
502 fd = open(in, O_WRONLY);
503 ret = write(fd, "pipe-in", 8);
504 g_assert_cmpint(ret, ==, 8);
507 qemu_chr_fe_set_handlers(&be,
517 g_assert_cmpint(fe.read_count, ==, 8);
518 g_assert_cmpstr(fe.read_buf, ==, "pipe-in");
520 qemu_chr_fe_deinit(&be, true);
522 g_assert(g_unlink(in) == 0);
523 g_assert(g_unlink(out) == 0);
524 g_assert(g_rmdir(tmp_path) == 0);
532 typedef struct SocketIdleData {
537 CharBackend *client_be;
541 static void socket_read_hello(void *opaque, const uint8_t *buf, int size)
543 g_assert_cmpint(size, ==, 5);
544 g_assert(strncmp((char *)buf, "hello", 5) == 0);
549 static int socket_can_read_hello(void *opaque)
554 static int make_udp_socket(int *port)
556 struct sockaddr_in addr = { 0, };
557 socklen_t alen = sizeof(addr);
558 int ret, sock = qemu_socket(PF_INET, SOCK_DGRAM, 0);
560 g_assert_cmpint(sock, >, 0);
561 addr.sin_family = AF_INET ;
562 addr.sin_addr.s_addr = htonl(INADDR_ANY);
564 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
565 g_assert_cmpint(ret, ==, 0);
566 ret = getsockname(sock, (struct sockaddr *)&addr, &alen);
567 g_assert_cmpint(ret, ==, 0);
569 *port = ntohs(addr.sin_port);
573 static void char_udp_test_internal(Chardev *reuse_chr, int sock)
575 struct sockaddr_in other;
576 SocketIdleData d = { 0, };
579 socklen_t alen = sizeof(other);
589 sock = make_udp_socket(&port);
590 tmp = g_strdup_printf("udp:127.0.0.1:%d", port);
591 chr = qemu_chr_new("client", tmp, NULL);
592 g_assert_nonnull(chr);
594 be = g_alloca(sizeof(CharBackend));
595 qemu_chr_fe_init(be, chr, &error_abort);
599 qemu_chr_fe_set_handlers(be, socket_can_read_hello, socket_read_hello,
600 NULL, NULL, &d, NULL, true);
601 ret = qemu_chr_write_all(chr, (uint8_t *)"hello", 5);
602 g_assert_cmpint(ret, ==, 5);
604 ret = recvfrom(sock, buf, sizeof(buf), 0,
605 (struct sockaddr *)&other, &alen);
606 g_assert_cmpint(ret, ==, 5);
607 ret = sendto(sock, buf, 5, 0, (struct sockaddr *)&other, alen);
608 g_assert_cmpint(ret, ==, 5);
614 qemu_chr_fe_deinit(be, true);
619 static void char_udp_test(void)
621 char_udp_test_internal(NULL, 0);
628 } CharSocketTestData;
631 #define SOCKET_PING "Hello"
632 #define SOCKET_PONG "World"
636 char_socket_event(void *opaque, int event)
638 CharSocketTestData *data = opaque;
644 char_socket_read(void *opaque, const uint8_t *buf, int size)
646 CharSocketTestData *data = opaque;
647 g_assert_cmpint(size, ==, sizeof(SOCKET_PONG));
648 g_assert(memcmp(buf, SOCKET_PONG, size) == 0);
649 data->got_pong = true;
654 char_socket_can_read(void *opaque)
656 return sizeof(SOCKET_PONG);
661 char_socket_addr_to_opt_str(SocketAddress *addr, bool fd_pass,
662 const char *reconnect, bool is_listen)
665 QIOChannelSocket *ioc = qio_channel_socket_new();
668 g_assert(!reconnect);
670 qio_channel_socket_listen_sync(ioc, addr, 1, &error_abort);
672 qio_channel_socket_connect_sync(ioc, addr, &error_abort);
676 optstr = g_strdup_printf("socket,id=cdev0,fd=%d%s",
677 fd, is_listen ? ",server,nowait" : "");
678 object_unref(OBJECT(ioc));
681 switch (addr->type) {
682 case SOCKET_ADDRESS_TYPE_INET:
683 return g_strdup_printf("socket,id=cdev0,host=%s,port=%s%s%s",
686 reconnect ? reconnect : "",
687 is_listen ? ",server,nowait" : "");
689 case SOCKET_ADDRESS_TYPE_UNIX:
690 return g_strdup_printf("socket,id=cdev0,path=%s%s%s",
692 reconnect ? reconnect : "",
693 is_listen ? ",server,nowait" : "");
696 g_assert_not_reached();
703 char_socket_ping_pong(QIOChannel *ioc)
705 char greeting[sizeof(SOCKET_PING)];
706 const char *response = SOCKET_PONG;
708 qio_channel_read_all(ioc, greeting, sizeof(greeting), &error_abort);
710 g_assert(memcmp(greeting, SOCKET_PING, sizeof(greeting)) == 0);
712 qio_channel_write_all(ioc, response, sizeof(SOCKET_PONG), &error_abort);
714 object_unref(OBJECT(ioc));
719 char_socket_server_client_thread(gpointer data)
721 SocketAddress *addr = data;
722 QIOChannelSocket *ioc = qio_channel_socket_new();
724 qio_channel_socket_connect_sync(ioc, addr, &error_abort);
726 char_socket_ping_pong(QIO_CHANNEL(ioc));
736 } CharSocketServerTestConfig;
739 static void char_socket_server_test(gconstpointer opaque)
741 const CharSocketServerTestConfig *config = opaque;
743 CharBackend be = {0};
744 CharSocketTestData data = {0};
750 bool reconnected = false;
754 g_setenv("QTEST_SILENT_ERRORS", "1", 1);
756 * We rely on config->addr containing "nowait", otherwise
757 * qemu_chr_new() will block until a client connects. We
758 * can't spawn our client thread though, because until
759 * qemu_chr_new() returns we don't know what TCP port was
760 * allocated by the OS
762 optstr = char_socket_addr_to_opt_str(config->addr,
766 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
768 g_assert_nonnull(opts);
769 chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
771 g_assert_nonnull(chr);
772 g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
774 qaddr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
775 g_assert_nonnull(qaddr);
777 v = qobject_input_visitor_new(qaddr);
778 visit_type_SocketAddress(v, "addr", &addr, &error_abort);
780 qobject_unref(qaddr);
782 qemu_chr_fe_init(&be, chr, &error_abort);
786 qemu_chr_fe_set_handlers(&be, NULL, NULL,
787 char_socket_event, NULL,
789 g_assert(data.event == -1);
792 * Kick off a thread to act as the "remote" client
793 * which just plays ping-pong with us
795 qemu_thread_create(&thread, "client",
796 char_socket_server_client_thread,
797 addr, QEMU_THREAD_JOINABLE);
798 g_assert(data.event == -1);
800 if (config->wait_connected) {
801 /* Synchronously accept a connection */
802 qemu_chr_wait_connected(chr, &error_abort);
805 * Asynchronously accept a connection when the evnt
806 * loop reports the listener socket as readable
808 while (data.event == -1) {
809 main_loop_wait(false);
812 g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
813 g_assert(data.event == CHR_EVENT_OPENED);
816 /* Send a greeting to the client */
817 ret = qemu_chr_fe_write_all(&be, (const uint8_t *)SOCKET_PING,
818 sizeof(SOCKET_PING));
819 g_assert_cmpint(ret, ==, sizeof(SOCKET_PING));
820 g_assert(data.event == -1);
822 /* Setup a callback to receive the reply to our greeting */
823 qemu_chr_fe_set_handlers(&be, char_socket_can_read,
825 char_socket_event, NULL,
827 g_assert(data.event == CHR_EVENT_OPENED);
830 /* Wait for the client to go away */
831 while (data.event == -1) {
832 main_loop_wait(false);
834 g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
835 g_assert(data.event == CHR_EVENT_CLOSED);
836 g_assert(data.got_pong);
838 qemu_thread_join(&thread);
845 qapi_free_SocketAddress(addr);
846 object_unparent(OBJECT(chr));
848 g_unsetenv("QTEST_SILENT_ERRORS");
853 char_socket_client_server_thread(gpointer data)
855 QIOChannelSocket *ioc = data;
856 QIOChannelSocket *cioc;
858 cioc = qio_channel_socket_accept(ioc, &error_abort);
859 g_assert_nonnull(cioc);
861 char_socket_ping_pong(QIO_CHANNEL(cioc));
869 const char *reconnect;
872 } CharSocketClientTestConfig;
875 static void char_socket_client_test(gconstpointer opaque)
877 const CharSocketClientTestConfig *config = opaque;
878 QIOChannelSocket *ioc;
881 CharBackend be = {0};
882 CharSocketTestData data = {0};
886 bool reconnected = false;
890 * Setup a listener socket and determine get its address
891 * so we know the TCP port for the client later
893 ioc = qio_channel_socket_new();
894 g_assert_nonnull(ioc);
895 qio_channel_socket_listen_sync(ioc, config->addr, 1, &error_abort);
896 addr = qio_channel_socket_get_local_address(ioc, &error_abort);
897 g_assert_nonnull(addr);
900 * Kick off a thread to act as the "remote" client
901 * which just plays ping-pong with us
903 qemu_thread_create(&thread, "client",
904 char_socket_client_server_thread,
905 ioc, QEMU_THREAD_JOINABLE);
908 * Populate the chardev address based on what the server
909 * is actually listening on
911 optstr = char_socket_addr_to_opt_str(addr,
916 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
918 g_assert_nonnull(opts);
919 chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
921 g_assert_nonnull(chr);
923 if (config->reconnect) {
925 * If reconnect is set, the connection will be
926 * established in a background thread and we won't
927 * see the "connected" status updated until we
928 * run the main event loop, or call qemu_chr_wait_connected
930 g_assert(!object_property_get_bool(OBJECT(chr), "connected",
933 g_assert(object_property_get_bool(OBJECT(chr), "connected",
937 qemu_chr_fe_init(&be, chr, &error_abort);
941 qemu_chr_fe_set_handlers(&be, NULL, NULL,
942 char_socket_event, NULL,
944 if (config->reconnect) {
945 g_assert(data.event == -1);
947 g_assert(data.event == CHR_EVENT_OPENED);
950 if (config->wait_connected) {
952 * Synchronously wait for the connection to complete
953 * This should be a no-op if reconnect is not set.
955 qemu_chr_wait_connected(chr, &error_abort);
958 * Asynchronously wait for the connection to be reported
959 * as complete when the background thread reports its
961 * The loop will short-circuit if reconnect was set
963 while (data.event == -1) {
964 main_loop_wait(false);
967 g_assert(data.event == CHR_EVENT_OPENED);
969 g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
971 /* Send a greeting to the server */
972 ret = qemu_chr_fe_write_all(&be, (const uint8_t *)SOCKET_PING,
973 sizeof(SOCKET_PING));
974 g_assert_cmpint(ret, ==, sizeof(SOCKET_PING));
975 g_assert(data.event == -1);
977 /* Setup a callback to receive the reply to our greeting */
978 qemu_chr_fe_set_handlers(&be, char_socket_can_read,
980 char_socket_event, NULL,
982 g_assert(data.event == CHR_EVENT_OPENED);
985 /* Wait for the server to go away */
986 while (data.event == -1) {
987 main_loop_wait(false);
989 g_assert(data.event == CHR_EVENT_CLOSED);
990 g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
991 g_assert(data.got_pong);
992 qemu_thread_join(&thread);
994 if (config->reconnect && !reconnected) {
996 qemu_thread_create(&thread, "client",
997 char_socket_client_server_thread,
998 ioc, QEMU_THREAD_JOINABLE);
1002 object_unref(OBJECT(ioc));
1003 object_unparent(OBJECT(chr));
1004 qapi_free_SocketAddress(addr);
1009 count_closed_event(void *opaque, int event)
1011 int *count = opaque;
1012 if (event == CHR_EVENT_CLOSED) {
1018 char_socket_discard_read(void *opaque, const uint8_t *buf, int size)
1022 static void char_socket_server_two_clients_test(gconstpointer opaque)
1024 SocketAddress *incoming_addr = (gpointer) opaque;
1026 CharBackend be = {0};
1028 SocketAddress *addr;
1032 QIOChannelSocket *ioc1, *ioc2;
1035 g_setenv("QTEST_SILENT_ERRORS", "1", 1);
1037 * We rely on addr containing "nowait", otherwise
1038 * qemu_chr_new() will block until a client connects. We
1039 * can't spawn our client thread though, because until
1040 * qemu_chr_new() returns we don't know what TCP port was
1041 * allocated by the OS
1043 optstr = char_socket_addr_to_opt_str(incoming_addr,
1047 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
1049 g_assert_nonnull(opts);
1050 chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
1051 qemu_opts_del(opts);
1052 g_assert_nonnull(chr);
1053 g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
1055 qaddr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
1056 g_assert_nonnull(qaddr);
1058 v = qobject_input_visitor_new(qaddr);
1059 visit_type_SocketAddress(v, "addr", &addr, &error_abort);
1061 qobject_unref(qaddr);
1063 qemu_chr_fe_init(&be, chr, &error_abort);
1065 qemu_chr_fe_set_handlers(&be, char_socket_can_read, char_socket_discard_read,
1066 count_closed_event, NULL,
1067 &closed, NULL, true);
1069 ioc1 = qio_channel_socket_new();
1070 qio_channel_socket_connect_sync(ioc1, addr, &error_abort);
1071 qemu_chr_wait_connected(chr, &error_abort);
1073 /* switch the chardev to another context */
1074 GMainContext *ctx = g_main_context_new();
1075 qemu_chr_fe_set_handlers(&be, char_socket_can_read, char_socket_discard_read,
1076 count_closed_event, NULL,
1077 &closed, ctx, true);
1079 /* Start a second connection while the first is still connected.
1080 * It will be placed in the listen() backlog, and connect() will
1081 * succeed immediately.
1083 ioc2 = qio_channel_socket_new();
1084 qio_channel_socket_connect_sync(ioc2, addr, &error_abort);
1086 object_unref(OBJECT(ioc1));
1087 /* The two connections should now be processed serially. */
1088 while (g_main_context_iteration(ctx, TRUE)) {
1089 if (closed == 1 && ioc2) {
1090 object_unref(OBJECT(ioc2));
1098 qapi_free_SocketAddress(addr);
1099 object_unparent(OBJECT(chr));
1100 g_main_context_unref(ctx);
1102 g_unsetenv("QTEST_SILENT_ERRORS");
1106 #if defined(HAVE_CHARDEV_SERIAL) && !defined(WIN32)
1107 static void char_serial_test(void)
1112 opts = qemu_opts_create(qemu_find_opts("chardev"), "serial-id",
1114 qemu_opt_set(opts, "backend", "serial", &error_abort);
1115 qemu_opt_set(opts, "path", "/dev/null", &error_abort);
1117 chr = qemu_chr_new_from_opts(opts, NULL, NULL);
1118 g_assert_nonnull(chr);
1119 /* TODO: add more tests with a pty */
1120 object_unparent(OBJECT(chr));
1122 /* test tty alias */
1123 qemu_opt_set(opts, "backend", "tty", &error_abort);
1124 chr = qemu_chr_new_from_opts(opts, NULL, NULL);
1125 g_assert_nonnull(chr);
1126 object_unparent(OBJECT(chr));
1128 qemu_opts_del(opts);
1133 static void char_file_fifo_test(void)
1137 char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
1138 char *fifo = g_build_filename(tmp_path, "fifo", NULL);
1139 char *out = g_build_filename(tmp_path, "out", NULL);
1140 ChardevFile file = { .in = fifo,
1143 ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
1144 .u.file.data = &file };
1145 FeHandler fe = { 0, };
1148 if (mkfifo(fifo, 0600) < 0) {
1152 fd = open(fifo, O_RDWR);
1153 ret = write(fd, "fifo-in", 8);
1154 g_assert_cmpint(ret, ==, 8);
1156 chr = qemu_chardev_new("label-file", TYPE_CHARDEV_FILE, &backend,
1157 NULL, &error_abort);
1159 qemu_chr_fe_init(&be, chr, &error_abort);
1160 qemu_chr_fe_set_handlers(&be,
1167 g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK);
1168 qmp_chardev_send_break("label-foo", NULL);
1169 g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK);
1170 qmp_chardev_send_break("label-file", NULL);
1171 g_assert_cmpint(fe.last_event, ==, CHR_EVENT_BREAK);
1177 g_assert_cmpint(fe.read_count, ==, 8);
1178 g_assert_cmpstr(fe.read_buf, ==, "fifo-in");
1180 qemu_chr_fe_deinit(&be, true);
1191 static void char_file_test_internal(Chardev *ext_chr, const char *filepath)
1193 char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
1196 char *contents = NULL;
1197 ChardevFile file = {};
1198 ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
1199 .u.file.data = &file };
1205 out = g_strdup(filepath);
1208 out = g_build_filename(tmp_path, "out", NULL);
1210 chr = qemu_chardev_new(NULL, TYPE_CHARDEV_FILE, &backend,
1211 NULL, &error_abort);
1213 ret = qemu_chr_write_all(chr, (uint8_t *)"hello!", 6);
1214 g_assert_cmpint(ret, ==, 6);
1216 ret = g_file_get_contents(out, &contents, &length, NULL);
1217 g_assert(ret == TRUE);
1218 g_assert_cmpint(length, ==, 6);
1219 g_assert(strncmp(contents, "hello!", 6) == 0);
1222 object_unref(OBJECT(chr));
1231 static void char_file_test(void)
1233 char_file_test_internal(NULL, NULL);
1236 static void char_null_test(void)
1243 chr = qemu_chr_find("label-null");
1246 chr = qemu_chr_new("label-null", "null", NULL);
1247 chr = qemu_chr_find("label-null");
1248 g_assert_nonnull(chr);
1250 g_assert(qemu_chr_has_feature(chr,
1251 QEMU_CHAR_FEATURE_FD_PASS) == false);
1252 g_assert(qemu_chr_has_feature(chr,
1253 QEMU_CHAR_FEATURE_RECONNECTABLE) == false);
1255 /* check max avail */
1256 qemu_chr_fe_init(&be, chr, &error_abort);
1257 qemu_chr_fe_init(&be, chr, &err);
1258 error_free_or_abort(&err);
1260 /* deinit & reinit */
1261 qemu_chr_fe_deinit(&be, false);
1262 qemu_chr_fe_init(&be, chr, &error_abort);
1264 qemu_chr_fe_set_open(&be, true);
1266 qemu_chr_fe_set_handlers(&be,
1273 ret = qemu_chr_fe_write(&be, (void *)"buf", 4);
1274 g_assert_cmpint(ret, ==, 4);
1276 qemu_chr_fe_deinit(&be, true);
1279 static void char_invalid_test(void)
1282 g_setenv("QTEST_SILENT_ERRORS", "1", 1);
1283 chr = qemu_chr_new("label-invalid", "invalid", NULL);
1285 g_unsetenv("QTEST_SILENT_ERRORS");
1288 static int chardev_change(void *opaque)
1293 static int chardev_change_denied(void *opaque)
1298 static void char_hotswap_test(void)
1304 gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
1305 char *filename = g_build_filename(tmp_path, "file", NULL);
1306 ChardevFile file = { .out = filename };
1307 ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
1308 .u.file.data = &file };
1312 int sock = make_udp_socket(&port);
1313 g_assert_cmpint(sock, >, 0);
1315 chr_args = g_strdup_printf("udp:127.0.0.1:%d", port);
1317 chr = qemu_chr_new("chardev", chr_args, NULL);
1318 qemu_chr_fe_init(&be, chr, &error_abort);
1320 /* check that chardev operates correctly */
1321 char_udp_test_internal(chr, sock);
1323 /* set the handler that denies the hotswap */
1324 qemu_chr_fe_set_handlers(&be, NULL, NULL,
1325 NULL, chardev_change_denied, NULL, NULL, true);
1327 /* now, change is denied and has to keep the old backend operating */
1328 ret = qmp_chardev_change("chardev", &backend, NULL);
1330 g_assert(be.chr == chr);
1332 char_udp_test_internal(chr, sock);
1334 /* now allow the change */
1335 qemu_chr_fe_set_handlers(&be, NULL, NULL,
1336 NULL, chardev_change, NULL, NULL, true);
1338 /* has to succeed now */
1339 ret = qmp_chardev_change("chardev", &backend, &error_abort);
1340 g_assert(be.chr != chr);
1345 /* run the file chardev test */
1346 char_file_test_internal(chr, filename);
1348 object_unparent(OBJECT(chr));
1350 qapi_free_ChardevReturn(ret);
1358 static SocketAddress tcpaddr = {
1359 .type = SOCKET_ADDRESS_TYPE_INET,
1360 .u.inet.host = (char *)"127.0.0.1",
1361 .u.inet.port = (char *)"0",
1364 static SocketAddress unixaddr = {
1365 .type = SOCKET_ADDRESS_TYPE_UNIX,
1366 .u.q_unix.path = (char *)"test-char.sock",
1370 int main(int argc, char **argv)
1372 bool has_ipv4, has_ipv6;
1374 qemu_init_main_loop(&error_abort);
1377 g_test_init(&argc, &argv, NULL);
1379 if (socket_check_protocol_support(&has_ipv4, &has_ipv6) < 0) {
1380 g_printerr("socket_check_protocol_support() failed\n");
1384 module_call_init(MODULE_INIT_QOM);
1385 qemu_add_opts(&qemu_chardev_opts);
1387 g_test_add_func("/char/null", char_null_test);
1388 g_test_add_func("/char/invalid", char_invalid_test);
1389 g_test_add_func("/char/ringbuf", char_ringbuf_test);
1390 g_test_add_func("/char/mux", char_mux_test);
1392 g_test_add_func("/char/console/subprocess", char_console_test_subprocess);
1393 g_test_add_func("/char/console", char_console_test);
1395 g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess);
1396 g_test_add_func("/char/stdio", char_stdio_test);
1398 g_test_add_func("/char/pipe", char_pipe_test);
1400 g_test_add_func("/char/file", char_file_test);
1402 g_test_add_func("/char/file-fifo", char_file_fifo_test);
1405 #define SOCKET_SERVER_TEST(name, addr) \
1406 static CharSocketServerTestConfig server1 ## name = \
1407 { addr, false, false }; \
1408 static CharSocketServerTestConfig server2 ## name = \
1409 { addr, true, false }; \
1410 static CharSocketServerTestConfig server3 ## name = \
1411 { addr, false, true }; \
1412 static CharSocketServerTestConfig server4 ## name = \
1413 { addr, true, true }; \
1414 g_test_add_data_func("/char/socket/server/mainloop/" # name, \
1415 &server1 ##name, char_socket_server_test); \
1416 g_test_add_data_func("/char/socket/server/wait-conn/" # name, \
1417 &server2 ##name, char_socket_server_test); \
1418 g_test_add_data_func("/char/socket/server/mainloop-fdpass/" # name, \
1419 &server3 ##name, char_socket_server_test); \
1420 g_test_add_data_func("/char/socket/server/wait-conn-fdpass/" # name, \
1421 &server4 ##name, char_socket_server_test)
1423 #define SOCKET_CLIENT_TEST(name, addr) \
1424 static CharSocketClientTestConfig client1 ## name = \
1425 { addr, NULL, false, false }; \
1426 static CharSocketClientTestConfig client2 ## name = \
1427 { addr, NULL, true, false }; \
1428 static CharSocketClientTestConfig client3 ## name = \
1429 { addr, ",reconnect=1", false }; \
1430 static CharSocketClientTestConfig client4 ## name = \
1431 { addr, ",reconnect=1", true }; \
1432 static CharSocketClientTestConfig client5 ## name = \
1433 { addr, NULL, false, true }; \
1434 static CharSocketClientTestConfig client6 ## name = \
1435 { addr, NULL, true, true }; \
1436 g_test_add_data_func("/char/socket/client/mainloop/" # name, \
1437 &client1 ##name, char_socket_client_test); \
1438 g_test_add_data_func("/char/socket/client/wait-conn/" # name, \
1439 &client2 ##name, char_socket_client_test); \
1440 g_test_add_data_func("/char/socket/client/mainloop-reconnect/" # name, \
1441 &client3 ##name, char_socket_client_test); \
1442 g_test_add_data_func("/char/socket/client/wait-conn-reconnect/" # name, \
1443 &client4 ##name, char_socket_client_test); \
1444 g_test_add_data_func("/char/socket/client/mainloop-fdpass/" # name, \
1445 &client5 ##name, char_socket_client_test); \
1446 g_test_add_data_func("/char/socket/client/wait-conn-fdpass/" # name, \
1447 &client6 ##name, char_socket_client_test)
1450 SOCKET_SERVER_TEST(tcp, &tcpaddr);
1451 SOCKET_CLIENT_TEST(tcp, &tcpaddr);
1452 g_test_add_data_func("/char/socket/server/two-clients/tcp", &tcpaddr,
1453 char_socket_server_two_clients_test);
1456 SOCKET_SERVER_TEST(unix, &unixaddr);
1457 SOCKET_CLIENT_TEST(unix, &unixaddr);
1458 g_test_add_data_func("/char/socket/server/two-clients/unix", &unixaddr,
1459 char_socket_server_two_clients_test);
1462 g_test_add_func("/char/udp", char_udp_test);
1463 #if defined(HAVE_CHARDEV_SERIAL) && !defined(WIN32)
1464 g_test_add_func("/char/serial", char_serial_test);
1466 g_test_add_func("/char/hotswap", char_hotswap_test);
1467 g_test_add_func("/char/websocket", char_websock_test);
1470 return g_test_run();