r8220: added auto-generation of ENUM constants in ejs wrapper. So we can now use...
[kai/samba-autobuild/.git] / testprogs / ejs / echo.js
1 /*
2         demonstrate access to rpc calls from ejs
3 */      
4
5
6 /*
7   helper function to setup a rpc io object, ready for input
8 */
9 function irpcObj()
10 {
11         var o = new Object();
12         o.input = new Object();
13         return o;
14 }
15
16 /*
17   generate a ramp as an integer array
18  */
19 function ramp_array(N)
20 {
21         var a = new Array(N);
22         for (i=0;i<N;i++) {
23                 a[i] = i;
24         }
25         return a;
26 }
27
28
29 /*
30   check that a status result is OK
31 */
32 function check_status_ok(status)
33 {
34         if (status.is_ok != true) {
35                 printVars(status);
36         }
37         assert(status.is_ok == true);
38 }
39
40 /*
41   check that two arrays are equal
42 */
43 function check_array_equal(a1, a2)
44 {
45         assert(a1.length == a2.length);
46         for (i=0; i<a1.length; i++) {
47                 assert(a1[i] == a2[i]);
48         }
49 }
50
51 /*
52   check that an array is all zeros
53 */
54 function check_array_zero(a)
55 {
56         for (i=0; i<a.length; i++) {
57                 assert(a[i] == 0);
58         }
59 }
60
61 /*
62   test the echo_AddOne interface
63 */
64 function test_AddOne(conn)
65 {
66         var io = irpcObj();
67
68         print("Testing echo_AddOne\n");
69
70         for (i=0;i<10;i++) {
71                 io.input.in_data = i;
72                 status = dcerpc_echo_AddOne(conn, io);
73                 check_status_ok(status);
74                 assert(io.output.out_data == i + 1);
75         }
76 }
77
78 /*
79   test the echo_EchoData interface
80 */
81 function test_EchoData(conn)
82 {
83         var io = irpcObj();
84
85         print("Testing echo_EchoData\n");
86
87         for (i=0; i<30; i=i+5) {
88                 io.input.len = i;
89                 io.input.in_data = ramp_array(i);
90                 status = dcerpc_echo_EchoData(conn, io);
91                 check_status_ok(status);
92                 check_array_equal(io.input.in_data, io.output.out_data);
93         }
94 }
95
96
97 /*
98   test the echo_SinkData interface
99 */
100 function test_SinkData(conn)
101 {
102         var io = irpcObj();
103
104         print("Testing echo_SinkData\n");
105
106         for (i=0; i<30; i=i+5) {
107                 io.input.len = i;
108                 io.input.data = ramp_array(i);
109                 status = dcerpc_echo_SinkData(conn, io);
110                 check_status_ok(status);
111         }
112 }
113
114
115 /*
116   test the echo_SourceData interface
117 */
118 function test_SourceData(conn)
119 {
120         var io = irpcObj();
121
122         print("Testing echo_SourceData\n");
123
124         for (i=0; i<30; i=i+5) {
125                 io.input.len = i;
126                 status = dcerpc_echo_SourceData(conn, io);
127                 check_status_ok(status);
128                 correct = ramp_array(i);
129                 check_array_equal(correct, io.output.data);
130         }
131 }
132
133
134 /*
135   test the echo_TestCall interface
136 */
137 function test_TestCall(conn)
138 {
139         var io = irpcObj();
140
141         print("Testing echo_TestCall\n");
142
143         io.input.s1 = "my test string";
144         status = dcerpc_echo_TestCall(conn, io);
145         check_status_ok(status);
146         assert("this is a test string" == io.output.s2);
147 }
148
149 /*
150   test the echo_TestCall2 interface
151 */
152 function test_TestCall2(conn)
153 {
154         var io = irpcObj();
155
156         print("Testing echo_TestCall2\n");
157
158         for (i=1;i<=7;i++) {
159                 io.input.level = i;
160                 status = dcerpc_echo_TestCall2(conn, io);
161                 check_status_ok(status);
162         }
163 }
164
165 /*
166   test the echo_TestSleep interface
167 */
168 function test_TestSleep(conn)
169 {
170         var io = irpcObj();
171
172         print("Testing echo_TestSleep\n");
173
174         io.input.seconds = 1;
175         status = dcerpc_echo_TestSleep(conn, io);
176         check_status_ok(status);
177 }
178
179 /*
180   test the echo_TestEnum interface
181 */
182 function test_TestEnum(conn)
183 {
184         var io = irpcObj();
185
186         print("Testing echo_TestEnum\n");
187
188         io.input.foo1 = ECHO_ENUM1;
189         io.input.foo2 = new Object();
190         io.input.foo2.e1 = ECHO_ENUM1;
191         io.input.foo2.e2 = ECHO_ENUM1_32;
192         io.input.foo3 = new Object();
193         io.input.foo3.e1 = ECHO_ENUM2;
194         status = dcerpc_echo_TestEnum(conn, io);
195         check_status_ok(status);
196         assert(io.output.foo1    == ECHO_ENUM1);
197         assert(io.output.foo2.e1 == ECHO_ENUM2);
198         assert(io.output.foo2.e2 == ECHO_ENUM1_32);
199         assert(io.output.foo3.e1 == ECHO_ENUM2);
200 }
201
202 /*
203   test the echo_TestSurrounding interface
204 */
205 function test_TestSurrounding(conn)
206 {
207         var io = irpcObj();
208
209         print("Testing echo_TestSurrounding\n");
210         
211         io.input.data = new Object();
212         io.input.data.x = 10;
213         io.input.data.surrounding = ramp_array(10);
214         status = dcerpc_echo_TestSurrounding(conn, io);
215         check_status_ok(status);
216         assert(io.output.data.surrounding.length == 20);
217         check_array_zero(io.output.data.surrounding);
218 }
219
220 /*
221   test the echo_TestDoublePointer interface
222 */
223 function test_TestDoublePointer(conn)
224 {
225         var io = irpcObj();
226
227         print("Testing echo_TestDoublePointer\n");
228         
229         io.input.data = 7;
230         status = dcerpc_echo_TestDoublePointer(conn, io);
231         check_status_ok(status);
232         assert(io.input.data == io.input.data);
233 }
234
235
236 if (ARGV.length == 0) {
237    print("Usage: echo.js <RPCBINDING>\n");
238    exit(0);
239 }
240
241 var binding = ARGV[0];
242 var conn = new Object();
243
244 print("Connecting to " + binding + "\n");
245 status = rpc_connect(conn, binding, "rpcecho");
246 if (status.is_ok != true) {
247    print("Failed to connect to " + binding + " - " + status.errstr + "\n");
248    return;
249 }
250
251 test_AddOne(conn);
252 test_EchoData(conn);
253 test_SinkData(conn);
254 test_SourceData(conn);
255 test_TestCall(conn);
256 test_TestCall2(conn);
257 test_TestSleep(conn);
258 test_TestEnum(conn);
259 test_TestSurrounding(conn);
260 test_TestDoublePointer(conn);
261
262 print("All OK\n");