test-pipe-getsockname.c
Go to the documentation of this file.
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 #if defined(__linux__)
29  #include <sys/socket.h>
30  #include <sys/un.h>
31 #endif
32 
33 #ifndef _WIN32
34 # include <unistd.h> /* close */
35 #else
36 # include <fcntl.h>
37 #endif
38 
42 
43 static int pipe_close_cb_called = 0;
45 
46 
51 }
52 
53 
55  char buf[1024];
56  size_t len;
57  int r;
58 
59  ASSERT(req == &connect_req);
60  ASSERT(status == 0);
61 
62  len = sizeof buf;
64  ASSERT(r == 0);
65 
66  ASSERT(buf[len - 1] != 0);
67  ASSERT(memcmp(buf, TEST_PIPENAME, len) == 0);
68 
69  len = sizeof buf;
71  ASSERT(r == 0 && len == 0);
72 
74 
75 
78 }
79 
80 
82  /* This function *may* be called, depending on whether accept or the
83  * connection callback is called first.
84  */
85  ASSERT(status == 0);
86 }
87 
88 
89 TEST_IMPL(pipe_getsockname) {
90 #if defined(NO_SELF_CONNECT)
91  RETURN_SKIP(NO_SELF_CONNECT);
92 #endif
93  uv_loop_t* loop;
94  char buf[1024];
95  size_t len;
96  int r;
97 
99  ASSERT(loop != NULL);
100 
101  r = uv_pipe_init(loop, &pipe_server, 0);
102  ASSERT(r == 0);
103 
104  len = sizeof buf;
106  ASSERT(r == UV_EBADF);
107 
108  len = sizeof buf;
110  ASSERT(r == UV_EBADF);
111 
113  ASSERT(r == 0);
114 
115  len = sizeof buf;
117  ASSERT(r == 0);
118 
119  ASSERT(buf[len - 1] != 0);
120  ASSERT(buf[len] == '\0');
121  ASSERT(memcmp(buf, TEST_PIPENAME, len) == 0);
122 
123  len = sizeof buf;
125  ASSERT(r == UV_ENOTCONN);
126 
128  ASSERT(r == 0);
129 
130  r = uv_pipe_init(loop, &pipe_client, 0);
131  ASSERT(r == 0);
132 
133  len = sizeof buf;
135  ASSERT(r == UV_EBADF);
136 
137  len = sizeof buf;
139  ASSERT(r == UV_EBADF);
140 
142 
143  len = sizeof buf;
145  ASSERT(r == 0 && len == 0);
146 
147  len = sizeof buf;
149  ASSERT(r == 0);
150 
151  ASSERT(buf[len - 1] != 0);
152  ASSERT(memcmp(buf, TEST_PIPENAME, len) == 0);
153 
155  ASSERT(r == 0);
158 
160  return 0;
161 }
162 
163 
164 TEST_IMPL(pipe_getsockname_abstract) {
165 #if defined(__linux__)
166  char buf[1024];
167  size_t len;
168  int r;
169  int sock;
170  struct sockaddr_un sun;
171  socklen_t sun_len;
172  char abstract_pipe[] = "\0test-pipe";
173 
174  sock = socket(AF_UNIX, SOCK_STREAM, 0);
175  ASSERT(sock != -1);
176 
177  sun_len = sizeof sun;
178  memset(&sun, 0, sun_len);
179  sun.sun_family = AF_UNIX;
180  memcpy(sun.sun_path, abstract_pipe, sizeof abstract_pipe);
181 
182  r = bind(sock, (struct sockaddr*)&sun, sun_len);
183  ASSERT(r == 0);
184 
186  ASSERT(r == 0);
187  r = uv_pipe_open(&pipe_server, sock);
188  ASSERT(r == 0);
189 
190  len = sizeof buf;
192  ASSERT(r == 0);
193 
194  ASSERT(memcmp(buf, abstract_pipe, sizeof abstract_pipe) == 0);
195 
197 
199 
200  close(sock);
201 
204  return 0;
205 #else
207  return 0;
208 #endif
209 }
210 
211 TEST_IMPL(pipe_getsockname_blocking) {
212 #ifdef _WIN32
213  HANDLE readh, writeh;
214  int readfd;
215  char buf1[1024], buf2[1024];
216  size_t len1, len2;
217  int r;
218 
219  r = CreatePipe(&readh, &writeh, NULL, 65536);
220  ASSERT(r != 0);
221 
223  ASSERT(r == 0);
224  readfd = _open_osfhandle((intptr_t)readh, _O_RDONLY);
225  ASSERT(r != -1);
226  r = uv_pipe_open(&pipe_client, readfd);
227  ASSERT(r == 0);
228  r = uv_read_start((uv_stream_t*)&pipe_client, NULL, NULL);
229  ASSERT(r == 0);
230  Sleep(100);
232  ASSERT(r == 0);
233 
234  len1 = sizeof buf1;
235  r = uv_pipe_getsockname(&pipe_client, buf1, &len1);
236  ASSERT(r == 0);
237  ASSERT(len1 == 0); /* It's an annonymous pipe. */
238 
239  r = uv_read_start((uv_stream_t*)&pipe_client, NULL, NULL);
240  ASSERT(r == 0);
241  Sleep(100);
242 
243  len2 = sizeof buf2;
245  ASSERT(r == 0);
246  ASSERT(len2 == 0); /* It's an annonymous pipe. */
247 
249  ASSERT(r == 0);
250 
251  ASSERT(len1 == len2);
252  ASSERT(memcmp(buf1, buf2, len1) == 0);
253 
256 
258 
260 
261  CloseHandle(writeh);
262 #endif
263 
265  return 0;
266 }
connect_req
static uv_connect_t connect_req
Definition: test-pipe-getsockname.c:41
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
task.h
uv_pipe_connect
UV_EXTERN void uv_pipe_connect(uv_connect_t *req, uv_pipe_t *handle, const char *name, uv_connect_cb cb)
Definition: unix/pipe.c:173
memset
return memset(p, 0, total)
uv_pipe_init
UV_EXTERN int uv_pipe_init(uv_loop_t *, uv_pipe_t *handle, int ipc)
Definition: unix/pipe.c:33
uv_connect_s
Definition: uv.h:580
pipe_client_connect_cb
static void pipe_client_connect_cb(uv_connect_t *req, int status)
Definition: test-pipe-getsockname.c:54
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
uv_listen
UV_EXTERN int uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb)
Definition: unix/stream.c:656
uv_pipe_getpeername
UV_EXTERN int uv_pipe_getpeername(const uv_pipe_t *handle, char *buffer, size_t *size)
Definition: unix/pipe.c:289
ASSERT
#define ASSERT(expr)
Definition: task.h:102
status
absl::Status status
Definition: rls.cc:251
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
TEST_IMPL
TEST_IMPL(pipe_getsockname)
Definition: test-pipe-getsockname.c:89
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
pipe_server_connection_cb
static void pipe_server_connection_cb(uv_stream_t *handle, int status)
Definition: test-pipe-getsockname.c:81
uv_stream_s
Definition: uv.h:491
pipe_close_cb_called
static int pipe_close_cb_called
Definition: test-pipe-getsockname.c:43
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
req
static uv_connect_t req
Definition: test-connection-fail.c:30
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_read_start
UV_EXTERN int uv_read_start(uv_stream_t *, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: unix/stream.c:1555
pipe_client
static uv_pipe_t pipe_client
Definition: test-pipe-getsockname.c:39
close
#define close
Definition: test-fs.c:48
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
pipe_client_connect_cb_called
static int pipe_client_connect_cb_called
Definition: test-pipe-getsockname.c:44
uv_pipe_getsockname
UV_EXTERN int uv_pipe_getsockname(const uv_pipe_t *handle, char *buffer, size_t *size)
Definition: unix/pipe.c:284
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
fix_build_deps.r
r
Definition: fix_build_deps.py:491
uv_pipe_s
Definition: uv.h:757
buf2
static char buf2[32]
Definition: test-fs.c:127
RETURN_SKIP
#define RETURN_SKIP(explanation)
Definition: task.h:262
pipe_server
static uv_pipe_t pipe_server
Definition: test-pipe-getsockname.c:40
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
TEST_PIPENAME
#define TEST_PIPENAME
Definition: task.h:61
test_server.socket
socket
Definition: test_server.py:65
uv_loop_s
Definition: uv.h:1767
pipe_close_cb
static void pipe_close_cb(uv_handle_t *handle)
Definition: test-pipe-getsockname.c:47
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
uv_pipe_bind
UV_EXTERN int uv_pipe_bind(uv_pipe_t *handle, const char *name)
Definition: unix/pipe.c:43
uv_read_stop
UV_EXTERN int uv_read_stop(uv_stream_t *)
Definition: unix/stream.c:1590
uv_pipe_open
UV_EXTERN int uv_pipe_open(uv_pipe_t *, uv_file file)
Definition: unix/pipe.c:137


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:26