r3323: more warning reductions
[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 /*
1353   print out server properties
1354  */
1355 static BOOL run_properties(int dummy)
1356 {
1357         struct smbcli_state *cli;
1358         BOOL correct = True;
1359         
1360         printf("starting properties test\n");
1361         
1362         ZERO_STRUCT(cli);
1363
1364         if (!torture_open_connection(&cli)) {
1365                 return False;
1366         }
1367         
1368         d_printf("Capabilities 0x%08x\n", cli->transport->negotiate.capabilities);
1369
1370         if (!torture_close_connection(cli)) {
1371                 correct = False;
1372         }
1373
1374         return correct;
1375 }
1376
1377
1378
1379 /* FIRST_DESIRED_ACCESS   0xf019f */
1380 #define FIRST_DESIRED_ACCESS   SA_RIGHT_FILE_READ_DATA|SA_RIGHT_FILE_WRITE_DATA|SA_RIGHT_FILE_APPEND_DATA|\
1381                                SA_RIGHT_FILE_READ_EA|                           /* 0xf */ \
1382                                SA_RIGHT_FILE_WRITE_EA|SA_RIGHT_FILE_READ_ATTRIBUTES|     /* 0x90 */ \
1383                                SA_RIGHT_FILE_WRITE_ATTRIBUTES|                  /* 0x100 */ \
1384                                STD_RIGHT_DELETE_ACCESS|STD_RIGHT_READ_CONTROL_ACCESS|\
1385                                STD_RIGHT_WRITE_DAC_ACCESS|STD_RIGHT_WRITE_OWNER_ACCESS     /* 0xf0000 */
1386 /* SECOND_DESIRED_ACCESS  0xe0080 */
1387 #define SECOND_DESIRED_ACCESS  SA_RIGHT_FILE_READ_ATTRIBUTES|                   /* 0x80 */ \
1388                                STD_RIGHT_READ_CONTROL_ACCESS|STD_RIGHT_WRITE_DAC_ACCESS|\
1389                                STD_RIGHT_WRITE_OWNER_ACCESS                      /* 0xe0000 */
1390
1391 #if 0
1392 #define THIRD_DESIRED_ACCESS   FILE_READ_ATTRIBUTES|                   /* 0x80 */ \
1393                                READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
1394                                SA_RIGHT_FILE_READ_DATA|\
1395                                WRITE_OWNER_ACCESS                      /* */
1396 #endif
1397
1398 /*
1399   Test ntcreate calls made by xcopy
1400  */
1401 static BOOL run_xcopy(int dummy)
1402 {
1403         struct smbcli_state *cli1;
1404         const char *fname = "\\test.txt";
1405         BOOL correct = True;
1406         int fnum1, fnum2;
1407
1408         printf("starting xcopy test\n");
1409         
1410         if (!torture_open_connection(&cli1)) {
1411                 return False;
1412         }
1413         
1414         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0,
1415                                    FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
1416                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 
1417                                    0x4044, 0);
1418
1419         if (fnum1 == -1) {
1420                 printf("First open failed - %s\n", smbcli_errstr(cli1->tree));
1421                 return False;
1422         }
1423
1424         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0,
1425                                    SECOND_DESIRED_ACCESS, 0,
1426                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN, 
1427                                    0x200000, 0);
1428         if (fnum2 == -1) {
1429                 printf("second open failed - %s\n", smbcli_errstr(cli1->tree));
1430                 return False;
1431         }
1432         
1433         if (!torture_close_connection(cli1)) {
1434                 correct = False;
1435         }
1436         
1437         return correct;
1438 }
1439
1440
1441 /*
1442   see how many RPC pipes we can open at once
1443 */
1444 static BOOL run_pipe_number(int dummy)
1445 {
1446         struct smbcli_state *cli1;
1447         const char *pipe_name = "\\WKSSVC";
1448         int fnum;
1449         int num_pipes = 0;
1450
1451         printf("starting pipenumber test\n");
1452         if (!torture_open_connection(&cli1)) {
1453                 return False;
1454         }
1455
1456         while(1) {
1457                 fnum = smbcli_nt_create_full(cli1->tree, pipe_name, 0, SA_RIGHT_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
1458                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1459
1460                 if (fnum == -1) {
1461                         printf("Open of pipe %s failed with error (%s)\n", pipe_name, smbcli_errstr(cli1->tree));
1462                         break;
1463                 }
1464                 num_pipes++;
1465                 printf("%d\r", num_pipes);
1466                 fflush(stdout);
1467         }
1468
1469         printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
1470         torture_close_connection(cli1);
1471         return True;
1472 }
1473
1474
1475
1476
1477 /*
1478   open N connections to the server and just hold them open
1479   used for testing performance when there are N idle users
1480   already connected
1481  */
1482  static BOOL torture_holdcon(int dummy)
1483 {
1484         int i;
1485         struct smbcli_state **cli;
1486         int num_dead = 0;
1487
1488         printf("Opening %d connections\n", torture_numops);
1489         
1490         cli = malloc(sizeof(struct smbcli_state *) * torture_numops);
1491
1492         for (i=0;i<torture_numops;i++) {
1493                 if (!torture_open_connection(&cli[i])) {
1494                         return False;
1495                 }
1496                 printf("opened %d connections\r", i);
1497                 fflush(stdout);
1498         }
1499
1500         printf("\nStarting pings\n");
1501
1502         while (1) {
1503                 for (i=0;i<torture_numops;i++) {
1504                         NTSTATUS status;
1505                         if (cli[i]) {
1506                                 status = smbcli_chkpath(cli[i]->tree, "\\");
1507                                 if (!NT_STATUS_IS_OK(status)) {
1508                                         printf("Connection %d is dead\n", i);
1509                                         cli[i] = NULL;
1510                                         num_dead++;
1511                                 }
1512                                 usleep(100);
1513                         }
1514                 }
1515
1516                 if (num_dead == torture_numops) {
1517                         printf("All connections dead - finishing\n");
1518                         break;
1519                 }
1520
1521                 printf(".");
1522                 fflush(stdout);
1523         }
1524
1525         return True;
1526 }
1527
1528 /*
1529   Try with a wrong vuid and check error message.
1530  */
1531
1532 static BOOL run_vuidtest(int dummy)
1533 {
1534         struct smbcli_state *cli;
1535         const char *fname = "\\vuid.tst";
1536         int fnum;
1537         size_t size;
1538         time_t c_time, a_time, m_time;
1539         BOOL correct = True;
1540
1541         uint16_t orig_vuid;
1542         NTSTATUS result;
1543
1544         printf("starting vuid test\n");
1545
1546         if (!torture_open_connection(&cli)) {
1547                 return False;
1548         }
1549
1550         smbcli_unlink(cli->tree, fname);
1551
1552         fnum = smbcli_open(cli->tree, fname, 
1553                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1554
1555         orig_vuid = cli->session->vuid;
1556
1557         cli->session->vuid += 1234;
1558
1559         printf("Testing qfileinfo with wrong vuid\n");
1560         
1561         if (NT_STATUS_IS_OK(result = smbcli_qfileinfo(cli->tree, fnum, NULL,
1562                                                    &size, &c_time, &a_time,
1563                                                    &m_time, NULL, NULL))) {
1564                 printf("ERROR: qfileinfo passed with wrong vuid\n");
1565                 correct = False;
1566         }
1567
1568         if ( (cli->transport->error.etype != ETYPE_DOS) ||
1569              (cli->transport->error.e.dos.eclass != ERRSRV) ||
1570              (cli->transport->error.e.dos.ecode != ERRbaduid) ) {
1571                 printf("ERROR: qfileinfo should have returned DOS error "
1572                        "ERRSRV:ERRbaduid\n  but returned %s\n",
1573                        smbcli_errstr(cli->tree));
1574                 correct = False;
1575         }
1576
1577         cli->session->vuid -= 1234;
1578
1579         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
1580                 printf("close failed (%s)\n", smbcli_errstr(cli->tree));
1581                 correct = False;
1582         }
1583
1584         smbcli_unlink(cli->tree, fname);
1585
1586         if (!torture_close_connection(cli)) {
1587                 correct = False;
1588         }
1589
1590         printf("vuid test finished\n");
1591
1592         return correct;
1593 }
1594
1595 /*
1596   Test open mode returns on read-only files.
1597  */
1598  static BOOL run_opentest(int dummy)
1599 {
1600         static struct smbcli_state *cli1;
1601         static struct smbcli_state *cli2;
1602         const char *fname = "\\readonly.file";
1603         int fnum1, fnum2;
1604         char buf[20];
1605         size_t fsize;
1606         BOOL correct = True;
1607         char *tmp_path;
1608         int failures = 0;
1609
1610         printf("starting open test\n");
1611         
1612         if (!torture_open_connection(&cli1)) {
1613                 return False;
1614         }
1615         
1616         smbcli_setatr(cli1->tree, fname, 0, 0);
1617         smbcli_unlink(cli1->tree, fname);
1618         
1619         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1620         if (fnum1 == -1) {
1621                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1622                 return False;
1623         }
1624
1625         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1626                 printf("close2 failed (%s)\n", smbcli_errstr(cli1->tree));
1627                 return False;
1628         }
1629         
1630         if (NT_STATUS_IS_ERR(smbcli_setatr(cli1->tree, fname, FILE_ATTRIBUTE_READONLY, 0))) {
1631                 printf("smbcli_setatr failed (%s)\n", smbcli_errstr(cli1->tree));
1632                 CHECK_MAX_FAILURES(error_test1);
1633                 return False;
1634         }
1635         
1636         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_WRITE);
1637         if (fnum1 == -1) {
1638                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1639                 CHECK_MAX_FAILURES(error_test1);
1640                 return False;
1641         }
1642         
1643         /* This will fail - but the error should be ERRnoaccess, not ERRbadshare. */
1644         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_ALL);
1645         
1646         if (check_error(__location__, cli1, ERRDOS, ERRnoaccess, 
1647                         NT_STATUS_ACCESS_DENIED)) {
1648                 printf("correct error code ERRDOS/ERRnoaccess returned\n");
1649         }
1650         
1651         printf("finished open test 1\n");
1652 error_test1:
1653         smbcli_close(cli1->tree, fnum1);
1654         
1655         /* Now try not readonly and ensure ERRbadshare is returned. */
1656         
1657         smbcli_setatr(cli1->tree, fname, 0, 0);
1658         
1659         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_WRITE);
1660         if (fnum1 == -1) {
1661                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1662                 return False;
1663         }
1664         
1665         /* This will fail - but the error should be ERRshare. */
1666         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_ALL);
1667         
1668         if (check_error(__location__, cli1, ERRDOS, ERRbadshare, 
1669                         NT_STATUS_SHARING_VIOLATION)) {
1670                 printf("correct error code ERRDOS/ERRbadshare returned\n");
1671         }
1672         
1673         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1674                 printf("close2 failed (%s)\n", smbcli_errstr(cli1->tree));
1675                 return False;
1676         }
1677         
1678         smbcli_unlink(cli1->tree, fname);
1679         
1680         printf("finished open test 2\n");
1681         
1682         /* Test truncate open disposition on file opened for read. */
1683         
1684         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1685         if (fnum1 == -1) {
1686                 printf("(3) open (1) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1687                 return False;
1688         }
1689         
1690         /* write 20 bytes. */
1691         
1692         memset(buf, '\0', 20);
1693
1694         if (smbcli_write(cli1->tree, fnum1, 0, buf, 0, 20) != 20) {
1695                 printf("write failed (%s)\n", smbcli_errstr(cli1->tree));
1696                 correct = False;
1697         }
1698
1699         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1700                 printf("(3) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
1701                 return False;
1702         }
1703         
1704         /* Ensure size == 20. */
1705         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1706                 printf("(3) getatr failed (%s)\n", smbcli_errstr(cli1->tree));
1707                 CHECK_MAX_FAILURES(error_test3);
1708                 return False;
1709         }
1710         
1711         if (fsize != 20) {
1712                 printf("(3) file size != 20\n");
1713                 CHECK_MAX_FAILURES(error_test3);
1714                 return False;
1715         }
1716
1717         /* Now test if we can truncate a file opened for readonly. */
1718         
1719         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY|O_TRUNC, DENY_NONE);
1720         if (fnum1 == -1) {
1721                 printf("(3) open (2) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1722                 CHECK_MAX_FAILURES(error_test3);
1723                 return False;
1724         }
1725         
1726         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1727                 printf("close2 failed (%s)\n", smbcli_errstr(cli1->tree));
1728                 return False;
1729         }
1730
1731         /* Ensure size == 0. */
1732         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1733                 printf("(3) getatr failed (%s)\n", smbcli_errstr(cli1->tree));
1734                 CHECK_MAX_FAILURES(error_test3);
1735                 return False;
1736         }
1737
1738         if (fsize != 0) {
1739                 printf("(3) file size != 0\n");
1740                 CHECK_MAX_FAILURES(error_test3);
1741                 return False;
1742         }
1743         printf("finished open test 3\n");
1744 error_test3:    
1745         smbcli_unlink(cli1->tree, fname);
1746
1747
1748         printf("testing ctemp\n");
1749         fnum1 = smbcli_ctemp(cli1->tree, "\\", &tmp_path);
1750         if (fnum1 == -1) {
1751                 printf("ctemp failed (%s)\n", smbcli_errstr(cli1->tree));
1752                 CHECK_MAX_FAILURES(error_test4);
1753                 return False;
1754         }
1755         printf("ctemp gave path %s\n", tmp_path);
1756         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1757                 printf("close of temp failed (%s)\n", smbcli_errstr(cli1->tree));
1758         }
1759         if (NT_STATUS_IS_ERR(smbcli_unlink(cli1->tree, tmp_path))) {
1760                 printf("unlink of temp failed (%s)\n", smbcli_errstr(cli1->tree));
1761         }
1762 error_test4:    
1763         /* Test the non-io opens... */
1764
1765         if (!torture_open_connection(&cli2)) {
1766                 return False;
1767         }
1768         
1769         smbcli_setatr(cli2->tree, fname, 0, 0);
1770         smbcli_unlink(cli2->tree, fname);
1771         
1772         printf("TEST #1 testing 2 non-io opens (no delete)\n");
1773         
1774         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1775                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1776
1777         if (fnum1 == -1) {
1778                 printf("test 1 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1779                 CHECK_MAX_FAILURES(error_test10);
1780                 return False;
1781         }
1782
1783         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1784                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1785         if (fnum2 == -1) {
1786                 printf("test 1 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1787                 CHECK_MAX_FAILURES(error_test10);
1788                 return False;
1789         }
1790
1791         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1792                 printf("test 1 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1793                 return False;
1794         }
1795         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1796                 printf("test 1 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1797                 return False;
1798         }
1799
1800         printf("non-io open test #1 passed.\n");
1801 error_test10:
1802         smbcli_unlink(cli1->tree, fname);
1803
1804         printf("TEST #2 testing 2 non-io opens (first with delete)\n");
1805         
1806         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1807                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1808
1809         if (fnum1 == -1) {
1810                 printf("test 2 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1811                 CHECK_MAX_FAILURES(error_test20);
1812                 return False;
1813         }
1814
1815         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1816                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1817
1818         if (fnum2 == -1) {
1819                 printf("test 2 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1820                 CHECK_MAX_FAILURES(error_test20);
1821                 return False;
1822         }
1823
1824         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1825                 printf("test 1 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1826                 return False;
1827         }
1828         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1829                 printf("test 1 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1830                 return False;
1831         }
1832
1833         printf("non-io open test #2 passed.\n");
1834 error_test20:
1835         smbcli_unlink(cli1->tree, fname);
1836
1837         printf("TEST #3 testing 2 non-io opens (second with delete)\n");
1838         
1839         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1840                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1841
1842         if (fnum1 == -1) {
1843                 printf("test 3 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1844                 CHECK_MAX_FAILURES(error_test30);
1845                 return False;
1846         }
1847
1848         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1849                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1850
1851         if (fnum2 == -1) {
1852                 printf("test 3 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1853                 CHECK_MAX_FAILURES(error_test30);
1854                 return False;
1855         }
1856
1857         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1858                 printf("test 3 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1859                 return False;
1860         }
1861         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1862                 printf("test 3 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1863                 return False;
1864         }
1865
1866         printf("non-io open test #3 passed.\n");
1867 error_test30:
1868         smbcli_unlink(cli1->tree, fname);
1869
1870         printf("TEST #4 testing 2 non-io opens (both with delete)\n");
1871         
1872         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1873                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1874
1875         if (fnum1 == -1) {
1876                 printf("test 4 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1877                 CHECK_MAX_FAILURES(error_test40);
1878                 return False;
1879         }
1880
1881         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1882                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1883
1884         if (fnum2 != -1) {
1885                 printf("test 4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1886                 CHECK_MAX_FAILURES(error_test40);
1887                 return False;
1888         }
1889
1890         printf("test 4 open 2 of %s gave %s (correct error should be %s)\n", fname, smbcli_errstr(cli2->tree), "sharing violation");
1891
1892         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1893                 printf("test 4 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1894                 return False;
1895         }
1896
1897         printf("non-io open test #4 passed.\n");
1898 error_test40:
1899         smbcli_unlink(cli1->tree, fname);
1900
1901         printf("TEST #5 testing 2 non-io opens (both with delete - both with file share delete)\n");
1902         
1903         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1904                                    NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1905
1906         if (fnum1 == -1) {
1907                 printf("test 5 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1908                 CHECK_MAX_FAILURES(error_test50);
1909                 return False;
1910         }
1911
1912         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1913                                    NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1914
1915         if (fnum2 == -1) {
1916                 printf("test 5 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1917                 CHECK_MAX_FAILURES(error_test50);
1918                 return False;
1919         }
1920
1921         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1922                 printf("test 5 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1923                 return False;
1924         }
1925
1926         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1927                 printf("test 5 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1928                 return False;
1929         }
1930
1931         printf("non-io open test #5 passed.\n");
1932 error_test50:
1933         printf("TEST #6 testing 1 non-io open, one io open\n");
1934         
1935         smbcli_unlink(cli1->tree, fname);
1936
1937         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SA_RIGHT_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
1938                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1939
1940         if (fnum1 == -1) {
1941                 printf("test 6 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1942                 CHECK_MAX_FAILURES(error_test60);
1943                 return False;
1944         }
1945
1946         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1947                                    NTCREATEX_SHARE_ACCESS_READ, NTCREATEX_DISP_OPEN_IF, 0, 0);
1948
1949         if (fnum2 == -1) {
1950                 printf("test 6 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1951                 CHECK_MAX_FAILURES(error_test60);
1952                 return False;
1953         }
1954
1955         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1956                 printf("test 6 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1957                 return False;
1958         }
1959
1960         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1961                 printf("test 6 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1962                 return False;
1963         }
1964
1965         printf("non-io open test #6 passed.\n");
1966 error_test60:
1967         printf("TEST #7 testing 1 non-io open, one io open with delete\n");
1968
1969         smbcli_unlink(cli1->tree, fname);
1970
1971         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SA_RIGHT_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
1972                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1973
1974         if (fnum1 == -1) {
1975                 printf("test 7 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1976                 CHECK_MAX_FAILURES(error_test70);
1977                 return False;
1978         }
1979
1980         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1981                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1982
1983         if (fnum2 != -1) {
1984                 printf("test 7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1985                 CHECK_MAX_FAILURES(error_test70);
1986                 return False;
1987         }
1988
1989         printf("test 7 open 2 of %s gave %s (correct error should be %s)\n", fname, smbcli_errstr(cli2->tree), "sharing violation");
1990
1991         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1992                 printf("test 7 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1993                 return False;
1994         }
1995
1996         printf("non-io open test #7 passed.\n");
1997
1998 error_test70:
1999
2000         printf("TEST #8 testing one normal open, followed by lock, followed by open with truncate\n");
2001
2002         smbcli_unlink(cli1->tree, fname);
2003
2004         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
2005         if (fnum1 == -1) {
2006                 printf("(8) open (1) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2007                 return False;
2008         }
2009         
2010         /* write 20 bytes. */
2011         
2012         memset(buf, '\0', 20);
2013
2014         if (smbcli_write(cli1->tree, fnum1, 0, buf, 0, 20) != 20) {
2015                 printf("(8) write failed (%s)\n", smbcli_errstr(cli1->tree));
2016                 correct = False;
2017         }
2018
2019         /* Ensure size == 20. */
2020         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
2021                 printf("(8) getatr (1) failed (%s)\n", smbcli_errstr(cli1->tree));
2022                 CHECK_MAX_FAILURES(error_test80);
2023                 return False;
2024         }
2025         
2026         if (fsize != 20) {
2027                 printf("(8) file size != 20\n");
2028                 CHECK_MAX_FAILURES(error_test80);
2029                 return False;
2030         }
2031
2032         /* Get an exclusive lock on the open file. */
2033         if (NT_STATUS_IS_ERR(smbcli_lock(cli1->tree, fnum1, 0, 4, 0, WRITE_LOCK))) {
2034                 printf("(8) lock1 failed (%s)\n", smbcli_errstr(cli1->tree));
2035                 CHECK_MAX_FAILURES(error_test80);
2036                 return False;
2037         }
2038
2039         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR|O_TRUNC, DENY_NONE);
2040         if (fnum1 == -1) {
2041                 printf("(8) open (2) of %s with truncate failed (%s)\n", fname, smbcli_errstr(cli1->tree));
2042                 return False;
2043         }
2044
2045         /* Ensure size == 0. */
2046         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
2047                 printf("(8) getatr (2) failed (%s)\n", smbcli_errstr(cli1->tree));
2048                 CHECK_MAX_FAILURES(error_test80);
2049                 return False;
2050         }
2051         
2052         if (fsize != 0) {
2053                 printf("(8) file size != 0\n");
2054                 CHECK_MAX_FAILURES(error_test80);
2055                 return False;
2056         }
2057
2058         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2059                 printf("(8) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
2060                 return False;
2061         }
2062         
2063         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum2))) {
2064                 printf("(8) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
2065                 return False;
2066         }
2067         
2068 error_test80:
2069
2070         printf("open test #8 passed.\n");
2071
2072         smbcli_unlink(cli1->tree, fname);
2073
2074         if (!torture_close_connection(cli1)) {
2075                 correct = False;
2076         }
2077         if (!torture_close_connection(cli2)) {
2078                 correct = False;
2079         }
2080         
2081         return correct;
2082 }
2083
2084
2085 /*
2086   sees what IOCTLs are supported
2087  */
2088 BOOL torture_ioctl_test(int dummy)
2089 {
2090         struct smbcli_state *cli;
2091         uint16_t device, function;
2092         int fnum;
2093         const char *fname = "\\ioctl.dat";
2094         NTSTATUS status;
2095         union smb_ioctl parms;
2096         TALLOC_CTX *mem_ctx;
2097
2098         if (!torture_open_connection(&cli)) {
2099                 return False;
2100         }
2101
2102         mem_ctx = talloc_init("ioctl_test");
2103
2104         printf("starting ioctl test\n");
2105
2106         smbcli_unlink(cli->tree, fname);
2107
2108         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2109         if (fnum == -1) {
2110                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
2111                 return False;
2112         }
2113
2114         parms.ioctl.level = RAW_IOCTL_IOCTL;
2115         parms.ioctl.in.fnum = fnum;
2116         parms.ioctl.in.request = IOCTL_QUERY_JOB_INFO;
2117         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
2118         printf("ioctl job info: %s\n", smbcli_errstr(cli->tree));
2119
2120         for (device=0;device<0x100;device++) {
2121                 printf("testing device=0x%x\n", device);
2122                 for (function=0;function<0x100;function++) {
2123                         parms.ioctl.in.request = (device << 16) | function;
2124                         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
2125
2126                         if (NT_STATUS_IS_OK(status)) {
2127                                 printf("ioctl device=0x%x function=0x%x OK : %d bytes\n", 
2128                                         device, function, parms.ioctl.out.blob.length);
2129                         }
2130                 }
2131         }
2132
2133         if (!torture_close_connection(cli)) {
2134                 return False;
2135         }
2136
2137         return True;
2138 }
2139
2140
2141 /*
2142   tries variants of chkpath
2143  */
2144 BOOL torture_chkpath_test(int dummy)
2145 {
2146         struct smbcli_state *cli;
2147         int fnum;
2148         BOOL ret;
2149
2150         if (!torture_open_connection(&cli)) {
2151                 return False;
2152         }
2153
2154         printf("starting chkpath test\n");
2155
2156         printf("Testing valid and invalid paths\n");
2157
2158         /* cleanup from an old run */
2159         smbcli_rmdir(cli->tree, "\\chkpath.dir\\dir2");
2160         smbcli_unlink(cli->tree, "\\chkpath.dir\\*");
2161         smbcli_rmdir(cli->tree, "\\chkpath.dir");
2162
2163         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\chkpath.dir"))) {
2164                 printf("mkdir1 failed : %s\n", smbcli_errstr(cli->tree));
2165                 return False;
2166         }
2167
2168         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\chkpath.dir\\dir2"))) {
2169                 printf("mkdir2 failed : %s\n", smbcli_errstr(cli->tree));
2170                 return False;
2171         }
2172
2173         fnum = smbcli_open(cli->tree, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2174         if (fnum == -1) {
2175                 printf("open1 failed (%s)\n", smbcli_errstr(cli->tree));
2176                 return False;
2177         }
2178         smbcli_close(cli->tree, fnum);
2179
2180         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir"))) {
2181                 printf("chkpath1 failed: %s\n", smbcli_errstr(cli->tree));
2182                 ret = False;
2183         }
2184
2185         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\dir2"))) {
2186                 printf("chkpath2 failed: %s\n", smbcli_errstr(cli->tree));
2187                 ret = False;
2188         }
2189
2190         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\foo.txt"))) {
2191                 ret = check_error(__location__, cli, ERRDOS, ERRbadpath, 
2192                                   NT_STATUS_NOT_A_DIRECTORY);
2193         } else {
2194                 printf("* chkpath on a file should fail\n");
2195                 ret = False;
2196         }
2197
2198         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\bar.txt"))) {
2199                 ret = check_error(__location__, cli, ERRDOS, ERRbadfile, 
2200                                   NT_STATUS_OBJECT_NAME_NOT_FOUND);
2201         } else {
2202                 printf("* chkpath on a non existent file should fail\n");
2203                 ret = False;
2204         }
2205
2206         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\dirxx\\bar.txt"))) {
2207                 ret = check_error(__location__, cli, ERRDOS, ERRbadpath, 
2208                                   NT_STATUS_OBJECT_PATH_NOT_FOUND);
2209         } else {
2210                 printf("* chkpath on a non existent component should fail\n");
2211                 ret = False;
2212         }
2213
2214         smbcli_rmdir(cli->tree, "\\chkpath.dir\\dir2");
2215         smbcli_unlink(cli->tree, "\\chkpath.dir\\*");
2216         smbcli_rmdir(cli->tree, "\\chkpath.dir");
2217
2218         if (!torture_close_connection(cli)) {
2219                 return False;
2220         }
2221
2222         return ret;
2223 }
2224
2225
2226 /*
2227   parse a //server/share type UNC name
2228 */
2229 static BOOL parse_unc(const char *unc_name, char **hostname, char **sharename)
2230 {
2231         char *p;
2232
2233         if (strncmp(unc_name, "//", 2)) {
2234                 return False;
2235         }
2236
2237         *hostname = strdup(&unc_name[2]);
2238         p = strchr_m(&(*hostname)[2],'/');
2239         if (!p) {
2240                 return False;
2241         }
2242         *p = 0;
2243         *sharename = strdup(p+1);
2244
2245         return True;
2246 }
2247
2248
2249
2250 static void sigcont(int sig)
2251 {
2252 }
2253
2254 double torture_create_procs(BOOL (*fn)(struct smbcli_state *, int), BOOL *result)
2255 {
2256         int i, status;
2257         volatile pid_t *child_status;
2258         volatile BOOL *child_status_out;
2259         int synccount;
2260         int tries = 8;
2261         double start_time_limit = 10 + (torture_nprocs * 1.5);
2262         char **unc_list = NULL;
2263         const char *p;
2264         int num_unc_names = 0;
2265
2266         synccount = 0;
2267
2268         signal(SIGCONT, sigcont);
2269
2270         child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*torture_nprocs);
2271         if (!child_status) {
2272                 printf("Failed to setup shared memory\n");
2273                 return -1;
2274         }
2275
2276         child_status_out = (volatile BOOL *)shm_setup(sizeof(BOOL)*torture_nprocs);
2277         if (!child_status_out) {
2278                 printf("Failed to setup result status shared memory\n");
2279                 return -1;
2280         }
2281
2282         p = lp_parm_string(-1, "torture", "unclist");
2283         if (p) {
2284                 unc_list = file_lines_load(p, &num_unc_names);
2285                 if (!unc_list || num_unc_names <= 0) {
2286                         printf("Failed to load unc names list from '%s'\n", p);
2287                         exit(1);
2288                 }
2289         }
2290
2291         for (i = 0; i < torture_nprocs; i++) {
2292                 child_status[i] = 0;
2293                 child_status_out[i] = True;
2294         }
2295
2296         start_timer();
2297
2298         for (i=0;i<torture_nprocs;i++) {
2299                 procnum = i;
2300                 if (fork() == 0) {
2301                         char *myname;
2302                         char *hostname=NULL, *sharename;
2303
2304                         pid_t mypid = getpid();
2305                         sys_srandom(((int)mypid) ^ ((int)time(NULL)));
2306
2307                         asprintf(&myname, "CLIENT%d", i);
2308                         lp_set_cmdline("netbios name", myname);
2309                         free(myname);
2310
2311
2312                         if (unc_list) {
2313                                 if (!parse_unc(unc_list[i % num_unc_names],
2314                                                &hostname, &sharename)) {
2315                                         printf("Failed to parse UNC name %s\n",
2316                                                unc_list[i % num_unc_names]);
2317                                         exit(1);
2318                                 }
2319                         }
2320
2321                         while (1) {
2322                                 if (hostname) {
2323                                         if (torture_open_connection_share(&current_cli,
2324                                                                           hostname, 
2325                                                                           sharename)) {
2326                                                 break;
2327                                         }
2328                                 } else if (torture_open_connection(&current_cli)) {
2329                                                 break;
2330                                 }
2331                                 if (tries-- == 0) {
2332                                         printf("pid %d failed to start\n", (int)getpid());
2333                                         _exit(1);
2334                                 }
2335                                 msleep(100);    
2336                         }
2337
2338                         child_status[i] = getpid();
2339
2340                         pause();
2341
2342                         if (child_status[i]) {
2343                                 printf("Child %d failed to start!\n", i);
2344                                 child_status_out[i] = 1;
2345                                 _exit(1);
2346                         }
2347
2348                         child_status_out[i] = fn(current_cli, i);
2349                         _exit(0);
2350                 }
2351         }
2352
2353         do {
2354                 synccount = 0;
2355                 for (i=0;i<torture_nprocs;i++) {
2356                         if (child_status[i]) synccount++;
2357                 }
2358                 if (synccount == torture_nprocs) break;
2359                 msleep(100);
2360         } while (end_timer() < start_time_limit);
2361
2362         if (synccount != torture_nprocs) {
2363                 printf("FAILED TO START %d CLIENTS (started %d)\n", torture_nprocs, synccount);
2364                 *result = False;
2365                 return end_timer();
2366         }
2367
2368         printf("Starting %d clients\n", torture_nprocs);
2369
2370         /* start the client load */
2371         start_timer();
2372         for (i=0;i<torture_nprocs;i++) {
2373                 child_status[i] = 0;
2374         }
2375         kill(0, SIGCONT);
2376
2377         printf("%d clients started\n", torture_nprocs);
2378
2379         for (i=0;i<torture_nprocs;i++) {
2380                 int ret;
2381                 while ((ret=waitpid(0, &status, 0)) == -1 && errno == EINTR) /* noop */ ;
2382                 if (ret == -1 || WEXITSTATUS(status) != 0) {
2383                         *result = False;
2384                 }
2385         }
2386
2387         printf("\n");
2388         
2389         for (i=0;i<torture_nprocs;i++) {
2390                 if (!child_status_out[i]) {
2391                         *result = False;
2392                 }
2393         }
2394         return end_timer();
2395 }
2396
2397 #define FLAG_MULTIPROC 1
2398
2399 static struct {
2400         const char *name;
2401         BOOL (*fn)(int);
2402         uint_t flags;
2403 } torture_ops[] = {
2404         /* base tests */
2405         {"BASE-FDPASS", run_fdpasstest, 0},
2406         {"BASE-LOCK1",  torture_locktest1,  0},
2407         {"BASE-LOCK2",  torture_locktest2,  0},
2408         {"BASE-LOCK3",  torture_locktest3,  0},
2409         {"BASE-LOCK4",  torture_locktest4,  0},
2410         {"BASE-LOCK5",  torture_locktest5,  0},
2411         {"BASE-LOCK6",  torture_locktest6,  0},
2412         {"BASE-LOCK7",  torture_locktest7,  0},
2413         {"BASE-UNLINK", run_unlinktest, 0},
2414         {"BASE-ATTR",   run_attrtest,   0},
2415         {"BASE-TRANS2", run_trans2test, 0},
2416         {"BASE-NEGNOWAIT", run_negprot_nowait, 0},
2417         {"BASE-DIR1",  torture_dirtest1, 0},
2418         {"BASE-DIR2",  torture_dirtest2, 0},
2419         {"BASE-DENY1",  torture_denytest1, 0},
2420         {"BASE-DENY2",  torture_denytest2, 0},
2421         {"BASE-DENY3",  torture_denytest3, 0},
2422         {"BASE-TCON",  run_tcon_test, 0},
2423         {"BASE-TCONDEV",  run_tcon_devtype_test, 0},
2424         {"BASE-VUID", run_vuidtest, 0},
2425         {"BASE-RW1",  run_readwritetest, 0},
2426         {"BASE-RW2",  run_readwritemulti, FLAG_MULTIPROC},
2427         {"BASE-OPEN", run_opentest, 0},
2428         {"BASE-DEFER_OPEN", run_deferopen, FLAG_MULTIPROC},
2429         {"BASE-XCOPY", run_xcopy, 0},
2430         {"BASE-RENAME", torture_test_rename, 0},
2431         {"BASE-DELETE", torture_test_delete, 0},
2432         {"BASE-PROPERTIES", run_properties, 0},
2433         {"BASE-MANGLE", torture_mangle, 0},
2434         {"BASE-OPENATTR", torture_openattrtest, 0},
2435         {"BASE-CHARSET", torture_charset, 0},
2436         {"BASE-CHKPATH",  torture_chkpath_test, 0},
2437         {"BASE-SECLEAK",  torture_sec_leak, 0},
2438
2439         /* benchmarking tests */
2440         {"BENCH-HOLDCON",  torture_holdcon, 0},
2441         {"BENCH-NBENCH",  torture_nbench, 0},
2442         {"BENCH-TORTURE",run_torture,    FLAG_MULTIPROC},
2443
2444         /* RAW smb tests */
2445         {"RAW-QFSINFO", torture_raw_qfsinfo, 0},
2446         {"RAW-QFILEINFO", torture_raw_qfileinfo, 0},
2447         {"RAW-SFILEINFO", torture_raw_sfileinfo, 0},
2448         {"RAW-SFILEINFO-BUG", torture_raw_sfileinfo_bug, 0},
2449         {"RAW-SEARCH", torture_raw_search, 0},
2450         {"RAW-CLOSE", torture_raw_close, 0},
2451         {"RAW-OPEN", torture_raw_open, 0},
2452         {"RAW-MKDIR", torture_raw_mkdir, 0},
2453         {"RAW-OPLOCK", torture_raw_oplock, 0},
2454         {"RAW-NOTIFY", torture_raw_notify, 0},
2455         {"RAW-MUX", torture_raw_mux, 0},
2456         {"RAW-IOCTL", torture_raw_ioctl, 0},
2457         {"RAW-CHKPATH", torture_raw_chkpath, 0},
2458         {"RAW-UNLINK", torture_raw_unlink, 0},
2459         {"RAW-READ", torture_raw_read, 0},
2460         {"RAW-WRITE", torture_raw_write, 0},
2461         {"RAW-LOCK", torture_raw_lock, 0},
2462         {"RAW-CONTEXT", torture_raw_context, 0},
2463         {"RAW-RENAME", torture_raw_rename, 0},
2464         {"RAW-SEEK", torture_raw_seek, 0},
2465         {"RAW-RAP", torture_raw_rap, 0},
2466
2467         /* protocol scanners */
2468         {"SCAN-TRANS2", torture_trans2_scan, 0},
2469         {"SCAN-NTTRANS", torture_nttrans_scan, 0},
2470         {"SCAN-ALIASES", torture_trans2_aliases, 0},
2471         {"SCAN-SMB", torture_smb_scan, 0},
2472         {"SCAN-MAXFID", run_maxfidtest, FLAG_MULTIPROC},
2473         {"SCAN-UTABLE", torture_utable, 0},
2474         {"SCAN-CASETABLE", torture_casetable, 0},
2475         {"SCAN-PIPE_NUMBER", run_pipe_number, 0},
2476         {"SCAN-IOCTL",  torture_ioctl_test, 0},
2477
2478         /* rpc testers */
2479         {"RPC-LSA", torture_rpc_lsa, 0},
2480         {"RPC-ECHO", torture_rpc_echo, 0},
2481         {"RPC-DFS", torture_rpc_dfs, 0},
2482         {"RPC-SPOOLSS", torture_rpc_spoolss, 0},
2483         {"RPC-SAMR", torture_rpc_samr, 0},
2484         {"RPC-NETLOGON", torture_rpc_netlogon, 0},
2485         {"RPC-SCHANNEL", torture_rpc_schannel, 0},
2486         {"RPC-WKSSVC", torture_rpc_wkssvc, 0},
2487         {"RPC-SRVSVC", torture_rpc_srvsvc, 0},
2488         {"RPC-SVCCTL", torture_rpc_svcctl, 0},
2489         {"RPC-ATSVC", torture_rpc_atsvc, 0},
2490         {"RPC-EVENTLOG", torture_rpc_eventlog, 0},
2491         {"RPC-EPMAPPER", torture_rpc_epmapper, 0},
2492         {"RPC-WINREG", torture_rpc_winreg, 0},
2493         {"RPC-OXIDRESOLVE", torture_rpc_oxidresolve, 0},
2494         {"RPC-MGMT", torture_rpc_mgmt, 0},
2495         {"RPC-SCANNER", torture_rpc_scanner, 0},
2496         {"RPC-AUTOIDL", torture_rpc_autoidl, 0},
2497         {"RPC-COUNTCALLS", torture_rpc_countcalls, 0},
2498         {"RPC-MULTIBIND", torture_multi_bind, 0},
2499         {"RPC-DRSUAPI", torture_rpc_drsuapi, 0},
2500
2501         /* local (no server) testers */
2502         {"LOCAL-NTLMSSP", torture_ntlmssp_self_check, 0},
2503         {"LOCAL-ICONV", torture_local_iconv, 0},
2504         {"LOCAL-TALLOC", torture_local_talloc, 0},
2505         {"LOCAL-MESSAGING", torture_local_messaging, 0},
2506         {"LOCAL-BINDING", torture_local_binding_string, 0},
2507         {"LOCAL-IDTREE", torture_local_idtree, 0},
2508
2509         /* ldap testers */
2510         {"LDAP-BASIC", torture_ldap_basic, 0},
2511
2512         {NULL, NULL, 0}};
2513
2514
2515
2516 /****************************************************************************
2517 run a specified test or "ALL"
2518 ****************************************************************************/
2519 static BOOL run_test(const char *name)
2520 {
2521         BOOL ret = True;
2522         int i;
2523         BOOL matched = False;
2524
2525         if (strequal(name,"ALL")) {
2526                 for (i=0;torture_ops[i].name;i++) {
2527                         if (!run_test(torture_ops[i].name)) {
2528                                 ret = False;
2529                         }
2530                 }
2531                 return ret;
2532         }
2533
2534         for (i=0;torture_ops[i].name;i++) {
2535                 if (gen_fnmatch(name, torture_ops[i].name) == 0) {
2536                         double t;
2537                         matched = True;
2538                         init_iconv();
2539                         printf("Running %s\n", torture_ops[i].name);
2540                         if (torture_ops[i].flags & FLAG_MULTIPROC) {
2541                                 BOOL result;
2542                                 t = torture_create_procs(torture_ops[i].fn, &result);
2543                                 if (!result) { 
2544                                         ret = False;
2545                                         printf("TEST %s FAILED!\n", torture_ops[i].name);
2546                                 }
2547                                          
2548                         } else {
2549                                 start_timer();
2550                                 if (!torture_ops[i].fn(0)) {
2551                                         ret = False;
2552                                         printf("TEST %s FAILED!\n", torture_ops[i].name);
2553                                 }
2554                                 t = end_timer();
2555                         }
2556                         printf("%s took %g secs\n\n", torture_ops[i].name, t);
2557                 }
2558         }
2559
2560         if (!matched) {
2561                 printf("Unknown torture operation '%s'\n", name);
2562         }
2563
2564         return ret;
2565 }
2566
2567
2568 static void parse_dns(const char *dns)
2569 {
2570         char *userdn, *basedn, *secret;
2571         char *p, *d;
2572
2573         /* retrievieng the userdn */
2574         p = strchr_m(dns, '#');
2575         if (!p) {
2576                 lp_set_cmdline("torture:ldap_userdn", "");
2577                 lp_set_cmdline("torture:ldap_basedn", "");
2578                 lp_set_cmdline("torture:ldap_secret", "");
2579                 return;
2580         }
2581         userdn = strndup(dns, p - dns);
2582         lp_set_cmdline("torture:ldap_userdn", userdn);
2583
2584         /* retrieve the basedn */
2585         d = p + 1;
2586         p = strchr_m(d, '#');
2587         if (!p) {
2588                 lp_set_cmdline("torture:ldap_basedn", "");
2589                 lp_set_cmdline("torture:ldap_secret", "");
2590                 return;
2591         }
2592         basedn = strndup(d, p - d);
2593         lp_set_cmdline("torture:ldap_basedn", basedn);
2594
2595         /* retrieve the secret */
2596         p = p + 1;
2597         if (!p) {
2598                 lp_set_cmdline("torture:ldap_secret", "");
2599                 return;
2600         }
2601         secret = strdup(p);
2602         lp_set_cmdline("torture:ldap_secret", secret);
2603
2604         printf ("%s - %s - %s\n", userdn, basedn, secret);
2605
2606 }
2607
2608 static void usage(poptContext pc)
2609 {
2610         int i;
2611         int perline = 5;
2612
2613         poptPrintUsage(pc, stdout, 0);
2614         printf("\n");
2615
2616         printf("tests are:");
2617         for (i=0;torture_ops[i].name;i++) {
2618                 if ((i%perline)==0) {
2619                         printf("\n");
2620                 }
2621                 printf("%s ", torture_ops[i].name);
2622         }
2623         printf("\n\n");
2624
2625         printf("default test is ALL\n");
2626         
2627         exit(1);
2628 }
2629
2630 static BOOL is_binding_string(const char *binding_string)
2631 {
2632         TALLOC_CTX *mem_ctx = talloc_init("is_binding_string");
2633         struct dcerpc_binding binding_struct;
2634         NTSTATUS status;
2635         
2636         status = dcerpc_parse_binding(mem_ctx, binding_string, &binding_struct);
2637
2638         talloc_destroy(mem_ctx);
2639         return NT_STATUS_IS_OK(status);
2640 }
2641
2642 /****************************************************************************
2643   main program
2644 ****************************************************************************/
2645  int main(int argc,char *argv[])
2646 {
2647         int opt, i;
2648         char *p;
2649         BOOL correct = True;
2650         int argc_new;
2651         char **argv_new;
2652         poptContext pc;
2653         enum {OPT_LOADFILE=1000,OPT_UNCLIST,OPT_TIMELIMIT,OPT_DNS,OPT_DANGEROUS};
2654         struct poptOption long_options[] = {
2655                 POPT_AUTOHELP
2656                 {"smb-ports",   'p', POPT_ARG_STRING, NULL,             0,      "SMB ports",    NULL},
2657                 {"seed",          0, POPT_ARG_STRING, NULL,             0,      "seed",         NULL},
2658                 {"num-progs",     0, POPT_ARG_INT,  &torture_nprocs,    0,      "num progs",    NULL},
2659                 {"num-ops",       0, POPT_ARG_INT,  &torture_numops,    0,      "num ops",      NULL},
2660                 {"entries",       0, POPT_ARG_INT,  &torture_entries,   0,      "entries",      NULL},
2661                 {"use-oplocks", 'L', POPT_ARG_NONE, &use_oplocks,       0,      "use oplocks",  NULL},
2662                 {"show-all",      0, POPT_ARG_NONE, &torture_showall,   0,      "show all",     NULL},
2663                 {"loadfile",      0, POPT_ARG_STRING,   NULL,   OPT_LOADFILE,   "loadfile",     NULL},
2664                 {"unclist",       0, POPT_ARG_STRING,   NULL,   OPT_UNCLIST,    "unclist",      NULL},
2665                 {"timelimit",   't', POPT_ARG_STRING,   NULL,   OPT_TIMELIMIT,  "timelimit",    NULL},
2666                 {"failures",    'f', POPT_ARG_INT,  &torture_failures,  0,      "failures",     NULL},
2667                 {"parse-dns",   'D', POPT_ARG_STRING,   NULL,   OPT_DNS,        "parse-dns",    NULL},
2668                 {"dangerous",   'X', POPT_ARG_NONE,     NULL,   OPT_DANGEROUS,  "dangerous",    NULL},
2669                 POPT_COMMON_SAMBA
2670                 POPT_COMMON_CONNECTION
2671                 POPT_COMMON_CREDENTIALS
2672                 POPT_COMMON_VERSION
2673                 POPT_TABLEEND
2674         };
2675
2676         setup_logging("smbtorture", DEBUG_STDOUT);
2677
2678 #ifdef HAVE_SETBUFFER
2679         setbuffer(stdout, NULL, 0);
2680 #endif
2681
2682         pc = poptGetContext("smbtorture", argc, (const char **) argv, long_options, 
2683                                 POPT_CONTEXT_KEEP_FIRST);
2684
2685         poptSetOtherOptionHelp(pc, "<binding>|<unc> TEST1 TEST2 ...");
2686
2687         while((opt = poptGetNextOpt(pc)) != -1) {
2688                 switch (opt) {
2689                 case OPT_LOADFILE:
2690                         lp_set_cmdline("torture:loadfile", poptGetOptArg(pc));
2691                         break;
2692                 case OPT_UNCLIST:
2693                         lp_set_cmdline("torture:unclist", poptGetOptArg(pc));
2694                         break;
2695                 case OPT_TIMELIMIT:
2696                         lp_set_cmdline("torture:timelimit", poptGetOptArg(pc));
2697                         break;
2698                 case OPT_DNS:
2699                         parse_dns(poptGetOptArg(pc));
2700                         break;
2701                 case OPT_DANGEROUS:
2702                         lp_set_cmdline("torture:dangerous", "1");
2703                         break;
2704                 default:
2705                         d_printf("Invalid option %s: %s\n", 
2706                                  poptBadOption(pc, 0), poptStrerror(opt));
2707                         usage(pc);
2708                         exit(1);
2709                 }
2710         }
2711
2712         lp_load(dyn_CONFIGFILE,True,False,False);
2713         load_interfaces();
2714         srandom(time(NULL));
2715
2716         argv_new = (const char **)poptGetArgs(pc);
2717
2718         argc_new = argc;
2719         for (i=0; i<argc; i++) {
2720                 if (argv_new[i] == NULL) {
2721                         argc_new = i;
2722                         break;
2723                 }
2724         }
2725
2726         if (argc_new < 3) {
2727                 usage(pc);
2728                 exit(1);
2729         }
2730
2731         for(p = argv_new[1]; *p; p++) {
2732                 if(*p == '\\')
2733                         *p = '/';
2734         }
2735
2736         /* see if its a RPC transport specifier */
2737         if (is_binding_string(argv_new[1])) {
2738                 lp_set_cmdline("torture:binding", argv_new[1]);
2739         } else {
2740                 char *binding = NULL;
2741                 char *host = NULL, *share = NULL;
2742
2743                 if (!parse_unc(argv_new[1], &host, &share)) {
2744                         usage(pc);
2745                 }
2746
2747                 lp_set_cmdline("torture:host", host);
2748                 lp_set_cmdline("torture:share", share);
2749                 asprintf(&binding, "ncacn_np:%s", host);
2750                 lp_set_cmdline("torture:binding", binding);
2751         }
2752
2753         if (!lp_parm_string(-1,"torture","username")) {
2754                 lp_set_cmdline("torture:username", cmdline_get_username());
2755         }
2756         if (!lp_parm_string(-1,"torture","userdomain")) {
2757                 /* 
2758                  * backward compatibility
2759                  * maybe we should remove this to make this consistent
2760                  * for all cmdline tools
2761                  * --metze
2762                  */
2763                 if (strequal(lp_netbios_name(),cmdline_get_userdomain())) {
2764                         cmdline_set_userdomain(lp_workgroup());
2765                 }
2766                 lp_set_cmdline("torture:userdomain", cmdline_get_userdomain());
2767         }
2768         if (!lp_parm_string(-1,"torture","password")) {
2769                 lp_set_cmdline("torture:password", cmdline_get_userpassword());
2770         }
2771
2772         if (argc_new == 0) {
2773                 printf("You must specify a test to run, or 'ALL'\n");
2774         } else {
2775                 for (i=2;i<argc_new;i++) {
2776                         if (!run_test(argv_new[i])) {
2777                                 correct = False;
2778                         }
2779                 }
2780         }
2781
2782         if (correct) {
2783                 return(0);
2784         } else {
2785                 return(1);
2786         }
2787 }