2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / corlib / Test / System.Runtime.Remoting / RemotingConfigurationTest.cs
1 // created on 05/03/2003 at 13:18
2 // RemotingConfigurationTest.cs unit test class
3 // for System.Runtime.Remoting.RemotingConfiguration
4 //
5 // Author: Jean-Marc ANDRE <jean-marc.andre@polymtl.ca>
6 // 
7
8 using System;
9 #if NET_1_1
10         using System.Net.Sockets;
11 #endif
12 using System.Reflection;
13 using System.Runtime.Remoting;
14 using System.Runtime.Remoting.Channels;
15 using System.Runtime.Remoting.Channels.Tcp;
16 using System.Runtime.Remoting.Channels.Http;
17 using NUnit.Framework;
18
19 // internal namespace with classes I'll use during the unit tests
20 namespace MonoTests.System.Runtime.RemotingConfigurationInternal
21 {
22         public class MarshalObject: MarshalByRefObject
23         {
24                 private bool _method1Called = false;
25                 
26                 public bool Method1Called
27                 {
28                         get{ return _method1Called;}
29                 }
30                 
31                 public void Method1()
32                 {
33                         _method1Called = true;
34                 }
35                 
36         }
37         
38         public class DerivedMarshalObject: MarshalObject
39         {
40                 
41         }
42         
43         public class WellKnownObject: MarshalByRefObject
44         {
45                 private bool _method1Called = false;
46                 
47                 public bool Method1Called
48                 {
49                         get{ return _method1Called;}
50                 }
51                 
52                 public void Method1()
53                 {
54                         _method1Called = true;
55                 }               
56         }
57         
58         public class DerivedWellKnownObject: WellKnownObject
59         {
60                 
61         }
62         
63         public class ActivatedObject: MarshalByRefObject
64         {
65                 
66         }
67         
68         public class DerivedActivatedObject: ActivatedObject
69         {
70                 
71         }
72         
73         public class AppNameTest: MarshalByRefObject
74         {
75                 
76         }
77 }
78
79 namespace MonoTests.System.Runtime.Remoting
80 {
81         using MonoTests.System.Runtime.RemotingConfigurationInternal;
82         
83         // The unit test class
84         [TestFixture]
85         public class RemotingConfigurationTest
86         {
87                 
88                 [Test]
89                 public void ApplicationId()
90                 {
91                         string _Id = RemotingConfiguration.ApplicationId;
92                         
93                         Assert.IsTrue(_Id != null, "#A01");
94                 }
95                 
96                 // tests and set the ApplicationName
97                 [Test]
98                 public void ApplicationName()
99                 {
100                         TcpChannel chn = null;
101                         AppNameTest objAppNameTest = null;
102                         try
103                         {
104                                 chn = new TcpChannel(1234);
105                                 ChannelServices.RegisterChannel(chn);
106                                 
107                                 // the URL of the application's marshaled objects should be 
108                                 // tcp://localhost:1234/RemotingConfigurationTest/<object>
109                                 RemotingConfiguration.ApplicationName = "RemotingConfigurationTest";
110                                 
111                                 objAppNameTest = new AppNameTest();
112                                 RemotingServices.Marshal(objAppNameTest, "AppNameTest.rem");
113                                 
114                                 AppNameTest remAppNameTest = (AppNameTest) Activator.GetObject(typeof(AppNameTest), "tcp://localhost:1234/" + RemotingConfiguration.ApplicationName + "AppNameTest.rem");
115                                 
116                                 Assert.IsTrue(remAppNameTest != null, "#B01");
117                         }
118                         catch(Exception e)
119                         {
120                                 Assert.Fail ("#B02: " + e.Message);
121                         }
122                         finally
123                         {
124                                 RemotingServices.Disconnect(objAppNameTest);
125                                 ChannelServices.UnregisterChannel(chn);
126                         }
127                 }
128                 
129                 // tests related to the SAO
130                 [Test]
131                 public void RegisterWellKnownType()
132                 {
133                         TcpChannel chn = null;
134                         try
135                         {
136                                 chn = new TcpChannel(1234);
137                                 ChannelServices.RegisterChannel(chn);
138                                 
139                                 // register the SAO
140                                 if(RemotingConfiguration.ApplicationName == null) RemotingConfiguration.ApplicationName = "RemotingConfigurationTest";
141                                 RemotingConfiguration.RegisterWellKnownServiceType(typeof(DerivedWellKnownObject), "WellKnownObject.rem", WellKnownObjectMode.Singleton);
142                                 
143                                 // get the registered services
144                                 WellKnownServiceTypeEntry[] ast = RemotingConfiguration.GetRegisteredWellKnownServiceTypes();
145                                 
146                                 bool IsServerRegistered = false;
147                                 foreach(WellKnownServiceTypeEntry aste in ast)
148                                 {
149                                         if(aste.ObjectType == typeof(DerivedWellKnownObject))
150                                         {
151                                                 IsServerRegistered = true;
152                                                 break;
153                                         }
154                                 }
155                                 
156                                 Assert.IsTrue(IsServerRegistered, "#A02");
157                                 
158                                 // register the client
159                                 RemotingConfiguration.RegisterWellKnownClientType(typeof(WellKnownObject), "tcp://localhost:1234/"+RemotingConfiguration.ApplicationName+"/WellKnownObject.rem");
160                                 
161                                 // get the registered client
162                                 WellKnownClientTypeEntry[] act = RemotingConfiguration.GetRegisteredWellKnownClientTypes();
163                                 
164                                 bool IsClientRegistered = false;
165                                 foreach(WellKnownClientTypeEntry acte in act)
166                                 {
167                                         if(acte.ObjectType == typeof(WellKnownObject))
168                                         {
169                                                 IsClientRegistered = true;
170                                                 break;
171                                         }
172                                 }
173                                 
174                                 Assert.IsTrue(IsClientRegistered, "#A03");
175                                 
176                                 WellKnownObject objWellKnown = new WellKnownObject();
177                                 
178                                 
179                                 Assert.IsTrue(objWellKnown != null, "#A04");
180                                 Assert.IsTrue(RemotingServices.IsTransparentProxy(objWellKnown), "#A05");
181                                 objWellKnown.Method1();
182                                 Assert.IsTrue(objWellKnown.Method1Called, "#A06");
183                         }
184                         finally
185                         {
186                                 ChannelServices.UnregisterChannel(chn);
187                         }
188                         
189                 }
190                 
191                 // tests the CAO related methods
192                 [Test]
193 #if NET_1_1
194                 [ExpectedException(typeof(SocketException))]
195 #else
196                 [ExpectedException(typeof(RemotingException))]
197 #endif
198                 public void RegisterActivatedType()
199                 {
200                         TcpChannel chn = null;
201                         try
202                         {
203                                 chn = new TcpChannel(1234);
204                                 ChannelServices.RegisterChannel(chn);
205                                 
206                                 // register the CAO
207                                 RemotingConfiguration.RegisterActivatedServiceType(typeof(ActivatedObject));
208                                 
209                                 // get the registered CAO
210                                 ActivatedServiceTypeEntry[] ast = RemotingConfiguration.GetRegisteredActivatedServiceTypes();
211                                 
212                                 bool IsServerRegistered = false;
213                                 foreach(ActivatedServiceTypeEntry aste in ast)
214                                 {
215                                         if(aste.ObjectType == typeof(ActivatedObject))
216                                         {
217                                                 IsServerRegistered = true;
218                                                 break;
219                                         }
220                                 }
221                                 
222                                 Assert.IsTrue(IsServerRegistered, "#A07");
223                                 
224                                 RemotingConfiguration.RegisterActivatedClientType(typeof(DerivedActivatedObject), "tcp://localhost:1234");
225                                 
226                                 ActivatedClientTypeEntry[] act = RemotingConfiguration.GetRegisteredActivatedClientTypes();
227                                 
228                                 bool IsClientRegistered = false;
229                                 foreach(ActivatedClientTypeEntry acte in act)
230                                 {
231                                         if(acte.ObjectType == typeof(DerivedActivatedObject))
232                                         {
233                                                 IsClientRegistered = true;
234                                                 break;
235                                         }
236                                 }
237                                 
238                                 Assert.IsTrue(IsClientRegistered);                              , "#A08");
239                                 
240                                 // This will send a RemotingException since there is no service named DerivedActivatedObject
241                                 // on the server
242                                 DerivedActivatedObject objDerivedActivated = new DerivedActivatedObject();
243                         }
244                         finally
245                         {
246                                 ChannelServices.UnregisterChannel(chn);
247                         }
248                         
249                 }
250                 
251                 // Get the process ID
252                 [Test]
253                 public void ProcessId()
254                 {
255                         string strProcessId = null;
256                         strProcessId = RemotingConfiguration.ProcessId;
257                         Assert.IsTrue(strProcessId != null, "#AO9");
258                 }
259                 
260                 [Test]
261                 public void IsActivationAllowed()
262                 {
263                         // register the CAO
264                         RemotingConfiguration.RegisterActivatedServiceType(typeof(ActivatedObject));
265                         // ActivatedObject was previously registered as a CAO on the server
266                         // so IsActivationAllowed() should return TRUE
267                         Assert.IsTrue(RemotingConfiguration.IsActivationAllowed(typeof(ActivatedObject)), "#A10");
268                 }
269                 
270                 [Test]
271                 public void IsRemotelyActivatedClientType()
272                 {
273                         Assembly ass = Assembly.GetExecutingAssembly();
274                         AssemblyName assName = ass.GetName();
275                         
276                         ActivatedClientTypeEntry acte = null;
277
278                         RemotingConfiguration.RegisterActivatedClientType(typeof(DerivedActivatedObject), "tcp://localhost:1234");
279                                 
280                         // DerivedActivatedObject was registered as a CAO on the client
281                         acte = RemotingConfiguration.IsRemotelyActivatedClientType(typeof(DerivedActivatedObject));
282                         
283                         Assert.IsTrue(acte != null, "#A11");
284                         Assert.AreEqual(typeof(DerivedActivatedObject), acte.ObjectType, "#A12");
285                         
286                         acte = RemotingConfiguration.IsRemotelyActivatedClientType(typeof(DerivedActivatedObject).ToString(), assName.Name);
287                         Assert.IsTrue(acte != null, "#A13");
288                         Assert.AreEqual(typeof(DerivedActivatedObject), acte.ObjectType, "#A14");
289                 }
290                 
291                 [Test]
292                 public void IsWellKnownClientType()
293                 {
294                         Assembly ass = Assembly.GetExecutingAssembly();
295                         AssemblyName assName = ass.GetName();
296                         
297                         WellKnownClientTypeEntry acte = null;
298                         // WellKnownObject was registered as a SAO on th client
299                         acte = RemotingConfiguration.IsWellKnownClientType(typeof(WellKnownObject));
300                         
301                         Assert.IsTrue(acte != null, "#A11");
302                         Assert.AreEqual(typeof(WellKnownObject), acte.ObjectType, "#A12");
303                         
304                         acte = RemotingConfiguration.IsWellKnownClientType(typeof(WellKnownObject).ToString(), assName.Name);
305                         Assert.IsTrue(acte != null, "#A13");
306                         Assert.AreEqual(typeof(WellKnownObject), acte.ObjectType, "#A14");
307                         
308                 }
309         }
310 }