master
c 1,999 lines 62.5 KB
Raw
1 #include "qemu/osdep.h"
2 #include <glib/gstdio.h>
3
4 #include "qapi/error.h"
5 #include "qemu/config-file.h"
6 #include "qemu/module.h"
7 #include "qemu/option.h"
8 #include "qemu/sockets.h"
9 #include "chardev/char-fe.h"
10 #include "system/system.h"
11 #include "qapi/error.h"
12 #include "qapi/qapi-commands-char.h"
13 #include "qobject/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"
19
20 static bool quit;
21
22 typedef struct FeHandler {
23 int read_count;
24 bool is_open;
25 int openclose_count;
26 bool openclose_mismatch;
27 int last_event;
28 char read_buf[128];
29 } FeHandler;
30
31 static void main_loop(void)
32 {
33 quit = false;
34 do {
35 main_loop_wait(false);
36 } while (!quit);
37 }
38
39 static int fe_can_read(void *opaque)
40 {
41 FeHandler *h = opaque;
42
43 return sizeof(h->read_buf) - h->read_count;
44 }
45
46 static void fe_read(void *opaque, const uint8_t *buf, int size)
47 {
48 FeHandler *h = opaque;
49
50 g_assert_cmpint(size, <=, fe_can_read(opaque));
51
52 memcpy(h->read_buf + h->read_count, buf, size);
53 h->read_count += size;
54 quit = true;
55 }
56
57 static void fe_event(void *opaque, QEMUChrEvent event)
58 {
59 FeHandler *h = opaque;
60 bool new_open_state;
61
62 h->last_event = event;
63 switch (event) {
64 case CHR_EVENT_BREAK:
65 break;
66 case CHR_EVENT_OPENED:
67 case CHR_EVENT_CLOSED:
68 h->openclose_count++;
69 new_open_state = (event == CHR_EVENT_OPENED);
70 if (h->is_open == new_open_state) {
71 h->openclose_mismatch = true;
72 }
73 h->is_open = new_open_state;
74 /* fallthrough */
75 default:
76 quit = true;
77 break;
78 }
79 }
80
81 #ifdef _WIN32
82 static void char_console_test_subprocess(void)
83 {
84 QemuOpts *opts;
85 Chardev *chr;
86
87 opts = qemu_opts_create(qemu_find_opts("chardev"), "console-label",
88 1, &error_abort);
89 qemu_opt_set(opts, "backend", "console", &error_abort);
90
91 chr = qemu_chr_new_from_opts(opts, NULL, NULL);
92 g_assert_nonnull(chr);
93
94 qemu_chr_write_all(chr, (const uint8_t *)"CONSOLE", 7);
95
96 qemu_opts_del(opts);
97 object_unparent(OBJECT(chr));
98 }
99
100 static void char_console_test(void)
101 {
102 g_test_trap_subprocess("/char/console/subprocess", 0, 0);
103 g_test_trap_assert_passed();
104 g_test_trap_assert_stdout("CONSOLE");
105 }
106 #endif
107 static void char_stdio_test_subprocess(void)
108 {
109 Chardev *chr;
110 CharFrontend c;
111 int ret;
112
113 chr = qemu_chr_new("label", "stdio", NULL);
114 g_assert_nonnull(chr);
115
116 qemu_chr_fe_init(&c, chr, &error_abort);
117 qemu_chr_fe_set_open(&c, true);
118 ret = qemu_chr_fe_write(&c, (void *)"buf", 4);
119 g_assert_cmpint(ret, ==, 4);
120
121 qemu_chr_fe_deinit(&c, true);
122 }
123
124 static void char_stdio_test(void)
125 {
126 g_test_trap_subprocess("/char/stdio/subprocess", 0, 0);
127 g_test_trap_assert_passed();
128 g_test_trap_assert_stdout("buf");
129 }
130
131 static void char_ringbuf_test(void)
132 {
133 QemuOpts *opts;
134 Chardev *chr;
135 CharFrontend c;
136 char *data;
137 int ret;
138
139 opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
140 1, &error_abort);
141 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
142
143 qemu_opt_set(opts, "size", "5", &error_abort);
144 chr = qemu_chr_new_from_opts(opts, NULL, NULL);
145 g_assert_null(chr);
146 qemu_opts_del(opts);
147
148 opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
149 1, &error_abort);
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);
154 qemu_opts_del(opts);
155
156 qemu_chr_fe_init(&c, chr, &error_abort);
157 ret = qemu_chr_fe_write(&c, (void *)"buff", 4);
158 g_assert_cmpint(ret, ==, 4);
159
160 data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
161 g_assert_cmpstr(data, ==, "ff");
162 g_free(data);
163
164 data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
165 g_assert_cmpstr(data, ==, "");
166 g_free(data);
167
168 qemu_chr_fe_deinit(&c, true);
169
170 /* check alias */
171 opts = qemu_opts_create(qemu_find_opts("chardev"), "memory-label",
172 1, &error_abort);
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));
178 qemu_opts_del(opts);
179 }
180
181 static void char_mux_test(void)
182 {
183 QemuOpts *opts;
184 Chardev *chr, *base;
185 char *data;
186 FeHandler h1 = { 0, false, 0, false, }, h2 = { 0, false, 0, false, };
187 CharFrontend chr_fe1, chr_fe2;
188 Error *error = NULL;
189
190 /* Create mux and chardev to be immediately removed */
191 opts = qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
192 1, &error_abort);
193 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
194 qemu_opt_set(opts, "size", "128", &error_abort);
195 qemu_opt_set(opts, "mux", "on", &error_abort);
196 chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
197 g_assert_nonnull(chr);
198 qemu_opts_del(opts);
199
200 /* Remove just created mux and chardev */
201 qmp_chardev_remove("mux-label", &error_abort);
202 qmp_chardev_remove("mux-label-base", &error_abort);
203
204 opts = qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
205 1, &error_abort);
206 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
207 qemu_opt_set(opts, "size", "128", &error_abort);
208 qemu_opt_set(opts, "mux", "on", &error_abort);
209 chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
210 g_assert_nonnull(chr);
211 qemu_opts_del(opts);
212
213 qemu_chr_fe_init(&chr_fe1, chr, &error_abort);
214 qemu_chr_fe_set_handlers(&chr_fe1,
215 fe_can_read,
216 fe_read,
217 fe_event,
218 NULL,
219 &h1,
220 NULL, true);
221
222 qemu_chr_fe_init(&chr_fe2, chr, &error_abort);
223 qemu_chr_fe_set_handlers(&chr_fe2,
224 fe_can_read,
225 fe_read,
226 fe_event,
227 NULL,
228 &h2,
229 NULL, true);
230 qemu_chr_fe_take_focus(&chr_fe2);
231
232 base = qemu_chr_find("mux-label-base");
233 g_assert_cmpint(qemu_chr_be_can_write(base), !=, 0);
234
235 qemu_chr_be_write(base, (void *)"hello", 6);
236 g_assert_cmpint(h1.read_count, ==, 0);
237 g_assert_cmpint(h2.read_count, ==, 6);
238 g_assert_cmpstr(h2.read_buf, ==, "hello");
239 h2.read_count = 0;
240
241 g_assert_cmpint(h1.last_event, !=, 42); /* should be MUX_OUT or OPENED */
242 g_assert_cmpint(h2.last_event, !=, 42); /* should be MUX_IN or OPENED */
243 /* sending event on the base broadcast to all fe, historical reasons? */
244 qemu_chr_be_event(base, 42);
245 g_assert_cmpint(h1.last_event, ==, 42);
246 g_assert_cmpint(h2.last_event, ==, 42);
247 qemu_chr_be_event(chr, -1);
248 g_assert_cmpint(h1.last_event, ==, 42);
249 g_assert_cmpint(h2.last_event, ==, -1);
250
251 /* switch focus */
252 qemu_chr_be_write(base, (void *)"\1b", 2);
253 g_assert_cmpint(h1.last_event, ==, 42);
254 g_assert_cmpint(h2.last_event, ==, CHR_EVENT_BREAK);
255
256 qemu_chr_be_write(base, (void *)"\1c", 2);
257 g_assert_cmpint(h1.last_event, ==, CHR_EVENT_MUX_IN);
258 g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT);
259 qemu_chr_be_event(chr, -1);
260 g_assert_cmpint(h1.last_event, ==, -1);
261 g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT);
262
263 qemu_chr_be_write(base, (void *)"hello", 6);
264 g_assert_cmpint(h2.read_count, ==, 0);
265 g_assert_cmpint(h1.read_count, ==, 6);
266 g_assert_cmpstr(h1.read_buf, ==, "hello");
267 h1.read_count = 0;
268
269 qemu_chr_be_write(base, (void *)"\1b", 2);
270 g_assert_cmpint(h1.last_event, ==, CHR_EVENT_BREAK);
271 g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT);
272
273 /* open/close state and corresponding events */
274 g_assert_true(qemu_chr_fe_backend_open(&chr_fe1));
275 g_assert_true(qemu_chr_fe_backend_open(&chr_fe2));
276 g_assert_true(h1.is_open);
277 g_assert_false(h1.openclose_mismatch);
278 g_assert_true(h2.is_open);
279 g_assert_false(h2.openclose_mismatch);
280
281 h1.openclose_count = h2.openclose_count = 0;
282
283 qemu_chr_fe_set_handlers(&chr_fe1, NULL, NULL, NULL, NULL,
284 NULL, NULL, false);
285 qemu_chr_fe_set_handlers(&chr_fe2, NULL, NULL, NULL, NULL,
286 NULL, NULL, false);
287 g_assert_cmpint(h1.openclose_count, ==, 0);
288 g_assert_cmpint(h2.openclose_count, ==, 0);
289
290 h1.is_open = h2.is_open = false;
291 qemu_chr_fe_set_handlers(&chr_fe1,
292 NULL,
293 NULL,
294 fe_event,
295 NULL,
296 &h1,
297 NULL, false);
298 qemu_chr_fe_set_handlers(&chr_fe2,
299 NULL,
300 NULL,
301 fe_event,
302 NULL,
303 &h2,
304 NULL, false);
305 g_assert_cmpint(h1.openclose_count, ==, 1);
306 g_assert_false(h1.openclose_mismatch);
307 g_assert_cmpint(h2.openclose_count, ==, 1);
308 g_assert_false(h2.openclose_mismatch);
309
310 qemu_chr_be_event(base, CHR_EVENT_CLOSED);
311 qemu_chr_be_event(base, CHR_EVENT_OPENED);
312 g_assert_cmpint(h1.openclose_count, ==, 3);
313 g_assert_false(h1.openclose_mismatch);
314 g_assert_cmpint(h2.openclose_count, ==, 3);
315 g_assert_false(h2.openclose_mismatch);
316
317 qemu_chr_fe_set_handlers(&chr_fe2,
318 fe_can_read,
319 fe_read,
320 fe_event,
321 NULL,
322 &h2,
323 NULL, false);
324 qemu_chr_fe_set_handlers(&chr_fe1,
325 fe_can_read,
326 fe_read,
327 fe_event,
328 NULL,
329 &h1,
330 NULL, false);
331
332 /* remove first handler */
333 qemu_chr_fe_set_handlers(&chr_fe1, NULL, NULL, NULL, NULL,
334 NULL, NULL, true);
335 qemu_chr_be_write(base, (void *)"hello", 6);
336 g_assert_cmpint(h1.read_count, ==, 0);
337 g_assert_cmpint(h2.read_count, ==, 0);
338
339 qemu_chr_be_write(base, (void *)"\1c", 2);
340 qemu_chr_be_write(base, (void *)"hello", 6);
341 g_assert_cmpint(h1.read_count, ==, 0);
342 g_assert_cmpint(h2.read_count, ==, 6);
343 g_assert_cmpstr(h2.read_buf, ==, "hello");
344 h2.read_count = 0;
345
346 /* print help */
347 qemu_chr_be_write(base, (void *)"\1?", 2);
348 data = qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort);
349 g_assert_cmpint(strlen(data), !=, 0);
350 g_free(data);
351
352 qemu_chr_fe_deinit(&chr_fe1, false);
353
354 qmp_chardev_remove("mux-label", &error);
355 g_assert_cmpstr(error_get_pretty(error), ==, "Chardev 'mux-label' is busy");
356 error_free(error);
357
358 qemu_chr_fe_deinit(&chr_fe2, false);
359 qmp_chardev_remove("mux-label", &error_abort);
360 }
361
362 static void char_hub_test(void)
363 {
364 QemuOpts *opts;
365 Chardev *hub, *chr1, *chr2, *base;
366 char *data;
367 FeHandler h = { 0, false, 0, false, };
368 Error *error = NULL;
369 CharFrontend chr_fe;
370 int ret, i;
371
372 #define RB_SIZE 128
373
374 /*
375 * Create invalid hub
376 * 1. Create hub without a 'chardevs.N' defined (expect error)
377 */
378 opts = qemu_opts_create(qemu_find_opts("chardev"), "hub0",
379 1, &error_abort);
380 qemu_opt_set(opts, "backend", "hub", &error_abort);
381 hub = qemu_chr_new_from_opts(opts, NULL, &error);
382 g_assert_cmpstr(error_get_pretty(error), ==,
383 "hub: 'chardevs' list is not defined");
384 error_free(error);
385 error = NULL;
386 qemu_opts_del(opts);
387
388 /*
389 * Create invalid hub
390 * 1. Create chardev with embedded mux: 'mux=on'
391 * 2. Create hub which refers mux
392 * 3. Create hub which refers chardev already attached
393 * to the mux (already in use, expect error)
394 */
395 opts = qemu_opts_create(qemu_find_opts("chardev"), "chr0",
396 1, &error_abort);
397 qemu_opt_set(opts, "mux", "on", &error_abort);
398 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
399 qemu_opt_set(opts, "size", stringify(RB_SIZE), &error_abort);
400 base = qemu_chr_new_from_opts(opts, NULL, &error_abort);
401 g_assert_nonnull(base);
402 qemu_opts_del(opts);
403
404 opts = qemu_opts_create(qemu_find_opts("chardev"), "hub0",
405 1, &error_abort);
406 qemu_opt_set(opts, "backend", "hub", &error_abort);
407 qemu_opt_set(opts, "chardevs.0", "chr0", &error_abort);
408 hub = qemu_chr_new_from_opts(opts, NULL, &error);
409 g_assert_cmpstr(error_get_pretty(error), ==,
410 "hub: multiplexers and hub devices can't be "
411 "stacked, check chardev 'chr0', chardev should "
412 "not be a hub device or have 'mux=on' enabled");
413 error_free(error);
414 error = NULL;
415 qemu_opts_del(opts);
416
417 opts = qemu_opts_create(qemu_find_opts("chardev"), "hub0",
418 1, &error_abort);
419 qemu_opt_set(opts, "backend", "hub", &error_abort);
420 qemu_opt_set(opts, "chardevs.0", "chr0-base", &error_abort);
421 hub = qemu_chr_new_from_opts(opts, NULL, &error);
422 g_assert_cmpstr(error_get_pretty(error), ==,
423 "chardev 'chr0-base' is already in use");
424 error_free(error);
425 error = NULL;
426 qemu_opts_del(opts);
427
428 /* Finalize chr0 */
429 qmp_chardev_remove("chr0", &error_abort);
430
431 /*
432 * Create invalid hub with more than maximum allowed backends
433 * 1. Create more than maximum allowed 'chardevs.%d' options for
434 * hub (expect error)
435 */
436 opts = qemu_opts_create(qemu_find_opts("chardev"), "hub0",
437 1, &error_abort);
438 for (i = 0; i < 10; i++) {
439 char key[32], val[32];
440
441 snprintf(key, sizeof(key), "chardevs.%d", i);
442 snprintf(val, sizeof(val), "chr%d", i);
443 qemu_opt_set(opts, key, val, &error);
444 if (error) {
445 char buf[64];
446
447 snprintf(buf, sizeof(buf), "Invalid parameter 'chardevs.%d'", i);
448 g_assert_cmpstr(error_get_pretty(error), ==, buf);
449 error_free(error);
450 break;
451 }
452 }
453 g_assert_nonnull(error);
454 error = NULL;
455 qemu_opts_del(opts);
456
457 /*
458 * Create hub with 2 backend chardevs and 1 frontend and perform
459 * data aggregation
460 * 1. Create 2 ringbuf backend chardevs
461 * 2. Create 1 frontend
462 * 3. Create hub which refers 2 backend chardevs
463 * 4. Attach hub to a frontend
464 * 5. Attach hub to a frontend second time (expect error)
465 * 6. Perform data aggregation
466 * 7. Remove chr1 ("chr1 is busy", expect error)
467 * 8. Remove hub0 ("hub0 is busy", expect error);
468 * 9. Finilize frontend, hub and backend chardevs in correct order
469 */
470
471 /* Create first chardev */
472 opts = qemu_opts_create(qemu_find_opts("chardev"), "chr1",
473 1, &error_abort);
474 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
475 qemu_opt_set(opts, "size", stringify(RB_SIZE), &error_abort);
476 chr1 = qemu_chr_new_from_opts(opts, NULL, &error_abort);
477 g_assert_nonnull(chr1);
478 qemu_opts_del(opts);
479
480 /* Create second chardev */
481 opts = qemu_opts_create(qemu_find_opts("chardev"), "chr2",
482 1, &error_abort);
483 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
484 qemu_opt_set(opts, "size", stringify(RB_SIZE), &error_abort);
485 chr2 = qemu_chr_new_from_opts(opts, NULL, &error_abort);
486 g_assert_nonnull(chr2);
487 qemu_opts_del(opts);
488
489 /* Create hub0 and refer 2 backend chardevs */
490 opts = qemu_opts_create(qemu_find_opts("chardev"), "hub0",
491 1, &error_abort);
492 qemu_opt_set(opts, "backend", "hub", &error_abort);
493 qemu_opt_set(opts, "chardevs.0", "chr1", &error_abort);
494 qemu_opt_set(opts, "chardevs.1", "chr2", &error_abort);
495 hub = qemu_chr_new_from_opts(opts, NULL, &error_abort);
496 g_assert_nonnull(hub);
497 qemu_opts_del(opts);
498
499 /* Attach hub to a frontend */
500 qemu_chr_fe_init(&chr_fe, hub, &error_abort);
501 qemu_chr_fe_set_handlers(&chr_fe,
502 fe_can_read,
503 fe_read,
504 fe_event,
505 NULL,
506 &h,
507 NULL, true);
508
509 /* Fails second time */
510 qemu_chr_fe_init(&chr_fe, hub, &error);
511 g_assert_cmpstr(error_get_pretty(error), ==, "chardev 'hub0' is already in use");
512 error_free(error);
513 error = NULL;
514
515 /* Write to backend, chr1 */
516 base = qemu_chr_find("chr1");
517 g_assert_cmpint(qemu_chr_be_can_write(base), !=, 0);
518
519 qemu_chr_be_write(base, (void *)"hello", 6);
520 g_assert_cmpint(h.read_count, ==, 6);
521 g_assert_cmpstr(h.read_buf, ==, "hello");
522 h.read_count = 0;
523
524 /* Write to backend, chr2 */
525 base = qemu_chr_find("chr2");
526 g_assert_cmpint(qemu_chr_be_can_write(base), !=, 0);
527
528 qemu_chr_be_write(base, (void *)"olleh", 6);
529 g_assert_cmpint(h.read_count, ==, 6);
530 g_assert_cmpstr(h.read_buf, ==, "olleh");
531 h.read_count = 0;
532
533 /* Write to frontend, chr_be */
534 ret = qemu_chr_fe_write(&chr_fe, (void *)"heyhey", 6);
535 g_assert_cmpint(ret, ==, 6);
536
537 data = qmp_ringbuf_read("chr1", RB_SIZE, false, 0, &error_abort);
538 g_assert_cmpint(strlen(data), ==, 6);
539 g_assert_cmpstr(data, ==, "heyhey");
540 g_free(data);
541
542 data = qmp_ringbuf_read("chr2", RB_SIZE, false, 0, &error_abort);
543 g_assert_cmpint(strlen(data), ==, 6);
544 g_assert_cmpstr(data, ==, "heyhey");
545 g_free(data);
546
547 /* Can't be removed, depends on hub0 */
548 qmp_chardev_remove("chr1", &error);
549 g_assert_cmpstr(error_get_pretty(error), ==, "Chardev 'chr1' is busy");
550 error_free(error);
551 error = NULL;
552
553 /* Can't be removed, depends on frontend chr_be */
554 qmp_chardev_remove("hub0", &error);
555 g_assert_cmpstr(error_get_pretty(error), ==, "Chardev 'hub0' is busy");
556 error_free(error);
557 error = NULL;
558
559 /* Finalize frontend */
560 qemu_chr_fe_deinit(&chr_fe, false);
561
562 /* Finalize hub0 */
563 qmp_chardev_remove("hub0", &error_abort);
564
565 /* Finalize backend chardevs */
566 qmp_chardev_remove("chr1", &error_abort);
567 qmp_chardev_remove("chr2", &error_abort);
568
569 #ifndef _WIN32
570 /*
571 * Create 3 backend chardevs to simulate EAGAIN and watcher.
572 * Mainly copied from char_pipe_test().
573 * 1. Create 2 ringbuf backend chardevs
574 * 2. Create 1 pipe backend chardev
575 * 3. Create 1 frontend
576 * 4. Create hub which refers 2 backend chardevs
577 * 5. Attach hub to a frontend
578 * 6. Perform data aggregation and check watcher
579 * 7. Finilize frontend, hub and backend chardevs in correct order
580 */
581 {
582 gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
583 gchar *in, *out, *pipe = g_build_filename(tmp_path, "pipe", NULL);
584 Chardev *chr3;
585 int fd, len;
586 char buf[128];
587
588 in = g_strdup_printf("%s.in", pipe);
589 if (mkfifo(in, 0600) < 0) {
590 abort();
591 }
592 out = g_strdup_printf("%s.out", pipe);
593 if (mkfifo(out, 0600) < 0) {
594 abort();
595 }
596
597 /* Create first chardev */
598 opts = qemu_opts_create(qemu_find_opts("chardev"), "chr1",
599 1, &error_abort);
600 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
601 qemu_opt_set(opts, "size", stringify(RB_SIZE), &error_abort);
602 chr1 = qemu_chr_new_from_opts(opts, NULL, &error_abort);
603 g_assert_nonnull(chr1);
604 qemu_opts_del(opts);
605
606 /* Create second chardev */
607 opts = qemu_opts_create(qemu_find_opts("chardev"), "chr2",
608 1, &error_abort);
609 qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
610 qemu_opt_set(opts, "size", stringify(RB_SIZE), &error_abort);
611 chr2 = qemu_chr_new_from_opts(opts, NULL, &error_abort);
612 g_assert_nonnull(chr2);
613 qemu_opts_del(opts);
614
615 /* Create third chardev */
616 opts = qemu_opts_create(qemu_find_opts("chardev"), "chr3",
617 1, &error_abort);
618 qemu_opt_set(opts, "backend", "pipe", &error_abort);
619 qemu_opt_set(opts, "path", pipe, &error_abort);
620 chr3 = qemu_chr_new_from_opts(opts, NULL, &error_abort);
621 g_assert_nonnull(chr3);
622
623 /* Create hub0 and refer 3 backend chardevs */
624 opts = qemu_opts_create(qemu_find_opts("chardev"), "hub0",
625 1, &error_abort);
626 qemu_opt_set(opts, "backend", "hub", &error_abort);
627 qemu_opt_set(opts, "chardevs.0", "chr1", &error_abort);
628 qemu_opt_set(opts, "chardevs.1", "chr2", &error_abort);
629 qemu_opt_set(opts, "chardevs.2", "chr3", &error_abort);
630 hub = qemu_chr_new_from_opts(opts, NULL, &error_abort);
631 g_assert_nonnull(hub);
632 qemu_opts_del(opts);
633
634 /* Attach hub to a frontend */
635 qemu_chr_fe_init(&chr_fe, hub, &error_abort);
636 qemu_chr_fe_set_handlers(&chr_fe,
637 fe_can_read,
638 fe_read,
639 fe_event,
640 NULL,
641 &h,
642 NULL, true);
643
644 /* Write to frontend, chr_be */
645 ret = qemu_chr_fe_write(&chr_fe, (void *)"thisis", 6);
646 g_assert_cmpint(ret, ==, 6);
647
648 data = qmp_ringbuf_read("chr1", RB_SIZE, false, 0, &error_abort);
649 g_assert_cmpint(strlen(data), ==, 6);
650 g_assert_cmpstr(data, ==, "thisis");
651 g_free(data);
652
653 data = qmp_ringbuf_read("chr2", RB_SIZE, false, 0, &error_abort);
654 g_assert_cmpint(strlen(data), ==, 6);
655 g_assert_cmpstr(data, ==, "thisis");
656 g_free(data);
657
658 fd = open(out, O_RDWR);
659 ret = read(fd, buf, sizeof(buf));
660 g_assert_cmpint(ret, ==, 6);
661 buf[ret] = 0;
662 g_assert_cmpstr(buf, ==, "thisis");
663 close(fd);
664
665 /* Add watch. 0 indicates no watches if nothing to wait for */
666 ret = qemu_chr_fe_add_watch(&chr_fe, G_IO_OUT | G_IO_HUP,
667 NULL, NULL);
668 g_assert_cmpint(ret, ==, 0);
669
670 /*
671 * Write to frontend, chr_be, until EAGAIN. Make sure length is
672 * power of two to fit nicely the whole pipe buffer.
673 */
674 len = 0;
675 while ((ret = qemu_chr_fe_write(&chr_fe, (void *)"thisisit", 8))
676 != -1) {
677 len += ret;
678 }
679 g_assert_cmpint(errno, ==, EAGAIN);
680
681 /* Further all writes should cause EAGAIN */
682 ret = qemu_chr_fe_write(&chr_fe, (void *)"b", 1);
683 g_assert_cmpint(ret, ==, -1);
684 g_assert_cmpint(errno, ==, EAGAIN);
685
686 /*
687 * Add watch. Non 0 indicates we have a blocked chardev, which
688 * can wakes us up when write is possible.
689 */
690 ret = qemu_chr_fe_add_watch(&chr_fe, G_IO_OUT | G_IO_HUP,
691 NULL, NULL);
692 g_assert_cmpint(ret, !=, 0);
693 g_source_remove(ret);
694
695 /* Drain pipe and ring buffers */
696 fd = open(out, O_RDWR);
697 while ((ret = read(fd, buf, MIN(sizeof(buf), len))) != -1 && len > 0) {
698 len -= ret;
699 }
700 close(fd);
701
702 data = qmp_ringbuf_read("chr1", RB_SIZE, false, 0, &error_abort);
703 g_assert_cmpint(strlen(data), ==, 128);
704 g_free(data);
705
706 data = qmp_ringbuf_read("chr2", RB_SIZE, false, 0, &error_abort);
707 g_assert_cmpint(strlen(data), ==, 128);
708 g_free(data);
709
710 /*
711 * Now we are good to go, first repeat "lost" sequence, which
712 * was already consumed and drained by the ring buffers, but
713 * the pipe has not received it yet.
714 */
715 ret = qemu_chr_fe_write(&chr_fe, (void *)"thisisit", 8);
716 g_assert_cmpint(ret, ==, 8);
717
718 ret = qemu_chr_fe_write(&chr_fe, (void *)"streamisrestored", 16);
719 g_assert_cmpint(ret, ==, 16);
720
721 data = qmp_ringbuf_read("chr1", RB_SIZE, false, 0, &error_abort);
722 g_assert_cmpint(strlen(data), ==, 16);
723 /* Only last 16 bytes, see big comment above */
724 g_assert_cmpstr(data, ==, "streamisrestored");
725 g_free(data);
726
727 data = qmp_ringbuf_read("chr2", RB_SIZE, false, 0, &error_abort);
728 g_assert_cmpint(strlen(data), ==, 16);
729 /* Only last 16 bytes, see big comment above */
730 g_assert_cmpstr(data, ==, "streamisrestored");
731 g_free(data);
732
733 fd = open(out, O_RDWR);
734 ret = read(fd, buf, sizeof(buf));
735 g_assert_cmpint(ret, ==, 24);
736 buf[ret] = 0;
737 /* Both 8 and 16 bytes */
738 g_assert_cmpstr(buf, ==, "thisisitstreamisrestored");
739 close(fd);
740
741 g_free(in);
742 g_free(out);
743 g_free(tmp_path);
744 g_free(pipe);
745
746 /* Finalize frontend */
747 qemu_chr_fe_deinit(&chr_fe, false);
748
749 /* Finalize hub0 */
750 qmp_chardev_remove("hub0", &error_abort);
751
752 /* Finalize backend chardevs */
753 qmp_chardev_remove("chr1", &error_abort);
754 qmp_chardev_remove("chr2", &error_abort);
755 qmp_chardev_remove("chr3", &error_abort);
756 }
757 #endif
758 }
759
760 static void websock_server_read(void *opaque, const uint8_t *buf, int size)
761 {
762 g_assert_cmpint(size, ==, 5);
763 g_assert(memcmp(buf, "world", size) == 0);
764 quit = true;
765 }
766
767
768 static int websock_server_can_read(void *opaque)
769 {
770 return 10;
771 }
772
773
774 static bool websock_check_http_headers(char *buf, int size)
775 {
776 int i;
777 const char *ans[] = { "HTTP/1.1 101 Switching Protocols\r\n",
778 "Server: QEMU VNC\r\n",
779 "Upgrade: websocket\r\n",
780 "Connection: Upgrade\r\n",
781 "Sec-WebSocket-Accept:",
782 "Sec-WebSocket-Protocol: binary\r\n" };
783
784 for (i = 0; i < 6; i++) {
785 if (g_strstr_len(buf, size, ans[i]) == NULL) {
786 return false;
787 }
788 }
789
790 return true;
791 }
792
793
794 static void websock_client_read(void *opaque, const uint8_t *buf, int size)
795 {
796 const uint8_t ping[] = { 0x89, 0x85, /* Ping header */
797 0x07, 0x77, 0x9e, 0xf9, /* Masking key */
798 0x6f, 0x12, 0xf2, 0x95, 0x68 /* "hello" */ };
799
800 const uint8_t binary[] = { 0x82, 0x85, /* Binary header */
801 0x74, 0x90, 0xb9, 0xdf, /* Masking key */
802 0x03, 0xff, 0xcb, 0xb3, 0x10 /* "world" */ };
803 Chardev *chr_client = opaque;
804
805 if (websock_check_http_headers((char *) buf, size)) {
806 qemu_chr_fe_write(chr_client->fe, ping, sizeof(ping));
807 } else if (buf[0] == 0x8a && buf[1] == 0x05) {
808 g_assert(strncmp((char *) buf + 2, "hello", 5) == 0);
809 qemu_chr_fe_write(chr_client->fe, binary, sizeof(binary));
810 } else {
811 g_assert(buf[0] == 0x88 && buf[1] == 0x16);
812 g_assert(strncmp((char *) buf + 4, "peer requested close", 10) == 0);
813 quit = true;
814 }
815 }
816
817
818 static int websock_client_can_read(void *opaque)
819 {
820 return 4096;
821 }
822
823
824 static void char_websock_test(void)
825 {
826 QObject *addr;
827 QDict *qdict;
828 const char *port;
829 char *tmp;
830 char *handshake_port;
831 CharFrontend fe;
832 CharFrontend client_fe;
833 Chardev *chr_client;
834 Chardev *chr = qemu_chr_new("server",
835 "websocket:127.0.0.1:0,server=on,wait=off", NULL);
836 const char handshake[] = "GET / HTTP/1.1\r\n"
837 "Upgrade: websocket\r\n"
838 "Connection: Upgrade\r\n"
839 "Host: localhost:%s\r\n"
840 "Origin: http://localhost:%s\r\n"
841 "Sec-WebSocket-Key: o9JHNiS3/0/0zYE1wa3yIw==\r\n"
842 "Sec-WebSocket-Version: 13\r\n"
843 "Sec-WebSocket-Protocol: binary\r\n\r\n";
844 const uint8_t close[] = { 0x88, 0x82, /* Close header */
845 0xef, 0xaa, 0xc5, 0x97, /* Masking key */
846 0xec, 0x42 /* Status code */ };
847
848 addr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
849 qdict = qobject_to(QDict, addr);
850 port = qdict_get_str(qdict, "port");
851 tmp = g_strdup_printf("tcp:127.0.0.1:%s", port);
852 handshake_port = g_strdup_printf(handshake, port, port);
853 qobject_unref(qdict);
854
855 qemu_chr_fe_init(&fe, chr, &error_abort);
856 qemu_chr_fe_set_handlers(&fe, websock_server_can_read, websock_server_read,
857 NULL, NULL, chr, NULL, true);
858
859 chr_client = qemu_chr_new("client", tmp, NULL);
860 qemu_chr_fe_init(&client_fe, chr_client, &error_abort);
861 qemu_chr_fe_set_handlers(&client_fe, websock_client_can_read,
862 websock_client_read,
863 NULL, NULL, chr_client, NULL, true);
864 g_free(tmp);
865
866 qemu_chr_write_all(chr_client,
867 (uint8_t *) handshake_port,
868 strlen(handshake_port));
869 g_free(handshake_port);
870 main_loop();
871
872 g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
873 g_assert(object_property_get_bool(OBJECT(chr_client),
874 "connected", &error_abort));
875
876 qemu_chr_write_all(chr_client, close, sizeof(close));
877 main_loop();
878
879 object_unparent(OBJECT(chr_client));
880 object_unparent(OBJECT(chr));
881 }
882
883
884 #ifndef _WIN32
885 static void char_pipe_test(void)
886 {
887 gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
888 gchar *tmp, *in, *out, *pipe = g_build_filename(tmp_path, "pipe", NULL);
889 Chardev *chr;
890 CharFrontend c;
891 int ret, fd;
892 char buf[10];
893 FeHandler fe = { 0, };
894
895 in = g_strdup_printf("%s.in", pipe);
896 if (mkfifo(in, 0600) < 0) {
897 abort();
898 }
899 out = g_strdup_printf("%s.out", pipe);
900 if (mkfifo(out, 0600) < 0) {
901 abort();
902 }
903
904 tmp = g_strdup_printf("pipe:%s", pipe);
905 chr = qemu_chr_new("pipe", tmp, NULL);
906 g_assert_nonnull(chr);
907 g_free(tmp);
908
909 qemu_chr_fe_init(&c, chr, &error_abort);
910
911 ret = qemu_chr_fe_write(&c, (void *)"pipe-out", 9);
912 g_assert_cmpint(ret, ==, 9);
913
914 fd = open(out, O_RDWR);
915 ret = read(fd, buf, sizeof(buf));
916 g_assert_cmpint(ret, ==, 9);
917 g_assert_cmpstr(buf, ==, "pipe-out");
918 close(fd);
919
920 fd = open(in, O_WRONLY);
921 ret = write(fd, "pipe-in", 8);
922 g_assert_cmpint(ret, ==, 8);
923 close(fd);
924
925 qemu_chr_fe_set_handlers(&c,
926 fe_can_read,
927 fe_read,
928 fe_event,
929 NULL,
930 &fe,
931 NULL, true);
932
933 main_loop();
934
935 g_assert_cmpint(fe.read_count, ==, 8);
936 g_assert_cmpstr(fe.read_buf, ==, "pipe-in");
937
938 qemu_chr_fe_deinit(&c, true);
939
940 g_assert(g_unlink(in) == 0);
941 g_assert(g_unlink(out) == 0);
942 g_assert(g_rmdir(tmp_path) == 0);
943 g_free(in);
944 g_free(out);
945 g_free(tmp_path);
946 g_free(pipe);
947 }
948 #endif
949
950 typedef struct SocketIdleData {
951 GMainLoop *loop;
952 Chardev *chr;
953 bool conn_expected;
954 CharFrontend *fe;
955 CharFrontend *client_fe;
956 } SocketIdleData;
957
958
959 static void socket_read_hello(void *opaque, const uint8_t *buf, int size)
960 {
961 g_assert_cmpint(size, ==, 5);
962 g_assert(strncmp((char *)buf, "hello", 5) == 0);
963
964 quit = true;
965 }
966
967 static int socket_can_read_hello(void *opaque)
968 {
969 return 10;
970 }
971
972 static int make_udp_socket(int *port)
973 {
974 struct sockaddr_in addr = { 0, };
975 socklen_t alen = sizeof(addr);
976 int ret, sock = qemu_socket(PF_INET, SOCK_DGRAM, 0);
977
978 g_assert_cmpint(sock, >=, 0);
979 addr.sin_family = AF_INET ;
980 addr.sin_addr.s_addr = htonl(INADDR_ANY);
981 addr.sin_port = 0;
982 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
983 g_assert_cmpint(ret, ==, 0);
984 ret = getsockname(sock, (struct sockaddr *)&addr, &alen);
985 g_assert_cmpint(ret, ==, 0);
986
987 *port = ntohs(addr.sin_port);
988 return sock;
989 }
990
991 static void char_udp_test_internal(Chardev *reuse_chr, int sock)
992 {
993 struct sockaddr_in other;
994 SocketIdleData d = { 0, };
995 Chardev *chr;
996 CharFrontend stack_fe, *fe = &stack_fe;
997 socklen_t alen = sizeof(other);
998 int ret;
999 char buf[10];
1000 char *tmp = NULL;
1001
1002 if (reuse_chr) {
1003 chr = reuse_chr;
1004 fe = chr->fe;
1005 } else {
1006 int port;
1007 sock = make_udp_socket(&port);
1008 tmp = g_strdup_printf("udp:127.0.0.1:%d", port);
1009 chr = qemu_chr_new("client", tmp, NULL);
1010 g_assert_nonnull(chr);
1011
1012 qemu_chr_fe_init(fe, chr, &error_abort);
1013 }
1014
1015 g_assert(chr->be_open);
1016
1017 d.chr = chr;
1018 qemu_chr_fe_set_handlers(fe, socket_can_read_hello, socket_read_hello,
1019 NULL, NULL, &d, NULL, true);
1020 ret = qemu_chr_write_all(chr, (uint8_t *)"hello", 5);
1021 g_assert_cmpint(ret, ==, 5);
1022
1023 ret = recvfrom(sock, buf, sizeof(buf), 0,
1024 (struct sockaddr *)&other, &alen);
1025 g_assert_cmpint(ret, ==, 5);
1026 ret = sendto(sock, buf, 5, 0, (struct sockaddr *)&other, alen);
1027 g_assert_cmpint(ret, ==, 5);
1028
1029 main_loop();
1030
1031 if (!reuse_chr) {
1032 close(sock);
1033 qemu_chr_fe_deinit(fe, true);
1034 }
1035 g_free(tmp);
1036 }
1037
1038 static void char_udp_test(void)
1039 {
1040 char_udp_test_internal(NULL, 0);
1041 }
1042
1043
1044 typedef struct {
1045 int event;
1046 bool got_pong;
1047 CharFrontend *fe;
1048 } CharSocketTestData;
1049
1050
1051 #define SOCKET_PING "Hello"
1052 #define SOCKET_PONG "World"
1053
1054 typedef void (*char_socket_cb)(void *opaque, QEMUChrEvent event);
1055
1056 static void
1057 char_socket_event(void *opaque, QEMUChrEvent event)
1058 {
1059 CharSocketTestData *data = opaque;
1060 data->event = event;
1061 }
1062
1063 static void
1064 char_socket_event_with_error(void *opaque, QEMUChrEvent event)
1065 {
1066 static bool first_error;
1067 CharSocketTestData *data = opaque;
1068 CharFrontend *fe = data->fe;
1069 data->event = event;
1070 switch (event) {
1071 case CHR_EVENT_OPENED:
1072 if (!first_error) {
1073 first_error = true;
1074 qemu_chr_fe_disconnect(fe);
1075 }
1076 return;
1077 case CHR_EVENT_CLOSED:
1078 return;
1079 default:
1080 return;
1081 }
1082 }
1083
1084
1085 static void
1086 char_socket_read(void *opaque, const uint8_t *buf, int size)
1087 {
1088 CharSocketTestData *data = opaque;
1089 g_assert_cmpint(size, ==, sizeof(SOCKET_PONG));
1090 g_assert(memcmp(buf, SOCKET_PONG, size) == 0);
1091 data->got_pong = true;
1092 }
1093
1094
1095 static int
1096 char_socket_can_read(void *opaque)
1097 {
1098 return sizeof(SOCKET_PONG);
1099 }
1100
1101
1102 static char *
1103 char_socket_addr_to_opt_str(SocketAddress *addr, bool fd_pass,
1104 const char *reconnect, bool is_listen)
1105 {
1106 if (fd_pass) {
1107 QIOChannelSocket *ioc = qio_channel_socket_new();
1108 int fd;
1109 char *optstr;
1110 g_assert(!reconnect);
1111 if (is_listen) {
1112 qio_channel_socket_listen_sync(ioc, addr, 1, &error_abort);
1113 } else {
1114 qio_channel_socket_connect_sync(ioc, addr, &error_abort);
1115 }
1116 fd = ioc->fd;
1117 ioc->fd = -1;
1118 optstr = g_strdup_printf("socket,id=cdev0,fd=%d%s",
1119 fd, is_listen ? ",server=on,wait=off" : "");
1120 object_unref(OBJECT(ioc));
1121 return optstr;
1122 } else {
1123 switch (addr->type) {
1124 case SOCKET_ADDRESS_TYPE_INET:
1125 return g_strdup_printf("socket,id=cdev0,host=%s,port=%s%s%s",
1126 addr->u.inet.host,
1127 addr->u.inet.port,
1128 reconnect ? reconnect : "",
1129 is_listen ? ",server=on,wait=off" : "");
1130
1131 case SOCKET_ADDRESS_TYPE_UNIX:
1132 return g_strdup_printf("socket,id=cdev0,path=%s%s%s",
1133 addr->u.q_unix.path,
1134 reconnect ? reconnect : "",
1135 is_listen ? ",server=on,wait=off" : "");
1136
1137 default:
1138 g_assert_not_reached();
1139 }
1140 }
1141 }
1142
1143
1144 static int
1145 char_socket_ping_pong(QIOChannel *ioc, Error **errp)
1146 {
1147 char greeting[sizeof(SOCKET_PING)];
1148 const char *response = SOCKET_PONG;
1149
1150 int ret;
1151 ret = qio_channel_read_all(ioc, greeting, sizeof(greeting), errp);
1152 if (ret != 0) {
1153 object_unref(OBJECT(ioc));
1154 return -1;
1155 }
1156
1157 g_assert(memcmp(greeting, SOCKET_PING, sizeof(greeting)) == 0);
1158
1159 qio_channel_write_all(ioc, response, sizeof(SOCKET_PONG), errp);
1160 object_unref(OBJECT(ioc));
1161 return 0;
1162 }
1163
1164
1165 static gpointer
1166 char_socket_server_client_thread(gpointer data)
1167 {
1168 SocketAddress *addr = data;
1169 QIOChannelSocket *ioc = qio_channel_socket_new();
1170
1171 qio_channel_socket_connect_sync(ioc, addr, &error_abort);
1172
1173 char_socket_ping_pong(QIO_CHANNEL(ioc), &error_abort);
1174
1175 return NULL;
1176 }
1177
1178
1179 typedef struct {
1180 SocketAddress *addr;
1181 bool wait_connected;
1182 bool fd_pass;
1183 } CharSocketServerTestConfig;
1184
1185
1186 static void char_socket_server_test(gconstpointer opaque)
1187 {
1188 const CharSocketServerTestConfig *config = opaque;
1189 Chardev *chr;
1190 CharFrontend c = {0};
1191 CharSocketTestData data = {0};
1192 QObject *qaddr;
1193 SocketAddress *addr;
1194 Visitor *v;
1195 QemuThread thread;
1196 int ret;
1197 bool reconnected = false;
1198 char *optstr;
1199 QemuOpts *opts;
1200
1201 g_setenv("QTEST_SILENT_ERRORS", "1", 1);
1202 /*
1203 * We rely on config->addr containing "wait=off", otherwise
1204 * qemu_chr_new() will block until a client connects. We
1205 * can't spawn our client thread though, because until
1206 * qemu_chr_new() returns we don't know what TCP port was
1207 * allocated by the OS
1208 */
1209 optstr = char_socket_addr_to_opt_str(config->addr,
1210 config->fd_pass,
1211 NULL,
1212 true);
1213 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
1214 optstr, true);
1215 g_assert_nonnull(opts);
1216 chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
1217 qemu_opts_del(opts);
1218 g_assert_nonnull(chr);
1219 g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
1220
1221 qaddr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
1222 g_assert_nonnull(qaddr);
1223
1224 v = qobject_input_visitor_new(qaddr);
1225 visit_type_SocketAddress(v, "addr", &addr, &error_abort);
1226 visit_free(v);
1227 qobject_unref(qaddr);
1228
1229 qemu_chr_fe_init(&c, chr, &error_abort);
1230
1231 reconnect:
1232 data.event = -1;
1233 data.fe = &c;
1234 qemu_chr_fe_set_handlers(&c, NULL, NULL,
1235 char_socket_event, NULL,
1236 &data, NULL, true);
1237 g_assert(data.event == -1);
1238
1239 /*
1240 * Kick off a thread to act as the "remote" client
1241 * which just plays ping-pong with us
1242 */
1243 qemu_thread_create(&thread, "client",
1244 char_socket_server_client_thread,
1245 addr, QEMU_THREAD_JOINABLE);
1246 g_assert(data.event == -1);
1247
1248 if (config->wait_connected) {
1249 /* Synchronously accept a connection */
1250 qemu_chr_wait_connected(chr, &error_abort);
1251 } else {
1252 /*
1253 * Asynchronously accept a connection when the evnt
1254 * loop reports the listener socket as readable
1255 */
1256 while (data.event == -1) {
1257 main_loop_wait(false);
1258 }
1259 }
1260 g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
1261 g_assert(data.event == CHR_EVENT_OPENED);
1262 data.event = -1;
1263
1264 /* Send a greeting to the client */
1265 ret = qemu_chr_fe_write_all(&c, (const uint8_t *)SOCKET_PING,
1266 sizeof(SOCKET_PING));
1267 g_assert_cmpint(ret, ==, sizeof(SOCKET_PING));
1268 g_assert(data.event == -1);
1269
1270 /* Setup a callback to receive the reply to our greeting */
1271 qemu_chr_fe_set_handlers(&c, char_socket_can_read,
1272 char_socket_read,
1273 char_socket_event, NULL,
1274 &data, NULL, true);
1275 g_assert(data.event == CHR_EVENT_OPENED);
1276 data.event = -1;
1277
1278 /* Wait for the client to go away */
1279 while (data.event == -1) {
1280 main_loop_wait(false);
1281 }
1282 g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
1283 g_assert(data.event == CHR_EVENT_CLOSED);
1284 g_assert(data.got_pong);
1285
1286 qemu_thread_join(&thread);
1287
1288 if (!reconnected) {
1289 reconnected = true;
1290 goto reconnect;
1291 }
1292
1293 qapi_free_SocketAddress(addr);
1294 object_unparent(OBJECT(chr));
1295 g_free(optstr);
1296 g_unsetenv("QTEST_SILENT_ERRORS");
1297 }
1298
1299
1300 static gpointer
1301 char_socket_client_server_thread(gpointer data)
1302 {
1303 QIOChannelSocket *ioc = data;
1304 QIOChannelSocket *cioc;
1305
1306 retry:
1307 cioc = qio_channel_socket_accept(ioc, &error_abort);
1308 g_assert_nonnull(cioc);
1309
1310 if (char_socket_ping_pong(QIO_CHANNEL(cioc), NULL) != 0) {
1311 goto retry;
1312 }
1313
1314 return NULL;
1315 }
1316
1317
1318 typedef struct {
1319 SocketAddress *addr;
1320 const char *reconnect;
1321 bool wait_connected;
1322 bool fd_pass;
1323 char_socket_cb event_cb;
1324 } CharSocketClientTestConfig;
1325
1326 static void char_socket_client_dupid_test(gconstpointer opaque)
1327 {
1328 const CharSocketClientTestConfig *config = opaque;
1329 QIOChannelSocket *ioc;
1330 char *optstr;
1331 Chardev *chr1, *chr2;
1332 SocketAddress *addr;
1333 QemuOpts *opts;
1334 Error *local_err = NULL;
1335
1336 /*
1337 * Setup a listener socket and determine get its address
1338 * so we know the TCP port for the client later
1339 */
1340 ioc = qio_channel_socket_new();
1341 g_assert_nonnull(ioc);
1342 qio_channel_socket_listen_sync(ioc, config->addr, 1, &error_abort);
1343 addr = qio_channel_socket_get_local_address(ioc, &error_abort);
1344 g_assert_nonnull(addr);
1345
1346 /*
1347 * Populate the chardev address based on what the server
1348 * is actually listening on
1349 */
1350 optstr = char_socket_addr_to_opt_str(addr,
1351 config->fd_pass,
1352 config->reconnect,
1353 false);
1354
1355 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
1356 optstr, true);
1357 g_assert_nonnull(opts);
1358 chr1 = qemu_chr_new_from_opts(opts, NULL, &error_abort);
1359 g_assert_nonnull(chr1);
1360 qemu_chr_wait_connected(chr1, &error_abort);
1361
1362 chr2 = qemu_chr_new_from_opts(opts, NULL, &local_err);
1363 g_assert_null(chr2);
1364 error_free_or_abort(&local_err);
1365
1366 object_unref(OBJECT(ioc));
1367 qemu_opts_del(opts);
1368 object_unparent(OBJECT(chr1));
1369 qapi_free_SocketAddress(addr);
1370 g_free(optstr);
1371 }
1372
1373 static void char_socket_client_test(gconstpointer opaque)
1374 {
1375 const CharSocketClientTestConfig *config = opaque;
1376 const char_socket_cb event_cb = config->event_cb;
1377 QIOChannelSocket *ioc;
1378 char *optstr;
1379 Chardev *chr;
1380 CharFrontend c = {0};
1381 CharSocketTestData data = {0};
1382 SocketAddress *addr;
1383 QemuThread thread;
1384 int ret;
1385 bool reconnected = false;
1386 QemuOpts *opts;
1387
1388 /*
1389 * Setup a listener socket and determine get its address
1390 * so we know the TCP port for the client later
1391 */
1392 ioc = qio_channel_socket_new();
1393 g_assert_nonnull(ioc);
1394 qio_channel_socket_listen_sync(ioc, config->addr, 1, &error_abort);
1395 addr = qio_channel_socket_get_local_address(ioc, &error_abort);
1396 g_assert_nonnull(addr);
1397
1398 /*
1399 * Kick off a thread to act as the "remote" client
1400 * which just plays ping-pong with us
1401 */
1402 qemu_thread_create(&thread, "client",
1403 char_socket_client_server_thread,
1404 ioc, QEMU_THREAD_JOINABLE);
1405
1406 /*
1407 * Populate the chardev address based on what the server
1408 * is actually listening on
1409 */
1410 optstr = char_socket_addr_to_opt_str(addr,
1411 config->fd_pass,
1412 config->reconnect,
1413 false);
1414
1415 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
1416 optstr, true);
1417 g_assert_nonnull(opts);
1418 chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
1419 qemu_opts_del(opts);
1420 g_assert_nonnull(chr);
1421
1422 if (config->reconnect) {
1423 /*
1424 * If reconnect is set, the connection will be
1425 * established in a background thread and we won't
1426 * see the "connected" status updated until we
1427 * run the main event loop, or call qemu_chr_wait_connected
1428 */
1429 g_assert(!object_property_get_bool(OBJECT(chr), "connected",
1430 &error_abort));
1431 } else {
1432 g_assert(object_property_get_bool(OBJECT(chr), "connected",
1433 &error_abort));
1434 }
1435
1436 qemu_chr_fe_init(&c, chr, &error_abort);
1437
1438 reconnect:
1439 data.event = -1;
1440 data.fe = &c;
1441 qemu_chr_fe_set_handlers(&c, NULL, NULL,
1442 event_cb, NULL,
1443 &data, NULL, true);
1444 if (config->reconnect) {
1445 g_assert(data.event == -1);
1446 } else {
1447 g_assert(data.event == CHR_EVENT_OPENED);
1448 }
1449
1450 if (config->wait_connected) {
1451 /*
1452 * Synchronously wait for the connection to complete
1453 * This should be a no-op if reconnect is not set.
1454 */
1455 qemu_chr_wait_connected(chr, &error_abort);
1456 } else {
1457 /*
1458 * Asynchronously wait for the connection to be reported
1459 * as complete when the background thread reports its
1460 * status.
1461 * The loop will short-circuit if reconnect was set
1462 */
1463 while (data.event == -1) {
1464 main_loop_wait(false);
1465 }
1466 }
1467 g_assert(data.event == CHR_EVENT_OPENED);
1468 data.event = -1;
1469 g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
1470
1471 /* Send a greeting to the server */
1472 ret = qemu_chr_fe_write_all(&c, (const uint8_t *)SOCKET_PING,
1473 sizeof(SOCKET_PING));
1474 g_assert_cmpint(ret, ==, sizeof(SOCKET_PING));
1475 g_assert(data.event == -1);
1476
1477 /* Setup a callback to receive the reply to our greeting */
1478 qemu_chr_fe_set_handlers(&c, char_socket_can_read,
1479 char_socket_read,
1480 event_cb, NULL,
1481 &data, NULL, true);
1482 g_assert(data.event == CHR_EVENT_OPENED);
1483 data.event = -1;
1484
1485 /* Wait for the server to go away */
1486 while (data.event == -1) {
1487 main_loop_wait(false);
1488 }
1489 g_assert(data.event == CHR_EVENT_CLOSED);
1490 g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
1491 g_assert(data.got_pong);
1492 qemu_thread_join(&thread);
1493
1494 if (config->reconnect && !reconnected) {
1495 reconnected = true;
1496 qemu_thread_create(&thread, "client",
1497 char_socket_client_server_thread,
1498 ioc, QEMU_THREAD_JOINABLE);
1499 goto reconnect;
1500 }
1501
1502 object_unref(OBJECT(ioc));
1503 object_unparent(OBJECT(chr));
1504 qapi_free_SocketAddress(addr);
1505 g_free(optstr);
1506 }
1507
1508 static void
1509 count_closed_event(void *opaque, QEMUChrEvent event)
1510 {
1511 int *count = opaque;
1512 if (event == CHR_EVENT_CLOSED) {
1513 (*count)++;
1514 }
1515 }
1516
1517 static void
1518 char_socket_discard_read(void *opaque, const uint8_t *buf, int size)
1519 {
1520 }
1521
1522 static void char_socket_server_two_clients_test(gconstpointer opaque)
1523 {
1524 SocketAddress *incoming_addr = (gpointer) opaque;
1525 Chardev *chr;
1526 CharFrontend c = {0};
1527 QObject *qaddr;
1528 SocketAddress *addr;
1529 Visitor *v;
1530 char *optstr;
1531 QemuOpts *opts;
1532 QIOChannelSocket *ioc1, *ioc2;
1533 int closed = 0;
1534
1535 g_setenv("QTEST_SILENT_ERRORS", "1", 1);
1536 /*
1537 * We rely on addr containing "wait=off", otherwise
1538 * qemu_chr_new() will block until a client connects. We
1539 * can't spawn our client thread though, because until
1540 * qemu_chr_new() returns we don't know what TCP port was
1541 * allocated by the OS
1542 */
1543 optstr = char_socket_addr_to_opt_str(incoming_addr,
1544 false,
1545 NULL,
1546 true);
1547 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
1548 optstr, true);
1549 g_assert_nonnull(opts);
1550 chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
1551 qemu_opts_del(opts);
1552 g_assert_nonnull(chr);
1553 g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
1554
1555 qaddr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
1556 g_assert_nonnull(qaddr);
1557
1558 v = qobject_input_visitor_new(qaddr);
1559 visit_type_SocketAddress(v, "addr", &addr, &error_abort);
1560 visit_free(v);
1561 qobject_unref(qaddr);
1562
1563 qemu_chr_fe_init(&c, chr, &error_abort);
1564
1565 qemu_chr_fe_set_handlers(&c, char_socket_can_read, char_socket_discard_read,
1566 count_closed_event, NULL,
1567 &closed, NULL, true);
1568
1569 ioc1 = qio_channel_socket_new();
1570 qio_channel_socket_connect_sync(ioc1, addr, &error_abort);
1571 qemu_chr_wait_connected(chr, &error_abort);
1572
1573 /* switch the chardev to another context */
1574 GMainContext *ctx = g_main_context_new();
1575 qemu_chr_fe_set_handlers(&c, char_socket_can_read, char_socket_discard_read,
1576 count_closed_event, NULL,
1577 &closed, ctx, true);
1578
1579 /* Start a second connection while the first is still connected.
1580 * It will be placed in the listen() backlog, and connect() will
1581 * succeed immediately.
1582 */
1583 ioc2 = qio_channel_socket_new();
1584 qio_channel_socket_connect_sync(ioc2, addr, &error_abort);
1585
1586 object_unref(OBJECT(ioc1));
1587 /* The two connections should now be processed serially. */
1588 while (g_main_context_iteration(ctx, TRUE)) {
1589 if (closed == 1 && ioc2) {
1590 object_unref(OBJECT(ioc2));
1591 ioc2 = NULL;
1592 }
1593 if (closed == 2) {
1594 break;
1595 }
1596 }
1597
1598 qapi_free_SocketAddress(addr);
1599 object_unparent(OBJECT(chr));
1600 g_main_context_unref(ctx);
1601 g_free(optstr);
1602 g_unsetenv("QTEST_SILENT_ERRORS");
1603 }
1604
1605
1606 #if defined(HAVE_CHARDEV_SERIAL) && !defined(WIN32)
1607 static void char_serial_test(void)
1608 {
1609 QemuOpts *opts;
1610 Chardev *chr;
1611
1612 opts = qemu_opts_create(qemu_find_opts("chardev"), "serial-id",
1613 1, &error_abort);
1614 qemu_opt_set(opts, "backend", "serial", &error_abort);
1615 qemu_opt_set(opts, "path", "/dev/null", &error_abort);
1616
1617 chr = qemu_chr_new_from_opts(opts, NULL, NULL);
1618 g_assert_nonnull(chr);
1619 /* TODO: add more tests with a pty */
1620 object_unparent(OBJECT(chr));
1621
1622 qemu_opts_del(opts);
1623 }
1624 #endif
1625
1626 #if defined(HAVE_CHARDEV_PARALLEL) && !defined(WIN32)
1627 static void char_parallel_test(void)
1628 {
1629 QemuOpts *opts;
1630 Chardev *chr;
1631
1632 opts = qemu_opts_create(qemu_find_opts("chardev"), "parallel-id",
1633 1, &error_abort);
1634 qemu_opt_set(opts, "backend", "parallel", &error_abort);
1635 qemu_opt_set(opts, "path", "/dev/null", &error_abort);
1636
1637 chr = qemu_chr_new_from_opts(opts, NULL, NULL);
1638 #ifdef __linux__
1639 /* fails to PPCLAIM, see qemu_chr_open_pp_fd() */
1640 g_assert_null(chr);
1641 #else
1642 g_assert_nonnull(chr);
1643 object_unparent(OBJECT(chr));
1644 #endif
1645
1646 qemu_opts_del(opts);
1647 }
1648 #endif
1649
1650 #ifndef _WIN32
1651 static void char_file_fifo_test(void)
1652 {
1653 Chardev *chr;
1654 CharFrontend c;
1655 char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
1656 char *fifo = g_build_filename(tmp_path, "fifo", NULL);
1657 char *out = g_build_filename(tmp_path, "out", NULL);
1658 ChardevFile file = { .in = fifo,
1659 .out = out };
1660 ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
1661 .u.file.data = &file };
1662 FeHandler fe = { 0, };
1663 int fd, ret;
1664
1665 if (mkfifo(fifo, 0600) < 0) {
1666 abort();
1667 }
1668
1669 fd = open(fifo, O_RDWR);
1670 ret = write(fd, "fifo-in", 8);
1671 g_assert_cmpint(ret, ==, 8);
1672
1673 chr = qemu_chardev_new("label-file", TYPE_CHARDEV_FILE, &backend,
1674 NULL, &error_abort);
1675
1676 qemu_chr_fe_init(&c, chr, &error_abort);
1677 qemu_chr_fe_set_handlers(&c,
1678 fe_can_read,
1679 fe_read,
1680 fe_event,
1681 NULL,
1682 &fe, NULL, true);
1683
1684 g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK);
1685 qmp_chardev_send_break("label-foo", NULL);
1686 g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK);
1687 qmp_chardev_send_break("label-file", NULL);
1688 g_assert_cmpint(fe.last_event, ==, CHR_EVENT_BREAK);
1689
1690 main_loop();
1691
1692 close(fd);
1693
1694 g_assert_cmpint(fe.read_count, ==, 8);
1695 g_assert_cmpstr(fe.read_buf, ==, "fifo-in");
1696
1697 qemu_chr_fe_deinit(&c, true);
1698
1699 g_unlink(fifo);
1700 g_free(fifo);
1701 g_unlink(out);
1702 g_free(out);
1703 g_rmdir(tmp_path);
1704 g_free(tmp_path);
1705 }
1706 #endif
1707
1708 static void char_file_test_internal(Chardev *ext_chr, const char *filepath)
1709 {
1710 char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
1711 char *out;
1712 Chardev *chr;
1713 char *contents = NULL;
1714 ChardevFile file = {};
1715 ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
1716 .u.file.data = &file };
1717 gsize length;
1718 int ret;
1719
1720 if (ext_chr) {
1721 chr = ext_chr;
1722 out = g_strdup(filepath);
1723 file.out = out;
1724 } else {
1725 out = g_build_filename(tmp_path, "out", NULL);
1726 file.out = out;
1727 chr = qemu_chardev_new(NULL, TYPE_CHARDEV_FILE, &backend,
1728 NULL, &error_abort);
1729 }
1730 ret = qemu_chr_write_all(chr, (uint8_t *)"hello!", 6);
1731 g_assert_cmpint(ret, ==, 6);
1732
1733 ret = g_file_get_contents(out, &contents, &length, NULL);
1734 g_assert(ret == TRUE);
1735 g_assert_cmpint(length, ==, 6);
1736 g_assert(strncmp(contents, "hello!", 6) == 0);
1737
1738 if (!ext_chr) {
1739 object_unparent(OBJECT(chr));
1740 g_unlink(out);
1741 }
1742 g_free(contents);
1743 g_rmdir(tmp_path);
1744 g_free(tmp_path);
1745 g_free(out);
1746 }
1747
1748 static void char_file_test(void)
1749 {
1750 char_file_test_internal(NULL, NULL);
1751 }
1752
1753 static void char_null_test(void)
1754 {
1755 Error *err = NULL;
1756 Chardev *chr;
1757 CharFrontend c;
1758 int ret;
1759
1760 chr = qemu_chr_find("label-null");
1761 g_assert_null(chr);
1762
1763 chr = qemu_chr_new("label-null", "null", NULL);
1764 chr = qemu_chr_find("label-null");
1765 g_assert_nonnull(chr);
1766
1767 g_assert(qemu_chr_has_feature(chr,
1768 QEMU_CHAR_FEATURE_FD_PASS) == false);
1769 g_assert(qemu_chr_has_feature(chr,
1770 QEMU_CHAR_FEATURE_RECONNECTABLE) == false);
1771
1772 /* check max avail */
1773 qemu_chr_fe_init(&c, chr, &error_abort);
1774 qemu_chr_fe_init(&c, chr, &err);
1775 error_free_or_abort(&err);
1776
1777 /* deinit & reinit */
1778 qemu_chr_fe_deinit(&c, false);
1779 qemu_chr_fe_init(&c, chr, &error_abort);
1780
1781 qemu_chr_fe_set_open(&c, true);
1782
1783 qemu_chr_fe_set_handlers(&c,
1784 fe_can_read,
1785 fe_read,
1786 fe_event,
1787 NULL,
1788 NULL, NULL, true);
1789
1790 ret = qemu_chr_fe_write(&c, (void *)"buf", 4);
1791 g_assert_cmpint(ret, ==, 4);
1792
1793 qemu_chr_fe_deinit(&c, true);
1794 }
1795
1796 static void char_invalid_test(void)
1797 {
1798 Chardev *chr;
1799 g_setenv("QTEST_SILENT_ERRORS", "1", 1);
1800 chr = qemu_chr_new("label-invalid", "invalid", NULL);
1801 g_assert_null(chr);
1802 g_unsetenv("QTEST_SILENT_ERRORS");
1803 }
1804
1805 static int chardev_change(void *opaque)
1806 {
1807 return 0;
1808 }
1809
1810 static int chardev_change_denied(void *opaque)
1811 {
1812 return -1;
1813 }
1814
1815 static void char_hotswap_test(void)
1816 {
1817 char *chr_args;
1818 Chardev *chr;
1819 CharFrontend c;
1820
1821 gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
1822 char *filename = g_build_filename(tmp_path, "file", NULL);
1823 ChardevFile file = { .out = filename };
1824 ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
1825 .u.file.data = &file };
1826 ChardevReturn *ret;
1827
1828 int port;
1829 int sock = make_udp_socket(&port);
1830 g_assert_cmpint(sock, >=, 0);
1831
1832 chr_args = g_strdup_printf("udp:127.0.0.1:%d", port);
1833
1834 chr = qemu_chr_new("chardev", chr_args, NULL);
1835 qemu_chr_fe_init(&c, chr, &error_abort);
1836
1837 /* check that chardev operates correctly */
1838 char_udp_test_internal(chr, sock);
1839
1840 /* set the handler that denies the hotswap */
1841 qemu_chr_fe_set_handlers(&c, NULL, NULL,
1842 NULL, chardev_change_denied, NULL, NULL, true);
1843
1844 /* now, change is denied and has to keep the old backend operating */
1845 ret = qmp_chardev_change("chardev", &backend, NULL);
1846 g_assert(!ret);
1847 g_assert(c.chr == chr);
1848
1849 char_udp_test_internal(chr, sock);
1850
1851 /* now allow the change */
1852 qemu_chr_fe_set_handlers(&c, NULL, NULL,
1853 NULL, chardev_change, NULL, NULL, true);
1854
1855 /* has to succeed now */
1856 ret = qmp_chardev_change("chardev", &backend, &error_abort);
1857 g_assert(c.chr != chr);
1858
1859 close(sock);
1860 chr = c.chr;
1861
1862 /* run the file chardev test */
1863 char_file_test_internal(chr, filename);
1864
1865 object_unparent(OBJECT(chr));
1866
1867 qapi_free_ChardevReturn(ret);
1868 g_unlink(filename);
1869 g_free(filename);
1870 g_rmdir(tmp_path);
1871 g_free(tmp_path);
1872 g_free(chr_args);
1873 }
1874
1875 static SocketAddress tcpaddr = {
1876 .type = SOCKET_ADDRESS_TYPE_INET,
1877 .u.inet.host = (char *)"127.0.0.1",
1878 .u.inet.port = (char *)"0",
1879 };
1880 #ifndef WIN32
1881 static SocketAddress unixaddr = {
1882 .type = SOCKET_ADDRESS_TYPE_UNIX,
1883 .u.q_unix.path = (char *)"test-char.sock",
1884 };
1885 #endif
1886
1887 int main(int argc, char **argv)
1888 {
1889 bool has_ipv4, has_ipv6;
1890
1891 qemu_init_main_loop(&error_abort);
1892 socket_init();
1893
1894 g_test_init(&argc, &argv, NULL);
1895
1896 if (socket_check_protocol_support(&has_ipv4, &has_ipv6) < 0) {
1897 g_printerr("socket_check_protocol_support() failed\n");
1898 goto end;
1899 }
1900
1901 module_call_init(MODULE_INIT_QOM);
1902 qemu_add_opts(&qemu_chardev_opts);
1903
1904 g_test_add_func("/char/null", char_null_test);
1905 g_test_add_func("/char/invalid", char_invalid_test);
1906 g_test_add_func("/char/ringbuf", char_ringbuf_test);
1907 g_test_add_func("/char/mux", char_mux_test);
1908 g_test_add_func("/char/hub", char_hub_test);
1909 #ifdef _WIN32
1910 g_test_add_func("/char/console/subprocess", char_console_test_subprocess);
1911 g_test_add_func("/char/console", char_console_test);
1912 #endif
1913 g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess);
1914 g_test_add_func("/char/stdio", char_stdio_test);
1915 #ifndef _WIN32
1916 g_test_add_func("/char/pipe", char_pipe_test);
1917 #endif
1918 g_test_add_func("/char/file", char_file_test);
1919 #ifndef _WIN32
1920 g_test_add_func("/char/file-fifo", char_file_fifo_test);
1921 #endif
1922
1923 #define SOCKET_SERVER_TEST(name, addr) \
1924 static CharSocketServerTestConfig server1 ## name = \
1925 { addr, false, false }; \
1926 static CharSocketServerTestConfig server2 ## name = \
1927 { addr, true, false }; \
1928 static CharSocketServerTestConfig server3 ## name = \
1929 { addr, false, true }; \
1930 static CharSocketServerTestConfig server4 ## name = \
1931 { addr, true, true }; \
1932 g_test_add_data_func("/char/socket/server/mainloop/" # name, \
1933 &server1 ##name, char_socket_server_test); \
1934 g_test_add_data_func("/char/socket/server/wait-conn/" # name, \
1935 &server2 ##name, char_socket_server_test); \
1936 g_test_add_data_func("/char/socket/server/mainloop-fdpass/" # name, \
1937 &server3 ##name, char_socket_server_test); \
1938 g_test_add_data_func("/char/socket/server/wait-conn-fdpass/" # name, \
1939 &server4 ##name, char_socket_server_test); \
1940 g_test_add_data_func("/char/socket/server/two-clients/" # name, \
1941 addr, char_socket_server_two_clients_test)
1942
1943 #define SOCKET_CLIENT_TEST(name, addr) \
1944 static CharSocketClientTestConfig client1 ## name = \
1945 { addr, NULL, false, false, char_socket_event }; \
1946 static CharSocketClientTestConfig client2 ## name = \
1947 { addr, NULL, true, false, char_socket_event }; \
1948 static CharSocketClientTestConfig client3 ## name = \
1949 { addr, ",reconnect-ms=1000", false, false, char_socket_event }; \
1950 static CharSocketClientTestConfig client4 ## name = \
1951 { addr, ",reconnect-ms=1000", true, false, char_socket_event }; \
1952 static CharSocketClientTestConfig client5 ## name = \
1953 { addr, NULL, false, true, char_socket_event }; \
1954 static CharSocketClientTestConfig client6 ## name = \
1955 { addr, NULL, true, true, char_socket_event }; \
1956 static CharSocketClientTestConfig client7 ## name = \
1957 { addr, ",reconnect-ms=1000", true, false, \
1958 char_socket_event_with_error }; \
1959 static CharSocketClientTestConfig client8 ## name = \
1960 { addr, ",reconnect-ms=1000", false, false, char_socket_event };\
1961 g_test_add_data_func("/char/socket/client/mainloop/" # name, \
1962 &client1 ##name, char_socket_client_test); \
1963 g_test_add_data_func("/char/socket/client/wait-conn/" # name, \
1964 &client2 ##name, char_socket_client_test); \
1965 g_test_add_data_func("/char/socket/client/mainloop-reconnect/" # name, \
1966 &client3 ##name, char_socket_client_test); \
1967 g_test_add_data_func("/char/socket/client/wait-conn-reconnect/" # name, \
1968 &client4 ##name, char_socket_client_test); \
1969 g_test_add_data_func("/char/socket/client/mainloop-fdpass/" # name, \
1970 &client5 ##name, char_socket_client_test); \
1971 g_test_add_data_func("/char/socket/client/wait-conn-fdpass/" # name, \
1972 &client6 ##name, char_socket_client_test); \
1973 g_test_add_data_func("/char/socket/client/reconnect-error/" # name, \
1974 &client7 ##name, char_socket_client_test); \
1975 g_test_add_data_func("/char/socket/client/dupid-reconnect/" # name, \
1976 &client8 ##name, char_socket_client_dupid_test)
1977
1978 if (has_ipv4) {
1979 SOCKET_SERVER_TEST(tcp, &tcpaddr);
1980 SOCKET_CLIENT_TEST(tcp, &tcpaddr);
1981 }
1982 #ifndef WIN32
1983 SOCKET_SERVER_TEST(unix, &unixaddr);
1984 SOCKET_CLIENT_TEST(unix, &unixaddr);
1985 #endif
1986
1987 g_test_add_func("/char/udp", char_udp_test);
1988 #if defined(HAVE_CHARDEV_SERIAL) && !defined(WIN32)
1989 g_test_add_func("/char/serial", char_serial_test);
1990 #endif
1991 #if defined(HAVE_CHARDEV_PARALLEL) && !defined(WIN32)
1992 g_test_add_func("/char/parallel", char_parallel_test);
1993 #endif
1994 g_test_add_func("/char/hotswap", char_hotswap_test);
1995 g_test_add_func("/char/websocket", char_websock_test);
1996
1997 end:
1998 return g_test_run();
1999 }