test-io-channel-command.c 3.7 KB

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