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