r4037: fixed a bunch of "might be uninitialised" warnings after enabling -O1 in my...
[metze/samba/wip.git] / source4 / torture / raw / eas.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    test DOS extended attributes
5
6    Copyright (C) Andrew Tridgell 2004
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 "libcli/raw/libcliraw.h"
25 #include "librpc/gen_ndr/ndr_security.h"
26
27 #define BASEDIR "\\testeas"
28
29 #define CHECK_STATUS(status, correct) do { \
30         if (!NT_STATUS_EQUAL(status, correct)) { \
31                 printf("(%s) Incorrect status %s - should be %s\n", \
32                        __location__, nt_errstr(status), nt_errstr(correct)); \
33                 ret = False; \
34                 goto done; \
35         }} while (0)
36
37 /*
38   check that an EA has the right value 
39 */
40 static BOOL check_ea(struct smbcli_state *cli, TALLOC_CTX *mem_ctx,
41                      const char *fname, const char *eaname, const char *value)
42 {
43         union smb_fileinfo info;
44         NTSTATUS status;
45         BOOL ret = True;
46         int i;
47
48         info.all_eas.level = RAW_FILEINFO_ALL_EAS;
49         info.all_eas.in.fname = fname;
50
51         status = smb_raw_pathinfo(cli->tree, mem_ctx, &info);
52         CHECK_STATUS(status, NT_STATUS_OK);
53
54         for (i=0;i<info.all_eas.out.num_eas;i++) {
55                 if (StrCaseCmp(eaname, info.all_eas.out.eas[i].name.s) == 0) {
56                         if (value == NULL) {
57                                 printf("attr '%s' should not be present\n", eaname);
58                                 return False;
59                         }
60                         if (strlen(value) == info.all_eas.out.eas[i].value.length &&
61                             memcmp(value, 
62                                    info.all_eas.out.eas[i].value.data,
63                                    info.all_eas.out.eas[i].value.length) == 0) {
64                                 return True;
65                         } else {
66                                 printf("attr '%s' has wrong value '%*.*s'\n", 
67                                        eaname, 
68                                        info.all_eas.out.eas[i].value.length,
69                                        info.all_eas.out.eas[i].value.length,
70                                        info.all_eas.out.eas[i].value.data);
71                                 ret = False;
72                                 goto done;
73                         }
74                 }
75         }
76
77         if (value != NULL) {
78                 printf("attr '%s' not found\n", eaname);
79                 ret = False;
80         }
81
82 done:
83         return ret;
84 }
85
86 static DATA_BLOB data_blob_string_const(const char *str)
87 {
88         DATA_BLOB blob;
89         blob.data = discard_const(str);
90         blob.length = strlen(str);
91         return blob;
92 }
93
94
95 static BOOL test_eas(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
96 {
97         NTSTATUS status;
98         union smb_setfileinfo setfile;
99         union smb_open io;
100         const char *fname = BASEDIR "\\ea.txt";
101         BOOL ret = True;
102         int fnum = -1;
103
104         printf("TESTING SETFILEINFO EA_SET\n");
105
106         io.generic.level = RAW_OPEN_NTCREATEX;
107         io.ntcreatex.in.root_fid = 0;
108         io.ntcreatex.in.flags = 0;
109         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
110         io.ntcreatex.in.create_options = 0;
111         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
112         io.ntcreatex.in.share_access = 
113                 NTCREATEX_SHARE_ACCESS_READ | 
114                 NTCREATEX_SHARE_ACCESS_WRITE;
115         io.ntcreatex.in.alloc_size = 0;
116         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
117         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
118         io.ntcreatex.in.security_flags = 0;
119         io.ntcreatex.in.fname = fname;
120         status = smb_raw_open(cli->tree, mem_ctx, &io);
121         CHECK_STATUS(status, NT_STATUS_OK);
122         fnum = io.ntcreatex.out.fnum;
123         
124         ret &= check_ea(cli, mem_ctx, fname, "EAONE", NULL);
125
126         printf("Adding first EA\n");
127         setfile.generic.level = RAW_SFILEINFO_EA_SET;
128         setfile.generic.file.fnum = fnum;
129         setfile.ea_set.in.ea.flags = 0;
130         setfile.ea_set.in.ea.name.s = "EAONE";
131         setfile.ea_set.in.ea.value = data_blob_string_const("VALUE1");
132
133         status = smb_raw_setfileinfo(cli->tree, &setfile);
134         CHECK_STATUS(status, NT_STATUS_OK);
135
136         ret &= check_ea(cli, mem_ctx, fname, "EAONE", "VALUE1");
137
138         setfile.ea_set.in.ea.name.s = "SECONDEA";
139         setfile.ea_set.in.ea.value = data_blob_string_const("ValueTwo");
140
141         printf("Adding second EA\n");
142         status = smb_raw_setfileinfo(cli->tree, &setfile);
143         CHECK_STATUS(status, NT_STATUS_OK);
144
145         ret &= check_ea(cli, mem_ctx, fname, "EAONE", "VALUE1");
146         ret &= check_ea(cli, mem_ctx, fname, "SECONDEA", "ValueTwo");
147
148         printf("Modifying 2nd EA\n");
149         setfile.ea_set.in.ea.value = data_blob_string_const(" Changed Value");
150         status = smb_raw_setfileinfo(cli->tree, &setfile);
151         CHECK_STATUS(status, NT_STATUS_OK);
152
153         ret &= check_ea(cli, mem_ctx, fname, "EAONE", "VALUE1");
154         ret &= check_ea(cli, mem_ctx, fname, "SECONDEA", " Changed Value");
155
156         printf("Setting a NULL EA\n");
157         setfile.ea_set.in.ea.value = data_blob(NULL, 0);
158         setfile.ea_set.in.ea.name.s = "NULLEA";
159         status = smb_raw_setfileinfo(cli->tree, &setfile);
160         CHECK_STATUS(status, NT_STATUS_OK);
161
162         ret &= check_ea(cli, mem_ctx, fname, "EAONE", "VALUE1");
163         ret &= check_ea(cli, mem_ctx, fname, "SECONDEA", " Changed Value");
164         ret &= check_ea(cli, mem_ctx, fname, "NULLEA", NULL);
165
166         printf("Deleting first EA\n");
167         setfile.ea_set.in.ea.flags = 0;
168         setfile.ea_set.in.ea.name.s = "EAONE";
169         setfile.ea_set.in.ea.value = data_blob(NULL, 0);
170         status = smb_raw_setfileinfo(cli->tree, &setfile);
171         CHECK_STATUS(status, NT_STATUS_OK);
172
173         ret &= check_ea(cli, mem_ctx, fname, "EAONE", NULL);
174         ret &= check_ea(cli, mem_ctx, fname, "SECONDEA", " Changed Value");
175
176         printf("Deleting second EA\n");
177         setfile.ea_set.in.ea.flags = 0;
178         setfile.ea_set.in.ea.name.s = "SECONDEA";
179         setfile.ea_set.in.ea.value = data_blob(NULL, 0);
180         status = smb_raw_setfileinfo(cli->tree, &setfile);
181         CHECK_STATUS(status, NT_STATUS_OK);
182
183         ret &= check_ea(cli, mem_ctx, fname, "EAONE", NULL);
184         ret &= check_ea(cli, mem_ctx, fname, "SECONDEA", NULL);
185
186 done:
187         smbcli_close(cli->tree, fnum);
188         return ret;
189 }
190
191
192 /*
193   test using NTTRANS CREATE to create a file with an initial EA set
194 */
195 static BOOL test_nttrans_create(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
196 {
197         NTSTATUS status;
198         union smb_open io;
199         const char *fname = BASEDIR "\\ea2.txt";
200         BOOL ret = True;
201         int fnum = -1;
202         struct ea_struct eas[3];
203         struct smb_ea_list ea_list;
204
205         printf("TESTING NTTRANS CREATE WITH EAS\n");
206
207         io.generic.level = RAW_OPEN_NTTRANS_CREATE;
208         io.ntcreatex.in.root_fid = 0;
209         io.ntcreatex.in.flags = 0;
210         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
211         io.ntcreatex.in.create_options = 0;
212         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
213         io.ntcreatex.in.share_access = 
214                 NTCREATEX_SHARE_ACCESS_READ | 
215                 NTCREATEX_SHARE_ACCESS_WRITE;
216         io.ntcreatex.in.alloc_size = 0;
217         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
218         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
219         io.ntcreatex.in.security_flags = 0;
220         io.ntcreatex.in.fname = fname;
221
222         ea_list.num_eas = 3;
223         ea_list.eas = eas;
224
225         eas[0].flags = 0;
226         eas[0].name.s = "1st EA";
227         eas[0].value = data_blob_string_const("Value One");
228
229         eas[1].flags = 0;
230         eas[1].name.s = "2nd EA";
231         eas[1].value = data_blob_string_const("Second Value");
232
233         eas[2].flags = 0;
234         eas[2].name.s = "and 3rd";
235         eas[2].value = data_blob_string_const("final value");
236
237         io.ntcreatex.in.ea_list = &ea_list;
238         io.ntcreatex.in.sec_desc = NULL;
239
240         status = smb_raw_open(cli->tree, mem_ctx, &io);
241         CHECK_STATUS(status, NT_STATUS_OK);
242         fnum = io.ntcreatex.out.fnum;
243         
244         ret &= check_ea(cli, mem_ctx, fname, "EAONE", NULL);
245         ret &= check_ea(cli, mem_ctx, fname, "1st EA", "Value One");
246         ret &= check_ea(cli, mem_ctx, fname, "2nd EA", "Second Value");
247         ret &= check_ea(cli, mem_ctx, fname, "and 3rd", "final value");
248
249         smbcli_close(cli->tree, fnum);
250
251         printf("Trying to add EAs on non-create\n");
252         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
253         io.ntcreatex.in.fname = fname;
254
255         ea_list.num_eas = 1;
256         eas[0].flags = 0;
257         eas[0].name.s = "Fourth EA";
258         eas[0].value = data_blob_string_const("Value Four");
259
260         status = smb_raw_open(cli->tree, mem_ctx, &io);
261         CHECK_STATUS(status, NT_STATUS_OK);
262         fnum = io.ntcreatex.out.fnum;
263         
264         ret &= check_ea(cli, mem_ctx, fname, "1st EA", "Value One");
265         ret &= check_ea(cli, mem_ctx, fname, "2nd EA", "Second Value");
266         ret &= check_ea(cli, mem_ctx, fname, "and 3rd", "final value");
267         ret &= check_ea(cli, mem_ctx, fname, "Fourth EA", NULL);
268
269 done:
270         smbcli_close(cli->tree, fnum);
271         return ret;
272 }
273
274 /* 
275    basic testing of EA calls
276 */
277 BOOL torture_raw_eas(void)
278 {
279         struct smbcli_state *cli;
280         BOOL ret = True;
281         TALLOC_CTX *mem_ctx;
282
283         if (!torture_open_connection(&cli)) {
284                 return False;
285         }
286
287         mem_ctx = talloc_init("torture_raw_eas");
288
289         if (!torture_setup_dir(cli, BASEDIR)) {
290                 return False;
291         }
292
293         ret &= test_eas(cli, mem_ctx);
294         ret &= test_nttrans_create(cli, mem_ctx);
295
296         smb_raw_exit(cli->session);
297         smbcli_deltree(cli->tree, BASEDIR);
298
299         torture_close_connection(cli);
300         talloc_destroy(mem_ctx);
301         return ret;
302 }