Make us bug-for-bug compatible with W2K3 - to get delete on close semantics
[amitay/samba.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 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #define NO_SYSLOG
22
23 #include "includes.h"
24
25 static fstring host, workgroup, share, password, username, myname;
26 static int max_protocol = PROTOCOL_NT1;
27 static const char *sockops="TCP_NODELAY";
28 static int nprocs=1;
29 static int port_to_use=0;
30 int torture_numops=100;
31 static int procnum; /* records process count number when forking */
32 static struct cli_state *current_cli;
33 static fstring randomfname;
34 static BOOL use_oplocks;
35 static BOOL use_level_II_oplocks;
36 static const char *client_txt = "client_oplocks.txt";
37 static BOOL use_kerberos;
38
39 BOOL torture_showall = False;
40
41 static double create_procs(BOOL (*fn)(int), BOOL *result);
42
43
44 static struct timeval tp1,tp2;
45
46 void start_timer(void)
47 {
48         gettimeofday(&tp1,NULL);
49 }
50
51 double end_timer(void)
52 {
53         gettimeofday(&tp2,NULL);
54         return((tp2.tv_sec - tp1.tv_sec) + 
55                (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
56 }
57
58
59 /* return a pointer to a anonymous shared memory segment of size "size"
60    which will persist across fork() but will disappear when all processes
61    exit 
62
63    The memory is not zeroed 
64
65    This function uses system5 shared memory. It takes advantage of a property
66    that the memory is not destroyed if it is attached when the id is removed
67    */
68 void *shm_setup(int size)
69 {
70         int shmid;
71         void *ret;
72
73         shmid = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
74         if (shmid == -1) {
75                 printf("can't get shared memory\n");
76                 exit(1);
77         }
78         ret = (void *)shmat(shmid, 0, 0);
79         if (!ret || ret == (void *)-1) {
80                 printf("can't attach to shared memory\n");
81                 return NULL;
82         }
83         /* the following releases the ipc, but note that this process
84            and all its children will still have access to the memory, its
85            just that the shmid is no longer valid for other shm calls. This
86            means we don't leave behind lots of shm segments after we exit 
87
88            See Stevens "advanced programming in unix env" for details
89            */
90         shmctl(shmid, IPC_RMID, 0);
91         
92         return ret;
93 }
94
95
96 static BOOL open_nbt_connection(struct cli_state *c)
97 {
98         struct nmb_name called, calling;
99         struct in_addr ip;
100
101         ZERO_STRUCTP(c);
102
103         make_nmb_name(&calling, myname, 0x0);
104         make_nmb_name(&called , host, 0x20);
105
106         zero_ip(&ip);
107
108         if (!cli_initialise(c)) {
109                 printf("Failed initialize cli_struct to connect with %s\n", host);
110                 return False;
111         }
112
113         c->port = port_to_use;
114
115         if (!cli_connect(c, host, &ip)) {
116                 printf("Failed to connect with %s\n", host);
117                 return False;
118         }
119
120         c->use_kerberos = use_kerberos;
121
122         c->timeout = 120000; /* set a really long timeout (2 minutes) */
123         if (use_oplocks) c->use_oplocks = True;
124         if (use_level_II_oplocks) c->use_level_II_oplocks = True;
125
126         if (!cli_session_request(c, &calling, &called)) {
127                 /*
128                  * Well, that failed, try *SMBSERVER ... 
129                  * However, we must reconnect as well ...
130                  */
131                 if (!cli_connect(c, host, &ip)) {
132                         printf("Failed to connect with %s\n", host);
133                         return False;
134                 }
135
136                 make_nmb_name(&called, "*SMBSERVER", 0x20);
137                 if (!cli_session_request(c, &calling, &called)) {
138                         printf("%s rejected the session\n",host);
139                         printf("We tried with a called name of %s & %s\n",
140                                 host, "*SMBSERVER");
141                         cli_shutdown(c);
142                         return False;
143                 }
144         }
145
146         return True;
147 }
148
149 BOOL torture_open_connection(struct cli_state **c)
150 {
151         BOOL retry;
152         int flags = 0;
153         NTSTATUS status;
154
155         if (use_kerberos)
156                 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
157         
158         status = cli_full_connection(c, myname,
159                                      host, NULL, port_to_use, 
160                                      share, "?????", 
161                                      username, workgroup, 
162                                      password, flags, Undefined, &retry);
163         if (!NT_STATUS_IS_OK(status)) {
164                 return False;
165         }
166
167         if (use_oplocks) (*c)->use_oplocks = True;
168         if (use_level_II_oplocks) (*c)->use_level_II_oplocks = True;
169         (*c)->timeout = 120000; /* set a really long timeout (2 minutes) */
170
171         return True;
172 }
173
174 BOOL torture_cli_session_setup2(struct cli_state *cli, uint16 *new_vuid)
175 {
176         uint16 old_vuid = cli->vuid;
177         fstring old_user_name;
178         size_t passlen = strlen(password);
179         BOOL ret;
180
181         fstrcpy(old_user_name, cli->user_name);
182         cli->vuid = 0;
183         ret = cli_session_setup(cli, username, password, passlen, password, passlen, workgroup);
184         *new_vuid = cli->vuid;
185         cli->vuid = old_vuid;
186         fstrcpy(cli->user_name, old_user_name);
187         return ret;
188 }
189
190
191 BOOL torture_close_connection(struct cli_state *c)
192 {
193         BOOL ret = True;
194         if (!cli_tdis(c)) {
195                 printf("tdis failed (%s)\n", cli_errstr(c));
196                 ret = False;
197         }
198
199         cli_shutdown(c);
200
201         return ret;
202 }
203
204
205 /* check if the server produced the expected error code */
206 static BOOL check_error(int line, struct cli_state *c, 
207                         uint8 eclass, uint32 ecode, NTSTATUS nterr)
208 {
209         if (cli_is_dos_error(c)) {
210                 uint8 class;
211                 uint32 num;
212
213                 /* Check DOS error */
214
215                 cli_dos_error(c, &class, &num);
216
217                 if (eclass != class || ecode != num) {
218                         printf("unexpected error code class=%d code=%d\n", 
219                                (int)class, (int)num);
220                         printf(" expected %d/%d %s (line=%d)\n", 
221                                (int)eclass, (int)ecode, nt_errstr(nterr), line);
222                         return False;
223                 }
224
225         } else {
226                 NTSTATUS status;
227
228                 /* Check NT error */
229
230                 status = cli_nt_error(c);
231
232                 if (NT_STATUS_V(nterr) != NT_STATUS_V(status)) {
233                         printf("unexpected error code %s\n", nt_errstr(status));
234                         printf(" expected %s (line=%d)\n", nt_errstr(nterr), line);
235                         return False;
236                 }
237         }
238
239         return True;
240 }
241
242
243 static BOOL wait_lock(struct cli_state *c, int fnum, uint32 offset, uint32 len)
244 {
245         while (!cli_lock(c, fnum, offset, len, -1, WRITE_LOCK)) {
246                 if (!check_error(__LINE__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
247         }
248         return True;
249 }
250
251
252 static BOOL rw_torture(struct cli_state *c)
253 {
254         const char *lockfname = "\\torture.lck";
255         fstring fname;
256         int fnum;
257         int fnum2;
258         pid_t pid2, pid = getpid();
259         int i, j;
260         char buf[1024];
261         BOOL correct = True;
262
263         fnum2 = cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL, 
264                          DENY_NONE);
265         if (fnum2 == -1)
266                 fnum2 = cli_open(c, lockfname, O_RDWR, DENY_NONE);
267         if (fnum2 == -1) {
268                 printf("open of %s failed (%s)\n", lockfname, cli_errstr(c));
269                 return False;
270         }
271
272
273         for (i=0;i<torture_numops;i++) {
274                 unsigned n = (unsigned)sys_random()%10;
275                 if (i % 10 == 0) {
276                         printf("%d\r", i); fflush(stdout);
277                 }
278                 slprintf(fname, sizeof(fstring) - 1, "\\torture.%u", n);
279
280                 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
281                         return False;
282                 }
283
284                 fnum = cli_open(c, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL);
285                 if (fnum == -1) {
286                         printf("open failed (%s)\n", cli_errstr(c));
287                         correct = False;
288                         break;
289                 }
290
291                 if (cli_write(c, fnum, 0, (char *)&pid, 0, sizeof(pid)) != sizeof(pid)) {
292                         printf("write failed (%s)\n", cli_errstr(c));
293                         correct = False;
294                 }
295
296                 for (j=0;j<50;j++) {
297                         if (cli_write(c, fnum, 0, (char *)buf, 
298                                       sizeof(pid)+(j*sizeof(buf)), 
299                                       sizeof(buf)) != sizeof(buf)) {
300                                 printf("write failed (%s)\n", cli_errstr(c));
301                                 correct = False;
302                         }
303                 }
304
305                 pid2 = 0;
306
307                 if (cli_read(c, fnum, (char *)&pid2, 0, sizeof(pid)) != sizeof(pid)) {
308                         printf("read failed (%s)\n", cli_errstr(c));
309                         correct = False;
310                 }
311
312                 if (pid2 != pid) {
313                         printf("data corruption!\n");
314                         correct = False;
315                 }
316
317                 if (!cli_close(c, fnum)) {
318                         printf("close failed (%s)\n", cli_errstr(c));
319                         correct = False;
320                 }
321
322                 if (!cli_unlink(c, fname)) {
323                         printf("unlink failed (%s)\n", cli_errstr(c));
324                         correct = False;
325                 }
326
327                 if (!cli_unlock(c, fnum2, n*sizeof(int), sizeof(int))) {
328                         printf("unlock failed (%s)\n", cli_errstr(c));
329                         correct = False;
330                 }
331         }
332
333         cli_close(c, fnum2);
334         cli_unlink(c, lockfname);
335
336         printf("%d\n", i);
337
338         return correct;
339 }
340
341 static BOOL run_torture(int dummy)
342 {
343         struct cli_state *cli;
344         BOOL ret;
345
346         cli = current_cli;
347
348         cli_sockopt(cli, sockops);
349
350         ret = rw_torture(cli);
351         
352         if (!torture_close_connection(cli)) {
353                 ret = False;
354         }
355
356         return ret;
357 }
358
359 static BOOL rw_torture3(struct cli_state *c, char *lockfname)
360 {
361         int fnum = -1;
362         unsigned int i = 0;
363         char buf[131072];
364         char buf_rd[131072];
365         unsigned count;
366         unsigned countprev = 0;
367         ssize_t sent = 0;
368         BOOL correct = True;
369
370         srandom(1);
371         for (i = 0; i < sizeof(buf); i += sizeof(uint32))
372         {
373                 SIVAL(buf, i, sys_random());
374         }
375
376         if (procnum == 0)
377         {
378                 fnum = cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL, 
379                                  DENY_NONE);
380                 if (fnum == -1) {
381                         printf("first open read/write of %s failed (%s)\n",
382                                         lockfname, cli_errstr(c));
383                         return False;
384                 }
385         }
386         else
387         {
388                 for (i = 0; i < 500 && fnum == -1; i++)
389                 {
390                         fnum = cli_open(c, lockfname, O_RDONLY, 
391                                          DENY_NONE);
392                         msleep(10);
393                 }
394                 if (fnum == -1) {
395                         printf("second open read-only of %s failed (%s)\n",
396                                         lockfname, cli_errstr(c));
397                         return False;
398                 }
399         }
400
401         i = 0;
402         for (count = 0; count < sizeof(buf); count += sent)
403         {
404                 if (count >= countprev) {
405                         printf("%d %8d\r", i, count);
406                         fflush(stdout);
407                         i++;
408                         countprev += (sizeof(buf) / 20);
409                 }
410
411                 if (procnum == 0)
412                 {
413                         sent = ((unsigned)sys_random()%(20))+ 1;
414                         if (sent > sizeof(buf) - count)
415                         {
416                                 sent = sizeof(buf) - count;
417                         }
418
419                         if (cli_write(c, fnum, 0, buf+count, count, (size_t)sent) != sent) {
420                                 printf("write failed (%s)\n", cli_errstr(c));
421                                 correct = False;
422                         }
423                 }
424                 else
425                 {
426                         sent = cli_read(c, fnum, buf_rd+count, count,
427                                                   sizeof(buf)-count);
428                         if (sent < 0)
429                         {
430                                 printf("read failed offset:%d size:%ld (%s)\n",
431                                        count, (unsigned long)sizeof(buf)-count,
432                                        cli_errstr(c));
433                                 correct = False;
434                                 sent = 0;
435                         }
436                         if (sent > 0)
437                         {
438                                 if (memcmp(buf_rd+count, buf+count, sent) != 0)
439                                 {
440                                         printf("read/write compare failed\n");
441                                         printf("offset: %d req %ld recvd %ld\n", count, (unsigned long)sizeof(buf)-count, (unsigned long)sent);
442                                         correct = False;
443                                         break;
444                                 }
445                         }
446                 }
447
448         }
449
450         if (!cli_close(c, fnum)) {
451                 printf("close failed (%s)\n", cli_errstr(c));
452                 correct = False;
453         }
454
455         return correct;
456 }
457
458 static BOOL rw_torture2(struct cli_state *c1, struct cli_state *c2)
459 {
460         const char *lockfname = "\\torture2.lck";
461         int fnum1;
462         int fnum2;
463         int i;
464         uchar buf[131072];
465         uchar buf_rd[131072];
466         BOOL correct = True;
467         ssize_t bytes_read;
468
469         if (!cli_unlink(c1, lockfname)) {
470                 printf("unlink failed (%s) (normal, this file should not exist)\n", cli_errstr(c1));
471         }
472
473         fnum1 = cli_open(c1, lockfname, O_RDWR | O_CREAT | O_EXCL, 
474                          DENY_NONE);
475         if (fnum1 == -1) {
476                 printf("first open read/write of %s failed (%s)\n",
477                                 lockfname, cli_errstr(c1));
478                 return False;
479         }
480         fnum2 = cli_open(c2, lockfname, O_RDONLY, 
481                          DENY_NONE);
482         if (fnum2 == -1) {
483                 printf("second open read-only of %s failed (%s)\n",
484                                 lockfname, cli_errstr(c2));
485                 cli_close(c1, fnum1);
486                 return False;
487         }
488
489         for (i=0;i<torture_numops;i++)
490         {
491                 size_t buf_size = ((unsigned)sys_random()%(sizeof(buf)-1))+ 1;
492                 if (i % 10 == 0) {
493                         printf("%d\r", i); fflush(stdout);
494                 }
495
496                 generate_random_buffer(buf, buf_size, False);
497
498                 if (cli_write(c1, fnum1, 0, buf, 0, buf_size) != buf_size) {
499                         printf("write failed (%s)\n", cli_errstr(c1));
500                         correct = False;
501                         break;
502                 }
503
504                 if ((bytes_read = cli_read(c2, fnum2, buf_rd, 0, buf_size)) != buf_size) {
505                         printf("read failed (%s)\n", cli_errstr(c2));
506                         printf("read %d, expected %ld\n", bytes_read, 
507                                (unsigned long)buf_size); 
508                         correct = False;
509                         break;
510                 }
511
512                 if (memcmp(buf_rd, buf, buf_size) != 0)
513                 {
514                         printf("read/write compare failed\n");
515                         correct = False;
516                         break;
517                 }
518         }
519
520         if (!cli_close(c2, fnum2)) {
521                 printf("close failed (%s)\n", cli_errstr(c2));
522                 correct = False;
523         }
524         if (!cli_close(c1, fnum1)) {
525                 printf("close failed (%s)\n", cli_errstr(c1));
526                 correct = False;
527         }
528
529         if (!cli_unlink(c1, lockfname)) {
530                 printf("unlink failed (%s)\n", cli_errstr(c1));
531                 correct = False;
532         }
533
534         return correct;
535 }
536
537 static BOOL run_readwritetest(int dummy)
538 {
539         static struct cli_state *cli1, *cli2;
540         BOOL test1, test2;
541
542         if (!torture_open_connection(&cli1) || !torture_open_connection(&cli2)) {
543                 return False;
544         }
545         cli_sockopt(cli1, sockops);
546         cli_sockopt(cli2, sockops);
547
548         printf("starting readwritetest\n");
549
550         test1 = rw_torture2(cli1, cli2);
551         printf("Passed readwritetest v1: %s\n", BOOLSTR(test1));
552
553         if (test1) {
554                 test2 = rw_torture2(cli1, cli1);
555                 printf("Passed readwritetest v2: %s\n", BOOLSTR(test2));
556         }
557
558         if (!torture_close_connection(cli1)) {
559                 test1 = False;
560         }
561
562         if (!torture_close_connection(cli2)) {
563                 test2 = False;
564         }
565
566         return (test1 && test2);
567 }
568
569 static BOOL run_readwritemulti(int dummy)
570 {
571         struct cli_state *cli;
572         BOOL test;
573
574         cli = current_cli;
575
576         cli_sockopt(cli, sockops);
577
578         printf("run_readwritemulti: fname %s\n", randomfname);
579         test = rw_torture3(cli, randomfname);
580
581         if (!torture_close_connection(cli)) {
582                 test = False;
583         }
584         
585         return test;
586 }
587
588 static BOOL run_readwritelarge(int dummy)
589 {
590         static struct cli_state *cli1;
591         int fnum1;
592         const char *lockfname = "\\large.dat";
593         size_t fsize;
594         char buf[126*1024];
595         BOOL correct = True;
596  
597         if (!torture_open_connection(&cli1)) {
598                 return False;
599         }
600         cli_sockopt(cli1, sockops);
601         memset(buf,'\0',sizeof(buf));
602         
603         cli1->max_xmit = 128*1024;
604         
605         printf("starting readwritelarge\n");
606  
607         cli_unlink(cli1, lockfname);
608
609         fnum1 = cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE);
610         if (fnum1 == -1) {
611                 printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
612                 return False;
613         }
614    
615         cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf));
616
617         if (!cli_qfileinfo(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL, NULL, NULL)) {
618                 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
619                 correct = False;
620         }
621
622         if (fsize == sizeof(buf))
623                 printf("readwritelarge test 1 succeeded (size = %lx)\n", 
624                        (unsigned long)fsize);
625         else {
626                 printf("readwritelarge test 1 failed (size = %lx)\n", 
627                        (unsigned long)fsize);
628                 correct = False;
629         }
630
631         if (!cli_close(cli1, fnum1)) {
632                 printf("close failed (%s)\n", cli_errstr(cli1));
633                 correct = False;
634         }
635
636         if (!cli_unlink(cli1, lockfname)) {
637                 printf("unlink failed (%s)\n", cli_errstr(cli1));
638                 correct = False;
639         }
640
641         fnum1 = cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE);
642         if (fnum1 == -1) {
643                 printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
644                 return False;
645         }
646         
647         cli1->max_xmit = 4*1024;
648         
649         cli_smbwrite(cli1, fnum1, buf, 0, sizeof(buf));
650         
651         if (!cli_qfileinfo(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL, NULL, NULL)) {
652                 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
653                 correct = False;
654         }
655
656         if (fsize == sizeof(buf))
657                 printf("readwritelarge test 2 succeeded (size = %lx)\n", 
658                        (unsigned long)fsize);
659         else {
660                 printf("readwritelarge test 2 failed (size = %lx)\n", 
661                        (unsigned long)fsize);
662                 correct = False;
663         }
664
665 #if 0
666         /* ToDo - set allocation. JRA */
667         if(!cli_set_allocation_size(cli1, fnum1, 0)) {
668                 printf("set allocation size to zero failed (%s)\n", cli_errstr(&cli1));
669                 return False;
670         }
671         if (!cli_qfileinfo(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL, NULL, NULL)) {
672                 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
673                 correct = False;
674         }
675         if (fsize != 0)
676                 printf("readwritelarge test 3 (truncate test) succeeded (size = %x)\n", fsize);
677 #endif
678
679         if (!cli_close(cli1, fnum1)) {
680                 printf("close failed (%s)\n", cli_errstr(cli1));
681                 correct = False;
682         }
683         
684         if (!torture_close_connection(cli1)) {
685                 correct = False;
686         }
687         return correct;
688 }
689
690 int line_count = 0;
691 int nbio_id;
692
693 #define ival(s) strtol(s, NULL, 0)
694
695 /* run a test that simulates an approximate netbench client load */
696 static BOOL run_netbench(int client)
697 {
698         struct cli_state *cli;
699         int i;
700         fstring fname;
701         pstring line;
702         char cname[20];
703         FILE *f;
704         const char *params[20];
705         BOOL correct = True;
706
707         cli = current_cli;
708
709         nbio_id = client;
710
711         cli_sockopt(cli, sockops);
712
713         nb_setup(cli);
714
715         slprintf(cname,sizeof(fname), "client%d", client);
716
717         f = fopen(client_txt, "r");
718
719         if (!f) {
720                 perror(client_txt);
721                 return False;
722         }
723
724         while (fgets(line, sizeof(line)-1, f)) {
725                 line_count++;
726
727                 line[strlen(line)-1] = 0;
728
729                 /* printf("[%d] %s\n", line_count, line); */
730
731                 all_string_sub(line,"client1", cname, sizeof(line));
732                 
733                 /* parse the command parameters */
734                 params[0] = strtok(line," ");
735                 i = 0;
736                 while (params[i]) params[++i] = strtok(NULL," ");
737
738                 params[i] = "";
739
740                 if (i < 2) continue;
741
742                 if (!strncmp(params[0],"SMB", 3)) {
743                         printf("ERROR: You are using a dbench 1 load file\n");
744                         exit(1);
745                 }
746
747                 if (!strcmp(params[0],"NTCreateX")) {
748                         nb_createx(params[1], ival(params[2]), ival(params[3]), 
749                                    ival(params[4]));
750                 } else if (!strcmp(params[0],"Close")) {
751                         nb_close(ival(params[1]));
752                 } else if (!strcmp(params[0],"Rename")) {
753                         nb_rename(params[1], params[2]);
754                 } else if (!strcmp(params[0],"Unlink")) {
755                         nb_unlink(params[1]);
756                 } else if (!strcmp(params[0],"Deltree")) {
757                         nb_deltree(params[1]);
758                 } else if (!strcmp(params[0],"Rmdir")) {
759                         nb_rmdir(params[1]);
760                 } else if (!strcmp(params[0],"QUERY_PATH_INFORMATION")) {
761                         nb_qpathinfo(params[1]);
762                 } else if (!strcmp(params[0],"QUERY_FILE_INFORMATION")) {
763                         nb_qfileinfo(ival(params[1]));
764                 } else if (!strcmp(params[0],"QUERY_FS_INFORMATION")) {
765                         nb_qfsinfo(ival(params[1]));
766                 } else if (!strcmp(params[0],"FIND_FIRST")) {
767                         nb_findfirst(params[1]);
768                 } else if (!strcmp(params[0],"WriteX")) {
769                         nb_writex(ival(params[1]), 
770                                   ival(params[2]), ival(params[3]), ival(params[4]));
771                 } else if (!strcmp(params[0],"ReadX")) {
772                         nb_readx(ival(params[1]), 
773                                   ival(params[2]), ival(params[3]), ival(params[4]));
774                 } else if (!strcmp(params[0],"Flush")) {
775                         nb_flush(ival(params[1]));
776                 } else {
777                         printf("Unknown operation %s\n", params[0]);
778                         exit(1);
779                 }
780         }
781         fclose(f);
782
783         nb_cleanup();
784
785         if (!torture_close_connection(cli)) {
786                 correct = False;
787         }
788         
789         return correct;
790 }
791
792
793 /* run a test that simulates an approximate netbench client load */
794 static BOOL run_nbench(int dummy)
795 {
796         double t;
797         BOOL correct = True;
798
799         nbio_shmem(nprocs);
800
801         nbio_id = -1;
802
803         signal(SIGALRM, nb_alarm);
804         alarm(1);
805         t = create_procs(run_netbench, &correct);
806         alarm(0);
807
808         printf("\nThroughput %g MB/sec\n", 
809                1.0e-6 * nbio_total() / t);
810         return correct;
811 }
812
813
814 /*
815   This test checks for two things:
816
817   1) correct support for retaining locks over a close (ie. the server
818      must not use posix semantics)
819   2) support for lock timeouts
820  */
821 static BOOL run_locktest1(int dummy)
822 {
823         struct cli_state *cli1, *cli2;
824         const char *fname = "\\lockt1.lck";
825         int fnum1, fnum2, fnum3;
826         time_t t1, t2;
827         unsigned lock_timeout;
828
829         if (!torture_open_connection(&cli1) || !torture_open_connection(&cli2)) {
830                 return False;
831         }
832         cli_sockopt(cli1, sockops);
833         cli_sockopt(cli2, sockops);
834
835         printf("starting locktest1\n");
836
837         cli_unlink(cli1, fname);
838
839         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
840         if (fnum1 == -1) {
841                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
842                 return False;
843         }
844         fnum2 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
845         if (fnum2 == -1) {
846                 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli1));
847                 return False;
848         }
849         fnum3 = cli_open(cli2, fname, O_RDWR, DENY_NONE);
850         if (fnum3 == -1) {
851                 printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli2));
852                 return False;
853         }
854
855         if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
856                 printf("lock1 failed (%s)\n", cli_errstr(cli1));
857                 return False;
858         }
859
860
861         if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
862                 printf("lock2 succeeded! This is a locking bug\n");
863                 return False;
864         } else {
865                 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock, 
866                                  NT_STATUS_LOCK_NOT_GRANTED)) return False;
867         }
868
869
870         lock_timeout = (1 + (random() % 20));
871         printf("Testing lock timeout with timeout=%u\n", lock_timeout);
872         t1 = time(NULL);
873         if (cli_lock(cli2, fnum3, 0, 4, lock_timeout * 500, WRITE_LOCK)) {
874                 printf("lock3 succeeded! This is a locking bug\n");
875                 return False;
876         } else {
877                 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock, 
878                                  NT_STATUS_FILE_LOCK_CONFLICT)) return False;
879         }
880         t2 = time(NULL);
881
882         if (ABS(t2 - t1) < lock_timeout-1) {
883                 printf("error: This server appears not to support timed lock requests\n");
884         }
885
886         printf("server slept for %u seconds for a %u second timeout\n",
887                (unsigned int)(t2-t1), lock_timeout);
888
889         if (!cli_close(cli1, fnum2)) {
890                 printf("close1 failed (%s)\n", cli_errstr(cli1));
891                 return False;
892         }
893
894         if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
895                 printf("lock4 succeeded! This is a locking bug\n");
896                 return False;
897         } else {
898                 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock, 
899                                  NT_STATUS_FILE_LOCK_CONFLICT)) return False;
900         }
901
902         if (!cli_close(cli1, fnum1)) {
903                 printf("close2 failed (%s)\n", cli_errstr(cli1));
904                 return False;
905         }
906
907         if (!cli_close(cli2, fnum3)) {
908                 printf("close3 failed (%s)\n", cli_errstr(cli2));
909                 return False;
910         }
911
912         if (!cli_unlink(cli1, fname)) {
913                 printf("unlink failed (%s)\n", cli_errstr(cli1));
914                 return False;
915         }
916
917
918         if (!torture_close_connection(cli1)) {
919                 return False;
920         }
921
922         if (!torture_close_connection(cli2)) {
923                 return False;
924         }
925
926         printf("Passed locktest1\n");
927         return True;
928 }
929
930 /*
931   this checks to see if a secondary tconx can use open files from an
932   earlier tconx
933  */
934 static BOOL run_tcon_test(int dummy)
935 {
936         static struct cli_state *cli;
937         const char *fname = "\\tcontest.tmp";
938         int fnum1;
939         uint16 cnum1, cnum2, cnum3;
940         uint16 vuid1, vuid2;
941         char buf[4];
942         BOOL ret = True;
943
944         if (!torture_open_connection(&cli)) {
945                 return False;
946         }
947         cli_sockopt(cli, sockops);
948
949         printf("starting tcontest\n");
950
951         cli_unlink(cli, fname);
952
953         fnum1 = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
954         if (fnum1 == -1) {
955                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
956                 return False;
957         }
958
959         cnum1 = cli->cnum;
960         vuid1 = cli->vuid;
961
962         if (cli_write(cli, fnum1, 0, buf, 130, 4) != 4) {
963                 printf("initial write failed (%s)", cli_errstr(cli));
964                 return False;
965         }
966
967         if (!cli_send_tconX(cli, share, "?????",
968                             password, strlen(password)+1)) {
969                 printf("%s refused 2nd tree connect (%s)\n", host,
970                            cli_errstr(cli));
971                 cli_shutdown(cli);
972                 return False;
973         }
974
975         cnum2 = cli->cnum;
976         cnum3 = MAX(cnum1, cnum2) + 1; /* any invalid number */
977         vuid2 = cli->vuid + 1;
978
979         /* try a write with the wrong tid */
980         cli->cnum = cnum2;
981
982         if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
983                 printf("* server allows write with wrong TID\n");
984                 ret = False;
985         } else {
986                 printf("server fails write with wrong TID : %s\n", cli_errstr(cli));
987         }
988
989
990         /* try a write with an invalid tid */
991         cli->cnum = cnum3;
992
993         if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
994                 printf("* server allows write with invalid TID\n");
995                 ret = False;
996         } else {
997                 printf("server fails write with invalid TID : %s\n", cli_errstr(cli));
998         }
999
1000         /* try a write with an invalid vuid */
1001         cli->vuid = vuid2;
1002         cli->cnum = cnum1;
1003
1004         if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1005                 printf("* server allows write with invalid VUID\n");
1006                 ret = False;
1007         } else {
1008                 printf("server fails write with invalid VUID : %s\n", cli_errstr(cli));
1009         }
1010
1011         cli->cnum = cnum1;
1012         cli->vuid = vuid1;
1013
1014         if (!cli_close(cli, fnum1)) {
1015                 printf("close failed (%s)\n", cli_errstr(cli));
1016                 return False;
1017         }
1018
1019         cli->cnum = cnum2;
1020
1021         if (!cli_tdis(cli)) {
1022                 printf("secondary tdis failed (%s)\n", cli_errstr(cli));
1023                 return False;
1024         }
1025
1026         cli->cnum = cnum1;
1027
1028         if (!torture_close_connection(cli)) {
1029                 return False;
1030         }
1031
1032         return ret;
1033 }
1034
1035
1036 /*
1037  checks for old style tcon support
1038  */
1039 static BOOL run_tcon2_test(int dummy)
1040 {
1041         static struct cli_state *cli;
1042         uint16 cnum, max_xmit;
1043         char *service;
1044         NTSTATUS status;
1045
1046         if (!torture_open_connection(&cli)) {
1047                 return False;
1048         }
1049         cli_sockopt(cli, sockops);
1050
1051         printf("starting tcon2 test\n");
1052
1053         asprintf(&service, "\\\\%s\\%s", host, share);
1054
1055         status = cli_raw_tcon(cli, service, password, "?????", &max_xmit, &cnum);
1056
1057         if (!NT_STATUS_IS_OK(status)) {
1058                 printf("tcon2 failed : %s\n", cli_errstr(cli));
1059         } else {
1060                 printf("tcon OK : max_xmit=%d cnum=%d tid=%d\n", 
1061                        (int)max_xmit, (int)cnum, SVAL(cli->inbuf, smb_tid));
1062         }
1063
1064         if (!torture_close_connection(cli)) {
1065                 return False;
1066         }
1067
1068         printf("Passed tcon2 test\n");
1069         return True;
1070 }
1071
1072 static BOOL tcon_devtest(struct cli_state *cli,
1073                          const char *myshare, const char *devtype,
1074                          const char *return_devtype,
1075                          NTSTATUS expected_error)
1076 {
1077         BOOL status;
1078         BOOL ret;
1079
1080         status = cli_send_tconX(cli, myshare, devtype,
1081                                 password, strlen(password)+1);
1082
1083         if (NT_STATUS_IS_OK(expected_error)) {
1084                 if (status) {
1085                         if (strcmp(cli->dev, return_devtype) == 0) {
1086                                 ret = True;
1087                         } else { 
1088                                 printf("tconX to share %s with type %s "
1089                                        "succeeded but returned the wrong "
1090                                        "device type (got [%s] but should have got [%s])\n",
1091                                        myshare, devtype, cli->dev, return_devtype);
1092                                 ret = False;
1093                         }
1094                 } else {
1095                         printf("tconX to share %s with type %s "
1096                                "should have succeeded but failed\n",
1097                                myshare, devtype);
1098                         ret = False;
1099                 }
1100                 cli_tdis(cli);
1101         } else {
1102                 if (status) {
1103                         printf("tconx to share %s with type %s "
1104                                "should have failed but succeeded\n",
1105                                myshare, devtype);
1106                         ret = False;
1107                 } else {
1108                         if (NT_STATUS_EQUAL(cli_nt_error(cli),
1109                                             expected_error)) {
1110                                 ret = True;
1111                         } else {
1112                                 printf("Returned unexpected error\n");
1113                                 ret = False;
1114                         }
1115                 }
1116         }
1117         return ret;
1118 }
1119
1120 /*
1121  checks for correct tconX support
1122  */
1123 static BOOL run_tcon_devtype_test(int dummy)
1124 {
1125         static struct cli_state *cli1 = NULL;
1126         BOOL retry;
1127         int flags = 0;
1128         NTSTATUS status;
1129         BOOL ret = True;
1130
1131         status = cli_full_connection(&cli1, myname,
1132                                      host, NULL, port_to_use,
1133                                      NULL, NULL,
1134                                      username, workgroup,
1135                                      password, flags, Undefined, &retry);
1136
1137         if (!NT_STATUS_IS_OK(status)) {
1138                 printf("could not open connection\n");
1139                 return False;
1140         }
1141
1142         if (!tcon_devtest(cli1, "IPC$", "A:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1143                 ret = False;
1144
1145         if (!tcon_devtest(cli1, "IPC$", "?????", "IPC", NT_STATUS_OK))
1146                 ret = False;
1147
1148         if (!tcon_devtest(cli1, "IPC$", "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1149                 ret = False;
1150
1151         if (!tcon_devtest(cli1, "IPC$", "IPC", "IPC", NT_STATUS_OK))
1152                 ret = False;
1153                         
1154         if (!tcon_devtest(cli1, "IPC$", "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1155                 ret = False;
1156
1157         if (!tcon_devtest(cli1, share, "A:", "A:", NT_STATUS_OK))
1158                 ret = False;
1159
1160         if (!tcon_devtest(cli1, share, "?????", "A:", NT_STATUS_OK))
1161                 ret = False;
1162
1163         if (!tcon_devtest(cli1, share, "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1164                 ret = False;
1165
1166         if (!tcon_devtest(cli1, share, "IPC", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1167                 ret = False;
1168                         
1169         if (!tcon_devtest(cli1, share, "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1170                 ret = False;
1171
1172         cli_shutdown(cli1);
1173
1174         if (ret)
1175                 printf("Passed tcondevtest\n");
1176
1177         return ret;
1178 }
1179
1180
1181 /*
1182   This test checks that 
1183
1184   1) the server supports multiple locking contexts on the one SMB
1185   connection, distinguished by PID.  
1186
1187   2) the server correctly fails overlapping locks made by the same PID (this
1188      goes against POSIX behaviour, which is why it is tricky to implement)
1189
1190   3) the server denies unlock requests by an incorrect client PID
1191 */
1192 static BOOL run_locktest2(int dummy)
1193 {
1194         static struct cli_state *cli;
1195         const char *fname = "\\lockt2.lck";
1196         int fnum1, fnum2, fnum3;
1197         BOOL correct = True;
1198
1199         if (!torture_open_connection(&cli)) {
1200                 return False;
1201         }
1202
1203         cli_sockopt(cli, sockops);
1204
1205         printf("starting locktest2\n");
1206
1207         cli_unlink(cli, fname);
1208
1209         cli_setpid(cli, 1);
1210
1211         fnum1 = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1212         if (fnum1 == -1) {
1213                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
1214                 return False;
1215         }
1216
1217         fnum2 = cli_open(cli, fname, O_RDWR, DENY_NONE);
1218         if (fnum2 == -1) {
1219                 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli));
1220                 return False;
1221         }
1222
1223         cli_setpid(cli, 2);
1224
1225         fnum3 = cli_open(cli, fname, O_RDWR, DENY_NONE);
1226         if (fnum3 == -1) {
1227                 printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli));
1228                 return False;
1229         }
1230
1231         cli_setpid(cli, 1);
1232
1233         if (!cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
1234                 printf("lock1 failed (%s)\n", cli_errstr(cli));
1235                 return False;
1236         }
1237
1238         if (cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
1239                 printf("WRITE lock1 succeeded! This is a locking bug\n");
1240                 correct = False;
1241         } else {
1242                 if (!check_error(__LINE__, cli, ERRDOS, ERRlock, 
1243                                  NT_STATUS_LOCK_NOT_GRANTED)) return False;
1244         }
1245
1246         if (cli_lock(cli, fnum2, 0, 4, 0, WRITE_LOCK)) {
1247                 printf("WRITE lock2 succeeded! This is a locking bug\n");
1248                 correct = False;
1249         } else {
1250                 if (!check_error(__LINE__, cli, ERRDOS, ERRlock, 
1251                                  NT_STATUS_LOCK_NOT_GRANTED)) return False;
1252         }
1253
1254         if (cli_lock(cli, fnum2, 0, 4, 0, READ_LOCK)) {
1255                 printf("READ lock2 succeeded! This is a locking bug\n");
1256                 correct = False;
1257         } else {
1258                 if (!check_error(__LINE__, cli, ERRDOS, ERRlock, 
1259                                  NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1260         }
1261
1262         if (!cli_lock(cli, fnum1, 100, 4, 0, WRITE_LOCK)) {
1263                 printf("lock at 100 failed (%s)\n", cli_errstr(cli));
1264         }
1265         cli_setpid(cli, 2);
1266         if (cli_unlock(cli, fnum1, 100, 4)) {
1267                 printf("unlock at 100 succeeded! This is a locking bug\n");
1268                 correct = False;
1269         }
1270
1271         if (cli_unlock(cli, fnum1, 0, 4)) {
1272                 printf("unlock1 succeeded! This is a locking bug\n");
1273                 correct = False;
1274         } else {
1275                 if (!check_error(__LINE__, cli, 
1276                                  ERRDOS, ERRlock, 
1277                                  NT_STATUS_RANGE_NOT_LOCKED)) return False;
1278         }
1279
1280         if (cli_unlock(cli, fnum1, 0, 8)) {
1281                 printf("unlock2 succeeded! This is a locking bug\n");
1282                 correct = False;
1283         } else {
1284                 if (!check_error(__LINE__, cli, 
1285                                  ERRDOS, ERRlock, 
1286                                  NT_STATUS_RANGE_NOT_LOCKED)) return False;
1287         }
1288
1289         if (cli_lock(cli, fnum3, 0, 4, 0, WRITE_LOCK)) {
1290                 printf("lock3 succeeded! This is a locking bug\n");
1291                 correct = False;
1292         } else {
1293                 if (!check_error(__LINE__, cli, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
1294         }
1295
1296         cli_setpid(cli, 1);
1297
1298         if (!cli_close(cli, fnum1)) {
1299                 printf("close1 failed (%s)\n", cli_errstr(cli));
1300                 return False;
1301         }
1302
1303         if (!cli_close(cli, fnum2)) {
1304                 printf("close2 failed (%s)\n", cli_errstr(cli));
1305                 return False;
1306         }
1307
1308         if (!cli_close(cli, fnum3)) {
1309                 printf("close3 failed (%s)\n", cli_errstr(cli));
1310                 return False;
1311         }
1312
1313         if (!torture_close_connection(cli)) {
1314                 correct = False;
1315         }
1316
1317         printf("locktest2 finished\n");
1318
1319         return correct;
1320 }
1321
1322
1323 /*
1324   This test checks that 
1325
1326   1) the server supports the full offset range in lock requests
1327 */
1328 static BOOL run_locktest3(int dummy)
1329 {
1330         static struct cli_state *cli1, *cli2;
1331         const char *fname = "\\lockt3.lck";
1332         int fnum1, fnum2, i;
1333         uint32 offset;
1334         BOOL correct = True;
1335
1336 #define NEXT_OFFSET offset += (~(uint32)0) / torture_numops
1337
1338         if (!torture_open_connection(&cli1) || !torture_open_connection(&cli2)) {
1339                 return False;
1340         }
1341         cli_sockopt(cli1, sockops);
1342         cli_sockopt(cli2, sockops);
1343
1344         printf("starting locktest3\n");
1345
1346         cli_unlink(cli1, fname);
1347
1348         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1349         if (fnum1 == -1) {
1350                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
1351                 return False;
1352         }
1353         fnum2 = cli_open(cli2, fname, O_RDWR, DENY_NONE);
1354         if (fnum2 == -1) {
1355                 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli2));
1356                 return False;
1357         }
1358
1359         for (offset=i=0;i<torture_numops;i++) {
1360                 NEXT_OFFSET;
1361                 if (!cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
1362                         printf("lock1 %d failed (%s)\n", 
1363                                i,
1364                                cli_errstr(cli1));
1365                         return False;
1366                 }
1367
1368                 if (!cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
1369                         printf("lock2 %d failed (%s)\n", 
1370                                i,
1371                                cli_errstr(cli1));
1372                         return False;
1373                 }
1374         }
1375
1376         for (offset=i=0;i<torture_numops;i++) {
1377                 NEXT_OFFSET;
1378
1379                 if (cli_lock(cli1, fnum1, offset-2, 1, 0, WRITE_LOCK)) {
1380                         printf("error: lock1 %d succeeded!\n", i);
1381                         return False;
1382                 }
1383
1384                 if (cli_lock(cli2, fnum2, offset-1, 1, 0, WRITE_LOCK)) {
1385                         printf("error: lock2 %d succeeded!\n", i);
1386                         return False;
1387                 }
1388
1389                 if (cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
1390                         printf("error: lock3 %d succeeded!\n", i);
1391                         return False;
1392                 }
1393
1394                 if (cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
1395                         printf("error: lock4 %d succeeded!\n", i);
1396                         return False;
1397                 }
1398         }
1399
1400         for (offset=i=0;i<torture_numops;i++) {
1401                 NEXT_OFFSET;
1402
1403                 if (!cli_unlock(cli1, fnum1, offset-1, 1)) {
1404                         printf("unlock1 %d failed (%s)\n", 
1405                                i,
1406                                cli_errstr(cli1));
1407                         return False;
1408                 }
1409
1410                 if (!cli_unlock(cli2, fnum2, offset-2, 1)) {
1411                         printf("unlock2 %d failed (%s)\n", 
1412                                i,
1413                                cli_errstr(cli1));
1414                         return False;
1415                 }
1416         }
1417
1418         if (!cli_close(cli1, fnum1)) {
1419                 printf("close1 failed (%s)\n", cli_errstr(cli1));
1420                 return False;
1421         }
1422
1423         if (!cli_close(cli2, fnum2)) {
1424                 printf("close2 failed (%s)\n", cli_errstr(cli2));
1425                 return False;
1426         }
1427
1428         if (!cli_unlink(cli1, fname)) {
1429                 printf("unlink failed (%s)\n", cli_errstr(cli1));
1430                 return False;
1431         }
1432
1433         if (!torture_close_connection(cli1)) {
1434                 correct = False;
1435         }
1436         
1437         if (!torture_close_connection(cli2)) {
1438                 correct = False;
1439         }
1440
1441         printf("finished locktest3\n");
1442
1443         return correct;
1444 }
1445
1446 #define EXPECTED(ret, v) if ((ret) != (v)) { \
1447         printf("** "); correct = False; \
1448         }
1449
1450 /*
1451   looks at overlapping locks
1452 */
1453 static BOOL run_locktest4(int dummy)
1454 {
1455         static struct cli_state *cli1, *cli2;
1456         const char *fname = "\\lockt4.lck";
1457         int fnum1, fnum2, f;
1458         BOOL ret;
1459         char buf[1000];
1460         BOOL correct = True;
1461
1462         if (!torture_open_connection(&cli1) || !torture_open_connection(&cli2)) {
1463                 return False;
1464         }
1465
1466         cli_sockopt(cli1, sockops);
1467         cli_sockopt(cli2, sockops);
1468
1469         printf("starting locktest4\n");
1470
1471         cli_unlink(cli1, fname);
1472
1473         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1474         fnum2 = cli_open(cli2, fname, O_RDWR, DENY_NONE);
1475
1476         memset(buf, 0, sizeof(buf));
1477
1478         if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1479                 printf("Failed to create file\n");
1480                 correct = False;
1481                 goto fail;
1482         }
1483
1484         ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
1485               cli_lock(cli1, fnum1, 2, 4, 0, WRITE_LOCK);
1486         EXPECTED(ret, False);
1487         printf("the same process %s set overlapping write locks\n", ret?"can":"cannot");
1488             
1489         ret = cli_lock(cli1, fnum1, 10, 4, 0, READ_LOCK) &&
1490               cli_lock(cli1, fnum1, 12, 4, 0, READ_LOCK);
1491         EXPECTED(ret, True);
1492         printf("the same process %s set overlapping read locks\n", ret?"can":"cannot");
1493
1494         ret = cli_lock(cli1, fnum1, 20, 4, 0, WRITE_LOCK) &&
1495               cli_lock(cli2, fnum2, 22, 4, 0, WRITE_LOCK);
1496         EXPECTED(ret, False);
1497         printf("a different connection %s set overlapping write locks\n", ret?"can":"cannot");
1498             
1499         ret = cli_lock(cli1, fnum1, 30, 4, 0, READ_LOCK) &&
1500               cli_lock(cli2, fnum2, 32, 4, 0, READ_LOCK);
1501         EXPECTED(ret, True);
1502         printf("a different connection %s set overlapping read locks\n", ret?"can":"cannot");
1503         
1504         ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 40, 4, 0, WRITE_LOCK)) &&
1505               (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 42, 4, 0, WRITE_LOCK));
1506         EXPECTED(ret, False);
1507         printf("a different pid %s set overlapping write locks\n", ret?"can":"cannot");
1508             
1509         ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 50, 4, 0, READ_LOCK)) &&
1510               (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 52, 4, 0, READ_LOCK));
1511         EXPECTED(ret, True);
1512         printf("a different pid %s set overlapping read locks\n", ret?"can":"cannot");
1513
1514         ret = cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK) &&
1515               cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK);
1516         EXPECTED(ret, True);
1517         printf("the same process %s set the same read lock twice\n", ret?"can":"cannot");
1518
1519         ret = cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK) &&
1520               cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK);
1521         EXPECTED(ret, False);
1522         printf("the same process %s set the same write lock twice\n", ret?"can":"cannot");
1523
1524         ret = cli_lock(cli1, fnum1, 80, 4, 0, READ_LOCK) &&
1525               cli_lock(cli1, fnum1, 80, 4, 0, WRITE_LOCK);
1526         EXPECTED(ret, False);
1527         printf("the same process %s overlay a read lock with a write lock\n", ret?"can":"cannot");
1528
1529         ret = cli_lock(cli1, fnum1, 90, 4, 0, WRITE_LOCK) &&
1530               cli_lock(cli1, fnum1, 90, 4, 0, READ_LOCK);
1531         EXPECTED(ret, True);
1532         printf("the same process %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1533
1534         ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 100, 4, 0, WRITE_LOCK)) &&
1535               (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 100, 4, 0, READ_LOCK));
1536         EXPECTED(ret, False);
1537         printf("a different pid %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1538
1539         ret = cli_lock(cli1, fnum1, 110, 4, 0, READ_LOCK) &&
1540               cli_lock(cli1, fnum1, 112, 4, 0, READ_LOCK) &&
1541               cli_unlock(cli1, fnum1, 110, 6);
1542         EXPECTED(ret, False);
1543         printf("the same process %s coalesce read locks\n", ret?"can":"cannot");
1544
1545
1546         ret = cli_lock(cli1, fnum1, 120, 4, 0, WRITE_LOCK) &&
1547               (cli_read(cli2, fnum2, buf, 120, 4) == 4);
1548         EXPECTED(ret, False);
1549         printf("this server %s strict write locking\n", ret?"doesn't do":"does");
1550
1551         ret = cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK) &&
1552               (cli_write(cli2, fnum2, 0, buf, 130, 4) == 4);
1553         EXPECTED(ret, False);
1554         printf("this server %s strict read locking\n", ret?"doesn't do":"does");
1555
1556
1557         ret = cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
1558               cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
1559               cli_unlock(cli1, fnum1, 140, 4) &&
1560               cli_unlock(cli1, fnum1, 140, 4);
1561         EXPECTED(ret, True);
1562         printf("this server %s do recursive read locking\n", ret?"does":"doesn't");
1563
1564
1565         ret = cli_lock(cli1, fnum1, 150, 4, 0, WRITE_LOCK) &&
1566               cli_lock(cli1, fnum1, 150, 4, 0, READ_LOCK) &&
1567               cli_unlock(cli1, fnum1, 150, 4) &&
1568               (cli_read(cli2, fnum2, buf, 150, 4) == 4) &&
1569               !(cli_write(cli2, fnum2, 0, buf, 150, 4) == 4) &&
1570               cli_unlock(cli1, fnum1, 150, 4);
1571         EXPECTED(ret, True);
1572         printf("this server %s do recursive lock overlays\n", ret?"does":"doesn't");
1573
1574         ret = cli_lock(cli1, fnum1, 160, 4, 0, READ_LOCK) &&
1575               cli_unlock(cli1, fnum1, 160, 4) &&
1576               (cli_write(cli2, fnum2, 0, buf, 160, 4) == 4) &&          
1577               (cli_read(cli2, fnum2, buf, 160, 4) == 4);                
1578         EXPECTED(ret, True);
1579         printf("the same process %s remove a read lock using write locking\n", ret?"can":"cannot");
1580
1581         ret = cli_lock(cli1, fnum1, 170, 4, 0, WRITE_LOCK) &&
1582               cli_unlock(cli1, fnum1, 170, 4) &&
1583               (cli_write(cli2, fnum2, 0, buf, 170, 4) == 4) &&          
1584               (cli_read(cli2, fnum2, buf, 170, 4) == 4);                
1585         EXPECTED(ret, True);
1586         printf("the same process %s remove a write lock using read locking\n", ret?"can":"cannot");
1587
1588         ret = cli_lock(cli1, fnum1, 190, 4, 0, WRITE_LOCK) &&
1589               cli_lock(cli1, fnum1, 190, 4, 0, READ_LOCK) &&
1590               cli_unlock(cli1, fnum1, 190, 4) &&
1591               !(cli_write(cli2, fnum2, 0, buf, 190, 4) == 4) &&         
1592               (cli_read(cli2, fnum2, buf, 190, 4) == 4);                
1593         EXPECTED(ret, True);
1594         printf("the same process %s remove the first lock first\n", ret?"does":"doesn't");
1595
1596         cli_close(cli1, fnum1);
1597         cli_close(cli2, fnum2);
1598         fnum1 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1599         f = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1600         ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
1601               cli_lock(cli1, f, 0, 1, 0, READ_LOCK) &&
1602               cli_close(cli1, fnum1) &&
1603               ((fnum1 = cli_open(cli1, fname, O_RDWR, DENY_NONE)) != -1) &&
1604               cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
1605         cli_close(cli1, f);
1606         cli_close(cli1, fnum1);
1607         EXPECTED(ret, True);
1608         printf("the server %s have the NT byte range lock bug\n", !ret?"does":"doesn't");
1609
1610  fail:
1611         cli_close(cli1, fnum1);
1612         cli_close(cli2, fnum2);
1613         cli_unlink(cli1, fname);
1614         torture_close_connection(cli1);
1615         torture_close_connection(cli2);
1616
1617         printf("finished locktest4\n");
1618         return correct;
1619 }
1620
1621 /*
1622   looks at lock upgrade/downgrade.
1623 */
1624 static BOOL run_locktest5(int dummy)
1625 {
1626         static struct cli_state *cli1, *cli2;
1627         const char *fname = "\\lockt5.lck";
1628         int fnum1, fnum2, fnum3;
1629         BOOL ret;
1630         char buf[1000];
1631         BOOL correct = True;
1632
1633         if (!torture_open_connection(&cli1) || !torture_open_connection(&cli2)) {
1634                 return False;
1635         }
1636
1637         cli_sockopt(cli1, sockops);
1638         cli_sockopt(cli2, sockops);
1639
1640         printf("starting locktest5\n");
1641
1642         cli_unlink(cli1, fname);
1643
1644         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1645         fnum2 = cli_open(cli2, fname, O_RDWR, DENY_NONE);
1646         fnum3 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1647
1648         memset(buf, 0, sizeof(buf));
1649
1650         if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1651                 printf("Failed to create file\n");
1652                 correct = False;
1653                 goto fail;
1654         }
1655
1656         /* Check for NT bug... */
1657         ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
1658                   cli_lock(cli1, fnum3, 0, 1, 0, READ_LOCK);
1659         cli_close(cli1, fnum1);
1660         fnum1 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1661         ret = cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
1662         EXPECTED(ret, True);
1663         printf("this server %s the NT locking bug\n", ret ? "doesn't have" : "has");
1664         cli_close(cli1, fnum1);
1665         fnum1 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1666         cli_unlock(cli1, fnum3, 0, 1);
1667
1668         ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
1669               cli_lock(cli1, fnum1, 1, 1, 0, READ_LOCK);
1670         EXPECTED(ret, True);
1671         printf("the same process %s overlay a write with a read lock\n", ret?"can":"cannot");
1672
1673         ret = cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
1674         EXPECTED(ret, False);
1675
1676         printf("a different processs %s get a read lock on the first process lock stack\n", ret?"can":"cannot");
1677
1678         /* Unlock the process 2 lock. */
1679         cli_unlock(cli2, fnum2, 0, 4);
1680
1681         ret = cli_lock(cli1, fnum3, 0, 4, 0, READ_LOCK);
1682         EXPECTED(ret, False);
1683
1684         printf("the same processs on a different fnum %s get a read lock\n", ret?"can":"cannot");
1685
1686         /* Unlock the process 1 fnum3 lock. */
1687         cli_unlock(cli1, fnum3, 0, 4);
1688
1689         /* Stack 2 more locks here. */
1690         ret = cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK) &&
1691                   cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK);
1692
1693         EXPECTED(ret, True);
1694         printf("the same process %s stack read locks\n", ret?"can":"cannot");
1695
1696         /* Unlock the first process lock, then check this was the WRITE lock that was
1697                 removed. */
1698
1699         ret = cli_unlock(cli1, fnum1, 0, 4) &&
1700                         cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
1701
1702         EXPECTED(ret, True);
1703         printf("the first unlock removes the %s lock\n", ret?"WRITE":"READ");
1704
1705         /* Unlock the process 2 lock. */
1706         cli_unlock(cli2, fnum2, 0, 4);
1707
1708         /* We should have 3 stacked locks here. Ensure we need to do 3 unlocks. */
1709
1710         ret = cli_unlock(cli1, fnum1, 1, 1) &&
1711                   cli_unlock(cli1, fnum1, 0, 4) &&
1712                   cli_unlock(cli1, fnum1, 0, 4);
1713
1714         EXPECTED(ret, True);
1715         printf("the same process %s unlock the stack of 4 locks\n", ret?"can":"cannot"); 
1716
1717         /* Ensure the next unlock fails. */
1718         ret = cli_unlock(cli1, fnum1, 0, 4);
1719         EXPECTED(ret, False);
1720         printf("the same process %s count the lock stack\n", !ret?"can":"cannot"); 
1721
1722         /* Ensure connection 2 can get a write lock. */
1723         ret = cli_lock(cli2, fnum2, 0, 4, 0, WRITE_LOCK);
1724         EXPECTED(ret, True);
1725
1726         printf("a different processs %s get a write lock on the unlocked stack\n", ret?"can":"cannot");
1727
1728
1729  fail:
1730         cli_close(cli1, fnum1);
1731         cli_close(cli2, fnum2);
1732         cli_unlink(cli1, fname);
1733         if (!torture_close_connection(cli1)) {
1734                 correct = False;
1735         }
1736         if (!torture_close_connection(cli2)) {
1737                 correct = False;
1738         }
1739
1740         printf("finished locktest5\n");
1741        
1742         return correct;
1743 }
1744
1745 /*
1746   tries the unusual lockingX locktype bits
1747 */
1748 static BOOL run_locktest6(int dummy)
1749 {
1750         static struct cli_state *cli;
1751         const char *fname[1] = { "\\lock6.txt" };
1752         int i;
1753         int fnum;
1754         NTSTATUS status;
1755
1756         if (!torture_open_connection(&cli)) {
1757                 return False;
1758         }
1759
1760         cli_sockopt(cli, sockops);
1761
1762         printf("starting locktest6\n");
1763
1764         for (i=0;i<1;i++) {
1765                 printf("Testing %s\n", fname[i]);
1766
1767                 cli_unlink(cli, fname[i]);
1768
1769                 fnum = cli_open(cli, fname[i], O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1770                 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CHANGE_LOCKTYPE);
1771                 cli_close(cli, fnum);
1772                 printf("CHANGE_LOCKTYPE gave %s\n", nt_errstr(status));
1773
1774                 fnum = cli_open(cli, fname[i], O_RDWR, DENY_NONE);
1775                 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CANCEL_LOCK);
1776                 cli_close(cli, fnum);
1777                 printf("CANCEL_LOCK gave %s\n", nt_errstr(status));
1778
1779                 cli_unlink(cli, fname[i]);
1780         }
1781
1782         torture_close_connection(cli);
1783
1784         printf("finished locktest6\n");
1785         return True;
1786 }
1787
1788 static BOOL run_locktest7(int dummy)
1789 {
1790         struct cli_state *cli1;
1791         const char *fname = "\\lockt7.lck";
1792         int fnum1;
1793         char buf[200];
1794         BOOL correct = False;
1795
1796         if (!torture_open_connection(&cli1)) {
1797                 return False;
1798         }
1799
1800         cli_sockopt(cli1, sockops);
1801
1802         printf("starting locktest7\n");
1803
1804         cli_unlink(cli1, fname);
1805
1806         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1807
1808         memset(buf, 0, sizeof(buf));
1809
1810         if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1811                 printf("Failed to create file\n");
1812                 goto fail;
1813         }
1814
1815         cli_setpid(cli1, 1);
1816
1817         if (!cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK)) {
1818                 printf("Unable to apply read lock on range 130:4, error was %s\n", cli_errstr(cli1));
1819                 goto fail;
1820         } else {
1821                 printf("pid1 successfully locked range 130:4 for READ\n");
1822         }
1823
1824         if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
1825                 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
1826                 goto fail;
1827         } else {
1828                 printf("pid1 successfully read the range 130:4\n");
1829         }
1830
1831         if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
1832                 printf("pid1 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
1833                 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
1834                         printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
1835                         goto fail;
1836                 }
1837         } else {
1838                 printf("pid1 successfully wrote to the range 130:4 (should be denied)\n");
1839                 goto fail;
1840         }
1841
1842         cli_setpid(cli1, 2);
1843
1844         if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
1845                 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
1846         } else {
1847                 printf("pid2 successfully read the range 130:4\n");
1848         }
1849
1850         if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
1851                 printf("pid2 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
1852                 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
1853                         printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
1854                         goto fail;
1855                 }
1856         } else {
1857                 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
1858                 goto fail;
1859         }
1860
1861         cli_setpid(cli1, 1);
1862         cli_unlock(cli1, fnum1, 130, 4);
1863
1864         if (!cli_lock(cli1, fnum1, 130, 4, 0, WRITE_LOCK)) {
1865                 printf("Unable to apply write lock on range 130:4, error was %s\n", cli_errstr(cli1));
1866                 goto fail;
1867         } else {
1868                 printf("pid1 successfully locked range 130:4 for WRITE\n");
1869         }
1870
1871         if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
1872                 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
1873                 goto fail;
1874         } else {
1875                 printf("pid1 successfully read the range 130:4\n");
1876         }
1877
1878         if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
1879                 printf("pid1 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
1880                 goto fail;
1881         } else {
1882                 printf("pid1 successfully wrote to the range 130:4\n");
1883         }
1884
1885         cli_setpid(cli1, 2);
1886
1887         if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
1888                 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
1889                 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
1890                         printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
1891                         goto fail;
1892                 }
1893         } else {
1894                 printf("pid2 successfully read the range 130:4 (should be denied)\n");
1895                 goto fail;
1896         }
1897
1898         if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
1899                 printf("pid2 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
1900                 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
1901                         printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
1902                         goto fail;
1903                 }
1904         } else {
1905                 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
1906                 goto fail;
1907         }
1908
1909         cli_unlock(cli1, fnum1, 130, 0);
1910         correct = True;
1911
1912 fail:
1913         cli_close(cli1, fnum1);
1914         cli_unlink(cli1, fname);
1915         torture_close_connection(cli1);
1916
1917         printf("finished locktest7\n");
1918         return correct;
1919 }
1920
1921 /*
1922 test whether fnums and tids open on one VC are available on another (a major
1923 security hole)
1924 */
1925 static BOOL run_fdpasstest(int dummy)
1926 {
1927         struct cli_state *cli1, *cli2;
1928         const char *fname = "\\fdpass.tst";
1929         int fnum1;
1930         pstring buf;
1931
1932         if (!torture_open_connection(&cli1) || !torture_open_connection(&cli2)) {
1933                 return False;
1934         }
1935         cli_sockopt(cli1, sockops);
1936         cli_sockopt(cli2, sockops);
1937
1938         printf("starting fdpasstest\n");
1939
1940         cli_unlink(cli1, fname);
1941
1942         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1943         if (fnum1 == -1) {
1944                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
1945                 return False;
1946         }
1947
1948         if (cli_write(cli1, fnum1, 0, "hello world\n", 0, 13) != 13) {
1949                 printf("write failed (%s)\n", cli_errstr(cli1));
1950                 return False;
1951         }
1952
1953         cli2->vuid = cli1->vuid;
1954         cli2->cnum = cli1->cnum;
1955         cli2->pid = cli1->pid;
1956
1957         if (cli_read(cli2, fnum1, buf, 0, 13) == 13) {
1958                 printf("read succeeded! nasty security hole [%s]\n",
1959                        buf);
1960                 return False;
1961         }
1962
1963         cli_close(cli1, fnum1);
1964         cli_unlink(cli1, fname);
1965
1966         torture_close_connection(cli1);
1967         torture_close_connection(cli2);
1968
1969         printf("finished fdpasstest\n");
1970         return True;
1971 }
1972
1973 static BOOL run_fdsesstest(int dummy)
1974 {
1975         struct cli_state *cli;
1976         uint16 new_vuid;
1977         uint16 saved_vuid;
1978         uint16 new_cnum;
1979         uint16 saved_cnum;
1980         const char *fname = "\\fdsess.tst";
1981         const char *fname1 = "\\fdsess1.tst";
1982         int fnum1;
1983         int fnum2;
1984         pstring buf;
1985         BOOL ret = True;
1986
1987         if (!torture_open_connection(&cli))
1988                 return False;
1989         cli_sockopt(cli, sockops);
1990
1991         if (!torture_cli_session_setup2(cli, &new_vuid))
1992                 return False;
1993
1994         saved_cnum = cli->cnum;
1995         if (!cli_send_tconX(cli, share, "?????", "", 1))
1996                 return False;
1997         new_cnum = cli->cnum;
1998         cli->cnum = saved_cnum;
1999
2000         printf("starting fdsesstest\n");
2001
2002         cli_unlink(cli, fname);
2003         cli_unlink(cli, fname1);
2004
2005         fnum1 = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2006         if (fnum1 == -1) {
2007                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2008                 return False;
2009         }
2010
2011         if (cli_write(cli, fnum1, 0, "hello world\n", 0, 13) != 13) {
2012                 printf("write failed (%s)\n", cli_errstr(cli));
2013                 return False;
2014         }
2015
2016         saved_vuid = cli->vuid;
2017         cli->vuid = new_vuid;
2018
2019         if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2020                 printf("read succeeded with different vuid! nasty security hole [%s]\n",
2021                        buf);
2022                 ret = False;
2023         }
2024         /* Try to open a file with different vuid, samba cnum. */
2025         fnum2 = cli_open(cli, fname1, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2026         if (fnum2 != -1) {
2027                 printf("create with different vuid, same cnum succeeded.\n");
2028                 cli_close(cli, fnum2);
2029                 cli_unlink(cli, fname1);
2030         } else {
2031                 printf("create with different vuid, same cnum failed.\n");
2032                 printf("This will cause problems with service clients.\n");
2033                 ret = False;
2034         }
2035
2036         cli->vuid = saved_vuid;
2037
2038         /* Try with same vuid, different cnum. */
2039         cli->cnum = new_cnum;
2040
2041         if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2042                 printf("read succeeded with different cnum![%s]\n",
2043                        buf);
2044                 ret = False;
2045         }
2046
2047         cli->cnum = saved_cnum;
2048         cli_close(cli, fnum1);
2049         cli_unlink(cli, fname);
2050
2051         torture_close_connection(cli);
2052
2053         printf("finished fdsesstest\n");
2054         return ret;
2055 }
2056
2057 /*
2058   This test checks that 
2059
2060   1) the server does not allow an unlink on a file that is open
2061 */
2062 static BOOL run_unlinktest(int dummy)
2063 {
2064         struct cli_state *cli;
2065         const char *fname = "\\unlink.tst";
2066         int fnum;
2067         BOOL correct = True;
2068
2069         if (!torture_open_connection(&cli)) {
2070                 return False;
2071         }
2072
2073         cli_sockopt(cli, sockops);
2074
2075         printf("starting unlink test\n");
2076
2077         cli_unlink(cli, fname);
2078
2079         cli_setpid(cli, 1);
2080
2081         fnum = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2082         if (fnum == -1) {
2083                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2084                 return False;
2085         }
2086
2087         if (cli_unlink(cli, fname)) {
2088                 printf("error: server allowed unlink on an open file\n");
2089                 correct = False;
2090         } else {
2091                 correct = check_error(__LINE__, cli, ERRDOS, ERRbadshare, 
2092                                       NT_STATUS_SHARING_VIOLATION);
2093         }
2094
2095         cli_close(cli, fnum);
2096         cli_unlink(cli, fname);
2097
2098         if (!torture_close_connection(cli)) {
2099                 correct = False;
2100         }
2101
2102         printf("unlink test finished\n");
2103         
2104         return correct;
2105 }
2106
2107
2108 /*
2109 test how many open files this server supports on the one socket
2110 */
2111 static BOOL run_maxfidtest(int dummy)
2112 {
2113         struct cli_state *cli;
2114         const char *template = "\\maxfid.%d.%d";
2115         fstring fname;
2116         int fnums[0x11000], i;
2117         int retries=4;
2118         BOOL correct = True;
2119
2120         cli = current_cli;
2121
2122         if (retries <= 0) {
2123                 printf("failed to connect\n");
2124                 return False;
2125         }
2126
2127         cli_sockopt(cli, sockops);
2128
2129         for (i=0; i<0x11000; i++) {
2130                 slprintf(fname,sizeof(fname)-1,template, i,(int)getpid());
2131                 if ((fnums[i] = cli_open(cli, fname, 
2132                                         O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) ==
2133                     -1) {
2134                         printf("open of %s failed (%s)\n", 
2135                                fname, cli_errstr(cli));
2136                         printf("maximum fnum is %d\n", i);
2137                         break;
2138                 }
2139                 printf("%6d\r", i);
2140         }
2141         printf("%6d\n", i);
2142         i--;
2143
2144         printf("cleaning up\n");
2145         for (;i>=0;i--) {
2146                 slprintf(fname,sizeof(fname)-1,template, i,(int)getpid());
2147                 cli_close(cli, fnums[i]);
2148                 if (!cli_unlink(cli, fname)) {
2149                         printf("unlink of %s failed (%s)\n", 
2150                                fname, cli_errstr(cli));
2151                         correct = False;
2152                 }
2153                 printf("%6d\r", i);
2154         }
2155         printf("%6d\n", 0);
2156
2157         printf("maxfid test finished\n");
2158         if (!torture_close_connection(cli)) {
2159                 correct = False;
2160         }
2161         return correct;
2162 }
2163
2164 /* generate a random buffer */
2165 static void rand_buf(char *buf, int len)
2166 {
2167         while (len--) {
2168                 *buf = (char)sys_random();
2169                 buf++;
2170         }
2171 }
2172
2173 /* send smb negprot commands, not reading the response */
2174 static BOOL run_negprot_nowait(int dummy)
2175 {
2176         int i;
2177         static struct cli_state cli;
2178         BOOL correct = True;
2179
2180         printf("starting negprot nowait test\n");
2181
2182         if (!open_nbt_connection(&cli)) {
2183                 return False;
2184         }
2185
2186         for (i=0;i<50000;i++) {
2187                 cli_negprot_send(&cli);
2188         }
2189
2190         if (!torture_close_connection(&cli)) {
2191                 correct = False;
2192         }
2193
2194         printf("finished negprot nowait test\n");
2195
2196         return correct;
2197 }
2198
2199
2200 /* send random IPC commands */
2201 static BOOL run_randomipc(int dummy)
2202 {
2203         char *rparam = NULL;
2204         char *rdata = NULL;
2205         int rdrcnt,rprcnt;
2206         pstring param;
2207         int api, param_len, i;
2208         struct cli_state *cli;
2209         BOOL correct = True;
2210         int count = 50000;
2211
2212         printf("starting random ipc test\n");
2213
2214         if (!torture_open_connection(&cli)) {
2215                 return False;
2216         }
2217
2218         for (i=0;i<count;i++) {
2219                 api = sys_random() % 500;
2220                 param_len = (sys_random() % 64);
2221
2222                 rand_buf(param, param_len);
2223   
2224                 SSVAL(param,0,api); 
2225
2226                 cli_api(cli, 
2227                         param, param_len, 8,  
2228                         NULL, 0, BUFFER_SIZE, 
2229                         &rparam, &rprcnt,     
2230                         &rdata, &rdrcnt);
2231                 if (i % 100 == 0) {
2232                         printf("%d/%d\r", i,count);
2233                 }
2234         }
2235         printf("%d/%d\n", i, count);
2236
2237         if (!torture_close_connection(cli)) {
2238                 correct = False;
2239         }
2240
2241         printf("finished random ipc test\n");
2242
2243         return correct;
2244 }
2245
2246
2247
2248 static void browse_callback(const char *sname, uint32 stype, 
2249                             const char *comment, void *state)
2250 {
2251         printf("\t%20.20s %08x %s\n", sname, stype, comment);
2252 }
2253
2254
2255
2256 /*
2257   This test checks the browse list code
2258
2259 */
2260 static BOOL run_browsetest(int dummy)
2261 {
2262         static struct cli_state *cli;
2263         BOOL correct = True;
2264
2265         printf("starting browse test\n");
2266
2267         if (!torture_open_connection(&cli)) {
2268                 return False;
2269         }
2270
2271         printf("domain list:\n");
2272         cli_NetServerEnum(cli, cli->server_domain, 
2273                           SV_TYPE_DOMAIN_ENUM,
2274                           browse_callback, NULL);
2275
2276         printf("machine list:\n");
2277         cli_NetServerEnum(cli, cli->server_domain, 
2278                           SV_TYPE_ALL,
2279                           browse_callback, NULL);
2280
2281         if (!torture_close_connection(cli)) {
2282                 correct = False;
2283         }
2284
2285         printf("browse test finished\n");
2286
2287         return correct;
2288
2289 }
2290
2291
2292 /*
2293   This checks how the getatr calls works
2294 */
2295 static BOOL run_attrtest(int dummy)
2296 {
2297         struct cli_state *cli;
2298         int fnum;
2299         time_t t, t2;
2300         const char *fname = "\\attrib123456789.tst";
2301         BOOL correct = True;
2302
2303         printf("starting attrib test\n");
2304
2305         if (!torture_open_connection(&cli)) {
2306                 return False;
2307         }
2308
2309         cli_unlink(cli, fname);
2310         fnum = cli_open(cli, fname, 
2311                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2312         cli_close(cli, fnum);
2313         if (!cli_getatr(cli, fname, NULL, NULL, &t)) {
2314                 printf("getatr failed (%s)\n", cli_errstr(cli));
2315                 correct = False;
2316         }
2317
2318         if (abs(t - time(NULL)) > 60*60*24*10) {
2319                 printf("ERROR: SMBgetatr bug. time is %s",
2320                        ctime(&t));
2321                 t = time(NULL);
2322                 correct = True;
2323         }
2324
2325         t2 = t-60*60*24; /* 1 day ago */
2326
2327         if (!cli_setatr(cli, fname, 0, t2)) {
2328                 printf("setatr failed (%s)\n", cli_errstr(cli));
2329                 correct = True;
2330         }
2331
2332         if (!cli_getatr(cli, fname, NULL, NULL, &t)) {
2333                 printf("getatr failed (%s)\n", cli_errstr(cli));
2334                 correct = True;
2335         }
2336
2337         if (t != t2) {
2338                 printf("ERROR: getatr/setatr bug. times are\n%s",
2339                        ctime(&t));
2340                 printf("%s", ctime(&t2));
2341                 correct = True;
2342         }
2343
2344         cli_unlink(cli, fname);
2345
2346         if (!torture_close_connection(cli)) {
2347                 correct = False;
2348         }
2349
2350         printf("attrib test finished\n");
2351
2352         return correct;
2353 }
2354
2355
2356 /*
2357   This checks a couple of trans2 calls
2358 */
2359 static BOOL run_trans2test(int dummy)
2360 {
2361         struct cli_state *cli;
2362         int fnum;
2363         size_t size;
2364         time_t c_time, a_time, m_time, w_time, m_time2;
2365         const char *fname = "\\trans2.tst";
2366         const char *dname = "\\trans2";
2367         const char *fname2 = "\\trans2\\trans2.tst";
2368         pstring pname;
2369         BOOL correct = True;
2370
2371         printf("starting trans2 test\n");
2372
2373         if (!torture_open_connection(&cli)) {
2374                 return False;
2375         }
2376
2377         cli_unlink(cli, fname);
2378         fnum = cli_open(cli, fname, 
2379                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2380         if (!cli_qfileinfo(cli, fnum, NULL, &size, &c_time, &a_time, &m_time,
2381                            NULL, NULL)) {
2382                 printf("ERROR: qfileinfo failed (%s)\n", cli_errstr(cli));
2383                 correct = False;
2384         }
2385
2386         if (!cli_qfilename(cli, fnum, pname)) {
2387                 printf("ERROR: qfilename failed (%s)\n", cli_errstr(cli));
2388                 correct = False;
2389         }
2390
2391         if (strcmp(pname, fname)) {
2392                 printf("qfilename gave different name? [%s] [%s]\n",
2393                        fname, pname);
2394                 correct = False;
2395         }
2396
2397         cli_close(cli, fnum);
2398
2399         sleep(2);
2400
2401         cli_unlink(cli, fname);
2402         fnum = cli_open(cli, fname, 
2403                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2404         if (fnum == -1) {
2405                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2406                 return False;
2407         }
2408         cli_close(cli, fnum);
2409
2410         if (!cli_qpathinfo(cli, fname, &c_time, &a_time, &m_time, &size, NULL)) {
2411                 printf("ERROR: qpathinfo failed (%s)\n", cli_errstr(cli));
2412                 correct = False;
2413         } else {
2414                 if (c_time != m_time) {
2415                         printf("create time=%s", ctime(&c_time));
2416                         printf("modify time=%s", ctime(&m_time));
2417                         printf("This system appears to have sticky create times\n");
2418                 }
2419                 if (a_time % (60*60) == 0) {
2420                         printf("access time=%s", ctime(&a_time));
2421                         printf("This system appears to set a midnight access time\n");
2422                         correct = False;
2423                 }
2424
2425                 if (abs(m_time - time(NULL)) > 60*60*24*7) {
2426                         printf("ERROR: totally incorrect times - maybe word reversed? mtime=%s", ctime(&m_time));
2427                         correct = False;
2428                 }
2429         }
2430
2431
2432         cli_unlink(cli, fname);
2433         fnum = cli_open(cli, fname, 
2434                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2435         cli_close(cli, fnum);
2436         if (!cli_qpathinfo2(cli, fname, &c_time, &a_time, &m_time, 
2437                             &w_time, &size, NULL, NULL)) {
2438                 printf("ERROR: qpathinfo2 failed (%s)\n", cli_errstr(cli));
2439                 correct = False;
2440         } else {
2441                 if (w_time < 60*60*24*2) {
2442                         printf("write time=%s", ctime(&w_time));
2443                         printf("This system appears to set a initial 0 write time\n");
2444                         correct = False;
2445                 }
2446         }
2447
2448         cli_unlink(cli, fname);
2449
2450
2451         /* check if the server updates the directory modification time
2452            when creating a new file */
2453         if (!cli_mkdir(cli, dname)) {
2454                 printf("ERROR: mkdir failed (%s)\n", cli_errstr(cli));
2455                 correct = False;
2456         }
2457         sleep(3);
2458         if (!cli_qpathinfo2(cli, "\\trans2\\", &c_time, &a_time, &m_time, 
2459                             &w_time, &size, NULL, NULL)) {
2460                 printf("ERROR: qpathinfo2 failed (%s)\n", cli_errstr(cli));
2461                 correct = False;
2462         }
2463
2464         fnum = cli_open(cli, fname2, 
2465                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2466         cli_write(cli, fnum,  0, (char *)&fnum, 0, sizeof(fnum));
2467         cli_close(cli, fnum);
2468         if (!cli_qpathinfo2(cli, "\\trans2\\", &c_time, &a_time, &m_time2, 
2469                             &w_time, &size, NULL, NULL)) {
2470                 printf("ERROR: qpathinfo2 failed (%s)\n", cli_errstr(cli));
2471                 correct = False;
2472         } else {
2473                 if (m_time2 == m_time) {
2474                         printf("This system does not update directory modification times\n");
2475                         correct = False;
2476                 }
2477         }
2478         cli_unlink(cli, fname2);
2479         cli_rmdir(cli, dname);
2480
2481         if (!torture_close_connection(cli)) {
2482                 correct = False;
2483         }
2484
2485         printf("trans2 test finished\n");
2486
2487         return correct;
2488 }
2489
2490 /*
2491   This checks new W2K calls.
2492 */
2493
2494 static BOOL new_trans(struct cli_state *pcli, int fnum, int level)
2495 {
2496         char *buf = NULL;
2497         uint32 len;
2498         BOOL correct = True;
2499
2500         if (!cli_qfileinfo_test(pcli, fnum, level, &buf, &len)) {
2501                 printf("ERROR: qfileinfo (%d) failed (%s)\n", level, cli_errstr(pcli));
2502                 correct = False;
2503         } else {
2504                 printf("qfileinfo: level %d, len = %u\n", level, len);
2505                 dump_data(0, buf, len);
2506                 printf("\n");
2507         }
2508         SAFE_FREE(buf);
2509         return correct;
2510 }
2511
2512 static BOOL run_w2ktest(int dummy)
2513 {
2514         struct cli_state *cli;
2515         int fnum;
2516         const char *fname = "\\w2ktest\\w2k.tst";
2517         int level;
2518         BOOL correct = True;
2519
2520         printf("starting w2k test\n");
2521
2522         if (!torture_open_connection(&cli)) {
2523                 return False;
2524         }
2525
2526         fnum = cli_open(cli, fname, 
2527                         O_RDWR | O_CREAT , DENY_NONE);
2528
2529         for (level = 1004; level < 1040; level++) {
2530                 new_trans(cli, fnum, level);
2531         }
2532
2533         cli_close(cli, fnum);
2534
2535         if (!torture_close_connection(cli)) {
2536                 correct = False;
2537         }
2538
2539         printf("w2k test finished\n");
2540         
2541         return correct;
2542 }
2543
2544
2545 /*
2546   this is a harness for some oplock tests
2547  */
2548 static BOOL run_oplock1(int dummy)
2549 {
2550         struct cli_state *cli1;
2551         const char *fname = "\\lockt1.lck";
2552         int fnum1;
2553         BOOL correct = True;
2554
2555         printf("starting oplock test 1\n");
2556
2557         if (!torture_open_connection(&cli1)) {
2558                 return False;
2559         }
2560
2561         cli_unlink(cli1, fname);
2562
2563         cli_sockopt(cli1, sockops);
2564
2565         cli1->use_oplocks = True;
2566
2567         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2568         if (fnum1 == -1) {
2569                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
2570                 return False;
2571         }
2572
2573         cli1->use_oplocks = False;
2574
2575         cli_unlink(cli1, fname);
2576         cli_unlink(cli1, fname);
2577
2578         if (!cli_close(cli1, fnum1)) {
2579                 printf("close2 failed (%s)\n", cli_errstr(cli1));
2580                 return False;
2581         }
2582
2583         if (!cli_unlink(cli1, fname)) {
2584                 printf("unlink failed (%s)\n", cli_errstr(cli1));
2585                 return False;
2586         }
2587
2588         if (!torture_close_connection(cli1)) {
2589                 correct = False;
2590         }
2591
2592         printf("finished oplock test 1\n");
2593
2594         return correct;
2595 }
2596
2597 static BOOL run_oplock2(int dummy)
2598 {
2599         struct cli_state *cli1, *cli2;
2600         const char *fname = "\\lockt2.lck";
2601         int fnum1, fnum2;
2602         int saved_use_oplocks = use_oplocks;
2603         char buf[4];
2604         BOOL correct = True;
2605         volatile BOOL *shared_correct;
2606
2607         shared_correct = (volatile BOOL *)shm_setup(sizeof(BOOL));
2608         *shared_correct = True;
2609
2610         use_level_II_oplocks = True;
2611         use_oplocks = True;
2612
2613         printf("starting oplock test 2\n");
2614
2615         if (!torture_open_connection(&cli1)) {
2616                 use_level_II_oplocks = False;
2617                 use_oplocks = saved_use_oplocks;
2618                 return False;
2619         }
2620
2621         cli1->use_oplocks = True;
2622         cli1->use_level_II_oplocks = True;
2623
2624         if (!torture_open_connection(&cli2)) {
2625                 use_level_II_oplocks = False;
2626                 use_oplocks = saved_use_oplocks;
2627                 return False;
2628         }
2629
2630         cli2->use_oplocks = True;
2631         cli2->use_level_II_oplocks = True;
2632
2633         cli_unlink(cli1, fname);
2634
2635         cli_sockopt(cli1, sockops);
2636         cli_sockopt(cli2, sockops);
2637
2638         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2639         if (fnum1 == -1) {
2640                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
2641                 return False;
2642         }
2643
2644         /* Don't need the globals any more. */
2645         use_level_II_oplocks = False;
2646         use_oplocks = saved_use_oplocks;
2647
2648         if (fork() == 0) {
2649                 /* Child code */
2650                 fnum2 = cli_open(cli2, fname, O_RDWR, DENY_NONE);
2651                 if (fnum2 == -1) {
2652                         printf("second open of %s failed (%s)\n", fname, cli_errstr(cli1));
2653                         *shared_correct = False;
2654                         exit(0);
2655                 }
2656
2657                 sleep(2);
2658
2659                 if (!cli_close(cli2, fnum2)) {
2660                         printf("close2 failed (%s)\n", cli_errstr(cli1));
2661                         *shared_correct = False;
2662                 }
2663
2664                 exit(0);
2665         }
2666
2667         sleep(2);
2668
2669         /* Ensure cli1 processes the break. */
2670
2671         if (cli_read(cli1, fnum1, buf, 0, 4) != 4) {
2672                 printf("read on fnum1 failed (%s)\n", cli_errstr(cli1));
2673                 correct = False;
2674         }
2675
2676         /* Should now be at level II. */
2677         /* Test if sending a write locks causes a break to none. */
2678
2679         if (!cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK)) {
2680                 printf("lock failed (%s)\n", cli_errstr(cli1));
2681                 correct = False;
2682         }
2683
2684         cli_unlock(cli1, fnum1, 0, 4);
2685
2686         sleep(2);
2687
2688         if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
2689                 printf("lock failed (%s)\n", cli_errstr(cli1));
2690                 correct = False;
2691         }
2692
2693         cli_unlock(cli1, fnum1, 0, 4);
2694
2695         sleep(2);
2696
2697         cli_read(cli1, fnum1, buf, 0, 4);
2698
2699 #if 0
2700         if (cli_write(cli1, fnum1, 0, buf, 0, 4) != 4) {
2701                 printf("write on fnum1 failed (%s)\n", cli_errstr(cli1));
2702                 correct = False;
2703         }
2704 #endif
2705
2706         if (!cli_close(cli1, fnum1)) {
2707                 printf("close1 failed (%s)\n", cli_errstr(cli1));
2708                 correct = False;
2709         }
2710
2711         sleep(4);
2712
2713         if (!cli_unlink(cli1, fname)) {
2714                 printf("unlink failed (%s)\n", cli_errstr(cli1));
2715                 correct = False;
2716         }
2717
2718         if (!torture_close_connection(cli1)) {
2719                 correct = False;
2720         }
2721
2722         if (!*shared_correct) {
2723                 correct = False;
2724         }
2725
2726         printf("finished oplock test 2\n");
2727
2728         return correct;
2729 }
2730
2731 /* handler for oplock 3 tests */
2732 static BOOL oplock3_handler(struct cli_state *cli, int fnum, unsigned char level)
2733 {
2734         printf("got oplock break fnum=%d level=%d\n",
2735                fnum, level);
2736         return cli_oplock_ack(cli, fnum, level);
2737 }
2738
2739 static BOOL run_oplock3(int dummy)
2740 {
2741         struct cli_state *cli;
2742         const char *fname = "\\oplockt3.dat";
2743         int fnum;
2744         char buf[4] = "abcd";
2745         BOOL correct = True;
2746         volatile BOOL *shared_correct;
2747
2748         shared_correct = (volatile BOOL *)shm_setup(sizeof(BOOL));
2749         *shared_correct = True;
2750
2751         printf("starting oplock test 3\n");
2752
2753         if (fork() == 0) {
2754                 /* Child code */
2755                 use_oplocks = True;
2756                 use_level_II_oplocks = True;
2757                 if (!torture_open_connection(&cli)) {
2758                         *shared_correct = False;
2759                         exit(0);
2760                 } 
2761                 sleep(2);
2762                 /* try to trigger a oplock break in parent */
2763                 fnum = cli_open(cli, fname, O_RDWR, DENY_NONE);
2764                 cli_write(cli, fnum, 0, buf, 0, 4);
2765                 exit(0);
2766         }
2767
2768         /* parent code */
2769         use_oplocks = True;
2770         use_level_II_oplocks = True;
2771         if (!torture_open_connection(&cli)) { 
2772                 return False;
2773         }
2774         cli_oplock_handler(cli, oplock3_handler);
2775         fnum = cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE);
2776         cli_write(cli, fnum, 0, buf, 0, 4);
2777         cli_close(cli, fnum);
2778         fnum = cli_open(cli, fname, O_RDWR, DENY_NONE);
2779         cli->timeout = 20000;
2780         cli_receive_smb(cli);
2781         printf("finished oplock test 3\n");
2782
2783         return (correct && *shared_correct);
2784
2785 /* What are we looking for here?  What's sucess and what's FAILURE? */
2786 }
2787
2788
2789
2790 /*
2791   Test delete on close semantics.
2792  */
2793 static BOOL run_deletetest(int dummy)
2794 {
2795         struct cli_state *cli1 = NULL;
2796         struct cli_state *cli2 = NULL;
2797         const char *fname = "\\delete.file";
2798         int fnum1 = -1;
2799         int fnum2 = -1;
2800         BOOL correct = True;
2801         
2802         printf("starting delete test\n");
2803         
2804         if (!torture_open_connection(&cli1)) {
2805                 return False;
2806         }
2807         
2808         cli_sockopt(cli1, sockops);
2809
2810         /* Test 1 - this should delete the file on close. */
2811         
2812         cli_setatr(cli1, fname, 0, 0);
2813         cli_unlink(cli1, fname);
2814         
2815         fnum1 = cli_nt_create_full(cli1, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
2816                                    0, FILE_OVERWRITE_IF, 
2817                                    FILE_DELETE_ON_CLOSE, 0);
2818         
2819         if (fnum1 == -1) {
2820                 printf("[1] open of %s failed (%s)\n", fname, cli_errstr(cli1));
2821                 correct = False;
2822                 goto fail;
2823         }
2824
2825 #if 0
2826         {
2827                 uint32 accinfo = 0;
2828                 cli_qfileinfo_test(cli1, fnum1, SMB_FILE_ACCESS_INFORMATION, (char *)&accinfo);
2829                 printf("access mode = 0x%lx\n", accinfo);
2830         }
2831 #endif
2832
2833         if (!cli_close(cli1, fnum1)) {
2834                 printf("[1] close failed (%s)\n", cli_errstr(cli1));
2835                 correct = False;
2836                 goto fail;
2837         }
2838
2839         fnum1 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
2840         if (fnum1 != -1) {
2841                 printf("[1] open of %s succeeded (should fail)\n", fname);
2842                 correct = False;
2843                 goto fail;
2844         }
2845         
2846         printf("first delete on close test succeeded.\n");
2847         
2848         /* Test 2 - this should delete the file on close. */
2849         
2850         cli_setatr(cli1, fname, 0, 0);
2851         cli_unlink(cli1, fname);
2852         
2853         fnum1 = cli_nt_create_full(cli1, fname, 0, GENERIC_ALL_ACCESS,
2854                                    FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, 
2855                                    FILE_OVERWRITE_IF, 0, 0);
2856         
2857         if (fnum1 == -1) {
2858                 printf("[2] open of %s failed (%s)\n", fname, cli_errstr(cli1));
2859                 correct = False;
2860                 goto fail;
2861         }
2862         
2863         if (!cli_nt_delete_on_close(cli1, fnum1, True)) {
2864                 printf("[2] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
2865                 correct = False;
2866                 goto fail;
2867         }
2868         
2869         if (!cli_close(cli1, fnum1)) {
2870                 printf("[2] close failed (%s)\n", cli_errstr(cli1));
2871                 correct = False;
2872                 goto fail;
2873         }
2874         
2875         fnum1 = cli_open(cli1, fname, O_RDONLY, DENY_NONE);
2876         if (fnum1 != -1) {
2877                 printf("[2] open of %s succeeded should have been deleted on close !\n", fname);
2878                 if (!cli_close(cli1, fnum1)) {
2879                         printf("[2] close failed (%s)\n", cli_errstr(cli1));
2880                         correct = False;
2881                         goto fail;
2882                 }
2883                 cli_unlink(cli1, fname);
2884         } else
2885                 printf("second delete on close test succeeded.\n");
2886         
2887         /* Test 3 - ... */
2888         cli_setatr(cli1, fname, 0, 0);
2889         cli_unlink(cli1, fname);
2890
2891         fnum1 = cli_nt_create_full(cli1, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
2892                                    FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0);
2893
2894         if (fnum1 == -1) {
2895                 printf("[3] open - 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
2896                 correct = False;
2897                 goto fail;
2898         }
2899
2900         /* This should fail with a sharing violation - open for delete is only compatible
2901            with SHARE_DELETE. */
2902
2903         fnum2 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
2904                         FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0, 0);
2905
2906         if (fnum2 != -1) {
2907                 printf("[3] open  - 2 of %s succeeded - should have failed.\n", fname);
2908                 correct = False;
2909                 goto fail;
2910         }
2911
2912         /* This should succeed. */
2913
2914         fnum2 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
2915                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0);
2916
2917         if (fnum2 == -1) {
2918                 printf("[3] open  - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
2919                 correct = False;
2920                 goto fail;
2921         }
2922
2923         if (!cli_nt_delete_on_close(cli1, fnum1, True)) {
2924                 printf("[3] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
2925                 correct = False;
2926                 goto fail;
2927         }
2928         
2929         if (!cli_close(cli1, fnum1)) {
2930                 printf("[3] close 1 failed (%s)\n", cli_errstr(cli1));
2931                 correct = False;
2932                 goto fail;
2933         }
2934         
2935         if (!cli_close(cli1, fnum2)) {
2936                 printf("[3] close 2 failed (%s)\n", cli_errstr(cli1));
2937                 correct = False;
2938                 goto fail;
2939         }
2940         
2941         /* This should fail - file should no longer be there. */
2942
2943         fnum1 = cli_open(cli1, fname, O_RDONLY, DENY_NONE);
2944         if (fnum1 != -1) {
2945                 printf("[3] open of %s succeeded should have been deleted on close !\n", fname);
2946                 if (!cli_close(cli1, fnum1)) {
2947                         printf("[3] close failed (%s)\n", cli_errstr(cli1));
2948                 }
2949                 cli_unlink(cli1, fname);
2950                 correct = False;
2951                 goto fail;
2952         } else
2953                 printf("third delete on close test succeeded.\n");
2954
2955         /* Test 4 ... */
2956         cli_setatr(cli1, fname, 0, 0);
2957         cli_unlink(cli1, fname);
2958
2959         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
2960                         FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0);
2961                                                                 
2962         if (fnum1 == -1) {
2963                 printf("[4] open of %s failed (%s)\n", fname, cli_errstr(cli1));
2964                 correct = False;
2965                 goto fail;
2966         }
2967
2968         /* This should succeed. */
2969         fnum2 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS,
2970                         FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0);
2971         if (fnum2 == -1) {
2972                 printf("[4] open  - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
2973                 correct = False;
2974                 goto fail;
2975         }
2976         
2977         if (!cli_close(cli1, fnum2)) {
2978                 printf("[4] close - 1 failed (%s)\n", cli_errstr(cli1));
2979                 correct = False;
2980                 goto fail;
2981         }
2982         
2983         if (!cli_nt_delete_on_close(cli1, fnum1, True)) {
2984                 printf("[4] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
2985                 correct = False;
2986                 goto fail;
2987         }
2988         
2989         /* This should fail - no more opens once delete on close set. */
2990         fnum2 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS,
2991                                    FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
2992                                    FILE_OPEN, 0, 0);
2993         if (fnum2 != -1) {
2994                 printf("[4] open  - 3 of %s succeeded ! Should have failed.\n", fname );
2995                 correct = False;
2996                 goto fail;
2997         } else
2998                 printf("fourth delete on close test succeeded.\n");
2999         
3000         if (!cli_close(cli1, fnum1)) {
3001                 printf("[4] close - 2 failed (%s)\n", cli_errstr(cli1));
3002                 correct = False;
3003                 goto fail;
3004         }
3005         
3006         /* Test 5 ... */
3007         cli_setatr(cli1, fname, 0, 0);
3008         cli_unlink(cli1, fname);
3009         
3010         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT, DENY_NONE);
3011         if (fnum1 == -1) {
3012                 printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3013                 correct = False;
3014                 goto fail;
3015         }
3016
3017         /* This should fail - only allowed on NT opens with DELETE access. */
3018
3019         if (cli_nt_delete_on_close(cli1, fnum1, True)) {
3020                 printf("[5] setting delete_on_close on OpenX file succeeded - should fail !\n");
3021                 correct = False;
3022                 goto fail;
3023         }
3024
3025         if (!cli_close(cli1, fnum1)) {
3026                 printf("[5] close - 2 failed (%s)\n", cli_errstr(cli1));
3027                 correct = False;
3028                 goto fail;
3029         }
3030         
3031         printf("fifth delete on close test succeeded.\n");
3032         
3033         /* Test 6 ... */
3034         cli_setatr(cli1, fname, 0, 0);
3035         cli_unlink(cli1, fname);
3036         
3037         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
3038                                    FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3039                                    FILE_OVERWRITE_IF, 0, 0);
3040         
3041         if (fnum1 == -1) {
3042                 printf("[6] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3043                 correct = False;
3044                 goto fail;
3045         }
3046         
3047         /* This should fail - only allowed on NT opens with DELETE access. */
3048         
3049         if (cli_nt_delete_on_close(cli1, fnum1, True)) {
3050                 printf("[6] setting delete_on_close on file with no delete access succeeded - should fail !\n");
3051                 correct = False;
3052                 goto fail;
3053         }
3054
3055         if (!cli_close(cli1, fnum1)) {
3056                 printf("[6] close - 2 failed (%s)\n", cli_errstr(cli1));
3057                 correct = False;
3058                 goto fail;
3059         }
3060
3061         printf("sixth delete on close test succeeded.\n");
3062         
3063         /* Test 7 ... */
3064         cli_setatr(cli1, fname, 0, 0);
3065         cli_unlink(cli1, fname);
3066         
3067         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3068                                    FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, 0, 0);
3069                                                                 
3070         if (fnum1 == -1) {
3071                 printf("[7] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3072                 correct = False;
3073                 goto fail;
3074         }
3075
3076         if (!cli_nt_delete_on_close(cli1, fnum1, True)) {
3077                 printf("[7] setting delete_on_close on file failed !\n");
3078                 correct = False;
3079                 goto fail;
3080         }
3081         
3082         if (!cli_nt_delete_on_close(cli1, fnum1, False)) {
3083                 printf("[7] unsetting delete_on_close on file failed !\n");
3084                 correct = False;
3085                 goto fail;
3086         }
3087
3088         if (!cli_close(cli1, fnum1)) {
3089                 printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
3090                 correct = False;
3091                 goto fail;
3092         }
3093         
3094         /* This next open should succeed - we reset the flag. */
3095         
3096         fnum1 = cli_open(cli1, fname, O_RDONLY, DENY_NONE);
3097         if (fnum1 == -1) {
3098                 printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3099                 correct = False;
3100                 goto fail;
3101         }
3102
3103         if (!cli_close(cli1, fnum1)) {
3104                 printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
3105                 correct = False;
3106                 goto fail;
3107         }
3108
3109         printf("seventh delete on close test succeeded.\n");
3110         
3111         /* Test 7 ... */
3112         cli_setatr(cli1, fname, 0, 0);
3113         cli_unlink(cli1, fname);
3114         
3115         if (!torture_open_connection(&cli2)) {
3116                 printf("[8] failed to open second connection.\n");
3117                 correct = False;
3118                 goto fail;
3119         }
3120
3121         cli_sockopt(cli1, sockops);
3122         
3123         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3124                                    FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3125                                    FILE_OVERWRITE_IF, 0, 0);
3126         
3127         if (fnum1 == -1) {
3128                 printf("[8] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3129                 correct = False;
3130                 goto fail;
3131         }
3132
3133         fnum2 = cli_nt_create_full(cli2, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3134                                    FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3135                                    FILE_OPEN, 0, 0);
3136         
3137         if (fnum2 == -1) {
3138                 printf("[8] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3139                 correct = False;
3140                 goto fail;
3141         }
3142
3143         if (!cli_nt_delete_on_close(cli1, fnum1, True)) {
3144                 printf("[8] setting delete_on_close on file failed !\n");
3145                 correct = False;
3146                 goto fail;
3147         }
3148         
3149         if (!cli_close(cli1, fnum1)) {
3150                 printf("[8] close - 1 failed (%s)\n", cli_errstr(cli1));
3151                 correct = False;
3152                 goto fail;
3153         }
3154
3155         if (!cli_close(cli2, fnum2)) {
3156                 printf("[8] close - 2 failed (%s)\n", cli_errstr(cli2));
3157                 correct = False;
3158                 goto fail;
3159         }
3160
3161         /* This should fail.. */
3162         fnum1 = cli_open(cli1, fname, O_RDONLY, DENY_NONE);
3163         if (fnum1 != -1) {
3164                 printf("[8] open of %s succeeded should have been deleted on close !\n", fname);
3165                 goto fail;
3166                 correct = False;
3167         } else
3168                 printf("eighth delete on close test succeeded.\n");
3169
3170         /* This should fail - we need to set DELETE_ACCESS. */
3171         fnum1 = cli_nt_create_full(cli1, fname, 0,FILE_READ_DATA|FILE_WRITE_DATA,
3172                                    FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0);
3173         
3174         if (fnum1 != -1) {
3175                 printf("[9] open of %s succeeded should have failed!\n", fname);
3176                 correct = False;
3177                 goto fail;
3178         }
3179
3180         printf("ninth delete on close test succeeded.\n");
3181
3182         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3183                                    FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0);
3184         if (fnum1 == -1) {
3185                 printf("[10] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3186                 correct = False;
3187                 goto fail;
3188         }
3189
3190         /* This should delete the file. */
3191         if (!cli_close(cli1, fnum1)) {
3192                 printf("[10] close failed (%s)\n", cli_errstr(cli1));
3193                 correct = False;
3194                 goto fail;
3195         }
3196
3197         /* This should fail.. */
3198         fnum1 = cli_open(cli1, fname, O_RDONLY, DENY_NONE);
3199         if (fnum1 != -1) {
3200                 printf("[10] open of %s succeeded should have been deleted on close !\n", fname);
3201                 goto fail;
3202                 correct = False;
3203         } else
3204                 printf("tenth delete on close test succeeded.\n");
3205         printf("finished delete test\n");
3206
3207   fail:
3208         /* FIXME: This will crash if we aborted before cli2 got
3209          * intialized, because these functions don't handle
3210          * uninitialized connections. */
3211                 
3212         if (fnum1 != -1) cli_close(cli1, fnum1);
3213         if (fnum2 != -1) cli_close(cli1, fnum2);
3214         cli_setatr(cli1, fname, 0, 0);
3215         cli_unlink(cli1, fname);
3216
3217         if (cli1 && !torture_close_connection(cli1)) {
3218                 correct = False;
3219         }
3220         if (cli2 && !torture_close_connection(cli2)) {
3221                 correct = False;
3222         }
3223         return correct;
3224 }
3225
3226
3227 /*
3228   print out server properties
3229  */
3230 static BOOL run_properties(int dummy)
3231 {
3232         static struct cli_state *cli;
3233         BOOL correct = True;
3234         
3235         printf("starting properties test\n");
3236         
3237         ZERO_STRUCT(cli);
3238
3239         if (!torture_open_connection(&cli)) {
3240                 return False;
3241         }
3242         
3243         cli_sockopt(cli, sockops);
3244
3245         d_printf("Capabilities 0x%08x\n", cli->capabilities);
3246
3247         if (!torture_close_connection(cli)) {
3248                 correct = False;
3249         }
3250
3251         return correct;
3252 }
3253
3254
3255
3256 /* FIRST_DESIRED_ACCESS   0xf019f */
3257 #define FIRST_DESIRED_ACCESS   FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|\
3258                                FILE_READ_EA|                           /* 0xf */ \
3259                                FILE_WRITE_EA|FILE_READ_ATTRIBUTES|     /* 0x90 */ \
3260                                FILE_WRITE_ATTRIBUTES|                  /* 0x100 */ \
3261                                DELETE_ACCESS|READ_CONTROL_ACCESS|\
3262                                WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS     /* 0xf0000 */
3263 /* SECOND_DESIRED_ACCESS  0xe0080 */
3264 #define SECOND_DESIRED_ACCESS  FILE_READ_ATTRIBUTES|                   /* 0x80 */ \
3265                                READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
3266                                WRITE_OWNER_ACCESS                      /* 0xe0000 */
3267
3268 #if 0
3269 #define THIRD_DESIRED_ACCESS   FILE_READ_ATTRIBUTES|                   /* 0x80 */ \
3270                                READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
3271                                FILE_READ_DATA|\
3272                                WRITE_OWNER_ACCESS                      /* */
3273 #endif
3274
3275 /*
3276   Test ntcreate calls made by xcopy
3277  */
3278 static BOOL run_xcopy(int dummy)
3279 {
3280         static struct cli_state *cli1;
3281         const char *fname = "\\test.txt";
3282         BOOL correct = True;
3283         int fnum1, fnum2;
3284
3285         printf("starting xcopy test\n");
3286         
3287         if (!torture_open_connection(&cli1)) {
3288                 return False;
3289         }
3290         
3291         fnum1 = cli_nt_create_full(cli1, fname, 0,
3292                                    FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
3293                                    FILE_SHARE_NONE, FILE_OVERWRITE_IF, 
3294                                    0x4044, 0);
3295
3296         if (fnum1 == -1) {
3297                 printf("First open failed - %s\n", cli_errstr(cli1));
3298                 return False;
3299         }
3300
3301         fnum2 = cli_nt_create_full(cli1, fname, 0,
3302                                    SECOND_DESIRED_ACCESS, 0,
3303                                    FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 
3304                                    0x200000, 0);
3305         if (fnum2 == -1) {
3306                 printf("second open failed - %s\n", cli_errstr(cli1));
3307                 return False;
3308         }
3309         
3310         if (!torture_close_connection(cli1)) {
3311                 correct = False;
3312         }
3313         
3314         return correct;
3315 }
3316
3317 /*
3318   Test rename on files open with share delete and no share delete.
3319  */
3320 static BOOL run_rename(int dummy)
3321 {
3322         static struct cli_state *cli1;
3323         const char *fname = "\\test.txt";
3324         const char *fname1 = "\\test1.txt";
3325         BOOL correct = True;
3326         int fnum1;
3327
3328         printf("starting rename test\n");
3329         
3330         if (!torture_open_connection(&cli1)) {
3331                 return False;
3332         }
3333         
3334         cli_unlink(cli1, fname);
3335         cli_unlink(cli1, fname1);
3336         fnum1 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3337                                    FILE_SHARE_READ, FILE_OVERWRITE_IF, 0, 0);
3338
3339         if (fnum1 == -1) {
3340                 printf("First open failed - %s\n", cli_errstr(cli1));
3341                 return False;
3342         }
3343
3344         if (!cli_rename(cli1, fname, fname1)) {
3345                 printf("First rename failed (this is correct) - %s\n", cli_errstr(cli1));
3346         } else {
3347                 printf("First rename succeeded - this should have failed !\n");
3348                 correct = False;
3349         }
3350
3351         if (!cli_close(cli1, fnum1)) {
3352                 printf("close - 1 failed (%s)\n", cli_errstr(cli1));
3353                 return False;
3354         }
3355
3356         cli_unlink(cli1, fname);
3357         cli_unlink(cli1, fname1);
3358         fnum1 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3359 #if 0
3360                                    FILE_SHARE_DELETE|FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0);
3361 #else
3362                                    FILE_SHARE_DELETE|FILE_SHARE_READ, FILE_OVERWRITE_IF, 0, 0);
3363 #endif
3364
3365         if (fnum1 == -1) {
3366                 printf("Second open failed - %s\n", cli_errstr(cli1));
3367                 return False;
3368         }
3369
3370         if (!cli_rename(cli1, fname, fname1)) {
3371                 printf("Second rename failed - this should have succeeded - %s\n", cli_errstr(cli1));
3372                 correct = False;
3373         } else {
3374                 printf("Second rename succeeded\n");
3375         }
3376
3377         if (!cli_close(cli1, fnum1)) {
3378                 printf("close - 2 failed (%s)\n", cli_errstr(cli1));
3379                 return False;
3380         }
3381
3382         cli_unlink(cli1, fname);
3383         cli_unlink(cli1, fname1);
3384
3385         fnum1 = cli_nt_create_full(cli1, fname, 0, READ_CONTROL_ACCESS, FILE_ATTRIBUTE_NORMAL,
3386                                    FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0);
3387
3388         if (fnum1 == -1) {
3389                 printf("Third open failed - %s\n", cli_errstr(cli1));
3390                 return False;
3391         }
3392
3393
3394 #if 0
3395   {
3396   int fnum2;
3397
3398         fnum2 = cli_nt_create_full(cli1, fname, 0, DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
3399                                    FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0);
3400
3401         if (fnum2 == -1) {
3402                 printf("Fourth open failed - %s\n", cli_errstr(cli1));
3403                 return False;
3404         }
3405         if (!cli_nt_delete_on_close(cli1, fnum2, True)) {
3406                 printf("[8] setting delete_on_close on file failed !\n");
3407                 return False;
3408         }
3409         
3410         if (!cli_close(cli1, fnum2)) {
3411                 printf("close - 4 failed (%s)\n", cli_errstr(cli1));
3412                 return False;
3413         }
3414   }
3415 #endif
3416
3417         if (!cli_rename(cli1, fname, fname1)) {
3418                 printf("Third rename failed - this should have succeeded - %s\n", cli_errstr(cli1));
3419                 correct = False;
3420         } else {
3421                 printf("Third rename succeeded\n");
3422         }
3423
3424         if (!cli_close(cli1, fnum1)) {
3425                 printf("close - 3 failed (%s)\n", cli_errstr(cli1));
3426                 return False;
3427         }
3428
3429         cli_unlink(cli1, fname);
3430         cli_unlink(cli1, fname1);
3431
3432         if (!torture_close_connection(cli1)) {
3433                 correct = False;
3434         }
3435         
3436         return correct;
3437 }
3438
3439 static BOOL run_pipe_number(int dummy)
3440 {
3441         struct cli_state *cli1;
3442         const char *pipe_name = "\\SPOOLSS";
3443         int fnum;
3444         int num_pipes = 0;
3445
3446         printf("starting pipenumber test\n");
3447         if (!torture_open_connection(&cli1)) {
3448                 return False;
3449         }
3450
3451         cli_sockopt(cli1, sockops);
3452         while(1) {
3453                 fnum = cli_nt_create_full(cli1, pipe_name, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
3454                                    FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF, 0, 0);
3455
3456                 if (fnum == -1) {
3457                         printf("Open of pipe %s failed with error (%s)\n", pipe_name, cli_errstr(cli1));
3458                         break;
3459                 }
3460                 num_pipes++;
3461         }
3462
3463         printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
3464         torture_close_connection(cli1);
3465         return True;
3466 }
3467
3468 /*
3469   Test open mode returns on read-only files.
3470  */
3471 static BOOL run_opentest(int dummy)
3472 {
3473         static struct cli_state *cli1;
3474         static struct cli_state *cli2;
3475         const char *fname = "\\readonly.file";
3476         int fnum1, fnum2;
3477         char buf[20];
3478         size_t fsize;
3479         BOOL correct = True;
3480         char *tmp_path;
3481
3482         printf("starting open test\n");
3483         
3484         if (!torture_open_connection(&cli1)) {
3485                 return False;
3486         }
3487         
3488         cli_setatr(cli1, fname, 0, 0);
3489         cli_unlink(cli1, fname);
3490         
3491         cli_sockopt(cli1, sockops);
3492         
3493         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
3494         if (fnum1 == -1) {
3495                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3496                 return False;
3497         }
3498
3499         if (!cli_close(cli1, fnum1)) {
3500                 printf("close2 failed (%s)\n", cli_errstr(cli1));
3501                 return False;
3502         }
3503         
3504         if (!cli_setatr(cli1, fname, aRONLY, 0)) {
3505                 printf("cli_setatr failed (%s)\n", cli_errstr(cli1));
3506                 return False;
3507         }
3508         
3509         fnum1 = cli_open(cli1, fname, O_RDONLY, DENY_WRITE);
3510         if (fnum1 == -1) {
3511                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3512                 return False;
3513         }
3514         
3515         /* This will fail - but the error should be ERRnoaccess, not ERRbadshare. */
3516         fnum2 = cli_open(cli1, fname, O_RDWR, DENY_ALL);
3517         
3518         if (check_error(__LINE__, cli1, ERRDOS, ERRnoaccess, 
3519                         NT_STATUS_ACCESS_DENIED)) {
3520                 printf("correct error code ERRDOS/ERRnoaccess returned\n");
3521         }
3522         
3523         printf("finished open test 1\n");
3524         
3525         cli_close(cli1, fnum1);
3526         
3527         /* Now try not readonly and ensure ERRbadshare is returned. */
3528         
3529         cli_setatr(cli1, fname, 0, 0);
3530         
3531         fnum1 = cli_open(cli1, fname, O_RDONLY, DENY_WRITE);
3532         if (fnum1 == -1) {
3533                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3534                 return False;
3535         }
3536         
3537         /* This will fail - but the error should be ERRshare. */
3538         fnum2 = cli_open(cli1, fname, O_RDWR, DENY_ALL);
3539         
3540         if (check_error(__LINE__, cli1, ERRDOS, ERRbadshare, 
3541                         NT_STATUS_SHARING_VIOLATION)) {
3542                 printf("correct error code ERRDOS/ERRbadshare returned\n");
3543         }
3544         
3545         if (!cli_close(cli1, fnum1)) {
3546                 printf("close2 failed (%s)\n", cli_errstr(cli1));
3547                 return False;
3548         }
3549         
3550         cli_unlink(cli1, fname);
3551         
3552         printf("finished open test 2\n");
3553         
3554         /* Test truncate open disposition on file opened for read. */
3555         
3556         fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
3557         if (fnum1 == -1) {
3558                 printf("(3) open (1) of %s failed (%s)\n", fname, cli_errstr(cli1));
3559                 return False;
3560         }
3561         
3562         /* write 20 bytes. */
3563         
3564         memset(buf, '\0', 20);
3565
3566         if (cli_write(cli1, fnum1, 0, buf, 0, 20) != 20) {
3567                 printf("write failed (%s)\n", cli_errstr(cli1));
3568                 correct = False;
3569         }
3570
3571         if (!cli_close(cli1, fnum1)) {
3572                 printf("(3) close1 failed (%s)\n", cli_errstr(cli1));
3573                 return False;
3574         }
3575         
3576         /* Ensure size == 20. */
3577         if (!cli_getatr(cli1, fname, NULL, &fsize, NULL)) {
3578                 printf("(3) getatr failed (%s)\n", cli_errstr(cli1));
3579                 return False;
3580         }
3581         
3582         if (fsize != 20) {
3583                 printf("(3) file size != 20\n");
3584                 return False;
3585         }
3586
3587         /* Now test if we can truncate a file opened for readonly. */
3588         
3589         fnum1 = cli_open(cli1, fname, O_RDONLY|O_TRUNC, DENY_NONE);
3590         if (fnum1 == -1) {
3591                 printf("(3) open (2) of %s failed (%s)\n", fname, cli_errstr(cli1));
3592                 return False;
3593         }
3594         
3595         if (!cli_close(cli1, fnum1)) {
3596                 printf("close2 failed (%s)\n", cli_errstr(cli1));
3597                 return False;
3598         }
3599
3600         /* Ensure size == 0. */
3601         if (!cli_getatr(cli1, fname, NULL, &fsize, NULL)) {
3602                 printf("(3) getatr failed (%s)\n", cli_errstr(cli1));
3603                 return False;
3604         }
3605
3606         if (fsize != 0) {
3607                 printf("(3) file size != 0\n");
3608                 return False;
3609         }
3610         printf("finished open test 3\n");
3611         
3612         cli_unlink(cli1, fname);
3613
3614
3615         printf("testing ctemp\n");
3616         fnum1 = cli_ctemp(cli1, "\\", &tmp_path);
3617         if (fnum1 == -1) {
3618                 printf("ctemp failed (%s)\n", cli_errstr(cli1));
3619                 return False;
3620         }
3621         printf("ctemp gave path %s\n", tmp_path);
3622         if (!cli_close(cli1, fnum1)) {
3623                 printf("close of temp failed (%s)\n", cli_errstr(cli1));
3624         }
3625         if (!cli_unlink(cli1, tmp_path)) {
3626                 printf("unlink of temp failed (%s)\n", cli_errstr(cli1));
3627         }
3628         
3629         /* Test the non-io opens... */
3630
3631         if (!torture_open_connection(&cli2)) {
3632                 return False;
3633         }
3634         
3635         cli_setatr(cli2, fname, 0, 0);
3636         cli_unlink(cli2, fname);
3637         
3638         cli_sockopt(cli2, sockops);
3639
3640         printf("TEST #1 testing 2 non-io opens (no delete)\n");
3641         
3642         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
3643                                    FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0);
3644
3645         if (fnum1 == -1) {
3646                 printf("test 1 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3647                 return False;
3648         }
3649
3650         fnum2 = cli_nt_create_full(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
3651                                    FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0);
3652
3653         if (fnum2 == -1) {
3654                 printf("test 1 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
3655                 return False;
3656         }
3657
3658         if (!cli_close(cli1, fnum1)) {
3659                 printf("test 1 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3660                 return False;
3661         }
3662         if (!cli_close(cli2, fnum2)) {
3663                 printf("test 1 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
3664                 return False;
3665         }
3666
3667         printf("non-io open test #1 passed.\n");
3668
3669         cli_unlink(cli1, fname);
3670
3671         printf("TEST #2 testing 2 non-io opens (first with delete)\n");
3672         
3673         fnum1 = cli_nt_create_full(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
3674                                    FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0);
3675
3676         if (fnum1 == -1) {
3677                 printf("test 2 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3678                 return False;
3679         }
3680
3681         fnum2 = cli_nt_create_full(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
3682                                    FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0);
3683
3684         if (fnum2 == -1) {
3685                 printf("test 2 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
3686                 return False;
3687         }
3688
3689         if (!cli_close(cli1, fnum1)) {
3690                 printf("test 1 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3691                 return False;
3692         }
3693         if (!cli_close(cli2, fnum2)) {
3694                 printf("test 1 close 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
3695                 return False;
3696         }
3697
3698         printf("non-io open test #2 passed.\n");
3699
3700         cli_unlink(cli1, fname);
3701
3702         printf("TEST #3 testing 2 non-io opens (second with delete)\n");
3703         
3704         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
3705                                    FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0);
3706
3707         if (fnum1 == -1) {
3708                 printf("test 3 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3709                 return False;
3710         }
3711
3712         fnum2 = cli_nt_create_full(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
3713                                    FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0);
3714
3715         if (fnum2 == -1) {
3716                 printf("test 3 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
3717                 return False;
3718         }
3719
3720         if (!cli_close(cli1, fnum1)) {
3721                 printf("test 3 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3722                 return False;
3723         }
3724         if (!cli_close(cli2, fnum2)) {
3725                 printf("test 3 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
3726                 return False;
3727         }
3728
3729         printf("non-io open test #3 passed.\n");
3730
3731         cli_unlink(cli1, fname);
3732
3733         printf("TEST #4 testing 2 non-io opens (both with delete)\n");
3734         
3735         fnum1 = cli_nt_create_full(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
3736                                    FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0);
3737
3738         if (fnum1 == -1) {
3739                 printf("test 4 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3740                 return False;
3741         }
3742
3743         fnum2 = cli_nt_create_full(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
3744                                    FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0);
3745
3746         if (fnum2 != -1) {
3747                 printf("test 4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, cli_errstr(cli2));
3748                 return False;
3749         }
3750
3751         printf("test 3 open 2 of %s gave %s (correct error should be %s)\n", fname, cli_errstr(cli2), "sharing violation");
3752
3753         if (!cli_close(cli1, fnum1)) {
3754                 printf("test 4 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3755                 return False;
3756         }
3757
3758         printf("non-io open test #4 passed.\n");
3759
3760         cli_unlink(cli1, fname);
3761
3762         printf("TEST #5 testing 2 non-io opens (both with delete - both with file share delete)\n");
3763         
3764         fnum1 = cli_nt_create_full(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
3765                                    FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0);
3766
3767         if (fnum1 == -1) {
3768                 printf("test 5 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3769                 return False;
3770         }
3771
3772         fnum2 = cli_nt_create_full(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
3773                                    FILE_SHARE_DELETE, FILE_OPEN_IF, 0, 0);
3774
3775         if (fnum2 == -1) {
3776                 printf("test 5 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
3777                 return False;
3778         }
3779
3780         if (!cli_close(cli1, fnum1)) {
3781                 printf("test 5 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3782                 return False;
3783         }
3784
3785         if (!cli_close(cli2, fnum2)) {
3786                 printf("test 5 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
3787                 return False;
3788         }
3789
3790         printf("non-io open test #5 passed.\n");
3791
3792         printf("TEST #6 testing 1 non-io open, one io open\n");
3793         
3794         cli_unlink(cli1, fname);
3795
3796         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
3797                                    FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0);
3798
3799         if (fnum1 == -1) {
3800                 printf("test 6 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3801                 return False;
3802         }
3803
3804         fnum2 = cli_nt_create_full(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
3805                                    FILE_SHARE_READ, FILE_OPEN_IF, 0, 0);
3806
3807         if (fnum2 == -1) {
3808                 printf("test 6 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
3809                 return False;
3810         }
3811
3812         if (!cli_close(cli1, fnum1)) {
3813                 printf("test 6 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3814                 return False;
3815         }
3816
3817         if (!cli_close(cli2, fnum2)) {
3818                 printf("test 6 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
3819                 return False;
3820         }
3821
3822         printf("non-io open test #6 passed.\n");
3823
3824         printf("TEST #7 testing 1 non-io open, one io open with delete\n");
3825
3826         cli_unlink(cli1, fname);
3827
3828         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
3829                                    FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0);
3830
3831         if (fnum1 == -1) {
3832                 printf("test 7 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3833                 return False;
3834         }
3835
3836         fnum2 = cli_nt_create_full(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
3837                                    FILE_SHARE_READ|FILE_SHARE_DELETE, FILE_OPEN_IF, 0, 0);
3838
3839         if (fnum2 != -1) {
3840                 printf("test 7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, cli_errstr(cli2));
3841                 return False;
3842         }
3843
3844         printf("test 7 open 2 of %s gave %s (correct error should be %s)\n", fname, cli_errstr(cli2), "sharing violation");
3845
3846         if (!cli_close(cli1, fnum1)) {
3847                 printf("test 7 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3848                 return False;
3849         }
3850
3851         printf("non-io open test #7 passed.\n");
3852
3853         cli_unlink(cli1, fname);
3854
3855         if (!torture_close_connection(cli1)) {
3856                 correct = False;
3857         }
3858         if (!torture_close_connection(cli2)) {
3859                 correct = False;
3860         }
3861         
3862         return correct;
3863 }
3864
3865 static uint32 open_attrs_table[] = {
3866                 FILE_ATTRIBUTE_NORMAL,
3867                 FILE_ATTRIBUTE_ARCHIVE,
3868                 FILE_ATTRIBUTE_READONLY,
3869                 FILE_ATTRIBUTE_HIDDEN,
3870                 FILE_ATTRIBUTE_SYSTEM,
3871
3872                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY,
3873                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN,
3874                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM,
3875                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
3876                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
3877                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
3878
3879                 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
3880                 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
3881                 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
3882                 FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_SYSTEM,
3883 };
3884
3885 struct trunc_open_results {
3886         unsigned int num;
3887         uint32 init_attr;
3888         uint32 trunc_attr;
3889         uint32 result_attr;
3890 };
3891
3892 static struct trunc_open_results attr_results[] = {
3893         { 0, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
3894         { 1, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
3895         { 2, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
3896         { 16, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
3897         { 17, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
3898         { 18, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
3899         { 51, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
3900         { 54, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
3901         { 56, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
3902         { 68, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
3903         { 71, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
3904         { 73, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM },
3905         { 99, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
3906         { 102, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
3907         { 104, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
3908         { 116, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
3909         { 119,  FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM,  FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
3910         { 121, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM },
3911         { 170, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN },
3912         { 173, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM },
3913         { 227, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
3914         { 230, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
3915         { 232, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
3916         { 244, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
3917         { 247, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
3918         { 249, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM }
3919 };
3920
3921 static BOOL run_openattrtest(int dummy)
3922 {
3923         static struct cli_state *cli1;
3924         const char *fname = "\\openattr.file";
3925         int fnum1;
3926         BOOL correct = True;
3927         uint16 attr;
3928         unsigned int i, j, k, l;
3929
3930         printf("starting open attr test\n");
3931         
3932         if (!torture_open_connection(&cli1)) {
3933                 return False;
3934         }
3935         
3936         cli_sockopt(cli1, sockops);
3937
3938         for (k = 0, i = 0; i < sizeof(open_attrs_table)/sizeof(uint32); i++) {
3939                 cli_setatr(cli1, fname, 0, 0);
3940                 cli_unlink(cli1, fname);
3941                 fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_WRITE_DATA, open_attrs_table[i],
3942                                    FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0);
3943
3944                 if (fnum1 == -1) {
3945                         printf("open %d (1) of %s failed (%s)\n", i, fname, cli_errstr(cli1));
3946                         return False;
3947                 }
3948
3949                 if (!cli_close(cli1, fnum1)) {
3950                         printf("close %d (1) of %s failed (%s)\n", i, fname, cli_errstr(cli1));
3951                         return False;
3952                 }
3953
3954                 for (j = 0; j < sizeof(open_attrs_table)/sizeof(uint32); j++) {
3955                         fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA, open_attrs_table[j],
3956                                            FILE_SHARE_NONE, FILE_OVERWRITE, 0, 0);
3957
3958                         if (fnum1 == -1) {
3959                                 for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
3960                                         if (attr_results[l].num == k) {
3961                                                 printf("[%d] trunc open 0x%x -> 0x%x of %s failed - should have succeeded !(0x%x:%s)\n",
3962                                                                 k, open_attrs_table[i],
3963                                                                 open_attrs_table[j],
3964                                                                 fname, NT_STATUS_V(cli_nt_error(cli1)), cli_errstr(cli1));
3965                                                 correct = False;
3966                                         }
3967                                 }
3968                                 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_ACCESS_DENIED)) {
3969                                         printf("[%d] trunc open 0x%x -> 0x%x failed with wrong error code %s\n",
3970                                                         k, open_attrs_table[i], open_attrs_table[j],
3971                                                         cli_errstr(cli1));
3972                                         correct = False;
3973                                 }
3974 #if 0
3975                                 printf("[%d] trunc open 0x%x -> 0x%x failed\n", k, open_attrs_table[i], open_attrs_table[j]);
3976 #endif
3977                                 k++;
3978                                 continue;
3979                         }
3980
3981                         if (!cli_close(cli1, fnum1)) {
3982                                 printf("close %d (2) of %s failed (%s)\n", j, fname, cli_errstr(cli1));
3983                                 return False;
3984                         }
3985
3986                         if (!cli_getatr(cli1, fname, &attr, NULL, NULL)) {
3987                                 printf("getatr(2) failed (%s)\n", cli_errstr(cli1));
3988                                 return False;
3989                         }
3990
3991 #if 0
3992                         printf("[%d] getatr check [0x%x] trunc [0x%x] got attr 0x%x\n",
3993                                         k,  open_attrs_table[i],  open_attrs_table[j], attr );
3994 #endif
3995
3996                         for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
3997                                 if (attr_results[l].num == k) {
3998                                         if (attr != attr_results[l].result_attr ||
3999                                                         open_attrs_table[i] != attr_results[l].init_attr ||
4000                                                         open_attrs_table[j] != attr_results[l].trunc_attr) {
4001                                                 printf("getatr check failed. [0x%x] trunc [0x%x] got attr 0x%x, should be 0x%x\n",
4002                                                 open_attrs_table[i],
4003                                                 open_attrs_table[j],
4004                                                 (unsigned int)attr,
4005                                                 attr_results[l].result_attr);
4006                                                 correct = False;
4007                                         }
4008                                         break;
4009                                 }
4010                         }
4011                         k++;
4012                 }
4013         }
4014
4015         cli_setatr(cli1, fname, 0, 0);
4016         cli_unlink(cli1, fname);
4017
4018         printf("open attr test %s.\n", correct ? "passed" : "failed");
4019
4020         if (!torture_close_connection(cli1)) {
4021                 correct = False;
4022         }
4023         return correct;
4024 }
4025
4026 static void list_fn(file_info *finfo, const char *name, void *state)
4027 {
4028         
4029 }
4030
4031 /*
4032   test directory listing speed
4033  */
4034 static BOOL run_dirtest(int dummy)
4035 {
4036         int i;
4037         static struct cli_state *cli;
4038         int fnum;
4039         double t1;
4040         BOOL correct = True;
4041
4042         printf("starting directory test\n");
4043
4044         if (!torture_open_connection(&cli)) {
4045                 return False;
4046         }
4047
4048         cli_sockopt(cli, sockops);
4049
4050         srandom(0);
4051         for (i=0;i<torture_numops;i++) {
4052                 fstring fname;
4053                 slprintf(fname, sizeof(fname), "\\%x", (int)random());
4054                 fnum = cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE);
4055                 if (fnum == -1) {
4056                         fprintf(stderr,"Failed to open %s\n", fname);
4057                         return False;
4058                 }
4059                 cli_close(cli, fnum);
4060         }
4061
4062         t1 = end_timer();
4063
4064         printf("Matched %d\n", cli_list(cli, "a*.*", 0, list_fn, NULL));
4065         printf("Matched %d\n", cli_list(cli, "b*.*", 0, list_fn, NULL));
4066         printf("Matched %d\n", cli_list(cli, "xyzabc", 0, list_fn, NULL));
4067
4068         printf("dirtest core %g seconds\n", end_timer() - t1);
4069
4070         srandom(0);
4071         for (i=0;i<torture_numops;i++) {
4072                 fstring fname;
4073                 slprintf(fname, sizeof(fname), "\\%x", (int)random());
4074                 cli_unlink(cli, fname);
4075         }
4076
4077         if (!torture_close_connection(cli)) {
4078                 correct = False;
4079         }
4080
4081         printf("finished dirtest\n");
4082
4083         return correct;
4084 }
4085
4086 static void del_fn(file_info *finfo, const char *mask, void *state)
4087 {
4088         struct cli_state *pcli = (struct cli_state *)state;
4089         fstring fname;
4090         slprintf(fname, sizeof(fname), "\\LISTDIR\\%s", finfo->name);
4091
4092         if (strcmp(finfo->name, ".") == 0 || strcmp(finfo->name, "..") == 0)
4093                 return;
4094
4095         if (finfo->mode & aDIR) {
4096                 if (!cli_rmdir(pcli, fname))
4097                         printf("del_fn: failed to rmdir %s\n,", fname );
4098         } else {
4099                 if (!cli_unlink(pcli, fname))
4100                         printf("del_fn: failed to unlink %s\n,", fname );
4101         }
4102 }
4103
4104
4105 /*
4106   sees what IOCTLs are supported
4107  */
4108 BOOL torture_ioctl_test(int dummy)
4109 {
4110         static struct cli_state *cli;
4111         uint16 device, function;
4112         int fnum;
4113         const char *fname = "\\ioctl.dat";
4114         DATA_BLOB blob;
4115         NTSTATUS status;
4116
4117         if (!torture_open_connection(&cli)) {
4118                 return False;
4119         }
4120
4121         printf("starting ioctl test\n");
4122
4123         cli_unlink(cli, fname);
4124
4125         fnum = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
4126         if (fnum == -1) {
4127                 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
4128                 return False;
4129         }
4130
4131         status = cli_raw_ioctl(cli, fnum, 0x2d0000 | (0x0420<<2), &blob);
4132         printf("ioctl device info: %s\n", cli_errstr(cli));
4133
4134         status = cli_raw_ioctl(cli, fnum, IOCTL_QUERY_JOB_INFO, &blob);
4135         printf("ioctl job info: %s\n", cli_errstr(cli));
4136
4137         for (device=0;device<0x100;device++) {
4138                 printf("testing device=0x%x\n", device);
4139                 for (function=0;function<0x100;function++) {
4140                         uint32 code = (device<<16) | function;
4141
4142                         status = cli_raw_ioctl(cli, fnum, code, &blob);
4143
4144                         if (NT_STATUS_IS_OK(status)) {
4145                                 printf("ioctl 0x%x OK : %d bytes\n", code, blob.length);
4146                                 data_blob_free(&blob);
4147                         }
4148                 }
4149         }
4150
4151         if (!torture_close_connection(cli)) {
4152                 return False;
4153         }
4154
4155         return True;
4156 }
4157
4158
4159 /*
4160   tries varients of chkpath
4161  */
4162 BOOL torture_chkpath_test(int dummy)
4163 {
4164         static struct cli_state *cli;
4165         int fnum;
4166         BOOL ret;
4167
4168         if (!torture_open_connection(&cli)) {
4169                 return False;
4170         }
4171
4172         printf("starting chkpath test\n");
4173
4174         /* cleanup from an old run */
4175         cli_rmdir(cli, "\\chkpath.dir\\dir2");
4176         cli_unlink(cli, "\\chkpath.dir\\*");
4177         cli_rmdir(cli, "\\chkpath.dir");
4178
4179         if (!cli_mkdir(cli, "\\chkpath.dir")) {
4180                 printf("mkdir1 failed : %s\n", cli_errstr(cli));
4181                 return False;
4182         }
4183
4184         if (!cli_mkdir(cli, "\\chkpath.dir\\dir2")) {
4185                 printf("mkdir2 failed : %s\n", cli_errstr(cli));
4186                 return False;
4187         }
4188
4189         fnum = cli_open(cli, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
4190         if (fnum == -1) {
4191                 printf("open1 failed (%s)\n", cli_errstr(cli));
4192                 return False;
4193         }
4194         cli_close(cli, fnum);
4195
4196         if (!cli_chkpath(cli, "\\chkpath.dir")) {
4197                 printf("chkpath1 failed: %s\n", cli_errstr(cli));
4198                 ret = False;
4199         }
4200
4201         if (!cli_chkpath(cli, "\\chkpath.dir\\dir2")) {
4202                 printf("chkpath2 failed: %s\n", cli_errstr(cli));
4203                 ret = False;
4204         }
4205
4206         if (!cli_chkpath(cli, "\\chkpath.dir\\foo.txt")) {
4207                 ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath, 
4208                                   NT_STATUS_NOT_A_DIRECTORY);
4209         } else {
4210                 printf("* chkpath on a file should fail\n");
4211                 ret = False;
4212         }
4213
4214         if (!cli_chkpath(cli, "\\chkpath.dir\\bar.txt")) {
4215                 ret = check_error(__LINE__, cli, ERRDOS, ERRbadfile, 
4216                                   NT_STATUS_OBJECT_NAME_NOT_FOUND);
4217         } else {
4218                 printf("* chkpath on a non existant file should fail\n");
4219                 ret = False;
4220         }
4221
4222         if (!cli_chkpath(cli, "\\chkpath.dir\\dirxx\\bar.txt")) {
4223                 ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath, 
4224                                   NT_STATUS_OBJECT_PATH_NOT_FOUND);
4225         } else {
4226                 printf("* chkpath on a non existent component should fail\n");
4227                 ret = False;
4228         }
4229
4230         cli_rmdir(cli, "\\chkpath.dir\\dir2");
4231         cli_unlink(cli, "\\chkpath.dir\\*");
4232         cli_rmdir(cli, "\\chkpath.dir");
4233
4234         if (!torture_close_connection(cli)) {
4235                 return False;
4236         }
4237
4238         return ret;
4239 }
4240
4241
4242
4243
4244 static BOOL run_dirtest1(int dummy)
4245 {
4246         int i;
4247         static struct cli_state *cli;
4248         int fnum, num_seen;
4249         BOOL correct = True;
4250
4251         printf("starting directory test\n");
4252
4253         if (!torture_open_connection(&cli)) {
4254                 return False;
4255         }
4256
4257         cli_sockopt(cli, sockops);
4258
4259         cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
4260         cli_list(cli, "\\LISTDIR\\*", aDIR, del_fn, cli);
4261         cli_rmdir(cli, "\\LISTDIR");
4262         cli_mkdir(cli, "\\LISTDIR");
4263
4264         /* Create 1000 files and 1000 directories. */
4265         for (i=0;i<1000;i++) {
4266                 fstring fname;
4267                 slprintf(fname, sizeof(fname), "\\LISTDIR\\f%d", i);
4268                 fnum = cli_nt_create_full(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
4269                                    FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0);
4270                 if (fnum == -1) {
4271                         fprintf(stderr,"Failed to open %s\n", fname);
4272                         return False;
4273                 }
4274                 cli_close(cli, fnum);
4275         }
4276         for (i=0;i<1000;i++) {
4277                 fstring fname;
4278                 slprintf(fname, sizeof(fname), "\\LISTDIR\\d%d", i);
4279                 if (!cli_mkdir(cli, fname)) {
4280                         fprintf(stderr,"Failed to open %s\n", fname);
4281                         return False;
4282                 }
4283         }
4284
4285         /* Now ensure that doing an old list sees both files and directories. */
4286         num_seen = cli_list_old(cli, "\\LISTDIR\\*", aDIR, list_fn, NULL);
4287         printf("num_seen = %d\n", num_seen );
4288         /* We should see 100 files + 1000 directories + . and .. */
4289         if (num_seen != 2002)
4290                 correct = False;
4291
4292         /* Ensure if we have the "must have" bits we only see the
4293          * relevent entries.
4294          */
4295         num_seen = cli_list_old(cli, "\\LISTDIR\\*", (aDIR<<8)|aDIR, list_fn, NULL);
4296         printf("num_seen = %d\n", num_seen );
4297         if (num_seen != 1002)
4298                 correct = False;
4299
4300         num_seen = cli_list_old(cli, "\\LISTDIR\\*", (aARCH<<8)|aDIR, list_fn, NULL);
4301         printf("num_seen = %d\n", num_seen );
4302         if (num_seen != 1000)
4303                 correct = False;
4304
4305         /* Delete everything. */
4306         cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
4307         cli_list(cli, "\\LISTDIR\\*", aDIR, del_fn, cli);
4308         cli_rmdir(cli, "\\LISTDIR");
4309
4310 #if 0
4311         printf("Matched %d\n", cli_list(cli, "a*.*", 0, list_fn, NULL));
4312         printf("Matched %d\n", cli_list(cli, "b*.*", 0, list_fn, NULL));
4313         printf("Matched %d\n", cli_list(cli, "xyzabc", 0, list_fn, NULL));
4314 #endif
4315
4316         if (!torture_close_connection(cli)) {
4317                 correct = False;
4318         }
4319
4320         printf("finished dirtest1\n");
4321
4322         return correct;
4323 }
4324
4325 static BOOL run_error_map_extract(int dummy) {
4326         
4327         static struct cli_state c_dos;
4328         static struct cli_state c_nt;
4329
4330         uint32 error;
4331
4332         uint32 flgs2, errnum;
4333         uint8 errclass;
4334
4335         NTSTATUS nt_status;
4336
4337         fstring user;
4338
4339         /* NT-Error connection */
4340
4341         if (!open_nbt_connection(&c_nt)) {
4342                 return False;
4343         }
4344
4345         c_nt.use_spnego = False;
4346
4347         if (!cli_negprot(&c_nt)) {
4348                 printf("%s rejected the NT-error negprot (%s)\n",host, cli_errstr(&c_nt));
4349                 cli_shutdown(&c_nt);
4350                 return False;
4351         }
4352
4353         if (!cli_session_setup(&c_nt, "", "", 0, "", 0,
4354                                workgroup)) {
4355                 printf("%s rejected the NT-error initial session setup (%s)\n",host, cli_errstr(&c_nt));
4356                 return False;
4357         }
4358
4359         /* DOS-Error connection */
4360
4361         if (!open_nbt_connection(&c_dos)) {
4362                 return False;
4363         }
4364
4365         c_dos.use_spnego = False;
4366         c_dos.force_dos_errors = True;
4367
4368         if (!cli_negprot(&c_dos)) {
4369                 printf("%s rejected the DOS-error negprot (%s)\n",host, cli_errstr(&c_dos));
4370                 cli_shutdown(&c_dos);
4371                 return False;
4372         }
4373
4374         if (!cli_session_setup(&c_dos, "", "", 0, "", 0,
4375                                workgroup)) {
4376                 printf("%s rejected the DOS-error initial session setup (%s)\n",host, cli_errstr(&c_dos));
4377                 return False;
4378         }
4379
4380         for (error=(0xc0000000 | 0x1); error < (0xc0000000| 0xFFF); error++) {
4381                 fstr_sprintf(user, "%X", error);
4382
4383                 if (cli_session_setup(&c_nt, user, 
4384                                        password, strlen(password),
4385                                        password, strlen(password),
4386                                       workgroup)) {
4387                         printf("/** Session setup succeeded.  This shouldn't happen...*/\n");
4388                 }
4389                 
4390                 flgs2 = SVAL(c_nt.inbuf,smb_flg2);
4391                 
4392                 /* Case #1: 32-bit NT errors */
4393                 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
4394                         nt_status = NT_STATUS(IVAL(c_nt.inbuf,smb_rcls));
4395                 } else {
4396                         printf("/** Dos error on NT connection! (%s) */\n", 
4397                                cli_errstr(&c_nt));
4398                         nt_status = NT_STATUS(0xc0000000);
4399                 }
4400
4401                 if (cli_session_setup(&c_dos, user, 
4402                                        password, strlen(password),
4403                                        password, strlen(password),
4404                                        workgroup)) {
4405                         printf("/** Session setup succeeded.  This shouldn't happen...*/\n");
4406                 }
4407                 flgs2 = SVAL(c_dos.inbuf,smb_flg2), errnum;
4408                 
4409                 /* Case #1: 32-bit NT errors */
4410                 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
4411                         printf("/** NT error on DOS connection! (%s) */\n", 
4412                                cli_errstr(&c_nt));
4413                         errnum = errclass = 0;
4414                 } else {
4415                         cli_dos_error(&c_dos, &errclass, &errnum);
4416                 }
4417
4418                 if (NT_STATUS_V(nt_status) != error) { 
4419                         printf("/*\t{ This NT error code was 'sqashed'\n\t from %s to %s \n\t during the session setup }\n*/\n", 
4420                                get_nt_error_c_code(NT_STATUS(error)), 
4421                                get_nt_error_c_code(nt_status));
4422                 }
4423                 
4424                 printf("\t{%s,\t%s,\t%s},\n", 
4425                        smb_dos_err_class(errclass), 
4426                        smb_dos_err_name(errclass, errnum), 
4427                        get_nt_error_c_code(NT_STATUS(error)));
4428         }
4429         return True;
4430 }
4431
4432 static double create_procs(BOOL (*fn)(int), BOOL *result)
4433 {
4434         int i, status;
4435         volatile pid_t *child_status;
4436         volatile BOOL *child_status_out;
4437         int synccount;
4438         int tries = 8;
4439
4440         synccount = 0;
4441
4442         child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*nprocs);
4443         if (!child_status) {
4444                 printf("Failed to setup shared memory\n");
4445                 return -1;
4446         }
4447
4448         child_status_out = (volatile BOOL *)shm_setup(sizeof(BOOL)*nprocs);
4449         if (!child_status_out) {
4450                 printf("Failed to setup result status shared memory\n");
4451                 return -1;
4452         }
4453
4454         for (i = 0; i < nprocs; i++) {
4455                 child_status[i] = 0;
4456                 child_status_out[i] = True;
4457         }
4458
4459         start_timer();
4460
4461         for (i=0;i<nprocs;i++) {
4462                 procnum = i;
4463                 if (fork() == 0) {
4464                         pid_t mypid = getpid();
4465                         sys_srandom(((int)mypid) ^ ((int)time(NULL)));
4466
4467                         slprintf(myname,sizeof(myname),"CLIENT%d", i);
4468
4469                         while (1) {
4470                                 if (torture_open_connection(&current_cli)) break;
4471                                 if (tries-- == 0) {
4472                                         printf("pid %d failed to start\n", (int)getpid());
4473                                         _exit(1);
4474                                 }
4475                                 msleep(10);     
4476                         }
4477
4478                         child_status[i] = getpid();
4479
4480                         while (child_status[i] && end_timer() < 5) msleep(2);
4481
4482                         child_status_out[i] = fn(i);
4483                         _exit(0);
4484                 }
4485         }
4486
4487         do {
4488                 synccount = 0;
4489                 for (i=0;i<nprocs;i++) {
4490                         if (child_status[i]) synccount++;
4491                 }
4492                 if (synccount == nprocs) break;
4493                 msleep(10);
4494         } while (end_timer() < 30);
4495
4496         if (synccount != nprocs) {
4497                 printf("FAILED TO START %d CLIENTS (started %d)\n", nprocs, synccount);
4498                 *result = False;
4499                 return end_timer();
4500         }
4501
4502         /* start the client load */
4503         start_timer();
4504
4505         for (i=0;i<nprocs;i++) {
4506                 child_status[i] = 0;
4507         }
4508
4509         printf("%d clients started\n", nprocs);
4510
4511         for (i=0;i<nprocs;i++) {
4512                 while (waitpid(0, &status, 0) == -1 && errno == EINTR) /* noop */ ;
4513         }
4514
4515         printf("\n");
4516         
4517         for (i=0;i<nprocs;i++) {
4518                 if (!child_status_out[i]) {
4519                         *result = False;
4520                 }
4521         }
4522         return end_timer();
4523 }
4524
4525 #define FLAG_MULTIPROC 1
4526
4527 static struct {
4528         const char *name;
4529         BOOL (*fn)(int);
4530         unsigned flags;
4531 } torture_ops[] = {
4532         {"FDPASS", run_fdpasstest, 0},
4533         {"LOCK1",  run_locktest1,  0},
4534         {"LOCK2",  run_locktest2,  0},
4535         {"LOCK3",  run_locktest3,  0},
4536         {"LOCK4",  run_locktest4,  0},
4537         {"LOCK5",  run_locktest5,  0},
4538         {"LOCK6",  run_locktest6,  0},
4539         {"LOCK7",  run_locktest7,  0},
4540         {"UNLINK", run_unlinktest, 0},
4541         {"BROWSE", run_browsetest, 0},
4542         {"ATTR",   run_attrtest,   0},
4543         {"TRANS2", run_trans2test, 0},
4544         {"MAXFID", run_maxfidtest, FLAG_MULTIPROC},
4545         {"TORTURE",run_torture,    FLAG_MULTIPROC},
4546         {"RANDOMIPC", run_randomipc, 0},
4547         {"NEGNOWAIT", run_negprot_nowait, 0},
4548         {"NBENCH",  run_nbench, 0},
4549         {"OPLOCK1",  run_oplock1, 0},
4550         {"OPLOCK2",  run_oplock2, 0},
4551         {"OPLOCK3",  run_oplock3, 0},
4552         {"DIR",  run_dirtest, 0},
4553         {"DIR1",  run_dirtest1, 0},
4554         {"DENY1",  torture_denytest1, 0},
4555         {"DENY2",  torture_denytest2, 0},
4556         {"TCON",  run_tcon_test, 0},
4557         {"TCONDEV",  run_tcon_devtype_test, 0},
4558         {"RW1",  run_readwritetest, 0},
4559         {"RW2",  run_readwritemulti, FLAG_MULTIPROC},
4560         {"RW3",  run_readwritelarge, 0},
4561         {"OPEN", run_opentest, 0},
4562 #if 1
4563         {"OPENATTR", run_openattrtest, 0},
4564 #endif
4565         {"XCOPY", run_xcopy, 0},
4566         {"RENAME", run_rename, 0},
4567         {"DELETE", run_deletetest, 0},
4568         {"PROPERTIES", run_properties, 0},
4569         {"MANGLE", torture_mangle, 0},
4570         {"W2K", run_w2ktest, 0},
4571         {"TRANS2SCAN", torture_trans2_scan, 0},
4572         {"NTTRANSSCAN", torture_nttrans_scan, 0},
4573         {"UTABLE", torture_utable, 0},
4574         {"CASETABLE", torture_casetable, 0},
4575         {"ERRMAPEXTRACT", run_error_map_extract, 0},
4576         {"PIPE_NUMBER", run_pipe_number, 0},
4577         {"TCON2",  run_tcon2_test, 0},
4578         {"IOCTL",  torture_ioctl_test, 0},
4579         {"CHKPATH",  torture_chkpath_test, 0},
4580         {"FDSESS", run_fdsesstest, 0},
4581         {NULL, NULL, 0}};
4582
4583
4584
4585 /****************************************************************************
4586 run a specified test or "ALL"
4587 ****************************************************************************/
4588 static BOOL run_test(const char *name)
4589 {
4590         BOOL ret = True;
4591         BOOL result = True;
4592         BOOL found = False;
4593         int i;
4594         double t;
4595         if (strequal(name,"ALL")) {
4596                 for (i=0;torture_ops[i].name;i++) {
4597                         run_test(torture_ops[i].name);
4598                 }
4599                 found = True;
4600         }
4601         
4602         for (i=0;torture_ops[i].name;i++) {
4603                 fstr_sprintf(randomfname, "\\XX%x", 
4604                          (unsigned)random());
4605
4606                 if (strequal(name, torture_ops[i].name)) {
4607                         found = True;
4608                         printf("Running %s\n", name);
4609                         if (torture_ops[i].flags & FLAG_MULTIPROC) {
4610                                 t = create_procs(torture_ops[i].fn, &result);
4611                                 if (!result) { 
4612                                         ret = False;
4613                                         printf("TEST %s FAILED!\n", name);
4614                                 }
4615                                          
4616                         } else {
4617                                 start_timer();
4618                                 if (!torture_ops[i].fn(0)) {
4619                                         ret = False;
4620                                         printf("TEST %s FAILED!\n", name);
4621                                 }
4622                                 t = end_timer();
4623                         }
4624                         printf("%s took %g secs\n\n", name, t);
4625                 }
4626         }
4627
4628         if (!found) {
4629                 printf("Did not find a test named %s\n", name);
4630                 ret = False;
4631         }
4632
4633         return ret;
4634 }
4635
4636
4637 static void usage(void)
4638 {
4639         int i;
4640
4641         printf("Usage: smbtorture //server/share <options> TEST1 TEST2 ...\n");
4642
4643         printf("\t-d debuglevel\n");
4644         printf("\t-U user%%pass\n");
4645         printf("\t-k               use kerberos\n");
4646         printf("\t-N numprocs\n");
4647         printf("\t-n my_netbios_name\n");
4648         printf("\t-W workgroup\n");
4649         printf("\t-o num_operations\n");
4650         printf("\t-O socket_options\n");
4651         printf("\t-m maximum protocol\n");
4652         printf("\t-L use oplocks\n");
4653         printf("\t-c CLIENT.TXT   specify client load file for NBENCH\n");
4654         printf("\t-A showall\n");
4655         printf("\t-p port\n");
4656         printf("\t-s seed\n");
4657         printf("\n\n");
4658
4659         printf("tests are:");
4660         for (i=0;torture_ops[i].name;i++) {
4661                 printf(" %s", torture_ops[i].name);
4662         }
4663         printf("\n");
4664
4665         printf("default test is ALL\n");
4666         
4667         exit(1);
4668 }
4669
4670 /****************************************************************************
4671   main program
4672 ****************************************************************************/
4673  int main(int argc,char *argv[])
4674 {
4675         int opt, i;
4676         char *p;
4677         int gotuser = 0;
4678         int gotpass = 0;
4679         extern char *optarg;
4680         extern int optind;
4681         BOOL correct = True;
4682
4683         dbf = x_stdout;
4684
4685 #ifdef HAVE_SETBUFFER
4686         setbuffer(stdout, NULL, 0);
4687 #endif
4688
4689         lp_load(dyn_CONFIGFILE,True,False,False);
4690         load_interfaces();
4691
4692         if (argc < 2) {
4693                 usage();
4694         }
4695
4696         for(p = argv[1]; *p; p++)
4697           if(*p == '\\')
4698             *p = '/';
4699  
4700         if (strncmp(argv[1], "//", 2)) {
4701                 usage();
4702         }
4703
4704         fstrcpy(host, &argv[1][2]);
4705         p = strchr_m(&host[2],'/');
4706         if (!p) {
4707                 usage();
4708         }
4709         *p = 0;
4710         fstrcpy(share, p+1);
4711
4712         get_myname(myname);
4713
4714         if (*username == 0 && getenv("LOGNAME")) {
4715           fstrcpy(username,getenv("LOGNAME"));
4716         }
4717
4718         argc--;
4719         argv++;
4720
4721         srandom(time(NULL));
4722
4723         fstrcpy(workgroup, lp_workgroup());
4724
4725         while ((opt = getopt(argc, argv, "p:hW:U:n:N:O:o:m:Ld:Ac:ks:")) != EOF) {
4726                 switch (opt) {
4727                 case 'p':
4728                         port_to_use = atoi(optarg);
4729                         break;
4730                 case 's':
4731                         srandom(atoi(optarg));
4732                         break;
4733                 case 'W':
4734                         fstrcpy(workgroup,optarg);
4735                         break;
4736                 case 'm':
4737                         max_protocol = interpret_protocol(optarg, max_protocol);
4738                         break;
4739                 case 'N':
4740                         nprocs = atoi(optarg);
4741                         break;
4742                 case 'o':
4743                         torture_numops = atoi(optarg);
4744                         break;
4745                 case 'd':
4746                         DEBUGLEVEL = atoi(optarg);
4747                         break;
4748                 case 'O':
4749                         sockops = optarg;
4750                         break;
4751                 case 'L':
4752                         use_oplocks = True;
4753                         break;
4754                 case 'A':
4755                         torture_showall = True;
4756                         break;
4757                 case 'n':
4758                         fstrcpy(myname, optarg);
4759                         break;
4760                 case 'c':
4761                         client_txt = optarg;
4762                         break;
4763                 case 'k':
4764 #ifdef HAVE_KRB5
4765                         use_kerberos = True;
4766 #else
4767                         d_printf("No kerberos support compiled in\n");
4768                         exit(1);
4769 #endif
4770                         break;
4771                 case 'U':
4772                         gotuser = 1;
4773                         fstrcpy(username,optarg);
4774                         p = strchr_m(username,'%');
4775                         if (p) {
4776                                 *p = 0;
4777                                 fstrcpy(password, p+1);
4778                                 gotpass = 1;
4779                         }
4780                         break;
4781                 default:
4782                         printf("Unknown option %c (%d)\n", (char)opt, opt);
4783                         usage();
4784                 }
4785         }
4786
4787         if(use_kerberos && !gotuser) gotpass = True;
4788
4789         while (!gotpass) {
4790                 p = getpass("Password:");
4791                 if (p) {
4792                         fstrcpy(password, p);
4793                         gotpass = 1;
4794                 }
4795         }
4796
4797         printf("host=%s share=%s user=%s myname=%s\n", 
4798                host, share, username, myname);
4799
4800         if (argc == optind) {
4801                 correct = run_test("ALL");
4802         } else {
4803                 for (i=optind;i<argc;i++) {
4804                         if (!run_test(argv[i])) {
4805                                 correct = False;
4806                         }
4807                 }
4808         }
4809
4810         if (correct) {
4811                 return(0);
4812         } else {
4813                 return(1);
4814         }
4815 }