r5035: fixed composite test to use --num-ops command line option
[ira/wip.git] / source / 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 "libcli/raw/libcliraw.h"
25 #include "libcli/composite/composite.h"
26
27 #define BASEDIR "\\composite"
28
29 static void loadfile_complete(struct smbcli_composite *c)
30 {
31         int *count = talloc_get_type(c->async.private, int);
32         (*count)++;
33 }
34
35 /*
36   test a simple savefile/loadfile combination
37 */
38 static BOOL test_loadfile(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
39 {
40         const char *fname = BASEDIR "\\test.txt";
41         NTSTATUS status;
42         struct smb_composite_savefile io1;
43         struct smb_composite_loadfile io2;
44         struct smbcli_composite **c;
45         char *data;
46         size_t len = random() % 100000;
47         const int num_ops = 50;
48         int i;
49         int *count = talloc_zero(mem_ctx, int);
50
51         data = talloc_array(mem_ctx, uint8_t, len);
52
53         generate_random_buffer(data, len);
54
55         io1.in.fname = fname;
56         io1.in.data  = data;
57         io1.in.size  = len;
58
59         printf("testing savefile\n");
60
61         status = smb_composite_savefile(cli->tree, &io1);
62         if (!NT_STATUS_IS_OK(status)) {
63                 printf("savefile failed: %s\n", nt_errstr(status));
64                 return False;
65         }
66
67         io2.in.fname = fname;
68
69         printf("testing parallel loadfile with %d ops\n", num_ops);
70
71         c = talloc_array(mem_ctx, struct smbcli_composite *, num_ops);
72
73         for (i=0;i<num_ops;i++) {
74                 c[i] = smb_composite_loadfile_send(cli->tree, &io2);
75                 c[i]->async.fn = loadfile_complete;
76                 c[i]->async.private = count;
77         }
78
79         printf("waiting for completion\n");
80         while (*count != num_ops) {
81                 event_loop_once(cli->transport->socket->event.ctx);
82                 printf("count=%d\r", *count);
83                 fflush(stdout);
84         }
85         printf("count=%d\n", *count);
86         
87         for (i=0;i<num_ops;i++) {
88                 status = smb_composite_loadfile_recv(c[i], mem_ctx);
89                 if (!NT_STATUS_IS_OK(status)) {
90                         printf("loadfile[%d] failed - %s\n", i, nt_errstr(status));
91                         return False;
92                 }
93
94                 if (io2.out.size != len) {
95                         printf("wrong length in returned data - %d should be %d\n",
96                                io2.out.size, len);
97                         return False;
98                 }
99                 
100                 if (memcmp(io2.out.data, data, len) != 0) {
101                         printf("wrong data in loadfile!\n");
102                         return False;
103                 }
104         }
105
106         talloc_free(data);
107
108         return True;
109 }
110
111 /*
112   test a simple savefile/loadfile combination
113 */
114 static BOOL test_fetchfile(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
115 {
116         const char *fname = BASEDIR "\\test.txt";
117         NTSTATUS status;
118         struct smb_composite_savefile io1;
119         struct smb_composite_fetchfile io2;
120         struct smbcli_composite **c;
121         char *data;
122         int i;
123         size_t len = random() % 10000;
124         extern int torture_numops;
125         struct event_context *event_ctx;
126         int *count = talloc_zero(mem_ctx, int);
127         BOOL ret = True;
128
129         data = talloc_array(mem_ctx, uint8_t, len);
130
131         generate_random_buffer(data, len);
132
133         io1.in.fname = fname;
134         io1.in.data  = data;
135         io1.in.size  = len;
136
137         printf("testing savefile\n");
138
139         status = smb_composite_savefile(cli->tree, &io1);
140         if (!NT_STATUS_IS_OK(status)) {
141                 printf("savefile failed: %s\n", nt_errstr(status));
142                 return False;
143         }
144
145         io2.in.dest_host = lp_parm_string(-1, "torture", "host");
146         io2.in.port = 0;
147         io2.in.called_name = lp_parm_string(-1, "torture", "host");
148         io2.in.calling_name = lp_netbios_name();
149         io2.in.service = lp_parm_string(-1, "torture", "share");
150         io2.in.service_type = "A:";
151         io2.in.user = lp_parm_string(-1, "torture", "username");
152         io2.in.domain = lp_parm_string(-1, "torture", "userdomain");
153         io2.in.password = lp_parm_string(-1, "torture", "password");
154         io2.in.filename = fname;
155
156         printf("testing parallel fetchfile with %d ops\n", torture_numops);
157
158         event_ctx = event_context_init(mem_ctx);
159         c = talloc_array(mem_ctx, struct smbcli_composite *, torture_numops);
160
161         for (i=0; i<torture_numops; i++) {
162                 c[i] = smb_composite_fetchfile_send(&io2, event_ctx);
163                 c[i]->async.fn = loadfile_complete;
164                 c[i]->async.private = count;
165         }
166
167         printf("waiting for completion\n");
168
169         while (*count != torture_numops) {
170                 event_loop_once(event_ctx);
171                 printf("count=%d\r", *count);
172                 fflush(stdout);
173         }
174         printf("count=%d\n", *count);
175
176         for (i=0;i<torture_numops;i++) {
177                 status = smb_composite_fetchfile_recv(c[i], mem_ctx);
178                 if (!NT_STATUS_IS_OK(status)) {
179                         printf("loadfile[%d] failed - %s\n", i,
180                                nt_errstr(status));
181                         ret = False;
182                         continue;
183                 }
184
185                 if (io2.out.size != len) {
186                         printf("wrong length in returned data - %d "
187                                "should be %d\n",
188                                io2.out.size, len);
189                         ret = False;
190                         continue;
191                 }
192                 
193                 if (memcmp(io2.out.data, data, len) != 0) {
194                         printf("wrong data in loadfile!\n");
195                         ret = False;
196                         continue;
197                 }
198         }
199
200         return ret;
201 }
202
203 /* 
204    basic testing of libcli composite calls
205 */
206 BOOL torture_raw_composite(void)
207 {
208         struct smbcli_state *cli;
209         BOOL ret = True;
210         TALLOC_CTX *mem_ctx;
211
212         if (!torture_open_connection(&cli)) {
213                 return False;
214         }
215
216         mem_ctx = talloc_init("torture_raw_composite");
217
218         if (!torture_setup_dir(cli, BASEDIR)) {
219                 return False;
220         }
221
222         ret &= test_fetchfile(cli, mem_ctx);
223         ret &= test_loadfile(cli, mem_ctx);
224
225         smb_raw_exit(cli->session);
226         smbcli_deltree(cli->tree, BASEDIR);
227
228         torture_close_connection(cli);
229         talloc_destroy(mem_ctx);
230         return ret;
231 }