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 "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"
17 #include "socket-helpers.h"
21 typedef struct FeHandler {
25 bool openclose_mismatch;
30 static void main_loop(void)
34 main_loop_wait(false);
38 static int fe_can_read(void *opaque)
40 FeHandler *h = opaque;
42 return sizeof(h->read_buf) - h->read_count;
45 static void fe_read(void *opaque, const uint8_t *buf, int size)
47 FeHandler *h = opaque;
49 g_assert_cmpint(size, <=, fe_can_read(opaque));
51 memcpy(h->read_buf + h->read_count, buf, size);
52 h->read_count += size;
56 static void fe_event(void *opaque, QEMUChrEvent event)
58 FeHandler *h = opaque;
61 h->last_event = event;
65 case CHR_EVENT_OPENED:
66 case CHR_EVENT_CLOSED:
68 new_open_state = (event == CHR_EVENT_OPENED);
69 if (h->is_open == new_open_state) {
70 h->openclose_mismatch = true;
72 h->is_open = new_open_state;
81 static void char_console_test_subprocess(void)
86 opts = qemu_opts_create(qemu_find_opts("chardev"), "console-label",
88 qemu_opt_set(opts, "backend", "console", &error_abort);
90 chr = qemu_chr_new_from_opts(opts, NULL, NULL);
91 g_assert_nonnull(chr);
93 qemu_chr_write_all(chr, (const uint8_t *)"CONSOLE", 7);
96 object_unparent(OBJECT(chr));
99 static void char_console_test(void)
101 g_test_trap_subprocess("/char/console/subprocess", 0, 0);
102 g_test_trap_assert_passed();
103 g_test_trap_assert_stdout("CONSOLE");
106 static void char_stdio_test_subprocess(void)
112 chr = qemu_chr_new("label", "stdio", NULL);
113 g_assert_nonnull(chr);
115 qemu_chr_fe_init(&be, chr, &error_abort);
116 qemu_chr_fe_set_open(&be, true);
117 ret = qemu_chr_fe_write(&be, (void *)"buf", 4);
118 g_assert_cmpint(ret, ==, 4);
120 qemu_chr_fe_deinit(&be, true);
123 static void char_stdio_test(void)
125 g_test_trap_subprocess("/char/stdio/subprocess", 0, 0);
126 g_test_trap_assert_passed();
127 g_test_trap_assert_stdout("buf");
130 static void char_ringbuf_test(void)
138 opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
140 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
142 qemu_opt_set(opts, "size", "5", &error_abort);
143 chr = qemu_chr_new_from_opts(opts, NULL, NULL);
147 opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
149 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
150 qemu_opt_set(opts, "size", "2", &error_abort);
151 chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
152 g_assert_nonnull(chr);
155 qemu_chr_fe_init(&be, chr, &error_abort);
156 ret = qemu_chr_fe_write(&be, (void *)"buff", 4);
157 g_assert_cmpint(ret, ==, 4);
159 data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
160 g_assert_cmpstr(data, ==, "ff");
163 data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
164 g_assert_cmpstr(data, ==, "");
167 qemu_chr_fe_deinit(&be, true);
170 opts = qemu_opts_create(qemu_find_opts("chardev"), "memory-label",
172 qemu_opt_set(opts, "backend", "memory", &error_abort);
173 qemu_opt_set(opts, "size", "2", &error_abort);
174 chr = qemu_chr_new_from_opts(opts, NULL, NULL);
175 g_assert_nonnull(chr);
176 object_unparent(OBJECT(chr));
180 static void char_mux_test(void)
185 FeHandler h1 = { 0, false, 0, false, }, h2 = { 0, false, 0, false, };
186 CharBackend chr_be1, chr_be2;
188 opts = qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
190 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
191 qemu_opt_set(opts, "size", "128", &error_abort);
192 qemu_opt_set(opts, "mux", "on", &error_abort);
193 chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
194 g_assert_nonnull(chr);
197 qemu_chr_fe_init(&chr_be1, chr, &error_abort);
198 qemu_chr_fe_set_handlers(&chr_be1,
206 qemu_chr_fe_init(&chr_be2, chr, &error_abort);
207 qemu_chr_fe_set_handlers(&chr_be2,
214 qemu_chr_fe_take_focus(&chr_be2);
216 base = qemu_chr_find("mux-label-base");
217 g_assert_cmpint(qemu_chr_be_can_write(base), !=, 0);
219 qemu_chr_be_write(base, (void *)"hello", 6);
220 g_assert_cmpint(h1.read_count, ==, 0);
221 g_assert_cmpint(h2.read_count, ==, 6);
222 g_assert_cmpstr(h2.read_buf, ==, "hello");
225 g_assert_cmpint(h1.last_event, !=, 42); /* should be MUX_OUT or OPENED */
226 g_assert_cmpint(h2.last_event, !=, 42); /* should be MUX_IN or OPENED */
227 /* sending event on the base broadcast to all fe, historical reasons? */
228 qemu_chr_be_event(base, 42);
229 g_assert_cmpint(h1.last_event, ==, 42);
230 g_assert_cmpint(h2.last_event, ==, 42);
231 qemu_chr_be_event(chr, -1);
232 g_assert_cmpint(h1.last_event, ==, 42);
233 g_assert_cmpint(h2.last_event, ==, -1);
236 qemu_chr_be_write(base, (void *)"\1b", 2);
237 g_assert_cmpint(h1.last_event, ==, 42);
238 g_assert_cmpint(h2.last_event, ==, CHR_EVENT_BREAK);
240 qemu_chr_be_write(base, (void *)"\1c", 2);
241 g_assert_cmpint(h1.last_event, ==, CHR_EVENT_MUX_IN);
242 g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT);
243 qemu_chr_be_event(chr, -1);
244 g_assert_cmpint(h1.last_event, ==, -1);
245 g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT);
247 qemu_chr_be_write(base, (void *)"hello", 6);
248 g_assert_cmpint(h2.read_count, ==, 0);
249 g_assert_cmpint(h1.read_count, ==, 6);
250 g_assert_cmpstr(h1.read_buf, ==, "hello");
253 qemu_chr_be_write(base, (void *)"\1b", 2);
254 g_assert_cmpint(h1.last_event, ==, CHR_EVENT_BREAK);
255 g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT);
257 /* open/close state and corresponding events */
258 g_assert_true(qemu_chr_fe_backend_open(&chr_be1));
259 g_assert_true(qemu_chr_fe_backend_open(&chr_be2));
260 g_assert_true(h1.is_open);
261 g_assert_false(h1.openclose_mismatch);
262 g_assert_true(h2.is_open);
263 g_assert_false(h2.openclose_mismatch);
265 h1.openclose_count = h2.openclose_count = 0;
267 qemu_chr_fe_set_handlers(&chr_be1, NULL, NULL, NULL, NULL,
269 qemu_chr_fe_set_handlers(&chr_be2, NULL, NULL, NULL, NULL,
271 g_assert_cmpint(h1.openclose_count, ==, 0);
272 g_assert_cmpint(h2.openclose_count, ==, 0);
274 h1.is_open = h2.is_open = false;
275 qemu_chr_fe_set_handlers(&chr_be1,
282 qemu_chr_fe_set_handlers(&chr_be2,
289 g_assert_cmpint(h1.openclose_count, ==, 1);
290 g_assert_false(h1.openclose_mismatch);
291 g_assert_cmpint(h2.openclose_count, ==, 1);
292 g_assert_false(h2.openclose_mismatch);
294 qemu_chr_be_event(base, CHR_EVENT_CLOSED);
295 qemu_chr_be_event(base, CHR_EVENT_OPENED);
296 g_assert_cmpint(h1.openclose_count, ==, 3);
297 g_assert_false(h1.openclose_mismatch);
298 g_assert_cmpint(h2.openclose_count, ==, 3);
299 g_assert_false(h2.openclose_mismatch);
301 qemu_chr_fe_set_handlers(&chr_be2,
308 qemu_chr_fe_set_handlers(&chr_be1,
316 /* remove first handler */
317 qemu_chr_fe_set_handlers(&chr_be1, NULL, NULL, NULL, NULL,
319 qemu_chr_be_write(base, (void *)"hello", 6);
320 g_assert_cmpint(h1.read_count, ==, 0);
321 g_assert_cmpint(h2.read_count, ==, 0);
323 qemu_chr_be_write(base, (void *)"\1c", 2);
324 qemu_chr_be_write(base, (void *)"hello", 6);
325 g_assert_cmpint(h1.read_count, ==, 0);
326 g_assert_cmpint(h2.read_count, ==, 6);
327 g_assert_cmpstr(h2.read_buf, ==, "hello");
331 qemu_chr_be_write(base, (void *)"\1?", 2);
332 data = qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort);
333 g_assert_cmpint(strlen(data), !=, 0);
336 qemu_chr_fe_deinit(&chr_be1, false);
337 qemu_chr_fe_deinit(&chr_be2, true);
341 static void websock_server_read(void *opaque, const uint8_t *buf, int size)
343 g_assert_cmpint(size, ==, 5);
344 g_assert(memcmp(buf, "world", size) == 0);
349 static int websock_server_can_read(void *opaque)
355 static bool websock_check_http_headers(char *buf, int size)
358 const char *ans[] = { "HTTP/1.1 101 Switching Protocols\r\n",
359 "Server: QEMU VNC\r\n",
360 "Upgrade: websocket\r\n",
361 "Connection: Upgrade\r\n",
362 "Sec-WebSocket-Accept:",
363 "Sec-WebSocket-Protocol: binary\r\n" };
365 for (i = 0; i < 6; i++) {
366 if (g_strstr_len(buf, size, ans[i]) == NULL) {
375 static void websock_client_read(void *opaque, const uint8_t *buf, int size)
377 const uint8_t ping[] = { 0x89, 0x85, /* Ping header */
378 0x07, 0x77, 0x9e, 0xf9, /* Masking key */
379 0x6f, 0x12, 0xf2, 0x95, 0x68 /* "hello" */ };
381 const uint8_t binary[] = { 0x82, 0x85, /* Binary header */
382 0x74, 0x90, 0xb9, 0xdf, /* Masking key */
383 0x03, 0xff, 0xcb, 0xb3, 0x10 /* "world" */ };
384 Chardev *chr_client = opaque;
386 if (websock_check_http_headers((char *) buf, size)) {
387 qemu_chr_fe_write(chr_client->be, ping, sizeof(ping));
388 } else if (buf[0] == 0x8a && buf[1] == 0x05) {
389 g_assert(strncmp((char *) buf + 2, "hello", 5) == 0);
390 qemu_chr_fe_write(chr_client->be, binary, sizeof(binary));
392 g_assert(buf[0] == 0x88 && buf[1] == 0x16);
393 g_assert(strncmp((char *) buf + 4, "peer requested close", 10) == 0);
399 static int websock_client_can_read(void *opaque)
405 static void char_websock_test(void)
411 char *handshake_port;
413 CharBackend client_be;
415 Chardev *chr = qemu_chr_new("server",
416 "websocket:127.0.0.1:0,server=on,wait=off", NULL);
417 const char handshake[] = "GET / HTTP/1.1\r\n"
418 "Upgrade: websocket\r\n"
419 "Connection: Upgrade\r\n"
420 "Host: localhost:%s\r\n"
421 "Origin: http://localhost:%s\r\n"
422 "Sec-WebSocket-Key: o9JHNiS3/0/0zYE1wa3yIw==\r\n"
423 "Sec-WebSocket-Version: 13\r\n"
424 "Sec-WebSocket-Protocol: binary\r\n\r\n";
425 const uint8_t close[] = { 0x88, 0x82, /* Close header */
426 0xef, 0xaa, 0xc5, 0x97, /* Masking key */
427 0xec, 0x42 /* Status code */ };
429 addr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
430 qdict = qobject_to(QDict, addr);
431 port = qdict_get_str(qdict, "port");
432 tmp = g_strdup_printf("tcp:127.0.0.1:%s", port);
433 handshake_port = g_strdup_printf(handshake, port, port);
434 qobject_unref(qdict);
436 qemu_chr_fe_init(&be, chr, &error_abort);
437 qemu_chr_fe_set_handlers(&be, websock_server_can_read, websock_server_read,
438 NULL, NULL, chr, NULL, true);
440 chr_client = qemu_chr_new("client", tmp, NULL);
441 qemu_chr_fe_init(&client_be, chr_client, &error_abort);
442 qemu_chr_fe_set_handlers(&client_be, websock_client_can_read,
444 NULL, NULL, chr_client, NULL, true);
447 qemu_chr_write_all(chr_client,
448 (uint8_t *) handshake_port,
449 strlen(handshake_port));
450 g_free(handshake_port);
453 g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
454 g_assert(object_property_get_bool(OBJECT(chr_client),
455 "connected", &error_abort));
457 qemu_chr_write_all(chr_client, close, sizeof(close));
460 object_unparent(OBJECT(chr_client));
461 object_unparent(OBJECT(chr));
466 static void char_pipe_test(void)
468 gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
469 gchar *tmp, *in, *out, *pipe = g_build_filename(tmp_path, "pipe", NULL);
474 FeHandler fe = { 0, };
476 in = g_strdup_printf("%s.in", pipe);
477 if (mkfifo(in, 0600) < 0) {
480 out = g_strdup_printf("%s.out", pipe);
481 if (mkfifo(out, 0600) < 0) {
485 tmp = g_strdup_printf("pipe:%s", pipe);
486 chr = qemu_chr_new("pipe", tmp, NULL);
487 g_assert_nonnull(chr);
490 qemu_chr_fe_init(&be, chr, &error_abort);
492 ret = qemu_chr_fe_write(&be, (void *)"pipe-out", 9);
493 g_assert_cmpint(ret, ==, 9);
495 fd = open(out, O_RDWR);
496 ret = read(fd, buf, sizeof(buf));
497 g_assert_cmpint(ret, ==, 9);
498 g_assert_cmpstr(buf, ==, "pipe-out");
501 fd = open(in, O_WRONLY);
502 ret = write(fd, "pipe-in", 8);
503 g_assert_cmpint(ret, ==, 8);
506 qemu_chr_fe_set_handlers(&be,
516 g_assert_cmpint(fe.read_count, ==, 8);
517 g_assert_cmpstr(fe.read_buf, ==, "pipe-in");
519 qemu_chr_fe_deinit(&be, true);
521 g_assert(g_unlink(in) == 0);
522 g_assert(g_unlink(out) == 0);
523 g_assert(g_rmdir(tmp_path) == 0);
531 typedef struct SocketIdleData {
536 CharBackend *client_be;
540 static void socket_read_hello(void *opaque, const uint8_t *buf, int size)
542 g_assert_cmpint(size, ==, 5);
543 g_assert(strncmp((char *)buf, "hello", 5) == 0);
548 static int socket_can_read_hello(void *opaque)
553 static int make_udp_socket(int *port)
555 struct sockaddr_in addr = { 0, };
556 socklen_t alen = sizeof(addr);
557 int ret, sock = qemu_socket(PF_INET, SOCK_DGRAM, 0);
559 g_assert_cmpint(sock, >, 0);
560 addr.sin_family = AF_INET ;
561 addr.sin_addr.s_addr = htonl(INADDR_ANY);
563 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
564 g_assert_cmpint(ret, ==, 0);
565 ret = getsockname(sock, (struct sockaddr *)&addr, &alen);
566 g_assert_cmpint(ret, ==, 0);
568 *port = ntohs(addr.sin_port);
572 static void char_udp_test_internal(Chardev *reuse_chr, int sock)
574 struct sockaddr_in other;
575 SocketIdleData d = { 0, };
578 socklen_t alen = sizeof(other);
588 sock = make_udp_socket(&port);
589 tmp = g_strdup_printf("udp:127.0.0.1:%d", port);
590 chr = qemu_chr_new("client", tmp, NULL);
591 g_assert_nonnull(chr);
593 be = g_alloca(sizeof(CharBackend));
594 qemu_chr_fe_init(be, chr, &error_abort);
598 qemu_chr_fe_set_handlers(be, socket_can_read_hello, socket_read_hello,
599 NULL, NULL, &d, NULL, true);
600 ret = qemu_chr_write_all(chr, (uint8_t *)"hello", 5);
601 g_assert_cmpint(ret, ==, 5);
603 ret = recvfrom(sock, buf, sizeof(buf), 0,
604 (struct sockaddr *)&other, &alen);
605 g_assert_cmpint(ret, ==, 5);
606 ret = sendto(sock, buf, 5, 0, (struct sockaddr *)&other, alen);
607 g_assert_cmpint(ret, ==, 5);
613 qemu_chr_fe_deinit(be, true);
618 static void char_udp_test(void)
620 char_udp_test_internal(NULL, 0);
628 } CharSocketTestData;
631 #define SOCKET_PING "Hello"
632 #define SOCKET_PONG "World"
634 typedef void (*char_socket_cb)(void *opaque, QEMUChrEvent event);
637 char_socket_event(void *opaque, QEMUChrEvent event)
639 CharSocketTestData *data = opaque;
644 char_socket_event_with_error(void *opaque, QEMUChrEvent event)
646 static bool first_error;
647 CharSocketTestData *data = opaque;
648 CharBackend *be = data->be;
651 case CHR_EVENT_OPENED:
654 qemu_chr_fe_disconnect(be);
657 case CHR_EVENT_CLOSED:
666 char_socket_read(void *opaque, const uint8_t *buf, int size)
668 CharSocketTestData *data = opaque;
669 g_assert_cmpint(size, ==, sizeof(SOCKET_PONG));
670 g_assert(memcmp(buf, SOCKET_PONG, size) == 0);
671 data->got_pong = true;
676 char_socket_can_read(void *opaque)
678 return sizeof(SOCKET_PONG);
683 char_socket_addr_to_opt_str(SocketAddress *addr, bool fd_pass,
684 const char *reconnect, bool is_listen)
687 QIOChannelSocket *ioc = qio_channel_socket_new();
690 g_assert(!reconnect);
692 qio_channel_socket_listen_sync(ioc, addr, 1, &error_abort);
694 qio_channel_socket_connect_sync(ioc, addr, &error_abort);
698 optstr = g_strdup_printf("socket,id=cdev0,fd=%d%s",
699 fd, is_listen ? ",server=on,wait=off" : "");
700 object_unref(OBJECT(ioc));
703 switch (addr->type) {
704 case SOCKET_ADDRESS_TYPE_INET:
705 return g_strdup_printf("socket,id=cdev0,host=%s,port=%s%s%s",
708 reconnect ? reconnect : "",
709 is_listen ? ",server=on,wait=off" : "");
711 case SOCKET_ADDRESS_TYPE_UNIX:
712 return g_strdup_printf("socket,id=cdev0,path=%s%s%s",
714 reconnect ? reconnect : "",
715 is_listen ? ",server=on,wait=off" : "");
718 g_assert_not_reached();
725 char_socket_ping_pong(QIOChannel *ioc, Error **errp)
727 char greeting[sizeof(SOCKET_PING)];
728 const char *response = SOCKET_PONG;
731 ret = qio_channel_read_all(ioc, greeting, sizeof(greeting), errp);
733 object_unref(OBJECT(ioc));
737 g_assert(memcmp(greeting, SOCKET_PING, sizeof(greeting)) == 0);
739 qio_channel_write_all(ioc, response, sizeof(SOCKET_PONG), errp);
740 object_unref(OBJECT(ioc));
746 char_socket_server_client_thread(gpointer data)
748 SocketAddress *addr = data;
749 QIOChannelSocket *ioc = qio_channel_socket_new();
751 qio_channel_socket_connect_sync(ioc, addr, &error_abort);
753 char_socket_ping_pong(QIO_CHANNEL(ioc), &error_abort);
763 } CharSocketServerTestConfig;
766 static void char_socket_server_test(gconstpointer opaque)
768 const CharSocketServerTestConfig *config = opaque;
770 CharBackend be = {0};
771 CharSocketTestData data = {0};
777 bool reconnected = false;
781 g_setenv("QTEST_SILENT_ERRORS", "1", 1);
783 * We rely on config->addr containing "wait=off", otherwise
784 * qemu_chr_new() will block until a client connects. We
785 * can't spawn our client thread though, because until
786 * qemu_chr_new() returns we don't know what TCP port was
787 * allocated by the OS
789 optstr = char_socket_addr_to_opt_str(config->addr,
793 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
795 g_assert_nonnull(opts);
796 chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
798 g_assert_nonnull(chr);
799 g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
801 qaddr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
802 g_assert_nonnull(qaddr);
804 v = qobject_input_visitor_new(qaddr);
805 visit_type_SocketAddress(v, "addr", &addr, &error_abort);
807 qobject_unref(qaddr);
809 qemu_chr_fe_init(&be, chr, &error_abort);
814 qemu_chr_fe_set_handlers(&be, NULL, NULL,
815 char_socket_event, NULL,
817 g_assert(data.event == -1);
820 * Kick off a thread to act as the "remote" client
821 * which just plays ping-pong with us
823 qemu_thread_create(&thread, "client",
824 char_socket_server_client_thread,
825 addr, QEMU_THREAD_JOINABLE);
826 g_assert(data.event == -1);
828 if (config->wait_connected) {
829 /* Synchronously accept a connection */
830 qemu_chr_wait_connected(chr, &error_abort);
833 * Asynchronously accept a connection when the evnt
834 * loop reports the listener socket as readable
836 while (data.event == -1) {
837 main_loop_wait(false);
840 g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
841 g_assert(data.event == CHR_EVENT_OPENED);
844 /* Send a greeting to the client */
845 ret = qemu_chr_fe_write_all(&be, (const uint8_t *)SOCKET_PING,
846 sizeof(SOCKET_PING));
847 g_assert_cmpint(ret, ==, sizeof(SOCKET_PING));
848 g_assert(data.event == -1);
850 /* Setup a callback to receive the reply to our greeting */
851 qemu_chr_fe_set_handlers(&be, char_socket_can_read,
853 char_socket_event, NULL,
855 g_assert(data.event == CHR_EVENT_OPENED);
858 /* Wait for the client to go away */
859 while (data.event == -1) {
860 main_loop_wait(false);
862 g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
863 g_assert(data.event == CHR_EVENT_CLOSED);
864 g_assert(data.got_pong);
866 qemu_thread_join(&thread);
873 qapi_free_SocketAddress(addr);
874 object_unparent(OBJECT(chr));
876 g_unsetenv("QTEST_SILENT_ERRORS");
881 char_socket_client_server_thread(gpointer data)
883 QIOChannelSocket *ioc = data;
884 QIOChannelSocket *cioc;
887 cioc = qio_channel_socket_accept(ioc, &error_abort);
888 g_assert_nonnull(cioc);
890 if (char_socket_ping_pong(QIO_CHANNEL(cioc), NULL) != 0) {
900 const char *reconnect;
903 char_socket_cb event_cb;
904 } CharSocketClientTestConfig;
906 static void char_socket_client_dupid_test(gconstpointer opaque)
908 const CharSocketClientTestConfig *config = opaque;
909 QIOChannelSocket *ioc;
911 Chardev *chr1, *chr2;
914 Error *local_err = NULL;
917 * Setup a listener socket and determine get its address
918 * so we know the TCP port for the client later
920 ioc = qio_channel_socket_new();
921 g_assert_nonnull(ioc);
922 qio_channel_socket_listen_sync(ioc, config->addr, 1, &error_abort);
923 addr = qio_channel_socket_get_local_address(ioc, &error_abort);
924 g_assert_nonnull(addr);
927 * Populate the chardev address based on what the server
928 * is actually listening on
930 optstr = char_socket_addr_to_opt_str(addr,
935 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
937 g_assert_nonnull(opts);
938 chr1 = qemu_chr_new_from_opts(opts, NULL, &error_abort);
939 g_assert_nonnull(chr1);
940 qemu_chr_wait_connected(chr1, &error_abort);
942 chr2 = qemu_chr_new_from_opts(opts, NULL, &local_err);
944 error_free_or_abort(&local_err);
946 object_unref(OBJECT(ioc));
948 object_unparent(OBJECT(chr1));
949 qapi_free_SocketAddress(addr);
953 static void char_socket_client_test(gconstpointer opaque)
955 const CharSocketClientTestConfig *config = opaque;
956 const char_socket_cb event_cb = config->event_cb;
957 QIOChannelSocket *ioc;
960 CharBackend be = {0};
961 CharSocketTestData data = {0};
965 bool reconnected = false;
969 * Setup a listener socket and determine get its address
970 * so we know the TCP port for the client later
972 ioc = qio_channel_socket_new();
973 g_assert_nonnull(ioc);
974 qio_channel_socket_listen_sync(ioc, config->addr, 1, &error_abort);
975 addr = qio_channel_socket_get_local_address(ioc, &error_abort);
976 g_assert_nonnull(addr);
979 * Kick off a thread to act as the "remote" client
980 * which just plays ping-pong with us
982 qemu_thread_create(&thread, "client",
983 char_socket_client_server_thread,
984 ioc, QEMU_THREAD_JOINABLE);
987 * Populate the chardev address based on what the server
988 * is actually listening on
990 optstr = char_socket_addr_to_opt_str(addr,
995 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
997 g_assert_nonnull(opts);
998 chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
1000 g_assert_nonnull(chr);
1002 if (config->reconnect) {
1004 * If reconnect is set, the connection will be
1005 * established in a background thread and we won't
1006 * see the "connected" status updated until we
1007 * run the main event loop, or call qemu_chr_wait_connected
1009 g_assert(!object_property_get_bool(OBJECT(chr), "connected",
1012 g_assert(object_property_get_bool(OBJECT(chr), "connected",
1016 qemu_chr_fe_init(&be, chr, &error_abort);
1021 qemu_chr_fe_set_handlers(&be, NULL, NULL,
1024 if (config->reconnect) {
1025 g_assert(data.event == -1);
1027 g_assert(data.event == CHR_EVENT_OPENED);
1030 if (config->wait_connected) {
1032 * Synchronously wait for the connection to complete
1033 * This should be a no-op if reconnect is not set.
1035 qemu_chr_wait_connected(chr, &error_abort);
1038 * Asynchronously wait for the connection to be reported
1039 * as complete when the background thread reports its
1041 * The loop will short-circuit if reconnect was set
1043 while (data.event == -1) {
1044 main_loop_wait(false);
1047 g_assert(data.event == CHR_EVENT_OPENED);
1049 g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
1051 /* Send a greeting to the server */
1052 ret = qemu_chr_fe_write_all(&be, (const uint8_t *)SOCKET_PING,
1053 sizeof(SOCKET_PING));
1054 g_assert_cmpint(ret, ==, sizeof(SOCKET_PING));
1055 g_assert(data.event == -1);
1057 /* Setup a callback to receive the reply to our greeting */
1058 qemu_chr_fe_set_handlers(&be, char_socket_can_read,
1062 g_assert(data.event == CHR_EVENT_OPENED);
1065 /* Wait for the server to go away */
1066 while (data.event == -1) {
1067 main_loop_wait(false);
1069 g_assert(data.event == CHR_EVENT_CLOSED);
1070 g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
1071 g_assert(data.got_pong);
1072 qemu_thread_join(&thread);
1074 if (config->reconnect && !reconnected) {
1076 qemu_thread_create(&thread, "client",
1077 char_socket_client_server_thread,
1078 ioc, QEMU_THREAD_JOINABLE);
1082 object_unref(OBJECT(ioc));
1083 object_unparent(OBJECT(chr));
1084 qapi_free_SocketAddress(addr);
1089 count_closed_event(void *opaque, QEMUChrEvent event)
1091 int *count = opaque;
1092 if (event == CHR_EVENT_CLOSED) {
1098 char_socket_discard_read(void *opaque, const uint8_t *buf, int size)
1102 static void char_socket_server_two_clients_test(gconstpointer opaque)
1104 SocketAddress *incoming_addr = (gpointer) opaque;
1106 CharBackend be = {0};
1108 SocketAddress *addr;
1112 QIOChannelSocket *ioc1, *ioc2;
1115 g_setenv("QTEST_SILENT_ERRORS", "1", 1);
1117 * We rely on addr containing "wait=off", otherwise
1118 * qemu_chr_new() will block until a client connects. We
1119 * can't spawn our client thread though, because until
1120 * qemu_chr_new() returns we don't know what TCP port was
1121 * allocated by the OS
1123 optstr = char_socket_addr_to_opt_str(incoming_addr,
1127 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
1129 g_assert_nonnull(opts);
1130 chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
1131 qemu_opts_del(opts);
1132 g_assert_nonnull(chr);
1133 g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
1135 qaddr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
1136 g_assert_nonnull(qaddr);
1138 v = qobject_input_visitor_new(qaddr);
1139 visit_type_SocketAddress(v, "addr", &addr, &error_abort);
1141 qobject_unref(qaddr);
1143 qemu_chr_fe_init(&be, chr, &error_abort);
1145 qemu_chr_fe_set_handlers(&be, char_socket_can_read, char_socket_discard_read,
1146 count_closed_event, NULL,
1147 &closed, NULL, true);
1149 ioc1 = qio_channel_socket_new();
1150 qio_channel_socket_connect_sync(ioc1, addr, &error_abort);
1151 qemu_chr_wait_connected(chr, &error_abort);
1153 /* switch the chardev to another context */
1154 GMainContext *ctx = g_main_context_new();
1155 qemu_chr_fe_set_handlers(&be, char_socket_can_read, char_socket_discard_read,
1156 count_closed_event, NULL,
1157 &closed, ctx, true);
1159 /* Start a second connection while the first is still connected.
1160 * It will be placed in the listen() backlog, and connect() will
1161 * succeed immediately.
1163 ioc2 = qio_channel_socket_new();
1164 qio_channel_socket_connect_sync(ioc2, addr, &error_abort);
1166 object_unref(OBJECT(ioc1));
1167 /* The two connections should now be processed serially. */
1168 while (g_main_context_iteration(ctx, TRUE)) {
1169 if (closed == 1 && ioc2) {
1170 object_unref(OBJECT(ioc2));
1178 qapi_free_SocketAddress(addr);
1179 object_unparent(OBJECT(chr));
1180 g_main_context_unref(ctx);
1182 g_unsetenv("QTEST_SILENT_ERRORS");
1186 #if defined(HAVE_CHARDEV_SERIAL) && !defined(WIN32)
1187 static void char_serial_test(void)
1192 opts = qemu_opts_create(qemu_find_opts("chardev"), "serial-id",
1194 qemu_opt_set(opts, "backend", "serial", &error_abort);
1195 qemu_opt_set(opts, "path", "/dev/null", &error_abort);
1197 chr = qemu_chr_new_from_opts(opts, NULL, NULL);
1198 g_assert_nonnull(chr);
1199 /* TODO: add more tests with a pty */
1200 object_unparent(OBJECT(chr));
1202 qemu_opts_del(opts);
1207 static void char_file_fifo_test(void)
1211 char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
1212 char *fifo = g_build_filename(tmp_path, "fifo", NULL);
1213 char *out = g_build_filename(tmp_path, "out", NULL);
1214 ChardevFile file = { .in = fifo,
1216 ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
1217 .u.file.data = &file };
1218 FeHandler fe = { 0, };
1221 if (mkfifo(fifo, 0600) < 0) {
1225 fd = open(fifo, O_RDWR);
1226 ret = write(fd, "fifo-in", 8);
1227 g_assert_cmpint(ret, ==, 8);
1229 chr = qemu_chardev_new("label-file", TYPE_CHARDEV_FILE, &backend,
1230 NULL, &error_abort);
1232 qemu_chr_fe_init(&be, chr, &error_abort);
1233 qemu_chr_fe_set_handlers(&be,
1240 g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK);
1241 qmp_chardev_send_break("label-foo", NULL);
1242 g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK);
1243 qmp_chardev_send_break("label-file", NULL);
1244 g_assert_cmpint(fe.last_event, ==, CHR_EVENT_BREAK);
1250 g_assert_cmpint(fe.read_count, ==, 8);
1251 g_assert_cmpstr(fe.read_buf, ==, "fifo-in");
1253 qemu_chr_fe_deinit(&be, true);
1264 static void char_file_test_internal(Chardev *ext_chr, const char *filepath)
1266 char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
1269 char *contents = NULL;
1270 ChardevFile file = {};
1271 ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
1272 .u.file.data = &file };
1278 out = g_strdup(filepath);
1281 out = g_build_filename(tmp_path, "out", NULL);
1283 chr = qemu_chardev_new(NULL, TYPE_CHARDEV_FILE, &backend,
1284 NULL, &error_abort);
1286 ret = qemu_chr_write_all(chr, (uint8_t *)"hello!", 6);
1287 g_assert_cmpint(ret, ==, 6);
1289 ret = g_file_get_contents(out, &contents, &length, NULL);
1290 g_assert(ret == TRUE);
1291 g_assert_cmpint(length, ==, 6);
1292 g_assert(strncmp(contents, "hello!", 6) == 0);
1295 object_unparent(OBJECT(chr));
1304 static void char_file_test(void)
1306 char_file_test_internal(NULL, NULL);
1309 static void char_null_test(void)
1316 chr = qemu_chr_find("label-null");
1319 chr = qemu_chr_new("label-null", "null", NULL);
1320 chr = qemu_chr_find("label-null");
1321 g_assert_nonnull(chr);
1323 g_assert(qemu_chr_has_feature(chr,
1324 QEMU_CHAR_FEATURE_FD_PASS) == false);
1325 g_assert(qemu_chr_has_feature(chr,
1326 QEMU_CHAR_FEATURE_RECONNECTABLE) == false);
1328 /* check max avail */
1329 qemu_chr_fe_init(&be, chr, &error_abort);
1330 qemu_chr_fe_init(&be, chr, &err);
1331 error_free_or_abort(&err);
1333 /* deinit & reinit */
1334 qemu_chr_fe_deinit(&be, false);
1335 qemu_chr_fe_init(&be, chr, &error_abort);
1337 qemu_chr_fe_set_open(&be, true);
1339 qemu_chr_fe_set_handlers(&be,
1346 ret = qemu_chr_fe_write(&be, (void *)"buf", 4);
1347 g_assert_cmpint(ret, ==, 4);
1349 qemu_chr_fe_deinit(&be, true);
1352 static void char_invalid_test(void)
1355 g_setenv("QTEST_SILENT_ERRORS", "1", 1);
1356 chr = qemu_chr_new("label-invalid", "invalid", NULL);
1358 g_unsetenv("QTEST_SILENT_ERRORS");
1361 static int chardev_change(void *opaque)
1366 static int chardev_change_denied(void *opaque)
1371 static void char_hotswap_test(void)
1377 gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
1378 char *filename = g_build_filename(tmp_path, "file", NULL);
1379 ChardevFile file = { .out = filename };
1380 ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
1381 .u.file.data = &file };
1385 int sock = make_udp_socket(&port);
1386 g_assert_cmpint(sock, >, 0);
1388 chr_args = g_strdup_printf("udp:127.0.0.1:%d", port);
1390 chr = qemu_chr_new("chardev", chr_args, NULL);
1391 qemu_chr_fe_init(&be, chr, &error_abort);
1393 /* check that chardev operates correctly */
1394 char_udp_test_internal(chr, sock);
1396 /* set the handler that denies the hotswap */
1397 qemu_chr_fe_set_handlers(&be, NULL, NULL,
1398 NULL, chardev_change_denied, NULL, NULL, true);
1400 /* now, change is denied and has to keep the old backend operating */
1401 ret = qmp_chardev_change("chardev", &backend, NULL);
1403 g_assert(be.chr == chr);
1405 char_udp_test_internal(chr, sock);
1407 /* now allow the change */
1408 qemu_chr_fe_set_handlers(&be, NULL, NULL,
1409 NULL, chardev_change, NULL, NULL, true);
1411 /* has to succeed now */
1412 ret = qmp_chardev_change("chardev", &backend, &error_abort);
1413 g_assert(be.chr != chr);
1418 /* run the file chardev test */
1419 char_file_test_internal(chr, filename);
1421 object_unparent(OBJECT(chr));
1423 qapi_free_ChardevReturn(ret);
1431 static SocketAddress tcpaddr = {
1432 .type = SOCKET_ADDRESS_TYPE_INET,
1433 .u.inet.host = (char *)"127.0.0.1",
1434 .u.inet.port = (char *)"0",
1437 static SocketAddress unixaddr = {
1438 .type = SOCKET_ADDRESS_TYPE_UNIX,
1439 .u.q_unix.path = (char *)"test-char.sock",
1443 int main(int argc, char **argv)
1445 bool has_ipv4, has_ipv6;
1447 qemu_init_main_loop(&error_abort);
1450 g_test_init(&argc, &argv, NULL);
1452 if (socket_check_protocol_support(&has_ipv4, &has_ipv6) < 0) {
1453 g_printerr("socket_check_protocol_support() failed\n");
1457 module_call_init(MODULE_INIT_QOM);
1458 qemu_add_opts(&qemu_chardev_opts);
1460 g_test_add_func("/char/null", char_null_test);
1461 g_test_add_func("/char/invalid", char_invalid_test);
1462 g_test_add_func("/char/ringbuf", char_ringbuf_test);
1463 g_test_add_func("/char/mux", char_mux_test);
1465 g_test_add_func("/char/console/subprocess", char_console_test_subprocess);
1466 g_test_add_func("/char/console", char_console_test);
1468 g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess);
1469 g_test_add_func("/char/stdio", char_stdio_test);
1471 g_test_add_func("/char/pipe", char_pipe_test);
1473 g_test_add_func("/char/file", char_file_test);
1475 g_test_add_func("/char/file-fifo", char_file_fifo_test);
1478 #define SOCKET_SERVER_TEST(name, addr) \
1479 static CharSocketServerTestConfig server1 ## name = \
1480 { addr, false, false }; \
1481 static CharSocketServerTestConfig server2 ## name = \
1482 { addr, true, false }; \
1483 static CharSocketServerTestConfig server3 ## name = \
1484 { addr, false, true }; \
1485 static CharSocketServerTestConfig server4 ## name = \
1486 { addr, true, true }; \
1487 g_test_add_data_func("/char/socket/server/mainloop/" # name, \
1488 &server1 ##name, char_socket_server_test); \
1489 g_test_add_data_func("/char/socket/server/wait-conn/" # name, \
1490 &server2 ##name, char_socket_server_test); \
1491 g_test_add_data_func("/char/socket/server/mainloop-fdpass/" # name, \
1492 &server3 ##name, char_socket_server_test); \
1493 g_test_add_data_func("/char/socket/server/wait-conn-fdpass/" # name, \
1494 &server4 ##name, char_socket_server_test)
1496 #define SOCKET_CLIENT_TEST(name, addr) \
1497 static CharSocketClientTestConfig client1 ## name = \
1498 { addr, NULL, false, false, char_socket_event }; \
1499 static CharSocketClientTestConfig client2 ## name = \
1500 { addr, NULL, true, false, char_socket_event }; \
1501 static CharSocketClientTestConfig client3 ## name = \
1502 { addr, ",reconnect=1", false, false, char_socket_event }; \
1503 static CharSocketClientTestConfig client4 ## name = \
1504 { addr, ",reconnect=1", true, false, char_socket_event }; \
1505 static CharSocketClientTestConfig client5 ## name = \
1506 { addr, NULL, false, true, char_socket_event }; \
1507 static CharSocketClientTestConfig client6 ## name = \
1508 { addr, NULL, true, true, char_socket_event }; \
1509 static CharSocketClientTestConfig client7 ## name = \
1510 { addr, ",reconnect=1", true, false, \
1511 char_socket_event_with_error }; \
1512 static CharSocketClientTestConfig client8 ## name = \
1513 { addr, ",reconnect=1", false, false, char_socket_event }; \
1514 g_test_add_data_func("/char/socket/client/mainloop/" # name, \
1515 &client1 ##name, char_socket_client_test); \
1516 g_test_add_data_func("/char/socket/client/wait-conn/" # name, \
1517 &client2 ##name, char_socket_client_test); \
1518 g_test_add_data_func("/char/socket/client/mainloop-reconnect/" # name, \
1519 &client3 ##name, char_socket_client_test); \
1520 g_test_add_data_func("/char/socket/client/wait-conn-reconnect/" # name, \
1521 &client4 ##name, char_socket_client_test); \
1522 g_test_add_data_func("/char/socket/client/mainloop-fdpass/" # name, \
1523 &client5 ##name, char_socket_client_test); \
1524 g_test_add_data_func("/char/socket/client/wait-conn-fdpass/" # name, \
1525 &client6 ##name, char_socket_client_test); \
1526 g_test_add_data_func("/char/socket/client/reconnect-error/" # name, \
1527 &client7 ##name, char_socket_client_test); \
1528 g_test_add_data_func("/char/socket/client/dupid-reconnect/" # name, \
1529 &client8 ##name, char_socket_client_dupid_test)
1532 SOCKET_SERVER_TEST(tcp, &tcpaddr);
1533 SOCKET_CLIENT_TEST(tcp, &tcpaddr);
1534 g_test_add_data_func("/char/socket/server/two-clients/tcp", &tcpaddr,
1535 char_socket_server_two_clients_test);
1538 SOCKET_SERVER_TEST(unix, &unixaddr);
1539 SOCKET_CLIENT_TEST(unix, &unixaddr);
1540 g_test_add_data_func("/char/socket/server/two-clients/unix", &unixaddr,
1541 char_socket_server_two_clients_test);
1544 g_test_add_func("/char/udp", char_udp_test);
1545 #if defined(HAVE_CHARDEV_SERIAL) && !defined(WIN32)
1546 g_test_add_func("/char/serial", char_serial_test);
1548 g_test_add_func("/char/hotswap", char_hotswap_test);
1549 g_test_add_func("/char/websocket", char_websock_test);
1552 return g_test_run();