Add this for backwards compatibility
[mono.git] / mcs / class / System.Runtime.Remoting / Test / BaseCalls.cs
1 //
2 // MonoTests.Remoting.BaseCalls.cs
3 //
4 // Author: Lluis Sanchez Gual (lluis@ximian.com)
5 //
6 // 2003 (C) Copyright, Ximian, Inc.
7 //
8
9 using System;
10 using System.Net;
11 using System.Text;
12 using System.Threading;
13 using System.Collections;
14 using System.Globalization;
15 using System.Runtime.Remoting;
16 using System.Runtime.Remoting.Channels;
17 using System.Runtime.Remoting.Messaging;
18 using System.Runtime.Remoting.Lifetime;
19 using System.Runtime.Remoting.Channels.Tcp;
20 using System.Runtime.Remoting.Activation;
21 using System.Runtime.Remoting.Contexts;
22 using System.Runtime.InteropServices;
23 using NUnit.Framework;
24
25 namespace MonoTests.Remoting
26 {
27         public abstract class BaseCallTest
28         {
29                 IChannelSender chs;
30                 string[] remoteUris;
31                 CallsDomainServer server;
32                 int remoteDomId;
33
34                 [TestFixtureSetUp]
35                 public void Run()
36                 {
37                         remoteDomId = CreateServer ();
38                 }
39
40                 [TestFixtureTearDown]
41                 public void End ()
42                 {
43                         ShutdownServer ();
44                 }
45
46                 protected virtual int CreateServer ()
47                 {
48                         ChannelManager cm = CreateChannelManager ();
49                         chs = cm.CreateClientChannel ();
50                         ChannelServices.RegisterChannel (chs);
51
52                         AppDomain domain = AppDomain.CreateDomain ("testdomain");
53                         server = (CallsDomainServer) domain.CreateInstanceAndUnwrap(GetType().Assembly.FullName,"MonoTests.Remoting.CallsDomainServer");
54                         remoteUris = server.Start (cm);
55                         return server.GetDomId ();
56                 }
57                 
58                 protected virtual void ShutdownServer ()
59                 {
60                         if (server != null) {
61                                 server.Stop ();
62                                 if (chs != null)
63                                         ChannelServices.UnregisterChannel (chs);
64                         }
65                 }
66
67                 protected virtual RemoteObject CreateRemoteInstance ()
68                 {
69                         return (RemoteObject) Activator.GetObject (typeof(RemoteObject), remoteUris[0]);
70                 }
71
72                 protected virtual AbstractRemoteObject CreateRemoteAbstract ()
73                 {
74                         return (AbstractRemoteObject) Activator.GetObject (typeof(AbstractRemoteObject), remoteUris[1]);
75                 }
76
77                 protected virtual IRemoteObject CreateRemoteInterface ()
78                 {
79                         return (IRemoteObject) Activator.GetObject (typeof(IRemoteObject), remoteUris[2]);
80                 }
81
82                 public InstanceSurrogate InternalGetInstanceSurrogate ()
83                 {
84                         InstanceSurrogate s = GetInstanceSurrogate ();
85                         s.RemoteObject = CreateRemoteInstance ();
86                         return s;
87                 }
88                 
89                 public AbstractSurrogate InternalGetAbstractSurrogate ()
90                 {
91                         AbstractSurrogate s = GetAbstractSurrogate ();
92                         s.RemoteObject = CreateRemoteAbstract ();
93                         return s;
94                 }
95                 
96                 public InterfaceSurrogate InternalGetInterfaceSurrogate ()
97                 {
98                         InterfaceSurrogate s = GetInterfaceSurrogate ();
99                         s.RemoteObject = CreateRemoteInterface ();
100                         return s;
101                 }
102
103                 public abstract InstanceSurrogate GetInstanceSurrogate ();
104                 public abstract AbstractSurrogate GetAbstractSurrogate ();
105                 public abstract InterfaceSurrogate GetInterfaceSurrogate ();
106                 
107                 public virtual ChannelManager CreateChannelManager ()
108                 {
109                         return null;
110                 }
111
112                 //
113                 // The tests
114                 //
115
116                 [Test]
117                 public void TestInstanceSimple ()
118                 {
119                         RunTestSimple (InternalGetInstanceSurrogate());
120                 }
121
122                 [Test]
123                 public void TestAbstractSimple ()
124                 {
125                         RunTestSimple (InternalGetAbstractSurrogate());
126                 }
127
128                 [Test]
129                 public void TestInterfaceSimple ()
130                 {
131                         RunTestSimple (InternalGetInterfaceSurrogate());
132                 }
133
134                 [Test]
135                 public void TestInstancePrimitiveParams ()
136                 {
137                         RunTestPrimitiveParams (InternalGetInstanceSurrogate());
138                 }
139
140                 [Test]
141                 public void TestAbstractPrimitiveParams ()
142                 {
143                         RunTestPrimitiveParams (InternalGetAbstractSurrogate());
144                 }
145
146                 [Test]
147                 public void TestInterfacePrimitiveParams ()
148                 {
149                         RunTestPrimitiveParams (InternalGetInterfaceSurrogate());
150                 }
151
152                 [Test]
153                 public void TestInstancePrimitiveParamsInOut ()
154                 {
155                         RunTestPrimitiveParamsInOut (InternalGetInstanceSurrogate());
156                 }
157
158                 [Test]
159                 public void TestAbstractPrimitiveParamsInOut ()
160                 {
161                         RunTestPrimitiveParamsInOut (InternalGetAbstractSurrogate());
162                 }
163
164                 [Test]
165                 public void TestInterfacePrimitiveParamsInOut ()
166                 {
167                         RunTestPrimitiveParamsInOut (InternalGetInterfaceSurrogate());
168                 }
169
170                 [Test]
171                 public void TestInstanceComplexParams ()
172                 {
173                         RunTestComplexParams (InternalGetInstanceSurrogate());
174                 }
175
176                 [Test]
177                 public void TestAbstractComplexParams ()
178                 {
179                         RunTestComplexParams (InternalGetAbstractSurrogate());
180                 }
181
182                 [Test]
183                 public void TestInterfaceComplexParams ()
184                 {
185                         RunTestComplexParams (InternalGetInterfaceSurrogate());
186                 }
187
188                 [Test]
189                 public void TestInstanceComplexParamsInOut ()
190                 {
191                         RunTestComplexParamsInOut (InternalGetInstanceSurrogate());
192                 }
193
194                 [Test]
195                 public void TestAbstractComplexParamsInOut ()
196                 {
197                         RunTestComplexParamsInOut (InternalGetAbstractSurrogate());
198                 }
199
200                 [Test]
201                 public void TestInterfaceComplexParamsInOut ()
202                 {
203                         RunTestComplexParamsInOut (InternalGetInterfaceSurrogate());
204                 }
205
206                 [Test]
207                 public void TestInstanceProcessContextData ()
208                 {
209                         RunTestProcessContextData (InternalGetInstanceSurrogate());
210                 }
211
212                 [Test]
213                 public void TestAbstractProcessContextData ()
214                 {
215                         RunTestProcessContextData (InternalGetAbstractSurrogate());
216                 }
217
218                 [Test]
219                 public void TestInterfaceProcessContextData ()
220                 {
221                         RunTestProcessContextData (InternalGetInterfaceSurrogate());
222                 }
223
224                 //
225                 // The tests runners
226                 //
227
228                 public void RunTestSimple (IRemoteObject testerSurrogate)
229                 {
230                         Assert.AreEqual (130772 + remoteDomId, testerSurrogate.Simple (), "ReturnValue");
231                 }
232
233                 public void RunTestPrimitiveParams (IRemoteObject testerSurrogate)
234                 {
235                         Assert.AreEqual ("11-22-L-SG@"+remoteDomId, testerSurrogate.PrimitiveParams (11, 22, 'L', "SG"), "ReturnValue");
236                 }
237
238                 public void RunTestPrimitiveParamsInOut (IRemoteObject testerSurrogate)
239                 {
240                         int a2, a1 = 9876543;
241                         float b2, b1 = 82437.83f;
242                         char c2, c1 = 's';
243                         string d2, d1 = "asdASDzxcZXC";
244
245                         string res = testerSurrogate.PrimitiveParamsInOut (ref a1, out a2, ref b1, out b2, 9821, ref c1, out c2, ref d1, out d2);
246
247                         Assert.AreEqual ("9876543-82437.83-s-asdASDzxcZXC@" + remoteDomId, res, "ReturnValue");
248
249                         Assert.AreEqual (12345678, a2, "a2");
250                         Assert.AreEqual (53455.345f, b2, "b2");
251                         Assert.AreEqual ('g', c2, "c2");
252                         Assert.AreEqual ("sfARREG$5345DGDfgY7656gDFG>><<dasdasd", d2, "d2");
253
254                         Assert.AreEqual (65748392, a1, "a1");
255                         Assert.AreEqual (98395.654f, b1, "b1");
256                         Assert.AreEqual ('l', c1, "c1");
257                         Assert.AreEqual ("aasbasbdyhasbduybo234243", d1, "d1");
258                 }
259
260                 public void RunTestComplexParams (IRemoteObject testerSurrogate)
261                 {
262                         ArrayList list = new ArrayList ();
263                         list.Add (new Complex (11,"first"));
264                         Complex c = new Complex (22,"second");
265
266                         Complex r = testerSurrogate.ComplexParams (list, c, "third");
267
268                         Assert.IsNotNull (r, "ReturnValue is null");
269                         Assert.IsNotNull (r.Child, "ReturnValue.Child is null");
270                         Assert.IsNotNull (r.Child.Child, "ReturnValue.Child.Child is null");
271                         
272                         Assert.AreEqual (33, r.Id, "ReturnValue.Id");
273                         Assert.AreEqual ("third@"+remoteDomId, r.Name, "ReturnValue.Name");
274                         Assert.AreEqual (22, r.Child.Id, "ReturnValue.Child.Id");
275                         Assert.AreEqual ("second", r.Child.Name, "ReturnValue.Child.Name");
276                         Assert.AreEqual (11, r.Child.Child.Id, "ReturnValue.Child.Child.Id");
277                         Assert.AreEqual ("first", r.Child.Child.Name, "ReturnValue.Child.Child.Name");
278                 }
279
280                 public void RunTestComplexParamsInOut (IRemoteObject testerSurrogate)
281                 {
282                         ArrayList list = new ArrayList ();
283                         list.Add (new Complex (11,"first"));
284                         list.Add (new Complex (22,"second"));
285                         
286                         byte[] bytes = new byte [100];
287                         for (byte n=0; n<100; n++) bytes[n] = n;
288                         StringBuilder sb = new StringBuilder ("hello from client");
289
290                         Complex c;
291                         Complex r = testerSurrogate.ComplexParamsInOut (ref list, out c, bytes, sb, "third");
292
293                         Assert.IsNotNull (r, "ReturnValue is null");
294                         Assert.IsNotNull (c, "c is null");
295                         Assert.IsNotNull (list, "list is null");
296                         Assert.IsTrue (list.Count == 3, "Invalid list count");
297                         Assert.IsNotNull (list[0], "list[0] is null");
298                         Assert.IsNotNull (list[1], "list[1] is null");
299                         Assert.IsNotNull (list[2], "list[2] is null");
300                         Assert.IsNotNull (bytes, "bytes is null");
301                         Assert.IsNotNull (sb, "sb is null");
302                         
303                         Assert.AreEqual (33, r.Id, "ReturnValue.Id");
304                         Assert.AreEqual ("third@"+remoteDomId, r.Name, "ReturnValue.Name");
305                         Assert.AreEqual (33, c.Id, "c.Id");
306                         Assert.AreEqual ("third@"+remoteDomId, c.Name, "c.Name");
307
308                         Assert.AreEqual (33, ((Complex)list[2]).Id, "list[2].Id");
309                         Assert.AreEqual ("third@"+remoteDomId, ((Complex)list[2]).Name, "list[2].Name");
310                         Assert.AreEqual (22, ((Complex)list[1]).Id, "list[1].Id");
311                         Assert.AreEqual ("second", ((Complex)list[1]).Name, "list[1].Name");
312                         Assert.AreEqual (11, ((Complex)list[0]).Id, "list[0].Id");
313                         Assert.AreEqual ("first", ((Complex)list[0]).Name, "list[0].Name");
314                         
315                         Assert.AreEqual ("hello from client and from server", sb.ToString (), "sb");
316                         for (int n=0; n<100; n++) 
317                                 Assert.AreEqual (n+1, bytes[n], "bytes["+n+"]");
318                 }
319                 
320                 public void RunTestProcessContextData (IRemoteObject testerSurrogate)
321                 {
322                         CallContext.FreeNamedDataSlot ("clientData");
323                         CallContext.FreeNamedDataSlot ("serverData");
324                         CallContext.FreeNamedDataSlot ("mustNotPass");
325
326                         ContextData cdata = new ContextData ();
327                         cdata.data = "hi from client";
328                         cdata.id = 1123;
329                         CallContext.SetData ("clientData", cdata);
330                         CallContext.SetData ("mustNotPass", "more data");
331                         
332                         testerSurrogate.ProcessContextData ();
333                         
334                         cdata = CallContext.GetData ("clientData") as ContextData;
335                         Assert.IsNotNull (cdata, "clientData is null");
336                         Assert.AreEqual ("hi from client", cdata.data, "clientData.data");
337                         Assert.AreEqual (1123, cdata.id, "clientData.id");
338                         
339                         cdata = CallContext.GetData ("serverData") as ContextData;
340                         Assert.IsNotNull (cdata, "serverData is null");
341                         Assert.AreEqual ("hi from server", cdata.data, "serverData.data");
342                         Assert.AreEqual (3211, cdata.id, "serverData.id");
343                         
344                         string mdata = CallContext.GetData ("mustNotPass") as string;
345                         Assert.IsNotNull (mdata, "mustNotPass is null");
346                         Assert.AreEqual ("more data", mdata, "mustNotPass");
347                 }
348         }
349
350         //
351         // The server running in the remote domain
352         //
353
354         class CallsDomainServer: MarshalByRefObject
355         {
356                 IChannelReceiver ch;
357
358                 public string[] Start(ChannelManager cm)
359                 {
360                         try
361                         {
362                                 ch = cm.CreateServerChannel ();
363                                 ChannelServices.RegisterChannel ((IChannel)ch);
364                                 RemotingConfiguration.RegisterWellKnownServiceType (typeof (RemoteObject), "test1", WellKnownObjectMode.SingleCall);
365                                 RemotingConfiguration.RegisterWellKnownServiceType (typeof (RemoteObject), "test2", WellKnownObjectMode.SingleCall);
366                                 RemotingConfiguration.RegisterWellKnownServiceType (typeof (RemoteObject), "test3", WellKnownObjectMode.SingleCall);
367                                 string[] uris = new string[3];
368                                 uris[0] = ch.GetUrlsForUri ("test1")[0];
369                                 uris[1] = ch.GetUrlsForUri ("test2")[0];
370                                 uris[2] = ch.GetUrlsForUri ("test3")[0];
371                                 return uris;
372                         }
373                         catch (Exception ex)
374                         {
375                                 Console.WriteLine (ex.ToString());
376                                 throw;
377                         }
378                 }
379
380                 public void Stop ()
381                 {
382                         if (ch != null)
383                                 ChannelServices.UnregisterChannel (ch);
384                 }
385
386                 public int GetDomId ()
387                 {
388                         return Thread.GetDomainID();
389                 }
390         }
391         
392         [Serializable]
393         public class ContextData : ILogicalThreadAffinative
394         {
395                 public string data;
396                 public int id;
397         }
398
399         [Serializable]
400         public abstract class ChannelManager
401         {
402                 public abstract IChannelSender CreateClientChannel ();
403                 public abstract IChannelReceiver CreateServerChannel ();
404         }
405
406
407         //
408         // Test interface
409         //
410         public interface IRemoteObject
411         {
412                 int Simple ();
413                 string PrimitiveParams (int a, uint b, char c, string d);
414                 string PrimitiveParamsInOut (ref int a1, out int a2, ref float b1, out float b2, int filler, ref char c1, out char c2, ref string d1, out string d2);
415                 Complex ComplexParams (ArrayList a, Complex b, string c);
416                 Complex ComplexParamsInOut (ref ArrayList a, out Complex b, [In,Out] byte[] bytes, [In,Out] StringBuilder sb, string c);
417                 void ProcessContextData ();
418         }
419
420         // Base classes for tester surrogates
421         
422         public abstract class InstanceSurrogate : IRemoteObject
423         {
424                 public RemoteObject RemoteObject;
425                 public abstract int Simple ();
426                 public abstract string PrimitiveParams (int a, uint b, char c, string d);
427                 public abstract string PrimitiveParamsInOut (ref int a1, out int a2, ref float b1, out float b2, int filler, ref char c1, out char c2, ref string d1, out string d2);
428                 public abstract Complex ComplexParams (ArrayList a, Complex b, string c);
429                 public abstract Complex ComplexParamsInOut (ref ArrayList a, out Complex b, [In,Out] byte[] bytes, [In,Out] StringBuilder sb, string c);
430                 public abstract void ProcessContextData ();
431         }
432         
433         public abstract class AbstractSurrogate : IRemoteObject
434         {
435                 public AbstractRemoteObject RemoteObject;
436                 public abstract int Simple ();
437                 public abstract string PrimitiveParams (int a, uint b, char c, string d);
438                 public abstract string PrimitiveParamsInOut (ref int a1, out int a2, ref float b1, out float b2, int filler, ref char c1, out char c2, ref string d1, out string d2);
439                 public abstract Complex ComplexParams (ArrayList a, Complex b, string c);
440                 public abstract Complex ComplexParamsInOut (ref ArrayList a, out Complex b, [In,Out] byte[] bytes, [In,Out] StringBuilder sb, string c);
441                 public abstract void ProcessContextData ();
442         }
443
444         public abstract class InterfaceSurrogate : IRemoteObject
445         {
446                 public IRemoteObject RemoteObject;
447                 public abstract int Simple ();
448                 public abstract string PrimitiveParams (int a, uint b, char c, string d);
449                 public abstract string PrimitiveParamsInOut (ref int a1, out int a2, ref float b1, out float b2, int filler, ref char c1, out char c2, ref string d1, out string d2);
450                 public abstract Complex ComplexParams (ArrayList a, Complex b, string c);
451                 public abstract Complex ComplexParamsInOut (ref ArrayList a, out Complex b, [In,Out] byte[] bytes, [In,Out] StringBuilder sb, string c);
452                 public abstract void ProcessContextData ();
453         }
454
455         
456         //
457         // Test abstract base class
458         //
459
460         public abstract class AbstractRemoteObject : MarshalByRefObject
461         {
462                 public abstract int Simple ();
463                 public abstract string PrimitiveParams (int a, uint b, char c, string d);
464                 public abstract string PrimitiveParamsInOut (ref int a1, out int a2, ref float b1, out float b2, int filler, ref char c1, out char c2, ref string d1, out string d2);
465                 public abstract Complex ComplexParams (ArrayList a, Complex b, string c);
466                 public abstract Complex ComplexParamsInOut (ref ArrayList a, out Complex b, [In,Out] byte[] bytes, [In,Out] StringBuilder sb, string c);
467                 public abstract void ProcessContextData ();
468         }
469
470         //
471         // Test class
472         //
473         
474         public class RemoteObject : AbstractRemoteObject, IRemoteObject
475         {
476                 public override int Simple ()
477                 {
478                         return 130772 + Thread.GetDomainID();
479                 }
480
481                 public override string PrimitiveParams (int a, uint b, char c, string d)
482                 {
483                         return "" + a + "-" + b + "-" + c + "-" + d + "@" + Thread.GetDomainID();
484                 }
485
486                 public override string PrimitiveParamsInOut (ref int a1, out int a2, ref float b1, out float b2, int filler, ref char c1, out char c2, ref string d1, out string d2)
487                 {
488                         string res = "" + a1 + "-" + b1.ToString(CultureInfo.InvariantCulture) + "-" + c1 + "-" + d1 + "@" + Thread.GetDomainID();
489                         a2 = 12345678;
490                         b2 = 53455.345f;
491                         c2 = 'g';
492                         d2 = "sfARREG$5345DGDfgY7656gDFG>><<dasdasd";
493                         a1 = 65748392;
494                         b1 = 98395.654f;
495                         c1 = 'l';
496                         d1 = "aasbasbdyhasbduybo234243";
497                         return res;
498                 }
499
500                 public override Complex ComplexParams (ArrayList a, Complex b, string c)
501                 {
502                         Complex cp = new Complex (33,c+ "@" + Thread.GetDomainID());
503                         cp.Child = b;
504                         cp.Child.Child = (Complex)a[0];
505                         return cp;
506                 }
507
508                 public override Complex ComplexParamsInOut (ref ArrayList a, out Complex b, [In,Out] byte[] bytes, [In,Out] StringBuilder sb, string c)
509                 {
510                         b = new Complex (33,c+ "@" + Thread.GetDomainID());
511                         a.Add (b);
512                         for (byte n=0; n<100; n++) bytes[n] = (byte)(bytes[n] + 1);
513                         sb.Append (" and from server");
514                         return b;
515                 }
516
517                 public override void ProcessContextData ()
518                 {
519                         ContextData cdata = CallContext.GetData ("clientData") as ContextData;
520                         if (cdata == null) 
521                                 throw new Exception ("server: clientData is null");
522                         if (cdata.data != "hi from client" || cdata.id != 1123)
523                                 throw new Exception ("server: clientData is not valid");
524                         
525                         cdata = new ContextData ();
526                         cdata.data = "hi from server";
527                         cdata.id = 3211;
528                         CallContext.SetData ("serverData", cdata);
529                         
530                         string mdata = CallContext.GetData ("mustNotPass") as string;
531                         if (mdata != null) throw new Exception ("mustNotPass is not null");
532                 }
533         }
534
535         [Serializable]
536         public class Complex
537         {
538                 public Complex (int id, string name)
539                 {
540                         Id = id;
541                         Name = name;
542                 }
543
544                 public string Name;
545                 public int Id;
546                 public Complex Child;
547         }
548 }