test-pipe-getsockname.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #include <string.h>
  26. #if defined(__linux__)
  27. #include <sys/socket.h>
  28. #include <sys/un.h>
  29. #endif
  30. #ifndef _WIN32
  31. # include <unistd.h> /* close */
  32. #else
  33. # include <fcntl.h>
  34. #endif
  35. static uv_pipe_t pipe_client;
  36. static uv_pipe_t pipe_server;
  37. static uv_connect_t connect_req;
  38. static int pipe_close_cb_called = 0;
  39. static int pipe_client_connect_cb_called = 0;
  40. static void pipe_close_cb(uv_handle_t* handle) {
  41. ASSERT(handle == (uv_handle_t*) &pipe_client ||
  42. handle == (uv_handle_t*) &pipe_server);
  43. pipe_close_cb_called++;
  44. }
  45. static void pipe_client_connect_cb(uv_connect_t* req, int status) {
  46. char buf[1024];
  47. size_t len;
  48. int r;
  49. ASSERT(req == &connect_req);
  50. ASSERT(status == 0);
  51. len = sizeof buf;
  52. r = uv_pipe_getpeername(&pipe_client, buf, &len);
  53. ASSERT(r == 0);
  54. ASSERT(buf[len - 1] != 0);
  55. ASSERT(memcmp(buf, TEST_PIPENAME, len) == 0);
  56. len = sizeof buf;
  57. r = uv_pipe_getsockname(&pipe_client, buf, &len);
  58. ASSERT(r == 0 && len == 0);
  59. pipe_client_connect_cb_called++;
  60. uv_close((uv_handle_t*) &pipe_client, pipe_close_cb);
  61. uv_close((uv_handle_t*) &pipe_server, pipe_close_cb);
  62. }
  63. static void pipe_server_connection_cb(uv_stream_t* handle, int status) {
  64. /* This function *may* be called, depending on whether accept or the
  65. * connection callback is called first.
  66. */
  67. ASSERT(status == 0);
  68. }
  69. TEST_IMPL(pipe_getsockname) {
  70. #if defined(NO_SELF_CONNECT)
  71. RETURN_SKIP(NO_SELF_CONNECT);
  72. #endif
  73. uv_loop_t* loop;
  74. char buf[1024];
  75. size_t len;
  76. int r;
  77. loop = uv_default_loop();
  78. ASSERT(loop != NULL);
  79. r = uv_pipe_init(loop, &pipe_server, 0);
  80. ASSERT(r == 0);
  81. len = sizeof buf;
  82. r = uv_pipe_getsockname(&pipe_server, buf, &len);
  83. ASSERT(r == UV_EBADF);
  84. len = sizeof buf;
  85. r = uv_pipe_getpeername(&pipe_server, buf, &len);
  86. ASSERT(r == UV_EBADF);
  87. r = uv_pipe_bind(&pipe_server, TEST_PIPENAME);
  88. ASSERT(r == 0);
  89. len = sizeof buf;
  90. r = uv_pipe_getsockname(&pipe_server, buf, &len);
  91. ASSERT(r == 0);
  92. ASSERT(buf[len - 1] != 0);
  93. ASSERT(buf[len] == '\0');
  94. ASSERT(memcmp(buf, TEST_PIPENAME, len) == 0);
  95. len = sizeof buf;
  96. r = uv_pipe_getpeername(&pipe_server, buf, &len);
  97. ASSERT(r == UV_ENOTCONN);
  98. r = uv_listen((uv_stream_t*) &pipe_server, 0, pipe_server_connection_cb);
  99. ASSERT(r == 0);
  100. r = uv_pipe_init(loop, &pipe_client, 0);
  101. ASSERT(r == 0);
  102. len = sizeof buf;
  103. r = uv_pipe_getsockname(&pipe_client, buf, &len);
  104. ASSERT(r == UV_EBADF);
  105. len = sizeof buf;
  106. r = uv_pipe_getpeername(&pipe_client, buf, &len);
  107. ASSERT(r == UV_EBADF);
  108. uv_pipe_connect(&connect_req, &pipe_client, TEST_PIPENAME, pipe_client_connect_cb);
  109. len = sizeof buf;
  110. r = uv_pipe_getsockname(&pipe_client, buf, &len);
  111. ASSERT(r == 0 && len == 0);
  112. len = sizeof buf;
  113. r = uv_pipe_getpeername(&pipe_client, buf, &len);
  114. ASSERT(r == 0);
  115. ASSERT(buf[len - 1] != 0);
  116. ASSERT(memcmp(buf, TEST_PIPENAME, len) == 0);
  117. r = uv_run(loop, UV_RUN_DEFAULT);
  118. ASSERT(r == 0);
  119. ASSERT(pipe_client_connect_cb_called == 1);
  120. ASSERT(pipe_close_cb_called == 2);
  121. MAKE_VALGRIND_HAPPY();
  122. return 0;
  123. }
  124. TEST_IMPL(pipe_getsockname_abstract) {
  125. #if defined(__linux__)
  126. char buf[1024];
  127. size_t len;
  128. int r;
  129. int sock;
  130. struct sockaddr_un sun;
  131. socklen_t sun_len;
  132. char abstract_pipe[] = "\0test-pipe";
  133. sock = socket(AF_UNIX, SOCK_STREAM, 0);
  134. ASSERT(sock != -1);
  135. sun_len = sizeof sun;
  136. memset(&sun, 0, sun_len);
  137. sun.sun_family = AF_UNIX;
  138. memcpy(sun.sun_path, abstract_pipe, sizeof abstract_pipe);
  139. r = bind(sock, (struct sockaddr*)&sun, sun_len);
  140. ASSERT(r == 0);
  141. r = uv_pipe_init(uv_default_loop(), &pipe_server, 0);
  142. ASSERT(r == 0);
  143. r = uv_pipe_open(&pipe_server, sock);
  144. ASSERT(r == 0);
  145. len = sizeof buf;
  146. r = uv_pipe_getsockname(&pipe_server, buf, &len);
  147. ASSERT(r == 0);
  148. ASSERT(memcmp(buf, abstract_pipe, sizeof abstract_pipe) == 0);
  149. uv_close((uv_handle_t*)&pipe_server, pipe_close_cb);
  150. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  151. close(sock);
  152. ASSERT(pipe_close_cb_called == 1);
  153. MAKE_VALGRIND_HAPPY();
  154. return 0;
  155. #else
  156. MAKE_VALGRIND_HAPPY();
  157. return 0;
  158. #endif
  159. }
  160. TEST_IMPL(pipe_getsockname_blocking) {
  161. #ifdef _WIN32
  162. HANDLE readh, writeh;
  163. int readfd;
  164. char buf1[1024], buf2[1024];
  165. size_t len1, len2;
  166. int r;
  167. r = CreatePipe(&readh, &writeh, NULL, 65536);
  168. ASSERT(r != 0);
  169. r = uv_pipe_init(uv_default_loop(), &pipe_client, 0);
  170. ASSERT(r == 0);
  171. readfd = _open_osfhandle((intptr_t)readh, _O_RDONLY);
  172. ASSERT(r != -1);
  173. r = uv_pipe_open(&pipe_client, readfd);
  174. ASSERT(r == 0);
  175. r = uv_read_start((uv_stream_t*)&pipe_client, NULL, NULL);
  176. ASSERT(r == 0);
  177. Sleep(100);
  178. r = uv_read_stop((uv_stream_t*)&pipe_client);
  179. ASSERT(r == 0);
  180. len1 = sizeof buf1;
  181. r = uv_pipe_getsockname(&pipe_client, buf1, &len1);
  182. ASSERT(r == 0);
  183. ASSERT(len1 == 0); /* It's an annonymous pipe. */
  184. r = uv_read_start((uv_stream_t*)&pipe_client, NULL, NULL);
  185. ASSERT(r == 0);
  186. Sleep(100);
  187. len2 = sizeof buf2;
  188. r = uv_pipe_getsockname(&pipe_client, buf2, &len2);
  189. ASSERT(r == 0);
  190. ASSERT(len2 == 0); /* It's an annonymous pipe. */
  191. r = uv_read_stop((uv_stream_t*)&pipe_client);
  192. ASSERT(r == 0);
  193. ASSERT(len1 == len2);
  194. ASSERT(memcmp(buf1, buf2, len1) == 0);
  195. pipe_close_cb_called = 0;
  196. uv_close((uv_handle_t*)&pipe_client, pipe_close_cb);
  197. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  198. ASSERT(pipe_close_cb_called == 1);
  199. CloseHandle(writeh);
  200. #endif
  201. MAKE_VALGRIND_HAPPY();
  202. return 0;
  203. }