r12694: Move some headers to the directory of the subsystem they belong to.
[samba.git] / source4 / torture / basic / rename.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    rename testing
5
6    Copyright (C) Andrew Tridgell 2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/libcli.h"
25 #include "torture/torture.h"
26
27 /*
28   Test rename on files open with share delete and no share delete.
29  */
30 BOOL torture_test_rename(void)
31 {
32         struct smbcli_state *cli1;
33         const char *fname = "\\test.txt";
34         const char *fname1 = "\\test1.txt";
35         BOOL correct = True;
36         int fnum1;
37
38         printf("starting rename test\n");
39         
40         if (!torture_open_connection(&cli1)) {
41                 return False;
42         }
43         
44         smbcli_unlink(cli1->tree, fname);
45         smbcli_unlink(cli1->tree, fname1);
46         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
47                                       SEC_RIGHTS_FILE_READ, 
48                                       FILE_ATTRIBUTE_NORMAL,
49                                       NTCREATEX_SHARE_ACCESS_READ, 
50                                       NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
51
52         if (fnum1 == -1) {
53                 printf("(%s) First open failed - %s\n", 
54                        __location__, smbcli_errstr(cli1->tree));
55                 return False;
56         }
57
58         if (NT_STATUS_IS_ERR(smbcli_rename(cli1->tree, fname, fname1))) {
59                 printf("First rename failed (this is correct) - %s\n", smbcli_errstr(cli1->tree));
60         } else {
61                 printf("(%s) First rename succeeded - this should have failed !\n",
62                        __location__);
63                 correct = False;
64         }
65
66         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
67                 printf("(%s) close - 1 failed (%s)\n", __location__, smbcli_errstr(cli1->tree));
68                 return False;
69         }
70
71         smbcli_unlink(cli1->tree, fname);
72         smbcli_unlink(cli1->tree, fname1);
73         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
74                                       SEC_RIGHTS_FILE_READ, 
75                                       FILE_ATTRIBUTE_NORMAL,
76                                       NTCREATEX_SHARE_ACCESS_DELETE|NTCREATEX_SHARE_ACCESS_READ, 
77                                       NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
78
79         if (fnum1 == -1) {
80                 printf("(%s) Second open failed - %s\n", __location__, smbcli_errstr(cli1->tree));
81                 return False;
82         }
83
84         if (NT_STATUS_IS_ERR(smbcli_rename(cli1->tree, fname, fname1))) {
85                 printf("(%s) Second rename failed - this should have succeeded - %s\n", 
86                        __location__, smbcli_errstr(cli1->tree));
87                 correct = False;
88         } else {
89                 printf("Second rename succeeded\n");
90         }
91
92         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
93                 printf("(%s) close - 2 failed (%s)\n", 
94                        __location__, smbcli_errstr(cli1->tree));
95                 return False;
96         }
97
98         smbcli_unlink(cli1->tree, fname);
99         smbcli_unlink(cli1->tree, fname1);
100
101         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
102                                       SEC_STD_READ_CONTROL, 
103                                       FILE_ATTRIBUTE_NORMAL,
104                                       NTCREATEX_SHARE_ACCESS_NONE, 
105                                       NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
106
107         if (fnum1 == -1) {
108                 printf("(%s) Third open failed - %s\n", __location__, smbcli_errstr(cli1->tree));
109                 return False;
110         }
111
112
113         if (NT_STATUS_IS_ERR(smbcli_rename(cli1->tree, fname, fname1))) {
114                 printf("(%s) Third rename failed - this should have succeeded - %s\n", 
115                        __location__, smbcli_errstr(cli1->tree));
116                 correct = False;
117         } else {
118                 printf("Third rename succeeded\n");
119         }
120
121         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
122                 printf("(%s) close - 3 failed (%s)\n", 
123                        __location__, smbcli_errstr(cli1->tree));
124                 return False;
125         }
126
127         smbcli_unlink(cli1->tree, fname);
128         smbcli_unlink(cli1->tree, fname1);
129
130         if (!torture_close_connection(cli1)) {
131                 correct = False;
132         }
133         
134         return correct;
135 }
136