r3494: got rid of include/rewrite.h, and split out the dynconfig.h header
[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 #include "dynconfig.h"
23 #include "client.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         uint_t flags = 0;
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         status = smbcli_full_connection(NULL, &c, "masktest",
82                                      server, NULL, 
83                                      share, "?????", 
84                                      username, lp_workgroup(), 
85                                      password, flags, NULL);
86
87         if (!NT_STATUS_IS_OK(status)) {
88                 return NULL;
89         }
90
91         return c;
92 }
93
94 static char *resultp;
95 static struct {
96         pstring long_name;
97         pstring short_name;
98 } last_hit;
99 static BOOL f_info_hit;
100
101 static void listfn(struct file_info *f, const char *s, void *state)
102 {
103         if (strcmp(f->name,".") == 0) {
104                 resultp[0] = '+';
105         } else if (strcmp(f->name,"..") == 0) {
106                 resultp[1] = '+';               
107         } else {
108                 resultp[2] = '+';
109         }
110         pstrcpy(last_hit.long_name, f->name);
111         pstrcpy(last_hit.short_name, f->short_name);
112         f_info_hit = True;
113 }
114
115 static void get_real_name(struct smbcli_state *cli, 
116                           pstring long_name, fstring short_name)
117 {
118         const char *mask;
119         if (cli->transport->negotiate.protocol <= PROTOCOL_LANMAN1) {
120                 mask = "\\masktest\\*.*";
121         } else {
122                 mask = "\\masktest\\*";
123         }
124
125         f_info_hit = False;
126
127         smbcli_list_new(cli->tree, mask, 
128                         FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, 
129                         RAW_SEARCH_BOTH_DIRECTORY_INFO,
130                         listfn, NULL);
131
132         if (f_info_hit) {
133                 fstrcpy(short_name, last_hit.short_name);
134                 strlower(short_name);
135                 pstrcpy(long_name, last_hit.long_name);
136                 strlower(long_name);
137         }
138
139         if (*short_name == 0) {
140                 fstrcpy(short_name, long_name);
141         }
142 }
143
144 static void testpair(struct smbcli_state *cli, char *mask, char *file)
145 {
146         int fnum;
147         fstring res1;
148         char *res2;
149         static int count;
150         fstring short_name;
151         pstring long_name;
152
153         count++;
154
155         fstrcpy(res1, "---");
156
157         fnum = smbcli_open(cli->tree, file, O_CREAT|O_TRUNC|O_RDWR, 0);
158         if (fnum == -1) {
159                 DEBUG(0,("Can't create %s\n", file));
160                 return;
161         }
162         smbcli_close(cli->tree, fnum);
163
164         resultp = res1;
165         fstrcpy(short_name, "");
166         get_real_name(cli, long_name, short_name);
167         fstrcpy(res1, "---");
168         smbcli_list_new(cli->tree, mask, 
169                         FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, 
170                         RAW_SEARCH_BOTH_DIRECTORY_INFO,
171                         listfn, NULL);
172
173         res2 = reg_test(cli, mask, long_name, short_name);
174
175         if (showall || strcmp(res1, res2)) {
176                 d_printf("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
177                          res1, res2, count, mask, file, long_name, short_name);
178                 if (die_on_error) exit(1);
179         }
180
181         smbcli_unlink(cli->tree, file);
182
183         if (count % 100 == 0) DEBUG(0,("%d\n", count));
184 }
185
186 static void test_mask(int argc, char *argv[], 
187                       struct smbcli_state *cli)
188 {
189         pstring mask, file;
190         int l1, l2, i, l;
191         int mc_len = strlen(maskchars);
192         int fc_len = strlen(filechars);
193
194         smbcli_mkdir(cli->tree, "\\masktest");
195
196         smbcli_unlink(cli->tree, "\\masktest\\*");
197
198         if (argc >= 2) {
199                 while (argc >= 2) {
200                         pstrcpy(mask,"\\masktest\\");
201                         pstrcpy(file,"\\masktest\\");
202                         pstrcat(mask, argv[0]);
203                         pstrcat(file, argv[1]);
204                         testpair(cli, mask, file);
205                         argv += 2;
206                         argc -= 2;
207                 }
208                 goto finished;
209         }
210
211         while (1) {
212                 l1 = 1 + random() % max_length;
213                 l2 = 1 + random() % max_length;
214                 pstrcpy(mask,"\\masktest\\");
215                 pstrcpy(file,"\\masktest\\");
216                 l = strlen(mask);
217                 for (i=0;i<l1;i++) {
218                         mask[i+l] = maskchars[random() % mc_len];
219                 }
220                 mask[l+l1] = 0;
221
222                 for (i=0;i<l2;i++) {
223                         file[i+l] = filechars[random() % fc_len];
224                 }
225                 file[l+l2] = 0;
226
227                 if (strcmp(file+l,".") == 0 || 
228                     strcmp(file+l,"..") == 0 ||
229                     strcmp(mask+l,"..") == 0) continue;
230
231                 if (strspn(file+l, ".") == strlen(file+l)) continue;
232
233                 testpair(cli, mask, file);
234                 if (NumLoops && (--NumLoops == 0))
235                         break;
236         }
237
238  finished:
239         smbcli_rmdir(cli->tree, "\\masktest");
240 }
241
242
243 static void usage(void)
244 {
245         printf(
246 "Usage:\n\
247   masktest //server/share [options..]\n\
248   options:\n\
249         -d debuglevel\n\
250         -n numloops\n\
251         -W workgroup\n\
252         -U user%%pass\n\
253         -s seed\n\
254         -l max test length\n\
255         -M max protocol\n\
256         -f filechars (default %s)\n\
257         -m maskchars (default %s)\n\
258         -v                             verbose mode\n\
259         -E                             die on error\n\
260         -a                             show all tests\n\
261 \n\
262   This program tests wildcard matching between two servers. It generates\n\
263   random pairs of filenames/masks and tests that they match in the same\n\
264   way on the servers and internally\n\
265 ", 
266   filechars, maskchars);
267 }
268
269 /****************************************************************************
270   main program
271 ****************************************************************************/
272  int main(int argc,char *argv[])
273 {
274         char *share;
275         struct smbcli_state *cli;       
276         int opt;
277         char *p;
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(dyn_CONFIGFILE,True,False,False);
301         load_interfaces();
302
303         if (getenv("USER")) {
304                 fstrcpy(username,getenv("USER"));
305         }
306
307         seed = time(NULL);
308
309         init_iconv();
310
311         while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEl:")) != EOF) {
312                 switch (opt) {
313                 case 'n':
314                         NumLoops = atoi(optarg);
315                         break;
316                 case 'd':
317                         DEBUGLEVEL = atoi(optarg);
318                         break;
319                 case 'E':
320                         die_on_error = 1;
321                         break;
322                 case 'v':
323                         verbose++;
324                         break;
325                 case 'M':
326                         lp_set_cmdline("max protocol", optarg);
327                         break;
328                 case 'U':
329                         fstrcpy(username,optarg);
330                         p = strchr_m(username,'%');
331                         if (p) {
332                                 *p = 0;
333                                 fstrcpy(password, p+1);
334                         }
335                         break;
336                 case 's':
337                         seed = atoi(optarg);
338                         break;
339                 case 'h':
340                         usage();
341                         exit(1);
342                 case 'm':
343                         maskchars = optarg;
344                         break;
345                 case 'l':
346                         max_length = atoi(optarg);
347                         break;
348                 case 'f':
349                         filechars = optarg;
350                         break;
351                 case 'a':
352                         showall = 1;
353                         break;
354                 case 'o':
355                         old_list = True;
356                         break;
357                 default:
358                         printf("Unknown option %c (%d)\n", (char)opt, opt);
359                         exit(1);
360                 }
361         }
362
363         argc -= optind;
364         argv += optind;
365
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 }