2 * Copyright (c) 2011-2013 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
49 #include "sudo_plugin.h"
52 __dso_public int main(int argc, char *argv[]);
68 * In "normal" fill, anything can be escaped and hex chars are expanded.
70 static struct fill_test txt_data[] = {
71 { "Embedded\\x20Space", "Embedded Space", 0 },
72 { "\\x20Leading", " Leading", 0 },
73 { "Trailing\\x20", "Trailing ", 0 },
74 { "Multiple\\x20\\x20Spaces", "Multiple Spaces", 0 },
75 { "Hexparse\\x200Check", "Hexparse 0Check", 0 },
76 { "Escaped\\\\Escape", "Escaped\\Escape", 0 },
77 { "LongGroupName", "LongGrou", 8 }
81 * The only escaped chars in a command should be [,:= \t#]
82 * The rest are done by glob() or fnmatch().
84 static struct fill_test cmd_data[] = {
85 { "foo\\,bar", "foo,bar", 0 },
86 { "this\\:that", "this:that", 0 },
87 { "foo\\=bar", "foo=bar", 0 },
88 { "tab\\\tstop", "tab\tstop", 0 },
89 { "not a \\#comment", "not a #comment", 0 }
93 * No escaped characters in command line args.
94 * Arguments get appended.
96 static struct fill_test args_data[] = {
98 { "-type", "/ -type", 0, 1 },
99 { "f", "/ -type f", 0, 1 },
100 { "-exec", "/ -type f -exec", 0, 1 },
101 { "ls", "/ -type f -exec ls", 0, 1 },
102 { "{}", "/ -type f -exec ls {}", 0, 1 }
106 check_fill(const char *input, int len, int addspace, const char *expect, char **resultp)
108 if (!fill(input, len))
110 *resultp = sudoerslval.string;
111 return !strcmp(sudoerslval.string, expect);
115 check_fill_cmnd(const char *input, int len, int addspace, const char *expect, char **resultp)
117 if (!fill_cmnd(input, len))
119 *resultp = sudoerslval.command.cmnd;
120 return !strcmp(sudoerslval.command.cmnd, expect);
124 check_fill_args(const char *input, int len, int addspace, const char *expect, char **resultp)
126 if (!fill_args(input, len, addspace))
128 *resultp = sudoerslval.command.args;
129 return !strcmp(sudoerslval.command.args, expect);
133 do_tests(int (*checker)(const char *, int, int, const char *, char **),
134 struct fill_test *data, size_t ntests)
140 for (i = 0; i < ntests; i++) {
141 if (data[i].len == 0)
142 len = strlen(data[i].input);
146 switch ((*checker)(data[i].input, len, data[i].addspace, data[i].output, &result)) {
149 fprintf(stderr, "Failed parsing %.*s: expected [%s], got [%s]\n",
150 (int)data[i].len, data[i].input, data[i].output, result);
158 fprintf(stderr, "Failed parsing %.*s: fill function failure\n",
159 (int)data[i].len, data[i].input);
169 main(int argc, char *argv[])
171 int ntests, errors = 0;
173 errors += do_tests(check_fill, txt_data, sizeof(txt_data) / sizeof(txt_data[0]));
174 errors += do_tests(check_fill_cmnd, cmd_data, sizeof(cmd_data) / sizeof(cmd_data[0]));
175 errors += do_tests(check_fill_args, args_data, sizeof(args_data) / sizeof(args_data[0]));
177 ntests = sizeof(txt_data) / sizeof(txt_data[0]) +
178 sizeof(cmd_data) / sizeof(cmd_data[0]) +
179 sizeof(args_data) / sizeof(args_data[0]);
180 printf("check_fill: %d tests run, %d errors, %d%% success rate\n",
181 ntests, errors, (ntests - errors) * 100 / ntests);
188 sudoerserror(const char *s)