r5037: got rid of all of the TALLOC_DEPRECATED stuff. My apologies for the
[tprouty/samba.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 static BOOL check_ea(struct smbcli_state *cli, 
38                      const char *fname, const char *eaname, const char *value)
39 {
40         NTSTATUS status = torture_check_ea(cli, fname, eaname, value);
41         return NT_STATUS_IS_OK(status);
42 }
43
44 static BOOL test_eas(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
45 {
46         NTSTATUS status;
47         union smb_setfileinfo setfile;
48         union smb_open io;
49         const char *fname = BASEDIR "\\ea.txt";
50         BOOL ret = True;
51         int fnum = -1;
52
53         printf("TESTING SETFILEINFO EA_SET\n");
54
55         io.generic.level = RAW_OPEN_NTCREATEX;
56         io.ntcreatex.in.root_fid = 0;
57         io.ntcreatex.in.flags = 0;
58         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
59         io.ntcreatex.in.create_options = 0;
60         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
61         io.ntcreatex.in.share_access = 
62                 NTCREATEX_SHARE_ACCESS_READ | 
63                 NTCREATEX_SHARE_ACCESS_WRITE;
64         io.ntcreatex.in.alloc_size = 0;
65         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
66         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
67         io.ntcreatex.in.security_flags = 0;
68         io.ntcreatex.in.fname = fname;
69         status = smb_raw_open(cli->tree, mem_ctx, &io);
70         CHECK_STATUS(status, NT_STATUS_OK);
71         fnum = io.ntcreatex.out.fnum;
72         
73         ret &= check_ea(cli, fname, "EAONE", NULL);
74
75         printf("Adding first two EAs\n");
76         setfile.generic.level = RAW_SFILEINFO_EA_SET;
77         setfile.generic.file.fnum = fnum;
78         setfile.ea_set.in.num_eas = 2;
79         setfile.ea_set.in.eas = talloc_array(mem_ctx, struct ea_struct, 2);
80         setfile.ea_set.in.eas[0].flags = 0;
81         setfile.ea_set.in.eas[0].name.s = "EAONE";
82         setfile.ea_set.in.eas[0].value = data_blob_string_const("VALUE1");
83         setfile.ea_set.in.eas[1].flags = 0;
84         setfile.ea_set.in.eas[1].name.s = "SECONDEA";
85         setfile.ea_set.in.eas[1].value = data_blob_string_const("ValueTwo");
86
87         status = smb_raw_setfileinfo(cli->tree, &setfile);
88         CHECK_STATUS(status, NT_STATUS_OK);
89
90         ret &= check_ea(cli, fname, "EAONE", "VALUE1");
91         ret &= check_ea(cli, fname, "SECONDEA", "ValueTwo");
92
93         printf("Modifying 2nd EA\n");
94         setfile.ea_set.in.num_eas = 1;
95         setfile.ea_set.in.eas[0].name.s = "SECONDEA";
96         setfile.ea_set.in.eas[0].value = data_blob_string_const(" Changed Value");
97         status = smb_raw_setfileinfo(cli->tree, &setfile);
98         CHECK_STATUS(status, NT_STATUS_OK);
99
100         ret &= check_ea(cli, fname, "EAONE", "VALUE1");
101         ret &= check_ea(cli, fname, "SECONDEA", " Changed Value");
102
103         printf("Setting a NULL EA\n");
104         setfile.ea_set.in.eas[0].value = data_blob(NULL, 0);
105         setfile.ea_set.in.eas[0].name.s = "NULLEA";
106         status = smb_raw_setfileinfo(cli->tree, &setfile);
107         CHECK_STATUS(status, NT_STATUS_OK);
108
109         ret &= check_ea(cli, fname, "EAONE", "VALUE1");
110         ret &= check_ea(cli, fname, "SECONDEA", " Changed Value");
111         ret &= check_ea(cli, fname, "NULLEA", NULL);
112
113         printf("Deleting first EA\n");
114         setfile.ea_set.in.eas[0].flags = 0;
115         setfile.ea_set.in.eas[0].name.s = "EAONE";
116         setfile.ea_set.in.eas[0].value = data_blob(NULL, 0);
117         status = smb_raw_setfileinfo(cli->tree, &setfile);
118         CHECK_STATUS(status, NT_STATUS_OK);
119
120         ret &= check_ea(cli, fname, "EAONE", NULL);
121         ret &= check_ea(cli, fname, "SECONDEA", " Changed Value");
122
123         printf("Deleting second EA\n");
124         setfile.ea_set.in.eas[0].flags = 0;
125         setfile.ea_set.in.eas[0].name.s = "SECONDEA";
126         setfile.ea_set.in.eas[0].value = data_blob(NULL, 0);
127         status = smb_raw_setfileinfo(cli->tree, &setfile);
128         CHECK_STATUS(status, NT_STATUS_OK);
129
130         ret &= check_ea(cli, fname, "EAONE", NULL);
131         ret &= check_ea(cli, fname, "SECONDEA", NULL);
132
133 done:
134         smbcli_close(cli->tree, fnum);
135         return ret;
136 }
137
138
139 /*
140   test using NTTRANS CREATE to create a file with an initial EA set
141 */
142 static BOOL test_nttrans_create(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
143 {
144         NTSTATUS status;
145         union smb_open io;
146         const char *fname = BASEDIR "\\ea2.txt";
147         BOOL ret = True;
148         int fnum = -1;
149         struct ea_struct eas[3];
150         struct smb_ea_list ea_list;
151
152         printf("TESTING NTTRANS CREATE WITH EAS\n");
153
154         io.generic.level = RAW_OPEN_NTTRANS_CREATE;
155         io.ntcreatex.in.root_fid = 0;
156         io.ntcreatex.in.flags = 0;
157         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
158         io.ntcreatex.in.create_options = 0;
159         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
160         io.ntcreatex.in.share_access = 
161                 NTCREATEX_SHARE_ACCESS_READ | 
162                 NTCREATEX_SHARE_ACCESS_WRITE;
163         io.ntcreatex.in.alloc_size = 0;
164         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
165         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
166         io.ntcreatex.in.security_flags = 0;
167         io.ntcreatex.in.fname = fname;
168
169         ea_list.num_eas = 3;
170         ea_list.eas = eas;
171
172         eas[0].flags = 0;
173         eas[0].name.s = "1st EA";
174         eas[0].value = data_blob_string_const("Value One");
175
176         eas[1].flags = 0;
177         eas[1].name.s = "2nd EA";
178         eas[1].value = data_blob_string_const("Second Value");
179
180         eas[2].flags = 0;
181         eas[2].name.s = "and 3rd";
182         eas[2].value = data_blob_string_const("final value");
183
184         io.ntcreatex.in.ea_list = &ea_list;
185         io.ntcreatex.in.sec_desc = NULL;
186
187         status = smb_raw_open(cli->tree, mem_ctx, &io);
188         CHECK_STATUS(status, NT_STATUS_OK);
189         fnum = io.ntcreatex.out.fnum;
190         
191         ret &= check_ea(cli, fname, "EAONE", NULL);
192         ret &= check_ea(cli, fname, "1st EA", "Value One");
193         ret &= check_ea(cli, fname, "2nd EA", "Second Value");
194         ret &= check_ea(cli, fname, "and 3rd", "final value");
195
196         smbcli_close(cli->tree, fnum);
197
198         printf("Trying to add EAs on non-create\n");
199         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
200         io.ntcreatex.in.fname = fname;
201
202         ea_list.num_eas = 1;
203         eas[0].flags = 0;
204         eas[0].name.s = "Fourth EA";
205         eas[0].value = data_blob_string_const("Value Four");
206
207         status = smb_raw_open(cli->tree, mem_ctx, &io);
208         CHECK_STATUS(status, NT_STATUS_OK);
209         fnum = io.ntcreatex.out.fnum;
210         
211         ret &= check_ea(cli, fname, "1st EA", "Value One");
212         ret &= check_ea(cli, fname, "2nd EA", "Second Value");
213         ret &= check_ea(cli, fname, "and 3rd", "final value");
214         ret &= check_ea(cli, fname, "Fourth EA", NULL);
215
216 done:
217         smbcli_close(cli->tree, fnum);
218         return ret;
219 }
220
221 /* 
222    basic testing of EA calls
223 */
224 BOOL torture_raw_eas(void)
225 {
226         struct smbcli_state *cli;
227         BOOL ret = True;
228         TALLOC_CTX *mem_ctx;
229
230         if (!torture_open_connection(&cli)) {
231                 return False;
232         }
233
234         mem_ctx = talloc_init("torture_raw_eas");
235
236         if (!torture_setup_dir(cli, BASEDIR)) {
237                 return False;
238         }
239
240         ret &= test_eas(cli, mem_ctx);
241         ret &= test_nttrans_create(cli, mem_ctx);
242
243         smb_raw_exit(cli->session);
244         smbcli_deltree(cli->tree, BASEDIR);
245
246         torture_close_connection(cli);
247         talloc_free(mem_ctx);
248         return ret;
249 }