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