Add SocketErrorPal mapping for EPERM (dotnet/corefx#35133)
authorStephen Toub <stoub@microsoft.com>
Wed, 6 Feb 2019 18:29:12 +0000 (13:29 -0500)
committerGitHub <noreply@github.com>
Wed, 6 Feb 2019 18:29:12 +0000 (13:29 -0500)
Commit migrated from https://github.com/dotnet/corefx/commit/470a7ffeb5badcd465f88b3fb1b2444fd2f5c69d

src/libraries/Common/src/System/Net/Sockets/SocketErrorPal.Unix.cs
src/libraries/System.Net.Sockets/tests/FunctionalTests/CreateSocketTests.cs

index d97f9c8..f6f627e 100644 (file)
@@ -20,7 +20,7 @@ namespace System.Net.Sockets
         }
 #endif
 
-        private const int NativeErrorToSocketErrorCount = 41;
+        private const int NativeErrorToSocketErrorCount = 42;
         private const int SocketErrorToNativeErrorCount = 40;
 
         // No Interop.Errors are included for the following SocketErrors, as there's no good mapping:
@@ -67,6 +67,7 @@ namespace System.Net.Sockets
             { Interop.Error.ENOTCONN, SocketError.NotConnected },
             { Interop.Error.ENOTSOCK, SocketError.NotSocket },
             { Interop.Error.ENOTSUP, SocketError.OperationNotSupported },
+            { Interop.Error.EPERM, SocketError.AccessDenied },
             { Interop.Error.EPIPE, SocketError.Shutdown },
             { Interop.Error.EPFNOSUPPORT, SocketError.ProtocolFamilyNotSupported },
             { Interop.Error.EPROTONOSUPPORT, SocketError.ProtocolNotSupported },
@@ -85,7 +86,7 @@ namespace System.Net.Sockets
             // SocketError.IOPending also maps closest to EINPROGRESS.  As such, roundtripping won't necessarily provide the original value 100% of the time,
             // but it's the best we can do given the mismatch between Interop.Error and SocketError.
 
-            { SocketError.AccessDenied, Interop.Error.EACCES},
+            { SocketError.AccessDenied, Interop.Error.EACCES}, // could also have been EPERM
             { SocketError.AddressAlreadyInUse, Interop.Error.EADDRINUSE  },
             { SocketError.AddressNotAvailable, Interop.Error.EADDRNOTAVAIL },
             { SocketError.AddressFamilyNotSupported, Interop.Error.EAFNOSUPPORT  },
index 58ab9d4..48f6cea 100644 (file)
@@ -30,6 +30,7 @@ namespace System.Net.Sockets.Tests
         };
 
         private static bool SupportsRawSockets => AdminHelpers.IsProcessElevated();
+        private static bool NotSupportsRawSockets => !SupportsRawSockets;
 
         [OuterLoop] // TODO: Issue #11345
         [Theory, MemberData(nameof(DualModeSuccessInputs))]
@@ -91,13 +92,27 @@ namespace System.Net.Sockets.Tests
         [InlineData(AddressFamily.InterNetworkV6, ProtocolType.Udp)]
         [InlineData(AddressFamily.InterNetworkV6, ProtocolType.IcmpV6)]
         [ConditionalTheory(nameof(SupportsRawSockets))]
-        public void Ctor_Raw_Success(AddressFamily addressFamily, ProtocolType protocolType)
+        public void Ctor_Raw_Supported_Success(AddressFamily addressFamily, ProtocolType protocolType)
         {
             using (new Socket(addressFamily, SocketType.Raw, protocolType))
             {
             }
         }
 
+        [PlatformSpecific(TestPlatforms.AnyUnix)]
+        [InlineData(AddressFamily.InterNetwork, ProtocolType.Tcp)]
+        [InlineData(AddressFamily.InterNetwork, ProtocolType.Udp)]
+        [InlineData(AddressFamily.InterNetwork, ProtocolType.Icmp)]
+        [InlineData(AddressFamily.InterNetworkV6, ProtocolType.Tcp)]
+        [InlineData(AddressFamily.InterNetworkV6, ProtocolType.Udp)]
+        [InlineData(AddressFamily.InterNetworkV6, ProtocolType.IcmpV6)]
+        [ConditionalTheory(nameof(NotSupportsRawSockets))]
+        public void Ctor_Raw_NotSupported_ExpectedError(AddressFamily addressFamily, ProtocolType protocolType)
+        {
+            SocketException e = Assert.Throws<SocketException>(() => new Socket(addressFamily, SocketType.Raw, protocolType));
+            Assert.Contains(e.SocketErrorCode, new[] { SocketError.AccessDenied, SocketError.ProtocolNotSupported });
+        }
+
         [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Sockets are still inheritable on netfx: https://github.com/dotnet/corefx/pull/32903")]
         [Theory]
         [InlineData(true, 0)] // Accept