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