1
0

test-tcp-close-accept.c 6.0 KB

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