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