mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-09-10 17:25:51 +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>
100 lines
3.0 KiB
C#
100 lines
3.0 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
using Moq;
|
|
|
|
using Renci.SshNet.Channels;
|
|
using Renci.SshNet.Common;
|
|
using Renci.SshNet.Tests.Common;
|
|
|
|
namespace Renci.SshNet.Tests.Classes
|
|
{
|
|
[TestClass]
|
|
public class SshCommandTest_Dispose : TestBase
|
|
{
|
|
private Mock<ISession> _sessionMock;
|
|
private Mock<IChannelSession> _channelSessionMock;
|
|
private string _commandText;
|
|
private Encoding _encoding;
|
|
private SshCommand _sshCommand;
|
|
private Stream _outputStream;
|
|
private Stream _extendedOutputStream;
|
|
|
|
protected override void OnInit()
|
|
{
|
|
base.OnInit();
|
|
|
|
Arrange();
|
|
Act();
|
|
}
|
|
|
|
private void Arrange()
|
|
{
|
|
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
|
|
_sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
|
|
_commandText = new Random().Next().ToString(CultureInfo.InvariantCulture);
|
|
_encoding = Encoding.UTF8;
|
|
_channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
|
|
|
|
var seq = new MockSequence();
|
|
|
|
_sessionMock.InSequence(seq).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
|
|
_channelSessionMock.InSequence(seq).Setup(p => p.Open());
|
|
_channelSessionMock.InSequence(seq).Setup(p => p.SendExecRequest(_commandText)).Returns(true);
|
|
_channelSessionMock.InSequence(seq).Setup(p => p.Dispose());
|
|
|
|
_sshCommand = new SshCommand(_sessionMock.Object, _commandText, _encoding);
|
|
_sshCommand.BeginExecute();
|
|
|
|
_outputStream = _sshCommand.OutputStream;
|
|
_extendedOutputStream = _sshCommand.ExtendedOutputStream;
|
|
}
|
|
|
|
private void Act()
|
|
{
|
|
_sshCommand.Dispose();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ChannelSessionShouldBeDisposedOnce()
|
|
{
|
|
_channelSessionMock.Verify(p => p.Dispose(), Times.Once);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void OutputStreamShouldHaveBeenDisposed()
|
|
{
|
|
Assert.AreEqual(-1, _outputStream.ReadByte());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ExtendedOutputStreamShouldHaveBeenDisposed()
|
|
{
|
|
Assert.AreEqual(-1, _extendedOutputStream.ReadByte());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void RaisingDisconnectedOnSessionShouldDoNothing()
|
|
{
|
|
_sessionMock.Raise(s => s.Disconnected += null, new EventArgs());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void RaisingErrorOccurredOnSessionShouldDoNothing()
|
|
{
|
|
_sessionMock.Raise(s => s.ErrorOccured += null, new ExceptionEventArgs(new Exception()));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void InvokingDisposeAgainShouldNotRaiseAnException()
|
|
{
|
|
_sshCommand.Dispose();
|
|
}
|
|
}
|
|
}
|