Merge pull request #5428 from kumpera/wasm-support-p2
[mono.git] / mcs / class / System.Runtime.Remoting / Test / HttpBugTests.cs
1
2 using System;
3 using System.Xml;
4 using System.Collections;
5 using System.Runtime.Remoting;
6 using System.Runtime.Remoting.Channels;
7 using System.Runtime.Remoting.Channels.Http;
8 using NUnit.Framework;
9
10 using MonoTests.Helpers;
11
12 namespace MonoTests.Remoting.Http
13 {
14         //Test for Bug 324362 - SoapFormatter cannot deserialize the same MBR twice
15         [TestFixture]
16         public class Bug324362
17         {
18                 [Test]
19 //              [Category ("NotWorking")] // the assertion fails, and if it's removed, there's an exception
20                 [Ignore ("This test somehow keeps http channel registered and then blocks any further http tests working. This also happens under .NET, so this test itself is wrong with nunit 2.4.8.")]
21                 public void Test ()
22                 {
23                         new HttpChannel (8086);
24                         RemotingServices.Marshal (new Service (), "test");
25
26                         Service remObj = (Service) RemotingServices.Connect (
27                                 typeof (Service), "http://localhost:8086/test");
28
29                         ArrayList list;
30                         remObj.Test (out list);
31                         // it's of type 'ObjRef' instead of 'Service':
32                         Assert.IsTrue (list [0] is Service);
33
34                         Service [] array;
35                         remObj.Test (out array);
36                 }
37         
38                 public class Service : MarshalByRefObject
39                 {
40                         public Service Test (out Service[] a)
41                         {
42                                 Service obj = new Service ();
43                                 a = new Service [] { obj };
44                                 return obj;
45                                 // return null or return otherObj works
46                         }
47                 
48                         public Service Test (out ArrayList a)
49                         {
50                                 a = new ArrayList ();
51                                 Service obj = new Service ();
52                                 a.Add (obj);
53                                 return obj; 
54                                 // return null or return otherObj works
55                         }
56                 }
57         }
58         
59         //Bug 321420 - SoapReader fails to deserialize some method calls
60         [TestFixture]
61         public class Bug321420 : MarshalByRefObject
62         {
63                 HttpChannel channel;
64                 
65                 public void Method (string p1, string p2)
66                 {
67                 }
68                 
69                 [Test]
70                 public void Main ()
71                 {
72                         var port = NetworkHelpers.FindFreePort ();
73                         channel = new HttpChannel (port);
74                         ChannelServices.RegisterChannel (channel);
75                         RemotingConfiguration.RegisterWellKnownServiceType
76                                 (typeof (Bug321420),"Server.soap", WellKnownObjectMode.Singleton);
77                         
78                         Bug321420 s = (Bug321420) Activator.GetObject (typeof
79                                 (Bug321420), $"http://localhost:{port}/Server.soap");
80                         
81                         // this works: s.Method ("a", "b");
82                         s.Method ("a", "a");
83                 }
84                 
85                 [TestFixtureTearDown]
86                 public void Stop ()
87                 {
88                         if (channel != null)
89                                 ChannelServices.UnregisterChannel (channel);
90                 }
91         }
92         
93         //Bug 315570 - Remoting over HTTP fails when returning a null reference.
94         [TestFixture]   
95         public class Bug315570
96         {
97                 Server server;
98                 
99                 [Test]
100                 [Ignore ("This test somehow keeps http channel registered and then blocks any further http tests working. This also happens under .NET, so this test itself is wrong with nunit 2.4.8.")]
101                 public void Main ()
102                 {
103                         Foo foo = (Foo) Activator.GetObject (typeof (Foo),
104                                 $"http://localhost:{server.HttpPort}/Test");
105
106                         Bar bar = foo.Login ();
107                         if (bar != null)
108                                 bar.Foobar ();
109                 }
110                 
111                 [TestFixtureSetUp]
112                 public void Start ()
113                 {
114                         AppDomain domain = BaseCallTest.CreateDomain ("testdomain");
115                         server = (Server) domain.CreateInstanceAndUnwrap
116                                 (typeof (Server).Assembly.FullName, typeof (Server).FullName);
117                         server.Start ();
118                 }
119
120                 [TestFixtureTearDown]
121                 public void Stop ()
122                 {
123                         server.Stop ();
124                 }
125                 
126                 public class Foo: MarshalByRefObject
127                 {
128                         public Bar Login ()
129                         {
130                                 return null;
131                         }
132                 }
133                 
134                 public class Bar: MarshalByRefObject
135                 {
136                         public void Foobar ()
137                         {
138                         //      Console.WriteLine("Bar::foo()");
139                         }
140                 }
141                 
142                 public class Server : MarshalByRefObject
143                 {
144                         HttpChannel c;
145                         
146                         public void Start ()
147                         {
148                                 HttpPort = NetworkHelpers.FindFreePort ();
149                                 c = new HttpChannel (HttpPort);
150                                 ChannelServices.RegisterChannel (c);
151                                 
152                                 Type t = typeof(Foo);
153                                 RemotingConfiguration.RegisterWellKnownServiceType (t, "Test",
154                                         WellKnownObjectMode.SingleCall);
155                         }
156                         
157                         public void Stop ()
158                         {
159                                 c.StopListening (null);
160                                 ChannelServices.UnregisterChannel (c);
161                         }
162
163                         public int HttpPort { get; private set; }
164                 }
165         }
166         
167         //Bug 315170 - exception thrown in remoting if interface parameter names differ from the impelmentation method parameter names
168         [TestFixture]
169         public class Bug315170
170         {
171                 Server server;
172                 HttpChannel channel;
173                 
174                 [Test]
175                 public void Main ()
176                 {
177                         channel = new HttpChannel (0);
178                         ChannelServices.RegisterChannel (channel);
179                         MarshalByRefObject obj = (MarshalByRefObject) RemotingServices.Connect (
180                                 typeof (IFactorial),
181                                 $"http://localhost:{server.HttpPort}/MyEndPoint");
182                         IFactorial cal = (IFactorial) obj;
183                         Assert.AreEqual (cal.CalculateFactorial (4), 24);
184                 }
185                 
186                 [TestFixtureSetUp]
187                 public void Start ()
188                 {
189                         AppDomain domain = BaseCallTest.CreateDomain ("testdomain");
190                         server = (Server) domain.CreateInstanceAndUnwrap
191                                 (typeof (Server).Assembly.FullName, typeof (Server).FullName);
192                         server.Start ();
193                 }
194
195                 [TestFixtureTearDown]
196                 public void Stop ()
197                 {
198                         server.Stop ();
199                         if (channel != null)
200                                 ChannelServices.UnregisterChannel (channel);
201                 }
202                 
203                 public interface IFactorial
204                 {
205                         // With this line everything works
206                         //ulong CalculateFactorial(uint a);
207                         // With this line it doesn't
208                         ulong CalculateFactorial(uint b);
209                 }
210
211                 
212                 public class Server : MarshalByRefObject
213                 {
214                         HttpChannel c;
215                         
216                         public void Start ()
217                         {
218                                 HttpPort = NetworkHelpers.FindFreePort ();
219                                 c = new HttpChannel (HttpPort);
220                                 ChannelServices.RegisterChannel (c);
221                                 
222                                 Type t = typeof(Calculator);
223                                 RemotingConfiguration.RegisterWellKnownServiceType (t, "MyEndPoint",
224                                         WellKnownObjectMode.Singleton);
225                         }
226                         
227                         public void Stop ()
228                         {
229                                 c.StopListening (null);
230                                 ChannelServices.UnregisterChannel (c);
231                         }
232
233                         public int HttpPort { get; private set; }
234                 }
235                 
236                 public class Calculator : MarshalByRefObject, IFactorial
237                 {
238                         public ulong CalculateFactorial (uint a)
239                         {
240                                 ulong res = 1;
241                                 for (uint i=1 ; i<=a; i++)
242                                         res = res * i;
243                                 return res;
244                         }
245                 }
246         }
247 }
248
249