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_readword.c 584 2012-04-07 22:47:16Z des $
45 #include <security/pam_appl.h>
46 #include <security/openpam.h>
50 static char filename[1024];
54 * Open the temp file and immediately unlink it so it doesn't leak in case
62 if ((fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0600)) < 0)
63 err(1, "%s(): %s", __func__, filename);
64 if ((f = fdopen(fd, "r+")) == NULL)
65 err(1, "%s(): %s", __func__, filename);
66 if (unlink(filename) < 0)
67 err(1, "%s(): %s", __func__, filename);
71 * Write text to the temp file.
74 orw_output(const char *fmt, ...)
82 err(1, "%s", filename);
86 * Rewind the temp file.
95 err(1, "%s(): %s", __func__, filename);
99 * Read a word from the temp file and verify that the result matches our
100 * expectations: whether a word was read at all, how many lines were read
101 * (in case of quoted or escaped newlines), whether we reached the end of
102 * the file and whether we reached the end of the line.
105 orw_expect(const char *expected, int lines, int eof, int eol)
111 got = openpam_readword(f, &lineno, &len);
113 err(1, "%s(): %s", __func__, filename);
114 if (expected != NULL && got == NULL) {
115 t_verbose("expected <<%s>>, got nothing\n", expected);
118 if (expected == NULL && got != NULL) {
119 t_verbose("expected nothing, got <<%s>>\n", got);
122 if (expected != NULL && got != NULL && strcmp(expected, got) != 0) {
123 t_verbose("expected <<%s>>, got <<%s>>\n", expected, got);
126 if (lineno != lines) {
127 t_verbose("expected to advance %d lines, advanced %d lines\n",
131 if (eof && !feof(f)) {
132 t_verbose("expected EOF, but didn't get it\n");
135 if (!eof && feof(f)) {
136 t_verbose("didn't expect EOF, but got it anyway\n");
141 err(1, "%s(): %s", __func__, filename);
142 if (eol && ch != '\n') {
143 t_verbose("expected EOL, but didn't get it\n");
146 if (!eol && ch == '\n') {
147 t_verbose("didn't expect EOL, but got it anyway\n");
156 * Close the temp file.
163 err(1, "%s(): %s", __func__, filename);
168 /***************************************************************************
169 * Lines without words
172 T_FUNC(empty_input, "empty input")
177 ret = orw_expect(NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
182 T_FUNC(empty_line, "empty line")
189 ret = orw_expect(NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
194 T_FUNC(unterminated_line, "unterminated line")
201 ret = orw_expect(NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
206 T_FUNC(single_whitespace, "single whitespace")
213 ret = orw_expect(NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
218 T_FUNC(multiple_whitespace, "multiple whitespace")
223 orw_output(" \t\r\n");
225 ret = orw_expect(NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
230 T_FUNC(comment, "comment")
235 orw_output("# comment\n");
237 ret = orw_expect(NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
242 T_FUNC(whitespace_before_comment, "whitespace before comment")
247 orw_output(" # comment\n");
249 ret = orw_expect(NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
255 /***************************************************************************
256 * Simple cases - no quotes or escapes
259 T_FUNC(single_word, "single word")
261 const char *word = "hello";
265 orw_output("%s\n", word);
267 ret = orw_expect(word, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
272 T_FUNC(single_whitespace_before_word, "single whitespace before word")
274 const char *word = "hello";
278 orw_output(" %s\n", word);
280 ret = orw_expect(word, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
285 T_FUNC(double_whitespace_before_word, "double whitespace before word")
287 const char *word = "hello";
291 orw_output(" %s\n", word);
293 ret = orw_expect(word, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
298 T_FUNC(single_whitespace_after_word, "single whitespace after word")
300 const char *word = "hello";
304 orw_output("%s \n", word);
306 ret = orw_expect(word, 0 /*lines*/, 0 /*eof*/, 0 /*eol*/);
311 T_FUNC(double_whitespace_after_word, "double whitespace after word")
313 const char *word = "hello";
317 orw_output("%s \n", word);
319 ret = orw_expect(word, 0 /*lines*/, 0 /*eof*/, 0 /*eol*/);
324 T_FUNC(comment_after_word, "comment after word")
326 const char *word = "hello";
330 orw_output("%s # comment\n", word);
332 ret = orw_expect(word, 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
333 orw_expect(NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
338 T_FUNC(word_containing_hash, "word containing hash")
340 const char *word = "hello#world";
344 orw_output("%s\n", word);
346 ret = orw_expect(word, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
351 T_FUNC(two_words, "two words")
353 const char *word[] = { "hello", "world" };
357 orw_output("%s %s\n", word[0], word[1]);
359 ret = orw_expect(word[0], 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
360 orw_expect(word[1], 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
366 /***************************************************************************
370 T_FUNC(naked_escape, "naked escape")
377 ret = orw_expect(NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
382 T_FUNC(escaped_escape, "escaped escape")
387 orw_output("\\\\\n");
389 ret = orw_expect("\\", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
394 T_FUNC(escaped_whitespace, "escaped whitespace")
399 orw_output("\\ \\\t \\\r \\\n\n");
401 ret = orw_expect(" ", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
402 orw_expect("\t", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
403 orw_expect("\r", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
404 /* this last one is a line continuation */
405 orw_expect(NULL, 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
410 T_FUNC(escaped_newline_before_word, "escaped newline before word")
415 orw_output("\\\nhello world\n");
417 ret = orw_expect("hello", 1 /*lines*/, 0 /*eof*/, 0 /*eol*/);
422 T_FUNC(escaped_newline_within_word, "escaped newline within word")
427 orw_output("hello\\\nworld\n");
429 ret = orw_expect("helloworld", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
434 T_FUNC(escaped_newline_after_word, "escaped newline after word")
439 orw_output("hello\\\n world\n");
441 ret = orw_expect("hello", 1 /*lines*/, 0 /*eof*/, 0 /*eol*/);
446 T_FUNC(escaped_letter, "escaped letter")
453 ret = orw_expect("z", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
459 /***************************************************************************
463 T_FUNC(naked_single_quote, "naked single quote")
470 ret = orw_expect(NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
475 T_FUNC(naked_double_quote, "naked double quote")
482 ret = orw_expect(NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
487 T_FUNC(empty_single_quotes, "empty single quotes")
494 ret = orw_expect("", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
499 T_FUNC(empty_double_quotes, "empty double quotes")
504 orw_output("\"\"\n");
506 ret = orw_expect("", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
511 T_FUNC(single_quotes_within_double_quotes, "single quotes within double quotes")
516 orw_output("\"' '\"\n");
518 ret = orw_expect("' '", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
523 T_FUNC(double_quotes_within_single_quotes, "double quotes within single quotes")
528 orw_output("'\" \"'\n");
530 ret = orw_expect("\" \"", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
535 T_FUNC(single_quoted_whitespace, "single-quoted whitespace")
540 orw_output("' ' '\t' '\r' '\n'\n");
542 ret = orw_expect(" ", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
543 orw_expect("\t", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
544 orw_expect("\r", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
545 orw_expect("\n", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
550 T_FUNC(double_quoted_whitespace, "double-quoted whitespace")
555 orw_output("\" \" \"\t\" \"\r\" \"\n\"\n");
557 ret = orw_expect(" ", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
558 orw_expect("\t", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
559 orw_expect("\r", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
560 orw_expect("\n", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
565 T_FUNC(single_quoted_words, "single-quoted words")
570 orw_output("'hello world'\n");
572 ret = orw_expect("hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
577 T_FUNC(double_quoted_words, "double-quoted words")
582 orw_output("\"hello world\"\n");
584 ret = orw_expect("hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
590 /***************************************************************************
591 * Combinations of escape and quotes
594 T_FUNC(escaped_single_quote,
595 "escaped single quote")
602 ret = orw_expect("'", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
607 T_FUNC(escaped_double_quote,
608 "escaped double quote")
613 orw_output("\\\"\n");
615 ret = orw_expect("\"", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
620 T_FUNC(escaped_whitespace_within_single_quotes,
621 "escaped whitespace within single quotes")
626 orw_output("'\\ ' '\\\t' '\\\r' '\\\n'\n");
628 ret = orw_expect("\\ ", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
629 orw_expect("\\\t", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
630 orw_expect("\\\r", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
631 orw_expect("\\\n", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
636 T_FUNC(escaped_whitespace_within_double_quotes,
637 "escaped whitespace within double quotes")
642 orw_output("\"\\ \" \"\\\t\" \"\\\r\" \"\\\n\"\n");
644 ret = orw_expect("\\ ", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
645 orw_expect("\\\t", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
646 orw_expect("\\\r", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
647 /* this last one is a line continuation */
648 orw_expect("", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
653 T_FUNC(escaped_letter_within_single_quotes,
654 "escaped letter within single quotes")
659 orw_output("'\\z'\n");
661 ret = orw_expect("\\z", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
666 T_FUNC(escaped_letter_within_double_quotes,
667 "escaped letter within double quotes")
672 orw_output("\"\\z\"\n");
674 ret = orw_expect("\\z", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
679 T_FUNC(escaped_escape_within_single_quotes,
680 "escaped escape within single quotes")
685 orw_output("'\\\\'\n");
687 ret = orw_expect("\\\\", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
692 T_FUNC(escaped_escape_within_double_quotes,
693 "escaped escape within double quotes")
698 orw_output("\"\\\\\"\n");
700 ret = orw_expect("\\", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
705 T_FUNC(escaped_single_quote_within_single_quotes,
706 "escaped single quote within single quotes")
711 orw_output("'\\''\n");
713 ret = orw_expect(NULL, 1 /*lines*/, 1 /*eof*/, 0 /*eol*/);
718 T_FUNC(escaped_double_quote_within_single_quotes,
719 "escaped double quote within single quotes")
724 orw_output("'\\\"'\n");
726 ret = orw_expect("\\\"", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
731 T_FUNC(escaped_single_quote_within_double_quotes,
732 "escaped single quote within double quotes")
737 orw_output("\"\\'\"\n");
739 ret = orw_expect("\\'", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
744 T_FUNC(escaped_double_quote_within_double_quotes,
745 "escaped double quote within double quotes")
750 orw_output("\"\\\"\"\n");
752 ret = orw_expect("\"", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
758 /***************************************************************************
762 const struct t_test *t_plan[] = {
765 T(single_whitespace),
766 T(multiple_whitespace),
768 T(whitespace_before_comment),
771 T(single_whitespace_before_word),
772 T(double_whitespace_before_word),
773 T(single_whitespace_after_word),
774 T(double_whitespace_after_word),
775 T(comment_after_word),
776 T(word_containing_hash),
781 T(escaped_whitespace),
782 T(escaped_newline_before_word),
783 T(escaped_newline_within_word),
784 T(escaped_newline_after_word),
787 T(naked_single_quote),
788 T(naked_double_quote),
789 T(empty_single_quotes),
790 T(empty_double_quotes),
791 T(single_quotes_within_double_quotes),
792 T(double_quotes_within_single_quotes),
793 T(single_quoted_whitespace),
794 T(double_quoted_whitespace),
795 T(single_quoted_words),
796 T(double_quoted_words),
798 T(escaped_single_quote),
799 T(escaped_double_quote),
800 T(escaped_whitespace_within_single_quotes),
801 T(escaped_whitespace_within_double_quotes),
802 T(escaped_letter_within_single_quotes),
803 T(escaped_letter_within_double_quotes),
804 T(escaped_escape_within_single_quotes),
805 T(escaped_escape_within_double_quotes),
806 T(escaped_single_quote_within_single_quotes),
807 T(escaped_double_quote_within_single_quotes),
808 T(escaped_single_quote_within_double_quotes),
809 T(escaped_double_quote_within_double_quotes),
814 const struct t_test **
815 t_prepare(int argc, char *argv[])
820 snprintf(filename, sizeof filename, "%s.%d.tmp", t_progname, getpid());
821 if (filename == NULL)
822 err(1, "asprintf()");