s4-torture: ran minimal_includes.pl over source4/torture
[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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/libcli.h"
24 #include "torture/util.h"
25
26 /*
27   Test rename on files open with share delete and no share delete.
28  */
29 bool torture_test_rename(struct torture_context *tctx, 
30                          struct smbcli_state *cli1)
31 {
32         const char *fname = "\\test.txt";
33         const char *fname1 = "\\test1.txt";
34         int fnum1;
35
36         smbcli_unlink(cli1->tree, fname);
37         smbcli_unlink(cli1->tree, fname1);
38         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
39                                       SEC_RIGHTS_FILE_READ, 
40                                       FILE_ATTRIBUTE_NORMAL,
41                                       NTCREATEX_SHARE_ACCESS_READ, 
42                                       NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
43
44         torture_assert(tctx, fnum1 != -1, talloc_asprintf(tctx, "First open failed - %s", 
45                        smbcli_errstr(cli1->tree)));
46
47         torture_assert(tctx, NT_STATUS_IS_ERR(smbcli_rename(cli1->tree, fname, fname1)), 
48                                         "First rename succeeded - this should have failed !");
49
50         torture_assert_ntstatus_ok(tctx, smbcli_close(cli1->tree, fnum1),
51                 talloc_asprintf(tctx, "close - 1 failed (%s)", smbcli_errstr(cli1->tree)));
52
53         smbcli_unlink(cli1->tree, fname);
54         smbcli_unlink(cli1->tree, fname1);
55         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
56                                       SEC_RIGHTS_FILE_READ, 
57                                       FILE_ATTRIBUTE_NORMAL,
58                                       NTCREATEX_SHARE_ACCESS_DELETE|NTCREATEX_SHARE_ACCESS_READ, 
59                                       NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
60
61         torture_assert(tctx, fnum1 != -1, talloc_asprintf(tctx, 
62                                    "Second open failed - %s", smbcli_errstr(cli1->tree)));
63
64         torture_assert_ntstatus_ok(tctx, smbcli_rename(cli1->tree, fname, fname1),
65                 talloc_asprintf(tctx, 
66                                 "Second rename failed - this should have succeeded - %s", 
67                        smbcli_errstr(cli1->tree)));
68
69         torture_assert_ntstatus_ok(tctx, smbcli_close(cli1->tree, fnum1),
70                 talloc_asprintf(tctx, 
71                 "close - 2 failed (%s)", smbcli_errstr(cli1->tree)));
72
73         smbcli_unlink(cli1->tree, fname);
74         smbcli_unlink(cli1->tree, fname1);
75
76         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
77                                       SEC_STD_READ_CONTROL, 
78                                       FILE_ATTRIBUTE_NORMAL,
79                                       NTCREATEX_SHARE_ACCESS_NONE, 
80                                       NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
81
82         torture_assert(tctx, fnum1 != -1, talloc_asprintf(tctx, "Third open failed - %s", 
83                         smbcli_errstr(cli1->tree)));
84
85         torture_assert_ntstatus_ok(tctx, smbcli_rename(cli1->tree, fname, fname1),
86                 talloc_asprintf(tctx, "Third rename failed - this should have succeeded - %s", 
87                        smbcli_errstr(cli1->tree)));
88
89         torture_assert_ntstatus_ok(tctx, smbcli_close(cli1->tree, fnum1), 
90                 talloc_asprintf(tctx, "close - 3 failed (%s)", smbcli_errstr(cli1->tree)));
91
92         smbcli_unlink(cli1->tree, fname);
93         smbcli_unlink(cli1->tree, fname1);
94
95         return true;
96 }
97