test-tcp-connect-error-after-write.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 int connect_cb_called;
  27. static int write_cb_called;
  28. static int close_cb_called;
  29. static void close_cb(uv_handle_t* handle) {
  30. close_cb_called++;
  31. }
  32. static void connect_cb(uv_connect_t* req, int status) {
  33. ASSERT(status == -1);
  34. connect_cb_called++;
  35. uv_close((uv_handle_t*)req->handle, close_cb);
  36. }
  37. static void write_cb(uv_write_t* req, int status) {
  38. ASSERT(status == -1);
  39. write_cb_called++;
  40. }
  41. /*
  42. * Try to connect to an address on which nothing listens, get ECONNREFUSED
  43. * (uv errno 12) and get connect_cb() called once with status != 0.
  44. * Related issue: https://github.com/joyent/libuv/issues/443
  45. */
  46. TEST_IMPL(tcp_connect_error_after_write) {
  47. uv_connect_t connect_req;
  48. struct sockaddr_in addr;
  49. uv_write_t write_req;
  50. uv_tcp_t conn;
  51. uv_buf_t buf;
  52. int r;
  53. #ifdef _WIN32
  54. fprintf(stderr, "This test is disabled on Windows for now.\n");
  55. fprintf(stderr, "See https://github.com/joyent/libuv/issues/444\n");
  56. return 0; /* windows slackers... */
  57. #endif
  58. addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  59. buf = uv_buf_init("TEST", 4);
  60. r = uv_tcp_init(uv_default_loop(), &conn);
  61. ASSERT(r == 0);
  62. r = uv_write(&write_req, (uv_stream_t*)&conn, &buf, 1, write_cb);
  63. ASSERT(r == -1);
  64. ASSERT(uv_last_error(uv_default_loop()).code == UV_EBADF);
  65. r = uv_tcp_connect(&connect_req, &conn, addr, connect_cb);
  66. ASSERT(r == 0);
  67. r = uv_write(&write_req, (uv_stream_t*)&conn, &buf, 1, write_cb);
  68. ASSERT(r == 0);
  69. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  70. ASSERT(r == 0);
  71. ASSERT(connect_cb_called == 1);
  72. ASSERT(write_cb_called == 1);
  73. ASSERT(close_cb_called == 1);
  74. MAKE_VALGRIND_HAPPY();
  75. return 0;
  76. }