r4690: - add support for async rpc server replies
[ira/wip.git] / source4 / torture / rpc / echo.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for echo rpc operations
4
5    Copyright (C) Andrew Tridgell 2003
6    Copyright (C) Stefan (metze) Metzmacher 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 "librpc/gen_ndr/ndr_echo.h"
25
26
27 /*
28   test the AddOne interface
29 */
30 static BOOL test_addone(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
31 {
32         int i;
33         NTSTATUS status;
34
35         printf("\nTesting AddOne\n");
36
37         for (i=0;i<10;i++) {
38                 uint32_t n = i;
39                 struct echo_AddOne r;
40                 r.in.v = &n;
41                 r.out.v = &n;
42                 status = dcerpc_echo_AddOne(p, mem_ctx, &r);
43                 if (!NT_STATUS_IS_OK(status)) {
44                         printf("AddOne(%d) failed - %s\n", i, nt_errstr(status));
45                         return False;
46                 }
47                 printf("%d + 1 = %u\n", i, n);
48         }
49
50         return True;
51 }
52
53 /*
54   test the EchoData interface
55 */
56 static BOOL test_echodata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
57 {
58         int i;
59         NTSTATUS status;
60         uint8_t *data_in, *data_out;
61         int len = 1 + (random() % 5000);
62         struct echo_EchoData r;
63
64         printf("\nTesting EchoData\n");
65
66         data_in = talloc_size(mem_ctx, len);
67         data_out = talloc_size(mem_ctx, len);
68         for (i=0;i<len;i++) {
69                 data_in[i] = i;
70         }
71         
72         r.in.len = len;
73         r.in.in_data = data_in;
74
75         status = dcerpc_echo_EchoData(p, mem_ctx, &r);
76         if (!NT_STATUS_IS_OK(status)) {
77                 printf("EchoData(%d) failed - %s\n", len, nt_errstr(status));
78                 return False;
79         }
80
81         data_out = r.out.out_data;
82
83         for (i=0;i<len;i++) {
84                 if (data_in[i] != data_out[i]) {
85                         printf("Bad data returned for len %d at offset %d\n", 
86                                len, i);
87                         printf("in:\n");
88                         dump_data(0, data_in+i, MIN(len-i, 16));
89                         printf("out:\n");
90                         dump_data(0, data_out+i, MIN(len-1, 16));
91                         return False;
92                 }
93         }
94
95
96         return True;
97 }
98
99
100 /*
101   test the SourceData interface
102 */
103 static BOOL test_sourcedata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
104 {
105         int i;
106         NTSTATUS status;
107         int len = 200000 + (random() % 5000);
108         uint8_t *data_out;
109         struct echo_SourceData r;
110
111         printf("\nTesting SourceData\n");
112
113         data_out = talloc_size(mem_ctx, len);
114
115         r.in.len = len;
116         r.out.data = data_out;
117
118         status = dcerpc_echo_SourceData(p, mem_ctx, &r);
119         if (!NT_STATUS_IS_OK(status)) {
120                 printf("SourceData(%d) failed - %s\n", len, nt_errstr(status));
121                 return False;
122         }
123
124         for (i=0;i<len;i++) {
125                 uint8_t *v = (uint8_t *)data_out;
126                 if (v[i] != (i & 0xFF)) {
127                         printf("bad data 0x%x at %d\n", (uint8_t)data_out[i], i);
128                         return False;
129                 }
130         }
131
132         return True;
133 }
134
135 /*
136   test the SinkData interface
137 */
138 static BOOL test_sinkdata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
139 {
140         int i;
141         NTSTATUS status;
142         uint8_t *data_in;
143         int len = 200000 + (random() % 5000);
144         struct echo_SinkData r;
145
146         printf("\nTesting SinkData\n");
147
148         data_in = talloc_size(mem_ctx, len);
149         for (i=0;i<len;i++) {
150                 data_in[i] = i+1;
151         }
152
153         r.in.len = len;
154         r.in.data = data_in;
155
156         status = dcerpc_echo_SinkData(p, mem_ctx, &r);
157         if (!NT_STATUS_IS_OK(status)) {
158                 printf("SinkData(%d) failed - %s\n", len, nt_errstr(status));
159                 return False;
160         }
161
162         printf("sunk %d bytes\n", len);
163
164         return True;
165 }
166
167
168 /*
169   test the testcall interface
170 */
171 static BOOL test_testcall(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
172 {
173         NTSTATUS status;
174         struct echo_TestCall r;
175
176         r.in.s1 = "input string";
177
178         printf("\nTesting TestCall\n");
179         status = dcerpc_echo_TestCall(p, mem_ctx, &r);
180         if (!NT_STATUS_IS_OK(status)) {
181                 printf("TestCall failed - %s\n", nt_errstr(status));
182                 return False;
183         }
184
185         return True;
186 }
187
188 /*
189   test the testcall interface
190 */
191 static BOOL test_testcall2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
192 {
193         NTSTATUS status;
194         struct echo_TestCall2 r;
195         int i;
196         BOOL ret = True;
197
198         for (i=1;i<=7;i++) {
199                 r.in.level = i;
200
201                 printf("\nTesting TestCall2 level %d\n", i);
202                 status = dcerpc_echo_TestCall2(p, mem_ctx, &r);
203                 if (!NT_STATUS_IS_OK(status)) {
204                         printf("TestCall2 failed - %s\n", nt_errstr(status));
205                         ret = False;
206                 }
207         }
208
209         return ret;
210 }
211
212 /*
213   test the TestSleep interface
214 */
215 static BOOL test_sleep(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
216 {
217         int i;
218         NTSTATUS status;
219 #define ASYNC_COUNT 3
220         struct rpc_request *req[ASYNC_COUNT];
221         struct echo_TestSleep r[ASYNC_COUNT];
222         BOOL done[ASYNC_COUNT];
223         struct timeval snd[ASYNC_COUNT];
224         struct timeval rcv[ASYNC_COUNT];
225         struct timeval diff[ASYNC_COUNT];
226         struct event_context *ctx;
227         int total_done = 0;
228         BOOL ret = True;
229
230         printf("\nTesting TestSleep\n");
231
232         for (i=0;i<ASYNC_COUNT;i++) {
233                 done[i]         = False;
234                 snd[i]          = timeval_current();
235                 rcv[i]          = timeval_zero();
236                 r[i].in.seconds = ASYNC_COUNT-i;
237                 req[i] = dcerpc_echo_TestSleep_send(p, mem_ctx, &r[i]);
238                 if (!req[i]) {
239                         printf("Failed to send async sleep request\n");
240                         return False;
241                 }
242         }
243
244         ctx = dcerpc_event_context(p);
245         while (total_done < ASYNC_COUNT) {
246                 if (event_loop_once(ctx) != 0) {
247                         return False;
248                 }
249                 for (i=0;i<ASYNC_COUNT;i++) {
250                         if (done[i] == False && req[i]->state == RPC_REQUEST_DONE) {
251                                 total_done++;
252                                 done[i] = True;
253                                 rcv[i]  = timeval_current();
254                                 diff[i] = timeval_diff(&rcv[i], &snd[i]);
255                                 status  = dcerpc_ndr_request_recv(req[i]);
256                                 if (!NT_STATUS_IS_OK(status)) {
257                                         printf("TestSleep(%d) failed - %s\n",
258                                                i, nt_errstr(status));
259                                         ret = False;
260                                 } else if (r[i].out.result != r[i].in.seconds) {
261                                         printf("Failed - Sleeped for %u seconds (but we said %u seconds and the reply takes only %u seconds)\n", 
262                                                 r[i].out.result, r[i].in.seconds, (uint_t)diff[i].tv_sec);
263                                         ret = False;
264                                 } else {
265                                         if (r[i].out.result > diff[i].tv_sec) {
266                                                 printf("Failed - Sleeped for %u seconds (but reply takes only %u seconds)\n", 
267                                                         r[i].out.result, (uint_t)diff[i].tv_sec);
268                                                 ret = False;
269                                         } else if (r[i].out.result+1 == diff[i].tv_sec) {
270                                                 printf("Sleeped for %u seconds (but reply takes %u seconds - busy server?)\n", 
271                                                         r[i].out.result, (uint_t)diff[i].tv_sec);
272                                         } else if (r[i].out.result == diff[i].tv_sec) {
273                                                 printf("Sleeped for %u seconds (reply takes %u seconds - ok)\n", 
274                                                         r[i].out.result, (uint_t)diff[i].tv_sec);
275                                         } else {
276                                                 printf("(Failed) - Not async - Sleeped for %u seconds (but reply takes %u seconds)\n", 
277                                                         r[i].out.result, (uint_t)diff[i].tv_sec);
278                                                 /* TODO: let the test fail here, when we support async rpc on ncacn_np
279                                                 ret = False;*/
280                                         }
281                                 }
282                         }
283                 }
284         }
285
286         return ret;
287 }
288
289 /*
290   test enum handling
291 */
292 static BOOL test_enum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
293 {
294         NTSTATUS status;
295         struct echo_TestEnum r;
296         BOOL ret = True;
297         enum echo_Enum1 v = ECHO_ENUM1;
298         struct echo_Enum2 e2;
299         union echo_Enum3 e3;
300
301         r.in.foo1 = &v;
302         r.in.foo2 = &e2;
303         r.in.foo3 = &e3;
304         r.out.foo1 = &v;
305         r.out.foo2 = &e2;
306         r.out.foo3 = &e3;
307
308         e2.e1 = 76;
309         e2.e2 = ECHO_ENUM1_32;
310         e3.e1 = ECHO_ENUM2;
311
312         printf("\nTesting TestEnum\n");
313         status = dcerpc_echo_TestEnum(p, mem_ctx, &r);
314         if (!NT_STATUS_IS_OK(status)) {
315                 printf("TestEnum failed - %s\n", nt_errstr(status));
316                 ret = False;
317         }
318
319         return ret;
320 }
321
322 BOOL torture_rpc_echo(void)
323 {
324         NTSTATUS status;
325         struct dcerpc_pipe *p;
326         TALLOC_CTX *mem_ctx;
327         BOOL ret = True;
328
329         mem_ctx = talloc_init("torture_rpc_echo");
330
331         status = torture_rpc_connection(&p, 
332                                         DCERPC_RPCECHO_NAME,
333                                         DCERPC_RPCECHO_UUID,
334                                         DCERPC_RPCECHO_VERSION);
335         if (!NT_STATUS_IS_OK(status)) {
336                 return False;
337         }
338
339         if (!test_addone(p, mem_ctx)) {
340                 ret = False;
341         }
342
343         if (!test_sinkdata(p, mem_ctx)) {
344                 ret = False;
345         }
346
347         if (!test_echodata(p, mem_ctx)) {
348                 ret = False;
349         }
350
351         if (!test_sourcedata(p, mem_ctx)) {
352                 ret = False;
353         }
354
355         if (!test_testcall(p, mem_ctx)) {
356                 ret = False;
357         }
358
359         if (!test_testcall2(p, mem_ctx)) {
360                 ret = False;
361         }
362
363         if (!test_enum(p, mem_ctx)) {
364                 ret = False;
365         }
366
367         if (!test_sleep(p, mem_ctx)) {
368                 ret = False;
369         }
370
371         printf("\n");
372         
373         talloc_free(mem_ctx);
374
375         torture_rpc_close(p);
376         return ret;
377 }