test-tcp-bind-error.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 = uv_ip4_addr("0.0.0.0", TEST_PORT);
  32. uv_tcp_t server1, server2;
  33. int r;
  34. r = uv_tcp_init(uv_default_loop(), &server1);
  35. ASSERT(r == 0);
  36. r = uv_tcp_bind(&server1, addr);
  37. ASSERT(r == 0);
  38. r = uv_tcp_init(uv_default_loop(), &server2);
  39. ASSERT(r == 0);
  40. r = uv_tcp_bind(&server2, addr);
  41. ASSERT(r == 0);
  42. r = uv_listen((uv_stream_t*)&server1, 128, NULL);
  43. ASSERT(r == 0);
  44. r = uv_listen((uv_stream_t*)&server2, 128, NULL);
  45. ASSERT(r == -1);
  46. ASSERT(uv_last_error(uv_default_loop()).code == 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 = uv_ip4_addr("127.255.255.255", TEST_PORT);
  56. uv_tcp_t server;
  57. int r;
  58. r = uv_tcp_init(uv_default_loop(), &server);
  59. ASSERT(r == 0);
  60. r = uv_tcp_bind(&server, addr);
  61. /* It seems that Linux is broken here - bind succeeds. */
  62. if (r == -1) {
  63. ASSERT(uv_last_error(uv_default_loop()).code == UV_EADDRNOTAVAIL);
  64. }
  65. uv_close((uv_handle_t*)&server, close_cb);
  66. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  67. ASSERT(close_cb_called == 1);
  68. MAKE_VALGRIND_HAPPY();
  69. return 0;
  70. }
  71. TEST_IMPL(tcp_bind_error_addrnotavail_2) {
  72. struct sockaddr_in addr = uv_ip4_addr("4.4.4.4", TEST_PORT);
  73. uv_tcp_t server;
  74. int r;
  75. r = uv_tcp_init(uv_default_loop(), &server);
  76. ASSERT(r == 0);
  77. r = uv_tcp_bind(&server, addr);
  78. ASSERT(r == -1);
  79. ASSERT(uv_last_error(uv_default_loop()).code == UV_EADDRNOTAVAIL);
  80. uv_close((uv_handle_t*)&server, close_cb);
  81. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  82. ASSERT(close_cb_called == 1);
  83. MAKE_VALGRIND_HAPPY();
  84. return 0;
  85. }
  86. TEST_IMPL(tcp_bind_error_fault) {
  87. char garbage[] = "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, *garbage_addr);
  95. ASSERT(r == -1);
  96. ASSERT(uv_last_error(uv_default_loop()).code == UV_EINVAL);
  97. uv_close((uv_handle_t*)&server, close_cb);
  98. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  99. ASSERT(close_cb_called == 1);
  100. MAKE_VALGRIND_HAPPY();
  101. return 0;
  102. }
  103. /* Notes: On Linux uv_bind(server, NULL) will segfault the program. */
  104. TEST_IMPL(tcp_bind_error_inval) {
  105. struct sockaddr_in addr1 = uv_ip4_addr("0.0.0.0", TEST_PORT);
  106. struct sockaddr_in addr2 = uv_ip4_addr("0.0.0.0", TEST_PORT_2);
  107. uv_tcp_t server;
  108. int r;
  109. r = uv_tcp_init(uv_default_loop(), &server);
  110. ASSERT(r == 0);
  111. r = uv_tcp_bind(&server, addr1);
  112. ASSERT(r == 0);
  113. r = uv_tcp_bind(&server, addr2);
  114. ASSERT(r == -1);
  115. ASSERT(uv_last_error(uv_default_loop()).code == 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 = uv_ip4_addr("127.0.0.1", TEST_PORT);
  124. uv_tcp_t server;
  125. int r;
  126. r = uv_tcp_init(uv_default_loop(), &server);
  127. ASSERT(r == 0);
  128. r = uv_tcp_bind(&server, addr);
  129. ASSERT(r == 0);
  130. MAKE_VALGRIND_HAPPY();
  131. return 0;
  132. }
  133. TEST_IMPL(tcp_listen_without_bind) {
  134. int r;
  135. uv_tcp_t server;
  136. r = uv_tcp_init(uv_default_loop(), &server);
  137. ASSERT(r == 0);
  138. r = uv_listen((uv_stream_t*)&server, 128, NULL);
  139. ASSERT(r == 0);
  140. MAKE_VALGRIND_HAPPY();
  141. return 0;
  142. }