2 * Copyright (c) 2012-2015 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_readword.c 922 2017-02-19 19:28:30Z des $
42 #include <cryb/test.h>
44 #include <security/pam_appl.h>
45 #include <security/openpam.h>
47 #define T_FUNC(n, d) \
48 static const char *t_ ## n ## _desc = d; \
49 static int t_ ## n ## _func(OPENPAM_UNUSED(char **desc), \
50 OPENPAM_UNUSED(void *arg))
53 t_add_test(&t_ ## n ## _func, NULL, t_ ## n ## _desc)
56 * Read a word from the temp file and verify that the result matches our
57 * expectations: whether a word was read at all, how many lines were read
58 * (in case of quoted or escaped newlines), whether we reached the end of
59 * the file and whether we reached the end of the line.
62 orw_expect(struct t_file *tf, const char *expected, int lines, int eof, int eol)
69 got = openpam_readword(tf->file, &lineno, &len);
72 err(1, "%s(): %s", __func__, tf->name);
73 if (expected != NULL && got == NULL) {
74 t_verbose("expected <<%s>>, got nothing\n", expected);
76 } else if (expected == NULL && got != NULL) {
77 t_verbose("expected nothing, got <<%s>>\n", got);
79 } else if (expected != NULL && got != NULL && strcmp(expected, got) != 0) {
80 t_verbose("expected <<%s>>, got <<%s>>\n", expected, got);
84 if (lineno != lines) {
85 t_verbose("expected to advance %d lines, advanced %d lines\n",
89 if (eof && !t_feof(tf)) {
90 t_verbose("expected EOF, but didn't get it\n");
93 if (!eof && t_feof(tf)) {
94 t_verbose("didn't expect EOF, but got it anyway\n");
99 err(1, "%s(): %s", __func__, tf->name);
100 if (eol && ch != '\n') {
101 t_verbose("expected EOL, but didn't get it\n");
103 } else if (!eol && ch == '\n') {
104 t_verbose("didn't expect EOL, but got it anyway\n");
108 ungetc(ch, tf->file);
113 /***************************************************************************
114 * Lines without words
117 T_FUNC(empty_input, "empty input")
123 ret = orw_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
128 T_FUNC(empty_line, "empty line")
136 ret = orw_expect(tf, NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
141 T_FUNC(unterminated_line, "unterminated line")
149 ret = orw_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
154 T_FUNC(single_whitespace, "single whitespace")
160 t_fprintf(tf, " \n");
162 ret = orw_expect(tf, NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
167 T_FUNC(multiple_whitespace, "multiple whitespace")
173 t_fprintf(tf, " \t\r\n");
175 ret = orw_expect(tf, NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
180 T_FUNC(comment, "comment")
186 t_fprintf(tf, "# comment\n");
188 ret = orw_expect(tf, NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
193 T_FUNC(whitespace_before_comment, "whitespace before comment")
199 t_fprintf(tf, " # comment\n");
201 ret = orw_expect(tf, NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
206 T_FUNC(single_quoted_comment, "single-quoted comment")
212 t_fprintf(tf, " '# comment'\n");
214 ret = orw_expect(tf, "# comment", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
219 T_FUNC(double_quoted_comment, "double-quoted comment")
225 t_fprintf(tf, " \"# comment\"\n");
227 ret = orw_expect(tf, "# comment", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
232 T_FUNC(comment_at_eof, "comment at end of file")
238 t_fprintf(tf, "# comment");
240 ret = orw_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
246 /***************************************************************************
247 * Simple cases - no quotes or escapes
250 T_FUNC(single_word, "single word")
252 const char *word = "hello";
257 t_fprintf(tf, "%s\n", word);
259 ret = orw_expect(tf, word, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
264 T_FUNC(single_whitespace_before_word, "single whitespace before word")
266 const char *word = "hello";
271 t_fprintf(tf, " %s\n", word);
273 ret = orw_expect(tf, word, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
278 T_FUNC(double_whitespace_before_word, "double whitespace before word")
280 const char *word = "hello";
285 t_fprintf(tf, " %s\n", word);
287 ret = orw_expect(tf, word, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
292 T_FUNC(single_whitespace_after_word, "single whitespace after word")
294 const char *word = "hello";
299 t_fprintf(tf, "%s \n", word);
301 ret = orw_expect(tf, word, 0 /*lines*/, 0 /*eof*/, 0 /*eol*/);
306 T_FUNC(double_whitespace_after_word, "double whitespace after word")
308 const char *word = "hello";
313 t_fprintf(tf, "%s \n", word);
315 ret = orw_expect(tf, word, 0 /*lines*/, 0 /*eof*/, 0 /*eol*/);
320 T_FUNC(comment_after_word, "comment after word")
322 const char *word = "hello";
327 t_fprintf(tf, "%s # comment\n", word);
329 ret = orw_expect(tf, word, 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
330 orw_expect(tf, NULL, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
335 T_FUNC(word_containing_hash, "word containing hash")
337 const char *word = "hello#world";
342 t_fprintf(tf, "%s\n", word);
344 ret = orw_expect(tf, word, 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
349 T_FUNC(two_words, "two words")
351 const char *word[] = { "hello", "world" };
356 t_fprintf(tf, "%s %s\n", word[0], word[1]);
358 ret = orw_expect(tf, word[0], 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
359 orw_expect(tf, word[1], 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
365 /***************************************************************************
369 T_FUNC(naked_escape, "naked escape")
377 ret = orw_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
382 T_FUNC(escaped_escape, "escaped escape")
388 t_fprintf(tf, "\\\\\n");
390 ret = orw_expect(tf, "\\", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
395 T_FUNC(escaped_whitespace, "escaped whitespace")
401 t_fprintf(tf, "\\ \\\t \\\r \\\n\n");
403 ret = orw_expect(tf, " ", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
404 orw_expect(tf, "\t", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
405 orw_expect(tf, "\r", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
406 /* this last one is a line continuation */
407 orw_expect(tf, NULL, 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
412 T_FUNC(escaped_newline_before_word, "escaped newline before word")
418 t_fprintf(tf, "\\\nhello world\n");
420 ret = orw_expect(tf, "hello", 1 /*lines*/, 0 /*eof*/, 0 /*eol*/);
425 T_FUNC(escaped_newline_within_word, "escaped newline within word")
431 t_fprintf(tf, "hello\\\nworld\n");
433 ret = orw_expect(tf, "helloworld", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
438 T_FUNC(escaped_newline_after_word, "escaped newline after word")
444 t_fprintf(tf, "hello\\\n world\n");
446 ret = orw_expect(tf, "hello", 1 /*lines*/, 0 /*eof*/, 0 /*eol*/);
451 T_FUNC(escaped_letter, "escaped letter")
457 t_fprintf(tf, "\\z\n");
459 ret = orw_expect(tf, "z", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
464 T_FUNC(escaped_comment, "escaped comment")
470 t_fprintf(tf, " \\# comment\n");
472 ret = orw_expect(tf, "#", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
473 orw_expect(tf, "comment", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
478 T_FUNC(escape_at_eof, "escape at end of file")
484 t_fprintf(tf, "z\\");
486 ret = orw_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
492 /***************************************************************************
496 T_FUNC(naked_single_quote, "naked single quote")
504 ret = orw_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
509 T_FUNC(naked_double_quote, "naked double quote")
517 ret = orw_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/, 0 /*eol*/);
522 T_FUNC(empty_single_quotes, "empty single quotes")
528 t_fprintf(tf, "''\n");
530 ret = orw_expect(tf, "", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
535 T_FUNC(empty_double_quotes, "empty double quotes")
541 t_fprintf(tf, "\"\"\n");
543 ret = orw_expect(tf, "", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
548 T_FUNC(single_quotes_within_double_quotes, "single quotes within double quotes")
554 t_fprintf(tf, "\"' '\"\n");
556 ret = orw_expect(tf, "' '", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
561 T_FUNC(double_quotes_within_single_quotes, "double quotes within single quotes")
567 t_fprintf(tf, "'\" \"'\n");
569 ret = orw_expect(tf, "\" \"", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
574 T_FUNC(single_quoted_whitespace, "single-quoted whitespace")
580 t_fprintf(tf, "' ' '\t' '\r' '\n'\n");
582 ret = orw_expect(tf, " ", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
583 orw_expect(tf, "\t", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
584 orw_expect(tf, "\r", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
585 orw_expect(tf, "\n", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
590 T_FUNC(double_quoted_whitespace, "double-quoted whitespace")
596 t_fprintf(tf, "\" \" \"\t\" \"\r\" \"\n\"\n");
598 ret = orw_expect(tf, " ", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
599 orw_expect(tf, "\t", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
600 orw_expect(tf, "\r", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
601 orw_expect(tf, "\n", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
606 T_FUNC(single_quoted_words, "single-quoted words")
612 t_fprintf(tf, "'hello world'\n");
614 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
619 T_FUNC(double_quoted_words, "double-quoted words")
625 t_fprintf(tf, "\"hello world\"\n");
627 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
633 /***************************************************************************
634 * Combinations of quoted and unquoted text
637 T_FUNC(single_quote_before_word, "single quote before word")
643 t_fprintf(tf, "'hello 'world\n");
645 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
650 T_FUNC(double_quote_before_word, "double quote before word")
656 t_fprintf(tf, "\"hello \"world\n");
658 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
663 T_FUNC(single_quote_within_word, "single quote within word")
669 t_fprintf(tf, "hello' 'world\n");
671 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
676 T_FUNC(double_quote_within_word, "double quote within word")
682 t_fprintf(tf, "hello\" \"world\n");
684 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
689 T_FUNC(single_quote_after_word, "single quote after word")
695 t_fprintf(tf, "hello' world'\n");
697 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
702 T_FUNC(double_quote_after_word, "double quote after word")
708 t_fprintf(tf, "hello\" world\"\n");
710 ret = orw_expect(tf, "hello world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
716 /***************************************************************************
717 * Combinations of escape and quotes
720 T_FUNC(escaped_single_quote,
721 "escaped single quote")
727 t_fprintf(tf, "\\'\n");
729 ret = orw_expect(tf, "'", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
734 T_FUNC(escaped_double_quote,
735 "escaped double quote")
741 t_fprintf(tf, "\\\"\n");
743 ret = orw_expect(tf, "\"", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
748 T_FUNC(escaped_whitespace_within_single_quotes,
749 "escaped whitespace within single quotes")
755 t_fprintf(tf, "'\\ ' '\\\t' '\\\r' '\\\n'\n");
757 ret = orw_expect(tf, "\\ ", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
758 orw_expect(tf, "\\\t", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
759 orw_expect(tf, "\\\r", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
760 orw_expect(tf, "\\\n", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
765 T_FUNC(escaped_whitespace_within_double_quotes,
766 "escaped whitespace within double quotes")
772 t_fprintf(tf, "\"\\ \" \"\\\t\" \"\\\r\" \"\\\n\"\n");
774 ret = orw_expect(tf, "\\ ", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
775 orw_expect(tf, "\\\t", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
776 orw_expect(tf, "\\\r", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
777 /* this last one is a line continuation */
778 orw_expect(tf, "", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
783 T_FUNC(escaped_letter_within_single_quotes,
784 "escaped letter within single quotes")
790 t_fprintf(tf, "'\\z'\n");
792 ret = orw_expect(tf, "\\z", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
797 T_FUNC(escaped_letter_within_double_quotes,
798 "escaped letter within double quotes")
804 t_fprintf(tf, "\"\\z\"\n");
806 ret = orw_expect(tf, "\\z", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
811 T_FUNC(escaped_escape_within_single_quotes,
812 "escaped escape within single quotes")
818 t_fprintf(tf, "'\\\\'\n");
820 ret = orw_expect(tf, "\\\\", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
825 T_FUNC(escaped_escape_within_double_quotes,
826 "escaped escape within double quotes")
832 t_fprintf(tf, "\"\\\\\"\n");
834 ret = orw_expect(tf, "\\", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
839 T_FUNC(escaped_single_quote_within_single_quotes,
840 "escaped single quote within single quotes")
846 t_fprintf(tf, "'\\''\n");
848 ret = orw_expect(tf, NULL, 1 /*lines*/, 1 /*eof*/, 0 /*eol*/);
853 T_FUNC(escaped_double_quote_within_single_quotes,
854 "escaped double quote within single quotes")
860 t_fprintf(tf, "'\\\"'\n");
862 ret = orw_expect(tf, "\\\"", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
867 T_FUNC(escaped_single_quote_within_double_quotes,
868 "escaped single quote within double quotes")
874 t_fprintf(tf, "\"\\'\"\n");
876 ret = orw_expect(tf, "\\'", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
881 T_FUNC(escaped_double_quote_within_double_quotes,
882 "escaped double quote within double quotes")
888 t_fprintf(tf, "\"\\\"\"\n");
890 ret = orw_expect(tf, "\"", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
896 /***************************************************************************
900 T_FUNC(line_continuation_within_whitespace, "line continuation within whitespace")
906 t_fprintf(tf, "hello \\\n world\n");
908 ret = orw_expect(tf, "hello", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
909 orw_expect(tf, "world", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
914 T_FUNC(line_continuation_before_whitespace, "line continuation before whitespace")
920 t_fprintf(tf, "hello\\\n world\n");
922 ret = orw_expect(tf, "hello", 1 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
923 orw_expect(tf, "world", 0 /*lines*/, 0 /*eof*/, 1 /*eol*/);
928 T_FUNC(line_continuation_after_whitespace, "line continuation after whitespace")
934 t_fprintf(tf, "hello \\\nworld\n");
936 ret = orw_expect(tf, "hello", 0 /*lines*/, 0 /*eof*/, 0 /*eol*/) &&
937 orw_expect(tf, "world", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
942 T_FUNC(line_continuation_within_word, "line continuation within word")
948 t_fprintf(tf, "hello\\\nworld\n");
950 ret = orw_expect(tf, "helloworld", 1 /*lines*/, 0 /*eof*/, 1 /*eol*/);
956 /***************************************************************************
961 t_prepare(int argc, char *argv[])
969 T(unterminated_line);
970 T(single_whitespace);
971 T(multiple_whitespace);
973 T(whitespace_before_comment);
974 T(single_quoted_comment);
975 T(double_quoted_comment);
979 T(single_whitespace_before_word);
980 T(double_whitespace_before_word);
981 T(single_whitespace_after_word);
982 T(double_whitespace_after_word);
983 T(comment_after_word);
984 T(word_containing_hash);
989 T(escaped_whitespace);
990 T(escaped_newline_before_word);
991 T(escaped_newline_within_word);
992 T(escaped_newline_after_word);
997 T(naked_single_quote);
998 T(naked_double_quote);
999 T(empty_single_quotes);
1000 T(empty_double_quotes);
1001 T(single_quotes_within_double_quotes);
1002 T(double_quotes_within_single_quotes);
1003 T(single_quoted_whitespace);
1004 T(double_quoted_whitespace);
1005 T(single_quoted_words);
1006 T(double_quoted_words);
1008 T(single_quote_before_word);
1009 T(double_quote_before_word);
1010 T(single_quote_within_word);
1011 T(double_quote_within_word);
1012 T(single_quote_after_word);
1013 T(double_quote_after_word);
1015 T(escaped_single_quote);
1016 T(escaped_double_quote);
1017 T(escaped_whitespace_within_single_quotes);
1018 T(escaped_whitespace_within_double_quotes);
1019 T(escaped_letter_within_single_quotes);
1020 T(escaped_letter_within_double_quotes);
1021 T(escaped_escape_within_single_quotes);
1022 T(escaped_escape_within_double_quotes);
1023 T(escaped_single_quote_within_single_quotes);
1024 T(escaped_double_quote_within_single_quotes);
1025 T(escaped_single_quote_within_double_quotes);
1026 T(escaped_double_quote_within_double_quotes);
1028 T(line_continuation_within_whitespace);
1029 T(line_continuation_before_whitespace);
1030 T(line_continuation_after_whitespace);
1031 T(line_continuation_within_word);
1037 main(int argc, char *argv[])
1040 t_main(t_prepare, NULL, argc, argv);