From 1782a8840cebe0eac69ebb79672b2b6397c8b570 Mon Sep 17 00:00:00 2001 From: Tomas Weinfurt Date: Fri, 26 Jul 2019 13:31:37 -0700 Subject: [PATCH] throw for unknown or unsupported AddressFamily on Windows (dotnet/corefx#39785) * throw for unknown or unsupported SocketAddress on Windows * feedback from review Commit migrated from https://github.com/dotnet/corefx/commit/f0a0eca0242cb072acdd940d98d08e43d6adfe28 --- .../src/System/Net/SocketAddressPal.Windows.cs | 8 ++++ .../tests/FunctionalTests/SocketAddressTest.cs | 49 +++++++++++++++++++--- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/libraries/Common/src/System/Net/SocketAddressPal.Windows.cs b/src/libraries/Common/src/System/Net/SocketAddressPal.Windows.cs index 3a2d9bb..1033886 100644 --- a/src/libraries/Common/src/System/Net/SocketAddressPal.Windows.cs +++ b/src/libraries/Common/src/System/Net/SocketAddressPal.Windows.cs @@ -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)); diff --git a/src/libraries/System.Net.Primitives/tests/FunctionalTests/SocketAddressTest.cs b/src/libraries/System.Net.Primitives/tests/FunctionalTests/SocketAddressTest.cs index e851b40..974d16e 100644 --- a/src/libraries/System.Net.Primitives/tests/FunctionalTests/SocketAddressTest.cs +++ b/src/libraries/System.Net.Primitives/tests/FunctionalTests/SocketAddressTest.cs @@ -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(() => 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(() => 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()); + } + } } } -- 2.7.4