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