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