From a353f95ebfcb9bbf79a5a9d89ebc78433b3e378a Mon Sep 17 00:00:00 2001 From: Gert Driesen Date: Sun, 17 Sep 2017 18:05:37 +0200 Subject: [PATCH] Ensure read buffer is large enough. --- ...eadBuffer_NewLengthGreatherThanPosition.cs | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthGreatherThanPosition.cs b/src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthGreatherThanPosition.cs index dd6cefa7b..aee4b2762 100644 --- a/src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthGreatherThanPosition.cs +++ b/src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthGreatherThanPosition.cs @@ -43,11 +43,11 @@ protected override void SetupData() _path = random.Next().ToString(CultureInfo.InvariantCulture); _handle = GenerateRandom(random.Next(2, 6), random); _bufferSize = (uint) random.Next(1, 1000); - _readBufferSize = (uint) random.Next(1, 1000); - _writeBufferSize = (uint) random.Next(100, 1000); _readBytes1 = new byte[5]; _readBytes2 = new byte[random.Next(1, 3)]; _actualReadBytes = GenerateRandom(_readBytes1.Length + _readBytes2.Length + 2, random); // server returns more bytes than the caller requested + _readBufferSize = (uint) random.Next(_actualReadBytes.Length, _actualReadBytes.Length * 2); + _writeBufferSize = (uint) random.Next(100, 1000); _length = _readBytes1.Length + _readBytes2.Length + 5; _fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC") @@ -99,7 +99,11 @@ protected override void Arrange() { base.Arrange(); - _sftpFileStream = new SftpFileStream(SftpSessionMock.Object, _path, FileMode.Open, FileAccess.ReadWrite, (int)_bufferSize); + _sftpFileStream = new SftpFileStream(SftpSessionMock.Object, + _path, + FileMode.Open, + FileAccess.ReadWrite, + (int) _bufferSize); _sftpFileStream.Read(_readBytes1, 0, _readBytes1.Length); _sftpFileStream.Read(_readBytes2, 0, _readBytes2.Length); // this will return bytes from the buffer } @@ -143,14 +147,19 @@ public void ReadShouldReadStartFromSamePositionAsBeforeSetLength() { SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestRead(_handle, (uint) (_readBytes1.Length + _readBytes2.Length), _readBufferSize)) - .Returns(new byte[] { 0x0f }); + .Setup(p => p.RequestRead(_handle, + (uint) (_readBytes1.Length + _readBytes2.Length), + _readBufferSize)) + .Returns(new byte[] {0x0f}); var byteRead = _sftpFileStream.ReadByte(); Assert.AreEqual(0x0f, byteRead); - SftpSessionMock.Verify(p => p.RequestRead(_handle, (uint)(_readBytes1.Length + _readBytes2.Length), _readBufferSize), Times.Once); + SftpSessionMock.Verify(p => p.RequestRead(_handle, + (uint) (_readBytes1.Length + _readBytes2.Length), + _readBufferSize), + Times.Once); SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4)); } @@ -162,20 +171,34 @@ public void WriteShouldStartFromSamePositionAsBeforeSetLength() SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true); SftpSessionMock.InSequence(_sequence) - .Setup(p => p.RequestWrite(_handle, (uint) (_readBytes1.Length + _readBytes2.Length), It.IsAny(), 0, bytesToWrite.Length, It.IsAny(), null)) - .Callback>((handle, serverOffset, data, offset, length, wait, writeCompleted) => - { - bytesWritten = data.Take(offset, length); - wait.Set(); - }); + .Setup(p => p.RequestWrite(_handle, + (uint) (_readBytes1.Length + _readBytes2.Length), + It.IsAny(), + 0, + bytesToWrite.Length, + It.IsAny(), + null)) + .Callback>( + (handle, serverOffset, data, offset, length, wait, writeCompleted) => + { + bytesWritten = data.Take(offset, length); + wait.Set(); + }); _sftpFileStream.Write(bytesToWrite, 0, bytesToWrite.Length); Assert.IsNotNull(bytesWritten); CollectionAssert.AreEqual(bytesToWrite, bytesWritten); - SftpSessionMock.Verify(p => p.RequestWrite(_handle, (uint)(_readBytes1.Length + _readBytes2.Length), It.IsAny(), 0, bytesToWrite.Length, It.IsAny(), null), Times.Once); + SftpSessionMock.Verify(p => p.RequestWrite(_handle, + (uint) (_readBytes1.Length + _readBytes2.Length), + It.IsAny(), + 0, + bytesToWrite.Length, + It.IsAny(), + null), + Times.Once); SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4)); } } -} +} \ No newline at end of file