1 #include "qemu/osdep.h"
3 #include "qemu-common.h"
4 #include "qemu/config-file.h"
5 #include "sysemu/char.h"
6 #include "sysemu/sysemu.h"
7 #include "qapi/error.h"
8 #include "qmp-commands.h"
10 typedef struct FeHandler
{
16 static int fe_can_read(void *opaque
)
18 FeHandler
*h
= opaque
;
20 return sizeof(h
->read_buf
) - h
->read_count
;
23 static void fe_read(void *opaque
, const uint8_t *buf
, int size
)
25 FeHandler
*h
= opaque
;
27 g_assert_cmpint(size
, <=, fe_can_read(opaque
));
29 memcpy(h
->read_buf
+ h
->read_count
, buf
, size
);
30 h
->read_count
+= size
;
33 static void fe_event(void *opaque
, int event
)
35 FeHandler
*h
= opaque
;
37 h
->last_event
= event
;
40 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
41 static void char_stdio_test_subprocess(void)
47 chr
= qemu_chr_new("label", "stdio");
48 g_assert_nonnull(chr
);
50 qemu_chr_fe_init(&be
, chr
, &error_abort
);
51 qemu_chr_fe_set_open(&be
, true);
52 ret
= qemu_chr_fe_write(&be
, (void *)"buf", 4);
53 g_assert_cmpint(ret
, ==, 4);
55 qemu_chr_fe_deinit(&be
);
59 static void char_stdio_test(void)
61 g_test_trap_subprocess("/char/stdio/subprocess", 0, 0);
62 g_test_trap_assert_passed();
63 g_test_trap_assert_stdout("buf");
68 static void char_ringbuf_test(void)
76 opts
= qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
78 qemu_opt_set(opts
, "backend", "ringbuf", &error_abort
);
80 qemu_opt_set(opts
, "size", "5", &error_abort
);
81 chr
= qemu_chr_new_from_opts(opts
, NULL
);
85 opts
= qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
87 qemu_opt_set(opts
, "backend", "ringbuf", &error_abort
);
88 qemu_opt_set(opts
, "size", "2", &error_abort
);
89 chr
= qemu_chr_new_from_opts(opts
, &error_abort
);
90 g_assert_nonnull(chr
);
93 qemu_chr_fe_init(&be
, chr
, &error_abort
);
94 ret
= qemu_chr_fe_write(&be
, (void *)"buff", 4);
95 g_assert_cmpint(ret
, ==, 4);
97 data
= qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort
);
98 g_assert_cmpstr(data
, ==, "ff");
101 data
= qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort
);
102 g_assert_cmpstr(data
, ==, "");
105 qemu_chr_fe_deinit(&be
);
106 qemu_chr_delete(chr
);
109 static void char_mux_test(void)
112 CharDriverState
*chr
, *base
;
114 FeHandler h1
= { 0, }, h2
= { 0, };
115 CharBackend chr_be1
, chr_be2
;
117 opts
= qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
119 qemu_opt_set(opts
, "backend", "ringbuf", &error_abort
);
120 qemu_opt_set(opts
, "size", "128", &error_abort
);
121 qemu_opt_set(opts
, "mux", "on", &error_abort
);
122 chr
= qemu_chr_new_from_opts(opts
, &error_abort
);
123 g_assert_nonnull(chr
);
126 qemu_chr_fe_init(&chr_be1
, chr
, &error_abort
);
127 qemu_chr_fe_set_handlers(&chr_be1
,
134 qemu_chr_fe_init(&chr_be2
, chr
, &error_abort
);
135 qemu_chr_fe_set_handlers(&chr_be2
,
141 qemu_chr_fe_take_focus(&chr_be2
);
143 base
= qemu_chr_find("mux-label-base");
144 g_assert_cmpint(qemu_chr_be_can_write(base
), !=, 0);
146 qemu_chr_be_write(base
, (void *)"hello", 6);
147 g_assert_cmpint(h1
.read_count
, ==, 0);
148 g_assert_cmpint(h2
.read_count
, ==, 6);
149 g_assert_cmpstr(h2
.read_buf
, ==, "hello");
153 qemu_chr_be_write(base
, (void *)"\1c", 2);
155 qemu_chr_be_write(base
, (void *)"hello", 6);
156 g_assert_cmpint(h2
.read_count
, ==, 0);
157 g_assert_cmpint(h1
.read_count
, ==, 6);
158 g_assert_cmpstr(h1
.read_buf
, ==, "hello");
161 /* remove first handler */
162 qemu_chr_fe_set_handlers(&chr_be1
, NULL
, NULL
, NULL
, NULL
, NULL
, true);
163 qemu_chr_be_write(base
, (void *)"hello", 6);
164 g_assert_cmpint(h1
.read_count
, ==, 0);
165 g_assert_cmpint(h2
.read_count
, ==, 0);
167 qemu_chr_be_write(base
, (void *)"\1c", 2);
168 qemu_chr_be_write(base
, (void *)"hello", 6);
169 g_assert_cmpint(h1
.read_count
, ==, 0);
170 g_assert_cmpint(h2
.read_count
, ==, 6);
171 g_assert_cmpstr(h2
.read_buf
, ==, "hello");
175 qemu_chr_be_write(base
, (void *)"\1?", 2);
176 data
= qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort
);
177 g_assert_cmpint(strlen(data
), !=, 0);
180 qemu_chr_fe_deinit(&chr_be1
);
181 qemu_chr_fe_deinit(&chr_be2
);
182 qemu_chr_delete(chr
);
185 static void char_null_test(void)
188 CharDriverState
*chr
;
192 chr
= qemu_chr_find("label-null");
195 chr
= qemu_chr_new("label-null", "null");
196 chr
= qemu_chr_find("label-null");
197 g_assert_nonnull(chr
);
199 g_assert(qemu_chr_has_feature(chr
,
200 QEMU_CHAR_FEATURE_FD_PASS
) == false);
201 g_assert(qemu_chr_has_feature(chr
,
202 QEMU_CHAR_FEATURE_RECONNECTABLE
) == false);
204 /* check max avail */
205 qemu_chr_fe_init(&be
, chr
, &error_abort
);
206 qemu_chr_fe_init(&be
, chr
, &err
);
207 error_free_or_abort(&err
);
209 /* deinit & reinit */
210 qemu_chr_fe_deinit(&be
);
211 qemu_chr_fe_init(&be
, chr
, &error_abort
);
213 qemu_chr_fe_set_open(&be
, true);
215 qemu_chr_fe_set_handlers(&be
,
221 ret
= qemu_chr_fe_write(&be
, (void *)"buf", 4);
222 g_assert_cmpint(ret
, ==, 4);
224 qemu_chr_fe_deinit(&be
);
225 qemu_chr_delete(chr
);
228 static void char_invalid_test(void)
230 CharDriverState
*chr
;
232 chr
= qemu_chr_new("label-invalid", "invalid");
236 int main(int argc
, char **argv
)
238 g_test_init(&argc
, &argv
, NULL
);
240 module_call_init(MODULE_INIT_QOM
);
241 qemu_add_opts(&qemu_chardev_opts
);
243 g_test_add_func("/char/null", char_null_test
);
244 g_test_add_func("/char/invalid", char_invalid_test
);
245 g_test_add_func("/char/ringbuf", char_ringbuf_test
);
246 g_test_add_func("/char/mux", char_mux_test
);
247 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
248 g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess
);
249 g_test_add_func("/char/stdio", char_stdio_test
);