qemu/tests/unit/test-io-channel-command.c
<<
>>
Prefs
   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 "io/channel-command.h"
  23#include "io-channel-helpers.h"
  24#include "qapi/error.h"
  25#include "qemu/module.h"
  26
  27#ifndef WIN32
  28static void test_io_channel_command_fifo(bool async)
  29{
  30#define TEST_FIFO "tests/test-io-channel-command.fifo"
  31    QIOChannel *src, *dst;
  32    QIOChannelTest *test;
  33    const char *srcfifo = "PIPE:" TEST_FIFO ",wronly";
  34    const char *dstfifo = "PIPE:" TEST_FIFO ",rdonly";
  35    const char *srcargv[] = {
  36        "/bin/socat", "-", srcfifo, NULL,
  37    };
  38    const char *dstargv[] = {
  39        "/bin/socat", dstfifo, "-", NULL,
  40    };
  41
  42    unlink(TEST_FIFO);
  43    if (access("/bin/socat", X_OK) < 0) {
  44        return; /* Pretend success if socat is not present */
  45    }
  46    if (mkfifo(TEST_FIFO, 0600) < 0) {
  47        abort();
  48    }
  49    src = QIO_CHANNEL(qio_channel_command_new_spawn(srcargv,
  50                                                    O_WRONLY,
  51                                                    &error_abort));
  52    dst = QIO_CHANNEL(qio_channel_command_new_spawn(dstargv,
  53                                                    O_RDONLY,
  54                                                    &error_abort));
  55
  56    test = qio_channel_test_new();
  57    qio_channel_test_run_threads(test, async, src, dst);
  58    qio_channel_test_validate(test);
  59
  60    object_unref(OBJECT(src));
  61    object_unref(OBJECT(dst));
  62
  63    unlink(TEST_FIFO);
  64}
  65
  66
  67static void test_io_channel_command_fifo_async(void)
  68{
  69    test_io_channel_command_fifo(true);
  70}
  71
  72static void test_io_channel_command_fifo_sync(void)
  73{
  74    test_io_channel_command_fifo(false);
  75}
  76
  77
  78static void test_io_channel_command_echo(bool async)
  79{
  80    QIOChannel *ioc;
  81    QIOChannelTest *test;
  82    const char *socatargv[] = {
  83        "/bin/socat", "-", "-", NULL,
  84    };
  85
  86    if (access("/bin/socat", X_OK) < 0) {
  87        return; /* Pretend success if socat is not present */
  88    }
  89
  90    ioc = QIO_CHANNEL(qio_channel_command_new_spawn(socatargv,
  91                                                    O_RDWR,
  92                                                    &error_abort));
  93    test = qio_channel_test_new();
  94    qio_channel_test_run_threads(test, async, ioc, ioc);
  95    qio_channel_test_validate(test);
  96
  97    object_unref(OBJECT(ioc));
  98}
  99
 100
 101static void test_io_channel_command_echo_async(void)
 102{
 103    test_io_channel_command_echo(true);
 104}
 105
 106static void test_io_channel_command_echo_sync(void)
 107{
 108    test_io_channel_command_echo(false);
 109}
 110#endif
 111
 112int main(int argc, char **argv)
 113{
 114    module_call_init(MODULE_INIT_QOM);
 115
 116    g_test_init(&argc, &argv, NULL);
 117
 118#ifndef WIN32
 119    g_test_add_func("/io/channel/command/fifo/sync",
 120                    test_io_channel_command_fifo_sync);
 121    g_test_add_func("/io/channel/command/fifo/async",
 122                    test_io_channel_command_fifo_async);
 123    g_test_add_func("/io/channel/command/echo/sync",
 124                    test_io_channel_command_echo_sync);
 125    g_test_add_func("/io/channel/command/echo/async",
 126                    test_io_channel_command_echo_async);
 127#endif
 128
 129    return g_test_run();
 130}
 131