test-tcp-create-socket-early.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* Copyright (c) 2015 Saúl Ibarra Corretgé <saghul@gmail.com>.
  2. * All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. #include "uv.h"
  23. #include "task.h"
  24. #include <string.h>
  25. #ifdef _WIN32
  26. # define INVALID_FD (INVALID_HANDLE_VALUE)
  27. #else
  28. # define INVALID_FD (-1)
  29. #endif
  30. static void on_connect(uv_connect_t* req, int status) {
  31. ASSERT(status == 0);
  32. uv_close((uv_handle_t*) req->handle, NULL);
  33. }
  34. static void on_connection(uv_stream_t* server, int status) {
  35. uv_tcp_t* handle;
  36. int r;
  37. ASSERT(status == 0);
  38. handle = malloc(sizeof(*handle));
  39. ASSERT(handle != NULL);
  40. r = uv_tcp_init_ex(server->loop, handle, AF_INET);
  41. ASSERT(r == 0);
  42. r = uv_accept(server, (uv_stream_t*)handle);
  43. ASSERT(r == UV_EBUSY);
  44. uv_close((uv_handle_t*) server, NULL);
  45. uv_close((uv_handle_t*) handle, (uv_close_cb)free);
  46. }
  47. static void tcp_listener(uv_loop_t* loop, uv_tcp_t* server) {
  48. struct sockaddr_in addr;
  49. int r;
  50. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
  51. r = uv_tcp_init(loop, server);
  52. ASSERT(r == 0);
  53. r = uv_tcp_bind(server, (const struct sockaddr*) &addr, 0);
  54. ASSERT(r == 0);
  55. r = uv_listen((uv_stream_t*) server, 128, on_connection);
  56. ASSERT(r == 0);
  57. }
  58. static void tcp_connector(uv_loop_t* loop, uv_tcp_t* client, uv_connect_t* req) {
  59. struct sockaddr_in server_addr;
  60. int r;
  61. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr));
  62. r = uv_tcp_init(loop, client);
  63. ASSERT(r == 0);
  64. r = uv_tcp_connect(req,
  65. client,
  66. (const struct sockaddr*) &server_addr,
  67. on_connect);
  68. ASSERT(r == 0);
  69. }
  70. TEST_IMPL(tcp_create_early) {
  71. struct sockaddr_in addr;
  72. struct sockaddr_in sockname;
  73. uv_tcp_t client;
  74. uv_os_fd_t fd;
  75. int r, namelen;
  76. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  77. r = uv_tcp_init_ex(uv_default_loop(), &client, AF_INET);
  78. ASSERT(r == 0);
  79. r = uv_fileno((const uv_handle_t*) &client, &fd);
  80. ASSERT(r == 0);
  81. ASSERT(fd != INVALID_FD);
  82. /* Windows returns WSAEINVAL if the socket is not bound */
  83. #ifndef _WIN32
  84. namelen = sizeof sockname;
  85. r = uv_tcp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
  86. ASSERT(r == 0);
  87. ASSERT(sockname.sin_family == AF_INET);
  88. #endif
  89. r = uv_tcp_bind(&client, (const struct sockaddr*) &addr, 0);
  90. ASSERT(r == 0);
  91. namelen = sizeof sockname;
  92. r = uv_tcp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
  93. ASSERT(r == 0);
  94. ASSERT(memcmp(&addr.sin_addr,
  95. &sockname.sin_addr,
  96. sizeof(addr.sin_addr)) == 0);
  97. uv_close((uv_handle_t*) &client, NULL);
  98. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  99. MAKE_VALGRIND_HAPPY();
  100. return 0;
  101. }
  102. TEST_IMPL(tcp_create_early_bad_bind) {
  103. struct sockaddr_in addr;
  104. uv_tcp_t client;
  105. uv_os_fd_t fd;
  106. int r;
  107. if (!can_ipv6())
  108. RETURN_SKIP("IPv6 not supported");
  109. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  110. r = uv_tcp_init_ex(uv_default_loop(), &client, AF_INET6);
  111. ASSERT(r == 0);
  112. r = uv_fileno((const uv_handle_t*) &client, &fd);
  113. ASSERT(r == 0);
  114. ASSERT(fd != INVALID_FD);
  115. /* Windows returns WSAEINVAL if the socket is not bound */
  116. #ifndef _WIN32
  117. {
  118. int namelen;
  119. struct sockaddr_in6 sockname;
  120. namelen = sizeof sockname;
  121. r = uv_tcp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
  122. ASSERT(r == 0);
  123. ASSERT(sockname.sin6_family == AF_INET6);
  124. }
  125. #endif
  126. r = uv_tcp_bind(&client, (const struct sockaddr*) &addr, 0);
  127. #if !defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MSYS__)
  128. ASSERT(r == UV_EINVAL);
  129. #else
  130. ASSERT(r == UV_EFAULT);
  131. #endif
  132. uv_close((uv_handle_t*) &client, NULL);
  133. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  134. MAKE_VALGRIND_HAPPY();
  135. return 0;
  136. }
  137. TEST_IMPL(tcp_create_early_bad_domain) {
  138. uv_tcp_t client;
  139. int r;
  140. r = uv_tcp_init_ex(uv_default_loop(), &client, 47);
  141. ASSERT(r == UV_EINVAL);
  142. r = uv_tcp_init_ex(uv_default_loop(), &client, 1024);
  143. ASSERT(r == UV_EINVAL);
  144. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  145. MAKE_VALGRIND_HAPPY();
  146. return 0;
  147. }
  148. TEST_IMPL(tcp_create_early_accept) {
  149. uv_tcp_t client, server;
  150. uv_connect_t connect_req;
  151. tcp_listener(uv_default_loop(), &server);
  152. tcp_connector(uv_default_loop(), &client, &connect_req);
  153. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  154. MAKE_VALGRIND_HAPPY();
  155. return 0;
  156. }