mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-09-11 01:36:39 +00:00
d40bc43ac1
* 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>
146 lines
4.8 KiB
C#
146 lines
4.8 KiB
C#
using System;
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
using Moq;
|
|
|
|
using Renci.SshNet.Connection;
|
|
|
|
namespace Renci.SshNet.Tests.Classes
|
|
{
|
|
[TestClass]
|
|
public class ServiceFactoryTest_CreateConnector
|
|
{
|
|
private ServiceFactory _serviceFactory;
|
|
private Mock<IConnectionInfo> _connectionInfoMock;
|
|
private Mock<ISocketFactory> _socketFactoryMock;
|
|
|
|
[TestInitialize]
|
|
public void Setup()
|
|
{
|
|
_serviceFactory = new ServiceFactory();
|
|
_connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
|
|
_connectionInfoMock.Setup(p => p.LoggerFactory).Returns(NullLoggerFactory.Instance);
|
|
_socketFactoryMock = new Mock<ISocketFactory>(MockBehavior.Strict);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ConnectionInfoIsNull()
|
|
{
|
|
const IConnectionInfo connectionInfo = null;
|
|
|
|
try
|
|
{
|
|
_serviceFactory.CreateConnector(connectionInfo, _socketFactoryMock.Object);
|
|
Assert.Fail();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
Assert.IsNull(ex.InnerException);
|
|
Assert.AreEqual("connectionInfo", ex.ParamName);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void SocketFactoryIsNull()
|
|
{
|
|
const ISocketFactory socketFactory = null;
|
|
|
|
try
|
|
{
|
|
_serviceFactory.CreateConnector(_connectionInfoMock.Object, socketFactory);
|
|
Assert.Fail();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
Assert.IsNull(ex.InnerException);
|
|
Assert.AreEqual("socketFactory", ex.ParamName);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ProxyType_Http()
|
|
{
|
|
_connectionInfoMock.Setup(p => p.ProxyType).Returns(ProxyTypes.Http);
|
|
|
|
var actual = _serviceFactory.CreateConnector(_connectionInfoMock.Object, _socketFactoryMock.Object);
|
|
|
|
Assert.IsNotNull(actual);
|
|
Assert.AreEqual(typeof(HttpConnector), actual.GetType());
|
|
|
|
var httpConnector = (HttpConnector)actual;
|
|
Assert.AreSame(_socketFactoryMock.Object, httpConnector.SocketFactory);
|
|
|
|
_connectionInfoMock.Verify(p => p.ProxyType, Times.Once);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ProxyType_None()
|
|
{
|
|
_connectionInfoMock.Setup(p => p.ProxyType).Returns(ProxyTypes.None);
|
|
|
|
var actual = _serviceFactory.CreateConnector(_connectionInfoMock.Object, _socketFactoryMock.Object);
|
|
|
|
Assert.IsNotNull(actual);
|
|
Assert.AreEqual(typeof(DirectConnector), actual.GetType());
|
|
|
|
var directConnector = (DirectConnector)actual;
|
|
Assert.AreSame(_socketFactoryMock.Object, directConnector.SocketFactory);
|
|
|
|
_connectionInfoMock.Verify(p => p.ProxyType, Times.Once);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ProxyType_Socks4()
|
|
{
|
|
_connectionInfoMock.Setup(p => p.ProxyType).Returns(ProxyTypes.Socks4);
|
|
|
|
var actual = _serviceFactory.CreateConnector(_connectionInfoMock.Object, _socketFactoryMock.Object);
|
|
|
|
Assert.IsNotNull(actual);
|
|
Assert.AreEqual(typeof(Socks4Connector), actual.GetType());
|
|
|
|
var socks4Connector = (Socks4Connector)actual;
|
|
Assert.AreSame(_socketFactoryMock.Object, socks4Connector.SocketFactory);
|
|
|
|
_connectionInfoMock.Verify(p => p.ProxyType, Times.Once);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ProxyType_Socks5()
|
|
{
|
|
_connectionInfoMock.Setup(p => p.ProxyType).Returns(ProxyTypes.Socks5);
|
|
|
|
var actual = _serviceFactory.CreateConnector(_connectionInfoMock.Object, _socketFactoryMock.Object);
|
|
|
|
Assert.IsNotNull(actual);
|
|
Assert.AreEqual(typeof(Socks5Connector), actual.GetType());
|
|
|
|
var socks5Connector = (Socks5Connector)actual;
|
|
Assert.AreSame(_socketFactoryMock.Object, socks5Connector.SocketFactory);
|
|
|
|
_connectionInfoMock.Verify(p => p.ProxyType, Times.Once);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ProxyType_Undefined()
|
|
{
|
|
_connectionInfoMock.Setup(p => p.ProxyType).Returns((ProxyTypes)666);
|
|
|
|
try
|
|
{
|
|
_serviceFactory.CreateConnector(_connectionInfoMock.Object, _socketFactoryMock.Object);
|
|
Assert.Fail();
|
|
}
|
|
catch (NotSupportedException ex)
|
|
{
|
|
Assert.IsNull(ex.InnerException);
|
|
Assert.AreEqual("ProxyTypes '666' is not supported.", ex.Message);
|
|
}
|
|
|
|
_connectionInfoMock.Verify(p => p.ProxyType, Times.Exactly(2));
|
|
}
|
|
}
|
|
}
|