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

proftpd / proftpd / 30135626854

25 Jul 2026 12:15AM UTC coverage: 93.034% (+0.6%) from 92.428%
30135626854

push

github

51363 of 55209 relevant lines covered (93.03%)

219.82 hits per line

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

95.9
/tests/api/netio.c
1
/*
2
 * ProFTPD - FTP server testsuite
3
 * Copyright (c) 2008-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
/* NetIO API tests. */
25

26
#include "tests.h"
27

28
/* See RFC 854 for the definition of these Telnet values */
29

30
/* Telnet "Interpret As Command" indicator */
31
#define TELNET_IAC     255
32
#define TELNET_DONT    254
33
#define TELNET_DO      253
34
#define TELNET_WONT    252
35
#define TELNET_WILL    251
36
#define TELNET_IP      244
37
#define TELNET_DM      242
38

39
static pool *p = NULL;
40
static int xfer_bufsz = -1;
41

42
static int tmp_fd = -1;
43
static const char *tmp_path = NULL;
44

45
static void test_cleanup(void) {
46
  (void) close(tmp_fd);
52✔
47
  tmp_fd = -1;
52✔
48

52✔
49
  if (tmp_path != NULL) {
50
    (void) unlink(tmp_path);
52✔
51
    tmp_path = NULL;
5✔
52
  }
5✔
53

54
  pr_unregister_netio(PR_NETIO_STRM_CTRL|PR_NETIO_STRM_DATA|PR_NETIO_STRM_OTHR);
55
}
52✔
56

52✔
57
static int open_tmpfile(void) {
58
  int fd;
5✔
59

5✔
60
  if (tmp_path != NULL) {
61
    test_cleanup();
5✔
62
  }
×
63

64
  tmp_path = "/tmp/netio-test.dat";
65
  fd = open(tmp_path, O_RDWR|O_CREAT, 0666);
5✔
66
  ck_assert_msg(fd >= 0, "Failed to open '%s': %s", tmp_path, strerror(errno));
5✔
67
  tmp_fd = fd;
5✔
68

5✔
69
  return fd;
70
}
5✔
71

72
static void set_up(void) {
73
  if (p == NULL) {
47✔
74
    p = permanent_pool = make_sub_pool(NULL);
47✔
75
  }
47✔
76

77
  init_netio();
78
  pr_random_init();
47✔
79
  xfer_bufsz = pr_config_get_server_xfer_bufsz(PR_NETIO_IO_RD);
47✔
80

47✔
81
  if (getenv("TEST_VERBOSE") != NULL) {
82
    pr_trace_set_levels("netio", 1, 20);
47✔
83
  }
47✔
84
}
85

47✔
86
static void tear_down(void) {
87
  if (getenv("TEST_VERBOSE") != NULL) {
47✔
88
    pr_trace_set_levels("netio", 0, 0);
47✔
89
  }
47✔
90

91
  test_cleanup();
92

47✔
93
  if (p) {
94
    destroy_pool(p);
47✔
95
    p = permanent_pool = NULL;
47✔
96
  }
47✔
97
}
98

47✔
99
/* Tests */
100

101
START_TEST (netio_open_test) {
102
  pr_netio_stream_t *nstrm;
1✔
103
  int fd = -1;
1✔
104

1✔
105
  nstrm = pr_netio_open(NULL, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
106
  ck_assert_msg(nstrm == NULL, "Failed to handle null pool argument");
1✔
107
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
1✔
108
    strerror(errno), errno);
1✔
109

110
  nstrm = pr_netio_open(p, 7777, fd, PR_NETIO_IO_RD);
111
  ck_assert_msg(nstrm == NULL, "Failed to handle unknown stream type argument");
1✔
112
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
1✔
113
    strerror(errno), errno);
1✔
114

115
  /* open/close CTRL stream */
116
  nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
117
  ck_assert_msg(nstrm != NULL, "Failed to open ctrl stream on fd %d: %s", fd,
1✔
118
    strerror(errno));
1✔
119
  ck_assert_msg(nstrm->strm_netio != NULL,
120
    "Failed to assign owning NetIO to stream");
1✔
121
  pr_netio_close(nstrm);
122

1✔
123
  /* open/close DATA stream */
124
  nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_WR);
125
  ck_assert_msg(nstrm != NULL, "Failed to open data stream on fd %d: %s", fd,
1✔
126
    strerror(errno));
1✔
127
  ck_assert_msg(nstrm->strm_netio != NULL,
128
    "Failed to assign owning NetIO to stream");
1✔
129
  pr_netio_close(nstrm);
130

1✔
131
  /* open/close OTHR stream */
132
  nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_WR);
133
  ck_assert_msg(nstrm != NULL, "Failed to open othr stream on fd %d: %s", fd,
1✔
134
    strerror(errno));
1✔
135
  ck_assert_msg(nstrm->strm_netio != NULL,
136
    "Failed to assign owning NetIO to stream");
1✔
137
  pr_netio_close(nstrm);
138
}
1✔
139
END_TEST
1✔
140

141
START_TEST (netio_postopen_test) {
142
  pr_netio_stream_t *nstrm;
1✔
143
  int fd = -1, res;
1✔
144

1✔
145
  res = pr_netio_postopen(NULL);
146
  ck_assert_msg(res < 0, "Failed to handle null argument");
1✔
147
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
148
    strerror(errno), errno);
1✔
149

150
  /* open/postopen/close CTRL stream */
151
  nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
152
  ck_assert_msg(nstrm != NULL, "Failed to open stream on fd %d: %s", fd,
1✔
153
    strerror(errno));
1✔
154

155
  res = pr_netio_postopen(nstrm);
156
  ck_assert_msg(res == 0, "Failed to post-open ctrl stream: %s", strerror(errno));
1✔
157
  (void) pr_netio_close(nstrm);
1✔
158

1✔
159
  /* open/postopen/close DATA stream */
160
  nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_RD);
161
  ck_assert_msg(nstrm != NULL, "Failed to open stream on fd %d: %s", fd,
1✔
162
    strerror(errno));
1✔
163

164
  res = pr_netio_postopen(nstrm);
165
  ck_assert_msg(res == 0, "Failed to post-open data stream: %s", strerror(errno));
1✔
166
  (void) pr_netio_close(nstrm);
1✔
167

1✔
168
  /* open/postopen/close OTHR stream */
169
  nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_RD);
170
  ck_assert_msg(nstrm != NULL, "Failed to open stream on fd %d: %s", fd,
1✔
171
    strerror(errno));
1✔
172

173
  res = pr_netio_postopen(nstrm);
174
  ck_assert_msg(res == 0, "Failed to post-open othr stream: %s", strerror(errno));
1✔
175
  (void) pr_netio_close(nstrm);
1✔
176
}
1✔
177
END_TEST
1✔
178

179
START_TEST (netio_close_test) {
180
  pr_netio_stream_t *nstrm;
1✔
181
  int res, fd = -1;
1✔
182

1✔
183
  res = pr_netio_close(NULL);
184
  ck_assert_msg(res == -1, "Failed to handle null stream argument");
1✔
185
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
1✔
186
    strerror(errno), errno);
1✔
187

188
  /* Open/close CTRL stream */
189
  nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
190
  nstrm->strm_type = 7777;
1✔
191

1✔
192
  res = pr_netio_close(nstrm);
193
  ck_assert_msg(res == -1, "Failed to handle unknown stream type argument");
1✔
194
  ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)",
1✔
195
    strerror(errno), errno);
1✔
196

197
  nstrm->strm_type = PR_NETIO_STRM_CTRL;
198
  res = pr_netio_close(nstrm);
1✔
199
  ck_assert_msg(res == -1, "Failed to handle bad file descriptor");
1✔
200
  ck_assert_msg(errno == EBADF, "Failed to set errno to EBADF, got %s (%d)",
1✔
201
    strerror(errno), errno);
1✔
202

203
  /* Open/close DATA stream */
204
  nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_RD);
205
  res = pr_netio_close(nstrm);
1✔
206
  ck_assert_msg(res == -1, "Failed to handle bad file descriptor");
1✔
207
  ck_assert_msg(errno == EBADF, "Failed to set errno to EBADF, got %s (%d)",
1✔
208
    strerror(errno), errno);
1✔
209

210
  /* Open/close OTHR stream */
211
  nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_RD);
212
  res = pr_netio_close(nstrm);
1✔
213
  ck_assert_msg(res == -1, "Failed to handle bad file descriptor");
1✔
214
  ck_assert_msg(errno == EBADF, "Failed to set errno to EBADF, got %s (%d)",
1✔
215
    strerror(errno), errno);
1✔
216
}
217
END_TEST
1✔
218

219
START_TEST (netio_lingering_close_test) {
220
  pr_netio_stream_t *nstrm;
1✔
221
  int res, fd = -1;
1✔
222
  long linger = 0L;
1✔
223

1✔
224
  res = pr_netio_lingering_close(NULL, linger);
225
  ck_assert_msg(res == -1, "Failed to handle null stream argument");
1✔
226
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
1✔
227
    strerror(errno), errno);
1✔
228

229
  /* Open/close CTRL stream */
230
  nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
231
  nstrm->strm_type = 7777;
1✔
232

1✔
233
  res = pr_netio_lingering_close(nstrm, linger);
234
  ck_assert_msg(res < 0, "Failed to handle unknown stream type argument");
1✔
235
  ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)",
1✔
236
    strerror(errno), errno);
1✔
237

238
  nstrm->strm_type = PR_NETIO_STRM_CTRL;
239
  res = pr_netio_lingering_close(nstrm, linger);
1✔
240
  ck_assert_msg(res == 0, "Failed to close stream: %s", strerror(errno));
1✔
241

1✔
242
  /* Open/close DATA stream */
243
  nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_RD);
244
  res = pr_netio_lingering_close(nstrm, linger);
1✔
245
  ck_assert_msg(res == 0, "Failed to close stream: %s", strerror(errno));
1✔
246

1✔
247
  /* Open/close OTHR stream */
248
  nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_RD);
249
  res = pr_netio_lingering_close(nstrm, linger);
1✔
250
  ck_assert_msg(res == 0, "Failed to close stream: %s", strerror(errno));
1✔
251
}
1✔
252
END_TEST
1✔
253

254
START_TEST (netio_reopen_test) {
255
  pr_netio_stream_t *nstrm, *nstrm2;
1✔
256
  int res, fd = -1;
1✔
257

1✔
258
  nstrm2 = pr_netio_reopen(NULL, fd, PR_NETIO_IO_RD);
259
  ck_assert_msg(nstrm2 == NULL, "Failed to handle null stream argument");
1✔
260
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
1✔
261
    strerror(errno), errno);
1✔
262

263
  /* Open/reopen/close CTRL stream */
264
  nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
265
  nstrm->strm_type = 7777;
1✔
266

1✔
267
  nstrm2 = pr_netio_reopen(nstrm, fd, PR_NETIO_IO_RD);
268
  ck_assert_msg(nstrm2 == NULL, "Failed to handle unknown stream type argument");
1✔
269
  ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)",
1✔
270
    strerror(errno), errno);
1✔
271

272
  nstrm->strm_type = PR_NETIO_STRM_CTRL;
273
  nstrm2 = pr_netio_reopen(nstrm, fd, PR_NETIO_IO_RD);
1✔
274
  ck_assert_msg(nstrm2 != NULL, "Failed to reopen ctrl stream: %s",
1✔
275
    strerror(errno));
1✔
276

277
  /* Open/reopen/close DATA stream */
278
  nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_RD);
279
  nstrm2 = pr_netio_reopen(nstrm, fd, PR_NETIO_IO_RD);
1✔
280
  ck_assert_msg(nstrm2 != NULL, "Failed to reopen data stream: %s",
1✔
281
    strerror(errno));
1✔
282

283
  res = pr_netio_close(nstrm);
284
  ck_assert_msg(res == -1, "Failed to handle bad file descriptor");
1✔
285
  ck_assert_msg(errno == EBADF, "Failed to set errno to EBADF, got %s (%d)",
1✔
286
    strerror(errno), errno);
1✔
287

288
  /* Open/reopen/close OTHR stream */
289
  nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_RD);
290
  nstrm2 = pr_netio_reopen(nstrm, fd, PR_NETIO_IO_RD);
1✔
291
  ck_assert_msg(nstrm2 != NULL, "Failed to reopen othr stream: %s",
1✔
292
    strerror(errno));
1✔
293

294
  res = pr_netio_close(nstrm);
295
  ck_assert_msg(res == -1, "Failed to handle bad file descriptor");
1✔
296
  ck_assert_msg(errno == EBADF, "Failed to set errno to EBADF, got %s (%d)",
1✔
297
    strerror(errno), errno);
1✔
298
}
299
END_TEST
1✔
300

301
START_TEST (netio_buffer_alloc_test) {
302
  pr_buffer_t *pbuf;
1✔
303
  pr_netio_stream_t *nstrm;
1✔
304

1✔
305
  pbuf = pr_netio_buffer_alloc(NULL);
306
  ck_assert_msg(pbuf == NULL, "Failed to handle null arguments");
1✔
307
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
1✔
308
    strerror(errno), errno);
1✔
309

310
  nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
311

1✔
312
  pbuf = pr_netio_buffer_alloc(nstrm);
313
  ck_assert_msg(pbuf != NULL, "Failed to allocate buffer: %s", strerror(errno));
1✔
314

1✔
315
  pr_netio_close(nstrm);
316
}
1✔
317
END_TEST
1✔
318

