1 /*- 2 * Copyright (c) 2012 Dag-Erling Smørgrav 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 * 3. The name of the author may not be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * Id: t_openpam_readlinev.c 648 2013-03-05 17:54:27Z des 30 */ 31 32 #ifdef HAVE_CONFIG_H 33 # include "config.h" 34 #endif 35 36 #include <err.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 41 #include <security/pam_appl.h> 42 #include <security/openpam.h> 43 44 #include "openpam_impl.h" 45 #include "t.h" 46 47 /* 48 * Read a line from the temp file and verify that the result matches our 49 * expectations: whether a line was read at all, how many and which words 50 * it contained, how many lines were read (in case of quoted or escaped 51 * newlines) and whether we reached the end of the file. 52 */ 53 static int 54 orlv_expect(struct t_file *tf, const char **expectedv, int lines, int eof) 55 { 56 int expectedc, gotc, i, lineno = 0; 57 char **gotv; 58 59 expectedc = 0; 60 if (expectedv != NULL) 61 while (expectedv[expectedc] != NULL) 62 ++expectedc; 63 gotv = openpam_readlinev(tf->file, &lineno, &gotc); 64 if (t_ferror(tf)) 65 err(1, "%s(): %s", __func__, tf->name); 66 if (expectedv != NULL && gotv == NULL) { 67 t_verbose("expected %d words, got nothing\n", expectedc); 68 return (0); 69 } 70 if (expectedv == NULL && gotv != NULL) { 71 t_verbose("expected nothing, got %d words\n", gotc); 72 FREEV(gotc, gotv); 73 return (0); 74 } 75 if (expectedv != NULL && gotv != NULL) { 76 if (expectedc != gotc) { 77 t_verbose("expected %d words, got %d\n", 78 expectedc, gotc); 79 FREEV(gotc, gotv); 80 return (0); 81 } 82 for (i = 0; i < gotc; ++i) { 83 if (strcmp(expectedv[i], gotv[i]) != 0) { 84 t_verbose("word %d: expected <<%s>>, " 85 "got <<%s>>\n", i, expectedv[i], gotv[i]); 86 FREEV(gotc, gotv); 87 return (0); 88 } 89 } 90 FREEV(gotc, gotv); 91 } 92 if (lineno != lines) { 93 t_verbose("expected to advance %d lines, advanced %d lines\n", 94 lines, lineno); 95 return (0); 96 } 97 if (eof && !t_feof(tf)) { 98 t_verbose("expected EOF, but didn't get it\n"); 99 return (0); 100 } 101 if (!eof && t_feof(tf)) { 102 t_verbose("didn't expect EOF, but got it anyway\n"); 103 return (0); 104 } 105 return (1); 106 } 107 108 109 /*************************************************************************** 110 * Commonly-used lines 111 */ 112 113 static const char *empty[] = { 114 NULL 115 }; 116 117 static const char *hello[] = { 118 "hello", 119 NULL 120 }; 121 122 static const char *hello_world[] = { 123 "hello", 124 "world", 125 NULL 126 }; 127 128 129 static const char *numbers[] = { 130 "zero", "one", "two", "three", "four", "five", "six", "seven", 131 "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", 132 "fifteen", "sixteen", "seventeen", "nineteen", "twenty", 133 "twenty-one", "twenty-two", "twenty-three", "twenty-four", 134 "twenty-five", "twenty-six", "twenty-seven", "twenty-eight", 135 "twenty-nine", "thirty", "thirty-one", "thirty-two", "thirty-three", 136 "thirty-four", "thirty-five", "thirty-six", "thirty-seven", 137 "thirty-eight", "thirty-nine", "fourty", "fourty-one", "fourty-two", 138 "fourty-three", "fourty-four", "fourty-five", "fourty-six", 139 "fourty-seven", "fourty-eight", "fourty-nine", "fifty", "fifty-one", 140 "fifty-two", "fifty-three", "fifty-four", "fifty-five", "fifty-six", 141 "fifty-seven", "fifty-eight", "fifty-nine", "sixty", "sixty-one", 142 "sixty-two", "sixty-three", 143 NULL 144 }; 145 146 /*************************************************************************** 147 * Lines without words 148 */ 149 150 T_FUNC(empty_input, "empty input") 151 { 152 struct t_file *tf; 153 int ret; 154 155 tf = t_fopen(NULL); 156 ret = orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/); 157 t_fclose(tf); 158 return (ret); 159 } 160 161 T_FUNC(empty_line, "empty line") 162 { 163 struct t_file *tf; 164 int ret; 165 166 tf = t_fopen(NULL); 167 t_fprintf(tf, "\n"); 168 t_frewind(tf); 169 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/); 170 t_fclose(tf); 171 return (ret); 172 } 173 174 T_FUNC(unterminated_empty_line, "unterminated empty line") 175 { 176 struct t_file *tf; 177 int ret; 178 179 tf = t_fopen(NULL); 180 t_fprintf(tf, " "); 181 t_frewind(tf); 182 ret = orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/); 183 t_fclose(tf); 184 return (ret); 185 } 186 187 T_FUNC(whitespace, "whitespace") 188 { 189 struct t_file *tf; 190 int ret; 191 192 tf = t_fopen(NULL); 193 t_fprintf(tf, " \n"); 194 t_frewind(tf); 195 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/); 196 t_fclose(tf); 197 return (ret); 198 } 199 200 T_FUNC(comment, "comment") 201 { 202 struct t_file *tf; 203 int ret; 204 205 tf = t_fopen(NULL); 206 t_fprintf(tf, "# comment\n"); 207 t_frewind(tf); 208 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/); 209 t_fclose(tf); 210 return (ret); 211 } 212 213 T_FUNC(whitespace_before_comment, "whitespace before comment") 214 { 215 struct t_file *tf; 216 int ret; 217 218 tf = t_fopen(NULL); 219 t_fprintf(tf, " # comment\n"); 220 t_frewind(tf); 221 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/); 222 t_fclose(tf); 223 return (ret); 224 } 225 226 227 /*************************************************************************** 228 * Simple words 229 */ 230 231 T_FUNC(one_word, "one word") 232 { 233 struct t_file *tf; 234 int ret; 235 236 tf = t_fopen(NULL); 237 t_fprintf(tf, "hello\n"); 238 t_frewind(tf); 239 ret = orlv_expect(tf, hello, 1 /*lines*/, 0 /*eof*/); 240 t_fclose(tf); 241 return (ret); 242 } 243 244 T_FUNC(two_words, "two words") 245 { 246 struct t_file *tf; 247 int ret; 248 249 tf = t_fopen(NULL); 250 t_fprintf(tf, "hello world\n"); 251 t_frewind(tf); 252 ret = orlv_expect(tf, hello_world, 1 /*lines*/, 0 /*eof*/); 253 t_fclose(tf); 254 return (ret); 255 } 256 257 T_FUNC(many_words, "many words") 258 { 259 struct t_file *tf; 260 const char **word; 261 int ret; 262 263 tf = t_fopen(NULL); 264 for (word = numbers; *word; ++word) 265 t_fprintf(tf, " %s", *word); 266 t_fprintf(tf, "\n"); 267 t_frewind(tf); 268 ret = orlv_expect(tf, numbers, 1 /*lines*/, 0 /*eof*/); 269 t_fclose(tf); 270 return (ret); 271 } 272 273 T_FUNC(unterminated_line, "unterminated line") 274 { 275 struct t_file *tf; 276 int ret; 277 278 tf = t_fopen(NULL); 279 t_fprintf(tf, "hello world"); 280 t_frewind(tf); 281 ret = orlv_expect(tf, hello_world, 0 /*lines*/, 1 /*eof*/); 282 t_fclose(tf); 283 return (ret); 284 } 285 286 287 /*************************************************************************** 288 * Boilerplate 289 */ 290 291 const struct t_test *t_plan[] = { 292 T(empty_input), 293 T(empty_line), 294 T(unterminated_empty_line), 295 T(whitespace), 296 T(comment), 297 T(whitespace_before_comment), 298 299 T(one_word), 300 T(two_words), 301 T(many_words), 302 T(unterminated_line), 303 304 NULL 305 }; 306 307 const struct t_test ** 308 t_prepare(int argc, char *argv[]) 309 { 310 311 (void)argc; 312 (void)argv; 313 return (t_plan); 314 } 315 316 void 317 t_cleanup(void) 318 { 319 } 320