1 /*- 2 * Copyright (c) 2012-2017 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 * $OpenPAM: t_openpam_readlinev.c 938 2017-04-30 21:34:42Z des $ 30 */ 31 32 #ifdef HAVE_CONFIG_H 33 # include "config.h" 34 #endif 35 36 #include <err.h> 37 #include <stdint.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 42 #include <cryb/test.h> 43 44 #include <security/pam_appl.h> 45 #include <security/openpam.h> 46 47 #include "openpam_impl.h" 48 49 #define T_FUNC(n, d) \ 50 static const char *t_ ## n ## _desc = d; \ 51 static int t_ ## n ## _func(OPENPAM_UNUSED(char **desc), \ 52 OPENPAM_UNUSED(void *arg)) 53 54 #define T(n) \ 55 t_add_test(&t_ ## n ## _func, NULL, "%s", t_ ## n ## _desc) 56 57 /* 58 * Read a line from the temp file and verify that the result matches our 59 * expectations: whether a line was read at all, how many and which words 60 * it contained, how many lines were read (in case of quoted or escaped 61 * newlines) and whether we reached the end of the file. 62 */ 63 static int 64 orlv_expect(struct t_file *tf, const char **expectedv, int lines, int eof) 65 { 66 int expectedc, gotc, i, lineno = 0; 67 char **gotv; 68 int ret; 69 70 ret = 1; 71 expectedc = 0; 72 if (expectedv != NULL) 73 while (expectedv[expectedc] != NULL) 74 ++expectedc; 75 gotv = openpam_readlinev(tf->file, &lineno, &gotc); 76 if (t_ferror(tf)) 77 err(1, "%s(): %s", __func__, tf->name); 78 if (expectedv != NULL && gotv == NULL) { 79 t_printv("expected %d words, got nothing\n", expectedc); 80 ret = 0; 81 } else if (expectedv == NULL && gotv != NULL) { 82 t_printv("expected nothing, got %d words\n", gotc); 83 ret = 0; 84 } else if (expectedv != NULL && gotv != NULL) { 85 if (expectedc != gotc) { 86 t_printv("expected %d words, got %d\n", 87 expectedc, gotc); 88 ret = 0; 89 } 90 for (i = 0; i < gotc; ++i) { 91 if (strcmp(expectedv[i], gotv[i]) != 0) { 92 t_printv("word %d: expected <<%s>>, " 93 "got <<%s>>\n", i, expectedv[i], gotv[i]); 94 ret = 0; 95 } 96 } 97 } 98 FREEV(gotc, gotv); 99 if (lineno != lines) { 100 t_printv("expected to advance %d lines, advanced %d lines\n", 101 lines, lineno); 102 ret = 0; 103 } 104 if (eof && !t_feof(tf)) { 105 t_printv("expected EOF, but didn't get it\n"); 106 ret = 0; 107 } else if (!eof && t_feof(tf)) { 108 t_printv("didn't expect EOF, but got it anyway\n"); 109 ret = 0; 110 } 111 return (ret); 112 } 113 114 115 /*************************************************************************** 116 * Commonly-used lines 117 */ 118 119 static const char *empty[] = { 120 NULL 121 }; 122 123 static const char *hello[] = { 124 "hello", 125 NULL 126 }; 127 128 static const char *hello_world[] = { 129 "hello", 130 "world", 131 NULL 132 }; 133 134 static const char *numbers[] = { 135 "zero", "one", "two", "three", "four", "five", "six", "seven", 136 "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", 137 "fifteen", "sixteen", "seventeen", "nineteen", "twenty", 138 "twenty-one", "twenty-two", "twenty-three", "twenty-four", 139 "twenty-five", "twenty-six", "twenty-seven", "twenty-eight", 140 "twenty-nine", "thirty", "thirty-one", "thirty-two", "thirty-three", 141 "thirty-four", "thirty-five", "thirty-six", "thirty-seven", 142 "thirty-eight", "thirty-nine", "fourty", "fourty-one", "fourty-two", 143 "fourty-three", "fourty-four", "fourty-five", "fourty-six", 144 "fourty-seven", "fourty-eight", "fourty-nine", "fifty", "fifty-one", 145 "fifty-two", "fifty-three", "fifty-four", "fifty-five", "fifty-six", 146 "fifty-seven", "fifty-eight", "fifty-nine", "sixty", "sixty-one", 147 "sixty-two", "sixty-three", 148 NULL 149 }; 150 151 152 static const char *numbers[] = { 153 "zero", "one", "two", "three", "four", "five", "six", "seven", 154 "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", 155 "fifteen", "sixteen", "seventeen", "nineteen", "twenty", 156 "twenty-one", "twenty-two", "twenty-three", "twenty-four", 157 "twenty-five", "twenty-six", "twenty-seven", "twenty-eight", 158 "twenty-nine", "thirty", "thirty-one", "thirty-two", "thirty-three", 159 "thirty-four", "thirty-five", "thirty-six", "thirty-seven", 160 "thirty-eight", "thirty-nine", "fourty", "fourty-one", "fourty-two", 161 "fourty-three", "fourty-four", "fourty-five", "fourty-six", 162 "fourty-seven", "fourty-eight", "fourty-nine", "fifty", "fifty-one", 163 "fifty-two", "fifty-three", "fifty-four", "fifty-five", "fifty-six", 164 "fifty-seven", "fifty-eight", "fifty-nine", "sixty", "sixty-one", 165 "sixty-two", "sixty-three", 166 NULL 167 }; 168 169 /*************************************************************************** 170 * Lines without words 171 */ 172 173 T_FUNC(empty_input, "empty input") 174 { 175 struct t_file *tf; 176 int ret; 177 178 tf = t_fopen(NULL); 179 ret = orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/); 180 t_fclose(tf); 181 return (ret); 182 } 183 184 T_FUNC(empty_line, "empty line") 185 { 186 struct t_file *tf; 187 int ret; 188 189 tf = t_fopen(NULL); 190 t_fprintf(tf, "\n"); 191 t_frewind(tf); 192 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/); 193 t_fclose(tf); 194 return (ret); 195 } 196 197 T_FUNC(unterminated_empty_line, "unterminated empty line") 198 { 199 struct t_file *tf; 200 int ret; 201 202 tf = t_fopen(NULL); 203 t_fprintf(tf, " "); 204 t_frewind(tf); 205 ret = orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/); 206 t_fclose(tf); 207 return (ret); 208 } 209 210 T_FUNC(whitespace, "whitespace") 211 { 212 struct t_file *tf; 213 int ret; 214 215 tf = t_fopen(NULL); 216 t_fprintf(tf, " \n"); 217 t_frewind(tf); 218 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/); 219 t_fclose(tf); 220 return (ret); 221 } 222 223 T_FUNC(comment, "comment") 224 { 225 struct t_file *tf; 226 int ret; 227 228 tf = t_fopen(NULL); 229 t_fprintf(tf, "# comment\n"); 230 t_frewind(tf); 231 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/); 232 t_fclose(tf); 233 return (ret); 234 } 235 236 T_FUNC(whitespace_before_comment, "whitespace before comment") 237 { 238 struct t_file *tf; 239 int ret; 240 241 tf = t_fopen(NULL); 242 t_fprintf(tf, " # comment\n"); 243 t_frewind(tf); 244 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/); 245 t_fclose(tf); 246 return (ret); 247 } 248 249 T_FUNC(line_continuation_within_whitespace, "line continuation within whitespace") 250 { 251 struct t_file *tf; 252 int ret; 253 254 tf = t_fopen(NULL); 255 t_fprintf(tf, "%s \\\n %s\n", hello_world[0], hello_world[1]); 256 t_frewind(tf); 257 ret = orlv_expect(tf, hello_world, 2 /*lines*/, 0 /*eof*/) && 258 orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/); 259 t_fclose(tf); 260 return (ret); 261 } 262 263 264 /*************************************************************************** 265 * Simple words 266 */ 267 268 T_FUNC(one_word, "one word") 269 { 270 struct t_file *tf; 271 int ret; 272 273 tf = t_fopen(NULL); 274 t_fprintf(tf, "hello\n"); 275 t_frewind(tf); 276 ret = orlv_expect(tf, hello, 1 /*lines*/, 0 /*eof*/); 277 t_fclose(tf); 278 return (ret); 279 } 280 281 T_FUNC(two_words, "two words") 282 { 283 struct t_file *tf; 284 int ret; 285 286 tf = t_fopen(NULL); 287 t_fprintf(tf, "hello world\n"); 288 t_frewind(tf); 289 ret = orlv_expect(tf, hello_world, 1 /*lines*/, 0 /*eof*/); 290 t_fclose(tf); 291 return (ret); 292 } 293 294 T_FUNC(many_words, "many words") 295 { 296 struct t_file *tf; 297 const char **word; 298 int ret; 299 300 tf = t_fopen(NULL); 301 for (word = numbers; *word; ++word) 302 t_fprintf(tf, " %s", *word); 303 t_fprintf(tf, "\n"); 304 t_frewind(tf); 305 ret = orlv_expect(tf, numbers, 1 /*lines*/, 0 /*eof*/); 306 t_fclose(tf); 307 return (ret); 308 } 309 310 T_FUNC(unterminated_line, "unterminated line") 311 { 312 struct t_file *tf; 313 int ret; 314 315 tf = t_fopen(NULL); 316 t_fprintf(tf, "hello world"); 317 t_frewind(tf); 318 ret = orlv_expect(tf, hello_world, 0 /*lines*/, 1 /*eof*/); 319 t_fclose(tf); 320 return (ret); 321 } 322 323 324 /*************************************************************************** 325 * Boilerplate 326 */ 327 328 static int 329 t_prepare(int argc, char *argv[]) 330 { 331 332 (void)argc; 333 (void)argv; 334 335 T(empty_input); 336 T(empty_line); 337 T(unterminated_empty_line); 338 T(whitespace); 339 T(comment); 340 T(whitespace_before_comment); 341 T(line_continuation_within_whitespace); 342 343 T(one_word); 344 T(two_words); 345 T(many_words); 346 T(unterminated_line); 347 348 return (0); 349 } 350 351 int 352 main(int argc, char *argv[]) 353 { 354 355 t_main(t_prepare, NULL, argc, argv); 356 } 357