Files
ssh.net/test/Renci.SshNet.Tests/Classes/ClientAuthenticationTestBase.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

89 lines
3.2 KiB
C#

using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Tests.Common;
namespace Renci.SshNet.Tests.Classes
{
[TestClass]
public abstract class ClientAuthenticationTestBase : TestBase
{
internal Mock<IConnectionInfoInternal> ConnectionInfoMock { get; private set; }
internal Mock<ISession> SessionMock { get; private set; }
internal Mock<IAuthenticationMethod> NoneAuthenticationMethodMock { get; private set; }
internal Mock<IAuthenticationMethod> PasswordAuthenticationMethodMock { get; private set; }
internal Mock<IAuthenticationMethod> PublicKeyAuthenticationMethodMock { get; private set; }
internal Mock<IAuthenticationMethod> KeyboardInteractiveAuthenticationMethodMock { get; private set; }
protected abstract void SetupData();
protected void CreateMocks()
{
ConnectionInfoMock = new Mock<IConnectionInfoInternal>(MockBehavior.Strict);
SessionMock = new Mock<ISession>(MockBehavior.Strict);
SessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
NoneAuthenticationMethodMock = new Mock<IAuthenticationMethod>(MockBehavior.Strict);
PasswordAuthenticationMethodMock = new Mock<IAuthenticationMethod>(MockBehavior.Strict);
PublicKeyAuthenticationMethodMock = new Mock<IAuthenticationMethod>(MockBehavior.Strict);
KeyboardInteractiveAuthenticationMethodMock = new Mock<IAuthenticationMethod>(MockBehavior.Strict);
}
protected abstract void SetupMocks();
protected virtual void Arrange()
{
SetupData();
CreateMocks();
SetupMocks();
}
protected abstract void Act();
protected sealed override void OnInit()
{
base.OnInit();
Arrange();
Act();
}
[TestMethod]
public void RegisterMessageWithUserAuthFailureShouldBeInvokedOnce()
{
SessionMock.Verify(p => p.RegisterMessage("SSH_MSG_USERAUTH_FAILURE"), Times.Once);
}
[TestMethod]
public void RegisterMessageWithUserAuthSuccessShouldBeInvokedOnce()
{
SessionMock.Verify(p => p.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS"), Times.Once);
}
[TestMethod]
public void RegisterMessageWithUserAuthBannerShouldBeInvokedOnce()
{
SessionMock.Verify(p => p.RegisterMessage("SSH_MSG_USERAUTH_BANNER"), Times.Once);
}
[TestMethod]
public void UnRegisterMessageWithUserAuthFailureShouldBeInvokedOnce()
{
SessionMock.Verify(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"), Times.Once);
}
[TestMethod]
public void UnRegisterMessageWithUserAuthSuccessShouldBeInvokedOnce()
{
SessionMock.Verify(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"), Times.Once);
}
[TestMethod]
public void UnRegisterMessageWithUserAuthBannerShouldBeInvokedOnce()
{
SessionMock.Verify(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"), Times.Once);
}
}
}