2 * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
3 * Copyright (c) 2021 Dell EMC
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/param.h>
29 #include <sys/types.h>
43 /* The assertions depend on this string. */
44 char apothegm[] = "All work and no play\0 makes Jack a dull boy.\n";
47 * This is a neurotic reader function designed to give getdelim() a
48 * hard time. It reads through the string `apothegm' and returns a
49 * random number of bytes up to the requested length.
52 _reader(void *cookie, char *buf, int len)
54 size_t *offp = cookie;
57 r = random() % CHUNK_MAX + 1;
60 if (len > sizeof(apothegm) - *offp)
61 len = sizeof(apothegm) - *offp;
62 memcpy(buf, apothegm + *offp, len);
72 offp = malloc(sizeof(*offp)); /* XXX leak */
74 return (fropen(offp, _reader));
77 ATF_TC_WITHOUT_HEAD(getline_basic);
78 ATF_TC_BODY(getline_basic, tc)
88 * Test multiple times with different buffer sizes
89 * and different _reader() return values.
92 for (i = 0; i < 8; i++) {
96 /* First line: the full apothegm */
97 ATF_REQUIRE(getline(&line, &linecap, fp) == sizeof(apothegm) - 1);
98 ATF_REQUIRE(memcmp(line, apothegm, sizeof(apothegm)) == 0);
99 ATF_REQUIRE(linecap >= sizeof(apothegm));
100 /* Second line: the NUL terminator following the newline */
101 ATF_REQUIRE(getline(&line, &linecap, fp) == 1);
102 ATF_REQUIRE(line[0] == '\0' && line[1] == '\0');
103 /* Third line: EOF */
105 ATF_REQUIRE(getline(&line, &linecap, fp) == -1);
106 ATF_REQUIRE(line[0] == '\0');
109 ATF_REQUIRE(feof(fp));
110 ATF_REQUIRE(!ferror(fp));
113 ATF_REQUIRE(errno == 0);
116 ATF_TC_WITHOUT_HEAD(stream_error);
117 ATF_TC_BODY(stream_error, tc)
122 /* Make sure read errors are handled properly. */
126 ATF_REQUIRE(getline(&line, &linecap, stdout) == -1);
127 ATF_REQUIRE(errno == EBADF);
129 ATF_REQUIRE(getdelim(&line, &linecap, 'X', stdout) == -1);
130 ATF_REQUIRE(errno == EBADF);
131 ATF_REQUIRE(ferror(stdout));
134 ATF_TC_WITHOUT_HEAD(invalid_params);
135 ATF_TC_BODY(invalid_params, tc)
141 /* Make sure NULL linep or linecapp pointers are handled. */
143 ATF_REQUIRE(getline(NULL, &linecap, fp) == -1);
144 ATF_REQUIRE(errno == EINVAL);
145 ATF_REQUIRE(getline(&line, NULL, fp) == -1);
146 ATF_REQUIRE(errno == EINVAL);
147 ATF_REQUIRE(ferror(fp));
151 ATF_TC_WITHOUT_HEAD(eof);
158 /* Make sure getline() allocates memory as needed if fp is at EOF. */
161 while (!feof(fp)) /* advance to EOF; can't fseek this stream */
166 ATF_REQUIRE(getline(&line, &linecap, fp) == -1);
167 ATF_REQUIRE(line[0] == '\0');
168 ATF_REQUIRE(linecap > 0);
169 ATF_REQUIRE(errno == 0);
171 ATF_REQUIRE(feof(fp));
172 ATF_REQUIRE(!ferror(fp));
176 ATF_TC_WITHOUT_HEAD(nul);
186 /* Make sure a NUL delimiter works. */
188 n = strlen(apothegm);
189 printf("getdelim\n");
190 ATF_REQUIRE(getdelim(&line, &linecap, '\0', fp) == n + 1);
191 ATF_REQUIRE(strcmp(line, apothegm) == 0);
192 ATF_REQUIRE(line[n + 1] == '\0');
193 ATF_REQUIRE(linecap > n + 1);
194 n = strlen(apothegm + n + 1);
195 printf("getdelim 2\n");
196 ATF_REQUIRE(getdelim(&line, &linecap, '\0', fp) == n + 1);
197 ATF_REQUIRE(line[n + 1] == '\0');
198 ATF_REQUIRE(linecap > n + 1);
199 ATF_REQUIRE(errno == 0);
200 ATF_REQUIRE(!ferror(fp));
204 ATF_TC_WITHOUT_HEAD(empty_NULL_buffer);
205 ATF_TC_BODY(empty_NULL_buffer, tc)
211 /* Make sure NULL *linep and zero *linecapp are handled. */
215 ATF_REQUIRE(getline(&line, &linecap, fp) == sizeof(apothegm) - 1);
216 ATF_REQUIRE(memcmp(line, apothegm, sizeof(apothegm)) == 0);
221 ATF_REQUIRE(getline(&line, &linecap, fp) == sizeof(apothegm) - 1);
222 ATF_REQUIRE(memcmp(line, apothegm, sizeof(apothegm)) == 0);
224 ATF_REQUIRE(!ferror(fp));
229 _ipc_read(int fd, char wait_c)
235 while (c != wait_c) {
236 len = read(fd, &c, 1);
237 ATF_CHECK_MSG(len != 0,
238 "EOF on IPC pipe while waiting. Did other side fail?");
239 ATF_CHECK_MSG(len == 1 || errno == EINTR,
240 "read %zu bytes errno %d\n", len, errno);
241 if (len != 1 || errno != EINTR)
247 _ipc_write(int fd, char c)
250 while ((write(fd, &c, 1) != 1))
251 ATF_REQUIRE(errno == EINTR);
255 ipc_wait(int ipcfd[2])
258 _ipc_read(ipcfd[0], '+');
260 _ipc_write(ipcfd[1], '-');
264 ipc_wakeup(int ipcfd[2])
267 _ipc_write(ipcfd[1], '+');
269 _ipc_read(ipcfd[0], '-');
273 _nonblock_eagain(int buf_mode)
276 const char delim = '!';
277 const char *strs[] = {
278 "first line partial!",
279 "second line is sent in full!",
280 "third line is sent partially!",
281 "last line is sent in full!",
284 size_t linecap, strslen[nitems(strs)];
286 int fd_fifo, flags, i, ipcfd[2], pipedes[2], pipedes2[2], status;
291 for (i = 0; i < nitems(strslen); i++)
292 strslen[i] = strlen(strs[i]);
293 ATF_REQUIRE(pipe2(pipedes, O_CLOEXEC) == 0);
294 ATF_REQUIRE(pipe2(pipedes2, O_CLOEXEC) == 0);
296 (void)unlink("fifo");
297 ATF_REQUIRE(mkfifo("fifo", 0666) == 0);
298 ATF_REQUIRE((pid = fork()) >= 0);
301 ipcfd[1] = pipedes[1];
302 ipcfd[0] = pipedes2[0];
305 ATF_REQUIRE((fd_fifo = open("fifo", O_WRONLY)) != -1);
308 ATF_REQUIRE(write(fd_fifo, strs[0], strslen[0] - 3) ==
313 /* Finish off the first line. */
314 ATF_REQUIRE(write(fd_fifo,
315 &(strs[0][strslen[0] - 3]), 3) == 3);
316 /* And include the second full line and a partial 3rd line. */
317 ATF_REQUIRE(write(fd_fifo, strs[1], strslen[1]) == strslen[1]);
318 ATF_REQUIRE(write(fd_fifo, strs[2], strslen[2] - 3) ==
323 /* Finish the partial write and partially send the last. */
324 ATF_REQUIRE(write(fd_fifo,
325 &(strs[2][strslen[2] - 3]), 3) == 3);
326 ATF_REQUIRE(write(fd_fifo, strs[3], strslen[3] - 3) ==
331 /* Finish the write */
332 ATF_REQUIRE(write(fd_fifo,
333 &(strs[3][strslen[3] - 3]), 3) == 3);
337 ipcfd[0] = pipedes[0];
340 ipcfd[1] = pipedes2[1];
342 ATF_REQUIRE((fp = fopen("fifo", "r")) != NULL);
343 setvbuf(fp, (char *)NULL, buf_mode, 0);
344 ATF_REQUIRE((flags = fcntl(fileno(fp), F_GETFL, 0)) != -1);
345 ATF_REQUIRE(fcntl(fileno(fp), F_SETFL, flags | O_NONBLOCK) >= 0);
347 /* Wait until the writer completes its partial write. */
349 ATF_REQUIRE_ERRNO(EAGAIN,
350 (linelen = getdelim(&line, &linecap, delim, fp)) == -1);
351 ATF_REQUIRE_STREQ("", line);
352 ATF_REQUIRE(ferror(fp));
353 ATF_REQUIRE(!feof(fp));
359 * Should now have the finished first line, a full second line,
360 * and a partial third line.
362 ATF_CHECK(getdelim(&line, &linecap, delim, fp) == strslen[0]);
363 ATF_REQUIRE_STREQ(strs[0], line);
364 ATF_REQUIRE(getdelim(&line, &linecap, delim, fp) == strslen[1]);
365 ATF_REQUIRE_STREQ(strs[1], line);
367 ATF_REQUIRE_ERRNO(EAGAIN,
368 (linelen = getdelim(&line, &linecap, delim, fp)) == -1);
369 ATF_REQUIRE_STREQ("", line);
370 ATF_REQUIRE(ferror(fp));
371 ATF_REQUIRE(!feof(fp));
375 /* Wait for the partial write to be completed and another to be done. */
377 ATF_REQUIRE((linelen = getdelim(&line, &linecap, delim, fp)) != -1);
378 ATF_REQUIRE(!ferror(fp));
379 ATF_REQUIRE(!feof(fp));
380 ATF_REQUIRE_STREQ(strs[2], line);
381 ATF_REQUIRE(linelen == strslen[2]);
383 ATF_REQUIRE_ERRNO(EAGAIN,
384 (linelen = getdelim(&line, &linecap, delim, fp)) == -1);
385 ATF_REQUIRE_STREQ("", line);
386 ATF_REQUIRE(ferror(fp));
387 ATF_REQUIRE(!feof(fp));
392 ATF_REQUIRE((linelen = getdelim(&line, &linecap, delim, fp)) != -1);
393 ATF_REQUIRE(!ferror(fp));
394 ATF_REQUIRE(!feof(fp));
395 ATF_REQUIRE_STREQ(strs[3], line);
396 ATF_REQUIRE(linelen == strslen[3]);
398 ATF_REQUIRE(waitpid(pid, &status, WEXITED) != -1);
399 ATF_REQUIRE(WIFEXITED(status));
400 ATF_REQUIRE(WEXITSTATUS(status) == 0);
403 ATF_TC_WITHOUT_HEAD(nonblock_eagain_buffered);
404 ATF_TC_BODY(nonblock_eagain_buffered, tc)
407 _nonblock_eagain(_IOFBF);
410 ATF_TC_WITHOUT_HEAD(nonblock_eagain_unbuffered);
411 ATF_TC_BODY(nonblock_eagain_unbuffered, tc)
414 _nonblock_eagain(_IONBF);
421 ATF_TP_ADD_TC(tp, getline_basic);
422 ATF_TP_ADD_TC(tp, stream_error);
423 ATF_TP_ADD_TC(tp, eof);
424 ATF_TP_ADD_TC(tp, invalid_params);
425 ATF_TP_ADD_TC(tp, nul);
426 ATF_TP_ADD_TC(tp, empty_NULL_buffer);
427 ATF_TP_ADD_TC(tp, nonblock_eagain_unbuffered);
428 ATF_TP_ADD_TC(tp, nonblock_eagain_buffered);
430 return (atf_no_error());