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