mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-09-10 09:15:47 +00:00
933613e31c
* Update to MSTest 4 * fix TestMethodForPlatformAttribute replace Execute with ExecuteAsync and fix MSTEST0057 https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0057 (link currently dead) * fix compilation error * fix MSTEST0037 https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0037 * fix MSTEST0052 https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0052 * fix MSTEST0045 Fixing this properly would require the tests to respect testContext.CancellationToken. I'm not sure this is worth fixing or how to even do it for the sync methods. https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0045 * fix MSTEST0001 I assume that parallelization would break a lot of stuff. https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0001 * Workaround for new Sonar warnings because of MSTest4 * revert analyzer fixes in OrderedDictionaryTest * use custom sync console logger for MSTest to work around https://github.com/microsoft/testfx/issues/6457 * remove redundant args --------- Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com> Co-authored-by: Rob Hague <rob.hague00@gmail.com>
118 lines
3.5 KiB
C#
118 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
using Moq;
|
|
|
|
using Renci.SshNet.Common;
|
|
|
|
namespace Renci.SshNet.Tests.Classes
|
|
{
|
|
[TestClass]
|
|
public class ForwardedPortDynamicTest_Start_SessionNotConnected
|
|
{
|
|
private Mock<ISession> _sessionMock;
|
|
private Mock<IConnectionInfo> _connectionInfoMock;
|
|
private ForwardedPortDynamic _forwardedPort;
|
|
private IList<EventArgs> _closingRegister;
|
|
private IList<ExceptionEventArgs> _exceptionRegister;
|
|
private SshConnectionException _actualException;
|
|
private IPEndPoint _endpoint;
|
|
|
|
[TestInitialize]
|
|
public void Setup()
|
|
{
|
|
Arrange();
|
|
Act();
|
|
}
|
|
|
|
[TestCleanup]
|
|
public void Cleanup()
|
|
{
|
|
if (_forwardedPort != null)
|
|
{
|
|
_connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(1));
|
|
_forwardedPort.Dispose();
|
|
_forwardedPort = null;
|
|
}
|
|
}
|
|
|
|
protected void Arrange()
|
|
{
|
|
_closingRegister = new List<EventArgs>();
|
|
_exceptionRegister = new List<ExceptionEventArgs>();
|
|
_endpoint = new IPEndPoint(IPAddress.Loopback, 8122);
|
|
|
|
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
|
|
_sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
|
|
_connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
|
|
|
|
_sessionMock.Setup(p => p.IsConnected).Returns(false);
|
|
_sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
|
|
|
|
_forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint)_endpoint.Port);
|
|
_forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
|
|
_forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
|
|
_forwardedPort.Session = _sessionMock.Object;
|
|
}
|
|
|
|
protected void Act()
|
|
{
|
|
try
|
|
{
|
|
_forwardedPort.Start();
|
|
Assert.Fail();
|
|
}
|
|
catch (SshConnectionException ex)
|
|
{
|
|
_actualException = ex;
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void StartShouldThrowSshConnectionException()
|
|
{
|
|
Assert.IsNotNull(_actualException);
|
|
Assert.AreEqual("Client not connected.", _actualException.Message);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void IsStartedShouldReturnFalse()
|
|
{
|
|
Assert.IsFalse(_forwardedPort.IsStarted);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ForwardedPortShouldRejectNewConnections()
|
|
{
|
|
using (var client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
|
|
{
|
|
try
|
|
{
|
|
client.Connect(_endpoint);
|
|
}
|
|
catch (SocketException ex)
|
|
{
|
|
Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode);
|
|
}
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ClosingShouldNotHaveFired()
|
|
{
|
|
Assert.IsEmpty(_closingRegister);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ExceptionShouldNotHaveFired()
|
|
{
|
|
Assert.IsEmpty(_exceptionRegister);
|
|
}
|
|
}
|
|
}
|