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