r3324: made the smbtorture code completely warning free
[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 (smbcli_deltree(cli->tree, BASEDIR) == -1 ||
44             NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
45                 printf("Unable to setup %s - %s\n", BASEDIR, smbcli_errstr(cli->tree));
46                 return False;
47         }
48
49         printf("Trying non-existant file\n");
50         io.in.pattern = fname;
51         io.in.attrib = 0;
52         status = smb_raw_unlink(cli->tree, &io);
53         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
54
55         smbcli_close(cli->tree, smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE));
56
57         io.in.pattern = fname;
58         io.in.attrib = 0;
59         status = smb_raw_unlink(cli->tree, &io);
60         CHECK_STATUS(status, NT_STATUS_OK);
61
62         printf("Trying a hidden file\n");
63         smbcli_close(cli->tree, smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE));
64         torture_set_file_attribute(cli->tree, fname, FILE_ATTRIBUTE_HIDDEN);
65
66         io.in.pattern = fname;
67         io.in.attrib = 0;
68         status = smb_raw_unlink(cli->tree, &io);
69         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
70
71         io.in.pattern = fname;
72         io.in.attrib = FILE_ATTRIBUTE_HIDDEN;
73         status = smb_raw_unlink(cli->tree, &io);
74         CHECK_STATUS(status, NT_STATUS_OK);
75
76         io.in.pattern = fname;
77         io.in.attrib = FILE_ATTRIBUTE_HIDDEN;
78         status = smb_raw_unlink(cli->tree, &io);
79         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
80
81         printf("Trying a directory\n");
82         io.in.pattern = BASEDIR;
83         io.in.attrib = 0;
84         status = smb_raw_unlink(cli->tree, &io);
85         CHECK_STATUS(status, NT_STATUS_FILE_IS_A_DIRECTORY);
86
87         io.in.pattern = BASEDIR;
88         io.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
89         status = smb_raw_unlink(cli->tree, &io);
90         CHECK_STATUS(status, NT_STATUS_FILE_IS_A_DIRECTORY);
91
92         printf("Trying a bad path\n");
93         io.in.pattern = "..";
94         io.in.attrib = 0;
95         status = smb_raw_unlink(cli->tree, &io);
96         CHECK_STATUS(status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
97
98         io.in.pattern = "\\..";
99         io.in.attrib = 0;
100         status = smb_raw_unlink(cli->tree, &io);
101         CHECK_STATUS(status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
102
103         io.in.pattern = BASEDIR "\\..\\..";
104         io.in.attrib = 0;
105         status = smb_raw_unlink(cli->tree, &io);
106         CHECK_STATUS(status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
107
108         io.in.pattern = BASEDIR "\\..";
109         io.in.attrib = 0;
110         status = smb_raw_unlink(cli->tree, &io);
111         CHECK_STATUS(status, NT_STATUS_FILE_IS_A_DIRECTORY);
112
113         printf("Trying wildcards\n");
114         smbcli_close(cli->tree, smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE));
115         io.in.pattern = BASEDIR "\\t*.t";
116         io.in.attrib = 0;
117         status = smb_raw_unlink(cli->tree, &io);
118         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
119
120         io.in.pattern = BASEDIR "\\z*";
121         io.in.attrib = 0;
122         status = smb_raw_unlink(cli->tree, &io);
123         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
124
125         io.in.pattern = BASEDIR "\\z*";
126         io.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
127         status = smb_raw_unlink(cli->tree, &io);
128         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
129
130         io.in.pattern = BASEDIR "\\*";
131         io.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
132         status = smb_raw_unlink(cli->tree, &io);
133         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
134
135         io.in.pattern = BASEDIR "\\?";
136         io.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
137         status = smb_raw_unlink(cli->tree, &io);
138         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
139
140         io.in.pattern = BASEDIR "\\t*";
141         io.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
142         status = smb_raw_unlink(cli->tree, &io);
143         CHECK_STATUS(status, NT_STATUS_OK);
144
145         smbcli_close(cli->tree, smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE));
146
147         io.in.pattern = BASEDIR "\\*.dat";
148         io.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
149         status = smb_raw_unlink(cli->tree, &io);
150         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
151
152         io.in.pattern = BASEDIR "\\*.tx?";
153         io.in.attrib = 0;
154         status = smb_raw_unlink(cli->tree, &io);
155         CHECK_STATUS(status, NT_STATUS_OK);
156
157         status = smb_raw_unlink(cli->tree, &io);
158         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
159
160
161 done:
162         smb_raw_exit(cli->session);
163         smbcli_deltree(cli->tree, BASEDIR);
164         return ret;
165 }
166
167
168 /* 
169    basic testing of unlink calls
170 */
171 BOOL torture_raw_unlink(void)
172 {
173         struct smbcli_state *cli;
174         BOOL ret = True;
175         TALLOC_CTX *mem_ctx;
176
177         if (!torture_open_connection(&cli)) {
178                 return False;
179         }
180
181         mem_ctx = talloc_init("torture_raw_unlink");
182
183         if (!test_unlink(cli, mem_ctx)) {
184                 ret = False;
185         }
186
187         torture_close_connection(cli);
188         talloc_destroy(mem_ctx);
189         return ret;
190 }