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);
111 struct echo_SourceData r;
113 printf("\nTesting SourceData\n");
115 data_out = talloc_size(mem_ctx, len);
118 r.out.data = data_out;
120 status = dcerpc_echo_SourceData(p, mem_ctx, &r);
121 if (!NT_STATUS_IS_OK(status)) {
122 printf("SourceData(%d) failed - %s\n", len, nt_errstr(status));
126 for (i=0;i<len;i++) {
127 uint8_t *v = (uint8_t *)data_out;
128 if (v[i] != (i & 0xFF)) {
129 printf("bad data 0x%x at %d\n", (uint8_t)data_out[i], i);
138 test the SinkData interface
140 static BOOL test_sinkdata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
145 int len = 200000 + (random() % 5000);
146 struct echo_SinkData r;
148 printf("\nTesting SinkData\n");
150 data_in = talloc_size(mem_ctx, len);
151 for (i=0;i<len;i++) {
158 status = dcerpc_echo_SinkData(p, mem_ctx, &r);
159 if (!NT_STATUS_IS_OK(status)) {
160 printf("SinkData(%d) failed - %s\n", len, nt_errstr(status));
164 printf("sunk %d bytes\n", len);
171 test the testcall interface
173 static BOOL test_testcall(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
176 struct echo_TestCall r;
178 r.in.s1 = "input string";
180 printf("\nTesting TestCall\n");
181 status = dcerpc_echo_TestCall(p, mem_ctx, &r);
182 if (!NT_STATUS_IS_OK(status)) {
183 printf("TestCall failed - %s\n", nt_errstr(status));
191 test the testcall interface
193 static BOOL test_testcall2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
196 struct echo_TestCall2 r;
203 printf("\nTesting TestCall2 level %d\n", i);
204 status = dcerpc_echo_TestCall2(p, mem_ctx, &r);
205 if (!NT_STATUS_IS_OK(status)) {
206 printf("TestCall2 failed - %s\n", nt_errstr(status));
215 test the TestSleep interface
217 static BOOL test_sleep(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
221 #define ASYNC_COUNT 3
222 struct rpc_request *req[ASYNC_COUNT];
223 struct echo_TestSleep r[ASYNC_COUNT];
224 BOOL done[ASYNC_COUNT];
225 struct timeval snd[ASYNC_COUNT];
226 struct timeval rcv[ASYNC_COUNT];
227 struct timeval diff[ASYNC_COUNT];
228 struct event_context *ctx;
232 printf("\nTesting TestSleep\n");
234 for (i=0;i<ASYNC_COUNT;i++) {
236 snd[i] = timeval_current();
237 rcv[i] = timeval_zero();
238 r[i].in.seconds = ASYNC_COUNT-i;
239 req[i] = dcerpc_echo_TestSleep_send(p, mem_ctx, &r[i]);
241 printf("Failed to send async sleep request\n");
246 ctx = dcerpc_event_context(p);
247 while (total_done < ASYNC_COUNT) {
248 if (event_loop_once(ctx) != 0) {
251 for (i=0;i<ASYNC_COUNT;i++) {
252 if (done[i] == False && req[i]->state == RPC_REQUEST_DONE) {
255 rcv[i] = timeval_current();
256 diff[i] = timeval_diff(&rcv[i], &snd[i]);
257 status = dcerpc_ndr_request_recv(req[i]);
258 if (!NT_STATUS_IS_OK(status)) {
259 printf("TestSleep(%d) failed - %s\n",
260 i, nt_errstr(status));
262 } else if (r[i].out.result != r[i].in.seconds) {
263 printf("Failed - Sleeped for %u seconds (but we said %u seconds and the reply takes only %u seconds)\n",
264 r[i].out.result, r[i].in.seconds, (uint_t)diff[i].tv_sec);
267 if (r[i].out.result > diff[i].tv_sec) {
268 printf("Failed - Sleeped for %u seconds (but reply takes only %u.%06u seconds)\n",
269 r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
270 } else if (r[i].out.result+1 == diff[i].tv_sec) {
271 printf("Sleeped for %u seconds (but reply takes %u.%06u seconds - busy server?)\n",
272 r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
273 } else if (r[i].out.result == diff[i].tv_sec) {
274 printf("Sleeped for %u seconds (reply takes %u.%06u seconds - ok)\n",
275 r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
277 printf("(Failed) - Not async - Sleeped for %u seconds (but reply takes %u.%06u seconds)\n",
278 r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
279 /* TODO: let the test fail here, when we support async rpc on ncacn_np
293 static BOOL test_enum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
296 struct echo_TestEnum r;
298 enum echo_Enum1 v = ECHO_ENUM1;
299 struct echo_Enum2 e2;
310 e2.e2 = ECHO_ENUM1_32;
313 printf("\nTesting TestEnum\n");
314 status = dcerpc_echo_TestEnum(p, mem_ctx, &r);
315 if (!NT_STATUS_IS_OK(status)) {
316 printf("TestEnum failed - %s\n", nt_errstr(status));
324 test surrounding conformant array handling
326 static BOOL test_surrounding(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
329 struct echo_TestSurrounding r;
333 r.in.data = talloc(mem_ctx, struct echo_Surrounding);
336 r.in.data->surrounding = talloc_zero_array(mem_ctx, uint16_t, r.in.data->x);
338 r.out.data = talloc(mem_ctx, struct echo_Surrounding);
340 printf("\nTesting TestSurrounding\n");
341 status = dcerpc_echo_TestSurrounding(p, mem_ctx, &r);
342 if (!NT_STATUS_IS_OK(status)) {
343 printf("TestSurrounding failed - %s\n", nt_errstr(status));
347 if (r.out.data->x != 2 * r.in.data->x) {
348 printf("TestSurrounding did not make the array twice as large\n");
356 test multiple levels of pointers
358 static BOOL test_doublepointer(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
361 struct echo_TestDoublePointer r;
364 uint16_t *pvalue = &value;
365 uint16_t **ppvalue = &pvalue;
368 r.in.data = &ppvalue;
370 printf("\nTesting TestDoublePointer\n");
371 status = dcerpc_echo_TestDoublePointer(p, mem_ctx, &r);
372 if (!NT_STATUS_IS_OK(status)) {
373 printf("TestDoublePointer failed - %s\n", nt_errstr(status));
377 if (value != r.out.result) {
378 printf("TestSurrounding did not return original value\n");
384 status = dcerpc_echo_TestDoublePointer(p, mem_ctx, &r);
385 if (!NT_STATUS_IS_OK(status)) {
386 printf("TestDoublePointer failed - %s\n", nt_errstr(status));
390 if (r.out.result != 0) {
391 printf("TestSurrounding did not return 0 when passed a NULL pointer\n");
398 BOOL torture_rpc_echo(void)
401 struct dcerpc_pipe *p;
405 mem_ctx = talloc_init("torture_rpc_echo");
407 status = torture_rpc_connection(mem_ctx,
411 DCERPC_RPCECHO_VERSION);
412 if (!NT_STATUS_IS_OK(status)) {
416 ret &= test_addone(p, mem_ctx);
417 ret &= test_sinkdata(p, mem_ctx);
418 ret &= test_echodata(p, mem_ctx);
419 ret &= test_sourcedata(p, mem_ctx);
420 ret &= test_testcall(p, mem_ctx);
421 ret &= test_testcall2(p, mem_ctx);
422 ret &= test_enum(p, mem_ctx);
423 ret &= test_surrounding(p, mem_ctx);
424 ret &= test_doublepointer(p, mem_ctx);
425 ret &= test_sleep(p, mem_ctx);
429 talloc_free(mem_ctx);