2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1999
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 static fstring password;
24 static fstring username;
25 static int max_protocol = PROTOCOL_NT1;
26 static BOOL showall = False;
27 static BOOL old_list = False;
28 static const char *maskchars = "<>\"?*abc.";
29 static const char *filechars = "abcdefghijklm.";
31 static int die_on_error;
32 static int NumLoops = 0;
34 /* a test fn for LANMAN mask support */
35 static int ms_fnmatch_lanman_core(const char *pattern, const char *string)
37 const char *p = pattern, *n = string;
40 if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
45 /* if (! *n && ! *p) goto match; */
46 if (*n != '.') goto nomatch;
51 if ((*n == '.' && n[1] != '.') || ! *n) goto next;
57 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
58 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
68 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
74 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
75 if (*n == '.' && !strchr_m(n+1,'.')) {
83 if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
84 if (*n != '.') goto nomatch;
89 if (c != *n) goto nomatch;
97 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
101 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
105 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
109 static int ms_fnmatch_lanman(const char *pattern, const char *string)
111 if (!strpbrk(pattern, "?*<>\"")) {
112 if (strcmp(string,"..") == 0)
115 return strcmp(pattern, string);
118 if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
119 return ms_fnmatch_lanman_core(pattern, "..") &&
120 ms_fnmatch_lanman_core(pattern, ".");
123 return ms_fnmatch_lanman_core(pattern, string);
126 static BOOL reg_match_one(struct cli_state *cli, const char *pattern, const char *file)
128 /* oh what a weird world this is */
129 if (old_list && strcmp(pattern, "*.*") == 0) return True;
131 if (strcmp(pattern,".") == 0) return False;
133 if (max_protocol <= PROTOCOL_LANMAN2) {
134 return ms_fnmatch_lanman(pattern, file)==0;
137 if (strcmp(file,"..") == 0) file = ".";
139 return ms_fnmatch(pattern, file, cli->transport->negotiate.protocol)==0;
142 static char *reg_test(struct cli_state *cli, char *pattern, char *long_name, char *short_name)
147 pattern = 1+strrchr_m(pattern,'\\');
149 if (reg_match_one(cli, pattern, ".")) ret[0] = '+';
150 if (reg_match_one(cli, pattern, "..")) ret[1] = '+';
151 if (reg_match_one(cli, pattern, long_name) ||
152 (*short_name && reg_match_one(cli, pattern, short_name))) ret[2] = '+';
157 /*****************************************************
158 return a connection to a server
159 *******************************************************/
160 static struct cli_state *connect_one(char *share)
167 fstrcpy(server,share+2);
168 share = strchr_m(server,'\\');
169 if (!share) return NULL;
173 status = cli_full_connection(&c, "masktest",
176 username, lp_workgroup(),
177 password, flags, NULL);
179 if (!NT_STATUS_IS_OK(status)) {
186 static char *resultp;
191 static BOOL f_info_hit;
193 static void listfn(file_info *f, const char *s, void *state)
195 if (strcmp(f->name,".") == 0) {
197 } else if (strcmp(f->name,"..") == 0) {
202 pstrcpy(last_hit.long_name, f->name);
203 pstrcpy(last_hit.short_name, f->short_name);
207 static void get_real_name(struct cli_state *cli,
208 pstring long_name, fstring short_name)
211 if (max_protocol <= PROTOCOL_LANMAN1) {
212 mask = "\\masktest\\*.*";
214 mask = "\\masktest\\*";
219 cli_list_new(cli, mask, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
223 fstrcpy(short_name, last_hit.short_name);
224 strlower(short_name);
225 pstrcpy(long_name, last_hit.long_name);
229 if (*short_name == 0) {
230 fstrcpy(short_name, long_name);
234 static void testpair(struct cli_state *cli, char *mask, char *file)
245 fstrcpy(res1, "---");
247 fnum = cli_open(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0);
249 DEBUG(0,("Can't create %s\n", file));
252 cli_close(cli, fnum);
255 fstrcpy(short_name, "");
256 get_real_name(cli, long_name, short_name);
257 fstrcpy(res1, "---");
258 cli_list(cli, mask, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
261 res2 = reg_test(cli, mask, long_name, short_name);
263 if (showall || strcmp(res1, res2)) {
264 d_printf("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
265 res1, res2, count, mask, file, long_name, short_name);
266 if (die_on_error) exit(1);
269 cli_unlink(cli, file);
271 if (count % 100 == 0) DEBUG(0,("%d\n", count));
274 static void test_mask(int argc, char *argv[],
275 struct cli_state *cli)
279 int mc_len = strlen(maskchars);
280 int fc_len = strlen(filechars);
282 cli_mkdir(cli, "\\masktest");
284 cli_unlink(cli, "\\masktest\\*");
288 pstrcpy(mask,"\\masktest\\");
289 pstrcpy(file,"\\masktest\\");
290 pstrcat(mask, argv[0]);
291 pstrcat(file, argv[1]);
292 testpair(cli, mask, file);
300 l1 = 1 + random() % 20;
301 l2 = 1 + random() % 20;
302 pstrcpy(mask,"\\masktest\\");
303 pstrcpy(file,"\\masktest\\");
306 mask[i+l] = maskchars[random() % mc_len];
311 file[i+l] = filechars[random() % fc_len];
315 if (strcmp(file+l,".") == 0 ||
316 strcmp(file+l,"..") == 0 ||
317 strcmp(mask+l,"..") == 0) continue;
319 if (strspn(file+l, ".") == strlen(file+l)) continue;
321 testpair(cli, mask, file);
322 if (NumLoops && (--NumLoops == 0))
327 cli_rmdir(cli, "\\masktest");
331 static void usage(void)
335 masktest //server/share [options..]\n\
343 -f filechars (default %s)\n\
344 -m maskchars (default %s)\n\
349 This program tests wildcard matching between two servers. It generates\n\
350 random pairs of filenames/masks and tests that they match in the same\n\
351 way on the servers and internally\n\
353 filechars, maskchars);
356 /****************************************************************************
358 ****************************************************************************/
359 int main(int argc,char *argv[])
362 struct cli_state *cli;
369 setup_logging("masktest", DEBUG_STDOUT);
371 lp_set_cmdline("log level", "0");
373 if (argc < 2 || argv[1][0] == '-') {
380 all_string_sub(share,"/","\\",0);
382 setup_logging(argv[0], DEBUG_STDOUT);
387 lp_load(dyn_CONFIGFILE,True,False,False);
390 if (getenv("USER")) {
391 fstrcpy(username,getenv("USER"));
396 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vE")) != EOF) {
399 NumLoops = atoi(optarg);
402 DEBUGLEVEL = atoi(optarg);
411 max_protocol = interpret_protocol(optarg, max_protocol);
414 fstrcpy(username,optarg);
415 p = strchr_m(username,'%');
418 fstrcpy(password, p+1);
440 printf("Unknown option %c (%d)\n", (char)opt, opt);
449 cli = connect_one(share);
451 DEBUG(0,("Failed to connect to %s\n", share));
455 /* need to init seed after connect as clientgen uses random numbers */
456 DEBUG(0,("seed=%d\n", seed));
459 test_mask(argc, argv, cli);