From 87bff6cc0239070e51c84e802a4810ce8d6c07a4 Mon Sep 17 00:00:00 2001 From: Tomas Weinfurt Date: Sat, 21 Jul 2018 17:02:06 -0700 Subject: [PATCH] Adjust DataOffset so SocketAddress.ToString() works similar to Windows (dotnet/corefx#31134) Correct DataOffset and add test for ToString() function. Fixes dotnet/corefx#30523 Commit migrated from https://github.com/dotnet/corefx/commit/4ce283d92d06b3b759155c5803e8036ca865ef22 --- src/libraries/Common/src/System/Net/SocketAddress.cs | 5 +++-- src/libraries/Common/src/System/Net/SocketAddressPal.Unix.cs | 2 -- src/libraries/Common/src/System/Net/SocketAddressPal.Windows.cs | 1 - .../System.Net.Primitives/tests/PalTests/SocketAddressPalTest.cs | 9 +++++++++ 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/libraries/Common/src/System/Net/SocketAddress.cs b/src/libraries/Common/src/System/Net/SocketAddress.cs index 726d1a6..c551c60 100644 --- a/src/libraries/Common/src/System/Net/SocketAddress.cs +++ b/src/libraries/Common/src/System/Net/SocketAddress.cs @@ -30,6 +30,7 @@ namespace System.Net.Internals private const int MinSize = 2; private const int MaxSize = 32; // IrDA requires 32 bytes + private const int DataOffset = 2; private bool _changed = true; private int _hash; @@ -230,9 +231,9 @@ namespace System.Net.Internals { var sb = new StringBuilder().Append(Family.ToString()).Append(':').Append(Size).Append(":{"); - for (int i = SocketAddressPal.DataOffset; i < Size; i++) + for (int i = DataOffset; i < Size; i++) { - if (i > SocketAddressPal.DataOffset) + if (i > DataOffset) { sb.Append(','); } diff --git a/src/libraries/Common/src/System/Net/SocketAddressPal.Unix.cs b/src/libraries/Common/src/System/Net/SocketAddressPal.Unix.cs index 9436063..027f050 100644 --- a/src/libraries/Common/src/System/Net/SocketAddressPal.Unix.cs +++ b/src/libraries/Common/src/System/Net/SocketAddressPal.Unix.cs @@ -12,8 +12,6 @@ namespace System.Net { internal static class SocketAddressPal { - public const int DataOffset = 0; - public static readonly int IPv6AddressSize = GetIPv6AddressSize(); public static readonly int IPv4AddressSize = GetIPv4AddressSize(); diff --git a/src/libraries/Common/src/System/Net/SocketAddressPal.Windows.cs b/src/libraries/Common/src/System/Net/SocketAddressPal.Windows.cs index 404a381..3a2d9bb 100644 --- a/src/libraries/Common/src/System/Net/SocketAddressPal.Windows.cs +++ b/src/libraries/Common/src/System/Net/SocketAddressPal.Windows.cs @@ -10,7 +10,6 @@ namespace System.Net { public const int IPv6AddressSize = 28; public const int IPv4AddressSize = 16; - public const int DataOffset = 2; public static unsafe AddressFamily GetAddressFamily(byte[] buffer) { diff --git a/src/libraries/System.Net.Primitives/tests/PalTests/SocketAddressPalTest.cs b/src/libraries/System.Net.Primitives/tests/PalTests/SocketAddressPalTest.cs index 8b731c2..680da0b 100644 --- a/src/libraries/System.Net.Primitives/tests/PalTests/SocketAddressPalTest.cs +++ b/src/libraries/System.Net.Primitives/tests/PalTests/SocketAddressPalTest.cs @@ -188,5 +188,14 @@ namespace System.Net.Primitives.PalTests Assert.Equal(scope, actualScope); Assert.Equal(port, SocketAddressPal.GetPort(buffer)); } + + [Fact] + public void ToString_ValidSocketAddress_ExpectedFormat() + { + IPEndPoint ipLocalEndPoint = new IPEndPoint(IPAddress.Loopback, Convert.ToInt32("cafe", 16)); + SocketAddress socketAddress = ipLocalEndPoint.Serialize(); + + Assert.Equal("InterNetwork:16:{202,254,127,0,0,1,0,0,0,0,0,0,0,0}", socketAddress.ToString()); + } } } -- 2.7.4