• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

proftpd / proftpd / 28259826111

26 Jun 2026 07:20PM UTC coverage: 93.032% (+0.6%) from 92.469%
28259826111

push

github

51363 of 55210 relevant lines covered (93.03%)

200.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.25
/tests/api/inet.c
1
/*
2
 * ProFTPD - FTP server testsuite
3
 * Copyright (c) 2014-2026 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, see <https://www.gnu.org/licenses/>.
17
 *
18
 * As a special exemption, The ProFTPD Project team and other respective
19
 * copyright holders give permission to link this program with OpenSSL, and
20
 * distribute the resulting executable, without including the source code for
21
 * OpenSSL in the source distribution.
22
 */
23

24
/* Inet API tests */
25

26
#include "tests.h"
27

28
static pool *p = NULL;
29

30
/* Use Google's DNS resolvers by default. */
31
static const char *dns_resolver = "8.8.8.8";
32

33
static void set_up(void) {
34
  const char *use_resolver = NULL;
25✔
35

25✔
36
  if (p == NULL) {
37
    p = permanent_pool = make_sub_pool(NULL);
25✔
38
  }
25✔
39

40
  init_netaddr();
41
  init_netio();
25✔
42
  init_inet();
25✔
43

25✔
44
  use_resolver = getenv("PR_USE_DNS_RESOLVER");
45
  if (use_resolver != NULL) {
25✔
46
    dns_resolver = use_resolver;
25✔
47
  }
×
48

49
  if (getenv("TEST_VERBOSE") != NULL) {
50
    pr_trace_set_levels("inet", 1, 20);
25✔
51
  }
25✔
52

53
  pr_inet_set_default_family(p, AF_INET);
54
}
25✔
55

25✔
56
static void tear_down(void) {
57
  if (getenv("TEST_VERBOSE") != NULL) {
25✔
58
    pr_trace_set_levels("inet", 0, 0);
25✔
59
  }
25✔
60

61
  pr_inet_set_default_family(p, 0);
62
  pr_inet_clear();
25✔
63

25✔
64
  if (p) {
65
    destroy_pool(p);
25✔
66
    p = permanent_pool = NULL;
25✔
67
  }
25✔
68
}
69

25✔
70
static int devnull_fd(void) {
71
  int fd;
9✔
72

9✔
73
  fd = open("/dev/null", O_RDWR);
74
  if (fd < 0) {
9✔
75
    fprintf(stderr, "Error opening /dev/null: %s\n", strerror(errno));
9✔
76
    return -1;
×
77
  }
×
78

79
  return fd;
80
}
81

82
/* Tests */
83

84
START_TEST (inet_family_test) {
85
  int res;
1✔
86

1✔
87
  pr_inet_set_default_family(p, 0);
88

1✔
89
  res = pr_inet_set_default_family(p, AF_INET);
90
  ck_assert_msg(res == 0, "Expected previous family 0, got %d", res);
1✔
91

1✔
92
  res = pr_inet_set_default_family(p, 0);
93
  ck_assert_msg(res == AF_INET, "Expected previous family %d, got %d", AF_INET,
1✔
94
    res);
1✔
95

96
  /* Restore the default family to AF_INET, for other tests. */
97
  pr_inet_set_default_family(p, AF_INET);
98
}
1✔
99
END_TEST
1✔
100

101
START_TEST (inet_getservport_test) {
102
  int res;
1✔
103

1✔
104
  mark_point();
105
  res = pr_inet_getservport(NULL, NULL, NULL);
1✔
106
  ck_assert_msg(res < 0, "Failed to handle null service");
1✔
107
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d) got %s (%d)", EINVAL,
1✔
108
    strerror(errno), errno);
1✔
109

110
  mark_point();
111
  res = pr_inet_getservport(p, "ftp", NULL);
1✔
112
  ck_assert_msg(res > 0, "Failed to handle known service");
1✔
113

1✔
114
  mark_point();
115
  res = pr_inet_getservport(p, "ftp", "tcp");
1✔
116
  ck_assert_msg(res > 0, "Failed to handle service 'ftp', proto 'tcp': %s",
1✔
117
    strerror(errno));
1✔
118

119
  mark_point();
120
  res = pr_inet_getservport(p, "foobarbaz", "quxxquzz");
1✔
121

1✔
122
  /* Different platforms/implementations handle this differently. */
123
  if (res < 0 &&
124
      errno != 0) {
1✔
125
    ck_assert_msg(errno == EINVAL || errno == ENOENT,
1✔
126
      "Expected EINVAL (%d) or ENOENT (%d), got %s (%d)", EINVAL, ENOENT,
×
127
      strerror(errno), errno);
128
  }
129
}
130
END_TEST
1✔
131

132
START_TEST (inet_close_test) {
133
  mark_point();
1✔
134
  pr_inet_close(NULL, NULL);
1✔
135

1✔
136
  mark_point();
137
  pr_inet_close(p, NULL);
1✔
138
}
1✔
139
END_TEST
1✔
140

141
START_TEST (inet_create_conn_test) {
142
  int sockfd = -2, port = INPORT_ANY;
1✔
143
  conn_t *conn, *conn2;
1✔
144

1✔
145
  mark_point();
146
  conn = pr_inet_create_conn(NULL, sockfd, NULL, port, FALSE);
1✔
147
  ck_assert_msg(conn == NULL, "Failed to handle null arguments");
1✔
148
  ck_assert_msg(errno == EINVAL,
1✔
149
    "Failed to set errno to EINVAL (%d), got '%s' (%d)", EINVAL,
1✔
150
    strerror(errno), errno);
151

152
  mark_point();
153
  conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
1✔
154
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
155
  ck_assert_msg(conn->listen_fd == sockfd, "Expected listen_fd %d, got %d",
1✔
156
    sockfd, conn->listen_fd);
1✔
157
  pr_inet_close(p, conn);
158

1✔
159
  sockfd = -1;
160
  conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
1✔
161
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
162
  ck_assert_msg(conn->listen_fd != sockfd,
1✔
163
    "Expected listen_fd other than %d, got %d",
1✔
164
    sockfd, conn->listen_fd);
165

166
  /* Create another conn, with the same port, make sure it fails. */
167
  conn2 = pr_inet_create_conn(p, sockfd, NULL, conn->local_port, FALSE);
168
  if (conn2 == NULL) {
1✔
169
    ck_assert_msg(errno == EADDRINUSE, "Expected EADDRINUSE (%d), got %s (%d)",
1✔
170
      EADDRINUSE, strerror(errno), errno);
×
171
    pr_inet_close(p, conn2);
172
  }
×
173

174
  pr_inet_close(p, conn);
175
}
1✔
176
END_TEST
1✔
177

