In "ignoring unsafe symlink" messages, show only the file-list path.
[rsync/rsync.git] / wildtest.c
CommitLineData
5de45bca 1/*
0f78b815
WD
2 * Test suite for the wildmatch code.
3 *
b3bf9b9d 4 * Copyright (C) 2003-2009 Wayne Davison
0f78b815
WD
5 *
6 * This program is free software; you can redistribute it and/or modify
8e41b68e
WD
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
0f78b815
WD
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
e7c67065 16 * You should have received a copy of the GNU General Public License along
4fd842f9 17 * with this program; if not, visit the http://fsf.org website.
0f78b815 18 */
5de45bca 19
6cd50960 20/*#define COMPARE_WITH_FNMATCH*/
5de45bca 21
d5c973cc 22#define WILD_TEST_ITERATIONS
076f60ee
WD
23#include "lib/wildmatch.c"
24
a3869e92 25#include <popt.h>
076f60ee 26
5de45bca
WD
27#ifdef COMPARE_WITH_FNMATCH
28#include <fnmatch.h>
97d53f8c
WD
29
30int fnmatch_errors = 0;
5de45bca
WD
31#endif
32
97d53f8c 33int wildmatch_errors = 0;
adc2476f 34char number_separator = ',';
97d53f8c 35
5de45bca
WD
36typedef char bool;
37
d5c973cc 38int output_iterations = 0;
aec75b03
WD
39int explode_mod = 0;
40int empties_mod = 0;
41int empty_at_start = 0;
42int empty_at_end = 0;
076f60ee
WD
43
44static struct poptOption long_options[] = {
45 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
9a17dddb 46 {"iterations", 'i', POPT_ARG_NONE, &output_iterations, 0, 0, 0},
aec75b03
WD
47 {"empties", 'e', POPT_ARG_STRING, 0, 'e', 0, 0},
48 {"explode", 'x', POPT_ARG_INT, &explode_mod, 0, 0, 0},
076f60ee
WD
49 {0,0,0,0, 0, 0, 0}
50};
51
5de45bca
WD
52/* match just at the start of string (anchored tests) */
53static void
97d53f8c
WD
54run_test(int line, bool matches, bool same_as_fnmatch,
55 const char *text, const char *pattern)
5de45bca
WD
56{
57 bool matched;
58#ifdef COMPARE_WITH_FNMATCH
59 bool fn_matched;
60 int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
88085892
WD
61#else
62 same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */
5de45bca
WD
63#endif
64
aec75b03 65 if (explode_mod) {
75430208 66 char buf[MAXPATHLEN*2], *texts[MAXPATHLEN];
aec75b03
WD
67 int pos = 0, cnt = 0, ndx = 0, len = strlen(text);
68
aec75b03 69 if (empty_at_start)
75430208 70 texts[ndx++] = "";
aec75b03
WD
71 /* An empty string must turn into at least one empty array item. */
72 while (1) {
75430208
WD
73 texts[ndx] = buf + ndx * (explode_mod + 1);
74 strlcpy(texts[ndx++], text + pos, explode_mod + 1);
75 if (pos + explode_mod >= len)
aec75b03 76 break;
aec75b03
WD
77 pos += explode_mod;
78 if (!(++cnt % empties_mod))
75430208 79 texts[ndx++] = "";
aec75b03
WD
80 }
81 if (empty_at_end)
75430208 82 texts[ndx++] = "";
aec75b03
WD
83 texts[ndx] = NULL;
84 matched = wildmatch_array(pattern, (const char**)texts, 0);
aec75b03
WD
85 } else
86 matched = wildmatch(pattern, text);
5de45bca
WD
87#ifdef COMPARE_WITH_FNMATCH
88 fn_matched = !fnmatch(pattern, text, flags);
89#endif
90 if (matched != matches) {
97d53f8c
WD
91 printf("wildmatch failure on line %d:\n %s\n %s\n expected %s match\n",
92 line, text, pattern, matches? "a" : "NO");
93 wildmatch_errors++;
5de45bca
WD
94 }
95#ifdef COMPARE_WITH_FNMATCH
96 if (fn_matched != (matches ^ !same_as_fnmatch)) {
97d53f8c
WD
97 printf("fnmatch disagreement on line %d:\n %s\n %s\n expected %s match\n",
98 line, text, pattern, matches ^ !same_as_fnmatch? "a" : "NO");
99 fnmatch_errors++;
5de45bca
WD
100 }
101#endif
b6b42c89
WD
102 if (output_iterations) {
103 printf("%d: \"%s\" iterations = %d\n", line, pattern,
104 wildmatch_iteration_count);
105 }
5de45bca
WD
106}
107
108int
109main(int argc, char **argv)
110{
97d53f8c 111 char buf[2048], *s, *string[2], *end[2];
aec75b03 112 const char *arg;
97d53f8c
WD
113 FILE *fp;
114 int opt, line, i, flag[2];
076f60ee
WD
115 poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
116 long_options, 0);
117
118 while ((opt = poptGetNextOpt(pc)) != -1) {
119 switch (opt) {
aec75b03
WD
120 case 'e':
121 arg = poptGetOptArg(pc);
122 empties_mod = atoi(arg);
123 if (strchr(arg, 's'))
124 empty_at_start = 1;
125 if (strchr(arg, 'e'))
126 empty_at_end = 1;
127 if (!explode_mod)
128 explode_mod = 1024;
129 break;
076f60ee 130 default:
9a17dddb
WD
131 fprintf(stderr, "%s: %s\n",
132 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
133 poptStrerror(opt));
076f60ee
WD
134 exit(1);
135 }
136 }
5de45bca 137
aec75b03
WD
138 if (explode_mod && !empties_mod)
139 empties_mod = 1024;
140
c4cd2ca6
WD
141 argv = (char**)poptGetArgs(pc);
142 if (!argv || argv[1]) {
aec75b03 143 fprintf(stderr, "Usage: wildtest [OPTIONS] TESTFILE\n");
c4cd2ca6
WD
144 exit(1);
145 }
146
147 if ((fp = fopen(*argv, "r")) == NULL) {
148 fprintf(stderr, "Unable to open %s\n", *argv);
97d53f8c
WD
149 exit(1);
150 }
151
152 line = 0;
153 while (fgets(buf, sizeof buf, fp)) {
154 line++;
155 if (*buf == '#' || *buf == '\n')
156 continue;
157 for (s = buf, i = 0; i <= 1; i++) {
158 if (*s == '1')
159 flag[i] = 1;
160 else if (*s == '0')
161 flag[i] = 0;
162 else
163 flag[i] = -1;
164 if (*++s != ' ' && *s != '\t')
165 flag[i] = -1;
166 if (flag[i] < 0) {
c4cd2ca6
WD
167 fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s",
168 line, *argv, buf);
97d53f8c
WD
169 exit(1);
170 }
171 while (*++s == ' ' || *s == '\t') {}
172 }
173 for (i = 0; i <= 1; i++) {
174 if (*s == '\'' || *s == '"' || *s == '`') {
175 char quote = *s++;
176 string[i] = s;
177 while (*s && *s != quote) s++;
178 if (!*s) {
c4cd2ca6
WD
179 fprintf(stderr, "Unmatched quote on line %d of %s:\n%s",
180 line, *argv, buf);
97d53f8c
WD
181 exit(1);
182 }
183 end[i] = s;
184 }
185 else {
186 if (!*s || *s == '\n') {
c4cd2ca6
WD
187 fprintf(stderr, "Not enough strings on line %d of %s:\n%s",
188 line, *argv, buf);
97d53f8c
WD
189 exit(1);
190 }
191 string[i] = s;
192 while (*++s && *s != ' ' && *s != '\t' && *s != '\n') {}
193 end[i] = s;
194 }
195 while (*++s == ' ' || *s == '\t') {}
196 }
197 *end[0] = *end[1] = '\0';
198 run_test(line, flag[0], flag[1], string[0], string[1]);
199 }
200
201 if (!wildmatch_errors)
16859cdb 202 fputs("No", stdout);
97d53f8c 203 else
16859cdb
WD
204 printf("%d", wildmatch_errors);
205 printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s");
97d53f8c
WD
206
207#ifdef COMPARE_WITH_FNMATCH
208 if (!fnmatch_errors)
16859cdb 209 fputs("No", stdout);
97d53f8c 210 else
16859cdb
WD
211 printf("%d", fnmatch_errors);
212 printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s");
97d53f8c
WD
213
214#endif
5de45bca
WD
215
216 return 0;
217}