test-tcp-close-accept.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* Copyright Joyent, Inc. and other Node 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. /* this test is Unix only */
  22. #ifndef _WIN32
  23. #include "uv.h"
  24. #include "task.h"
  25. #include <stdio.h>
  26. #include <string.h>
  27. static struct sockaddr_in addr;
  28. static uv_tcp_t tcp_server;
  29. static uv_tcp_t tcp_outgoing[2];
  30. static uv_tcp_t tcp_incoming[ARRAY_SIZE(tcp_outgoing)];
  31. static uv_connect_t connect_reqs[ARRAY_SIZE(tcp_outgoing)];
  32. static uv_tcp_t tcp_check;
  33. static uv_connect_t tcp_check_req;
  34. static uv_write_t write_reqs[ARRAY_SIZE(tcp_outgoing)];
  35. static unsigned int got_connections;
  36. static unsigned int close_cb_called;
  37. static unsigned int write_cb_called;
  38. static unsigned int read_cb_called;
  39. static unsigned int pending_incoming;
  40. static void close_cb(uv_handle_t* handle) {
  41. close_cb_called++;
  42. }
  43. static void write_cb(uv_write_t* req, int status) {
  44. ASSERT(status == 0);
  45. write_cb_called++;
  46. }
  47. static void connect_cb(uv_connect_t* req, int status) {
  48. unsigned int i;
  49. uv_buf_t buf;
  50. uv_stream_t* outgoing;
  51. if (req == &tcp_check_req) {
  52. ASSERT(status != 0);
  53. /*
  54. * Time to finish the test: close both the check and pending incoming
  55. * connections
  56. */
  57. uv_close((uv_handle_t*) &tcp_incoming[pending_incoming], close_cb);
  58. uv_close((uv_handle_t*) &tcp_check, close_cb);
  59. return;
  60. }
  61. ASSERT(status == 0);
  62. ASSERT(connect_reqs <= req);
  63. ASSERT(req <= connect_reqs + ARRAY_SIZE(connect_reqs));
  64. i = req - connect_reqs;
  65. buf = uv_buf_init("x", 1);
  66. outgoing = (uv_stream_t*) &tcp_outgoing[i];
  67. ASSERT(0 == uv_write(&write_reqs[i], outgoing, &buf, 1, write_cb));
  68. }
  69. static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
  70. static char slab[1];
  71. buf->base = slab;
  72. buf->len = sizeof(slab);
  73. }
  74. static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
  75. uv_loop_t* loop;
  76. unsigned int i;
  77. pending_incoming = (uv_tcp_t*) stream - &tcp_incoming[0];
  78. ASSERT(pending_incoming < got_connections);
  79. ASSERT(0 == uv_read_stop(stream));
  80. ASSERT(1 == nread);
  81. loop = stream->loop;
  82. read_cb_called++;
  83. /* Close all active incomings, except current one */
  84. for (i = 0; i < got_connections; i++) {
  85. if (i != pending_incoming)
  86. uv_close((uv_handle_t*) &tcp_incoming[i], close_cb);
  87. }
  88. /* Close server, so no one will connect to it */
  89. uv_close((uv_handle_t*) &tcp_server, close_cb);
  90. /* Create new fd that should be one of the closed incomings */
  91. ASSERT(0 == uv_tcp_init(loop, &tcp_check));
  92. ASSERT(0 == uv_tcp_connect(&tcp_check_req,
  93. &tcp_check,
  94. (const struct sockaddr*) &addr,
  95. connect_cb));
  96. ASSERT(0 == uv_read_start((uv_stream_t*) &tcp_check, alloc_cb, read_cb));
  97. }
  98. static void connection_cb(uv_stream_t* server, int status) {
  99. unsigned int i;
  100. uv_tcp_t* incoming;
  101. ASSERT(server == (uv_stream_t*) &tcp_server);
  102. /* Ignore tcp_check connection */
  103. if (got_connections == ARRAY_SIZE(tcp_incoming))
  104. return;
  105. /* Accept everyone */
  106. incoming = &tcp_incoming[got_connections++];
  107. ASSERT(0 == uv_tcp_init(server->loop, incoming));
  108. ASSERT(0 == uv_accept(server, (uv_stream_t*) incoming));
  109. if (got_connections != ARRAY_SIZE(tcp_incoming))
  110. return;
  111. /* Once all clients are accepted - start reading */
  112. for (i = 0; i < ARRAY_SIZE(tcp_incoming); i++) {
  113. incoming = &tcp_incoming[i];
  114. ASSERT(0 == uv_read_start((uv_stream_t*) incoming, alloc_cb, read_cb));
  115. }
  116. }
  117. TEST_IMPL(tcp_close_accept) {
  118. unsigned int i;
  119. uv_loop_t* loop;
  120. uv_tcp_t* client;
  121. /*
  122. * A little explanation of what goes on below:
  123. *
  124. * We'll create server and connect to it using two clients, each writing one
  125. * byte once connected.
  126. *
  127. * When all clients will be accepted by server - we'll start reading from them
  128. * and, on first client's first byte, will close second client and server.
  129. * After that, we'll immediately initiate new connection to server using
  130. * tcp_check handle (thus, reusing fd from second client).
  131. *
  132. * In this situation uv__io_poll()'s event list should still contain read
  133. * event for second client, and, if not cleaned up properly, `tcp_check` will
  134. * receive stale event of second incoming and invoke `connect_cb` with zero
  135. * status.
  136. */
  137. loop = uv_default_loop();
  138. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  139. ASSERT(0 == uv_tcp_init(loop, &tcp_server));
  140. ASSERT(0 == uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0));
  141. ASSERT(0 == uv_listen((uv_stream_t*) &tcp_server,
  142. ARRAY_SIZE(tcp_outgoing),
  143. connection_cb));
  144. for (i = 0; i < ARRAY_SIZE(tcp_outgoing); i++) {
  145. client = tcp_outgoing + i;
  146. ASSERT(0 == uv_tcp_init(loop, client));
  147. ASSERT(0 == uv_tcp_connect(&connect_reqs[i],
  148. client,
  149. (const struct sockaddr*) &addr,
  150. connect_cb));
  151. }
  152. uv_run(loop, UV_RUN_DEFAULT);
  153. ASSERT(ARRAY_SIZE(tcp_outgoing) == got_connections);
  154. ASSERT((ARRAY_SIZE(tcp_outgoing) + 2) == close_cb_called);
  155. ASSERT(ARRAY_SIZE(tcp_outgoing) == write_cb_called);
  156. ASSERT(1 == read_cb_called);
  157. MAKE_VALGRIND_HAPPY();
  158. return 0;
  159. }
  160. #else
  161. typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
  162. #endif /* !_WIN32 */