Annotation of embedaddon/sudo/plugins/sudoers/regress/parser/check_fill.c, revision 1.1.1.4
1.1 misho 1: /*
1.1.1.3 misho 2: * Copyright (c) 2011-2013 Todd C. Miller <Todd.Miller@courtesan.com>
1.1 misho 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 */
1.1.1.2 misho 29: #ifdef HAVE_STDBOOL_H
30: # include <stdbool.h>
31: #else
32: # include "compat/stdbool.h"
33: #endif /* HAVE_STDBOOL_H */
1.1 misho 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:
1.1.1.2 misho 43: #define SUDO_ERROR_WRAP 0
44:
1.1.1.3 misho 45: #include "missing.h"
1.1.1.4 ! misho 46: #include "queue.h"
1.1 misho 47: #include "parse.h"
48: #include "toke.h"
1.1.1.2 misho 49: #include "sudo_plugin.h"
1.1.1.4 ! misho 50: #include "sudo_util.h"
1.1.1.2 misho 51: #include <gram.h>
1.1 misho 52:
1.1.1.3 misho 53: __dso_public int main(int argc, char *argv[]);
54:
1.1 misho 55: /*
56: * TODO: test realloc
57: */
58:
1.1.1.3 misho 59: YYSTYPE sudoerslval;
1.1 misho 60:
61: struct fill_test {
62: const char *input;
63: const char *output;
64: int len;
65: int addspace;
66: };
67:
68: /*
69: * In "normal" fill, anything can be escaped and hex chars are expanded.
70: */
71: static struct fill_test txt_data[] = {
72: { "Embedded\\x20Space", "Embedded Space", 0 },
73: { "\\x20Leading", " Leading", 0 },
74: { "Trailing\\x20", "Trailing ", 0 },
75: { "Multiple\\x20\\x20Spaces", "Multiple Spaces", 0 },
76: { "Hexparse\\x200Check", "Hexparse 0Check", 0 },
77: { "Escaped\\\\Escape", "Escaped\\Escape", 0 },
78: { "LongGroupName", "LongGrou", 8 }
79: };
80:
81: /*
82: * The only escaped chars in a command should be [,:= \t#]
83: * The rest are done by glob() or fnmatch().
84: */
85: static struct fill_test cmd_data[] = {
86: { "foo\\,bar", "foo,bar", 0 },
87: { "this\\:that", "this:that", 0 },
88: { "foo\\=bar", "foo=bar", 0 },
89: { "tab\\\tstop", "tab\tstop", 0 },
90: { "not a \\#comment", "not a #comment", 0 }
91: };
92:
93: /*
94: * No escaped characters in command line args.
95: * Arguments get appended.
96: */
97: static struct fill_test args_data[] = {
98: { "/", "/", 0, 0 },
99: { "-type", "/ -type", 0, 1 },
100: { "f", "/ -type f", 0, 1 },
101: { "-exec", "/ -type f -exec", 0, 1 },
102: { "ls", "/ -type f -exec ls", 0, 1 },
103: { "{}", "/ -type f -exec ls {}", 0, 1 }
104: };
105:
106: static int
107: check_fill(const char *input, int len, int addspace, const char *expect, char **resultp)
108: {
109: if (!fill(input, len))
110: return -1;
1.1.1.3 misho 111: *resultp = sudoerslval.string;
112: return !strcmp(sudoerslval.string, expect);
1.1 misho 113: }
114:
115: static int
116: check_fill_cmnd(const char *input, int len, int addspace, const char *expect, char **resultp)
117: {
118: if (!fill_cmnd(input, len))
119: return -1;
1.1.1.3 misho 120: *resultp = sudoerslval.command.cmnd;
121: return !strcmp(sudoerslval.command.cmnd, expect);
1.1 misho 122: }
123:
124: static int
125: check_fill_args(const char *input, int len, int addspace, const char *expect, char **resultp)
126: {
127: if (!fill_args(input, len, addspace))
128: return -1;
1.1.1.3 misho 129: *resultp = sudoerslval.command.args;
130: return !strcmp(sudoerslval.command.args, expect);
1.1 misho 131: }
132:
133: static int
134: do_tests(int (*checker)(const char *, int, int, const char *, char **),
135: struct fill_test *data, size_t ntests)
136: {
1.1.1.4 ! misho 137: int len, errors = 0;
! 138: unsigned int i;
1.1 misho 139: char *result;
140:
141: for (i = 0; i < ntests; i++) {
142: if (data[i].len == 0)
143: len = strlen(data[i].input);
144: else
145: len = data[i].len;
146:
147: switch ((*checker)(data[i].input, len, data[i].addspace, data[i].output, &result)) {
148: case 0:
149: /* no match */
150: fprintf(stderr, "Failed parsing %.*s: expected [%s], got [%s]\n",
151: (int)data[i].len, data[i].input, data[i].output, result);
152: errors++;
153: break;
154: case 1:
155: /* match */
156: break;
157: default:
158: /* error */
159: fprintf(stderr, "Failed parsing %.*s: fill function failure\n",
160: (int)data[i].len, data[i].input);
161: errors++;
162: break;
163: }
164: }
165:
166: return errors;
167: }
168:
169: int
170: main(int argc, char *argv[])
171: {
172: int ntests, errors = 0;
173:
1.1.1.4 ! misho 174: initprogname(argc > 0 ? argv[0] : "check_fill");
! 175:
1.1 misho 176: errors += do_tests(check_fill, txt_data, sizeof(txt_data) / sizeof(txt_data[0]));
177: errors += do_tests(check_fill_cmnd, cmd_data, sizeof(cmd_data) / sizeof(cmd_data[0]));
178: errors += do_tests(check_fill_args, args_data, sizeof(args_data) / sizeof(args_data[0]));
179:
180: ntests = sizeof(txt_data) / sizeof(txt_data[0]) +
181: sizeof(cmd_data) / sizeof(cmd_data[0]) +
182: sizeof(args_data) / sizeof(args_data[0]);
1.1.1.4 ! misho 183: printf("%s: %d tests run, %d errors, %d%% success rate\n", getprogname(),
1.1 misho 184: ntests, errors, (ntests - errors) * 100 / ntests);
185:
186: exit(errors);
187: }
188:
189: /* STUB */
190: void
1.1.1.3 misho 191: sudoerserror(const char *s)
1.1 misho 192: {
193: return;
194: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>