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_readword.c 938 2017-04-30 21:34:42Z des $
43 #include <cryb/test.h>
45 #include <security/pam_appl.h>
46 #include <security/openpam.h>
48 #define T_FUNC(n, d) \
49 static const char *t_ ## n ## _desc = d; \
50 static int t_ ## n ## _func(OPENPAM_UNUSED(char **desc), \
51 OPENPAM_UNUSED(void *arg))
54 t_add_test(&t_ ## n ## _func, NULL, "%s", t_ ## n ## _desc)
57 * Read a word from the temp file and verify that the result matches our
58 * expectations: whether a word was read at all, how many lines were read
59 * (in case of quoted or escaped newlines), whether we reached the end of
60 * the file and whether we reached the end of the line.
63 orw_expect(struct t_file *tf, const char *expected, int lines, int eof, int eol)
70 got = openpam_readword(tf->file, &lineno, &len);
73 err(1, "%s(): %s", __func__, tf->name);
74 if (expected != NULL && got == NULL) {
75 t_printv("expected <<%s>>, got nothing\n", expected);
77 } else if (expected == NULL && got != NULL) {
78 t_printv("expected nothing, got <<%s>>\n", got);
80 } else if (expected != NULL && got != NULL && strcmp(expected, got) != 0) {
81 t_printv("expected <<%s>>, got <<%s>>\n", expected, got);
85 if (lineno != lines) {
86 t_printv("expected to advance %d lines, advanced %d lines\n",
90 if (eof && !t_feof(tf)) {
91 t_printv("expected EOF, but didn't get it\n");
94 if (!eof && t_feof(tf)) {
95 t_printv("didn't expect EOF, but got it anyway\n");
100 err(1, "%s(): %s", __func__, tf->name);
101 if (eol && ch != '\n') {
102 t_printv("expected EOL, but didn't get it\n");
104 } else if (!eol && ch == '\n') {
105 t_printv("didn't expect EOL, but got it anyway\n");
109 ungetc(ch, tf->file);
114 /***************************************************************************
115 * Lines without words
118 T_FUNC(empty_input, "empty input")
124 ret = orw_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
129 T_FUNC(empty_line, "empty line")
137 ret = orw_expect(tf, NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
142 T_FUNC(unterminated_line, "unterminated line")
150 ret = orw_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
155 T_FUNC(single_whitespace, "single whitespace")
161 t_fprintf(tf, " \n");
163 ret = orw_expect(tf, NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
168 T_FUNC(multiple_whitespace, "multiple whitespace")
174 t_fprintf(tf, " \t\r\n");
176 ret = orw_expect(tf, NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
181 T_FUNC(comment, "comment")
187 t_fprintf(tf, "# comment\n");
189 ret = orw_expect(tf, NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
194 T_FUNC(whitespace_before_comment, "whitespace before comment")
200 t_fprintf(tf, " # comment\n");
202 ret = orw_expect(tf, NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
207 T_FUNC(single_quoted_comment, "single-quoted comment")
213 t_fprintf(tf, " '# comment'\n");
215 ret = orw_expect(tf, "# comment", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
220 T_FUNC(double_quoted_comment, "double-quoted comment")
226 t_fprintf(tf, " \"# comment\"\n");
228 ret = orw_expect(tf, "# comment", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
233 T_FUNC(comment_at_eof, "comment at end of file")
239 t_fprintf(tf, "# comment");
241 ret = orw_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
247 /***************************************************************************
248 * Simple cases - no quotes or escapes
251 T_FUNC(single_word, "single word")
253 const char *word = "hello";
258 t_fprintf(tf, "%s\n", word);
260 ret = orw_expect(tf, word, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
265 T_FUNC(single_whitespace_before_word, "single whitespace before word")
267 const char *word = "hello";
272 t_fprintf(tf, " %s\n", word);
274 ret = orw_expect(tf, word, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
279 T_FUNC(double_whitespace_before_word, "double whitespace before word")
281 const char *word = "hello";
286 t_fprintf(tf, " %s\n", word);
288 ret = orw_expect(tf, word, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
293 T_FUNC(single_whitespace_after_word, "single whitespace after word")
295 const char *word = "hello";
300 t_fprintf(tf, "%s \n", word);
302 ret = orw_expect(tf, word, 0 /*lines*/, 0 /*eof*/, 0 /*eol*/);
307 T_FUNC(double_whitespace_after_word, "double whitespace after word")
309 const char *word = "hello";
314 t_fprintf(tf, "%s \n", word);
316 ret = orw_expect(tf, word, 0 /*lines*/, 0 /*eof*/, 0 /*eol*/);
321 T_FUNC(comment_after_word, "comment after word")
323 const char *word = "hello";
328 t_fprintf(tf, "%s # comment\n", word);
330 ret = orw_expect(tf, word, 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
331 orw_expect(tf, NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
336 T_FUNC(word_containing_hash, "word containing hash")
338 const char *word = "hello#world";
343 t_fprintf(tf, "%s\n", word);
345 ret = orw_expect(tf, word, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
350 T_FUNC(two_words, "two words")
352 const char *word[] = { "hello", "world" };
357 t_fprintf(tf, "%s %s\n", word[0], word[1]);
359 ret = orw_expect(tf, word[0], 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
360 orw_expect(tf, word[1], 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
366 /***************************************************************************
370 T_FUNC(naked_escape, "naked escape")
378 ret = orw_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
383 T_FUNC(escaped_escape, "escaped escape")
389 t_fprintf(tf, "\\\\\n");
391 ret = orw_expect(tf, "\\", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
396 T_FUNC(escaped_whitespace, "escaped whitespace")
402 t_fprintf(tf, "\\ \\\t \\\r \\\n\n");
404 ret = orw_expect(tf, " ", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
405 orw_expect(tf, "\t", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
406 orw_expect(tf, "\r", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
407 /* this last one is a line continuation */
408 orw_expect(tf, NULL, 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
413 T_FUNC(escaped_newline_before_word, "escaped newline before word")
419 t_fprintf(tf, "\\\nhello world\n");
421 ret = orw_expect(tf, "hello", 1 /*lines*/, 0 /*eof*/, 0 /*eol*/);
426 T_FUNC(escaped_newline_within_word, "escaped newline within word")
432 t_fprintf(tf, "hello\\\nworld\n");
434 ret = orw_expect(tf, "helloworld", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
439 T_FUNC(escaped_newline_after_word, "escaped newline after word")
445 t_fprintf(tf, "hello\\\n world\n");
447 ret = orw_expect(tf, "hello", 1 /*lines*/, 0 /*eof*/, 0 /*eol*/);
452 T_FUNC(escaped_letter, "escaped letter")
458 t_fprintf(tf, "\\z\n");
460 ret = orw_expect(tf, "z", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
465 T_FUNC(escaped_comment, "escaped comment")
471 t_fprintf(tf, " \\# comment\n");
473 ret = orw_expect(tf, "#", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
474 orw_expect(tf, "comment", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
479 T_FUNC(escape_at_eof, "escape at end of file")
485 t_fprintf(tf, "z\\");
487 ret = orw_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
493 /***************************************************************************
497 T_FUNC(naked_single_quote, "naked single quote")
505 ret = orw_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
510 T_FUNC(naked_double_quote, "naked double quote")
518 ret = orw_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
523 T_FUNC(empty_single_quotes, "empty single quotes")
529 t_fprintf(tf, "''\n");
531 ret = orw_expect(tf, "", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
536 T_FUNC(empty_double_quotes, "empty double quotes")
542 t_fprintf(tf, "\"\"\n");
544 ret = orw_expect(tf, "", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
549 T_FUNC(single_quotes_within_double_quotes, "single quotes within double quotes")
555 t_fprintf(tf, "\"' '\"\n");
557 ret = orw_expect(tf, "' '", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
562 T_FUNC(double_quotes_within_single_quotes, "double quotes within single quotes")
568 t_fprintf(tf, "'\" \"'\n");
570 ret = orw_expect(tf, "\" \"", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
575 T_FUNC(single_quoted_whitespace, "single-quoted whitespace")
581 t_fprintf(tf, "' ' '\t' '\r' '\n'\n");
583 ret = orw_expect(tf, " ", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
584 orw_expect(tf, "\t", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
585 orw_expect(tf, "\r", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
586 orw_expect(tf, "\n", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
591 T_FUNC(double_quoted_whitespace, "double-quoted whitespace")
597 t_fprintf(tf, "\" \" \"\t\" \"\r\" \"\n\"\n");
599 ret = orw_expect(tf, " ", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
600 orw_expect(tf, "\t", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
601 orw_expect(tf, "\r", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
602 orw_expect(tf, "\n", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
607 T_FUNC(single_quoted_words, "single-quoted words")
613 t_fprintf(tf, "'hello world'\n");
615 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
620 T_FUNC(double_quoted_words, "double-quoted words")
626 t_fprintf(tf, "\"hello world\"\n");
628 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
634 /***************************************************************************
635 * Combinations of quoted and unquoted text
638 T_FUNC(single_quote_before_word, "single quote before word")
644 t_fprintf(tf, "'hello 'world\n");
646 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
651 T_FUNC(double_quote_before_word, "double quote before word")
657 t_fprintf(tf, "\"hello \"world\n");
659 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
664 T_FUNC(single_quote_within_word, "single quote within word")
670 t_fprintf(tf, "hello' 'world\n");
672 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
677 T_FUNC(double_quote_within_word, "double quote within word")
683 t_fprintf(tf, "hello\" \"world\n");
685 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
690 T_FUNC(single_quote_after_word, "single quote after word")
696 t_fprintf(tf, "hello' world'\n");
698 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
703 T_FUNC(double_quote_after_word, "double quote after word")
709 t_fprintf(tf, "hello\" world\"\n");
711 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
717 /***************************************************************************
718 * Combinations of escape and quotes
721 T_FUNC(escaped_single_quote,
722 "escaped single quote")
728 t_fprintf(tf, "\\'\n");
730 ret = orw_expect(tf, "'", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
735 T_FUNC(escaped_double_quote,
736 "escaped double quote")
742 t_fprintf(tf, "\\\"\n");
744 ret = orw_expect(tf, "\"", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
749 T_FUNC(escaped_whitespace_within_single_quotes,
750 "escaped whitespace within single quotes")
756 t_fprintf(tf, "'\\ ' '\\\t' '\\\r' '\\\n'\n");
758 ret = orw_expect(tf, "\\ ", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
759 orw_expect(tf, "\\\t", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
760 orw_expect(tf, "\\\r", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
761 orw_expect(tf, "\\\n", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
766 T_FUNC(escaped_whitespace_within_double_quotes,
767 "escaped whitespace within double quotes")
773 t_fprintf(tf, "\"\\ \" \"\\\t\" \"\\\r\" \"\\\n\"\n");
775 ret = orw_expect(tf, "\\ ", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
776 orw_expect(tf, "\\\t", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
777 orw_expect(tf, "\\\r", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
778 /* this last one is a line continuation */
779 orw_expect(tf, "", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
784 T_FUNC(escaped_letter_within_single_quotes,
785 "escaped letter within single quotes")
791 t_fprintf(tf, "'\\z'\n");
793 ret = orw_expect(tf, "\\z", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
798 T_FUNC(escaped_letter_within_double_quotes,
799 "escaped letter within double quotes")
805 t_fprintf(tf, "\"\\z\"\n");
807 ret = orw_expect(tf, "\\z", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
812 T_FUNC(escaped_escape_within_single_quotes,
813 "escaped escape within single quotes")
819 t_fprintf(tf, "'\\\\'\n");
821 ret = orw_expect(tf, "\\\\", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
826 T_FUNC(escaped_escape_within_double_quotes,
827 "escaped escape within double quotes")
833 t_fprintf(tf, "\"\\\\\"\n");
835 ret = orw_expect(tf, "\\", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
840 T_FUNC(escaped_single_quote_within_single_quotes,
841 "escaped single quote within single quotes")
847 t_fprintf(tf, "'\\''\n");
849 ret = orw_expect(tf, NULL, 1 /*lines*/, 1 /*eof*/, 0 /*eol*/);
854 T_FUNC(escaped_double_quote_within_single_quotes,
855 "escaped double quote within single quotes")
861 t_fprintf(tf, "'\\\"'\n");
863 ret = orw_expect(tf, "\\\"", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
868 T_FUNC(escaped_single_quote_within_double_quotes,
869 "escaped single quote within double quotes")
875 t_fprintf(tf, "\"\\'\"\n");
877 ret = orw_expect(tf, "\\'", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
882 T_FUNC(escaped_double_quote_within_double_quotes,
883 "escaped double quote within double quotes")
889 t_fprintf(tf, "\"\\\"\"\n");
891 ret = orw_expect(tf, "\"", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
897 /***************************************************************************
901 T_FUNC(line_continuation_within_whitespace, "line continuation within whitespace")
907 t_fprintf(tf, "hello \\\n world\n");
909 ret = orw_expect(tf, "hello", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
910 orw_expect(tf, "world", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
915 T_FUNC(line_continuation_before_whitespace, "line continuation before whitespace")
921 t_fprintf(tf, "hello\\\n world\n");
923 ret = orw_expect(tf, "hello", 1 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
924 orw_expect(tf, "world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
929 T_FUNC(line_continuation_after_whitespace, "line continuation after whitespace")
935 t_fprintf(tf, "hello \\\nworld\n");
937 ret = orw_expect(tf, "hello", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
938 orw_expect(tf, "world", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
943 T_FUNC(line_continuation_within_word, "line continuation within word")
949 t_fprintf(tf, "hello\\\nworld\n");
951 ret = orw_expect(tf, "helloworld", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
957 /***************************************************************************
962 t_prepare(int argc, char *argv[])
970 T(unterminated_line);
971 T(single_whitespace);
972 T(multiple_whitespace);
974 T(whitespace_before_comment);
975 T(single_quoted_comment);
976 T(double_quoted_comment);
980 T(single_whitespace_before_word);
981 T(double_whitespace_before_word);
982 T(single_whitespace_after_word);
983 T(double_whitespace_after_word);
984 T(comment_after_word);
985 T(word_containing_hash);
990 T(escaped_whitespace);
991 T(escaped_newline_before_word);
992 T(escaped_newline_within_word);
993 T(escaped_newline_after_word);
998 T(naked_single_quote);
999 T(naked_double_quote);
1000 T(empty_single_quotes);
1001 T(empty_double_quotes);
1002 T(single_quotes_within_double_quotes);
1003 T(double_quotes_within_single_quotes);
1004 T(single_quoted_whitespace);
1005 T(double_quoted_whitespace);
1006 T(single_quoted_words);
1007 T(double_quoted_words);
1009 T(single_quote_before_word);
1010 T(double_quote_before_word);
1011 T(single_quote_within_word);
1012 T(double_quote_within_word);
1013 T(single_quote_after_word);
1014 T(double_quote_after_word);
1016 T(escaped_single_quote);
1017 T(escaped_double_quote);
1018 T(escaped_whitespace_within_single_quotes);
1019 T(escaped_whitespace_within_double_quotes);
1020 T(escaped_letter_within_single_quotes);
1021 T(escaped_letter_within_double_quotes);
1022 T(escaped_escape_within_single_quotes);
1023 T(escaped_escape_within_double_quotes);
1024 T(escaped_single_quote_within_single_quotes);
1025 T(escaped_double_quote_within_single_quotes);
1026 T(escaped_single_quote_within_double_quotes);
1027 T(escaped_double_quote_within_double_quotes);
1029 T(line_continuation_within_whitespace);
1030 T(line_continuation_before_whitespace);
1031 T(line_continuation_after_whitespace);
1032 T(line_continuation_within_word);
1038 main(int argc, char *argv[])
1041 t_main(t_prepare, NULL, argc, argv);