[System] Fix TDS 7.0 prepared InputOutput parameter info
[mono.git] / mcs / class / corlib / Test / System.Runtime.Remoting / RemotingServicesTest.cs
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
8
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;
20
21 namespace MonoTests.System.Runtime.Remoting.RemotingServicesInternal
22 {
23         // We need our own proxy to intercept messages to remote object
24         // and forward them using RemotingServices.ExecuteMessage
25         public class MyProxy: RealProxy
26         {
27                 MarshalByRefObject target;
28                 IMessageSink _sink;
29                 MethodBase _mthBase;
30                 bool methodOverloaded = false;
31                 
32                 public MethodBase MthBase
33                 {
34                         get{ return _mthBase;}
35                 }
36                 
37                 public bool IsMethodOverloaded
38                 {
39                         get{return methodOverloaded;}
40                 }
41                 
42                 public MyProxy(Type serverType, MarshalByRefObject target): base(serverType)
43                 {
44                         this.target = target;
45                         
46                         IChannel[] registeredChannels = ChannelServices.RegisteredChannels;
47                         string ObjectURI;
48                         
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)
53                         {
54                                 IChannelSender channelSender = channel as IChannelSender;
55                                 if(channelSender != null)
56                                 {
57                                         _sink = (IMessageSink) channelSender.CreateMessageSink(RemotingServices.GetObjectUri(target), null, out ObjectURI);
58                                 }
59                         }
60                         
61                 }
62                 
63                 // Messages will be intercepted here and redirected
64                 // to another object.
65                 public override IMessage Invoke(IMessage msg)
66                 {
67                         if(msg is IConstructionCallMessage)
68                         {
69                                 IActivator remActivator = (IActivator) RemotingServices.Connect(typeof(IActivator), "tcp://localhost:1234/RemoteActivationService.rem");
70                                 IConstructionReturnMessage crm = remActivator.Activate((IConstructionCallMessage)msg);
71                                 return crm;
72                         }
73                         else
74                         {
75                                 methodOverloaded = RemotingServices.IsMethodOverloaded((IMethodMessage)msg);
76                                 
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;
83                                 
84                                 try
85                                 {
86                                         rtnMsg = _sink.SyncProcessMessage(msg);
87                                 }
88                                 catch(Exception e)
89                                 {
90                                         Console.WriteLine(e.Message);
91                                 }
92                                 
93                                 return rtnMsg;
94                         }
95                 }
96         } // end MyProxy
97         
98         // This class is used to create "CAO"
99         public class MarshalObjectFactory: MarshalByRefObject
100         {
101                 public MarshalObject GetNewMarshalObject()
102                 {
103                         return new MarshalObject();
104                 }
105         }
106         
107         // A class used by the tests
108         public class MarshalObject: ContextBoundObject
109         {
110                 public MarshalObject()
111                 {
112                         
113                 }
114                 
115                 public MarshalObject(int id, string uri)
116                 {
117                         this.id = id;
118                         this.uri = uri;
119                 }
120                 public int Id
121                 {
122                         get{return id;}
123                         set{id = value;}
124                 }
125                 public string Uri
126                 {
127                         get{return uri;}
128                 }
129                 
130                 public void Method1()
131                 {
132                         _called++;
133                         methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());                        
134                 }
135                 
136                 public void Method2()
137                 {
138                         methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());                        
139                 }
140                 
141                 public void Method2(int i)
142                 {
143                         methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());                        
144                         
145                 }
146                 
147                 [OneWay()]
148                 public void Method3()
149                 {
150                         methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());                        
151                         
152                 }
153                 
154                 public static int Called
155                 {
156                         get{return _called;}
157                 }
158                 
159                 public static bool IsMethodOneWay
160                 {
161                         get{return methodOneWay;}
162                 }
163                 
164                 
165                 private static int _called;
166                 private int id = 0;
167                 private string uri;
168                 private static bool methodOneWay = false;
169         }
170         
171         // Another class used by the tests
172         public class DerivedMarshalObject: MarshalObject
173         {
174                 public DerivedMarshalObject(){}
175                 
176                 public DerivedMarshalObject(int id, string uri): base(id, uri) {}
177         }
178 } // namespace MonoTests.System.Runtime.Remoting.RemotingServicesInternal
179
180 namespace MonoTests.System.Runtime.Remoting
181 {
182         using MonoTests.System.Runtime.Remoting.RemotingServicesInternal;
183         
184         // The main test class
185         [TestFixture]
186         public class RemotingServicesTest 
187         {
188                 private static int MarshalObjectId = 0;
189                         
190                 public RemotingServicesTest()
191                 {
192                         MarshalObjectId = 0;
193                 }
194                 
195                 // Helper function that create a new
196                 // MarshalObject with an unique ID
197                 private static MarshalObject NewMarshalObject()
198                 {
199                         string uri = "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject" + MarshalObjectId.ToString();
200                         MarshalObject objMarshal = new MarshalObject(MarshalObjectId, uri);
201                         
202                         MarshalObjectId++;
203                         
204                         return objMarshal;
205                 }
206                 
207                 // Another helper function
208                 private DerivedMarshalObject NewDerivedMarshalObject()
209                 {
210                         string uri = "MonoTests.System.Runtime.Remoting.RemotingServicesTest.DerivedMarshalObject" + MarshalObjectId.ToString();
211                         DerivedMarshalObject objMarshal = new DerivedMarshalObject(MarshalObjectId, uri);
212                         
213                         MarshalObjectId++;
214                         
215                         return objMarshal;
216                 }
217                 
218                 // The two folling method test RemotingServices.Marshal()
219                 [Test]
220                 public void Marshal1()
221                 {
222                         
223                         MarshalObject objMarshal = NewMarshalObject();
224                         ObjRef objRef = RemotingServices.Marshal(objMarshal);
225                         
226                         Assert.IsTrue(objRef.URI != null, "#A01");
227                         
228                         MarshalObject objRem = (MarshalObject) RemotingServices.Unmarshal(objRef);
229                         Assert.AreEqual(objMarshal.Id, objRem.Id, "#A02");
230                         
231                         objRem.Id = 2;
232                         Assert.AreEqual(objMarshal.Id, objRem.Id, "#A03");
233                         
234                         // TODO: uncomment when RemotingServices.Disconnect is implemented
235                         //RemotingServices.Disconnect(objMarshal);
236                         
237                         objMarshal = NewMarshalObject();
238                         
239                         objRef = RemotingServices.Marshal(objMarshal, objMarshal.Uri);
240                         
241                         Assert.IsTrue(objRef.URI.EndsWith(objMarshal.Uri), "#A04");
242                         // TODO: uncomment when RemotingServices.Disconnect is implemented
243                         //RemotingServices.Disconnect(objMarshal);              
244                 }
245                 
246                 [Test]
247                 public void Marshal2()
248                 {
249                         DerivedMarshalObject derivedObjMarshal = NewDerivedMarshalObject();
250                         
251                         ObjRef objRef = RemotingServices.Marshal(derivedObjMarshal, derivedObjMarshal.Uri, typeof(MarshalObject));
252                         
253                         // Check that the type of the marshaled object is MarshalObject
254                         Assert.IsTrue(objRef.TypeInfo.TypeName.StartsWith((typeof(MarshalObject)).ToString()), "#A05");
255                         
256                         // TODO: uncomment when RemotingServices.Disconnect is implemented
257                         //RemotingServices.Disconnect(derivedObjMarshal);
258                 }
259                 
260                 // Tests RemotingServices.GetObjectUri()
261                 [Test]
262                 public void GetObjectUri()
263                 {
264                         MarshalObject objMarshal = NewMarshalObject();
265                         
266                         Assert.IsTrue(RemotingServices.GetObjectUri(objMarshal) == null, "#A06");
267                         
268                         ObjRef objRef = RemotingServices.Marshal(objMarshal);
269                         
270                         Assert.IsTrue(RemotingServices.GetObjectUri(objMarshal) != null, "#A07");
271                         // TODO: uncomment when RemotingServices.Disconnect is implemented
272                         //RemotingServices.Disconnect(objMarshal);
273                 }
274                 
275                 // Tests RemotingServices.Connect
276                 [Test]
277                 public void Connect()
278                 {
279                         MarshalObject objMarshal = NewMarshalObject();
280                         
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);
286                         
287                         RemotingServices.Marshal(objMarshal,objMarshal.Uri);
288                         
289                         MarshalObject objRem = (MarshalObject) RemotingServices.Connect(typeof(MarshalObject), "tcp://localhost:1236/" + objMarshal.Uri);
290                         
291                         Assert.IsTrue(RemotingServices.IsTransparentProxy(objRem), "#A08");
292                         
293                         ChannelServices.UnregisterChannel(chn);
294                         
295                         // TODO: uncomment when RemotingServices.Disconnect is implemented
296                         //RemotingServices.Disconnect(objMarshal);
297                 }
298                 
299                 // Tests RemotingServices.Marshal()
300                 [Test]
301                 [ExpectedException(typeof(RemotingException))]  
302                 public void MarshalThrowException()
303                 {
304                         MarshalObject objMarshal = NewMarshalObject();
305                         
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);
311                         
312                         RemotingServices.Marshal(objMarshal,objMarshal.Uri);
313                         
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
318                         try
319                         {
320                                 RemotingServices.Marshal(objRem, objMarshal.Uri);
321                         }
322                         catch(Exception e)
323                         {
324                                 ChannelServices.UnregisterChannel(chn);
325                         
326                         // TODO: uncomment when RemotingServices.Disconnect is implemented
327                         //RemotingServices.Disconnect(objMarshal);
328                         
329                                 throw e;
330                         }               
331                 }
332                 
333                 // Tests RemotingServices.ExecuteMessage()
334                 // also tests GetMethodBaseFromMessage()
335                 // IsMethodOverloaded()
336                 [Test]
337                 public void ExecuteMessage()
338                 {
339                         TcpChannel chn = null;
340                         try
341                         {
342                                 chn = new TcpChannel(1235);
343                                 ChannelServices.RegisterChannel(chn);
344                                 
345                                 MarshalObject objMarshal = NewMarshalObject();
346                                 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), objMarshal.Uri, WellKnownObjectMode.SingleCall);
347                                 
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));
350                                 
351                                 MarshalObject objRem = (MarshalObject) proxy.GetTransparentProxy();
352                                 
353                                 objRem.Method1();
354                                 
355                                 // Tests RemotingServices.GetMethodBaseFromMethodMessage()
356                                 AssertEquals("#A09","Method1",proxy.MthBase.Name);
357                                 Assert.IsTrue(!proxy.IsMethodOverloaded, "#A09.1");
358                                 
359                                 objRem.Method2();
360                                 Assert.IsTrue(proxy.IsMethodOverloaded, "#A09.2");
361                         
362                                 // Tests RemotingServices.ExecuteMessage();
363                                 // If ExecuteMessage does it job well, Method1 should be called 2 times
364                                 Assert.AreEqual(2, MarshalObject.Called, "#A10");
365                         }
366                         finally
367                         {
368                                 if(chn != null) ChannelServices.UnregisterChannel(chn);
369                         }
370                 }
371                 
372                 // Tests the IsOneWay method
373                 [Test]
374                 public void IsOneWay()
375                 {
376                         TcpChannel chn = null;
377                         try
378                         {
379                                 chn = new TcpChannel(1238);
380                                 ChannelServices.RegisterChannel(chn);
381                                 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), "MarshalObject.rem", WellKnownObjectMode.Singleton);
382                                 
383                                 MarshalObject objRem = (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1238/MarshalObject.rem");
384                                 
385                                 Assert.IsTrue(RemotingServices.IsTransparentProxy(objRem), "#A10.1");
386                                 
387                                 objRem.Method1();
388                                 Thread.Sleep(20);
389                                 Assert.IsTrue(!MarshalObject.IsMethodOneWay, "#A10.2");
390                                 objRem.Method3();
391                                 Thread.Sleep(20);
392                                 Assert.IsTrue(MarshalObject.IsMethodOneWay, "#A10.2");
393                         }
394                         finally
395                         {
396                                 if(chn != null) ChannelServices.UnregisterChannel(chn);
397                         }
398                 }
399                 
400                 [Test]
401                 public void GetObjRefForProxy()
402                 {
403                         TcpChannel chn = null;
404                         try
405                         {
406                                 chn = new TcpChannel(1239);
407                                 ChannelServices.RegisterChannel(chn);
408                                 
409                                 // Register le factory as a SAO
410                                 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObjectFactory), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap", WellKnownObjectMode.Singleton);
411                                 
412                                 MarshalObjectFactory objFactory = (MarshalObjectFactory) Activator.GetObject(typeof(MarshalObjectFactory), "tcp://localhost:1239/MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap");
413                                 
414                                 // Get a new "CAO"
415                                 MarshalObject objRem = objFactory.GetNewMarshalObject();
416                                 
417                                 ObjRef objRefRem = RemotingServices.GetObjRefForProxy((MarshalByRefObject)objRem);
418                                 
419                                 Assert.IsTrue(objRefRem != null, "#A11");
420                         }
421                         finally
422                         {
423                                 if(chn != null) ChannelServices.UnregisterChannel(chn);                         
424                         }
425                 }
426                 
427                 // Tests GetRealProxy
428                 [Test]
429                 public void GetRealProxy()
430                 {
431                         TcpChannel chn = null;
432                         try
433                         {
434                                 chn = new TcpChannel(1241);
435                                 ChannelServices.RegisterChannel(chn);
436                                 
437                                 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap", WellKnownObjectMode.Singleton);
438                                 
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();
441                                 
442                                 RealProxy rp = RemotingServices.GetRealProxy(objRem);
443                                 
444                                 Assert.IsTrue(rp != null, "#A12");
445                                 Assert.AreEqual("MonoTests.System.Runtime.Remoting.RemotingServicesInternal.MyProxy", rp.GetType().ToString(), "#A13");
446                         }
447                         finally
448                         {
449                                 if(chn != null) ChannelServices.UnregisterChannel(chn);
450                         }
451                 }
452                 
453                 // Tests SetObjectUriForMarshal()
454                 [Test]
455                 public void SetObjectUriForMarshal()
456                 {
457                         TcpChannel chn = null;
458                         try
459                         {
460                                 chn = new TcpChannel(1242);
461                                 ChannelServices.RegisterChannel(chn);
462                                 
463                                 MarshalObject objRem = NewMarshalObject();
464                                 RemotingServices.SetObjectUriForMarshal(objRem, objRem.Uri);
465                                 RemotingServices.Marshal(objRem);
466                                 
467                                 objRem = (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1242/"+objRem.Uri);
468                                 Assert.IsTrue(objRem != null, "#A14");
469                         }
470                         finally
471                         {
472                                 if(chn != null) ChannelServices.UnregisterChannel(chn);
473                         }                       
474                         
475                 }
476                 
477                 // Tests GetServeurTypeForUri()
478                 [Test]
479                 public void GetServeurTypeForUri()
480                 {
481                         TcpChannel chn = null;
482                         Type type = typeof(MarshalObject);
483                         try
484                         {
485                                 chn = new TcpChannel(1243);
486                                 ChannelServices.RegisterChannel(chn);
487                                 
488                                 MarshalObject objRem = NewMarshalObject();
489                                 RemotingServices.SetObjectUriForMarshal(objRem, objRem.Uri);
490                                 RemotingServices.Marshal(objRem);
491                                 
492                                 Type typeRem = RemotingServices.GetServerTypeForUri(RemotingServices.GetObjectUri(objRem));
493                                 Assert.AreEqual(type, typeRem, "#A15");
494                         }
495                         finally
496                         {
497                                 if(chn != null) ChannelServices.UnregisterChannel(chn);
498                         }                       
499                 }
500                 
501                 // Tests IsObjectOutOfDomain
502                 // Tests IsObjectOutOfContext
503                 [Test]
504                 public void IsObjectOutOf()
505                 {
506                         TcpChannel chn = null;
507                         try
508                         {
509                                 chn = new TcpChannel(1245);
510                                 ChannelServices.RegisterChannel(chn);
511                                 
512                                 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), "MarshalObject.rem", WellKnownObjectMode.Singleton);
513                                 
514                                 MarshalObject objRem = (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1245/MarshalObject.rem");
515                                 
516                                 Assert.IsTrue(RemotingServices.IsObjectOutOfAppDomain(objRem), "#A16");
517                                 Assert.IsTrue(RemotingServices.IsObjectOutOfContext(objRem), "#A17");
518                                 
519                                 MarshalObject objMarshal = new MarshalObject();
520                                 Assert.IsTrue(!RemotingServices.IsObjectOutOfAppDomain(objMarshal), "#A18");
521                                 Assert.IsTrue(!RemotingServices.IsObjectOutOfContext(objMarshal), "#A19");
522                         }
523                         finally
524                         {
525                                 ChannelServices.UnregisterChannel(chn);
526                         }
527                 }
528         } // end class RemotingServicesTest
529 } // end of namespace MonoTests.System.Runtime.Remoting.RemotingServicesTest