source: openpam/trunk/t/t_openpam_readlinev.c @ 835

Last change on this file since 835 was 835, checked in by Dag-Erling Smørgrav, 6 years ago

Add a test case for line continuation within whitespace, similar to
the corresponding test in t_openpam_readword.

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