Skip to content

Commit

Permalink
Ensure read buffer is large enough.
Browse files Browse the repository at this point in the history
  • Loading branch information
drieseng committed Sep 17, 2017
1 parent c3d43b6 commit a353f95
Showing 1 changed file with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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));
}

Expand All @@ -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<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null))
.Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((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<byte[]>(),
0,
bytesToWrite.Length,
It.IsAny<AutoResetEvent>(),
null))
.Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>(
(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<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null), Times.Once);
SftpSessionMock.Verify(p => p.RequestWrite(_handle,
(uint) (_readBytes1.Length + _readBytes2.Length),
It.IsAny<byte[]>(),
0,
bytesToWrite.Length,
It.IsAny<AutoResetEvent>(),
null),
Times.Once);
SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
}
}
}
}

0 comments on commit a353f95

Please sign in to comment.