f2d5b0c199a9015282a80eb53a06b3550b36dd5e
[kai/samba.git] / source4 / torture / raw / setfileinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RAW_SFILEINFO_* individual test suite
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "torture/torture.h"
22 #include "system/time.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/libcli.h"
25 #include "torture/util.h"
26 #include "torture/raw/proto.h"
27
28 #define BASEDIR "\\testsfileinfo"
29
30 /* basic testing of all RAW_SFILEINFO_* calls 
31    for each call we test that it succeeds, and where possible test 
32    for consistency between the calls. 
33 */
34 bool torture_raw_sfileinfo(struct torture_context *torture, 
35                                                    struct smbcli_state *cli)
36 {
37         BOOL ret = True;
38         int fnum_saved, d_fnum, fnum2, fnum = -1;
39         char *fnum_fname;
40         char *fnum_fname_new;
41         char *path_fname;
42         char *path_fname_new;
43         union smb_fileinfo finfo1, finfo2;
44         union smb_setfileinfo sfinfo;
45         NTSTATUS status, status2;
46         const char *call_name;
47         time_t basetime = (time(NULL) - 86400) & ~1;
48         BOOL check_fnum;
49         int n = time(NULL) % 100;
50         
51         asprintf(&path_fname, BASEDIR "\\fname_test_%d.txt", n);
52         asprintf(&path_fname_new, BASEDIR "\\fname_test_new_%d.txt", n);
53         asprintf(&fnum_fname, BASEDIR "\\fnum_test_%d.txt", n);
54         asprintf(&fnum_fname_new, BASEDIR "\\fnum_test_new_%d.txt", n);
55
56         if (!torture_setup_dir(cli, BASEDIR)) {
57                 return False;
58         }
59
60 #define RECREATE_FILE(fname) do { \
61         if (fnum != -1) smbcli_close(cli->tree, fnum); \
62         fnum = create_complex_file(cli, torture, fname); \
63         if (fnum == -1) { \
64                 printf("(%s) ERROR: open of %s failed (%s)\n", \
65                        __location__, fname, smbcli_errstr(cli->tree)); \
66                 ret = False; \
67                 goto done; \
68         }} while (0)
69
70 #define RECREATE_BOTH do { \
71                 RECREATE_FILE(path_fname); \
72                 smbcli_close(cli->tree, fnum); \
73                 RECREATE_FILE(fnum_fname); \
74         } while (0)
75
76         RECREATE_BOTH;
77         
78 #define CHECK_CALL_FNUM(call, rightstatus) do { \
79         check_fnum = True; \
80         call_name = #call; \
81         sfinfo.generic.level = RAW_SFILEINFO_ ## call; \
82         sfinfo.generic.in.file.fnum = fnum; \
83         status = smb_raw_setfileinfo(cli->tree, &sfinfo); \
84         if (!NT_STATUS_EQUAL(status, rightstatus)) { \
85                 printf("(%s) %s - %s (should be %s)\n", __location__, #call, \
86                         nt_errstr(status), nt_errstr(rightstatus)); \
87                 ret = False; \
88         } \
89         finfo1.generic.level = RAW_FILEINFO_ALL_INFO; \
90         finfo1.generic.in.file.fnum = fnum; \
91         status2 = smb_raw_fileinfo(cli->tree, torture, &finfo1); \
92         if (!NT_STATUS_IS_OK(status2)) { \
93                 printf("(%s) %s pathinfo - %s\n", __location__, #call, nt_errstr(status)); \
94                 ret = False; \
95         }} while (0)
96
97 #define CHECK_CALL_PATH(call, rightstatus) do { \
98         check_fnum = False; \
99         call_name = #call; \
100         sfinfo.generic.level = RAW_SFILEINFO_ ## call; \
101         sfinfo.generic.in.file.path = path_fname; \
102         status = smb_raw_setpathinfo(cli->tree, &sfinfo); \
103         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) { \
104                 sfinfo.generic.in.file.path = path_fname_new; \
105                 status = smb_raw_setpathinfo(cli->tree, &sfinfo); \
106         } \
107         if (!NT_STATUS_EQUAL(status, rightstatus)) { \
108                 printf("(%s) %s - %s (should be %s)\n", __location__, #call, \
109                         nt_errstr(status), nt_errstr(rightstatus)); \
110                 ret = False; \
111         } \
112         finfo1.generic.level = RAW_FILEINFO_ALL_INFO; \
113         finfo1.generic.in.file.path = path_fname; \
114         status2 = smb_raw_pathinfo(cli->tree, torture, &finfo1); \
115         if (NT_STATUS_EQUAL(status2, NT_STATUS_OBJECT_NAME_NOT_FOUND)) { \
116                 finfo1.generic.in.file.path = path_fname_new; \
117                 status2 = smb_raw_pathinfo(cli->tree, torture, &finfo1); \
118         } \
119         if (!NT_STATUS_IS_OK(status2)) { \
120                 printf("(%s) %s pathinfo - %s\n", __location__, #call, nt_errstr(status2)); \
121                 ret = False; \
122         }} while (0)
123
124 #define CHECK1(call) \
125         do { if (NT_STATUS_IS_OK(status)) { \
126                 finfo2.generic.level = RAW_FILEINFO_ ## call; \
127                 if (check_fnum) { \
128                         finfo2.generic.in.file.fnum = fnum; \
129                         status2 = smb_raw_fileinfo(cli->tree, torture, &finfo2); \
130                 } else { \
131                         finfo2.generic.in.file.path = path_fname; \
132                         status2 = smb_raw_pathinfo(cli->tree, torture, &finfo2); \
133                         if (NT_STATUS_EQUAL(status2, NT_STATUS_OBJECT_NAME_NOT_FOUND)) { \
134                                 finfo2.generic.in.file.path = path_fname_new; \
135                                 status2 = smb_raw_pathinfo(cli->tree, torture, &finfo2); \
136                         } \
137                 } \
138                 if (!NT_STATUS_IS_OK(status2)) { \
139                         printf("%s - %s\n", #call, nt_errstr(status2)); \
140                         ret = False; \
141                 } \
142         }} while (0)
143
144 #define CHECK_VALUE(call, stype, field, value) do { \
145         CHECK1(call); \
146         if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2) && finfo2.stype.out.field != value) { \
147                 printf("(%s) %s - %s/%s should be 0x%x - 0x%x\n", __location__, \
148                        call_name, #stype, #field, \
149                        (uint_t)value, (uint_t)finfo2.stype.out.field); \
150                 dump_all_info(torture, &finfo1); \
151                 ret = False; \
152         }} while (0)
153
154 #define CHECK_TIME(call, stype, field, value) do { \
155         CHECK1(call); \
156         if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2) && nt_time_to_unix(finfo2.stype.out.field) != value) { \
157                 printf("(%s) %s - %s/%s should be 0x%x - 0x%x\n", __location__, \
158                         call_name, #stype, #field, \
159                         (uint_t)value, \
160                         (uint_t)nt_time_to_unix(finfo2.stype.out.field)); \
161                 printf("\t%s", timestring(torture, value)); \
162                 printf("\t%s\n", nt_time_string(torture, finfo2.stype.out.field)); \
163                 dump_all_info(torture, &finfo1); \
164                 ret = False; \
165         }} while (0)
166
167 #define CHECK_STR(call, stype, field, value) do { \
168         CHECK1(call); \
169         if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2) && strcmp(finfo2.stype.out.field, value) != 0) { \
170                 printf("(%s) %s - %s/%s should be '%s' - '%s'\n", __location__, \
171                         call_name, #stype, #field, \
172                         value, \
173                         finfo2.stype.out.field); \
174                 dump_all_info(torture, &finfo1); \
175                 ret = False; \
176         }} while (0)
177
178 #define CHECK_STATUS(status, correct) do { \
179         if (!NT_STATUS_EQUAL(status, correct)) { \
180                 printf("(%s) Incorrect status %s - should be %s\n", \
181                        __location__, nt_errstr(status), nt_errstr(correct)); \
182                 ret = False; \
183                 goto done; \
184         }} while (0)
185
186         
187         printf("test setattr\n");
188         sfinfo.setattr.in.attrib = FILE_ATTRIBUTE_READONLY;
189         sfinfo.setattr.in.write_time = basetime;
190         CHECK_CALL_PATH(SETATTR, NT_STATUS_OK);
191         CHECK_VALUE  (ALL_INFO, all_info, attrib,     FILE_ATTRIBUTE_READONLY);
192         CHECK_TIME   (ALL_INFO, all_info, write_time, basetime);
193
194         printf("setting to NORMAL doesn't do anything\n");
195         sfinfo.setattr.in.attrib = FILE_ATTRIBUTE_NORMAL;
196         sfinfo.setattr.in.write_time = 0;
197         CHECK_CALL_PATH(SETATTR, NT_STATUS_OK);
198         CHECK_VALUE(ALL_INFO, all_info, attrib,     FILE_ATTRIBUTE_READONLY);
199         CHECK_TIME (ALL_INFO, all_info, write_time, basetime);
200
201         printf("a zero write_time means don't change\n");
202         sfinfo.setattr.in.attrib = 0;
203         sfinfo.setattr.in.write_time = 0;
204         CHECK_CALL_PATH(SETATTR, NT_STATUS_OK);
205         CHECK_VALUE(ALL_INFO, all_info, attrib,     FILE_ATTRIBUTE_NORMAL);
206         CHECK_TIME (ALL_INFO, all_info, write_time, basetime);
207
208         printf("test setattre\n");
209         sfinfo.setattre.in.create_time = basetime + 20;
210         sfinfo.setattre.in.access_time = basetime + 30;
211         sfinfo.setattre.in.write_time  = basetime + 40;
212         CHECK_CALL_FNUM(SETATTRE, NT_STATUS_OK);
213         CHECK_TIME(ALL_INFO, all_info, create_time, basetime + 20);
214         CHECK_TIME(ALL_INFO, all_info, access_time, basetime + 30);
215         CHECK_TIME(ALL_INFO, all_info, write_time,  basetime + 40);
216
217         sfinfo.setattre.in.create_time = 0;
218         sfinfo.setattre.in.access_time = 0;
219         sfinfo.setattre.in.write_time  = 0;
220         CHECK_CALL_FNUM(SETATTRE, NT_STATUS_OK);
221         CHECK_TIME(ALL_INFO, all_info, create_time, basetime + 20);
222         CHECK_TIME(ALL_INFO, all_info, access_time, basetime + 30);
223         CHECK_TIME(ALL_INFO, all_info, write_time,  basetime + 40);
224
225         printf("test standard level\n");
226         sfinfo.standard.in.create_time = basetime + 100;
227         sfinfo.standard.in.access_time = basetime + 200;
228         sfinfo.standard.in.write_time  = basetime + 300;
229         CHECK_CALL_FNUM(STANDARD, NT_STATUS_OK);
230         CHECK_TIME(ALL_INFO, all_info, create_time, basetime + 100);
231         CHECK_TIME(ALL_INFO, all_info, access_time, basetime + 200);
232         CHECK_TIME(ALL_INFO, all_info, write_time,  basetime + 300);
233
234         printf("test basic_info level\n");
235         basetime += 86400;
236         unix_to_nt_time(&sfinfo.basic_info.in.create_time, basetime + 100);
237         unix_to_nt_time(&sfinfo.basic_info.in.access_time, basetime + 200);
238         unix_to_nt_time(&sfinfo.basic_info.in.write_time,  basetime + 300);
239         unix_to_nt_time(&sfinfo.basic_info.in.change_time, basetime + 400);
240         sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_READONLY;
241         CHECK_CALL_FNUM(BASIC_INFO, NT_STATUS_OK);
242         CHECK_TIME(ALL_INFO, all_info, create_time, basetime + 100);
243         CHECK_TIME(ALL_INFO, all_info, access_time, basetime + 200);
244         CHECK_TIME(ALL_INFO, all_info, write_time,  basetime + 300);
245         CHECK_TIME(ALL_INFO, all_info, change_time, basetime + 400);
246         CHECK_VALUE(ALL_INFO, all_info, attrib,     FILE_ATTRIBUTE_READONLY);
247
248         printf("a zero time means don't change\n");
249         unix_to_nt_time(&sfinfo.basic_info.in.create_time, 0);
250         unix_to_nt_time(&sfinfo.basic_info.in.access_time, 0);
251         unix_to_nt_time(&sfinfo.basic_info.in.write_time,  0);
252         unix_to_nt_time(&sfinfo.basic_info.in.change_time, 0);
253         sfinfo.basic_info.in.attrib = 0;
254         CHECK_CALL_FNUM(BASIC_INFO, NT_STATUS_OK);
255         CHECK_TIME(ALL_INFO, all_info, create_time, basetime + 100);
256         CHECK_TIME(ALL_INFO, all_info, access_time, basetime + 200);
257         CHECK_TIME(ALL_INFO, all_info, write_time,  basetime + 300);
258         CHECK_TIME(ALL_INFO, all_info, change_time, basetime + 400);
259         CHECK_VALUE(ALL_INFO, all_info, attrib,     FILE_ATTRIBUTE_READONLY);
260
261         printf("test basic_information level\n");
262         basetime += 86400;
263         unix_to_nt_time(&sfinfo.basic_info.in.create_time, basetime + 100);
264         unix_to_nt_time(&sfinfo.basic_info.in.access_time, basetime + 200);
265         unix_to_nt_time(&sfinfo.basic_info.in.write_time,  basetime + 300);
266         unix_to_nt_time(&sfinfo.basic_info.in.change_time, basetime + 400);
267         sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
268         CHECK_CALL_FNUM(BASIC_INFORMATION, NT_STATUS_OK);
269         CHECK_TIME(ALL_INFO, all_info, create_time, basetime + 100);
270         CHECK_TIME(ALL_INFO, all_info, access_time, basetime + 200);
271         CHECK_TIME(ALL_INFO, all_info, write_time,  basetime + 300);
272         CHECK_TIME(ALL_INFO, all_info, change_time, basetime + 400);
273         CHECK_VALUE(ALL_INFO, all_info, attrib,     FILE_ATTRIBUTE_NORMAL);
274
275         CHECK_CALL_PATH(BASIC_INFORMATION, NT_STATUS_OK);
276         CHECK_TIME(ALL_INFO, all_info, create_time, basetime + 100);
277         CHECK_TIME(ALL_INFO, all_info, access_time, basetime + 200);
278         CHECK_TIME(ALL_INFO, all_info, write_time,  basetime + 300);
279         CHECK_TIME(ALL_INFO, all_info, change_time, basetime + 400);
280         CHECK_VALUE(ALL_INFO, all_info, attrib,     FILE_ATTRIBUTE_NORMAL);
281
282         printf("a zero time means don't change\n");
283         unix_to_nt_time(&sfinfo.basic_info.in.create_time, 0);
284         unix_to_nt_time(&sfinfo.basic_info.in.access_time, 0);
285         unix_to_nt_time(&sfinfo.basic_info.in.write_time,  0);
286         unix_to_nt_time(&sfinfo.basic_info.in.change_time, 0);
287         sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
288         CHECK_CALL_FNUM(BASIC_INFORMATION, NT_STATUS_OK);
289         CHECK_TIME(ALL_INFO, all_info, create_time, basetime + 100);
290         CHECK_TIME(ALL_INFO, all_info, access_time, basetime + 200);
291         CHECK_TIME(ALL_INFO, all_info, write_time,  basetime + 300);
292         CHECK_TIME(ALL_INFO, all_info, change_time, basetime + 400);
293         CHECK_VALUE(ALL_INFO, all_info, attrib,     FILE_ATTRIBUTE_NORMAL);
294
295         CHECK_CALL_PATH(BASIC_INFORMATION, NT_STATUS_OK);
296         CHECK_TIME(ALL_INFO, all_info, create_time, basetime + 100);
297         CHECK_TIME(ALL_INFO, all_info, access_time, basetime + 200);
298         CHECK_TIME(ALL_INFO, all_info, write_time,  basetime + 300);
299
300         /* interesting - w2k3 leaves change_time as current time for 0 change time
301            in setpathinfo
302           CHECK_TIME(ALL_INFO, all_info, change_time, basetime + 400);
303         */
304         CHECK_VALUE(ALL_INFO, all_info, attrib,     FILE_ATTRIBUTE_NORMAL);
305
306         printf("test disposition_info level\n");
307         sfinfo.disposition_info.in.delete_on_close = 1;
308         CHECK_CALL_FNUM(DISPOSITION_INFO, NT_STATUS_OK);
309         CHECK_VALUE(ALL_INFO, all_info, delete_pending, 1);
310         CHECK_VALUE(ALL_INFO, all_info, nlink, 0);
311
312         sfinfo.disposition_info.in.delete_on_close = 0;
313         CHECK_CALL_FNUM(DISPOSITION_INFO, NT_STATUS_OK);
314         CHECK_VALUE(ALL_INFO, all_info, delete_pending, 0);
315         CHECK_VALUE(ALL_INFO, all_info, nlink, 1);
316
317         printf("test disposition_information level\n");
318         sfinfo.disposition_info.in.delete_on_close = 1;
319         CHECK_CALL_FNUM(DISPOSITION_INFORMATION, NT_STATUS_OK);
320         CHECK_VALUE(ALL_INFO, all_info, delete_pending, 1);
321         CHECK_VALUE(ALL_INFO, all_info, nlink, 0);
322
323         /* this would delete the file! */
324         /*
325           CHECK_CALL_PATH(DISPOSITION_INFORMATION, NT_STATUS_OK);
326           CHECK_VALUE(ALL_INFO, all_info, delete_pending, 1);
327           CHECK_VALUE(ALL_INFO, all_info, nlink, 0);
328         */
329
330         sfinfo.disposition_info.in.delete_on_close = 0;
331         CHECK_CALL_FNUM(DISPOSITION_INFORMATION, NT_STATUS_OK);
332         CHECK_VALUE(ALL_INFO, all_info, delete_pending, 0);
333         CHECK_VALUE(ALL_INFO, all_info, nlink, 1);
334
335         CHECK_CALL_PATH(DISPOSITION_INFORMATION, NT_STATUS_OK);
336         CHECK_VALUE(ALL_INFO, all_info, delete_pending, 0);
337         CHECK_VALUE(ALL_INFO, all_info, nlink, 1);
338
339         printf("test allocation_info level\n");
340         sfinfo.allocation_info.in.alloc_size = 0;
341         CHECK_CALL_FNUM(ALLOCATION_INFO, NT_STATUS_OK);
342         CHECK_VALUE(ALL_INFO, all_info, size, 0);
343         CHECK_VALUE(ALL_INFO, all_info, alloc_size, 0);
344
345         sfinfo.allocation_info.in.alloc_size = 4096;
346         CHECK_CALL_FNUM(ALLOCATION_INFO, NT_STATUS_OK);
347         CHECK_VALUE(ALL_INFO, all_info, alloc_size, 4096);
348         CHECK_VALUE(ALL_INFO, all_info, size, 0);
349
350         RECREATE_BOTH;
351         sfinfo.allocation_info.in.alloc_size = 0;
352         CHECK_CALL_FNUM(ALLOCATION_INFORMATION, NT_STATUS_OK);
353         CHECK_VALUE(ALL_INFO, all_info, size, 0);
354         CHECK_VALUE(ALL_INFO, all_info, alloc_size, 0);
355
356         CHECK_CALL_PATH(ALLOCATION_INFORMATION, NT_STATUS_OK);
357         CHECK_VALUE(ALL_INFO, all_info, size, 0);
358         CHECK_VALUE(ALL_INFO, all_info, alloc_size, 0);
359
360         sfinfo.allocation_info.in.alloc_size = 4096;
361         CHECK_CALL_FNUM(ALLOCATION_INFORMATION, NT_STATUS_OK);
362         CHECK_VALUE(ALL_INFO, all_info, alloc_size, 4096);
363         CHECK_VALUE(ALL_INFO, all_info, size, 0);
364
365         /* setting the allocation size up via setpathinfo seems
366            to be broken in w2k3 */
367         CHECK_CALL_PATH(ALLOCATION_INFORMATION, NT_STATUS_OK);
368         CHECK_VALUE(ALL_INFO, all_info, alloc_size, 0);
369         CHECK_VALUE(ALL_INFO, all_info, size, 0);
370
371         printf("test end_of_file_info level\n");
372         sfinfo.end_of_file_info.in.size = 37;
373         CHECK_CALL_FNUM(END_OF_FILE_INFO, NT_STATUS_OK);
374         CHECK_VALUE(ALL_INFO, all_info, size, 37);
375
376         sfinfo.end_of_file_info.in.size = 7;
377         CHECK_CALL_FNUM(END_OF_FILE_INFO, NT_STATUS_OK);
378         CHECK_VALUE(ALL_INFO, all_info, size, 7);
379
380         sfinfo.end_of_file_info.in.size = 37;
381         CHECK_CALL_FNUM(END_OF_FILE_INFORMATION, NT_STATUS_OK);
382         CHECK_VALUE(ALL_INFO, all_info, size, 37);
383
384         CHECK_CALL_PATH(END_OF_FILE_INFORMATION, NT_STATUS_OK);
385         CHECK_VALUE(ALL_INFO, all_info, size, 37);
386
387         sfinfo.end_of_file_info.in.size = 7;
388         CHECK_CALL_FNUM(END_OF_FILE_INFORMATION, NT_STATUS_OK);
389         CHECK_VALUE(ALL_INFO, all_info, size, 7);
390
391         CHECK_CALL_PATH(END_OF_FILE_INFORMATION, NT_STATUS_OK);
392         CHECK_VALUE(ALL_INFO, all_info, size, 7);
393
394         printf("test position_information level\n");
395         sfinfo.position_information.in.position = 123456;
396         CHECK_CALL_FNUM(POSITION_INFORMATION, NT_STATUS_OK);
397         CHECK_VALUE(POSITION_INFORMATION, position_information, position, 123456);
398
399         CHECK_CALL_PATH(POSITION_INFORMATION, NT_STATUS_OK);
400         CHECK_VALUE(POSITION_INFORMATION, position_information, position, 0);
401
402         printf("test mode_information level\n");
403         sfinfo.mode_information.in.mode = 2;
404         CHECK_CALL_FNUM(MODE_INFORMATION, NT_STATUS_OK);
405         CHECK_VALUE(MODE_INFORMATION, mode_information, mode, 2);
406
407         CHECK_CALL_PATH(MODE_INFORMATION, NT_STATUS_OK);
408         CHECK_VALUE(MODE_INFORMATION, mode_information, mode, 0);
409
410         sfinfo.mode_information.in.mode = 1;
411         CHECK_CALL_FNUM(MODE_INFORMATION, NT_STATUS_INVALID_PARAMETER);
412         CHECK_CALL_PATH(MODE_INFORMATION, NT_STATUS_INVALID_PARAMETER);
413
414         sfinfo.mode_information.in.mode = 0;
415         CHECK_CALL_FNUM(MODE_INFORMATION, NT_STATUS_OK);
416         CHECK_VALUE(MODE_INFORMATION, mode_information, mode, 0);
417
418         CHECK_CALL_PATH(MODE_INFORMATION, NT_STATUS_OK);
419         CHECK_VALUE(MODE_INFORMATION, mode_information, mode, 0);
420
421 #if 0
422         printf("test unix_basic level\n");
423         CHECK_CALL_FNUM(UNIX_BASIC, NT_STATUS_OK);
424         CHECK_CALL_PATH(UNIX_BASIC, NT_STATUS_OK);
425
426         printf("test unix_link level\n");
427         CHECK_CALL_FNUM(UNIX_LINK, NT_STATUS_OK);
428         CHECK_CALL_PATH(UNIX_LINK, NT_STATUS_OK);
429 #endif
430
431 done:
432         smb_raw_exit(cli->session);
433         smbcli_close(cli->tree, fnum);
434         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fnum_fname))) {
435                 printf("Failed to delete %s - %s\n", fnum_fname, smbcli_errstr(cli->tree));
436         }
437         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, path_fname))) {
438                 printf("Failed to delete %s - %s\n", path_fname, smbcli_errstr(cli->tree));
439         }
440
441         return ret;
442 }
443
444 /*
445  * basic testing of all RAW_SFILEINFO_RENAME call
446  */
447 bool torture_raw_sfileinfo_rename(struct torture_context *torture,
448                                                                   struct smbcli_state *cli)
449 {
450         BOOL ret = True;
451         int fnum_saved, d_fnum, fnum2, fnum = -1;
452         char *fnum_fname;
453         char *fnum_fname_new;
454         char *path_fname;
455         char *path_fname_new;
456         union smb_fileinfo finfo1, finfo2;
457         union smb_setfileinfo sfinfo;
458         NTSTATUS status, status2;
459         const char *call_name;
460         BOOL check_fnum;
461         int n = time(NULL) % 100;
462         
463         asprintf(&path_fname, BASEDIR "\\fname_test_%d.txt", n);
464         asprintf(&path_fname_new, BASEDIR "\\fname_test_new_%d.txt", n);
465         asprintf(&fnum_fname, BASEDIR "\\fnum_test_%d.txt", n);
466         asprintf(&fnum_fname_new, BASEDIR "\\fnum_test_new_%d.txt", n);
467
468         if (!torture_setup_dir(cli, BASEDIR)) {
469                 return False;
470         }
471
472         RECREATE_BOTH;
473
474         ZERO_STRUCT(sfinfo);
475
476         smbcli_close(cli->tree, create_complex_file(cli, torture, fnum_fname_new));
477         smbcli_close(cli->tree, create_complex_file(cli, torture, path_fname_new));
478
479         sfinfo.rename_information.in.overwrite = 0;
480         sfinfo.rename_information.in.root_fid  = 0;
481         sfinfo.rename_information.in.new_name  = fnum_fname_new+strlen(BASEDIR)+1;
482         CHECK_CALL_FNUM(RENAME_INFORMATION, NT_STATUS_OBJECT_NAME_COLLISION);
483
484         sfinfo.rename_information.in.new_name  = path_fname_new+strlen(BASEDIR)+1;
485         CHECK_CALL_PATH(RENAME_INFORMATION, NT_STATUS_OBJECT_NAME_COLLISION);
486
487         sfinfo.rename_information.in.new_name  = fnum_fname_new;
488         sfinfo.rename_information.in.overwrite = 1;
489         CHECK_CALL_FNUM(RENAME_INFORMATION, NT_STATUS_NOT_SUPPORTED);
490
491         sfinfo.rename_information.in.new_name  = fnum_fname_new+strlen(BASEDIR)+1;
492         sfinfo.rename_information.in.overwrite = 1;
493         CHECK_CALL_FNUM(RENAME_INFORMATION, NT_STATUS_OK);
494         CHECK_STR(NAME_INFO, name_info, fname.s, fnum_fname_new);
495
496         printf("Trying rename with dest file open\n");
497         fnum2 = create_complex_file(cli, torture, fnum_fname);
498         sfinfo.rename_information.in.new_name  = fnum_fname+strlen(BASEDIR)+1;
499         sfinfo.rename_information.in.overwrite = 1;
500         CHECK_CALL_FNUM(RENAME_INFORMATION, NT_STATUS_ACCESS_DENIED);
501         CHECK_STR(NAME_INFO, name_info, fname.s, fnum_fname_new);
502
503         fnum_saved = fnum;
504         fnum = fnum2;
505         sfinfo.disposition_info.in.delete_on_close = 1;
506         CHECK_CALL_FNUM(DISPOSITION_INFO, NT_STATUS_OK);
507         fnum = fnum_saved;
508
509         printf("Trying rename with dest file open and delete_on_close\n");
510         sfinfo.rename_information.in.new_name  = fnum_fname+strlen(BASEDIR)+1;
511         sfinfo.rename_information.in.overwrite = 1;
512         CHECK_CALL_FNUM(RENAME_INFORMATION, NT_STATUS_ACCESS_DENIED);
513
514         smbcli_close(cli->tree, fnum2);
515         CHECK_CALL_FNUM(RENAME_INFORMATION, NT_STATUS_OK);
516         CHECK_STR(NAME_INFO, name_info, fname.s, fnum_fname);
517
518         printf("Trying rename with source file open twice\n");
519         sfinfo.rename_information.in.new_name  = fnum_fname+strlen(BASEDIR)+1;
520         sfinfo.rename_information.in.overwrite = 1;
521         CHECK_CALL_FNUM(RENAME_INFORMATION, NT_STATUS_OK);
522         CHECK_STR(NAME_INFO, name_info, fname.s, fnum_fname);
523
524         fnum2 = create_complex_file(cli, torture, fnum_fname);
525         sfinfo.rename_information.in.new_name  = fnum_fname_new+strlen(BASEDIR)+1;
526         sfinfo.rename_information.in.overwrite = 0;
527         CHECK_CALL_FNUM(RENAME_INFORMATION, NT_STATUS_OK);
528         CHECK_STR(NAME_INFO, name_info, fname.s, fnum_fname_new);
529         smbcli_close(cli->tree, fnum2);
530
531         sfinfo.rename_information.in.new_name  = fnum_fname+strlen(BASEDIR)+1;
532         sfinfo.rename_information.in.overwrite = 0;
533         CHECK_CALL_FNUM(RENAME_INFORMATION, NT_STATUS_OK);
534         CHECK_STR(NAME_INFO, name_info, fname.s, fnum_fname);
535
536         sfinfo.rename_information.in.new_name  = path_fname_new+strlen(BASEDIR)+1;
537         sfinfo.rename_information.in.overwrite = 1;
538         CHECK_CALL_PATH(RENAME_INFORMATION, NT_STATUS_OK);
539         CHECK_STR(NAME_INFO, name_info, fname.s, path_fname_new);
540
541         sfinfo.rename_information.in.new_name  = fnum_fname+strlen(BASEDIR)+1;
542         CHECK_CALL_FNUM(RENAME_INFORMATION, NT_STATUS_OK);
543         CHECK_STR(NAME_INFO, name_info, fname.s, fnum_fname);
544
545         sfinfo.rename_information.in.new_name  = path_fname+strlen(BASEDIR)+1;
546         CHECK_CALL_PATH(RENAME_INFORMATION, NT_STATUS_OK);
547         CHECK_STR(NAME_INFO, name_info, fname.s, path_fname);
548
549         printf("Trying rename with a root fid\n");
550         status = create_directory_handle(cli->tree, BASEDIR, &d_fnum);
551         CHECK_STATUS(status, NT_STATUS_OK);
552         sfinfo.rename_information.in.new_name  = fnum_fname_new+strlen(BASEDIR)+1;
553         sfinfo.rename_information.in.root_fid = d_fnum;
554         CHECK_CALL_FNUM(RENAME_INFORMATION, NT_STATUS_INVALID_PARAMETER);
555         CHECK_STR(NAME_INFO, name_info, fname.s, fnum_fname);
556
557 done:
558         smb_raw_exit(cli->session);
559         smbcli_close(cli->tree, fnum);
560         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fnum_fname))) {
561                 printf("Failed to delete %s - %s\n", fnum_fname, smbcli_errstr(cli->tree));
562         }
563         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, path_fname))) {
564                 printf("Failed to delete %s - %s\n", path_fname, smbcli_errstr(cli->tree));
565         }
566
567         return ret;
568 }
569
570 /* 
571    look for the w2k3 setpathinfo STANDARD bug
572 */
573 bool torture_raw_sfileinfo_bug(struct torture_context *torture,
574                                                            struct smbcli_state *cli)
575 {
576         const char *fname = "\\bug3.txt";
577         union smb_setfileinfo sfinfo;
578         NTSTATUS status;
579         int fnum;
580
581         if (!torture_setting_bool(torture, "dangerous", false))
582                 torture_skip(torture, 
583                         "torture_raw_sfileinfo_bug disabled - enable dangerous tests to use\n");
584
585         fnum = create_complex_file(cli, torture, fname);
586         smbcli_close(cli->tree, fnum);
587
588         sfinfo.generic.level = RAW_SFILEINFO_STANDARD;
589         sfinfo.generic.in.file.path = fname;
590
591         sfinfo.standard.in.create_time = 0;
592         sfinfo.standard.in.access_time = 0;
593         sfinfo.standard.in.write_time  = 0;
594
595         status = smb_raw_setpathinfo(cli->tree, &sfinfo);
596         printf("%s - %s\n", fname, nt_errstr(status));
597
598         printf("now try and delete %s\n", fname);
599
600         return True;
601 }