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