2 * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
39 /* The assertions depend on this string. */
40 char apothegm[] = "All work and no play\0 makes Jack a dull boy.\n";
43 * This is a neurotic reader function designed to give getdelim() a
44 * hard time. It reads through the string `apothegm' and returns a
45 * random number of bytes up to the requested length.
48 _reader(void *cookie, char *buf, int len)
50 size_t *offp = cookie;
53 r = random() % CHUNK_MAX + 1;
56 if (len > sizeof(apothegm) - *offp)
57 len = sizeof(apothegm) - *offp;
58 memcpy(buf, apothegm + *offp, len);
68 offp = malloc(sizeof(*offp)); /* XXX leak */
70 return (fropen(offp, _reader));
73 ATF_TC_WITHOUT_HEAD(getline_basic);
74 ATF_TC_BODY(getline_basic, tc)
84 * Test multiple times with different buffer sizes
85 * and different _reader() return values.
88 for (i = 0; i < 8; i++) {
92 /* First line: the full apothegm */
93 ATF_REQUIRE(getline(&line, &linecap, fp) == sizeof(apothegm) - 1);
94 ATF_REQUIRE(memcmp(line, apothegm, sizeof(apothegm)) == 0);
95 ATF_REQUIRE(linecap >= sizeof(apothegm));
96 /* Second line: the NUL terminator following the newline */
97 ATF_REQUIRE(getline(&line, &linecap, fp) == 1);
98 ATF_REQUIRE(line[0] == '\0' && line[1] == '\0');
101 ATF_REQUIRE(getline(&line, &linecap, fp) == -1);
102 ATF_REQUIRE(line[0] == '\0');
105 ATF_REQUIRE(feof(fp));
106 ATF_REQUIRE(!ferror(fp));
109 ATF_REQUIRE(errno == 0);
112 ATF_TC_WITHOUT_HEAD(stream_error);
113 ATF_TC_BODY(stream_error, tc)
118 /* Make sure read errors are handled properly. */
122 ATF_REQUIRE(getline(&line, &linecap, stdout) == -1);
123 ATF_REQUIRE(errno == EBADF);
125 ATF_REQUIRE(getdelim(&line, &linecap, 'X', stdout) == -1);
126 ATF_REQUIRE(errno == EBADF);
127 ATF_REQUIRE(ferror(stdout));
130 ATF_TC_WITHOUT_HEAD(invalid_params);
131 ATF_TC_BODY(invalid_params, tc)
137 /* Make sure NULL linep or linecapp pointers are handled. */
139 ATF_REQUIRE(getline(NULL, &linecap, fp) == -1);
140 ATF_REQUIRE(errno == EINVAL);
141 ATF_REQUIRE(getline(&line, NULL, fp) == -1);
142 ATF_REQUIRE(errno == EINVAL);
143 ATF_REQUIRE(ferror(fp));
147 ATF_TC_WITHOUT_HEAD(eof);
154 /* Make sure getline() allocates memory as needed if fp is at EOF. */
157 while (!feof(fp)) /* advance to EOF; can't fseek this stream */
162 ATF_REQUIRE(getline(&line, &linecap, fp) == -1);
163 ATF_REQUIRE(line[0] == '\0');
164 ATF_REQUIRE(linecap > 0);
165 ATF_REQUIRE(errno == 0);
167 ATF_REQUIRE(feof(fp));
168 ATF_REQUIRE(!ferror(fp));
172 ATF_TC_WITHOUT_HEAD(nul);
182 /* Make sure a NUL delimiter works. */
184 n = strlen(apothegm);
185 printf("getdelim\n");
186 ATF_REQUIRE(getdelim(&line, &linecap, '\0', fp) == n + 1);
187 ATF_REQUIRE(strcmp(line, apothegm) == 0);
188 ATF_REQUIRE(line[n + 1] == '\0');
189 ATF_REQUIRE(linecap > n + 1);
190 n = strlen(apothegm + n + 1);
191 printf("getdelim 2\n");
192 ATF_REQUIRE(getdelim(&line, &linecap, '\0', fp) == n + 1);
193 ATF_REQUIRE(line[n + 1] == '\0');
194 ATF_REQUIRE(linecap > n + 1);
195 ATF_REQUIRE(errno == 0);
196 ATF_REQUIRE(!ferror(fp));
200 ATF_TC_WITHOUT_HEAD(empty_NULL_buffer);
201 ATF_TC_BODY(empty_NULL_buffer, tc)
207 /* Make sure NULL *linep and zero *linecapp are handled. */
211 ATF_REQUIRE(getline(&line, &linecap, fp) == sizeof(apothegm) - 1);
212 ATF_REQUIRE(memcmp(line, apothegm, sizeof(apothegm)) == 0);
217 ATF_REQUIRE(getline(&line, &linecap, fp) == sizeof(apothegm) - 1);
218 ATF_REQUIRE(memcmp(line, apothegm, sizeof(apothegm)) == 0);
220 ATF_REQUIRE(!ferror(fp));
227 ATF_TP_ADD_TC(tp, getline_basic);
228 ATF_TP_ADD_TC(tp, stream_error);
229 ATF_TP_ADD_TC(tp, eof);
230 ATF_TP_ADD_TC(tp, invalid_params);
231 ATF_TP_ADD_TC(tp, nul);
232 ATF_TP_ADD_TC(tp, empty_NULL_buffer);
234 return (atf_no_error());