r3699: - split the delayed write testing out of RAW-WRITE, as it is not yet
[gd/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 #include "dynconfig.h"
23 #include "client.h"
24 #include "lib/cmdline/popt_common.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "system/time.h"
27 #include "system/wait.h"
28 #include "ioctl.h"
29
30 int torture_nprocs=4;
31 int torture_numops=100;
32 int torture_entries=1000;
33 int torture_failures=1;
34 int torture_seed=0;
35 static int procnum; /* records process count number when forking */
36 static struct smbcli_state *current_cli;
37 static BOOL use_oplocks;
38 static BOOL use_level_II_oplocks;
39 static BOOL use_kerberos;
40
41 BOOL torture_showall = False;
42
43 #define CHECK_MAX_FAILURES(label) do { if (++failures >= torture_failures) goto label; } while (0)
44
45 static struct smbcli_state *open_nbt_connection(void)
46 {
47         struct nmb_name called, calling;
48         struct smbcli_state *cli;
49         const char *host = lp_parm_string(-1, "torture", "host");
50
51         make_nmb_name(&calling, lp_netbios_name(), 0x0);
52         choose_called_name(&called, host, 0x20);
53
54         cli = smbcli_state_init(NULL);
55         if (!cli) {
56                 printf("Failed initialize smbcli_struct to connect with %s\n", host);
57                 return NULL;
58         }
59
60         if (!smbcli_socket_connect(cli, host)) {
61                 printf("Failed to connect with %s\n", host);
62                 return cli;
63         }
64
65         cli->transport->socket->timeout = 120000; /* set a really long timeout (2 minutes) */
66
67         if (!smbcli_transport_establish(cli, &calling, &called)) {
68                 /*
69                  * Well, that failed, try *SMBSERVER ... 
70                  * However, we must reconnect as well ...
71                  */
72                 if (!smbcli_socket_connect(cli, host)) {
73                         printf("Failed to connect with %s\n", host);
74                         return False;
75                 }
76
77                 make_nmb_name(&called, "*SMBSERVER", 0x20);
78                 if (!smbcli_transport_establish(cli, &calling, &called)) {
79                         printf("%s rejected the session\n",host);
80                         printf("We tried with a called name of %s & %s\n",
81                                 host, "*SMBSERVER");
82                         smbcli_shutdown(cli);
83                         return NULL;
84                 }
85         }
86
87         return cli;
88 }
89
90 BOOL torture_open_connection_share(struct smbcli_state **c, 
91                                    const char *hostname, 
92                                    const char *sharename)
93 {
94         BOOL retry;
95         int flags = 0;
96         NTSTATUS status;
97         const char *username = lp_parm_string(-1, "torture", "username");
98         const char *userdomain = lp_parm_string(-1, "torture", "userdomain");
99         const char *password = lp_parm_string(-1, "torture", "password");
100
101         if (use_kerberos)
102                 flags |= SMBCLI_FULL_CONNECTION_USE_KERBEROS;
103
104         status = smbcli_full_connection(NULL,
105                                         c, lp_netbios_name(),
106                                         hostname, NULL, 
107                                         sharename, "?????", 
108                                         username, username[0]?userdomain:"",
109                                         password, flags, &retry);
110         if (!NT_STATUS_IS_OK(status)) {
111                 printf("Failed to open connection - %s\n", nt_errstr(status));
112                 return False;
113         }
114
115         (*c)->transport->options.use_oplocks = use_oplocks;
116         (*c)->transport->options.use_level2_oplocks = use_level_II_oplocks;
117         (*c)->transport->socket->timeout = 120000;
118
119         return True;
120 }
121
122 BOOL torture_open_connection(struct smbcli_state **c)
123 {
124         const char *host = lp_parm_string(-1, "torture", "host");
125         const char *share = lp_parm_string(-1, "torture", "share");
126
127         return torture_open_connection_share(c, host, share);
128 }
129
130
131
132 BOOL torture_close_connection(struct smbcli_state *c)
133 {
134         BOOL ret = True;
135         if (!c) return True;
136         if (NT_STATUS_IS_ERR(smbcli_tdis(c))) {
137                 printf("tdis failed (%s)\n", smbcli_errstr(c->tree));
138                 ret = False;
139         }
140         smbcli_shutdown(c);
141         return ret;
142 }
143
144
145 /* open a rpc connection to the chosen binding string */
146 NTSTATUS torture_rpc_connection(struct dcerpc_pipe **p, 
147                                 const char *pipe_name,
148                                 const char *pipe_uuid, 
149                                 uint32_t pipe_version)
150 {
151         NTSTATUS status;
152         const char *binding = lp_parm_string(-1, "torture", "binding");
153
154         if (!binding) {
155                 printf("You must specify a ncacn binding string\n");
156                 return NT_STATUS_INVALID_PARAMETER;
157         }
158
159         status = dcerpc_pipe_connect(p, binding, pipe_uuid, pipe_version,
160                                      lp_parm_string(-1, "torture", "userdomain"), 
161                                      lp_parm_string(-1, "torture", "username"),
162                                      lp_parm_string(-1, "torture", "password"));
163  
164         return status;
165 }
166
167 /* open a rpc connection to a specific transport */
168 NTSTATUS torture_rpc_connection_transport(struct dcerpc_pipe **p, 
169                                           const char *pipe_name,
170                                           const char *pipe_uuid, 
171                                           uint32_t pipe_version,
172                                           enum dcerpc_transport_t transport)
173 {
174         NTSTATUS status;
175         const char *binding = lp_parm_string(-1, "torture", "binding");
176         struct dcerpc_binding b;
177         TALLOC_CTX *mem_ctx = talloc_init("torture_rpc_connection_smb");
178
179         if (!binding) {
180                 printf("You must specify a ncacn binding string\n");
181                 return NT_STATUS_INVALID_PARAMETER;
182         }
183
184         status = dcerpc_parse_binding(mem_ctx, binding, &b);
185         if (!NT_STATUS_IS_OK(status)) {
186                 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
187                 talloc_destroy(mem_ctx);
188                 return status;
189         }
190
191         b.transport = transport;
192
193         status = dcerpc_pipe_connect_b(p, &b, pipe_uuid, pipe_version,
194                                        lp_parm_string(-1, "torture", "userdomain"), 
195                                        lp_parm_string(-1, "torture", "username"),
196                                        lp_parm_string(-1, "torture", "password"));
197  
198         return status;
199 }
200
201 /* close a rpc connection to a named pipe */
202 NTSTATUS torture_rpc_close(struct dcerpc_pipe *p)
203 {
204         dcerpc_pipe_close(p);
205         return NT_STATUS_OK;
206 }
207
208
209 /* check if the server produced the expected error code */
210 BOOL check_error(const char *location, struct smbcli_state *c, 
211                  uint8_t eclass, uint32_t ecode, NTSTATUS nterr)
212 {
213         if (smbcli_is_dos_error(c->tree)) {
214                 uint8_t class;
215                 uint32_t num;
216
217                 /* Check DOS error */
218
219                 smbcli_dos_error(c, &class, &num);
220
221                 if (eclass != class || ecode != num) {
222                         printf("unexpected error code class=%d code=%d\n", 
223                                (int)class, (int)num);
224                         printf(" expected %d/%d %s (at %s)\n", 
225                                (int)eclass, (int)ecode, nt_errstr(nterr), location);
226                         return False;
227                 }
228
229         } else {
230                 NTSTATUS status;
231
232                 /* Check NT error */
233
234                 status = smbcli_nt_error(c->tree);
235
236                 if (NT_STATUS_V(nterr) != NT_STATUS_V(status)) {
237                         printf("unexpected error code %s\n", nt_errstr(status));
238                         printf(" expected %s (at %s)\n", nt_errstr(nterr), location);
239                         return False;
240                 }
241         }
242
243         return True;
244 }
245
246
247 static BOOL wait_lock(struct smbcli_state *c, int fnum, uint32_t offset, uint32_t len)
248 {
249         while (NT_STATUS_IS_ERR(smbcli_lock(c->tree, fnum, offset, len, -1, WRITE_LOCK))) {
250                 if (!check_error(__location__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
251         }
252         return True;
253 }
254
255
256 static BOOL rw_torture(struct smbcli_state *c)
257 {
258         const char *lockfname = "\\torture.lck";
259         char *fname;
260         int fnum;
261         int fnum2;
262         pid_t pid2, pid = getpid();
263         int i, j;
264         char buf[1024];
265         BOOL correct = True;
266
267         fnum2 = smbcli_open(c->tree, lockfname, O_RDWR | O_CREAT | O_EXCL, 
268                          DENY_NONE);
269         if (fnum2 == -1)
270                 fnum2 = smbcli_open(c->tree, lockfname, O_RDWR, DENY_NONE);
271         if (fnum2 == -1) {
272                 printf("open of %s failed (%s)\n", lockfname, smbcli_errstr(c->tree));
273                 return False;
274         }
275
276
277         for (i=0;i<torture_numops;i++) {
278                 uint_t n = (uint_t)sys_random()%10;
279                 if (i % 10 == 0) {
280                         printf("%d\r", i); fflush(stdout);
281                 }
282                 asprintf(&fname, "\\torture.%u", n);
283
284                 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
285                         return False;
286                 }
287
288                 fnum = smbcli_open(c->tree, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL);
289                 if (fnum == -1) {
290                         printf("open failed (%s)\n", smbcli_errstr(c->tree));
291                         correct = False;
292                         break;
293                 }
294
295                 if (smbcli_write(c->tree, fnum, 0, (char *)&pid, 0, sizeof(pid)) != sizeof(pid)) {
296                         printf("write failed (%s)\n", smbcli_errstr(c->tree));
297                         correct = False;
298                 }
299
300                 for (j=0;j<50;j++) {
301                         if (smbcli_write(c->tree, fnum, 0, (char *)buf, 
302                                       sizeof(pid)+(j*sizeof(buf)), 
303                                       sizeof(buf)) != sizeof(buf)) {
304                                 printf("write failed (%s)\n", smbcli_errstr(c->tree));
305                                 correct = False;
306                         }
307                 }
308
309                 pid2 = 0;
310
311                 if (smbcli_read(c->tree, fnum, (char *)&pid2, 0, sizeof(pid)) != sizeof(pid)) {
312                         printf("read failed (%s)\n", smbcli_errstr(c->tree));
313                         correct = False;
314                 }
315
316                 if (pid2 != pid) {
317                         printf("data corruption!\n");
318                         correct = False;
319                 }
320
321                 if (NT_STATUS_IS_ERR(smbcli_close(c->tree, fnum))) {
322                         printf("close failed (%s)\n", smbcli_errstr(c->tree));
323                         correct = False;
324                 }
325
326                 if (NT_STATUS_IS_ERR(smbcli_unlink(c->tree, fname))) {
327                         printf("unlink failed (%s)\n", smbcli_errstr(c->tree));
328                         correct = False;
329                 }
330
331                 if (NT_STATUS_IS_ERR(smbcli_unlock(c->tree, fnum2, n*sizeof(int), sizeof(int)))) {
332                         printf("unlock failed (%s)\n", smbcli_errstr(c->tree));
333                         correct = False;
334                 }
335                 free(fname);
336         }
337
338         smbcli_close(c->tree, fnum2);
339         smbcli_unlink(c->tree, lockfname);
340
341         printf("%d\n", i);
342
343         return correct;
344 }
345
346 static BOOL run_torture(struct smbcli_state *cli, int dummy)
347 {
348         BOOL ret;
349
350         ret = rw_torture(cli);
351         
352         if (!torture_close_connection(cli)) {
353                 ret = False;
354         }
355
356         return ret;
357 }
358
359 static BOOL rw_torture3(struct smbcli_state *c, const char *lockfname)
360 {
361         int fnum = -1;
362         uint_t i = 0;
363         char buf[131072];
364         char buf_rd[131072];
365         uint_t count;
366         uint_t countprev = 0;
367         ssize_t sent = 0;
368         BOOL correct = True;
369
370         for (i = 0; i < sizeof(buf); i += sizeof(uint32_t))
371         {
372                 SIVAL(buf, i, sys_random());
373         }
374
375         if (procnum == 0)
376         {
377                 fnum = smbcli_open(c->tree, lockfname, O_RDWR | O_CREAT | O_EXCL, 
378                                 DENY_NONE);
379                 if (fnum == -1) {
380                         printf("first open read/write of %s failed (%s)\n",
381                                         lockfname, smbcli_errstr(c->tree));
382                         return False;
383                 }
384         }
385         else
386         {
387                 for (i = 0; i < 500 && fnum == -1; i++)
388                 {
389                         fnum = smbcli_open(c->tree, lockfname, O_RDONLY, 
390                                         DENY_NONE);
391                         msleep(10);
392                 }
393                 if (fnum == -1) {
394                         printf("second open read-only of %s failed (%s)\n",
395                                         lockfname, smbcli_errstr(c->tree));
396                         return False;
397                 }
398         }
399
400         i = 0;
401         for (count = 0; count < sizeof(buf); count += sent)
402         {
403                 if (count >= countprev) {
404                         printf("%d %8d\r", i, count);
405                         fflush(stdout);
406                         i++;
407                         countprev += (sizeof(buf) / 20);
408                 }
409
410                 if (procnum == 0)
411                 {
412                         sent = ((uint_t)sys_random()%(20))+ 1;
413                         if (sent > sizeof(buf) - count)
414                         {
415                                 sent = sizeof(buf) - count;
416                         }
417
418                         if (smbcli_write(c->tree, fnum, 0, buf+count, count, (size_t)sent) != sent) {
419                                 printf("write failed (%s)\n", smbcli_errstr(c->tree));
420                                 correct = False;
421                         }
422                 }
423                 else
424                 {
425                         sent = smbcli_read(c->tree, fnum, buf_rd+count, count,
426                                         sizeof(buf)-count);
427                         if (sent < 0)
428                         {
429                                 printf("read failed offset:%d size:%d (%s)\n",
430                                                 count, sizeof(buf)-count,
431                                                 smbcli_errstr(c->tree));
432                                 correct = False;
433                                 sent = 0;
434                         }
435                         if (sent > 0)
436                         {
437                                 if (memcmp(buf_rd+count, buf+count, sent) != 0)
438                                 {
439                                         printf("read/write compare failed\n");
440                                         printf("offset: %d req %d recvd %d\n",
441                                                 count, sizeof(buf)-count, sent);
442                                         correct = False;
443                                         break;
444                                 }
445                         }
446                 }
447
448         }
449
450         if (NT_STATUS_IS_ERR(smbcli_close(c->tree, fnum))) {
451                 printf("close failed (%s)\n", smbcli_errstr(c->tree));
452                 correct = False;
453         }
454
455         return correct;
456 }
457
458 static BOOL rw_torture2(struct smbcli_state *c1, struct smbcli_state *c2)
459 {
460         const char *lockfname = "\\torture2.lck";
461         int fnum1;
462         int fnum2;
463         int i;
464         uint8_t buf[131072];
465         uint8_t buf_rd[131072];
466         BOOL correct = True;
467         ssize_t bytes_read, bytes_written;
468
469         if (smbcli_deltree(c1->tree, lockfname) == -1) {
470                 printf("unlink failed (%s)\n", smbcli_errstr(c1->tree));
471         }
472
473         fnum1 = smbcli_open(c1->tree, lockfname, O_RDWR | O_CREAT | O_EXCL, 
474                          DENY_NONE);
475         if (fnum1 == -1) {
476                 printf("first open read/write of %s failed (%s)\n",
477                                 lockfname, smbcli_errstr(c1->tree));
478                 return False;
479         }
480         fnum2 = smbcli_open(c2->tree, lockfname, O_RDONLY, 
481                          DENY_NONE);
482         if (fnum2 == -1) {
483                 printf("second open read-only of %s failed (%s)\n",
484                                 lockfname, smbcli_errstr(c2->tree));
485                 smbcli_close(c1->tree, fnum1);
486                 return False;
487         }
488
489         printf("Checking data integrity over %d ops\n", torture_numops);
490
491         for (i=0;i<torture_numops;i++)
492         {
493                 size_t buf_size = ((uint_t)sys_random()%(sizeof(buf)-1))+ 1;
494                 if (i % 10 == 0) {
495                         printf("%d\r", i); fflush(stdout);
496                 }
497
498                 generate_random_buffer(buf, buf_size);
499
500                 if ((bytes_written = smbcli_write(c1->tree, fnum1, 0, buf, 0, buf_size)) != buf_size) {
501                         printf("write failed (%s)\n", smbcli_errstr(c1->tree));
502                         printf("wrote %d, expected %d\n", bytes_written, buf_size); 
503                         correct = False;
504                         break;
505                 }
506
507                 if ((bytes_read = smbcli_read(c2->tree, fnum2, buf_rd, 0, buf_size)) != buf_size) {
508                         printf("read failed (%s)\n", smbcli_errstr(c2->tree));
509                         printf("read %d, expected %d\n", bytes_read, buf_size); 
510                         correct = False;
511                         break;
512                 }
513
514                 if (memcmp(buf_rd, buf, buf_size) != 0)
515                 {
516                         printf("read/write compare failed\n");
517                         correct = False;
518                         break;
519                 }
520         }
521
522         if (NT_STATUS_IS_ERR(smbcli_close(c2->tree, fnum2))) {
523                 printf("close failed (%s)\n", smbcli_errstr(c2->tree));
524                 correct = False;
525         }
526         if (NT_STATUS_IS_ERR(smbcli_close(c1->tree, fnum1))) {
527                 printf("close failed (%s)\n", smbcli_errstr(c1->tree));
528                 correct = False;
529         }
530
531         if (NT_STATUS_IS_ERR(smbcli_unlink(c1->tree, lockfname))) {
532                 printf("unlink failed (%s)\n", smbcli_errstr(c1->tree));
533                 correct = False;
534         }
535
536         return correct;
537 }
538
539 static BOOL run_readwritetest(void)
540 {
541         struct smbcli_state *cli1, *cli2;
542         BOOL test1, test2 = True;
543
544         if (!torture_open_connection(&cli1) || !torture_open_connection(&cli2)) {
545                 return False;
546         }
547
548         printf("starting readwritetest\n");
549
550         test1 = rw_torture2(cli1, cli2);
551         printf("Passed readwritetest v1: %s\n", BOOLSTR(test1));
552
553         if (test1) {
554                 test2 = rw_torture2(cli1, cli1);
555                 printf("Passed readwritetest v2: %s\n", BOOLSTR(test2));
556         }
557
558         if (!torture_close_connection(cli1)) {
559                 test1 = False;
560         }
561
562         if (!torture_close_connection(cli2)) {
563                 test2 = False;
564         }
565
566         return (test1 && test2);
567 }
568
569 static BOOL run_readwritemulti(struct smbcli_state *cli, int dummy)
570 {
571         BOOL test;
572
573         test = rw_torture3(cli, "\\multitest.txt");
574
575         if (!torture_close_connection(cli)) {
576                 test = False;
577         }
578         
579         return test;
580 }
581
582
583 /*
584   this checks to see if a secondary tconx can use open files from an
585   earlier tconx
586  */
587 static BOOL run_tcon_test(void)
588 {
589         struct smbcli_state *cli;
590         const char *fname = "\\tcontest.tmp";
591         int fnum1;
592         uint16_t cnum1, cnum2, cnum3;
593         uint16_t vuid1, vuid2;
594         char buf[4];
595         BOOL ret = True;
596         struct smbcli_tree *tree1;
597         const char *host = lp_parm_string(-1, "torture", "host");
598         const char *share = lp_parm_string(-1, "torture", "share");
599         const char *password = lp_parm_string(-1, "torture", "password");
600
601         if (!torture_open_connection(&cli)) {
602                 return False;
603         }
604
605         printf("starting tcontest\n");
606
607         if (smbcli_deltree(cli->tree, fname) == -1) {
608                 printf("unlink of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
609         }
610
611         fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
612         if (fnum1 == -1) {
613                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
614                 return False;
615         }
616
617         cnum1 = cli->tree->tid;
618         vuid1 = cli->session->vuid;
619
620         memset(&buf, 0, 4); /* init buf so valgrind won't complain */
621         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) != 4) {
622                 printf("initial write failed (%s)\n", smbcli_errstr(cli->tree));
623                 return False;
624         }
625
626         tree1 = cli->tree;      /* save old tree connection */
627         if (NT_STATUS_IS_ERR(smbcli_send_tconX(cli, share, "?????", password))) {
628                 printf("%s refused 2nd tree connect (%s)\n", host,
629                            smbcli_errstr(cli->tree));
630                 smbcli_shutdown(cli);
631                 return False;
632         }
633
634         cnum2 = cli->tree->tid;
635         cnum3 = MAX(cnum1, cnum2) + 1; /* any invalid number */
636         vuid2 = cli->session->vuid + 1;
637
638         /* try a write with the wrong tid */
639         cli->tree->tid = cnum2;
640
641         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) == 4) {
642                 printf("* server allows write with wrong TID\n");
643                 ret = False;
644         } else {
645                 printf("server fails write with wrong TID : %s\n", smbcli_errstr(cli->tree));
646         }
647
648
649         /* try a write with an invalid tid */
650         cli->tree->tid = cnum3;
651
652         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) == 4) {
653                 printf("* server allows write with invalid TID\n");
654                 ret = False;
655         } else {
656                 printf("server fails write with invalid TID : %s\n", smbcli_errstr(cli->tree));
657         }
658
659         /* try a write with an invalid vuid */
660         cli->session->vuid = vuid2;
661         cli->tree->tid = cnum1;
662
663         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) == 4) {
664                 printf("* server allows write with invalid VUID\n");
665                 ret = False;
666         } else {
667                 printf("server fails write with invalid VUID : %s\n", smbcli_errstr(cli->tree));
668         }
669
670         cli->session->vuid = vuid1;
671         cli->tree->tid = cnum1;
672
673         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum1))) {
674                 printf("close failed (%s)\n", smbcli_errstr(cli->tree));
675                 return False;
676         }
677
678         cli->tree->tid = cnum2;
679
680         if (NT_STATUS_IS_ERR(smbcli_tdis(cli))) {
681                 printf("secondary tdis failed (%s)\n", smbcli_errstr(cli->tree));
682                 return False;
683         }
684
685         cli->tree = tree1;  /* restore initial tree */
686         cli->tree->tid = cnum1;
687
688         if (!torture_close_connection(cli)) {
689                 return False;
690         }
691
692         return ret;
693 }
694
695
696
697 static BOOL tcon_devtest(struct smbcli_state *cli,
698                          const char *myshare, const char *devtype,
699                          NTSTATUS expected_error)
700 {
701         BOOL status;
702         BOOL ret;
703         const char *password = lp_parm_string(-1, "torture", "password");
704
705         status = NT_STATUS_IS_OK(smbcli_send_tconX(cli, myshare, devtype, 
706                                                 password));
707
708         printf("Trying share %s with devtype %s\n", myshare, devtype);
709
710         if (NT_STATUS_IS_OK(expected_error)) {
711                 if (status) {
712                         ret = True;
713                 } else {
714                         printf("tconX to share %s with type %s "
715                                "should have succeeded but failed\n",
716                                myshare, devtype);
717                         ret = False;
718                 }
719                 smbcli_tdis(cli);
720         } else {
721                 if (status) {
722                         printf("tconx to share %s with type %s "
723                                "should have failed but succeeded\n",
724                                myshare, devtype);
725                         ret = False;
726                 } else {
727                         if (NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),
728                                             expected_error)) {
729                                 ret = True;
730                         } else {
731                                 printf("Returned unexpected error\n");
732                                 ret = False;
733                         }
734                 }
735         }
736         return ret;
737 }
738
739 /*
740  checks for correct tconX support
741  */
742 static BOOL run_tcon_devtype_test(void)
743 {
744         struct smbcli_state *cli1 = NULL;
745         BOOL retry;
746         int flags = 0;
747         NTSTATUS status;
748         BOOL ret = True;
749         const char *host = lp_parm_string(-1, "torture", "host");
750         const char *share = lp_parm_string(-1, "torture", "share");
751         const char *username = lp_parm_string(-1, "torture", "username");
752         const char *userdomain = lp_parm_string(-1, "torture", "userdomain");
753         const char *password = lp_parm_string(-1, "torture", "password");
754         
755         status = smbcli_full_connection(NULL,
756                                         &cli1, lp_netbios_name(),
757                                         host, NULL, 
758                                         share, "?????",
759                                         username, userdomain,
760                                         password, flags, &retry);
761
762         if (!NT_STATUS_IS_OK(status)) {
763                 printf("could not open connection\n");
764                 return False;
765         }
766
767         if (!tcon_devtest(cli1, "IPC$", "A:", NT_STATUS_BAD_DEVICE_TYPE))
768                 ret = False;
769
770         if (!tcon_devtest(cli1, "IPC$", "?????", NT_STATUS_OK))
771                 ret = False;
772
773         if (!tcon_devtest(cli1, "IPC$", "LPT:", NT_STATUS_BAD_DEVICE_TYPE))
774                 ret = False;
775
776         if (!tcon_devtest(cli1, "IPC$", "IPC", NT_STATUS_OK))
777                 ret = False;
778                         
779         if (!tcon_devtest(cli1, "IPC$", "FOOBA", NT_STATUS_BAD_DEVICE_TYPE))
780                 ret = False;
781
782         if (!tcon_devtest(cli1, share, "A:", NT_STATUS_OK))
783                 ret = False;
784
785         if (!tcon_devtest(cli1, share, "?????", NT_STATUS_OK))
786                 ret = False;
787
788         if (!tcon_devtest(cli1, share, "LPT:", NT_STATUS_BAD_DEVICE_TYPE))
789                 ret = False;
790
791         if (!tcon_devtest(cli1, share, "IPC", NT_STATUS_BAD_DEVICE_TYPE))
792                 ret = False;
793                         
794         if (!tcon_devtest(cli1, share, "FOOBA", NT_STATUS_BAD_DEVICE_TYPE))
795                 ret = False;
796
797         smbcli_shutdown(cli1);
798
799         if (ret)
800                 printf("Passed tcondevtest\n");
801
802         return ret;
803 }
804
805
806 /*
807 test whether fnums and tids open on one VC are available on another (a major
808 security hole)
809 */
810 static BOOL run_fdpasstest(void)
811 {
812         struct smbcli_state *cli1, *cli2;
813         const char *fname = "\\fdpass.tst";
814         int fnum1, oldtid;
815         pstring buf;
816
817         if (!torture_open_connection(&cli1) || !torture_open_connection(&cli2)) {
818                 return False;
819         }
820
821         printf("starting fdpasstest\n");
822
823         smbcli_unlink(cli1->tree, fname);
824
825         printf("Opening a file on connection 1\n");
826
827         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
828         if (fnum1 == -1) {
829                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
830                 return False;
831         }
832
833         printf("writing to file on connection 1\n");
834
835         if (smbcli_write(cli1->tree, fnum1, 0, "hello world\n", 0, 13) != 13) {
836                 printf("write failed (%s)\n", smbcli_errstr(cli1->tree));
837                 return False;
838         }
839
840         oldtid = cli2->tree->tid;
841         cli2->session->vuid = cli1->session->vuid;
842         cli2->tree->tid = cli1->tree->tid;
843         cli2->session->pid = cli1->session->pid;
844
845         printf("reading from file on connection 2\n");
846
847         if (smbcli_read(cli2->tree, fnum1, buf, 0, 13) == 13) {
848                 printf("read succeeded! nasty security hole [%s]\n",
849                        buf);
850                 return False;
851         }
852
853         smbcli_close(cli1->tree, fnum1);
854         smbcli_unlink(cli1->tree, fname);
855
856         cli2->tree->tid = oldtid;
857
858         torture_close_connection(cli1);
859         torture_close_connection(cli2);
860
861         printf("finished fdpasstest\n");
862         return True;
863 }
864
865
866 /*
867 test the timing of deferred open requests
868 */
869 static BOOL run_deferopen(struct smbcli_state *cli, int dummy)
870 {
871         const char *fname = "\\defer_open_test.dat";
872         int retries=4;
873         int i = 0;
874         BOOL correct = True;
875
876         if (retries <= 0) {
877                 printf("failed to connect\n");
878                 return False;
879         }
880
881         printf("Testing deferred open requests.\n");
882
883         while (i < 4) {
884                 int fnum = -1;
885
886                 do {
887                         struct timeval tv;
888                         tv = timeval_current();
889                         fnum = smbcli_nt_create_full(cli->tree, fname, 0, GENERIC_RIGHTS_FILE_ALL_ACCESS,
890                                 FILE_ATTRIBUTE_NORMAL, NTCREATEX_SHARE_ACCESS_NONE,
891                                 NTCREATEX_DISP_OPEN_IF, 0, 0);
892                         if (fnum != -1) {
893                                 break;
894                         }
895                         if (NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),NT_STATUS_SHARING_VIOLATION)) {
896                                 double e = timeval_elapsed(&tv);
897                                 if (e < 0.5 || e > 1.5) {
898                                         fprintf(stderr,"Timing incorrect %.2f violation\n",
899                                                 e);
900                                 }
901                         }
902                 } while (NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),NT_STATUS_SHARING_VIOLATION));
903
904                 if (fnum == -1) {
905                         fprintf(stderr,"Failed to open %s, error=%s\n", fname, smbcli_errstr(cli->tree));
906                         return False;
907                 }
908
909                 printf("pid %u open %d\n", getpid(), i);
910
911                 sleep(10);
912                 i++;
913                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
914                         fprintf(stderr,"Failed to close %s, error=%s\n", fname, smbcli_errstr(cli->tree));
915                         return False;
916                 }
917                 sleep(2);
918         }
919
920         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
921                 /* All until the last unlink will fail with sharing violation. */
922                 if (!NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),NT_STATUS_SHARING_VIOLATION)) {
923                         printf("unlink of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
924                         correct = False;
925                 }
926         }
927
928         printf("deferred test finished\n");
929         if (!torture_close_connection(cli)) {
930                 correct = False;
931         }
932         return correct;
933 }
934
935 /*
936 test how many open files this server supports on the one socket
937 */
938 static BOOL run_maxfidtest(struct smbcli_state *cli, int dummy)
939 {
940 #define MAXFID_TEMPLATE "\\maxfid\\fid%d\\maxfid.%d.%d"
941         char *fname;
942         int fnums[0x11000], i;
943         int retries=4, maxfid;
944         BOOL correct = True;
945
946         if (retries <= 0) {
947                 printf("failed to connect\n");
948                 return False;
949         }
950
951         if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
952                 printf("Failed to deltree \\maxfid - %s\n",
953                        smbcli_errstr(cli->tree));
954                 return False;
955         }
956         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\maxfid"))) {
957                 printf("Failed to mkdir \\maxfid, error=%s\n", 
958                        smbcli_errstr(cli->tree));
959                 return False;
960         }
961
962         printf("Testing maximum number of open files\n");
963
964         for (i=0; i<0x11000; i++) {
965                 if (i % 1000 == 0) {
966                         asprintf(&fname, "\\maxfid\\fid%d", i/1000);
967                         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, fname))) {
968                                 printf("Failed to mkdir %s, error=%s\n", 
969                                        fname, smbcli_errstr(cli->tree));
970                                 return False;
971                         }
972                         free(fname);
973                 }
974                 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
975                 if ((fnums[i] = smbcli_open(cli->tree, fname, 
976                                         O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) ==
977                     -1) {
978                         printf("open of %s failed (%s)\n", 
979                                fname, smbcli_errstr(cli->tree));
980                         printf("maximum fnum is %d\n", i);
981                         break;
982                 }
983                 free(fname);
984                 printf("%6d\r", i);
985         }
986         printf("%6d\n", i);
987         i--;
988
989         maxfid = i;
990
991         printf("cleaning up\n");
992         for (i=0;i<maxfid/2;i++) {
993                 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
994                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[i]))) {
995                         printf("Close of fnum %d failed - %s\n", fnums[i], smbcli_errstr(cli->tree));
996                 }
997                 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
998                         printf("unlink of %s failed (%s)\n", 
999                                fname, smbcli_errstr(cli->tree));
1000                         correct = False;
1001                 }
1002                 free(fname);
1003
1004                 asprintf(&fname, MAXFID_TEMPLATE, (maxfid-i)/1000, maxfid-i,(int)getpid());
1005                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[maxfid-i]))) {
1006                         printf("Close of fnum %d failed - %s\n", fnums[maxfid-i], smbcli_errstr(cli->tree));
1007                 }
1008                 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
1009                         printf("unlink of %s failed (%s)\n", 
1010                                fname, smbcli_errstr(cli->tree));
1011                         correct = False;
1012                 }
1013                 free(fname);
1014
1015                 printf("%6d %6d\r", i, maxfid-i);
1016         }
1017         printf("%6d\n", 0);
1018
1019         if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
1020                 printf("Failed to deltree \\maxfid - %s\n",
1021                        smbcli_errstr(cli->tree));
1022                 return False;
1023         }
1024
1025         printf("maxfid test finished\n");
1026         if (!torture_close_connection(cli)) {
1027                 correct = False;
1028         }
1029         return correct;
1030 #undef MAXFID_TEMPLATE
1031 }
1032
1033 /* send smb negprot commands, not reading the response */
1034 static BOOL run_negprot_nowait(void)
1035 {
1036         int i;
1037         struct smbcli_state *cli, *cli2;
1038         BOOL correct = True;
1039
1040         printf("starting negprot nowait test\n");
1041
1042         cli = open_nbt_connection();
1043         if (!cli) {
1044                 return False;
1045         }
1046
1047         printf("Filling send buffer\n");
1048
1049         for (i=0;i<10000;i++) {
1050                 struct smbcli_request *req;
1051                 time_t t1 = time(NULL);
1052                 req = smb_negprot_send(cli->transport, PROTOCOL_NT1);
1053                 while (req->state == SMBCLI_REQUEST_SEND && time(NULL) < t1+5) {
1054                         smbcli_transport_process(cli->transport);
1055                 }
1056                 if (req->state == SMBCLI_REQUEST_ERROR) {
1057                         printf("Failed to fill pipe - %s\n", nt_errstr(req->status));
1058                         torture_close_connection(cli);
1059                         return correct;
1060                 }
1061                 if (req->state == SMBCLI_REQUEST_SEND) {
1062                         break;
1063                 }
1064         }
1065
1066         if (i == 10000) {
1067                 printf("send buffer failed to fill\n");
1068                 if (!torture_close_connection(cli)) {
1069                         correct = False;
1070                 }
1071                 return correct;
1072         }
1073
1074         printf("send buffer filled after %d requests\n", i);
1075
1076         printf("Opening secondary connection\n");
1077         if (!torture_open_connection(&cli2)) {
1078                 return False;
1079         }
1080
1081         if (!torture_close_connection(cli)) {
1082                 correct = False;
1083         }
1084
1085         if (!torture_close_connection(cli2)) {
1086                 correct = False;
1087         }
1088
1089         printf("finished negprot nowait test\n");
1090
1091         return correct;
1092 }
1093
1094
1095 /*
1096   This checks how the getatr calls works
1097 */
1098 static BOOL run_attrtest(void)
1099 {
1100         struct smbcli_state *cli;
1101         int fnum;
1102         time_t t, t2;
1103         const char *fname = "\\attrib123456789.tst";
1104         BOOL correct = True;
1105
1106         printf("starting attrib test\n");
1107
1108         if (!torture_open_connection(&cli)) {
1109                 return False;
1110         }
1111
1112         smbcli_unlink(cli->tree, fname);
1113         fnum = smbcli_open(cli->tree, fname, 
1114                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1115         smbcli_close(cli->tree, fnum);
1116
1117         if (NT_STATUS_IS_ERR(smbcli_getatr(cli->tree, fname, NULL, NULL, &t))) {
1118                 printf("getatr failed (%s)\n", smbcli_errstr(cli->tree));
1119                 correct = False;
1120         }
1121
1122         printf("New file time is %s", ctime(&t));
1123
1124         if (abs(t - time(NULL)) > 60*60*24*10) {
1125                 printf("ERROR: SMBgetatr bug. time is %s",
1126                        ctime(&t));
1127                 t = time(NULL);
1128                 correct = False;
1129         }
1130
1131         t2 = t-60*60*24; /* 1 day ago */
1132
1133         printf("Setting file time to %s", ctime(&t2));
1134
1135         if (NT_STATUS_IS_ERR(smbcli_setatr(cli->tree, fname, 0, t2))) {
1136                 printf("setatr failed (%s)\n", smbcli_errstr(cli->tree));
1137                 correct = True;
1138         }
1139
1140         if (NT_STATUS_IS_ERR(smbcli_getatr(cli->tree, fname, NULL, NULL, &t))) {
1141                 printf("getatr failed (%s)\n", smbcli_errstr(cli->tree));
1142                 correct = True;
1143         }
1144
1145         printf("Retrieved file time as %s", ctime(&t));
1146
1147         if (t != t2) {
1148                 printf("ERROR: getatr/setatr bug. times are\n%s",
1149                        ctime(&t));
1150                 printf("%s", ctime(&t2));
1151                 correct = True;
1152         }
1153
1154         smbcli_unlink(cli->tree, fname);
1155
1156         if (!torture_close_connection(cli)) {
1157                 correct = False;
1158         }
1159
1160         printf("attrib test finished\n");
1161
1162         return correct;
1163 }
1164
1165
1166 /*
1167   This checks a couple of trans2 calls
1168 */
1169 static BOOL run_trans2test(void)
1170 {
1171         struct smbcli_state *cli;
1172         int fnum;
1173         size_t size;
1174         time_t c_time, a_time, m_time, w_time, m_time2;
1175         const char *fname = "\\trans2.tst";
1176         const char *dname = "\\trans2";
1177         const char *fname2 = "\\trans2\\trans2.tst";
1178         const char *pname;
1179         BOOL correct = True;
1180
1181         printf("starting trans2 test\n");
1182
1183         if (!torture_open_connection(&cli)) {
1184                 return False;
1185         }
1186
1187         smbcli_unlink(cli->tree, fname);
1188
1189         printf("Testing qfileinfo\n");
1190         
1191         fnum = smbcli_open(cli->tree, fname, 
1192                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1193         if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, NULL, &size, &c_time, &a_time, &m_time,
1194                            NULL, NULL))) {
1195                 printf("ERROR: qfileinfo failed (%s)\n", smbcli_errstr(cli->tree));
1196                 correct = False;
1197         }
1198
1199         printf("Testing NAME_INFO\n");
1200
1201         if (NT_STATUS_IS_ERR(smbcli_qfilename(cli->tree, fnum, &pname))) {
1202                 printf("ERROR: qfilename failed (%s)\n", smbcli_errstr(cli->tree));
1203                 correct = False;
1204         }
1205
1206         if (!pname || strcmp(pname, fname)) {
1207                 printf("qfilename gave different name? [%s] [%s]\n",
1208                        fname, pname);
1209                 correct = False;
1210         }
1211
1212         smbcli_close(cli->tree, fnum);
1213         smbcli_unlink(cli->tree, fname);
1214
1215         fnum = smbcli_open(cli->tree, fname, 
1216                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1217         if (fnum == -1) {
1218                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
1219                 return False;
1220         }
1221         smbcli_close(cli->tree, fnum);
1222
1223         printf("Checking for sticky create times\n");
1224
1225         if (NT_STATUS_IS_ERR(smbcli_qpathinfo(cli->tree, fname, &c_time, &a_time, &m_time, &size, NULL))) {
1226                 printf("ERROR: qpathinfo failed (%s)\n", smbcli_errstr(cli->tree));
1227                 correct = False;
1228         } else {
1229                 if (c_time != m_time) {
1230                         printf("create time=%s", ctime(&c_time));
1231                         printf("modify time=%s", ctime(&m_time));
1232                         printf("This system appears to have sticky create times\n");
1233                 }
1234                 if (a_time % (60*60) == 0) {
1235                         printf("access time=%s", ctime(&a_time));
1236                         printf("This system appears to set a midnight access time\n");
1237                         correct = False;
1238                 }
1239
1240                 if (abs(m_time - time(NULL)) > 60*60*24*7) {
1241                         printf("ERROR: totally incorrect times - maybe word reversed? mtime=%s", ctime(&m_time));
1242                         correct = False;
1243                 }
1244         }
1245
1246
1247         smbcli_unlink(cli->tree, fname);
1248         fnum = smbcli_open(cli->tree, fname, 
1249                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1250         smbcli_close(cli->tree, fnum);
1251         if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, fname, &c_time, &a_time, &m_time, &w_time, &size, NULL, NULL))) {
1252                 printf("ERROR: qpathinfo2 failed (%s)\n", smbcli_errstr(cli->tree));
1253                 correct = False;
1254         } else {
1255                 if (w_time < 60*60*24*2) {
1256                         printf("write time=%s", ctime(&w_time));
1257                         printf("This system appears to set a initial 0 write time\n");
1258                         correct = False;
1259                 }
1260         }
1261
1262         smbcli_unlink(cli->tree, fname);
1263
1264
1265         /* check if the server updates the directory modification time
1266            when creating a new file */
1267         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, dname))) {
1268                 printf("ERROR: mkdir failed (%s)\n", smbcli_errstr(cli->tree));
1269                 correct = False;
1270         }
1271         sleep(3);
1272         if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, "\\trans2\\", &c_time, &a_time, &m_time, &w_time, &size, NULL, NULL))) {
1273                 printf("ERROR: qpathinfo2 failed (%s)\n", smbcli_errstr(cli->tree));
1274                 correct = False;
1275         }
1276
1277         fnum = smbcli_open(cli->tree, fname2, 
1278                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1279         smbcli_write(cli->tree, fnum,  0, (char *)&fnum, 0, sizeof(fnum));
1280         smbcli_close(cli->tree, fnum);
1281         if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, "\\trans2\\", &c_time, &a_time, &m_time2, &w_time, &size, NULL, NULL))) {
1282                 printf("ERROR: qpathinfo2 failed (%s)\n", smbcli_errstr(cli->tree));
1283                 correct = False;
1284         } else {
1285                 if (m_time2 == m_time) {
1286                         printf("This system does not update directory modification times\n");
1287                         correct = False;
1288                 }
1289         }
1290         smbcli_unlink(cli->tree, fname2);
1291         smbcli_rmdir(cli->tree, dname);
1292
1293         if (!torture_close_connection(cli)) {
1294                 correct = False;
1295         }
1296
1297         printf("trans2 test finished\n");
1298
1299         return correct;
1300 }
1301
1302
1303 /*
1304   print out server properties
1305  */
1306 static BOOL run_properties(void)
1307 {
1308         struct smbcli_state *cli;
1309         BOOL correct = True;
1310         
1311         printf("starting properties test\n");
1312         
1313         ZERO_STRUCT(cli);
1314
1315         if (!torture_open_connection(&cli)) {
1316                 return False;
1317         }
1318         
1319         d_printf("Capabilities 0x%08x\n", cli->transport->negotiate.capabilities);
1320
1321         if (!torture_close_connection(cli)) {
1322                 correct = False;
1323         }
1324
1325         return correct;
1326 }
1327
1328
1329
1330 /* FIRST_DESIRED_ACCESS   0xf019f */
1331 #define FIRST_DESIRED_ACCESS   SA_RIGHT_FILE_READ_DATA|SA_RIGHT_FILE_WRITE_DATA|SA_RIGHT_FILE_APPEND_DATA|\
1332                                SA_RIGHT_FILE_READ_EA|                           /* 0xf */ \
1333                                SA_RIGHT_FILE_WRITE_EA|SA_RIGHT_FILE_READ_ATTRIBUTES|     /* 0x90 */ \
1334                                SA_RIGHT_FILE_WRITE_ATTRIBUTES|                  /* 0x100 */ \
1335                                STD_RIGHT_DELETE_ACCESS|STD_RIGHT_READ_CONTROL_ACCESS|\
1336                                STD_RIGHT_WRITE_DAC_ACCESS|STD_RIGHT_WRITE_OWNER_ACCESS     /* 0xf0000 */
1337 /* SECOND_DESIRED_ACCESS  0xe0080 */
1338 #define SECOND_DESIRED_ACCESS  SA_RIGHT_FILE_READ_ATTRIBUTES|                   /* 0x80 */ \
1339                                STD_RIGHT_READ_CONTROL_ACCESS|STD_RIGHT_WRITE_DAC_ACCESS|\
1340                                STD_RIGHT_WRITE_OWNER_ACCESS                      /* 0xe0000 */
1341
1342 #if 0
1343 #define THIRD_DESIRED_ACCESS   FILE_READ_ATTRIBUTES|                   /* 0x80 */ \
1344                                READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
1345                                SA_RIGHT_FILE_READ_DATA|\
1346                                WRITE_OWNER_ACCESS                      /* */
1347 #endif
1348
1349 /*
1350   Test ntcreate calls made by xcopy
1351  */
1352 static BOOL run_xcopy(void)
1353 {
1354         struct smbcli_state *cli1;
1355         const char *fname = "\\test.txt";
1356         BOOL correct = True;
1357         int fnum1, fnum2;
1358
1359         printf("starting xcopy test\n");
1360         
1361         if (!torture_open_connection(&cli1)) {
1362                 return False;
1363         }
1364         
1365         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0,
1366                                    FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
1367                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 
1368                                    0x4044, 0);
1369
1370         if (fnum1 == -1) {
1371                 printf("First open failed - %s\n", smbcli_errstr(cli1->tree));
1372                 return False;
1373         }
1374
1375         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0,
1376                                    SECOND_DESIRED_ACCESS, 0,
1377                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN, 
1378                                    0x200000, 0);
1379         if (fnum2 == -1) {
1380                 printf("second open failed - %s\n", smbcli_errstr(cli1->tree));
1381                 return False;
1382         }
1383         
1384         if (!torture_close_connection(cli1)) {
1385                 correct = False;
1386         }
1387         
1388         return correct;
1389 }
1390
1391
1392 /*
1393   see how many RPC pipes we can open at once
1394 */
1395 static BOOL run_pipe_number(void)
1396 {
1397         struct smbcli_state *cli1;
1398         const char *pipe_name = "\\WKSSVC";
1399         int fnum;
1400         int num_pipes = 0;
1401
1402         printf("starting pipenumber test\n");
1403         if (!torture_open_connection(&cli1)) {
1404                 return False;
1405         }
1406
1407         while(1) {
1408                 fnum = smbcli_nt_create_full(cli1->tree, pipe_name, 0, SA_RIGHT_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
1409                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1410
1411                 if (fnum == -1) {
1412                         printf("Open of pipe %s failed with error (%s)\n", pipe_name, smbcli_errstr(cli1->tree));
1413                         break;
1414                 }
1415                 num_pipes++;
1416                 printf("%d\r", num_pipes);
1417                 fflush(stdout);
1418         }
1419
1420         printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
1421         torture_close_connection(cli1);
1422         return True;
1423 }
1424
1425
1426
1427
1428 /*
1429   open N connections to the server and just hold them open
1430   used for testing performance when there are N idle users
1431   already connected
1432  */
1433  static BOOL torture_holdcon(void)
1434 {
1435         int i;
1436         struct smbcli_state **cli;
1437         int num_dead = 0;
1438
1439         printf("Opening %d connections\n", torture_numops);
1440         
1441         cli = malloc(sizeof(struct smbcli_state *) * torture_numops);
1442
1443         for (i=0;i<torture_numops;i++) {
1444                 if (!torture_open_connection(&cli[i])) {
1445                         return False;
1446                 }
1447                 printf("opened %d connections\r", i);
1448                 fflush(stdout);
1449         }
1450
1451         printf("\nStarting pings\n");
1452
1453         while (1) {
1454                 for (i=0;i<torture_numops;i++) {
1455                         NTSTATUS status;
1456                         if (cli[i]) {
1457                                 status = smbcli_chkpath(cli[i]->tree, "\\");
1458                                 if (!NT_STATUS_IS_OK(status)) {
1459                                         printf("Connection %d is dead\n", i);
1460                                         cli[i] = NULL;
1461                                         num_dead++;
1462                                 }
1463                                 usleep(100);
1464                         }
1465                 }
1466
1467                 if (num_dead == torture_numops) {
1468                         printf("All connections dead - finishing\n");
1469                         break;
1470                 }
1471
1472                 printf(".");
1473                 fflush(stdout);
1474         }
1475
1476         return True;
1477 }
1478
1479 /*
1480   Try with a wrong vuid and check error message.
1481  */
1482
1483 static BOOL run_vuidtest(void)
1484 {
1485         struct smbcli_state *cli;
1486         const char *fname = "\\vuid.tst";
1487         int fnum;
1488         size_t size;
1489         time_t c_time, a_time, m_time;
1490         BOOL correct = True;
1491
1492         uint16_t orig_vuid;
1493         NTSTATUS result;
1494
1495         printf("starting vuid test\n");
1496
1497         if (!torture_open_connection(&cli)) {
1498                 return False;
1499         }
1500
1501         smbcli_unlink(cli->tree, fname);
1502
1503         fnum = smbcli_open(cli->tree, fname, 
1504                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1505
1506         orig_vuid = cli->session->vuid;
1507
1508         cli->session->vuid += 1234;
1509
1510         printf("Testing qfileinfo with wrong vuid\n");
1511         
1512         if (NT_STATUS_IS_OK(result = smbcli_qfileinfo(cli->tree, fnum, NULL,
1513                                                    &size, &c_time, &a_time,
1514                                                    &m_time, NULL, NULL))) {
1515                 printf("ERROR: qfileinfo passed with wrong vuid\n");
1516                 correct = False;
1517         }
1518
1519         if ( (cli->transport->error.etype != ETYPE_DOS) ||
1520              (cli->transport->error.e.dos.eclass != ERRSRV) ||
1521              (cli->transport->error.e.dos.ecode != ERRbaduid) ) {
1522                 printf("ERROR: qfileinfo should have returned DOS error "
1523                        "ERRSRV:ERRbaduid\n  but returned %s\n",
1524                        smbcli_errstr(cli->tree));
1525                 correct = False;
1526         }
1527
1528         cli->session->vuid -= 1234;
1529
1530         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
1531                 printf("close failed (%s)\n", smbcli_errstr(cli->tree));
1532                 correct = False;
1533         }
1534
1535         smbcli_unlink(cli->tree, fname);
1536
1537         if (!torture_close_connection(cli)) {
1538                 correct = False;
1539         }
1540
1541         printf("vuid test finished\n");
1542
1543         return correct;
1544 }
1545
1546 /*
1547   Test open mode returns on read-only files.
1548  */
1549  static BOOL run_opentest(void)
1550 {
1551         static struct smbcli_state *cli1;
1552         static struct smbcli_state *cli2;
1553         const char *fname = "\\readonly.file";
1554         int fnum1, fnum2;
1555         char buf[20];
1556         size_t fsize;
1557         BOOL correct = True;
1558         char *tmp_path;
1559         int failures = 0;
1560
1561         printf("starting open test\n");
1562         
1563         if (!torture_open_connection(&cli1)) {
1564                 return False;
1565         }
1566         
1567         smbcli_setatr(cli1->tree, fname, 0, 0);
1568         smbcli_unlink(cli1->tree, fname);
1569         
1570         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1571         if (fnum1 == -1) {
1572                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1573                 return False;
1574         }
1575
1576         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1577                 printf("close2 failed (%s)\n", smbcli_errstr(cli1->tree));
1578                 return False;
1579         }
1580         
1581         if (NT_STATUS_IS_ERR(smbcli_setatr(cli1->tree, fname, FILE_ATTRIBUTE_READONLY, 0))) {
1582                 printf("smbcli_setatr failed (%s)\n", smbcli_errstr(cli1->tree));
1583                 CHECK_MAX_FAILURES(error_test1);
1584                 return False;
1585         }
1586         
1587         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_WRITE);
1588         if (fnum1 == -1) {
1589                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1590                 CHECK_MAX_FAILURES(error_test1);
1591                 return False;
1592         }
1593         
1594         /* This will fail - but the error should be ERRnoaccess, not ERRbadshare. */
1595         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_ALL);
1596         
1597         if (check_error(__location__, cli1, ERRDOS, ERRnoaccess, 
1598                         NT_STATUS_ACCESS_DENIED)) {
1599                 printf("correct error code ERRDOS/ERRnoaccess returned\n");
1600         }
1601         
1602         printf("finished open test 1\n");
1603 error_test1:
1604         smbcli_close(cli1->tree, fnum1);
1605         
1606         /* Now try not readonly and ensure ERRbadshare is returned. */
1607         
1608         smbcli_setatr(cli1->tree, fname, 0, 0);
1609         
1610         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_WRITE);
1611         if (fnum1 == -1) {
1612                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1613                 return False;
1614         }
1615         
1616         /* This will fail - but the error should be ERRshare. */
1617         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_ALL);
1618         
1619         if (check_error(__location__, cli1, ERRDOS, ERRbadshare, 
1620                         NT_STATUS_SHARING_VIOLATION)) {
1621                 printf("correct error code ERRDOS/ERRbadshare returned\n");
1622         }
1623         
1624         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1625                 printf("close2 failed (%s)\n", smbcli_errstr(cli1->tree));
1626                 return False;
1627         }
1628         
1629         smbcli_unlink(cli1->tree, fname);
1630         
1631         printf("finished open test 2\n");
1632         
1633         /* Test truncate open disposition on file opened for read. */
1634         
1635         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1636         if (fnum1 == -1) {
1637                 printf("(3) open (1) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1638                 return False;
1639         }
1640         
1641         /* write 20 bytes. */
1642         
1643         memset(buf, '\0', 20);
1644
1645         if (smbcli_write(cli1->tree, fnum1, 0, buf, 0, 20) != 20) {
1646                 printf("write failed (%s)\n", smbcli_errstr(cli1->tree));
1647                 correct = False;
1648         }
1649
1650         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1651                 printf("(3) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
1652                 return False;
1653         }
1654         
1655         /* Ensure size == 20. */
1656         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1657                 printf("(3) getatr failed (%s)\n", smbcli_errstr(cli1->tree));
1658                 CHECK_MAX_FAILURES(error_test3);
1659                 return False;
1660         }
1661         
1662         if (fsize != 20) {
1663                 printf("(3) file size != 20\n");
1664                 CHECK_MAX_FAILURES(error_test3);
1665                 return False;
1666         }
1667
1668         /* Now test if we can truncate a file opened for readonly. */
1669         
1670         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY|O_TRUNC, DENY_NONE);
1671         if (fnum1 == -1) {
1672                 printf("(3) open (2) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1673                 CHECK_MAX_FAILURES(error_test3);
1674                 return False;
1675         }
1676         
1677         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1678                 printf("close2 failed (%s)\n", smbcli_errstr(cli1->tree));
1679                 return False;
1680         }
1681
1682         /* Ensure size == 0. */
1683         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1684                 printf("(3) getatr failed (%s)\n", smbcli_errstr(cli1->tree));
1685                 CHECK_MAX_FAILURES(error_test3);
1686                 return False;
1687         }
1688
1689         if (fsize != 0) {
1690                 printf("(3) file size != 0\n");
1691                 CHECK_MAX_FAILURES(error_test3);
1692                 return False;
1693         }
1694         printf("finished open test 3\n");
1695 error_test3:    
1696         smbcli_unlink(cli1->tree, fname);
1697
1698
1699         printf("testing ctemp\n");
1700         fnum1 = smbcli_ctemp(cli1->tree, "\\", &tmp_path);
1701         if (fnum1 == -1) {
1702                 printf("ctemp failed (%s)\n", smbcli_errstr(cli1->tree));
1703                 CHECK_MAX_FAILURES(error_test4);
1704                 return False;
1705         }
1706         printf("ctemp gave path %s\n", tmp_path);
1707         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1708                 printf("close of temp failed (%s)\n", smbcli_errstr(cli1->tree));
1709         }
1710         if (NT_STATUS_IS_ERR(smbcli_unlink(cli1->tree, tmp_path))) {
1711                 printf("unlink of temp failed (%s)\n", smbcli_errstr(cli1->tree));
1712         }
1713 error_test4:    
1714         /* Test the non-io opens... */
1715
1716         if (!torture_open_connection(&cli2)) {
1717                 return False;
1718         }
1719         
1720         smbcli_setatr(cli2->tree, fname, 0, 0);
1721         smbcli_unlink(cli2->tree, fname);
1722         
1723         printf("TEST #1 testing 2 non-io opens (no delete)\n");
1724         
1725         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1726                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1727
1728         if (fnum1 == -1) {
1729                 printf("test 1 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1730                 CHECK_MAX_FAILURES(error_test10);
1731                 return False;
1732         }
1733
1734         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1735                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1736         if (fnum2 == -1) {
1737                 printf("test 1 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1738                 CHECK_MAX_FAILURES(error_test10);
1739                 return False;
1740         }
1741
1742         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1743                 printf("test 1 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1744                 return False;
1745         }
1746         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1747                 printf("test 1 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1748                 return False;
1749         }
1750
1751         printf("non-io open test #1 passed.\n");
1752 error_test10:
1753         smbcli_unlink(cli1->tree, fname);
1754
1755         printf("TEST #2 testing 2 non-io opens (first with delete)\n");
1756         
1757         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1758                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1759
1760         if (fnum1 == -1) {
1761                 printf("test 2 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1762                 CHECK_MAX_FAILURES(error_test20);
1763                 return False;
1764         }
1765
1766         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1767                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1768
1769         if (fnum2 == -1) {
1770                 printf("test 2 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1771                 CHECK_MAX_FAILURES(error_test20);
1772                 return False;
1773         }
1774
1775         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1776                 printf("test 1 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1777                 return False;
1778         }
1779         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1780                 printf("test 1 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1781                 return False;
1782         }
1783
1784         printf("non-io open test #2 passed.\n");
1785 error_test20:
1786         smbcli_unlink(cli1->tree, fname);
1787
1788         printf("TEST #3 testing 2 non-io opens (second with delete)\n");
1789         
1790         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1791                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1792
1793         if (fnum1 == -1) {
1794                 printf("test 3 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1795                 CHECK_MAX_FAILURES(error_test30);
1796                 return False;
1797         }
1798
1799         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1800                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1801
1802         if (fnum2 == -1) {
1803                 printf("test 3 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1804                 CHECK_MAX_FAILURES(error_test30);
1805                 return False;
1806         }
1807
1808         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1809                 printf("test 3 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1810                 return False;
1811         }
1812         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1813                 printf("test 3 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1814                 return False;
1815         }
1816
1817         printf("non-io open test #3 passed.\n");
1818 error_test30:
1819         smbcli_unlink(cli1->tree, fname);
1820
1821         printf("TEST #4 testing 2 non-io opens (both with delete)\n");
1822         
1823         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1824                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1825
1826         if (fnum1 == -1) {
1827                 printf("test 4 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1828                 CHECK_MAX_FAILURES(error_test40);
1829                 return False;
1830         }
1831
1832         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1833                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1834
1835         if (fnum2 != -1) {
1836                 printf("test 4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1837                 CHECK_MAX_FAILURES(error_test40);
1838                 return False;
1839         }
1840
1841         printf("test 4 open 2 of %s gave %s (correct error should be %s)\n", fname, smbcli_errstr(cli2->tree), "sharing violation");
1842
1843         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1844                 printf("test 4 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1845                 return False;
1846         }
1847
1848         printf("non-io open test #4 passed.\n");
1849 error_test40:
1850         smbcli_unlink(cli1->tree, fname);
1851
1852         printf("TEST #5 testing 2 non-io opens (both with delete - both with file share delete)\n");
1853         
1854         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1855                                    NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1856
1857         if (fnum1 == -1) {
1858                 printf("test 5 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1859                 CHECK_MAX_FAILURES(error_test50);
1860                 return False;
1861         }
1862
1863         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1864                                    NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1865
1866         if (fnum2 == -1) {
1867                 printf("test 5 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1868                 CHECK_MAX_FAILURES(error_test50);
1869                 return False;
1870         }
1871
1872         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1873                 printf("test 5 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1874                 return False;
1875         }
1876
1877         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1878                 printf("test 5 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1879                 return False;
1880         }
1881
1882         printf("non-io open test #5 passed.\n");
1883 error_test50:
1884         printf("TEST #6 testing 1 non-io open, one io open\n");
1885         
1886         smbcli_unlink(cli1->tree, fname);
1887
1888         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SA_RIGHT_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
1889                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1890
1891         if (fnum1 == -1) {
1892                 printf("test 6 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1893                 CHECK_MAX_FAILURES(error_test60);
1894                 return False;
1895         }
1896
1897         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1898                                    NTCREATEX_SHARE_ACCESS_READ, NTCREATEX_DISP_OPEN_IF, 0, 0);
1899
1900         if (fnum2 == -1) {
1901                 printf("test 6 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1902                 CHECK_MAX_FAILURES(error_test60);
1903                 return False;
1904         }
1905
1906         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1907                 printf("test 6 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1908                 return False;
1909         }
1910
1911         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1912                 printf("test 6 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1913                 return False;
1914         }
1915
1916         printf("non-io open test #6 passed.\n");
1917 error_test60:
1918         printf("TEST #7 testing 1 non-io open, one io open with delete\n");
1919
1920         smbcli_unlink(cli1->tree, fname);
1921
1922         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SA_RIGHT_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
1923                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1924
1925         if (fnum1 == -1) {
1926                 printf("test 7 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1927                 CHECK_MAX_FAILURES(error_test70);
1928                 return False;
1929         }
1930
1931         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, STD_RIGHT_DELETE_ACCESS|SA_RIGHT_FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
1932                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1933
1934         if (fnum2 != -1) {
1935                 printf("test 7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1936                 CHECK_MAX_FAILURES(error_test70);
1937                 return False;
1938         }
1939
1940         printf("test 7 open 2 of %s gave %s (correct error should be %s)\n", fname, smbcli_errstr(cli2->tree), "sharing violation");
1941
1942         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1943                 printf("test 7 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1944                 return False;
1945         }
1946
1947         printf("non-io open test #7 passed.\n");
1948
1949 error_test70:
1950
1951         printf("TEST #8 testing one normal open, followed by lock, followed by open with truncate\n");
1952
1953         smbcli_unlink(cli1->tree, fname);
1954
1955         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1956         if (fnum1 == -1) {
1957                 printf("(8) open (1) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1958                 return False;
1959         }
1960         
1961         /* write 20 bytes. */
1962         
1963         memset(buf, '\0', 20);
1964
1965         if (smbcli_write(cli1->tree, fnum1, 0, buf, 0, 20) != 20) {
1966                 printf("(8) write failed (%s)\n", smbcli_errstr(cli1->tree));
1967                 correct = False;
1968         }
1969
1970         /* Ensure size == 20. */
1971         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1972                 printf("(8) getatr (1) failed (%s)\n", smbcli_errstr(cli1->tree));
1973                 CHECK_MAX_FAILURES(error_test80);
1974                 return False;
1975         }
1976         
1977         if (fsize != 20) {
1978                 printf("(8) file size != 20\n");
1979                 CHECK_MAX_FAILURES(error_test80);
1980                 return False;
1981         }
1982
1983         /* Get an exclusive lock on the open file. */
1984         if (NT_STATUS_IS_ERR(smbcli_lock(cli1->tree, fnum1, 0, 4, 0, WRITE_LOCK))) {
1985                 printf("(8) lock1 failed (%s)\n", smbcli_errstr(cli1->tree));
1986                 CHECK_MAX_FAILURES(error_test80);
1987                 return False;
1988         }
1989
1990         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR|O_TRUNC, DENY_NONE);
1991         if (fnum1 == -1) {
1992                 printf("(8) open (2) of %s with truncate failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1993                 return False;
1994         }
1995
1996         /* Ensure size == 0. */
1997         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1998                 printf("(8) getatr (2) failed (%s)\n", smbcli_errstr(cli1->tree));
1999                 CHECK_MAX_FAILURES(error_test80);
2000                 return False;
2001         }
2002         
2003         if (fsize != 0) {
2004                 printf("(8) file size != 0\n");
2005                 CHECK_MAX_FAILURES(error_test80);
2006                 return False;
2007         }
2008
2009         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
2010                 printf("(8) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
2011                 return False;
2012         }
2013         
2014         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum2))) {
2015                 printf("(8) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
2016                 return False;
2017         }
2018         
2019 error_test80:
2020
2021         printf("open test #8 passed.\n");
2022
2023         smbcli_unlink(cli1->tree, fname);
2024
2025         if (!torture_close_connection(cli1)) {
2026                 correct = False;
2027         }
2028         if (!torture_close_connection(cli2)) {
2029                 correct = False;
2030         }
2031         
2032         return correct;
2033 }
2034
2035
2036 /*
2037   sees what IOCTLs are supported
2038  */
2039 BOOL torture_ioctl_test(void)
2040 {
2041         struct smbcli_state *cli;
2042         uint16_t device, function;
2043         int fnum;
2044         const char *fname = "\\ioctl.dat";
2045         NTSTATUS status;
2046         union smb_ioctl parms;
2047         TALLOC_CTX *mem_ctx;
2048
2049         if (!torture_open_connection(&cli)) {
2050                 return False;
2051         }
2052
2053         mem_ctx = talloc_init("ioctl_test");
2054
2055         printf("starting ioctl test\n");
2056
2057         smbcli_unlink(cli->tree, fname);
2058
2059         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2060         if (fnum == -1) {
2061                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
2062                 return False;
2063         }
2064
2065         parms.ioctl.level = RAW_IOCTL_IOCTL;
2066         parms.ioctl.in.fnum = fnum;
2067         parms.ioctl.in.request = IOCTL_QUERY_JOB_INFO;
2068         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
2069         printf("ioctl job info: %s\n", smbcli_errstr(cli->tree));
2070
2071         for (device=0;device<0x100;device++) {
2072                 printf("testing device=0x%x\n", device);
2073                 for (function=0;function<0x100;function++) {
2074                         parms.ioctl.in.request = (device << 16) | function;
2075                         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
2076
2077                         if (NT_STATUS_IS_OK(status)) {
2078                                 printf("ioctl device=0x%x function=0x%x OK : %d bytes\n", 
2079                                         device, function, parms.ioctl.out.blob.length);
2080                         }
2081                 }
2082         }
2083
2084         if (!torture_close_connection(cli)) {
2085                 return False;
2086         }
2087
2088         return True;
2089 }
2090
2091
2092 /*
2093   tries variants of chkpath
2094  */
2095 BOOL torture_chkpath_test(void)
2096 {
2097         struct smbcli_state *cli;
2098         int fnum;
2099         BOOL ret;
2100
2101         if (!torture_open_connection(&cli)) {
2102                 return False;
2103         }
2104
2105         printf("starting chkpath test\n");
2106
2107         printf("Testing valid and invalid paths\n");
2108
2109         /* cleanup from an old run */
2110         smbcli_rmdir(cli->tree, "\\chkpath.dir\\dir2");
2111         smbcli_unlink(cli->tree, "\\chkpath.dir\\*");
2112         smbcli_rmdir(cli->tree, "\\chkpath.dir");
2113
2114         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\chkpath.dir"))) {
2115                 printf("mkdir1 failed : %s\n", smbcli_errstr(cli->tree));
2116                 return False;
2117         }
2118
2119         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\chkpath.dir\\dir2"))) {
2120                 printf("mkdir2 failed : %s\n", smbcli_errstr(cli->tree));
2121                 return False;
2122         }
2123
2124         fnum = smbcli_open(cli->tree, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2125         if (fnum == -1) {
2126                 printf("open1 failed (%s)\n", smbcli_errstr(cli->tree));
2127                 return False;
2128         }
2129         smbcli_close(cli->tree, fnum);
2130
2131         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir"))) {
2132                 printf("chkpath1 failed: %s\n", smbcli_errstr(cli->tree));
2133                 ret = False;
2134         }
2135
2136         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\dir2"))) {
2137                 printf("chkpath2 failed: %s\n", smbcli_errstr(cli->tree));
2138                 ret = False;
2139         }
2140
2141         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\foo.txt"))) {
2142                 ret = check_error(__location__, cli, ERRDOS, ERRbadpath, 
2143                                   NT_STATUS_NOT_A_DIRECTORY);
2144         } else {
2145                 printf("* chkpath on a file should fail\n");
2146                 ret = False;
2147         }
2148
2149         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\bar.txt"))) {
2150                 ret = check_error(__location__, cli, ERRDOS, ERRbadfile, 
2151                                   NT_STATUS_OBJECT_NAME_NOT_FOUND);
2152         } else {
2153                 printf("* chkpath on a non existent file should fail\n");
2154                 ret = False;
2155         }
2156
2157         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\dirxx\\bar.txt"))) {
2158                 ret = check_error(__location__, cli, ERRDOS, ERRbadpath, 
2159                                   NT_STATUS_OBJECT_PATH_NOT_FOUND);
2160         } else {
2161                 printf("* chkpath on a non existent component should fail\n");
2162                 ret = False;
2163         }
2164
2165         smbcli_rmdir(cli->tree, "\\chkpath.dir\\dir2");
2166         smbcli_unlink(cli->tree, "\\chkpath.dir\\*");
2167         smbcli_rmdir(cli->tree, "\\chkpath.dir");
2168
2169         if (!torture_close_connection(cli)) {
2170                 return False;
2171         }
2172
2173         return ret;
2174 }
2175
2176
2177 /*
2178   parse a //server/share type UNC name
2179 */
2180 static BOOL parse_unc(const char *unc_name, char **hostname, char **sharename)
2181 {
2182         char *p;
2183
2184         if (strncmp(unc_name, "//", 2)) {
2185                 return False;
2186         }
2187
2188         *hostname = strdup(&unc_name[2]);
2189         p = strchr_m(&(*hostname)[2],'/');
2190         if (!p) {
2191                 return False;
2192         }
2193         *p = 0;
2194         *sharename = strdup(p+1);
2195
2196         return True;
2197 }
2198
2199
2200
2201 static void sigcont(int sig)
2202 {
2203 }
2204
2205 double torture_create_procs(BOOL (*fn)(struct smbcli_state *, int), BOOL *result)
2206 {
2207         int i, status;
2208         volatile pid_t *child_status;
2209         volatile BOOL *child_status_out;
2210         int synccount;
2211         int tries = 8;
2212         double start_time_limit = 10 + (torture_nprocs * 1.5);
2213         char **unc_list = NULL;
2214         const char *p;
2215         int num_unc_names = 0;
2216         struct timeval tv;
2217
2218         synccount = 0;
2219
2220         signal(SIGCONT, sigcont);
2221
2222         child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*torture_nprocs);
2223         if (!child_status) {
2224                 printf("Failed to setup shared memory\n");
2225                 return -1;
2226         }
2227
2228         child_status_out = (volatile BOOL *)shm_setup(sizeof(BOOL)*torture_nprocs);
2229         if (!child_status_out) {
2230                 printf("Failed to setup result status shared memory\n");
2231                 return -1;
2232         }
2233
2234         p = lp_parm_string(-1, "torture", "unclist");
2235         if (p) {
2236                 unc_list = file_lines_load(p, &num_unc_names);
2237                 if (!unc_list || num_unc_names <= 0) {
2238                         printf("Failed to load unc names list from '%s'\n", p);
2239                         exit(1);
2240                 }
2241         }
2242
2243         for (i = 0; i < torture_nprocs; i++) {
2244                 child_status[i] = 0;
2245                 child_status_out[i] = True;
2246         }
2247
2248         tv = timeval_current();
2249
2250         for (i=0;i<torture_nprocs;i++) {
2251                 procnum = i;
2252                 if (fork() == 0) {
2253                         char *myname;
2254                         char *hostname=NULL, *sharename;
2255
2256                         pid_t mypid = getpid();
2257                         sys_srandom(((int)mypid) ^ ((int)time(NULL)));
2258
2259                         asprintf(&myname, "CLIENT%d", i);
2260                         lp_set_cmdline("netbios name", myname);
2261                         free(myname);
2262
2263
2264                         if (unc_list) {
2265                                 if (!parse_unc(unc_list[i % num_unc_names],
2266                                                &hostname, &sharename)) {
2267                                         printf("Failed to parse UNC name %s\n",
2268                                                unc_list[i % num_unc_names]);
2269                                         exit(1);
2270                                 }
2271                         }
2272
2273                         while (1) {
2274                                 if (hostname) {
2275                                         if (torture_open_connection_share(&current_cli,
2276                                                                           hostname, 
2277                                                                           sharename)) {
2278                                                 break;
2279                                         }
2280                                 } else if (torture_open_connection(&current_cli)) {
2281                                                 break;
2282                                 }
2283                                 if (tries-- == 0) {
2284                                         printf("pid %d failed to start\n", (int)getpid());
2285                                         _exit(1);
2286                                 }
2287                                 msleep(100);    
2288                         }
2289
2290                         child_status[i] = getpid();
2291
2292                         pause();
2293
2294                         if (child_status[i]) {
2295                                 printf("Child %d failed to start!\n", i);
2296                                 child_status_out[i] = 1;
2297                                 _exit(1);
2298                         }
2299
2300                         child_status_out[i] = fn(current_cli, i);
2301                         _exit(0);
2302                 }
2303         }
2304
2305         do {
2306                 synccount = 0;
2307                 for (i=0;i<torture_nprocs;i++) {
2308                         if (child_status[i]) synccount++;
2309                 }
2310                 if (synccount == torture_nprocs) break;
2311                 msleep(100);
2312         } while (timeval_elapsed(&tv) < start_time_limit);
2313
2314         if (synccount != torture_nprocs) {
2315                 printf("FAILED TO START %d CLIENTS (started %d)\n", torture_nprocs, synccount);
2316                 *result = False;
2317                 return timeval_elapsed(&tv);
2318         }
2319
2320         printf("Starting %d clients\n", torture_nprocs);
2321
2322         /* start the client load */
2323         tv = timeval_current();
2324         for (i=0;i<torture_nprocs;i++) {
2325                 child_status[i] = 0;
2326         }
2327         kill(0, SIGCONT);
2328
2329         printf("%d clients started\n", torture_nprocs);
2330
2331         for (i=0;i<torture_nprocs;i++) {
2332                 int ret;
2333                 while ((ret=sys_waitpid(0, &status, 0)) == -1 && errno == EINTR) /* noop */ ;
2334                 if (ret == -1 || WEXITSTATUS(status) != 0) {
2335                         *result = False;
2336                 }
2337         }
2338
2339         printf("\n");
2340         
2341         for (i=0;i<torture_nprocs;i++) {
2342                 if (!child_status_out[i]) {
2343                         *result = False;
2344                 }
2345         }
2346         return timeval_elapsed(&tv);
2347 }
2348
2349 #define FLAG_MULTIPROC 1
2350
2351 static struct {
2352         const char *name;
2353         BOOL (*fn)(void);
2354         BOOL (*multi_fn)(struct smbcli_state *, int );
2355 } torture_ops[] = {
2356         /* base tests */
2357         {"BASE-FDPASS", run_fdpasstest, 0},
2358         {"BASE-LOCK1",  torture_locktest1,  0},
2359         {"BASE-LOCK2",  torture_locktest2,  0},
2360         {"BASE-LOCK3",  torture_locktest3,  0},
2361         {"BASE-LOCK4",  torture_locktest4,  0},
2362         {"BASE-LOCK5",  torture_locktest5,  0},
2363         {"BASE-LOCK6",  torture_locktest6,  0},
2364         {"BASE-LOCK7",  torture_locktest7,  0},
2365         {"BASE-UNLINK", torture_unlinktest, 0},
2366         {"BASE-ATTR",   run_attrtest,   0},
2367         {"BASE-TRANS2", run_trans2test, 0},
2368         {"BASE-NEGNOWAIT", run_negprot_nowait, 0},
2369         {"BASE-DIR1",  torture_dirtest1, 0},
2370         {"BASE-DIR2",  torture_dirtest2, 0},
2371         {"BASE-DENY1",  torture_denytest1, 0},
2372         {"BASE-DENY2",  torture_denytest2, 0},
2373         {"BASE-DENY3",  torture_denytest3, 0},
2374         {"BASE-DENYDOS",  torture_denydos_sharing, 0},
2375         {"BASE-NTDENY1",  NULL, torture_ntdenytest1},
2376         {"BASE-NTDENY2",  torture_ntdenytest2, 0},
2377         {"BASE-TCON",  run_tcon_test, 0},
2378         {"BASE-TCONDEV",  run_tcon_devtype_test, 0},
2379         {"BASE-VUID", run_vuidtest, 0},
2380         {"BASE-RW1",  run_readwritetest, 0},
2381         {"BASE-RW2",  NULL, run_readwritemulti},
2382         {"BASE-OPEN", run_opentest, 0},
2383         {"BASE-DEFER_OPEN", NULL, run_deferopen},
2384         {"BASE-XCOPY", run_xcopy, 0},
2385         {"BASE-RENAME", torture_test_rename, 0},
2386         {"BASE-DELETE", torture_test_delete, 0},
2387         {"BASE-PROPERTIES", run_properties, 0},
2388         {"BASE-MANGLE", torture_mangle, 0},
2389         {"BASE-OPENATTR", torture_openattrtest, 0},
2390         {"BASE-CHARSET", torture_charset, 0},
2391         {"BASE-CHKPATH",  torture_chkpath_test, 0},
2392         {"BASE-SECLEAK",  torture_sec_leak, 0},
2393         {"BASE-DISCONNECT",  torture_disconnect, 0},
2394         {"BASE-DELAYWRITE", torture_delay_write, 0},
2395
2396         /* benchmarking tests */
2397         {"BENCH-HOLDCON",  torture_holdcon, 0},
2398         {"BENCH-NBENCH",  torture_nbench, 0},
2399         {"BENCH-TORTURE", NULL, run_torture},
2400
2401         /* RAW smb tests */
2402         {"RAW-QFSINFO", torture_raw_qfsinfo, 0},
2403         {"RAW-QFILEINFO", torture_raw_qfileinfo, 0},
2404         {"RAW-SFILEINFO", torture_raw_sfileinfo, 0},
2405         {"RAW-SFILEINFO-BUG", torture_raw_sfileinfo_bug, 0},
2406         {"RAW-SEARCH", torture_raw_search, 0},
2407         {"RAW-CLOSE", torture_raw_close, 0},
2408         {"RAW-OPEN", torture_raw_open, 0},
2409         {"RAW-MKDIR", torture_raw_mkdir, 0},
2410         {"RAW-OPLOCK", torture_raw_oplock, 0},
2411         {"RAW-NOTIFY", torture_raw_notify, 0},
2412         {"RAW-MUX", torture_raw_mux, 0},
2413         {"RAW-IOCTL", torture_raw_ioctl, 0},
2414         {"RAW-CHKPATH", torture_raw_chkpath, 0},
2415         {"RAW-UNLINK", torture_raw_unlink, 0},
2416         {"RAW-READ", torture_raw_read, 0},
2417         {"RAW-WRITE", torture_raw_write, 0},
2418         {"RAW-LOCK", torture_raw_lock, 0},
2419         {"RAW-CONTEXT", torture_raw_context, 0},
2420         {"RAW-RENAME", torture_raw_rename, 0},
2421         {"RAW-SEEK", torture_raw_seek, 0},
2422         {"RAW-RAP", torture_raw_rap, 0},
2423
2424         /* protocol scanners */
2425         {"SCAN-TRANS2", torture_trans2_scan, 0},
2426         {"SCAN-NTTRANS", torture_nttrans_scan, 0},
2427         {"SCAN-ALIASES", torture_trans2_aliases, 0},
2428         {"SCAN-SMB", torture_smb_scan, 0},
2429         {"SCAN-MAXFID", NULL, run_maxfidtest},
2430         {"SCAN-UTABLE", torture_utable, 0},
2431         {"SCAN-CASETABLE", torture_casetable, 0},
2432         {"SCAN-PIPE_NUMBER", run_pipe_number, 0},
2433         {"SCAN-IOCTL",  torture_ioctl_test, 0},
2434
2435         /* rpc testers */
2436         {"RPC-LSA", torture_rpc_lsa, 0},
2437         {"RPC-ECHO", torture_rpc_echo, 0},
2438         {"RPC-DFS", torture_rpc_dfs, 0},
2439         {"RPC-SPOOLSS", torture_rpc_spoolss, 0},
2440         {"RPC-SAMR", torture_rpc_samr, 0},
2441         {"RPC-NETLOGON", torture_rpc_netlogon, 0},
2442         {"RPC-SAMLOGON", torture_rpc_samlogon, 0},
2443         {"RPC-SAMSYNC", torture_rpc_samsync, 0},
2444         {"RPC-SCHANNEL", torture_rpc_schannel, 0},
2445         {"RPC-WKSSVC", torture_rpc_wkssvc, 0},
2446         {"RPC-SRVSVC", torture_rpc_srvsvc, 0},
2447         {"RPC-SVCCTL", torture_rpc_svcctl, 0},
2448         {"RPC-ATSVC", torture_rpc_atsvc, 0},
2449         {"RPC-EVENTLOG", torture_rpc_eventlog, 0},
2450         {"RPC-EPMAPPER", torture_rpc_epmapper, 0},
2451         {"RPC-WINREG", torture_rpc_winreg, 0},
2452         {"RPC-OXIDRESOLVE", torture_rpc_oxidresolve, 0},
2453         {"RPC-REMACT", torture_rpc_remact, 0},
2454         {"RPC-MGMT", torture_rpc_mgmt, 0},
2455         {"RPC-SCANNER", torture_rpc_scanner, 0},
2456         {"RPC-AUTOIDL", torture_rpc_autoidl, 0},
2457         {"RPC-COUNTCALLS", torture_rpc_countcalls, 0},
2458         {"RPC-MULTIBIND", torture_multi_bind, 0},
2459         {"RPC-DRSUAPI", torture_rpc_drsuapi, 0},
2460         {"RPC-LOGIN", torture_rpc_login, 0},
2461
2462         /* Distributed COM testers */
2463         {"DCOM-SIMPLE", torture_dcom_simple, 0},
2464
2465         /* local (no server) testers */
2466         {"LOCAL-NTLMSSP", torture_ntlmssp_self_check, 0},
2467         {"LOCAL-ICONV", torture_local_iconv, 0},
2468         {"LOCAL-TALLOC", torture_local_talloc, 0},
2469         {"LOCAL-MESSAGING", torture_local_messaging, 0},
2470         {"LOCAL-BINDING", torture_local_binding_string, 0},
2471         {"LOCAL-IDTREE", torture_local_idtree, 0},
2472
2473         /* ldap testers */
2474         {"LDAP-BASIC", torture_ldap_basic, 0},
2475
2476         {NULL, NULL, 0}};
2477
2478
2479
2480 /****************************************************************************
2481 run a specified test or "ALL"
2482 ****************************************************************************/
2483 static BOOL run_test(const char *name)
2484 {
2485         BOOL ret = True;
2486         int i;
2487         BOOL matched = False;
2488
2489         if (strequal(name,"ALL")) {
2490                 for (i=0;torture_ops[i].name;i++) {
2491                         if (!run_test(torture_ops[i].name)) {
2492                                 ret = False;
2493                         }
2494                 }
2495                 return ret;
2496         }
2497
2498         for (i=0;torture_ops[i].name;i++) {
2499                 if (gen_fnmatch(name, torture_ops[i].name) == 0) {
2500                         double t;
2501                         matched = True;
2502                         init_iconv();
2503                         printf("Running %s\n", torture_ops[i].name);
2504                         if (torture_ops[i].multi_fn) {
2505                                 BOOL result;
2506                                 t = torture_create_procs(torture_ops[i].multi_fn, 
2507                                                          &result);
2508                                 if (!result) { 
2509                                         ret = False;
2510                                         printf("TEST %s FAILED!\n", torture_ops[i].name);
2511                                 }
2512                                          
2513                         } else {
2514                                 struct timeval tv = timeval_current();
2515                                 if (!torture_ops[i].fn()) {
2516                                         ret = False;
2517                                         printf("TEST %s FAILED!\n", torture_ops[i].name);
2518                                 }
2519                                 t = timeval_elapsed(&tv);
2520                         }
2521                         printf("%s took %g secs\n\n", torture_ops[i].name, t);
2522                 }
2523         }
2524
2525         if (!matched) {
2526                 printf("Unknown torture operation '%s'\n", name);
2527         }
2528
2529         return ret;
2530 }
2531
2532
2533 static void parse_dns(const char *dns)
2534 {
2535         char *userdn, *basedn, *secret;
2536         char *p, *d;
2537
2538         /* retrievieng the userdn */
2539         p = strchr_m(dns, '#');
2540         if (!p) {
2541                 lp_set_cmdline("torture:ldap_userdn", "");
2542                 lp_set_cmdline("torture:ldap_basedn", "");
2543                 lp_set_cmdline("torture:ldap_secret", "");
2544                 return;
2545         }
2546         userdn = strndup(dns, p - dns);
2547         lp_set_cmdline("torture:ldap_userdn", userdn);
2548
2549         /* retrieve the basedn */
2550         d = p + 1;
2551         p = strchr_m(d, '#');
2552         if (!p) {
2553                 lp_set_cmdline("torture:ldap_basedn", "");
2554                 lp_set_cmdline("torture:ldap_secret", "");
2555                 return;
2556         }
2557         basedn = strndup(d, p - d);
2558         lp_set_cmdline("torture:ldap_basedn", basedn);
2559
2560         /* retrieve the secret */
2561         p = p + 1;
2562         if (!p) {
2563                 lp_set_cmdline("torture:ldap_secret", "");
2564                 return;
2565         }
2566         secret = strdup(p);
2567         lp_set_cmdline("torture:ldap_secret", secret);
2568
2569         printf ("%s - %s - %s\n", userdn, basedn, secret);
2570
2571 }
2572
2573 static void usage(poptContext pc)
2574 {
2575         int i;
2576         int perline = 5;
2577
2578         poptPrintUsage(pc, stdout, 0);
2579         printf("\n");
2580
2581         printf("tests are:");
2582         for (i=0;torture_ops[i].name;i++) {
2583                 if ((i%perline)==0) {
2584                         printf("\n");
2585                 }
2586                 printf("%s ", torture_ops[i].name);
2587         }
2588         printf("\n\n");
2589
2590         printf("default test is ALL\n");
2591         
2592         exit(1);
2593 }
2594
2595 static BOOL is_binding_string(const char *binding_string)
2596 {
2597         TALLOC_CTX *mem_ctx = talloc_init("is_binding_string");
2598         struct dcerpc_binding binding_struct;
2599         NTSTATUS status;
2600         
2601         status = dcerpc_parse_binding(mem_ctx, binding_string, &binding_struct);
2602
2603         talloc_destroy(mem_ctx);
2604         return NT_STATUS_IS_OK(status);
2605 }
2606
2607 /****************************************************************************
2608   main program
2609 ****************************************************************************/
2610  int main(int argc,char *argv[])
2611 {
2612         int opt, i;
2613         char *p;
2614         BOOL correct = True;
2615         int argc_new;
2616         char **argv_new;
2617         poptContext pc;
2618         enum {OPT_LOADFILE=1000,OPT_UNCLIST,OPT_TIMELIMIT,OPT_DNS,OPT_DANGEROUS};
2619         struct poptOption long_options[] = {
2620                 POPT_AUTOHELP
2621                 {"smb-ports",   'p', POPT_ARG_STRING, NULL,             0,      "SMB ports",    NULL},
2622                 {"seed",          0, POPT_ARG_INT,  &torture_seed,      0,      "seed",         NULL},
2623                 {"num-progs",     0, POPT_ARG_INT,  &torture_nprocs,    0,      "num progs",    NULL},
2624                 {"num-ops",       0, POPT_ARG_INT,  &torture_numops,    0,      "num ops",      NULL},
2625                 {"entries",       0, POPT_ARG_INT,  &torture_entries,   0,      "entries",      NULL},
2626                 {"use-oplocks", 'L', POPT_ARG_NONE, &use_oplocks,       0,      "use oplocks",  NULL},
2627                 {"show-all",      0, POPT_ARG_NONE, &torture_showall,   0,      "show all",     NULL},
2628                 {"loadfile",      0, POPT_ARG_STRING,   NULL,   OPT_LOADFILE,   "loadfile",     NULL},
2629                 {"unclist",       0, POPT_ARG_STRING,   NULL,   OPT_UNCLIST,    "unclist",      NULL},
2630                 {"timelimit",   't', POPT_ARG_STRING,   NULL,   OPT_TIMELIMIT,  "timelimit",    NULL},
2631                 {"failures",    'f', POPT_ARG_INT,  &torture_failures,  0,      "failures",     NULL},
2632                 {"parse-dns",   'D', POPT_ARG_STRING,   NULL,   OPT_DNS,        "parse-dns",    NULL},
2633                 {"dangerous",   'X', POPT_ARG_NONE,     NULL,   OPT_DANGEROUS,  "dangerous",    NULL},
2634                 POPT_COMMON_SAMBA
2635                 POPT_COMMON_CONNECTION
2636                 POPT_COMMON_CREDENTIALS
2637                 POPT_COMMON_VERSION
2638                 POPT_TABLEEND
2639         };
2640
2641         setup_logging("smbtorture", DEBUG_STDOUT);
2642
2643 #ifdef HAVE_SETBUFFER
2644         setbuffer(stdout, NULL, 0);
2645 #endif
2646
2647         pc = poptGetContext("smbtorture", argc, (const char **) argv, long_options, 
2648                                 POPT_CONTEXT_KEEP_FIRST);
2649
2650         poptSetOtherOptionHelp(pc, "<binding>|<unc> TEST1 TEST2 ...");
2651
2652         while((opt = poptGetNextOpt(pc)) != -1) {
2653                 switch (opt) {
2654                 case OPT_LOADFILE:
2655                         lp_set_cmdline("torture:loadfile", poptGetOptArg(pc));
2656                         break;
2657                 case OPT_UNCLIST:
2658                         lp_set_cmdline("torture:unclist", poptGetOptArg(pc));
2659                         break;
2660                 case OPT_TIMELIMIT:
2661                         lp_set_cmdline("torture:timelimit", poptGetOptArg(pc));
2662                         break;
2663                 case OPT_DNS:
2664                         parse_dns(poptGetOptArg(pc));
2665                         break;
2666                 case OPT_DANGEROUS:
2667                         lp_set_cmdline("torture:dangerous", "1");
2668                         break;
2669                 default:
2670                         d_printf("Invalid option %s: %s\n", 
2671                                  poptBadOption(pc, 0), poptStrerror(opt));
2672                         usage(pc);
2673                         exit(1);
2674                 }
2675         }
2676
2677         lp_load(dyn_CONFIGFILE,True,False,False);
2678         load_interfaces();
2679
2680         smbtorture_init_subsystems;
2681
2682
2683         if (torture_seed == 0) {
2684                 torture_seed = time(NULL);
2685         } 
2686         printf("Using seed %d\n", torture_seed);
2687         srandom(torture_seed);
2688
2689         argv_new = discard_const_p(char *, poptGetArgs(pc));
2690
2691         argc_new = argc;
2692         for (i=0; i<argc; i++) {
2693                 if (argv_new[i] == NULL) {
2694                         argc_new = i;
2695                         break;
2696                 }
2697         }
2698
2699         if (argc_new < 3) {
2700                 usage(pc);
2701                 exit(1);
2702         }
2703
2704         for(p = argv_new[1]; *p; p++) {
2705                 if(*p == '\\')
2706                         *p = '/';
2707         }
2708
2709         /* see if its a RPC transport specifier */
2710         if (is_binding_string(argv_new[1])) {
2711                 lp_set_cmdline("torture:binding", argv_new[1]);
2712         } else {
2713                 char *binding = NULL;
2714                 char *host = NULL, *share = NULL;
2715
2716                 if (!parse_unc(argv_new[1], &host, &share)) {
2717                         d_printf("Invalid option: %s is not a valid torture target (share or binding string)\n\n", argv_new[1]);
2718                         usage(pc);
2719                 }
2720
2721                 lp_set_cmdline("torture:host", host);
2722                 lp_set_cmdline("torture:share", share);
2723                 asprintf(&binding, "ncacn_np:%s", host);
2724                 lp_set_cmdline("torture:binding", binding);
2725         }
2726
2727         if (!lp_parm_string(-1,"torture","username")) {
2728                 lp_set_cmdline("torture:username", cmdline_get_username());
2729         }
2730         if (!lp_parm_string(-1,"torture","userdomain")) {
2731                 /* 
2732                  * backward compatibility
2733                  * maybe we should remove this to make this consistent
2734                  * for all cmdline tools
2735                  * --metze
2736                  */
2737                 if (strequal(lp_netbios_name(),cmdline_get_userdomain())) {
2738                         cmdline_set_userdomain(lp_workgroup());
2739                 }
2740                 lp_set_cmdline("torture:userdomain", cmdline_get_userdomain());
2741         }
2742         if (!lp_parm_string(-1,"torture","password")) {
2743                 lp_set_cmdline("torture:password", cmdline_get_userpassword());
2744         }
2745
2746         if (argc_new == 0) {
2747                 printf("You must specify a test to run, or 'ALL'\n");
2748         } else {
2749                 for (i=2;i<argc_new;i++) {
2750                         if (!run_test(argv_new[i])) {
2751                                 correct = False;
2752                         }
2753                 }
2754         }
2755
2756         if (correct) {
2757                 return(0);
2758         } else {
2759                 return(1);
2760         }
2761 }