r5452: Add implementation + torture test for echo_Surrounding
[ira/wip.git] / source / 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 "lib/events/events.h"
25 #include "librpc/gen_ndr/ndr_echo.h"
26
27
28 /*
29   test the AddOne interface
30 */
31 static BOOL test_addone(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
32 {
33         int i;
34         NTSTATUS status;
35
36         printf("\nTesting AddOne\n");
37
38         for (i=0;i<10;i++) {
39                 uint32_t n = i;
40                 struct echo_AddOne r;
41                 r.in.v = &n;
42                 r.out.v = &n;
43                 status = dcerpc_echo_AddOne(p, mem_ctx, &r);
44                 if (!NT_STATUS_IS_OK(status)) {
45                         printf("AddOne(%d) failed - %s\n", i, nt_errstr(status));
46                         return False;
47                 }
48                 printf("%d + 1 = %u\n", i, n);
49         }
50
51         return True;
52 }
53
54 /*
55   test the EchoData interface
56 */
57 static BOOL test_echodata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
58 {
59         int i;
60         NTSTATUS status;
61         uint8_t *data_in, *data_out;
62         int len = 1 + (random() % 5000);
63         struct echo_EchoData r;
64
65         printf("\nTesting EchoData\n");
66
67         data_in = talloc_size(mem_ctx, len);
68         data_out = talloc_size(mem_ctx, len);
69         for (i=0;i<len;i++) {
70                 data_in[i] = i;
71         }
72         
73         r.in.len = len;
74         r.in.in_data = data_in;
75
76         status = dcerpc_echo_EchoData(p, mem_ctx, &r);
77         if (!NT_STATUS_IS_OK(status)) {
78                 printf("EchoData(%d) failed - %s\n", len, nt_errstr(status));
79                 return False;
80         }
81
82         data_out = r.out.out_data;
83
84         for (i=0;i<len;i++) {
85                 if (data_in[i] != data_out[i]) {
86                         printf("Bad data returned for len %d at offset %d\n", 
87                                len, i);
88                         printf("in:\n");
89                         dump_data(0, data_in+i, MIN(len-i, 16));
90                         printf("out:\n");
91                         dump_data(0, data_out+i, MIN(len-1, 16));
92                         return False;
93                 }
94         }
95
96
97         return True;
98 }
99
100
101 /*
102   test the SourceData interface
103 */
104 static BOOL test_sourcedata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
105 {
106         int i;
107         NTSTATUS status;
108         int len = 200000 + (random() % 5000);
109         uint8_t *data_out;
110         struct echo_SourceData r;
111
112         printf("\nTesting SourceData\n");
113
114         data_out = talloc_size(mem_ctx, len);
115
116         r.in.len = len;
117         r.out.data = data_out;
118
119         status = dcerpc_echo_SourceData(p, mem_ctx, &r);
120         if (!NT_STATUS_IS_OK(status)) {
121                 printf("SourceData(%d) failed - %s\n", len, nt_errstr(status));
122                 return False;
123         }
124
125         for (i=0;i<len;i++) {
126                 uint8_t *v = (uint8_t *)data_out;
127                 if (v[i] != (i & 0xFF)) {
128                         printf("bad data 0x%x at %d\n", (uint8_t)data_out[i], i);
129                         return False;
130                 }
131         }
132
133         return True;
134 }
135
136 /*
137   test the SinkData interface
138 */
139 static BOOL test_sinkdata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
140 {
141         int i;
142         NTSTATUS status;
143         uint8_t *data_in;
144         int len = 200000 + (random() % 5000);
145         struct echo_SinkData r;
146
147         printf("\nTesting SinkData\n");
148
149         data_in = talloc_size(mem_ctx, len);
150         for (i=0;i<len;i++) {
151                 data_in[i] = i+1;
152         }
153
154         r.in.len = len;
155         r.in.data = data_in;
156
157         status = dcerpc_echo_SinkData(p, mem_ctx, &r);
158         if (!NT_STATUS_IS_OK(status)) {
159                 printf("SinkData(%d) failed - %s\n", len, nt_errstr(status));
160                 return False;
161         }
162
163         printf("sunk %d bytes\n", len);
164
165         return True;
166 }
167
168
169 /*
170   test the testcall interface
171 */
172 static BOOL test_testcall(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
173 {
174         NTSTATUS status;
175         struct echo_TestCall r;
176
177         r.in.s1 = "input string";
178
179         printf("\nTesting TestCall\n");
180         status = dcerpc_echo_TestCall(p, mem_ctx, &r);
181         if (!NT_STATUS_IS_OK(status)) {
182                 printf("TestCall failed - %s\n", nt_errstr(status));
183                 return False;
184         }
185
186         return True;
187 }
188
189 /*
190   test the testcall interface
191 */
192 static BOOL test_testcall2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
193 {
194         NTSTATUS status;
195         struct echo_TestCall2 r;
196         int i;
197         BOOL ret = True;
198
199         for (i=1;i<=7;i++) {
200                 r.in.level = i;
201
202                 printf("\nTesting TestCall2 level %d\n", i);
203                 status = dcerpc_echo_TestCall2(p, mem_ctx, &r);
204                 if (!NT_STATUS_IS_OK(status)) {
205                         printf("TestCall2 failed - %s\n", nt_errstr(status));
206                         ret = False;
207                 }
208         }
209
210         return ret;
211 }
212
213 /*
214   test the TestSleep interface
215 */
216 static BOOL test_sleep(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
217 {
218         int i;
219         NTSTATUS status;
220 #define ASYNC_COUNT 3
221         struct rpc_request *req[ASYNC_COUNT];
222         struct echo_TestSleep r[ASYNC_COUNT];
223         BOOL done[ASYNC_COUNT];
224         struct timeval snd[ASYNC_COUNT];
225         struct timeval rcv[ASYNC_COUNT];
226         struct timeval diff[ASYNC_COUNT];
227         struct event_context *ctx;
228         int total_done = 0;
229         BOOL ret = True;
230
231         printf("\nTesting TestSleep\n");
232
233         for (i=0;i<ASYNC_COUNT;i++) {
234                 done[i]         = False;
235                 snd[i]          = timeval_current();
236                 rcv[i]          = timeval_zero();
237                 r[i].in.seconds = ASYNC_COUNT-i;
238                 req[i] = dcerpc_echo_TestSleep_send(p, mem_ctx, &r[i]);
239                 if (!req[i]) {
240                         printf("Failed to send async sleep request\n");
241                         return False;
242                 }
243         }
244
245         ctx = dcerpc_event_context(p);
246         while (total_done < ASYNC_COUNT) {
247                 if (event_loop_once(ctx) != 0) {
248                         return False;
249                 }
250                 for (i=0;i<ASYNC_COUNT;i++) {
251                         if (done[i] == False && req[i]->state == RPC_REQUEST_DONE) {
252                                 total_done++;
253                                 done[i] = True;
254                                 rcv[i]  = timeval_current();
255                                 diff[i] = timeval_diff(&rcv[i], &snd[i]);
256                                 status  = dcerpc_ndr_request_recv(req[i]);
257                                 if (!NT_STATUS_IS_OK(status)) {
258                                         printf("TestSleep(%d) failed - %s\n",
259                                                i, nt_errstr(status));
260                                         ret = False;
261                                 } else if (r[i].out.result != r[i].in.seconds) {
262                                         printf("Failed - Sleeped for %u seconds (but we said %u seconds and the reply takes only %u seconds)\n", 
263                                                 r[i].out.result, r[i].in.seconds, (uint_t)diff[i].tv_sec);
264                                         ret = False;
265                                 } else {
266                                         if (r[i].out.result > diff[i].tv_sec) {
267                                                 printf("Failed - Sleeped for %u seconds (but reply takes only %u.%06u seconds)\n", 
268                                                         r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
269                                         } else if (r[i].out.result+1 == diff[i].tv_sec) {
270                                                 printf("Sleeped for %u seconds (but reply takes %u.%06u seconds - busy server?)\n", 
271                                                         r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
272                                         } else if (r[i].out.result == diff[i].tv_sec) {
273                                                 printf("Sleeped for %u seconds (reply takes %u.%06u seconds - ok)\n", 
274                                                         r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
275                                         } else {
276                                                 printf("(Failed) - Not async - Sleeped for %u seconds (but reply takes %u.%06u seconds)\n", 
277                                                         r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
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 /*
323   test surrounding conformant array handling
324 */
325 static BOOL test_surrounding(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
326 {
327         NTSTATUS status;
328         struct echo_TestSurrounding r;
329         BOOL ret = True;
330
331         ZERO_STRUCT(r);
332         r.in.data = talloc(mem_ctx, struct echo_Surrounding);
333
334         r.in.data->x = 20;
335         r.in.data->surrounding = talloc_zero_array(mem_ctx, uint16_t, r.in.data->x);
336
337         r.out.data = talloc(mem_ctx, struct echo_Surrounding);
338
339         printf("\nTesting TestSurrounding\n");
340         status = dcerpc_echo_TestSurrounding(p, mem_ctx, &r);
341         if (!NT_STATUS_IS_OK(status)) {
342                 printf("TestSurrounding failed - %s\n", nt_errstr(status));
343                 ret = False;
344         }
345         
346         if (r.out.data->x != 2 * r.in.data->x) {
347                 printf("TestSurrounding did not make the array twice as large\n");
348                 ret = False;
349         }
350
351         return ret;
352 }
353
354
355
356 BOOL torture_rpc_echo(void)
357 {
358         NTSTATUS status;
359         struct dcerpc_pipe *p;
360         TALLOC_CTX *mem_ctx;
361         BOOL ret = True;
362
363         mem_ctx = talloc_init("torture_rpc_echo");
364
365         status = torture_rpc_connection(&p, 
366                                         DCERPC_RPCECHO_NAME,
367                                         DCERPC_RPCECHO_UUID,
368                                         DCERPC_RPCECHO_VERSION);
369         if (!NT_STATUS_IS_OK(status)) {
370                 return False;
371         }
372
373         ret &= test_addone(p, mem_ctx);
374         ret &= test_sinkdata(p, mem_ctx);
375         ret &= test_echodata(p, mem_ctx);
376         ret &= test_sourcedata(p, mem_ctx);
377         ret &= test_testcall(p, mem_ctx);
378         ret &= test_testcall2(p, mem_ctx);
379         ret &= test_enum(p, mem_ctx);
380         ret &= test_sleep(p, mem_ctx);
381         ret &= test_surrounding(p, mem_ctx);
382
383         printf("\n");
384         
385         talloc_free(mem_ctx);
386
387         torture_rpc_close(p);
388         return ret;
389 }