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