Merge commit 'release-4-0-0alpha15' into master4-tmp
[nivanova/samba-autobuild/.git] / source4 / torture / raw / open.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RAW_OPEN_* individual test suite
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "libcli/raw/libcliraw.h"
22 #include "system/time.h"
23 #include "system/filesys.h"
24 #include "lib/events/events.h"
25 #include "libcli/libcli.h"
26 #include "torture/util.h"
27 #include "torture/raw/proto.h"
28
29 /* enum for whether reads/writes are possible on a file */
30 enum rdwr_mode {RDWR_NONE, RDWR_RDONLY, RDWR_WRONLY, RDWR_RDWR};
31
32 #define BASEDIR "\\rawopen"
33
34 /*
35   check if a open file can be read/written
36 */
37 static enum rdwr_mode check_rdwr(struct smbcli_tree *tree, int fnum)
38 {
39         uint8_t c = 1;
40         bool can_read  = (smbcli_read(tree, fnum, &c, 0, 1) == 1);
41         bool can_write = (smbcli_write(tree, fnum, 0, &c, 0, 1) == 1);
42         if ( can_read &&  can_write) return RDWR_RDWR;
43         if ( can_read && !can_write) return RDWR_RDONLY;
44         if (!can_read &&  can_write) return RDWR_WRONLY;
45         return RDWR_NONE;
46 }
47
48 /*
49   describe a RDWR mode as a string
50 */
51 static const char *rdwr_string(enum rdwr_mode m)
52 {
53         switch (m) {
54         case RDWR_NONE: return "NONE";
55         case RDWR_RDONLY: return "RDONLY";
56         case RDWR_WRONLY: return "WRONLY";
57         case RDWR_RDWR: return "RDWR";
58         }
59         return "-";
60 }
61
62 #define CHECK_STATUS(status, correct) do { \
63         if (!NT_STATUS_EQUAL(status, correct)) { \
64                 torture_result(tctx, TORTURE_FAIL, \
65                         "(%s) Incorrect status %s - should be %s\n", \
66                        __location__, nt_errstr(status), nt_errstr(correct)); \
67                 ret = false; \
68                 goto done; \
69         }} while (0)
70
71 #define CREATE_FILE do { \
72         fnum = create_complex_file(cli, tctx, fname); \
73         if (fnum == -1) { \
74                 torture_result(tctx, TORTURE_FAIL, \
75                         "(%s) Failed to create %s - %s\n", \
76                          __location__, fname, smbcli_errstr(cli->tree)); \
77                 ret = false; \
78                 goto done; \
79         }} while (0)
80
81 #define CHECK_RDWR(fnum, correct) do { \
82         enum rdwr_mode m = check_rdwr(cli->tree, fnum); \
83         if (m != correct) { \
84                 torture_result(tctx, TORTURE_FAIL, \
85                        "(%s) Incorrect readwrite mode %s - expected %s\n", \
86                        __location__, rdwr_string(m), rdwr_string(correct)); \
87                 ret = false; \
88         }} while (0)
89
90 #define CHECK_TIME(t, field) do { \
91         time_t t1, t2; \
92         finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
93         finfo.all_info.in.file.path = fname; \
94         status = smb_raw_pathinfo(cli->tree, tctx, &finfo); \
95         CHECK_STATUS(status, NT_STATUS_OK); \
96         t1 = t & ~1; \
97         t2 = nt_time_to_unix(finfo.all_info.out.field) & ~1; \
98         if (abs(t1-t2) > 2) { \
99                 torture_result(tctx, TORTURE_FAIL, \
100                        "(%s) wrong time for field %s  %s - %s\n", \
101                        __location__, #field, \
102                        timestring(tctx, t1), \
103                        timestring(tctx, t2)); \
104                 dump_all_info(tctx, &finfo); \
105                 ret = false; \
106         }} while (0)
107
108 #define CHECK_NTTIME(t, field) do { \
109         NTTIME t2; \
110         finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
111         finfo.all_info.in.file.path = fname; \
112         status = smb_raw_pathinfo(cli->tree, tctx, &finfo); \
113         CHECK_STATUS(status, NT_STATUS_OK); \
114         t2 = finfo.all_info.out.field; \
115         if (t != t2) { \
116                 torture_result(tctx, TORTURE_FAIL, \
117                        "(%s) wrong time for field %s  %s - %s\n", \
118                        __location__, #field, \
119                        nt_time_string(tctx, t), \
120                        nt_time_string(tctx, t2)); \
121                 dump_all_info(tctx, &finfo); \
122                 ret = false; \
123         }} while (0)
124
125 #define CHECK_ALL_INFO(v, field) do { \
126         finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
127         finfo.all_info.in.file.path = fname; \
128         status = smb_raw_pathinfo(cli->tree, tctx, &finfo); \
129         CHECK_STATUS(status, NT_STATUS_OK); \
130         if ((v) != (finfo.all_info.out.field)) { \
131                 torture_result(tctx, TORTURE_FAIL, \
132                        "(%s) wrong value for field %s  0x%x - 0x%x\n", \
133                        __location__, #field, (unsigned int)(v), (unsigned int)(finfo.all_info.out.field)); \
134                 dump_all_info(tctx, &finfo); \
135                 ret = false; \
136         }} while (0)
137
138 #define CHECK_VAL(v, correct) do { \
139         if ((v) != (correct)) { \
140                 torture_result(tctx, TORTURE_FAIL, \
141                        "(%s) wrong value for %s  0x%x - should be 0x%x\n", \
142                        __location__, #v, (unsigned int)(v), (unsigned int)(correct)); \
143                 ret = false; \
144         }} while (0)
145
146 #define SET_ATTRIB(sattrib) do { \
147         union smb_setfileinfo sfinfo; \
148         ZERO_STRUCT(sfinfo.basic_info.in); \
149         sfinfo.basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION; \
150         sfinfo.basic_info.in.file.path = fname; \
151         sfinfo.basic_info.in.attrib = sattrib; \
152         status = smb_raw_setpathinfo(cli->tree, &sfinfo); \
153         if (!NT_STATUS_IS_OK(status)) { \
154                 torture_warning(tctx, "(%s) Failed to set attrib 0x%x on %s\n", \
155                        __location__, (unsigned int)(sattrib), fname); \
156         }} while (0)
157
158 /*
159   test RAW_OPEN_OPEN
160 */
161 static bool test_open(struct torture_context *tctx, struct smbcli_state *cli)
162 {
163         union smb_open io;
164         union smb_fileinfo finfo;
165         const char *fname = BASEDIR "\\torture_open.txt";
166         NTSTATUS status;
167         int fnum = -1, fnum2;
168         bool ret = true;
169
170         if (!torture_setup_dir(cli, BASEDIR)) {
171                 return false;
172         }
173
174         io.openold.level = RAW_OPEN_OPEN;
175         io.openold.in.fname = fname;
176         io.openold.in.open_mode = OPEN_FLAGS_FCB;
177         io.openold.in.search_attrs = 0;
178         status = smb_raw_open(cli->tree, tctx, &io);
179         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
180         fnum = io.openold.out.file.fnum;
181
182         smbcli_unlink(cli->tree, fname);
183         CREATE_FILE;
184         smbcli_close(cli->tree, fnum);
185
186         status = smb_raw_open(cli->tree, tctx, &io);
187         CHECK_STATUS(status, NT_STATUS_OK);
188         fnum = io.openold.out.file.fnum;
189         CHECK_RDWR(fnum, RDWR_RDWR);
190
191         status = smb_raw_open(cli->tree, tctx, &io);
192         CHECK_STATUS(status, NT_STATUS_OK);
193         fnum2 = io.openold.out.file.fnum;
194         CHECK_RDWR(fnum2, RDWR_RDWR);
195         smbcli_close(cli->tree, fnum2);
196         smbcli_close(cli->tree, fnum);
197
198         /* check the read/write modes */
199         io.openold.level = RAW_OPEN_OPEN;
200         io.openold.in.fname = fname;
201         io.openold.in.search_attrs = 0;
202
203         io.openold.in.open_mode = OPEN_FLAGS_OPEN_READ;
204         status = smb_raw_open(cli->tree, tctx, &io);
205         CHECK_STATUS(status, NT_STATUS_OK);
206         fnum = io.openold.out.file.fnum;
207         CHECK_RDWR(fnum, RDWR_RDONLY);
208         smbcli_close(cli->tree, fnum);
209
210         io.openold.in.open_mode = OPEN_FLAGS_OPEN_WRITE;
211         status = smb_raw_open(cli->tree, tctx, &io);
212         CHECK_STATUS(status, NT_STATUS_OK);
213         fnum = io.openold.out.file.fnum;
214         CHECK_RDWR(fnum, RDWR_WRONLY);
215         smbcli_close(cli->tree, fnum);
216
217         io.openold.in.open_mode = OPEN_FLAGS_OPEN_RDWR;
218         status = smb_raw_open(cli->tree, tctx, &io);
219         CHECK_STATUS(status, NT_STATUS_OK);
220         fnum = io.openold.out.file.fnum;
221         CHECK_RDWR(fnum, RDWR_RDWR);
222         smbcli_close(cli->tree, fnum);
223
224         /* check the share modes roughly - not a complete matrix */
225         io.openold.in.open_mode = OPEN_FLAGS_OPEN_RDWR | OPEN_FLAGS_DENY_WRITE;
226         status = smb_raw_open(cli->tree, tctx, &io);
227         CHECK_STATUS(status, NT_STATUS_OK);
228         fnum = io.openold.out.file.fnum;
229         CHECK_RDWR(fnum, RDWR_RDWR);
230         
231         if (io.openold.in.open_mode != io.openold.out.rmode) {
232                 torture_warning(tctx, "(%s) rmode should equal open_mode - 0x%x 0x%x\n",
233                        __location__, io.openold.out.rmode, io.openold.in.open_mode);
234         }
235
236         io.openold.in.open_mode = OPEN_FLAGS_OPEN_RDWR | OPEN_FLAGS_DENY_NONE;
237         status = smb_raw_open(cli->tree, tctx, &io);
238         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
239
240         io.openold.in.open_mode = OPEN_FLAGS_OPEN_READ | OPEN_FLAGS_DENY_NONE;
241         status = smb_raw_open(cli->tree, tctx, &io);
242         CHECK_STATUS(status, NT_STATUS_OK);
243         fnum2 = io.openold.out.file.fnum;
244         CHECK_RDWR(fnum2, RDWR_RDONLY);
245         smbcli_close(cli->tree, fnum);
246         smbcli_close(cli->tree, fnum2);
247
248
249         /* check the returned write time */
250         io.openold.level = RAW_OPEN_OPEN;
251         io.openold.in.fname = fname;
252         io.openold.in.search_attrs = 0;
253         io.openold.in.open_mode = OPEN_FLAGS_OPEN_READ;
254         status = smb_raw_open(cli->tree, tctx, &io);
255         CHECK_STATUS(status, NT_STATUS_OK);
256         fnum = io.openold.out.file.fnum;
257
258         /* check other reply fields */
259         CHECK_TIME(io.openold.out.write_time, write_time);
260         CHECK_ALL_INFO(io.openold.out.size, size);
261         CHECK_ALL_INFO(io.openold.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
262
263 done:
264         smbcli_close(cli->tree, fnum);
265         smbcli_deltree(cli->tree, BASEDIR);
266
267         return ret;
268 }
269
270
271 /*
272   test RAW_OPEN_OPENX
273 */
274 static bool test_openx(struct torture_context *tctx, struct smbcli_state *cli)
275 {
276         union smb_open io;
277         union smb_fileinfo finfo;
278         const char *fname = BASEDIR "\\torture_openx.txt";
279         const char *fname_exe = BASEDIR "\\torture_openx.exe";
280         NTSTATUS status;
281         int fnum = -1, fnum2;
282         bool ret = true;
283         int i;
284         struct timeval tv;
285         struct {
286                 uint16_t open_func;
287                 bool with_file;
288                 NTSTATUS correct_status;
289         } open_funcs[] = {
290                 { OPENX_OPEN_FUNC_OPEN,                           true,  NT_STATUS_OK },
291                 { OPENX_OPEN_FUNC_OPEN,                           false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
292                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, true,  NT_STATUS_OK },
293                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, false, NT_STATUS_OK },
294                 { OPENX_OPEN_FUNC_FAIL,                           true,  NT_STATUS_DOS(ERRDOS, ERRbadaccess) },
295                 { OPENX_OPEN_FUNC_FAIL,                           false, NT_STATUS_DOS(ERRDOS, ERRbadaccess) },
296                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, true,  NT_STATUS_OBJECT_NAME_COLLISION },
297                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, false, NT_STATUS_OK },
298                 { OPENX_OPEN_FUNC_TRUNC,                          true,  NT_STATUS_OK },
299                 { OPENX_OPEN_FUNC_TRUNC,                          false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
300                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, true,  NT_STATUS_OK },
301                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, false, NT_STATUS_OK },
302         };
303
304         if (!torture_setup_dir(cli, BASEDIR)) {
305                 return false;
306         }
307
308         io.openx.level = RAW_OPEN_OPENX;
309         io.openx.in.fname = fname;
310         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
311         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
312         io.openx.in.search_attrs = 0;
313         io.openx.in.file_attrs = 0;
314         io.openx.in.write_time = 0;
315         io.openx.in.size = 1024*1024;
316         io.openx.in.timeout = 0;
317
318         /* check all combinations of open_func */
319         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
320                 if (open_funcs[i].with_file) {
321                         fnum = create_complex_file(cli, tctx, fname);
322                         if (fnum == -1) {
323                                 torture_result(tctx, TORTURE_FAIL,
324                                         "Failed to create file %s - %s\n",
325                                         fname, smbcli_errstr(cli->tree));
326                                 ret = false;
327                                 goto done;
328                         }
329                         smbcli_close(cli->tree, fnum);
330                 }
331                 io.openx.in.open_func = open_funcs[i].open_func;
332                 status = smb_raw_open(cli->tree, tctx, &io);
333                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
334                         torture_result(tctx, TORTURE_FAIL,
335                                 "(%s) incorrect status %s should be %s "
336                                 "(i=%d with_file=%d open_func=0x%x)\n",
337                                 __location__, nt_errstr(status),
338                                 nt_errstr(open_funcs[i].correct_status),
339                                 i, (int)open_funcs[i].with_file,
340                                 open_funcs[i].open_func);
341                         ret = false;
342                 }
343                 if (NT_STATUS_IS_OK(status)) {
344                         smbcli_close(cli->tree, io.openx.out.file.fnum);
345                 }
346                 if (open_funcs[i].with_file) {
347                         smbcli_unlink(cli->tree, fname);
348                 }
349         }
350
351         smbcli_unlink(cli->tree, fname);
352
353         /* check the basic return fields */
354         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
355         status = smb_raw_open(cli->tree, tctx, &io);
356         CHECK_STATUS(status, NT_STATUS_OK);
357         fnum = io.openx.out.file.fnum;
358
359         CHECK_ALL_INFO(io.openx.out.size, size);
360         CHECK_TIME(io.openx.out.write_time, write_time);
361         CHECK_ALL_INFO(io.openx.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
362         CHECK_VAL(io.openx.out.access, OPENX_MODE_ACCESS_RDWR);
363         CHECK_VAL(io.openx.out.ftype, 0);
364         CHECK_VAL(io.openx.out.devstate, 0);
365         CHECK_VAL(io.openx.out.action, OPENX_ACTION_CREATED);
366         CHECK_VAL(io.openx.out.size, 1024*1024);
367         CHECK_ALL_INFO(io.openx.in.size, size);
368         smbcli_close(cli->tree, fnum);
369         smbcli_unlink(cli->tree, fname);
370
371         /* check the fields when the file already existed */
372         fnum2 = create_complex_file(cli, tctx, fname);
373         if (fnum2 == -1) {
374                 ret = false;
375                 goto done;
376         }
377         smbcli_close(cli->tree, fnum2);
378
379         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
380         status = smb_raw_open(cli->tree, tctx, &io);
381         CHECK_STATUS(status, NT_STATUS_OK);
382         fnum = io.openx.out.file.fnum;
383
384         CHECK_ALL_INFO(io.openx.out.size, size);
385         CHECK_TIME(io.openx.out.write_time, write_time);
386         CHECK_VAL(io.openx.out.action, OPENX_ACTION_EXISTED);
387         CHECK_VAL(io.openx.out.unknown, 0);
388         CHECK_ALL_INFO(io.openx.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
389         smbcli_close(cli->tree, fnum);
390
391         /* now check the search attrib for hidden files - win2003 ignores this? */
392         SET_ATTRIB(FILE_ATTRIBUTE_HIDDEN);
393         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN, attrib);
394
395         io.openx.in.search_attrs = FILE_ATTRIBUTE_HIDDEN;
396         status = smb_raw_open(cli->tree, tctx, &io);
397         CHECK_STATUS(status, NT_STATUS_OK);
398         smbcli_close(cli->tree, io.openx.out.file.fnum);
399
400         io.openx.in.search_attrs = 0;
401         status = smb_raw_open(cli->tree, tctx, &io);
402         CHECK_STATUS(status, NT_STATUS_OK);
403         smbcli_close(cli->tree, io.openx.out.file.fnum);
404
405         SET_ATTRIB(FILE_ATTRIBUTE_NORMAL);
406         smbcli_unlink(cli->tree, fname);
407
408         /* and check attrib on create */
409         io.openx.in.open_func = OPENX_OPEN_FUNC_FAIL | OPENX_OPEN_FUNC_CREATE;
410         io.openx.in.search_attrs = 0;
411         io.openx.in.file_attrs = FILE_ATTRIBUTE_SYSTEM;
412         status = smb_raw_open(cli->tree, tctx, &io);
413         CHECK_STATUS(status, NT_STATUS_OK);
414         if (torture_setting_bool(tctx, "samba3", false)) {
415                 CHECK_ALL_INFO(FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE, 
416                                attrib & ~(FILE_ATTRIBUTE_NONINDEXED|
417                                           FILE_ATTRIBUTE_SPARSE));
418         }
419         else {
420                 CHECK_ALL_INFO(FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE, 
421                                attrib & ~(FILE_ATTRIBUTE_NONINDEXED));
422         }
423         smbcli_close(cli->tree, io.openx.out.file.fnum);
424         smbcli_unlink(cli->tree, fname);
425
426         /* check timeout on create - win2003 ignores the timeout! */
427         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
428         io.openx.in.file_attrs = 0;
429         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_ALL;
430         status = smb_raw_open(cli->tree, tctx, &io);
431         CHECK_STATUS(status, NT_STATUS_OK);
432         fnum = io.openx.out.file.fnum;
433
434         io.openx.in.timeout = 20000;
435         tv = timeval_current();
436         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_NONE;
437         status = smb_raw_open(cli->tree, tctx, &io);
438         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
439         if (timeval_elapsed(&tv) > 3.0) {
440                 torture_result(tctx, TORTURE_FAIL,
441                         "(%s) Incorrect timing in openx with timeout "
442                         "- waited %.2f seconds\n",
443                         __location__, timeval_elapsed(&tv));
444                 ret = false;
445         }
446         smbcli_close(cli->tree, fnum);
447         smbcli_unlink(cli->tree, fname);
448
449         /* now this is a really weird one - open for execute implies create?! */
450         io.openx.in.fname = fname;
451         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
452         io.openx.in.open_mode = OPENX_MODE_ACCESS_EXEC | OPENX_MODE_DENY_NONE;
453         io.openx.in.search_attrs = 0;
454         io.openx.in.open_func = OPENX_OPEN_FUNC_FAIL;
455         io.openx.in.file_attrs = 0;
456         io.openx.in.write_time = 0;
457         io.openx.in.size = 0;
458         io.openx.in.timeout = 0;
459         status = smb_raw_open(cli->tree, tctx, &io);
460         CHECK_STATUS(status, NT_STATUS_OK);
461         smbcli_close(cli->tree, io.openx.out.file.fnum);
462
463         /* check the extended return flag */
464         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO | OPENX_FLAGS_EXTENDED_RETURN;
465         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
466         status = smb_raw_open(cli->tree, tctx, &io);
467         CHECK_STATUS(status, NT_STATUS_OK);
468         CHECK_VAL(io.openx.out.access_mask, SEC_STD_ALL);
469         smbcli_close(cli->tree, io.openx.out.file.fnum);
470
471         io.openx.in.fname = "\\A.+,;=[].B";
472         status = smb_raw_open(cli->tree, tctx, &io);
473         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
474
475         /* Check the mapping for open exec. */
476
477         /* First create an .exe file. */
478         smbcli_unlink(cli->tree, fname_exe);
479         fnum = create_complex_file(cli, tctx, fname_exe);
480         smbcli_close(cli->tree, fnum);
481
482         io.openx.level = RAW_OPEN_OPENX;
483         io.openx.in.fname = fname_exe;
484         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
485         io.openx.in.open_mode = OPENX_MODE_ACCESS_EXEC | OPENX_MODE_DENY_NONE;
486         io.openx.in.search_attrs = 0;
487         io.openx.in.file_attrs = 0;
488         io.openx.in.write_time = 0;
489         io.openx.in.size = 0;
490         io.openx.in.timeout = 0;
491         status = smb_raw_open(cli->tree, tctx, &io);
492         CHECK_STATUS(status, NT_STATUS_OK);
493
494         /* Can we read and write ? */
495         CHECK_RDWR(io.openx.out.file.fnum, RDWR_RDONLY);
496         smbcli_close(cli->tree, io.openx.out.file.fnum);
497         smbcli_unlink(cli->tree, fname);
498
499 done:
500         smbcli_close(cli->tree, fnum);
501         smbcli_deltree(cli->tree, BASEDIR);
502
503         return ret;
504 }
505
506
507 /*
508   test RAW_OPEN_T2OPEN
509
510   many thanks to kukks for a sniff showing how this works with os2->w2k
511 */
512 static bool test_t2open(struct torture_context *tctx, struct smbcli_state *cli)
513 {
514         union smb_open io;
515         union smb_fileinfo finfo;
516         const char *fname1 = BASEDIR "\\torture_t2open_yes.txt";
517         const char *fname2 = BASEDIR "\\torture_t2open_no.txt";
518         const char *fname = BASEDIR "\\torture_t2open_3.txt";
519         NTSTATUS status;
520         int fnum;
521         bool ret = true;
522         int i;
523         struct {
524                 uint16_t open_func;
525                 bool with_file;
526                 NTSTATUS correct_status;
527         } open_funcs[] = {
528                 { OPENX_OPEN_FUNC_OPEN,                           true,  NT_STATUS_OK },
529                 { OPENX_OPEN_FUNC_OPEN,                           false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
530                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, true,  NT_STATUS_OK },
531                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, false, NT_STATUS_OK },
532                 { OPENX_OPEN_FUNC_FAIL,                           true,  NT_STATUS_OBJECT_NAME_COLLISION },
533                 { OPENX_OPEN_FUNC_FAIL,                           false, NT_STATUS_OBJECT_NAME_COLLISION },
534                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, true,  NT_STATUS_OBJECT_NAME_COLLISION },
535                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, false, NT_STATUS_OBJECT_NAME_COLLISION },
536                 { OPENX_OPEN_FUNC_TRUNC,                          true,  NT_STATUS_OK },
537                 { OPENX_OPEN_FUNC_TRUNC,                          false, NT_STATUS_OK },
538                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, true,  NT_STATUS_OK },
539                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, false, NT_STATUS_OK },
540         };
541
542         if (!torture_setup_dir(cli, BASEDIR)) {
543                 return false;
544         }
545
546         fnum = create_complex_file(cli, tctx, fname1);
547         if (fnum == -1) {
548                 torture_result(tctx, TORTURE_FAIL,
549                         "(%s): Failed to create file %s - %s\n",
550                         __location__, fname1, smbcli_errstr(cli->tree));
551                 ret = false;
552                 goto done;
553         }
554         smbcli_close(cli->tree, fnum);
555
556         io.t2open.level = RAW_OPEN_T2OPEN;
557         io.t2open.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
558         io.t2open.in.open_mode = OPENX_MODE_DENY_NONE | OPENX_MODE_ACCESS_RDWR;
559         io.t2open.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
560         io.t2open.in.search_attrs = 0;
561         io.t2open.in.file_attrs = 0;
562         io.t2open.in.write_time = 0;
563         io.t2open.in.size = 0;
564         io.t2open.in.timeout = 0;
565
566         io.t2open.in.num_eas = 3;
567         io.t2open.in.eas = talloc_array(tctx, struct ea_struct, io.t2open.in.num_eas);
568         io.t2open.in.eas[0].flags = 0;
569         io.t2open.in.eas[0].name.s = ".CLASSINFO";
570         io.t2open.in.eas[0].value = data_blob_talloc(tctx, "first value", 11);
571         io.t2open.in.eas[1].flags = 0;
572         io.t2open.in.eas[1].name.s = "EA TWO";
573         io.t2open.in.eas[1].value = data_blob_talloc(tctx, "foo", 3);
574         io.t2open.in.eas[2].flags = 0;
575         io.t2open.in.eas[2].name.s = "X THIRD";
576         io.t2open.in.eas[2].value = data_blob_talloc(tctx, "xy", 2);
577
578         /* check all combinations of open_func */
579         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
580         again:
581                 if (open_funcs[i].with_file) {
582                         io.t2open.in.fname = fname1;
583                 } else {
584                         io.t2open.in.fname = fname2;
585                 }
586                 io.t2open.in.open_func = open_funcs[i].open_func;
587                 status = smb_raw_open(cli->tree, tctx, &io);
588                 if ((io.t2open.in.num_eas != 0)
589                     && NT_STATUS_EQUAL(status, NT_STATUS_EAS_NOT_SUPPORTED)
590                     && torture_setting_bool(tctx, "samba3", false)) {
591                         torture_warning(tctx, "(%s) EAs not supported, not "
592                                 "treating as fatal in Samba3 test\n",
593                                 __location__);
594                         io.t2open.in.num_eas = 0;
595                         goto again;
596                 }
597
598                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
599                         torture_result(tctx, TORTURE_FAIL,
600                                 "(%s) incorrect status %s should be %s "
601                                 "(i=%d with_file=%d open_func=0x%x)\n",
602                                  __location__, nt_errstr(status),
603                                 nt_errstr(open_funcs[i].correct_status),
604                                 i, (int)open_funcs[i].with_file,
605                                 open_funcs[i].open_func);
606                         ret = false;
607                 }
608                 if (NT_STATUS_IS_OK(status)) {
609                         smbcli_close(cli->tree, io.t2open.out.file.fnum);
610                 }
611         }
612
613         smbcli_unlink(cli->tree, fname1);
614         smbcli_unlink(cli->tree, fname2);
615
616         /* check the basic return fields */
617         io.t2open.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
618         io.t2open.in.write_time = 0;
619         io.t2open.in.fname = fname;
620         status = smb_raw_open(cli->tree, tctx, &io);
621         CHECK_STATUS(status, NT_STATUS_OK);
622         fnum = io.t2open.out.file.fnum;
623
624         CHECK_ALL_INFO(io.t2open.out.size, size);
625 #if 0
626         /* windows appears to leak uninitialised memory here */
627         CHECK_VAL(io.t2open.out.write_time, 0);
628 #endif
629         CHECK_ALL_INFO(io.t2open.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
630         CHECK_VAL(io.t2open.out.access, OPENX_MODE_DENY_NONE | OPENX_MODE_ACCESS_RDWR);
631         CHECK_VAL(io.t2open.out.ftype, 0);
632         CHECK_VAL(io.t2open.out.devstate, 0);
633         CHECK_VAL(io.t2open.out.action, OPENX_ACTION_CREATED);
634         smbcli_close(cli->tree, fnum);
635
636         status = torture_check_ea(cli, fname, ".CLASSINFO", "first value");
637         CHECK_STATUS(status, io.t2open.in.num_eas
638                      ? NT_STATUS_OK : NT_STATUS_EAS_NOT_SUPPORTED);
639         status = torture_check_ea(cli, fname, "EA TWO", "foo");
640         CHECK_STATUS(status, io.t2open.in.num_eas
641                      ? NT_STATUS_OK : NT_STATUS_EAS_NOT_SUPPORTED);
642         status = torture_check_ea(cli, fname, "X THIRD", "xy");
643         CHECK_STATUS(status, io.t2open.in.num_eas
644                      ? NT_STATUS_OK : NT_STATUS_EAS_NOT_SUPPORTED);
645
646         /* now check the search attrib for hidden files - win2003 ignores this? */
647         SET_ATTRIB(FILE_ATTRIBUTE_HIDDEN);
648         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN, attrib);
649
650         status = smb_raw_open(cli->tree, tctx, &io);
651         CHECK_STATUS(status, NT_STATUS_OK);
652         smbcli_close(cli->tree, io.t2open.out.file.fnum);
653
654         status = smb_raw_open(cli->tree, tctx, &io);
655         CHECK_STATUS(status, NT_STATUS_OK);
656         smbcli_close(cli->tree, io.t2open.out.file.fnum);
657
658         SET_ATTRIB(FILE_ATTRIBUTE_NORMAL);
659         smbcli_unlink(cli->tree, fname);
660
661         /* and check attrib on create */
662         io.t2open.in.open_func = OPENX_OPEN_FUNC_FAIL | OPENX_OPEN_FUNC_CREATE;
663         io.t2open.in.file_attrs = FILE_ATTRIBUTE_SYSTEM;
664         status = smb_raw_open(cli->tree, tctx, &io);
665         CHECK_STATUS(status, NT_STATUS_OK);
666
667         /* check timeout on create - win2003 ignores the timeout! */
668         io.t2open.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
669         io.t2open.in.file_attrs = 0;
670         io.t2open.in.timeout = 20000;
671         io.t2open.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_ALL;
672         status = smb_raw_open(cli->tree, tctx, &io);
673         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
674
675 done:
676         smbcli_close(cli->tree, fnum);
677         smbcli_deltree(cli->tree, BASEDIR);
678
679         return ret;
680 }
681         
682
683 /*
684   test RAW_OPEN_NTCREATEX
685 */
686 static bool test_ntcreatex(struct torture_context *tctx, struct smbcli_state *cli)
687 {
688         union smb_open io;
689         union smb_fileinfo finfo;
690         const char *fname = BASEDIR "\\torture_ntcreatex.txt";
691         const char *dname = BASEDIR "\\torture_ntcreatex.dir";
692         NTSTATUS status;
693         int fnum = -1;
694         bool ret = true;
695         int i;
696         struct {
697                 uint32_t open_disp;
698                 bool with_file;
699                 NTSTATUS correct_status;
700         } open_funcs[] = {
701                 { NTCREATEX_DISP_SUPERSEDE,     true,  NT_STATUS_OK },
702                 { NTCREATEX_DISP_SUPERSEDE,     false, NT_STATUS_OK },
703                 { NTCREATEX_DISP_OPEN,          true,  NT_STATUS_OK },
704                 { NTCREATEX_DISP_OPEN,          false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
705                 { NTCREATEX_DISP_CREATE,        true,  NT_STATUS_OBJECT_NAME_COLLISION },
706                 { NTCREATEX_DISP_CREATE,        false, NT_STATUS_OK },
707                 { NTCREATEX_DISP_OPEN_IF,       true,  NT_STATUS_OK },
708                 { NTCREATEX_DISP_OPEN_IF,       false, NT_STATUS_OK },
709                 { NTCREATEX_DISP_OVERWRITE,     true,  NT_STATUS_OK },
710                 { NTCREATEX_DISP_OVERWRITE,     false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
711                 { NTCREATEX_DISP_OVERWRITE_IF,  true,  NT_STATUS_OK },
712                 { NTCREATEX_DISP_OVERWRITE_IF,  false, NT_STATUS_OK },
713                 { 6,                            true,  NT_STATUS_INVALID_PARAMETER },
714                 { 6,                            false, NT_STATUS_INVALID_PARAMETER },
715         };
716
717         if (!torture_setup_dir(cli, BASEDIR)) {
718                 return false;
719         }
720
721         /* reasonable default parameters */
722         io.generic.level = RAW_OPEN_NTCREATEX;
723         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
724         io.ntcreatex.in.root_fid.fnum = 0;
725         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
726         io.ntcreatex.in.alloc_size = 1024*1024;
727         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
728         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
729         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
730         io.ntcreatex.in.create_options = 0;
731         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
732         io.ntcreatex.in.security_flags = 0;
733         io.ntcreatex.in.fname = fname;
734
735         /* test the open disposition */
736         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
737                 if (open_funcs[i].with_file) {
738                         fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR|O_TRUNC, DENY_NONE);
739                         if (fnum == -1) {
740                                 torture_result(tctx, TORTURE_FAIL,
741                                         "Failed to create file %s - %s\n",
742                                         fname, smbcli_errstr(cli->tree));
743                                 ret = false;
744                                 goto done;
745                         }
746                         smbcli_close(cli->tree, fnum);
747                 }
748                 io.ntcreatex.in.open_disposition = open_funcs[i].open_disp;
749                 status = smb_raw_open(cli->tree, tctx, &io);
750                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
751                         torture_result(tctx, TORTURE_FAIL,
752                                 "(%s) incorrect status %s should be %s "
753                                 "(i=%d with_file=%d open_disp=%d)\n",
754                                 __location__, nt_errstr(status),
755                                 nt_errstr(open_funcs[i].correct_status),
756                                 i, (int)open_funcs[i].with_file,
757                                 (int)open_funcs[i].open_disp);
758                         ret = false;
759                 }
760                 if (NT_STATUS_IS_OK(status) || open_funcs[i].with_file) {
761                         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
762                         smbcli_unlink(cli->tree, fname);
763                 }
764         }
765
766         /* basic field testing */
767         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
768
769         status = smb_raw_open(cli->tree, tctx, &io);
770         CHECK_STATUS(status, NT_STATUS_OK);
771         fnum = io.ntcreatex.out.file.fnum;
772
773         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
774         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
775         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
776         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
777         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
778         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
779         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
780         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
781         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
782         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
783         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
784
785         /* check fields when the file already existed */
786         smbcli_close(cli->tree, fnum);
787         smbcli_unlink(cli->tree, fname);
788         fnum = create_complex_file(cli, tctx, fname);
789         if (fnum == -1) {
790                 ret = false;
791                 goto done;
792         }
793         smbcli_close(cli->tree, fnum);
794
795         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
796         status = smb_raw_open(cli->tree, tctx, &io);
797         CHECK_STATUS(status, NT_STATUS_OK);
798         fnum = io.ntcreatex.out.file.fnum;
799
800         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
801         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_EXISTED);
802         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
803         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
804         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
805         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
806         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
807         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
808         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
809         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
810         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
811         smbcli_close(cli->tree, fnum);
812         smbcli_unlink(cli->tree, fname);
813
814
815         /* create a directory */
816         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
817         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
818         io.ntcreatex.in.alloc_size = 0;
819         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
820         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
821         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
822         io.ntcreatex.in.create_options = 0;
823         io.ntcreatex.in.fname = dname;
824         fname = dname;
825
826         smbcli_rmdir(cli->tree, fname);
827         smbcli_unlink(cli->tree, fname);
828
829         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
830         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
831         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
832         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
833         status = smb_raw_open(cli->tree, tctx, &io);
834         CHECK_STATUS(status, NT_STATUS_OK);
835         fnum = io.ntcreatex.out.file.fnum;
836
837         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
838         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
839         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
840         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
841         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
842         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
843         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
844         CHECK_VAL(io.ntcreatex.out.attrib & ~FILE_ATTRIBUTE_NONINDEXED, 
845                   FILE_ATTRIBUTE_DIRECTORY);
846         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
847         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
848         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
849         CHECK_VAL(io.ntcreatex.out.is_directory, 1);
850         CHECK_VAL(io.ntcreatex.out.size, 0);
851         CHECK_VAL(io.ntcreatex.out.alloc_size, 0);
852         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
853         smbcli_unlink(cli->tree, fname);
854         
855
856 done:
857         smbcli_close(cli->tree, fnum);
858         smbcli_deltree(cli->tree, BASEDIR);
859
860         return ret;
861 }
862
863
864 /*
865   test RAW_OPEN_NTTRANS_CREATE
866 */
867 static bool test_nttrans_create(struct torture_context *tctx, struct smbcli_state *cli)
868 {
869         union smb_open io;
870         union smb_fileinfo finfo;
871         const char *fname = BASEDIR "\\torture_ntcreatex.txt";
872         const char *dname = BASEDIR "\\torture_ntcreatex.dir";
873         NTSTATUS status;
874         int fnum = -1;
875         bool ret = true;
876         int i;
877         uint32_t ok_mask, not_supported_mask, invalid_parameter_mask;
878         uint32_t not_a_directory_mask, unexpected_mask;
879         struct {
880                 uint32_t open_disp;
881                 bool with_file;
882                 NTSTATUS correct_status;
883         } open_funcs[] = {
884                 { NTCREATEX_DISP_SUPERSEDE,     true,  NT_STATUS_OK },
885                 { NTCREATEX_DISP_SUPERSEDE,     false, NT_STATUS_OK },
886                 { NTCREATEX_DISP_OPEN,          true,  NT_STATUS_OK },
887                 { NTCREATEX_DISP_OPEN,          false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
888                 { NTCREATEX_DISP_CREATE,        true,  NT_STATUS_OBJECT_NAME_COLLISION },
889                 { NTCREATEX_DISP_CREATE,        false, NT_STATUS_OK },
890                 { NTCREATEX_DISP_OPEN_IF,       true,  NT_STATUS_OK },
891                 { NTCREATEX_DISP_OPEN_IF,       false, NT_STATUS_OK },
892                 { NTCREATEX_DISP_OVERWRITE,     true,  NT_STATUS_OK },
893                 { NTCREATEX_DISP_OVERWRITE,     false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
894                 { NTCREATEX_DISP_OVERWRITE_IF,  true,  NT_STATUS_OK },
895                 { NTCREATEX_DISP_OVERWRITE_IF,  false, NT_STATUS_OK },
896                 { 6,                            true,  NT_STATUS_INVALID_PARAMETER },
897                 { 6,                            false, NT_STATUS_INVALID_PARAMETER },
898         };
899
900         if (!torture_setup_dir(cli, BASEDIR)) {
901                 return false;
902         }
903
904         /* reasonable default parameters */
905         io.generic.level = RAW_OPEN_NTTRANS_CREATE;
906         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
907         io.ntcreatex.in.root_fid.fnum = 0;
908         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
909         io.ntcreatex.in.alloc_size = 1024*1024;
910         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
911         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
912         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
913         io.ntcreatex.in.create_options = 0;
914         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
915         io.ntcreatex.in.security_flags = 0;
916         io.ntcreatex.in.fname = fname;
917         io.ntcreatex.in.sec_desc = NULL;
918         io.ntcreatex.in.ea_list = NULL;
919
920         /* test the open disposition */
921         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
922                 if (open_funcs[i].with_file) {
923                         fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR|O_TRUNC, DENY_NONE);
924                         if (fnum == -1) {
925                                 torture_result(tctx, TORTURE_FAIL,
926                                         "Failed to create file %s - %s\n",
927                                         fname, smbcli_errstr(cli->tree));
928                                 ret = false;
929                                 goto done;
930                         }
931                         smbcli_close(cli->tree, fnum);
932                 }
933                 io.ntcreatex.in.open_disposition = open_funcs[i].open_disp;
934                 status = smb_raw_open(cli->tree, tctx, &io);
935                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
936                         torture_result(tctx, TORTURE_FAIL,
937                                 "(%s) incorrect status %s should be %s "
938                                 "(i=%d with_file=%d open_disp=%d)\n",
939                                 __location__, nt_errstr(status),
940                                 nt_errstr(open_funcs[i].correct_status),
941                                 i, (int)open_funcs[i].with_file,
942                                 (int)open_funcs[i].open_disp);
943                         ret = false;
944                 }
945                 if (NT_STATUS_IS_OK(status) || open_funcs[i].with_file) {
946                         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
947                         smbcli_unlink(cli->tree, fname);
948                 }
949         }
950
951         /* basic field testing */
952         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
953
954         status = smb_raw_open(cli->tree, tctx, &io);
955         CHECK_STATUS(status, NT_STATUS_OK);
956         fnum = io.ntcreatex.out.file.fnum;
957
958         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
959         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
960         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
961         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
962         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
963         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
964         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
965         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
966         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
967         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
968         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
969
970         /* check fields when the file already existed */
971         smbcli_close(cli->tree, fnum);
972         smbcli_unlink(cli->tree, fname);
973         fnum = create_complex_file(cli, tctx, fname);
974         if (fnum == -1) {
975                 ret = false;
976                 goto done;
977         }
978         smbcli_close(cli->tree, fnum);
979
980         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
981         status = smb_raw_open(cli->tree, tctx, &io);
982         CHECK_STATUS(status, NT_STATUS_OK);
983         fnum = io.ntcreatex.out.file.fnum;
984
985         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
986         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_EXISTED);
987         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
988         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
989         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
990         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
991         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
992         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
993         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
994         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
995         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
996         smbcli_close(cli->tree, fnum);
997
998         /* check no-recall - don't pull a file from tape on a HSM */
999         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_NO_RECALL;
1000         status = smb_raw_open(cli->tree, tctx, &io);
1001         CHECK_STATUS(status, NT_STATUS_OK);
1002         fnum = io.ntcreatex.out.file.fnum;
1003
1004         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
1005         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_EXISTED);
1006         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
1007         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
1008         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
1009         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
1010         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
1011         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
1012         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
1013         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
1014         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
1015         smbcli_close(cli->tree, fnum);
1016
1017         /* Check some create options (these all should be ignored) */
1018         for (i=0; i < 32; i++) {
1019                 uint32_t create_option = (1 << i) & NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1020                 if (create_option == 0) {
1021                         continue;
1022                 }
1023                 io.ntcreatex.in.create_options = create_option;
1024                 status = smb_raw_open(cli->tree, tctx, &io);
1025                 if (!NT_STATUS_IS_OK(status)) {
1026                         torture_warning(tctx, "ntcreatex create option 0x%08x "
1027                                 "gave %s - should give NT_STATUS_OK\n",
1028                                 create_option, nt_errstr(status));
1029                 }
1030                 CHECK_STATUS(status, NT_STATUS_OK);
1031                 fnum = io.ntcreatex.out.file.fnum;
1032
1033                 CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
1034                 CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_EXISTED);
1035                 CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
1036                 CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
1037                 CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
1038                 CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
1039                 CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
1040                 CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
1041                 CHECK_ALL_INFO(io.ntcreatex.out.size, size);
1042                 CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
1043                 CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
1044                 smbcli_close(cli->tree, fnum);
1045         }
1046
1047         io.ntcreatex.in.file_attr = 0;
1048         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
1049         io.ntcreatex.in.access_mask     = SEC_FLAG_MAXIMUM_ALLOWED;
1050
1051         /* Check for options that should return NOT_SUPPORTED, OK or INVALID_PARAMETER */
1052         ok_mask = 0;
1053         not_supported_mask = 0;
1054         invalid_parameter_mask = 0;
1055         not_a_directory_mask = 0;
1056         unexpected_mask = 0;
1057         for (i=0; i < 32; i++) {
1058                 uint32_t create_option = 1<<i;
1059                 if (create_option & NTCREATEX_OPTIONS_DELETE_ON_CLOSE) {
1060                         continue;
1061                 }
1062                 io.ntcreatex.in.create_options = create_option;
1063                 status = smb_raw_open(cli->tree, tctx, &io);
1064                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
1065                         not_supported_mask |= create_option;
1066                 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
1067                         ok_mask |= create_option;
1068                         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
1069                 } else if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
1070                         invalid_parameter_mask |= create_option;
1071                 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_A_DIRECTORY)) {
1072                         not_a_directory_mask |= 1<<i;
1073                 } else {
1074                         unexpected_mask |= 1<<i;
1075                         torture_comment(tctx, "create option 0x%08x returned %s\n",
1076                                         create_option, nt_errstr(status));
1077                 }
1078         }
1079
1080         CHECK_VAL(ok_mask,                0x00efcfce);
1081         CHECK_VAL(not_a_directory_mask,   0x00000001);
1082         CHECK_VAL(not_supported_mask,     0x00002000);
1083         CHECK_VAL(invalid_parameter_mask, 0xff100030);
1084         CHECK_VAL(unexpected_mask,        0x00000000);
1085
1086         smbcli_unlink(cli->tree, fname);
1087
1088
1089         /* create a directory */
1090         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
1091         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
1092         io.ntcreatex.in.alloc_size = 0;
1093         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
1094         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
1095         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
1096         io.ntcreatex.in.create_options = 0;
1097         io.ntcreatex.in.fname = dname;
1098         fname = dname;
1099
1100         smbcli_rmdir(cli->tree, fname);
1101         smbcli_unlink(cli->tree, fname);
1102
1103         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1104         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1105         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1106         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1107         status = smb_raw_open(cli->tree, tctx, &io);
1108         CHECK_STATUS(status, NT_STATUS_OK);
1109         fnum = io.ntcreatex.out.file.fnum;
1110
1111         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
1112         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
1113         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
1114         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
1115         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
1116         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
1117         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
1118         CHECK_VAL(io.ntcreatex.out.attrib & ~FILE_ATTRIBUTE_NONINDEXED, 
1119                   FILE_ATTRIBUTE_DIRECTORY);
1120         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
1121         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
1122         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
1123         CHECK_VAL(io.ntcreatex.out.is_directory, 1);
1124         CHECK_VAL(io.ntcreatex.out.size, 0);
1125         CHECK_VAL(io.ntcreatex.out.alloc_size, 0);
1126         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
1127         smbcli_unlink(cli->tree, fname);
1128         
1129
1130 done:
1131         smbcli_close(cli->tree, fnum);
1132         smbcli_deltree(cli->tree, BASEDIR);
1133
1134         return ret;
1135 }
1136
1137 /*
1138   test RAW_OPEN_NTCREATEX with an already opened and byte range locked file
1139
1140   I've got an application that does a similar sequence of ntcreate&x,
1141   locking&x and another ntcreate&x with
1142   open_disposition==NTCREATEX_DISP_OVERWRITE_IF. Windows 2003 allows the
1143   second open.
1144 */
1145 static bool test_ntcreatex_brlocked(struct torture_context *tctx, struct smbcli_state *cli)
1146 {
1147         union smb_open io, io1;
1148         union smb_lock io2;
1149         struct smb_lock_entry lock[1];
1150         const char *fname = BASEDIR "\\torture_ntcreatex.txt";
1151         NTSTATUS status;
1152         bool ret = true;
1153
1154         if (!torture_setup_dir(cli, BASEDIR)) {
1155                 return false;
1156         }
1157
1158         torture_comment(tctx, "Testing ntcreatex with a byte range locked file\n");
1159
1160         io.generic.level = RAW_OPEN_NTCREATEX;
1161         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
1162         io.ntcreatex.in.root_fid.fnum = 0;
1163         io.ntcreatex.in.access_mask = 0x2019f;
1164         io.ntcreatex.in.alloc_size = 0;
1165         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1166         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1167                 NTCREATEX_SHARE_ACCESS_WRITE;
1168         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
1169         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_NON_DIRECTORY_FILE;
1170         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
1171         io.ntcreatex.in.security_flags = NTCREATEX_SECURITY_DYNAMIC |
1172                 NTCREATEX_SECURITY_ALL;
1173         io.ntcreatex.in.fname = fname;
1174
1175         status = smb_raw_open(cli->tree, tctx, &io);
1176         CHECK_STATUS(status, NT_STATUS_OK);
1177
1178         io2.lockx.level = RAW_LOCK_LOCKX;
1179         io2.lockx.in.file.fnum = io.ntcreatex.out.file.fnum;
1180         io2.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
1181         io2.lockx.in.timeout = 0;
1182         io2.lockx.in.ulock_cnt = 0;
1183         io2.lockx.in.lock_cnt = 1;
1184         lock[0].pid = cli->session->pid;
1185         lock[0].offset = 0;
1186         lock[0].count = 0x1;
1187         io2.lockx.in.locks = &lock[0];
1188         status = smb_raw_lock(cli->tree, &io2);
1189         CHECK_STATUS(status, NT_STATUS_OK);
1190
1191         io1.generic.level = RAW_OPEN_NTCREATEX;
1192         io1.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
1193         io1.ntcreatex.in.root_fid.fnum = 0;
1194         io1.ntcreatex.in.access_mask = 0x20196;
1195         io1.ntcreatex.in.alloc_size = 0;
1196         io1.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1197         io1.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1198                 NTCREATEX_SHARE_ACCESS_WRITE;
1199         io1.ntcreatex.in.open_disposition = NTCREATEX_DISP_OVERWRITE_IF;
1200         io1.ntcreatex.in.create_options = 0;
1201         io1.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
1202         io1.ntcreatex.in.security_flags = NTCREATEX_SECURITY_DYNAMIC |
1203                 NTCREATEX_SECURITY_ALL;
1204         io1.ntcreatex.in.fname = fname;
1205
1206         status = smb_raw_open(cli->tree, tctx, &io1);
1207         CHECK_STATUS(status, NT_STATUS_OK);
1208
1209  done:
1210         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
1211         smbcli_close(cli->tree, io1.ntcreatex.out.file.fnum);
1212         smbcli_deltree(cli->tree, BASEDIR);
1213         return ret;
1214 }
1215
1216 /*
1217   test RAW_OPEN_MKNEW
1218 */
1219 static bool test_mknew(struct torture_context *tctx, struct smbcli_state *cli)
1220 {
1221         union smb_open io;
1222         const char *fname = BASEDIR "\\torture_mknew.txt";
1223         NTSTATUS status;
1224         int fnum = -1;
1225         bool ret = true;
1226         time_t basetime = (time(NULL) + 3600*24*3) & ~1;
1227         union smb_fileinfo finfo;
1228
1229         if (!torture_setup_dir(cli, BASEDIR)) {
1230                 return false;
1231         }
1232
1233         io.mknew.level = RAW_OPEN_MKNEW;
1234         io.mknew.in.attrib = 0;
1235         io.mknew.in.write_time = 0;
1236         io.mknew.in.fname = fname;
1237         status = smb_raw_open(cli->tree, tctx, &io);
1238         CHECK_STATUS(status, NT_STATUS_OK);
1239         fnum = io.mknew.out.file.fnum;
1240
1241         status = smb_raw_open(cli->tree, tctx, &io);
1242         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_COLLISION);
1243
1244         smbcli_close(cli->tree, fnum);
1245         smbcli_unlink(cli->tree, fname);
1246
1247         /* make sure write_time works */
1248         io.mknew.in.write_time = basetime;
1249         status = smb_raw_open(cli->tree, tctx, &io);
1250         CHECK_STATUS(status, NT_STATUS_OK);
1251         fnum = io.mknew.out.file.fnum;
1252         CHECK_TIME(basetime, write_time);
1253
1254         smbcli_close(cli->tree, fnum);
1255         smbcli_unlink(cli->tree, fname);
1256
1257         /* make sure file_attrs works */
1258         io.mknew.in.attrib = FILE_ATTRIBUTE_HIDDEN;
1259         status = smb_raw_open(cli->tree, tctx, &io);
1260         CHECK_STATUS(status, NT_STATUS_OK);
1261         fnum = io.mknew.out.file.fnum;
1262         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE, 
1263                        attrib & ~FILE_ATTRIBUTE_NONINDEXED);
1264         
1265 done:
1266         smbcli_close(cli->tree, fnum);
1267         smbcli_deltree(cli->tree, BASEDIR);
1268
1269         return ret;
1270 }
1271
1272
1273 /*
1274   test RAW_OPEN_CREATE
1275 */
1276 static bool test_create(struct torture_context *tctx, struct smbcli_state *cli)
1277 {
1278         union smb_open io;
1279         const char *fname = BASEDIR "\\torture_create.txt";
1280         NTSTATUS status;
1281         int fnum = -1;
1282         bool ret = true;
1283         time_t basetime = (time(NULL) + 3600*24*3) & ~1;
1284         union smb_fileinfo finfo;
1285
1286         if (!torture_setup_dir(cli, BASEDIR)) {
1287                 return false;
1288         }
1289
1290         io.create.level = RAW_OPEN_CREATE;
1291         io.create.in.attrib = 0;
1292         io.create.in.write_time = 0;
1293         io.create.in.fname = fname;
1294         status = smb_raw_open(cli->tree, tctx, &io);
1295         CHECK_STATUS(status, NT_STATUS_OK);
1296         fnum = io.create.out.file.fnum;
1297
1298         status = smb_raw_open(cli->tree, tctx, &io);
1299         CHECK_STATUS(status, NT_STATUS_OK);
1300
1301         smbcli_close(cli->tree, io.create.out.file.fnum);
1302         smbcli_close(cli->tree, fnum);
1303         smbcli_unlink(cli->tree, fname);
1304
1305         /* make sure write_time works */
1306         io.create.in.write_time = basetime;
1307         status = smb_raw_open(cli->tree, tctx, &io);
1308         CHECK_STATUS(status, NT_STATUS_OK);
1309         fnum = io.create.out.file.fnum;
1310         CHECK_TIME(basetime, write_time);
1311
1312         smbcli_close(cli->tree, fnum);
1313         smbcli_unlink(cli->tree, fname);
1314
1315         /* make sure file_attrs works */
1316         io.create.in.attrib = FILE_ATTRIBUTE_HIDDEN;
1317         status = smb_raw_open(cli->tree, tctx, &io);
1318         CHECK_STATUS(status, NT_STATUS_OK);
1319         fnum = io.create.out.file.fnum;
1320         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE, 
1321                        attrib & ~FILE_ATTRIBUTE_NONINDEXED);
1322         
1323 done:
1324         smbcli_close(cli->tree, fnum);
1325         smbcli_deltree(cli->tree, BASEDIR);
1326
1327         return ret;
1328 }
1329
1330
1331 /*
1332   test RAW_OPEN_CTEMP
1333 */
1334 static bool test_ctemp(struct torture_context *tctx, struct smbcli_state *cli)
1335 {
1336         union smb_open io;
1337         NTSTATUS status;
1338         int fnum = -1;
1339         bool ret = true;
1340         time_t basetime = (time(NULL) + 3600*24*3) & ~1;
1341         union smb_fileinfo finfo;
1342         const char *name, *fname = NULL;
1343
1344         if (!torture_setup_dir(cli, BASEDIR)) {
1345                 return false;
1346         }
1347
1348         io.ctemp.level = RAW_OPEN_CTEMP;
1349         io.ctemp.in.attrib = FILE_ATTRIBUTE_HIDDEN;
1350         io.ctemp.in.write_time = basetime;
1351         io.ctemp.in.directory = BASEDIR;
1352         status = smb_raw_open(cli->tree, tctx, &io);
1353         CHECK_STATUS(status, NT_STATUS_OK);
1354         fnum = io.ctemp.out.file.fnum;
1355
1356         name = io.ctemp.out.name;
1357
1358         finfo.generic.level = RAW_FILEINFO_NAME_INFO;
1359         finfo.generic.in.file.fnum = fnum;
1360         status = smb_raw_fileinfo(cli->tree, tctx, &finfo);
1361         CHECK_STATUS(status, NT_STATUS_OK);
1362
1363         fname = finfo.name_info.out.fname.s;
1364         torture_comment(tctx, "ctemp name=%s  real name=%s\n", name, fname);
1365
1366 done:
1367         smbcli_close(cli->tree, fnum);
1368         smbcli_deltree(cli->tree, BASEDIR);
1369
1370         return ret;
1371 }
1372
1373
1374 /*
1375   test chained RAW_OPEN_OPENX_READX
1376 */
1377 static bool test_chained(struct torture_context *tctx, struct smbcli_state *cli)
1378 {
1379         union smb_open io;
1380         const char *fname = BASEDIR "\\torture_chained.txt";
1381         NTSTATUS status;
1382         int fnum = -1;
1383         bool ret = true;
1384         const char *buf = "test";
1385         char buf2[4];
1386
1387         if (!torture_setup_dir(cli, BASEDIR)) {
1388                 return false;
1389         }
1390
1391         fnum = create_complex_file(cli, tctx, fname);
1392
1393         smbcli_write(cli->tree, fnum, 0, buf, 0, sizeof(buf));
1394
1395         smbcli_close(cli->tree, fnum);  
1396
1397         io.openxreadx.level = RAW_OPEN_OPENX_READX;
1398         io.openxreadx.in.fname = fname;
1399         io.openxreadx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
1400         io.openxreadx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
1401         io.openxreadx.in.open_func = OPENX_OPEN_FUNC_OPEN;
1402         io.openxreadx.in.search_attrs = 0;
1403         io.openxreadx.in.file_attrs = 0;
1404         io.openxreadx.in.write_time = 0;
1405         io.openxreadx.in.size = 1024*1024;
1406         io.openxreadx.in.timeout = 0;
1407         
1408         io.openxreadx.in.offset = 0;
1409         io.openxreadx.in.mincnt = sizeof(buf);
1410         io.openxreadx.in.maxcnt = sizeof(buf);
1411         io.openxreadx.in.remaining = 0;
1412         io.openxreadx.out.data = (uint8_t *)buf2;
1413
1414         status = smb_raw_open(cli->tree, tctx, &io);
1415         CHECK_STATUS(status, NT_STATUS_OK);
1416         fnum = io.openxreadx.out.file.fnum;
1417
1418         if (memcmp(buf, buf2, sizeof(buf)) != 0) {
1419                 torture_result(tctx, TORTURE_FAIL,
1420                         "wrong data in reply buffer\n");
1421                 ret = false;
1422         }
1423
1424 done:
1425         smbcli_close(cli->tree, fnum);
1426         smbcli_deltree(cli->tree, BASEDIR);
1427
1428         return ret;
1429 }
1430
1431 /*
1432   test RAW_OPEN_OPENX without a leading slash on the path.
1433   NetApp filers are known to fail on this.
1434   
1435 */
1436 static bool test_no_leading_slash(struct torture_context *tctx, struct smbcli_state *cli)
1437 {
1438         union smb_open io;
1439         const char *fname = BASEDIR "\\torture_no_leading_slash.txt";
1440         NTSTATUS status;
1441         int fnum = -1;
1442         bool ret = true;
1443         const char *buf = "test";
1444
1445         if (!torture_setup_dir(cli, BASEDIR)) {
1446                 return false;
1447         }
1448
1449         smbcli_unlink(cli->tree, fname);
1450
1451         /* Create the file */
1452         fnum = create_complex_file(cli, tctx, fname);
1453         smbcli_write(cli->tree, fnum, 0, buf, 0, sizeof(buf));
1454         smbcli_close(cli->tree, fnum);  
1455
1456         /* Prepare to open the file using path without leading slash */
1457         io.openx.level = RAW_OPEN_OPENX;
1458         io.openx.in.fname = fname + 1;
1459         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
1460         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
1461         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
1462         io.openx.in.search_attrs = 0;
1463         io.openx.in.file_attrs = 0;
1464         io.openx.in.write_time = 0;
1465         io.openx.in.size = 1024*1024;
1466         io.openx.in.timeout = 0;
1467
1468         status = smb_raw_open(cli->tree, tctx, &io);
1469         CHECK_STATUS(status, NT_STATUS_OK);
1470         fnum = io.openx.out.file.fnum;
1471
1472 done:
1473         smbcli_close(cli->tree, fnum);
1474         smbcli_deltree(cli->tree, BASEDIR);
1475
1476         return ret;
1477 }
1478
1479 /*
1480   test RAW_OPEN_OPENX against an existing directory to
1481   ensure it returns NT_STATUS_FILE_IS_A_DIRECTORY.
1482   Samba 3.2.0 - 3.2.6 are known to fail this.
1483   
1484 */
1485 static bool test_openx_over_dir(struct torture_context *tctx, struct smbcli_state *cli)
1486 {
1487         union smb_open io;
1488         const char *fname = BASEDIR "\\openx_over_dir";
1489         NTSTATUS status;
1490         int d_fnum = -1;
1491         int fnum = -1;
1492         bool ret = true;
1493
1494         if (!torture_setup_dir(cli, BASEDIR)) {
1495                 return false;
1496         }
1497
1498         /* Create the Directory */
1499         status = create_directory_handle(cli->tree, fname, &d_fnum);
1500         smbcli_close(cli->tree, d_fnum);        
1501
1502         /* Prepare to open the file over the directory. */
1503         io.openx.level = RAW_OPEN_OPENX;
1504         io.openx.in.fname = fname;
1505         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
1506         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
1507         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
1508         io.openx.in.search_attrs = 0;
1509         io.openx.in.file_attrs = 0;
1510         io.openx.in.write_time = 0;
1511         io.openx.in.size = 1024*1024;
1512         io.openx.in.timeout = 0;
1513
1514         status = smb_raw_open(cli->tree, tctx, &io);
1515         CHECK_STATUS(status, NT_STATUS_FILE_IS_A_DIRECTORY);
1516         fnum = io.openx.out.file.fnum;
1517
1518 done:
1519         smbcli_close(cli->tree, fnum);
1520         smbcli_deltree(cli->tree, BASEDIR);
1521
1522         return ret;
1523 }
1524
1525
1526 /* A little torture test to expose a race condition in Samba 3.0.20 ... :-) */
1527
1528 static bool test_raw_open_multi(struct torture_context *tctx, struct smbcli_state *cli_ignored)
1529 {
1530         struct smbcli_state *cli;
1531         TALLOC_CTX *mem_ctx = talloc_init("torture_test_oplock_multi");
1532         const char *fname = "\\test_oplock.dat";
1533         NTSTATUS status;
1534         bool ret = true;
1535         union smb_open io;
1536         struct smbcli_state **clients;
1537         struct smbcli_request **requests;
1538         union smb_open *ios;
1539         const char *host = torture_setting_string(tctx, "host", NULL);
1540         const char *share = torture_setting_string(tctx, "share", NULL);
1541         int i, num_files = 3;
1542         int num_ok = 0;
1543         int num_collision = 0;
1544         
1545         clients = talloc_array(mem_ctx, struct smbcli_state *, num_files);
1546         requests = talloc_array(mem_ctx, struct smbcli_request *, num_files);
1547         ios = talloc_array(mem_ctx, union smb_open, num_files);
1548         if ((tctx->ev == NULL) || (clients == NULL) || (requests == NULL) ||
1549             (ios == NULL)) {
1550                 torture_result(tctx, TORTURE_FAIL, "(%s): talloc failed\n",
1551                                 __location__);
1552                 return false;
1553         }
1554
1555         if (!torture_open_connection_share(mem_ctx, &cli, tctx, host, share, tctx->ev)) {
1556                 return false;
1557         }
1558
1559         cli->tree->session->transport->options.request_timeout = 60;
1560
1561         for (i=0; i<num_files; i++) {
1562                 if (!torture_open_connection_share(mem_ctx, &(clients[i]),
1563                                                    tctx, host, share, tctx->ev)) {
1564                         torture_result(tctx, TORTURE_FAIL,
1565                                        "(%s): Could not open %d'th connection\n",
1566                                        __location__, i);
1567                         return false;
1568                 }
1569                 clients[i]->tree->session->transport->options.request_timeout = 60;
1570         }
1571
1572         /* cleanup */
1573         smbcli_unlink(cli->tree, fname);
1574
1575         /*
1576           base ntcreatex parms
1577         */
1578         io.generic.level = RAW_OPEN_NTCREATEX;
1579         io.ntcreatex.in.root_fid.fnum = 0;
1580         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
1581         io.ntcreatex.in.alloc_size = 0;
1582         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1583         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ|
1584                 NTCREATEX_SHARE_ACCESS_WRITE|
1585                 NTCREATEX_SHARE_ACCESS_DELETE;
1586         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
1587         io.ntcreatex.in.create_options = 0;
1588         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1589         io.ntcreatex.in.security_flags = 0;
1590         io.ntcreatex.in.fname = fname;
1591         io.ntcreatex.in.flags = 0;
1592
1593         for (i=0; i<num_files; i++) {
1594                 ios[i] = io;
1595                 requests[i] = smb_raw_open_send(clients[i]->tree, &ios[i]);
1596                 if (requests[i] == NULL) {
1597                         torture_result(tctx, TORTURE_FAIL,
1598                                 "(%s): could not send %d'th request\n",
1599                                 __location__, i);
1600                         return false;
1601                 }
1602         }
1603
1604         torture_comment(tctx, "waiting for replies\n");
1605         while (1) {
1606                 bool unreplied = false;
1607                 for (i=0; i<num_files; i++) {
1608                         if (requests[i] == NULL) {
1609                                 continue;
1610                         }
1611                         if (requests[i]->state < SMBCLI_REQUEST_DONE) {
1612                                 unreplied = true;
1613                                 break;
1614                         }
1615                         status = smb_raw_open_recv(requests[i], mem_ctx,
1616                                                    &ios[i]);
1617
1618                         torture_comment(tctx, "File %d returned status %s\n", i,
1619                                   nt_errstr(status));
1620
1621                         if (NT_STATUS_IS_OK(status)) {
1622                                 num_ok += 1;
1623                         } 
1624
1625                         if (NT_STATUS_EQUAL(status,
1626                                             NT_STATUS_OBJECT_NAME_COLLISION)) {
1627                                 num_collision += 1;
1628                         }
1629
1630                         requests[i] = NULL;
1631                 }
1632                 if (!unreplied) {
1633                         break;
1634                 }
1635
1636                 if (event_loop_once(tctx->ev) != 0) {
1637                         torture_result(tctx, TORTURE_FAIL,
1638                                 "(%s): event_loop_once failed\n", __location__);
1639                         return false;
1640                 }
1641         }
1642
1643         if ((num_ok != 1) || (num_ok + num_collision != num_files)) {
1644                 ret = false;
1645         }
1646
1647         for (i=0; i<num_files; i++) {
1648                 torture_close_connection(clients[i]);
1649         }
1650         talloc_free(mem_ctx);
1651         return ret;
1652 }
1653
1654 /*
1655   test opening for delete on a read-only attribute file.
1656 */
1657 static bool test_open_for_delete(struct torture_context *tctx, struct smbcli_state *cli)
1658 {
1659         union smb_open io;
1660         union smb_fileinfo finfo;
1661         const char *fname = BASEDIR "\\torture_open_for_delete.txt";
1662         NTSTATUS status;
1663         int fnum = -1;
1664         bool ret = true;
1665
1666         if (!torture_setup_dir(cli, BASEDIR)) {
1667                 return false;
1668         }
1669
1670         /* reasonable default parameters */
1671         io.generic.level = RAW_OPEN_NTCREATEX;
1672         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
1673         io.ntcreatex.in.root_fid.fnum = 0;
1674         io.ntcreatex.in.alloc_size = 0;
1675         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
1676         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_READONLY;
1677         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
1678         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
1679         io.ntcreatex.in.create_options = 0;
1680         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1681         io.ntcreatex.in.security_flags = 0;
1682         io.ntcreatex.in.fname = fname;
1683
1684         /* Create the readonly file. */
1685
1686         status = smb_raw_open(cli->tree, tctx, &io);
1687         CHECK_STATUS(status, NT_STATUS_OK);
1688         fnum = io.ntcreatex.out.file.fnum;
1689
1690         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
1691         io.ntcreatex.in.create_options = 0;
1692         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
1693         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
1694         smbcli_close(cli->tree, fnum);
1695
1696         /* Now try and open for delete only - should succeed. */
1697         io.ntcreatex.in.access_mask = SEC_STD_DELETE;
1698         io.ntcreatex.in.file_attr = 0;
1699         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE | NTCREATEX_SHARE_ACCESS_DELETE;
1700         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1701         status = smb_raw_open(cli->tree, tctx, &io);
1702         CHECK_STATUS(status, NT_STATUS_OK);
1703
1704         smbcli_unlink(cli->tree, fname);
1705
1706 done:
1707         smbcli_close(cli->tree, fnum);
1708         smbcli_deltree(cli->tree, BASEDIR);
1709
1710         return ret;
1711 }
1712
1713 /*
1714   test chained RAW_OPEN_NTCREATEX_READX
1715   Send chained NTCREATEX_READX on a file that doesn't exist, then create
1716   the file and try again.
1717 */
1718 static bool test_chained_ntcreatex_readx(struct torture_context *tctx, struct smbcli_state *cli)
1719 {
1720         TALLOC_CTX *mem_ctx = talloc_new(tctx);
1721         union smb_open io;
1722         const char *fname = BASEDIR "\\torture_chained.txt";
1723         NTSTATUS status;
1724         int fnum = -1;
1725         bool ret = true;
1726         const char *buf = "test";
1727         char buf2[4];
1728
1729         if (!torture_setup_dir(cli, BASEDIR)) {
1730                 return false;
1731         }
1732
1733         torture_comment(tctx, "Checking RAW_NTCREATEX_READX chained on "
1734                               "non-existant file \n");
1735
1736         /* ntcreatex parameters */
1737         io.generic.level = RAW_OPEN_NTCREATEX_READX;
1738         io.ntcreatexreadx.in.flags = 0;
1739         io.ntcreatexreadx.in.root_fid.fnum = 0;
1740         io.ntcreatexreadx.in.access_mask = SEC_FILE_READ_DATA;
1741         io.ntcreatexreadx.in.alloc_size = 0;
1742         io.ntcreatexreadx.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1743         io.ntcreatexreadx.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1744                 NTCREATEX_SHARE_ACCESS_WRITE | NTCREATEX_SHARE_ACCESS_DELETE;
1745         io.ntcreatexreadx.in.open_disposition = NTCREATEX_DISP_OPEN;
1746         io.ntcreatexreadx.in.create_options = 0;
1747         io.ntcreatexreadx.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
1748         io.ntcreatexreadx.in.security_flags = 0;
1749         io.ntcreatexreadx.in.fname = fname;
1750
1751         /* readx parameters */
1752         io.ntcreatexreadx.in.offset = 0;
1753         io.ntcreatexreadx.in.mincnt = sizeof(buf);
1754         io.ntcreatexreadx.in.maxcnt = sizeof(buf);
1755         io.ntcreatexreadx.in.remaining = 0;
1756         io.ntcreatexreadx.out.data = (uint8_t *)buf2;
1757
1758         /* try to open the non-existant file */
1759         status = smb_raw_open(cli->tree, mem_ctx, &io);
1760         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1761         fnum = io.ntcreatexreadx.out.file.fnum;
1762
1763         smbcli_close(cli->tree, fnum);
1764         smbcli_unlink(cli->tree, fname);
1765
1766         torture_comment(tctx, "Checking RAW_NTCREATEX_READX chained on "
1767                               "existing file \n");
1768
1769         fnum = create_complex_file(cli, mem_ctx, fname);
1770         smbcli_write(cli->tree, fnum, 0, buf, 0, sizeof(buf));
1771         smbcli_close(cli->tree, fnum);
1772
1773         status = smb_raw_open(cli->tree, mem_ctx, &io);
1774         CHECK_STATUS(status, NT_STATUS_OK);
1775         fnum = io.ntcreatexreadx.out.file.fnum;
1776
1777         if (memcmp(buf, buf2, sizeof(buf)) != 0) {
1778                 torture_result(tctx, TORTURE_FAIL,
1779                         "(%s): wrong data in reply buffer\n", __location__);
1780                 ret = false;
1781         }
1782
1783 done:
1784         smbcli_close(cli->tree, fnum);
1785         smbcli_deltree(cli->tree, BASEDIR);
1786         talloc_free(mem_ctx);
1787
1788         return ret;
1789 }
1790
1791 static bool test_ntcreatex_opendisp_dir(struct torture_context *tctx,
1792                                         struct smbcli_state *cli)
1793 {
1794         const char *dname = BASEDIR "\\torture_ntcreatex_opendisp_dir";
1795         NTSTATUS status;
1796         bool ret = true;
1797         int i;
1798         struct {
1799                 uint32_t open_disp;
1800                 bool dir_exists;
1801                 NTSTATUS correct_status;
1802         } open_funcs_dir[] = {
1803                 { NTCREATEX_DISP_SUPERSEDE,     true,  NT_STATUS_INVALID_PARAMETER },
1804                 { NTCREATEX_DISP_SUPERSEDE,     false, NT_STATUS_INVALID_PARAMETER },
1805                 { NTCREATEX_DISP_OPEN,          true,  NT_STATUS_OK },
1806                 { NTCREATEX_DISP_OPEN,          false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
1807                 { NTCREATEX_DISP_CREATE,        true,  NT_STATUS_OBJECT_NAME_COLLISION },
1808                 { NTCREATEX_DISP_CREATE,        false, NT_STATUS_OK },
1809                 { NTCREATEX_DISP_OPEN_IF,       true,  NT_STATUS_OK },
1810                 { NTCREATEX_DISP_OPEN_IF,       false, NT_STATUS_OK },
1811                 { NTCREATEX_DISP_OVERWRITE,     true,  NT_STATUS_INVALID_PARAMETER },
1812                 { NTCREATEX_DISP_OVERWRITE,     false, NT_STATUS_INVALID_PARAMETER },
1813                 { NTCREATEX_DISP_OVERWRITE_IF,  true,  NT_STATUS_INVALID_PARAMETER },
1814                 { NTCREATEX_DISP_OVERWRITE_IF,  false, NT_STATUS_INVALID_PARAMETER },
1815                 { 6,                            true,  NT_STATUS_INVALID_PARAMETER },
1816                 { 6,                            false, NT_STATUS_INVALID_PARAMETER },
1817         };
1818         union smb_open io;
1819
1820         ZERO_STRUCT(io);
1821         io.generic.level = RAW_OPEN_NTCREATEX;
1822         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
1823         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1824         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
1825         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1826         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1827         io.ntcreatex.in.fname = dname;
1828
1829         if (!torture_setup_dir(cli, BASEDIR)) {
1830                 return false;
1831         }
1832
1833         smbcli_rmdir(cli->tree, dname);
1834         smbcli_unlink(cli->tree, dname);
1835
1836         /* test the open disposition for directories */
1837         torture_comment(tctx, "Testing open dispositions for directories...\n");
1838
1839         for (i=0; i<ARRAY_SIZE(open_funcs_dir); i++) {
1840                 if (open_funcs_dir[i].dir_exists) {
1841                         status = smbcli_mkdir(cli->tree, dname);
1842                         if (!NT_STATUS_IS_OK(status)) {
1843                                 torture_result(tctx, TORTURE_FAIL,
1844                                         "(%s): Failed to make directory "
1845                                         "%s - %s\n", __location__, dname,
1846                                         smbcli_errstr(cli->tree));
1847                                 ret = false;
1848                                 goto done;
1849                         }
1850                 }
1851
1852                 io.ntcreatex.in.open_disposition = open_funcs_dir[i].open_disp;
1853                 status = smb_raw_open(cli->tree, tctx, &io);
1854                 if (!NT_STATUS_EQUAL(status, open_funcs_dir[i].correct_status)) {
1855                         torture_result(tctx, TORTURE_FAIL,
1856                                 "(%s) incorrect status %s should be %s "
1857                                 "(i=%d dir_exists=%d open_disp=%d)\n",
1858                                 __location__, nt_errstr(status),
1859                                 nt_errstr(open_funcs_dir[i].correct_status),
1860                                 i, (int)open_funcs_dir[i].dir_exists,
1861                                 (int)open_funcs_dir[i].open_disp);
1862                         ret = false;
1863                 }
1864                 if (NT_STATUS_IS_OK(status) || open_funcs_dir[i].dir_exists) {
1865                         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
1866                         smbcli_rmdir(cli->tree, dname);
1867                 }
1868         }
1869
1870 done:
1871         smbcli_deltree(cli->tree, BASEDIR);
1872
1873         return ret;
1874 }
1875
1876 /**
1877  * Test what happens when trying to open a file with directory parameters and
1878  * vice-versa.  Also test that NTCREATEX_OPTIONS_DIRECTORY is treated as
1879  * mandatory and FILE_ATTRIBUTE_DIRECTORY is advisory for directory
1880  * creation/opening.
1881  */
1882 static bool test_ntcreatexdir(struct torture_context *tctx,
1883     struct smbcli_state *cli)
1884 {
1885         union smb_open io;
1886         const char *fname = BASEDIR "\\torture_ntcreatex.txt";
1887         const char *dname = BASEDIR "\\torture_ntcreatex_dir";
1888         NTSTATUS status;
1889         int i;
1890
1891         struct {
1892                 uint32_t open_disp;
1893                 uint32_t file_attr;
1894                 uint32_t create_options;
1895                 NTSTATUS correct_status;
1896         } open_funcs[] = {
1897                 { NTCREATEX_DISP_SUPERSEDE,     0, NTCREATEX_OPTIONS_DIRECTORY,
1898                   NT_STATUS_INVALID_PARAMETER },
1899                 { NTCREATEX_DISP_OPEN,          0, NTCREATEX_OPTIONS_DIRECTORY,
1900                   NT_STATUS_OBJECT_NAME_NOT_FOUND },
1901                 { NTCREATEX_DISP_CREATE,        0, NTCREATEX_OPTIONS_DIRECTORY,
1902                   NT_STATUS_OK },
1903                 { NTCREATEX_DISP_OPEN_IF,       0, NTCREATEX_OPTIONS_DIRECTORY,
1904                   NT_STATUS_OK },
1905                 { NTCREATEX_DISP_OVERWRITE,     0, NTCREATEX_OPTIONS_DIRECTORY,
1906                   NT_STATUS_INVALID_PARAMETER },
1907                 { NTCREATEX_DISP_OVERWRITE_IF,  0, NTCREATEX_OPTIONS_DIRECTORY,
1908                   NT_STATUS_INVALID_PARAMETER },
1909                 { NTCREATEX_DISP_SUPERSEDE,     FILE_ATTRIBUTE_DIRECTORY, 0,
1910                   NT_STATUS_OK },
1911                 { NTCREATEX_DISP_OPEN,          FILE_ATTRIBUTE_DIRECTORY, 0,
1912                   NT_STATUS_OBJECT_NAME_NOT_FOUND },
1913                 { NTCREATEX_DISP_CREATE,        FILE_ATTRIBUTE_DIRECTORY, 0,
1914                   NT_STATUS_OK },
1915                 { NTCREATEX_DISP_OPEN_IF,       FILE_ATTRIBUTE_DIRECTORY, 0,
1916                   NT_STATUS_OK },
1917                 { NTCREATEX_DISP_OVERWRITE,     FILE_ATTRIBUTE_DIRECTORY, 0,
1918                   NT_STATUS_OBJECT_NAME_NOT_FOUND },
1919                 { NTCREATEX_DISP_OVERWRITE_IF,  FILE_ATTRIBUTE_DIRECTORY, 0,
1920                   NT_STATUS_OK },
1921
1922         };
1923
1924         if (!torture_setup_dir(cli, BASEDIR)) {
1925                 return false;
1926         }
1927
1928         /* setup some base params. */
1929         io.generic.level = RAW_OPEN_NTCREATEX;
1930         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
1931         io.ntcreatex.in.root_fid.fnum = 0;
1932         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
1933         io.ntcreatex.in.alloc_size = 0;
1934         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
1935         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1936         io.ntcreatex.in.security_flags = 0;
1937         io.ntcreatex.in.fname = fname;
1938
1939         /*
1940          * Test the validity checking for create dispositions, which is done
1941          * against the requested parameters rather than what's actually on
1942          * disk.
1943          */
1944         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
1945                 io.ntcreatex.in.open_disposition = open_funcs[i].open_disp;
1946                 io.ntcreatex.in.file_attr = open_funcs[i].file_attr;
1947                 io.ntcreatex.in.create_options = open_funcs[i].create_options;
1948                 status = smb_raw_open(cli->tree, tctx, &io);
1949                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
1950                         torture_result(tctx, TORTURE_FAIL,
1951                                 "(%s) incorrect status %s should be %s "
1952                                 "(i=%d open_disp=%d)\n",
1953                                 __location__, nt_errstr(status),
1954                                 nt_errstr(open_funcs[i].correct_status),
1955                                 i, (int)open_funcs[i].open_disp);
1956                         return false;
1957                 }
1958                 /* Close and delete the file. */
1959                 if (NT_STATUS_IS_OK(status)) {
1960                         if (open_funcs[i].create_options != 0) {
1961                                 /* out attrib should be a directory. */
1962                                 torture_assert_int_equal(tctx,
1963                                     io.ntcreatex.out.attrib,
1964                                     FILE_ATTRIBUTE_DIRECTORY, "should have "
1965                                     "created a directory");
1966
1967                                 smbcli_close(cli->tree,
1968                                     io.ntcreatex.out.file.fnum);
1969
1970                                 /* Make sure unlink fails. */
1971                                 status = smbcli_unlink(cli->tree, fname);
1972                                 torture_assert_ntstatus_equal(tctx, status,
1973                                     NT_STATUS_FILE_IS_A_DIRECTORY,
1974                                     "unlink should fail for a directory");
1975
1976                                 status = smbcli_rmdir(cli->tree, fname);
1977                                 torture_assert_ntstatus_ok(tctx, status,
1978                                     "rmdir failed");
1979                         } else {
1980                                 torture_assert_int_equal(tctx,
1981                                     io.ntcreatex.out.attrib,
1982                                     FILE_ATTRIBUTE_ARCHIVE, "should not have "
1983                                     "created a directory");
1984
1985                                 smbcli_close(cli->tree,
1986                                     io.ntcreatex.out.file.fnum);
1987
1988                                 /* Make sure rmdir fails. */
1989                                 status = smbcli_rmdir(cli->tree, fname);
1990                                 torture_assert_ntstatus_equal(tctx, status,
1991                                     NT_STATUS_NOT_A_DIRECTORY,
1992                                     "rmdir should fail for a file");
1993
1994                                 status = smbcli_unlink(cli->tree, fname);
1995                                 torture_assert_ntstatus_ok(tctx, status,
1996                                     "unlink failed");
1997                         }
1998                 }
1999         }
2000
2001         /* Create a file. */
2002         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
2003         io.ntcreatex.in.create_options = 0;
2004         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
2005         status = smb_raw_open(cli->tree, tctx, &io);
2006         torture_assert_ntstatus_ok(tctx, status, "Failed to create file.");
2007         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
2008
2009         /* Try and open the file with file_attr_dir and check the error. */
2010         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
2011         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
2012
2013         status = smb_raw_open(cli->tree, tctx, &io);
2014         torture_assert_ntstatus_ok(tctx, status, "FILE_ATTRIBUTE_DIRECTORY "
2015             "doesn't produce a hard failure.");
2016         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
2017
2018         /* Try and open file with createx_option_dir and check the error. */
2019         io.ntcreatex.in.file_attr = 0;
2020         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
2021
2022         status = smb_raw_open(cli->tree, tctx, &io);
2023         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_NOT_A_DIRECTORY,
2024             "NTCREATEX_OPTIONS_DIRECTORY will a file from being opened.");
2025         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
2026
2027         /* Delete the file and move onto directory testing. */
2028         smbcli_unlink(cli->tree, fname);
2029
2030         /* Now try some tests on a directory. */
2031         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
2032         io.ntcreatex.in.file_attr = 0;
2033         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
2034         io.ntcreatex.in.fname = dname;
2035
2036         status = smb_raw_open(cli->tree, tctx, &io);
2037         torture_assert_ntstatus_ok(tctx, status, "Failed to create dir.");
2038
2039         /* out attrib should be a directory. */
2040         torture_assert_int_equal(tctx, io.ntcreatex.out.attrib,
2041             FILE_ATTRIBUTE_DIRECTORY, "should have created a directory");
2042
2043         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
2044
2045         /* Try and open it with normal attr and check the error. */
2046         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
2047         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
2048
2049         status = smb_raw_open(cli->tree, tctx, &io);
2050         torture_assert_ntstatus_ok(tctx, status, "FILE_ATTRIBUTE_NORMAL "
2051             "doesn't produce a hard failure.");
2052         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
2053
2054         /* Try and open it with file create_options and check the error. */
2055         io.ntcreatex.in.file_attr = 0;
2056         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_NON_DIRECTORY_FILE;
2057
2058         status = smb_raw_open(cli->tree, tctx, &io);
2059         torture_assert_ntstatus_equal(tctx, status,
2060             NT_STATUS_FILE_IS_A_DIRECTORY,
2061             "NTCREATEX_OPTIONS_NON_DIRECTORY_FILE should be returned ");
2062         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
2063
2064         smbcli_deltree(cli->tree, BASEDIR);
2065
2066         return true;
2067 }
2068
2069 /* basic testing of all RAW_OPEN_* calls
2070 */
2071 struct torture_suite *torture_raw_open(TALLOC_CTX *mem_ctx)
2072 {
2073         struct torture_suite *suite = torture_suite_create(mem_ctx, "open");
2074
2075         torture_suite_add_1smb_test(suite, "brlocked", test_ntcreatex_brlocked);
2076         torture_suite_add_1smb_test(suite, "open", test_open);
2077         torture_suite_add_1smb_test(suite, "open-multi", test_raw_open_multi);
2078         torture_suite_add_1smb_test(suite, "openx", test_openx);
2079         torture_suite_add_1smb_test(suite, "ntcreatex", test_ntcreatex);
2080         torture_suite_add_1smb_test(suite, "nttrans-create", test_nttrans_create);
2081         torture_suite_add_1smb_test(suite, "t2open", test_t2open);
2082         torture_suite_add_1smb_test(suite, "mknew", test_mknew);
2083         torture_suite_add_1smb_test(suite, "create", test_create);
2084         torture_suite_add_1smb_test(suite, "ctemp", test_ctemp);
2085         torture_suite_add_1smb_test(suite, "chained-openx", test_chained);
2086         torture_suite_add_1smb_test(suite, "chained-ntcreatex", test_chained_ntcreatex_readx);
2087         torture_suite_add_1smb_test(suite, "no-leading-slash", test_no_leading_slash);
2088         torture_suite_add_1smb_test(suite, "openx-over-dir", test_openx_over_dir);
2089         torture_suite_add_1smb_test(suite, "open-for-delete", test_open_for_delete);
2090         torture_suite_add_1smb_test(suite, "opendisp-dir", test_ntcreatex_opendisp_dir);
2091         torture_suite_add_1smb_test(suite, "ntcreatedir", test_ntcreatexdir);
2092
2093         return suite;
2094 }