test-tcp-oob.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Copyright Fedor Indutny. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #if !defined(_WIN32)
  22. #include "uv.h"
  23. #include "task.h"
  24. #include <errno.h>
  25. #include <sys/socket.h>
  26. #include <unistd.h>
  27. static uv_tcp_t server_handle;
  28. static uv_tcp_t client_handle;
  29. static uv_tcp_t peer_handle;
  30. static uv_idle_t idle;
  31. static uv_connect_t connect_req;
  32. static int ticks;
  33. static const int kMaxTicks = 10;
  34. static void alloc_cb(uv_handle_t* handle,
  35. size_t suggested_size,
  36. uv_buf_t* buf) {
  37. static char storage[1024];
  38. *buf = uv_buf_init(storage, sizeof(storage));
  39. }
  40. static void idle_cb(uv_idle_t* idle) {
  41. if (++ticks < kMaxTicks)
  42. return;
  43. uv_close((uv_handle_t*) &server_handle, NULL);
  44. uv_close((uv_handle_t*) &client_handle, NULL);
  45. uv_close((uv_handle_t*) &peer_handle, NULL);
  46. uv_close((uv_handle_t*) idle, NULL);
  47. }
  48. static void read_cb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
  49. #ifdef __MVS__
  50. char lbuf[12];
  51. #endif
  52. uv_os_fd_t fd;
  53. ASSERT(nread >= 0);
  54. ASSERT(0 == uv_fileno((uv_handle_t*)handle, &fd));
  55. ASSERT(0 == uv_idle_start(&idle, idle_cb));
  56. #ifdef __MVS__
  57. /* Need to flush out the OOB data. Otherwise, this callback will get
  58. * triggered on every poll with nread = 0.
  59. */
  60. ASSERT(-1 != recv(fd, lbuf, sizeof(lbuf), MSG_OOB));
  61. #endif
  62. }
  63. static void connect_cb(uv_connect_t* req, int status) {
  64. ASSERT(req->handle == (uv_stream_t*) &client_handle);
  65. ASSERT(0 == status);
  66. }
  67. static void connection_cb(uv_stream_t* handle, int status) {
  68. int r;
  69. uv_os_fd_t fd;
  70. ASSERT(0 == status);
  71. ASSERT(0 == uv_accept(handle, (uv_stream_t*) &peer_handle));
  72. ASSERT(0 == uv_read_start((uv_stream_t*) &peer_handle, alloc_cb, read_cb));
  73. /* Send some OOB data */
  74. ASSERT(0 == uv_fileno((uv_handle_t*) &client_handle, &fd));
  75. ASSERT(0 == uv_stream_set_blocking((uv_stream_t*) &client_handle, 1));
  76. /* The problem triggers only on a second message, it seem that xnu is not
  77. * triggering `kevent()` for the first one
  78. */
  79. do {
  80. r = send(fd, "hello", 5, MSG_OOB);
  81. } while (r < 0 && errno == EINTR);
  82. ASSERT(5 == r);
  83. do {
  84. r = send(fd, "hello", 5, MSG_OOB);
  85. } while (r < 0 && errno == EINTR);
  86. ASSERT(5 == r);
  87. ASSERT(0 == uv_stream_set_blocking((uv_stream_t*) &client_handle, 0));
  88. }
  89. TEST_IMPL(tcp_oob) {
  90. struct sockaddr_in addr;
  91. uv_loop_t* loop;
  92. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  93. loop = uv_default_loop();
  94. ASSERT(0 == uv_tcp_init(loop, &server_handle));
  95. ASSERT(0 == uv_tcp_init(loop, &client_handle));
  96. ASSERT(0 == uv_tcp_init(loop, &peer_handle));
  97. ASSERT(0 == uv_idle_init(loop, &idle));
  98. ASSERT(0 == uv_tcp_bind(&server_handle, (const struct sockaddr*) &addr, 0));
  99. ASSERT(0 == uv_listen((uv_stream_t*) &server_handle, 1, connection_cb));
  100. /* Ensure two separate packets */
  101. ASSERT(0 == uv_tcp_nodelay(&client_handle, 1));
  102. ASSERT(0 == uv_tcp_connect(&connect_req,
  103. &client_handle,
  104. (const struct sockaddr*) &addr,
  105. connect_cb));
  106. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  107. ASSERT(ticks == kMaxTicks);
  108. MAKE_VALGRIND_HAPPY();
  109. return 0;
  110. }
  111. #else
  112. typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
  113. #endif /* !_WIN32 */