mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-09-10 09:15:47 +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>
140 lines
5.3 KiB
C#
140 lines
5.3 KiB
C#
using System;
|
|
using System.Globalization;
|
|
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.Messages.Connection;
|
|
using Renci.SshNet.Tests.Common;
|
|
|
|
namespace Renci.SshNet.Tests.Classes
|
|
{
|
|
[TestClass]
|
|
public class SshCommandTest_EndExecute_ChannelOpen : TestBase
|
|
{
|
|
private Mock<ISession> _sessionMock;
|
|
private Mock<IChannelSession> _channelSessionMock;
|
|
private string _commandText;
|
|
private Encoding _encoding;
|
|
private SshCommand _sshCommand;
|
|
private IAsyncResult _asyncResult;
|
|
private string _actual;
|
|
private string _dataA;
|
|
private string _dataB;
|
|
private string _extendedDataA;
|
|
private string _extendedDataB;
|
|
private int _expectedExitStatus;
|
|
|
|
protected override void OnInit()
|
|
{
|
|
base.OnInit();
|
|
|
|
Arrange();
|
|
Act();
|
|
}
|
|
|
|
private void Arrange()
|
|
{
|
|
var random = new Random();
|
|
|
|
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
|
|
_sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
|
|
_channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
|
|
_commandText = random.Next().ToString(CultureInfo.InvariantCulture);
|
|
_encoding = Encoding.UTF8;
|
|
_expectedExitStatus = random.Next();
|
|
_dataA = random.Next().ToString(CultureInfo.InvariantCulture);
|
|
_dataB = random.Next().ToString(CultureInfo.InvariantCulture);
|
|
_extendedDataA = random.Next().ToString(CultureInfo.InvariantCulture);
|
|
_extendedDataB = random.Next().ToString(CultureInfo.InvariantCulture);
|
|
_asyncResult = null;
|
|
|
|
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);
|
|
_asyncResult = _sshCommand.BeginExecute();
|
|
|
|
_channelSessionMock.Raise(c => c.DataReceived += null,
|
|
new ChannelDataEventArgs(0, _encoding.GetBytes(_dataA)));
|
|
_channelSessionMock.Raise(c => c.ExtendedDataReceived += null,
|
|
new ChannelExtendedDataEventArgs(0, _encoding.GetBytes(_extendedDataA), 0));
|
|
_channelSessionMock.Raise(c => c.DataReceived += null,
|
|
new ChannelDataEventArgs(0, _encoding.GetBytes(_dataB)));
|
|
_channelSessionMock.Raise(c => c.ExtendedDataReceived += null,
|
|
new ChannelExtendedDataEventArgs(0, _encoding.GetBytes(_extendedDataB), 0));
|
|
_channelSessionMock.Raise(c => c.RequestReceived += null,
|
|
new ChannelRequestEventArgs(new ExitStatusRequestInfo((uint)_expectedExitStatus)));
|
|
_channelSessionMock.Raise(c => c.Closed += null,
|
|
new ChannelEventArgs(5));
|
|
}
|
|
|
|
private void Act()
|
|
{
|
|
_actual = _sshCommand.EndExecute(_asyncResult);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void EndExecuteShouldReturnAllDataReceivedInSpecifiedEncoding()
|
|
{
|
|
Assert.AreEqual(string.Concat(_dataA, _dataB), _actual);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void EndExecuteShouldNotThrowWhenInvokedAgainWithSameAsyncResult()
|
|
{
|
|
Assert.AreEqual(_sshCommand.Result, _sshCommand.EndExecute(_asyncResult));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ErrorShouldReturnZeroLengthString()
|
|
{
|
|
Assert.AreEqual(string.Empty, _sshCommand.Error);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ExitStatusShouldReturnExitStatusFromExitStatusRequestInfo()
|
|
{
|
|
Assert.AreEqual(_expectedExitStatus, _sshCommand.ExitStatus);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ExtendedOutputStreamShouldContainAllExtendedDataReceived()
|
|
{
|
|
var extendedDataABytes = _encoding.GetBytes(_extendedDataA);
|
|
var extendedDataBBytes = _encoding.GetBytes(_extendedDataB);
|
|
|
|
var extendedOutputStream = _sshCommand.ExtendedOutputStream;
|
|
Assert.AreEqual(extendedDataABytes.Length + extendedDataBBytes.Length, extendedOutputStream.Length);
|
|
|
|
var buffer = new byte[extendedOutputStream.Length];
|
|
var bytesRead = extendedOutputStream.Read(buffer, 0, buffer.Length);
|
|
|
|
Assert.AreEqual(buffer.Length, bytesRead);
|
|
Assert.AreEqual(string.Concat(_extendedDataA, _extendedDataB), _encoding.GetString(buffer));
|
|
Assert.AreEqual(0, extendedOutputStream.Length);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void OutputStreamShouldBeEmpty()
|
|
{
|
|
Assert.AreEqual(0, _sshCommand.OutputStream.Length);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ResultShouldReturnAllDataReceived()
|
|
{
|
|
Assert.AreEqual(string.Concat(_dataA, _dataB), _sshCommand.Result);
|
|
}
|
|
}
|
|
}
|