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.
22 #include "system/filesys.h"
23 #include "system/dir.h"
24 #include "libcli/libcli.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "system/time.h"
28 #include "auth/gensec/gensec.h"
30 static struct cli_credentials *credentials;
31 static BOOL showall = False;
32 static BOOL old_list = False;
33 static const char *maskchars = "<>\"?*abc.";
34 static const char *filechars = "abcdefghijklm.";
36 static int die_on_error;
37 static int NumLoops = 0;
38 static int max_length = 20;
40 static BOOL reg_match_one(struct smbcli_state *cli, const char *pattern, const char *file)
42 /* oh what a weird world this is */
43 if (old_list && strcmp(pattern, "*.*") == 0) return True;
45 if (ISDOT(pattern)) return False;
47 if (ISDOTDOT(file)) file = ".";
49 return ms_fnmatch(pattern, file, cli->transport->negotiate.protocol)==0;
52 static char *reg_test(struct smbcli_state *cli, char *pattern, char *long_name, char *short_name)
57 pattern = 1+strrchr_m(pattern,'\\');
59 if (reg_match_one(cli, pattern, ".")) ret[0] = '+';
60 if (reg_match_one(cli, pattern, "..")) ret[1] = '+';
61 if (reg_match_one(cli, pattern, long_name) ||
62 (*short_name && reg_match_one(cli, pattern, short_name))) ret[2] = '+';
67 /*****************************************************
68 return a connection to a server
69 *******************************************************/
70 static struct smbcli_state *connect_one(char *share)
72 struct smbcli_state *c;
76 fstrcpy(server,share+2);
77 share = strchr_m(server,'\\');
78 if (!share) return NULL;
82 cli_credentials_set_workstation(credentials, "masktest", CRED_SPECIFIED);
84 status = smbcli_full_connection(NULL, &c,
89 if (!NT_STATUS_IS_OK(status)) {
101 static BOOL f_info_hit;
103 static void listfn(struct clilist_file_info *f, const char *s, void *state)
105 if (ISDOT(f->name)) {
107 } else if (ISDOTDOT(f->name)) {
112 pstrcpy(last_hit.long_name, f->name);
113 pstrcpy(last_hit.short_name, f->short_name);
117 static void get_real_name(struct smbcli_state *cli,
118 pstring long_name, fstring short_name)
121 if (cli->transport->negotiate.protocol <= PROTOCOL_LANMAN1) {
122 mask = "\\masktest\\*.*";
124 mask = "\\masktest\\*";
129 smbcli_list_new(cli->tree, mask,
130 FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
131 RAW_SEARCH_BOTH_DIRECTORY_INFO,
135 fstrcpy(short_name, last_hit.short_name);
136 strlower(short_name);
137 pstrcpy(long_name, last_hit.long_name);
141 if (*short_name == 0) {
142 fstrcpy(short_name, long_name);
146 static void testpair(struct smbcli_state *cli, char *mask, char *file)
157 fstrcpy(res1, "---");
159 fnum = smbcli_open(cli->tree, file, O_CREAT|O_TRUNC|O_RDWR, 0);
161 DEBUG(0,("Can't create %s\n", file));
164 smbcli_close(cli->tree, fnum);
167 fstrcpy(short_name, "");
168 get_real_name(cli, long_name, short_name);
169 fstrcpy(res1, "---");
170 smbcli_list_new(cli->tree, mask,
171 FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
172 RAW_SEARCH_BOTH_DIRECTORY_INFO,
175 res2 = reg_test(cli, mask, long_name, short_name);
177 if (showall || strcmp(res1, res2)) {
178 d_printf("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
179 res1, res2, count, mask, file, long_name, short_name);
180 if (die_on_error) exit(1);
183 smbcli_unlink(cli->tree, file);
185 if (count % 100 == 0) DEBUG(0,("%d\n", count));
190 static void test_mask(int argc, char *argv[],
191 struct smbcli_state *cli)
195 int mc_len = strlen(maskchars);
196 int fc_len = strlen(filechars);
198 smbcli_mkdir(cli->tree, "\\masktest");
200 smbcli_unlink(cli->tree, "\\masktest\\*");
204 pstrcpy(mask,"\\masktest\\");
205 pstrcpy(file,"\\masktest\\");
206 pstrcat(mask, argv[0]);
207 pstrcat(file, argv[1]);
208 testpair(cli, mask, file);
216 l1 = 1 + random() % max_length;
217 l2 = 1 + random() % max_length;
218 pstrcpy(mask,"\\masktest\\");
219 pstrcpy(file,"\\masktest\\");
222 mask[i+l] = maskchars[random() % mc_len];
227 file[i+l] = filechars[random() % fc_len];
231 if (ISDOT(file+l) || ISDOTDOT(file+l) || ISDOTDOT(mask+l)) {
235 if (strspn(file+l, ".") == strlen(file+l)) continue;
237 testpair(cli, mask, file);
238 if (NumLoops && (--NumLoops == 0))
243 smbcli_rmdir(cli->tree, "\\masktest");
247 static void usage(void)
251 masktest //server/share [options..]\n\
258 -l max test length\n\
260 -f filechars (default %s)\n\
261 -m maskchars (default %s)\n\
266 This program tests wildcard matching between two servers. It generates\n\
267 random pairs of filenames/masks and tests that they match in the same\n\
268 way on the servers and internally\n\
270 filechars, maskchars);
273 /****************************************************************************
275 ****************************************************************************/
276 int main(int argc,char *argv[])
279 struct smbcli_state *cli;
285 setup_logging("masktest", DEBUG_STDOUT);
287 lp_set_cmdline("log level", "0");
289 if (argc < 2 || argv[1][0] == '-') {
296 all_string_sub(share,"/","\\",0);
298 setup_logging(argv[0], DEBUG_STDOUT);
305 credentials = cli_credentials_init(talloc_autofree_context());
306 cli_credentials_guess(credentials);
312 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEl:")) != EOF) {
315 NumLoops = atoi(optarg);
318 DEBUGLEVEL = atoi(optarg);
327 lp_set_cmdline("max protocol", optarg);
330 cli_credentials_parse_string(credentials, optarg, CRED_SPECIFIED);
342 max_length = atoi(optarg);
354 printf("Unknown option %c (%d)\n", (char)opt, opt);
364 cli = connect_one(share);
366 DEBUG(0,("Failed to connect to %s\n", share));
370 /* need to init seed after connect as clientgen uses random numbers */
371 DEBUG(0,("seed=%d format --- --- (server, correct)\n", seed));
374 test_mask(argc, argv, cli);