Convert cli_posix_unlink() and cli_posix_rmdir()
[nivanova/samba-autobuild/.git] / source3 / torture / torture.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Andrew Tridgell 1997-1998
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 "wbc_async.h"
22
23 extern char *optarg;
24 extern int optind;
25
26 static fstring host, workgroup, share, password, username, myname;
27 static int max_protocol = PROTOCOL_NT1;
28 static const char *sockops="TCP_NODELAY";
29 static int nprocs=1;
30 static int port_to_use=0;
31 int torture_numops=100;
32 int torture_blocksize=1024*1024;
33 static int procnum; /* records process count number when forking */
34 static struct cli_state *current_cli;
35 static fstring randomfname;
36 static bool use_oplocks;
37 static bool use_level_II_oplocks;
38 static const char *client_txt = "client_oplocks.txt";
39 static bool use_kerberos;
40 static fstring multishare_conn_fname;
41 static bool use_multishare_conn = False;
42 static bool do_encrypt;
43
44 bool torture_showall = False;
45
46 static double create_procs(bool (*fn)(int), bool *result);
47
48
49 static struct timeval tp1,tp2;
50
51
52 void start_timer(void)
53 {
54         GetTimeOfDay(&tp1);
55 }
56
57 double end_timer(void)
58 {
59         GetTimeOfDay(&tp2);
60         return((tp2.tv_sec - tp1.tv_sec) + 
61                (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
62 }
63
64
65 /* return a pointer to a anonymous shared memory segment of size "size"
66    which will persist across fork() but will disappear when all processes
67    exit 
68
69    The memory is not zeroed 
70
71    This function uses system5 shared memory. It takes advantage of a property
72    that the memory is not destroyed if it is attached when the id is removed
73    */
74 void *shm_setup(int size)
75 {
76         int shmid;
77         void *ret;
78
79         shmid = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
80         if (shmid == -1) {
81                 printf("can't get shared memory\n");
82                 exit(1);
83         }
84         ret = (void *)shmat(shmid, 0, 0);
85         if (!ret || ret == (void *)-1) {
86                 printf("can't attach to shared memory\n");
87                 return NULL;
88         }
89         /* the following releases the ipc, but note that this process
90            and all its children will still have access to the memory, its
91            just that the shmid is no longer valid for other shm calls. This
92            means we don't leave behind lots of shm segments after we exit 
93
94            See Stevens "advanced programming in unix env" for details
95            */
96         shmctl(shmid, IPC_RMID, 0);
97
98         return ret;
99 }
100
101 /********************************************************************
102  Ensure a connection is encrypted.
103 ********************************************************************/
104
105 static bool force_cli_encryption(struct cli_state *c,
106                         const char *sharename)
107 {
108         uint16 major, minor;
109         uint32 caplow, caphigh;
110         NTSTATUS status;
111
112         if (!SERVER_HAS_UNIX_CIFS(c)) {
113                 d_printf("Encryption required and "
114                         "server that doesn't support "
115                         "UNIX extensions - failing connect\n");
116                         return false;
117         }
118
119         if (!cli_unix_extensions_version(c, &major, &minor, &caplow, &caphigh)) {
120                 d_printf("Encryption required and "
121                         "can't get UNIX CIFS extensions "
122                         "version from server.\n");
123                 return false;
124         }
125
126         if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
127                 d_printf("Encryption required and "
128                         "share %s doesn't support "
129                         "encryption.\n", sharename);
130                 return false;
131         }
132
133         if (c->use_kerberos) {
134                 status = cli_gss_smb_encryption_start(c);
135         } else {
136                 status = cli_raw_ntlm_smb_encryption_start(c,
137                                                 username,
138                                                 password,
139                                                 workgroup);
140         }
141
142         if (!NT_STATUS_IS_OK(status)) {
143                 d_printf("Encryption required and "
144                         "setup failed with error %s.\n",
145                         nt_errstr(status));
146                 return false;
147         }
148
149         return true;
150 }
151
152
153 static struct cli_state *open_nbt_connection(void)
154 {
155         struct nmb_name called, calling;
156         struct sockaddr_storage ss;
157         struct cli_state *c;
158         NTSTATUS status;
159
160         make_nmb_name(&calling, myname, 0x0);
161         make_nmb_name(&called , host, 0x20);
162
163         zero_sockaddr(&ss);
164
165         if (!(c = cli_initialise())) {
166                 printf("Failed initialize cli_struct to connect with %s\n", host);
167                 return NULL;
168         }
169
170         c->port = port_to_use;
171
172         status = cli_connect(c, host, &ss);
173         if (!NT_STATUS_IS_OK(status)) {
174                 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
175                 return NULL;
176         }
177
178         c->use_kerberos = use_kerberos;
179
180         c->timeout = 120000; /* set a really long timeout (2 minutes) */
181         if (use_oplocks) c->use_oplocks = True;
182         if (use_level_II_oplocks) c->use_level_II_oplocks = True;
183
184         if (!cli_session_request(c, &calling, &called)) {
185                 /*
186                  * Well, that failed, try *SMBSERVER ...
187                  * However, we must reconnect as well ...
188                  */
189                 status = cli_connect(c, host, &ss);
190                 if (!NT_STATUS_IS_OK(status)) {
191                         printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
192                         return NULL;
193                 }
194
195                 make_nmb_name(&called, "*SMBSERVER", 0x20);
196                 if (!cli_session_request(c, &calling, &called)) {
197                         printf("%s rejected the session\n",host);
198                         printf("We tried with a called name of %s & %s\n",
199                                 host, "*SMBSERVER");
200                         cli_shutdown(c);
201                         return NULL;
202                 }
203         }
204
205         return c;
206 }
207
208 /* Insert a NULL at the first separator of the given path and return a pointer
209  * to the remainder of the string.
210  */
211 static char *
212 terminate_path_at_separator(char * path)
213 {
214         char * p;
215
216         if (!path) {
217                 return NULL;
218         }
219
220         if ((p = strchr_m(path, '/'))) {
221                 *p = '\0';
222                 return p + 1;
223         }
224
225         if ((p = strchr_m(path, '\\'))) {
226                 *p = '\0';
227                 return p + 1;
228         }
229
230         /* No separator. */
231         return NULL;
232 }
233
234 /*
235   parse a //server/share type UNC name
236 */
237 bool smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
238                       char **hostname, char **sharename)
239 {
240         char *p;
241
242         *hostname = *sharename = NULL;
243
244         if (strncmp(unc_name, "\\\\", 2) &&
245             strncmp(unc_name, "//", 2)) {
246                 return False;
247         }
248
249         *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
250         p = terminate_path_at_separator(*hostname);
251
252         if (p && *p) {
253                 *sharename = talloc_strdup(mem_ctx, p);
254                 terminate_path_at_separator(*sharename);
255         }
256
257         if (*hostname && *sharename) {
258                 return True;
259         }
260
261         TALLOC_FREE(*hostname);
262         TALLOC_FREE(*sharename);
263         return False;
264 }
265
266 static bool torture_open_connection_share(struct cli_state **c,
267                                    const char *hostname, 
268                                    const char *sharename)
269 {
270         bool retry;
271         int flags = 0;
272         NTSTATUS status;
273
274         if (use_kerberos)
275                 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
276         if (use_oplocks)
277                 flags |= CLI_FULL_CONNECTION_OPLOCKS;
278         if (use_level_II_oplocks)
279                 flags |= CLI_FULL_CONNECTION_LEVEL_II_OPLOCKS;
280
281         status = cli_full_connection(c, myname,
282                                      hostname, NULL, port_to_use, 
283                                      sharename, "?????", 
284                                      username, workgroup, 
285                                      password, flags, Undefined, &retry);
286         if (!NT_STATUS_IS_OK(status)) {
287                 printf("failed to open share connection: //%s/%s port:%d - %s\n",
288                         hostname, sharename, port_to_use, nt_errstr(status));
289                 return False;
290         }
291
292         (*c)->timeout = 120000; /* set a really long timeout (2 minutes) */
293
294         if (do_encrypt) {
295                 return force_cli_encryption(*c,
296                                         sharename);
297         }
298         return True;
299 }
300
301 bool torture_open_connection(struct cli_state **c, int conn_index)
302 {
303         char **unc_list = NULL;
304         int num_unc_names = 0;
305         bool result;
306
307         if (use_multishare_conn==True) {
308                 char *h, *s;
309                 unc_list = file_lines_load(multishare_conn_fname, &num_unc_names, 0, NULL);
310                 if (!unc_list || num_unc_names <= 0) {
311                         printf("Failed to load unc names list from '%s'\n", multishare_conn_fname);
312                         exit(1);
313                 }
314
315                 if (!smbcli_parse_unc(unc_list[conn_index % num_unc_names],
316                                       NULL, &h, &s)) {
317                         printf("Failed to parse UNC name %s\n",
318                                unc_list[conn_index % num_unc_names]);
319                         TALLOC_FREE(unc_list);
320                         exit(1);
321                 }
322
323                 result = torture_open_connection_share(c, h, s);
324
325                 /* h, s were copied earlier */
326                 TALLOC_FREE(unc_list);
327                 return result;
328         }
329
330         return torture_open_connection_share(c, host, share);
331 }
332
333 bool torture_cli_session_setup2(struct cli_state *cli, uint16 *new_vuid)
334 {
335         uint16 old_vuid = cli->vuid;
336         fstring old_user_name;
337         size_t passlen = strlen(password);
338         NTSTATUS status;
339         bool ret;
340
341         fstrcpy(old_user_name, cli->user_name);
342         cli->vuid = 0;
343         ret = NT_STATUS_IS_OK(cli_session_setup(cli, username,
344                                                 password, passlen,
345                                                 password, passlen,
346                                                 workgroup));
347         *new_vuid = cli->vuid;
348         cli->vuid = old_vuid;
349         status = cli_set_username(cli, old_user_name);
350         if (!NT_STATUS_IS_OK(status)) {
351                 return false;
352         }
353         return ret;
354 }
355
356
357 bool torture_close_connection(struct cli_state *c)
358 {
359         bool ret = True;
360         if (!cli_tdis(c)) {
361                 printf("tdis failed (%s)\n", cli_errstr(c));
362                 ret = False;
363         }
364
365         cli_shutdown(c);
366
367         return ret;
368 }
369
370
371 /* check if the server produced the expected error code */
372 static bool check_error(int line, struct cli_state *c, 
373                         uint8 eclass, uint32 ecode, NTSTATUS nterr)
374 {
375         if (cli_is_dos_error(c)) {
376                 uint8 cclass;
377                 uint32 num;
378
379                 /* Check DOS error */
380
381                 cli_dos_error(c, &cclass, &num);
382
383                 if (eclass != cclass || ecode != num) {
384                         printf("unexpected error code class=%d code=%d\n", 
385                                (int)cclass, (int)num);
386                         printf(" expected %d/%d %s (line=%d)\n", 
387                                (int)eclass, (int)ecode, nt_errstr(nterr), line);
388                         return False;
389                 }
390
391         } else {
392                 NTSTATUS status;
393
394                 /* Check NT error */
395
396                 status = cli_nt_error(c);
397
398                 if (NT_STATUS_V(nterr) != NT_STATUS_V(status)) {
399                         printf("unexpected error code %s\n", nt_errstr(status));
400                         printf(" expected %s (line=%d)\n", nt_errstr(nterr), line);
401                         return False;
402                 }
403         }
404
405         return True;
406 }
407
408
409 static bool wait_lock(struct cli_state *c, int fnum, uint32 offset, uint32 len)
410 {
411         while (!cli_lock(c, fnum, offset, len, -1, WRITE_LOCK)) {
412                 if (!check_error(__LINE__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
413         }
414         return True;
415 }
416
417
418 static bool rw_torture(struct cli_state *c)
419 {
420         const char *lockfname = "\\torture.lck";
421         fstring fname;
422         int fnum;
423         int fnum2;
424         pid_t pid2, pid = getpid();
425         int i, j;
426         char buf[1024];
427         bool correct = True;
428
429         memset(buf, '\0', sizeof(buf));
430
431         fnum2 = cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL, 
432                          DENY_NONE);
433         if (fnum2 == -1)
434                 fnum2 = cli_open(c, lockfname, O_RDWR, DENY_NONE);
435         if (fnum2 == -1) {
436                 printf("open of %s failed (%s)\n", lockfname, cli_errstr(c));
437                 return False;
438         }
439
440
441         for (i=0;i<torture_numops;i++) {
442                 unsigned n = (unsigned)sys_random()%10;
443                 if (i % 10 == 0) {
444                         printf("%d\r", i); fflush(stdout);
445                 }
446                 slprintf(fname, sizeof(fstring) - 1, "\\torture.%u", n);
447
448                 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
449                         return False;
450                 }
451
452                 fnum = cli_open(c, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL);
453                 if (fnum == -1) {
454                         printf("open failed (%s)\n", cli_errstr(c));
455                         correct = False;
456                         break;
457                 }
458
459                 if (cli_write(c, fnum, 0, (char *)&pid, 0, sizeof(pid)) != sizeof(pid)) {
460                         printf("write failed (%s)\n", cli_errstr(c));
461                         correct = False;
462                 }
463
464                 for (j=0;j<50;j++) {
465                         if (cli_write(c, fnum, 0, (char *)buf, 
466                                       sizeof(pid)+(j*sizeof(buf)), 
467                                       sizeof(buf)) != sizeof(buf)) {
468                                 printf("write failed (%s)\n", cli_errstr(c));
469                                 correct = False;
470                         }
471                 }
472
473                 pid2 = 0;
474
475                 if (cli_read(c, fnum, (char *)&pid2, 0, sizeof(pid)) != sizeof(pid)) {
476                         printf("read failed (%s)\n", cli_errstr(c));
477                         correct = False;
478                 }
479
480                 if (pid2 != pid) {
481                         printf("data corruption!\n");
482                         correct = False;
483                 }
484
485                 if (!cli_close(c, fnum)) {
486                         printf("close failed (%s)\n", cli_errstr(c));
487                         correct = False;
488                 }
489
490                 if (!cli_unlink(c, fname)) {
491                         printf("unlink failed (%s)\n", cli_errstr(c));
492                         correct = False;
493                 }
494
495                 if (!cli_unlock(c, fnum2, n*sizeof(int), sizeof(int))) {
496                         printf("unlock failed (%s)\n", cli_errstr(c));
497                         correct = False;
498                 }
499         }
500
501         cli_close(c, fnum2);
502         cli_unlink(c, lockfname);
503
504         printf("%d\n", i);
505
506         return correct;
507 }
508
509 static bool run_torture(int dummy)
510 {
511         struct cli_state *cli;
512         bool ret;
513
514         cli = current_cli;
515
516         cli_sockopt(cli, sockops);
517
518         ret = rw_torture(cli);
519
520         if (!torture_close_connection(cli)) {
521                 ret = False;
522         }
523
524         return ret;
525 }
526
527 static bool rw_torture3(struct cli_state *c, char *lockfname)
528 {
529         int fnum = -1;
530         unsigned int i = 0;
531         char buf[131072];
532         char buf_rd[131072];
533         unsigned count;
534         unsigned countprev = 0;
535         ssize_t sent = 0;
536         bool correct = True;
537
538         srandom(1);
539         for (i = 0; i < sizeof(buf); i += sizeof(uint32))
540         {
541                 SIVAL(buf, i, sys_random());
542         }
543
544         if (procnum == 0)
545         {
546                 fnum = cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL, 
547                                  DENY_NONE);
548                 if (fnum == -1) {
549                         printf("first open read/write of %s failed (%s)\n",
550                                         lockfname, cli_errstr(c));
551                         return False;
552                 }
553         }
554         else
555         {
556                 for (i = 0; i < 500 && fnum == -1; i++)
557                 {
558                         fnum = cli_open(c, lockfname, O_RDONLY, 
559                                          DENY_NONE);
560                         smb_msleep(10);
561                 }
562                 if (fnum == -1) {
563                         printf("second open read-only of %s failed (%s)\n",
564                                         lockfname, cli_errstr(c));
565                         return False;
566                 }
567         }
568
569         i = 0;
570         for (count = 0; count < sizeof(buf); count += sent)
571         {
572                 if (count >= countprev) {
573                         printf("%d %8d\r", i, count);
574                         fflush(stdout);
575                         i++;
576                         countprev += (sizeof(buf) / 20);
577                 }
578
579                 if (procnum == 0)
580                 {
581                         sent = ((unsigned)sys_random()%(20))+ 1;
582                         if (sent > sizeof(buf) - count)
583                         {
584                                 sent = sizeof(buf) - count;
585                         }
586
587                         if (cli_write(c, fnum, 0, buf+count, count, (size_t)sent) != sent) {
588                                 printf("write failed (%s)\n", cli_errstr(c));
589                                 correct = False;
590                         }
591                 }
592                 else
593                 {
594                         sent = cli_read(c, fnum, buf_rd+count, count,
595                                                   sizeof(buf)-count);
596                         if (sent < 0)
597                         {
598                                 printf("read failed offset:%d size:%ld (%s)\n",
599                                        count, (unsigned long)sizeof(buf)-count,
600                                        cli_errstr(c));
601                                 correct = False;
602                                 sent = 0;
603                         }
604                         if (sent > 0)
605                         {
606                                 if (memcmp(buf_rd+count, buf+count, sent) != 0)
607                                 {
608                                         printf("read/write compare failed\n");
609                                         printf("offset: %d req %ld recvd %ld\n", count, (unsigned long)sizeof(buf)-count, (unsigned long)sent);
610                                         correct = False;
611                                         break;
612                                 }
613                         }
614                 }
615
616         }
617
618         if (!cli_close(c, fnum)) {
619                 printf("close failed (%s)\n", cli_errstr(c));
620                 correct = False;
621         }
622
623         return correct;
624 }
625
626 static bool rw_torture2(struct cli_state *c1, struct cli_state *c2)
627 {
628         const char *lockfname = "\\torture2.lck";
629         int fnum1;
630         int fnum2;
631         int i;
632         char buf[131072];
633         char buf_rd[131072];
634         bool correct = True;
635         ssize_t bytes_read;
636
637         if (!cli_unlink(c1, lockfname)) {
638                 printf("unlink failed (%s) (normal, this file should not exist)\n", cli_errstr(c1));
639         }
640
641         fnum1 = cli_open(c1, lockfname, O_RDWR | O_CREAT | O_EXCL, 
642                          DENY_NONE);
643         if (fnum1 == -1) {
644                 printf("first open read/write of %s failed (%s)\n",
645                                 lockfname, cli_errstr(c1));
646                 return False;
647         }
648         fnum2 = cli_open(c2, lockfname, O_RDONLY, 
649                          DENY_NONE);
650         if (fnum2 == -1) {
651                 printf("second open read-only of %s failed (%s)\n",
652                                 lockfname, cli_errstr(c2));
653                 cli_close(c1, fnum1);
654                 return False;
655         }
656
657         for (i=0;i<torture_numops;i++)
658         {
659                 size_t buf_size = ((unsigned)sys_random()%(sizeof(buf)-1))+ 1;
660                 if (i % 10 == 0) {
661                         printf("%d\r", i); fflush(stdout);
662                 }
663
664                 generate_random_buffer((unsigned char *)buf, buf_size);
665
666                 if (cli_write(c1, fnum1, 0, buf, 0, buf_size) != buf_size) {
667                         printf("write failed (%s)\n", cli_errstr(c1));
668                         correct = False;
669                         break;
670                 }
671
672                 if ((bytes_read = cli_read(c2, fnum2, buf_rd, 0, buf_size)) != buf_size) {
673                         printf("read failed (%s)\n", cli_errstr(c2));
674                         printf("read %d, expected %ld\n", (int)bytes_read, 
675                                (unsigned long)buf_size); 
676                         correct = False;
677                         break;
678                 }
679
680                 if (memcmp(buf_rd, buf, buf_size) != 0)
681                 {
682                         printf("read/write compare failed\n");
683                         correct = False;
684                         break;
685                 }
686         }
687
688         if (!cli_close(c2, fnum2)) {
689                 printf("close failed (%s)\n", cli_errstr(c2));
690                 correct = False;
691         }
692         if (!cli_close(c1, fnum1)) {
693                 printf("close failed (%s)\n", cli_errstr(c1));
694                 correct = False;
695         }
696
697         if (!cli_unlink(c1, lockfname)) {
698                 printf("unlink failed (%s)\n", cli_errstr(c1));
699                 correct = False;
700         }
701
702         return correct;
703 }
704
705 static bool run_readwritetest(int dummy)
706 {
707         static struct cli_state *cli1, *cli2;
708         bool test1, test2 = False;
709
710         if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
711                 return False;
712         }
713         cli_sockopt(cli1, sockops);
714         cli_sockopt(cli2, sockops);
715
716         printf("starting readwritetest\n");
717
718         test1 = rw_torture2(cli1, cli2);
719         printf("Passed readwritetest v1: %s\n", BOOLSTR(test1));
720
721         if (test1) {
722                 test2 = rw_torture2(cli1, cli1);
723                 printf("Passed readwritetest v2: %s\n", BOOLSTR(test2));
724         }
725
726         if (!torture_close_connection(cli1)) {
727                 test1 = False;
728         }
729
730         if (!torture_close_connection(cli2)) {
731                 test2 = False;
732         }
733
734         return (test1 && test2);
735 }
736
737 static bool run_readwritemulti(int dummy)
738 {
739         struct cli_state *cli;
740         bool test;
741
742         cli = current_cli;
743
744         cli_sockopt(cli, sockops);
745
746         printf("run_readwritemulti: fname %s\n", randomfname);
747         test = rw_torture3(cli, randomfname);
748
749         if (!torture_close_connection(cli)) {
750                 test = False;
751         }
752
753         return test;
754 }
755
756 static bool run_readwritelarge(int dummy)
757 {
758         static struct cli_state *cli1;
759         int fnum1;
760         const char *lockfname = "\\large.dat";
761         SMB_OFF_T fsize;
762         char buf[126*1024];
763         bool correct = True;
764
765         if (!torture_open_connection(&cli1, 0)) {
766                 return False;
767         }
768         cli_sockopt(cli1, sockops);
769         memset(buf,'\0',sizeof(buf));
770
771         cli1->max_xmit = 128*1024;
772
773         printf("starting readwritelarge\n");
774
775         cli_unlink(cli1, lockfname);
776
777         fnum1 = cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE);
778         if (fnum1 == -1) {
779                 printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
780                 return False;
781         }
782
783         cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf));
784
785         if (!cli_qfileinfo(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL, NULL, NULL)) {
786                 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
787                 correct = False;
788         }
789
790         if (fsize == sizeof(buf))
791                 printf("readwritelarge test 1 succeeded (size = %lx)\n", 
792                        (unsigned long)fsize);
793         else {
794                 printf("readwritelarge test 1 failed (size = %lx)\n", 
795                        (unsigned long)fsize);
796                 correct = False;
797         }
798
799         if (!cli_close(cli1, fnum1)) {
800                 printf("close failed (%s)\n", cli_errstr(cli1));
801                 correct = False;
802         }
803
804         if (!cli_unlink(cli1, lockfname)) {
805                 printf("unlink failed (%s)\n", cli_errstr(cli1));
806                 correct = False;
807         }
808
809         fnum1 = cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE);
810         if (fnum1 == -1) {
811                 printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
812                 return False;
813         }
814
815         cli1->max_xmit = 4*1024;
816
817         cli_smbwrite(cli1, fnum1, buf, 0, sizeof(buf));
818
819         if (!cli_qfileinfo(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL, NULL, NULL)) {
820                 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
821                 correct = False;
822         }
823
824         if (fsize == sizeof(buf))
825                 printf("readwritelarge test 2 succeeded (size = %lx)\n", 
826                        (unsigned long)fsize);
827         else {
828                 printf("readwritelarge test 2 failed (size = %lx)\n", 
829                        (unsigned long)fsize);
830                 correct = False;
831         }
832
833 #if 0
834         /* ToDo - set allocation. JRA */
835         if(!cli_set_allocation_size(cli1, fnum1, 0)) {
836                 printf("set allocation size to zero failed (%s)\n", cli_errstr(&cli1));
837                 return False;
838         }
839         if (!cli_qfileinfo(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL, NULL, NULL)) {
840                 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
841                 correct = False;
842         }
843         if (fsize != 0)
844                 printf("readwritelarge test 3 (truncate test) succeeded (size = %x)\n", fsize);
845 #endif
846
847         if (!cli_close(cli1, fnum1)) {
848                 printf("close failed (%s)\n", cli_errstr(cli1));
849                 correct = False;
850         }
851
852         if (!torture_close_connection(cli1)) {
853                 correct = False;
854         }
855         return correct;
856 }
857
858 int line_count = 0;
859 int nbio_id;
860
861 #define ival(s) strtol(s, NULL, 0)
862
863 /* run a test that simulates an approximate netbench client load */
864 static bool run_netbench(int client)
865 {
866         struct cli_state *cli;
867         int i;
868         char line[1024];
869         char cname[20];
870         FILE *f;
871         const char *params[20];
872         bool correct = True;
873
874         cli = current_cli;
875
876         nbio_id = client;
877
878         cli_sockopt(cli, sockops);
879
880         nb_setup(cli);
881
882         slprintf(cname,sizeof(cname)-1, "client%d", client);
883
884         f = fopen(client_txt, "r");
885
886         if (!f) {
887                 perror(client_txt);
888                 return False;
889         }
890
891         while (fgets(line, sizeof(line)-1, f)) {
892                 char *saveptr;
893                 line_count++;
894
895                 line[strlen(line)-1] = 0;
896
897                 /* printf("[%d] %s\n", line_count, line); */
898
899                 all_string_sub(line,"client1", cname, sizeof(line));
900
901                 /* parse the command parameters */
902                 params[0] = strtok_r(line, " ", &saveptr);
903                 i = 0;
904                 while (params[i]) params[++i] = strtok_r(NULL, " ", &saveptr);
905
906                 params[i] = "";
907
908                 if (i < 2) continue;
909
910                 if (!strncmp(params[0],"SMB", 3)) {
911                         printf("ERROR: You are using a dbench 1 load file\n");
912                         exit(1);
913                 }
914
915                 if (!strcmp(params[0],"NTCreateX")) {
916                         nb_createx(params[1], ival(params[2]), ival(params[3]), 
917                                    ival(params[4]));
918                 } else if (!strcmp(params[0],"Close")) {
919                         nb_close(ival(params[1]));
920                 } else if (!strcmp(params[0],"Rename")) {
921                         nb_rename(params[1], params[2]);
922                 } else if (!strcmp(params[0],"Unlink")) {
923                         nb_unlink(params[1]);
924                 } else if (!strcmp(params[0],"Deltree")) {
925                         nb_deltree(params[1]);
926                 } else if (!strcmp(params[0],"Rmdir")) {
927                         nb_rmdir(params[1]);
928                 } else if (!strcmp(params[0],"QUERY_PATH_INFORMATION")) {
929                         nb_qpathinfo(params[1]);
930                 } else if (!strcmp(params[0],"QUERY_FILE_INFORMATION")) {
931                         nb_qfileinfo(ival(params[1]));
932                 } else if (!strcmp(params[0],"QUERY_FS_INFORMATION")) {
933                         nb_qfsinfo(ival(params[1]));
934                 } else if (!strcmp(params[0],"FIND_FIRST")) {
935                         nb_findfirst(params[1]);
936                 } else if (!strcmp(params[0],"WriteX")) {
937                         nb_writex(ival(params[1]), 
938                                   ival(params[2]), ival(params[3]), ival(params[4]));
939                 } else if (!strcmp(params[0],"ReadX")) {
940                         nb_readx(ival(params[1]), 
941                                   ival(params[2]), ival(params[3]), ival(params[4]));
942                 } else if (!strcmp(params[0],"Flush")) {
943                         nb_flush(ival(params[1]));
944                 } else {
945                         printf("Unknown operation %s\n", params[0]);
946                         exit(1);
947                 }
948         }
949         fclose(f);
950
951         nb_cleanup();
952
953         if (!torture_close_connection(cli)) {
954                 correct = False;
955         }
956
957         return correct;
958 }
959
960
961 /* run a test that simulates an approximate netbench client load */
962 static bool run_nbench(int dummy)
963 {
964         double t;
965         bool correct = True;
966
967         nbio_shmem(nprocs);
968
969         nbio_id = -1;
970
971         signal(SIGALRM, nb_alarm);
972         alarm(1);
973         t = create_procs(run_netbench, &correct);
974         alarm(0);
975
976         printf("\nThroughput %g MB/sec\n", 
977                1.0e-6 * nbio_total() / t);
978         return correct;
979 }
980
981
982 /*
983   This test checks for two things:
984
985   1) correct support for retaining locks over a close (ie. the server
986      must not use posix semantics)
987   2) support for lock timeouts
988  */
989 static bool run_locktest1(int dummy)
990 {
991         struct cli_state *cli1, *cli2;
992         const char *fname = "\\lockt1.lck";
993         int fnum1, fnum2, fnum3;
994         time_t t1, t2;
995         unsigned lock_timeout;
996
997         if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
998                 return False;
999         }
1000         cli_sockopt(cli1, sockops);
1001         cli_sockopt(cli2, sockops);
1002
1003         printf("starting locktest1\n");
1004
1005         cli_unlink(cli1, fname);
1006
1007         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1008         if (fnum1 == -1) {
1009                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
1010                 return False;
1011         }
1012         fnum2 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1013         if (fnum2 == -1) {
1014                 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli1));
1015                 return False;
1016         }
1017         fnum3 = cli_open(cli2, fname, O_RDWR, DENY_NONE);
1018         if (fnum3 == -1) {
1019                 printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli2));
1020                 return False;
1021         }
1022
1023         if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
1024                 printf("lock1 failed (%s)\n", cli_errstr(cli1));
1025                 return False;
1026         }
1027
1028
1029         if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
1030                 printf("lock2 succeeded! This is a locking bug\n");
1031                 return False;
1032         } else {
1033                 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock, 
1034                                  NT_STATUS_LOCK_NOT_GRANTED)) return False;
1035         }
1036
1037
1038         lock_timeout = (1 + (random() % 20));
1039         printf("Testing lock timeout with timeout=%u\n", lock_timeout);
1040         t1 = time(NULL);
1041         if (cli_lock(cli2, fnum3, 0, 4, lock_timeout * 1000, WRITE_LOCK)) {
1042                 printf("lock3 succeeded! This is a locking bug\n");
1043                 return False;
1044         } else {
1045                 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock, 
1046                                  NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1047         }
1048         t2 = time(NULL);
1049
1050         if (ABS(t2 - t1) < lock_timeout-1) {
1051                 printf("error: This server appears not to support timed lock requests\n");
1052         }
1053
1054         printf("server slept for %u seconds for a %u second timeout\n",
1055                (unsigned int)(t2-t1), lock_timeout);
1056
1057         if (!cli_close(cli1, fnum2)) {
1058                 printf("close1 failed (%s)\n", cli_errstr(cli1));
1059                 return False;
1060         }
1061
1062         if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
1063                 printf("lock4 succeeded! This is a locking bug\n");
1064                 return False;
1065         } else {
1066                 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock, 
1067                                  NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1068         }
1069
1070         if (!cli_close(cli1, fnum1)) {
1071                 printf("close2 failed (%s)\n", cli_errstr(cli1));
1072                 return False;
1073         }
1074
1075         if (!cli_close(cli2, fnum3)) {
1076                 printf("close3 failed (%s)\n", cli_errstr(cli2));
1077                 return False;
1078         }
1079
1080         if (!cli_unlink(cli1, fname)) {
1081                 printf("unlink failed (%s)\n", cli_errstr(cli1));
1082                 return False;
1083         }
1084
1085
1086         if (!torture_close_connection(cli1)) {
1087                 return False;
1088         }
1089
1090         if (!torture_close_connection(cli2)) {
1091                 return False;
1092         }
1093
1094         printf("Passed locktest1\n");
1095         return True;
1096 }
1097
1098 /*
1099   this checks to see if a secondary tconx can use open files from an
1100   earlier tconx
1101  */
1102 static bool run_tcon_test(int dummy)
1103 {
1104         static struct cli_state *cli;
1105         const char *fname = "\\tcontest.tmp";
1106         int fnum1;
1107         uint16 cnum1, cnum2, cnum3;
1108         uint16 vuid1, vuid2;
1109         char buf[4];
1110         bool ret = True;
1111         NTSTATUS status;
1112
1113         memset(buf, '\0', sizeof(buf));
1114
1115         if (!torture_open_connection(&cli, 0)) {
1116                 return False;
1117         }
1118         cli_sockopt(cli, sockops);
1119
1120         printf("starting tcontest\n");
1121
1122         cli_unlink(cli, fname);
1123
1124         fnum1 = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1125         if (fnum1 == -1) {
1126                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
1127                 return False;
1128         }
1129
1130         cnum1 = cli->cnum;
1131         vuid1 = cli->vuid;
1132
1133         if (cli_write(cli, fnum1, 0, buf, 130, 4) != 4) {
1134                 printf("initial write failed (%s)", cli_errstr(cli));
1135                 return False;
1136         }
1137
1138         status = cli_tcon_andx(cli, share, "?????",
1139                                password, strlen(password)+1);
1140         if (!NT_STATUS_IS_OK(status)) {
1141                 printf("%s refused 2nd tree connect (%s)\n", host,
1142                        nt_errstr(status));
1143                 cli_shutdown(cli);
1144                 return False;
1145         }
1146
1147         cnum2 = cli->cnum;
1148         cnum3 = MAX(cnum1, cnum2) + 1; /* any invalid number */
1149         vuid2 = cli->vuid + 1;
1150
1151         /* try a write with the wrong tid */
1152         cli->cnum = cnum2;
1153
1154         if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1155                 printf("* server allows write with wrong TID\n");
1156                 ret = False;
1157         } else {
1158                 printf("server fails write with wrong TID : %s\n", cli_errstr(cli));
1159         }
1160
1161
1162         /* try a write with an invalid tid */
1163         cli->cnum = cnum3;
1164
1165         if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1166                 printf("* server allows write with invalid TID\n");
1167                 ret = False;
1168         } else {
1169                 printf("server fails write with invalid TID : %s\n", cli_errstr(cli));
1170         }
1171
1172         /* try a write with an invalid vuid */
1173         cli->vuid = vuid2;
1174         cli->cnum = cnum1;
1175
1176         if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1177                 printf("* server allows write with invalid VUID\n");
1178                 ret = False;
1179         } else {
1180                 printf("server fails write with invalid VUID : %s\n", cli_errstr(cli));
1181         }
1182
1183         cli->cnum = cnum1;
1184         cli->vuid = vuid1;
1185
1186         if (!cli_close(cli, fnum1)) {
1187                 printf("close failed (%s)\n", cli_errstr(cli));
1188                 return False;
1189         }
1190
1191         cli->cnum = cnum2;
1192
1193         if (!cli_tdis(cli)) {
1194                 printf("secondary tdis failed (%s)\n", cli_errstr(cli));
1195                 return False;
1196         }
1197
1198         cli->cnum = cnum1;
1199
1200         if (!torture_close_connection(cli)) {
1201                 return False;
1202         }
1203
1204         return ret;
1205 }
1206
1207
1208 /*
1209  checks for old style tcon support
1210  */
1211 static bool run_tcon2_test(int dummy)
1212 {
1213         static struct cli_state *cli;
1214         uint16 cnum, max_xmit;
1215         char *service;
1216         NTSTATUS status;
1217
1218         if (!torture_open_connection(&cli, 0)) {
1219                 return False;
1220         }
1221         cli_sockopt(cli, sockops);
1222
1223         printf("starting tcon2 test\n");
1224
1225         if (asprintf(&service, "\\\\%s\\%s", host, share) == -1) {
1226                 return false;
1227         }
1228
1229         status = cli_raw_tcon(cli, service, password, "?????", &max_xmit, &cnum);
1230
1231         if (!NT_STATUS_IS_OK(status)) {
1232                 printf("tcon2 failed : %s\n", cli_errstr(cli));
1233         } else {
1234                 printf("tcon OK : max_xmit=%d cnum=%d tid=%d\n", 
1235                        (int)max_xmit, (int)cnum, SVAL(cli->inbuf, smb_tid));
1236         }
1237
1238         if (!torture_close_connection(cli)) {
1239                 return False;
1240         }
1241
1242         printf("Passed tcon2 test\n");
1243         return True;
1244 }
1245
1246 static bool tcon_devtest(struct cli_state *cli,
1247                          const char *myshare, const char *devtype,
1248                          const char *return_devtype,
1249                          NTSTATUS expected_error)
1250 {
1251         NTSTATUS status;
1252         bool ret;
1253
1254         status = cli_tcon_andx(cli, myshare, devtype,
1255                                password, strlen(password)+1);
1256
1257         if (NT_STATUS_IS_OK(expected_error)) {
1258                 if (NT_STATUS_IS_OK(status)) {
1259                         if (strcmp(cli->dev, return_devtype) == 0) {
1260                                 ret = True;
1261                         } else { 
1262                                 printf("tconX to share %s with type %s "
1263                                        "succeeded but returned the wrong "
1264                                        "device type (got [%s] but should have got [%s])\n",
1265                                        myshare, devtype, cli->dev, return_devtype);
1266                                 ret = False;
1267                         }
1268                 } else {
1269                         printf("tconX to share %s with type %s "
1270                                "should have succeeded but failed\n",
1271                                myshare, devtype);
1272                         ret = False;
1273                 }
1274                 cli_tdis(cli);
1275         } else {
1276                 if (NT_STATUS_IS_OK(status)) {
1277                         printf("tconx to share %s with type %s "
1278                                "should have failed but succeeded\n",
1279                                myshare, devtype);
1280                         ret = False;
1281                 } else {
1282                         if (NT_STATUS_EQUAL(cli_nt_error(cli),
1283                                             expected_error)) {
1284                                 ret = True;
1285                         } else {
1286                                 printf("Returned unexpected error\n");
1287                                 ret = False;
1288                         }
1289                 }
1290         }
1291         return ret;
1292 }
1293
1294 /*
1295  checks for correct tconX support
1296  */
1297 static bool run_tcon_devtype_test(int dummy)
1298 {
1299         static struct cli_state *cli1 = NULL;
1300         bool retry;
1301         int flags = 0;
1302         NTSTATUS status;
1303         bool ret = True;
1304
1305         status = cli_full_connection(&cli1, myname,
1306                                      host, NULL, port_to_use,
1307                                      NULL, NULL,
1308                                      username, workgroup,
1309                                      password, flags, Undefined, &retry);
1310
1311         if (!NT_STATUS_IS_OK(status)) {
1312                 printf("could not open connection\n");
1313                 return False;
1314         }
1315
1316         if (!tcon_devtest(cli1, "IPC$", "A:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1317                 ret = False;
1318
1319         if (!tcon_devtest(cli1, "IPC$", "?????", "IPC", NT_STATUS_OK))
1320                 ret = False;
1321
1322         if (!tcon_devtest(cli1, "IPC$", "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1323                 ret = False;
1324
1325         if (!tcon_devtest(cli1, "IPC$", "IPC", "IPC", NT_STATUS_OK))
1326                 ret = False;
1327
1328         if (!tcon_devtest(cli1, "IPC$", "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1329                 ret = False;
1330
1331         if (!tcon_devtest(cli1, share, "A:", "A:", NT_STATUS_OK))
1332                 ret = False;
1333
1334         if (!tcon_devtest(cli1, share, "?????", "A:", NT_STATUS_OK))
1335                 ret = False;
1336
1337         if (!tcon_devtest(cli1, share, "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1338                 ret = False;
1339
1340         if (!tcon_devtest(cli1, share, "IPC", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1341                 ret = False;
1342
1343         if (!tcon_devtest(cli1, share, "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1344                 ret = False;
1345
1346         cli_shutdown(cli1);
1347
1348         if (ret)
1349                 printf("Passed tcondevtest\n");
1350
1351         return ret;
1352 }
1353
1354
1355 /*
1356   This test checks that 
1357
1358   1) the server supports multiple locking contexts on the one SMB
1359   connection, distinguished by PID.  
1360
1361   2) the server correctly fails overlapping locks made by the same PID (this
1362      goes against POSIX behaviour, which is why it is tricky to implement)
1363
1364   3) the server denies unlock requests by an incorrect client PID
1365 */
1366 static bool run_locktest2(int dummy)
1367 {
1368         static struct cli_state *cli;
1369         const char *fname = "\\lockt2.lck";
1370         int fnum1, fnum2, fnum3;
1371         bool correct = True;
1372
1373         if (!torture_open_connection(&cli, 0)) {
1374                 return False;
1375         }
1376
1377         cli_sockopt(cli, sockops);
1378
1379         printf("starting locktest2\n");
1380
1381         cli_unlink(cli, fname);
1382
1383         cli_setpid(cli, 1);
1384
1385         fnum1 = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1386         if (fnum1 == -1) {
1387                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
1388                 return False;
1389         }
1390
1391         fnum2 = cli_open(cli, fname, O_RDWR, DENY_NONE);
1392         if (fnum2 == -1) {
1393                 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli));
1394                 return False;
1395         }
1396
1397         cli_setpid(cli, 2);
1398
1399         fnum3 = cli_open(cli, fname, O_RDWR, DENY_NONE);
1400         if (fnum3 == -1) {
1401                 printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli));
1402                 return False;
1403         }
1404
1405         cli_setpid(cli, 1);
1406
1407         if (!cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
1408                 printf("lock1 failed (%s)\n", cli_errstr(cli));
1409                 return False;
1410         }
1411
1412         if (cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
1413                 printf("WRITE lock1 succeeded! This is a locking bug\n");
1414                 correct = False;
1415         } else {
1416                 if (!check_error(__LINE__, cli, ERRDOS, ERRlock, 
1417                                  NT_STATUS_LOCK_NOT_GRANTED)) return False;
1418         }
1419
1420         if (cli_lock(cli, fnum2, 0, 4, 0, WRITE_LOCK)) {
1421                 printf("WRITE lock2 succeeded! This is a locking bug\n");
1422                 correct = False;
1423         } else {
1424                 if (!check_error(__LINE__, cli, ERRDOS, ERRlock, 
1425                                  NT_STATUS_LOCK_NOT_GRANTED)) return False;
1426         }
1427
1428         if (cli_lock(cli, fnum2, 0, 4, 0, READ_LOCK)) {
1429                 printf("READ lock2 succeeded! This is a locking bug\n");
1430                 correct = False;
1431         } else {
1432                 if (!check_error(__LINE__, cli, ERRDOS, ERRlock, 
1433                                  NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1434         }
1435
1436         if (!cli_lock(cli, fnum1, 100, 4, 0, WRITE_LOCK)) {
1437                 printf("lock at 100 failed (%s)\n", cli_errstr(cli));
1438         }
1439         cli_setpid(cli, 2);
1440         if (cli_unlock(cli, fnum1, 100, 4)) {
1441                 printf("unlock at 100 succeeded! This is a locking bug\n");
1442                 correct = False;
1443         }
1444
1445         if (cli_unlock(cli, fnum1, 0, 4)) {
1446                 printf("unlock1 succeeded! This is a locking bug\n");
1447                 correct = False;
1448         } else {
1449                 if (!check_error(__LINE__, cli, 
1450                                  ERRDOS, ERRlock, 
1451                                  NT_STATUS_RANGE_NOT_LOCKED)) return False;
1452         }
1453
1454         if (cli_unlock(cli, fnum1, 0, 8)) {
1455                 printf("unlock2 succeeded! This is a locking bug\n");
1456                 correct = False;
1457         } else {
1458                 if (!check_error(__LINE__, cli, 
1459                                  ERRDOS, ERRlock, 
1460                                  NT_STATUS_RANGE_NOT_LOCKED)) return False;
1461         }
1462
1463         if (cli_lock(cli, fnum3, 0, 4, 0, WRITE_LOCK)) {
1464                 printf("lock3 succeeded! This is a locking bug\n");
1465                 correct = False;
1466         } else {
1467                 if (!check_error(__LINE__, cli, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
1468         }
1469
1470         cli_setpid(cli, 1);
1471
1472         if (!cli_close(cli, fnum1)) {
1473                 printf("close1 failed (%s)\n", cli_errstr(cli));
1474                 return False;
1475         }
1476
1477         if (!cli_close(cli, fnum2)) {
1478                 printf("close2 failed (%s)\n", cli_errstr(cli));
1479                 return False;
1480         }
1481
1482         if (!cli_close(cli, fnum3)) {
1483                 printf("close3 failed (%s)\n", cli_errstr(cli));
1484                 return False;
1485         }
1486
1487         if (!torture_close_connection(cli)) {
1488                 correct = False;
1489         }
1490
1491         printf("locktest2 finished\n");
1492
1493         return correct;
1494 }
1495
1496
1497 /*
1498   This test checks that 
1499
1500   1) the server supports the full offset range in lock requests
1501 */
1502 static bool run_locktest3(int dummy)
1503 {
1504         static struct cli_state *cli1, *cli2;
1505         const char *fname = "\\lockt3.lck";
1506         int fnum1, fnum2, i;
1507         uint32 offset;
1508         bool correct = True;
1509
1510 #define NEXT_OFFSET offset += (~(uint32)0) / torture_numops
1511
1512         if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1513                 return False;
1514         }
1515         cli_sockopt(cli1, sockops);
1516         cli_sockopt(cli2, sockops);
1517
1518         printf("starting locktest3\n");
1519
1520         cli_unlink(cli1, fname);
1521
1522         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1523         if (fnum1 == -1) {
1524                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
1525                 return False;
1526         }
1527         fnum2 = cli_open(cli2, fname, O_RDWR, DENY_NONE);
1528         if (fnum2 == -1) {
1529                 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli2));
1530                 return False;
1531         }
1532
1533         for (offset=i=0;i<torture_numops;i++) {
1534                 NEXT_OFFSET;
1535                 if (!cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
1536                         printf("lock1 %d failed (%s)\n", 
1537                                i,
1538                                cli_errstr(cli1));
1539                         return False;
1540                 }
1541
1542                 if (!cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
1543                         printf("lock2 %d failed (%s)\n", 
1544                                i,
1545                                cli_errstr(cli1));
1546                         return False;
1547                 }
1548         }
1549
1550         for (offset=i=0;i<torture_numops;i++) {
1551                 NEXT_OFFSET;
1552
1553                 if (cli_lock(cli1, fnum1, offset-2, 1, 0, WRITE_LOCK)) {
1554                         printf("error: lock1 %d succeeded!\n", i);
1555                         return False;
1556                 }
1557
1558                 if (cli_lock(cli2, fnum2, offset-1, 1, 0, WRITE_LOCK)) {
1559                         printf("error: lock2 %d succeeded!\n", i);
1560                         return False;
1561                 }
1562
1563                 if (cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
1564                         printf("error: lock3 %d succeeded!\n", i);
1565                         return False;
1566                 }
1567
1568                 if (cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
1569                         printf("error: lock4 %d succeeded!\n", i);
1570                         return False;
1571                 }
1572         }
1573
1574         for (offset=i=0;i<torture_numops;i++) {
1575                 NEXT_OFFSET;
1576
1577                 if (!cli_unlock(cli1, fnum1, offset-1, 1)) {
1578                         printf("unlock1 %d failed (%s)\n", 
1579                                i,
1580                                cli_errstr(cli1));
1581                         return False;
1582                 }
1583
1584                 if (!cli_unlock(cli2, fnum2, offset-2, 1)) {
1585                         printf("unlock2 %d failed (%s)\n", 
1586                                i,
1587                                cli_errstr(cli1));
1588                         return False;
1589                 }
1590         }
1591
1592         if (!cli_close(cli1, fnum1)) {
1593                 printf("close1 failed (%s)\n", cli_errstr(cli1));
1594                 return False;
1595         }
1596
1597         if (!cli_close(cli2, fnum2)) {
1598                 printf("close2 failed (%s)\n", cli_errstr(cli2));
1599                 return False;
1600         }
1601
1602         if (!cli_unlink(cli1, fname)) {
1603                 printf("unlink failed (%s)\n", cli_errstr(cli1));
1604                 return False;
1605         }
1606
1607         if (!torture_close_connection(cli1)) {
1608                 correct = False;
1609         }
1610
1611         if (!torture_close_connection(cli2)) {
1612                 correct = False;
1613         }
1614
1615         printf("finished locktest3\n");
1616
1617         return correct;
1618 }
1619
1620 #define EXPECTED(ret, v) if ((ret) != (v)) { \
1621         printf("** "); correct = False; \
1622         }
1623
1624 /*
1625   looks at overlapping locks
1626 */
1627 static bool run_locktest4(int dummy)
1628 {
1629         static struct cli_state *cli1, *cli2;
1630         const char *fname = "\\lockt4.lck";
1631         int fnum1, fnum2, f;
1632         bool ret;
1633         char buf[1000];
1634         bool correct = True;
1635
1636         if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1637                 return False;
1638         }
1639
1640         cli_sockopt(cli1, sockops);
1641         cli_sockopt(cli2, sockops);
1642
1643         printf("starting locktest4\n");
1644
1645         cli_unlink(cli1, fname);
1646
1647         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1648         fnum2 = cli_open(cli2, fname, O_RDWR, DENY_NONE);
1649
1650         memset(buf, 0, sizeof(buf));
1651
1652         if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1653                 printf("Failed to create file\n");
1654                 correct = False;
1655                 goto fail;
1656         }
1657
1658         ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
1659               cli_lock(cli1, fnum1, 2, 4, 0, WRITE_LOCK);
1660         EXPECTED(ret, False);
1661         printf("the same process %s set overlapping write locks\n", ret?"can":"cannot");
1662
1663         ret = cli_lock(cli1, fnum1, 10, 4, 0, READ_LOCK) &&
1664               cli_lock(cli1, fnum1, 12, 4, 0, READ_LOCK);
1665         EXPECTED(ret, True);
1666         printf("the same process %s set overlapping read locks\n", ret?"can":"cannot");
1667
1668         ret = cli_lock(cli1, fnum1, 20, 4, 0, WRITE_LOCK) &&
1669               cli_lock(cli2, fnum2, 22, 4, 0, WRITE_LOCK);
1670         EXPECTED(ret, False);
1671         printf("a different connection %s set overlapping write locks\n", ret?"can":"cannot");
1672
1673         ret = cli_lock(cli1, fnum1, 30, 4, 0, READ_LOCK) &&
1674               cli_lock(cli2, fnum2, 32, 4, 0, READ_LOCK);
1675         EXPECTED(ret, True);
1676         printf("a different connection %s set overlapping read locks\n", ret?"can":"cannot");
1677
1678         ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 40, 4, 0, WRITE_LOCK)) &&
1679               (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 42, 4, 0, WRITE_LOCK));
1680         EXPECTED(ret, False);
1681         printf("a different pid %s set overlapping write locks\n", ret?"can":"cannot");
1682
1683         ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 50, 4, 0, READ_LOCK)) &&
1684               (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 52, 4, 0, READ_LOCK));
1685         EXPECTED(ret, True);
1686         printf("a different pid %s set overlapping read locks\n", ret?"can":"cannot");
1687
1688         ret = cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK) &&
1689               cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK);
1690         EXPECTED(ret, True);
1691         printf("the same process %s set the same read lock twice\n", ret?"can":"cannot");
1692
1693         ret = cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK) &&
1694               cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK);
1695         EXPECTED(ret, False);
1696         printf("the same process %s set the same write lock twice\n", ret?"can":"cannot");
1697
1698         ret = cli_lock(cli1, fnum1, 80, 4, 0, READ_LOCK) &&
1699               cli_lock(cli1, fnum1, 80, 4, 0, WRITE_LOCK);
1700         EXPECTED(ret, False);
1701         printf("the same process %s overlay a read lock with a write lock\n", ret?"can":"cannot");
1702
1703         ret = cli_lock(cli1, fnum1, 90, 4, 0, WRITE_LOCK) &&
1704               cli_lock(cli1, fnum1, 90, 4, 0, READ_LOCK);
1705         EXPECTED(ret, True);
1706         printf("the same process %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1707
1708         ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 100, 4, 0, WRITE_LOCK)) &&
1709               (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 100, 4, 0, READ_LOCK));
1710         EXPECTED(ret, False);
1711         printf("a different pid %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1712
1713         ret = cli_lock(cli1, fnum1, 110, 4, 0, READ_LOCK) &&
1714               cli_lock(cli1, fnum1, 112, 4, 0, READ_LOCK) &&
1715               cli_unlock(cli1, fnum1, 110, 6);
1716         EXPECTED(ret, False);
1717         printf("the same process %s coalesce read locks\n", ret?"can":"cannot");
1718
1719
1720         ret = cli_lock(cli1, fnum1, 120, 4, 0, WRITE_LOCK) &&
1721               (cli_read(cli2, fnum2, buf, 120, 4) == 4);
1722         EXPECTED(ret, False);
1723         printf("this server %s strict write locking\n", ret?"doesn't do":"does");
1724
1725         ret = cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK) &&
1726               (cli_write(cli2, fnum2, 0, buf, 130, 4) == 4);
1727         EXPECTED(ret, False);
1728         printf("this server %s strict read locking\n", ret?"doesn't do":"does");
1729
1730
1731         ret = cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
1732               cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
1733               cli_unlock(cli1, fnum1, 140, 4) &&
1734               cli_unlock(cli1, fnum1, 140, 4);
1735         EXPECTED(ret, True);
1736         printf("this server %s do recursive read locking\n", ret?"does":"doesn't");
1737
1738
1739         ret = cli_lock(cli1, fnum1, 150, 4, 0, WRITE_LOCK) &&
1740               cli_lock(cli1, fnum1, 150, 4, 0, READ_LOCK) &&
1741               cli_unlock(cli1, fnum1, 150, 4) &&
1742               (cli_read(cli2, fnum2, buf, 150, 4) == 4) &&
1743               !(cli_write(cli2, fnum2, 0, buf, 150, 4) == 4) &&
1744               cli_unlock(cli1, fnum1, 150, 4);
1745         EXPECTED(ret, True);
1746         printf("this server %s do recursive lock overlays\n", ret?"does":"doesn't");
1747
1748         ret = cli_lock(cli1, fnum1, 160, 4, 0, READ_LOCK) &&
1749               cli_unlock(cli1, fnum1, 160, 4) &&
1750               (cli_write(cli2, fnum2, 0, buf, 160, 4) == 4) &&          
1751               (cli_read(cli2, fnum2, buf, 160, 4) == 4);                
1752         EXPECTED(ret, True);
1753         printf("the same process %s remove a read lock using write locking\n", ret?"can":"cannot");
1754
1755         ret = cli_lock(cli1, fnum1, 170, 4, 0, WRITE_LOCK) &&
1756               cli_unlock(cli1, fnum1, 170, 4) &&
1757               (cli_write(cli2, fnum2, 0, buf, 170, 4) == 4) &&          
1758               (cli_read(cli2, fnum2, buf, 170, 4) == 4);                
1759         EXPECTED(ret, True);
1760         printf("the same process %s remove a write lock using read locking\n", ret?"can":"cannot");
1761
1762         ret = cli_lock(cli1, fnum1, 190, 4, 0, WRITE_LOCK) &&
1763               cli_lock(cli1, fnum1, 190, 4, 0, READ_LOCK) &&
1764               cli_unlock(cli1, fnum1, 190, 4) &&
1765               !(cli_write(cli2, fnum2, 0, buf, 190, 4) == 4) &&         
1766               (cli_read(cli2, fnum2, buf, 190, 4) == 4);                
1767         EXPECTED(ret, True);
1768         printf("the same process %s remove the first lock first\n", ret?"does":"doesn't");
1769
1770         cli_close(cli1, fnum1);
1771         cli_close(cli2, fnum2);
1772         fnum1 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1773         f = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1774         ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
1775               cli_lock(cli1, f, 0, 1, 0, READ_LOCK) &&
1776               cli_close(cli1, fnum1) &&
1777               ((fnum1 = cli_open(cli1, fname, O_RDWR, DENY_NONE)) != -1) &&
1778               cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
1779         cli_close(cli1, f);
1780         cli_close(cli1, fnum1);
1781         EXPECTED(ret, True);
1782         printf("the server %s have the NT byte range lock bug\n", !ret?"does":"doesn't");
1783
1784  fail:
1785         cli_close(cli1, fnum1);
1786         cli_close(cli2, fnum2);
1787         cli_unlink(cli1, fname);
1788         torture_close_connection(cli1);
1789         torture_close_connection(cli2);
1790
1791         printf("finished locktest4\n");
1792         return correct;
1793 }
1794
1795 /*
1796   looks at lock upgrade/downgrade.
1797 */
1798 static bool run_locktest5(int dummy)
1799 {
1800         static struct cli_state *cli1, *cli2;
1801         const char *fname = "\\lockt5.lck";
1802         int fnum1, fnum2, fnum3;
1803         bool ret;
1804         char buf[1000];
1805         bool correct = True;
1806
1807         if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1808                 return False;
1809         }
1810
1811         cli_sockopt(cli1, sockops);
1812         cli_sockopt(cli2, sockops);
1813
1814         printf("starting locktest5\n");
1815
1816         cli_unlink(cli1, fname);
1817
1818         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1819         fnum2 = cli_open(cli2, fname, O_RDWR, DENY_NONE);
1820         fnum3 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1821
1822         memset(buf, 0, sizeof(buf));
1823
1824         if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1825                 printf("Failed to create file\n");
1826                 correct = False;
1827                 goto fail;
1828         }
1829
1830         /* Check for NT bug... */
1831         ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
1832                   cli_lock(cli1, fnum3, 0, 1, 0, READ_LOCK);
1833         cli_close(cli1, fnum1);
1834         fnum1 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1835         ret = cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
1836         EXPECTED(ret, True);
1837         printf("this server %s the NT locking bug\n", ret ? "doesn't have" : "has");
1838         cli_close(cli1, fnum1);
1839         fnum1 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1840         cli_unlock(cli1, fnum3, 0, 1);
1841
1842         ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
1843               cli_lock(cli1, fnum1, 1, 1, 0, READ_LOCK);
1844         EXPECTED(ret, True);
1845         printf("the same process %s overlay a write with a read lock\n", ret?"can":"cannot");
1846
1847         ret = cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
1848         EXPECTED(ret, False);
1849
1850         printf("a different processs %s get a read lock on the first process lock stack\n", ret?"can":"cannot");
1851
1852         /* Unlock the process 2 lock. */
1853         cli_unlock(cli2, fnum2, 0, 4);
1854
1855         ret = cli_lock(cli1, fnum3, 0, 4, 0, READ_LOCK);
1856         EXPECTED(ret, False);
1857
1858         printf("the same processs on a different fnum %s get a read lock\n", ret?"can":"cannot");
1859
1860         /* Unlock the process 1 fnum3 lock. */
1861         cli_unlock(cli1, fnum3, 0, 4);
1862
1863         /* Stack 2 more locks here. */
1864         ret = cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK) &&
1865                   cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK);
1866
1867         EXPECTED(ret, True);
1868         printf("the same process %s stack read locks\n", ret?"can":"cannot");
1869
1870         /* Unlock the first process lock, then check this was the WRITE lock that was
1871                 removed. */
1872
1873         ret = cli_unlock(cli1, fnum1, 0, 4) &&
1874                         cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
1875
1876         EXPECTED(ret, True);
1877         printf("the first unlock removes the %s lock\n", ret?"WRITE":"READ");
1878
1879         /* Unlock the process 2 lock. */
1880         cli_unlock(cli2, fnum2, 0, 4);
1881
1882         /* We should have 3 stacked locks here. Ensure we need to do 3 unlocks. */
1883
1884         ret = cli_unlock(cli1, fnum1, 1, 1) &&
1885                   cli_unlock(cli1, fnum1, 0, 4) &&
1886                   cli_unlock(cli1, fnum1, 0, 4);
1887
1888         EXPECTED(ret, True);
1889         printf("the same process %s unlock the stack of 4 locks\n", ret?"can":"cannot"); 
1890
1891         /* Ensure the next unlock fails. */
1892         ret = cli_unlock(cli1, fnum1, 0, 4);
1893         EXPECTED(ret, False);
1894         printf("the same process %s count the lock stack\n", !ret?"can":"cannot"); 
1895
1896         /* Ensure connection 2 can get a write lock. */
1897         ret = cli_lock(cli2, fnum2, 0, 4, 0, WRITE_LOCK);
1898         EXPECTED(ret, True);
1899
1900         printf("a different processs %s get a write lock on the unlocked stack\n", ret?"can":"cannot");
1901
1902
1903  fail:
1904         cli_close(cli1, fnum1);
1905         cli_close(cli2, fnum2);
1906         cli_unlink(cli1, fname);
1907         if (!torture_close_connection(cli1)) {
1908                 correct = False;
1909         }
1910         if (!torture_close_connection(cli2)) {
1911                 correct = False;
1912         }
1913
1914         printf("finished locktest5\n");
1915
1916         return correct;
1917 }
1918
1919 /*
1920   tries the unusual lockingX locktype bits
1921 */
1922 static bool run_locktest6(int dummy)
1923 {
1924         static struct cli_state *cli;
1925         const char *fname[1] = { "\\lock6.txt" };
1926         int i;
1927         int fnum;
1928         NTSTATUS status;
1929
1930         if (!torture_open_connection(&cli, 0)) {
1931                 return False;
1932         }
1933
1934         cli_sockopt(cli, sockops);
1935
1936         printf("starting locktest6\n");
1937
1938         for (i=0;i<1;i++) {
1939                 printf("Testing %s\n", fname[i]);
1940
1941                 cli_unlink(cli, fname[i]);
1942
1943                 fnum = cli_open(cli, fname[i], O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1944                 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CHANGE_LOCKTYPE);
1945                 cli_close(cli, fnum);
1946                 printf("CHANGE_LOCKTYPE gave %s\n", nt_errstr(status));
1947
1948                 fnum = cli_open(cli, fname[i], O_RDWR, DENY_NONE);
1949                 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CANCEL_LOCK);
1950                 cli_close(cli, fnum);
1951                 printf("CANCEL_LOCK gave %s\n", nt_errstr(status));
1952
1953                 cli_unlink(cli, fname[i]);
1954         }
1955
1956         torture_close_connection(cli);
1957
1958         printf("finished locktest6\n");
1959         return True;
1960 }
1961
1962 static bool run_locktest7(int dummy)
1963 {
1964         struct cli_state *cli1;
1965         const char *fname = "\\lockt7.lck";
1966         int fnum1;
1967         char buf[200];
1968         bool correct = False;
1969
1970         if (!torture_open_connection(&cli1, 0)) {
1971                 return False;
1972         }
1973
1974         cli_sockopt(cli1, sockops);
1975
1976         printf("starting locktest7\n");
1977
1978         cli_unlink(cli1, fname);
1979
1980         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1981
1982         memset(buf, 0, sizeof(buf));
1983
1984         if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1985                 printf("Failed to create file\n");
1986                 goto fail;
1987         }
1988
1989         cli_setpid(cli1, 1);
1990
1991         if (!cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK)) {
1992                 printf("Unable to apply read lock on range 130:4, error was %s\n", cli_errstr(cli1));
1993                 goto fail;
1994         } else {
1995                 printf("pid1 successfully locked range 130:4 for READ\n");
1996         }
1997
1998         if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
1999                 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2000                 goto fail;
2001         } else {
2002                 printf("pid1 successfully read the range 130:4\n");
2003         }
2004
2005         if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2006                 printf("pid1 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2007                 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2008                         printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2009                         goto fail;
2010                 }
2011         } else {
2012                 printf("pid1 successfully wrote to the range 130:4 (should be denied)\n");
2013                 goto fail;
2014         }
2015
2016         cli_setpid(cli1, 2);
2017
2018         if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2019                 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2020         } else {
2021                 printf("pid2 successfully read the range 130:4\n");
2022         }
2023
2024         if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2025                 printf("pid2 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2026                 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2027                         printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2028                         goto fail;
2029                 }
2030         } else {
2031                 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
2032                 goto fail;
2033         }
2034
2035         cli_setpid(cli1, 1);
2036         cli_unlock(cli1, fnum1, 130, 4);
2037
2038         if (!cli_lock(cli1, fnum1, 130, 4, 0, WRITE_LOCK)) {
2039                 printf("Unable to apply write lock on range 130:4, error was %s\n", cli_errstr(cli1));
2040                 goto fail;
2041         } else {
2042                 printf("pid1 successfully locked range 130:4 for WRITE\n");
2043         }
2044
2045         if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2046                 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2047                 goto fail;
2048         } else {
2049                 printf("pid1 successfully read the range 130:4\n");
2050         }
2051
2052         if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2053                 printf("pid1 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2054                 goto fail;
2055         } else {
2056                 printf("pid1 successfully wrote to the range 130:4\n");
2057         }
2058
2059         cli_setpid(cli1, 2);
2060
2061         if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2062                 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2063                 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2064                         printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2065                         goto fail;
2066                 }
2067         } else {
2068                 printf("pid2 successfully read the range 130:4 (should be denied)\n");
2069                 goto fail;
2070         }
2071
2072         if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2073                 printf("pid2 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2074                 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2075                         printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2076                         goto fail;
2077                 }
2078         } else {
2079                 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
2080                 goto fail;
2081         }
2082
2083         cli_unlock(cli1, fnum1, 130, 0);
2084         correct = True;
2085
2086 fail:
2087         cli_close(cli1, fnum1);
2088         cli_unlink(cli1, fname);
2089         torture_close_connection(cli1);
2090
2091         printf("finished locktest7\n");
2092         return correct;
2093 }
2094
2095 /*
2096 test whether fnums and tids open on one VC are available on another (a major
2097 security hole)
2098 */
2099 static bool run_fdpasstest(int dummy)
2100 {
2101         struct cli_state *cli1, *cli2;
2102         const char *fname = "\\fdpass.tst";
2103         int fnum1;
2104         char buf[1024];
2105
2106         if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
2107                 return False;
2108         }
2109         cli_sockopt(cli1, sockops);
2110         cli_sockopt(cli2, sockops);
2111
2112         printf("starting fdpasstest\n");
2113
2114         cli_unlink(cli1, fname);
2115
2116         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2117         if (fnum1 == -1) {
2118                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
2119                 return False;
2120         }
2121
2122         if (cli_write(cli1, fnum1, 0, "hello world\n", 0, 13) != 13) {
2123                 printf("write failed (%s)\n", cli_errstr(cli1));
2124                 return False;
2125         }
2126
2127         cli2->vuid = cli1->vuid;
2128         cli2->cnum = cli1->cnum;
2129         cli2->pid = cli1->pid;
2130
2131         if (cli_read(cli2, fnum1, buf, 0, 13) == 13) {
2132                 printf("read succeeded! nasty security hole [%s]\n",
2133                        buf);
2134                 return False;
2135         }
2136
2137         cli_close(cli1, fnum1);
2138         cli_unlink(cli1, fname);
2139
2140         torture_close_connection(cli1);
2141         torture_close_connection(cli2);
2142
2143         printf("finished fdpasstest\n");
2144         return True;
2145 }
2146
2147 static bool run_fdsesstest(int dummy)
2148 {
2149         struct cli_state *cli;
2150         uint16 new_vuid;
2151         uint16 saved_vuid;
2152         uint16 new_cnum;
2153         uint16 saved_cnum;
2154         const char *fname = "\\fdsess.tst";
2155         const char *fname1 = "\\fdsess1.tst";
2156         int fnum1;
2157         int fnum2;
2158         char buf[1024];
2159         bool ret = True;
2160
2161         if (!torture_open_connection(&cli, 0))
2162                 return False;
2163         cli_sockopt(cli, sockops);
2164
2165         if (!torture_cli_session_setup2(cli, &new_vuid))
2166                 return False;
2167
2168         saved_cnum = cli->cnum;
2169         if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, share, "?????", "", 1)))
2170                 return False;
2171         new_cnum = cli->cnum;
2172         cli->cnum = saved_cnum;
2173
2174         printf("starting fdsesstest\n");
2175
2176         cli_unlink(cli, fname);
2177         cli_unlink(cli, fname1);
2178
2179         fnum1 = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2180         if (fnum1 == -1) {
2181                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2182                 return False;
2183         }
2184
2185         if (cli_write(cli, fnum1, 0, "hello world\n", 0, 13) != 13) {
2186                 printf("write failed (%s)\n", cli_errstr(cli));
2187                 return False;
2188         }
2189
2190         saved_vuid = cli->vuid;
2191         cli->vuid = new_vuid;
2192
2193         if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2194                 printf("read succeeded with different vuid! nasty security hole [%s]\n",
2195                        buf);
2196                 ret = False;
2197         }
2198         /* Try to open a file with different vuid, samba cnum. */
2199         fnum2 = cli_open(cli, fname1, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2200         if (fnum2 != -1) {
2201                 printf("create with different vuid, same cnum succeeded.\n");
2202                 cli_close(cli, fnum2);
2203                 cli_unlink(cli, fname1);
2204         } else {
2205                 printf("create with different vuid, same cnum failed.\n");
2206                 printf("This will cause problems with service clients.\n");
2207                 ret = False;
2208         }
2209
2210         cli->vuid = saved_vuid;
2211
2212         /* Try with same vuid, different cnum. */
2213         cli->cnum = new_cnum;
2214
2215         if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2216                 printf("read succeeded with different cnum![%s]\n",
2217                        buf);
2218                 ret = False;
2219         }
2220
2221         cli->cnum = saved_cnum;
2222         cli_close(cli, fnum1);
2223         cli_unlink(cli, fname);
2224
2225         torture_close_connection(cli);
2226
2227         printf("finished fdsesstest\n");
2228         return ret;
2229 }
2230
2231 /*
2232   This test checks that 
2233
2234   1) the server does not allow an unlink on a file that is open
2235 */
2236 static bool run_unlinktest(int dummy)
2237 {
2238         struct cli_state *cli;
2239         const char *fname = "\\unlink.tst";
2240         int fnum;
2241         bool correct = True;
2242
2243         if (!torture_open_connection(&cli, 0)) {
2244                 return False;
2245         }
2246
2247         cli_sockopt(cli, sockops);
2248
2249         printf("starting unlink test\n");
2250
2251         cli_unlink(cli, fname);
2252
2253         cli_setpid(cli, 1);
2254
2255         fnum = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2256         if (fnum == -1) {
2257                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2258                 return False;
2259         }
2260
2261         if (cli_unlink(cli, fname)) {
2262                 printf("error: server allowed unlink on an open file\n");
2263                 correct = False;
2264         } else {
2265                 correct = check_error(__LINE__, cli, ERRDOS, ERRbadshare, 
2266                                       NT_STATUS_SHARING_VIOLATION);
2267         }
2268
2269         cli_close(cli, fnum);
2270         cli_unlink(cli, fname);
2271
2272         if (!torture_close_connection(cli)) {
2273                 correct = False;
2274         }
2275
2276         printf("unlink test finished\n");
2277
2278         return correct;
2279 }
2280
2281
2282 /*
2283 test how many open files this server supports on the one socket
2284 */
2285 static bool run_maxfidtest(int dummy)
2286 {
2287         struct cli_state *cli;
2288         const char *ftemplate = "\\maxfid.%d.%d";
2289         fstring fname;
2290         int fnums[0x11000], i;
2291         int retries=4;
2292         bool correct = True;
2293
2294         cli = current_cli;
2295
2296         if (retries <= 0) {
2297                 printf("failed to connect\n");
2298                 return False;
2299         }
2300
2301         cli_sockopt(cli, sockops);
2302
2303         for (i=0; i<0x11000; i++) {
2304                 slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
2305                 if ((fnums[i] = cli_open(cli, fname, 
2306                                         O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) ==
2307                     -1) {
2308                         printf("open of %s failed (%s)\n", 
2309                                fname, cli_errstr(cli));
2310                         printf("maximum fnum is %d\n", i);
2311                         break;
2312                 }
2313                 printf("%6d\r", i);
2314         }
2315         printf("%6d\n", i);
2316         i--;
2317
2318         printf("cleaning up\n");
2319         for (;i>=0;i--) {
2320                 slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
2321                 cli_close(cli, fnums[i]);
2322                 if (!cli_unlink(cli, fname)) {
2323                         printf("unlink of %s failed (%s)\n", 
2324                                fname, cli_errstr(cli));
2325                         correct = False;
2326                 }
2327                 printf("%6d\r", i);
2328         }
2329         printf("%6d\n", 0);
2330
2331         printf("maxfid test finished\n");
2332         if (!torture_close_connection(cli)) {
2333                 correct = False;
2334         }
2335         return correct;
2336 }
2337
2338 /* generate a random buffer */
2339 static void rand_buf(char *buf, int len)
2340 {
2341         while (len--) {
2342                 *buf = (char)sys_random();
2343                 buf++;
2344         }
2345 }
2346
2347 /* send smb negprot commands, not reading the response */
2348 static bool run_negprot_nowait(int dummy)
2349 {
2350         int i;
2351         static struct cli_state *cli;
2352         bool correct = True;
2353
2354         printf("starting negprot nowait test\n");
2355
2356         if (!(cli = open_nbt_connection())) {
2357                 return False;
2358         }
2359
2360         for (i=0;i<50000;i++) {
2361                 cli_negprot_sendsync(cli);
2362         }
2363
2364         if (!torture_close_connection(cli)) {
2365                 correct = False;
2366         }
2367
2368         printf("finished negprot nowait test\n");
2369
2370         return correct;
2371 }
2372
2373
2374 /* send random IPC commands */
2375 static bool run_randomipc(int dummy)
2376 {
2377         char *rparam = NULL;
2378         char *rdata = NULL;
2379         unsigned int rdrcnt,rprcnt;
2380         char param[1024];
2381         int api, param_len, i;
2382         struct cli_state *cli;
2383         bool correct = True;
2384         int count = 50000;
2385
2386         printf("starting random ipc test\n");
2387
2388         if (!torture_open_connection(&cli, 0)) {
2389                 return False;
2390         }
2391
2392         for (i=0;i<count;i++) {
2393                 api = sys_random() % 500;
2394                 param_len = (sys_random() % 64);
2395
2396                 rand_buf(param, param_len);
2397
2398                 SSVAL(param,0,api); 
2399
2400                 cli_api(cli, 
2401                         param, param_len, 8,  
2402                         NULL, 0, BUFFER_SIZE, 
2403                         &rparam, &rprcnt,     
2404                         &rdata, &rdrcnt);
2405                 if (i % 100 == 0) {
2406                         printf("%d/%d\r", i,count);
2407                 }
2408         }
2409         printf("%d/%d\n", i, count);
2410
2411         if (!torture_close_connection(cli)) {
2412                 correct = False;
2413         }
2414
2415         printf("finished random ipc test\n");
2416
2417         return correct;
2418 }
2419
2420
2421
2422 static void browse_callback(const char *sname, uint32 stype, 
2423                             const char *comment, void *state)
2424 {
2425         printf("\t%20.20s %08x %s\n", sname, stype, comment);
2426 }
2427
2428
2429
2430 /*
2431   This test checks the browse list code
2432
2433 */
2434 static bool run_browsetest(int dummy)
2435 {
2436         static struct cli_state *cli;
2437         bool correct = True;
2438
2439         printf("starting browse test\n");
2440
2441         if (!torture_open_connection(&cli, 0)) {
2442                 return False;
2443         }
2444
2445         printf("domain list:\n");
2446         cli_NetServerEnum(cli, cli->server_domain, 
2447                           SV_TYPE_DOMAIN_ENUM,
2448                           browse_callback, NULL);
2449
2450         printf("machine list:\n");
2451         cli_NetServerEnum(cli, cli->server_domain, 
2452                           SV_TYPE_ALL,
2453                           browse_callback, NULL);
2454
2455         if (!torture_close_connection(cli)) {
2456                 correct = False;
2457         }
2458
2459         printf("browse test finished\n");
2460
2461         return correct;
2462
2463 }
2464
2465
2466 /*
2467   This checks how the getatr calls works
2468 */
2469 static bool run_attrtest(int dummy)
2470 {
2471         struct cli_state *cli;
2472         int fnum;
2473         time_t t, t2;
2474         const char *fname = "\\attrib123456789.tst";
2475         bool correct = True;
2476
2477         printf("starting attrib test\n");
2478
2479         if (!torture_open_connection(&cli, 0)) {
2480                 return False;
2481         }
2482
2483         cli_unlink(cli, fname);
2484         fnum = cli_open(cli, fname, 
2485                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2486         cli_close(cli, fnum);
2487         if (!cli_getatr(cli, fname, NULL, NULL, &t)) {
2488                 printf("getatr failed (%s)\n", cli_errstr(cli));
2489                 correct = False;
2490         }
2491
2492         if (abs(t - time(NULL)) > 60*60*24*10) {
2493                 printf("ERROR: SMBgetatr bug. time is %s",
2494                        ctime(&t));
2495                 t = time(NULL);
2496                 correct = True;
2497         }
2498
2499         t2 = t-60*60*24; /* 1 day ago */
2500
2501         if (!cli_setatr(cli, fname, 0, t2)) {
2502                 printf("setatr failed (%s)\n", cli_errstr(cli));
2503                 correct = True;
2504         }
2505
2506         if (!cli_getatr(cli, fname, NULL, NULL, &t)) {
2507                 printf("getatr failed (%s)\n", cli_errstr(cli));
2508                 correct = True;
2509         }
2510
2511         if (t != t2) {
2512                 printf("ERROR: getatr/setatr bug. times are\n%s",
2513                        ctime(&t));
2514                 printf("%s", ctime(&t2));
2515                 correct = True;
2516         }
2517
2518         cli_unlink(cli, fname);
2519
2520         if (!torture_close_connection(cli)) {
2521                 correct = False;
2522         }
2523
2524         printf("attrib test finished\n");
2525
2526         return correct;
2527 }
2528
2529
2530 /*
2531   This checks a couple of trans2 calls
2532 */
2533 static bool run_trans2test(int dummy)
2534 {
2535         struct cli_state *cli;
2536         int fnum;
2537         SMB_OFF_T size;
2538         time_t c_time, a_time, m_time;
2539         struct timespec c_time_ts, a_time_ts, m_time_ts, w_time_ts, m_time2_ts;
2540         const char *fname = "\\trans2.tst";
2541         const char *dname = "\\trans2";
2542         const char *fname2 = "\\trans2\\trans2.tst";
2543         char pname[1024];
2544         bool correct = True;
2545
2546         printf("starting trans2 test\n");
2547
2548         if (!torture_open_connection(&cli, 0)) {
2549                 return False;
2550         }
2551
2552         cli_unlink(cli, fname);
2553         fnum = cli_open(cli, fname, 
2554                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2555         if (!cli_qfileinfo(cli, fnum, NULL, &size, &c_time_ts, &a_time_ts, &w_time_ts,
2556                            &m_time_ts, NULL)) {
2557                 printf("ERROR: qfileinfo failed (%s)\n", cli_errstr(cli));
2558                 correct = False;
2559         }
2560
2561         if (!cli_qfilename(cli, fnum, pname, sizeof(pname))) {
2562                 printf("ERROR: qfilename failed (%s)\n", cli_errstr(cli));
2563                 correct = False;
2564         }
2565
2566         if (strcmp(pname, fname)) {
2567                 printf("qfilename gave different name? [%s] [%s]\n",
2568                        fname, pname);
2569                 correct = False;
2570         }
2571
2572         cli_close(cli, fnum);
2573
2574         sleep(2);
2575
2576         cli_unlink(cli, fname);
2577         fnum = cli_open(cli, fname, 
2578                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2579         if (fnum == -1) {
2580                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2581                 return False;
2582         }
2583         cli_close(cli, fnum);
2584
2585         if (!cli_qpathinfo(cli, fname, &c_time, &a_time, &m_time, &size, NULL)) {
2586                 printf("ERROR: qpathinfo failed (%s)\n", cli_errstr(cli));
2587                 correct = False;
2588         } else {
2589                 if (c_time != m_time) {
2590                         printf("create time=%s", ctime(&c_time));
2591                         printf("modify time=%s", ctime(&m_time));
2592                         printf("This system appears to have sticky create times\n");
2593                 }
2594                 if (a_time % (60*60) == 0) {
2595                         printf("access time=%s", ctime(&a_time));
2596                         printf("This system appears to set a midnight access time\n");
2597                         correct = False;
2598                 }
2599
2600                 if (abs(m_time - time(NULL)) > 60*60*24*7) {
2601                         printf("ERROR: totally incorrect times - maybe word reversed? mtime=%s", ctime(&m_time));
2602                         correct = False;
2603                 }
2604         }
2605
2606
2607         cli_unlink(cli, fname);
2608         fnum = cli_open(cli, fname, 
2609                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2610         cli_close(cli, fnum);
2611         if (!cli_qpathinfo2(cli, fname, &c_time_ts, &a_time_ts, &w_time_ts, 
2612                             &m_time_ts, &size, NULL, NULL)) {
2613                 printf("ERROR: qpathinfo2 failed (%s)\n", cli_errstr(cli));
2614                 correct = False;
2615         } else {
2616                 if (w_time_ts.tv_sec < 60*60*24*2) {
2617                         printf("write time=%s", ctime(&w_time_ts.tv_sec));
2618                         printf("This system appears to set a initial 0 write time\n");
2619                         correct = False;
2620                 }
2621         }
2622
2623         cli_unlink(cli, fname);
2624
2625
2626         /* check if the server updates the directory modification time
2627            when creating a new file */
2628         if (!NT_STATUS_IS_OK(cli_mkdir(cli, dname))) {
2629                 printf("ERROR: mkdir failed (%s)\n", cli_errstr(cli));
2630                 correct = False;
2631         }
2632         sleep(3);
2633         if (!cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts, &w_time_ts, 
2634                             &m_time_ts, &size, NULL, NULL)) {
2635                 printf("ERROR: qpathinfo2 failed (%s)\n", cli_errstr(cli));
2636                 correct = False;
2637         }
2638
2639         fnum = cli_open(cli, fname2, 
2640                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2641         cli_write(cli, fnum,  0, (char *)&fnum, 0, sizeof(fnum));
2642         cli_close(cli, fnum);
2643         if (!cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts, &w_time_ts, 
2644                             &m_time2_ts, &size, NULL, NULL)) {
2645                 printf("ERROR: qpathinfo2 failed (%s)\n", cli_errstr(cli));
2646                 correct = False;
2647         } else {
2648                 if (memcmp(&m_time_ts, &m_time2_ts, sizeof(struct timespec))
2649                     == 0) {
2650                         printf("This system does not update directory modification times\n");
2651                         correct = False;
2652                 }
2653         }
2654         cli_unlink(cli, fname2);
2655         cli_rmdir(cli, dname);
2656
2657         if (!torture_close_connection(cli)) {
2658                 correct = False;
2659         }
2660
2661         printf("trans2 test finished\n");
2662
2663         return correct;
2664 }
2665
2666 /*
2667   This checks new W2K calls.
2668 */
2669
2670 static bool new_trans(struct cli_state *pcli, int fnum, int level)
2671 {
2672         char *buf = NULL;
2673         uint32 len;
2674         bool correct = True;
2675
2676         if (!cli_qfileinfo_test(pcli, fnum, level, &buf, &len)) {
2677                 printf("ERROR: qfileinfo (%d) failed (%s)\n", level, cli_errstr(pcli));
2678                 correct = False;
2679         } else {
2680                 printf("qfileinfo: level %d, len = %u\n", level, len);
2681                 dump_data(0, (uint8 *)buf, len);
2682                 printf("\n");
2683         }
2684         SAFE_FREE(buf);
2685         return correct;
2686 }
2687
2688 static bool run_w2ktest(int dummy)
2689 {
2690         struct cli_state *cli;
2691         int fnum;
2692         const char *fname = "\\w2ktest\\w2k.tst";
2693         int level;
2694         bool correct = True;
2695
2696         printf("starting w2k test\n");
2697
2698         if (!torture_open_connection(&cli, 0)) {
2699                 return False;
2700         }
2701
2702         fnum = cli_open(cli, fname, 
2703                         O_RDWR | O_CREAT , DENY_NONE);
2704
2705         for (level = 1004; level < 1040; level++) {
2706                 new_trans(cli, fnum, level);
2707         }
2708
2709         cli_close(cli, fnum);
2710
2711         if (!torture_close_connection(cli)) {
2712                 correct = False;
2713         }
2714
2715         printf("w2k test finished\n");
2716
2717         return correct;
2718 }
2719
2720
2721 /*
2722   this is a harness for some oplock tests
2723  */
2724 static bool run_oplock1(int dummy)
2725 {
2726         struct cli_state *cli1;
2727         const char *fname = "\\lockt1.lck";
2728         int fnum1;
2729         bool correct = True;
2730
2731         printf("starting oplock test 1\n");
2732
2733         if (!torture_open_connection(&cli1, 0)) {
2734                 return False;
2735         }
2736
2737         cli_unlink(cli1, fname);
2738
2739         cli_sockopt(cli1, sockops);
2740
2741         cli1->use_oplocks = True;
2742
2743         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2744         if (fnum1 == -1) {
2745                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
2746                 return False;
2747         }
2748
2749         cli1->use_oplocks = False;
2750
2751         cli_unlink(cli1, fname);
2752         cli_unlink(cli1, fname);
2753
2754         if (!cli_close(cli1, fnum1)) {
2755                 printf("close2 failed (%s)\n", cli_errstr(cli1));
2756                 return False;
2757         }
2758
2759         if (!cli_unlink(cli1, fname)) {
2760                 printf("unlink failed (%s)\n", cli_errstr(cli1));
2761                 return False;
2762         }
2763
2764         if (!torture_close_connection(cli1)) {
2765                 correct = False;
2766         }
2767
2768         printf("finished oplock test 1\n");
2769
2770         return correct;
2771 }
2772
2773 static bool run_oplock2(int dummy)
2774 {
2775         struct cli_state *cli1, *cli2;
2776         const char *fname = "\\lockt2.lck";
2777         int fnum1, fnum2;
2778         int saved_use_oplocks = use_oplocks;
2779         char buf[4];
2780         bool correct = True;
2781         volatile bool *shared_correct;
2782
2783         shared_correct = (volatile bool *)shm_setup(sizeof(bool));
2784         *shared_correct = True;
2785
2786         use_level_II_oplocks = True;
2787         use_oplocks = True;
2788
2789         printf("starting oplock test 2\n");
2790
2791         if (!torture_open_connection(&cli1, 0)) {
2792                 use_level_II_oplocks = False;
2793                 use_oplocks = saved_use_oplocks;
2794                 return False;
2795         }
2796
2797         cli1->use_oplocks = True;
2798         cli1->use_level_II_oplocks = True;
2799
2800         if (!torture_open_connection(&cli2, 1)) {
2801                 use_level_II_oplocks = False;
2802                 use_oplocks = saved_use_oplocks;
2803                 return False;
2804         }
2805
2806         cli2->use_oplocks = True;
2807         cli2->use_level_II_oplocks = True;
2808
2809         cli_unlink(cli1, fname);
2810
2811         cli_sockopt(cli1, sockops);
2812         cli_sockopt(cli2, sockops);
2813
2814         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2815         if (fnum1 == -1) {
2816                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
2817                 return False;
2818         }
2819
2820         /* Don't need the globals any more. */
2821         use_level_II_oplocks = False;
2822         use_oplocks = saved_use_oplocks;
2823
2824         if (fork() == 0) {
2825                 /* Child code */
2826                 fnum2 = cli_open(cli2, fname, O_RDWR, DENY_NONE);
2827                 if (fnum2 == -1) {
2828                         printf("second open of %s failed (%s)\n", fname, cli_errstr(cli1));
2829                         *shared_correct = False;
2830                         exit(0);
2831                 }
2832
2833                 sleep(2);
2834
2835                 if (!cli_close(cli2, fnum2)) {
2836                         printf("close2 failed (%s)\n", cli_errstr(cli1));
2837                         *shared_correct = False;
2838                 }
2839
2840                 exit(0);
2841         }
2842
2843         sleep(2);
2844
2845         /* Ensure cli1 processes the break. Empty file should always return 0
2846          * bytes.  */
2847
2848         if (cli_read(cli1, fnum1, buf, 0, 4) != 0) {
2849                 printf("read on fnum1 failed (%s)\n", cli_errstr(cli1));
2850                 correct = False;
2851         }
2852
2853         /* Should now be at level II. */
2854         /* Test if sending a write locks causes a break to none. */
2855
2856         if (!cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK)) {
2857                 printf("lock failed (%s)\n", cli_errstr(cli1));
2858                 correct = False;
2859         }
2860
2861         cli_unlock(cli1, fnum1, 0, 4);
2862
2863         sleep(2);
2864
2865         if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
2866                 printf("lock failed (%s)\n", cli_errstr(cli1));
2867                 correct = False;
2868         }
2869
2870         cli_unlock(cli1, fnum1, 0, 4);
2871
2872         sleep(2);
2873
2874         cli_read(cli1, fnum1, buf, 0, 4);
2875
2876 #if 0
2877         if (cli_write(cli1, fnum1, 0, buf, 0, 4) != 4) {
2878                 printf("write on fnum1 failed (%s)\n", cli_errstr(cli1));
2879                 correct = False;
2880         }
2881 #endif
2882
2883         if (!cli_close(cli1, fnum1)) {
2884                 printf("close1 failed (%s)\n", cli_errstr(cli1));
2885                 correct = False;
2886         }
2887
2888         sleep(4);
2889
2890         if (!cli_unlink(cli1, fname)) {
2891                 printf("unlink failed (%s)\n", cli_errstr(cli1));
2892                 correct = False;
2893         }
2894
2895         if (!torture_close_connection(cli1)) {
2896                 correct = False;
2897         }
2898
2899         if (!*shared_correct) {
2900                 correct = False;
2901         }
2902
2903         printf("finished oplock test 2\n");
2904
2905         return correct;
2906 }
2907
2908 /* handler for oplock 3 tests */
2909 static bool oplock3_handler(struct cli_state *cli, int fnum, unsigned char level)
2910 {
2911         printf("got oplock break fnum=%d level=%d\n",
2912                fnum, level);
2913         return cli_oplock_ack(cli, fnum, level);
2914 }
2915
2916 static bool run_oplock3(int dummy)
2917 {
2918         struct cli_state *cli;
2919         const char *fname = "\\oplockt3.dat";
2920         int fnum;
2921         char buf[4] = "abcd";
2922         bool correct = True;
2923         volatile bool *shared_correct;
2924
2925         shared_correct = (volatile bool *)shm_setup(sizeof(bool));
2926         *shared_correct = True;
2927
2928         printf("starting oplock test 3\n");
2929
2930         if (fork() == 0) {
2931                 /* Child code */
2932                 use_oplocks = True;
2933                 use_level_II_oplocks = True;
2934                 if (!torture_open_connection(&cli, 0)) {
2935                         *shared_correct = False;
2936                         exit(0);
2937                 } 
2938                 sleep(2);
2939                 /* try to trigger a oplock break in parent */
2940                 fnum = cli_open(cli, fname, O_RDWR, DENY_NONE);
2941                 cli_write(cli, fnum, 0, buf, 0, 4);
2942                 exit(0);
2943         }
2944
2945         /* parent code */
2946         use_oplocks = True;
2947         use_level_II_oplocks = True;
2948         if (!torture_open_connection(&cli, 1)) { /* other is forked */
2949                 return False;
2950         }
2951         cli_oplock_handler(cli, oplock3_handler);
2952         fnum = cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE);
2953         cli_write(cli, fnum, 0, buf, 0, 4);
2954         cli_close(cli, fnum);
2955         fnum = cli_open(cli, fname, O_RDWR, DENY_NONE);
2956         cli->timeout = 20000;
2957         cli_receive_smb(cli);
2958         printf("finished oplock test 3\n");
2959
2960         return (correct && *shared_correct);
2961
2962 /* What are we looking for here?  What's sucess and what's FAILURE? */
2963 }
2964
2965
2966
2967 /*
2968   Test delete on close semantics.
2969  */
2970 static bool run_deletetest(int dummy)
2971 {
2972         struct cli_state *cli1 = NULL;
2973         struct cli_state *cli2 = NULL;
2974         const char *fname = "\\delete.file";
2975         int fnum1 = -1;
2976         int fnum2 = -1;
2977         bool correct = True;
2978
2979         printf("starting delete test\n");
2980
2981         if (!torture_open_connection(&cli1, 0)) {
2982                 return False;
2983         }
2984
2985         cli_sockopt(cli1, sockops);
2986
2987         /* Test 1 - this should delete the file on close. */
2988
2989         cli_setatr(cli1, fname, 0, 0);
2990         cli_unlink(cli1, fname);
2991
2992         fnum1 = cli_nt_create_full(cli1, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
2993                                    0, FILE_OVERWRITE_IF, 
2994                                    FILE_DELETE_ON_CLOSE, 0);
2995
2996         if (fnum1 == -1) {
2997                 printf("[1] open of %s failed (%s)\n", fname, cli_errstr(cli1));
2998                 correct = False;
2999                 goto fail;
3000         }
3001
3002 #if 0 /* JRATEST */
3003         {
3004                 uint32 *accinfo = NULL;
3005                 uint32 len;
3006                 cli_qfileinfo_test(cli1, fnum1, SMB_FILE_ACCESS_INFORMATION, (char **)&accinfo, &len);
3007                 if (accinfo)
3008                         printf("access mode = 0x%lx\n", *accinfo);
3009                 SAFE_FREE(accinfo);
3010         }
3011 #endif
3012
3013         if (!cli_close(cli1, fnum1)) {
3014                 printf("[1] close failed (%s)\n", cli_errstr(cli1));
3015                 correct = False;
3016                 goto fail;
3017         }
3018
3019         fnum1 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
3020         if (fnum1 != -1) {
3021                 printf("[1] open of %s succeeded (should fail)\n", fname);
3022                 correct = False;
3023                 goto fail;
3024         }
3025
3026         printf("first delete on close test succeeded.\n");
3027
3028         /* Test 2 - this should delete the file on close. */
3029
3030         cli_setatr(cli1, fname, 0, 0);
3031         cli_unlink(cli1, fname);
3032
3033         fnum1 = cli_nt_create_full(cli1, fname, 0, GENERIC_ALL_ACCESS,
3034                                    FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, 
3035                                    FILE_OVERWRITE_IF, 0, 0);
3036
3037         if (fnum1 == -1) {
3038                 printf("[2] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3039                 correct = False;
3040                 goto fail;
3041         }
3042
3043         if (!cli_nt_delete_on_close(cli1, fnum1, True)) {
3044                 printf("[2] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3045                 correct = False;
3046                 goto fail;
3047         }
3048
3049         if (!cli_close(cli1, fnum1)) {
3050                 printf("[2] close failed (%s)\n", cli_errstr(cli1));
3051                 correct = False;
3052                 goto fail;
3053         }
3054
3055         fnum1 = cli_open(cli1, fname, O_RDONLY, DENY_NONE);
3056         if (fnum1 != -1) {
3057                 printf("[2] open of %s succeeded should have been deleted on close !\n", fname);
3058                 if (!cli_close(cli1, fnum1)) {
3059                         printf("[2] close failed (%s)\n", cli_errstr(cli1));
3060                         correct = False;
3061                         goto fail;
3062                 }
3063                 cli_unlink(cli1, fname);
3064         } else
3065                 printf("second delete on close test succeeded.\n");
3066
3067         /* Test 3 - ... */
3068         cli_setatr(cli1, fname, 0, 0);
3069         cli_unlink(cli1, fname);
3070
3071         fnum1 = cli_nt_create_full(cli1, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
3072                                    FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0);
3073
3074         if (fnum1 == -1) {
3075                 printf("[3] open - 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3076                 correct = False;
3077                 goto fail;
3078         }
3079
3080         /* This should fail with a sharing violation - open for delete is only compatible
3081            with SHARE_DELETE. */
3082
3083         fnum2 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3084                         FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0, 0);
3085
3086         if (fnum2 != -1) {
3087                 printf("[3] open  - 2 of %s succeeded - should have failed.\n", fname);
3088                 correct = False;
3089                 goto fail;
3090         }
3091
3092         /* This should succeed. */
3093
3094         fnum2 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3095                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0);
3096
3097         if (fnum2 == -1) {
3098                 printf("[3] open  - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
3099                 correct = False;
3100                 goto fail;
3101         }
3102
3103         if (!cli_nt_delete_on_close(cli1, fnum1, True)) {
3104                 printf("[3] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3105                 correct = False;
3106                 goto fail;
3107         }
3108
3109         if (!cli_close(cli1, fnum1)) {
3110                 printf("[3] close 1 failed (%s)\n", cli_errstr(cli1));
3111                 correct = False;
3112                 goto fail;
3113         }
3114
3115         if (!cli_close(cli1, fnum2)) {
3116                 printf("[3] close 2 failed (%s)\n", cli_errstr(cli1));
3117                 correct = False;
3118                 goto fail;
3119         }
3120
3121         /* This should fail - file should no longer be there. */
3122
3123         fnum1 = cli_open(cli1, fname, O_RDONLY, DENY_NONE);
3124         if (fnum1 != -1) {
3125                 printf("[3] open of %s succeeded should have been deleted on close !\n", fname);
3126                 if (!cli_close(cli1, fnum1)) {
3127                         printf("[3] close failed (%s)\n", cli_errstr(cli1));
3128                 }
3129                 cli_unlink(cli1, fname);
3130                 correct = False;
3131                 goto fail;
3132         } else
3133                 printf("third delete on close test succeeded.\n");
3134
3135         /* Test 4 ... */
3136         cli_setatr(cli1, fname, 0, 0);
3137         cli_unlink(cli1, fname);
3138
3139         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3140                         FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0);
3141
3142         if (fnum1 == -1) {
3143                 printf("[4] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3144                 correct = False;
3145                 goto fail;
3146         }
3147
3148         /* This should succeed. */
3149         fnum2 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS,
3150                         FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0);
3151         if (fnum2 == -1) {
3152                 printf("[4] open  - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
3153                 correct = False;
3154                 goto fail;
3155         }
3156
3157         if (!cli_close(cli1, fnum2)) {
3158                 printf("[4] close - 1 failed (%s)\n", cli_errstr(cli1));
3159                 correct = False;
3160                 goto fail;
3161         }
3162
3163         if (!cli_nt_delete_on_close(cli1, fnum1, True)) {
3164                 printf("[4] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3165                 correct = False;
3166                 goto fail;
3167         }
3168
3169         /* This should fail - no more opens once delete on close set. */
3170         fnum2 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS,
3171                                    FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3172                                    FILE_OPEN, 0, 0);
3173         if (fnum2 != -1) {
3174                 printf("[4] open  - 3 of %s succeeded ! Should have failed.\n", fname );
3175                 correct = False;
3176                 goto fail;
3177         } else
3178                 printf("fourth delete on close test succeeded.\n");
3179
3180         if (!cli_close(cli1, fnum1)) {
3181                 printf("[4] close - 2 failed (%s)\n", cli_errstr(cli1));
3182                 correct = False;
3183                 goto fail;
3184         }
3185
3186         /* Test 5 ... */
3187         cli_setatr(cli1, fname, 0, 0);
3188         cli_unlink(cli1, fname);
3189
3190         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT, DENY_NONE);
3191         if (fnum1 == -1) {
3192                 printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3193                 correct = False;
3194                 goto fail;
3195         }
3196
3197         /* This should fail - only allowed on NT opens with DELETE access. */
3198
3199         if (cli_nt_delete_on_close(cli1, fnum1, True)) {
3200                 printf("[5] setting delete_on_close on OpenX file succeeded - should fail !\n");
3201                 correct = False;
3202                 goto fail;
3203         }
3204
3205         if (!cli_close(cli1, fnum1)) {
3206                 printf("[5] close - 2 failed (%s)\n", cli_errstr(cli1));
3207                 correct = False;
3208                 goto fail;
3209         }
3210
3211         printf("fifth delete on close test succeeded.\n");
3212
3213         /* Test 6 ... */
3214         cli_setatr(cli1, fname, 0, 0);
3215         cli_unlink(cli1, fname);
3216
3217         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
3218                                    FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3219                                    FILE_OVERWRITE_IF, 0, 0);
3220
3221         if (fnum1 == -1) {
3222                 printf("[6] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3223                 correct = False;
3224                 goto fail;
3225         }
3226
3227         /* This should fail - only allowed on NT opens with DELETE access. */
3228
3229         if (cli_nt_delete_on_close(cli1, fnum1, True)) {
3230                 printf("[6] setting delete_on_close on file with no delete access succeeded - should fail !\n");
3231                 correct = False;
3232                 goto fail;
3233         }
3234
3235         if (!cli_close(cli1, fnum1)) {
3236                 printf("[6] close - 2 failed (%s)\n", cli_errstr(cli1));
3237                 correct = False;
3238                 goto fail;
3239         }
3240
3241         printf("sixth delete on close test succeeded.\n");
3242
3243         /* Test 7 ... */
3244         cli_setatr(cli1, fname, 0, 0);
3245         cli_unlink(cli1, fname);
3246
3247         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3248                                    FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, 0, 0);
3249
3250         if (fnum1 == -1) {
3251                 printf("[7] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3252                 correct = False;
3253                 goto fail;
3254         }
3255
3256         if (!cli_nt_delete_on_close(cli1, fnum1, True)) {
3257                 printf("[7] setting delete_on_close on file failed !\n");
3258                 correct = False;
3259                 goto fail;
3260         }
3261
3262         if (!cli_nt_delete_on_close(cli1, fnum1, False)) {
3263                 printf("[7] unsetting delete_on_close on file failed !\n");
3264                 correct = False;
3265                 goto fail;
3266         }
3267
3268         if (!cli_close(cli1, fnum1)) {
3269                 printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
3270                 correct = False;
3271                 goto fail;
3272         }
3273
3274         /* This next open should succeed - we reset the flag. */
3275
3276         fnum1 = cli_open(cli1, fname, O_RDONLY, DENY_NONE);
3277         if (fnum1 == -1) {
3278                 printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3279                 correct = False;
3280                 goto fail;
3281         }
3282
3283         if (!cli_close(cli1, fnum1)) {
3284                 printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
3285                 correct = False;
3286                 goto fail;
3287         }
3288
3289         printf("seventh delete on close test succeeded.\n");
3290
3291         /* Test 7 ... */
3292         cli_setatr(cli1, fname, 0, 0);
3293         cli_unlink(cli1, fname);
3294
3295         if (!torture_open_connection(&cli2, 1)) {
3296                 printf("[8] failed to open second connection.\n");
3297                 correct = False;
3298                 goto fail;
3299         }
3300
3301         cli_sockopt(cli1, sockops);
3302
3303         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3304                                    FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3305                                    FILE_OVERWRITE_IF, 0, 0);
3306
3307         if (fnum1 == -1) {
3308                 printf("[8] open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3309                 correct = False;
3310                 goto fail;
3311         }
3312
3313         fnum2 = cli_nt_create_full(cli2, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3314                                    FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3315                                    FILE_OPEN, 0, 0);
3316
3317         if (fnum2 == -1) {
3318                 printf("[8] open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
3319                 correct = False;
3320                 goto fail;
3321         }
3322
3323         if (!cli_nt_delete_on_close(cli1, fnum1, True)) {
3324                 printf("[8] setting delete_on_close on file failed !\n");
3325                 correct = False;
3326                 goto fail;
3327         }
3328
3329         if (!cli_close(cli1, fnum1)) {
3330                 printf("[8] close - 1 failed (%s)\n", cli_errstr(cli1));
3331                 correct = False;
3332                 goto fail;
3333         }
3334
3335         if (!cli_close(cli2, fnum2)) {
3336                 printf("[8] close - 2 failed (%s)\n", cli_errstr(cli2));
3337                 correct = False;
3338                 goto fail;
3339         }
3340
3341         /* This should fail.. */
3342         fnum1 = cli_open(cli1, fname, O_RDONLY, DENY_NONE);
3343         if (fnum1 != -1) {
3344                 printf("[8] open of %s succeeded should have been deleted on close !\n", fname);
3345                 goto fail;
3346                 correct = False;
3347         } else
3348                 printf("eighth delete on close test succeeded.\n");
3349
3350         /* This should fail - we need to set DELETE_ACCESS. */
3351         fnum1 = cli_nt_create_full(cli1, fname, 0,FILE_READ_DATA|FILE_WRITE_DATA,
3352                                    FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0);
3353
3354         if (fnum1 != -1) {
3355                 printf("[9] open of %s succeeded should have failed!\n", fname);
3356                 correct = False;
3357                 goto fail;
3358         }
3359
3360         printf("ninth delete on close test succeeded.\n");
3361
3362         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3363                                    FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0);
3364         if (fnum1 == -1) {
3365                 printf("[10] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3366                 correct = False;
3367                 goto fail;
3368         }
3369
3370         /* This should delete the file. */
3371         if (!cli_close(cli1, fnum1)) {
3372                 printf("[10] close failed (%s)\n", cli_errstr(cli1));
3373                 correct = False;
3374                 goto fail;
3375         }
3376
3377         /* This should fail.. */
3378         fnum1 = cli_open(cli1, fname, O_RDONLY, DENY_NONE);
3379         if (fnum1 != -1) {
3380                 printf("[10] open of %s succeeded should have been deleted on close !\n", fname);
3381                 goto fail;
3382                 correct = False;
3383         } else
3384                 printf("tenth delete on close test succeeded.\n");
3385
3386         cli_setatr(cli1, fname, 0, 0);
3387         cli_unlink(cli1, fname);
3388
3389         /* What error do we get when attempting to open a read-only file with
3390            delete access ? */
3391
3392         /* Create a readonly file. */
3393         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
3394                                    FILE_ATTRIBUTE_READONLY, FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0);
3395         if (fnum1 == -1) {
3396                 printf("[11] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3397                 correct = False;
3398                 goto fail;
3399         }
3400
3401         if (!cli_close(cli1, fnum1)) {
3402                 printf("[11] close failed (%s)\n", cli_errstr(cli1));
3403                 correct = False;
3404                 goto fail;
3405         }
3406
3407         /* Now try open for delete access. */
3408         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_ATTRIBUTES|DELETE_ACCESS,
3409                                    0, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3410                                    FILE_OVERWRITE_IF, 0, 0);
3411
3412         if (fnum1 != -1) {
3413                 printf("[11] open of %s succeeded should have been denied with ACCESS_DENIED!\n", fname);
3414                 cli_close(cli1, fnum1);
3415                 goto fail;
3416                 correct = False;
3417         } else {
3418                 NTSTATUS nterr = cli_nt_error(cli1);
3419                 if (!NT_STATUS_EQUAL(nterr,NT_STATUS_ACCESS_DENIED)) {
3420                         printf("[11] open of %s should have been denied with ACCESS_DENIED! Got error %s\n", fname, nt_errstr(nterr));
3421                         goto fail;
3422                         correct = False;
3423                 } else {
3424                         printf("eleventh delete on close test succeeded.\n");
3425                 }
3426         }
3427
3428         printf("finished delete test\n");
3429
3430   fail:
3431         /* FIXME: This will crash if we aborted before cli2 got
3432          * intialized, because these functions don't handle
3433          * uninitialized connections. */
3434
3435         if (fnum1 != -1) cli_close(cli1, fnum1);
3436         if (fnum2 != -1) cli_close(cli1, fnum2);
3437         cli_setatr(cli1, fname, 0, 0);
3438         cli_unlink(cli1, fname);
3439
3440         if (cli1 && !torture_close_connection(cli1)) {
3441                 correct = False;
3442         }
3443         if (cli2 && !torture_close_connection(cli2)) {
3444                 correct = False;
3445         }
3446         return correct;
3447 }
3448
3449
3450 /*
3451   print out server properties
3452  */
3453 static bool run_properties(int dummy)
3454 {
3455         struct cli_state *cli;
3456         bool correct = True;
3457
3458         printf("starting properties test\n");
3459
3460         ZERO_STRUCT(cli);
3461
3462         if (!torture_open_connection(&cli, 0)) {
3463                 return False;
3464         }
3465
3466         cli_sockopt(cli, sockops);
3467
3468         d_printf("Capabilities 0x%08x\n", cli->capabilities);
3469
3470         if (!torture_close_connection(cli)) {
3471                 correct = False;
3472         }
3473
3474         return correct;
3475 }
3476
3477
3478
3479 /* FIRST_DESIRED_ACCESS   0xf019f */
3480 #define FIRST_DESIRED_ACCESS   FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|\
3481                                FILE_READ_EA|                           /* 0xf */ \
3482                                FILE_WRITE_EA|FILE_READ_ATTRIBUTES|     /* 0x90 */ \
3483                                FILE_WRITE_ATTRIBUTES|                  /* 0x100 */ \
3484                                DELETE_ACCESS|READ_CONTROL_ACCESS|\
3485                                WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS     /* 0xf0000 */
3486 /* SECOND_DESIRED_ACCESS  0xe0080 */
3487 #define SECOND_DESIRED_ACCESS  FILE_READ_ATTRIBUTES|                   /* 0x80 */ \
3488                                READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
3489                                WRITE_OWNER_ACCESS                      /* 0xe0000 */
3490
3491 #if 0
3492 #define THIRD_DESIRED_ACCESS   FILE_READ_ATTRIBUTES|                   /* 0x80 */ \
3493                                READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
3494                                FILE_READ_DATA|\
3495                                WRITE_OWNER_ACCESS                      /* */
3496 #endif
3497
3498 /*
3499   Test ntcreate calls made by xcopy
3500  */
3501 static bool run_xcopy(int dummy)
3502 {
3503         static struct cli_state *cli1;
3504         const char *fname = "\\test.txt";
3505         bool correct = True;
3506         int fnum1, fnum2;
3507
3508         printf("starting xcopy test\n");
3509
3510         if (!torture_open_connection(&cli1, 0)) {
3511                 return False;
3512         }
3513
3514         fnum1 = cli_nt_create_full(cli1, fname, 0,
3515                                    FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
3516                                    FILE_SHARE_NONE, FILE_OVERWRITE_IF, 
3517                                    0x4044, 0);
3518
3519         if (fnum1 == -1) {
3520                 printf("First open failed - %s\n", cli_errstr(cli1));
3521                 return False;
3522         }
3523
3524         fnum2 = cli_nt_create_full(cli1, fname, 0,
3525                                    SECOND_DESIRED_ACCESS, 0,
3526                                    FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 
3527                                    0x200000, 0);
3528         if (fnum2 == -1) {
3529                 printf("second open failed - %s\n", cli_errstr(cli1));
3530                 return False;
3531         }
3532
3533         if (!torture_close_connection(cli1)) {
3534                 correct = False;
3535         }
3536
3537         return correct;
3538 }
3539
3540 /*
3541   Test rename on files open with share delete and no share delete.
3542  */
3543 static bool run_rename(int dummy)
3544 {
3545         static struct cli_state *cli1;
3546         const char *fname = "\\test.txt";
3547         const char *fname1 = "\\test1.txt";
3548         bool correct = True;
3549         int fnum1;
3550
3551         printf("starting rename test\n");
3552
3553         if (!torture_open_connection(&cli1, 0)) {
3554                 return False;
3555         }
3556
3557         cli_unlink(cli1, fname);
3558         cli_unlink(cli1, fname1);
3559         fnum1 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3560                                    FILE_SHARE_READ, FILE_OVERWRITE_IF, 0, 0);
3561
3562         if (fnum1 == -1) {
3563                 printf("First open failed - %s\n", cli_errstr(cli1));
3564                 return False;
3565         }
3566
3567         if (!cli_rename(cli1, fname, fname1)) {
3568                 printf("First rename failed (SHARE_READ) (this is correct) - %s\n", cli_errstr(cli1));
3569         } else {
3570                 printf("First rename succeeded (SHARE_READ) - this should have failed !\n");
3571                 correct = False;
3572         }
3573
3574         if (!cli_close(cli1, fnum1)) {
3575                 printf("close - 1 failed (%s)\n", cli_errstr(cli1));
3576                 return False;
3577         }
3578
3579         cli_unlink(cli1, fname);
3580         cli_unlink(cli1, fname1);
3581         fnum1 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3582 #if 0
3583                                    FILE_SHARE_DELETE|FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0);
3584 #else
3585                                    FILE_SHARE_DELETE|FILE_SHARE_READ, FILE_OVERWRITE_IF, 0, 0);
3586 #endif
3587
3588         if (fnum1 == -1) {
3589                 printf("Second open failed - %s\n", cli_errstr(cli1));
3590                 return False;
3591         }
3592
3593         if (!cli_rename(cli1, fname, fname1)) {
3594                 printf("Second rename failed (SHARE_DELETE | SHARE_READ) - this should have succeeded - %s\n", cli_errstr(cli1));
3595                 correct = False;
3596         } else {
3597                 printf("Second rename succeeded (SHARE_DELETE | SHARE_READ)\n");
3598         }
3599
3600         if (!cli_close(cli1, fnum1)) {
3601                 printf("close - 2 failed (%s)\n", cli_errstr(cli1));
3602                 return False;
3603         }
3604
3605         cli_unlink(cli1, fname);
3606         cli_unlink(cli1, fname1);
3607
3608         fnum1 = cli_nt_create_full(cli1, fname, 0, READ_CONTROL_ACCESS, FILE_ATTRIBUTE_NORMAL,
3609                                    FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0);
3610
3611         if (fnum1 == -1) {
3612                 printf("Third open failed - %s\n", cli_errstr(cli1));
3613                 return False;
3614         }
3615
3616
3617 #if 0
3618   {
36