r24569: Add two tests
[gd/samba-autobuild/.git] / source4 / torture / raw / rename.c
1 /* 
2    Unix SMB/CIFS implementation.
3    rename test suite
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "torture/torture.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "libcli/libcli.h"
24 #include "torture/util.h"
25
26 #define CHECK_STATUS(status, correct) do { \
27         if (!NT_STATUS_EQUAL(status, correct)) { \
28                 printf("(%s) Incorrect status %s - should be %s\n", \
29                        __location__, nt_errstr(status), nt_errstr(correct)); \
30                 ret = False; \
31                 goto done; \
32         }} while (0)
33
34 #define CHECK_VALUE(v, correct) do { \
35         if ((v) != (correct)) { \
36                 printf("(%s) Incorrect %s %d - should be %d\n", \
37                        __location__, #v, (int)v, (int)correct); \
38                 ret = False; \
39         }} while (0)
40
41 #define BASEDIR "\\testrename"
42
43 /*
44   test SMBmv ops
45 */
46 static BOOL test_mv(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
47 {
48         union smb_rename io;
49         NTSTATUS status;
50         BOOL ret = True;
51         int fnum = -1;
52         const char *fname1 = BASEDIR "\\test1.txt";
53         const char *fname2 = BASEDIR "\\test2.txt";
54         const char *Fname1 = BASEDIR "\\Test1.txt";
55         union smb_fileinfo finfo;
56         union smb_open op;
57
58         printf("Testing SMBmv\n");
59
60         if (!torture_setup_dir(cli, BASEDIR)) {
61                 return False;
62         }
63
64         printf("Trying simple rename\n");
65
66         op.generic.level = RAW_OPEN_NTCREATEX;
67         op.ntcreatex.in.root_fid = 0;
68         op.ntcreatex.in.flags = 0;
69         op.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
70         op.ntcreatex.in.create_options = 0;
71         op.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
72         op.ntcreatex.in.share_access = 
73                 NTCREATEX_SHARE_ACCESS_READ | 
74                 NTCREATEX_SHARE_ACCESS_WRITE;
75         op.ntcreatex.in.alloc_size = 0;
76         op.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
77         op.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
78         op.ntcreatex.in.security_flags = 0;
79         op.ntcreatex.in.fname = fname1;
80
81         status = smb_raw_open(cli->tree, mem_ctx, &op);
82         CHECK_STATUS(status, NT_STATUS_OK);
83         fnum = op.ntcreatex.out.file.fnum;
84
85         io.generic.level = RAW_RENAME_RENAME;
86         io.rename.in.pattern1 = fname1;
87         io.rename.in.pattern2 = fname2;
88         io.rename.in.attrib = 0;
89         
90         printf("trying rename while first file open\n");
91         status = smb_raw_rename(cli->tree, &io);
92         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
93
94         smbcli_close(cli->tree, fnum);
95
96         op.ntcreatex.in.access_mask = SEC_FILE_READ_DATA;
97         op.ntcreatex.in.share_access = 
98                 NTCREATEX_SHARE_ACCESS_DELETE | 
99                 NTCREATEX_SHARE_ACCESS_READ |
100                 NTCREATEX_SHARE_ACCESS_WRITE;
101         status = smb_raw_open(cli->tree, mem_ctx, &op);
102         CHECK_STATUS(status, NT_STATUS_OK);
103         fnum = op.ntcreatex.out.file.fnum;
104
105         printf("trying rename while first file open with SHARE_ACCESS_DELETE\n");
106         status = smb_raw_rename(cli->tree, &io);
107         CHECK_STATUS(status, NT_STATUS_OK);
108
109         io.rename.in.pattern1 = fname2;
110         io.rename.in.pattern2 = fname1;
111         status = smb_raw_rename(cli->tree, &io);
112         CHECK_STATUS(status, NT_STATUS_OK);
113
114         printf("Trying case-changing rename\n");
115         io.rename.in.pattern1 = fname1;
116         io.rename.in.pattern2 = Fname1;
117         status = smb_raw_rename(cli->tree, &io);
118         CHECK_STATUS(status, NT_STATUS_OK);
119
120         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
121         finfo.all_info.in.file.path = fname1;
122         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
123         CHECK_STATUS(status, NT_STATUS_OK);
124         if (strcmp(finfo.all_info.out.fname.s, Fname1) != 0) {
125                 printf("(%s) Incorrect filename [%s] after case-changing "
126                        "rename, should be [%s]\n", __location__,
127                        finfo.all_info.out.fname.s, Fname1);
128                 ret = False;
129                 goto done;
130         }
131
132         io.rename.in.pattern1 = fname1;
133         io.rename.in.pattern2 = fname2;
134
135         printf("trying rename while not open\n");
136         smb_raw_exit(cli->session);
137         status = smb_raw_rename(cli->tree, &io);
138         CHECK_STATUS(status, NT_STATUS_OK);
139         
140         printf("Trying self rename\n");
141         io.rename.in.pattern1 = fname2;
142         io.rename.in.pattern2 = fname2;
143         status = smb_raw_rename(cli->tree, &io);
144         CHECK_STATUS(status, NT_STATUS_OK);
145
146         io.rename.in.pattern1 = fname1;
147         io.rename.in.pattern2 = fname1;
148         status = smb_raw_rename(cli->tree, &io);
149         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
150
151
152         printf("trying wildcard rename\n");
153         io.rename.in.pattern1 = BASEDIR "\\*.txt";
154         io.rename.in.pattern2 = fname1;
155         
156         status = smb_raw_rename(cli->tree, &io);
157         CHECK_STATUS(status, NT_STATUS_OK);
158
159         printf("and again\n");
160         status = smb_raw_rename(cli->tree, &io);
161         CHECK_STATUS(status, NT_STATUS_OK);
162
163         printf("Trying extension change\n");
164         io.rename.in.pattern1 = BASEDIR "\\*.txt";
165         io.rename.in.pattern2 = BASEDIR "\\*.bak";
166         status = smb_raw_rename(cli->tree, &io);
167         CHECK_STATUS(status, NT_STATUS_OK);
168
169         status = smb_raw_rename(cli->tree, &io);
170         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
171
172         printf("Checking attrib handling\n");
173         torture_set_file_attribute(cli->tree, BASEDIR "\\test1.bak", FILE_ATTRIBUTE_HIDDEN);
174         io.rename.in.pattern1 = BASEDIR "\\test1.bak";
175         io.rename.in.pattern2 = BASEDIR "\\*.txt";
176         io.rename.in.attrib = 0;
177         status = smb_raw_rename(cli->tree, &io);
178         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
179
180         io.rename.in.attrib = FILE_ATTRIBUTE_HIDDEN;
181         status = smb_raw_rename(cli->tree, &io);
182         CHECK_STATUS(status, NT_STATUS_OK);
183
184 done:
185         smbcli_close(cli->tree, fnum);
186         smb_raw_exit(cli->session);
187         smbcli_deltree(cli->tree, BASEDIR);
188         return ret;
189 }
190
191
192
193 /*
194   test SMBntrename ops
195 */
196 static BOOL test_ntrename(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
197 {
198         union smb_rename io;
199         NTSTATUS status;
200         BOOL ret = True;
201         int fnum, i;
202         const char *fname1 = BASEDIR "\\test1.txt";
203         const char *fname2 = BASEDIR "\\test2.txt";
204         union smb_fileinfo finfo;
205
206         printf("Testing SMBntrename\n");
207
208         if (!torture_setup_dir(cli, BASEDIR)) {
209                 return False;
210         }
211
212         printf("Trying simple rename\n");
213
214         fnum = create_complex_file(cli, mem_ctx, fname1);
215         
216         io.generic.level = RAW_RENAME_NTRENAME;
217         io.ntrename.in.old_name = fname1;
218         io.ntrename.in.new_name = fname2;
219         io.ntrename.in.attrib = 0;
220         io.ntrename.in.cluster_size = 0;
221         io.ntrename.in.flags = RENAME_FLAG_RENAME;
222         
223         status = smb_raw_rename(cli->tree, &io);
224         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
225         
226         smb_raw_exit(cli->session);
227         status = smb_raw_rename(cli->tree, &io);
228         CHECK_STATUS(status, NT_STATUS_OK);
229
230         printf("Trying self rename\n");
231         io.ntrename.in.old_name = fname2;
232         io.ntrename.in.new_name = fname2;
233         status = smb_raw_rename(cli->tree, &io);
234         CHECK_STATUS(status, NT_STATUS_OK);
235
236         io.ntrename.in.old_name = fname1;
237         io.ntrename.in.new_name = fname1;
238         status = smb_raw_rename(cli->tree, &io);
239         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
240
241         printf("trying wildcard rename\n");
242         io.ntrename.in.old_name = BASEDIR "\\*.txt";
243         io.ntrename.in.new_name = fname1;
244         
245         status = smb_raw_rename(cli->tree, &io);
246         CHECK_STATUS(status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
247
248         printf("Checking attrib handling\n");
249         torture_set_file_attribute(cli->tree, fname2, FILE_ATTRIBUTE_HIDDEN);
250         io.ntrename.in.old_name = fname2;
251         io.ntrename.in.new_name = fname1;
252         io.ntrename.in.attrib = 0;
253         status = smb_raw_rename(cli->tree, &io);
254         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
255
256         io.ntrename.in.attrib = FILE_ATTRIBUTE_HIDDEN;
257         status = smb_raw_rename(cli->tree, &io);
258         CHECK_STATUS(status, NT_STATUS_OK);
259
260         torture_set_file_attribute(cli->tree, fname1, FILE_ATTRIBUTE_NORMAL);
261
262         printf("Checking hard link\n");
263         io.ntrename.in.old_name = fname1;
264         io.ntrename.in.new_name = fname2;
265         io.ntrename.in.attrib = 0;
266         io.ntrename.in.flags = RENAME_FLAG_HARD_LINK;
267         status = smb_raw_rename(cli->tree, &io);
268         CHECK_STATUS(status, NT_STATUS_OK);
269
270         torture_set_file_attribute(cli->tree, fname1, FILE_ATTRIBUTE_SYSTEM);
271
272         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
273         finfo.generic.in.file.path = fname2;
274         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
275         CHECK_STATUS(status, NT_STATUS_OK);
276         CHECK_VALUE(finfo.all_info.out.nlink, 2);
277         CHECK_VALUE(finfo.all_info.out.attrib, FILE_ATTRIBUTE_SYSTEM);
278
279         finfo.generic.in.file.path = fname1;
280         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
281         CHECK_STATUS(status, NT_STATUS_OK);
282         CHECK_VALUE(finfo.all_info.out.nlink, 2);
283         CHECK_VALUE(finfo.all_info.out.attrib, FILE_ATTRIBUTE_SYSTEM);
284
285         torture_set_file_attribute(cli->tree, fname1, FILE_ATTRIBUTE_NORMAL);
286
287         smbcli_unlink(cli->tree, fname2);
288
289         finfo.generic.in.file.path = fname1;
290         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
291         CHECK_STATUS(status, NT_STATUS_OK);
292         CHECK_VALUE(finfo.all_info.out.nlink, 1);
293         CHECK_VALUE(finfo.all_info.out.attrib, FILE_ATTRIBUTE_NORMAL);
294
295         printf("Checking copy\n");
296         io.ntrename.in.old_name = fname1;
297         io.ntrename.in.new_name = fname2;
298         io.ntrename.in.attrib = 0;
299         io.ntrename.in.flags = RENAME_FLAG_COPY;
300         status = smb_raw_rename(cli->tree, &io);
301         CHECK_STATUS(status, NT_STATUS_OK);
302
303         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
304         finfo.generic.in.file.path = fname1;
305         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
306         CHECK_STATUS(status, NT_STATUS_OK);
307         CHECK_VALUE(finfo.all_info.out.nlink, 1);
308         CHECK_VALUE(finfo.all_info.out.attrib, FILE_ATTRIBUTE_NORMAL);
309
310         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
311         finfo.generic.in.file.path = fname2;
312         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
313         CHECK_STATUS(status, NT_STATUS_OK);
314         CHECK_VALUE(finfo.all_info.out.nlink, 1);
315         CHECK_VALUE(finfo.all_info.out.attrib, FILE_ATTRIBUTE_NORMAL);
316
317         torture_set_file_attribute(cli->tree, fname1, FILE_ATTRIBUTE_SYSTEM);
318
319         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
320         finfo.generic.in.file.path = fname2;
321         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
322         CHECK_STATUS(status, NT_STATUS_OK);
323         CHECK_VALUE(finfo.all_info.out.nlink, 1);
324         CHECK_VALUE(finfo.all_info.out.attrib, FILE_ATTRIBUTE_NORMAL);
325
326         finfo.generic.in.file.path = fname1;
327         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
328         CHECK_STATUS(status, NT_STATUS_OK);
329         CHECK_VALUE(finfo.all_info.out.nlink, 1);
330         CHECK_VALUE(finfo.all_info.out.attrib, FILE_ATTRIBUTE_SYSTEM);
331
332         torture_set_file_attribute(cli->tree, fname1, FILE_ATTRIBUTE_NORMAL);
333
334         smbcli_unlink(cli->tree, fname2);
335
336         finfo.generic.in.file.path = fname1;
337         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
338         CHECK_STATUS(status, NT_STATUS_OK);
339         CHECK_VALUE(finfo.all_info.out.nlink, 1);
340
341         printf("Checking invalid flags\n");
342         io.ntrename.in.old_name = fname1;
343         io.ntrename.in.new_name = fname2;
344         io.ntrename.in.attrib = 0;
345         io.ntrename.in.flags = 0;
346         status = smb_raw_rename(cli->tree, &io);
347         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
348
349         io.ntrename.in.flags = 300;
350         status = smb_raw_rename(cli->tree, &io);
351         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
352
353         io.ntrename.in.flags = 0x106;
354         status = smb_raw_rename(cli->tree, &io);
355         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
356
357         printf("Checking unknown field\n");
358         io.ntrename.in.old_name = fname1;
359         io.ntrename.in.new_name = fname2;
360         io.ntrename.in.attrib = 0;
361         io.ntrename.in.flags = RENAME_FLAG_RENAME;
362         io.ntrename.in.cluster_size = 0xff;
363         status = smb_raw_rename(cli->tree, &io);
364         CHECK_STATUS(status, NT_STATUS_OK);
365
366         printf("Trying RENAME_FLAG_MOVE_CLUSTER_INFORMATION\n");
367
368         io.ntrename.in.old_name = fname2;
369         io.ntrename.in.new_name = fname1;
370         io.ntrename.in.attrib = 0;
371         io.ntrename.in.flags = RENAME_FLAG_MOVE_CLUSTER_INFORMATION;
372         io.ntrename.in.cluster_size = 1;
373         status = smb_raw_rename(cli->tree, &io);
374         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
375
376         io.ntrename.in.flags = RENAME_FLAG_COPY;
377         status = smb_raw_rename(cli->tree, &io);
378         CHECK_STATUS(status, NT_STATUS_OK);
379
380 #if 0
381         {
382                 char buf[16384];
383                 fnum = smbcli_open(cli->tree, fname1, O_RDWR, DENY_NONE);
384                 memset(buf, 1, sizeof(buf));
385                 smbcli_write(cli->tree, fnum, 0, buf, 0, sizeof(buf));
386                 smbcli_close(cli->tree, fnum);
387
388                 fnum = smbcli_open(cli->tree, fname2, O_RDWR, DENY_NONE);
389                 memset(buf, 1, sizeof(buf));
390                 smbcli_write(cli->tree, fnum, 0, buf, 0, sizeof(buf)-1);
391                 smbcli_close(cli->tree, fnum);
392
393                 torture_all_info(cli->tree, fname1);
394                 torture_all_info(cli->tree, fname2);
395         }
396         
397
398         io.ntrename.in.flags = RENAME_FLAG_MOVE_CLUSTER_INFORMATION;
399         status = smb_raw_rename(cli->tree, &io);
400         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
401
402         for (i=0;i<20000;i++) {
403                 io.ntrename.in.cluster_size = i;
404                 status = smb_raw_rename(cli->tree, &io);
405                 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
406                         printf("i=%d status=%s\n", i, nt_errstr(status));
407                 }
408         }
409 #endif
410
411         printf("Checking other flags\n");
412
413         for (i=0;i<0xFFF;i++) {
414                 if (i == RENAME_FLAG_RENAME ||
415                     i == RENAME_FLAG_HARD_LINK ||
416                     i == RENAME_FLAG_COPY) {
417                         continue;
418                 }
419
420                 io.ntrename.in.old_name = fname2;
421                 io.ntrename.in.new_name = fname1;
422                 io.ntrename.in.flags = i;
423                 io.ntrename.in.attrib = 0;
424                 io.ntrename.in.cluster_size = 0;
425                 status = smb_raw_rename(cli->tree, &io);
426                 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
427                         printf("flags=0x%x status=%s\n", i, nt_errstr(status));
428                 }
429         }
430         
431 done:
432         smb_raw_exit(cli->session);
433         smbcli_deltree(cli->tree, BASEDIR);
434         return ret;
435 }
436
437
438 /* 
439    basic testing of rename calls
440 */
441 BOOL torture_raw_rename(struct torture_context *torture)
442 {
443         struct smbcli_state *cli;
444         BOOL ret = True;
445         TALLOC_CTX *mem_ctx;
446
447         if (!torture_open_connection(&cli, 0)) {
448                 return False;
449         }
450
451         mem_ctx = talloc_init("torture_raw_rename");
452
453         if (!test_mv(cli, mem_ctx)) {
454                 ret = False;
455         }
456
457         if (!test_ntrename(cli, mem_ctx)) {
458                 ret = False;
459         }
460
461         torture_close_connection(cli);
462         talloc_free(mem_ctx);
463         return ret;
464 }