Files
ssh.net/test/Renci.SshNet.Tests/Classes/ForwardedPortDynamicTest_Dispose_PortStopped.cs
mus65 4e02502bdf Add .NET 10 target and make use of C#14 extension members (#1672)
* 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 commit 5a59ac9aa8.

* fix new warnings with MSTest 4 + .NET 10

* use same Randomizer instance

* disable CA2000

* reduce CA1849 suppressions

and disable duplicate S6966

* disable preview analyzers

reverts 6c3c06d95a
2025-11-15 12:21:49 +01:00

103 lines
3.1 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_Dispose_PortStopped
{
private Mock<ISession> _sessionMock;
private Mock<IConnectionInfo> _connectionInfoMock;
private ForwardedPortDynamic _forwardedPort;
private IList<EventArgs> _closingRegister;
private IList<ExceptionEventArgs> _exceptionRegister;
private IPEndPoint _endpoint;
[TestInitialize]
public void Setup()
{
Arrange();
Act();
}
[TestCleanup]
public void Cleanup()
{
_forwardedPort?.Dispose();
_forwardedPort = null;
}
protected void Arrange()
{
_closingRegister = new List<EventArgs>();
_exceptionRegister = new List<ExceptionEventArgs>();
_endpoint = new IPEndPoint(IPAddress.Loopback, 8122);
_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);
_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;
_forwardedPort.Start();
_forwardedPort.Stop();
_closingRegister.Clear();
}
protected void Act()
{
_forwardedPort.Dispose();
}
[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);
}
}
}