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