xref: /qemu/tests/unit/test-io-channel-command.c (revision 195e14d0262c5676cb9ff4b253e4b2b15c19d3f0)
1 /*
2  * QEMU I/O channel command test
3  *
4  * Copyright (c) 2015 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #include "io/channel-command.h"
22 #include "io-channel-helpers.h"
23 
24 #ifndef WIN32
25 static void test_io_channel_command_fifo(bool async)
26 {
27 #define TEST_FIFO "tests/test-io-channel-command.fifo"
28     QIOChannel *src, *dst;
29     QIOChannelTest *test;
30     char *srcfifo = g_strdup_printf("PIPE:%s,wronly", TEST_FIFO);
31     char *dstfifo = g_strdup_printf("PIPE:%s,rdonly", TEST_FIFO);
32     const char *srcargv[] = {
33         "/bin/socat", "-", srcfifo, NULL,
34     };
35     const char *dstargv[] = {
36         "/bin/socat", dstfifo, "-", NULL,
37     };
38 
39     unlink(TEST_FIFO);
40     if (access("/bin/socat", X_OK) < 0) {
41         return; /* Pretend success if socat is not present */
42     }
43     if (mkfifo(TEST_FIFO, 0600) < 0) {
44         abort();
45     }
46     src = QIO_CHANNEL(qio_channel_command_new_spawn(srcargv,
47                                                     O_WRONLY,
48                                                     &error_abort));
49     dst = QIO_CHANNEL(qio_channel_command_new_spawn(dstargv,
50                                                     O_RDONLY,
51                                                     &error_abort));
52 
53     test = qio_channel_test_new();
54     qio_channel_test_run_threads(test, async, src, dst);
55     qio_channel_test_validate(test);
56 
57     object_unref(OBJECT(src));
58     object_unref(OBJECT(dst));
59 
60     g_free(srcfifo);
61     g_free(dstfifo);
62     unlink(TEST_FIFO);
63 }
64 
65 
66 static void test_io_channel_command_fifo_async(void)
67 {
68     test_io_channel_command_fifo(true);
69 }
70 
71 static void test_io_channel_command_fifo_sync(void)
72 {
73     test_io_channel_command_fifo(false);
74 }
75 
76 
77 static void test_io_channel_command_echo(bool async)
78 {
79     QIOChannel *ioc;
80     QIOChannelTest *test;
81     const char *socatargv[] = {
82         "/bin/socat", "-", "-", NULL,
83     };
84 
85     if (access("/bin/socat", X_OK) < 0) {
86         return; /* Pretend success if socat is not present */
87     }
88 
89     ioc = QIO_CHANNEL(qio_channel_command_new_spawn(socatargv,
90                                                     O_RDWR,
91                                                     &error_abort));
92     test = qio_channel_test_new();
93     qio_channel_test_run_threads(test, async, ioc, ioc);
94     qio_channel_test_validate(test);
95 
96     object_unref(OBJECT(ioc));
97 }
98 
99 
100 static void test_io_channel_command_echo_async(void)
101 {
102     test_io_channel_command_echo(true);
103 }
104 
105 static void test_io_channel_command_echo_sync(void)
106 {
107     test_io_channel_command_echo(false);
108 }
109 #endif
110 
111 int main(int argc, char **argv)
112 {
113     module_call_init(MODULE_INIT_QOM);
114 
115     g_test_init(&argc, &argv, NULL);
116 
117 #ifndef WIN32
118     g_test_add_func("/io/channel/command/fifo/sync",
119                     test_io_channel_command_fifo_sync);
120     g_test_add_func("/io/channel/command/fifo/async",
121                     test_io_channel_command_fifo_async);
122     g_test_add_func("/io/channel/command/echo/sync",
123                     test_io_channel_command_echo_sync);
124     g_test_add_func("/io/channel/command/echo/async",
125                     test_io_channel_command_echo_async);
126 #endif
127 
128     return g_test_run();
129 }
130