2 * Copyright (c) 2012 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 * $Id: t_openpam_readlinev.c 648 2013-03-05 17:54:27Z des $
41 #include <security/pam_appl.h>
42 #include <security/openpam.h>
44 #include "openpam_impl.h"
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.
54 orlv_expect(struct t_file *tf, const char **expectedv, int lines, int eof)
56 int expectedc, gotc, i, lineno = 0;
60 if (expectedv != NULL)
61 while (expectedv[expectedc] != NULL)
63 gotv = openpam_readlinev(tf->file, &lineno, &gotc);
65 err(1, "%s(): %s", __func__, tf->name);
66 if (expectedv != NULL && gotv == NULL) {
67 t_verbose("expected %d words, got nothing\n", expectedc);
70 if (expectedv == NULL && gotv != NULL) {
71 t_verbose("expected nothing, got %d words\n", gotc);
75 if (expectedv != NULL && gotv != NULL) {
76 if (expectedc != gotc) {
77 t_verbose("expected %d words, got %d\n",
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]);
92 if (lineno != lines) {
93 t_verbose("expected to advance %d lines, advanced %d lines\n",
97 if (eof && !t_feof(tf)) {
98 t_verbose("expected EOF, but didn't get it\n");
101 if (!eof && t_feof(tf)) {
102 t_verbose("didn't expect EOF, but got it anyway\n");
109 /***************************************************************************
110 * Commonly-used lines
113 static const char *empty[] = {
117 static const char *hello[] = {
122 static const char *hello_world[] = {
129 /***************************************************************************
130 * Lines without words
133 T_FUNC(empty_input, "empty input")
139 ret = orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/);
144 T_FUNC(empty_line, "empty line")
152 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
157 T_FUNC(unterminated_empty_line, "unterminated empty line")
165 ret = orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/);
170 T_FUNC(whitespace, "whitespace")
176 t_fprintf(tf, " \n");
178 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
183 T_FUNC(comment, "comment")
189 t_fprintf(tf, "# comment\n");
191 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
196 T_FUNC(whitespace_before_comment, "whitespace before comment")
202 t_fprintf(tf, " # comment\n");
204 ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
210 /***************************************************************************
214 T_FUNC(one_word, "one word")
220 t_fprintf(tf, "hello\n");
222 ret = orlv_expect(tf, hello, 1 /*lines*/, 0 /*eof*/);
227 T_FUNC(two_words, "two words")
233 t_fprintf(tf, "hello world\n");
235 ret = orlv_expect(tf, hello_world, 1 /*lines*/, 0 /*eof*/);
240 T_FUNC(unterminated_line, "unterminated line")
246 t_fprintf(tf, "hello world");
248 ret = orlv_expect(tf, hello_world, 0 /*lines*/, 1 /*eof*/);
254 /***************************************************************************
258 const struct t_test *t_plan[] = {
261 T(unterminated_empty_line),
264 T(whitespace_before_comment),
268 T(unterminated_line),
273 const struct t_test **
274 t_prepare(int argc, char *argv[])