]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - tools/regression/lib/libc/stdio/test-getdelim.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / tools / regression / lib / libc / stdio / test-getdelim.c
1 /*-
2  * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #define _WITH_GETLINE
31 #include <assert.h>
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #define CHUNK_MAX       10
38
39 /* The assertions depend on this string. */
40 char apothegm[] = "All work and no play\0 makes Jack a dull boy.\n";
41
42 /*
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.
46  */
47 static int
48 _reader(void *cookie, char *buf, int len)
49 {
50         size_t *offp = cookie;
51         size_t r;
52
53         r = random() % CHUNK_MAX + 1;
54         if (len > r)
55                 len = r;
56         if (len > sizeof(apothegm) - *offp)
57                 len = sizeof(apothegm) - *offp;
58         memcpy(buf, apothegm + *offp, len);
59         *offp += len;
60         return (len);
61 }
62
63 static FILE *
64 mkfilebuf(void)
65 {
66         size_t *offp;
67
68         offp = malloc(sizeof(*offp));   /* XXX leak */
69         *offp = 0;
70         return (fropen(offp, _reader));
71 }
72
73 int
74 main(int argc, char *argv[])
75 {
76         FILE *fp;
77         char *line;
78         size_t linecap;
79         int i, n;
80
81         srandom(0);
82
83         printf("1..6\n");
84
85         /*
86          * Test multiple times with different buffer sizes
87          * and different _reader() return values.
88          */
89         errno = 0;
90         for (i = 0; i < 8; i++) {
91                 fp = mkfilebuf();
92                 linecap = i;
93                 line = malloc(i);
94                 /* First line: the full apothegm */
95                 assert(getline(&line, &linecap, fp) == sizeof(apothegm) - 1);
96                 assert(memcmp(line, apothegm, sizeof(apothegm)) == 0);
97                 assert(linecap >= sizeof(apothegm));
98                 /* Second line: the NUL terminator following the newline */
99                 assert(getline(&line, &linecap, fp) == 1);
100                 assert(line[0] == '\0' && line[1] == '\0');
101                 /* Third line: EOF */
102                 line[0] = 'X';
103                 assert(getline(&line, &linecap, fp) == -1);
104                 assert(line[0] == '\0');
105                 free(line);
106                 line = NULL;
107                 assert(feof(fp));
108                 assert(!ferror(fp));
109                 fclose(fp);
110         }
111         assert(errno == 0);
112         printf("ok 1 - getline basic\n");
113
114         /* Make sure read errors are handled properly. */
115         linecap = 0;
116         errno = 0;
117         assert(getline(&line, &linecap, stdout) == -1);
118         assert(errno == EBADF);
119         errno = 0;
120         assert(getdelim(&line, &linecap, 'X', stdout) == -1);
121         assert(errno == EBADF);
122         assert(ferror(stdout));
123         printf("ok 2 - stream error\n");
124
125         /* Make sure NULL linep or linecapp pointers are handled. */
126         fp = mkfilebuf();
127         assert(getline(NULL, &linecap, fp) == -1);
128         assert(errno == EINVAL);
129         assert(getline(&line, NULL, fp) == -1);
130         assert(errno == EINVAL);
131         assert(ferror(fp));
132         fclose(fp);
133         printf("ok 3 - invalid params\n");
134
135         /* Make sure getline() allocates memory as needed if fp is at EOF. */
136         errno = 0;
137         fp = mkfilebuf();
138         while (!feof(fp))       /* advance to EOF; can't fseek this stream */
139                 getc(fp);
140         free(line);
141         line = NULL;
142         linecap = 0;
143         assert(getline(&line, &linecap, fp) == -1);
144         assert(line[0] == '\0');
145         assert(linecap > 0);
146         assert(errno == 0);
147         assert(feof(fp));
148         assert(!ferror(fp));
149         fclose(fp);
150         printf("ok 4 - eof\n");
151
152         /* Make sure a NUL delimiter works. */
153         fp = mkfilebuf();
154         n = strlen(apothegm);
155         assert(getdelim(&line, &linecap, '\0', fp) == n + 1);
156         assert(strcmp(line, apothegm) == 0);
157         assert(line[n + 1] == '\0');
158         assert(linecap > n + 1);
159         n = strlen(apothegm + n + 1);
160         assert(getdelim(&line, &linecap, '\0', fp) == n + 1);
161         assert(line[n + 1] == '\0');
162         assert(linecap > n + 1);
163         assert(errno == 0);
164         assert(!ferror(fp));
165         fclose(fp);
166         printf("ok 5 - nul\n");
167
168         /* Make sure NULL *linep and zero *linecapp are handled. */
169         fp = mkfilebuf();
170         free(line);
171         line = NULL;
172         linecap = 42;
173         assert(getline(&line, &linecap, fp) == sizeof(apothegm) - 1);
174         assert(memcmp(line, apothegm, sizeof(apothegm)) == 0);
175         fp = mkfilebuf();
176         free(line);
177         line = malloc(100);
178         linecap = 0;
179         assert(getline(&line, &linecap, fp) == sizeof(apothegm) - 1);
180         assert(memcmp(line, apothegm, sizeof(apothegm)) == 0);
181         free(line);
182         assert(!ferror(fp));
183         fclose(fp);
184         printf("ok 6 - empty/NULL initial buffer\n");
185
186         exit(0);
187 }