Debian Codegraph @

tests/api/inet.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2014-2024 The ProFTPD Project team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, The ProFTPD Project team and other respective
20 * copyright holders give permission to link this program with OpenSSL, and
21 * distribute the resulting executable, without including the source code for
22 * OpenSSL in the source distribution.
23 */
24
25 /* Inet API tests */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 /* Use Google's DNS resolvers by default. */
32 static const char *dns_resolver = "8.8.8.8";
33
34 static void set_up(void) {
35 const char *use_resolver = NULL;
36
37 if (p == NULL) {
38 p = permanent_pool = make_sub_pool(NULL);
39 }
40
41 main_server = pcalloc(p, sizeof(server_rec));
42 main_server->pool = p;
43
44 init_class();
45 init_netaddr();
46 init_netio();
47 init_inet();
48
49 use_resolver = getenv("PR_USE_DNS_RESOLVER");
50 if (use_resolver != NULL) {
51 dns_resolver = use_resolver;
52 }
53
54 if (getenv("TEST_VERBOSE") != NULL) {
55 pr_trace_set_levels("class", 1, 20);
56 pr_trace_set_levels("inet", 1, 20);
57 }
58
59 pr_inet_set_default_family(p, AF_INET);
60 }
61
62 static void tear_down(void) {
63 if (getenv("TEST_VERBOSE") != NULL) {
64 pr_trace_set_levels("class", 0, 0);
65 pr_trace_set_levels("inet", 0, 0);
66 }
67
68 pr_inet_set_default_family(p, 0);
69 pr_inet_clear();
70
71 if (p) {
72 destroy_pool(p);
73 main_server = NULL;
74 p = permanent_pool = NULL;
75 }
76 }
77
78 static int devnull_fd(void) {
79 int fd;
80
81 fd = open("/dev/null", O_RDWR);
82 if (fd < 0) {
83 fprintf(stderr, "Error opening /dev/null: %s\n", strerror(errno));
84 return -1;
85 }
86
87 return fd;
88 }
89
90 /* Tests */
91
92 START_TEST (inet_family_test) {
93 int res;
94
95 pr_inet_set_default_family(p, 0);
96
97 res = pr_inet_set_default_family(p, AF_INET);
98 ck_assert_msg(res == 0, "Expected previous family 0, got %d", res);
99
100 res = pr_inet_set_default_family(p, 0);
101 ck_assert_msg(res == AF_INET, "Expected previous family %d, got %d", AF_INET,
102 res);
103
104 /* Restore the default family to AF_INET, for other tests. */
105 pr_inet_set_default_family(p, AF_INET);
106 }
107 END_TEST
108
109 START_TEST (inet_getservport_test) {
110 int res;
111
112 mark_point();
113 res = pr_inet_getservport(NULL, NULL, NULL);
114 ck_assert_msg(res < 0, "Failed to handle null service");
115 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d) got %s (%d)", EINVAL,
116 strerror(errno), errno);
117
118 mark_point();
119 res = pr_inet_getservport(p, "ftp", NULL);
120 ck_assert_msg(res > 0, "Failed to handle known service");
121
122 mark_point();
123 res = pr_inet_getservport(p, "ftp", "tcp");
124 ck_assert_msg(res > 0, "Failed to handle service 'ftp', proto 'tcp': %s",
125 strerror(errno));
126
127 mark_point();
128 res = pr_inet_getservport(p, "foobarbaz", "quxxquzz");
129
130 /* Different platforms/implementations handle this differently. */
131 if (res < 0 &&
132 errno != 0) {
133 ck_assert_msg(errno == EINVAL || errno == ENOENT,
134 "Expected EINVAL (%d) or ENOENT (%d), got %s (%d)", EINVAL, ENOENT,
135 strerror(errno), errno);
136 }
137 }
138 END_TEST
139
140 START_TEST (inet_close_test) {
141 mark_point();
142 pr_inet_close(NULL, NULL);
143
144 mark_point();
145 pr_inet_close(p, NULL);
146 }
147 END_TEST
148
149 START_TEST (inet_create_conn_test) {
150 int sockfd = -2, port = INPORT_ANY;
151 conn_t *conn, *conn2;
152
153 mark_point();
154 conn = pr_inet_create_conn(NULL, sockfd, NULL, port, FALSE);
155 ck_assert_msg(conn == NULL, "Failed to handle null arguments");
156 ck_assert_msg(errno == EINVAL,
157 "Failed to set errno to EINVAL (%d), got '%s' (%d)", EINVAL,
158 strerror(errno), errno);
159
160 mark_point();
161 conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
162 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
163 ck_assert_msg(conn->listen_fd == sockfd, "Expected listen_fd %d, got %d",
164 sockfd, conn->listen_fd);
165 pr_inet_close(p, conn);
166
167 sockfd = -1;
168 conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
169 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
170 ck_assert_msg(conn->listen_fd != sockfd,
171 "Expected listen_fd other than %d, got %d",
172 sockfd, conn->listen_fd);
173
174 /* Create another conn, with the same port, make sure it fails. */
175 conn2 = pr_inet_create_conn(p, sockfd, NULL, conn->local_port, FALSE);
176 if (conn2 == NULL) {
177 ck_assert_msg(errno == EADDRINUSE, "Expected EADDRINUSE (%d), got %s (%d)",
178 EADDRINUSE, strerror(errno), errno);
179 pr_inet_close(p, conn2);
180 }
181
182 pr_inet_close(p, conn);
183 }
184 END_TEST
185
186 START_TEST (inet_create_conn_portrange_test) {
187 conn_t *conn;
188
189 conn = pr_inet_create_conn_portrange(NULL, NULL, -1, -1);
190 ck_assert_msg(conn == NULL, "Failed to handle negative ports");
191 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
192 strerror(errno), errno);
193
194 conn = pr_inet_create_conn_portrange(NULL, NULL, 10, 1);
195 ck_assert_msg(conn == NULL, "Failed to handle bad ports");
196 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
197 strerror(errno), errno);
198
199 conn = pr_inet_create_conn_portrange(p, NULL, 49152, 65534);
200 ck_assert_msg(conn != NULL, "Failed to create conn in portrange: %s",
201 strerror(errno));
202 pr_inet_lingering_close(p, conn, 0L);
203 }
204 END_TEST
205
206 START_TEST (inet_copy_conn_test) {
207 int fd = -1, sockfd = -1, port = INPORT_ANY;
208 conn_t *conn, *conn2;
209 const char *name;
210
211 conn = pr_inet_copy_conn(NULL, NULL);
212 ck_assert_msg(conn == NULL, "Failed to handle null arguments");
213 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
214 strerror(errno), errno);
215
216 conn = pr_inet_copy_conn(p, NULL);
217 ck_assert_msg(conn == NULL, "Failed to handle null conn argument");
218 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
219 strerror(errno), errno);
220
221 conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
222 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
223
224 conn2 = pr_inet_copy_conn(p, conn);
225 ck_assert_msg(conn2 != NULL, "Failed to copy conn: %s", strerror(errno));
226
227 pr_inet_close(p, conn);
228 pr_inet_close(p, conn2);
229
230 conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
231 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
232
233 name = "127.0.0.1";
234 conn->remote_addr = pr_netaddr_get_addr(p, name, NULL);
235 ck_assert_msg(conn->remote_addr != NULL, "Failed to resolve '%s': %s",
236 name, strerror(errno));
237 conn->remote_name = pstrdup(p, name);
238 conn->instrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
239 ck_assert_msg(conn->instrm != NULL, "Failed to open ctrl reading stream: %s",
240 strerror(errno));
241 conn->outstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_WR);
242 ck_assert_msg(conn->instrm != NULL, "Failed to open ctrl writing stream: %s",
243 strerror(errno));
244
245 conn2 = pr_inet_copy_conn(p, conn);
246 ck_assert_msg(conn2 != NULL, "Failed to copy conn: %s", strerror(errno));
247
248 mark_point();
249 pr_inet_lingering_close(NULL, NULL, 0L);
250
251 pr_inet_lingering_close(p, conn, 0L);
252 pr_inet_close(p, conn2);
253
254 conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
255 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
256
257 conn->instrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
258 ck_assert_msg(conn->instrm != NULL, "Failed to open ctrl reading stream: %s",
259 strerror(errno));
260 conn->outstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_WR);
261 ck_assert_msg(conn->instrm != NULL, "Failed to open ctrl writing stream: %s",
262 strerror(errno));
263
264 mark_point();
265 pr_inet_lingering_abort(NULL, NULL, 0L);
266
267 pr_inet_lingering_abort(p, conn, 0L);
268 }
269 END_TEST
270
271 START_TEST (inet_set_async_test) {
272 int fd, sockfd = -1, port = INPORT_ANY, res;
273 conn_t *conn;
274
275 res = pr_inet_set_async(NULL, NULL);
276 ck_assert_msg(res < 0, "Failed to handle null arguments");
277 ck_assert_msg(errno == EINVAL, "Expected errno EINVAL (%d), got '%s' (%d)",
278 EINVAL, strerror(errno), errno);
279
280 conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
281 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
282
283 res = pr_inet_set_async(p, conn);
284 ck_assert_msg(res == 0, "Failed to set conn %p async: %s", conn,
285 strerror(errno));
286
287 fd = conn->rfd;
288 conn->rfd = 77;
289 res = pr_inet_set_async(p, conn);
290 ck_assert_msg(res == 0, "Failed to set conn %p async: %s", conn,
291 strerror(errno));
292 conn->rfd = fd;
293
294 fd = conn->wfd;
295 conn->wfd = 78;
296 res = pr_inet_set_async(p, conn);
297 ck_assert_msg(res == 0, "Failed to set conn %p async: %s", conn,
298 strerror(errno));
299 conn->wfd = fd;
300
301 fd = conn->listen_fd;
302 conn->listen_fd = 79;
303 res = pr_inet_set_async(p, conn);
304 ck_assert_msg(res == 0, "Failed to set conn %p async: %s", conn,
305 strerror(errno));
306 conn->listen_fd = fd;
307
308 pr_inet_close(p, conn);
309 }
310 END_TEST
311
312 START_TEST (inet_set_block_test) {
313 int fd, sockfd, port = INPORT_ANY, res;
314 conn_t *conn;
315
316 mark_point();
317 res = pr_inet_set_block(NULL, NULL);
318 ck_assert_msg(res < 0, "Failed to handle null arguments");
319 ck_assert_msg(errno == EINVAL, "Expected errno EINVAL (%d), got '%s' (%d)",
320 EINVAL, strerror(errno), errno);
321
322 mark_point();
323 res = pr_inet_set_nonblock(NULL, NULL);
324 ck_assert_msg(res < 0, "Failed to handle null arguments");
325 ck_assert_msg(errno == EINVAL, "Expected errno EINVAL (%d), got '%s' (%d)",
326 EINVAL, strerror(errno), errno);
327
328 mark_point();
329 conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
330 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
331
332 res = pr_inet_set_nonblock(p, conn);
333 ck_assert_msg(res < 0, "Failed to handle bad socket");
334 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
335 strerror(errno), errno);
336
337 res = pr_inet_set_block(p, conn);
338 ck_assert_msg(res < 0, "Failed to handle bad socket");
339 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
340 strerror(errno), errno);
341
342 mark_point();
343 sockfd = devnull_fd();
344 if (sockfd < 0) {
345 return;
346 }
347
348 fd = conn->listen_fd;
349 conn->listen_fd = sockfd;
350 conn->mode = CM_LISTEN;
351 res = pr_inet_set_nonblock(p, conn);
352 ck_assert_msg(res == 0, "Failed to set nonblock on listen fd: %s",
353 strerror(errno));
354 conn->listen_fd = fd;
355
356 mark_point();
357 fd = conn->rfd;
358 conn->rfd = sockfd;
359 conn->mode = CM_NONE;
360 res = pr_inet_set_nonblock(p, conn);
361 ck_assert_msg(res == 0, "Failed to set nonblock on rfd: %s", strerror(errno));
362 conn->rfd = fd;
363
364 mark_point();
365 fd = conn->wfd;
366 conn->wfd = sockfd;
367 conn->mode = CM_NONE;
368 res = pr_inet_set_nonblock(p, conn);
369 ck_assert_msg(res == 0, "Failed to set nonblock on wfd: %s", strerror(errno));
370 conn->wfd = fd;
371
372 (void) close(sockfd);
373 pr_inet_close(p, conn);
374 }
375 END_TEST
376
377 START_TEST (inet_set_proto_cork_test) {
378 int fd = -1, res;
379
380 mark_point();
381 res = pr_inet_set_proto_cork(fd, TRUE);
382 ck_assert_msg(res < 0, "Failed to handle bad socket descriptor");
383 ck_assert_msg(errno == EBADF, "Expectedl EBADF (%d), got '%s' (%d)", EBADF,
384 strerror(errno), errno);
385
386 mark_point();
387 fd = devnull_fd();
388 if (fd < 0) {
389 return;
390 }
391
392 mark_point();
393 res = pr_inet_set_proto_cork(fd, TRUE);
394 ck_assert_msg(res < 0, "Failed to handle bad socket descriptor");
395 ck_assert_msg(errno == ENOTSOCK, "Expected ENOTSOCK (%d), got '%s' (%d)",
396 ENOTSOCK, strerror(errno), errno);
397
398 (void) close(fd);
399 }
400 END_TEST
401
402 START_TEST (inet_set_proto_keepalive_test) {
403 int fd, sockfd, port = INPORT_ANY, res;
404 conn_t *conn;
405 struct tcp_keepalive keepalive;
406
407 mark_point();
408 res = pr_inet_set_proto_keepalive(NULL, NULL, NULL);
409 ck_assert_msg(res < 0, "Failed to handle null pool");
410 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
411 strerror(errno), errno);
412
413 mark_point();
414 res = pr_inet_set_proto_keepalive(p, NULL, NULL);
415 ck_assert_msg(res < 0, "Failed to handle null conn");
416 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
417 strerror(errno), errno);
418
419 mark_point();
420 conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
421 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
422
423 mark_point();
424 res = pr_inet_set_proto_keepalive(p, conn, NULL);
425 ck_assert_msg(res < 0, "Failed to handle null keepalive");
426 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
427 strerror(errno), errno);
428
429 mark_point();
430 conn->listen_fd = -1;
431 keepalive.keepalive_enabled = 1;
432 keepalive.keepalive_idle = 1;
433 keepalive.keepalive_count = 2;
434 keepalive.keepalive_intvl = 3;
435 res = pr_inet_set_proto_keepalive(p, conn, &keepalive);
436 ck_assert_msg(res < 0, "Failed to handle bad listen fd");
437 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
438 strerror(errno), errno);
439
440 mark_point();
441 sockfd = devnull_fd();
442 if (sockfd < 0) {
443 return;
444 }
445
446 fd = conn->listen_fd;
447 conn->listen_fd = sockfd;
448 res = pr_inet_set_proto_keepalive(p, conn, &keepalive);
449 ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
450 conn->listen_fd = fd;
451
452 (void) close(sockfd);
453 pr_inet_close(p, conn);
454 }
455 END_TEST
456
457 START_TEST (inet_set_proto_nodelay_test) {
458 int fd, sockfd, port = INPORT_ANY, res;
459 conn_t *conn;
460
461 mark_point();
462 res = pr_inet_set_proto_nodelay(NULL, NULL, 1);
463 ck_assert_msg(res < 0, "Failed to handle null arguments");
464 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
465 strerror(errno), errno);
466
467 conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
468 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
469
470 mark_point();
471 res = pr_inet_set_proto_nodelay(p, conn, 1);
472 ck_assert_msg(res == 0, "Failed to enable nodelay: %s", strerror(errno));
473
474 mark_point();
475 res = pr_inet_set_proto_nodelay(p, conn, 0);
476 ck_assert_msg(res == 0, "Failed to disable nodelay: %s", strerror(errno));
477
478 fd = conn->rfd;
479 conn->rfd = 8;
480 res = pr_inet_set_proto_nodelay(p, conn, 0);
481 ck_assert_msg(res == 0, "Failed to disable nodelay: %s", strerror(errno));
482 conn->rfd = fd;
483
484 mark_point();
485 sockfd = devnull_fd();
486 if (sockfd < 0) {
487 return;
488 }
489
490 fd = conn->rfd;
491 conn->rfd = sockfd;
492 res = pr_inet_set_proto_nodelay(p, conn, 0);
493 ck_assert_msg(res == 0, "Failed to disable nodelay: %s", strerror(errno));
494 conn->rfd = fd;
495
496 fd = conn->rfd;
497 conn->rfd = -2;
498 res = pr_inet_set_proto_nodelay(p, conn, 0);
499 ck_assert_msg(res == 0, "Failed to disable nodelay: %s", strerror(errno));
500 conn->rfd = fd;
501
502 fd = conn->wfd;
503 conn->wfd = 9;
504 res = pr_inet_set_proto_nodelay(p, conn, 0);
505 ck_assert_msg(res == 0, "Failed to disable nodelay: %s", strerror(errno));
506 conn->wfd = fd;
507
508 fd = conn->wfd;
509 conn->wfd = -3;
510 res = pr_inet_set_proto_nodelay(p, conn, 0);
511 ck_assert_msg(res == 0, "Failed to disable nodelay: %s", strerror(errno));
512 conn->wfd = fd;
513
514 mark_point();
515 fd = conn->wfd;
516 conn->wfd = sockfd;
517 res = pr_inet_set_proto_nodelay(p, conn, 0);
518 ck_assert_msg(res == 0, "Failed to disable nodelay: %s", strerror(errno));
519 conn->wfd = fd;
520
521 (void) close(sockfd);
522 pr_inet_close(p, conn);
523 }
524 END_TEST
525
526 START_TEST (inet_set_proto_opts_test) {
527 int fd, sockfd, port = INPORT_ANY, res;
528 conn_t *conn;
529
530 mark_point();
531 res = pr_inet_set_proto_opts(NULL, NULL, 1, 1, 1, 1);
532 ck_assert_msg(res < 0, "Failed to handle null arguments");
533 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
534 strerror(errno), errno);
535
536 conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
537 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
538
539 mark_point();
540 res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
541 ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
542
543 mark_point();
544 fd = conn->rfd;
545 conn->rfd = 8;
546 res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
547 ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
548 conn->rfd = fd;
549
550 mark_point();
551 sockfd = devnull_fd();
552 if (sockfd < 0) {
553 return;
554 }
555
556 fd = conn->rfd;
557 conn->rfd = sockfd;
558 res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
559 ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
560 conn->rfd = sockfd;
561
562 mark_point();
563 fd = conn->wfd;
564 conn->wfd = 9;
565 res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
566 ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
567 conn->wfd = fd;
568
569 mark_point();
570 fd = conn->wfd;
571 conn->wfd = sockfd;
572 res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
573 ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
574 conn->wfd = fd;
575
576 mark_point();
577 fd = conn->listen_fd;
578 conn->listen_fd = 10;
579 res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
580 ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
581 conn->listen_fd = fd;
582
583 mark_point();
584 fd = conn->listen_fd;
585 conn->listen_fd = sockfd;
586 res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
587 ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
588 conn->listen_fd = fd;
589
590 (void) close(sockfd);
591 pr_inet_close(p, conn);
592 }
593 END_TEST
594
595 START_TEST (inet_set_proto_opts_ipv6_test) {
596 #ifdef PR_USE_IPV6
597 int fd, sockfd = -1, port = INPORT_ANY, res;
598 conn_t *conn;
599 unsigned char use_ipv6;
600
601 use_ipv6 = pr_netaddr_use_ipv6();
602
603 pr_netaddr_enable_ipv6();
604 pr_inet_set_default_family(p, AF_INET6);
605
606 conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
607 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
608
609 mark_point();
610 res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
611 ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
612
613 mark_point();
614 fd = conn->rfd;
615 conn->rfd = 8;
616 res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
617 ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
618 conn->rfd = fd;
619
620 mark_point();
621 fd = conn->wfd;
622 conn->wfd = 9;
623 res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
624 ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
625 conn->wfd = fd;
626
627 mark_point();
628 fd = conn->listen_fd;
629 conn->listen_fd = 10;
630 res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
631 ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
632 conn->listen_fd = fd;
633
634 pr_inet_close(p, conn);
635
636 pr_inet_set_default_family(p, AF_INET);
637 if (use_ipv6 == FALSE) {
638 pr_netaddr_disable_ipv6();
639 }
640 #endif /* PR_USE_IPV6 */
641 }
642 END_TEST
643
644 START_TEST (inet_set_reuse_port_test) {
645 int port = INPORT_ANY, res;
646 conn_t *conn;
647
648 mark_point();
649 res = pr_inet_set_reuse_port(NULL, NULL, 1);
650 ck_assert_msg(res < 0, "Failed to handle null arguments");
651 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
652 strerror(errno), errno);
653
654 mark_point();
655 conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
656 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
657
658 mark_point();
659 res = pr_inet_set_reuse_port(p, conn, -1);
660 ck_assert_msg(res < 0, "Failed to handle invalid arguments");
661 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
662 strerror(errno), errno);
663
664 mark_point();
665 res = pr_inet_set_reuse_port(p, conn, 1);
666 ck_assert_msg(res == 0, "Failed to set reuseport option: %s", strerror(errno));
667
668 mark_point();
669 res = pr_inet_set_reuse_port(p, conn, 0);
670 ck_assert_msg(res == 0, "Failed to set reuseport option: %s", strerror(errno));
671
672 pr_inet_close(p, conn);
673 }
674 END_TEST
675
676 START_TEST (inet_set_socket_opts_test) {
677 int fd, sockfd, port = INPORT_ANY, res;
678 conn_t *conn;
679 struct tcp_keepalive keepalive;
680
681 mark_point();
682 res = pr_inet_set_socket_opts(NULL, NULL, 1, 2, NULL);
683 ck_assert_msg(res < 0, "Failed to handle null arguments");
684 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
685 strerror(errno), errno);
686
687 conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
688 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
689
690 mark_point();
691 res = pr_inet_set_socket_opts(p, conn, 1, 2, NULL);
692 ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
693
694 mark_point();
695 res = pr_inet_set_socket_opts(p, conn, INT_MAX, INT_MAX, NULL);
696 ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
697
698 keepalive.keepalive_enabled = 1;
699 keepalive.keepalive_idle = 1;
700 keepalive.keepalive_count = 2;
701 keepalive.keepalive_intvl = 3;
702 res = pr_inet_set_socket_opts(p, conn, 1, 2, &keepalive);
703 ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
704
705 mark_point();
706 sockfd = devnull_fd();
707 if (sockfd < 0) {
708 return;
709 }
710
711 fd = conn->listen_fd;
712 conn->listen_fd = sockfd;
713 res = pr_inet_set_socket_opts(p, conn, 1, 2, &keepalive);
714 ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
715 conn->listen_fd = fd;
716
717 (void) close(sockfd);
718 pr_inet_close(p, conn);
719 }
720 END_TEST
721
722 START_TEST (inet_set_socket_opts2_test) {
723 int fd, sockfd, port = INPORT_ANY, res;
724 conn_t *conn;
725 struct tcp_keepalive keepalive;
726
727 mark_point();
728 res = pr_inet_set_socket_opts2(NULL, NULL, 1, 2, NULL, -1);
729 ck_assert_msg(res < 0, "Failed to handle null arguments");
730 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
731 strerror(errno), errno);
732
733 conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
734 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
735
736 mark_point();
737 res = pr_inet_set_socket_opts2(p, conn, 1, 2, NULL, -1);
738 ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
739
740 mark_point();
741 res = pr_inet_set_socket_opts2(p, conn, INT_MAX, INT_MAX, NULL, 0);
742 ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
743
744 keepalive.keepalive_enabled = 1;
745 keepalive.keepalive_idle = 1;
746 keepalive.keepalive_count = 2;
747 keepalive.keepalive_intvl = 3;
748 res = pr_inet_set_socket_opts2(p, conn, 1, 2, &keepalive, 1);
749 ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
750
751 mark_point();
752 sockfd = devnull_fd();
753 if (sockfd < 0) {
754 return;
755 }
756
757 fd = conn->listen_fd;
758 conn->listen_fd = sockfd;
759 res = pr_inet_set_socket_opts2(p, conn, 1, 2, &keepalive, 1);
760 ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
761 conn->listen_fd = fd;
762
763 (void) close(sockfd);
764 pr_inet_close(p, conn);
765 }
766 END_TEST
767
768 START_TEST (inet_listen_test) {
769 int fd, mode, sockfd = -1, port = INPORT_ANY, res;
770 conn_t *conn;
771
772 res = pr_inet_listen(NULL, NULL, 5, 0);
773 ck_assert_msg(res < 0, "Failed to handle null arguments");
774 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
775 strerror(errno), errno);
776
777 res = pr_inet_resetlisten(NULL, NULL);
778 ck_assert_msg(res < 0, "Failed to handle null arguments");
779 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
780 strerror(errno), errno);
781
782 conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
783 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
784
785 fd = conn->listen_fd;
786 conn->listen_fd = 777;
787 res = pr_inet_listen(p, conn, 5, 0);
788 ck_assert_msg(res < 0, "Succeeded in listening on conn unexpectedly");
789 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
790 strerror(errno), errno);
791
792 mode = conn->mode;
793 res = pr_inet_resetlisten(p, conn);
794 ck_assert_msg(res < 0, "Succeeded in resetting listening on conn unexpectedly");
795 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
796 strerror(errno), errno);
797
798 conn->listen_fd = fd;
799 conn->mode = mode;
800
801 res = pr_inet_listen(p, conn, 5, 0);
802 ck_assert_msg(res == 0, "Failed to listen on conn: %s", strerror(errno));
803
804 res = pr_inet_resetlisten(p, conn);
805 ck_assert_msg(res == 0, "Failed to reset listen mode: %s", strerror(errno));
806
807 res = pr_inet_listen(p, conn, 5, 0);
808 ck_assert_msg(res < 0, "Failed to handle already-listening socket");
809 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
810 strerror(errno), errno);
811
812 pr_inet_close(p, conn);
813 }
814 END_TEST
815
816 START_TEST (inet_connect_ipv4_test) {
817 int sockfd = -1, port = INPORT_ANY, res;
818 conn_t *conn;
819 const pr_netaddr_t *addr;
820
821 res = pr_inet_connect(NULL, NULL, NULL, port);
822 ck_assert_msg(res < 0, "Failed to handle null arguments");
823 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
824 strerror(errno), errno);
825
826 conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
827 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
828
829 res = pr_inet_connect(p, conn, NULL, 180);
830 ck_assert_msg(res < 0, "Failed to handle null address");
831 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
832 strerror(errno), errno);
833
834 mark_point();
835 addr = pr_netaddr_get_addr(p, "127.0.0.1", NULL);
836 ck_assert_msg(addr != NULL, "Failed to resolve '127.0.0.1': %s",
837 strerror(errno));
838
839 mark_point();
840 res = pr_inet_connect(p, conn, addr, 180);
841 ck_assert_msg(res < 0, "Connected to 127.0.0.1#180 unexpectedly");
842 ck_assert_msg(errno == ECONNREFUSED, "Expected ECONNREFUSED (%d), got %s (%d)",
843 ECONNREFUSED, strerror(errno), errno);
844
845 #if defined(PR_USE_NETWORK_TESTS)
846 addr = pr_netaddr_get_addr(p, dns_resolver, NULL);
847 ck_assert_msg(addr != NULL, "Failed to resolve '%s': %s", dns_resolver,
848 strerror(errno));
849
850 res = pr_inet_connect(p, conn, addr, 53);
851 if (res < 0) {
852 /* Note: We get EINVAL here because the socket already tried (and failed)
853 * to connect to a different address. Interestingly, trying to connect(2)
854 * using that same fd to a different address yields EINVAL.
855 */
856 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
857 strerror(errno), errno);
858 }
859 pr_inet_close(p, conn);
860
861 conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
862 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
863
864 res = pr_inet_connect(p, conn, addr, 53);
865 ck_assert_msg(res >= 0, "Failed to connect to %s#53: %s", dns_resolver,
866 strerror(errno));
867
868 res = pr_inet_connect(p, conn, addr, 53);
869 ck_assert_msg(res < 0, "Failed to connect to %s#53: %s", dns_resolver,
870 strerror(errno));
871 ck_assert_msg(errno == EISCONN, "Expected EISCONN (%d), got %s (%d)",
872 EISCONN, strerror(errno), errno);
873 pr_inet_close(p, conn);
874 #endif
875 }
876 END_TEST
877
878 START_TEST (inet_connect_ipv6_test) {
879 #ifdef PR_USE_IPV6
880 int sockfd = -1, port = INPORT_ANY, res;
881 conn_t *conn;
882 const pr_netaddr_t *addr;
883 unsigned char use_ipv6;
884
885 use_ipv6 = pr_netaddr_use_ipv6();
886
887 pr_netaddr_enable_ipv6();
888 pr_inet_set_default_family(p, AF_INET6);
889
890 conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
891 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
892
893 mark_point();
894 addr = pr_netaddr_get_addr(p, "::1", NULL);
895 ck_assert_msg(addr != NULL, "Failed to resolve '::1': %s",
896 strerror(errno));
897
898 mark_point();
899 res = pr_inet_connect(p, conn, addr, 180);
900 ck_assert_msg(res < 0, "Connected to ::1#180 unexpectedly");
901 ck_assert_msg(errno == ECONNREFUSED || errno == ENETUNREACH || errno == EADDRNOTAVAIL,
902 "Expected ECONNREFUSED (%d), ENETUNREACH (%d), or EADDRNOTAVAIL (%d), got %s (%d)",
903 ECONNREFUSED, ENETUNREACH, EADDRNOTAVAIL, strerror(errno), errno);
904
905 #if defined(PR_USE_NETWORK_TESTS)
906 /* Try connecting to Google's DNS server. */
907
908 addr = pr_netaddr_get_addr(p, "2001:4860:4860::8888", NULL);
909 ck_assert_msg(addr != NULL, "Failed to resolve '2001:4860:4860::8888': %s",
910 strerror(errno));
911
912 res = pr_inet_connect(p, conn, addr, 53);
913 if (res < 0) {
914 /* Note: We get EINVAL here because the socket already tried (and failed)
915 * to connect to a different address. Interestingly, trying to connect(2)
916 * using that same fd to a different address yields EINVAL.
917 */
918 ck_assert_msg(errno == EINVAL || errno == ENETUNREACH || errno == EADDRNOTAVAIL,
919 "Expected EINVAL (%d), ENETUNREACH (%d) or EADDRNOTAVAIL (%d), got %s (%d)",
920 EINVAL, ENETUNREACH, EADDRNOTAVAIL, strerror(errno), errno);
921 }
922 pr_inet_close(p, conn);
923
924 conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
925 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
926
927 res = pr_inet_connect(p, conn, addr, 53);
928 if (res < 0) {
929 /* This could be expected, e.g. if there's no route. */
930 ck_assert_msg(errno == EHOSTUNREACH || errno == ENETUNREACH || errno == EADDRNOTAVAIL,
931 "Expected EHOSTUNREACH (%d) or ENETUNREACH (%d) or EADDRNOTAVAIL (%d), got %s (%d)",
932 EHOSTUNREACH, ENETUNREACH, EADDRNOTAVAIL, strerror(errno), errno);
933 }
934
935 res = pr_inet_connect(p, conn, addr, 53);
936 ck_assert_msg(res < 0, "Failed to connect to 2001:4860:4860::8888#53: %s",
937 strerror(errno));
938 ck_assert_msg(errno == EISCONN || errno == EHOSTUNREACH || errno == ENETUNREACH || errno == EADDRNOTAVAIL,
939 "Expected EISCONN (%d) or EHOSTUNREACH (%d) or ENETUNREACH (%d) or EADDRNOTAVAIL (%d), got %s (%d)", EISCONN, EHOSTUNREACH, ENETUNREACH, EADDRNOTAVAIL, strerror(errno), errno);
940 pr_inet_close(p, conn);
941 #endif
942
943 pr_inet_set_default_family(p, AF_INET);
944
945 if (use_ipv6 == FALSE) {
946 pr_netaddr_disable_ipv6();
947 }
948 #endif /* PR_USE_IPV6 */
949 }
950 END_TEST
951
952 START_TEST (inet_connect_nowait_test) {
953 int sockfd = -1, port = INPORT_ANY, res;
954 conn_t *conn;
955 const pr_netaddr_t *addr;
956
957 res = pr_inet_connect_nowait(NULL, NULL, NULL, port);
958 ck_assert_msg(res < 0, "Failed to handle null arguments");
959 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
960 strerror(errno), errno);
961
962 conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
963 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
964
965 res = pr_inet_connect_nowait(p, conn, NULL, 180);
966 ck_assert_msg(res < 0, "Failed to handle null address");
967 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
968 strerror(errno), errno);
969
970 addr = pr_netaddr_get_addr(p, "127.0.0.1", NULL);
971 ck_assert_msg(addr != NULL, "Failed to resolve '127.0.0.1': %s",
972 strerror(errno));
973
974 res = pr_inet_connect_nowait(p, conn, addr, 180);
975 ck_assert_msg(res != -1, "Connected to 127.0.0.1#180 unexpectedly");
976
977 #if defined(PR_USE_NETWORK_TESTS)
978 /* Try connecting to Google's DNS server. */
979
980 addr = pr_netaddr_get_addr(p, dns_resolver, NULL);
981 ck_assert_msg(addr != NULL, "Failed to resolve '%s': %s", dns_resolver,
982 strerror(errno));
983
984 res = pr_inet_connect_nowait(p, conn, addr, 53);
985 if (res < 0 &&
986 errno != ECONNREFUSED &&
987 errno != EBADF) {
988 ck_assert_msg(res != -1, "Failed to connect to %s#53: %s", dns_resolver,
989 strerror(errno));
990 }
991
992 pr_inet_close(p, conn);
993 #endif
994
995 /* Restore the default family to AF_INET, for other tests. */
996 pr_inet_set_default_family(p, AF_INET);
997 }
998 END_TEST
999
1000 START_TEST (inet_accept_test) {
1001 conn_t *conn;
1002
1003 conn = pr_inet_accept(NULL, NULL, NULL, 0, 2, FALSE);
1004 ck_assert_msg(conn == NULL, "Failed to handle null arguments");
1005 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1006 strerror(errno), errno);
1007 }
1008 END_TEST
1009
1010 START_TEST (inet_accept_nowait_test) {
1011 int fd, sockfd, port = INPORT_ANY, res;
1012 conn_t *conn;
1013
1014 mark_point();
1015 res = pr_inet_accept_nowait(NULL, NULL);
1016 ck_assert_msg(res < 0, "Failed to handle null arguments");
1017 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1018 strerror(errno), errno);
1019
1020 conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
1021 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1022
1023 mark_point();
1024 res = pr_inet_accept_nowait(p, conn);
1025 ck_assert_msg(res < 0, "Accepted connection unexpectedly");
1026 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1027 strerror(errno), errno);
1028
1029 mark_point();
1030 sockfd = devnull_fd();
1031 if (sockfd < 0) {
1032 return;
1033 }
1034
1035 fd = conn->listen_fd;
1036 conn->listen_fd = sockfd;
1037 conn->mode = CM_LISTEN;
1038 res = pr_inet_accept_nowait(p, conn);
1039 ck_assert_msg(res < 0, "Failed to handle non-socket");
1040 ck_assert_msg(errno == ENOTSOCK, "Expected ENOTSOCK (%d), got %s (%d)",
1041 ENOTSOCK, strerror(errno), errno);
1042 conn->listen_fd = fd;
1043
1044 (void) close(sockfd);
1045 pr_inet_close(p, conn);
1046 }
1047 END_TEST
1048
1049 START_TEST (inet_conn_info_test) {
1050 int sockfd = -1, port = INPORT_ANY, res;
1051 conn_t *conn;
1052
1053 res = pr_inet_get_conn_info(NULL, -1);
1054 ck_assert_msg(res < 0, "Failed to handle null arguments");
1055 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1056 strerror(errno), errno);
1057
1058 conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
1059 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1060
1061 res = pr_inet_get_conn_info(conn, -1);
1062 ck_assert_msg(res < 0, "Failed to handle bad file descriptor");
1063 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
1064 strerror(errno), errno);
1065
1066 res = pr_inet_get_conn_info(conn, 1);
1067 ck_assert_msg(res < 0, "Failed to handle bad file descriptor");
1068 ck_assert_msg(errno == ENOTSOCK, "Expected ENOTSOCK (%d), got %s (%d)",
1069 ENOTSOCK, strerror(errno), errno);
1070
1071 pr_inet_close(p, conn);
1072 }
1073 END_TEST
1074
1075 START_TEST (inet_openrw_test) {
1076 int fd, sockfd = -1, port = INPORT_ANY;
1077 conn_t *conn, *res;
1078 const pr_netaddr_t *addr;
1079
1080 mark_point();
1081 res = pr_inet_openrw(NULL, NULL, NULL, PR_NETIO_STRM_CTRL, -1, -1, -1, FALSE);
1082 ck_assert_msg(res == NULL, "Failed to handle null arguments");
1083 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1084 strerror(errno), errno);
1085
1086 conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
1087 ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1088
1089 mark_point();
1090 res = pr_inet_openrw(p, conn, NULL, PR_NETIO_STRM_CTRL, -1, -1, -1, FALSE);
1091 ck_assert_msg(res == NULL, "Opened rw conn unexpectedly");
1092 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1093 strerror(errno), errno);
1094
1095 addr = pr_netaddr_get_addr(p, "127.0.0.1", NULL);
1096 ck_assert_msg(addr != NULL, "Failed to resolve 127.0.0.1: %s", strerror(errno));
1097
1098 mark_point();
1099 res = pr_inet_openrw(p, conn, addr, PR_NETIO_STRM_CTRL, -1, -1, -1, FALSE);
1100 ck_assert_msg(res != NULL, "Failed to open rw conn: %s", strerror(errno));
1101 (void) pr_inet_close(p, res);
1102
1103 mark_point();
1104 res = pr_inet_openrw(p, conn, addr, PR_NETIO_STRM_CTRL, -1, -1, -1, TRUE);
1105 ck_assert_msg(res != NULL, "Failed to open rw conn: %s", strerror(errno));
1106
1107 mark_point();
1108 fd = devnull_fd();
1109 if (fd < 0) {
1110 return;
1111 }
1112 res = pr_inet_openrw(p, conn, addr, PR_NETIO_STRM_CTRL, fd, -1, -1, TRUE);
1113 ck_assert_msg(res == NULL, "Failed to handle non-socket");
1114 ck_assert_msg(errno == ENOTSOCK, "Expected ENOTSOCK (%d), got %s (%d)",
1115 ENOTSOCK, strerror(errno), errno);
1116
1117 (void) close(fd);
1118 }
1119 END_TEST
1120
1121 START_TEST (inet_generate_socket_event_test) {
1122 int res;
1123 const char *name;
1124 server_rec *s;
1125
1126 res = pr_inet_generate_socket_event(NULL, NULL, NULL, -1);
1127 ck_assert_msg(res < 0, "Failed to handle null arguments");
1128 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1129 strerror(errno), errno);
1130
1131 name = "foo.bar";
1132 res = pr_inet_generate_socket_event(name, NULL, NULL, -1);
1133 ck_assert_msg(res < 0, "Failed to handle null server_rec");
1134 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1135 strerror(errno), errno);
1136
1137 s = pcalloc(p, sizeof(server_rec));
1138 res = pr_inet_generate_socket_event(name, s, NULL, -1);
1139 ck_assert_msg(res < 0, "Failed to handle null address");
1140 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1141 strerror(errno), errno);
1142 }
1143 END_TEST
1144
1145 START_TEST (inet_allowforeignaddress_default_test) {
1146 int res;
1147 const pr_netaddr_t *proposed_addr, *actual_addr;
1148 const char *name;
1149
1150 mark_point();
1151 res = pr_inet_allowforeignaddress(NULL, NULL, NULL, NULL);
1152 ck_assert_msg(res < 0, "Failed to handle null pool");
1153 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1154 strerror(errno), errno);
1155
1156 mark_point();
1157 res = pr_inet_allowforeignaddress(p, NULL, NULL, NULL);
1158 ck_assert_msg(res < 0, "Failed to handle null proposed address");
1159 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1160 strerror(errno), errno);
1161
1162 /* First, use matching addresses. */
1163 name = "127.0.0.1";
1164 proposed_addr = pr_netaddr_get_addr(p, name, NULL);
1165 ck_assert_msg(proposed_addr != NULL, "Failed to resolve '%s': %s",
1166 name, strerror(errno));
1167
1168 mark_point();
1169 res = pr_inet_allowforeignaddress(p, proposed_addr, proposed_addr, NULL);
1170 ck_assert_msg(res == 1, "Address not allowed as expected (%d)", res);
1171
1172 /* Now, use mismatching addresses. */
1173
1174 name = "127.0.0.2";
1175 actual_addr = pr_netaddr_get_addr(p, name, NULL);
1176 ck_assert_msg(actual_addr != NULL, "Failed to resolve '%s': %s",
1177 name, strerror(errno));
1178
1179 mark_point();
1180 res = pr_inet_allowforeignaddress(p, proposed_addr, actual_addr, NULL);
1181 ck_assert_msg(res == 0, "Address not denied as expected (%d)", res);
1182 }
1183 END_TEST
1184
1185 START_TEST (inet_allowforeignaddress_off_test) {
1186 int res;
1187 config_rec *c;
1188 const pr_netaddr_t *proposed_addr, *actual_addr;
1189 const char *name;
1190
1191 c = pcalloc(p, sizeof(config_rec));
1192 c->argc = 2;
1193 c->argv = pcalloc(p, sizeof(void *) * (c->argc + 1));
1194 c->argv[0] = palloc(p, sizeof(int));
1195 *((int *) c->argv[0]) = FALSE;
1196
1197 /* First, use matching addresses. */
1198 name = "127.0.0.1";
1199 proposed_addr = pr_netaddr_get_addr(p, name, NULL);
1200 ck_assert_msg(proposed_addr != NULL, "Failed to resolve '%s': %s",
1201 name, strerror(errno));
1202
1203 mark_point();
1204 res = pr_inet_allowforeignaddress(p, proposed_addr, proposed_addr, c);
1205 ck_assert_msg(res == 1, "Address not allowed as expected (%d)", res);
1206
1207 /* Now, use mismatching addresses. */
1208
1209 name = "127.0.0.2";
1210 actual_addr = pr_netaddr_get_addr(p, name, NULL);
1211 ck_assert_msg(actual_addr != NULL, "Failed to resolve '%s': %s",
1212 name, strerror(errno));
1213
1214 mark_point();
1215 res = pr_inet_allowforeignaddress(p, proposed_addr, actual_addr, c);
1216 ck_assert_msg(res == 0, "Address not denied as expected (%d)", res);
1217 }
1218 END_TEST
1219
1220 START_TEST (inet_allowforeignaddress_on_test) {
1221 int res;
1222 config_rec *c;
1223 const pr_netaddr_t *proposed_addr, *actual_addr;
1224 const char *name;
1225
1226 c = pcalloc(p, sizeof(config_rec));
1227 c->argc = 2;
1228 c->argv = pcalloc(p, sizeof(void *) * (c->argc + 1));
1229 c->argv[0] = palloc(p, sizeof(int));
1230 *((int *) c->argv[0]) = TRUE;
1231
1232 /* First, use matching addresses. */
1233 name = "127.0.0.1";
1234 proposed_addr = pr_netaddr_get_addr(p, name, NULL);
1235 ck_assert_msg(proposed_addr != NULL, "Failed to resolve '%s': %s",
1236 name, strerror(errno));
1237
1238 mark_point();
1239 res = pr_inet_allowforeignaddress(p, proposed_addr, proposed_addr, c);
1240 ck_assert_msg(res == 1, "Address not allowed as expected (%d)", res);
1241
1242 /* Now, use mismatching addresses. */
1243
1244 name = "127.0.0.2";
1245 actual_addr = pr_netaddr_get_addr(p, name, NULL);
1246 ck_assert_msg(actual_addr != NULL, "Failed to resolve '%s': %s",
1247 name, strerror(errno));
1248
1249 mark_point();
1250 res = pr_inet_allowforeignaddress(p, proposed_addr, actual_addr, c);
1251 ck_assert_msg(res == 1, "Address not allowed as expected (%d)", res);
1252 }
1253 END_TEST
1254
1255 START_TEST (inet_allowforeignaddress_matching_class_test) {
1256 int res;
1257 config_rec *c;
1258 const pr_netaddr_t *proposed_addr, *actual_addr;
1259 const char *name, *class_name;
1260 pr_netacl_t *acl;
1261
1262 mark_point();
1263 class_name = "test-class";
1264 res = pr_class_open(p, class_name);
1265 ck_assert_msg(res == 0, "Failed to open '%s' class: %s", class_name,
1266 strerror(errno));
1267
1268 acl = pr_netacl_create(p, "all");
1269 ck_assert_msg(acl != NULL, "Failed to create 'all' ACL: %s", strerror(errno));
1270
1271 res = pr_class_add_acl(acl);
1272 ck_assert_msg(res == 0, "Failed to add ACL to '%s' class: %s", class_name,
1273 strerror(errno));
1274
1275 res = pr_class_close();
1276 ck_assert_msg(res == 0, "Failed to close '%s' class: %s", class_name,
1277 strerror(errno));
1278
1279 c = pcalloc(p, sizeof(config_rec));
1280 c->argc = 2;
1281 c->argv = pcalloc(p, sizeof(void *) * (c->argc + 1));
1282 c->argv[0] = palloc(p, sizeof(int));
1283 *((int *) c->argv[0]) = -1;
1284 c->argv[1] = pstrdup(p, class_name);
1285
1286 /* First, use matching addresses. */
1287 name = "127.0.0.1";
1288 proposed_addr = pr_netaddr_get_addr(p, name, NULL);
1289 ck_assert_msg(proposed_addr != NULL, "Failed to resolve '%s': %s",
1290 name, strerror(errno));
1291
1292 mark_point();
1293 res = pr_inet_allowforeignaddress(p, proposed_addr, proposed_addr, c);
1294 ck_assert_msg(res == 1, "Address not allowed as expected (%d)", res);
1295
1296 /* Now, use mismatching addresses. */
1297
1298 name = "127.0.0.2";
1299 actual_addr = pr_netaddr_get_addr(p, name, NULL);
1300 ck_assert_msg(actual_addr != NULL, "Failed to resolve '%s': %s",
1301 name, strerror(errno));
1302
1303 mark_point();
1304 res = pr_inet_allowforeignaddress(p, proposed_addr, actual_addr, c);
1305 ck_assert_msg(res == 1, "Address not allowed as expected (%d)", res);
1306 }
1307 END_TEST
1308
1309 START_TEST (inet_allowforeignaddress_mismatching_class_test) {
1310 int res;
1311 config_rec *c;
1312 const pr_netaddr_t *proposed_addr, *actual_addr;
1313 const char *name, *class_name;
1314 pr_netacl_t *acl;
1315
1316 mark_point();
1317 class_name = "test-class";
1318 res = pr_class_open(p, class_name);
1319 ck_assert_msg(res == 0, "Failed to open '%s' class: %s", class_name,
1320 strerror(errno));
1321
1322 acl = pr_netacl_create(p, "none");
1323 ck_assert_msg(acl != NULL, "Failed to create 'none' ACL: %s",
1324 strerror(errno));
1325
1326 res = pr_class_add_acl(acl);
1327 ck_assert_msg(res == 0, "Failed to add ACL to '%s' class: %s", class_name,
1328 strerror(errno));
1329
1330 res = pr_class_close();
1331 ck_assert_msg(res == 0, "Failed to close '%s' class: %s", class_name,
1332 strerror(errno));
1333
1334 c = pcalloc(p, sizeof(config_rec));
1335 c->argc = 2;
1336 c->argv = pcalloc(p, sizeof(void *) * (c->argc + 1));
1337 c->argv[0] = palloc(p, sizeof(int));
1338 *((int *) c->argv[0]) = -1;
1339 c->argv[1] = pstrdup(p, class_name);
1340
1341 /* First, use matching addresses. */
1342 name = "127.0.0.1";
1343 proposed_addr = pr_netaddr_get_addr(p, name, NULL);
1344 ck_assert_msg(proposed_addr != NULL, "Failed to resolve '%s': %s",
1345 name, strerror(errno));
1346
1347 mark_point();
1348 res = pr_inet_allowforeignaddress(p, proposed_addr, proposed_addr, c);
1349 ck_assert_msg(res == 1, "Address not allowed as expected (%d)", res);
1350
1351 /* Now, use mismatching addresses. */
1352
1353 name = "127.0.0.2";
1354 actual_addr = pr_netaddr_get_addr(p, name, NULL);
1355 ck_assert_msg(actual_addr != NULL, "Failed to resolve '%s': %s",
1356 name, strerror(errno));
1357
1358 mark_point();
1359 res = pr_inet_allowforeignaddress(p, proposed_addr, actual_addr, c);
1360 ck_assert_msg(res == 0, "Address not denied as expected (%d)", res);
1361 }
1362 END_TEST
1363
1364 START_TEST (inet_allowforeignaddress_unknown_class_test) {
1365 int res;
1366 config_rec *c;
1367 const pr_netaddr_t *proposed_addr, *actual_addr;
1368 const char *name, *class_name;
1369 pr_netacl_t *acl;
1370
1371 mark_point();
1372 class_name = "test-class";
1373
1374 c = pcalloc(p, sizeof(config_rec));
1375 c->argc = 2;
1376 c->argv = pcalloc(p, sizeof(void *) * (c->argc + 1));
1377 c->argv[0] = palloc(p, sizeof(int));
1378 *((int *) c->argv[0]) = -1;
1379 c->argv[1] = pstrdup(p, class_name);
1380
1381 /* First, use matching addresses. */
1382 name = "127.0.0.1";
1383 proposed_addr = pr_netaddr_get_addr(p, name, NULL);
1384 ck_assert_msg(proposed_addr != NULL, "Failed to resolve '%s': %s",
1385 name, strerror(errno));
1386
1387 mark_point();
1388 res = pr_inet_allowforeignaddress(p, proposed_addr, proposed_addr, c);
1389 ck_assert_msg(res == 1, "Address not allowed as expected (%d)", res);
1390
1391 /* Now, use mismatching addresses. */
1392
1393 name = "127.0.0.2";
1394 actual_addr = pr_netaddr_get_addr(p, name, NULL);
1395 ck_assert_msg(actual_addr != NULL, "Failed to resolve '%s': %s",
1396 name, strerror(errno));
1397
1398 mark_point();
1399 res = pr_inet_allowforeignaddress(p, proposed_addr, actual_addr, c);
1400 ck_assert_msg(res == 0, "Address not denied as expected (%d)", res);
1401 }
1402 END_TEST
1403
1404 Suite *tests_get_inet_suite(void) {
1405 Suite *suite;
1406 TCase *testcase;
1407
1408 suite = suite_create("inet");
1409
1410 testcase = tcase_create("base");
1411 tcase_add_checked_fixture(testcase, set_up, tear_down);
1412
1413 tcase_add_test(testcase, inet_family_test);
1414 tcase_add_test(testcase, inet_getservport_test);
1415 tcase_add_test(testcase, inet_close_test);
1416 tcase_add_test(testcase, inet_create_conn_test);
1417 tcase_add_test(testcase, inet_create_conn_portrange_test);
1418 tcase_add_test(testcase, inet_copy_conn_test);
1419 tcase_add_test(testcase, inet_set_async_test);
1420 tcase_add_test(testcase, inet_set_block_test);
1421 tcase_add_test(testcase, inet_set_proto_cork_test);
1422 tcase_add_test(testcase, inet_set_proto_keepalive_test);
1423 tcase_add_test(testcase, inet_set_proto_nodelay_test);
1424 tcase_add_test(testcase, inet_set_proto_opts_test);
1425 tcase_add_test(testcase, inet_set_proto_opts_ipv6_test);
1426 tcase_add_test(testcase, inet_set_reuse_port_test);
1427 tcase_add_test(testcase, inet_set_socket_opts_test);
1428 tcase_add_test(testcase, inet_set_socket_opts2_test);
1429 tcase_add_test(testcase, inet_listen_test);
1430 tcase_add_test(testcase, inet_connect_ipv4_test);
1431 tcase_add_test(testcase, inet_connect_ipv6_test);
1432 tcase_add_test(testcase, inet_connect_nowait_test);
1433 tcase_add_test(testcase, inet_accept_test);
1434 tcase_add_test(testcase, inet_accept_nowait_test);
1435 tcase_add_test(testcase, inet_conn_info_test);
1436 tcase_add_test(testcase, inet_openrw_test);
1437 tcase_add_test(testcase, inet_generate_socket_event_test);
1438 tcase_add_test(testcase, inet_allowforeignaddress_default_test);
1439 tcase_add_test(testcase, inet_allowforeignaddress_off_test);
1440 tcase_add_test(testcase, inet_allowforeignaddress_on_test);
1441 tcase_add_test(testcase, inet_allowforeignaddress_matching_class_test);
1442 tcase_add_test(testcase, inet_allowforeignaddress_mismatching_class_test);
1443 tcase_add_test(testcase, inet_allowforeignaddress_unknown_class_test);
1444
1445 suite_add_tcase(suite, testcase);
1446 return suite;
1447 }