test-tcp-create-socket-early.c
Go to the documentation of this file.
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 
23 #include "uv.h"
24 #include "task.h"
25 #include <string.h>
26 
27 #ifdef _WIN32
28 # define INVALID_FD (INVALID_HANDLE_VALUE)
29 #else
30 # define INVALID_FD (-1)
31 #endif
32 
33 
34 static void on_connect(uv_connect_t* req, int status) {
35  ASSERT(status == 0);
36  uv_close((uv_handle_t*) req->handle, NULL);
37 }
38 
39 
40 static void on_connection(uv_stream_t* server, int status) {
42  int r;
43 
44  ASSERT(status == 0);
45 
46  handle = malloc(sizeof(*handle));
47  ASSERT(handle != NULL);
48 
49  r = uv_tcp_init_ex(server->loop, handle, AF_INET);
50  ASSERT(r == 0);
51 
53  ASSERT(r == UV_EBUSY);
54 
55  uv_close((uv_handle_t*) server, NULL);
57 }
58 
59 
61  struct sockaddr_in addr;
62  int r;
63 
64  ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
65 
67  ASSERT(r == 0);
68 
69  r = uv_tcp_bind(server, (const struct sockaddr*) &addr, 0);
70  ASSERT(r == 0);
71 
73  ASSERT(r == 0);
74 }
75 
76 
78  struct sockaddr_in server_addr;
79  int r;
80 
81  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr));
82 
84  ASSERT(r == 0);
85 
87  client,
88  (const struct sockaddr*) &server_addr,
89  on_connect);
90  ASSERT(r == 0);
91 }
92 
93 
94 TEST_IMPL(tcp_create_early) {
95  struct sockaddr_in addr;
96  struct sockaddr_in sockname;
98  uv_os_fd_t fd;
99  int r, namelen;
100 
101  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
102 
103  r = uv_tcp_init_ex(uv_default_loop(), &client, AF_INET);
104  ASSERT(r == 0);
105 
106  r = uv_fileno((const uv_handle_t*) &client, &fd);
107  ASSERT(r == 0);
108  ASSERT(fd != INVALID_FD);
109 
110  /* Windows returns WSAEINVAL if the socket is not bound */
111 #ifndef _WIN32
112  namelen = sizeof sockname;
113  r = uv_tcp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
114  ASSERT(r == 0);
115  ASSERT(sockname.sin_family == AF_INET);
116 #endif
117 
118  r = uv_tcp_bind(&client, (const struct sockaddr*) &addr, 0);
119  ASSERT(r == 0);
120 
121  namelen = sizeof sockname;
122  r = uv_tcp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
123  ASSERT(r == 0);
124  ASSERT(memcmp(&addr.sin_addr,
125  &sockname.sin_addr,
126  sizeof(addr.sin_addr)) == 0);
127 
128  uv_close((uv_handle_t*) &client, NULL);
130 
132  return 0;
133 }
134 
135 
136 TEST_IMPL(tcp_create_early_bad_bind) {
137  struct sockaddr_in addr;
139  uv_os_fd_t fd;
140  int r;
141 
142  if (!can_ipv6())
143  RETURN_SKIP("IPv6 not supported");
144 
145  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
146 
148  ASSERT(r == 0);
149 
150  r = uv_fileno((const uv_handle_t*) &client, &fd);
151  ASSERT(r == 0);
152  ASSERT(fd != INVALID_FD);
153 
154  /* Windows returns WSAEINVAL if the socket is not bound */
155 #ifndef _WIN32
156  {
157  int namelen;
158  struct sockaddr_in6 sockname;
159  namelen = sizeof sockname;
160  r = uv_tcp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
161  ASSERT(r == 0);
162  ASSERT(sockname.sin6_family == AF_INET6);
163  }
164 #endif
165 
166  r = uv_tcp_bind(&client, (const struct sockaddr*) &addr, 0);
167 #if !defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MSYS__)
168  ASSERT(r == UV_EINVAL);
169 #else
170  ASSERT(r == UV_EFAULT);
171 #endif
172 
173  uv_close((uv_handle_t*) &client, NULL);
175 
177  return 0;
178 }
179 
180 
181 TEST_IMPL(tcp_create_early_bad_domain) {
183  int r;
184 
186  ASSERT(r == UV_EINVAL);
187 
188  r = uv_tcp_init_ex(uv_default_loop(), &client, 1024);
189  ASSERT(r == UV_EINVAL);
190 
192 
194  return 0;
195 }
196 
197 
198 TEST_IMPL(tcp_create_early_accept) {
201 
204 
206 
208  return 0;
209 }
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
tcp_listener
static void tcp_listener(uv_loop_t *loop, uv_tcp_t *server)
Definition: test-tcp-create-socket-early.c:60
task.h
AF_INET6
#define AF_INET6
Definition: ares_setup.h:208
uv_connect_s
Definition: uv.h:580
client
Definition: examples/python/async_streaming/client.py:1
string.h
uv_listen
UV_EXTERN int uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb)
Definition: unix/stream.c:656
uv_connect_s::handle
uv_stream_t * handle
Definition: uv.h:583
tcp_connector
static void tcp_connector(uv_loop_t *loop, uv_tcp_t *client, uv_connect_t *req)
Definition: test-tcp-create-socket-early.c:77
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
uv_tcp_getsockname
UV_EXTERN int uv_tcp_getsockname(const uv_tcp_t *handle, struct sockaddr *name, int *namelen)
Definition: unix/tcp.c:283
uv_tcp_bind
UV_EXTERN int uv_tcp_bind(uv_tcp_t *handle, const struct sockaddr *addr, unsigned int flags)
Definition: uv-common.c:277
TEST_PORT
#define TEST_PORT
Definition: task.h:53
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_stream_s
Definition: uv.h:491
client
static uv_tcp_t client
Definition: test-callback-stack.c:33
uv_tcp_connect
UV_EXTERN int uv_tcp_connect(uv_connect_t *req, uv_tcp_t *handle, const struct sockaddr *addr, uv_connect_cb cb)
Definition: uv-common.c:315
uv_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
server
std::unique_ptr< Server > server
Definition: channelz_service_test.cc:330
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
sockaddr_in6
Definition: ares_ipv6.h:25
req
static uv_connect_t req
Definition: test-connection-fail.c:30
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_tcp_init
UV_EXTERN int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: unix/tcp.c:143
uv_accept
UV_EXTERN int uv_accept(uv_stream_t *server, uv_stream_t *client)
Definition: unix/stream.c:591
uv_tcp_s
Definition: uv.h:544
INVALID_FD
#define INVALID_FD
Definition: test-tcp-create-socket-early.c:30
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
uv_tcp_init_ex
UV_EXTERN int uv_tcp_init_ex(uv_loop_t *, uv_tcp_t *handle, unsigned int flags)
Definition: unix/tcp.c:114
uv_fileno
UV_EXTERN int uv_fileno(const uv_handle_t *handle, uv_os_fd_t *fd)
Definition: unix/core.c:755
connect_req
static uv_connect_t connect_req
Definition: benchmark-tcp-write-batch.c:39
uv_os_fd_t
int uv_os_fd_t
Definition: unix.h:128
server
Definition: examples/python/async_streaming/server.py:1
fix_build_deps.r
r
Definition: fix_build_deps.py:491
on_connect
static void on_connect(uv_connect_t *req, int status)
Definition: test-tcp-create-socket-early.c:34
can_ipv6
static UNUSED int can_ipv6(void)
Definition: task.h:315
RETURN_SKIP
#define RETURN_SKIP(explanation)
Definition: task.h:262
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
uv_loop_s
Definition: uv.h:1767
uv_close_cb
void(* uv_close_cb)(uv_handle_t *handle)
Definition: uv.h:316
on_connection
static void on_connection(uv_stream_t *server, int status)
Definition: test-tcp-create-socket-early.c:40
TEST_IMPL
TEST_IMPL(tcp_create_early)
Definition: test-tcp-create-socket-early.c:94
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:30