Imported Upstream version 1.8.4p4
[debian/sudo] / plugins / sudoers / regress / parser / check_fill.c
1 /*
2  * Copyright (c) 2011 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <config.h>
18
19 #include <sys/types.h>
20 #include <stdio.h>
21 #ifdef STDC_HEADERS
22 # include <stdlib.h>
23 # include <stddef.h>
24 #else
25 # ifdef HAVE_STDLIB_H
26 #  include <stdlib.h>
27 # endif
28 #endif /* STDC_HEADERS */
29 #ifdef HAVE_STDBOOL_H
30 # include <stdbool.h>
31 #else
32 # include "compat/stdbool.h"
33 #endif /* HAVE_STDBOOL_H */
34 #ifdef HAVE_STRING_H
35 # include <string.h>
36 #endif /* HAVE_STRING_H */
37 #ifdef HAVE_STRINGS_H
38 # include <strings.h>
39 #endif /* HAVE_STRINGS_H */
40 #include <grp.h>
41 #include <pwd.h>
42
43 #define SUDO_ERROR_WRAP 0
44
45 #include "list.h"
46 #include "parse.h"
47 #include "toke.h"
48 #include "sudo_plugin.h"
49 #include <gram.h>
50
51 /*
52  * TODO: test realloc
53  */
54
55 sudo_conv_t sudo_conv;          /* NULL in non-plugin */
56
57 YYSTYPE yylval;
58
59 struct fill_test {
60     const char *input;
61     const char *output;
62     int len;
63     int addspace;
64 };
65
66 /*
67  * In "normal" fill, anything can be escaped and hex chars are expanded.
68  */
69 static struct fill_test txt_data[] = {
70     { "Embedded\\x20Space", "Embedded Space", 0 },
71     { "\\x20Leading", " Leading", 0 },
72     { "Trailing\\x20", "Trailing ", 0 },
73     { "Multiple\\x20\\x20Spaces", "Multiple  Spaces", 0 },
74     { "Hexparse\\x200Check", "Hexparse 0Check", 0 },
75     { "Escaped\\\\Escape", "Escaped\\Escape", 0 },
76     { "LongGroupName", "LongGrou", 8 }
77 };
78
79 /*
80  * The only escaped chars in a command should be [,:= \t#]
81  * The rest are done by glob() or fnmatch().
82  */
83 static struct fill_test cmd_data[] = {
84     { "foo\\,bar", "foo,bar", 0 },
85     { "this\\:that", "this:that", 0 },
86     { "foo\\=bar", "foo=bar", 0 },
87     { "tab\\\tstop", "tab\tstop", 0 },
88     { "not a \\#comment", "not a #comment", 0 }
89 };
90
91 /*
92  * No escaped characters in command line args.
93  * Arguments get appended.
94  */
95 static struct fill_test args_data[] = {
96     { "/", "/", 0, 0 },
97     { "-type", "/ -type", 0, 1 },
98     { "f", "/ -type f", 0, 1 },
99     { "-exec", "/ -type f -exec", 0, 1 },
100     { "ls", "/ -type f -exec ls", 0, 1 },
101     { "{}", "/ -type f -exec ls {}", 0, 1 }
102 };
103
104 static int
105 check_fill(const char *input, int len, int addspace, const char *expect, char **resultp)
106 {
107     if (!fill(input, len))
108         return -1;
109     *resultp = yylval.string;
110     return !strcmp(yylval.string, expect);
111 }
112
113 static int
114 check_fill_cmnd(const char *input, int len, int addspace, const char *expect, char **resultp)
115 {
116     if (!fill_cmnd(input, len))
117         return -1;
118     *resultp = yylval.command.cmnd;
119     return !strcmp(yylval.command.cmnd, expect);
120 }
121
122 static int
123 check_fill_args(const char *input, int len, int addspace, const char *expect, char **resultp)
124 {
125     if (!fill_args(input, len, addspace))
126         return -1;
127     *resultp = yylval.command.args;
128     return !strcmp(yylval.command.args, expect);
129 }
130
131 static int
132 do_tests(int (*checker)(const char *, int, int, const char *, char **),
133     struct fill_test *data, size_t ntests)
134 {
135     int i, len;
136     int errors = 0;
137     char *result;
138
139     for (i = 0; i < ntests; i++) {
140         if (data[i].len == 0)
141             len = strlen(data[i].input);
142         else
143             len = data[i].len;
144
145         switch ((*checker)(data[i].input, len, data[i].addspace, data[i].output, &result)) {
146         case 0:
147             /* no match */
148             fprintf(stderr, "Failed parsing %.*s: expected [%s], got [%s]\n",
149                 (int)data[i].len, data[i].input, data[i].output, result);
150             errors++;
151             break;
152         case 1:
153             /* match */
154             break;
155         default:
156             /* error */
157             fprintf(stderr, "Failed parsing %.*s: fill function failure\n",
158                 (int)data[i].len, data[i].input);
159             errors++;
160             break;
161         }
162     }
163
164     return errors;
165 }
166
167 int
168 main(int argc, char *argv[])
169 {
170     int ntests, errors = 0;
171
172     errors += do_tests(check_fill, txt_data, sizeof(txt_data) / sizeof(txt_data[0]));
173     errors += do_tests(check_fill_cmnd, cmd_data, sizeof(cmd_data) / sizeof(cmd_data[0]));
174     errors += do_tests(check_fill_args, args_data, sizeof(args_data) / sizeof(args_data[0]));
175
176     ntests = sizeof(txt_data) / sizeof(txt_data[0]) +
177         sizeof(cmd_data) / sizeof(cmd_data[0]) +
178         sizeof(args_data) / sizeof(args_data[0]);
179     printf("check_fill: %d tests run, %d errors, %d%% success rate\n",
180         ntests, errors, (ntests - errors) * 100 / ntests);
181
182     exit(errors);
183 }
184
185 /* STUB */
186 void
187 cleanup(int gotsig)
188 {
189     return;
190 }
191
192 /* STUB */
193 void
194 yyerror(const char *s)
195 {
196     return;
197 }