2 * QEMU I/O channel command test
4 * Copyright (c) 2015 Red Hat, Inc.
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.
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.
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/>.
21 #include "qemu/osdep.h"
22 #include <glib/gstdio.h>
23 #include <sys/types.h>
25 #include "io/channel-command.h"
26 #include "io-channel-helpers.h"
27 #include "qapi/error.h"
28 #include "qemu/module.h"
30 #define TEST_FIFO "test-io-channel-command.fifo"
32 static char *socat = NULL;
34 #if !defined(_WIN32) && !defined(CONFIG_DARWIN)
35 static void test_io_channel_command_fifo(bool async)
37 g_autofree gchar *tmpdir = g_dir_make_tmp("qemu-test-io-channel.XXXXXX", NULL);
38 g_autofree gchar *fifo = g_build_filename(tmpdir, TEST_FIFO, NULL);
39 g_autofree gchar *srcargs = g_strdup_printf("%s - PIPE:%s,wronly", socat, fifo);
40 g_autofree gchar *dstargs = g_strdup_printf("%s PIPE:%s,rdonly -", socat, fifo);
41 g_auto(GStrv) srcargv = g_strsplit(srcargs, " ", -1);
42 g_auto(GStrv) dstargv = g_strsplit(dstargs, " ", -1);
43 QIOChannel *src, *dst;
47 if (mkfifo(fifo, 0600)) {
48 g_error("mkfifo: %s", strerror(errno));
51 src = QIO_CHANNEL(qio_channel_command_new_spawn((const char **) srcargv,
54 dst = QIO_CHANNEL(qio_channel_command_new_spawn((const char **) dstargv,
58 test = qio_channel_test_new();
59 qio_channel_test_run_threads(test, async, src, dst);
60 qio_channel_test_validate(test);
62 object_unref(OBJECT(src));
63 object_unref(OBJECT(dst));
67 err = g_rmdir(tmpdir);
71 static void test_io_channel_command_fifo_async(void)
74 g_test_skip("socat is not found in PATH");
78 test_io_channel_command_fifo(true);
81 static void test_io_channel_command_fifo_sync(void)
84 g_test_skip("socat is not found in PATH");
88 test_io_channel_command_fifo(false);
93 static void test_io_channel_command_echo(bool async)
97 const char *socatargv[] = {
98 socat, "-", "-", NULL,
102 g_test_skip("socat is not found in PATH");
106 ioc = QIO_CHANNEL(qio_channel_command_new_spawn(socatargv,
109 test = qio_channel_test_new();
110 qio_channel_test_run_threads(test, async, ioc, ioc);
111 qio_channel_test_validate(test);
113 object_unref(OBJECT(ioc));
117 static void test_io_channel_command_echo_async(void)
119 test_io_channel_command_echo(true);
122 static void test_io_channel_command_echo_sync(void)
124 test_io_channel_command_echo(false);
127 int main(int argc, char **argv)
129 module_call_init(MODULE_INIT_QOM);
131 g_test_init(&argc, &argv, NULL);
133 socat = g_find_program_in_path("socat");
135 #if !defined(_WIN32) && !defined(CONFIG_DARWIN)
136 g_test_add_func("/io/channel/command/fifo/sync",
137 test_io_channel_command_fifo_sync);
138 g_test_add_func("/io/channel/command/fifo/async",
139 test_io_channel_command_fifo_async);
141 g_test_add_func("/io/channel/command/echo/sync",
142 test_io_channel_command_echo_sync);
143 g_test_add_func("/io/channel/command/echo/async",
144 test_io_channel_command_echo_async);