1da668aa1SThomas Huth /*
2da668aa1SThomas Huth * QEMU I/O channel command test
3da668aa1SThomas Huth *
4da668aa1SThomas Huth * Copyright (c) 2015 Red Hat, Inc.
5da668aa1SThomas Huth *
6da668aa1SThomas Huth * This library is free software; you can redistribute it and/or
7da668aa1SThomas Huth * modify it under the terms of the GNU Lesser General Public
8da668aa1SThomas Huth * License as published by the Free Software Foundation; either
9da668aa1SThomas Huth * version 2.1 of the License, or (at your option) any later version.
10da668aa1SThomas Huth *
11da668aa1SThomas Huth * This library is distributed in the hope that it will be useful,
12da668aa1SThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of
13da668aa1SThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14da668aa1SThomas Huth * Lesser General Public License for more details.
15da668aa1SThomas Huth *
16da668aa1SThomas Huth * You should have received a copy of the GNU Lesser General Public
17da668aa1SThomas Huth * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18da668aa1SThomas Huth *
19da668aa1SThomas Huth */
20da668aa1SThomas Huth
21da668aa1SThomas Huth #include "qemu/osdep.h"
2268406d10SAlex Bennée #include <glib/gstdio.h>
23c906e6fbSAlex Bennée #include <sys/types.h>
24c906e6fbSAlex Bennée #include <sys/stat.h>
25da668aa1SThomas Huth #include "io/channel-command.h"
26da668aa1SThomas Huth #include "io-channel-helpers.h"
27da668aa1SThomas Huth #include "qapi/error.h"
28da668aa1SThomas Huth #include "qemu/module.h"
29da668aa1SThomas Huth
3076f5148cSMarc-André Lureau #define TEST_FIFO "test-io-channel-command.fifo"
3176f5148cSMarc-André Lureau
3276f5148cSMarc-André Lureau static char *socat = NULL;
3376f5148cSMarc-André Lureau
34a6d71511SAlex Bennée #if !defined(_WIN32) && !defined(CONFIG_DARWIN)
test_io_channel_command_fifo(bool async)35da668aa1SThomas Huth static void test_io_channel_command_fifo(bool async)
36da668aa1SThomas Huth {
3768406d10SAlex Bennée g_autofree gchar *tmpdir = g_dir_make_tmp("qemu-test-io-channel.XXXXXX", NULL);
38*6d3b418aSMarc-André Lureau g_autofree gchar *fifo = g_build_filename(tmpdir, TEST_FIFO, NULL);
395a820d5dSAlex Bennée g_autofree gchar *srcargs = g_strdup_printf("%s - PIPE:%s,wronly", socat, fifo);
405a820d5dSAlex Bennée g_autofree gchar *dstargs = g_strdup_printf("%s PIPE:%s,rdonly -", socat, fifo);
415a820d5dSAlex Bennée g_auto(GStrv) srcargv = g_strsplit(srcargs, " ", -1);
425a820d5dSAlex Bennée g_auto(GStrv) dstargv = g_strsplit(dstargs, " ", -1);
43da668aa1SThomas Huth QIOChannel *src, *dst;
44da668aa1SThomas Huth QIOChannelTest *test;
45c9970680SAlex Bennée int err;
46da668aa1SThomas Huth
47c906e6fbSAlex Bennée if (mkfifo(fifo, 0600)) {
48c906e6fbSAlex Bennée g_error("mkfifo: %s", strerror(errno));
49c906e6fbSAlex Bennée }
50c906e6fbSAlex Bennée
5168406d10SAlex Bennée src = QIO_CHANNEL(qio_channel_command_new_spawn((const char **) srcargv,
52da668aa1SThomas Huth O_WRONLY,
53da668aa1SThomas Huth &error_abort));
5468406d10SAlex Bennée dst = QIO_CHANNEL(qio_channel_command_new_spawn((const char **) dstargv,
55da668aa1SThomas Huth O_RDONLY,
56da668aa1SThomas Huth &error_abort));
57da668aa1SThomas Huth
58da668aa1SThomas Huth test = qio_channel_test_new();
59da668aa1SThomas Huth qio_channel_test_run_threads(test, async, src, dst);
60da668aa1SThomas Huth qio_channel_test_validate(test);
61da668aa1SThomas Huth
62da668aa1SThomas Huth object_unref(OBJECT(src));
63da668aa1SThomas Huth object_unref(OBJECT(dst));
64da668aa1SThomas Huth
65c9970680SAlex Bennée err = g_unlink(fifo);
66c9970680SAlex Bennée g_assert(err == 0);
67c9970680SAlex Bennée err = g_rmdir(tmpdir);
68c9970680SAlex Bennée g_assert(err == 0);
69da668aa1SThomas Huth }
70da668aa1SThomas Huth
test_io_channel_command_fifo_async(void)71da668aa1SThomas Huth static void test_io_channel_command_fifo_async(void)
72da668aa1SThomas Huth {
7368406d10SAlex Bennée if (!socat) {
7468406d10SAlex Bennée g_test_skip("socat is not found in PATH");
7568406d10SAlex Bennée return;
7668406d10SAlex Bennée }
7768406d10SAlex Bennée
78da668aa1SThomas Huth test_io_channel_command_fifo(true);
79da668aa1SThomas Huth }
80da668aa1SThomas Huth
test_io_channel_command_fifo_sync(void)81da668aa1SThomas Huth static void test_io_channel_command_fifo_sync(void)
82da668aa1SThomas Huth {
8368406d10SAlex Bennée if (!socat) {
8468406d10SAlex Bennée g_test_skip("socat is not found in PATH");
8568406d10SAlex Bennée return;
8668406d10SAlex Bennée }
8768406d10SAlex Bennée
88da668aa1SThomas Huth test_io_channel_command_fifo(false);
89da668aa1SThomas Huth }
90c906e6fbSAlex Bennée #endif
91da668aa1SThomas Huth
92da668aa1SThomas Huth
test_io_channel_command_echo(bool async)93da668aa1SThomas Huth static void test_io_channel_command_echo(bool async)
94da668aa1SThomas Huth {
95da668aa1SThomas Huth QIOChannel *ioc;
96da668aa1SThomas Huth QIOChannelTest *test;
97da668aa1SThomas Huth const char *socatargv[] = {
9876f5148cSMarc-André Lureau socat, "-", "-", NULL,
99da668aa1SThomas Huth };
100da668aa1SThomas Huth
10176f5148cSMarc-André Lureau if (!socat) {
10276f5148cSMarc-André Lureau g_test_skip("socat is not found in PATH");
10376f5148cSMarc-André Lureau return;
104da668aa1SThomas Huth }
105da668aa1SThomas Huth
106da668aa1SThomas Huth ioc = QIO_CHANNEL(qio_channel_command_new_spawn(socatargv,
107da668aa1SThomas Huth O_RDWR,
108da668aa1SThomas Huth &error_abort));
109da668aa1SThomas Huth test = qio_channel_test_new();
110da668aa1SThomas Huth qio_channel_test_run_threads(test, async, ioc, ioc);
111da668aa1SThomas Huth qio_channel_test_validate(test);
112da668aa1SThomas Huth
113da668aa1SThomas Huth object_unref(OBJECT(ioc));
114da668aa1SThomas Huth }
115da668aa1SThomas Huth
116da668aa1SThomas Huth
test_io_channel_command_echo_async(void)117da668aa1SThomas Huth static void test_io_channel_command_echo_async(void)
118da668aa1SThomas Huth {
119da668aa1SThomas Huth test_io_channel_command_echo(true);
120da668aa1SThomas Huth }
121da668aa1SThomas Huth
test_io_channel_command_echo_sync(void)122da668aa1SThomas Huth static void test_io_channel_command_echo_sync(void)
123da668aa1SThomas Huth {
124da668aa1SThomas Huth test_io_channel_command_echo(false);
125da668aa1SThomas Huth }
126da668aa1SThomas Huth
main(int argc,char ** argv)127da668aa1SThomas Huth int main(int argc, char **argv)
128da668aa1SThomas Huth {
129da668aa1SThomas Huth module_call_init(MODULE_INIT_QOM);
130da668aa1SThomas Huth
131da668aa1SThomas Huth g_test_init(&argc, &argv, NULL);
132da668aa1SThomas Huth
13376f5148cSMarc-André Lureau socat = g_find_program_in_path("socat");
13476f5148cSMarc-André Lureau
135a6d71511SAlex Bennée #if !defined(_WIN32) && !defined(CONFIG_DARWIN)
136da668aa1SThomas Huth g_test_add_func("/io/channel/command/fifo/sync",
137da668aa1SThomas Huth test_io_channel_command_fifo_sync);
138da668aa1SThomas Huth g_test_add_func("/io/channel/command/fifo/async",
139da668aa1SThomas Huth test_io_channel_command_fifo_async);
140c906e6fbSAlex Bennée #endif
141da668aa1SThomas Huth g_test_add_func("/io/channel/command/echo/sync",
142da668aa1SThomas Huth test_io_channel_command_echo_sync);
143da668aa1SThomas Huth g_test_add_func("/io/channel/command/echo/async",
144da668aa1SThomas Huth test_io_channel_command_echo_async);
145da668aa1SThomas Huth
146da668aa1SThomas Huth return g_test_run();
147da668aa1SThomas Huth }
148