178
START_TEST (inet_create_conn_portrange_test) {
179
  conn_t *conn;
1✔
180

1✔
181
  conn = pr_inet_create_conn_portrange(NULL, NULL, -1, -1);
182
  ck_assert_msg(conn == NULL, "Failed to handle negative ports");
1✔
183
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
184
    strerror(errno), errno);
1✔
185

186
  conn = pr_inet_create_conn_portrange(NULL, NULL, 10, 1);
187
  ck_assert_msg(conn == NULL, "Failed to handle bad ports");
1✔
188
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
1✔
189
    strerror(errno), errno);
1✔
190

191
  conn = pr_inet_create_conn_portrange(p, NULL, 49152, 65534);
192
  ck_assert_msg(conn != NULL, "Failed to create conn in portrange: %s",
1✔
193
    strerror(errno));
1✔
194
  pr_inet_lingering_close(p, conn, 0L);
195
}
1✔
196
END_TEST
1✔
197

198
START_TEST (inet_copy_conn_test) {
199
  int fd = -1, sockfd = -1, port = INPORT_ANY;
1✔
200
  conn_t *conn, *conn2;
1✔
201
  const char *name;
1✔
202

1✔
203
  conn = pr_inet_copy_conn(NULL, NULL);
204
  ck_assert_msg(conn == NULL, "Failed to handle null arguments");
1✔
205
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
206
    strerror(errno), errno);
1✔
207

208
  conn = pr_inet_copy_conn(p, NULL);
209
  ck_assert_msg(conn == NULL, "Failed to handle null conn argument");
1✔
210
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
211
    strerror(errno), errno);
1✔
212

213
  conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
214
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
215

1✔
216
  conn2 = pr_inet_copy_conn(p, conn);
217
  ck_assert_msg(conn2 != NULL, "Failed to copy conn: %s", strerror(errno));
1✔
218

1✔
219
  pr_inet_close(p, conn);
220
  pr_inet_close(p, conn2);
1✔
221

1✔
222
  conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
223
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
224

1✔
225
  name = "127.0.0.1";
226
  conn->remote_addr = pr_netaddr_get_addr(p, name, NULL);
1✔
227
  ck_assert_msg(conn->remote_addr != NULL, "Failed to resolve '%s': %s",
1✔
228
    name, strerror(errno));
1✔
229
  conn->remote_name = pstrdup(p, name);
230
  conn->instrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
1✔
231
  ck_assert_msg(conn->instrm != NULL, "Failed to open ctrl reading stream: %s",
1✔
232
    strerror(errno));
1✔
233
  conn->outstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_WR);
234
  ck_assert_msg(conn->instrm != NULL, "Failed to open ctrl writing stream: %s",
1✔
235
    strerror(errno));
1✔
236

237
  conn2 = pr_inet_copy_conn(p, conn);
238
  ck_assert_msg(conn2 != NULL, "Failed to copy conn: %s", strerror(errno));
1✔
239

1✔
240
  mark_point();
241
  pr_inet_lingering_close(NULL, NULL, 0L);
1✔
242

1✔
243
  pr_inet_lingering_close(p, conn, 0L);
244
  pr_inet_close(p, conn2);
1✔
245

1✔
246
  conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
247
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
248

1✔
249
  conn->instrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
250
  ck_assert_msg(conn->instrm != NULL, "Failed to open ctrl reading stream: %s",
1✔
251
    strerror(errno));
1✔
252
  conn->outstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_WR);
253
  ck_assert_msg(conn->instrm != NULL, "Failed to open ctrl writing stream: %s",
1✔
254
    strerror(errno));
1✔
255

256
  mark_point();
257
  pr_inet_lingering_abort(NULL, NULL, 0L);
1✔
258

1✔
259
  pr_inet_lingering_abort(p, conn, 0L);
260
}
1✔
261
END_TEST
1✔
262

263
START_TEST (inet_set_async_test) {
264
  int fd, sockfd = -1, port = INPORT_ANY, res;
1✔
265
  conn_t *conn;
1✔
266

1✔
267
  res = pr_inet_set_async(NULL, NULL);
268
  ck_assert_msg(res < 0, "Failed to handle null arguments");
1✔
269
  ck_assert_msg(errno == EINVAL, "Expected errno EINVAL (%d), got '%s' (%d)",
1✔
270
    EINVAL, strerror(errno), errno);
1✔
271

272
  conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
273
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
274

1✔
275
  res = pr_inet_set_async(p, conn);
276
  ck_assert_msg(res == 0, "Failed to set conn %p async: %s", conn,
1✔
277
    strerror(errno));
1✔
278

279
  fd = conn->rfd;
280
  conn->rfd = 77;
1✔
281
  res = pr_inet_set_async(p, conn);
1✔
282
  ck_assert_msg(res == 0, "Failed to set conn %p async: %s", conn,
1✔
283
    strerror(errno));
1✔
284
  conn->rfd = fd;
285

1✔
286
  fd = conn->wfd;
287
  conn->wfd = 78;
1✔
288
  res = pr_inet_set_async(p, conn);
1✔
289
  ck_assert_msg(res == 0, "Failed to set conn %p async: %s", conn,
1✔
290
    strerror(errno));
1✔
291
  conn->wfd = fd;
292

1✔
293
  fd = conn->listen_fd;
294
  conn->listen_fd = 79;
1✔
295
  res = pr_inet_set_async(p, conn);
1✔
296
  ck_assert_msg(res == 0, "Failed to set conn %p async: %s", conn,
1✔
297
    strerror(errno));
1✔
298
  conn->listen_fd = fd;
299

1✔
300
  pr_inet_close(p, conn);
301
}
1✔
302
END_TEST
1✔
303

304
START_TEST (inet_set_block_test) {
305
  int fd, sockfd, port = INPORT_ANY, res;
1✔
306
  conn_t *conn;
1✔
307

1✔
308
  mark_point();
309
  res = pr_inet_set_block(NULL, NULL);
1✔
310
  ck_assert_msg(res < 0, "Failed to handle null arguments");
1✔
311
  ck_assert_msg(errno == EINVAL, "Expected errno EINVAL (%d), got '%s' (%d)",
1✔
312
    EINVAL, strerror(errno), errno);
1✔
313

314
  mark_point();
315
  res = pr_inet_set_nonblock(NULL, NULL);
1✔
316
  ck_assert_msg(res < 0, "Failed to handle null arguments");
1✔
317
  ck_assert_msg(errno == EINVAL, "Expected errno EINVAL (%d), got '%s' (%d)",
1✔
318
    EINVAL, strerror(errno), errno);
1✔
319

320
  mark_point();
321
  conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
1✔
322
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
323

1✔
324
  res = pr_inet_set_nonblock(p, conn);
325
  ck_assert_msg(res < 0, "Failed to handle bad socket");
1✔
326
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
1✔
327
    strerror(errno), errno);
