2
0

test-io-channel-file.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * QEMU I/O channel file 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-file.h"
  22. #include "io/channel-util.h"
  23. #include "io-channel-helpers.h"
  24. #include "qapi/error.h"
  25. #include "qemu/module.h"
  26. #define TEST_FILE "tests/test-io-channel-file.txt"
  27. #define TEST_MASK 0600
  28. static void test_io_channel_file_helper(int flags)
  29. {
  30. QIOChannel *src, *dst;
  31. QIOChannelTest *test;
  32. struct stat st;
  33. mode_t mask;
  34. int ret;
  35. unlink(TEST_FILE);
  36. src = QIO_CHANNEL(qio_channel_file_new_path(
  37. TEST_FILE,
  38. flags, TEST_MASK,
  39. &error_abort));
  40. dst = QIO_CHANNEL(qio_channel_file_new_path(
  41. TEST_FILE,
  42. O_RDONLY | O_BINARY, 0,
  43. &error_abort));
  44. test = qio_channel_test_new();
  45. qio_channel_test_run_writer(test, src);
  46. qio_channel_test_run_reader(test, dst);
  47. qio_channel_test_validate(test);
  48. /* Check that the requested mode took effect. */
  49. mask = umask(0);
  50. umask(mask);
  51. ret = stat(TEST_FILE, &st);
  52. g_assert_cmpint(ret, >, -1);
  53. g_assert_cmpuint(TEST_MASK & ~mask, ==, st.st_mode & 0777);
  54. unlink(TEST_FILE);
  55. object_unref(OBJECT(src));
  56. object_unref(OBJECT(dst));
  57. }
  58. static void test_io_channel_file(void)
  59. {
  60. test_io_channel_file_helper(O_WRONLY | O_CREAT | O_TRUNC | O_BINARY);
  61. }
  62. static void test_io_channel_file_rdwr(void)
  63. {
  64. test_io_channel_file_helper(O_RDWR | O_CREAT | O_TRUNC | O_BINARY);
  65. }
  66. static void test_io_channel_fd(void)
  67. {
  68. QIOChannel *ioc;
  69. int fd = -1;
  70. fd = open(TEST_FILE, O_CREAT | O_TRUNC | O_WRONLY, 0600);
  71. g_assert_cmpint(fd, >, -1);
  72. ioc = qio_channel_new_fd(fd, &error_abort);
  73. g_assert_cmpstr(object_get_typename(OBJECT(ioc)),
  74. ==,
  75. TYPE_QIO_CHANNEL_FILE);
  76. unlink(TEST_FILE);
  77. object_unref(OBJECT(ioc));
  78. }
  79. #ifndef _WIN32
  80. static void test_io_channel_pipe(bool async)
  81. {
  82. QIOChannel *src, *dst;
  83. QIOChannelTest *test;
  84. int fd[2];
  85. if (pipe(fd) < 0) {
  86. perror("pipe");
  87. abort();
  88. }
  89. src = QIO_CHANNEL(qio_channel_file_new_fd(fd[1]));
  90. dst = QIO_CHANNEL(qio_channel_file_new_fd(fd[0]));
  91. test = qio_channel_test_new();
  92. qio_channel_test_run_threads(test, async, src, dst);
  93. qio_channel_test_validate(test);
  94. object_unref(OBJECT(src));
  95. object_unref(OBJECT(dst));
  96. }
  97. static void test_io_channel_pipe_async(void)
  98. {
  99. test_io_channel_pipe(true);
  100. }
  101. static void test_io_channel_pipe_sync(void)
  102. {
  103. test_io_channel_pipe(false);
  104. }
  105. #endif /* ! _WIN32 */
  106. int main(int argc, char **argv)
  107. {
  108. module_call_init(MODULE_INIT_QOM);
  109. g_test_init(&argc, &argv, NULL);
  110. g_test_add_func("/io/channel/file", test_io_channel_file);
  111. g_test_add_func("/io/channel/file/rdwr", test_io_channel_file_rdwr);
  112. g_test_add_func("/io/channel/file/fd", test_io_channel_fd);
  113. #ifndef _WIN32
  114. g_test_add_func("/io/channel/pipe/sync", test_io_channel_pipe_sync);
  115. g_test_add_func("/io/channel/pipe/async", test_io_channel_pipe_async);
  116. #endif
  117. return g_test_run();
  118. }