2 // System.Runtime.Remoting.RemotingServices NUnit V2.0 test class
\r
4 // Author Jean-Marc ANDRE (jean-marc.andre@polymtl.ca)
\r
6 // ToDo: I didn't write test functions for the method not yep
\r
7 // implemented by Mono
\r
10 using System.Collections;
\r
11 using NUnit.Framework;
\r
12 using System.Reflection;
\r
13 using System.Runtime.Remoting;
\r
14 using System.Threading;
\r
15 using System.Runtime.Remoting.Activation;
\r
16 using System.Runtime.Remoting.Messaging;
\r
17 using System.Runtime.Remoting.Proxies;
\r
18 using System.Runtime.Remoting.Channels;
\r
19 using System.Runtime.Remoting.Channels.Tcp;
\r
21 namespace MonoTests.System.Runtime.Remoting.RemotingServicesInternal
\r
23 // We need our own proxy to intercept messages to remote object
\r
24 // and forward them using RemotingServices.ExecuteMessage
\r
25 public class MyProxy: RealProxy
\r
27 MarshalByRefObject target;
\r
29 MethodBase _mthBase;
\r
30 bool methodOverloaded = false;
\r
32 public MethodBase MthBase
\r
34 get{ return _mthBase;}
\r
37 public bool IsMethodOverloaded
\r
39 get{return methodOverloaded;}
\r
42 public MyProxy(Type serverType, MarshalByRefObject target): base(serverType)
\r
44 this.target = target;
\r
46 IChannel[] registeredChannels = ChannelServices.RegisteredChannels;
\r
49 // A new IMessageSink chain has to be created
\r
50 // since the RemotingServices.GetEnvoyChainForProxy() is not yet
\r
52 foreach(IChannel channel in registeredChannels)
\r
54 IChannelSender channelSender = channel as IChannelSender;
\r
55 if(channelSender != null)
\r
57 _sink = (IMessageSink) channelSender.CreateMessageSink(RemotingServices.GetObjectUri(target), null, out ObjectURI);
\r
63 // Messages will be intercepted here and redirected
\r
64 // to another object.
\r
65 public override IMessage Invoke(IMessage msg)
\r
69 if(msg is IConstructionCallMessage)
\r
71 IActivator remActivator = (IActivator) RemotingServices.Connect(typeof(IActivator), "tcp://localhost:1234/RemoteActivationService.rem");
\r
72 IConstructionReturnMessage crm = remActivator.Activate((IConstructionCallMessage)msg);
\r
77 methodOverloaded = RemotingServices.IsMethodOverloaded((IMethodMessage)msg);
\r
79 _mthBase = RemotingServices.GetMethodBaseFromMethodMessage((IMethodMessage)msg);
\r
80 MethodCallMessageWrapper mcm = new MethodCallMessageWrapper((IMethodCallMessage) msg);
\r
81 mcm.Uri = RemotingServices.GetObjectUri((MarshalByRefObject)target);
\r
82 MarshalByRefObject objRem = (MarshalByRefObject)Activator.CreateInstance(GetProxiedType());
\r
83 RemotingServices.ExecuteMessage((MarshalByRefObject)objRem, (IMethodCallMessage)msg);
\r
84 IMessage rtnMsg = null;
\r
88 rtnMsg = _sink.SyncProcessMessage(msg);
\r
92 Console.WriteLine(e.Message);
\r
95 Console.WriteLine ("RR:" + rtnMsg);
\r
99 catch (Exception ex)
\r
101 Console.WriteLine (ex);
\r
107 // This class is used to create "CAO"
\r
108 public class MarshalObjectFactory: MarshalByRefObject
\r
110 public MarshalObject GetNewMarshalObject()
\r
112 return new MarshalObject();
\r
116 // A class used by the tests
\r
117 public class MarshalObject: ContextBoundObject
\r
119 public MarshalObject()
\r
124 public MarshalObject(int id, string uri)
\r
139 public void Method1()
\r
142 methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());
\r
145 public void Method2()
\r
147 methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());
\r
150 public void Method2(int i)
\r
152 methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());
\r
157 public void Method3()
\r
159 methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());
\r
163 public static int Called
\r
165 get{return _called;}
\r
168 public static bool IsMethodOneWay
\r
170 get{return methodOneWay;}
\r
174 private static int _called;
\r
175 private int id = 0;
\r
176 private string uri;
\r
177 private static bool methodOneWay = false;
\r
180 // Another class used by the tests
\r
181 public class DerivedMarshalObject: MarshalObject
\r
183 public DerivedMarshalObject(){}
\r
185 public DerivedMarshalObject(int id, string uri): base(id, uri) {}
\r
196 public class CC: MarshalByRefObject
\r
200 public class DD: MarshalByRefObject
\r
205 } // namespace MonoTests.System.Runtime.Remoting.RemotingServicesInternal
\r
207 namespace MonoTests.Remoting
\r
209 using MonoTests.System.Runtime.Remoting.RemotingServicesInternal;
\r
211 // The main test class
\r
213 public class RemotingServicesTest : Assertion
\r
215 private static int MarshalObjectId = 0;
\r
217 public RemotingServicesTest()
\r
219 MarshalObjectId = 0;
\r
222 // Helper function that create a new
\r
223 // MarshalObject with an unique ID
\r
224 private static MarshalObject NewMarshalObject()
\r
226 string uri = "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject" + MarshalObjectId.ToString();
\r
227 MarshalObject objMarshal = new MarshalObject(MarshalObjectId, uri);
\r
234 // Another helper function
\r
235 private DerivedMarshalObject NewDerivedMarshalObject()
\r
237 string uri = "MonoTests.System.Runtime.Remoting.RemotingServicesTest.DerivedMarshalObject" + MarshalObjectId.ToString();
\r
238 DerivedMarshalObject objMarshal = new DerivedMarshalObject(MarshalObjectId, uri);
\r
245 // The two folling method test RemotingServices.Marshal()
\r
247 public void Marshal1()
\r
250 MarshalObject objMarshal = NewMarshalObject();
\r
251 ObjRef objRef = RemotingServices.Marshal(objMarshal);
\r
253 Assert("#A01", objRef.URI != null);
\r
255 MarshalObject objRem = (MarshalObject) RemotingServices.Unmarshal(objRef);
\r
256 AssertEquals("#A02", objMarshal.Id, objRem.Id);
\r
259 AssertEquals("#A03", objMarshal.Id, objRem.Id);
\r
261 // TODO: uncomment when RemotingServices.Disconnect is implemented
\r
262 //RemotingServices.Disconnect(objMarshal);
\r
264 objMarshal = NewMarshalObject();
\r
266 objRef = RemotingServices.Marshal(objMarshal, objMarshal.Uri);
\r
268 Assert("#A04", objRef.URI.EndsWith(objMarshal.Uri));
\r
269 // TODO: uncomment when RemotingServices.Disconnect is implemented
\r
270 //RemotingServices.Disconnect(objMarshal);
\r
274 public void Marshal2()
\r
276 DerivedMarshalObject derivedObjMarshal = NewDerivedMarshalObject();
\r
278 ObjRef objRef = RemotingServices.Marshal(derivedObjMarshal, derivedObjMarshal.Uri, typeof(MarshalObject));
\r
280 // Check that the type of the marshaled object is MarshalObject
\r
281 Assert("#A05", objRef.TypeInfo.TypeName.StartsWith((typeof(MarshalObject)).ToString()));
\r
283 // TODO: uncomment when RemotingServices.Disconnect is implemented
\r
284 //RemotingServices.Disconnect(derivedObjMarshal);
\r
287 // Tests RemotingServices.GetObjectUri()
\r
289 public void GetObjectUri()
\r
291 MarshalObject objMarshal = NewMarshalObject();
\r
293 Assert("#A06", RemotingServices.GetObjectUri(objMarshal) == null);
\r
295 ObjRef objRef = RemotingServices.Marshal(objMarshal);
\r
297 Assert("#A07", RemotingServices.GetObjectUri(objMarshal) != null);
\r
298 // TODO: uncomment when RemotingServices.Disconnect is implemented
\r
299 //RemotingServices.Disconnect(objMarshal);
\r
302 // Tests RemotingServices.Connect
\r
304 public void Connect()
\r
306 MarshalObject objMarshal = NewMarshalObject();
\r
308 IDictionary props = new Hashtable();
\r
309 props["name"] = objMarshal.Uri;
\r
310 props["port"] = 1236;
\r
311 TcpChannel chn = new TcpChannel(props, null, null);
\r
312 ChannelServices.RegisterChannel(chn);
\r
314 RemotingServices.Marshal(objMarshal,objMarshal.Uri);
\r
316 MarshalObject objRem = (MarshalObject) RemotingServices.Connect(typeof(MarshalObject), "tcp://localhost:1236/" + objMarshal.Uri);
\r
318 Assert("#A08", RemotingServices.IsTransparentProxy(objRem));
\r
320 ChannelServices.UnregisterChannel(chn);
\r
322 RemotingServices.Disconnect(objMarshal);
\r
325 // Tests RemotingServices.Marshal()
\r
327 [ExpectedException(typeof(RemotingException))]
\r
328 public void MarshalThrowException()
\r
330 MarshalObject objMarshal = NewMarshalObject();
\r
332 IDictionary props = new Hashtable();
\r
333 props["name"] = objMarshal.Uri;
\r
334 props["port"] = 1237;
\r
335 TcpChannel chn = new TcpChannel(props, null, null);
\r
336 ChannelServices.RegisterChannel(chn);
\r
338 RemotingServices.Marshal(objMarshal,objMarshal.Uri);
\r
340 MarshalObject objRem = (MarshalObject) RemotingServices.Connect(typeof(MarshalObject), "tcp://localhost:1237/" + objMarshal.Uri);
\r
341 // This line sould throw a RemotingException
\r
342 // It is forbidden to export an object which is not
\r
346 RemotingServices.Marshal(objRem, objMarshal.Uri);
\r
350 ChannelServices.UnregisterChannel(chn);
\r
352 // TODO: uncomment when RemotingServices.Disconnect is implemented
\r
353 //RemotingServices.Disconnect(objMarshal);
\r
359 // Tests RemotingServices.ExecuteMessage()
\r
360 // also tests GetMethodBaseFromMessage()
\r
361 // IsMethodOverloaded()
\r
363 public void ExecuteMessage()
\r
365 TcpChannel chn = null;
\r
368 chn = new TcpChannel(1235);
\r
369 ChannelServices.RegisterChannel(chn);
\r
371 MarshalObject objMarshal = NewMarshalObject();
\r
372 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), objMarshal.Uri, WellKnownObjectMode.SingleCall);
\r
374 // use a proxy to catch the Message
\r
375 MyProxy proxy = new MyProxy(typeof(MarshalObject), (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1235/" + objMarshal.Uri));
\r
377 MarshalObject objRem = (MarshalObject) proxy.GetTransparentProxy();
\r
381 // Tests RemotingServices.GetMethodBaseFromMethodMessage()
\r
382 AssertEquals("#A09","Method1",proxy.MthBase.Name);
\r
383 Assert("#A09.1", !proxy.IsMethodOverloaded);
\r
386 Assert("#A09.2", proxy.IsMethodOverloaded);
\r
388 // Tests RemotingServices.ExecuteMessage();
\r
389 // If ExecuteMessage does it job well, Method1 should be called 2 times
\r
390 AssertEquals("#A10", 2, MarshalObject.Called);
\r
394 if(chn != null) ChannelServices.UnregisterChannel(chn);
\r
398 // Tests the IsOneWay method
\r
400 public void IsOneWay()
\r
402 TcpChannel chn = null;
\r
405 chn = new TcpChannel(1238);
\r
406 ChannelServices.RegisterChannel(chn);
\r
407 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), "MarshalObject.rem", WellKnownObjectMode.Singleton);
\r
409 MarshalObject objRem = (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1238/MarshalObject.rem");
\r
411 Assert("#A10.1", RemotingServices.IsTransparentProxy(objRem));
\r
415 Assert("#A10.2", !MarshalObject.IsMethodOneWay);
\r
418 Assert("#A10.3", MarshalObject.IsMethodOneWay);
\r
422 if(chn != null) ChannelServices.UnregisterChannel(chn);
\r
427 public void GetObjRefForProxy()
\r
429 TcpChannel chn = null;
\r
432 chn = new TcpChannel(1239);
\r
433 ChannelServices.RegisterChannel(chn);
\r
435 // Register le factory as a SAO
\r
436 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObjectFactory), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap", WellKnownObjectMode.Singleton);
\r
438 MarshalObjectFactory objFactory = (MarshalObjectFactory) Activator.GetObject(typeof(MarshalObjectFactory), "tcp://localhost:1239/MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap");
\r
441 MarshalObject objRem = objFactory.GetNewMarshalObject();
\r
443 ObjRef objRefRem = RemotingServices.GetObjRefForProxy((MarshalByRefObject)objRem);
\r
445 Assert("#A11", objRefRem != null);
\r
449 if(chn != null) ChannelServices.UnregisterChannel(chn);
\r
453 // Tests GetRealProxy
\r
455 public void GetRealProxy()
\r
457 TcpChannel chn = null;
\r
460 chn = new TcpChannel(1241);
\r
461 ChannelServices.RegisterChannel(chn);
\r
463 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap", WellKnownObjectMode.Singleton);
\r
465 MyProxy proxy = new MyProxy(typeof(MarshalObject), (MarshalByRefObject)Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1241/MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap"));
\r
466 MarshalObject objRem = (MarshalObject) proxy.GetTransparentProxy();
\r
468 RealProxy rp = RemotingServices.GetRealProxy(objRem);
\r
470 Assert("#A12", rp != null);
\r
471 AssertEquals("#A13", "MonoTests.System.Runtime.Remoting.RemotingServicesInternal.MyProxy", rp.GetType().ToString());
\r
475 if(chn != null) ChannelServices.UnregisterChannel(chn);
\r
479 // Tests SetObjectUriForMarshal()
\r
481 public void SetObjectUriForMarshal()
\r
483 TcpChannel chn = null;
\r
486 chn = new TcpChannel(1242);
\r
487 ChannelServices.RegisterChannel(chn);
\r
489 MarshalObject objRem = NewMarshalObject();
\r
490 RemotingServices.SetObjectUriForMarshal(objRem, objRem.Uri);
\r
491 RemotingServices.Marshal(objRem);
\r
493 objRem = (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1242/"+objRem.Uri);
\r
494 Assert("#A14", objRem != null);
\r
498 if(chn != null) ChannelServices.UnregisterChannel(chn);
\r
503 // Tests GetServeurTypeForUri()
\r
505 public void GetServeurTypeForUri()
\r
507 TcpChannel chn = null;
\r
508 Type type = typeof(MarshalObject);
\r
511 chn = new TcpChannel(1243);
\r
512 ChannelServices.RegisterChannel(chn);
\r
514 MarshalObject objRem = NewMarshalObject();
\r
515 RemotingServices.SetObjectUriForMarshal(objRem, objRem.Uri);
\r
516 RemotingServices.Marshal(objRem);
\r
518 Type typeRem = RemotingServices.GetServerTypeForUri(RemotingServices.GetObjectUri(objRem));
\r
519 AssertEquals("#A15", type, typeRem);
\r
523 if(chn != null) ChannelServices.UnregisterChannel(chn);
\r
527 // Tests IsObjectOutOfDomain
\r
528 // Tests IsObjectOutOfContext
\r
530 public void IsObjectOutOf()
\r
532 TcpChannel chn = null;
\r
535 chn = new TcpChannel(1245);
\r
536 ChannelServices.RegisterChannel(chn);
\r
538 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), "MarshalObject2.rem", WellKnownObjectMode.Singleton);
\r
540 MarshalObject objRem = (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1245/MarshalObject2.rem");
\r
542 Assert("#A16", RemotingServices.IsObjectOutOfAppDomain(objRem));
\r
543 Assert("#A17", RemotingServices.IsObjectOutOfContext(objRem));
\r
545 MarshalObject objMarshal = new MarshalObject();
\r
546 Assert("#A18", !RemotingServices.IsObjectOutOfAppDomain(objMarshal));
\r
547 Assert("#A19", !RemotingServices.IsObjectOutOfContext(objMarshal));
\r
551 ChannelServices.UnregisterChannel(chn);
\r
556 public void ConnectProxyCast ()
\r
559 RemotingConfiguration.Configure (null);
\r
561 o = RemotingServices.Connect (typeof(MarshalByRefObject), "tcp://localhost:3434/ff1.rem");
\r
562 Assert ("#m1", o is DD);
\r
563 Assert ("#m2", o is A);
\r
564 Assert ("#m3", o is B);
\r
565 Assert ("#m4", !(o is CC));
\r
567 o = RemotingServices.Connect (typeof(A), "tcp://localhost:3434/ff3.rem");
\r
568 Assert ("#a1", o is DD);
\r
569 Assert ("#a2", o is A);
\r
570 Assert ("#a3", o is B);
\r
571 Assert ("#a4", !(o is CC));
\r
573 o = RemotingServices.Connect (typeof(DD), "tcp://localhost:3434/ff4.rem");
\r
574 Assert ("#d1", o is DD);
\r
575 Assert ("#d2", o is A);
\r
576 Assert ("#d3", o is B);
\r
577 Assert ("#d4", !(o is CC));
\r
579 o = RemotingServices.Connect (typeof(CC), "tcp://localhost:3434/ff5.rem");
\r
580 Assert ("#c1", !(o is DD));
\r
581 Assert ("#c2", o is A);
\r
582 Assert ("#c3", o is B);
\r
583 Assert ("#c4", o is CC);
\r
586 } // end class RemotingServicesTest
\r
587 } // end of namespace MonoTests.Remoting
\r