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