Debian Codegraph @

gnulib-tests/test-sys_socket-h.c

raw
1 /* Test of <sys/socket.h> substitute.
2 Copyright (C) 2007, 2009-2025 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Bruno Haible <[email protected]>, 2007. */
18
19 #include <config.h>
20
21 #include <sys/socket.h>
22
23 /* POSIX mandates that AF_UNSPEC shall be 0. */
24 static_assert (AF_UNSPEC == 0);
25
26 /* Check that the 'socklen_t' type is defined. */
27 socklen_t t1;
28
29 /* Check that the 'size_t' and 'ssize_t' types are defined. */
30 size_t t2;
31 ssize_t t3;
32
33 /* Check that the 'struct sockaddr' type is defined. */
34 struct sockaddr t4;
35
36 /* Check that the 'struct sockaddr_storage' type is defined. */
37 struct sockaddr_storage t5;
38
39 /* Check that 'struct iovec' is defined. */
40 struct iovec io;
41
42 /* Check that a minimal set of 'struct msghdr' is defined. */
43 struct msghdr msg;
44
45 #ifdef SCM_RIGHTS
46 /* Check that the 'struct cmsghdr' type is defined. */
47 struct cmsghdr cmsg;
48 /* Check constant cmsg_type values. */
49 int cmsg_types[] = { SCM_RIGHTS };
50 #endif
51
52 /* Check that the 'struct linger' type is defined. */
53 struct linger li;
54
55 /* Check constant level arguments. */
56 int levels[] = { SOL_SOCKET };
57
58 /* Check constant backlog arguments. */
59 int backlogs[] = { SOMAXCONN };
60
61 #include <errno.h>
62
63 #include "intprops.h"
64 #include "macros.h"
65
66 /* POSIX requires that 'socklen_t' is an integer type with a width of at
67 least 32 bits. */
68 static_assert (32 <= TYPE_WIDTH (socklen_t));
69
70 /* POSIX requires that sa_family_t is an unsigned integer type. */
71 static_assert (! TYPE_SIGNED (sa_family_t));
72
73
74 int
75 main (void)
76 {
77 struct sockaddr_storage x;
78 sa_family_t i;
79
80 /* Check some errno values. */
81 switch (ENOTSOCK)
82 {
83 case ENOTSOCK:
84 case EADDRINUSE:
85 case ENETRESET:
86 case ECONNABORTED:
87 case ECONNRESET:
88 case ENOTCONN:
89 case ESHUTDOWN:
90 break;
91 }
92
93 /* Check that the supported socket types have distinct values. */
94 switch (0)
95 {
96 case SOCK_DGRAM:
97 #ifdef SOCK_RAW
98 case SOCK_RAW:
99 #endif
100 case SOCK_SEQPACKET:
101 case SOCK_STREAM:
102 default:
103 break;
104 }
105 int socket_types = 0;
106 socket_types |= SOCK_DGRAM;
107 #ifdef SOCK_RAW
108 socket_types |= SOCK_RAW;
109 #endif
110 socket_types |= SOCK_SEQPACKET;
111 socket_types |= SOCK_STREAM;
112
113 /* Check socket creation flags. */
114 #ifdef SOCK_NONBLOCK
115 ASSERT ((SOCK_NONBLOCK & socket_types) == 0);
116 #endif
117 #ifdef SOCK_CLOEXEC
118 ASSERT ((SOCK_CLOEXEC & socket_types) == 0);
119 #endif
120 #ifdef SOCK_CLOFORK
121 ASSERT ((SOCK_CLOFORK & socket_types) == 0);
122 #endif
123 #if defined SOCK_NONBLOCK && defined SOCK_CLOEXEC
124 static_assert ((SOCK_NONBLOCK & SOCK_CLOEXEC) == 0);
125 #endif
126 #if defined SOCK_NONBLOCK && defined SOCK_CLOFORK
127 static_assert ((SOCK_NONBLOCK & SOCK_CLOFORK) == 0);
128 #endif
129 #if defined SOCK_CLOEXEC && defined SOCK_CLOFORK
130 static_assert ((SOCK_CLOEXEC & SOCK_CLOFORK) == 0);
131 #endif
132
133 /* Check that the option_name constants have distinct values. */
134 switch (0)
135 {
136 case SO_ACCEPTCONN:
137 case SO_BROADCAST:
138 case SO_DEBUG:
139 #ifdef SO_DOMAIN
140 case SO_DOMAIN:
141 #endif
142 case SO_DONTROUTE:
143 case SO_ERROR:
144 case SO_KEEPALIVE:
145 case SO_LINGER:
146 case SO_OOBINLINE:
147 #ifdef SO_PROTOCOL
148 case SO_PROTOCOL:
149 #endif
150 case SO_RCVBUF:
151 case SO_RCVLOWAT:
152 case SO_RCVTIMEO:
153 case SO_REUSEADDR:
154 case SO_SNDBUF:
155 case SO_SNDLOWAT:
156 case SO_SNDTIMEO:
157 case SO_TYPE:
158 default:
159 break;
160 }
161
162 /* Check the msg_flags constants have distinct values. */
163 switch (0)
164 {
165 #ifdef MSG_CMSG_CLOEXEC
166 case MSG_CMSG_CLOEXEC:
167 #endif
168 #ifdef MSG_CMSG_CLOFORK
169 case MSG_CMSG_CLOFORK:
170 #endif
171 case MSG_DONTROUTE:
172 #ifdef MSG_EOR
173 case MSG_EOR:
174 #endif
175 case MSG_OOB:
176 #ifdef MSG_NOSIGNAL
177 case MSG_NOSIGNAL:
178 #endif
179 case MSG_PEEK:
180 #ifdef MSG_TRUNC
181 case MSG_CTRUNC:
182 case MSG_TRUNC:
183 #endif
184 #ifdef MSG_WAITALL
185 case MSG_WAITALL:
186 #endif
187 default:
188 break;
189 }
190
191 /* Check that each supported address family has a distinct value. */
192 switch (0)
193 {
194 case AF_UNSPEC:
195 #if HAVE_IPV4
196 case AF_INET:
197 #endif
198 #if HAVE_IPV6
199 case AF_INET6:
200 #endif
201 #if HAVE_UNIXSOCKET
202 case AF_UNIX:
203 #endif
204 default:
205 break;
206 }
207
208 /* Check that the shutdown type macros are defined to distinct values. */
209 #if HAVE_SHUTDOWN
210 switch (0)
211 {
212 case SHUT_RD:
213 case SHUT_WR:
214 case SHUT_RDWR:
215 default:
216 break;
217 }
218 #endif
219
220 x.ss_family = 42;
221 i = 42;
222 msg.msg_iov = &io;
223
224 return (x.ss_family - i
225 + msg.msg_namelen + msg.msg_iov->iov_len + msg.msg_iovlen)
226 + test_exit_status;
227 }