r9663: Portability fix for IE.
[kai/samba.git] / testprogs / ejs / echo.js
1 #!/usr/bin/env smbscript
2 /*
3         test echo pipe calls from ejs
4 */      
5
6 var options = GetOptions(ARGV, 
7                 "POPT_AUTOHELP",
8                 "POPT_COMMON_SAMBA",
9                 "POPT_COMMON_CREDENTIALS");
10 if (options == undefined) {
11    println("Failed to parse options");
12    return -1;
13 }
14
15 libinclude("base.js");
16
17 /*
18   generate a ramp as an integer array
19  */
20 function ramp_array(N)
21 {
22         var a = new Array(N);
23         var data = datablob_init();
24         for (i=0;i<N;i++) {
25                 a[i] = i;
26         }
27         return data.blobFromArray(a);
28 }
29
30
31 /*
32   test the echo_AddOne interface
33 */
34 function test_AddOne(echo)
35 {
36         var io = irpcObj();
37
38         print("Testing echo_AddOne\n");
39
40         for (i=0;i<10;i++) {
41                 io.input.in_data = i;
42                 status = echo.echo_AddOne(io);
43                 check_status_ok(status);
44                 assert(io.output.out_data == i + 1);
45         }
46 }
47
48 /*
49   test the echo_EchoData interface
50 */
51 function test_EchoData(echo)
52 {
53         var io = irpcObj();
54
55         print("Testing echo_EchoData\n");
56
57         for (i=0; i<30; i=i+5) {
58                 io.input.len = i;
59                 io.input.in_data = ramp_array(i);
60                 status = echo.echo_EchoData(io);
61                 check_status_ok(status);
62                 assert(true == echo.blobCompare(io.input.in_data, io.output.out_data));
63         }
64 }
65
66
67 /*
68   test the echo_SinkData interface
69 */
70 function test_SinkData(echo)
71 {
72         var io = irpcObj();
73
74         print("Testing echo_SinkData\n");
75
76         for (i=0; i<30; i=i+5) {
77                 io.input.len = i;
78                 io.input.data = ramp_array(i);
79                 status = echo.echo_SinkData(io);
80                 check_status_ok(status);
81         }
82 }
83
84
85 /*
86   test the echo_SourceData interface
87 */
88 function test_SourceData(echo)
89 {
90         var io = irpcObj();
91
92         print("Testing echo_SourceData\n");
93
94         for (i=0; i<30; i=i+5) {
95                 io.input.len = i;
96                 status = echo.echo_SourceData(io);
97                 check_status_ok(status);
98                 correct = ramp_array(i);
99                 assert(true == echo.blobCompare(correct, io.output.data));
100         }
101 }
102
103
104 /*
105   test the echo_TestCall interface
106 */
107 function test_TestCall(echo)
108 {
109         var io = irpcObj();
110
111         print("Testing echo_TestCall\n");
112
113         io.input.s1 = "my test string";
114         status = echo.echo_TestCall(io);
115         check_status_ok(status);
116         assert("this is a test string" == io.output.s2);
117 }
118
119 /*
120   test the echo_TestCall2 interface
121 */
122 function test_TestCall2(echo)
123 {
124         var io = irpcObj();
125
126         print("Testing echo_TestCall2\n");
127
128         for (i=1;i<=7;i++) {
129                 io.input.level = i;
130                 status = echo.echo_TestCall2(io);
131                 check_status_ok(status);
132         }
133 }
134
135 /*
136   test the echo_TestSleep interface
137 */
138 function test_TestSleep(echo)
139 {
140         var io = irpcObj();
141
142         print("Testing echo_TestSleep\n");
143
144         io.input.seconds = 1;
145         status = echo.echo_TestSleep(io);
146         check_status_ok(status);
147 }
148
149 /*
150   test the echo_TestEnum interface
151 */
152 function test_TestEnum(echo)
153 {
154         var io = irpcObj();
155
156         print("Testing echo_TestEnum\n");
157
158         io.input.foo1 = echo.ECHO_ENUM1;
159         io.input.foo2 = new Object();
160         io.input.foo2.e1 = echo.ECHO_ENUM1;
161         io.input.foo2.e2 = echo.ECHO_ENUM1_32;
162         io.input.foo3 = new Object();
163         io.input.foo3.e1 = echo.ECHO_ENUM2;
164         status = echo.echo_TestEnum(io);
165         check_status_ok(status);
166         assert(io.output.foo1    == echo.ECHO_ENUM1);
167         assert(io.output.foo2.e1 == echo.ECHO_ENUM2);
168         assert(io.output.foo2.e2 == echo.ECHO_ENUM1_32);
169         assert(io.output.foo3.e1 == echo.ECHO_ENUM2);
170 }
171
172 /*
173   test the echo_TestSurrounding interface
174 */
175 function test_TestSurrounding(echo)
176 {
177         var io = irpcObj();
178
179         print("Testing echo_TestSurrounding\n");
180         
181         io.input.data = new Object();
182         io.input.data.x = 10;
183         io.input.data.surrounding = new Array(10);
184         status = echo.echo_TestSurrounding(io);
185         check_status_ok(status);
186         assert(io.output.data.surrounding.length == 20);
187         check_array_zero(io.output.data.surrounding);
188 }
189
190 /*
191   test the echo_TestDoublePointer interface
192 */
193 function test_TestDoublePointer(echo)
194 {
195         var io = irpcObj();
196
197         print("Testing echo_TestDoublePointer\n");
198         
199         io.input.data = 7;
200         status = echo.echo_TestDoublePointer(io);
201         check_status_ok(status);
202         assert(io.input.data == io.input.data);
203 }
204
205
206 if (options.ARGV.length != 1) {
207    println("Usage: echo.js <BINDING>");
208    return -1;
209 }
210 var binding = options.ARGV[0];
211 var echo = rpcecho_init();
212 datablob_init(echo);
213
214 print("Connecting to " + binding + "\n");
215 status = echo.connect(binding);
216 if (status.is_ok != true) {
217    printf("Failed to connect to %s - %s\n", binding, status.errstr);
218    return;
219 }
220
221 test_AddOne(echo);
222 test_EchoData(echo);
223 test_SinkData(echo);
224 test_SourceData(echo);
225 test_TestCall(echo);
226 test_TestCall2(echo);
227 test_TestSleep(echo);
228 test_TestEnum(echo);
229 test_TestSurrounding(echo);
230 test_TestDoublePointer(echo);
231
232 println("All OK\n");
233 return 0;