xref: /netbsd-src/external/bsd/openpam/dist/t/t_openpam_readlinev.c (revision 0d9d0fd8a30be9a1924e715bbcf67a4a83efd262)
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 static const char *numbers[] = {
151 	"zero", "one", "two", "three", "four", "five", "six", "seven",
152 	"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
153 	"fifteen", "sixteen", "seventeen", "nineteen", "twenty",
154 	"twenty-one", "twenty-two", "twenty-three", "twenty-four",
155 	"twenty-five", "twenty-six", "twenty-seven", "twenty-eight",
156 	"twenty-nine", "thirty", "thirty-one", "thirty-two", "thirty-three",
157 	"thirty-four", "thirty-five", "thirty-six", "thirty-seven",
158 	"thirty-eight", "thirty-nine", "fourty", "fourty-one", "fourty-two",
159 	"fourty-three", "fourty-four", "fourty-five", "fourty-six",
160 	"fourty-seven", "fourty-eight", "fourty-nine", "fifty", "fifty-one",
161 	"fifty-two", "fifty-three", "fifty-four", "fifty-five", "fifty-six",
162 	"fifty-seven", "fifty-eight", "fifty-nine", "sixty", "sixty-one",
163 	"sixty-two", "sixty-three",
164 	NULL
165 };
166 
167 /***************************************************************************
168  * Lines without words
169  */
170 
171 T_FUNC(empty_input, "empty input")
172 {
173 	struct t_file *tf;
174 	int ret;
175 
176 	tf = t_fopen(NULL);
177 	ret = orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/);
178 	t_fclose(tf);
179 	return (ret);
180 }
181 
182 T_FUNC(empty_line, "empty line")
183 {
184 	struct t_file *tf;
185 	int ret;
186 
187 	tf = t_fopen(NULL);
188 	t_fprintf(tf, "\n");
189 	t_frewind(tf);
190 	ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
191 	t_fclose(tf);
192 	return (ret);
193 }
194 
195 T_FUNC(unterminated_empty_line, "unterminated empty line")
196 {
197 	struct t_file *tf;
198 	int ret;
199 
200 	tf = t_fopen(NULL);
201 	t_fprintf(tf, " ");
202 	t_frewind(tf);
203 	ret = orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/);
204 	t_fclose(tf);
205 	return (ret);
206 }
207 
208 T_FUNC(whitespace, "whitespace")
209 {
210 	struct t_file *tf;
211 	int ret;
212 
213 	tf = t_fopen(NULL);
214 	t_fprintf(tf, " \n");
215 	t_frewind(tf);
216 	ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
217 	t_fclose(tf);
218 	return (ret);
219 }
220 
221 T_FUNC(comment, "comment")
222 {
223 	struct t_file *tf;
224 	int ret;
225 
226 	tf = t_fopen(NULL);
227 	t_fprintf(tf, "# comment\n");
228 	t_frewind(tf);
229 	ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
230 	t_fclose(tf);
231 	return (ret);
232 }
233 
234 T_FUNC(whitespace_before_comment, "whitespace before comment")
235 {
236 	struct t_file *tf;
237 	int ret;
238 
239 	tf = t_fopen(NULL);
240 	t_fprintf(tf, " # comment\n");
241 	t_frewind(tf);
242 	ret = orlv_expect(tf, empty, 1 /*lines*/, 0 /*eof*/);
243 	t_fclose(tf);
244 	return (ret);
245 }
246 
247 T_FUNC(line_continuation_within_whitespace, "line continuation within whitespace")
248 {
249 	struct t_file *tf;
250 	int ret;
251 
252 	tf = t_fopen(NULL);
253 	t_fprintf(tf, "%s \\\n %s\n", hello_world[0], hello_world[1]);
254 	t_frewind(tf);
255 	ret = orlv_expect(tf, hello_world, 2 /*lines*/, 0 /*eof*/) &&
256 	    orlv_expect(tf, NULL, 0 /*lines*/, 1 /*eof*/);
257 	t_fclose(tf);
258 	return (ret);
259 }
260 
261 
262 /***************************************************************************
263  * Simple words
264  */
265 
266 T_FUNC(one_word, "one word")
267 {
268 	struct t_file *tf;
269 	int ret;
270 
271 	tf = t_fopen(NULL);
272 	t_fprintf(tf, "hello\n");
273 	t_frewind(tf);
274 	ret = orlv_expect(tf, hello, 1 /*lines*/, 0 /*eof*/);
275 	t_fclose(tf);
276 	return (ret);
277 }
278 
279 T_FUNC(two_words, "two words")
280 {
281 	struct t_file *tf;
282 	int ret;
283 
284 	tf = t_fopen(NULL);
285 	t_fprintf(tf, "hello world\n");
286 	t_frewind(tf);
287 	ret = orlv_expect(tf, hello_world, 1 /*lines*/, 0 /*eof*/);
288 	t_fclose(tf);
289 	return (ret);
290 }
291 
292 T_FUNC(many_words, "many words")
293 {
294 	struct t_file *tf;
295 	const char **word;
296 	int ret;
297 
298 	tf = t_fopen(NULL);
299 	for (word = numbers; *word; ++word)
300 		t_fprintf(tf, " %s", *word);
301 	t_fprintf(tf, "\n");
302 	t_frewind(tf);
303 	ret = orlv_expect(tf, numbers, 1 /*lines*/, 0 /*eof*/);
304 	t_fclose(tf);
305 	return (ret);
306 }
307 
308 T_FUNC(unterminated_line, "unterminated line")
309 {
310 	struct t_file *tf;
311 	int ret;
312 
313 	tf = t_fopen(NULL);
314 	t_fprintf(tf, "hello world");
315 	t_frewind(tf);
316 	ret = orlv_expect(tf, hello_world, 0 /*lines*/, 1 /*eof*/);
317 	t_fclose(tf);
318 	return (ret);
319 }
320 
321 
322 /***************************************************************************
323  * Boilerplate
324  */
325 
326 static int
t_prepare(int argc,char * argv[])327 t_prepare(int argc, char *argv[])
328 {
329 
330 	(void)argc;
331 	(void)argv;
332 
333 	T(empty_input);
334 	T(empty_line);
335 	T(unterminated_empty_line);
336 	T(whitespace);
337 	T(comment);
338 	T(whitespace_before_comment);
339 	T(line_continuation_within_whitespace);
340 
341 	T(one_word);
342 	T(two_words);
343 	T(many_words);
344 	T(unterminated_line);
345 
346 	return (0);
347 }
348 
349 int
main(int argc,char * argv[])350 main(int argc, char *argv[])
351 {
352 
353 	t_main(t_prepare, NULL, argc, argv);
354 }
355