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