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 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: t_openpam_readlinev.c 581 2012-04-06 01:08:37Z des $
45 #include <security/pam_appl.h>
46 #include <security/openpam.h>
48 #include "openpam_impl.h"
51 static char filename[1024];
55 * Open the temp file and immediately unlink it so it doesn't leak in case
63 if ((fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0600)) < 0)
64 err(1, "%s(): %s", __func__, filename);
65 if ((f = fdopen(fd, "r+")) == NULL)
66 err(1, "%s(): %s", __func__, filename);
67 if (unlink(filename) < 0)
68 err(1, "%s(): %s", __func__, filename);
72 * Write text to the temp file.
75 orlv_output(const char *fmt, ...)
83 err(1, "%s", filename);
87 * Rewind the temp file.
96 err(1, "%s(): %s", __func__, filename);
100 * Read a line from the temp file and verify that the result matches our
101 * expectations: whether a line was read at all, how many and which words
102 * it contained, how many lines were read (in case of quoted or escaped
103 * newlines) and whether we reached the end of the file.
106 orlv_expect(const char **expectedv, int lines, int eof)
108 int expectedc, gotc, i, lineno = 0;
112 if (expectedv != NULL)
113 while (expectedv[expectedc] != NULL)
115 gotv = openpam_readlinev(f, &lineno, &gotc);
117 err(1, "%s(): %s", __func__, filename);
118 if (expectedv != NULL && gotv == NULL) {
119 t_verbose("expected %d words, got nothing\n", expectedc);
122 if (expectedv == NULL && gotv != NULL) {
123 t_verbose("expected nothing, got %d words\n", gotc);
127 if (expectedv != NULL && gotv != NULL) {
128 if (expectedc != gotc) {
129 t_verbose("expected %d words, got %d\n",
134 for (i = 0; i < gotc; ++i) {
135 if (strcmp(expectedv[i], gotv[i]) != 0) {
136 t_verbose("word %d: expected <<%s>>, "
137 "got <<%s>>\n", i, expectedv[i], gotv[i]);
144 if (lineno != lines) {
145 t_verbose("expected to advance %d lines, advanced %d lines\n",
149 if (eof && !feof(f)) {
150 t_verbose("expected EOF, but didn't get it\n");
153 if (!eof && feof(f)) {
154 t_verbose("didn't expect EOF, but got it anyway\n");
161 * Close the temp file.
168 err(1, "%s(): %s", __func__, filename);
172 /***************************************************************************
173 * Commonly-used lines
176 static const char *empty[] = {
180 static const char *hello[] = {
185 static const char *hello_world[] = {
192 /***************************************************************************
193 * Lines without words
196 T_FUNC(empty_input, "empty input")
201 ret = orlv_expect(NULL, 0 /*lines*/, 1 /*eof*/);
206 T_FUNC(empty_line, "empty line")
213 ret = orlv_expect(empty, 1 /*lines*/, 0 /*eof*/);
218 T_FUNC(unterminated_empty_line, "unterminated empty line")
225 ret = orlv_expect(NULL, 0 /*lines*/, 1 /*eof*/);
230 T_FUNC(whitespace, "whitespace")
237 ret = orlv_expect(empty, 1 /*lines*/, 0 /*eof*/);
242 T_FUNC(comment, "comment")
247 orlv_output("# comment\n");
249 ret = orlv_expect(empty, 1 /*lines*/, 0 /*eof*/);
254 T_FUNC(whitespace_before_comment, "whitespace before comment")
259 orlv_output(" # comment\n");
261 ret = orlv_expect(empty, 1 /*lines*/, 0 /*eof*/);
267 /***************************************************************************
271 T_FUNC(one_word, "one word")
276 orlv_output("hello\n");
278 ret = orlv_expect(hello, 1 /*lines*/, 0 /*eof*/);
283 T_FUNC(two_words, "two words")
288 orlv_output("hello world\n");
290 ret = orlv_expect(hello_world, 1 /*lines*/, 0 /*eof*/);
295 T_FUNC(unterminated_line, "unterminated line")
300 orlv_output("hello world");
302 ret = orlv_expect(hello_world, 0 /*lines*/, 1 /*eof*/);
308 /***************************************************************************
312 const struct t_test *t_plan[] = {
315 T(unterminated_empty_line),
318 T(whitespace_before_comment),
322 T(unterminated_line),
327 const struct t_test **
328 t_prepare(int argc, char *argv[])
333 snprintf(filename, sizeof filename, "%s.%d.tmp", t_progname, getpid());
334 if (filename == NULL)
335 err(1, "asprintf()");