test-connection-fail.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <stdlib.h>
  24. #include <stdio.h>
  25. static uv_tcp_t tcp;
  26. static uv_connect_t req;
  27. static int connect_cb_calls;
  28. static int close_cb_calls;
  29. static uv_timer_t timer;
  30. static int timer_close_cb_calls;
  31. static int timer_cb_calls;
  32. static void on_close(uv_handle_t* handle) {
  33. close_cb_calls++;
  34. }
  35. static void timer_close_cb(uv_handle_t* handle) {
  36. timer_close_cb_calls++;
  37. }
  38. static void timer_cb(uv_timer_t* handle, int status) {
  39. ASSERT(status == 0);
  40. timer_cb_calls++;
  41. /*
  42. * These are the important asserts. The connection callback has been made,
  43. * but libuv hasn't automatically closed the socket. The user must
  44. * uv_close the handle manually.
  45. */
  46. ASSERT(close_cb_calls == 0);
  47. ASSERT(connect_cb_calls == 1);
  48. /* Close the tcp handle. */
  49. uv_close((uv_handle_t*)&tcp, on_close);
  50. /* Close the timer. */
  51. uv_close((uv_handle_t*)handle, timer_close_cb);
  52. }
  53. static void on_connect_with_close(uv_connect_t *req, int status) {
  54. ASSERT((uv_stream_t*) &tcp == req->handle);
  55. ASSERT(status == UV_ECONNREFUSED);
  56. connect_cb_calls++;
  57. ASSERT(close_cb_calls == 0);
  58. uv_close((uv_handle_t*)req->handle, on_close);
  59. }
  60. static void on_connect_without_close(uv_connect_t *req, int status) {
  61. ASSERT(status == UV_ECONNREFUSED);
  62. connect_cb_calls++;
  63. uv_timer_start(&timer, timer_cb, 100, 0);
  64. ASSERT(close_cb_calls == 0);
  65. }
  66. static void connection_fail(uv_connect_cb connect_cb) {
  67. struct sockaddr_in client_addr, server_addr;
  68. int r;
  69. ASSERT(0 == uv_ip4_addr("0.0.0.0", 0, &client_addr));
  70. /* There should be no servers listening on this port. */
  71. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr));
  72. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  73. r = uv_tcp_init(uv_default_loop(), &tcp);
  74. ASSERT(!r);
  75. /* We are never doing multiple reads/connects at a time anyway. */
  76. /* so these handles can be pre-initialized. */
  77. ASSERT(0 == uv_tcp_bind(&tcp, (const struct sockaddr*) &client_addr, 0));
  78. r = uv_tcp_connect(&req,
  79. &tcp,
  80. (const struct sockaddr*) &server_addr,
  81. connect_cb);
  82. ASSERT(!r);
  83. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  84. ASSERT(connect_cb_calls == 1);
  85. ASSERT(close_cb_calls == 1);
  86. }
  87. /*
  88. * This test attempts to connect to a port where no server is running. We
  89. * expect an error.
  90. */
  91. TEST_IMPL(connection_fail) {
  92. connection_fail(on_connect_with_close);
  93. ASSERT(timer_close_cb_calls == 0);
  94. ASSERT(timer_cb_calls == 0);
  95. MAKE_VALGRIND_HAPPY();
  96. return 0;
  97. }
  98. /*
  99. * This test is the same as the first except it check that the close
  100. * callback of the tcp handle hasn't been made after the failed connection
  101. * attempt.
  102. */
  103. TEST_IMPL(connection_fail_doesnt_auto_close) {
  104. int r;
  105. r = uv_timer_init(uv_default_loop(), &timer);
  106. ASSERT(r == 0);
  107. connection_fail(on_connect_without_close);
  108. ASSERT(timer_close_cb_calls == 1);
  109. ASSERT(timer_cb_calls == 1);
  110. MAKE_VALGRIND_HAPPY();
  111. return 0;
  112. }