r8520: fixed a pile of warnings from the build farm gcc -Wall output on
[samba.git] / source4 / torture / raw / composite.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    libcli composite function testing
5
6    Copyright (C) Andrew Tridgell 2005
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 "lib/events/events.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "librpc/gen_ndr/ndr_security.h"
29
30 #define BASEDIR "\\composite"
31
32 static void loadfile_complete(struct composite_context *c)
33 {
34         int *count = talloc_get_type(c->async.private, int);
35         (*count)++;
36 }
37
38 /*
39   test a simple savefile/loadfile combination
40 */
41 static BOOL test_loadfile(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
42 {
43         const char *fname = BASEDIR "\\test.txt";
44         NTSTATUS status;
45         struct smb_composite_savefile io1;
46         struct smb_composite_loadfile io2;
47         struct composite_context **c;
48         char *data;
49         size_t len = random() % 100000;
50         const int num_ops = 50;
51         int i;
52         int *count = talloc_zero(mem_ctx, int);
53
54         data = talloc_array(mem_ctx, uint8_t, len);
55
56         generate_random_buffer(data, len);
57
58         io1.in.fname = fname;
59         io1.in.data  = data;
60         io1.in.size  = len;
61
62         printf("testing savefile\n");
63
64         status = smb_composite_savefile(cli->tree, &io1);
65         if (!NT_STATUS_IS_OK(status)) {
66                 printf("savefile failed: %s\n", nt_errstr(status));
67                 return False;
68         }
69
70         io2.in.fname = fname;
71
72         printf("testing parallel loadfile with %d ops\n", num_ops);
73
74         c = talloc_array(mem_ctx, struct composite_context *, num_ops);
75
76         for (i=0;i<num_ops;i++) {
77                 c[i] = smb_composite_loadfile_send(cli->tree, &io2);
78                 c[i]->async.fn = loadfile_complete;
79                 c[i]->async.private = count;
80         }
81
82         printf("waiting for completion\n");
83         while (*count != num_ops) {
84                 event_loop_once(cli->transport->socket->event.ctx);
85                 printf("count=%d\r", *count);
86                 fflush(stdout);
87         }
88         printf("count=%d\n", *count);
89         
90         for (i=0;i<num_ops;i++) {
91                 status = smb_composite_loadfile_recv(c[i], mem_ctx);
92                 if (!NT_STATUS_IS_OK(status)) {
93                         printf("loadfile[%d] failed - %s\n", i, nt_errstr(status));
94                         return False;
95                 }
96
97                 if (io2.out.size != len) {
98                         printf("wrong length in returned data - %d should be %d\n",
99                                io2.out.size, (int)len);
100                         return False;
101                 }
102                 
103                 if (memcmp(io2.out.data, data, len) != 0) {
104                         printf("wrong data in loadfile!\n");
105                         return False;
106                 }
107         }
108
109         talloc_free(data);
110
111         return True;
112 }
113
114 /*
115   test a simple savefile/loadfile combination
116 */
117 static BOOL test_fetchfile(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
118 {
119         const char *fname = BASEDIR "\\test.txt";
120         NTSTATUS status;
121         struct smb_composite_savefile io1;
122         struct smb_composite_fetchfile io2;
123         struct composite_context **c;
124         char *data;
125         int i;
126         size_t len = random() % 10000;
127         extern int torture_numops;
128         struct event_context *event_ctx;
129         int *count = talloc_zero(mem_ctx, int);
130         BOOL ret = True;
131
132         data = talloc_array(mem_ctx, uint8_t, len);
133
134         generate_random_buffer(data, len);
135
136         io1.in.fname = fname;
137         io1.in.data  = data;
138         io1.in.size  = len;
139
140         printf("testing savefile\n");
141
142         status = smb_composite_savefile(cli->tree, &io1);
143         if (!NT_STATUS_IS_OK(status)) {
144                 printf("savefile failed: %s\n", nt_errstr(status));
145                 return False;
146         }
147
148         io2.in.dest_host = lp_parm_string(-1, "torture", "host");
149         io2.in.port = 0;
150         io2.in.called_name = lp_parm_string(-1, "torture", "host");
151         io2.in.service = lp_parm_string(-1, "torture", "share");
152         io2.in.service_type = "A:";
153
154         io2.in.credentials = cmdline_credentials;
155         io2.in.workgroup  = lp_workgroup();
156         io2.in.filename = fname;
157
158         printf("testing parallel fetchfile with %d ops\n", torture_numops);
159
160         event_ctx = event_context_init(mem_ctx);
161         c = talloc_array(mem_ctx, struct composite_context *, torture_numops);
162
163         for (i=0; i<torture_numops; i++) {
164                 c[i] = smb_composite_fetchfile_send(&io2, event_ctx);
165                 c[i]->async.fn = loadfile_complete;
166                 c[i]->async.private = count;
167         }
168
169         printf("waiting for completion\n");
170
171         while (*count != torture_numops) {
172                 event_loop_once(event_ctx);
173                 printf("count=%d\r", *count);
174                 fflush(stdout);
175         }
176         printf("count=%d\n", *count);
177
178         for (i=0;i<torture_numops;i++) {
179                 status = smb_composite_fetchfile_recv(c[i], mem_ctx);
180                 if (!NT_STATUS_IS_OK(status)) {
181                         printf("loadfile[%d] failed - %s\n", i,
182                                nt_errstr(status));
183                         ret = False;
184                         continue;
185                 }
186
187                 if (io2.out.size != len) {
188                         printf("wrong length in returned data - %d "
189                                "should be %d\n",
190                                io2.out.size, (int)len);
191                         ret = False;
192                         continue;
193                 }
194                 
195                 if (memcmp(io2.out.data, data, len) != 0) {
196                         printf("wrong data in loadfile!\n");
197                         ret = False;
198                         continue;
199                 }
200         }
201
202         return ret;
203 }
204
205 /*
206   test setfileacl
207 */
208 static BOOL test_appendacl(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
209 {
210         struct smb_composite_appendacl **io;
211         struct smb_composite_appendacl **io_orig;
212         struct composite_context **c;
213         struct event_context *event_ctx;
214
215         struct security_descriptor *test_sd;
216         struct security_ace *ace;
217         struct dom_sid *test_sid;
218
219         const int num_ops = 50;
220         int *count = talloc_zero(mem_ctx, int);
221         struct smb_composite_savefile io1;
222
223         NTSTATUS status;
224         int i;
225
226         io_orig = talloc_array(mem_ctx, struct smb_composite_appendacl *, num_ops);
227
228         printf ("creating %d empty files and getting their acls with appendacl\n", num_ops);
229
230         for (i = 0; i < num_ops; i++) {
231                 io1.in.fname = talloc_asprintf(io_orig, BASEDIR "\\test%d.txt", i);
232                 io1.in.data  = NULL;
233                 io1.in.size  = 0;
234           
235                 status = smb_composite_savefile(cli->tree, &io1);
236                 if (!NT_STATUS_IS_OK(status)) {
237                         printf("savefile failed: %s\n", nt_errstr(status));
238                         return False;
239                 }
240
241                 io_orig[i] = talloc (io_orig, struct smb_composite_appendacl);
242                 io_orig[i]->in.fname = talloc_steal(io_orig[i], io1.in.fname);
243                 io_orig[i]->in.sd = security_descriptor_initialise(io_orig[i]);
244                 status = smb_composite_appendacl(cli->tree, io_orig[i], io_orig[i]);
245                 if (!NT_STATUS_IS_OK(status)) {
246                         printf("appendacl failed: %s\n", nt_errstr(status));
247                         return False;
248                 }
249         }
250         
251
252         /* fill Security Descriptor with aces to be added */
253
254         test_sd = security_descriptor_initialise(mem_ctx);
255         test_sid = dom_sid_parse_talloc (mem_ctx, "S-1-5-32-1234-5432");
256
257         ace = talloc_zero(mem_ctx, struct security_ace);
258
259         ace->type = SEC_ACE_TYPE_ACCESS_ALLOWED;
260         ace->flags = 0;
261         ace->access_mask = SEC_STD_ALL;
262         ace->trustee = *test_sid;
263
264         status = security_descriptor_dacl_add(test_sd, ace);
265         if (!NT_STATUS_IS_OK(status)) {
266                 printf("appendacl failed: %s\n", nt_errstr(status));
267                 return False;
268         }
269
270         /* set parameters for appendacl async call */
271
272         printf("testing parallel appendacl with %d ops\n", num_ops);
273
274         c = talloc_array(mem_ctx, struct composite_context *, num_ops);
275         io = talloc_array(mem_ctx, struct  smb_composite_appendacl *, num_ops);
276
277         for (i=0; i < num_ops; i++) {
278                 io[i] = talloc (io, struct smb_composite_appendacl);
279                 io[i]->in.sd = test_sd;
280                 io[i]->in.fname = talloc_asprintf(io[i], BASEDIR "\\test%d.txt", i);
281
282                 c[i] = smb_composite_appendacl_send(cli->tree, io[i]);
283                 c[i]->async.fn = loadfile_complete;
284                 c[i]->async.private = count;
285         }
286
287         event_ctx = talloc_reference(mem_ctx, cli->tree->session->transport->socket->event.ctx);
288         printf("waiting for completion\n");
289         while (*count != num_ops) {
290                 event_loop_once(event_ctx);
291                 printf("count=%d\r", *count);
292                 fflush(stdout);
293         }
294         printf("count=%d\n", *count);
295
296         for (i=0; i < num_ops; i++) {
297                 status = smb_composite_appendacl_recv(c[i], io[i]);
298                 if (!NT_STATUS_IS_OK(status)) {
299                         printf("appendacl[%d] failed - %s\n", i, nt_errstr(status));
300                         return False;
301                 }
302                 
303                 security_descriptor_dacl_add(io_orig[i]->out.sd, ace);
304                 if (!security_acl_equal(io_orig[i]->out.sd->dacl, io[i]->out.sd->dacl)) {
305                         printf("appendacl[%d] failed - needed acl isn't set\n", i);
306                         return False;
307                 }
308         }
309         
310
311         talloc_free (ace);
312         talloc_free (test_sid);
313         talloc_free (test_sd);
314                 
315         return True;
316 }
317
318 /* test a query FS info by asking for share's GUID */
319 static BOOL test_fsinfo(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
320 {
321         char *guid = NULL;
322         NTSTATUS status;
323         struct smb_composite_fsinfo io1;
324         struct composite_context **c;
325
326         int i;
327         extern int torture_numops;
328         struct event_context *event_ctx;
329         int *count = talloc_zero(mem_ctx, int);
330         BOOL ret = True;
331
332         io1.in.dest_host = lp_parm_string(-1, "torture", "host");
333         io1.in.port = 0;
334         io1.in.called_name = lp_parm_string(-1, "torture", "host");
335         io1.in.service = lp_parm_string(-1, "torture", "share");
336         io1.in.service_type = "A:";
337         io1.in.credentials = cmdline_credentials;
338         io1.in.workgroup = lp_workgroup();
339         io1.in.level = RAW_QFS_OBJECTID_INFORMATION;
340
341         printf("testing parallel queryfsinfo [Object ID] with %d ops\n", torture_numops);
342
343         event_ctx = talloc_reference(mem_ctx, cli->tree->session->transport->socket->event.ctx);
344         c = talloc_array(mem_ctx, struct composite_context *, torture_numops);
345
346         for (i=0; i<torture_numops; i++) {
347                 c[i] = smb_composite_fsinfo_send(cli->tree,&io1);
348                 c[i]->async.fn = loadfile_complete;
349                 c[i]->async.private = count;
350         }
351
352         printf("waiting for completion\n");
353
354         while (*count < torture_numops) {
355                 event_loop_once(event_ctx);
356                 printf("count=%d\r", *count);
357                 fflush(stdout);
358         }
359         printf("count=%d\n", *count);
360
361         for (i=0;i<torture_numops;i++) {
362                 status = smb_composite_fsinfo_recv(c[i], mem_ctx);
363                 if (!NT_STATUS_IS_OK(status)) {
364                         printf("fsinfo[%d] failed - %s\n", i, nt_errstr(status));
365                         ret = False;
366                         continue;
367                 }
368
369                 if (io1.out.fsinfo->generic.level != RAW_QFS_OBJECTID_INFORMATION) {
370                         printf("wrong level in returned info - %d "
371                                "should be %d\n",
372                                io1.out.fsinfo->generic.level, RAW_QFS_OBJECTID_INFORMATION);
373                         ret = False;
374                         continue;
375                 }
376
377                 guid=GUID_string(mem_ctx, &io1.out.fsinfo->objectid_information.out.guid);
378                 printf("[%d] GUID: %s\n", i, guid);
379
380                 
381         }
382
383         return ret;
384 }
385
386
387 /* 
388    basic testing of libcli composite calls
389 */
390 BOOL torture_raw_composite(void)
391 {
392         struct smbcli_state *cli;
393         BOOL ret = True;
394         TALLOC_CTX *mem_ctx;
395
396         if (!torture_open_connection(&cli)) {
397                 return False;
398         }
399
400         mem_ctx = talloc_init("torture_raw_composite");
401
402         if (!torture_setup_dir(cli, BASEDIR)) {
403                 return False;
404         }
405
406         ret &= test_fetchfile(cli, mem_ctx);
407         ret &= test_loadfile(cli, mem_ctx);
408         ret &= test_appendacl(cli, mem_ctx);
409         ret &= test_fsinfo(cli, mem_ctx);
410
411         smb_raw_exit(cli->session);
412         smbcli_deltree(cli->tree, BASEDIR);
413
414         torture_close_connection(cli);
415         talloc_free(mem_ctx);
416         return ret;
417 }