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