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