r5037: got rid of all of the TALLOC_DEPRECATED stuff. My apologies for the
[amitay/samba.git] / source4 / torture / raw / unlink.c
1 /* 
2    Unix SMB/CIFS implementation.
3    unlink 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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 #define CHECK_STATUS(status, correct) do { \
24         if (!NT_STATUS_EQUAL(status, correct)) { \
25                 printf("(%s) Incorrect status %s - should be %s\n", \
26                        __location__, nt_errstr(status), nt_errstr(correct)); \
27                 ret = False; \
28                 goto done; \
29         }} while (0)
30
31 #define BASEDIR "\\testunlink"
32
33 /*
34   test unlink ops
35 */
36 static BOOL test_unlink(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
37 {
38         struct smb_unlink io;
39         NTSTATUS status;
40         BOOL ret = True;
41         const char *fname = BASEDIR "\\test.txt";
42
43         if (!torture_setup_dir(cli, BASEDIR)) {
44                 return False;
45         }
46
47         printf("Trying non-existant file\n");
48         io.in.pattern = fname;
49         io.in.attrib = 0;
50         status = smb_raw_unlink(cli->tree, &io);
51         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
52
53         smbcli_close(cli->tree, smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE));
54
55         io.in.pattern = fname;
56         io.in.attrib = 0;
57         status = smb_raw_unlink(cli->tree, &io);
58         CHECK_STATUS(status, NT_STATUS_OK);
59
60         printf("Trying a hidden file\n");
61         smbcli_close(cli->tree, smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE));
62         torture_set_file_attribute(cli->tree, fname, FILE_ATTRIBUTE_HIDDEN);
63
64         io.in.pattern = fname;
65         io.in.attrib = 0;
66         status = smb_raw_unlink(cli->tree, &io);
67         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
68
69         io.in.pattern = fname;
70         io.in.attrib = FILE_ATTRIBUTE_HIDDEN;
71         status = smb_raw_unlink(cli->tree, &io);
72         CHECK_STATUS(status, NT_STATUS_OK);
73
74         io.in.pattern = fname;
75         io.in.attrib = FILE_ATTRIBUTE_HIDDEN;
76         status = smb_raw_unlink(cli->tree, &io);
77         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
78
79         printf("Trying a directory\n");
80         io.in.pattern = BASEDIR;
81         io.in.attrib = 0;
82         status = smb_raw_unlink(cli->tree, &io);
83         CHECK_STATUS(status, NT_STATUS_FILE_IS_A_DIRECTORY);
84
85         io.in.pattern = BASEDIR;
86         io.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
87         status = smb_raw_unlink(cli->tree, &io);
88         CHECK_STATUS(status, NT_STATUS_FILE_IS_A_DIRECTORY);
89
90         printf("Trying a bad path\n");
91         io.in.pattern = "..";
92         io.in.attrib = 0;
93         status = smb_raw_unlink(cli->tree, &io);
94         CHECK_STATUS(status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
95
96         io.in.pattern = "\\..";
97         io.in.attrib = 0;
98         status = smb_raw_unlink(cli->tree, &io);
99         CHECK_STATUS(status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
100
101         io.in.pattern = BASEDIR "\\..\\..";
102         io.in.attrib = 0;
103         status = smb_raw_unlink(cli->tree, &io);
104         CHECK_STATUS(status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
105
106         io.in.pattern = BASEDIR "\\..";
107         io.in.attrib = 0;
108         status = smb_raw_unlink(cli->tree, &io);
109         CHECK_STATUS(status, NT_STATUS_FILE_IS_A_DIRECTORY);
110
111         printf("Trying wildcards\n");
112         smbcli_close(cli->tree, smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE));
113         io.in.pattern = BASEDIR "\\t*.t";
114         io.in.attrib = 0;
115         status = smb_raw_unlink(cli->tree, &io);
116         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
117
118         io.in.pattern = BASEDIR "\\z*";
119         io.in.attrib = 0;
120         status = smb_raw_unlink(cli->tree, &io);
121         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
122
123         io.in.pattern = BASEDIR "\\z*";
124         io.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
125         status = smb_raw_unlink(cli->tree, &io);
126         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
127
128         io.in.pattern = BASEDIR "\\*";
129         io.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
130         status = smb_raw_unlink(cli->tree, &io);
131         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
132
133         io.in.pattern = BASEDIR "\\?";
134         io.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
135         status = smb_raw_unlink(cli->tree, &io);
136         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
137
138         io.in.pattern = BASEDIR "\\t*";
139         io.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
140         status = smb_raw_unlink(cli->tree, &io);
141         CHECK_STATUS(status, NT_STATUS_OK);
142
143         smbcli_close(cli->tree, smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE));
144
145         io.in.pattern = BASEDIR "\\*.dat";
146         io.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
147         status = smb_raw_unlink(cli->tree, &io);
148         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
149
150         io.in.pattern = BASEDIR "\\*.tx?";
151         io.in.attrib = 0;
152         status = smb_raw_unlink(cli->tree, &io);
153         CHECK_STATUS(status, NT_STATUS_OK);
154
155         status = smb_raw_unlink(cli->tree, &io);
156         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
157
158
159 done:
160         smb_raw_exit(cli->session);
161         smbcli_deltree(cli->tree, BASEDIR);
162         return ret;
163 }
164
165
166 /* 
167    basic testing of unlink calls
168 */
169 BOOL torture_raw_unlink(void)
170 {
171         struct smbcli_state *cli;
172         BOOL ret = True;
173         TALLOC_CTX *mem_ctx;
174
175         if (!torture_open_connection(&cli)) {
176                 return False;
177         }
178
179         mem_ctx = talloc_init("torture_raw_unlink");
180
181         if (!test_unlink(cli, mem_ctx)) {
182                 ret = False;
183         }
184
185         torture_close_connection(cli);
186         talloc_free(mem_ctx);
187         return ret;
188 }