test-poll-oob.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Copyright libuv project contributors. 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 <sys/ioctl.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. static uv_tcp_t server_handle;
  30. static uv_tcp_t client_handle;
  31. static uv_tcp_t peer_handle;
  32. static uv_poll_t poll_req[2];
  33. static uv_idle_t idle;
  34. static uv_os_fd_t client_fd;
  35. static uv_os_fd_t server_fd;
  36. static int ticks;
  37. static const int kMaxTicks = 10;
  38. static int cli_pr_check = 0;
  39. static int cli_rd_check = 0;
  40. static int srv_rd_check = 0;
  41. static int got_eagain(void) {
  42. return errno == EAGAIN
  43. || errno == EINPROGRESS
  44. #ifdef EWOULDBLOCK
  45. || errno == EWOULDBLOCK
  46. #endif
  47. ;
  48. }
  49. static void idle_cb(uv_idle_t* idle) {
  50. uv_sleep(100);
  51. if (++ticks < kMaxTicks)
  52. return;
  53. uv_poll_stop(&poll_req[0]);
  54. uv_poll_stop(&poll_req[1]);
  55. uv_close((uv_handle_t*) &server_handle, NULL);
  56. uv_close((uv_handle_t*) &client_handle, NULL);
  57. uv_close((uv_handle_t*) &peer_handle, NULL);
  58. uv_close((uv_handle_t*) idle, NULL);
  59. }
  60. static void poll_cb(uv_poll_t* handle, int status, int events) {
  61. char buffer[5];
  62. int n;
  63. int fd;
  64. ASSERT(0 == uv_fileno((uv_handle_t*)handle, &fd));
  65. memset(buffer, 0, 5);
  66. if (events & UV_PRIORITIZED) {
  67. do
  68. n = recv(client_fd, &buffer, 5, MSG_OOB);
  69. while (n == -1 && errno == EINTR);
  70. ASSERT(n >= 0 || errno != EINVAL);
  71. cli_pr_check = 1;
  72. ASSERT(0 == uv_poll_stop(&poll_req[0]));
  73. ASSERT(0 == uv_poll_start(&poll_req[0],
  74. UV_READABLE | UV_WRITABLE,
  75. poll_cb));
  76. }
  77. if (events & UV_READABLE) {
  78. if (fd == client_fd) {
  79. do
  80. n = recv(client_fd, &buffer, 5, 0);
  81. while (n == -1 && errno == EINTR);
  82. ASSERT(n >= 0 || errno != EINVAL);
  83. if (cli_rd_check == 1) {
  84. ASSERT(strncmp(buffer, "world", n) == 0);
  85. ASSERT(5 == n);
  86. cli_rd_check = 2;
  87. }
  88. if (cli_rd_check == 0) {
  89. ASSERT(n == 4);
  90. ASSERT(strncmp(buffer, "hello", n) == 0);
  91. cli_rd_check = 1;
  92. do {
  93. do
  94. n = recv(server_fd, &buffer, 5, 0);
  95. while (n == -1 && errno == EINTR);
  96. if (n > 0) {
  97. ASSERT(n == 5);
  98. ASSERT(strncmp(buffer, "world", n) == 0);
  99. cli_rd_check = 2;
  100. }
  101. } while (n > 0);
  102. ASSERT(got_eagain());
  103. }
  104. }
  105. if (fd == server_fd) {
  106. do
  107. n = recv(server_fd, &buffer, 3, 0);
  108. while (n == -1 && errno == EINTR);
  109. ASSERT(n >= 0 || errno != EINVAL);
  110. ASSERT(3 == n);
  111. ASSERT(strncmp(buffer, "foo", n) == 0);
  112. srv_rd_check = 1;
  113. uv_poll_stop(&poll_req[1]);
  114. }
  115. }
  116. if (events & UV_WRITABLE) {
  117. do {
  118. n = send(client_fd, "foo", 3, 0);
  119. } while (n < 0 && errno == EINTR);
  120. ASSERT(3 == n);
  121. }
  122. }
  123. static void connection_cb(uv_stream_t* handle, int status) {
  124. int r;
  125. ASSERT(0 == status);
  126. ASSERT(0 == uv_accept(handle, (uv_stream_t*) &peer_handle));
  127. ASSERT(0 == uv_fileno((uv_handle_t*) &peer_handle, &server_fd));
  128. ASSERT(0 == uv_poll_init_socket(uv_default_loop(), &poll_req[0], client_fd));
  129. ASSERT(0 == uv_poll_init_socket(uv_default_loop(), &poll_req[1], server_fd));
  130. ASSERT(0 == uv_poll_start(&poll_req[0],
  131. UV_PRIORITIZED | UV_READABLE | UV_WRITABLE,
  132. poll_cb));
  133. ASSERT(0 == uv_poll_start(&poll_req[1],
  134. UV_READABLE,
  135. poll_cb));
  136. do {
  137. r = send(server_fd, "hello", 5, MSG_OOB);
  138. } while (r < 0 && errno == EINTR);
  139. ASSERT(5 == r);
  140. do {
  141. r = send(server_fd, "world", 5, 0);
  142. } while (r < 0 && errno == EINTR);
  143. ASSERT(5 == r);
  144. ASSERT(0 == uv_idle_start(&idle, idle_cb));
  145. }
  146. TEST_IMPL(poll_oob) {
  147. struct sockaddr_in addr;
  148. int r = 0;
  149. uv_loop_t* loop;
  150. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  151. loop = uv_default_loop();
  152. ASSERT(0 == uv_tcp_init(loop, &server_handle));
  153. ASSERT(0 == uv_tcp_init(loop, &client_handle));
  154. ASSERT(0 == uv_tcp_init(loop, &peer_handle));
  155. ASSERT(0 == uv_idle_init(loop, &idle));
  156. ASSERT(0 == uv_tcp_bind(&server_handle, (const struct sockaddr*) &addr, 0));
  157. ASSERT(0 == uv_listen((uv_stream_t*) &server_handle, 1, connection_cb));
  158. /* Ensure two separate packets */
  159. ASSERT(0 == uv_tcp_nodelay(&client_handle, 1));
  160. client_fd = socket(PF_INET, SOCK_STREAM, 0);
  161. ASSERT(client_fd >= 0);
  162. do {
  163. errno = 0;
  164. r = connect(client_fd, (const struct sockaddr*)&addr, sizeof(addr));
  165. } while (r == -1 && errno == EINTR);
  166. ASSERT(r == 0);
  167. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  168. ASSERT(ticks == kMaxTicks);
  169. /* Did client receive the POLLPRI message */
  170. ASSERT(cli_pr_check == 1);
  171. /* Did client receive the POLLIN message */
  172. ASSERT(cli_rd_check == 2);
  173. /* Could we write with POLLOUT and did the server receive our POLLOUT message
  174. * through POLLIN.
  175. */
  176. ASSERT(srv_rd_check == 1);
  177. MAKE_VALGRIND_HAPPY();
  178. return 0;
  179. }
  180. #else
  181. typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
  182. #endif