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