319
START_TEST (netio_telnet_gets_args_test) {
320
  char *buf, *res;
1✔
321
  pr_netio_stream_t *in, *out;
1✔
322

1✔
323
  res = pr_netio_telnet_gets(NULL, 0, NULL, NULL);
324
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
1✔
325
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
1✔
326
    strerror(errno), errno);
1✔
327

328
  buf = "";
329
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1✔
330
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
331

1✔
332
  res = pr_netio_telnet_gets(buf, 0, in, out);
333
  ck_assert_msg(res == NULL,
1✔
334
    "Failed to handle zero-length buffer length argument");
1✔
335
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
336
    strerror(errno), errno);
1✔
337

338
  res = pr_netio_telnet_gets(buf, 1, NULL, out);
339
  ck_assert_msg(res == NULL, "Failed to handle null input stream argument");
1✔
340
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
1✔
341
    strerror(errno), errno);
1✔
342

343
  res = pr_netio_telnet_gets(buf, 1, in, NULL);
344
  ck_assert_msg(res == NULL, "Failed to handle null output stream argument");
1✔
345
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
1✔
346
    strerror(errno), errno);
1✔
347

348
  pr_netio_close(in);
349
  pr_netio_close(out);
1✔
350
}
1✔
351
END_TEST
1✔
352

353
START_TEST (netio_telnet_gets_single_line_test) {
354
  char buf[256], *cmd, *res;
1✔
355
  pr_netio_stream_t *in, *out;
1✔
356
  pr_buffer_t *pbuf;
1✔
357
  int len, xerrno;
1✔
358

1✔
359
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
360
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
361

1✔
362
  cmd = "Hello, World!\n";
363

1✔
364
  pr_netio_buffer_alloc(in);
365
  pbuf = in->strm_buf;
1✔
366
  len = snprintf(pbuf->buf, pbuf->buflen-1, "%s", cmd);
1✔
367
  pbuf->remaining = pbuf->buflen - len;
1✔
368
  pbuf->current = pbuf->buf;
1✔
369

1✔
370
  buf[sizeof(buf)-1] = '\0';
371

1✔
372
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
373
  xerrno = errno;
1✔
374

1✔
375
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
376
    xerrno, strerror(xerrno));
1✔
377
  ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
378
    buf);
1✔
379
  ck_assert_msg(pbuf->remaining == (size_t) xfer_bufsz,
380
    "Expected %d remaining bytes, got %lu", xfer_bufsz,
1✔
381
    (unsigned long) pbuf->remaining);
382

383
  pr_netio_close(in);
384
  pr_netio_close(out);
1✔
385
}
1✔
386
END_TEST
1✔
387

388
START_TEST (netio_telnet_gets_multi_line_test) {
389
  char buf[256], *cmd, *first_cmd, *second_cmd, *res;
1✔
390
  pr_netio_stream_t *in, *out;
1✔
391
  pr_buffer_t *pbuf;
1✔
392
  int len, xerrno;
1✔
393

1✔
394
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
395
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
396

1✔
397
  /* Note: the line terminator in Telnet is CRLF, not just a bare LF. */
398
  cmd = "Hello, World!\r\nHow are you?\r\n";
399
  first_cmd = "Hello, World!\n";
1✔
400
  second_cmd = "How are you?\n";
1✔
401

1✔
402
  pr_netio_buffer_alloc(in);
403
  pbuf = in->strm_buf;
1✔
404
  len = snprintf(pbuf->buf, pbuf->buflen-1, "%s", cmd);
1✔
405
  pbuf->remaining = pbuf->buflen - len;
1✔
406
  pbuf->current = pbuf->buf;
1✔
407

1✔
408
  buf[sizeof(buf)-1] = '\0';
409

1✔
410
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
411
  xerrno = errno;
1✔
412

1✔
413
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
414
    xerrno, strerror(xerrno));
1✔
415
  ck_assert_msg(strcmp(buf, first_cmd) == 0, "Expected string '%s', got '%s'",
416
    first_cmd, buf);
1✔
417

418
  memset(buf, '\0', sizeof(buf));
419
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
1✔
420
  xerrno = errno;
1✔
421

1✔
422
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
423
    xerrno, strerror(xerrno));
1✔
424
  ck_assert_msg(strcmp(buf, second_cmd) == 0, "Expected string '%s', got '%s'",
425
    second_cmd, buf);
1✔
426

427
  ck_assert_msg(pbuf->remaining == (size_t) xfer_bufsz,
428
    "Expected %d remaining bytes, got %lu", xfer_bufsz,
1✔
429
    (unsigned long) pbuf->remaining);
430

431
  pr_netio_close(in);
432
  pr_netio_close(out);
1✔
433
}
1✔
434
END_TEST
1✔
435

436
START_TEST (netio_telnet_gets_no_newline_test) {
437
  char buf[8], *cmd, *res;
1✔
438
  pr_netio_stream_t *in, *out;
1✔
439
  pr_buffer_t *pbuf;
1✔
440
  int len, xerrno;
1✔
441

1✔
442
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
443
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
444

1✔
445
  cmd = "Hello, World!";
446

1✔
447
  pr_netio_buffer_alloc(in);
448
  pbuf = in->strm_buf;
1✔
449
  len = snprintf(pbuf->buf, pbuf->buflen-1, "%s", cmd);
1✔
450
  pbuf->remaining = pbuf->buflen - len;
1✔
451
  pbuf->current = pbuf->buf;
1✔
452

1✔
453
  buf[sizeof(buf)-1] = '\0';
454

1✔
455
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
456
  xerrno = errno;
1✔
457

1✔
458
  ck_assert_msg(res == NULL, "Read in string unexpectedly, got '%s'", buf);
459
  ck_assert_msg(xerrno == E2BIG, "Failed to set errno to E2BIG, got (%d) %s",
1✔
460
    xerrno, strerror(xerrno));
1✔
461

462
  pr_netio_close(in);
463
  pr_netio_close(out);
1✔
464
}
1✔
465
END_TEST
1✔
466

467
START_TEST (netio_telnet_gets_telnet_will_test) {
468
  char buf[256], *cmd, *res, telnet_opt;
1✔
469
  pr_netio_stream_t *in, *out;
1✔
470
  pr_buffer_t *pbuf;
1✔
471
  int len, out_fd, xerrno;
1✔
472

1✔
473
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
474

1✔
475
  out_fd = open_tmpfile();
476
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, out_fd, PR_NETIO_IO_WR);
1✔
477

1✔
478
  cmd = "Hello, World!\n";
479

1✔
480
  pr_netio_buffer_alloc(in);
481
  pbuf = in->strm_buf;
1✔
482

1✔
483
  telnet_opt = 7;
484
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%c%cWorld!\n", TELNET_IAC,
1✔
485
    TELNET_WILL, telnet_opt);
1✔
486
  pbuf->remaining = pbuf->buflen - len;
487
  pbuf->current = pbuf->buf;
1✔
488

1✔
489
  buf[sizeof(buf)-1] = '\0';
490

1✔
491
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
492
  xerrno = errno;
1✔
493

1✔
494
  pr_netio_close(in);
495

1✔
496
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
497
    xerrno, strerror(xerrno));
1✔
498
  ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
499
    buf);
1✔
500

501
  /* Rewind the output stream fd. */
502
  lseek(out_fd, 0, SEEK_SET);
503
  len = read(out_fd, buf, sizeof(buf)-1);
1✔
504
  pr_netio_close(out);
1✔
505

1✔
506
  ck_assert_msg(len == 3, "Expected to read 3 bytes from output stream, got %d",
507
    len);
1✔
508
  ck_assert_msg(buf[0] == (char) TELNET_IAC, "Expected IAC at index 0, got %d",
509
    buf[0]);
1✔
510
  ck_assert_msg(buf[1] == (char) TELNET_DONT, "Expected DONT at index 1, got %d",
511
    buf[1]);
1✔
512
  ck_assert_msg(buf[2] == telnet_opt, "Expected opt '%c' at index 2, got %c",
513
    telnet_opt, buf[2]);
1✔
514

515
  test_cleanup();
516
}
1✔
517
END_TEST
1✔
518

519
START_TEST (netio_telnet_gets_telnet_bare_will_test) {
520
  char buf[256], *cmd, *res, telnet_opt;
1✔
521
  pr_netio_stream_t *in, *out;
1✔
522
  pr_buffer_t *pbuf;
1✔
523
  int len, xerrno;
1✔
524

1✔
525
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
526
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
527

1✔
528
  cmd = "Hello, World!\n";
529

1✔
530
  pr_netio_buffer_alloc(in);
531
  pbuf = in->strm_buf;
1✔
532

1✔
533
  telnet_opt = 7;
534
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%cWorld!\n", TELNET_WILL,
1✔
535
    telnet_opt);
1✔
536
  pbuf->remaining = pbuf->buflen - len;
537
  pbuf->current = pbuf->buf;
1✔
538

1✔
539
  buf[sizeof(buf)-1] = '\0';
540

1✔
541
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
542
  xerrno = errno;
1✔
543

1✔
544
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
545
    xerrno, strerror(xerrno));
1✔
546
  ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
547
    7, cmd, 7, buf);
1✔
548
  ck_assert_msg(buf[7] == (char) TELNET_WILL, "Expected WILL at index 7, got %d",
549
    buf[7]);
1✔
550
  ck_assert_msg(buf[8] == telnet_opt, "Expected Telnet opt %c at index 8, got %d",
551
    telnet_opt, buf[8]);
1✔
552
  ck_assert_msg(strcmp(buf + 9, cmd + 7) == 0, "Expected string '%s', got '%s'",
553
    cmd + 7, buf + 9);
1✔
554

555
  pr_netio_close(in);
556
  pr_netio_close(out);
1✔
557
}
1✔
558
END_TEST
1✔
559

560
START_TEST (netio_telnet_gets_telnet_will_multi_read_test) {
561
  char buf[256], *cmd, *res, telnet_opt;
1✔
562
  pr_netio_stream_t *in, *out;
1✔
563
  pr_buffer_t *pbuf;
1✔
564
  int len, out_fd, xerrno;
1✔
565

1✔
566
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
567

1✔
568
  out_fd = open_tmpfile();
569
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, out_fd, PR_NETIO_IO_WR);
1✔
570

1✔
571
  cmd = "Hello, World!\n";
572

1✔
573
  pr_netio_buffer_alloc(in);
574
  pbuf = in->strm_buf;
1✔
575

1✔
576
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%c", TELNET_IAC,
577
    TELNET_WILL);
1✔
578
  pbuf->remaining = pbuf->buflen - len;
579
  pbuf->current = pbuf->buf;
1✔
580

1✔
581
  buf[sizeof(buf)-1] = '\0';
582

1✔
583
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
584
  xerrno = errno;
1✔
585

1✔
586
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
587
    xerrno, strerror(xerrno));
1✔
588
  ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
589
    7, cmd, 7, buf);
1✔
590

591
  /* Fill up the input stream's buffer with the rest of the Telnet WILL
592
   * sequence.
593
   */
594
  telnet_opt = 7;
595
  len = snprintf(pbuf->buf, pbuf->buflen-1, "%cWorld!\n", telnet_opt);
1✔
596
  pbuf->remaining = pbuf->buflen - len;
1✔
597
  pbuf->current = pbuf->buf;
1✔
598

1✔
599
  /* Read again, to see if the state was preserved across multiple calls
600
   * to pr_netio_telnet_gets().
601
   */
602
  res = pr_netio_telnet_gets(buf + 7, sizeof(buf)-8, in, out);
603
  xerrno = errno;
1✔
604

1✔
605
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
606
    xerrno, strerror(xerrno));
1✔
607
  ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'",
608
    cmd, buf);
1✔
609

610
  pr_netio_close(in);
611

1✔
612
  /* Rewind the output stream fd. */
613
  lseek(out_fd, 0, SEEK_SET);
614
  len = read(out_fd, buf, sizeof(buf)-1);
1✔
615
  pr_netio_close(out);
1✔
616

1✔
617
  ck_assert_msg(len == 3, "Expected to read 3 bytes from output stream, got %d",
618
    len);
1✔
619
  ck_assert_msg(buf[0] == (char) TELNET_IAC, "Expected IAC at index 0, got %d",
620
    buf[0]);
1✔
621
  ck_assert_msg(buf[1] == (char) TELNET_DONT, "Expected DONT at index 1, got %d",
622
    buf[1]);
1✔
623
  ck_assert_msg(buf[2] == telnet_opt, "Expected %c at index 2, got %d",
624
    telnet_opt, buf[2]);
1✔
625

626
  test_cleanup();
627
}
1✔
628
END_TEST
1✔
629

630
START_TEST (netio_telnet_gets_telnet_wont_test) {
631
  char buf[256], *cmd, *res, telnet_opt;
1✔
632
  pr_netio_stream_t *in, *out;
1✔
633
  pr_buffer_t *pbuf;
1✔
634
  int len, out_fd, xerrno;
1✔
635

1✔
636
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
637

1✔
638
  out_fd = open_tmpfile();
639
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, out_fd, PR_NETIO_IO_WR);
1✔
640

1✔
641
  cmd = "Hello, World!\n";
642

1✔
643
  pr_netio_buffer_alloc(in);
644
  pbuf = in->strm_buf;
1✔
645

1✔
646
  telnet_opt = 7;
647
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%c%cWorld!\n", TELNET_IAC,
1✔
648
    TELNET_WONT, telnet_opt);
1✔
649
  pbuf->remaining = pbuf->buflen - len;
650
  pbuf->current = pbuf->buf;
1✔
651

1✔
652
  buf[sizeof(buf)-1] = '\0';
653

1✔
654
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
655
  xerrno = errno;
1✔
656

