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