* fixed conformant arrays in structures
[kai/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 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, MIN(len, 16));
87                         printf("out:\n");
88                         dump_data(0, data_out, MIN(len, 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                 unsigned char *v = (unsigned char *)data_out;
124                 if (v[i] != (i & 0xFF)) {
125                         printf("bad data 0x%x at %d\n", (unsigned char)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 TestCall r;
173
174         printf("\nTesting TestCall\n");
175         status = dcerpc_TestCall(p, mem_ctx, &r);
176         if (!NT_STATUS_IS_OK(status)) {
177                 printf("TestCall failed - %s\n", nt_errstr(status));
178                 return False;
179         }
180
181         NDR_PRINT_DEBUG(Struct1, r.out.s1);     
182
183         return True;
184 }
185
186 BOOL torture_rpc_echo(int dummy)
187 {
188         NTSTATUS status;
189         struct dcerpc_pipe *p;
190         TALLOC_CTX *mem_ctx;
191         BOOL ret = True;
192
193         mem_ctx = talloc_init("torture_rpc_echo");
194
195         status = torture_rpc_connection(&p, "rpcecho");
196         if (!NT_STATUS_IS_OK(status)) {
197                 return False;
198         }
199
200         if (!test_addone(p, mem_ctx)) {
201                 ret = False;
202         }
203
204         if (!test_echodata(p, mem_ctx)) {
205                 ret = False;
206         }
207
208         if (!test_sourcedata(p, mem_ctx)) {
209                 ret = False;
210         }
211
212         if (!test_sinkdata(p, mem_ctx)) {
213                 ret = False;
214         }
215
216         if (!test_testcall(p, mem_ctx)) {
217                 ret = False;
218         }
219
220         printf("\n");
221         
222         torture_rpc_close(p);
223         return ret;
224 }