1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "qemu/osdep.h"
22#include "io/channel-command.h"
23#include "io-channel-helpers.h"
24#include "qapi/error.h"
25
26#ifndef WIN32
27static void test_io_channel_command_fifo(bool async)
28{
29#define TEST_FIFO "tests/test-io-channel-command.fifo"
30 QIOChannel *src, *dst;
31 QIOChannelTest *test;
32 char *srcfifo = g_strdup_printf("PIPE:%s,wronly", TEST_FIFO);
33 char *dstfifo = g_strdup_printf("PIPE:%s,rdonly", TEST_FIFO);
34 const char *srcargv[] = {
35 "/bin/socat", "-", srcfifo, NULL,
36 };
37 const char *dstargv[] = {
38 "/bin/socat", dstfifo, "-", NULL,
39 };
40
41 unlink(TEST_FIFO);
42 if (access("/bin/socat", X_OK) < 0) {
43 return;
44 }
45 if (mkfifo(TEST_FIFO, 0600) < 0) {
46 abort();
47 }
48 src = QIO_CHANNEL(qio_channel_command_new_spawn(srcargv,
49 O_WRONLY,
50 &error_abort));
51 dst = QIO_CHANNEL(qio_channel_command_new_spawn(dstargv,
52 O_RDONLY,
53 &error_abort));
54
55 test = qio_channel_test_new();
56 qio_channel_test_run_threads(test, async, src, dst);
57 qio_channel_test_validate(test);
58
59 object_unref(OBJECT(src));
60 object_unref(OBJECT(dst));
61
62 g_free(srcfifo);
63 g_free(dstfifo);
64 unlink(TEST_FIFO);
65}
66
67
68static void test_io_channel_command_fifo_async(void)
69{
70 test_io_channel_command_fifo(true);
71}
72
73static void test_io_channel_command_fifo_sync(void)
74{
75 test_io_channel_command_fifo(false);
76}
77
78
79static void test_io_channel_command_echo(bool async)
80{
81 QIOChannel *ioc;
82 QIOChannelTest *test;
83 const char *socatargv[] = {
84 "/bin/socat", "-", "-", NULL,
85 };
86
87 if (access("/bin/socat", X_OK) < 0) {
88 return;
89 }
90
91 ioc = QIO_CHANNEL(qio_channel_command_new_spawn(socatargv,
92 O_RDWR,
93 &error_abort));
94 test = qio_channel_test_new();
95 qio_channel_test_run_threads(test, async, ioc, ioc);
96 qio_channel_test_validate(test);
97
98 object_unref(OBJECT(ioc));
99}
100
101
102static void test_io_channel_command_echo_async(void)
103{
104 test_io_channel_command_echo(true);
105}
106
107static void test_io_channel_command_echo_sync(void)
108{
109 test_io_channel_command_echo(false);
110}
111#endif
112
113int main(int argc, char **argv)
114{
115 module_call_init(MODULE_INIT_QOM);
116
117 g_test_init(&argc, &argv, NULL);
118
119#ifndef WIN32
120 g_test_add_func("/io/channel/command/fifo/sync",
121 test_io_channel_command_fifo_sync);
122 g_test_add_func("/io/channel/command/fifo/async",
123 test_io_channel_command_fifo_async);
124 g_test_add_func("/io/channel/command/echo/sync",
125 test_io_channel_command_echo_sync);
126 g_test_add_func("/io/channel/command/echo/async",
127 test_io_channel_command_echo_async);
128#endif
129
130 return g_test_run();
131}
132