1189142Sdas/*-
2189142Sdas * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
3189142Sdas * All rights reserved.
4189142Sdas *
5189142Sdas * Redistribution and use in source and binary forms, with or without
6189142Sdas * modification, are permitted provided that the following conditions
7189142Sdas * are met:
8189142Sdas * 1. Redistributions of source code must retain the above copyright
9189142Sdas *    notice, this list of conditions and the following disclaimer.
10189142Sdas * 2. Redistributions in binary form must reproduce the above copyright
11189142Sdas *    notice, this list of conditions and the following disclaimer in the
12189142Sdas *    documentation and/or other materials provided with the distribution.
13189142Sdas *
14189142Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15189142Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16189142Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17189142Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18189142Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19189142Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20189142Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21189142Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22189142Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23189142Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24189142Sdas * SUCH DAMAGE.
25189142Sdas */
26189142Sdas
27189142Sdas#include <sys/cdefs.h>
28189142Sdas__FBSDID("$FreeBSD$");
29189142Sdas
30189142Sdas#define	_WITH_GETLINE
31189142Sdas#include <errno.h>
32189142Sdas#include <stdio.h>
33189142Sdas#include <stdlib.h>
34189142Sdas#include <string.h>
35189142Sdas
36290537Sngie#include <atf-c.h>
37290537Sngie
38189142Sdas#define	CHUNK_MAX	10
39189142Sdas
40189142Sdas/* The assertions depend on this string. */
41189142Sdaschar apothegm[] = "All work and no play\0 makes Jack a dull boy.\n";
42189142Sdas
43189142Sdas/*
44189142Sdas * This is a neurotic reader function designed to give getdelim() a
45189142Sdas * hard time. It reads through the string `apothegm' and returns a
46189142Sdas * random number of bytes up to the requested length.
47189142Sdas */
48189142Sdasstatic int
49189142Sdas_reader(void *cookie, char *buf, int len)
50189142Sdas{
51189142Sdas	size_t *offp = cookie;
52189142Sdas	size_t r;
53189142Sdas
54189142Sdas	r = random() % CHUNK_MAX + 1;
55189142Sdas	if (len > r)
56189142Sdas		len = r;
57189142Sdas	if (len > sizeof(apothegm) - *offp)
58189142Sdas		len = sizeof(apothegm) - *offp;
59189142Sdas	memcpy(buf, apothegm + *offp, len);
60189142Sdas	*offp += len;
61189142Sdas	return (len);
62189142Sdas}
63189142Sdas
64189142Sdasstatic FILE *
65189142Sdasmkfilebuf(void)
66189142Sdas{
67189142Sdas	size_t *offp;
68189142Sdas
69189142Sdas	offp = malloc(sizeof(*offp));	/* XXX leak */
70189142Sdas	*offp = 0;
71189142Sdas	return (fropen(offp, _reader));
72189142Sdas}
73189142Sdas
74290537SngieATF_TC_WITHOUT_HEAD(getline_basic);
75290537SngieATF_TC_BODY(getline_basic, tc)
76189142Sdas{
77189142Sdas	FILE *fp;
78189142Sdas	char *line;
79189142Sdas	size_t linecap;
80290537Sngie	int i;
81189142Sdas
82189142Sdas	srandom(0);
83189142Sdas
84189142Sdas	/*
85189142Sdas	 * Test multiple times with different buffer sizes
86189142Sdas	 * and different _reader() return values.
87189142Sdas	 */
88189142Sdas	errno = 0;
89189142Sdas	for (i = 0; i < 8; i++) {
90189142Sdas		fp = mkfilebuf();
91189142Sdas		linecap = i;
92189142Sdas		line = malloc(i);
93189142Sdas		/* First line: the full apothegm */
94290537Sngie		ATF_REQUIRE(getline(&line, &linecap, fp) == sizeof(apothegm) - 1);
95290537Sngie		ATF_REQUIRE(memcmp(line, apothegm, sizeof(apothegm)) == 0);
96290537Sngie		ATF_REQUIRE(linecap >= sizeof(apothegm));
97189142Sdas		/* Second line: the NUL terminator following the newline */
98290537Sngie		ATF_REQUIRE(getline(&line, &linecap, fp) == 1);
99290537Sngie		ATF_REQUIRE(line[0] == '\0' && line[1] == '\0');
100189142Sdas		/* Third line: EOF */
101189142Sdas		line[0] = 'X';
102290537Sngie		ATF_REQUIRE(getline(&line, &linecap, fp) == -1);
103290537Sngie		ATF_REQUIRE(line[0] == '\0');
104189142Sdas		free(line);
105197753Sdas		line = NULL;
106290537Sngie		ATF_REQUIRE(feof(fp));
107290537Sngie		ATF_REQUIRE(!ferror(fp));
108189142Sdas		fclose(fp);
109189142Sdas	}
110290537Sngie	ATF_REQUIRE(errno == 0);
111290537Sngie}
112189142Sdas
113290537SngieATF_TC_WITHOUT_HEAD(stream_error);
114290537SngieATF_TC_BODY(stream_error, tc)
115290537Sngie{
116290537Sngie	char *line;
117290537Sngie	size_t linecap;
118290537Sngie
119189142Sdas	/* Make sure read errors are handled properly. */
120290537Sngie	line = NULL;
121189142Sdas	linecap = 0;
122189142Sdas	errno = 0;
123290537Sngie	ATF_REQUIRE(getline(&line, &linecap, stdout) == -1);
124290537Sngie	ATF_REQUIRE(errno == EBADF);
125189142Sdas	errno = 0;
126290537Sngie	ATF_REQUIRE(getdelim(&line, &linecap, 'X', stdout) == -1);
127290537Sngie	ATF_REQUIRE(errno == EBADF);
128290537Sngie	ATF_REQUIRE(ferror(stdout));
129290537Sngie}
130189142Sdas
131290537SngieATF_TC_WITHOUT_HEAD(invalid_params);
132290537SngieATF_TC_BODY(invalid_params, tc)
133290537Sngie{
134290537Sngie	FILE *fp;
135290537Sngie	char *line;
136290537Sngie	size_t linecap;
137290537Sngie
138189142Sdas	/* Make sure NULL linep or linecapp pointers are handled. */
139189142Sdas	fp = mkfilebuf();
140290537Sngie	ATF_REQUIRE(getline(NULL, &linecap, fp) == -1);
141290537Sngie	ATF_REQUIRE(errno == EINVAL);
142290537Sngie	ATF_REQUIRE(getline(&line, NULL, fp) == -1);
143290537Sngie	ATF_REQUIRE(errno == EINVAL);
144290537Sngie	ATF_REQUIRE(ferror(fp));
145189142Sdas	fclose(fp);
146290537Sngie}
147189142Sdas
148290537SngieATF_TC_WITHOUT_HEAD(eof);
149290537SngieATF_TC_BODY(eof, tc)
150290537Sngie{
151290537Sngie	FILE *fp;
152290537Sngie	char *line;
153290537Sngie	size_t linecap;
154290537Sngie
155189142Sdas	/* Make sure getline() allocates memory as needed if fp is at EOF. */
156189142Sdas	errno = 0;
157189142Sdas	fp = mkfilebuf();
158189142Sdas	while (!feof(fp))	/* advance to EOF; can't fseek this stream */
159189142Sdas		getc(fp);
160189142Sdas	line = NULL;
161189142Sdas	linecap = 0;
162290537Sngie	printf("getline\n");
163290537Sngie	ATF_REQUIRE(getline(&line, &linecap, fp) == -1);
164290537Sngie	ATF_REQUIRE(line[0] == '\0');
165290537Sngie	ATF_REQUIRE(linecap > 0);
166290537Sngie	ATF_REQUIRE(errno == 0);
167290537Sngie	printf("feof\n");
168291840Sngie	errno = 0;
169290537Sngie	ATF_REQUIRE(feof(fp));
170290537Sngie	ATF_REQUIRE(!ferror(fp));
171189142Sdas	fclose(fp);
172290537Sngie}
173189142Sdas
174290537SngieATF_TC_WITHOUT_HEAD(nul);
175290537SngieATF_TC_BODY(nul, tc)
176290537Sngie{
177290537Sngie	FILE *fp;
178290537Sngie	char *line;
179290537Sngie	size_t linecap, n;
180290537Sngie
181291840Sngie	errno = 0;
182290537Sngie	line = NULL;
183290537Sngie	linecap = 0;
184189142Sdas	/* Make sure a NUL delimiter works. */
185189142Sdas	fp = mkfilebuf();
186189142Sdas	n = strlen(apothegm);
187290537Sngie	printf("getdelim\n");
188290537Sngie	ATF_REQUIRE(getdelim(&line, &linecap, '\0', fp) == n + 1);
189290537Sngie	ATF_REQUIRE(strcmp(line, apothegm) == 0);
190290537Sngie	ATF_REQUIRE(line[n + 1] == '\0');
191290537Sngie	ATF_REQUIRE(linecap > n + 1);
192189142Sdas	n = strlen(apothegm + n + 1);
193290537Sngie	printf("getdelim 2\n");
194290537Sngie	ATF_REQUIRE(getdelim(&line, &linecap, '\0', fp) == n + 1);
195290537Sngie	ATF_REQUIRE(line[n + 1] == '\0');
196290537Sngie	ATF_REQUIRE(linecap > n + 1);
197290537Sngie	ATF_REQUIRE(errno == 0);
198290537Sngie	ATF_REQUIRE(!ferror(fp));
199189142Sdas	fclose(fp);
200290537Sngie}
201189142Sdas
202290537SngieATF_TC_WITHOUT_HEAD(empty_NULL_buffer);
203290537SngieATF_TC_BODY(empty_NULL_buffer, tc)
204290537Sngie{
205290537Sngie	FILE *fp;
206290537Sngie	char *line;
207290537Sngie	size_t linecap;
208290537Sngie
209197753Sdas	/* Make sure NULL *linep and zero *linecapp are handled. */
210197753Sdas	fp = mkfilebuf();
211197753Sdas	line = NULL;
212197753Sdas	linecap = 42;
213290537Sngie	ATF_REQUIRE(getline(&line, &linecap, fp) == sizeof(apothegm) - 1);
214290537Sngie	ATF_REQUIRE(memcmp(line, apothegm, sizeof(apothegm)) == 0);
215197753Sdas	fp = mkfilebuf();
216197753Sdas	free(line);
217197753Sdas	line = malloc(100);
218197753Sdas	linecap = 0;
219290537Sngie	ATF_REQUIRE(getline(&line, &linecap, fp) == sizeof(apothegm) - 1);
220290537Sngie	ATF_REQUIRE(memcmp(line, apothegm, sizeof(apothegm)) == 0);
221197753Sdas	free(line);
222290537Sngie	ATF_REQUIRE(!ferror(fp));
223197753Sdas	fclose(fp);
224290537Sngie}
225197753Sdas
226290537SngieATF_TP_ADD_TCS(tp)
227290537Sngie{
228290537Sngie
229290537Sngie	ATF_TP_ADD_TC(tp, getline_basic);
230290537Sngie	ATF_TP_ADD_TC(tp, stream_error);
231290537Sngie	ATF_TP_ADD_TC(tp, eof);
232290537Sngie	ATF_TP_ADD_TC(tp, invalid_params);
233290537Sngie	ATF_TP_ADD_TC(tp, nul);
234290537Sngie	ATF_TP_ADD_TC(tp, empty_NULL_buffer);
235290537Sngie
236290537Sngie	return (atf_no_error());
237189142Sdas}
238