r26654: libcli/smb_composite: Rather than specifying each of the gazillion options...
[jelmer/samba4-debian.git] / source / 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
637         if (retries <= 0) {
638                 torture_comment(tctx, "failed to connect\n");
639                 return false;
640         }
641
642         torture_comment(tctx, "Testing deferred open requests.\n");
643
644         while (i < 4) {
645                 int fnum = -1;
646
647                 do {
648                         struct timeval tv;
649                         tv = timeval_current();
650                         fnum = smbcli_nt_create_full(cli->tree, fname, 0, 
651                                                      SEC_RIGHTS_FILE_ALL,
652                                                      FILE_ATTRIBUTE_NORMAL, 
653                                                      NTCREATEX_SHARE_ACCESS_NONE,
654                                                      NTCREATEX_DISP_OPEN_IF, 0, 0);
655                         if (fnum != -1) {
656                                 break;
657                         }
658                         if (NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),NT_STATUS_SHARING_VIOLATION)) {
659                                 double e = timeval_elapsed(&tv);
660                                 if (e < 0.5 || e > 1.5) {
661                                         torture_comment(tctx,"Timing incorrect %.2f violation\n",
662                                                 e);
663                                 }
664                         }
665                 } while (NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),NT_STATUS_SHARING_VIOLATION));
666
667                 if (fnum == -1) {
668                         torture_comment(tctx,"Failed to open %s, error=%s\n", fname, smbcli_errstr(cli->tree));
669                         return false;
670                 }
671
672                 torture_comment(tctx, "pid %u open %d\n", (unsigned)getpid(), i);
673
674                 sleep(10);
675                 i++;
676                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
677                         torture_comment(tctx,"Failed to close %s, error=%s\n", fname, smbcli_errstr(cli->tree));
678                         return false;
679                 }
680                 sleep(2);
681         }
682
683         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
684                 /* All until the last unlink will fail with sharing violation. */
685                 if (!NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),NT_STATUS_SHARING_VIOLATION)) {
686                         torture_comment(tctx, "unlink of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
687                         correct = false;
688                 }
689         }
690
691         torture_comment(tctx, "deferred test finished\n");
692         return correct;
693 }
694
695 /**
696   Try with a wrong vuid and check error message.
697  */
698
699 static bool run_vuidtest(struct torture_context *tctx, 
700                                                  struct smbcli_state *cli)
701 {
702         const char *fname = "\\vuid.tst";
703         int fnum;
704         size_t size;
705         time_t c_time, a_time, m_time;
706
707         uint16_t orig_vuid;
708         NTSTATUS result;
709
710         smbcli_unlink(cli->tree, fname);
711
712         fnum = smbcli_open(cli->tree, fname, 
713                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
714
715         orig_vuid = cli->session->vuid;
716
717         cli->session->vuid += 1234;
718
719         torture_comment(tctx, "Testing qfileinfo with wrong vuid\n");
720         
721         if (NT_STATUS_IS_OK(result = smbcli_qfileinfo(cli->tree, fnum, NULL,
722                                                    &size, &c_time, &a_time,
723                                                    &m_time, NULL, NULL))) {
724                 torture_fail(tctx, "qfileinfo passed with wrong vuid");
725         }
726
727         if (!NT_STATUS_EQUAL(cli->transport->error.e.nt_status, 
728                              NT_STATUS_DOS(ERRSRV, ERRbaduid)) &&
729             !NT_STATUS_EQUAL(cli->transport->error.e.nt_status, 
730                              NT_STATUS_INVALID_HANDLE)) {
731                 torture_fail(tctx, talloc_asprintf(tctx, 
732                                 "qfileinfo should have returned DOS error "
733                        "ERRSRV:ERRbaduid\n  but returned %s",
734                        smbcli_errstr(cli->tree)));
735         }
736
737         cli->session->vuid -= 1234;
738
739         torture_assert_ntstatus_ok(tctx, smbcli_close(cli->tree, fnum),
740                 talloc_asprintf(tctx, "close failed (%s)", smbcli_errstr(cli->tree)));
741
742         smbcli_unlink(cli->tree, fname);
743
744         return true;
745 }
746
747 /*
748   Test open mode returns on read-only files.
749  */
750  static bool run_opentest(struct torture_context *tctx, struct smbcli_state *cli1, 
751                                                   struct smbcli_state *cli2)
752 {
753         const char *fname = "\\readonly.file";
754         char *control_char_fname;
755         int fnum1, fnum2;
756         uint8_t buf[20];
757         size_t fsize;
758         bool correct = true;
759         char *tmp_path;
760         int failures = 0;
761         int i;
762
763         asprintf(&control_char_fname, "\\readonly.afile");
764         for (i = 1; i <= 0x1f; i++) {
765                 control_char_fname[10] = i;
766                 fnum1 = smbcli_nt_create_full(cli1->tree, control_char_fname, 0, SEC_FILE_WRITE_DATA, FILE_ATTRIBUTE_NORMAL,
767                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
768                 
769                 if (!check_error(__location__, cli1, ERRDOS, ERRinvalidname, 
770                                 NT_STATUS_OBJECT_NAME_INVALID)) {
771                         torture_comment(tctx, "Error code should be NT_STATUS_OBJECT_NAME_INVALID, was %s for file with %d char\n",
772                                         smbcli_errstr(cli1->tree), i);
773                         failures++;
774                 }
775
776                 if (fnum1 != -1) {
777                         smbcli_close(cli1->tree, fnum1);
778                 }
779                 smbcli_setatr(cli1->tree, control_char_fname, 0, 0);
780                 smbcli_unlink(cli1->tree, control_char_fname);
781         }
782         free(control_char_fname);
783
784         if (!failures)
785                 torture_comment(tctx, "Create file with control char names passed.\n");
786
787         smbcli_setatr(cli1->tree, fname, 0, 0);
788         smbcli_unlink(cli1->tree, fname);
789         
790         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
791         if (fnum1 == -1) {
792                 torture_comment(tctx, "open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
793                 return false;
794         }
795
796         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
797                 torture_comment(tctx, "close2 failed (%s)\n", smbcli_errstr(cli1->tree));
798                 return false;
799         }
800         
801         if (NT_STATUS_IS_ERR(smbcli_setatr(cli1->tree, fname, FILE_ATTRIBUTE_READONLY, 0))) {
802                 torture_comment(tctx, "smbcli_setatr failed (%s)\n", smbcli_errstr(cli1->tree));
803                 CHECK_MAX_FAILURES(error_test1);
804                 return false;
805         }
806         
807         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_WRITE);
808         if (fnum1 == -1) {
809                 torture_comment(tctx, "open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
810                 CHECK_MAX_FAILURES(error_test1);
811                 return false;
812         }
813         
814         /* This will fail - but the error should be ERRnoaccess, not ERRbadshare. */
815         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_ALL);
816         
817         if (check_error(__location__, cli1, ERRDOS, ERRnoaccess, 
818                         NT_STATUS_ACCESS_DENIED)) {
819                 torture_comment(tctx, "correct error code ERRDOS/ERRnoaccess returned\n");
820         }
821         
822         torture_comment(tctx, "finished open test 1\n");
823 error_test1:
824         smbcli_close(cli1->tree, fnum1);
825         
826         /* Now try not readonly and ensure ERRbadshare is returned. */
827         
828         smbcli_setatr(cli1->tree, fname, 0, 0);
829         
830         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_WRITE);
831         if (fnum1 == -1) {
832                 torture_comment(tctx, "open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
833                 return false;
834         }
835         
836         /* This will fail - but the error should be ERRshare. */
837         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_ALL);
838         
839         if (check_error(__location__, cli1, ERRDOS, ERRbadshare, 
840                         NT_STATUS_SHARING_VIOLATION)) {
841                 torture_comment(tctx, "correct error code ERRDOS/ERRbadshare returned\n");
842         }
843         
844         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
845                 torture_comment(tctx, "close2 failed (%s)\n", smbcli_errstr(cli1->tree));
846                 return false;
847         }
848         
849         smbcli_unlink(cli1->tree, fname);
850         
851         torture_comment(tctx, "finished open test 2\n");
852         
853         /* Test truncate open disposition on file opened for read. */
854         
855         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
856         if (fnum1 == -1) {
857                 torture_comment(tctx, "(3) open (1) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
858                 return false;
859         }
860         
861         /* write 20 bytes. */
862         
863         memset(buf, '\0', 20);
864
865         if (smbcli_write(cli1->tree, fnum1, 0, buf, 0, 20) != 20) {
866                 torture_comment(tctx, "write failed (%s)\n", smbcli_errstr(cli1->tree));
867                 correct = false;
868         }
869
870         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
871                 torture_comment(tctx, "(3) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
872                 return false;
873         }
874         
875         /* Ensure size == 20. */
876         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
877                 torture_comment(tctx, "(3) getatr failed (%s)\n", smbcli_errstr(cli1->tree));
878                 CHECK_MAX_FAILURES(error_test3);
879                 return false;
880         }
881         
882         if (fsize != 20) {
883                 torture_comment(tctx, "(3) file size != 20\n");
884                 CHECK_MAX_FAILURES(error_test3);
885                 return false;
886         }
887
888         /* Now test if we can truncate a file opened for readonly. */
889         
890         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY|O_TRUNC, DENY_NONE);
891         if (fnum1 == -1) {
892                 torture_comment(tctx, "(3) open (2) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
893                 CHECK_MAX_FAILURES(error_test3);
894                 return false;
895         }
896         
897         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
898                 torture_comment(tctx, "close2 failed (%s)\n", smbcli_errstr(cli1->tree));
899                 return false;
900         }
901
902         /* Ensure size == 0. */
903         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
904                 torture_comment(tctx, "(3) getatr failed (%s)\n", smbcli_errstr(cli1->tree));
905                 CHECK_MAX_FAILURES(error_test3);
906                 return false;
907         }
908
909         if (fsize != 0) {
910                 torture_comment(tctx, "(3) file size != 0\n");
911                 CHECK_MAX_FAILURES(error_test3);
912                 return false;
913         }
914         torture_comment(tctx, "finished open test 3\n");
915 error_test3:    
916         smbcli_unlink(cli1->tree, fname);
917
918
919         torture_comment(tctx, "testing ctemp\n");
920         fnum1 = smbcli_ctemp(cli1->tree, "\\", &tmp_path);
921         if (fnum1 == -1) {
922                 torture_comment(tctx, "ctemp failed (%s)\n", smbcli_errstr(cli1->tree));
923                 CHECK_MAX_FAILURES(error_test4);
924                 return false;
925         }
926         torture_comment(tctx, "ctemp gave path %s\n", tmp_path);
927         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
928                 torture_comment(tctx, "close of temp failed (%s)\n", smbcli_errstr(cli1->tree));
929         }
930         if (NT_STATUS_IS_ERR(smbcli_unlink(cli1->tree, tmp_path))) {
931                 torture_comment(tctx, "unlink of temp failed (%s)\n", smbcli_errstr(cli1->tree));
932         }
933 error_test4:    
934         /* Test the non-io opens... */
935
936         smbcli_setatr(cli2->tree, fname, 0, 0);
937         smbcli_unlink(cli2->tree, fname);
938         
939         torture_comment(tctx, "TEST #1 testing 2 non-io opens (no delete)\n");
940         
941         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
942                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
943
944         if (fnum1 == -1) {
945                 torture_comment(tctx, "test 1 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
946                 CHECK_MAX_FAILURES(error_test10);
947                 return false;
948         }
949
950         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
951                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
952         if (fnum2 == -1) {
953                 torture_comment(tctx, "test 1 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
954                 CHECK_MAX_FAILURES(error_test10);
955                 return false;
956         }
957
958         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
959                 torture_comment(tctx, "test 1 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
960                 return false;
961         }
962         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
963                 torture_comment(tctx, "test 1 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
964                 return false;
965         }
966
967         torture_comment(tctx, "non-io open test #1 passed.\n");
968 error_test10:
969         smbcli_unlink(cli1->tree, fname);
970
971         torture_comment(tctx, "TEST #2 testing 2 non-io opens (first with delete)\n");
972         
973         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
974                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
975
976         if (fnum1 == -1) {
977                 torture_comment(tctx, "test 2 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
978                 CHECK_MAX_FAILURES(error_test20);
979                 return false;
980         }
981
982         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
983                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
984
985         if (fnum2 == -1) {
986                 torture_comment(tctx, "test 2 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
987                 CHECK_MAX_FAILURES(error_test20);
988                 return false;
989         }
990
991         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
992                 torture_comment(tctx, "test 1 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
993                 return false;
994         }
995         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
996                 torture_comment(tctx, "test 1 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
997                 return false;
998         }
999
1000         torture_comment(tctx, "non-io open test #2 passed.\n");
1001 error_test20:
1002         smbcli_unlink(cli1->tree, fname);
1003
1004         torture_comment(tctx, "TEST #3 testing 2 non-io opens (second with delete)\n");
1005         
1006         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1007                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1008
1009         if (fnum1 == -1) {
1010                 torture_comment(tctx, "test 3 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1011                 CHECK_MAX_FAILURES(error_test30);
1012                 return false;
1013         }
1014
1015         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1016                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1017
1018         if (fnum2 == -1) {
1019                 torture_comment(tctx, "test 3 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1020                 CHECK_MAX_FAILURES(error_test30);
1021                 return false;
1022         }
1023
1024         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1025                 torture_comment(tctx, "test 3 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1026                 return false;
1027         }
1028         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1029                 torture_comment(tctx, "test 3 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1030                 return false;
1031         }
1032
1033         torture_comment(tctx, "non-io open test #3 passed.\n");
1034 error_test30:
1035         smbcli_unlink(cli1->tree, fname);
1036
1037         torture_comment(tctx, "TEST #4 testing 2 non-io opens (both with delete)\n");
1038         
1039         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1040                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1041
1042         if (fnum1 == -1) {
1043                 torture_comment(tctx, "test 4 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1044                 CHECK_MAX_FAILURES(error_test40);
1045                 return false;
1046         }
1047
1048         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1049                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1050
1051         if (fnum2 != -1) {
1052                 torture_comment(tctx, "test 4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1053                 CHECK_MAX_FAILURES(error_test40);
1054                 return false;
1055         }
1056
1057         torture_comment(tctx, "test 4 open 2 of %s gave %s (correct error should be %s)\n", fname, smbcli_errstr(cli2->tree), "sharing violation");
1058
1059         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1060                 torture_comment(tctx, "test 4 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1061                 return false;
1062         }
1063
1064         torture_comment(tctx, "non-io open test #4 passed.\n");
1065 error_test40:
1066         smbcli_unlink(cli1->tree, fname);
1067
1068         torture_comment(tctx, "TEST #5 testing 2 non-io opens (both with delete - both with file share delete)\n");
1069         
1070         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1071                                    NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1072
1073         if (fnum1 == -1) {
1074                 torture_comment(tctx, "test 5 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1075                 CHECK_MAX_FAILURES(error_test50);
1076                 return false;
1077         }
1078
1079         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1080                                    NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1081
1082         if (fnum2 == -1) {
1083                 torture_comment(tctx, "test 5 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1084                 CHECK_MAX_FAILURES(error_test50);
1085                 return false;
1086         }
1087
1088         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1089                 torture_comment(tctx, "test 5 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1090                 return false;
1091         }
1092
1093         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1094                 torture_comment(tctx, "test 5 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1095                 return false;
1096         }
1097
1098         torture_comment(tctx, "non-io open test #5 passed.\n");
1099 error_test50:
1100         torture_comment(tctx, "TEST #6 testing 1 non-io open, one io open\n");
1101         
1102         smbcli_unlink(cli1->tree, fname);
1103
1104         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
1105                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1106
1107         if (fnum1 == -1) {
1108                 torture_comment(tctx, "test 6 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1109                 CHECK_MAX_FAILURES(error_test60);
1110                 return false;
1111         }
1112
1113         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1114                                    NTCREATEX_SHARE_ACCESS_READ, NTCREATEX_DISP_OPEN_IF, 0, 0);
1115
1116         if (fnum2 == -1) {
1117                 torture_comment(tctx, "test 6 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1118                 CHECK_MAX_FAILURES(error_test60);
1119                 return false;
1120         }
1121
1122         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1123                 torture_comment(tctx, "test 6 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1124                 return false;
1125         }
1126
1127         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1128                 torture_comment(tctx, "test 6 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1129                 return false;
1130         }
1131
1132         torture_comment(tctx, "non-io open test #6 passed.\n");
1133 error_test60:
1134         torture_comment(tctx, "TEST #7 testing 1 non-io open, one io open with delete\n");
1135
1136         smbcli_unlink(cli1->tree, fname);
1137
1138         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
1139                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1140
1141         if (fnum1 == -1) {
1142                 torture_comment(tctx, "test 7 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1143                 CHECK_MAX_FAILURES(error_test70);
1144                 return false;
1145         }
1146
1147         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1148                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1149
1150         if (fnum2 != -1) {
1151                 torture_comment(tctx, "test 7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1152                 CHECK_MAX_FAILURES(error_test70);
1153                 return false;
1154         }
1155
1156         torture_comment(tctx, "test 7 open 2 of %s gave %s (correct error should be %s)\n", fname, smbcli_errstr(cli2->tree), "sharing violation");
1157
1158         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1159                 torture_comment(tctx, "test 7 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1160                 return false;
1161         }
1162
1163         torture_comment(tctx, "non-io open test #7 passed.\n");
1164
1165 error_test70:
1166
1167         torture_comment(tctx, "TEST #8 testing one normal open, followed by lock, followed by open with truncate\n");
1168
1169         smbcli_unlink(cli1->tree, fname);
1170
1171         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1172         if (fnum1 == -1) {
1173                 torture_comment(tctx, "(8) open (1) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1174                 return false;
1175         }
1176         
1177         /* write 20 bytes. */
1178         
1179         memset(buf, '\0', 20);
1180
1181         if (smbcli_write(cli1->tree, fnum1, 0, buf, 0, 20) != 20) {
1182                 torture_comment(tctx, "(8) write failed (%s)\n", smbcli_errstr(cli1->tree));
1183                 correct = false;
1184         }
1185
1186         /* Ensure size == 20. */
1187         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1188                 torture_comment(tctx, "(8) getatr (1) failed (%s)\n", smbcli_errstr(cli1->tree));
1189                 CHECK_MAX_FAILURES(error_test80);
1190                 return false;
1191         }
1192         
1193         if (fsize != 20) {
1194                 torture_comment(tctx, "(8) file size != 20\n");
1195                 CHECK_MAX_FAILURES(error_test80);
1196                 return false;
1197         }
1198
1199         /* Get an exclusive lock on the open file. */
1200         if (NT_STATUS_IS_ERR(smbcli_lock(cli1->tree, fnum1, 0, 4, 0, WRITE_LOCK))) {
1201                 torture_comment(tctx, "(8) lock1 failed (%s)\n", smbcli_errstr(cli1->tree));
1202                 CHECK_MAX_FAILURES(error_test80);
1203                 return false;
1204         }
1205
1206         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR|O_TRUNC, DENY_NONE);
1207         if (fnum1 == -1) {
1208                 torture_comment(tctx, "(8) open (2) of %s with truncate failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1209                 return false;
1210         }
1211
1212         /* Ensure size == 0. */
1213         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1214                 torture_comment(tctx, "(8) getatr (2) failed (%s)\n", smbcli_errstr(cli1->tree));
1215                 CHECK_MAX_FAILURES(error_test80);
1216                 return false;
1217         }
1218         
1219         if (fsize != 0) {
1220                 torture_comment(tctx, "(8) file size != 0\n");
1221                 CHECK_MAX_FAILURES(error_test80);
1222                 return false;
1223         }
1224
1225         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1226                 torture_comment(tctx, "(8) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
1227                 return false;
1228         }
1229         
1230         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum2))) {
1231                 torture_comment(tctx, "(8) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
1232                 return false;
1233         }
1234         
1235 error_test80:
1236
1237         torture_comment(tctx, "open test #8 passed.\n");
1238
1239         smbcli_unlink(cli1->tree, fname);
1240
1241         return correct;
1242 }
1243
1244 /* FIRST_DESIRED_ACCESS   0xf019f */
1245 #define FIRST_DESIRED_ACCESS   SEC_FILE_READ_DATA|SEC_FILE_WRITE_DATA|SEC_FILE_APPEND_DATA|\
1246                                SEC_FILE_READ_EA|                           /* 0xf */ \
1247                                SEC_FILE_WRITE_EA|SEC_FILE_READ_ATTRIBUTE|     /* 0x90 */ \
1248                                SEC_FILE_WRITE_ATTRIBUTE|                  /* 0x100 */ \
1249                                SEC_STD_DELETE|SEC_STD_READ_CONTROL|\
1250                                SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER     /* 0xf0000 */
1251 /* SECOND_DESIRED_ACCESS  0xe0080 */
1252 #define SECOND_DESIRED_ACCESS  SEC_FILE_READ_ATTRIBUTE|                   /* 0x80 */ \
1253                                SEC_STD_READ_CONTROL|SEC_STD_WRITE_DAC|\
1254                                SEC_STD_WRITE_OWNER                      /* 0xe0000 */
1255
1256 #if 0
1257 #define THIRD_DESIRED_ACCESS   FILE_READ_ATTRIBUTE|                   /* 0x80 */ \
1258                                READ_CONTROL|WRITE_DAC|\
1259                                SEC_FILE_READ_DATA|\
1260                                WRITE_OWNER                      /* */
1261 #endif
1262
1263
1264
1265 /**
1266   Test ntcreate calls made by xcopy
1267  */
1268 static bool run_xcopy(struct torture_context *tctx,
1269                                           struct smbcli_state *cli1)
1270 {
1271         const char *fname = "\\test.txt";
1272         int fnum1, fnum2;
1273
1274         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0,
1275                                       FIRST_DESIRED_ACCESS, 
1276                                       FILE_ATTRIBUTE_ARCHIVE,
1277                                       NTCREATEX_SHARE_ACCESS_NONE, 
1278                                       NTCREATEX_DISP_OVERWRITE_IF, 
1279                                       0x4044, 0);
1280
1281         torture_assert(tctx, fnum1 != -1, talloc_asprintf(tctx, 
1282                                 "First open failed - %s", smbcli_errstr(cli1->tree)));
1283
1284         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0,
1285                                    SECOND_DESIRED_ACCESS, 0,
1286                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN, 
1287                                    0x200000, 0);
1288         torture_assert(tctx, fnum2 != -1, talloc_asprintf(tctx, 
1289                                 "second open failed - %s", smbcli_errstr(cli1->tree)));
1290         
1291         return true;
1292 }
1293
1294 static bool run_iometer(struct torture_context *tctx,
1295                                                 struct smbcli_state *cli)
1296 {
1297         const char *fname = "\\iobw.tst";
1298         int fnum;
1299         size_t filesize;
1300         NTSTATUS status;
1301         char buf[2048];
1302         int ops;
1303
1304         memset(buf, 0, sizeof(buf));
1305
1306         status = smbcli_getatr(cli->tree, fname, NULL, &filesize, NULL);
1307         torture_assert_ntstatus_ok(tctx, status, 
1308                 talloc_asprintf(tctx, "smbcli_getatr failed: %s", nt_errstr(status)));
1309
1310         torture_comment(tctx, "size: %d\n", (int)filesize);
1311
1312         filesize -= (sizeof(buf) - 1);
1313
1314         fnum = smbcli_nt_create_full(cli->tree, fname, 0x16,
1315                                      0x2019f, 0, 0x3, 3, 0x42, 0x3);
1316         torture_assert(tctx, fnum != -1, talloc_asprintf(tctx, "open failed: %s", 
1317                                    smbcli_errstr(cli->tree)));
1318
1319         ops = 0;
1320
1321         while (true) {
1322                 int i, num_reads, num_writes;
1323
1324                 num_reads = random() % 10;
1325                 num_writes = random() % 3;
1326
1327                 for (i=0; i<num_reads; i++) {
1328                         ssize_t res;
1329                         if (ops++ > torture_numops) {
1330                                 return true;
1331                         }
1332                         res = smbcli_read(cli->tree, fnum, buf,
1333                                           random() % filesize, sizeof(buf));
1334                         torture_assert(tctx, res == sizeof(buf), 
1335                                                    talloc_asprintf(tctx, "read failed: %s",
1336                                                                                    smbcli_errstr(cli->tree)));
1337                 }
1338                 for (i=0; i<num_writes; i++) {
1339                         ssize_t res;
1340                         if (ops++ > torture_numops) {
1341                                 return true;
1342                         }
1343                         res = smbcli_write(cli->tree, fnum, 0, buf,
1344                                           random() % filesize, sizeof(buf));
1345                         torture_assert(tctx, res == sizeof(buf),
1346                                                    talloc_asprintf(tctx, "read failed: %s",
1347                                        smbcli_errstr(cli->tree)));
1348                 }
1349         }
1350
1351         return true;
1352 }
1353
1354 /**
1355   tries variants of chkpath
1356  */
1357 static bool torture_chkpath_test(struct torture_context *tctx, 
1358                                                                  struct smbcli_state *cli)
1359 {
1360         int fnum;
1361         bool ret;
1362
1363         torture_comment(tctx, "Testing valid and invalid paths\n");
1364
1365         /* cleanup from an old run */
1366         smbcli_rmdir(cli->tree, "\\chkpath.dir\\dir2");
1367         smbcli_unlink(cli->tree, "\\chkpath.dir\\*");
1368         smbcli_rmdir(cli->tree, "\\chkpath.dir");
1369
1370         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\chkpath.dir"))) {
1371                 torture_comment(tctx, "mkdir1 failed : %s\n", smbcli_errstr(cli->tree));
1372                 return false;
1373         }
1374
1375         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\chkpath.dir\\dir2"))) {
1376                 torture_comment(tctx, "mkdir2 failed : %s\n", smbcli_errstr(cli->tree));
1377                 return false;
1378         }
1379
1380         fnum = smbcli_open(cli->tree, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1381         if (fnum == -1) {
1382                 torture_comment(tctx, "open1 failed (%s)\n", smbcli_errstr(cli->tree));
1383                 return false;
1384         }
1385         smbcli_close(cli->tree, fnum);
1386
1387         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir"))) {
1388                 torture_comment(tctx, "chkpath1 failed: %s\n", smbcli_errstr(cli->tree));
1389                 ret = false;
1390         }
1391
1392         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\dir2"))) {
1393                 torture_comment(tctx, "chkpath2 failed: %s\n", smbcli_errstr(cli->tree));
1394                 ret = false;
1395         }
1396
1397         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\foo.txt"))) {
1398                 ret = check_error(__location__, cli, ERRDOS, ERRbadpath, 
1399                                   NT_STATUS_NOT_A_DIRECTORY);
1400         } else {
1401                 torture_comment(tctx, "* chkpath on a file should fail\n");
1402                 ret = false;
1403         }
1404
1405         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\bar.txt"))) {
1406                 ret = check_error(__location__, cli, ERRDOS, ERRbadpath, 
1407                                   NT_STATUS_OBJECT_NAME_NOT_FOUND);
1408         } else {
1409                 torture_comment(tctx, "* chkpath on a non existent file should fail\n");
1410                 ret = false;
1411         }
1412
1413         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\dirxx\\bar.txt"))) {
1414                 ret = check_error(__location__, cli, ERRDOS, ERRbadpath, 
1415                                   NT_STATUS_OBJECT_PATH_NOT_FOUND);
1416         } else {
1417                 torture_comment(tctx, "* chkpath on a non existent component should fail\n");
1418                 ret = false;
1419         }
1420
1421         smbcli_rmdir(cli->tree, "\\chkpath.dir\\dir2");
1422         smbcli_unlink(cli->tree, "\\chkpath.dir\\*");
1423         smbcli_rmdir(cli->tree, "\\chkpath.dir");
1424
1425         return ret;
1426 }
1427
1428 /*
1429  * This is a test to excercise some weird Samba3 error paths.
1430  */
1431
1432 static bool torture_samba3_errorpaths(struct torture_context *tctx)
1433 {
1434         bool nt_status_support;
1435         struct smbcli_state *cli_nt = NULL, *cli_dos = NULL;
1436         bool result = false;
1437         int fnum;
1438         const char *os2_fname = ".+,;=[].";
1439         const char *dname = "samba3_errordir";
1440         union smb_open io;
1441         TALLOC_CTX *mem_ctx = talloc_init("samba3_errorpaths");
1442         NTSTATUS status;
1443
1444         if (mem_ctx == NULL) {
1445                 torture_comment(tctx, "talloc_init failed\n");
1446                 return false;
1447         }
1448
1449         nt_status_support = lp_nt_status_support(tctx->lp_ctx);
1450
1451         if (!lp_set_cmdline(tctx->lp_ctx, "nt status support", "yes")) {
1452                 torture_comment(tctx, "Could not set 'nt status support = yes'\n");
1453                 goto fail;
1454         }
1455
1456         if (!torture_open_connection(&cli_nt, tctx, 0)) {
1457                 goto fail;
1458         }
1459
1460         if (!lp_set_cmdline(tctx->lp_ctx, "nt status support", "no")) {
1461                 torture_comment(tctx, "Could not set 'nt status support = yes'\n");
1462                 goto fail;
1463         }
1464
1465         if (!torture_open_connection(&cli_dos, tctx, 1)) {
1466                 goto fail;
1467         }
1468
1469         if (!lp_set_cmdline(tctx->lp_ctx, "nt status support",
1470                             nt_status_support ? "yes":"no")) {
1471                 torture_comment(tctx, "Could not reset 'nt status support = yes'");
1472                 goto fail;
1473         }
1474
1475         smbcli_unlink(cli_nt->tree, os2_fname);
1476         smbcli_rmdir(cli_nt->tree, dname);
1477
1478         if (!NT_STATUS_IS_OK(smbcli_mkdir(cli_nt->tree, dname))) {
1479                 torture_comment(tctx, "smbcli_mkdir(%s) failed: %s\n", dname,
1480                        smbcli_errstr(cli_nt->tree));
1481                 goto fail;
1482         }
1483
1484         io.generic.level = RAW_OPEN_NTCREATEX;
1485         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
1486         io.ntcreatex.in.root_fid = 0;
1487         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
1488         io.ntcreatex.in.alloc_size = 1024*1024;
1489         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
1490         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
1491         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
1492         io.ntcreatex.in.create_options = 0;
1493         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1494         io.ntcreatex.in.security_flags = 0;
1495         io.ntcreatex.in.fname = dname;
1496
1497         status = smb_raw_open(cli_nt->tree, mem_ctx, &io);
1498         if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1499                 torture_comment(tctx, "(%s) incorrect status %s should be %s\n",
1500                        __location__, nt_errstr(status),
1501                        nt_errstr(NT_STATUS_OBJECT_NAME_COLLISION));
1502                 goto fail;
1503         }
1504         status = smb_raw_open(cli_dos->tree, mem_ctx, &io);
1505         if (!NT_STATUS_EQUAL(status, NT_STATUS_DOS(ERRDOS, ERRfilexists))) {
1506                 torture_comment(tctx, "(%s) incorrect status %s should be %s\n",
1507                        __location__, nt_errstr(status),
1508                        nt_errstr(NT_STATUS_DOS(ERRDOS, ERRfilexists)));
1509                 goto fail;
1510         }
1511
1512         status = smbcli_mkdir(cli_nt->tree, dname);
1513         if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1514                 torture_comment(tctx, "(%s) incorrect status %s should be %s\n",
1515                        __location__, nt_errstr(status),
1516                        nt_errstr(NT_STATUS_OBJECT_NAME_COLLISION));
1517                 goto fail;
1518         }
1519         status = smbcli_mkdir(cli_dos->tree, dname);
1520         if (!NT_STATUS_EQUAL(status, NT_STATUS_DOS(ERRDOS, ERRnoaccess))) {
1521                 torture_comment(tctx, "(%s) incorrect status %s should be %s\n",
1522                        __location__, nt_errstr(status),
1523                        nt_errstr(NT_STATUS_DOS(ERRDOS, ERRnoaccess)));
1524                 goto fail;
1525         }
1526
1527         {
1528                 union smb_mkdir md;
1529                 md.t2mkdir.level = RAW_MKDIR_T2MKDIR;
1530                 md.t2mkdir.in.path = dname;
1531                 md.t2mkdir.in.num_eas = 0;
1532                 md.t2mkdir.in.eas = NULL;
1533
1534                 status = smb_raw_mkdir(cli_nt->tree, &md);
1535                 if (!NT_STATUS_EQUAL(status,
1536                                      NT_STATUS_OBJECT_NAME_COLLISION)) {
1537                         torture_comment(
1538                                 tctx, "(%s) incorrect status %s should be "
1539                                 "NT_STATUS_OBJECT_NAME_COLLISION\n",
1540                                 __location__, nt_errstr(status));
1541                         goto fail;
1542                 }
1543                 status = smb_raw_mkdir(cli_dos->tree, &md);
1544                 if (!NT_STATUS_EQUAL(status,
1545                                      NT_STATUS_DOS(ERRDOS, ERRrename))) {
1546                         torture_comment(tctx, "(%s) incorrect status %s "
1547                                         "should be ERRDOS:ERRrename\n",
1548                                         __location__, nt_errstr(status));
1549                         goto fail;
1550                 }
1551         }
1552
1553         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1554         status = smb_raw_open(cli_nt->tree, mem_ctx, &io);
1555         if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1556                 torture_comment(tctx, "(%s) incorrect status %s should be %s\n",
1557                        __location__, nt_errstr(status),
1558                        nt_errstr(NT_STATUS_OBJECT_NAME_COLLISION));
1559                 goto fail;
1560         }
1561
1562         status = smb_raw_open(cli_dos->tree, mem_ctx, &io);
1563         if (!NT_STATUS_EQUAL(status, NT_STATUS_DOS(ERRDOS, ERRfilexists))) {
1564                 torture_comment(tctx, "(%s) incorrect status %s should be %s\n",
1565                        __location__, nt_errstr(status),
1566                        nt_errstr(NT_STATUS_DOS(ERRDOS, ERRfilexists)));
1567                 goto fail;
1568         }
1569
1570         {
1571                 /* Test an invalid DOS deny mode */
1572                 const char *fname = "test.txt";
1573
1574                 fnum = smbcli_open(cli_nt->tree, fname, O_RDWR | O_CREAT, 5);
1575                 if (fnum != -1) {
1576                         torture_comment(tctx, "Open(%s) with invalid deny mode succeeded -- "
1577                                "expected failure\n", fname);
1578                         smbcli_close(cli_nt->tree, fnum);
1579                         goto fail;
1580                 }
1581                 if (!NT_STATUS_EQUAL(smbcli_nt_error(cli_nt->tree),
1582                                      NT_STATUS_DOS(ERRDOS,ERRbadaccess))) {
1583                         torture_comment(tctx, "Expected DOS error ERRDOS/ERRbadaccess, "
1584                                "got %s\n", smbcli_errstr(cli_nt->tree));
1585                         goto fail;
1586                 }
1587
1588                 fnum = smbcli_open(cli_dos->tree, fname, O_RDWR | O_CREAT, 5);
1589                 if (fnum != -1) {
1590                         torture_comment(tctx, "Open(%s) with invalid deny mode succeeded -- "
1591                                "expected failure\n", fname);
1592                         smbcli_close(cli_nt->tree, fnum);
1593                         goto fail;
1594                 }
1595                 if (!NT_STATUS_EQUAL(smbcli_nt_error(cli_nt->tree),
1596                                      NT_STATUS_DOS(ERRDOS,ERRbadaccess))) {
1597                         torture_comment(tctx, "Expected DOS error ERRDOS:ERRbadaccess, "
1598                                "got %s\n", smbcli_errstr(cli_nt->tree));
1599                         goto fail;
1600                 }
1601         }
1602
1603         {
1604                 /*
1605                  * Samba 3.0.23 has a bug that an existing file can be opened
1606                  * as a directory using ntcreate&x. Test this.
1607                  */
1608
1609                 const char *fname = "\\test_dir.txt";
1610
1611                 fnum = smbcli_open(cli_nt->tree, fname, O_RDWR|O_CREAT,
1612                                    DENY_NONE);
1613                 if (fnum == -1) {
1614                         d_printf("(%s) smbcli_open failed: %s\n", __location__,
1615                                  smbcli_errstr(cli_nt->tree));
1616                 }
1617                 smbcli_close(cli_nt->tree, fnum);
1618
1619                 io.generic.level = RAW_OPEN_NTCREATEX;
1620                 io.ntcreatex.in.root_fid = 0;
1621                 io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
1622                 io.ntcreatex.in.alloc_size = 0;
1623                 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
1624                 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ|
1625                         NTCREATEX_SHARE_ACCESS_WRITE|
1626                         NTCREATEX_SHARE_ACCESS_DELETE;
1627                 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1628                 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1629                 io.ntcreatex.in.impersonation =
1630                         NTCREATEX_IMPERSONATION_ANONYMOUS;
1631                 io.ntcreatex.in.security_flags = 0;
1632                 io.ntcreatex.in.fname = fname;
1633                 io.ntcreatex.in.flags = 0;
1634
1635                 status = smb_raw_open(cli_nt->tree, mem_ctx, &io);
1636                 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_A_DIRECTORY)) {
1637                         torture_comment(tctx, "ntcreate as dir gave %s, "
1638                                         "expected NT_STATUS_NOT_A_DIRECTORY\n",
1639                                         nt_errstr(status));
1640                         result = false;
1641                 }
1642
1643                 if (NT_STATUS_IS_OK(status)) {
1644                         smbcli_close(cli_nt->tree, io.ntcreatex.out.file.fnum);
1645                 }
1646
1647                 status = smb_raw_open(cli_dos->tree, mem_ctx, &io);
1648                 if (!NT_STATUS_EQUAL(status, NT_STATUS_DOS(ERRDOS,
1649                                                            ERRbaddirectory))) {
1650                         torture_comment(tctx, "ntcreate as dir gave %s, "
1651                                         "expected NT_STATUS_NOT_A_DIRECTORY\n",
1652                                         nt_errstr(status));
1653                         result = false;
1654                 }
1655
1656                 if (NT_STATUS_IS_OK(status)) {
1657                         smbcli_close(cli_dos->tree,
1658                                      io.ntcreatex.out.file.fnum);
1659                 }
1660
1661                 smbcli_unlink(cli_nt->tree, fname);
1662         }
1663
1664         if (!torture_setting_bool(tctx, "samba3", false)) {
1665                 goto done;
1666         }
1667
1668         fnum = smbcli_open(cli_dos->tree, os2_fname, 
1669                            O_RDWR | O_CREAT | O_TRUNC,
1670                            DENY_NONE);
1671         if (fnum != -1) {
1672                 torture_comment(tctx, "Open(%s) succeeded -- expected failure\n",
1673                        os2_fname);
1674                 smbcli_close(cli_dos->tree, fnum);
1675                 goto fail;
1676         }
1677
1678         if (!NT_STATUS_EQUAL(smbcli_nt_error(cli_dos->tree),
1679                              NT_STATUS_DOS(ERRDOS, ERRcannotopen))) {
1680                 torture_comment(tctx, "Expected DOS error ERRDOS/ERRcannotopen, got %s\n",
1681                        smbcli_errstr(cli_dos->tree));
1682                 goto fail;
1683         }
1684
1685         fnum = smbcli_open(cli_nt->tree, os2_fname, 
1686                            O_RDWR | O_CREAT | O_TRUNC,
1687                            DENY_NONE);
1688         if (fnum != -1) {
1689                 torture_comment(tctx, "Open(%s) succeeded -- expected failure\n",
1690                        os2_fname);
1691                 smbcli_close(cli_nt->tree, fnum);
1692                 goto fail;
1693         }
1694
1695         if (!NT_STATUS_EQUAL(smbcli_nt_error(cli_nt->tree),
1696                              NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1697                 torture_comment(tctx, "Expected error NT_STATUS_OBJECT_NAME_NOT_FOUND, "
1698                        "got %s\n", smbcli_errstr(cli_nt->tree));
1699                 goto fail;
1700         }
1701
1702  done:
1703         result = true;
1704
1705  fail:
1706         if (cli_dos != NULL) {
1707                 torture_close_connection(cli_dos);
1708         }
1709         if (cli_nt != NULL) {
1710                 torture_close_connection(cli_nt);
1711         }
1712         
1713         return result;
1714 }
1715
1716
1717 NTSTATUS torture_base_init(void)
1718 {
1719         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "BASE");
1720
1721         torture_suite_add_2smb_test(suite, "FDPASS", run_fdpasstest);
1722         torture_suite_add_suite(suite, torture_base_locktest(suite));
1723         torture_suite_add_1smb_test(suite, "UNLINK", torture_unlinktest);
1724         torture_suite_add_1smb_test(suite, "ATTR",   run_attrtest);
1725         torture_suite_add_1smb_test(suite, "TRANS2", run_trans2test);
1726         torture_suite_add_simple_test(suite, "NEGNOWAIT", run_negprot_nowait);
1727         torture_suite_add_1smb_test(suite, "DIR1",  torture_dirtest1);
1728         torture_suite_add_1smb_test(suite, "DIR2",  torture_dirtest2);
1729         torture_suite_add_1smb_test(suite, "DENY1",  torture_denytest1);
1730         torture_suite_add_2smb_test(suite, "DENY2",  torture_denytest2);
1731         torture_suite_add_2smb_test(suite, "DENY3",  torture_denytest3);
1732         torture_suite_add_1smb_test(suite, "DENYDOS",  torture_denydos_sharing);
1733         torture_suite_add_smb_multi_test(suite, "NTDENY1", torture_ntdenytest1);
1734         torture_suite_add_2smb_test(suite, "NTDENY2",  torture_ntdenytest2);
1735         torture_suite_add_1smb_test(suite, "TCON",  run_tcon_test);
1736         torture_suite_add_1smb_test(suite, "TCONDEV",  run_tcon_devtype_test);
1737         torture_suite_add_1smb_test(suite, "VUID", run_vuidtest);
1738         torture_suite_add_2smb_test(suite, "RW1",  run_readwritetest);
1739         torture_suite_add_2smb_test(suite, "OPEN", run_opentest);
1740         torture_suite_add_smb_multi_test(suite, "DEFER_OPEN", run_deferopen);
1741         torture_suite_add_1smb_test(suite, "XCOPY", run_xcopy);
1742         torture_suite_add_1smb_test(suite, "IOMETER", run_iometer);
1743         torture_suite_add_1smb_test(suite, "RENAME", torture_test_rename);
1744         torture_suite_add_suite(suite, torture_test_delete());
1745         torture_suite_add_1smb_test(suite, "PROPERTIES", torture_test_properties);
1746         torture_suite_add_1smb_test(suite, "MANGLE", torture_mangle);
1747         torture_suite_add_1smb_test(suite, "OPENATTR", torture_openattrtest);
1748         torture_suite_add_suite(suite, torture_charset(suite));
1749         torture_suite_add_1smb_test(suite, "CHKPATH",  torture_chkpath_test);
1750         torture_suite_add_1smb_test(suite, "SECLEAK",  torture_sec_leak);
1751         torture_suite_add_simple_test(suite, "DISCONNECT",  torture_disconnect);
1752         torture_suite_add_suite(suite, torture_delay_write());
1753         torture_suite_add_simple_test(suite, "SAMBA3ERROR", torture_samba3_errorpaths);
1754         torture_suite_add_1smb_test(suite, "CASETABLE", torture_casetable);
1755         torture_suite_add_1smb_test(suite, "UTABLE", torture_utable);
1756         torture_suite_add_simple_test(suite, "SMB", torture_smb_scan);
1757         torture_suite_add_suite(suite, torture_trans2_aliases(suite));
1758         torture_suite_add_1smb_test(suite, "TRANS2-SCAN", torture_trans2_scan);
1759         torture_suite_add_1smb_test(suite, "NTTRANS", torture_nttrans_scan);
1760
1761         torture_suite_add_simple_test(suite, "BENCH-HOLDCON", torture_holdcon);
1762         torture_suite_add_simple_test(suite, "BENCH-READWRITE", run_benchrw);
1763         torture_suite_add_smb_multi_test(suite, "BENCH-TORTURE", run_torture);
1764         torture_suite_add_1smb_test(suite, "SCAN-PIPE_NUMBER", run_pipe_number);
1765         torture_suite_add_1smb_test(suite, "SCAN-IOCTL", torture_ioctl_test);
1766         torture_suite_add_smb_multi_test(suite, "SCAN-MAXFID", run_maxfidtest);
1767
1768         suite->description = talloc_strdup(suite, 
1769                                         "Basic SMB tests (imported from the original smbtorture)");
1770
1771         torture_register_suite(suite);
1772
1773         return NT_STATUS_OK;
1774 }