xref: /libuv/test/test-pipe-getsockname.c (revision 31ea3411)
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   r = uv_pipe_getsockname(&pipe_server, NULL, &len);
158   ASSERT_EQ(r, UV_EINVAL);
159 
160   r = uv_pipe_getsockname(&pipe_server, buf, NULL);
161   ASSERT_EQ(r, UV_EINVAL);
162 
163   r = uv_pipe_getsockname(&pipe_server, NULL, NULL);
164   ASSERT_EQ(r, UV_EINVAL);
165 
166   len = sizeof(TEST_PIPENAME) - 1;
167   ASSERT_EQ(UV_ENOBUFS, uv_pipe_getsockname(&pipe_server, buf, &len));
168 
169   len = sizeof(TEST_PIPENAME);
170   ASSERT_OK(uv_pipe_getsockname(&pipe_server, buf, &len));
171 
172   ASSERT_NE(0, buf[len - 1]);
173   ASSERT_EQ(buf[len], '\0');
174   ASSERT_OK(memcmp(buf, TEST_PIPENAME, len));
175 
176   len = sizeof buf;
177   r = uv_pipe_getpeername(&pipe_server, buf, &len);
178   ASSERT_EQ(r, UV_ENOTCONN);
179 
180   r = uv_listen((uv_stream_t*) &pipe_server, 0, pipe_server_connection_cb);
181   ASSERT_OK(r);
182 
183   r = uv_pipe_init(loop, &pipe_client, 0);
184   ASSERT_OK(r);
185 
186   len = sizeof buf;
187   r = uv_pipe_getsockname(&pipe_client, buf, &len);
188   ASSERT_EQ(r, UV_EBADF);
189 
190   len = sizeof buf;
191   r = uv_pipe_getpeername(&pipe_client, buf, &len);
192   ASSERT_EQ(r, UV_EBADF);
193 
194   r = uv_pipe_connect2(&connect_req,
195                        &pipe_client,
196                        namebuf,
197                        namelen,
198                        0,
199                        pipe_client_connect_cb);
200   ASSERT_OK(r);
201 
202   len = sizeof buf;
203   r = uv_pipe_getsockname(&pipe_client, buf, &len);
204   ASSERT_EQ(r, 0);
205   ASSERT_EQ(len, 0);
206 
207   len = sizeof buf;
208   r = uv_pipe_getpeername(&pipe_client, buf, &len);
209   ASSERT_OK(r);
210 
211   ASSERT_NE(0, buf[len - 1]);
212   ASSERT_OK(memcmp(buf, TEST_PIPENAME, len));
213 
214   r = uv_run(loop, UV_RUN_DEFAULT);
215   ASSERT_OK(r);
216   ASSERT_EQ(1, pipe_client_connect_cb_called);
217   ASSERT_EQ(2, pipe_close_cb_called);
218 
219   MAKE_VALGRIND_HAPPY(loop);
220   return 0;
221 }
222 
223 
TEST_IMPL(pipe_getsockname_abstract)224 TEST_IMPL(pipe_getsockname_abstract) {
225   /* TODO(bnoordhuis) Use unique name, susceptible to concurrent test runs. */
226   static const char name[] = "\0" TEST_PIPENAME;
227 #if defined(__linux__)
228   char buf[256];
229   size_t buflen;
230 
231   buflen = sizeof(buf);
232   memset(buf, 0, sizeof(buf));
233   ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_server, 0));
234   ASSERT_OK(uv_pipe_bind2(&pipe_server, name, sizeof(name) - 1, 0));
235   ASSERT_OK(uv_pipe_getsockname(&pipe_server, buf, &buflen));
236   ASSERT_UINT64_EQ(sizeof(name) - 1, buflen);
237   ASSERT_MEM_EQ(name, buf, buflen);
238   ASSERT_OK(uv_listen((uv_stream_t*) &pipe_server,
239                       0,
240                       pipe_server_connection_cb));
241   ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_client, 0));
242   ASSERT_OK(uv_pipe_connect2(&connect_req,
243                              &pipe_client,
244                              name,
245                              sizeof(name) - 1,
246                              0,
247                              pipe_client_connect_cb));
248   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
249   ASSERT_EQ(1, pipe_client_connect_cb_called);
250   ASSERT_EQ(2, pipe_close_cb_called);
251   MAKE_VALGRIND_HAPPY(uv_default_loop());
252   return 0;
253 #else
254   /* On other platforms it should simply fail with UV_EINVAL. */
255   ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_server, 0));
256   ASSERT_EQ(UV_EINVAL, uv_pipe_bind2(&pipe_server, name, sizeof(name), 0));
257   ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_client, 0));
258   uv_close((uv_handle_t*) &pipe_server, pipe_close_cb);
259   ASSERT_EQ(UV_EINVAL, uv_pipe_connect2(&connect_req,
260                                         &pipe_client,
261                                         name,
262                                         sizeof(name),
263                                         0,
264                                         (uv_connect_cb) abort));
265   uv_close((uv_handle_t*) &pipe_client, pipe_close_cb);
266   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
267   ASSERT_EQ(2, pipe_close_cb_called);
268   MAKE_VALGRIND_HAPPY(uv_default_loop());
269   return 0;
270 #endif
271 }
272 
273 
TEST_IMPL(pipe_getsockname_autobind)274 TEST_IMPL(pipe_getsockname_autobind) {
275 #if defined(__linux__)
276   char buf[256];
277   size_t buflen;
278 
279   buflen = sizeof(buf);
280   memset(buf, 0, sizeof(buf));
281   ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_server, 0));
282   ASSERT_OK(uv_pipe_bind2(&pipe_server, "", 0, 0));
283   ASSERT_OK(uv_pipe_getsockname(&pipe_server, buf, &buflen));
284   check_is_autobind_abstract_socket_name(buf, buflen);
285   ASSERT_OK(uv_listen((uv_stream_t*) &pipe_server, 0,
286                       pipe_server_connection_cb));
287   ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_client, 0));
288   ASSERT_OK(uv_pipe_connect2(&connect_req, &pipe_client,
289                              buf,
290                              1 + strlen(&buf[1]),
291                              0,
292                              pipe_client_autobind_connect_cb));
293   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
294   ASSERT_EQ(1, pipe_client_connect_cb_called);
295   ASSERT_EQ(2, pipe_close_cb_called);
296   MAKE_VALGRIND_HAPPY(uv_default_loop());
297   return 0;
298 #else
299   /* On other platforms it should simply fail with UV_EINVAL. */
300   ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_server, 0));
301   ASSERT_EQ(UV_EINVAL, uv_pipe_bind2(&pipe_server, "", 0, 0));
302   uv_close((uv_handle_t*) &pipe_server, pipe_close_cb);
303   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
304   ASSERT_EQ(1, pipe_close_cb_called);
305   MAKE_VALGRIND_HAPPY(uv_default_loop());
306   return 0;
307 #endif
308 }
309 
310 
TEST_IMPL(pipe_getsockname_blocking)311 TEST_IMPL(pipe_getsockname_blocking) {
312 #ifdef _WIN32
313   HANDLE readh, writeh;
314   int readfd;
315   char buf1[1024], buf2[1024];
316   size_t len1, len2;
317   int r;
318 
319   r = CreatePipe(&readh, &writeh, NULL, 65536);
320   ASSERT(r);
321 
322   r = uv_pipe_init(uv_default_loop(), &pipe_client, 0);
323   ASSERT_OK(r);
324   readfd = _open_osfhandle((intptr_t)readh, _O_RDONLY);
325   ASSERT_NE(r, -1);
326   r = uv_pipe_open(&pipe_client, readfd);
327   ASSERT_OK(r);
328   r = uv_read_start((uv_stream_t*) &pipe_client,
329                     (uv_alloc_cb) abort,
330                     (uv_read_cb) abort);
331   ASSERT_OK(r);
332   Sleep(100);
333   r = uv_read_stop((uv_stream_t*)&pipe_client);
334   ASSERT_OK(r);
335 
336   len1 = sizeof buf1;
337   r = uv_pipe_getsockname(&pipe_client, buf1, &len1);
338   ASSERT_OK(r);
339   ASSERT_OK(len1);  /* It's an annonymous pipe. */
340 
341   r = uv_read_start((uv_stream_t*)&pipe_client,
342                     (uv_alloc_cb) abort,
343                     (uv_read_cb) abort);
344   ASSERT_OK(r);
345   Sleep(100);
346 
347   len2 = sizeof buf2;
348   r = uv_pipe_getsockname(&pipe_client, buf2, &len2);
349   ASSERT_OK(r);
350   ASSERT_OK(len2);  /* It's an annonymous pipe. */
351 
352   r = uv_read_stop((uv_stream_t*)&pipe_client);
353   ASSERT_OK(r);
354 
355   ASSERT_EQ(len1, len2);
356   ASSERT_OK(memcmp(buf1, buf2, len1));
357 
358   pipe_close_cb_called = 0;
359   uv_close((uv_handle_t*)&pipe_client, pipe_close_cb);
360 
361   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
362 
363   ASSERT_EQ(1, pipe_close_cb_called);
364 
365   CloseHandle(writeh);
366 #endif
367 
368   MAKE_VALGRIND_HAPPY(uv_default_loop());
369   return 0;
370 }
371