1✔
328

329
  res = pr_inet_set_block(p, conn);
330
  ck_assert_msg(res < 0, "Failed to handle bad socket");
1✔
331
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
1✔
332
    strerror(errno), errno);
1✔
333

334
  mark_point();
335
  sockfd = devnull_fd();
1✔
336
  if (sockfd < 0) {
1✔
337
    return;
1✔
338
  }
339

340
  fd = conn->listen_fd;
341
  conn->listen_fd = sockfd;
1✔
342
  conn->mode = CM_LISTEN;
1✔
343
  res = pr_inet_set_nonblock(p, conn);
1✔
344
  ck_assert_msg(res == 0, "Failed to set nonblock on listen fd: %s",
1✔
345
    strerror(errno));
1✔
346
  conn->listen_fd = fd;
347

1✔
348
  mark_point();
349
  fd = conn->rfd;
1✔
350
  conn->rfd = sockfd;
1✔
351
  conn->mode = CM_NONE;
1✔
352
  res = pr_inet_set_nonblock(p, conn);
1✔
353
  ck_assert_msg(res == 0, "Failed to set nonblock on rfd: %s", strerror(errno));
1✔
354
  conn->rfd = fd;
1✔
355

1✔
356
  mark_point();
357
  fd = conn->wfd;
1✔
358
  conn->wfd = sockfd;
1✔
359
  conn->mode = CM_NONE;
1✔
360
  res = pr_inet_set_nonblock(p, conn);
1✔
361
  ck_assert_msg(res == 0, "Failed to set nonblock on wfd: %s", strerror(errno));
1✔
362
  conn->wfd = fd;
1✔
363

1✔
364
  (void) close(sockfd);
365
  pr_inet_close(p, conn);
1✔
366
}
1✔
367
END_TEST
368

369
START_TEST (inet_set_proto_cork_test) {
370
  int fd = -1, res;
1✔
371

1✔
372
  mark_point();
373
  res = pr_inet_set_proto_cork(fd, TRUE);
1✔
374
  ck_assert_msg(res < 0, "Failed to handle bad socket descriptor");
1✔
375
  ck_assert_msg(errno == EBADF, "Expectedl EBADF (%d), got '%s' (%d)", EBADF,
1✔
376
    strerror(errno), errno);
1✔
377

378
  mark_point();
379
  fd = devnull_fd();
1✔
380
  if (fd < 0) {
1✔
381
    return;
1✔
382
  }
383

384
  mark_point();
385
  res = pr_inet_set_proto_cork(fd, TRUE);
1✔
386
  ck_assert_msg(res < 0, "Failed to handle bad socket descriptor");
1✔
387
  ck_assert_msg(errno == ENOTSOCK, "Expected ENOTSOCK (%d), got '%s' (%d)",
1✔
388
    ENOTSOCK, strerror(errno), errno);
1✔
389

390
  (void) close(fd);
391
}
1✔
392
END_TEST
393

394
START_TEST (inet_set_proto_keepalive_test) {
395
  int fd, sockfd, port = INPORT_ANY, res;
1✔
396
  conn_t *conn;
1✔
397
  struct tcp_keepalive keepalive;
1✔
398

1✔
399
  mark_point();
400
  res = pr_inet_set_proto_keepalive(NULL, NULL, NULL);
1✔
401
  ck_assert_msg(res < 0, "Failed to handle null pool");
1✔
402
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
403
    strerror(errno), errno);
1✔
404

405
  mark_point();
406
  res = pr_inet_set_proto_keepalive(p, NULL, NULL);
1✔
407
  ck_assert_msg(res < 0, "Failed to handle null conn");
1✔
408
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
409
    strerror(errno), errno);
1✔
410

411
  mark_point();
412
  conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
1✔
413
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
414

1✔
415
  mark_point();
416
  res = pr_inet_set_proto_keepalive(p, conn, NULL);
1✔
417
  ck_assert_msg(res < 0, "Failed to handle null keepalive");
1✔
418
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
419
    strerror(errno), errno);
1✔
420

421
  mark_point();
422
  conn->listen_fd = -1;
1✔
423
  keepalive.keepalive_enabled = 1;
1✔
424
  keepalive.keepalive_idle = 1;
1✔
425
  keepalive.keepalive_count = 2;
1✔
426
  keepalive.keepalive_intvl = 3;
1✔
427
  res = pr_inet_set_proto_keepalive(p, conn, &keepalive);
1✔
428
  ck_assert_msg(res < 0, "Failed to handle bad listen fd");
1✔
429
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
430
    strerror(errno), errno);
1✔
431

432
  mark_point();
433
  sockfd = devnull_fd();
1✔
434
  if (sockfd < 0) {
1✔
435
    return;
1✔
436
  }
×
437

438
  fd = conn->listen_fd;
439
  conn->listen_fd = sockfd;
1✔
440
  res = pr_inet_set_proto_keepalive(p, conn, &keepalive);
1✔
441
  ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
1✔
442
  conn->listen_fd = fd;
1✔
443

1✔
444
  (void) close(sockfd);
445
  pr_inet_close(p, conn);
1✔
446
}
1✔
447
END_TEST
448

449
START_TEST (inet_set_proto_nodelay_test) {
450
  int fd, sockfd, port = INPORT_ANY, res;
1✔
451
  conn_t *conn;
1✔
452

1✔
453
  mark_point();
454
  res = pr_inet_set_proto_nodelay(NULL, NULL, 1);
1✔
455
  ck_assert_msg(res < 0, "Failed to handle null arguments");
1✔
456
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
457
    strerror(errno), errno);
1✔
458

459
  conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
460
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
461

1✔
462
  mark_point();
463
  res = pr_inet_set_proto_nodelay(p, conn, 1);
1✔
464
  ck_assert_msg(res == 0, "Failed to enable nodelay: %s", strerror(errno));
1✔
465

1✔
466
  mark_point();
467
  res = pr_inet_set_proto_nodelay(p, conn, 0);
