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 "torture/torture.h"
26 #include "torture/rpc/rpc.h"
27 #include "lib/events/events.h"
28 #include "librpc/gen_ndr/ndr_echo.h"
32 test the AddOne interface
34 #define TEST_ADDONE(value) do { \
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)); \
44 printf("%d + 1 != %u (should be %u)\n", i, n, i+1); \
47 printf("%d + 1 = %u\n", i, n); \
51 static BOOL test_addone(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
59 printf("\nTesting AddOne\n");
65 TEST_ADDONE(0x7FFFFFFE);
66 TEST_ADDONE(0xFFFFFFFE);
67 TEST_ADDONE(0xFFFFFFFF);
68 TEST_ADDONE(random() & 0xFFFFFFFF);
74 test the EchoData interface
76 static BOOL test_echodata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
80 uint8_t *data_in, *data_out;
82 struct echo_EchoData r;
84 if (lp_parm_bool(-1, "torture", "quick", False) &&
85 (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
86 len = 1 + (random() % 500);
88 len = 1 + (random() % 5000);
91 printf("\nTesting EchoData\n");
93 data_in = talloc_size(mem_ctx, len);
94 data_out = talloc_size(mem_ctx, len);
100 r.in.in_data = data_in;
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));
108 data_out = r.out.out_data;
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",
115 dump_data(0, data_in+i, MIN(len-i, 16));
117 dump_data(0, data_out+i, MIN(len-1, 16));
128 test the SourceData interface
130 static BOOL test_sourcedata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
135 struct echo_SourceData r;
137 if (lp_parm_bool(-1, "torture", "quick", False) &&
138 (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
139 len = 100 + (random() % 500);
141 len = 200000 + (random() % 5000);
144 printf("\nTesting SourceData\n");
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));
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);
166 test the SinkData interface
168 static BOOL test_sinkdata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
174 struct echo_SinkData r;
176 if (lp_parm_bool(-1, "torture", "quick", False) &&
177 (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
178 len = 100 + (random() % 5000);
180 len = 200000 + (random() % 5000);
183 printf("\nTesting SinkData\n");
185 data_in = talloc_size(mem_ctx, len);
186 for (i=0;i<len;i++) {
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));
199 printf("sunk %d bytes\n", len);
206 test the testcall interface
208 static BOOL test_testcall(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
211 struct echo_TestCall r;
213 r.in.s1 = "input string";
215 printf("\nTesting TestCall\n");
216 status = dcerpc_echo_TestCall(p, mem_ctx, &r);
217 if (!NT_STATUS_IS_OK(status)) {
218 printf("TestCall failed - %s\n", nt_errstr(status));
226 test the testcall interface
228 static BOOL test_testcall2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
231 struct echo_TestCall2 r;
237 r.out.info = talloc(mem_ctx, union echo_Info);
239 printf("\nTesting TestCall2 level %d\n", i);
240 status = dcerpc_echo_TestCall2(p, mem_ctx, &r);
241 if (!NT_STATUS_IS_OK(status)) {
242 printf("TestCall2 failed - %s\n", nt_errstr(status));
251 test the TestSleep interface
253 static BOOL test_sleep(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
257 #define ASYNC_COUNT 3
258 struct rpc_request *req[ASYNC_COUNT];
259 struct echo_TestSleep r[ASYNC_COUNT];
260 BOOL done[ASYNC_COUNT];
261 struct timeval snd[ASYNC_COUNT];
262 struct timeval rcv[ASYNC_COUNT];
263 struct timeval diff[ASYNC_COUNT];
264 struct event_context *ctx;
268 if (lp_parm_bool(-1, "torture", "quick", False)) {
269 printf("TestSleep disabled - use \"torture:quick=no\" to enable\n");
272 printf("Testing TestSleep - use \"torture:quick=no\" to disable\n");
274 for (i=0;i<ASYNC_COUNT;i++) {
276 snd[i] = timeval_current();
277 rcv[i] = timeval_zero();
278 r[i].in.seconds = ASYNC_COUNT-i;
279 req[i] = dcerpc_echo_TestSleep_send(p, mem_ctx, &r[i]);
281 printf("Failed to send async sleep request\n");
286 ctx = dcerpc_event_context(p);
287 while (total_done < ASYNC_COUNT) {
288 if (event_loop_once(ctx) != 0) {
291 for (i=0;i<ASYNC_COUNT;i++) {
292 if (done[i] == False && req[i]->state == RPC_REQUEST_DONE) {
295 rcv[i] = timeval_current();
296 diff[i] = timeval_until(&snd[i], &rcv[i]);
297 status = dcerpc_ndr_request_recv(req[i]);
298 if (!NT_STATUS_IS_OK(status)) {
299 printf("TestSleep(%d) failed - %s\n",
300 i, nt_errstr(status));
302 } else if (r[i].out.result != r[i].in.seconds) {
303 printf("Failed - Asked to sleep for %u seconds (server replied with %u seconds and the reply takes only %u seconds)\n",
304 r[i].out.result, r[i].in.seconds, (uint_t)diff[i].tv_sec);
307 if (r[i].out.result > diff[i].tv_sec) {
308 printf("Failed - Slept for %u seconds (but reply takes only %u.%06u seconds)\n",
309 r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
310 } else if (r[i].out.result+1 == diff[i].tv_sec) {
311 printf("Slept for %u seconds (but reply takes %u.%06u seconds - busy server?)\n",
312 r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
313 } else if (r[i].out.result == diff[i].tv_sec) {
314 printf("Slept for %u seconds (reply takes %u.%06u seconds - ok)\n",
315 r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
317 printf("(Failed) - Not async - Slept for %u seconds (but reply takes %u.%06u seconds)\n",
318 r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
319 /* TODO: let the test fail here, when we support async rpc on ncacn_np
333 static BOOL test_enum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
336 struct echo_TestEnum r;
338 enum echo_Enum1 v = ECHO_ENUM1;
339 struct echo_Enum2 e2;
350 e2.e2 = ECHO_ENUM1_32;
353 printf("\nTesting TestEnum\n");
354 status = dcerpc_echo_TestEnum(p, mem_ctx, &r);
355 if (!NT_STATUS_IS_OK(status)) {
356 printf("TestEnum failed - %s\n", nt_errstr(status));
364 test surrounding conformant array handling
366 static BOOL test_surrounding(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
369 struct echo_TestSurrounding r;
373 r.in.data = talloc(mem_ctx, struct echo_Surrounding);
376 r.in.data->surrounding = talloc_zero_array(mem_ctx, uint16_t, r.in.data->x);
378 r.out.data = talloc(mem_ctx, struct echo_Surrounding);
380 printf("\nTesting TestSurrounding\n");
381 status = dcerpc_echo_TestSurrounding(p, mem_ctx, &r);
382 if (!NT_STATUS_IS_OK(status)) {
383 printf("TestSurrounding failed - %s\n", nt_errstr(status));
387 if (r.out.data->x != 2 * r.in.data->x) {
388 printf("TestSurrounding did not make the array twice as large\n");
396 test multiple levels of pointers
398 static BOOL test_doublepointer(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
401 struct echo_TestDoublePointer r;
404 uint16_t *pvalue = &value;
405 uint16_t **ppvalue = &pvalue;
408 r.in.data = &ppvalue;
410 printf("\nTesting TestDoublePointer\n");
411 status = dcerpc_echo_TestDoublePointer(p, mem_ctx, &r);
412 if (!NT_STATUS_IS_OK(status)) {
413 printf("TestDoublePointer failed - %s\n", nt_errstr(status));
417 if (value != r.out.result) {
418 printf("TestDoublePointer did not return original value (%d != %d)\n", value, r.out.result);
427 test request timeouts
429 static BOOL test_timeout(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
432 struct rpc_request *req;
433 struct echo_TestSleep r;
434 int timeout_saved = p->request_timeout;
436 if (lp_parm_bool(-1, "torture", "quick", False)) {
437 printf("timeout testing disabled - use \"torture:quick=no\" to enable\n");
441 printf("testing request timeouts\n");
443 p->request_timeout = 1;
445 req = dcerpc_echo_TestSleep_send(p, mem_ctx, &r);
447 printf("Failed to send async sleep request\n");
451 status = dcerpc_ndr_request_recv(req);
452 if (!NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
453 printf("request should have timed out - %s\n", nt_errstr(status));
457 printf("testing request destruction\n");
458 req = dcerpc_echo_TestSleep_send(p, mem_ctx, &r);
460 printf("Failed to send async sleep request\n");
465 req = dcerpc_echo_TestSleep_send(p, mem_ctx, &r);
467 printf("Failed to send async sleep request\n");
470 status = dcerpc_ndr_request_recv(req);
471 if (!NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
472 printf("request should have timed out - %s\n", nt_errstr(status));
476 p->request_timeout = timeout_saved;
477 return test_addone(p, mem_ctx);
480 p->request_timeout = timeout_saved;
485 BOOL torture_rpc_echo(void)
488 struct dcerpc_pipe *p;
492 mem_ctx = talloc_init("torture_rpc_echo");
494 status = torture_rpc_connection(mem_ctx,
496 &dcerpc_table_rpcecho);
497 if (!NT_STATUS_IS_OK(status)) {
501 ret &= test_addone(p, mem_ctx);
502 ret &= test_sinkdata(p, mem_ctx);
503 ret &= test_echodata(p, mem_ctx);
504 ret &= test_sourcedata(p, mem_ctx);
505 ret &= test_testcall(p, mem_ctx);
506 ret &= test_testcall2(p, mem_ctx);
507 ret &= test_enum(p, mem_ctx);
508 ret &= test_surrounding(p, mem_ctx);
509 ret &= test_doublepointer(p, mem_ctx);
510 ret &= test_sleep(p, mem_ctx);
511 ret &= test_timeout(p, mem_ctx);
515 talloc_free(mem_ctx);