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