r14720: Add torture_context argument to all torture tests
[gd/samba-autobuild/.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 #include "torture/util.h"
27
28 /*
29   Test rename on files open with share delete and no share delete.
30  */
31 BOOL torture_test_rename(struct torture_context *torture)
32 {
33         struct smbcli_state *cli1;
34         const char *fname = "\\test.txt";
35         const char *fname1 = "\\test1.txt";
36         BOOL correct = True;
37         int fnum1;
38
39         printf("starting rename test\n");
40         
41         if (!torture_open_connection(&cli1)) {
42                 return False;
43         }
44         
45         smbcli_unlink(cli1->tree, fname);
46         smbcli_unlink(cli1->tree, fname1);
47         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
48                                       SEC_RIGHTS_FILE_READ, 
49                                       FILE_ATTRIBUTE_NORMAL,
50                                       NTCREATEX_SHARE_ACCESS_READ, 
51                                       NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
52
53         if (fnum1 == -1) {
54                 printf("(%s) First open failed - %s\n", 
55                        __location__, smbcli_errstr(cli1->tree));
56                 return False;
57         }
58
59         if (NT_STATUS_IS_ERR(smbcli_rename(cli1->tree, fname, fname1))) {
60                 printf("First rename failed (this is correct) - %s\n", smbcli_errstr(cli1->tree));
61         } else {
62                 printf("(%s) First rename succeeded - this should have failed !\n",
63                        __location__);
64                 correct = False;
65         }
66
67         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
68                 printf("(%s) close - 1 failed (%s)\n", __location__, smbcli_errstr(cli1->tree));
69                 return False;
70         }
71
72         smbcli_unlink(cli1->tree, fname);
73         smbcli_unlink(cli1->tree, fname1);
74         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
75                                       SEC_RIGHTS_FILE_READ, 
76                                       FILE_ATTRIBUTE_NORMAL,
77                                       NTCREATEX_SHARE_ACCESS_DELETE|NTCREATEX_SHARE_ACCESS_READ, 
78                                       NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
79
80         if (fnum1 == -1) {
81                 printf("(%s) Second open failed - %s\n", __location__, smbcli_errstr(cli1->tree));
82                 return False;
83         }
84
85         if (NT_STATUS_IS_ERR(smbcli_rename(cli1->tree, fname, fname1))) {
86                 printf("(%s) Second rename failed - this should have succeeded - %s\n", 
87                        __location__, smbcli_errstr(cli1->tree));
88                 correct = False;
89         } else {
90                 printf("Second rename succeeded\n");
91         }
92
93         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
94                 printf("(%s) close - 2 failed (%s)\n", 
95                        __location__, smbcli_errstr(cli1->tree));
96                 return False;
97         }
98
99         smbcli_unlink(cli1->tree, fname);
100         smbcli_unlink(cli1->tree, fname1);
101
102         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
103                                       SEC_STD_READ_CONTROL, 
104                                       FILE_ATTRIBUTE_NORMAL,
105                                       NTCREATEX_SHARE_ACCESS_NONE, 
106                                       NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
107
108         if (fnum1 == -1) {
109                 printf("(%s) Third open failed - %s\n", __location__, smbcli_errstr(cli1->tree));
110                 return False;
111         }
112
113
114         if (NT_STATUS_IS_ERR(smbcli_rename(cli1->tree, fname, fname1))) {
115                 printf("(%s) Third rename failed - this should have succeeded - %s\n", 
116                        __location__, smbcli_errstr(cli1->tree));
117                 correct = False;
118         } else {
119                 printf("Third rename succeeded\n");
120         }
121
122         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
123                 printf("(%s) close - 3 failed (%s)\n", 
124                        __location__, smbcli_errstr(cli1->tree));
125                 return False;
126         }
127
128         smbcli_unlink(cli1->tree, fname);
129         smbcli_unlink(cli1->tree, fname1);
130
131         if (!torture_close_connection(cli1)) {
132                 correct = False;
133         }
134         
135         return correct;
136 }
137