]> git.proxmox.com Git - mirror_qemu.git/blame - tests/unit/test-io-channel-command.c
Merge tag 'edk2-stable202302-20230320-pull-request' of https://gitlab.com/kraxel...
[mirror_qemu.git] / tests / unit / test-io-channel-command.c
CommitLineData
195e14d0
DB
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
c8198bd5 9 * version 2.1 of the License, or (at your option) any later version.
195e14d0
DB
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
681c28a3 21#include "qemu/osdep.h"
68406d10 22#include <glib/gstdio.h>
c906e6fb
AB
23#include <sys/types.h>
24#include <sys/stat.h>
195e14d0
DB
25#include "io/channel-command.h"
26#include "io-channel-helpers.h"
da34e65c 27#include "qapi/error.h"
0b8fa32f 28#include "qemu/module.h"
195e14d0 29
76f5148c
MAL
30#define TEST_FIFO "test-io-channel-command.fifo"
31
76f5148c
MAL
32static char *socat = NULL;
33
a6d71511 34#if !defined(_WIN32) && !defined(CONFIG_DARWIN)
195e14d0
DB
35static void test_io_channel_command_fifo(bool async)
36{
68406d10 37 g_autofree gchar *tmpdir = g_dir_make_tmp("qemu-test-io-channel.XXXXXX", NULL);
6d3b418a 38 g_autofree gchar *fifo = g_build_filename(tmpdir, TEST_FIFO, NULL);
5a820d5d
AB
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);
195e14d0
DB
43 QIOChannel *src, *dst;
44 QIOChannelTest *test;
c9970680 45 int err;
195e14d0 46
c906e6fb
AB
47 if (mkfifo(fifo, 0600)) {
48 g_error("mkfifo: %s", strerror(errno));
49 }
50
68406d10 51 src = QIO_CHANNEL(qio_channel_command_new_spawn((const char **) srcargv,
195e14d0
DB
52 O_WRONLY,
53 &error_abort));
68406d10 54 dst = QIO_CHANNEL(qio_channel_command_new_spawn((const char **) dstargv,
195e14d0
DB
55 O_RDONLY,
56 &error_abort));
57
58 test = qio_channel_test_new();
59 qio_channel_test_run_threads(test, async, src, dst);
60 qio_channel_test_validate(test);
61
62 object_unref(OBJECT(src));
63 object_unref(OBJECT(dst));
64
c9970680
AB
65 err = g_unlink(fifo);
66 g_assert(err == 0);
67 err = g_rmdir(tmpdir);
68 g_assert(err == 0);
195e14d0
DB
69}
70
195e14d0
DB
71static void test_io_channel_command_fifo_async(void)
72{
68406d10
AB
73 if (!socat) {
74 g_test_skip("socat is not found in PATH");
75 return;
76 }
77
195e14d0
DB
78 test_io_channel_command_fifo(true);
79}
80
81static void test_io_channel_command_fifo_sync(void)
82{
68406d10
AB
83 if (!socat) {
84 g_test_skip("socat is not found in PATH");
85 return;
86 }
87
195e14d0
DB
88 test_io_channel_command_fifo(false);
89}
c906e6fb 90#endif
195e14d0
DB
91
92
93static void test_io_channel_command_echo(bool async)
94{
95 QIOChannel *ioc;
96 QIOChannelTest *test;
97 const char *socatargv[] = {
76f5148c 98 socat, "-", "-", NULL,
195e14d0
DB
99 };
100
76f5148c
MAL
101 if (!socat) {
102 g_test_skip("socat is not found in PATH");
103 return;
195e14d0
DB
104 }
105
106 ioc = QIO_CHANNEL(qio_channel_command_new_spawn(socatargv,
107 O_RDWR,
108 &error_abort));
109 test = qio_channel_test_new();
110 qio_channel_test_run_threads(test, async, ioc, ioc);
111 qio_channel_test_validate(test);
112
113 object_unref(OBJECT(ioc));
114}
115
116
117static void test_io_channel_command_echo_async(void)
118{
119 test_io_channel_command_echo(true);
120}
121
122static void test_io_channel_command_echo_sync(void)
123{
124 test_io_channel_command_echo(false);
125}
195e14d0
DB
126
127int main(int argc, char **argv)
128{
129 module_call_init(MODULE_INIT_QOM);
130
131 g_test_init(&argc, &argv, NULL);
132
76f5148c
MAL
133 socat = g_find_program_in_path("socat");
134
a6d71511 135#if !defined(_WIN32) && !defined(CONFIG_DARWIN)
195e14d0
DB
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);
c906e6fb 140#endif
195e14d0
DB
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);
195e14d0
DB
145
146 return g_test_run();
147}