r7159: Improve the messages from pidl's validator module.
[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    Copyright (C) Jelmer Vernooij 2005
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "lib/events/events.h"
26 #include "librpc/gen_ndr/ndr_echo.h"
27
28
29 /*
30   test the AddOne interface
31 */
32 static BOOL test_addone(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
33 {
34         int i;
35         NTSTATUS status;
36
37         printf("\nTesting AddOne\n");
38
39         for (i=0;i<10;i++) {
40                 uint32_t n = i;
41                 struct echo_AddOne r;
42                 r.in.in_data = n;
43                 r.out.out_data = &n;
44                 status = dcerpc_echo_AddOne(p, mem_ctx, &r);
45                 if (!NT_STATUS_IS_OK(status)) {
46                         printf("AddOne(%d) failed - %s\n", i, nt_errstr(status));
47                         return False;
48                 }
49                 printf("%d + 1 = %u\n", i, n);
50         }
51
52         return True;
53 }
54
55 /*
56   test the EchoData interface
57 */
58 static BOOL test_echodata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
59 {
60         int i;
61         NTSTATUS status;
62         uint8_t *data_in, *data_out;
63         int len = 1 + (random() % 5000);
64         struct echo_EchoData r;
65
66         printf("\nTesting EchoData\n");
67
68         data_in = talloc_size(mem_ctx, len);
69         data_out = talloc_size(mem_ctx, len);
70         for (i=0;i<len;i++) {
71                 data_in[i] = i;
72         }
73         
74         r.in.len = len;
75         r.in.in_data = data_in;
76
77         status = dcerpc_echo_EchoData(p, mem_ctx, &r);
78         if (!NT_STATUS_IS_OK(status)) {
79                 printf("EchoData(%d) failed - %s\n", len, nt_errstr(status));
80                 return False;
81         }
82
83         data_out = r.out.out_data;
84
85         for (i=0;i<len;i++) {
86                 if (data_in[i] != data_out[i]) {
87                         printf("Bad data returned for len %d at offset %d\n", 
88                                len, i);
89                         printf("in:\n");
90                         dump_data(0, data_in+i, MIN(len-i, 16));
91                         printf("out:\n");
92                         dump_data(0, data_out+i, MIN(len-1, 16));
93                         return False;
94                 }
95         }
96
97
98         return True;
99 }
100
101
102 /*
103   test the SourceData interface
104 */
105 static BOOL test_sourcedata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
106 {
107         int i;
108         NTSTATUS status;
109         int len = 200000 + (random() % 5000);
110         uint8_t *data_out;
111         struct echo_SourceData r;
112
113         printf("\nTesting SourceData\n");
114
115         data_out = talloc_size(mem_ctx, len);
116
117         r.in.len = len;
118         r.out.data = data_out;
119
120         status = dcerpc_echo_SourceData(p, mem_ctx, &r);
121         if (!NT_STATUS_IS_OK(status)) {
122                 printf("SourceData(%d) failed - %s\n", len, nt_errstr(status));
123                 return False;
124         }
125
126         for (i=0;i<len;i++) {
127                 uint8_t *v = (uint8_t *)data_out;
128                 if (v[i] != (i & 0xFF)) {
129                         printf("bad data 0x%x at %d\n", (uint8_t)data_out[i], i);
130                         return False;
131                 }
132         }
133
134         return True;
135 }
136
137 /*
138   test the SinkData interface
139 */
140 static BOOL test_sinkdata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
141 {
142         int i;
143         NTSTATUS status;
144         uint8_t *data_in;
145         int len = 200000 + (random() % 5000);
146         struct echo_SinkData r;
147
148         printf("\nTesting SinkData\n");
149
150         data_in = talloc_size(mem_ctx, len);
151         for (i=0;i<len;i++) {
152                 data_in[i] = i+1;
153         }
154
155         r.in.len = len;
156         r.in.data = data_in;
157
158         status = dcerpc_echo_SinkData(p, mem_ctx, &r);
159         if (!NT_STATUS_IS_OK(status)) {
160                 printf("SinkData(%d) failed - %s\n", len, nt_errstr(status));
161                 return False;
162         }
163
164         printf("sunk %d bytes\n", len);
165
166         return True;
167 }
168
169
170 /*
171   test the testcall interface
172 */
173 static BOOL test_testcall(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
174 {
175         NTSTATUS status;
176         struct echo_TestCall r;
177
178         r.in.s1 = "input string";
179
180         printf("\nTesting TestCall\n");
181         status = dcerpc_echo_TestCall(p, mem_ctx, &r);
182         if (!NT_STATUS_IS_OK(status)) {
183                 printf("TestCall failed - %s\n", nt_errstr(status));
184                 return False;
185         }
186
187         return True;
188 }
189
190 /*
191   test the testcall interface
192 */
193 static BOOL test_testcall2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
194 {
195         NTSTATUS status;
196         struct echo_TestCall2 r;
197         int i;
198         BOOL ret = True;
199
200         for (i=1;i<=7;i++) {
201                 r.in.level = i;
202
203                 printf("\nTesting TestCall2 level %d\n", i);
204                 status = dcerpc_echo_TestCall2(p, mem_ctx, &r);
205                 if (!NT_STATUS_IS_OK(status)) {
206                         printf("TestCall2 failed - %s\n", nt_errstr(status));
207                         ret = False;
208                 }
209         }
210
211         return ret;
212 }
213
214 /*
215   test the TestSleep interface
216 */
217 static BOOL test_sleep(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
218 {
219         int i;
220         NTSTATUS status;
221 #define ASYNC_COUNT 3
222         struct rpc_request *req[ASYNC_COUNT];
223         struct echo_TestSleep r[ASYNC_COUNT];
224         BOOL done[ASYNC_COUNT];
225         struct timeval snd[ASYNC_COUNT];
226         struct timeval rcv[ASYNC_COUNT];
227         struct timeval diff[ASYNC_COUNT];
228         struct event_context *ctx;
229         int total_done = 0;
230         BOOL ret = True;
231
232         printf("\nTesting TestSleep\n");
233
234         for (i=0;i<ASYNC_COUNT;i++) {
235                 done[i]         = False;
236                 snd[i]          = timeval_current();
237                 rcv[i]          = timeval_zero();
238                 r[i].in.seconds = ASYNC_COUNT-i;
239                 req[i] = dcerpc_echo_TestSleep_send(p, mem_ctx, &r[i]);
240                 if (!req[i]) {
241                         printf("Failed to send async sleep request\n");
242                         return False;
243                 }
244         }
245
246         ctx = dcerpc_event_context(p);
247         while (total_done < ASYNC_COUNT) {
248                 if (event_loop_once(ctx) != 0) {
249                         return False;
250                 }
251                 for (i=0;i<ASYNC_COUNT;i++) {
252                         if (done[i] == False && req[i]->state == RPC_REQUEST_DONE) {
253                                 total_done++;
254                                 done[i] = True;
255                                 rcv[i]  = timeval_current();
256                                 diff[i] = timeval_diff(&rcv[i], &snd[i]);
257                                 status  = dcerpc_ndr_request_recv(req[i]);
258                                 if (!NT_STATUS_IS_OK(status)) {
259                                         printf("TestSleep(%d) failed - %s\n",
260                                                i, nt_errstr(status));
261                                         ret = False;
262                                 } else if (r[i].out.result != r[i].in.seconds) {
263                                         printf("Failed - Asked to sleep for %u seconds (server replied with %u seconds and the reply takes only %u seconds)\n", 
264                                                 r[i].out.result, r[i].in.seconds, (uint_t)diff[i].tv_sec);
265                                         ret = False;
266                                 } else {
267                                         if (r[i].out.result > diff[i].tv_sec) {
268                                                 printf("Failed - Slept for %u seconds (but reply takes only %u.%06u seconds)\n", 
269                                                         r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
270                                         } else if (r[i].out.result+1 == diff[i].tv_sec) {
271                                                 printf("Slept for %u seconds (but reply takes %u.%06u seconds - busy server?)\n", 
272                                                         r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
273                                         } else if (r[i].out.result == diff[i].tv_sec) {
274                                                 printf("Slept for %u seconds (reply takes %u.%06u seconds - ok)\n", 
275                                                         r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
276                                         } else {
277                                                 printf("(Failed) - Not async - Slept for %u seconds (but reply takes %u.%06u seconds)\n", 
278                                                         r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
279                                                 /* TODO: let the test fail here, when we support async rpc on ncacn_np
280                                                 ret = False;*/
281                                         }
282                                 }
283                         }
284                 }
285         }
286
287         return ret;
288 }
289
290 /*
291   test enum handling
292 */
293 static BOOL test_enum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
294 {
295         NTSTATUS status;
296         struct echo_TestEnum r;
297         BOOL ret = True;
298         enum echo_Enum1 v = ECHO_ENUM1;
299         struct echo_Enum2 e2;
300         union echo_Enum3 e3;
301
302         r.in.foo1 = &v;
303         r.in.foo2 = &e2;
304         r.in.foo3 = &e3;
305         r.out.foo1 = &v;
306         r.out.foo2 = &e2;
307         r.out.foo3 = &e3;
308
309         e2.e1 = 76;
310         e2.e2 = ECHO_ENUM1_32;
311         e3.e1 = ECHO_ENUM2;
312
313         printf("\nTesting TestEnum\n");
314         status = dcerpc_echo_TestEnum(p, mem_ctx, &r);
315         if (!NT_STATUS_IS_OK(status)) {
316                 printf("TestEnum failed - %s\n", nt_errstr(status));
317                 ret = False;
318         }
319
320         return ret;
321 }
322
323 /*
324   test surrounding conformant array handling
325 */
326 static BOOL test_surrounding(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
327 {
328         NTSTATUS status;
329         struct echo_TestSurrounding r;
330         BOOL ret = True;
331
332         ZERO_STRUCT(r);
333         r.in.data = talloc(mem_ctx, struct echo_Surrounding);
334
335         r.in.data->x = 20;
336         r.in.data->surrounding = talloc_zero_array(mem_ctx, uint16_t, r.in.data->x);
337
338         r.out.data = talloc(mem_ctx, struct echo_Surrounding);
339
340         printf("\nTesting TestSurrounding\n");
341         status = dcerpc_echo_TestSurrounding(p, mem_ctx, &r);
342         if (!NT_STATUS_IS_OK(status)) {
343                 printf("TestSurrounding failed - %s\n", nt_errstr(status));
344                 ret = False;
345         }
346         
347         if (r.out.data->x != 2 * r.in.data->x) {
348                 printf("TestSurrounding did not make the array twice as large\n");
349                 ret = False;
350         }
351
352         return ret;
353 }
354
355 /*
356   test multiple levels of pointers
357 */
358 static BOOL test_doublepointer(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
359 {
360         NTSTATUS status;
361         struct echo_TestDoublePointer r;
362         BOOL ret = True;
363         uint16_t value = 12;
364         uint16_t *pvalue = &value;
365         uint16_t **ppvalue = &pvalue;
366
367         ZERO_STRUCT(r);
368         r.in.data = &ppvalue;
369
370         printf("\nTesting TestDoublePointer\n");
371         status = dcerpc_echo_TestDoublePointer(p, mem_ctx, &r);
372         if (!NT_STATUS_IS_OK(status)) {
373                 printf("TestDoublePointer failed - %s\n", nt_errstr(status));
374                 ret = False;
375         }
376
377         if (value != r.out.result) {
378                 printf("TestDoublePointer did not return original value (%d != %d)\n", value, r.out.result);
379                 ret = False;
380         }
381
382         return ret;
383 }
384
385 BOOL torture_rpc_echo(void)
386 {
387         NTSTATUS status;
388         struct dcerpc_pipe *p;
389         TALLOC_CTX *mem_ctx;
390         BOOL ret = True;
391
392         mem_ctx = talloc_init("torture_rpc_echo");
393
394         status = torture_rpc_connection(mem_ctx, 
395                                         &p, 
396                                         DCERPC_RPCECHO_NAME,
397                                         DCERPC_RPCECHO_UUID,
398                                         DCERPC_RPCECHO_VERSION);
399         if (!NT_STATUS_IS_OK(status)) {
400                 return False;
401         }
402
403         ret &= test_addone(p, mem_ctx);
404         ret &= test_sinkdata(p, mem_ctx);
405         ret &= test_echodata(p, mem_ctx);
406         ret &= test_sourcedata(p, mem_ctx);
407         ret &= test_testcall(p, mem_ctx);
408         ret &= test_testcall2(p, mem_ctx);
409         ret &= test_enum(p, mem_ctx);
410         ret &= test_surrounding(p, mem_ctx);
411         ret &= test_doublepointer(p, mem_ctx);
412         ret &= test_sleep(p, mem_ctx);
413
414         printf("\n");
415         
416         talloc_free(mem_ctx);
417
418         return ret;
419 }