xref: /libuv/test/test-pipe-getsockname.c (revision 1eac3310)
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 
22 #include "uv.h"
23 #include "task.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #ifndef _WIN32
29 # include <unistd.h>  /* close */
30 #else
31 # include <fcntl.h>
32 #endif
33 
34 static uv_pipe_t pipe_client;
35 static uv_pipe_t pipe_server;
36 static uv_connect_t connect_req;
37 
38 static int pipe_close_cb_called = 0;
39 static int pipe_client_connect_cb_called = 0;
40 
41 
pipe_close_cb(uv_handle_t * handle)42 static void pipe_close_cb(uv_handle_t* handle) {
43   ASSERT_NE(handle == (uv_handle_t*) &pipe_client ||
44             handle == (uv_handle_t*) &pipe_server, 0);
45   pipe_close_cb_called++;
46 }
47 
48 
pipe_client_connect_cb(uv_connect_t * req,int status)49 static void pipe_client_connect_cb(uv_connect_t* req, int status) {
50   char buf[1024];
51   size_t len;
52   int r;
53 
54   ASSERT_PTR_EQ(req, &connect_req);
55   ASSERT_OK(status);
56 
57   len = sizeof buf;
58   r = uv_pipe_getpeername(&pipe_client, buf, &len);
59   ASSERT_OK(r);
60 
61   if (*buf == '\0') {  /* Linux abstract socket. */
62     const char expected[] = "\0" TEST_PIPENAME;
63     ASSERT_EQ(len, sizeof(expected) - 1);
64     ASSERT_MEM_EQ(buf, expected, len);
65   } else {
66     ASSERT_NE(0, buf[len - 1]);
67     ASSERT_MEM_EQ(buf, TEST_PIPENAME, len);
68   }
69 
70   len = sizeof buf;
71   r = uv_pipe_getsockname(&pipe_client, buf, &len);
72   ASSERT(r == 0 && len == 0);
73 
74   pipe_client_connect_cb_called++;
75 
76   uv_close((uv_handle_t*) &pipe_client, pipe_close_cb);
77   uv_close((uv_handle_t*) &pipe_server, pipe_close_cb);
78 }
79 
80 
81 #if defined(__linux__)
82 /* Socket name looks like \0[0-9a-f]{5}, e.g. "\0bad42" */
check_is_autobind_abstract_socket_name(const char * p,size_t len)83 static void check_is_autobind_abstract_socket_name(const char *p, size_t len) {
84   ASSERT_EQ(len, 6);
85   ASSERT_EQ(*p, '\0');
86 
87   while (*p != '\0') {
88     ASSERT((*p >= '0' && *p <= '9') || (*p >= 'a' && *p <= 'f'));
89     p++;
90   }
91 }
92 
93 
pipe_client_autobind_connect_cb(uv_connect_t * req,int status)94 static void pipe_client_autobind_connect_cb(uv_connect_t* req, int status) {
95   char buf[16];
96   size_t len;
97 
98   ASSERT_OK(status);
99   len = 5;
100   ASSERT_EQ(UV_ENOBUFS, uv_pipe_getpeername(&pipe_client, buf, &len));
101   len = 6;
102   ASSERT_OK(uv_pipe_getpeername(&pipe_client, buf, &len));
103   check_is_autobind_abstract_socket_name(buf, len);
104   pipe_client_connect_cb_called++;
105   uv_close((uv_handle_t*) &pipe_client, pipe_close_cb);
106   uv_close((uv_handle_t*) &pipe_server, pipe_close_cb);
107 }
108 #endif  /* defined(__linux__) */
109 
110 
pipe_server_connection_cb(uv_stream_t * handle,int status)111 static void pipe_server_connection_cb(uv_stream_t* handle, int status) {
112   /* This function *may* be called, depending on whether accept or the
113    * connection callback is called first.
114    */
115   ASSERT_OK(status);
116 }
117 
118 
TEST_IMPL(pipe_getsockname)119 TEST_IMPL(pipe_getsockname) {
120 #if defined(NO_SELF_CONNECT)
121   RETURN_SKIP(NO_SELF_CONNECT);
122 #endif
123   uv_loop_t* loop;
124   char namebuf[256];
125   char buf[1024];
126   size_t namelen;
127   size_t len;
128   int r;
129 
130   snprintf(namebuf, sizeof(namebuf), "%s-oob", TEST_PIPENAME);
131   namelen = sizeof(TEST_PIPENAME) - 1;
132 
133   loop = uv_default_loop();
134   ASSERT_NOT_NULL(loop);
135 
136   r = uv_pipe_init(loop, &pipe_server, 0);
137   ASSERT_OK(r);
138 
139   r = uv_pipe_bind2(&pipe_server, "bad\0path", 8, 0);
140   ASSERT_EQ(r, UV_EINVAL);
141 
142   len = sizeof buf;
143   r = uv_pipe_getsockname(&pipe_server, buf, &len);
144   ASSERT_EQ(r, UV_EBADF);
145 
146   len = sizeof buf;
147   r = uv_pipe_getpeername(&pipe_server, buf, &len);
148   ASSERT_EQ(r, UV_EBADF);
149 
150   r = uv_pipe_bind2(&pipe_server, namebuf, namelen, 0);
151   ASSERT_OK(r);
152 
153 #ifndef _WIN32
154   ASSERT_STR_EQ(pipe_server.pipe_fname, TEST_PIPENAME);
155 #endif
156 
157   len = sizeof(TEST_PIPENAME) - 1;
158   ASSERT_EQ(UV_ENOBUFS, uv_pipe_getsockname(&pipe_server, buf, &len));
159 
160   len = sizeof(TEST_PIPENAME);
161   ASSERT_OK(uv_pipe_getsockname(&pipe_server, buf, &len));
162 
163   ASSERT_NE(0, buf[len - 1]);
164   ASSERT_EQ(buf[len], '\0');
165   ASSERT_OK(memcmp(buf, TEST_PIPENAME, len));
166 
167   len = sizeof buf;
168   r = uv_pipe_getpeername(&pipe_server, buf, &len);
169   ASSERT_EQ(r, UV_ENOTCONN);
170 
171   r = uv_listen((uv_stream_t*) &pipe_server, 0, pipe_server_connection_cb);
172   ASSERT_OK(r);
173 
174   r = uv_pipe_init(loop, &pipe_client, 0);
175   ASSERT_OK(r);
176 
177   len = sizeof buf;
178   r = uv_pipe_getsockname(&pipe_client, buf, &len);
179   ASSERT_EQ(r, UV_EBADF);
180 
181   len = sizeof buf;
182   r = uv_pipe_getpeername(&pipe_client, buf, &len);
183   ASSERT_EQ(r, UV_EBADF);
184 
185   r = uv_pipe_connect2(&connect_req,
186                        &pipe_client,
187                        namebuf,
188                        namelen,
189                        0,
190                        pipe_client_connect_cb);
191   ASSERT_OK(r);
192 
193   len = sizeof buf;
194   r = uv_pipe_getsockname(&pipe_client, buf, &len);
195   ASSERT_EQ(r, 0);
196   ASSERT_EQ(len, 0);
197 
198   len = sizeof buf;
199   r = uv_pipe_getpeername(&pipe_client, buf, &len);
200   ASSERT_OK(r);
201 
202   ASSERT_NE(0, buf[len - 1]);
203   ASSERT_OK(memcmp(buf, TEST_PIPENAME, len));
204 
205   r = uv_run(loop, UV_RUN_DEFAULT);
206   ASSERT_OK(r);
207   ASSERT_EQ(1, pipe_client_connect_cb_called);
208   ASSERT_EQ(2, pipe_close_cb_called);
209 
210   MAKE_VALGRIND_HAPPY(loop);
211   return 0;
212 }
213 
214 
TEST_IMPL(pipe_getsockname_abstract)215 TEST_IMPL(pipe_getsockname_abstract) {
216   /* TODO(bnoordhuis) Use unique name, susceptible to concurrent test runs. */
217   static const char name[] = "\0" TEST_PIPENAME;
218 #if defined(__linux__)
219   char buf[256];
220   size_t buflen;
221 
222   buflen = sizeof(buf);
223   memset(buf, 0, sizeof(buf));
224   ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_server, 0));
225   ASSERT_OK(uv_pipe_bind2(&pipe_server, name, sizeof(name) - 1, 0));
226   ASSERT_OK(uv_pipe_getsockname(&pipe_server, buf, &buflen));
227   ASSERT_UINT64_EQ(sizeof(name) - 1, buflen);
228   ASSERT_MEM_EQ(name, buf, buflen);
229   ASSERT_OK(uv_listen((uv_stream_t*) &pipe_server,
230                       0,
231                       pipe_server_connection_cb));
232   ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_client, 0));
233   ASSERT_OK(uv_pipe_connect2(&connect_req,
234                              &pipe_client,
235                              name,
236                              sizeof(name) - 1,
237                              0,
238                              pipe_client_connect_cb));
239   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
240   ASSERT_EQ(1, pipe_client_connect_cb_called);
241   ASSERT_EQ(2, pipe_close_cb_called);
242   MAKE_VALGRIND_HAPPY(uv_default_loop());
243   return 0;
244 #else
245   /* On other platforms it should simply fail with UV_EINVAL. */
246   ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_server, 0));
247   ASSERT_EQ(UV_EINVAL, uv_pipe_bind2(&pipe_server, name, sizeof(name), 0));
248   ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_client, 0));
249   uv_close((uv_handle_t*) &pipe_server, pipe_close_cb);
250   ASSERT_EQ(UV_EINVAL, uv_pipe_connect2(&connect_req,
251                                         &pipe_client,
252                                         name,
253                                         sizeof(name),
254                                         0,
255                                         (uv_connect_cb) abort));
256   uv_close((uv_handle_t*) &pipe_client, pipe_close_cb);
257   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
258   ASSERT_EQ(2, pipe_close_cb_called);
259   MAKE_VALGRIND_HAPPY(uv_default_loop());
260   return 0;
261 #endif
262 }
263 
264 
TEST_IMPL(pipe_getsockname_autobind)265 TEST_IMPL(pipe_getsockname_autobind) {
266 #if defined(__linux__)
267   char buf[256];
268   size_t buflen;
269 
270   buflen = sizeof(buf);
271   memset(buf, 0, sizeof(buf));
272   ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_server, 0));
273   ASSERT_OK(uv_pipe_bind2(&pipe_server, "", 0, 0));
274   ASSERT_OK(uv_pipe_getsockname(&pipe_server, buf, &buflen));
275   check_is_autobind_abstract_socket_name(buf, buflen);
276   ASSERT_OK(uv_listen((uv_stream_t*) &pipe_server, 0,
277                       pipe_server_connection_cb));
278   ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_client, 0));
279   ASSERT_OK(uv_pipe_connect2(&connect_req, &pipe_client,
280                              buf,
281                              1 + strlen(&buf[1]),
282                              0,
283                              pipe_client_autobind_connect_cb));
284   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
285   ASSERT_EQ(1, pipe_client_connect_cb_called);
286   ASSERT_EQ(2, pipe_close_cb_called);
287   MAKE_VALGRIND_HAPPY(uv_default_loop());
288   return 0;
289 #else
290   /* On other platforms it should simply fail with UV_EINVAL. */
291   ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_server, 0));
292   ASSERT_EQ(UV_EINVAL, uv_pipe_bind2(&pipe_server, "", 0, 0));
293   uv_close((uv_handle_t*) &pipe_server, pipe_close_cb);
294   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
295   ASSERT_EQ(1, pipe_close_cb_called);
296   MAKE_VALGRIND_HAPPY(uv_default_loop());
297   return 0;
298 #endif
299 }
300 
301 
TEST_IMPL(pipe_getsockname_blocking)302 TEST_IMPL(pipe_getsockname_blocking) {
303 #ifdef _WIN32
304   HANDLE readh, writeh;
305   int readfd;
306   char buf1[1024], buf2[1024];
307   size_t len1, len2;
308   int r;
309 
310   r = CreatePipe(&readh, &writeh, NULL, 65536);
311   ASSERT(r);
312 
313   r = uv_pipe_init(uv_default_loop(), &pipe_client, 0);
314   ASSERT_OK(r);
315   readfd = _open_osfhandle((intptr_t)readh, _O_RDONLY);
316   ASSERT_NE(r, -1);
317   r = uv_pipe_open(&pipe_client, readfd);
318   ASSERT_OK(r);
319   r = uv_read_start((uv_stream_t*) &pipe_client,
320                     (uv_alloc_cb) abort,
321                     (uv_read_cb) abort);
322   ASSERT_OK(r);
323   Sleep(100);
324   r = uv_read_stop((uv_stream_t*)&pipe_client);
325   ASSERT_OK(r);
326 
327   len1 = sizeof buf1;
328   r = uv_pipe_getsockname(&pipe_client, buf1, &len1);
329   ASSERT_OK(r);
330   ASSERT_OK(len1);  /* It's an annonymous pipe. */
331 
332   r = uv_read_start((uv_stream_t*)&pipe_client,
333                     (uv_alloc_cb) abort,
334                     (uv_read_cb) abort);
335   ASSERT_OK(r);
336   Sleep(100);
337 
338   len2 = sizeof buf2;
339   r = uv_pipe_getsockname(&pipe_client, buf2, &len2);
340   ASSERT_OK(r);
341   ASSERT_OK(len2);  /* It's an annonymous pipe. */
342 
343   r = uv_read_stop((uv_stream_t*)&pipe_client);
344   ASSERT_OK(r);
345 
346   ASSERT_EQ(len1, len2);
347   ASSERT_OK(memcmp(buf1, buf2, len1));
348 
349   pipe_close_cb_called = 0;
350   uv_close((uv_handle_t*)&pipe_client, pipe_close_cb);
351 
352   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
353 
354   ASSERT_EQ(1, pipe_close_cb_called);
355 
356   CloseHandle(writeh);
357 #endif
358 
359   MAKE_VALGRIND_HAPPY(uv_default_loop());
360   return 0;
361 }
362