Files
ssh.net/test/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendShellRequestThrowsException.cs
T
Rob Hague 06af2ec6c0 Fix a few issues with ShellStream (#1322)
* Fix a few issues with ShellStream

The main change is to replace the Queue<byte> with a byte[] and a couple of
variables which index the start and end of the data. The remainder is mainly
slightly more careful locking semantics.

It also implements Expect(string) separately so that it can work on the bytes
and skip a lot of encoding work (this is where I wish ShellStream derived from
StreamReader).

One possibly contentious point: in fixing the Write behaviour I chose to
remove the "outgoing" buffer and immediately send the data across the channel.
Write(string) and WriteLine(string) were already doing this, and I felt it
was better to change Write(byte[]) to match rather than changing the string
methods.

* Integrate expectSize (as "windowSize" parameter)

* Rename "windowSize" to "lookback"

---------

Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
2024-02-16 20:11:22 +01:00

124 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Channels;
using Renci.SshNet.Common;
namespace Renci.SshNet.Tests.Classes
{
[TestClass]
public class ServiceFactoryTest_CreateShellStream_SendShellRequestThrowsException
{
private Mock<ISession> _sessionMock;
private Mock<IConnectionInfo> _connectionInfoMock;
private Mock<IChannelSession> _channelSessionMock;
private ServiceFactory _serviceFactory;
private string _terminalName;
private uint _columns;
private uint _rows;
private uint _width;
private uint _height;
private IDictionary<TerminalModes, uint> _terminalModeValues;
private int _bufferSize;
private SshException _sendShellRequestException;
private SshException _actualException;
private void SetupData()
{
var random = new Random();
_terminalName = random.Next().ToString();
_columns = (uint) random.Next();
_rows = (uint) random.Next();
_width = (uint) random.Next();
_height = (uint) random.Next();
_terminalModeValues = new Dictionary<TerminalModes, uint>();
_bufferSize = random.Next();
_sendShellRequestException = new SshException();
_actualException = null;
}
private void CreateMocks()
{
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
_connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
_channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
}
private void SetupMocks()
{
var sequence = new MockSequence();
_sessionMock.InSequence(sequence)
.Setup(p => p.ConnectionInfo)
.Returns(_connectionInfoMock.Object);
_connectionInfoMock.InSequence(sequence)
.Setup(p => p.Encoding)
.Returns(new UTF8Encoding());
_sessionMock.InSequence(sequence)
.Setup(p => p.CreateChannelSession())
.Returns(_channelSessionMock.Object);
_channelSessionMock.InSequence(sequence)
.Setup(p => p.Open());
_channelSessionMock.InSequence(sequence)
.Setup(p => p.SendPseudoTerminalRequest(_terminalName, _columns, _rows, _width, _height, _terminalModeValues))
.Returns(true);
_channelSessionMock.InSequence(sequence)
.Setup(p => p.SendShellRequest())
.Throws(_sendShellRequestException);
_channelSessionMock.InSequence(sequence)
.Setup(p => p.Dispose());
}
private void Arrange()
{
SetupData();
CreateMocks();
SetupMocks();
_serviceFactory = new ServiceFactory();
}
[TestInitialize]
public void Initialize()
{
Arrange();
Act();
}
private void Act()
{
try
{
_serviceFactory.CreateShellStream(_sessionMock.Object,
_terminalName,
_columns,
_rows,
_width,
_height,
_terminalModeValues,
_bufferSize);
Assert.Fail();
}
catch (SshException ex)
{
_actualException = ex;
}
}
[TestMethod]
public void CreateShellStreamShouldRethrowExceptionThrownBySendShellRequestOnChannelSession()
{
Assert.IsNotNull(_actualException);
Assert.AreSame(_sendShellRequestException, _actualException);
}
[TestMethod]
public void DisposeOnChannelSessionShouldHaveBeenInvokedOnce()
{
_channelSessionMock.Verify(p => p.Dispose(), Times.Once);
}
}
}