1✔
657
  pr_netio_close(in);
658

1✔
659
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
660
    xerrno, strerror(xerrno));
1✔
661
  ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
662
    buf);
1✔
663

664
  /* Rewind the output stream fd. */
665
  lseek(out_fd, 0, SEEK_SET);
666
  len = read(out_fd, buf, sizeof(buf)-1);
1✔
667
  pr_netio_close(out);
1✔
668

1✔
669
  ck_assert_msg(len == 3, "Expected to read 3 bytes from output stream, got %d",
670
    len);
1✔
671
  ck_assert_msg(buf[0] == (char) TELNET_IAC, "Expected IAC at index 0, got %d",
672
    buf[0]);
1✔
673
  ck_assert_msg(buf[1] == (char) TELNET_DONT, "Expected DONT at index 1, got %d",
674
    buf[1]);
1✔
675
  ck_assert_msg(buf[2] == telnet_opt, "Expected opt '%c' at index 2, got %c",
676
    telnet_opt, buf[2]);
1✔
677

678
  test_cleanup();
679
}
1✔
680
END_TEST
1✔
681

682
START_TEST (netio_telnet_gets_telnet_bare_wont_test) {
683
  char buf[256], *cmd, *res, telnet_opt;
1✔
684
  pr_netio_stream_t *in, *out;
1✔
685
  pr_buffer_t *pbuf;
1✔
686
  int len, xerrno;
1✔
687

1✔
688
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
689
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
690

1✔
691
  cmd = "Hello, World!\n";
692

1✔
693
  pr_netio_buffer_alloc(in);
694
  pbuf = in->strm_buf;
1✔
695

1✔
696
  telnet_opt = 7;
697
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%cWorld!\n", TELNET_WONT,
1✔
698
    telnet_opt);
1✔
699
  pbuf->remaining = pbuf->buflen - len;
700
  pbuf->current = pbuf->buf;
1✔
701

1✔
702
  buf[sizeof(buf)-1] = '\0';
703

1✔
704
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
705
  xerrno = errno;
1✔
706

1✔
707
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
708
    xerrno, strerror(xerrno));
1✔
709
  ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
710
    7, cmd, 7, buf);
1✔
711
  ck_assert_msg(buf[7] == (char) TELNET_WONT, "Expected WONT at index 7, got %d",
712
    buf[7]);
1✔
713
  ck_assert_msg(buf[8] == telnet_opt, "Expected Telnet opt %c at index 8, got %d",
714
    telnet_opt, buf[8]);
1✔
715
  ck_assert_msg(strcmp(buf + 9, cmd + 7) == 0, "Expected string '%s', got '%s'",
716
    cmd + 7, buf + 9);
1✔
717

718
  pr_netio_close(in);
719
  pr_netio_close(out);
1✔
720
}
1✔
721
END_TEST
1✔
722

723
START_TEST (netio_telnet_gets_telnet_do_test) {
724
  char buf[256], *cmd, *res, telnet_opt;
1✔
725
  pr_netio_stream_t *in, *out;
1✔
726
  pr_buffer_t *pbuf;
1✔
727
  int len, out_fd, xerrno;
1✔
728

1✔
729
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
730

1✔
731
  out_fd = open_tmpfile();
732
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, out_fd, PR_NETIO_IO_WR);
1✔
733

1✔
734
  cmd = "Hello, World!\n";
735

1✔
736
  pr_netio_buffer_alloc(in);
737
  pbuf = in->strm_buf;
1✔
738

1✔
739
  telnet_opt = 7;
740
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%c%cWorld!\n", TELNET_IAC,
1✔
741
    TELNET_DO, telnet_opt);
1✔
742
  pbuf->remaining = pbuf->buflen - len;
743
  pbuf->current = pbuf->buf;
1✔
744

1✔
745
  buf[sizeof(buf)-1] = '\0';
746

1✔
747
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
748
  xerrno = errno;
1✔
749

1✔
750
  pr_netio_close(in);
751

1✔
752
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
753
    xerrno, strerror(xerrno));
1✔
754
  ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
755
    buf);
1✔
756

757
  /* Rewind the output stream fd. */
758
  lseek(out_fd, 0, SEEK_SET);
759
  len = read(out_fd, buf, sizeof(buf)-1);
1✔
760
  pr_netio_close(out);
1✔
761

1✔
762
  ck_assert_msg(len == 3, "Expected to read 3 bytes from output stream, got %d",
763
    len);
1✔
764
  ck_assert_msg(buf[0] == (char) TELNET_IAC, "Expected IAC at index 0, got %d",
765
    buf[0]);
1✔
766
  ck_assert_msg(buf[1] == (char) TELNET_WONT, "Expected WONT at index 1, got %d",
767
    buf[1]);
1✔
768
  ck_assert_msg(buf[2] == telnet_opt, "Expected opt '%c' at index 2, got %c",
769
    telnet_opt, buf[2]);
1✔
770

771
  test_cleanup();
772
}
1✔
773
END_TEST
1✔
774

775
START_TEST (netio_telnet_gets_telnet_bare_do_test) {
776
  char buf[256], *cmd, *res, telnet_opt;
1✔
777
  pr_netio_stream_t *in, *out;
1✔
778
  pr_buffer_t *pbuf;
1✔
779
  int len, xerrno;
1✔
780

1✔
781
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
782
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
783

1✔
784
  cmd = "Hello, World!\n";
785

1✔
786
  pr_netio_buffer_alloc(in);
787
  pbuf = in->strm_buf;
1✔
788

1✔
789
  telnet_opt = 7;
790
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%cWorld!\n", TELNET_DO,
1✔
791
    telnet_opt);
1✔
792
  pbuf->remaining = pbuf->buflen - len;
793
  pbuf->current = pbuf->buf;
1✔
794

1✔
795
  buf[sizeof(buf)-1] = '\0';
796

1✔
797
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
798
  xerrno = errno;
1✔
799

1✔
800
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
801
    xerrno, strerror(xerrno));
1✔
802
  ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
803
    7, cmd, 7, buf);
1✔
804
  ck_assert_msg(buf[7] == (char) TELNET_DO, "Expected DO at index 7, got %d",
805
    buf[7]);
1✔
806
  ck_assert_msg(buf[8] == telnet_opt, "Expected Telnet opt %c at index 8, got %d",
807
    telnet_opt, buf[8]);
1✔
808
  ck_assert_msg(strcmp(buf + 9, cmd + 7) == 0, "Expected string '%s', got '%s'",
809
    cmd + 7, buf + 9);
1✔
810

811
  pr_netio_close(in);
812
  pr_netio_close(out);
1✔
813
}
1✔
814
END_TEST
1✔
815

816
START_TEST (netio_telnet_gets_telnet_dont_test) {
817
  char buf[256], *cmd, *res, telnet_opt;
1✔
818
  pr_netio_stream_t *in, *out;
1✔
819
  pr_buffer_t *pbuf;
1✔
820
  int len, out_fd, xerrno;
1✔
821

1✔
822
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
823

1✔
824
  out_fd = open_tmpfile();
825
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, out_fd, PR_NETIO_IO_WR);
1✔
826

1✔
827
  cmd = "Hello, World!\n";
828

1✔
829
  pr_netio_buffer_alloc(in);
830
  pbuf = in->strm_buf;
1✔
831

1✔
832
  telnet_opt = 7;
833
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%c%cWorld!\n", TELNET_IAC,
1✔
834
    TELNET_DONT, telnet_opt);
1✔
835
  pbuf->remaining = pbuf->buflen - len;
836
  pbuf->current = pbuf->buf;
1✔
837

1✔
838
  buf[sizeof(buf)-1] = '\0';
839

1✔
840
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
841
  xerrno = errno;
1✔
842

1✔
843
  pr_netio_close(in);
844

1✔
845
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
846
    xerrno, strerror(xerrno));
1✔
847
  ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
848
    buf);
1✔
849

850
  /* Rewind the output stream fd. */
851
  lseek(out_fd, 0, SEEK_SET);
852
  len = read(out_fd, buf, sizeof(buf)-1);
1✔
853
  pr_netio_close(out);
1✔
854

1✔
855
  ck_assert_msg(len == 3, "Expected to read 3 bytes from output stream, got %d",
856
    len);
1✔
857
  ck_assert_msg(buf[0] == (char) TELNET_IAC, "Expected IAC at index 0, got %d",
858
    buf[0]);
1✔
859
  ck_assert_msg(buf[1] == (char) TELNET_WONT, "Expected WONT at index 1, got %d",
860
    buf[1]);
1✔
861
  ck_assert_msg(buf[2] == telnet_opt, "Expected opt '%c' at index 2, got %c",
862
    telnet_opt, buf[2]);
1✔
863

864
  test_cleanup();
865
}
1✔
866
END_TEST
1✔
867

868
START_TEST (netio_telnet_gets_telnet_bare_dont_test) {
869
  char buf[256], *cmd, *res, telnet_opt;
1✔
870
  pr_netio_stream_t *in, *out;
1✔
871
  pr_buffer_t *pbuf;
1✔
872
  int len, xerrno;
1✔
873

1✔
874
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
875
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
876

1✔
877
  cmd = "Hello, World!\n";
878

1✔
879
  pr_netio_buffer_alloc(in);
880
  pbuf = in->strm_buf;
1✔
881

1✔
882
  telnet_opt = 7;
883
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%cWorld!\n", TELNET_DONT,
1✔
884
    telnet_opt);
1✔
885
  pbuf->remaining = pbuf->buflen - len;
886
  pbuf->current = pbuf->buf;
1✔
887

1✔
888
  buf[sizeof(buf)-1] = '\0';
889

1✔
890
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
891
  xerrno = errno;
1✔
892

1✔
893
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
894
    xerrno, strerror(xerrno));
1✔
895
  ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
896
    7, cmd, 7, buf);
1✔
897
  ck_assert_msg(buf[7] == (char) TELNET_DONT, "Expected DONT at index 7, got %d",
898
    buf[7]);
1✔
899
  ck_assert_msg(buf[8] == telnet_opt, "Expected Telnet opt %c at index 8, got %d",
900
    telnet_opt, buf[8]);
1✔
901
  ck_assert_msg(strcmp(buf + 9, cmd + 7) == 0, "Expected string '%s', got '%s'",
902
    cmd + 7, buf + 9);
1✔
903

904
  pr_netio_close(in);
905
  pr_netio_close(out);
1✔
906
}
1✔
907
END_TEST
1✔
908

909
START_TEST (netio_telnet_gets_telnet_ip_test) {
910
  char buf[256], *cmd, *res;
1✔
911
  pr_netio_stream_t *in, *out;
1✔
912
  pr_buffer_t *pbuf;
1✔
913
  int len, xerrno;
1✔
914

1✔
915
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
916
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
917

1✔
918
  cmd = "Hello, World!\n";
919

1✔
920
  pr_netio_buffer_alloc(in);
921
  pbuf = in->strm_buf;
1✔
922

1✔
923
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%cWorld!\n", TELNET_IAC,
924
    TELNET_IP);
1✔
925
  pbuf->remaining = pbuf->buflen - len;
926
  pbuf->current = pbuf->buf;
1✔
927

1✔
928
  buf[sizeof(buf)-1] = '\0';
929

1✔
930
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
931
  xerrno = errno;
1✔
932

1✔
933
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
934
    xerrno, strerror(xerrno));
1✔
935
  ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
936
    buf);
1✔
937

938
  pr_netio_close(in);
939
  pr_netio_close(out);
1✔
940
}
1✔
941
END_TEST
1✔
942

943
START_TEST (netio_telnet_gets_telnet_bare_ip_test) {
944
  char buf[256], *cmd, *res;
1✔
945
  pr_netio_stream_t *in, *out;
1✔
946
  pr_buffer_t *pbuf;
1✔
947
  int len, xerrno;
1✔
948

1✔
949
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
950
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
951

1✔
952
  cmd = "Hello, World!\n";
953

1✔
954
  pr_netio_buffer_alloc(in);
955
  pbuf = in->strm_buf;
1✔
956

1✔
957
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %cWorld!\n", TELNET_IP);
958
  pbuf->remaining = pbuf->buflen - len;
1✔
959
  pbuf->current = pbuf->buf;
1✔
960

1✔
961
  buf[sizeof(buf)-1] = '\0';
962

1✔
963
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
964
  xerrno = errno;
1✔
965

1✔
966
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
967
    xerrno, strerror(xerrno));
1✔
968
  ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
969
    7, cmd, 7, buf);
1✔
970
  ck_assert_msg(buf[7] == (char) TELNET_IP, "Expected IP at index 7, got %d",
971
    buf[7]);
1✔
972
  ck_assert_msg(strcmp(buf + 8, cmd + 7) == 0, "Expected string '%s', got '%s'",
973
    cmd + 7, buf + 8);
1✔
974

975
  pr_netio_close(in);
976
  pr_netio_close(out);
1✔
977
}
1✔
978
END_TEST
1✔
979

980
START_TEST (netio_telnet_gets_telnet_dm_test) {
981
  char buf[256], *cmd, *res;
1✔
982
  pr_netio_stream_t *in, *out;
1✔
983
  pr_buffer_t *pbuf;
1✔
984
  int len, xerrno;
1✔
985

1✔
986
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
987
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
988

1✔
989
  cmd = "Hello, World!\n";
990

1✔
991
  pr_netio_buffer_alloc(in);
992
  pbuf = in->strm_buf;
1✔
993

1✔
994
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%cWorld!\n", TELNET_IAC,
995
    TELNET_DM);
1✔
996
  pbuf->remaining = pbuf->buflen - len;
