--- /dev/null
+//\r
+// System.Runtime.Remoting.RemotingServices NUnit V2.0 test class\r
+//\r
+// Author Jean-Marc ANDRE (jean-marc.andre@polymtl.ca)\r
+//\r
+// ToDo: I didn't write test functions for the method not yep\r
+// implemented by Mono\r
+\r
+using System;\r
+using System.Collections;\r
+using NUnit.Framework;\r
+using System.Reflection;\r
+using System.Runtime.Remoting;\r
+using System.Threading;\r
+using System.Runtime.Remoting.Activation;\r
+using System.Runtime.Remoting.Messaging;\r
+using System.Runtime.Remoting.Proxies;\r
+using System.Runtime.Remoting.Channels;\r
+using System.Runtime.Remoting.Channels.Tcp;\r
+\r
+namespace MonoTests.System.Runtime.Remoting.RemotingServicesInternal\r
+{\r
+ // We need our own proxy to intercept messages to remote object\r
+ // and forward them using RemotingServices.ExecuteMessage\r
+ public class MyProxy: RealProxy\r
+ {\r
+ MarshalByRefObject target;\r
+ IMessageSink _sink;\r
+ MethodBase _mthBase;\r
+ bool methodOverloaded = false;\r
+ \r
+ public MethodBase MthBase\r
+ {\r
+ get{ return _mthBase;}\r
+ }\r
+ \r
+ public bool IsMethodOverloaded\r
+ {\r
+ get{return methodOverloaded;}\r
+ }\r
+ \r
+ public MyProxy(Type serverType, MarshalByRefObject target): base(serverType)\r
+ {\r
+ this.target = target;\r
+ \r
+ IChannel[] registeredChannels = ChannelServices.RegisteredChannels;\r
+ string ObjectURI;\r
+ \r
+ // A new IMessageSink chain has to be created\r
+ // since the RemotingServices.GetEnvoyChainForProxy() is not yet\r
+ // implemented.\r
+ foreach(IChannel channel in registeredChannels)\r
+ {\r
+ IChannelSender channelSender = channel as IChannelSender;\r
+ if(channelSender != null)\r
+ {\r
+ _sink = (IMessageSink) channelSender.CreateMessageSink(RemotingServices.GetObjectUri(target), null, out ObjectURI);\r
+ }\r
+ }\r
+ \r
+ }\r
+ \r
+ // Messages will be intercepted here and redirected\r
+ // to another object.\r
+ public override IMessage Invoke(IMessage msg)\r
+ {\r
+ if(msg is IConstructionCallMessage)\r
+ {\r
+ IActivator remActivator = (IActivator) RemotingServices.Connect(typeof(IActivator), "tcp://localhost:1234/RemoteActivationService.rem");\r
+ IConstructionReturnMessage crm = remActivator.Activate((IConstructionCallMessage)msg);\r
+ return crm;\r
+ }\r
+ else\r
+ {\r
+ methodOverloaded = RemotingServices.IsMethodOverloaded((IMethodMessage)msg);\r
+ \r
+ _mthBase = RemotingServices.GetMethodBaseFromMethodMessage((IMethodMessage)msg);\r
+ MethodCallMessageWrapper mcm = new MethodCallMessageWrapper((IMethodCallMessage) msg);\r
+ mcm.Uri = RemotingServices.GetObjectUri((MarshalByRefObject)target);\r
+ MarshalByRefObject objRem = (MarshalByRefObject)Activator.CreateInstance(GetProxiedType());\r
+ RemotingServices.ExecuteMessage((MarshalByRefObject)objRem, (IMethodCallMessage)msg);\r
+ IMessage rtnMsg = null;\r
+ \r
+ try\r
+ {\r
+ rtnMsg = _sink.SyncProcessMessage(msg);\r
+ }\r
+ catch(Exception e)\r
+ {\r
+ Console.WriteLine(e.Message);\r
+ }\r
+ \r
+ return rtnMsg;\r
+ }\r
+ }\r
+ } // end MyProxy\r
+ \r
+ // This class is used to create "CAO"\r
+ public class MarshalObjectFactory: MarshalByRefObject\r
+ {\r
+ public MarshalObject GetNewMarshalObject()\r
+ {\r
+ return new MarshalObject();\r
+ }\r
+ }\r
+ \r
+ // A class used by the tests\r
+ public class MarshalObject: ContextBoundObject\r
+ {\r
+ public MarshalObject()\r
+ {\r
+ \r
+ }\r
+ \r
+ public MarshalObject(int id, string uri)\r
+ {\r
+ this.id = id;\r
+ this.uri = uri;\r
+ }\r
+ public int Id\r
+ {\r
+ get{return id;}\r
+ set{id = value;}\r
+ }\r
+ public string Uri\r
+ {\r
+ get{return uri;}\r
+ }\r
+ \r
+ public void Method1()\r
+ {\r
+ _called++;\r
+ methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod()); \r
+ }\r
+ \r
+ public void Method2()\r
+ {\r
+ methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod()); \r
+ }\r
+ \r
+ public void Method2(int i)\r
+ {\r
+ methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod()); \r
+ \r
+ }\r
+ \r
+ [OneWay()]\r
+ public void Method3()\r
+ {\r
+ methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod()); \r
+ \r
+ }\r
+ \r
+ public static int Called\r
+ {\r
+ get{return _called;}\r
+ }\r
+ \r
+ public static bool IsMethodOneWay\r
+ {\r
+ get{return methodOneWay;}\r
+ }\r
+ \r
+ \r
+ private static int _called;\r
+ private int id = 0;\r
+ private string uri;\r
+ private static bool methodOneWay = false;\r
+ }\r
+ \r
+ // Another class used by the tests\r
+ public class DerivedMarshalObject: MarshalObject\r
+ {\r
+ public DerivedMarshalObject(){}\r
+ \r
+ public DerivedMarshalObject(int id, string uri): base(id, uri) {}\r
+ }\r
+} // namespace MonoTests.System.Runtime.Remoting.RemotingServicesInternal\r
+\r
+namespace MonoTests.System.Runtime.Remoting\r
+{\r
+ using MonoTests.System.Runtime.Remoting.RemotingServicesInternal;\r
+ \r
+ // The main test class\r
+ [TestFixture]\r
+ public class RemotingServicesTest\r
+ {\r
+ private static int MarshalObjectId = 0;\r
+ \r
+ public RemotingServicesTest()\r
+ {\r
+ MarshalObjectId = 0;\r
+ }\r
+ \r
+ // Helper function that create a new\r
+ // MarshalObject with an unique ID\r
+ private static MarshalObject NewMarshalObject()\r
+ {\r
+ string uri = "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject" + MarshalObjectId.ToString();\r
+ MarshalObject objMarshal = new MarshalObject(MarshalObjectId, uri);\r
+ \r
+ MarshalObjectId++;\r
+ \r
+ return objMarshal;\r
+ }\r
+ \r
+ // Another helper function\r
+ private DerivedMarshalObject NewDerivedMarshalObject()\r
+ {\r
+ string uri = "MonoTests.System.Runtime.Remoting.RemotingServicesTest.DerivedMarshalObject" + MarshalObjectId.ToString();\r
+ DerivedMarshalObject objMarshal = new DerivedMarshalObject(MarshalObjectId, uri);\r
+ \r
+ MarshalObjectId++;\r
+ \r
+ return objMarshal;\r
+ }\r
+ \r
+ // The two folling method test RemotingServices.Marshal()\r
+ [Test]\r
+ public void Marshal1()\r
+ {\r
+ \r
+ MarshalObject objMarshal = NewMarshalObject();\r
+ ObjRef objRef = RemotingServices.Marshal(objMarshal);\r
+ \r
+ Assertion.Assert("#A01", objRef.URI != null);\r
+ \r
+ MarshalObject objRem = (MarshalObject) RemotingServices.Unmarshal(objRef);\r
+ Assertion.AssertEquals("#A02", objMarshal.Id, objRem.Id);\r
+ \r
+ objRem.Id = 2;\r
+ Assertion.AssertEquals("#A03", objMarshal.Id, objRem.Id);\r
+ \r
+ // TODO: uncomment when RemotingServices.Disconnect is implemented\r
+ //RemotingServices.Disconnect(objMarshal);\r
+ \r
+ objMarshal = NewMarshalObject();\r
+ \r
+ objRef = RemotingServices.Marshal(objMarshal, objMarshal.Uri);\r
+ \r
+ Assertion.Assert("#A04", objRef.URI.EndsWith(objMarshal.Uri));\r
+ // TODO: uncomment when RemotingServices.Disconnect is implemented\r
+ //RemotingServices.Disconnect(objMarshal); \r
+ }\r
+ \r
+ [Test]\r
+ public void Marshal2()\r
+ {\r
+ DerivedMarshalObject derivedObjMarshal = NewDerivedMarshalObject();\r
+ \r
+ ObjRef objRef = RemotingServices.Marshal(derivedObjMarshal, derivedObjMarshal.Uri, typeof(MarshalObject));\r
+ \r
+ // Check that the type of the marshaled object is MarshalObject\r
+ Assertion.Assert("#A05", objRef.TypeInfo.TypeName.StartsWith((typeof(MarshalObject)).ToString()));\r
+ \r
+ // TODO: uncomment when RemotingServices.Disconnect is implemented\r
+ //RemotingServices.Disconnect(derivedObjMarshal);\r
+ }\r
+ \r
+ // Tests RemotingServices.GetObjectUri()\r
+ [Test]\r
+ public void GetObjectUri()\r
+ {\r
+ MarshalObject objMarshal = NewMarshalObject();\r
+ \r
+ Assertion.Assert("#A06", RemotingServices.GetObjectUri(objMarshal) == null);\r
+ \r
+ ObjRef objRef = RemotingServices.Marshal(objMarshal);\r
+ \r
+ Assertion.Assert("#A07", RemotingServices.GetObjectUri(objMarshal) != null);\r
+ // TODO: uncomment when RemotingServices.Disconnect is implemented\r
+ //RemotingServices.Disconnect(objMarshal);\r
+ }\r
+ \r
+ // Tests RemotingServices.Connect\r
+ [Test]\r
+ public void Connect()\r
+ {\r
+ MarshalObject objMarshal = NewMarshalObject();\r
+ \r
+ IDictionary props = new Hashtable();\r
+ props["name"] = objMarshal.Uri;\r
+ props["port"] = 1234;\r
+ TcpChannel chn = new TcpChannel(props, null, null);\r
+ ChannelServices.RegisterChannel(chn);\r
+ \r
+ RemotingServices.Marshal(objMarshal,objMarshal.Uri);\r
+ \r
+ MarshalObject objRem = (MarshalObject) RemotingServices.Connect(typeof(MarshalObject), "tcp://localhost:1234/" + objMarshal.Uri);\r
+ \r
+ Assertion.Assert("#A08", RemotingServices.IsTransparentProxy(objRem));\r
+ \r
+ ChannelServices.UnregisterChannel(chn);\r
+ \r
+ // TODO: uncomment when RemotingServices.Disconnect is implemented\r
+ //RemotingServices.Disconnect(objMarshal);\r
+ }\r
+ \r
+ // Tests RemotingServices.Marshal()\r
+ [Test]\r
+ [ExpectedException(typeof(RemotingException))] \r
+ public void MarshalThrowException()\r
+ {\r
+ MarshalObject objMarshal = NewMarshalObject();\r
+ \r
+ IDictionary props = new Hashtable();\r
+ props["name"] = objMarshal.Uri;\r
+ props["port"] = 1234;\r
+ TcpChannel chn = new TcpChannel(props, null, null);\r
+ ChannelServices.RegisterChannel(chn);\r
+ \r
+ RemotingServices.Marshal(objMarshal,objMarshal.Uri);\r
+ \r
+ MarshalObject objRem = (MarshalObject) RemotingServices.Connect(typeof(MarshalObject), "tcp://localhost:1234/" + objMarshal.Uri);\r
+ // This line sould throw a RemotingException\r
+ // It is forbidden to export an object which is not\r
+ // a real object\r
+ try\r
+ {\r
+ RemotingServices.Marshal(objRem, objMarshal.Uri);\r
+ }\r
+ catch(Exception e)\r
+ {\r
+ ChannelServices.UnregisterChannel(chn);\r
+ \r
+ // TODO: uncomment when RemotingServices.Disconnect is implemented\r
+ //RemotingServices.Disconnect(objMarshal);\r
+ \r
+ throw e;\r
+ } \r
+ }\r
+ \r
+ // Tests RemotingServices.ExecuteMessage()\r
+ // also tests GetMethodBaseFromMessage()\r
+ // IsMethodOverloaded()\r
+ [Test]\r
+ public void ExecuteMessage()\r
+ {\r
+ TcpChannel chn = null;\r
+ try\r
+ {\r
+ chn = new TcpChannel(1234);\r
+ ChannelServices.RegisterChannel(chn);\r
+ \r
+ MarshalObject objMarshal = NewMarshalObject();\r
+ RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), objMarshal.Uri, WellKnownObjectMode.SingleCall);\r
+ \r
+ // use a proxy to catch the Message\r
+ MyProxy proxy = new MyProxy(typeof(MarshalObject), (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1234/" + objMarshal.Uri));\r
+ \r
+ MarshalObject objRem = (MarshalObject) proxy.GetTransparentProxy();\r
+ \r
+ objRem.Method1();\r
+ \r
+ // Tests RemotingServices.GetMethodBaseFromMethodMessage()\r
+ Assertion.AssertEquals("#A09","Method1",proxy.MthBase.Name);\r
+ Assertion.Assert("#A09.1", !proxy.IsMethodOverloaded);\r
+ \r
+ objRem.Method2();\r
+ Assertion.Assert("#A09.2", proxy.IsMethodOverloaded);\r
+ \r
+ // Tests RemotingServices.ExecuteMessage();\r
+ // If ExecuteMessage does it job well, Method1 should be called 2 times\r
+ Assertion.AssertEquals("#A10", 2, MarshalObject.Called);\r
+ }\r
+ finally\r
+ {\r
+ if(chn != null) ChannelServices.UnregisterChannel(chn);\r
+ }\r
+ }\r
+ \r
+ // Tests the IsOneWay method\r
+ [Test]\r
+ public void IsOneWay()\r
+ {\r
+ TcpChannel chn = null;\r
+ try\r
+ {\r
+ chn = new TcpChannel(1234);\r
+ ChannelServices.RegisterChannel(chn);\r
+ RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), "MarshalObject.rem", WellKnownObjectMode.Singleton);\r
+ \r
+ MarshalObject objRem = (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1234/MarshalObject.rem");\r
+ \r
+ Assertion.Assert("#A10.1", RemotingServices.IsTransparentProxy(objRem));\r
+ \r
+ objRem.Method1();\r
+ Thread.Sleep(20);\r
+ Assertion.Assert("#A10.2", !MarshalObject.IsMethodOneWay);\r
+ objRem.Method3();\r
+ Thread.Sleep(20);\r
+ Assertion.Assert("#A10.2", MarshalObject.IsMethodOneWay);\r
+ }\r
+ finally\r
+ {\r
+ if(chn != null) ChannelServices.UnregisterChannel(chn);\r
+ }\r
+ }\r
+ \r
+ [Test]\r
+ public void GetObjRefForProxy()\r
+ {\r
+ TcpChannel chn = null;\r
+ try\r
+ {\r
+ chn = new TcpChannel(1234);\r
+ ChannelServices.RegisterChannel(chn);\r
+ \r
+ // Register le factory as a SAO\r
+ RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObjectFactory), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap", WellKnownObjectMode.Singleton);\r
+ \r
+ MarshalObjectFactory objFactory = (MarshalObjectFactory) Activator.GetObject(typeof(MarshalObjectFactory), "tcp://localhost:1234/MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap");\r
+ \r
+ // Get a new "CAO"\r
+ MarshalObject objRem = objFactory.GetNewMarshalObject();\r
+ \r
+ ObjRef objRefRem = RemotingServices.GetObjRefForProxy((MarshalByRefObject)objRem);\r
+ \r
+ Assertion.Assert("#A11", objRefRem != null);\r
+ }\r
+ finally\r
+ {\r
+ if(chn != null) ChannelServices.UnregisterChannel(chn); \r
+ }\r
+ }\r
+ \r
+ // Tests GetRealProxy\r
+ [Test]\r
+ public void GetRealProxy()\r
+ {\r
+ TcpChannel chn = null;\r
+ try\r
+ {\r
+ chn = new TcpChannel(1234);\r
+ ChannelServices.RegisterChannel(chn);\r
+ \r
+ RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap", WellKnownObjectMode.Singleton);\r
+ \r
+ MyProxy proxy = new MyProxy(typeof(MarshalObject), (MarshalByRefObject)Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1234/MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap"));\r
+ MarshalObject objRem = (MarshalObject) proxy.GetTransparentProxy();\r
+ \r
+ RealProxy rp = RemotingServices.GetRealProxy(objRem);\r
+ \r
+ Assertion.Assert("#A12", rp != null);\r
+ Assertion.AssertEquals("#A13", "MonoTests.System.Runtime.Remoting.RemotingServicesInternal.MyProxy", rp.GetType().ToString());\r
+ }\r
+ finally\r
+ {\r
+ if(chn != null) ChannelServices.UnregisterChannel(chn);\r
+ }\r
+ }\r
+ \r
+ // Tests SetObjectUriForMarshal()\r
+ [Test]\r
+ public void SetObjectUriForMarshal()\r
+ {\r
+ TcpChannel chn = null;\r
+ try\r
+ {\r
+ chn = new TcpChannel(1234);\r
+ ChannelServices.RegisterChannel(chn);\r
+ \r
+ MarshalObject objRem = NewMarshalObject();\r
+ RemotingServices.SetObjectUriForMarshal(objRem, objRem.Uri);\r
+ RemotingServices.Marshal(objRem);\r
+ \r
+ objRem = (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1234/"+objRem.Uri);\r
+ Assertion.Assert("#A14", objRem != null);\r
+ }\r
+ finally\r
+ {\r
+ if(chn != null) ChannelServices.UnregisterChannel(chn);\r
+ } \r
+ \r
+ }\r
+ \r
+ // Tests GetServeurTypeForUri()\r
+ [Test]\r
+ public void GetServeurTypeForUri()\r
+ {\r
+ TcpChannel chn = null;\r
+ Type type = typeof(MarshalObject);\r
+ try\r
+ {\r
+ chn = new TcpChannel(1234);\r
+ ChannelServices.RegisterChannel(chn);\r
+ \r
+ MarshalObject objRem = NewMarshalObject();\r
+ RemotingServices.SetObjectUriForMarshal(objRem, objRem.Uri);\r
+ RemotingServices.Marshal(objRem);\r
+ \r
+ Type typeRem = RemotingServices.GetServerTypeForUri(RemotingServices.GetObjectUri(objRem));\r
+ Assertion.AssertEquals("#A15", type, typeRem);\r
+ }\r
+ finally\r
+ {\r
+ if(chn != null) ChannelServices.UnregisterChannel(chn);\r
+ } \r
+ }\r
+ \r
+ // Tests IsObjectOutOfDomain\r
+ // Tests IsObjectOutOfContext\r
+ [Test]\r
+ public void IsObjectOutOf()\r
+ {\r
+ TcpChannel chn = null;\r
+ try\r
+ {\r
+ chn = new TcpChannel(1234);\r
+ ChannelServices.RegisterChannel(chn);\r
+ \r
+ RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), "MarshalObject.rem", WellKnownObjectMode.Singleton);\r
+ \r
+ MarshalObject objRem = (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1234/MarshalObject.rem");\r
+ \r
+ Assertion.Assert("#A16", RemotingServices.IsObjectOutOfAppDomain(objRem));\r
+ Assertion.Assert("#A17", RemotingServices.IsObjectOutOfContext(objRem));\r
+ \r
+ MarshalObject objMarshal = new MarshalObject();\r
+ Assertion.Assert("#A18", !RemotingServices.IsObjectOutOfAppDomain(objMarshal));\r
+ Assertion.Assert("#A19", !RemotingServices.IsObjectOutOfContext(objMarshal));\r
+ }\r
+ finally\r
+ {\r
+ ChannelServices.UnregisterChannel(chn);\r
+ }\r
+ }\r
+ } // end class RemotingServicesTest\r
+} // end of namespace MonoTests.System.Runtime.Remoting.RemotingServicesTest\r