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 <assert.h>
32189142Sdas#include <errno.h>
33189142Sdas#include <stdio.h>
34189142Sdas#include <stdlib.h>
35189142Sdas#include <string.h>
36189142Sdas
37189142Sdas#define	CHUNK_MAX	10
38189142Sdas
39189142Sdas/* The assertions depend on this string. */
40189142Sdaschar apothegm[] = "All work and no play\0 makes Jack a dull boy.\n";
41189142Sdas
42189142Sdas/*
43189142Sdas * This is a neurotic reader function designed to give getdelim() a
44189142Sdas * hard time. It reads through the string `apothegm' and returns a
45189142Sdas * random number of bytes up to the requested length.
46189142Sdas */
47189142Sdasstatic int
48189142Sdas_reader(void *cookie, char *buf, int len)
49189142Sdas{
50189142Sdas	size_t *offp = cookie;
51189142Sdas	size_t r;
52189142Sdas
53189142Sdas	r = random() % CHUNK_MAX + 1;
54189142Sdas	if (len > r)
55189142Sdas		len = r;
56189142Sdas	if (len > sizeof(apothegm) - *offp)
57189142Sdas		len = sizeof(apothegm) - *offp;
58189142Sdas	memcpy(buf, apothegm + *offp, len);
59189142Sdas	*offp += len;
60189142Sdas	return (len);
61189142Sdas}
62189142Sdas
63189142Sdasstatic FILE *
64189142Sdasmkfilebuf(void)
65189142Sdas{
66189142Sdas	size_t *offp;
67189142Sdas
68189142Sdas	offp = malloc(sizeof(*offp));	/* XXX leak */
69189142Sdas	*offp = 0;
70189142Sdas	return (fropen(offp, _reader));
71189142Sdas}
72189142Sdas
73189142Sdasint
74189142Sdasmain(int argc, char *argv[])
75189142Sdas{
76189142Sdas	FILE *fp;
77189142Sdas	char *line;
78189142Sdas	size_t linecap;
79189142Sdas	int i, n;
80189142Sdas
81189142Sdas	srandom(0);
82189142Sdas
83197753Sdas	printf("1..6\n");
84189142Sdas
85189142Sdas	/*
86189142Sdas	 * Test multiple times with different buffer sizes
87189142Sdas	 * and different _reader() return values.
88189142Sdas	 */
89189142Sdas	errno = 0;
90189142Sdas	for (i = 0; i < 8; i++) {
91189142Sdas		fp = mkfilebuf();
92189142Sdas		linecap = i;
93189142Sdas		line = malloc(i);
94189142Sdas		/* First line: the full apothegm */
95189142Sdas		assert(getline(&line, &linecap, fp) == sizeof(apothegm) - 1);
96189142Sdas		assert(memcmp(line, apothegm, sizeof(apothegm)) == 0);
97189142Sdas		assert(linecap >= sizeof(apothegm));
98189142Sdas		/* Second line: the NUL terminator following the newline */
99189142Sdas		assert(getline(&line, &linecap, fp) == 1);
100189142Sdas		assert(line[0] == '\0' && line[1] == '\0');
101189142Sdas		/* Third line: EOF */
102189142Sdas		line[0] = 'X';
103190773Sdas		assert(getline(&line, &linecap, fp) == -1);
104189142Sdas		assert(line[0] == '\0');
105189142Sdas		free(line);
106197753Sdas		line = NULL;
107189142Sdas		assert(feof(fp));
108189142Sdas		assert(!ferror(fp));
109189142Sdas		fclose(fp);
110189142Sdas	}
111189142Sdas	assert(errno == 0);
112189142Sdas	printf("ok 1 - getline basic\n");
113189142Sdas
114189142Sdas	/* Make sure read errors are handled properly. */
115189142Sdas	linecap = 0;
116189142Sdas	errno = 0;
117189142Sdas	assert(getline(&line, &linecap, stdout) == -1);
118189142Sdas	assert(errno == EBADF);
119189142Sdas	errno = 0;
120189142Sdas	assert(getdelim(&line, &linecap, 'X', stdout) == -1);
121189142Sdas	assert(errno == EBADF);
122189142Sdas	assert(ferror(stdout));
123189142Sdas	printf("ok 2 - stream error\n");
124189142Sdas
125189142Sdas	/* Make sure NULL linep or linecapp pointers are handled. */
126189142Sdas	fp = mkfilebuf();
127189142Sdas	assert(getline(NULL, &linecap, fp) == -1);
128189142Sdas	assert(errno == EINVAL);
129189142Sdas	assert(getline(&line, NULL, fp) == -1);
130189142Sdas	assert(errno == EINVAL);
131189142Sdas	assert(ferror(fp));
132189142Sdas	fclose(fp);
133189142Sdas	printf("ok 3 - invalid params\n");
134189142Sdas
135189142Sdas	/* Make sure getline() allocates memory as needed if fp is at EOF. */
136189142Sdas	errno = 0;
137189142Sdas	fp = mkfilebuf();
138189142Sdas	while (!feof(fp))	/* advance to EOF; can't fseek this stream */
139189142Sdas		getc(fp);
140189142Sdas	free(line);
141189142Sdas	line = NULL;
142189142Sdas	linecap = 0;
143190773Sdas	assert(getline(&line, &linecap, fp) == -1);
144189142Sdas	assert(line[0] == '\0');
145189142Sdas	assert(linecap > 0);
146189142Sdas	assert(errno == 0);
147189142Sdas	assert(feof(fp));
148189142Sdas	assert(!ferror(fp));
149189142Sdas	fclose(fp);
150189142Sdas	printf("ok 4 - eof\n");
151189142Sdas
152189142Sdas	/* Make sure a NUL delimiter works. */
153189142Sdas	fp = mkfilebuf();
154189142Sdas	n = strlen(apothegm);
155189142Sdas	assert(getdelim(&line, &linecap, '\0', fp) == n + 1);
156189142Sdas	assert(strcmp(line, apothegm) == 0);
157189142Sdas	assert(line[n + 1] == '\0');
158189142Sdas	assert(linecap > n + 1);
159189142Sdas	n = strlen(apothegm + n + 1);
160189142Sdas	assert(getdelim(&line, &linecap, '\0', fp) == n + 1);
161189142Sdas	assert(line[n + 1] == '\0');
162189142Sdas	assert(linecap > n + 1);
163189142Sdas	assert(errno == 0);
164189142Sdas	assert(!ferror(fp));
165189142Sdas	fclose(fp);
166189142Sdas	printf("ok 5 - nul\n");
167189142Sdas
168197753Sdas	/* Make sure NULL *linep and zero *linecapp are handled. */
169197753Sdas	fp = mkfilebuf();
170197753Sdas	free(line);
171197753Sdas	line = NULL;
172197753Sdas	linecap = 42;
173197753Sdas	assert(getline(&line, &linecap, fp) == sizeof(apothegm) - 1);
174197753Sdas	assert(memcmp(line, apothegm, sizeof(apothegm)) == 0);
175197753Sdas	fp = mkfilebuf();
176197753Sdas	free(line);
177197753Sdas	line = malloc(100);
178197753Sdas	linecap = 0;
179197753Sdas	assert(getline(&line, &linecap, fp) == sizeof(apothegm) - 1);
180197753Sdas	assert(memcmp(line, apothegm, sizeof(apothegm)) == 0);
181197753Sdas	free(line);
182197753Sdas	assert(!ferror(fp));
183197753Sdas	fclose(fp);
184197753Sdas	printf("ok 6 - empty/NULL initial buffer\n");
185197753Sdas
186189142Sdas	exit(0);
187189142Sdas}
188