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.1 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 "qemu/osdep.h"
22 #include "io/channel-command.h"
23 #include "io-channel-helpers.h"
24 #include "qapi/error.h"
25 #include "qemu/module.h"
26 
27 #define TEST_FIFO "test-io-channel-command.fifo"
28 
29 #define SOCAT_SRC "PIPE:" TEST_FIFO ",wronly"
30 #define SOCAT_DST "PIPE:" TEST_FIFO ",rdonly"
31 
32 static char *socat = NULL;
33 
34 static void test_io_channel_command_fifo(bool async)
35 {
36     QIOChannel *src, *dst;
37     QIOChannelTest *test;
38     const char *srcargv[] = {
39         socat, "-", SOCAT_SRC, NULL,
40     };
41     const char *dstargv[] = {
42         socat, SOCAT_DST, "-", NULL,
43     };
44 
45     if (!socat) {
46         g_test_skip("socat is not found in PATH");
47         return;
48     }
49 
50     unlink(TEST_FIFO);
51     src = QIO_CHANNEL(qio_channel_command_new_spawn(srcargv,
52                                                     O_WRONLY,
53                                                     &error_abort));
54     dst = QIO_CHANNEL(qio_channel_command_new_spawn(dstargv,
55                                                     O_RDONLY,
56                                                     &error_abort));
57 
58     test = qio_channel_test_new();
59     qio_channel_test_run_threads(test, async, src, dst);
60     qio_channel_test_validate(test);
61 
62     object_unref(OBJECT(src));
63     object_unref(OBJECT(dst));
64 
65     unlink(TEST_FIFO);
66 }
67 
68 
69 static void test_io_channel_command_fifo_async(void)
70 {
71     test_io_channel_command_fifo(true);
72 }
73 
74 static void test_io_channel_command_fifo_sync(void)
75 {
76     test_io_channel_command_fifo(false);
77 }
78 
79 
80 static void test_io_channel_command_echo(bool async)
81 {
82     QIOChannel *ioc;
83     QIOChannelTest *test;
84     const char *socatargv[] = {
85         socat, "-", "-", NULL,
86     };
87 
88     if (!socat) {
89         g_test_skip("socat is not found in PATH");
90         return;
91     }
92 
93     ioc = QIO_CHANNEL(qio_channel_command_new_spawn(socatargv,
94                                                     O_RDWR,
95                                                     &error_abort));
96     test = qio_channel_test_new();
97     qio_channel_test_run_threads(test, async, ioc, ioc);
98     qio_channel_test_validate(test);
99 
100     object_unref(OBJECT(ioc));
101 }
102 
103 
104 static void test_io_channel_command_echo_async(void)
105 {
106     test_io_channel_command_echo(true);
107 }
108 
109 static void test_io_channel_command_echo_sync(void)
110 {
111     test_io_channel_command_echo(false);
112 }
113 
114 int main(int argc, char **argv)
115 {
116     module_call_init(MODULE_INIT_QOM);
117 
118     g_test_init(&argc, &argv, NULL);
119 
120     socat = g_find_program_in_path("socat");
121 
122     g_test_add_func("/io/channel/command/fifo/sync",
123                     test_io_channel_command_fifo_sync);
124     g_test_add_func("/io/channel/command/fifo/async",
125                     test_io_channel_command_fifo_async);
126     g_test_add_func("/io/channel/command/echo/sync",
127                     test_io_channel_command_echo_sync);
128     g_test_add_func("/io/channel/command/echo/async",
129                     test_io_channel_command_echo_async);
130 
131     return g_test_run();
132 }
133