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