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