test-tcp-bind6-error.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 = uv_ip6_addr("::", 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_bind6(&server1, addr);
  37. ASSERT(r == 0);
  38. r = uv_tcp_init(uv_default_loop(), &server2);
  39. ASSERT(r == 0);
  40. r = uv_tcp_bind6(&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_bind6_error_addrnotavail) {
  55. struct sockaddr_in6 addr = uv_ip6_addr("4:4:4:4:4:4:4:4", 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_bind6(&server, addr);
  61. ASSERT(r == -1);
  62. ASSERT(uv_last_error(uv_default_loop()).code == 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[] = "blah blah blah blah blah blah blah blah blah blah blah blah";
  71. struct sockaddr_in6* garbage_addr;
  72. uv_tcp_t server;
  73. int r;
  74. garbage_addr = (struct sockaddr_in6*) &garbage;
  75. r = uv_tcp_init(uv_default_loop(), &server);
  76. ASSERT(r == 0);
  77. r = uv_tcp_bind6(&server, *garbage_addr);
  78. ASSERT(r == -1);
  79. ASSERT(uv_last_error(uv_default_loop()).code == 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 = uv_ip6_addr("::", TEST_PORT);
  89. struct sockaddr_in6 addr2 = uv_ip6_addr("::", TEST_PORT_2);
  90. uv_tcp_t server;
  91. int r;
  92. r = uv_tcp_init(uv_default_loop(), &server);
  93. ASSERT(r == 0);
  94. r = uv_tcp_bind6(&server, addr1);
  95. ASSERT(r == 0);
  96. r = uv_tcp_bind6(&server, addr2);
  97. ASSERT(r == -1);
  98. ASSERT(uv_last_error(uv_default_loop()).code == UV_EINVAL);
  99. uv_close((uv_handle_t*)&server, close_cb);
  100. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  101. ASSERT(close_cb_called == 1);
  102. MAKE_VALGRIND_HAPPY();
  103. return 0;
  104. }
  105. TEST_IMPL(tcp_bind6_localhost_ok) {
  106. struct sockaddr_in6 addr = uv_ip6_addr("::1", TEST_PORT);
  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_bind6(&server, addr);
  112. ASSERT(r == 0);
  113. MAKE_VALGRIND_HAPPY();
  114. return 0;
  115. }