1//
2// Copyright (c) ZeroC, Inc. All rights reserved.
3//
4
5import {Ice} from "ice";
6import {Test} from "./generated";
7import {TestHelper} from "../../../Common/TestHelper";
8
9const test = TestHelper.test;
10
11export class Client extends TestHelper
12{
13    async allTests()
14    {
15        class TestError extends Error
16        {
17        }
18
19        const communicator = this.communicator();
20        const out = this.getWriter();
21
22        const defaultProtocol = communicator.getProperties().getPropertyWithDefault("Ice.Default.Protocol", "tcp");
23
24        out.write("testing stringToProxy... ");
25        const ref = "test:" + this.getTestEndpoint();
26        const base = communicator.stringToProxy(ref);
27        test(base !== null);
28
29        let b1 = communicator.stringToProxy("test");
30        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category.length === 0 &&
31                b1.ice_getAdapterId().length === 0 && b1.ice_getFacet().length === 0);
32        b1 = communicator.stringToProxy("test ");
33        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category.length === 0 &&
34                b1.ice_getFacet().length === 0);
35        b1 = communicator.stringToProxy(" test ");
36        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category.length === 0 &&
37                b1.ice_getFacet().length === 0);
38        b1 = communicator.stringToProxy(" test");
39        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category.length === 0 &&
40                b1.ice_getFacet().length === 0);
41        b1 = communicator.stringToProxy("'test -f facet'");
42        test(b1.ice_getIdentity().name === "test -f facet" && b1.ice_getIdentity().category.length === 0 &&
43                b1.ice_getFacet().length === 0);
44
45        try
46        {
47            b1 = communicator.stringToProxy("\"test -f facet'");
48            test(false);
49        }
50        catch(ex)
51        {
52            test(ex instanceof Ice.ProxyParseException, ex);
53        }
54
55        b1 = communicator.stringToProxy("\"test -f facet\"");
56        test(b1.ice_getIdentity().name === "test -f facet" && b1.ice_getIdentity().category.length === 0 &&
57                b1.ice_getFacet().length === 0);
58        b1 = communicator.stringToProxy("\"test -f facet@test\"");
59        test(b1.ice_getIdentity().name === "test -f facet@test" && b1.ice_getIdentity().category.length === 0 &&
60                b1.ice_getFacet().length === 0);
61        b1 = communicator.stringToProxy("\"test -f facet@test @test\"");
62        test(b1.ice_getIdentity().name === "test -f facet@test @test" &&
63                b1.ice_getIdentity().category.length === 0 &&
64                b1.ice_getFacet().length === 0);
65
66        try
67        {
68            b1 = communicator.stringToProxy("test test");
69            test(false);
70        }
71        catch(ex)
72        {
73            test(ex instanceof Ice.ProxyParseException, ex);
74        }
75
76        b1 = communicator.stringToProxy("test\\040test");
77        test(b1.ice_getIdentity().name === "test test" && b1.ice_getIdentity().category.length === 0);
78
79        try
80        {
81            b1 = communicator.stringToProxy("test\\777");
82            test(false);
83        }
84        catch(ex)
85        {
86            test(ex instanceof Ice.IdentityParseException, ex);
87        }
88
89        b1 = communicator.stringToProxy("test\\40test");
90        test(b1.ice_getIdentity().name === "test test");
91
92        // Test some octal and hex corner cases.
93        b1 = communicator.stringToProxy("test\\4test");
94        test(b1.ice_getIdentity().name === "test\x04test");
95        b1 = communicator.stringToProxy("test\\04test");
96        test(b1.ice_getIdentity().name === "test\x04test");
97        b1 = communicator.stringToProxy("test\\004test");
98        test(b1.ice_getIdentity().name === "test\x04test");
99        b1 = communicator.stringToProxy("test\\1114test");
100        test(b1.ice_getIdentity().name === "test\x494test");
101
102        b1 = communicator.stringToProxy("test\\b\\f\\n\\r\\t\\'\\\"\\\\test");
103        test(b1.ice_getIdentity().name === "test\b\f\n\r\t'\"\\test" &&
104                b1.ice_getIdentity().category.length === 0);
105
106        b1 = communicator.stringToProxy("category/test");
107        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category === "category" &&
108                b1.ice_getAdapterId().length === 0);
109
110        b1 = communicator.stringToProxy("");
111        test(b1 === null);
112        b1 = communicator.stringToProxy("\"\"");
113        test(b1 === null);
114        try
115        {
116            b1 = communicator.stringToProxy("\"\" test"); // Invalid trailing characters.
117            test(false);
118        }
119        catch(ex)
120        {
121            test(ex instanceof Ice.ProxyParseException, ex);
122        }
123
124        try
125        {
126            b1 = communicator.stringToProxy("test:"); // Missing endpoint.
127            test(false);
128        }
129        catch(ex)
130        {
131            test(ex instanceof Ice.EndpointParseException, ex);
132        }
133
134        b1 = communicator.stringToProxy("test@adapter");
135        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category.length === 0 &&
136                b1.ice_getAdapterId() === "adapter");
137        try
138        {
139            b1 = communicator.stringToProxy("id@adapter test");
140            test(false);
141        }
142        catch(ex)
143        {
144            test(ex instanceof Ice.ProxyParseException, ex);
145        }
146
147        b1 = communicator.stringToProxy("category/test@adapter");
148        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category === "category" &&
149                b1.ice_getAdapterId() === "adapter");
150        b1 = communicator.stringToProxy("category/test@adapter:" + defaultProtocol);
151        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category === "category" &&
152                b1.ice_getAdapterId() === "adapter:" + defaultProtocol);
153        b1 = communicator.stringToProxy("'category 1/test'@adapter");
154        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category === "category 1" &&
155                b1.ice_getAdapterId() === "adapter");
156        b1 = communicator.stringToProxy("'category/test 1'@adapter");
157        test(b1.ice_getIdentity().name === "test 1" && b1.ice_getIdentity().category === "category" &&
158                b1.ice_getAdapterId() === "adapter");
159        b1 = communicator.stringToProxy("'category/test'@'adapter 1'");
160        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category === "category" &&
161                b1.ice_getAdapterId() === "adapter 1");
162        b1 = communicator.stringToProxy("\"category \\/test@foo/test\"@adapter");
163        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category === "category /test@foo" &&
164                b1.ice_getAdapterId() === "adapter");
165        b1 = communicator.stringToProxy("\"category \\/test@foo/test\"@\"adapter:" + defaultProtocol + "\"");
166        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category === "category /test@foo" &&
167                b1.ice_getAdapterId() === "adapter:" + defaultProtocol);
168
169        b1 = communicator.stringToProxy("id -f facet");
170        test(b1.ice_getIdentity().name === "id" && b1.ice_getIdentity().category.length === 0 &&
171                b1.ice_getFacet() === "facet");
172        b1 = communicator.stringToProxy("id -f 'facet x'");
173        test(b1.ice_getIdentity().name === "id" && b1.ice_getIdentity().category.length === 0 &&
174                b1.ice_getFacet() === "facet x");
175        b1 = communicator.stringToProxy("id -f \"facet x\"");
176        test(b1.ice_getIdentity().name === "id" && b1.ice_getIdentity().category.length === 0 &&
177                b1.ice_getFacet() === "facet x");
178
179        try
180        {
181            b1 = communicator.stringToProxy("id -f \"facet x");
182            test(false);
183        }
184        catch(ex)
185        {
186            test(ex instanceof Ice.ProxyParseException, ex);
187        }
188
189        try
190        {
191            b1 = communicator.stringToProxy("id -f 'facet x");
192            test(false);
193        }
194        catch(ex)
195        {
196            test(ex instanceof Ice.ProxyParseException, ex);
197        }
198
199        b1 = communicator.stringToProxy("test -f facet:" + defaultProtocol);
200        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category.length === 0 &&
201                b1.ice_getFacet() === "facet" && b1.ice_getAdapterId().length === 0);
202        b1 = communicator.stringToProxy("test -f \"facet:" + defaultProtocol + "\"");
203        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category.length === 0 &&
204                b1.ice_getFacet() === "facet:" + defaultProtocol && b1.ice_getAdapterId().length === 0);
205        b1 = communicator.stringToProxy("test -f facet@test");
206        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category.length === 0 &&
207                b1.ice_getFacet() === "facet" && b1.ice_getAdapterId() === "test");
208        b1 = communicator.stringToProxy("test -f 'facet@test'");
209        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category.length === 0 &&
210                b1.ice_getFacet() === "facet@test" && b1.ice_getAdapterId().length === 0);
211        b1 = communicator.stringToProxy("test -f 'facet@test'@test");
212        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category.length === 0 &&
213                b1.ice_getFacet() === "facet@test" && b1.ice_getAdapterId() === "test");
214
215        try
216        {
217            b1 = communicator.stringToProxy("test -f facet@test @test");
218            test(false);
219        }
220        catch(ex)
221        {
222            test(ex instanceof Ice.ProxyParseException, ex);
223        }
224        b1 = communicator.stringToProxy("test");
225        test(b1.ice_isTwoway());
226        b1 = communicator.stringToProxy("test -t");
227        test(b1.ice_isTwoway());
228        b1 = communicator.stringToProxy("test -o");
229        test(b1.ice_isOneway());
230        b1 = communicator.stringToProxy("test -O");
231        test(b1.ice_isBatchOneway());
232        b1 = communicator.stringToProxy("test -d");
233        test(b1.ice_isDatagram());
234        b1 = communicator.stringToProxy("test -D");
235        test(b1.ice_isBatchDatagram());
236        b1 = communicator.stringToProxy("test");
237        test(!b1.ice_isSecure());
238        b1 = communicator.stringToProxy("test -s");
239        test(b1.ice_isSecure());
240
241        test(b1.ice_getEncodingVersion().equals(Ice.currentEncoding()));
242
243        b1 = communicator.stringToProxy("test -e 1.0");
244        test(b1.ice_getEncodingVersion().major === 1 && b1.ice_getEncodingVersion().minor === 0);
245
246        b1 = communicator.stringToProxy("test -e 6z.5");
247        test(b1.ice_getEncodingVersion().major === 6 && b1.ice_getEncodingVersion().minor === 5);
248
249        b1 = communicator.stringToProxy("test -p 1.0 -e 1.0");
250        test(b1.toString() === "test -t -e 1.0");
251
252        b1 = communicator.stringToProxy("test -p 6.5 -e 1.0");
253        test(b1.toString() === "test -t -p 6.5 -e 1.0");
254
255        try
256        {
257            b1 = communicator.stringToProxy("test:" + defaultProtocol + "@adapterId");
258            test(false);
259        }
260        catch(ex)
261        {
262            test(ex instanceof Ice.EndpointParseException, ex);
263        }
264
265        try
266        {
267            b1 = communicator.stringToProxy("test::" + defaultProtocol);
268            test(false);
269        }
270        catch(ex)
271        {
272            test(ex instanceof Ice.EndpointParseException, ex);
273        }
274
275        //
276        // Test for bug ICE-5543: escaped escapes in stringToIdentity
277        //
278
279        let id = new Ice.Identity("test", ",X2QNUAzSBcJ_e$AV;E\\");
280        let id2 = Ice.stringToIdentity(Ice.identityToString(id));
281        test(id.equals(id2));
282
283        id = new Ice.Identity("test", ",X2QNUAz\\SB\\/cJ_e$AV;E\\\\");
284        id2 = Ice.stringToIdentity(Ice.identityToString(id));
285        test(id.equals(id2));
286
287        id = new Ice.Identity("/test", "cat/");
288        let idStr = Ice.identityToString(id);
289        test(idStr === "cat\\//\\/test");
290        id2 = Ice.stringToIdentity(idStr);
291        test(id.equals(id2));
292
293        // Input string with various pitfalls
294        // id = Ice.stringToIdentity("\\342\\x82\\254\\60\\x9\\60\\");
295        // test(id.name === "€0\t0\\" && id.category.isEmpty());
296
297        try
298        {
299            // Illegal character < 32
300            id = Ice.stringToIdentity("xx\x01FooBar");
301            test(false);
302        }
303        catch(ex)
304        {
305            test(ex instanceof Ice.IdentityParseException);
306        }
307
308        try
309        {
310            // Illegal surrogate
311            id = Ice.stringToIdentity("xx\\ud911");
312            test(false);
313        }
314        catch(ex)
315        {
316            test(ex instanceof Ice.IdentityParseException, ex);
317        }
318
319        // Testing bytes 127 (\x7F) and €
320        id = new Ice.Identity("test", "\x7F€");
321
322        idStr = Ice.identityToString(id, Ice.ToStringMode.Unicode);
323        test(idStr === "\\u007f€/test");
324        id2 = Ice.stringToIdentity(idStr);
325        test(id.equals(id2));
326        test(Ice.identityToString(id) === idStr);
327
328        idStr = Ice.identityToString(id, Ice.ToStringMode.ASCII);
329        test(idStr === "\\u007f\\u20ac/test");
330        id2 = Ice.stringToIdentity(idStr);
331        test(id.equals(id2));
332
333        idStr = Ice.identityToString(id, Ice.ToStringMode.Compat);
334        test(idStr === "\\177\\342\\202\\254/test");
335        id2 = Ice.stringToIdentity(idStr);
336        test(id.equals(id2));
337
338        id2 = Ice.stringToIdentity(communicator.identityToString(id));
339        test(id.equals(id2));
340
341        // More unicode characters
342
343        id = new Ice.Identity("banana \x0e-\ud83c\udf4c\u20ac\u00a2\u0024", "greek \ud800\udd6a");
344
345        idStr = Ice.identityToString(id, Ice.ToStringMode.Unicode);
346        test(idStr === "greek \ud800\udd6a/banana \\u000e-\ud83c\udf4c\u20ac\u00a2$");
347        id2 = Ice.stringToIdentity(idStr);
348        test(id.equals(id2));
349
350        idStr = Ice.identityToString(id, Ice.ToStringMode.ASCII);
351        test(idStr === "greek \\U0001016a/banana \\u000e-\\U0001f34c\\u20ac\\u00a2$");
352        id2 = Ice.stringToIdentity(idStr);
353        test(id.equals(id2));
354
355        idStr = Ice.identityToString(id, Ice.ToStringMode.Compat);
356        test(idStr === "greek \\360\\220\\205\\252/banana \\016-\\360\\237\\215\\214\\342\\202\\254\\302\\242$");
357        id2 = Ice.stringToIdentity(idStr);
358        test(id.equals(id2));
359
360        out.writeLine("ok");
361
362        out.write("testing propertyToProxy... ");
363        const prop = communicator.getProperties();
364        const propertyPrefix = "Foo.Proxy";
365        prop.setProperty(propertyPrefix, "test:" + this.getTestEndpoint());
366        b1 = communicator.propertyToProxy(propertyPrefix);
367        test(b1.ice_getIdentity().name === "test" && b1.ice_getIdentity().category.length === 0 &&
368                b1.ice_getAdapterId().length === 0 && b1.ice_getFacet().length === 0);
369
370        let property = propertyPrefix + ".Locator";
371        test(b1.ice_getLocator() === null);
372        prop.setProperty(property, "locator:default -p 10000");
373        b1 = communicator.propertyToProxy(propertyPrefix);
374        test(b1.ice_getLocator() !== null && b1.ice_getLocator().ice_getIdentity().name === "locator");
375        prop.setProperty(property, "");
376
377        property = propertyPrefix + ".LocatorCacheTimeout";
378        test(b1.ice_getLocatorCacheTimeout() === -1);
379        prop.setProperty(property, "1");
380        b1 = communicator.propertyToProxy(propertyPrefix);
381        test(b1.ice_getLocatorCacheTimeout() === 1);
382        prop.setProperty(property, "");
383
384        // Now retest with an indirect proxy.
385        prop.setProperty(propertyPrefix, "test");
386        property = propertyPrefix + ".Locator";
387        prop.setProperty(property, "locator:default -p 10000");
388        b1 = communicator.propertyToProxy(propertyPrefix);
389        test(b1.ice_getLocator() !== null && b1.ice_getLocator().ice_getIdentity().name === "locator");
390        prop.setProperty(property, "");
391
392        property = propertyPrefix + ".LocatorCacheTimeout";
393        test(b1.ice_getLocatorCacheTimeout() === -1);
394        prop.setProperty(property, "1");
395        b1 = communicator.propertyToProxy(propertyPrefix);
396        test(b1.ice_getLocatorCacheTimeout() === 1);
397        prop.setProperty(property, "");
398
399        prop.setProperty(propertyPrefix, "test:" + this.getTestEndpoint());
400
401        property = propertyPrefix + ".Router";
402        test(b1.ice_getRouter() === null);
403        prop.setProperty(property, "router:default -p 10000");
404        b1 = communicator.propertyToProxy(propertyPrefix);
405        test(b1.ice_getRouter() !== null && b1.ice_getRouter().ice_getIdentity().name === "router");
406        prop.setProperty(property, "");
407
408        property = propertyPrefix + ".PreferSecure";
409        test(!b1.ice_isPreferSecure());
410        prop.setProperty(property, "1");
411        b1 = communicator.propertyToProxy(propertyPrefix);
412        test(b1.ice_isPreferSecure());
413        prop.setProperty(property, "");
414
415        property = propertyPrefix + ".ConnectionCached";
416        test(b1.ice_isConnectionCached());
417        prop.setProperty(property, "0");
418        b1 = communicator.propertyToProxy(propertyPrefix);
419        test(!b1.ice_isConnectionCached());
420        prop.setProperty(property, "");
421
422        property = propertyPrefix + ".InvocationTimeout";
423        test(b1.ice_getInvocationTimeout() == -1);
424        prop.setProperty(property, "1000");
425        b1 = communicator.propertyToProxy(propertyPrefix);
426        test(b1.ice_getInvocationTimeout() == 1000);
427        prop.setProperty(property, "");
428
429        property = propertyPrefix + ".EndpointSelection";
430        test(b1.ice_getEndpointSelection() === Ice.EndpointSelectionType.Random);
431        prop.setProperty(property, "Random");
432        b1 = communicator.propertyToProxy(propertyPrefix);
433        test(b1.ice_getEndpointSelection() === Ice.EndpointSelectionType.Random);
434        prop.setProperty(property, "Ordered");
435        b1 = communicator.propertyToProxy(propertyPrefix);
436        test(b1.ice_getEndpointSelection() === Ice.EndpointSelectionType.Ordered);
437        prop.setProperty(property, "");
438
439        out.writeLine("ok");
440
441        out.write("testing proxyToProperty... ");
442
443        b1 = communicator.stringToProxy("test");
444        b1 = b1.ice_connectionCached(true);
445        b1 = b1.ice_preferSecure(false);
446        b1 = b1.ice_endpointSelection(Ice.EndpointSelectionType.Ordered);
447        b1 = b1.ice_locatorCacheTimeout(100);
448        b1 = b1.ice_invocationTimeout(1234);
449        b1 = b1.ice_encodingVersion(new Ice.EncodingVersion(1, 0));
450
451        let router = communicator.stringToProxy("router");
452        router = router.ice_connectionCached(true);
453        router = router.ice_preferSecure(true);
454        router = router.ice_endpointSelection(Ice.EndpointSelectionType.Random);
455        router = router.ice_locatorCacheTimeout(200);
456        router = router.ice_invocationTimeout(1500);
457
458        let locator = communicator.stringToProxy("locator");
459        locator = locator.ice_connectionCached(false);
460        locator = locator.ice_preferSecure(true);
461        locator = locator.ice_endpointSelection(Ice.EndpointSelectionType.Random);
462        locator = locator.ice_locatorCacheTimeout(300);
463        locator = locator.ice_invocationTimeout(1500);
464
465        locator = locator.ice_router(Ice.RouterPrx.uncheckedCast(router));
466        b1 = b1.ice_locator(Ice.LocatorPrx.uncheckedCast(locator));
467
468        const proxyProps = communicator.proxyToProperty(b1, "Test");
469        test(proxyProps.size === 21);
470        test(proxyProps.get("Test") === "test -t -e 1.0");
471        test(proxyProps.get("Test.CollocationOptimized") === "0");
472        test(proxyProps.get("Test.ConnectionCached") === "1");
473        test(proxyProps.get("Test.PreferSecure") === "0");
474        test(proxyProps.get("Test.EndpointSelection") === "Ordered");
475        test(proxyProps.get("Test.LocatorCacheTimeout") === "100");
476        test(proxyProps.get("Test.InvocationTimeout") === "1234");
477
478        test(proxyProps.get("Test.Locator") === "locator -t -e " +
479                Ice.encodingVersionToString(Ice.currentEncoding()));
480        test(proxyProps.get("Test.Locator.CollocationOptimized") === "0");
481        test(proxyProps.get("Test.Locator.ConnectionCached") === "0");
482        test(proxyProps.get("Test.Locator.PreferSecure") === "1");
483        test(proxyProps.get("Test.Locator.EndpointSelection") === "Random");
484        test(proxyProps.get("Test.Locator.LocatorCacheTimeout") === "300");
485        test(proxyProps.get("Test.Locator.InvocationTimeout") === "1500");
486
487        test(proxyProps.get("Test.Locator.Router") === "router -t -e " +
488                Ice.encodingVersionToString(Ice.currentEncoding()));
489        test(proxyProps.get("Test.Locator.Router.CollocationOptimized") === "0");
490        test(proxyProps.get("Test.Locator.Router.ConnectionCached") === "1");
491        test(proxyProps.get("Test.Locator.Router.PreferSecure") === "1");
492        test(proxyProps.get("Test.Locator.Router.EndpointSelection") === "Random");
493        test(proxyProps.get("Test.Locator.Router.LocatorCacheTimeout") === "200");
494        test(proxyProps.get("Test.Locator.Router.InvocationTimeout") === "1500");
495
496        out.writeLine("ok");
497
498        out.write("testing ice_getCommunicator... ");
499        test(base.ice_getCommunicator() === communicator);
500        out.writeLine("ok");
501
502        out.write("testing proxy methods... ");
503        test(communicator.identityToString(
504            base.ice_identity(Ice.stringToIdentity("other")).ice_getIdentity()) === "other");
505        test(Ice.identityToString(
506            base.ice_identity(Ice.stringToIdentity("other")).ice_getIdentity()) === "other");
507        test(base.ice_facet("facet").ice_getFacet() === "facet");
508        test(base.ice_adapterId("id").ice_getAdapterId() === "id");
509        test(base.ice_twoway().ice_isTwoway());
510        test(base.ice_oneway().ice_isOneway());
511        test(base.ice_batchOneway().ice_isBatchOneway());
512        test(base.ice_datagram().ice_isDatagram());
513        test(base.ice_batchDatagram().ice_isBatchDatagram());
514        test(base.ice_secure(true).ice_isSecure());
515        test(!base.ice_secure(false).ice_isSecure());
516        test(base.ice_preferSecure(true).ice_isPreferSecure());
517        test(!base.ice_preferSecure(false).ice_isPreferSecure());
518        test(base.ice_encodingVersion(Ice.Encoding_1_0).ice_getEncodingVersion().equals(Ice.Encoding_1_0));
519        test(base.ice_encodingVersion(Ice.Encoding_1_1).ice_getEncodingVersion().equals(Ice.Encoding_1_1));
520        test(!base.ice_encodingVersion(Ice.Encoding_1_0).ice_getEncodingVersion().equals(Ice.Encoding_1_1));
521
522        try
523        {
524            base.ice_timeout(0);
525            test(false);
526        }
527        catch(ex)
528        {
529            test(!(ex instanceof TestError), ex);
530        }
531
532        try
533        {
534            base.ice_timeout(-1);
535        }
536        catch(ex)
537        {
538            test(false, ex);
539        }
540
541        try
542        {
543            base.ice_timeout(-2);
544            test(false);
545        }
546        catch(ex)
547        {
548            test(!(ex instanceof TestError), ex);
549        }
550
551        try
552        {
553            base.ice_invocationTimeout(0);
554            test(false);
555        }
556        catch(ex)
557        {
558            test(!(ex instanceof TestError), ex);
559        }
560
561        try
562        {
563            base.ice_invocationTimeout(-1);
564        }
565        catch(ex)
566        {
567            test(false);
568        }
569
570        try
571        {
572            base.ice_invocationTimeout(-2);
573            test(false);
574        }
575        catch(ex)
576        {
577            test(!(ex instanceof TestError), ex);
578        }
579
580        try
581        {
582            base.ice_locatorCacheTimeout(0);
583        }
584        catch(ex)
585        {
586            test(false, ex);
587        }
588
589        try
590        {
591            base.ice_locatorCacheTimeout(-1);
592        }
593        catch(ex)
594        {
595            test(false, ex);
596        }
597
598        try
599        {
600            base.ice_locatorCacheTimeout(-2);
601            test(false);
602        }
603        catch(ex)
604        {
605            test(!(ex instanceof TestError), ex);
606        }
607
608        out.writeLine("ok");
609
610        out.write("testing proxy comparison... ");
611
612        test(communicator.stringToProxy("foo").equals(communicator.stringToProxy("foo")));
613        test(!communicator.stringToProxy("foo").equals(communicator.stringToProxy("foo2")));
614
615        const compObj = communicator.stringToProxy("foo");
616
617        test(compObj.ice_facet("facet").equals(compObj.ice_facet("facet")));
618        test(!compObj.ice_facet("facet").equals(compObj.ice_facet("facet1")));
619
620        test(compObj.ice_oneway().equals(compObj.ice_oneway()));
621        test(!compObj.ice_oneway().equals(compObj.ice_twoway()));
622
623        test(compObj.ice_secure(true).equals(compObj.ice_secure(true)));
624        test(!compObj.ice_secure(false).equals(compObj.ice_secure(true)));
625
626        test(compObj.ice_connectionCached(true).equals(compObj.ice_connectionCached(true)));
627        test(!compObj.ice_connectionCached(false).equals(compObj.ice_connectionCached(true)));
628
629        test(compObj.ice_endpointSelection(Ice.EndpointSelectionType.Random).equals(
630            compObj.ice_endpointSelection(Ice.EndpointSelectionType.Random)));
631        test(!compObj.ice_endpointSelection(Ice.EndpointSelectionType.Random).equals(
632            compObj.ice_endpointSelection(Ice.EndpointSelectionType.Ordered)));
633
634        test(compObj.ice_connectionId("id2").equals(compObj.ice_connectionId("id2")));
635        test(!compObj.ice_connectionId("id1").equals(compObj.ice_connectionId("id2")));
636
637        test(compObj.ice_connectionId("id1").ice_getConnectionId() === "id1");
638        test(compObj.ice_connectionId("id2").ice_getConnectionId() === "id2");
639
640        test(compObj.ice_timeout(20).equals(compObj.ice_timeout(20)));
641        test(!compObj.ice_timeout(10).equals(compObj.ice_timeout(20)));
642
643        test(compObj.ice_getTimeout() === undefined);
644        test(compObj.ice_timeout(10).ice_getTimeout() == 10);
645        test(compObj.ice_timeout(20).ice_getTimeout() == 20);
646
647        const loc1 = Ice.LocatorPrx.uncheckedCast(communicator.stringToProxy("loc1:default -p 10000"));
648        const loc2 = Ice.LocatorPrx.uncheckedCast(communicator.stringToProxy("loc2:default -p 10000"));
649        test(compObj.ice_locator(null).equals(compObj.ice_locator(null)));
650        test(compObj.ice_locator(loc1).equals(compObj.ice_locator(loc1)));
651        test(!compObj.ice_locator(loc1).equals(compObj.ice_locator(null)));
652        test(!compObj.ice_locator(null).equals(compObj.ice_locator(loc2)));
653        test(!compObj.ice_locator(loc1).equals(compObj.ice_locator(loc2)));
654
655        const rtr1 = Ice.RouterPrx.uncheckedCast(communicator.stringToProxy("rtr1:default -p 10000"));
656        const rtr2 = Ice.RouterPrx.uncheckedCast(communicator.stringToProxy("rtr2:default -p 10000"));
657        test(compObj.ice_router(null).equals(compObj.ice_router(null)));
658        test(compObj.ice_router(rtr1).equals(compObj.ice_router(rtr1)));
659        test(!compObj.ice_router(rtr1).equals(compObj.ice_router(null)));
660        test(!compObj.ice_router(null).equals(compObj.ice_router(rtr2)));
661        test(!compObj.ice_router(rtr1).equals(compObj.ice_router(rtr2)));
662
663        const ctx1 = new Map();
664        ctx1.set("ctx1", "v1");
665        const ctx2 = new Map();
666        ctx2.set("ctx2", "v2");
667        test(compObj.ice_context(null).equals(compObj.ice_context(null)));
668        test(compObj.ice_context(ctx1).equals(compObj.ice_context(ctx1)));
669        test(!compObj.ice_context(ctx1).equals(compObj.ice_context(null)));
670        test(!compObj.ice_context(null).equals(compObj.ice_context(ctx2)));
671        test(!compObj.ice_context(ctx1).equals(compObj.ice_context(ctx2)));
672
673        test(compObj.ice_preferSecure(true).equals(compObj.ice_preferSecure(true)));
674        test(!compObj.ice_preferSecure(true).equals(compObj.ice_preferSecure(false)));
675
676        let compObj1 = communicator.stringToProxy("foo:" + defaultProtocol + " -h 127.0.0.1 -p 10000");
677        let compObj2 = communicator.stringToProxy("foo:" + defaultProtocol + " -h 127.0.0.1 -p 10001");
678        test(!compObj1.equals(compObj2));
679
680        compObj1 = communicator.stringToProxy("foo@MyAdapter1");
681        compObj2 = communicator.stringToProxy("foo@MyAdapter2");
682        test(!compObj1.equals(compObj2));
683
684        test(compObj1.ice_locatorCacheTimeout(20).equals(compObj1.ice_locatorCacheTimeout(20)));
685        test(!compObj1.ice_locatorCacheTimeout(10).equals(compObj1.ice_locatorCacheTimeout(20)));
686
687        test(compObj1.ice_invocationTimeout(20).equals(compObj1.ice_invocationTimeout(20)));
688        test(!compObj1.ice_invocationTimeout(10).equals(compObj1.ice_invocationTimeout(20)));
689
690        compObj1 = communicator.stringToProxy("foo:" + defaultProtocol + " -h 127.0.0.1 -p 1000");
691        compObj2 = communicator.stringToProxy("foo@MyAdapter1");
692        test(!compObj1.equals(compObj2));
693
694        const endpts1 =
695                communicator.stringToProxy(`foo:${defaultProtocol} -h 127.0.0.1 -p 10000`).ice_getEndpoints();
696        const endpts2 =
697                communicator.stringToProxy(`foo:${defaultProtocol} -h 127.0.0.1 -p 10001`).ice_getEndpoints();
698        test(!endpts1[0].equals(endpts2[0]));
699        test(endpts1[0].equals(
700            communicator.stringToProxy(`foo:${defaultProtocol} -h 127.0.0.1 -p 10000`).ice_getEndpoints()[0]));
701
702        test(compObj1.ice_encodingVersion(Ice.Encoding_1_0).equals(compObj1.ice_encodingVersion(Ice.Encoding_1_0)));
703        test(!compObj1.ice_encodingVersion(Ice.Encoding_1_0).equals(
704            compObj1.ice_encodingVersion(Ice.Encoding_1_1)));
705
706        const baseConnection = await base.ice_getConnection();
707        if(baseConnection !== null)
708        {
709            const baseConnection2 = await base.ice_connectionId("base2").ice_getConnection();
710            compObj1 = compObj1.ice_fixed(baseConnection);
711            compObj2 = compObj2.ice_fixed(baseConnection2);
712            test(!compObj1.equals(compObj2));
713        }
714        out.writeLine("ok");
715
716        out.write("testing checked cast... ");
717        const cl = await Test.MyClassPrx.checkedCast(base);
718        test(cl !== null);
719        let derived = await Test.MyDerivedClassPrx.checkedCast(cl);
720        test(derived !== null);
721        test(cl.equals(base));
722        test(derived.equals(base));
723        test(cl.equals(derived));
724        out.writeLine("ok");
725
726        out.write("testing checked cast with context... ");
727        let c = await cl.getContext();
728        test(c === null || c.size == 0);
729        c = new Map();
730        c.set("one", "hello");
731        c.set("two", "world");
732        const clc = await Test.MyClassPrx.checkedCast(base, undefined, c);
733        const c2 = await clc.getContext();
734        test(Ice.MapUtil.equals(c, c2));
735        out.writeLine("ok");
736
737        out.write("testing ice_fixed... ");
738        {
739            const connection = await cl.ice_getConnection();
740            if(connection !== null)
741            {
742                await cl.ice_fixed(connection).getContext();
743                test(cl.ice_secure(true).ice_fixed(connection).ice_isSecure());
744                test(cl.ice_facet("facet").ice_fixed(connection).ice_getFacet() == "facet");
745                test(cl.ice_oneway().ice_fixed(connection).ice_isOneway());
746                const ctx = new Map();
747                ctx.set("one", "hello");
748                ctx.set("two", "world");
749                test(cl.ice_fixed(connection).ice_getContext().size == 0);
750                test(cl.ice_context(ctx).ice_fixed(connection).ice_getContext().size == 2);
751                test(cl.ice_fixed(connection).ice_getInvocationTimeout() == -1);
752                test(cl.ice_invocationTimeout(10).ice_fixed(connection).ice_getInvocationTimeout() == 10);
753                test(await cl.ice_fixed(connection).ice_getConnection() == connection);
754                test(await cl.ice_fixed(connection).ice_fixed(connection).ice_getConnection() == connection);
755                test(cl.ice_fixed(connection).ice_getTimeout() === undefined);
756                const fixedConnection = await cl.ice_connectionId("ice_fixed").ice_getConnection();
757                test(await cl.ice_fixed(connection).ice_fixed(fixedConnection).ice_getConnection() ==
758                        fixedConnection);
759                try
760                {
761                    await cl.ice_secure(!connection.getEndpoint().getInfo().secure()).ice_fixed(connection).ice_ping();
762                }
763                catch(ex)
764                {
765                    test(ex instanceof Ice.NoEndpointException);
766                }
767                try
768                {
769                    await cl.ice_datagram().ice_fixed(connection).ice_ping();
770                }
771                catch(ex)
772                {
773                    test(ex instanceof Ice.NoEndpointException);
774                }
775
776            }
777            else
778            {
779                try
780                {
781                    cl.ice_fixed(connection);
782                    test(false);
783                }
784                catch(ex)
785                {
786                    // Expected with null connection.
787                }
788            }
789        }
790        out.writeLine("ok");
791
792        out.write("testing encoding versioning... ");
793
794        let ref20 = "test -e 2.0:" + this.getTestEndpoint();
795        let cl20 = Test.MyClassPrx.uncheckedCast(communicator.stringToProxy(ref20));
796        try
797        {
798            await cl20.ice_ping();
799            test(false);
800        }
801        catch(ex)
802        {
803            test(ex instanceof Ice.UnsupportedEncodingException, ex);
804        }
805
806        let ref10 = "test -e 1.0:" + this.getTestEndpoint();
807        let cl10 = Test.MyClassPrx.uncheckedCast(communicator.stringToProxy(ref10));
808
809        await cl10.ice_ping();
810        await cl10.ice_encodingVersion(Ice.Encoding_1_0).ice_ping();
811        await cl.ice_encodingVersion(Ice.Encoding_1_0).ice_ping();
812
813        // 1.3 isn't supported but since a 1.3 proxy supports 1.1, the
814        // call will use the 1.1 encoding
815        let ref13 = "test -e 1.3:" + this.getTestEndpoint();
816        let cl13 = Test.MyClassPrx.uncheckedCast(communicator.stringToProxy(ref13));
817        await cl13.ice_ping();
818
819        // TODO port ice_invoke test
820        out.writeLine("ok");
821
822        out.write("testing protocol versioning... ");
823        ref20 = "test -p 2.0:" + this.getTestEndpoint();
824        cl20 = Test.MyClassPrx.uncheckedCast(communicator.stringToProxy(ref20));
825        try
826        {
827            await cl20.ice_ping();
828            test(false);
829        }
830        catch(ex)
831        {
832            // Server 2.0 proxy doesn't support 1.0 version.
833            test(ex instanceof Ice.UnsupportedProtocolException, ex);
834        }
835
836        ref10 = "test -p 1.0:" + this.getTestEndpoint();
837        cl10 = Test.MyClassPrx.uncheckedCast(communicator.stringToProxy(ref10));
838        await cl10.ice_ping();
839
840        // 1.3 isn't supported but since a 1.3 proxy supports 1.1, the
841        // call will use the 1.1 protocol
842        ref13 = "test -p 1.3:" + this.getTestEndpoint();
843        cl13 = Test.MyClassPrx.uncheckedCast(communicator.stringToProxy(ref13));
844        await cl13.ice_ping();
845        out.writeLine("ok");
846
847        out.write("testing opaque endpoints... ");
848
849        try
850        {
851            // Invalid -x option
852            communicator.stringToProxy("id:opaque -t 99 -v abc -x abc");
853            test(false);
854        }
855        catch(ex)
856        {
857            test(ex instanceof Ice.EndpointParseException, ex);
858        }
859
860        try
861        {
862            // Missing -t and -v
863            communicator.stringToProxy("id:opaque");
864            test(false);
865        }
866        catch(ex)
867        {
868            test(ex instanceof Ice.EndpointParseException, ex);
869        }
870
871        try
872        {
873            // Repeated -t
874            communicator.stringToProxy("id:opaque -t 1 -t 1 -v abc");
875            test(false);
876        }
877        catch(ex)
878        {
879            test(ex instanceof Ice.EndpointParseException, ex);
880        }
881
882        try
883        {
884            // Repeated -v
885            communicator.stringToProxy("id:opaque -t 1 -v abc -v abc");
886            test(false);
887        }
888        catch(ex)
889        {
890            test(ex instanceof Ice.EndpointParseException, ex);
891        }
892
893        try
894        {
895            // Missing -t
896            communicator.stringToProxy("id:opaque -v abc");
897            test(false);
898        }
899        catch(ex)
900        {
901            test(ex instanceof Ice.EndpointParseException, ex);
902        }
903
904        try
905        {
906            // Missing -v
907            communicator.stringToProxy("id:opaque -t 1");
908            test(false);
909        }
910        catch(ex)
911        {
912            test(ex instanceof Ice.EndpointParseException, ex);
913        }
914
915        try
916        {
917            // Missing arg for -t
918            communicator.stringToProxy("id:opaque -t -v abc");
919            test(false);
920        }
921        catch(ex)
922        {
923            test(ex instanceof Ice.EndpointParseException, ex);
924        }
925
926        try
927        {
928            // Missing arg for -v
929            communicator.stringToProxy("id:opaque -t 1 -v");
930            test(false);
931        }
932        catch(ex)
933        {
934            test(ex instanceof Ice.EndpointParseException, ex);
935        }
936
937        try
938        {
939            // Not a number for -t
940            communicator.stringToProxy("id:opaque -t x -v abc");
941            test(false);
942        }
943        catch(ex)
944        {
945            test(ex instanceof Ice.EndpointParseException, ex);
946        }
947
948        try
949        {
950            // < 0 for -t
951            communicator.stringToProxy("id:opaque -t -1 -v abc");
952            test(false);
953        }
954        catch(ex)
955        {
956            test(ex instanceof Ice.EndpointParseException, ex);
957        }
958
959        try
960        {
961            // Invalid char for -v
962            communicator.stringToProxy("id:opaque -t 99 -v x?c");
963            test(false);
964        }
965        catch(ex)
966        {
967            test(ex instanceof Ice.EndpointParseException, ex);
968        }
969
970        // Legal TCP endpoint expressed as opaque endpoint
971        let p1 = communicator.stringToProxy("test -e 1.1:opaque -e 1.0 -t 1 -v CTEyNy4wLjAuMeouAAAQJwAAAA==");
972        let pstr = communicator.proxyToString(p1);
973        test(pstr === "test -t -e 1.1:tcp -h 127.0.0.1 -p 12010 -t 10000");
974
975        // Legal WS endpoint expressed as opaque endpoint
976        p1 = communicator.stringToProxy("test -e 1.1:opaque -t 4 -e 1.0 -v CTEyNy4wLjAuMeouAAAQJwAAAAA=");
977        pstr = communicator.proxyToString(p1);
978        test(pstr === "test -t -e 1.1:ws -h 127.0.0.1 -p 12010 -t 10000");
979
980        // Opaque endpoint encoded with 1.1 encoding.
981
982        let p2 = communicator.stringToProxy("test:opaque -e 1.1 -t 1 -v CTEyNy4wLjAuMeouAAAQJwAAAA==");
983        test(communicator.proxyToString(p2) === "test -t -e 1.1:tcp -h 127.0.0.1 -p 12010 -t 10000");
984
985        p2 = communicator.stringToProxy("test:opaque -e 1.1 -t 4 -v CTEyNy4wLjAuMeouAAAQJwAAAAA=");
986        test(communicator.proxyToString(p2) === "test -t -e 1.1:ws -h 127.0.0.1 -p 12010 -t 10000");
987
988        if(communicator.getProperties().getPropertyAsInt("Ice.IPv6") === 0)
989        {
990            const ref = "test:" + this.getTestEndpoint();
991
992            const ssl = communicator.getProperties().getProperty("Ice.Default.Protocol") === "ssl";
993            // TODO: p1 contains 127.0.0.1 - OK to invoke?
994            //   if(!ssl)
995            //   {
996            //       p1.ice_encodingVersion(Ice.Util.Encoding_1_0).ice_ping();
997            //   }
998
999            // Two legal TCP endpoints expressed as opaque endpoints
1000            p1 = communicator.stringToProxy("test -e 1.0:opaque -e 1.0 -t 1 -v CTEyNy4wLjAuMeouAAAQJwAAAA==:opaque -e 1.0 -t 1 -v CTEyNy4wLjAuMusuAAAQJwAAAA==");
1001            pstr = communicator.proxyToString(p1);
1002            test(pstr === "test -t -e 1.0:tcp -h 127.0.0.1 -p 12010 -t 10000:tcp -h 127.0.0.2 -p 12011 -t 10000");
1003
1004            p1 = communicator.stringToProxy("test -e 1.0:opaque -t 4 -e 1.0 -v CTEyNy4wLjAuMeouAAAQJwAAAAA=:opaque -t 4 -e 1.0 -v CTEyNy4wLjAuMusuAAAQJwAAAAA=");
1005            pstr = communicator.proxyToString(p1);
1006            test(pstr === "test -t -e 1.0:ws -h 127.0.0.1 -p 12010 -t 10000:ws -h 127.0.0.2 -p 12011 -t 10000");
1007
1008            //
1009            // Test that an SSL endpoint and a nonsense endpoint get
1010            // written back out as an opaque endpoint.
1011            //
1012            p1 = communicator.stringToProxy("test -e 1.0:opaque -e 1.0 -t 2 -v CTEyNy4wLjAuMREnAAD/////AA==:opaque -t 99 -e 1.0 -v abch");
1013            pstr = communicator.proxyToString(p1);
1014            test(pstr === "test -t -e 1.0:ssl -h 127.0.0.1 -p 10001 -t infinite:opaque -t 99 -e 1.0 -v abch");
1015
1016            //
1017            // Try to invoke on the SSL endpoint to verify that we get a
1018            // NoEndpointException (or ConnectFailedException when
1019            // running with SSL).
1020            //
1021            try
1022            {
1023                await p1.ice_encodingVersion(Ice.Encoding_1_0).ice_ping();
1024                test(false);
1025            }
1026            catch(ex)
1027            {
1028                if(ex instanceof Ice.NoEndpointException)
1029                {
1030                    test(!ssl);
1031                }
1032                else if(ex instanceof Ice.ConnectFailedException)
1033                {
1034                    test(ssl);
1035                }
1036                else
1037                {
1038                    throw ex;
1039                }
1040            }
1041            //
1042            // Test that the proxy with an SSL endpoint and a nonsense
1043            // endpoint (which the server doesn't understand either) can
1044            // be sent over the wire and returned by the server without
1045            // losing the opaque endpoints.
1046            //
1047            derived = Test.MyDerivedClassPrx.uncheckedCast(
1048                communicator.stringToProxy("test -e 1.0:" + this.getTestEndpoint()));
1049            p2 = await derived.echo(p1);
1050
1051            pstr = communicator.proxyToString(p2);
1052            test(pstr === "test -t -e 1.0:ssl -h 127.0.0.1 -p 10001 -t infinite:opaque -t 99 -e 1.0 -v abch");
1053
1054            let p = communicator.stringToProxy("test:" + this.getTestEndpoint());
1055            if(defaultProtocol === "tcp")
1056            {
1057                test(p.ice_getEndpoints()[0].getInfo() instanceof Ice.TCPEndpointInfo);
1058            }
1059            else if(defaultProtocol === "ws" || defaultProtocol === "wss")
1060            {
1061                test(p.ice_getEndpoints()[0].getInfo() instanceof Ice.WSEndpointInfo);
1062            }
1063
1064            let con = await p.ice_getConnection();
1065            if(defaultProtocol === "tcp")
1066            {
1067                test(con.getInfo() instanceof Ice.TCPConnectionInfo);
1068            }
1069            else if(defaultProtocol === "ws" || defaultProtocol === "wss")
1070            {
1071                test(con.getInfo() instanceof Ice.WSConnectionInfo);
1072            }
1073
1074            //
1075            // Ensure that non connectable endpoints are skipped.
1076            //
1077            p = communicator.stringToProxy("test:" + this.getTestEndpoint("ws") + ":" + this.getTestEndpoint());
1078
1079            p = p.ice_endpointSelection(Ice.EndpointSelectionType.Ordered);
1080            await p.ice_ping();
1081
1082            out.writeLine("ok");
1083
1084            out.write("testing proxyToString... ");
1085            b1 = communicator.stringToProxy(ref);
1086            let b2 = communicator.stringToProxy(communicator.proxyToString(b1));
1087            test(b1.equals(b2));
1088
1089            con = await b1.ice_getConnection();
1090            b2 = con.createProxy(Ice.stringToIdentity("fixed"));
1091            const str = communicator.proxyToString(b2);
1092            test(b2.toString() === str);
1093            const str2 = b1.ice_identity(b2.ice_getIdentity()).ice_secure(b2.ice_isSecure()).toString();
1094
1095            // Verify that the stringified fixed proxy is the same as a regular stringified proxy
1096            // but without endpoints
1097            test(str2.startsWith(str));
1098            test(str2.charAt(str.length) === ':');
1099
1100            out.writeLine("ok");
1101
1102            if(defaultProtocol === "ws" || defaultProtocol === "wss")
1103            {
1104                out.write("testing ping invalid WS proxies... ");
1105                //
1106                // Invocation in a WS or WSS proxy that has not hostname set
1107                // will fail creating the WebSocket object.
1108                //
1109                const communicator2 = Ice.initialize();
1110                const invalid = communicator2.stringToProxy("test:" + this.getTestEndpoint());
1111                try
1112                {
1113                    await invalid.ice_ping();
1114                    test(false);
1115                }
1116                catch(ex)
1117                {
1118                    // expected
1119                }
1120                finally
1121                {
1122                    await communicator2.destroy();
1123                }
1124                out.writeLine("ok");
1125            }
1126
1127            out.write("testing communicator shutdown/destroy... ");
1128            {
1129                const c = Ice.initialize();
1130                c.shutdown();
1131                test(c.isShutdown());
1132                await c.waitForShutdown();
1133                await c.destroy();
1134                c.shutdown();
1135                test(c.isShutdown());
1136                await c.waitForShutdown();
1137                await c.destroy();
1138            }
1139            out.writeLine("ok");
1140
1141            derived = Test.MyDerivedClassPrx.uncheckedCast(
1142                communicator.stringToProxy("test:" + this.getTestEndpoint()));
1143            await derived.shutdown();
1144        }
1145    }
1146
1147    async run(args:string[])
1148    {
1149        let communicator:Ice.Communicator;
1150        try
1151        {
1152            [communicator] = this.initialize(args);
1153            await this.allTests();
1154        }
1155        finally
1156        {
1157            if(communicator)
1158            {
1159                await communicator.destroy();
1160            }
1161        }
1162    }
1163}
1164