Debian Codegraph @

tests/api/netio.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2008-2025 The ProFTPD Project team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, The ProFTPD Project team and other respective
20 * copyright holders give permission to link this program with OpenSSL, and
21 * distribute the resulting executable, without including the source code for
22 * OpenSSL in the source distribution.
23 */
24
25 /* NetIO API tests. */
26
27 #include "tests.h"
28
29 /* See RFC 854 for the definition of these Telnet values */
30
31 /* Telnet "Interpret As Command" indicator */
32 #define TELNET_IAC 255
33 #define TELNET_DONT 254
34 #define TELNET_DO 253
35 #define TELNET_WONT 252
36 #define TELNET_WILL 251
37 #define TELNET_IP 244
38 #define TELNET_DM 242
39
40 static pool *p = NULL;
41 static int xfer_bufsz = -1;
42
43 static int tmp_fd = -1;
44 static const char *tmp_path = NULL;
45
46 static void test_cleanup(void) {
47 (void) close(tmp_fd);
48 tmp_fd = -1;
49
50 if (tmp_path != NULL) {
51 (void) unlink(tmp_path);
52 tmp_path = NULL;
53 }
54
55 pr_unregister_netio(PR_NETIO_STRM_CTRL|PR_NETIO_STRM_DATA|PR_NETIO_STRM_OTHR);
56 }
57
58 static int open_tmpfile(void) {
59 int fd;
60
61 if (tmp_path != NULL) {
62 test_cleanup();
63 }
64
65 tmp_path = "/tmp/netio-test.dat";
66 fd = open(tmp_path, O_RDWR|O_CREAT, 0666);
67 ck_assert_msg(fd >= 0, "Failed to open '%s': %s", tmp_path, strerror(errno));
68 tmp_fd = fd;
69
70 return fd;
71 }
72
73 static void set_up(void) {
74 if (p == NULL) {
75 p = permanent_pool = make_sub_pool(NULL);
76 }
77
78 init_netio();
79 pr_random_init();
80 xfer_bufsz = pr_config_get_server_xfer_bufsz(PR_NETIO_IO_RD);
81
82 if (getenv("TEST_VERBOSE") != NULL) {
83 pr_trace_set_levels("netio", 1, 20);
84 }
85 }
86
87 static void tear_down(void) {
88 if (getenv("TEST_VERBOSE") != NULL) {
89 pr_trace_set_levels("netio", 0, 0);
90 }
91
92 test_cleanup();
93
94 if (p) {
95 destroy_pool(p);
96 p = permanent_pool = NULL;
97 }
98 }
99
100 /* Tests */
101
102 START_TEST (netio_open_test) {
103 pr_netio_stream_t *nstrm;
104 int fd = -1;
105
106 nstrm = pr_netio_open(NULL, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
107 ck_assert_msg(nstrm == NULL, "Failed to handle null pool argument");
108 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
109 strerror(errno), errno);
110
111 nstrm = pr_netio_open(p, 7777, fd, PR_NETIO_IO_RD);
112 ck_assert_msg(nstrm == NULL, "Failed to handle unknown stream type argument");
113 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
114 strerror(errno), errno);
115
116 /* open/close CTRL stream */
117 nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
118 ck_assert_msg(nstrm != NULL, "Failed to open ctrl stream on fd %d: %s", fd,
119 strerror(errno));
120 ck_assert_msg(nstrm->strm_netio != NULL,
121 "Failed to assign owning NetIO to stream");
122 pr_netio_close(nstrm);
123
124 /* open/close DATA stream */
125 nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_WR);
126 ck_assert_msg(nstrm != NULL, "Failed to open data stream on fd %d: %s", fd,
127 strerror(errno));
128 ck_assert_msg(nstrm->strm_netio != NULL,
129 "Failed to assign owning NetIO to stream");
130 pr_netio_close(nstrm);
131
132 /* open/close OTHR stream */
133 nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_WR);
134 ck_assert_msg(nstrm != NULL, "Failed to open othr stream on fd %d: %s", fd,
135 strerror(errno));
136 ck_assert_msg(nstrm->strm_netio != NULL,
137 "Failed to assign owning NetIO to stream");
138 pr_netio_close(nstrm);
139 }
140 END_TEST
141
142 START_TEST (netio_postopen_test) {
143 pr_netio_stream_t *nstrm;
144 int fd = -1, res;
145
146 res = pr_netio_postopen(NULL);
147 ck_assert_msg(res < 0, "Failed to handle null argument");
148 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
149 strerror(errno), errno);
150
151 /* open/postopen/close CTRL stream */
152 nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
153 ck_assert_msg(nstrm != NULL, "Failed to open stream on fd %d: %s", fd,
154 strerror(errno));
155
156 res = pr_netio_postopen(nstrm);
157 ck_assert_msg(res == 0, "Failed to post-open ctrl stream: %s", strerror(errno));
158 (void) pr_netio_close(nstrm);
159
160 /* open/postopen/close DATA stream */
161 nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_RD);
162 ck_assert_msg(nstrm != NULL, "Failed to open stream on fd %d: %s", fd,
163 strerror(errno));
164
165 res = pr_netio_postopen(nstrm);
166 ck_assert_msg(res == 0, "Failed to post-open data stream: %s", strerror(errno));
167 (void) pr_netio_close(nstrm);
168
169 /* open/postopen/close OTHR stream */
170 nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_RD);
171 ck_assert_msg(nstrm != NULL, "Failed to open stream on fd %d: %s", fd,
172 strerror(errno));
173
174 res = pr_netio_postopen(nstrm);
175 ck_assert_msg(res == 0, "Failed to post-open othr stream: %s", strerror(errno));
176 (void) pr_netio_close(nstrm);
177 }
178 END_TEST
179
180 START_TEST (netio_close_test) {
181 pr_netio_stream_t *nstrm;
182 int res, fd = -1;
183
184 res = pr_netio_close(NULL);
185 ck_assert_msg(res == -1, "Failed to handle null stream argument");
186 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
187 strerror(errno), errno);
188
189 /* Open/close CTRL stream */
190 nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
191 nstrm->strm_type = 7777;
192
193 res = pr_netio_close(nstrm);
194 ck_assert_msg(res == -1, "Failed to handle unknown stream type argument");
195 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)",
196 strerror(errno), errno);
197
198 nstrm->strm_type = PR_NETIO_STRM_CTRL;
199 res = pr_netio_close(nstrm);
200 ck_assert_msg(res == -1, "Failed to handle bad file descriptor");
201 ck_assert_msg(errno == EBADF, "Failed to set errno to EBADF, got %s (%d)",
202 strerror(errno), errno);
203
204 /* Open/close DATA stream */
205 nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_RD);
206 res = pr_netio_close(nstrm);
207 ck_assert_msg(res == -1, "Failed to handle bad file descriptor");
208 ck_assert_msg(errno == EBADF, "Failed to set errno to EBADF, got %s (%d)",
209 strerror(errno), errno);
210
211 /* Open/close OTHR stream */
212 nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_RD);
213 res = pr_netio_close(nstrm);
214 ck_assert_msg(res == -1, "Failed to handle bad file descriptor");
215 ck_assert_msg(errno == EBADF, "Failed to set errno to EBADF, got %s (%d)",
216 strerror(errno), errno);
217 }
218 END_TEST
219
220 START_TEST (netio_lingering_close_test) {
221 pr_netio_stream_t *nstrm;
222 int res, fd = -1;
223 long linger = 0L;
224
225 res = pr_netio_lingering_close(NULL, linger);
226 ck_assert_msg(res == -1, "Failed to handle null stream argument");
227 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
228 strerror(errno), errno);
229
230 /* Open/close CTRL stream */
231 nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
232 nstrm->strm_type = 7777;
233
234 res = pr_netio_lingering_close(nstrm, linger);
235 ck_assert_msg(res < 0, "Failed to handle unknown stream type argument");
236 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)",
237 strerror(errno), errno);
238
239 nstrm->strm_type = PR_NETIO_STRM_CTRL;
240 res = pr_netio_lingering_close(nstrm, linger);
241 ck_assert_msg(res == 0, "Failed to close stream: %s", strerror(errno));
242
243 /* Open/close DATA stream */
244 nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_RD);
245 res = pr_netio_lingering_close(nstrm, linger);
246 ck_assert_msg(res == 0, "Failed to close stream: %s", strerror(errno));
247
248 /* Open/close OTHR stream */
249 nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_RD);
250 res = pr_netio_lingering_close(nstrm, linger);
251 ck_assert_msg(res == 0, "Failed to close stream: %s", strerror(errno));
252 }
253 END_TEST
254
255 START_TEST (netio_reopen_test) {
256 pr_netio_stream_t *nstrm, *nstrm2;
257 int res, fd = -1;
258
259 nstrm2 = pr_netio_reopen(NULL, fd, PR_NETIO_IO_RD);
260 ck_assert_msg(nstrm2 == NULL, "Failed to handle null stream argument");
261 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
262 strerror(errno), errno);
263
264 /* Open/reopen/close CTRL stream */
265 nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
266 nstrm->strm_type = 7777;
267
268 nstrm2 = pr_netio_reopen(nstrm, fd, PR_NETIO_IO_RD);
269 ck_assert_msg(nstrm2 == NULL, "Failed to handle unknown stream type argument");
270 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)",
271 strerror(errno), errno);
272
273 nstrm->strm_type = PR_NETIO_STRM_CTRL;
274 nstrm2 = pr_netio_reopen(nstrm, fd, PR_NETIO_IO_RD);
275 ck_assert_msg(nstrm2 != NULL, "Failed to reopen ctrl stream: %s",
276 strerror(errno));
277
278 /* Open/reopen/close DATA stream */
279 nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_RD);
280 nstrm2 = pr_netio_reopen(nstrm, fd, PR_NETIO_IO_RD);
281 ck_assert_msg(nstrm2 != NULL, "Failed to reopen data stream: %s",
282 strerror(errno));
283
284 res = pr_netio_close(nstrm);
285 ck_assert_msg(res == -1, "Failed to handle bad file descriptor");
286 ck_assert_msg(errno == EBADF, "Failed to set errno to EBADF, got %s (%d)",
287 strerror(errno), errno);
288
289 /* Open/reopen/close OTHR stream */
290 nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_RD);
291 nstrm2 = pr_netio_reopen(nstrm, fd, PR_NETIO_IO_RD);
292 ck_assert_msg(nstrm2 != NULL, "Failed to reopen othr stream: %s",
293 strerror(errno));
294
295 res = pr_netio_close(nstrm);
296 ck_assert_msg(res == -1, "Failed to handle bad file descriptor");
297 ck_assert_msg(errno == EBADF, "Failed to set errno to EBADF, got %s (%d)",
298 strerror(errno), errno);
299 }
300 END_TEST
301
302 START_TEST (netio_buffer_alloc_test) {
303 pr_buffer_t *pbuf;
304 pr_netio_stream_t *nstrm;
305
306 pbuf = pr_netio_buffer_alloc(NULL);
307 ck_assert_msg(pbuf == NULL, "Failed to handle null arguments");
308 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
309 strerror(errno), errno);
310
311 nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
312
313 pbuf = pr_netio_buffer_alloc(nstrm);
314 ck_assert_msg(pbuf != NULL, "Failed to allocate buffer: %s", strerror(errno));
315
316 pr_netio_close(nstrm);
317 }
318 END_TEST
319
320 START_TEST (netio_telnet_gets_args_test) {
321 char *buf, *res;
322 pr_netio_stream_t *in, *out;
323
324 res = pr_netio_telnet_gets(NULL, 0, NULL, NULL);
325 ck_assert_msg(res == NULL, "Failed to handle null arguments");
326 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
327 strerror(errno), errno);
328
329 buf = "";
330 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
331 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
332
333 res = pr_netio_telnet_gets(buf, 0, in, out);
334 ck_assert_msg(res == NULL,
335 "Failed to handle zero-length buffer length argument");
336 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
337 strerror(errno), errno);
338
339 res = pr_netio_telnet_gets(buf, 1, NULL, out);
340 ck_assert_msg(res == NULL, "Failed to handle null input stream argument");
341 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
342 strerror(errno), errno);
343
344 res = pr_netio_telnet_gets(buf, 1, in, NULL);
345 ck_assert_msg(res == NULL, "Failed to handle null output stream argument");
346 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
347 strerror(errno), errno);
348
349 pr_netio_close(in);
350 pr_netio_close(out);
351 }
352 END_TEST
353
354 START_TEST (netio_telnet_gets_single_line_test) {
355 char buf[256], *cmd, *res;
356 pr_netio_stream_t *in, *out;
357 pr_buffer_t *pbuf;
358 int len, xerrno;
359
360 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
361 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
362
363 cmd = "Hello, World!\n";
364
365 pr_netio_buffer_alloc(in);
366 pbuf = in->strm_buf;
367 len = snprintf(pbuf->buf, pbuf->buflen-1, "%s", cmd);
368 pbuf->remaining = pbuf->buflen - len;
369 pbuf->current = pbuf->buf;
370
371 buf[sizeof(buf)-1] = '\0';
372
373 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
374 xerrno = errno;
375
376 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
377 xerrno, strerror(xerrno));
378 ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
379 buf);
380 ck_assert_msg(pbuf->remaining == (size_t) xfer_bufsz,
381 "Expected %d remaining bytes, got %lu", xfer_bufsz,
382 (unsigned long) pbuf->remaining);
383
384 pr_netio_close(in);
385 pr_netio_close(out);
386 }
387 END_TEST
388
389 START_TEST (netio_telnet_gets_multi_line_test) {
390 char buf[256], *cmd, *first_cmd, *second_cmd, *res;
391 pr_netio_stream_t *in, *out;
392 pr_buffer_t *pbuf;
393 int len, xerrno;
394
395 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
396 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
397
398 /* Note: the line terminator in Telnet is CRLF, not just a bare LF. */
399 cmd = "Hello, World!\r\nHow are you?\r\n";
400 first_cmd = "Hello, World!\n";
401 second_cmd = "How are you?\n";
402
403 pr_netio_buffer_alloc(in);
404 pbuf = in->strm_buf;
405 len = snprintf(pbuf->buf, pbuf->buflen-1, "%s", cmd);
406 pbuf->remaining = pbuf->buflen - len;
407 pbuf->current = pbuf->buf;
408
409 buf[sizeof(buf)-1] = '\0';
410
411 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
412 xerrno = errno;
413
414 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
415 xerrno, strerror(xerrno));
416 ck_assert_msg(strcmp(buf, first_cmd) == 0, "Expected string '%s', got '%s'",
417 first_cmd, buf);
418
419 memset(buf, '\0', sizeof(buf));
420 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
421 xerrno = errno;
422
423 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
424 xerrno, strerror(xerrno));
425 ck_assert_msg(strcmp(buf, second_cmd) == 0, "Expected string '%s', got '%s'",
426 second_cmd, buf);
427
428 ck_assert_msg(pbuf->remaining == (size_t) xfer_bufsz,
429 "Expected %d remaining bytes, got %lu", xfer_bufsz,
430 (unsigned long) pbuf->remaining);
431
432 pr_netio_close(in);
433 pr_netio_close(out);
434 }
435 END_TEST
436
437 START_TEST (netio_telnet_gets_no_newline_test) {
438 char buf[8], *cmd, *res;
439 pr_netio_stream_t *in, *out;
440 pr_buffer_t *pbuf;
441 int len, xerrno;
442
443 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
444 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
445
446 cmd = "Hello, World!";
447
448 pr_netio_buffer_alloc(in);
449 pbuf = in->strm_buf;
450 len = snprintf(pbuf->buf, pbuf->buflen-1, "%s", cmd);
451 pbuf->remaining = pbuf->buflen - len;
452 pbuf->current = pbuf->buf;
453
454 buf[sizeof(buf)-1] = '\0';
455
456 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
457 xerrno = errno;
458
459 ck_assert_msg(res == NULL, "Read in string unexpectedly, got '%s'", buf);
460 ck_assert_msg(xerrno == E2BIG, "Failed to set errno to E2BIG, got (%d) %s",
461 xerrno, strerror(xerrno));
462
463 pr_netio_close(in);
464 pr_netio_close(out);
465 }
466 END_TEST
467
468 START_TEST (netio_telnet_gets_telnet_will_test) {
469 char buf[256], *cmd, *res, telnet_opt;
470 pr_netio_stream_t *in, *out;
471 pr_buffer_t *pbuf;
472 int len, out_fd, xerrno;
473
474 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
475
476 out_fd = open_tmpfile();
477 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, out_fd, PR_NETIO_IO_WR);
478
479 cmd = "Hello, World!\n";
480
481 pr_netio_buffer_alloc(in);
482 pbuf = in->strm_buf;
483
484 telnet_opt = 7;
485 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%c%cWorld!\n", TELNET_IAC,
486 TELNET_WILL, telnet_opt);
487 pbuf->remaining = pbuf->buflen - len;
488 pbuf->current = pbuf->buf;
489
490 buf[sizeof(buf)-1] = '\0';
491
492 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
493 xerrno = errno;
494
495 pr_netio_close(in);
496
497 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
498 xerrno, strerror(xerrno));
499 ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
500 buf);
501
502 /* Rewind the output stream fd. */
503 lseek(out_fd, 0, SEEK_SET);
504 len = read(out_fd, buf, sizeof(buf)-1);
505 pr_netio_close(out);
506
507 ck_assert_msg(len == 3, "Expected to read 3 bytes from output stream, got %d",
508 len);
509 ck_assert_msg(buf[0] == (char) TELNET_IAC, "Expected IAC at index 0, got %d",
510 buf[0]);
511 ck_assert_msg(buf[1] == (char) TELNET_DONT, "Expected DONT at index 1, got %d",
512 buf[1]);
513 ck_assert_msg(buf[2] == telnet_opt, "Expected opt '%c' at index 2, got %c",
514 telnet_opt, buf[2]);
515
516 test_cleanup();
517 }
518 END_TEST
519
520 START_TEST (netio_telnet_gets_telnet_bare_will_test) {
521 char buf[256], *cmd, *res, telnet_opt;
522 pr_netio_stream_t *in, *out;
523 pr_buffer_t *pbuf;
524 int len, xerrno;
525
526 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
527 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
528
529 cmd = "Hello, World!\n";
530
531 pr_netio_buffer_alloc(in);
532 pbuf = in->strm_buf;
533
534 telnet_opt = 7;
535 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%cWorld!\n", TELNET_WILL,
536 telnet_opt);
537 pbuf->remaining = pbuf->buflen - len;
538 pbuf->current = pbuf->buf;
539
540 buf[sizeof(buf)-1] = '\0';
541
542 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
543 xerrno = errno;
544
545 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
546 xerrno, strerror(xerrno));
547 ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
548 7, cmd, 7, buf);
549 ck_assert_msg(buf[7] == (char) TELNET_WILL, "Expected WILL at index 7, got %d",
550 buf[7]);
551 ck_assert_msg(buf[8] == telnet_opt, "Expected Telnet opt %c at index 8, got %d",
552 telnet_opt, buf[8]);
553 ck_assert_msg(strcmp(buf + 9, cmd + 7) == 0, "Expected string '%s', got '%s'",
554 cmd + 7, buf + 9);
555
556 pr_netio_close(in);
557 pr_netio_close(out);
558 }
559 END_TEST
560
561 START_TEST (netio_telnet_gets_telnet_will_multi_read_test) {
562 char buf[256], *cmd, *res, telnet_opt;
563 pr_netio_stream_t *in, *out;
564 pr_buffer_t *pbuf;
565 int len, out_fd, xerrno;
566
567 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
568
569 out_fd = open_tmpfile();
570 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, out_fd, PR_NETIO_IO_WR);
571
572 cmd = "Hello, World!\n";
573
574 pr_netio_buffer_alloc(in);
575 pbuf = in->strm_buf;
576
577 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%c", TELNET_IAC,
578 TELNET_WILL);
579 pbuf->remaining = pbuf->buflen - len;
580 pbuf->current = pbuf->buf;
581
582 buf[sizeof(buf)-1] = '\0';
583
584 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
585 xerrno = errno;
586
587 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
588 xerrno, strerror(xerrno));
589 ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
590 7, cmd, 7, buf);
591
592 /* Fill up the input stream's buffer with the rest of the Telnet WILL
593 * sequence.
594 */
595 telnet_opt = 7;
596 len = snprintf(pbuf->buf, pbuf->buflen-1, "%cWorld!\n", telnet_opt);
597 pbuf->remaining = pbuf->buflen - len;
598 pbuf->current = pbuf->buf;
599
600 /* Read again, to see if the state was preserved across multiple calls
601 * to pr_netio_telnet_gets().
602 */
603 res = pr_netio_telnet_gets(buf + 7, sizeof(buf)-8, in, out);
604 xerrno = errno;
605
606 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
607 xerrno, strerror(xerrno));
608 ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'",
609 cmd, buf);
610
611 pr_netio_close(in);
612
613 /* Rewind the output stream fd. */
614 lseek(out_fd, 0, SEEK_SET);
615 len = read(out_fd, buf, sizeof(buf)-1);
616 pr_netio_close(out);
617
618 ck_assert_msg(len == 3, "Expected to read 3 bytes from output stream, got %d",
619 len);
620 ck_assert_msg(buf[0] == (char) TELNET_IAC, "Expected IAC at index 0, got %d",
621 buf[0]);
622 ck_assert_msg(buf[1] == (char) TELNET_DONT, "Expected DONT at index 1, got %d",
623 buf[1]);
624 ck_assert_msg(buf[2] == telnet_opt, "Expected %c at index 2, got %d",
625 telnet_opt, buf[2]);
626
627 test_cleanup();
628 }
629 END_TEST
630
631 START_TEST (netio_telnet_gets_telnet_wont_test) {
632 char buf[256], *cmd, *res, telnet_opt;
633 pr_netio_stream_t *in, *out;
634 pr_buffer_t *pbuf;
635 int len, out_fd, xerrno;
636
637 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
638
639 out_fd = open_tmpfile();
640 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, out_fd, PR_NETIO_IO_WR);
641
642 cmd = "Hello, World!\n";
643
644 pr_netio_buffer_alloc(in);
645 pbuf = in->strm_buf;
646
647 telnet_opt = 7;
648 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%c%cWorld!\n", TELNET_IAC,
649 TELNET_WONT, telnet_opt);
650 pbuf->remaining = pbuf->buflen - len;
651 pbuf->current = pbuf->buf;
652
653 buf[sizeof(buf)-1] = '\0';
654
655 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
656 xerrno = errno;
657
658 pr_netio_close(in);
659
660 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
661 xerrno, strerror(xerrno));
662 ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
663 buf);
664
665 /* Rewind the output stream fd. */
666 lseek(out_fd, 0, SEEK_SET);
667 len = read(out_fd, buf, sizeof(buf)-1);
668 pr_netio_close(out);
669
670 ck_assert_msg(len == 3, "Expected to read 3 bytes from output stream, got %d",
671 len);
672 ck_assert_msg(buf[0] == (char) TELNET_IAC, "Expected IAC at index 0, got %d",
673 buf[0]);
674 ck_assert_msg(buf[1] == (char) TELNET_DONT, "Expected DONT at index 1, got %d",
675 buf[1]);
676 ck_assert_msg(buf[2] == telnet_opt, "Expected opt '%c' at index 2, got %c",
677 telnet_opt, buf[2]);
678
679 test_cleanup();
680 }
681 END_TEST
682
683 START_TEST (netio_telnet_gets_telnet_bare_wont_test) {
684 char buf[256], *cmd, *res, telnet_opt;
685 pr_netio_stream_t *in, *out;
686 pr_buffer_t *pbuf;
687 int len, xerrno;
688
689 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
690 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
691
692 cmd = "Hello, World!\n";
693
694 pr_netio_buffer_alloc(in);
695 pbuf = in->strm_buf;
696
697 telnet_opt = 7;
698 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%cWorld!\n", TELNET_WONT,
699 telnet_opt);
700 pbuf->remaining = pbuf->buflen - len;
701 pbuf->current = pbuf->buf;
702
703 buf[sizeof(buf)-1] = '\0';
704
705 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
706 xerrno = errno;
707
708 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
709 xerrno, strerror(xerrno));
710 ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
711 7, cmd, 7, buf);
712 ck_assert_msg(buf[7] == (char) TELNET_WONT, "Expected WONT at index 7, got %d",
713 buf[7]);
714 ck_assert_msg(buf[8] == telnet_opt, "Expected Telnet opt %c at index 8, got %d",
715 telnet_opt, buf[8]);
716 ck_assert_msg(strcmp(buf + 9, cmd + 7) == 0, "Expected string '%s', got '%s'",
717 cmd + 7, buf + 9);
718
719 pr_netio_close(in);
720 pr_netio_close(out);
721 }
722 END_TEST
723
724 START_TEST (netio_telnet_gets_telnet_do_test) {
725 char buf[256], *cmd, *res, telnet_opt;
726 pr_netio_stream_t *in, *out;
727 pr_buffer_t *pbuf;
728 int len, out_fd, xerrno;
729
730 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
731
732 out_fd = open_tmpfile();
733 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, out_fd, PR_NETIO_IO_WR);
734
735 cmd = "Hello, World!\n";
736
737 pr_netio_buffer_alloc(in);
738 pbuf = in->strm_buf;
739
740 telnet_opt = 7;
741 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%c%cWorld!\n", TELNET_IAC,
742 TELNET_DO, telnet_opt);
743 pbuf->remaining = pbuf->buflen - len;
744 pbuf->current = pbuf->buf;
745
746 buf[sizeof(buf)-1] = '\0';
747
748 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
749 xerrno = errno;
750
751 pr_netio_close(in);
752
753 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
754 xerrno, strerror(xerrno));
755 ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
756 buf);
757
758 /* Rewind the output stream fd. */
759 lseek(out_fd, 0, SEEK_SET);
760 len = read(out_fd, buf, sizeof(buf)-1);
761 pr_netio_close(out);
762
763 ck_assert_msg(len == 3, "Expected to read 3 bytes from output stream, got %d",
764 len);
765 ck_assert_msg(buf[0] == (char) TELNET_IAC, "Expected IAC at index 0, got %d",
766 buf[0]);
767 ck_assert_msg(buf[1] == (char) TELNET_WONT, "Expected WONT at index 1, got %d",
768 buf[1]);
769 ck_assert_msg(buf[2] == telnet_opt, "Expected opt '%c' at index 2, got %c",
770 telnet_opt, buf[2]);
771
772 test_cleanup();
773 }
774 END_TEST
775
776 START_TEST (netio_telnet_gets_telnet_bare_do_test) {
777 char buf[256], *cmd, *res, telnet_opt;
778 pr_netio_stream_t *in, *out;
779 pr_buffer_t *pbuf;
780 int len, xerrno;
781
782 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
783 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
784
785 cmd = "Hello, World!\n";
786
787 pr_netio_buffer_alloc(in);
788 pbuf = in->strm_buf;
789
790 telnet_opt = 7;
791 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%cWorld!\n", TELNET_DO,
792 telnet_opt);
793 pbuf->remaining = pbuf->buflen - len;
794 pbuf->current = pbuf->buf;
795
796 buf[sizeof(buf)-1] = '\0';
797
798 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
799 xerrno = errno;
800
801 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
802 xerrno, strerror(xerrno));
803 ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
804 7, cmd, 7, buf);
805 ck_assert_msg(buf[7] == (char) TELNET_DO, "Expected DO at index 7, got %d",
806 buf[7]);
807 ck_assert_msg(buf[8] == telnet_opt, "Expected Telnet opt %c at index 8, got %d",
808 telnet_opt, buf[8]);
809 ck_assert_msg(strcmp(buf + 9, cmd + 7) == 0, "Expected string '%s', got '%s'",
810 cmd + 7, buf + 9);
811
812 pr_netio_close(in);
813 pr_netio_close(out);
814 }
815 END_TEST
816
817 START_TEST (netio_telnet_gets_telnet_dont_test) {
818 char buf[256], *cmd, *res, telnet_opt;
819 pr_netio_stream_t *in, *out;
820 pr_buffer_t *pbuf;
821 int len, out_fd, xerrno;
822
823 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
824
825 out_fd = open_tmpfile();
826 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, out_fd, PR_NETIO_IO_WR);
827
828 cmd = "Hello, World!\n";
829
830 pr_netio_buffer_alloc(in);
831 pbuf = in->strm_buf;
832
833 telnet_opt = 7;
834 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%c%cWorld!\n", TELNET_IAC,
835 TELNET_DONT, telnet_opt);
836 pbuf->remaining = pbuf->buflen - len;
837 pbuf->current = pbuf->buf;
838
839 buf[sizeof(buf)-1] = '\0';
840
841 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
842 xerrno = errno;
843
844 pr_netio_close(in);
845
846 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
847 xerrno, strerror(xerrno));
848 ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
849 buf);
850
851 /* Rewind the output stream fd. */
852 lseek(out_fd, 0, SEEK_SET);
853 len = read(out_fd, buf, sizeof(buf)-1);
854 pr_netio_close(out);
855
856 ck_assert_msg(len == 3, "Expected to read 3 bytes from output stream, got %d",
857 len);
858 ck_assert_msg(buf[0] == (char) TELNET_IAC, "Expected IAC at index 0, got %d",
859 buf[0]);
860 ck_assert_msg(buf[1] == (char) TELNET_WONT, "Expected WONT at index 1, got %d",
861 buf[1]);
862 ck_assert_msg(buf[2] == telnet_opt, "Expected opt '%c' at index 2, got %c",
863 telnet_opt, buf[2]);
864
865 test_cleanup();
866 }
867 END_TEST
868
869 START_TEST (netio_telnet_gets_telnet_bare_dont_test) {
870 char buf[256], *cmd, *res, telnet_opt;
871 pr_netio_stream_t *in, *out;
872 pr_buffer_t *pbuf;
873 int len, xerrno;
874
875 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
876 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
877
878 cmd = "Hello, World!\n";
879
880 pr_netio_buffer_alloc(in);
881 pbuf = in->strm_buf;
882
883 telnet_opt = 7;
884 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%cWorld!\n", TELNET_DONT,
885 telnet_opt);
886 pbuf->remaining = pbuf->buflen - len;
887 pbuf->current = pbuf->buf;
888
889 buf[sizeof(buf)-1] = '\0';
890
891 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
892 xerrno = errno;
893
894 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
895 xerrno, strerror(xerrno));
896 ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
897 7, cmd, 7, buf);
898 ck_assert_msg(buf[7] == (char) TELNET_DONT, "Expected DONT at index 7, got %d",
899 buf[7]);
900 ck_assert_msg(buf[8] == telnet_opt, "Expected Telnet opt %c at index 8, got %d",
901 telnet_opt, buf[8]);
902 ck_assert_msg(strcmp(buf + 9, cmd + 7) == 0, "Expected string '%s', got '%s'",
903 cmd + 7, buf + 9);
904
905 pr_netio_close(in);
906 pr_netio_close(out);
907 }
908 END_TEST
909
910 START_TEST (netio_telnet_gets_telnet_ip_test) {
911 char buf[256], *cmd, *res;
912 pr_netio_stream_t *in, *out;
913 pr_buffer_t *pbuf;
914 int len, xerrno;
915
916 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
917 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
918
919 cmd = "Hello, World!\n";
920
921 pr_netio_buffer_alloc(in);
922 pbuf = in->strm_buf;
923
924 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%cWorld!\n", TELNET_IAC,
925 TELNET_IP);
926 pbuf->remaining = pbuf->buflen - len;
927 pbuf->current = pbuf->buf;
928
929 buf[sizeof(buf)-1] = '\0';
930
931 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
932 xerrno = errno;
933
934 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
935 xerrno, strerror(xerrno));
936 ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
937 buf);
938
939 pr_netio_close(in);
940 pr_netio_close(out);
941 }
942 END_TEST
943
944 START_TEST (netio_telnet_gets_telnet_bare_ip_test) {
945 char buf[256], *cmd, *res;
946 pr_netio_stream_t *in, *out;
947 pr_buffer_t *pbuf;
948 int len, xerrno;
949
950 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
951 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
952
953 cmd = "Hello, World!\n";
954
955 pr_netio_buffer_alloc(in);
956 pbuf = in->strm_buf;
957
958 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %cWorld!\n", TELNET_IP);
959 pbuf->remaining = pbuf->buflen - len;
960 pbuf->current = pbuf->buf;
961
962 buf[sizeof(buf)-1] = '\0';
963
964 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
965 xerrno = errno;
966
967 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
968 xerrno, strerror(xerrno));
969 ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
970 7, cmd, 7, buf);
971 ck_assert_msg(buf[7] == (char) TELNET_IP, "Expected IP at index 7, got %d",
972 buf[7]);
973 ck_assert_msg(strcmp(buf + 8, cmd + 7) == 0, "Expected string '%s', got '%s'",
974 cmd + 7, buf + 8);
975
976 pr_netio_close(in);
977 pr_netio_close(out);
978 }
979 END_TEST
980
981 START_TEST (netio_telnet_gets_telnet_dm_test) {
982 char buf[256], *cmd, *res;
983 pr_netio_stream_t *in, *out;
984 pr_buffer_t *pbuf;
985 int len, xerrno;
986
987 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
988 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
989
990 cmd = "Hello, World!\n";
991
992 pr_netio_buffer_alloc(in);
993 pbuf = in->strm_buf;
994
995 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%cWorld!\n", TELNET_IAC,
996 TELNET_DM);
997 pbuf->remaining = pbuf->buflen - len;
998 pbuf->current = pbuf->buf;
999
1000 buf[sizeof(buf)-1] = '\0';
1001
1002 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
1003 xerrno = errno;
1004
1005 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
1006 xerrno, strerror(xerrno));
1007 ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
1008 buf);
1009
1010 pr_netio_close(in);
1011 pr_netio_close(out);
1012 }
1013 END_TEST
1014
1015 START_TEST (netio_telnet_gets_telnet_bare_dm_test) {
1016 char buf[256], *cmd, *res;
1017 pr_netio_stream_t *in, *out;
1018 pr_buffer_t *pbuf;
1019 int len, xerrno;
1020
1021 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1022 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1023
1024 cmd = "Hello, World!\n";
1025
1026 pr_netio_buffer_alloc(in);
1027 pbuf = in->strm_buf;
1028
1029 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %cWorld!\n", TELNET_DM);
1030 pbuf->remaining = pbuf->buflen - len;
1031 pbuf->current = pbuf->buf;
1032
1033 buf[sizeof(buf)-1] = '\0';
1034
1035 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
1036 xerrno = errno;
1037
1038 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
1039 xerrno, strerror(xerrno));
1040 ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
1041 7, cmd, 7, buf);
1042 ck_assert_msg(buf[7] == (char) TELNET_DM, "Expected DM at index 7, got %d",
1043 buf[7]);
1044 ck_assert_msg(strcmp(buf + 8, cmd + 7) == 0, "Expected string '%s', got '%s'",
1045 cmd + 7, buf + 8);
1046
1047 pr_netio_close(in);
1048 pr_netio_close(out);
1049 }
1050 END_TEST
1051
1052 START_TEST (netio_telnet_gets_telnet_single_iac_test) {
1053 char buf[256], *cmd, *res;
1054 pr_netio_stream_t *in, *out;
1055 pr_buffer_t *pbuf;
1056 int len, xerrno;
1057
1058 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1059 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1060
1061 cmd = "Hello, World!\n";
1062
1063 pr_netio_buffer_alloc(in);
1064 pbuf = in->strm_buf;
1065
1066 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %cWorld!\n", TELNET_IAC);
1067 pbuf->remaining = pbuf->buflen - len;
1068 pbuf->current = pbuf->buf;
1069
1070 buf[sizeof(buf)-1] = '\0';
1071
1072 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
1073 xerrno = errno;
1074
1075 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
1076 xerrno, strerror(xerrno));
1077 ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
1078 7, cmd, 7, buf);
1079 ck_assert_msg(buf[7] == (char) TELNET_IAC, "Expected IAC at index 7, got %d",
1080 buf[7]);
1081 ck_assert_msg(strcmp(buf + 8, cmd + 7) == 0, "Expected string '%s', got '%s'",
1082 cmd + 7, buf + 8);
1083
1084 pr_netio_close(in);
1085 pr_netio_close(out);
1086 }
1087 END_TEST
1088
1089 START_TEST (netio_telnet_gets_bug3521_test) {
1090 char buf[10], *res, telnet_opt;
1091 pr_netio_stream_t *in, *out;
1092 pr_buffer_t *pbuf;
1093 int len, xerrno;
1094
1095 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1096 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1097
1098 pr_netio_buffer_alloc(in);
1099 pbuf = in->strm_buf;
1100
1101 telnet_opt = 7;
1102 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%c%c%cWorld!\n",
1103 TELNET_IAC, TELNET_IAC, TELNET_WILL, telnet_opt);
1104 pbuf->remaining = pbuf->buflen - len;
1105 pbuf->current = pbuf->buf;
1106
1107 buf[sizeof(buf)-1] = '\0';
1108
1109 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
1110 xerrno = errno;
1111
1112 ck_assert_msg(res == NULL, "Expected null");
1113 ck_assert_msg(xerrno == E2BIG, "Failed to set errno to E2BIG, got %s (%d)",
1114 strerror(xerrno), xerrno);
1115
1116 pr_netio_close(in);
1117 pr_netio_close(out);
1118 }
1119 END_TEST
1120
1121 START_TEST (netio_telnet_gets_bug3697_test) {
1122 char buf[256], *cmd, *res;
1123 pr_netio_stream_t *in, *out;
1124 pr_buffer_t *pbuf;
1125 int len, xerrno;
1126
1127 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1128 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1129
1130 cmd = "Hello, World!\n";
1131
1132 pr_netio_buffer_alloc(in);
1133 pbuf = in->strm_buf;
1134
1135 len = snprintf(pbuf->buf, pbuf->buflen-1, "Hello, %c%cWorld!\n", TELNET_IAC,
1136 TELNET_IAC);
1137 pbuf->remaining = pbuf->buflen - len;
1138 pbuf->current = pbuf->buf;
1139
1140 buf[sizeof(buf)-1] = '\0';
1141
1142 res = pr_netio_telnet_gets(buf, sizeof(buf)-1, in, out);
1143 xerrno = errno;
1144
1145 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
1146 xerrno, strerror(xerrno));
1147 ck_assert_msg(strncmp(buf, cmd, 7) == 0, "Expected string '%.*s', got '%.*s'",
1148 7, cmd, 7, buf);
1149 ck_assert_msg(buf[7] == (char) TELNET_IAC, "Expected IAC at index 7, got %d",
1150 buf[7]);
1151 ck_assert_msg(strcmp(buf + 8, cmd + 7) == 0, "Expected string '%s', got '%s'",
1152 cmd + 7, buf + 8);
1153
1154 pr_netio_close(in);
1155 pr_netio_close(out);
1156 }
1157 END_TEST
1158
1159 START_TEST (netio_telnet_gets_eof_test) {
1160 char buf[256], *cmd, *res;
1161 pr_netio_stream_t *in, *out;
1162 pr_buffer_t *pbuf;
1163 int len, xerrno;
1164
1165 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1166 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1167
1168 cmd = "Hello, World!";
1169
1170 pr_netio_buffer_alloc(in);
1171 pbuf = in->strm_buf;
1172
1173 len = snprintf(pbuf->buf, pbuf->buflen-1, "%s", cmd);
1174 pbuf->remaining = pbuf->buflen - len;
1175 pbuf->current = pbuf->buf;
1176
1177 buf[sizeof(buf)-1] = '\0';
1178
1179 /* In this scenario, we have not supplied an LF, but the resulting buffer
1180 * is terminated with a NUL because of the end-of-stream (or error) checks
1181 * in pr_netio_telnet_gets(), when we read the input stream for more data
1182 * looking for that LF.
1183 */
1184 res = pr_netio_telnet_gets(buf, strlen(cmd) + 2, in, out);
1185 xerrno = errno;
1186
1187 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
1188 xerrno, strerror(xerrno));
1189 ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
1190 buf);
1191
1192 pr_netio_close(in);
1193 pr_netio_close(out);
1194 }
1195 END_TEST
1196
1197 START_TEST (netio_telnet_gets_random_data_test) {
1198 register unsigned int i;
1199 char *buf, *res;
1200 size_t bufsz;
1201 pr_netio_stream_t *in, *out;
1202 pr_buffer_t *pbuf;
1203 int xerrno;
1204
1205 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1206 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1207
1208 pbuf = pr_netio_buffer_alloc(in);
1209
1210 /* Fill the input buffer with random values; save the last two bytes for
1211 * the CRLF.
1212 */
1213 for (i = 0; i < (pbuf->buflen - 2); i++) {
1214 long r;
1215
1216 r = pr_random_next(CHAR_MIN, CHAR_MAX);
1217 pbuf->buf[i] = (char) r;
1218 }
1219 pbuf->buf[i++] = '\r';
1220 pbuf->buf[i++] = '\n';
1221
1222 pbuf->remaining = 0;
1223 pbuf->current = pbuf->buf;
1224
1225 /* Make sure our output buffer is of sufficient size. */
1226 bufsz = pbuf->buflen + 1;
1227 buf = pcalloc(p, bufsz);
1228
1229 res = pr_netio_telnet_gets(buf, bufsz, in, out);
1230 xerrno = errno;
1231
1232 ck_assert_msg(res != NULL, "Failed to get string from stream: (%d) %s",
1233 xerrno, strerror(xerrno));
1234
1235 pr_netio_close(in);
1236 pr_netio_close(out);
1237 }
1238 END_TEST
1239
1240 START_TEST (netio_telnet_gets2_single_line_test) {
1241 int res;
1242 char buf[256], *cmd;
1243 size_t cmd_len;
1244 pr_netio_stream_t *in, *out;
1245 pr_buffer_t *pbuf;
1246 int len, xerrno;
1247
1248 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1249 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1250
1251 cmd = "Hello, World!\n";
1252 cmd_len = strlen(cmd);
1253
1254 pr_netio_buffer_alloc(in);
1255 pbuf = in->strm_buf;
1256 len = snprintf(pbuf->buf, pbuf->buflen-1, "%s", cmd);
1257 pbuf->remaining = pbuf->buflen - len;
1258 pbuf->current = pbuf->buf;
1259
1260 buf[sizeof(buf)-1] = '\0';
1261
1262 res = pr_netio_telnet_gets2(buf, sizeof(buf)-1, in, out);
1263 xerrno = errno;
1264
1265 ck_assert_msg(res > 0, "Failed to get string from stream: (%d) %s",
1266 xerrno, strerror(xerrno));
1267 ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
1268 buf);
1269
1270 ck_assert_msg((size_t) res == cmd_len, "Expected length %lu, got %d",
1271 (unsigned long) cmd_len, res);
1272 ck_assert_msg(pbuf->remaining == (size_t) xfer_bufsz,
1273 "Expected %d remaining bytes, got %lu", xfer_bufsz,
1274 (unsigned long) pbuf->remaining);
1275
1276 pr_netio_close(in);
1277 pr_netio_close(out);
1278 }
1279 END_TEST
1280
1281 START_TEST (netio_telnet_gets2_single_line_crnul_test) {
1282 int res;
1283 char buf[256], *cmd;
1284 size_t cmd_len;
1285 pr_netio_stream_t *in, *out;
1286 pr_buffer_t *pbuf;
1287 int xerrno;
1288
1289 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1290 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1291
1292 /* See Bug#4167. We cannot use strlen(3) due to the embedded NUL. */
1293 cmd = "Hello, \015\000World!\n";
1294 cmd_len = 14;
1295
1296 pr_netio_buffer_alloc(in);
1297 pbuf = in->strm_buf;
1298 memcpy(pbuf->buf, cmd, cmd_len);
1299 pbuf->remaining = pbuf->buflen - cmd_len;
1300 pbuf->current = pbuf->buf;
1301
1302 buf[sizeof(buf)-1] = '\0';
1303
1304 res = pr_netio_telnet_gets2(buf, sizeof(buf)-1, in, out);
1305 xerrno = errno;
1306
1307 ck_assert_msg(res > 0, "Failed to get string from stream: (%d) %s",
1308 xerrno, strerror(xerrno));
1309 ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
1310 buf);
1311
1312 ck_assert_msg((size_t) res == cmd_len, "Expected length %lu, got %d",
1313 (unsigned long) cmd_len, res);
1314 ck_assert_msg(pbuf->remaining == (size_t) xfer_bufsz,
1315 "Expected %d remaining bytes, got %lu", xfer_bufsz,
1316 (unsigned long) pbuf->remaining);
1317
1318 pr_netio_close(in);
1319 pr_netio_close(out);
1320 }
1321 END_TEST
1322
1323 START_TEST (netio_telnet_gets2_single_line_lf_test) {
1324 int res;
1325 char buf[256], *cmd;
1326 size_t cmd_len;
1327 pr_netio_stream_t *in, *out;
1328 pr_buffer_t *pbuf;
1329 int xerrno;
1330
1331 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1332 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1333
1334 cmd = "Hello, \012World!\n";
1335 cmd_len = strlen(cmd);
1336
1337 pr_netio_buffer_alloc(in);
1338 pbuf = in->strm_buf;
1339 memcpy(pbuf->buf, cmd, cmd_len);
1340 pbuf->remaining = pbuf->buflen - cmd_len;
1341 pbuf->current = pbuf->buf;
1342
1343 buf[sizeof(buf)-1] = '\0';
1344
1345 res = pr_netio_telnet_gets2(buf, sizeof(buf)-1, in, out);
1346 xerrno = errno;
1347
1348 ck_assert_msg(res > 0, "Failed to get string from stream: (%d) %s",
1349 xerrno, strerror(xerrno));
1350 ck_assert_msg(strcmp(buf, cmd) == 0, "Expected string '%s', got '%s'", cmd,
1351 buf);
1352
1353 ck_assert_msg((size_t) res == cmd_len, "Expected length %lu, got %d",
1354 (unsigned long) cmd_len, res);
1355 ck_assert_msg(pbuf->remaining == (size_t) xfer_bufsz,
1356 "Expected %d remaining bytes, got %lu", xfer_bufsz,
1357 (unsigned long) pbuf->remaining);
1358
1359 pr_netio_close(in);
1360 pr_netio_close(out);
1361 }
1362 END_TEST
1363
1364 START_TEST (netio_telnet_gets2_random_data_test) {
1365 register unsigned int i;
1366 int res;
1367 char *buf;
1368 size_t bufsz;
1369 pr_netio_stream_t *in, *out;
1370 pr_buffer_t *pbuf;
1371 int xerrno;
1372
1373 in = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_RD);
1374 out = pr_netio_open(p, PR_NETIO_STRM_CTRL, -1, PR_NETIO_IO_WR);
1375
1376 pbuf = pr_netio_buffer_alloc(in);
1377
1378 /* Fill the input buffer with random values; save the last two bytes for
1379 * the CRLF.
1380 */
1381 for (i = 0; i < (pbuf->buflen - 2); i++) {
1382 long r;
1383
1384 r = pr_random_next(CHAR_MIN, CHAR_MAX);
1385 pbuf->buf[i] = (char) r;
1386 }
1387 pbuf->buf[i++] = '\r';
1388 pbuf->buf[i++] = '\n';
1389
1390 pbuf->remaining = 0;
1391 pbuf->current = pbuf->buf;
1392
1393 /* Make sure our output buffer is of sufficient size. */
1394 bufsz = pbuf->buflen + 1;
1395 buf = pcalloc(p, bufsz);
1396
1397 res = pr_netio_telnet_gets2(buf, bufsz, in, out);
1398 xerrno = errno;
1399
1400 ck_assert_msg(res > 0, "Failed to get string from stream: (%d) %s",
1401 xerrno, strerror(xerrno));
1402
1403 pr_netio_close(in);
1404 pr_netio_close(out);
1405 }
1406 END_TEST
1407
1408 static int netio_close_cb(pr_netio_stream_t *nstrm) {
1409 return 0;
1410 }
1411
1412 static int netio_poll_cb(pr_netio_stream_t *nstrm) {
1413 /* Always return >0, to indicate that we haven't timed out, AND that there
1414 * is a writable fd available.
1415 */
1416 return 7;
1417 }
1418
1419 static int netio_read_eof = FALSE;
1420 static int netio_read_epipe = FALSE;
1421
1422 static int netio_read_cb(pr_netio_stream_t *nstrm, char *buf, size_t buflen) {
1423 const char *text;
1424 int res;
1425
1426 if (netio_read_eof) {
1427 netio_read_eof = FALSE;
1428 return 0;
1429 }
1430
1431 if (netio_read_epipe) {
1432 netio_read_epipe = FALSE;
1433 errno = EPIPE;
1434 return -1;
1435 }
1436
1437 text = "Hello, World!\r\n";
1438 sstrncpy(buf, text, buflen);
1439
1440 /* Make sure the next read returns EOF. */
1441 netio_read_eof = TRUE;
1442
1443 res = strlen(text);
1444 return res;
1445 }
1446
1447 static int netio_write_epipe = FALSE;
1448
1449 static int netio_write_cb(pr_netio_stream_t *nstrm, char *buf, size_t buflen) {
1450 if (netio_write_epipe) {
1451 netio_write_epipe = FALSE;
1452 errno = EPIPE;
1453 return -1;
1454 }
1455
1456 return buflen;
1457 }
1458
1459 static int devnull_fd(void) {
1460 int fd;
1461
1462 fd = open("/dev/null", O_RDWR);
1463 if (fd < 0) {
1464 fprintf(stderr, "Error opening /dev/null: %s\n", strerror(errno));
1465 return -1;
1466 }
1467
1468 return fd;
1469 }
1470
1471 static int netio_read_from_stream(int strm_type) {
1472 int fd, res;
1473 char buf[1024], *expected_text;
1474 size_t expected_sz;
1475 pr_netio_stream_t *nstrm;
1476
1477 res = pr_netio_read(NULL, NULL, 0, 0);
1478 if (res == 0) {
1479 errno = EINVAL;
1480 return -1;
1481 }
1482
1483 fd = devnull_fd();
1484 if (fd < 0) {
1485 return -1;
1486 }
1487
1488 nstrm = pr_netio_open(p, strm_type, fd, PR_NETIO_IO_RD);
1489 if (nstrm == NULL) {
1490 int xerrno = errno;
1491
1492 pr_trace_msg("netio", 1, "error opening custom netio stream: %s",
1493 strerror(xerrno));
1494 errno = xerrno;
1495 return -1;
1496 }
1497
1498 res = pr_netio_read(nstrm, NULL, 0, 0);
1499 if (res == 0) {
1500 pr_netio_close(nstrm);
1501 errno = EINVAL;
1502 return -1;
1503 }
1504
1505 res = pr_netio_read(nstrm, buf, 0, 0);
1506 if (res == 0) {
1507 pr_netio_close(nstrm);
1508 errno = EINVAL;
1509 return -1;
1510 }
1511
1512 expected_text = "Hello, World!\r\n";
1513 expected_sz = strlen(expected_text);
1514
1515 memset(buf, '\0', sizeof(buf));
1516 res = pr_netio_read(nstrm, buf, sizeof(buf)-1, 1);
1517
1518 if (res != (int) expected_sz) {
1519 pr_trace_msg("netio", 1, "Expected %lu bytes, got %d",
1520 (unsigned long) expected_sz, res);
1521 pr_netio_close(nstrm);
1522
1523 if (res < 0) {
1524 return -1;
1525 }
1526
1527 errno = EIO;
1528 return -1;
1529 }
1530
1531 if (strcmp(buf, expected_text) != 0) {
1532 pr_trace_msg("netio", 1, "Expected '%s', got '%s'", expected_text, buf);
1533 pr_netio_close(nstrm);
1534
1535 errno = EIO;
1536 return -1;
1537 }
1538
1539 netio_read_eof = TRUE;
1540 res = pr_netio_read(nstrm, buf, sizeof(buf)-1, 1);
1541 if (res > 0) {
1542 pr_trace_msg("netio", 1, "Expected EOF (0), got %d", res);
1543 pr_netio_close(nstrm);
1544
1545 errno = EIO;
1546 return -1;
1547 }
1548
1549 netio_read_epipe = TRUE;
1550 res = pr_netio_read(nstrm, buf, sizeof(buf)-1, sizeof(buf)-1);
1551 if (res >= 0) {
1552 pr_trace_msg("netio", 1, "Expected EPIPE (-1), got %d", res);
1553 pr_netio_close(nstrm);
1554
1555 errno = EIO;
1556 return -1;
1557 }
1558
1559 mark_point();
1560 pr_netio_close(nstrm);
1561 return 0;
1562 }
1563
1564 static int netio_write_to_stream(int strm_type, int use_async) {
1565 int fd, res;
1566 char *buf;
1567 size_t buflen;
1568 pr_netio_stream_t *nstrm;
1569
1570 res = pr_netio_write(NULL, NULL, 0);
1571 if (res == 0) {
1572 errno = EINVAL;
1573 return -1;
1574 }
1575
1576 fd = devnull_fd();
1577 if (fd < 0) {
1578 return -1;
1579 }
1580
1581 nstrm = pr_netio_open(p, strm_type, fd, PR_NETIO_IO_WR);
1582 if (nstrm == NULL) {
1583 int xerrno = errno;
1584
1585 pr_trace_msg("netio", 1, "error opening custom netio stream: %s",
1586 strerror(xerrno));
1587 errno = xerrno;
1588 return -1;
1589 }
1590
1591 res = pr_netio_write(nstrm, NULL, 0);
1592 if (res == 0) {
1593 pr_netio_close(nstrm);
1594 errno = EINVAL;
1595 return -1;
1596 }
1597
1598 buf = "Hello, World!\n";
1599 buflen = strlen(buf);
1600
1601 res = pr_netio_write(nstrm, buf, 0);
1602 if (res == 0) {
1603 pr_netio_close(nstrm);
1604 errno = EINVAL;
1605 return -1;
1606 }
1607
1608 if (use_async) {
1609 res = pr_netio_write_async(nstrm, buf, buflen);
1610
1611 } else {
1612 res = pr_netio_write(nstrm, buf, buflen);
1613 }
1614
1615 if ((size_t) res != buflen) {
1616 pr_trace_msg("netio", 1, "wrote buffer (%lu bytes), got %d",
1617 (unsigned long) buflen, res);
1618 pr_netio_close(nstrm);
1619
1620 if (res < 0) {
1621 return -1;
1622 }
1623
1624 errno = EIO;
1625 return -1;
1626 }
1627
1628 netio_write_epipe = TRUE;
1629 res = pr_netio_write(nstrm, buf, buflen);
1630 if (res >= 0) {
1631 pr_trace_msg("netio", 1, "Expected EPIPE (-1), got %d", res);
1632 pr_netio_close(nstrm);
1633 errno = EIO;
1634 return -1;
1635 }
1636
1637 mark_point();
1638 pr_netio_close(nstrm);
1639 return 0;
1640 }
1641
1642 START_TEST (netio_read_test) {
1643 int res;
1644 pr_netio_t *netio, *netio2;
1645 pr_netio_stream_t *nstrm;
1646 char *buf;
1647
1648 mark_point();
1649 res = pr_netio_read(NULL, NULL, 0, 0);
1650 ck_assert_msg(res < 0, "Failed to handle null nstrm");
1651 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1652 strerror(errno), errno);
1653
1654 mark_point();
1655 nstrm = pcalloc(p, sizeof(pr_netio_stream_t));
1656 res = pr_netio_read(nstrm, NULL, 0, 0);
1657 ck_assert_msg(res < 0, "Failed to handle null buf");
1658 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1659 strerror(errno), errno);
1660
1661 mark_point();
1662 buf = "foo";
1663 res = pr_netio_read(nstrm, buf, 0, 0);
1664 ck_assert_msg(res < 0, "Failed to handle zero buflen");
1665 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1666 strerror(errno), errno);
1667
1668 mark_point();
1669 nstrm->strm_fd = -2;
1670 res = pr_netio_read(nstrm, buf, 3, 0);
1671 ck_assert_msg(res < 0, "Failed to handle bad nstrm fd");
1672 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
1673 strerror(errno), errno);
1674
1675 netio = pr_alloc_netio2(p, NULL, "testsuite");
1676 netio->poll = netio_poll_cb;
1677 netio->read = netio_read_cb;
1678
1679 /* Write to control stream */
1680 res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
1681 ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
1682 strerror(errno));
1683
1684 netio2 = pr_get_netio(PR_NETIO_STRM_CTRL);
1685 ck_assert_msg(netio2 != NULL, "Failed to get custom ctrl NetIO: %s",
1686 strerror(errno));
1687 ck_assert_msg(netio2 == netio, "Expected custom ctrl NetIO %p, got %p",
1688 netio, netio2);
1689
1690 res = netio_read_from_stream(PR_NETIO_STRM_CTRL);
1691 ck_assert_msg(res == 0, "Failed to read from custom ctrl NetIO: %s",
1692 strerror(errno));
1693
1694 mark_point();
1695 pr_unregister_netio(PR_NETIO_STRM_CTRL);
1696
1697 /* Read from data stream */
1698 res = pr_register_netio(netio, PR_NETIO_STRM_DATA);
1699 ck_assert_msg(res == 0, "Failed to register custom data NetIO: %s",
1700 strerror(errno));
1701
1702 netio2 = pr_get_netio(PR_NETIO_STRM_DATA);
1703 ck_assert_msg(netio2 != NULL, "Failed to get custom data NetIO: %s",
1704 strerror(errno));
1705 ck_assert_msg(netio2 == netio, "Expected custom data NetIO %p, got %p",
1706 netio, netio2);
1707
1708 res = netio_read_from_stream(PR_NETIO_STRM_DATA);
1709 ck_assert_msg(res == 0, "Failed to read from custom data NetIO: %s",
1710 strerror(errno));
1711
1712 mark_point();
1713 pr_unregister_netio(PR_NETIO_STRM_DATA);
1714
1715 /* Read from other stream */
1716 res = pr_register_netio(netio, PR_NETIO_STRM_OTHR);
1717 ck_assert_msg(res == 0, "Failed to register custom other NetIO: %s",
1718 strerror(errno));
1719
1720 netio2 = pr_get_netio(PR_NETIO_STRM_OTHR);
1721 ck_assert_msg(netio2 != NULL, "Failed to get custom othr NetIO: %s",
1722 strerror(errno));
1723 ck_assert_msg(netio2 == netio, "Expected custom othr NetIO %p, got %p",
1724 netio, netio2);
1725
1726 res = netio_read_from_stream(PR_NETIO_STRM_OTHR);
1727 ck_assert_msg(res == 0, "Failed to read from custom other NetIO: %s",
1728 strerror(errno));
1729
1730 mark_point();
1731 pr_unregister_netio(PR_NETIO_STRM_OTHR);
1732 }
1733 END_TEST
1734
1735 START_TEST (netio_gets_test) {
1736 int fd = 2, res;
1737 char *buf, *expected, *text;
1738 size_t buflen;
1739 pr_netio_t *netio;
1740 pr_netio_stream_t *nstrm;
1741
1742 text = pr_netio_gets(NULL, 0, NULL);
1743 ck_assert_msg(text == NULL, "Failed to handle null arguments");
1744 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1745 strerror(errno), errno);
1746
1747 netio = pr_alloc_netio2(p, NULL, "testsuite");
1748 netio->poll = netio_poll_cb;
1749 netio->read = netio_read_cb;
1750
1751 res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
1752 ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
1753 strerror(errno));
1754
1755 nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
1756 ck_assert_msg(nstrm != NULL, "Failed to open stream: %s", strerror(errno));
1757
1758 text = pr_netio_gets(NULL, 0, nstrm);
1759 ck_assert_msg(text == NULL, "Failed to handle null buffer");
1760 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1761 strerror(errno), errno);
1762
1763 buflen = 1024;
1764 buf = pcalloc(p, buflen);
1765
1766 text = pr_netio_gets(buf, 0, nstrm);
1767 ck_assert_msg(text == NULL, "Failed to handle zero buffer length");
1768 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1769 strerror(errno), errno);
1770
1771 expected = "Hello, World!\r\n";
1772 text = pr_netio_gets(buf, buflen-1, nstrm);
1773 ck_assert_msg(text != NULL, "Failed to get text: %s", strerror(errno));
1774 ck_assert_msg(strcmp(text, expected) == 0, "Expected '%s', got '%s'",
1775 expected, text);
1776
1777 mark_point();
1778 pr_unregister_netio(PR_NETIO_STRM_CTRL);
1779 }
1780 END_TEST
1781
1782 START_TEST (netio_write_test) {
1783 int res;
1784 pr_netio_t *netio, *netio2;
1785 pr_netio_stream_t *nstrm;
1786 char *buf;
1787
1788 mark_point();
1789 res = pr_netio_write(NULL, NULL, 0);
1790 ck_assert_msg(res < 0, "Failed to handle null nstrm");
1791 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1792 strerror(errno), errno);
1793
1794 mark_point();
1795 nstrm = pcalloc(p, sizeof(pr_netio_stream_t));
1796 res = pr_netio_write(nstrm, NULL, 0);
1797 ck_assert_msg(res < 0, "Failed to handle null buf");
1798 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1799 strerror(errno), errno);
1800
1801 mark_point();
1802 buf = "foo";
1803 res = pr_netio_write(nstrm, buf, 0);
1804 ck_assert_msg(res < 0, "Failed to handle zero buflen");
1805 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1806 strerror(errno), errno);
1807
1808 mark_point();
1809 nstrm->strm_fd = -34;
1810 res = pr_netio_write(nstrm, buf, 3);
1811 ck_assert_msg(res < 0, "Failed to handle bad nstrm fd");
1812 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
1813 strerror(errno), errno);
1814
1815 netio = pr_alloc_netio2(p, NULL, "testsuite");
1816 netio->poll = netio_poll_cb;
1817 netio->write = netio_write_cb;
1818
1819 /* Write to control stream */
1820 res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
1821 ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
1822 strerror(errno));
1823
1824 netio2 = pr_get_netio(PR_NETIO_STRM_CTRL);
1825 ck_assert_msg(netio2 != NULL, "Failed to get custom ctrl NetIO: %s",
1826 strerror(errno));
1827 ck_assert_msg(netio2 == netio, "Expected custom ctrl NetIO %p, got %p",
1828 netio, netio2);
1829
1830 res = netio_write_to_stream(PR_NETIO_STRM_CTRL, FALSE);
1831 ck_assert_msg(res == 0, "Failed to write to custom ctrl NetIO: %s",
1832 strerror(errno));
1833
1834 mark_point();
1835 pr_unregister_netio(PR_NETIO_STRM_CTRL);
1836
1837 /* Write to data stream */
1838 res = pr_register_netio(netio, PR_NETIO_STRM_DATA);
1839 ck_assert_msg(res == 0, "Failed to register custom data NetIO: %s",
1840 strerror(errno));
1841
1842 netio2 = pr_get_netio(PR_NETIO_STRM_DATA);
1843 ck_assert_msg(netio2 != NULL, "Failed to get custom data NetIO: %s",
1844 strerror(errno));
1845 ck_assert_msg(netio2 == netio, "Expected custom data NetIO %p, got %p",
1846 netio, netio2);
1847
1848 res = netio_write_to_stream(PR_NETIO_STRM_DATA, FALSE);
1849 ck_assert_msg(res == 0, "Failed to write to custom data NetIO: %s",
1850 strerror(errno));
1851
1852 mark_point();
1853 pr_unregister_netio(PR_NETIO_STRM_DATA);
1854
1855 /* Write to other stream */
1856 res = pr_register_netio(netio, PR_NETIO_STRM_OTHR);
1857 ck_assert_msg(res == 0, "Failed to register custom other NetIO: %s",
1858 strerror(errno));
1859
1860 netio2 = pr_get_netio(PR_NETIO_STRM_OTHR);
1861 ck_assert_msg(netio2 != NULL, "Failed to get custom othr NetIO: %s",
1862 strerror(errno));
1863 ck_assert_msg(netio2 == netio, "Expected custom othr NetIO %p, got %p",
1864 netio, netio2);
1865
1866 res = netio_write_to_stream(PR_NETIO_STRM_OTHR, FALSE);
1867 ck_assert_msg(res == 0, "Failed to write to custom other NetIO: %s",
1868 strerror(errno));
1869
1870 mark_point();
1871 pr_unregister_netio(PR_NETIO_STRM_OTHR);
1872 }
1873 END_TEST
1874
1875 START_TEST (netio_write_async_test) {
1876 int res;
1877 pr_netio_t *netio;
1878 pr_netio_stream_t *nstrm;
1879
1880 mark_point();
1881 res = pr_netio_write_async(NULL, NULL, 0);
1882 ck_assert_msg(res < 0, "Failed to handle null nstrm");
1883 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1884 strerror(errno), errno);
1885
1886 mark_point();
1887 nstrm = pcalloc(p, sizeof(pr_netio_stream_t));
1888 nstrm->strm_fd = -1;
1889 res = pr_netio_write_async(nstrm, NULL, 0);
1890 ck_assert_msg(res < 0, "Failed to handle bad nstrm fd");
1891 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
1892 strerror(errno), errno);
1893
1894 netio = pr_alloc_netio2(p, NULL, "testsuite");
1895 netio->poll = netio_poll_cb;
1896 netio->write = netio_write_cb;
1897
1898 /* ctrl */
1899 res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
1900 ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
1901 strerror(errno));
1902
1903 mark_point();
1904 res = netio_write_to_stream(PR_NETIO_STRM_CTRL, TRUE);
1905 ck_assert_msg(res == 0, "Failed to write to custom ctrl NetIO: %s",
1906 strerror(errno));
1907
1908 mark_point();
1909 pr_unregister_netio(PR_NETIO_STRM_CTRL);
1910
1911 /* data */
1912 res = pr_register_netio(netio, PR_NETIO_STRM_DATA);
1913 ck_assert_msg(res == 0, "Failed to register custom data NetIO: %s",
1914 strerror(errno));
1915
1916 mark_point();
1917 res = netio_write_to_stream(PR_NETIO_STRM_DATA, TRUE);
1918 ck_assert_msg(res == 0, "Failed to write to custom data NetIO: %s",
1919 strerror(errno));
1920
1921 mark_point();
1922 pr_unregister_netio(PR_NETIO_STRM_DATA);
1923
1924 /* othr */
1925 res = pr_register_netio(netio, PR_NETIO_STRM_OTHR);
1926 ck_assert_msg(res == 0, "Failed to register custom othr NetIO: %s",
1927 strerror(errno));
1928
1929 mark_point();
1930 res = netio_write_to_stream(PR_NETIO_STRM_OTHR, TRUE);
1931 ck_assert_msg(res == 0, "Failed to write to custom othr NetIO: %s",
1932 strerror(errno));
1933
1934 mark_point();
1935 pr_unregister_netio(PR_NETIO_STRM_OTHR);
1936 }
1937 END_TEST
1938
1939 static int netio_print_to_stream(int strm_type, int use_async) {
1940 int fd = 2, res;
1941 char *buf;
1942 size_t buflen;
1943 pr_netio_stream_t *nstrm;
1944
1945 nstrm = pr_netio_open(p, strm_type, fd, PR_NETIO_IO_WR);
1946 if (nstrm == NULL) {
1947 int xerrno = errno;
1948
1949 pr_trace_msg("netio", 1, "error opening custom netio stream: %s",
1950 strerror(xerrno));
1951 errno = xerrno;
1952 return -1;
1953 }
1954
1955 buf = "Hello, World!\n";
1956 buflen = strlen(buf);
1957
1958 if (use_async) {
1959 res = pr_netio_printf_async(nstrm, "%s", buf);
1960
1961 } else {
1962 res = pr_netio_printf(nstrm, "%s", buf);
1963 }
1964
1965 if ((size_t) res != buflen) {
1966 pr_trace_msg("netio", 1, "printed buffer (%lu bytes), got %d",
1967 (unsigned long) buflen, res);
1968 pr_netio_close(nstrm);
1969
1970 if (res < 0) {
1971 return -1;
1972 }
1973
1974 errno = EIO;
1975 return -1;
1976 }
1977
1978 mark_point();
1979 pr_netio_close(nstrm);
1980 return 0;
1981 }
1982
1983 START_TEST (netio_printf_test) {
1984 int res;
1985 pr_netio_t *netio;
1986
1987 netio = pr_alloc_netio2(p, NULL, "testsuite");
1988 netio->poll = netio_poll_cb;
1989 netio->write = netio_write_cb;
1990 netio->close = netio_close_cb;
1991
1992 mark_point();
1993 res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
1994 ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
1995 strerror(errno));
1996
1997 mark_point();
1998 res = netio_print_to_stream(PR_NETIO_STRM_CTRL, FALSE);
1999 ck_assert_msg(res == 0, "Failed to print to custom ctrl NetIO: %s",
2000 strerror(errno));
2001
2002 mark_point();
2003 pr_unregister_netio(PR_NETIO_STRM_CTRL);
2004 }
2005 END_TEST
2006
2007 START_TEST (netio_printf_async_test) {
2008 int res;
2009 pr_netio_t *netio;
2010
2011 netio = pr_alloc_netio2(p, NULL, "testsuite");
2012 netio->poll = netio_poll_cb;
2013 netio->write = netio_write_cb;
2014 netio->close = netio_close_cb;
2015
2016 mark_point();
2017 res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
2018 ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
2019 strerror(errno));
2020
2021 mark_point();
2022 res = netio_print_to_stream(PR_NETIO_STRM_CTRL, TRUE);
2023 ck_assert_msg(res == 0, "Failed to print to custom ctrl NetIO: %s",
2024 strerror(errno));
2025
2026 mark_point();
2027 pr_unregister_netio(PR_NETIO_STRM_CTRL);
2028 }
2029 END_TEST
2030
2031 START_TEST (netio_abort_test) {
2032 pr_netio_stream_t *nstrm;
2033 int fd = -1;
2034
2035 mark_point();
2036 pr_netio_abort(NULL);
2037
2038 /* open/abort/close CTRL stream */
2039 nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
2040 ck_assert_msg(nstrm != NULL, "Failed to open ctrl stream on fd %d: %s", fd,
2041 strerror(errno));
2042
2043 pr_netio_abort(nstrm);
2044 ck_assert_msg(nstrm->strm_flags & PR_NETIO_SESS_ABORT,
2045 "Failed to set PR_NETIO_SESS_ABORT flags on ctrl stream");
2046
2047 pr_netio_close(nstrm);
2048
2049 /* open/abort/close DATA stream */
2050 nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_WR);
2051 ck_assert_msg(nstrm != NULL, "Failed to open data stream on fd %d: %s", fd,
2052 strerror(errno));
2053
2054 pr_netio_abort(nstrm);
2055 ck_assert_msg(nstrm->strm_flags & PR_NETIO_SESS_ABORT,
2056 "Failed to set PR_NETIO_SESS_ABORT flags on data stream");
2057
2058 pr_netio_close(nstrm);
2059
2060 /* open/abort/close OTHR stream */
2061 nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_WR);
2062 ck_assert_msg(nstrm != NULL, "Failed to open othr stream on fd %d: %s", fd,
2063 strerror(errno));
2064
2065 pr_netio_abort(nstrm);
2066 ck_assert_msg(nstrm->strm_flags & PR_NETIO_SESS_ABORT,
2067 "Failed to set PR_NETIO_SESS_ABORT flags on othr stream");
2068
2069 pr_netio_close(nstrm);
2070 }
2071 END_TEST
2072
2073 START_TEST (netio_lingering_abort_test) {
2074 pr_netio_t *netio;
2075 pr_netio_stream_t *nstrm;
2076 int fd = 0, res;
2077 long linger = 0L;
2078
2079 mark_point();
2080 res = pr_netio_lingering_abort(NULL, linger);
2081 ck_assert_msg(res < 0, "Failed to handle null nstrm");
2082 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2083 strerror(errno), errno);
2084
2085 mark_point();
2086 nstrm = pcalloc(p, sizeof(pr_netio_stream_t));
2087 nstrm->strm_type = 0;
2088 res = pr_netio_lingering_abort(nstrm, linger);
2089 ck_assert_msg(res < 0, "Failed to handle invalid nstrm type");
2090 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2091 strerror(errno), errno);
2092
2093 netio = pr_alloc_netio2(p, NULL, "testsuite");
2094 netio->close = netio_close_cb;
2095
2096 /* open/abort/close CTRL stream */
2097 res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
2098 ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
2099 strerror(errno));
2100
2101 nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
2102 ck_assert_msg(nstrm != NULL, "Failed to open ctrl stream on fd %d: %s", fd,
2103 strerror(errno));
2104
2105 res = pr_netio_lingering_abort(nstrm, linger);
2106 ck_assert_msg(res == 0, "Failed to set lingering abort on ctrl stream: %s",
2107 strerror(errno));
2108
2109 ck_assert_msg(nstrm->strm_flags & PR_NETIO_SESS_ABORT,
2110 "Failed to set PR_NETIO_SESS_ABORT flags on ctrl stream");
2111
2112 pr_netio_close(nstrm);
2113 pr_unregister_netio(PR_NETIO_STRM_CTRL);
2114
2115 /* open/abort/close DATA stream */
2116 res = pr_register_netio(netio, PR_NETIO_STRM_DATA);
2117 ck_assert_msg(res == 0, "Failed to register custom data NetIO: %s",
2118 strerror(errno));
2119
2120 nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_RD);
2121 ck_assert_msg(nstrm != NULL, "Failed to open data stream on fd %d: %s", fd,
2122 strerror(errno));
2123
2124 res = pr_netio_lingering_abort(nstrm, linger);
2125 ck_assert_msg(res == 0, "Failed to set lingering abort on data stream: %s",
2126 strerror(errno));
2127
2128 ck_assert_msg(nstrm->strm_flags & PR_NETIO_SESS_ABORT,
2129 "Failed to set PR_NETIO_SESS_ABORT flags on data stream");
2130
2131 pr_netio_close(nstrm);
2132 pr_unregister_netio(PR_NETIO_STRM_DATA);
2133
2134 /* open/abort/close OTHR stream */
2135 res = pr_register_netio(netio, PR_NETIO_STRM_OTHR);
2136 ck_assert_msg(res == 0, "Failed to register custom othr NetIO: %s",
2137 strerror(errno));
2138
2139 nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_RD);
2140 ck_assert_msg(nstrm != NULL, "Failed to open othr stream on fd %d: %s", fd,
2141 strerror(errno));
2142
2143 res = pr_netio_lingering_abort(nstrm, linger);
2144 ck_assert_msg(res == 0, "Failed to set lingering abort on othr stream: %s",
2145 strerror(errno));
2146
2147 ck_assert_msg(nstrm->strm_flags & PR_NETIO_SESS_ABORT,
2148 "Failed to set PR_NETIO_SESS_ABORT flags on othr stream");
2149
2150 pr_netio_close(nstrm);
2151 pr_unregister_netio(PR_NETIO_STRM_OTHR);
2152 }
2153 END_TEST
2154
2155 START_TEST (netio_poll_test) {
2156 int res;
2157 pr_netio_stream_t *nstrm;
2158
2159 mark_point();
2160 res = pr_netio_poll(NULL);
2161 ck_assert_msg(res < 0, "Failed to handle null nstrm");
2162 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2163 strerror(errno), errno);
2164
2165 mark_point();
2166 nstrm = pcalloc(p, sizeof(pr_netio_stream_t));
2167 nstrm->strm_fd = -3;
2168 res = pr_netio_poll(nstrm);
2169 ck_assert_msg(res < 0, "Failed to handle bad nstrm fd");
2170 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
2171 strerror(errno), errno);
2172
2173 mark_point();
2174 nstrm->strm_fd = fileno(stderr);
2175 nstrm->strm_flags |= PR_NETIO_SESS_ABORT;
2176 res = pr_netio_poll(nstrm);
2177 ck_assert_msg(res == 1, "Failed to handle SESS_ABORT flag");
2178
2179 mark_point();
2180 nstrm->strm_flags |= PR_NETIO_SESS_INTR;
2181 res = pr_netio_poll(nstrm);
2182 ck_assert_msg(res < 0, "Failed to handle SESS_INTR flag");
2183 ck_assert_msg(errno == EOF, "Expected EOF (%d), got %s (%d)", EOF,
2184 strerror(errno), errno);
2185
2186 mark_point();
2187 nstrm->strm_flags &= ~PR_NETIO_SESS_INTR;
2188 nstrm->strm_type = PR_NETIO_STRM_CTRL;
2189 res = pr_netio_poll(nstrm);
2190 ck_assert_msg(res == 0, "Failed to handle ctrl strm: %s", strerror(errno));
2191
2192 mark_point();
2193 nstrm->strm_type = PR_NETIO_STRM_DATA;
2194 res = pr_netio_poll(nstrm);
2195 ck_assert_msg(res == 0, "Failed to handle data strm: %s", strerror(errno));
2196
2197 mark_point();
2198 nstrm->strm_type = PR_NETIO_STRM_OTHR;
2199 res = pr_netio_poll(nstrm);
2200 ck_assert_msg(res == 0, "Failed to handle othr strm: %s", strerror(errno));
2201 }
2202 END_TEST
2203
2204 START_TEST (netio_poll_interval_test) {
2205 pr_netio_stream_t *nstrm;
2206 int fd = -1;
2207 unsigned int interval = 3;
2208
2209 mark_point();
2210 pr_netio_set_poll_interval(NULL, 0);
2211
2212 nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
2213 ck_assert_msg(nstrm != NULL, "Failed to open ctrl stream on fd %d: %s", fd,
2214 strerror(errno));
2215
2216 pr_netio_set_poll_interval(nstrm, interval);
2217 ck_assert_msg(nstrm->strm_interval == interval,
2218 "Expected stream interval %u, got %u", interval, nstrm->strm_interval);
2219 ck_assert_msg(nstrm->strm_flags & PR_NETIO_SESS_INTR,
2220 "Failed to set PR_NETIO_SESS_INTR stream flag");
2221
2222 mark_point();
2223 pr_netio_reset_poll_interval(NULL);
2224
2225 pr_netio_reset_poll_interval(nstrm);
2226 ck_assert_msg(!(nstrm->strm_flags & PR_NETIO_SESS_INTR),
2227 "Failed to clear PR_NETIO_SESS_INTR stream flag");
2228
2229 (void) pr_netio_close(nstrm);
2230 }
2231 END_TEST
2232
2233 static int netio_shutdown_cb(pr_netio_stream_t *nstrm, int how) {
2234 return 0;
2235 }
2236
2237 START_TEST (netio_shutdown_test) {
2238 pr_netio_t *netio;
2239 pr_netio_stream_t *nstrm;
2240 int fd = 0, how = SHUT_RD, res;
2241
2242 mark_point();
2243 res = pr_netio_shutdown(NULL, how);
2244 ck_assert_msg(res < 0, "Failed to handle null nstrm");
2245 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2246 strerror(errno), errno);
2247
2248 mark_point();
2249 nstrm = pcalloc(p, sizeof(pr_netio_stream_t));
2250 res = pr_netio_shutdown(nstrm, how);
2251 ck_assert_msg(res < 0, "Failed to handle invalid nstrm type");
2252 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2253 strerror(errno), errno);
2254
2255 netio = pr_alloc_netio2(p, NULL, "testsuite");
2256 netio->close = netio_close_cb;
2257 netio->shutdown = netio_shutdown_cb;
2258
2259 /* open/shutdown/close CTRL stream */
2260 res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
2261 ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
2262 strerror(errno));
2263
2264 nstrm = pr_netio_open(p, PR_NETIO_STRM_CTRL, fd, PR_NETIO_IO_RD);
2265 ck_assert_msg(nstrm != NULL, "Failed to open ctrl stream on fd %d: %s", fd,
2266 strerror(errno));
2267
2268 res = pr_netio_shutdown(nstrm, how);
2269 ck_assert_msg(res == 0, "Failed to shutdown ctrl stream: %s", strerror(errno));
2270
2271 pr_netio_close(nstrm);
2272 pr_unregister_netio(PR_NETIO_STRM_CTRL);
2273
2274 /* open/shutdown/close DATA stream */
2275 res = pr_register_netio(netio, PR_NETIO_STRM_DATA);
2276 ck_assert_msg(res == 0, "Failed to register custom data NetIO: %s",
2277 strerror(errno));
2278
2279 nstrm = pr_netio_open(p, PR_NETIO_STRM_DATA, fd, PR_NETIO_IO_RD);
2280 ck_assert_msg(nstrm != NULL, "Failed to open data stream on fd %d: %s", fd,
2281 strerror(errno));
2282
2283 res = pr_netio_shutdown(nstrm, how);
2284 ck_assert_msg(res == 0, "Failed to shutdown ctrl stream: %s", strerror(errno));
2285
2286 pr_netio_close(nstrm);
2287 pr_unregister_netio(PR_NETIO_STRM_DATA);
2288
2289 /* open/shutdown/close OTHR stream */
2290 res = pr_register_netio(netio, PR_NETIO_STRM_OTHR);
2291 ck_assert_msg(res == 0, "Failed to register custom othr NetIO: %s",
2292 strerror(errno));
2293
2294 nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, fd, PR_NETIO_IO_RD);
2295 ck_assert_msg(nstrm != NULL, "Failed to open othr stream on fd %d: %s", fd,
2296 strerror(errno));
2297
2298 res = pr_netio_shutdown(nstrm, how);
2299 ck_assert_msg(res == 0, "Failed to shutdown ctrl stream: %s", strerror(errno));
2300
2301 pr_netio_close(nstrm);
2302 pr_unregister_netio(PR_NETIO_STRM_OTHR);
2303 }
2304 END_TEST
2305
2306 START_TEST (netio_register_test) {
2307 int res;
2308 pr_netio_t *netio;
2309 void *cb;
2310
2311 netio = pr_alloc_netio(p);
2312
2313 /* abort */
2314 mark_point();
2315 cb = netio->abort;
2316 netio->abort = NULL;
2317 res = pr_register_netio(netio, 0);
2318 ck_assert_msg(res < 0, "Failed to handle null abort cb");
2319 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2320 strerror(errno), errno);
2321 netio->abort = cb;
2322
2323 /* close */
2324 mark_point();
2325 cb = netio->close;
2326 netio->close = NULL;
2327 res = pr_register_netio(netio, 0);
2328 ck_assert_msg(res < 0, "Failed to handle null close cb");
2329 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2330 strerror(errno), errno);
2331 netio->close = cb;
2332
2333 /* open */
2334 mark_point();
2335 cb = netio->open;
2336 netio->open = NULL;
2337 res = pr_register_netio(netio, 0);
2338 ck_assert_msg(res < 0, "Failed to handle null open cb");
2339 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2340 strerror(errno), errno);
2341 netio->open = cb;
2342
2343 /* poll */
2344 mark_point();
2345 cb = netio->poll;
2346 netio->poll = NULL;
2347 res = pr_register_netio(netio, 0);
2348 ck_assert_msg(res < 0, "Failed to handle null poll cb");
2349 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2350 strerror(errno), errno);
2351 netio->poll = cb;
2352
2353 /* postopen */
2354 mark_point();
2355 cb = netio->postopen;
2356 netio->postopen = NULL;
2357 res = pr_register_netio(netio, 0);
2358 ck_assert_msg(res < 0, "Failed to handle null postopen cb");
2359 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2360 strerror(errno), errno);
2361 netio->postopen = cb;
2362
2363 /* read */
2364 mark_point();
2365 cb = netio->read;
2366 netio->read = NULL;
2367 res = pr_register_netio(netio, 0);
2368 ck_assert_msg(res < 0, "Failed to handle null read cb");
2369 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2370 strerror(errno), errno);
2371 netio->read = cb;
2372
2373 /* reopen */
2374 mark_point();
2375 cb = netio->reopen;
2376 netio->reopen = NULL;
2377 res = pr_register_netio(netio, 0);
2378 ck_assert_msg(res < 0, "Failed to handle null reopen cb");
2379 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2380 strerror(errno), errno);
2381 netio->reopen = cb;
2382
2383 /* shutdown */
2384 mark_point();
2385 cb = netio->shutdown;
2386 netio->shutdown = NULL;
2387 res = pr_register_netio(netio, 0);
2388 ck_assert_msg(res < 0, "Failed to handle null shutdown cb");
2389 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2390 strerror(errno), errno);
2391 netio->shutdown = cb;
2392
2393 /* write */
2394 mark_point();
2395 cb = netio->write;
2396 netio->write = NULL;
2397 res = pr_register_netio(netio, 0);
2398 ck_assert_msg(res < 0, "Failed to handle null write cb");
2399 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2400 strerror(errno), errno);
2401 netio->write = cb;
2402 }
2403 END_TEST
2404
2405 START_TEST (netio_unregister_test) {
2406 int res;
2407
2408 mark_point();
2409 res = pr_unregister_netio(0);
2410 ck_assert_msg(res < 0, "Failed to handle invalid types");
2411 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2412 strerror(errno), errno);
2413
2414 mark_point();
2415 res = pr_unregister_netio(10000);
2416 ck_assert_msg(res == 0, "Failed to handle invalid types");
2417 }
2418 END_TEST
2419
2420 START_TEST (netio_get_test) {
2421 pr_netio_t *netio;
2422
2423 mark_point();
2424 netio = pr_get_netio(0);
2425 ck_assert_msg(netio == NULL, "Failed to handle zero type");
2426 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2427 strerror(errno), errno);
2428
2429 mark_point();
2430 netio = pr_get_netio(1000);
2431 ck_assert_msg(netio == NULL, "Failed to handle invalid type");
2432 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
2433 strerror(errno), errno);
2434 }
2435 END_TEST
2436
2437 START_TEST (netio_alloc_test) {
2438 pr_netio_t *netio;
2439
2440 mark_point();
2441 netio = pr_alloc_netio2(NULL, NULL, NULL);
2442 ck_assert_msg(netio == NULL, "Failed to handle null pool");
2443 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2444 strerror(errno), errno);
2445
2446 mark_point();
2447 netio = pr_alloc_netio(NULL);
2448 ck_assert_msg(netio == NULL, "Failed to handle null pool");
2449 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2450 strerror(errno), errno);
2451 }
2452 END_TEST
2453
2454 Suite *tests_get_netio_suite(void) {
2455 Suite *suite;
2456 TCase *testcase;
2457
2458 suite = suite_create("netio");
2459
2460 testcase = tcase_create("base");
2461 tcase_add_checked_fixture(testcase, set_up, tear_down);
2462
2463 tcase_add_test(testcase, netio_open_test);
2464 tcase_add_test(testcase, netio_postopen_test);
2465 tcase_add_test(testcase, netio_close_test);
2466 tcase_add_test(testcase, netio_lingering_close_test);
2467 tcase_add_test(testcase, netio_reopen_test);
2468 tcase_add_test(testcase, netio_buffer_alloc_test);
2469
2470 tcase_add_test(testcase, netio_telnet_gets_args_test);
2471 tcase_add_test(testcase, netio_telnet_gets_single_line_test);
2472 tcase_add_test(testcase, netio_telnet_gets_multi_line_test);
2473 tcase_add_test(testcase, netio_telnet_gets_no_newline_test);
2474 tcase_add_test(testcase, netio_telnet_gets_telnet_will_test);
2475 tcase_add_test(testcase, netio_telnet_gets_telnet_bare_will_test);
2476 tcase_add_test(testcase, netio_telnet_gets_telnet_will_multi_read_test);
2477 tcase_add_test(testcase, netio_telnet_gets_telnet_wont_test);
2478 tcase_add_test(testcase, netio_telnet_gets_telnet_bare_wont_test);
2479 tcase_add_test(testcase, netio_telnet_gets_telnet_do_test);
2480 tcase_add_test(testcase, netio_telnet_gets_telnet_bare_do_test);
2481 tcase_add_test(testcase, netio_telnet_gets_telnet_dont_test);
2482 tcase_add_test(testcase, netio_telnet_gets_telnet_bare_dont_test);
2483 tcase_add_test(testcase, netio_telnet_gets_telnet_ip_test);
2484 tcase_add_test(testcase, netio_telnet_gets_telnet_bare_ip_test);
2485 tcase_add_test(testcase, netio_telnet_gets_telnet_dm_test);
2486 tcase_add_test(testcase, netio_telnet_gets_telnet_bare_dm_test);
2487 tcase_add_test(testcase, netio_telnet_gets_telnet_single_iac_test);
2488 tcase_add_test(testcase, netio_telnet_gets_bug3521_test);
2489 tcase_add_test(testcase, netio_telnet_gets_bug3697_test);
2490 tcase_add_test(testcase, netio_telnet_gets_eof_test);
2491 tcase_add_test(testcase, netio_telnet_gets_random_data_test);
2492
2493 tcase_add_test(testcase, netio_telnet_gets2_single_line_test);
2494 tcase_add_test(testcase, netio_telnet_gets2_single_line_crnul_test);
2495 tcase_add_test(testcase, netio_telnet_gets2_single_line_lf_test);
2496 tcase_add_test(testcase, netio_telnet_gets2_random_data_test);
2497
2498 tcase_add_test(testcase, netio_read_test);
2499 tcase_add_test(testcase, netio_gets_test);
2500 tcase_add_test(testcase, netio_write_test);
2501 tcase_add_test(testcase, netio_write_async_test);
2502 tcase_add_test(testcase, netio_printf_test);
2503 tcase_add_test(testcase, netio_printf_async_test);
2504 tcase_add_test(testcase, netio_abort_test);
2505 tcase_add_test(testcase, netio_lingering_abort_test);
2506 tcase_add_test(testcase, netio_poll_test);
2507 tcase_add_test(testcase, netio_poll_interval_test);
2508 tcase_add_test(testcase, netio_shutdown_test);
2509
2510 tcase_add_test(testcase, netio_register_test);
2511 tcase_add_test(testcase, netio_unregister_test);
2512 tcase_add_test(testcase, netio_get_test);
2513 tcase_add_test(testcase, netio_alloc_test);
2514
2515 suite_add_tcase(suite, testcase);
2516 return suite;
2517 }