2 * Copyright (c) 2012-2017 Dag-Erling Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
29 * $OpenPAM: t_openpam_readlinev.c 938 2017-04-30 21:34:42Z des $
42 #include <cryb/test.h>
44 #include <security/pam_appl.h>
45 #include <security/openpam.h>
47 #include "openpam_impl.h"
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))
55 t_add_test(&t_ ## n ## _func, NULL, "%s", t_ ## n ## _desc)
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.
64 orlv_expect(struct t_file *tf, const char **expectedv, int lines, int eof)
66 int expectedc, gotc, i, lineno = 0;
72 if (expectedv != NULL)
73 while (expectedv[expectedc] != NULL)
75 gotv = openpam_readlinev(tf->file, &lineno, &gotc);
77 err(1, "%s(): %s", __func__, tf->name);
78 if (expectedv != NULL && gotv == NULL) {
79 t_printv("expected %d words, got nothing\n", expectedc);
81 } else if (expectedv == NULL && gotv != NULL) {
82 t_printv("expected nothing, got %d words\n", gotc);
84 } else if (expectedv != NULL && gotv != NULL) {
85 if (expectedc != gotc) {
86 t_printv("expected %d words, got %d\n",
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]);
99 if (lineno != lines) {
100 t_printv("expected to advance %d lines, advanced %d lines\n",
104 if (eof && !t_feof(tf)) {
105 t_printv("expected EOF, but didn't get it\n");
107 } else if (!eof && t_feof(tf)) {
108 t_printv("didn't expect EOF, but got it anyway\n");
115 /***************************************************************************
116 * Commonly-used lines
119 static const char *empty[] = {
123 static const char *hello[] = {
128 static const char *hello_world[] = {
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",
152 /***************************************************************************
153 * Lines without words
156 T_FUNC(empty_input, "empty input")
162 ret = orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/);
167 T_FUNC(empty_line, "empty line")
175 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
180 T_FUNC(unterminated_empty_line, "unterminated empty line")
188 ret = orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/);
193 T_FUNC(whitespace, "whitespace")
199 t_fprintf(tf, " \n");
201 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
206 T_FUNC(comment, "comment")
212 t_fprintf(tf, "# comment\n");
214 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
219 T_FUNC(whitespace_before_comment, "whitespace before comment")
225 t_fprintf(tf, " # comment\n");
227 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
232 T_FUNC(line_continuation_within_whitespace, "line continuation within whitespace")
238 t_fprintf(tf, "%s \\\n %s\n", hello_world[0], hello_world[1]);
240 ret = orlv_expect(tf, hello_world, 2 /*lines*/, 0 /*eof*/) &&
241 orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/);
247 /***************************************************************************
251 T_FUNC(one_word, "one word")
257 t_fprintf(tf, "hello\n");
259 ret = orlv_expect(tf, hello, 1 /*lines*/, 0 /*eof*/);
264 T_FUNC(two_words, "two words")
270 t_fprintf(tf, "hello world\n");
272 ret = orlv_expect(tf, hello_world, 1 /*lines*/, 0 /*eof*/);
277 T_FUNC(many_words, "many words")
284 for (word = numbers; *word; ++word)
285 t_fprintf(tf, " %s", *word);
288 ret = orlv_expect(tf, numbers, 1 /*lines*/, 0 /*eof*/);
293 T_FUNC(unterminated_line, "unterminated line")
299 t_fprintf(tf, "hello world");
301 ret = orlv_expect(tf, hello_world, 0 /*lines*/, 1 /*eof*/);
307 /***************************************************************************
312 t_prepare(int argc, char *argv[])
320 T(unterminated_empty_line);
323 T(whitespace_before_comment);
324 T(line_continuation_within_whitespace);
329 T(unterminated_line);
335 main(int argc, char *argv[])
338 t_main(t_prepare, NULL, argc, argv);