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
33 get { return _mthBase; }
\r
36 public bool IsMethodOverloaded {
\r
37 get { return methodOverloaded; }
\r
40 public MyProxy (Type serverType, MarshalByRefObject target)
\r
43 this.target = target;
\r
45 IChannel [] registeredChannels = ChannelServices.RegisteredChannels;
\r
48 // A new IMessageSink chain has to be created
\r
49 // since the RemotingServices.GetEnvoyChainForProxy() is not yet
\r
51 foreach (IChannel channel in registeredChannels) {
\r
52 IChannelSender channelSender = channel as IChannelSender;
\r
53 if (channelSender != null) {
\r
54 _sink = (IMessageSink) channelSender.CreateMessageSink (RemotingServices.GetObjectUri (target), null, out ObjectURI);
\r
60 // Messages will be intercepted here and redirected
\r
61 // to another object.
\r
62 public override IMessage Invoke (IMessage msg)
\r
65 if (msg is IConstructionCallMessage) {
\r
66 IActivator remActivator = (IActivator) RemotingServices.Connect (typeof (IActivator), "tcp://localhost:1234/RemoteActivationService.rem");
\r
67 IConstructionReturnMessage crm = remActivator.Activate ((IConstructionCallMessage) msg);
\r
70 methodOverloaded = RemotingServices.IsMethodOverloaded ((IMethodMessage) msg);
\r
72 _mthBase = RemotingServices.GetMethodBaseFromMethodMessage ((IMethodMessage) msg);
\r
73 MethodCallMessageWrapper mcm = new MethodCallMessageWrapper ((IMethodCallMessage) msg);
\r
74 mcm.Uri = RemotingServices.GetObjectUri ((MarshalByRefObject) target);
\r
75 MarshalByRefObject objRem = (MarshalByRefObject) Activator.CreateInstance (GetProxiedType ());
\r
76 RemotingServices.ExecuteMessage ((MarshalByRefObject) objRem, (IMethodCallMessage) msg);
\r
77 IMessage rtnMsg = null;
\r
80 rtnMsg = _sink.SyncProcessMessage (msg);
\r
81 } catch (Exception e) {
\r
82 Console.WriteLine (e.Message);
\r
85 Console.WriteLine ("RR:" + rtnMsg);
\r
88 } catch (Exception ex) {
\r
89 Console.WriteLine (ex);
\r
95 // This class is used to create "CAO"
\r
96 public class MarshalObjectFactory : MarshalByRefObject
\r
98 public MarshalObject GetNewMarshalObject ()
\r
100 return new MarshalObject ();
\r
104 // A class used by the tests
\r
105 public class MarshalObject : ContextBoundObject
\r
107 public MarshalObject ()
\r
112 public MarshalObject (int id, string uri)
\r
120 set { id = value; }
\r
123 public string Uri {
\r
124 get { return uri; }
\r
127 public void Method1 ()
\r
130 methodOneWay = RemotingServices.IsOneWay (MethodBase.GetCurrentMethod ());
\r
133 public void Method2 ()
\r
135 methodOneWay = RemotingServices.IsOneWay (MethodBase.GetCurrentMethod ());
\r
138 public void Method2 (int i)
\r
140 methodOneWay = RemotingServices.IsOneWay (MethodBase.GetCurrentMethod ());
\r
144 public void Method3 ()
\r
146 methodOneWay = RemotingServices.IsOneWay (MethodBase.GetCurrentMethod ());
\r
149 public static int Called {
\r
150 get { return _called; }
\r
153 public static bool IsMethodOneWay {
\r
154 get { return methodOneWay; }
\r
157 private static int _called;
\r
158 private int id = 0;
\r
159 private string uri;
\r
160 private static bool methodOneWay = false;
\r
163 // Another class used by the tests
\r
164 public class DerivedMarshalObject : MarshalObject
\r
166 public DerivedMarshalObject () { }
\r
168 public DerivedMarshalObject (int id, string uri) : base (id, uri) { }
\r
179 public class CC : MarshalByRefObject
\r
183 public class DD : MarshalByRefObject
\r
188 } // namespace MonoTests.System.Runtime.Remoting.RemotingServicesInternal
\r
190 namespace MonoTests.Remoting
\r
192 using MonoTests.System.Runtime.Remoting.RemotingServicesInternal;
\r
194 // The main test class
\r
196 public class RemotingServicesTest : Assertion
\r
198 private static int MarshalObjectId = 0;
\r
200 public RemotingServicesTest ()
\r
202 MarshalObjectId = 0;
\r
205 // Helper function that create a new
\r
206 // MarshalObject with an unique ID
\r
207 private static MarshalObject NewMarshalObject ()
\r
209 string uri = "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject" + MarshalObjectId.ToString ();
\r
210 MarshalObject objMarshal = new MarshalObject (MarshalObjectId, uri);
\r
217 // Another helper function
\r
218 private DerivedMarshalObject NewDerivedMarshalObject ()
\r
220 string uri = "MonoTests.System.Runtime.Remoting.RemotingServicesTest.DerivedMarshalObject" + MarshalObjectId.ToString ();
\r
221 DerivedMarshalObject objMarshal = new DerivedMarshalObject (MarshalObjectId, uri);
\r
228 // The two folling method test RemotingServices.Marshal()
\r
230 public void Marshal1 ()
\r
233 MarshalObject objMarshal = NewMarshalObject ();
\r
234 ObjRef objRef = RemotingServices.Marshal (objMarshal);
\r
236 Assert ("#A01", objRef.URI != null);
\r
238 MarshalObject objRem = (MarshalObject) RemotingServices.Unmarshal (objRef);
\r
239 AssertEquals ("#A02", objMarshal.Id, objRem.Id);
\r
242 AssertEquals ("#A03", objMarshal.Id, objRem.Id);
\r
244 // TODO: uncomment when RemotingServices.Disconnect is implemented
\r
245 //RemotingServices.Disconnect(objMarshal);
\r
247 objMarshal = NewMarshalObject ();
\r
249 objRef = RemotingServices.Marshal (objMarshal, objMarshal.Uri);
\r
251 Assert ("#A04", objRef.URI.EndsWith (objMarshal.Uri));
\r
252 // TODO: uncomment when RemotingServices.Disconnect is implemented
\r
253 //RemotingServices.Disconnect(objMarshal);
\r
257 public void Marshal2 ()
\r
259 DerivedMarshalObject derivedObjMarshal = NewDerivedMarshalObject ();
\r
261 ObjRef objRef = RemotingServices.Marshal (derivedObjMarshal, derivedObjMarshal.Uri, typeof (MarshalObject));
\r
263 // Check that the type of the marshaled object is MarshalObject
\r
264 Assert ("#A05", objRef.TypeInfo.TypeName.StartsWith ((typeof (MarshalObject)).ToString ()));
\r
266 // TODO: uncomment when RemotingServices.Disconnect is implemented
\r
267 //RemotingServices.Disconnect(derivedObjMarshal);
\r
270 // Tests RemotingServices.GetObjectUri()
\r
272 public void GetObjectUri ()
\r
274 MarshalObject objMarshal = NewMarshalObject ();
\r
276 Assert ("#A06", RemotingServices.GetObjectUri (objMarshal) == null);
\r
278 RemotingServices.Marshal (objMarshal);
\r
280 Assert ("#A07", RemotingServices.GetObjectUri (objMarshal) != null);
\r
281 // TODO: uncomment when RemotingServices.Disconnect is implemented
\r
282 //RemotingServices.Disconnect(objMarshal);
\r
285 // Tests RemotingServices.Connect
\r
287 public void Connect ()
\r
289 MarshalObject objMarshal = NewMarshalObject ();
\r
291 IDictionary props = new Hashtable ();
\r
292 props ["name"] = objMarshal.Uri;
\r
293 props ["port"] = 1236;
\r
294 TcpChannel chn = new TcpChannel (props, null, null);
\r
295 ChannelServices.RegisterChannel (chn);
\r
298 RemotingServices.Marshal (objMarshal, objMarshal.Uri);
\r
299 MarshalObject objRem = (MarshalObject) RemotingServices.Connect (typeof (MarshalObject), "tcp://localhost:1236/" + objMarshal.Uri);
\r
300 Assert ("#A08", RemotingServices.IsTransparentProxy (objRem));
\r
302 ChannelServices.UnregisterChannel (chn);
\r
303 RemotingServices.Disconnect (objMarshal);
\r
307 // Tests RemotingServices.Marshal()
\r
309 public void MarshalThrowException ()
\r
311 MarshalObject objMarshal = NewMarshalObject ();
\r
313 IDictionary props = new Hashtable ();
\r
314 props ["name"] = objMarshal.Uri;
\r
315 props ["port"] = 1237;
\r
316 TcpChannel chn = new TcpChannel (props, null, null);
\r
317 ChannelServices.RegisterChannel (chn);
\r
320 RemotingServices.Marshal (objMarshal, objMarshal.Uri);
\r
321 MarshalObject objRem = (MarshalObject) RemotingServices.Connect (typeof (MarshalObject), "tcp://localhost:1237/" + objMarshal.Uri);
\r
322 // This line should throw a RemotingException
\r
323 // It is forbidden to export an object which is not
\r
326 RemotingServices.Marshal (objRem, objMarshal.Uri);
\r
328 } catch (RemotingException e) {
\r
331 ChannelServices.UnregisterChannel (chn);
\r
333 // TODO: uncomment when RemotingServices.Disconnect is implemented
\r
334 //RemotingServices.Disconnect(objMarshal);
\r
338 // Tests RemotingServices.ExecuteMessage()
\r
339 // also tests GetMethodBaseFromMessage()
\r
340 // IsMethodOverloaded()
\r
342 public void ExecuteMessage ()
\r
344 TcpChannel chn = new TcpChannel (1235);
\r
345 ChannelServices.RegisterChannel (chn);
\r
347 MarshalObject objMarshal = NewMarshalObject ();
\r
348 RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), objMarshal.Uri, WellKnownObjectMode.SingleCall);
\r
350 // use a proxy to catch the Message
\r
351 MyProxy proxy = new MyProxy (typeof (MarshalObject), (MarshalObject) Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1235/" + objMarshal.Uri));
\r
353 MarshalObject objRem = (MarshalObject) proxy.GetTransparentProxy ();
\r
357 // Tests RemotingServices.GetMethodBaseFromMethodMessage()
\r
358 AssertEquals ("#A09", "Method1", proxy.MthBase.Name);
\r
359 Assert ("#A09.1", !proxy.IsMethodOverloaded);
\r
362 Assert ("#A09.2", proxy.IsMethodOverloaded);
\r
364 // Tests RemotingServices.ExecuteMessage();
\r
365 // If ExecuteMessage does it job well, Method1 should be called 2 times
\r
366 AssertEquals ("#A10", 2, MarshalObject.Called);
\r
368 ChannelServices.UnregisterChannel (chn);
\r
372 // Tests the IsOneWay method
\r
374 public void IsOneWay ()
\r
376 TcpChannel chn = new TcpChannel (1238);
\r
377 ChannelServices.RegisterChannel (chn);
\r
379 RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), "MarshalObject.rem", WellKnownObjectMode.Singleton);
\r
381 MarshalObject objRem = (MarshalObject) Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1238/MarshalObject.rem");
\r
383 Assert ("#A10.1", RemotingServices.IsTransparentProxy (objRem));
\r
387 Assert ("#A10.2", !MarshalObject.IsMethodOneWay);
\r
390 Assert ("#A10.3", MarshalObject.IsMethodOneWay);
\r
392 ChannelServices.UnregisterChannel (chn);
\r
397 public void GetObjRefForProxy ()
\r
399 TcpChannel chn = new TcpChannel (1239);
\r
400 ChannelServices.RegisterChannel (chn);
\r
402 // Register le factory as a SAO
\r
403 RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObjectFactory), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap", WellKnownObjectMode.Singleton);
\r
405 MarshalObjectFactory objFactory = (MarshalObjectFactory) Activator.GetObject (typeof (MarshalObjectFactory), "tcp://localhost:1239/MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap");
\r
408 MarshalObject objRem = objFactory.GetNewMarshalObject ();
\r
410 ObjRef objRefRem = RemotingServices.GetObjRefForProxy ((MarshalByRefObject) objRem);
\r
412 Assert ("#A11", objRefRem != null);
\r
414 ChannelServices.UnregisterChannel (chn);
\r
418 // Tests GetRealProxy
\r
420 public void GetRealProxy ()
\r
422 TcpChannel chn = new TcpChannel (1241);
\r
423 ChannelServices.RegisterChannel (chn);
\r
425 RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap", WellKnownObjectMode.Singleton);
\r
427 MyProxy proxy = new MyProxy (typeof (MarshalObject), (MarshalByRefObject) Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1241/MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap"));
\r
428 MarshalObject objRem = (MarshalObject) proxy.GetTransparentProxy ();
\r
430 RealProxy rp = RemotingServices.GetRealProxy (objRem);
\r
432 Assert ("#A12", rp != null);
\r
433 AssertEquals ("#A13", "MonoTests.System.Runtime.Remoting.RemotingServicesInternal.MyProxy", rp.GetType ().ToString ());
\r
435 ChannelServices.UnregisterChannel (chn);
\r
439 // Tests SetObjectUriForMarshal()
\r
441 public void SetObjectUriForMarshal ()
\r
443 TcpChannel chn = new TcpChannel (1242);
\r
444 ChannelServices.RegisterChannel (chn);
\r
446 MarshalObject objRem = NewMarshalObject ();
\r
447 RemotingServices.SetObjectUriForMarshal (objRem, objRem.Uri);
\r
448 RemotingServices.Marshal (objRem);
\r
450 objRem = (MarshalObject) Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1242/" + objRem.Uri);
\r
451 Assert ("#A14", objRem != null);
\r
453 ChannelServices.UnregisterChannel (chn);
\r
458 // Tests GetServeurTypeForUri()
\r
460 public void GetServeurTypeForUri ()
\r
462 TcpChannel chn = new TcpChannel (1243);
\r
463 Type type = typeof (MarshalObject);
\r
464 ChannelServices.RegisterChannel (chn);
\r
466 MarshalObject objRem = NewMarshalObject ();
\r
467 RemotingServices.SetObjectUriForMarshal (objRem, objRem.Uri);
\r
468 RemotingServices.Marshal (objRem);
\r
470 Type typeRem = RemotingServices.GetServerTypeForUri (RemotingServices.GetObjectUri (objRem));
\r
471 AssertEquals ("#A15", type, typeRem);
\r
473 ChannelServices.UnregisterChannel (chn);
\r
477 // Tests IsObjectOutOfDomain
\r
478 // Tests IsObjectOutOfContext
\r
480 [Category ("NotWorking")]
\r
481 public void IsObjectOutOf ()
\r
483 TcpChannel chn = new TcpChannel (1245);
\r
484 ChannelServices.RegisterChannel (chn);
\r
486 RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), "MarshalObject2.rem", WellKnownObjectMode.Singleton);
\r
488 MarshalObject objRem = (MarshalObject) Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1245/MarshalObject2.rem");
\r
490 Assert ("#A16", RemotingServices.IsObjectOutOfAppDomain (objRem));
\r
491 Assert ("#A17", RemotingServices.IsObjectOutOfContext (objRem));
\r
493 MarshalObject objMarshal = new MarshalObject ();
\r
494 Assert ("#A18", !RemotingServices.IsObjectOutOfAppDomain (objMarshal));
\r
495 Assert ("#A19", !RemotingServices.IsObjectOutOfContext (objMarshal));
\r
497 ChannelServices.UnregisterChannel (chn);
\r
502 public void ApplicationNameTest ()
\r
504 RemotingConfiguration.ApplicationName = "app";
\r
505 TcpChannel chn = new TcpChannel (1246);
\r
506 ChannelServices.RegisterChannel (chn);
\r
508 RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), "obj3.rem", WellKnownObjectMode.Singleton);
\r
510 MarshalObject objRem = (MarshalObject) Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1246/app/obj3.rem");
\r
511 MarshalObject objRem2 = (MarshalObject) Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1246/obj3.rem");
\r
513 Assert ("#AN1", RemotingServices.IsTransparentProxy (objRem));
\r
514 Assert ("#AN2", RemotingServices.IsTransparentProxy (objRem2));
\r
516 AssertNotNull ("#AN3", RemotingServices.GetServerTypeForUri ("obj3.rem"));
\r
517 AssertNotNull ("#AN4", RemotingServices.GetServerTypeForUri ("/app/obj3.rem"));
\r
518 AssertNull ("#AN5", RemotingServices.GetServerTypeForUri ("//app/obj3.rem"));
\r
519 AssertNull ("#AN6", RemotingServices.GetServerTypeForUri ("app/obj3.rem"));
\r
520 AssertNull ("#AN7", RemotingServices.GetServerTypeForUri ("/whatever/obj3.rem"));
\r
521 AssertNotNull ("#AN8", RemotingServices.GetServerTypeForUri ("/obj3.rem"));
\r
522 AssertNull ("#AN9", RemotingServices.GetServerTypeForUri ("//obj3.rem"));
\r
524 ChannelServices.UnregisterChannel (chn);
\r
529 public void GetObjectWithChannelDataTest ()
\r
531 TcpChannel chn = new TcpChannel (1247);
\r
532 ChannelServices.RegisterChannel (chn);
\r
534 RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), "getobjectwithchanneldata.rem", WellKnownObjectMode.Singleton);
\r
536 string channelData = "test";
\r
537 AssertNotNull ("#01", Activator.GetObject (typeof (MarshalObject), "tcp://localhost:1247/getobjectwithchanneldata.rem", channelData));
\r
539 ChannelServices.UnregisterChannel (chn);
\r
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")]
\r
545 public void ConnectProxyCast ()
\r
548 RemotingConfiguration.Configure (null);
\r
550 o = RemotingServices.Connect (typeof (MarshalByRefObject), "tcp://localhost:3434/ff1.rem");
\r
551 Assert ("#m1", o is DD);
\r
552 Assert ("#m2", o is A);
\r
553 Assert ("#m3", o is B);
\r
554 Assert ("#m4", !(o is CC));
\r
556 o = RemotingServices.Connect (typeof (A), "tcp://localhost:3434/ff3.rem");
\r
557 Assert ("#a1", o is DD);
\r
558 Assert ("#a2", o is A);
\r
559 Assert ("#a3", o is B);
\r
560 Assert ("#a4", !(o is CC));
\r
562 o = RemotingServices.Connect (typeof (DD), "tcp://localhost:3434/ff4.rem");
\r
563 Assert ("#d1", o is DD);
\r
564 Assert ("#d2", o is A);
\r
565 Assert ("#d3", o is B);
\r
566 Assert ("#d4", !(o is CC));
\r
568 o = RemotingServices.Connect (typeof (CC), "tcp://localhost:3434/ff5.rem");
\r
569 Assert ("#c1", !(o is DD));
\r
570 Assert ("#c2", o is A);
\r
571 Assert ("#c3", o is B);
\r
572 Assert ("#c4", o is CC);
\r
574 // Don't add any tests that must create channels
\r
575 // after ConnectProxyCast (), because this test calls
\r
576 // RemotingConfiguration.Configure ().
\r
577 } // end class RemotingServicesTest
\r
578 } // end of namespace MonoTests.Remoting
\r