r19339: Merge my 4.0-unittest branch. This adds an API for more fine-grained
[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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "torture/torture.h"
26 #include "torture/rpc/rpc.h"
27 #include "lib/events/events.h"
28 #include "librpc/gen_ndr/ndr_echo_c.h"
29
30
31 /*
32   test the AddOne interface
33 */
34 #define TEST_ADDONE(tctx, value) do { \
35         n = i = value; \
36         r.in.in_data = n; \
37         r.out.out_data = &n; \
38         status = dcerpc_echo_AddOne(p, tctx, &r); \
39         torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "AddOne(%d) failed", i)); \
40         torture_assert (tctx, n == i+1, talloc_asprintf(tctx, "%d + 1 != %u (should be %u)\n", i, n, i+1)); \
41         torture_comment (tctx, "%d + 1 = %u\n", i, n); \
42 } while(0)
43
44 static bool test_addone(struct torture_context *tctx, 
45                                                 struct dcerpc_pipe *p)
46 {
47         uint32_t i;
48         NTSTATUS status;
49         uint32_t n;
50         struct echo_AddOne r;
51
52         for (i=0;i<10;i++) {
53                 TEST_ADDONE(tctx, i);
54         }
55
56         TEST_ADDONE(tctx, 0x7FFFFFFE);
57         TEST_ADDONE(tctx, 0xFFFFFFFE);
58         TEST_ADDONE(tctx, 0xFFFFFFFF);
59         TEST_ADDONE(tctx, random() & 0xFFFFFFFF);
60         return true;
61 }
62
63 /*
64   test the EchoData interface
65 */
66 static bool test_echodata(struct torture_context *tctx,
67                                                   struct dcerpc_pipe *p)
68 {
69         int i;
70         NTSTATUS status;
71         uint8_t *data_in, *data_out;
72         int len;
73         struct echo_EchoData r;
74
75         if (torture_setting_bool(tctx, "quick", false) &&
76             (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
77                 len = 1 + (random() % 500);
78         } else {
79                 len = 1 + (random() % 5000);
80         }
81
82         data_in = talloc_size(tctx, len);
83         data_out = talloc_size(tctx, len);
84         for (i=0;i<len;i++) {
85                 data_in[i] = i;
86         }
87         
88         r.in.len = len;
89         r.in.in_data = data_in;
90
91         status = dcerpc_echo_EchoData(p, tctx, &r);
92         torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, 
93                                                                                         "EchoData(%d) failed\n", len));
94
95         data_out = r.out.out_data;
96
97         for (i=0;i<len;i++) {
98                 if (data_in[i] != data_out[i]) {
99                         torture_comment(tctx, "Bad data returned for len %d at offset %d\n", 
100                                len, i);
101                         torture_comment(tctx, "in:\n");
102                         dump_data(0, data_in+i, MIN(len-i, 16));
103                         torture_comment(tctx, "out:\n");
104                         dump_data(0, data_out+i, MIN(len-1, 16));
105                         return false;
106                 }
107         }
108         return true;
109 }
110
111 /*
112   test the SourceData interface
113 */
114 static bool test_sourcedata(struct torture_context *tctx,
115                                                   struct dcerpc_pipe *p)
116 {
117         int i;
118         NTSTATUS status;
119         int len;
120         struct echo_SourceData r;
121
122         if (torture_setting_bool(tctx, "quick", false) &&
123             (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
124                 len = 100 + (random() % 500);
125         } else {
126                 len = 200000 + (random() % 5000);
127         }
128
129         r.in.len = len;
130
131         status = dcerpc_echo_SourceData(p, tctx, &r);
132         torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, 
133                                                                                 "SourceData(%d) failed", len));
134
135         for (i=0;i<len;i++) {
136                 uint8_t *v = (uint8_t *)r.out.data;
137                 torture_assert(tctx, v[i] == (i & 0xFF),
138                         talloc_asprintf(tctx, 
139                                                 "bad data 0x%x at %d\n", (uint8_t)r.out.data[i], i));
140         }
141         return true;
142 }
143
144 /*
145   test the SinkData interface
146 */
147 static bool test_sinkdata(struct torture_context *tctx, 
148                                                   struct dcerpc_pipe *p)
149 {
150         int i;
151         NTSTATUS status;
152         uint8_t *data_in;
153         int len;
154         struct echo_SinkData r;
155
156         if (torture_setting_bool(tctx, "quick", false) &&
157             (p->conn->flags & DCERPC_DEBUG_VALIDATE_BOTH)) {
158                 len = 100 + (random() % 5000);
159         } else {
160                 len = 200000 + (random() % 5000);
161         }
162
163         data_in = talloc_size(tctx, len);
164         for (i=0;i<len;i++) {
165                 data_in[i] = i+1;
166         }
167
168         r.in.len = len;
169         r.in.data = data_in;
170
171         status = dcerpc_echo_SinkData(p, tctx, &r);
172         torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, 
173                                                                                 "SinkData(%d) failed", 
174                                                            len));
175
176         torture_comment(tctx, "sunk %d bytes\n", len);
177         return true;
178 }
179
180
181 /*
182   test the testcall interface
183 */
184 static bool test_testcall(struct torture_context *tctx,
185                                                   struct dcerpc_pipe *p)
186 {
187         NTSTATUS status;
188         struct echo_TestCall r;
189         const char *s = NULL;
190
191         r.in.s1 = "input string";
192         r.out.s2 = &s;
193
194         status = dcerpc_echo_TestCall(p, tctx, &r);
195         torture_assert_ntstatus_ok(tctx, status, "TestCall failed");
196
197         torture_assert_str_equal(tctx, s, "input string", "Didn't receive back same string");
198
199         return true;
200 }
201
202 /*
203   test the testcall interface
204 */
205 static bool test_testcall2(struct torture_context *tctx,
206                                                   struct dcerpc_pipe *p)
207 {
208         NTSTATUS status;
209         struct echo_TestCall2 r;
210         int i;
211
212         for (i=1;i<=7;i++) {
213                 r.in.level = i;
214                 r.out.info = talloc(tctx, union echo_Info);
215
216                 torture_comment(tctx, "Testing TestCall2 level %d\n", i);
217                 status = dcerpc_echo_TestCall2(p, tctx, &r);
218                 torture_assert_ntstatus_ok(tctx, status, "TestCall2 failed");
219         }
220         return true;
221 }
222
223 /*
224   test the TestSleep interface
225 */
226 static bool test_sleep(struct torture_context *tctx,
227                                                   struct dcerpc_pipe *p)
228 {
229         int i;
230         NTSTATUS status;
231 #define ASYNC_COUNT 3
232         struct rpc_request *req[ASYNC_COUNT];
233         struct echo_TestSleep r[ASYNC_COUNT];
234         BOOL done[ASYNC_COUNT];
235         struct timeval snd[ASYNC_COUNT];
236         struct timeval rcv[ASYNC_COUNT];
237         struct timeval diff[ASYNC_COUNT];
238         struct event_context *ctx;
239         int total_done = 0;
240
241         if (torture_setting_bool(tctx, "quick", false)) {
242                 torture_skip(tctx, "TestSleep disabled - use \"torture:quick=no\" to enable\n");
243         }
244         torture_comment(tctx, "Testing TestSleep - use \"torture:quick=no\" to disable\n");
245
246         for (i=0;i<ASYNC_COUNT;i++) {
247                 done[i]         = False;
248                 snd[i]          = timeval_current();
249                 rcv[i]          = timeval_zero();
250                 r[i].in.seconds = ASYNC_COUNT-i;
251                 req[i] = dcerpc_echo_TestSleep_send(p, tctx, &r[i]);
252                 torture_assert(tctx, req[i], "Failed to send async sleep request\n");
253         }
254
255         ctx = dcerpc_event_context(p);
256         while (total_done < ASYNC_COUNT) {
257                 torture_assert(tctx, event_loop_once(ctx) == 0, 
258                                            "Event context loop failed");
259                 for (i=0;i<ASYNC_COUNT;i++) {
260                         if (done[i] == False && req[i]->state == RPC_REQUEST_DONE) {
261                                 total_done++;
262                                 done[i] = True;
263                                 rcv[i]  = timeval_current();
264                                 diff[i] = timeval_until(&snd[i], &rcv[i]);
265                                 status  = dcerpc_ndr_request_recv(req[i]);
266                                 torture_assert_ntstatus_ok(tctx, status, 
267                                                         talloc_asprintf(tctx, "TestSleep(%d) failed", i));
268                                 torture_assert(tctx, r[i].out.result == r[i].in.seconds,
269                                         talloc_asprintf(tctx, "Failed - Asked to sleep for %u seconds (server replied with %u seconds and the reply takes only %u seconds)", 
270                                                 r[i].out.result, r[i].in.seconds, (uint_t)diff[i].tv_sec));
271                                 torture_assert(tctx, r[i].out.result <= diff[i].tv_sec, 
272                                         talloc_asprintf(tctx, "Failed - Slept for %u seconds (but reply takes only %u.%06u seconds)", 
273                                                 r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec));
274                                 if (r[i].out.result+1 == diff[i].tv_sec) {
275                                         torture_comment(tctx, "Slept for %u seconds (but reply takes %u.%06u seconds - busy server?)\n", 
276                                                         r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
277                                 } else if (r[i].out.result == diff[i].tv_sec) {
278                                         torture_comment(tctx, "Slept for %u seconds (reply takes %u.%06u seconds - ok)\n", 
279                                                         r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
280                                 } else {
281                                                 torture_comment(tctx, "(Failed) - Not async - Slept for %u seconds (but reply takes %u.%06u seconds)", 
282                                                         r[i].out.result, (uint_t)diff[i].tv_sec, (uint_t)diff[i].tv_usec);
283                                         /* TODO: let the test fail here, when we support async rpc on ncacn_np */
284                                 }
285                         }
286                 }
287         }
288         return true;
289 }
290
291 /*
292   test enum handling
293 */
294 static bool test_enum(struct torture_context *tctx,
295                                                   struct dcerpc_pipe *p)
296 {
297         NTSTATUS status;
298         struct echo_TestEnum r;
299         enum echo_Enum1 v = ECHO_ENUM1;
300         struct echo_Enum2 e2;
301         union echo_Enum3 e3;
302
303         r.in.foo1 = &v;
304         r.in.foo2 = &e2;
305         r.in.foo3 = &e3;
306         r.out.foo1 = &v;
307         r.out.foo2 = &e2;
308         r.out.foo3 = &e3;
309
310         e2.e1 = 76;
311         e2.e2 = ECHO_ENUM1_32;
312         e3.e1 = ECHO_ENUM2;
313
314         status = dcerpc_echo_TestEnum(p, tctx, &r);
315         torture_assert_ntstatus_ok(tctx, status, "TestEnum failed");
316         return true;
317 }
318
319 /*
320   test surrounding conformant array handling
321 */
322 static bool test_surrounding(struct torture_context *tctx,
323                                                   struct dcerpc_pipe *p)
324 {
325         NTSTATUS status;
326         struct echo_TestSurrounding r;
327
328         ZERO_STRUCT(r);
329         r.in.data = talloc(tctx, struct echo_Surrounding);
330
331         r.in.data->x = 20;
332         r.in.data->surrounding = talloc_zero_array(tctx, uint16_t, r.in.data->x);
333
334         r.out.data = talloc(tctx, struct echo_Surrounding);
335
336         status = dcerpc_echo_TestSurrounding(p, tctx, &r);
337         torture_assert_ntstatus_ok(tctx, status, "TestSurrounding failed");
338         
339         torture_assert(tctx, r.out.data->x == 2 * r.in.data->x,
340                 "TestSurrounding did not make the array twice as large");
341
342         return true;
343 }
344
345 /*
346   test multiple levels of pointers
347 */
348 static bool test_doublepointer(struct torture_context *tctx,
349                                                            struct dcerpc_pipe *p)
350 {
351         NTSTATUS status;
352         struct echo_TestDoublePointer r;
353         uint16_t value = 12;
354         uint16_t *pvalue = &value;
355         uint16_t **ppvalue = &pvalue;
356
357         ZERO_STRUCT(r);
358         r.in.data = &ppvalue;
359
360         status = dcerpc_echo_TestDoublePointer(p, tctx, &r);
361         torture_assert_ntstatus_ok(tctx, status, "TestDoublePointer failed");
362
363         torture_assert_int_equal(tctx, value, r.out.result, 
364                                         "TestDoublePointer did not return original value");
365         return true;
366 }
367
368
369 /*
370   test request timeouts
371 */
372 static bool test_timeout(struct torture_context *tctx,
373                                                  struct dcerpc_pipe *p)
374 {
375         NTSTATUS status;
376         struct rpc_request *req;
377         struct echo_TestSleep r;
378         int timeout_saved = p->request_timeout;
379
380         if (torture_setting_bool(tctx, "quick", false)) {
381                 torture_skip(tctx, "timeout testing disabled - use \"torture:quick=no\" to enable\n");
382         }
383
384         torture_comment(tctx, "testing request timeouts\n");
385         r.in.seconds = 2;
386         p->request_timeout = 1;
387
388         torture_assert(tctx, req = dcerpc_echo_TestSleep_send(p, tctx, &r), 
389                 "Failed to send async sleep request");
390
391         status  = dcerpc_ndr_request_recv(req);
392         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_IO_TIMEOUT, 
393                                                                   "request should have timed out");
394
395         torture_comment(tctx, "testing request destruction\n");
396         req = dcerpc_echo_TestSleep_send(p, tctx, &r);
397         if (!req) {
398                 torture_comment(tctx, "Failed to send async sleep request\n");
399                 goto failed;
400         }
401         talloc_free(req);
402
403         req = dcerpc_echo_TestSleep_send(p, tctx, &r);
404         if (!req) {
405                 torture_comment(tctx, "Failed to send async sleep request\n");
406                 goto failed;
407         }
408         status  = dcerpc_ndr_request_recv(req);
409         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_IO_TIMEOUT, 
410                 "request should have timed out");
411
412         p->request_timeout = timeout_saved;
413         
414         return test_addone(tctx, p);
415
416 failed:
417         p->request_timeout = timeout_saved;
418         return false;
419 }
420
421
422 struct torture_suite *torture_rpc_echo(void)
423 {
424         struct torture_suite *suite = torture_suite_create(
425                                                                         talloc_autofree_context(), "ECHO");
426         struct torture_tcase *tcase;
427
428         tcase = torture_suite_add_rpc_iface_tcase(suite, "echo", 
429                                                                                           &dcerpc_table_rpcecho);
430
431         torture_rpc_tcase_add_test(tcase, "addone", test_addone);
432         torture_rpc_tcase_add_test(tcase, "sinkdata", test_sinkdata);
433         torture_rpc_tcase_add_test(tcase, "echodata", test_echodata);
434         torture_rpc_tcase_add_test(tcase, "sourcedata", test_sourcedata);
435         torture_rpc_tcase_add_test(tcase, "testcall", test_testcall);
436         torture_rpc_tcase_add_test(tcase, "testcall2", test_testcall2);
437         torture_rpc_tcase_add_test(tcase, "enum", test_enum);
438         torture_rpc_tcase_add_test(tcase, "surrounding", test_surrounding);
439         torture_rpc_tcase_add_test(tcase, "doublepointer", test_doublepointer);
440         torture_rpc_tcase_add_test(tcase, "sleep", test_sleep);
441         torture_rpc_tcase_add_test(tcase, "timeout", test_timeout);
442
443         return suite;
444 }