(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / Test / System.Runtime.Remoting / RemotingServicesTest.cs
blobaed576b3598fa5746ce7efec772be3bb5931f72e
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
34 get{ return _mthBase;}
37 public bool IsMethodOverloaded
39 get{return methodOverloaded;}
42 public MyProxy(Type serverType, MarshalByRefObject target): base(serverType)
44 this.target = target;
46 IChannel[] registeredChannels = ChannelServices.RegisteredChannels;
47 string ObjectURI;
49 // A new IMessageSink chain has to be created
50 // since the RemotingServices.GetEnvoyChainForProxy() is not yet
51 // implemented.
52 foreach(IChannel channel in registeredChannels)
54 IChannelSender channelSender = channel as IChannelSender;
55 if(channelSender != null)
57 _sink = (IMessageSink) channelSender.CreateMessageSink(RemotingServices.GetObjectUri(target), null, out ObjectURI);
63 // Messages will be intercepted here and redirected
64 // to another object.
65 public override IMessage Invoke(IMessage msg)
67 if(msg is IConstructionCallMessage)
69 IActivator remActivator = (IActivator) RemotingServices.Connect(typeof(IActivator), "tcp://localhost:1234/RemoteActivationService.rem");
70 IConstructionReturnMessage crm = remActivator.Activate((IConstructionCallMessage)msg);
71 return crm;
73 else
75 methodOverloaded = RemotingServices.IsMethodOverloaded((IMethodMessage)msg);
77 _mthBase = RemotingServices.GetMethodBaseFromMethodMessage((IMethodMessage)msg);
78 MethodCallMessageWrapper mcm = new MethodCallMessageWrapper((IMethodCallMessage) msg);
79 mcm.Uri = RemotingServices.GetObjectUri((MarshalByRefObject)target);
80 MarshalByRefObject objRem = (MarshalByRefObject)Activator.CreateInstance(GetProxiedType());
81 RemotingServices.ExecuteMessage((MarshalByRefObject)objRem, (IMethodCallMessage)msg);
82 IMessage rtnMsg = null;
84 try
86 rtnMsg = _sink.SyncProcessMessage(msg);
88 catch(Exception e)
90 Console.WriteLine(e.Message);
93 return rtnMsg;
96 } // end MyProxy
98 // This class is used to create "CAO"
99 public class MarshalObjectFactory: MarshalByRefObject
101 public MarshalObject GetNewMarshalObject()
103 return new MarshalObject();
107 // A class used by the tests
108 public class MarshalObject: ContextBoundObject
110 public MarshalObject()
115 public MarshalObject(int id, string uri)
117 this.id = id;
118 this.uri = uri;
120 public int Id
122 get{return id;}
123 set{id = value;}
125 public string Uri
127 get{return uri;}
130 public void Method1()
132 _called++;
133 methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());
136 public void Method2()
138 methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());
141 public void Method2(int i)
143 methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());
147 [OneWay()]
148 public void Method3()
150 methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());
154 public static int Called
156 get{return _called;}
159 public static bool IsMethodOneWay
161 get{return methodOneWay;}
165 private static int _called;
166 private int id = 0;
167 private string uri;
168 private static bool methodOneWay = false;
171 // Another class used by the tests
172 public class DerivedMarshalObject: MarshalObject
174 public DerivedMarshalObject(){}
176 public DerivedMarshalObject(int id, string uri): base(id, uri) {}
178 } // namespace MonoTests.System.Runtime.Remoting.RemotingServicesInternal
180 namespace MonoTests.System.Runtime.Remoting
182 using MonoTests.System.Runtime.Remoting.RemotingServicesInternal;
184 // The main test class
185 [TestFixture]
186 public class RemotingServicesTest : Assertion
188 private static int MarshalObjectId = 0;
190 public RemotingServicesTest()
192 MarshalObjectId = 0;
195 // Helper function that create a new
196 // MarshalObject with an unique ID
197 private static MarshalObject NewMarshalObject()
199 string uri = "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject" + MarshalObjectId.ToString();
200 MarshalObject objMarshal = new MarshalObject(MarshalObjectId, uri);
202 MarshalObjectId++;
204 return objMarshal;
207 // Another helper function
208 private DerivedMarshalObject NewDerivedMarshalObject()
210 string uri = "MonoTests.System.Runtime.Remoting.RemotingServicesTest.DerivedMarshalObject" + MarshalObjectId.ToString();
211 DerivedMarshalObject objMarshal = new DerivedMarshalObject(MarshalObjectId, uri);
213 MarshalObjectId++;
215 return objMarshal;
218 // The two folling method test RemotingServices.Marshal()
219 [Test]
220 public void Marshal1()
223 MarshalObject objMarshal = NewMarshalObject();
224 ObjRef objRef = RemotingServices.Marshal(objMarshal);
226 Assert("#A01", objRef.URI != null);
228 MarshalObject objRem = (MarshalObject) RemotingServices.Unmarshal(objRef);
229 AssertEquals("#A02", objMarshal.Id, objRem.Id);
231 objRem.Id = 2;
232 AssertEquals("#A03", objMarshal.Id, objRem.Id);
234 // TODO: uncomment when RemotingServices.Disconnect is implemented
235 //RemotingServices.Disconnect(objMarshal);
237 objMarshal = NewMarshalObject();
239 objRef = RemotingServices.Marshal(objMarshal, objMarshal.Uri);
241 Assert("#A04", objRef.URI.EndsWith(objMarshal.Uri));
242 // TODO: uncomment when RemotingServices.Disconnect is implemented
243 //RemotingServices.Disconnect(objMarshal);
246 [Test]
247 public void Marshal2()
249 DerivedMarshalObject derivedObjMarshal = NewDerivedMarshalObject();
251 ObjRef objRef = RemotingServices.Marshal(derivedObjMarshal, derivedObjMarshal.Uri, typeof(MarshalObject));
253 // Check that the type of the marshaled object is MarshalObject
254 Assert("#A05", objRef.TypeInfo.TypeName.StartsWith((typeof(MarshalObject)).ToString()));
256 // TODO: uncomment when RemotingServices.Disconnect is implemented
257 //RemotingServices.Disconnect(derivedObjMarshal);
260 // Tests RemotingServices.GetObjectUri()
261 [Test]
262 public void GetObjectUri()
264 MarshalObject objMarshal = NewMarshalObject();
266 Assert("#A06", RemotingServices.GetObjectUri(objMarshal) == null);
268 ObjRef objRef = RemotingServices.Marshal(objMarshal);
270 Assert("#A07", RemotingServices.GetObjectUri(objMarshal) != null);
271 // TODO: uncomment when RemotingServices.Disconnect is implemented
272 //RemotingServices.Disconnect(objMarshal);
275 // Tests RemotingServices.Connect
276 [Test]
277 public void Connect()
279 MarshalObject objMarshal = NewMarshalObject();
281 IDictionary props = new Hashtable();
282 props["name"] = objMarshal.Uri;
283 props["port"] = 1236;
284 TcpChannel chn = new TcpChannel(props, null, null);
285 ChannelServices.RegisterChannel(chn);
287 RemotingServices.Marshal(objMarshal,objMarshal.Uri);
289 MarshalObject objRem = (MarshalObject) RemotingServices.Connect(typeof(MarshalObject), "tcp://localhost:1236/" + objMarshal.Uri);
291 Assert("#A08", RemotingServices.IsTransparentProxy(objRem));
293 ChannelServices.UnregisterChannel(chn);
295 // TODO: uncomment when RemotingServices.Disconnect is implemented
296 //RemotingServices.Disconnect(objMarshal);
299 // Tests RemotingServices.Marshal()
300 [Test]
301 [ExpectedException(typeof(RemotingException))]
302 public void MarshalThrowException()
304 MarshalObject objMarshal = NewMarshalObject();
306 IDictionary props = new Hashtable();
307 props["name"] = objMarshal.Uri;
308 props["port"] = 1237;
309 TcpChannel chn = new TcpChannel(props, null, null);
310 ChannelServices.RegisterChannel(chn);
312 RemotingServices.Marshal(objMarshal,objMarshal.Uri);
314 MarshalObject objRem = (MarshalObject) RemotingServices.Connect(typeof(MarshalObject), "tcp://localhost:1237/" + objMarshal.Uri);
315 // This line sould throw a RemotingException
316 // It is forbidden to export an object which is not
317 // a real object
320 RemotingServices.Marshal(objRem, objMarshal.Uri);
322 catch(Exception e)
324 ChannelServices.UnregisterChannel(chn);
326 // TODO: uncomment when RemotingServices.Disconnect is implemented
327 //RemotingServices.Disconnect(objMarshal);
329 throw e;
333 // Tests RemotingServices.ExecuteMessage()
334 // also tests GetMethodBaseFromMessage()
335 // IsMethodOverloaded()
336 [Test]
337 public void ExecuteMessage()
339 TcpChannel chn = null;
342 chn = new TcpChannel(1235);
343 ChannelServices.RegisterChannel(chn);
345 MarshalObject objMarshal = NewMarshalObject();
346 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), objMarshal.Uri, WellKnownObjectMode.SingleCall);
348 // use a proxy to catch the Message
349 MyProxy proxy = new MyProxy(typeof(MarshalObject), (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1235/" + objMarshal.Uri));
351 MarshalObject objRem = (MarshalObject) proxy.GetTransparentProxy();
353 objRem.Method1();
355 // Tests RemotingServices.GetMethodBaseFromMethodMessage()
356 AssertEquals("#A09","Method1",proxy.MthBase.Name);
357 Assert("#A09.1", !proxy.IsMethodOverloaded);
359 objRem.Method2();
360 Assert("#A09.2", proxy.IsMethodOverloaded);
362 // Tests RemotingServices.ExecuteMessage();
363 // If ExecuteMessage does it job well, Method1 should be called 2 times
364 AssertEquals("#A10", 2, MarshalObject.Called);
366 finally
368 if(chn != null) ChannelServices.UnregisterChannel(chn);
372 // Tests the IsOneWay method
373 [Test]
374 public void IsOneWay()
376 TcpChannel chn = null;
379 chn = new TcpChannel(1238);
380 ChannelServices.RegisterChannel(chn);
381 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), "MarshalObject.rem", WellKnownObjectMode.Singleton);
383 MarshalObject objRem = (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1238/MarshalObject.rem");
385 Assert("#A10.1", RemotingServices.IsTransparentProxy(objRem));
387 objRem.Method1();
388 Thread.Sleep(20);
389 Assert("#A10.2", !MarshalObject.IsMethodOneWay);
390 objRem.Method3();
391 Thread.Sleep(20);
392 Assert("#A10.2", MarshalObject.IsMethodOneWay);
394 finally
396 if(chn != null) ChannelServices.UnregisterChannel(chn);
400 [Test]
401 public void GetObjRefForProxy()
403 TcpChannel chn = null;
406 chn = new TcpChannel(1239);
407 ChannelServices.RegisterChannel(chn);
409 // Register le factory as a SAO
410 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObjectFactory), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap", WellKnownObjectMode.Singleton);
412 MarshalObjectFactory objFactory = (MarshalObjectFactory) Activator.GetObject(typeof(MarshalObjectFactory), "tcp://localhost:1239/MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap");
414 // Get a new "CAO"
415 MarshalObject objRem = objFactory.GetNewMarshalObject();
417 ObjRef objRefRem = RemotingServices.GetObjRefForProxy((MarshalByRefObject)objRem);
419 Assert("#A11", objRefRem != null);
421 finally
423 if(chn != null) ChannelServices.UnregisterChannel(chn);
427 // Tests GetRealProxy
428 [Test]
429 public void GetRealProxy()
431 TcpChannel chn = null;
434 chn = new TcpChannel(1241);
435 ChannelServices.RegisterChannel(chn);
437 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap", WellKnownObjectMode.Singleton);
439 MyProxy proxy = new MyProxy(typeof(MarshalObject), (MarshalByRefObject)Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1241/MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap"));
440 MarshalObject objRem = (MarshalObject) proxy.GetTransparentProxy();
442 RealProxy rp = RemotingServices.GetRealProxy(objRem);
444 Assert("#A12", rp != null);
445 AssertEquals("#A13", "MonoTests.System.Runtime.Remoting.RemotingServicesInternal.MyProxy", rp.GetType().ToString());
447 finally
449 if(chn != null) ChannelServices.UnregisterChannel(chn);
453 // Tests SetObjectUriForMarshal()
454 [Test]
455 public void SetObjectUriForMarshal()
457 TcpChannel chn = null;
460 chn = new TcpChannel(1242);
461 ChannelServices.RegisterChannel(chn);
463 MarshalObject objRem = NewMarshalObject();
464 RemotingServices.SetObjectUriForMarshal(objRem, objRem.Uri);
465 RemotingServices.Marshal(objRem);
467 objRem = (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1242/"+objRem.Uri);
468 Assert("#A14", objRem != null);
470 finally
472 if(chn != null) ChannelServices.UnregisterChannel(chn);
477 // Tests GetServeurTypeForUri()
478 [Test]
479 public void GetServeurTypeForUri()
481 TcpChannel chn = null;
482 Type type = typeof(MarshalObject);
485 chn = new TcpChannel(1243);
486 ChannelServices.RegisterChannel(chn);
488 MarshalObject objRem = NewMarshalObject();
489 RemotingServices.SetObjectUriForMarshal(objRem, objRem.Uri);
490 RemotingServices.Marshal(objRem);
492 Type typeRem = RemotingServices.GetServerTypeForUri(RemotingServices.GetObjectUri(objRem));
493 AssertEquals("#A15", type, typeRem);
495 finally
497 if(chn != null) ChannelServices.UnregisterChannel(chn);
501 // Tests IsObjectOutOfDomain
502 // Tests IsObjectOutOfContext
503 [Test]
504 public void IsObjectOutOf()
506 TcpChannel chn = null;
509 chn = new TcpChannel(1245);
510 ChannelServices.RegisterChannel(chn);
512 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), "MarshalObject.rem", WellKnownObjectMode.Singleton);
514 MarshalObject objRem = (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1245/MarshalObject.rem");
516 Assert("#A16", RemotingServices.IsObjectOutOfAppDomain(objRem));
517 Assert("#A17", RemotingServices.IsObjectOutOfContext(objRem));
519 MarshalObject objMarshal = new MarshalObject();
520 Assert("#A18", !RemotingServices.IsObjectOutOfAppDomain(objMarshal));
521 Assert("#A19", !RemotingServices.IsObjectOutOfContext(objMarshal));
523 finally
525 ChannelServices.UnregisterChannel(chn);
528 } // end class RemotingServicesTest
529 } // end of namespace MonoTests.System.Runtime.Remoting.RemotingServicesTest