tests: Expected failures in reparse point tests should not be errors
[Samba.git] / source3 / torture / locktest2.c
blob7586bbde44d51bac3fcf142dca7c29fc44573e34
1 /*
2 Unix SMB/CIFS implementation.
3 byte range lock tester - with local filesystem support
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 3 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, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "libsmb/libsmb.h"
22 #include "system/filesys.h"
23 #include "locking/share_mode_lock.h"
24 #include "locking/proto.h"
25 #include "lib/util/string_wrappers.h"
27 static fstring password;
28 static fstring username;
29 static int got_pass;
30 static int numops = 1000;
31 static bool showall;
32 static bool analyze;
33 static bool hide_unlock_fails;
34 static bool use_oplocks;
36 extern char *optarg;
37 extern int optind;
39 #define FILENAME "\\locktest.dat"
40 #define LOCKRANGE 100
41 #define LOCKBASE 0
44 #define LOCKBASE (0x40000000 - 50)
47 #define READ_PCT 50
48 #define LOCK_PCT 25
49 #define UNLOCK_PCT 65
50 #define RANGE_MULTIPLE 1
52 #define NSERVERS 2
53 #define NCONNECTIONS 2
54 #define NUMFSTYPES 2
55 #define NFILES 2
56 #define LOCK_TIMEOUT 0
58 #define FSTYPE_SMB 0
59 #define FSTYPE_NFS 1
61 struct record {
62 char r1, r2;
63 char conn, f, fstype;
64 unsigned start, len;
65 char needed;
68 static struct record *recorded;
70 static int try_open(struct cli_state *c, char *nfs, int fstype, const char *fname, int flags)
72 char *path;
74 switch (fstype) {
75 case FSTYPE_SMB:
77 uint16_t fd;
78 if (!NT_STATUS_IS_OK(cli_openx(c, fname, flags, DENY_NONE, &fd))) {
79 return -1;
81 return fd;
84 case FSTYPE_NFS:
85 if (asprintf(&path, "%s%s", nfs, fname) > 0) {
86 int ret;
87 string_replace(path,'\\', '/');
88 ret = open(path, flags, 0666);
89 SAFE_FREE(path);
90 return ret;
92 break;
95 return -1;
98 static bool try_close(struct cli_state *c, int fstype, int fd)
100 switch (fstype) {
101 case FSTYPE_SMB:
102 return NT_STATUS_IS_OK(cli_close(c, fd));
104 case FSTYPE_NFS:
105 return close(fd) == 0;
108 return False;
111 static bool try_lock(struct cli_state *c, int fstype,
112 int fd, unsigned start, unsigned len,
113 enum brl_type op)
115 struct flock lock;
117 switch (fstype) {
118 case FSTYPE_SMB:
119 return NT_STATUS_IS_OK(cli_lock32(c, fd, start, len,
120 LOCK_TIMEOUT, op));
122 case FSTYPE_NFS:
123 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
124 lock.l_whence = SEEK_SET;
125 lock.l_start = start;
126 lock.l_len = len;
127 lock.l_pid = getpid();
128 return fcntl(fd,F_SETLK,&lock) == 0;
131 return False;
134 static bool try_unlock(struct cli_state *c, int fstype,
135 int fd, unsigned start, unsigned len)
137 struct flock lock;
139 switch (fstype) {
140 case FSTYPE_SMB:
141 return NT_STATUS_IS_OK(cli_unlock(c, fd, start, len));
143 case FSTYPE_NFS:
144 lock.l_type = F_UNLCK;
145 lock.l_whence = SEEK_SET;
146 lock.l_start = start;
147 lock.l_len = len;
148 lock.l_pid = getpid();
149 return fcntl(fd,F_SETLK,&lock) == 0;
152 return False;
155 static void print_brl(struct file_id id, struct server_id pid,
156 enum brl_type lock_type,
157 enum brl_flavour lock_flav,
158 br_off start, br_off size,
159 void *private_data)
161 struct file_id_buf idbuf;
163 printf("%6d %s %s %.0f:%.0f(%.0f)\n",
164 (int)procid_to_pid(&pid),
165 file_id_str_buf(id, &idbuf),
166 lock_type==READ_LOCK?"R":"W",
167 (double)start, (double)start+size-1,(double)size);
171 /*****************************************************
172 return a connection to a server
173 *******************************************************/
174 static struct cli_state *connect_one(char *share)
176 struct cli_state *c;
177 char *server_n;
178 fstring server;
179 fstring myname;
180 static int count;
181 NTSTATUS nt_status;
182 bool use_kerberos = false;
183 bool fallback_after_kerberos = false;
184 bool use_ccache = false;
185 bool pw_nt_hash = false;
186 struct cli_credentials *creds = NULL;
188 fstrcpy(server,share+2);
189 share = strchr_m(server,'\\');
190 if (!share) return NULL;
191 *share = 0;
192 share++;
194 server_n = server;
196 if (!got_pass) {
197 char pwd[256] = {0};
198 int rc;
200 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
201 if (rc == 0) {
202 fstrcpy(password, pwd);
206 creds = cli_session_creds_init(NULL,
207 username,
208 lp_workgroup(),
209 NULL, /* realm (use default) */
210 password,
211 use_kerberos,
212 fallback_after_kerberos,
213 use_ccache,
214 pw_nt_hash);
215 if (creds == NULL) {
216 DEBUG(0, ("cli_session_creds_init failed\n"));
217 return NULL;
220 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
222 nt_status = cli_full_connection_creds(NULL,
224 myname,
225 server_n,
226 NULL,
228 share,
229 "?????",
230 creds,
232 TALLOC_FREE(creds);
233 if (!NT_STATUS_IS_OK(nt_status)) {
234 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
235 return NULL;
238 c->use_oplocks = use_oplocks;
240 return c;
244 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
245 char *nfs[NSERVERS],
246 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
247 char *share1, char *share2)
249 int server, conn, f, fstype;
250 char *share[2];
251 share[0] = share1;
252 share[1] = share2;
254 fstype = FSTYPE_SMB;
256 for (server=0;server<NSERVERS;server++)
257 for (conn=0;conn<NCONNECTIONS;conn++) {
258 if (cli[server][conn]) {
259 for (f=0;f<NFILES;f++) {
260 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
262 cli_ulogoff(cli[server][conn]);
263 cli_shutdown(cli[server][conn]);
265 cli[server][conn] = connect_one(share[server]);
266 if (!cli[server][conn]) {
267 DEBUG(0,("Failed to connect to %s\n", share[server]));
268 exit(1);
275 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
276 char *nfs[NSERVERS],
277 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
278 struct record *rec)
280 unsigned conn = rec->conn;
281 unsigned f = rec->f;
282 unsigned fstype = rec->fstype;
283 unsigned start = rec->start;
284 unsigned len = rec->len;
285 unsigned r1 = rec->r1;
286 unsigned r2 = rec->r2;
287 enum brl_type op;
288 int server;
289 bool ret[NSERVERS];
291 if (r1 < READ_PCT) {
292 op = READ_LOCK;
293 } else {
294 op = WRITE_LOCK;
297 if (fstype >= NUMFSTYPES) {
298 return false;
301 if (r2 < LOCK_PCT) {
302 /* set a lock */
303 for (server=0;server<NSERVERS;server++) {
304 ret[server] = try_lock(cli[server][conn], fstype,
305 fnum[server][fstype][conn][f],
306 start, len, op);
308 if (showall || ret[0] != ret[1]) {
309 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
310 conn, fstype, f,
311 start, start+len-1, len,
312 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
313 ret[0], ret[1]);
315 if (showall) brl_forall(print_brl, NULL);
316 if (ret[0] != ret[1]) return False;
317 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
318 /* unset a lock */
319 for (server=0;server<NSERVERS;server++) {
320 ret[server] = try_unlock(cli[server][conn], fstype,
321 fnum[server][fstype][conn][f],
322 start, len);
324 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
325 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
326 conn, fstype, f,
327 start, start+len-1, len,
328 ret[0], ret[1]);
330 if (showall) brl_forall(print_brl, NULL);
331 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
332 } else {
333 /* reopen the file */
334 for (server=0;server<NSERVERS;server++) {
335 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
336 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
337 O_RDWR|O_CREAT);
338 if (fnum[server][fstype][conn][f] == -1) {
339 printf("failed to reopen on share1\n");
340 return False;
343 if (showall) {
344 printf("reopen conn=%u fstype=%u f=%u\n",
345 conn, fstype, f);
346 brl_forall(print_brl, NULL);
349 return True;
352 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
353 char *nfs[NSERVERS],
354 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
356 int server, conn, f, fstype;
358 for (server=0;server<NSERVERS;server++)
359 for (fstype=0;fstype<NUMFSTYPES;fstype++)
360 for (conn=0;conn<NCONNECTIONS;conn++)
361 for (f=0;f<NFILES;f++) {
362 if (fnum[server][fstype][conn][f] != -1) {
363 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
364 fnum[server][fstype][conn][f] = -1;
367 for (server=0;server<NSERVERS;server++) {
368 cli_unlink(cli[server][0], FILENAME, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
372 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
373 char *nfs[NSERVERS],
374 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
376 int server, fstype, conn, f;
378 for (server=0;server<NSERVERS;server++)
379 for (fstype=0;fstype<NUMFSTYPES;fstype++)
380 for (conn=0;conn<NCONNECTIONS;conn++)
381 for (f=0;f<NFILES;f++) {
382 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
383 O_RDWR|O_CREAT);
384 if (fnum[server][fstype][conn][f] == -1) {
385 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
386 server, fstype, conn, f);
387 exit(1);
393 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
394 char *nfs[NSERVERS],
395 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
396 int n)
398 int i;
399 printf("testing %u ...\n", n);
400 for (i=0; i<n; i++) {
401 if (i && i % 100 == 0) {
402 printf("%u\n", i);
405 if (recorded[i].needed &&
406 !test_one(cli, nfs, fnum, &recorded[i])) return i;
408 return n;
412 /* each server has two connections open to it. Each connection has two file
413 descriptors open on the file - 8 file descriptors in total
415 we then do random locking ops in tamdem on the 4 fnums from each
416 server and ensure that the results match
418 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
420 struct cli_state *cli[NSERVERS][NCONNECTIONS];
421 char *nfs[NSERVERS];
422 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
423 int n, i, n1;
425 nfs[0] = nfspath1;
426 nfs[1] = nfspath2;
428 ZERO_STRUCT(fnum);
429 ZERO_STRUCT(cli);
431 recorded = SMB_MALLOC_ARRAY(struct record, numops);
433 for (n=0; n<numops; n++) {
434 recorded[n].conn = random() % NCONNECTIONS;
435 recorded[n].fstype = random() % NUMFSTYPES;
436 recorded[n].f = random() % NFILES;
437 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
438 recorded[n].len = 1 +
439 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
440 recorded[n].start *= RANGE_MULTIPLE;
441 recorded[n].len *= RANGE_MULTIPLE;
442 recorded[n].r1 = random() % 100;
443 recorded[n].r2 = random() % 100;
444 recorded[n].needed = True;
447 reconnect(cli, nfs, fnum, share1, share2);
448 open_files(cli, nfs, fnum);
449 n = retest(cli, nfs, fnum, numops);
451 if (n == numops || !analyze) return;
452 n++;
454 while (1) {
455 n1 = n;
457 close_files(cli, nfs, fnum);
458 reconnect(cli, nfs, fnum, share1, share2);
459 open_files(cli, nfs, fnum);
461 for (i=0;i<n-1;i++) {
462 int m;
463 recorded[i].needed = False;
465 close_files(cli, nfs, fnum);
466 open_files(cli, nfs, fnum);
468 m = retest(cli, nfs, fnum, n);
469 if (m == n) {
470 recorded[i].needed = True;
471 } else {
472 if (i < m) {
473 memmove(&recorded[i], &recorded[i+1],
474 (m-i)*sizeof(recorded[0]));
476 n = m;
477 i--;
481 if (n1 == n) break;
484 close_files(cli, nfs, fnum);
485 reconnect(cli, nfs, fnum, share1, share2);
486 open_files(cli, nfs, fnum);
487 showall = True;
488 n1 = retest(cli, nfs, fnum, n);
489 if (n1 != n-1) {
490 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
492 close_files(cli, nfs, fnum);
494 for (i=0;i<n;i++) {
495 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
496 recorded[i].r1,
497 recorded[i].r2,
498 recorded[i].conn,
499 recorded[i].fstype,
500 recorded[i].f,
501 recorded[i].start,
502 recorded[i].len,
503 recorded[i].needed);
509 static void usage(void)
511 printf(
512 "Usage:\n\
513 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
514 options:\n\
515 -U user%%pass\n\
516 -s seed\n\
517 -o numops\n\
518 -u hide unlock fails\n\
519 -a (show all ops)\n\
520 -O use oplocks\n\
524 /****************************************************************************
525 main program
526 ****************************************************************************/
527 int main(int argc,char *argv[])
529 char *share1, *share2, *nfspath1, *nfspath2;
530 int opt;
531 char *p;
532 int seed;
534 setlinebuf(stdout);
536 if (argc < 5 || argv[1][0] == '-') {
537 usage();
538 exit(1);
541 setup_logging(argv[0], DEBUG_STDOUT);
543 share1 = argv[1];
544 share2 = argv[2];
545 nfspath1 = argv[3];
546 nfspath2 = argv[4];
548 all_string_sub(share1,"/","\\",0);
549 all_string_sub(share2,"/","\\",0);
551 argc -= 4;
552 argv += 4;
554 lp_load_global(get_dyn_CONFIGFILE());
555 load_interfaces();
557 if (getenv("USER")) {
558 fstrcpy(username,getenv("USER"));
561 seed = time(NULL);
563 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
564 switch (opt) {
565 case 'U':
566 fstrcpy(username,optarg);
567 p = strchr_m(username,'%');
568 if (p) {
569 *p = 0;
570 fstrcpy(password, p+1);
571 got_pass = 1;
573 break;
574 case 's':
575 seed = atoi(optarg);
576 break;
577 case 'u':
578 hide_unlock_fails = True;
579 break;
580 case 'o':
581 numops = atoi(optarg);
582 break;
583 case 'O':
584 use_oplocks = True;
585 break;
586 case 'a':
587 showall = True;
588 break;
589 case 'A':
590 analyze = True;
591 break;
592 case 'h':
593 usage();
594 exit(1);
595 default:
596 printf("Unknown option %c (%d)\n", (char)opt, opt);
597 exit(1);
601 argc -= optind;
602 argv += optind;
604 DEBUG(0,("seed=%u\n", seed));
605 srandom(seed);
607 locking_init_readonly();
608 test_locks(share1, share2, nfspath1, nfspath2);
610 return(0);