997
  pbuf->current = pbuf->buf;
1✔
998

1✔
999
  buf[sizeof(buf)-1] = '\0';
1000

1✔
1001
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
1002
  xerrno = errno;
1✔
1003

1✔
1004
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
1005
    xerrno, strerror(xerrno));
1✔
1006
  ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
1007
    buf);
1✔
1008

1009
  pr_netio_close(in);
1010
  pr_netio_close(out);
1✔
1011
}
1✔
1012
END_TEST
1✔
1013

1014
START_TEST (netio_telnet_gets_telnet_bare_dm_test) {
1015
  char buf[256], *cmd, *res;
1✔
1016
  pr_netio_stream_t *in, *out;
1✔
1017
  pr_buffer_t *pbuf;
1✔
1018
  int len, xerrno;
1✔
1019

1✔
1020
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1021
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
1022

1✔
1023
  cmd = "Hello, World!\n";
1024

1✔
1025
  pr_netio_buffer_alloc(in);
1026
  pbuf = in->strm_buf;
1✔
1027

1✔
1028
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %cWorld!\n", TELNET_DM);
1029
  pbuf->remaining = pbuf->buflen - len;
1✔
1030
  pbuf->current = pbuf->buf;
1✔
1031

1✔
1032
  buf[sizeof(buf)-1] = '\0';
1033

1✔
1034
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
1035
  xerrno = errno;
1✔
1036

1✔
1037
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
1038
    xerrno, strerror(xerrno));
1✔
1039
  ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
1040
    7, cmd, 7, buf);
1✔
1041
  ck_assert_msg(buf[7] == (char) TELNET_DM, "Expected DM at index 7, got %d",
1042
    buf[7]);
1✔
1043
  ck_assert_msg(strcmp(buf + 8, cmd + 7) == 0, "Expected string '%s', got '%s'",
1044
    cmd + 7, buf + 8);
1✔
1045

1046
  pr_netio_close(in);
1047
  pr_netio_close(out);
1✔
1048
}
1✔
1049
END_TEST
1✔
1050

1051
START_TEST (netio_telnet_gets_telnet_single_iac_test) {
1052
  char buf[256], *cmd, *res;
1✔
1053
  pr_netio_stream_t *in, *out;
1✔
1054
  pr_buffer_t *pbuf;
1✔
1055
  int len, xerrno;
1✔
1056

1✔
1057
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1058
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
1059

1✔
1060
  cmd = "Hello, World!\n";
1061

1✔
1062
  pr_netio_buffer_alloc(in);
1063
  pbuf = in->strm_buf;
1✔
1064

1✔
1065
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %cWorld!\n", TELNET_IAC);
1066
  pbuf->remaining = pbuf->buflen - len;
1✔
1067
  pbuf->current = pbuf->buf;
1✔
1068

1✔
1069
  buf[sizeof(buf)-1] = '\0';
1070

1✔
1071
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
1072
  xerrno = errno;
1✔
1073

1✔
1074
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
1075
    xerrno, strerror(xerrno));
1✔
1076
  ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
1077
    7, cmd, 7, buf);
1✔
1078
  ck_assert_msg(buf[7] == (char) TELNET_IAC, "Expected IAC at index 7, got %d",
1079
    buf[7]);
1✔
1080
  ck_assert_msg(strcmp(buf + 8, cmd + 7) == 0, "Expected string '%s', got '%s'",
1081
    cmd + 7, buf + 8);
1✔
1082

1083
  pr_netio_close(in);
1084
  pr_netio_close(out);
1✔
1085
}
1✔
1086
END_TEST
1✔
1087

1088
START_TEST (netio_telnet_gets_bug3521_test) {
1089
  char buf[10], *res, telnet_opt;
1✔
1090
  pr_netio_stream_t *in, *out;
1✔
1091
  pr_buffer_t *pbuf;
1✔
1092
  int len, xerrno;
1✔
1093

1✔
1094
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1095
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
1096

1✔
1097
  pr_netio_buffer_alloc(in);
1098
  pbuf = in->strm_buf;
1✔
1099

1✔
1100
  telnet_opt = 7;
1101
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%c%c%cWorld!\n",
1✔
1102
    TELNET_IAC, TELNET_IAC, TELNET_WILL, telnet_opt);
1✔
1103
  pbuf->remaining = pbuf->buflen - len;
1104
  pbuf->current = pbuf->buf;
1✔
1105

1✔
1106
  buf[sizeof(buf)-1] = '\0';
1107

1✔
1108
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
1109
  xerrno = errno;
1✔
1110

1✔
1111
  ck_assert_msg(res == NULL, "Expected null");
1112
  ck_assert_msg(xerrno == E2BIG, "Failed to set errno to E2BIG, got %s (%d)",
1✔
1113
    strerror(xerrno), xerrno);
1✔
1114

1115
  pr_netio_close(in);
1116
  pr_netio_close(out);
1✔
1117
}
1✔
1118
END_TEST
1✔
1119

1120
START_TEST (netio_telnet_gets_bug3697_test) {
1121
  char buf[256], *cmd, *res;
1✔
1122
  pr_netio_stream_t *in, *out;
1✔
1123
  pr_buffer_t *pbuf;
1✔
1124
  int len, xerrno;
1✔
1125

1✔
1126
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1127
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
1128

1✔
1129
  cmd = "Hello, World!\n";
1130

1✔
1131
  pr_netio_buffer_alloc(in);
1132
  pbuf = in->strm_buf;
1✔
1133

1✔
1134
  len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%cWorld!\n", TELNET_IAC,
1135
    TELNET_IAC);
1✔
1136
  pbuf->remaining = pbuf->buflen - len;
1137
  pbuf->current = pbuf->buf;
1✔
1138

1✔
1139
  buf[sizeof(buf)-1] = '\0';
1140

1✔
1141
  res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
1142
  xerrno = errno;
1✔
1143

1✔
1144
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
1145
    xerrno, strerror(xerrno));
1✔
1146
  ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
1147
    7, cmd, 7, buf);
1✔
1148
  ck_assert_msg(buf[7] == (char) TELNET_IAC, "Expected IAC at index 7, got %d",
1149
    buf[7]);
1✔
1150
  ck_assert_msg(strcmp(buf + 8, cmd + 7) == 0, "Expected string '%s', got '%s'",
1151
    cmd + 7, buf + 8);
1✔
1152

1153
  pr_netio_close(in);
1154
  pr_netio_close(out);
1✔
1155
}
1✔
1156
END_TEST
1✔
1157

1158
START_TEST (netio_telnet_gets_eof_test) {
1159
  char buf[256], *cmd, *res;
1✔
1160
  pr_netio_stream_t *in, *out;
1✔
1161
  pr_buffer_t *pbuf;
1✔
1162
  int len, xerrno;
1✔
1163

1✔
1164
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1165
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
1166

1✔
1167
  cmd = "Hello, World!";
1168

1✔
1169
  pr_netio_buffer_alloc(in);
1170
  pbuf = in->strm_buf;
1✔
1171

1✔
1172
  len = snprintf(pbuf->buf, pbuf->buflen-1, "%s", cmd);
1173
  pbuf->remaining = pbuf->buflen - len;
1✔
1174
  pbuf->current = pbuf->buf;
1✔
1175

1✔
1176
  buf[sizeof(buf)-1] = '\0';
1177

1✔
1178
  /* In this scenario, we have not supplied an LF, but the resulting buffer
1179
   * is terminated with a NUL because of the end-of-stream (or error) checks
1180
   * in pr_netio_telnet_gets(), when we read the input stream for more data
1181
   * looking for that LF.
1182
   */
1183
  res = pr_netio_telnet_gets(buf, strlen(cmd) + 2, in, out);
1184
  xerrno = errno;
1✔
1185

1✔
1186
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
1187
    xerrno, strerror(xerrno));
1✔
1188
  ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
1189
    buf);
1✔
1190

1191
  pr_netio_close(in);
1192
  pr_netio_close(out);
1✔
1193
}
1✔
1194
END_TEST
1✔
1195

1196
START_TEST (netio_telnet_gets_random_data_test) {
1197
  register unsigned int i;
1✔
1198
  char *buf, *res;
1✔
1199
  size_t bufsz;
1✔
1200
  pr_netio_stream_t *in, *out;
1✔
1201
  pr_buffer_t *pbuf;
1✔
1202
  int xerrno;
1✔
1203

1✔
1204
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1205
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
1206

1✔
1207
  pbuf = pr_netio_buffer_alloc(in);
1208

1✔
1209
  /* Fill the input buffer with random values; save the last two bytes for
1210
   * the CRLF.
1211
   */
1212
  for (i = 0; i < (pbuf->buflen - 2); i++) {
1213
    long r;
65,536✔
1214

65,534✔
1215
    r = pr_random_next(CHAR_MIN, CHAR_MAX);
1216
    pbuf->buf[i] = (char) r;
65,534✔
1217
  }
65,534✔
1218
  pbuf->buf[i++] = '\r';
1219
  pbuf->buf[i++] = '\n';
1✔
1220

1✔
1221
  pbuf->remaining = 0;
1222
  pbuf->current = pbuf->buf;
1✔
1223

1✔
1224
  /* Make sure our output buffer is of sufficient size. */
1225
  bufsz = pbuf->buflen + 1;
1226
  buf = pcalloc(p, bufsz);
1✔
1227

1✔
1228
  res = pr_netio_telnet_gets(buf, bufsz, in, out);
1229
  xerrno = errno;
1✔
1230

1✔
1231
  ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
1232
    xerrno, strerror(xerrno));
1✔
1233

1234
  pr_netio_close(in);
1235
  pr_netio_close(out);
1✔
1236
}
1✔
1237
END_TEST
1✔
1238

1239
START_TEST (netio_telnet_gets2_single_line_test) {
1240
  int res;
1✔
1241
  char buf[256], *cmd;
1✔
1242
  size_t cmd_len;
1✔
1243
  pr_netio_stream_t *in, *out;
1✔
1244
  pr_buffer_t *pbuf;
1✔
1245
  int len, xerrno;
1✔
1246

1✔
1247
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1248
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
1249

1✔
1250
  cmd = "Hello, World!\n";
1251
  cmd_len = strlen(cmd);
1✔
1252

1✔
1253
  pr_netio_buffer_alloc(in);
1254
  pbuf = in->strm_buf;
1✔
1255
  len = snprintf(pbuf->buf, pbuf->buflen-1, "%s", cmd);
1✔
1256
  pbuf->remaining = pbuf->buflen - len;
1✔
1257
  pbuf->current = pbuf->buf;
1✔
1258

1✔
1259
  buf[sizeof(buf)-1] = '\0';
1260

1✔
1261
  res = pr_netio_telnet_gets2(buf, sizeof(buf)-1, in, out);
1262
  xerrno = errno;
1✔
1263

1✔
1264
  ck_assert_msg(res > 0, "Failed to get string from stream: (%d) %s",
1265
    xerrno, strerror(xerrno));
1✔
1266
  ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
1267
    buf);
1✔
1268

1269
  ck_assert_msg((size_t) res == cmd_len, "Expected length %lu, got %d",
1270
    (unsigned long) cmd_len, res);
1✔
1271
  ck_assert_msg(pbuf->remaining == (size_t) xfer_bufsz,
1272
    "Expected %d remaining bytes, got %lu", xfer_bufsz,
1✔
1273
    (unsigned long) pbuf->remaining);
1274

1275
  pr_netio_close(in);
1276
  pr_netio_close(out);
1✔
1277
}
1✔
1278
END_TEST
1✔
1279

1280
START_TEST (netio_telnet_gets2_single_line_crnul_test) {
1281
  int res;
1✔
1282
  char buf[256], *cmd;
1✔
1283
  size_t cmd_len;
1✔
1284
  pr_netio_stream_t *in, *out;
1✔
1285
  pr_buffer_t *pbuf;
1✔
1286
  int xerrno;
1✔
1287

1✔
1288
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1289
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
1290

1✔
1291
  /* See Bug#4167.  We cannot use strlen(3) due to the embedded NUL. */
1292
  cmd = "Hello, \015\000World!\n";
1293
  cmd_len = 14;
1✔
1294

1✔
1295
  pr_netio_buffer_alloc(in);
1296
  pbuf = in->strm_buf;
1✔
1297
  memcpy(pbuf->buf, cmd, cmd_len);
1✔
1298
  pbuf->remaining = pbuf->buflen - cmd_len;
1✔
1299
  pbuf->current = pbuf->buf;
1✔
1300

1✔
1301
  buf[sizeof(buf)-1] = '\0';
1302

1✔
1303
  res = pr_netio_telnet_gets2(buf, sizeof(buf)-1, in, out);
1304
  xerrno = errno;
1✔
1305

1✔
1306
  ck_assert_msg(res > 0, "Failed to get string from stream: (%d) %s",
1307
    xerrno, strerror(xerrno));
1✔
1308
  ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
1309
    buf);
1✔
1310

1311
  ck_assert_msg((size_t) res == cmd_len, "Expected length %lu, got %d",
1312
    (unsigned long) cmd_len, res);
1✔
1313
  ck_assert_msg(pbuf->remaining == (size_t) xfer_bufsz,
1314
    "Expected %d remaining bytes, got %lu", xfer_bufsz,
1✔
1315
    (unsigned long) pbuf->remaining);
1316

1317
  pr_netio_close(in);
1318
  pr_netio_close(out);
1✔
1319
}
1✔
1320
END_TEST
1✔
1321

