1 /*-
2  * Copyright (c) 2012-2017 Dag-Erling Smørgrav
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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
15  *    permission.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33 
34 #include <err.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include <cryb/test.h>
41 
42 #include <security/pam_appl.h>
43 #include <security/openpam.h>
44 
45 #include "openpam_impl.h"
46 
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))
51 
52 #define T(n)								\
53 	t_add_test(&t_ ## n ## _func, NULL, "%s", t_ ## n ## _desc)
54 
55 /*
56  * Read a line from the temp file and verify that the result matches our
57  * expectations: whether a line was read at all, how many and which words
58  * it contained, how many lines were read (in case of quoted or escaped
59  * newlines) and whether we reached the end of the file.
60  */
61 static int
orlv_expect(struct t_file * tf,const char ** expectedv,int lines,int eof)62 orlv_expect(struct t_file *tf, const char **expectedv, int lines, int eof)
63 {
64 	int expectedc, gotc, i, lineno = 0;
65 	char **gotv;
66 	int ret;
67 
68 	ret = 1;
69 	expectedc = 0;
70 	if (expectedv != NULL)
71 		while (expectedv[expectedc] != NULL)
72 			++expectedc;
73 	gotv = openpam_readlinev(tf->file, &lineno, &gotc);
74 	if (t_ferror(tf))
75 		err(1, "%s(): %s", __func__, tf->name);
76 	if (expectedv != NULL && gotv == NULL) {
77 		t_printv("expected %d words, got nothing\n", expectedc);
78 		ret = 0;
79 	} else if (expectedv == NULL && gotv != NULL) {
80 		t_printv("expected nothing, got %d words\n", gotc);
81 		ret = 0;
82 	} else if (expectedv != NULL && gotv != NULL) {
83 		if (expectedc != gotc) {
84 			t_printv("expected %d words, got %d\n",
85 			    expectedc, gotc);
86 			ret = 0;
87 		}
88 		for (i = 0; i < gotc; ++i) {
89 			if (strcmp(expectedv[i], gotv[i]) != 0) {
90 				t_printv("word %d: expected <<%s>>, "
91 				    "got <<%s>>\n", i, expectedv[i], gotv[i]);
92 				ret = 0;
93 			}
94 		}
95 	}
96 	FREEV(gotc, gotv);
97 	if (lineno != lines) {
98 		t_printv("expected to advance %d lines, advanced %d lines\n",
99 		    lines, lineno);
100 		ret = 0;
101 	}
102 	if (eof && !t_feof(tf)) {
103 		t_printv("expected EOF, but didn't get it\n");
104 		ret = 0;
105 	} else if (!eof && t_feof(tf)) {
106 		t_printv("didn't expect EOF, but got it anyway\n");
107 		ret = 0;
108 	}
109 	return (ret);
110 }
111 
112 
113 /***************************************************************************
114  * Commonly-used lines
115  */
116 
117 static const char *empty[] = {
118 	NULL
119 };
120 
121 static const char *hello[] = {
122 	"hello",
123 	NULL
124 };
125 
126 static const char *hello_world[] = {
127 	"hello",
128 	"world",
129 	NULL
130 };
131 
132 static const char *numbers[] = {
133 	"zero", "one", "two", "three", "four", "five", "six", "seven",
134 	"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
135 	"fifteen", "sixteen", "seventeen", "nineteen", "twenty",
136 	"twenty-one", "twenty-two", "twenty-three", "twenty-four",
137 	"twenty-five", "twenty-six", "twenty-seven", "twenty-eight",
138 	"twenty-nine", "thirty", "thirty-one", "thirty-two", "thirty-three",
139 	"thirty-four", "thirty-five", "thirty-six", "thirty-seven",
140 	"thirty-eight", "thirty-nine", "fourty", "fourty-one", "fourty-two",
141 	"fourty-three", "fourty-four", "fourty-five", "fourty-six",
142 	"fourty-seven", "fourty-eight", "fourty-nine", "fifty", "fifty-one",
143 	"fifty-two", "fifty-three", "fifty-four", "fifty-five", "fifty-six",
144 	"fifty-seven", "fifty-eight", "fifty-nine", "sixty", "sixty-one",
145 	"sixty-two", "sixty-three",
146 	NULL
147 };
148 
149 
150 /***************************************************************************
151  * Lines without words
152  */
153 
154 T_FUNC(empty_input, "empty input")
155 {
156 	struct t_file *tf;
157 	int ret;
158 
159 	tf = t_fopen(NULL);
160 	ret = orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/);
161 	t_fclose(tf);
162 	return (ret);
163 }
164 
165 T_FUNC(empty_line, "empty line")
166 {
167 	struct t_file *tf;
168 	int ret;
169 
170 	tf = t_fopen(NULL);
171 	t_fprintf(tf, "\n");
172 	t_frewind(tf);
173 	ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
174 	t_fclose(tf);
175 	return (ret);
176 }
177 
178 T_FUNC(unterminated_empty_line, "unterminated empty line")
179 {
180 	struct t_file *tf;
181 	int ret;
182 
183 	tf = t_fopen(NULL);
184 	t_fprintf(tf, " ");
185 	t_frewind(tf);
186 	ret = orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/);
187 	t_fclose(tf);
188 	return (ret);
189 }
190 
191 T_FUNC(whitespace, "whitespace")
192 {
193 	struct t_file *tf;
194 	int ret;
195 
196 	tf = t_fopen(NULL);
197 	t_fprintf(tf, " \n");
198 	t_frewind(tf);
199 	ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
200 	t_fclose(tf);
201 	return (ret);
202 }
203 
204 T_FUNC(comment, "comment")
205 {
206 	struct t_file *tf;
207 	int ret;
208 
209 	tf = t_fopen(NULL);
210 	t_fprintf(tf, "# comment\n");
211 	t_frewind(tf);
212 	ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
213 	t_fclose(tf);
214 	return (ret);
215 }
216 
217 T_FUNC(whitespace_before_comment, "whitespace before comment")
218 {
219 	struct t_file *tf;
220 	int ret;
221 
222 	tf = t_fopen(NULL);
223 	t_fprintf(tf, " # comment\n");
224 	t_frewind(tf);
225 	ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
226 	t_fclose(tf);
227 	return (ret);
228 }
229 
230 T_FUNC(line_continuation_within_whitespace, "line continuation within whitespace")
231 {
232 	struct t_file *tf;
233 	int ret;
234 
235 	tf = t_fopen(NULL);
236 	t_fprintf(tf, "%s \\\n %s\n", hello_world[0], hello_world[1]);
237 	t_frewind(tf);
238 	ret = orlv_expect(tf, hello_world, 2 /*lines*/, 0 /*eof*/) &&
239 	    orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/);
240 	t_fclose(tf);
241 	return (ret);
242 }
243 
244 
245 /***************************************************************************
246  * Simple words
247  */
248 
249 T_FUNC(one_word, "one word")
250 {
251 	struct t_file *tf;
252 	int ret;
253 
254 	tf = t_fopen(NULL);
255 	t_fprintf(tf, "hello\n");
256 	t_frewind(tf);
257 	ret = orlv_expect(tf, hello, 1 /*lines*/, 0 /*eof*/);
258 	t_fclose(tf);
259 	return (ret);
260 }
261 
262 T_FUNC(two_words, "two words")
263 {
264 	struct t_file *tf;
265 	int ret;
266 
267 	tf = t_fopen(NULL);
268 	t_fprintf(tf, "hello world\n");
269 	t_frewind(tf);
270 	ret = orlv_expect(tf, hello_world, 1 /*lines*/, 0 /*eof*/);
271 	t_fclose(tf);
272 	return (ret);
273 }
274 
275 T_FUNC(many_words, "many words")
276 {
277 	struct t_file *tf;
278 	const char **word;
279 	int ret;
280 
281 	tf = t_fopen(NULL);
282 	for (word = numbers; *word; ++word)
283 		t_fprintf(tf, " %s", *word);
284 	t_fprintf(tf, "\n");
285 	t_frewind(tf);
286 	ret = orlv_expect(tf, numbers, 1 /*lines*/, 0 /*eof*/);
287 	t_fclose(tf);
288 	return (ret);
289 }
290 
291 T_FUNC(unterminated_line, "unterminated line")
292 {
293 	struct t_file *tf;
294 	int ret;
295 
296 	tf = t_fopen(NULL);
297 	t_fprintf(tf, "hello world");
298 	t_frewind(tf);
299 	ret = orlv_expect(tf, hello_world, 0 /*lines*/, 1 /*eof*/);
300 	t_fclose(tf);
301 	return (ret);
302 }
303 
304 
305 /***************************************************************************
306  * Boilerplate
307  */
308 
309 static int
t_prepare(int argc,char * argv[])310 t_prepare(int argc, char *argv[])
311 {
312 
313 	(void)argc;
314 	(void)argv;
315 
316 	T(empty_input);
317 	T(empty_line);
318 	T(unterminated_empty_line);
319 	T(whitespace);
320 	T(comment);
321 	T(whitespace_before_comment);
322 	T(line_continuation_within_whitespace);
323 
324 	T(one_word);
325 	T(two_words);
326 	T(many_words);
327 	T(unterminated_line);
328 
329 	return (0);
330 }
331 
332 int
main(int argc,char * argv[])333 main(int argc, char *argv[])
334 {
335 
336 	t_main(t_prepare, NULL, argc, argv);
337 }
338