1✔
468
  ck_assert_msg(res == 0, "Failed to disable nodelay: %s", strerror(errno));
1✔
469

1✔
470
  fd = conn->rfd;
471
  conn->rfd = 8;
1✔
472
  res = pr_inet_set_proto_nodelay(p, conn, 0);
1✔
473
  ck_assert_msg(res == 0, "Failed to disable nodelay: %s", strerror(errno));
1✔
474
  conn->rfd = fd;
1✔
475

1✔
476
  mark_point();
477
  sockfd = devnull_fd();
1✔
478
  if (sockfd < 0) {
1✔
479
    return;
1✔
480
  }
481

482
  fd = conn->rfd;
483
  conn->rfd = sockfd;
1✔
484
  res = pr_inet_set_proto_nodelay(p, conn, 0);
1✔
485
  ck_assert_msg(res == 0, "Failed to disable nodelay: %s", strerror(errno));
1✔
486
  conn->rfd = fd;
1✔
487

1✔
488
  fd = conn->rfd;
489
  conn->rfd = -2;
1✔
490
  res = pr_inet_set_proto_nodelay(p, conn, 0);
1✔
491
  ck_assert_msg(res == 0, "Failed to disable nodelay: %s", strerror(errno));
1✔
492
  conn->rfd = fd;
1✔
493

1✔
494
  fd = conn->wfd;
495
  conn->wfd = 9;
1✔
496
  res = pr_inet_set_proto_nodelay(p, conn, 0);
1✔
497
  ck_assert_msg(res == 0, "Failed to disable nodelay: %s", strerror(errno));
1✔
498
  conn->wfd = fd;
1✔
499

1✔
500
  fd = conn->wfd;
501
  conn->wfd = -3;
1✔
502
  res = pr_inet_set_proto_nodelay(p, conn, 0);
1✔
503
  ck_assert_msg(res == 0, "Failed to disable nodelay: %s", strerror(errno));
1✔
504
  conn->wfd = fd;
1✔
505

1✔
506
  mark_point();
507
  fd = conn->wfd;
1✔
508
  conn->wfd = sockfd;
1✔
509
  res = pr_inet_set_proto_nodelay(p, conn, 0);
1✔
510
  ck_assert_msg(res == 0, "Failed to disable nodelay: %s", strerror(errno));
1✔
511
  conn->wfd = fd;
1✔
512

1✔
513
  (void) close(sockfd);
514
  pr_inet_close(p, conn);
1✔
515
}
1✔
516
END_TEST
517

518
START_TEST (inet_set_proto_opts_test) {
519
  int fd, sockfd, port = INPORT_ANY, res;
1✔
520
  conn_t *conn;
1✔
521

1✔
522
  mark_point();
523
  res = pr_inet_set_proto_opts(NULL, NULL, 1, 1, 1, 1);
1✔
524
  ck_assert_msg(res < 0, "Failed to handle null arguments");
1✔
525
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
526
    strerror(errno), errno);
1✔
527

528
  conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
529
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
530

1✔
531
  mark_point();
532
  res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
1✔
533
  ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
1✔
534

1✔
535
  mark_point();
536
  fd = conn->rfd;
1✔
537
  conn->rfd = 8;
1✔
538
  res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
1✔
539
  ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
1✔
540
  conn->rfd = fd;
1✔
541

1✔
542
  mark_point();
543
  sockfd = devnull_fd();
1✔
544
  if (sockfd < 0) {
1✔
545
    return;
1✔
546
  }
547

548
  fd = conn->rfd;
549
  conn->rfd = sockfd;
1✔
550
  res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
1✔
551
  ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
1✔
552
  conn->rfd = sockfd;
1✔
553

1✔
554
  mark_point();
555
  fd = conn->wfd;
1✔
556
  conn->wfd = 9;
1✔
557
  res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
1✔
558
  ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
1✔
559
  conn->wfd = fd;
1✔
560

1✔
561
  mark_point();
562
  fd = conn->wfd;
1✔
563
  conn->wfd = sockfd;
1✔
564
  res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
1✔
565
  ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
1✔
566
  conn->wfd = fd;
1✔
567

1✔
568
  mark_point();
569
  fd = conn->listen_fd;
1✔
570
  conn->listen_fd = 10;
1✔
571
  res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
1✔
572
  ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
1✔
573
  conn->listen_fd = fd;
1✔
574

1✔
575
  mark_point();
576
  fd = conn->listen_fd;
1✔
577
  conn->listen_fd = sockfd;
1✔
578
  res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
1✔
579
  ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
1✔
580
  conn->listen_fd = fd;
1✔
581

1✔
582
  (void) close(sockfd);
583
  pr_inet_close(p, conn);
1✔
584
}
1✔
585
END_TEST
586

587
START_TEST (inet_set_proto_opts_ipv6_test) {
588
#ifdef PR_USE_IPV6
1✔
589
  int fd, sockfd = -1, port = INPORT_ANY, res;
590
  conn_t *conn;
1✔
591
  unsigned char use_ipv6;
1✔
592

1✔
593
  use_ipv6 = pr_netaddr_use_ipv6();
594

1✔
595
  pr_netaddr_enable_ipv6();
596
  pr_inet_set_default_family(p, AF_INET6);
1✔
597

1✔
598
  conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
599
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
600

1✔
601
  mark_point();
602
  res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
1✔
603
  ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
1✔
604

1✔
605
  mark_point();
606
  fd = conn->rfd;
1✔
607
  conn->rfd = 8;
1✔
608
  res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
1✔
609
  ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
1✔
610
  conn->rfd = fd;
1✔
611

1✔
612
  mark_point();
613
  fd = conn->wfd;
1✔
614
  conn->wfd = 9;
1✔
615
  res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
1✔
616
  ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
1✔
617
  conn->wfd = fd;
1✔
618

1✔
619
  mark_point();
620
  fd = conn->listen_fd;
1✔
621
  conn->listen_fd = 10;
1✔
622
  res = pr_inet_set_proto_opts(p, conn, 1, 1, 1, 1);
1✔
623
  ck_assert_msg(res == 0, "Failed to set proto opts: %s", strerror(errno));
1✔
624
  conn->listen_fd = fd;
1✔
625

1✔
626
  pr_inet_close(p, conn);
627

1✔
628
  pr_inet_set_default_family(p, AF_INET);
629
  if (use_ipv6 == FALSE) {
1✔
630
    pr_netaddr_disable_ipv6();
1✔
631
  }
×
632
#endif /* PR_USE_IPV6 */
633
}
634
END_TEST
1✔
635

