2010-05-25 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Runtime.Remoting / Test / RemotingServicesTest.cs
blobf9ad75cd3ac4a05f55b984212b19982d795a576a
1 //
2 // System.Runtime.Remoting.RemotingServices NUnit V2.0 test class
3 //
4 // Author Jean-Marc ANDRE (jean-marc.andre@polymtl.ca)
5 //
6 // ToDo: I didn't write test functions for the method not yep
7 // implemented by Mono
9 using System;
10 using System.Collections;
11 using NUnit.Framework;
12 using System.Reflection;
13 using System.Runtime.Remoting;
14 using System.Threading;
15 using System.Runtime.Remoting.Activation;
16 using System.Runtime.Remoting.Messaging;
17 using System.Runtime.Remoting.Proxies;
18 using System.Runtime.Remoting.Channels;
19 using System.Runtime.Remoting.Channels.Tcp;
21 namespace MonoTests.System.Runtime.Remoting.RemotingServicesInternal
23 // We need our own proxy to intercept messages to remote object
24 // and forward them using RemotingServices.ExecuteMessage
25 public class MyProxy : RealProxy
27 MarshalByRefObject target;
28 IMessageSink _sink;
29 MethodBase _mthBase;
30 bool methodOverloaded = false;
32 public MethodBase MthBase {
33 get { return _mthBase; }
36 public bool IsMethodOverloaded {
37 get { return methodOverloaded; }
40 public MyProxy (Type serverType, MarshalByRefObject target)
41 : base (serverType)
43 this.target = target;
45 IChannel [] registeredChannels = ChannelServices.RegisteredChannels;
46 string ObjectURI;
48 // A new IMessageSink chain has to be created
49 // since the RemotingServices.GetEnvoyChainForProxy() is not yet
50 // implemented.
51 foreach (IChannel channel in registeredChannels) {
52 IChannelSender channelSender = channel as IChannelSender;
53 if (channelSender != null) {
54 _sink = (IMessageSink) channelSender.CreateMessageSink (RemotingServices.GetObjectUri (target), null, out ObjectURI);
60 // Messages will be intercepted here and redirected
61 // to another object.
62 public override IMessage Invoke (IMessage msg)
64 try {
65 if (msg is IConstructionCallMessage) {
66 IActivator remActivator = (IActivator) RemotingServices.Connect (typeof (IActivator), "tcp://localhost:1234/RemoteActivationService.rem");
67 IConstructionReturnMessage crm = remActivator.Activate ((IConstructionCallMessage) msg);
68 return crm;
69 } else {
70 methodOverloaded = RemotingServices.IsMethodOverloaded ((IMethodMessage) msg);
72 _mthBase = RemotingServices.GetMethodBaseFromMethodMessage ((IMethodMessage) msg);
73 MethodCallMessageWrapper mcm = new MethodCallMessageWrapper ((IMethodCallMessage) msg);
74 mcm.Uri = RemotingServices.GetObjectUri ((MarshalByRefObject) target);
75 MarshalByRefObject objRem = (MarshalByRefObject) Activator.CreateInstance (GetProxiedType ());
76 RemotingServices.ExecuteMessage ((MarshalByRefObject) objRem, (IMethodCallMessage) msg);
77 IMessage rtnMsg = null;
79 try {
80 rtnMsg = _sink.SyncProcessMessage (msg);
81 } catch (Exception e) {
82 Console.WriteLine (e.Message);
85 Console.WriteLine ("RR:" + rtnMsg);
86 return rtnMsg;
88 } catch (Exception ex) {
89 Console.WriteLine (ex);
90 return null;
93 } // end MyProxy
95 // This class is used to create "CAO"
96 public class MarshalObjectFactory : MarshalByRefObject
98 public MarshalObject GetNewMarshalObject ()
100 return new MarshalObject ();
104 // A class used by the tests
105 public class MarshalObject : ContextBoundObject
107 public MarshalObject ()
112 public MarshalObject (int id, string uri)
114 this.id = id;
115 this.uri = uri;
118 public int Id {
119 get { return id; }
120 set { id = value; }
123 public string Uri {
124 get { return uri; }
127 public void Method1 ()
129 _called++;
130 methodOneWay = RemotingServices.IsOneWay (MethodBase.GetCurrentMethod ());
133 public void Method2 ()
135 methodOneWay = RemotingServices.IsOneWay (MethodBase.GetCurrentMethod ());
138 public void Method2 (int i)
140 methodOneWay = RemotingServices.IsOneWay (MethodBase.GetCurrentMethod ());
143 [OneWay ()]
144 public void Method3 ()
146 methodOneWay = RemotingServices.IsOneWay (MethodBase.GetCurrentMethod ());
149 public static int Called {
150 get { return _called; }
153 public static bool IsMethodOneWay {
154 get { return methodOneWay; }
157 private static int _called;
158 private int id = 0;
159 private string uri;
160 private static bool methodOneWay = false;
163 // Another class used by the tests
164 public class DerivedMarshalObject : MarshalObject
166 public DerivedMarshalObject () { }
168 public DerivedMarshalObject (int id, string uri) : base (id, uri) { }
171 interface A
175 interface B : A
179 public class CC : MarshalByRefObject
183 public class DD : MarshalByRefObject
188 } // namespace MonoTests.System.Runtime.Remoting.RemotingServicesInternal
190 namespace MonoTests.Remoting
192 using MonoTests.System.Runtime.Remoting.RemotingServicesInternal;
194 // The main test class
195 [TestFixture]
196 public class RemotingServicesTest : Assertion
198 private static int MarshalObjectId = 0;
200 public RemotingServicesTest ()
202 MarshalObjectId = 0;
205 // Helper function that create a new
206 // MarshalObject with an unique ID
207 private static MarshalObject NewMarshalObject ()
209 string uri = "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject" + MarshalObjectId.ToString ();
210 MarshalObject objMarshal = new MarshalObject (MarshalObjectId, uri);
212 MarshalObjectId++;
214 return objMarshal;
217 // Another helper function
218 private DerivedMarshalObject NewDerivedMarshalObject ()
220 string uri = "MonoTests.System.Runtime.Remoting.RemotingServicesTest.DerivedMarshalObject" + MarshalObjectId.ToString ();
221 DerivedMarshalObject objMarshal = new DerivedMarshalObject (MarshalObjectId, uri);
223 MarshalObjectId++;
225 return objMarshal;
228 // The two folling method test RemotingServices.Marshal()
229 [Test]
230 public void Marshal1 ()
233 MarshalObject objMarshal = NewMarshalObject ();
234 ObjRef objRef = RemotingServices.Marshal (objMarshal);
236 Assert ("#A01", objRef.URI != null);
238 MarshalObject objRem = (MarshalObject) RemotingServices.Unmarshal (objRef);
239 AssertEquals ("#A02", objMarshal.Id, objRem.Id);
241 objRem.Id = 2;
242 AssertEquals ("#A03", objMarshal.Id, objRem.Id);
244 // TODO: uncomment when RemotingServices.Disconnect is implemented
245 //RemotingServices.Disconnect(objMarshal);
247 objMarshal = NewMarshalObject ();
249 objRef = RemotingServices.Marshal (objMarshal, objMarshal.Uri);
251 Assert ("#A04", objRef.URI.EndsWith (objMarshal.Uri));
252 // TODO: uncomment when RemotingServices.Disconnect is implemented
253 //RemotingServices.Disconnect(objMarshal);
256 [Test]
257 public void Marshal2 ()
259 DerivedMarshalObject derivedObjMarshal = NewDerivedMarshalObject ();
261 ObjRef objRef = RemotingServices.Marshal (derivedObjMarshal, derivedObjMarshal.Uri, typeof (MarshalObject));
263 // Check that the type of the marshaled object is MarshalObject
264 Assert ("#A05", objRef.TypeInfo.TypeName.StartsWith ((typeof (MarshalObject)).ToString ()));
266 // TODO: uncomment when RemotingServices.Disconnect is implemented
267 //RemotingServices.Disconnect(derivedObjMarshal);
270 // Tests RemotingServices.GetObjectUri()
271 [Test]
272 public void GetObjectUri ()
274 MarshalObject objMarshal = NewMarshalObject ();
276 Assert ("#A06", RemotingServices.GetObjectUri (objMarshal) == null);
278 RemotingServices.Marshal (objMarshal);
280 Assert ("#A07", RemotingServices.GetObjectUri (objMarshal) != null);
281 // TODO: uncomment when RemotingServices.Disconnect is implemented
282 //RemotingServices.Disconnect(objMarshal);
285 // Tests RemotingServices.Connect
286 [Test]
287 public void Connect ()
289 MarshalObject objMarshal = NewMarshalObject ();
291 IDictionary props = new Hashtable ();
292 props ["name"] = objMarshal.Uri;
293 props ["port"] = 1236;
294 TcpChannel chn = new TcpChannel (props, null, null);
295 ChannelServices.RegisterChannel (chn);
297 try {
298 RemotingServices.Marshal (objMarshal, objMarshal.Uri);
299 MarshalObject objRem = (MarshalObject) RemotingServices.Connect (typeof (MarshalObject), "tcp://localhost:1236/" + objMarshal.Uri);
300 Assert ("#A08", RemotingServices.IsTransparentProxy (objRem));
301 } finally {
302 ChannelServices.UnregisterChannel (chn);
303 RemotingServices.Disconnect (objMarshal);
307 // Tests RemotingServices.Marshal()
308 [Test]
309 public void MarshalThrowException ()
311 MarshalObject objMarshal = NewMarshalObject ();
313 IDictionary props = new Hashtable ();
314 props ["name"] = objMarshal.Uri;
315 props ["port"] = 1237;
316 TcpChannel chn = new TcpChannel (props, null, null);
317 ChannelServices.RegisterChannel (chn);
319 try {
320 RemotingServices.Marshal (objMarshal, objMarshal.Uri);
321 MarshalObject objRem = (MarshalObject) RemotingServices.Connect (typeof (MarshalObject), "tcp://localhost:1237/" + objMarshal.Uri);
322 // This line should throw a RemotingException
323 // It is forbidden to export an object which is not
324 // a real object
325 try {
326 RemotingServices.Marshal (objRem, objMarshal.Uri);
327 Fail ("#1");
328 } catch (RemotingException e) {
330 } finally {
331 ChannelServices.UnregisterChannel (chn);
333 // TODO: uncomment when RemotingServices.Disconnect is implemented
334 //RemotingServices.Disconnect(objMarshal);
338 // Tests RemotingServices.ExecuteMessage()
339 // also tests GetMethodBaseFromMessage()
340 // IsMethodOverloaded()
341 [Test]
342 public void ExecuteMessage ()
344 TcpChannel chn = new TcpChannel (1235);
345 ChannelServices.RegisterChannel (chn);
346 try {
347 MarshalObject objMarshal = NewMarshalObject ();
348 RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), objMarshal.Uri, WellKnownObjectMode.SingleCall);
350 // use a proxy to catch the Message
351 MyProxy proxy = new MyProxy (typeof (MarshalObject), (MarshalObject) Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1235/" + objMarshal.Uri));
353 MarshalObject objRem = (MarshalObject) proxy.GetTransparentProxy ();
355 objRem.Method1 ();
357 // Tests RemotingServices.GetMethodBaseFromMethodMessage()
358 AssertEquals ("#A09", "Method1", proxy.MthBase.Name);
359 Assert ("#A09.1", !proxy.IsMethodOverloaded);
361 objRem.Method2 ();
362 Assert ("#A09.2", proxy.IsMethodOverloaded);
364 // Tests RemotingServices.ExecuteMessage();
365 // If ExecuteMessage does it job well, Method1 should be called 2 times
366 AssertEquals ("#A10", 2, MarshalObject.Called);
367 } finally {
368 ChannelServices.UnregisterChannel (chn);
372 // Tests the IsOneWay method
373 [Test]
374 public void IsOneWay ()
376 TcpChannel chn = new TcpChannel (1238);
377 ChannelServices.RegisterChannel (chn);
378 try {
379 RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), "MarshalObject.rem", WellKnownObjectMode.Singleton);
381 MarshalObject objRem = (MarshalObject) Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1238/MarshalObject.rem");
383 Assert ("#A10.1", RemotingServices.IsTransparentProxy (objRem));
385 objRem.Method1 ();
386 Thread.Sleep (20);
387 Assert ("#A10.2", !MarshalObject.IsMethodOneWay);
388 objRem.Method3 ();
389 Thread.Sleep (20);
390 Assert ("#A10.3", MarshalObject.IsMethodOneWay);
391 } finally {
392 ChannelServices.UnregisterChannel (chn);
396 [Test]
397 public void GetObjRefForProxy ()
399 TcpChannel chn = new TcpChannel (1239);
400 ChannelServices.RegisterChannel (chn);
401 try {
402 // Register le factory as a SAO
403 RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObjectFactory), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap", WellKnownObjectMode.Singleton);
405 MarshalObjectFactory objFactory = (MarshalObjectFactory) Activator.GetObject (typeof (MarshalObjectFactory), "tcp://localhost:1239/MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap");
407 // Get a new "CAO"
408 MarshalObject objRem = objFactory.GetNewMarshalObject ();
410 ObjRef objRefRem = RemotingServices.GetObjRefForProxy ((MarshalByRefObject) objRem);
412 Assert ("#A11", objRefRem != null);
413 } finally {
414 ChannelServices.UnregisterChannel (chn);
418 // Tests GetRealProxy
419 [Test]
420 public void GetRealProxy ()
422 TcpChannel chn = new TcpChannel (1241);
423 ChannelServices.RegisterChannel (chn);
424 try {
425 RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap", WellKnownObjectMode.Singleton);
427 MyProxy proxy = new MyProxy (typeof (MarshalObject), (MarshalByRefObject) Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1241/MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap"));
428 MarshalObject objRem = (MarshalObject) proxy.GetTransparentProxy ();
430 RealProxy rp = RemotingServices.GetRealProxy (objRem);
432 Assert ("#A12", rp != null);
433 AssertEquals ("#A13", "MonoTests.System.Runtime.Remoting.RemotingServicesInternal.MyProxy", rp.GetType ().ToString ());
434 } finally {
435 ChannelServices.UnregisterChannel (chn);
439 // Tests SetObjectUriForMarshal()
440 [Test]
441 public void SetObjectUriForMarshal ()
443 TcpChannel chn = new TcpChannel (1242);
444 ChannelServices.RegisterChannel (chn);
445 try {
446 MarshalObject objRem = NewMarshalObject ();
447 RemotingServices.SetObjectUriForMarshal (objRem, objRem.Uri);
448 RemotingServices.Marshal (objRem);
450 objRem = (MarshalObject) Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1242/" + objRem.Uri);
451 Assert ("#A14", objRem != null);
452 } finally {
453 ChannelServices.UnregisterChannel (chn);
458 // Tests GetServeurTypeForUri()
459 [Test]
460 public void GetServeurTypeForUri ()
462 TcpChannel chn = new TcpChannel (1243);
463 Type type = typeof (MarshalObject);
464 ChannelServices.RegisterChannel (chn);
465 try {
466 MarshalObject objRem = NewMarshalObject ();
467 RemotingServices.SetObjectUriForMarshal (objRem, objRem.Uri);
468 RemotingServices.Marshal (objRem);
470 Type typeRem = RemotingServices.GetServerTypeForUri (RemotingServices.GetObjectUri (objRem));
471 AssertEquals ("#A15", type, typeRem);
472 } finally {
473 ChannelServices.UnregisterChannel (chn);
477 // Tests IsObjectOutOfDomain
478 // Tests IsObjectOutOfContext
479 [Test]
480 [Category ("NotWorking")]
481 public void IsObjectOutOf ()
483 TcpChannel chn = new TcpChannel (1245);
484 ChannelServices.RegisterChannel (chn);
485 try {
486 RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), "MarshalObject2.rem", WellKnownObjectMode.Singleton);
488 MarshalObject objRem = (MarshalObject) Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1245/MarshalObject2.rem");
490 Assert ("#A16", RemotingServices.IsObjectOutOfAppDomain (objRem));
491 Assert ("#A17", RemotingServices.IsObjectOutOfContext (objRem));
493 MarshalObject objMarshal = new MarshalObject ();
494 Assert ("#A18", !RemotingServices.IsObjectOutOfAppDomain (objMarshal));
495 Assert ("#A19", !RemotingServices.IsObjectOutOfContext (objMarshal));
496 } finally {
497 ChannelServices.UnregisterChannel (chn);
501 [Test]
502 public void ApplicationNameTest ()
504 RemotingConfiguration.ApplicationName = "app";
505 TcpChannel chn = new TcpChannel (1246);
506 ChannelServices.RegisterChannel (chn);
507 try {
508 RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), "obj3.rem", WellKnownObjectMode.Singleton);
510 MarshalObject objRem = (MarshalObject) Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1246/app/obj3.rem");
511 MarshalObject objRem2 = (MarshalObject) Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1246/obj3.rem");
513 Assert ("#AN1", RemotingServices.IsTransparentProxy (objRem));
514 Assert ("#AN2", RemotingServices.IsTransparentProxy (objRem2));
516 AssertNotNull ("#AN3", RemotingServices.GetServerTypeForUri ("obj3.rem"));
517 AssertNotNull ("#AN4", RemotingServices.GetServerTypeForUri ("/app/obj3.rem"));
518 AssertNull ("#AN5", RemotingServices.GetServerTypeForUri ("//app/obj3.rem"));
519 AssertNull ("#AN6", RemotingServices.GetServerTypeForUri ("app/obj3.rem"));
520 AssertNull ("#AN7", RemotingServices.GetServerTypeForUri ("/whatever/obj3.rem"));
521 AssertNotNull ("#AN8", RemotingServices.GetServerTypeForUri ("/obj3.rem"));
522 AssertNull ("#AN9", RemotingServices.GetServerTypeForUri ("//obj3.rem"));
523 } finally {
524 ChannelServices.UnregisterChannel (chn);
528 [Test]
529 public void GetObjectWithChannelDataTest ()
531 TcpChannel chn = new TcpChannel (1247);
532 ChannelServices.RegisterChannel (chn);
533 try {
534 RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), "getobjectwithchanneldata.rem", WellKnownObjectMode.Singleton);
536 string channelData = "test";
537 AssertNotNull ("#01", Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1247/getobjectwithchanneldata.rem", channelData));
538 } finally {
539 ChannelServices.UnregisterChannel (chn);
543 [Test]
544 [Ignore ("We cannot test RemotingConfiguration.Configure() because it keeps channels registered. If we really need to test it, do it as a standalone case")]
545 public void ConnectProxyCast ()
547 object o;
548 RemotingConfiguration.Configure (null);
550 o = RemotingServices.Connect (typeof (MarshalByRefObject), "tcp://localhost:3434/ff1.rem");
551 Assert ("#m1", o is DD);
552 Assert ("#m2", o is A);
553 Assert ("#m3", o is B);
554 Assert ("#m4", !(o is CC));
556 o = RemotingServices.Connect (typeof (A), "tcp://localhost:3434/ff3.rem");
557 Assert ("#a1", o is DD);
558 Assert ("#a2", o is A);
559 Assert ("#a3", o is B);
560 Assert ("#a4", !(o is CC));
562 o = RemotingServices.Connect (typeof (DD), "tcp://localhost:3434/ff4.rem");
563 Assert ("#d1", o is DD);
564 Assert ("#d2", o is A);
565 Assert ("#d3", o is B);
566 Assert ("#d4", !(o is CC));
568 o = RemotingServices.Connect (typeof (CC), "tcp://localhost:3434/ff5.rem");
569 Assert ("#c1", !(o is DD));
570 Assert ("#c2", o is A);
571 Assert ("#c3", o is B);
572 Assert ("#c4", o is CC);
574 // Don't add any tests that must create channels
575 // after ConnectProxyCast (), because this test calls
576 // RemotingConfiguration.Configure ().
577 } // end class RemotingServicesTest
578 } // end of namespace MonoTests.Remoting