r23792: convert Samba4 to GPLv3
[tprouty/samba.git] / source4 / torture / raw / mkdir.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RAW_MKDIR_* and RAW_RMDIR_* individual 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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "torture/torture.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "libcli/libcli.h"
24 #include "torture/util.h"
25
26 #define BASEDIR "\\mkdirtest"
27
28 #define CHECK_STATUS(status, correct) do { \
29         if (!NT_STATUS_EQUAL(status, correct)) { \
30                 printf("(%s) Incorrect status %s - should be %s\n", \
31                        __location__, nt_errstr(status), nt_errstr(correct)); \
32                 ret = False; \
33                 goto done; \
34         }} while (0)
35
36 /*
37   test mkdir ops
38 */
39 static BOOL test_mkdir(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
40 {
41         union smb_mkdir md;
42         struct smb_rmdir rd;
43         const char *path = BASEDIR "\\mkdir.dir";
44         NTSTATUS status;
45         BOOL ret = True;
46
47         if (!torture_setup_dir(cli, BASEDIR)) {
48                 return False;
49         }
50
51         /* 
52            basic mkdir
53         */
54         md.mkdir.level = RAW_MKDIR_MKDIR;
55         md.mkdir.in.path = path;
56
57         status = smb_raw_mkdir(cli->tree, &md);
58         CHECK_STATUS(status, NT_STATUS_OK);
59
60         printf("testing mkdir collision\n");
61
62         /* 2nd create */
63         status = smb_raw_mkdir(cli->tree, &md);
64         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_COLLISION);
65
66         /* basic rmdir */
67         rd.in.path = path;
68         status = smb_raw_rmdir(cli->tree, &rd);
69         CHECK_STATUS(status, NT_STATUS_OK);
70
71         status = smb_raw_rmdir(cli->tree, &rd);
72         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
73
74         printf("testing mkdir collision with file\n");
75
76         /* name collision with a file */
77         smbcli_close(cli->tree, create_complex_file(cli, mem_ctx, path));
78         status = smb_raw_mkdir(cli->tree, &md);
79         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_COLLISION);
80
81         printf("testing rmdir with file\n");
82
83         /* delete a file with rmdir */
84         status = smb_raw_rmdir(cli->tree, &rd);
85         CHECK_STATUS(status, NT_STATUS_NOT_A_DIRECTORY);
86
87         smbcli_unlink(cli->tree, path);
88
89         printf("testing invalid dir\n");
90
91         /* create an invalid dir */
92         md.mkdir.in.path = "..\\..\\..";
93         status = smb_raw_mkdir(cli->tree, &md);
94         CHECK_STATUS(status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
95         
96         printf("testing t2mkdir\n");
97
98         /* try a t2mkdir - need to work out why this fails! */
99         md.t2mkdir.level = RAW_MKDIR_T2MKDIR;
100         md.t2mkdir.in.path = path;
101         md.t2mkdir.in.num_eas = 0;      
102         status = smb_raw_mkdir(cli->tree, &md);
103         CHECK_STATUS(status, NT_STATUS_OK);
104
105         status = smb_raw_rmdir(cli->tree, &rd);
106         CHECK_STATUS(status, NT_STATUS_OK);
107
108         printf("testing t2mkdir bad path\n");
109         md.t2mkdir.in.path = talloc_asprintf(mem_ctx, "%s\\bad_path\\bad_path",
110                                              BASEDIR);
111         status = smb_raw_mkdir(cli->tree, &md);
112         CHECK_STATUS(status, NT_STATUS_OBJECT_PATH_NOT_FOUND);
113
114         printf("testing t2mkdir with EAs\n");
115
116         /* with EAs */
117         md.t2mkdir.level = RAW_MKDIR_T2MKDIR;
118         md.t2mkdir.in.path = path;
119         md.t2mkdir.in.num_eas = 3;
120         md.t2mkdir.in.eas = talloc_array(mem_ctx, struct ea_struct, md.t2mkdir.in.num_eas);
121         md.t2mkdir.in.eas[0].flags = 0;
122         md.t2mkdir.in.eas[0].name.s = "EAONE";
123         md.t2mkdir.in.eas[0].value = data_blob_talloc(mem_ctx, "blah", 4);
124         md.t2mkdir.in.eas[1].flags = 0;
125         md.t2mkdir.in.eas[1].name.s = "EA TWO";
126         md.t2mkdir.in.eas[1].value = data_blob_talloc(mem_ctx, "foo bar", 7);
127         md.t2mkdir.in.eas[2].flags = 0;
128         md.t2mkdir.in.eas[2].name.s = "EATHREE";
129         md.t2mkdir.in.eas[2].value = data_blob_talloc(mem_ctx, "xx1", 3);
130         status = smb_raw_mkdir(cli->tree, &md);
131
132         if (lp_parm_bool(-1, "torture", "samba3", False)
133             && NT_STATUS_EQUAL(status, NT_STATUS_EAS_NOT_SUPPORTED)) {
134                 d_printf("EAS not supported -- not treating as fatal\n");
135         }
136         else {
137                 /*
138                  * In Samba3, don't see this error as fatal
139                  */
140                 CHECK_STATUS(status, NT_STATUS_OK);
141
142                 status = torture_check_ea(cli, path, "EAONE", "blah");
143                 CHECK_STATUS(status, NT_STATUS_OK);
144                 status = torture_check_ea(cli, path, "EA TWO", "foo bar");
145                 CHECK_STATUS(status, NT_STATUS_OK);
146                 status = torture_check_ea(cli, path, "EATHREE", "xx1");
147                 CHECK_STATUS(status, NT_STATUS_OK);
148
149                 status = smb_raw_rmdir(cli->tree, &rd);
150                 CHECK_STATUS(status, NT_STATUS_OK);
151         }
152
153 done:
154         smb_raw_exit(cli->session);
155         smbcli_deltree(cli->tree, BASEDIR);
156         return ret;
157 }
158
159
160 /* 
161    basic testing of all RAW_MKDIR_* calls 
162 */
163 BOOL torture_raw_mkdir(struct torture_context *torture)
164 {
165         struct smbcli_state *cli;
166         BOOL ret = True;
167         TALLOC_CTX *mem_ctx;
168
169         if (!torture_open_connection(&cli, 0)) {
170                 return False;
171         }
172
173         mem_ctx = talloc_init("torture_raw_mkdir");
174
175         if (!test_mkdir(cli, mem_ctx)) {
176                 ret = False;
177         }
178
179         torture_close_connection(cli);
180         talloc_free(mem_ctx);
181         return ret;
182 }