converted the rpcecho pipe to use IDL
[ira/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    
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.data = data_in;
72         r.out.data = data_out;
73
74         status = dcerpc_echo_EchoData(p, mem_ctx, &r);
75         if (!NT_STATUS_IS_OK(status)) {
76                 printf("EchoData(%d) failed - %s\n", len, nt_errstr(status));
77                 return False;
78         }
79
80         if (memcmp(data_in, data_out, len) != 0) {
81                 printf("Bad data returned for len %d!\n", len);
82                 return False;
83         }
84
85
86         return True;
87 }
88
89
90 /*
91   test the SourceData interface
92 */
93 static BOOL test_sourcedata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
94 {
95         int i;
96         NTSTATUS status;
97         int len = 200000 + (random() % 5000);
98         char *data_out;
99         struct echo_SourceData r;
100
101         printf("\nTesting SourceData\n");
102
103         data_out = talloc(mem_ctx, len);
104
105         r.in.len = len;
106         r.out.data = data_out;
107
108         status = dcerpc_echo_SourceData(p, mem_ctx, &r);
109         if (!NT_STATUS_IS_OK(status)) {
110                 printf("SourceData(%d) failed - %s\n", len, nt_errstr(status));
111                 return False;
112         }
113
114         for (i=0;i<len;i++) {
115                 unsigned char *v = (unsigned char *)data_out;
116                 if (v[i] != (i & 0xFF)) {
117                         printf("bad data 0x%x at %d\n", (unsigned char)data_out[i], i);
118                         return False;
119                 }
120         }
121
122         return True;
123 }
124
125 /*
126   test the SinkData interface
127 */
128 static BOOL test_sinkdata(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
129 {
130         int i;
131         NTSTATUS status;
132         char *data_in;
133         int len = 200000 + (random() % 5000);
134         struct echo_SinkData r;
135
136         printf("\nTesting SinkData\n");
137
138         data_in = talloc(mem_ctx, len);
139         for (i=0;i<len;i++) {
140                 data_in[i] = i+1;
141         }
142
143         r.in.len = len;
144         r.in.data = data_in;
145
146         status = dcerpc_echo_SinkData(p, mem_ctx, &r);
147         if (!NT_STATUS_IS_OK(status)) {
148                 printf("SinkData(%d) failed - %s\n", len, nt_errstr(status));
149                 return False;
150         }
151
152         printf("sunk %d bytes\n", len);
153
154         return True;
155 }
156
157 BOOL torture_rpc_echo(int dummy)
158 {
159         NTSTATUS status;
160         struct dcerpc_pipe *p;
161         TALLOC_CTX *mem_ctx;
162         BOOL ret = True;
163
164         mem_ctx = talloc_init("torture_rpc_echo");
165
166         status = torture_rpc_connection(&p, "rpcecho");
167         if (!NT_STATUS_IS_OK(status)) {
168                 return False;
169         }
170
171         if (!test_addone(p, mem_ctx)) {
172                 ret = False;
173         }
174
175         if (!test_echodata(p, mem_ctx)) {
176                 ret = False;
177         }
178
179         if (!test_sourcedata(p, mem_ctx)) {
180                 ret = False;
181         }
182
183         if (!test_sinkdata(p, mem_ctx)) {
184                 ret = False;
185         }
186
187         printf("\n");
188         
189         torture_rpc_close(p);
190         return ret;
191 }