mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-09-10 09:15:47 +00:00
4e02502bdf
* Add .NET 10 target * fix IDE0031 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0031 * fix ca5399 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca5399 * fix ca1515 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1515 * fix ca2002 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2002 * fix ca1508 new false positives. https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1508 * fix ca2000 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2000 * fix ca2025 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2025 * fix ca1849 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1849 * fix Reverse() overloads because of https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/10.0/csharp-overload-resolution * supress CA2002 * Use extension members for ThrowHelpers * use extension members for CryptoAbstractions * use extension member for DateTime.UnixEpoch * use extension members for string.Join etc * use extension members for Convert.To/FromHexString * disable CA1508 * Update .NET 10 RC2 * Workaround Build Regression in .NET 10 RC2 https://github.com/dotnet/sdk/issues/51265 * suppress new warnings introduced by merge * Update to .NET 10 final release * Revert "Workaround Build Regression in .NET 10 RC2" This is fixed in the final release. This reverts commit5a59ac9aa8. * fix new warnings with MSTest 4 + .NET 10 * use same Randomizer instance * disable CA2000 * reduce CA1849 suppressions and disable duplicate S6966 * disable preview analyzers reverts6c3c06d95a
244 lines
8.7 KiB
C#
244 lines
8.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Threading;
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
using Moq;
|
|
|
|
using Renci.SshNet.Abstractions;
|
|
using Renci.SshNet.Channels;
|
|
using Renci.SshNet.Common;
|
|
using Renci.SshNet.Tests.Common;
|
|
|
|
namespace Renci.SshNet.Tests.Classes
|
|
{
|
|
[TestClass]
|
|
public class ForwardedPortDynamicTest_SessionErrorOccurred_ChannelBound
|
|
{
|
|
private Mock<ISession> _sessionMock;
|
|
private Mock<IConnectionInfo> _connectionInfoMock;
|
|
private Mock<IChannelDirectTcpip> _channelMock;
|
|
private ForwardedPortDynamic _forwardedPort;
|
|
private IList<EventArgs> _closingRegister;
|
|
private IList<ExceptionEventArgs> _exceptionRegister;
|
|
private Exception _sessionException;
|
|
private IPEndPoint _endpoint;
|
|
private Socket _client;
|
|
private IPEndPoint _remoteEndpoint;
|
|
private string _userName;
|
|
private TimeSpan _bindSleepTime;
|
|
private ManualResetEvent _channelBindStarted;
|
|
private ManualResetEvent _channelBindCompleted;
|
|
|
|
[TestInitialize]
|
|
public void Setup()
|
|
{
|
|
Arrange();
|
|
Act();
|
|
}
|
|
|
|
[TestCleanup]
|
|
public void Cleanup()
|
|
{
|
|
_client?.Dispose();
|
|
_client = null;
|
|
_forwardedPort?.Dispose();
|
|
_forwardedPort = null;
|
|
_channelBindStarted?.Dispose();
|
|
_channelBindStarted = null;
|
|
_channelBindCompleted?.Dispose();
|
|
_channelBindCompleted = null;
|
|
}
|
|
|
|
private void CreateMocks()
|
|
{
|
|
_connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
|
|
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
|
|
_sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
|
|
_channelMock = new Mock<IChannelDirectTcpip>(MockBehavior.Strict);
|
|
}
|
|
|
|
private void SetupData()
|
|
{
|
|
var random = new Random();
|
|
|
|
_closingRegister = new List<EventArgs>();
|
|
_exceptionRegister = new List<ExceptionEventArgs>();
|
|
_endpoint = new IPEndPoint(IPAddress.Loopback, 8122);
|
|
_remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"), random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
|
|
_bindSleepTime = TimeSpan.FromMilliseconds(random.Next(100, 500));
|
|
_userName = random.Next().ToString(CultureInfo.InvariantCulture);
|
|
_forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint)_endpoint.Port);
|
|
_sessionException = new Exception();
|
|
_channelBindStarted = new ManualResetEvent(false);
|
|
_channelBindCompleted = new ManualResetEvent(false);
|
|
|
|
_forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
|
|
_forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
|
|
_forwardedPort.Session = _sessionMock.Object;
|
|
|
|
_client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
|
|
{
|
|
ReceiveTimeout = 100,
|
|
SendTimeout = 100,
|
|
SendBufferSize = 0
|
|
};
|
|
}
|
|
|
|
private void SetupMocks()
|
|
{
|
|
_connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(15));
|
|
_sessionMock.Setup(p => p.IsConnected).Returns(true);
|
|
_sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
|
|
_sessionMock.Setup(p => p.CreateChannelDirectTcpip()).Returns(_channelMock.Object);
|
|
_channelMock.Setup(p => p.Open(_remoteEndpoint.Address.ToString(), (uint)_remoteEndpoint.Port, _forwardedPort, It.IsAny<Socket>()));
|
|
_channelMock.Setup(p => p.IsOpen).Returns(true);
|
|
_channelMock.Setup(p => p.Bind()).Callback(() =>
|
|
{
|
|
_channelBindStarted.Set();
|
|
Thread.Sleep(_bindSleepTime);
|
|
_channelBindCompleted.Set();
|
|
});
|
|
_channelMock.Setup(p => p.Dispose());
|
|
}
|
|
|
|
protected void Arrange()
|
|
{
|
|
CreateMocks();
|
|
SetupData();
|
|
SetupMocks();
|
|
|
|
// start port
|
|
_forwardedPort.Start();
|
|
// connect to port
|
|
EstablishSocks4Connection(_client);
|
|
// wait until SOCKS client is bound to channel
|
|
Assert.IsTrue(_channelBindStarted.WaitOne(TimeSpan.FromMilliseconds(200)));
|
|
}
|
|
|
|
protected void Act()
|
|
{
|
|
_sessionMock.Raise(p => p.ErrorOccured += null, new ExceptionEventArgs(_sessionException));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldBlockUntilBindHasCompleted()
|
|
{
|
|
Assert.IsTrue(_channelBindCompleted.WaitOne(0));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void IsStartedShouldReturnFalse()
|
|
{
|
|
Assert.IsFalse(_forwardedPort.IsStarted);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ForwardedPortShouldRefuseNewConnections()
|
|
{
|
|
using (var client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
|
|
{
|
|
try
|
|
{
|
|
client.Connect(_endpoint);
|
|
Assert.Fail();
|
|
}
|
|
catch (SocketException ex)
|
|
{
|
|
Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode);
|
|
}
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void BoundClientShouldNotBeClosed()
|
|
{
|
|
// the forwarded port itself does not close the client connection; when the channel is closed properly
|
|
// it's the channel that will take care of closing the client connection
|
|
//
|
|
// we'll check if the client connection is still alive by attempting to receive, which should time out
|
|
// as the forwarded port (or its channel) are not sending anything
|
|
|
|
var buffer = new byte[1];
|
|
|
|
try
|
|
{
|
|
_client.Receive(buffer);
|
|
Assert.Fail();
|
|
}
|
|
catch (SocketException ex)
|
|
{
|
|
Assert.AreEqual(SocketError.TimedOut, ex.SocketErrorCode);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ClosingShouldHaveFiredOnce()
|
|
{
|
|
Assert.HasCount(1, _closingRegister);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ExceptionShouldHaveFiredOne()
|
|
{
|
|
Assert.HasCount(1, _exceptionRegister, _exceptionRegister.AsString());
|
|
Assert.IsNotNull(_exceptionRegister[0], _exceptionRegister.AsString());
|
|
Assert.AreSame(_sessionException, _exceptionRegister[0].Exception, _exceptionRegister.AsString());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void OpenOnChannelShouldBeInvokedOnce()
|
|
{
|
|
_channelMock.Verify(
|
|
p =>
|
|
p.Open(_remoteEndpoint.Address.ToString(), (uint)_remoteEndpoint.Port, _forwardedPort,
|
|
It.IsAny<Socket>()), Times.Once);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void BindOnChannelShouldBeInvokedOnce()
|
|
{
|
|
_channelMock.Verify(p => p.Bind(), Times.Once);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void DisposeOnChannelShouldBeInvokedOnce()
|
|
{
|
|
_channelMock.Verify(p => p.Dispose(), Times.Once);
|
|
}
|
|
|
|
private void EstablishSocks4Connection(Socket client)
|
|
{
|
|
var userNameBytes = Encoding.ASCII.GetBytes(_userName);
|
|
var addressBytes = _remoteEndpoint.Address.GetAddressBytes();
|
|
var portBytes = BitConverter.GetBytes((ushort)_remoteEndpoint.Port).AsEnumerable().Reverse().ToArray();
|
|
|
|
_client.Connect(_endpoint);
|
|
|
|
// send SOCKS version
|
|
client.Send(new byte[] { 0x04 }, 0, 1, SocketFlags.None);
|
|
// send command byte
|
|
client.Send(new byte[] { 0x00 }, 0, 1, SocketFlags.None);
|
|
// send port
|
|
client.Send(portBytes, 0, portBytes.Length, SocketFlags.None);
|
|
// send address
|
|
client.Send(addressBytes, 0, addressBytes.Length, SocketFlags.None);
|
|
// send user name
|
|
client.Send(userNameBytes, 0, userNameBytes.Length, SocketFlags.None);
|
|
// terminate user name with null
|
|
client.Send(new byte[] { 0x00 }, 0, 1, SocketFlags.None);
|
|
|
|
var buffer = new byte[8];
|
|
var bytesRead = SocketAbstraction.Read(client, buffer, 0, buffer.Length, TimeSpan.FromMilliseconds(500));
|
|
Assert.AreEqual(buffer.Length, bytesRead);
|
|
}
|
|
}
|
|
}
|