2 Unix SMB/CIFS implementation.
3 randomised byte range lock tester
4 Copyright (C) Andrew Tridgell 1999
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.
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.
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.
22 #include "system/filesys.h"
23 #include "system/time.h"
25 #include "auth/gensec/gensec.h"
26 #include "libcli/libcli.h"
28 static int numops = 1000;
31 static BOOL hide_unlock_fails;
32 static BOOL use_oplocks;
33 static uint_t lock_range = 100;
34 static uint_t lock_base = 0;
35 static uint_t min_length = 0;
36 static BOOL exact_error_codes;
37 static BOOL zero_zero;
39 #define FILENAME "\\locktest.dat"
44 #define RANGE_MULTIPLE 1
46 #define NCONNECTIONS 2
48 #define LOCK_TIMEOUT 0
50 static struct cli_credentials *servers[NSERVERS];
52 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
56 enum brl_type lock_type;
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},
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},
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},
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},
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},
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},
100 static struct record *recorded;
102 /*****************************************************
103 return a connection to a server
104 *******************************************************/
105 static struct smbcli_state *connect_one(char *share, int snum, int conn)
107 struct smbcli_state *c;
108 fstring server, myname;
112 printf("connect_one(%s, %d, %d)\n", share, snum, conn);
114 fstrcpy(server,share+2);
115 share = strchr_m(server,'\\');
116 if (!share) return NULL;
121 char **unc_list = NULL;
124 p = lp_parm_string(-1, "torture", "unclist");
127 unc_list = file_lines_load(p, &num_unc_names, NULL);
128 if (!unc_list || num_unc_names <= 0) {
129 printf("Failed to load unc names list from '%s'\n", p);
133 if (!smbcli_parse_unc(unc_list[conn % num_unc_names],
135 printf("Failed to parse UNC name %s\n",
136 unc_list[conn % num_unc_names]);
145 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
146 cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
149 printf("\\\\%s\\%s\n", server, share);
150 status = smbcli_full_connection(NULL, &c,
153 servers[snum], NULL);
154 if (!NT_STATUS_IS_OK(status)) {
157 } while (!NT_STATUS_IS_OK(status) && retries--);
159 if (!NT_STATUS_IS_OK(status)) {
167 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
168 char *share[NSERVERS])
172 for (server=0;server<NSERVERS;server++)
173 for (conn=0;conn<NCONNECTIONS;conn++) {
174 if (cli[server][conn]) {
175 for (f=0;f<NFILES;f++) {
176 if (fnum[server][conn][f] != -1) {
177 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
178 fnum[server][conn][f] = -1;
181 talloc_free(cli[server][conn]);
183 cli[server][conn] = connect_one(share[server], server, conn);
184 if (!cli[server][conn]) {
185 DEBUG(0,("Failed to connect to %s\n", share[server]));
193 static BOOL test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
194 int fnum[NSERVERS][NCONNECTIONS][NFILES],
197 uint_t conn = rec->conn;
199 uint64_t start = rec->start;
200 uint64_t len = rec->len;
201 enum brl_type op = rec->lock_type;
204 NTSTATUS status[NSERVERS];
206 switch (rec->lock_op) {
209 for (server=0;server<NSERVERS;server++) {
210 ret[server] = NT_STATUS_IS_OK(smbcli_lock64(cli[server][conn]->tree,
211 fnum[server][conn][f],
212 start, len, LOCK_TIMEOUT, op));
213 status[server] = smbcli_nt_error(cli[server][conn]->tree);
214 if (!exact_error_codes &&
215 NT_STATUS_EQUAL(status[server],
216 NT_STATUS_FILE_LOCK_CONFLICT)) {
217 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
220 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
221 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
223 (double)start, (double)len,
224 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
225 nt_errstr(status[0]), nt_errstr(status[1]));
227 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
232 for (server=0;server<NSERVERS;server++) {
233 ret[server] = NT_STATUS_IS_OK(smbcli_unlock64(cli[server][conn]->tree,
234 fnum[server][conn][f],
236 status[server] = smbcli_nt_error(cli[server][conn]->tree);
239 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
240 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
242 (double)start, (double)len,
243 nt_errstr(status[0]), nt_errstr(status[1]));
245 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
250 /* reopen the file */
251 for (server=0;server<NSERVERS;server++) {
252 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
253 fnum[server][conn][f] = -1;
255 for (server=0;server<NSERVERS;server++) {
256 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
259 if (fnum[server][conn][f] == -1) {
260 printf("failed to reopen on share%d\n", server);
265 printf("reopen conn=%u f=%u\n",
274 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
275 int fnum[NSERVERS][NCONNECTIONS][NFILES])
279 for (server=0;server<NSERVERS;server++)
280 for (conn=0;conn<NCONNECTIONS;conn++)
281 for (f=0;f<NFILES;f++) {
282 if (fnum[server][conn][f] != -1) {
283 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
284 fnum[server][conn][f] = -1;
287 for (server=0;server<NSERVERS;server++) {
288 smbcli_unlink(cli[server][0]->tree, FILENAME);
292 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
293 int fnum[NSERVERS][NCONNECTIONS][NFILES])
297 for (server=0;server<NSERVERS;server++)
298 for (conn=0;conn<NCONNECTIONS;conn++)
299 for (f=0;f<NFILES;f++) {
300 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
303 if (fnum[server][conn][f] == -1) {
304 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
312 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
313 int fnum[NSERVERS][NCONNECTIONS][NFILES],
317 printf("testing %u ...\n", n);
318 for (i=0; i<n; i++) {
319 if (i && i % 100 == 0) {
323 if (recorded[i].needed &&
324 !test_one(cli, fnum, &recorded[i])) return i;
330 /* each server has two connections open to it. Each connection has two file
331 descriptors open on the file - 8 file descriptors in total
333 we then do random locking ops in tamdem on the 4 fnums from each
334 server and ensure that the results match
336 static void test_locks(char *share[NSERVERS])
338 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
339 int fnum[NSERVERS][NCONNECTIONS][NFILES];
340 int n, i, n1, skip, r1, r2;
345 recorded = malloc_array_p(struct record, numops);
347 for (n=0; n<numops; n++) {
349 if (n < sizeof(preset) / sizeof(preset[0])) {
350 recorded[n] = preset[n];
353 recorded[n].conn = random() % NCONNECTIONS;
354 recorded[n].f = random() % NFILES;
355 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
356 recorded[n].len = min_length +
357 random() % (lock_range-(recorded[n].start-lock_base));
358 recorded[n].start *= RANGE_MULTIPLE;
359 recorded[n].len *= RANGE_MULTIPLE;
363 recorded[n].lock_type = READ_LOCK;
365 recorded[n].lock_type = WRITE_LOCK;
368 recorded[n].lock_op = OP_LOCK;
369 } else if (r2 < UNLOCK_PCT) {
370 recorded[n].lock_op = OP_UNLOCK;
372 recorded[n].lock_op = OP_REOPEN;
374 recorded[n].needed = True;
375 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
383 reconnect(cli, fnum, share);
384 open_files(cli, fnum);
385 n = retest(cli, fnum, numops);
387 if (n == numops || !analyze) return;
395 close_files(cli, fnum);
396 reconnect(cli, fnum, share);
397 open_files(cli, fnum);
399 for (i=0;i<n-skip;i+=skip) {
401 printf("excluding %d-%d\n", i, i+skip-1);
402 for (j=i;j<i+skip;j++) {
403 recorded[j].needed = False;
406 close_files(cli, fnum);
407 open_files(cli, fnum);
409 m = retest(cli, fnum, n);
411 for (j=i;j<i+skip;j++) {
412 recorded[j].needed = True;
415 if (i+(skip-1) < m) {
416 memmove(&recorded[i], &recorded[i+skip],
417 (m-(i+skip-1))*sizeof(recorded[0]));
426 printf("skip=%d\n", skip);
433 close_files(cli, fnum);
434 reconnect(cli, fnum, share);
435 open_files(cli, fnum);
437 n1 = retest(cli, fnum, n);
439 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
441 close_files(cli, fnum);
444 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
446 recorded[i].lock_type,
449 (double)recorded[i].start,
450 (double)recorded[i].len,
457 static void usage(void)
461 locktest //server1/share1 //server2/share2 [options..]\n\
463 -U user%%pass (may be specified twice)\n\
466 -u hide unlock fails\n\
468 -A analyse for minimal ops\n\
470 -E enable exact error code checking\n\
471 -Z enable the zero/zero lock\n\
472 -R range set lock range\n\
473 -B base set lock base\n\
474 -M min set min lock length\n\
475 -l filename unclist file\n\
479 /****************************************************************************
481 ****************************************************************************/
482 int main(int argc,char *argv[])
484 char *share[NSERVERS];
487 int username_count=0;
491 setup_logging("locktest", DEBUG_STDOUT);
493 if (argc < 3 || argv[1][0] == '-') {
498 setup_logging(argv[0], DEBUG_STDOUT);
500 for (server=0;server<NSERVERS;server++) {
501 share[server] = argv[1+server];
502 all_string_sub(share[server],"/","\\",0);
510 servers[0] = cli_credentials_init(talloc_autofree_context());
511 servers[1] = cli_credentials_init(talloc_autofree_context());
512 cli_credentials_guess(servers[0]);
513 cli_credentials_guess(servers[1]);
517 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:l:")) != EOF) {
520 if (username_count == 2) {
524 cli_credentials_parse_string(servers[username_count],
525 optarg, CRED_SPECIFIED);
529 lock_range = strtol(optarg, NULL, 0);
532 lock_base = strtol(optarg, NULL, 0);
535 min_length = strtol(optarg, NULL, 0);
541 hide_unlock_fails = True;
544 numops = atoi(optarg);
559 exact_error_codes = True;
562 lp_set_cmdline("torture:unclist", optarg);
565 lp_set_cmdline("workgroup", optarg);
571 printf("Unknown option %c (%d)\n", (char)opt, opt);
576 if (username_count == 0) {
580 if (username_count == 1) {
581 servers[1] = servers[0];
589 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
590 seed, lock_base, lock_range, min_length));