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