1322
START_TEST (netio_telnet_gets2_single_line_lf_test) {
1323
  int res;
1✔
1324
  char buf[256], *cmd;
1✔
1325
  size_t cmd_len;
1✔
1326
  pr_netio_stream_t *in, *out;
1✔
1327
  pr_buffer_t *pbuf;
1✔
1328
  int xerrno;
1✔
1329

1✔
1330
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1331
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
1332

1✔
1333
  cmd = "Hello, \012World!\n";
1334
  cmd_len = strlen(cmd);
1✔
1335

1✔
1336
  pr_netio_buffer_alloc(in);
1337
  pbuf = in->strm_buf;
1✔
1338
  memcpy(pbuf->buf, cmd, cmd_len);
1✔
1339
  pbuf->remaining = pbuf->buflen - cmd_len;
1✔
1340
  pbuf->current = pbuf->buf;
1✔
1341

1✔
1342
  buf[sizeof(buf)-1] = '\0';
1343

1✔
1344
  res = pr_netio_telnet_gets2(buf, sizeof(buf)-1, in, out);
1345
  xerrno = errno;
1✔
1346

1✔
1347
  ck_assert_msg(res > 0, "Failed to get string from stream: (%d) %s",
1348
    xerrno, strerror(xerrno));
1✔
1349
  ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
1350
    buf);
1✔
1351

1352
  ck_assert_msg((size_t) res == cmd_len, "Expected length %lu, got %d",
1353
    (unsigned long) cmd_len, res);
1✔
1354
  ck_assert_msg(pbuf->remaining == (size_t) xfer_bufsz,
1355
    "Expected %d remaining bytes, got %lu", xfer_bufsz,
1✔
1356
    (unsigned long) pbuf->remaining);
1357

1358
  pr_netio_close(in);
1359
  pr_netio_close(out);
1✔
1360
}
1✔
1361
END_TEST
1✔
1362

1363
START_TEST (netio_telnet_gets2_random_data_test) {
1364
  register unsigned int i;
1✔
1365
  int res;
1✔
1366
  char *buf;
1✔
1367
  size_t bufsz;
1✔
1368
  pr_netio_stream_t *in, *out;
1✔
1369
  pr_buffer_t *pbuf;
1✔
1370
  int xerrno;
1✔
1371

1✔
1372
  in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1373
  out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1✔
1374

1✔
1375
  pbuf = pr_netio_buffer_alloc(in);
1376

1✔
1377
  /* Fill the input buffer with random values; save the last two bytes for
1378
   * the CRLF.
1379
   */
1380
  for (i = 0; i < (pbuf->buflen - 2); i++) {
1381
    long r;
65,536✔
1382

65,534✔
1383
    r = pr_random_next(CHAR_MIN, CHAR_MAX);
1384
    pbuf->buf[i] = (char) r;
65,534✔
1385
  }
65,534✔
1386
  pbuf->buf[i++] = '\r';
1387
  pbuf->buf[i++] = '\n';
1✔
1388

1✔
1389
  pbuf->remaining = 0;
1390
  pbuf->current = pbuf->buf;
1✔
1391

1✔
1392
  /* Make sure our output buffer is of sufficient size. */
1393
  bufsz = pbuf->buflen + 1;
1394
  buf = pcalloc(p, bufsz);
1✔
1395

1✔
1396
  res = pr_netio_telnet_gets2(buf, bufsz, in, out);
1397
  xerrno = errno;
1✔
1398

1✔
1399
  ck_assert_msg(res > 0, "Failed to get string from stream: (%d) %s",
1400
    xerrno, strerror(xerrno));
1✔
1401

1402
  pr_netio_close(in);
1403
  pr_netio_close(out);
1✔
1404
}
1✔
1405
END_TEST
1✔
1406

1407
static int netio_close_cb(pr_netio_stream_t *nstrm) {
1408
  return 0;
11✔
1409
}
11✔
1410

1411
static int netio_poll_cb(pr_netio_stream_t *nstrm) {
1412
  /* Always return >0, to indicate that we haven't timed out, AND that there
20✔
1413
   * is a writable fd available.
1414
   */
1415
  return 7;
1416
}
20✔
1417

1418
static int netio_read_eof = FALSE;
1419
static int netio_read_epipe = FALSE;
1420

1421
static int netio_read_cb(pr_netio_stream_t *nstrm, char *buf, size_t buflen) {
1422
  const char *text;
10✔
1423
  int res;
10✔
1424

10✔
1425
  if (netio_read_eof) {
1426
    netio_read_eof = FALSE;
10✔
1427
    return 0;
3✔
1428
  }
3✔
1429

1430
  if (netio_read_epipe) {
1431
    netio_read_epipe = FALSE;
7✔
1432
    errno = EPIPE;
3✔
1433
    return -1;
3✔
1434
  }
3✔
1435

1436
  text = "Hello, World!\r\n";
1437
  sstrncpy(buf, text, buflen);
4✔
1438

4✔
1439
  /* Make sure the next read returns EOF. */
1440
  netio_read_eof = TRUE;
1441

4✔
1442
  res = strlen(text);
1443
  return res;
4✔
1444
}
4✔
1445

1446
static int netio_write_epipe = FALSE;
1447

1448
static int netio_write_cb(pr_netio_stream_t *nstrm, char *buf, size_t buflen) {
1449
  if (netio_write_epipe) {
14✔
1450
    netio_write_epipe = FALSE;
14✔
1451
    errno = EPIPE;
6✔
1452
    return -1;
6✔
1453
  }
6✔
1454

1455
  return buflen;
1456
}
8✔
1457

1458
static int devnull_fd(void) {
1459
  int fd;
9✔
1460

9✔
1461
  fd = open("/dev/null", O_RDWR);
1462
  if (fd < 0) {
9✔
1463
    fprintf(stderr, "Error opening /dev/null: %s\n", strerror(errno));
9✔
1464
    return -1;
×
1465
  }
×
1466

1467
  return fd;
1468
}
1469

1470
static int netio_read_from_stream(int strm_type) {
1471
  int fd, res;
3✔
1472
  char buf[1024], *expected_text;
3✔
1473
  size_t expected_sz;
3✔
1474
  pr_netio_stream_t *nstrm;
3✔
1475

3✔
1476
  res = pr_netio_read(NULL, NULL, 0, 0);
1477
  if (res == 0) {
3✔
1478
    errno = EINVAL;
3✔
1479
    return -1;
×
1480
  }
×
1481

1482
  fd = devnull_fd();
1483
  if (fd < 0) {
3✔
1484
    return -1;
3✔
1485
  }
1486

1487
  nstrm = pr_netio_open(p, strm_type, fd, PR_NETIO_IO_RD);
1488
  if (nstrm == NULL) {
3✔
1489
    int xerrno = errno;
3✔
1490

×
1491
    pr_trace_msg("netio", 1, "error opening custom netio stream: %s",
1492
      strerror(xerrno));
×
1493
    errno = xerrno;
1494
    return -1;
×
1495
  }
×
1496

1497
  res = pr_netio_read(nstrm, NULL, 0, 0);
1498
  if (res == 0) {
3✔
1499
    pr_netio_close(nstrm);
3✔
1500
    errno = EINVAL;
×
1501
    return -1;
×
1502
  }
×
1503

1504
  res = pr_netio_read(nstrm, buf, 0, 0);
1505
  if (res == 0) {
3✔
1506
    pr_netio_close(nstrm);
3✔
1507
    errno = EINVAL;
×
1508
    return -1;
×
1509
  }
×
1510

1511
  expected_text = "Hello, World!\r\n";
1512
  expected_sz = strlen(expected_text);
3✔
1513

3✔
1514
  memset(buf, '\0', sizeof(buf));
1515
  res = pr_netio_read(nstrm, buf, sizeof(buf)-1, 1);
3✔
1516

3✔
1517
  if (res != (int) expected_sz) {
1518
    pr_trace_msg("netio", 1, "Expected %lu bytes, got %d",
3✔
1519
      (unsigned long) expected_sz, res);
×
1520
    pr_netio_close(nstrm);
1521

×
1522
    if (res < 0) {
1523
      return -1;
×
1524
    }
1525

1526
    errno = EIO;
1527
    return -1;
×
1528
  }
×
1529

1530
  if (strcmp(buf, expected_text) != 0) {
1531
    pr_trace_msg("netio", 1, "Expected '%s', got '%s'", expected_text, buf);
3✔
1532
    pr_netio_close(nstrm);
×
1533

×
1534
    errno = EIO;
1535
    return -1;
×
1536
  }
×
1537

1538
  netio_read_eof = TRUE;
1539
  res = pr_netio_read(nstrm, buf, sizeof(buf)-1, 1);
3✔
1540
  if (res > 0) {
3✔
1541
    pr_trace_msg("netio", 1, "Expected EOF (0), got %d", res);
3✔
1542
    pr_netio_close(nstrm);
×
1543

×
1544
    errno = EIO;
1545
    return -1;
×
1546
  }
×
1547

1548
  netio_read_epipe = TRUE;
1549
  res = pr_netio_read(nstrm, buf, sizeof(buf)-1, sizeof(buf)-1);
3✔
1550
  if (res >= 0) {
3✔
1551
    pr_trace_msg("netio", 1, "Expected EPIPE (-1), got %d", res);
3✔
1552
    pr_netio_close(nstrm);
×
1553

×
1554
    errno = EIO;
1555
    return -1;
×
1556
  }
×
1557

1558
  mark_point();
1559
  pr_netio_close(nstrm);
3✔
1560
  return 0;
3✔
1561
}
3✔
1562

1563
static int netio_write_to_stream(int strm_type, int use_async) {
1564
  int fd, res;
6✔
1565
  char *buf;
6✔
1566
  size_t buflen;
6✔
1567
  pr_netio_stream_t *nstrm;
6✔
1568

6✔
1569
  res = pr_netio_write(NULL, NULL, 0);
1570
  if (res == 0) {
6✔
1571
    errno = EINVAL;
6✔
1572
    return -1;
×
1573
  }
×
1574

1575
  fd = devnull_fd();
1576
  if (fd < 0) {
6✔
1577
    return -1;
6✔
1578
  }
1579

1580
  nstrm = pr_netio_open(p, strm_type, fd, PR_NETIO_IO_WR);
1581
  if (nstrm == NULL) {
6✔
1582
    int xerrno = errno;
6✔
1583

×
1584
    pr_trace_msg("netio", 1, "error opening custom netio stream: %s",
1585
      strerror(xerrno));
×
1586
    errno = xerrno;
1587
    return -1;
×
1588
  }
×
1589

1590
  res = pr_netio_write(nstrm, NULL, 0);
1591
  if (res == 0) {
6✔
1592
    pr_netio_close(nstrm);
6✔
1593
    errno = EINVAL;
×
1594
    return -1;
×
1595
  }
×
1596

1597
  buf = "Hello, World!\n";
1598
  buflen = strlen(buf);
6✔
1599

6✔
1600
  res = pr_netio_write(nstrm, buf, 0);
1601
  if (res == 0) {
6✔
1602
    pr_netio_close(nstrm);
6✔
1603
    errno = EINVAL;
×
1604
    return -1;
×
1605
  }
×
1606

1607
  if (use_async) {
1608
    res = pr_netio_write_async(nstrm, buf, buflen);
6✔
1609

3✔
1610
  } else {
1611
    res = pr_netio_write(nstrm, buf, buflen);
1612
  }
3✔
1613

1614
  if ((size_t) res != buflen) {
1615
    pr_trace_msg("netio", 1, "wrote buffer (%lu bytes), got %d",
6✔
1616
      (unsigned long) buflen, res);
×
1617
    pr_netio_close(nstrm);
1618

×
1619
    if (res < 0) {
1620
      return -1;
×
1621
    }
1622

1623
    errno = EIO;
1624
    return -1;
×
1625
  }
×
1626

1627
  netio_write_epipe = TRUE;
1628
  res = pr_netio_write(nstrm, buf, buflen);
6✔
1629
  if (res >= 0) {
6✔
1630
    pr_trace_msg("netio", 1, "Expected EPIPE (-1), got %d", res);
6✔
1631
    pr_netio_close(nstrm);
×
1632
    errno = EIO;
×
1633
    return -1;
×
1634
  }
×
1635

1636
  mark_point();
1637
  pr_netio_close(nstrm);
6✔
1638
  return 0;
6✔
1639
}
6✔
1640

1641
START_TEST (netio_read_test) {
1642
  int res;
1✔
1643
  pr_netio_t *netio, *netio2;
1✔
1644
  pr_netio_stream_t *nstrm;
1✔
1645
  char *buf;
1✔
1646

1✔
1647
  mark_point();
1648
  res = pr_netio_read(NULL, NULL, 0, 0);
1✔
1649
  ck_assert_msg(res < 0, "Failed to handle null nstrm");
1✔
1650
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1651
    strerror(errno), errno);
1✔
1652

1653
  mark_point();
1654
  nstrm = pcalloc(p, sizeof(pr_netio_stream_t));
1✔
1655
  res = pr_netio_read(nstrm, NULL, 0, 0);
1✔
1656
  ck_assert_msg(res < 0, "Failed to handle null buf");
1✔
1657
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1658
    strerror(errno), errno);
1✔
1659

1660
  mark_point();
1661
  buf = "foo";
1✔
1662
  res = pr_netio_read(nstrm, buf, 0, 0);
1✔
1663
  ck_assert_msg(res < 0, "Failed to handle zero buflen");
1✔
1664
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1665
    strerror(errno), errno);
1✔
1666

1667
  mark_point();
1668
  nstrm->strm_fd = -2;
1✔
1669
  res = pr_netio_read(nstrm, buf, 3, 0);
1✔
1670
  ck_assert_msg(res < 0, "Failed to handle bad nstrm fd");
