2 * Copyright (c) 2011 Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
19 #include <sys/types.h>
28 #endif /* STDC_HEADERS */
32 # include "compat/stdbool.h"
33 #endif /* HAVE_STDBOOL_H */
36 #endif /* HAVE_STRING_H */
39 #endif /* HAVE_STRINGS_H */
43 #define SUDO_ERROR_WRAP 0
48 #include "sudo_plugin.h"
55 sudo_conv_t sudo_conv; /* NULL in non-plugin */
67 * In "normal" fill, anything can be escaped and hex chars are expanded.
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 }
80 * The only escaped chars in a command should be [,:= \t#]
81 * The rest are done by glob() or fnmatch().
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 }
92 * No escaped characters in command line args.
93 * Arguments get appended.
95 static struct fill_test args_data[] = {
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 }
105 check_fill(const char *input, int len, int addspace, const char *expect, char **resultp)
107 if (!fill(input, len))
109 *resultp = yylval.string;
110 return !strcmp(yylval.string, expect);
114 check_fill_cmnd(const char *input, int len, int addspace, const char *expect, char **resultp)
116 if (!fill_cmnd(input, len))
118 *resultp = yylval.command.cmnd;
119 return !strcmp(yylval.command.cmnd, expect);
123 check_fill_args(const char *input, int len, int addspace, const char *expect, char **resultp)
125 if (!fill_args(input, len, addspace))
127 *resultp = yylval.command.args;
128 return !strcmp(yylval.command.args, expect);
132 do_tests(int (*checker)(const char *, int, int, const char *, char **),
133 struct fill_test *data, size_t ntests)
139 for (i = 0; i < ntests; i++) {
140 if (data[i].len == 0)
141 len = strlen(data[i].input);
145 switch ((*checker)(data[i].input, len, data[i].addspace, data[i].output, &result)) {
148 fprintf(stderr, "Failed parsing %.*s: expected [%s], got [%s]\n",
149 (int)data[i].len, data[i].input, data[i].output, result);
157 fprintf(stderr, "Failed parsing %.*s: fill function failure\n",
158 (int)data[i].len, data[i].input);
168 main(int argc, char *argv[])
170 int ntests, errors = 0;
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]));
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);
194 yyerror(const char *s)