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

This commit is contained in:
drieseng
2016-09-21 21:29:59 +02:00
parent 939325ee64
commit 852efaf9f9
4 changed files with 36 additions and 3 deletions
@@ -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
View File
@@ -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
@@ -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);
}
}
}
@@ -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" />