r26654: libcli/smb_composite: Rather than specifying each of the gazillion options...
[jelmer/samba4-debian.git] / source / 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 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/time.h"
23 #include "pstring.h"
24 #include "auth/credentials/credentials.h"
25 #include "auth/gensec/gensec.h"
26 #include "libcli/libcli.h"
27 #include "param/param.h"
28 #include "dynconfig.h"
29 #include "libcli/resolve/resolve.h"
30
31 static int numops = 1000;
32 static bool showall;
33 static bool analyze;
34 static bool hide_unlock_fails;
35 static bool use_oplocks;
36 static uint_t lock_range = 100;
37 static uint_t lock_base = 0;
38 static uint_t min_length = 0;
39 static bool exact_error_codes;
40 static bool zero_zero;
41
42 #define FILENAME "\\locktest.dat"
43
44 #define READ_PCT 50
45 #define LOCK_PCT 45
46 #define UNLOCK_PCT 70
47 #define RANGE_MULTIPLE 1
48 #define NSERVERS 2
49 #define NCONNECTIONS 2
50 #define NFILES 2
51 #define LOCK_TIMEOUT 0
52
53 static struct cli_credentials *servers[NSERVERS];
54
55 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
56
57 struct record {
58         enum lock_op lock_op;
59         enum brl_type lock_type;
60         char conn, f;
61         uint64_t start, len;
62         char needed;
63         uint16_t pid;
64 };
65
66 #define PRESETS 0
67
68 #if PRESETS
69 static struct record preset[] = {
70 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
71 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
72 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
73 {OP_UNLOCK, 0       , 0, 0, 2, 0, 1},
74 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
75
76 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
77 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
78 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
79 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
80
81 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
82 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
83 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
84 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
85
86 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
87 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
88 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
89 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
90
91 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
92 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
93 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
94 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
95
96 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
97 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
98 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
99 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
100
101 };
102 #endif
103
104 static struct record *recorded;
105
106 /***************************************************** 
107 return a connection to a server
108 *******************************************************/
109 static struct smbcli_state *connect_one(struct loadparm_context *lp_ctx,
110                                         char *share, int snum, int conn)
111 {
112         struct smbcli_state *c;
113         fstring server, myname;
114         NTSTATUS status;
115         int retries = 10;
116         struct smbcli_options options;
117
118         lp_smbcli_options(lp_ctx, &options);
119
120         printf("connect_one(%s, %d, %d)\n", share, snum, conn);
121
122         fstrcpy(server,share+2);
123         share = strchr_m(server,'\\');
124         if (!share) return NULL;
125         *share = 0;
126         share++;
127
128         if (snum == 0) {
129                 char **unc_list = NULL;
130                 int num_unc_names;
131                 const char *p;
132                 p = lp_parm_string(lp_ctx, NULL, "torture", "unclist");
133                 if (p) {
134                         char *h, *s;
135                         unc_list = file_lines_load(p, &num_unc_names, NULL);
136                         if (!unc_list || num_unc_names <= 0) {
137                                 printf("Failed to load unc names list from '%s'\n", p);
138                                 exit(1);
139                         }
140
141                         if (!smbcli_parse_unc(unc_list[conn % num_unc_names],
142                                               NULL, &h, &s)) {
143                                 printf("Failed to parse UNC name %s\n",
144                                        unc_list[conn % num_unc_names]);
145                                 exit(1);
146                         }
147                         fstrcpy(server, h);
148                         fstrcpy(share, s);
149                 }
150         }
151
152
153         slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
154         cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
155
156         do {
157                 printf("\\\\%s\\%s\n", server, share);
158                 status = smbcli_full_connection(NULL, &c, 
159                                                 server, 
160                                                 lp_smb_ports(lp_ctx),
161                                                 share, NULL,
162                                                 servers[snum], 
163                                                 lp_resolve_context(lp_ctx),
164                                                 NULL, &options);
165                 if (!NT_STATUS_IS_OK(status)) {
166                         sleep(2);
167                 }
168         } while (!NT_STATUS_IS_OK(status) && retries--);
169
170         if (!NT_STATUS_IS_OK(status)) {
171                 return NULL;
172         }
173
174         return c;
175 }
176
177
178 static void reconnect(struct loadparm_context *lp_ctx,
179                       struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
180                       char *share[NSERVERS])
181 {
182         int server, conn, f;
183
184         for (server=0;server<NSERVERS;server++)
185         for (conn=0;conn<NCONNECTIONS;conn++) {
186                 if (cli[server][conn]) {
187                         for (f=0;f<NFILES;f++) {
188                                 if (fnum[server][conn][f] != -1) {
189                                         smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
190                                         fnum[server][conn][f] = -1;
191                                 }
192                         }
193                         talloc_free(cli[server][conn]);
194                 }
195                 cli[server][conn] = connect_one(lp_ctx, share[server], 
196                                                 server, conn);
197                 if (!cli[server][conn]) {
198                         DEBUG(0,("Failed to connect to %s\n", share[server]));
199                         exit(1);
200                 }
201         }
202 }
203
204
205
206 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
207                      int fnum[NSERVERS][NCONNECTIONS][NFILES],
208                      struct record *rec)
209 {
210         uint_t conn = rec->conn;
211         uint_t f = rec->f;
212         uint64_t start = rec->start;
213         uint64_t len = rec->len;
214         enum brl_type op = rec->lock_type;
215         int server;
216         bool ret[NSERVERS];
217         NTSTATUS status[NSERVERS];
218
219         switch (rec->lock_op) {
220         case OP_LOCK:
221                 /* set a lock */
222                 for (server=0;server<NSERVERS;server++) {
223                         NTSTATUS res;
224                         struct smbcli_tree *tree=cli[server][conn]->tree;
225                         int fn=fnum[server][conn][f];
226
227                         if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
228                                 res=smbcli_lock(tree, fn, start, len, LOCK_TIMEOUT, rec->lock_op);
229                         } else {
230                                 union smb_lock parms;
231                                 int ltype;
232                                 struct smb_lock_entry lock[1];
233
234                                 parms.lockx.level = RAW_LOCK_LOCKX;
235                                 parms.lockx.in.file.fnum = fn;
236         
237                                 ltype = (rec->lock_op == READ_LOCK? 1 : 0);
238                                 ltype |= LOCKING_ANDX_LARGE_FILES;
239                                 parms.lockx.in.mode = ltype;
240                                 parms.lockx.in.timeout = LOCK_TIMEOUT;
241                                 parms.lockx.in.ulock_cnt = 0;
242                                 parms.lockx.in.lock_cnt = 1;
243                                 lock[0].pid = rec->pid;
244                                 lock[0].offset = start;
245                                 lock[0].count = len;
246                                 parms.lockx.in.locks = &lock[0];
247
248                                 res = smb_raw_lock(tree, &parms);
249                         }
250
251                         ret[server] = NT_STATUS_IS_OK(res); 
252                         status[server] = smbcli_nt_error(cli[server][conn]->tree);
253                         if (!exact_error_codes && 
254                             NT_STATUS_EQUAL(status[server], 
255                                             NT_STATUS_FILE_LOCK_CONFLICT)) {
256                                 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
257                         }
258                 }
259                 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
260                         printf("lock   conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
261                                conn, f, 
262                                (double)start, (double)len,
263                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
264                                nt_errstr(status[0]), nt_errstr(status[1]));
265                 }
266                 if (!NT_STATUS_EQUAL(status[0],status[1])) return false;
267                 break;
268                 
269         case OP_UNLOCK:
270                 /* unset a lock */
271                 for (server=0;server<NSERVERS;server++) {
272                         NTSTATUS res;
273                         struct smbcli_tree *tree=cli[server][conn]->tree;
274                         int fn=fnum[server][conn][f];
275
276
277                         if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
278                                 res=smbcli_unlock(tree, fn, start, len);
279                         } else {
280                                 union smb_lock parms;
281                                 struct smb_lock_entry lock[1];
282
283                                 parms.lockx.level = RAW_LOCK_LOCKX;
284                                 parms.lockx.in.file.fnum = fn;
285                                 parms.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
286                                 parms.lockx.in.timeout = 0;
287                                 parms.lockx.in.ulock_cnt = 1;
288                                 parms.lockx.in.lock_cnt = 0;
289                                 lock[0].pid = rec->pid;
290                                 lock[0].count = len;
291                                 lock[0].offset = start;
292                                 parms.lockx.in.locks = &lock[0];
293
294                                 res = smb_raw_lock(tree, &parms);
295                         }
296
297                         ret[server] = NT_STATUS_IS_OK(res);
298                         status[server] = smbcli_nt_error(cli[server][conn]->tree);
299                 }
300                 if (showall || 
301                     (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
302                         printf("unlock conn=%u f=%u range=%.0f(%.0f)       -> %s:%s\n",
303                                conn, f, 
304                                (double)start, (double)len,
305                                nt_errstr(status[0]), nt_errstr(status[1]));
306                 }
307                 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1])) 
308                         return false;
309                 break;
310
311         case OP_REOPEN:
312                 /* reopen the file */
313                 for (server=0;server<NSERVERS;server++) {
314                         smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
315                         fnum[server][conn][f] = -1;
316                 }
317                 for (server=0;server<NSERVERS;server++) {
318                         fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
319                                                          O_RDWR|O_CREAT,
320                                                          DENY_NONE);
321                         if (fnum[server][conn][f] == -1) {
322                                 printf("failed to reopen on share%d\n", server);
323                                 return false;
324                         }
325                 }
326                 if (showall) {
327                         printf("reopen conn=%u f=%u\n",
328                                conn, f);
329                 }
330                 break;
331         }
332
333         return true;
334 }
335
336 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
337                         int fnum[NSERVERS][NCONNECTIONS][NFILES])
338 {
339         int server, conn, f; 
340
341         for (server=0;server<NSERVERS;server++)
342         for (conn=0;conn<NCONNECTIONS;conn++)
343         for (f=0;f<NFILES;f++) {
344                 if (fnum[server][conn][f] != -1) {
345                         smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
346                         fnum[server][conn][f] = -1;
347                 }
348         }
349         for (server=0;server<NSERVERS;server++) {
350                 smbcli_unlink(cli[server][0]->tree, FILENAME);
351         }
352 }
353
354 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
355                        int fnum[NSERVERS][NCONNECTIONS][NFILES])
356 {
357         int server, conn, f; 
358
359         for (server=0;server<NSERVERS;server++)
360         for (conn=0;conn<NCONNECTIONS;conn++)
361         for (f=0;f<NFILES;f++) {
362                 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
363                                                  O_RDWR|O_CREAT,
364                                                  DENY_NONE);
365                 if (fnum[server][conn][f] == -1) {
366                         fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
367                                 server, conn, f);
368                         exit(1);
369                 }
370         }
371 }
372
373
374 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
375                    int fnum[NSERVERS][NCONNECTIONS][NFILES],
376                    int n)
377 {
378         int i;
379         printf("testing %u ...\n", n);
380         for (i=0; i<n; i++) {
381                 if (i && i % 100 == 0) {
382                         printf("%u\n", i);
383                 }
384
385                 if (recorded[i].needed &&
386                     !test_one(cli, fnum, &recorded[i])) return i;
387         }
388         return n;
389 }
390
391
392 /* each server has two connections open to it. Each connection has two file
393    descriptors open on the file - 8 file descriptors in total 
394
395    we then do random locking ops in tamdem on the 4 fnums from each
396    server and ensure that the results match
397  */
398 static void test_locks(struct loadparm_context *lp_ctx, char *share[NSERVERS])
399 {
400         struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
401         int fnum[NSERVERS][NCONNECTIONS][NFILES];
402         int n, i, n1, skip, r1, r2; 
403
404         ZERO_STRUCT(fnum);
405         ZERO_STRUCT(cli);
406
407         recorded = malloc_array_p(struct record, numops);
408
409         for (n=0; n<numops; n++) {
410 #if PRESETS
411                 if (n < sizeof(preset) / sizeof(preset[0])) {
412                         recorded[n] = preset[n];
413                 } else {
414 #endif
415                         recorded[n].conn = random() % NCONNECTIONS;
416                         recorded[n].f = random() % NFILES;
417                         recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
418                         recorded[n].len =  min_length +
419                                 random() % (lock_range-(recorded[n].start-lock_base));
420                         recorded[n].start *= RANGE_MULTIPLE;
421                         recorded[n].len *= RANGE_MULTIPLE;
422                         recorded[n].pid = random()%3;
423                         if (recorded[n].pid == 2) {
424                                 recorded[n].pid = 0xFFFF; /* see if its magic */
425                         }
426                         r1 = random() % 100;
427                         r2 = random() % 100;
428                         if (r1 < READ_PCT) {
429                                 recorded[n].lock_type = READ_LOCK;
430                         } else {
431                                 recorded[n].lock_type = WRITE_LOCK;
432                         }
433                         if (r2 < LOCK_PCT) {
434                                 recorded[n].lock_op = OP_LOCK;
435                         } else if (r2 < UNLOCK_PCT) {
436                                 recorded[n].lock_op = OP_UNLOCK;
437                         } else {
438                                 recorded[n].lock_op = OP_REOPEN;
439                         }
440                         recorded[n].needed = true;
441                         if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
442                                 recorded[n].len = 1;
443                         }
444 #if PRESETS
445                 }
446 #endif
447         }
448
449         reconnect(lp_ctx, cli, fnum, share);
450         open_files(cli, fnum);
451         n = retest(cli, fnum, numops);
452
453         if (n == numops || !analyze) return;
454         n++;
455
456         skip = n/2;
457
458         while (1) {
459                 n1 = n;
460
461                 close_files(cli, fnum);
462                 reconnect(lp_ctx, cli, fnum, share);
463                 open_files(cli, fnum);
464
465                 for (i=0;i<n-skip;i+=skip) {
466                         int m, j;
467                         printf("excluding %d-%d\n", i, i+skip-1);
468                         for (j=i;j<i+skip;j++) {
469                                 recorded[j].needed = false;
470                         }
471
472                         close_files(cli, fnum);
473                         open_files(cli, fnum);
474
475                         m = retest(cli, fnum, n);
476                         if (m == n) {
477                                 for (j=i;j<i+skip;j++) {
478                                         recorded[j].needed = true;
479                                 }
480                         } else {
481                                 if (i+(skip-1) < m) {
482                                         memmove(&recorded[i], &recorded[i+skip],
483                                                 (m-(i+skip-1))*sizeof(recorded[0]));
484                                 }
485                                 n = m-(skip-1);
486                                 i--;
487                         }
488                 }
489
490                 if (skip > 1) {
491                         skip = skip/2;
492                         printf("skip=%d\n", skip);
493                         continue;
494                 }
495
496                 if (n1 == n) break;
497         }
498
499         close_files(cli, fnum);
500         reconnect(lp_ctx, cli, fnum, share);
501         open_files(cli, fnum);
502         showall = true;
503         n1 = retest(cli, fnum, n);
504         if (n1 != n-1) {
505                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
506         }
507         close_files(cli, fnum);
508
509         for (i=0;i<n;i++) {
510                 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
511                        recorded[i].lock_op,
512                        recorded[i].lock_type,
513                        recorded[i].conn,
514                        recorded[i].f,
515                        (double)recorded[i].start,
516                        (double)recorded[i].len,
517                        recorded[i].needed);
518         }       
519 }
520
521
522
523 static void usage(void)
524 {
525         printf(
526 "Usage:\n\
527   locktest //server1/share1 //server2/share2 [options..]\n\
528   options:\n\
529         -U user%%pass        (may be specified twice)\n\
530         -s seed\n\
531         -o numops\n\
532         -u          hide unlock fails\n\
533         -a          (show all ops)\n\
534         -A          analyse for minimal ops\n\
535         -O          use oplocks\n\
536         -E          enable exact error code checking\n\
537         -Z          enable the zero/zero lock\n\
538         -R range    set lock range\n\
539         -B base     set lock base\n\
540         -M min      set min lock length\n\
541         -l filename unclist file\n\
542 ");
543 }
544
545 /****************************************************************************
546   main program
547 ****************************************************************************/
548  int main(int argc,char *argv[])
549 {
550         char *share[NSERVERS];
551         int opt;
552         int seed, server;
553         int username_count=0;
554         struct loadparm_context *lp_ctx;
555
556         setlinebuf(stdout);
557
558         setup_logging("locktest", DEBUG_STDOUT);
559
560         if (argc < 3 || argv[1][0] == '-') {
561                 usage();
562                 exit(1);
563         }
564
565         setup_logging(argv[0], DEBUG_STDOUT);
566
567         for (server=0;server<NSERVERS;server++) {
568                 share[server] = argv[1+server];
569                 all_string_sub(share[server],"/","\\",0);
570         }
571
572         argc -= NSERVERS;
573         argv += NSERVERS;
574
575         lp_ctx = loadparm_init(talloc_autofree_context());
576         lp_load(lp_ctx, dyn_CONFIGFILE);
577
578         servers[0] = cli_credentials_init(talloc_autofree_context());
579         servers[1] = cli_credentials_init(talloc_autofree_context());
580         cli_credentials_guess(servers[0], lp_ctx);
581         cli_credentials_guess(servers[1], lp_ctx);
582
583         seed = time(NULL);
584
585         while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:l:")) != EOF) {
586                 switch (opt) {
587                 case 'U':
588                         if (username_count == 2) {
589                                 usage();
590                                 exit(1);
591                         }
592                         cli_credentials_parse_string(servers[username_count], 
593                                                      optarg, CRED_SPECIFIED);
594                         username_count++;
595                         break;
596                 case 'R':
597                         lock_range = strtol(optarg, NULL, 0);
598                         break;
599                 case 'B':
600                         lock_base = strtol(optarg, NULL, 0);
601                         break;
602                 case 'M':
603                         min_length = strtol(optarg, NULL, 0);
604                         break;
605                 case 's':
606                         seed = atoi(optarg);
607                         break;
608                 case 'u':
609                         hide_unlock_fails = true;
610                         break;
611                 case 'o':
612                         numops = atoi(optarg);
613                         break;
614                 case 'O':
615                         use_oplocks = true;
616                         break;
617                 case 'a':
618                         showall = true;
619                         break;
620                 case 'A':
621                         analyze = true;
622                         break;
623                 case 'Z':
624                         zero_zero = true;
625                         break;
626                 case 'E':
627                         exact_error_codes = true;
628                         break;
629                 case 'l':
630                         lp_set_cmdline(lp_ctx, "torture:unclist", optarg);
631                         break;
632                 case 'W':
633                         lp_set_cmdline(lp_ctx, "workgroup", optarg);
634                         break;
635                 case 'h':
636                         usage();
637                         exit(1);
638                 default:
639                         printf("Unknown option %c (%d)\n", (char)opt, opt);
640                         exit(1);
641                 }
642         }
643
644         if (username_count == 0) {
645                 usage();
646                 return -1;
647         }
648         if (username_count == 1) {
649                 servers[1] = servers[0];
650         }
651
652         gensec_init(lp_ctx);
653
654         argc -= optind;
655         argv += optind;
656
657         DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n", 
658                  seed, lock_base, lock_range, min_length));
659         srandom(seed);
660
661         test_locks(lp_ctx, share);
662
663         return(0);
664 }
665