Remove another use of global_loadparm.
[kai/samba.git] / source4 / torture / basic / base.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Andrew Tridgell 1997-2003
5    Copyright (C) Jelmer Vernooij 2006
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "torture/smbtorture.h"
23 #include "torture/basic/proto.h"
24 #include "libcli/libcli.h"
25 #include "libcli/raw/raw_proto.h"
26 #include "torture/util.h"
27 #include "system/filesys.h"
28 #include "system/time.h"
29 #include "libcli/resolve/resolve.h"
30 #include "librpc/gen_ndr/ndr_nbt.h"
31 #include "lib/events/events.h"
32 #include "lib/cmdline/popt_common.h"
33 #include "param/param.h"
34
35
36 #define CHECK_MAX_FAILURES(label) do { if (++failures >= torture_failures) goto label; } while (0)
37
38
39 static struct smbcli_state *open_nbt_connection(struct torture_context *tctx)
40 {
41         struct nbt_name called, calling;
42         struct smbcli_state *cli;
43         const char *host = torture_setting_string(tctx, "host", NULL);
44         struct smbcli_options options;
45
46         make_nbt_name_client(&calling, lp_netbios_name(tctx->lp_ctx));
47
48         nbt_choose_called_name(NULL, &called, host, NBT_NAME_SERVER);
49
50         cli = smbcli_state_init(NULL);
51         if (!cli) {
52                 torture_comment(tctx, "Failed initialize smbcli_struct to connect with %s\n", host);
53                 goto failed;
54         }
55
56         lp_smbcli_options(tctx->lp_ctx, &options);
57
58         if (!smbcli_socket_connect(cli, host, lp_smb_ports(tctx->lp_ctx), tctx->ev,
59                                    lp_resolve_context(tctx->lp_ctx), &options, 
60                                    lp_iconv_convenience(tctx->lp_ctx),
61                    lp_socket_options(tctx->lp_ctx))) {
62                 torture_comment(tctx, "Failed to connect with %s\n", host);
63                 goto failed;
64         }
65
66         if (!smbcli_transport_establish(cli, &calling, &called)) {
67                 torture_comment(tctx, "%s rejected the session\n",host);
68                 goto failed;
69         }
70
71         return cli;
72
73 failed:
74         talloc_free(cli);
75         return NULL;
76 }
77
78 static bool tcon_devtest(struct torture_context *tctx, 
79                                                  struct smbcli_state *cli,
80                                                  const char *myshare, const char *devtype,
81                                                  NTSTATUS expected_error)
82 {
83         bool status;
84         const char *password = torture_setting_string(tctx, "password", NULL);
85
86         status = NT_STATUS_IS_OK(smbcli_tconX(cli, myshare, devtype, 
87                                                 password));
88
89         torture_comment(tctx, "Trying share %s with devtype %s\n", myshare, devtype);
90
91         if (NT_STATUS_IS_OK(expected_error)) {
92                 if (!status) {
93                         torture_fail(tctx, talloc_asprintf(tctx, 
94                                    "tconX to share %s with type %s "
95                                "should have succeeded but failed",
96                                myshare, devtype));
97                 }
98                 smbcli_tdis(cli);
99         } else {
100                 if (status) {
101                         torture_fail(tctx, talloc_asprintf(tctx, 
102                                    "tconx to share %s with type %s "
103                                "should have failed but succeeded",
104                                myshare, devtype));
105                 } else {
106                         if (NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),
107                                             expected_error)) {
108                         } else {
109                                 torture_fail(tctx, "Returned unexpected error");
110                         }
111                 }
112         }
113         return true;
114 }
115
116
117
118 /**
119 test whether fnums and tids open on one VC are available on another (a major
120 security hole)
121 */
122 static bool run_fdpasstest(struct torture_context *tctx,
123                                                    struct smbcli_state *cli1, 
124                                                    struct smbcli_state *cli2)
125 {
126         const char *fname = "\\fdpass.tst";
127         int fnum1, oldtid;
128         uint8_t buf[1024];
129
130         smbcli_unlink(cli1->tree, fname);
131
132         torture_comment(tctx, "Opening a file on connection 1\n");
133
134         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
135         torture_assert(tctx, fnum1 != -1, 
136                         talloc_asprintf(tctx, 
137                         "open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree)));
138
139         torture_comment(tctx, "writing to file on connection 1\n");
140
141         torture_assert(tctx, 
142                 smbcli_write(cli1->tree, fnum1, 0, "hello world\n", 0, 13) == 13,
143                 talloc_asprintf(tctx, 
144                 "write failed (%s)\n", smbcli_errstr(cli1->tree)));
145
146         oldtid = cli2->tree->tid;
147         cli2->session->vuid = cli1->session->vuid;
148         cli2->tree->tid = cli1->tree->tid;
149         cli2->session->pid = cli1->session->pid;
150
151         torture_comment(tctx, "reading from file on connection 2\n");
152
153         torture_assert(tctx, smbcli_read(cli2->tree, fnum1, buf, 0, 13) != 13,
154                                    talloc_asprintf(tctx,
155                 "read succeeded! nasty security hole [%s]\n", buf));
156
157         smbcli_close(cli1->tree, fnum1);
158         smbcli_unlink(cli1->tree, fname);
159
160         cli2->tree->tid = oldtid;
161
162         return true;
163 }
164
165 /**
166   This checks how the getatr calls works
167 */
168 static bool run_attrtest(struct torture_context *tctx, 
169                                                  struct smbcli_state *cli)
170 {
171         int fnum;
172         time_t t, t2;
173         const char *fname = "\\attrib123456789.tst";
174         bool correct = true;
175
176         smbcli_unlink(cli->tree, fname);
177         fnum = smbcli_open(cli->tree, fname, 
178                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
179         smbcli_close(cli->tree, fnum);
180
181         if (NT_STATUS_IS_ERR(smbcli_getatr(cli->tree, fname, NULL, NULL, &t))) {
182                 torture_comment(tctx, "getatr failed (%s)\n", smbcli_errstr(cli->tree));
183                 correct = false;
184         }
185
186         torture_comment(tctx, "New file time is %s", ctime(&t));
187
188         if (abs(t - time(NULL)) > 60*60*24*10) {
189                 torture_comment(tctx, "ERROR: SMBgetatr bug. time is %s",
190                        ctime(&t));
191                 t = time(NULL);
192                 correct = false;
193         }
194
195         t2 = t-60*60*24; /* 1 day ago */
196
197         torture_comment(tctx, "Setting file time to %s", ctime(&t2));
198
199         if (NT_STATUS_IS_ERR(smbcli_setatr(cli->tree, fname, 0, t2))) {
200                 torture_comment(tctx, "setatr failed (%s)\n", smbcli_errstr(cli->tree));
201                 correct = true;
202         }
203
204         if (NT_STATUS_IS_ERR(smbcli_getatr(cli->tree, fname, NULL, NULL, &t))) {
205                 torture_comment(tctx, "getatr failed (%s)\n", smbcli_errstr(cli->tree));
206                 correct = true;
207         }
208
209         torture_comment(tctx, "Retrieved file time as %s", ctime(&t));
210
211         if (t != t2) {
212                 torture_comment(tctx, "ERROR: getatr/setatr bug. times are\n%s",
213                        ctime(&t));
214                 torture_comment(tctx, "%s", ctime(&t2));
215                 correct = true;
216         }
217
218         smbcli_unlink(cli->tree, fname);
219
220         return correct;
221 }
222
223 /**
224   This checks a couple of trans2 calls
225 */
226 static bool run_trans2test(struct torture_context *tctx, 
227                                                    struct smbcli_state *cli)
228 {
229         int fnum;
230         size_t size;
231         time_t c_time, a_time, m_time, w_time, m_time2;
232         const char *fname = "\\trans2.tst";
233         const char *dname = "\\trans2";
234         const char *fname2 = "\\trans2\\trans2.tst";
235         const char *pname;
236         bool correct = true;
237
238         smbcli_unlink(cli->tree, fname);
239
240         torture_comment(tctx, "Testing qfileinfo\n");
241         
242         fnum = smbcli_open(cli->tree, fname, 
243                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
244         if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, NULL, &size, &c_time, &a_time, &m_time,
245                            NULL, NULL))) {
246                 torture_comment(tctx, "ERROR: qfileinfo failed (%s)\n", smbcli_errstr(cli->tree));
247                 correct = false;
248         }
249
250         torture_comment(tctx, "Testing NAME_INFO\n");
251
252         if (NT_STATUS_IS_ERR(smbcli_qfilename(cli->tree, fnum, &pname))) {
253                 torture_comment(tctx, "ERROR: qfilename failed (%s)\n", smbcli_errstr(cli->tree));
254                 correct = false;
255         }
256
257         if (!pname || strcmp(pname, fname)) {
258                 torture_comment(tctx, "qfilename gave different name? [%s] [%s]\n",
259                        fname, pname);
260                 correct = false;
261         }
262
263         smbcli_close(cli->tree, fnum);
264         smbcli_unlink(cli->tree, fname);
265
266         fnum = smbcli_open(cli->tree, fname, 
267                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
268         if (fnum == -1) {
269                 torture_comment(tctx, "open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
270                 return false;
271         }
272         smbcli_close(cli->tree, fnum);
273
274         torture_comment(tctx, "Checking for sticky create times\n");
275
276         if (NT_STATUS_IS_ERR(smbcli_qpathinfo(cli->tree, fname, &c_time, &a_time, &m_time, &size, NULL))) {
277                 torture_comment(tctx, "ERROR: qpathinfo failed (%s)\n", smbcli_errstr(cli->tree));
278                 correct = false;
279         } else {
280                 if (c_time != m_time) {
281                         torture_comment(tctx, "create time=%s", ctime(&c_time));
282                         torture_comment(tctx, "modify time=%s", ctime(&m_time));
283                         torture_comment(tctx, "This system appears to have sticky create times\n");
284                 }
285                 if (a_time % (60*60) == 0) {
286                         torture_comment(tctx, "access time=%s", ctime(&a_time));
287                         torture_comment(tctx, "This system appears to set a midnight access time\n");
288                         correct = false;
289                 }
290
291                 if (abs(m_time - time(NULL)) > 60*60*24*7) {
292                         torture_comment(tctx, "ERROR: totally incorrect times - maybe word reversed? mtime=%s", ctime(&m_time));
293                         correct = false;
294                 }
295         }
296
297
298         smbcli_unlink(cli->tree, fname);
299         fnum = smbcli_open(cli->tree, fname, 
300                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
301         smbcli_close(cli->tree, fnum);
302         if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, fname, &c_time, &a_time, &m_time, &w_time, &size, NULL, NULL))) {
303                 torture_comment(tctx, "ERROR: qpathinfo2 failed (%s)\n", smbcli_errstr(cli->tree));
304                 correct = false;
305         } else {
306                 if (w_time < 60*60*24*2) {
307                         torture_comment(tctx, "write time=%s", ctime(&w_time));
308                         torture_comment(tctx, "This system appears to set a initial 0 write time\n");
309                         correct = false;
310                 }
311         }
312
313         smbcli_unlink(cli->tree, fname);
314
315
316         /* check if the server updates the directory modification time
317            when creating a new file */
318         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, dname))) {
319                 torture_comment(tctx, "ERROR: mkdir failed (%s)\n", smbcli_errstr(cli->tree));
320                 correct = false;
321         }
322         sleep(3);
323         if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, "\\trans2\\", &c_time, &a_time, &m_time, &w_time, &size, NULL, NULL))) {
324                 torture_comment(tctx, "ERROR: qpathinfo2 failed (%s)\n", smbcli_errstr(cli->tree));
325                 correct = false;
326         }
327
328         fnum = smbcli_open(cli->tree, fname2, 
329                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
330         smbcli_write(cli->tree, fnum,  0, &fnum, 0, sizeof(fnum));
331         smbcli_close(cli->tree, fnum);
332         if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, "\\trans2\\", &c_time, &a_time, &m_time2, &w_time, &size, NULL, NULL))) {
333                 torture_comment(tctx, "ERROR: qpathinfo2 failed (%s)\n", smbcli_errstr(cli->tree));
334                 correct = false;
335         } else {
336                 if (m_time2 == m_time) {
337                         torture_comment(tctx, "This system does not update directory modification times\n");
338                         correct = false;
339                 }
340         }
341         smbcli_unlink(cli->tree, fname2);
342         smbcli_rmdir(cli->tree, dname);
343
344         return correct;
345 }
346
347 /* send smb negprot commands, not reading the response */
348 static bool run_negprot_nowait(struct torture_context *tctx)
349 {
350         int i;
351         struct smbcli_state *cli, *cli2;
352         bool correct = true;
353
354         torture_comment(tctx, "starting negprot nowait test\n");
355
356         cli = open_nbt_connection(tctx);
357         if (!cli) {
358                 return false;
359         }
360
361         torture_comment(tctx, "Filling send buffer\n");
362
363         for (i=0;i<100;i++) {
364                 struct smbcli_request *req;
365                 req = smb_raw_negotiate_send(cli->transport, lp_unicode(tctx->lp_ctx), PROTOCOL_NT1);
366                 event_loop_once(cli->transport->socket->event.ctx);
367                 if (req->state == SMBCLI_REQUEST_ERROR) {
368                         if (i > 0) {
369                                 torture_comment(tctx, "Failed to fill pipe packet[%d] - %s (ignored)\n", i+1, nt_errstr(req->status));
370                                 break;
371                         } else {
372                                 torture_comment(tctx, "Failed to fill pipe - %s \n", nt_errstr(req->status));
373                                 torture_close_connection(cli);
374                                 return false;
375                         }
376                 }
377         }
378
379         torture_comment(tctx, "Opening secondary connection\n");
380         if (!torture_open_connection(&cli2, tctx, 1)) {
381                 torture_comment(tctx, "Failed to open secondary connection\n");
382                 correct = false;
383         }
384
385         if (!torture_close_connection(cli2)) {
386                 torture_comment(tctx, "Failed to close secondary connection\n");
387                 correct = false;
388         }
389
390         torture_close_connection(cli);
391
392         return correct;
393 }
394
395 /**
396   this checks to see if a secondary tconx can use open files from an
397   earlier tconx
398  */
399 static bool run_tcon_test(struct torture_context *tctx, struct smbcli_state *cli)
400 {
401         const char *fname = "\\tcontest.tmp";
402         int fnum1;
403         uint16_t cnum1, cnum2, cnum3;
404         uint16_t vuid1, vuid2;
405         uint8_t buf[4];
406         bool ret = true;
407         struct smbcli_tree *tree1;
408         const char *host = torture_setting_string(tctx, "host", NULL);
409         const char *share = torture_setting_string(tctx, "share", NULL);
410         const char *password = torture_setting_string(tctx, "password", NULL);
411
412         if (smbcli_deltree(cli->tree, fname) == -1) {
413                 torture_comment(tctx, "unlink of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
414         }
415
416         fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
417         if (fnum1 == -1) {
418                 torture_comment(tctx, "open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
419                 return false;
420         }
421
422         cnum1 = cli->tree->tid;
423         vuid1 = cli->session->vuid;
424
425         memset(buf, 0, 4); /* init buf so valgrind won't complain */
426         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) != 4) {
427                 torture_comment(tctx, "initial write failed (%s)\n", smbcli_errstr(cli->tree));
428                 return false;
429         }
430
431         tree1 = cli->tree;      /* save old tree connection */
432         if (NT_STATUS_IS_ERR(smbcli_tconX(cli, share, "?????", password))) {
433                 torture_comment(tctx, "%s refused 2nd tree connect (%s)\n", host,
434                            smbcli_errstr(cli->tree));
435                 talloc_free(cli);
436                 return false;
437         }
438
439         cnum2 = cli->tree->tid;
440         cnum3 = MAX(cnum1, cnum2) + 1; /* any invalid number */
441         vuid2 = cli->session->vuid + 1;
442
443         /* try a write with the wrong tid */
444         cli->tree->tid = cnum2;
445
446         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) == 4) {
447                 torture_comment(tctx, "* server allows write with wrong TID\n");
448                 ret = false;
449         } else {
450                 torture_comment(tctx, "server fails write with wrong TID : %s\n", smbcli_errstr(cli->tree));
451         }
452
453
454         /* try a write with an invalid tid */
455         cli->tree->tid = cnum3;
456
457         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) == 4) {
458                 torture_comment(tctx, "* server allows write with invalid TID\n");
459                 ret = false;
460         } else {
461                 torture_comment(tctx, "server fails write with invalid TID : %s\n", smbcli_errstr(cli->tree));
462         }
463
464         /* try a write with an invalid vuid */
465         cli->session->vuid = vuid2;
466         cli->tree->tid = cnum1;
467
468         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) == 4) {
469                 torture_comment(tctx, "* server allows write with invalid VUID\n");
470                 ret = false;
471         } else {
472                 torture_comment(tctx, "server fails write with invalid VUID : %s\n", smbcli_errstr(cli->tree));
473         }
474
475         cli->session->vuid = vuid1;
476         cli->tree->tid = cnum1;
477
478         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum1))) {
479                 torture_comment(tctx, "close failed (%s)\n", smbcli_errstr(cli->tree));
480                 return false;
481         }
482
483         cli->tree->tid = cnum2;
484
485         if (NT_STATUS_IS_ERR(smbcli_tdis(cli))) {
486                 torture_comment(tctx, "secondary tdis failed (%s)\n", smbcli_errstr(cli->tree));
487                 return false;
488         }
489
490         cli->tree = tree1;  /* restore initial tree */
491         cli->tree->tid = cnum1;
492
493         smbcli_unlink(tree1, fname);
494
495         return ret;
496 }
497
498 /**
499  checks for correct tconX support
500  */
501 static bool run_tcon_devtype_test(struct torture_context *tctx, 
502                                                                   struct smbcli_state *cli1)
503 {
504         const char *share = torture_setting_string(tctx, "share", NULL);
505
506         if (!tcon_devtest(tctx, cli1, "IPC$", "A:", NT_STATUS_BAD_DEVICE_TYPE))
507                 return false;
508
509         if (!tcon_devtest(tctx, cli1, "IPC$", "?????", NT_STATUS_OK))
510                 return false;
511
512         if (!tcon_devtest(tctx, cli1, "IPC$", "LPT:", NT_STATUS_BAD_DEVICE_TYPE))
513                 return false;
514
515         if (!tcon_devtest(tctx, cli1, "IPC$", "IPC", NT_STATUS_OK))
516                 return false;
517                         
518         if (!tcon_devtest(tctx, cli1, "IPC$", "FOOBA", NT_STATUS_BAD_DEVICE_TYPE))
519                 return false;
520
521         if (!tcon_devtest(tctx, cli1, share, "A:", NT_STATUS_OK))
522                 return false;
523
524         if (!tcon_devtest(tctx, cli1, share, "?????", NT_STATUS_OK))
525                 return false;
526
527         if (!tcon_devtest(tctx, cli1, share, "LPT:", NT_STATUS_BAD_DEVICE_TYPE))
528                 return false;
529
530         if (!tcon_devtest(tctx, cli1, share, "IPC", NT_STATUS_BAD_DEVICE_TYPE))
531                 return false;
532                         
533         if (!tcon_devtest(tctx, cli1, share, "FOOBA", NT_STATUS_BAD_DEVICE_TYPE))
534                 return false;
535
536         return true;
537 }
538
539 static bool rw_torture2(struct torture_context *tctx,
540                                                 struct smbcli_state *c1, struct smbcli_state *c2)
541 {
542         const char *lockfname = "\\torture2.lck";
543         int fnum1;
544         int fnum2;
545         int i;
546         uint8_t buf[131072];
547         uint8_t buf_rd[131072];
548         bool correct = true;
549         ssize_t bytes_read, bytes_written;
550
551         torture_assert(tctx, smbcli_deltree(c1->tree, lockfname) != -1,
552                                    talloc_asprintf(tctx, 
553                 "unlink failed (%s)", smbcli_errstr(c1->tree)));
554
555         fnum1 = smbcli_open(c1->tree, lockfname, O_RDWR | O_CREAT | O_EXCL, 
556                          DENY_NONE);
557         torture_assert(tctx, fnum1 != -1, 
558                                    talloc_asprintf(tctx, 
559                 "first open read/write of %s failed (%s)",
560                                 lockfname, smbcli_errstr(c1->tree)));
561         fnum2 = smbcli_open(c2->tree, lockfname, O_RDONLY, 
562                          DENY_NONE);
563         torture_assert(tctx, fnum2 != -1, 
564                                    talloc_asprintf(tctx, 
565                 "second open read-only of %s failed (%s)",
566                                 lockfname, smbcli_errstr(c2->tree)));
567
568         torture_comment(tctx, "Checking data integrity over %d ops\n", 
569                                         torture_numops);
570
571         for (i=0;i<torture_numops;i++)
572         {
573                 size_t buf_size = ((uint_t)random()%(sizeof(buf)-1))+ 1;
574                 if (i % 10 == 0) {
575                         if (torture_setting_bool(tctx, "progress", true)) {
576                                 torture_comment(tctx, "%d\r", i); fflush(stdout);
577                         }
578                 }
579
580                 generate_random_buffer(buf, buf_size);
581
582                 if ((bytes_written = smbcli_write(c1->tree, fnum1, 0, buf, 0, buf_size)) != buf_size) {
583                         torture_comment(tctx, "write failed (%s)\n", smbcli_errstr(c1->tree));
584                         torture_comment(tctx, "wrote %d, expected %d\n", (int)bytes_written, (int)buf_size); 
585                         correct = false;
586                         break;
587                 }
588
589                 if ((bytes_read = smbcli_read(c2->tree, fnum2, buf_rd, 0, buf_size)) != buf_size) {
590                         torture_comment(tctx, "read failed (%s)\n", smbcli_errstr(c2->tree));
591                         torture_comment(tctx, "read %d, expected %d\n", (int)bytes_read, (int)buf_size); 
592                         correct = false;
593                         break;
594                 }
595
596                 torture_assert_mem_equal(tctx, buf_rd, buf, buf_size, 
597                         "read/write compare failed\n");
598         }
599
600         torture_assert_ntstatus_ok(tctx, smbcli_close(c2->tree, fnum2), 
601                 talloc_asprintf(tctx, "close failed (%s)", smbcli_errstr(c2->tree)));
602         torture_assert_ntstatus_ok(tctx, smbcli_close(c1->tree, fnum1),
603                 talloc_asprintf(tctx, "close failed (%s)", smbcli_errstr(c1->tree)));
604
605         torture_assert_ntstatus_ok(tctx, smbcli_unlink(c1->tree, lockfname),
606                 talloc_asprintf(tctx, "unlink failed (%s)", smbcli_errstr(c1->tree)));
607
608         torture_comment(tctx, "\n");
609
610         return correct;
611 }
612
613
614
615 static bool run_readwritetest(struct torture_context *tctx,
616                                                           struct smbcli_state *cli1,
617                                                           struct smbcli_state *cli2)
618 {
619         torture_comment(tctx, "Running readwritetest v1\n");
620         if (!rw_torture2(tctx, cli1, cli2)) 
621                 return false;
622
623         torture_comment(tctx, "Running readwritetest v2\n");
624
625         if (!rw_torture2(tctx, cli1, cli1))
626                 return false;
627
628         return true;
629 }
630
631 /*
632 test the timing of deferred open requests
633 */
634 static bool run_deferopen(struct torture_context *tctx, struct smbcli_state *cli, int dummy)
635 {
636         const char *fname = "\\defer_open_test.dat";
637         int retries=4;
638         int i = 0;
639         bool correct = true;
640         int nsec;
641         int msec;
642         double sec;
643
644         nsec = torture_setting_int(tctx, "sharedelay", 1000000);
645         msec = nsec / 1000;
646         sec = ((double)nsec) / ((double) 1000000);
647
648         if (retries <= 0) {
649                 torture_comment(tctx, "failed to connect\n");
650                 return false;
651         }
652
653         torture_comment(tctx, "Testing deferred open requests.\n");
654
655         while (i < 4) {
656                 int fnum = -1;
657
658                 do {
659                         struct timeval tv;
660                         tv = timeval_current();
661                         fnum = smbcli_nt_create_full(cli->tree, fname, 0, 
662                                                      SEC_RIGHTS_FILE_ALL,
663                                                      FILE_ATTRIBUTE_NORMAL, 
664                                                      NTCREATEX_SHARE_ACCESS_NONE,
665                                                      NTCREATEX_DISP_OPEN_IF, 0, 0);
666                         if (fnum != -1) {
667                                 break;
668                         }
669                         if (NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),NT_STATUS_SHARING_VIOLATION)) {
670                                 double e = timeval_elapsed(&tv);
671                                 if (e < (0.5 * sec) || e > ((1.5 * sec) + 1)) {
672                                         torture_comment(tctx,"Timing incorrect %.2f violation 1 sec == %.2f\n",
673                                                 e, sec);
674                                         return false;
675                                 }
676                         }
677                 } while (NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),NT_STATUS_SHARING_VIOLATION));
678
679                 if (fnum == -1) {
680                         torture_comment(tctx,"Failed to open %s, error=%s\n", fname, smbcli_errstr(cli->tree));
681                         return false;
682                 }
683
684                 torture_comment(tctx, "pid %u open %d\n", (unsigned)getpid(), i);
685
686                 msleep(10 * msec);
687                 i++;
688                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
689                         torture_comment(tctx,"Failed to close %s, error=%s\n", fname, smbcli_errstr(cli->tree));
690                         return false;
691                 }
692                 msleep(2 * msec);
693         }
694
695         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
696                 /* All until the last unlink will fail with sharing violation. */
697                 if (!NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),NT_STATUS_SHARING_VIOLATION)) {
698                         torture_comment(tctx, "unlink of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
699                         correct = false;
700                 }
701         }
702
703         torture_comment(tctx, "deferred test finished\n");
704         return correct;
705 }
706
707 /**
708   Try with a wrong vuid and check error message.
709  */
710
711 static bool run_vuidtest(struct torture_context *tctx, 
712                                                  struct smbcli_state *cli)
713 {
714         const char *fname = "\\vuid.tst";
715         int fnum;
716         size_t size;
717         time_t c_time, a_time, m_time;
718
719         uint16_t orig_vuid;
720         NTSTATUS result;
721
722         smbcli_unlink(cli->tree, fname);
723
724         fnum = smbcli_open(cli->tree, fname, 
725                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
726
727         orig_vuid = cli->session->vuid;
728
729         cli->session->vuid += 1234;
730
731         torture_comment(tctx, "Testing qfileinfo with wrong vuid\n");
732         
733         if (NT_STATUS_IS_OK(result = smbcli_qfileinfo(cli->tree, fnum, NULL,
734                                                    &size, &c_time, &a_time,
735                                                    &m_time, NULL, NULL))) {
736                 torture_fail(tctx, "qfileinfo passed with wrong vuid");
737         }
738
739         if (!NT_STATUS_EQUAL(cli->transport->error.e.nt_status, 
740                              NT_STATUS_DOS(ERRSRV, ERRbaduid)) &&
741             !NT_STATUS_EQUAL(cli->transport->error.e.nt_status, 
742                              NT_STATUS_INVALID_HANDLE)) {
743                 torture_fail(tctx, talloc_asprintf(tctx, 
744                                 "qfileinfo should have returned DOS error "
745                        "ERRSRV:ERRbaduid\n  but returned %s",
746                        smbcli_errstr(cli->tree)));
747         }
748
749         cli->session->vuid -= 1234;
750
751         torture_assert_ntstatus_ok(tctx, smbcli_close(cli->tree, fnum),
752                 talloc_asprintf(tctx, "close failed (%s)", smbcli_errstr(cli->tree)));
753
754         smbcli_unlink(cli->tree, fname);
755
756         return true;
757 }
758
759 /*
760   Test open mode returns on read-only files.
761  */
762  static bool run_opentest(struct torture_context *tctx, struct smbcli_state *cli1, 
763                                                   struct smbcli_state *cli2)
764 {
765         const char *fname = "\\readonly.file";
766         char *control_char_fname;
767         int fnum1, fnum2;
768         uint8_t buf[20];
769         size_t fsize;
770         bool correct = true;
771         char *tmp_path;
772         int failures = 0;
773         int i;
774
775         asprintf(&control_char_fname, "\\readonly.afile");
776         for (i = 1; i <= 0x1f; i++) {
777                 control_char_fname[10] = i;
778                 fnum1 = smbcli_nt_create_full(cli1->tree, control_char_fname, 0, SEC_FILE_WRITE_DATA, FILE_ATTRIBUTE_NORMAL,
779                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
780                 
781                 if (!check_error(__location__, cli1, ERRDOS, ERRinvalidname, 
782                                 NT_STATUS_OBJECT_NAME_INVALID)) {
783                         torture_comment(tctx, "Error code should be NT_STATUS_OBJECT_NAME_INVALID, was %s for file with %d char\n",
784                                         smbcli_errstr(cli1->tree), i);
785                         failures++;
786                 }
787
788                 if (fnum1 != -1) {
789                         smbcli_close(cli1->tree, fnum1);
790                 }
791                 smbcli_setatr(cli1->tree, control_char_fname, 0, 0);
792                 smbcli_unlink(cli1->tree, control_char_fname);
793         }
794         free(control_char_fname);
795
796         if (!failures)
797                 torture_comment(tctx, "Create file with control char names passed.\n");
798
799         smbcli_setatr(cli1->tree, fname, 0, 0);
800         smbcli_unlink(cli1->tree, fname);
801         
802         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
803         if (fnum1 == -1) {
804                 torture_comment(tctx, "open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
805                 return false;
806         }
807
808         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
809                 torture_comment(tctx, "close2 failed (%s)\n", smbcli_errstr(cli1->tree));
810                 return false;
811         }
812         
813         if (NT_STATUS_IS_ERR(smbcli_setatr(cli1->tree, fname, FILE_ATTRIBUTE_READONLY, 0))) {
814                 torture_comment(tctx, "smbcli_setatr failed (%s)\n", smbcli_errstr(cli1->tree));
815                 CHECK_MAX_FAILURES(error_test1);
816                 return false;
817         }
818         
819         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_WRITE);
820         if (fnum1 == -1) {
821                 torture_comment(tctx, "open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
822                 CHECK_MAX_FAILURES(error_test1);
823                 return false;
824         }
825         
826         /* This will fail - but the error should be ERRnoaccess, not ERRbadshare. */
827         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_ALL);
828         
829         if (check_error(__location__, cli1, ERRDOS, ERRnoaccess, 
830                         NT_STATUS_ACCESS_DENIED)) {
831                 torture_comment(tctx, "correct error code ERRDOS/ERRnoaccess returned\n");
832         }
833         
834         torture_comment(tctx, "finished open test 1\n");
835 error_test1:
836         smbcli_close(cli1->tree, fnum1);
837         
838         /* Now try not readonly and ensure ERRbadshare is returned. */
839         
840         smbcli_setatr(cli1->tree, fname, 0, 0);
841         
842         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_WRITE);
843         if (fnum1 == -1) {
844                 torture_comment(tctx, "open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
845                 return false;
846         }
847         
848         /* This will fail - but the error should be ERRshare. */
849         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_ALL);
850         
851         if (check_error(__location__, cli1, ERRDOS, ERRbadshare, 
852                         NT_STATUS_SHARING_VIOLATION)) {
853                 torture_comment(tctx, "correct error code ERRDOS/ERRbadshare returned\n");
854         }
855         
856         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
857                 torture_comment(tctx, "close2 failed (%s)\n", smbcli_errstr(cli1->tree));
858                 return false;
859         }
860         
861         smbcli_unlink(cli1->tree, fname);
862         
863         torture_comment(tctx, "finished open test 2\n");
864         
865         /* Test truncate open disposition on file opened for read. */
866         
867         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
868         if (fnum1 == -1) {
869                 torture_comment(tctx, "(3) open (1) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
870                 return false;
871         }
872         
873         /* write 20 bytes. */
874         
875         memset(buf, '\0', 20);
876
877         if (smbcli_write(cli1->tree, fnum1, 0, buf, 0, 20) != 20) {
878                 torture_comment(tctx, "write failed (%s)\n", smbcli_errstr(cli1->tree));
879                 correct = false;
880         }
881
882         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
883                 torture_comment(tctx, "(3) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
884                 return false;
885         }
886         
887         /* Ensure size == 20. */
888         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
889                 torture_comment(tctx, "(3) getatr failed (%s)\n", smbcli_errstr(cli1->tree));
890                 CHECK_MAX_FAILURES(error_test3);
891                 return false;
892         }
893         
894         if (fsize != 20) {
895                 torture_comment(tctx, "(3) file size != 20\n");
896                 CHECK_MAX_FAILURES(error_test3);
897                 return false;
898         }
899
900         /* Now test if we can truncate a file opened for readonly. */
901         
902         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY|O_TRUNC, DENY_NONE);
903         if (fnum1 == -1) {
904                 torture_comment(tctx, "(3) open (2) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
905                 CHECK_MAX_FAILURES(error_test3);
906                 return false;
907         }
908         
909         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
910                 torture_comment(tctx, "close2 failed (%s)\n", smbcli_errstr(cli1->tree));
911                 return false;
912         }
913
914         /* Ensure size == 0. */
915         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
916                 torture_comment(tctx, "(3) getatr failed (%s)\n", smbcli_errstr(cli1->tree));
917                 CHECK_MAX_FAILURES(error_test3);
918                 return false;
919         }
920
921         if (fsize != 0) {
922                 torture_comment(tctx, "(3) file size != 0\n");
923                 CHECK_MAX_FAILURES(error_test3);
924                 return false;
925         }
926         torture_comment(tctx, "finished open test 3\n");
927 error_test3:    
928         smbcli_unlink(cli1->tree, fname);
929
930
931         torture_comment(tctx, "testing ctemp\n");
932         fnum1 = smbcli_ctemp(cli1->tree, "\\", &tmp_path);
933         if (fnum1 == -1) {
934                 torture_comment(tctx, "ctemp failed (%s)\n", smbcli_errstr(cli1->tree));
935                 CHECK_MAX_FAILURES(error_test4);
936                 return false;
937         }
938         torture_comment(tctx, "ctemp gave path %s\n", tmp_path);
939         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
940                 torture_comment(tctx, "close of temp failed (%s)\n", smbcli_errstr(cli1->tree));
941         }
942         if (NT_STATUS_IS_ERR(smbcli_unlink(cli1->tree, tmp_path))) {
943                 torture_comment(tctx, "unlink of temp failed (%s)\n", smbcli_errstr(cli1->tree));
944         }
945 error_test4:    
946         /* Test the non-io opens... */
947
948         smbcli_setatr(cli2->tree, fname, 0, 0);
949         smbcli_unlink(cli2->tree, fname);
950         
951         torture_comment(tctx, "TEST #1 testing 2 non-io opens (no delete)\n");
952         
953         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
954                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
955
956         if (fnum1 == -1) {
957                 torture_comment(tctx, "test 1 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
958                 CHECK_MAX_FAILURES(error_test10);
959                 return false;
960         }
961
962         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
963                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
964         if (fnum2 == -1) {
965                 torture_comment(tctx, "test 1 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
966                 CHECK_MAX_FAILURES(error_test10);
967                 return false;
968         }
969
970         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
971                 torture_comment(tctx, "test 1 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
972                 return false;
973         }
974         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
975                 torture_comment(tctx, "test 1 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
976                 return false;
977         }
978
979         torture_comment(tctx, "non-io open test #1 passed.\n");
980 error_test10:
981         smbcli_unlink(cli1->tree, fname);
982
983         torture_comment(tctx, "TEST #2 testing 2 non-io opens (first with delete)\n");
984         
985         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
986                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
987
988         if (fnum1 == -1) {
989                 torture_comment(tctx, "test 2 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
990                 CHECK_MAX_FAILURES(error_test20);
991                 return false;
992         }
993
994         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
995                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
996
997         if (fnum2 == -1) {
998                 torture_comment(tctx, "test 2 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
999                 CHECK_MAX_FAILURES(error_test20);
1000                 return false;
1001         }
1002
1003         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1004                 torture_comment(tctx, "test 1 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1005                 return false;
1006         }
1007         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1008                 torture_comment(tctx, "test 1 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1009                 return false;
1010         }
1011
1012         torture_comment(tctx, "non-io open test #2 passed.\n");
1013 error_test20:
1014         smbcli_unlink(cli1->tree, fname);
1015
1016         torture_comment(tctx, "TEST #3 testing 2 non-io opens (second with delete)\n");
1017         
1018         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1019                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1020
1021         if (fnum1 == -1) {
1022                 torture_comment(tctx, "test 3 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1023                 CHECK_MAX_FAILURES(error_test30);
1024                 return false;
1025         }
1026
1027         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1028                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1029
1030         if (fnum2 == -1) {
1031                 torture_comment(tctx, "test 3 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1032                 CHECK_MAX_FAILURES(error_test30);
1033                 return false;
1034         }
1035
1036         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1037                 torture_comment(tctx, "test 3 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1038                 return false;
1039         }
1040         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1041                 torture_comment(tctx, "test 3 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1042                 return false;
1043         }
1044
1045         torture_comment(tctx, "non-io open test #3 passed.\n");
1046 error_test30:
1047         smbcli_unlink(cli1->tree, fname);
1048
1049         torture_comment(tctx, "TEST #4 testing 2 non-io opens (both with delete)\n");
1050         
1051         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1052                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1053
1054         if (fnum1 == -1) {
1055                 torture_comment(tctx, "test 4 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1056                 CHECK_MAX_FAILURES(error_test40);
1057                 return false;
1058         }
1059
1060         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1061                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1062
1063         if (fnum2 != -1) {
1064                 torture_comment(tctx, "test 4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1065                 CHECK_MAX_FAILURES(error_test40);
1066                 return false;
1067         }
1068
1069         torture_comment(tctx, "test 4 open 2 of %s gave %s (correct error should be %s)\n", fname, smbcli_errstr(cli2->tree), "sharing violation");
1070
1071         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1072                 torture_comment(tctx, "test 4 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1073                 return false;
1074         }
1075
1076         torture_comment(tctx, "non-io open test #4 passed.\n");
1077 error_test40:
1078         smbcli_unlink(cli1->tree, fname);
1079
1080         torture_comment(tctx, "TEST #5 testing 2 non-io opens (both with delete - both with file share delete)\n");
1081         
1082         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1083                                    NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1084
1085         if (fnum1 == -1) {
1086                 torture_comment(tctx, "test 5 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1087                 CHECK_MAX_FAILURES(error_test50);
1088                 return false;
1089         }
1090
1091         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1092                                    NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1093
1094         if (fnum2 == -1) {
1095                 torture_comment(tctx, "test 5 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1096                 CHECK_MAX_FAILURES(error_test50);
1097                 return false;
1098         }
1099
1100         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1101                 torture_comment(tctx, "test 5 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1102                 return false;
1103         }
1104
1105         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1106                 torture_comment(tctx, "test 5 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1107                 return false;
1108         }
1109
1110         torture_comment(tctx, "non-io open test #5 passed.\n");
1111 error_test50:
1112         torture_comment(tctx, "TEST #6 testing 1 non-io open, one io open\n");
1113         
1114         smbcli_unlink(cli1->tree, fname);
1115
1116         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
1117                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1118
1119         if (fnum1 == -1) {
1120                 torture_comment(tctx, "test 6 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1121                 CHECK_MAX_FAILURES(error_test60);
1122                 return false;
1123         }
1124
1125         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1126                                    NTCREATEX_SHARE_ACCESS_READ, NTCREATEX_DISP_OPEN_IF, 0, 0);
1127
1128         if (fnum2 == -1) {
1129                 torture_comment(tctx, "test 6 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1130                 CHECK_MAX_FAILURES(error_test60);
1131                 return false;
1132         }
1133
1134         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1135                 torture_comment(tctx, "test 6 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1136                 return false;
1137         }
1138
1139         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1140                 torture_comment(tctx, "test 6 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1141                 return false;
1142         }
1143
1144         torture_comment(tctx, "non-io open test #6 passed.\n");
1145 error_test60:
1146         torture_comment(tctx, "TEST #7 testing 1 non-io open, one io open with delete\n");
1147
1148         smbcli_unlink(cli1->tree, fname);
1149
1150         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
1151                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1152
1153         if (fnum1 == -1) {
1154                 torture_comment(tctx, "test 7 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1155                 CHECK_MAX_FAILURES(error_test70);
1156                 return false;
1157         }
1158
1159         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1160                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1161
1162         if (fnum2 != -1) {
1163                 torture_comment(tctx, "test 7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1164                 CHECK_MAX_FAILURES(error_test70);
1165                 return false;
1166         }
1167
1168         torture_comment(tctx, "test 7 open 2 of %s gave %s (correct error should be %s)\n", fname, smbcli_errstr(cli2->tree), "sharing violation");
1169
1170         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1171                 torture_comment(tctx, "test 7 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1172                 return false;
1173         }
1174
1175         torture_comment(tctx, "non-io open test #7 passed.\n");
1176
1177 error_test70:
1178
1179         torture_comment(tctx, "TEST #8 testing one normal open, followed by lock, followed by open with truncate\n");
1180
1181         smbcli_unlink(cli1->tree, fname);
1182
1183         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1184         if (fnum1 == -1) {
1185                 torture_comment(tctx, "(8) open (1) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1186                 return false;
1187         }
1188         
1189         /* write 20 bytes. */
1190         
1191         memset(buf, '\0', 20);
1192
1193         if (smbcli_write(cli1->tree, fnum1, 0, buf, 0, 20) != 20) {
1194                 torture_comment(tctx, "(8) write failed (%s)\n", smbcli_errstr(cli1->tree));
1195                 correct = false;
1196         }
1197
1198         /* Ensure size == 20. */
1199         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1200                 torture_comment(tctx, "(8) getatr (1) failed (%s)\n", smbcli_errstr(cli1->tree));
1201                 CHECK_MAX_FAILURES(error_test80);
1202                 return false;
1203         }
1204         
1205         if (fsize != 20) {
1206                 torture_comment(tctx, "(8) file size != 20\n");
1207                 CHECK_MAX_FAILURES(error_test80);
1208                 return false;
1209         }
1210
1211         /* Get an exclusive lock on the open file. */
1212         if (NT_STATUS_IS_ERR(smbcli_lock(cli1->tree, fnum1, 0, 4, 0, WRITE_LOCK))) {
1213                 torture_comment(tctx, "(8) lock1 failed (%s)\n", smbcli_errstr(cli1->tree));
1214                 CHECK_MAX_FAILURES(error_test80);
1215                 return false;
1216         }
1217
1218         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR|O_TRUNC, DENY_NONE);
1219         if (fnum1 == -1) {
1220                 torture_comment(tctx, "(8) open (2) of %s with truncate failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1221                 return false;
1222         }
1223
1224         /* Ensure size == 0. */
1225         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1226                 torture_comment(tctx, "(8) getatr (2) failed (%s)\n", smbcli_errstr(cli1->tree));
1227                 CHECK_MAX_FAILURES(error_test80);
1228                 return false;
1229         }
1230         
1231         if (fsize != 0) {
1232                 torture_comment(tctx, "(8) file size != 0\n");
1233                 CHECK_MAX_FAILURES(error_test80);
1234                 return false;
1235         }
1236
1237         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1238                 torture_comment(tctx, "(8) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
1239                 return false;
1240         }
1241         
1242         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum2))) {
1243                 torture_comment(tctx, "(8) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
1244                 return false;
1245         }
1246         
1247 error_test80:
1248
1249         torture_comment(tctx, "open test #8 passed.\n");
1250
1251         smbcli_unlink(cli1->tree, fname);
1252
1253         return correct;
1254 }
1255
1256 /* FIRST_DESIRED_ACCESS   0xf019f */
1257 #define FIRST_DESIRED_ACCESS   SEC_FILE_READ_DATA|SEC_FILE_WRITE_DATA|SEC_FILE_APPEND_DATA|\
1258                                SEC_FILE_READ_EA|                           /* 0xf */ \
1259                                SEC_FILE_WRITE_EA|SEC_FILE_READ_ATTRIBUTE|     /* 0x90 */ \
1260                                SEC_FILE_WRITE_ATTRIBUTE|                  /* 0x100 */ \
1261                                SEC_STD_DELETE|SEC_STD_READ_CONTROL|\
1262                                SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER     /* 0xf0000 */
1263 /* SECOND_DESIRED_ACCESS  0xe0080 */
1264 #define SECOND_DESIRED_ACCESS  SEC_FILE_READ_ATTRIBUTE|                   /* 0x80 */ \
1265                                SEC_STD_READ_CONTROL|SEC_STD_WRITE_DAC|\
1266                                SEC_STD_WRITE_OWNER                      /* 0xe0000 */
1267
1268 #if 0
1269 #define THIRD_DESIRED_ACCESS   FILE_READ_ATTRIBUTE|                   /* 0x80 */ \
1270                                READ_CONTROL|WRITE_DAC|\
1271                                SEC_FILE_READ_DATA|\
1272                                WRITE_OWNER                      /* */
1273 #endif
1274
1275
1276
1277 /**
1278   Test ntcreate calls made by xcopy
1279  */
1280 static bool run_xcopy(struct torture_context *tctx,
1281                                           struct smbcli_state *cli1)
1282 {
1283         const char *fname = "\\test.txt";
1284         int fnum1, fnum2;
1285
1286         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0,
1287                                       FIRST_DESIRED_ACCESS, 
1288                                       FILE_ATTRIBUTE_ARCHIVE,
1289                                       NTCREATEX_SHARE_ACCESS_NONE, 
1290                                       NTCREATEX_DISP_OVERWRITE_IF, 
1291                                       0x4044, 0);
1292
1293         torture_assert(tctx, fnum1 != -1, talloc_asprintf(tctx, 
1294                                 "First open failed - %s", smbcli_errstr(cli1->tree)));
1295
1296         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0,
1297                                    SECOND_DESIRED_ACCESS, 0,
1298                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN, 
1299                                    0x200000, 0);
1300         torture_assert(tctx, fnum2 != -1, talloc_asprintf(tctx, 
1301                                 "second open failed - %s", smbcli_errstr(cli1->tree)));
1302         
1303         return true;
1304 }
1305
1306 static bool run_iometer(struct torture_context *tctx,
1307                                                 struct smbcli_state *cli)
1308 {
1309         const char *fname = "\\iobw.tst";
1310         int fnum;
1311         size_t filesize;
1312         NTSTATUS status;
1313         char buf[2048];
1314         int ops;
1315
1316         memset(buf, 0, sizeof(buf));
1317
1318         status = smbcli_getatr(cli->tree, fname, NULL, &filesize, NULL);
1319         torture_assert_ntstatus_ok(tctx, status, 
1320                 talloc_asprintf(tctx, "smbcli_getatr failed: %s", nt_errstr(status)));
1321
1322         torture_comment(tctx, "size: %d\n", (int)filesize);
1323
1324         filesize -= (sizeof(buf) - 1);
1325
1326         fnum = smbcli_nt_create_full(cli->tree, fname, 0x16,
1327                                      0x2019f, 0, 0x3, 3, 0x42, 0x3);
1328         torture_assert(tctx, fnum != -1, talloc_asprintf(tctx, "open failed: %s", 
1329                                    smbcli_errstr(cli->tree)));
1330
1331         ops = 0;
1332
1333         while (true) {
1334                 int i, num_reads, num_writes;
1335
1336                 num_reads = random() % 10;
1337                 num_writes = random() % 3;
1338
1339                 for (i=0; i<num_reads; i++) {
1340                         ssize_t res;
1341                         if (ops++ > torture_numops) {
1342                                 return true;
1343                         }
1344                         res = smbcli_read(cli->tree, fnum, buf,
1345                                           random() % filesize, sizeof(buf));
1346                         torture_assert(tctx, res == sizeof(buf), 
1347                                                    talloc_asprintf(tctx, "read failed: %s",
1348                                                                                    smbcli_errstr(cli->tree)));
1349                 }
1350                 for (i=0; i<num_writes; i++) {
1351                         ssize_t res;
1352                         if (ops++ > torture_numops) {
1353                                 return true;
1354                         }
1355                         res = smbcli_write(cli->tree, fnum, 0, buf,
1356                                           random() % filesize, sizeof(buf));
1357                         torture_assert(tctx, res == sizeof(buf),
1358                                                    talloc_asprintf(tctx, "read failed: %s",
1359                                        smbcli_errstr(cli->tree)));
1360                 }
1361         }
1362
1363         return true;
1364 }
1365
1366 /**
1367   tries variants of chkpath
1368  */
1369 static bool torture_chkpath_test(struct torture_context *tctx, 
1370                                                                  struct smbcli_state *cli)
1371 {
1372         int fnum;
1373         bool ret;
1374
1375         torture_comment(tctx, "Testing valid and invalid paths\n");
1376
1377         /* cleanup from an old run */
1378         smbcli_rmdir(cli->tree, "\\chkpath.dir\\dir2");
1379         smbcli_unlink(cli->tree, "\\chkpath.dir\\*");
1380         smbcli_rmdir(cli->tree, "\\chkpath.dir");
1381
1382         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\chkpath.dir"))) {
1383                 torture_comment(tctx, "mkdir1 failed : %s\n", smbcli_errstr(cli->tree));
1384                 return false;
1385         }
1386
1387         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\chkpath.dir\\dir2"))) {
1388                 torture_comment(tctx, "mkdir2 failed : %s\n", smbcli_errstr(cli->tree));
1389                 return false;
1390         }
1391
1392         fnum = smbcli_open(cli->tree, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1393         if (fnum == -1) {
1394                 torture_comment(tctx, "open1 failed (%s)\n", smbcli_errstr(cli->tree));
1395                 return false;
1396         }
1397         smbcli_close(cli->tree, fnum);
1398
1399         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir"))) {
1400                 torture_comment(tctx, "chkpath1 failed: %s\n", smbcli_errstr(cli->tree));
1401                 ret = false;
1402         }
1403
1404         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\dir2"))) {
1405                 torture_comment(tctx, "chkpath2 failed: %s\n", smbcli_errstr(cli->tree));
1406                 ret = false;
1407         }
1408
1409         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\foo.txt"))) {
1410                 ret = check_error(__location__, cli, ERRDOS, ERRbadpath, 
1411                                   NT_STATUS_NOT_A_DIRECTORY);
1412         } else {
1413                 torture_comment(tctx, "* chkpath on a file should fail\n");
1414                 ret = false;
1415         }
1416
1417         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\bar.txt"))) {
1418                 ret = check_error(__location__, cli, ERRDOS, ERRbadpath, 
1419                                   NT_STATUS_OBJECT_NAME_NOT_FOUND);
1420         } else {
1421                 torture_comment(tctx, "* chkpath on a non existent file should fail\n");
1422                 ret = false;
1423         }
1424
1425         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\dirxx\\bar.txt"))) {
1426                 ret = check_error(__location__, cli, ERRDOS, ERRbadpath, 
1427                                   NT_STATUS_OBJECT_PATH_NOT_FOUND);
1428         } else {
1429                 torture_comment(tctx, "* chkpath on a non existent component should fail\n");
1430                 ret = false;
1431         }
1432
1433         smbcli_rmdir(cli->tree, "\\chkpath.dir\\dir2");
1434         smbcli_unlink(cli->tree, "\\chkpath.dir\\*");
1435         smbcli_rmdir(cli->tree, "\\chkpath.dir");
1436
1437         return ret;
1438 }
1439
1440 /*
1441  * This is a test to excercise some weird Samba3 error paths.
1442  */
1443
1444 static bool torture_samba3_errorpaths(struct torture_context *tctx)
1445 {
1446         bool nt_status_support;
1447         struct smbcli_state *cli_nt = NULL, *cli_dos = NULL;
1448         bool result = false;
1449         int fnum;
1450         const char *os2_fname = ".+,;=[].";
1451         const char *dname = "samba3_errordir";
1452         union smb_open io;
1453         NTSTATUS status;
1454
1455         nt_status_support = lp_nt_status_support(tctx->lp_ctx);
1456
1457         if (!lp_set_cmdline(tctx->lp_ctx, "nt status support", "yes")) {
1458                 torture_comment(tctx, "Could not set 'nt status support = yes'\n");
1459                 goto fail;
1460         }
1461
1462         if (!torture_open_connection(&cli_nt, tctx, 0)) {
1463                 goto fail;
1464         }
1465
1466         if (!lp_set_cmdline(tctx->lp_ctx, "nt status support", "no")) {
1467                 torture_comment(tctx, "Could not set 'nt status support = yes'\n");
1468                 goto fail;
1469         }
1470
1471         if (!torture_open_connection(&cli_dos, tctx, 1)) {
1472                 goto fail;
1473         }
1474
1475         if (!lp_set_cmdline(tctx->lp_ctx, "nt status support",
1476                             nt_status_support ? "yes":"no")) {
1477                 torture_comment(tctx, "Could not reset 'nt status support = yes'");
1478                 goto fail;
1479         }
1480
1481         smbcli_unlink(cli_nt->tree, os2_fname);
1482         smbcli_rmdir(cli_nt->tree, dname);
1483
1484         if (!NT_STATUS_IS_OK(smbcli_mkdir(cli_nt->tree, dname))) {
1485                 torture_comment(tctx, "smbcli_mkdir(%s) failed: %s\n", dname,
1486                        smbcli_errstr(cli_nt->tree));
1487                 goto fail;
1488         }
1489
1490         io.generic.level = RAW_OPEN_NTCREATEX;
1491         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
1492         io.ntcreatex.in.root_fid = 0;
1493         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
1494         io.ntcreatex.in.alloc_size = 1024*1024;
1495         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
1496         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
1497         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
1498         io.ntcreatex.in.create_options = 0;
1499         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1500         io.ntcreatex.in.security_flags = 0;
1501         io.ntcreatex.in.fname = dname;
1502
1503         status = smb_raw_open(cli_nt->tree, tctx, &io);
1504         if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1505                 torture_comment(tctx, "(%s) incorrect status %s should be %s\n",
1506                        __location__, nt_errstr(status),
1507                        nt_errstr(NT_STATUS_OBJECT_NAME_COLLISION));
1508                 goto fail;
1509         }
1510         status = smb_raw_open(cli_dos->tree, tctx, &io);
1511         if (!NT_STATUS_EQUAL(status, NT_STATUS_DOS(ERRDOS, ERRfilexists))) {
1512                 torture_comment(tctx, "(%s) incorrect status %s should be %s\n",
1513                        __location__, nt_errstr(status),
1514                        nt_errstr(NT_STATUS_DOS(ERRDOS, ERRfilexists)));
1515                 goto fail;
1516         }
1517
1518         status = smbcli_mkdir(cli_nt->tree, dname);
1519         if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1520                 torture_comment(tctx, "(%s) incorrect status %s should be %s\n",
1521                        __location__, nt_errstr(status),
1522                        nt_errstr(NT_STATUS_OBJECT_NAME_COLLISION));
1523                 goto fail;
1524         }
1525         status = smbcli_mkdir(cli_dos->tree, dname);
1526         if (!NT_STATUS_EQUAL(status, NT_STATUS_DOS(ERRDOS, ERRnoaccess))) {
1527                 torture_comment(tctx, "(%s) incorrect status %s should be %s\n",
1528                        __location__, nt_errstr(status),
1529                        nt_errstr(NT_STATUS_DOS(ERRDOS, ERRnoaccess)));
1530                 goto fail;
1531         }
1532
1533         {
1534                 union smb_mkdir md;
1535                 md.t2mkdir.level = RAW_MKDIR_T2MKDIR;
1536                 md.t2mkdir.in.path = dname;
1537                 md.t2mkdir.in.num_eas = 0;
1538                 md.t2mkdir.in.eas = NULL;
1539
1540                 status = smb_raw_mkdir(cli_nt->tree, &md);
1541                 if (!NT_STATUS_EQUAL(status,
1542                                      NT_STATUS_OBJECT_NAME_COLLISION)) {
1543                         torture_comment(
1544                                 tctx, "(%s) incorrect status %s should be "
1545                                 "NT_STATUS_OBJECT_NAME_COLLISION\n",
1546                                 __location__, nt_errstr(status));
1547                         goto fail;
1548                 }
1549                 status = smb_raw_mkdir(cli_dos->tree, &md);
1550                 if (!NT_STATUS_EQUAL(status,
1551                                      NT_STATUS_DOS(ERRDOS, ERRrename))) {
1552                         torture_comment(tctx, "(%s) incorrect status %s "
1553                                         "should be ERRDOS:ERRrename\n",
1554                                         __location__, nt_errstr(status));
1555                         goto fail;
1556                 }
1557         }
1558
1559         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1560         status = smb_raw_open(cli_nt->tree, tctx, &io);
1561         if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1562                 torture_comment(tctx, "(%s) incorrect status %s should be %s\n",
1563                        __location__, nt_errstr(status),
1564                        nt_errstr(NT_STATUS_OBJECT_NAME_COLLISION));
1565                 goto fail;
1566         }
1567
1568         status = smb_raw_open(cli_dos->tree, tctx, &io);
1569         if (!NT_STATUS_EQUAL(status, NT_STATUS_DOS(ERRDOS, ERRfilexists))) {
1570                 torture_comment(tctx, "(%s) incorrect status %s should be %s\n",
1571                        __location__, nt_errstr(status),
1572                        nt_errstr(NT_STATUS_DOS(ERRDOS, ERRfilexists)));
1573                 goto fail;
1574         }
1575
1576         {
1577                 /* Test an invalid DOS deny mode */
1578                 const char *fname = "test.txt";
1579
1580                 fnum = smbcli_open(cli_nt->tree, fname, O_RDWR | O_CREAT, 5);
1581                 if (fnum != -1) {
1582                         torture_comment(tctx, "Open(%s) with invalid deny mode succeeded -- "
1583                                "expected failure\n", fname);
1584                         smbcli_close(cli_nt->tree, fnum);
1585                         goto fail;
1586                 }
1587                 if (!NT_STATUS_EQUAL(smbcli_nt_error(cli_nt->tree),
1588                                      NT_STATUS_DOS(ERRDOS,ERRbadaccess))) {
1589                         torture_comment(tctx, "Expected DOS error ERRDOS/ERRbadaccess, "
1590                                "got %s\n", smbcli_errstr(cli_nt->tree));
1591                         goto fail;
1592                 }
1593
1594                 fnum = smbcli_open(cli_dos->tree, fname, O_RDWR | O_CREAT, 5);
1595                 if (fnum != -1) {
1596                         torture_comment(tctx, "Open(%s) with invalid deny mode succeeded -- "
1597                                "expected failure\n", fname);
1598                         smbcli_close(cli_nt->tree, fnum);
1599                         goto fail;
1600                 }
1601                 if (!NT_STATUS_EQUAL(smbcli_nt_error(cli_nt->tree),
1602                                      NT_STATUS_DOS(ERRDOS,ERRbadaccess))) {
1603                         torture_comment(tctx, "Expected DOS error ERRDOS:ERRbadaccess, "
1604                                "got %s\n", smbcli_errstr(cli_nt->tree));
1605                         goto fail;
1606                 }
1607         }
1608
1609         {
1610                 /*
1611                  * Samba 3.0.23 has a bug that an existing file can be opened
1612                  * as a directory using ntcreate&x. Test this.
1613                  */
1614
1615                 const char *fname = "\\test_dir.txt";
1616
1617                 fnum = smbcli_open(cli_nt->tree, fname, O_RDWR|O_CREAT,
1618                                    DENY_NONE);
1619                 if (fnum == -1) {
1620                         d_printf("(%s) smbcli_open failed: %s\n", __location__,
1621                                  smbcli_errstr(cli_nt->tree));
1622                 }
1623                 smbcli_close(cli_nt->tree, fnum);
1624
1625                 io.generic.level = RAW_OPEN_NTCREATEX;
1626                 io.ntcreatex.in.root_fid = 0;
1627                 io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
1628                 io.ntcreatex.in.alloc_size = 0;
1629                 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
1630                 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ|
1631                         NTCREATEX_SHARE_ACCESS_WRITE|
1632                         NTCREATEX_SHARE_ACCESS_DELETE;
1633                 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1634                 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1635                 io.ntcreatex.in.impersonation =
1636                         NTCREATEX_IMPERSONATION_ANONYMOUS;
1637                 io.ntcreatex.in.security_flags = 0;
1638                 io.ntcreatex.in.fname = fname;
1639                 io.ntcreatex.in.flags = 0;
1640
1641                 status = smb_raw_open(cli_nt->tree, tctx, &io);
1642                 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_A_DIRECTORY)) {
1643                         torture_comment(tctx, "ntcreate as dir gave %s, "
1644                                         "expected NT_STATUS_NOT_A_DIRECTORY\n",
1645                                         nt_errstr(status));
1646                         result = false;
1647                 }
1648
1649                 if (NT_STATUS_IS_OK(status)) {
1650                         smbcli_close(cli_nt->tree, io.ntcreatex.out.file.fnum);
1651                 }
1652
1653                 status = smb_raw_open(cli_dos->tree, tctx, &io);
1654                 if (!NT_STATUS_EQUAL(status, NT_STATUS_DOS(ERRDOS,
1655                                                            ERRbaddirectory))) {
1656                         torture_comment(tctx, "ntcreate as dir gave %s, "
1657                                         "expected NT_STATUS_NOT_A_DIRECTORY\n",
1658                                         nt_errstr(status));
1659                         result = false;
1660                 }
1661
1662                 if (NT_STATUS_IS_OK(status)) {
1663                         smbcli_close(cli_dos->tree,
1664                                      io.ntcreatex.out.file.fnum);
1665                 }
1666
1667                 smbcli_unlink(cli_nt->tree, fname);
1668         }
1669
1670         if (!torture_setting_bool(tctx, "samba3", false)) {
1671                 goto done;
1672         }
1673
1674         fnum = smbcli_open(cli_dos->tree, os2_fname, 
1675                            O_RDWR | O_CREAT | O_TRUNC,
1676                            DENY_NONE);
1677         if (fnum != -1) {
1678                 torture_comment(tctx, "Open(%s) succeeded -- expected failure\n",
1679                        os2_fname);
1680                 smbcli_close(cli_dos->tree, fnum);
1681                 goto fail;
1682         }
1683
1684         if (!NT_STATUS_EQUAL(smbcli_nt_error(cli_dos->tree),
1685                              NT_STATUS_DOS(ERRDOS, ERRcannotopen))) {
1686                 torture_comment(tctx, "Expected DOS error ERRDOS/ERRcannotopen, got %s\n",
1687                        smbcli_errstr(cli_dos->tree));
1688                 goto fail;
1689         }
1690
1691         fnum = smbcli_open(cli_nt->tree, os2_fname, 
1692                            O_RDWR | O_CREAT | O_TRUNC,
1693                            DENY_NONE);
1694         if (fnum != -1) {
1695                 torture_comment(tctx, "Open(%s) succeeded -- expected failure\n",
1696                        os2_fname);
1697                 smbcli_close(cli_nt->tree, fnum);
1698                 goto fail;
1699         }
1700
1701         if (!NT_STATUS_EQUAL(smbcli_nt_error(cli_nt->tree),
1702                              NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1703                 torture_comment(tctx, "Expected error NT_STATUS_OBJECT_NAME_NOT_FOUND, "
1704                        "got %s\n", smbcli_errstr(cli_nt->tree));
1705                 goto fail;
1706         }
1707
1708  done:
1709         result = true;
1710
1711  fail:
1712         if (cli_dos != NULL) {
1713                 torture_close_connection(cli_dos);
1714         }
1715         if (cli_nt != NULL) {
1716                 torture_close_connection(cli_nt);
1717         }
1718         
1719         return result;
1720 }
1721
1722
1723 NTSTATUS torture_base_init(void)
1724 {
1725         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "BASE");
1726
1727         torture_suite_add_2smb_test(suite, "FDPASS", run_fdpasstest);
1728         torture_suite_add_suite(suite, torture_base_locktest(suite));
1729         torture_suite_add_1smb_test(suite, "UNLINK", torture_unlinktest);
1730         torture_suite_add_1smb_test(suite, "ATTR",   run_attrtest);
1731         torture_suite_add_1smb_test(suite, "TRANS2", run_trans2test);
1732         torture_suite_add_simple_test(suite, "NEGNOWAIT", run_negprot_nowait);
1733         torture_suite_add_1smb_test(suite, "DIR1",  torture_dirtest1);
1734         torture_suite_add_1smb_test(suite, "DIR2",  torture_dirtest2);
1735         torture_suite_add_1smb_test(suite, "DENY1",  torture_denytest1);
1736         torture_suite_add_2smb_test(suite, "DENY2",  torture_denytest2);
1737         torture_suite_add_2smb_test(suite, "DENY3",  torture_denytest3);
1738         torture_suite_add_1smb_test(suite, "DENYDOS",  torture_denydos_sharing);
1739         torture_suite_add_smb_multi_test(suite, "NTDENY1", torture_ntdenytest1);
1740         torture_suite_add_2smb_test(suite, "NTDENY2",  torture_ntdenytest2);
1741         torture_suite_add_1smb_test(suite, "TCON",  run_tcon_test);
1742         torture_suite_add_1smb_test(suite, "TCONDEV",  run_tcon_devtype_test);
1743         torture_suite_add_1smb_test(suite, "VUID", run_vuidtest);
1744         torture_suite_add_2smb_test(suite, "RW1",  run_readwritetest);
1745         torture_suite_add_2smb_test(suite, "OPEN", run_opentest);
1746         torture_suite_add_smb_multi_test(suite, "DEFER_OPEN", run_deferopen);
1747         torture_suite_add_1smb_test(suite, "XCOPY", run_xcopy);
1748         torture_suite_add_1smb_test(suite, "IOMETER", run_iometer);
1749         torture_suite_add_1smb_test(suite, "RENAME", torture_test_rename);
1750         torture_suite_add_suite(suite, torture_test_delete());
1751         torture_suite_add_1smb_test(suite, "PROPERTIES", torture_test_properties);
1752         torture_suite_add_1smb_test(suite, "MANGLE", torture_mangle);
1753         torture_suite_add_1smb_test(suite, "OPENATTR", torture_openattrtest);
1754         torture_suite_add_suite(suite, torture_charset(suite));
1755         torture_suite_add_1smb_test(suite, "CHKPATH",  torture_chkpath_test);
1756         torture_suite_add_1smb_test(suite, "SECLEAK",  torture_sec_leak);
1757         torture_suite_add_simple_test(suite, "DISCONNECT",  torture_disconnect);
1758         torture_suite_add_suite(suite, torture_delay_write());
1759         torture_suite_add_simple_test(suite, "SAMBA3ERROR", torture_samba3_errorpaths);
1760         torture_suite_add_1smb_test(suite, "CASETABLE", torture_casetable);
1761         torture_suite_add_1smb_test(suite, "UTABLE", torture_utable);
1762         torture_suite_add_simple_test(suite, "SMB", torture_smb_scan);
1763         torture_suite_add_suite(suite, torture_trans2_aliases(suite));
1764         torture_suite_add_1smb_test(suite, "TRANS2-SCAN", torture_trans2_scan);
1765         torture_suite_add_1smb_test(suite, "NTTRANS", torture_nttrans_scan);
1766
1767         torture_suite_add_simple_test(suite, "BENCH-HOLDCON", torture_holdcon);
1768         torture_suite_add_simple_test(suite, "BENCH-READWRITE", run_benchrw);
1769         torture_suite_add_smb_multi_test(suite, "BENCH-TORTURE", run_torture);
1770         torture_suite_add_1smb_test(suite, "SCAN-PIPE_NUMBER", run_pipe_number);
1771         torture_suite_add_1smb_test(suite, "SCAN-IOCTL", torture_ioctl_test);
1772         torture_suite_add_smb_multi_test(suite, "SCAN-MAXFID", run_maxfidtest);
1773
1774         suite->description = talloc_strdup(suite, 
1775                                         "Basic SMB tests (imported from the original smbtorture)");
1776
1777         torture_register_suite(suite);
1778
1779         return NT_STATUS_OK;
1780 }