forked from sshnet/SSH.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass buffer size to ShellStream ctor.
Fixes issue sshnet#303. Added tests for issue sshnet#303 and PR sshnet#211.
- Loading branch information
Showing
19 changed files
with
1,822 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
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 | ||
{ | ||
private Mock<ISession> _sessionMock; | ||
private Mock<IConnectionInfo> _connectionInfoMock; | ||
private Mock<IChannelSession> _channelSessionMock; | ||
private MockSequence _mockSequence; | ||
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 ShellStream _shellStream; | ||
|
||
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(); | ||
} | ||
|
||
private void CreateMocks() | ||
{ | ||
_sessionMock = new Mock<ISession>(MockBehavior.Strict); | ||
_connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict); | ||
_channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict); | ||
} | ||
|
||
private void SetupMocks() | ||
{ | ||
_mockSequence = new MockSequence(); | ||
|
||
_sessionMock.InSequence(_mockSequence) | ||
.Setup(p => p.ConnectionInfo) | ||
.Returns(_connectionInfoMock.Object); | ||
_connectionInfoMock.InSequence(_mockSequence) | ||
.Setup(p => p.Encoding) | ||
.Returns(new UTF8Encoding()); | ||
_sessionMock.InSequence(_mockSequence) | ||
.Setup(p => p.CreateChannelSession()) | ||
.Returns(_channelSessionMock.Object); | ||
_channelSessionMock.InSequence(_mockSequence) | ||
.Setup(p => p.Open()); | ||
_channelSessionMock.InSequence(_mockSequence) | ||
.Setup(p => p.SendPseudoTerminalRequest(_terminalName, | ||
_columns, | ||
_rows, | ||
_width, | ||
_height, | ||
_terminalModeValues)) | ||
.Returns(true); | ||
_channelSessionMock.InSequence(_mockSequence) | ||
.Setup(p => p.SendShellRequest()) | ||
.Returns(true); | ||
} | ||
|
||
private void Arrange() | ||
{ | ||
SetupData(); | ||
CreateMocks(); | ||
SetupMocks(); | ||
|
||
_serviceFactory = new ServiceFactory(); | ||
} | ||
|
||
[TestInitialize] | ||
public void Initialize() | ||
{ | ||
Arrange(); | ||
Act(); | ||
} | ||
|
||
private void Act() | ||
{ | ||
_shellStream = _serviceFactory.CreateShellStream(_sessionMock.Object, | ||
_terminalName, | ||
_columns, | ||
_rows, | ||
_width, | ||
_height, | ||
_terminalModeValues, | ||
_bufferSize); | ||
} | ||
|
||
[TestMethod] | ||
public void CreateShellStreamShouldNotReturnNull() | ||
{ | ||
Assert.IsNotNull(_shellStream); | ||
} | ||
|
||
[TestMethod] | ||
public void BufferSizeOfShellStreamShouldBeValuePassedToCreateShellStream() | ||
{ | ||
Assert.AreEqual(_bufferSize, _shellStream.BufferSize); | ||
} | ||
|
||
[TestMethod] | ||
public void SendPseudoTerminalRequestShouldHaveBeenInvokedOnce() | ||
{ | ||
_channelSessionMock.Verify(p => p.SendPseudoTerminalRequest(_terminalName, | ||
_columns, | ||
_rows, | ||
_width, | ||
_height, | ||
_terminalModeValues), | ||
Times.Once); | ||
} | ||
|
||
[TestMethod] | ||
public void SendShellRequestShouldHaveBeenInvokedOnce() | ||
{ | ||
_channelSessionMock.Verify(p => p.SendShellRequest(), Times.Once); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
...et.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteLessBytesThanBufferSize.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using Renci.SshNet.Abstractions; | ||
using Renci.SshNet.Channels; | ||
using Renci.SshNet.Common; | ||
|
||
namespace Renci.SshNet.Tests.Classes | ||
{ | ||
[TestClass] | ||
public class ShellStreamTest_Write_WriteBufferEmptyAndWriteLessBytesThanBufferSize | ||
{ | ||
private Mock<ISession> _sessionMock; | ||
private Mock<IConnectionInfo> _connectionInfoMock; | ||
private Mock<IChannelSession> _channelSessionMock; | ||
private string _terminalName; | ||
private uint _widthColumns; | ||
private uint _heightRows; | ||
private uint _widthPixels; | ||
private uint _heightPixels; | ||
private Dictionary<TerminalModes, uint> _terminalModes; | ||
private ShellStream _shellStream; | ||
private int _bufferSize; | ||
|
||
private byte[] _data; | ||
private int _offset; | ||
private int _count; | ||
private MockSequence _mockSequence; | ||
|
||
[TestInitialize] | ||
public void Initialize() | ||
{ | ||
Arrange(); | ||
Act(); | ||
} | ||
|
||
private void SetupData() | ||
{ | ||
var random = new Random(); | ||
|
||
_terminalName = random.Next().ToString(); | ||
_widthColumns = (uint) random.Next(); | ||
_heightRows = (uint) random.Next(); | ||
_widthPixels = (uint) random.Next(); | ||
_heightPixels = (uint) random.Next(); | ||
_terminalModes = new Dictionary<TerminalModes, uint>(); | ||
_bufferSize = random.Next(100, 1000); | ||
|
||
_data = CryptoAbstraction.GenerateRandom(_bufferSize - 10); | ||
_offset = random.Next(1, 5); | ||
_count = _data.Length - _offset - random.Next(1, 10); | ||
} | ||
|
||
private void CreateMocks() | ||
{ | ||
_sessionMock = new Mock<ISession>(MockBehavior.Strict); | ||
_connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict); | ||
_channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict); | ||
} | ||
|
||
private void SetupMocks() | ||
{ | ||
_mockSequence = new MockSequence(); | ||
|
||
_sessionMock.InSequence(_mockSequence) | ||
.Setup(p => p.ConnectionInfo) | ||
.Returns(_connectionInfoMock.Object); | ||
_connectionInfoMock.InSequence(_mockSequence) | ||
.Setup(p => p.Encoding) | ||
.Returns(new UTF8Encoding()); | ||
_sessionMock.InSequence(_mockSequence) | ||
.Setup(p => p.CreateChannelSession()) | ||
.Returns(_channelSessionMock.Object); | ||
_channelSessionMock.InSequence(_mockSequence) | ||
.Setup(p => p.Open()); | ||
_channelSessionMock.InSequence(_mockSequence) | ||
.Setup(p => p.SendPseudoTerminalRequest(_terminalName, | ||
_widthColumns, | ||
_heightRows, | ||
_widthPixels, | ||
_heightPixels, | ||
_terminalModes)) | ||
.Returns(true); | ||
_channelSessionMock.InSequence(_mockSequence) | ||
.Setup(p => p.SendShellRequest()) | ||
.Returns(true); | ||
} | ||
|
||
private void Arrange() | ||
{ | ||
SetupData(); | ||
CreateMocks(); | ||
SetupMocks(); | ||
|
||
_shellStream = new ShellStream(_sessionMock.Object, | ||
_terminalName, | ||
_widthColumns, | ||
_heightRows, | ||
_widthPixels, | ||
_heightPixels, | ||
_terminalModes, | ||
_bufferSize); | ||
} | ||
|
||
private void Act() | ||
{ | ||
_shellStream.Write(_data, _offset, _count); | ||
} | ||
|
||
[TestMethod] | ||
public void NoDataShouldBeSentToServer() | ||
{ | ||
_channelSessionMock.Verify(p => p.SendData(It.IsAny<byte[]>()), Times.Never); | ||
} | ||
|
||
[TestMethod] | ||
public void FlushShouldSendWrittenBytesToServer() | ||
{ | ||
byte[] bytesSent = null; | ||
|
||
_channelSessionMock.InSequence(_mockSequence) | ||
.Setup(p => p.SendData(It.IsAny<byte[]>())) | ||
.Callback<byte[]>(data => bytesSent = data); | ||
|
||
_shellStream.Flush(); | ||
|
||
Assert.IsNotNull(bytesSent); | ||
Assert.IsTrue(_data.Take(_offset, _count).IsEqualTo(bytesSent)); | ||
|
||
_channelSessionMock.Verify(p => p.SendData(It.IsAny<byte[]>()), Times.Once); | ||
} | ||
} | ||
} |
Oops, something went wrong.