r18565: Fix echo.idl to be Samba3-, MIDL and midlc compatible
[kai/samba.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 "torture/torture.h"
26 #include "torture/rpc/rpc.h"
27 #include "lib/events/events.h"
28 #include "librpc/gen_ndr/ndr_echo_c.h"
29
30
31 /*
32   test the AddOne interface
33 */
34 #define TEST_ADDONE(value) do { \
35         n = i = value; \
36         r.in.in_data = n; \
37         r.out.out_data = &n; \
38         status = dcerpc_echo_AddOne(p, mem_ctx, &r); \
39         if (!NT_STATUS_IS_OK(status)) { \
40                 printf("AddOne(%d) failed - %s\n", i, nt_errstr(status)); \
41                 return False; \
42         } \
43         if (n != i+1) { \
44                 printf("%d + 1 != %u (should be %u)\n", i, n, i+1); \
45                 ret = False; \
46         } else { \
47                 printf("%d + 1 = %u\n", i, n); \
48         } \
49 } while(0)
50
51 static BOOL test_addone(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
52 {
53         BOOL ret = True;
54         uint32_t i;
55         NTSTATUS status;
56         uint32_t n;
57         struct echo_AddOne r;
58
59         printf("\nTesting AddOne\n");
60
61         for (i=0;i<10;i++) {
62                 TEST_ADDONE(i);
63         }
64
65         TEST_ADDONE(0x7FFFFFFE);
66         TEST_ADDONE(0xFFFFFFFE);
67         TEST_ADDONE(0xFFFFFFFF);
68         TEST_ADDONE(random() & 0xFFFFFFFF);
69
70         return ret;
71 }
72
73 /*
74   test the EchoData interface
75 */
76 static BOOL test_echodata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
77 {
78         int i;
79         NTSTATUS status;
80         uint8_t *data_in, *data_out;
81         int len;
82         struct echo_EchoData r;
83
84         if (lp_parm_bool(-1, "torture", "quick", False) &&
85             (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
86                 len = 1 + (random() % 500);
87         } else {
88                 len = 1 + (random() % 5000);
89         }
90
91         printf("\nTesting EchoData\n");
92
93         data_in = talloc_size(mem_ctx, len);
94         data_out = talloc_size(mem_ctx, len);
95         for (i=0;i<len;i++) {
96                 data_in[i] = i;
97         }
98         
99         r.in.len = len;
100         r.in.in_data = data_in;
101
102         status = dcerpc_echo_EchoData(p, mem_ctx, &r);
103         if (!NT_STATUS_IS_OK(status)) {
104                 printf("EchoData(%d) failed - %s\n", len, nt_errstr(status));
105                 return False;
106         }
107
108         data_out = r.out.out_data;
109
110         for (i=0;i<len;i++) {
111                 if (data_in[i] != data_out[i]) {
112                         printf("Bad data returned for len %d at offset %d\n", 
113                                len, i);
114                         printf("in:\n");
115                         dump_data(0, data_in+i, MIN(len-i, 16));
116                         printf("out:\n");
117                         dump_data(0, data_out+i, MIN(len-1, 16));
118                         return False;
119                 }
120         }
121
122
123         return True;
124 }
125
126
127 /*
128   test the SourceData interface
129 */
130 static BOOL test_sourcedata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
131 {
132         int i;
133         NTSTATUS status;
134         int len;
135         struct echo_SourceData r;
136
137         if (lp_parm_bool(-1, "torture", "quick", False) &&
138             (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
139                 len = 100 + (random() % 500);
140         } else {
141                 len = 200000 + (random() % 5000);
142         }
143
144         printf("\nTesting SourceData\n");
145
146         r.in.len = len;
147
148         status = dcerpc_echo_SourceData(p, mem_ctx, &r);
149         if (!NT_STATUS_IS_OK(status)) {
150                 printf("SourceData(%d) failed - %s\n", len, nt_errstr(status));
151                 return False;
152         }
153
154         for (i=0;i<len;i++) {
155                 uint8_t *v = (uint8_t *)r.out.data;
156                 if (v[i] != (i & 0xFF)) {
157                         printf("bad data 0x%x at %d\n", (uint8_t)r.out.data[i], i);
158                         return False;
159                 }
160         }
161
162         return True;
163 }
164
165 /*
166   test the SinkData interface
167 */
168 static BOOL test_sinkdata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
169 {
170         int i;
171         NTSTATUS status;
172         uint8_t *data_in;
173         int len;
174         struct echo_SinkData r;
175
176         if (lp_parm_bool(-1, "torture", "quick", False) &&
177             (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
178                 len = 100 + (random() % 5000);
179         } else {
180                 len = 200000 + (random() % 5000);
181         }
182
183         printf("\nTesting SinkData\n");
184
185         data_in = talloc_size(mem_ctx, len);
186         for (i=0;i<len;i++) {
187                 data_in[i] = i+1;
188         }
189
190         r.in.len = len;
191         r.in.data = data_in;
192
193         status = dcerpc_echo_SinkData(p, mem_ctx, &r);
194         if (!NT_STATUS_IS_OK(status)) {
195                 printf("SinkData(%d) failed - %s\n", len, nt_errstr(status));
196                 return False;
197         }
198
199         printf("sunk %d bytes\n", len);
200
201         return True;
202 }
203
204
205 /*
206   test the testcall interface
207 */
208 static BOOL test_testcall(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
209 {
210         NTSTATUS status;
211         struct echo_TestCall r;
212         char *s = NULL;
213
214         r.in.s1 = "input string";
215         r.out.s2 = &s;
216
217         printf("\nTesting TestCall\n");
218         status = dcerpc_echo_TestCall(p, mem_ctx, &r);
219         if (!NT_STATUS_IS_OK(status)) {
220                 printf("TestCall failed - %s\n", nt_errstr(status));
221                 return False;
222         }
223
224         if (!strcmp(s, "input string")) {
225                 printf("Didn't receive back same string\n");
226                 return False;
227         }
228
229         return True;
230 }
231
232 /*
233   test the testcall interface
234 */
235 static BOOL test_testcall2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
236 {
237         NTSTATUS status;
238         struct echo_TestCall2 r;
239         int i;
240         BOOL ret = True;
241
242         for (i=1;i<=7;i++) {
243                 r.in.level = i;
244                 r.out.info = talloc(mem_ctx, union echo_Info);
245
246                 printf("\nTesting TestCall2 level %d\n", i);
247                 status = dcerpc_echo_TestCall2(p, mem_ctx, &r);
248                 if (!NT_STATUS_IS_OK(status)) {
249                         printf("TestCall2 failed - %s\n", nt_errstr(status));
250                         ret = False;
251                 }
252         }
253
254         return ret;
255 }
256
257 /*
258   test the TestSleep interface
259 */
260 static BOOL test_sleep(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
261 {
262         int i;
263         NTSTATUS status;
264 #define ASYNC_COUNT 3
265         struct rpc_request *req[ASYNC_COUNT];
266         struct echo_TestSleep r[ASYNC_COUNT];
267         BOOL done[ASYNC_COUNT];
268         struct timeval snd[ASYNC_COUNT];
269         struct timeval rcv[ASYNC_COUNT];
270         struct timeval diff[ASYNC_COUNT];
271         struct event_context *ctx;
272         int total_done = 0;
273         BOOL ret = True;
274
275         if (lp_parm_bool(-1, "torture", "quick", False)) {
276                 printf("TestSleep disabled - use \"torture:quick=no\" to enable\n");
277                 return True;
278         }
279         printf("Testing TestSleep - use \"torture:quick=no\" to disable\n");
280
281         for (i=0;i<ASYNC_COUNT;i++) {
282                 done[i]         = False;
283                 snd[i]          = timeval_current();
284                 rcv[i]          = timeval_zero();
285                 r[i].in.seconds = ASYNC_COUNT-i;
286                 req[i] = dcerpc_echo_TestSleep_send(p, mem_ctx, &r[i]);
287                 if (!req[i]) {
288                         printf("Failed to send async sleep request\n");
289                         return False;
290                 }
291         }
292
293         ctx = dcerpc_event_context(p);
294         while (total_done < ASYNC_COUNT) {
295                 if (event_loop_once(ctx) != 0) {
296                         return False;
297                 }
298                 for (i=0;i<ASYNC_COUNT;i++) {
299                         if (done[i] == False && req[i]->state == RPC_REQUEST_DONE) {
300                                 total_done++;
301                                 done[i] = True;
302                                 rcv[i]  = timeval_current();
303                                 diff[i] = timeval_until(&snd[i], &rcv[i]);
304                                 status  = dcerpc_ndr_request_recv(req[i]);
305                                 if (!NT_STATUS_IS_OK(status)) {
306                                         printf("TestSleep(%d) failed - %s\n",
307                                                i, nt_errstr(status));
308                                         ret = False;
309                                 } else if (r[i].out.result != r[i].in.seconds) {
310                                         printf("Failed - Asked to sleep for %u seconds (server replied with %u seconds and the reply takes only %u seconds)\n", 
311                                                 r[i].out.result, r[i].in.seconds, (uint_t)diff[i].tv_sec);
312                                         ret = False;
313                                 } else {
314                                         if (r[i].out.result > diff[i].tv_sec) {
315                                                 printf("Failed - Slept for %u seconds (but reply takes only %u.%06u seconds)\n", 
316                                                         r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
317                                         } else if (r[i].out.result+1 == diff[i].tv_sec) {
318                                                 printf("Slept for %u seconds (but reply takes %u.%06u seconds - busy server?)\n", 
319                                                         r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
320                                         } else if (r[i].out.result == diff[i].tv_sec) {
321                                                 printf("Slept for %u seconds (reply takes %u.%06u seconds - ok)\n", 
322                                                         r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
323                                         } else {
324                                                 printf("(Failed) - Not async - Slept for %u seconds (but reply takes %u.%06u seconds)\n", 
325                                                         r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
326                                                 /* TODO: let the test fail here, when we support async rpc on ncacn_np
327                                                 ret = False;*/
328                                         }
329                                 }
330                         }
331                 }
332         }
333
334         return ret;
335 }
336
337 /*
338   test enum handling
339 */
340 static BOOL test_enum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
341 {
342         NTSTATUS status;
343         struct echo_TestEnum r;
344         BOOL ret = True;
345         enum echo_Enum1 v = ECHO_ENUM1;
346         struct echo_Enum2 e2;
347         union echo_Enum3 e3;
348
349         r.in.foo1 = &v;
350         r.in.foo2 = &e2;
351         r.in.foo3 = &e3;
352         r.out.foo1 = &v;
353         r.out.foo2 = &e2;
354         r.out.foo3 = &e3;
355
356         e2.e1 = 76;
357         e2.e2 = ECHO_ENUM1_32;
358         e3.e1 = ECHO_ENUM2;
359
360         printf("\nTesting TestEnum\n");
361         status = dcerpc_echo_TestEnum(p, mem_ctx, &r);
362         if (!NT_STATUS_IS_OK(status)) {
363                 printf("TestEnum failed - %s\n", nt_errstr(status));
364                 ret = False;
365         }
366
367         return ret;
368 }
369
370 /*
371   test surrounding conformant array handling
372 */
373 static BOOL test_surrounding(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
374 {
375         NTSTATUS status;
376         struct echo_TestSurrounding r;
377         BOOL ret = True;
378
379         ZERO_STRUCT(r);
380         r.in.data = talloc(mem_ctx, struct echo_Surrounding);
381
382         r.in.data->x = 20;
383         r.in.data->surrounding = talloc_zero_array(mem_ctx, uint16_t, r.in.data->x);
384
385         r.out.data = talloc(mem_ctx, struct echo_Surrounding);
386
387         printf("\nTesting TestSurrounding\n");
388         status = dcerpc_echo_TestSurrounding(p, mem_ctx, &r);
389         if (!NT_STATUS_IS_OK(status)) {
390                 printf("TestSurrounding failed - %s\n", nt_errstr(status));
391                 return False;
392         }
393         
394         if (r.out.data->x != 2 * r.in.data->x) {
395                 printf("TestSurrounding did not make the array twice as large\n");
396                 ret = False;
397         }
398
399         return ret;
400 }
401
402 /*
403   test multiple levels of pointers
404 */
405 static BOOL test_doublepointer(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
406 {
407         NTSTATUS status;
408         struct echo_TestDoublePointer r;
409         BOOL ret = True;
410         uint16_t value = 12;
411         uint16_t *pvalue = &value;
412         uint16_t **ppvalue = &pvalue;
413
414         ZERO_STRUCT(r);
415         r.in.data = &ppvalue;
416
417         printf("\nTesting TestDoublePointer\n");
418         status = dcerpc_echo_TestDoublePointer(p, mem_ctx, &r);
419         if (!NT_STATUS_IS_OK(status)) {
420                 printf("TestDoublePointer failed - %s\n", nt_errstr(status));
421                 ret = False;
422         }
423
424         if (value != r.out.result) {
425                 printf("TestDoublePointer did not return original value (%d != %d)\n", value, r.out.result);
426                 ret = False;
427         }
428
429         return ret;
430 }
431
432
433 /*
434   test request timeouts
435 */
436 static BOOL test_timeout(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
437 {
438         NTSTATUS status;
439         struct rpc_request *req;
440         struct echo_TestSleep r;
441         int timeout_saved = p->request_timeout;
442
443         if (lp_parm_bool(-1, "torture", "quick", False)) {
444                 printf("timeout testing disabled - use \"torture:quick=no\" to enable\n");
445                 return True;
446         }
447
448         printf("testing request timeouts\n");
449         r.in.seconds = 2;
450         p->request_timeout = 1;
451
452         req = dcerpc_echo_TestSleep_send(p, mem_ctx, &r);
453         if (!req) {
454                 printf("Failed to send async sleep request\n");
455                 goto failed;
456         }
457
458         status  = dcerpc_ndr_request_recv(req);
459         if (!NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
460                 printf("request should have timed out - %s\n", nt_errstr(status));
461                 goto failed;
462         }
463
464         printf("testing request destruction\n");
465         req = dcerpc_echo_TestSleep_send(p, mem_ctx, &r);
466         if (!req) {
467                 printf("Failed to send async sleep request\n");
468                 goto failed;
469         }
470         talloc_free(req);
471
472         req = dcerpc_echo_TestSleep_send(p, mem_ctx, &r);
473         if (!req) {
474                 printf("Failed to send async sleep request\n");
475                 goto failed;
476         }
477         status  = dcerpc_ndr_request_recv(req);
478         if (!NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
479                 printf("request should have timed out - %s\n", nt_errstr(status));
480                 goto failed;
481         }       
482
483         p->request_timeout = timeout_saved;
484         return test_addone(p, mem_ctx);
485
486 failed:
487         p->request_timeout = timeout_saved;
488         return False;
489 }
490
491
492 BOOL torture_rpc_echo(struct torture_context *torture)
493 {
494         NTSTATUS status;
495         struct dcerpc_pipe *p;
496         TALLOC_CTX *mem_ctx;
497         BOOL ret = True;
498
499         mem_ctx = talloc_init("torture_rpc_echo");
500
501         status = torture_rpc_connection(mem_ctx, 
502                                         &p, 
503                                         &dcerpc_table_rpcecho);
504         if (!NT_STATUS_IS_OK(status)) {
505                 return False;
506         }
507
508         ret &= test_addone(p, mem_ctx);
509         ret &= test_sinkdata(p, mem_ctx);
510         ret &= test_echodata(p, mem_ctx);
511         ret &= test_sourcedata(p, mem_ctx);
512         ret &= test_testcall(p, mem_ctx);
513         ret &= test_testcall2(p, mem_ctx);
514         ret &= test_enum(p, mem_ctx);
515         ret &= test_surrounding(p, mem_ctx);
516         ret &= test_doublepointer(p, mem_ctx);
517         ret &= test_sleep(p, mem_ctx);
518         ret &= test_timeout(p, mem_ctx);
519
520         printf("\n");
521         
522         talloc_free(mem_ctx);
523
524         return ret;
525 }