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 3 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, see <http://www.gnu.org/licenses/>.
24 #include "torture/rpc/torture_rpc.h"
25 #include "lib/events/events.h"
26 #include "librpc/gen_ndr/ndr_echo_c.h"
30 test the AddOne interface
32 #define TEST_ADDONE(tctx, value) do { \
35 r.out.out_data = &n; \
36 torture_assert_ntstatus_ok(tctx, dcerpc_echo_AddOne_r(b, tctx, &r), \
37 talloc_asprintf(tctx, "AddOne(%d) failed", i)); \
38 torture_assert (tctx, n == i+1, talloc_asprintf(tctx, "%d + 1 != %u (should be %u)\n", i, n, i+1)); \
39 torture_comment (tctx, "%d + 1 = %u\n", i, n); \
42 static bool test_addone(struct torture_context *tctx,
43 struct dcerpc_pipe *p)
48 struct dcerpc_binding_handle *b = p->binding_handle;
54 TEST_ADDONE(tctx, 0x7FFFFFFE);
55 TEST_ADDONE(tctx, 0xFFFFFFFE);
56 TEST_ADDONE(tctx, 0xFFFFFFFF);
57 TEST_ADDONE(tctx, random() & 0xFFFFFFFF);
62 test the EchoData interface
64 static bool test_echodata(struct torture_context *tctx,
65 struct dcerpc_pipe *p)
68 uint8_t *data_in, *data_out;
70 struct echo_EchoData r;
71 struct dcerpc_binding_handle *b = p->binding_handle;
73 if (torture_setting_bool(tctx, "quick", false) &&
74 (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
75 len = 1 + (random() % 500);
77 len = 1 + (random() % 5000);
80 data_in = talloc_array(tctx, uint8_t, len);
81 data_out = talloc_array(tctx, uint8_t, len);
87 r.in.in_data = data_in;
89 torture_assert_ntstatus_ok(tctx, dcerpc_echo_EchoData_r(b, tctx, &r),
90 talloc_asprintf(tctx, "EchoData(%d) failed\n", len));
92 data_out = r.out.out_data;
95 if (data_in[i] != data_out[i]) {
96 torture_comment(tctx, "Bad data returned for len %d at offset %d\n",
98 torture_comment(tctx, "in:\n");
99 dump_data(0, data_in+i, MIN(len-i, 16));
100 torture_comment(tctx, "out:\n");
101 dump_data(0, data_out+i, MIN(len-1, 16));
109 test the SourceData interface
111 static bool test_sourcedata(struct torture_context *tctx,
112 struct dcerpc_pipe *p)
116 struct echo_SourceData r;
117 struct dcerpc_binding_handle *b = p->binding_handle;
119 if (torture_setting_bool(tctx, "quick", false) &&
120 (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
121 len = 100 + (random() % 500);
123 len = 200000 + (random() % 5000);
128 torture_assert_ntstatus_ok(tctx, dcerpc_echo_SourceData_r(b, tctx, &r),
129 talloc_asprintf(tctx, "SourceData(%d) failed", len));
131 for (i=0;i<len;i++) {
132 uint8_t *v = (uint8_t *)r.out.data;
133 torture_assert(tctx, v[i] == (i & 0xFF),
134 talloc_asprintf(tctx,
135 "bad data 0x%x at %d\n", (uint8_t)r.out.data[i], i));
141 test the SinkData interface
143 static bool test_sinkdata(struct torture_context *tctx,
144 struct dcerpc_pipe *p)
149 struct echo_SinkData r;
150 struct dcerpc_binding_handle *b = p->binding_handle;
152 if (torture_setting_bool(tctx, "quick", false) &&
153 (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
154 len = 100 + (random() % 5000);
156 len = 200000 + (random() % 5000);
159 data_in = talloc_array(tctx, uint8_t, len);
160 for (i=0;i<len;i++) {
167 torture_assert_ntstatus_ok(tctx, dcerpc_echo_SinkData_r(b, tctx, &r),
168 talloc_asprintf(tctx, "SinkData(%d) failed", len));
170 torture_comment(tctx, "sunk %d bytes\n", len);
176 test the testcall interface
178 static bool test_testcall(struct torture_context *tctx,
179 struct dcerpc_pipe *p)
181 struct echo_TestCall r;
182 const char *s = NULL;
183 struct dcerpc_binding_handle *b = p->binding_handle;
185 r.in.s1 = "input string";
188 torture_assert_ntstatus_ok(tctx, dcerpc_echo_TestCall_r(b, tctx, &r),
191 torture_assert_str_equal(tctx, s, "input string", "Didn't receive back same string");
197 test the testcall interface
199 static bool test_testcall2(struct torture_context *tctx,
200 struct dcerpc_pipe *p)
202 struct echo_TestCall2 r;
204 struct dcerpc_binding_handle *b = p->binding_handle;
208 r.out.info = talloc(tctx, union echo_Info);
210 torture_comment(tctx, "Testing TestCall2 level %d\n", i);
211 torture_assert_ntstatus_ok(tctx, dcerpc_echo_TestCall2_r(b, tctx, &r),
213 torture_assert_ntstatus_ok(tctx, r.out.result, "TestCall2 failed");
218 static void test_sleep_done(struct tevent_req *subreq)
220 bool *done1 = (bool *)tevent_req_callback_data_void(subreq);
225 test the TestSleep interface
227 static bool test_sleep(struct torture_context *tctx,
228 struct dcerpc_pipe *p)
231 #define ASYNC_COUNT 3
232 struct tevent_req *req[ASYNC_COUNT];
233 struct echo_TestSleep r[ASYNC_COUNT];
234 bool done1[ASYNC_COUNT];
235 bool done2[ASYNC_COUNT];
236 struct timeval snd[ASYNC_COUNT];
237 struct timeval rcv[ASYNC_COUNT];
238 struct timeval diff[ASYNC_COUNT];
240 struct dcerpc_binding_handle *b = p->binding_handle;
241 enum dcerpc_transport_t transport;
242 uint32_t assoc_group_id;
243 struct dcerpc_pipe *p2 = NULL;
246 if (torture_setting_bool(tctx, "quick", false)) {
247 torture_skip(tctx, "TestSleep disabled - use \"torture:quick=no\" to enable\n");
249 torture_comment(tctx, "Testing TestSleep - use \"torture:quick=yes\" to disable\n");
251 transport = dcerpc_binding_get_transport(p->binding);
252 assoc_group_id = dcerpc_binding_get_assoc_group_id(p->binding);
254 torture_comment(tctx, "connect echo connection 2 with "
255 "DCERPC_CONCURRENT_MULTIPLEX\n");
256 status = torture_rpc_connection_transport(tctx, &p2,
260 DCERPC_CONCURRENT_MULTIPLEX);
261 torture_assert_ntstatus_ok(tctx, status, "opening echo connection 2");
262 b = p2->binding_handle;
264 for (i=0;i<ASYNC_COUNT;i++) {
267 snd[i] = timeval_current();
268 rcv[i] = timeval_zero();
269 r[i].in.seconds = ASYNC_COUNT-i;
270 req[i] = dcerpc_echo_TestSleep_r_send(tctx, tctx->ev, b, &r[i]);
271 torture_assert(tctx, req[i], "Failed to send async sleep request\n");
272 tevent_req_set_callback(req[i], test_sleep_done, &done1[i]);
275 while (total_done < ASYNC_COUNT) {
276 torture_assert(tctx, tevent_loop_once(tctx->ev) == 0,
277 "Event context loop failed");
278 for (i=0;i<ASYNC_COUNT;i++) {
279 if (done2[i] == false && done1[i] == true) {
283 rcv[i] = timeval_current();
284 diff[i] = timeval_until(&snd[i], &rcv[i]);
285 rounded_tdiff = (int)(0.5 + diff[i].tv_sec + (1.0e-6*diff[i].tv_usec));
286 torture_comment(tctx, "rounded_tdiff=%d\n", rounded_tdiff);
287 torture_assert_ntstatus_ok(tctx,
288 dcerpc_echo_TestSleep_r_recv(req[i], tctx),
289 talloc_asprintf(tctx, "TestSleep(%d) failed", i));
290 torture_assert(tctx, r[i].out.result == r[i].in.seconds,
291 talloc_asprintf(tctx, "Failed - Asked to sleep for %u seconds (server replied with %u seconds and the reply takes only %u seconds)",
292 r[i].out.result, r[i].in.seconds, (unsigned int)diff[i].tv_sec));
293 torture_assert(tctx, r[i].out.result <= rounded_tdiff,
294 talloc_asprintf(tctx, "Failed - Slept for %u seconds (but reply takes only %u.%06u seconds)",
295 r[i].out.result, (unsigned int)diff[i].tv_sec, (unsigned int)diff[i].tv_usec));
296 if (r[i].out.result+1 == rounded_tdiff) {
297 torture_comment(tctx, "Slept for %u seconds (but reply takes %u.%06u seconds - busy server?)\n",
298 r[i].out.result, (unsigned int)diff[i].tv_sec, (unsigned int)diff[i].tv_usec);
299 } else if (r[i].out.result == rounded_tdiff) {
300 torture_comment(tctx, "Slept for %u seconds (reply takes %u.%06u seconds - ok)\n",
301 r[i].out.result, (unsigned int)diff[i].tv_sec, (unsigned int)diff[i].tv_usec);
303 torture_fail(tctx, talloc_asprintf(tctx,
304 "(Failed) - Not async - Slept for %u seconds (but reply takes %u.%06u seconds)\n",
305 r[i].out.result, (unsigned int)diff[i].tv_sec, (unsigned int)diff[i].tv_usec));
310 torture_comment(tctx, "\n");
317 static bool test_enum(struct torture_context *tctx,
318 struct dcerpc_pipe *p)
320 struct echo_TestEnum r;
321 enum echo_Enum1 v = ECHO_ENUM1;
322 struct echo_Enum2 e2;
324 struct dcerpc_binding_handle *b = p->binding_handle;
334 e2.e2 = ECHO_ENUM1_32;
337 torture_assert_ntstatus_ok(tctx, dcerpc_echo_TestEnum_r(b, tctx, &r),
343 test surrounding conformant array handling
345 static bool test_surrounding(struct torture_context *tctx,
346 struct dcerpc_pipe *p)
348 struct echo_TestSurrounding r;
349 struct dcerpc_binding_handle *b = p->binding_handle;
352 r.in.data = talloc(tctx, struct echo_Surrounding);
355 r.in.data->surrounding = talloc_zero_array(tctx, uint16_t, r.in.data->x);
357 r.out.data = talloc(tctx, struct echo_Surrounding);
359 torture_assert_ntstatus_ok(tctx, dcerpc_echo_TestSurrounding_r(b, tctx, &r),
360 "TestSurrounding failed");
362 torture_assert(tctx, r.out.data->x == 2 * r.in.data->x,
363 "TestSurrounding did not make the array twice as large");
369 test multiple levels of pointers
371 static bool test_doublepointer(struct torture_context *tctx,
372 struct dcerpc_pipe *p)
374 struct echo_TestDoublePointer r;
376 uint16_t *pvalue = &value;
377 uint16_t **ppvalue = &pvalue;
378 struct dcerpc_binding_handle *b = p->binding_handle;
381 r.in.data = &ppvalue;
383 torture_assert_ntstatus_ok(tctx, dcerpc_echo_TestDoublePointer_r(b, tctx, &r),
384 "TestDoublePointer failed");
386 torture_assert_int_equal(tctx, value, r.out.result,
387 "TestDoublePointer did not return original value");
391 static bool test_bytepipein(struct torture_context *tctx,
392 struct dcerpc_pipe *p)
394 struct dcerpc_binding_handle *b = p->binding_handle;
395 struct echo_TestBytePipeIn r;
396 struct echo_BytePipe_chunk chunk;
397 struct echo_BytePipe *in_pipe;
398 struct tevent_req *subreq1;
399 struct tevent_req *subreq2;
401 uint8_t bytes[0x8000];
404 in_pipe = dcerpc_echo_BytePipe_create(tctx);
408 r.in.chunk_size = sizeof(bytes);
409 r.in.in_pipe = in_pipe;
413 subreq1 = dcerpc_echo_TestBytePipeIn_r_send(tctx, tctx->ev, b, &r);
414 for (i=0; i <= r.in.num_chunks; i++) {
415 struct timeval tv = timeval_current_ofs(0, 100);
417 memset(bytes, i, sizeof(bytes));
418 chunk.count = r.in.chunk_size;
421 if (i == r.in.num_chunks) {
425 subreq2 = tevent_wakeup_send(tctx, tctx->ev, tv);
426 tevent_req_poll(subreq2, tctx->ev);
427 tevent_wakeup_recv(subreq2);
428 TALLOC_FREE(subreq2);
430 torture_comment(tctx, "chunk[%u] count[%u]\n", (unsigned)i, (unsigned)chunk.count);
431 subreq2 = dcerpc_echo_BytePipe_chunk_push_send(tctx, tctx->ev, in_pipe, &chunk);
432 tevent_req_poll(subreq2, tctx->ev);
433 status = dcerpc_echo_BytePipe_chunk_push_recv(subreq2);
434 TALLOC_FREE(subreq2);
435 torture_assert_ntstatus_ok(tctx, status, __location__);
438 tevent_req_poll(subreq1, tctx->ev);
439 status = dcerpc_echo_TestBytePipeIn_r_recv(subreq1, tctx);
440 TALLOC_FREE(subreq1);
441 torture_assert_ntstatus_ok(tctx, status, __location__);
446 static bool test_bytepipeout(struct torture_context *tctx,
447 struct dcerpc_pipe *p)
449 struct dcerpc_binding_handle *b = p->binding_handle;
450 struct echo_TestBytePipeOut r;
451 struct echo_BytePipe_chunk *chunk;
452 struct echo_BytePipe *out_pipe;
453 struct tevent_req *subreq1;
454 struct tevent_req *subreq2;
459 out_pipe = dcerpc_echo_BytePipe_create(tctx);
463 r.in.chunk_size = sizeof(bytes);
464 r.out.out_pipe = out_pipe;
468 subreq1 = dcerpc_echo_TestBytePipeOut_r_send(tctx, tctx->ev, b, &r);
470 struct timeval tv = timeval_current_ofs(0, 100);
472 //subreq2 = tevent_wakeup_send(tctx, tctx->ev, tv);
473 //tevent_req_poll(subreq2, tctx->ev);
474 //tevent_wakeup_recv(subreq2);
475 //TALLOC_FREE(subreq2);
477 subreq2 = dcerpc_echo_BytePipe_chunk_pull_send(tctx, tctx->ev, out_pipe);
478 tevent_req_poll(subreq2, tctx->ev);
479 status = dcerpc_echo_BytePipe_chunk_pull_recv(subreq2, tctx, &chunk);
480 TALLOC_FREE(subreq2);
481 torture_assert_ntstatus_ok(tctx, status, __location__);
482 torture_comment(tctx, "chunk[%u] count[%u]\n", (unsigned)i, (unsigned)chunk->count);
483 if (chunk->count == 0) {
490 tevent_req_poll(subreq1, tctx->ev);
491 status = dcerpc_echo_TestBytePipeOut_r_recv(subreq1, tctx);
492 TALLOC_FREE(subreq1);
493 torture_assert_ntstatus_ok(tctx, status, __location__);
498 test request timeouts
500 #if 0 /* this test needs fixing to work over ncacn_np */
501 static bool test_timeout(struct torture_context *tctx,
502 struct dcerpc_pipe *p)
505 struct rpc_request *req;
506 struct echo_TestSleep r;
507 int timeout_saved = p->request_timeout;
509 if (torture_setting_bool(tctx, "quick", false)) {
510 torture_skip(tctx, "timeout testing disabled - use \"torture:quick=no\" to enable\n");
513 torture_comment(tctx, "testing request timeouts\n");
515 p->request_timeout = 1;
517 req = dcerpc_echo_TestSleep_send(p, tctx, &r);
519 torture_comment(tctx, "Failed to send async sleep request\n");
522 req->ignore_timeout = true;
524 status = dcerpc_echo_TestSleep_recv(req);
525 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_IO_TIMEOUT,
526 "request should have timed out");
528 torture_comment(tctx, "testing request destruction\n");
529 req = dcerpc_echo_TestSleep_send(p, tctx, &r);
531 torture_comment(tctx, "Failed to send async sleep request\n");
536 req = dcerpc_echo_TestSleep_send(p, tctx, &r);
538 torture_comment(tctx, "Failed to send async sleep request\n");
541 req->ignore_timeout = true;
542 status = dcerpc_echo_TestSleep_recv(req);
543 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_IO_TIMEOUT,
544 "request should have timed out");
546 p->request_timeout = timeout_saved;
548 return test_addone(tctx, p);
551 p->request_timeout = timeout_saved;
556 struct torture_suite *torture_rpc_echo(TALLOC_CTX *mem_ctx)
558 struct torture_suite *suite = torture_suite_create(mem_ctx, "echo");
559 struct torture_rpc_tcase *tcase;
561 tcase = torture_suite_add_rpc_iface_tcase(suite, "echo",
564 //torture_rpc_tcase_add_test(tcase, "addone", test_addone);
565 //torture_rpc_tcase_add_test(tcase, "sinkdata", test_sinkdata);
566 //torture_rpc_tcase_add_test(tcase, "echodata", test_echodata);
567 //torture_rpc_tcase_add_test(tcase, "sourcedata", test_sourcedata);
568 //torture_rpc_tcase_add_test(tcase, "testcall", test_testcall);
569 //torture_rpc_tcase_add_test(tcase, "testcall2", test_testcall2);
570 //torture_rpc_tcase_add_test(tcase, "enum", test_enum);
571 //torture_rpc_tcase_add_test(tcase, "surrounding", test_surrounding);
572 //torture_rpc_tcase_add_test(tcase, "doublepointer", test_doublepointer);
573 //torture_rpc_tcase_add_test(tcase, "sleep", test_sleep);
574 torture_rpc_tcase_add_test(tcase, "bytepipein", test_bytepipein);
575 torture_rpc_tcase_add_test(tcase, "bytepipeout", test_bytepipeout);
576 #if 0 /* this test needs fixing to work over ncacn_np */
577 torture_rpc_tcase_add_test(tcase, "timeout", test_timeout);