qemu-iotests: Test streaming with missing job ID
[qemu/kevin.git] / tests / test-char.c
blob124d0c54390bc8936a899e3f2d7b9dd14670b17c
1 #include "qemu/osdep.h"
2 #include <glib/gstdio.h>
4 #include "qemu-common.h"
5 #include "qemu/config-file.h"
6 #include "qemu/sockets.h"
7 #include "sysemu/char.h"
8 #include "sysemu/sysemu.h"
9 #include "qapi/error.h"
10 #include "qom/qom-qobject.h"
11 #include "qmp-commands.h"
13 static bool quit;
15 typedef struct FeHandler {
16 int read_count;
17 int last_event;
18 char read_buf[128];
19 } FeHandler;
21 static void main_loop(void)
23 bool nonblocking;
24 int last_io = 0;
26 quit = false;
27 do {
28 nonblocking = last_io > 0;
29 last_io = main_loop_wait(nonblocking);
30 } while (!quit);
33 static int fe_can_read(void *opaque)
35 FeHandler *h = opaque;
37 return sizeof(h->read_buf) - h->read_count;
40 static void fe_read(void *opaque, const uint8_t *buf, int size)
42 FeHandler *h = opaque;
44 g_assert_cmpint(size, <=, fe_can_read(opaque));
46 memcpy(h->read_buf + h->read_count, buf, size);
47 h->read_count += size;
48 quit = true;
51 static void fe_event(void *opaque, int event)
53 FeHandler *h = opaque;
55 h->last_event = event;
56 quit = true;
59 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
60 #ifdef _WIN32
61 static void char_console_test_subprocess(void)
63 QemuOpts *opts;
64 Chardev *chr;
66 opts = qemu_opts_create(qemu_find_opts("chardev"), "console-label",
67 1, &error_abort);
68 qemu_opt_set(opts, "backend", "console", &error_abort);
70 chr = qemu_chr_new_from_opts(opts, NULL);
71 g_assert_nonnull(chr);
73 qemu_chr_write_all(chr, (const uint8_t *)"CONSOLE", 7);
75 qemu_opts_del(opts);
76 object_unparent(OBJECT(chr));
79 static void char_console_test(void)
81 g_test_trap_subprocess("/char/console/subprocess", 0, 0);
82 g_test_trap_assert_passed();
83 g_test_trap_assert_stdout("CONSOLE");
85 #endif
86 static void char_stdio_test_subprocess(void)
88 Chardev *chr;
89 CharBackend be;
90 int ret;
92 chr = qemu_chr_new("label", "stdio");
93 g_assert_nonnull(chr);
95 qemu_chr_fe_init(&be, chr, &error_abort);
96 qemu_chr_fe_set_open(&be, true);
97 ret = qemu_chr_fe_write(&be, (void *)"buf", 4);
98 g_assert_cmpint(ret, ==, 4);
100 qemu_chr_fe_deinit(&be);
101 object_unparent(OBJECT(chr));
104 static void char_stdio_test(void)
106 g_test_trap_subprocess("/char/stdio/subprocess", 0, 0);
107 g_test_trap_assert_passed();
108 g_test_trap_assert_stdout("buf");
110 #endif
112 static void char_ringbuf_test(void)
114 QemuOpts *opts;
115 Chardev *chr;
116 CharBackend be;
117 char *data;
118 int ret;
120 opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
121 1, &error_abort);
122 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
124 qemu_opt_set(opts, "size", "5", &error_abort);
125 chr = qemu_chr_new_from_opts(opts, NULL);
126 g_assert_null(chr);
127 qemu_opts_del(opts);
129 opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
130 1, &error_abort);
131 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
132 qemu_opt_set(opts, "size", "2", &error_abort);
133 chr = qemu_chr_new_from_opts(opts, &error_abort);
134 g_assert_nonnull(chr);
135 qemu_opts_del(opts);
137 qemu_chr_fe_init(&be, chr, &error_abort);
138 ret = qemu_chr_fe_write(&be, (void *)"buff", 4);
139 g_assert_cmpint(ret, ==, 4);
141 data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
142 g_assert_cmpstr(data, ==, "ff");
143 g_free(data);
145 data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
146 g_assert_cmpstr(data, ==, "");
147 g_free(data);
149 qemu_chr_fe_deinit(&be);
150 object_unparent(OBJECT(chr));
152 /* check alias */
153 opts = qemu_opts_create(qemu_find_opts("chardev"), "memory-label",
154 1, &error_abort);
155 qemu_opt_set(opts, "backend", "memory", &error_abort);
156 qemu_opt_set(opts, "size", "2", &error_abort);
157 chr = qemu_chr_new_from_opts(opts, NULL);
158 g_assert_nonnull(chr);
159 object_unparent(OBJECT(chr));
160 qemu_opts_del(opts);
163 static void char_mux_test(void)
165 QemuOpts *opts;
166 Chardev *chr, *base;
167 char *data;
168 FeHandler h1 = { 0, }, h2 = { 0, };
169 CharBackend chr_be1, chr_be2;
171 opts = qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
172 1, &error_abort);
173 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
174 qemu_opt_set(opts, "size", "128", &error_abort);
175 qemu_opt_set(opts, "mux", "on", &error_abort);
176 chr = qemu_chr_new_from_opts(opts, &error_abort);
177 g_assert_nonnull(chr);
178 qemu_opts_del(opts);
180 qemu_chr_fe_init(&chr_be1, chr, &error_abort);
181 qemu_chr_fe_set_handlers(&chr_be1,
182 fe_can_read,
183 fe_read,
184 fe_event,
185 &h1,
186 NULL, true);
188 qemu_chr_fe_init(&chr_be2, chr, &error_abort);
189 qemu_chr_fe_set_handlers(&chr_be2,
190 fe_can_read,
191 fe_read,
192 fe_event,
193 &h2,
194 NULL, true);
195 qemu_chr_fe_take_focus(&chr_be2);
197 base = qemu_chr_find("mux-label-base");
198 g_assert_cmpint(qemu_chr_be_can_write(base), !=, 0);
200 qemu_chr_be_write(base, (void *)"hello", 6);
201 g_assert_cmpint(h1.read_count, ==, 0);
202 g_assert_cmpint(h2.read_count, ==, 6);
203 g_assert_cmpstr(h2.read_buf, ==, "hello");
204 h2.read_count = 0;
206 /* switch focus */
207 qemu_chr_be_write(base, (void *)"\1c", 2);
209 qemu_chr_be_write(base, (void *)"hello", 6);
210 g_assert_cmpint(h2.read_count, ==, 0);
211 g_assert_cmpint(h1.read_count, ==, 6);
212 g_assert_cmpstr(h1.read_buf, ==, "hello");
213 h1.read_count = 0;
215 /* remove first handler */
216 qemu_chr_fe_set_handlers(&chr_be1, NULL, NULL, NULL, NULL, NULL, true);
217 qemu_chr_be_write(base, (void *)"hello", 6);
218 g_assert_cmpint(h1.read_count, ==, 0);
219 g_assert_cmpint(h2.read_count, ==, 0);
221 qemu_chr_be_write(base, (void *)"\1c", 2);
222 qemu_chr_be_write(base, (void *)"hello", 6);
223 g_assert_cmpint(h1.read_count, ==, 0);
224 g_assert_cmpint(h2.read_count, ==, 6);
225 g_assert_cmpstr(h2.read_buf, ==, "hello");
226 h2.read_count = 0;
228 /* print help */
229 qemu_chr_be_write(base, (void *)"\1?", 2);
230 data = qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort);
231 g_assert_cmpint(strlen(data), !=, 0);
232 g_free(data);
234 qemu_chr_fe_deinit(&chr_be1);
235 qemu_chr_fe_deinit(&chr_be2);
236 object_unparent(OBJECT(chr));
239 typedef struct SocketIdleData {
240 GMainLoop *loop;
241 Chardev *chr;
242 bool conn_expected;
243 CharBackend *be;
244 CharBackend *client_be;
245 } SocketIdleData;
247 static gboolean char_socket_test_idle(gpointer user_data)
249 SocketIdleData *data = user_data;
251 if (object_property_get_bool(OBJECT(data->chr), "connected", NULL)
252 == data->conn_expected) {
253 quit = true;
254 return FALSE;
257 return TRUE;
260 static void socket_read(void *opaque, const uint8_t *buf, int size)
262 SocketIdleData *data = opaque;
264 g_assert_cmpint(size, ==, 1);
265 g_assert_cmpint(*buf, ==, 'Z');
267 size = qemu_chr_fe_write(data->be, (const uint8_t *)"hello", 5);
268 g_assert_cmpint(size, ==, 5);
271 static int socket_can_read(void *opaque)
273 return 10;
276 static void socket_read_hello(void *opaque, const uint8_t *buf, int size)
278 g_assert_cmpint(size, ==, 5);
279 g_assert(strncmp((char *)buf, "hello", 5) == 0);
281 quit = true;
284 static int socket_can_read_hello(void *opaque)
286 return 10;
289 static void char_socket_test(void)
291 Chardev *chr = qemu_chr_new("server", "tcp:127.0.0.1:0,server,nowait");
292 Chardev *chr_client;
293 QObject *addr;
294 QDict *qdict;
295 const char *port;
296 SocketIdleData d = { .chr = chr };
297 CharBackend be;
298 CharBackend client_be;
299 char *tmp;
301 d.be = &be;
302 d.client_be = &be;
304 g_assert_nonnull(chr);
305 g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
307 addr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
308 qdict = qobject_to_qdict(addr);
309 port = qdict_get_str(qdict, "port");
310 tmp = g_strdup_printf("tcp:127.0.0.1:%s", port);
311 QDECREF(qdict);
313 qemu_chr_fe_init(&be, chr, &error_abort);
314 qemu_chr_fe_set_handlers(&be, socket_can_read, socket_read,
315 NULL, &d, NULL, true);
317 chr_client = qemu_chr_new("client", tmp);
318 qemu_chr_fe_init(&client_be, chr_client, &error_abort);
319 qemu_chr_fe_set_handlers(&client_be, socket_can_read_hello,
320 socket_read_hello,
321 NULL, &d, NULL, true);
322 g_free(tmp);
324 d.conn_expected = true;
325 guint id = g_idle_add(char_socket_test_idle, &d);
326 g_source_set_name_by_id(id, "test-idle");
327 g_assert_cmpint(id, >, 0);
328 main_loop();
330 g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
331 g_assert(object_property_get_bool(OBJECT(chr_client),
332 "connected", &error_abort));
334 qemu_chr_write_all(chr_client, (const uint8_t *)"Z", 1);
335 main_loop();
337 object_unparent(OBJECT(chr_client));
339 d.conn_expected = false;
340 g_idle_add(char_socket_test_idle, &d);
341 main_loop();
343 object_unparent(OBJECT(chr));
346 #ifndef _WIN32
347 static void char_pipe_test(void)
349 gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
350 gchar *tmp, *in, *out, *pipe = g_build_filename(tmp_path, "pipe", NULL);
351 Chardev *chr;
352 CharBackend be;
353 int ret, fd;
354 char buf[10];
355 FeHandler fe = { 0, };
357 in = g_strdup_printf("%s.in", pipe);
358 if (mkfifo(in, 0600) < 0) {
359 abort();
361 out = g_strdup_printf("%s.out", pipe);
362 if (mkfifo(out, 0600) < 0) {
363 abort();
366 tmp = g_strdup_printf("pipe:%s", pipe);
367 chr = qemu_chr_new("pipe", tmp);
368 g_assert_nonnull(chr);
369 g_free(tmp);
371 qemu_chr_fe_init(&be, chr, &error_abort);
373 ret = qemu_chr_fe_write(&be, (void *)"pipe-out", 9);
374 g_assert_cmpint(ret, ==, 9);
376 fd = open(out, O_RDWR);
377 ret = read(fd, buf, sizeof(buf));
378 g_assert_cmpint(ret, ==, 9);
379 g_assert_cmpstr(buf, ==, "pipe-out");
380 close(fd);
382 fd = open(in, O_WRONLY);
383 ret = write(fd, "pipe-in", 8);
384 g_assert_cmpint(ret, ==, 8);
385 close(fd);
387 qemu_chr_fe_set_handlers(&be,
388 fe_can_read,
389 fe_read,
390 fe_event,
391 &fe,
392 NULL, true);
394 main_loop();
396 g_assert_cmpint(fe.read_count, ==, 8);
397 g_assert_cmpstr(fe.read_buf, ==, "pipe-in");
399 qemu_chr_fe_deinit(&be);
400 object_unparent(OBJECT(chr));
402 g_assert(g_unlink(in) == 0);
403 g_assert(g_unlink(out) == 0);
404 g_assert(g_rmdir(tmp_path) == 0);
405 g_free(in);
406 g_free(out);
407 g_free(tmp_path);
408 g_free(pipe);
410 #endif
412 static void char_udp_test(void)
414 struct sockaddr_in addr = { 0, }, other;
415 SocketIdleData d = { 0, };
416 Chardev *chr;
417 CharBackend be;
418 socklen_t alen = sizeof(addr);
419 int ret, sock = qemu_socket(PF_INET, SOCK_DGRAM, 0);
420 char buf[10];
421 char *tmp;
423 g_assert_cmpint(sock, >, 0);
424 addr.sin_family = AF_INET ;
425 addr.sin_addr.s_addr = htonl(INADDR_ANY);
426 addr.sin_port = 0;
427 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
428 g_assert_cmpint(ret, ==, 0);
429 ret = getsockname(sock, (struct sockaddr *)&addr, &alen);
430 g_assert_cmpint(ret, ==, 0);
432 tmp = g_strdup_printf("udp:127.0.0.1:%d",
433 ntohs(addr.sin_port));
434 chr = qemu_chr_new("client", tmp);
435 g_assert_nonnull(chr);
437 d.chr = chr;
438 qemu_chr_fe_init(&be, chr, &error_abort);
439 qemu_chr_fe_set_handlers(&be, socket_can_read_hello, socket_read_hello,
440 NULL, &d, NULL, true);
441 ret = qemu_chr_write_all(chr, (uint8_t *)"hello", 5);
442 g_assert_cmpint(ret, ==, 5);
444 alen = sizeof(addr);
445 ret = recvfrom(sock, buf, sizeof(buf), 0,
446 (struct sockaddr *)&other, &alen);
447 g_assert_cmpint(ret, ==, 5);
448 ret = sendto(sock, buf, 5, 0, (struct sockaddr *)&other, alen);
449 g_assert_cmpint(ret, ==, 5);
451 main_loop();
453 close(sock);
454 g_free(tmp);
457 static void char_file_test(void)
459 char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
460 char *out = g_build_filename(tmp_path, "out", NULL);
461 char *contents = NULL;
462 ChardevFile file = { .out = out };
463 ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
464 .u.file.data = &file };
465 Chardev *chr;
466 gsize length;
467 int ret;
469 chr = qemu_chardev_new(NULL, TYPE_CHARDEV_FILE, &backend,
470 &error_abort);
471 ret = qemu_chr_write_all(chr, (uint8_t *)"hello!", 6);
472 g_assert_cmpint(ret, ==, 6);
473 object_unref(OBJECT(chr));
475 ret = g_file_get_contents(out, &contents, &length, NULL);
476 g_assert(ret == TRUE);
477 g_assert_cmpint(length, ==, 6);
478 g_assert(strncmp(contents, "hello!", 6) == 0);
479 g_free(contents);
481 #ifndef _WIN32
483 CharBackend be;
484 FeHandler fe = { 0, };
485 char *fifo = g_build_filename(tmp_path, "fifo", NULL);
486 int fd;
488 if (mkfifo(fifo, 0600) < 0) {
489 abort();
492 fd = open(fifo, O_RDWR);
493 ret = write(fd, "fifo-in", 8);
494 g_assert_cmpint(ret, ==, 8);
496 file.in = fifo;
497 file.has_in = true;
498 chr = qemu_chardev_new(NULL, TYPE_CHARDEV_FILE, &backend,
499 &error_abort);
501 qemu_chr_fe_init(&be, chr, &error_abort);
502 qemu_chr_fe_set_handlers(&be,
503 fe_can_read,
504 fe_read,
505 fe_event,
506 &fe, NULL, true);
508 main_loop();
510 close(fd);
512 g_assert_cmpint(fe.read_count, ==, 8);
513 g_assert_cmpstr(fe.read_buf, ==, "fifo-in");
514 qemu_chr_fe_deinit(&be);
515 object_unref(OBJECT(chr));
516 g_unlink(fifo);
517 g_free(fifo);
519 #endif
521 g_unlink(out);
522 g_rmdir(tmp_path);
523 g_free(tmp_path);
524 g_free(out);
527 static void char_null_test(void)
529 Error *err = NULL;
530 Chardev *chr;
531 CharBackend be;
532 int ret;
534 chr = qemu_chr_find("label-null");
535 g_assert_null(chr);
537 chr = qemu_chr_new("label-null", "null");
538 chr = qemu_chr_find("label-null");
539 g_assert_nonnull(chr);
541 g_assert(qemu_chr_has_feature(chr,
542 QEMU_CHAR_FEATURE_FD_PASS) == false);
543 g_assert(qemu_chr_has_feature(chr,
544 QEMU_CHAR_FEATURE_RECONNECTABLE) == false);
546 /* check max avail */
547 qemu_chr_fe_init(&be, chr, &error_abort);
548 qemu_chr_fe_init(&be, chr, &err);
549 error_free_or_abort(&err);
551 /* deinit & reinit */
552 qemu_chr_fe_deinit(&be);
553 qemu_chr_fe_init(&be, chr, &error_abort);
555 qemu_chr_fe_set_open(&be, true);
557 qemu_chr_fe_set_handlers(&be,
558 fe_can_read,
559 fe_read,
560 fe_event,
561 NULL, NULL, true);
563 ret = qemu_chr_fe_write(&be, (void *)"buf", 4);
564 g_assert_cmpint(ret, ==, 4);
566 qemu_chr_fe_deinit(&be);
567 object_unparent(OBJECT(chr));
570 static void char_invalid_test(void)
572 Chardev *chr;
574 chr = qemu_chr_new("label-invalid", "invalid");
575 g_assert_null(chr);
578 int main(int argc, char **argv)
580 qemu_init_main_loop(&error_abort);
581 socket_init();
583 g_test_init(&argc, &argv, NULL);
585 module_call_init(MODULE_INIT_QOM);
586 qemu_add_opts(&qemu_chardev_opts);
588 g_test_add_func("/char/null", char_null_test);
589 g_test_add_func("/char/invalid", char_invalid_test);
590 g_test_add_func("/char/ringbuf", char_ringbuf_test);
591 g_test_add_func("/char/mux", char_mux_test);
592 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
593 #ifdef _WIN32
594 g_test_add_func("/char/console/subprocess", char_console_test_subprocess);
595 g_test_add_func("/char/console", char_console_test);
596 #endif
597 g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess);
598 g_test_add_func("/char/stdio", char_stdio_test);
599 #endif
600 #ifndef _WIN32
601 g_test_add_func("/char/pipe", char_pipe_test);
602 #endif
603 g_test_add_func("/char/file", char_file_test);
604 g_test_add_func("/char/socket", char_socket_test);
605 g_test_add_func("/char/udp", char_udp_test);
607 return g_test_run();