summaryrefslogtreecommitdiff
path: root/tests/t_socket.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/t_socket.c')
-rw-r--r--tests/t_socket.c416
1 files changed, 416 insertions, 0 deletions
diff --git a/tests/t_socket.c b/tests/t_socket.c
new file mode 100644
index 0000000..6f07f95
--- /dev/null
+++ b/tests/t_socket.c
@@ -0,0 +1,416 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever. */
3
4/* System */
5#include <errno.h>
6#include <fcntl.h>
7#include <netinet/in.h>
8#include <poll.h>
9#include <pthread.h>
10#include <signal.h>
11#include <stdatomic.h>
12#include <string.h>
13#include <sys/socket.h>
14#include <sys/time.h>
15#include <unistd.h>
16
17/* Opentracker */
18#include "ot_ip.h"
19#include "ot_socket.h"
20#include "trackerlogic.h"
21#include "test.h"
22
23/* These cases talk to the loopback interface rather than to a mock, because
24 what is worth pinning down here is the behaviour of the socket options the
25 rest of the tracker relies on, and only the kernel can report that. */
26
27static const char ip_any[16] = {0};
28static const char ip_loopback[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
29static const char ip_v4_loopback[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 127, 0, 0, 1};
30/* RFC 3849 reserves 2001:db8::/32 for documentation, so no host carries it */
31static const char ip_unassigned[16] = {0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
32
33/* What a socket ended up bound to, which is not always what was asked for: a
34 FreeBSD jail, for one, rewrites a loopback bind to the address it was given.
35 A datagram a socket sends to itself must come back carrying this, so the
36 cases below compare against it rather than against the address they passed
37 to the bind. */
38static void bound_address(int s, char ip[16], uint16_t *port) {
39 struct sockaddr_storage ss;
40 socklen_t len = sizeof(ss);
41
42 memset(&ss, 0, sizeof(ss));
43 memset(ip, 0, 16);
44 *port = 0;
45 if (getsockname(s, (struct sockaddr *)&ss, &len) == -1)
46 return;
47
48 /* Spelled out here rather than borrowed from ot_socket.c, so that a wrong
49 idea about how a kernel reports an address cannot hide in both at once */
50 if (ss.ss_family == AF_INET) {
51 const struct sockaddr_in *sa = (const struct sockaddr_in *)&ss;
52
53 memcpy(ip, ip_v4mapped_prefix, sizeof(ip_v4mapped_prefix));
54 memcpy(ip + sizeof(ip_v4mapped_prefix), &sa->sin_addr, 4);
55 *port = ntohs(sa->sin_port);
56 } else {
57 const struct sockaddr_in6 *sa = (const struct sockaddr_in6 *)&ss;
58
59 memcpy(ip, &sa->sin6_addr, 16);
60 *port = ntohs(sa->sin6_port);
61 }
62}
63
64static int is_nonblocking(int s) {
65 int flags = fcntl(s, F_GETFL, 0);
66 return flags != -1 && (flags & O_NONBLOCK) != 0;
67}
68
69static int sock_flag(int s, int level, int option) {
70 int value = -1;
71 socklen_t len = sizeof(value);
72
73 if (getsockopt(s, level, option, &value, &len) == -1)
74 return -1;
75 return value != 0;
76}
77
78/* Wait for one event, so a test never blocks forever on a lost packet */
79static int wait_for_ms(int s, short events, int timeout_ms) {
80 struct pollfd pfd;
81
82 pfd.fd = s;
83 pfd.events = events;
84 pfd.revents = 0;
85 return poll(&pfd, 1, timeout_ms) == 1;
86}
87
88static int wait_for(int s, short events) { return wait_for_ms(s, events, 2000); }
89
90/* ------------------------------------------------------------ invariants */
91
92/* Asked for the wildcard address, because a socket bound to one specific
93 IPv6 address cannot carry IPv4 traffic anyway and Linux turns IPV6_V6ONLY
94 back on in that case. The tracker's own default is the wildcard. */
95static void case_socket_options(void) {
96 int udp, tcp;
97
98 test_case("a bound udp socket is non blocking and serves both families");
99 udp = socket_udp_bind(ip_any, 0, SOCKET_EXCLUSIVE);
100 CHECK(udp != -1);
101 CHECK_INT(is_nonblocking(udp), 1);
102 CHECK_INT(sock_flag(udp, IPPROTO_IPV6, IPV6_V6ONLY), 0);
103 close(udp);
104
105 test_case("a listening tcp socket is non blocking and serves both families");
106 tcp = socket_tcp_listen(ip_any, 0, SOCKET_EXCLUSIVE);
107 CHECK(tcp != -1);
108 CHECK_INT(is_nonblocking(tcp), 1);
109 CHECK_INT(sock_flag(tcp, IPPROTO_IPV6, IPV6_V6ONLY), 0);
110 close(tcp);
111}
112
113#ifdef SO_REUSEPORT
114static void case_sharing_is_opt_in(void) {
115 int s;
116
117 test_case("an address is exclusive unless sharing is asked for");
118 s = socket_udp_bind(ip_loopback, 0, SOCKET_EXCLUSIVE);
119 CHECK(s != -1);
120 CHECK_INT(sock_flag(s, SOL_SOCKET, SO_REUSEPORT), 0);
121 close(s);
122
123 test_case("and shared when it is");
124 s = socket_udp_bind(ip_loopback, 0, SOCKET_SHARED);
125 CHECK(s != -1);
126 CHECK_INT(sock_flag(s, SOL_SOCKET, SO_REUSEPORT), 1);
127 close(s);
128}
129#endif
130
131/* ------------------------------------------------------------------- udp */
132
133static void case_udp_roundtrip(void) {
134 char buf[32], ip[16], own_ip[16];
135 uint16_t port = 0, own_port = 0;
136 int s;
137
138 test_case("a datagram comes back with the sender's address and port");
139 s = socket_udp_bind(ip_loopback, 0, SOCKET_EXCLUSIVE);
140 CHECK(s != -1);
141 bound_address(s, own_ip, &own_port);
142 CHECK(own_port != 0);
143 CHECK_INT(socket_send(s, "opentracker", 11, own_ip, own_port), 11);
144 CHECK_INT(wait_for(s, POLLIN), 1);
145 CHECK_INT(socket_recv(s, buf, sizeof(buf), ip, &port), 11);
146 CHECK_MEM(buf, "opentracker", 11);
147 CHECK_MEM(ip, own_ip, 16);
148 CHECK_INT(port, own_port);
149 close(s);
150}
151
152/* udp_workers_deinit and livesync_deinit end a read another thread is parked
153 in by signalling that thread. Which mechanisms wake a blocked reader
154 differs between kernels -- shutting the socket down does so on Linux and
155 FreeBSD but not on Darwin, closing it does so on Darwin but not on Linux --
156 so the one the tracker relies on is checked rather than assumed. The
157 watchdog is the test's own: where a reader is not woken this fails with a
158 figure to look at instead of hanging the suite. */
159static _Atomic int reader_returned;
160static int reader_socket;
161
162static void reader_wake(int s) { (void)s; }
163
164static void *blocked_reader(void *args) {
165 char buf[32], ip[16];
166 uint16_t port = 0;
167
168 (void)args;
169 socket_recv(reader_socket, buf, sizeof(buf), ip, &port);
170 atomic_store(&reader_returned, 1);
171 return NULL;
172}
173
174static void case_a_signal_ends_a_blocking_read(void) {
175 struct sigaction sa;
176 struct timeval watchdog = {2, 0};
177 pthread_t thread;
178 int waited;
179
180 test_case("a signal ends a read another thread is parked in");
181 memset(&sa, 0, sizeof(sa));
182 sa.sa_handler = reader_wake;
183 sa.sa_flags = 0; /* SA_RESTART would resume the read instead of ending it */
184 CHECK_INT(sigaction(OT_SIGNAL_WAKE, &sa, NULL), 0);
185
186 atomic_store(&reader_returned, 0);
187 reader_socket = socket_udp_bind(ip_loopback, 0, SOCKET_EXCLUSIVE);
188 CHECK(reader_socket != -1);
189 CHECK_INT(socket_set_blocking(reader_socket, 1), 0);
190 CHECK_INT(setsockopt(reader_socket, SOL_SOCKET, SO_RCVTIMEO, &watchdog, sizeof(watchdog)), 0);
191
192 CHECK_INT(pthread_create(&thread, NULL, blocked_reader, NULL), 0);
193 usleep(100000); /* long enough to be sure it reached the read */
194 CHECK_INT(pthread_kill(thread, OT_SIGNAL_WAKE), 0);
195
196 for (waited = 0; waited < 1000 && !atomic_load(&reader_returned); ++waited)
197 usleep(1000);
198 CHECK(waited < 1000);
199 pthread_join(thread, NULL);
200 close(reader_socket);
201}
202
203/* What a deferred accept is for is a descriptor that already has the request
204 in it, so what is checked is the silence before: a client that has
205 connected but said nothing must not make the listener readable. Kernels
206 that cannot do this say so, and then there is nothing to check. */
207static void case_deferred_accept(void) {
208 struct sockaddr_in6 address;
209 char ip[16];
210 uint16_t port = 0;
211 int listener, client;
212
213 test_case("a connection that has sent nothing is held back from accept");
214 listener = socket_tcp_listen(ip_loopback, 0, SOCKET_EXCLUSIVE);
215 CHECK(listener != -1);
216 if (socket_defer_accept(listener) == -1) {
217 close(listener);
218 return;
219 }
220 bound_address(listener, ip, &port);
221
222 client = socket(AF_INET6, SOCK_STREAM, 0);
223 CHECK(client != -1);
224 memset(&address, 0, sizeof(address));
225 address.sin6_family = AF_INET6;
226 address.sin6_port = htons(port);
227 memcpy(&address.sin6_addr, ip_loopback, 16);
228 CHECK_INT(connect(client, (struct sockaddr *)&address, sizeof(address)), 0);
229
230 CHECK_INT(wait_for_ms(listener, POLLIN, 300), 0);
231
232 CHECK_INT((int)write(client, "GET / HTTP/1.0\r\n\r\n", 18), 18);
233 CHECK_INT(wait_for(listener, POLLIN), 1);
234
235 close(client);
236 close(listener);
237}
238
239/* An IPv4 peer must arrive in the v4 mapped form opentracker stores, however
240 the kernel chose to report it. Linux fills a v4 mapped sockaddr_in6 here
241 and the BSDs fill a plain sockaddr_in, so this is the case that tells the
242 two apart. */
243static void case_udp_v4mapped(void) {
244 char buf[32], ip[16], receiver_ip[16], sender_ip[16];
245 uint16_t port = 0, receiver_port = 0, sender_port = 0;
246 int receiver, sender;
247
248 test_case("an IPv4 sender is reported in its v4 mapped form");
249 receiver = socket_udp_bind(ip_v4_loopback, 0, SOCKET_EXCLUSIVE);
250 CHECK(receiver != -1);
251 sender = socket_udp_bind(ip_v4_loopback, 0, SOCKET_EXCLUSIVE);
252 CHECK(sender != -1);
253 bound_address(receiver, receiver_ip, &receiver_port);
254 bound_address(sender, sender_ip, &sender_port);
255 CHECK_INT(ip_is_v4mapped(sender_ip), 1);
256
257 CHECK_INT(socket_send(sender, "v4", 2, receiver_ip, receiver_port), 2);
258 CHECK_INT(wait_for(receiver, POLLIN), 1);
259 CHECK_INT(socket_recv(receiver, buf, sizeof(buf), ip, &port), 2);
260 CHECK_INT(ip_is_v4mapped(ip), 1);
261 CHECK_MEM(ip, sender_ip, 16);
262 CHECK_INT(port, sender_port);
263 close(sender);
264 close(receiver);
265}
266
267/* ------------------------------------------------------------------- tcp */
268
269static void case_tcp_roundtrip(void) {
270 char ip[16], listener_ip[16], client_ip[16];
271 uint16_t port = 0, listener_port = 0, client_port = 0;
272 int listener, client, accepted;
273
274 test_case("a connection is accepted and carries the caller's address");
275 listener = socket_tcp_listen(ip_loopback, 0, SOCKET_EXCLUSIVE);
276 CHECK(listener != -1);
277 bound_address(listener, listener_ip, &listener_port);
278 CHECK(listener_port != 0);
279
280 client = socket_tcp_connect(ip_loopback, 0, listener_ip, listener_port);
281 CHECK(client != -1);
282 CHECK_INT(wait_for(client, POLLOUT), 1);
283 CHECK_INT(socket_is_connected(client), 1);
284 bound_address(client, client_ip, &client_port);
285
286 CHECK_INT(wait_for(listener, POLLIN), 1);
287 accepted = socket_accept(listener, ip, &port);
288 CHECK(accepted != -1);
289 CHECK_MEM(ip, client_ip, 16);
290 CHECK_INT(port, client_port);
291
292 test_case("the accepted socket is handed over non blocking");
293 CHECK_INT(is_nonblocking(accepted), 1);
294
295 close(accepted);
296 close(client);
297 close(listener);
298}
299
300static void case_connect_refused(void) {
301 char listener_ip[16];
302 int listener, client;
303 uint16_t port;
304
305 test_case("a refused connection is reported by socket_is_connected");
306 /* Take a port, then drop it, so nothing is listening on it any more */
307 listener = socket_tcp_listen(ip_loopback, 0, SOCKET_EXCLUSIVE);
308 CHECK(listener != -1);
309 bound_address(listener, listener_ip, &port);
310 close(listener);
311
312 client = socket_tcp_connect(ip_loopback, 0, listener_ip, port);
313 CHECK(client != -1);
314 CHECK_INT(wait_for(client, POLLOUT), 1);
315 CHECK_INT(socket_is_connected(client), 0);
316 CHECK_INT(errno, ECONNREFUSED);
317 close(client);
318}
319
320/* --------------------------------------------------------- failed binds */
321
322/* The lowest free descriptor is handed out first, so a number that stays put
323 across a failing call shows the call did not keep a socket behind. */
324static int next_descriptor(void) {
325 int fd = dup(0);
326
327 if (fd != -1)
328 close(fd);
329 return fd;
330}
331
332static void case_bind_failure(void) {
333 int before;
334
335 test_case("binding an address no interface carries fails");
336 before = next_descriptor();
337 CHECK(before != -1);
338 CHECK_INT(socket_tcp_listen(ip_unassigned, 0, SOCKET_EXCLUSIVE), -1);
339 CHECK_INT(socket_udp_bind(ip_unassigned, 0, SOCKET_EXCLUSIVE), -1);
340
341 test_case("a failed bind leaves no socket behind");
342 CHECK_INT(next_descriptor(), before);
343}
344
345/* The holder binds the way the tracker does, which lets a second socket in
346 beside it, and that is what the probe is there to see through. */
347static void case_a_taken_udp_address_is_noticed(void) {
348 char ip[16];
349 uint16_t port;
350 int holder;
351
352 test_case("a udp address somebody holds is reported as taken");
353 holder = socket_udp_bind(ip_loopback, 0, SOCKET_SHARED);
354 CHECK(holder != -1);
355 bound_address(holder, ip, &port);
356 CHECK_INT(socket_udp_is_taken(ip_loopback, port), 1);
357
358 test_case("and as free again once it is let go");
359 close(holder);
360 CHECK_INT(socket_udp_is_taken(ip_loopback, port), 0);
361}
362
363#ifdef WANT_SYNC_LIVE
364/* Reading an IPv4 multicast option back: Linux answers with an int, the BSDs
365 with a single byte, so ask how much was written. */
366static int mcast_option(int s, int option) {
367 int value = 0;
368 socklen_t len = sizeof(value);
369
370 if (getsockopt(s, IPPROTO_IP, option, &value, &len) == -1)
371 return -1;
372 if (len == 1)
373 return *(unsigned char *)&value;
374 return value;
375}
376
377static void case_mcast_sender(void) {
378 int s;
379
380 test_case("the sending socket keeps its traffic on the local network");
381 s = socket_mcast_bind(ip_v4_loopback, 0, 0);
382 CHECK(s != -1);
383 CHECK_INT(mcast_option(s, IP_MULTICAST_TTL), 1);
384
385 test_case("and does not hear itself unless it is asked to");
386 CHECK_INT(mcast_option(s, IP_MULTICAST_LOOP), 0);
387
388 test_case("whoever sends from it is serving clients too, so it cannot block");
389 CHECK_INT(is_nonblocking(s), 1);
390 close(s);
391
392 test_case("the proxy asks to hear itself and gets it");
393 s = socket_mcast_bind(ip_v4_loopback, 0, 1);
394 CHECK(s != -1);
395 CHECK_INT(mcast_option(s, IP_MULTICAST_LOOP), 1);
396 close(s);
397}
398#endif
399
400void test_socket(void) {
401 case_socket_options();
402#ifdef SO_REUSEPORT
403 case_sharing_is_opt_in();
404#endif
405 case_udp_roundtrip();
406 case_a_signal_ends_a_blocking_read();
407 case_udp_v4mapped();
408 case_tcp_roundtrip();
409 case_deferred_accept();
410 case_connect_refused();
411 case_bind_failure();
412 case_a_taken_udp_address_is_noticed();
413#ifdef WANT_SYNC_LIVE
414 case_mcast_sender();
415#endif
416}