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