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 #include <errno.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 35 #include <atf-c.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 ATF_TC_WITHOUT_HEAD(getline_basic); 74 ATF_TC_BODY(getline_basic, tc) 75 { 76 FILE *fp; 77 char *line; 78 size_t linecap; 79 int i; 80 81 srandom(0); 82 83 /* 84 * Test multiple times with different buffer sizes 85 * and different _reader() return values. 86 */ 87 errno = 0; 88 for (i = 0; i < 8; i++) { 89 fp = mkfilebuf(); 90 linecap = i; 91 line = malloc(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'); 99 /* Third line: EOF */ 100 line[0] = 'X'; 101 ATF_REQUIRE(getline(&line, &linecap, fp) == -1); 102 ATF_REQUIRE(line[0] == '\0'); 103 free(line); 104 line = NULL; 105 ATF_REQUIRE(feof(fp)); 106 ATF_REQUIRE(!ferror(fp)); 107 fclose(fp); 108 } 109 ATF_REQUIRE(errno == 0); 110 } 111 112 ATF_TC_WITHOUT_HEAD(stream_error); 113 ATF_TC_BODY(stream_error, tc) 114 { 115 char *line; 116 size_t linecap; 117 118 /* Make sure read errors are handled properly. */ 119 line = NULL; 120 linecap = 0; 121 errno = 0; 122 ATF_REQUIRE(getline(&line, &linecap, stdout) == -1); 123 ATF_REQUIRE(errno == EBADF); 124 errno = 0; 125 ATF_REQUIRE(getdelim(&line, &linecap, 'X', stdout) == -1); 126 ATF_REQUIRE(errno == EBADF); 127 ATF_REQUIRE(ferror(stdout)); 128 } 129 130 ATF_TC_WITHOUT_HEAD(invalid_params); 131 ATF_TC_BODY(invalid_params, tc) 132 { 133 FILE *fp; 134 char *line; 135 size_t linecap; 136 137 /* Make sure NULL linep or linecapp pointers are handled. */ 138 fp = mkfilebuf(); 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)); 144 fclose(fp); 145 } 146 147 ATF_TC_WITHOUT_HEAD(eof); 148 ATF_TC_BODY(eof, tc) 149 { 150 FILE *fp; 151 char *line; 152 size_t linecap; 153 154 /* Make sure getline() allocates memory as needed if fp is at EOF. */ 155 errno = 0; 156 fp = mkfilebuf(); 157 while (!feof(fp)) /* advance to EOF; can't fseek this stream */ 158 getc(fp); 159 line = NULL; 160 linecap = 0; 161 printf("getline\n"); 162 ATF_REQUIRE(getline(&line, &linecap, fp) == -1); 163 ATF_REQUIRE(line[0] == '\0'); 164 ATF_REQUIRE(linecap > 0); 165 ATF_REQUIRE(errno == 0); 166 printf("feof\n"); 167 ATF_REQUIRE(feof(fp)); 168 ATF_REQUIRE(!ferror(fp)); 169 fclose(fp); 170 } 171 172 ATF_TC_WITHOUT_HEAD(nul); 173 ATF_TC_BODY(nul, tc) 174 { 175 FILE *fp; 176 char *line; 177 size_t linecap, n; 178 179 errno = 0; 180 line = NULL; 181 linecap = 0; 182 /* Make sure a NUL delimiter works. */ 183 fp = mkfilebuf(); 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)); 197 fclose(fp); 198 } 199 200 ATF_TC_WITHOUT_HEAD(empty_NULL_buffer); 201 ATF_TC_BODY(empty_NULL_buffer, tc) 202 { 203 FILE *fp; 204 char *line; 205 size_t linecap; 206 207 /* Make sure NULL *linep and zero *linecapp are handled. */ 208 fp = mkfilebuf(); 209 line = NULL; 210 linecap = 42; 211 ATF_REQUIRE(getline(&line, &linecap, fp) == sizeof(apothegm) - 1); 212 ATF_REQUIRE(memcmp(line, apothegm, sizeof(apothegm)) == 0); 213 fp = mkfilebuf(); 214 free(line); 215 line = malloc(100); 216 linecap = 0; 217 ATF_REQUIRE(getline(&line, &linecap, fp) == sizeof(apothegm) - 1); 218 ATF_REQUIRE(memcmp(line, apothegm, sizeof(apothegm)) == 0); 219 free(line); 220 ATF_REQUIRE(!ferror(fp)); 221 fclose(fp); 222 } 223 224 ATF_TP_ADD_TCS(tp) 225 { 226 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); 233 234 return (atf_no_error()); 235 } 236