throw for unknown or unsupported AddressFamily on Windows (dotnet/corefx#39785)
authorTomas Weinfurt <tweinfurt@yahoo.com>
Fri, 26 Jul 2019 20:31:37 +0000 (13:31 -0700)
committerGitHub <noreply@github.com>
Fri, 26 Jul 2019 20:31:37 +0000 (13:31 -0700)
* throw for unknown or unsupported SocketAddress on Windows

* feedback from review

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

src/libraries/Common/src/System/Net/SocketAddressPal.Windows.cs
src/libraries/System.Net.Primitives/tests/FunctionalTests/SocketAddressTest.cs

index 3a2d9bb..1033886 100644 (file)
@@ -18,6 +18,14 @@ namespace System.Net
 
         public static unsafe void SetAddressFamily(byte[] buffer, AddressFamily family)
         {
+            if ((int)(family) > (int)AddressFamily.Max)
+            {
+                // For legacy values up to AddressFamily.Max, family maps directly to Winsock value.
+                // Other values will need mapping if/when supported.
+                // Currently, that is Netlink, Packet and ControllerAreaNetwork, neither of them supported on Windows.
+                throw new PlatformNotSupportedException();
+            }
+
 #if BIGENDIAN
             buffer[0] = unchecked((byte)((int)family >> 8));
             buffer[1] = unchecked((byte)((int)family));
index e851b40..974d16e 100644 (file)
@@ -75,10 +75,49 @@ namespace System.Net.Primitives.Functional.Tests
             Assert.Equal(expected, sa.ToString());
         }
 
-        [ActiveIssue(37997, TestPlatforms.AnyUnix)]
-        [Theory] // recombine into ToString_Expected_Success when #37997 is fixed
-        [InlineData((AddressFamily)12345, -1, false, "12345:32:{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}")]
-        public static void ToString_UnknownFamily_Success(AddressFamily family, int size, bool fillBytes, string expected) =>
-            ToString_Expected_Success(family, size, fillBytes, expected);
+        [Theory]
+        [InlineData((AddressFamily)12345, -1)]
+        [InlineData((AddressFamily)12345, 16)]
+        public static void ToString_UnknownFamily_Throws(AddressFamily family, int size)
+        {
+            Assert.Throws<PlatformNotSupportedException>(() => size >= 0 ? new SocketAddress(family, size) : new SocketAddress(family));
+        }
+
+        [Theory]
+        [InlineData(AddressFamily.Packet)]
+        [InlineData(AddressFamily.Netlink)]
+        [InlineData(AddressFamily.ControllerAreaNetwork)]
+        [PlatformSpecific(~TestPlatforms.Linux)]
+        public static void ToString_UnsupportedFamily_Throws(AddressFamily family)
+        {
+            Assert.Throws<PlatformNotSupportedException>(() => new SocketAddress(family));
+        }
+
+        [Theory]
+        [InlineData(AddressFamily.Packet)]
+        [InlineData(AddressFamily.ControllerAreaNetwork)]
+        [PlatformSpecific(TestPlatforms.Linux)]
+        public static void ToString_LinuxSpecificFamily_Success(AddressFamily family)
+        {
+            SocketAddress sa = new SocketAddress(family);
+            Assert.Equal(family, sa.Family);
+        }
+
+        [Fact]
+        [PlatformSpecific(TestPlatforms.Windows)]
+        public static void ToString_AllFamilies_Success()
+        {
+            foreach (AddressFamily family in Enum.GetValues(typeof(AddressFamily)))
+            {
+                if ((int)family > (int)AddressFamily.Max)
+                {
+                    // Skip Linux specific protocols.
+                    continue;
+                }
+
+                var sa = new SocketAddress(family);
+                Assert.NotNull(sa.ToString());
+            }
+        }
     }
 }