test-tcp-write-to-half-open-connection.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <stdlib.h>
  25. #include <string.h>
  26. static void connection_cb(uv_stream_t* server, int status);
  27. static void connect_cb(uv_connect_t* req, int status);
  28. static void write_cb(uv_write_t* req, int status);
  29. static void read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t buf);
  30. static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size);
  31. static uv_tcp_t tcp_server;
  32. static uv_tcp_t tcp_client;
  33. static uv_tcp_t tcp_peer; /* client socket as accept()-ed by server */
  34. static uv_connect_t connect_req;
  35. static uv_write_t write_req;
  36. static int write_cb_called;
  37. static int read_cb_called;
  38. static void connection_cb(uv_stream_t* server, int status) {
  39. int r;
  40. uv_buf_t buf;
  41. ASSERT(server == (uv_stream_t*)&tcp_server);
  42. ASSERT(status == 0);
  43. r = uv_tcp_init(server->loop, &tcp_peer);
  44. ASSERT(r == 0);
  45. r = uv_accept(server, (uv_stream_t*)&tcp_peer);
  46. ASSERT(r == 0);
  47. r = uv_read_start((uv_stream_t*)&tcp_peer, alloc_cb, read_cb);
  48. ASSERT(r == 0);
  49. buf.base = "hello\n";
  50. buf.len = 6;
  51. r = uv_write(&write_req, (uv_stream_t*)&tcp_peer, &buf, 1, write_cb);
  52. ASSERT(r == 0);
  53. }
  54. static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size) {
  55. static char slab[1024];
  56. return uv_buf_init(slab, sizeof slab);
  57. }
  58. static void read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t buf) {
  59. if (nread == -1) {
  60. fprintf(stderr, "read_cb error: %s\n", uv_err_name(uv_last_error(stream->loop)));
  61. ASSERT(uv_last_error(stream->loop).code == UV_ECONNRESET ||
  62. uv_last_error(stream->loop).code == UV_EOF);
  63. uv_close((uv_handle_t*)&tcp_server, NULL);
  64. uv_close((uv_handle_t*)&tcp_peer, NULL);
  65. }
  66. read_cb_called++;
  67. }
  68. static void connect_cb(uv_connect_t* req, int status) {
  69. ASSERT(req == &connect_req);
  70. ASSERT(status == 0);
  71. /* Close the client. */
  72. uv_close((uv_handle_t*)&tcp_client, NULL);
  73. }
  74. static void write_cb(uv_write_t* req, int status) {
  75. ASSERT(status == 0);
  76. write_cb_called++;
  77. }
  78. TEST_IMPL(tcp_write_to_half_open_connection) {
  79. uv_loop_t* loop;
  80. int r;
  81. loop = uv_default_loop();
  82. ASSERT(loop != NULL);
  83. r = uv_tcp_init(loop, &tcp_server);
  84. ASSERT(r == 0);
  85. r = uv_tcp_bind(&tcp_server, uv_ip4_addr("127.0.0.1", TEST_PORT));
  86. ASSERT(r == 0);
  87. r = uv_listen((uv_stream_t*)&tcp_server, 1, connection_cb);
  88. ASSERT(r == 0);
  89. r = uv_tcp_init(loop, &tcp_client);
  90. ASSERT(r == 0);
  91. r = uv_tcp_connect(&connect_req,
  92. &tcp_client,
  93. uv_ip4_addr("127.0.0.1", TEST_PORT),
  94. connect_cb);
  95. ASSERT(r == 0);
  96. r = uv_run(loop, UV_RUN_DEFAULT);
  97. ASSERT(r == 0);
  98. ASSERT(write_cb_called > 0);
  99. ASSERT(read_cb_called > 0);
  100. MAKE_VALGRIND_HAPPY();
  101. return 0;
  102. }