636
START_TEST (inet_set_reuse_port_test) {
637
  int port = INPORT_ANY, res;
1✔
638
  conn_t *conn;
1✔
639

1✔
640
  mark_point();
641
  res = pr_inet_set_reuse_port(NULL, NULL, 1);
1✔
642
  ck_assert_msg(res < 0, "Failed to handle null arguments");
1✔
643
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
644
    strerror(errno), errno);
1✔
645

646
  mark_point();
647
  conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
1✔
648
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
649

1✔
650
  mark_point();
651
  res = pr_inet_set_reuse_port(p, conn, -1);
1✔
652
  ck_assert_msg(res < 0, "Failed to handle invalid arguments");
1✔
653
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
654
    strerror(errno), errno);
1✔
655

656
  mark_point();
657
  res = pr_inet_set_reuse_port(p, conn, 1);
1✔
658
  ck_assert_msg(res == 0, "Failed to set reuseport option: %s", strerror(errno));
1✔
659

1✔
660
  mark_point();
661
  res = pr_inet_set_reuse_port(p, conn, 0);
1✔
662
  ck_assert_msg(res == 0, "Failed to set reuseport option: %s", strerror(errno));
1✔
663

1✔
664
  pr_inet_close(p, conn);
665
}
1✔
666
END_TEST
1✔
667

668
START_TEST (inet_set_socket_opts_test) {
669
  int fd, sockfd, port = INPORT_ANY, res;
1✔
670
  conn_t *conn;
1✔
671
  struct tcp_keepalive keepalive;
1✔
672

1✔
673
  mark_point();
674
  res = pr_inet_set_socket_opts(NULL, NULL, 1, 2, NULL);
1✔
675
  ck_assert_msg(res < 0, "Failed to handle null arguments");
1✔
676
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
677
    strerror(errno), errno);
1✔
678

679
  conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
680
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
681

1✔
682
  mark_point();
683
  res = pr_inet_set_socket_opts(p, conn, 1, 2, NULL);
1✔
684
  ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
1✔
685

1✔
686
  mark_point();
687
  res = pr_inet_set_socket_opts(p, conn, INT_MAX, INT_MAX, NULL);
1✔
688
  ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
1✔
689

1✔
690
  keepalive.keepalive_enabled = 1;
691
  keepalive.keepalive_idle = 1;
1✔
692
  keepalive.keepalive_count = 2;
1✔
693
  keepalive.keepalive_intvl = 3;
1✔
694
  res = pr_inet_set_socket_opts(p, conn, 1, 2, &keepalive);
1✔
695
  ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
1✔
696

1✔
697
  mark_point();
698
  sockfd = devnull_fd();
1✔
699
  if (sockfd < 0) {
1✔
700
    return;
1✔
701
  }
×
702

703
  fd = conn->listen_fd;
704
  conn->listen_fd = sockfd;
1✔
705
  res = pr_inet_set_socket_opts(p, conn, 1, 2, &keepalive);
1✔
706
  ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
1✔
707
  conn->listen_fd = fd;
1✔
708

1✔
709
  (void) close(sockfd);
710
  pr_inet_close(p, conn);
1✔
711
}
1✔
712
END_TEST
713

714
START_TEST (inet_set_socket_opts2_test) {
715
  int fd, sockfd, port = INPORT_ANY, res;
1✔
716
  conn_t *conn;
1✔
717
  struct tcp_keepalive keepalive;
1✔
718

1✔
719
  mark_point();
720
  res = pr_inet_set_socket_opts2(NULL, NULL, 1, 2, NULL, -1);
1✔
721
  ck_assert_msg(res < 0, "Failed to handle null arguments");
1✔
722
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
723
    strerror(errno), errno);
1✔
724

725
  conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
726
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
727

1✔
728
  mark_point();
729
  res = pr_inet_set_socket_opts2(p, conn, 1, 2, NULL, -1);
1✔
730
  ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
1✔
731

1✔
732
  mark_point();
733
  res = pr_inet_set_socket_opts2(p, conn, INT_MAX, INT_MAX, NULL, 0);
1✔
734
  ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
1✔
735

1✔
736
  keepalive.keepalive_enabled = 1;
737
  keepalive.keepalive_idle = 1;
1✔
738
  keepalive.keepalive_count = 2;
1✔
739
  keepalive.keepalive_intvl = 3;
1✔
740
  res = pr_inet_set_socket_opts2(p, conn, 1, 2, &keepalive, 1);
1✔
741
  ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
1✔
742

1✔
743
  mark_point();
744
  sockfd = devnull_fd();
1✔
745
  if (sockfd < 0) {
1✔
746
    return;
1✔
747
  }
×
748

749
  fd = conn->listen_fd;
750
  conn->listen_fd = sockfd;
1✔
751
  res = pr_inet_set_socket_opts2(p, conn, 1, 2, &keepalive, 1);
1✔
752
  ck_assert_msg(res == 0, "Failed to set socket opts: %s", strerror(errno));
1✔
753
  conn->listen_fd = fd;
1✔
754

1✔
755
  (void) close(sockfd);
756
  pr_inet_close(p, conn);
1✔
757
}
1✔
758
END_TEST
759

760
START_TEST (inet_listen_test) {
761
  int fd, mode, sockfd = -1, port = INPORT_ANY, res;
1✔
762
  conn_t *conn;
1✔
763

1✔
764
  res = pr_inet_listen(NULL, NULL, 5, 0);
765
  ck_assert_msg(res < 0, "Failed to handle null arguments");
1✔
766
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
767
    strerror(errno), errno);
1✔
768

769
  res = pr_inet_resetlisten(NULL, NULL);
770
  ck_assert_msg(res < 0, "Failed to handle null arguments");
1✔
771
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
772
    strerror(errno), errno);
1✔
773

774
  conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
775
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
776

1✔
777
  fd = conn->listen_fd;
778
  conn->listen_fd = 777;
1✔
779
  res = pr_inet_listen(p, conn, 5, 0);
1✔
780
  ck_assert_msg(res < 0, "Succeeded in listening on conn unexpectedly");
1✔
781
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
1✔
782
    strerror(errno), errno);
1✔
783

784
  mode = conn->mode;
785
  res = pr_inet_resetlisten(p, conn);
1✔
786
  ck_assert_msg(res < 0, "Succeeded in resetting listening on conn unexpectedly");
1✔
787
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
1✔
788
    strerror(errno), errno);
1✔
789

790
  conn->listen_fd = fd;
791
  conn->mode = mode;
1✔
792

1✔
793
  res = pr_inet_listen(p, conn, 5, 0);