1✔
1671
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
1✔
1672
    strerror(errno), errno);
1✔
1673

1674
  netio = pr_alloc_netio2(p, NULL, "testsuite");
1675
  netio->poll = netio_poll_cb;
1✔
1676
  netio->read = netio_read_cb;
1✔
1677

1✔
1678
  /* Write to control stream */
1679
  res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
1680
  ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
1✔
1681
    strerror(errno));
1✔
1682

1683
  netio2 = pr_get_netio(PR_NETIO_STRM_CTRL);
1684
  ck_assert_msg(netio2 != NULL, "Failed to get custom ctrl NetIO: %s",
1✔
1685
    strerror(errno));
1✔
1686
  ck_assert_msg(netio2 == netio, "Expected custom ctrl NetIO %p, got %p",
1687
    netio, netio2);
1✔
1688

1689
  res = netio_read_from_stream(PR_NETIO_STRM_CTRL);
1690
  ck_assert_msg(res == 0, "Failed to read from custom ctrl NetIO: %s",
1✔
1691
    strerror(errno));
1✔
1692

1693
  mark_point();
1694
  pr_unregister_netio(PR_NETIO_STRM_CTRL);
1✔
1695

1✔
1696
  /* Read from data stream */
1697
  res = pr_register_netio(netio, PR_NETIO_STRM_DATA);
1698
  ck_assert_msg(res == 0, "Failed to register custom data NetIO: %s",
1✔
1699
    strerror(errno));
1✔
1700

1701
  netio2 = pr_get_netio(PR_NETIO_STRM_DATA);
1702
  ck_assert_msg(netio2 != NULL, "Failed to get custom data NetIO: %s",
1✔
1703
    strerror(errno));
1✔
1704
  ck_assert_msg(netio2 == netio, "Expected custom data NetIO %p, got %p",
1705
    netio, netio2);
1✔
1706

1707
  res = netio_read_from_stream(PR_NETIO_STRM_DATA);
1708
  ck_assert_msg(res == 0, "Failed to read from custom data NetIO: %s",
1✔
1709
    strerror(errno));
1✔
1710

1711
  mark_point();
1712
  pr_unregister_netio(PR_NETIO_STRM_DATA);
1✔
1713

1✔
1714
  /* Read from other stream */
1715
  res = pr_register_netio(netio, PR_NETIO_STRM_OTHR);
1716
  ck_assert_msg(res == 0, "Failed to register custom other NetIO: %s",
1✔
1717
    strerror(errno));
1✔
1718

1719
  netio2 = pr_get_netio(PR_NETIO_STRM_OTHR);
1720
  ck_assert_msg(netio2 != NULL, "Failed to get custom othr NetIO: %s",
1✔
1721
    strerror(errno));
1✔
1722
  ck_assert_msg(netio2 == netio, "Expected custom othr NetIO %p, got %p",
1723
    netio, netio2);
1✔
1724

1725
  res = netio_read_from_stream(PR_NETIO_STRM_OTHR);
1726
  ck_assert_msg(res == 0, "Failed to read from custom other NetIO: %s",
1✔
1727
    strerror(errno));
1✔
1728

1729
  mark_point();
1730
  pr_unregister_netio(PR_NETIO_STRM_OTHR);
1✔
1731
}
1✔
1732
END_TEST
1✔
1733

1734
START_TEST (netio_gets_test) {
1735
  int fd = 2, res;
1✔
1736
  char *buf, *expected, *text;
1✔
1737
  size_t buflen;
1✔
1738
  pr_netio_t *netio;
1✔
1739
  pr_netio_stream_t *nstrm;
1✔
1740

1✔
1741
  text = pr_netio_gets(NULL, 0, NULL);
1742
  ck_assert_msg(text == NULL, "Failed to handle null arguments");
1✔
1743
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1744
    strerror(errno), errno);
1✔
1745

1746
  netio = pr_alloc_netio2(p, NULL, "testsuite");
1747
  netio->poll = netio_poll_cb;
1✔
1748
  netio->read = netio_read_cb;
1✔
1749

1✔
1750
  res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
1751
  ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
1✔
1752
    strerror(errno));
1✔
1753

1754
  nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
1755
  ck_assert_msg(nstrm != NULL, "Failed to open stream: %s", strerror(errno));
1✔
1756

1✔
1757
  text = pr_netio_gets(NULL, 0, nstrm);
1758
  ck_assert_msg(text == NULL, "Failed to handle null buffer");
1✔
1759
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1760
    strerror(errno), errno);
1✔
1761

1762
  buflen = 1024;
1763
  buf = pcalloc(p, buflen);
1✔
1764

1✔
1765
  text = pr_netio_gets(buf, 0, nstrm);
1766
  ck_assert_msg(text == NULL, "Failed to handle zero buffer length");
1✔
1767
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1768
    strerror(errno), errno);
1✔
1769

1770
  expected = "Hello, World!\r\n";
1771
  text = pr_netio_gets(buf, buflen-1, nstrm);
1✔
1772
  ck_assert_msg(text != NULL, "Failed to get text: %s", strerror(errno));
1✔
1773
  ck_assert_msg(strcmp(text, expected) == 0, "Expected '%s', got '%s'",
1✔
1774
    expected, text);
1✔
1775

1776
  mark_point();
1777
  pr_unregister_netio(PR_NETIO_STRM_CTRL);
1✔
1778
}
1✔
1779
END_TEST
1✔
1780

1781
START_TEST (netio_write_test) {
1782
  int res;
1✔
1783
  pr_netio_t *netio, *netio2;
1✔
1784
  pr_netio_stream_t *nstrm;
1✔
1785
  char *buf;
1✔
1786

1✔
1787
  mark_point();
1788
  res = pr_netio_write(NULL, NULL, 0);
1✔
1789
  ck_assert_msg(res < 0, "Failed to handle null nstrm");
1✔
1790
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1791
    strerror(errno), errno);
1✔
1792

1793
  mark_point();
1794
  nstrm = pcalloc(p, sizeof(pr_netio_stream_t));
1✔
1795
  res = pr_netio_write(nstrm, NULL, 0);
1✔
1796
  ck_assert_msg(res < 0, "Failed to handle null buf");
1✔
1797
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1798
    strerror(errno), errno);
1✔
1799

1800
  mark_point();
1801
  buf = "foo";
1✔
1802
  res = pr_netio_write(nstrm, buf, 0);
1✔
1803
  ck_assert_msg(res < 0, "Failed to handle zero buflen");
1✔
1804
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1805
    strerror(errno), errno);
1✔
1806

1807
  mark_point();
1808
  nstrm->strm_fd = -34;
1✔
1809
  res = pr_netio_write(nstrm, buf, 3);
1✔
1810
  ck_assert_msg(res < 0, "Failed to handle bad nstrm fd");
1✔
1811
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
1✔
1812
    strerror(errno), errno);
1✔
1813

1814
  netio = pr_alloc_netio2(p, NULL, "testsuite");
1815
  netio->poll = netio_poll_cb;
1✔
1816
  netio->write = netio_write_cb;
1✔
1817

1✔
1818
  /* Write to control stream */
1819
  res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
1820
  ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
1✔
1821
    strerror(errno));
1✔
1822

1823
  netio2 = pr_get_netio(PR_NETIO_STRM_CTRL);
1824
  ck_assert_msg(netio2 != NULL, "Failed to get custom ctrl NetIO: %s",
1✔
1825
    strerror(errno));
1✔
1826
  ck_assert_msg(netio2 == netio, "Expected custom ctrl NetIO %p, got %p",
1827
    netio, netio2);
1✔
1828

1829
  res = netio_write_to_stream(PR_NETIO_STRM_CTRL, FALSE);
1830
  ck_assert_msg(res == 0, "Failed to write to custom ctrl NetIO: %s",
1✔
1831
    strerror(errno));
1✔
1832

1833
  mark_point();
1834
  pr_unregister_netio(PR_NETIO_STRM_CTRL);
1✔
1835

1✔
1836
  /* Write to data stream */
1837
  res = pr_register_netio(netio, PR_NETIO_STRM_DATA);
1838
  ck_assert_msg(res == 0, "Failed to register custom data NetIO: %s",
1✔
1839
    strerror(errno));
1✔
1840

1841
  netio2 = pr_get_netio(PR_NETIO_STRM_DATA);
1842
  ck_assert_msg(netio2 != NULL, "Failed to get custom data NetIO: %s",
1✔
1843
    strerror(errno));
1✔
1844
  ck_assert_msg(netio2 == netio, "Expected custom data NetIO %p, got %p",
1845
    netio, netio2);
1✔
1846

1847
  res = netio_write_to_stream(PR_NETIO_STRM_DATA, FALSE);
1848
  ck_assert_msg(res == 0, "Failed to write to custom data NetIO: %s",
1✔
1849
    strerror(errno));
1✔
1850

1851
  mark_point();
1852
  pr_unregister_netio(PR_NETIO_STRM_DATA);
1✔
1853

1✔
1854
  /* Write to other stream */
1855
  res = pr_register_netio(netio, PR_NETIO_STRM_OTHR);
1856
  ck_assert_msg(res == 0, "Failed to register custom other NetIO: %s",
1✔
1857
    strerror(errno));
1✔
1858

1859
  netio2 = pr_get_netio(PR_NETIO_STRM_OTHR);
1860
  ck_assert_msg(netio2 != NULL, "Failed to get custom othr NetIO: %s",
1✔
1861
    strerror(errno));
1✔
1862
  ck_assert_msg(netio2 == netio, "Expected custom othr NetIO %p, got %p",
1863
    netio, netio2);
1✔
1864

1865
  res = netio_write_to_stream(PR_NETIO_STRM_OTHR, FALSE);
1866
  ck_assert_msg(res == 0, "Failed to write to custom other NetIO: %s",
1✔
1867
    strerror(errno));
1✔
1868

1869
  mark_point();
1870
  pr_unregister_netio(PR_NETIO_STRM_OTHR);
1✔
1871
}
1✔
1872
END_TEST
1✔
1873

1874
START_TEST (netio_write_async_test) {
1875
  int res;
1✔
1876
  pr_netio_t *netio;
1✔
1877
  pr_netio_stream_t *nstrm;
1✔
1878

1✔
1879
  mark_point();
1880
  res = pr_netio_write_async(NULL, NULL, 0);
1✔
1881
  ck_assert_msg(res < 0, "Failed to handle null nstrm");
1✔
1882
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1883
    strerror(errno), errno);
1✔
1884

1885
  mark_point();
1886
  nstrm = pcalloc(p, sizeof(pr_netio_stream_t));
1✔
1887
  nstrm->strm_fd = -1;
1✔
1888
  res = pr_netio_write_async(nstrm, NULL, 0);
1✔
1889
  ck_assert_msg(res < 0, "Failed to handle bad nstrm fd");
1✔
1890
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
1✔
1891
    strerror(errno), errno);
1✔
1892

1893
  netio = pr_alloc_netio2(p, NULL, "testsuite");
1894
  netio->poll = netio_poll_cb;
1✔
1895
  netio->write = netio_write_cb;
1✔
1896

1✔
1897
  /* ctrl */
1898
  res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
1899
  ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
1✔
1900
    strerror(errno));
1✔
1901

1902
  mark_point();
1903
  res = netio_write_to_stream(PR_NETIO_STRM_CTRL, TRUE);
1✔
1904
  ck_assert_msg(res == 0, "Failed to write to custom ctrl NetIO: %s",
1✔
1905
    strerror(errno));
1✔
1906

1907
  mark_point();
1908
  pr_unregister_netio(PR_NETIO_STRM_CTRL);
1✔
1909

1✔
1910
  /* data */
1911
  res = pr_register_netio(netio, PR_NETIO_STRM_DATA);
1912
  ck_assert_msg(res == 0, "Failed to register custom data NetIO: %s",
1✔
1913
    strerror(errno));
1✔
1914

1915
  mark_point();
1916
  res = netio_write_to_stream(PR_NETIO_STRM_DATA, TRUE);
1✔
1917
  ck_assert_msg(res == 0, "Failed to write to custom data NetIO: %s",
1✔
1918
    strerror(errno));
1✔
1919

1920
  mark_point();
1921
  pr_unregister_netio(PR_NETIO_STRM_DATA);
1✔
1922

1✔
1923
  /* othr */
1924
  res = pr_register_netio(netio, PR_NETIO_STRM_OTHR);
1925
  ck_assert_msg(res == 0, "Failed to register custom othr NetIO: %s",
1✔
1926
    strerror(errno));
1✔
1927

1928
  mark_point();
1929
  res = netio_write_to_stream(PR_NETIO_STRM_OTHR, TRUE);
1✔
1930
  ck_assert_msg(res == 0, "Failed to write to custom othr NetIO: %s",
1✔
1931
    strerror(errno));
1✔
1932

1933
  mark_point();
1934
  pr_unregister_netio(PR_NETIO_STRM_OTHR);
1✔
1935
}
1✔
1936
END_TEST
1✔
1937

