diff --git a/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_ChannelOpenThrowsException.cs b/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_ChannelOpenThrowsException.cs index 651f2fbc2..97386393e 100644 --- a/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_ChannelOpenThrowsException.cs +++ b/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_ChannelOpenThrowsException.cs @@ -22,6 +22,7 @@ public class ServiceFactoryTest_CreateShellStream_ChannelOpenThrowsException private uint _height; private IDictionary _terminalModeValues; private int _bufferSize; + private int _expectSize; private SshException _channelOpenException; private SshException _actualException; @@ -30,12 +31,13 @@ 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(); + _columns = (uint)random.Next(); + _rows = (uint)random.Next(); + _width = (uint)random.Next(); + _height = (uint)random.Next(); _terminalModeValues = new Dictionary(); _bufferSize = random.Next(); + _expectSize = random.Next(); _channelOpenException = new SshException(); _actualException = null; @@ -95,7 +97,8 @@ private void Act() _width, _height, _terminalModeValues, - _bufferSize); + _bufferSize, + _expectSize); Assert.Fail(); } catch (SshException ex) diff --git a/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendPseudoTerminalRequestReturnsFalse.cs b/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendPseudoTerminalRequestReturnsFalse.cs index 362351a5f..914083b23 100644 --- a/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendPseudoTerminalRequestReturnsFalse.cs +++ b/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendPseudoTerminalRequestReturnsFalse.cs @@ -22,6 +22,7 @@ public class ServiceFactoryTest_CreateShellStream_SendPseudoTerminalRequestRetur private uint _height; private IDictionary _terminalModeValues; private int _bufferSize; + private int _expectSize; private SshException _actualException; private void SetupData() @@ -35,6 +36,7 @@ private void SetupData() _height = (uint)random.Next(); _terminalModeValues = new Dictionary(); _bufferSize = random.Next(); + _expectSize = random.Next(); _actualException = null; } @@ -94,7 +96,8 @@ private void Act() _width, _height, _terminalModeValues, - _bufferSize); + _bufferSize, + _expectSize); Assert.Fail(); } catch (SshException ex) diff --git a/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendPseudoTerminalRequestThrowsException.cs b/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendPseudoTerminalRequestThrowsException.cs index 67606992a..f2487c807 100644 --- a/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendPseudoTerminalRequestThrowsException.cs +++ b/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendPseudoTerminalRequestThrowsException.cs @@ -22,6 +22,7 @@ public class ServiceFactoryTest_CreateShellStream_SendPseudoTerminalRequestThrow private uint _height; private IDictionary _terminalModeValues; private int _bufferSize; + private int _expectSize; private SshException _sendPseudoTerminalRequestException; private SshException _actualException; @@ -30,12 +31,13 @@ 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(); + _columns = (uint)random.Next(); + _rows = (uint)random.Next(); + _width = (uint)random.Next(); + _height = (uint)random.Next(); _terminalModeValues = new Dictionary(); _bufferSize = random.Next(); + _expectSize = random.Next(); _sendPseudoTerminalRequestException = new SshException(); _actualException = null; @@ -97,7 +99,8 @@ private void Act() _width, _height, _terminalModeValues, - _bufferSize); + _bufferSize, + _expectSize); Assert.Fail(); } catch (SshException ex) diff --git a/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendShellRequestReturnsFalse.cs b/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendShellRequestReturnsFalse.cs index 5a6d43871..247bb6392 100644 --- a/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendShellRequestReturnsFalse.cs +++ b/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendShellRequestReturnsFalse.cs @@ -22,6 +22,7 @@ public class ServiceFactoryTest_CreateShellStream_SendShellRequestReturnsFalse private uint _height; private IDictionary _terminalModeValues; private int _bufferSize; + private int _expectSize; private SshException _actualException; private void SetupData() @@ -29,12 +30,13 @@ 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(); + _columns = (uint)random.Next(); + _rows = (uint)random.Next(); + _width = (uint)random.Next(); + _height = (uint)random.Next(); _terminalModeValues = new Dictionary(); _bufferSize = random.Next(); + _expectSize = random.Next(); _actualException = null; } @@ -97,7 +99,8 @@ private void Act() _width, _height, _terminalModeValues, - _bufferSize); + _bufferSize, + _expectSize); Assert.Fail(); } catch (SshException ex) diff --git a/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendShellRequestThrowsException.cs b/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendShellRequestThrowsException.cs index da9cd6be8..aa0c48bd5 100644 --- a/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendShellRequestThrowsException.cs +++ b/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_SendShellRequestThrowsException.cs @@ -22,6 +22,7 @@ public class ServiceFactoryTest_CreateShellStream_SendShellRequestThrowsExceptio private uint _height; private IDictionary _terminalModeValues; private int _bufferSize; + private int _expectSize; private SshException _sendShellRequestException; private SshException _actualException; @@ -30,12 +31,13 @@ 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(); + _columns = (uint)random.Next(); + _rows = (uint)random.Next(); + _width = (uint)random.Next(); + _height = (uint)random.Next(); _terminalModeValues = new Dictionary(); _bufferSize = random.Next(); + _expectSize = random.Next(); _sendShellRequestException = new SshException(); _actualException = null; } @@ -99,7 +101,8 @@ private void Act() _width, _height, _terminalModeValues, - _bufferSize); + _bufferSize, + _expectSize); Assert.Fail(); } catch (SshException ex) diff --git a/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_Success.cs b/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_Success.cs index dbd8377c4..858be746d 100644 --- a/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_Success.cs +++ b/src/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateShellStream_Success.cs @@ -22,6 +22,7 @@ public class ServiceFactoryTest_CreateShellStream_Success private uint _height; private IDictionary _terminalModeValues; private int _bufferSize; + private int _expectSize; private ShellStream _shellStream; private void SetupData() @@ -29,12 +30,13 @@ 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(); + _columns = (uint)random.Next(); + _rows = (uint)random.Next(); + _width = (uint)random.Next(); + _height = (uint)random.Next(); _terminalModeValues = new Dictionary(); _bufferSize = random.Next(); + _expectSize = random.Next(); } private void CreateMocks() @@ -97,7 +99,8 @@ private void Act() _width, _height, _terminalModeValues, - _bufferSize); + _bufferSize, + _expectSize); } [TestMethod] diff --git a/src/Renci.SshNet.Tests/Classes/ShellStreamTest.cs b/src/Renci.SshNet.Tests/Classes/ShellStreamTest.cs index ecec9d30e..669aef387 100644 --- a/src/Renci.SshNet.Tests/Classes/ShellStreamTest.cs +++ b/src/Renci.SshNet.Tests/Classes/ShellStreamTest.cs @@ -27,6 +27,7 @@ public class ShellStreamTest : TestBase private uint _heightPixels; private Dictionary _terminalModes; private int _bufferSize; + private int _expectSize; private Mock _channelSessionMock; protected override void OnInit() @@ -35,12 +36,13 @@ protected override void OnInit() var random = new Random(); _terminalName = random.Next().ToString(CultureInfo.InvariantCulture); - _widthColumns = (uint) random.Next(); - _heightRows = (uint) random.Next(); + _widthColumns = (uint)random.Next(); + _heightRows = (uint)random.Next(); _widthPixels = (uint)random.Next(); _heightPixels = (uint)random.Next(); _terminalModes = new Dictionary(); _bufferSize = random.Next(100, 500); + _expectSize = random.Next(100, 500); _encoding = Encoding.UTF8; _sessionMock = new Mock(MockBehavior.Strict); @@ -61,15 +63,15 @@ public void ReadLine_MultiByteCharacters() var shellStream = CreateShellStream(); var channelDataPublishThread = new Thread(() => - { - _channelSessionMock.Raise(p => p.DataReceived += null, - new ChannelDataEventArgs(5, _encoding.GetBytes(data1))); - Thread.Sleep(50); - _channelSessionMock.Raise(p => p.DataReceived += null, - new ChannelDataEventArgs(5, _encoding.GetBytes(data2 + "\r\n"))); - _channelSessionMock.Raise(p => p.DataReceived += null, - new ChannelDataEventArgs(5, _encoding.GetBytes(data3 + "\r\n"))); - }); + { + _channelSessionMock.Raise(p => p.DataReceived += null, + new ChannelDataEventArgs(5, _encoding.GetBytes(data1))); + Thread.Sleep(50); + _channelSessionMock.Raise(p => p.DataReceived += null, + new ChannelDataEventArgs(5, _encoding.GetBytes(data2 + "\r\n"))); + _channelSessionMock.Raise(p => p.DataReceived += null, + new ChannelDataEventArgs(5, _encoding.GetBytes(data3 + "\r\n"))); + }); channelDataPublishThread.Start(); Assert.AreEqual(data1 + data2, shellStream.ReadLine()); @@ -120,7 +122,8 @@ private ShellStream CreateShellStream() _widthPixels, _heightPixels, _terminalModes, - _bufferSize); + _bufferSize, + _expectSize); } } } \ No newline at end of file diff --git a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteLessBytesThanBufferSize.cs b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteLessBytesThanBufferSize.cs index 2f4b25921..1547ae028 100644 --- a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteLessBytesThanBufferSize.cs +++ b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteLessBytesThanBufferSize.cs @@ -23,6 +23,7 @@ public class ShellStreamTest_Write_WriteBufferEmptyAndWriteLessBytesThanBufferSi private Dictionary _terminalModes; private ShellStream _shellStream; private int _bufferSize; + private int _expectSize; private byte[] _data; private int _offset; @@ -41,12 +42,13 @@ 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(); + _widthColumns = (uint)random.Next(); + _heightRows = (uint)random.Next(); + _widthPixels = (uint)random.Next(); + _heightPixels = (uint)random.Next(); _terminalModes = new Dictionary(); _bufferSize = random.Next(100, 1000); + _expectSize = random.Next(100, 1000); _data = CryptoAbstraction.GenerateRandom(_bufferSize - 10); _offset = random.Next(1, 5); @@ -101,7 +103,8 @@ private void Arrange() _widthPixels, _heightPixels, _terminalModes, - _bufferSize); + _bufferSize, + _expectSize); } private void Act() diff --git a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteMoreBytesThanBufferSize.cs b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteMoreBytesThanBufferSize.cs index 4f1fa8692..80ec467c1 100644 --- a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteMoreBytesThanBufferSize.cs +++ b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteMoreBytesThanBufferSize.cs @@ -24,6 +24,7 @@ public class ShellStreamTest_Write_WriteBufferEmptyAndWriteMoreBytesThanBufferSi private Dictionary _terminalModes; private ShellStream _shellStream; private int _bufferSize; + private int _expectSize; private byte[] _data; private int _offset; @@ -50,6 +51,7 @@ private void SetupData() _heightPixels = (uint)random.Next(); _terminalModes = new Dictionary(); _bufferSize = random.Next(100, 1000); + _expectSize = random.Next(100, 1000); _data = CryptoAbstraction.GenerateRandom(_bufferSize * 2 + 10); _offset = 0; @@ -111,7 +113,8 @@ private void Arrange() _widthPixels, _heightPixels, _terminalModes, - _bufferSize); + _bufferSize, + _expectSize); } private void Act() diff --git a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteNumberOfBytesEqualToBufferSize.cs b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteNumberOfBytesEqualToBufferSize.cs index 42872913b..41ee1e78f 100644 --- a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteNumberOfBytesEqualToBufferSize.cs +++ b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteNumberOfBytesEqualToBufferSize.cs @@ -23,6 +23,7 @@ public class ShellStreamTest_Write_WriteBufferEmptyAndWriteNumberOfBytesEqualToB private Dictionary _terminalModes; private ShellStream _shellStream; private int _bufferSize; + private int _expectSize; private byte[] _data; private int _offset; @@ -47,6 +48,7 @@ private void SetupData() _heightPixels = (uint)random.Next(); _terminalModes = new Dictionary(); _bufferSize = random.Next(100, 1000); + _expectSize = random.Next(100, 1000); _data = CryptoAbstraction.GenerateRandom(_bufferSize); _offset = 0; @@ -101,7 +103,8 @@ private void Arrange() _widthPixels, _heightPixels, _terminalModes, - _bufferSize); + _bufferSize, + _expectSize); } private void Act() diff --git a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteZeroBytes.cs b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteZeroBytes.cs index 05e63a19f..012bed2d1 100644 --- a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteZeroBytes.cs +++ b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferEmptyAndWriteZeroBytes.cs @@ -23,6 +23,7 @@ public class ShellStreamTest_Write_WriteBufferEmptyAndWriteZeroBytes private Dictionary _terminalModes; private ShellStream _shellStream; private int _bufferSize; + private int _expectSize; private byte[] _data; private int _offset; @@ -47,6 +48,7 @@ private void SetupData() _heightPixels = (uint)random.Next(); _terminalModes = new Dictionary(); _bufferSize = random.Next(100, 1000); + _expectSize = random.Next(100, 1000); _data = new byte[0]; _offset = 0; @@ -101,7 +103,8 @@ private void Arrange() _widthPixels, _heightPixels, _terminalModes, - _bufferSize); + _bufferSize, + _expectSize); } private void Act() diff --git a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferFullAndWriteLessBytesThanBufferSize.cs b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferFullAndWriteLessBytesThanBufferSize.cs index 9fed3f4e2..81fa985aa 100644 --- a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferFullAndWriteLessBytesThanBufferSize.cs +++ b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferFullAndWriteLessBytesThanBufferSize.cs @@ -23,6 +23,7 @@ public class ShellStreamTest_Write_WriteBufferFullAndWriteLessBytesThanBufferSiz private Dictionary _terminalModes; private ShellStream _shellStream; private int _bufferSize; + private int _expectSize; private byte[] _data; private int _offset; @@ -48,6 +49,7 @@ private void SetupData() _heightPixels = (uint)random.Next(); _terminalModes = new Dictionary(); _bufferSize = random.Next(100, 1000); + _expectSize = random.Next(100, 1000); _bufferData = CryptoAbstraction.GenerateRandom(_bufferSize); _data = CryptoAbstraction.GenerateRandom(_bufferSize - 10); @@ -105,7 +107,8 @@ private void Arrange() _widthPixels, _heightPixels, _terminalModes, - _bufferSize); + _bufferSize, + _expectSize); _shellStream.Write(_bufferData, 0, _bufferData.Length); } diff --git a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferFullAndWriteZeroBytes.cs b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferFullAndWriteZeroBytes.cs index f0961ea89..a12bd534d 100644 --- a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferFullAndWriteZeroBytes.cs +++ b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferFullAndWriteZeroBytes.cs @@ -23,6 +23,7 @@ public class ShellStreamTest_Write_WriteBufferFullAndWriteZeroBytes private Dictionary _terminalModes; private ShellStream _shellStream; private int _bufferSize; + private int _expectSize; private byte[] _data; private int _offset; @@ -42,12 +43,13 @@ 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(); + _widthColumns = (uint)random.Next(); + _heightRows = (uint)random.Next(); + _widthPixels = (uint)random.Next(); + _heightPixels = (uint)random.Next(); _terminalModes = new Dictionary(); _bufferSize = random.Next(100, 1000); + _expectSize = random.Next(100, 1000); _bufferData = CryptoAbstraction.GenerateRandom(_bufferSize); _data = new byte[0]; @@ -103,7 +105,8 @@ private void Arrange() _widthPixels, _heightPixels, _terminalModes, - _bufferSize); + _bufferSize, + _expectSize); _shellStream.Write(_bufferData, 0, _bufferData.Length); } diff --git a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferNotEmptyAndWriteLessBytesThanBufferCanContain.cs b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferNotEmptyAndWriteLessBytesThanBufferCanContain.cs index e14be3044..e399f4e21 100644 --- a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferNotEmptyAndWriteLessBytesThanBufferCanContain.cs +++ b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferNotEmptyAndWriteLessBytesThanBufferCanContain.cs @@ -23,6 +23,7 @@ public class ShellStreamTest_Write_WriteBufferNotEmptyAndWriteLessBytesThanBuffe private Dictionary _terminalModes; private ShellStream _shellStream; private int _bufferSize; + private int _expectSize; private byte[] _data; private int _offset; @@ -42,12 +43,13 @@ 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(); + _widthColumns = (uint)random.Next(); + _heightRows = (uint)random.Next(); + _widthPixels = (uint)random.Next(); + _heightPixels = (uint)random.Next(); _terminalModes = new Dictionary(); _bufferSize = random.Next(100, 1000); + _expectSize = random.Next(100, 1000); _bufferData = CryptoAbstraction.GenerateRandom(_bufferSize - 60); _data = CryptoAbstraction.GenerateRandom(_bufferSize + 100); @@ -103,7 +105,8 @@ private void Arrange() _widthPixels, _heightPixels, _terminalModes, - _bufferSize); + _bufferSize, + _expectSize); _shellStream.Write(_bufferData, 0, _bufferData.Length); } diff --git a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferNotEmptyAndWriteMoreBytesThanBufferCanContain.cs b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferNotEmptyAndWriteMoreBytesThanBufferCanContain.cs index 20436c56f..d638d2188 100644 --- a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferNotEmptyAndWriteMoreBytesThanBufferCanContain.cs +++ b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferNotEmptyAndWriteMoreBytesThanBufferCanContain.cs @@ -24,6 +24,7 @@ public class ShellStreamTest_Write_WriteBufferNotEmptyAndWriteMoreBytesThanBuffe private Dictionary _terminalModes; private ShellStream _shellStream; private int _bufferSize; + private int _expectSize; private byte[] _data; private int _offset; @@ -44,12 +45,13 @@ 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(); + _widthColumns = (uint)random.Next(); + _heightRows = (uint)random.Next(); + _widthPixels = (uint)random.Next(); + _heightPixels = (uint)random.Next(); _terminalModes = new Dictionary(); _bufferSize = random.Next(100, 1000); + _expectSize = random.Next(100, 1000); _bufferData = CryptoAbstraction.GenerateRandom(_bufferSize - 60); _data = CryptoAbstraction.GenerateRandom(_bufferSize - _bufferData.Length + random.Next(1, 10)); @@ -111,7 +113,8 @@ private void Arrange() _widthPixels, _heightPixels, _terminalModes, - _bufferSize); + _bufferSize, + _expectSize); _shellStream.Write(_bufferData, 0, _bufferData.Length); } @@ -130,7 +133,7 @@ public void BufferShouldBeSentToServer() [TestMethod] public void FlushShouldSendRemainingBytesInBufferToServer() { - var expectedBytesSent = _data.Take(_bufferSize - _bufferData.Length, _data.Length + _bufferData.Length - _bufferSize); + var expectedBytesSent = _data.Take(_bufferSize - _bufferData.Length, _data.Length + _bufferData.Length - _bufferSize); byte[] actualBytesSent = null; _channelSessionMock.InSequence(_mockSequence) diff --git a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferNotEmptyAndWriteZeroBytes.cs b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferNotEmptyAndWriteZeroBytes.cs index 411fae02c..55a7e91d9 100644 --- a/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferNotEmptyAndWriteZeroBytes.cs +++ b/src/Renci.SshNet.Tests/Classes/ShellStreamTest_Write_WriteBufferNotEmptyAndWriteZeroBytes.cs @@ -23,6 +23,7 @@ public class ShellStreamTest_Write_WriteBufferNotEmptyAndWriteZeroBytes private Dictionary _terminalModes; private ShellStream _shellStream; private int _bufferSize; + private int _expectSize; private byte[] _data; private int _offset; @@ -48,6 +49,7 @@ private void SetupData() _heightPixels = (uint)random.Next(); _terminalModes = new Dictionary(); _bufferSize = random.Next(100, 1000); + _expectSize = random.Next(100, 1000); _bufferData = CryptoAbstraction.GenerateRandom(_bufferSize - 60); _data = new byte[0]; @@ -103,7 +105,8 @@ private void Arrange() _widthPixels, _heightPixels, _terminalModes, - _bufferSize); + _bufferSize, + _expectSize); _shellStream.Write(_bufferData, 0, _bufferData.Length); } diff --git a/src/Renci.SshNet.Tests/Classes/SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWidthAndHeightAndBufferSizeAndTerminalModes_Connected.cs b/src/Renci.SshNet.Tests/Classes/SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWidthAndHeightAndBufferSizeAndTerminalModes_Connected.cs index df1ea835f..5babf6136 100644 --- a/src/Renci.SshNet.Tests/Classes/SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWidthAndHeightAndBufferSizeAndTerminalModes_Connected.cs +++ b/src/Renci.SshNet.Tests/Classes/SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWidthAndHeightAndBufferSizeAndTerminalModes_Connected.cs @@ -19,6 +19,7 @@ public class SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWid private uint _heightPixels; private Dictionary _terminalModes; private int _bufferSize; + private int _expectSize; private ShellStream _expected; private ShellStream _actual; @@ -29,12 +30,13 @@ protected override void SetupData() _connectionInfo = new ConnectionInfo("host", "user", new NoneAuthenticationMethod("userauth")); _terminalName = random.Next().ToString(); - _widthColumns = (uint) random.Next(); - _heightRows = (uint) random.Next(); - _widthPixels = (uint) random.Next(); - _heightPixels = (uint) random.Next(); + _widthColumns = (uint)random.Next(); + _heightRows = (uint)random.Next(); + _widthPixels = (uint)random.Next(); + _heightPixels = (uint)random.Next(); _terminalModes = new Dictionary(); _bufferSize = random.Next(100, 1000); + _expectSize = random.Next(100, 1000); _expected = CreateShellStream(); } @@ -59,7 +61,8 @@ protected override void SetupMocks() _widthPixels, _heightPixels, _terminalModes, - _bufferSize)) + _bufferSize, + _expectSize)) .Returns(_expected); } @@ -92,7 +95,8 @@ public void CreateShellStreamOnServiceFactoryShouldBeInvokedOnce() _widthPixels, _heightPixels, _terminalModes, - _bufferSize), + _bufferSize, + _expectSize), Times.Once); } @@ -130,6 +134,7 @@ private ShellStream CreateShellStream() _widthPixels, _heightPixels, _terminalModes, + 1, 1); } } diff --git a/src/Renci.SshNet.Tests/Classes/SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWidthAndHeightAndBufferSize_Connected.cs b/src/Renci.SshNet.Tests/Classes/SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWidthAndHeightAndBufferSize_Connected.cs index 337f9bce3..e8ff88789 100644 --- a/src/Renci.SshNet.Tests/Classes/SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWidthAndHeightAndBufferSize_Connected.cs +++ b/src/Renci.SshNet.Tests/Classes/SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWidthAndHeightAndBufferSize_Connected.cs @@ -18,6 +18,7 @@ public class SshClientTest_CreateShellStream_TerminalNameAndColumnsAndRowsAndWid private uint _widthPixels; private uint _heightPixels; private int _bufferSize; + private int _expectSize; private ShellStream _expected; private ShellStream _actual; @@ -33,6 +34,7 @@ protected override void SetupData() _widthPixels = (uint)random.Next(); _heightPixels = (uint)random.Next(); _bufferSize = random.Next(100, 1000); + _expectSize = random.Next(100, 1000); _expected = CreateShellStream(); } @@ -57,7 +59,8 @@ protected override void SetupMocks() _widthPixels, _heightPixels, null, - _bufferSize)) + _bufferSize, + _expectSize)) .Returns(_expected); } @@ -89,7 +92,8 @@ public void CreateShellStreamOnServiceFactoryShouldBeInvokedOnce() _widthPixels, _heightPixels, null, - _bufferSize), + _bufferSize, + _expectSize), Times.Once); } @@ -127,6 +131,7 @@ private ShellStream CreateShellStream() _widthPixels, _heightPixels, null, + 1, 1); } } diff --git a/src/Renci.SshNet/IServiceFactory.cs b/src/Renci.SshNet/IServiceFactory.cs index dad93e237..ccdc529a0 100644 --- a/src/Renci.SshNet/IServiceFactory.cs +++ b/src/Renci.SshNet/IServiceFactory.cs @@ -77,8 +77,9 @@ internal partial interface IServiceFactory /// The terminal width in rows. /// The terminal height in pixels. /// The terminal height in pixels. - /// Size of the buffer. /// The terminal mode values. + /// Size of the buffer. + /// Size of the expect buffer. /// /// The created instance. /// @@ -100,7 +101,8 @@ ShellStream CreateShellStream(ISession session, uint width, uint height, IDictionary terminalModeValues, - int bufferSize); + int bufferSize, + int expectSize); /// /// Creates an that encloses a path in double quotes, and escapes diff --git a/src/Renci.SshNet/ServiceFactory.cs b/src/Renci.SshNet/ServiceFactory.cs index f04e6116d..100a70d80 100644 --- a/src/Renci.SshNet/ServiceFactory.cs +++ b/src/Renci.SshNet/ServiceFactory.cs @@ -132,7 +132,7 @@ public ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSe { var fileAttributes = sftpSession.EndLStat(statAsyncResult); fileSize = fileAttributes.Size; - maxPendingReads = Math.Min(10, (int) Math.Ceiling((double) fileAttributes.Size / chunkSize) + 1); + maxPendingReads = Math.Min(10, (int)Math.Ceiling((double)fileAttributes.Size / chunkSize) + 1); } catch (SshException ex) { @@ -161,6 +161,7 @@ public ISftpResponseFactory CreateSftpResponseFactory() /// The terminal height in pixels. /// The terminal mode values. /// The size of the buffer. + /// The size of the expect buffer. /// /// The created instance. /// @@ -175,9 +176,9 @@ public ISftpResponseFactory CreateSftpResponseFactory() /// to the drawable area of the window. /// /// - public ShellStream CreateShellStream(ISession session, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary terminalModeValues, int bufferSize) + public ShellStream CreateShellStream(ISession session, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary terminalModeValues, int bufferSize, int expectSize) { - return new ShellStream(session, terminalName, columns, rows, width, height, terminalModeValues, bufferSize); + return new ShellStream(session, terminalName, columns, rows, width, height, terminalModeValues, bufferSize, expectSize); } /// diff --git a/src/Renci.SshNet/ShellStream.cs b/src/Renci.SshNet/ShellStream.cs index 3274fe19c..6061a2260 100644 --- a/src/Renci.SshNet/ShellStream.cs +++ b/src/Renci.SshNet/ShellStream.cs @@ -21,6 +21,8 @@ public class ShellStream : Stream private readonly Encoding _encoding; private readonly int _bufferSize; private readonly Queue _incoming; + private readonly int _expectSize; + private readonly Queue _expect; private readonly Queue _outgoing; private IChannelSession _channel; private AutoResetEvent _dataReceived = new AutoResetEvent(false); @@ -75,15 +77,18 @@ internal int BufferSize /// The terminal height in pixels. /// The terminal mode values. /// The size of the buffer. + /// The size of the expect buffer. /// The channel could not be opened. /// The pseudo-terminal request was not accepted by the server. /// The request to start a shell was not accepted by the server. - internal ShellStream(ISession session, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary terminalModeValues, int bufferSize) + internal ShellStream(ISession session, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary terminalModeValues, int bufferSize, int expectSize) { _encoding = session.ConnectionInfo.Encoding; _session = session; _bufferSize = bufferSize; _incoming = new Queue(); + _expectSize = expectSize; + _expect = new Queue(_expectSize); _outgoing = new Queue(); _channel = _session.CreateChannelSession(); @@ -221,6 +226,8 @@ public override int Read(byte[] buffer, int offset, int count) { for (; i < count && _incoming.Count > 0; i++) { + if (_expect.Count == _incoming.Count) + _expect.Dequeue(); buffer[offset + i] = _incoming.Dequeue(); } } @@ -300,34 +307,36 @@ public void Expect(params ExpectAction[] expectActions) public void Expect(TimeSpan timeout, params ExpectAction[] expectActions) { var expectedFound = false; - var text = string.Empty; + var matchText = string.Empty; + var returnText = string.Empty; do { lock (_incoming) { - if (_incoming.Count > 0) - { - text = _encoding.GetString(_incoming.ToArray(), 0, _incoming.Count); - } + if (_expect.Count > 0) + matchText = Encoding.ASCII.GetString(_expect.ToArray(), 0, _expect.Count); - if (text.Length > 0) + if (matchText.Length > 0) { foreach (var expectAction in expectActions) { - var match = expectAction.Expect.Match(text); + var match = expectAction.Expect.Match(matchText); if (match.Success) { - var result = text.Substring(0, match.Index + match.Length); + var returnLength = (_incoming.Count - _expect.Count) + match.Index + match.Length; + returnText = _encoding.GetString(_incoming.ToArray(), 0, returnLength); - for (var i = 0; i < match.Index + match.Length && _incoming.Count > 0; i++) + // Remove processed items from the queue + for (var i = 0; i < returnLength && _incoming.Count > 0; i++) { - // Remove processed items from the queue + if (_expect.Count == _incoming.Count) + _expect.Dequeue(); _incoming.Dequeue(); } - expectAction.Action(result); + expectAction.Action(returnText); expectedFound = true; } } @@ -339,9 +348,7 @@ public void Expect(TimeSpan timeout, params ExpectAction[] expectActions) if (timeout.Ticks > 0) { if (!_dataReceived.WaitOne(timeout)) - { return; - } } else { @@ -403,7 +410,8 @@ public IAsyncResult BeginExpect(AsyncCallback callback, object state, params Exp /// public IAsyncResult BeginExpect(TimeSpan timeout, AsyncCallback callback, object state, params ExpectAction[] expectActions) { - var text = string.Empty; + var matchText = string.Empty; + var returnText = string.Empty; // Create new AsyncResult object var asyncResult = new ExpectAsyncResult(callback, state); @@ -414,40 +422,38 @@ public IAsyncResult BeginExpect(TimeSpan timeout, AsyncCallback callback, object string expectActionResult = null; try { - do { lock (_incoming) { + if (_expect.Count > 0) + matchText = Encoding.ASCII.GetString(_expect.ToArray(), 0, _expect.Count); - if (_incoming.Count > 0) - { - text = _encoding.GetString(_incoming.ToArray(), 0, _incoming.Count); - } - - if (text.Length > 0) + if (matchText.Length > 0) { foreach (var expectAction in expectActions) { - var match = expectAction.Expect.Match(text); + var match = expectAction.Expect.Match(matchText); if (match.Success) { - var result = text.Substring(0, match.Index + match.Length); + var returnLength = (_incoming.Count - _expect.Count) + match.Index + match.Length; + returnText = _encoding.GetString(_incoming.ToArray(), 0, returnLength); - for (var i = 0; i < match.Index + match.Length && _incoming.Count > 0; i++) + // Remove processed items from the queue + for (var i = 0; i < returnLength && _incoming.Count > 0; i++) { - // Remove processed items from the queue + if (_expect.Count == _incoming.Count) + _expect.Dequeue(); _incoming.Dequeue(); } - expectAction.Action(result); + expectAction.Action(returnText); if (callback != null) - { callback(asyncResult); - } - expectActionResult = result; + + expectActionResult = returnText; } } } @@ -461,9 +467,7 @@ public IAsyncResult BeginExpect(TimeSpan timeout, AsyncCallback callback, object if (!_dataReceived.WaitOne(timeout)) { if (callback != null) - { callback(asyncResult); - } break; } } @@ -548,24 +552,28 @@ public string Expect(Regex regex) /// public string Expect(Regex regex, TimeSpan timeout) { - var text = string.Empty; + var matchText = string.Empty; + var returnText = string.Empty; while (true) { lock (_incoming) { - if (_incoming.Count > 0) - { - text = _encoding.GetString(_incoming.ToArray(), 0, _incoming.Count); - } + if (_expect.Count > 0) + matchText = Encoding.ASCII.GetString(_expect.ToArray(), 0, _expect.Count); - var match = regex.Match(text); + var match = regex.Match(matchText); if (match.Success) { + var returnLength = (_incoming.Count - _expect.Count) + match.Index + match.Length; + returnText = _encoding.GetString(_incoming.ToArray(), 0, returnLength); + // Remove processed items from the queue - for (var i = 0; i < match.Index + match.Length && _incoming.Count > 0; i++) + for (var i = 0; i < returnLength && _incoming.Count > 0; i++) { + if (_expect.Count == _incoming.Count) + _expect.Dequeue(); _incoming.Dequeue(); } break; @@ -575,9 +583,7 @@ public string Expect(Regex regex, TimeSpan timeout) if (timeout.Ticks > 0) { if (!_dataReceived.WaitOne(timeout)) - { return null; - } } else { @@ -586,7 +592,7 @@ public string Expect(Regex regex, TimeSpan timeout) } - return text; + return returnText; } /// @@ -631,7 +637,11 @@ public string ReadLine(TimeSpan timeout) // remove processed bytes from the queue for (var i = 0; i < bytesProcessed; i++) + { + if (_expect.Count == _incoming.Count) + _expect.Dequeue(); _incoming.Dequeue(); + } break; } @@ -667,6 +677,7 @@ public string Read() lock (_incoming) { text = _encoding.GetString(_incoming.ToArray(), 0, _incoming.Count); + _expect.Clear(); _incoming.Clear(); } @@ -781,7 +792,12 @@ private void Channel_DataReceived(object sender, ChannelDataEventArgs e) lock (_incoming) { foreach (var b in e.Data) + { _incoming.Enqueue(b); + if (_expect.Count == _expectSize) + _expect.Dequeue(); + _expect.Enqueue(b); + } } if (_dataReceived != null) diff --git a/src/Renci.SshNet/SshClient.cs b/src/Renci.SshNet/SshClient.cs index 49c9ff84b..f5a9f739e 100644 --- a/src/Renci.SshNet/SshClient.cs +++ b/src/Renci.SshNet/SshClient.cs @@ -429,7 +429,7 @@ public Shell CreateShell(Encoding encoding, string input, Stream output, Stream /// public ShellStream CreateShellStream(string terminalName, uint columns, uint rows, uint width, uint height, int bufferSize) { - return CreateShellStream(terminalName, columns, rows, width, height, bufferSize, null); + return CreateShellStream(terminalName, columns, rows, width, height, bufferSize, null, (int)columns); } /// @@ -452,15 +452,45 @@ public ShellStream CreateShellStream(string terminalName, uint columns, uint row /// You can get a detailed list of these cababilities by using the ‘infocmp’ command. /// /// - /// The column/row dimensions override the pixel dimensions(when non-zero). Pixel dimensions refer + /// The column/row dimensions override the pixel dimensions(when nonzero). Pixel dimensions refer /// to the drawable area of the window. /// /// public ShellStream CreateShellStream(string terminalName, uint columns, uint rows, uint width, uint height, int bufferSize, IDictionary terminalModeValues) + { + return CreateShellStream(terminalName, columns, rows, width, height, bufferSize, terminalModeValues, (int)columns); + } + + /// + /// Creates the shell stream. + /// + /// The TERM environment variable. + /// The terminal width in columns. + /// The terminal width in rows. + /// The terminal height in pixels. + /// The terminal height in pixels. + /// The size of the buffer. + /// The terminal mode values. + /// The size of the expect buffer. + /// + /// The created instance. + /// + /// Client is not connected. + /// + /// + /// The TERM environment variable contains an identifier for the text window's capabilities. + /// You can get a detailed list of these cababilities by using the ‘infocmp’ command. + /// + /// + /// The column/row dimensions override the pixel dimensions(when non-zero). Pixel dimensions refer + /// to the drawable area of the window. + /// + /// + public ShellStream CreateShellStream(string terminalName, uint columns, uint rows, uint width, uint height, int bufferSize, IDictionary terminalModeValues, int expectSize) { EnsureSessionIsOpen(); - return ServiceFactory.CreateShellStream(Session, terminalName, columns, rows, width, height, terminalModeValues, bufferSize); + return ServiceFactory.CreateShellStream(Session, terminalName, columns, rows, width, height, terminalModeValues, bufferSize, expectSize); } ///