r152: a quick airport commit ....
[gd/samba-autobuild/.git] / source4 / torture / raw / open.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RAW_OPEN_* individual test suite
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 /* enum for whether reads/writes are possible on a file */
24 enum rdwr_mode {RDWR_NONE, RDWR_RDONLY, RDWR_WRONLY, RDWR_RDWR};
25
26 #define BASEDIR "\\rawopen"
27
28 /*
29   check if a open file can be read/written
30 */
31 static enum rdwr_mode check_rdwr(struct cli_tree *tree, int fnum)
32 {
33         char c = 1;
34         BOOL can_read  = (cli_read(tree, fnum, &c, 0, 1) == 1);
35         BOOL can_write = (cli_write(tree, fnum, 0, &c, 0, 1) == 1);
36         if ( can_read &&  can_write) return RDWR_RDWR;
37         if ( can_read && !can_write) return RDWR_RDONLY;
38         if (!can_read &&  can_write) return RDWR_WRONLY;
39         return RDWR_NONE;
40 }
41
42 /*
43   describe a RDWR mode as a string
44 */
45 static const char *rdwr_string(enum rdwr_mode m)
46 {
47         switch (m) {
48         case RDWR_NONE: return "NONE";
49         case RDWR_RDONLY: return "RDONLY";
50         case RDWR_WRONLY: return "WRONLY";
51         case RDWR_RDWR: return "RDWR";
52         }
53         return "-";
54 }
55
56 #define CHECK_STATUS(status, correct) do { \
57         if (!NT_STATUS_EQUAL(status, correct)) { \
58                 printf("(%d) Incorrect status %s - should be %s\n", \
59                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
60                 ret = False; \
61                 goto done; \
62         }} while (0)
63
64 #define CREATE_FILE do { \
65         fnum = create_complex_file(cli, mem_ctx, fname); \
66         if (fnum == -1) { \
67                 printf("(%d) Failed to create %s - %s\n", __LINE__, fname, cli_errstr(cli->tree)); \
68                 ret = False; \
69                 goto done; \
70         }} while (0)
71
72 #define CHECK_RDWR(fnum, correct) do { \
73         enum rdwr_mode m = check_rdwr(cli->tree, fnum); \
74         if (m != correct) { \
75                 printf("(%d) Incorrect readwrite mode %s - expected %s\n", \
76                        __LINE__, rdwr_string(m), rdwr_string(correct)); \
77                 ret = False; \
78         }} while (0)
79
80 #define CHECK_TIME(t, field) do { \
81         time_t t1, t2; \
82         finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
83         finfo.all_info.in.fname = fname; \
84         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo); \
85         CHECK_STATUS(status, NT_STATUS_OK); \
86         t1 = t & ~1; \
87         t2 = nt_time_to_unix(&finfo.all_info.out.field) & ~1; \
88         if (ABS(t1-t2) > 2) { \
89                 printf("(%d) wrong time for field %s  %s - %s\n", \
90                        __LINE__, #field, \
91                        timestring(mem_ctx, t1), \
92                        timestring(mem_ctx, t2)); \
93                 dump_all_info(mem_ctx, &finfo); \
94                 ret = False; \
95         }} while (0)
96
97 #define CHECK_NTTIME(t, field) do { \
98         NTTIME t2; \
99         finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
100         finfo.all_info.in.fname = fname; \
101         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo); \
102         CHECK_STATUS(status, NT_STATUS_OK); \
103         t2 = finfo.all_info.out.field; \
104         if (!nt_time_equal(&t, &t2)) { \
105                 printf("(%d) wrong time for field %s  %s - %s\n", \
106                        __LINE__, #field, \
107                        nt_time_string(mem_ctx, &t), \
108                        nt_time_string(mem_ctx, &t2)); \
109                 dump_all_info(mem_ctx, &finfo); \
110                 ret = False; \
111         }} while (0)
112
113 #define CHECK_ALL_INFO(v, field) do { \
114         finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
115         finfo.all_info.in.fname = fname; \
116         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo); \
117         CHECK_STATUS(status, NT_STATUS_OK); \
118         if ((v) != finfo.all_info.out.field) { \
119                 printf("(%d) wrong value for field %s  0x%x - 0x%x\n", \
120                        __LINE__, #field, (int)v, (int)finfo.all_info.out.field); \
121                 dump_all_info(mem_ctx, &finfo); \
122                 ret = False; \
123         }} while (0)
124
125 #define CHECK_VAL(v, correct) do { \
126         if ((v) != (correct)) { \
127                 printf("(%d) wrong value for %s  0x%x - 0x%x\n", \
128                        __LINE__, #v, (int)v, (int)correct); \
129                 ret = False; \
130         }} while (0)
131
132 #define SET_ATTRIB(sattrib) do { \
133         union smb_setfileinfo sfinfo; \
134         sfinfo.generic.level = RAW_SFILEINFO_BASIC_INFORMATION; \
135         sfinfo.generic.file.fname = fname; \
136         ZERO_STRUCT(sfinfo.basic_info.in); \
137         sfinfo.basic_info.in.attrib = sattrib; \
138         status = smb_raw_setpathinfo(cli->tree, &sfinfo); \
139         if (!NT_STATUS_IS_OK(status)) { \
140                 printf("(%d) Failed to set attrib 0x%x on %s\n", \
141                        __LINE__, sattrib, fname); \
142         }} while (0)
143
144 /*
145   test RAW_OPEN_OPEN
146 */
147 static BOOL test_open(struct cli_state *cli, TALLOC_CTX *mem_ctx)
148 {
149         union smb_open io;
150         union smb_fileinfo finfo;
151         const char *fname = BASEDIR "\\torture_open.txt";
152         NTSTATUS status;
153         int fnum, fnum2;
154         BOOL ret = True;
155
156         printf("Checking RAW_OPEN_OPEN\n");
157
158         io.open.level = RAW_OPEN_OPEN;
159         io.open.in.fname = fname;
160         io.open.in.flags = OPEN_FLAGS_FCB;
161         io.open.in.search_attrs = 0;
162         status = smb_raw_open(cli->tree, mem_ctx, &io);
163         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
164         fnum = io.open.out.fnum;
165
166         cli_unlink(cli->tree, fname);
167         CREATE_FILE;
168         cli_close(cli->tree, fnum);
169
170         status = smb_raw_open(cli->tree, mem_ctx, &io);
171         CHECK_STATUS(status, NT_STATUS_OK);
172         fnum = io.open.out.fnum;
173         CHECK_RDWR(fnum, RDWR_RDWR);
174
175         status = smb_raw_open(cli->tree, mem_ctx, &io);
176         CHECK_STATUS(status, NT_STATUS_OK);
177         fnum2 = io.open.out.fnum;
178         CHECK_RDWR(fnum2, RDWR_RDWR);
179         cli_close(cli->tree, fnum2);
180         cli_close(cli->tree, fnum);
181
182         /* check the read/write modes */
183         io.open.level = RAW_OPEN_OPEN;
184         io.open.in.fname = fname;
185         io.open.in.search_attrs = 0;
186
187         io.open.in.flags = OPEN_FLAGS_OPEN_READ;
188         status = smb_raw_open(cli->tree, mem_ctx, &io);
189         CHECK_STATUS(status, NT_STATUS_OK);
190         fnum = io.open.out.fnum;
191         CHECK_RDWR(fnum, RDWR_RDONLY);
192         cli_close(cli->tree, fnum);
193
194         io.open.in.flags = OPEN_FLAGS_OPEN_WRITE;
195         status = smb_raw_open(cli->tree, mem_ctx, &io);
196         CHECK_STATUS(status, NT_STATUS_OK);
197         fnum = io.open.out.fnum;
198         CHECK_RDWR(fnum, RDWR_WRONLY);
199         cli_close(cli->tree, fnum);
200
201         io.open.in.flags = OPEN_FLAGS_OPEN_RDWR;
202         status = smb_raw_open(cli->tree, mem_ctx, &io);
203         CHECK_STATUS(status, NT_STATUS_OK);
204         fnum = io.open.out.fnum;
205         CHECK_RDWR(fnum, RDWR_RDWR);
206         cli_close(cli->tree, fnum);
207
208         /* check the share modes roughly - not a complete matrix */
209         io.open.in.flags = OPEN_FLAGS_OPEN_RDWR | OPEN_FLAGS_DENY_WRITE;
210         status = smb_raw_open(cli->tree, mem_ctx, &io);
211         CHECK_STATUS(status, NT_STATUS_OK);
212         fnum = io.open.out.fnum;
213         CHECK_RDWR(fnum, RDWR_RDWR);
214         
215         if (io.open.in.flags != io.open.out.rmode) {
216                 printf("(%d) rmode should equal flags - 0x%x 0x%x\n",
217                        __LINE__, io.open.out.rmode, io.open.in.flags);
218         }
219
220         io.open.in.flags = OPEN_FLAGS_OPEN_RDWR | OPEN_FLAGS_DENY_NONE;
221         status = smb_raw_open(cli->tree, mem_ctx, &io);
222         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
223
224         io.open.in.flags = OPEN_FLAGS_OPEN_READ | OPEN_FLAGS_DENY_NONE;
225         status = smb_raw_open(cli->tree, mem_ctx, &io);
226         CHECK_STATUS(status, NT_STATUS_OK);
227         fnum2 = io.open.out.fnum;
228         CHECK_RDWR(fnum2, RDWR_RDONLY);
229         cli_close(cli->tree, fnum);
230         cli_close(cli->tree, fnum2);
231
232
233         /* check the returned write time */
234         io.open.level = RAW_OPEN_OPEN;
235         io.open.in.fname = fname;
236         io.open.in.search_attrs = 0;
237         io.open.in.flags = OPEN_FLAGS_OPEN_READ;
238         status = smb_raw_open(cli->tree, mem_ctx, &io);
239         CHECK_STATUS(status, NT_STATUS_OK);
240         fnum = io.open.out.fnum;
241
242         /* check other reply fields */
243         CHECK_TIME(io.open.out.write_time, write_time);
244         CHECK_ALL_INFO(io.open.out.size, size);
245         CHECK_ALL_INFO(io.open.out.attrib, attrib);
246
247 done:
248         cli_close(cli->tree, fnum);
249         cli_unlink(cli->tree, fname);
250
251         return ret;
252 }
253
254
255 /*
256   test RAW_OPEN_OPENX
257 */
258 static BOOL test_openx(struct cli_state *cli, TALLOC_CTX *mem_ctx)
259 {
260         union smb_open io;
261         union smb_fileinfo finfo;
262         const char *fname = BASEDIR "\\torture_openx.txt";
263         NTSTATUS status;
264         int fnum, fnum2;
265         BOOL ret = True;
266         int i;
267         struct {
268                 uint16 open_func;
269                 BOOL with_file;
270                 NTSTATUS correct_status;
271         } open_funcs[] = {
272                 { OPENX_OPEN_FUNC_OPEN,                           True,  NT_STATUS_OK },
273                 { OPENX_OPEN_FUNC_OPEN,                           False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
274                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OK },
275                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_OK },
276                 { OPENX_OPEN_FUNC_FAIL,                           True,  NT_STATUS_INVALID_LOCK_SEQUENCE },
277                 { OPENX_OPEN_FUNC_FAIL,                           False, NT_STATUS_INVALID_LOCK_SEQUENCE },
278                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OBJECT_NAME_COLLISION },
279                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_OK },
280                 { OPENX_OPEN_FUNC_TRUNC,                          True,  NT_STATUS_OK },
281                 { OPENX_OPEN_FUNC_TRUNC,                          False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
282                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OK },
283                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_OK },
284         };
285
286         printf("Checking RAW_OPEN_OPENX\n");
287         cli_unlink(cli->tree, fname);
288
289         io.openx.level = RAW_OPEN_OPENX;
290         io.openx.in.fname = fname;
291         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
292         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
293         io.openx.in.search_attrs = 0;
294         io.openx.in.file_attrs = 0;
295         io.openx.in.write_time = 0;
296         io.openx.in.size = 1024*1024;
297         io.openx.in.timeout = 0;
298
299         /* check all combinations of open_func */
300         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
301                 if (open_funcs[i].with_file) {
302                         fnum = create_complex_file(cli, mem_ctx, fname);
303                         if (fnum == -1) {
304                                 d_printf("Failed to create file %s - %s\n", fname, cli_errstr(cli->tree));
305                                 ret = False;
306                                 goto done;
307                         }
308                         cli_close(cli->tree, fnum);
309                 }
310                 io.openx.in.open_func = open_funcs[i].open_func;
311                 status = smb_raw_open(cli->tree, mem_ctx, &io);
312                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
313                         printf("(%d) incorrect status %s should be %s (i=%d with_file=%d open_func=0x%x)\n", 
314                                __LINE__, nt_errstr(status), nt_errstr(open_funcs[i].correct_status),
315                                i, (int)open_funcs[i].with_file, (int)open_funcs[i].open_func);
316                         ret = False;
317                 }
318                 if (NT_STATUS_IS_OK(status) || open_funcs[i].with_file) {
319                         cli_close(cli->tree, io.openx.out.fnum);
320                         cli_unlink(cli->tree, fname);
321                 }
322         }
323
324         /* check the basic return fields */
325         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
326         status = smb_raw_open(cli->tree, mem_ctx, &io);
327         CHECK_STATUS(status, NT_STATUS_OK);
328         fnum = io.openx.out.fnum;
329
330         CHECK_ALL_INFO(io.openx.out.size, size);
331         CHECK_VAL(io.openx.out.size, 1024*1024);
332         CHECK_ALL_INFO(io.openx.in.size, size);
333         CHECK_TIME(io.openx.out.write_time, write_time);
334         CHECK_ALL_INFO(io.openx.out.attrib, attrib);
335         CHECK_VAL(io.openx.out.access, OPENX_MODE_ACCESS_RDWR);
336         CHECK_VAL(io.openx.out.ftype, 0);
337         CHECK_VAL(io.openx.out.devstate, 0);
338         CHECK_VAL(io.openx.out.action, OPENX_ACTION_CREATED);
339         cli_close(cli->tree, fnum);
340         cli_unlink(cli->tree, fname);
341
342         /* check the fields when the file already existed */
343         fnum2 = create_complex_file(cli, mem_ctx, fname);
344         if (fnum2 == -1) {
345                 ret = False;
346                 goto done;
347         }
348         cli_close(cli->tree, fnum2);
349
350         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
351         status = smb_raw_open(cli->tree, mem_ctx, &io);
352         CHECK_STATUS(status, NT_STATUS_OK);
353         fnum = io.openx.out.fnum;
354
355         CHECK_ALL_INFO(io.openx.out.size, size);
356         CHECK_TIME(io.openx.out.write_time, write_time);
357         CHECK_VAL(io.openx.out.action, OPENX_ACTION_EXISTED);
358         CHECK_VAL(io.openx.out.unknown, 0);
359         CHECK_ALL_INFO(io.openx.out.attrib, attrib);
360         cli_close(cli->tree, fnum);
361
362         /* now check the search attrib for hidden files - win2003 ignores this? */
363         SET_ATTRIB(FILE_ATTRIBUTE_HIDDEN);
364         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN, attrib);
365
366         io.openx.in.search_attrs = FILE_ATTRIBUTE_HIDDEN;
367         status = smb_raw_open(cli->tree, mem_ctx, &io);
368         CHECK_STATUS(status, NT_STATUS_OK);
369         cli_close(cli->tree, io.openx.out.fnum);
370
371         io.openx.in.search_attrs = 0;
372         status = smb_raw_open(cli->tree, mem_ctx, &io);
373         CHECK_STATUS(status, NT_STATUS_OK);
374         cli_close(cli->tree, io.openx.out.fnum);
375
376         SET_ATTRIB(FILE_ATTRIBUTE_NORMAL);
377         cli_unlink(cli->tree, fname);
378
379         /* and check attrib on create */
380         io.openx.in.open_func = OPENX_OPEN_FUNC_FAIL | OPENX_OPEN_FUNC_CREATE;
381         io.openx.in.search_attrs = 0;
382         io.openx.in.file_attrs = FILE_ATTRIBUTE_SYSTEM;
383         status = smb_raw_open(cli->tree, mem_ctx, &io);
384         CHECK_STATUS(status, NT_STATUS_OK);
385         CHECK_ALL_INFO(FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE, attrib);
386         cli_close(cli->tree, io.openx.out.fnum);
387         cli_unlink(cli->tree, fname);
388
389         /* check timeout on create - win2003 ignores the timeout! */
390         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
391         io.openx.in.file_attrs = 0;
392         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_ALL;
393         status = smb_raw_open(cli->tree, mem_ctx, &io);
394         CHECK_STATUS(status, NT_STATUS_OK);
395         fnum = io.openx.out.fnum;
396
397         io.openx.in.timeout = 20000;
398         start_timer();
399         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_NONE;
400         status = smb_raw_open(cli->tree, mem_ctx, &io);
401         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
402         if (end_timer() > 3) {
403                 printf("(%d) Incorrect timing in openx with timeout - waited %d seconds\n",
404                        __LINE__, (int)end_timer());
405                 ret = False;
406         }
407         cli_close(cli->tree, fnum);
408         cli_unlink(cli->tree, fname);
409
410         /* now this is a really weird one - open for execute implies create?! */
411         io.openx.in.fname = fname;
412         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
413         io.openx.in.open_mode = OPENX_MODE_ACCESS_EXEC | OPENX_MODE_DENY_NONE;
414         io.openx.in.search_attrs = 0;
415         io.openx.in.open_func = OPENX_OPEN_FUNC_FAIL;
416         io.openx.in.file_attrs = 0;
417         io.openx.in.write_time = 0;
418         io.openx.in.size = 0;
419         io.openx.in.timeout = 0;
420         status = smb_raw_open(cli->tree, mem_ctx, &io);
421         CHECK_STATUS(status, NT_STATUS_OK);
422         cli_close(cli->tree, io.openx.out.fnum);
423
424         /* check the extended return flag */
425         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO | OPENX_FLAGS_EXTENDED_RETURN;
426         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
427         status = smb_raw_open(cli->tree, mem_ctx, &io);
428         CHECK_STATUS(status, NT_STATUS_OK);
429         CHECK_VAL(io.openx.out.access_mask, STD_RIGHT_ALL_ACCESS);
430         cli_close(cli->tree, io.openx.out.fnum);
431
432 done:
433         cli_close(cli->tree, fnum);
434         cli_unlink(cli->tree, fname);
435
436         return ret;
437 }
438
439
440 /*
441   test RAW_OPEN_T2OPEN
442
443   I can't work out how to get win2003 to accept a create file via TRANS2_OPEN, which
444   is why you see all the ACCESS_DENIED results below. When we finally work this out then this
445   test will make more sense
446 */
447 static BOOL test_t2open(struct cli_state *cli, TALLOC_CTX *mem_ctx)
448 {
449         union smb_open io;
450         union smb_fileinfo finfo;
451         const char *fname = BASEDIR "\\torture_t2open.txt";
452         NTSTATUS status;
453         int fnum;
454         BOOL ret = True;
455         int i;
456         struct {
457                 uint16 open_func;
458                 BOOL with_file;
459                 NTSTATUS correct_status;
460         } open_funcs[] = {
461                 { OPENX_OPEN_FUNC_OPEN,                           True,  NT_STATUS_OK },
462                 { OPENX_OPEN_FUNC_OPEN,                           False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
463                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OK },
464                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_ACCESS_DENIED },
465                 { OPENX_OPEN_FUNC_FAIL,                           True,  NT_STATUS_OBJECT_NAME_COLLISION },
466                 { OPENX_OPEN_FUNC_FAIL,                           False, NT_STATUS_ACCESS_DENIED },
467                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OBJECT_NAME_COLLISION },
468                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_ACCESS_DENIED },
469                 { OPENX_OPEN_FUNC_TRUNC,                          True,  NT_STATUS_ACCESS_DENIED },
470                 { OPENX_OPEN_FUNC_TRUNC,                          False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
471                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_ACCESS_DENIED },
472                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_ACCESS_DENIED },
473         };
474
475         printf("Checking RAW_OPEN_T2OPEN\n");
476
477         io.t2open.level = RAW_OPEN_T2OPEN;
478         io.t2open.in.fname = fname;
479         io.t2open.in.flags = OPENX_FLAGS_ADDITIONAL_INFO | 
480                 OPENX_FLAGS_EA_LEN | OPENX_FLAGS_EXTENDED_RETURN;
481         io.t2open.in.open_mode = OPENX_MODE_DENY_NONE | OPENX_MODE_ACCESS_RDWR;
482         io.t2open.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
483         io.t2open.in.file_attrs = 0;
484         io.t2open.in.write_time = 0;
485         io.t2open.in.size = 0;
486         io.t2open.in.timeout = 0;
487
488         io.t2open.in.eas = talloc(mem_ctx, sizeof(io.t2open.in.eas[0]));
489         io.t2open.in.num_eas = 1;
490         io.t2open.in.eas[0].flags = 0;
491         io.t2open.in.eas[0].name.s = "EAONE";
492         io.t2open.in.eas[0].value = data_blob_talloc(mem_ctx, "1", 1);
493
494         /* check all combinations of open_func */
495         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
496                 if (open_funcs[i].with_file) {
497                         fnum = create_complex_file(cli, mem_ctx, fname);
498                         if (fnum == -1) {
499                                 d_printf("Failed to create file %s - %s\n", fname, cli_errstr(cli->tree));
500                                 ret = False;
501                                 goto done;
502                         }
503                         cli_close(cli->tree, fnum);
504                 }
505                 io.t2open.in.open_func = open_funcs[i].open_func;
506                 status = smb_raw_open(cli->tree, mem_ctx, &io);
507                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
508                         printf("(%d) incorrect status %s should be %s (i=%d with_file=%d open_func=0x%x)\n", 
509                                __LINE__, nt_errstr(status), nt_errstr(open_funcs[i].correct_status),
510                                i, (int)open_funcs[i].with_file, (int)open_funcs[i].open_func);
511                         ret = False;
512                 }
513                 if (NT_STATUS_IS_OK(status) || open_funcs[i].with_file) {
514                         cli_close(cli->tree, io.t2open.out.fnum);
515                         cli_unlink(cli->tree, fname);
516                 }
517         }
518
519         /* check the basic return fields */
520         fnum = create_complex_file(cli, mem_ctx, fname);
521         cli_close(cli->tree, fnum);
522         io.t2open.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
523         status = smb_raw_open(cli->tree, mem_ctx, &io);
524         CHECK_STATUS(status, NT_STATUS_OK);
525         fnum = io.t2open.out.fnum;
526
527         CHECK_ALL_INFO(io.t2open.out.size, size);
528         CHECK_VAL(io.t2open.out.write_time, 0);
529         CHECK_ALL_INFO(io.t2open.out.attrib, attrib);
530         CHECK_VAL(io.t2open.out.access, OPENX_MODE_DENY_NONE | OPENX_MODE_ACCESS_RDWR);
531         CHECK_VAL(io.t2open.out.ftype, 0);
532         CHECK_VAL(io.t2open.out.devstate, 0);
533         CHECK_VAL(io.t2open.out.action, OPENX_ACTION_EXISTED);
534         cli_close(cli->tree, fnum);
535
536         /* now check the search attrib for hidden files - win2003 ignores this? */
537         SET_ATTRIB(FILE_ATTRIBUTE_HIDDEN);
538         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN, attrib);
539
540         status = smb_raw_open(cli->tree, mem_ctx, &io);
541         CHECK_STATUS(status, NT_STATUS_OK);
542         cli_close(cli->tree, io.t2open.out.fnum);
543
544         status = smb_raw_open(cli->tree, mem_ctx, &io);
545         CHECK_STATUS(status, NT_STATUS_OK);
546         cli_close(cli->tree, io.t2open.out.fnum);
547
548         SET_ATTRIB(FILE_ATTRIBUTE_NORMAL);
549         cli_unlink(cli->tree, fname);
550
551         /* and check attrib on create */
552         io.t2open.in.open_func = OPENX_OPEN_FUNC_FAIL | OPENX_OPEN_FUNC_CREATE;
553         io.t2open.in.file_attrs = FILE_ATTRIBUTE_SYSTEM;
554         status = smb_raw_open(cli->tree, mem_ctx, &io);
555         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
556
557         /* check timeout on create - win2003 ignores the timeout! */
558         io.t2open.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
559         io.t2open.in.file_attrs = 0;
560         io.t2open.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_ALL;
561         status = smb_raw_open(cli->tree, mem_ctx, &io);
562         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
563
564 done:
565         cli_close(cli->tree, fnum);
566         cli_unlink(cli->tree, fname);
567
568         return ret;
569 }
570         
571
572 /*
573   test RAW_OPEN_NTCREATEX
574 */
575 static BOOL test_ntcreatex(struct cli_state *cli, TALLOC_CTX *mem_ctx)
576 {
577         union smb_open io;
578         union smb_fileinfo finfo;
579         const char *fname = BASEDIR "\\torture_ntcreatex.txt";
580         const char *dname = BASEDIR "\\torture_ntcreatex.dir";
581         NTSTATUS status;
582         int fnum;
583         BOOL ret = True;
584         int i;
585         struct {
586                 uint32 open_disp;
587                 BOOL with_file;
588                 NTSTATUS correct_status;
589         } open_funcs[] = {
590                 { NTCREATEX_DISP_SUPERSEDE,     True,  NT_STATUS_OK },
591                 { NTCREATEX_DISP_SUPERSEDE,     False, NT_STATUS_OK },
592                 { NTCREATEX_DISP_OPEN,          True,  NT_STATUS_OK },
593                 { NTCREATEX_DISP_OPEN,          False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
594                 { NTCREATEX_DISP_CREATE,        True,  NT_STATUS_OBJECT_NAME_COLLISION },
595                 { NTCREATEX_DISP_CREATE,        False, NT_STATUS_OK },
596                 { NTCREATEX_DISP_OPEN_IF,       True,  NT_STATUS_OK },
597                 { NTCREATEX_DISP_OPEN_IF,       False, NT_STATUS_OK },
598                 { NTCREATEX_DISP_OVERWRITE,     True,  NT_STATUS_OK },
599                 { NTCREATEX_DISP_OVERWRITE,     False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
600                 { NTCREATEX_DISP_OVERWRITE_IF,  True,  NT_STATUS_OK },
601                 { NTCREATEX_DISP_OVERWRITE_IF,  False, NT_STATUS_OK },
602                 { 6,                            True,  NT_STATUS_INVALID_PARAMETER },
603                 { 6,                            False, NT_STATUS_INVALID_PARAMETER },
604         };
605
606         printf("Checking RAW_OPEN_NTCREATEX\n");
607
608         /* reasonable default parameters */
609         io.generic.level = RAW_OPEN_NTCREATEX;
610         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
611         io.ntcreatex.in.root_fid = 0;
612         io.ntcreatex.in.access_mask = GENERIC_RIGHTS_FILE_ALL_ACCESS;
613         io.ntcreatex.in.alloc_size = 1024*1024;
614         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
615         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
616         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
617         io.ntcreatex.in.create_options = 0;
618         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
619         io.ntcreatex.in.security_flags = 0;
620         io.ntcreatex.in.fname = fname;
621
622         /* test the open disposition */
623         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
624                 if (open_funcs[i].with_file) {
625                         fnum = cli_open(cli->tree, fname, O_CREAT|O_RDWR|O_TRUNC, DENY_NONE);
626                         if (fnum == -1) {
627                                 d_printf("Failed to create file %s - %s\n", fname, cli_errstr(cli->tree));
628                                 ret = False;
629                                 goto done;
630                         }
631                         cli_close(cli->tree, fnum);
632                 }
633                 io.ntcreatex.in.open_disposition = open_funcs[i].open_disp;
634                 status = smb_raw_open(cli->tree, mem_ctx, &io);
635                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
636                         printf("(%d) incorrect status %s should be %s (i=%d with_file=%d open_disp=%d)\n", 
637                                __LINE__, nt_errstr(status), nt_errstr(open_funcs[i].correct_status),
638                                i, (int)open_funcs[i].with_file, (int)open_funcs[i].open_disp);
639                         ret = False;
640                 }
641                 if (NT_STATUS_IS_OK(status) || open_funcs[i].with_file) {
642                         cli_close(cli->tree, io.ntcreatex.out.fnum);
643                         cli_unlink(cli->tree, fname);
644                 }
645         }
646
647         /* basic field testing */
648         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
649
650         status = smb_raw_open(cli->tree, mem_ctx, &io);
651         CHECK_STATUS(status, NT_STATUS_OK);
652         fnum = io.ntcreatex.out.fnum;
653
654         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
655         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
656         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
657         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
658         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
659         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
660         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
661         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
662         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
663         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
664         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
665
666         /* check fields when the file already existed */
667         cli_close(cli->tree, fnum);
668         cli_unlink(cli->tree, fname);
669         fnum = create_complex_file(cli, mem_ctx, fname);
670         if (fnum == -1) {
671                 ret = False;
672                 goto done;
673         }
674         cli_close(cli->tree, fnum);
675
676         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
677         status = smb_raw_open(cli->tree, mem_ctx, &io);
678         CHECK_STATUS(status, NT_STATUS_OK);
679         fnum = io.ntcreatex.out.fnum;
680
681         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
682         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_EXISTED);
683         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
684         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
685         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
686         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
687         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
688         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
689         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
690         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
691         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
692         cli_close(cli->tree, fnum);
693         cli_unlink(cli->tree, fname);
694
695
696         /* create a directory */
697         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
698         io.ntcreatex.in.access_mask = GENERIC_RIGHTS_FILE_ALL_ACCESS;
699         io.ntcreatex.in.alloc_size = 0;
700         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
701         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
702         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
703         io.ntcreatex.in.create_options = 0;
704         io.ntcreatex.in.fname = dname;
705         fname = dname;
706
707         cli_rmdir(cli->tree, fname);
708         cli_unlink(cli->tree, fname);
709
710         io.ntcreatex.in.access_mask = SEC_RIGHT_MAXIMUM_ALLOWED;
711         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
712         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
713         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
714         status = smb_raw_open(cli->tree, mem_ctx, &io);
715         CHECK_STATUS(status, NT_STATUS_OK);
716         fnum = io.ntcreatex.out.fnum;
717
718         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
719         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
720         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
721         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
722         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
723         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
724         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
725         CHECK_VAL(io.ntcreatex.out.attrib, FILE_ATTRIBUTE_DIRECTORY);
726         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
727         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
728         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
729         CHECK_VAL(io.ntcreatex.out.is_directory, 1);
730         CHECK_VAL(io.ntcreatex.out.size, 0);
731         CHECK_VAL(io.ntcreatex.out.alloc_size, 0);
732         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
733         cli_unlink(cli->tree, fname);
734         
735
736 done:
737         cli_close(cli->tree, fnum);
738         cli_unlink(cli->tree, fname);
739
740         return ret;
741 }
742
743
744 /*
745   test RAW_OPEN_MKNEW
746 */
747 static BOOL test_mknew(struct cli_state *cli, TALLOC_CTX *mem_ctx)
748 {
749         union smb_open io;
750         const char *fname = BASEDIR "\\torture_mknew.txt";
751         NTSTATUS status;
752         int fnum;
753         BOOL ret = True;
754         time_t basetime = (time(NULL) + 3600*24*3) & ~1;
755         union smb_fileinfo finfo;
756
757         printf("Checking RAW_OPEN_MKNEW\n");
758
759         io.mknew.level = RAW_OPEN_MKNEW;
760         io.mknew.in.attrib = 0;
761         io.mknew.in.write_time = 0;
762         io.mknew.in.fname = fname;
763         status = smb_raw_open(cli->tree, mem_ctx, &io);
764         CHECK_STATUS(status, NT_STATUS_OK);
765         fnum = io.mknew.out.fnum;
766
767         status = smb_raw_open(cli->tree, mem_ctx, &io);
768         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_COLLISION);
769
770         cli_close(cli->tree, fnum);
771         cli_unlink(cli->tree, fname);
772
773         /* make sure write_time works */
774         io.mknew.in.write_time = basetime;
775         status = smb_raw_open(cli->tree, mem_ctx, &io);
776         CHECK_STATUS(status, NT_STATUS_OK);
777         fnum = io.mknew.out.fnum;
778         CHECK_TIME(basetime, write_time);
779
780         cli_close(cli->tree, fnum);
781         cli_unlink(cli->tree, fname);
782
783         /* make sure file_attrs works */
784         io.mknew.in.attrib = FILE_ATTRIBUTE_HIDDEN;
785         status = smb_raw_open(cli->tree, mem_ctx, &io);
786         CHECK_STATUS(status, NT_STATUS_OK);
787         fnum = io.mknew.out.fnum;
788         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE, attrib);
789         
790 done:
791         cli_close(cli->tree, fnum);
792         cli_unlink(cli->tree, fname);
793
794         return ret;
795 }
796
797
798 /*
799   test RAW_OPEN_CREATE
800 */
801 static BOOL test_create(struct cli_state *cli, TALLOC_CTX *mem_ctx)
802 {
803         union smb_open io;
804         const char *fname = BASEDIR "\\torture_create.txt";
805         NTSTATUS status;
806         int fnum;
807         BOOL ret = True;
808         time_t basetime = (time(NULL) + 3600*24*3) & ~1;
809         union smb_fileinfo finfo;
810
811         printf("Checking RAW_OPEN_CREATE\n");
812
813         io.create.level = RAW_OPEN_CREATE;
814         io.create.in.attrib = 0;
815         io.create.in.write_time = 0;
816         io.create.in.fname = fname;
817         status = smb_raw_open(cli->tree, mem_ctx, &io);
818         CHECK_STATUS(status, NT_STATUS_OK);
819         fnum = io.create.out.fnum;
820
821         status = smb_raw_open(cli->tree, mem_ctx, &io);
822         CHECK_STATUS(status, NT_STATUS_OK);
823
824         cli_close(cli->tree, io.create.out.fnum);
825         cli_close(cli->tree, fnum);
826         cli_unlink(cli->tree, fname);
827
828         /* make sure write_time works */
829         io.create.in.write_time = basetime;
830         status = smb_raw_open(cli->tree, mem_ctx, &io);
831         CHECK_STATUS(status, NT_STATUS_OK);
832         fnum = io.create.out.fnum;
833         CHECK_TIME(basetime, write_time);
834
835         cli_close(cli->tree, fnum);
836         cli_unlink(cli->tree, fname);
837
838         /* make sure file_attrs works */
839         io.create.in.attrib = FILE_ATTRIBUTE_HIDDEN;
840         status = smb_raw_open(cli->tree, mem_ctx, &io);
841         CHECK_STATUS(status, NT_STATUS_OK);
842         fnum = io.create.out.fnum;
843         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE, attrib);
844         
845 done:
846         cli_close(cli->tree, fnum);
847         cli_unlink(cli->tree, fname);
848
849         return ret;
850 }
851
852
853 /*
854   test RAW_OPEN_CTEMP
855 */
856 static BOOL test_ctemp(struct cli_state *cli, TALLOC_CTX *mem_ctx)
857 {
858         union smb_open io;
859         NTSTATUS status;
860         int fnum;
861         BOOL ret = True;
862         time_t basetime = (time(NULL) + 3600*24*3) & ~1;
863         union smb_fileinfo finfo;
864         const char *name, *fname = NULL;
865
866         printf("Checking RAW_OPEN_CTEMP\n");
867
868         io.ctemp.level = RAW_OPEN_CTEMP;
869         io.ctemp.in.attrib = FILE_ATTRIBUTE_HIDDEN;
870         io.ctemp.in.write_time = basetime;
871         io.ctemp.in.directory = BASEDIR;
872         status = smb_raw_open(cli->tree, mem_ctx, &io);
873         CHECK_STATUS(status, NT_STATUS_OK);
874         fnum = io.ctemp.out.fnum;
875
876         name = io.ctemp.out.name;
877
878         finfo.generic.level = RAW_FILEINFO_NAME_INFO;
879         finfo.generic.in.fnum = fnum;
880         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
881         CHECK_STATUS(status, NT_STATUS_OK);
882
883         fname = finfo.name_info.out.fname.s;
884         d_printf("ctemp name=%s  real name=%s\n", name, fname);
885
886         CHECK_TIME(basetime, write_time);
887
888 done:
889         cli_close(cli->tree, fnum);
890         if (fname) {
891                 cli_unlink(cli->tree, fname);
892         }
893
894         return ret;
895 }
896
897 /* basic testing of all RAW_OPEN_* calls 
898 */
899 BOOL torture_raw_open(int dummy)
900 {
901         struct cli_state *cli;
902         BOOL ret = True;
903         TALLOC_CTX *mem_ctx;
904
905         if (!torture_open_connection(&cli)) {
906                 return False;
907         }
908
909         mem_ctx = talloc_init("torture_raw_open");
910
911         if (cli_deltree(cli->tree, BASEDIR) == -1) {
912                 printf("Failed to clean " BASEDIR "\n");
913                 return False;
914         }
915         if (NT_STATUS_IS_ERR(cli_mkdir(cli->tree, BASEDIR))) {
916                 printf("Failed to create " BASEDIR " - %s\n", cli_errstr(cli->tree));
917                 return False;
918         }
919
920         if (!test_open(cli, mem_ctx)) {
921                 ret = False;
922         }
923
924         if (!test_openx(cli, mem_ctx)) {
925                 ret = False;
926         }
927
928         if (!test_ntcreatex(cli, mem_ctx)) {
929                 ret = False;
930         }
931
932         if (!test_t2open(cli, mem_ctx)) {
933                 ret = False;
934         }
935
936         if (!test_mknew(cli, mem_ctx)) {
937                 ret = False;
938         }
939
940         if (!test_create(cli, mem_ctx)) {
941                 ret = False;
942         }
943
944         if (!test_ctemp(cli, mem_ctx)) {
945                 ret = False;
946         }
947
948         smb_raw_exit(cli->session);
949         cli_deltree(cli->tree, BASEDIR);
950
951         torture_close_connection(cli);
952         talloc_destroy(mem_ctx);
953         return ret;
954 }