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> 23*c906e6fbSAlex Bennée #include <sys/types.h> 24*c906e6fbSAlex 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 34*c906e6fbSAlex Bennée #ifndef _WIN32 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); 3868406d10SAlex Bennée g_autofree gchar *fifo = g_strdup_printf("%s/%s", tmpdir, TEST_FIFO); 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; 45da668aa1SThomas Huth 46*c906e6fbSAlex Bennée if (mkfifo(fifo, 0600)) { 47*c906e6fbSAlex Bennée g_error("mkfifo: %s", strerror(errno)); 48*c906e6fbSAlex Bennée } 49*c906e6fbSAlex Bennée 5068406d10SAlex Bennée src = QIO_CHANNEL(qio_channel_command_new_spawn((const char **) srcargv, 51da668aa1SThomas Huth O_WRONLY, 52da668aa1SThomas Huth &error_abort)); 5368406d10SAlex Bennée dst = QIO_CHANNEL(qio_channel_command_new_spawn((const char **) dstargv, 54da668aa1SThomas Huth O_RDONLY, 55da668aa1SThomas Huth &error_abort)); 56da668aa1SThomas Huth 57da668aa1SThomas Huth test = qio_channel_test_new(); 58da668aa1SThomas Huth qio_channel_test_run_threads(test, async, src, dst); 59da668aa1SThomas Huth qio_channel_test_validate(test); 60da668aa1SThomas Huth 61da668aa1SThomas Huth object_unref(OBJECT(src)); 62da668aa1SThomas Huth object_unref(OBJECT(dst)); 63da668aa1SThomas Huth 6468406d10SAlex Bennée g_rmdir(tmpdir); 65da668aa1SThomas Huth } 66da668aa1SThomas Huth 67da668aa1SThomas Huth static void test_io_channel_command_fifo_async(void) 68da668aa1SThomas Huth { 6968406d10SAlex Bennée if (!socat) { 7068406d10SAlex Bennée g_test_skip("socat is not found in PATH"); 7168406d10SAlex Bennée return; 7268406d10SAlex Bennée } 7368406d10SAlex Bennée 74da668aa1SThomas Huth test_io_channel_command_fifo(true); 75da668aa1SThomas Huth } 76da668aa1SThomas Huth 77da668aa1SThomas Huth static void test_io_channel_command_fifo_sync(void) 78da668aa1SThomas Huth { 7968406d10SAlex Bennée if (!socat) { 8068406d10SAlex Bennée g_test_skip("socat is not found in PATH"); 8168406d10SAlex Bennée return; 8268406d10SAlex Bennée } 8368406d10SAlex Bennée 84da668aa1SThomas Huth test_io_channel_command_fifo(false); 85da668aa1SThomas Huth } 86*c906e6fbSAlex Bennée #endif 87da668aa1SThomas Huth 88da668aa1SThomas Huth 89da668aa1SThomas Huth static void test_io_channel_command_echo(bool async) 90da668aa1SThomas Huth { 91da668aa1SThomas Huth QIOChannel *ioc; 92da668aa1SThomas Huth QIOChannelTest *test; 93da668aa1SThomas Huth const char *socatargv[] = { 9476f5148cSMarc-André Lureau socat, "-", "-", NULL, 95da668aa1SThomas Huth }; 96da668aa1SThomas Huth 9776f5148cSMarc-André Lureau if (!socat) { 9876f5148cSMarc-André Lureau g_test_skip("socat is not found in PATH"); 9976f5148cSMarc-André Lureau return; 100da668aa1SThomas Huth } 101da668aa1SThomas Huth 102da668aa1SThomas Huth ioc = QIO_CHANNEL(qio_channel_command_new_spawn(socatargv, 103da668aa1SThomas Huth O_RDWR, 104da668aa1SThomas Huth &error_abort)); 105da668aa1SThomas Huth test = qio_channel_test_new(); 106da668aa1SThomas Huth qio_channel_test_run_threads(test, async, ioc, ioc); 107da668aa1SThomas Huth qio_channel_test_validate(test); 108da668aa1SThomas Huth 109da668aa1SThomas Huth object_unref(OBJECT(ioc)); 110da668aa1SThomas Huth } 111da668aa1SThomas Huth 112da668aa1SThomas Huth 113da668aa1SThomas Huth static void test_io_channel_command_echo_async(void) 114da668aa1SThomas Huth { 115da668aa1SThomas Huth test_io_channel_command_echo(true); 116da668aa1SThomas Huth } 117da668aa1SThomas Huth 118da668aa1SThomas Huth static void test_io_channel_command_echo_sync(void) 119da668aa1SThomas Huth { 120da668aa1SThomas Huth test_io_channel_command_echo(false); 121da668aa1SThomas Huth } 122da668aa1SThomas Huth 123da668aa1SThomas Huth int main(int argc, char **argv) 124da668aa1SThomas Huth { 125da668aa1SThomas Huth module_call_init(MODULE_INIT_QOM); 126da668aa1SThomas Huth 127da668aa1SThomas Huth g_test_init(&argc, &argv, NULL); 128da668aa1SThomas Huth 12976f5148cSMarc-André Lureau socat = g_find_program_in_path("socat"); 13076f5148cSMarc-André Lureau 131*c906e6fbSAlex Bennée #ifndef _WIN32 132da668aa1SThomas Huth g_test_add_func("/io/channel/command/fifo/sync", 133da668aa1SThomas Huth test_io_channel_command_fifo_sync); 134da668aa1SThomas Huth g_test_add_func("/io/channel/command/fifo/async", 135da668aa1SThomas Huth test_io_channel_command_fifo_async); 136*c906e6fbSAlex Bennée #endif 137da668aa1SThomas Huth g_test_add_func("/io/channel/command/echo/sync", 138da668aa1SThomas Huth test_io_channel_command_echo_sync); 139da668aa1SThomas Huth g_test_add_func("/io/channel/command/echo/async", 140da668aa1SThomas Huth test_io_channel_command_echo_async); 141da668aa1SThomas Huth 142da668aa1SThomas Huth return g_test_run(); 143da668aa1SThomas Huth } 144