2 // System.Runtime.Remoting.RemotingServices NUnit V2.0 test class
4 // Author Jean-Marc ANDRE (jean-marc.andre@polymtl.ca)
6 // ToDo: I didn't write test functions for the method not yep
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 using MonoTests
.Helpers
;
23 namespace MonoTests
.System
.Runtime
.Remoting
.RemotingServicesInternal
25 // We need our own proxy to intercept messages to remote object
26 // and forward them using RemotingServices.ExecuteMessage
27 public class MyProxy
: RealProxy
29 MarshalByRefObject target
;
32 bool methodOverloaded
= false;
34 public MethodBase MthBase
{
35 get { return _mthBase; }
38 public bool IsMethodOverloaded
{
39 get { return methodOverloaded; }
42 public MyProxy (Type serverType
, MarshalByRefObject target
)
47 IChannel
[] registeredChannels
= ChannelServices
.RegisteredChannels
;
50 // A new IMessageSink chain has to be created
51 // since the RemotingServices.GetEnvoyChainForProxy() is not yet
53 foreach (IChannel channel
in registeredChannels
) {
54 IChannelSender channelSender
= channel
as IChannelSender
;
55 if (channelSender
!= null) {
56 _sink
= (IMessageSink
) channelSender
.CreateMessageSink (RemotingServices
.GetObjectUri (target
), null, out ObjectURI
);
62 // Messages will be intercepted here and redirected
64 public override IMessage
Invoke (IMessage msg
)
67 if (msg
is IConstructionCallMessage
) {
68 IActivator remActivator
= (IActivator
) RemotingServices
.Connect (typeof (IActivator
), "tcp://localhost:1234/RemoteActivationService.rem");
69 IConstructionReturnMessage crm
= remActivator
.Activate ((IConstructionCallMessage
) msg
);
72 methodOverloaded
= RemotingServices
.IsMethodOverloaded ((IMethodMessage
) msg
);
74 _mthBase
= RemotingServices
.GetMethodBaseFromMethodMessage ((IMethodMessage
) msg
);
75 MethodCallMessageWrapper mcm
= new MethodCallMessageWrapper ((IMethodCallMessage
) msg
);
76 mcm
.Uri
= RemotingServices
.GetObjectUri ((MarshalByRefObject
) target
);
77 MarshalByRefObject objRem
= (MarshalByRefObject
) Activator
.CreateInstance (GetProxiedType ());
78 RemotingServices
.ExecuteMessage ((MarshalByRefObject
) objRem
, (IMethodCallMessage
) msg
);
79 IMessage rtnMsg
= null;
82 rtnMsg
= _sink
.SyncProcessMessage (msg
);
83 } catch (Exception e
) {
84 Console
.WriteLine (e
.Message
);
87 Console
.WriteLine ("RR:" + rtnMsg
);
90 } catch (Exception ex
) {
91 Console
.WriteLine (ex
);
97 // This class is used to create "CAO"
98 public class MarshalObjectFactory
: MarshalByRefObject
100 public MarshalObject
GetNewMarshalObject ()
102 return new MarshalObject ();
106 // A class used by the tests
107 public class MarshalObject
: ContextBoundObject
109 public MarshalObject ()
114 public MarshalObject (int id
, string uri
)
129 public void Method1 ()
132 methodOneWay
= RemotingServices
.IsOneWay (MethodBase
.GetCurrentMethod ());
135 public void Method2 ()
137 methodOneWay
= RemotingServices
.IsOneWay (MethodBase
.GetCurrentMethod ());
140 public void Method2 (int i
)
142 methodOneWay
= RemotingServices
.IsOneWay (MethodBase
.GetCurrentMethod ());
146 public void Method3 ()
148 methodOneWay
= RemotingServices
.IsOneWay (MethodBase
.GetCurrentMethod ());
151 public static int Called
{
152 get { return _called; }
155 public static bool IsMethodOneWay
{
156 get { return methodOneWay; }
159 private static int _called
;
162 private static bool methodOneWay
= false;
165 // Another class used by the tests
166 public class DerivedMarshalObject
: MarshalObject
168 public DerivedMarshalObject () { }
170 public DerivedMarshalObject (int id
, string uri
) : base (id
, uri
) { }
181 public class CC
: MarshalByRefObject
185 public class DD
: MarshalByRefObject
190 } // namespace MonoTests.System.Runtime.Remoting.RemotingServicesInternal
192 namespace MonoTests
.Remoting
194 using MonoTests
.System
.Runtime
.Remoting
.RemotingServicesInternal
;
196 // The main test class
198 public class RemotingServicesTest
200 private static int MarshalObjectId
= 0;
202 public RemotingServicesTest ()
207 // Helper function that create a new
208 // MarshalObject with an unique ID
209 private static MarshalObject
NewMarshalObject ()
211 string uri
= "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject" + MarshalObjectId
.ToString ();
212 MarshalObject objMarshal
= new MarshalObject (MarshalObjectId
, uri
);
219 // Another helper function
220 private DerivedMarshalObject
NewDerivedMarshalObject ()
222 string uri
= "MonoTests.System.Runtime.Remoting.RemotingServicesTest.DerivedMarshalObject" + MarshalObjectId
.ToString ();
223 DerivedMarshalObject objMarshal
= new DerivedMarshalObject (MarshalObjectId
, uri
);
230 // The two folling method test RemotingServices.Marshal()
232 public void Marshal1 ()
235 MarshalObject objMarshal
= NewMarshalObject ();
236 ObjRef objRef
= RemotingServices
.Marshal (objMarshal
);
238 Assert
.IsNotNull (objRef
.URI
, "#A01");
240 MarshalObject objRem
= (MarshalObject
) RemotingServices
.Unmarshal (objRef
);
241 Assert
.AreEqual (objMarshal
.Id
, objRem
.Id
, "#A02");
244 Assert
.AreEqual (objMarshal
.Id
, objRem
.Id
, "#A03");
246 // TODO: uncomment when RemotingServices.Disconnect is implemented
247 //RemotingServices.Disconnect(objMarshal);
249 objMarshal
= NewMarshalObject ();
251 objRef
= RemotingServices
.Marshal (objMarshal
, objMarshal
.Uri
);
253 Assert
.IsTrue (objRef
.URI
.EndsWith (objMarshal
.Uri
), "#A04");
254 // TODO: uncomment when RemotingServices.Disconnect is implemented
255 //RemotingServices.Disconnect(objMarshal);
259 public void Marshal2 ()
261 DerivedMarshalObject derivedObjMarshal
= NewDerivedMarshalObject ();
263 ObjRef objRef
= RemotingServices
.Marshal (derivedObjMarshal
, derivedObjMarshal
.Uri
, typeof (MarshalObject
));
265 // Check that the type of the marshaled object is MarshalObject
266 Assert
.IsTrue (objRef
.TypeInfo
.TypeName
.StartsWith ((typeof (MarshalObject
)).ToString ()), "#A05");
268 // TODO: uncomment when RemotingServices.Disconnect is implemented
269 //RemotingServices.Disconnect(derivedObjMarshal);
272 // Tests RemotingServices.GetObjectUri()
274 public void GetObjectUri ()
276 MarshalObject objMarshal
= NewMarshalObject ();
278 Assert
.IsNull (RemotingServices
.GetObjectUri (objMarshal
), "#A06");
280 RemotingServices
.Marshal (objMarshal
);
282 Assert
.IsNotNull (RemotingServices
.GetObjectUri (objMarshal
), "#A07");
283 // TODO: uncomment when RemotingServices.Disconnect is implemented
284 //RemotingServices.Disconnect(objMarshal);
287 // Tests RemotingServices.Connect
289 public void Connect ()
291 var port
= NetworkHelpers
.FindFreePort ();
292 MarshalObject objMarshal
= NewMarshalObject ();
294 IDictionary props
= new Hashtable ();
295 props
["name"] = objMarshal
.Uri
;
296 props
["port"] = port
;
297 props
["bindTo"] = "127.0.0.1";
298 TcpChannel chn
= new TcpChannel (props
, null, null);
299 ChannelServices
.RegisterChannel (chn
);
302 RemotingServices
.Marshal (objMarshal
, objMarshal
.Uri
);
303 MarshalObject objRem
= (MarshalObject
) RemotingServices
.Connect (typeof (MarshalObject
), $"tcp://localhost:{port}/" + objMarshal
.Uri
);
304 Assert
.IsTrue (RemotingServices
.IsTransparentProxy (objRem
), "#A08");
306 ChannelServices
.UnregisterChannel (chn
);
307 RemotingServices
.Disconnect (objMarshal
);
311 // Tests RemotingServices.Marshal()
313 public void MarshalThrowException ()
315 var port
= NetworkHelpers
.FindFreePort ();
316 MarshalObject objMarshal
= NewMarshalObject ();
318 IDictionary props
= new Hashtable ();
319 props
["name"] = objMarshal
.Uri
;
320 props
["port"] = port
;
321 props
["bindTo"] = "127.0.0.1";
322 TcpChannel chn
= new TcpChannel (props
, null, null);
323 ChannelServices
.RegisterChannel (chn
);
326 RemotingServices
.Marshal (objMarshal
, objMarshal
.Uri
);
327 MarshalObject objRem
= (MarshalObject
) RemotingServices
.Connect (typeof (MarshalObject
), $"tcp://localhost:{port}/" + objMarshal
.Uri
);
328 // This line should throw a RemotingException
329 // It is forbidden to export an object which is not
332 RemotingServices
.Marshal (objRem
, objMarshal
.Uri
);
334 } catch (RemotingException e
) {
337 ChannelServices
.UnregisterChannel (chn
);
339 // TODO: uncomment when RemotingServices.Disconnect is implemented
340 //RemotingServices.Disconnect(objMarshal);
344 // Tests RemotingServices.ExecuteMessage()
345 // also tests GetMethodBaseFromMessage()
346 // IsMethodOverloaded()
348 public void ExecuteMessage ()
350 var port
= NetworkHelpers
.FindFreePort ();
351 IDictionary props
= new Hashtable ();
352 props
["port"] = port
;
353 props
["bindTo"] = "127.0.0.1";
354 TcpChannel chn
= new TcpChannel (props
, null, null);
355 ChannelServices
.RegisterChannel (chn
);
357 MarshalObject objMarshal
= NewMarshalObject ();
358 RemotingConfiguration
.RegisterWellKnownServiceType (typeof (MarshalObject
), objMarshal
.Uri
, WellKnownObjectMode
.SingleCall
);
360 // use a proxy to catch the Message
361 MyProxy proxy
= new MyProxy (typeof (MarshalObject
), (MarshalObject
) Activator
.GetObject (typeof (MarshalObject
), $"tcp://localhost:{port}/" + objMarshal
.Uri
));
363 MarshalObject objRem
= (MarshalObject
) proxy
.GetTransparentProxy ();
367 // Tests RemotingServices.GetMethodBaseFromMethodMessage()
368 Assert
.AreEqual ("Method1", proxy
.MthBase
.Name
, "#A09");
369 Assert
.IsFalse (proxy
.IsMethodOverloaded
, "#A09.1");
372 Assert
.IsTrue (proxy
.IsMethodOverloaded
, "#A09.2");
374 // Tests RemotingServices.ExecuteMessage();
375 // If ExecuteMessage does it job well, Method1 should be called 2 times
376 Assert
.AreEqual (2, MarshalObject
.Called
, "#A10");
378 ChannelServices
.UnregisterChannel (chn
);
382 // Tests the IsOneWay method
384 public void IsOneWay ()
386 var port
= NetworkHelpers
.FindFreePort ();
387 IDictionary props
= new Hashtable ();
388 props
["port"] = port
;
389 props
["bindTo"] = "127.0.0.1";
390 TcpChannel chn
= new TcpChannel (props
, null, null);
391 ChannelServices
.RegisterChannel (chn
);
393 RemotingConfiguration
.RegisterWellKnownServiceType (typeof (MarshalObject
), "MarshalObject.rem", WellKnownObjectMode
.Singleton
);
395 MarshalObject objRem
= (MarshalObject
) Activator
.GetObject (typeof (MarshalObject
), $"tcp://localhost:{port}/MarshalObject.rem");
397 Assert
.IsTrue (RemotingServices
.IsTransparentProxy (objRem
), "#A10.1");
401 Assert
.IsFalse (MarshalObject
.IsMethodOneWay
, "#A10.2");
404 Assert
.IsTrue (MarshalObject
.IsMethodOneWay
, "#A10.3");
406 ChannelServices
.UnregisterChannel (chn
);
411 public void GetObjRefForProxy ()
413 var port
= NetworkHelpers
.FindFreePort ();
414 IDictionary props
= new Hashtable ();
415 props
["port"] = port
;
416 props
["bindTo"] = "127.0.0.1";
417 TcpChannel chn
= new TcpChannel (props
, null, null);
418 ChannelServices
.RegisterChannel (chn
);
420 // Register le factory as a SAO
421 RemotingConfiguration
.RegisterWellKnownServiceType (typeof (MarshalObjectFactory
), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap", WellKnownObjectMode
.Singleton
);
423 MarshalObjectFactory objFactory
= (MarshalObjectFactory
) Activator
.GetObject (typeof (MarshalObjectFactory
), $"tcp://localhost:{port}/MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap");
426 MarshalObject objRem
= objFactory
.GetNewMarshalObject ();
428 ObjRef objRefRem
= RemotingServices
.GetObjRefForProxy ((MarshalByRefObject
) objRem
);
430 Assert
.IsNotNull (objRefRem
, "#A11");
432 ChannelServices
.UnregisterChannel (chn
);
436 // Tests GetRealProxy
438 public void GetRealProxy ()
440 var port
= NetworkHelpers
.FindFreePort ();
441 IDictionary props
= new Hashtable ();
442 props
["port"] = port
;
443 props
["bindTo"] = "127.0.0.1";
444 TcpChannel chn
= new TcpChannel (props
, null, null);
445 ChannelServices
.RegisterChannel (chn
);
447 RemotingConfiguration
.RegisterWellKnownServiceType (typeof (MarshalObject
), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap", WellKnownObjectMode
.Singleton
);
449 MyProxy proxy
= new MyProxy (typeof (MarshalObject
), (MarshalByRefObject
) Activator
.GetObject (typeof (MarshalObject
), $"tcp://localhost:{port}/MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap"));
450 MarshalObject objRem
= (MarshalObject
) proxy
.GetTransparentProxy ();
452 RealProxy rp
= RemotingServices
.GetRealProxy (objRem
);
454 Assert
.IsNotNull (rp
, "#A12");
455 Assert
.AreEqual ("MonoTests.System.Runtime.Remoting.RemotingServicesInternal.MyProxy", rp
.GetType ().ToString (), "#A13");
457 ChannelServices
.UnregisterChannel (chn
);
461 // Tests SetObjectUriForMarshal()
463 public void SetObjectUriForMarshal ()
465 var port
= NetworkHelpers
.FindFreePort ();
466 IDictionary props
= new Hashtable ();
467 props
["port"] = port
;
468 props
["bindTo"] = "127.0.0.1";
469 TcpChannel chn
= new TcpChannel (props
, null, null);
470 ChannelServices
.RegisterChannel (chn
);
472 MarshalObject objRem
= NewMarshalObject ();
473 RemotingServices
.SetObjectUriForMarshal (objRem
, objRem
.Uri
);
474 RemotingServices
.Marshal (objRem
);
476 objRem
= (MarshalObject
) Activator
.GetObject (typeof (MarshalObject
), $"tcp://localhost:{port}/" + objRem
.Uri
);
477 Assert
.IsNotNull (objRem
, "#A14");
479 ChannelServices
.UnregisterChannel (chn
);
484 // Tests GetServeurTypeForUri()
486 public void GetServeurTypeForUri ()
488 var port
= NetworkHelpers
.FindFreePort ();
489 IDictionary props
= new Hashtable ();
490 props
["port"] = port
;
491 props
["bindTo"] = "127.0.0.1";
492 TcpChannel chn
= new TcpChannel (props
, null, null);
493 Type type
= typeof (MarshalObject
);
494 ChannelServices
.RegisterChannel (chn
);
496 MarshalObject objRem
= NewMarshalObject ();
497 RemotingServices
.SetObjectUriForMarshal (objRem
, objRem
.Uri
);
498 RemotingServices
.Marshal (objRem
);
500 Type typeRem
= RemotingServices
.GetServerTypeForUri (RemotingServices
.GetObjectUri (objRem
));
501 Assert
.AreEqual (type
, typeRem
, "#A15");
503 ChannelServices
.UnregisterChannel (chn
);
507 // Tests IsObjectOutOfDomain
508 // Tests IsObjectOutOfContext
510 [Category ("NotWorking")]
511 public void IsObjectOutOf ()
513 var port
= NetworkHelpers
.FindFreePort ();
514 IDictionary props
= new Hashtable ();
515 props
["port"] = port
;
516 props
["bindTo"] = "127.0.0.1";
517 TcpChannel chn
= new TcpChannel (props
, null, null);
518 ChannelServices
.RegisterChannel (chn
);
520 RemotingConfiguration
.RegisterWellKnownServiceType (typeof (MarshalObject
), "MarshalObject2.rem", WellKnownObjectMode
.Singleton
);
522 MarshalObject objRem
= (MarshalObject
) Activator
.GetObject (typeof (MarshalObject
), $"tcp://localhost:{port}/MarshalObject2.rem");
524 Assert
.IsTrue (RemotingServices
.IsObjectOutOfAppDomain (objRem
), "#A16");
525 Assert
.IsTrue (RemotingServices
.IsObjectOutOfContext (objRem
), "#A17");
527 MarshalObject objMarshal
= new MarshalObject ();
528 Assert
.IsFalse (RemotingServices
.IsObjectOutOfAppDomain (objMarshal
), "#A18");
529 Assert
.IsFalse (RemotingServices
.IsObjectOutOfContext (objMarshal
), "#A19");
531 ChannelServices
.UnregisterChannel (chn
);
536 public void ApplicationNameTest ()
538 var port
= NetworkHelpers
.FindFreePort ();
539 RemotingConfiguration
.ApplicationName
= "app";
540 IDictionary props
= new Hashtable ();
541 props
["port"] = port
;
542 props
["bindTo"] = "127.0.0.1";
543 TcpChannel chn
= new TcpChannel (props
, null, null);
544 ChannelServices
.RegisterChannel (chn
);
546 RemotingConfiguration
.RegisterWellKnownServiceType (typeof (MarshalObject
), "obj3.rem", WellKnownObjectMode
.Singleton
);
548 MarshalObject objRem
= (MarshalObject
) Activator
.GetObject (typeof (MarshalObject
), $"tcp://localhost:{port}/app/obj3.rem");
549 MarshalObject objRem2
= (MarshalObject
) Activator
.GetObject (typeof (MarshalObject
), $"tcp://localhost:{port}/obj3.rem");
551 Assert
.IsTrue (RemotingServices
.IsTransparentProxy (objRem
), "#AN1");
552 Assert
.IsTrue (RemotingServices
.IsTransparentProxy (objRem2
), "#AN2");
554 Assert
.IsNotNull (RemotingServices
.GetServerTypeForUri ("obj3.rem"), "#AN3");
555 Assert
.IsNotNull (RemotingServices
.GetServerTypeForUri ("/app/obj3.rem"), "#AN4");
556 Assert
.IsNull (RemotingServices
.GetServerTypeForUri ("//app/obj3.rem"), "#AN5");
557 Assert
.IsNull (RemotingServices
.GetServerTypeForUri ("app/obj3.rem"), "#AN6");
558 Assert
.IsNull (RemotingServices
.GetServerTypeForUri ("/whatever/obj3.rem"), "#AN7");
559 Assert
.IsNotNull (RemotingServices
.GetServerTypeForUri ("/obj3.rem"), "#AN8");
560 Assert
.IsNull (RemotingServices
.GetServerTypeForUri ("//obj3.rem"), "#AN9");
562 ChannelServices
.UnregisterChannel (chn
);
567 public void GetObjectWithChannelDataTest ()
569 var port
= NetworkHelpers
.FindFreePort ();
570 IDictionary props
= new Hashtable ();
571 props
["port"] = port
;
572 props
["bindTo"] = "127.0.0.1";
573 TcpChannel chn
= new TcpChannel (props
, null, null);
574 ChannelServices
.RegisterChannel (chn
);
576 RemotingConfiguration
.RegisterWellKnownServiceType (typeof (MarshalObject
), "getobjectwithchanneldata.rem", WellKnownObjectMode
.Singleton
);
578 string channelData
= "test";
579 Assert
.IsNotNull (Activator
.GetObject (typeof (MarshalObject
), $"tcp://localhost:{port}/getobjectwithchanneldata.rem", channelData
), "#01");
581 ChannelServices
.UnregisterChannel (chn
);
585 public interface IMarshalTest
590 public interface ITest2
: IMarshalTest
592 void DoSomethingElse();
595 public class TestClass
: MarshalByRefObject
, ITest2
597 public void DoSomething()
601 public void DoSomethingElse()
607 public void MarshalBaseInterfaces()
609 TestClass test
= new TestClass();
610 ObjRef obj
= RemotingServices
.Marshal(test
, "TestClass", typeof(ITest2
));
611 FieldInfo interfacesImplemented
= obj
.TypeInfo
.GetType().GetField("interfacesImplemented", BindingFlags
.NonPublic
| BindingFlags
.Instance
);
612 string[] interfaces
= (string[])interfacesImplemented
.GetValue(obj
.TypeInfo
);
613 Assert
.AreEqual(2, interfaces
.Length
);
614 Assert
.IsTrue(interfaces
[0].Contains("IMarshalTest"));
615 Assert
.IsTrue(interfaces
[1].Contains("ITest2"));
619 [Ignore ("We cannot test RemotingConfiguration.Configure() because it keeps channels registered. If we really need to test it, do it as a standalone case")]
620 public void ConnectProxyCast ()
622 var port
= NetworkHelpers
.FindFreePort ();
624 RemotingConfiguration
.Configure (null);
626 o
= RemotingServices
.Connect (typeof (MarshalByRefObject
), $"tcp://localhost:{port}/ff1.rem");
627 Assert
.IsInstanceOfType (typeof (DD
), o
, "#m1");
628 Assert
.IsInstanceOfType (typeof (A
), o
, "#m2");
629 Assert
.IsInstanceOfType (typeof (B
), o
, "#m3");
630 AssertHelper
.IsNotInstanceOfType (typeof (CC
), !(o
is CC
), "#m4");
632 o
= RemotingServices
.Connect (typeof (A
), $"tcp://localhost:{port}/ff3.rem");
633 Assert
.IsInstanceOfType (typeof (DD
), o
, "#a1");
634 Assert
.IsInstanceOfType (typeof (A
), o
, "#a2");
635 Assert
.IsInstanceOfType (typeof (B
), o
, "#a3");
636 AssertHelper
.IsNotInstanceOfType (typeof (CC
), o
, "#a4");
638 o
= RemotingServices
.Connect (typeof (DD
), $"tcp://localhost:{port}/ff4.rem");
639 Assert
.IsInstanceOfType (typeof (DD
), o
, "#d1");
640 Assert
.IsInstanceOfType (typeof (A
), o
, "#d2");
641 Assert
.IsInstanceOfType (typeof (B
), o
, "#d3");
642 AssertHelper
.IsNotInstanceOfType (typeof (CC
), o
, "#d4");
644 o
= RemotingServices
.Connect (typeof (CC
), $"tcp://localhost:{port}/ff5.rem");
645 AssertHelper
.IsNotInstanceOfType (typeof (DD
), o
, "#c1");
646 Assert
.IsInstanceOfType (typeof (A
), o
, "#c2");
647 Assert
.IsInstanceOfType (typeof (B
), o
, "#c3");
648 Assert
.IsInstanceOfType (typeof (CC
), o
, "#c4");
650 // Don't add any tests that must create channels
651 // after ConnectProxyCast (), because this test calls
652 // RemotingConfiguration.Configure ().
653 } // end class RemotingServicesTest
654 } // end of namespace MonoTests.Remoting