1938
static int netio_print_to_stream(int strm_type, int use_async) {
1939
  int fd = 2, res;
2✔
1940
  char *buf;
2✔
1941
  size_t buflen;
2✔
1942
  pr_netio_stream_t *nstrm;
2✔
1943

2✔
1944
  nstrm = pr_netio_open(p, strm_type, fd, PR_NETIO_IO_WR);
1945
  if (nstrm == NULL) {
2✔
1946
    int xerrno = errno;
2✔
1947

×
1948
    pr_trace_msg("netio", 1, "error opening custom netio stream: %s",
1949
      strerror(xerrno));
×
1950
    errno = xerrno;
1951
    return -1;
×
1952
  }
×
1953

1954
  buf = "Hello, World!\n";
1955
  buflen = strlen(buf);
2✔
1956

2✔
1957
  if (use_async) {
1958
    res = pr_netio_printf_async(nstrm, "%s", buf);
2✔
1959

1✔
1960
  } else {
1961
    res = pr_netio_printf(nstrm, "%s", buf);
1962
  }
1✔
1963

1964
  if ((size_t) res != buflen) {
1965
    pr_trace_msg("netio", 1, "printed buffer (%lu bytes), got %d",
2✔
1966
      (unsigned long) buflen, res);
×
1967
    pr_netio_close(nstrm);
1968

×
1969
    if (res < 0) {
1970
      return -1;
×
1971
    }
1972

1973
    errno = EIO;
1974
    return -1;
×
1975
  }
×
1976

1977
  mark_point();
1978
  pr_netio_close(nstrm);
2✔
1979
  return 0;
2✔
1980
}
2✔
1981

1982
START_TEST (netio_printf_test) {
1983
  int res;
1✔
1984
  pr_netio_t *netio;
1✔
1985

1✔
1986
  netio = pr_alloc_netio2(p, NULL, "testsuite");
1987
  netio->poll = netio_poll_cb;
1✔
1988
  netio->write = netio_write_cb;
1✔
1989
  netio->close = netio_close_cb;
1✔
1990

1✔
1991
  mark_point();
1992
  res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
1✔
1993
  ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
1✔
1994
    strerror(errno));
1✔
1995

1996
  mark_point();
1997
  res = netio_print_to_stream(PR_NETIO_STRM_CTRL, FALSE);
1✔
1998
  ck_assert_msg(res == 0, "Failed to print to custom ctrl NetIO: %s",
1✔
1999
    strerror(errno));
1✔
2000

2001
  mark_point();
2002
  pr_unregister_netio(PR_NETIO_STRM_CTRL);
1✔
2003
}
1✔
2004
END_TEST
1✔
2005

2006
START_TEST (netio_printf_async_test) {
2007
  int res;
1✔
2008
  pr_netio_t *netio;
1✔
2009

1✔
2010
  netio = pr_alloc_netio2(p, NULL, "testsuite");
2011
  netio->poll = netio_poll_cb;
1✔
2012
  netio->write = netio_write_cb;
1✔
2013
  netio->close = netio_close_cb;
1✔
2014

1✔
2015
  mark_point();
2016
  res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
1✔
2017
  ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
1✔
2018
    strerror(errno));
1✔
2019

2020
  mark_point();
2021
  res = netio_print_to_stream(PR_NETIO_STRM_CTRL, TRUE);
1✔
2022
  ck_assert_msg(res == 0, "Failed to print to custom ctrl NetIO: %s",
1✔
2023
    strerror(errno));
1✔
2024

2025
  mark_point();
2026
  pr_unregister_netio(PR_NETIO_STRM_CTRL);
1✔
2027
}
1✔
2028
END_TEST
1✔
2029

2030
START_TEST (netio_abort_test) {
2031
  pr_netio_stream_t *nstrm;
1✔
2032
  int fd = -1;
1✔
2033

1✔
2034
  mark_point();
2035
  pr_netio_abort(NULL);
1✔
2036

1✔
2037
  /* open/abort/close CTRL stream */
2038
  nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
2039
  ck_assert_msg(nstrm != NULL, "Failed to open ctrl stream on fd %d: %s", fd,
1✔
2040
    strerror(errno));
1✔
2041

2042
  pr_netio_abort(nstrm);
2043
  ck_assert_msg(nstrm->strm_flags & PR_NETIO_SESS_ABORT,
1✔
2044
    "Failed to set PR_NETIO_SESS_ABORT flags on ctrl stream");
1✔
2045

2046
  pr_netio_close(nstrm);
2047

1✔
2048
  /* open/abort/close DATA stream */
2049
  nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_WR);
2050
  ck_assert_msg(nstrm != NULL, "Failed to open data stream on fd %d: %s", fd,
1✔
2051
    strerror(errno));
1✔
2052

2053
  pr_netio_abort(nstrm);
2054
  ck_assert_msg(nstrm->strm_flags & PR_NETIO_SESS_ABORT,
1✔
2055
    "Failed to set PR_NETIO_SESS_ABORT flags on data stream");
1✔
2056

2057
  pr_netio_close(nstrm);
2058

1✔
2059
  /* open/abort/close OTHR stream */
2060
  nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_WR);
2061
  ck_assert_msg(nstrm != NULL, "Failed to open othr stream on fd %d: %s", fd,
1✔
2062
    strerror(errno));
1✔
2063

2064
  pr_netio_abort(nstrm);
2065
  ck_assert_msg(nstrm->strm_flags & PR_NETIO_SESS_ABORT,
1✔
2066
    "Failed to set PR_NETIO_SESS_ABORT flags on othr stream");
1✔
2067

2068
  pr_netio_close(nstrm);
2069
}
1✔
2070
END_TEST
1✔
2071

2072
START_TEST (netio_lingering_abort_test) {
2073
  pr_netio_t *netio;
1✔
2074
  pr_netio_stream_t *nstrm;
1✔
2075
  int fd = 0, res;
1✔
2076
  long linger = 0L;
1✔
2077

1✔
2078
  mark_point();
2079
  res = pr_netio_lingering_abort(NULL, linger);
1✔
2080
  ck_assert_msg(res < 0, "Failed to handle null nstrm");
1✔
2081
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2082
    strerror(errno), errno);
1✔
2083

2084
  mark_point();
2085
  nstrm = pcalloc(p, sizeof(pr_netio_stream_t));
1✔
2086
  nstrm->strm_type = 0;
1✔
2087
  res = pr_netio_lingering_abort(nstrm, linger);
1✔
2088
  ck_assert_msg(res < 0, "Failed to handle invalid nstrm type");
1✔
2089
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2090
    strerror(errno), errno);
1✔
2091

2092
  netio = pr_alloc_netio2(p, NULL, "testsuite");
2093
  netio->close = netio_close_cb;
1✔
2094

1✔
2095
  /* open/abort/close CTRL stream */
2096
  res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
2097
  ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
1✔
2098
    strerror(errno));
1✔
2099

2100
  nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
2101
  ck_assert_msg(nstrm != NULL, "Failed to open ctrl stream on fd %d: %s", fd,
1✔
2102
    strerror(errno));
1✔
2103

2104
  res = pr_netio_lingering_abort(nstrm, linger);
2105
  ck_assert_msg(res == 0, "Failed to set lingering abort on ctrl stream: %s",
1✔
2106
    strerror(errno));
1✔
2107

2108
  ck_assert_msg(nstrm->strm_flags & PR_NETIO_SESS_ABORT,
2109
    "Failed to set PR_NETIO_SESS_ABORT flags on ctrl stream");
1✔
2110

2111
  pr_netio_close(nstrm);
2112
  pr_unregister_netio(PR_NETIO_STRM_CTRL);
1✔
2113

1✔
2114
  /* open/abort/close DATA stream */
2115
  res = pr_register_netio(netio, PR_NETIO_STRM_DATA);
2116
  ck_assert_msg(res == 0, "Failed to register custom data NetIO: %s",
1✔
2117
    strerror(errno));
1✔
2118

2119
  nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_RD);
2120
  ck_assert_msg(nstrm != NULL, "Failed to open data stream on fd %d: %s", fd,
1✔
2121
    strerror(errno));
1✔
2122

2123
  res = pr_netio_lingering_abort(nstrm, linger);
2124
  ck_assert_msg(res == 0, "Failed to set lingering abort on data stream: %s",
1✔
2125
    strerror(errno));
1✔
2126

2127
  ck_assert_msg(nstrm->strm_flags & PR_NETIO_SESS_ABORT,
2128
    "Failed to set PR_NETIO_SESS_ABORT flags on data stream");
1✔
2129

2130
  pr_netio_close(nstrm);
2131
  pr_unregister_netio(PR_NETIO_STRM_DATA);
1✔
2132

1✔
2133
  /* open/abort/close OTHR stream */
2134
  res = pr_register_netio(netio, PR_NETIO_STRM_OTHR);
2135
  ck_assert_msg(res == 0, "Failed to register custom othr NetIO: %s",
1✔
2136
    strerror(errno));
1✔
2137

2138
  nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_RD);
2139
  ck_assert_msg(nstrm != NULL, "Failed to open othr stream on fd %d: %s", fd,
1✔
2140
    strerror(errno));
1✔
2141

2142
  res = pr_netio_lingering_abort(nstrm, linger);
2143
  ck_assert_msg(res == 0, "Failed to set lingering abort on othr stream: %s",
1✔
2144
    strerror(errno));
1✔
2145

2146
  ck_assert_msg(nstrm->strm_flags & PR_NETIO_SESS_ABORT,
2147
    "Failed to set PR_NETIO_SESS_ABORT flags on othr stream");
1✔
2148

2149
  pr_netio_close(nstrm);
2150
  pr_unregister_netio(PR_NETIO_STRM_OTHR);
1✔
2151
}
1✔
2152
END_TEST
1✔
2153

2154
START_TEST (netio_poll_test) {
2155
  int res;
1✔
2156
  pr_netio_stream_t *nstrm;
1✔
2157

1✔
2158
  mark_point();
2159
  res = pr_netio_poll(NULL);
1✔
2160
  ck_assert_msg(res < 0, "Failed to handle null nstrm");
1✔
2161
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2162
    strerror(errno), errno);
1✔
2163

2164
  mark_point();
2165
  nstrm = pcalloc(p, sizeof(pr_netio_stream_t));
1✔
2166
  nstrm->strm_fd = -3;
1✔
2167
  res = pr_netio_poll(nstrm);
1✔
2168
  ck_assert_msg(res < 0, "Failed to handle bad nstrm fd");
1✔
2169
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
1✔
2170
    strerror(errno), errno);
1✔
2171

2172
  mark_point();
2173
  nstrm->strm_fd = fileno(stderr);
1✔
2174
  nstrm->strm_flags |= PR_NETIO_SESS_ABORT;
1✔
2175
  res = pr_netio_poll(nstrm);
1✔
2176
  ck_assert_msg(res == 1, "Failed to handle SESS_ABORT flag");
1✔
2177

1✔
2178
  mark_point();
2179
  nstrm->strm_flags |= PR_NETIO_SESS_INTR;
1✔
2180
  res = pr_netio_poll(nstrm);
1✔
2181
  ck_assert_msg(res < 0, "Failed to handle SESS_INTR flag");
1✔
2182
  ck_assert_msg(errno == EOF, "Expected EOF (%d), got %s (%d)", EOF,
1✔
2183
    strerror(errno), errno);
1✔
2184

2185
  mark_point();
2186
  nstrm->strm_flags &= ~PR_NETIO_SESS_INTR;
1✔
2187
  nstrm->strm_type = PR_NETIO_STRM_CTRL;
1✔
2188
  res = pr_netio_poll(nstrm);
1✔
2189
  ck_assert_msg(res == 0, "Failed to handle ctrl strm: %s", strerror(errno));
1✔
2190

1✔
2191
  mark_point();
2192
  nstrm->strm_type = PR_NETIO_STRM_DATA;
1✔
2193
  res = pr_netio_poll(nstrm);
1✔
2194
  ck_assert_msg(res == 0, "Failed to handle data strm: %s", strerror(errno));
1✔
2195

1✔
2196
  mark_point();
2197
  nstrm->strm_type = PR_NETIO_STRM_OTHR;
1✔
2198
  res = pr_netio_poll(nstrm);
1✔
2199
  ck_assert_msg(res == 0, "Failed to handle othr strm: %s", strerror(errno));
1✔
2200
}
1✔
2201
END_TEST
1✔
2202

2203
START_TEST (netio_poll_interval_test) {
2204
  pr_netio_stream_t *nstrm;
1✔
2205
  int fd = -1;
1✔
2206
  unsigned int interval = 3;
1✔
2207

1✔
2208
  mark_point();
2209
  pr_netio_set_poll_interval(NULL, 0);
1✔
2210

1✔
2211
  nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
2212
  ck_assert_msg(nstrm != NULL, "Failed to open ctrl stream on fd %d: %s", fd,
1✔
2213
    strerror(errno));
1✔
2214

2215
  pr_netio_set_poll_interval(nstrm, interval);
2216
  ck_assert_msg(nstrm->strm_interval == interval,
1✔
2217
    "Expected stream interval %u, got %u", interval, nstrm->strm_interval);
1✔
2218
  ck_assert_msg(nstrm->strm_flags & PR_NETIO_SESS_INTR,
2219
    "Failed to set PR_NETIO_SESS_INTR stream flag");
1✔
2220

2221
  mark_point();
2222
  pr_netio_reset_poll_interval(NULL);
1✔
2223

1✔
2224
  pr_netio_reset_poll_interval(nstrm);
2225
  ck_assert_msg(!(nstrm->strm_flags & PR_NETIO_SESS_INTR),
1✔
2226
    "Failed to clear PR_NETIO_SESS_INTR stream flag");
1✔
2227

2228
  (void) pr_netio_close(nstrm);
2229
}
1✔
2230
END_TEST
1✔
2231

2232
static int netio_shutdown_cb(pr_netio_stream_t *nstrm, int how) {
2233
  return 0;
3✔
2234
}
3✔
2235

