From 24f151365fe1a1795775f2ad0287aba8f14f40d0 Mon Sep 17 00:00:00 2001 From: rhirano0715 <104247605+rhirano0715@users.noreply.github.com> Date: Wed, 9 Aug 2023 21:19:38 +0900 Subject: [PATCH] Unified to throw NotSupportedException when SendFile() for connectionless sockets (#87108) * Unified to throw NotSupportedException when SendFile() for connectionless sockets The issue was that the Socket.SendFile() threw inconsistent exceptions when the platform was Windows and the protocol was UDP. The first call would throw a SocketException, while the second call would throw a NotSupportedException. With this commit, SendFile() will consistently throw NotSupportException on all platforms when the protocol is UDP. Fix #47472 * Change to throws `NotSupportedException` if `!IsConnectionOriented` or `!Connected`. Before:. Throws `NotSupportedException` on UDP. After: Throws `NotSupportedException` if `!IsConnectionOriented` or `!Connected`. * Changed test case `UdpConnection_ThrowsException` to run regardless of platform. * Update src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs Co-authored-by: Karel Zikmund --------- Co-authored-by: Karel Zikmund --- .../System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs | 4 ++-- src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs | 2 +- src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs | 7 ++----- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs index a04605b..e1891be 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs @@ -749,8 +749,8 @@ namespace System.Net.Sockets if (!IsConnectionOriented) { - var soex = new SocketException((int)SocketError.NotConnected); - return ValueTask.FromException(soex); + var ex = new NotSupportedException(SR.net_notconnected); + return ValueTask.FromException(ex); } int packetsCount = 0; diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs index b918ceb..414b0ba 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs @@ -1258,7 +1258,7 @@ namespace System.Net.Sockets { ThrowIfDisposed(); - if (!Connected) + if (!IsConnectionOriented || !Connected) { throw new NotSupportedException(SR.net_notconnected); } diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs index e3d35f2..7a87613 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs @@ -62,7 +62,6 @@ namespace System.Net.Sockets.Tests [Theory] [InlineData(false)] [InlineData(true)] - [PlatformSpecific(TestPlatforms.Windows)] public async Task UdpConnection_ThrowsException(bool usePreAndPostbufferOverload) { // Create file to send @@ -77,16 +76,14 @@ namespace System.Net.Sockets.Tests client.Connect(listener.LocalEndPoint); - SocketException ex; if (usePreAndPostbufferOverload) { - ex = await Assert.ThrowsAsync(() => SendFileAsync(client, tempFile.Path, Array.Empty(), Array.Empty(), TransmitFileOptions.UseDefaultWorkerThread)); + await Assert.ThrowsAsync(() => SendFileAsync(client, tempFile.Path, Array.Empty(), Array.Empty(), TransmitFileOptions.UseDefaultWorkerThread)); } else { - ex = await Assert.ThrowsAsync(() => SendFileAsync(client, tempFile.Path)); + await Assert.ThrowsAsync(() => SendFileAsync(client, tempFile.Path)); } - Assert.Equal(SocketError.NotConnected, ex.SocketErrorCode); } public static IEnumerable SendFile_MemberData() -- 2.7.4