r5929: Use cli_credentials for the SMB functions as well.
[samba.git] / source4 / torture / locktest.c
1 /* 
2    Unix SMB/CIFS implementation.
3    randomised byte range lock 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 "dynconfig.h"
24 #include "system/time.h"
25
26 static int numops = 1000;
27 static BOOL showall;
28 static BOOL analyze;
29 static BOOL hide_unlock_fails;
30 static BOOL use_oplocks;
31 static uint_t lock_range = 100;
32 static uint_t lock_base = 0;
33 static uint_t min_length = 0;
34 static BOOL exact_error_codes;
35 static BOOL zero_zero;
36
37 #define FILENAME "\\locktest.dat"
38
39 #define READ_PCT 50
40 #define LOCK_PCT 45
41 #define UNLOCK_PCT 70
42 #define RANGE_MULTIPLE 1
43 #define NSERVERS 2
44 #define NCONNECTIONS 2
45 #define NFILES 2
46 #define LOCK_TIMEOUT 0
47
48 #define NASTY_POSIX_LOCK_HACK 0
49
50 static struct cli_credentials servers[NSERVERS];
51
52 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
53
54 struct record {
55         enum lock_op lock_op;
56         enum brl_type lock_type;
57         char conn, f;
58         uint64_t start, len;
59         char needed;
60 };
61
62 #define PRESETS 0
63
64 #if PRESETS
65 static struct record preset[] = {
66 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
67 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
68 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
69 {OP_UNLOCK, 0       , 0, 0, 2, 0, 1},
70 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
71
72 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
73 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
74 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
75 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
76
77 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
78 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
79 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
80 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
81
82 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
83 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
84 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
85 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
86
87 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
88 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
89 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
90 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
91
92 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
93 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
94 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
95 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
96
97 };
98 #endif
99
100 static struct record *recorded;
101
102 /***************************************************** 
103 return a connection to a server
104 *******************************************************/
105 static struct smbcli_state *connect_one(char *share, int snum)
106 {
107         struct smbcli_state *c;
108         fstring server, myname;
109         NTSTATUS status;
110         int retries = 10;
111
112         fstrcpy(server,share+2);
113         share = strchr_m(server,'\\');
114         if (!share) return NULL;
115         *share = 0;
116         share++;
117
118         slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
119
120         do {
121                 status = smbcli_full_connection(NULL, &c, myname,
122                                                 server, 
123                                                 share, NULL,
124                                                 &servers[snum]);
125                 if (!NT_STATUS_IS_OK(status)) {
126                         sleep(2);
127                 }
128         } while (!NT_STATUS_IS_OK(status) && retries--);
129
130         if (!NT_STATUS_IS_OK(status)) {
131                 return NULL;
132         }
133
134         return c;
135 }
136
137
138 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
139                       char *share[NSERVERS])
140 {
141         int server, conn, f;
142
143         for (server=0;server<NSERVERS;server++)
144         for (conn=0;conn<NCONNECTIONS;conn++) {
145                 if (cli[server][conn]) {
146                         for (f=0;f<NFILES;f++) {
147                                 if (fnum[server][conn][f] != -1) {
148                                         smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
149                                         fnum[server][conn][f] = -1;
150                                 }
151                         }
152                         smbcli_shutdown(cli[server][conn]);
153                 }
154                 cli[server][conn] = connect_one(share[server], server);
155                 if (!cli[server][conn]) {
156                         DEBUG(0,("Failed to connect to %s\n", share[server]));
157                         exit(1);
158                 }
159         }
160 }
161
162
163
164 static BOOL test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
165                      int fnum[NSERVERS][NCONNECTIONS][NFILES],
166                      struct record *rec)
167 {
168         uint_t conn = rec->conn;
169         uint_t f = rec->f;
170         uint64_t start = rec->start;
171         uint64_t len = rec->len;
172         enum brl_type op = rec->lock_type;
173         int server;
174         BOOL ret[NSERVERS];
175         NTSTATUS status[NSERVERS];
176
177         switch (rec->lock_op) {
178         case OP_LOCK:
179                 /* set a lock */
180                 for (server=0;server<NSERVERS;server++) {
181                         ret[server] = NT_STATUS_IS_OK(smbcli_lock64(cli[server][conn]->tree, 
182                                                  fnum[server][conn][f],
183                                                  start, len, LOCK_TIMEOUT, op));
184                         status[server] = smbcli_nt_error(cli[server][conn]->tree);
185                         if (!exact_error_codes && 
186                             NT_STATUS_EQUAL(status[server], 
187                                             NT_STATUS_FILE_LOCK_CONFLICT)) {
188                                 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
189                         }
190                 }
191                 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
192                         printf("lock   conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
193                                conn, f, 
194                                (double)start, (double)len,
195                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
196                                nt_errstr(status[0]), nt_errstr(status[1]));
197                 }
198                 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
199                 break;
200                 
201         case OP_UNLOCK:
202                 /* unset a lock */
203                 for (server=0;server<NSERVERS;server++) {
204                         ret[server] = NT_STATUS_IS_OK(smbcli_unlock64(cli[server][conn]->tree, 
205                                                    fnum[server][conn][f],
206                                                    start, len));
207                         status[server] = smbcli_nt_error(cli[server][conn]->tree);
208                 }
209                 if (showall || 
210                     (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
211                         printf("unlock conn=%u f=%u range=%.0f(%.0f)       -> %s:%s\n",
212                                conn, f, 
213                                (double)start, (double)len,
214                                nt_errstr(status[0]), nt_errstr(status[1]));
215                 }
216                 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1])) 
217                         return False;
218                 break;
219
220         case OP_REOPEN:
221                 /* reopen the file */
222                 for (server=0;server<NSERVERS;server++) {
223                         smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
224                         fnum[server][conn][f] = -1;
225                 }
226                 for (server=0;server<NSERVERS;server++) {
227                         fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
228                                                          O_RDWR|O_CREAT,
229                                                          DENY_NONE);
230                         if (fnum[server][conn][f] == -1) {
231                                 printf("failed to reopen on share%d\n", server);
232                                 return False;
233                         }
234                 }
235                 if (showall) {
236                         printf("reopen conn=%u f=%u\n",
237                                conn, f);
238                 }
239                 break;
240         }
241
242         return True;
243 }
244
245 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
246                         int fnum[NSERVERS][NCONNECTIONS][NFILES])
247 {
248         int server, conn, f; 
249
250         for (server=0;server<NSERVERS;server++)
251         for (conn=0;conn<NCONNECTIONS;conn++)
252         for (f=0;f<NFILES;f++) {
253                 if (fnum[server][conn][f] != -1) {
254                         smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
255                         fnum[server][conn][f] = -1;
256                 }
257         }
258         for (server=0;server<NSERVERS;server++) {
259                 smbcli_unlink(cli[server][0]->tree, FILENAME);
260         }
261 }
262
263 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
264                        int fnum[NSERVERS][NCONNECTIONS][NFILES])
265 {
266         int server, conn, f; 
267
268         for (server=0;server<NSERVERS;server++)
269         for (conn=0;conn<NCONNECTIONS;conn++)
270         for (f=0;f<NFILES;f++) {
271                 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
272                                                  O_RDWR|O_CREAT,
273                                                  DENY_NONE);
274                 if (fnum[server][conn][f] == -1) {
275                         fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
276                                 server, conn, f);
277                         exit(1);
278                 }
279         }
280 }
281
282
283 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
284                    int fnum[NSERVERS][NCONNECTIONS][NFILES],
285                    int n)
286 {
287         int i;
288         printf("testing %u ...\n", n);
289         for (i=0; i<n; i++) {
290                 if (i && i % 100 == 0) {
291                         printf("%u\n", i);
292                 }
293
294                 if (recorded[i].needed &&
295                     !test_one(cli, fnum, &recorded[i])) return i;
296         }
297         return n;
298 }
299
300
301 /* each server has two connections open to it. Each connection has two file
302    descriptors open on the file - 8 file descriptors in total 
303
304    we then do random locking ops in tamdem on the 4 fnums from each
305    server and ensure that the results match
306  */
307 static void test_locks(char *share[NSERVERS])
308 {
309         struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
310         int fnum[NSERVERS][NCONNECTIONS][NFILES];
311         int n, i, n1, skip, r1, r2; 
312
313         ZERO_STRUCT(fnum);
314         ZERO_STRUCT(cli);
315
316         recorded = malloc_array_p(struct record, numops);
317
318         for (n=0; n<numops; n++) {
319 #if PRESETS
320                 if (n < sizeof(preset) / sizeof(preset[0])) {
321                         recorded[n] = preset[n];
322                 } else {
323 #endif
324                         recorded[n].conn = random() % NCONNECTIONS;
325                         recorded[n].f = random() % NFILES;
326                         recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
327                         recorded[n].len =  min_length +
328                                 random() % (lock_range-(recorded[n].start-lock_base));
329                         recorded[n].start *= RANGE_MULTIPLE;
330                         recorded[n].len *= RANGE_MULTIPLE;
331                         r1 = random() % 100;
332                         r2 = random() % 100;
333                         if (r1 < READ_PCT) {
334                                 recorded[n].lock_type = READ_LOCK;
335                         } else {
336                                 recorded[n].lock_type = WRITE_LOCK;
337                         }
338                         if (r2 < LOCK_PCT) {
339                                 recorded[n].lock_op = OP_LOCK;
340                         } else if (r2 < UNLOCK_PCT) {
341                                 recorded[n].lock_op = OP_UNLOCK;
342                         } else {
343                                 recorded[n].lock_op = OP_REOPEN;
344                         }
345                         recorded[n].needed = True;
346                         if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
347                                 recorded[n].len = 1;
348                         }
349 #if PRESETS
350                 }
351 #endif
352         }
353
354         reconnect(cli, fnum, share);
355         open_files(cli, fnum);
356         n = retest(cli, fnum, numops);
357
358         if (n == numops || !analyze) return;
359         n++;
360
361         skip = n/2;
362
363         while (1) {
364                 n1 = n;
365
366                 close_files(cli, fnum);
367                 reconnect(cli, fnum, share);
368                 open_files(cli, fnum);
369
370                 for (i=0;i<n-skip;i+=skip) {
371                         int m, j;
372                         printf("excluding %d-%d\n", i, i+skip-1);
373                         for (j=i;j<i+skip;j++) {
374                                 recorded[j].needed = False;
375                         }
376
377                         close_files(cli, fnum);
378                         open_files(cli, fnum);
379
380                         m = retest(cli, fnum, n);
381                         if (m == n) {
382                                 for (j=i;j<i+skip;j++) {
383                                         recorded[j].needed = True;
384                                 }
385                         } else {
386                                 if (i+(skip-1) < m) {
387                                         memmove(&recorded[i], &recorded[i+skip],
388                                                 (m-(i+skip-1))*sizeof(recorded[0]));
389                                 }
390                                 n = m-(skip-1);
391                                 i--;
392                         }
393                 }
394
395                 if (skip > 1) {
396                         skip = skip/2;
397                         printf("skip=%d\n", skip);
398                         continue;
399                 }
400
401                 if (n1 == n) break;
402         }
403
404         close_files(cli, fnum);
405         reconnect(cli, fnum, share);
406         open_files(cli, fnum);
407         showall = True;
408         n1 = retest(cli, fnum, n);
409         if (n1 != n-1) {
410                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
411         }
412         close_files(cli, fnum);
413
414         for (i=0;i<n;i++) {
415                 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
416                        recorded[i].lock_op,
417                        recorded[i].lock_type,
418                        recorded[i].conn,
419                        recorded[i].f,
420                        (double)recorded[i].start,
421                        (double)recorded[i].len,
422                        recorded[i].needed);
423         }       
424 }
425
426
427
428 static void usage(void)
429 {
430         printf(
431 "Usage:\n\
432   locktest //server1/share1 //server2/share2 [options..]\n\
433   options:\n\
434         -U user%%pass        (may be specified twice)\n\
435         -s seed\n\
436         -o numops\n\
437         -u          hide unlock fails\n\
438         -a          (show all ops)\n\
439         -A          analyse for minimal ops\n\
440         -O          use oplocks\n\
441         -E          enable exact error code checking\n\
442         -Z          enable the zero/zero lock\n\
443         -R range    set lock range\n\
444         -B base     set lock base\n\
445         -M min      set min lock length\n\
446 ");
447 }
448
449 /****************************************************************************
450   main program
451 ****************************************************************************/
452  int main(int argc,char *argv[])
453 {
454         char *share[NSERVERS];
455         int opt;
456         int seed, server, i;
457
458         setlinebuf(stdout);
459
460         setup_logging("locktest", DEBUG_STDOUT);
461
462         if (argc < 3 || argv[1][0] == '-') {
463                 usage();
464                 exit(1);
465         }
466
467         setup_logging(argv[0], DEBUG_STDOUT);
468
469         for (server=0;server<NSERVERS;server++) {
470                 share[server] = argv[1+server];
471                 all_string_sub(share[server],"/","\\",0);
472         }
473
474         argc -= NSERVERS;
475         argv += NSERVERS;
476
477         lp_load(dyn_CONFIGFILE,True,False,False);
478         load_interfaces();
479
480         seed = time(NULL);
481
482         while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:")) != EOF) {
483                 switch (opt) {
484                 case 'U':
485                         i = servers[0].username?1:0;
486                         cli_credentials_parse_string(&servers[0], optarg, CRED_SPECIFIED);
487
488                         break;
489                 case 'R':
490                         lock_range = strtol(optarg, NULL, 0);
491                         break;
492                 case 'B':
493                         lock_base = strtol(optarg, NULL, 0);
494                         break;
495                 case 'M':
496                         min_length = strtol(optarg, NULL, 0);
497                         break;
498                 case 's':
499                         seed = atoi(optarg);
500                         break;
501                 case 'u':
502                         hide_unlock_fails = True;
503                         break;
504                 case 'o':
505                         numops = atoi(optarg);
506                         break;
507                 case 'O':
508                         use_oplocks = True;
509                         break;
510                 case 'a':
511                         showall = True;
512                         break;
513                 case 'A':
514                         analyze = True;
515                         break;
516                 case 'Z':
517                         zero_zero = True;
518                         break;
519                 case 'E':
520                         exact_error_codes = True;
521                         break;
522                 case 'W':
523                         lp_set_cmdline("workgroup", optarg);
524                         break;
525                 case 'h':
526                         usage();
527                         exit(1);
528                 default:
529                         printf("Unknown option %c (%d)\n", (char)opt, opt);
530                         exit(1);
531                 }
532         }
533
534         if (!servers[0].username) {
535                 usage();
536                 return -1;
537         }
538         if (!servers[1].username) {
539                 servers[1].username = servers[0].username;
540                 servers[1].password = servers[0].password;
541         }
542
543         locktest_init_subsystems;
544
545         argc -= optind;
546         argv += optind;
547
548         DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n", 
549                  seed, lock_base, lock_range, min_length));
550         srandom(seed);
551
552         test_locks(share);
553
554         return(0);
555 }