2 Unix SMB/CIFS implementation.
3 test suite for echo rpc operations
5 Copyright (C) Andrew Tridgell 2003
6 Copyright (C) Stefan (metze) Metzmacher 2005
7 Copyright (C) Jelmer Vernooij 2005
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.
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.
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.
25 #include "lib/events/events.h"
26 #include "librpc/gen_ndr/ndr_echo.h"
30 test the AddOne interface
32 static BOOL test_addone(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
37 printf("\nTesting AddOne\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));
49 printf("%d + 1 = %u\n", i, n);
56 test the EchoData interface
58 static BOOL test_echodata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
62 uint8_t *data_in, *data_out;
63 int len = 1 + (random() % 5000);
64 struct echo_EchoData r;
66 printf("\nTesting EchoData\n");
68 data_in = talloc_size(mem_ctx, len);
69 data_out = talloc_size(mem_ctx, len);
75 r.in.in_data = data_in;
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));
83 data_out = r.out.out_data;
86 if (data_in[i] != data_out[i]) {
87 printf("Bad data returned for len %d at offset %d\n",
90 dump_data(0, data_in+i, MIN(len-i, 16));
92 dump_data(0, data_out+i, MIN(len-1, 16));
103 test the SourceData interface
105 static BOOL test_sourcedata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
109 int len = 200000 + (random() % 5000);
110 struct echo_SourceData r;
112 printf("\nTesting SourceData\n");
116 status = dcerpc_echo_SourceData(p, mem_ctx, &r);
117 if (!NT_STATUS_IS_OK(status)) {
118 printf("SourceData(%d) failed - %s\n", len, nt_errstr(status));
122 for (i=0;i<len;i++) {
123 uint8_t *v = (uint8_t *)r.out.data;
124 if (v[i] != (i & 0xFF)) {
125 printf("bad data 0x%x at %d\n", (uint8_t)r.out.data[i], i);
134 test the SinkData interface
136 static BOOL test_sinkdata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
141 int len = 200000 + (random() % 5000);
142 struct echo_SinkData r;
144 printf("\nTesting SinkData\n");
146 data_in = talloc_size(mem_ctx, len);
147 for (i=0;i<len;i++) {
154 status = dcerpc_echo_SinkData(p, mem_ctx, &r);
155 if (!NT_STATUS_IS_OK(status)) {
156 printf("SinkData(%d) failed - %s\n", len, nt_errstr(status));
160 printf("sunk %d bytes\n", len);
167 test the testcall interface
169 static BOOL test_testcall(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
172 struct echo_TestCall r;
174 r.in.s1 = "input string";
176 printf("\nTesting TestCall\n");
177 status = dcerpc_echo_TestCall(p, mem_ctx, &r);
178 if (!NT_STATUS_IS_OK(status)) {
179 printf("TestCall failed - %s\n", nt_errstr(status));
187 test the testcall interface
189 static BOOL test_testcall2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
192 struct echo_TestCall2 r;
198 r.out.info = talloc(mem_ctx, union echo_Info);
200 printf("\nTesting TestCall2 level %d\n", i);
201 status = dcerpc_echo_TestCall2(p, mem_ctx, &r);
202 if (!NT_STATUS_IS_OK(status)) {
203 printf("TestCall2 failed - %s\n", nt_errstr(status));
212 test the TestSleep interface
214 static BOOL test_sleep(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
218 #define ASYNC_COUNT 3
219 struct rpc_request *req[ASYNC_COUNT];
220 struct echo_TestSleep r[ASYNC_COUNT];
221 BOOL done[ASYNC_COUNT];
222 struct timeval snd[ASYNC_COUNT];
223 struct timeval rcv[ASYNC_COUNT];
224 struct timeval diff[ASYNC_COUNT];
225 struct event_context *ctx;
229 if (!lp_parm_bool(-1, "torture", "echo_TestSleep", True)) {
230 printf("TestSleep disabled - use \"torture:echo_TestSleep=yes\" to enable\n");
233 printf("Testing TestSleep - use \"torture:echo_TestSleep=no\" to disable\n");
235 for (i=0;i<ASYNC_COUNT;i++) {
237 snd[i] = timeval_current();
238 rcv[i] = timeval_zero();
239 r[i].in.seconds = ASYNC_COUNT-i;
240 req[i] = dcerpc_echo_TestSleep_send(p, mem_ctx, &r[i]);
242 printf("Failed to send async sleep request\n");
247 ctx = dcerpc_event_context(p);
248 while (total_done < ASYNC_COUNT) {
249 if (event_loop_once(ctx) != 0) {
252 for (i=0;i<ASYNC_COUNT;i++) {
253 if (done[i] == False && req[i]->state == RPC_REQUEST_DONE) {
256 rcv[i] = timeval_current();
257 diff[i] = timeval_until(&snd[i], &rcv[i]);
258 status = dcerpc_ndr_request_recv(req[i]);
259 if (!NT_STATUS_IS_OK(status)) {
260 printf("TestSleep(%d) failed - %s\n",
261 i, nt_errstr(status));
263 } else if (r[i].out.result != r[i].in.seconds) {
264 printf("Failed - Asked to sleep for %u seconds (server replied with %u seconds and the reply takes only %u seconds)\n",
265 r[i].out.result, r[i].in.seconds, (uint_t)diff[i].tv_sec);
268 if (r[i].out.result > diff[i].tv_sec) {
269 printf("Failed - Slept for %u seconds (but reply takes only %u.%06u seconds)\n",
270 r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
271 } else if (r[i].out.result+1 == diff[i].tv_sec) {
272 printf("Slept for %u seconds (but reply takes %u.%06u seconds - busy server?)\n",
273 r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
274 } else if (r[i].out.result == diff[i].tv_sec) {
275 printf("Slept for %u seconds (reply takes %u.%06u seconds - ok)\n",
276 r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
278 printf("(Failed) - Not async - Slept for %u seconds (but reply takes %u.%06u seconds)\n",
279 r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
280 /* TODO: let the test fail here, when we support async rpc on ncacn_np
294 static BOOL test_enum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
297 struct echo_TestEnum r;
299 enum echo_Enum1 v = ECHO_ENUM1;
300 struct echo_Enum2 e2;
311 e2.e2 = ECHO_ENUM1_32;
314 printf("\nTesting TestEnum\n");
315 status = dcerpc_echo_TestEnum(p, mem_ctx, &r);
316 if (!NT_STATUS_IS_OK(status)) {
317 printf("TestEnum failed - %s\n", nt_errstr(status));
325 test surrounding conformant array handling
327 static BOOL test_surrounding(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
330 struct echo_TestSurrounding r;
334 r.in.data = talloc(mem_ctx, struct echo_Surrounding);
337 r.in.data->surrounding = talloc_zero_array(mem_ctx, uint16_t, r.in.data->x);
339 r.out.data = talloc(mem_ctx, struct echo_Surrounding);
341 printf("\nTesting TestSurrounding\n");
342 status = dcerpc_echo_TestSurrounding(p, mem_ctx, &r);
343 if (!NT_STATUS_IS_OK(status)) {
344 printf("TestSurrounding failed - %s\n", nt_errstr(status));
348 if (r.out.data->x != 2 * r.in.data->x) {
349 printf("TestSurrounding did not make the array twice as large\n");
357 test multiple levels of pointers
359 static BOOL test_doublepointer(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
362 struct echo_TestDoublePointer r;
365 uint16_t *pvalue = &value;
366 uint16_t **ppvalue = &pvalue;
369 r.in.data = &ppvalue;
371 printf("\nTesting TestDoublePointer\n");
372 status = dcerpc_echo_TestDoublePointer(p, mem_ctx, &r);
373 if (!NT_STATUS_IS_OK(status)) {
374 printf("TestDoublePointer failed - %s\n", nt_errstr(status));
378 if (value != r.out.result) {
379 printf("TestDoublePointer did not return original value (%d != %d)\n", value, r.out.result);
388 test request timeouts
390 static BOOL test_timeout(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
393 struct rpc_request *req;
394 struct echo_TestSleep r;
395 int timeout_saved = p->request_timeout;
397 if (!lp_parm_bool(-1, "torture", "echo_TestSleep", True)) {
398 printf("timeout testing disabled - use \"torture:echo_TestSleep=yes\" to enable\n");
402 printf("testing request timeouts\n");
404 p->request_timeout = 1;
406 req = dcerpc_echo_TestSleep_send(p, mem_ctx, &r);
408 printf("Failed to send async sleep request\n");
412 status = dcerpc_ndr_request_recv(req);
413 if (!NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
414 printf("request should have timed out - %s\n", nt_errstr(status));
418 printf("testing request destruction\n");
419 req = dcerpc_echo_TestSleep_send(p, mem_ctx, &r);
421 printf("Failed to send async sleep request\n");
426 req = dcerpc_echo_TestSleep_send(p, mem_ctx, &r);
428 printf("Failed to send async sleep request\n");
431 status = dcerpc_ndr_request_recv(req);
432 if (!NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
433 printf("request should have timed out - %s\n", nt_errstr(status));
437 p->request_timeout = timeout_saved;
438 return test_addone(p, mem_ctx);
441 p->request_timeout = timeout_saved;
446 BOOL torture_rpc_echo(void)
449 struct dcerpc_pipe *p;
453 mem_ctx = talloc_init("torture_rpc_echo");
455 status = torture_rpc_connection(mem_ctx,
459 DCERPC_RPCECHO_VERSION);
460 if (!NT_STATUS_IS_OK(status)) {
464 ret &= test_addone(p, mem_ctx);
465 ret &= test_sinkdata(p, mem_ctx);
466 ret &= test_echodata(p, mem_ctx);
467 ret &= test_sourcedata(p, mem_ctx);
468 ret &= test_testcall(p, mem_ctx);
469 ret &= test_testcall2(p, mem_ctx);
470 ret &= test_enum(p, mem_ctx);
471 ret &= test_surrounding(p, mem_ctx);
472 ret &= test_doublepointer(p, mem_ctx);
473 ret &= test_sleep(p, mem_ctx);
474 ret &= test_timeout(p, mem_ctx);
478 talloc_free(mem_ctx);