]>
Commit | Line | Data |
---|---|---|
ea3af47d | 1 | #include "qemu/osdep.h" |
a86c83d7 | 2 | #include <glib/gstdio.h> |
ea3af47d | 3 | |
ea3af47d | 4 | #include "qemu/config-file.h" |
0b8fa32f | 5 | #include "qemu/module.h" |
922a01a0 | 6 | #include "qemu/option.h" |
d47fb5d1 | 7 | #include "qemu/sockets.h" |
4d43a603 | 8 | #include "chardev/char-fe.h" |
ea3af47d MAL |
9 | #include "sysemu/sysemu.h" |
10 | #include "qapi/error.h" | |
9af23989 | 11 | #include "qapi/qapi-commands-char.h" |
452fcdbc | 12 | #include "qapi/qmp/qdict.h" |
d47fb5d1 | 13 | #include "qom/qom-qobject.h" |
9baa6802 DB |
14 | #include "io/channel-socket.h" |
15 | #include "qapi/qobject-input-visitor.h" | |
16 | #include "qapi/qapi-visit-sockets.h" | |
e7b6ba41 | 17 | #include "socket-helpers.h" |
ea3af47d | 18 | |
a86c83d7 MAL |
19 | static bool quit; |
20 | ||
ea3af47d MAL |
21 | typedef struct FeHandler { |
22 | int read_count; | |
68cf36a7 AP |
23 | bool is_open; |
24 | int openclose_count; | |
25 | bool openclose_mismatch; | |
ea3af47d MAL |
26 | int last_event; |
27 | char read_buf[128]; | |
28 | } FeHandler; | |
29 | ||
a86c83d7 MAL |
30 | static void main_loop(void) |
31 | { | |
a86c83d7 MAL |
32 | quit = false; |
33 | do { | |
be59df79 | 34 | main_loop_wait(false); |
a86c83d7 MAL |
35 | } while (!quit); |
36 | } | |
a86c83d7 | 37 | |
ea3af47d MAL |
38 | static int fe_can_read(void *opaque) |
39 | { | |
40 | FeHandler *h = opaque; | |
41 | ||
42 | return sizeof(h->read_buf) - h->read_count; | |
43 | } | |
44 | ||
45 | static void fe_read(void *opaque, const uint8_t *buf, int size) | |
46 | { | |
47 | FeHandler *h = opaque; | |
48 | ||
49 | g_assert_cmpint(size, <=, fe_can_read(opaque)); | |
50 | ||
51 | memcpy(h->read_buf + h->read_count, buf, size); | |
52 | h->read_count += size; | |
a86c83d7 | 53 | quit = true; |
ea3af47d MAL |
54 | } |
55 | ||
083b266f | 56 | static void fe_event(void *opaque, QEMUChrEvent event) |
ea3af47d MAL |
57 | { |
58 | FeHandler *h = opaque; | |
68cf36a7 | 59 | bool new_open_state; |
ea3af47d MAL |
60 | |
61 | h->last_event = event; | |
68cf36a7 AP |
62 | switch (event) { |
63 | case CHR_EVENT_BREAK: | |
64 | break; | |
65 | case CHR_EVENT_OPENED: | |
66 | case CHR_EVENT_CLOSED: | |
67 | h->openclose_count++; | |
68 | new_open_state = (event == CHR_EVENT_OPENED); | |
69 | if (h->is_open == new_open_state) { | |
70 | h->openclose_mismatch = true; | |
71 | } | |
72 | h->is_open = new_open_state; | |
ff82a54b | 73 | /* fallthrough */ |
68cf36a7 | 74 | default: |
bd1d5ad9 | 75 | quit = true; |
68cf36a7 | 76 | break; |
bd1d5ad9 | 77 | } |
ea3af47d MAL |
78 | } |
79 | ||
79c8db5a MAL |
80 | #ifdef _WIN32 |
81 | static void char_console_test_subprocess(void) | |
82 | { | |
83 | QemuOpts *opts; | |
84 | Chardev *chr; | |
85 | ||
86 | opts = qemu_opts_create(qemu_find_opts("chardev"), "console-label", | |
87 | 1, &error_abort); | |
88 | qemu_opt_set(opts, "backend", "console", &error_abort); | |
89 | ||
4ad6f6cb | 90 | chr = qemu_chr_new_from_opts(opts, NULL, NULL); |
79c8db5a MAL |
91 | g_assert_nonnull(chr); |
92 | ||
93 | qemu_chr_write_all(chr, (const uint8_t *)"CONSOLE", 7); | |
94 | ||
95 | qemu_opts_del(opts); | |
96 | object_unparent(OBJECT(chr)); | |
97 | } | |
98 | ||
99 | static void char_console_test(void) | |
100 | { | |
101 | g_test_trap_subprocess("/char/console/subprocess", 0, 0); | |
102 | g_test_trap_assert_passed(); | |
103 | g_test_trap_assert_stdout("CONSOLE"); | |
104 | } | |
105 | #endif | |
ea3af47d MAL |
106 | static void char_stdio_test_subprocess(void) |
107 | { | |
0ec7b3e7 | 108 | Chardev *chr; |
ea3af47d MAL |
109 | CharBackend be; |
110 | int ret; | |
111 | ||
4ad6f6cb | 112 | chr = qemu_chr_new("label", "stdio", NULL); |
ea3af47d MAL |
113 | g_assert_nonnull(chr); |
114 | ||
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); | |
119 | ||
1ce2610c | 120 | qemu_chr_fe_deinit(&be, true); |
ea3af47d MAL |
121 | } |
122 | ||
123 | static void char_stdio_test(void) | |
124 | { | |
125 | g_test_trap_subprocess("/char/stdio/subprocess", 0, 0); | |
126 | g_test_trap_assert_passed(); | |
127 | g_test_trap_assert_stdout("buf"); | |
128 | } | |
ea3af47d | 129 | |
ea3af47d MAL |
130 | static void char_ringbuf_test(void) |
131 | { | |
132 | QemuOpts *opts; | |
0ec7b3e7 | 133 | Chardev *chr; |
ea3af47d MAL |
134 | CharBackend be; |
135 | char *data; | |
136 | int ret; | |
137 | ||
138 | opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label", | |
139 | 1, &error_abort); | |
140 | qemu_opt_set(opts, "backend", "ringbuf", &error_abort); | |
141 | ||
142 | qemu_opt_set(opts, "size", "5", &error_abort); | |
4ad6f6cb | 143 | chr = qemu_chr_new_from_opts(opts, NULL, NULL); |
ea3af47d MAL |
144 | g_assert_null(chr); |
145 | qemu_opts_del(opts); | |
146 | ||
147 | opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label", | |
148 | 1, &error_abort); | |
149 | qemu_opt_set(opts, "backend", "ringbuf", &error_abort); | |
150 | qemu_opt_set(opts, "size", "2", &error_abort); | |
4ad6f6cb | 151 | chr = qemu_chr_new_from_opts(opts, NULL, &error_abort); |
ea3af47d MAL |
152 | g_assert_nonnull(chr); |
153 | qemu_opts_del(opts); | |
154 | ||
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); | |
158 | ||
159 | data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort); | |
160 | g_assert_cmpstr(data, ==, "ff"); | |
161 | g_free(data); | |
162 | ||
163 | data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort); | |
164 | g_assert_cmpstr(data, ==, ""); | |
165 | g_free(data); | |
166 | ||
1ce2610c | 167 | qemu_chr_fe_deinit(&be, true); |
e24ef0db MAL |
168 | |
169 | /* check alias */ | |
170 | opts = qemu_opts_create(qemu_find_opts("chardev"), "memory-label", | |
171 | 1, &error_abort); | |
172 | qemu_opt_set(opts, "backend", "memory", &error_abort); | |
173 | qemu_opt_set(opts, "size", "2", &error_abort); | |
4ad6f6cb | 174 | chr = qemu_chr_new_from_opts(opts, NULL, NULL); |
e24ef0db MAL |
175 | g_assert_nonnull(chr); |
176 | object_unparent(OBJECT(chr)); | |
177 | qemu_opts_del(opts); | |
ea3af47d MAL |
178 | } |
179 | ||
180 | static void char_mux_test(void) | |
181 | { | |
182 | QemuOpts *opts; | |
0ec7b3e7 | 183 | Chardev *chr, *base; |
ea3af47d | 184 | char *data; |
68cf36a7 | 185 | FeHandler h1 = { 0, false, 0, false, }, h2 = { 0, false, 0, false, }; |
ea3af47d MAL |
186 | CharBackend chr_be1, chr_be2; |
187 | ||
188 | opts = qemu_opts_create(qemu_find_opts("chardev"), "mux-label", | |
189 | 1, &error_abort); | |
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); | |
4ad6f6cb | 193 | chr = qemu_chr_new_from_opts(opts, NULL, &error_abort); |
ea3af47d MAL |
194 | g_assert_nonnull(chr); |
195 | qemu_opts_del(opts); | |
196 | ||
197 | qemu_chr_fe_init(&chr_be1, chr, &error_abort); | |
198 | qemu_chr_fe_set_handlers(&chr_be1, | |
199 | fe_can_read, | |
200 | fe_read, | |
201 | fe_event, | |
81517ba3 | 202 | NULL, |
ea3af47d | 203 | &h1, |
39ab61c6 | 204 | NULL, true); |
ea3af47d MAL |
205 | |
206 | qemu_chr_fe_init(&chr_be2, chr, &error_abort); | |
207 | qemu_chr_fe_set_handlers(&chr_be2, | |
208 | fe_can_read, | |
209 | fe_read, | |
210 | fe_event, | |
81517ba3 | 211 | NULL, |
ea3af47d | 212 | &h2, |
39ab61c6 | 213 | NULL, true); |
ea3af47d MAL |
214 | qemu_chr_fe_take_focus(&chr_be2); |
215 | ||
216 | base = qemu_chr_find("mux-label-base"); | |
217 | g_assert_cmpint(qemu_chr_be_can_write(base), !=, 0); | |
218 | ||
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"); | |
223 | h2.read_count = 0; | |
224 | ||
d45f80ba MAL |
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); | |
234 | ||
ea3af47d | 235 | /* switch focus */ |
eeaa6715 MAL |
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); | |
239 | ||
ea3af47d | 240 | qemu_chr_be_write(base, (void *)"\1c", 2); |
d45f80ba MAL |
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); | |
ea3af47d MAL |
246 | |
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"); | |
251 | h1.read_count = 0; | |
252 | ||
eeaa6715 MAL |
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); | |
256 | ||
68cf36a7 AP |
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); | |
264 | ||
265 | h1.openclose_count = h2.openclose_count = 0; | |
266 | ||
267 | qemu_chr_fe_set_handlers(&chr_be1, NULL, NULL, NULL, NULL, | |
268 | NULL, NULL, false); | |
269 | qemu_chr_fe_set_handlers(&chr_be2, NULL, NULL, NULL, NULL, | |
270 | NULL, NULL, false); | |
271 | g_assert_cmpint(h1.openclose_count, ==, 0); | |
272 | g_assert_cmpint(h2.openclose_count, ==, 0); | |
273 | ||
274 | h1.is_open = h2.is_open = false; | |
275 | qemu_chr_fe_set_handlers(&chr_be1, | |
276 | NULL, | |
277 | NULL, | |
278 | fe_event, | |
279 | NULL, | |
280 | &h1, | |
281 | NULL, false); | |
282 | qemu_chr_fe_set_handlers(&chr_be2, | |
283 | NULL, | |
284 | NULL, | |
285 | fe_event, | |
286 | NULL, | |
287 | &h2, | |
288 | NULL, false); | |
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); | |
293 | ||
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); | |
300 | ||
301 | qemu_chr_fe_set_handlers(&chr_be2, | |
302 | fe_can_read, | |
303 | fe_read, | |
304 | fe_event, | |
305 | NULL, | |
306 | &h2, | |
307 | NULL, false); | |
308 | qemu_chr_fe_set_handlers(&chr_be1, | |
309 | fe_can_read, | |
310 | fe_read, | |
311 | fe_event, | |
312 | NULL, | |
313 | &h1, | |
314 | NULL, false); | |
315 | ||
ea3af47d | 316 | /* remove first handler */ |
81517ba3 AN |
317 | qemu_chr_fe_set_handlers(&chr_be1, NULL, NULL, NULL, NULL, |
318 | NULL, NULL, true); | |
ea3af47d MAL |
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); | |
322 | ||
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"); | |
328 | h2.read_count = 0; | |
329 | ||
330 | /* print help */ | |
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); | |
334 | g_free(data); | |
335 | ||
1ce2610c MAL |
336 | qemu_chr_fe_deinit(&chr_be1, false); |
337 | qemu_chr_fe_deinit(&chr_be2, true); | |
ea3af47d MAL |
338 | } |
339 | ||
0935700f | 340 | |
125fc4a7 JS |
341 | static void websock_server_read(void *opaque, const uint8_t *buf, int size) |
342 | { | |
343 | g_assert_cmpint(size, ==, 5); | |
344 | g_assert(memcmp(buf, "world", size) == 0); | |
345 | quit = true; | |
346 | } | |
347 | ||
348 | ||
349 | static int websock_server_can_read(void *opaque) | |
350 | { | |
351 | return 10; | |
352 | } | |
353 | ||
354 | ||
355 | static bool websock_check_http_headers(char *buf, int size) | |
356 | { | |
357 | int i; | |
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" }; | |
364 | ||
365 | for (i = 0; i < 6; i++) { | |
366 | if (g_strstr_len(buf, size, ans[i]) == NULL) { | |
367 | return false; | |
368 | } | |
369 | } | |
370 | ||
371 | return true; | |
372 | } | |
373 | ||
374 | ||
375 | static void websock_client_read(void *opaque, const uint8_t *buf, int size) | |
376 | { | |
377 | const uint8_t ping[] = { 0x89, 0x85, /* Ping header */ | |
378 | 0x07, 0x77, 0x9e, 0xf9, /* Masking key */ | |
379 | 0x6f, 0x12, 0xf2, 0x95, 0x68 /* "hello" */ }; | |
380 | ||
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; | |
385 | ||
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)); | |
391 | } else { | |
392 | g_assert(buf[0] == 0x88 && buf[1] == 0x16); | |
393 | g_assert(strncmp((char *) buf + 4, "peer requested close", 10) == 0); | |
394 | quit = true; | |
395 | } | |
396 | } | |
397 | ||
398 | ||
399 | static int websock_client_can_read(void *opaque) | |
400 | { | |
401 | return 4096; | |
402 | } | |
403 | ||
404 | ||
405 | static void char_websock_test(void) | |
406 | { | |
407 | QObject *addr; | |
408 | QDict *qdict; | |
409 | const char *port; | |
410 | char *tmp; | |
411 | char *handshake_port; | |
412 | CharBackend be; | |
413 | CharBackend client_be; | |
414 | Chardev *chr_client; | |
415 | Chardev *chr = qemu_chr_new("server", | |
4ad6f6cb | 416 | "websocket:127.0.0.1:0,server,nowait", NULL); |
125fc4a7 JS |
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 */ }; | |
428 | ||
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); | |
435 | ||
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); | |
439 | ||
4ad6f6cb | 440 | chr_client = qemu_chr_new("client", tmp, NULL); |
125fc4a7 JS |
441 | qemu_chr_fe_init(&client_be, chr_client, &error_abort); |
442 | qemu_chr_fe_set_handlers(&client_be, websock_client_can_read, | |
443 | websock_client_read, | |
444 | NULL, NULL, chr_client, NULL, true); | |
445 | g_free(tmp); | |
446 | ||
447 | qemu_chr_write_all(chr_client, | |
448 | (uint8_t *) handshake_port, | |
449 | strlen(handshake_port)); | |
450 | g_free(handshake_port); | |
451 | main_loop(); | |
452 | ||
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)); | |
456 | ||
457 | qemu_chr_write_all(chr_client, close, sizeof(close)); | |
458 | main_loop(); | |
459 | ||
460 | object_unparent(OBJECT(chr_client)); | |
461 | object_unparent(OBJECT(chr)); | |
462 | } | |
463 | ||
464 | ||
a86c83d7 MAL |
465 | #ifndef _WIN32 |
466 | static void char_pipe_test(void) | |
467 | { | |
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); | |
470 | Chardev *chr; | |
471 | CharBackend be; | |
472 | int ret, fd; | |
473 | char buf[10]; | |
474 | FeHandler fe = { 0, }; | |
475 | ||
476 | in = g_strdup_printf("%s.in", pipe); | |
477 | if (mkfifo(in, 0600) < 0) { | |
478 | abort(); | |
479 | } | |
480 | out = g_strdup_printf("%s.out", pipe); | |
481 | if (mkfifo(out, 0600) < 0) { | |
482 | abort(); | |
483 | } | |
484 | ||
485 | tmp = g_strdup_printf("pipe:%s", pipe); | |
4ad6f6cb | 486 | chr = qemu_chr_new("pipe", tmp, NULL); |
a86c83d7 MAL |
487 | g_assert_nonnull(chr); |
488 | g_free(tmp); | |
489 | ||
490 | qemu_chr_fe_init(&be, chr, &error_abort); | |
491 | ||
492 | ret = qemu_chr_fe_write(&be, (void *)"pipe-out", 9); | |
493 | g_assert_cmpint(ret, ==, 9); | |
494 | ||
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"); | |
499 | close(fd); | |
500 | ||
501 | fd = open(in, O_WRONLY); | |
502 | ret = write(fd, "pipe-in", 8); | |
503 | g_assert_cmpint(ret, ==, 8); | |
504 | close(fd); | |
505 | ||
506 | qemu_chr_fe_set_handlers(&be, | |
507 | fe_can_read, | |
508 | fe_read, | |
509 | fe_event, | |
81517ba3 | 510 | NULL, |
a86c83d7 MAL |
511 | &fe, |
512 | NULL, true); | |
513 | ||
514 | main_loop(); | |
515 | ||
516 | g_assert_cmpint(fe.read_count, ==, 8); | |
517 | g_assert_cmpstr(fe.read_buf, ==, "pipe-in"); | |
518 | ||
1ce2610c | 519 | qemu_chr_fe_deinit(&be, true); |
a86c83d7 MAL |
520 | |
521 | g_assert(g_unlink(in) == 0); | |
522 | g_assert(g_unlink(out) == 0); | |
523 | g_assert(g_rmdir(tmp_path) == 0); | |
524 | g_free(in); | |
525 | g_free(out); | |
526 | g_free(tmp_path); | |
527 | g_free(pipe); | |
528 | } | |
529 | #endif | |
530 | ||
9baa6802 DB |
531 | typedef struct SocketIdleData { |
532 | GMainLoop *loop; | |
533 | Chardev *chr; | |
534 | bool conn_expected; | |
535 | CharBackend *be; | |
536 | CharBackend *client_be; | |
537 | } SocketIdleData; | |
538 | ||
539 | ||
540 | static void socket_read_hello(void *opaque, const uint8_t *buf, int size) | |
541 | { | |
542 | g_assert_cmpint(size, ==, 5); | |
543 | g_assert(strncmp((char *)buf, "hello", 5) == 0); | |
544 | ||
545 | quit = true; | |
546 | } | |
547 | ||
548 | static int socket_can_read_hello(void *opaque) | |
549 | { | |
550 | return 10; | |
551 | } | |
552 | ||
92ddfade | 553 | static int make_udp_socket(int *port) |
bb149048 | 554 | { |
92ddfade | 555 | struct sockaddr_in addr = { 0, }; |
bb149048 MAL |
556 | socklen_t alen = sizeof(addr); |
557 | int ret, sock = qemu_socket(PF_INET, SOCK_DGRAM, 0); | |
bb149048 MAL |
558 | |
559 | g_assert_cmpint(sock, >, 0); | |
560 | addr.sin_family = AF_INET ; | |
561 | addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
562 | addr.sin_port = 0; | |
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); | |
567 | ||
92ddfade AN |
568 | *port = ntohs(addr.sin_port); |
569 | return sock; | |
570 | } | |
571 | ||
572 | static void char_udp_test_internal(Chardev *reuse_chr, int sock) | |
573 | { | |
574 | struct sockaddr_in other; | |
575 | SocketIdleData d = { 0, }; | |
576 | Chardev *chr; | |
577 | CharBackend *be; | |
578 | socklen_t alen = sizeof(other); | |
579 | int ret; | |
580 | char buf[10]; | |
581 | char *tmp = NULL; | |
582 | ||
583 | if (reuse_chr) { | |
584 | chr = reuse_chr; | |
585 | be = chr->be; | |
586 | } else { | |
587 | int port; | |
588 | sock = make_udp_socket(&port); | |
589 | tmp = g_strdup_printf("udp:127.0.0.1:%d", port); | |
4ad6f6cb | 590 | chr = qemu_chr_new("client", tmp, NULL); |
92ddfade AN |
591 | g_assert_nonnull(chr); |
592 | ||
593 | be = g_alloca(sizeof(CharBackend)); | |
594 | qemu_chr_fe_init(be, chr, &error_abort); | |
595 | } | |
bb149048 MAL |
596 | |
597 | d.chr = chr; | |
92ddfade | 598 | qemu_chr_fe_set_handlers(be, socket_can_read_hello, socket_read_hello, |
81517ba3 | 599 | NULL, NULL, &d, NULL, true); |
bb149048 MAL |
600 | ret = qemu_chr_write_all(chr, (uint8_t *)"hello", 5); |
601 | g_assert_cmpint(ret, ==, 5); | |
602 | ||
bb149048 MAL |
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); | |
608 | ||
609 | main_loop(); | |
610 | ||
92ddfade AN |
611 | if (!reuse_chr) { |
612 | close(sock); | |
613 | qemu_chr_fe_deinit(be, true); | |
614 | } | |
bb149048 | 615 | g_free(tmp); |
92ddfade AN |
616 | } |
617 | ||
618 | static void char_udp_test(void) | |
619 | { | |
620 | char_udp_test_internal(NULL, 0); | |
bb149048 MAL |
621 | } |
622 | ||
9baa6802 DB |
623 | |
624 | typedef struct { | |
625 | int event; | |
626 | bool got_pong; | |
2b61bb71 | 627 | CharBackend *be; |
9baa6802 DB |
628 | } CharSocketTestData; |
629 | ||
630 | ||
631 | #define SOCKET_PING "Hello" | |
632 | #define SOCKET_PONG "World" | |
633 | ||
2b61bb71 | 634 | typedef void (*char_socket_cb)(void *opaque, QEMUChrEvent event); |
9baa6802 DB |
635 | |
636 | static void | |
083b266f | 637 | char_socket_event(void *opaque, QEMUChrEvent event) |
9baa6802 DB |
638 | { |
639 | CharSocketTestData *data = opaque; | |
640 | data->event = event; | |
641 | } | |
642 | ||
2b61bb71 LF |
643 | static void |
644 | char_socket_event_with_error(void *opaque, QEMUChrEvent event) | |
645 | { | |
646 | static bool first_error; | |
647 | CharSocketTestData *data = opaque; | |
648 | CharBackend *be = data->be; | |
649 | data->event = event; | |
650 | switch (event) { | |
651 | case CHR_EVENT_OPENED: | |
652 | if (!first_error) { | |
653 | first_error = true; | |
654 | qemu_chr_fe_disconnect(be); | |
655 | } | |
656 | return; | |
657 | case CHR_EVENT_CLOSED: | |
658 | return; | |
659 | default: | |
660 | return; | |
661 | } | |
662 | } | |
663 | ||
9baa6802 DB |
664 | |
665 | static void | |
666 | char_socket_read(void *opaque, const uint8_t *buf, int size) | |
667 | { | |
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; | |
672 | } | |
673 | ||
674 | ||
675 | static int | |
676 | char_socket_can_read(void *opaque) | |
677 | { | |
678 | return sizeof(SOCKET_PONG); | |
679 | } | |
680 | ||
681 | ||
682 | static char * | |
683 | char_socket_addr_to_opt_str(SocketAddress *addr, bool fd_pass, | |
684 | const char *reconnect, bool is_listen) | |
685 | { | |
686 | if (fd_pass) { | |
687 | QIOChannelSocket *ioc = qio_channel_socket_new(); | |
688 | int fd; | |
689 | char *optstr; | |
690 | g_assert(!reconnect); | |
691 | if (is_listen) { | |
4e2d8bf6 | 692 | qio_channel_socket_listen_sync(ioc, addr, 1, &error_abort); |
9baa6802 DB |
693 | } else { |
694 | qio_channel_socket_connect_sync(ioc, addr, &error_abort); | |
695 | } | |
696 | fd = ioc->fd; | |
697 | ioc->fd = -1; | |
698 | optstr = g_strdup_printf("socket,id=cdev0,fd=%d%s", | |
699 | fd, is_listen ? ",server,nowait" : ""); | |
700 | object_unref(OBJECT(ioc)); | |
701 | return optstr; | |
702 | } else { | |
703 | switch (addr->type) { | |
704 | case SOCKET_ADDRESS_TYPE_INET: | |
705 | return g_strdup_printf("socket,id=cdev0,host=%s,port=%s%s%s", | |
706 | addr->u.inet.host, | |
707 | addr->u.inet.port, | |
708 | reconnect ? reconnect : "", | |
709 | is_listen ? ",server,nowait" : ""); | |
710 | ||
711 | case SOCKET_ADDRESS_TYPE_UNIX: | |
712 | return g_strdup_printf("socket,id=cdev0,path=%s%s%s", | |
713 | addr->u.q_unix.path, | |
714 | reconnect ? reconnect : "", | |
715 | is_listen ? ",server,nowait" : ""); | |
716 | ||
717 | default: | |
718 | g_assert_not_reached(); | |
719 | } | |
720 | } | |
721 | } | |
722 | ||
723 | ||
2b61bb71 LF |
724 | static int |
725 | char_socket_ping_pong(QIOChannel *ioc, Error **errp) | |
9baa6802 DB |
726 | { |
727 | char greeting[sizeof(SOCKET_PING)]; | |
728 | const char *response = SOCKET_PONG; | |
729 | ||
2b61bb71 LF |
730 | int ret; |
731 | ret = qio_channel_read_all(ioc, greeting, sizeof(greeting), errp); | |
732 | if (ret != 0) { | |
733 | object_unref(OBJECT(ioc)); | |
734 | return -1; | |
735 | } | |
9baa6802 DB |
736 | |
737 | g_assert(memcmp(greeting, SOCKET_PING, sizeof(greeting)) == 0); | |
738 | ||
2b61bb71 | 739 | qio_channel_write_all(ioc, response, sizeof(SOCKET_PONG), errp); |
9baa6802 | 740 | object_unref(OBJECT(ioc)); |
2b61bb71 | 741 | return 0; |
9baa6802 DB |
742 | } |
743 | ||
744 | ||
745 | static gpointer | |
746 | char_socket_server_client_thread(gpointer data) | |
747 | { | |
748 | SocketAddress *addr = data; | |
749 | QIOChannelSocket *ioc = qio_channel_socket_new(); | |
750 | ||
751 | qio_channel_socket_connect_sync(ioc, addr, &error_abort); | |
752 | ||
2b61bb71 | 753 | char_socket_ping_pong(QIO_CHANNEL(ioc), &error_abort); |
9baa6802 DB |
754 | |
755 | return NULL; | |
756 | } | |
757 | ||
758 | ||
759 | typedef struct { | |
760 | SocketAddress *addr; | |
761 | bool wait_connected; | |
762 | bool fd_pass; | |
763 | } CharSocketServerTestConfig; | |
764 | ||
765 | ||
766 | static void char_socket_server_test(gconstpointer opaque) | |
767 | { | |
768 | const CharSocketServerTestConfig *config = opaque; | |
769 | Chardev *chr; | |
770 | CharBackend be = {0}; | |
771 | CharSocketTestData data = {0}; | |
772 | QObject *qaddr; | |
773 | SocketAddress *addr; | |
774 | Visitor *v; | |
775 | QemuThread thread; | |
776 | int ret; | |
edc6e501 | 777 | bool reconnected = false; |
9baa6802 DB |
778 | char *optstr; |
779 | QemuOpts *opts; | |
780 | ||
781 | g_setenv("QTEST_SILENT_ERRORS", "1", 1); | |
782 | /* | |
783 | * We rely on config->addr containing "nowait", 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 | |
788 | */ | |
789 | optstr = char_socket_addr_to_opt_str(config->addr, | |
790 | config->fd_pass, | |
791 | NULL, | |
792 | true); | |
793 | opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), | |
794 | optstr, true); | |
795 | g_assert_nonnull(opts); | |
4ad6f6cb | 796 | chr = qemu_chr_new_from_opts(opts, NULL, &error_abort); |
9baa6802 DB |
797 | qemu_opts_del(opts); |
798 | g_assert_nonnull(chr); | |
799 | g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort)); | |
800 | ||
801 | qaddr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort); | |
802 | g_assert_nonnull(qaddr); | |
803 | ||
804 | v = qobject_input_visitor_new(qaddr); | |
805 | visit_type_SocketAddress(v, "addr", &addr, &error_abort); | |
806 | visit_free(v); | |
807 | qobject_unref(qaddr); | |
808 | ||
809 | qemu_chr_fe_init(&be, chr, &error_abort); | |
810 | ||
811 | reconnect: | |
812 | data.event = -1; | |
2b61bb71 | 813 | data.be = &be; |
9baa6802 DB |
814 | qemu_chr_fe_set_handlers(&be, NULL, NULL, |
815 | char_socket_event, NULL, | |
816 | &data, NULL, true); | |
817 | g_assert(data.event == -1); | |
818 | ||
819 | /* | |
820 | * Kick off a thread to act as the "remote" client | |
821 | * which just plays ping-pong with us | |
822 | */ | |
823 | qemu_thread_create(&thread, "client", | |
824 | char_socket_server_client_thread, | |
825 | addr, QEMU_THREAD_JOINABLE); | |
826 | g_assert(data.event == -1); | |
827 | ||
828 | if (config->wait_connected) { | |
829 | /* Synchronously accept a connection */ | |
830 | qemu_chr_wait_connected(chr, &error_abort); | |
831 | } else { | |
832 | /* | |
833 | * Asynchronously accept a connection when the evnt | |
834 | * loop reports the listener socket as readable | |
835 | */ | |
836 | while (data.event == -1) { | |
837 | main_loop_wait(false); | |
838 | } | |
839 | } | |
840 | g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort)); | |
841 | g_assert(data.event == CHR_EVENT_OPENED); | |
842 | data.event = -1; | |
843 | ||
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); | |
849 | ||
850 | /* Setup a callback to receive the reply to our greeting */ | |
851 | qemu_chr_fe_set_handlers(&be, char_socket_can_read, | |
852 | char_socket_read, | |
853 | char_socket_event, NULL, | |
854 | &data, NULL, true); | |
855 | g_assert(data.event == CHR_EVENT_OPENED); | |
856 | data.event = -1; | |
857 | ||
858 | /* Wait for the client to go away */ | |
859 | while (data.event == -1) { | |
860 | main_loop_wait(false); | |
861 | } | |
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); | |
865 | ||
866 | qemu_thread_join(&thread); | |
867 | ||
868 | if (!reconnected) { | |
869 | reconnected = true; | |
870 | goto reconnect; | |
871 | } | |
872 | ||
873 | qapi_free_SocketAddress(addr); | |
874 | object_unparent(OBJECT(chr)); | |
875 | g_free(optstr); | |
876 | g_unsetenv("QTEST_SILENT_ERRORS"); | |
877 | } | |
878 | ||
879 | ||
880 | static gpointer | |
881 | char_socket_client_server_thread(gpointer data) | |
882 | { | |
883 | QIOChannelSocket *ioc = data; | |
884 | QIOChannelSocket *cioc; | |
885 | ||
2b61bb71 | 886 | retry: |
9baa6802 DB |
887 | cioc = qio_channel_socket_accept(ioc, &error_abort); |
888 | g_assert_nonnull(cioc); | |
889 | ||
2b61bb71 LF |
890 | if (char_socket_ping_pong(QIO_CHANNEL(cioc), NULL) != 0) { |
891 | goto retry; | |
892 | } | |
9baa6802 DB |
893 | |
894 | return NULL; | |
895 | } | |
896 | ||
897 | ||
898 | typedef struct { | |
899 | SocketAddress *addr; | |
900 | const char *reconnect; | |
901 | bool wait_connected; | |
902 | bool fd_pass; | |
2b61bb71 | 903 | char_socket_cb event_cb; |
9baa6802 DB |
904 | } CharSocketClientTestConfig; |
905 | ||
68066019 MAL |
906 | static void char_socket_client_dupid_test(gconstpointer opaque) |
907 | { | |
908 | const CharSocketClientTestConfig *config = opaque; | |
909 | QIOChannelSocket *ioc; | |
910 | char *optstr; | |
911 | Chardev *chr1, *chr2; | |
912 | SocketAddress *addr; | |
913 | QemuOpts *opts; | |
914 | Error *local_err = NULL; | |
915 | ||
916 | /* | |
917 | * Setup a listener socket and determine get its address | |
918 | * so we know the TCP port for the client later | |
919 | */ | |
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); | |
925 | ||
926 | /* | |
927 | * Populate the chardev address based on what the server | |
928 | * is actually listening on | |
929 | */ | |
930 | optstr = char_socket_addr_to_opt_str(addr, | |
931 | config->fd_pass, | |
932 | config->reconnect, | |
933 | false); | |
934 | ||
935 | opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), | |
936 | optstr, true); | |
937 | g_assert_nonnull(opts); | |
938 | chr1 = qemu_chr_new_from_opts(opts, NULL, &error_abort); | |
939 | g_assert_nonnull(chr1); | |
940 | ||
941 | chr2 = qemu_chr_new_from_opts(opts, NULL, &local_err); | |
942 | g_assert_null(chr2); | |
943 | error_free_or_abort(&local_err); | |
944 | ||
945 | object_unref(OBJECT(ioc)); | |
946 | qemu_opts_del(opts); | |
947 | object_unparent(OBJECT(chr1)); | |
948 | qapi_free_SocketAddress(addr); | |
949 | g_free(optstr); | |
950 | } | |
951 | ||
9baa6802 DB |
952 | static void char_socket_client_test(gconstpointer opaque) |
953 | { | |
954 | const CharSocketClientTestConfig *config = opaque; | |
2b61bb71 | 955 | const char_socket_cb event_cb = config->event_cb; |
9baa6802 DB |
956 | QIOChannelSocket *ioc; |
957 | char *optstr; | |
958 | Chardev *chr; | |
959 | CharBackend be = {0}; | |
960 | CharSocketTestData data = {0}; | |
961 | SocketAddress *addr; | |
962 | QemuThread thread; | |
963 | int ret; | |
964 | bool reconnected = false; | |
965 | QemuOpts *opts; | |
966 | ||
967 | /* | |
968 | * Setup a listener socket and determine get its address | |
969 | * so we know the TCP port for the client later | |
970 | */ | |
971 | ioc = qio_channel_socket_new(); | |
972 | g_assert_nonnull(ioc); | |
4e2d8bf6 | 973 | qio_channel_socket_listen_sync(ioc, config->addr, 1, &error_abort); |
9baa6802 DB |
974 | addr = qio_channel_socket_get_local_address(ioc, &error_abort); |
975 | g_assert_nonnull(addr); | |
976 | ||
977 | /* | |
978 | * Kick off a thread to act as the "remote" client | |
979 | * which just plays ping-pong with us | |
980 | */ | |
981 | qemu_thread_create(&thread, "client", | |
982 | char_socket_client_server_thread, | |
983 | ioc, QEMU_THREAD_JOINABLE); | |
984 | ||
985 | /* | |
986 | * Populate the chardev address based on what the server | |
987 | * is actually listening on | |
988 | */ | |
989 | optstr = char_socket_addr_to_opt_str(addr, | |
990 | config->fd_pass, | |
991 | config->reconnect, | |
992 | false); | |
993 | ||
994 | opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), | |
995 | optstr, true); | |
996 | g_assert_nonnull(opts); | |
4ad6f6cb | 997 | chr = qemu_chr_new_from_opts(opts, NULL, &error_abort); |
9baa6802 DB |
998 | qemu_opts_del(opts); |
999 | g_assert_nonnull(chr); | |
1000 | ||
1001 | if (config->reconnect) { | |
1002 | /* | |
1003 | * If reconnect is set, the connection will be | |
1004 | * established in a background thread and we won't | |
1005 | * see the "connected" status updated until we | |
1006 | * run the main event loop, or call qemu_chr_wait_connected | |
1007 | */ | |
1008 | g_assert(!object_property_get_bool(OBJECT(chr), "connected", | |
1009 | &error_abort)); | |
1010 | } else { | |
1011 | g_assert(object_property_get_bool(OBJECT(chr), "connected", | |
1012 | &error_abort)); | |
1013 | } | |
1014 | ||
1015 | qemu_chr_fe_init(&be, chr, &error_abort); | |
1016 | ||
1017 | reconnect: | |
1018 | data.event = -1; | |
2b61bb71 | 1019 | data.be = &be; |
9baa6802 | 1020 | qemu_chr_fe_set_handlers(&be, NULL, NULL, |
2b61bb71 | 1021 | event_cb, NULL, |
9baa6802 DB |
1022 | &data, NULL, true); |
1023 | if (config->reconnect) { | |
1024 | g_assert(data.event == -1); | |
1025 | } else { | |
1026 | g_assert(data.event == CHR_EVENT_OPENED); | |
1027 | } | |
1028 | ||
1029 | if (config->wait_connected) { | |
1030 | /* | |
1031 | * Synchronously wait for the connection to complete | |
1032 | * This should be a no-op if reconnect is not set. | |
1033 | */ | |
1034 | qemu_chr_wait_connected(chr, &error_abort); | |
1035 | } else { | |
1036 | /* | |
1037 | * Asynchronously wait for the connection to be reported | |
1038 | * as complete when the background thread reports its | |
1039 | * status. | |
1040 | * The loop will short-circuit if reconnect was set | |
1041 | */ | |
1042 | while (data.event == -1) { | |
1043 | main_loop_wait(false); | |
1044 | } | |
1045 | } | |
1046 | g_assert(data.event == CHR_EVENT_OPENED); | |
1047 | data.event = -1; | |
1048 | g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort)); | |
1049 | ||
1050 | /* Send a greeting to the server */ | |
1051 | ret = qemu_chr_fe_write_all(&be, (const uint8_t *)SOCKET_PING, | |
1052 | sizeof(SOCKET_PING)); | |
1053 | g_assert_cmpint(ret, ==, sizeof(SOCKET_PING)); | |
1054 | g_assert(data.event == -1); | |
1055 | ||
1056 | /* Setup a callback to receive the reply to our greeting */ | |
1057 | qemu_chr_fe_set_handlers(&be, char_socket_can_read, | |
1058 | char_socket_read, | |
2b61bb71 | 1059 | event_cb, NULL, |
9baa6802 DB |
1060 | &data, NULL, true); |
1061 | g_assert(data.event == CHR_EVENT_OPENED); | |
1062 | data.event = -1; | |
1063 | ||
1064 | /* Wait for the server to go away */ | |
1065 | while (data.event == -1) { | |
1066 | main_loop_wait(false); | |
1067 | } | |
1068 | g_assert(data.event == CHR_EVENT_CLOSED); | |
1069 | g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort)); | |
1070 | g_assert(data.got_pong); | |
1071 | qemu_thread_join(&thread); | |
1072 | ||
1073 | if (config->reconnect && !reconnected) { | |
1074 | reconnected = true; | |
1075 | qemu_thread_create(&thread, "client", | |
1076 | char_socket_client_server_thread, | |
1077 | ioc, QEMU_THREAD_JOINABLE); | |
1078 | goto reconnect; | |
1079 | } | |
1080 | ||
1081 | object_unref(OBJECT(ioc)); | |
1082 | object_unparent(OBJECT(chr)); | |
1083 | qapi_free_SocketAddress(addr); | |
1084 | g_free(optstr); | |
1085 | } | |
1086 | ||
5b774fe5 | 1087 | static void |
083b266f | 1088 | count_closed_event(void *opaque, QEMUChrEvent event) |
5b774fe5 PB |
1089 | { |
1090 | int *count = opaque; | |
1091 | if (event == CHR_EVENT_CLOSED) { | |
1092 | (*count)++; | |
1093 | } | |
1094 | } | |
1095 | ||
1096 | static void | |
1097 | char_socket_discard_read(void *opaque, const uint8_t *buf, int size) | |
1098 | { | |
1099 | } | |
1100 | ||
1101 | static void char_socket_server_two_clients_test(gconstpointer opaque) | |
1102 | { | |
1103 | SocketAddress *incoming_addr = (gpointer) opaque; | |
1104 | Chardev *chr; | |
1105 | CharBackend be = {0}; | |
1106 | QObject *qaddr; | |
1107 | SocketAddress *addr; | |
1108 | Visitor *v; | |
1109 | char *optstr; | |
1110 | QemuOpts *opts; | |
1111 | QIOChannelSocket *ioc1, *ioc2; | |
1112 | int closed = 0; | |
1113 | ||
1114 | g_setenv("QTEST_SILENT_ERRORS", "1", 1); | |
1115 | /* | |
1116 | * We rely on addr containing "nowait", otherwise | |
1117 | * qemu_chr_new() will block until a client connects. We | |
1118 | * can't spawn our client thread though, because until | |
1119 | * qemu_chr_new() returns we don't know what TCP port was | |
1120 | * allocated by the OS | |
1121 | */ | |
1122 | optstr = char_socket_addr_to_opt_str(incoming_addr, | |
1123 | false, | |
1124 | NULL, | |
1125 | true); | |
1126 | opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), | |
1127 | optstr, true); | |
1128 | g_assert_nonnull(opts); | |
1129 | chr = qemu_chr_new_from_opts(opts, NULL, &error_abort); | |
1130 | qemu_opts_del(opts); | |
1131 | g_assert_nonnull(chr); | |
1132 | g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort)); | |
1133 | ||
1134 | qaddr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort); | |
1135 | g_assert_nonnull(qaddr); | |
1136 | ||
1137 | v = qobject_input_visitor_new(qaddr); | |
1138 | visit_type_SocketAddress(v, "addr", &addr, &error_abort); | |
1139 | visit_free(v); | |
1140 | qobject_unref(qaddr); | |
1141 | ||
1142 | qemu_chr_fe_init(&be, chr, &error_abort); | |
1143 | ||
1144 | qemu_chr_fe_set_handlers(&be, char_socket_can_read, char_socket_discard_read, | |
1145 | count_closed_event, NULL, | |
1146 | &closed, NULL, true); | |
1147 | ||
1148 | ioc1 = qio_channel_socket_new(); | |
1149 | qio_channel_socket_connect_sync(ioc1, addr, &error_abort); | |
1150 | qemu_chr_wait_connected(chr, &error_abort); | |
1151 | ||
1152 | /* switch the chardev to another context */ | |
1153 | GMainContext *ctx = g_main_context_new(); | |
1154 | qemu_chr_fe_set_handlers(&be, char_socket_can_read, char_socket_discard_read, | |
1155 | count_closed_event, NULL, | |
1156 | &closed, ctx, true); | |
1157 | ||
1158 | /* Start a second connection while the first is still connected. | |
1159 | * It will be placed in the listen() backlog, and connect() will | |
1160 | * succeed immediately. | |
1161 | */ | |
1162 | ioc2 = qio_channel_socket_new(); | |
1163 | qio_channel_socket_connect_sync(ioc2, addr, &error_abort); | |
1164 | ||
1165 | object_unref(OBJECT(ioc1)); | |
1166 | /* The two connections should now be processed serially. */ | |
1167 | while (g_main_context_iteration(ctx, TRUE)) { | |
1168 | if (closed == 1 && ioc2) { | |
1169 | object_unref(OBJECT(ioc2)); | |
1170 | ioc2 = NULL; | |
1171 | } | |
1172 | if (closed == 2) { | |
1173 | break; | |
1174 | } | |
1175 | } | |
1176 | ||
1177 | qapi_free_SocketAddress(addr); | |
1178 | object_unparent(OBJECT(chr)); | |
1179 | g_main_context_unref(ctx); | |
1180 | g_free(optstr); | |
1181 | g_unsetenv("QTEST_SILENT_ERRORS"); | |
1182 | } | |
1183 | ||
9baa6802 | 1184 | |
2d18ec29 | 1185 | #if defined(HAVE_CHARDEV_SERIAL) && !defined(WIN32) |
27d4c378 MAL |
1186 | static void char_serial_test(void) |
1187 | { | |
1188 | QemuOpts *opts; | |
1189 | Chardev *chr; | |
1190 | ||
1191 | opts = qemu_opts_create(qemu_find_opts("chardev"), "serial-id", | |
1192 | 1, &error_abort); | |
1193 | qemu_opt_set(opts, "backend", "serial", &error_abort); | |
1194 | qemu_opt_set(opts, "path", "/dev/null", &error_abort); | |
1195 | ||
4ad6f6cb | 1196 | chr = qemu_chr_new_from_opts(opts, NULL, NULL); |
27d4c378 MAL |
1197 | g_assert_nonnull(chr); |
1198 | /* TODO: add more tests with a pty */ | |
1199 | object_unparent(OBJECT(chr)); | |
1200 | ||
1201 | /* test tty alias */ | |
1202 | qemu_opt_set(opts, "backend", "tty", &error_abort); | |
297641d4 | 1203 | chr = qemu_chr_new_from_opts(opts, NULL, &error_abort); |
27d4c378 MAL |
1204 | g_assert_nonnull(chr); |
1205 | object_unparent(OBJECT(chr)); | |
1206 | ||
1207 | qemu_opts_del(opts); | |
1208 | } | |
1209 | #endif | |
1210 | ||
ae69e482 AN |
1211 | #ifndef _WIN32 |
1212 | static void char_file_fifo_test(void) | |
9dbaa4ce | 1213 | { |
ae69e482 AN |
1214 | Chardev *chr; |
1215 | CharBackend be; | |
9dbaa4ce | 1216 | char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL); |
ae69e482 | 1217 | char *fifo = g_build_filename(tmp_path, "fifo", NULL); |
9dbaa4ce | 1218 | char *out = g_build_filename(tmp_path, "out", NULL); |
ae69e482 AN |
1219 | ChardevFile file = { .in = fifo, |
1220 | .has_in = true, | |
1221 | .out = out }; | |
9dbaa4ce MAL |
1222 | ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE, |
1223 | .u.file.data = &file }; | |
ae69e482 AN |
1224 | FeHandler fe = { 0, }; |
1225 | int fd, ret; | |
1226 | ||
1227 | if (mkfifo(fifo, 0600) < 0) { | |
1228 | abort(); | |
1229 | } | |
1230 | ||
1231 | fd = open(fifo, O_RDWR); | |
1232 | ret = write(fd, "fifo-in", 8); | |
1233 | g_assert_cmpint(ret, ==, 8); | |
1234 | ||
1235 | chr = qemu_chardev_new("label-file", TYPE_CHARDEV_FILE, &backend, | |
4ad6f6cb | 1236 | NULL, &error_abort); |
ae69e482 AN |
1237 | |
1238 | qemu_chr_fe_init(&be, chr, &error_abort); | |
1239 | qemu_chr_fe_set_handlers(&be, | |
1240 | fe_can_read, | |
1241 | fe_read, | |
1242 | fe_event, | |
1243 | NULL, | |
1244 | &fe, NULL, true); | |
1245 | ||
1246 | g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK); | |
1247 | qmp_chardev_send_break("label-foo", NULL); | |
1248 | g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK); | |
1249 | qmp_chardev_send_break("label-file", NULL); | |
1250 | g_assert_cmpint(fe.last_event, ==, CHR_EVENT_BREAK); | |
1251 | ||
1252 | main_loop(); | |
1253 | ||
1254 | close(fd); | |
1255 | ||
1256 | g_assert_cmpint(fe.read_count, ==, 8); | |
1257 | g_assert_cmpstr(fe.read_buf, ==, "fifo-in"); | |
1258 | ||
1259 | qemu_chr_fe_deinit(&be, true); | |
1260 | ||
1261 | g_unlink(fifo); | |
1262 | g_free(fifo); | |
1263 | g_unlink(out); | |
1264 | g_free(out); | |
1265 | g_rmdir(tmp_path); | |
1266 | g_free(tmp_path); | |
1267 | } | |
1268 | #endif | |
1269 | ||
1270 | static void char_file_test_internal(Chardev *ext_chr, const char *filepath) | |
1271 | { | |
1272 | char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL); | |
1273 | char *out; | |
9dbaa4ce | 1274 | Chardev *chr; |
ae69e482 AN |
1275 | char *contents = NULL; |
1276 | ChardevFile file = {}; | |
1277 | ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE, | |
1278 | .u.file.data = &file }; | |
9dbaa4ce MAL |
1279 | gsize length; |
1280 | int ret; | |
1281 | ||
ae69e482 AN |
1282 | if (ext_chr) { |
1283 | chr = ext_chr; | |
1284 | out = g_strdup(filepath); | |
1285 | file.out = out; | |
1286 | } else { | |
1287 | out = g_build_filename(tmp_path, "out", NULL); | |
1288 | file.out = out; | |
1289 | chr = qemu_chardev_new(NULL, TYPE_CHARDEV_FILE, &backend, | |
4ad6f6cb | 1290 | NULL, &error_abort); |
ae69e482 | 1291 | } |
9dbaa4ce MAL |
1292 | ret = qemu_chr_write_all(chr, (uint8_t *)"hello!", 6); |
1293 | g_assert_cmpint(ret, ==, 6); | |
9dbaa4ce MAL |
1294 | |
1295 | ret = g_file_get_contents(out, &contents, &length, NULL); | |
1296 | g_assert(ret == TRUE); | |
1297 | g_assert_cmpint(length, ==, 6); | |
1298 | g_assert(strncmp(contents, "hello!", 6) == 0); | |
9dbaa4ce | 1299 | |
ae69e482 AN |
1300 | if (!ext_chr) { |
1301 | object_unref(OBJECT(chr)); | |
1302 | g_unlink(out); | |
9dbaa4ce | 1303 | } |
ae69e482 | 1304 | g_free(contents); |
9dbaa4ce MAL |
1305 | g_rmdir(tmp_path); |
1306 | g_free(tmp_path); | |
1307 | g_free(out); | |
1308 | } | |
1309 | ||
ae69e482 AN |
1310 | static void char_file_test(void) |
1311 | { | |
1312 | char_file_test_internal(NULL, NULL); | |
1313 | } | |
1314 | ||
ea3af47d MAL |
1315 | static void char_null_test(void) |
1316 | { | |
1317 | Error *err = NULL; | |
0ec7b3e7 | 1318 | Chardev *chr; |
ea3af47d MAL |
1319 | CharBackend be; |
1320 | int ret; | |
1321 | ||
1322 | chr = qemu_chr_find("label-null"); | |
1323 | g_assert_null(chr); | |
1324 | ||
4ad6f6cb | 1325 | chr = qemu_chr_new("label-null", "null", NULL); |
ea3af47d MAL |
1326 | chr = qemu_chr_find("label-null"); |
1327 | g_assert_nonnull(chr); | |
1328 | ||
1329 | g_assert(qemu_chr_has_feature(chr, | |
1330 | QEMU_CHAR_FEATURE_FD_PASS) == false); | |
1331 | g_assert(qemu_chr_has_feature(chr, | |
1332 | QEMU_CHAR_FEATURE_RECONNECTABLE) == false); | |
1333 | ||
1334 | /* check max avail */ | |
1335 | qemu_chr_fe_init(&be, chr, &error_abort); | |
1336 | qemu_chr_fe_init(&be, chr, &err); | |
1337 | error_free_or_abort(&err); | |
1338 | ||
1339 | /* deinit & reinit */ | |
1ce2610c | 1340 | qemu_chr_fe_deinit(&be, false); |
ea3af47d MAL |
1341 | qemu_chr_fe_init(&be, chr, &error_abort); |
1342 | ||
1343 | qemu_chr_fe_set_open(&be, true); | |
1344 | ||
1345 | qemu_chr_fe_set_handlers(&be, | |
1346 | fe_can_read, | |
1347 | fe_read, | |
1348 | fe_event, | |
81517ba3 | 1349 | NULL, |
39ab61c6 | 1350 | NULL, NULL, true); |
ea3af47d MAL |
1351 | |
1352 | ret = qemu_chr_fe_write(&be, (void *)"buf", 4); | |
1353 | g_assert_cmpint(ret, ==, 4); | |
1354 | ||
1ce2610c | 1355 | qemu_chr_fe_deinit(&be, true); |
ea3af47d MAL |
1356 | } |
1357 | ||
1358 | static void char_invalid_test(void) | |
1359 | { | |
0ec7b3e7 | 1360 | Chardev *chr; |
0bf62dc8 | 1361 | g_setenv("QTEST_SILENT_ERRORS", "1", 1); |
4ad6f6cb | 1362 | chr = qemu_chr_new("label-invalid", "invalid", NULL); |
ea3af47d | 1363 | g_assert_null(chr); |
0bf62dc8 | 1364 | g_unsetenv("QTEST_SILENT_ERRORS"); |
ea3af47d MAL |
1365 | } |
1366 | ||
7b5a9e45 AN |
1367 | static int chardev_change(void *opaque) |
1368 | { | |
1369 | return 0; | |
1370 | } | |
1371 | ||
1372 | static int chardev_change_denied(void *opaque) | |
1373 | { | |
1374 | return -1; | |
1375 | } | |
1376 | ||
1377 | static void char_hotswap_test(void) | |
1378 | { | |
1379 | char *chr_args; | |
1380 | Chardev *chr; | |
1381 | CharBackend be; | |
1382 | ||
1383 | gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL); | |
1384 | char *filename = g_build_filename(tmp_path, "file", NULL); | |
1385 | ChardevFile file = { .out = filename }; | |
1386 | ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE, | |
1387 | .u.file.data = &file }; | |
1388 | ChardevReturn *ret; | |
1389 | ||
1390 | int port; | |
1391 | int sock = make_udp_socket(&port); | |
1392 | g_assert_cmpint(sock, >, 0); | |
1393 | ||
1394 | chr_args = g_strdup_printf("udp:127.0.0.1:%d", port); | |
1395 | ||
4ad6f6cb | 1396 | chr = qemu_chr_new("chardev", chr_args, NULL); |
7b5a9e45 AN |
1397 | qemu_chr_fe_init(&be, chr, &error_abort); |
1398 | ||
1399 | /* check that chardev operates correctly */ | |
1400 | char_udp_test_internal(chr, sock); | |
1401 | ||
1402 | /* set the handler that denies the hotswap */ | |
1403 | qemu_chr_fe_set_handlers(&be, NULL, NULL, | |
1404 | NULL, chardev_change_denied, NULL, NULL, true); | |
1405 | ||
1406 | /* now, change is denied and has to keep the old backend operating */ | |
1407 | ret = qmp_chardev_change("chardev", &backend, NULL); | |
1408 | g_assert(!ret); | |
1409 | g_assert(be.chr == chr); | |
1410 | ||
1411 | char_udp_test_internal(chr, sock); | |
1412 | ||
1413 | /* now allow the change */ | |
1414 | qemu_chr_fe_set_handlers(&be, NULL, NULL, | |
1415 | NULL, chardev_change, NULL, NULL, true); | |
1416 | ||
1417 | /* has to succeed now */ | |
1418 | ret = qmp_chardev_change("chardev", &backend, &error_abort); | |
1419 | g_assert(be.chr != chr); | |
1420 | ||
1421 | close(sock); | |
1422 | chr = be.chr; | |
1423 | ||
1424 | /* run the file chardev test */ | |
1425 | char_file_test_internal(chr, filename); | |
1426 | ||
1427 | object_unparent(OBJECT(chr)); | |
1428 | ||
1429 | qapi_free_ChardevReturn(ret); | |
1430 | g_unlink(filename); | |
1431 | g_free(filename); | |
1432 | g_rmdir(tmp_path); | |
1433 | g_free(tmp_path); | |
1434 | g_free(chr_args); | |
1435 | } | |
1436 | ||
0c9956f8 PB |
1437 | static SocketAddress tcpaddr = { |
1438 | .type = SOCKET_ADDRESS_TYPE_INET, | |
1439 | .u.inet.host = (char *)"127.0.0.1", | |
1440 | .u.inet.port = (char *)"0", | |
1441 | }; | |
1442 | #ifndef WIN32 | |
1443 | static SocketAddress unixaddr = { | |
1444 | .type = SOCKET_ADDRESS_TYPE_UNIX, | |
1445 | .u.q_unix.path = (char *)"test-char.sock", | |
1446 | }; | |
1447 | #endif | |
1448 | ||
ea3af47d MAL |
1449 | int main(int argc, char **argv) |
1450 | { | |
e7b6ba41 MAL |
1451 | bool has_ipv4, has_ipv6; |
1452 | ||
a86c83d7 | 1453 | qemu_init_main_loop(&error_abort); |
d47fb5d1 | 1454 | socket_init(); |
a86c83d7 | 1455 | |
ea3af47d MAL |
1456 | g_test_init(&argc, &argv, NULL); |
1457 | ||
e7b6ba41 | 1458 | if (socket_check_protocol_support(&has_ipv4, &has_ipv6) < 0) { |
a4eb74a6 MAL |
1459 | g_printerr("socket_check_protocol_support() failed\n"); |
1460 | goto end; | |
e7b6ba41 MAL |
1461 | } |
1462 | ||
ea3af47d MAL |
1463 | module_call_init(MODULE_INIT_QOM); |
1464 | qemu_add_opts(&qemu_chardev_opts); | |
1465 | ||
1466 | g_test_add_func("/char/null", char_null_test); | |
1467 | g_test_add_func("/char/invalid", char_invalid_test); | |
1468 | g_test_add_func("/char/ringbuf", char_ringbuf_test); | |
1469 | g_test_add_func("/char/mux", char_mux_test); | |
79c8db5a MAL |
1470 | #ifdef _WIN32 |
1471 | g_test_add_func("/char/console/subprocess", char_console_test_subprocess); | |
1472 | g_test_add_func("/char/console", char_console_test); | |
1473 | #endif | |
ea3af47d MAL |
1474 | g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess); |
1475 | g_test_add_func("/char/stdio", char_stdio_test); | |
a86c83d7 MAL |
1476 | #ifndef _WIN32 |
1477 | g_test_add_func("/char/pipe", char_pipe_test); | |
1478 | #endif | |
9dbaa4ce | 1479 | g_test_add_func("/char/file", char_file_test); |
ae69e482 AN |
1480 | #ifndef _WIN32 |
1481 | g_test_add_func("/char/file-fifo", char_file_fifo_test); | |
1482 | #endif | |
9baa6802 | 1483 | |
9baa6802 | 1484 | #define SOCKET_SERVER_TEST(name, addr) \ |
0c9956f8 | 1485 | static CharSocketServerTestConfig server1 ## name = \ |
9baa6802 | 1486 | { addr, false, false }; \ |
0c9956f8 | 1487 | static CharSocketServerTestConfig server2 ## name = \ |
9baa6802 | 1488 | { addr, true, false }; \ |
0c9956f8 | 1489 | static CharSocketServerTestConfig server3 ## name = \ |
9baa6802 | 1490 | { addr, false, true }; \ |
0c9956f8 | 1491 | static CharSocketServerTestConfig server4 ## name = \ |
9baa6802 DB |
1492 | { addr, true, true }; \ |
1493 | g_test_add_data_func("/char/socket/server/mainloop/" # name, \ | |
1494 | &server1 ##name, char_socket_server_test); \ | |
1495 | g_test_add_data_func("/char/socket/server/wait-conn/" # name, \ | |
1496 | &server2 ##name, char_socket_server_test); \ | |
1497 | g_test_add_data_func("/char/socket/server/mainloop-fdpass/" # name, \ | |
1498 | &server3 ##name, char_socket_server_test); \ | |
1499 | g_test_add_data_func("/char/socket/server/wait-conn-fdpass/" # name, \ | |
1500 | &server4 ##name, char_socket_server_test) | |
1501 | ||
1502 | #define SOCKET_CLIENT_TEST(name, addr) \ | |
0c9956f8 | 1503 | static CharSocketClientTestConfig client1 ## name = \ |
68066019 | 1504 | { addr, NULL, false, false, char_socket_event }; \ |
0c9956f8 | 1505 | static CharSocketClientTestConfig client2 ## name = \ |
2b61bb71 | 1506 | { addr, NULL, true, false, char_socket_event }; \ |
0c9956f8 | 1507 | static CharSocketClientTestConfig client3 ## name = \ |
2b61bb71 | 1508 | { addr, ",reconnect=1", false, false, char_socket_event }; \ |
0c9956f8 | 1509 | static CharSocketClientTestConfig client4 ## name = \ |
2b61bb71 | 1510 | { addr, ",reconnect=1", true, false, char_socket_event }; \ |
0c9956f8 | 1511 | static CharSocketClientTestConfig client5 ## name = \ |
2b61bb71 | 1512 | { addr, NULL, false, true, char_socket_event }; \ |
0c9956f8 | 1513 | static CharSocketClientTestConfig client6 ## name = \ |
2b61bb71 LF |
1514 | { addr, NULL, true, true, char_socket_event }; \ |
1515 | static CharSocketClientTestConfig client7 ## name = \ | |
1516 | { addr, ",reconnect=1", true, false, \ | |
1517 | char_socket_event_with_error }; \ | |
68066019 MAL |
1518 | static CharSocketClientTestConfig client8 ## name = \ |
1519 | { addr, ",reconnect=1", false, false, char_socket_event }; \ | |
9baa6802 DB |
1520 | g_test_add_data_func("/char/socket/client/mainloop/" # name, \ |
1521 | &client1 ##name, char_socket_client_test); \ | |
1522 | g_test_add_data_func("/char/socket/client/wait-conn/" # name, \ | |
1523 | &client2 ##name, char_socket_client_test); \ | |
1524 | g_test_add_data_func("/char/socket/client/mainloop-reconnect/" # name, \ | |
1525 | &client3 ##name, char_socket_client_test); \ | |
1526 | g_test_add_data_func("/char/socket/client/wait-conn-reconnect/" # name, \ | |
1527 | &client4 ##name, char_socket_client_test); \ | |
1528 | g_test_add_data_func("/char/socket/client/mainloop-fdpass/" # name, \ | |
1529 | &client5 ##name, char_socket_client_test); \ | |
1530 | g_test_add_data_func("/char/socket/client/wait-conn-fdpass/" # name, \ | |
2b61bb71 LF |
1531 | &client6 ##name, char_socket_client_test); \ |
1532 | g_test_add_data_func("/char/socket/client/reconnect-error/" # name, \ | |
68066019 MAL |
1533 | &client7 ##name, char_socket_client_test); \ |
1534 | g_test_add_data_func("/char/socket/client/dupid-reconnect/" # name, \ | |
1535 | &client8 ##name, char_socket_client_dupid_test) | |
9baa6802 | 1536 | |
e7b6ba41 MAL |
1537 | if (has_ipv4) { |
1538 | SOCKET_SERVER_TEST(tcp, &tcpaddr); | |
1539 | SOCKET_CLIENT_TEST(tcp, &tcpaddr); | |
1540 | g_test_add_data_func("/char/socket/server/two-clients/tcp", &tcpaddr, | |
1541 | char_socket_server_two_clients_test); | |
1542 | } | |
9baa6802 DB |
1543 | #ifndef WIN32 |
1544 | SOCKET_SERVER_TEST(unix, &unixaddr); | |
1545 | SOCKET_CLIENT_TEST(unix, &unixaddr); | |
5b774fe5 PB |
1546 | g_test_add_data_func("/char/socket/server/two-clients/unix", &unixaddr, |
1547 | char_socket_server_two_clients_test); | |
9baa6802 DB |
1548 | #endif |
1549 | ||
bb149048 | 1550 | g_test_add_func("/char/udp", char_udp_test); |
2d18ec29 | 1551 | #if defined(HAVE_CHARDEV_SERIAL) && !defined(WIN32) |
27d4c378 MAL |
1552 | g_test_add_func("/char/serial", char_serial_test); |
1553 | #endif | |
7b5a9e45 | 1554 | g_test_add_func("/char/hotswap", char_hotswap_test); |
125fc4a7 | 1555 | g_test_add_func("/char/websocket", char_websock_test); |
ea3af47d | 1556 | |
a4eb74a6 | 1557 | end: |
ea3af47d MAL |
1558 | return g_test_run(); |
1559 | } |