794
  ck_assert_msg(res == 0, "Failed to listen on conn: %s", strerror(errno));
1✔
795

1✔
796
  res = pr_inet_resetlisten(p, conn);
797
  ck_assert_msg(res == 0, "Failed to reset listen mode: %s", strerror(errno));
1✔
798

1✔
799
  res = pr_inet_listen(p, conn, 5, 0);
800
  ck_assert_msg(res < 0, "Failed to handle already-listening socket");
1✔
801
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
1✔
802
    strerror(errno), errno);
1✔
803

804
  pr_inet_close(p, conn);
805
}
1✔
806
END_TEST
1✔
807

808
START_TEST (inet_connect_ipv4_test) {
809
  int sockfd = -1, port = INPORT_ANY, res;
1✔
810
  conn_t *conn;
1✔
811
  const pr_netaddr_t *addr;
1✔
812

1✔
813
  res = pr_inet_connect(NULL, NULL, NULL, port);
814
  ck_assert_msg(res < 0, "Failed to handle null arguments");
1✔
815
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
816
    strerror(errno), errno);
1✔
817

818
  conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
819
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
820

1✔
821
  res = pr_inet_connect(p, conn, NULL, 180);
822
  ck_assert_msg(res < 0, "Failed to handle null address");
1✔
823
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
824
    strerror(errno), errno);
1✔
825

826
  mark_point();
827
  addr = pr_netaddr_get_addr(p, "127.0.0.1", NULL);
1✔
828
  ck_assert_msg(addr != NULL, "Failed to resolve '127.0.0.1': %s",
1✔
829
    strerror(errno));
1✔
830

831
  mark_point();
832
  res = pr_inet_connect(p, conn, addr, 180);
1✔
833
  ck_assert_msg(res < 0, "Connected to 127.0.0.1#180 unexpectedly");
1✔
834
  ck_assert_msg(errno == ECONNREFUSED, "Expected ECONNREFUSED (%d), got %s (%d)",
1✔
835
    ECONNREFUSED, strerror(errno), errno);
1✔
836

837
#if defined(PR_USE_NETWORK_TESTS)
838
  addr = pr_netaddr_get_addr(p, dns_resolver, NULL);
839
  ck_assert_msg(addr != NULL, "Failed to resolve '%s': %s", dns_resolver,
1✔
840
    strerror(errno));
1✔
841

842
  res = pr_inet_connect(p, conn, addr, 53);
843
  if (res < 0) {
1✔
844
    /* Note: We get EINVAL here because the socket already tried (and failed)
1✔
845
     * to connect to a different address.  Interestingly, trying to connect(2)
846
     * using that same fd to a different address yields EINVAL.
847
     */
848
    ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
849
      strerror(errno), errno);
×
850
  }
851
  pr_inet_close(p, conn);
852

1✔
853
  conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
854
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
855

1✔
856
  res = pr_inet_connect(p, conn, addr, 53);
857
  ck_assert_msg(res >= 0, "Failed to connect to %s#53: %s", dns_resolver,
1✔
858
    strerror(errno));
1✔
859

860
  res = pr_inet_connect(p, conn, addr, 53);
861
  ck_assert_msg(res < 0, "Failed to connect to %s#53: %s", dns_resolver,
1✔
862
    strerror(errno));
1✔
863
  ck_assert_msg(errno == EISCONN, "Expected EISCONN (%d), got %s (%d)",
864
    EISCONN, strerror(errno), errno);
1✔
865
  pr_inet_close(p, conn);
866
#endif
1✔
867
}
868
END_TEST
1✔
869

870
START_TEST (inet_connect_ipv6_test) {
871
#ifdef PR_USE_IPV6
1✔
872
  int sockfd = -1, port = INPORT_ANY, res;
873
  conn_t *conn;
1✔
874
  const pr_netaddr_t *addr;
1✔
875
  unsigned char use_ipv6;
1✔
876

1✔
877
  use_ipv6 = pr_netaddr_use_ipv6();
878

1✔
879
  pr_netaddr_enable_ipv6();
880
  pr_inet_set_default_family(p, AF_INET6);
1✔
881

1✔
882
  conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
883
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
884

1✔
885
  mark_point();
886
  addr = pr_netaddr_get_addr(p, "::1", NULL);
1✔
887
  ck_assert_msg(addr != NULL, "Failed to resolve '::1': %s",
1✔
888
    strerror(errno));
1✔
889

890
  mark_point();
891
  res = pr_inet_connect(p, conn, addr, 180);
1✔
892
  ck_assert_msg(res < 0, "Connected to ::1#180 unexpectedly");
1✔
893
  ck_assert_msg(errno == ECONNREFUSED || errno == ENETUNREACH || errno == EADDRNOTAVAIL,
1✔
894
    "Expected ECONNREFUSED (%d), ENETUNREACH (%d), or EADDRNOTAVAIL (%d), got %s (%d)",
1✔
895
    ECONNREFUSED, ENETUNREACH, EADDRNOTAVAIL, strerror(errno), errno);
896

897
#if defined(PR_USE_NETWORK_TESTS)
898
  /* Try connecting to Google's DNS server. */
899

900
  addr = pr_netaddr_get_addr(p, "2001:4860:4860::8888", NULL);
901
  ck_assert_msg(addr != NULL, "Failed to resolve '2001:4860:4860::8888': %s",
1✔
902
    strerror(errno));
1✔
903

904
  res = pr_inet_connect(p, conn, addr, 53);
905
  if (res < 0) {
1✔
906
    /* Note: We get EINVAL here because the socket already tried (and failed)
1✔
907
     * to connect to a different address.  Interestingly, trying to connect(2)
908
     * using that same fd to a different address yields EINVAL.
909
     */
910
    ck_assert_msg(errno == EINVAL || errno == ENETUNREACH || errno == EADDRNOTAVAIL,
911
      "Expected EINVAL (%d), ENETUNREACH (%d) or EADDRNOTAVAIL (%d), got %s (%d)",
1✔
912
      EINVAL, ENETUNREACH, EADDRNOTAVAIL, strerror(errno), errno);
913
  }
914
  pr_inet_close(p, conn);
915

1✔
916
  conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
917
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
918

1✔
919
  res = pr_inet_connect(p, conn, addr, 53);
920
  if (res < 0) {
1✔
921
    /* This could be expected, e.g. if there's no route. */
1✔
922
    ck_assert_msg(errno == EHOSTUNREACH || errno == ENETUNREACH || errno == EADDRNOTAVAIL,
923
      "Expected EHOSTUNREACH (%d) or ENETUNREACH (%d) or EADDRNOTAVAIL (%d), got %s (%d)",
1✔
924
      EHOSTUNREACH, ENETUNREACH, EADDRNOTAVAIL, strerror(errno), errno);
925
  }
926

927
  res = pr_inet_connect(p, conn, addr, 53);
928
  ck_assert_msg(res < 0, "Failed to connect to 2001:4860:4860::8888#53: %s",
1✔
929
    strerror(errno));
1✔
930
  ck_assert_msg(errno == EISCONN || errno == EHOSTUNREACH || errno == ENETUNREACH || errno == EADDRNOTAVAIL,
931
    "Expected EISCONN (%d) or EHOSTUNREACH (%d) or ENETUNREACH (%d) or EADDRNOTAVAIL (%d), got %s (%d)", EISCONN, EHOSTUNREACH, ENETUNREACH, EADDRNOTAVAIL, strerror(errno), errno);
1✔
932
  pr_inet_close(p, conn);
933
#endif
1✔
934

935
  pr_inet_set_default_family(p, AF_INET);
936

1✔
937
  if (use_ipv6 == FALSE) {
938
    pr_netaddr_disable_ipv6();
1✔
939
  }
×
940
#endif /* PR_USE_IPV6 */
941
}
942
END_TEST
1✔
943

