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