r2787: force masktest to use RAW_SEARCH_BOTH_DIRECTORY_INFO so it can obtain the...
[ira/wip.git] / source / torture / masktest.c
1 /* 
2    Unix SMB/CIFS implementation.
3    mask_match tester
4    Copyright (C) Andrew Tridgell 1999
5    
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.
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    
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.
19 */
20
21 #include "includes.h"
22
23 static fstring password;
24 static fstring username;
25 static BOOL showall = False;
26 static BOOL old_list = False;
27 static const char *maskchars = "<>\"?*abc.";
28 static const char *filechars = "abcdefghijklm.";
29 static int verbose;
30 static int die_on_error;
31 static int NumLoops = 0;
32 static int max_length = 20;
33
34 static BOOL reg_match_one(struct smbcli_state *cli, const char *pattern, const char *file)
35 {
36         /* oh what a weird world this is */
37         if (old_list && strcmp(pattern, "*.*") == 0) return True;
38
39         if (strcmp(pattern,".") == 0) return False;
40
41         if (strcmp(file,"..") == 0) file = ".";
42
43         return ms_fnmatch(pattern, file, cli->transport->negotiate.protocol)==0;
44 }
45
46 static char *reg_test(struct smbcli_state *cli, char *pattern, char *long_name, char *short_name)
47 {
48         static fstring ret;
49         fstrcpy(ret, "---");
50
51         pattern = 1+strrchr_m(pattern,'\\');
52
53         if (reg_match_one(cli, pattern, ".")) ret[0] = '+';
54         if (reg_match_one(cli, pattern, "..")) ret[1] = '+';
55         if (reg_match_one(cli, pattern, long_name) || 
56             (*short_name && reg_match_one(cli, pattern, short_name))) ret[2] = '+';
57         return ret;
58 }
59
60
61 /***************************************************** 
62 return a connection to a server
63 *******************************************************/
64 static struct smbcli_state *connect_one(char *share)
65 {
66         struct smbcli_state *c;
67         fstring server;
68         uint_t flags = 0;
69         NTSTATUS status;
70
71         fstrcpy(server,share+2);
72         share = strchr_m(server,'\\');
73         if (!share) return NULL;
74         *share = 0;
75         share++;
76
77         status = smbcli_full_connection(NULL, &c, "masktest",
78                                      server, NULL, 
79                                      share, "?????", 
80                                      username, lp_workgroup(), 
81                                      password, flags, NULL);
82
83         if (!NT_STATUS_IS_OK(status)) {
84                 return NULL;
85         }
86
87         return c;
88 }
89
90 static char *resultp;
91 static struct {
92         pstring long_name;
93         pstring short_name;
94 } last_hit;
95 static BOOL f_info_hit;
96
97 static void listfn(file_info *f, const char *s, void *state)
98 {
99         if (strcmp(f->name,".") == 0) {
100                 resultp[0] = '+';
101         } else if (strcmp(f->name,"..") == 0) {
102                 resultp[1] = '+';               
103         } else {
104                 resultp[2] = '+';
105         }
106         pstrcpy(last_hit.long_name, f->name);
107         pstrcpy(last_hit.short_name, f->short_name);
108         f_info_hit = True;
109 }
110
111 static void get_real_name(struct smbcli_state *cli, 
112                           pstring long_name, fstring short_name)
113 {
114         const char *mask;
115         if (cli->transport->negotiate.protocol <= PROTOCOL_LANMAN1) {
116                 mask = "\\masktest\\*.*";
117         } else {
118                 mask = "\\masktest\\*";
119         }
120
121         f_info_hit = False;
122
123         smbcli_list_new(cli->tree, mask, 
124                         FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, 
125                         RAW_SEARCH_BOTH_DIRECTORY_INFO,
126                         listfn, NULL);
127
128         if (f_info_hit) {
129                 fstrcpy(short_name, last_hit.short_name);
130                 strlower(short_name);
131                 pstrcpy(long_name, last_hit.long_name);
132                 strlower(long_name);
133         }
134
135         if (*short_name == 0) {
136                 fstrcpy(short_name, long_name);
137         }
138 }
139
140 static void testpair(struct smbcli_state *cli, char *mask, char *file)
141 {
142         int fnum;
143         fstring res1;
144         char *res2;
145         static int count;
146         fstring short_name;
147         pstring long_name;
148
149         count++;
150
151         fstrcpy(res1, "---");
152
153         fnum = smbcli_open(cli->tree, file, O_CREAT|O_TRUNC|O_RDWR, 0);
154         if (fnum == -1) {
155                 DEBUG(0,("Can't create %s\n", file));
156                 return;
157         }
158         smbcli_close(cli->tree, fnum);
159
160         resultp = res1;
161         fstrcpy(short_name, "");
162         get_real_name(cli, long_name, short_name);
163         fstrcpy(res1, "---");
164         smbcli_list(cli->tree, mask, 
165                  FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, 
166                  listfn, NULL);
167
168         res2 = reg_test(cli, mask, long_name, short_name);
169
170         if (showall || strcmp(res1, res2)) {
171                 d_printf("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
172                          res1, res2, count, mask, file, long_name, short_name);
173                 if (die_on_error) exit(1);
174         }
175
176         smbcli_unlink(cli->tree, file);
177
178         if (count % 100 == 0) DEBUG(0,("%d\n", count));
179 }
180
181 static void test_mask(int argc, char *argv[], 
182                       struct smbcli_state *cli)
183 {
184         pstring mask, file;
185         int l1, l2, i, l;
186         int mc_len = strlen(maskchars);
187         int fc_len = strlen(filechars);
188
189         smbcli_mkdir(cli->tree, "\\masktest");
190
191         smbcli_unlink(cli->tree, "\\masktest\\*");
192
193         if (argc >= 2) {
194                 while (argc >= 2) {
195                         pstrcpy(mask,"\\masktest\\");
196                         pstrcpy(file,"\\masktest\\");
197                         pstrcat(mask, argv[0]);
198                         pstrcat(file, argv[1]);
199                         testpair(cli, mask, file);
200                         argv += 2;
201                         argc -= 2;
202                 }
203                 goto finished;
204         }
205
206         while (1) {
207                 l1 = 1 + random() % max_length;
208                 l2 = 1 + random() % max_length;
209                 pstrcpy(mask,"\\masktest\\");
210                 pstrcpy(file,"\\masktest\\");
211                 l = strlen(mask);
212                 for (i=0;i<l1;i++) {
213                         mask[i+l] = maskchars[random() % mc_len];
214                 }
215                 mask[l+l1] = 0;
216
217                 for (i=0;i<l2;i++) {
218                         file[i+l] = filechars[random() % fc_len];
219                 }
220                 file[l+l2] = 0;
221
222                 if (strcmp(file+l,".") == 0 || 
223                     strcmp(file+l,"..") == 0 ||
224                     strcmp(mask+l,"..") == 0) continue;
225
226                 if (strspn(file+l, ".") == strlen(file+l)) continue;
227
228                 testpair(cli, mask, file);
229                 if (NumLoops && (--NumLoops == 0))
230                         break;
231         }
232
233  finished:
234         smbcli_rmdir(cli->tree, "\\masktest");
235 }
236
237
238 static void usage(void)
239 {
240         printf(
241 "Usage:\n\
242   masktest //server/share [options..]\n\
243   options:\n\
244         -d debuglevel\n\
245         -n numloops\n\
246         -W workgroup\n\
247         -U user%%pass\n\
248         -s seed\n\
249         -l max test length\n\
250         -M max protocol\n\
251         -f filechars (default %s)\n\
252         -m maskchars (default %s)\n\
253         -v                             verbose mode\n\
254         -E                             die on error\n\
255         -a                             show all tests\n\
256 \n\
257   This program tests wildcard matching between two servers. It generates\n\
258   random pairs of filenames/masks and tests that they match in the same\n\
259   way on the servers and internally\n\
260 ", 
261   filechars, maskchars);
262 }
263
264 /****************************************************************************
265   main program
266 ****************************************************************************/
267  int main(int argc,char *argv[])
268 {
269         char *share;
270         struct smbcli_state *cli;       
271         int opt;
272         char *p;
273         int seed;
274
275         setlinebuf(stdout);
276
277         setup_logging("masktest", DEBUG_STDOUT);
278
279         lp_set_cmdline("log level", "0");
280
281         if (argc < 2 || argv[1][0] == '-') {
282                 usage();
283                 exit(1);
284         }
285
286         share = argv[1];
287
288         all_string_sub(share,"/","\\",0);
289
290         setup_logging(argv[0], DEBUG_STDOUT);
291
292         argc -= 1;
293         argv += 1;
294
295         lp_load(dyn_CONFIGFILE,True,False,False);
296         load_interfaces();
297
298         if (getenv("USER")) {
299                 fstrcpy(username,getenv("USER"));
300         }
301
302         seed = time(NULL);
303
304         while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEl:")) != EOF) {
305                 switch (opt) {
306                 case 'n':
307                         NumLoops = atoi(optarg);
308                         break;
309                 case 'd':
310                         DEBUGLEVEL = atoi(optarg);
311                         break;
312                 case 'E':
313                         die_on_error = 1;
314                         break;
315                 case 'v':
316                         verbose++;
317                         break;
318                 case 'M':
319                         lp_set_cmdline("max protocol", optarg);
320                         break;
321                 case 'U':
322                         fstrcpy(username,optarg);
323                         p = strchr_m(username,'%');
324                         if (p) {
325                                 *p = 0;
326                                 fstrcpy(password, p+1);
327                         }
328                         break;
329                 case 's':
330                         seed = atoi(optarg);
331                         break;
332                 case 'h':
333                         usage();
334                         exit(1);
335                 case 'm':
336                         maskchars = optarg;
337                         break;
338                 case 'l':
339                         max_length = atoi(optarg);
340                         break;
341                 case 'f':
342                         filechars = optarg;
343                         break;
344                 case 'a':
345                         showall = 1;
346                         break;
347                 case 'o':
348                         old_list = True;
349                         break;
350                 default:
351                         printf("Unknown option %c (%d)\n", (char)opt, opt);
352                         exit(1);
353                 }
354         }
355
356         argc -= optind;
357         argv += optind;
358
359
360         cli = connect_one(share);
361         if (!cli) {
362                 DEBUG(0,("Failed to connect to %s\n", share));
363                 exit(1);
364         }
365
366         /* need to init seed after connect as clientgen uses random numbers */
367         DEBUG(0,("seed=%d     format --- --- (server, correct)\n", seed));
368         srandom(seed);
369
370         test_mask(argc, argv, cli);
371
372         return(0);
373 }