1
0

test-connection-fail.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 == -1);
  56. ASSERT(uv_last_error(uv_default_loop()).code == UV_ECONNREFUSED);
  57. connect_cb_calls++;
  58. ASSERT(close_cb_calls == 0);
  59. uv_close((uv_handle_t*)req->handle, on_close);
  60. }
  61. static void on_connect_without_close(uv_connect_t *req, int status) {
  62. ASSERT(status == -1);
  63. ASSERT(uv_last_error(uv_default_loop()).code == UV_ECONNREFUSED);
  64. connect_cb_calls++;
  65. uv_timer_start(&timer, timer_cb, 100, 0);
  66. ASSERT(close_cb_calls == 0);
  67. }
  68. static void connection_fail(uv_connect_cb connect_cb) {
  69. struct sockaddr_in client_addr, server_addr;
  70. int r;
  71. client_addr = uv_ip4_addr("0.0.0.0", 0);
  72. /* There should be no servers listening on this port. */
  73. server_addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  74. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  75. r = uv_tcp_init(uv_default_loop(), &tcp);
  76. ASSERT(!r);
  77. /* We are never doing multiple reads/connects at a time anyway. */
  78. /* so these handles can be pre-initialized. */
  79. uv_tcp_bind(&tcp, client_addr);
  80. r = uv_tcp_connect(&req, &tcp, server_addr, connect_cb);
  81. ASSERT(!r);
  82. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  83. ASSERT(connect_cb_calls == 1);
  84. ASSERT(close_cb_calls == 1);
  85. }
  86. /*
  87. * This test attempts to connect to a port where no server is running. We
  88. * expect an error.
  89. */
  90. TEST_IMPL(connection_fail) {
  91. connection_fail(on_connect_with_close);
  92. ASSERT(timer_close_cb_calls == 0);
  93. ASSERT(timer_cb_calls == 0);
  94. MAKE_VALGRIND_HAPPY();
  95. return 0;
  96. }
  97. /*
  98. * This test is the same as the first except it check that the close
  99. * callback of the tcp handle hasn't been made after the failed connection
  100. * attempt.
  101. */
  102. TEST_IMPL(connection_fail_doesnt_auto_close) {
  103. int r;
  104. r = uv_timer_init(uv_default_loop(), &timer);
  105. ASSERT(r == 0);
  106. connection_fail(on_connect_without_close);
  107. ASSERT(timer_close_cb_calls == 1);
  108. ASSERT(timer_cb_calls == 1);
  109. MAKE_VALGRIND_HAPPY();
  110. return 0;
  111. }