Merge branch 'master' of ctdb into 'master' of samba
[metze/samba/wip.git] / source4 / torture / rpc / echo.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for echo rpc operations
4
5    Copyright (C) Andrew Tridgell 2003
6    Copyright (C) Stefan (metze) Metzmacher 2005
7    Copyright (C) Jelmer Vernooij 2005
8    
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.
13    
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.
18    
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/>.
21 */
22
23 #include "includes.h"
24 #include "torture/rpc/torture_rpc.h"
25 #include "lib/events/events.h"
26 #include "librpc/gen_ndr/ndr_echo_c.h"
27
28
29 /*
30   test the AddOne interface
31 */
32 #define TEST_ADDONE(tctx, value) do { \
33         n = i = value; \
34         r.in.in_data = n; \
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); \
40 } while(0)
41
42 static bool test_addone(struct torture_context *tctx, 
43                         struct dcerpc_pipe *p)
44 {
45         uint32_t i;
46         uint32_t n;
47         struct echo_AddOne r;
48         struct dcerpc_binding_handle *b = p->binding_handle;
49
50         for (i=0;i<10;i++) {
51                 TEST_ADDONE(tctx, i);
52         }
53
54         TEST_ADDONE(tctx, 0x7FFFFFFE);
55         TEST_ADDONE(tctx, 0xFFFFFFFE);
56         TEST_ADDONE(tctx, 0xFFFFFFFF);
57         TEST_ADDONE(tctx, random() & 0xFFFFFFFF);
58         return true;
59 }
60
61 /*
62   test the EchoData interface
63 */
64 static bool test_echodata(struct torture_context *tctx,
65                           struct dcerpc_pipe *p)
66 {
67         int i;
68         uint8_t *data_in, *data_out;
69         int len;
70         struct echo_EchoData r;
71         struct dcerpc_binding_handle *b = p->binding_handle;
72
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);
78         }
79
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;
84         }
85         
86         r.in.len = len;
87         r.in.in_data = data_in;
88
89         torture_assert_ntstatus_ok(tctx, dcerpc_echo_EchoData_r(b, tctx, &r),
90                 talloc_asprintf(tctx, "EchoData(%d) failed\n", len));
91
92         data_out = r.out.out_data;
93
94         for (i=0;i<len;i++) {
95                 if (data_in[i] != data_out[i]) {
96                         torture_comment(tctx, "Bad data returned for len %d at offset %d\n", 
97                                len, i);
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));
102                         return false;
103                 }
104         }
105         return true;
106 }
107
108 /*
109   test the SourceData interface
110 */
111 static bool test_sourcedata(struct torture_context *tctx,
112                             struct dcerpc_pipe *p)
113 {
114         int i;
115         int len;
116         struct echo_SourceData r;
117         struct dcerpc_binding_handle *b = p->binding_handle;
118
119         if (torture_setting_bool(tctx, "quick", false) &&
120             (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
121                 len = 100 + (random() % 500);
122         } else {
123                 len = 200000 + (random() % 5000);
124         }
125
126         r.in.len = len;
127
128         torture_assert_ntstatus_ok(tctx, dcerpc_echo_SourceData_r(b, tctx, &r),
129                 talloc_asprintf(tctx, "SourceData(%d) failed", len));
130
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));
136         }
137         return true;
138 }
139
140 /*
141   test the SinkData interface
142 */
143 static bool test_sinkdata(struct torture_context *tctx, 
144                           struct dcerpc_pipe *p)
145 {
146         int i;
147         uint8_t *data_in;
148         int len;
149         struct echo_SinkData r;
150         struct dcerpc_binding_handle *b = p->binding_handle;
151
152         if (torture_setting_bool(tctx, "quick", false) &&
153             (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
154                 len = 100 + (random() % 5000);
155         } else {
156                 len = 200000 + (random() % 5000);
157         }
158
159         data_in = talloc_array(tctx, uint8_t, len);
160         for (i=0;i<len;i++) {
161                 data_in[i] = i+1;
162         }
163
164         r.in.len = len;
165         r.in.data = data_in;
166
167         torture_assert_ntstatus_ok(tctx, dcerpc_echo_SinkData_r(b, tctx, &r),
168                 talloc_asprintf(tctx, "SinkData(%d) failed", len));
169
170         torture_comment(tctx, "sunk %d bytes\n", len);
171         return true;
172 }
173
174
175 /*
176   test the testcall interface
177 */
178 static bool test_testcall(struct torture_context *tctx,
179                           struct dcerpc_pipe *p)
180 {
181         struct echo_TestCall r;
182         const char *s = NULL;
183         struct dcerpc_binding_handle *b = p->binding_handle;
184
185         r.in.s1 = "input string";
186         r.out.s2 = &s;
187
188         torture_assert_ntstatus_ok(tctx, dcerpc_echo_TestCall_r(b, tctx, &r),
189                 "TestCall failed");
190
191         torture_assert_str_equal(tctx, s, "input string", "Didn't receive back same string");
192
193         return true;
194 }
195
196 /*
197   test the testcall interface
198 */
199 static bool test_testcall2(struct torture_context *tctx,
200                            struct dcerpc_pipe *p)
201 {
202         struct echo_TestCall2 r;
203         int i;
204         struct dcerpc_binding_handle *b = p->binding_handle;
205
206         for (i=1;i<=7;i++) {
207                 r.in.level = i;
208                 r.out.info = talloc(tctx, union echo_Info);
209
210                 torture_comment(tctx, "Testing TestCall2 level %d\n", i);
211                 torture_assert_ntstatus_ok(tctx, dcerpc_echo_TestCall2_r(b, tctx, &r),
212                         "TestCall2 failed");
213                 torture_assert_ntstatus_ok(tctx, r.out.result, "TestCall2 failed");
214         }
215         return true;
216 }
217
218 static void test_sleep_done(struct tevent_req *subreq)
219 {
220         bool *done1 = (bool *)tevent_req_callback_data_void(subreq);
221         *done1 = true;
222 }
223
224 /*
225   test the TestSleep interface
226 */
227 static bool test_sleep(struct torture_context *tctx,
228                        struct dcerpc_pipe *p)
229 {
230         int i;
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];
239         struct tevent_context *ctx;
240         int total_done = 0;
241         struct dcerpc_binding_handle *b = p->binding_handle;
242
243         if (torture_setting_bool(tctx, "quick", false)) {
244                 torture_skip(tctx, "TestSleep disabled - use \"torture:quick=no\" to enable\n");
245         }
246         torture_comment(tctx, "Testing TestSleep - use \"torture:quick=yes\" to disable\n");
247
248         for (i=0;i<ASYNC_COUNT;i++) {
249                 done1[i]        = false;
250                 done2[i]        = false;
251                 snd[i]          = timeval_current();
252                 rcv[i]          = timeval_zero();
253                 r[i].in.seconds = ASYNC_COUNT-i;
254                 req[i] = dcerpc_echo_TestSleep_r_send(tctx, tctx->ev, b, &r[i]);
255                 torture_assert(tctx, req[i], "Failed to send async sleep request\n");
256                 tevent_req_set_callback(req[i], test_sleep_done, &done1[i]);
257         }
258
259         ctx = dcerpc_event_context(p);
260         while (total_done < ASYNC_COUNT) {
261                 torture_assert(tctx, tevent_loop_once(ctx) == 0, 
262                                            "Event context loop failed");
263                 for (i=0;i<ASYNC_COUNT;i++) {
264                         if (done2[i] == false && done1[i] == true) {
265                                 int rounded_tdiff;
266                                 total_done++;
267                                 done2[i] = true;
268                                 rcv[i]  = timeval_current();
269                                 diff[i] = timeval_until(&snd[i], &rcv[i]);
270                                 rounded_tdiff = (int)(0.5 + diff[i].tv_sec + (1.0e-6*diff[i].tv_usec));
271                                 torture_comment(tctx, "rounded_tdiff=%d\n", rounded_tdiff);
272                                 torture_assert_ntstatus_ok(tctx,
273                                         dcerpc_echo_TestSleep_r_recv(req[i], tctx),
274                                         talloc_asprintf(tctx, "TestSleep(%d) failed", i));
275                                 torture_assert(tctx, r[i].out.result == r[i].in.seconds,
276                                         talloc_asprintf(tctx, "Failed - Asked to sleep for %u seconds (server replied with %u seconds and the reply takes only %u seconds)", 
277                                                 r[i].out.result, r[i].in.seconds, (unsigned int)diff[i].tv_sec));
278                                 torture_assert(tctx, r[i].out.result <= rounded_tdiff, 
279                                         talloc_asprintf(tctx, "Failed - Slept for %u seconds (but reply takes only %u.%06u seconds)", 
280                                                 r[i].out.result, (unsigned int)diff[i].tv_sec, (unsigned int)diff[i].tv_usec));
281                                 if (r[i].out.result+1 == rounded_tdiff) {
282                                         torture_comment(tctx, "Slept for %u seconds (but reply takes %u.%06u seconds - busy server?)\n", 
283                                                         r[i].out.result, (unsigned int)diff[i].tv_sec, (unsigned int)diff[i].tv_usec);
284                                 } else if (r[i].out.result == rounded_tdiff) {
285                                         torture_comment(tctx, "Slept for %u seconds (reply takes %u.%06u seconds - ok)\n", 
286                                                         r[i].out.result, (unsigned int)diff[i].tv_sec, (unsigned int)diff[i].tv_usec);
287                                 } else {
288                                         torture_fail(tctx, talloc_asprintf(tctx,
289                                                      "(Failed) - Not async - Slept for %u seconds (but reply takes %u.%06u seconds)\n",
290                                                      r[i].out.result, (unsigned int)diff[i].tv_sec, (unsigned int)diff[i].tv_usec));
291                                 }
292                         }
293                 }
294         }
295         torture_comment(tctx, "\n");
296         return true;
297 }
298
299 /*
300   test enum handling
301 */
302 static bool test_enum(struct torture_context *tctx,
303                                                   struct dcerpc_pipe *p)
304 {
305         struct echo_TestEnum r;
306         enum echo_Enum1 v = ECHO_ENUM1;
307         struct echo_Enum2 e2;
308         union echo_Enum3 e3;
309         struct dcerpc_binding_handle *b = p->binding_handle;
310
311         r.in.foo1 = &v;
312         r.in.foo2 = &e2;
313         r.in.foo3 = &e3;
314         r.out.foo1 = &v;
315         r.out.foo2 = &e2;
316         r.out.foo3 = &e3;
317
318         e2.e1 = 76;
319         e2.e2 = ECHO_ENUM1_32;
320         e3.e1 = ECHO_ENUM2;
321
322         torture_assert_ntstatus_ok(tctx, dcerpc_echo_TestEnum_r(b, tctx, &r),
323                 "TestEnum failed");
324         return true;
325 }
326
327 /*
328   test surrounding conformant array handling
329 */
330 static bool test_surrounding(struct torture_context *tctx,
331                                                   struct dcerpc_pipe *p)
332 {
333         struct echo_TestSurrounding r;
334         struct dcerpc_binding_handle *b = p->binding_handle;
335
336         ZERO_STRUCT(r);
337         r.in.data = talloc(tctx, struct echo_Surrounding);
338
339         r.in.data->x = 20;
340         r.in.data->surrounding = talloc_zero_array(tctx, uint16_t, r.in.data->x);
341
342         r.out.data = talloc(tctx, struct echo_Surrounding);
343
344         torture_assert_ntstatus_ok(tctx, dcerpc_echo_TestSurrounding_r(b, tctx, &r),
345                 "TestSurrounding failed");
346         
347         torture_assert(tctx, r.out.data->x == 2 * r.in.data->x,
348                 "TestSurrounding did not make the array twice as large");
349
350         return true;
351 }
352
353 /*
354   test multiple levels of pointers
355 */
356 static bool test_doublepointer(struct torture_context *tctx,
357                                struct dcerpc_pipe *p)
358 {
359         struct echo_TestDoublePointer r;
360         uint16_t value = 12;
361         uint16_t *pvalue = &value;
362         uint16_t **ppvalue = &pvalue;
363         struct dcerpc_binding_handle *b = p->binding_handle;
364
365         ZERO_STRUCT(r);
366         r.in.data = &ppvalue;
367
368         torture_assert_ntstatus_ok(tctx, dcerpc_echo_TestDoublePointer_r(b, tctx, &r),
369                 "TestDoublePointer failed");
370
371         torture_assert_int_equal(tctx, value, r.out.result, 
372                                         "TestDoublePointer did not return original value");
373         return true;
374 }
375
376
377 /*
378   test request timeouts
379 */
380 #if 0 /* this test needs fixing to work over ncacn_np */
381 static bool test_timeout(struct torture_context *tctx,
382                                                  struct dcerpc_pipe *p)
383 {
384         NTSTATUS status;
385         struct rpc_request *req;
386         struct echo_TestSleep r;
387         int timeout_saved = p->request_timeout;
388
389         if (torture_setting_bool(tctx, "quick", false)) {
390                 torture_skip(tctx, "timeout testing disabled - use \"torture:quick=no\" to enable\n");
391         }
392
393         torture_comment(tctx, "testing request timeouts\n");
394         r.in.seconds = 2;
395         p->request_timeout = 1;
396
397         req = dcerpc_echo_TestSleep_send(p, tctx, &r);
398         if (!req) {
399                 torture_comment(tctx, "Failed to send async sleep request\n");
400                 goto failed;
401         }
402         req->ignore_timeout = true;
403
404         status  = dcerpc_echo_TestSleep_recv(req);
405         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_IO_TIMEOUT, 
406                                                                   "request should have timed out");
407
408         torture_comment(tctx, "testing request destruction\n");
409         req = dcerpc_echo_TestSleep_send(p, tctx, &r);
410         if (!req) {
411                 torture_comment(tctx, "Failed to send async sleep request\n");
412                 goto failed;
413         }
414         talloc_free(req);
415
416         req = dcerpc_echo_TestSleep_send(p, tctx, &r);
417         if (!req) {
418                 torture_comment(tctx, "Failed to send async sleep request\n");
419                 goto failed;
420         }
421         req->ignore_timeout = true;
422         status  = dcerpc_echo_TestSleep_recv(req);
423         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_IO_TIMEOUT, 
424                 "request should have timed out");
425
426         p->request_timeout = timeout_saved;
427         
428         return test_addone(tctx, p);
429
430 failed:
431         p->request_timeout = timeout_saved;
432         return false;
433 }
434 #endif
435
436 struct torture_suite *torture_rpc_echo(TALLOC_CTX *mem_ctx)
437 {
438         struct torture_suite *suite = torture_suite_create(mem_ctx, "echo");
439         struct torture_rpc_tcase *tcase;
440
441         tcase = torture_suite_add_rpc_iface_tcase(suite, "echo", 
442                                                   &ndr_table_rpcecho);
443
444         torture_rpc_tcase_add_test(tcase, "addone", test_addone);
445         torture_rpc_tcase_add_test(tcase, "sinkdata", test_sinkdata);
446         torture_rpc_tcase_add_test(tcase, "echodata", test_echodata);
447         torture_rpc_tcase_add_test(tcase, "sourcedata", test_sourcedata);
448         torture_rpc_tcase_add_test(tcase, "testcall", test_testcall);
449         torture_rpc_tcase_add_test(tcase, "testcall2", test_testcall2);
450         torture_rpc_tcase_add_test(tcase, "enum", test_enum);
451         torture_rpc_tcase_add_test(tcase, "surrounding", test_surrounding);
452         torture_rpc_tcase_add_test(tcase, "doublepointer", test_doublepointer);
453         torture_rpc_tcase_add_test(tcase, "sleep", test_sleep);
454 #if 0 /* this test needs fixing to work over ncacn_np */
455         torture_rpc_tcase_add_test(tcase, "timeout", test_timeout);
456 #endif
457
458         return suite;
459 }