test-udp-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 TEST_IMPL(udp_create_early) {
35  struct sockaddr_in addr;
36  struct sockaddr_in sockname;
38  uv_os_fd_t fd;
39  int r, namelen;
40 
41  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
42 
43  r = uv_udp_init_ex(uv_default_loop(), &client, AF_INET);
44  ASSERT(r == 0);
45 
46  r = uv_fileno((const uv_handle_t*) &client, &fd);
47  ASSERT(r == 0);
48  ASSERT(fd != INVALID_FD);
49 
50  /* Windows returns WSAEINVAL if the socket is not bound */
51 #ifndef _WIN32
52  namelen = sizeof sockname;
53  r = uv_udp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
54  ASSERT(r == 0);
55  ASSERT(sockname.sin_family == AF_INET);
56 #endif
57 
58  r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 0);
59  ASSERT(r == 0);
60 
61  namelen = sizeof sockname;
62  r = uv_udp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
63  ASSERT(r == 0);
64  ASSERT(memcmp(&addr.sin_addr,
65  &sockname.sin_addr,
66  sizeof(addr.sin_addr)) == 0);
67 
68  uv_close((uv_handle_t*) &client, NULL);
70 
72  return 0;
73 }
74 
75 
76 TEST_IMPL(udp_create_early_bad_bind) {
77  struct sockaddr_in addr;
79  uv_os_fd_t fd;
80  int r;
81 
82  if (!can_ipv6())
83  RETURN_SKIP("IPv6 not supported");
84 
85  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
86 
88  ASSERT(r == 0);
89 
90  r = uv_fileno((const uv_handle_t*) &client, &fd);
91  ASSERT(r == 0);
92  ASSERT(fd != INVALID_FD);
93 
94  /* Windows returns WSAEINVAL if the socket is not bound */
95 #ifndef _WIN32
96  {
97  int namelen;
98  struct sockaddr_in6 sockname;
99  namelen = sizeof sockname;
100  r = uv_udp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
101  ASSERT(r == 0);
102  ASSERT(sockname.sin6_family == AF_INET6);
103  }
104 #endif
105 
106  r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 0);
107 #if !defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MSYS__)
108  ASSERT(r == UV_EINVAL);
109 #else
110  ASSERT(r == UV_EFAULT);
111 #endif
112 
113  uv_close((uv_handle_t*) &client, NULL);
115 
117  return 0;
118 }
119 
120 
121 TEST_IMPL(udp_create_early_bad_domain) {
123  int r;
124 
126  ASSERT(r == UV_EINVAL);
127 
128  r = uv_udp_init_ex(uv_default_loop(), &client, 1024);
129  ASSERT(r == UV_EINVAL);
130 
132 
134  return 0;
135 }
task.h
AF_INET6
#define AF_INET6
Definition: ares_setup.h:208
client
Definition: examples/python/async_streaming/client.py:1
string.h
ASSERT
#define ASSERT(expr)
Definition: task.h:102
uv_udp_getsockname
UV_EXTERN int uv_udp_getsockname(const uv_udp_t *handle, struct sockaddr *name, int *namelen)
Definition: unix/udp.c:1293
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
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
client
static uv_tcp_t client
Definition: test-callback-stack.c:33
uv_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
sockaddr_in6
Definition: ares_ipv6.h:25
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_udp_s
Definition: uv.h:629
INVALID_FD
#define INVALID_FD
Definition: test-udp-create-socket-early.c:30
TEST_IMPL
TEST_IMPL(udp_create_early)
Definition: test-udp-create-socket-early.c:34
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
uv_fileno
UV_EXTERN int uv_fileno(const uv_handle_t *handle, uv_os_fd_t *fd)
Definition: unix/core.c:755
uv_udp_bind
UV_EXTERN int uv_udp_bind(uv_udp_t *handle, const struct sockaddr *addr, unsigned int flags)
Definition: uv-common.c:296
uv_os_fd_t
int uv_os_fd_t
Definition: unix.h:128
fix_build_deps.r
r
Definition: fix_build_deps.py:491
can_ipv6
static UNUSED int can_ipv6(void)
Definition: task.h:315
RETURN_SKIP
#define RETURN_SKIP(explanation)
Definition: task.h:262
uv_handle_s
Definition: uv.h:441
uv_udp_init_ex
UV_EXTERN int uv_udp_init_ex(uv_loop_t *, uv_udp_t *handle, unsigned int flags)
Definition: unix/udp.c:947
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