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