1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 using Test;
6 using System;
7 using System.Reflection;
8 using System.Threading;
9 
10 [assembly: CLSCompliant(true)]
11 
12 [assembly: AssemblyTitle("IceTest")]
13 [assembly: AssemblyDescription("Ice test")]
14 [assembly: AssemblyCompany("ZeroC, Inc.")]
15 
16 public class Client : Test.TestHelper
17 {
18     public class SessionCallback1 : Glacier2.SessionCallback
19     {
20         public
SessionCallback1(Client app)21         SessionCallback1(Client app)
22         {
23             _app = app;
24         }
25 
26         public void
connected(Glacier2.SessionHelper session)27         connected(Glacier2.SessionHelper session)
28         {
29              test(false);
30         }
31 
32         public void
disconnected(Glacier2.SessionHelper session)33         disconnected(Glacier2.SessionHelper session)
34         {
35             test(false);
36         }
37 
38         public void
connectFailed(Glacier2.SessionHelper session, Exception exception)39         connectFailed(Glacier2.SessionHelper session, Exception exception)
40         {
41             try
42             {
43                 throw exception;
44             }
45             catch(Glacier2.PermissionDeniedException)
46             {
47                 Console.Out.WriteLine("ok");
48                 _app.wakeUp();
49             }
50             catch(Exception)
51             {
52                 test(false);
53             }
54         }
55 
56         public void
createdCommunicator(Glacier2.SessionHelper session)57         createdCommunicator(Glacier2.SessionHelper session)
58         {
59             test(session.communicator() != null);
60         }
61 
62         private Client _app;
63     }
64 
65     public class SessionCallback2 : Glacier2.SessionCallback
66     {
67         public
SessionCallback2(Client app)68         SessionCallback2(Client app)
69         {
70             _app = app;
71         }
72 
73         public void
connected(Glacier2.SessionHelper session)74         connected(Glacier2.SessionHelper session)
75         {
76             Console.Out.WriteLine("ok");
77             _app.wakeUp();
78         }
79 
80         public void
disconnected(Glacier2.SessionHelper session)81         disconnected(Glacier2.SessionHelper session)
82         {
83             Console.Out.WriteLine("ok");
84             _app.wakeUp();
85         }
86 
87         public void
connectFailed(Glacier2.SessionHelper session, Exception ex)88         connectFailed(Glacier2.SessionHelper session, Exception ex)
89         {
90             Console.Out.WriteLine(ex.ToString());
91             test(false);
92         }
93 
94         public void
createdCommunicator(Glacier2.SessionHelper session)95         createdCommunicator(Glacier2.SessionHelper session)
96         {
97             test(session.communicator() != null);
98         }
99 
100         private Client _app;
101     }
102 
103     public class SessionCallback3 : Glacier2.SessionCallback
104     {
105         public
SessionCallback3(Client app)106         SessionCallback3(Client app)
107         {
108             _app = app;
109         }
110 
111         public void
connected(Glacier2.SessionHelper session)112         connected(Glacier2.SessionHelper session)
113         {
114              test(false);
115         }
116 
117         public void
disconnected(Glacier2.SessionHelper session)118         disconnected(Glacier2.SessionHelper session)
119         {
120             test(false);
121         }
122 
123         public void
connectFailed(Glacier2.SessionHelper session, Exception exception)124         connectFailed(Glacier2.SessionHelper session, Exception exception)
125         {
126             try
127             {
128                 throw exception;
129             }
130             catch(Ice.ConnectionRefusedException)
131             {
132                 Console.Out.WriteLine("ok");
133                 _app.wakeUp();
134             }
135             catch(Exception)
136             {
137                 test(false);
138             }
139         }
140 
141         public void
createdCommunicator(Glacier2.SessionHelper session)142         createdCommunicator(Glacier2.SessionHelper session)
143         {
144             test(session.communicator() != null);
145         }
146 
147         private Client _app;
148     }
149 
150     public class SessionCallback4 : Glacier2.SessionCallback
151     {
152         public
SessionCallback4(Client app)153         SessionCallback4(Client app)
154         {
155             _app = app;
156         }
157 
158         public void
connected(Glacier2.SessionHelper session)159         connected(Glacier2.SessionHelper session)
160         {
161              test(false);
162         }
163 
164         public void
disconnected(Glacier2.SessionHelper session)165         disconnected(Glacier2.SessionHelper session)
166         {
167             test(false);
168         }
169 
170         public void
connectFailed(Glacier2.SessionHelper session, Exception exception)171         connectFailed(Glacier2.SessionHelper session, Exception exception)
172         {
173             try
174             {
175                 throw exception;
176             }
177             catch(Ice.CommunicatorDestroyedException)
178             {
179                 Console.Out.WriteLine("ok");
180                 _app.wakeUp();
181             }
182             catch(Exception)
183             {
184                 test(false);
185             }
186         }
187 
188         public void
createdCommunicator(Glacier2.SessionHelper session)189         createdCommunicator(Glacier2.SessionHelper session)
190         {
191             test(session.communicator() != null);
192         }
193 
194         private Client _app;
195     }
196 
run(string[] args)197     public override void run(string[] args)
198     {
199         Ice.InitializationData initData = new Ice.InitializationData();
200         initData.properties = createTestProperties(ref args);
201         initData.properties.setProperty("Ice.Warn.Connections", "0");
202         initData.properties.setProperty("Ice.Default.Router",
203                                         "Glacier2/router:" + getTestEndpoint(initData.properties, 50));
204         initData.dispatcher = delegate(Action action, Ice.Connection connection)
205             {
206                 action();
207             };
208 
209         using(var communicator = initialize(initData))
210         {
211             string protocol = getTestProtocol();
212             string host = getTestHost();
213 
214             Glacier2.SessionFactoryHelper factory = new Glacier2.SessionFactoryHelper(initData, new SessionCallback1(this));
215             Glacier2.SessionHelper session = null;
216 
217             //
218             // Test to create a session with wrong userid/password
219             //
220             lock(this)
221             {
222                 Console.Out.Write("testing SessionHelper connect with wrong userid/password... ");
223                 Console.Out.Flush();
224 
225                 factory.setProtocol(protocol);
226                 session  = factory.connect("userid", "xxx");
227                 while(true)
228                 {
229                     try
230                     {
231                         Monitor.Wait(this, 30000);
232                         break;
233                     }
234                     catch(ThreadInterruptedException)
235                     {
236                     }
237                 }
238                 test(!session.isConnected());
239             }
240 
241             initData.properties.setProperty("Ice.Default.Router", "");
242             factory = new Glacier2.SessionFactoryHelper(initData, new SessionCallback4(this));
243             lock(this)
244             {
245                 Console.Out.Write("testing SessionHelper connect interrupt... ");
246                 Console.Out.Flush();
247                 factory.setRouterHost(host);
248                 factory.setPort(getTestPort(1));
249                 factory.setProtocol(protocol);
250                 session = factory.connect("userid", "abc123");
251 
252                 Thread.Sleep(100);
253                 session.destroy();
254 
255                 while(true)
256                 {
257                     try
258                     {
259                         Monitor.Wait(this, 30000);
260                         break;
261                     }
262                     catch(ThreadInterruptedException)
263                     {
264                     }
265                 }
266                 test(!session.isConnected());
267             }
268 
269             factory = new Glacier2.SessionFactoryHelper(initData, new SessionCallback2(this));
270             lock(this)
271             {
272                 Console.Out.Write("testing SessionHelper connect... ");
273                 Console.Out.Flush();
274                 factory.setRouterHost(host);
275                 factory.setPort(getTestPort(50));
276                 factory.setProtocol(protocol);
277                 session = factory.connect("userid", "abc123");
278                 while(true)
279                 {
280                     try
281                     {
282                         Monitor.Wait(this, 30000);
283                         break;
284                     }
285                     catch(ThreadInterruptedException)
286                     {
287                     }
288                 }
289 
290                 Console.Out.Write("testing SessionHelper isConnected after connect... ");
291                 Console.Out.Flush();
292                 test(session.isConnected());
293                 Console.Out.WriteLine("ok");
294 
295                 Console.Out.Write("testing SessionHelper categoryForClient after connect... ");
296                 Console.Out.Flush();
297                 try
298                 {
299                     test(!session.categoryForClient().Equals(""));
300                 }
301                 catch(Glacier2.SessionNotExistException)
302                 {
303                     test(false);
304                 }
305                 Console.Out.WriteLine("ok");
306 
307                 test(session.session() == null);
308 
309                 Console.Out.Write("testing stringToProxy for server object... ");
310                 Console.Out.Flush();
311                 Ice.ObjectPrx @base = session.communicator().stringToProxy("callback:" + getTestEndpoint(0));
312                 Console.Out.WriteLine("ok");
313 
314                 Console.Out.Write("pinging server after session creation... ");
315                 Console.Out.Flush();
316                 @base.ice_ping();
317                 Console.Out.WriteLine("ok");
318 
319                 Console.Out.Write("testing checked cast for server object... ");
320                 Console.Out.Flush();
321                 CallbackPrx twoway = CallbackPrxHelper.checkedCast(@base);
322                 test(twoway != null);
323                 Console.Out.WriteLine("ok");
324 
325                 Console.Out.Write("testing server shutdown... ");
326                 Console.Out.Flush();
327                 twoway.shutdown();
328                 Console.Out.WriteLine("ok");
329 
330                 test(session.communicator() != null);
331                 Console.Out.Write("testing SessionHelper destroy... ");
332                 Console.Out.Flush();
333                 session.destroy();
334                 while(true)
335                 {
336                     try
337                     {
338                         Monitor.Wait(this);
339                         break;
340                     }
341                     catch(ThreadInterruptedException)
342                     {
343                     }
344                 }
345 
346                 Console.Out.Write("testing SessionHelper isConnected after destroy... ");
347                 Console.Out.Flush();
348                 test(session.isConnected() == false);
349                 Console.Out.WriteLine("ok");
350 
351                 Console.Out.Write("testing SessionHelper categoryForClient after destroy... ");
352                 Console.Out.Flush();
353                 try
354                 {
355                     test(!session.categoryForClient().Equals(""));
356                     test(false);
357                 }
358                 catch(Glacier2.SessionNotExistException)
359                 {
360                 }
361                 Console.Out.WriteLine("ok");
362 
363                 Console.Out.Write("testing SessionHelper session after destroy... ");
364                 test(session.session() == null);
365                 Console.Out.WriteLine("ok");
366 
367                 Console.Out.Write("testing SessionHelper communicator after destroy... ");
368                 Console.Out.Flush();
369                 try
370                 {
371                     test(session.communicator() != null);
372                     session.communicator().stringToProxy("dummy");
373                     test(false);
374                 }
375                 catch(Ice.CommunicatorDestroyedException)
376                 {
377                 }
378                 Console.Out.WriteLine("ok");
379 
380                 Console.Out.Write("uninstalling router with communicator... ");
381                 Console.Out.Flush();
382                 communicator.setDefaultRouter(null);
383                 Console.Out.WriteLine("ok");
384 
385                 Ice.ObjectPrx processBase;
386                 {
387                     Console.Out.Write("testing stringToProxy for process object... ");
388                     processBase = communicator.stringToProxy("Glacier2/admin -f Process:" + getTestEndpoint(51));
389                     Console.Out.WriteLine("ok");
390                 }
391 
392                 Ice.ProcessPrx process;
393                 {
394                     Console.Out.Write("testing checked cast for admin object... ");
395                     process = Ice.ProcessPrxHelper.checkedCast(processBase);
396                     test(process != null);
397                     Console.Out.WriteLine("ok");
398                 }
399 
400                 Console.Out.Write("testing Glacier2 shutdown... ");
401                 process.shutdown();
402                 try
403                 {
404                     process.ice_ping();
405                     test(false);
406                 }
407                 catch(Ice.LocalException)
408                 {
409                     Console.Out.WriteLine("ok");
410                 }
411             }
412 
413             factory = new Glacier2.SessionFactoryHelper(initData, new SessionCallback3(this));
414             lock(this)
415             {
416                 Console.Out.Write("testing SessionHelper connect after router shutdown... ");
417                 Console.Out.Flush();
418 
419                 factory.setRouterHost(host);
420                 factory.setPort(getTestPort(50));
421                 factory.setProtocol(protocol);
422                 session = factory.connect("userid", "abc123");
423                 while(true)
424                 {
425                     try
426                     {
427                         Monitor.Wait(this);
428                         break;
429                     }
430                     catch(ThreadInterruptedException)
431                     {
432                     }
433                 }
434 
435                 Console.Out.Write("testing SessionHelper isConnect after connect failure... ");
436                 Console.Out.Flush();
437                 test(session.isConnected() == false);
438                 Console.Out.WriteLine("ok");
439 
440                 Console.Out.Write("testing SessionHelper communicator after connect failure... ");
441                 Console.Out.Flush();
442                 try
443                 {
444                     test(session.communicator() != null);
445                     session.communicator().stringToProxy("dummy");
446                     test(false);
447                 }
448                 catch(Ice.CommunicatorDestroyedException)
449                 {
450                 }
451                 Console.Out.WriteLine("ok");
452 
453                 Console.Out.Write("testing SessionHelper destroy after connect failure... ");
454                 Console.Out.Flush();
455                 session.destroy();
456                 Console.Out.WriteLine("ok");
457             }
458         }
459     }
460 
461     public void
wakeUp()462     wakeUp()
463     {
464         lock(this)
465         {
466             Monitor.Pulse(this);
467         }
468     }
469 
Main(string[] args)470     public static int Main(string[] args)
471     {
472         return Test.TestDriver.runTest<Client>(args);
473     }
474 }
475