944
START_TEST (inet_connect_nowait_test) {
945
  int sockfd = -1, port = INPORT_ANY, res;
1✔
946
  conn_t *conn;
1✔
947
  const pr_netaddr_t *addr;
1✔
948

1✔
949
  res = pr_inet_connect_nowait(NULL, NULL, NULL, port);
950
  ck_assert_msg(res < 0, "Failed to handle null arguments");
1✔
951
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
952
    strerror(errno), errno);
1✔
953

954
  conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
955
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
956

1✔
957
  res = pr_inet_connect_nowait(p, conn, NULL, 180);
958
  ck_assert_msg(res < 0, "Failed to handle null address");
1✔
959
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
960
    strerror(errno), errno);
1✔
961

962
  addr = pr_netaddr_get_addr(p, "127.0.0.1", NULL);
963
  ck_assert_msg(addr != NULL, "Failed to resolve '127.0.0.1': %s",
1✔
964
    strerror(errno));
1✔
965

966
  res = pr_inet_connect_nowait(p, conn, addr, 180);
967
  ck_assert_msg(res != -1, "Connected to 127.0.0.1#180 unexpectedly");
1✔
968

1✔
969
#if defined(PR_USE_NETWORK_TESTS)
970
  /* Try connecting to Google's DNS server. */
971

972
  addr = pr_netaddr_get_addr(p, dns_resolver, NULL);
973
  ck_assert_msg(addr != NULL, "Failed to resolve '%s': %s", dns_resolver,
1✔
974
    strerror(errno));
1✔
975

976
  res = pr_inet_connect_nowait(p, conn, addr, 53);
977
  if (res < 0 &&
1✔
978
      errno != ECONNREFUSED &&
1✔
979
      errno != EBADF) {
1✔
980
    ck_assert_msg(res != -1, "Failed to connect to %s#53: %s", dns_resolver,
981
      strerror(errno));
×
982
  }
983

984
  pr_inet_close(p, conn);
985
#endif
1✔
986

987
  /* Restore the default family to AF_INET, for other tests. */
988
  pr_inet_set_default_family(p, AF_INET);
989
}
1✔
990
END_TEST
1✔
991

992
START_TEST (inet_accept_test) {
993
  conn_t *conn;
1✔
994

1✔
995
  conn = pr_inet_accept(NULL, NULL, NULL, 0, 2, FALSE);
996
  ck_assert_msg(conn == NULL, "Failed to handle null arguments");
1✔
997
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
998
    strerror(errno), errno);
1✔
999
}
1000
END_TEST
1✔
1001

1002
START_TEST (inet_accept_nowait_test) {
1003
  int fd, sockfd, port = INPORT_ANY, res;
1✔
1004
  conn_t *conn;
1✔
1005

1✔
1006
  mark_point();
1007
  res = pr_inet_accept_nowait(NULL, NULL);
1✔
1008
  ck_assert_msg(res < 0, "Failed to handle null arguments");
1✔
1009
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1010
    strerror(errno), errno);
1✔
1011

1012
  conn = pr_inet_create_conn(p, -1, NULL, port, FALSE);
1013
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
1014

1✔
1015
  mark_point();
1016
  res = pr_inet_accept_nowait(p, conn);
1✔
1017
  ck_assert_msg(res < 0, "Accepted connection unexpectedly");
1✔
1018
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1019
    strerror(errno), errno);
1✔
1020

1021
  mark_point();
1022
  sockfd = devnull_fd();
1✔
1023
  if (sockfd < 0) {
1✔
1024
    return;
1✔
1025
  }
1026

1027
  fd = conn->listen_fd;
1028
  conn->listen_fd = sockfd;
1✔
1029
  conn->mode = CM_LISTEN;
1✔
1030
  res = pr_inet_accept_nowait(p, conn);
1✔
1031
  ck_assert_msg(res < 0, "Failed to handle non-socket");
1✔
1032
  ck_assert_msg(errno == ENOTSOCK, "Expected ENOTSOCK (%d), got %s (%d)",
1✔
1033
    ENOTSOCK, strerror(errno), errno);
1✔
1034
  conn->listen_fd = fd;
1035

1✔
1036
  (void) close(sockfd);
1037
  pr_inet_close(p, conn);
1✔
1038
}
1✔
1039
END_TEST
1040

1041
START_TEST (inet_conn_info_test) {
1042
  int sockfd = -1, port = INPORT_ANY, res;
1✔
1043
  conn_t *conn;
1✔
1044

1✔
1045
  res = pr_inet_get_conn_info(NULL, -1);
1046
  ck_assert_msg(res < 0, "Failed to handle null arguments");
1✔
1047
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1048
    strerror(errno), errno);
1✔
1049

1050
  conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
1051
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
1052

1✔
1053
  res = pr_inet_get_conn_info(conn, -1);
1054
  ck_assert_msg(res < 0, "Failed to handle bad file descriptor");
1✔
1055
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
1✔
1056
    strerror(errno), errno);
1✔
1057

1058
  res = pr_inet_get_conn_info(conn, 1);
1059
  ck_assert_msg(res < 0, "Failed to handle bad file descriptor");
