r3148: make --failures work for the BASE-DENY1 and BASE-DENY2 tests
[bbaumbach/samba-autobuild/.git] / source4 / torture / torture.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Andrew Tridgell 1997-2003
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 #include "includes.h"
22
23 int torture_nprocs=4;
24 int torture_numops=100;
25 int torture_entries=1000;
26 int torture_failures=1;
27 static int procnum; /* records process count number when forking */
28 static struct smbcli_state *current_cli;
29 static BOOL use_oplocks;
30 static BOOL use_level_II_oplocks;
31 static BOOL use_kerberos;
32
33 BOOL torture_showall = False;
34
35 #define CHECK_MAX_FAILURES(label) do { if (++failures >= torture_failures) goto label; } while (0)
36
37 static struct smbcli_state *open_nbt_connection(void)
38 {
39         struct nmb_name called, calling;
40         struct smbcli_state *cli;
41         const char *host = lp_parm_string(-1, "torture", "host");
42
43         make_nmb_name(&calling, lp_netbios_name(), 0x0);
44         choose_called_name(&called, host, 0x20);
45
46         cli = smbcli_state_init(NULL);
47         if (!cli) {
48                 printf("Failed initialize smbcli_struct to connect with %s\n", host);
49                 return NULL;
50         }
51
52         if (!smbcli_socket_connect(cli, host)) {
53                 printf("Failed to connect with %s\n", host);
54                 return cli;
55         }
56
57         cli->transport->socket->timeout = 120000; /* set a really long timeout (2 minutes) */
58
59         if (!smbcli_transport_establish(cli, &calling, &called)) {
60                 /*
61                  * Well, that failed, try *SMBSERVER ... 
62                  * However, we must reconnect as well ...
63                  */
64                 if (!smbcli_socket_connect(cli, host)) {
65                         printf("Failed to connect with %s\n", host);
66                         return False;
67                 }
68
69                 make_nmb_name(&called, "*SMBSERVER", 0x20);
70                 if (!smbcli_transport_establish(cli, &calling, &called)) {
71                         printf("%s rejected the session\n",host);
72                         printf("We tried with a called name of %s & %s\n",
73                                 host, "*SMBSERVER");
74                         smbcli_shutdown(cli);
75                         return NULL;
76                 }
77         }
78
79         return cli;
80 }
81
82 BOOL torture_open_connection_share(struct smbcli_state **c, 
83                                    const char *hostname, 
84                                    const char *sharename)
85 {
86         BOOL retry;
87         int flags = 0;
88         NTSTATUS status;
89         const char *username = lp_parm_string(-1, "torture", "username");
90         const char *userdomain = lp_parm_string(-1, "torture", "userdomain");
91         const char *password = lp_parm_string(-1, "torture", "password");
92
93         if (use_kerberos)
94                 flags |= SMBCLI_FULL_CONNECTION_USE_KERBEROS;
95
96         status = smbcli_full_connection(NULL,
97                                         c, lp_netbios_name(),
98                                         hostname, NULL, 
99                                         sharename, "?????", 
100                                         username, username[0]?userdomain:"",
101                                         password, flags, &retry);
102         if (!NT_STATUS_IS_OK(status)) {
103                 printf("Failed to open connection - %s\n", nt_errstr(status));
104                 return False;
105         }
106
107         (*c)->transport->options.use_oplocks = use_oplocks;
108         (*c)->transport->options.use_level2_oplocks = use_level_II_oplocks;
109         (*c)->transport->socket->timeout = 120000;
110
111         return True;
112 }
113
114 BOOL torture_open_connection(struct smbcli_state **c)
115 {
116         const char *host = lp_parm_string(-1, "torture", "host");
117         const char *share = lp_parm_string(-1, "torture", "share");
118
119         return torture_open_connection_share(c, host, share);
120 }
121
122
123
124 BOOL torture_close_connection(struct smbcli_state *c)
125 {
126         BOOL ret = True;
127         if (!c) return True;
128         if (NT_STATUS_IS_ERR(smbcli_tdis(c))) {
129                 printf("tdis failed (%s)\n", smbcli_errstr(c->tree));
130                 ret = False;
131         }
132         smbcli_shutdown(c);
133         return ret;
134 }
135
136
137 /* open a rpc connection to the chosen binding string */
138 NTSTATUS torture_rpc_connection(struct dcerpc_pipe **p, 
139                                 const char *pipe_name,
140                                 const char *pipe_uuid, 
141                                 uint32_t pipe_version)
142 {
143         NTSTATUS status;
144         const char *binding = lp_parm_string(-1, "torture", "binding");
145
146         if (!binding) {
147                 printf("You must specify a ncacn binding string\n");
148                 return NT_STATUS_INVALID_PARAMETER;
149         }
150
151         status = dcerpc_pipe_connect(p, binding, pipe_uuid, pipe_version,
152                                      lp_parm_string(-1, "torture", "userdomain"), 
153                                      lp_parm_string(-1, "torture", "username"),
154                                      lp_parm_string(-1, "torture", "password"));
155  
156         return status;
157 }
158
159 /* open a rpc connection to a specific transport */
160 NTSTATUS torture_rpc_connection_transport(struct dcerpc_pipe **p, 
161                                           const char *pipe_name,
162                                           const char *pipe_uuid, 
163                                           uint32_t pipe_version,
164                                           enum dcerpc_transport_t transport)
165 {
166         NTSTATUS status;
167         const char *binding = lp_parm_string(-1, "torture", "binding");
168         struct dcerpc_binding b;
169         TALLOC_CTX *mem_ctx = talloc_init("torture_rpc_connection_smb");
170
171         if (!binding) {
172                 printf("You must specify a ncacn binding string\n");
173                 return NT_STATUS_INVALID_PARAMETER;
174         }
175
176         status = dcerpc_parse_binding(mem_ctx, binding, &b);
177         if (!NT_STATUS_IS_OK(status)) {
178                 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
179                 talloc_destroy(mem_ctx);
180                 return status;
181         }
182
183         b.transport = transport;
184
185         status = dcerpc_pipe_connect_b(p, &b, pipe_uuid, pipe_version,
186                                        lp_parm_string(-1, "torture", "userdomain"), 
187                                        lp_parm_string(-1, "torture", "username"),
188                                        lp_parm_string(-1, "torture", "password"));
189  
190         return status;
191 }
192
193 /* close a rpc connection to a named pipe */
194 NTSTATUS torture_rpc_close(struct dcerpc_pipe *p)
195 {
196         dcerpc_pipe_close(p);
197         return NT_STATUS_OK;
198 }
199
200
201 /* check if the server produced the expected error code */
202 BOOL check_error(const char *location, struct smbcli_state *c, 
203                  uint8_t eclass, uint32_t ecode, NTSTATUS nterr)
204 {
205         if (smbcli_is_dos_error(c->tree)) {
206                 uint8_t class;
207                 uint32_t num;
208
209                 /* Check DOS error */
210
211                 smbcli_dos_error(c, &class, &num);
212
213                 if (eclass != class || ecode != num) {
214                         printf("unexpected error code class=%d code=%d\n", 
215                                (int)class, (int)num);
216                         printf(" expected %d/%d %s (at %s)\n", 
217                                (int)eclass, (int)ecode, nt_errstr(nterr), location);
218                         return False;
219                 }
220
221         } else {
222                 NTSTATUS status;
223
224                 /* Check NT error */
225
226                 status = smbcli_nt_error(c->tree);
227
228                 if (NT_STATUS_V(nterr) != NT_STATUS_V(status)) {
229                         printf("unexpected error code %s\n", nt_errstr(status));
230                         printf(" expected %s (at %s)\n", nt_errstr(nterr), location);
231                         return False;
232                 }
233         }
234
235         return True;
236 }
237
238
239 static BOOL wait_lock(struct smbcli_state *c, int fnum, uint32_t offset, uint32_t len)
240 {
241         while (NT_STATUS_IS_ERR(smbcli_lock(c->tree, fnum, offset, len, -1, WRITE_LOCK))) {
242                 if (!check_error(__location__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
243         }
244         return True;
245 }
246
247
248 static BOOL rw_torture(struct smbcli_state *c)
249 {
250         const char *lockfname = "\\torture.lck";
251         char *fname;
252         int fnum;
253         int fnum2;
254         pid_t pid2, pid = getpid();
255         int i, j;
256         char buf[1024];
257         BOOL correct = True;
258
259         fnum2 = smbcli_open(c->tree, lockfname, O_RDWR | O_CREAT | O_EXCL, 
260                          DENY_NONE);
261         if (fnum2 == -1)
262                 fnum2 = smbcli_open(c->tree, lockfname, O_RDWR, DENY_NONE);
263         if (fnum2 == -1) {
264                 printf("open of %s failed (%s)\n", lockfname, smbcli_errstr(c->tree));
265                 return False;
266         }
267
268
269         for (i=0;i<torture_numops;i++) {
270                 uint_t n = (uint_t)sys_random()%10;
271                 if (i % 10 == 0) {
272                         printf("%d\r", i); fflush(stdout);
273                 }
274                 asprintf(&fname, "\\torture.%u", n);
275
276                 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
277                         return False;
278                 }
279
280                 fnum = smbcli_open(c->tree, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL);
281                 if (fnum == -1) {
282                         printf("open failed (%s)\n", smbcli_errstr(c->tree));
283                         correct = False;
284                         break;
285                 }
286
287                 if (smbcli_write(c->tree, fnum, 0, (char *)&pid, 0, sizeof(pid)) != sizeof(pid)) {
288                         printf("write failed (%s)\n", smbcli_errstr(c->tree));
289                         correct = False;
290                 }
291
292                 for (j=0;j<50;j++) {
293                         if (smbcli_write(c->tree, fnum, 0, (char *)buf, 
294                                       sizeof(pid)+(j*sizeof(buf)), 
295                                       sizeof(buf)) != sizeof(buf)) {
296                                 printf("write failed (%s)\n", smbcli_errstr(c->tree));
297                                 correct = False;
298                         }
299                 }
300
301                 pid2 = 0;
302
303                 if (smbcli_read(c->tree, fnum, (char *)&pid2, 0, sizeof(pid)) != sizeof(pid)) {
304                         printf("read failed (%s)\n", smbcli_errstr(c->tree));
305                         correct = False;
306                 }
307
308                 if (pid2 != pid) {
309                         printf("data corruption!\n");
310                         correct = False;
311                 }
312
313                 if (NT_STATUS_IS_ERR(smbcli_close(c->tree, fnum))) {
314                         printf("close failed (%s)\n", smbcli_errstr(c->tree));
315                         correct = False;
316                 }
317
318                 if (NT_STATUS_IS_ERR(smbcli_unlink(c->tree, fname))) {
319                         printf("unlink failed (%s)\n", smbcli_errstr(c->tree));
320                         correct = False;
321                 }
322
323                 if (NT_STATUS_IS_ERR(smbcli_unlock(c->tree, fnum2, n*sizeof(int), sizeof(int)))) {
324                         printf("unlock failed (%s)\n", smbcli_errstr(c->tree));
325                         correct = False;
326                 }
327                 free(fname);
328         }
329
330         smbcli_close(c->tree, fnum2);
331         smbcli_unlink(c->tree, lockfname);
332
333         printf("%d\n", i);
334
335         return correct;
336 }
337
338 static BOOL run_torture(struct smbcli_state *cli, int dummy)
339 {
340         BOOL ret;
341
342         ret = rw_torture(cli);
343         
344         if (!torture_close_connection(cli)) {
345                 ret = False;
346         }
347
348         return ret;
349 }
350
351 static BOOL rw_torture3(struct smbcli_state *c, const char *lockfname)
352 {
353         int fnum = -1;
354         uint_t i = 0;
355         char buf[131072];
356         char buf_rd[131072];
357         uint_t count;
358         uint_t countprev = 0;
359         ssize_t sent = 0;
360         BOOL correct = True;
361
362         srandom(1);
363         for (i = 0; i < sizeof(buf); i += sizeof(uint32_t))
364         {
365                 SIVAL(buf, i, sys_random());
366         }
367
368         if (procnum == 0)
369         {
370                 fnum = smbcli_open(c->tree, lockfname, O_RDWR | O_CREAT | O_EXCL, 
371                                 DENY_NONE);
372                 if (fnum == -1) {
373                         printf("first open read/write of %s failed (%s)\n",
374                                         lockfname, smbcli_errstr(c->tree));
375                         return False;
376                 }
377         }
378         else
379         {
380                 for (i = 0; i < 500 && fnum == -1; i++)
381                 {
382                         fnum = smbcli_open(c->tree, lockfname, O_RDONLY, 
383                                         DENY_NONE);
384                         msleep(10);
385                 }
386                 if (fnum == -1) {
387                         printf("second open read-only of %s failed (%s)\n",
388                                         lockfname, smbcli_errstr(c->tree));
389                         return False;
390                 }
391         }
392
393         i = 0;
394         for (count = 0; count < sizeof(buf); count += sent)
395         {
396                 if (count >= countprev) {
397                         printf("%d %8d\r", i, count);
398                         fflush(stdout);
399                         i++;
400                         countprev += (sizeof(buf) / 20);
401                 }
402
403                 if (procnum == 0)
404                 {
405                         sent = ((uint_t)sys_random()%(20))+ 1;
406                         if (sent > sizeof(buf) - count)
407                         {
408                                 sent = sizeof(buf) - count;
409                         }
410
411                         if (smbcli_write(c->tree, fnum, 0, buf+count, count, (size_t)sent) != sent) {
412                                 printf("write failed (%s)\n", smbcli_errstr(c->tree));
413                                 correct = False;
414                         }
415                 }
416                 else
417                 {
418                         sent = smbcli_read(c->tree, fnum, buf_rd+count, count,
419                                         sizeof(buf)-count);
420                         if (sent < 0)
421                         {
422                                 printf("read failed offset:%d size:%d (%s)\n",
423                                                 count, sizeof(buf)-count,
424                                                 smbcli_errstr(c->tree));
425                                 correct = False;
426                                 sent = 0;
427                         }
428                         if (sent > 0)
429                         {
430                                 if (memcmp(buf_rd+count, buf+count, sent) != 0)
431                                 {
432                                         printf("read/write compare failed\n");
433                                         printf("offset: %d req %d recvd %d\n",
434                                                 count, sizeof(buf)-count, sent);
435                                         correct = False;
436                                         break;
437                                 }
438                         }
439                 }
440
441         }
442
443         if (NT_STATUS_IS_ERR(smbcli_close(c->tree, fnum))) {
444                 printf("close failed (%s)\n", smbcli_errstr(c->tree));
445                 correct = False;
446         }
447
448         return correct;
449 }
450
451 static BOOL rw_torture2(struct smbcli_state *c1, struct smbcli_state *c2)
452 {
453         const char *lockfname = "\\torture2.lck";
454         int fnum1;
455         int fnum2;
456         int i;
457         uint8_t buf[131072];
458         uint8_t buf_rd[131072];
459         BOOL correct = True;
460         ssize_t bytes_read, bytes_written;
461
462         if (smbcli_deltree(c1->tree, lockfname) == -1) {
463                 printf("unlink failed (%s)\n", smbcli_errstr(c1->tree));
464         }
465
466         fnum1 = smbcli_open(c1->tree, lockfname, O_RDWR | O_CREAT | O_EXCL, 
467                          DENY_NONE);
468         if (fnum1 == -1) {
469                 printf("first open read/write of %s failed (%s)\n",
470                                 lockfname, smbcli_errstr(c1->tree));
471                 return False;
472         }
473         fnum2 = smbcli_open(c2->tree, lockfname, O_RDONLY, 
474                          DENY_NONE);
475         if (fnum2 == -1) {
476                 printf("second open read-only of %s failed (%s)\n",
477                                 lockfname, smbcli_errstr(c2->tree));
478                 smbcli_close(c1->tree, fnum1);
479                 return False;
480         }
481
482         printf("Checking data integrity over %d ops\n", torture_numops);
483
484         for (i=0;i<torture_numops;i++)
485         {
486                 size_t buf_size = ((uint_t)sys_random()%(sizeof(buf)-1))+ 1;
487                 if (i % 10 == 0) {
488                         printf("%d\r", i); fflush(stdout);
489                 }
490
491                 generate_random_buffer(buf, buf_size);
492
493                 if ((bytes_written = smbcli_write(c1->tree, fnum1, 0, buf, 0, buf_size)) != buf_size) {
494                         printf("write failed (%s)\n", smbcli_errstr(c1->tree));
495                         printf("wrote %d, expected %d\n", bytes_written, buf_size); 
496                         correct = False;
497                         break;
498                 }
499
500                 if ((bytes_read = smbcli_read(c2->tree, fnum2, buf_rd, 0, buf_size)) != buf_size) {
501                         printf("read failed (%s)\n", smbcli_errstr(c2->tree));
502                         printf("read %d, expected %d\n", bytes_read, buf_size); 
503                         correct = False;
504                         break;
505                 }
506
507                 if (memcmp(buf_rd, buf, buf_size) != 0)
508                 {
509                         printf("read/write compare failed\n");
510                         correct = False;
511                         break;
512                 }
513         }
514
515         if (NT_STATUS_IS_ERR(smbcli_close(c2->tree, fnum2))) {
516                 printf("close failed (%s)\n", smbcli_errstr(c2->tree));
517                 correct = False;
518         }
519         if (NT_STATUS_IS_ERR(smbcli_close(c1->tree, fnum1))) {
520                 printf("close failed (%s)\n", smbcli_errstr(c1->tree));
521                 correct = False;
522         }
523
524         if (NT_STATUS_IS_ERR(smbcli_unlink(c1->tree, lockfname))) {
525                 printf("unlink failed (%s)\n", smbcli_errstr(c1->tree));
526                 correct = False;
527         }
528
529         return correct;
530 }
531
532 static BOOL run_readwritetest(int dummy)
533 {
534         struct smbcli_state *cli1, *cli2;
535         BOOL test1, test2 = True;
536
537         if (!torture_open_connection(&cli1) || !torture_open_connection(&cli2)) {
538                 return False;
539         }
540
541         printf("starting readwritetest\n");
542
543         test1 = rw_torture2(cli1, cli2);
544         printf("Passed readwritetest v1: %s\n", BOOLSTR(test1));
545
546         if (test1) {
547                 test2 = rw_torture2(cli1, cli1);
548                 printf("Passed readwritetest v2: %s\n", BOOLSTR(test2));
549         }
550
551         if (!torture_close_connection(cli1)) {
552                 test1 = False;
553         }
554
555         if (!torture_close_connection(cli2)) {
556                 test2 = False;
557         }
558
559         return (test1 && test2);
560 }
561
562 static BOOL run_readwritemulti(struct smbcli_state *cli, int dummy)
563 {
564         BOOL test;
565
566         test = rw_torture3(cli, "\\multitest.txt");
567
568         if (!torture_close_connection(cli)) {
569                 test = False;
570         }
571         
572         return test;
573 }
574
575
576 /*
577   this checks to see if a secondary tconx can use open files from an
578   earlier tconx
579  */
580 static BOOL run_tcon_test(int dummy)
581 {
582         struct smbcli_state *cli;
583         const char *fname = "\\tcontest.tmp";
584         int fnum1;
585         uint16_t cnum1, cnum2, cnum3;
586         uint16_t vuid1, vuid2;
587         char buf[4];
588         BOOL ret = True;
589         struct smbcli_tree *tree1;
590         const char *host = lp_parm_string(-1, "torture", "host");
591         const char *share = lp_parm_string(-1, "torture", "share");
592         const char *password = lp_parm_string(-1, "torture", "password");
593
594         if (!torture_open_connection(&cli)) {
595                 return False;
596         }
597
598         printf("starting tcontest\n");
599
600         if (smbcli_deltree(cli->tree, fname) == -1) {
601                 printf("unlink of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
602         }
603
604         fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
605         if (fnum1 == -1) {
606                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
607                 return False;
608         }
609
610         cnum1 = cli->tree->tid;
611         vuid1 = cli->session->vuid;
612
613         memset(&buf, 0, 4); /* init buf so valgrind won't complain */
614         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) != 4) {
615                 printf("initial write failed (%s)\n", smbcli_errstr(cli->tree));
616                 return False;
617         }
618
619         tree1 = cli->tree;      /* save old tree connection */
620         if (NT_STATUS_IS_ERR(smbcli_send_tconX(cli, share, "?????", password))) {
621                 printf("%s refused 2nd tree connect (%s)\n", host,
622                            smbcli_errstr(cli->tree));
623                 smbcli_shutdown(cli);
624                 return False;
625         }
626
627         cnum2 = cli->tree->tid;
628         cnum3 = MAX(cnum1, cnum2) + 1; /* any invalid number */
629         vuid2 = cli->session->vuid + 1;
630
631         /* try a write with the wrong tid */
632         cli->tree->tid = cnum2;
633
634         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) == 4) {
635                 printf("* server allows write with wrong TID\n");
636                 ret = False;
637         } else {
638                 printf("server fails write with wrong TID : %s\n", smbcli_errstr(cli->tree));
639         }
640
641
642         /* try a write with an invalid tid */
643         cli->tree->tid = cnum3;
644
645         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) == 4) {
646                 printf("* server allows write with invalid TID\n");
647                 ret = False;
648         } else {
649                 printf("server fails write with invalid TID : %s\n", smbcli_errstr(cli->tree));
650         }
651
652         /* try a write with an invalid vuid */
653         cli->session->vuid = vuid2;
654         cli->tree->tid = cnum1;
655
656         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) == 4) {
657                 printf("* server allows write with invalid VUID\n");
658                 ret = False;
659         } else {
660                 printf("server fails write with invalid VUID : %s\n", smbcli_errstr(cli->tree));
661         }
662
663         cli->session->vuid = vuid1;
664         cli->tree->tid = cnum1;
665
666         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum1))) {
667                 printf("close failed (%s)\n", smbcli_errstr(cli->tree));
668                 return False;
669         }
670
671         cli->tree->tid = cnum2;
672
673         if (NT_STATUS_IS_ERR(smbcli_tdis(cli))) {
674                 printf("secondary tdis failed (%s)\n", smbcli_errstr(cli->tree));
675                 return False;
676         }
677
678         cli->tree = tree1;  /* restore initial tree */
679         cli->tree->tid = cnum1;
680
681         if (!torture_close_connection(cli)) {
682                 return False;
683         }
684
685         return ret;
686 }
687
688
689
690 static BOOL tcon_devtest(struct smbcli_state *cli,
691                          const char *myshare, const char *devtype,
692                          NTSTATUS expected_error)
693 {
694         BOOL status;
695         BOOL ret;
696         const char *password = lp_parm_string(-1, "torture", "password");
697
698         status = NT_STATUS_IS_OK(smbcli_send_tconX(cli, myshare, devtype, 
699                                                 password));
700
701         printf("Trying share %s with devtype %s\n", myshare, devtype);
702
703         if (NT_STATUS_IS_OK(expected_error)) {
704                 if (status) {
705                         ret = True;
706                 } else {
707                         printf("tconX to share %s with type %s "
708                                "should have succeeded but failed\n",
709                                myshare, devtype);
710                         ret = False;
711                 }
712                 smbcli_tdis(cli);
713         } else {
714                 if (status) {
715                         printf("tconx to share %s with type %s "
716                                "should have failed but succeeded\n",
717                                myshare, devtype);
718                         ret = False;
719                 } else {
720                         if (NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),
721                                             expected_error)) {
722                                 ret = True;
723                         } else {
724                                 printf("Returned unexpected error\n");
725                                 ret = False;
726                         }
727                 }
728         }
729         return ret;
730 }
731
732 /*
733  checks for correct tconX support
734  */
735 static BOOL run_tcon_devtype_test(int dummy)
736 {
737         struct smbcli_state *cli1 = NULL;
738         BOOL retry;
739         int flags = 0;
740         NTSTATUS status;
741         BOOL ret = True;
742         const char *host = lp_parm_string(-1, "torture", "host");
743         const char *share = lp_parm_string(-1, "torture", "share");
744         const char *username = lp_parm_string(-1, "torture", "username");
745         const char *userdomain = lp_parm_string(-1, "torture", "userdomain");
746         const char *password = lp_parm_string(-1, "torture", "password");
747         
748         status = smbcli_full_connection(NULL,
749                                         &cli1, lp_netbios_name(),
750                                         host, NULL, 
751                                         share, "?????",
752                                         username, userdomain,
753                                         password, flags, &retry);
754
755         if (!NT_STATUS_IS_OK(status)) {
756                 printf("could not open connection\n");
757                 return False;
758         }
759
760         if (!tcon_devtest(cli1, "IPC$", "A:", NT_STATUS_BAD_DEVICE_TYPE))
761                 ret = False;
762
763         if (!tcon_devtest(cli1, "IPC$", "?????", NT_STATUS_OK))
764                 ret = False;
765
766         if (!tcon_devtest(cli1, "IPC$", "LPT:", NT_STATUS_BAD_DEVICE_TYPE))
767                 ret = False;
768
769         if (!tcon_devtest(cli1, "IPC$", "IPC", NT_STATUS_OK))
770                 ret = False;
771                         
772         if (!tcon_devtest(cli1, "IPC$", "FOOBA", NT_STATUS_BAD_DEVICE_TYPE))
773                 ret = False;
774
775         if (!tcon_devtest(cli1, share, "A:", NT_STATUS_OK))
776                 ret = False;
777
778         if (!tcon_devtest(cli1, share, "?????", NT_STATUS_OK))
779                 ret = False;
780
781         if (!tcon_devtest(cli1, share, "LPT:", NT_STATUS_BAD_DEVICE_TYPE))
782                 ret = False;
783
784         if (!tcon_devtest(cli1, share, "IPC", NT_STATUS_BAD_DEVICE_TYPE))
785                 ret = False;
786                         
787         if (!tcon_devtest(cli1, share, "FOOBA", NT_STATUS_BAD_DEVICE_TYPE))
788                 ret = False;
789
790         smbcli_shutdown(cli1);
791
792         if (ret)
793                 printf("Passed tcondevtest\n");
794
795         return ret;
796 }
797
798
799 /*
800 test whether fnums and tids open on one VC are available on another (a major
801 security hole)
802 */
803 static BOOL run_fdpasstest(int dummy)
804 {
805         struct smbcli_state *cli1, *cli2;
806         const char *fname = "\\fdpass.tst";
807         int fnum1, oldtid;
808         pstring buf;
809
810         if (!torture_open_connection(&cli1) || !torture_open_connection(&cli2)) {
811                 return False;
812         }
813
814         printf("starting fdpasstest\n");
815
816         smbcli_unlink(cli1->tree, fname);
817
818         printf("Opening a file on connection 1\n");
819
820         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
821         if (fnum1 == -1) {
822                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
823                 return False;
824         }
825
826         printf("writing to file on connection 1\n");
827
828         if (smbcli_write(cli1->tree, fnum1, 0, "hello world\n", 0, 13) != 13) {
829                 printf("write failed (%s)\n", smbcli_errstr(cli1->tree));
830                 return False;
831         }
832
833         oldtid = cli2->tree->tid;
834         cli2->session->vuid = cli1->session->vuid;
835         cli2->tree->tid = cli1->tree->tid;
836         cli2->session->pid = cli1->session->pid;
837
838         printf("reading from file on connection 2\n");
839
840         if (smbcli_read(cli2->tree, fnum1, buf, 0, 13) == 13) {
841                 printf("read succeeded! nasty security hole [%s]\n",
842                        buf);
843                 return False;
844         }
845
846         smbcli_close(cli1->tree, fnum1);
847         smbcli_unlink(cli1->tree, fname);
848
849         cli2->tree->tid = oldtid;
850
851         torture_close_connection(cli1);
852         torture_close_connection(cli2);
853
854         printf("finished fdpasstest\n");
855         return True;
856 }
857
858
859 /*
860   This test checks that 
861
862   1) the server does not allow an unlink on a file that is open
863 */
864 static BOOL run_unlinktest(int dummy)
865 {
866         struct smbcli_state *cli;
867         const char *fname = "\\unlink.tst";
868         int fnum;
869         BOOL correct = True;
870
871         if (!torture_open_connection(&cli)) {
872                 return False;
873         }
874
875         printf("starting unlink test\n");
876
877         smbcli_unlink(cli->tree, fname);
878
879         cli->session->pid = 1;
880
881         printf("Opening a file\n");
882
883         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
884         if (fnum == -1) {
885                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
886                 return False;
887         }
888
889         printf("Unlinking a open file\n");
890
891         if (NT_STATUS_IS_OK(smbcli_unlink(cli->tree, fname))) {
892                 printf("error: server allowed unlink on an open file\n");
893                 correct = False;
894         } else {
895                 correct = check_error(__location__, cli, ERRDOS, ERRbadshare, 
896                                       NT_STATUS_SHARING_VIOLATION);
897         }
898
899         smbcli_close(cli->tree, fnum);
900         smbcli_unlink(cli->tree, fname);
901
902         if (!torture_close_connection(cli)) {
903                 correct = False;
904         }
905
906         printf("unlink test finished\n");
907         
908         return correct;
909 }
910
911
912 /*
913 test the timing of deferred open requests
914 */
915 static BOOL run_deferopen(struct smbcli_state *cli, int dummy)
916 {
917         const char *fname = "\\defer_open_test.dat";
918         int retries=4;
919         int i = 0;
920         BOOL correct = True;
921
922         if (retries <= 0) {
923                 printf("failed to connect\n");
924                 return False;
925         }
926
927         printf("Testing deferred open requests.\n");
928
929         while (i < 4) {
930                 int fnum = -1;
931
932                 do {
933                         struct timeval tv_start, tv_end;
934                         GetTimeOfDay(&tv_start);
935                         fnum = smbcli_nt_create_full(cli->tree, fname, 0, GENERIC_RIGHTS_FILE_ALL_ACCESS,
936                                 FILE_ATTRIBUTE_NORMAL, NTCREATEX_SHARE_ACCESS_NONE,
937                                 NTCREATEX_DISP_OPEN_IF, 0, 0);
938                         if (fnum != -1) {
939                                 break;
940                         }
941                         GetTimeOfDay(&tv_end);
942                         if (NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),NT_STATUS_SHARING_VIOLATION)) {
943                                 /* Sharing violation errors need to be 1 second apart. */
944                                 int64_t tdif = usec_time_diff(&tv_end, &tv_start);
945                                 if (tdif < 500000 || tdif > 1500000) {
946                                         fprintf(stderr,"Timing incorrect %lld.%lld for share violation\n",
947                                                 tdif / (int64_t)1000000, 
948                                                 tdif % (int64_t)1000000);
949                                 }
950                         }
951                 } while (NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),NT_STATUS_SHARING_VIOLATION));
952
953                 if (fnum == -1) {
954                         fprintf(stderr,"Failed to open %s, error=%s\n", fname, smbcli_errstr(cli->tree));
955                         return False;
956                 }
957
958                 printf("pid %u open %d\n", getpid(), i);
959
960                 sleep(10);
961                 i++;
962                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
963                         fprintf(stderr,"Failed to close %s, error=%s\n", fname, smbcli_errstr(cli->tree));
964                         return False;
965                 }
966                 sleep(2);
967         }
968
969         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
970                 /* All until the last unlink will fail with sharing violation. */
971                 if (!NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),NT_STATUS_SHARING_VIOLATION)) {
972                         printf("unlink of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
973                         correct = False;
974                 }
975         }
976
977         printf("deferred test finished\n");
978         if (!torture_close_connection(cli)) {
979                 correct = False;
980         }
981         return correct;
982 }
983
984 /*
985 test how many open files this server supports on the one socket
986 */
987 static BOOL run_maxfidtest(struct smbcli_state *cli, int dummy)
988 {
989 #define MAXFID_TEMPLATE "\\maxfid\\fid%d\\maxfid.%d.%d"
990         char *fname;
991         int fnums[0x11000], i;
992         int retries=4, maxfid;
993         BOOL correct = True;
994
995         if (retries <= 0) {
996                 printf("failed to connect\n");
997                 return False;
998         }
999
1000         if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
1001                 printf("Failed to deltree \\maxfid - %s\n",
1002                        smbcli_errstr(cli->tree));
1003                 return False;
1004         }
1005         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\maxfid"))) {
1006                 printf("Failed to mkdir \\maxfid, error=%s\n", 
1007                        smbcli_errstr(cli->tree));
1008                 return False;
1009         }
1010
1011         printf("Testing maximum number of open files\n");
1012
1013         for (i=0; i<0x11000; i++) {
1014                 if (i % 1000 == 0) {
1015                         asprintf(&fname, "\\maxfid\\fid%d", i/1000);
1016                         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, fname))) {
1017                                 printf("Failed to mkdir %s, error=%s\n", 
1018                                        fname, smbcli_errstr(cli->tree));
1019                                 return False;
1020                         }
1021                         free(fname);
1022                 }
1023                 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
1024                 if ((fnums[i] = smbcli_open(cli->tree, fname, 
1025                                         O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) ==
1026                     -1) {
1027                         printf("open of %s failed (%s)\n", 
1028                                fname, smbcli_errstr(cli->tree));
1029                         printf("maximum fnum is %d\n", i);
1030                         break;
1031                 }
1032                 free(fname);
1033                 printf("%6d\r", i);
1034         }
1035         printf("%6d\n", i);
1036         i--;
1037
1038         maxfid = i;
1039
1040         printf("cleaning up\n");
1041         for (i=0;i<maxfid/2;i++) {
1042                 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
1043                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[i]))) {
1044                         printf("Close of fnum %d failed - %s\n", fnums[i], smbcli_errstr(cli->tree));
1045                 }
1046                 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
1047                         printf("unlink of %s failed (%s)\n", 
1048                                fname, smbcli_errstr(cli->tree));
1049                         correct = False;
1050                 }
1051                 free(fname);
1052
1053                 asprintf(&fname, MAXFID_TEMPLATE, (maxfid-i)/1000, maxfid-i,(int)getpid());
1054                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[maxfid-i]))) {
1055                         printf("Close of fnum %d failed - %s\n", fnums[maxfid-i], smbcli_errstr(cli->tree));
1056                 }
1057                 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
1058                         printf("unlink of %s failed (%s)\n", 
1059                                fname, smbcli_errstr(cli->tree));
1060                         correct = False;
1061                 }
1062                 free(fname);
1063
1064                 printf("%6d %6d\r", i, maxfid-i);
1065         }
1066         printf("%6d\n", 0);
1067
1068         if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
1069                 printf("Failed to deltree \\maxfid - %s\n",
1070                        smbcli_errstr(cli->tree));
1071                 return False;
1072         }
1073
1074         printf("maxfid test finished\n");
1075         if (!torture_close_connection(cli)) {
1076                 correct = False;
1077         }
1078         return correct;
1079 #undef MAXFID_TEMPLATE
1080 }
1081
1082 /* send smb negprot commands, not reading the response */
1083 static BOOL run_negprot_nowait(int dummy)
1084 {
1085         int i;
1086         struct smbcli_state *cli, *cli2;
1087         BOOL correct = True;
1088
1089         printf("starting negprot nowait test\n");
1090
1091         cli = open_nbt_connection();
1092         if (!cli) {
1093                 return False;
1094         }
1095
1096         printf("Filling send buffer\n");
1097
1098         for (i=0;i<10000;i++) {
1099                 struct smbcli_request *req;
1100                 time_t t1 = time(NULL);
1101                 req = smb_negprot_send(cli->transport, PROTOCOL_NT1);
1102                 while (req->state == SMBCLI_REQUEST_SEND && time(NULL) < t1+5) {
1103                         smbcli_transport_process(cli->transport);
1104                 }
1105                 if (req->state == SMBCLI_REQUEST_ERROR) {
1106                         printf("Failed to fill pipe - %s\n", nt_errstr(req->status));
1107                         torture_close_connection(cli);
1108                         return correct;
1109                 }
1110                 if (req->state == SMBCLI_REQUEST_SEND) {
1111                         break;
1112                 }
1113         }
1114
1115         if (i == 10000) {
1116                 printf("send buffer failed to fill\n");
1117                 if (!torture_close_connection(cli)) {
1118                         correct = False;
1119                 }
1120                 return correct;
1121         }
1122
1123         printf("send buffer filled after %d requests\n", i);
1124
1125         printf("Opening secondary connection\n");
1126         if (!torture_open_connection(&cli2)) {
1127                 return False;
1128         }
1129
1130         if (!torture_close_connection(cli)) {
1131                 correct = False;
1132         }
1133
1134         if (!torture_close_connection(cli2)) {
1135                 correct = False;
1136         }
1137
1138         printf("finished negprot nowait test\n");
1139
1140         return correct;
1141 }
1142
1143
1144 /*
1145   This checks how the getatr calls works
1146 */
1147 static BOOL run_attrtest(int dummy)
1148 {
1149         struct smbcli_state *cli;
1150         int fnum;
1151         time_t t, t2;
1152         const char *fname = "\\attrib123456789.tst";
1153         BOOL correct = True;
1154
1155         printf("starting attrib test\n");
1156
1157         if (!torture_open_connection(&cli)) {
1158                 return False;
1159         }
1160
1161         smbcli_unlink(cli->tree, fname);
1162         fnum = smbcli_open(cli->tree, fname, 
1163                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1164         smbcli_close(cli->tree, fnum);
1165
1166         if (NT_STATUS_IS_ERR(smbcli_getatr(cli->tree, fname, NULL, NULL, &t))) {
1167                 printf("getatr failed (%s)\n", smbcli_errstr(cli->tree));
1168                 correct = False;
1169         }
1170
1171         printf("New file time is %s", ctime(&t));
1172
1173         if (abs(t - time(NULL)) > 60*60*24*10) {
1174                 printf("ERROR: SMBgetatr bug. time is %s",
1175                        ctime(&t));
1176                 t = time(NULL);
1177                 correct = False;
1178         }
1179
1180         t2 = t-60*60*24; /* 1 day ago */
1181
1182         printf("Setting file time to %s", ctime(&t2));
1183
1184         if (NT_STATUS_IS_ERR(smbcli_setatr(cli->tree, fname, 0, t2))) {
1185                 printf("setatr failed (%s)\n", smbcli_errstr(cli->tree));
1186                 correct = True;
1187         }
1188
1189         if (NT_STATUS_IS_ERR(smbcli_getatr(cli->tree, fname, NULL, NULL, &t))) {
1190                 printf("getatr failed (%s)\n", smbcli_errstr(cli->tree));
1191                 correct = True;
1192         }
1193
1194         printf("Retrieved file time as %s", ctime(&t));
1195
1196         if (t != t2) {
1197                 printf("ERROR: getatr/setatr bug. times are\n%s",
1198                        ctime(&t));
1199                 printf("%s", ctime(&t2));
1200                 correct = True;
1201         }
1202
1203         smbcli_unlink(cli->tree, fname);
1204
1205         if (!torture_close_connection(cli)) {
1206                 correct = False;
1207         }
1208
1209         printf("attrib test finished\n");
1210
1211         return correct;
1212 }
1213
1214
1215 /*
1216   This checks a couple of trans2 calls
1217 */
1218 static BOOL run_trans2test(int dummy)
1219 {
1220         struct smbcli_state *cli;
1221         int fnum;
1222         size_t size;
1223         time_t c_time, a_time, m_time, w_time, m_time2;
1224         const char *fname = "\\trans2.tst";
1225         const char *dname = "\\trans2";
1226         const char *fname2 = "\\trans2\\trans2.tst";
1227         const char *pname;
1228         BOOL correct = True;
1229
1230         printf("starting trans2 test\n");
1231
1232         if (!torture_open_connection(&cli)) {
1233                 return False;
1234         }
1235
1236         smbcli_unlink(cli->tree, fname);
1237
1238         printf("Testing qfileinfo\n");
1239         
1240         fnum = smbcli_open(cli->tree, fname, 
1241                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1242         if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, NULL, &size, &c_time, &a_time, &m_time,
1243                            NULL, NULL))) {
1244                 printf("ERROR: qfileinfo failed (%s)\n", smbcli_errstr(cli->tree));
1245                 correct = False;
1246         }
1247
1248         printf("Testing NAME_INFO\n");
1249
1250         if (NT_STATUS_IS_ERR(smbcli_qfilename(cli->tree, fnum, &pname))) {
1251                 printf("ERROR: qfilename failed (%s)\n", smbcli_errstr(cli->tree));
1252                 correct = False;
1253         }
1254
1255         if (!pname || strcmp(pname, fname)) {
1256                 printf("qfilename gave different name? [%s] [%s]\n",
1257                        fname, pname);
1258                 correct = False;
1259         }
1260
1261         smbcli_close(cli->tree, fnum);
1262         smbcli_unlink(cli->tree, fname);
1263
1264         fnum = smbcli_open(cli->tree, fname, 
1265                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1266         if (fnum == -1) {
1267                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
1268                 return False;
1269         }
1270         smbcli_close(cli->tree, fnum);
1271
1272         printf("Checking for sticky create times\n");
1273
1274         if (NT_STATUS_IS_ERR(smbcli_qpathinfo(cli->tree, fname, &c_time, &a_time, &m_time, &size, NULL))) {
1275                 printf("ERROR: qpathinfo failed (%s)\n", smbcli_errstr(cli->tree));
1276                 correct = False;
1277         } else {
1278                 if (c_time != m_time) {
1279                         printf("create time=%s", ctime(&c_time));
1280                         printf("modify time=%s", ctime(&m_time));
1281                         printf("This system appears to have sticky create times\n");
1282                 }
1283                 if (a_time % (60*60) == 0) {
1284                         printf("access time=%s", ctime(&a_time));
1285                         printf("This system appears to set a midnight access time\n");
1286                         correct = False;
1287                 }
1288
1289                 if (abs(m_time - time(NULL)) > 60*60*24*7) {
1290                         printf("ERROR: totally incorrect times - maybe word reversed? mtime=%s", ctime(&m_time));
1291                         correct = False;
1292                 }
1293         }
1294
1295
1296         smbcli_unlink(cli->tree, fname);
1297         fnum = smbcli_open(cli->tree, fname, 
1298                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1299         smbcli_close(cli->tree, fnum);
1300         if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, fname, &c_time, &a_time, &m_time, &w_time, &size, NULL, NULL))) {
1301                 printf("ERROR: qpathinfo2 failed (%s)\n", smbcli_errstr(cli->tree));
1302                 correct = False;
1303         } else {
1304                 if (w_time < 60*60*24*2) {
1305                         printf("write time=%s", ctime(&w_time));
1306                         printf("This system appears to set a initial 0 write time\n");
1307                         correct = False;
1308                 }
1309         }
1310
1311         smbcli_unlink(cli->tree, fname);
1312
1313
1314         /* check if the server updates the directory modification time
1315            when creating a new file */
1316         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, dname))) {
1317                 printf("ERROR: mkdir failed (%s)\n", smbcli_errstr(cli->tree));
1318                 correct = False;
1319         }
1320         sleep(3);
1321         if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, "\\trans2\\", &c_time, &a_time, &m_time, &w_time, &size, NULL, NULL))) {
1322                 printf("ERROR: qpathinfo2 failed (%s)\n", smbcli_errstr(cli->tree));
1323                 correct = False;
1324         }
1325
1326         fnum = smbcli_open(cli->tree, fname2, 
1327                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1328         smbcli_write(cli->tree, fnum,  0, (char *)&fnum, 0, sizeof(fnum));
1329         smbcli_close(cli->tree, fnum);
1330         if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, "\\trans2\\", &c_time, &a_time, &m_time2, &w_time, &size, NULL, NULL))) {
1331                 printf("ERROR: qpathinfo2 failed (%s)\n", smbcli_errstr(cli->tree));
1332                 correct = False;
1333         } else {
1334                 if (m_time2 == m_time) {
1335                         printf("This system does not update directory modification times\n");
1336                         correct = False;
1337                 }
1338         }
1339         smbcli_unlink(cli->tree, fname2);
1340         smbcli_rmdir(cli->tree, dname);
1341
1342         if (!torture_close_connection(cli)) {
1343                 correct = False;
1344         }
1345
1346         printf("trans2 test finished\n");
1347
1348         return correct;
1349 }
1350
1351 /*
1352   Test delete on close semantics.
1353  */
1354 static BOOL run_deletetest(int dummy)
1355 {
1356         struct smbcli_state *cli1;
1357         struct smbcli_state *cli2 = NULL;
1358         const char *fname = "\\delete.file";
1359         int fnum1 = -1;
1360         int fnum2 = -1;
1361         BOOL correct = True;
1362         NTSTATUS status;
1363         
1364         printf("starting delete test\n");
1365         
1366         if (!torture_open_connection(&cli1)) {
1367                 return False;
1368         }
1369         
1370         /* Test 1 - this should delete the file on close. */
1371         
1372         smbcli_setatr(cli1->tree, fname, 0, 0);
1373         smbcli_unlink(cli1->tree, fname);
1374         
1375         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, GENERIC_RIGHTS_FILE_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
1376                                    NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OVERWRITE_IF, 
1377                                    NTCREATEX_OPTIONS_DELETE_ON_CLOSE, 0);
1378         
1379         if (fnum1 == -1) {
1380                 printf("[1] open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1381                 correct = False;
1382                 goto fail;
1383         }
1384         
1385         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1386                 printf("[1] close failed (%s)\n", smbcli_errstr(cli1->tree));
1387                 correct = False;
1388                 goto fail;
1389         }
1390
1391         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_NONE);
1392         if (fnum1 != -1) {
1393                 printf("[1] open of %s succeeded (should fail)\n", fname);
1394                 correct = False;
1395                 goto fail;
1396         }
1397         
1398         printf("first delete on close test succeeded.\n");
1399         
1400         /* Test 2 - this should delete the file on close. */
1401         
1402         smbcli_setatr(cli1->tree, fname, 0, 0);
1403         smbcli_unlink(cli1->tree, fname);
1404         
1405         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, GENERIC_RIGHTS_FILE_ALL_ACCESS,
1406                                    FILE_ATTRIBUTE_NORMAL, NTCREATEX_SHARE_ACCESS_NONE, 
1407                                    NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1408         
1409         if (fnum1 == -1) {
1410                 printf("[2] open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1411                 correct = False;
1412                 goto fail;
1413         }
1414         
1415         if (NT_STATUS_IS_ERR(smbcli_nt_delete_on_close(cli1->tree, fnum1, True))) {
1416                 printf("[2] setting delete_on_close failed (%s)\n", smbcli_errstr(cli1->tree));
1417                 correct = False;
1418                 goto fail;
1419         }
1420         
1421         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1422                 printf("[2] close failed (%s)\n", smbcli_errstr(cli1->tree));
1423                 correct = False;
1424                 goto fail;
1425         }
1426         
1427         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_NONE);
1428         if (fnum1 != -1) {
1429                 printf("[2] open of %s succeeded should have been deleted on close !\n", fname);
1430                 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1431                         printf("[2] close failed (%s)\n", smbcli_errstr(cli1->tree));
1432                         correct = False;
1433                         goto fail;
1434                 }
1435                 smbcli_unlink(cli1->tree, fname);
1436         } else
1437                 printf("second delete on close test succeeded.\n");
1438         
1439         /* Test 3 - ... */
1440         smbcli_setatr(cli1->tree, fname, 0, 0);
1441         smbcli_unlink(cli1->tree, fname);
1442
1443         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, GENERIC_RIGHTS_FILE_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
1444                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1445
1446         if (fnum1 == -1) {
1447                 printf("[3] open - 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1448                 correct = False;
1449                 goto fail;
1450         }
1451
1452         /* This should fail with a sharing violation - open for delete is only compatible
1453            with SHARE_DELETE. */
1454
1455         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0, GENERIC_RIGHTS_FILE_READ, FILE_ATTRIBUTE_NORMAL,
1456                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, 
1457                                    NTCREATEX_DISP_OPEN, 0, 0);
1458
1459         if (fnum2 != -1) {
1460                 printf("[3] open  - 2 of %s succeeded - should have failed.\n", fname);
1461                 correct = False;
1462                 goto fail;
1463         }
1464
1465         /* This should succeed. */
1466
1467         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0, GENERIC_RIGHTS_FILE_READ, FILE_ATTRIBUTE_NORMAL,
1468                         NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN, 0, 0);
1469
1470         if (fnum2 == -1) {
1471                 printf("[3] open  - 2 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1472                 correct = False;
1473                 goto fail;
1474         }
1475
1476         if (NT_STATUS_IS_ERR(smbcli_nt_delete_on_close(cli1->tree, fnum1, True))) {
1477                 printf("[3] setting delete_on_close failed (%s)\n", smbcli_errstr(cli1->tree));
1478                 correct = False;
1479                 goto fail;
1480         }
1481         
1482         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1483                 printf("[3] close 1 failed (%s)\n", smbcli_errstr(cli1->tree));
1484                 correct = False;
1485                 goto fail;
1486         }
1487         
1488         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum2))) {
1489                 printf("[3] close 2 failed (%s)\n", smbcli_errstr(cli1->tree));
1490                 correct = False;
1491                 goto fail;
1492         }
1493         
1494         /* This should fail - file should no longer be there. */
1495
1496         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_NONE);
1497         if (fnum1 != -1) {
1498                 printf("[3] open of %s succeeded should have been deleted on close !\n", fname);
1499                 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1500                         printf("[3] close failed (%s)\n", smbcli_errstr(cli1->tree));
1501                 }
1502                 smbcli_unlink(cli1->tree, fname);
1503                 correct = False;
1504                 goto fail;
1505         } else
1506                 printf("third delete on close test succeeded.\n");
1507
1508         /* Test 4 ... */
1509         smbcli_setatr(cli1->tree, fname, 0, 0);
1510         smbcli_unlink(cli1->tree, fname);
1511
1512         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
1513                                    SA_RIGHT_FILE_READ_DATA  | 
1514                                    SA_RIGHT_FILE_WRITE_DATA |
1515                                    STD_RIGHT_DELETE_ACCESS,
1516                                    FILE_ATTRIBUTE_NORMAL, 
1517                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, 
1518                                    NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1519                                                                 
1520         if (fnum1 == -1) {
1521                 printf("[4] open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1522                 correct = False;
1523                 goto fail;
1524         }
1525
1526         /* This should succeed. */
1527         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0, GENERIC_RIGHTS_FILE_READ,
1528                                    FILE_ATTRIBUTE_NORMAL, 
1529                                    NTCREATEX_SHARE_ACCESS_READ  | 
1530                                    NTCREATEX_SHARE_ACCESS_WRITE |
1531                                    NTCREATEX_SHARE_ACCESS_DELETE, 
1532                                    NTCREATEX_DISP_OPEN, 0, 0);
1533         if (fnum2 == -1) {
1534                 printf("[4] open  - 2 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1535                 correct = False;
1536                 goto fail;
1537         }
1538         
1539         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum2))) {
1540                 printf("[4] close - 1 failed (%s)\n", smbcli_errstr(cli1->tree));
1541                 correct = False;
1542                 goto fail;
1543         }
1544         
1545         if (NT_STATUS_IS_ERR(smbcli_nt_delete_on_close(cli1->tree, fnum1, True))) {
1546                 printf("[4] setting delete_on_close failed (%s)\n", smbcli_errstr(cli1->tree));
1547                 correct = False;
1548                 goto fail;
1549         }
1550         
1551         /* This should fail - no more opens once delete on close set. */
1552         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0, GENERIC_RIGHTS_FILE_READ,
1553                                    FILE_ATTRIBUTE_NORMAL, NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE,
1554                                    NTCREATEX_DISP_OPEN, 0, 0);
1555         if (fnum2 != -1) {
1556                 printf("[4] open  - 3 of %s succeeded ! Should have failed.\n", fname );
1557                 correct = False;
1558                 goto fail;
1559         } else
1560                 printf("fourth delete on close test succeeded.\n");
1561         
1562         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1563                 printf("[4] close - 2 failed (%s)\n", smbcli_errstr(cli1->tree));
1564                 correct = False;
1565                 goto fail;
1566         }
1567         
1568         /* Test 5 ... */
1569         smbcli_setatr(cli1->tree, fname, 0, 0);
1570         smbcli_unlink(cli1->tree, fname);
1571         
1572         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1573         if (fnum1 == -1) {
1574                 printf("[5] open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1575                 correct = False;
1576                 goto fail;
1577         }
1578
1579         /* This should fail - only allowed on NT opens with DELETE access. */
1580
1581         if (NT_STATUS_IS_OK(smbcli_nt_delete_on_close(cli1->tree, fnum1, True))) {
1582                 printf("[5] setting delete_on_close on OpenX file succeeded - should fail !\n");
1583                 correct = False;
1584                 goto fail;
1585         }
1586
1587         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1588                 printf("[5] close - 2 failed (%s)\n", smbcli_errstr(cli1->tree));
1589                 correct = False;
1590                 goto fail;
1591         }
1592         
1593         printf("fifth delete on close test succeeded.\n");
1594         
1595         /* Test 6 ... */
1596         smbcli_setatr(cli1->tree, fname, 0, 0);
1597         smbcli_unlink(cli1->tree, fname);
1598         
1599         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
1600                                    SA_RIGHT_FILE_READ_DATA | SA_RIGHT_FILE_WRITE_DATA,
1601                                    FILE_ATTRIBUTE_NORMAL, 
1602                                    NTCREATEX_SHARE_ACCESS_READ  |
1603                                    NTCREATEX_SHARE_ACCESS_WRITE |
1604                                    NTCREATEX_SHARE_ACCESS_DELETE,
1605                                    NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1606         
1607         if (fnum1 == -1) {
1608                 printf("[6] open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1609                 correct = False;
1610                 goto fail;
1611         }
1612         
1613         /* This should fail - only allowed on NT opens with DELETE access. */
1614         
1615         if (NT_STATUS_IS_OK(smbcli_nt_delete_on_close(cli1->tree, fnum1, True))) {
1616                 printf("[6] setting delete_on_close on file with no delete access succeeded - should fail !\n");
1617                 correct = False;
1618                 goto fail;
1619         }
1620
1621         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1622                 printf("[6] close - 2 failed (%s)\n", smbcli_errstr(cli1->tree));
1623                 correct = False;
1624                 goto fail;
1625         }
1626
1627         printf("sixth delete on close test succeeded.\n");
1628         
1629         /* Test 7 ... */
1630         smbcli_setatr(cli1->tree, fname, 0, 0);
1631         smbcli_unlink(cli1->tree, fname);
1632         
1633         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
1634                                    SA_RIGHT_FILE_READ_DATA  | 
1635                                    SA_RIGHT_FILE_WRITE_DATA |
1636                                    STD_RIGHT_DELETE_ACCESS,
1637                                    FILE_ATTRIBUTE_NORMAL, 0, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1638                                                                 
1639         if (fnum1 == -1) {
1640                 printf("[7] open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1641                 correct = False;
1642                 goto fail;
1643         }
1644
1645         if (NT_STATUS_IS_ERR(smbcli_nt_delete_on_close(cli1->tree, fnum1, True))) {
1646                 printf("[7] setting delete_on_close on file failed !\n");
1647                 correct = False;
1648                 goto fail;
1649         }
1650         
1651         if (NT_STATUS_IS_ERR(smbcli_nt_delete_on_close(cli1->tree, fnum1, False))) {
1652                 printf("[7] unsetting delete_on_close on file failed !\n");
1653                 correct = False;
1654                 goto fail;
1655         }
1656
1657         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1658                 printf("[7] close - 2 failed (%s)\n", smbcli_errstr(cli1->tree));
1659                 correct = False;
1660                 goto fail;
1661         }
1662         
1663         /* This next open should succeed - we reset the flag. */
1664         
1665         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_NONE);
1666         if (fnum1 == -1) {
1667                 printf("[5] open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1668                 correct = False;
1669                 goto fail;
1670         }
1671
1672         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1673                 printf("[7] close - 2 failed (%s)\n", smbcli_errstr(cli1->tree));
1674                 correct = False;
1675                 goto fail;
1676         }
1677
1678         printf("seventh delete on close test succeeded.\n");
1679         
1680         /* Test 7 ... */
1681         smbcli_setatr(cli1->tree, fname, 0, 0);
1682         smbcli_unlink(cli1->tree, fname);
1683         
1684         if (!torture_open_connection(&cli2)) {
1685                 printf("[8] failed to open second connection.\n");
1686                 correct = False;
1687                 goto fail;
1688         }
1689
1690         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SA_RIGHT_FILE_READ_DATA|SA_RIGHT_FILE_WRITE_DATA|STD_RIGHT_DELETE_ACCESS,
1691                                    FILE_ATTRIBUTE_NORMAL, NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE,
1692                                    NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1693         
1694         if (fnum1 == -1) {
1695                 printf("[8] open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1696                 correct = False;
1697                 goto fail;
1698         }
1699
1700         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SA_RIGHT_FILE_READ_DATA|SA_RIGHT_FILE_WRITE_DATA|STD_RIGHT_DELETE_ACCESS,
1701                                    FILE_ATTRIBUTE_NORMAL, NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE,
1702                                    NTCREATEX_DISP_OPEN, 0, 0);
1703         
1704         if (fnum2 == -1) {
1705                 printf("[8] open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1706                 correct = False;
1707                 goto fail;
1708         }
1709
1710         if (NT_STATUS_IS_ERR(smbcli_nt_delete_on_close(cli1->tree, fnum1, True))) {
1711                 printf("[8] setting delete_on_close on file failed !\n");
1712                 correct = False;
1713                 goto fail;
1714         }
1715         
1716         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1717                 printf("[8] close - 1 failed (%s)\n", smbcli_errstr(cli1->tree));
1718                 correct = False;
1719                 goto fail;
1720         }
1721
1722         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1723                 printf("[8] close - 2 failed (%s)\n", smbcli_errstr(cli2->tree));
1724                 correct = False;
1725                 goto fail;
1726         }
1727
1728         /* This should fail.. */
1729         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_NONE);
1730         if (fnum1 != -1) {
1731                 printf("[8] open of %s succeeded should have been deleted on close !\n", fname);
1732                 goto fail;
1733                 correct = False;
1734         } else
1735                 printf("eighth delete on close test succeeded.\n");
1736
1737         /* This should fail - we need to set DELETE_ACCESS. */
1738         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0,SA_RIGHT_FILE_READ_DATA|SA_RIGHT_FILE_WRITE_DATA,
1739                                    FILE_ATTRIBUTE_NORMAL, NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, NTCREATEX_OPTIONS_DELETE_ON_CLOSE, 0);
1740         
1741         if (fnum1 != -1) {
1742                 printf("[9] open of %s succeeded should have failed!\n", fname);
1743                 correct = False;
1744                 goto fail;
1745         }
1746
1747         printf("ninth delete on close test succeeded.\n");
1748
1749         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SA_RIGHT_FILE_READ_DATA|SA_RIGHT_FILE_WRITE_DATA|STD_RIGHT_DELETE_ACCESS,
1750                                    FILE_ATTRIBUTE_NORMAL, NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, NTCREATEX_OPTIONS_DELETE_ON_CLOSE, 0);
1751         if (fnum1 == -1) {
1752                 printf("[10] open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1753                 correct = False;
1754                 goto fail;
1755         }
1756
1757         /* This should delete the file. */
1758         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1759                 printf("[10] close failed (%s)\n", smbcli_errstr(cli1->tree));
1760                 correct = False;
1761                 goto fail;
1762         }
1763
1764         /* This should fail.. */
1765         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_NONE);
1766         if (fnum1 != -1) {
1767                 printf("[10] open of %s succeeded should have been deleted on close !\n", fname);
1768                 goto fail;
1769                 correct = False;
1770         } else
1771                 printf("tenth delete on close test succeeded.\n");
1772
1773         /* test 11 - does having read only attribute still allow delete on close. */
1774
1775         smbcli_setatr(cli1->tree, fname, 0, 0);
1776         smbcli_unlink(cli1->tree, fname);
1777                                                                                                                                         
1778         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, GENERIC_RIGHTS_FILE_ALL_ACCESS,
1779                                    FILE_ATTRIBUTE_READONLY, NTCREATEX_SHARE_ACCESS_NONE, 
1780                                    NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1781         
1782         if (fnum1 == -1) {
1783                 printf("[11] open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1784                 correct = False;
1785                 goto fail;
1786         }
1787
1788         status = smbcli_nt_delete_on_close(cli1->tree, fnum1, True);
1789
1790         if (NT_STATUS_V(status) != NT_STATUS_V(NT_STATUS_CANNOT_DELETE)) {
1791                 printf("[11] setting delete_on_close should fail with NT_STATUS_CANNOT_DELETE. Got %s instead)\n", smbcli_errstr(cli1->tree));
1792                 correct = False;
1793                 goto fail;
1794         }
1795
1796         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1797                 printf("[11] close failed (%s)\n", smbcli_errstr(cli1->tree));
1798                 correct = False;
1799                 goto fail;
1800         }
1801
1802         smbcli_setatr(cli1->tree, fname, 0, 0);
1803         smbcli_unlink(cli1->tree, fname);
1804                                                                                                                                         
1805         printf("eleventh delete on close test succeeded.\n");
1806
1807         /* test 12 - does having read only attribute still allow delete on close at time of open. */
1808
1809         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, GENERIC_RIGHTS_FILE_ALL_ACCESS, FILE_ATTRIBUTE_READONLY,
1810                                    NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OVERWRITE_IF, 
1811                                    NTCREATEX_OPTIONS_DELETE_ON_CLOSE, 0);
1812         
1813         if (fnum1 != -1) {
1814                 printf("[12] open of %s succeeded. Should fail with NT_STATUS_CANNOT_DELETE.\n", fname);
1815                 smbcli_close(cli1->tree, fnum1);
1816                 correct = False;
1817                 goto fail;
1818         } else {
1819                 status = smbcli_nt_error(cli1->tree);
1820                 if (NT_STATUS_V(status) != NT_STATUS_V(NT_STATUS_CANNOT_DELETE)) {
1821                         printf("[12] setting delete_on_close on open should fail with NT_STATUS_CANNOT_DELETE. Got %s instead)\n", smbcli_errstr(cli1->tree));
1822                         correct = False;
1823                         goto fail;
1824                 }
1825         }
1826         
1827         printf("twelvth delete on close test succeeded.\n");
1828
1829         printf("finished delete test\n");
1830
1831   fail:
1832         /* FIXME: This will crash if we aborted before cli2 got
1833          * intialized, because these functions don't handle
1834          * uninitialized connections. */
1835                 
1836         smbcli_close(cli1->tree, fnum1);
1837         smbcli_close(cli1->tree, fnum2);
1838         smbcli_setatr(cli1->tree, fname, 0, 0);
1839         smbcli_unlink(cli1->tree, fname);
1840
1841         if (!torture_close_connection(cli1)) {
1842                 correct = False;
1843         }
1844         if (!torture_close_connection(cli2)) {
1845                 correct = False;
1846         }
1847         return correct;
1848 }
1849
1850
1851 /*
1852   print out server properties
1853  */
1854 static BOOL run_properties(int dummy)
1855 {
1856         struct smbcli_state *cli;
1857         BOOL correct = True;
1858         
1859         printf("starting properties test\n");
1860         
1861         ZERO_STRUCT(cli);
1862
1863         if (!torture_open_connection(&cli)) {
1864                 return False;
1865         }
1866         
1867         d_printf("Capabilities 0x%08x\n", cli->transport->negotiate.capabilities);
1868
1869         if (!torture_close_connection(cli)) {
1870                 correct = False;
1871         }
1872
1873         return correct;
1874 }
1875
1876
1877
1878 /* FIRST_DESIRED_ACCESS   0xf019f */
1879 #define FIRST_DESIRED_ACCESS   SA_RIGHT_FILE_READ_DATA|SA_RIGHT_FILE_WRITE_DATA|SA_RIGHT_FILE_APPEND_DATA|\
1880                                SA_RIGHT_FILE_READ_EA|                           /* 0xf */ \
1881                                SA_RIGHT_FILE_WRITE_EA|SA_RIGHT_FILE_READ_ATTRIBUTES|     /* 0x90 */ \
1882                                SA_RIGHT_FILE_WRITE_ATTRIBUTES|                  /* 0x100 */ \
1883                                STD_RIGHT_DELETE_ACCESS|STD_RIGHT_READ_CONTROL_ACCESS|\
1884                                STD_RIGHT_WRITE_DAC_ACCESS|STD_RIGHT_WRITE_OWNER_ACCESS     /* 0xf0000 */
1885 /* SECOND_DESIRED_ACCESS  0xe0080 */
1886 #define SECOND_DESIRED_ACCESS  SA_RIGHT_FILE_READ_ATTRIBUTES|                   /* 0x80 */ \
1887                                STD_RIGHT_READ_CONTROL_ACCESS|STD_RIGHT_WRITE_DAC_ACCESS|\
1888                                STD_RIGHT_WRITE_OWNER_ACCESS                      /* 0xe0000 */
1889
1890 #if 0
1891 #define THIRD_DESIRED_ACCESS   FILE_READ_ATTRIBUTES|                   /* 0x80 */ \
1892                                READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
1893                                SA_RIGHT_FILE_READ_DATA|\
1894                                WRITE_OWNER_ACCESS                      /* */
1895 #endif
1896
1897 /*
1898   Test ntcreate calls made by xcopy
1899  */
1900 static BOOL run_xcopy(int dummy)
1901 {
1902         struct smbcli_state *cli1;
1903         const char *fname = "\\test.txt";
1904         BOOL correct = True;
1905         int fnum1, fnum2;
1906
1907         printf("starting xcopy test\n");
1908         
1909         if (!torture_open_connection(&cli1)) {
1910                 return False;
1911         }
1912         
1913         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0,
1914                                    FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
1915                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 
1916                                    0x4044, 0);
1917
1918         if (fnum1 == -1) {
1919                 printf("First open failed - %s\n", smbcli_errstr(cli1->tree));
1920                 return False;
1921         }
1922
1923         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0,
1924                                    SECOND_DESIRED_ACCESS, 0,
1925                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN, 
1926                                    0x200000, 0);
1927         if (fnum2 == -1) {
1928                 printf("second open failed - %s\n", smbcli_errstr(cli1->tree));
1929                 return False;
1930         }
1931         
1932         if (!torture_close_connection(cli1)) {
1933                 correct = False;
1934         }
1935         
1936         return correct;
1937 }
1938
1939 /*
1940   Test rename on files open with share delete and no share delete.
1941  */
1942 static BOOL run_rename(int dummy)
1943 {
1944         struct smbcli_state *cli1;
1945         const char *fname = "\\test.txt";
1946         const char *fname1 = "\\test1.txt";
1947         BOOL correct = True;
1948         int fnum1;
1949
1950         printf("starting rename test\n");
1951         
1952         if (!torture_open_connection(&cli1)) {
1953                 return False;
1954         }
1955         
1956         smbcli_unlink(cli1->tree, fname);
1957         smbcli_unlink(cli1->tree, fname1);
1958         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, GENERIC_RIGHTS_FILE_READ, FILE_ATTRIBUTE_NORMAL,
1959                                    NTCREATEX_SHARE_ACCESS_READ, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1960
1961         if (fnum1 == -1) {
1962                 printf("First open failed - %s\n", smbcli_errstr(cli1->tree));
1963                 return False;
1964         }
1965
1966         if (NT_STATUS_IS_ERR(smbcli_rename(cli1->tree, fname, fname1))) {
1967                 printf("First rename failed (this is correct) - %s\n", smbcli_errstr(cli1->tree));
1968         } else {
1969                 printf("First rename succeeded - this should have failed !\n");
1970                 correct = False;
1971         }
1972
1973         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1974                 printf("close - 1 failed (%s)\n", smbcli_errstr(cli1->tree));
1975                 return False;
1976         }
1977
1978         smbcli_unlink(cli1->tree, fname);
1979         smbcli_unlink(cli1->tree, fname1);
1980         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, GENERIC_RIGHTS_FILE_READ, FILE_ATTRIBUTE_NORMAL,
1981                                    NTCREATEX_SHARE_ACCESS_DELETE|NTCREATEX_SHARE_ACCESS_READ, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1982
1983         if (fnum1 == -1) {
1984                 printf("Second open failed - %s\n", smbcli_errstr(cli1->tree));
1985                 return False;
1986         }
1987
1988         if (NT_STATUS_IS_ERR(smbcli_rename(cli1->tree, fname, fname1))) {
1989                 printf("Second rename failed - this should have succeeded - %s\n", smbcli_errstr(cli1->tree));
1990                 correct = False;
1991         } else {
1992                 printf("Second rename succeeded\n");
1993         }
1994
1995         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1996                 printf("close - 2 failed (%s)\n", smbcli_errstr(cli1->tree));
1997                 return False;
1998         }
1999
2000         smbcli_unlink(cli1->tree, fname);
2001         smbcli_unlink(cli1->tree, fname1);
2002
2003         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, STD_RIGHT_READ_CONTROL_ACCESS, FILE_ATTRIBUTE_NORMAL,
2004                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
2005
2006         if (fnum1 == -1) {
2007                 printf("Third open failed - %s\n", smbcli_errstr(cli1->tree));
2008                 return False;
2009         }
2010
2011
2012         if (NT_STATUS_IS_ERR(smbcli_rename(cli1->tree, fname, fname1))) {
2013                 printf("Third rename failed - this should have succeeded - %s\n", smbcli_errstr(cli1->tree));
2014                 correct = False;
2015         } else {
2016                 printf("Third rename succeeded\n");
2017         }
2018
2019         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2020                 printf("close - 3 failed (%s)\n", smbcli_errstr(cli1->tree));
2021                 return False;
2022         }
2023
2024         smbcli_unlink(cli1->tree, fname);
2025         smbcli_unlink(cli1->tree, fname1);
2026
2027         if (!torture_close_connection(cli1)) {
2028                 correct = False;
2029         }
2030         
2031         return correct;
2032 }
2033
2034
2035 /*
2036   see how many RPC pipes we can open at once
2037 */
2038 static BOOL run_pipe_number(int dummy)
2039 {
2040         struct smbcli_state *cli1;
2041         const char *pipe_name = "\\WKSSVC";
2042         int fnum;
2043         int num_pipes = 0;
2044
2045         printf("starting pipenumber test\n");
2046         if (!torture_open_connection(&cli1)) {
2047                 return False;
2048         }
2049
2050         while(1) {
2051                 fnum = smbcli_nt_create_full(cli1->tree, pipe_name, 0, SA_RIGHT_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
2052                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, NTCREATEX_DISP_OPEN_IF, 0, 0);
2053
2054                 if (fnum == -1) {
2055                         printf("Open of pipe %s failed with error (%s)\n", pipe_name, smbcli_errstr(cli1->tree));
2056                         break;
2057                 }
2058                 num_pipes++;
2059                 printf("%d\r", num_pipes);
2060                 fflush(stdout);
2061         }
2062
2063         printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
2064         torture_close_connection(cli1);
2065         return True;
2066 }
2067
2068
2069
2070
2071 /*
2072   open N connections to the server and just hold them open
2073   used for testing performance when there are N idle users
2074   already connected
2075  */
2076  static BOOL torture_holdcon(int dummy)
2077 {
2078         int i;
2079         struct smbcli_state **cli;
2080         int num_dead = 0;
2081
2082         printf("Opening %d connections\n", torture_numops);
2083         
2084         cli = malloc(sizeof(struct smbcli_state *) * torture_numops);
2085
2086         for (i=0;i<torture_numops;i++) {
2087                 if (!torture_open_connection(&cli[i])) {
2088                         return False;
2089                 }
2090                 printf("opened %d connections\r", i);
2091                 fflush(stdout);
2092         }
2093
2094         printf("\nStarting pings\n");
2095
2096         while (1) {
2097                 for (i=0;i<torture_numops;i++) {
2098                         NTSTATUS status;
2099                         if (cli[i]) {
2100                                 status = smbcli_chkpath(cli[i]->tree, "\\");
2101                                 if (!NT_STATUS_IS_OK(status)) {
2102                                         printf("Connection %d is dead\n", i);
2103                                         cli[i] = NULL;
2104                                         num_dead++;
2105                                 }
2106                                 usleep(100);
2107                         }
2108                 }
2109
2110                 if (num_dead == torture_numops) {
2111                         printf("All connections dead - finishing\n");
2112                         break;
2113                 }
2114
2115                 printf(".");
2116                 fflush(stdout);
2117         }
2118
2119         return True;
2120 }
2121
2122 /*
2123   Try with a wrong vuid and check error message.
2124  */
2125
2126 static BOOL run_vuidtest(int dummy)
2127 {
2128         struct smbcli_state *cli;
2129         const char *fname = "\\vuid.tst";
2130         int fnum;
2131         size_t size;
2132         time_t c_time, a_time, m_time;
2133         BOOL correct = True;
2134
2135         uint16_t orig_vuid;
2136         NTSTATUS result;
2137
2138         printf("starting vuid test\n");
2139
2140         if (!torture_open_connection(&cli)) {
2141                 return False;
2142         }
2143
2144         smbcli_unlink(cli->tree, fname);
2145
2146         fnum = smbcli_open(cli->tree, fname, 
2147                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2148
2149         orig_vuid = cli->session->vuid;
2150
2151         cli->session->vuid += 1234;
2152
2153         printf("Testing qfileinfo with wrong vuid\n");
2154         
2155         if (NT_STATUS_IS_OK(result = smbcli_qfileinfo(cli->tree, fnum, NULL,
2156                                                    &size, &c_time, &a_time,
2157                                                    &m_time, NULL, NULL))) {
2158                 printf("ERROR: qfileinfo passed with wrong vuid\n");
2159                 correct = False;
2160         }
2161
2162         if ( (cli->transport->error.etype != ETYPE_DOS) ||
2163              (cli->transport->error.e.dos.eclass != ERRSRV) ||
2164              (cli->transport->error.e.dos.ecode != ERRbaduid) ) {
2165                 printf("ERROR: qfileinfo should have returned DOS error "
2166                        "ERRSRV:ERRbaduid\n  but returned %s\n",
2167                        smbcli_errstr(cli->tree));
2168                 correct = False;
2169         }
2170
2171         cli->session->vuid -= 1234;
2172
2173         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
2174                 printf("close failed (%s)\n", smbcli_errstr(cli->tree));
2175                 correct = False;
2176         }
2177
2178         smbcli_unlink(cli->tree, fname);
2179
2180         if (!torture_close_connection(cli)) {
2181                 correct = False;
2182         }
2183
2184         printf("vuid test finished\n");
2185
2186         return correct;
2187 }
2188
2189 /*
2190   Test open mode returns on read-only files.
2191  */
2192  static BOOL run_opentest(int dummy)
2193 {
2194         static struct smbcli_state *cli1;
2195         static struct smbcli_state *cli2;
2196         const char *fname = "\\readonly.file";
2197         int fnum1, fnum2;
2198         char buf[20];
2199         size_t fsize;
2200         BOOL correct = True;
2201         char *tmp_path;
2202         int failures = 0;
2203
2204         printf("starting open test\n");
2205         
2206         if (!torture_open_connection(&cli1)) {
2207                 return False;
2208         }
2209         
2210         smbcli_setatr(cli1->tree, fname, 0, 0);
2211         smbcli_unlink(cli1->tree, fname);
2212         
2213         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2214         if (fnum1 == -1) {
2215                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2216                 return False;
2217         }
2218
2219         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2220                 printf("close2 failed (%s)\n", smbcli_errstr(cli1->tree));
2221                 return False;
2222         }
2223         
2224         if (NT_STATUS_IS_ERR(smbcli_setatr(cli1->tree, fname, FILE_ATTRIBUTE_READONLY, 0))) {
2225                 printf("smbcli_setatr failed (%s)\n", smbcli_errstr(cli1->tree));
2226                 CHECK_MAX_FAILURES(error_test1);
2227                 return False;
2228         }
2229         
2230         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_WRITE);
2231         if (fnum1 == -1) {
2232                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2233                 CHECK_MAX_FAILURES(error_test1);
2234                 return False;
2235         }
2236         
2237         /* This will fail - but the error should be ERRnoaccess, not ERRbadshare. */
2238         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_ALL);
2239         
2240         if (check_error(__location__, cli1, ERRDOS, ERRnoaccess, 
2241                         NT_STATUS_ACCESS_DENIED)) {
2242                 printf("correct error code ERRDOS/ERRnoaccess returned\n");
2243         }
2244         
2245         printf("finished open test 1\n");
2246 error_test1:
2247         smbcli_close(cli1->tree, fnum1);
2248         
2249         /* Now try not readonly and ensure ERRbadshare is returned. */
2250         
2251         smbcli_setatr(cli1->tree, fname, 0, 0);
2252         
2253         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_WRITE);
2254         if (fnum1 == -1) {
2255                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2256                 return False;
2257         }
2258         
2259         /* This will fail - but the error should be ERRshare. */
2260         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_ALL);
2261         
2262         if (check_error(__location__, cli1, ERRDOS, ERRbadshare, 
2263                         NT_STATUS_SHARING_VIOLATION)) {
2264                 printf("correct error code ERRDOS/ERRbadshare returned\n");
2265         }
2266         
2267         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2268                 printf("close2 failed (%s)\n", smbcli_errstr(cli1->tree));
2269                 return False;
2270         }
2271         
2272         smbcli_unlink(cli1->tree, fname);
2273         
2274         printf("finished open test 2\n");
2275         
2276         /* Test truncate open disposition on file opened for read. */
2277         
2278         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2279         if (fnum1 == -1) {
2280                 printf("(3) open (1) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2281                 return False;
2282         }
2283         
2284         /* write 20 bytes. */
2285         
2286         memset(buf, '\0', 20);
2287
2288         if (smbcli_write(cli1->tree, fnum1, 0, buf, 0, 20) != 20) {
2289                 printf("write failed (%s)\n", smbcli_errstr(cli1->tree));
2290                 correct = False;
2291         }
2292
2293         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2294                 printf("(3) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
2295                 return False;
2296         }
2297         
2298         /* Ensure size == 20. */
2299         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
2300                 printf("(3) getatr failed (%s)\n", smbcli_errstr(cli1->tree));
2301                 CHECK_MAX_FAILURES(error_test3);
2302                 return False;
2303         }
2304         
2305         if (fsize != 20) {
2306                 printf("(3) file size != 20\n");
2307                 CHECK_MAX_FAILURES(error_test3);
2308                 return False;
2309         }
2310
2311         /* Now test if we can truncate a file opened for readonly. */
2312         
2313         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY|O_TRUNC, DENY_NONE);
2314         if (fnum1 == -1) {
2315                 printf("(3) open (2) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2316                 CHECK_MAX_FAILURES(error_test3);
2317                 return False;
2318         }
2319         
2320         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2321                 printf("close2 failed (%s)\n", smbcli_errstr(cli1->tree));
2322                 return False;
2323         }
2324
2325         /* Ensure size == 0. */
2326         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
2327                 printf("(3) getatr failed (%s)\n", smbcli_errstr(cli1->tree));
2328                 CHECK_MAX_FAILURES(error_test3);
2329                 return False;
2330         }
2331
2332         if (fsize != 0) {
2333                 printf("(3) file size != 0\n");
2334                 CHECK_MAX_FAILURES(error_test3);
2335                 return False;
2336         }
2337         printf("finished open test 3\n");
2338 error_test3:    
2339         smbcli_unlink(cli1->tree, fname);
2340
2341
2342         printf("testing ctemp\n");
2343         fnum1 = smbcli_ctemp(cli1->tree, "\\", &tmp_path);
2344         if (fnum1 == -1) {
2345                 printf("ctemp failed (%s)\n", smbcli_errstr(cli1->tree));
2346                 CHECK_MAX_FAILURES(error_test4);
2347                 return False;
2348         }
2349         printf("ctemp gave path %s\n", tmp_path);
2350         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2351                 printf("close of temp failed (%s)\n", smbcli_errstr(cli1->tree));
2352         }
2353         if (NT_STATUS_IS_ERR(smbcli_unlink(cli1->tree, tmp_path))) {
2354                 printf("unlink of temp failed (%s)\n", smbcli_errstr(cli1->tree));
2355         }
2356 error_test4:    
2357         /* Test the non-io opens... */
2358
2359         if (!torture_open_connection(&cli2)) {
2360                 return False;
2361         }
2362         
2363         smbcli_setatr(cli2->tree, fname, 0, 0);
2364         smbcli_unlink(cli2->tree, fname);
2365         
2366         printf("TEST #1 testing 2 non-io opens (no delete)\n");
2367         
2368         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
2369                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
2370
2371         if (fnum1 == -1) {
2372                 printf("test 1 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2373                 CHECK_MAX_FAILURES(error_test10);
2374                 return False;
2375         }
2376
2377         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
2378                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
2379         if (fnum2 == -1) {
2380                 printf("test 1 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
2381                 CHECK_MAX_FAILURES(error_test10);
2382                 return False;
2383         }
2384
2385         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2386                 printf("test 1 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2387                 return False;
2388         }
2389         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
2390                 printf("test 1 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
2391                 return False;
2392         }
2393
2394         printf("non-io open test #1 passed.\n");
2395 error_test10:
2396         smbcli_unlink(cli1->tree, fname);
2397
2398         printf("TEST #2 testing 2 non-io opens (first with delete)\n");
2399         
2400         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
2401                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
2402
2403         if (fnum1 == -1) {
2404                 printf("test 2 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2405                 CHECK_MAX_FAILURES(error_test20);
2406                 return False;
2407         }
2408
2409         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
2410                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
2411
2412         if (fnum2 == -1) {
2413                 printf("test 2 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
2414                 CHECK_MAX_FAILURES(error_test20);
2415                 return False;
2416         }
2417
2418         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2419                 printf("test 1 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2420                 return False;
2421         }
2422         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
2423                 printf("test 1 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2424                 return False;
2425         }
2426
2427         printf("non-io open test #2 passed.\n");
2428 error_test20:
2429         smbcli_unlink(cli1->tree, fname);
2430
2431         printf("TEST #3 testing 2 non-io opens (second with delete)\n");
2432         
2433         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
2434                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
2435
2436         if (fnum1 == -1) {
2437                 printf("test 3 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2438                 CHECK_MAX_FAILURES(error_test30);
2439                 return False;
2440         }
2441
2442         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
2443                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
2444
2445         if (fnum2 == -1) {
2446                 printf("test 3 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
2447                 CHECK_MAX_FAILURES(error_test30);
2448                 return False;
2449         }
2450
2451         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2452                 printf("test 3 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2453                 return False;
2454         }
2455         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
2456                 printf("test 3 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
2457                 return False;
2458         }
2459
2460         printf("non-io open test #3 passed.\n");
2461 error_test30:
2462         smbcli_unlink(cli1->tree, fname);
2463
2464         printf("TEST #4 testing 2 non-io opens (both with delete)\n");
2465         
2466         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
2467                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
2468
2469         if (fnum1 == -1) {
2470                 printf("test 4 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2471                 CHECK_MAX_FAILURES(error_test40);
2472                 return False;
2473         }
2474
2475         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
2476                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
2477
2478         if (fnum2 != -1) {
2479                 printf("test 4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, smbcli_errstr(cli2->tree));
2480                 CHECK_MAX_FAILURES(error_test40);
2481                 return False;
2482         }
2483
2484         printf("test 4 open 2 of %s gave %s (correct error should be %s)\n", fname, smbcli_errstr(cli2->tree), "sharing violation");
2485
2486         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2487                 printf("test 4 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2488                 return False;
2489         }
2490
2491         printf("non-io open test #4 passed.\n");
2492 error_test40:
2493         smbcli_unlink(cli1->tree, fname);
2494
2495         printf("TEST #5 testing 2 non-io opens (both with delete - both with file share delete)\n");
2496         
2497         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
2498                                    NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
2499
2500         if (fnum1 == -1) {
2501                 printf("test 5 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2502                 CHECK_MAX_FAILURES(error_test50);
2503                 return False;
2504         }
2505
2506         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
2507                                    NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN_IF, 0, 0);
2508
2509         if (fnum2 == -1) {
2510                 printf("test 5 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
2511                 CHECK_MAX_FAILURES(error_test50);
2512                 return False;
2513         }
2514
2515         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2516                 printf("test 5 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2517                 return False;
2518         }
2519
2520         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
2521                 printf("test 5 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
2522                 return False;
2523         }
2524
2525         printf("non-io open test #5 passed.\n");
2526 error_test50:
2527         printf("TEST #6 testing 1 non-io open, one io open\n");
2528         
2529         smbcli_unlink(cli1->tree, fname);
2530
2531         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SA_RIGHT_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
2532                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
2533
2534         if (fnum1 == -1) {
2535                 printf("test 6 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2536                 CHECK_MAX_FAILURES(error_test60);
2537                 return False;
2538         }
2539
2540         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
2541                                    NTCREATEX_SHARE_ACCESS_READ, NTCREATEX_DISP_OPEN_IF, 0, 0);
2542
2543         if (fnum2 == -1) {
2544                 printf("test 6 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
2545                 CHECK_MAX_FAILURES(error_test60);
2546                 return False;
2547         }
2548
2549         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2550                 printf("test 6 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2551                 return False;
2552         }
2553
2554         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
2555                 printf("test 6 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
2556                 return False;
2557         }
2558
2559         printf("non-io open test #6 passed.\n");
2560 error_test60:
2561         printf("TEST #7 testing 1 non-io open, one io open with delete\n");
2562
2563         smbcli_unlink(cli1->tree, fname);
2564
2565         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SA_RIGHT_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
2566                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
2567
2568         if (fnum1 == -1) {
2569                 printf("test 7 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2570                 CHECK_MAX_FAILURES(error_test70);
2571                 return False;
2572         }
2573
2574         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
2575                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN_IF, 0, 0);
2576
2577         if (fnum2 != -1) {
2578                 printf("test 7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, smbcli_errstr(cli2->tree));
2579                 CHECK_MAX_FAILURES(error_test70);
2580                 return False;
2581         }
2582
2583         printf("test 7 open 2 of %s gave %s (correct error should be %s)\n", fname, smbcli_errstr(cli2->tree), "sharing violation");
2584
2585         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2586                 printf("test 7 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2587                 return False;
2588         }
2589
2590         printf("non-io open test #7 passed.\n");
2591
2592 error_test70:
2593
2594         printf("TEST #8 testing one normal open, followed by lock, followed by open with truncate\n");
2595
2596         smbcli_unlink(cli1->tree, fname);
2597
2598         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
2599         if (fnum1 == -1) {
2600                 printf("(8) open (1) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2601                 return False;
2602         }
2603         
2604         /* write 20 bytes. */
2605         
2606         memset(buf, '\0', 20);
2607
2608         if (smbcli_write(cli1->tree, fnum1, 0, buf, 0, 20) != 20) {
2609                 printf("(8) write failed (%s)\n", smbcli_errstr(cli1->tree));
2610                 correct = False;
2611         }
2612
2613         /* Ensure size == 20. */
2614         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
2615                 printf("(8) getatr (1) failed (%s)\n", smbcli_errstr(cli1->tree));
2616                 CHECK_MAX_FAILURES(error_test80);
2617                 return False;
2618         }
2619         
2620         if (fsize != 20) {
2621                 printf("(8) file size != 20\n");
2622                 CHECK_MAX_FAILURES(error_test80);
2623                 return False;
2624         }
2625
2626         /* Get an exclusive lock on the open file. */
2627         if (NT_STATUS_IS_ERR(smbcli_lock(cli1->tree, fnum1, 0, 4, 0, WRITE_LOCK))) {
2628                 printf("(8) lock1 failed (%s)\n", smbcli_errstr(cli1->tree));
2629                 CHECK_MAX_FAILURES(error_test80);
2630                 return False;
2631         }
2632
2633         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR|O_TRUNC, DENY_NONE);
2634         if (fnum1 == -1) {
2635                 printf("(8) open (2) of %s with truncate failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2636                 return False;
2637         }
2638
2639         /* Ensure size == 0. */
2640         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
2641                 printf("(8) getatr (2) failed (%s)\n", smbcli_errstr(cli1->tree));
2642                 CHECK_MAX_FAILURES(error_test80);
2643                 return False;
2644         }
2645         
2646         if (fsize != 0) {
2647                 printf("(8) file size != 0\n");
2648                 CHECK_MAX_FAILURES(error_test80);
2649                 return False;
2650         }
2651
2652         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2653                 printf("(8) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
2654                 return False;
2655         }
2656         
2657         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum2))) {
2658                 printf("(8) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
2659                 return False;
2660         }
2661         
2662 error_test80:
2663
2664         printf("open test #8 passed.\n");
2665
2666         smbcli_unlink(cli1->tree, fname);
2667
2668         if (!torture_close_connection(cli1)) {
2669                 correct = False;
2670         }
2671         if (!torture_close_connection(cli2)) {
2672                 correct = False;
2673         }
2674         
2675         return correct;
2676 }
2677
2678
2679 static uint32_t open_attrs_table[] = {
2680                 FILE_ATTRIBUTE_NORMAL,
2681                 FILE_ATTRIBUTE_ARCHIVE,
2682                 FILE_ATTRIBUTE_READONLY,
2683                 FILE_ATTRIBUTE_HIDDEN,
2684                 FILE_ATTRIBUTE_SYSTEM,
2685
2686                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY,
2687                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN,
2688                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM,
2689                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
2690                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
2691                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
2692
2693                 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
2694                 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
2695                 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
2696                 FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_SYSTEM,
2697 };
2698
2699 struct trunc_open_results {
2700         uint_t num;
2701         uint32_t init_attr;
2702         uint32_t trunc_attr;
2703         uint32_t result_attr;
2704 };
2705
2706 static struct trunc_open_results attr_results[] = {
2707         { 0, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
2708         { 1, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
2709         { 2, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
2710         { 16, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
2711         { 17, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
2712         { 18, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
2713         { 51, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
2714         { 54, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
2715         { 56, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
2716         { 68, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
2717         { 71, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
2718         { 73, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM },
2719         { 99, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
2720         { 102, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
2721         { 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 },
2722         { 116, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
2723         { 119,  FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM,  FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
2724         { 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 },
2725         { 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 },
2726         { 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 },
2727         { 227, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
2728         { 230, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
2729         { 232, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
2730         { 244, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
2731         { 247, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
2732         { 249, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM }
2733 };
2734
2735 static BOOL run_openattrtest(int dummy)
2736 {
2737         struct smbcli_state *cli1;
2738         const char *fname = "\\openattr.file";
2739         int fnum1;
2740         BOOL correct = True;
2741         uint16_t attr;
2742         uint_t i, j, k, l;
2743         int failures = 0;
2744
2745         printf("starting open attr test\n");
2746         
2747         if (!torture_open_connection(&cli1)) {
2748                 return False;
2749         }
2750         
2751         for (k = 0, i = 0; i < sizeof(open_attrs_table)/sizeof(uint32_t); i++) {
2752                 smbcli_setatr(cli1->tree, fname, 0, 0);
2753                 smbcli_unlink(cli1->tree, fname);
2754                 fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SA_RIGHT_FILE_WRITE_DATA, open_attrs_table[i],
2755                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
2756
2757                 if (fnum1 == -1) {
2758                         printf("open %d (1) of %s failed (%s)\n", i, fname, smbcli_errstr(cli1->tree));
2759                         return False;
2760                 }
2761
2762                 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2763                         printf("close %d (1) of %s failed (%s)\n", i, fname, smbcli_errstr(cli1->tree));
2764                         return False;
2765                 }
2766
2767                 for (j = 0; j < ARRAY_SIZE(open_attrs_table); j++) {
2768                         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
2769                                                    SA_RIGHT_FILE_READ_DATA|SA_RIGHT_FILE_WRITE_DATA, 
2770                                                    open_attrs_table[j],
2771                                                    NTCREATEX_SHARE_ACCESS_NONE, 
2772                                                    NTCREATEX_DISP_OVERWRITE, 0, 0);
2773
2774                         if (fnum1 == -1) {
2775                                 for (l = 0; l < ARRAY_SIZE(attr_results); l++) {
2776                                         if (attr_results[l].num == k) {
2777                                                 printf("[%d] trunc open 0x%x -> 0x%x of %s failed - should have succeeded !(0x%x:%s)\n",
2778                                                                 k, open_attrs_table[i],
2779                                                                 open_attrs_table[j],
2780                                                                 fname, NT_STATUS_V(smbcli_nt_error(cli1->tree)), smbcli_errstr(cli1->tree));
2781                                                 correct = False;
2782                                                 CHECK_MAX_FAILURES(error_exit);
2783                                         }
2784                                 }
2785                                 if (NT_STATUS_V(smbcli_nt_error(cli1->tree)) != NT_STATUS_V(NT_STATUS_ACCESS_DENIED)) {
2786                                         printf("[%d] trunc open 0x%x -> 0x%x failed with wrong error code %s\n",
2787                                                         k, open_attrs_table[i], open_attrs_table[j],
2788                                                         smbcli_errstr(cli1->tree));
2789                                         correct = False;
2790                                         CHECK_MAX_FAILURES(error_exit);
2791                                 }
2792 #if 0
2793                                 printf("[%d] trunc open 0x%x -> 0x%x failed\n", k, open_attrs_table[i], open_attrs_table[j]);
2794 #endif
2795                                 k++;
2796                                 continue;
2797                         }
2798
2799                         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2800                                 printf("close %d (2) of %s failed (%s)\n", j, fname, smbcli_errstr(cli1->tree));
2801                                 return False;
2802                         }
2803
2804                         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, &attr, NULL, NULL))) {
2805                                 printf("getatr(2) failed (%s)\n", smbcli_errstr(cli1->tree));
2806                                 return False;
2807                         }
2808
2809 #if 0
2810                         printf("[%d] getatr check [0x%x] trunc [0x%x] got attr 0x%x\n",
2811                                         k,  open_attrs_table[i],  open_attrs_table[j], attr );
2812 #endif
2813
2814                         for (l = 0; l < ARRAY_SIZE(attr_results); l++) {
2815                                 if (attr_results[l].num == k) {
2816                                         if (attr != attr_results[l].result_attr ||
2817                                                         open_attrs_table[i] != attr_results[l].init_attr ||
2818                                                         open_attrs_table[j] != attr_results[l].trunc_attr) {
2819                                                 printf("[%d] getatr check failed. [0x%x] trunc [0x%x] got attr 0x%x, should be 0x%x\n",
2820                                                         k, open_attrs_table[i],
2821                                                         open_attrs_table[j],
2822                                                         (uint_t)attr,
2823                                                         attr_results[l].result_attr);
2824                                                 correct = False;
2825                                                 CHECK_MAX_FAILURES(error_exit);
2826                                         }
2827                                         break;
2828                                 }
2829                         }
2830                         k++;
2831                 }
2832         }
2833 error_exit:
2834         smbcli_setatr(cli1->tree, fname, 0, 0);
2835         smbcli_unlink(cli1->tree, fname);
2836
2837         printf("open attr test %s.\n", correct ? "passed" : "failed");
2838
2839         if (!torture_close_connection(cli1)) {
2840                 correct = False;
2841         }
2842         return correct;
2843 }
2844
2845 static void list_fn(file_info *finfo, const char *name, void *state)
2846 {
2847         
2848 }
2849
2850 /*
2851   test directory listing speed
2852  */
2853 static BOOL run_dirtest(int dummy)
2854 {
2855         int i;
2856         struct smbcli_state *cli;
2857         int fnum;
2858         double t1;
2859         BOOL correct = True;
2860
2861         printf("starting directory test\n");
2862
2863         if (!torture_open_connection(&cli)) {
2864                 return False;
2865         }
2866
2867         printf("Creating %d random filenames\n", torture_numops);
2868
2869         srandom(0);
2870         for (i=0;i<torture_numops;i++) {
2871                 char *fname;
2872                 asprintf(&fname, "\\%x", (int)random());
2873                 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
2874                 if (fnum == -1) {
2875                         fprintf(stderr,"Failed to open %s\n", fname);
2876                         return False;
2877                 }
2878                 smbcli_close(cli->tree, fnum);
2879                 free(fname);
2880         }
2881
2882         t1 = end_timer();
2883
2884         printf("Matched %d\n", smbcli_list(cli->tree, "a*.*", 0, list_fn, NULL));
2885         printf("Matched %d\n", smbcli_list(cli->tree, "b*.*", 0, list_fn, NULL));
2886         printf("Matched %d\n", smbcli_list(cli->tree, "xyzabc", 0, list_fn, NULL));
2887
2888         printf("dirtest core %g seconds\n", end_timer() - t1);
2889
2890         srandom(0);
2891         for (i=0;i<torture_numops;i++) {
2892                 char *fname;
2893                 asprintf(&fname, "\\%x", (int)random());
2894                 smbcli_unlink(cli->tree, fname);
2895                 free(fname);
2896         }
2897
2898         if (!torture_close_connection(cli)) {
2899                 correct = False;
2900         }
2901
2902         printf("finished dirtest\n");
2903
2904         return correct;
2905 }
2906
2907 /*
2908   sees what IOCTLs are supported
2909  */
2910 BOOL torture_ioctl_test(int dummy)
2911 {
2912         struct smbcli_state *cli;
2913         uint16_t device, function;
2914         int fnum;
2915         const char *fname = "\\ioctl.dat";
2916         NTSTATUS status;
2917         union smb_ioctl parms;
2918         TALLOC_CTX *mem_ctx;
2919
2920         if (!torture_open_connection(&cli)) {
2921                 return False;
2922         }
2923
2924         mem_ctx = talloc_init("ioctl_test");
2925
2926         printf("starting ioctl test\n");
2927
2928         smbcli_unlink(cli->tree, fname);
2929
2930         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2931         if (fnum == -1) {
2932                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
2933                 return False;
2934         }
2935
2936         parms.ioctl.level = RAW_IOCTL_IOCTL;
2937         parms.ioctl.in.fnum = fnum;
2938         parms.ioctl.in.request = IOCTL_QUERY_JOB_INFO;
2939         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
2940         printf("ioctl job info: %s\n", smbcli_errstr(cli->tree));
2941
2942         for (device=0;device<0x100;device++) {
2943                 printf("testing device=0x%x\n", device);
2944                 for (function=0;function<0x100;function++) {
2945                         parms.ioctl.in.request = (device << 16) | function;
2946                         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
2947
2948                         if (NT_STATUS_IS_OK(status)) {
2949                                 printf("ioctl device=0x%x function=0x%x OK : %d bytes\n", 
2950                                         device, function, parms.ioctl.out.blob.length);
2951                         }
2952                 }
2953         }
2954
2955         if (!torture_close_connection(cli)) {
2956                 return False;
2957         }
2958
2959         return True;
2960 }
2961
2962
2963 /*
2964   tries variants of chkpath
2965  */
2966 BOOL torture_chkpath_test(int dummy)
2967 {
2968         struct smbcli_state *cli;
2969         int fnum;
2970         BOOL ret;
2971
2972         if (!torture_open_connection(&cli)) {
2973                 return False;
2974         }
2975
2976         printf("starting chkpath test\n");
2977
2978         printf("Testing valid and invalid paths\n");
2979
2980         /* cleanup from an old run */
2981         smbcli_rmdir(cli->tree, "\\chkpath.dir\\dir2");
2982         smbcli_unlink(cli->tree, "\\chkpath.dir\\*");
2983         smbcli_rmdir(cli->tree, "\\chkpath.dir");
2984
2985         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\chkpath.dir"))) {
2986                 printf("mkdir1 failed : %s\n", smbcli_errstr(cli->tree));
2987                 return False;
2988         }
2989
2990         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\chkpath.dir\\dir2"))) {
2991                 printf("mkdir2 failed : %s\n", smbcli_errstr(cli->tree));
2992                 return False;
2993         }
2994
2995         fnum = smbcli_open(cli->tree, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2996         if (fnum == -1) {
2997                 printf("open1 failed (%s)\n", smbcli_errstr(cli->tree));
2998                 return False;
2999         }
3000         smbcli_close(cli->tree, fnum);
3001
3002         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir"))) {
3003                 printf("chkpath1 failed: %s\n", smbcli_errstr(cli->tree));
3004                 ret = False;
3005         }
3006
3007         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\dir2"))) {
3008                 printf("chkpath2 failed: %s\n", smbcli_errstr(cli->tree));
3009                 ret = False;
3010         }
3011
3012         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\foo.txt"))) {
3013                 ret = check_error(__location__, cli, ERRDOS, ERRbadpath, 
3014                                   NT_STATUS_NOT_A_DIRECTORY);
3015         } else {
3016                 printf("* chkpath on a file should fail\n");
3017                 ret = False;
3018         }
3019
3020         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\bar.txt"))) {
3021                 ret = check_error(__location__, cli, ERRDOS, ERRbadfile, 
3022                                   NT_STATUS_OBJECT_NAME_NOT_FOUND);
3023         } else {
3024                 printf("* chkpath on a non existent file should fail\n");
3025                 ret = False;
3026         }
3027
3028         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\dirxx\\bar.txt"))) {
3029                 ret = check_error(__location__, cli, ERRDOS, ERRbadpath, 
3030                                   NT_STATUS_OBJECT_PATH_NOT_FOUND);
3031         } else {
3032                 printf("* chkpath on a non existent component should fail\n");
3033                 ret = False;
3034         }
3035
3036         smbcli_rmdir(cli->tree, "\\chkpath.dir\\dir2");
3037         smbcli_unlink(cli->tree, "\\chkpath.dir\\*");
3038         smbcli_rmdir(cli->tree, "\\chkpath.dir");
3039
3040         if (!torture_close_connection(cli)) {
3041                 return False;
3042         }
3043
3044         return ret;
3045 }
3046
3047 static BOOL run_dirtest1(int dummy)
3048 {
3049         int i;
3050         struct smbcli_state *cli;
3051         int fnum, num_seen;
3052         BOOL correct = True;
3053
3054         printf("starting directory test\n");
3055
3056         if (!torture_open_connection(&cli)) {
3057                 return False;
3058         }
3059
3060         if (smbcli_deltree(cli->tree, "\\LISTDIR") == -1) {
3061                 fprintf(stderr,"Failed to deltree %s, error=%s\n", "\\LISTDIR", smbcli_errstr(cli->tree));
3062                 return False;
3063         }
3064         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\LISTDIR"))) {
3065                 fprintf(stderr,"Failed to mkdir %s, error=%s\n", "\\LISTDIR", smbcli_errstr(cli->tree));
3066                 return False;
3067         }
3068
3069         printf("Creating %d files\n", torture_entries);
3070
3071         /* Create torture_entries files and torture_entries directories. */
3072         for (i=0;i<torture_entries;i++) {
3073                 char *fname;
3074                 asprintf(&fname, "\\LISTDIR\\f%d", i);
3075                 fnum = smbcli_nt_create_full(cli->tree, fname, 0, GENERIC_RIGHTS_FILE_ALL_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
3076                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
3077                 if (fnum == -1) {
3078                         fprintf(stderr,"Failed to open %s, error=%s\n", fname, smbcli_errstr(cli->tree));
3079                         return False;
3080                 }
3081                 free(fname);
3082                 smbcli_close(cli->tree, fnum);
3083         }
3084         for (i=0;i<torture_entries;i++) {
3085                 char *fname;
3086                 asprintf(&fname, "\\LISTDIR\\d%d", i);
3087                 if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, fname))) {
3088                         fprintf(stderr,"Failed to open %s, error=%s\n", fname, smbcli_errstr(cli->tree));
3089                         return False;
3090                 }
3091                 free(fname);
3092         }
3093
3094         /* Now ensure that doing an old list sees both files and directories. */
3095         num_seen = smbcli_list_old(cli->tree, "\\LISTDIR\\*", FILE_ATTRIBUTE_DIRECTORY, list_fn, NULL);
3096         printf("num_seen = %d\n", num_seen );
3097         /* We should see (torture_entries) each of files & directories + . and .. */
3098         if (num_seen != (2*torture_entries)+2) {
3099                 correct = False;
3100                 fprintf(stderr,"entry count mismatch, should be %d, was %d\n",
3101                         (2*torture_entries)+2, num_seen);
3102         }
3103                 
3104
3105         /* Ensure if we have the "must have" bits we only see the
3106          * relevant entries.
3107          */
3108         num_seen = smbcli_list_old(cli->tree, "\\LISTDIR\\*", (FILE_ATTRIBUTE_DIRECTORY<<8)|FILE_ATTRIBUTE_DIRECTORY, list_fn, NULL);
3109         printf("num_seen = %d\n", num_seen );
3110         if (num_seen != torture_entries+2) {
3111                 correct = False;
3112                 fprintf(stderr,"entry count mismatch, should be %d, was %d\n",
3113                         torture_entries+2, num_seen);
3114         }
3115
3116         num_seen = smbcli_list_old(cli->tree, "\\LISTDIR\\*", (FILE_ATTRIBUTE_ARCHIVE<<8)|FILE_ATTRIBUTE_DIRECTORY, list_fn, NULL);
3117         printf("num_seen = %d\n", num_seen );
3118         if (num_seen != torture_entries) {
3119                 correct = False;
3120                 fprintf(stderr,"entry count mismatch, should be %d, was %d\n",
3121                         torture_entries, num_seen);
3122         }
3123
3124         /* Delete everything. */
3125         if (smbcli_deltree(cli->tree, "\\LISTDIR") == -1) {
3126                 fprintf(stderr,"Failed to deltree %s, error=%s\n", "\\LISTDIR", smbcli_errstr(cli->tree));
3127                 return False;
3128         }
3129
3130 #if 0
3131         printf("Matched %d\n", smbcli_list(cli->tree, "a*.*", 0, list_fn, NULL));
3132         printf("Matched %d\n", smbcli_list(cli->tree, "b*.*", 0, list_fn, NULL));
3133         printf("Matched %d\n", smbcli_list(cli->tree, "xyzabc", 0, list_fn, NULL));
3134 #endif
3135
3136         if (!torture_close_connection(cli)) {
3137                 correct = False;
3138         }
3139
3140         printf("finished dirtest1\n");
3141
3142         return correct;
3143 }
3144
3145
3146 /*
3147    simple test harness for playing with deny modes
3148  */
3149 static BOOL run_deny3test(int dummy)
3150 {
3151         struct smbcli_state *cli1, *cli2;
3152         int fnum1, fnum2;
3153         const char *fname;
3154
3155         printf("starting deny3 test\n");
3156
3157         printf("Testing simple deny modes\n");
3158         
3159         if (!torture_open_connection(&cli1)) {
3160                 return False;
3161         }
3162         if (!torture_open_connection(&cli2)) {
3163                 return False;
3164         }
3165
3166         fname = "\\deny_dos1.dat";
3167
3168         smbcli_unlink(cli1->tree, fname);
3169         fnum1 = smbcli_open(cli1->tree, fname, O_CREAT|O_TRUNC|O_WRONLY, DENY_DOS);
3170         fnum2 = smbcli_open(cli1->tree, fname, O_CREAT|O_TRUNC|O_WRONLY, DENY_DOS);
3171         if (fnum1 != -1) smbcli_close(cli1->tree, fnum1);
3172         if (fnum2 != -1) smbcli_close(cli1->tree, fnum2);
3173         smbcli_unlink(cli1->tree, fname);
3174         printf("fnum1=%d fnum2=%d\n", fnum1, fnum2);
3175
3176
3177         fname = "\\deny_dos2.dat";
3178
3179         smbcli_unlink(cli1->tree, fname);
3180         fnum1 = smbcli_open(cli1->tree, fname, O_CREAT|O_TRUNC|O_WRONLY, DENY_DOS);
3181         fnum2 = smbcli_open(cli2->tree, fname, O_CREAT|O_TRUNC|O_WRONLY, DENY_DOS);
3182         if (fnum1 != -1) smbcli_close(cli1->tree, fnum1);
3183         if (fnum2 != -1) smbcli_close(cli2->tree, fnum2);
3184         smbcli_unlink(cli1->tree, fname);
3185         printf("fnum1=%d fnum2=%d\n", fnum1, fnum2);
3186
3187
3188         torture_close_connection(cli1);
3189         torture_close_connection(cli2);
3190
3191         return True;
3192 }
3193
3194 /*
3195   parse a //server/share type UNC name
3196 */
3197 static BOOL parse_unc(const char *unc_name, char **hostname, char **sharename)
3198 {
3199         char *p;
3200
3201         if (strncmp(unc_name, "//", 2)) {
3202                 return False;
3203         }
3204
3205         *hostname = strdup(&unc_name[2]);
3206         p = strchr_m(&(*hostname)[2],'/');
3207         if (!p) {
3208                 return False;
3209         }
3210         *p = 0;
3211         *sharename = strdup(p+1);
3212
3213         return True;
3214 }
3215
3216
3217
3218 static void sigcont(void)
3219 {
3220 }
3221
3222 double torture_create_procs(BOOL (*fn)(struct smbcli_state *, int), BOOL *result)
3223 {
3224         int i, status;
3225         volatile pid_t *child_status;
3226         volatile BOOL *child_status_out;
3227         int synccount;
3228         int tries = 8;
3229         double start_time_limit = 10 + (torture_nprocs * 1.5);
3230         char **unc_list = NULL;
3231         const char *p;
3232         int num_unc_names = 0;
3233
3234         synccount = 0;
3235
3236         signal(SIGCONT, sigcont);
3237
3238         child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*torture_nprocs);
3239         if (!child_status) {
3240                 printf("Failed to setup shared memory\n");
3241                 return -1;
3242         }
3243
3244         child_status_out = (volatile BOOL *)shm_setup(sizeof(BOOL)*torture_nprocs);
3245         if (!child_status_out) {
3246                 printf("Failed to setup result status shared memory\n");
3247                 return -1;
3248         }
3249
3250         p = lp_parm_string(-1, "torture", "unclist");
3251         if (p) {
3252                 unc_list = file_lines_load(p, &num_unc_names);
3253                 if (!unc_list || num_unc_names <= 0) {
3254                         printf("Failed to load unc names list from '%s'\n", p);
3255                         exit(1);
3256                 }
3257         }
3258
3259         for (i = 0; i < torture_nprocs; i++) {
3260                 child_status[i] = 0;
3261                 child_status_out[i] = True;
3262         }
3263
3264         start_timer();
3265
3266         for (i=0;i<torture_nprocs;i++) {
3267                 procnum = i;
3268                 if (fork() == 0) {
3269                         char *myname;
3270                         char *hostname=NULL, *sharename;
3271
3272                         pid_t mypid = getpid();
3273                         sys_srandom(((int)mypid) ^ ((int)time(NULL)));
3274
3275                         asprintf(&myname, "CLIENT%d", i);
3276                         lp_set_cmdline("netbios name", myname);
3277                         free(myname);
3278
3279
3280                         if (unc_list) {
3281                                 if (!parse_unc(unc_list[i % num_unc_names],
3282                                                &hostname, &sharename)) {
3283                                         printf("Failed to parse UNC name %s\n",
3284                                                unc_list[i % num_unc_names]);
3285                                         exit(1);
3286                                 }
3287                         }
3288
3289                         while (1) {
3290                                 if (hostname) {
3291                                         if (torture_open_connection_share(&current_cli,
3292                                                                           hostname, 
3293                                                                           sharename)) {
3294                                                 break;
3295                                         }
3296                                 } else if (torture_open_connection(&current_cli)) {
3297                                                 break;
3298                                 }
3299                                 if (tries-- == 0) {
3300                                         printf("pid %d failed to start\n", (int)getpid());
3301                                         _exit(1);
3302                                 }
3303                                 msleep(100);    
3304                         }
3305
3306                         child_status[i] = getpid();
3307
3308                         pause();
3309
3310                         if (child_status[i]) {
3311                                 printf("Child %d failed to start!\n", i);
3312                                 child_status_out[i] = 1;
3313                                 _exit(1);
3314                         }
3315
3316                         child_status_out[i] = fn(current_cli, i);
3317                         _exit(0);
3318                 }
3319         }
3320
3321         do {
3322                 synccount = 0;
3323                 for (i=0;i<torture_nprocs;i++) {
3324                         if (child_status[i]) synccount++;
3325                 }
3326                 if (synccount == torture_nprocs) break;
3327                 msleep(100);
3328         } while (end_timer() < start_time_limit);
3329
3330         if (synccount != torture_nprocs) {
3331                 printf("FAILED TO START %d CLIENTS (started %d)\n", torture_nprocs, synccount);
3332                 *result = False;
3333                 return end_timer();
3334         }
3335
3336         printf("Starting %d clients\n", torture_nprocs);
3337
3338         /* start the client load */
3339         start_timer();
3340         for (i=0;i<torture_nprocs;i++) {
3341                 child_status[i] = 0;
3342         }
3343         kill(0, SIGCONT);
3344
3345         printf("%d clients started\n", torture_nprocs);
3346
3347         for (i=0;i<torture_nprocs;i++) {
3348                 int ret;
3349                 while ((ret=waitpid(0, &status, 0)) == -1 && errno == EINTR) /* noop */ ;
3350                 if (ret == -1 || WEXITSTATUS(status) != 0) {
3351                         *result = False;
3352                 }
3353         }
3354
3355         printf("\n");
3356         
3357         for (i=0;i<torture_nprocs;i++) {
3358                 if (!child_status_out[i]) {
3359                         *result = False;
3360                 }
3361         }
3362         return end_timer();
3363 }
3364
3365 #define FLAG_MULTIPROC 1
3366
3367 static struct {
3368         const char *name;
3369         BOOL (*fn)(int);
3370         uint_t flags;
3371 } torture_ops[] = {
3372         /* base tests */
3373         {"BASE-FDPASS", run_fdpasstest, 0},
3374         {"BASE-LOCK1",  torture_locktest1,  0},
3375         {"BASE-LOCK2",  torture_locktest2,  0},
3376         {"BASE-LOCK3",  torture_locktest3,  0},
3377         {"BASE-LOCK4",  torture_locktest4,  0},
3378         {"BASE-LOCK5",  torture_locktest5,  0},
3379         {"BASE-LOCK6",  torture_locktest6,  0},
3380         {"BASE-LOCK7",  torture_locktest7,  0},
3381         {"BASE-UNLINK", run_unlinktest, 0},
3382         {"BASE-ATTR",   run_attrtest,   0},
3383         {"BASE-TRANS2", run_trans2test, 0},
3384         {"BASE-NEGNOWAIT", run_negprot_nowait, 0},
3385         {"BASE-DIR",  run_dirtest, 0},
3386         {"BASE-DIR1",  run_dirtest1, 0},
3387         {"BASE-DENY1",  torture_denytest1, 0},
3388         {"BASE-DENY2",  torture_denytest2, 0},
3389         {"BASE-TCON",  run_tcon_test, 0},
3390         {"BASE-TCONDEV",  run_tcon_devtype_test, 0},
3391         {"BASE-VUID", run_vuidtest, 0},
3392         {"BASE-RW1",  run_readwritetest, 0},
3393         {"BASE-RW2",  run_readwritemulti, FLAG_MULTIPROC},
3394         {"BASE-OPEN", run_opentest, 0},
3395         {"BASE-DENY3", run_deny3test, 0},
3396         {"BASE-DEFER_OPEN", run_deferopen, FLAG_MULTIPROC},
3397         {"BASE-XCOPY", run_xcopy, 0},
3398         {"BASE-RENAME", run_rename, 0},
3399         {"BASE-DELETE", run_deletetest, 0},
3400         {"BASE-PROPERTIES", run_properties, 0},
3401         {"BASE-MANGLE", torture_mangle, 0},
3402         {"BASE-OPENATTR", run_openattrtest, 0},
3403         {"BASE-CHARSET", torture_charset, 0},
3404         {"BASE-CHKPATH",  torture_chkpath_test, 0},
3405         {"BASE-SECLEAK",  torture_sec_leak, 0},
3406
3407         /* benchmarking tests */
3408         {"BENCH-HOLDCON",  torture_holdcon, 0},
3409         {"BENCH-NBENCH",  torture_nbench, 0},
3410         {"BENCH-TORTURE",run_torture,    FLAG_MULTIPROC},
3411
3412         /* RAW smb tests */
3413         {"RAW-QFSINFO", torture_raw_qfsinfo, 0},
3414         {"RAW-QFILEINFO", torture_raw_qfileinfo, 0},
3415         {"RAW-SFILEINFO", torture_raw_sfileinfo, 0},
3416         {"RAW-SFILEINFO-BUG", torture_raw_sfileinfo_bug, 0},
3417         {"RAW-SEARCH", torture_raw_search, 0},
3418         {"RAW-CLOSE", torture_raw_close, 0},
3419         {"RAW-OPEN", torture_raw_open, 0},
3420         {"RAW-MKDIR", torture_raw_mkdir, 0},
3421         {"RAW-OPLOCK", torture_raw_oplock, 0},
3422         {"RAW-NOTIFY", torture_raw_notify, 0},
3423         {"RAW-MUX", torture_raw_mux, 0},
3424         {"RAW-IOCTL", torture_raw_ioctl, 0},
3425         {"RAW-CHKPATH", torture_raw_chkpath, 0},
3426         {"RAW-UNLINK", torture_raw_unlink, 0},
3427         {"RAW-READ", torture_raw_read, 0},
3428         {"RAW-WRITE", torture_raw_write, 0},
3429         {"RAW-LOCK", torture_raw_lock, 0},
3430         {"RAW-CONTEXT", torture_raw_context, 0},
3431         {"RAW-RENAME", torture_raw_rename, 0},
3432         {"RAW-SEEK", torture_raw_seek, 0},
3433         {"RAW-RAP", torture_raw_rap, 0},
3434
3435         /* protocol scanners */
3436         {"SCAN-TRANS2", torture_trans2_scan, 0},
3437         {"SCAN-NTTRANS", torture_nttrans_scan, 0},
3438         {"SCAN-ALIASES", torture_trans2_aliases, 0},
3439         {"SCAN-SMB", torture_smb_scan, 0},
3440         {"SCAN-MAXFID", run_maxfidtest, FLAG_MULTIPROC},
3441         {"SCAN-UTABLE", torture_utable, 0},
3442         {"SCAN-CASETABLE", torture_casetable, 0},
3443         {"SCAN-PIPE_NUMBER", run_pipe_number, 0},
3444         {"SCAN-IOCTL",  torture_ioctl_test, 0},
3445
3446         /* rpc testers */
3447         {"RPC-LSA", torture_rpc_lsa, 0},
3448         {"RPC-ECHO", torture_rpc_echo, 0},
3449         {"RPC-DFS", torture_rpc_dfs, 0},
3450         {"RPC-SPOOLSS", torture_rpc_spoolss, 0},
3451         {"RPC-SAMR", torture_rpc_samr, 0},
3452         {"RPC-NETLOGON", torture_rpc_netlogon, 0},
3453         {"RPC-SCHANNEL", torture_rpc_schannel, 0},
3454         {"RPC-WKSSVC", torture_rpc_wkssvc, 0},
3455         {"RPC-SRVSVC", torture_rpc_srvsvc, 0},
3456         {"RPC-SVCCTL", torture_rpc_svcctl, 0},
3457         {"RPC-ATSVC", torture_rpc_atsvc, 0},
3458         {"RPC-EVENTLOG", torture_rpc_eventlog, 0},
3459         {"RPC-EPMAPPER", torture_rpc_epmapper, 0},
3460         {"RPC-WINREG", torture_rpc_winreg, 0},
3461         {"RPC-OXIDRESOLVE", torture_rpc_oxidresolve, 0},
3462         {"RPC-MGMT", torture_rpc_mgmt, 0},
3463         {"RPC-SCANNER", torture_rpc_scanner, 0},
3464         {"RPC-AUTOIDL", torture_rpc_autoidl, 0},
3465         {"RPC-COUNTCALLS", torture_rpc_countcalls, 0},
3466         {"RPC-MULTIBIND", torture_multi_bind, 0},
3467         {"RPC-DRSUAPI", torture_rpc_drsuapi, 0},
3468
3469         /* local (no server) testers */
3470         {"LOCAL-NTLMSSP", torture_ntlmssp_self_check, 0},
3471         {"LOCAL-ICONV", torture_local_iconv, 0},
3472         {"LOCAL-TALLOC", torture_local_talloc, 0},
3473         {"LOCAL-MESSAGING", torture_local_messaging, 0},
3474         {"LOCAL-BINDINGSTRING", torture_local_binding_string, 0},
3475         {"LOCAL-IDTREE", torture_local_idtree, 0},
3476
3477         /* ldap testers */
3478         {"LDAP-BASIC", torture_ldap_basic, 0},
3479
3480         {NULL, NULL, 0}};
3481
3482
3483
3484 /****************************************************************************
3485 run a specified test or "ALL"
3486 ****************************************************************************/
3487 static BOOL run_test(const char *name)
3488 {
3489         BOOL ret = True;
3490         int i;
3491         BOOL matched = False;
3492
3493         if (strequal(name,"ALL")) {
3494                 for (i=0;torture_ops[i].name;i++) {
3495                         if (!run_test(torture_ops[i].name)) {
3496                                 ret = False;
3497                         }
3498                 }
3499                 return ret;
3500         }
3501
3502         for (i=0;torture_ops[i].name;i++) {
3503                 if (gen_fnmatch(name, torture_ops[i].name) == 0) {
3504                         double t;
3505                         matched = True;
3506                         init_iconv();
3507                         printf("Running %s\n", torture_ops[i].name);
3508                         if (torture_ops[i].flags & FLAG_MULTIPROC) {
3509                                 BOOL result;
3510                                 t = torture_create_procs(torture_ops[i].fn, &result);
3511                                 if (!result) { 
3512                                         ret = False;
3513                                         printf("TEST %s FAILED!\n", torture_ops[i].name);
3514                                 }
3515                                          
3516                         } else {
3517                                 start_timer();
3518                                 if (!torture_ops[i].fn(0)) {
3519                                         ret = False;
3520                                         printf("TEST %s FAILED!\n", torture_ops[i].name);
3521                                 }
3522                                 t = end_timer();
3523                         }
3524                         printf("%s took %g secs\n\n", torture_ops[i].name, t);
3525                 }
3526         }
3527
3528         if (!matched) {
3529                 printf("Unknown torture operation '%s'\n", name);
3530         }
3531
3532         return ret;
3533 }
3534
3535
3536 static void parse_dns(const char *dns)
3537 {
3538         char *userdn, *basedn, *secret;
3539         char *p, *d;
3540
3541         /* retrievieng the userdn */
3542         p = strchr_m(dns, '#');
3543         if (!p) {
3544                 lp_set_cmdline("torture:ldap_userdn", "");
3545                 lp_set_cmdline("torture:ldap_basedn", "");
3546                 lp_set_cmdline("torture:ldap_secret", "");
3547                 return;
3548         }
3549         userdn = strndup(dns, p - dns);
3550         lp_set_cmdline("torture:ldap_userdn", userdn);
3551
3552         /* retrieve the basedn */
3553         d = p + 1;
3554         p = strchr_m(d, '#');
3555         if (!p) {
3556                 lp_set_cmdline("torture:ldap_basedn", "");
3557                 lp_set_cmdline("torture:ldap_secret", "");
3558                 return;
3559         }
3560         basedn = strndup(d, p - d);
3561         lp_set_cmdline("torture:ldap_basedn", basedn);
3562
3563         /* retrieve the secret */
3564         p = p + 1;
3565         if (!p) {
3566                 lp_set_cmdline("torture:ldap_secret", "");
3567                 return;
3568         }
3569         secret = strdup(p);
3570         lp_set_cmdline("torture:ldap_secret", secret);
3571
3572         printf ("%s - %s - %s\n", userdn, basedn, secret);
3573
3574 }
3575
3576 static void usage(poptContext pc)
3577 {
3578         int i;
3579         int perline = 5;
3580
3581         poptPrintUsage(pc, stdout, 0);
3582         printf("\n");
3583
3584         printf("tests are:");
3585         for (i=0;torture_ops[i].name;i++) {
3586                 if ((i%perline)==0) {
3587                         printf("\n");
3588                 }
3589                 printf("%s ", torture_ops[i].name);
3590         }
3591         printf("\n\n");
3592
3593         printf("default test is ALL\n");
3594         
3595         exit(1);
3596 }
3597
3598 static BOOL is_binding_string(const char *binding_string)
3599 {
3600         TALLOC_CTX *mem_ctx = talloc_init("is_binding_string");
3601         struct dcerpc_binding binding_struct;
3602         NTSTATUS status;
3603         
3604         status = dcerpc_parse_binding(mem_ctx, binding_string, &binding_struct);
3605
3606         talloc_destroy(mem_ctx);
3607         return NT_STATUS_IS_OK(status);
3608 }
3609
3610 /****************************************************************************
3611   main program
3612 ****************************************************************************/
3613  int main(int argc,char *argv[])
3614 {
3615         int opt, i;
3616         char *p;
3617         BOOL correct = True;
3618         int argc_new;
3619         char **argv_new;
3620         poptContext pc;
3621         enum {OPT_LOADFILE=1000,OPT_UNCLIST,OPT_TIMELIMIT,OPT_DNS,OPT_DANGEROUS};
3622         struct poptOption long_options[] = {
3623                 POPT_AUTOHELP
3624                 {"smb-ports",   'p', POPT_ARG_STRING, NULL,             0,      "SMB ports",    NULL},
3625                 {"seed",          0, POPT_ARG_STRING, NULL,             0,      "seed",         NULL},
3626                 {"num-progs",     0, POPT_ARG_INT,  &torture_nprocs,    0,      "num progs",    NULL},
3627                 {"num-ops",       0, POPT_ARG_INT,  &torture_numops,    0,      "num ops",      NULL},
3628                 {"entries",       0, POPT_ARG_INT,  &torture_entries,   0,      "entries",      NULL},
3629                 {"use-oplocks", 'L', POPT_ARG_NONE, &use_oplocks,       0,      "use oplocks",  NULL},
3630                 {"show-all",      0, POPT_ARG_NONE, &torture_showall,   0,      "show all",     NULL},
3631                 {"loadfile",      0, POPT_ARG_STRING,   NULL,   OPT_LOADFILE,   "loadfile",     NULL},
3632                 {"unclist",       0, POPT_ARG_STRING,   NULL,   OPT_UNCLIST,    "unclist",      NULL},
3633                 {"timelimit",   't', POPT_ARG_STRING,   NULL,   OPT_TIMELIMIT,  "timelimit",    NULL},
3634                 {"failures",    'f', POPT_ARG_INT,  &torture_failures,  0,      "failures",     NULL},
3635                 {"parse-dns",   'D', POPT_ARG_STRING,   NULL,   OPT_DNS,        "parse-dns",    NULL},
3636                 {"dangerous",   'X', POPT_ARG_NONE,     NULL,   OPT_DANGEROUS,  "dangerous",    NULL},
3637                 POPT_COMMON_SAMBA
3638                 POPT_COMMON_CONNECTION
3639                 POPT_COMMON_CREDENTIALS
3640                 POPT_COMMON_VERSION
3641                 POPT_TABLEEND
3642         };
3643
3644         setup_logging("smbtorture", DEBUG_STDOUT);
3645
3646 #ifdef HAVE_SETBUFFER
3647         setbuffer(stdout, NULL, 0);
3648 #endif
3649
3650         pc = poptGetContext("smbtorture", argc, (const char **) argv, long_options, 
3651                                 POPT_CONTEXT_KEEP_FIRST);
3652
3653         poptSetOtherOptionHelp(pc, "<binding>|<unc> TEST1 TEST2 ...");
3654
3655         while((opt = poptGetNextOpt(pc)) != -1) {
3656                 switch (opt) {
3657                 case OPT_LOADFILE:
3658                         lp_set_cmdline("torture:loadfile", poptGetOptArg(pc));
3659                         break;
3660                 case OPT_UNCLIST:
3661                         lp_set_cmdline("torture:unclist", poptGetOptArg(pc));
3662                         break;
3663                 case OPT_TIMELIMIT:
3664                         lp_set_cmdline("torture:timelimit", poptGetOptArg(pc));
3665                         break;
3666                 case OPT_DNS:
3667                         parse_dns(poptGetOptArg(pc));
3668                         break;
3669                 case OPT_DANGEROUS:
3670                         lp_set_cmdline("torture:dangerous", "1");
3671                         break;
3672                 default:
3673                         d_printf("Invalid option %s: %s\n", 
3674                                  poptBadOption(pc, 0), poptStrerror(opt));
3675                         usage(pc);
3676                         exit(1);
3677                 }
3678         }
3679
3680         lp_load(dyn_CONFIGFILE,True,False,False);
3681         load_interfaces();
3682         srandom(time(NULL));
3683
3684         argv_new = (const char **)poptGetArgs(pc);
3685
3686         argc_new = argc;
3687         for (i=0; i<argc; i++) {
3688                 if (argv_new[i] == NULL) {
3689                         argc_new = i;
3690                         break;
3691                 }
3692         }
3693
3694         if (argc_new < 3) {
3695                 usage(pc);
3696                 exit(1);
3697         }
3698
3699         for(p = argv_new[1]; *p; p++) {
3700                 if(*p == '\\')
3701                         *p = '/';
3702         }
3703
3704         /* see if its a RPC transport specifier */
3705         if (is_binding_string(argv_new[1])) {
3706                 lp_set_cmdline("torture:binding", argv_new[1]);
3707         } else {
3708                 char *binding = NULL;
3709                 char *host = NULL, *share = NULL;
3710
3711                 if (!parse_unc(argv_new[1], &host, &share)) {
3712                         usage(pc);
3713                 }
3714
3715                 lp_set_cmdline("torture:host", host);
3716                 lp_set_cmdline("torture:share", share);
3717                 asprintf(&binding, "ncacn_np:%s", host);
3718                 lp_set_cmdline("torture:binding", binding);
3719         }
3720
3721         if (!lp_parm_string(-1,"torture","username")) {
3722                 lp_set_cmdline("torture:username", cmdline_get_username());
3723         }
3724         if (!lp_parm_string(-1,"torture","userdomain")) {
3725                 /* 
3726                  * backward compatibility
3727                  * maybe we should remove this to make this consistent
3728                  * for all cmdline tools
3729                  * --metze
3730                  */
3731                 if (strequal(lp_netbios_name(),cmdline_get_userdomain())) {
3732                         cmdline_set_userdomain(lp_workgroup());
3733                 }
3734                 lp_set_cmdline("torture:userdomain", cmdline_get_userdomain());
3735         }
3736         if (!lp_parm_string(-1,"torture","password")) {
3737                 lp_set_cmdline("torture:password", cmdline_get_userpassword());
3738         }
3739
3740         if (argc_new == 0) {
3741                 printf("You must specify a test to run, or 'ALL'\n");
3742         } else {
3743                 for (i=2;i<argc_new;i++) {
3744                         if (!run_test(argv_new[i])) {
3745                                 correct = False;
3746                         }
3747                 }
3748         }
3749
3750         if (correct) {
3751                 return(0);
3752         } else {
3753                 return(1);
3754         }
3755 }