40a486f93f98df0beb255eedf7a6d2abc279bf38
[jelmer/samba4-debian.git] / source / torture / basic / unlink.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    unlink tester
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 "system/filesys.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "librpc/gen_ndr/ndr_security.h"
27
28 #define BASEDIR "\\unlinktest"
29
30 /*
31   This test checks that 
32
33   1) the server does not allow an unlink on a file that is open
34 */
35 BOOL torture_unlinktest(void)
36 {
37         struct smbcli_state *cli;
38         const char *fname = BASEDIR "\\unlink.tst";
39         int fnum;
40         BOOL correct = True;
41         union smb_open io;
42         NTSTATUS status;
43
44         if (!torture_open_connection(&cli)) {
45                 return False;
46         }
47
48         printf("starting unlink test\n");
49
50         if (!torture_setup_dir(cli, BASEDIR)) {
51                 return False;
52         }
53
54         cli->session->pid = 1;
55
56         printf("Opening a file\n");
57
58         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
59         if (fnum == -1) {
60                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
61                 return False;
62         }
63
64         printf("Unlinking a open file\n");
65
66         if (NT_STATUS_IS_OK(smbcli_unlink(cli->tree, fname))) {
67                 printf("(%s) error: server allowed unlink on an open file\n", __location__);
68                 correct = False;
69         } else {
70                 correct = check_error(__location__, cli, ERRDOS, ERRbadshare, 
71                                       NT_STATUS_SHARING_VIOLATION);
72         }
73
74         smbcli_close(cli->tree, fnum);
75         smbcli_unlink(cli->tree, fname);
76
77         printf("testing unlink after ntcreatex with DELETE access\n");
78
79         io.ntcreatex.level = RAW_OPEN_NTCREATEX;
80         io.ntcreatex.in.root_fid = 0;
81         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
82         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_NON_DIRECTORY_FILE;
83         io.ntcreatex.in.file_attr = 0;
84         io.ntcreatex.in.alloc_size = 0;
85         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
86         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
87         io.ntcreatex.in.security_flags = 0;
88         io.ntcreatex.in.fname = fname;
89         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_DELETE;
90         io.ntcreatex.in.access_mask  = SEC_RIGHTS_FILE_ALL;
91
92         status = smb_raw_open(cli->tree, cli, &io);
93         if (!NT_STATUS_IS_OK(status)) {
94                 printf("(%s) failed to open %s\n", __location__, fname);
95         }
96         if (NT_STATUS_IS_OK(smbcli_unlink(cli->tree, fname))) {
97                 printf("(%s) error: server allowed unlink on an open file\n", __location__);
98                 correct = False;
99         } else {
100                 correct = check_error(__location__, cli, ERRDOS, ERRbadshare, 
101                                       NT_STATUS_SHARING_VIOLATION);
102         }
103
104         if (!torture_close_connection(cli)) {
105                 correct = False;
106         }
107
108         printf("unlink test finished\n");
109         
110         return correct;
111 }
112
113