Convert libcli routines to return NTSTATUS instead of BOOL. Again, the
[kai/samba-autobuild/.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("(%d) Incorrect status %s - should be %s\n", \
26                        __LINE__, 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 cli_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 (cli_deltree(cli->tree, BASEDIR) == -1 ||
44             NT_STATUS_IS_ERR(cli_mkdir(cli->tree, BASEDIR))) {
45                 printf("Unable to setup %s - %s\n", BASEDIR, cli_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         cli_close(cli->tree, cli_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         cli_close(cli->tree, cli_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         printf("Trying a directory\n");
77         io.in.pattern = BASEDIR;
78         io.in.attrib = 0;
79         status = smb_raw_unlink(cli->tree, &io);
80         CHECK_STATUS(status, NT_STATUS_FILE_IS_A_DIRECTORY);
81
82         io.in.pattern = BASEDIR;
83         io.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
84         status = smb_raw_unlink(cli->tree, &io);
85         CHECK_STATUS(status, NT_STATUS_FILE_IS_A_DIRECTORY);
86
87         printf("Trying a bad path\n");
88         io.in.pattern = "..";
89         io.in.attrib = 0;
90         status = smb_raw_unlink(cli->tree, &io);
91         CHECK_STATUS(status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
92
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 = BASEDIR "\\..";
99         io.in.attrib = 0;
100         status = smb_raw_unlink(cli->tree, &io);
101         CHECK_STATUS(status, NT_STATUS_FILE_IS_A_DIRECTORY);
102
103         printf("Trying wildcards\n");
104         cli_close(cli->tree, cli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE));
105         io.in.pattern = BASEDIR "\\t*.t";
106         io.in.attrib = 0;
107         status = smb_raw_unlink(cli->tree, &io);
108         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
109
110         io.in.pattern = BASEDIR "\\*";
111         io.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
112         status = smb_raw_unlink(cli->tree, &io);
113         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
114
115         io.in.pattern = BASEDIR "\\*.dat";
116         io.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
117         status = smb_raw_unlink(cli->tree, &io);
118         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
119
120         io.in.pattern = BASEDIR "\\*.tx?";
121         io.in.attrib = 0;
122         status = smb_raw_unlink(cli->tree, &io);
123         CHECK_STATUS(status, NT_STATUS_OK);
124
125         status = smb_raw_unlink(cli->tree, &io);
126         CHECK_STATUS(status, NT_STATUS_NO_SUCH_FILE);
127
128
129 done:
130         smb_raw_exit(cli->session);
131         cli_deltree(cli->tree, BASEDIR);
132         return ret;
133 }
134
135
136 /* 
137    basic testing of unlink calls
138 */
139 BOOL torture_raw_unlink(int dummy)
140 {
141         struct cli_state *cli;
142         BOOL ret = True;
143         TALLOC_CTX *mem_ctx;
144
145         if (!torture_open_connection(&cli)) {
146                 return False;
147         }
148
149         mem_ctx = talloc_init("torture_raw_unlink");
150
151         if (!test_unlink(cli, mem_ctx)) {
152                 ret = False;
153         }
154
155         torture_close_connection(cli);
156         talloc_destroy(mem_ctx);
157         return ret;
158 }