r24735: Use torture API in more places.
[jelmer/samba4-debian.git] / source / 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 "torture/torture.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "system/time.h"
24 #include "system/filesys.h"
25 #include "librpc/gen_ndr/security.h"
26 #include "lib/events/events.h"
27 #include "libcli/libcli.h"
28 #include "torture/util.h"
29 #include "auth/credentials/credentials.h"
30 #include "lib/cmdline/popt_common.h"
31
32 /* enum for whether reads/writes are possible on a file */
33 enum rdwr_mode {RDWR_NONE, RDWR_RDONLY, RDWR_WRONLY, RDWR_RDWR};
34
35 #define BASEDIR "\\rawopen"
36
37 /*
38   check if a open file can be read/written
39 */
40 static enum rdwr_mode check_rdwr(struct smbcli_tree *tree, int fnum)
41 {
42         uint8_t c = 1;
43         BOOL can_read  = (smbcli_read(tree, fnum, &c, 0, 1) == 1);
44         BOOL can_write = (smbcli_write(tree, fnum, 0, &c, 0, 1) == 1);
45         if ( can_read &&  can_write) return RDWR_RDWR;
46         if ( can_read && !can_write) return RDWR_RDONLY;
47         if (!can_read &&  can_write) return RDWR_WRONLY;
48         return RDWR_NONE;
49 }
50
51 /*
52   describe a RDWR mode as a string
53 */
54 static const char *rdwr_string(enum rdwr_mode m)
55 {
56         switch (m) {
57         case RDWR_NONE: return "NONE";
58         case RDWR_RDONLY: return "RDONLY";
59         case RDWR_WRONLY: return "WRONLY";
60         case RDWR_RDWR: return "RDWR";
61         }
62         return "-";
63 }
64
65 #define CHECK_STATUS(status, correct) do { \
66         if (!NT_STATUS_EQUAL(status, correct)) { \
67                 printf("(%s) Incorrect status %s - should be %s\n", \
68                        __location__, nt_errstr(status), nt_errstr(correct)); \
69                 ret = False; \
70                 goto done; \
71         }} while (0)
72
73 #define CREATE_FILE do { \
74         fnum = create_complex_file(cli, mem_ctx, fname); \
75         if (fnum == -1) { \
76                 printf("(%s) Failed to create %s - %s\n", __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                 printf("(%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, mem_ctx, &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                 printf("(%s) wrong time for field %s  %s - %s\n", \
99                        __location__, #field, \
100                        timestring(mem_ctx, t1), \
101                        timestring(mem_ctx, t2)); \
102                 dump_all_info(mem_ctx, &finfo); \
103                 ret = False; \
104         }} while (0)
105
106 #define CHECK_NTTIME(t, field) do { \
107         NTTIME t2; \
108         finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
109         finfo.all_info.in.file.path = fname; \
110         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo); \
111         CHECK_STATUS(status, NT_STATUS_OK); \
112         t2 = finfo.all_info.out.field; \
113         if (t != t2) { \
114                 printf("(%s) wrong time for field %s  %s - %s\n", \
115                        __location__, #field, \
116                        nt_time_string(mem_ctx, t), \
117                        nt_time_string(mem_ctx, t2)); \
118                 dump_all_info(mem_ctx, &finfo); \
119                 ret = False; \
120         }} while (0)
121
122 #define CHECK_ALL_INFO(v, field) do { \
123         finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
124         finfo.all_info.in.file.path = fname; \
125         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo); \
126         CHECK_STATUS(status, NT_STATUS_OK); \
127         if ((v) != (finfo.all_info.out.field)) { \
128                 printf("(%s) wrong value for field %s  0x%x - 0x%x\n", \
129                        __location__, #field, (int)v, (int)(finfo.all_info.out.field)); \
130                 dump_all_info(mem_ctx, &finfo); \
131                 ret = False; \
132         }} while (0)
133
134 #define CHECK_VAL(v, correct) do { \
135         if ((v) != (correct)) { \
136                 printf("(%s) wrong value for %s  0x%x - should be 0x%x\n", \
137                        __location__, #v, (int)(v), (int)correct); \
138                 ret = False; \
139         }} while (0)
140
141 #define SET_ATTRIB(sattrib) do { \
142         union smb_setfileinfo sfinfo; \
143         ZERO_STRUCT(sfinfo.basic_info.in); \
144         sfinfo.basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION; \
145         sfinfo.basic_info.in.file.path = fname; \
146         sfinfo.basic_info.in.attrib = sattrib; \
147         status = smb_raw_setpathinfo(cli->tree, &sfinfo); \
148         if (!NT_STATUS_IS_OK(status)) { \
149                 printf("(%s) Failed to set attrib 0x%x on %s\n", \
150                        __location__, sattrib, fname); \
151         }} while (0)
152
153 /*
154   test RAW_OPEN_OPEN
155 */
156 static BOOL test_open(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
157 {
158         union smb_open io;
159         union smb_fileinfo finfo;
160         const char *fname = BASEDIR "\\torture_open.txt";
161         NTSTATUS status;
162         int fnum = -1, fnum2;
163         BOOL ret = True;
164
165         printf("Checking RAW_OPEN_OPEN\n");
166
167         io.openold.level = RAW_OPEN_OPEN;
168         io.openold.in.fname = fname;
169         io.openold.in.open_mode = OPEN_FLAGS_FCB;
170         io.openold.in.search_attrs = 0;
171         status = smb_raw_open(cli->tree, mem_ctx, &io);
172         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
173         fnum = io.openold.out.file.fnum;
174
175         smbcli_unlink(cli->tree, fname);
176         CREATE_FILE;
177         smbcli_close(cli->tree, fnum);
178
179         status = smb_raw_open(cli->tree, mem_ctx, &io);
180         CHECK_STATUS(status, NT_STATUS_OK);
181         fnum = io.openold.out.file.fnum;
182         CHECK_RDWR(fnum, RDWR_RDWR);
183
184         status = smb_raw_open(cli->tree, mem_ctx, &io);
185         CHECK_STATUS(status, NT_STATUS_OK);
186         fnum2 = io.openold.out.file.fnum;
187         CHECK_RDWR(fnum2, RDWR_RDWR);
188         smbcli_close(cli->tree, fnum2);
189         smbcli_close(cli->tree, fnum);
190
191         /* check the read/write modes */
192         io.openold.level = RAW_OPEN_OPEN;
193         io.openold.in.fname = fname;
194         io.openold.in.search_attrs = 0;
195
196         io.openold.in.open_mode = OPEN_FLAGS_OPEN_READ;
197         status = smb_raw_open(cli->tree, mem_ctx, &io);
198         CHECK_STATUS(status, NT_STATUS_OK);
199         fnum = io.openold.out.file.fnum;
200         CHECK_RDWR(fnum, RDWR_RDONLY);
201         smbcli_close(cli->tree, fnum);
202
203         io.openold.in.open_mode = OPEN_FLAGS_OPEN_WRITE;
204         status = smb_raw_open(cli->tree, mem_ctx, &io);
205         CHECK_STATUS(status, NT_STATUS_OK);
206         fnum = io.openold.out.file.fnum;
207         CHECK_RDWR(fnum, RDWR_WRONLY);
208         smbcli_close(cli->tree, fnum);
209
210         io.openold.in.open_mode = OPEN_FLAGS_OPEN_RDWR;
211         status = smb_raw_open(cli->tree, mem_ctx, &io);
212         CHECK_STATUS(status, NT_STATUS_OK);
213         fnum = io.openold.out.file.fnum;
214         CHECK_RDWR(fnum, RDWR_RDWR);
215         smbcli_close(cli->tree, fnum);
216
217         /* check the share modes roughly - not a complete matrix */
218         io.openold.in.open_mode = OPEN_FLAGS_OPEN_RDWR | OPEN_FLAGS_DENY_WRITE;
219         status = smb_raw_open(cli->tree, mem_ctx, &io);
220         CHECK_STATUS(status, NT_STATUS_OK);
221         fnum = io.openold.out.file.fnum;
222         CHECK_RDWR(fnum, RDWR_RDWR);
223         
224         if (io.openold.in.open_mode != io.openold.out.rmode) {
225                 printf("(%s) rmode should equal open_mode - 0x%x 0x%x\n",
226                        __location__, io.openold.out.rmode, io.openold.in.open_mode);
227         }
228
229         io.openold.in.open_mode = OPEN_FLAGS_OPEN_RDWR | OPEN_FLAGS_DENY_NONE;
230         status = smb_raw_open(cli->tree, mem_ctx, &io);
231         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
232
233         io.openold.in.open_mode = OPEN_FLAGS_OPEN_READ | OPEN_FLAGS_DENY_NONE;
234         status = smb_raw_open(cli->tree, mem_ctx, &io);
235         CHECK_STATUS(status, NT_STATUS_OK);
236         fnum2 = io.openold.out.file.fnum;
237         CHECK_RDWR(fnum2, RDWR_RDONLY);
238         smbcli_close(cli->tree, fnum);
239         smbcli_close(cli->tree, fnum2);
240
241
242         /* check the returned write time */
243         io.openold.level = RAW_OPEN_OPEN;
244         io.openold.in.fname = fname;
245         io.openold.in.search_attrs = 0;
246         io.openold.in.open_mode = OPEN_FLAGS_OPEN_READ;
247         status = smb_raw_open(cli->tree, mem_ctx, &io);
248         CHECK_STATUS(status, NT_STATUS_OK);
249         fnum = io.openold.out.file.fnum;
250
251         /* check other reply fields */
252         CHECK_TIME(io.openold.out.write_time, write_time);
253         CHECK_ALL_INFO(io.openold.out.size, size);
254         CHECK_ALL_INFO(io.openold.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
255
256 done:
257         smbcli_close(cli->tree, fnum);
258         smbcli_unlink(cli->tree, fname);
259
260         return ret;
261 }
262
263
264 /*
265   test RAW_OPEN_OPENX
266 */
267 static BOOL test_openx(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
268 {
269         union smb_open io;
270         union smb_fileinfo finfo;
271         const char *fname = BASEDIR "\\torture_openx.txt";
272         const char *fname_exe = BASEDIR "\\torture_openx.exe";
273         NTSTATUS status;
274         int fnum = -1, fnum2;
275         BOOL ret = True;
276         int i;
277         struct timeval tv;
278         struct {
279                 uint16_t open_func;
280                 BOOL with_file;
281                 NTSTATUS correct_status;
282         } open_funcs[] = {
283                 { OPENX_OPEN_FUNC_OPEN,                           True,  NT_STATUS_OK },
284                 { OPENX_OPEN_FUNC_OPEN,                           False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
285                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OK },
286                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_OK },
287                 { OPENX_OPEN_FUNC_FAIL,                           True,  NT_STATUS_DOS(ERRDOS, ERRbadaccess) },
288                 { OPENX_OPEN_FUNC_FAIL,                           False, NT_STATUS_DOS(ERRDOS, ERRbadaccess) },
289                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OBJECT_NAME_COLLISION },
290                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_OK },
291                 { OPENX_OPEN_FUNC_TRUNC,                          True,  NT_STATUS_OK },
292                 { OPENX_OPEN_FUNC_TRUNC,                          False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
293                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OK },
294                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_OK },
295         };
296
297         printf("Checking RAW_OPEN_OPENX\n");
298         smbcli_unlink(cli->tree, fname);
299
300         io.openx.level = RAW_OPEN_OPENX;
301         io.openx.in.fname = fname;
302         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
303         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
304         io.openx.in.search_attrs = 0;
305         io.openx.in.file_attrs = 0;
306         io.openx.in.write_time = 0;
307         io.openx.in.size = 1024*1024;
308         io.openx.in.timeout = 0;
309
310         /* check all combinations of open_func */
311         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
312                 if (open_funcs[i].with_file) {
313                         fnum = create_complex_file(cli, mem_ctx, fname);
314                         if (fnum == -1) {
315                                 d_printf("Failed to create file %s - %s\n", fname, smbcli_errstr(cli->tree));
316                                 ret = False;
317                                 goto done;
318                         }
319                         smbcli_close(cli->tree, fnum);
320                 }
321                 io.openx.in.open_func = open_funcs[i].open_func;
322                 status = smb_raw_open(cli->tree, mem_ctx, &io);
323                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
324                         printf("(%s) incorrect status %s should be %s (i=%d with_file=%d open_func=0x%x)\n", 
325                                __location__, nt_errstr(status), nt_errstr(open_funcs[i].correct_status),
326                                i, (int)open_funcs[i].with_file, (int)open_funcs[i].open_func);
327                         ret = False;
328                 }
329                 if (NT_STATUS_IS_OK(status)) {
330                         smbcli_close(cli->tree, io.openx.out.file.fnum);
331                 }
332                 if (open_funcs[i].with_file) {
333                         smbcli_unlink(cli->tree, fname);
334                 }
335         }
336
337         smbcli_unlink(cli->tree, fname);
338
339         /* check the basic return fields */
340         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
341         status = smb_raw_open(cli->tree, mem_ctx, &io);
342         CHECK_STATUS(status, NT_STATUS_OK);
343         fnum = io.openx.out.file.fnum;
344
345         CHECK_ALL_INFO(io.openx.out.size, size);
346         CHECK_TIME(io.openx.out.write_time, write_time);
347         CHECK_ALL_INFO(io.openx.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
348         CHECK_VAL(io.openx.out.access, OPENX_MODE_ACCESS_RDWR);
349         CHECK_VAL(io.openx.out.ftype, 0);
350         CHECK_VAL(io.openx.out.devstate, 0);
351         CHECK_VAL(io.openx.out.action, OPENX_ACTION_CREATED);
352         CHECK_VAL(io.openx.out.size, 1024*1024);
353         CHECK_ALL_INFO(io.openx.in.size, size);
354         smbcli_close(cli->tree, fnum);
355         smbcli_unlink(cli->tree, fname);
356
357         /* check the fields when the file already existed */
358         fnum2 = create_complex_file(cli, mem_ctx, fname);
359         if (fnum2 == -1) {
360                 ret = False;
361                 goto done;
362         }
363         smbcli_close(cli->tree, fnum2);
364
365         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
366         status = smb_raw_open(cli->tree, mem_ctx, &io);
367         CHECK_STATUS(status, NT_STATUS_OK);
368         fnum = io.openx.out.file.fnum;
369
370         CHECK_ALL_INFO(io.openx.out.size, size);
371         CHECK_TIME(io.openx.out.write_time, write_time);
372         CHECK_VAL(io.openx.out.action, OPENX_ACTION_EXISTED);
373         CHECK_VAL(io.openx.out.unknown, 0);
374         CHECK_ALL_INFO(io.openx.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
375         smbcli_close(cli->tree, fnum);
376
377         /* now check the search attrib for hidden files - win2003 ignores this? */
378         SET_ATTRIB(FILE_ATTRIBUTE_HIDDEN);
379         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN, attrib);
380
381         io.openx.in.search_attrs = FILE_ATTRIBUTE_HIDDEN;
382         status = smb_raw_open(cli->tree, mem_ctx, &io);
383         CHECK_STATUS(status, NT_STATUS_OK);
384         smbcli_close(cli->tree, io.openx.out.file.fnum);
385
386         io.openx.in.search_attrs = 0;
387         status = smb_raw_open(cli->tree, mem_ctx, &io);
388         CHECK_STATUS(status, NT_STATUS_OK);
389         smbcli_close(cli->tree, io.openx.out.file.fnum);
390
391         SET_ATTRIB(FILE_ATTRIBUTE_NORMAL);
392         smbcli_unlink(cli->tree, fname);
393
394         /* and check attrib on create */
395         io.openx.in.open_func = OPENX_OPEN_FUNC_FAIL | OPENX_OPEN_FUNC_CREATE;
396         io.openx.in.search_attrs = 0;
397         io.openx.in.file_attrs = FILE_ATTRIBUTE_SYSTEM;
398         status = smb_raw_open(cli->tree, mem_ctx, &io);
399         CHECK_STATUS(status, NT_STATUS_OK);
400         if (lp_parm_bool(-1, "torture", "samba3", False)) {
401                 CHECK_ALL_INFO(FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE, 
402                                attrib & ~(FILE_ATTRIBUTE_NONINDEXED|
403                                           FILE_ATTRIBUTE_SPARSE));
404         }
405         else {
406                 CHECK_ALL_INFO(FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE, 
407                                attrib & ~(FILE_ATTRIBUTE_NONINDEXED));
408         }
409         smbcli_close(cli->tree, io.openx.out.file.fnum);
410         smbcli_unlink(cli->tree, fname);
411
412         /* check timeout on create - win2003 ignores the timeout! */
413         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
414         io.openx.in.file_attrs = 0;
415         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_ALL;
416         status = smb_raw_open(cli->tree, mem_ctx, &io);
417         CHECK_STATUS(status, NT_STATUS_OK);
418         fnum = io.openx.out.file.fnum;
419
420         io.openx.in.timeout = 20000;
421         tv = timeval_current();
422         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_NONE;
423         status = smb_raw_open(cli->tree, mem_ctx, &io);
424         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
425         if (timeval_elapsed(&tv) > 3.0) {
426                 printf("(%s) Incorrect timing in openx with timeout - waited %.2f seconds\n",
427                        __location__, timeval_elapsed(&tv));
428                 ret = False;
429         }
430         smbcli_close(cli->tree, fnum);
431         smbcli_unlink(cli->tree, fname);
432
433         /* now this is a really weird one - open for execute implies create?! */
434         io.openx.in.fname = fname;
435         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
436         io.openx.in.open_mode = OPENX_MODE_ACCESS_EXEC | OPENX_MODE_DENY_NONE;
437         io.openx.in.search_attrs = 0;
438         io.openx.in.open_func = OPENX_OPEN_FUNC_FAIL;
439         io.openx.in.file_attrs = 0;
440         io.openx.in.write_time = 0;
441         io.openx.in.size = 0;
442         io.openx.in.timeout = 0;
443         status = smb_raw_open(cli->tree, mem_ctx, &io);
444         CHECK_STATUS(status, NT_STATUS_OK);
445         smbcli_close(cli->tree, io.openx.out.file.fnum);
446
447         /* check the extended return flag */
448         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO | OPENX_FLAGS_EXTENDED_RETURN;
449         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
450         status = smb_raw_open(cli->tree, mem_ctx, &io);
451         CHECK_STATUS(status, NT_STATUS_OK);
452         CHECK_VAL(io.openx.out.access_mask, SEC_STD_ALL);
453         smbcli_close(cli->tree, io.openx.out.file.fnum);
454
455         io.openx.in.fname = "\\A.+,;=[].B";
456         status = smb_raw_open(cli->tree, mem_ctx, &io);
457         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
458
459         /* Check the mapping for open exec. */
460
461         /* First create an .exe file. */
462         smbcli_unlink(cli->tree, fname_exe);
463         fnum = create_complex_file(cli, mem_ctx, fname_exe);
464         smbcli_close(cli->tree, fnum);
465
466         io.openx.level = RAW_OPEN_OPENX;
467         io.openx.in.fname = fname_exe;
468         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
469         io.openx.in.open_mode = OPENX_MODE_ACCESS_EXEC | OPENX_MODE_DENY_NONE;
470         io.openx.in.search_attrs = 0;
471         io.openx.in.file_attrs = 0;
472         io.openx.in.write_time = 0;
473         io.openx.in.size = 0;
474         io.openx.in.timeout = 0;
475         status = smb_raw_open(cli->tree, mem_ctx, &io);
476         CHECK_STATUS(status, NT_STATUS_OK);
477
478         /* Can we read and write ? */
479         CHECK_RDWR(io.openx.out.file.fnum, RDWR_RDONLY);
480         smbcli_close(cli->tree, io.openx.out.file.fnum);
481         smbcli_unlink(cli->tree, fname);
482
483 done:
484         smbcli_close(cli->tree, fnum);
485         smbcli_unlink(cli->tree, fname_exe);
486         smbcli_unlink(cli->tree, fname);
487
488         return ret;
489 }
490
491
492 /*
493   test RAW_OPEN_T2OPEN
494
495   many thanks to kukks for a sniff showing how this works with os2->w2k
496 */
497 static BOOL test_t2open(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
498 {
499         union smb_open io;
500         union smb_fileinfo finfo;
501         const char *fname1 = BASEDIR "\\torture_t2open_yes.txt";
502         const char *fname2 = BASEDIR "\\torture_t2open_no.txt";
503         const char *fname = BASEDIR "\\torture_t2open_3.txt";
504         NTSTATUS status;
505         int fnum;
506         BOOL ret = True;
507         int i;
508         struct {
509                 uint16_t open_func;
510                 BOOL with_file;
511                 NTSTATUS correct_status;
512         } open_funcs[] = {
513                 { OPENX_OPEN_FUNC_OPEN,                           True,  NT_STATUS_OK },
514                 { OPENX_OPEN_FUNC_OPEN,                           False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
515                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OK },
516                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_OK },
517                 { OPENX_OPEN_FUNC_FAIL,                           True,  NT_STATUS_OBJECT_NAME_COLLISION },
518                 { OPENX_OPEN_FUNC_FAIL,                           False, NT_STATUS_OBJECT_NAME_COLLISION },
519                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OBJECT_NAME_COLLISION },
520                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_OBJECT_NAME_COLLISION },
521                 { OPENX_OPEN_FUNC_TRUNC,                          True,  NT_STATUS_OK },
522                 { OPENX_OPEN_FUNC_TRUNC,                          False, NT_STATUS_OK },
523                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OK },
524                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_OK },
525         };
526
527         fnum = create_complex_file(cli, mem_ctx, fname1);
528         if (fnum == -1) {
529                 d_printf("Failed to create file %s - %s\n", fname1, smbcli_errstr(cli->tree));
530                 ret = False;
531                 goto done;
532         }
533         smbcli_close(cli->tree, fnum);
534
535         printf("Checking RAW_OPEN_T2OPEN\n");
536
537         io.t2open.level = RAW_OPEN_T2OPEN;
538         io.t2open.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
539         io.t2open.in.open_mode = OPENX_MODE_DENY_NONE | OPENX_MODE_ACCESS_RDWR;
540         io.t2open.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
541         io.t2open.in.search_attrs = 0;
542         io.t2open.in.file_attrs = 0;
543         io.t2open.in.write_time = 0;
544         io.t2open.in.size = 0;
545         io.t2open.in.timeout = 0;
546
547         io.t2open.in.num_eas = 3;
548         io.t2open.in.eas = talloc_array(mem_ctx, struct ea_struct, io.t2open.in.num_eas);
549         io.t2open.in.eas[0].flags = 0;
550         io.t2open.in.eas[0].name.s = ".CLASSINFO";
551         io.t2open.in.eas[0].value = data_blob_talloc(mem_ctx, "first value", 11);
552         io.t2open.in.eas[1].flags = 0;
553         io.t2open.in.eas[1].name.s = "EA TWO";
554         io.t2open.in.eas[1].value = data_blob_talloc(mem_ctx, "foo", 3);
555         io.t2open.in.eas[2].flags = 0;
556         io.t2open.in.eas[2].name.s = "X THIRD";
557         io.t2open.in.eas[2].value = data_blob_talloc(mem_ctx, "xy", 2);
558
559         /* check all combinations of open_func */
560         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
561         again:
562                 if (open_funcs[i].with_file) {
563                         io.t2open.in.fname = fname1;
564                 } else {
565                         io.t2open.in.fname = fname2;
566                 }
567                 io.t2open.in.open_func = open_funcs[i].open_func;
568                 status = smb_raw_open(cli->tree, mem_ctx, &io);
569                 if ((io.t2open.in.num_eas != 0)
570                     && NT_STATUS_EQUAL(status, NT_STATUS_EAS_NOT_SUPPORTED)
571                     && lp_parm_bool(-1, "torture", "samba3", False)) {
572                         printf("(%s) EAs not supported, not treating as fatal "
573                                "in Samba3 test\n", __location__);
574                         io.t2open.in.num_eas = 0;
575                         goto again;
576                 }
577
578                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
579                         printf("(%s) incorrect status %s should be %s (i=%d with_file=%d open_func=0x%x)\n", 
580                                __location__, nt_errstr(status), nt_errstr(open_funcs[i].correct_status),
581                                i, (int)open_funcs[i].with_file, (int)open_funcs[i].open_func);
582                         ret = False;
583                 }
584                 if (NT_STATUS_IS_OK(status)) {
585                         smbcli_close(cli->tree, io.t2open.out.file.fnum);
586                 }
587         }
588
589         smbcli_unlink(cli->tree, fname1);
590         smbcli_unlink(cli->tree, fname2);
591
592         /* check the basic return fields */
593         io.t2open.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
594         io.t2open.in.write_time = 0;
595         io.t2open.in.fname = fname;
596         status = smb_raw_open(cli->tree, mem_ctx, &io);
597         CHECK_STATUS(status, NT_STATUS_OK);
598         fnum = io.t2open.out.file.fnum;
599
600         CHECK_ALL_INFO(io.t2open.out.size, size);
601 #if 0
602         /* windows appears to leak uninitialised memory here */
603         CHECK_VAL(io.t2open.out.write_time, 0);
604 #endif
605         CHECK_ALL_INFO(io.t2open.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
606         CHECK_VAL(io.t2open.out.access, OPENX_MODE_DENY_NONE | OPENX_MODE_ACCESS_RDWR);
607         CHECK_VAL(io.t2open.out.ftype, 0);
608         CHECK_VAL(io.t2open.out.devstate, 0);
609         CHECK_VAL(io.t2open.out.action, OPENX_ACTION_CREATED);
610         smbcli_close(cli->tree, fnum);
611
612         status = torture_check_ea(cli, fname, ".CLASSINFO", "first value");
613         CHECK_STATUS(status, io.t2open.in.num_eas
614                      ? NT_STATUS_OK : NT_STATUS_EAS_NOT_SUPPORTED);
615         status = torture_check_ea(cli, fname, "EA TWO", "foo");
616         CHECK_STATUS(status, io.t2open.in.num_eas
617                      ? NT_STATUS_OK : NT_STATUS_EAS_NOT_SUPPORTED);
618         status = torture_check_ea(cli, fname, "X THIRD", "xy");
619         CHECK_STATUS(status, io.t2open.in.num_eas
620                      ? NT_STATUS_OK : NT_STATUS_EAS_NOT_SUPPORTED);
621
622         /* now check the search attrib for hidden files - win2003 ignores this? */
623         SET_ATTRIB(FILE_ATTRIBUTE_HIDDEN);
624         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN, attrib);
625
626         status = smb_raw_open(cli->tree, mem_ctx, &io);
627         CHECK_STATUS(status, NT_STATUS_OK);
628         smbcli_close(cli->tree, io.t2open.out.file.fnum);
629
630         status = smb_raw_open(cli->tree, mem_ctx, &io);
631         CHECK_STATUS(status, NT_STATUS_OK);
632         smbcli_close(cli->tree, io.t2open.out.file.fnum);
633
634         SET_ATTRIB(FILE_ATTRIBUTE_NORMAL);
635         smbcli_unlink(cli->tree, fname);
636
637         /* and check attrib on create */
638         io.t2open.in.open_func = OPENX_OPEN_FUNC_FAIL | OPENX_OPEN_FUNC_CREATE;
639         io.t2open.in.file_attrs = FILE_ATTRIBUTE_SYSTEM;
640         status = smb_raw_open(cli->tree, mem_ctx, &io);
641         CHECK_STATUS(status, NT_STATUS_OK);
642
643         /* check timeout on create - win2003 ignores the timeout! */
644         io.t2open.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
645         io.t2open.in.file_attrs = 0;
646         io.t2open.in.timeout = 20000;
647         io.t2open.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_ALL;
648         status = smb_raw_open(cli->tree, mem_ctx, &io);
649         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
650
651 done:
652         smbcli_close(cli->tree, fnum);
653         smbcli_unlink(cli->tree, fname);
654
655         return ret;
656 }
657         
658
659 /*
660   test RAW_OPEN_NTCREATEX
661 */
662 static BOOL test_ntcreatex(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
663 {
664         union smb_open io;
665         union smb_fileinfo finfo;
666         const char *fname = BASEDIR "\\torture_ntcreatex.txt";
667         const char *dname = BASEDIR "\\torture_ntcreatex.dir";
668         NTSTATUS status;
669         int fnum = -1;
670         BOOL ret = True;
671         int i;
672         struct {
673                 uint32_t open_disp;
674                 BOOL with_file;
675                 NTSTATUS correct_status;
676         } open_funcs[] = {
677                 { NTCREATEX_DISP_SUPERSEDE,     True,  NT_STATUS_OK },
678                 { NTCREATEX_DISP_SUPERSEDE,     False, NT_STATUS_OK },
679                 { NTCREATEX_DISP_OPEN,          True,  NT_STATUS_OK },
680                 { NTCREATEX_DISP_OPEN,          False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
681                 { NTCREATEX_DISP_CREATE,        True,  NT_STATUS_OBJECT_NAME_COLLISION },
682                 { NTCREATEX_DISP_CREATE,        False, NT_STATUS_OK },
683                 { NTCREATEX_DISP_OPEN_IF,       True,  NT_STATUS_OK },
684                 { NTCREATEX_DISP_OPEN_IF,       False, NT_STATUS_OK },
685                 { NTCREATEX_DISP_OVERWRITE,     True,  NT_STATUS_OK },
686                 { NTCREATEX_DISP_OVERWRITE,     False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
687                 { NTCREATEX_DISP_OVERWRITE_IF,  True,  NT_STATUS_OK },
688                 { NTCREATEX_DISP_OVERWRITE_IF,  False, NT_STATUS_OK },
689                 { 6,                            True,  NT_STATUS_INVALID_PARAMETER },
690                 { 6,                            False, NT_STATUS_INVALID_PARAMETER },
691         };
692
693         printf("Checking RAW_OPEN_NTCREATEX\n");
694
695         /* reasonable default parameters */
696         io.generic.level = RAW_OPEN_NTCREATEX;
697         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
698         io.ntcreatex.in.root_fid = 0;
699         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
700         io.ntcreatex.in.alloc_size = 1024*1024;
701         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
702         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
703         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
704         io.ntcreatex.in.create_options = 0;
705         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
706         io.ntcreatex.in.security_flags = 0;
707         io.ntcreatex.in.fname = fname;
708
709         /* test the open disposition */
710         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
711                 if (open_funcs[i].with_file) {
712                         fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR|O_TRUNC, DENY_NONE);
713                         if (fnum == -1) {
714                                 d_printf("Failed to create file %s - %s\n", fname, smbcli_errstr(cli->tree));
715                                 ret = False;
716                                 goto done;
717                         }
718                         smbcli_close(cli->tree, fnum);
719                 }
720                 io.ntcreatex.in.open_disposition = open_funcs[i].open_disp;
721                 status = smb_raw_open(cli->tree, mem_ctx, &io);
722                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
723                         printf("(%s) incorrect status %s should be %s (i=%d with_file=%d open_disp=%d)\n", 
724                                __location__, nt_errstr(status), nt_errstr(open_funcs[i].correct_status),
725                                i, (int)open_funcs[i].with_file, (int)open_funcs[i].open_disp);
726                         ret = False;
727                 }
728                 if (NT_STATUS_IS_OK(status) || open_funcs[i].with_file) {
729                         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
730                         smbcli_unlink(cli->tree, fname);
731                 }
732         }
733
734         /* basic field testing */
735         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
736
737         status = smb_raw_open(cli->tree, mem_ctx, &io);
738         CHECK_STATUS(status, NT_STATUS_OK);
739         fnum = io.ntcreatex.out.file.fnum;
740
741         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
742         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
743         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
744         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
745         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
746         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
747         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
748         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
749         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
750         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
751         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
752
753         /* check fields when the file already existed */
754         smbcli_close(cli->tree, fnum);
755         smbcli_unlink(cli->tree, fname);
756         fnum = create_complex_file(cli, mem_ctx, fname);
757         if (fnum == -1) {
758                 ret = False;
759                 goto done;
760         }
761         smbcli_close(cli->tree, fnum);
762
763         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
764         status = smb_raw_open(cli->tree, mem_ctx, &io);
765         CHECK_STATUS(status, NT_STATUS_OK);
766         fnum = io.ntcreatex.out.file.fnum;
767
768         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
769         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_EXISTED);
770         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
771         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
772         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
773         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
774         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
775         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
776         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
777         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
778         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
779         smbcli_close(cli->tree, fnum);
780         smbcli_unlink(cli->tree, fname);
781
782
783         /* create a directory */
784         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
785         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
786         io.ntcreatex.in.alloc_size = 0;
787         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
788         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
789         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
790         io.ntcreatex.in.create_options = 0;
791         io.ntcreatex.in.fname = dname;
792         fname = dname;
793
794         smbcli_rmdir(cli->tree, fname);
795         smbcli_unlink(cli->tree, fname);
796
797         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
798         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
799         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
800         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
801         status = smb_raw_open(cli->tree, mem_ctx, &io);
802         CHECK_STATUS(status, NT_STATUS_OK);
803         fnum = io.ntcreatex.out.file.fnum;
804
805         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
806         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
807         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
808         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
809         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
810         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
811         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
812         CHECK_VAL(io.ntcreatex.out.attrib & ~FILE_ATTRIBUTE_NONINDEXED, 
813                   FILE_ATTRIBUTE_DIRECTORY);
814         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
815         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
816         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
817         CHECK_VAL(io.ntcreatex.out.is_directory, 1);
818         CHECK_VAL(io.ntcreatex.out.size, 0);
819         CHECK_VAL(io.ntcreatex.out.alloc_size, 0);
820         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
821         smbcli_unlink(cli->tree, fname);
822         
823
824 done:
825         smbcli_close(cli->tree, fnum);
826         smbcli_unlink(cli->tree, fname);
827
828         return ret;
829 }
830
831
832 /*
833   test RAW_OPEN_NTTRANS_CREATE
834 */
835 static BOOL test_nttrans_create(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
836 {
837         union smb_open io;
838         union smb_fileinfo finfo;
839         const char *fname = BASEDIR "\\torture_ntcreatex.txt";
840         const char *dname = BASEDIR "\\torture_ntcreatex.dir";
841         NTSTATUS status;
842         int fnum = -1;
843         BOOL ret = True;
844         int i;
845         struct {
846                 uint32_t open_disp;
847                 BOOL with_file;
848                 NTSTATUS correct_status;
849         } open_funcs[] = {
850                 { NTCREATEX_DISP_SUPERSEDE,     True,  NT_STATUS_OK },
851                 { NTCREATEX_DISP_SUPERSEDE,     False, NT_STATUS_OK },
852                 { NTCREATEX_DISP_OPEN,          True,  NT_STATUS_OK },
853                 { NTCREATEX_DISP_OPEN,          False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
854                 { NTCREATEX_DISP_CREATE,        True,  NT_STATUS_OBJECT_NAME_COLLISION },
855                 { NTCREATEX_DISP_CREATE,        False, NT_STATUS_OK },
856                 { NTCREATEX_DISP_OPEN_IF,       True,  NT_STATUS_OK },
857                 { NTCREATEX_DISP_OPEN_IF,       False, NT_STATUS_OK },
858                 { NTCREATEX_DISP_OVERWRITE,     True,  NT_STATUS_OK },
859                 { NTCREATEX_DISP_OVERWRITE,     False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
860                 { NTCREATEX_DISP_OVERWRITE_IF,  True,  NT_STATUS_OK },
861                 { NTCREATEX_DISP_OVERWRITE_IF,  False, NT_STATUS_OK },
862                 { 6,                            True,  NT_STATUS_INVALID_PARAMETER },
863                 { 6,                            False, NT_STATUS_INVALID_PARAMETER },
864         };
865
866         printf("Checking RAW_OPEN_NTTRANS_CREATE\n");
867
868         /* reasonable default parameters */
869         io.generic.level = RAW_OPEN_NTTRANS_CREATE;
870         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
871         io.ntcreatex.in.root_fid = 0;
872         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
873         io.ntcreatex.in.alloc_size = 1024*1024;
874         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
875         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
876         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
877         io.ntcreatex.in.create_options = 0;
878         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
879         io.ntcreatex.in.security_flags = 0;
880         io.ntcreatex.in.fname = fname;
881         io.ntcreatex.in.sec_desc = NULL;
882         io.ntcreatex.in.ea_list = NULL;
883
884         /* test the open disposition */
885         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
886                 if (open_funcs[i].with_file) {
887                         fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR|O_TRUNC, DENY_NONE);
888                         if (fnum == -1) {
889                                 d_printf("Failed to create file %s - %s\n", fname, smbcli_errstr(cli->tree));
890                                 ret = False;
891                                 goto done;
892                         }
893                         smbcli_close(cli->tree, fnum);
894                 }
895                 io.ntcreatex.in.open_disposition = open_funcs[i].open_disp;
896                 status = smb_raw_open(cli->tree, mem_ctx, &io);
897                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
898                         printf("(%s) incorrect status %s should be %s (i=%d with_file=%d open_disp=%d)\n", 
899                                __location__, nt_errstr(status), nt_errstr(open_funcs[i].correct_status),
900                                i, (int)open_funcs[i].with_file, (int)open_funcs[i].open_disp);
901                         ret = False;
902                 }
903                 if (NT_STATUS_IS_OK(status) || open_funcs[i].with_file) {
904                         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
905                         smbcli_unlink(cli->tree, fname);
906                 }
907         }
908
909         /* basic field testing */
910         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
911
912         status = smb_raw_open(cli->tree, mem_ctx, &io);
913         CHECK_STATUS(status, NT_STATUS_OK);
914         fnum = io.ntcreatex.out.file.fnum;
915
916         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
917         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
918         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
919         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
920         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
921         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
922         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
923         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
924         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
925         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
926         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
927
928         /* check fields when the file already existed */
929         smbcli_close(cli->tree, fnum);
930         smbcli_unlink(cli->tree, fname);
931         fnum = create_complex_file(cli, mem_ctx, fname);
932         if (fnum == -1) {
933                 ret = False;
934                 goto done;
935         }
936         smbcli_close(cli->tree, fnum);
937
938         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
939         status = smb_raw_open(cli->tree, mem_ctx, &io);
940         CHECK_STATUS(status, NT_STATUS_OK);
941         fnum = io.ntcreatex.out.file.fnum;
942
943         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
944         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_EXISTED);
945         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
946         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
947         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
948         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
949         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
950         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
951         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
952         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
953         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
954         smbcli_close(cli->tree, fnum);
955         smbcli_unlink(cli->tree, fname);
956
957
958         /* create a directory */
959         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
960         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
961         io.ntcreatex.in.alloc_size = 0;
962         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
963         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
964         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
965         io.ntcreatex.in.create_options = 0;
966         io.ntcreatex.in.fname = dname;
967         fname = dname;
968
969         smbcli_rmdir(cli->tree, fname);
970         smbcli_unlink(cli->tree, fname);
971
972         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
973         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
974         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
975         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
976         status = smb_raw_open(cli->tree, mem_ctx, &io);
977         CHECK_STATUS(status, NT_STATUS_OK);
978         fnum = io.ntcreatex.out.file.fnum;
979
980         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
981         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
982         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
983         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
984         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
985         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
986         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
987         CHECK_VAL(io.ntcreatex.out.attrib & ~FILE_ATTRIBUTE_NONINDEXED, 
988                   FILE_ATTRIBUTE_DIRECTORY);
989         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
990         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
991         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
992         CHECK_VAL(io.ntcreatex.out.is_directory, 1);
993         CHECK_VAL(io.ntcreatex.out.size, 0);
994         CHECK_VAL(io.ntcreatex.out.alloc_size, 0);
995         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
996         smbcli_unlink(cli->tree, fname);
997         
998
999 done:
1000         smbcli_close(cli->tree, fnum);
1001         smbcli_unlink(cli->tree, fname);
1002
1003         return ret;
1004 }
1005
1006 /*
1007   test RAW_OPEN_NTCREATEX with an already opened and byte range locked file
1008
1009   I've got an application that does a similar sequence of ntcreate&x,
1010   locking&x and another ntcreate&x with
1011   open_disposition==NTCREATEX_DISP_OVERWRITE_IF. Windows 2003 allows the
1012   second open.
1013 */
1014 static BOOL test_ntcreatex_brlocked(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1015 {
1016         union smb_open io, io1;
1017         union smb_lock io2;
1018         struct smb_lock_entry lock[1];
1019         const char *fname = BASEDIR "\\torture_ntcreatex.txt";
1020         NTSTATUS status;
1021         BOOL ret = True;
1022
1023         printf("Testing ntcreatex with a byte range locked file\n");
1024
1025         io.generic.level = RAW_OPEN_NTCREATEX;
1026         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
1027         io.ntcreatex.in.root_fid = 0;
1028         io.ntcreatex.in.access_mask = 0x2019f;
1029         io.ntcreatex.in.alloc_size = 0;
1030         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1031         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1032                 NTCREATEX_SHARE_ACCESS_WRITE;
1033         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
1034         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_NON_DIRECTORY_FILE;
1035         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
1036         io.ntcreatex.in.security_flags = NTCREATEX_SECURITY_DYNAMIC |
1037                 NTCREATEX_SECURITY_ALL;
1038         io.ntcreatex.in.fname = fname;
1039
1040         status = smb_raw_open(cli->tree, mem_ctx, &io);
1041         CHECK_STATUS(status, NT_STATUS_OK);
1042
1043         io2.lockx.level = RAW_LOCK_LOCKX;
1044         io2.lockx.in.file.fnum = io.ntcreatex.out.file.fnum;
1045         io2.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
1046         io2.lockx.in.timeout = 0;
1047         io2.lockx.in.ulock_cnt = 0;
1048         io2.lockx.in.lock_cnt = 1;
1049         lock[0].pid = cli->session->pid;
1050         lock[0].offset = 0;
1051         lock[0].count = 0x1;
1052         io2.lockx.in.locks = &lock[0];
1053         status = smb_raw_lock(cli->tree, &io2);
1054         CHECK_STATUS(status, NT_STATUS_OK);
1055
1056         io1.generic.level = RAW_OPEN_NTCREATEX;
1057         io1.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
1058         io1.ntcreatex.in.root_fid = 0;
1059         io1.ntcreatex.in.access_mask = 0x20196;
1060         io1.ntcreatex.in.alloc_size = 0;
1061         io1.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1062         io1.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1063                 NTCREATEX_SHARE_ACCESS_WRITE;
1064         io1.ntcreatex.in.open_disposition = NTCREATEX_DISP_OVERWRITE_IF;
1065         io1.ntcreatex.in.create_options = 0;
1066         io1.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
1067         io1.ntcreatex.in.security_flags = NTCREATEX_SECURITY_DYNAMIC |
1068                 NTCREATEX_SECURITY_ALL;
1069         io1.ntcreatex.in.fname = fname;
1070
1071         status = smb_raw_open(cli->tree, mem_ctx, &io1);
1072         CHECK_STATUS(status, NT_STATUS_OK);
1073
1074  done:
1075         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
1076         smbcli_close(cli->tree, io1.ntcreatex.out.file.fnum);
1077         smbcli_unlink(cli->tree, fname);
1078         return ret;
1079 }
1080
1081 /*
1082   test RAW_OPEN_MKNEW
1083 */
1084 static BOOL test_mknew(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1085 {
1086         union smb_open io;
1087         const char *fname = BASEDIR "\\torture_mknew.txt";
1088         NTSTATUS status;
1089         int fnum = -1;
1090         BOOL ret = True;
1091         time_t basetime = (time(NULL) + 3600*24*3) & ~1;
1092         union smb_fileinfo finfo;
1093
1094         printf("Checking RAW_OPEN_MKNEW\n");
1095
1096         io.mknew.level = RAW_OPEN_MKNEW;
1097         io.mknew.in.attrib = 0;
1098         io.mknew.in.write_time = 0;
1099         io.mknew.in.fname = fname;
1100         status = smb_raw_open(cli->tree, mem_ctx, &io);
1101         CHECK_STATUS(status, NT_STATUS_OK);
1102         fnum = io.mknew.out.file.fnum;
1103
1104         status = smb_raw_open(cli->tree, mem_ctx, &io);
1105         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_COLLISION);
1106
1107         smbcli_close(cli->tree, fnum);
1108         smbcli_unlink(cli->tree, fname);
1109
1110         /* make sure write_time works */
1111         io.mknew.in.write_time = basetime;
1112         status = smb_raw_open(cli->tree, mem_ctx, &io);
1113         CHECK_STATUS(status, NT_STATUS_OK);
1114         fnum = io.mknew.out.file.fnum;
1115         CHECK_TIME(basetime, write_time);
1116
1117         smbcli_close(cli->tree, fnum);
1118         smbcli_unlink(cli->tree, fname);
1119
1120         /* make sure file_attrs works */
1121         io.mknew.in.attrib = FILE_ATTRIBUTE_HIDDEN;
1122         status = smb_raw_open(cli->tree, mem_ctx, &io);
1123         CHECK_STATUS(status, NT_STATUS_OK);
1124         fnum = io.mknew.out.file.fnum;
1125         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE, 
1126                        attrib & ~FILE_ATTRIBUTE_NONINDEXED);
1127         
1128 done:
1129         smbcli_close(cli->tree, fnum);
1130         smbcli_unlink(cli->tree, fname);
1131
1132         return ret;
1133 }
1134
1135
1136 /*
1137   test RAW_OPEN_CREATE
1138 */
1139 static BOOL test_create(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1140 {
1141         union smb_open io;
1142         const char *fname = BASEDIR "\\torture_create.txt";
1143         NTSTATUS status;
1144         int fnum = -1;
1145         BOOL ret = True;
1146         time_t basetime = (time(NULL) + 3600*24*3) & ~1;
1147         union smb_fileinfo finfo;
1148
1149         printf("Checking RAW_OPEN_CREATE\n");
1150
1151         io.create.level = RAW_OPEN_CREATE;
1152         io.create.in.attrib = 0;
1153         io.create.in.write_time = 0;
1154         io.create.in.fname = fname;
1155         status = smb_raw_open(cli->tree, mem_ctx, &io);
1156         CHECK_STATUS(status, NT_STATUS_OK);
1157         fnum = io.create.out.file.fnum;
1158
1159         status = smb_raw_open(cli->tree, mem_ctx, &io);
1160         CHECK_STATUS(status, NT_STATUS_OK);
1161
1162         smbcli_close(cli->tree, io.create.out.file.fnum);
1163         smbcli_close(cli->tree, fnum);
1164         smbcli_unlink(cli->tree, fname);
1165
1166         /* make sure write_time works */
1167         io.create.in.write_time = basetime;
1168         status = smb_raw_open(cli->tree, mem_ctx, &io);
1169         CHECK_STATUS(status, NT_STATUS_OK);
1170         fnum = io.create.out.file.fnum;
1171         CHECK_TIME(basetime, write_time);
1172
1173         smbcli_close(cli->tree, fnum);
1174         smbcli_unlink(cli->tree, fname);
1175
1176         /* make sure file_attrs works */
1177         io.create.in.attrib = FILE_ATTRIBUTE_HIDDEN;
1178         status = smb_raw_open(cli->tree, mem_ctx, &io);
1179         CHECK_STATUS(status, NT_STATUS_OK);
1180         fnum = io.create.out.file.fnum;
1181         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE, 
1182                        attrib & ~FILE_ATTRIBUTE_NONINDEXED);
1183         
1184 done:
1185         smbcli_close(cli->tree, fnum);
1186         smbcli_unlink(cli->tree, fname);
1187
1188         return ret;
1189 }
1190
1191
1192 /*
1193   test RAW_OPEN_CTEMP
1194 */
1195 static BOOL test_ctemp(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1196 {
1197         union smb_open io;
1198         NTSTATUS status;
1199         int fnum = -1;
1200         BOOL ret = True;
1201         time_t basetime = (time(NULL) + 3600*24*3) & ~1;
1202         union smb_fileinfo finfo;
1203         const char *name, *fname = NULL;
1204
1205         printf("Checking RAW_OPEN_CTEMP\n");
1206
1207         io.ctemp.level = RAW_OPEN_CTEMP;
1208         io.ctemp.in.attrib = FILE_ATTRIBUTE_HIDDEN;
1209         io.ctemp.in.write_time = basetime;
1210         io.ctemp.in.directory = BASEDIR;
1211         status = smb_raw_open(cli->tree, mem_ctx, &io);
1212         CHECK_STATUS(status, NT_STATUS_OK);
1213         fnum = io.ctemp.out.file.fnum;
1214
1215         name = io.ctemp.out.name;
1216
1217         finfo.generic.level = RAW_FILEINFO_NAME_INFO;
1218         finfo.generic.in.file.fnum = fnum;
1219         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
1220         CHECK_STATUS(status, NT_STATUS_OK);
1221
1222         fname = finfo.name_info.out.fname.s;
1223         d_printf("ctemp name=%s  real name=%s\n", name, fname);
1224
1225 done:
1226         smbcli_close(cli->tree, fnum);
1227         if (fname) {
1228                 smbcli_unlink(cli->tree, fname);
1229         }
1230
1231         return ret;
1232 }
1233
1234
1235 /*
1236   test chained RAW_OPEN_OPENX_READX
1237 */
1238 static BOOL test_chained(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1239 {
1240         union smb_open io;
1241         const char *fname = BASEDIR "\\torture_chained.txt";
1242         NTSTATUS status;
1243         int fnum = -1;
1244         BOOL ret = True;
1245         const char *buf = "test";
1246         char buf2[4];
1247
1248         printf("Checking RAW_OPEN_OPENX chained with READX\n");
1249         smbcli_unlink(cli->tree, fname);
1250
1251         fnum = create_complex_file(cli, mem_ctx, fname);
1252
1253         smbcli_write(cli->tree, fnum, 0, buf, 0, sizeof(buf));
1254
1255         smbcli_close(cli->tree, fnum);  
1256
1257         io.openxreadx.level = RAW_OPEN_OPENX_READX;
1258         io.openxreadx.in.fname = fname;
1259         io.openxreadx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
1260         io.openxreadx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
1261         io.openxreadx.in.open_func = OPENX_OPEN_FUNC_OPEN;
1262         io.openxreadx.in.search_attrs = 0;
1263         io.openxreadx.in.file_attrs = 0;
1264         io.openxreadx.in.write_time = 0;
1265         io.openxreadx.in.size = 1024*1024;
1266         io.openxreadx.in.timeout = 0;
1267         
1268         io.openxreadx.in.offset = 0;
1269         io.openxreadx.in.mincnt = sizeof(buf);
1270         io.openxreadx.in.maxcnt = sizeof(buf);
1271         io.openxreadx.in.remaining = 0;
1272         io.openxreadx.out.data = (uint8_t *)buf2;
1273
1274         status = smb_raw_open(cli->tree, mem_ctx, &io);
1275         CHECK_STATUS(status, NT_STATUS_OK);
1276         fnum = io.openxreadx.out.file.fnum;
1277
1278         if (memcmp(buf, buf2, sizeof(buf)) != 0) {
1279                 d_printf("wrong data in reply buffer\n");
1280                 ret = False;
1281         }
1282
1283 done:
1284         smbcli_close(cli->tree, fnum);
1285         smbcli_unlink(cli->tree, fname);
1286
1287         return ret;
1288 }
1289
1290 /*
1291   test RAW_OPEN_OPENX without a leading slash on the path.
1292   NetApp filers are known to fail on this.
1293   
1294 */
1295 static BOOL test_no_leading_slash(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1296 {
1297         union smb_open io;
1298         const char *fname = BASEDIR "\\torture_no_leading_slash.txt";
1299         NTSTATUS status;
1300         int fnum = -1;
1301         BOOL ret = True;
1302         const char *buf = "test";
1303
1304         printf("Checking RAW_OPEN_OPENX without leading slash on path\n");
1305         smbcli_unlink(cli->tree, fname);
1306
1307         /* Create the file */
1308         fnum = create_complex_file(cli, mem_ctx, fname);
1309         smbcli_write(cli->tree, fnum, 0, buf, 0, sizeof(buf));
1310         smbcli_close(cli->tree, fnum);  
1311
1312         /* Prepare to open the file using path without leading slash */
1313         io.openx.level = RAW_OPEN_OPENX;
1314         io.openx.in.fname = fname + 1;
1315         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
1316         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
1317         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
1318         io.openx.in.search_attrs = 0;
1319         io.openx.in.file_attrs = 0;
1320         io.openx.in.write_time = 0;
1321         io.openx.in.size = 1024*1024;
1322         io.openx.in.timeout = 0;
1323
1324         status = smb_raw_open(cli->tree, mem_ctx, &io);
1325         CHECK_STATUS(status, NT_STATUS_OK);
1326         fnum = io.openx.out.file.fnum;
1327
1328 done:
1329         smbcli_close(cli->tree, fnum);
1330         smbcli_unlink(cli->tree, fname);
1331
1332         return ret;
1333 }
1334
1335 /* A little torture test to expose a race condition in Samba 3.0.20 ... :-) */
1336
1337 static BOOL test_raw_open_multi(void)
1338 {
1339         struct smbcli_state *cli;
1340         TALLOC_CTX *mem_ctx = talloc_init("torture_test_oplock_multi");
1341         const char *fname = "\\test_oplock.dat";
1342         NTSTATUS status;
1343         BOOL ret = True;
1344         union smb_open io;
1345         struct smbcli_state **clients;
1346         struct smbcli_request **requests;
1347         union smb_open *ios;
1348         const char *host = lp_parm_string(-1, "torture", "host");
1349         const char *share = lp_parm_string(-1, "torture", "share");
1350         int i, num_files = 3;
1351         struct event_context *ev;
1352         int num_ok = 0;
1353         int num_collision = 0;
1354         
1355         ev = cli_credentials_get_event_context(cmdline_credentials);
1356         clients = talloc_array(mem_ctx, struct smbcli_state *, num_files);
1357         requests = talloc_array(mem_ctx, struct smbcli_request *, num_files);
1358         ios = talloc_array(mem_ctx, union smb_open, num_files);
1359         if ((ev == NULL) || (clients == NULL) || (requests == NULL) ||
1360             (ios == NULL)) {
1361                 DEBUG(0, ("talloc failed\n"));
1362                 return False;
1363         }
1364
1365         if (!torture_open_connection_share(mem_ctx, &cli, host, share, ev)) {
1366                 return False;
1367         }
1368
1369         cli->tree->session->transport->options.request_timeout = 60000;
1370
1371         for (i=0; i<num_files; i++) {
1372                 if (!torture_open_connection_share(mem_ctx, &(clients[i]),
1373                                                    host, share, ev)) {
1374                         DEBUG(0, ("Could not open %d'th connection\n", i));
1375                         return False;
1376                 }
1377                 clients[i]->tree->session->transport->
1378                         options.request_timeout = 60000;
1379         }
1380
1381         /* cleanup */
1382         smbcli_unlink(cli->tree, fname);
1383
1384         /*
1385           base ntcreatex parms
1386         */
1387         io.generic.level = RAW_OPEN_NTCREATEX;
1388         io.ntcreatex.in.root_fid = 0;
1389         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
1390         io.ntcreatex.in.alloc_size = 0;
1391         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1392         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ|
1393                 NTCREATEX_SHARE_ACCESS_WRITE|
1394                 NTCREATEX_SHARE_ACCESS_DELETE;
1395         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
1396         io.ntcreatex.in.create_options = 0;
1397         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1398         io.ntcreatex.in.security_flags = 0;
1399         io.ntcreatex.in.fname = fname;
1400         io.ntcreatex.in.flags = 0;
1401
1402         for (i=0; i<num_files; i++) {
1403                 ios[i] = io;
1404                 requests[i] = smb_raw_open_send(clients[i]->tree, &ios[i]);
1405                 if (requests[i] == NULL) {
1406                         DEBUG(0, ("could not send %d'th request\n", i));
1407                         return False;
1408                 }
1409         }
1410
1411         DEBUG(10, ("waiting for replies\n"));
1412         while (1) {
1413                 BOOL unreplied = False;
1414                 for (i=0; i<num_files; i++) {
1415                         if (requests[i] == NULL) {
1416                                 continue;
1417                         }
1418                         if (requests[i]->state < SMBCLI_REQUEST_DONE) {
1419                                 unreplied = True;
1420                                 break;
1421                         }
1422                         status = smb_raw_open_recv(requests[i], mem_ctx,
1423                                                    &ios[i]);
1424
1425                         DEBUG(0, ("File %d returned status %s\n", i,
1426                                   nt_errstr(status)));
1427
1428                         if (NT_STATUS_IS_OK(status)) {
1429                                 num_ok += 1;
1430                         } 
1431
1432                         if (NT_STATUS_EQUAL(status,
1433                                             NT_STATUS_OBJECT_NAME_COLLISION)) {
1434                                 num_collision += 1;
1435                         }
1436
1437                         requests[i] = NULL;
1438                 }
1439                 if (!unreplied) {
1440                         break;
1441                 }
1442
1443                 if (event_loop_once(ev) != 0) {
1444                         DEBUG(0, ("event_loop_once failed\n"));
1445                         return False;
1446                 }
1447         }
1448
1449         if ((num_ok != 1) || (num_ok + num_collision != num_files)) {
1450                 ret = False;
1451         }
1452
1453         for (i=0; i<num_files; i++) {
1454                 torture_close_connection(clients[i]);
1455         }
1456         talloc_free(mem_ctx);
1457         return ret;
1458 }
1459
1460 /* basic testing of all RAW_OPEN_* calls 
1461 */
1462 bool torture_raw_open(struct torture_context *torture, struct smbcli_state *cli)
1463 {
1464         bool ret = true;
1465
1466         if (!torture_setup_dir(cli, BASEDIR)) {
1467                 return false;
1468         }
1469
1470         ret &= test_ntcreatex_brlocked(cli, torture);
1471         ret &= test_open(cli, torture);
1472         ret &= test_raw_open_multi();
1473         ret &= test_openx(cli, torture);
1474         ret &= test_ntcreatex(cli, torture);
1475         ret &= test_nttrans_create(cli, torture);
1476         ret &= test_t2open(cli, torture);
1477         ret &= test_mknew(cli, torture);
1478         ret &= test_create(cli, torture);
1479         ret &= test_ctemp(cli, torture);
1480         ret &= test_chained(cli, torture);
1481         ret &= test_no_leading_slash(cli, torture);
1482
1483         smb_raw_exit(cli->session);
1484         smbcli_deltree(cli->tree, BASEDIR);
1485
1486         return ret;
1487 }