diff options
Diffstat (limited to 'tests/t_socket.c')
| -rw-r--r-- | tests/t_socket.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/t_socket.c b/tests/t_socket.c index 8eef7fd..ab4b785 100644 --- a/tests/t_socket.c +++ b/tests/t_socket.c | |||
| @@ -8,13 +8,18 @@ | |||
| 8 | #include <fcntl.h> | 8 | #include <fcntl.h> |
| 9 | #include <netinet/in.h> | 9 | #include <netinet/in.h> |
| 10 | #include <poll.h> | 10 | #include <poll.h> |
| 11 | #include <pthread.h> | ||
| 12 | #include <signal.h> | ||
| 13 | #include <stdatomic.h> | ||
| 11 | #include <string.h> | 14 | #include <string.h> |
| 12 | #include <sys/socket.h> | 15 | #include <sys/socket.h> |
| 16 | #include <sys/time.h> | ||
| 13 | #include <unistd.h> | 17 | #include <unistd.h> |
| 14 | 18 | ||
| 15 | /* Opentracker */ | 19 | /* Opentracker */ |
| 16 | #include "ot_ip.h" | 20 | #include "ot_ip.h" |
| 17 | #include "ot_socket.h" | 21 | #include "ot_socket.h" |
| 22 | #include "trackerlogic.h" | ||
| 18 | #include "test.h" | 23 | #include "test.h" |
| 19 | 24 | ||
| 20 | /* These cases talk to the loopback interface rather than to a mock, because | 25 | /* These cases talk to the loopback interface rather than to a mock, because |
| @@ -144,6 +149,57 @@ static void case_udp_roundtrip(void) { | |||
| 144 | close(s); | 149 | close(s); |
| 145 | } | 150 | } |
| 146 | 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. */ | ||
| 159 | static _Atomic int reader_returned; | ||
| 160 | static int reader_socket; | ||
| 161 | |||
| 162 | static void reader_wake(int s) { (void)s; } | ||
| 163 | |||
| 164 | static 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 | |||
| 174 | static 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 | |||
| 147 | /* An IPv4 peer must arrive in the v4 mapped form opentracker stores, however | 203 | /* An IPv4 peer must arrive in the v4 mapped form opentracker stores, however |
| 148 | the kernel chose to report it. Linux fills a v4 mapped sockaddr_in6 here | 204 | the kernel chose to report it. Linux fills a v4 mapped sockaddr_in6 here |
| 149 | and the BSDs fill a plain sockaddr_in, so this is the case that tells the | 205 | and the BSDs fill a plain sockaddr_in, so this is the case that tells the |
| @@ -293,6 +349,7 @@ void test_socket(void) { | |||
| 293 | case_sharing_is_opt_in(); | 349 | case_sharing_is_opt_in(); |
| 294 | #endif | 350 | #endif |
| 295 | case_udp_roundtrip(); | 351 | case_udp_roundtrip(); |
| 352 | case_a_signal_ends_a_blocking_read(); | ||
| 296 | case_udp_v4mapped(); | 353 | case_udp_v4mapped(); |
| 297 | case_tcp_roundtrip(); | 354 | case_tcp_roundtrip(); |
| 298 | case_connect_refused(); | 355 | case_connect_refused(); |
