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