2236
START_TEST (netio_shutdown_test) {
2237
  pr_netio_t *netio;
1✔
2238
  pr_netio_stream_t *nstrm;
1✔
2239
  int fd = 0, how = SHUT_RD, res;
1✔
2240

1✔
2241
  mark_point();
2242
  res = pr_netio_shutdown(NULL, how);
1✔
2243
  ck_assert_msg(res < 0, "Failed to handle null nstrm");
1✔
2244
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2245
    strerror(errno), errno);
1✔
2246

2247
  mark_point();
2248
  nstrm = pcalloc(p, sizeof(pr_netio_stream_t));
1✔
2249
  res = pr_netio_shutdown(nstrm, how);
1✔
2250
  ck_assert_msg(res < 0, "Failed to handle invalid nstrm type");
1✔
2251
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2252
    strerror(errno), errno);
1✔
2253

2254
  netio = pr_alloc_netio2(p, NULL, "testsuite");
2255
  netio->close = netio_close_cb;
1✔
2256
  netio->shutdown = netio_shutdown_cb;
1✔
2257

1✔
2258
  /* open/shutdown/close CTRL stream */
2259
  res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
2260
  ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
1✔
2261
    strerror(errno));
1✔
2262

2263
  nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
2264
  ck_assert_msg(nstrm != NULL, "Failed to open ctrl stream on fd %d: %s", fd,
1✔
2265
    strerror(errno));
1✔
2266

2267
  res = pr_netio_shutdown(nstrm, how);
2268
  ck_assert_msg(res == 0, "Failed to shutdown ctrl stream: %s", strerror(errno));
1✔
2269

1✔
2270
  pr_netio_close(nstrm);
2271
  pr_unregister_netio(PR_NETIO_STRM_CTRL);
1✔
2272

1✔
2273
  /* open/shutdown/close DATA stream */
2274
  res = pr_register_netio(netio, PR_NETIO_STRM_DATA);
2275
  ck_assert_msg(res == 0, "Failed to register custom data NetIO: %s",
1✔
2276
    strerror(errno));
1✔
2277

2278
  nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_RD);
2279
  ck_assert_msg(nstrm != NULL, "Failed to open data stream on fd %d: %s", fd,
1✔
2280
    strerror(errno));
1✔
2281

2282
  res = pr_netio_shutdown(nstrm, how);
2283
  ck_assert_msg(res == 0, "Failed to shutdown ctrl stream: %s", strerror(errno));
1✔
2284

1✔
2285
  pr_netio_close(nstrm);
2286
  pr_unregister_netio(PR_NETIO_STRM_DATA);
1✔
2287

1✔
2288
  /* open/shutdown/close OTHR stream */
2289
  res = pr_register_netio(netio, PR_NETIO_STRM_OTHR);
2290
  ck_assert_msg(res == 0, "Failed to register custom othr NetIO: %s",
1✔
2291
    strerror(errno));
1✔
2292

2293
  nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_RD);
2294
  ck_assert_msg(nstrm != NULL, "Failed to open othr stream on fd %d: %s", fd,
1✔
2295
    strerror(errno));
1✔
2296

2297
  res = pr_netio_shutdown(nstrm, how);
2298
  ck_assert_msg(res == 0, "Failed to shutdown ctrl stream: %s", strerror(errno));
1✔
2299

1✔
2300
  pr_netio_close(nstrm);
2301
  pr_unregister_netio(PR_NETIO_STRM_OTHR);
1✔
2302
}
1✔
2303
END_TEST
1✔
2304

2305
START_TEST (netio_register_test) {
2306
  int res;
1✔
2307
  pr_netio_t *netio;
1✔
2308
  void *cb;
1✔
2309

1✔
2310
  netio = pr_alloc_netio(p);
2311

1✔
2312
  /* abort */
2313
  mark_point();
2314
  cb = netio->abort;
1✔
2315
  netio->abort = NULL;
1✔
2316
  res = pr_register_netio(netio, 0);
1✔
2317
  ck_assert_msg(res < 0, "Failed to handle null abort cb");
1✔
2318
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2319
    strerror(errno), errno);
1✔
2320
  netio->abort = cb;
2321

1✔
2322
  /* close */
2323
  mark_point();
2324
  cb = netio->close;
1✔
2325
  netio->close = NULL;
1✔
2326
  res = pr_register_netio(netio, 0);
1✔
2327
  ck_assert_msg(res < 0, "Failed to handle null close cb");
1✔
2328
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2329
    strerror(errno), errno);
1✔
2330
  netio->close = cb;
2331

1✔
2332
  /* open */
2333
  mark_point();
2334
  cb = netio->open;
1✔
2335
  netio->open = NULL;
1✔
2336
  res = pr_register_netio(netio, 0);
1✔
2337
  ck_assert_msg(res < 0, "Failed to handle null open cb");
1✔
2338
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2339
    strerror(errno), errno);
1✔
2340
  netio->open = cb;
2341

1✔
2342
  /* poll */
2343
  mark_point();
2344
  cb = netio->poll;
1✔
2345
  netio->poll = NULL;
1✔
2346
  res = pr_register_netio(netio, 0);
1✔
2347
  ck_assert_msg(res < 0, "Failed to handle null poll cb");
1✔
2348
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2349
    strerror(errno), errno);
1✔
2350
  netio->poll = cb;
2351

1✔
2352
  /* postopen */
2353
  mark_point();
2354
  cb = netio->postopen;
1✔
2355
  netio->postopen = NULL;
1✔
2356
  res = pr_register_netio(netio, 0);
1✔
2357
  ck_assert_msg(res < 0, "Failed to handle null postopen cb");
1✔
2358
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2359
    strerror(errno), errno);
1✔
2360
  netio->postopen = cb;
2361

1✔
2362
  /* read */
2363
  mark_point();
2364
  cb = netio->read;
1✔
2365
  netio->read = NULL;
1✔
2366
  res = pr_register_netio(netio, 0);
1✔
2367
  ck_assert_msg(res < 0, "Failed to handle null read cb");
1✔
2368
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2369
    strerror(errno), errno);
1✔
2370
  netio->read = cb;
2371

1✔
2372
  /* reopen */
2373
  mark_point();
2374
  cb = netio->reopen;
1✔
2375
  netio->reopen = NULL;
1✔
2376
  res = pr_register_netio(netio, 0);
1✔
2377
  ck_assert_msg(res < 0, "Failed to handle null reopen cb");
1✔
2378
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2379
    strerror(errno), errno);
1✔
2380
  netio->reopen = cb;
2381

1✔
2382
  /* shutdown */
2383
  mark_point();
2384
  cb = netio->shutdown;
1✔
2385
  netio->shutdown = NULL;
1✔
2386
  res = pr_register_netio(netio, 0);
1✔
2387
  ck_assert_msg(res < 0, "Failed to handle null shutdown cb");
1✔
2388
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2389
    strerror(errno), errno);
1✔
2390
  netio->shutdown = cb;
2391

1✔
2392
  /* write */
2393
  mark_point();
2394
  cb = netio->write;
1✔
2395
  netio->write = NULL;
1✔
2396
  res = pr_register_netio(netio, 0);
1✔
2397
  ck_assert_msg(res < 0, "Failed to handle null write cb");
1✔
2398
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2399
    strerror(errno), errno);
1✔
2400
  netio->write = cb;
2401
}
1✔
2402
END_TEST
1✔
2403

2404
START_TEST (netio_unregister_test) {
2405
  int res;
1✔
2406

1✔
2407
  mark_point();
2408
  res = pr_unregister_netio(0);
1✔
2409
  ck_assert_msg(res < 0, "Failed to handle invalid types");
1✔
2410
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2411
    strerror(errno), errno);
1✔
2412

2413
  mark_point();
2414
  res = pr_unregister_netio(10000);
1✔
2415
  ck_assert_msg(res == 0, "Failed to handle invalid types");
1✔
2416
}
1✔
2417
END_TEST
1✔
2418

2419
START_TEST (netio_get_test) {
2420
  pr_netio_t *netio;
1✔
2421

1✔
2422
  mark_point();
2423
  netio = pr_get_netio(0);
1✔
2424
  ck_assert_msg(netio == NULL, "Failed to handle zero type");
1✔
2425
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2426
    strerror(errno), errno);
1✔
2427

2428
  mark_point();
2429
  netio = pr_get_netio(1000);
1✔
2430
  ck_assert_msg(netio == NULL, "Failed to handle invalid type");
1✔
2431
  ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1✔
2432
    strerror(errno), errno);
1✔
2433
}
2434
END_TEST
1✔
2435

2436
START_TEST (netio_alloc_test) {
2437
  pr_netio_t *netio;
1✔
2438

1✔
2439
  mark_point();
2440
  netio = pr_alloc_netio2(NULL, NULL, NULL);
1✔
2441
  ck_assert_msg(netio == NULL, "Failed to handle null pool");
1✔
2442
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2443
    strerror(errno), errno);
1✔
2444

2445
  mark_point();
2446
  netio = pr_alloc_netio(NULL);
1✔
2447
  ck_assert_msg(netio == NULL, "Failed to handle null pool");
1✔
2448
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
2449
    strerror(errno), errno);
1✔
2450
}
2451
END_TEST
1✔
2452

2453
Suite *tests_get_netio_suite(void) {
2454
  Suite *suite;
890✔
2455
  TCase *testcase;
890✔
2456

890✔
2457
  suite = suite_create("netio");
2458

890✔
2459
  testcase = tcase_create("base");
2460
  tcase_add_checked_fixture(testcase, set_up, tear_down);
890✔
2461

890✔
2462
  tcase_add_test(testcase, netio_open_test);
2463
  tcase_add_test(testcase, netio_postopen_test);
890✔
2464
  tcase_add_test(testcase, netio_close_test);
890✔
2465
  tcase_add_test(testcase, netio_lingering_close_test);
890✔
2466
  tcase_add_test(testcase, netio_reopen_test);
890✔
2467
  tcase_add_test(testcase, netio_buffer_alloc_test);
890✔
2468

890✔
2469
  tcase_add_test(testcase, netio_telnet_gets_args_test);
2470
  tcase_add_test(testcase, netio_telnet_gets_single_line_test);
890✔
2471
  tcase_add_test(testcase, netio_telnet_gets_multi_line_test);
890✔
2472
  tcase_add_test(testcase, netio_telnet_gets_no_newline_test);
890✔
2473
  tcase_add_test(testcase, netio_telnet_gets_telnet_will_test);
890✔
2474
  tcase_add_test(testcase, netio_telnet_gets_telnet_bare_will_test);
890✔
2475
  tcase_add_test(testcase, netio_telnet_gets_telnet_will_multi_read_test);
890✔
2476
  tcase_add_test(testcase, netio_telnet_gets_telnet_wont_test);
890✔
2477
  tcase_add_test(testcase, netio_telnet_gets_telnet_bare_wont_test);
890✔
2478
  tcase_add_test(testcase, netio_telnet_gets_telnet_do_test);
890✔
2479
  tcase_add_test(testcase, netio_telnet_gets_telnet_bare_do_test);
890✔
2480
  tcase_add_test(testcase, netio_telnet_gets_telnet_dont_test);
890✔
2481
  tcase_add_test(testcase, netio_telnet_gets_telnet_bare_dont_test);
890✔
2482
  tcase_add_test(testcase, netio_telnet_gets_telnet_ip_test);
890✔
2483
  tcase_add_test(testcase, netio_telnet_gets_telnet_bare_ip_test);
890✔
2484
  tcase_add_test(testcase, netio_telnet_gets_telnet_dm_test);
890✔
2485
  tcase_add_test(testcase, netio_telnet_gets_telnet_bare_dm_test);
890✔
2486
  tcase_add_test(testcase, netio_telnet_gets_telnet_single_iac_test);
890✔
2487
  tcase_add_test(testcase, netio_telnet_gets_bug3521_test);
890✔
2488
  tcase_add_test(testcase, netio_telnet_gets_bug3697_test);
890✔
2489
  tcase_add_test(testcase, netio_telnet_gets_eof_test);
890✔
2490
  tcase_add_test(testcase, netio_telnet_gets_random_data_test);
890✔
2491

890✔
2492
  tcase_add_test(testcase, netio_telnet_gets2_single_line_test);
2493
  tcase_add_test(testcase, netio_telnet_gets2_single_line_crnul_test);
890✔
2494
  tcase_add_test(testcase, netio_telnet_gets2_single_line_lf_test);
890✔
2495
  tcase_add_test(testcase, netio_telnet_gets2_random_data_test);
890✔
2496

890✔
2497
  tcase_add_test(testcase, netio_read_test);
2498
  tcase_add_test(testcase, netio_gets_test);
890✔
2499
  tcase_add_test(testcase, netio_write_test);
890✔
2500
  tcase_add_test(testcase, netio_write_async_test);
890✔
2501
  tcase_add_test(testcase, netio_printf_test);
890✔
2502
  tcase_add_test(testcase, netio_printf_async_test);
890✔
2503
  tcase_add_test(testcase, netio_abort_test);
890✔
2504
  tcase_add_test(testcase, netio_lingering_abort_test);
890✔
2505
  tcase_add_test(testcase, netio_poll_test);
890✔
2506
  tcase_add_test(testcase, netio_poll_interval_test);
890✔
2507
  tcase_add_test(testcase, netio_shutdown_test);
890✔
2508

890✔
2509
  tcase_add_test(testcase, netio_register_test);
2510
  tcase_add_test(testcase, netio_unregister_test);
890✔
2511
  tcase_add_test(testcase, netio_get_test);
890✔
2512
  tcase_add_test(testcase, netio_alloc_test);
890✔
2513

890✔
2514
  suite_add_tcase(suite, testcase);
2515
  return suite;
890✔
2516
}
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