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