test-tcp-bind-error.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. static int close_cb_called = 0;
  26. static void close_cb(uv_handle_t* handle) {
  27. ASSERT(handle != NULL);
  28. close_cb_called++;
  29. }
  30. TEST_IMPL(tcp_bind_error_addrinuse) {
  31. struct sockaddr_in addr;
  32. uv_tcp_t server1, server2;
  33. int r;
  34. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
  35. r = uv_tcp_init(uv_default_loop(), &server1);
  36. ASSERT(r == 0);
  37. r = uv_tcp_bind(&server1, (const struct sockaddr*) &addr, 0);
  38. ASSERT(r == 0);
  39. r = uv_tcp_init(uv_default_loop(), &server2);
  40. ASSERT(r == 0);
  41. r = uv_tcp_bind(&server2, (const struct sockaddr*) &addr, 0);
  42. ASSERT(r == 0);
  43. r = uv_listen((uv_stream_t*)&server1, 128, NULL);
  44. ASSERT(r == 0);
  45. r = uv_listen((uv_stream_t*)&server2, 128, NULL);
  46. ASSERT(r == UV_EADDRINUSE);
  47. uv_close((uv_handle_t*)&server1, close_cb);
  48. uv_close((uv_handle_t*)&server2, close_cb);
  49. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  50. ASSERT(close_cb_called == 2);
  51. MAKE_VALGRIND_HAPPY();
  52. return 0;
  53. }
  54. TEST_IMPL(tcp_bind_error_addrnotavail_1) {
  55. struct sockaddr_in addr;
  56. uv_tcp_t server;
  57. int r;
  58. ASSERT(0 == uv_ip4_addr("127.255.255.255", TEST_PORT, &addr));
  59. r = uv_tcp_init(uv_default_loop(), &server);
  60. ASSERT(r == 0);
  61. /* It seems that Linux is broken here - bind succeeds. */
  62. r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);
  63. ASSERT(r == 0 || r == UV_EADDRNOTAVAIL);
  64. uv_close((uv_handle_t*)&server, close_cb);
  65. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  66. ASSERT(close_cb_called == 1);
  67. MAKE_VALGRIND_HAPPY();
  68. return 0;
  69. }
  70. TEST_IMPL(tcp_bind_error_addrnotavail_2) {
  71. struct sockaddr_in addr;
  72. uv_tcp_t server;
  73. int r;
  74. ASSERT(0 == uv_ip4_addr("4.4.4.4", TEST_PORT, &addr));
  75. r = uv_tcp_init(uv_default_loop(), &server);
  76. ASSERT(r == 0);
  77. r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);
  78. ASSERT(r == UV_EADDRNOTAVAIL);
  79. uv_close((uv_handle_t*)&server, close_cb);
  80. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  81. ASSERT(close_cb_called == 1);
  82. MAKE_VALGRIND_HAPPY();
  83. return 0;
  84. }
  85. TEST_IMPL(tcp_bind_error_fault) {
  86. char garbage[] =
  87. "blah blah blah blah blah blah blah blah blah blah blah blah";
  88. struct sockaddr_in* garbage_addr;
  89. uv_tcp_t server;
  90. int r;
  91. garbage_addr = (struct sockaddr_in*) &garbage;
  92. r = uv_tcp_init(uv_default_loop(), &server);
  93. ASSERT(r == 0);
  94. r = uv_tcp_bind(&server, (const struct sockaddr*) garbage_addr, 0);
  95. ASSERT(r == UV_EINVAL);
  96. uv_close((uv_handle_t*)&server, close_cb);
  97. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  98. ASSERT(close_cb_called == 1);
  99. MAKE_VALGRIND_HAPPY();
  100. return 0;
  101. }
  102. /* Notes: On Linux uv_bind(server, NULL) will segfault the program. */
  103. TEST_IMPL(tcp_bind_error_inval) {
  104. struct sockaddr_in addr1;
  105. struct sockaddr_in addr2;
  106. uv_tcp_t server;
  107. int r;
  108. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr1));
  109. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT_2, &addr2));
  110. r = uv_tcp_init(uv_default_loop(), &server);
  111. ASSERT(r == 0);
  112. r = uv_tcp_bind(&server, (const struct sockaddr*) &addr1, 0);
  113. ASSERT(r == 0);
  114. r = uv_tcp_bind(&server, (const struct sockaddr*) &addr2, 0);
  115. ASSERT(r == UV_EINVAL);
  116. uv_close((uv_handle_t*)&server, close_cb);
  117. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  118. ASSERT(close_cb_called == 1);
  119. MAKE_VALGRIND_HAPPY();
  120. return 0;
  121. }
  122. TEST_IMPL(tcp_bind_localhost_ok) {
  123. struct sockaddr_in addr;
  124. uv_tcp_t server;
  125. int r;
  126. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  127. r = uv_tcp_init(uv_default_loop(), &server);
  128. ASSERT(r == 0);
  129. r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);
  130. ASSERT(r == 0);
  131. MAKE_VALGRIND_HAPPY();
  132. return 0;
  133. }
  134. TEST_IMPL(tcp_listen_without_bind) {
  135. int r;
  136. uv_tcp_t server;
  137. r = uv_tcp_init(uv_default_loop(), &server);
  138. ASSERT(r == 0);
  139. r = uv_listen((uv_stream_t*)&server, 128, NULL);
  140. ASSERT(r == 0);
  141. MAKE_VALGRIND_HAPPY();
  142. return 0;
  143. }