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