1✔
1060
  ck_assert_msg(errno == ENOTSOCK, "Expected ENOTSOCK (%d), got %s (%d)",
1✔
1061
    ENOTSOCK, strerror(errno), errno);
1✔
1062

1063
  pr_inet_close(p, conn);
1064
}
1✔
1065
END_TEST
1✔
1066

1067
START_TEST (inet_openrw_test) {
1068
  int fd, sockfd = -1, port = INPORT_ANY;
1✔
1069
  conn_t *conn, *res;
1✔
1070
  const pr_netaddr_t *addr;
1✔
1071

1✔
1072
  mark_point();
1073
  res = pr_inet_openrw(NULL, NULL, NULL, PR_NETIO_STRM_CTRL, -1, -1, -1, FALSE);
1✔
1074
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
1✔
1075
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1076
    strerror(errno), errno);
1✔
1077

1078
  conn = pr_inet_create_conn(p, sockfd, NULL, port, FALSE);
1079
  ck_assert_msg(conn != NULL, "Failed to create conn: %s", strerror(errno));
1✔
1080

1✔
1081
  mark_point();
1082
  res = pr_inet_openrw(p, conn, NULL, PR_NETIO_STRM_CTRL, -1, -1, -1, FALSE);
1✔
1083
  ck_assert_msg(res == NULL, "Opened rw conn unexpectedly");
1✔
1084
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1085
    strerror(errno), errno);
1✔
1086

1087
  addr = pr_netaddr_get_addr(p, "127.0.0.1", NULL);
1088
  ck_assert_msg(addr != NULL, "Failed to resolve 127.0.0.1: %s", strerror(errno));
1✔
1089

1✔
1090
  mark_point();
1091
  res = pr_inet_openrw(p, conn, addr, PR_NETIO_STRM_CTRL, -1, -1, -1, FALSE);
1✔
1092
  ck_assert_msg(res != NULL, "Failed to open rw conn: %s", strerror(errno));
1✔
1093
  (void) pr_inet_close(p, res);
1✔
1094

1✔
1095
  mark_point();
1096
  res = pr_inet_openrw(p, conn, addr, PR_NETIO_STRM_CTRL, -1, -1, -1, TRUE);
1✔
1097
  ck_assert_msg(res != NULL, "Failed to open rw conn: %s", strerror(errno));
1✔
1098

1✔
1099
  mark_point();
1100
  fd = devnull_fd();
1✔
1101
  if (fd < 0) {
1✔
1102
    return;
1✔
1103
  }
1104
  res = pr_inet_openrw(p, conn, addr, PR_NETIO_STRM_CTRL, fd, -1, -1, TRUE);
1105
  ck_assert_msg(res == NULL, "Failed to handle non-socket");
1✔
1106
  ck_assert_msg(errno == ENOTSOCK, "Expected ENOTSOCK (%d), got %s (%d)",
1✔
1107
    ENOTSOCK, strerror(errno), errno);
1✔
1108

1109
  (void) close(fd);
1110
}
1✔
1111
END_TEST
1112

1113
START_TEST (inet_generate_socket_event_test) {
1114
  int res;
1✔
1115
  const char *name;
1✔
1116
  server_rec *s;
1✔
1117

1✔
1118
  res = pr_inet_generate_socket_event(NULL, NULL, NULL, -1);
1119
  ck_assert_msg(res < 0, "Failed to handle null arguments");
1✔
1120
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1121
    strerror(errno), errno);
1✔
1122

1123
  name = "foo.bar";
1124
  res = pr_inet_generate_socket_event(name, NULL, NULL, -1);
1✔
1125
  ck_assert_msg(res < 0, "Failed to handle null server_rec");
1✔
1126
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1127
    strerror(errno), errno);
1✔
1128

1129
  s = pcalloc(p, sizeof(server_rec));
1130
  res = pr_inet_generate_socket_event(name, s, NULL, -1);
1✔
1131
  ck_assert_msg(res < 0, "Failed to handle null address");
1✔
1132
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1133
    strerror(errno), errno);
1✔
1134
}
1135
END_TEST
1✔
1136

1137
Suite *tests_get_inet_suite(void) {
1138
  Suite *suite;
890✔
1139
  TCase *testcase;
890✔
1140

890✔
1141
  suite = suite_create("inet");
1142

890✔
1143
  testcase = tcase_create("base");
1144
  tcase_add_checked_fixture(testcase, set_up, tear_down);
890✔
1145

890✔
1146
  tcase_add_test(testcase, inet_family_test);
1147
  tcase_add_test(testcase, inet_getservport_test);
890✔
1148
  tcase_add_test(testcase, inet_close_test);
890✔
1149
  tcase_add_test(testcase, inet_create_conn_test);
890✔
1150
  tcase_add_test(testcase, inet_create_conn_portrange_test);
890✔
1151
  tcase_add_test(testcase, inet_copy_conn_test);
890✔
1152
  tcase_add_test(testcase, inet_set_async_test);
890✔
1153
  tcase_add_test(testcase, inet_set_block_test);
890✔
1154
  tcase_add_test(testcase, inet_set_proto_cork_test);
890✔
1155
  tcase_add_test(testcase, inet_set_proto_keepalive_test);
890✔
1156
  tcase_add_test(testcase, inet_set_proto_nodelay_test);
890✔
1157
  tcase_add_test(testcase, inet_set_proto_opts_test);
890✔
1158
  tcase_add_test(testcase, inet_set_proto_opts_ipv6_test);
890✔
1159
  tcase_add_test(testcase, inet_set_reuse_port_test);
890✔
1160
  tcase_add_test(testcase, inet_set_socket_opts_test);
890✔
1161
  tcase_add_test(testcase, inet_set_socket_opts2_test);
890✔
1162
  tcase_add_test(testcase, inet_listen_test);
890✔
1163
  tcase_add_test(testcase, inet_connect_ipv4_test);
890✔
1164
  tcase_add_test(testcase, inet_connect_ipv6_test);
890✔
1165
  tcase_add_test(testcase, inet_connect_nowait_test);
890✔
1166
  tcase_add_test(testcase, inet_accept_test);
890✔
1167
  tcase_add_test(testcase, inet_accept_nowait_test);
890✔
1168
  tcase_add_test(testcase, inet_conn_info_test);
890✔
1169
  tcase_add_test(testcase, inet_openrw_test);
890✔
1170
  tcase_add_test(testcase, inet_generate_socket_event_test);
890✔
1171

890✔
1172
  suite_add_tcase(suite, testcase);
1173
  return suite;
890✔
1174
}
890✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc