test-tcp-bind6-error.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_bind6_error_addrinuse) {
  31. struct sockaddr_in6 addr;
  32. uv_tcp_t server1, server2;
  33. int r;
  34. ASSERT(0 == uv_ip6_addr("::", 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_bind6_error_addrnotavail) {
  55. struct sockaddr_in6 addr;
  56. uv_tcp_t server;
  57. int r;
  58. ASSERT(0 == uv_ip6_addr("4:4:4:4:4:4:4:4", TEST_PORT, &addr));
  59. r = uv_tcp_init(uv_default_loop(), &server);
  60. ASSERT(r == 0);
  61. r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);
  62. ASSERT(r == UV_EADDRNOTAVAIL);
  63. uv_close((uv_handle_t*)&server, close_cb);
  64. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  65. ASSERT(close_cb_called == 1);
  66. MAKE_VALGRIND_HAPPY();
  67. return 0;
  68. }
  69. TEST_IMPL(tcp_bind6_error_fault) {
  70. char garbage[] =
  71. "blah blah blah blah blah blah blah blah blah blah blah blah";
  72. struct sockaddr_in6* garbage_addr;
  73. uv_tcp_t server;
  74. int r;
  75. garbage_addr = (struct sockaddr_in6*) &garbage;
  76. r = uv_tcp_init(uv_default_loop(), &server);
  77. ASSERT(r == 0);
  78. r = uv_tcp_bind(&server, (const struct sockaddr*) garbage_addr, 0);
  79. ASSERT(r == UV_EINVAL);
  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. /* Notes: On Linux uv_bind6(server, NULL) will segfault the program. */
  87. TEST_IMPL(tcp_bind6_error_inval) {
  88. struct sockaddr_in6 addr1;
  89. struct sockaddr_in6 addr2;
  90. uv_tcp_t server;
  91. int r;
  92. ASSERT(0 == uv_ip6_addr("::", TEST_PORT, &addr1));
  93. ASSERT(0 == uv_ip6_addr("::", TEST_PORT_2, &addr2));
  94. r = uv_tcp_init(uv_default_loop(), &server);
  95. ASSERT(r == 0);
  96. r = uv_tcp_bind(&server, (const struct sockaddr*) &addr1, 0);
  97. ASSERT(r == 0);
  98. r = uv_tcp_bind(&server, (const struct sockaddr*) &addr2, 0);
  99. ASSERT(r == UV_EINVAL);
  100. uv_close((uv_handle_t*)&server, close_cb);
  101. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  102. ASSERT(close_cb_called == 1);
  103. MAKE_VALGRIND_HAPPY();
  104. return 0;
  105. }
  106. TEST_IMPL(tcp_bind6_localhost_ok) {
  107. struct sockaddr_in6 addr;
  108. uv_tcp_t server;
  109. int r;
  110. ASSERT(0 == uv_ip6_addr("::1", TEST_PORT, &addr));
  111. r = uv_tcp_init(uv_default_loop(), &server);
  112. ASSERT(r == 0);
  113. r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);
  114. ASSERT(r == 0);
  115. MAKE_VALGRIND_HAPPY();
  116. return 0;
  117. }