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
143 lines
6.3 KiB
C#
143 lines
6.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Net;
|
|
using System.Threading;
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
using Moq;
|
|
|
|
using Renci.SshNet.Channels;
|
|
using Renci.SshNet.Common;
|
|
using Renci.SshNet.Messages.Connection;
|
|
|
|
namespace Renci.SshNet.Tests.Classes
|
|
{
|
|
[TestClass]
|
|
public class ForwardedPortRemoteTest_Dispose_PortStopped
|
|
{
|
|
private Mock<ISession> _sessionMock;
|
|
private Mock<IConnectionInfo> _connectionInfoMock;
|
|
private IList<EventArgs> _closingRegister;
|
|
private IList<ExceptionEventArgs> _exceptionRegister;
|
|
private IPEndPoint _bindEndpoint;
|
|
private IPEndPoint _remoteEndpoint;
|
|
|
|
protected ForwardedPortRemote ForwardedPort { get; private set; }
|
|
|
|
[TestInitialize]
|
|
public void Setup()
|
|
{
|
|
Arrange();
|
|
Act();
|
|
}
|
|
|
|
[TestCleanup]
|
|
public void Cleanup()
|
|
{
|
|
ForwardedPort?.Dispose();
|
|
ForwardedPort = null;
|
|
}
|
|
|
|
protected void Arrange()
|
|
{
|
|
var random = new Random();
|
|
_closingRegister = new List<EventArgs>();
|
|
_exceptionRegister = new List<ExceptionEventArgs>();
|
|
_bindEndpoint = new IPEndPoint(IPAddress.Any, random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
|
|
_remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"), random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
|
|
ForwardedPort = new ForwardedPortRemote(_bindEndpoint.Address, (uint)_bindEndpoint.Port, _remoteEndpoint.Address, (uint)_remoteEndpoint.Port);
|
|
|
|
_connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
|
|
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
|
|
_sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
|
|
|
|
_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.RegisterMessage("SSH_MSG_REQUEST_FAILURE"));
|
|
_sessionMock.Setup(p => p.RegisterMessage("SSH_MSG_REQUEST_SUCCESS"));
|
|
_sessionMock.Setup(p => p.RegisterMessage("SSH_MSG_CHANNEL_OPEN"));
|
|
_sessionMock.Setup(
|
|
p =>
|
|
p.SendMessage(
|
|
It.Is<TcpIpForwardGlobalRequestMessage>(
|
|
g =>
|
|
g.AddressToBind == ForwardedPort.BoundHost &&
|
|
g.PortToBind == ForwardedPort.BoundPort)))
|
|
.Callback(
|
|
() =>
|
|
_sessionMock.Raise(s => s.RequestSuccessReceived += null,
|
|
new MessageEventArgs<RequestSuccessMessage>(new RequestSuccessMessage())));
|
|
_sessionMock.Setup(p => p.WaitOnHandle(It.IsAny<WaitHandle>()));
|
|
_sessionMock.Setup(
|
|
p =>
|
|
p.SendMessage(
|
|
It.Is<CancelTcpIpForwardGlobalRequestMessage>(
|
|
g =>
|
|
g.AddressToBind == ForwardedPort.BoundHost &&
|
|
g.PortToBind == ForwardedPort.BoundPort)))
|
|
.Callback(
|
|
() =>
|
|
_sessionMock.Raise(s => s.RequestSuccessReceived += null,
|
|
new MessageEventArgs<RequestSuccessMessage>(new RequestSuccessMessage())));
|
|
_sessionMock.Setup(p => p.MessageListenerCompleted).Returns(new ManualResetEvent(false));
|
|
|
|
ForwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
|
|
ForwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
|
|
ForwardedPort.Session = _sessionMock.Object;
|
|
ForwardedPort.Start();
|
|
ForwardedPort.Stop();
|
|
}
|
|
|
|
protected virtual void Act()
|
|
{
|
|
ForwardedPort.Dispose();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void IsStartedShouldReturnFalse()
|
|
{
|
|
Assert.IsFalse(ForwardedPort.IsStarted);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ForwardedPortShouldIgnoreChannelOpenMessagesWhenDisposed()
|
|
{
|
|
var channelNumberDisposed = (uint)new Random().Next(1001, int.MaxValue);
|
|
var initialWindowSizeDisposed = (uint)new Random().Next(0, int.MaxValue);
|
|
var maximumPacketSizeDisposed = (uint)new Random().Next(0, int.MaxValue);
|
|
var originatorAddressDisposed = new Random().Next().ToString(CultureInfo.InvariantCulture);
|
|
var originatorPortDisposed = (uint)new Random().Next(0, int.MaxValue);
|
|
var channelMock = new Mock<IChannelForwardedTcpip>(MockBehavior.Strict);
|
|
|
|
_sessionMock.Setup(
|
|
p =>
|
|
p.CreateChannelForwardedTcpip(channelNumberDisposed, initialWindowSizeDisposed, maximumPacketSizeDisposed)).Returns(channelMock.Object);
|
|
_sessionMock.Setup(
|
|
p =>
|
|
p.SendMessage(new ChannelOpenFailureMessage(channelNumberDisposed, string.Empty,
|
|
ChannelOpenFailureMessage.AdministrativelyProhibited)));
|
|
|
|
_sessionMock.Raise(p => p.ChannelOpenReceived += null, new ChannelOpenMessage(channelNumberDisposed, initialWindowSizeDisposed, maximumPacketSizeDisposed, new ForwardedTcpipChannelInfo(ForwardedPort.BoundHost, ForwardedPort.BoundPort, originatorAddressDisposed, originatorPortDisposed)));
|
|
|
|
_sessionMock.Verify(p => p.CreateChannelForwardedTcpip(channelNumberDisposed, initialWindowSizeDisposed, maximumPacketSizeDisposed), Times.Never);
|
|
_sessionMock.Verify(p => p.SendMessage(It.Is<ChannelOpenFailureMessage>(c => c.LocalChannelNumber == channelNumberDisposed && c.ReasonCode == ChannelOpenFailureMessage.AdministrativelyProhibited && c.Description == string.Empty && c.Language == null)), Times.Never);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ClosingShouldHaveFiredOnce()
|
|
{
|
|
Assert.HasCount(1, _closingRegister);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ExceptionShouldNotHaveFired()
|
|
{
|
|
Assert.IsEmpty(_exceptionRegister);
|
|
}
|
|
}
|
|
}
|