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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 < 0);
  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 < 0);
  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. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  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 == UV_EBADF);
  64. r = uv_tcp_connect(&connect_req,
  65. &conn,
  66. (const struct sockaddr*) &addr,
  67. connect_cb);
  68. ASSERT(r == 0);
  69. r = uv_write(&write_req, (uv_stream_t*)&conn, &buf, 1, write_cb);
  70. ASSERT(r == 0);
  71. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  72. ASSERT(r == 0);
  73. ASSERT(connect_cb_called == 1);
  74. ASSERT(write_cb_called == 1);
  75. ASSERT(close_cb_called == 1);
  76. MAKE_VALGRIND_HAPPY();
  77. return 0;
  78. }