[System] Fix HttpListenerRequestTest.HttpRequestIsLocal intermittently failing
authorAlexander Köplinger <alex.koeplinger@outlook.com>
Thu, 9 Jun 2016 23:37:45 +0000 (01:37 +0200)
committerAlexander Köplinger <alex.koeplinger@outlook.com>
Thu, 9 Jun 2016 23:39:14 +0000 (01:39 +0200)
We're seeing the following exception on devices in CI:

```
[FAIL] HttpListenerRequestTest.HttpRequestIsLocal : System.Net.Sockets.SocketException : Could not resolve host 'XQA-AppleTV-1'
  at System.Net.Dns.Error_11001 (System.String hostName) [0x00000] in /Users/builder/data/lanes/1381/d8287824/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net/Dns.cs:292
  at System.Net.Dns.GetHostByName (System.String hostName) [0x00024] in /Users/builder/data/lanes/1381/d8287824/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net/Dns.cs:419
  at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00061] in /Users/builder/data/lanes/1381/d8287824/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net/Dns.cs:380
  at System.Net.Dns.GetHostAddresses (System.String hostNameOrAddress) [0x00065] in /Users/builder/data/lanes/1381/d8287824/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net/Dns.cs:406
  at MonoTests.System.Net.HttpListenerRequestTest.HttpRequestIsLocal () [0x00006] in /Users/xamarinqa/QABot/data/lanes/2441/d8287824/source/xamarin-macios/external/mono/mcs/class/System/Test/System.Net/HttpListenerRequestTest.cs:197
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in /Users/builder/data/lanes/1381/d8287824/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:309
```

From looking at the code, resolving the hostname to IP addresses via DNS fails so the test doesn't even run.
We don't care where the local IPs come from so we can use an alternative way of getting them - via querying the network interfaces.

Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=41671

PS: fixed a hardcoded port while I was touching the code

mcs/class/System/Test/System.Net/HttpListenerRequestTest.cs

index c088e4e14e2ddeb71509bbda356c4d7761d12c66..6ff1b33260ed2ab7e82883d4f600a12e89136888 100644 (file)
@@ -30,6 +30,7 @@
 using System;
 using System.IO;
 using System.Net;
+using System.Net.NetworkInformation;
 using System.Net.Sockets;
 using System.Text;
 using System.Collections.Generic;
@@ -194,15 +195,22 @@ namespace MonoTests.System.Net
                [Test]
                public void HttpRequestIsLocal ()
                {
-                       var ips = new List<IPAddress> (Dns.GetHostAddresses (Dns.GetHostName ()));
+                       var port = NetworkHelpers.FindFreePort ();
+                       var ips = new List<IPAddress> ();
                        ips.Add (IPAddress.Loopback);
+                       foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces ()) {
+                               foreach (var ip in adapter.GetIPProperties ().UnicastAddresses) {
+                                       ips.Add (ip.Address);
+                               }
+                       }
+
                        foreach (var ip in ips) {
                                if (ip.AddressFamily != AddressFamily.InterNetwork)
                                        continue;
 
                                HttpListener listener = HttpListener2Test.CreateAndStartListener (
-                                       "http://" + ip + ":9000/HttpRequestIsLocal/");
-                               NetworkStream ns = HttpListener2Test.CreateNS (ip, 9000);
+                                       "http://" + ip + ":" + port + "/HttpRequestIsLocal/");
+                               NetworkStream ns = HttpListener2Test.CreateNS (ip, port);
                                HttpListener2Test.Send (ns, "GET /HttpRequestIsLocal/ HTTP/1.0\r\n\r\n");
                                HttpListenerContext ctx = listener.GetContext ();
                                HttpListenerRequest request = ctx.Request;