Fix MulticastInterface_Set_ValidIndex_Success failures on non-Windows platforms....
authorEric Eilebrecht <ericeil@users.noreply.github.com>
Thu, 16 Jun 2016 23:14:21 +0000 (16:14 -0700)
committerGitHub <noreply@github.com>
Thu, 16 Jun 2016 23:14:21 +0000 (16:14 -0700)
This test assumed that the "loopback" interface was always at index 1, which is not true on Linux/OSX/etc.  This change makes that assumption explicitly Windows-only, while allowing the "any interface" case to run on all platforms.

Commit migrated from https://github.com/dotnet/corefx/commit/212783dd5e84051675b65fa69ae1809d01f1c8c6

src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketOptionNameTest.cs

index 29fd280..dc5ee4e 100644 (file)
@@ -94,15 +94,26 @@ namespace System.Net.Sockets.Tests
             }
         }
 
-        private static bool IsNotOSXOrFedora23()
+        [Fact]
+        public void MulticastInterface_Set_AnyInterface_Succeeds()
+        {
+            // On all platforms, index 0 means "any interface"
+            MulticastInterface_Set_Helper(0);
+        }
+
+        [Fact]
+        [PlatformSpecific(PlatformID.Windows)] // see comment below
+        public void MulticastInterface_Set_Loopback_Succeeds()
         {
-            return !PlatformDetection.IsOSX && !PlatformDetection.IsFedora23;
+            // On Windows, we can apparently assume interface 1 is "loopback."  On other platforms, this is not a
+            // valid assumption.  We could maybe use NetworkInterface.LoopbackInterfaceIndex to get the index, but
+            // this would introduce a dependency on System.Net.NetworkInformation, which depends on System.Net.Sockets,
+            // which is what we're testing here....  So for now, we'll just assume "loopback == 1" and run this on
+            // Windows only.
+            MulticastInterface_Set_Helper(1);
         }
 
-        [ConditionalTheory(nameof(IsNotOSXOrFedora23))] // Receive times out on loopback interface
-        [InlineData(0)] // Any
-        [InlineData(1)] // Loopback
-        public void MulticastInterface_Set_ValidIndex_Succeeds(int interfaceIndex)
+        private void MulticastInterface_Set_Helper(int interfaceIndex)
         {
             IPAddress multicastAddress = IPAddress.Parse("239.1.2.3");
             string message = "hello";
@@ -115,7 +126,11 @@ namespace System.Net.Sockets.Tests
                 receiveSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(multicastAddress, interfaceIndex));
 
                 sendSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(interfaceIndex));
-                sendSocket.SendTo(Encoding.UTF8.GetBytes(message), new IPEndPoint(multicastAddress, port));
+
+                for (int i = 0; i < Configuration.UDPRedundancy; i++)
+                {
+                    sendSocket.SendTo(Encoding.UTF8.GetBytes(message), new IPEndPoint(multicastAddress, port));
+                }
 
                 var receiveBuffer = new byte[1024];
                 int bytesReceived = receiveSocket.Receive(receiveBuffer);