Remove DNS lookups of the local hostname in tests (#18059)
[mono-project.git] / mcs / class / System.Runtime.Remoting / Test / HttpBugTests.cs
blobb80d79fd12f120f1241f26d96595f145f5d5da0c
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;
10 using MonoTests.Helpers;
12 namespace MonoTests.Remoting.Http
14 //Test for Bug 324362 - SoapFormatter cannot deserialize the same MBR twice
15 [TestFixture]
16 public class Bug324362
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 ()
23 var port = NetworkHelpers.FindFreePort ();
24 Hashtable props = new Hashtable ();
25 props["port"] = port;
26 props["bindTo"] = "127.0.0.1";
27 new HttpChannel (props, null, null);
28 RemotingServices.Marshal (new Service (), "test");
30 Service remObj = (Service) RemotingServices.Connect (
31 typeof (Service), $"http://127.0.0.1:{port}/test");
33 ArrayList list;
34 remObj.Test (out list);
35 // it's of type 'ObjRef' instead of 'Service':
36 Assert.IsTrue (list [0] is Service);
38 Service [] array;
39 remObj.Test (out array);
42 public class Service : MarshalByRefObject
44 public Service Test (out Service[] a)
46 Service obj = new Service ();
47 a = new Service [] { obj };
48 return obj;
49 // return null or return otherObj works
52 public Service Test (out ArrayList a)
54 a = new ArrayList ();
55 Service obj = new Service ();
56 a.Add (obj);
57 return obj;
58 // return null or return otherObj works
63 //Bug 321420 - SoapReader fails to deserialize some method calls
64 [TestFixture]
65 public class Bug321420 : MarshalByRefObject
67 HttpChannel channel;
69 public void Method (string p1, string p2)
73 [Test]
74 public void Main ()
76 var port = NetworkHelpers.FindFreePort ();
77 Hashtable props = new Hashtable ();
78 props["port"] = port;
79 props["bindTo"] = "127.0.0.1";
80 channel = new HttpChannel (props, null, null);
81 ChannelServices.RegisterChannel (channel);
82 RemotingConfiguration.RegisterWellKnownServiceType
83 (typeof (Bug321420),"Server.soap", WellKnownObjectMode.Singleton);
85 Bug321420 s = (Bug321420) Activator.GetObject (typeof
86 (Bug321420), $"http://127.0.0.1:{port}/Server.soap");
88 // this works: s.Method ("a", "b");
89 s.Method ("a", "a");
92 [TestFixtureTearDown]
93 public void Stop ()
95 if (channel != null)
96 ChannelServices.UnregisterChannel (channel);
100 //Bug 315570 - Remoting over HTTP fails when returning a null reference.
101 [TestFixture]
102 public class Bug315570
104 Server server;
106 [Test]
107 [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.")]
108 public void Main ()
110 Foo foo = (Foo) Activator.GetObject (typeof (Foo),
111 $"http://127.0.0.1:{server.HttpPort}/Test");
113 Bar bar = foo.Login ();
114 if (bar != null)
115 bar.Foobar ();
118 [TestFixtureSetUp]
119 public void Start ()
121 AppDomain domain = BaseCallTest.CreateDomain ("testdomain");
122 server = (Server) domain.CreateInstanceAndUnwrap
123 (typeof (Server).Assembly.FullName, typeof (Server).FullName);
124 server.Start ();
127 [TestFixtureTearDown]
128 public void Stop ()
130 server.Stop ();
133 public class Foo: MarshalByRefObject
135 public Bar Login ()
137 return null;
141 public class Bar: MarshalByRefObject
143 public void Foobar ()
145 // Console.WriteLine("Bar::foo()");
149 public class Server : MarshalByRefObject
151 HttpChannel c;
153 public void Start ()
155 HttpPort = NetworkHelpers.FindFreePort ();
156 Hashtable props = new Hashtable ();
157 props["port"] = HttpPort;
158 props["bindTo"] = "127.0.0.1";
159 c = new HttpChannel (props, null, null);
160 ChannelServices.RegisterChannel (c);
162 Type t = typeof(Foo);
163 RemotingConfiguration.RegisterWellKnownServiceType (t, "Test",
164 WellKnownObjectMode.SingleCall);
167 public void Stop ()
169 c.StopListening (null);
170 ChannelServices.UnregisterChannel (c);
173 public int HttpPort { get; private set; }
177 //Bug 315170 - exception thrown in remoting if interface parameter names differ from the impelmentation method parameter names
178 [TestFixture]
179 public class Bug315170
181 Server server;
182 HttpChannel channel;
184 [Test]
185 public void Main ()
187 Hashtable props = new Hashtable ();
188 props["port"] = 0;
189 props["bindTo"] = "127.0.0.1";
190 channel = new HttpChannel (props, null, null);
191 ChannelServices.RegisterChannel (channel);
192 MarshalByRefObject obj = (MarshalByRefObject) RemotingServices.Connect (
193 typeof (IFactorial),
194 $"http://127.0.0.1:{server.HttpPort}/MyEndPoint");
195 IFactorial cal = (IFactorial) obj;
196 Assert.AreEqual (cal.CalculateFactorial (4), 24);
199 [TestFixtureSetUp]
200 public void Start ()
202 AppDomain domain = BaseCallTest.CreateDomain ("testdomain");
203 server = (Server) domain.CreateInstanceAndUnwrap
204 (typeof (Server).Assembly.FullName, typeof (Server).FullName);
205 server.Start ();
208 [TestFixtureTearDown]
209 public void Stop ()
211 server.Stop ();
212 if (channel != null)
213 ChannelServices.UnregisterChannel (channel);
216 public interface IFactorial
218 // With this line everything works
219 //ulong CalculateFactorial(uint a);
220 // With this line it doesn't
221 ulong CalculateFactorial(uint b);
225 public class Server : MarshalByRefObject
227 HttpChannel c;
229 public void Start ()
231 HttpPort = NetworkHelpers.FindFreePort ();
232 Hashtable props = new Hashtable ();
233 props["port"] = HttpPort;
234 props["bindTo"] = "127.0.0.1";
235 c = new HttpChannel (props, null, null);
236 ChannelServices.RegisterChannel (c);
238 Type t = typeof(Calculator);
239 RemotingConfiguration.RegisterWellKnownServiceType (t, "MyEndPoint",
240 WellKnownObjectMode.Singleton);
243 public void Stop ()
245 c.StopListening (null);
246 ChannelServices.UnregisterChannel (c);
249 public int HttpPort { get; private set; }
252 public class Calculator : MarshalByRefObject, IFactorial
254 public ulong CalculateFactorial (uint a)
256 ulong res = 1;
257 for (uint i=1 ; i<=a; i++)
258 res = res * i;
259 return res;