Files
ssh.net/test/Renci.SshNet.Tests/Classes/ConnectionInfoTest_Authenticate_Failure.cs
Bart Vries d40bc43ac1 Refactor logging to allow a loggerfactory per session (#1673)
* Refactor logging to allow a loggerfactory per session specified in the ConnectionInfo.

This commit introduces an `ILoggerFactory` to various classes, replacing the static logger factory with an instance-based approach for more flexible and session-specific logging. These changes improve the logging framework's flexibility and maintainability and allow unit testing of logging.

* Improvements bases on feedback. Fixed tests. Added documentation.

* Update src/Renci.SshNet/ConnectionInfo.cs

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2025-07-26 21:24:48 +02:00

83 lines
2.9 KiB
C#

using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Tests.Properties;
namespace Renci.SshNet.Tests.Classes
{
[TestClass]
public class ConnectionInfoTest_Authenticate_Failure
{
private Mock<IServiceFactory> _serviceFactoryMock;
private Mock<IClientAuthentication> _clientAuthenticationMock;
private Mock<ISession> _sessionMock;
private ConnectionInfo _connectionInfo;
private SshAuthenticationException _authenticationException;
private SshAuthenticationException _actualException;
[TestInitialize]
public void Init()
{
Arrange();
Act();
}
protected void Arrange()
{
_serviceFactoryMock = new Mock<IServiceFactory>(MockBehavior.Strict);
_clientAuthenticationMock = new Mock<IClientAuthentication>(MockBehavior.Strict);
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
_sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
_connectionInfo = new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None,
Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
_authenticationException = new SshAuthenticationException();
_serviceFactoryMock.Setup(p => p.CreateClientAuthentication()).Returns(_clientAuthenticationMock.Object);
_clientAuthenticationMock.Setup(p => p.Authenticate(_connectionInfo, _sessionMock.Object))
.Throws(_authenticationException);
}
protected void Act()
{
try
{
_connectionInfo.Authenticate(_sessionMock.Object, _serviceFactoryMock.Object);
}
catch (SshAuthenticationException ex)
{
_actualException = ex;
}
}
[TestMethod]
public void AuthenticateShouldHaveThrownSshAuthenticationException()
{
Assert.IsNotNull(_actualException);
Assert.AreSame(_authenticationException, _actualException);
}
[TestMethod]
public void IsAuthenticatedShouldReturnFalse()
{
Assert.IsFalse(_connectionInfo.IsAuthenticated);
}
[TestMethod]
public void CreateClientAuthenticationOnServiceFactoryShouldBeInvokedOnce()
{
_serviceFactoryMock.Verify(p => p.CreateClientAuthentication(), Times.Once);
}
[TestMethod]
public void AuthenticateOnClientAuthenticationShouldBeInvokedOnce()
{
_clientAuthenticationMock.Verify(p => p.Authenticate(_connectionInfo, _sessionMock.Object), Times.Once);
}
}
}