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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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, const uv_buf_t* buf);
  30. static void alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
  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 void alloc_cb(uv_handle_t* handle,
  55. size_t suggested_size,
  56. uv_buf_t* buf) {
  57. static char slab[1024];
  58. buf->base = slab;
  59. buf->len = sizeof(slab);
  60. }
  61. static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
  62. if (nread < 0) {
  63. fprintf(stderr, "read_cb error: %s\n", uv_err_name(nread));
  64. ASSERT(nread == UV_ECONNRESET || nread == UV_EOF);
  65. uv_close((uv_handle_t*)&tcp_server, NULL);
  66. uv_close((uv_handle_t*)&tcp_peer, NULL);
  67. }
  68. read_cb_called++;
  69. }
  70. static void connect_cb(uv_connect_t* req, int status) {
  71. ASSERT(req == &connect_req);
  72. ASSERT(status == 0);
  73. /* Close the client. */
  74. uv_close((uv_handle_t*)&tcp_client, NULL);
  75. }
  76. static void write_cb(uv_write_t* req, int status) {
  77. ASSERT(status == 0);
  78. write_cb_called++;
  79. }
  80. TEST_IMPL(tcp_write_to_half_open_connection) {
  81. struct sockaddr_in addr;
  82. uv_loop_t* loop;
  83. int r;
  84. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  85. loop = uv_default_loop();
  86. ASSERT(loop != NULL);
  87. r = uv_tcp_init(loop, &tcp_server);
  88. ASSERT(r == 0);
  89. r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
  90. ASSERT(r == 0);
  91. r = uv_listen((uv_stream_t*)&tcp_server, 1, connection_cb);
  92. ASSERT(r == 0);
  93. r = uv_tcp_init(loop, &tcp_client);
  94. ASSERT(r == 0);
  95. r = uv_tcp_connect(&connect_req,
  96. &tcp_client,
  97. (const struct sockaddr*) &addr,
  98. connect_cb);
  99. ASSERT(r == 0);
  100. r = uv_run(loop, UV_RUN_DEFAULT);
  101. ASSERT(r == 0);
  102. ASSERT(write_cb_called > 0);
  103. ASSERT(read_cb_called > 0);
  104. MAKE_VALGRIND_HAPPY();
  105. return 0;
  106. }