s4-smbtorture: disable winreg QueryValue test for today.
[Samba/nascimento.git] / source4 / torture / rpc / echo.c
blobe0ee8a580e2e4933ce65ef50f961a34d3b089ed5
1 /*
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/>.
23 #include "includes.h"
24 #include "torture/rpc/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 { \
33 n = i = value; \
34 r.in.in_data = n; \
35 r.out.out_data = &n; \
36 status = dcerpc_echo_AddOne(p, tctx, &r); \
37 torture_assert_ntstatus_ok(tctx, status, 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); \
40 } while(0)
42 static bool test_addone(struct torture_context *tctx,
43 struct dcerpc_pipe *p)
45 uint32_t i;
46 NTSTATUS status;
47 uint32_t n;
48 struct echo_AddOne r;
50 for (i=0;i<10;i++) {
51 TEST_ADDONE(tctx, i);
54 TEST_ADDONE(tctx, 0x7FFFFFFE);
55 TEST_ADDONE(tctx, 0xFFFFFFFE);
56 TEST_ADDONE(tctx, 0xFFFFFFFF);
57 TEST_ADDONE(tctx, random() & 0xFFFFFFFF);
58 return true;
62 test the EchoData interface
64 static bool test_echodata(struct torture_context *tctx,
65 struct dcerpc_pipe *p)
67 int i;
68 NTSTATUS status;
69 uint8_t *data_in, *data_out;
70 int len;
71 struct echo_EchoData r;
73 if (torture_setting_bool(tctx, "quick", false) &&
74 (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
75 len = 1 + (random() % 500);
76 } else {
77 len = 1 + (random() % 5000);
80 data_in = talloc_array(tctx, uint8_t, len);
81 data_out = talloc_array(tctx, uint8_t, len);
82 for (i=0;i<len;i++) {
83 data_in[i] = i;
86 r.in.len = len;
87 r.in.in_data = data_in;
89 status = dcerpc_echo_EchoData(p, tctx, &r);
90 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx,
91 "EchoData(%d) failed\n", len));
93 data_out = r.out.out_data;
95 for (i=0;i<len;i++) {
96 if (data_in[i] != data_out[i]) {
97 torture_comment(tctx, "Bad data returned for len %d at offset %d\n",
98 len, i);
99 torture_comment(tctx, "in:\n");
100 dump_data(0, data_in+i, MIN(len-i, 16));
101 torture_comment(tctx, "out:\n");
102 dump_data(0, data_out+i, MIN(len-1, 16));
103 return false;
106 return true;
110 test the SourceData interface
112 static bool test_sourcedata(struct torture_context *tctx,
113 struct dcerpc_pipe *p)
115 int i;
116 NTSTATUS status;
117 int len;
118 struct echo_SourceData r;
120 if (torture_setting_bool(tctx, "quick", false) &&
121 (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
122 len = 100 + (random() % 500);
123 } else {
124 len = 200000 + (random() % 5000);
127 r.in.len = len;
129 status = dcerpc_echo_SourceData(p, tctx, &r);
130 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx,
131 "SourceData(%d) failed", len));
133 for (i=0;i<len;i++) {
134 uint8_t *v = (uint8_t *)r.out.data;
135 torture_assert(tctx, v[i] == (i & 0xFF),
136 talloc_asprintf(tctx,
137 "bad data 0x%x at %d\n", (uint8_t)r.out.data[i], i));
139 return true;
143 test the SinkData interface
145 static bool test_sinkdata(struct torture_context *tctx,
146 struct dcerpc_pipe *p)
148 int i;
149 NTSTATUS status;
150 uint8_t *data_in;
151 int len;
152 struct echo_SinkData r;
154 if (torture_setting_bool(tctx, "quick", false) &&
155 (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
156 len = 100 + (random() % 5000);
157 } else {
158 len = 200000 + (random() % 5000);
161 data_in = talloc_array(tctx, uint8_t, len);
162 for (i=0;i<len;i++) {
163 data_in[i] = i+1;
166 r.in.len = len;
167 r.in.data = data_in;
169 status = dcerpc_echo_SinkData(p, tctx, &r);
170 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx,
171 "SinkData(%d) failed",
172 len));
174 torture_comment(tctx, "sunk %d bytes\n", len);
175 return true;
180 test the testcall interface
182 static bool test_testcall(struct torture_context *tctx,
183 struct dcerpc_pipe *p)
185 NTSTATUS status;
186 struct echo_TestCall r;
187 const char *s = NULL;
189 r.in.s1 = "input string";
190 r.out.s2 = &s;
192 status = dcerpc_echo_TestCall(p, tctx, &r);
193 torture_assert_ntstatus_ok(tctx, status, "TestCall failed");
195 torture_assert_str_equal(tctx, s, "input string", "Didn't receive back same string");
197 return true;
201 test the testcall interface
203 static bool test_testcall2(struct torture_context *tctx,
204 struct dcerpc_pipe *p)
206 NTSTATUS status;
207 struct echo_TestCall2 r;
208 int i;
210 for (i=1;i<=7;i++) {
211 r.in.level = i;
212 r.out.info = talloc(tctx, union echo_Info);
214 torture_comment(tctx, "Testing TestCall2 level %d\n", i);
215 status = dcerpc_echo_TestCall2(p, tctx, &r);
216 torture_assert_ntstatus_ok(tctx, status, "TestCall2 failed");
218 return true;
221 static void test_sleep_done(struct rpc_request *rreq)
223 bool *done1 = (bool *)rreq->async.private_data;
224 *done1 = true;
228 test the TestSleep interface
230 static bool test_sleep(struct torture_context *tctx,
231 struct dcerpc_pipe *p)
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 done1[ASYNC_COUNT];
239 bool done2[ASYNC_COUNT];
240 struct timeval snd[ASYNC_COUNT];
241 struct timeval rcv[ASYNC_COUNT];
242 struct timeval diff[ASYNC_COUNT];
243 struct tevent_context *ctx;
244 int total_done = 0;
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 for (i=0;i<ASYNC_COUNT;i++) {
252 done1[i] = false;
253 done2[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, tctx, &r[i]);
258 torture_assert(tctx, req[i], "Failed to send async sleep request\n");
259 req[i]->async.callback = test_sleep_done;
260 req[i]->async.private_data = &done1[i];
263 ctx = dcerpc_event_context(p);
264 while (total_done < ASYNC_COUNT) {
265 torture_assert(tctx, event_loop_once(ctx) == 0,
266 "Event context loop failed");
267 for (i=0;i<ASYNC_COUNT;i++) {
268 if (done2[i] == false && done1[i] == true) {
269 int rounded_tdiff;
270 total_done++;
271 done2[i] = true;
272 rcv[i] = timeval_current();
273 diff[i] = timeval_until(&snd[i], &rcv[i]);
274 rounded_tdiff = (int)(0.5 + diff[i].tv_sec + (1.0e-6*diff[i].tv_usec));
275 status = dcerpc_echo_TestSleep_recv(req[i]);
276 torture_comment(tctx, "rounded_tdiff=%d\n", rounded_tdiff);
277 torture_assert_ntstatus_ok(tctx, status,
278 talloc_asprintf(tctx, "TestSleep(%d) failed", i));
279 torture_assert(tctx, r[i].out.result == r[i].in.seconds,
280 talloc_asprintf(tctx, "Failed - Asked to sleep for %u seconds (server replied with %u seconds and the reply takes only %u seconds)",
281 r[i].out.result, r[i].in.seconds, (unsigned int)diff[i].tv_sec));
282 torture_assert(tctx, r[i].out.result <= rounded_tdiff,
283 talloc_asprintf(tctx, "Failed - Slept for %u seconds (but reply takes only %u.%06u seconds)",
284 r[i].out.result, (unsigned int)diff[i].tv_sec, (unsigned int)diff[i].tv_usec));
285 if (r[i].out.result+1 == rounded_tdiff) {
286 torture_comment(tctx, "Slept for %u seconds (but reply takes %u.%06u seconds - busy server?)\n",
287 r[i].out.result, (unsigned int)diff[i].tv_sec, (unsigned int)diff[i].tv_usec);
288 } else if (r[i].out.result == rounded_tdiff) {
289 torture_comment(tctx, "Slept for %u seconds (reply takes %u.%06u seconds - ok)\n",
290 r[i].out.result, (unsigned int)diff[i].tv_sec, (unsigned int)diff[i].tv_usec);
291 } else {
292 torture_fail(tctx, talloc_asprintf(tctx,
293 "(Failed) - Not async - Slept for %u seconds (but reply takes %u.%06u seconds)\n",
294 r[i].out.result, (unsigned int)diff[i].tv_sec, (unsigned int)diff[i].tv_usec));
299 torture_comment(tctx, "\n");
300 return true;
304 test enum handling
306 static bool test_enum(struct torture_context *tctx,
307 struct dcerpc_pipe *p)
309 NTSTATUS status;
310 struct echo_TestEnum r;
311 enum echo_Enum1 v = ECHO_ENUM1;
312 struct echo_Enum2 e2;
313 union echo_Enum3 e3;
315 r.in.foo1 = &v;
316 r.in.foo2 = &e2;
317 r.in.foo3 = &e3;
318 r.out.foo1 = &v;
319 r.out.foo2 = &e2;
320 r.out.foo3 = &e3;
322 e2.e1 = 76;
323 e2.e2 = ECHO_ENUM1_32;
324 e3.e1 = ECHO_ENUM2;
326 status = dcerpc_echo_TestEnum(p, tctx, &r);
327 torture_assert_ntstatus_ok(tctx, status, "TestEnum failed");
328 return true;
332 test surrounding conformant array handling
334 static bool test_surrounding(struct torture_context *tctx,
335 struct dcerpc_pipe *p)
337 NTSTATUS status;
338 struct echo_TestSurrounding r;
340 ZERO_STRUCT(r);
341 r.in.data = talloc(tctx, struct echo_Surrounding);
343 r.in.data->x = 20;
344 r.in.data->surrounding = talloc_zero_array(tctx, uint16_t, r.in.data->x);
346 r.out.data = talloc(tctx, struct echo_Surrounding);
348 status = dcerpc_echo_TestSurrounding(p, tctx, &r);
349 torture_assert_ntstatus_ok(tctx, status, "TestSurrounding failed");
351 torture_assert(tctx, r.out.data->x == 2 * r.in.data->x,
352 "TestSurrounding did not make the array twice as large");
354 return true;
358 test multiple levels of pointers
360 static bool test_doublepointer(struct torture_context *tctx,
361 struct dcerpc_pipe *p)
363 NTSTATUS status;
364 struct echo_TestDoublePointer r;
365 uint16_t value = 12;
366 uint16_t *pvalue = &value;
367 uint16_t **ppvalue = &pvalue;
369 ZERO_STRUCT(r);
370 r.in.data = &ppvalue;
372 status = dcerpc_echo_TestDoublePointer(p, tctx, &r);
373 torture_assert_ntstatus_ok(tctx, status, "TestDoublePointer failed");
375 torture_assert_int_equal(tctx, value, r.out.result,
376 "TestDoublePointer did not return original value");
377 return true;
382 test request timeouts
384 #if 0 /* this test needs fixing to work over ncacn_np */
385 static bool test_timeout(struct torture_context *tctx,
386 struct dcerpc_pipe *p)
388 NTSTATUS status;
389 struct rpc_request *req;
390 struct echo_TestSleep r;
391 int timeout_saved = p->request_timeout;
393 if (torture_setting_bool(tctx, "quick", false)) {
394 torture_skip(tctx, "timeout testing disabled - use \"torture:quick=no\" to enable\n");
397 torture_comment(tctx, "testing request timeouts\n");
398 r.in.seconds = 2;
399 p->request_timeout = 1;
401 req = dcerpc_echo_TestSleep_send(p, tctx, &r);
402 if (!req) {
403 torture_comment(tctx, "Failed to send async sleep request\n");
404 goto failed;
406 req->ignore_timeout = true;
408 status = dcerpc_echo_TestSleep_recv(req);
409 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_IO_TIMEOUT,
410 "request should have timed out");
412 torture_comment(tctx, "testing request destruction\n");
413 req = dcerpc_echo_TestSleep_send(p, tctx, &r);
414 if (!req) {
415 torture_comment(tctx, "Failed to send async sleep request\n");
416 goto failed;
418 talloc_free(req);
420 req = dcerpc_echo_TestSleep_send(p, tctx, &r);
421 if (!req) {
422 torture_comment(tctx, "Failed to send async sleep request\n");
423 goto failed;
425 req->ignore_timeout = true;
426 status = dcerpc_echo_TestSleep_recv(req);
427 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_IO_TIMEOUT,
428 "request should have timed out");
430 p->request_timeout = timeout_saved;
432 return test_addone(tctx, p);
434 failed:
435 p->request_timeout = timeout_saved;
436 return false;
438 #endif
440 struct torture_suite *torture_rpc_echo(TALLOC_CTX *mem_ctx)
442 struct torture_suite *suite = torture_suite_create(
443 mem_ctx, "ECHO");
444 struct torture_rpc_tcase *tcase;
446 tcase = torture_suite_add_rpc_iface_tcase(suite, "echo",
447 &ndr_table_rpcecho);
449 torture_rpc_tcase_add_test(tcase, "addone", test_addone);
450 torture_rpc_tcase_add_test(tcase, "sinkdata", test_sinkdata);
451 torture_rpc_tcase_add_test(tcase, "echodata", test_echodata);
452 torture_rpc_tcase_add_test(tcase, "sourcedata", test_sourcedata);
453 torture_rpc_tcase_add_test(tcase, "testcall", test_testcall);
454 torture_rpc_tcase_add_test(tcase, "testcall2", test_testcall2);
455 torture_rpc_tcase_add_test(tcase, "enum", test_enum);
456 torture_rpc_tcase_add_test(tcase, "surrounding", test_surrounding);
457 torture_rpc_tcase_add_test(tcase, "doublepointer", test_doublepointer);
458 torture_rpc_tcase_add_test(tcase, "sleep", test_sleep);
459 #if 0 /* this test needs fixing to work over ncacn_np */
460 torture_rpc_tcase_add_test(tcase, "timeout", test_timeout);
461 #endif
463 return suite;