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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "system/dir.h"
23 #include "libcli/libcli.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "system/time.h"
27 #include "auth/credentials/credentials.h"
28 #include "auth/gensec/gensec.h"
29 #include "param/param.h"
30 #include "dynconfig.h"
32 static struct cli_credentials *credentials;
33 static bool showall = false;
34 static bool old_list = false;
35 static const char *maskchars = "<>\"?*abc.";
36 static const char *filechars = "abcdefghijklm.";
38 static int die_on_error;
39 static int NumLoops = 0;
40 static int max_length = 20;
42 static bool reg_match_one(struct smbcli_state *cli, const char *pattern, const char *file)
44 /* oh what a weird world this is */
45 if (old_list && strcmp(pattern, "*.*") == 0) return true;
47 if (ISDOT(pattern)) return false;
49 if (ISDOTDOT(file)) file = ".";
51 return ms_fnmatch(pattern, file, cli->transport->negotiate.protocol)==0;
54 static char *reg_test(struct smbcli_state *cli, char *pattern, char *long_name, char *short_name)
59 pattern = 1+strrchr_m(pattern,'\\');
61 if (reg_match_one(cli, pattern, ".")) ret[0] = '+';
62 if (reg_match_one(cli, pattern, "..")) ret[1] = '+';
63 if (reg_match_one(cli, pattern, long_name) ||
64 (*short_name && reg_match_one(cli, pattern, short_name))) ret[2] = '+';
69 /*****************************************************
70 return a connection to a server
71 *******************************************************/
72 static struct smbcli_state *connect_one(char *share)
74 struct smbcli_state *c;
78 fstrcpy(server,share+2);
79 share = strchr_m(server,'\\');
80 if (!share) return NULL;
84 cli_credentials_set_workstation(credentials, "masktest", CRED_SPECIFIED);
86 status = smbcli_full_connection(NULL, &c,
91 if (!NT_STATUS_IS_OK(status)) {
103 static bool f_info_hit;
105 static void listfn(struct clilist_file_info *f, const char *s, void *state)
107 if (ISDOT(f->name)) {
109 } else if (ISDOTDOT(f->name)) {
114 pstrcpy(last_hit.long_name, f->name);
115 pstrcpy(last_hit.short_name, f->short_name);
119 static void get_real_name(struct smbcli_state *cli,
120 pstring long_name, fstring short_name)
123 if (cli->transport->negotiate.protocol <= PROTOCOL_LANMAN1) {
124 mask = "\\masktest\\*.*";
126 mask = "\\masktest\\*";
131 smbcli_list_new(cli->tree, mask,
132 FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
133 RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
137 fstrcpy(short_name, last_hit.short_name);
138 strlower(short_name);
139 pstrcpy(long_name, last_hit.long_name);
143 if (*short_name == 0) {
144 fstrcpy(short_name, long_name);
148 static void testpair(struct smbcli_state *cli, char *mask, char *file)
159 fstrcpy(res1, "---");
161 fnum = smbcli_open(cli->tree, file, O_CREAT|O_TRUNC|O_RDWR, 0);
163 DEBUG(0,("Can't create %s\n", file));
166 smbcli_close(cli->tree, fnum);
169 fstrcpy(short_name, "");
170 get_real_name(cli, long_name, short_name);
171 fstrcpy(res1, "---");
172 smbcli_list_new(cli->tree, mask,
173 FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
174 RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
177 res2 = reg_test(cli, mask, long_name, short_name);
179 if (showall || strcmp(res1, res2)) {
180 d_printf("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
181 res1, res2, count, mask, file, long_name, short_name);
182 if (die_on_error) exit(1);
185 smbcli_unlink(cli->tree, file);
187 if (count % 100 == 0) DEBUG(0,("%d\n", count));
192 static void test_mask(int argc, char *argv[],
193 struct smbcli_state *cli)
197 int mc_len = strlen(maskchars);
198 int fc_len = strlen(filechars);
200 smbcli_mkdir(cli->tree, "\\masktest");
202 smbcli_unlink(cli->tree, "\\masktest\\*");
206 pstrcpy(mask,"\\masktest\\");
207 pstrcpy(file,"\\masktest\\");
208 pstrcat(mask, argv[0]);
209 pstrcat(file, argv[1]);
210 testpair(cli, mask, file);
218 l1 = 1 + random() % max_length;
219 l2 = 1 + random() % max_length;
220 pstrcpy(mask,"\\masktest\\");
221 pstrcpy(file,"\\masktest\\");
224 mask[i+l] = maskchars[random() % mc_len];
229 file[i+l] = filechars[random() % fc_len];
233 if (ISDOT(file+l) || ISDOTDOT(file+l) || ISDOTDOT(mask+l)) {
237 if (strspn(file+l, ".") == strlen(file+l)) continue;
239 testpair(cli, mask, file);
240 if (NumLoops && (--NumLoops == 0))
245 smbcli_rmdir(cli->tree, "\\masktest");
249 static void usage(void)
253 masktest //server/share [options..]\n\
260 -l max test length\n\
262 -f filechars (default %s)\n\
263 -m maskchars (default %s)\n\
268 This program tests wildcard matching between two servers. It generates\n\
269 random pairs of filenames/masks and tests that they match in the same\n\
270 way on the servers and internally\n\
272 filechars, maskchars);
275 /****************************************************************************
277 ****************************************************************************/
278 int main(int argc,char *argv[])
281 struct smbcli_state *cli;
284 struct loadparm_context *lp_ctx;
288 setup_logging("masktest", DEBUG_STDOUT);
290 lp_set_cmdline(global_loadparm, "log level", "0");
292 if (argc < 2 || argv[1][0] == '-') {
299 all_string_sub(share,"/","\\",0);
301 setup_logging(argv[0], DEBUG_STDOUT);
306 lp_ctx = loadparm_init(talloc_autofree_context());
307 lp_load(lp_ctx, dyn_CONFIGFILE);
309 credentials = cli_credentials_init(talloc_autofree_context());
310 cli_credentials_guess(credentials, lp_ctx);
316 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEl:")) != EOF) {
319 NumLoops = atoi(optarg);
322 DEBUGLEVEL = atoi(optarg);
331 lp_set_cmdline(lp_ctx, "max protocol", optarg);
334 cli_credentials_parse_string(credentials, optarg, CRED_SPECIFIED);
346 max_length = atoi(optarg);
358 printf("Unknown option %c (%d)\n", (char)opt, opt);
368 cli = connect_one(share);
370 DEBUG(0,("Failed to connect to %s\n", share));
374 /* need to init seed after connect as clientgen uses random numbers */
375 DEBUG(0,("seed=%d format --- --- (server, correct)\n", seed));
378 test_mask(argc, argv, cli);