]>
Commit | Line | Data |
---|---|---|
ea3af47d | 1 | #include "qemu/osdep.h" |
a86c83d7 | 2 | #include <glib/gstdio.h> |
ea3af47d | 3 | |
ea3af47d | 4 | #include "qemu/config-file.h" |
922a01a0 | 5 | #include "qemu/option.h" |
d47fb5d1 | 6 | #include "qemu/sockets.h" |
4d43a603 | 7 | #include "chardev/char-fe.h" |
d45f80ba | 8 | #include "chardev/char-mux.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" | |
ea3af47d | 17 | |
a86c83d7 MAL |
18 | static bool quit; |
19 | ||
ea3af47d MAL |
20 | typedef struct FeHandler { |
21 | int read_count; | |
68cf36a7 AP |
22 | bool is_open; |
23 | int openclose_count; | |
24 | bool openclose_mismatch; | |
ea3af47d MAL |
25 | int last_event; |
26 | char read_buf[128]; | |
27 | } FeHandler; | |
28 | ||
a86c83d7 MAL |
29 | static void main_loop(void) |
30 | { | |
a86c83d7 MAL |
31 | quit = false; |
32 | do { | |
be59df79 | 33 | main_loop_wait(false); |
a86c83d7 MAL |
34 | } while (!quit); |
35 | } | |
a86c83d7 | 36 | |
ea3af47d MAL |
37 | static int fe_can_read(void *opaque) |
38 | { | |
39 | FeHandler *h = opaque; | |
40 | ||
41 | return sizeof(h->read_buf) - h->read_count; | |
42 | } | |
43 | ||
44 | static void fe_read(void *opaque, const uint8_t *buf, int size) | |
45 | { | |
46 | FeHandler *h = opaque; | |
47 | ||
48 | g_assert_cmpint(size, <=, fe_can_read(opaque)); | |
49 | ||
50 | memcpy(h->read_buf + h->read_count, buf, size); | |
51 | h->read_count += size; | |
a86c83d7 | 52 | quit = true; |
ea3af47d MAL |
53 | } |
54 | ||
55 | static void fe_event(void *opaque, int event) | |
56 | { | |
57 | FeHandler *h = opaque; | |
68cf36a7 | 58 | bool new_open_state; |
ea3af47d MAL |
59 | |
60 | h->last_event = event; | |
68cf36a7 AP |
61 | switch (event) { |
62 | case CHR_EVENT_BREAK: | |
63 | break; | |
64 | case CHR_EVENT_OPENED: | |
65 | case CHR_EVENT_CLOSED: | |
66 | h->openclose_count++; | |
67 | new_open_state = (event == CHR_EVENT_OPENED); | |
68 | if (h->is_open == new_open_state) { | |
69 | h->openclose_mismatch = true; | |
70 | } | |
71 | h->is_open = new_open_state; | |
72 | /* no break */ | |
73 | default: | |
bd1d5ad9 | 74 | quit = true; |
68cf36a7 | 75 | break; |
bd1d5ad9 | 76 | } |
ea3af47d MAL |
77 | } |
78 | ||
79c8db5a MAL |
79 | #ifdef _WIN32 |
80 | static void char_console_test_subprocess(void) | |
81 | { | |
82 | QemuOpts *opts; | |
83 | Chardev *chr; | |
84 | ||
85 | opts = qemu_opts_create(qemu_find_opts("chardev"), "console-label", | |
86 | 1, &error_abort); | |
87 | qemu_opt_set(opts, "backend", "console", &error_abort); | |
88 | ||
4ad6f6cb | 89 | chr = qemu_chr_new_from_opts(opts, NULL, NULL); |
79c8db5a MAL |
90 | g_assert_nonnull(chr); |
91 | ||
92 | qemu_chr_write_all(chr, (const uint8_t *)"CONSOLE", 7); | |
93 | ||
94 | qemu_opts_del(opts); | |
95 | object_unparent(OBJECT(chr)); | |
96 | } | |
97 | ||
98 | static void char_console_test(void) | |
99 | { | |
100 | g_test_trap_subprocess("/char/console/subprocess", 0, 0); | |
101 | g_test_trap_assert_passed(); | |
102 | g_test_trap_assert_stdout("CONSOLE"); | |
103 | } | |
104 | #endif | |
ea3af47d MAL |
105 | static void char_stdio_test_subprocess(void) |
106 | { | |
0ec7b3e7 | 107 | Chardev *chr; |
ea3af47d MAL |
108 | CharBackend be; |
109 | int ret; | |
110 | ||
4ad6f6cb | 111 | chr = qemu_chr_new("label", "stdio", NULL); |
ea3af47d MAL |
112 | g_assert_nonnull(chr); |
113 | ||
114 | qemu_chr_fe_init(&be, chr, &error_abort); | |
115 | qemu_chr_fe_set_open(&be, true); | |
116 | ret = qemu_chr_fe_write(&be, (void *)"buf", 4); | |
117 | g_assert_cmpint(ret, ==, 4); | |
118 | ||
1ce2610c | 119 | qemu_chr_fe_deinit(&be, true); |
ea3af47d MAL |
120 | } |
121 | ||
122 | static void char_stdio_test(void) | |
123 | { | |
124 | g_test_trap_subprocess("/char/stdio/subprocess", 0, 0); | |
125 | g_test_trap_assert_passed(); | |
126 | g_test_trap_assert_stdout("buf"); | |
127 | } | |
ea3af47d | 128 | |
ea3af47d MAL |
129 | static void char_ringbuf_test(void) |
130 | { | |
131 | QemuOpts *opts; | |
0ec7b3e7 | 132 | Chardev *chr; |
ea3af47d MAL |
133 | CharBackend be; |
134 | char *data; | |
135 | int ret; | |
136 | ||
137 | opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label", | |
138 | 1, &error_abort); | |
139 | qemu_opt_set(opts, "backend", "ringbuf", &error_abort); | |
140 | ||
141 | qemu_opt_set(opts, "size", "5", &error_abort); | |
4ad6f6cb | 142 | chr = qemu_chr_new_from_opts(opts, NULL, NULL); |
ea3af47d MAL |
143 | g_assert_null(chr); |
144 | qemu_opts_del(opts); | |
145 | ||
146 | opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label", | |
147 | 1, &error_abort); | |
148 | qemu_opt_set(opts, "backend", "ringbuf", &error_abort); | |
149 | qemu_opt_set(opts, "size", "2", &error_abort); | |
4ad6f6cb | 150 | chr = qemu_chr_new_from_opts(opts, NULL, &error_abort); |
ea3af47d MAL |
151 | g_assert_nonnull(chr); |
152 | qemu_opts_del(opts); | |
153 | ||
154 | qemu_chr_fe_init(&be, chr, &error_abort); | |
155 | ret = qemu_chr_fe_write(&be, (void *)"buff", 4); | |
156 | g_assert_cmpint(ret, ==, 4); | |
157 | ||
158 | data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort); | |
159 | g_assert_cmpstr(data, ==, "ff"); | |
160 | g_free(data); | |
161 | ||
162 | data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort); | |
163 | g_assert_cmpstr(data, ==, ""); | |
164 | g_free(data); | |
165 | ||
1ce2610c | 166 | qemu_chr_fe_deinit(&be, true); |
e24ef0db MAL |
167 | |
168 | /* check alias */ | |
169 | opts = qemu_opts_create(qemu_find_opts("chardev"), "memory-label", | |
170 | 1, &error_abort); | |
171 | qemu_opt_set(opts, "backend", "memory", &error_abort); | |
172 | qemu_opt_set(opts, "size", "2", &error_abort); | |
4ad6f6cb | 173 | chr = qemu_chr_new_from_opts(opts, NULL, NULL); |
e24ef0db MAL |
174 | g_assert_nonnull(chr); |
175 | object_unparent(OBJECT(chr)); | |
176 | qemu_opts_del(opts); | |
ea3af47d MAL |
177 | } |
178 | ||
179 | static void char_mux_test(void) | |
180 | { | |
181 | QemuOpts *opts; | |
0ec7b3e7 | 182 | Chardev *chr, *base; |
ea3af47d | 183 | char *data; |
68cf36a7 | 184 | FeHandler h1 = { 0, false, 0, false, }, h2 = { 0, false, 0, false, }; |
ea3af47d MAL |
185 | CharBackend chr_be1, chr_be2; |
186 | ||
187 | opts = qemu_opts_create(qemu_find_opts("chardev"), "mux-label", | |
188 | 1, &error_abort); | |
189 | qemu_opt_set(opts, "backend", "ringbuf", &error_abort); | |
190 | qemu_opt_set(opts, "size", "128", &error_abort); | |
191 | qemu_opt_set(opts, "mux", "on", &error_abort); | |
4ad6f6cb | 192 | chr = qemu_chr_new_from_opts(opts, NULL, &error_abort); |
ea3af47d MAL |
193 | g_assert_nonnull(chr); |
194 | qemu_opts_del(opts); | |
195 | ||
196 | qemu_chr_fe_init(&chr_be1, chr, &error_abort); | |
197 | qemu_chr_fe_set_handlers(&chr_be1, | |
198 | fe_can_read, | |
199 | fe_read, | |
200 | fe_event, | |
81517ba3 | 201 | NULL, |
ea3af47d | 202 | &h1, |
39ab61c6 | 203 | NULL, true); |
ea3af47d MAL |
204 | |
205 | qemu_chr_fe_init(&chr_be2, chr, &error_abort); | |
206 | qemu_chr_fe_set_handlers(&chr_be2, | |
207 | fe_can_read, | |
208 | fe_read, | |
209 | fe_event, | |
81517ba3 | 210 | NULL, |
ea3af47d | 211 | &h2, |
39ab61c6 | 212 | NULL, true); |
ea3af47d MAL |
213 | qemu_chr_fe_take_focus(&chr_be2); |
214 | ||
215 | base = qemu_chr_find("mux-label-base"); | |
216 | g_assert_cmpint(qemu_chr_be_can_write(base), !=, 0); | |
217 | ||
218 | qemu_chr_be_write(base, (void *)"hello", 6); | |
219 | g_assert_cmpint(h1.read_count, ==, 0); | |
220 | g_assert_cmpint(h2.read_count, ==, 6); | |
221 | g_assert_cmpstr(h2.read_buf, ==, "hello"); | |
222 | h2.read_count = 0; | |
223 | ||
d45f80ba MAL |
224 | g_assert_cmpint(h1.last_event, !=, 42); /* should be MUX_OUT or OPENED */ |
225 | g_assert_cmpint(h2.last_event, !=, 42); /* should be MUX_IN or OPENED */ | |
226 | /* sending event on the base broadcast to all fe, historical reasons? */ | |
227 | qemu_chr_be_event(base, 42); | |
228 | g_assert_cmpint(h1.last_event, ==, 42); | |
229 | g_assert_cmpint(h2.last_event, ==, 42); | |
230 | qemu_chr_be_event(chr, -1); | |
231 | g_assert_cmpint(h1.last_event, ==, 42); | |
232 | g_assert_cmpint(h2.last_event, ==, -1); | |
233 | ||
ea3af47d | 234 | /* switch focus */ |
eeaa6715 MAL |
235 | qemu_chr_be_write(base, (void *)"\1b", 2); |
236 | g_assert_cmpint(h1.last_event, ==, 42); | |
237 | g_assert_cmpint(h2.last_event, ==, CHR_EVENT_BREAK); | |
238 | ||
ea3af47d | 239 | qemu_chr_be_write(base, (void *)"\1c", 2); |
d45f80ba MAL |
240 | g_assert_cmpint(h1.last_event, ==, CHR_EVENT_MUX_IN); |
241 | g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT); | |
242 | qemu_chr_be_event(chr, -1); | |
243 | g_assert_cmpint(h1.last_event, ==, -1); | |
244 | g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT); | |
ea3af47d MAL |
245 | |
246 | qemu_chr_be_write(base, (void *)"hello", 6); | |
247 | g_assert_cmpint(h2.read_count, ==, 0); | |
248 | g_assert_cmpint(h1.read_count, ==, 6); | |
249 | g_assert_cmpstr(h1.read_buf, ==, "hello"); | |
250 | h1.read_count = 0; | |
251 | ||
eeaa6715 MAL |
252 | qemu_chr_be_write(base, (void *)"\1b", 2); |
253 | g_assert_cmpint(h1.last_event, ==, CHR_EVENT_BREAK); | |
254 | g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT); | |
255 | ||
68cf36a7 AP |
256 | /* open/close state and corresponding events */ |
257 | g_assert_true(qemu_chr_fe_backend_open(&chr_be1)); | |
258 | g_assert_true(qemu_chr_fe_backend_open(&chr_be2)); | |
259 | g_assert_true(h1.is_open); | |
260 | g_assert_false(h1.openclose_mismatch); | |
261 | g_assert_true(h2.is_open); | |
262 | g_assert_false(h2.openclose_mismatch); | |
263 | ||
264 | h1.openclose_count = h2.openclose_count = 0; | |
265 | ||
266 | qemu_chr_fe_set_handlers(&chr_be1, NULL, NULL, NULL, NULL, | |
267 | NULL, NULL, false); | |
268 | qemu_chr_fe_set_handlers(&chr_be2, NULL, NULL, NULL, NULL, | |
269 | NULL, NULL, false); | |
270 | g_assert_cmpint(h1.openclose_count, ==, 0); | |
271 | g_assert_cmpint(h2.openclose_count, ==, 0); | |
272 | ||
273 | h1.is_open = h2.is_open = false; | |
274 | qemu_chr_fe_set_handlers(&chr_be1, | |
275 | NULL, | |
276 | NULL, | |
277 | fe_event, | |
278 | NULL, | |
279 | &h1, | |
280 | NULL, false); | |
281 | qemu_chr_fe_set_handlers(&chr_be2, | |
282 | NULL, | |
283 | NULL, | |
284 | fe_event, | |
285 | NULL, | |
286 | &h2, | |
287 | NULL, false); | |
288 | g_assert_cmpint(h1.openclose_count, ==, 1); | |
289 | g_assert_false(h1.openclose_mismatch); | |
290 | g_assert_cmpint(h2.openclose_count, ==, 1); | |
291 | g_assert_false(h2.openclose_mismatch); | |
292 | ||
293 | qemu_chr_be_event(base, CHR_EVENT_CLOSED); | |
294 | qemu_chr_be_event(base, CHR_EVENT_OPENED); | |
295 | g_assert_cmpint(h1.openclose_count, ==, 3); | |
296 | g_assert_false(h1.openclose_mismatch); | |
297 | g_assert_cmpint(h2.openclose_count, ==, 3); | |
298 | g_assert_false(h2.openclose_mismatch); | |
299 | ||
300 | qemu_chr_fe_set_handlers(&chr_be2, | |
301 | fe_can_read, | |
302 | fe_read, | |
303 | fe_event, | |
304 | NULL, | |
305 | &h2, | |
306 | NULL, false); | |
307 | qemu_chr_fe_set_handlers(&chr_be1, | |
308 | fe_can_read, | |
309 | fe_read, | |
310 | fe_event, | |
311 | NULL, | |
312 | &h1, | |
313 | NULL, false); | |
314 | ||
ea3af47d | 315 | /* remove first handler */ |
81517ba3 AN |
316 | qemu_chr_fe_set_handlers(&chr_be1, NULL, NULL, NULL, NULL, |
317 | NULL, NULL, true); | |
ea3af47d MAL |
318 | qemu_chr_be_write(base, (void *)"hello", 6); |
319 | g_assert_cmpint(h1.read_count, ==, 0); | |
320 | g_assert_cmpint(h2.read_count, ==, 0); | |
321 | ||
322 | qemu_chr_be_write(base, (void *)"\1c", 2); | |
323 | qemu_chr_be_write(base, (void *)"hello", 6); | |
324 | g_assert_cmpint(h1.read_count, ==, 0); | |
325 | g_assert_cmpint(h2.read_count, ==, 6); | |
326 | g_assert_cmpstr(h2.read_buf, ==, "hello"); | |
327 | h2.read_count = 0; | |
328 | ||
329 | /* print help */ | |
330 | qemu_chr_be_write(base, (void *)"\1?", 2); | |
331 | data = qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort); | |
332 | g_assert_cmpint(strlen(data), !=, 0); | |
333 | g_free(data); | |
334 | ||
1ce2610c MAL |
335 | qemu_chr_fe_deinit(&chr_be1, false); |
336 | qemu_chr_fe_deinit(&chr_be2, true); | |
ea3af47d MAL |
337 | } |
338 | ||
0935700f | 339 | |
125fc4a7 JS |
340 | static void websock_server_read(void *opaque, const uint8_t *buf, int size) |
341 | { | |
342 | g_assert_cmpint(size, ==, 5); | |
343 | g_assert(memcmp(buf, "world", size) == 0); | |
344 | quit = true; | |
345 | } | |
346 | ||
347 | ||
348 | static int websock_server_can_read(void *opaque) | |
349 | { | |
350 | return 10; | |
351 | } | |
352 | ||
353 | ||
354 | static bool websock_check_http_headers(char *buf, int size) | |
355 | { | |
356 | int i; | |
357 | const char *ans[] = { "HTTP/1.1 101 Switching Protocols\r\n", | |
358 | "Server: QEMU VNC\r\n", | |
359 | "Upgrade: websocket\r\n", | |
360 | "Connection: Upgrade\r\n", | |
361 | "Sec-WebSocket-Accept:", | |
362 | "Sec-WebSocket-Protocol: binary\r\n" }; | |
363 | ||
364 | for (i = 0; i < 6; i++) { | |
365 | if (g_strstr_len(buf, size, ans[i]) == NULL) { | |
366 | return false; | |
367 | } | |
368 | } | |
369 | ||
370 | return true; | |
371 | } | |
372 | ||
373 | ||
374 | static void websock_client_read(void *opaque, const uint8_t *buf, int size) | |
375 | { | |
376 | const uint8_t ping[] = { 0x89, 0x85, /* Ping header */ | |
377 | 0x07, 0x77, 0x9e, 0xf9, /* Masking key */ | |
378 | 0x6f, 0x12, 0xf2, 0x95, 0x68 /* "hello" */ }; | |
379 | ||
380 | const uint8_t binary[] = { 0x82, 0x85, /* Binary header */ | |
381 | 0x74, 0x90, 0xb9, 0xdf, /* Masking key */ | |
382 | 0x03, 0xff, 0xcb, 0xb3, 0x10 /* "world" */ }; | |
383 | Chardev *chr_client = opaque; | |
384 | ||
385 | if (websock_check_http_headers((char *) buf, size)) { | |
386 | qemu_chr_fe_write(chr_client->be, ping, sizeof(ping)); | |
387 | } else if (buf[0] == 0x8a && buf[1] == 0x05) { | |
388 | g_assert(strncmp((char *) buf + 2, "hello", 5) == 0); | |
389 | qemu_chr_fe_write(chr_client->be, binary, sizeof(binary)); | |
390 | } else { | |
391 | g_assert(buf[0] == 0x88 && buf[1] == 0x16); | |
392 | g_assert(strncmp((char *) buf + 4, "peer requested close", 10) == 0); | |
393 | quit = true; | |
394 | } | |
395 | } | |
396 | ||
397 | ||
398 | static int websock_client_can_read(void *opaque) | |
399 | { | |
400 | return 4096; | |
401 | } | |
402 | ||
403 | ||
404 | static void char_websock_test(void) | |
405 | { | |
406 | QObject *addr; | |
407 | QDict *qdict; | |
408 | const char *port; | |
409 | char *tmp; | |
410 | char *handshake_port; | |
411 | CharBackend be; | |
412 | CharBackend client_be; | |
413 | Chardev *chr_client; | |
414 | Chardev *chr = qemu_chr_new("server", | |
4ad6f6cb | 415 | "websocket:127.0.0.1:0,server,nowait", NULL); |
125fc4a7 JS |
416 | const char handshake[] = "GET / HTTP/1.1\r\n" |
417 | "Upgrade: websocket\r\n" | |
418 | "Connection: Upgrade\r\n" | |
419 | "Host: localhost:%s\r\n" | |
420 | "Origin: http://localhost:%s\r\n" | |
421 | "Sec-WebSocket-Key: o9JHNiS3/0/0zYE1wa3yIw==\r\n" | |
422 | "Sec-WebSocket-Version: 13\r\n" | |
423 | "Sec-WebSocket-Protocol: binary\r\n\r\n"; | |
424 | const uint8_t close[] = { 0x88, 0x82, /* Close header */ | |
425 | 0xef, 0xaa, 0xc5, 0x97, /* Masking key */ | |
426 | 0xec, 0x42 /* Status code */ }; | |
427 | ||
428 | addr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort); | |
429 | qdict = qobject_to(QDict, addr); | |
430 | port = qdict_get_str(qdict, "port"); | |
431 | tmp = g_strdup_printf("tcp:127.0.0.1:%s", port); | |
432 | handshake_port = g_strdup_printf(handshake, port, port); | |
433 | qobject_unref(qdict); | |
434 | ||
435 | qemu_chr_fe_init(&be, chr, &error_abort); | |
436 | qemu_chr_fe_set_handlers(&be, websock_server_can_read, websock_server_read, | |
437 | NULL, NULL, chr, NULL, true); | |
438 | ||
4ad6f6cb | 439 | chr_client = qemu_chr_new("client", tmp, NULL); |
125fc4a7 JS |
440 | qemu_chr_fe_init(&client_be, chr_client, &error_abort); |
441 | qemu_chr_fe_set_handlers(&client_be, websock_client_can_read, | |
442 | websock_client_read, | |
443 | NULL, NULL, chr_client, NULL, true); | |
444 | g_free(tmp); | |
445 | ||
446 | qemu_chr_write_all(chr_client, | |
447 | (uint8_t *) handshake_port, | |
448 | strlen(handshake_port)); | |
449 | g_free(handshake_port); | |
450 | main_loop(); | |
451 | ||
452 | g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort)); | |
453 | g_assert(object_property_get_bool(OBJECT(chr_client), | |
454 | "connected", &error_abort)); | |
455 | ||
456 | qemu_chr_write_all(chr_client, close, sizeof(close)); | |
457 | main_loop(); | |
458 | ||
459 | object_unparent(OBJECT(chr_client)); | |
460 | object_unparent(OBJECT(chr)); | |
461 | } | |
462 | ||
463 | ||
a86c83d7 MAL |
464 | #ifndef _WIN32 |
465 | static void char_pipe_test(void) | |
466 | { | |
467 | gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL); | |
468 | gchar *tmp, *in, *out, *pipe = g_build_filename(tmp_path, "pipe", NULL); | |
469 | Chardev *chr; | |
470 | CharBackend be; | |
471 | int ret, fd; | |
472 | char buf[10]; | |
473 | FeHandler fe = { 0, }; | |
474 | ||
475 | in = g_strdup_printf("%s.in", pipe); | |
476 | if (mkfifo(in, 0600) < 0) { | |
477 | abort(); | |
478 | } | |
479 | out = g_strdup_printf("%s.out", pipe); | |
480 | if (mkfifo(out, 0600) < 0) { | |
481 | abort(); | |
482 | } | |
483 | ||
484 | tmp = g_strdup_printf("pipe:%s", pipe); | |
4ad6f6cb | 485 | chr = qemu_chr_new("pipe", tmp, NULL); |
a86c83d7 MAL |
486 | g_assert_nonnull(chr); |
487 | g_free(tmp); | |
488 | ||
489 | qemu_chr_fe_init(&be, chr, &error_abort); | |
490 | ||
491 | ret = qemu_chr_fe_write(&be, (void *)"pipe-out", 9); | |
492 | g_assert_cmpint(ret, ==, 9); | |
493 | ||
494 | fd = open(out, O_RDWR); | |
495 | ret = read(fd, buf, sizeof(buf)); | |
496 | g_assert_cmpint(ret, ==, 9); | |
497 | g_assert_cmpstr(buf, ==, "pipe-out"); | |
498 | close(fd); | |
499 | ||
500 | fd = open(in, O_WRONLY); | |
501 | ret = write(fd, "pipe-in", 8); | |
502 | g_assert_cmpint(ret, ==, 8); | |
503 | close(fd); | |
504 | ||
505 | qemu_chr_fe_set_handlers(&be, | |
506 | fe_can_read, | |
507 | fe_read, | |
508 | fe_event, | |
81517ba3 | 509 | NULL, |
a86c83d7 MAL |
510 | &fe, |
511 | NULL, true); | |
512 | ||
513 | main_loop(); | |
514 | ||
515 | g_assert_cmpint(fe.read_count, ==, 8); | |
516 | g_assert_cmpstr(fe.read_buf, ==, "pipe-in"); | |
517 | ||
1ce2610c | 518 | qemu_chr_fe_deinit(&be, true); |
a86c83d7 MAL |
519 | |
520 | g_assert(g_unlink(in) == 0); | |
521 | g_assert(g_unlink(out) == 0); | |
522 | g_assert(g_rmdir(tmp_path) == 0); | |
523 | g_free(in); | |
524 | g_free(out); | |
525 | g_free(tmp_path); | |
526 | g_free(pipe); | |
527 | } | |
528 | #endif | |
529 | ||
9baa6802 DB |
530 | typedef struct SocketIdleData { |
531 | GMainLoop *loop; | |
532 | Chardev *chr; | |
533 | bool conn_expected; | |
534 | CharBackend *be; | |
535 | CharBackend *client_be; | |
536 | } SocketIdleData; | |
537 | ||
538 | ||
539 | static void socket_read_hello(void *opaque, const uint8_t *buf, int size) | |
540 | { | |
541 | g_assert_cmpint(size, ==, 5); | |
542 | g_assert(strncmp((char *)buf, "hello", 5) == 0); | |
543 | ||
544 | quit = true; | |
545 | } | |
546 | ||
547 | static int socket_can_read_hello(void *opaque) | |
548 | { | |
549 | return 10; | |
550 | } | |
551 | ||
92ddfade | 552 | static int make_udp_socket(int *port) |
bb149048 | 553 | { |
92ddfade | 554 | struct sockaddr_in addr = { 0, }; |
bb149048 MAL |
555 | socklen_t alen = sizeof(addr); |
556 | int ret, sock = qemu_socket(PF_INET, SOCK_DGRAM, 0); | |
bb149048 MAL |
557 | |
558 | g_assert_cmpint(sock, >, 0); | |
559 | addr.sin_family = AF_INET ; | |
560 | addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
561 | addr.sin_port = 0; | |
562 | ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr)); | |
563 | g_assert_cmpint(ret, ==, 0); | |
564 | ret = getsockname(sock, (struct sockaddr *)&addr, &alen); | |
565 | g_assert_cmpint(ret, ==, 0); | |
566 | ||
92ddfade AN |
567 | *port = ntohs(addr.sin_port); |
568 | return sock; | |
569 | } | |
570 | ||
571 | static void char_udp_test_internal(Chardev *reuse_chr, int sock) | |
572 | { | |
573 | struct sockaddr_in other; | |
574 | SocketIdleData d = { 0, }; | |
575 | Chardev *chr; | |
576 | CharBackend *be; | |
577 | socklen_t alen = sizeof(other); | |
578 | int ret; | |
579 | char buf[10]; | |
580 | char *tmp = NULL; | |
581 | ||
582 | if (reuse_chr) { | |
583 | chr = reuse_chr; | |
584 | be = chr->be; | |
585 | } else { | |
586 | int port; | |
587 | sock = make_udp_socket(&port); | |
588 | tmp = g_strdup_printf("udp:127.0.0.1:%d", port); | |
4ad6f6cb | 589 | chr = qemu_chr_new("client", tmp, NULL); |
92ddfade AN |
590 | g_assert_nonnull(chr); |
591 | ||
592 | be = g_alloca(sizeof(CharBackend)); | |
593 | qemu_chr_fe_init(be, chr, &error_abort); | |
594 | } | |
bb149048 MAL |
595 | |
596 | d.chr = chr; | |
92ddfade | 597 | qemu_chr_fe_set_handlers(be, socket_can_read_hello, socket_read_hello, |
81517ba3 | 598 | NULL, NULL, &d, NULL, true); |
bb149048 MAL |
599 | ret = qemu_chr_write_all(chr, (uint8_t *)"hello", 5); |
600 | g_assert_cmpint(ret, ==, 5); | |
601 | ||
bb149048 MAL |
602 | ret = recvfrom(sock, buf, sizeof(buf), 0, |
603 | (struct sockaddr *)&other, &alen); | |
604 | g_assert_cmpint(ret, ==, 5); | |
605 | ret = sendto(sock, buf, 5, 0, (struct sockaddr *)&other, alen); | |
606 | g_assert_cmpint(ret, ==, 5); | |
607 | ||
608 | main_loop(); | |
609 | ||
92ddfade AN |
610 | if (!reuse_chr) { |
611 | close(sock); | |
612 | qemu_chr_fe_deinit(be, true); | |
613 | } | |
bb149048 | 614 | g_free(tmp); |
92ddfade AN |
615 | } |
616 | ||
617 | static void char_udp_test(void) | |
618 | { | |
619 | char_udp_test_internal(NULL, 0); | |
bb149048 MAL |
620 | } |
621 | ||
9baa6802 DB |
622 | |
623 | typedef struct { | |
624 | int event; | |
625 | bool got_pong; | |
626 | } CharSocketTestData; | |
627 | ||
628 | ||
629 | #define SOCKET_PING "Hello" | |
630 | #define SOCKET_PONG "World" | |
631 | ||
632 | ||
633 | static void | |
634 | char_socket_event(void *opaque, int event) | |
635 | { | |
636 | CharSocketTestData *data = opaque; | |
637 | data->event = event; | |
638 | } | |
639 | ||
640 | ||
641 | static void | |
642 | char_socket_read(void *opaque, const uint8_t *buf, int size) | |
643 | { | |
644 | CharSocketTestData *data = opaque; | |
645 | g_assert_cmpint(size, ==, sizeof(SOCKET_PONG)); | |
646 | g_assert(memcmp(buf, SOCKET_PONG, size) == 0); | |
647 | data->got_pong = true; | |
648 | } | |
649 | ||
650 | ||
651 | static int | |
652 | char_socket_can_read(void *opaque) | |
653 | { | |
654 | return sizeof(SOCKET_PONG); | |
655 | } | |
656 | ||
657 | ||
658 | static char * | |
659 | char_socket_addr_to_opt_str(SocketAddress *addr, bool fd_pass, | |
660 | const char *reconnect, bool is_listen) | |
661 | { | |
662 | if (fd_pass) { | |
663 | QIOChannelSocket *ioc = qio_channel_socket_new(); | |
664 | int fd; | |
665 | char *optstr; | |
666 | g_assert(!reconnect); | |
667 | if (is_listen) { | |
668 | qio_channel_socket_listen_sync(ioc, addr, &error_abort); | |
669 | } else { | |
670 | qio_channel_socket_connect_sync(ioc, addr, &error_abort); | |
671 | } | |
672 | fd = ioc->fd; | |
673 | ioc->fd = -1; | |
674 | optstr = g_strdup_printf("socket,id=cdev0,fd=%d%s", | |
675 | fd, is_listen ? ",server,nowait" : ""); | |
676 | object_unref(OBJECT(ioc)); | |
677 | return optstr; | |
678 | } else { | |
679 | switch (addr->type) { | |
680 | case SOCKET_ADDRESS_TYPE_INET: | |
681 | return g_strdup_printf("socket,id=cdev0,host=%s,port=%s%s%s", | |
682 | addr->u.inet.host, | |
683 | addr->u.inet.port, | |
684 | reconnect ? reconnect : "", | |
685 | is_listen ? ",server,nowait" : ""); | |
686 | ||
687 | case SOCKET_ADDRESS_TYPE_UNIX: | |
688 | return g_strdup_printf("socket,id=cdev0,path=%s%s%s", | |
689 | addr->u.q_unix.path, | |
690 | reconnect ? reconnect : "", | |
691 | is_listen ? ",server,nowait" : ""); | |
692 | ||
693 | default: | |
694 | g_assert_not_reached(); | |
695 | } | |
696 | } | |
697 | } | |
698 | ||
699 | ||
700 | static void | |
701 | char_socket_ping_pong(QIOChannel *ioc) | |
702 | { | |
703 | char greeting[sizeof(SOCKET_PING)]; | |
704 | const char *response = SOCKET_PONG; | |
705 | ||
706 | qio_channel_read_all(ioc, greeting, sizeof(greeting), &error_abort); | |
707 | ||
708 | g_assert(memcmp(greeting, SOCKET_PING, sizeof(greeting)) == 0); | |
709 | ||
710 | qio_channel_write_all(ioc, response, sizeof(SOCKET_PONG), &error_abort); | |
711 | ||
712 | object_unref(OBJECT(ioc)); | |
713 | } | |
714 | ||
715 | ||
716 | static gpointer | |
717 | char_socket_server_client_thread(gpointer data) | |
718 | { | |
719 | SocketAddress *addr = data; | |
720 | QIOChannelSocket *ioc = qio_channel_socket_new(); | |
721 | ||
722 | qio_channel_socket_connect_sync(ioc, addr, &error_abort); | |
723 | ||
724 | char_socket_ping_pong(QIO_CHANNEL(ioc)); | |
725 | ||
726 | return NULL; | |
727 | } | |
728 | ||
729 | ||
730 | typedef struct { | |
731 | SocketAddress *addr; | |
732 | bool wait_connected; | |
733 | bool fd_pass; | |
734 | } CharSocketServerTestConfig; | |
735 | ||
736 | ||
737 | static void char_socket_server_test(gconstpointer opaque) | |
738 | { | |
739 | const CharSocketServerTestConfig *config = opaque; | |
740 | Chardev *chr; | |
741 | CharBackend be = {0}; | |
742 | CharSocketTestData data = {0}; | |
743 | QObject *qaddr; | |
744 | SocketAddress *addr; | |
745 | Visitor *v; | |
746 | QemuThread thread; | |
747 | int ret; | |
748 | bool reconnected; | |
749 | char *optstr; | |
750 | QemuOpts *opts; | |
751 | ||
752 | g_setenv("QTEST_SILENT_ERRORS", "1", 1); | |
753 | /* | |
754 | * We rely on config->addr containing "nowait", otherwise | |
755 | * qemu_chr_new() will block until a client connects. We | |
756 | * can't spawn our client thread though, because until | |
757 | * qemu_chr_new() returns we don't know what TCP port was | |
758 | * allocated by the OS | |
759 | */ | |
760 | optstr = char_socket_addr_to_opt_str(config->addr, | |
761 | config->fd_pass, | |
762 | NULL, | |
763 | true); | |
764 | opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), | |
765 | optstr, true); | |
766 | g_assert_nonnull(opts); | |
4ad6f6cb | 767 | chr = qemu_chr_new_from_opts(opts, NULL, &error_abort); |
9baa6802 DB |
768 | qemu_opts_del(opts); |
769 | g_assert_nonnull(chr); | |
770 | g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort)); | |
771 | ||
772 | qaddr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort); | |
773 | g_assert_nonnull(qaddr); | |
774 | ||
775 | v = qobject_input_visitor_new(qaddr); | |
776 | visit_type_SocketAddress(v, "addr", &addr, &error_abort); | |
777 | visit_free(v); | |
778 | qobject_unref(qaddr); | |
779 | ||
780 | qemu_chr_fe_init(&be, chr, &error_abort); | |
781 | ||
782 | reconnect: | |
783 | data.event = -1; | |
784 | qemu_chr_fe_set_handlers(&be, NULL, NULL, | |
785 | char_socket_event, NULL, | |
786 | &data, NULL, true); | |
787 | g_assert(data.event == -1); | |
788 | ||
789 | /* | |
790 | * Kick off a thread to act as the "remote" client | |
791 | * which just plays ping-pong with us | |
792 | */ | |
793 | qemu_thread_create(&thread, "client", | |
794 | char_socket_server_client_thread, | |
795 | addr, QEMU_THREAD_JOINABLE); | |
796 | g_assert(data.event == -1); | |
797 | ||
798 | if (config->wait_connected) { | |
799 | /* Synchronously accept a connection */ | |
800 | qemu_chr_wait_connected(chr, &error_abort); | |
801 | } else { | |
802 | /* | |
803 | * Asynchronously accept a connection when the evnt | |
804 | * loop reports the listener socket as readable | |
805 | */ | |
806 | while (data.event == -1) { | |
807 | main_loop_wait(false); | |
808 | } | |
809 | } | |
810 | g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort)); | |
811 | g_assert(data.event == CHR_EVENT_OPENED); | |
812 | data.event = -1; | |
813 | ||
814 | /* Send a greeting to the client */ | |
815 | ret = qemu_chr_fe_write_all(&be, (const uint8_t *)SOCKET_PING, | |
816 | sizeof(SOCKET_PING)); | |
817 | g_assert_cmpint(ret, ==, sizeof(SOCKET_PING)); | |
818 | g_assert(data.event == -1); | |
819 | ||
820 | /* Setup a callback to receive the reply to our greeting */ | |
821 | qemu_chr_fe_set_handlers(&be, char_socket_can_read, | |
822 | char_socket_read, | |
823 | char_socket_event, NULL, | |
824 | &data, NULL, true); | |
825 | g_assert(data.event == CHR_EVENT_OPENED); | |
826 | data.event = -1; | |
827 | ||
828 | /* Wait for the client to go away */ | |
829 | while (data.event == -1) { | |
830 | main_loop_wait(false); | |
831 | } | |
832 | g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort)); | |
833 | g_assert(data.event == CHR_EVENT_CLOSED); | |
834 | g_assert(data.got_pong); | |
835 | ||
836 | qemu_thread_join(&thread); | |
837 | ||
838 | if (!reconnected) { | |
839 | reconnected = true; | |
840 | goto reconnect; | |
841 | } | |
842 | ||
843 | qapi_free_SocketAddress(addr); | |
844 | object_unparent(OBJECT(chr)); | |
845 | g_free(optstr); | |
846 | g_unsetenv("QTEST_SILENT_ERRORS"); | |
847 | } | |
848 | ||
849 | ||
850 | static gpointer | |
851 | char_socket_client_server_thread(gpointer data) | |
852 | { | |
853 | QIOChannelSocket *ioc = data; | |
854 | QIOChannelSocket *cioc; | |
855 | ||
856 | cioc = qio_channel_socket_accept(ioc, &error_abort); | |
857 | g_assert_nonnull(cioc); | |
858 | ||
859 | char_socket_ping_pong(QIO_CHANNEL(cioc)); | |
860 | ||
861 | return NULL; | |
862 | } | |
863 | ||
864 | ||
865 | typedef struct { | |
866 | SocketAddress *addr; | |
867 | const char *reconnect; | |
868 | bool wait_connected; | |
869 | bool fd_pass; | |
870 | } CharSocketClientTestConfig; | |
871 | ||
872 | ||
873 | static void char_socket_client_test(gconstpointer opaque) | |
874 | { | |
875 | const CharSocketClientTestConfig *config = opaque; | |
876 | QIOChannelSocket *ioc; | |
877 | char *optstr; | |
878 | Chardev *chr; | |
879 | CharBackend be = {0}; | |
880 | CharSocketTestData data = {0}; | |
881 | SocketAddress *addr; | |
882 | QemuThread thread; | |
883 | int ret; | |
884 | bool reconnected = false; | |
885 | QemuOpts *opts; | |
886 | ||
887 | /* | |
888 | * Setup a listener socket and determine get its address | |
889 | * so we know the TCP port for the client later | |
890 | */ | |
891 | ioc = qio_channel_socket_new(); | |
892 | g_assert_nonnull(ioc); | |
893 | qio_channel_socket_listen_sync(ioc, config->addr, &error_abort); | |
894 | addr = qio_channel_socket_get_local_address(ioc, &error_abort); | |
895 | g_assert_nonnull(addr); | |
896 | ||
897 | /* | |
898 | * Kick off a thread to act as the "remote" client | |
899 | * which just plays ping-pong with us | |
900 | */ | |
901 | qemu_thread_create(&thread, "client", | |
902 | char_socket_client_server_thread, | |
903 | ioc, QEMU_THREAD_JOINABLE); | |
904 | ||
905 | /* | |
906 | * Populate the chardev address based on what the server | |
907 | * is actually listening on | |
908 | */ | |
909 | optstr = char_socket_addr_to_opt_str(addr, | |
910 | config->fd_pass, | |
911 | config->reconnect, | |
912 | false); | |
913 | ||
914 | opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), | |
915 | optstr, true); | |
916 | g_assert_nonnull(opts); | |
4ad6f6cb | 917 | chr = qemu_chr_new_from_opts(opts, NULL, &error_abort); |
9baa6802 DB |
918 | qemu_opts_del(opts); |
919 | g_assert_nonnull(chr); | |
920 | ||
921 | if (config->reconnect) { | |
922 | /* | |
923 | * If reconnect is set, the connection will be | |
924 | * established in a background thread and we won't | |
925 | * see the "connected" status updated until we | |
926 | * run the main event loop, or call qemu_chr_wait_connected | |
927 | */ | |
928 | g_assert(!object_property_get_bool(OBJECT(chr), "connected", | |
929 | &error_abort)); | |
930 | } else { | |
931 | g_assert(object_property_get_bool(OBJECT(chr), "connected", | |
932 | &error_abort)); | |
933 | } | |
934 | ||
935 | qemu_chr_fe_init(&be, chr, &error_abort); | |
936 | ||
937 | reconnect: | |
938 | data.event = -1; | |
939 | qemu_chr_fe_set_handlers(&be, NULL, NULL, | |
940 | char_socket_event, NULL, | |
941 | &data, NULL, true); | |
942 | if (config->reconnect) { | |
943 | g_assert(data.event == -1); | |
944 | } else { | |
945 | g_assert(data.event == CHR_EVENT_OPENED); | |
946 | } | |
947 | ||
948 | if (config->wait_connected) { | |
949 | /* | |
950 | * Synchronously wait for the connection to complete | |
951 | * This should be a no-op if reconnect is not set. | |
952 | */ | |
953 | qemu_chr_wait_connected(chr, &error_abort); | |
954 | } else { | |
955 | /* | |
956 | * Asynchronously wait for the connection to be reported | |
957 | * as complete when the background thread reports its | |
958 | * status. | |
959 | * The loop will short-circuit if reconnect was set | |
960 | */ | |
961 | while (data.event == -1) { | |
962 | main_loop_wait(false); | |
963 | } | |
964 | } | |
965 | g_assert(data.event == CHR_EVENT_OPENED); | |
966 | data.event = -1; | |
967 | g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort)); | |
968 | ||
969 | /* Send a greeting to the server */ | |
970 | ret = qemu_chr_fe_write_all(&be, (const uint8_t *)SOCKET_PING, | |
971 | sizeof(SOCKET_PING)); | |
972 | g_assert_cmpint(ret, ==, sizeof(SOCKET_PING)); | |
973 | g_assert(data.event == -1); | |
974 | ||
975 | /* Setup a callback to receive the reply to our greeting */ | |
976 | qemu_chr_fe_set_handlers(&be, char_socket_can_read, | |
977 | char_socket_read, | |
978 | char_socket_event, NULL, | |
979 | &data, NULL, true); | |
980 | g_assert(data.event == CHR_EVENT_OPENED); | |
981 | data.event = -1; | |
982 | ||
983 | /* Wait for the server to go away */ | |
984 | while (data.event == -1) { | |
985 | main_loop_wait(false); | |
986 | } | |
987 | g_assert(data.event == CHR_EVENT_CLOSED); | |
988 | g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort)); | |
989 | g_assert(data.got_pong); | |
990 | qemu_thread_join(&thread); | |
991 | ||
992 | if (config->reconnect && !reconnected) { | |
993 | reconnected = true; | |
994 | qemu_thread_create(&thread, "client", | |
995 | char_socket_client_server_thread, | |
996 | ioc, QEMU_THREAD_JOINABLE); | |
997 | goto reconnect; | |
998 | } | |
999 | ||
1000 | object_unref(OBJECT(ioc)); | |
1001 | object_unparent(OBJECT(chr)); | |
1002 | qapi_free_SocketAddress(addr); | |
1003 | g_free(optstr); | |
1004 | } | |
1005 | ||
1006 | ||
27d4c378 MAL |
1007 | #ifdef HAVE_CHARDEV_SERIAL |
1008 | static void char_serial_test(void) | |
1009 | { | |
1010 | QemuOpts *opts; | |
1011 | Chardev *chr; | |
1012 | ||
1013 | opts = qemu_opts_create(qemu_find_opts("chardev"), "serial-id", | |
1014 | 1, &error_abort); | |
1015 | qemu_opt_set(opts, "backend", "serial", &error_abort); | |
1016 | qemu_opt_set(opts, "path", "/dev/null", &error_abort); | |
1017 | ||
4ad6f6cb | 1018 | chr = qemu_chr_new_from_opts(opts, NULL, NULL); |
27d4c378 MAL |
1019 | g_assert_nonnull(chr); |
1020 | /* TODO: add more tests with a pty */ | |
1021 | object_unparent(OBJECT(chr)); | |
1022 | ||
1023 | /* test tty alias */ | |
1024 | qemu_opt_set(opts, "backend", "tty", &error_abort); | |
4ad6f6cb | 1025 | chr = qemu_chr_new_from_opts(opts, NULL, NULL); |
27d4c378 MAL |
1026 | g_assert_nonnull(chr); |
1027 | object_unparent(OBJECT(chr)); | |
1028 | ||
1029 | qemu_opts_del(opts); | |
1030 | } | |
1031 | #endif | |
1032 | ||
ae69e482 AN |
1033 | #ifndef _WIN32 |
1034 | static void char_file_fifo_test(void) | |
9dbaa4ce | 1035 | { |
ae69e482 AN |
1036 | Chardev *chr; |
1037 | CharBackend be; | |
9dbaa4ce | 1038 | char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL); |
ae69e482 | 1039 | char *fifo = g_build_filename(tmp_path, "fifo", NULL); |
9dbaa4ce | 1040 | char *out = g_build_filename(tmp_path, "out", NULL); |
ae69e482 AN |
1041 | ChardevFile file = { .in = fifo, |
1042 | .has_in = true, | |
1043 | .out = out }; | |
9dbaa4ce MAL |
1044 | ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE, |
1045 | .u.file.data = &file }; | |
ae69e482 AN |
1046 | FeHandler fe = { 0, }; |
1047 | int fd, ret; | |
1048 | ||
1049 | if (mkfifo(fifo, 0600) < 0) { | |
1050 | abort(); | |
1051 | } | |
1052 | ||
1053 | fd = open(fifo, O_RDWR); | |
1054 | ret = write(fd, "fifo-in", 8); | |
1055 | g_assert_cmpint(ret, ==, 8); | |
1056 | ||
1057 | chr = qemu_chardev_new("label-file", TYPE_CHARDEV_FILE, &backend, | |
4ad6f6cb | 1058 | NULL, &error_abort); |
ae69e482 AN |
1059 | |
1060 | qemu_chr_fe_init(&be, chr, &error_abort); | |
1061 | qemu_chr_fe_set_handlers(&be, | |
1062 | fe_can_read, | |
1063 | fe_read, | |
1064 | fe_event, | |
1065 | NULL, | |
1066 | &fe, NULL, true); | |
1067 | ||
1068 | g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK); | |
1069 | qmp_chardev_send_break("label-foo", NULL); | |
1070 | g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK); | |
1071 | qmp_chardev_send_break("label-file", NULL); | |
1072 | g_assert_cmpint(fe.last_event, ==, CHR_EVENT_BREAK); | |
1073 | ||
1074 | main_loop(); | |
1075 | ||
1076 | close(fd); | |
1077 | ||
1078 | g_assert_cmpint(fe.read_count, ==, 8); | |
1079 | g_assert_cmpstr(fe.read_buf, ==, "fifo-in"); | |
1080 | ||
1081 | qemu_chr_fe_deinit(&be, true); | |
1082 | ||
1083 | g_unlink(fifo); | |
1084 | g_free(fifo); | |
1085 | g_unlink(out); | |
1086 | g_free(out); | |
1087 | g_rmdir(tmp_path); | |
1088 | g_free(tmp_path); | |
1089 | } | |
1090 | #endif | |
1091 | ||
1092 | static void char_file_test_internal(Chardev *ext_chr, const char *filepath) | |
1093 | { | |
1094 | char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL); | |
1095 | char *out; | |
9dbaa4ce | 1096 | Chardev *chr; |
ae69e482 AN |
1097 | char *contents = NULL; |
1098 | ChardevFile file = {}; | |
1099 | ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE, | |
1100 | .u.file.data = &file }; | |
9dbaa4ce MAL |
1101 | gsize length; |
1102 | int ret; | |
1103 | ||
ae69e482 AN |
1104 | if (ext_chr) { |
1105 | chr = ext_chr; | |
1106 | out = g_strdup(filepath); | |
1107 | file.out = out; | |
1108 | } else { | |
1109 | out = g_build_filename(tmp_path, "out", NULL); | |
1110 | file.out = out; | |
1111 | chr = qemu_chardev_new(NULL, TYPE_CHARDEV_FILE, &backend, | |
4ad6f6cb | 1112 | NULL, &error_abort); |
ae69e482 | 1113 | } |
9dbaa4ce MAL |
1114 | ret = qemu_chr_write_all(chr, (uint8_t *)"hello!", 6); |
1115 | g_assert_cmpint(ret, ==, 6); | |
9dbaa4ce MAL |
1116 | |
1117 | ret = g_file_get_contents(out, &contents, &length, NULL); | |
1118 | g_assert(ret == TRUE); | |
1119 | g_assert_cmpint(length, ==, 6); | |
1120 | g_assert(strncmp(contents, "hello!", 6) == 0); | |
9dbaa4ce | 1121 | |
ae69e482 AN |
1122 | if (!ext_chr) { |
1123 | object_unref(OBJECT(chr)); | |
1124 | g_unlink(out); | |
9dbaa4ce | 1125 | } |
ae69e482 | 1126 | g_free(contents); |
9dbaa4ce MAL |
1127 | g_rmdir(tmp_path); |
1128 | g_free(tmp_path); | |
1129 | g_free(out); | |
1130 | } | |
1131 | ||
ae69e482 AN |
1132 | static void char_file_test(void) |
1133 | { | |
1134 | char_file_test_internal(NULL, NULL); | |
1135 | } | |
1136 | ||
ea3af47d MAL |
1137 | static void char_null_test(void) |
1138 | { | |
1139 | Error *err = NULL; | |
0ec7b3e7 | 1140 | Chardev *chr; |
ea3af47d MAL |
1141 | CharBackend be; |
1142 | int ret; | |
1143 | ||
1144 | chr = qemu_chr_find("label-null"); | |
1145 | g_assert_null(chr); | |
1146 | ||
4ad6f6cb | 1147 | chr = qemu_chr_new("label-null", "null", NULL); |
ea3af47d MAL |
1148 | chr = qemu_chr_find("label-null"); |
1149 | g_assert_nonnull(chr); | |
1150 | ||
1151 | g_assert(qemu_chr_has_feature(chr, | |
1152 | QEMU_CHAR_FEATURE_FD_PASS) == false); | |
1153 | g_assert(qemu_chr_has_feature(chr, | |
1154 | QEMU_CHAR_FEATURE_RECONNECTABLE) == false); | |
1155 | ||
1156 | /* check max avail */ | |
1157 | qemu_chr_fe_init(&be, chr, &error_abort); | |
1158 | qemu_chr_fe_init(&be, chr, &err); | |
1159 | error_free_or_abort(&err); | |
1160 | ||
1161 | /* deinit & reinit */ | |
1ce2610c | 1162 | qemu_chr_fe_deinit(&be, false); |
ea3af47d MAL |
1163 | qemu_chr_fe_init(&be, chr, &error_abort); |
1164 | ||
1165 | qemu_chr_fe_set_open(&be, true); | |
1166 | ||
1167 | qemu_chr_fe_set_handlers(&be, | |
1168 | fe_can_read, | |
1169 | fe_read, | |
1170 | fe_event, | |
81517ba3 | 1171 | NULL, |
39ab61c6 | 1172 | NULL, NULL, true); |
ea3af47d MAL |
1173 | |
1174 | ret = qemu_chr_fe_write(&be, (void *)"buf", 4); | |
1175 | g_assert_cmpint(ret, ==, 4); | |
1176 | ||
1ce2610c | 1177 | qemu_chr_fe_deinit(&be, true); |
ea3af47d MAL |
1178 | } |
1179 | ||
1180 | static void char_invalid_test(void) | |
1181 | { | |
0ec7b3e7 | 1182 | Chardev *chr; |
0bf62dc8 | 1183 | g_setenv("QTEST_SILENT_ERRORS", "1", 1); |
4ad6f6cb | 1184 | chr = qemu_chr_new("label-invalid", "invalid", NULL); |
ea3af47d | 1185 | g_assert_null(chr); |
0bf62dc8 | 1186 | g_unsetenv("QTEST_SILENT_ERRORS"); |
ea3af47d MAL |
1187 | } |
1188 | ||
7b5a9e45 AN |
1189 | static int chardev_change(void *opaque) |
1190 | { | |
1191 | return 0; | |
1192 | } | |
1193 | ||
1194 | static int chardev_change_denied(void *opaque) | |
1195 | { | |
1196 | return -1; | |
1197 | } | |
1198 | ||
1199 | static void char_hotswap_test(void) | |
1200 | { | |
1201 | char *chr_args; | |
1202 | Chardev *chr; | |
1203 | CharBackend be; | |
1204 | ||
1205 | gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL); | |
1206 | char *filename = g_build_filename(tmp_path, "file", NULL); | |
1207 | ChardevFile file = { .out = filename }; | |
1208 | ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE, | |
1209 | .u.file.data = &file }; | |
1210 | ChardevReturn *ret; | |
1211 | ||
1212 | int port; | |
1213 | int sock = make_udp_socket(&port); | |
1214 | g_assert_cmpint(sock, >, 0); | |
1215 | ||
1216 | chr_args = g_strdup_printf("udp:127.0.0.1:%d", port); | |
1217 | ||
4ad6f6cb | 1218 | chr = qemu_chr_new("chardev", chr_args, NULL); |
7b5a9e45 AN |
1219 | qemu_chr_fe_init(&be, chr, &error_abort); |
1220 | ||
1221 | /* check that chardev operates correctly */ | |
1222 | char_udp_test_internal(chr, sock); | |
1223 | ||
1224 | /* set the handler that denies the hotswap */ | |
1225 | qemu_chr_fe_set_handlers(&be, NULL, NULL, | |
1226 | NULL, chardev_change_denied, NULL, NULL, true); | |
1227 | ||
1228 | /* now, change is denied and has to keep the old backend operating */ | |
1229 | ret = qmp_chardev_change("chardev", &backend, NULL); | |
1230 | g_assert(!ret); | |
1231 | g_assert(be.chr == chr); | |
1232 | ||
1233 | char_udp_test_internal(chr, sock); | |
1234 | ||
1235 | /* now allow the change */ | |
1236 | qemu_chr_fe_set_handlers(&be, NULL, NULL, | |
1237 | NULL, chardev_change, NULL, NULL, true); | |
1238 | ||
1239 | /* has to succeed now */ | |
1240 | ret = qmp_chardev_change("chardev", &backend, &error_abort); | |
1241 | g_assert(be.chr != chr); | |
1242 | ||
1243 | close(sock); | |
1244 | chr = be.chr; | |
1245 | ||
1246 | /* run the file chardev test */ | |
1247 | char_file_test_internal(chr, filename); | |
1248 | ||
1249 | object_unparent(OBJECT(chr)); | |
1250 | ||
1251 | qapi_free_ChardevReturn(ret); | |
1252 | g_unlink(filename); | |
1253 | g_free(filename); | |
1254 | g_rmdir(tmp_path); | |
1255 | g_free(tmp_path); | |
1256 | g_free(chr_args); | |
1257 | } | |
1258 | ||
ea3af47d MAL |
1259 | int main(int argc, char **argv) |
1260 | { | |
a86c83d7 | 1261 | qemu_init_main_loop(&error_abort); |
d47fb5d1 | 1262 | socket_init(); |
a86c83d7 | 1263 | |
ea3af47d MAL |
1264 | g_test_init(&argc, &argv, NULL); |
1265 | ||
1266 | module_call_init(MODULE_INIT_QOM); | |
1267 | qemu_add_opts(&qemu_chardev_opts); | |
1268 | ||
1269 | g_test_add_func("/char/null", char_null_test); | |
1270 | g_test_add_func("/char/invalid", char_invalid_test); | |
1271 | g_test_add_func("/char/ringbuf", char_ringbuf_test); | |
1272 | g_test_add_func("/char/mux", char_mux_test); | |
79c8db5a MAL |
1273 | #ifdef _WIN32 |
1274 | g_test_add_func("/char/console/subprocess", char_console_test_subprocess); | |
1275 | g_test_add_func("/char/console", char_console_test); | |
1276 | #endif | |
ea3af47d MAL |
1277 | g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess); |
1278 | g_test_add_func("/char/stdio", char_stdio_test); | |
a86c83d7 MAL |
1279 | #ifndef _WIN32 |
1280 | g_test_add_func("/char/pipe", char_pipe_test); | |
1281 | #endif | |
9dbaa4ce | 1282 | g_test_add_func("/char/file", char_file_test); |
ae69e482 AN |
1283 | #ifndef _WIN32 |
1284 | g_test_add_func("/char/file-fifo", char_file_fifo_test); | |
1285 | #endif | |
9baa6802 DB |
1286 | |
1287 | SocketAddress tcpaddr = { | |
1288 | .type = SOCKET_ADDRESS_TYPE_INET, | |
1289 | .u.inet.host = (char *)"127.0.0.1", | |
1290 | .u.inet.port = (char *)"0", | |
1291 | }; | |
1292 | #ifndef WIN32 | |
1293 | SocketAddress unixaddr = { | |
1294 | .type = SOCKET_ADDRESS_TYPE_UNIX, | |
1295 | .u.q_unix.path = (char *)"test-char.sock", | |
1296 | }; | |
1297 | #endif | |
1298 | ||
1299 | #define SOCKET_SERVER_TEST(name, addr) \ | |
1300 | CharSocketServerTestConfig server1 ## name = \ | |
1301 | { addr, false, false }; \ | |
1302 | CharSocketServerTestConfig server2 ## name = \ | |
1303 | { addr, true, false }; \ | |
1304 | CharSocketServerTestConfig server3 ## name = \ | |
1305 | { addr, false, true }; \ | |
1306 | CharSocketServerTestConfig server4 ## name = \ | |
1307 | { addr, true, true }; \ | |
1308 | g_test_add_data_func("/char/socket/server/mainloop/" # name, \ | |
1309 | &server1 ##name, char_socket_server_test); \ | |
1310 | g_test_add_data_func("/char/socket/server/wait-conn/" # name, \ | |
1311 | &server2 ##name, char_socket_server_test); \ | |
1312 | g_test_add_data_func("/char/socket/server/mainloop-fdpass/" # name, \ | |
1313 | &server3 ##name, char_socket_server_test); \ | |
1314 | g_test_add_data_func("/char/socket/server/wait-conn-fdpass/" # name, \ | |
1315 | &server4 ##name, char_socket_server_test) | |
1316 | ||
1317 | #define SOCKET_CLIENT_TEST(name, addr) \ | |
1318 | CharSocketClientTestConfig client1 ## name = \ | |
1319 | { addr, NULL, false, false }; \ | |
1320 | CharSocketClientTestConfig client2 ## name = \ | |
1321 | { addr, NULL, true, false }; \ | |
1322 | CharSocketClientTestConfig client3 ## name = \ | |
1323 | { addr, ",reconnect=1", false }; \ | |
1324 | CharSocketClientTestConfig client4 ## name = \ | |
1325 | { addr, ",reconnect=1", true }; \ | |
1326 | CharSocketClientTestConfig client5 ## name = \ | |
1327 | { addr, NULL, false, true }; \ | |
1328 | CharSocketClientTestConfig client6 ## name = \ | |
1329 | { addr, NULL, true, true }; \ | |
1330 | g_test_add_data_func("/char/socket/client/mainloop/" # name, \ | |
1331 | &client1 ##name, char_socket_client_test); \ | |
1332 | g_test_add_data_func("/char/socket/client/wait-conn/" # name, \ | |
1333 | &client2 ##name, char_socket_client_test); \ | |
1334 | g_test_add_data_func("/char/socket/client/mainloop-reconnect/" # name, \ | |
1335 | &client3 ##name, char_socket_client_test); \ | |
1336 | g_test_add_data_func("/char/socket/client/wait-conn-reconnect/" # name, \ | |
1337 | &client4 ##name, char_socket_client_test); \ | |
1338 | g_test_add_data_func("/char/socket/client/mainloop-fdpass/" # name, \ | |
1339 | &client5 ##name, char_socket_client_test); \ | |
1340 | g_test_add_data_func("/char/socket/client/wait-conn-fdpass/" # name, \ | |
1341 | &client6 ##name, char_socket_client_test) | |
1342 | ||
1343 | SOCKET_SERVER_TEST(tcp, &tcpaddr); | |
1344 | SOCKET_CLIENT_TEST(tcp, &tcpaddr); | |
1345 | #ifndef WIN32 | |
1346 | SOCKET_SERVER_TEST(unix, &unixaddr); | |
1347 | SOCKET_CLIENT_TEST(unix, &unixaddr); | |
1348 | #endif | |
1349 | ||
1350 | ||
bb149048 | 1351 | g_test_add_func("/char/udp", char_udp_test); |
27d4c378 MAL |
1352 | #ifdef HAVE_CHARDEV_SERIAL |
1353 | g_test_add_func("/char/serial", char_serial_test); | |
1354 | #endif | |
7b5a9e45 | 1355 | g_test_add_func("/char/hotswap", char_hotswap_test); |
125fc4a7 | 1356 | g_test_add_func("/char/websocket", char_websock_test); |
ea3af47d MAL |
1357 | |
1358 | return g_test_run(); | |
1359 | } |