Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SocketException]: New Constructor with string? for SocketException #74744

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Net.Sockets
{
internal static partial class SocketExceptionFactory
{
public static SocketException CreateSocketException(int socketError, EndPoint endPoint)
{
int nativeErr = (int)socketError;

// If an interop error was not found, then don't invoke Info().RawErrno as that will fail with assert.
if (SocketErrorPal.TryGetNativeErrorForSocketError((SocketError)socketError, out Interop.Error interopErr))
{
nativeErr = interopErr.Info().RawErrno;
}

return new SocketException(socketError, CreateMessage(nativeErr, endPoint));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Net.Sockets
{
internal static partial class SocketExceptionFactory
{
public static SocketException CreateSocketException(int socketError, EndPoint endPoint)
{
// Windows directly maps socketError to native error code.
return new SocketException(socketError, CreateMessage(socketError, endPoint));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;

namespace System.Net.Sockets
{
internal static partial class SocketExceptionFactory
{
private static string CreateMessage(int nativeSocketError, EndPoint endPoint)
{
return Marshal.GetPInvokeErrorMessage(nativeSocketError) + " " + endPoint.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ public partial class SocketException : System.ComponentModel.Win32Exception
{
public SocketException() { }
public SocketException(int errorCode) { }
public SocketException(int errorCode, string? message) { }
protected SocketException(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext) { }
public override int ErrorCode { get { throw null; } }
public override string Message { get { throw null; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@ public SocketException(int errorCode) : this((SocketError)errorCode)
// but that's the least bad option right now.
}

/// <summary>Initializes a new instance of the <see cref='System.Net.Sockets.SocketException'/> class with the specified error code and optional message.</summary>
public SocketException(int errorCode, string? message) : this((SocketError)errorCode, message)
{
}

/// <summary>Creates a new instance of the <see cref='System.Net.Sockets.SocketException'/> class with the specified error code as SocketError.</summary>
internal SocketException(SocketError socketError) : base(GetNativeErrorForSocketError(socketError))
{
_errorCode = socketError;
}

/// <summary>Initializes a new instance of the <see cref='System.Net.Sockets.SocketException'/> class with the specified error code as SocketError and optional message.</summary>
internal SocketException(SocketError socketError, string? message) : base(GetNativeErrorForSocketError(socketError), message)
{
_errorCode = socketError;
}

public override string Message => base.Message;

public SocketError SocketErrorCode => _errorCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Net.Sockets;
using Xunit;

namespace System.Net.Primitives.Functional.Tests
{
public static class SocketExceptionTest
{
[Fact]
public static void Create_AllErrorCodes_Success()
{
foreach (SocketError error in Enum.GetValues(typeof(SocketError)))
{
SocketException e = new SocketException((int)error);
Assert.Equal(error, e.SocketErrorCode);
Assert.Null(e.InnerException);
Assert.NotNull(e.Message);
}
}

[Fact]
public static void Create_ExceptionWithMessage_Success()
{
const string message = "Hello World";
liveans marked this conversation as resolved.
Show resolved Hide resolved
SocketException e = new SocketException((int)SocketError.AccessDenied, message);
Assert.Equal(SocketError.AccessDenied, e.SocketErrorCode);
Assert.Null(e.InnerException);
Assert.Equal(message, e.Message);
Assert.Contains(message, e.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<Compile Include="LoggingTest.cs" />
<Compile Include="RequestCachePolicyTest.cs" />
<Compile Include="CookieContainerAddTest.cs" />
<Compile Include="$(CommonTestPath)System\Diagnostics\Tracing\TestEventListener.cs"
Link="Common\System\Diagnostics\Tracing\TestEventListener.cs" />
<Compile Include="$(CommonTestPath)System\Diagnostics\Tracing\TestEventListener.cs" Link="Common\System\Diagnostics\Tracing\TestEventListener.cs" />
<Compile Include="SocketExceptionTest.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
Link="Common\System\Net\Internals\IPEndPointExtensions.cs" />
<Compile Include="$(CommonPath)System\Net\Internals\IPAddressExtensions.cs"
Link="Common\System\Net\Internals\IPAddressExtensions.cs" />
<Compile Include="$(CommonPath)System\Net\Internals\SocketExceptionFactory.cs"
Link="Common\System\Net\Internals\SocketExceptionFactory.cs" />
<Compile Include="$(CommonPath)System\Net\Sockets\SocketExceptionFactory.cs"
Link="Common\System\Net\Sockets\SocketExceptionFactory.cs" />
<Compile Include="$(CommonPath)System\Net\Sockets\ProtocolFamily.cs"
Link="Common\System\Net\Sockets\ProtocolFamily.cs" />
<Compile Include="$(CommonPath)System\Net\Sockets\ProtocolType.cs"
Expand All @@ -100,6 +100,8 @@
Link="Common\System\Net\SocketAddressPal.Windows.cs" />
<Compile Include="$(CommonPath)System\Net\SocketProtocolSupportPal.Windows.cs"
Link="Common\System\Net\SocketProtocolSupportPal.Windows" />
<Compile Include="$(CommonPath)System\Net\Sockets\SocketExceptionFactory.Windows.cs"
Link="Common\System\Net\Sockets\SocketExceptionFactory.Windows.cs" />
<!-- Interop -->
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs"
Link="Common\Interop\Windows\Interop.Libraries.cs" />
Expand Down Expand Up @@ -278,6 +280,8 @@
Link="Common\Interop\Unix\System.Native\Interop.Pipe.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Write.cs"
Link="Common\Interop\Unix\System.Native\Interop.Write.cs" />
<Compile Include="$(CommonPath)System\Net\Sockets\SocketExceptionFactory.Unix.cs"
Link="Common\System\Net\Sockets\SocketExceptionFactory.Unix.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.Win32.Primitives" />
Expand Down
23 changes: 23 additions & 0 deletions src/libraries/System.Net.Sockets/tests/FunctionalTests/Connect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
Expand Down Expand Up @@ -218,6 +219,28 @@ public ConnectApm(ITestOutputHelper output) : base(output) {}
public sealed class ConnectTask : Connect<SocketHelperTask>
{
public ConnectTask(ITestOutputHelper output) : base(output) {}

[OuterLoop]
[Fact]
public static void Connect_ThrowSocketException_Success()
{
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
int anonymousPort = socket.BindToAnonymousPort(IPAddress.Loopback);
IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, anonymousPort);
Assert.ThrowsAsync<SocketException>(() => socket.ConnectAsync(ep));
try
{
socket.Connect(ep);
Assert.Fail("Socket Connect should throw SocketException in this case.");
}
catch (SocketException ex)
{
Assert.Contains(Marshal.GetPInvokeErrorMessage(ex.NativeErrorCode), ex.Message);
Assert.Contains(ep.ToString(), ex.Message);
}
}
}
}

public sealed class ConnectEap : Connect<SocketHelperEap>
Expand Down