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