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