r2680: switched the libcli/raw/ code over to use talloc_reference(), which simplifies...
[samba.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    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24
25 /*
26   test the AddOne interface
27 */
28 static BOOL test_addone(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
29 {
30         int i;
31         NTSTATUS status;
32
33         printf("\nTesting AddOne\n");
34
35         for (i=0;i<10;i++) {
36                 uint32_t n = i;
37                 struct echo_AddOne r;
38                 r.in.v = &n;
39                 r.out.v = &n;
40                 status = dcerpc_echo_AddOne(p, mem_ctx, &r);
41                 if (!NT_STATUS_IS_OK(status)) {
42                         printf("AddOne(%d) failed - %s\n", i, nt_errstr(status));
43                         return False;
44                 }
45                 printf("%d + 1 = %u\n", i, n);
46         }
47
48         return True;
49 }
50
51 /*
52   test the EchoData interface
53 */
54 static BOOL test_echodata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
55 {
56         int i;
57         NTSTATUS status;
58         char *data_in, *data_out;
59         int len = 1 + (random() % 5000);
60         struct echo_EchoData r;
61
62         printf("\nTesting EchoData\n");
63
64         data_in = talloc(mem_ctx, len);
65         data_out = talloc(mem_ctx, len);
66         for (i=0;i<len;i++) {
67                 data_in[i] = i;
68         }
69         
70         r.in.len = len;
71         r.in.in_data = data_in;
72
73         status = dcerpc_echo_EchoData(p, mem_ctx, &r);
74         if (!NT_STATUS_IS_OK(status)) {
75                 printf("EchoData(%d) failed - %s\n", len, nt_errstr(status));
76                 return False;
77         }
78
79         data_out = r.out.out_data;
80
81         for (i=0;i<len;i++) {
82                 if (data_in[i] != data_out[i]) {
83                         printf("Bad data returned for len %d at offset %d\n", 
84                                len, i);
85                         printf("in:\n");
86                         dump_data(0, data_in+i, MIN(len-i, 16));
87                         printf("out:\n");
88                         dump_data(0, data_out+i, MIN(len-1, 16));
89                         return False;
90                 }
91         }
92
93
94         return True;
95 }
96
97
98 /*
99   test the SourceData interface
100 */
101 static BOOL test_sourcedata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
102 {
103         int i;
104         NTSTATUS status;
105         int len = 200000 + (random() % 5000);
106         char *data_out;
107         struct echo_SourceData r;
108
109         printf("\nTesting SourceData\n");
110
111         data_out = talloc(mem_ctx, len);
112
113         r.in.len = len;
114         r.out.data = data_out;
115
116         status = dcerpc_echo_SourceData(p, mem_ctx, &r);
117         if (!NT_STATUS_IS_OK(status)) {
118                 printf("SourceData(%d) failed - %s\n", len, nt_errstr(status));
119                 return False;
120         }
121
122         for (i=0;i<len;i++) {
123                 uint8_t *v = (uint8_t *)data_out;
124                 if (v[i] != (i & 0xFF)) {
125                         printf("bad data 0x%x at %d\n", (uint8_t)data_out[i], i);
126                         return False;
127                 }
128         }
129
130         return True;
131 }
132
133 /*
134   test the SinkData interface
135 */
136 static BOOL test_sinkdata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
137 {
138         int i;
139         NTSTATUS status;
140         char *data_in;
141         int len = 200000 + (random() % 5000);
142         struct echo_SinkData r;
143
144         printf("\nTesting SinkData\n");
145
146         data_in = talloc(mem_ctx, len);
147         for (i=0;i<len;i++) {
148                 data_in[i] = i+1;
149         }
150
151         r.in.len = len;
152         r.in.data = data_in;
153
154         status = dcerpc_echo_SinkData(p, mem_ctx, &r);
155         if (!NT_STATUS_IS_OK(status)) {
156                 printf("SinkData(%d) failed - %s\n", len, nt_errstr(status));
157                 return False;
158         }
159
160         printf("sunk %d bytes\n", len);
161
162         return True;
163 }
164
165
166 /*
167   test the testcall interface
168 */
169 static BOOL test_testcall(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
170 {
171         NTSTATUS status;
172         struct echo_TestCall r;
173
174         r.in.s1 = "input string";
175
176         printf("\nTesting TestCall\n");
177         status = dcerpc_echo_TestCall(p, mem_ctx, &r);
178         if (!NT_STATUS_IS_OK(status)) {
179                 printf("TestCall failed - %s\n", nt_errstr(status));
180                 return False;
181         }
182
183         return True;
184 }
185
186 /*
187   test the testcall interface
188 */
189 static BOOL test_testcall2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
190 {
191         NTSTATUS status;
192         struct echo_TestCall2 r;
193         int i;
194         BOOL ret = True;
195
196         for (i=1;i<=7;i++) {
197                 r.in.level = i;
198
199                 printf("\nTesting TestCall2 level %d\n", i);
200                 status = dcerpc_echo_TestCall2(p, mem_ctx, &r);
201                 if (!NT_STATUS_IS_OK(status)) {
202                         printf("TestCall2 failed - %s\n", nt_errstr(status));
203                         ret = False;
204                 }
205         }
206
207         return ret;
208 }
209
210
211 /*
212   test the TestSleep interface
213 */
214 static BOOL test_sleep(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
215 {
216         int i;
217         NTSTATUS status;
218 #define ASYNC_COUNT 5
219         struct rpc_request *req[ASYNC_COUNT];
220         struct echo_TestSleep r[ASYNC_COUNT];
221         int done[ASYNC_COUNT];
222         struct event_context *ctx;
223         int total_done = 0;
224         BOOL ret = True;
225
226         printf("\nTesting TestSleep\n");
227
228         for (i=0;i<ASYNC_COUNT;i++) {
229                 done[i] = 0;
230                 r[i].in.seconds = ASYNC_COUNT-i;
231                 req[i] = dcerpc_echo_TestSleep_send(p, mem_ctx, &r[i]);
232                 if (!req[i]) {
233                         printf("Failed to send async sleep request\n");
234                         return False;
235                 }
236         }
237
238         ctx = dcerpc_event_context(p);
239         while (total_done < ASYNC_COUNT) {
240                 if (event_loop_once(ctx) != 0) {
241                         return False;
242                 }
243                 for (i=0;i<ASYNC_COUNT;i++) {
244                         if (done[i] == 0 && req[i]->state == RPC_REQUEST_DONE) {
245                                 total_done++;
246                                 done[i] = 1;
247                                 status = dcerpc_ndr_request_recv(req[i]);
248                                 if (!NT_STATUS_IS_OK(status)) {
249                                         printf("TestSleep(%d) failed - %s\n",
250                                                i, nt_errstr(status));
251                                         ret = False;
252                                 } else {
253                                         printf("Sleep for %d seconds\n", 
254                                                r[i].out.result);
255                                 }
256                         }
257                 }
258         }
259
260         return ret;
261 }
262
263 BOOL torture_rpc_echo(int dummy)
264 {
265         NTSTATUS status;
266         struct dcerpc_pipe *p;
267         TALLOC_CTX *mem_ctx;
268         BOOL ret = True;
269
270         mem_ctx = talloc_init("torture_rpc_echo");
271
272         status = torture_rpc_connection(&p, 
273                                         DCERPC_RPCECHO_NAME,
274                                         DCERPC_RPCECHO_UUID,
275                                         DCERPC_RPCECHO_VERSION);
276         if (!NT_STATUS_IS_OK(status)) {
277                 return False;
278         }
279
280         if (!test_addone(p, mem_ctx)) {
281                 ret = False;
282         }
283
284         if (!test_sinkdata(p, mem_ctx)) {
285                 ret = False;
286         }
287
288         if (!test_echodata(p, mem_ctx)) {
289                 ret = False;
290         }
291
292         if (!test_sourcedata(p, mem_ctx)) {
293                 ret = False;
294         }
295
296         if (!test_testcall(p, mem_ctx)) {
297                 ret = False;
298         }
299
300         if (!test_testcall2(p, mem_ctx)) {
301                 ret = False;
302         }
303
304 /*
305         if (!test_sleep(p, mem_ctx)) {
306                 ret = False;
307         }
308 */
309         printf("\n");
310         
311         talloc_free(mem_ctx);
312
313         torture_rpc_close(p);
314         return ret;
315 }