浏览代码

Modify SocketAbstraction.CanWrite to return false when socket is null.

drieseng 9 年之前
父节点
当前提交
852efaf9f9

+ 9 - 1
src/Renci.SshNet/Abstractions/SocketAbstraction.cs

@@ -25,9 +25,17 @@ namespace Renci.SshNet.Abstractions
 
         }
 
+        /// <summary>
+        /// Returns a value indicating whether the specified <see cref="Socket"/> can be used
+        /// to send data.
+        /// </summary>
+        /// <param name="socket">The <see cref="Socket"/> to check.</param>
+        /// <returns>
+        /// <c>true</c> if <paramref name="socket"/> can be written to; otherwise, <c>false</c>.
+        /// </returns>
         public static bool CanWrite(Socket socket)
         {
-            if (socket.Connected)
+            if (socket != null && socket.Connected)
             {
 #if FEATURE_SOCKET_POLL
                 return socket.Poll(-1, SelectMode.SelectWrite);

+ 2 - 2
src/Renci.SshNet/Session.cs

@@ -834,7 +834,7 @@ namespace Renci.SshNet
         /// <exception cref="InvalidOperationException">The size of the packet exceeds the maximum size defined by the protocol.</exception>
         internal void SendMessage(Message message)
         {
-            if (_socket == null || !_socket.CanWrite())
+            if (!_socket.CanWrite())
                 throw new SshConnectionException("Client not connected.");
 
             if (_keyExchangeInProgress && !(message is IKeyExchangedAllowed))
@@ -893,7 +893,7 @@ namespace Renci.SshNet
                 }
 
                 // increment the packet sequence number only after we're sure the packet has
-                // been sent; even though it's only used for the MAC, it need to be incremented
+                // been sent; even though it's only used for the MAC, it needs to be incremented
                 // for each package sent.
                 // 
                 // the server will use it to verify the data integrity, and as such the order in

+ 24 - 0
test/Renci.SshNet.Shared.Tests/Abstractions/SocketAbstraction_CanWrite.cs

@@ -0,0 +1,24 @@
+using System.Net.Sockets;
+using Renci.SshNet.Abstractions;
+#if SILVERLIGHT
+using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
+#else
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+#endif
+
+namespace Renci.SshNet.Tests.Abstractions
+{
+    [TestClass]
+    public class SocketAbstraction_CanWrite
+    {
+        [TestMethod]
+        public void ShouldReturnFalseWhenSocketIsNull()
+        {
+            const Socket socket = null;
+
+            var actual = SocketAbstraction.CanWrite(socket);
+
+            Assert.IsFalse(actual);
+        }
+    }
+}

+ 1 - 0
test/Renci.SshNet.Shared.Tests/Renci.SshNet.Shared.Tests.projitems

@@ -12,6 +12,7 @@
     <Compile Include="$(MSBuildThisFileDirectory)Abstractions\CryptoAbstraction_GenerateRandom.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Abstractions\DnsAbstraction_GetHostAddresses.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Abstractions\FileSystemAbstraction_EnumerateFiles.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Abstractions\SocketAbstraction_CanWrite.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Abstractions\ThreadAbstraction_ExecuteThread.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)ForwardedPortStatusTest_Started.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)ForwardedPortStatusTest_Starting.cs" />